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