vnode.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Basic vnode support functions.
  31. */
  32. #include <types.h>
  33. #include <kern/errno.h>
  34. #include <lib.h>
  35. #include <synch.h>
  36. #include <vfs.h>
  37. #include <vnode.h>
  38. /*
  39. * Initialize an abstract vnode.
  40. * Invoked by VOP_INIT.
  41. */
  42. int
  43. vnode_init(struct vnode *vn, const struct vnode_ops *ops,
  44. struct fs *fs, void *fsdata)
  45. {
  46. KASSERT(vn!=NULL);
  47. KASSERT(ops!=NULL);
  48. vn->vn_ops = ops;
  49. vn->vn_refcount = 1;
  50. vn->vn_opencount = 0;
  51. vn->vn_fs = fs;
  52. vn->vn_data = fsdata;
  53. return 0;
  54. }
  55. /*
  56. * Destroy an abstract vnode.
  57. * Invoked by VOP_CLEANUP.
  58. */
  59. void
  60. vnode_cleanup(struct vnode *vn)
  61. {
  62. KASSERT(vn->vn_refcount==1);
  63. KASSERT(vn->vn_opencount==0);
  64. vn->vn_ops = NULL;
  65. vn->vn_refcount = 0;
  66. vn->vn_opencount = 0;
  67. vn->vn_fs = NULL;
  68. vn->vn_data = NULL;
  69. }
  70. /*
  71. * Increment refcount.
  72. * Called by VOP_INCREF.
  73. */
  74. void
  75. vnode_incref(struct vnode *vn)
  76. {
  77. KASSERT(vn != NULL);
  78. vfs_biglock_acquire();
  79. vn->vn_refcount++;
  80. vfs_biglock_release();
  81. }
  82. /*
  83. * Decrement refcount.
  84. * Called by VOP_DECREF.
  85. * Calls VOP_RECLAIM if the refcount hits zero.
  86. */
  87. void
  88. vnode_decref(struct vnode *vn)
  89. {
  90. int result;
  91. KASSERT(vn != NULL);
  92. vfs_biglock_acquire();
  93. KASSERT(vn->vn_refcount>0);
  94. if (vn->vn_refcount>1) {
  95. vn->vn_refcount--;
  96. }
  97. else {
  98. result = VOP_RECLAIM(vn);
  99. if (result != 0 && result != EBUSY) {
  100. // XXX: lame.
  101. kprintf("vfs: Warning: VOP_RECLAIM: %s\n",
  102. strerror(result));
  103. }
  104. }
  105. vfs_biglock_release();
  106. }
  107. /*
  108. * Increment the open count.
  109. * Called by VOP_INCOPEN.
  110. */
  111. void
  112. vnode_incopen(struct vnode *vn)
  113. {
  114. KASSERT(vn != NULL);
  115. vfs_biglock_acquire();
  116. vn->vn_opencount++;
  117. vfs_biglock_release();
  118. }
  119. /*
  120. * Decrement the open count.
  121. * Called by VOP_DECOPEN.
  122. */
  123. void
  124. vnode_decopen(struct vnode *vn)
  125. {
  126. int result;
  127. KASSERT(vn != NULL);
  128. vfs_biglock_acquire();
  129. KASSERT(vn->vn_opencount>0);
  130. vn->vn_opencount--;
  131. if (vn->vn_opencount > 0) {
  132. vfs_biglock_release();
  133. return;
  134. }
  135. result = VOP_CLOSE(vn);
  136. if (result) {
  137. // XXX: also lame.
  138. // The FS should do what it can to make sure this code
  139. // doesn't get reached...
  140. kprintf("vfs: Warning: VOP_CLOSE: %s\n", strerror(result));
  141. }
  142. vfs_biglock_release();
  143. }
  144. /*
  145. * Check for various things being valid.
  146. * Called before all VOP_* calls.
  147. */
  148. void
  149. vnode_check(struct vnode *v, const char *opstr)
  150. {
  151. vfs_biglock_acquire();
  152. if (v == NULL) {
  153. panic("vnode_check: vop_%s: null vnode\n", opstr);
  154. }
  155. if (v == (void *)0xdeadbeef) {
  156. panic("vnode_check: vop_%s: deadbeef vnode\n", opstr);
  157. }
  158. if (v->vn_ops == NULL) {
  159. panic("vnode_check: vop_%s: null ops pointer\n", opstr);
  160. }
  161. if (v->vn_ops == (void *)0xdeadbeef) {
  162. panic("vnode_check: vop_%s: deadbeef ops pointer\n", opstr);
  163. }
  164. if (v->vn_ops->vop_magic != VOP_MAGIC) {
  165. panic("vnode_check: vop_%s: ops with bad magic number %lx\n",
  166. opstr, v->vn_ops->vop_magic);
  167. }
  168. // Device vnodes have null fs pointers.
  169. //if (v->vn_fs == NULL) {
  170. // panic("vnode_check: vop_%s: null fs pointer\n", opstr);
  171. //}
  172. if (v->vn_fs == (void *)0xdeadbeef) {
  173. panic("vnode_check: vop_%s: deadbeef fs pointer\n", opstr);
  174. }
  175. if (v->vn_refcount < 0) {
  176. panic("vnode_check: vop_%s: negative refcount %d\n", opstr,
  177. v->vn_refcount);
  178. }
  179. else if (v->vn_refcount == 0 && strcmp(opstr, "reclaim")) {
  180. panic("vnode_check: vop_%s: zero refcount\n", opstr);
  181. }
  182. else if (v->vn_refcount > 0x100000) {
  183. kprintf("vnode_check: vop_%s: warning: large refcount %d\n",
  184. opstr, v->vn_refcount);
  185. }
  186. if (v->vn_opencount < 0) {
  187. panic("vnode_check: vop_%s: negative opencount %d\n", opstr,
  188. v->vn_opencount);
  189. }
  190. else if (v->vn_opencount > 0x100000) {
  191. kprintf("vnode_check: vop_%s: warning: large opencount %d\n",
  192. opstr, v->vn_opencount);
  193. }
  194. vfs_biglock_release();
  195. }