null_vnops.c revision 24987
150477Speter/*
22729Sdfr * Copyright (c) 1992, 1993
32729Sdfr *	The Regents of the University of California.  All rights reserved.
42729Sdfr *
52729Sdfr * This code is derived from software contributed to Berkeley by
62729Sdfr * John Heidemann of the UCLA Ficus project.
72729Sdfr *
82729Sdfr * Redistribution and use in source and binary forms, with or without
92729Sdfr * modification, are permitted provided that the following conditions
102729Sdfr * are met:
112729Sdfr * 1. Redistributions of source code must retain the above copyright
122729Sdfr *    notice, this list of conditions and the following disclaimer.
132729Sdfr * 2. Redistributions in binary form must reproduce the above copyright
142729Sdfr *    notice, this list of conditions and the following disclaimer in the
152729Sdfr *    documentation and/or other materials provided with the distribution.
162729Sdfr * 3. All advertising materials mentioning features or use of this software
172729Sdfr *    must display the following acknowledgement:
182729Sdfr *	This product includes software developed by the University of
192729Sdfr *	California, Berkeley and its contributors.
202729Sdfr * 4. Neither the name of the University nor the names of its contributors
212729Sdfr *    may be used to endorse or promote products derived from this software
2259839Speter *    without specific prior written permission.
2359839Speter *
242729Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
252729Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2611626Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
272729Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
282729Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2982607Sdillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3082607Sdillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
312729Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3269449Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3311626Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3459839Speter * SUCH DAMAGE.
3559839Speter *
3668024Srwatson *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
372729Sdfr *
3859839Speter * Ancestors:
3959839Speter *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
4069449Salfred *	$Id: null_vnops.c,v 1.16 1997/02/22 09:40:22 peter Exp $
4169449Salfred *	...and...
4269449Salfred *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
4310358Sjulian *
442729Sdfr * $Id: null_vnops.c,v 1.16 1997/02/22 09:40:22 peter Exp $
452729Sdfr */
462729Sdfr
4711626Sbde/*
482729Sdfr * Null Layer
4911626Sbde *
5012819Sphk * (See mount_null(8) for more information.)
5111626Sbde *
5211626Sbde * The null layer duplicates a portion of the file system
5311626Sbde * name space under a new name.  In this respect, it is
542729Sdfr * similar to the loopback file system.  It differs from
5559839Speter * the loopback fs in two respects:  it is implemented using
5659839Speter * a stackable layers techniques, and it's "null-node"s stack above
5759839Speter * all lower-layer vnodes, not just over directory vnodes.
5859839Speter *
5959839Speter * The null layer has two purposes.  First, it serves as a demonstration
6059839Speter * of layering by proving a layer which does nothing.  (It actually
6159839Speter * does everything the loopback file system does, which is slightly
6259839Speter * more than nothing.)  Second, the null layer can serve as a prototype
6359839Speter * layer.  Since it provides all necessary layer framework,
6459839Speter * new file system layers can be created very easily be starting
6559839Speter * with a null layer.
6659839Speter *
6759839Speter * The remainder of this man page examines the null layer as a basis
6859839Speter * for constructing new layers.
6959839Speter *
7059839Speter *
7159839Speter * INSTANTIATING NEW NULL LAYERS
7259839Speter *
7359839Speter * New null layers are created with mount_null(8).
7459839Speter * Mount_null(8) takes two arguments, the pathname
7559839Speter * of the lower vfs (target-pn) and the pathname where the null
7659839Speter * layer will appear in the namespace (alias-pn).  After
7759839Speter * the null layer is put into place, the contents
7859839Speter * of target-pn subtree will be aliased under alias-pn.
7959839Speter *
8059839Speter *
8159839Speter * OPERATION OF A NULL LAYER
8259839Speter *
8359839Speter * The null layer is the minimum file system layer,
8459839Speter * simply bypassing all possible operations to the lower layer
8559839Speter * for processing there.  The majority of its activity centers
8659839Speter * on the bypass routine, though which nearly all vnode operations
8759839Speter * pass.
8859839Speter *
8959839Speter * The bypass routine accepts arbitrary vnode operations for
9059839Speter * handling by the lower layer.  It begins by examing vnode
9159839Speter * operation arguments and replacing any null-nodes by their
9259839Speter * lower-layer equivlants.  It then invokes the operation
9359839Speter * on the lower layer.  Finally, it replaces the null-nodes
9459839Speter * in the arguments and, if a vnode is return by the operation,
9559839Speter * stacks a null-node on top of the returned vnode.
9659839Speter *
9759839Speter * Although bypass handles most operations, vop_getattr, vop_lock,
9859839Speter * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
9959839Speter * bypassed. Vop_getattr must change the fsid being returned.
10059839Speter * Vop_lock and vop_unlock must handle any locking for the
10159839Speter * current vnode as well as pass the lock request down.
10259839Speter * Vop_inactive and vop_reclaim are not bypassed so that
10359839Speter * they can handle freeing null-layer specific data. Vop_print
10459839Speter * is not bypassed to avoid excessive debugging information.
10559839Speter * Also, certain vnode operations change the locking state within
10659839Speter * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
10759839Speter * and symlink). Ideally these operations should not change the
10859839Speter * lock state, but should be changed to let the caller of the
10959839Speter * function unlock them. Otherwise all intermediate vnode layers
11059839Speter * (such as union, umapfs, etc) must catch these functions to do
11159839Speter * the necessary locking at their layer.
11259839Speter *
11359839Speter *
11459839Speter * INSTANTIATING VNODE STACKS
11559839Speter *
11659839Speter * Mounting associates the null layer with a lower layer,
11759839Speter * effect stacking two VFSes.  Vnode stacks are instead
11859839Speter * created on demand as files are accessed.
11959839Speter *
12059839Speter * The initial mount creates a single vnode stack for the
12159839Speter * root of the new null layer.  All other vnode stacks
12212819Sphk * are created as a result of vnode operations on
12312819Sphk * this or other null vnode stacks.
12459839Speter *
12559839Speter * New vnode stacks come into existance as a result of
12659839Speter * an operation which returns a vnode.
12759839Speter * The bypass routine stacks a null-node above the new
12859839Speter * vnode before returning it to the caller.
1292729Sdfr *
13059839Speter * For example, imagine mounting a null layer with
13169449Salfred * "mount_null /usr/include /dev/layer/null".
1322729Sdfr * Changing directory to /dev/layer/null will assign
1332729Sdfr * the root null-node (which was created when the null layer was mounted).
1342729Sdfr * Now consider opening "sys".  A vop_lookup would be
13559839Speter * done on the root null-node.  This operation would bypass through
13659839Speter * to the lower layer which would return a vnode representing
13759839Speter * the UFS "sys".  Null_bypass then builds a null-node
13859839Speter * aliasing the UFS "sys" and returns this to the caller.
13959839Speter * Later operations on the null-node "sys" will repeat this
14059839Speter * process when constructing other vnode stacks.
14159839Speter *
14259839Speter *
14359839Speter * CREATING OTHER FILE SYSTEM LAYERS
14459839Speter *
14559839Speter * One of the easiest ways to construct new file system layers is to make
14659839Speter * a copy of the null layer, rename all files and variables, and
14759839Speter * then begin modifing the copy.  Sed can be used to easily rename
1482729Sdfr * all variables.
1492729Sdfr *
1502729Sdfr * The umap layer is an example of a layer descended from the
1512729Sdfr * null layer.
1522729Sdfr *
1532729Sdfr *
1542729Sdfr * INVOKING OPERATIONS ON LOWER LAYERS
1552729Sdfr *
1562729Sdfr * There are two techniques to invoke operations on a lower layer
1572729Sdfr * when the operation cannot be completely bypassed.  Each method
1582729Sdfr * is appropriate in different situations.  In both cases,
1592729Sdfr * it is the responsibility of the aliasing layer to make
1602729Sdfr * the operation arguments "correct" for the lower layer
1612729Sdfr * by mapping an vnode arguments to the lower layer.
1622729Sdfr *
1632729Sdfr * The first approach is to call the aliasing layer's bypass routine.
1642729Sdfr * This method is most suitable when you wish to invoke the operation
1652729Sdfr * currently being hanldled on the lower layer.  It has the advantage
1662729Sdfr * that the bypass routine already must do argument mapping.
1672729Sdfr * An example of this is null_getattrs in the null layer.
1682729Sdfr *
1692729Sdfr * A second approach is to directly invoked vnode operations on
1702729Sdfr * the lower layer with the VOP_OPERATIONNAME interface.
1712729Sdfr * The advantage of this method is that it is easy to invoke
1722729Sdfr * arbitrary operations on the lower layer.  The disadvantage
1732729Sdfr * is that vnodes arguments must be manualy mapped.
1742729Sdfr *
1752729Sdfr */
1762729Sdfr
1772729Sdfr#include <sys/param.h>
1782729Sdfr#include <sys/systm.h>
1792729Sdfr#include <sys/kernel.h>
1802729Sdfr#include <sys/sysctl.h>
1812729Sdfr#include <sys/proc.h>
1822729Sdfr#include <sys/time.h>
1832729Sdfr#include <sys/types.h>
1842729Sdfr#include <sys/vnode.h>
1852729Sdfr#include <sys/mount.h>
1862729Sdfr#include <sys/namei.h>
1872729Sdfr#include <sys/malloc.h>
1882729Sdfr#include <sys/buf.h>
1892729Sdfr#include <miscfs/nullfs/null.h>
1902729Sdfr
1912729Sdfrstatic int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
1922729SdfrSYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
1932729Sdfr	&null_bug_bypass, 0, "");
1942729Sdfr
1952729Sdfrstatic int	null_access __P((struct vop_access_args *ap));
19666085Speterint		null_bypass __P((struct vop_generic_args *ap));
1972729Sdfrstatic int	null_bwrite __P((struct vop_bwrite_args *ap));
1982729Sdfrstatic int	null_getattr __P((struct vop_getattr_args *ap));
1992729Sdfrstatic int	null_inactive __P((struct vop_inactive_args *ap));
20069449Salfredstatic int	null_lock __P((struct vop_lock_args *ap));
20169449Salfredstatic int	null_lookup __P((struct vop_lookup_args *ap));
20269449Salfredstatic int	null_print __P((struct vop_print_args *ap));
20369449Salfredstatic int	null_reclaim __P((struct vop_reclaim_args *ap));
20469449Salfredstatic int	null_setattr __P((struct vop_setattr_args *ap));
20569449Salfredstatic int	null_strategy __P((struct vop_strategy_args *ap));
20669449Salfredstatic int	null_unlock __P((struct vop_unlock_args *ap));
20769449Salfred
20869449Salfred/*
20969449Salfred * This is the 10-Apr-92 bypass routine.
21069449Salfred *    This version has been optimized for speed, throwing away some
21169449Salfred * safety checks.  It should still always work, but it's not as
21269449Salfred * robust to programmer errors.
21369449Salfred *    Define SAFETY to include some error checking code.
21469449Salfred *
21569449Salfred * In general, we map all vnodes going down and unmap them on the way back.
21669449Salfred * As an exception to this, vnodes can be marked "unmapped" by setting
21769449Salfred * the Nth bit in operation's vdesc_flags.
21869449Salfred *
21969449Salfred * Also, some BSD vnode operations have the side effect of vrele'ing
22069449Salfred * their arguments.  With stacking, the reference counts are held
22169449Salfred * by the upper node, not the lower one, so we must handle these
22269449Salfred * side-effects here.  This is not of concern in Sun-derived systems
22369449Salfred * since there are no such side-effects.
22469449Salfred *
22569449Salfred * This makes the following assumptions:
22669449Salfred * - only one returned vpp
22769449Salfred * - no INOUT vpp's (Sun's vop_open has one of these)
22869449Salfred * - the vnode operation vector of the first vnode should be used
22969449Salfred *   to determine what implementation of the op should be invoked
23069449Salfred * - all mapped vnodes are of our vnode-type (NEEDSWORK:
23169449Salfred *   problems on rmdir'ing mount points and renaming?)
23269449Salfred */
23369449Salfredint
23469449Salfrednull_bypass(ap)
23569449Salfred	struct vop_generic_args /* {
23669449Salfred		struct vnodeop_desc *a_desc;
23769449Salfred		<other random data follows, presumably>
23869449Salfred	} */ *ap;
23969449Salfred{
24069449Salfred	register struct vnode **this_vp_p;
24169449Salfred	int error;
24269449Salfred	struct vnode *old_vps[VDESC_MAX_VPS];
24369449Salfred	struct vnode **vps_p[VDESC_MAX_VPS];
24469449Salfred	struct vnode ***vppp;
24569449Salfred	struct vnodeop_desc *descp = ap->a_desc;
24669449Salfred	int reles, i;
24769449Salfred
24869449Salfred	if (null_bug_bypass)
24969449Salfred		printf ("null_bypass: %s\n", descp->vdesc_name);
25071038Sdes
25171038Sdes#ifdef SAFETY
25269449Salfred	/*
25369449Salfred	 * We require at least one vp.
25469449Salfred	 */
25569449Salfred	if (descp->vdesc_vp_offsets == NULL ||
25669644Salfred	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
25769449Salfred		panic ("null_bypass: no vp's in map.");
25869449Salfred#endif
25969449Salfred
26069449Salfred	/*
26169449Salfred	 * Map the vnodes going in.
26271038Sdes	 * Later, we'll invoke the operation based on
26369449Salfred	 * the first mapped vnode's operation vector.
26471038Sdes	 */
26569449Salfred	reles = descp->vdesc_flags;
2662729Sdfr	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
2672729Sdfr		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
26882607Sdillon			break;   /* bail out at end of list */
26982607Sdillon		vps_p[i] = this_vp_p =
2702729Sdfr			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
2712729Sdfr		/*
27283366Sjulian		 * We're not guaranteed that any but the first vnode
27383366Sjulian		 * are of our type.  Check for and don't map any
27411626Sbde		 * that aren't.  (We must always map first vp or vclean fails.)
27511626Sbde		 */
27611626Sbde		if (i && (*this_vp_p == NULLVP ||
27711626Sbde		    (*this_vp_p)->v_op != null_vnodeop_p)) {
27811626Sbde			old_vps[i] = NULLVP;
27911626Sbde		} else {
28011626Sbde			old_vps[i] = *this_vp_p;
28111626Sbde			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
28211626Sbde			/*
2832729Sdfr			 * XXX - Several operations have the side effect
28482607Sdillon			 * of vrele'ing their vp's.  We must account for
2852729Sdfr			 * that.  (This should go away in the future.)
28682607Sdillon			 */
28783366Sjulian			if (reles & 1)
28882607Sdillon				VREF(*this_vp_p);
28982607Sdillon		}
29082607Sdillon
29182607Sdillon	}
29282607Sdillon
29382607Sdillon	/*
29482607Sdillon	 * Call the operation on the lower layer
29583366Sjulian	 * with the modified argument structure.
29682607Sdillon	 */
29782607Sdillon	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
29882607Sdillon
2992729Sdfr	/*
3002729Sdfr	 * Maintain the illusion of call-by-value
3012729Sdfr	 * by restoring vnodes in the argument structure
3022729Sdfr	 * to their original value.
3032729Sdfr	 */
3042729Sdfr	reles = descp->vdesc_flags;
3052729Sdfr	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
3062729Sdfr		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
3072729Sdfr			break;   /* bail out at end of list */
3082729Sdfr		if (old_vps[i]) {
3092729Sdfr			*(vps_p[i]) = old_vps[i];
3102729Sdfr			if (reles & 1)
3112729Sdfr				vrele(*(vps_p[i]));
3122729Sdfr		}
3132729Sdfr	}
3142729Sdfr
3152729Sdfr	/*
3162729Sdfr	 * Map the possible out-going vpp
3172729Sdfr	 * (Assumes that the lower layer always returns
3182729Sdfr	 * a VREF'ed vpp unless it gets an error.)
3192729Sdfr	 */
3202729Sdfr	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
3212729Sdfr	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
3222729Sdfr	    !error) {
3232729Sdfr		/*
3242729Sdfr		 * XXX - even though some ops have vpp returned vp's,
32512866Speter		 * several ops actually vrele this before returning.
3262729Sdfr		 * We must avoid these ops.
3272729Sdfr		 * (This should go away when these ops are regularized.)
3282729Sdfr		 */
32912866Speter		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
3302729Sdfr			goto out;
33112866Speter		vppp = VOPARG_OFFSETTO(struct vnode***,
3322729Sdfr				 descp->vdesc_vpp_offset,ap);
33382607Sdillon		error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
33482607Sdillon	}
33582607Sdillon
33612866Speter out:
33783366Sjulian	return (error);
33883366Sjulian}
3392729Sdfr
3402729Sdfr/*
3412729Sdfr * We have to carry on the locking protocol on the null layer vnodes
3422729Sdfr * as we progress through the tree. We also have to enforce read-only
34312866Speter * if this layer is mounted read-only.
34482607Sdillon */
3452729Sdfrstatic int
3462729Sdfrnull_lookup(ap)
3472729Sdfr	struct vop_lookup_args /* {
3482729Sdfr		struct vnode * a_dvp;
3492729Sdfr		struct vnode ** a_vpp;
3502729Sdfr		struct componentname * a_cnp;
35182607Sdillon	} */ *ap;
35283366Sjulian{
35382607Sdillon	struct componentname *cnp = ap->a_cnp;
35482607Sdillon	struct proc *p = cnp->cn_proc;
35582607Sdillon	int flags = cnp->cn_flags;
35668024Srwatson	struct vop_lock_args lockargs;
3572729Sdfr	struct vop_unlock_args unlockargs;
3582729Sdfr	struct vnode *dvp, *vp;
3592729Sdfr	int error;
3602729Sdfr
3612729Sdfr	if ((flags & ISLASTCN) && (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3622729Sdfr	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
3632729Sdfr		return (EROFS);
36482607Sdillon	error = null_bypass((struct vop_generic_args *)ap);
36582607Sdillon	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
3662729Sdfr	    (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3672729Sdfr	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
3682729Sdfr		error = EROFS;
3692729Sdfr	/*
3702729Sdfr	 * We must do the same locking and unlocking at this layer as
3712729Sdfr	 * is done in the layers below us. We could figure this out
3722729Sdfr	 * based on the error return and the LASTCN, LOCKPARENT, and
3732729Sdfr	 * LOCKLEAF flags. However, it is more expidient to just find
37482607Sdillon	 * out the state of the lower level vnodes and set ours to the
37582607Sdillon	 * same state.
3762729Sdfr	 */
3772729Sdfr	dvp = ap->a_dvp;
3782729Sdfr	vp = *ap->a_vpp;
3792729Sdfr	if (dvp == vp)
3802729Sdfr		return (error);
38182607Sdillon	if (!VOP_ISLOCKED(dvp)) {
38282607Sdillon		unlockargs.a_vp = dvp;
3832729Sdfr		unlockargs.a_flags = 0;
3842729Sdfr		unlockargs.a_p = p;
38582607Sdillon		vop_nounlock(&unlockargs);
3862729Sdfr	}
3872729Sdfr	if (vp != NULLVP && VOP_ISLOCKED(vp)) {
3882729Sdfr		lockargs.a_vp = vp;
3892729Sdfr		lockargs.a_flags = LK_SHARED;
3902729Sdfr		lockargs.a_p = p;
3912729Sdfr		vop_nolock(&lockargs);
3922729Sdfr	}
39383366Sjulian	return (error);
39482607Sdillon}
3952729Sdfr
3962729Sdfr/*
3972729Sdfr * Setattr call. Disallow write attempts if the layer is mounted read-only.
3982729Sdfr */
3992729Sdfrint
4002729Sdfrnull_setattr(ap)
4012729Sdfr	struct vop_setattr_args /* {
4022729Sdfr		struct vnodeop_desc *a_desc;
4032729Sdfr		struct vnode *a_vp;
4042729Sdfr		struct vattr *a_vap;
4052729Sdfr		struct ucred *a_cred;
4062729Sdfr		struct proc *a_p;
4072729Sdfr	} */ *ap;
4082729Sdfr{
4092729Sdfr	struct vnode *vp = ap->a_vp;
4102729Sdfr	struct vattr *vap = ap->a_vap;
4112729Sdfr
4122729Sdfr  	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
4132729Sdfr	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
4142729Sdfr	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
4152729Sdfr	    (vp->v_mount->mnt_flag & MNT_RDONLY))
4162729Sdfr		return (EROFS);
4172729Sdfr	if (vap->va_size != VNOVAL) {
4182729Sdfr 		switch (vp->v_type) {
4192729Sdfr 		case VDIR:
4202729Sdfr 			return (EISDIR);
42183366Sjulian 		case VCHR:
42282607Sdillon 		case VBLK:
42382607Sdillon 		case VSOCK:
42482607Sdillon 		case VFIFO:
42543426Sphk			return (0);
42683366Sjulian		case VREG:
42782607Sdillon		case VLNK:
42882607Sdillon 		default:
42943426Sphk			/*
4302729Sdfr			 * Disallow write attempts if the filesystem is
4312729Sdfr			 * mounted read-only.
4322729Sdfr			 */
4332729Sdfr			if (vp->v_mount->mnt_flag & MNT_RDONLY)
4342729Sdfr				return (EROFS);
4352729Sdfr		}
4362729Sdfr	}
4372729Sdfr	return (null_bypass((struct vop_generic_args *)ap));
4382729Sdfr}
4392729Sdfr
4402729Sdfr/*
44182607Sdillon *  We handle getattr only to change the fsid.
44282607Sdillon */
4432729Sdfrstatic int
4442729Sdfrnull_getattr(ap)
4452729Sdfr	struct vop_getattr_args /* {
4462729Sdfr		struct vnode *a_vp;
4472729Sdfr		struct vattr *a_vap;
4482729Sdfr		struct ucred *a_cred;
44934961Sphk		struct proc *a_p;
4502729Sdfr	} */ *ap;
4512729Sdfr{
4522729Sdfr	int error;
45383366Sjulian
4542729Sdfr	if (error = null_bypass((struct vop_generic_args *)ap))
4552729Sdfr		return (error);
4562729Sdfr	/* Requires that arguments be restored. */
45782607Sdillon	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
4582729Sdfr	return (0);
45982607Sdillon}
4602729Sdfr
4612729Sdfrstatic int
4622729Sdfrnull_access(ap)
4632729Sdfr	struct vop_access_args /* {
4642729Sdfr		struct vnode *a_vp;
4652729Sdfr		int  a_mode;
4662729Sdfr		struct ucred *a_cred;
46782607Sdillon		struct proc *a_p;
46882607Sdillon	} */ *ap;
4692729Sdfr{
4702729Sdfr	struct vnode *vp = ap->a_vp;
47182607Sdillon	mode_t mode = ap->a_mode;
47283366Sjulian
47382607Sdillon	/*
47482607Sdillon	 * Disallow write attempts on read-only layers;
47582607Sdillon	 * unless the file is a socket, fifo, or a block or
4762729Sdfr	 * character device resident on the file system.
4772729Sdfr	 */
47812866Speter	if (mode & VWRITE) {
4792729Sdfr		switch (vp->v_type) {
4802729Sdfr		case VDIR:
4812729Sdfr		case VLNK:
4822729Sdfr		case VREG:
48312866Speter			if (vp->v_mount->mnt_flag & MNT_RDONLY)
4842729Sdfr				return (EROFS);
48582607Sdillon			break;
48682607Sdillon		}
48782607Sdillon	}
48812866Speter	return (null_bypass((struct vop_generic_args *)ap));
48983366Sjulian}
49083366Sjulian
4912729Sdfr/*
4922729Sdfr * We need to process our own vnode lock and then clear the
49382607Sdillon * interlock flag as it applies only to our vnode, not the
4942729Sdfr * vnodes below us on the stack.
4952729Sdfr */
49683366Sjulianstatic int
4972836Sdgnull_lock(ap)
4982729Sdfr	struct vop_lock_args /* {
4992729Sdfr		struct vnode *a_vp;
5002729Sdfr		int a_flags;
5012729Sdfr		struct proc *a_p;
5022729Sdfr	} */ *ap;
50382607Sdillon{
50483366Sjulian
50582607Sdillon	vop_nolock(ap);
50682607Sdillon	if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
50782607Sdillon		return (0);
50882607Sdillon	ap->a_flags &= ~LK_INTERLOCK;
5092729Sdfr	return (null_bypass((struct vop_generic_args *)ap));
5102729Sdfr}
5112729Sdfr
5122729Sdfr/*
5132729Sdfr * We need to process our own vnode unlock and then clear the
5142729Sdfr * interlock flag as it applies only to our vnode, not the
5152729Sdfr * vnodes below us on the stack.
5162729Sdfr */
5172729Sdfrstatic int
5182729Sdfrnull_unlock(ap)
5192729Sdfr	struct vop_unlock_args /* {
5202729Sdfr		struct vnode *a_vp;
5212729Sdfr		int a_flags;
5222729Sdfr		struct proc *a_p;
5232729Sdfr	} */ *ap;
52482607Sdillon{
52582607Sdillon	struct vnode *vp = ap->a_vp;
5262729Sdfr
52783366Sjulian	vop_nounlock(ap);
5282729Sdfr	ap->a_flags &= ~LK_INTERLOCK;
5292729Sdfr	return (null_bypass((struct vop_generic_args *)ap));
5302729Sdfr}
5312729Sdfr
53282607Sdillonstatic int
5332729Sdfrnull_inactive(ap)
5342729Sdfr	struct vop_inactive_args /* {
5352729Sdfr		struct vnode *a_vp;
5362729Sdfr		struct proc *a_p;
5372729Sdfr	} */ *ap;
5382729Sdfr{
5392729Sdfr	/*
5402729Sdfr	 * Do nothing (and _don't_ bypass).
5412729Sdfr	 * Wait to vrele lowervp until reclaim,
5422729Sdfr	 * so that until then our null_node is in the
5432729Sdfr	 * cache and reusable.
5442729Sdfr	 *
5452729Sdfr	 * NEEDSWORK: Someday, consider inactive'ing
5462729Sdfr	 * the lowervp and then trying to reactivate it
5472729Sdfr	 * with capabilities (v_id)
5482729Sdfr	 * like they do in the name lookup cache code.
5492729Sdfr	 * That's too much work for now.
5502729Sdfr	 */
5512729Sdfr	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
5522729Sdfr	return (0);
5532729Sdfr}
5542729Sdfr
5552729Sdfrstatic int
5562729Sdfrnull_reclaim(ap)
5572729Sdfr	struct vop_reclaim_args /* {
55882607Sdillon		struct vnode *a_vp;
55982607Sdillon		struct proc *a_p;
5602729Sdfr	} */ *ap;
5612729Sdfr{
5622729Sdfr	struct vnode *vp = ap->a_vp;
5632729Sdfr	struct null_node *xp = VTONULL(vp);
5642729Sdfr	struct vnode *lowervp = xp->null_lowervp;
5652729Sdfr
5662729Sdfr	/*
5672729Sdfr	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
5682729Sdfr	 * so we can't call VOPs on ourself.
5692729Sdfr	 */
5702729Sdfr	/* After this assignment, this node will not be re-used. */
5712729Sdfr	xp->null_lowervp = NULLVP;
5722729Sdfr	LIST_REMOVE(xp, null_hash);
5732729Sdfr	FREE(vp->v_data, M_TEMP);
5742729Sdfr	vp->v_data = NULL;
5752729Sdfr	vrele (lowervp);
5762729Sdfr	return (0);
5772729Sdfr}
5782729Sdfr
5792729Sdfrstatic int
5802729Sdfrnull_print(ap)
58134961Sphk	struct vop_print_args /* {
5822729Sdfr		struct vnode *a_vp;
5832729Sdfr	} */ *ap;
5842729Sdfr{
5852729Sdfr	register struct vnode *vp = ap->a_vp;
58682607Sdillon	printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
58782607Sdillon	return (0);
5882729Sdfr}
5892729Sdfr
5902729Sdfr/*
5912729Sdfr * XXX - vop_strategy must be hand coded because it has no
59283366Sjulian * vnode in its arguments.
59382607Sdillon * This goes away with a merged VM/buffer cache.
59482607Sdillon */
59582607Sdillonstatic int
5962729Sdfrnull_strategy(ap)
5972729Sdfr	struct vop_strategy_args /* {
59812866Speter		struct buf *a_bp;
5992729Sdfr	} */ *ap;
6002729Sdfr{
60112866Speter	struct buf *bp = ap->a_bp;
6022729Sdfr	int error;
6032729Sdfr	struct vnode *savedvp;
6042729Sdfr
60512866Speter	savedvp = bp->b_vp;
6062729Sdfr	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
60782607Sdillon
60882607Sdillon	error = VOP_STRATEGY(bp);
60982607Sdillon
61012866Speter	bp->b_vp = savedvp;
61183366Sjulian
61283366Sjulian	return (error);
6132729Sdfr}
6142729Sdfr
6152729Sdfr/*
61612866Speter * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
6172729Sdfr * vnode in its arguments.
6182729Sdfr * This goes away with a merged VM/buffer cache.
61982607Sdillon */
6202729Sdfrstatic int
6212729Sdfrnull_bwrite(ap)
6222729Sdfr	struct vop_bwrite_args /* {
6232729Sdfr		struct buf *a_bp;
6242729Sdfr	} */ *ap;
6252729Sdfr{
6262729Sdfr	struct buf *bp = ap->a_bp;
6272729Sdfr	int error;
62882607Sdillon	struct vnode *savedvp;
62983366Sjulian
63082607Sdillon	savedvp = bp->b_vp;
63182607Sdillon	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
63282607Sdillon
63368024Srwatson	error = VOP_BWRITE(bp);
6342729Sdfr
6352729Sdfr	bp->b_vp = savedvp;
6362729Sdfr
6372729Sdfr	return (error);
6382729Sdfr}
6392729Sdfr
6402729Sdfr/*
64182607Sdillon * Global vfs data structures
64282607Sdillon */
6432729Sdfrvop_t **null_vnodeop_p;
6442729Sdfrstatic struct vnodeopv_entry_desc null_vnodeop_entries[] = {
6452729Sdfr	{ &vop_default_desc, (vop_t *)null_bypass },
6462729Sdfr
6472729Sdfr	{ &vop_lookup_desc, (vop_t *)null_lookup },
6482729Sdfr	{ &vop_setattr_desc, (vop_t *)null_setattr },
6492729Sdfr	{ &vop_getattr_desc, (vop_t *)null_getattr },
65082607Sdillon	{ &vop_access_desc, (vop_t *)null_access },
65182607Sdillon	{ &vop_lock_desc, (vop_t *)null_lock },
6522729Sdfr	{ &vop_unlock_desc, (vop_t *)null_unlock },
6532729Sdfr	{ &vop_inactive_desc, (vop_t *)null_inactive },
6542729Sdfr	{ &vop_reclaim_desc, (vop_t *)null_reclaim },
6552729Sdfr	{ &vop_print_desc, (vop_t *)null_print },
6562729Sdfr
65782607Sdillon	{ &vop_strategy_desc, (vop_t *)null_strategy },
65882607Sdillon	{ &vop_bwrite_desc, (vop_t *)null_bwrite },
6592729Sdfr
6602729Sdfr	{ NULL, NULL }
66183366Sjulian};
6622729Sdfrstatic struct vnodeopv_desc null_vnodeop_opv_desc =
6632729Sdfr	{ &null_vnodeop_p, null_vnodeop_entries };
6642729Sdfr
66582607SdillonVNODEOP_SET(null_vnodeop_opv_desc);
6662729Sdfr