2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
36 uint64_t vn_generation; /* vnode generation number */
37 spinlock_t vnumber_lock = SPIN_LOCK_UNLOCKED;
40 * Dedicated vnode inactive/reclaim sync semaphores.
41 * Prime number of hash buckets since address is used as the key.
44 #define vptosync(v) (&vsync[((unsigned long)v) % NVSYNC])
48 * Translate stat(2) file types to vnode types and vice versa.
49 * Aware of numeric order of S_IFMT and vnode type values.
51 enum vtype iftovt_tab[] = {
52 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
53 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
56 u_short vttoif_tab[] = {
57 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, 0, S_IFSOCK
67 for (svp = vsync, i = 0; i < NVSYNC; i++, svp++)
68 init_sv(svp, SV_DEFAULT, "vsy", i);
72 * Clean a vnode of filesystem-specific data and prepare it for reuse.
80 XFS_STATS_INC(vn_reclaim);
81 vn_trace_entry(vp, "vn_reclaim", (inst_t *)__return_address);
84 * Only make the VOP_RECLAIM call if there are behaviors
88 VOP_RECLAIM(vp, error);
92 ASSERT(vp->v_fbhv == NULL);
95 vp->v_flag &= (VRECLM|VWAIT);
101 #ifdef XFS_VNODE_TRACE
102 ktrace_free(vp->v_trace);
114 if (vp->v_flag & VWAIT)
115 sv_broadcast(vptosync(vp));
116 vp->v_flag &= ~(VRECLM|VWAIT|VMODIFIED);
125 if (vp->v_flag & (VINACT | VRECLM)) {
127 sv_wait(vptosync(vp), PINOD, &vp->v_lock, 0);
138 struct vnode *vp = LINVFS_GET_VP(inode);
140 XFS_STATS_INC(vn_active);
141 XFS_STATS_INC(vn_alloc);
143 vp->v_flag = VMODIFIED;
144 spinlock_init(&vp->v_lock, "v_lock");
146 spin_lock(&vnumber_lock);
147 if (!++vn_generation) /* v_number shouldn't be zero */
149 vp->v_number = vn_generation;
150 spin_unlock(&vnumber_lock);
152 ASSERT(VN_CACHED(vp) == 0);
154 /* Initialize the first behavior and the behavior chain head. */
155 vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode");
157 #ifdef XFS_VNODE_TRACE
158 vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
159 printk("Allocated VNODE_TRACE at 0x%p\n", vp->v_trace);
160 #endif /* XFS_VNODE_TRACE */
162 vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
167 * Get a reference on a vnode.
176 XFS_STATS_INC(vn_get);
177 inode = LINVFS_GET_IP(vp);
178 if (inode->i_state & I_FREEING)
181 inode = ilookup(vmap->v_vfsp->vfs_super, vmap->v_ino);
182 if (!inode) /* Inode not present */
185 vn_trace_exit(vp, "vn_get", (inst_t *)__return_address);
191 * Revalidate the Linux inode from the vattr.
192 * Note: i_size _not_ updated; we must hold the inode
193 * semaphore when doing that - callers responsibility.
200 struct inode *inode = LINVFS_GET_IP(vp);
202 inode = LINVFS_GET_IP(vp);
203 inode->i_mode = VTTOIF(vap->va_type) | vap->va_mode;
204 inode->i_nlink = vap->va_nlink;
205 inode->i_uid = vap->va_uid;
206 inode->i_gid = vap->va_gid;
207 inode->i_blocks = vap->va_nblocks;
208 inode->i_mtime = vap->va_mtime;
209 inode->i_ctime = vap->va_ctime;
210 inode->i_atime = vap->va_atime;
211 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
212 inode->i_flags |= S_IMMUTABLE;
214 inode->i_flags &= ~S_IMMUTABLE;
215 if (vap->va_xflags & XFS_XFLAG_IUNLINK)
216 inode->i_flags |= S_IUNLINK;
218 inode->i_flags &= ~S_IUNLINK;
219 if (vap->va_xflags & XFS_XFLAG_BARRIER)
220 inode->i_flags |= S_BARRIER;
222 inode->i_flags &= ~S_BARRIER;
223 if (vap->va_xflags & XFS_XFLAG_APPEND)
224 inode->i_flags |= S_APPEND;
226 inode->i_flags &= ~S_APPEND;
227 if (vap->va_xflags & XFS_XFLAG_SYNC)
228 inode->i_flags |= S_SYNC;
230 inode->i_flags &= ~S_SYNC;
231 if (vap->va_xflags & XFS_XFLAG_NOATIME)
232 inode->i_flags |= S_NOATIME;
234 inode->i_flags &= ~S_NOATIME;
238 * Revalidate the Linux inode from the vnode.
247 vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address);
248 ASSERT(vp->v_fbhv != NULL);
250 va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS;
251 VOP_GETATTR(vp, &va, 0, NULL, error);
253 vn_revalidate_core(vp, &va);
260 * purge a vnode from the cache
261 * At this point the vnode is guaranteed to have no references (vn_count == 0)
262 * The caller has to make sure that there are no ways someone could
263 * get a handle (via vn_get) on the vnode (usually done via a mount/vfs lock).
270 vn_trace_entry(vp, "vn_purge", (inst_t *)__return_address);
274 * Check whether vp has already been reclaimed since our caller
275 * sampled its version while holding a filesystem cache lock that
276 * its VOP_RECLAIM function acquires.
279 if (vp->v_number != vmap->v_number) {
285 * If vp is being reclaimed or inactivated, wait until it is inert,
286 * then proceed. Can't assume that vnode is actually reclaimed
287 * just because the reclaimed flag is asserted -- a vn_alloc
290 if (vp->v_flag & (VINACT | VRECLM)) {
291 ASSERT(vn_count(vp) == 0);
293 sv_wait(vptosync(vp), PINOD, &vp->v_lock, 0);
298 * Another process could have raced in and gotten this vnode...
300 if (vn_count(vp) > 0) {
305 XFS_STATS_DEC(vn_active);
306 vp->v_flag |= VRECLM;
310 * Call VOP_RECLAIM and clean vp. The FSYNC_INVAL flag tells
311 * vp's filesystem to flush and invalidate all cached resources.
312 * When vn_reclaim returns, vp should have no private data,
313 * either in a system cache or attached to v_data.
315 if (vn_reclaim(vp) != 0)
316 panic("vn_purge: cannot reclaim");
319 * Wakeup anyone waiting for vp to be reclaimed.
325 * Add a reference to a referenced vnode.
333 XFS_STATS_INC(vn_hold);
336 inode = igrab(LINVFS_GET_IP(vp));
344 * Call VOP_INACTIVE on last reference.
353 XFS_STATS_INC(vn_rele);
357 vn_trace_entry(vp, "vn_rele", (inst_t *)__return_address);
361 * Since we always get called from put_inode we know
362 * that i_count won't be decremented after we
367 * As soon as we turn this on, noone can find us in vn_get
368 * until we turn off VINACT or VRECLM
370 vp->v_flag |= VINACT;
374 * Do not make the VOP_INACTIVE call if there
375 * are no behaviors attached to the vnode to call.
378 VOP_INACTIVE(vp, NULL, cache);
381 if (vp->v_flag & VWAIT)
382 sv_broadcast(vptosync(vp));
384 vp->v_flag &= ~(VINACT|VWAIT|VRECLM|VMODIFIED);
389 vn_trace_exit(vp, "vn_rele", (inst_t *)__return_address);
393 * Finish the removal of a vnode.
401 /* Make sure we don't do this to the same vnode twice */
405 XFS_STATS_INC(vn_remove);
406 vn_trace_exit(vp, "vn_remove", (inst_t *)__return_address);
409 * After the following purge the vnode
410 * will no longer exist.
417 #ifdef XFS_VNODE_TRACE
419 #define KTRACE_ENTER(vp, vk, s, line, ra) \
420 ktrace_enter( (vp)->v_trace, \
421 /* 0 */ (void *)(__psint_t)(vk), \
422 /* 1 */ (void *)(s), \
423 /* 2 */ (void *)(__psint_t) line, \
424 /* 3 */ (void *)(vn_count(vp)), \
425 /* 4 */ (void *)(ra), \
426 /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
427 /* 6 */ (void *)(__psint_t)smp_processor_id(), \
428 /* 7 */ (void *)(__psint_t)(current->pid), \
429 /* 8 */ (void *)__return_address, \
430 /* 9 */ 0, 0, 0, 0, 0, 0, 0)
433 * Vnode tracing code.
436 vn_trace_entry(vnode_t *vp, char *func, inst_t *ra)
438 KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
442 vn_trace_exit(vnode_t *vp, char *func, inst_t *ra)
444 KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
448 vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
450 KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
454 vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
456 KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
460 vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
462 KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
464 #endif /* XFS_VNODE_TRACE */