xfs_vnode.c 9.4 KB

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