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