xfs_vnode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. VN_LOCK(vp);
  71. vp->v_flag &= (VRECLM|VWAIT);
  72. VN_UNLOCK(vp, 0);
  73. vp->v_fbhv = NULL;
  74. #ifdef XFS_VNODE_TRACE
  75. ktrace_free(vp->v_trace);
  76. vp->v_trace = NULL;
  77. #endif
  78. return 0;
  79. }
  80. STATIC void
  81. vn_wakeup(
  82. struct vnode *vp)
  83. {
  84. VN_LOCK(vp);
  85. if (vp->v_flag & VWAIT)
  86. sv_broadcast(vptosync(vp));
  87. vp->v_flag &= ~(VRECLM|VWAIT|VMODIFIED);
  88. VN_UNLOCK(vp, 0);
  89. }
  90. int
  91. vn_wait(
  92. struct vnode *vp)
  93. {
  94. VN_LOCK(vp);
  95. if (vp->v_flag & (VINACT | VRECLM)) {
  96. vp->v_flag |= VWAIT;
  97. sv_wait(vptosync(vp), PINOD, &vp->v_lock, 0);
  98. return 1;
  99. }
  100. VN_UNLOCK(vp, 0);
  101. return 0;
  102. }
  103. struct vnode *
  104. vn_initialize(
  105. struct inode *inode)
  106. {
  107. struct vnode *vp = LINVFS_GET_VP(inode);
  108. XFS_STATS_INC(vn_active);
  109. XFS_STATS_INC(vn_alloc);
  110. vp->v_flag = VMODIFIED;
  111. spinlock_init(&vp->v_lock, "v_lock");
  112. spin_lock(&vnumber_lock);
  113. if (!++vn_generation) /* v_number shouldn't be zero */
  114. vn_generation++;
  115. vp->v_number = vn_generation;
  116. spin_unlock(&vnumber_lock);
  117. ASSERT(VN_CACHED(vp) == 0);
  118. /* Initialize the first behavior and the behavior chain head. */
  119. vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode");
  120. #ifdef XFS_VNODE_TRACE
  121. vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
  122. #endif /* XFS_VNODE_TRACE */
  123. vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
  124. return vp;
  125. }
  126. /*
  127. * Revalidate the Linux inode from the vattr.
  128. * Note: i_size _not_ updated; we must hold the inode
  129. * semaphore when doing that - callers responsibility.
  130. */
  131. void
  132. vn_revalidate_core(
  133. struct vnode *vp,
  134. vattr_t *vap)
  135. {
  136. struct inode *inode = LINVFS_GET_IP(vp);
  137. inode->i_mode = vap->va_mode;
  138. inode->i_nlink = vap->va_nlink;
  139. inode->i_uid = vap->va_uid;
  140. inode->i_gid = vap->va_gid;
  141. inode->i_blocks = vap->va_nblocks;
  142. inode->i_mtime = vap->va_mtime;
  143. inode->i_ctime = vap->va_ctime;
  144. inode->i_atime = vap->va_atime;
  145. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  146. inode->i_flags |= S_IMMUTABLE;
  147. else
  148. inode->i_flags &= ~S_IMMUTABLE;
  149. if (vap->va_xflags & XFS_XFLAG_APPEND)
  150. inode->i_flags |= S_APPEND;
  151. else
  152. inode->i_flags &= ~S_APPEND;
  153. if (vap->va_xflags & XFS_XFLAG_SYNC)
  154. inode->i_flags |= S_SYNC;
  155. else
  156. inode->i_flags &= ~S_SYNC;
  157. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  158. inode->i_flags |= S_NOATIME;
  159. else
  160. inode->i_flags &= ~S_NOATIME;
  161. }
  162. /*
  163. * Revalidate the Linux inode from the vnode.
  164. */
  165. int
  166. vn_revalidate(
  167. struct vnode *vp)
  168. {
  169. vattr_t va;
  170. int error;
  171. vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address);
  172. ASSERT(vp->v_fbhv != NULL);
  173. va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS;
  174. VOP_GETATTR(vp, &va, 0, NULL, error);
  175. if (!error) {
  176. vn_revalidate_core(vp, &va);
  177. VUNMODIFY(vp);
  178. }
  179. return -error;
  180. }
  181. /*
  182. * purge a vnode from the cache
  183. * At this point the vnode is guaranteed to have no references (vn_count == 0)
  184. * The caller has to make sure that there are no ways someone could
  185. * get a handle (via vn_get) on the vnode (usually done via a mount/vfs lock).
  186. */
  187. void
  188. vn_purge(
  189. struct vnode *vp,
  190. vmap_t *vmap)
  191. {
  192. vn_trace_entry(vp, "vn_purge", (inst_t *)__return_address);
  193. again:
  194. /*
  195. * Check whether vp has already been reclaimed since our caller
  196. * sampled its version while holding a filesystem cache lock that
  197. * its VOP_RECLAIM function acquires.
  198. */
  199. VN_LOCK(vp);
  200. if (vp->v_number != vmap->v_number) {
  201. VN_UNLOCK(vp, 0);
  202. return;
  203. }
  204. /*
  205. * If vp is being reclaimed or inactivated, wait until it is inert,
  206. * then proceed. Can't assume that vnode is actually reclaimed
  207. * just because the reclaimed flag is asserted -- a vn_alloc
  208. * reclaim can fail.
  209. */
  210. if (vp->v_flag & (VINACT | VRECLM)) {
  211. ASSERT(vn_count(vp) == 0);
  212. vp->v_flag |= VWAIT;
  213. sv_wait(vptosync(vp), PINOD, &vp->v_lock, 0);
  214. goto again;
  215. }
  216. /*
  217. * Another process could have raced in and gotten this vnode...
  218. */
  219. if (vn_count(vp) > 0) {
  220. VN_UNLOCK(vp, 0);
  221. return;
  222. }
  223. XFS_STATS_DEC(vn_active);
  224. vp->v_flag |= VRECLM;
  225. VN_UNLOCK(vp, 0);
  226. /*
  227. * Call VOP_RECLAIM and clean vp. The FSYNC_INVAL flag tells
  228. * vp's filesystem to flush and invalidate all cached resources.
  229. * When vn_reclaim returns, vp should have no private data,
  230. * either in a system cache or attached to v_data.
  231. */
  232. if (vn_reclaim(vp) != 0)
  233. panic("vn_purge: cannot reclaim");
  234. /*
  235. * Wakeup anyone waiting for vp to be reclaimed.
  236. */
  237. vn_wakeup(vp);
  238. }
  239. /*
  240. * Add a reference to a referenced vnode.
  241. */
  242. struct vnode *
  243. vn_hold(
  244. struct vnode *vp)
  245. {
  246. struct inode *inode;
  247. XFS_STATS_INC(vn_hold);
  248. VN_LOCK(vp);
  249. inode = igrab(LINVFS_GET_IP(vp));
  250. ASSERT(inode);
  251. VN_UNLOCK(vp, 0);
  252. return vp;
  253. }
  254. /*
  255. * Call VOP_INACTIVE on last reference.
  256. */
  257. void
  258. vn_rele(
  259. struct vnode *vp)
  260. {
  261. int vcnt;
  262. int cache;
  263. XFS_STATS_INC(vn_rele);
  264. VN_LOCK(vp);
  265. vn_trace_entry(vp, "vn_rele", (inst_t *)__return_address);
  266. vcnt = vn_count(vp);
  267. /*
  268. * Since we always get called from put_inode we know
  269. * that i_count won't be decremented after we
  270. * return.
  271. */
  272. if (!vcnt) {
  273. /*
  274. * As soon as we turn this on, noone can find us in vn_get
  275. * until we turn off VINACT or VRECLM
  276. */
  277. vp->v_flag |= VINACT;
  278. VN_UNLOCK(vp, 0);
  279. /*
  280. * Do not make the VOP_INACTIVE call if there
  281. * are no behaviors attached to the vnode to call.
  282. */
  283. if (vp->v_fbhv)
  284. VOP_INACTIVE(vp, NULL, cache);
  285. VN_LOCK(vp);
  286. if (vp->v_flag & VWAIT)
  287. sv_broadcast(vptosync(vp));
  288. vp->v_flag &= ~(VINACT|VWAIT|VRECLM|VMODIFIED);
  289. }
  290. VN_UNLOCK(vp, 0);
  291. vn_trace_exit(vp, "vn_rele", (inst_t *)__return_address);
  292. }
  293. /*
  294. * Finish the removal of a vnode.
  295. */
  296. void
  297. vn_remove(
  298. struct vnode *vp)
  299. {
  300. vmap_t vmap;
  301. /* Make sure we don't do this to the same vnode twice */
  302. if (!(vp->v_fbhv))
  303. return;
  304. XFS_STATS_INC(vn_remove);
  305. vn_trace_exit(vp, "vn_remove", (inst_t *)__return_address);
  306. /*
  307. * After the following purge the vnode
  308. * will no longer exist.
  309. */
  310. VMAP(vp, vmap);
  311. vn_purge(vp, &vmap);
  312. }
  313. #ifdef XFS_VNODE_TRACE
  314. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  315. ktrace_enter( (vp)->v_trace, \
  316. /* 0 */ (void *)(__psint_t)(vk), \
  317. /* 1 */ (void *)(s), \
  318. /* 2 */ (void *)(__psint_t) line, \
  319. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  320. /* 4 */ (void *)(ra), \
  321. /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
  322. /* 6 */ (void *)(__psint_t)current_cpu(), \
  323. /* 7 */ (void *)(__psint_t)current_pid(), \
  324. /* 8 */ (void *)__return_address, \
  325. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  326. /*
  327. * Vnode tracing code.
  328. */
  329. void
  330. vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra)
  331. {
  332. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  333. }
  334. void
  335. vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra)
  336. {
  337. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  338. }
  339. void
  340. vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
  341. {
  342. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  343. }
  344. void
  345. vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
  346. {
  347. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  348. }
  349. void
  350. vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
  351. {
  352. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  353. }
  354. #endif /* XFS_VNODE_TRACE */