xfs_vnode.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. struct vnode *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. struct vnode *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. struct vnode *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. struct vnode *
  65. vn_initialize(
  66. struct inode *inode)
  67. {
  68. struct vnode *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. struct vnode *vp,
  96. 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. inode->i_blksize = vap->va_blocksize;
  107. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  108. inode->i_flags |= S_IMMUTABLE;
  109. else
  110. inode->i_flags &= ~S_IMMUTABLE;
  111. if (vap->va_xflags & XFS_XFLAG_APPEND)
  112. inode->i_flags |= S_APPEND;
  113. else
  114. inode->i_flags &= ~S_APPEND;
  115. if (vap->va_xflags & XFS_XFLAG_SYNC)
  116. inode->i_flags |= S_SYNC;
  117. else
  118. inode->i_flags &= ~S_SYNC;
  119. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  120. inode->i_flags |= S_NOATIME;
  121. else
  122. inode->i_flags &= ~S_NOATIME;
  123. }
  124. /*
  125. * Revalidate the Linux inode from the vnode.
  126. */
  127. int
  128. __vn_revalidate(
  129. struct vnode *vp,
  130. struct vattr *vattr)
  131. {
  132. int error;
  133. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  134. vattr->va_mask = XFS_AT_STAT | XFS_AT_XFLAGS;
  135. VOP_GETATTR(vp, vattr, 0, NULL, error);
  136. if (likely(!error)) {
  137. vn_revalidate_core(vp, vattr);
  138. VUNMODIFY(vp);
  139. }
  140. return -error;
  141. }
  142. int
  143. vn_revalidate(
  144. struct vnode *vp)
  145. {
  146. vattr_t vattr;
  147. return __vn_revalidate(vp, &vattr);
  148. }
  149. /*
  150. * Add a reference to a referenced vnode.
  151. */
  152. struct vnode *
  153. vn_hold(
  154. struct vnode *vp)
  155. {
  156. struct inode *inode;
  157. XFS_STATS_INC(vn_hold);
  158. VN_LOCK(vp);
  159. inode = igrab(vn_to_inode(vp));
  160. ASSERT(inode);
  161. VN_UNLOCK(vp, 0);
  162. return vp;
  163. }
  164. #ifdef XFS_VNODE_TRACE
  165. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  166. ktrace_enter( (vp)->v_trace, \
  167. /* 0 */ (void *)(__psint_t)(vk), \
  168. /* 1 */ (void *)(s), \
  169. /* 2 */ (void *)(__psint_t) line, \
  170. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  171. /* 4 */ (void *)(ra), \
  172. /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
  173. /* 6 */ (void *)(__psint_t)current_cpu(), \
  174. /* 7 */ (void *)(__psint_t)current_pid(), \
  175. /* 8 */ (void *)__return_address, \
  176. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  177. /*
  178. * Vnode tracing code.
  179. */
  180. void
  181. vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra)
  182. {
  183. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  184. }
  185. void
  186. vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra)
  187. {
  188. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  189. }
  190. void
  191. vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
  192. {
  193. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  194. }
  195. void
  196. vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
  197. {
  198. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  199. }
  200. void
  201. vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
  202. {
  203. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  204. }
  205. #endif /* XFS_VNODE_TRACE */