xfs_vnode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. vp->v_flag = VMODIFIED;
  76. spinlock_init(&vp->v_lock, "v_lock");
  77. spin_lock(&vnumber_lock);
  78. if (!++vn_generation) /* v_number shouldn't be zero */
  79. vn_generation++;
  80. vp->v_number = vn_generation;
  81. spin_unlock(&vnumber_lock);
  82. ASSERT(VN_CACHED(vp) == 0);
  83. atomic_set(&vp->v_iocount, 0);
  84. #ifdef XFS_VNODE_TRACE
  85. vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
  86. #endif /* XFS_VNODE_TRACE */
  87. vn_trace_exit(vp, __FUNCTION__, (inst_t *)__return_address);
  88. return vp;
  89. }
  90. /*
  91. * Revalidate the Linux inode from the vattr.
  92. * Note: i_size _not_ updated; we must hold the inode
  93. * semaphore when doing that - callers responsibility.
  94. */
  95. void
  96. vn_revalidate_core(
  97. bhv_vnode_t *vp,
  98. bhv_vattr_t *vap)
  99. {
  100. struct inode *inode = vn_to_inode(vp);
  101. inode->i_mode = vap->va_mode;
  102. inode->i_nlink = vap->va_nlink;
  103. inode->i_uid = vap->va_uid;
  104. inode->i_gid = vap->va_gid;
  105. inode->i_blocks = vap->va_nblocks;
  106. inode->i_mtime = vap->va_mtime;
  107. inode->i_ctime = vap->va_ctime;
  108. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  109. inode->i_flags |= S_IMMUTABLE;
  110. else
  111. inode->i_flags &= ~S_IMMUTABLE;
  112. if (vap->va_xflags & XFS_XFLAG_APPEND)
  113. inode->i_flags |= S_APPEND;
  114. else
  115. inode->i_flags &= ~S_APPEND;
  116. if (vap->va_xflags & XFS_XFLAG_SYNC)
  117. inode->i_flags |= S_SYNC;
  118. else
  119. inode->i_flags &= ~S_SYNC;
  120. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  121. inode->i_flags |= S_NOATIME;
  122. else
  123. inode->i_flags &= ~S_NOATIME;
  124. }
  125. /*
  126. * Revalidate the Linux inode from the vnode.
  127. */
  128. int
  129. __vn_revalidate(
  130. bhv_vnode_t *vp,
  131. bhv_vattr_t *vattr)
  132. {
  133. int error;
  134. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  135. vattr->va_mask = XFS_AT_STAT | XFS_AT_XFLAGS;
  136. error = xfs_getattr(xfs_vtoi(vp), vattr, 0);
  137. if (likely(!error)) {
  138. vn_revalidate_core(vp, vattr);
  139. VUNMODIFY(vp);
  140. }
  141. return -error;
  142. }
  143. int
  144. vn_revalidate(
  145. bhv_vnode_t *vp)
  146. {
  147. bhv_vattr_t vattr;
  148. return __vn_revalidate(vp, &vattr);
  149. }
  150. /*
  151. * Add a reference to a referenced vnode.
  152. */
  153. bhv_vnode_t *
  154. vn_hold(
  155. bhv_vnode_t *vp)
  156. {
  157. struct inode *inode;
  158. XFS_STATS_INC(vn_hold);
  159. VN_LOCK(vp);
  160. inode = igrab(vn_to_inode(vp));
  161. ASSERT(inode);
  162. VN_UNLOCK(vp, 0);
  163. return vp;
  164. }
  165. #ifdef XFS_VNODE_TRACE
  166. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  167. ktrace_enter( (vp)->v_trace, \
  168. /* 0 */ (void *)(__psint_t)(vk), \
  169. /* 1 */ (void *)(s), \
  170. /* 2 */ (void *)(__psint_t) line, \
  171. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  172. /* 4 */ (void *)(ra), \
  173. /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
  174. /* 6 */ (void *)(__psint_t)current_cpu(), \
  175. /* 7 */ (void *)(__psint_t)current_pid(), \
  176. /* 8 */ (void *)__return_address, \
  177. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  178. /*
  179. * Vnode tracing code.
  180. */
  181. void
  182. vn_trace_entry(bhv_vnode_t *vp, const char *func, inst_t *ra)
  183. {
  184. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  185. }
  186. void
  187. vn_trace_exit(bhv_vnode_t *vp, const char *func, inst_t *ra)
  188. {
  189. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  190. }
  191. void
  192. vn_trace_hold(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  193. {
  194. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  195. }
  196. void
  197. vn_trace_ref(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  198. {
  199. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  200. }
  201. void
  202. vn_trace_rele(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  203. {
  204. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  205. }
  206. #endif /* XFS_VNODE_TRACE */