xref: /dragonfly/sys/vfs/nullfs/null_vnops.c (revision 43b4d1bd)
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.15 2004/08/28 21:32:28 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/proc.h>
185 #include <sys/namei.h>
186 #include <sys/malloc.h>
187 #include <sys/buf.h>
188 #include "null.h"
189 
190 static int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
191 SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
192 	&null_bug_bypass, 0, "");
193 
194 static int	null_access(struct vop_access_args *ap);
195 static int	null_createvobject(struct vop_createvobject_args *ap);
196 static int	null_destroyvobject(struct vop_destroyvobject_args *ap);
197 static int	null_getattr(struct vop_getattr_args *ap);
198 static int	null_getvobject(struct vop_getvobject_args *ap);
199 static int	null_inactive(struct vop_inactive_args *ap);
200 static int	null_islocked(struct vop_islocked_args *ap);
201 static int	null_lock(struct vop_lock_args *ap);
202 static int	null_lookup(struct vop_lookup_args *ap);
203 static int	null_open(struct vop_open_args *ap);
204 static int	null_print(struct vop_print_args *ap);
205 static int	null_reclaim(struct vop_reclaim_args *ap);
206 static int	null_rename(struct vop_rename_args *ap);
207 static int	null_setattr(struct vop_setattr_args *ap);
208 static int	null_unlock(struct vop_unlock_args *ap);
209 
210 /*
211  * This is the 10-Apr-92 bypass routine.
212  *    This version has been optimized for speed, throwing away some
213  * safety checks.  It should still always work, but it's not as
214  * robust to programmer errors.
215  *
216  * In general, we map all vnodes going down and unmap them on the way back.
217  * As an exception to this, vnodes can be marked "unmapped" by setting
218  * the Nth bit in operation's vdesc_flags.
219  *
220  * Also, some BSD vnode operations have the side effect of vrele'ing
221  * their arguments.  With stacking, the reference counts are held
222  * by the upper node, not the lower one, so we must handle these
223  * side-effects here.  This is not of concern in Sun-derived systems
224  * since there are no such side-effects.
225  *
226  * This makes the following assumptions:
227  * - only one returned vpp
228  * - no INOUT vpp's (Sun's vop_open has one of these)
229  * - the vnode operation vector of the first vnode should be used
230  *   to determine what implementation of the op should be invoked
231  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
232  *   problems on rmdir'ing mount points and renaming?)
233  *
234  * null_bypass(struct vnodeop_desc *a_desc, ...)
235  */
236 int
237 null_bypass(struct vop_generic_args *ap)
238 {
239 	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, j;
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 	 */
262 	reles = descp->vdesc_flags;
263 	for (i = 0; i < VDESC_MAX_VPS; ++i) {
264 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
265 			break;   /* bail out at end of list */
266 		vps_p[i] = this_vp_p =
267 			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
268 		/*
269 		 * We're not guaranteed that any but the first vnode
270 		 * are of our type.  Check for and don't map any
271 		 * that aren't.  (We must always map first vp or vclean fails.)
272 		 */
273 		if (i && (*this_vp_p == NULLVP ||
274 		    (*this_vp_p)->v_tag != VT_NULL)) {
275 			old_vps[i] = NULLVP;
276 		} else {
277 			old_vps[i] = *this_vp_p;
278 			*this_vp_p = NULLVPTOLOWERVP(*this_vp_p);
279 			/*
280 			 * Several operations have the side effect of vrele'ing
281 			 * their vp's.  We must account for that in the lower
282 			 * vp we pass down.
283 			 */
284 			if (reles & (VDESC_VP0_WILLRELE << i))
285 				vref(*this_vp_p);
286 		}
287 
288 	}
289 
290 	/*
291 	 * Call the operation on the lower layer with the modified
292 	 * argument structure.  We have to adjust a_fm to point to the
293 	 * lower vp's vop_ops structure.
294 	 */
295 	if (vps_p[0] && *vps_p[0]) {
296 		ap->a_ops = (*(vps_p[0]))->v_ops;
297 		error = vop_vnoperate_ap(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 by restoring vnodes in the
305 	 * argument structure to their original value.
306 	 */
307 	reles = descp->vdesc_flags;
308 	for (i = 0; i < VDESC_MAX_VPS; ++i) {
309 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
310 			break;   /* bail out at end of list */
311 		if (old_vps[i]) {
312 			*(vps_p[i]) = old_vps[i];
313 
314 			/*
315 			 * Since we operated on the lowervp's instead of the
316 			 * null node vp's, we have to adjust the null node
317 			 * vp's based on what the VOP did to the lower vp.
318 			 *
319 			 * Note: the unlock case only occurs with rename.
320 			 * tdvp and tvp are both locked on call and must be
321 			 * unlocked on return.
322 			 *
323 			 * Unlock semantics indicate that if two locked vp's
324 			 * are passed and they are the same vp, they are only
325 			 * actually locked once.
326 			 */
327 			if (reles & (VDESC_VP0_WILLUNLOCK << i)) {
328 				VOP_UNLOCK(old_vps[i], NULL,
329 					    LK_THISLAYER, curthread);
330 				for (j = i + 1; j < VDESC_MAX_VPS; ++j) {
331 					if (descp->vdesc_vp_offsets[j] == VDESC_NO_OFFSET)
332 						break;
333 					if (old_vps[i] == old_vps[j]) {
334 						reles &= ~(1 << (VDESC_VP0_WILLUNLOCK << j));
335 					}
336 				}
337 			}
338 
339 			if (reles & (VDESC_VP0_WILLRELE << i))
340 				vrele(old_vps[i]);
341 		}
342 	}
343 
344 	/*
345 	 * Map the possible out-going vpp
346 	 * (Assumes that the lower layer always returns
347 	 * a vref'ed vpp unless it gets an error.)
348 	 */
349 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
350 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
351 	    !error) {
352 		/*
353 		 * XXX - even though some ops have vpp returned vp's,
354 		 * several ops actually vrele this before returning.
355 		 * We must avoid these ops.
356 		 * (This should go away when these ops are regularized.)
357 		 */
358 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
359 			goto out;
360 		vppp = VOPARG_OFFSETTO(struct vnode***,
361 				 descp->vdesc_vpp_offset,ap);
362 		if (*vppp)
363 			error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
364 	}
365 
366  out:
367 	return (error);
368 }
369 
370 /*
371  * We have to carry on the locking protocol on the null layer vnodes
372  * as we progress through the tree. We also have to enforce read-only
373  * if this layer is mounted read-only.
374  *
375  * null_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
376  *		struct componentname *a_cnp)
377  */
378 static int
379 null_lookup(struct vop_lookup_args *ap)
380 {
381 	struct componentname *cnp = ap->a_cnp;
382 	struct vnode *dvp = ap->a_dvp;
383 	struct thread *td = cnp->cn_td;
384 	int flags = cnp->cn_flags;
385 	struct vnode *vp, *ldvp, *lvp;
386 	int error;
387 
388 	if ((flags & CNP_ISLASTCN) &&
389 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
390 	    (cnp->cn_nameiop == NAMEI_DELETE ||
391 	     cnp->cn_nameiop == NAMEI_RENAME)) {
392 		return (EROFS);
393 	}
394 	ldvp = NULLVPTOLOWERVP(dvp);
395 
396 	/*
397 	 * If we are doing a ".." lookup we must release the lock on dvp
398 	 * now, before we run a lookup in the underlying fs, or we may
399 	 * deadlock.  If we do this we must protect ldvp by ref'ing it.
400 	 */
401 	if (flags & CNP_ISDOTDOT) {
402 		vref(ldvp);
403 		VOP_UNLOCK(dvp, NULL, LK_THISLAYER, td);
404 	}
405 
406 	/*
407 	 * Due to the non-deterministic nature of the handling of the
408 	 * parent directory lock by lookup, we cannot call null_bypass()
409 	 * here.  We must make a direct call.  It's faster to do a direct
410 	 * call, anyway.
411 	 */
412 	vp = lvp = NULL;
413 	error = VOP_LOOKUP(ldvp, NCPNULL, &lvp, NCPPNULL, cnp);
414 	if (error == EJUSTRETURN && (flags & CNP_ISLASTCN) &&
415 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
416 	    (cnp->cn_nameiop == NAMEI_CREATE ||
417 	     cnp->cn_nameiop == NAMEI_RENAME)) {
418 		error = EROFS;
419 	}
420 
421 	if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
422 		if (ldvp == lvp) {
423 			*ap->a_vpp = dvp;
424 			vref(dvp);
425 			vrele(lvp);
426 		} else {
427 			error = null_node_create(dvp->v_mount, lvp, &vp);
428 			if (error == 0)
429 				*ap->a_vpp = vp;
430 		}
431 	}
432 
433 	/*
434 	 * The underlying fs will set PDIRUNLOCK if it unlocked the parent
435 	 * directory, which means we have to follow suit in the nullfs layer.
436 	 * Note that the parent directory may have already been unlocked due
437 	 * to the ".." case.  Note that use of cnp->cn_flags instead of flags.
438 	 */
439 	if (flags & CNP_ISDOTDOT) {
440 		if ((cnp->cn_flags & CNP_PDIRUNLOCK) == 0)
441 			VOP_LOCK(dvp, NULL, LK_THISLAYER | LK_EXCLUSIVE, td);
442 		vrele(ldvp);
443 	} else if (cnp->cn_flags & CNP_PDIRUNLOCK) {
444 		VOP_UNLOCK(dvp, NULL, LK_THISLAYER, td);
445 	}
446 	return (error);
447 }
448 
449 /*
450  * Setattr call. Disallow write attempts if the layer is mounted read-only.
451  *
452  * null_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
453  *		struct vattr *a_vap, struct ucred *a_cred,
454  *		struct thread *a_td)
455  */
456 int
457 null_setattr(struct vop_setattr_args *ap)
458 {
459 	struct vnode *vp = ap->a_vp;
460 	struct vattr *vap = ap->a_vap;
461 
462   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
463 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
464 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
465 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
466 		return (EROFS);
467 	if (vap->va_size != VNOVAL) {
468  		switch (vp->v_type) {
469  		case VDIR:
470  			return (EISDIR);
471  		case VCHR:
472  		case VBLK:
473  		case VSOCK:
474  		case VFIFO:
475 			if (vap->va_flags != VNOVAL)
476 				return (EOPNOTSUPP);
477 			return (0);
478 		case VREG:
479 		case VLNK:
480  		default:
481 			/*
482 			 * Disallow write attempts if the filesystem is
483 			 * mounted read-only.
484 			 */
485 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
486 				return (EROFS);
487 		}
488 	}
489 
490 	return (null_bypass(&ap->a_head));
491 }
492 
493 /*
494  *  We handle getattr only to change the fsid.
495  *
496  * null_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
497  *		struct thread *a_td)
498  */
499 static int
500 null_getattr(struct vop_getattr_args *ap)
501 {
502 	int error;
503 
504 	if ((error = null_bypass(&ap->a_head)) != 0)
505 		return (error);
506 
507 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
508 	return (0);
509 }
510 
511 /*
512  * Handle to disallow write access if mounted read-only.
513  *
514  * null_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
515  *		struct thread *a_td)
516  */
517 static int
518 null_access(struct vop_access_args *ap)
519 {
520 	struct vnode *vp = ap->a_vp;
521 	mode_t mode = ap->a_mode;
522 
523 	/*
524 	 * Disallow write attempts on read-only layers;
525 	 * unless the file is a socket, fifo, or a block or
526 	 * character device resident on the file system.
527 	 */
528 	if (mode & VWRITE) {
529 		switch (vp->v_type) {
530 		case VDIR:
531 		case VLNK:
532 		case VREG:
533 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
534 				return (EROFS);
535 			break;
536 		default:
537 			break;
538 		}
539 	}
540 	return (null_bypass(&ap->a_head));
541 }
542 
543 /*
544  * We must handle open to be able to catch MNT_NODEV and friends.
545  *
546  * null_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
547  *	     struct thread *a_td)
548  */
549 static int
550 null_open(struct vop_open_args *ap)
551 {
552 	struct vnode *vp = ap->a_vp;
553 	struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
554 
555 	if ((vp->v_mount->mnt_flag & MNT_NODEV) &&
556 	    (lvp->v_type == VBLK || lvp->v_type == VCHR))
557 		return ENXIO;
558 
559 	return (null_bypass(&ap->a_head));
560 }
561 
562 /*
563  * We handle this to eliminate null FS to lower FS
564  * file moving. Don't know why we don't allow this,
565  * possibly we should.
566  *
567  * null_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
568  *		struct componentname *a_fcnp, struct vnode *a_tdvp,
569  *		struct vnode *a_tvp, struct componentname *a_tcnp)
570  */
571 static int
572 null_rename(struct vop_rename_args *ap)
573 {
574 	struct vnode *tdvp = ap->a_tdvp;
575 	struct vnode *fvp = ap->a_fvp;
576 	struct vnode *fdvp = ap->a_fdvp;
577 	struct vnode *tvp = ap->a_tvp;
578 
579 	/* Check for cross-device rename. */
580 	if ((fvp->v_mount != tdvp->v_mount) ||
581 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
582 		if (tdvp == tvp)
583 			vrele(tdvp);
584 		else
585 			vput(tdvp);
586 		if (tvp)
587 			vput(tvp);
588 		vrele(fdvp);
589 		vrele(fvp);
590 		return (EXDEV);
591 	}
592 
593 	return (null_bypass(&ap->a_head));
594 }
595 
596 /*
597  * A special flag, LK_THISLAYER, causes the locking function to operate
598  * ONLY on the nullfs layer.  Otherwise we are responsible for locking not
599  * only our layer, but the lower layer as well.
600  *
601  * null_lock(struct vnode *a_vp, lwkt_tokref_t a_vlock, int a_flags,
602  *	     struct thread *a_td)
603  */
604 static int
605 null_lock(struct vop_lock_args *ap)
606 {
607 	struct vnode *vp = ap->a_vp;
608 	int flags = ap->a_flags;
609 	struct null_node *np = VTONULL(vp);
610 	struct vnode *lvp;
611 	int error;
612 
613 	/*
614 	 * Lock the nullfs layer first, disposing of the interlock in the
615 	 * process.
616 	 */
617 	error = lockmgr(&vp->v_lock, flags & ~LK_THISLAYER,
618 			ap->a_vlock, ap->a_td);
619 	flags &= ~LK_INTERLOCK;
620 
621 	/*
622 	 * If locking only the nullfs layer, or if there is no lower layer,
623 	 * or if an error occured while attempting to lock the nullfs layer,
624 	 * we are done.
625 	 *
626 	 * np can be NULL is the vnode is being recycled from a previous
627 	 * hash collision.
628 	 */
629 	if ((flags & LK_THISLAYER) || np == NULL ||
630 	    np->null_lowervp == NULL || error) {
631 		return (error);
632 	}
633 
634 	/*
635 	 * Lock the underlying vnode.  If we are draining we should not drain
636 	 * the underlying vnode, since it is not being destroyed, but we do
637 	 * lock it exclusively in that case.  Note that any interlocks have
638 	 * already been disposed of above.
639 	 */
640 	lvp = np->null_lowervp;
641 	if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
642 		NULLFSDEBUG("null_lock: avoiding LK_DRAIN\n");
643 		error = vn_lock(lvp, NULL,
644 				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
645 				ap->a_td);
646 	} else {
647 		error = vn_lock(lvp, NULL, flags, ap->a_td);
648 	}
649 
650 	/*
651 	 * If an error occured we have to undo our nullfs lock, then return
652 	 * the original error.
653 	 */
654 	if (error)
655 		lockmgr(&vp->v_lock, LK_RELEASE, NULL, ap->a_td);
656 	return(error);
657 }
658 
659 /*
660  * A special flag, LK_THISLAYER, causes the unlocking function to operate
661  * ONLY on the nullfs layer.  Otherwise we are responsible for unlocking not
662  * only our layer, but the lower layer as well.
663  *
664  * null_unlock(struct vnode *a_vp, lwkt_tokref_t a_vlock, int a_flags,
665  *		struct thread *a_td)
666  */
667 static int
668 null_unlock(struct vop_unlock_args *ap)
669 {
670 	struct vnode *vp = ap->a_vp;
671 	int flags = ap->a_flags;
672 	struct null_node *np = VTONULL(vp);
673 	struct vnode *lvp;
674 	int error;
675 
676 	/*
677 	 * nullfs layer only
678 	 */
679 	if (flags & LK_THISLAYER) {
680 		error = lockmgr(&vp->v_lock,
681 				(flags & ~LK_THISLAYER) | LK_RELEASE,
682 				ap->a_vlock,
683 				ap->a_td);
684 		return (error);
685 	}
686 
687 	/*
688 	 * If there is no underlying vnode the lock operation occurs at
689 	 * the nullfs layer.  np can be NULL is the vnode is being recycled
690 	 * from a previous hash collision.
691 	 */
692 	if (np == NULL || (lvp = np->null_lowervp) == NULL) {
693 		error = lockmgr(&vp->v_lock, flags | LK_RELEASE,
694 				ap->a_vlock, ap->a_td);
695 		return(error);
696 	}
697 
698 	/*
699 	 * Unlock the lower layer first, then our nullfs layer.
700 	 */
701 	VOP_UNLOCK(lvp, NULL, flags & ~LK_INTERLOCK, ap->a_td);
702 	error = lockmgr(&vp->v_lock, flags | LK_RELEASE,
703 			ap->a_vlock, ap->a_td);
704 	return (error);
705 }
706 
707 /*
708  * null_islocked(struct vnode *a_vp, struct thread *a_td)
709  *
710  * If a lower layer exists return the lock status of the lower layer,
711  * otherwise return the lock status of our nullfs layer.
712  */
713 static int
714 null_islocked(struct vop_islocked_args *ap)
715 {
716 	struct vnode *vp = ap->a_vp;
717 	struct vnode *lvp;
718 	struct null_node *np = VTONULL(vp);
719 	int error;
720 
721 	lvp = np->null_lowervp;
722 	if (lvp == NULL)
723 		error = lockstatus(&vp->v_lock, ap->a_td);
724 	else
725 		error = VOP_ISLOCKED(lvp, ap->a_td);
726 	return (error);
727 }
728 
729 
730 /*
731  * There is no way to tell that someone issued remove/rmdir operation
732  * on the underlying filesystem. For now we just have to release lowevrp
733  * as soon as possible.
734  *
735  * null_inactive(struct vnode *a_vp, struct thread *a_td)
736  */
737 static int
738 null_inactive(struct vop_inactive_args *ap)
739 {
740 	struct vnode *vp = ap->a_vp;
741 	struct null_node *np = VTONULL(vp);
742 	struct vnode *lowervp;
743 
744 	/*
745 	 * Clean out the lowervp.  Due to the drain the lower vp has
746 	 * been exclusively locked.  We undo that, then release our
747 	 * null_lowervp reference to lowervp.  The lower vnode's
748 	 * inactive routine may or may not be called when we do the
749 	 * final vrele().
750 	 */
751 	if (np) {
752 		null_node_rem(np);
753 		lowervp = np->null_lowervp;
754 		np->null_lowervp = NULLVP;
755 		if (lowervp) {
756 			vput(lowervp);
757 			vrele (lowervp);
758 		}
759 	}
760 
761 	/*
762 	 * Now it is safe to release our nullfs layer vnode.
763 	 */
764 	VOP_UNLOCK(vp, NULL, LK_THISLAYER, ap->a_td);
765 	return (0);
766 }
767 
768 /*
769  * We can free memory in null_inactive, but we do this
770  * here. (Possible to guard vp->v_data to point somewhere)
771  *
772  * null_reclaim(struct vnode *a_vp, struct thread *a_td)
773  */
774 static int
775 null_reclaim(struct vop_reclaim_args *ap)
776 {
777 	struct vnode *vp = ap->a_vp;
778 	struct null_node *np;
779 
780 	np = VTONULL(vp);
781 	vp->v_data = NULL;
782 	if (np)
783 		free(np, M_NULLFSNODE);
784 	return (0);
785 }
786 
787 /*
788  * null_print(struct vnode *a_vp)
789  */
790 static int
791 null_print(struct vop_print_args *ap)
792 {
793 	struct vnode *vp = ap->a_vp;
794 	struct null_node *np = VTONULL(vp);
795 
796 	if (np == NULL) {
797 		printf ("\ttag VT_NULLFS, vp=%p, NULL v_data!\n", vp);
798 		return(0);
799 	}
800 	printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, np->null_lowervp);
801 	if (np->null_lowervp != NULL) {
802 		printf("\tlowervp_lock: ");
803 		lockmgr_printinfo(&np->null_lowervp->v_lock);
804 	} else {
805 		printf("\tnull_lock: ");
806 		lockmgr_printinfo(&vp->v_lock);
807 	}
808 	printf("\n");
809 	return (0);
810 }
811 
812 /*
813  * Let an underlying filesystem do the work
814  *
815  * null_createvobject(struct vnode *vp, struct ucred *cred, struct proc *p)
816  */
817 static int
818 null_createvobject(struct vop_createvobject_args *ap)
819 {
820 	struct vnode *vp = ap->a_vp;
821 	struct vnode *lowervp = VTONULL(vp) ? NULLVPTOLOWERVP(vp) : NULL;
822 	int error;
823 
824 	if (vp->v_type == VNON || lowervp == NULL)
825 		return 0;
826 	error = VOP_CREATEVOBJECT(lowervp, ap->a_td);
827 	if (error)
828 		return (error);
829 	vp->v_flag |= VOBJBUF;
830 	return (0);
831 }
832 
833 /*
834  * We have nothing to destroy and this operation shouldn't be bypassed.
835  *
836  * null_destroyvobject(struct vnode *vp)
837  */
838 static int
839 null_destroyvobject(struct vop_destroyvobject_args *ap)
840 {
841 	struct vnode *vp = ap->a_vp;
842 
843 	vp->v_flag &= ~VOBJBUF;
844 	return (0);
845 }
846 
847 /*
848  * null_getvobject(struct vnode *vp, struct vm_object **objpp)
849  *
850  * Note that this can be called when a vnode is being recycled, and
851  * v_data may be NULL in that case if nullfs had to recycle a vnode
852  * due to a null_node collision.
853  */
854 static int
855 null_getvobject(struct vop_getvobject_args *ap)
856 {
857 	struct vnode *lvp;
858 
859 	if (ap->a_vp->v_data == NULL)
860 		return EINVAL;
861 
862 	lvp = NULLVPTOLOWERVP(ap->a_vp);
863 	if (lvp == NULL)
864 		return EINVAL;
865 	return (VOP_GETVOBJECT(lvp, ap->a_objpp));
866 }
867 
868 /*
869  * Global vfs data structures
870  */
871 struct vnodeopv_entry_desc null_vnodeop_entries[] = {
872 	{ &vop_default_desc,		(void *) null_bypass },
873 	{ &vop_access_desc,		(void *) null_access },
874 	{ &vop_createvobject_desc,	(void *) null_createvobject },
875 	{ &vop_destroyvobject_desc,	(void *) null_destroyvobject },
876 	{ &vop_getattr_desc,		(void *) null_getattr },
877 	{ &vop_getvobject_desc,		(void *) null_getvobject },
878 	{ &vop_inactive_desc,		(void *) null_inactive },
879 	{ &vop_islocked_desc,		(void *) null_islocked },
880 	{ &vop_lock_desc,		(void *) null_lock },
881 	{ &vop_lookup_desc,		(void *) null_lookup },
882 	{ &vop_open_desc,		(void *) null_open },
883 	{ &vop_print_desc,		(void *) null_print },
884 	{ &vop_reclaim_desc,		(void *) null_reclaim },
885 	{ &vop_rename_desc,		(void *) null_rename },
886 	{ &vop_setattr_desc,		(void *) null_setattr },
887 	{ &vop_unlock_desc,		(void *) null_unlock },
888 	{ NULL, NULL }
889 };
890 
891