xfs_vnode.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. uint64_t vn_generation; /* vnode generation number */
  34. DEFINE_SPINLOCK(vnumber_lock);
  35. /*
  36. * Dedicated vnode inactive/reclaim sync semaphores.
  37. * Prime number of hash buckets since address is used as the key.
  38. */
  39. #define NVSYNC 37
  40. #define vptosync(v) (&vsync[((unsigned long)v) % NVSYNC])
  41. STATIC wait_queue_head_t vsync[NVSYNC];
  42. void
  43. vn_init(void)
  44. {
  45. int i;
  46. for (i = 0; i < NVSYNC; i++)
  47. init_waitqueue_head(&vsync[i]);
  48. }
  49. void
  50. vn_iowait(
  51. struct vnode *vp)
  52. {
  53. wait_queue_head_t *wq = vptosync(vp);
  54. wait_event(*wq, (atomic_read(&vp->v_iocount) == 0));
  55. }
  56. void
  57. vn_iowake(
  58. struct vnode *vp)
  59. {
  60. if (atomic_dec_and_test(&vp->v_iocount))
  61. wake_up(vptosync(vp));
  62. }
  63. struct vnode *
  64. vn_initialize(
  65. struct inode *inode)
  66. {
  67. struct vnode *vp = LINVFS_GET_VP(inode);
  68. XFS_STATS_INC(vn_active);
  69. XFS_STATS_INC(vn_alloc);
  70. vp->v_flag = VMODIFIED;
  71. spinlock_init(&vp->v_lock, "v_lock");
  72. spin_lock(&vnumber_lock);
  73. if (!++vn_generation) /* v_number shouldn't be zero */
  74. vn_generation++;
  75. vp->v_number = vn_generation;
  76. spin_unlock(&vnumber_lock);
  77. ASSERT(VN_CACHED(vp) == 0);
  78. /* Initialize the first behavior and the behavior chain head. */
  79. vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode");
  80. atomic_set(&vp->v_iocount, 0);
  81. #ifdef XFS_VNODE_TRACE
  82. vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
  83. #endif /* XFS_VNODE_TRACE */
  84. vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
  85. return vp;
  86. }
  87. /*
  88. * Revalidate the Linux inode from the vattr.
  89. * Note: i_size _not_ updated; we must hold the inode
  90. * semaphore when doing that - callers responsibility.
  91. */
  92. void
  93. vn_revalidate_core(
  94. struct vnode *vp,
  95. vattr_t *vap)
  96. {
  97. struct inode *inode = LINVFS_GET_IP(vp);
  98. inode->i_mode = vap->va_mode;
  99. inode->i_nlink = vap->va_nlink;
  100. inode->i_uid = vap->va_uid;
  101. inode->i_gid = vap->va_gid;
  102. inode->i_blocks = vap->va_nblocks;
  103. inode->i_mtime = vap->va_mtime;
  104. inode->i_ctime = vap->va_ctime;
  105. inode->i_atime = vap->va_atime;
  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. struct vnode *vp)
  129. {
  130. vattr_t va;
  131. int error;
  132. vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address);
  133. ASSERT(vp->v_fbhv != NULL);
  134. va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS;
  135. VOP_GETATTR(vp, &va, 0, NULL, error);
  136. if (!error) {
  137. vn_revalidate_core(vp, &va);
  138. VUNMODIFY(vp);
  139. }
  140. return -error;
  141. }
  142. /*
  143. * Add a reference to a referenced vnode.
  144. */
  145. struct vnode *
  146. vn_hold(
  147. struct vnode *vp)
  148. {
  149. struct inode *inode;
  150. XFS_STATS_INC(vn_hold);
  151. VN_LOCK(vp);
  152. inode = igrab(LINVFS_GET_IP(vp));
  153. ASSERT(inode);
  154. VN_UNLOCK(vp, 0);
  155. return vp;
  156. }
  157. #ifdef XFS_VNODE_TRACE
  158. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  159. ktrace_enter( (vp)->v_trace, \
  160. /* 0 */ (void *)(__psint_t)(vk), \
  161. /* 1 */ (void *)(s), \
  162. /* 2 */ (void *)(__psint_t) line, \
  163. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  164. /* 4 */ (void *)(ra), \
  165. /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
  166. /* 6 */ (void *)(__psint_t)current_cpu(), \
  167. /* 7 */ (void *)(__psint_t)current_pid(), \
  168. /* 8 */ (void *)__return_address, \
  169. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  170. /*
  171. * Vnode tracing code.
  172. */
  173. void
  174. vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra)
  175. {
  176. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  177. }
  178. void
  179. vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra)
  180. {
  181. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  182. }
  183. void
  184. vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
  185. {
  186. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  187. }
  188. void
  189. vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
  190. {
  191. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  192. }
  193. void
  194. vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
  195. {
  196. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  197. }
  198. #endif /* XFS_VNODE_TRACE */