xfs_vnode.c 9.8 KB

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