xfs_vnode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "xfs_vnodeops.h"
  20. #include "xfs_bmap_btree.h"
  21. #include "xfs_inode.h"
  22. uint64_t vn_generation; /* vnode generation number */
  23. DEFINE_SPINLOCK(vnumber_lock);
  24. /*
  25. * Dedicated vnode inactive/reclaim sync semaphores.
  26. * Prime number of hash buckets since address is used as the key.
  27. */
  28. #define NVSYNC 37
  29. #define vptosync(v) (&vsync[((unsigned long)v) % NVSYNC])
  30. static wait_queue_head_t vsync[NVSYNC];
  31. void
  32. vn_init(void)
  33. {
  34. int i;
  35. for (i = 0; i < NVSYNC; i++)
  36. init_waitqueue_head(&vsync[i]);
  37. }
  38. void
  39. vn_iowait(
  40. bhv_vnode_t *vp)
  41. {
  42. wait_queue_head_t *wq = vptosync(vp);
  43. wait_event(*wq, (atomic_read(&vp->v_iocount) == 0));
  44. }
  45. void
  46. vn_iowake(
  47. bhv_vnode_t *vp)
  48. {
  49. if (atomic_dec_and_test(&vp->v_iocount))
  50. wake_up(vptosync(vp));
  51. }
  52. /*
  53. * Volume managers supporting multiple paths can send back ENODEV when the
  54. * final path disappears. In this case continuing to fill the page cache
  55. * with dirty data which cannot be written out is evil, so prevent that.
  56. */
  57. void
  58. vn_ioerror(
  59. bhv_vnode_t *vp,
  60. int error,
  61. char *f,
  62. int l)
  63. {
  64. bhv_vfs_t *vfsp = vfs_from_sb(vp->v_inode.i_sb);
  65. if (unlikely(error == -ENODEV))
  66. bhv_vfs_force_shutdown(vfsp, SHUTDOWN_DEVICE_REQ, f, l);
  67. }
  68. bhv_vnode_t *
  69. vn_initialize(
  70. struct inode *inode)
  71. {
  72. bhv_vnode_t *vp = vn_from_inode(inode);
  73. XFS_STATS_INC(vn_active);
  74. XFS_STATS_INC(vn_alloc);
  75. spin_lock(&vnumber_lock);
  76. if (!++vn_generation) /* v_number shouldn't be zero */
  77. vn_generation++;
  78. vp->v_number = vn_generation;
  79. spin_unlock(&vnumber_lock);
  80. ASSERT(VN_CACHED(vp) == 0);
  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 = xfs_getattr(xfs_vtoi(vp), vattr, 0);
  135. if (likely(!error)) {
  136. vn_revalidate_core(vp, vattr);
  137. xfs_iflags_clear(xfs_vtoi(vp), XFS_IMODIFIED);
  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. inode = igrab(vn_to_inode(vp));
  158. ASSERT(inode);
  159. return vp;
  160. }
  161. #ifdef XFS_VNODE_TRACE
  162. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  163. ktrace_enter( (vp)->v_trace, \
  164. /* 0 */ (void *)(__psint_t)(vk), \
  165. /* 1 */ (void *)(s), \
  166. /* 2 */ (void *)(__psint_t) line, \
  167. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  168. /* 4 */ (void *)(ra), \
  169. /* 5 */ NULL, \
  170. /* 6 */ (void *)(__psint_t)current_cpu(), \
  171. /* 7 */ (void *)(__psint_t)current_pid(), \
  172. /* 8 */ (void *)__return_address, \
  173. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  174. /*
  175. * Vnode tracing code.
  176. */
  177. void
  178. vn_trace_entry(bhv_vnode_t *vp, const char *func, inst_t *ra)
  179. {
  180. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  181. }
  182. void
  183. vn_trace_exit(bhv_vnode_t *vp, const char *func, inst_t *ra)
  184. {
  185. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  186. }
  187. void
  188. vn_trace_hold(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  189. {
  190. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  191. }
  192. void
  193. vn_trace_ref(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  194. {
  195. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  196. }
  197. void
  198. vn_trace_rele(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  199. {
  200. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  201. }
  202. #endif /* XFS_VNODE_TRACE */