null_vnops.c revision 22607
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1992, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * John Heidemann of the UCLA Ficus project.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 3. All advertising materials mentioning features or use of this software
171541Srgrimes *    must display the following acknowledgement:
181541Srgrimes *	This product includes software developed by the University of
191541Srgrimes *	California, Berkeley and its contributors.
201541Srgrimes * 4. Neither the name of the University nor the names of its contributors
211541Srgrimes *    may be used to endorse or promote products derived from this software
221541Srgrimes *    without specific prior written permission.
231541Srgrimes *
241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341541Srgrimes * SUCH DAMAGE.
351541Srgrimes *
3622521Sdyson *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
371541Srgrimes *
3822521Sdyson * Ancestors:
3922521Sdyson *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
4022607Smpp *	$Id: null_vnops.c,v 1.14 1997/02/12 14:55:01 mpp Exp $
4122521Sdyson *	...and...
4222521Sdyson *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
4322521Sdyson *
4421673Sjkh * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 22607 1997-02-12 18:06:08Z mpp $
451541Srgrimes */
461541Srgrimes
471541Srgrimes/*
481541Srgrimes * Null Layer
491541Srgrimes *
501541Srgrimes * (See mount_null(8) for more information.)
511541Srgrimes *
521541Srgrimes * The null layer duplicates a portion of the file system
531541Srgrimes * name space under a new name.  In this respect, it is
541541Srgrimes * similar to the loopback file system.  It differs from
551541Srgrimes * the loopback fs in two respects:  it is implemented using
561541Srgrimes * a stackable layers techniques, and it's "null-node"s stack above
571541Srgrimes * all lower-layer vnodes, not just over directory vnodes.
581541Srgrimes *
591541Srgrimes * The null layer has two purposes.  First, it serves as a demonstration
601541Srgrimes * of layering by proving a layer which does nothing.  (It actually
611541Srgrimes * does everything the loopback file system does, which is slightly
621541Srgrimes * more than nothing.)  Second, the null layer can serve as a prototype
631541Srgrimes * layer.  Since it provides all necessary layer framework,
641541Srgrimes * new file system layers can be created very easily be starting
651541Srgrimes * with a null layer.
661541Srgrimes *
671541Srgrimes * The remainder of this man page examines the null layer as a basis
681541Srgrimes * for constructing new layers.
691541Srgrimes *
701541Srgrimes *
711541Srgrimes * INSTANTIATING NEW NULL LAYERS
721541Srgrimes *
731541Srgrimes * New null layers are created with mount_null(8).
741541Srgrimes * Mount_null(8) takes two arguments, the pathname
751541Srgrimes * of the lower vfs (target-pn) and the pathname where the null
761541Srgrimes * layer will appear in the namespace (alias-pn).  After
771541Srgrimes * the null layer is put into place, the contents
781541Srgrimes * of target-pn subtree will be aliased under alias-pn.
791541Srgrimes *
801541Srgrimes *
811541Srgrimes * OPERATION OF A NULL LAYER
821541Srgrimes *
831541Srgrimes * The null layer is the minimum file system layer,
841541Srgrimes * simply bypassing all possible operations to the lower layer
851541Srgrimes * for processing there.  The majority of its activity centers
861541Srgrimes * on the bypass routine, though which nearly all vnode operations
871541Srgrimes * pass.
881541Srgrimes *
891541Srgrimes * The bypass routine accepts arbitrary vnode operations for
901541Srgrimes * handling by the lower layer.  It begins by examing vnode
911541Srgrimes * operation arguments and replacing any null-nodes by their
921541Srgrimes * lower-layer equivlants.  It then invokes the operation
931541Srgrimes * on the lower layer.  Finally, it replaces the null-nodes
941541Srgrimes * in the arguments and, if a vnode is return by the operation,
951541Srgrimes * stacks a null-node on top of the returned vnode.
961541Srgrimes *
9722521Sdyson * Although bypass handles most operations, vop_getattr, vop_lock,
9822521Sdyson * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
9922521Sdyson * bypassed. Vop_getattr must change the fsid being returned.
10022521Sdyson * Vop_lock and vop_unlock must handle any locking for the
10122521Sdyson * current vnode as well as pass the lock request down.
1021541Srgrimes * Vop_inactive and vop_reclaim are not bypassed so that
10322521Sdyson * they can handle freeing null-layer specific data. Vop_print
10422521Sdyson * is not bypassed to avoid excessive debugging information.
10522521Sdyson * Also, certain vnode operations change the locking state within
10622521Sdyson * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
10722521Sdyson * and symlink). Ideally these operations should not change the
10822521Sdyson * lock state, but should be changed to let the caller of the
10922521Sdyson * function unlock them. Otherwise all intermediate vnode layers
11022521Sdyson * (such as union, umapfs, etc) must catch these functions to do
11122521Sdyson * the necessary locking at their layer.
1121541Srgrimes *
1131541Srgrimes *
1141541Srgrimes * INSTANTIATING VNODE STACKS
1151541Srgrimes *
1161541Srgrimes * Mounting associates the null layer with a lower layer,
1171541Srgrimes * effect stacking two VFSes.  Vnode stacks are instead
1181541Srgrimes * created on demand as files are accessed.
1191541Srgrimes *
1201541Srgrimes * The initial mount creates a single vnode stack for the
1211541Srgrimes * root of the new null layer.  All other vnode stacks
1221541Srgrimes * are created as a result of vnode operations on
1231541Srgrimes * this or other null vnode stacks.
1241541Srgrimes *
1251541Srgrimes * New vnode stacks come into existance as a result of
1268876Srgrimes * an operation which returns a vnode.
1271541Srgrimes * The bypass routine stacks a null-node above the new
1281541Srgrimes * vnode before returning it to the caller.
1291541Srgrimes *
1301541Srgrimes * For example, imagine mounting a null layer with
1311541Srgrimes * "mount_null /usr/include /dev/layer/null".
1321541Srgrimes * Changing directory to /dev/layer/null will assign
1331541Srgrimes * the root null-node (which was created when the null layer was mounted).
1341541Srgrimes * Now consider opening "sys".  A vop_lookup would be
1351541Srgrimes * done on the root null-node.  This operation would bypass through
1368876Srgrimes * to the lower layer which would return a vnode representing
1371541Srgrimes * the UFS "sys".  Null_bypass then builds a null-node
1381541Srgrimes * aliasing the UFS "sys" and returns this to the caller.
1391541Srgrimes * Later operations on the null-node "sys" will repeat this
1401541Srgrimes * process when constructing other vnode stacks.
1411541Srgrimes *
1421541Srgrimes *
1431541Srgrimes * CREATING OTHER FILE SYSTEM LAYERS
1441541Srgrimes *
1451541Srgrimes * One of the easiest ways to construct new file system layers is to make
1461541Srgrimes * a copy of the null layer, rename all files and variables, and
1471541Srgrimes * then begin modifing the copy.  Sed can be used to easily rename
1481541Srgrimes * all variables.
1491541Srgrimes *
1508876Srgrimes * The umap layer is an example of a layer descended from the
1511541Srgrimes * null layer.
1521541Srgrimes *
1531541Srgrimes *
1541541Srgrimes * INVOKING OPERATIONS ON LOWER LAYERS
1551541Srgrimes *
1568876Srgrimes * There are two techniques to invoke operations on a lower layer
1571541Srgrimes * when the operation cannot be completely bypassed.  Each method
1581541Srgrimes * is appropriate in different situations.  In both cases,
1591541Srgrimes * it is the responsibility of the aliasing layer to make
1601541Srgrimes * the operation arguments "correct" for the lower layer
1611541Srgrimes * by mapping an vnode arguments to the lower layer.
1621541Srgrimes *
1631541Srgrimes * The first approach is to call the aliasing layer's bypass routine.
1641541Srgrimes * This method is most suitable when you wish to invoke the operation
1651541Srgrimes * currently being hanldled on the lower layer.  It has the advantage
1661541Srgrimes * that the bypass routine already must do argument mapping.
1671541Srgrimes * An example of this is null_getattrs in the null layer.
1681541Srgrimes *
1691541Srgrimes * A second approach is to directly invoked vnode operations on
1701541Srgrimes * the lower layer with the VOP_OPERATIONNAME interface.
1711541Srgrimes * The advantage of this method is that it is easy to invoke
1721541Srgrimes * arbitrary operations on the lower layer.  The disadvantage
1731541Srgrimes * is that vnodes arguments must be manualy mapped.
1741541Srgrimes *
1751541Srgrimes */
1761541Srgrimes
1771541Srgrimes#include <sys/param.h>
1781541Srgrimes#include <sys/systm.h>
1792960Swollman#include <sys/kernel.h>
18012769Sphk#include <sys/sysctl.h>
1811541Srgrimes#include <sys/proc.h>
1821541Srgrimes#include <sys/time.h>
1831541Srgrimes#include <sys/types.h>
1841541Srgrimes#include <sys/vnode.h>
1851541Srgrimes#include <sys/mount.h>
1861541Srgrimes#include <sys/namei.h>
1871541Srgrimes#include <sys/malloc.h>
1881541Srgrimes#include <sys/buf.h>
1891541Srgrimes#include <miscfs/nullfs/null.h>
1901541Srgrimes
19112769Sphkstatic int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
19212769SphkSYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
19312769Sphk	&null_bug_bypass, 0, "");
1941541Srgrimes
19522597Smppstatic int	null_access __P((struct vop_access_args *ap));
19622521Sdysonint		null_bypass __P((struct vop_generic_args *ap));
19712769Sphkstatic int	null_bwrite __P((struct vop_bwrite_args *ap));
19812769Sphkstatic int	null_getattr __P((struct vop_getattr_args *ap));
19912769Sphkstatic int	null_inactive __P((struct vop_inactive_args *ap));
20022597Smppstatic int	null_lock __P((struct vop_lock_args *ap));
20122597Smppstatic int	null_lookup __P((struct vop_lookup_args *ap));
20212769Sphkstatic int	null_print __P((struct vop_print_args *ap));
20312769Sphkstatic int	null_reclaim __P((struct vop_reclaim_args *ap));
20422597Smppstatic int	null_setattr __P((struct vop_setattr_args *ap));
20512769Sphkstatic int	null_strategy __P((struct vop_strategy_args *ap));
20622597Smppstatic int	null_unlock __P((struct vop_unlock_args *ap));
20712595Sbde
2081541Srgrimes/*
2091541Srgrimes * This is the 10-Apr-92 bypass routine.
2101541Srgrimes *    This version has been optimized for speed, throwing away some
2111541Srgrimes * safety checks.  It should still always work, but it's not as
2121541Srgrimes * robust to programmer errors.
2131541Srgrimes *    Define SAFETY to include some error checking code.
2141541Srgrimes *
2151541Srgrimes * In general, we map all vnodes going down and unmap them on the way back.
2161541Srgrimes * As an exception to this, vnodes can be marked "unmapped" by setting
2171541Srgrimes * the Nth bit in operation's vdesc_flags.
2181541Srgrimes *
2191541Srgrimes * Also, some BSD vnode operations have the side effect of vrele'ing
2201541Srgrimes * their arguments.  With stacking, the reference counts are held
2211541Srgrimes * by the upper node, not the lower one, so we must handle these
2221541Srgrimes * side-effects here.  This is not of concern in Sun-derived systems
2231541Srgrimes * since there are no such side-effects.
2241541Srgrimes *
2251541Srgrimes * This makes the following assumptions:
2261541Srgrimes * - only one returned vpp
2271541Srgrimes * - no INOUT vpp's (Sun's vop_open has one of these)
2281541Srgrimes * - the vnode operation vector of the first vnode should be used
2291541Srgrimes *   to determine what implementation of the op should be invoked
2301541Srgrimes * - all mapped vnodes are of our vnode-type (NEEDSWORK:
2311541Srgrimes *   problems on rmdir'ing mount points and renaming?)
2328876Srgrimes */
23322521Sdysonint
2341541Srgrimesnull_bypass(ap)
2351541Srgrimes	struct vop_generic_args /* {
2361541Srgrimes		struct vnodeop_desc *a_desc;
2371541Srgrimes		<other random data follows, presumably>
2381541Srgrimes	} */ *ap;
2391541Srgrimes{
2401541Srgrimes	register struct vnode **this_vp_p;
2411541Srgrimes	int error;
2421541Srgrimes	struct vnode *old_vps[VDESC_MAX_VPS];
2431541Srgrimes	struct vnode **vps_p[VDESC_MAX_VPS];
2441541Srgrimes	struct vnode ***vppp;
2451541Srgrimes	struct vnodeop_desc *descp = ap->a_desc;
2461541Srgrimes	int reles, i;
2471541Srgrimes
2481541Srgrimes	if (null_bug_bypass)
2491541Srgrimes		printf ("null_bypass: %s\n", descp->vdesc_name);
2501541Srgrimes
2511541Srgrimes#ifdef SAFETY
2521541Srgrimes	/*
2531541Srgrimes	 * We require at least one vp.
2541541Srgrimes	 */
2551541Srgrimes	if (descp->vdesc_vp_offsets == NULL ||
2561541Srgrimes	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
2577170Sdg		panic ("null_bypass: no vp's in map.");
2581541Srgrimes#endif
2591541Srgrimes
2601541Srgrimes	/*
2611541Srgrimes	 * Map the vnodes going in.
2621541Srgrimes	 * Later, we'll invoke the operation based on
2631541Srgrimes	 * the first mapped vnode's operation vector.
2641541Srgrimes	 */
2651541Srgrimes	reles = descp->vdesc_flags;
2661541Srgrimes	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
2671541Srgrimes		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
2681541Srgrimes			break;   /* bail out at end of list */
2698876Srgrimes		vps_p[i] = this_vp_p =
2701541Srgrimes			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
2711541Srgrimes		/*
2721541Srgrimes		 * We're not guaranteed that any but the first vnode
2731541Srgrimes		 * are of our type.  Check for and don't map any
2741541Srgrimes		 * that aren't.  (We must always map first vp or vclean fails.)
2751541Srgrimes		 */
27622521Sdyson		if (i && (*this_vp_p == NULL ||
27722521Sdyson		    (*this_vp_p)->v_op != null_vnodeop_p)) {
2781541Srgrimes			old_vps[i] = NULL;
2791541Srgrimes		} else {
2801541Srgrimes			old_vps[i] = *this_vp_p;
2811541Srgrimes			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
2821541Srgrimes			/*
2831541Srgrimes			 * XXX - Several operations have the side effect
2841541Srgrimes			 * of vrele'ing their vp's.  We must account for
2851541Srgrimes			 * that.  (This should go away in the future.)
2861541Srgrimes			 */
2871541Srgrimes			if (reles & 1)
2881541Srgrimes				VREF(*this_vp_p);
2891541Srgrimes		}
2908876Srgrimes
2911541Srgrimes	}
2921541Srgrimes
2931541Srgrimes	/*
2941541Srgrimes	 * Call the operation on the lower layer
2951541Srgrimes	 * with the modified argument structure.
2961541Srgrimes	 */
2971541Srgrimes	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
2981541Srgrimes
2991541Srgrimes	/*
3001541Srgrimes	 * Maintain the illusion of call-by-value
3011541Srgrimes	 * by restoring vnodes in the argument structure
3021541Srgrimes	 * to their original value.
3031541Srgrimes	 */
3041541Srgrimes	reles = descp->vdesc_flags;
3051541Srgrimes	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
3061541Srgrimes		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
3071541Srgrimes			break;   /* bail out at end of list */
3081541Srgrimes		if (old_vps[i]) {
3091541Srgrimes			*(vps_p[i]) = old_vps[i];
3101541Srgrimes			if (reles & 1)
3111541Srgrimes				vrele(*(vps_p[i]));
3121541Srgrimes		}
3131541Srgrimes	}
3141541Srgrimes
3151541Srgrimes	/*
3161541Srgrimes	 * Map the possible out-going vpp
3171541Srgrimes	 * (Assumes that the lower layer always returns
3181541Srgrimes	 * a VREF'ed vpp unless it gets an error.)
3191541Srgrimes	 */
3201541Srgrimes	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
3211541Srgrimes	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
3221541Srgrimes	    !error) {
3231541Srgrimes		/*
3241541Srgrimes		 * XXX - even though some ops have vpp returned vp's,
3251541Srgrimes		 * several ops actually vrele this before returning.
3261541Srgrimes		 * We must avoid these ops.
3271541Srgrimes		 * (This should go away when these ops are regularized.)
3281541Srgrimes		 */
3291541Srgrimes		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
3301541Srgrimes			goto out;
3311541Srgrimes		vppp = VOPARG_OFFSETTO(struct vnode***,
3321541Srgrimes				 descp->vdesc_vpp_offset,ap);
3331541Srgrimes		error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
3341541Srgrimes	}
3351541Srgrimes
3361541Srgrimes out:
3371541Srgrimes	return (error);
3381541Srgrimes}
3391541Srgrimes
34022521Sdyson/*
34122521Sdyson * We have to carry on the locking protocol on the null layer vnodes
34222521Sdyson * as we progress through the tree. We also have to enforce read-only
34322521Sdyson * if this layer is mounted read-only.
34422521Sdyson */
34522521Sdysonstatic int
34622521Sdysonnull_lookup(ap)
34722521Sdyson	struct vop_lookup_args /* {
34822521Sdyson		struct vnode * a_dvp;
34922521Sdyson		struct vnode ** a_vpp;
35022521Sdyson		struct componentname * a_cnp;
35122521Sdyson	} */ *ap;
35222521Sdyson{
35322521Sdyson	struct componentname *cnp = ap->a_cnp;
35422521Sdyson	struct proc *p = cnp->cn_proc;
35522521Sdyson	int flags = cnp->cn_flags;
35622521Sdyson	struct vop_lock_args lockargs;
35722521Sdyson	struct vop_unlock_args unlockargs;
35822521Sdyson	struct vnode *dvp, *vp;
35922521Sdyson	int error;
3601541Srgrimes
36122521Sdyson	if ((flags & ISLASTCN) && (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
36222521Sdyson	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
36322521Sdyson		return (EROFS);
36422607Smpp	error = null_bypass((struct vop_generic_args *)ap);
36522521Sdyson	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
36622521Sdyson	    (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
36722521Sdyson	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
36822521Sdyson		error = EROFS;
36922521Sdyson	/*
37022521Sdyson	 * We must do the same locking and unlocking at this layer as
37122521Sdyson	 * is done in the layers below us. We could figure this out
37222521Sdyson	 * based on the error return and the LASTCN, LOCKPARENT, and
37322521Sdyson	 * LOCKLEAF flags. However, it is more expidient to just find
37422521Sdyson	 * out the state of the lower level vnodes and set ours to the
37522521Sdyson	 * same state.
37622521Sdyson	 */
37722521Sdyson	dvp = ap->a_dvp;
37822521Sdyson	vp = *ap->a_vpp;
37922521Sdyson	if (dvp == vp)
38022521Sdyson		return (error);
38122521Sdyson	if (!VOP_ISLOCKED(dvp)) {
38222521Sdyson		unlockargs.a_vp = dvp;
38322521Sdyson		unlockargs.a_flags = 0;
38422521Sdyson		unlockargs.a_p = p;
38522521Sdyson		vop_nounlock(&unlockargs);
38622521Sdyson	}
38722521Sdyson	if (vp != NULL && VOP_ISLOCKED(vp)) {
38822521Sdyson		lockargs.a_vp = vp;
38922521Sdyson		lockargs.a_flags = LK_SHARED;
39022521Sdyson		lockargs.a_p = p;
39122521Sdyson		vop_nolock(&lockargs);
39222521Sdyson	}
39322521Sdyson	return (error);
39422521Sdyson}
39522521Sdyson
3961541Srgrimes/*
39722521Sdyson * Setattr call. Disallow write attempts if the layer is mounted read-only.
39822521Sdyson */
39922521Sdysonint
40022521Sdysonnull_setattr(ap)
40122521Sdyson	struct vop_setattr_args /* {
40222521Sdyson		struct vnodeop_desc *a_desc;
40322521Sdyson		struct vnode *a_vp;
40422521Sdyson		struct vattr *a_vap;
40522521Sdyson		struct ucred *a_cred;
40622521Sdyson		struct proc *a_p;
40722521Sdyson	} */ *ap;
40822521Sdyson{
40922521Sdyson	struct vnode *vp = ap->a_vp;
41022521Sdyson	struct vattr *vap = ap->a_vap;
41122521Sdyson
41222521Sdyson  	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
41322597Smpp	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
41422597Smpp	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
41522521Sdyson	    (vp->v_mount->mnt_flag & MNT_RDONLY))
41622521Sdyson		return (EROFS);
41722521Sdyson	if (vap->va_size != VNOVAL) {
41822521Sdyson 		switch (vp->v_type) {
41922521Sdyson 		case VDIR:
42022521Sdyson 			return (EISDIR);
42122521Sdyson 		case VCHR:
42222521Sdyson 		case VBLK:
42322521Sdyson 		case VSOCK:
42422521Sdyson 		case VFIFO:
42522521Sdyson			return (0);
42622521Sdyson		case VREG:
42722521Sdyson		case VLNK:
42822521Sdyson 		default:
42922521Sdyson			/*
43022521Sdyson			 * Disallow write attempts if the filesystem is
43122521Sdyson			 * mounted read-only.
43222521Sdyson			 */
43322521Sdyson			if (vp->v_mount->mnt_flag & MNT_RDONLY)
43422521Sdyson				return (EROFS);
43522521Sdyson		}
43622521Sdyson	}
43722607Smpp	return (null_bypass((struct vop_generic_args *)ap));
43822521Sdyson}
43922521Sdyson
44022521Sdyson/*
4411541Srgrimes *  We handle getattr only to change the fsid.
4421541Srgrimes */
44312769Sphkstatic int
4441541Srgrimesnull_getattr(ap)
4451541Srgrimes	struct vop_getattr_args /* {
4461541Srgrimes		struct vnode *a_vp;
4471541Srgrimes		struct vattr *a_vap;
4481541Srgrimes		struct ucred *a_cred;
4491541Srgrimes		struct proc *a_p;
4501541Srgrimes	} */ *ap;
4511541Srgrimes{
4521541Srgrimes	int error;
45322521Sdyson
45422607Smpp	if (error = null_bypass((struct vop_generic_args *)ap))
4551541Srgrimes		return (error);
4561541Srgrimes	/* Requires that arguments be restored. */
4571541Srgrimes	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
4581541Srgrimes	return (0);
4591541Srgrimes}
4601541Srgrimes
46122521Sdysonstatic int
46222521Sdysonnull_access(ap)
46322521Sdyson	struct vop_access_args /* {
46422521Sdyson		struct vnode *a_vp;
46522521Sdyson		int  a_mode;
46622521Sdyson		struct ucred *a_cred;
46722521Sdyson		struct proc *a_p;
46822521Sdyson	} */ *ap;
46922521Sdyson{
47022521Sdyson	struct vnode *vp = ap->a_vp;
47122521Sdyson	mode_t mode = ap->a_mode;
4721541Srgrimes
47322521Sdyson	/*
47422521Sdyson	 * Disallow write attempts on read-only layers;
47522521Sdyson	 * unless the file is a socket, fifo, or a block or
47622521Sdyson	 * character device resident on the file system.
47722521Sdyson	 */
47822521Sdyson	if (mode & VWRITE) {
47922521Sdyson		switch (vp->v_type) {
48022521Sdyson		case VDIR:
48122521Sdyson		case VLNK:
48222521Sdyson		case VREG:
48322521Sdyson			if (vp->v_mount->mnt_flag & MNT_RDONLY)
48422521Sdyson				return (EROFS);
48522521Sdyson			break;
48622521Sdyson		}
48722521Sdyson	}
48822607Smpp	return (null_bypass((struct vop_generic_args *)ap));
48922521Sdyson}
49022521Sdyson
49122521Sdyson/*
49222521Sdyson * We need to process our own vnode lock and then clear the
49322521Sdyson * interlock flag as it applies only to our vnode, not the
49422521Sdyson * vnodes below us on the stack.
49522521Sdyson */
49622597Smppstatic int
49722521Sdysonnull_lock(ap)
49822521Sdyson	struct vop_lock_args /* {
49922521Sdyson		struct vnode *a_vp;
50022521Sdyson		int a_flags;
50122521Sdyson		struct proc *a_p;
50222521Sdyson	} */ *ap;
50322521Sdyson{
50422521Sdyson
50522521Sdyson	vop_nolock(ap);
50622521Sdyson	if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
50722521Sdyson		return (0);
50822521Sdyson	ap->a_flags &= ~LK_INTERLOCK;
50922607Smpp	return (null_bypass((struct vop_generic_args *)ap));
51022521Sdyson}
51122521Sdyson
51222521Sdyson/*
51322521Sdyson * We need to process our own vnode unlock and then clear the
51422521Sdyson * interlock flag as it applies only to our vnode, not the
51522521Sdyson * vnodes below us on the stack.
51622521Sdyson */
51722597Smppstatic int
51822521Sdysonnull_unlock(ap)
51922521Sdyson	struct vop_unlock_args /* {
52022521Sdyson		struct vnode *a_vp;
52122521Sdyson		int a_flags;
52222521Sdyson		struct proc *a_p;
52322521Sdyson	} */ *ap;
52422521Sdyson{
52522521Sdyson	struct vnode *vp = ap->a_vp;
52622521Sdyson
52722521Sdyson	vop_nounlock(ap);
52822521Sdyson	ap->a_flags &= ~LK_INTERLOCK;
52922607Smpp	return (null_bypass((struct vop_generic_args *)ap));
53022521Sdyson}
53122521Sdyson
53222597Smppstatic int
5331541Srgrimesnull_inactive(ap)
5341541Srgrimes	struct vop_inactive_args /* {
5351541Srgrimes		struct vnode *a_vp;
53622521Sdyson		struct proc *a_p;
5371541Srgrimes	} */ *ap;
5381541Srgrimes{
5391541Srgrimes	/*
5401541Srgrimes	 * Do nothing (and _don't_ bypass).
5411541Srgrimes	 * Wait to vrele lowervp until reclaim,
5421541Srgrimes	 * so that until then our null_node is in the
5431541Srgrimes	 * cache and reusable.
5441541Srgrimes	 *
5451541Srgrimes	 * NEEDSWORK: Someday, consider inactive'ing
5461541Srgrimes	 * the lowervp and then trying to reactivate it
5471541Srgrimes	 * with capabilities (v_id)
5481541Srgrimes	 * like they do in the name lookup cache code.
5491541Srgrimes	 * That's too much work for now.
5501541Srgrimes	 */
55122521Sdyson	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
5521541Srgrimes	return (0);
5531541Srgrimes}
5541541Srgrimes
55512769Sphkstatic int
5561541Srgrimesnull_reclaim(ap)
5571541Srgrimes	struct vop_reclaim_args /* {
5581541Srgrimes		struct vnode *a_vp;
55922521Sdyson		struct proc *a_p;
5601541Srgrimes	} */ *ap;
5611541Srgrimes{
5621541Srgrimes	struct vnode *vp = ap->a_vp;
5631541Srgrimes	struct null_node *xp = VTONULL(vp);
5641541Srgrimes	struct vnode *lowervp = xp->null_lowervp;
5651541Srgrimes
5661541Srgrimes	/*
5671541Srgrimes	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
5681541Srgrimes	 * so we can't call VOPs on ourself.
5691541Srgrimes	 */
5701541Srgrimes	/* After this assignment, this node will not be re-used. */
5711541Srgrimes	xp->null_lowervp = NULL;
57222521Sdyson	LIST_REMOVE(xp, null_hash);
5731541Srgrimes	FREE(vp->v_data, M_TEMP);
5741541Srgrimes	vp->v_data = NULL;
5751541Srgrimes	vrele (lowervp);
5761541Srgrimes	return (0);
5771541Srgrimes}
5781541Srgrimes
57912769Sphkstatic int
5801541Srgrimesnull_print(ap)
5811541Srgrimes	struct vop_print_args /* {
5821541Srgrimes		struct vnode *a_vp;
5831541Srgrimes	} */ *ap;
5841541Srgrimes{
5851541Srgrimes	register struct vnode *vp = ap->a_vp;
5863496Sphk	printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
5871541Srgrimes	return (0);
5881541Srgrimes}
5891541Srgrimes
5901541Srgrimes/*
5911541Srgrimes * XXX - vop_strategy must be hand coded because it has no
5921541Srgrimes * vnode in its arguments.
5931541Srgrimes * This goes away with a merged VM/buffer cache.
5941541Srgrimes */
59512769Sphkstatic int
5961541Srgrimesnull_strategy(ap)
5971541Srgrimes	struct vop_strategy_args /* {
5981541Srgrimes		struct buf *a_bp;
5991541Srgrimes	} */ *ap;
6001541Srgrimes{
6011541Srgrimes	struct buf *bp = ap->a_bp;
6021541Srgrimes	int error;
6031541Srgrimes	struct vnode *savedvp;
6041541Srgrimes
6051541Srgrimes	savedvp = bp->b_vp;
6061541Srgrimes	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
6071541Srgrimes
6081541Srgrimes	error = VOP_STRATEGY(bp);
6091541Srgrimes
6101541Srgrimes	bp->b_vp = savedvp;
6111541Srgrimes
6121541Srgrimes	return (error);
6131541Srgrimes}
6141541Srgrimes
6151541Srgrimes/*
6161541Srgrimes * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
6171541Srgrimes * vnode in its arguments.
6181541Srgrimes * This goes away with a merged VM/buffer cache.
6191541Srgrimes */
62012769Sphkstatic int
6211541Srgrimesnull_bwrite(ap)
6221541Srgrimes	struct vop_bwrite_args /* {
6231541Srgrimes		struct buf *a_bp;
6241541Srgrimes	} */ *ap;
6251541Srgrimes{
6261541Srgrimes	struct buf *bp = ap->a_bp;
6271541Srgrimes	int error;
6281541Srgrimes	struct vnode *savedvp;
6291541Srgrimes
6301541Srgrimes	savedvp = bp->b_vp;
6311541Srgrimes	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
6321541Srgrimes
6331541Srgrimes	error = VOP_BWRITE(bp);
6341541Srgrimes
6351541Srgrimes	bp->b_vp = savedvp;
6361541Srgrimes
6371541Srgrimes	return (error);
6381541Srgrimes}
6391541Srgrimes
6401541Srgrimes/*
6411541Srgrimes * Global vfs data structures
6421541Srgrimes */
64312158Sbdevop_t **null_vnodeop_p;
64412769Sphkstatic struct vnodeopv_entry_desc null_vnodeop_entries[] = {
64512158Sbde	{ &vop_default_desc, (vop_t *)null_bypass },
6461541Srgrimes
64722521Sdyson	{ &vop_lookup_desc, (vop_t *)null_lookup },
64822521Sdyson	{ &vop_setattr_desc, (vop_t *)null_setattr },
64912158Sbde	{ &vop_getattr_desc, (vop_t *)null_getattr },
65022521Sdyson	{ &vop_access_desc, (vop_t *)null_access },
65122521Sdyson	{ &vop_lock_desc, (vop_t *)null_lock },
65222521Sdyson	{ &vop_unlock_desc, (vop_t *)null_unlock },
65312158Sbde	{ &vop_inactive_desc, (vop_t *)null_inactive },
65412158Sbde	{ &vop_reclaim_desc, (vop_t *)null_reclaim },
65512158Sbde	{ &vop_print_desc, (vop_t *)null_print },
6561541Srgrimes
65712158Sbde	{ &vop_strategy_desc, (vop_t *)null_strategy },
65812158Sbde	{ &vop_bwrite_desc, (vop_t *)null_bwrite },
6591541Srgrimes
66012158Sbde	{ NULL, NULL }
6611541Srgrimes};
66212769Sphkstatic struct vnodeopv_desc null_vnodeop_opv_desc =
6631541Srgrimes	{ &null_vnodeop_p, null_vnodeop_entries };
6642946Swollman
6652946SwollmanVNODEOP_SET(null_vnodeop_opv_desc);
666