xfs_vnode.c 8.2 KB

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