xfs_vnode.c 9.8 KB

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