]> err.no Git - linux-2.6/blob - fs/xfs/linux-2.6/xfs_vnode.c
[XFS] Make metadata IO completion consistent with other IO completion
[linux-2.6] / fs / xfs / linux-2.6 / xfs_vnode.c
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
33 #include "xfs.h"
34
35
36 uint64_t vn_generation;         /* vnode generation number */
37 DEFINE_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])
45 sv_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  */
51 enum vtype iftovt_tab[] = {
52         VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
53         VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
54 };
55
56 u_short vttoif_tab[] = {
57         0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, 0, S_IFSOCK
58 };
59
60
61 void
62 vn_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  */
74 STATIC int
75 vn_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
109 STATIC void
110 vn_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
120 int
121 vn_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
134 struct vnode *
135 vn_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);
159 #endif  /* XFS_VNODE_TRACE */
160
161         vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
162         return vp;
163 }
164
165 /*
166  * Revalidate the Linux inode from the vattr.
167  * Note: i_size _not_ updated; we must hold the inode
168  * semaphore when doing that - callers responsibility.
169  */
170 void
171 vn_revalidate_core(
172         struct vnode    *vp,
173         vattr_t         *vap)
174 {
175         struct inode    *inode = LINVFS_GET_IP(vp);
176
177         inode->i_mode       = VTTOIF(vap->va_type) | vap->va_mode;
178         inode->i_nlink      = vap->va_nlink;
179         inode->i_uid        = vap->va_uid;
180         inode->i_gid        = vap->va_gid;
181         inode->i_blocks     = vap->va_nblocks;
182         inode->i_mtime      = vap->va_mtime;
183         inode->i_ctime      = vap->va_ctime;
184         inode->i_atime      = vap->va_atime;
185         if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
186                 inode->i_flags |= S_IMMUTABLE;
187         else
188                 inode->i_flags &= ~S_IMMUTABLE;
189         if (vap->va_xflags & XFS_XFLAG_APPEND)
190                 inode->i_flags |= S_APPEND;
191         else
192                 inode->i_flags &= ~S_APPEND;
193         if (vap->va_xflags & XFS_XFLAG_SYNC)
194                 inode->i_flags |= S_SYNC;
195         else
196                 inode->i_flags &= ~S_SYNC;
197         if (vap->va_xflags & XFS_XFLAG_NOATIME)
198                 inode->i_flags |= S_NOATIME;
199         else
200                 inode->i_flags &= ~S_NOATIME;
201 }
202
203 /*
204  * Revalidate the Linux inode from the vnode.
205  */
206 int
207 vn_revalidate(
208         struct vnode    *vp)
209 {
210         vattr_t         va;
211         int             error;
212
213         vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address);
214         ASSERT(vp->v_fbhv != NULL);
215
216         va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS;
217         VOP_GETATTR(vp, &va, 0, NULL, error);
218         if (!error) {
219                 vn_revalidate_core(vp, &va);
220                 VUNMODIFY(vp);
221         }
222         return -error;
223 }
224
225 /*
226  * purge a vnode from the cache
227  * At this point the vnode is guaranteed to have no references (vn_count == 0)
228  * The caller has to make sure that there are no ways someone could
229  * get a handle (via vn_get) on the vnode (usually done via a mount/vfs lock).
230  */
231 void
232 vn_purge(
233         struct vnode    *vp,
234         vmap_t          *vmap)
235 {
236         vn_trace_entry(vp, "vn_purge", (inst_t *)__return_address);
237
238 again:
239         /*
240          * Check whether vp has already been reclaimed since our caller
241          * sampled its version while holding a filesystem cache lock that
242          * its VOP_RECLAIM function acquires.
243          */
244         VN_LOCK(vp);
245         if (vp->v_number != vmap->v_number) {
246                 VN_UNLOCK(vp, 0);
247                 return;
248         }
249
250         /*
251          * If vp is being reclaimed or inactivated, wait until it is inert,
252          * then proceed.  Can't assume that vnode is actually reclaimed
253          * just because the reclaimed flag is asserted -- a vn_alloc
254          * reclaim can fail.
255          */
256         if (vp->v_flag & (VINACT | VRECLM)) {
257                 ASSERT(vn_count(vp) == 0);
258                 vp->v_flag |= VWAIT;
259                 sv_wait(vptosync(vp), PINOD, &vp->v_lock, 0);
260                 goto again;
261         }
262
263         /*
264          * Another process could have raced in and gotten this vnode...
265          */
266         if (vn_count(vp) > 0) {
267                 VN_UNLOCK(vp, 0);
268                 return;
269         }
270
271         XFS_STATS_DEC(vn_active);
272         vp->v_flag |= VRECLM;
273         VN_UNLOCK(vp, 0);
274
275         /*
276          * Call VOP_RECLAIM and clean vp. The FSYNC_INVAL flag tells
277          * vp's filesystem to flush and invalidate all cached resources.
278          * When vn_reclaim returns, vp should have no private data,
279          * either in a system cache or attached to v_data.
280          */
281         if (vn_reclaim(vp) != 0)
282                 panic("vn_purge: cannot reclaim");
283
284         /*
285          * Wakeup anyone waiting for vp to be reclaimed.
286          */
287         vn_wakeup(vp);
288 }
289
290 /*
291  * Add a reference to a referenced vnode.
292  */
293 struct vnode *
294 vn_hold(
295         struct vnode    *vp)
296 {
297         struct inode    *inode;
298
299         XFS_STATS_INC(vn_hold);
300
301         VN_LOCK(vp);
302         inode = igrab(LINVFS_GET_IP(vp));
303         ASSERT(inode);
304         VN_UNLOCK(vp, 0);
305
306         return vp;
307 }
308
309 /*
310  *  Call VOP_INACTIVE on last reference.
311  */
312 void
313 vn_rele(
314         struct vnode    *vp)
315 {
316         int             vcnt;
317         int             cache;
318
319         XFS_STATS_INC(vn_rele);
320
321         VN_LOCK(vp);
322
323         vn_trace_entry(vp, "vn_rele", (inst_t *)__return_address);
324         vcnt = vn_count(vp);
325
326         /*
327          * Since we always get called from put_inode we know
328          * that i_count won't be decremented after we
329          * return.
330          */
331         if (!vcnt) {
332                 /*
333                  * As soon as we turn this on, noone can find us in vn_get
334                  * until we turn off VINACT or VRECLM
335                  */
336                 vp->v_flag |= VINACT;
337                 VN_UNLOCK(vp, 0);
338
339                 /*
340                  * Do not make the VOP_INACTIVE call if there
341                  * are no behaviors attached to the vnode to call.
342                  */
343                 if (vp->v_fbhv)
344                         VOP_INACTIVE(vp, NULL, cache);
345
346                 VN_LOCK(vp);
347                 if (vp->v_flag & VWAIT)
348                         sv_broadcast(vptosync(vp));
349
350                 vp->v_flag &= ~(VINACT|VWAIT|VRECLM|VMODIFIED);
351         }
352
353         VN_UNLOCK(vp, 0);
354
355         vn_trace_exit(vp, "vn_rele", (inst_t *)__return_address);
356 }
357
358 /*
359  * Finish the removal of a vnode.
360  */
361 void
362 vn_remove(
363         struct vnode    *vp)
364 {
365         vmap_t          vmap;
366
367         /* Make sure we don't do this to the same vnode twice */
368         if (!(vp->v_fbhv))
369                 return;
370
371         XFS_STATS_INC(vn_remove);
372         vn_trace_exit(vp, "vn_remove", (inst_t *)__return_address);
373
374         /*
375          * After the following purge the vnode
376          * will no longer exist.
377          */
378         VMAP(vp, vmap);
379         vn_purge(vp, &vmap);
380 }
381
382
383 #ifdef  XFS_VNODE_TRACE
384
385 #define KTRACE_ENTER(vp, vk, s, line, ra)                       \
386         ktrace_enter(   (vp)->v_trace,                          \
387 /*  0 */                (void *)(__psint_t)(vk),                \
388 /*  1 */                (void *)(s),                            \
389 /*  2 */                (void *)(__psint_t) line,               \
390 /*  3 */                (void *)(__psint_t)(vn_count(vp)),      \
391 /*  4 */                (void *)(ra),                           \
392 /*  5 */                (void *)(__psunsigned_t)(vp)->v_flag,   \
393 /*  6 */                (void *)(__psint_t)current_cpu(),       \
394 /*  7 */                (void *)(__psint_t)current_pid(),       \
395 /*  8 */                (void *)__return_address,               \
396 /*  9 */                NULL, NULL, NULL, NULL, NULL, NULL, NULL)
397
398 /*
399  * Vnode tracing code.
400  */
401 void
402 vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra)
403 {
404         KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
405 }
406
407 void
408 vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra)
409 {
410         KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
411 }
412
413 void
414 vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra)
415 {
416         KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
417 }
418
419 void
420 vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra)
421 {
422         KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
423 }
424
425 void
426 vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra)
427 {
428         KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
429 }
430 #endif  /* XFS_VNODE_TRACE */