null_vnops.c revision 72200
1/*
2 * Copyright (c) 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * John Heidemann of the UCLA Ficus project.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
37 *
38 * Ancestors:
39 *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
40 *	...and...
41 *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
42 *
43 * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 72200 2001-02-09 06:11:45Z bmilekic $
44 */
45
46/*
47 * Null Layer
48 *
49 * (See mount_null(8) for more information.)
50 *
51 * The null layer duplicates a portion of the file system
52 * name space under a new name.  In this respect, it is
53 * similar to the loopback file system.  It differs from
54 * the loopback fs in two respects:  it is implemented using
55 * a stackable layers techniques, and its "null-node"s stack above
56 * all lower-layer vnodes, not just over directory vnodes.
57 *
58 * The null layer has two purposes.  First, it serves as a demonstration
59 * of layering by proving a layer which does nothing.  (It actually
60 * does everything the loopback file system does, which is slightly
61 * more than nothing.)  Second, the null layer can serve as a prototype
62 * layer.  Since it provides all necessary layer framework,
63 * new file system layers can be created very easily be starting
64 * with a null layer.
65 *
66 * The remainder of this man page examines the null layer as a basis
67 * for constructing new layers.
68 *
69 *
70 * INSTANTIATING NEW NULL LAYERS
71 *
72 * New null layers are created with mount_null(8).
73 * Mount_null(8) takes two arguments, the pathname
74 * of the lower vfs (target-pn) and the pathname where the null
75 * layer will appear in the namespace (alias-pn).  After
76 * the null layer is put into place, the contents
77 * of target-pn subtree will be aliased under alias-pn.
78 *
79 *
80 * OPERATION OF A NULL LAYER
81 *
82 * The null layer is the minimum file system layer,
83 * simply bypassing all possible operations to the lower layer
84 * for processing there.  The majority of its activity centers
85 * on the bypass routine, through which nearly all vnode operations
86 * pass.
87 *
88 * The bypass routine accepts arbitrary vnode operations for
89 * handling by the lower layer.  It begins by examing vnode
90 * operation arguments and replacing any null-nodes by their
91 * lower-layer equivlants.  It then invokes the operation
92 * on the lower layer.  Finally, it replaces the null-nodes
93 * in the arguments and, if a vnode is return by the operation,
94 * stacks a null-node on top of the returned vnode.
95 *
96 * Although bypass handles most operations, vop_getattr, vop_lock,
97 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
98 * bypassed. Vop_getattr must change the fsid being returned.
99 * Vop_lock and vop_unlock must handle any locking for the
100 * current vnode as well as pass the lock request down.
101 * Vop_inactive and vop_reclaim are not bypassed so that
102 * they can handle freeing null-layer specific data. Vop_print
103 * is not bypassed to avoid excessive debugging information.
104 * Also, certain vnode operations change the locking state within
105 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
106 * and symlink). Ideally these operations should not change the
107 * lock state, but should be changed to let the caller of the
108 * function unlock them. Otherwise all intermediate vnode layers
109 * (such as union, umapfs, etc) must catch these functions to do
110 * the necessary locking at their layer.
111 *
112 *
113 * INSTANTIATING VNODE STACKS
114 *
115 * Mounting associates the null layer with a lower layer,
116 * effect stacking two VFSes.  Vnode stacks are instead
117 * created on demand as files are accessed.
118 *
119 * The initial mount creates a single vnode stack for the
120 * root of the new null layer.  All other vnode stacks
121 * are created as a result of vnode operations on
122 * this or other null vnode stacks.
123 *
124 * New vnode stacks come into existance as a result of
125 * an operation which returns a vnode.
126 * The bypass routine stacks a null-node above the new
127 * vnode before returning it to the caller.
128 *
129 * For example, imagine mounting a null layer with
130 * "mount_null /usr/include /dev/layer/null".
131 * Changing directory to /dev/layer/null will assign
132 * the root null-node (which was created when the null layer was mounted).
133 * Now consider opening "sys".  A vop_lookup would be
134 * done on the root null-node.  This operation would bypass through
135 * to the lower layer which would return a vnode representing
136 * the UFS "sys".  Null_bypass then builds a null-node
137 * aliasing the UFS "sys" and returns this to the caller.
138 * Later operations on the null-node "sys" will repeat this
139 * process when constructing other vnode stacks.
140 *
141 *
142 * CREATING OTHER FILE SYSTEM LAYERS
143 *
144 * One of the easiest ways to construct new file system layers is to make
145 * a copy of the null layer, rename all files and variables, and
146 * then begin modifing the copy.  Sed can be used to easily rename
147 * all variables.
148 *
149 * The umap layer is an example of a layer descended from the
150 * null layer.
151 *
152 *
153 * INVOKING OPERATIONS ON LOWER LAYERS
154 *
155 * There are two techniques to invoke operations on a lower layer
156 * when the operation cannot be completely bypassed.  Each method
157 * is appropriate in different situations.  In both cases,
158 * it is the responsibility of the aliasing layer to make
159 * the operation arguments "correct" for the lower layer
160 * by mapping an vnode arguments to the lower layer.
161 *
162 * The first approach is to call the aliasing layer's bypass routine.
163 * This method is most suitable when you wish to invoke the operation
164 * currently being handled on the lower layer.  It has the advantage
165 * that the bypass routine already must do argument mapping.
166 * An example of this is null_getattrs in the null layer.
167 *
168 * A second approach is to directly invoke vnode operations on
169 * the lower layer with the VOP_OPERATIONNAME interface.
170 * The advantage of this method is that it is easy to invoke
171 * arbitrary operations on the lower layer.  The disadvantage
172 * is that vnode arguments must be manualy mapped.
173 *
174 */
175
176#include <sys/param.h>
177#include <sys/systm.h>
178#include <sys/kernel.h>
179#include <sys/conf.h>
180#include <sys/sysctl.h>
181#include <sys/vnode.h>
182#include <sys/mount.h>
183#include <sys/namei.h>
184#include <sys/malloc.h>
185#include <miscfs/nullfs/null.h>
186
187#include <vm/vm.h>
188#include <vm/vm_extern.h>
189#include <vm/vm_object.h>
190#include <vm/vnode_pager.h>
191
192static int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
193SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
194	&null_bug_bypass, 0, "");
195
196static int	null_access(struct vop_access_args *ap);
197static int	null_createvobject(struct vop_createvobject_args *ap);
198static int	null_destroyvobject(struct vop_destroyvobject_args *ap);
199static int	null_getattr(struct vop_getattr_args *ap);
200static int	null_getvobject(struct vop_getvobject_args *ap);
201static int	null_inactive(struct vop_inactive_args *ap);
202static int	null_islocked(struct vop_islocked_args *ap);
203static int	null_lock(struct vop_lock_args *ap);
204static int	null_lookup(struct vop_lookup_args *ap);
205static int	null_open(struct vop_open_args *ap);
206static int	null_print(struct vop_print_args *ap);
207static int	null_reclaim(struct vop_reclaim_args *ap);
208static int	null_rename(struct vop_rename_args *ap);
209static int	null_setattr(struct vop_setattr_args *ap);
210static int	null_unlock(struct vop_unlock_args *ap);
211
212/*
213 * This is the 10-Apr-92 bypass routine.
214 *    This version has been optimized for speed, throwing away some
215 * safety checks.  It should still always work, but it's not as
216 * robust to programmer errors.
217 *
218 * In general, we map all vnodes going down and unmap them on the way back.
219 * As an exception to this, vnodes can be marked "unmapped" by setting
220 * the Nth bit in operation's vdesc_flags.
221 *
222 * Also, some BSD vnode operations have the side effect of vrele'ing
223 * their arguments.  With stacking, the reference counts are held
224 * by the upper node, not the lower one, so we must handle these
225 * side-effects here.  This is not of concern in Sun-derived systems
226 * since there are no such side-effects.
227 *
228 * This makes the following assumptions:
229 * - only one returned vpp
230 * - no INOUT vpp's (Sun's vop_open has one of these)
231 * - the vnode operation vector of the first vnode should be used
232 *   to determine what implementation of the op should be invoked
233 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
234 *   problems on rmdir'ing mount points and renaming?)
235 */
236int
237null_bypass(ap)
238	struct vop_generic_args /* {
239		struct vnodeop_desc *a_desc;
240		<other random data follows, presumably>
241	} */ *ap;
242{
243	register struct vnode **this_vp_p;
244	int error;
245	struct vnode *old_vps[VDESC_MAX_VPS];
246	struct vnode **vps_p[VDESC_MAX_VPS];
247	struct vnode ***vppp;
248	struct vnodeop_desc *descp = ap->a_desc;
249	int reles, i;
250
251	if (null_bug_bypass)
252		printf ("null_bypass: %s\n", descp->vdesc_name);
253
254#ifdef DIAGNOSTIC
255	/*
256	 * We require at least one vp.
257	 */
258	if (descp->vdesc_vp_offsets == NULL ||
259	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
260		panic ("null_bypass: no vp's in map");
261#endif
262
263	/*
264	 * Map the vnodes going in.
265	 * Later, we'll invoke the operation based on
266	 * the first mapped vnode's operation vector.
267	 */
268	reles = descp->vdesc_flags;
269	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
270		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
271			break;   /* bail out at end of list */
272		vps_p[i] = this_vp_p =
273			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
274		/*
275		 * We're not guaranteed that any but the first vnode
276		 * are of our type.  Check for and don't map any
277		 * that aren't.  (We must always map first vp or vclean fails.)
278		 */
279		if (i && (*this_vp_p == NULLVP ||
280		    (*this_vp_p)->v_op != null_vnodeop_p)) {
281			old_vps[i] = NULLVP;
282		} else {
283			old_vps[i] = *this_vp_p;
284			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
285			/*
286			 * XXX - Several operations have the side effect
287			 * of vrele'ing their vp's.  We must account for
288			 * that.  (This should go away in the future.)
289			 */
290			if (reles & VDESC_VP0_WILLRELE)
291				VREF(*this_vp_p);
292		}
293
294	}
295
296	/*
297	 * Call the operation on the lower layer
298	 * with the modified argument structure.
299	 */
300	if (vps_p[0] && *vps_p[0])
301		error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
302	else {
303		printf("null_bypass: no map for %s\n", descp->vdesc_name);
304		error = EINVAL;
305	}
306
307	/*
308	 * Maintain the illusion of call-by-value
309	 * by restoring vnodes in the argument structure
310	 * to their original value.
311	 */
312	reles = descp->vdesc_flags;
313	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
314		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
315			break;   /* bail out at end of list */
316		if (old_vps[i]) {
317			*(vps_p[i]) = old_vps[i];
318#if 0
319			if (reles & VDESC_VP0_WILLUNLOCK)
320				VOP_UNLOCK(*(vps_p[i]), LK_THISLAYER, curproc);
321#endif
322			if (reles & VDESC_VP0_WILLRELE)
323				vrele(*(vps_p[i]));
324		}
325	}
326
327	/*
328	 * Map the possible out-going vpp
329	 * (Assumes that the lower layer always returns
330	 * a VREF'ed vpp unless it gets an error.)
331	 */
332	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
333	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
334	    !error) {
335		/*
336		 * XXX - even though some ops have vpp returned vp's,
337		 * several ops actually vrele this before returning.
338		 * We must avoid these ops.
339		 * (This should go away when these ops are regularized.)
340		 */
341		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
342			goto out;
343		vppp = VOPARG_OFFSETTO(struct vnode***,
344				 descp->vdesc_vpp_offset,ap);
345		if (*vppp)
346			error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
347	}
348
349 out:
350	return (error);
351}
352
353/*
354 * We have to carry on the locking protocol on the null layer vnodes
355 * as we progress through the tree. We also have to enforce read-only
356 * if this layer is mounted read-only.
357 */
358static int
359null_lookup(ap)
360	struct vop_lookup_args /* {
361		struct vnode * a_dvp;
362		struct vnode ** a_vpp;
363		struct componentname * a_cnp;
364	} */ *ap;
365{
366	struct componentname *cnp = ap->a_cnp;
367	struct vnode *dvp = ap->a_dvp;
368	struct proc *p = cnp->cn_proc;
369	int flags = cnp->cn_flags;
370	struct vnode *vp, *ldvp, *lvp;
371	int error;
372
373	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
374	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
375		return (EROFS);
376	/*
377	 * Although it is possible to call null_bypass(), we'll do
378	 * a direct call to reduce overhead
379	 */
380	ldvp = NULLVPTOLOWERVP(dvp);
381	vp = lvp = NULL;
382	error = VOP_LOOKUP(ldvp, &lvp, cnp);
383	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
384	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
385	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
386		error = EROFS;
387
388	/*
389	 * Rely only on the PDIRUNLOCK flag which should be carefully
390	 * tracked by underlying filesystem.
391	 */
392	if (cnp->cn_flags & PDIRUNLOCK)
393		VOP_UNLOCK(dvp, LK_THISLAYER, p);
394	if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
395		if (ldvp == lvp) {
396			*ap->a_vpp = dvp;
397			VREF(dvp);
398			vrele(lvp);
399		} else {
400			error = null_node_create(dvp->v_mount, lvp, &vp);
401			if (error == 0)
402				*ap->a_vpp = vp;
403		}
404	}
405	return (error);
406}
407
408/*
409 * Setattr call. Disallow write attempts if the layer is mounted read-only.
410 */
411int
412null_setattr(ap)
413	struct vop_setattr_args /* {
414		struct vnodeop_desc *a_desc;
415		struct vnode *a_vp;
416		struct vattr *a_vap;
417		struct ucred *a_cred;
418		struct proc *a_p;
419	} */ *ap;
420{
421	struct vnode *vp = ap->a_vp;
422	struct vattr *vap = ap->a_vap;
423
424  	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
425	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
426	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
427	    (vp->v_mount->mnt_flag & MNT_RDONLY))
428		return (EROFS);
429	if (vap->va_size != VNOVAL) {
430 		switch (vp->v_type) {
431 		case VDIR:
432 			return (EISDIR);
433 		case VCHR:
434 		case VBLK:
435 		case VSOCK:
436 		case VFIFO:
437			if (vap->va_flags != VNOVAL)
438				return (EOPNOTSUPP);
439			return (0);
440		case VREG:
441		case VLNK:
442 		default:
443			/*
444			 * Disallow write attempts if the filesystem is
445			 * mounted read-only.
446			 */
447			if (vp->v_mount->mnt_flag & MNT_RDONLY)
448				return (EROFS);
449		}
450	}
451
452	return (null_bypass((struct vop_generic_args *)ap));
453}
454
455/*
456 *  We handle getattr only to change the fsid.
457 */
458static int
459null_getattr(ap)
460	struct vop_getattr_args /* {
461		struct vnode *a_vp;
462		struct vattr *a_vap;
463		struct ucred *a_cred;
464		struct proc *a_p;
465	} */ *ap;
466{
467	int error;
468
469	if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
470		return (error);
471
472	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
473	return (0);
474}
475
476/*
477 * Handle to disallow write access if mounted read-only.
478 */
479static int
480null_access(ap)
481	struct vop_access_args /* {
482		struct vnode *a_vp;
483		int  a_mode;
484		struct ucred *a_cred;
485		struct proc *a_p;
486	} */ *ap;
487{
488	struct vnode *vp = ap->a_vp;
489	mode_t mode = ap->a_mode;
490
491	/*
492	 * Disallow write attempts on read-only layers;
493	 * unless the file is a socket, fifo, or a block or
494	 * character device resident on the file system.
495	 */
496	if (mode & VWRITE) {
497		switch (vp->v_type) {
498		case VDIR:
499		case VLNK:
500		case VREG:
501			if (vp->v_mount->mnt_flag & MNT_RDONLY)
502				return (EROFS);
503			break;
504		default:
505			break;
506		}
507	}
508	return (null_bypass((struct vop_generic_args *)ap));
509}
510
511/*
512 * We must handle open to be able to catch MNT_NODEV and friends.
513 */
514static int
515null_open(ap)
516	struct vop_open_args /* {
517		struct vnode *a_vp;
518		int  a_mode;
519		struct ucred *a_cred;
520		struct proc *a_p;
521	} */ *ap;
522{
523	struct vnode *vp = ap->a_vp;
524	struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
525
526	if ((vp->v_mount->mnt_flag & MNT_NODEV) &&
527	    (lvp->v_type == VBLK || lvp->v_type == VCHR))
528		return ENXIO;
529
530	return (null_bypass((struct vop_generic_args *)ap));
531}
532
533/*
534 * We handle this to eliminate null FS to lower FS
535 * file moving. Don't know why we don't allow this,
536 * possibly we should.
537 */
538static int
539null_rename(ap)
540	struct vop_rename_args /* {
541		struct vnode *a_fdvp;
542		struct vnode *a_fvp;
543		struct componentname *a_fcnp;
544		struct vnode *a_tdvp;
545		struct vnode *a_tvp;
546		struct componentname *a_tcnp;
547	} */ *ap;
548{
549	struct vnode *tdvp = ap->a_tdvp;
550	struct vnode *fvp = ap->a_fvp;
551	struct vnode *fdvp = ap->a_fdvp;
552	struct vnode *tvp = ap->a_tvp;
553
554	/* Check for cross-device rename. */
555	if ((fvp->v_mount != tdvp->v_mount) ||
556	    (tvp && (fvp->v_mount != tvp->v_mount))) {
557		if (tdvp == tvp)
558			vrele(tdvp);
559		else
560			vput(tdvp);
561		if (tvp)
562			vput(tvp);
563		vrele(fdvp);
564		vrele(fvp);
565		return (EXDEV);
566	}
567
568	return (null_bypass((struct vop_generic_args *)ap));
569}
570
571/*
572 * We need to process our own vnode lock and then clear the
573 * interlock flag as it applies only to our vnode, not the
574 * vnodes below us on the stack.
575 */
576static int
577null_lock(ap)
578	struct vop_lock_args /* {
579		struct vnode *a_vp;
580		int a_flags;
581		struct proc *a_p;
582	} */ *ap;
583{
584	struct vnode *vp = ap->a_vp;
585	int flags = ap->a_flags;
586	struct proc *p = ap->a_p;
587	struct vnode *lvp;
588	int error;
589
590	if (flags & LK_THISLAYER) {
591		if (vp->v_vnlock != NULL)
592			return 0;	/* lock is shared across layers */
593		error = lockmgr(&vp->v_lock, flags & ~LK_THISLAYER,
594		    &vp->v_interlock, p);
595		return (error);
596	}
597
598	if (vp->v_vnlock != NULL) {
599		/*
600		 * The lower level has exported a struct lock to us. Use
601		 * it so that all vnodes in the stack lock and unlock
602		 * simultaneously. Note: we don't DRAIN the lock as DRAIN
603		 * decommissions the lock - just because our vnode is
604		 * going away doesn't mean the struct lock below us is.
605		 * LK_EXCLUSIVE is fine.
606		 */
607		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
608			NULLFSDEBUG("null_lock: avoiding LK_DRAIN\n");
609			return(lockmgr(vp->v_vnlock,
610				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
611				&vp->v_interlock, p));
612		}
613		return(lockmgr(vp->v_vnlock, flags, &vp->v_interlock, p));
614	} else {
615		/*
616		 * To prevent race conditions involving doing a lookup
617		 * on "..", we have to lock the lower node, then lock our
618		 * node. Most of the time it won't matter that we lock our
619		 * node (as any locking would need the lower one locked
620		 * first). But we can LK_DRAIN the upper lock as a step
621		 * towards decomissioning it.
622		 */
623		lvp = NULLVPTOLOWERVP(vp);
624		if (lvp == NULL)
625			return (lockmgr(&vp->v_lock, flags, &vp->v_interlock, p));
626		if (flags & LK_INTERLOCK) {
627			mtx_unlock(&vp->v_interlock);
628			flags &= ~LK_INTERLOCK;
629		}
630		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
631			error = VOP_LOCK(lvp,
632				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE, p);
633		} else
634			error = VOP_LOCK(lvp, flags, p);
635		if (error)
636			return (error);
637		error = lockmgr(&vp->v_lock, flags, &vp->v_interlock, p);
638		if (error)
639			VOP_UNLOCK(lvp, 0, p);
640		return (error);
641	}
642}
643
644/*
645 * We need to process our own vnode unlock and then clear the
646 * interlock flag as it applies only to our vnode, not the
647 * vnodes below us on the stack.
648 */
649static int
650null_unlock(ap)
651	struct vop_unlock_args /* {
652		struct vnode *a_vp;
653		int a_flags;
654		struct proc *a_p;
655	} */ *ap;
656{
657	struct vnode *vp = ap->a_vp;
658	int flags = ap->a_flags;
659	struct proc *p = ap->a_p;
660	struct vnode *lvp;
661
662	if (vp->v_vnlock != NULL) {
663		if (flags & LK_THISLAYER)
664			return 0;	/* the lock is shared across layers */
665		flags &= ~LK_THISLAYER;
666		return (lockmgr(vp->v_vnlock, flags | LK_RELEASE,
667			&vp->v_interlock, p));
668	}
669	lvp = NULLVPTOLOWERVP(vp);
670	if (lvp == NULL)
671		return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, p));
672	if ((flags & LK_THISLAYER) == 0) {
673		if (flags & LK_INTERLOCK) {
674			mtx_unlock(&vp->v_interlock);
675			flags &= ~LK_INTERLOCK;
676		}
677		VOP_UNLOCK(lvp, flags & ~LK_INTERLOCK, p);
678	} else
679		flags &= ~LK_THISLAYER;
680	return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, p));
681}
682
683static int
684null_islocked(ap)
685	struct vop_islocked_args /* {
686		struct vnode *a_vp;
687		struct proc *a_p;
688	} */ *ap;
689{
690	struct vnode *vp = ap->a_vp;
691	struct proc *p = ap->a_p;
692
693	if (vp->v_vnlock != NULL)
694		return (lockstatus(vp->v_vnlock, p));
695	return (lockstatus(&vp->v_lock, p));
696}
697
698/*
699 * There is no way to tell that someone issued remove/rmdir operation
700 * on the underlying filesystem. For now we just have to release lowevrp
701 * as soon as possible.
702 */
703static int
704null_inactive(ap)
705	struct vop_inactive_args /* {
706		struct vnode *a_vp;
707		struct proc *a_p;
708	} */ *ap;
709{
710	struct vnode *vp = ap->a_vp;
711	struct proc *p = ap->a_p;
712	struct null_node *xp = VTONULL(vp);
713	struct vnode *lowervp = xp->null_lowervp;
714
715	lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, p);
716	LIST_REMOVE(xp, null_hash);
717	lockmgr(&null_hashlock, LK_RELEASE, NULL, p);
718
719	xp->null_lowervp = NULLVP;
720	if (vp->v_vnlock != NULL) {
721		vp->v_vnlock = &vp->v_lock;	/* we no longer share the lock */
722	} else
723		VOP_UNLOCK(vp, LK_THISLAYER, p);
724
725	vput(lowervp);
726	/*
727	 * Now it is safe to drop references to the lower vnode.
728	 * VOP_INACTIVE() will be called by vrele() if necessary.
729	 */
730	vrele (lowervp);
731
732	return (0);
733}
734
735/*
736 * We can free memory in null_inactive, but we do this
737 * here. (Possible to guard vp->v_data to point somewhere)
738 */
739static int
740null_reclaim(ap)
741	struct vop_reclaim_args /* {
742		struct vnode *a_vp;
743		struct proc *a_p;
744	} */ *ap;
745{
746	struct vnode *vp = ap->a_vp;
747	void *vdata = vp->v_data;
748
749	vp->v_data = NULL;
750	FREE(vdata, M_NULLFSNODE);
751
752	return (0);
753}
754
755static int
756null_print(ap)
757	struct vop_print_args /* {
758		struct vnode *a_vp;
759	} */ *ap;
760{
761	register struct vnode *vp = ap->a_vp;
762	printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
763	return (0);
764}
765
766/*
767 * Let an underlying filesystem do the work
768 */
769static int
770null_createvobject(ap)
771	struct vop_createvobject_args /* {
772		struct vnode *vp;
773		struct ucred *cred;
774		struct proc *p;
775	} */ *ap;
776{
777	struct vnode *vp = ap->a_vp;
778	struct vnode *lowervp = VTONULL(vp) ? NULLVPTOLOWERVP(vp) : NULL;
779	int error;
780
781	if (vp->v_type == VNON || lowervp == NULL)
782		return 0;
783	error = VOP_CREATEVOBJECT(lowervp, ap->a_cred, ap->a_p);
784	if (error)
785		return (error);
786	vp->v_flag |= VOBJBUF;
787	return (0);
788}
789
790/*
791 * We have nothing to destroy and this operation shouldn't be bypassed.
792 */
793static int
794null_destroyvobject(ap)
795	struct vop_destroyvobject_args /* {
796		struct vnode *vp;
797	} */ *ap;
798{
799	struct vnode *vp = ap->a_vp;
800
801	vp->v_flag &= ~VOBJBUF;
802	return (0);
803}
804
805static int
806null_getvobject(ap)
807	struct vop_getvobject_args /* {
808		struct vnode *vp;
809		struct vm_object **objpp;
810	} */ *ap;
811{
812	struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
813
814	if (lvp == NULL)
815		return EINVAL;
816	return (VOP_GETVOBJECT(lvp, ap->a_objpp));
817}
818
819/*
820 * Global vfs data structures
821 */
822vop_t **null_vnodeop_p;
823static struct vnodeopv_entry_desc null_vnodeop_entries[] = {
824	{ &vop_default_desc,		(vop_t *) null_bypass },
825
826	{ &vop_access_desc,		(vop_t *) null_access },
827	{ &vop_bmap_desc,		(vop_t *) vop_eopnotsupp },
828	{ &vop_createvobject_desc,	(vop_t *) null_createvobject },
829	{ &vop_destroyvobject_desc,	(vop_t *) null_destroyvobject },
830	{ &vop_getattr_desc,		(vop_t *) null_getattr },
831	{ &vop_getvobject_desc,		(vop_t *) null_getvobject },
832	{ &vop_getwritemount_desc,	(vop_t *) vop_stdgetwritemount},
833	{ &vop_inactive_desc,		(vop_t *) null_inactive },
834	{ &vop_islocked_desc,		(vop_t *) null_islocked },
835	{ &vop_lock_desc,		(vop_t *) null_lock },
836	{ &vop_lookup_desc,		(vop_t *) null_lookup },
837	{ &vop_open_desc,		(vop_t *) null_open },
838	{ &vop_print_desc,		(vop_t *) null_print },
839	{ &vop_reclaim_desc,		(vop_t *) null_reclaim },
840	{ &vop_rename_desc,		(vop_t *) null_rename },
841	{ &vop_setattr_desc,		(vop_t *) null_setattr },
842	{ &vop_strategy_desc,		(vop_t *) vop_eopnotsupp },
843	{ &vop_unlock_desc,		(vop_t *) null_unlock },
844	{ NULL, NULL }
845};
846static struct vnodeopv_desc null_vnodeop_opv_desc =
847	{ &null_vnodeop_p, null_vnodeop_entries };
848
849VNODEOP_SET(null_vnodeop_opv_desc);
850