xfs_vnode.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. struct vnode *
  50. vn_initialize(
  51. struct inode *inode)
  52. {
  53. struct vnode *vp = LINVFS_GET_VP(inode);
  54. XFS_STATS_INC(vn_active);
  55. XFS_STATS_INC(vn_alloc);
  56. vp->v_flag = VMODIFIED;
  57. spinlock_init(&vp->v_lock, "v_lock");
  58. spin_lock(&vnumber_lock);
  59. if (!++vn_generation) /* v_number shouldn't be zero */
  60. vn_generation++;
  61. vp->v_number = vn_generation;
  62. spin_unlock(&vnumber_lock);
  63. ASSERT(VN_CACHED(vp) == 0);
  64. /* Initialize the first behavior and the behavior chain head. */
  65. vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode");
  66. atomic_set(&vp->v_iocount, 0);
  67. #ifdef XFS_VNODE_TRACE
  68. vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
  69. #endif /* XFS_VNODE_TRACE */
  70. vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
  71. return vp;
  72. }
  73. /*
  74. * Revalidate the Linux inode from the vattr.
  75. * Note: i_size _not_ updated; we must hold the inode
  76. * semaphore when doing that - callers responsibility.
  77. */
  78. void
  79. vn_revalidate_core(
  80. struct vnode *vp,
  81. vattr_t *vap)
  82. {
  83. struct inode *inode = LINVFS_GET_IP(vp);
  84. inode->i_mode = vap->va_mode;
  85. inode->i_nlink = vap->va_nlink;
  86. inode->i_uid = vap->va_uid;
  87. inode->i_gid = vap->va_gid;
  88. inode->i_blocks = vap->va_nblocks;
  89. inode->i_mtime = vap->va_mtime;
  90. inode->i_ctime = vap->va_ctime;
  91. inode->i_blksize = vap->va_blocksize;
  92. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  93. inode->i_flags |= S_IMMUTABLE;
  94. else
  95. inode->i_flags &= ~S_IMMUTABLE;
  96. if (vap->va_xflags & XFS_XFLAG_APPEND)
  97. inode->i_flags |= S_APPEND;
  98. else
  99. inode->i_flags &= ~S_APPEND;
  100. if (vap->va_xflags & XFS_XFLAG_SYNC)
  101. inode->i_flags |= S_SYNC;
  102. else
  103. inode->i_flags &= ~S_SYNC;
  104. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  105. inode->i_flags |= S_NOATIME;
  106. else
  107. inode->i_flags &= ~S_NOATIME;
  108. }
  109. /*
  110. * Revalidate the Linux inode from the vnode.
  111. */
  112. int
  113. vn_revalidate(
  114. struct vnode *vp)
  115. {
  116. vattr_t va;
  117. int error;
  118. vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address);
  119. ASSERT(vp->v_fbhv != NULL);
  120. va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS;
  121. VOP_GETATTR(vp, &va, 0, NULL, error);
  122. if (!error) {
  123. vn_revalidate_core(vp, &va);
  124. VUNMODIFY(vp);
  125. }
  126. return -error;
  127. }
  128. /*
  129. * Add a reference to a referenced vnode.
  130. */
  131. struct vnode *
  132. vn_hold(
  133. struct vnode *vp)
  134. {
  135. struct inode *inode;
  136. XFS_STATS_INC(vn_hold);
  137. VN_LOCK(vp);
  138. inode = igrab(LINVFS_GET_IP(vp));
  139. ASSERT(inode);
  140. VN_UNLOCK(vp, 0);
  141. return vp;
  142. }
  143. #ifdef XFS_VNODE_TRACE
  144. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  145. ktrace_enter( (vp)->v_trace, \
  146. /* 0 */ (void *)(__psint_t)(vk), \
  147. /* 1 */ (void *)(s), \
  148. /* 2 */ (void *)(__psint_t) line, \
  149. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  150. /* 4 */ (void *)(ra), \
  151. /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
  152. /* 6 */ (void *)(__psint_t)current_cpu(), \
  153. /* 7 */ (void *)(__psint_t)current_pid(), \
  154. /* 8 */ (void *)__return_address, \
  155. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  156. /*
  157. * Vnode tracing code.
  158. */
  159. void
  160. vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra)
  161. {
  162. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  163. }
  164. void
  165. vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra)
  166. {
  167. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  168. }
  169. void
  170. vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
  171. {
  172. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  173. }
  174. void
  175. vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
  176. {
  177. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  178. }
  179. void
  180. vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
  181. {
  182. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  183. }
  184. #endif /* XFS_VNODE_TRACE */