《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.6 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. #include <defs.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <atomic.h>
  5. #include <vfs.h>
  6. #include <inode.h>
  7. #include <error.h>
  8. #include <assert.h>
  9. #include <kmalloc.h>
  10. /* *
  11. * __alloc_inode - alloc a inode structure and initialize in_type
  12. * */
  13. struct inode *
  14. __alloc_inode(int type) {
  15. struct inode *node;
  16. if ((node = kmalloc(sizeof(struct inode))) != NULL) {
  17. node->in_type = type;
  18. }
  19. return node;
  20. }
  21. /* *
  22. * inode_init - initialize a inode structure
  23. * invoked by vop_init
  24. * */
  25. void
  26. inode_init(struct inode *node, const struct inode_ops *ops, struct fs *fs) {
  27. node->ref_count = 0;
  28. node->open_count = 0;
  29. node->in_ops = ops, node->in_fs = fs;
  30. vop_ref_inc(node);
  31. }
  32. /* *
  33. * inode_kill - kill a inode structure
  34. * invoked by vop_kill
  35. * */
  36. void
  37. inode_kill(struct inode *node) {
  38. assert(inode_ref_count(node) == 0);
  39. assert(inode_open_count(node) == 0);
  40. kfree(node);
  41. }
  42. /* *
  43. * inode_ref_inc - increment ref_count
  44. * invoked by vop_ref_inc
  45. * */
  46. int
  47. inode_ref_inc(struct inode *node) {
  48. node->ref_count += 1;
  49. return node->ref_count;
  50. }
  51. /* *
  52. * inode_ref_dec - decrement ref_count
  53. * invoked by vop_ref_dec
  54. * calls vop_reclaim if the ref_count hits zero
  55. * */
  56. int
  57. inode_ref_dec(struct inode *node) {
  58. assert(inode_ref_count(node) > 0);
  59. int ref_count, ret;
  60. node->ref_count-= 1;
  61. ref_count = node->ref_count;
  62. if (ref_count == 0) {
  63. if ((ret = vop_reclaim(node)) != 0 && ret != -E_BUSY) {
  64. cprintf("vfs: warning: vop_reclaim: %e.\n", ret);
  65. }
  66. }
  67. return ref_count;
  68. }
  69. /* *
  70. * inode_open_inc - increment the open_count
  71. * invoked by vop_open_inc
  72. * */
  73. int
  74. inode_open_inc(struct inode *node) {
  75. node->open_count += 1;
  76. return node->open_count;
  77. }
  78. /* *
  79. * inode_open_dec - decrement the open_count
  80. * invoked by vop_open_dec
  81. * calls vop_close if the open_count hits zero
  82. * */
  83. int
  84. inode_open_dec(struct inode *node) {
  85. assert(inode_open_count(node) > 0);
  86. int open_count, ret;
  87. node->open_count -= 1;
  88. open_count = node->open_count;
  89. if (open_count == 0) {
  90. if ((ret = vop_close(node)) != 0) {
  91. cprintf("vfs: warning: vop_close: %e.\n", ret);
  92. }
  93. }
  94. return open_count;
  95. }
  96. /* *
  97. * inode_check - check the various things being valid
  98. * called before all vop_* calls
  99. * */
  100. void
  101. inode_check(struct inode *node, const char *opstr) {
  102. assert(node != NULL && node->in_ops != NULL);
  103. assert(node->in_ops->vop_magic == VOP_MAGIC);
  104. int ref_count = inode_ref_count(node), open_count = inode_open_count(node);
  105. assert(ref_count >= open_count && open_count >= 0);
  106. assert(ref_count < MAX_INODE_COUNT && open_count < MAX_INODE_COUNT);
  107. }