xfs_vnode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. uint64_t vn_generation; /* vnode generation number */
  20. DEFINE_SPINLOCK(vnumber_lock);
  21. /*
  22. * Dedicated vnode inactive/reclaim sync semaphores.
  23. * Prime number of hash buckets since address is used as the key.
  24. */
  25. #define NVSYNC 37
  26. #define vptosync(v) (&vsync[((unsigned long)v) % NVSYNC])
  27. STATIC wait_queue_head_t vsync[NVSYNC];
  28. void
  29. vn_init(void)
  30. {
  31. int i;
  32. for (i = 0; i < NVSYNC; i++)
  33. init_waitqueue_head(&vsync[i]);
  34. }
  35. void
  36. vn_iowait(
  37. bhv_vnode_t *vp)
  38. {
  39. wait_queue_head_t *wq = vptosync(vp);
  40. wait_event(*wq, (atomic_read(&vp->v_iocount) == 0));
  41. }
  42. void
  43. vn_iowake(
  44. bhv_vnode_t *vp)
  45. {
  46. if (atomic_dec_and_test(&vp->v_iocount))
  47. wake_up(vptosync(vp));
  48. }
  49. /*
  50. * Volume managers supporting multiple paths can send back ENODEV when the
  51. * final path disappears. In this case continuing to fill the page cache
  52. * with dirty data which cannot be written out is evil, so prevent that.
  53. */
  54. void
  55. vn_ioerror(
  56. bhv_vnode_t *vp,
  57. int error,
  58. char *f,
  59. int l)
  60. {
  61. if (unlikely(error == -ENODEV))
  62. bhv_vfs_force_shutdown(vp->v_vfsp, SHUTDOWN_DEVICE_REQ, f, l);
  63. }
  64. bhv_vnode_t *
  65. vn_initialize(
  66. struct inode *inode)
  67. {
  68. bhv_vnode_t *vp = vn_from_inode(inode);
  69. XFS_STATS_INC(vn_active);
  70. XFS_STATS_INC(vn_alloc);
  71. vp->v_flag = VMODIFIED;
  72. spinlock_init(&vp->v_lock, "v_lock");
  73. spin_lock(&vnumber_lock);
  74. if (!++vn_generation) /* v_number shouldn't be zero */
  75. vn_generation++;
  76. vp->v_number = vn_generation;
  77. spin_unlock(&vnumber_lock);
  78. ASSERT(VN_CACHED(vp) == 0);
  79. /* Initialize the first behavior and the behavior chain head. */
  80. vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode");
  81. atomic_set(&vp->v_iocount, 0);
  82. #ifdef XFS_VNODE_TRACE
  83. vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
  84. #endif /* XFS_VNODE_TRACE */
  85. vn_trace_exit(vp, __FUNCTION__, (inst_t *)__return_address);
  86. return vp;
  87. }
  88. /*
  89. * Revalidate the Linux inode from the vattr.
  90. * Note: i_size _not_ updated; we must hold the inode
  91. * semaphore when doing that - callers responsibility.
  92. */
  93. void
  94. vn_revalidate_core(
  95. bhv_vnode_t *vp,
  96. bhv_vattr_t *vap)
  97. {
  98. struct inode *inode = vn_to_inode(vp);
  99. inode->i_mode = vap->va_mode;
  100. inode->i_nlink = vap->va_nlink;
  101. inode->i_uid = vap->va_uid;
  102. inode->i_gid = vap->va_gid;
  103. inode->i_blocks = vap->va_nblocks;
  104. inode->i_mtime = vap->va_mtime;
  105. inode->i_ctime = vap->va_ctime;
  106. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  107. inode->i_flags |= S_IMMUTABLE;
  108. else
  109. inode->i_flags &= ~S_IMMUTABLE;
  110. if (vap->va_xflags & XFS_XFLAG_APPEND)
  111. inode->i_flags |= S_APPEND;
  112. else
  113. inode->i_flags &= ~S_APPEND;
  114. if (vap->va_xflags & XFS_XFLAG_SYNC)
  115. inode->i_flags |= S_SYNC;
  116. else
  117. inode->i_flags &= ~S_SYNC;
  118. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  119. inode->i_flags |= S_NOATIME;
  120. else
  121. inode->i_flags &= ~S_NOATIME;
  122. }
  123. /*
  124. * Revalidate the Linux inode from the vnode.
  125. */
  126. int
  127. __vn_revalidate(
  128. bhv_vnode_t *vp,
  129. bhv_vattr_t *vattr)
  130. {
  131. int error;
  132. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  133. vattr->va_mask = XFS_AT_STAT | XFS_AT_XFLAGS;
  134. error = bhv_vop_getattr(vp, vattr, 0, NULL);
  135. if (likely(!error)) {
  136. vn_revalidate_core(vp, vattr);
  137. VUNMODIFY(vp);
  138. }
  139. return -error;
  140. }
  141. int
  142. vn_revalidate(
  143. bhv_vnode_t *vp)
  144. {
  145. bhv_vattr_t vattr;
  146. return __vn_revalidate(vp, &vattr);
  147. }
  148. /*
  149. * Add a reference to a referenced vnode.
  150. */
  151. bhv_vnode_t *
  152. vn_hold(
  153. bhv_vnode_t *vp)
  154. {
  155. struct inode *inode;
  156. XFS_STATS_INC(vn_hold);
  157. VN_LOCK(vp);
  158. inode = igrab(vn_to_inode(vp));
  159. ASSERT(inode);
  160. VN_UNLOCK(vp, 0);
  161. return vp;
  162. }
  163. #ifdef XFS_VNODE_TRACE
  164. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  165. ktrace_enter( (vp)->v_trace, \
  166. /* 0 */ (void *)(__psint_t)(vk), \
  167. /* 1 */ (void *)(s), \
  168. /* 2 */ (void *)(__psint_t) line, \
  169. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  170. /* 4 */ (void *)(ra), \
  171. /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
  172. /* 6 */ (void *)(__psint_t)current_cpu(), \
  173. /* 7 */ (void *)(__psint_t)current_pid(), \
  174. /* 8 */ (void *)__return_address, \
  175. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  176. /*
  177. * Vnode tracing code.
  178. */
  179. void
  180. vn_trace_entry(bhv_vnode_t *vp, const char *func, inst_t *ra)
  181. {
  182. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  183. }
  184. void
  185. vn_trace_exit(bhv_vnode_t *vp, const char *func, inst_t *ra)
  186. {
  187. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  188. }
  189. void
  190. vn_trace_hold(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  191. {
  192. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  193. }
  194. void
  195. vn_trace_ref(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  196. {
  197. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  198. }
  199. void
  200. vn_trace_rele(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  201. {
  202. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  203. }
  204. #endif /* XFS_VNODE_TRACE */