xfs_vnode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. sv_t vsync[NVSYNC];
  42. void
  43. vn_init(void)
  44. {
  45. register sv_t *svp;
  46. register int i;
  47. for (svp = vsync, i = 0; i < NVSYNC; i++, svp++)
  48. init_sv(svp, SV_DEFAULT, "vsy", i);
  49. }
  50. /*
  51. * Clean a vnode of filesystem-specific data and prepare it for reuse.
  52. */
  53. STATIC int
  54. vn_reclaim(
  55. struct vnode *vp)
  56. {
  57. int error;
  58. XFS_STATS_INC(vn_reclaim);
  59. vn_trace_entry(vp, "vn_reclaim", (inst_t *)__return_address);
  60. /*
  61. * Only make the VOP_RECLAIM call if there are behaviors
  62. * to call.
  63. */
  64. if (vp->v_fbhv) {
  65. VOP_RECLAIM(vp, error);
  66. if (error)
  67. return -error;
  68. }
  69. ASSERT(vp->v_fbhv == NULL);
  70. vp->v_fbhv = NULL;
  71. #ifdef XFS_VNODE_TRACE
  72. ktrace_free(vp->v_trace);
  73. vp->v_trace = NULL;
  74. #endif
  75. return 0;
  76. }
  77. struct vnode *
  78. vn_initialize(
  79. struct inode *inode)
  80. {
  81. struct vnode *vp = LINVFS_GET_VP(inode);
  82. XFS_STATS_INC(vn_active);
  83. XFS_STATS_INC(vn_alloc);
  84. vp->v_flag = VMODIFIED;
  85. spinlock_init(&vp->v_lock, "v_lock");
  86. spin_lock(&vnumber_lock);
  87. if (!++vn_generation) /* v_number shouldn't be zero */
  88. vn_generation++;
  89. vp->v_number = vn_generation;
  90. spin_unlock(&vnumber_lock);
  91. ASSERT(VN_CACHED(vp) == 0);
  92. /* Initialize the first behavior and the behavior chain head. */
  93. vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode");
  94. #ifdef XFS_VNODE_TRACE
  95. vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
  96. #endif /* XFS_VNODE_TRACE */
  97. vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
  98. return vp;
  99. }
  100. /*
  101. * Revalidate the Linux inode from the vattr.
  102. * Note: i_size _not_ updated; we must hold the inode
  103. * semaphore when doing that - callers responsibility.
  104. */
  105. void
  106. vn_revalidate_core(
  107. struct vnode *vp,
  108. vattr_t *vap)
  109. {
  110. struct inode *inode = LINVFS_GET_IP(vp);
  111. inode->i_mode = vap->va_mode;
  112. inode->i_nlink = vap->va_nlink;
  113. inode->i_uid = vap->va_uid;
  114. inode->i_gid = vap->va_gid;
  115. inode->i_blocks = vap->va_nblocks;
  116. inode->i_mtime = vap->va_mtime;
  117. inode->i_ctime = vap->va_ctime;
  118. inode->i_atime = vap->va_atime;
  119. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  120. inode->i_flags |= S_IMMUTABLE;
  121. else
  122. inode->i_flags &= ~S_IMMUTABLE;
  123. if (vap->va_xflags & XFS_XFLAG_APPEND)
  124. inode->i_flags |= S_APPEND;
  125. else
  126. inode->i_flags &= ~S_APPEND;
  127. if (vap->va_xflags & XFS_XFLAG_SYNC)
  128. inode->i_flags |= S_SYNC;
  129. else
  130. inode->i_flags &= ~S_SYNC;
  131. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  132. inode->i_flags |= S_NOATIME;
  133. else
  134. inode->i_flags &= ~S_NOATIME;
  135. }
  136. /*
  137. * Revalidate the Linux inode from the vnode.
  138. */
  139. int
  140. vn_revalidate(
  141. struct vnode *vp)
  142. {
  143. vattr_t va;
  144. int error;
  145. vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address);
  146. ASSERT(vp->v_fbhv != NULL);
  147. va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS;
  148. VOP_GETATTR(vp, &va, 0, NULL, error);
  149. if (!error) {
  150. vn_revalidate_core(vp, &va);
  151. VUNMODIFY(vp);
  152. }
  153. return -error;
  154. }
  155. /*
  156. * purge a vnode from the cache
  157. * At this point the vnode is guaranteed to have no references (vn_count == 0)
  158. * The caller has to make sure that there are no ways someone could
  159. * get a handle (via vn_get) on the vnode (usually done via a mount/vfs lock).
  160. */
  161. void
  162. vn_purge(
  163. struct vnode *vp,
  164. vmap_t *vmap)
  165. {
  166. vn_trace_entry(vp, "vn_purge", (inst_t *)__return_address);
  167. /*
  168. * Check whether vp has already been reclaimed since our caller
  169. * sampled its version while holding a filesystem cache lock that
  170. * its VOP_RECLAIM function acquires.
  171. */
  172. VN_LOCK(vp);
  173. if (vp->v_number != vmap->v_number) {
  174. VN_UNLOCK(vp, 0);
  175. return;
  176. }
  177. /*
  178. * Another process could have raced in and gotten this vnode...
  179. */
  180. if (vn_count(vp) > 0) {
  181. VN_UNLOCK(vp, 0);
  182. return;
  183. }
  184. XFS_STATS_DEC(vn_active);
  185. VN_UNLOCK(vp, 0);
  186. /*
  187. * Call VOP_RECLAIM and clean vp. The FSYNC_INVAL flag tells
  188. * vp's filesystem to flush and invalidate all cached resources.
  189. * When vn_reclaim returns, vp should have no private data,
  190. * either in a system cache or attached to v_data.
  191. */
  192. if (vn_reclaim(vp) != 0)
  193. panic("vn_purge: cannot reclaim");
  194. }
  195. /*
  196. * Add a reference to a referenced vnode.
  197. */
  198. struct vnode *
  199. vn_hold(
  200. struct vnode *vp)
  201. {
  202. struct inode *inode;
  203. XFS_STATS_INC(vn_hold);
  204. VN_LOCK(vp);
  205. inode = igrab(LINVFS_GET_IP(vp));
  206. ASSERT(inode);
  207. VN_UNLOCK(vp, 0);
  208. return vp;
  209. }
  210. /*
  211. * Call VOP_INACTIVE on last reference.
  212. */
  213. void
  214. vn_rele(
  215. struct vnode *vp)
  216. {
  217. int vcnt;
  218. int cache;
  219. XFS_STATS_INC(vn_rele);
  220. VN_LOCK(vp);
  221. vn_trace_entry(vp, "vn_rele", (inst_t *)__return_address);
  222. vcnt = vn_count(vp);
  223. /*
  224. * Since we always get called from put_inode we know
  225. * that i_count won't be decremented after we
  226. * return.
  227. */
  228. if (!vcnt) {
  229. VN_UNLOCK(vp, 0);
  230. /*
  231. * Do not make the VOP_INACTIVE call if there
  232. * are no behaviors attached to the vnode to call.
  233. */
  234. if (vp->v_fbhv)
  235. VOP_INACTIVE(vp, NULL, cache);
  236. VN_LOCK(vp);
  237. vp->v_flag &= ~VMODIFIED;
  238. }
  239. VN_UNLOCK(vp, 0);
  240. vn_trace_exit(vp, "vn_rele", (inst_t *)__return_address);
  241. }
  242. /*
  243. * Finish the removal of a vnode.
  244. */
  245. void
  246. vn_remove(
  247. struct vnode *vp)
  248. {
  249. vmap_t vmap;
  250. /* Make sure we don't do this to the same vnode twice */
  251. if (!(vp->v_fbhv))
  252. return;
  253. XFS_STATS_INC(vn_remove);
  254. vn_trace_exit(vp, "vn_remove", (inst_t *)__return_address);
  255. /*
  256. * After the following purge the vnode
  257. * will no longer exist.
  258. */
  259. VMAP(vp, vmap);
  260. vn_purge(vp, &vmap);
  261. }
  262. #ifdef XFS_VNODE_TRACE
  263. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  264. ktrace_enter( (vp)->v_trace, \
  265. /* 0 */ (void *)(__psint_t)(vk), \
  266. /* 1 */ (void *)(s), \
  267. /* 2 */ (void *)(__psint_t) line, \
  268. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  269. /* 4 */ (void *)(ra), \
  270. /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
  271. /* 6 */ (void *)(__psint_t)current_cpu(), \
  272. /* 7 */ (void *)(__psint_t)current_pid(), \
  273. /* 8 */ (void *)__return_address, \
  274. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  275. /*
  276. * Vnode tracing code.
  277. */
  278. void
  279. vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra)
  280. {
  281. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  282. }
  283. void
  284. vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra)
  285. {
  286. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  287. }
  288. void
  289. vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
  290. {
  291. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  292. }
  293. void
  294. vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
  295. {
  296. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  297. }
  298. void
  299. vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
  300. {
  301. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  302. }
  303. #endif /* XFS_VNODE_TRACE */