xref: /csrg-svn/sys/miscfs/nullfs/null_vnops.c (revision 68349)
154753Sjohnh /*
263245Sbostic  * Copyright (c) 1992, 1993
363245Sbostic  *	The Regents of the University of California.  All rights reserved.
454753Sjohnh  *
554951Sheideman  * This code is derived from software contributed to Berkeley by
654951Sheideman  * John Heidemann of the UCLA Ficus project.
754753Sjohnh  *
854753Sjohnh  * %sccs.include.redist.c%
954753Sjohnh  *
10*68349Smckusick  *	@(#)null_vnops.c	8.3 (Berkeley) 02/16/95
1154766Sjohnh  *
1254766Sjohnh  * Ancestors:
1354753Sjohnh  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
1454766Sjohnh  *	$Id: lofs_vnops.c,v 1.11 1992/05/30 10:05:43 jsp Exp jsp $
1554766Sjohnh  *	...and...
1654766Sjohnh  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
1754753Sjohnh  */
1854753Sjohnh 
1954753Sjohnh /*
2054766Sjohnh  * Null Layer
2154766Sjohnh  *
2254951Sheideman  * (See mount_null(8) for more information.)
2354951Sheideman  *
2454766Sjohnh  * The null layer duplicates a portion of the file system
2554766Sjohnh  * name space under a new name.  In this respect, it is
2654766Sjohnh  * similar to the loopback file system.  It differs from
2754766Sjohnh  * the loopback fs in two respects:  it is implemented using
2854951Sheideman  * a stackable layers techniques, and it's "null-node"s stack above
2954766Sjohnh  * all lower-layer vnodes, not just over directory vnodes.
3054766Sjohnh  *
3154951Sheideman  * The null layer has two purposes.  First, it serves as a demonstration
3254951Sheideman  * of layering by proving a layer which does nothing.  (It actually
3354951Sheideman  * does everything the loopback file system does, which is slightly
3454951Sheideman  * more than nothing.)  Second, the null layer can serve as a prototype
3554951Sheideman  * layer.  Since it provides all necessary layer framework,
3654951Sheideman  * new file system layers can be created very easily be starting
3754951Sheideman  * with a null layer.
3854951Sheideman  *
3954951Sheideman  * The remainder of this man page examines the null layer as a basis
4054951Sheideman  * for constructing new layers.
4154951Sheideman  *
4254951Sheideman  *
4354951Sheideman  * INSTANTIATING NEW NULL LAYERS
4454951Sheideman  *
4554951Sheideman  * New null layers are created with mount_null(8).
4654951Sheideman  * Mount_null(8) takes two arguments, the pathname
4754951Sheideman  * of the lower vfs (target-pn) and the pathname where the null
4854951Sheideman  * layer will appear in the namespace (alias-pn).  After
4954951Sheideman  * the null layer is put into place, the contents
5054951Sheideman  * of target-pn subtree will be aliased under alias-pn.
5154951Sheideman  *
5254951Sheideman  *
5354951Sheideman  * OPERATION OF A NULL LAYER
5454951Sheideman  *
5554766Sjohnh  * The null layer is the minimum file system layer,
5654766Sjohnh  * simply bypassing all possible operations to the lower layer
5754951Sheideman  * for processing there.  The majority of its activity centers
5854951Sheideman  * on the bypass routine, though which nearly all vnode operations
5954951Sheideman  * pass.
6054766Sjohnh  *
6154951Sheideman  * The bypass routine accepts arbitrary vnode operations for
6254951Sheideman  * handling by the lower layer.  It begins by examing vnode
6354951Sheideman  * operation arguments and replacing any null-nodes by their
6454951Sheideman  * lower-layer equivlants.  It then invokes the operation
6554951Sheideman  * on the lower layer.  Finally, it replaces the null-nodes
6654951Sheideman  * in the arguments and, if a vnode is return by the operation,
6754951Sheideman  * stacks a null-node on top of the returned vnode.
6854951Sheideman  *
6954951Sheideman  * Although bypass handles most operations,
7054951Sheideman  * vop_getattr, _inactive, _reclaim, and _print are not bypassed.
7154951Sheideman  * Vop_getattr must change the fsid being returned.
7254951Sheideman  * Vop_inactive and vop_reclaim are not bypassed so that
7354766Sjohnh  * they can handle freeing null-layer specific data.
7454951Sheideman  * Vop_print is not bypassed to avoid excessive debugging
7554951Sheideman  * information.
7654766Sjohnh  *
7754893Sheideman  *
7854951Sheideman  * INSTANTIATING VNODE STACKS
7954893Sheideman  *
8054951Sheideman  * Mounting associates the null layer with a lower layer,
8154951Sheideman  * effect stacking two VFSes.  Vnode stacks are instead
8254951Sheideman  * created on demand as files are accessed.
8354893Sheideman  *
8454951Sheideman  * The initial mount creates a single vnode stack for the
8554951Sheideman  * root of the new null layer.  All other vnode stacks
8654951Sheideman  * are created as a result of vnode operations on
8754951Sheideman  * this or other null vnode stacks.
8854893Sheideman  *
8954951Sheideman  * New vnode stacks come into existance as a result of
9054951Sheideman  * an operation which returns a vnode.
9154951Sheideman  * The bypass routine stacks a null-node above the new
9254951Sheideman  * vnode before returning it to the caller.
9354893Sheideman  *
9454951Sheideman  * For example, imagine mounting a null layer with
9554951Sheideman  * "mount_null /usr/include /dev/layer/null".
9655025Smckusick  * Changing directory to /dev/layer/null will assign
9754951Sheideman  * the root null-node (which was created when the null layer was mounted).
9854951Sheideman  * Now consider opening "sys".  A vop_lookup would be
9954951Sheideman  * done on the root null-node.  This operation would bypass through
10054951Sheideman  * to the lower layer which would return a vnode representing
10154951Sheideman  * the UFS "sys".  Null_bypass then builds a null-node
10254951Sheideman  * aliasing the UFS "sys" and returns this to the caller.
10354951Sheideman  * Later operations on the null-node "sys" will repeat this
10454951Sheideman  * process when constructing other vnode stacks.
10554951Sheideman  *
10654951Sheideman  *
10754951Sheideman  * CREATING OTHER FILE SYSTEM LAYERS
10854951Sheideman  *
10954893Sheideman  * One of the easiest ways to construct new file system layers is to make
11054893Sheideman  * a copy of the null layer, rename all files and variables, and
11154893Sheideman  * then begin modifing the copy.  Sed can be used to easily rename
11254893Sheideman  * all variables.
11354893Sheideman  *
11454951Sheideman  * The umap layer is an example of a layer descended from the
11554951Sheideman  * null layer.
11654951Sheideman  *
11754951Sheideman  *
11854951Sheideman  * INVOKING OPERATIONS ON LOWER LAYERS
11954951Sheideman  *
12054951Sheideman  * There are two techniques to invoke operations on a lower layer
12154951Sheideman  * when the operation cannot be completely bypassed.  Each method
12254951Sheideman  * is appropriate in different situations.  In both cases,
12354951Sheideman  * it is the responsibility of the aliasing layer to make
12454951Sheideman  * the operation arguments "correct" for the lower layer
12554951Sheideman  * by mapping an vnode arguments to the lower layer.
12654951Sheideman  *
12754951Sheideman  * The first approach is to call the aliasing layer's bypass routine.
12854951Sheideman  * This method is most suitable when you wish to invoke the operation
12954951Sheideman  * currently being hanldled on the lower layer.  It has the advantage
13055025Smckusick  * that the bypass routine already must do argument mapping.
13154951Sheideman  * An example of this is null_getattrs in the null layer.
13254951Sheideman  *
13354951Sheideman  * A second approach is to directly invoked vnode operations on
13454951Sheideman  * the lower layer with the VOP_OPERATIONNAME interface.
13554951Sheideman  * The advantage of this method is that it is easy to invoke
13654951Sheideman  * arbitrary operations on the lower layer.  The disadvantage
13754951Sheideman  * is that vnodes arguments must be manualy mapped.
13854951Sheideman  *
13954753Sjohnh  */
14054753Sjohnh 
14154753Sjohnh #include <sys/param.h>
14254753Sjohnh #include <sys/systm.h>
14354753Sjohnh #include <sys/proc.h>
14454753Sjohnh #include <sys/time.h>
14554753Sjohnh #include <sys/types.h>
14654753Sjohnh #include <sys/vnode.h>
14754753Sjohnh #include <sys/mount.h>
14854753Sjohnh #include <sys/namei.h>
14954753Sjohnh #include <sys/malloc.h>
15054753Sjohnh #include <sys/buf.h>
15155025Smckusick #include <miscfs/nullfs/null.h>
15254753Sjohnh 
15354753Sjohnh 
15454766Sjohnh int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
15554753Sjohnh 
15654753Sjohnh /*
15754766Sjohnh  * This is the 10-Apr-92 bypass routine.
15854766Sjohnh  *    This version has been optimized for speed, throwing away some
15954766Sjohnh  * safety checks.  It should still always work, but it's not as
16054766Sjohnh  * robust to programmer errors.
16154766Sjohnh  *    Define SAFETY to include some error checking code.
16254766Sjohnh  *
16354766Sjohnh  * In general, we map all vnodes going down and unmap them on the way back.
16454766Sjohnh  * As an exception to this, vnodes can be marked "unmapped" by setting
16554766Sjohnh  * the Nth bit in operation's vdesc_flags.
16654766Sjohnh  *
16754766Sjohnh  * Also, some BSD vnode operations have the side effect of vrele'ing
16854766Sjohnh  * their arguments.  With stacking, the reference counts are held
16954766Sjohnh  * by the upper node, not the lower one, so we must handle these
17054766Sjohnh  * side-effects here.  This is not of concern in Sun-derived systems
17154766Sjohnh  * since there are no such side-effects.
17254766Sjohnh  *
17354766Sjohnh  * This makes the following assumptions:
17454766Sjohnh  * - only one returned vpp
17554766Sjohnh  * - no INOUT vpp's (Sun's vop_open has one of these)
17654766Sjohnh  * - the vnode operation vector of the first vnode should be used
17754766Sjohnh  *   to determine what implementation of the op should be invoked
17854766Sjohnh  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
17954766Sjohnh  *   problems on rmdir'ing mount points and renaming?)
18054766Sjohnh  */
18154766Sjohnh int
18254766Sjohnh null_bypass(ap)
18355025Smckusick 	struct vop_generic_args /* {
18455025Smckusick 		struct vnodeop_desc *a_desc;
18555025Smckusick 		<other random data follows, presumably>
18655025Smckusick 	} */ *ap;
18754753Sjohnh {
18854893Sheideman 	extern int (**null_vnodeop_p)();  /* not extern, really "forward" */
18954893Sheideman 	register struct vnode **this_vp_p;
19054753Sjohnh 	int error;
19154766Sjohnh 	struct vnode *old_vps[VDESC_MAX_VPS];
19254766Sjohnh 	struct vnode **vps_p[VDESC_MAX_VPS];
19354766Sjohnh 	struct vnode ***vppp;
19454766Sjohnh 	struct vnodeop_desc *descp = ap->a_desc;
19554893Sheideman 	int reles, i;
19654753Sjohnh 
19754766Sjohnh 	if (null_bug_bypass)
19854766Sjohnh 		printf ("null_bypass: %s\n", descp->vdesc_name);
19954753Sjohnh 
20054766Sjohnh #ifdef SAFETY
20154753Sjohnh 	/*
20254766Sjohnh 	 * We require at least one vp.
20354753Sjohnh 	 */
20454938Sheideman 	if (descp->vdesc_vp_offsets == NULL ||
20554938Sheideman 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
20654766Sjohnh 		panic ("null_bypass: no vp's in map.\n");
20754753Sjohnh #endif
20854753Sjohnh 
20954753Sjohnh 	/*
21054766Sjohnh 	 * Map the vnodes going in.
21154766Sjohnh 	 * Later, we'll invoke the operation based on
21254766Sjohnh 	 * the first mapped vnode's operation vector.
21354753Sjohnh 	 */
21454893Sheideman 	reles = descp->vdesc_flags;
21554938Sheideman 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
21654938Sheideman 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
21754766Sjohnh 			break;   /* bail out at end of list */
21854766Sjohnh 		vps_p[i] = this_vp_p =
21954766Sjohnh 			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
22054893Sheideman 		/*
22154893Sheideman 		 * We're not guaranteed that any but the first vnode
22254893Sheideman 		 * are of our type.  Check for and don't map any
22354951Sheideman 		 * that aren't.  (We must always map first vp or vclean fails.)
22454893Sheideman 		 */
225*68349Smckusick 		if (i && (*this_vp_p == NULL ||
226*68349Smckusick 		    (*this_vp_p)->v_op != null_vnodeop_p)) {
22754893Sheideman 			old_vps[i] = NULL;
22854893Sheideman 		} else {
22954893Sheideman 			old_vps[i] = *this_vp_p;
23054893Sheideman 			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
23154938Sheideman 			/*
23254938Sheideman 			 * XXX - Several operations have the side effect
23354938Sheideman 			 * of vrele'ing their vp's.  We must account for
23454938Sheideman 			 * that.  (This should go away in the future.)
23554938Sheideman 			 */
23654893Sheideman 			if (reles & 1)
23754893Sheideman 				VREF(*this_vp_p);
23854938Sheideman 		}
23954766Sjohnh 
24054938Sheideman 	}
24154753Sjohnh 
24254753Sjohnh 	/*
24354766Sjohnh 	 * Call the operation on the lower layer
24454766Sjohnh 	 * with the modified argument structure.
24554753Sjohnh 	 */
24654766Sjohnh 	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
24754753Sjohnh 
24854753Sjohnh 	/*
24954766Sjohnh 	 * Maintain the illusion of call-by-value
25054766Sjohnh 	 * by restoring vnodes in the argument structure
25154766Sjohnh 	 * to their original value.
25254753Sjohnh 	 */
25354893Sheideman 	reles = descp->vdesc_flags;
25454938Sheideman 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
25554938Sheideman 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
25654766Sjohnh 			break;   /* bail out at end of list */
25754893Sheideman 		if (old_vps[i]) {
25854893Sheideman 			*(vps_p[i]) = old_vps[i];
25954893Sheideman 			if (reles & 1)
26054893Sheideman 				vrele(*(vps_p[i]));
26154938Sheideman 		}
26254938Sheideman 	}
26354766Sjohnh 
26454753Sjohnh 	/*
26554938Sheideman 	 * Map the possible out-going vpp
26654938Sheideman 	 * (Assumes that the lower layer always returns
26754938Sheideman 	 * a VREF'ed vpp unless it gets an error.)
26854753Sjohnh 	 */
26954766Sjohnh 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
27054766Sjohnh 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
27154766Sjohnh 	    !error) {
27254938Sheideman 		/*
27354938Sheideman 		 * XXX - even though some ops have vpp returned vp's,
27454938Sheideman 		 * several ops actually vrele this before returning.
27554938Sheideman 		 * We must avoid these ops.
27654951Sheideman 		 * (This should go away when these ops are regularized.)
27754938Sheideman 		 */
27854951Sheideman 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
27954951Sheideman 			goto out;
28054938Sheideman 		vppp = VOPARG_OFFSETTO(struct vnode***,
28154766Sjohnh 				 descp->vdesc_vpp_offset,ap);
28254893Sheideman 		error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
28354938Sheideman 	}
28454753Sjohnh 
28554951Sheideman  out:
28654766Sjohnh 	return (error);
28754753Sjohnh }
28854753Sjohnh 
28954753Sjohnh 
29054753Sjohnh /*
29154951Sheideman  *  We handle getattr only to change the fsid.
29254753Sjohnh  */
29354766Sjohnh int
29454766Sjohnh null_getattr(ap)
29555025Smckusick 	struct vop_getattr_args /* {
29655025Smckusick 		struct vnode *a_vp;
29755025Smckusick 		struct vattr *a_vap;
29855025Smckusick 		struct ucred *a_cred;
29955025Smckusick 		struct proc *a_p;
30055025Smckusick 	} */ *ap;
30154753Sjohnh {
30254753Sjohnh 	int error;
30354938Sheideman 	if (error = null_bypass(ap))
30455025Smckusick 		return (error);
30554766Sjohnh 	/* Requires that arguments be restored. */
30654766Sjohnh 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
30755025Smckusick 	return (0);
30854766Sjohnh }
30954753Sjohnh 
31054753Sjohnh 
31154766Sjohnh int
31255025Smckusick null_inactive(ap)
31355025Smckusick 	struct vop_inactive_args /* {
31455025Smckusick 		struct vnode *a_vp;
31555025Smckusick 	} */ *ap;
31654753Sjohnh {
31754766Sjohnh 	/*
31854766Sjohnh 	 * Do nothing (and _don't_ bypass).
31954766Sjohnh 	 * Wait to vrele lowervp until reclaim,
32054766Sjohnh 	 * so that until then our null_node is in the
32154766Sjohnh 	 * cache and reusable.
32254766Sjohnh 	 *
32354766Sjohnh 	 * NEEDSWORK: Someday, consider inactive'ing
32454766Sjohnh 	 * the lowervp and then trying to reactivate it
32554951Sheideman 	 * with capabilities (v_id)
32654766Sjohnh 	 * like they do in the name lookup cache code.
32754766Sjohnh 	 * That's too much work for now.
32854766Sjohnh 	 */
32955025Smckusick 	return (0);
33054753Sjohnh }
33154753Sjohnh 
33254938Sheideman int
33355025Smckusick null_reclaim(ap)
33455025Smckusick 	struct vop_reclaim_args /* {
33555025Smckusick 		struct vnode *a_vp;
33655025Smckusick 	} */ *ap;
33754753Sjohnh {
33854938Sheideman 	struct vnode *vp = ap->a_vp;
33954938Sheideman 	struct null_node *xp = VTONULL(vp);
34054938Sheideman 	struct vnode *lowervp = xp->null_lowervp;
34154938Sheideman 
34254938Sheideman 	/*
34354951Sheideman 	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
34454938Sheideman 	 * so we can't call VOPs on ourself.
34554938Sheideman 	 */
34654938Sheideman 	/* After this assignment, this node will not be re-used. */
34754938Sheideman 	xp->null_lowervp = NULL;
34867716Smckusick 	LIST_REMOVE(xp, null_hash);
34954938Sheideman 	FREE(vp->v_data, M_TEMP);
35054938Sheideman 	vp->v_data = NULL;
35154938Sheideman 	vrele (lowervp);
35255025Smckusick 	return (0);
35354753Sjohnh }
35454753Sjohnh 
35554951Sheideman 
35654938Sheideman int
35755025Smckusick null_print(ap)
35855025Smckusick 	struct vop_print_args /* {
35955025Smckusick 		struct vnode *a_vp;
36055025Smckusick 	} */ *ap;
36154753Sjohnh {
36254951Sheideman 	register struct vnode *vp = ap->a_vp;
36354951Sheideman 	printf ("\ttag VT_NULLFS, vp=%x, lowervp=%x\n", vp, NULLVPTOLOWERVP(vp));
36455025Smckusick 	return (0);
36554753Sjohnh }
36654753Sjohnh 
36754951Sheideman 
36854951Sheideman /*
36954951Sheideman  * XXX - vop_strategy must be hand coded because it has no
37054951Sheideman  * vnode in its arguments.
37154951Sheideman  * This goes away with a merged VM/buffer cache.
37254951Sheideman  */
37354938Sheideman int
37455025Smckusick null_strategy(ap)
37555025Smckusick 	struct vop_strategy_args /* {
37655025Smckusick 		struct buf *a_bp;
37755025Smckusick 	} */ *ap;
37854753Sjohnh {
37954951Sheideman 	struct buf *bp = ap->a_bp;
38054753Sjohnh 	int error;
38154766Sjohnh 	struct vnode *savedvp;
38254753Sjohnh 
38354951Sheideman 	savedvp = bp->b_vp;
38454951Sheideman 	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
38554753Sjohnh 
38654951Sheideman 	error = VOP_STRATEGY(bp);
38754753Sjohnh 
38854951Sheideman 	bp->b_vp = savedvp;
38954753Sjohnh 
39055025Smckusick 	return (error);
39154753Sjohnh }
39254753Sjohnh 
39354766Sjohnh 
39454951Sheideman /*
39554951Sheideman  * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
39654951Sheideman  * vnode in its arguments.
39754951Sheideman  * This goes away with a merged VM/buffer cache.
39854951Sheideman  */
39954766Sjohnh int
40055025Smckusick null_bwrite(ap)
40155025Smckusick 	struct vop_bwrite_args /* {
40255025Smckusick 		struct buf *a_bp;
40355025Smckusick 	} */ *ap;
40454753Sjohnh {
40554951Sheideman 	struct buf *bp = ap->a_bp;
40654951Sheideman 	int error;
40754951Sheideman 	struct vnode *savedvp;
40854753Sjohnh 
40954951Sheideman 	savedvp = bp->b_vp;
41054951Sheideman 	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
41154753Sjohnh 
41254951Sheideman 	error = VOP_BWRITE(bp);
41354951Sheideman 
41454951Sheideman 	bp->b_vp = savedvp;
41554951Sheideman 
41655025Smckusick 	return (error);
41754944Sheideman }
41854944Sheideman 
41954753Sjohnh /*
42054766Sjohnh  * Global vfs data structures
42154753Sjohnh  */
42254766Sjohnh int (**null_vnodeop_p)();
42354893Sheideman struct vnodeopv_entry_desc null_vnodeop_entries[] = {
42454766Sjohnh 	{ &vop_default_desc, null_bypass },
42554753Sjohnh 
42654766Sjohnh 	{ &vop_getattr_desc, null_getattr },
42754766Sjohnh 	{ &vop_inactive_desc, null_inactive },
42854766Sjohnh 	{ &vop_reclaim_desc, null_reclaim },
42954766Sjohnh 	{ &vop_print_desc, null_print },
43054753Sjohnh 
43154766Sjohnh 	{ &vop_strategy_desc, null_strategy },
43254951Sheideman 	{ &vop_bwrite_desc, null_bwrite },
43354753Sjohnh 
43454753Sjohnh 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
43554753Sjohnh };
43654893Sheideman struct vnodeopv_desc null_vnodeop_opv_desc =
43754893Sheideman 	{ &null_vnodeop_p, null_vnodeop_entries };
438