layer_vnops.c revision 1.42 1 /* $NetBSD: layer_vnops.c,v 1.42 2010/07/02 03:16:01 rmind Exp $ */
2
3 /*
4 * Copyright (c) 1999 National Aeronautics & Space Administration
5 * All rights reserved.
6 *
7 * This software was written by William Studenmund of the
8 * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the National Aeronautics & Space Administration
19 * nor the names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27 * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * John Heidemann of the UCLA Ficus project.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
68 *
69 * Ancestors:
70 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92
71 * Id: lofs_vnops.c,v 1.11 1992/05/30 10:05:43 jsp Exp jsp
72 * ...and...
73 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
74 */
75
76 /*
77 * Generic layer vnode operations.
78 *
79 * The layer.h, layer_extern.h, layer_vfs.c, and layer_vnops.c files provide
80 * the core implementation of stacked file-systems.
81 *
82 * The layerfs duplicates a portion of the file system name space under
83 * a new name. In this respect, it is similar to the loopback file system.
84 * It differs from the loopback fs in two respects: it is implemented using
85 * a stackable layers technique, and it is "layerfs-nodes" stack above all
86 * lower-layer vnodes, not just over directory vnodes.
87 *
88 * OPERATION OF LAYERFS
89 *
90 * The layerfs is the minimum file system layer, bypassing all possible
91 * operations to the lower layer for processing there. The majority of its
92 * activity centers on the bypass routine, through which nearly all vnode
93 * operations pass.
94 *
95 * The bypass routine accepts arbitrary vnode operations for handling by
96 * the lower layer. It begins by examining vnode operation arguments and
97 * replacing any layered nodes by their lower-layer equivalents. It then
98 * invokes an operation on the lower layer. Finally, it replaces the
99 * layered nodes in the arguments and, if a vnode is returned by the
100 * operation, stacks a layered node on top of the returned vnode.
101 *
102 * The bypass routine in this file, layer_bypass(), is suitable for use
103 * by many different layered filesystems. It can be used by multiple
104 * filesystems simultaneously. Alternatively, a layered fs may provide
105 * its own bypass routine, in which case layer_bypass() should be used as
106 * a model. For instance, the main functionality provided by umapfs, the user
107 * identity mapping file system, is handled by a custom bypass routine.
108 *
109 * Typically a layered fs registers its selected bypass routine as the
110 * default vnode operation in its vnodeopv_entry_desc table. Additionally
111 * the filesystem must store the bypass entry point in the layerm_bypass
112 * field of struct layer_mount. All other layer routines in this file will
113 * use the layerm_bypass() routine.
114 *
115 * Although the bypass routine handles most operations outright, a number
116 * of operations are special cased and handled by the layerfs. For instance,
117 * layer_getattr() must change the fsid being returned. While layer_lock()
118 * and layer_unlock() must handle any locking for the current vnode as well
119 * as pass the lock request down. layer_inactive() and layer_reclaim() are
120 * not bypassed so that they can handle freeing layerfs-specific data. Also,
121 * certain vnode operations (create, mknod, remove, link, rename, mkdir,
122 * rmdir, and symlink) change the locking state within the operation. Ideally
123 * these operations should not change the lock state, but should be changed
124 * to let the caller of the function unlock them. Otherwise, all intermediate
125 * vnode layers (such as union, umapfs, etc) must catch these functions to do
126 * the necessary locking at their layer.
127 *
128 * INSTANTIATING VNODE STACKS
129 *
130 * Mounting associates "layerfs-nodes" stack and lower layer, in effect
131 * stacking two VFSes. The initial mount creates a single vnode stack for
132 * the root of the new layerfs. All other vnode stacks are created as a
133 * result of vnode operations on this or other layerfs vnode stacks.
134 *
135 * New vnode stacks come into existence as a result of an operation which
136 * returns a vnode. The bypass routine stacks a layerfs-node above the new
137 * vnode before returning it to the caller.
138 *
139 * For example, imagine mounting a null layer with:
140 *
141 * "mount_null /usr/include /dev/layer/null"
142 *
143 * Changing directory to /dev/layer/null will assign the root layerfs-node,
144 * which was created when the null layer was mounted). Now consider opening
145 * "sys". A layer_lookup() would be performed on the root layerfs-node.
146 * This operation would bypass through to the lower layer which would return
147 * a vnode representing the UFS "sys". Then, layer_bypass() builds a
148 * layerfs-node aliasing the UFS "sys" and returns this to the caller.
149 * Later operations on the layerfs-node "sys" will repeat this process when
150 * constructing other vnode stacks.
151 *
152 * INVOKING OPERATIONS ON LOWER LAYERS
153 *
154 * There are two techniques to invoke operations on a lower layer when the
155 * operation cannot be completely bypassed. Each method is appropriate in
156 * different situations. In both cases, it is the responsibility of the
157 * aliasing layer to make the operation arguments "correct" for the lower
158 * layer by mapping any vnode arguments to the lower layer.
159 *
160 * The first approach is to call the aliasing layer's bypass routine. This
161 * method is most suitable when you wish to invoke the operation currently
162 * being handled on the lower layer. It has the advantage that the bypass
163 * routine already must do argument mapping. An example of this is
164 * layer_getattr().
165 *
166 * A second approach is to directly invoke vnode operations on the lower
167 * layer with the VOP_OPERATIONNAME interface. The advantage of this method
168 * is that it is easy to invoke arbitrary operations on the lower layer.
169 * The disadvantage is that vnode's arguments must be manually mapped.
170 */
171
172 #include <sys/cdefs.h>
173 __KERNEL_RCSID(0, "$NetBSD: layer_vnops.c,v 1.42 2010/07/02 03:16:01 rmind Exp $");
174
175 #include <sys/param.h>
176 #include <sys/systm.h>
177 #include <sys/proc.h>
178 #include <sys/time.h>
179 #include <sys/vnode.h>
180 #include <sys/mount.h>
181 #include <sys/namei.h>
182 #include <sys/kmem.h>
183 #include <sys/buf.h>
184 #include <sys/kauth.h>
185
186 #include <miscfs/genfs/layer.h>
187 #include <miscfs/genfs/layer_extern.h>
188 #include <miscfs/genfs/genfs.h>
189
190 /*
191 * This is the 08-June-99 bypass routine, based on the 10-Apr-92 bypass
192 * routine by John Heidemann.
193 * The new element for this version is that the whole nullfs
194 * system gained the concept of locks on the lower node.
195 * The 10-Apr-92 version was optimized for speed, throwing away some
196 * safety checks. It should still always work, but it's not as
197 * robust to programmer errors.
198 *
199 * In general, we map all vnodes going down and unmap them on the way back.
200 *
201 * Also, some BSD vnode operations have the side effect of vrele'ing
202 * their arguments. With stacking, the reference counts are held
203 * by the upper node, not the lower one, so we must handle these
204 * side-effects here. This is not of concern in Sun-derived systems
205 * since there are no such side-effects.
206 *
207 * New for the 08-June-99 version: we also handle operations which unlock
208 * the passed-in node (typically they vput the node).
209 *
210 * This makes the following assumptions:
211 * - only one returned vpp
212 * - no INOUT vpp's (Sun's vop_open has one of these)
213 * - the vnode operation vector of the first vnode should be used
214 * to determine what implementation of the op should be invoked
215 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
216 * problems on rmdir'ing mount points and renaming?)
217 */
218 int
219 layer_bypass(void *v)
220 {
221 struct vop_generic_args /* {
222 struct vnodeop_desc *a_desc;
223 <other random data follows, presumably>
224 } */ *ap = v;
225 int (**our_vnodeop_p)(void *);
226 struct vnode **this_vp_p;
227 int error;
228 struct vnode *old_vps[VDESC_MAX_VPS], *vp0;
229 struct vnode **vps_p[VDESC_MAX_VPS];
230 struct vnode ***vppp;
231 struct mount *mp;
232 struct vnodeop_desc *descp = ap->a_desc;
233 int reles, i, flags;
234
235 #ifdef DIAGNOSTIC
236 /*
237 * We require at least one vp.
238 */
239 if (descp->vdesc_vp_offsets == NULL ||
240 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
241 panic("%s: no vp's in map.\n", __func__);
242 #endif
243
244 vps_p[0] =
245 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
246 vp0 = *vps_p[0];
247 mp = vp0->v_mount;
248 flags = MOUNTTOLAYERMOUNT(mp)->layerm_flags;
249 our_vnodeop_p = vp0->v_op;
250
251 if (flags & LAYERFS_MBYPASSDEBUG)
252 printf("%s: %s\n", __func__, descp->vdesc_name);
253
254 /*
255 * Map the vnodes going in.
256 * Later, we'll invoke the operation based on
257 * the first mapped vnode's operation vector.
258 */
259 reles = descp->vdesc_flags;
260 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
261 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
262 break; /* bail out at end of list */
263 vps_p[i] = this_vp_p =
264 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i],
265 ap);
266 /*
267 * We're not guaranteed that any but the first vnode
268 * are of our type. Check for and don't map any
269 * that aren't. (We must always map first vp or vclean fails.)
270 */
271 if (i && (*this_vp_p == NULL ||
272 (*this_vp_p)->v_op != our_vnodeop_p)) {
273 old_vps[i] = NULL;
274 } else {
275 old_vps[i] = *this_vp_p;
276 *(vps_p[i]) = LAYERVPTOLOWERVP(*this_vp_p);
277 /*
278 * XXX - Several operations have the side effect
279 * of vrele'ing their vp's. We must account for
280 * that. (This should go away in the future.)
281 */
282 if (reles & VDESC_VP0_WILLRELE)
283 vref(*this_vp_p);
284 }
285 }
286
287 /*
288 * Call the operation on the lower layer
289 * with the modified argument structure.
290 */
291 error = VCALL(*vps_p[0], descp->vdesc_offset, ap);
292
293 /*
294 * Maintain the illusion of call-by-value
295 * by restoring vnodes in the argument structure
296 * to their original value.
297 */
298 reles = descp->vdesc_flags;
299 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
300 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
301 break; /* bail out at end of list */
302 if (old_vps[i]) {
303 *(vps_p[i]) = old_vps[i];
304 if (reles & VDESC_VP0_WILLRELE)
305 vrele(*(vps_p[i]));
306 }
307 }
308
309 /*
310 * Map the possible out-going vpp
311 * (Assumes that the lower layer always returns
312 * a VREF'ed vpp unless it gets an error.)
313 */
314 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
315 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
316 !error) {
317 /*
318 * XXX - even though some ops have vpp returned vp's,
319 * several ops actually vrele this before returning.
320 * We must avoid these ops.
321 * (This should go away when these ops are regularized.)
322 */
323 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
324 goto out;
325 vppp = VOPARG_OFFSETTO(struct vnode***,
326 descp->vdesc_vpp_offset, ap);
327 /*
328 * Only vop_lookup, vop_create, vop_makedir, vop_bmap,
329 * vop_mknod, and vop_symlink return vpp's. vop_bmap
330 * doesn't call bypass as the lower vpp is fine (we're just
331 * going to do i/o on it). vop_lookup doesn't call bypass
332 * as a lookup on "." would generate a locking error.
333 * So all the calls which get us here have a locked vpp. :-)
334 */
335 error = layer_node_create(mp, **vppp, *vppp);
336 if (error) {
337 vput(**vppp);
338 **vppp = NULL;
339 }
340 }
341 out:
342 return error;
343 }
344
345 /*
346 * We have to carry on the locking protocol on the layer vnodes
347 * as we progress through the tree. We also have to enforce read-only
348 * if this layer is mounted read-only.
349 */
350 int
351 layer_lookup(void *v)
352 {
353 struct vop_lookup_args /* {
354 struct vnodeop_desc *a_desc;
355 struct vnode * a_dvp;
356 struct vnode ** a_vpp;
357 struct componentname * a_cnp;
358 } */ *ap = v;
359 struct componentname *cnp = ap->a_cnp;
360 struct vnode *dvp, *lvp, *ldvp;
361 int error, flags = cnp->cn_flags;
362
363 dvp = ap->a_dvp;
364
365 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
366 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
367 return EROFS;
368
369 ldvp = LAYERVPTOLOWERVP(dvp);
370 ap->a_dvp = ldvp;
371 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap);
372 lvp = *ap->a_vpp;
373 *ap->a_vpp = NULL;
374
375 if (error == EJUSTRETURN && (flags & ISLASTCN) &&
376 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
377 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
378 error = EROFS;
379
380 /*
381 * We must do the same locking and unlocking at this layer as
382 * is done in the layers below us.
383 */
384 if (ldvp == lvp) {
385 /*
386 * Got the same object back, because we looked up ".",
387 * or ".." in the root node of a mount point.
388 * So we make another reference to dvp and return it.
389 */
390 vref(dvp);
391 *ap->a_vpp = dvp;
392 vrele(lvp);
393 } else if (lvp != NULL) {
394 /* Note: dvp, ldvp and lvp are all locked. */
395 error = layer_node_create(dvp->v_mount, lvp, ap->a_vpp);
396 if (error) {
397 vput(lvp);
398 }
399 }
400 return error;
401 }
402
403 /*
404 * Setattr call. Disallow write attempts if the layer is mounted read-only.
405 */
406 int
407 layer_setattr(void *v)
408 {
409 struct vop_setattr_args /* {
410 struct vnodeop_desc *a_desc;
411 struct vnode *a_vp;
412 struct vattr *a_vap;
413 kauth_cred_t a_cred;
414 struct lwp *a_l;
415 } */ *ap = v;
416 struct vnode *vp = ap->a_vp;
417 struct vattr *vap = ap->a_vap;
418
419 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
420 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
421 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
422 (vp->v_mount->mnt_flag & MNT_RDONLY))
423 return EROFS;
424 if (vap->va_size != VNOVAL) {
425 switch (vp->v_type) {
426 case VDIR:
427 return EISDIR;
428 case VCHR:
429 case VBLK:
430 case VSOCK:
431 case VFIFO:
432 return 0;
433 case VREG:
434 case VLNK:
435 default:
436 /*
437 * Disallow write attempts if the filesystem is
438 * mounted read-only.
439 */
440 if (vp->v_mount->mnt_flag & MNT_RDONLY)
441 return EROFS;
442 }
443 }
444 return LAYERFS_DO_BYPASS(vp, ap);
445 }
446
447 /*
448 * We handle getattr only to change the fsid.
449 */
450 int
451 layer_getattr(void *v)
452 {
453 struct vop_getattr_args /* {
454 struct vnode *a_vp;
455 struct vattr *a_vap;
456 kauth_cred_t a_cred;
457 struct lwp *a_l;
458 } */ *ap = v;
459 struct vnode *vp = ap->a_vp;
460 int error;
461
462 error = LAYERFS_DO_BYPASS(vp, ap);
463 if (error) {
464 return error;
465 }
466 /* Requires that arguments be restored. */
467 ap->a_vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
468 return 0;
469 }
470
471 int
472 layer_access(void *v)
473 {
474 struct vop_access_args /* {
475 struct vnode *a_vp;
476 int a_mode;
477 kauth_cred_t a_cred;
478 struct lwp *a_l;
479 } */ *ap = v;
480 struct vnode *vp = ap->a_vp;
481 mode_t mode = ap->a_mode;
482
483 /*
484 * Disallow write attempts on read-only layers;
485 * unless the file is a socket, fifo, or a block or
486 * character device resident on the file system.
487 */
488 if (mode & VWRITE) {
489 switch (vp->v_type) {
490 case VDIR:
491 case VLNK:
492 case VREG:
493 if (vp->v_mount->mnt_flag & MNT_RDONLY)
494 return EROFS;
495 break;
496 default:
497 break;
498 }
499 }
500 return LAYERFS_DO_BYPASS(vp, ap);
501 }
502
503 /*
504 * We must handle open to be able to catch MNT_NODEV and friends.
505 */
506 int
507 layer_open(void *v)
508 {
509 struct vop_open_args /* {
510 const struct vnodeop_desc *a_desc;
511 struct vnode *a_vp;
512 int a_mode;
513 kauth_cred_t a_cred;
514 } */ *ap = v;
515 struct vnode *vp = ap->a_vp;
516 enum vtype lower_type = LAYERVPTOLOWERVP(vp)->v_type;
517
518 if (((lower_type == VBLK) || (lower_type == VCHR)) &&
519 (vp->v_mount->mnt_flag & MNT_NODEV))
520 return ENXIO;
521
522 return LAYERFS_DO_BYPASS(vp, ap);
523 }
524
525 /*
526 * We need to clear the interlock flag as it applies only to our vnode,
527 * not the vnodes below us on the stack.
528 */
529 int
530 layer_lock(void *v)
531 {
532 struct vop_lock_args /* {
533 struct vnode *a_vp;
534 int a_flags;
535 struct proc *a_p;
536 } */ *ap = v;
537 struct vnode *vp = ap->a_vp;
538
539 if (ap->a_flags & LK_INTERLOCK) {
540 mutex_exit(&vp->v_interlock);
541 ap->a_flags &= ~LK_INTERLOCK;
542 }
543 return LAYERFS_DO_BYPASS(vp, ap);
544 }
545
546 int
547 layer_unlock(void *v)
548 {
549 struct vop_unlock_args /* {
550 struct vnode *a_vp;
551 int a_flags;
552 struct proc *a_p;
553 } */ *ap = v;
554 struct vnode *vp = ap->a_vp;
555
556 return LAYERFS_DO_BYPASS(vp, ap);
557 }
558
559 int
560 layer_islocked(void *v)
561 {
562 struct vop_islocked_args /* {
563 struct vnode *a_vp;
564 } */ *ap = v;
565 struct vnode *vp = ap->a_vp;
566
567 return LAYERFS_DO_BYPASS(vp, ap);
568 }
569
570 /*
571 * If vinvalbuf is calling us, it's a "shallow fsync" -- don't bother
572 * syncing the underlying vnodes, since they'll be fsync'ed when
573 * reclaimed; otherwise, pass it through to the underlying layer.
574 *
575 * XXX Do we still need to worry about shallow fsync?
576 */
577 int
578 layer_fsync(void *v)
579 {
580 struct vop_fsync_args /* {
581 struct vnode *a_vp;
582 kauth_cred_t a_cred;
583 int a_flags;
584 off_t offlo;
585 off_t offhi;
586 struct lwp *a_l;
587 } */ *ap = v;
588
589 if (ap->a_flags & FSYNC_RECLAIM) {
590 return 0;
591 }
592 return LAYERFS_DO_BYPASS(ap->a_vp, ap);
593 }
594
595 int
596 layer_inactive(void *v)
597 {
598 struct vop_inactive_args /* {
599 struct vnode *a_vp;
600 bool *a_recycle;
601 } */ *ap = v;
602 struct vnode *vp = ap->a_vp;
603
604 /*
605 * ..., but don't cache the device node. Also, if we did a
606 * remove, don't cache the node.
607 */
608 *ap->a_recycle = (vp->v_type == VBLK || vp->v_type == VCHR
609 || (VTOLAYER(vp)->layer_flags & LAYERFS_REMOVED));
610
611 /*
612 * Do nothing (and _don't_ bypass).
613 * Wait to vrele lowervp until reclaim,
614 * so that until then our layer_node is in the
615 * cache and reusable.
616 *
617 * NEEDSWORK: Someday, consider inactive'ing
618 * the lowervp and then trying to reactivate it
619 * with capabilities (v_id)
620 * like they do in the name lookup cache code.
621 * That's too much work for now.
622 */
623 VOP_UNLOCK(vp);
624 return 0;
625 }
626
627 int
628 layer_remove(void *v)
629 {
630 struct vop_remove_args /* {
631 struct vonde *a_dvp;
632 struct vnode *a_vp;
633 struct componentname *a_cnp;
634 } */ *ap = v;
635 struct vnode *vp = ap->a_vp;
636 int error;
637
638 vref(vp);
639 error = LAYERFS_DO_BYPASS(vp, ap);
640 if (error == 0) {
641 VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED;
642 }
643 vrele(vp);
644
645 return error;
646 }
647
648 int
649 layer_rename(void *v)
650 {
651 struct vop_rename_args /* {
652 struct vnode *a_fdvp;
653 struct vnode *a_fvp;
654 struct componentname *a_fcnp;
655 struct vnode *a_tdvp;
656 struct vnode *a_tvp;
657 struct componentname *a_tcnp;
658 } */ *ap = v;
659 struct vnode *fdvp = ap->a_fdvp, *tvp;
660 int error;
661
662 tvp = ap->a_tvp;
663 if (tvp) {
664 if (tvp->v_mount != fdvp->v_mount)
665 tvp = NULL;
666 else
667 vref(tvp);
668 }
669 error = LAYERFS_DO_BYPASS(fdvp, ap);
670 if (tvp) {
671 if (error == 0)
672 VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED;
673 vrele(tvp);
674 }
675 return error;
676 }
677
678 int
679 layer_rmdir(void *v)
680 {
681 struct vop_rmdir_args /* {
682 struct vnode *a_dvp;
683 struct vnode *a_vp;
684 struct componentname *a_cnp;
685 } */ *ap = v;
686 int error;
687 struct vnode *vp = ap->a_vp;
688
689 vref(vp);
690 error = LAYERFS_DO_BYPASS(vp, ap);
691 if (error == 0) {
692 VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED;
693 }
694 vrele(vp);
695
696 return error;
697 }
698
699 int
700 layer_reclaim(void *v)
701 {
702 struct vop_reclaim_args /* {
703 struct vnode *a_vp;
704 struct lwp *a_l;
705 } */ *ap = v;
706 struct vnode *vp = ap->a_vp;
707 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(vp->v_mount);
708 struct layer_node *xp = VTOLAYER(vp);
709 struct vnode *lowervp = xp->layer_lowervp;
710
711 /*
712 * Note: in vop_reclaim, the node's struct lock has been
713 * decomissioned, so we have to be careful about calling
714 * VOP's on ourself. We must be careful as VXLOCK is set.
715 */
716 if (vp == lmp->layerm_rootvp) {
717 /*
718 * Oops! We no longer have a root node. Most likely reason is
719 * that someone forcably unmunted the underlying fs.
720 *
721 * Now getting the root vnode will fail. We're dead. :-(
722 */
723 lmp->layerm_rootvp = NULL;
724 }
725 /* After this assignment, this node will not be re-used. */
726 xp->layer_lowervp = NULL;
727 mutex_enter(&lmp->layerm_hashlock);
728 LIST_REMOVE(xp, layer_hash);
729 mutex_exit(&lmp->layerm_hashlock);
730 kmem_free(vp->v_data, lmp->layerm_size);
731 vp->v_data = NULL;
732 vrele(lowervp);
733
734 return 0;
735 }
736
737 /*
738 * We just feed the returned vnode up to the caller - there's no need
739 * to build a layer node on top of the node on which we're going to do
740 * i/o. :-)
741 */
742 int
743 layer_bmap(void *v)
744 {
745 struct vop_bmap_args /* {
746 struct vnode *a_vp;
747 daddr_t a_bn;
748 struct vnode **a_vpp;
749 daddr_t *a_bnp;
750 int *a_runp;
751 } */ *ap = v;
752 struct vnode *vp;
753
754 vp = LAYERVPTOLOWERVP(ap->a_vp);
755 ap->a_vp = vp;
756
757 return VCALL(vp, ap->a_desc->vdesc_offset, ap);
758 }
759
760 int
761 layer_print(void *v)
762 {
763 struct vop_print_args /* {
764 struct vnode *a_vp;
765 } */ *ap = v;
766 struct vnode *vp = ap->a_vp;
767 printf ("\ttag VT_LAYERFS, vp=%p, lowervp=%p\n", vp, LAYERVPTOLOWERVP(vp));
768 return 0;
769 }
770
771 /*
772 * XXX - vop_bwrite must be hand coded because it has no
773 * vnode in its arguments.
774 * This goes away with a merged VM/buffer cache.
775 */
776 int
777 layer_bwrite(void *v)
778 {
779 struct vop_bwrite_args /* {
780 struct buf *a_bp;
781 } */ *ap = v;
782 struct buf *bp = ap->a_bp;
783 struct vnode *savedvp;
784 int error;
785
786 savedvp = bp->b_vp;
787 bp->b_vp = LAYERVPTOLOWERVP(bp->b_vp);
788 error = VOP_BWRITE(bp);
789 bp->b_vp = savedvp;
790
791 return error;
792 }
793
794 int
795 layer_getpages(void *v)
796 {
797 struct vop_getpages_args /* {
798 struct vnode *a_vp;
799 voff_t a_offset;
800 struct vm_page **a_m;
801 int *a_count;
802 int a_centeridx;
803 vm_prot_t a_access_type;
804 int a_advice;
805 int a_flags;
806 } */ *ap = v;
807 struct vnode *vp = ap->a_vp;
808 int error;
809
810 /*
811 * just pass the request on to the underlying layer.
812 */
813
814 if (ap->a_flags & PGO_LOCKED) {
815 return EBUSY;
816 }
817 ap->a_vp = LAYERVPTOLOWERVP(vp);
818 mutex_exit(&vp->v_interlock);
819 mutex_enter(&ap->a_vp->v_interlock);
820 error = VCALL(ap->a_vp, VOFFSET(vop_getpages), ap);
821 return error;
822 }
823
824 int
825 layer_putpages(void *v)
826 {
827 struct vop_putpages_args /* {
828 struct vnode *a_vp;
829 voff_t a_offlo;
830 voff_t a_offhi;
831 int a_flags;
832 } */ *ap = v;
833 struct vnode *vp = ap->a_vp;
834 int error;
835
836 /*
837 * just pass the request on to the underlying layer.
838 */
839
840 ap->a_vp = LAYERVPTOLOWERVP(vp);
841 mutex_exit(&vp->v_interlock);
842 if (ap->a_flags & PGO_RECLAIM) {
843 return 0;
844 }
845 mutex_enter(&ap->a_vp->v_interlock);
846 error = VCALL(ap->a_vp, VOFFSET(vop_putpages), ap);
847 return error;
848 }
849