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