null_vnops.c revision 30636
119872Swollman/*
219872Swollman * Copyright (c) 1992, 1993
319872Swollman *	The Regents of the University of California.  All rights reserved.
419872Swollman *
519872Swollman * This code is derived from software contributed to Berkeley by
619872Swollman * John Heidemann of the UCLA Ficus project.
719872Swollman *
819872Swollman * Redistribution and use in source and binary forms, with or without
919872Swollman * modification, are permitted provided that the following conditions
1019872Swollman * are met:
1119872Swollman * 1. Redistributions of source code must retain the above copyright
1219872Swollman *    notice, this list of conditions and the following disclaimer.
1319872Swollman * 2. Redistributions in binary form must reproduce the above copyright
1419872Swollman *    notice, this list of conditions and the following disclaimer in the
15179530Sjkim *    documentation and/or other materials provided with the distribution.
1619872Swollman * 3. All advertising materials mentioning features or use of this software
1719872Swollman *    must display the following acknowledgement:
1819872Swollman *	This product includes software developed by the University of
1919872Swollman *	California, Berkeley and its contributors.
2019872Swollman * 4. Neither the name of the University nor the names of its contributors
2119872Swollman *    may be used to endorse or promote products derived from this software
2219872Swollman *    without specific prior written permission.
2319872Swollman *
2419872Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2519872Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2619872Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2719872Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2819872Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2919872Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3019872Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3119872Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3219872Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3319872Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3419872Swollman * SUCH DAMAGE.
35179530Sjkim *
36179530Sjkim *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
3730763Scharnier *
3819872Swollman * Ancestors:
3919872Swollman *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
4019872Swollman *	$Id: null_vnops.c,v 1.24 1997/10/15 10:04:31 phk Exp $
4119872Swollman *	...and...
4219872Swollman *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
4366907Swollman *
4419872Swollman * $Id: null_vnops.c,v 1.24 1997/10/15 10:04:31 phk Exp $
4519872Swollman */
4619872Swollman
47198267Sedwin/*
4819872Swollman * Null Layer
4919872Swollman *
50274394Sdteske * (See mount_null(8) for more information.)
5119872Swollman *
52227934Sfjoe * The null layer duplicates a portion of the file system
53227934Sfjoe * name space under a new name.  In this respect, it is
54179530Sjkim * similar to the loopback file system.  It differs from
55179530Sjkim * the loopback fs in two respects:  it is implemented using
56179530Sjkim * a stackable layers techniques, and it's "null-node"s stack above
57179530Sjkim * all lower-layer vnodes, not just over directory vnodes.
58198267Sedwin *
59179530Sjkim * The null layer has two purposes.  First, it serves as a demonstration
6019872Swollman * of layering by proving a layer which does nothing.  (It actually
61230005Swollman * does everything the loopback file system does, which is slightly
62230005Swollman * more than nothing.)  Second, the null layer can serve as a prototype
63230005Swollman * layer.  Since it provides all necessary layer framework,
64230005Swollman * new file system layers can be created very easily be starting
65230005Swollman * with a null layer.
66230005Swollman *
67230005Swollman * The remainder of this man page examines the null layer as a basis
68227934Sfjoe * for constructing new layers.
69227934Sfjoe *
70227934Sfjoe *
71227934Sfjoe * INSTANTIATING NEW NULL LAYERS
72227934Sfjoe *
73227934Sfjoe * New null layers are created with mount_null(8).
74227934Sfjoe * Mount_null(8) takes two arguments, the pathname
75227934Sfjoe * of the lower vfs (target-pn) and the pathname where the null
76227934Sfjoe * layer will appear in the namespace (alias-pn).  After
77227934Sfjoe * the null layer is put into place, the contents
78227934Sfjoe * of target-pn subtree will be aliased under alias-pn.
79227934Sfjoe *
80227934Sfjoe *
81227934Sfjoe * OPERATION OF A NULL LAYER
82227934Sfjoe *
83227934Sfjoe * The null layer is the minimum file system layer,
84228176Sfjoe * simply bypassing all possible operations to the lower layer
85228176Sfjoe * for processing there.  The majority of its activity centers
86228176Sfjoe * on the bypass routine, through which nearly all vnode operations
87228176Sfjoe * pass.
88228176Sfjoe *
89228176Sfjoe * The bypass routine accepts arbitrary vnode operations for
90228176Sfjoe * handling by the lower layer.  It begins by examing vnode
91228176Sfjoe * operation arguments and replacing any null-nodes by their
92228176Sfjoe * lower-layer equivlants.  It then invokes the operation
93228176Sfjoe * on the lower layer.  Finally, it replaces the null-nodes
94228176Sfjoe * in the arguments and, if a vnode is return by the operation,
95228176Sfjoe * stacks a null-node on top of the returned vnode.
96228176Sfjoe *
97228176Sfjoe * Although bypass handles most operations, vop_getattr, vop_lock,
98228176Sfjoe * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
99228176Sfjoe * bypassed. Vop_getattr must change the fsid being returned.
100228176Sfjoe * Vop_lock and vop_unlock must handle any locking for the
101228176Sfjoe * current vnode as well as pass the lock request down.
102228176Sfjoe * Vop_inactive and vop_reclaim are not bypassed so that
103228176Sfjoe * they can handle freeing null-layer specific data. Vop_print
104228176Sfjoe * is not bypassed to avoid excessive debugging information.
105228176Sfjoe * Also, certain vnode operations change the locking state within
106228176Sfjoe * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
107228176Sfjoe * and symlink). Ideally these operations should not change the
108228176Sfjoe * lock state, but should be changed to let the caller of the
109228176Sfjoe * function unlock them. Otherwise all intermediate vnode layers
110228176Sfjoe * (such as union, umapfs, etc) must catch these functions to do
111228176Sfjoe * the necessary locking at their layer.
112228176Sfjoe *
113228176Sfjoe *
114228176Sfjoe * INSTANTIATING VNODE STACKS
115228176Sfjoe *
116227934Sfjoe * Mounting associates the null layer with a lower layer,
117227934Sfjoe * effect stacking two VFSes.  Vnode stacks are instead
118227934Sfjoe * created on demand as files are accessed.
119227947Sfjoe *
120227934Sfjoe * The initial mount creates a single vnode stack for the
121227934Sfjoe * root of the new null layer.  All other vnode stacks
122227934Sfjoe * are created as a result of vnode operations on
123227934Sfjoe * this or other null vnode stacks.
124227934Sfjoe *
125227934Sfjoe * New vnode stacks come into existance as a result of
126227947Sfjoe * an operation which returns a vnode.
127227934Sfjoe * The bypass routine stacks a null-node above the new
128227934Sfjoe * vnode before returning it to the caller.
129227934Sfjoe *
130227934Sfjoe * For example, imagine mounting a null layer with
131227934Sfjoe * "mount_null /usr/include /dev/layer/null".
132227934Sfjoe * Changing directory to /dev/layer/null will assign
133228176Sfjoe * the root null-node (which was created when the null layer was mounted).
134228176Sfjoe * Now consider opening "sys".  A vop_lookup would be
135228176Sfjoe * done on the root null-node.  This operation would bypass through
136228176Sfjoe * to the lower layer which would return a vnode representing
137228176Sfjoe * the UFS "sys".  Null_bypass then builds a null-node
138228176Sfjoe * aliasing the UFS "sys" and returns this to the caller.
139227934Sfjoe * Later operations on the null-node "sys" will repeat this
140227934Sfjoe * process when constructing other vnode stacks.
141227934Sfjoe *
142227934Sfjoe *
143227934Sfjoe * CREATING OTHER FILE SYSTEM LAYERS
144227934Sfjoe *
145227934Sfjoe * One of the easiest ways to construct new file system layers is to make
146227934Sfjoe * a copy of the null layer, rename all files and variables, and
147227934Sfjoe * then begin modifing the copy.  Sed can be used to easily rename
148227934Sfjoe * all variables.
149227934Sfjoe *
150227934Sfjoe * The umap layer is an example of a layer descended from the
151227934Sfjoe * null layer.
152228176Sfjoe *
153227934Sfjoe *
154227934Sfjoe * INVOKING OPERATIONS ON LOWER LAYERS
155227934Sfjoe *
156227934Sfjoe * There are two techniques to invoke operations on a lower layer
157227934Sfjoe * when the operation cannot be completely bypassed.  Each method
158227934Sfjoe * is appropriate in different situations.  In both cases,
159227934Sfjoe * it is the responsibility of the aliasing layer to make
160227947Sfjoe * the operation arguments "correct" for the lower layer
161227934Sfjoe * by mapping an vnode arguments to the lower layer.
162227934Sfjoe *
163227934Sfjoe * The first approach is to call the aliasing layer's bypass routine.
164227934Sfjoe * This method is most suitable when you wish to invoke the operation
165227934Sfjoe * currently being handled on the lower layer.  It has the advantage
166227934Sfjoe * that the bypass routine already must do argument mapping.
167227934Sfjoe * An example of this is null_getattrs in the null layer.
168227934Sfjoe *
169227934Sfjoe * A second approach is to directly invoke vnode operations on
170227934Sfjoe * the lower layer with the VOP_OPERATIONNAME interface.
171227934Sfjoe * The advantage of this method is that it is easy to invoke
172227934Sfjoe * arbitrary operations on the lower layer.  The disadvantage
173227934Sfjoe * is that vnode arguments must be manualy mapped.
174227934Sfjoe *
175227934Sfjoe */
176227934Sfjoe
177227934Sfjoe#include <sys/param.h>
178227934Sfjoe#include <sys/systm.h>
179227934Sfjoe#include <sys/kernel.h>
180227934Sfjoe#include <sys/sysctl.h>
181227934Sfjoe#include <sys/vnode.h>
182227934Sfjoe#include <sys/mount.h>
183227934Sfjoe#include <sys/namei.h>
184227934Sfjoe#include <sys/malloc.h>
185227934Sfjoe#include <sys/buf.h>
186227934Sfjoe#include <miscfs/nullfs/null.h>
187227934Sfjoe
188227934Sfjoestatic int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
189227934SfjoeSYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
190198350Sedwin	&null_bug_bypass, 0, "");
191227934Sfjoe
192198350Sedwinstatic int	null_access __P((struct vop_access_args *ap));
193198350Sedwinstatic int	null_bwrite __P((struct vop_bwrite_args *ap));
19419872Swollmanstatic int	null_getattr __P((struct vop_getattr_args *ap));
195198267Sedwinstatic int	null_inactive __P((struct vop_inactive_args *ap));
196198350Sedwinstatic int	null_lock __P((struct vop_lock_args *ap));
197198350Sedwinstatic int	null_lookup __P((struct vop_lookup_args *ap));
19819872Swollmanstatic int	null_print __P((struct vop_print_args *ap));
199179530Sjkimstatic int	null_reclaim __P((struct vop_reclaim_args *ap));
200220172Sedwinstatic int	null_setattr __P((struct vop_setattr_args *ap));
201179530Sjkimstatic int	null_strategy __P((struct vop_strategy_args *ap));
202220172Sedwinstatic int	null_unlock __P((struct vop_unlock_args *ap));
203179530Sjkim
204179530Sjkim/*
205179530Sjkim * This is the 10-Apr-92 bypass routine.
206220172Sedwin *    This version has been optimized for speed, throwing away some
20719872Swollman * safety checks.  It should still always work, but it's not as
20819872Swollman * robust to programmer errors.
20919872Swollman *    Define SAFETY to include some error checking code.
210179530Sjkim *
21119872Swollman * In general, we map all vnodes going down and unmap them on the way back.
21219872Swollman * As an exception to this, vnodes can be marked "unmapped" by setting
213179530Sjkim * the Nth bit in operation's vdesc_flags.
214220172Sedwin *
21519872Swollman * Also, some BSD vnode operations have the side effect of vrele'ing
21619872Swollman * their arguments.  With stacking, the reference counts are held
217179530Sjkim * by the upper node, not the lower one, so we must handle these
21819872Swollman * side-effects here.  This is not of concern in Sun-derived systems
21919872Swollman * since there are no such side-effects.
220179530Sjkim *
221179530Sjkim * This makes the following assumptions:
222179530Sjkim * - only one returned vpp
223179530Sjkim * - no INOUT vpp's (Sun's vop_open has one of these)
224179530Sjkim * - the vnode operation vector of the first vnode should be used
225179530Sjkim *   to determine what implementation of the op should be invoked
226179530Sjkim * - all mapped vnodes are of our vnode-type (NEEDSWORK:
227179530Sjkim *   problems on rmdir'ing mount points and renaming?)
228179530Sjkim */
229220172Sedwinint
230227934Sfjoenull_bypass(ap)
23119872Swollman	struct vop_generic_args /* {
23219872Swollman		struct vnodeop_desc *a_desc;
233179530Sjkim		<other random data follows, presumably>
234179530Sjkim	} */ *ap;
235179530Sjkim{
236179530Sjkim	register struct vnode **this_vp_p;
237179530Sjkim	int error;
238179530Sjkim	struct vnode *old_vps[VDESC_MAX_VPS];
239179530Sjkim	struct vnode **vps_p[VDESC_MAX_VPS];
240179530Sjkim	struct vnode ***vppp;
241179530Sjkim	struct vnodeop_desc *descp = ap->a_desc;
242179530Sjkim	int reles, i;
243179530Sjkim
244179530Sjkim	if (null_bug_bypass)
245179530Sjkim		printf ("null_bypass: %s\n", descp->vdesc_name);
246220172Sedwin
247220172Sedwin#ifdef SAFETY
24819872Swollman	/*
24919872Swollman	 * We require at least one vp.
250179530Sjkim	 */
251179530Sjkim	if (descp->vdesc_vp_offsets == NULL ||
252179530Sjkim	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
253179530Sjkim		panic ("null_bypass: no vp's in map.");
254179530Sjkim#endif
255179530Sjkim
25619872Swollman	/*
25719872Swollman	 * Map the vnodes going in.
25819872Swollman	 * Later, we'll invoke the operation based on
259179530Sjkim	 * the first mapped vnode's operation vector.
26019872Swollman	 */
261179530Sjkim	reles = descp->vdesc_flags;
262179530Sjkim	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
263179530Sjkim		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
26419872Swollman			break;   /* bail out at end of list */
265220172Sedwin		vps_p[i] = this_vp_p =
266227934Sfjoe			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
267220172Sedwin		/*
26819872Swollman		 * We're not guaranteed that any but the first vnode
269179497Sjkim		 * are of our type.  Check for and don't map any
270179497Sjkim		 * that aren't.  (We must always map first vp or vclean fails.)
27119872Swollman		 */
27219872Swollman		if (i && (*this_vp_p == NULLVP ||
273179530Sjkim		    (*this_vp_p)->v_op != null_vnodeop_p)) {
274179530Sjkim			old_vps[i] = NULLVP;
275179530Sjkim		} else {
276179530Sjkim			old_vps[i] = *this_vp_p;
277179530Sjkim			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
278179530Sjkim			/*
279179530Sjkim			 * XXX - Several operations have the side effect
280179530Sjkim			 * of vrele'ing their vp's.  We must account for
281179530Sjkim			 * that.  (This should go away in the future.)
28219872Swollman			 */
28319872Swollman			if (reles & 1)
284227934Sfjoe				VREF(*this_vp_p);
285227934Sfjoe		}
28619872Swollman
287179530Sjkim	}
288179530Sjkim
28919872Swollman	/*
29019872Swollman	 * Call the operation on the lower layer
29119872Swollman	 * with the modified argument structure.
29219872Swollman	 */
29319872Swollman	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
294179530Sjkim
29519872Swollman	/*
296179530Sjkim	 * Maintain the illusion of call-by-value
29719872Swollman	 * by restoring vnodes in the argument structure
298179530Sjkim	 * to their original value.
299179530Sjkim	 */
30019872Swollman	reles = descp->vdesc_flags;
30119872Swollman	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
30219872Swollman		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
303179530Sjkim			break;   /* bail out at end of list */
304179530Sjkim		if (old_vps[i]) {
305179530Sjkim			*(vps_p[i]) = old_vps[i];
306179530Sjkim			if (reles & 1)
307179530Sjkim				vrele(*(vps_p[i]));
308179530Sjkim		}
309179530Sjkim	}
31019872Swollman
31119872Swollman	/*
31219872Swollman	 * Map the possible out-going vpp
31360938Sjake	 * (Assumes that the lower layer always returns
314179530Sjkim	 * a VREF'ed vpp unless it gets an error.)
315179530Sjkim	 */
31619872Swollman	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
31719872Swollman	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
31819872Swollman	    !error) {
31919872Swollman		/*
32019872Swollman		 * XXX - even though some ops have vpp returned vp's,
32119872Swollman		 * several ops actually vrele this before returning.
32219872Swollman		 * We must avoid these ops.
32319872Swollman		 * (This should go away when these ops are regularized.)
324179530Sjkim		 */
32522181Sjhay		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
32619872Swollman			goto out;
327179530Sjkim		vppp = VOPARG_OFFSETTO(struct vnode***,
328179530Sjkim				 descp->vdesc_vpp_offset,ap);
32919872Swollman		if (*vppp)
33019872Swollman			error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
33119872Swollman	}
33219872Swollman
33319872Swollman out:
33419872Swollman	return (error);
33519872Swollman}
336179530Sjkim
337179530Sjkim/*
338179530Sjkim * We have to carry on the locking protocol on the null layer vnodes
339179530Sjkim * as we progress through the tree. We also have to enforce read-only
340179530Sjkim * if this layer is mounted read-only.
34119872Swollman */
342198350Sedwinstatic int
34319872Swollmannull_lookup(ap)
344209190Semaste	struct vop_lookup_args /* {
34519872Swollman		struct vnode * a_dvp;
34619872Swollman		struct vnode ** a_vpp;
347298033Saraujo		struct componentname * a_cnp;
34819872Swollman	} */ *ap;
34919872Swollman{
350198350Sedwin	struct componentname *cnp = ap->a_cnp;
35119872Swollman	struct proc *p = cnp->cn_proc;
35230999Sjoerg	int flags = cnp->cn_flags;
35319872Swollman	struct vop_lock_args lockargs;
35419872Swollman	struct vop_unlock_args unlockargs;
35519872Swollman	struct vnode *dvp, *vp;
35619872Swollman	int error;
357298033Saraujo
358198350Sedwin	if ((flags & ISLASTCN) && (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
35919872Swollman	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
360198350Sedwin		return (EROFS);
361179530Sjkim	error = null_bypass((struct vop_generic_args *)ap);
36219872Swollman	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
36319872Swollman	    (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) &&
364179530Sjkim	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
365298033Saraujo		error = EROFS;
366198350Sedwin	/*
367179530Sjkim	 * We must do the same locking and unlocking at this layer as
368298033Saraujo	 * is done in the layers below us. We could figure this out
369198350Sedwin	 * based on the error return and the LASTCN, LOCKPARENT, and
37019872Swollman	 * LOCKLEAF flags. However, it is more expidient to just find
37119872Swollman	 * out the state of the lower level vnodes and set ours to the
37219872Swollman	 * same state.
37319872Swollman	 */
37419872Swollman	dvp = ap->a_dvp;
375198350Sedwin	vp = *ap->a_vpp;
376198350Sedwin	if (dvp == vp)
37719872Swollman		return (error);
37856487Scharnier	if (!VOP_ISLOCKED(dvp)) {
37956487Scharnier		unlockargs.a_vp = dvp;
38019872Swollman		unlockargs.a_flags = 0;
38156487Scharnier		unlockargs.a_p = p;
38256487Scharnier		vop_nounlock(&unlockargs);
38319872Swollman	}
38419872Swollman	if (vp != NULLVP && VOP_ISLOCKED(vp)) {
38519872Swollman		lockargs.a_vp = vp;
38619872Swollman		lockargs.a_flags = LK_SHARED;
38719872Swollman		lockargs.a_p = p;
38819872Swollman		vop_nolock(&lockargs);
38919872Swollman	}
390179530Sjkim	return (error);
39119872Swollman}
392179530Sjkim
393179530Sjkim/*
39419872Swollman * Setattr call. Disallow write attempts if the layer is mounted read-only.
39519872Swollman */
396198350Sedwinint
397179530Sjkimnull_setattr(ap)
398227934Sfjoe	struct vop_setattr_args /* {
39919872Swollman		struct vnodeop_desc *a_desc;
40019872Swollman		struct vnode *a_vp;
401198350Sedwin		struct vattr *a_vap;
402179530Sjkim		struct ucred *a_cred;
40319872Swollman		struct proc *a_p;
40419872Swollman	} */ *ap;
40519872Swollman{
406198350Sedwin	struct vnode *vp = ap->a_vp;
407198350Sedwin	struct vattr *vap = ap->a_vap;
40819872Swollman
409179530Sjkim  	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
410298033Saraujo	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
411179530Sjkim	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
412227934Sfjoe	    (vp->v_mount->mnt_flag & MNT_RDONLY))
41319872Swollman		return (EROFS);
41419872Swollman	if (vap->va_size != VNOVAL) {
41519872Swollman 		switch (vp->v_type) {
41619872Swollman 		case VDIR:
41756487Scharnier 			return (EISDIR);
41856487Scharnier 		case VCHR:
41919872Swollman 		case VBLK:
42056487Scharnier 		case VSOCK:
42156487Scharnier 		case VFIFO:
42219872Swollman			return (0);
42319872Swollman		case VREG:
42419872Swollman		case VLNK:
42519872Swollman 		default:
42619872Swollman			/*
427198350Sedwin			 * Disallow write attempts if the filesystem is
428198350Sedwin			 * mounted read-only.
42919872Swollman			 */
430198350Sedwin			if (vp->v_mount->mnt_flag & MNT_RDONLY)
431198350Sedwin				return (EROFS);
43219872Swollman		}
43319872Swollman	}
43456487Scharnier	return (null_bypass((struct vop_generic_args *)ap));
43556487Scharnier}
43619872Swollman
43719872Swollman/*
43819872Swollman *  We handle getattr only to change the fsid.
43919872Swollman */
44019872Swollmanstatic int
44119872Swollmannull_getattr(ap)
44219872Swollman	struct vop_getattr_args /* {
44319872Swollman		struct vnode *a_vp;
44419872Swollman		struct vattr *a_vap;
44519872Swollman		struct ucred *a_cred;
44619872Swollman		struct proc *a_p;
44719872Swollman	} */ *ap;
44819872Swollman{
44919872Swollman	int error;
45019872Swollman
451179530Sjkim	if (error = null_bypass((struct vop_generic_args *)ap))
45219872Swollman		return (error);
453179530Sjkim	/* Requires that arguments be restored. */
45419872Swollman	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
455179530Sjkim	return (0);
45619872Swollman}
457179530Sjkim
45819872Swollmanstatic int
45919872Swollmannull_access(ap)
46019872Swollman	struct vop_access_args /* {
46119872Swollman		struct vnode *a_vp;
46219872Swollman		int  a_mode;
46319872Swollman		struct ucred *a_cred;
46419872Swollman		struct proc *a_p;
46519872Swollman	} */ *ap;
46619872Swollman{
467179530Sjkim	struct vnode *vp = ap->a_vp;
468179530Sjkim	mode_t mode = ap->a_mode;
46919872Swollman
47019872Swollman	/*
47119872Swollman	 * Disallow write attempts on read-only layers;
47219872Swollman	 * unless the file is a socket, fifo, or a block or
47319872Swollman	 * character device resident on the file system.
474179530Sjkim	 */
475179530Sjkim	if (mode & VWRITE) {
47619872Swollman		switch (vp->v_type) {
477179530Sjkim		case VDIR:
478281733Seadler		case VLNK:
479179530Sjkim		case VREG:
48019872Swollman			if (vp->v_mount->mnt_flag & MNT_RDONLY)
481198350Sedwin				return (EROFS);
48219872Swollman			break;
483209190Semaste		}
48419872Swollman	}
48519872Swollman	return (null_bypass((struct vop_generic_args *)ap));
486298033Saraujo}
48719872Swollman
48819872Swollman/*
489198350Sedwin * We need to process our own vnode lock and then clear the
49019872Swollman * interlock flag as it applies only to our vnode, not the
49119872Swollman * vnodes below us on the stack.
49219872Swollman */
49319872Swollmanstatic int
49419872Swollmannull_lock(ap)
49519872Swollman	struct vop_lock_args /* {
496198350Sedwin		struct vnode *a_vp;
497198350Sedwin		int a_flags;
498281733Seadler		struct proc *a_p;
49919872Swollman	} */ *ap;
50019872Swollman{
501298033Saraujo
502198350Sedwin	vop_nolock(ap);
503179530Sjkim	if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
50419872Swollman		return (0);
50519872Swollman	ap->a_flags &= ~LK_INTERLOCK;
50619872Swollman	return (null_bypass((struct vop_generic_args *)ap));
50719872Swollman}
508198350Sedwin
509179530Sjkim/*
51019872Swollman * We need to process our own vnode unlock and then clear the
511179530Sjkim * interlock flag as it applies only to our vnode, not the
51219872Swollman * vnodes below us on the stack.
51319872Swollman */
51419872Swollmanstatic int
51519872Swollmannull_unlock(ap)
51619872Swollman	struct vop_unlock_args /* {
51719872Swollman		struct vnode *a_vp;
51819872Swollman		int a_flags;
51919872Swollman		struct proc *a_p;
52019872Swollman	} */ *ap;
521179530Sjkim{
522179530Sjkim	struct vnode *vp = ap->a_vp;
52319872Swollman
524179530Sjkim	vop_nounlock(ap);
525179530Sjkim	ap->a_flags &= ~LK_INTERLOCK;
52619872Swollman	return (null_bypass((struct vop_generic_args *)ap));
52719872Swollman}
52819872Swollman
52919872Swollmanstatic int
53019872Swollmannull_inactive(ap)
53119872Swollman	struct vop_inactive_args /* {
53219872Swollman		struct vnode *a_vp;
53319872Swollman		struct proc *a_p;
53419872Swollman	} */ *ap;
53519872Swollman{
53619872Swollman	struct vnode *vp = ap->a_vp;
53719872Swollman	struct null_node *xp = VTONULL(vp);
53819872Swollman	struct vnode *lowervp = xp->null_lowervp;
53970486Sben	/*
54019872Swollman	 * Do nothing (and _don't_ bypass).
54170486Sben	 * Wait to vrele lowervp until reclaim,
542179530Sjkim	 * so that until then our null_node is in the
543179530Sjkim	 * cache and reusable.
54419872Swollman	 * We still have to tell the lower layer the vnode
54519872Swollman	 * is now inactive though.
54619872Swollman	 *
54719872Swollman	 * NEEDSWORK: Someday, consider inactive'ing
54819872Swollman	 * the lowervp and then trying to reactivate it
54919872Swollman	 * with capabilities (v_id)
55019872Swollman	 * like they do in the name lookup cache code.
55119872Swollman	 * That's too much work for now.
552179530Sjkim	 */
553179530Sjkim	VOP_INACTIVE(lowervp, ap->a_p);
554179530Sjkim	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
55519872Swollman	return (0);
556179530Sjkim}
55719872Swollman
55819872Swollmanstatic int
559179530Sjkimnull_reclaim(ap)
560179530Sjkim	struct vop_reclaim_args /* {
561298033Saraujo		struct vnode *a_vp;
56256487Scharnier		struct proc *a_p;
56319872Swollman	} */ *ap;
564179530Sjkim{
565179530Sjkim	struct vnode *vp = ap->a_vp;
566179530Sjkim	struct null_node *xp = VTONULL(vp);
567179530Sjkim	struct vnode *lowervp = xp->null_lowervp;
56819872Swollman
56919872Swollman	/*
57019872Swollman	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
57119872Swollman	 * so we can't call VOPs on ourself.
57219872Swollman	 */
57319872Swollman	/* After this assignment, this node will not be re-used. */
57419872Swollman	xp->null_lowervp = NULLVP;
57519872Swollman	LIST_REMOVE(xp, null_hash);
57619872Swollman	FREE(vp->v_data, M_TEMP);
57719872Swollman	vp->v_data = NULL;
57819872Swollman	vrele (lowervp);
57919872Swollman	return (0);
580179530Sjkim}
581179530Sjkim
58219872Swollmanstatic int
58319872Swollmannull_print(ap)
58419872Swollman	struct vop_print_args /* {
58519872Swollman		struct vnode *a_vp;
586179530Sjkim	} */ *ap;
58719872Swollman{
58856487Scharnier	register struct vnode *vp = ap->a_vp;
58919872Swollman	printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
59070486Sben	return (0);
59119872Swollman}
59219872Swollman
593179530Sjkim/*
594179530Sjkim * XXX - vop_strategy must be hand coded because it has no
59519872Swollman * vnode in its arguments.
59619872Swollman * This goes away with a merged VM/buffer cache.
59719872Swollman */
59819872Swollmanstatic int
59970486Sbennull_strategy(ap)
600179530Sjkim	struct vop_strategy_args /* {
601179530Sjkim		struct buf *a_bp;
60219872Swollman	} */ *ap;
60319872Swollman{
60419872Swollman	struct buf *bp = ap->a_bp;
60519872Swollman	int error;
60619872Swollman	struct vnode *savedvp;
607179530Sjkim
60819872Swollman	savedvp = bp->b_vp;
60919872Swollman	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
61019872Swollman
61119872Swollman	error = VOP_STRATEGY(bp);
61219872Swollman
61319872Swollman	bp->b_vp = savedvp;
61419872Swollman
61519872Swollman	return (error);
61619872Swollman}
61719872Swollman
61819872Swollman/*
61919872Swollman * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
620179530Sjkim * vnode in its arguments.
621179530Sjkim * This goes away with a merged VM/buffer cache.
622179530Sjkim */
623179530Sjkimstatic int
62419872Swollmannull_bwrite(ap)
625179530Sjkim	struct vop_bwrite_args /* {
626179530Sjkim		struct buf *a_bp;
627179530Sjkim	} */ *ap;
62819872Swollman{
629227934Sfjoe	struct buf *bp = ap->a_bp;
630227934Sfjoe	int error;
63119872Swollman	struct vnode *savedvp;
632179530Sjkim
633179530Sjkim	savedvp = bp->b_vp;
63419872Swollman	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
63519872Swollman
636220172Sedwin	error = VOP_BWRITE(bp);
637220172Sedwin
638220172Sedwin	bp->b_vp = savedvp;
639220172Sedwin
640220172Sedwin	return (error);
641227934Sfjoe}
642220172Sedwin
643220172Sedwin/*
644220172Sedwin * Global vfs data structures
64519872Swollman */
646198350Sedwinvop_t **null_vnodeop_p;
64719872Swollmanstatic struct vnodeopv_entry_desc null_vnodeop_entries[] = {
648179530Sjkim	{ &vop_default_desc,		(vop_t *) null_bypass },
649230005Swollman	{ &vop_access_desc,		(vop_t *) null_access },
650179530Sjkim	{ &vop_bwrite_desc,		(vop_t *) null_bwrite },
651179530Sjkim	{ &vop_getattr_desc,		(vop_t *) null_getattr },
652179530Sjkim	{ &vop_inactive_desc,		(vop_t *) null_inactive },
65319872Swollman	{ &vop_lock_desc,		(vop_t *) null_lock },
654198350Sedwin	{ &vop_lookup_desc,		(vop_t *) null_lookup },
65519872Swollman	{ &vop_print_desc,		(vop_t *) null_print },
65619872Swollman	{ &vop_reclaim_desc,		(vop_t *) null_reclaim },
657179530Sjkim	{ &vop_setattr_desc,		(vop_t *) null_setattr },
65819872Swollman	{ &vop_strategy_desc,		(vop_t *) null_strategy },
65919872Swollman	{ &vop_unlock_desc,		(vop_t *) null_unlock },
66019872Swollman	{ NULL, NULL }
66119872Swollman};
66221915Sjkhstatic struct vnodeopv_desc null_vnodeop_opv_desc =
663230299Semaste	{ &null_vnodeop_p, null_vnodeop_entries };
664230299Semaste
665179530SjkimVNODEOP_SET(null_vnodeop_opv_desc);
666230299Semaste