null_vnops.c revision 65467
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
4022521Sdyson *	...and...
4122521Sdyson *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
4222521Sdyson *
4350477Speter * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 65467 2000-09-05 09:02:07Z bp $
441541Srgrimes */
451541Srgrimes
461541Srgrimes/*
471541Srgrimes * Null Layer
481541Srgrimes *
491541Srgrimes * (See mount_null(8) for more information.)
501541Srgrimes *
511541Srgrimes * The null layer duplicates a portion of the file system
521541Srgrimes * name space under a new name.  In this respect, it is
531541Srgrimes * similar to the loopback file system.  It differs from
541541Srgrimes * the loopback fs in two respects:  it is implemented using
5535256Sdes * a stackable layers techniques, and its "null-node"s stack above
561541Srgrimes * all lower-layer vnodes, not just over directory vnodes.
571541Srgrimes *
581541Srgrimes * The null layer has two purposes.  First, it serves as a demonstration
591541Srgrimes * of layering by proving a layer which does nothing.  (It actually
601541Srgrimes * does everything the loopback file system does, which is slightly
611541Srgrimes * more than nothing.)  Second, the null layer can serve as a prototype
621541Srgrimes * layer.  Since it provides all necessary layer framework,
631541Srgrimes * new file system layers can be created very easily be starting
641541Srgrimes * with a null layer.
651541Srgrimes *
661541Srgrimes * The remainder of this man page examines the null layer as a basis
671541Srgrimes * for constructing new layers.
681541Srgrimes *
691541Srgrimes *
701541Srgrimes * INSTANTIATING NEW NULL LAYERS
711541Srgrimes *
721541Srgrimes * New null layers are created with mount_null(8).
731541Srgrimes * Mount_null(8) takes two arguments, the pathname
741541Srgrimes * of the lower vfs (target-pn) and the pathname where the null
751541Srgrimes * layer will appear in the namespace (alias-pn).  After
761541Srgrimes * the null layer is put into place, the contents
771541Srgrimes * of target-pn subtree will be aliased under alias-pn.
781541Srgrimes *
791541Srgrimes *
801541Srgrimes * OPERATION OF A NULL LAYER
811541Srgrimes *
821541Srgrimes * The null layer is the minimum file system layer,
831541Srgrimes * simply bypassing all possible operations to the lower layer
841541Srgrimes * for processing there.  The majority of its activity centers
8526963Salex * on the bypass routine, through which nearly all vnode operations
861541Srgrimes * pass.
871541Srgrimes *
881541Srgrimes * The bypass routine accepts arbitrary vnode operations for
891541Srgrimes * handling by the lower layer.  It begins by examing vnode
901541Srgrimes * operation arguments and replacing any null-nodes by their
911541Srgrimes * lower-layer equivlants.  It then invokes the operation
921541Srgrimes * on the lower layer.  Finally, it replaces the null-nodes
931541Srgrimes * in the arguments and, if a vnode is return by the operation,
941541Srgrimes * stacks a null-node on top of the returned vnode.
951541Srgrimes *
9622521Sdyson * Although bypass handles most operations, vop_getattr, vop_lock,
9722521Sdyson * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
9822521Sdyson * bypassed. Vop_getattr must change the fsid being returned.
9922521Sdyson * Vop_lock and vop_unlock must handle any locking for the
10022521Sdyson * current vnode as well as pass the lock request down.
1011541Srgrimes * Vop_inactive and vop_reclaim are not bypassed so that
10222521Sdyson * they can handle freeing null-layer specific data. Vop_print
10322521Sdyson * is not bypassed to avoid excessive debugging information.
10422521Sdyson * Also, certain vnode operations change the locking state within
10522521Sdyson * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
10622521Sdyson * and symlink). Ideally these operations should not change the
10722521Sdyson * lock state, but should be changed to let the caller of the
10822521Sdyson * function unlock them. Otherwise all intermediate vnode layers
10922521Sdyson * (such as union, umapfs, etc) must catch these functions to do
11022521Sdyson * the necessary locking at their layer.
1111541Srgrimes *
1121541Srgrimes *
1131541Srgrimes * INSTANTIATING VNODE STACKS
1141541Srgrimes *
1151541Srgrimes * Mounting associates the null layer with a lower layer,
1161541Srgrimes * effect stacking two VFSes.  Vnode stacks are instead
1171541Srgrimes * created on demand as files are accessed.
1181541Srgrimes *
1191541Srgrimes * The initial mount creates a single vnode stack for the
1201541Srgrimes * root of the new null layer.  All other vnode stacks
1211541Srgrimes * are created as a result of vnode operations on
1221541Srgrimes * this or other null vnode stacks.
1231541Srgrimes *
1241541Srgrimes * New vnode stacks come into existance as a result of
1258876Srgrimes * an operation which returns a vnode.
1261541Srgrimes * The bypass routine stacks a null-node above the new
1271541Srgrimes * vnode before returning it to the caller.
1281541Srgrimes *
1291541Srgrimes * For example, imagine mounting a null layer with
1301541Srgrimes * "mount_null /usr/include /dev/layer/null".
1311541Srgrimes * Changing directory to /dev/layer/null will assign
1321541Srgrimes * the root null-node (which was created when the null layer was mounted).
1331541Srgrimes * Now consider opening "sys".  A vop_lookup would be
1341541Srgrimes * done on the root null-node.  This operation would bypass through
1358876Srgrimes * to the lower layer which would return a vnode representing
1361541Srgrimes * the UFS "sys".  Null_bypass then builds a null-node
1371541Srgrimes * aliasing the UFS "sys" and returns this to the caller.
1381541Srgrimes * Later operations on the null-node "sys" will repeat this
1391541Srgrimes * process when constructing other vnode stacks.
1401541Srgrimes *
1411541Srgrimes *
1421541Srgrimes * CREATING OTHER FILE SYSTEM LAYERS
1431541Srgrimes *
1441541Srgrimes * One of the easiest ways to construct new file system layers is to make
1451541Srgrimes * a copy of the null layer, rename all files and variables, and
1461541Srgrimes * then begin modifing the copy.  Sed can be used to easily rename
1471541Srgrimes * all variables.
1481541Srgrimes *
1498876Srgrimes * The umap layer is an example of a layer descended from the
1501541Srgrimes * null layer.
1511541Srgrimes *
1521541Srgrimes *
1531541Srgrimes * INVOKING OPERATIONS ON LOWER LAYERS
1541541Srgrimes *
1558876Srgrimes * There are two techniques to invoke operations on a lower layer
1561541Srgrimes * when the operation cannot be completely bypassed.  Each method
1571541Srgrimes * is appropriate in different situations.  In both cases,
1581541Srgrimes * it is the responsibility of the aliasing layer to make
1591541Srgrimes * the operation arguments "correct" for the lower layer
1601541Srgrimes * by mapping an vnode arguments to the lower layer.
1611541Srgrimes *
1621541Srgrimes * The first approach is to call the aliasing layer's bypass routine.
1631541Srgrimes * This method is most suitable when you wish to invoke the operation
16426964Salex * currently being handled on the lower layer.  It has the advantage
1651541Srgrimes * that the bypass routine already must do argument mapping.
1661541Srgrimes * An example of this is null_getattrs in the null layer.
1671541Srgrimes *
16826964Salex * A second approach is to directly invoke vnode operations on
1691541Srgrimes * the lower layer with the VOP_OPERATIONNAME interface.
1701541Srgrimes * The advantage of this method is that it is easy to invoke
1711541Srgrimes * arbitrary operations on the lower layer.  The disadvantage
17226964Salex * is that vnode arguments must be manualy mapped.
1731541Srgrimes *
1741541Srgrimes */
1751541Srgrimes
1761541Srgrimes#include <sys/param.h>
1771541Srgrimes#include <sys/systm.h>
1782960Swollman#include <sys/kernel.h>
17912769Sphk#include <sys/sysctl.h>
1801541Srgrimes#include <sys/vnode.h>
1811541Srgrimes#include <sys/mount.h>
1821541Srgrimes#include <sys/namei.h>
1831541Srgrimes#include <sys/malloc.h>
1841541Srgrimes#include <miscfs/nullfs/null.h>
1851541Srgrimes
18612769Sphkstatic int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
18712769SphkSYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
18812769Sphk	&null_bug_bypass, 0, "");
1891541Srgrimes
19065464Sbpstatic int	null_access(struct vop_access_args *ap);
19165464Sbpstatic int	null_getattr(struct vop_getattr_args *ap);
19265464Sbpstatic int	null_inactive(struct vop_inactive_args *ap);
19365464Sbpstatic int	null_lock(struct vop_lock_args *ap);
19465464Sbpstatic int	null_lookup(struct vop_lookup_args *ap);
19565467Sbpstatic int	null_open(struct vop_open_args *ap);
19665464Sbpstatic int	null_print(struct vop_print_args *ap);
19765464Sbpstatic int	null_reclaim(struct vop_reclaim_args *ap);
19865467Sbpstatic int	null_rename(struct vop_rename_args *ap);
19965464Sbpstatic int	null_setattr(struct vop_setattr_args *ap);
20065464Sbpstatic int	null_unlock(struct vop_unlock_args *ap);
20112595Sbde
2021541Srgrimes/*
2031541Srgrimes * This is the 10-Apr-92 bypass routine.
2041541Srgrimes *    This version has been optimized for speed, throwing away some
2051541Srgrimes * safety checks.  It should still always work, but it's not as
2061541Srgrimes * robust to programmer errors.
2071541Srgrimes *
2081541Srgrimes * In general, we map all vnodes going down and unmap them on the way back.
2091541Srgrimes * As an exception to this, vnodes can be marked "unmapped" by setting
2101541Srgrimes * the Nth bit in operation's vdesc_flags.
2111541Srgrimes *
2121541Srgrimes * Also, some BSD vnode operations have the side effect of vrele'ing
2131541Srgrimes * their arguments.  With stacking, the reference counts are held
2141541Srgrimes * by the upper node, not the lower one, so we must handle these
2151541Srgrimes * side-effects here.  This is not of concern in Sun-derived systems
2161541Srgrimes * since there are no such side-effects.
2171541Srgrimes *
2181541Srgrimes * This makes the following assumptions:
2191541Srgrimes * - only one returned vpp
2201541Srgrimes * - no INOUT vpp's (Sun's vop_open has one of these)
2211541Srgrimes * - the vnode operation vector of the first vnode should be used
2221541Srgrimes *   to determine what implementation of the op should be invoked
2231541Srgrimes * - all mapped vnodes are of our vnode-type (NEEDSWORK:
2241541Srgrimes *   problems on rmdir'ing mount points and renaming?)
2258876Srgrimes */
22622521Sdysonint
2271541Srgrimesnull_bypass(ap)
2281541Srgrimes	struct vop_generic_args /* {
2291541Srgrimes		struct vnodeop_desc *a_desc;
2301541Srgrimes		<other random data follows, presumably>
2311541Srgrimes	} */ *ap;
2321541Srgrimes{
2331541Srgrimes	register struct vnode **this_vp_p;
2341541Srgrimes	int error;
2351541Srgrimes	struct vnode *old_vps[VDESC_MAX_VPS];
2361541Srgrimes	struct vnode **vps_p[VDESC_MAX_VPS];
2371541Srgrimes	struct vnode ***vppp;
2381541Srgrimes	struct vnodeop_desc *descp = ap->a_desc;
2391541Srgrimes	int reles, i;
2401541Srgrimes
2411541Srgrimes	if (null_bug_bypass)
2421541Srgrimes		printf ("null_bypass: %s\n", descp->vdesc_name);
2431541Srgrimes
24450616Sbde#ifdef DIAGNOSTIC
2451541Srgrimes	/*
2461541Srgrimes	 * We require at least one vp.
2471541Srgrimes	 */
2481541Srgrimes	if (descp->vdesc_vp_offsets == NULL ||
2491541Srgrimes	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
25050616Sbde		panic ("null_bypass: no vp's in map");
2511541Srgrimes#endif
2521541Srgrimes
2531541Srgrimes	/*
2541541Srgrimes	 * Map the vnodes going in.
2551541Srgrimes	 * Later, we'll invoke the operation based on
2561541Srgrimes	 * the first mapped vnode's operation vector.
2571541Srgrimes	 */
2581541Srgrimes	reles = descp->vdesc_flags;
2591541Srgrimes	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
2601541Srgrimes		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
2611541Srgrimes			break;   /* bail out at end of list */
2628876Srgrimes		vps_p[i] = this_vp_p =
2631541Srgrimes			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
2641541Srgrimes		/*
2651541Srgrimes		 * We're not guaranteed that any but the first vnode
2661541Srgrimes		 * are of our type.  Check for and don't map any
2671541Srgrimes		 * that aren't.  (We must always map first vp or vclean fails.)
2681541Srgrimes		 */
26924987Skato		if (i && (*this_vp_p == NULLVP ||
27022521Sdyson		    (*this_vp_p)->v_op != null_vnodeop_p)) {
27124987Skato			old_vps[i] = NULLVP;
2721541Srgrimes		} else {
2731541Srgrimes			old_vps[i] = *this_vp_p;
2741541Srgrimes			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
2751541Srgrimes			/*
2761541Srgrimes			 * XXX - Several operations have the side effect
2771541Srgrimes			 * of vrele'ing their vp's.  We must account for
2781541Srgrimes			 * that.  (This should go away in the future.)
2791541Srgrimes			 */
2801541Srgrimes			if (reles & 1)
2811541Srgrimes				VREF(*this_vp_p);
2821541Srgrimes		}
2838876Srgrimes
2841541Srgrimes	}
2851541Srgrimes
2861541Srgrimes	/*
2871541Srgrimes	 * Call the operation on the lower layer
2881541Srgrimes	 * with the modified argument structure.
2891541Srgrimes	 */
2901541Srgrimes	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
2911541Srgrimes
2921541Srgrimes	/*
2931541Srgrimes	 * Maintain the illusion of call-by-value
2941541Srgrimes	 * by restoring vnodes in the argument structure
2951541Srgrimes	 * to their original value.
2961541Srgrimes	 */
2971541Srgrimes	reles = descp->vdesc_flags;
2981541Srgrimes	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
2991541Srgrimes		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
3001541Srgrimes			break;   /* bail out at end of list */
3011541Srgrimes		if (old_vps[i]) {
3021541Srgrimes			*(vps_p[i]) = old_vps[i];
3031541Srgrimes			if (reles & 1)
3041541Srgrimes				vrele(*(vps_p[i]));
3051541Srgrimes		}
3061541Srgrimes	}
3071541Srgrimes
3081541Srgrimes	/*
3091541Srgrimes	 * Map the possible out-going vpp
3101541Srgrimes	 * (Assumes that the lower layer always returns
3111541Srgrimes	 * a VREF'ed vpp unless it gets an error.)
3121541Srgrimes	 */
3131541Srgrimes	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
3141541Srgrimes	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
3151541Srgrimes	    !error) {
3161541Srgrimes		/*
3171541Srgrimes		 * XXX - even though some ops have vpp returned vp's,
3181541Srgrimes		 * several ops actually vrele this before returning.
3191541Srgrimes		 * We must avoid these ops.
3201541Srgrimes		 * (This should go away when these ops are regularized.)
3211541Srgrimes		 */
3221541Srgrimes		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
3231541Srgrimes			goto out;
3241541Srgrimes		vppp = VOPARG_OFFSETTO(struct vnode***,
3251541Srgrimes				 descp->vdesc_vpp_offset,ap);
32629584Sphk		if (*vppp)
32729584Sphk			error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
3281541Srgrimes	}
3291541Srgrimes
3301541Srgrimes out:
3311541Srgrimes	return (error);
3321541Srgrimes}
3331541Srgrimes
33422521Sdyson/*
33522521Sdyson * We have to carry on the locking protocol on the null layer vnodes
33622521Sdyson * as we progress through the tree. We also have to enforce read-only
33722521Sdyson * if this layer is mounted read-only.
33822521Sdyson */
33922521Sdysonstatic int
34022521Sdysonnull_lookup(ap)
34122521Sdyson	struct vop_lookup_args /* {
34222521Sdyson		struct vnode * a_dvp;
34322521Sdyson		struct vnode ** a_vpp;
34422521Sdyson		struct componentname * a_cnp;
34522521Sdyson	} */ *ap;
34622521Sdyson{
34722521Sdyson	struct componentname *cnp = ap->a_cnp;
34822521Sdyson	struct proc *p = cnp->cn_proc;
34922521Sdyson	int flags = cnp->cn_flags;
35022521Sdyson	struct vop_lock_args lockargs;
35122521Sdyson	struct vop_unlock_args unlockargs;
35222521Sdyson	struct vnode *dvp, *vp;
35322521Sdyson	int error;
3541541Srgrimes
35522521Sdyson	if ((flags & ISLASTCN) && (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
35622521Sdyson	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
35722521Sdyson		return (EROFS);
35822607Smpp	error = null_bypass((struct vop_generic_args *)ap);
35922521Sdyson	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
36022521Sdyson	    (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
36122521Sdyson	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
36222521Sdyson		error = EROFS;
36322521Sdyson	/*
36422521Sdyson	 * We must do the same locking and unlocking at this layer as
36522521Sdyson	 * is done in the layers below us. We could figure this out
36622521Sdyson	 * based on the error return and the LASTCN, LOCKPARENT, and
36722521Sdyson	 * LOCKLEAF flags. However, it is more expidient to just find
36822521Sdyson	 * out the state of the lower level vnodes and set ours to the
36922521Sdyson	 * same state.
37022521Sdyson	 */
37122521Sdyson	dvp = ap->a_dvp;
37222521Sdyson	vp = *ap->a_vpp;
37322521Sdyson	if (dvp == vp)
37422521Sdyson		return (error);
37554444Seivind	if (!VOP_ISLOCKED(dvp, NULL)) {
37622521Sdyson		unlockargs.a_vp = dvp;
37722521Sdyson		unlockargs.a_flags = 0;
37822521Sdyson		unlockargs.a_p = p;
37922521Sdyson		vop_nounlock(&unlockargs);
38022521Sdyson	}
38154444Seivind	if (vp != NULLVP && VOP_ISLOCKED(vp, NULL)) {
38222521Sdyson		lockargs.a_vp = vp;
38322521Sdyson		lockargs.a_flags = LK_SHARED;
38422521Sdyson		lockargs.a_p = p;
38522521Sdyson		vop_nolock(&lockargs);
38622521Sdyson	}
38722521Sdyson	return (error);
38822521Sdyson}
38922521Sdyson
3901541Srgrimes/*
39122521Sdyson * Setattr call. Disallow write attempts if the layer is mounted read-only.
39222521Sdyson */
39322521Sdysonint
39422521Sdysonnull_setattr(ap)
39522521Sdyson	struct vop_setattr_args /* {
39622521Sdyson		struct vnodeop_desc *a_desc;
39722521Sdyson		struct vnode *a_vp;
39822521Sdyson		struct vattr *a_vap;
39922521Sdyson		struct ucred *a_cred;
40022521Sdyson		struct proc *a_p;
40122521Sdyson	} */ *ap;
40222521Sdyson{
40322521Sdyson	struct vnode *vp = ap->a_vp;
40422521Sdyson	struct vattr *vap = ap->a_vap;
40522521Sdyson
40622521Sdyson  	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
40722597Smpp	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
40822597Smpp	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
40922521Sdyson	    (vp->v_mount->mnt_flag & MNT_RDONLY))
41022521Sdyson		return (EROFS);
41122521Sdyson	if (vap->va_size != VNOVAL) {
41222521Sdyson 		switch (vp->v_type) {
41322521Sdyson 		case VDIR:
41422521Sdyson 			return (EISDIR);
41522521Sdyson 		case VCHR:
41622521Sdyson 		case VBLK:
41722521Sdyson 		case VSOCK:
41822521Sdyson 		case VFIFO:
41936840Speter			if (vap->va_flags != VNOVAL)
42036840Speter				return (EOPNOTSUPP);
42122521Sdyson			return (0);
42222521Sdyson		case VREG:
42322521Sdyson		case VLNK:
42422521Sdyson 		default:
42522521Sdyson			/*
42622521Sdyson			 * Disallow write attempts if the filesystem is
42722521Sdyson			 * mounted read-only.
42822521Sdyson			 */
42922521Sdyson			if (vp->v_mount->mnt_flag & MNT_RDONLY)
43022521Sdyson				return (EROFS);
43122521Sdyson		}
43222521Sdyson	}
43322607Smpp	return (null_bypass((struct vop_generic_args *)ap));
43422521Sdyson}
43522521Sdyson
43622521Sdyson/*
4371541Srgrimes *  We handle getattr only to change the fsid.
4381541Srgrimes */
43912769Sphkstatic int
4401541Srgrimesnull_getattr(ap)
4411541Srgrimes	struct vop_getattr_args /* {
4421541Srgrimes		struct vnode *a_vp;
4431541Srgrimes		struct vattr *a_vap;
4441541Srgrimes		struct ucred *a_cred;
4451541Srgrimes		struct proc *a_p;
4461541Srgrimes	} */ *ap;
4471541Srgrimes{
4481541Srgrimes	int error;
44922521Sdyson
45043311Sdillon	if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
4511541Srgrimes		return (error);
45265467Sbp
45365467Sbp	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
4541541Srgrimes	return (0);
4551541Srgrimes}
4561541Srgrimes
45722521Sdysonstatic int
45822521Sdysonnull_access(ap)
45922521Sdyson	struct vop_access_args /* {
46022521Sdyson		struct vnode *a_vp;
46122521Sdyson		int  a_mode;
46222521Sdyson		struct ucred *a_cred;
46322521Sdyson		struct proc *a_p;
46422521Sdyson	} */ *ap;
46522521Sdyson{
46622521Sdyson	struct vnode *vp = ap->a_vp;
46722521Sdyson	mode_t mode = ap->a_mode;
4681541Srgrimes
46922521Sdyson	/*
47022521Sdyson	 * Disallow write attempts on read-only layers;
47122521Sdyson	 * unless the file is a socket, fifo, or a block or
47222521Sdyson	 * character device resident on the file system.
47322521Sdyson	 */
47422521Sdyson	if (mode & VWRITE) {
47522521Sdyson		switch (vp->v_type) {
47622521Sdyson		case VDIR:
47722521Sdyson		case VLNK:
47822521Sdyson		case VREG:
47922521Sdyson			if (vp->v_mount->mnt_flag & MNT_RDONLY)
48022521Sdyson				return (EROFS);
48122521Sdyson			break;
48243305Sdillon		default:
48343305Sdillon			break;
48422521Sdyson		}
48522521Sdyson	}
48622607Smpp	return (null_bypass((struct vop_generic_args *)ap));
48722521Sdyson}
48822521Sdyson
48922521Sdyson/*
49065467Sbp * We must handle open to be able to catch MNT_NODEV and friends.
49165467Sbp */
49265467Sbpstatic int
49365467Sbpnull_open(ap)
49465467Sbp	struct vop_open_args /* {
49565467Sbp		struct vnode *a_vp;
49665467Sbp		int  a_mode;
49765467Sbp		struct ucred *a_cred;
49865467Sbp		struct proc *a_p;
49965467Sbp	} */ *ap;
50065467Sbp{
50165467Sbp	struct vnode *vp = ap->a_vp;
50265467Sbp	struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
50365467Sbp
50465467Sbp	if ((vp->v_mount->mnt_flag & MNT_NODEV) &&
50565467Sbp	    (lvp->v_type == VBLK || lvp->v_type == VCHR))
50665467Sbp		return ENXIO;
50765467Sbp
50865467Sbp	return (null_bypass((struct vop_generic_args *)ap));
50965467Sbp}
51065467Sbp
51165467Sbp/*
51265467Sbp * We handle this to eliminate null FS to lower FS
51365467Sbp * file moving. Don't know why we don't allow this,
51465467Sbp * possibly we should.
51565467Sbp */
51665467Sbpstatic int
51765467Sbpnull_rename(ap)
51865467Sbp	struct vop_rename_args /* {
51965467Sbp		struct vnode *a_fdvp;
52065467Sbp		struct vnode *a_fvp;
52165467Sbp		struct componentname *a_fcnp;
52265467Sbp		struct vnode *a_tdvp;
52365467Sbp		struct vnode *a_tvp;
52465467Sbp		struct componentname *a_tcnp;
52565467Sbp	} */ *ap;
52665467Sbp{
52765467Sbp	struct vnode *tdvp = ap->a_tdvp;
52865467Sbp	struct vnode *fvp = ap->a_fvp;
52965467Sbp	struct vnode *fdvp = ap->a_fdvp;
53065467Sbp	struct vnode *tvp = ap->a_tvp;
53165467Sbp
53265467Sbp	/* Check for cross-device rename. */
53365467Sbp	if ((fvp->v_mount != tdvp->v_mount) ||
53465467Sbp	    (tvp && (fvp->v_mount != tvp->v_mount))) {
53565467Sbp		if (tdvp == tvp)
53665467Sbp			vrele(tdvp);
53765467Sbp		else
53865467Sbp			vput(tdvp);
53965467Sbp		if (tvp)
54065467Sbp			vput(tvp);
54165467Sbp		vrele(fdvp);
54265467Sbp		vrele(fvp);
54365467Sbp		return (EXDEV);
54465467Sbp	}
54565467Sbp
54665467Sbp	return (null_bypass((struct vop_generic_args *)ap));
54765467Sbp}
54865467Sbp
54965467Sbp/*
55022521Sdyson * We need to process our own vnode lock and then clear the
55122521Sdyson * interlock flag as it applies only to our vnode, not the
55222521Sdyson * vnodes below us on the stack.
55322521Sdyson */
55422597Smppstatic int
55522521Sdysonnull_lock(ap)
55622521Sdyson	struct vop_lock_args /* {
55722521Sdyson		struct vnode *a_vp;
55822521Sdyson		int a_flags;
55922521Sdyson		struct proc *a_p;
56022521Sdyson	} */ *ap;
56122521Sdyson{
56222521Sdyson
56322521Sdyson	vop_nolock(ap);
56422521Sdyson	if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
56522521Sdyson		return (0);
56622521Sdyson	ap->a_flags &= ~LK_INTERLOCK;
56722607Smpp	return (null_bypass((struct vop_generic_args *)ap));
56822521Sdyson}
56922521Sdyson
57022521Sdyson/*
57122521Sdyson * We need to process our own vnode unlock and then clear the
57222521Sdyson * interlock flag as it applies only to our vnode, not the
57322521Sdyson * vnodes below us on the stack.
57422521Sdyson */
57522597Smppstatic int
57622521Sdysonnull_unlock(ap)
57722521Sdyson	struct vop_unlock_args /* {
57822521Sdyson		struct vnode *a_vp;
57922521Sdyson		int a_flags;
58022521Sdyson		struct proc *a_p;
58122521Sdyson	} */ *ap;
58222521Sdyson{
58322521Sdyson	vop_nounlock(ap);
58422521Sdyson	ap->a_flags &= ~LK_INTERLOCK;
58522607Smpp	return (null_bypass((struct vop_generic_args *)ap));
58622521Sdyson}
58722521Sdyson
58822597Smppstatic int
5891541Srgrimesnull_inactive(ap)
5901541Srgrimes	struct vop_inactive_args /* {
5911541Srgrimes		struct vnode *a_vp;
59222521Sdyson		struct proc *a_p;
5931541Srgrimes	} */ *ap;
5941541Srgrimes{
59530636Sroberto	struct vnode *vp = ap->a_vp;
59630636Sroberto	struct null_node *xp = VTONULL(vp);
59730636Sroberto	struct vnode *lowervp = xp->null_lowervp;
5981541Srgrimes	/*
5991541Srgrimes	 * Do nothing (and _don't_ bypass).
6001541Srgrimes	 * Wait to vrele lowervp until reclaim,
6011541Srgrimes	 * so that until then our null_node is in the
6021541Srgrimes	 * cache and reusable.
60330636Sroberto	 * We still have to tell the lower layer the vnode
60430636Sroberto	 * is now inactive though.
6051541Srgrimes	 *
6061541Srgrimes	 * NEEDSWORK: Someday, consider inactive'ing
6071541Srgrimes	 * the lowervp and then trying to reactivate it
6081541Srgrimes	 * with capabilities (v_id)
6091541Srgrimes	 * like they do in the name lookup cache code.
6101541Srgrimes	 * That's too much work for now.
6111541Srgrimes	 */
61230636Sroberto	VOP_INACTIVE(lowervp, ap->a_p);
61322521Sdyson	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
6141541Srgrimes	return (0);
6151541Srgrimes}
6161541Srgrimes
61712769Sphkstatic int
6181541Srgrimesnull_reclaim(ap)
6191541Srgrimes	struct vop_reclaim_args /* {
6201541Srgrimes		struct vnode *a_vp;
62122521Sdyson		struct proc *a_p;
6221541Srgrimes	} */ *ap;
6231541Srgrimes{
6241541Srgrimes	struct vnode *vp = ap->a_vp;
6251541Srgrimes	struct null_node *xp = VTONULL(vp);
6261541Srgrimes	struct vnode *lowervp = xp->null_lowervp;
6271541Srgrimes
6281541Srgrimes	/*
6291541Srgrimes	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
6301541Srgrimes	 * so we can't call VOPs on ourself.
6311541Srgrimes	 */
6321541Srgrimes	/* After this assignment, this node will not be re-used. */
63324987Skato	xp->null_lowervp = NULLVP;
63465467Sbp	lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, ap->a_p);
63522521Sdyson	LIST_REMOVE(xp, null_hash);
63665467Sbp	lockmgr(&null_hashlock, LK_RELEASE, NULL, ap->a_p);
6371541Srgrimes	FREE(vp->v_data, M_TEMP);
6381541Srgrimes	vp->v_data = NULL;
6391541Srgrimes	vrele (lowervp);
6401541Srgrimes	return (0);
6411541Srgrimes}
6421541Srgrimes
64312769Sphkstatic int
6441541Srgrimesnull_print(ap)
6451541Srgrimes	struct vop_print_args /* {
6461541Srgrimes		struct vnode *a_vp;
6471541Srgrimes	} */ *ap;
6481541Srgrimes{
6491541Srgrimes	register struct vnode *vp = ap->a_vp;
6503496Sphk	printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
6511541Srgrimes	return (0);
6521541Srgrimes}
6531541Srgrimes
6541541Srgrimes/*
6551541Srgrimes * Global vfs data structures
6561541Srgrimes */
65712158Sbdevop_t **null_vnodeop_p;
65812769Sphkstatic struct vnodeopv_entry_desc null_vnodeop_entries[] = {
65930431Sphk	{ &vop_default_desc,		(vop_t *) null_bypass },
66030431Sphk	{ &vop_access_desc,		(vop_t *) null_access },
66165467Sbp	{ &vop_bmap_desc,		(vop_t *) vop_eopnotsupp },
66230431Sphk	{ &vop_getattr_desc,		(vop_t *) null_getattr },
66365467Sbp	{ &vop_getwritemount_desc,	(vop_t *) vop_stdgetwritemount},
66430434Sphk	{ &vop_inactive_desc,		(vop_t *) null_inactive },
66530431Sphk	{ &vop_lock_desc,		(vop_t *) null_lock },
66630431Sphk	{ &vop_lookup_desc,		(vop_t *) null_lookup },
66765467Sbp	{ &vop_open_desc,		(vop_t *) null_open },
66830431Sphk	{ &vop_print_desc,		(vop_t *) null_print },
66930431Sphk	{ &vop_reclaim_desc,		(vop_t *) null_reclaim },
67065467Sbp	{ &vop_rename_desc,		(vop_t *) null_rename },
67130431Sphk	{ &vop_setattr_desc,		(vop_t *) null_setattr },
67265467Sbp	{ &vop_strategy_desc,		(vop_t *) vop_eopnotsupp },
67330431Sphk	{ &vop_unlock_desc,		(vop_t *) null_unlock },
67412158Sbde	{ NULL, NULL }
6751541Srgrimes};
67612769Sphkstatic struct vnodeopv_desc null_vnodeop_opv_desc =
6771541Srgrimes	{ &null_vnodeop_p, null_vnodeop_entries };
6782946Swollman
6792946SwollmanVNODEOP_SET(null_vnodeop_opv_desc);
680