blob: ad16af38e96573d6542a98102ce295890631ba58 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48void
49vn_init(void)
50{
51 register sv_t *svp;
52 register int i;
53
54 for (svp = vsync, i = 0; i < NVSYNC; i++, svp++)
55 init_sv(svp, SV_DEFAULT, "vsy", i);
56}
57
58/*
59 * Clean a vnode of filesystem-specific data and prepare it for reuse.
60 */
61STATIC int
62vn_reclaim(
63 struct vnode *vp)
64{
65 int error;
66
67 XFS_STATS_INC(vn_reclaim);
68 vn_trace_entry(vp, "vn_reclaim", (inst_t *)__return_address);
69
70 /*
71 * Only make the VOP_RECLAIM call if there are behaviors
72 * to call.
73 */
74 if (vp->v_fbhv) {
75 VOP_RECLAIM(vp, error);
76 if (error)
77 return -error;
78 }
79 ASSERT(vp->v_fbhv == NULL);
80
81 VN_LOCK(vp);
82 vp->v_flag &= (VRECLM|VWAIT);
83 VN_UNLOCK(vp, 0);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 vp->v_fbhv = NULL;
86
87#ifdef XFS_VNODE_TRACE
88 ktrace_free(vp->v_trace);
89 vp->v_trace = NULL;
90#endif
91
92 return 0;
93}
94
95STATIC void
96vn_wakeup(
97 struct vnode *vp)
98{
99 VN_LOCK(vp);
100 if (vp->v_flag & VWAIT)
101 sv_broadcast(vptosync(vp));
102 vp->v_flag &= ~(VRECLM|VWAIT|VMODIFIED);
103 VN_UNLOCK(vp, 0);
104}
105
106int
107vn_wait(
108 struct vnode *vp)
109{
110 VN_LOCK(vp);
111 if (vp->v_flag & (VINACT | VRECLM)) {
112 vp->v_flag |= VWAIT;
113 sv_wait(vptosync(vp), PINOD, &vp->v_lock, 0);
114 return 1;
115 }
116 VN_UNLOCK(vp, 0);
117 return 0;
118}
119
120struct vnode *
121vn_initialize(
122 struct inode *inode)
123{
124 struct vnode *vp = LINVFS_GET_VP(inode);
125
126 XFS_STATS_INC(vn_active);
127 XFS_STATS_INC(vn_alloc);
128
129 vp->v_flag = VMODIFIED;
130 spinlock_init(&vp->v_lock, "v_lock");
131
132 spin_lock(&vnumber_lock);
133 if (!++vn_generation) /* v_number shouldn't be zero */
134 vn_generation++;
135 vp->v_number = vn_generation;
136 spin_unlock(&vnumber_lock);
137
138 ASSERT(VN_CACHED(vp) == 0);
139
140 /* Initialize the first behavior and the behavior chain head. */
141 vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode");
142
143#ifdef XFS_VNODE_TRACE
144 vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#endif /* XFS_VNODE_TRACE */
146
147 vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
148 return vp;
149}
150
151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Revalidate the Linux inode from the vattr.
153 * Note: i_size _not_ updated; we must hold the inode
154 * semaphore when doing that - callers responsibility.
155 */
156void
157vn_revalidate_core(
158 struct vnode *vp,
159 vattr_t *vap)
160{
161 struct inode *inode = LINVFS_GET_IP(vp);
162
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000163 inode->i_mode = vap->va_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 inode->i_nlink = vap->va_nlink;
165 inode->i_uid = vap->va_uid;
166 inode->i_gid = vap->va_gid;
167 inode->i_blocks = vap->va_nblocks;
168 inode->i_mtime = vap->va_mtime;
169 inode->i_ctime = vap->va_ctime;
170 inode->i_atime = vap->va_atime;
171 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
172 inode->i_flags |= S_IMMUTABLE;
173 else
174 inode->i_flags &= ~S_IMMUTABLE;
175 if (vap->va_xflags & XFS_XFLAG_APPEND)
176 inode->i_flags |= S_APPEND;
177 else
178 inode->i_flags &= ~S_APPEND;
179 if (vap->va_xflags & XFS_XFLAG_SYNC)
180 inode->i_flags |= S_SYNC;
181 else
182 inode->i_flags &= ~S_SYNC;
183 if (vap->va_xflags & XFS_XFLAG_NOATIME)
184 inode->i_flags |= S_NOATIME;
185 else
186 inode->i_flags &= ~S_NOATIME;
187}
188
189/*
190 * Revalidate the Linux inode from the vnode.
191 */
192int
193vn_revalidate(
194 struct vnode *vp)
195{
196 vattr_t va;
197 int error;
198
199 vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address);
200 ASSERT(vp->v_fbhv != NULL);
201
202 va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS;
203 VOP_GETATTR(vp, &va, 0, NULL, error);
204 if (!error) {
205 vn_revalidate_core(vp, &va);
206 VUNMODIFY(vp);
207 }
208 return -error;
209}
210
211/*
212 * purge a vnode from the cache
213 * At this point the vnode is guaranteed to have no references (vn_count == 0)
214 * The caller has to make sure that there are no ways someone could
215 * get a handle (via vn_get) on the vnode (usually done via a mount/vfs lock).
216 */
217void
218vn_purge(
219 struct vnode *vp,
220 vmap_t *vmap)
221{
222 vn_trace_entry(vp, "vn_purge", (inst_t *)__return_address);
223
224again:
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 /*
237 * If vp is being reclaimed or inactivated, wait until it is inert,
238 * then proceed. Can't assume that vnode is actually reclaimed
239 * just because the reclaimed flag is asserted -- a vn_alloc
240 * reclaim can fail.
241 */
242 if (vp->v_flag & (VINACT | VRECLM)) {
243 ASSERT(vn_count(vp) == 0);
244 vp->v_flag |= VWAIT;
245 sv_wait(vptosync(vp), PINOD, &vp->v_lock, 0);
246 goto again;
247 }
248
249 /*
250 * Another process could have raced in and gotten this vnode...
251 */
252 if (vn_count(vp) > 0) {
253 VN_UNLOCK(vp, 0);
254 return;
255 }
256
257 XFS_STATS_DEC(vn_active);
258 vp->v_flag |= VRECLM;
259 VN_UNLOCK(vp, 0);
260
261 /*
262 * Call VOP_RECLAIM and clean vp. The FSYNC_INVAL flag tells
263 * vp's filesystem to flush and invalidate all cached resources.
264 * When vn_reclaim returns, vp should have no private data,
265 * either in a system cache or attached to v_data.
266 */
267 if (vn_reclaim(vp) != 0)
268 panic("vn_purge: cannot reclaim");
269
270 /*
271 * Wakeup anyone waiting for vp to be reclaimed.
272 */
273 vn_wakeup(vp);
274}
275
276/*
277 * Add a reference to a referenced vnode.
278 */
279struct vnode *
280vn_hold(
281 struct vnode *vp)
282{
283 struct inode *inode;
284
285 XFS_STATS_INC(vn_hold);
286
287 VN_LOCK(vp);
288 inode = igrab(LINVFS_GET_IP(vp));
289 ASSERT(inode);
290 VN_UNLOCK(vp, 0);
291
292 return vp;
293}
294
295/*
296 * Call VOP_INACTIVE on last reference.
297 */
298void
299vn_rele(
300 struct vnode *vp)
301{
302 int vcnt;
303 int cache;
304
305 XFS_STATS_INC(vn_rele);
306
307 VN_LOCK(vp);
308
309 vn_trace_entry(vp, "vn_rele", (inst_t *)__return_address);
310 vcnt = vn_count(vp);
311
312 /*
313 * Since we always get called from put_inode we know
314 * that i_count won't be decremented after we
315 * return.
316 */
317 if (!vcnt) {
318 /*
319 * As soon as we turn this on, noone can find us in vn_get
320 * until we turn off VINACT or VRECLM
321 */
322 vp->v_flag |= VINACT;
323 VN_UNLOCK(vp, 0);
324
325 /*
326 * Do not make the VOP_INACTIVE call if there
327 * are no behaviors attached to the vnode to call.
328 */
329 if (vp->v_fbhv)
330 VOP_INACTIVE(vp, NULL, cache);
331
332 VN_LOCK(vp);
333 if (vp->v_flag & VWAIT)
334 sv_broadcast(vptosync(vp));
335
336 vp->v_flag &= ~(VINACT|VWAIT|VRECLM|VMODIFIED);
337 }
338
339 VN_UNLOCK(vp, 0);
340
341 vn_trace_exit(vp, "vn_rele", (inst_t *)__return_address);
342}
343
344/*
345 * Finish the removal of a vnode.
346 */
347void
348vn_remove(
349 struct vnode *vp)
350{
351 vmap_t vmap;
352
353 /* Make sure we don't do this to the same vnode twice */
354 if (!(vp->v_fbhv))
355 return;
356
357 XFS_STATS_INC(vn_remove);
358 vn_trace_exit(vp, "vn_remove", (inst_t *)__return_address);
359
360 /*
361 * After the following purge the vnode
362 * will no longer exist.
363 */
364 VMAP(vp, vmap);
365 vn_purge(vp, &vmap);
366}
367
368
369#ifdef XFS_VNODE_TRACE
370
371#define KTRACE_ENTER(vp, vk, s, line, ra) \
372 ktrace_enter( (vp)->v_trace, \
373/* 0 */ (void *)(__psint_t)(vk), \
374/* 1 */ (void *)(s), \
375/* 2 */ (void *)(__psint_t) line, \
Christoph Hellwig02de1f02005-06-21 15:33:48 +1000376/* 3 */ (void *)(__psint_t)(vn_count(vp)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/* 4 */ (void *)(ra), \
378/* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \
379/* 6 */ (void *)(__psint_t)current_cpu(), \
380/* 7 */ (void *)(__psint_t)current_pid(), \
381/* 8 */ (void *)__return_address, \
Christoph Hellwig02de1f02005-06-21 15:33:48 +1000382/* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384/*
385 * Vnode tracing code.
386 */
387void
Eric Sandeen764433b2005-05-05 13:29:17 -0700388vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
391}
392
393void
Eric Sandeen764433b2005-05-05 13:29:17 -0700394vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
397}
398
399void
400vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
401{
402 KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
403}
404
405void
406vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
407{
408 KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
409}
410
411void
412vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
413{
414 KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
415}
416#endif /* XFS_VNODE_TRACE */