1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * John Heidemann of the UCLA Ficus project.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
35  *
36  * Ancestors:
37  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
38  *	...and...
39  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
40  *
41  * $FreeBSD: stable/12/sys/fs/nullfs/null_vnops.c 372390 2022-08-10 12:29:02Z git2svn $
42  */
43 
44 /*
45  * Null Layer
46  *
47  * (See mount_nullfs(8) for more information.)
48  *
49  * The null layer duplicates a portion of the filesystem
50  * name space under a new name.  In this respect, it is
51  * similar to the loopback filesystem.  It differs from
52  * the loopback fs in two respects:  it is implemented using
53  * a stackable layers techniques, and its "null-node"s stack above
54  * all lower-layer vnodes, not just over directory vnodes.
55  *
56  * The null layer has two purposes.  First, it serves as a demonstration
57  * of layering by proving a layer which does nothing.  (It actually
58  * does everything the loopback filesystem does, which is slightly
59  * more than nothing.)  Second, the null layer can serve as a prototype
60  * layer.  Since it provides all necessary layer framework,
61  * new filesystem layers can be created very easily be starting
62  * with a null layer.
63  *
64  * The remainder of this man page examines the null layer as a basis
65  * for constructing new layers.
66  *
67  *
68  * INSTANTIATING NEW NULL LAYERS
69  *
70  * New null layers are created with mount_nullfs(8).
71  * Mount_nullfs(8) takes two arguments, the pathname
72  * of the lower vfs (target-pn) and the pathname where the null
73  * layer will appear in the namespace (alias-pn).  After
74  * the null layer is put into place, the contents
75  * of target-pn subtree will be aliased under alias-pn.
76  *
77  *
78  * OPERATION OF A NULL LAYER
79  *
80  * The null layer is the minimum filesystem layer,
81  * simply bypassing all possible operations to the lower layer
82  * for processing there.  The majority of its activity centers
83  * on the bypass routine, through which nearly all vnode operations
84  * pass.
85  *
86  * The bypass routine accepts arbitrary vnode operations for
87  * handling by the lower layer.  It begins by examining vnode
88  * operation arguments and replacing any null-nodes by their
89  * lower-layer equivlants.  It then invokes the operation
90  * on the lower layer.  Finally, it replaces the null-nodes
91  * in the arguments and, if a vnode is return by the operation,
92  * stacks a null-node on top of the returned vnode.
93  *
94  * Although bypass handles most operations, vop_getattr, vop_lock,
95  * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
96  * bypassed. Vop_getattr must change the fsid being returned.
97  * Vop_lock and vop_unlock must handle any locking for the
98  * current vnode as well as pass the lock request down.
99  * Vop_inactive and vop_reclaim are not bypassed so that
100  * they can handle freeing null-layer specific data. Vop_print
101  * is not bypassed to avoid excessive debugging information.
102  * Also, certain vnode operations change the locking state within
103  * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
104  * and symlink). Ideally these operations should not change the
105  * lock state, but should be changed to let the caller of the
106  * function unlock them. Otherwise all intermediate vnode layers
107  * (such as union, umapfs, etc) must catch these functions to do
108  * the necessary locking at their layer.
109  *
110  *
111  * INSTANTIATING VNODE STACKS
112  *
113  * Mounting associates the null layer with a lower layer,
114  * effect stacking two VFSes.  Vnode stacks are instead
115  * created on demand as files are accessed.
116  *
117  * The initial mount creates a single vnode stack for the
118  * root of the new null layer.  All other vnode stacks
119  * are created as a result of vnode operations on
120  * this or other null vnode stacks.
121  *
122  * New vnode stacks come into existence as a result of
123  * an operation which returns a vnode.
124  * The bypass routine stacks a null-node above the new
125  * vnode before returning it to the caller.
126  *
127  * For example, imagine mounting a null layer with
128  * "mount_nullfs /usr/include /dev/layer/null".
129  * Changing directory to /dev/layer/null will assign
130  * the root null-node (which was created when the null layer was mounted).
131  * Now consider opening "sys".  A vop_lookup would be
132  * done on the root null-node.  This operation would bypass through
133  * to the lower layer which would return a vnode representing
134  * the UFS "sys".  Null_bypass then builds a null-node
135  * aliasing the UFS "sys" and returns this to the caller.
136  * Later operations on the null-node "sys" will repeat this
137  * process when constructing other vnode stacks.
138  *
139  *
140  * CREATING OTHER FILE SYSTEM LAYERS
141  *
142  * One of the easiest ways to construct new filesystem layers is to make
143  * a copy of the null layer, rename all files and variables, and
144  * then begin modifing the copy.  Sed can be used to easily rename
145  * all variables.
146  *
147  * The umap layer is an example of a layer descended from the
148  * null layer.
149  *
150  *
151  * INVOKING OPERATIONS ON LOWER LAYERS
152  *
153  * There are two techniques to invoke operations on a lower layer
154  * when the operation cannot be completely bypassed.  Each method
155  * is appropriate in different situations.  In both cases,
156  * it is the responsibility of the aliasing layer to make
157  * the operation arguments "correct" for the lower layer
158  * by mapping a vnode arguments to the lower layer.
159  *
160  * The first approach is to call the aliasing layer's bypass routine.
161  * This method is most suitable when you wish to invoke the operation
162  * currently being handled on the lower layer.  It has the advantage
163  * that the bypass routine already must do argument mapping.
164  * An example of this is null_getattrs in the null layer.
165  *
166  * A second approach is to directly invoke vnode operations on
167  * the lower layer with the VOP_OPERATIONNAME interface.
168  * The advantage of this method is that it is easy to invoke
169  * arbitrary operations on the lower layer.  The disadvantage
170  * is that vnode arguments must be manualy mapped.
171  *
172  */
173 
174 #include <sys/param.h>
175 #include <sys/systm.h>
176 #include <sys/conf.h>
177 #include <sys/kernel.h>
178 #include <sys/lock.h>
179 #include <sys/malloc.h>
180 #include <sys/mount.h>
181 #include <sys/mutex.h>
182 #include <sys/namei.h>
183 #include <sys/sysctl.h>
184 #include <sys/vnode.h>
185 
186 #include <fs/nullfs/null.h>
187 
188 #include <vm/vm.h>
189 #include <vm/vm_extern.h>
190 #include <vm/vm_object.h>
191 #include <vm/vnode_pager.h>
192 
193 static int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
194 SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
195 	&null_bug_bypass, 0, "");
196 
197 /*
198  * This is the 10-Apr-92 bypass routine.
199  *    This version has been optimized for speed, throwing away some
200  * safety checks.  It should still always work, but it's not as
201  * robust to programmer errors.
202  *
203  * In general, we map all vnodes going down and unmap them on the way back.
204  * As an exception to this, vnodes can be marked "unmapped" by setting
205  * the Nth bit in operation's vdesc_flags.
206  *
207  * Also, some BSD vnode operations have the side effect of vrele'ing
208  * their arguments.  With stacking, the reference counts are held
209  * by the upper node, not the lower one, so we must handle these
210  * side-effects here.  This is not of concern in Sun-derived systems
211  * since there are no such side-effects.
212  *
213  * This makes the following assumptions:
214  * - only one returned vpp
215  * - no INOUT vpp's (Sun's vop_open has one of these)
216  * - the vnode operation vector of the first vnode should be used
217  *   to determine what implementation of the op should be invoked
218  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
219  *   problems on rmdir'ing mount points and renaming?)
220  */
221 int
null_bypass(struct vop_generic_args * ap)222 null_bypass(struct vop_generic_args *ap)
223 {
224 	struct vnode **this_vp_p;
225 	int error;
226 	struct vnode *old_vps[VDESC_MAX_VPS];
227 	struct vnode **vps_p[VDESC_MAX_VPS];
228 	struct vnode ***vppp;
229 	struct vnodeop_desc *descp = ap->a_desc;
230 	int reles, i;
231 
232 	if (null_bug_bypass)
233 		printf ("null_bypass: %s\n", descp->vdesc_name);
234 
235 #ifdef DIAGNOSTIC
236 	/*
237 	 * We require at least one vp.
238 	 */
239 	if (descp->vdesc_vp_offsets == NULL ||
240 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
241 		panic ("null_bypass: no vp's in map");
242 #endif
243 
244 	/*
245 	 * Map the vnodes going in.
246 	 * Later, we'll invoke the operation based on
247 	 * the first mapped vnode's operation vector.
248 	 */
249 	reles = descp->vdesc_flags;
250 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
251 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
252 			break;   /* bail out at end of list */
253 		vps_p[i] = this_vp_p =
254 			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
255 		/*
256 		 * We're not guaranteed that any but the first vnode
257 		 * are of our type.  Check for and don't map any
258 		 * that aren't.  (We must always map first vp or vclean fails.)
259 		 */
260 		if (i && (*this_vp_p == NULLVP ||
261 		    (*this_vp_p)->v_op != &null_vnodeops)) {
262 			old_vps[i] = NULLVP;
263 		} else {
264 			old_vps[i] = *this_vp_p;
265 			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
266 			/*
267 			 * XXX - Several operations have the side effect
268 			 * of vrele'ing their vp's.  We must account for
269 			 * that.  (This should go away in the future.)
270 			 */
271 			if (reles & VDESC_VP0_WILLRELE)
272 				VREF(*this_vp_p);
273 		}
274 
275 	}
276 
277 	/*
278 	 * Call the operation on the lower layer
279 	 * with the modified argument structure.
280 	 */
281 	if (vps_p[0] && *vps_p[0])
282 		error = VCALL(ap);
283 	else {
284 		printf("null_bypass: no map for %s\n", descp->vdesc_name);
285 		error = EINVAL;
286 	}
287 
288 	/*
289 	 * Maintain the illusion of call-by-value
290 	 * by restoring vnodes in the argument structure
291 	 * to their original value.
292 	 */
293 	reles = descp->vdesc_flags;
294 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
295 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
296 			break;   /* bail out at end of list */
297 		if (old_vps[i]) {
298 			*(vps_p[i]) = old_vps[i];
299 #if 0
300 			if (reles & VDESC_VP0_WILLUNLOCK)
301 				VOP_UNLOCK(*(vps_p[i]), 0);
302 #endif
303 			if (reles & VDESC_VP0_WILLRELE)
304 				vrele(*(vps_p[i]));
305 		}
306 	}
307 
308 	/*
309 	 * Map the possible out-going vpp
310 	 * (Assumes that the lower layer always returns
311 	 * a VREF'ed vpp unless it gets an error.)
312 	 */
313 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
314 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
315 	    !error) {
316 		/*
317 		 * XXX - even though some ops have vpp returned vp's,
318 		 * several ops actually vrele this before returning.
319 		 * We must avoid these ops.
320 		 * (This should go away when these ops are regularized.)
321 		 */
322 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
323 			goto out;
324 		vppp = VOPARG_OFFSETTO(struct vnode***,
325 				 descp->vdesc_vpp_offset,ap);
326 		if (*vppp)
327 			error = null_nodeget(old_vps[0]->v_mount, **vppp, *vppp);
328 	}
329 
330  out:
331 	return (error);
332 }
333 
334 static int
null_add_writecount(struct vop_add_writecount_args * ap)335 null_add_writecount(struct vop_add_writecount_args *ap)
336 {
337 	struct vnode *lvp, *vp;
338 	int error;
339 
340 	vp = ap->a_vp;
341 	lvp = NULLVPTOLOWERVP(vp);
342 	VI_LOCK(vp);
343 	/* text refs are bypassed to lowervp */
344 	VNASSERT(vp->v_writecount >= 0, vp, ("wrong null writecount"));
345 	VNASSERT(vp->v_writecount + ap->a_inc >= 0, vp,
346 	    ("wrong writecount inc %d", ap->a_inc));
347 	error = VOP_ADD_WRITECOUNT(lvp, ap->a_inc);
348 	if (error == 0)
349 		vp->v_writecount += ap->a_inc;
350 	VI_UNLOCK(vp);
351 	return (error);
352 }
353 
354 /*
355  * We have to carry on the locking protocol on the null layer vnodes
356  * as we progress through the tree. We also have to enforce read-only
357  * if this layer is mounted read-only.
358  */
359 static int
null_lookup(struct vop_lookup_args * ap)360 null_lookup(struct vop_lookup_args *ap)
361 {
362 	struct componentname *cnp = ap->a_cnp;
363 	struct vnode *dvp = ap->a_dvp;
364 	int flags = cnp->cn_flags;
365 	struct vnode *vp, *ldvp, *lvp;
366 	struct mount *mp;
367 	int error;
368 
369 	mp = dvp->v_mount;
370 	if ((flags & ISLASTCN) != 0 && (mp->mnt_flag & MNT_RDONLY) != 0 &&
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 
380 	/*
381 	 * Renames in the lower mounts might create an inconsistent
382 	 * configuration where lower vnode is moved out of the
383 	 * directory tree remounted by our null mount.  Do not try to
384 	 * handle it fancy, just avoid VOP_LOOKUP() with DOTDOT name
385 	 * which cannot be handled by VOP, at least passing over lower
386 	 * root.
387 	 */
388 	if ((ldvp->v_vflag & VV_ROOT) != 0 && (flags & ISDOTDOT) != 0) {
389 		KASSERT((dvp->v_vflag & VV_ROOT) == 0,
390 		    ("ldvp %p fl %#x dvp %p fl %#x flags %#x",
391 		    ldvp, ldvp->v_vflag, dvp, dvp->v_vflag, flags));
392 		return (ENOENT);
393 	}
394 
395 	/*
396 	 * Hold ldvp.  The reference on it, owned by dvp, is lost in
397 	 * case of dvp reclamation, and we need ldvp to move our lock
398 	 * from ldvp to dvp.
399 	 */
400 	vhold(ldvp);
401 
402 	error = VOP_LOOKUP(ldvp, &lvp, cnp);
403 
404 	/*
405 	 * VOP_LOOKUP() on lower vnode may unlock ldvp, which allows
406 	 * dvp to be reclaimed due to shared v_vnlock.  Check for the
407 	 * doomed state and return error.
408 	 */
409 	if ((error == 0 || error == EJUSTRETURN) &&
410 	    (dvp->v_iflag & VI_DOOMED) != 0) {
411 		error = ENOENT;
412 		if (lvp != NULL)
413 			vput(lvp);
414 
415 		/*
416 		 * If vgone() did reclaimed dvp before curthread
417 		 * relocked ldvp, the locks of dvp and ldpv are no
418 		 * longer shared.  In this case, relock of ldvp in
419 		 * lower fs VOP_LOOKUP() does not restore the locking
420 		 * state of dvp.  Compensate for this by unlocking
421 		 * ldvp and locking dvp, which is also correct if the
422 		 * locks are still shared.
423 		 */
424 		VOP_UNLOCK(ldvp, 0);
425 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
426 	}
427 	vdrop(ldvp);
428 
429 	if (error == EJUSTRETURN && (flags & ISLASTCN) != 0 &&
430 	    (mp->mnt_flag & MNT_RDONLY) != 0 &&
431 	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
432 		error = EROFS;
433 
434 	if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
435 		if (ldvp == lvp) {
436 			*ap->a_vpp = dvp;
437 			VREF(dvp);
438 			vrele(lvp);
439 		} else {
440 			error = null_nodeget(mp, lvp, &vp);
441 			if (error == 0)
442 				*ap->a_vpp = vp;
443 		}
444 	}
445 	return (error);
446 }
447 
448 static int
null_open(struct vop_open_args * ap)449 null_open(struct vop_open_args *ap)
450 {
451 	int retval;
452 	struct vnode *vp, *ldvp;
453 
454 	vp = ap->a_vp;
455 	ldvp = NULLVPTOLOWERVP(vp);
456 	retval = null_bypass(&ap->a_gen);
457 	if (retval == 0)
458 		vp->v_object = ldvp->v_object;
459 	return (retval);
460 }
461 
462 /*
463  * Setattr call. Disallow write attempts if the layer is mounted read-only.
464  */
465 static int
null_setattr(struct vop_setattr_args * ap)466 null_setattr(struct vop_setattr_args *ap)
467 {
468 	struct vnode *vp = ap->a_vp;
469 	struct vattr *vap = ap->a_vap;
470 
471   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
472 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
473 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
474 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
475 		return (EROFS);
476 	if (vap->va_size != VNOVAL) {
477  		switch (vp->v_type) {
478  		case VDIR:
479  			return (EISDIR);
480  		case VCHR:
481  		case VBLK:
482  		case VSOCK:
483  		case VFIFO:
484 			if (vap->va_flags != VNOVAL)
485 				return (EOPNOTSUPP);
486 			return (0);
487 		case VREG:
488 		case VLNK:
489  		default:
490 			/*
491 			 * Disallow write attempts if the filesystem is
492 			 * mounted read-only.
493 			 */
494 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
495 				return (EROFS);
496 		}
497 	}
498 
499 	return (null_bypass((struct vop_generic_args *)ap));
500 }
501 
502 /*
503  *  We handle getattr only to change the fsid.
504  */
505 static int
null_getattr(struct vop_getattr_args * ap)506 null_getattr(struct vop_getattr_args *ap)
507 {
508 	int error;
509 
510 	if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
511 		return (error);
512 
513 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
514 	return (0);
515 }
516 
517 /*
518  * Handle to disallow write access if mounted read-only.
519  */
520 static int
null_access(struct vop_access_args * ap)521 null_access(struct vop_access_args *ap)
522 {
523 	struct vnode *vp = ap->a_vp;
524 	accmode_t accmode = ap->a_accmode;
525 
526 	/*
527 	 * Disallow write attempts on read-only layers;
528 	 * unless the file is a socket, fifo, or a block or
529 	 * character device resident on the filesystem.
530 	 */
531 	if (accmode & VWRITE) {
532 		switch (vp->v_type) {
533 		case VDIR:
534 		case VLNK:
535 		case VREG:
536 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
537 				return (EROFS);
538 			break;
539 		default:
540 			break;
541 		}
542 	}
543 	return (null_bypass((struct vop_generic_args *)ap));
544 }
545 
546 static int
null_accessx(struct vop_accessx_args * ap)547 null_accessx(struct vop_accessx_args *ap)
548 {
549 	struct vnode *vp = ap->a_vp;
550 	accmode_t accmode = ap->a_accmode;
551 
552 	/*
553 	 * Disallow write attempts on read-only layers;
554 	 * unless the file is a socket, fifo, or a block or
555 	 * character device resident on the filesystem.
556 	 */
557 	if (accmode & VWRITE) {
558 		switch (vp->v_type) {
559 		case VDIR:
560 		case VLNK:
561 		case VREG:
562 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
563 				return (EROFS);
564 			break;
565 		default:
566 			break;
567 		}
568 	}
569 	return (null_bypass((struct vop_generic_args *)ap));
570 }
571 
572 /*
573  * Increasing refcount of lower vnode is needed at least for the case
574  * when lower FS is NFS to do sillyrename if the file is in use.
575  * Unfortunately v_usecount is incremented in many places in
576  * the kernel and, as such, there may be races that result in
577  * the NFS client doing an extraneous silly rename, but that seems
578  * preferable to not doing a silly rename when it is needed.
579  */
580 static int
null_remove(struct vop_remove_args * ap)581 null_remove(struct vop_remove_args *ap)
582 {
583 	int retval, vreleit;
584 	struct vnode *lvp, *vp;
585 
586 	vp = ap->a_vp;
587 	if (vrefcnt(vp) > 1) {
588 		lvp = NULLVPTOLOWERVP(vp);
589 		VREF(lvp);
590 		vreleit = 1;
591 	} else
592 		vreleit = 0;
593 	VTONULL(vp)->null_flags |= NULLV_DROP;
594 	retval = null_bypass(&ap->a_gen);
595 	if (vreleit != 0)
596 		vrele(lvp);
597 	return (retval);
598 }
599 
600 /*
601  * We handle this to eliminate null FS to lower FS
602  * file moving. Don't know why we don't allow this,
603  * possibly we should.
604  */
605 static int
null_rename(struct vop_rename_args * ap)606 null_rename(struct vop_rename_args *ap)
607 {
608 	struct vnode *tdvp = ap->a_tdvp;
609 	struct vnode *fvp = ap->a_fvp;
610 	struct vnode *fdvp = ap->a_fdvp;
611 	struct vnode *tvp = ap->a_tvp;
612 	struct null_node *tnn;
613 
614 	/* Check for cross-device rename. */
615 	if ((fvp->v_mount != tdvp->v_mount) ||
616 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
617 		if (tdvp == tvp)
618 			vrele(tdvp);
619 		else
620 			vput(tdvp);
621 		if (tvp)
622 			vput(tvp);
623 		vrele(fdvp);
624 		vrele(fvp);
625 		return (EXDEV);
626 	}
627 
628 	if (tvp != NULL) {
629 		tnn = VTONULL(tvp);
630 		tnn->null_flags |= NULLV_DROP;
631 	}
632 	return (null_bypass((struct vop_generic_args *)ap));
633 }
634 
635 static int
null_rmdir(struct vop_rmdir_args * ap)636 null_rmdir(struct vop_rmdir_args *ap)
637 {
638 
639 	VTONULL(ap->a_vp)->null_flags |= NULLV_DROP;
640 	return (null_bypass(&ap->a_gen));
641 }
642 
643 /*
644  * We need to process our own vnode lock and then clear the
645  * interlock flag as it applies only to our vnode, not the
646  * vnodes below us on the stack.
647  */
648 static int
null_lock(struct vop_lock1_args * ap)649 null_lock(struct vop_lock1_args *ap)
650 {
651 	struct vnode *vp = ap->a_vp;
652 	int flags = ap->a_flags;
653 	struct null_node *nn;
654 	struct vnode *lvp;
655 	int error;
656 
657 
658 	if ((flags & LK_INTERLOCK) == 0) {
659 		VI_LOCK(vp);
660 		ap->a_flags = flags |= LK_INTERLOCK;
661 	}
662 	nn = VTONULL(vp);
663 	/*
664 	 * If we're still active we must ask the lower layer to
665 	 * lock as ffs has special lock considerations in its
666 	 * vop lock.
667 	 */
668 	if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
669 		VI_LOCK_FLAGS(lvp, MTX_DUPOK);
670 		VI_UNLOCK(vp);
671 		/*
672 		 * We have to hold the vnode here to solve a potential
673 		 * reclaim race.  If we're forcibly vgone'd while we
674 		 * still have refs, a thread could be sleeping inside
675 		 * the lowervp's vop_lock routine.  When we vgone we will
676 		 * drop our last ref to the lowervp, which would allow it
677 		 * to be reclaimed.  The lowervp could then be recycled,
678 		 * in which case it is not legal to be sleeping in its VOP.
679 		 * We prevent it from being recycled by holding the vnode
680 		 * here.
681 		 */
682 		vholdl(lvp);
683 		error = VOP_LOCK(lvp, flags);
684 
685 		/*
686 		 * We might have slept to get the lock and someone might have
687 		 * clean our vnode already, switching vnode lock from one in
688 		 * lowervp to v_lock in our own vnode structure.  Handle this
689 		 * case by reacquiring correct lock in requested mode.
690 		 */
691 		if (VTONULL(vp) == NULL && error == 0) {
692 			ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
693 			switch (flags & LK_TYPE_MASK) {
694 			case LK_SHARED:
695 				ap->a_flags |= LK_SHARED;
696 				break;
697 			case LK_UPGRADE:
698 			case LK_EXCLUSIVE:
699 				ap->a_flags |= LK_EXCLUSIVE;
700 				break;
701 			default:
702 				panic("Unsupported lock request %d\n",
703 				    ap->a_flags);
704 			}
705 			VOP_UNLOCK(lvp, 0);
706 			error = vop_stdlock(ap);
707 		}
708 		vdrop(lvp);
709 	} else
710 		error = vop_stdlock(ap);
711 
712 	return (error);
713 }
714 
715 /*
716  * We need to process our own vnode unlock and then clear the
717  * interlock flag as it applies only to our vnode, not the
718  * vnodes below us on the stack.
719  */
720 static int
null_unlock(struct vop_unlock_args * ap)721 null_unlock(struct vop_unlock_args *ap)
722 {
723 	struct vnode *vp = ap->a_vp;
724 	int flags = ap->a_flags;
725 	int mtxlkflag = 0;
726 	struct null_node *nn;
727 	struct vnode *lvp;
728 	int error;
729 
730 	if ((flags & LK_INTERLOCK) != 0)
731 		mtxlkflag = 1;
732 	else if (mtx_owned(VI_MTX(vp)) == 0) {
733 		VI_LOCK(vp);
734 		mtxlkflag = 2;
735 	}
736 	nn = VTONULL(vp);
737 	if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
738 		VI_LOCK_FLAGS(lvp, MTX_DUPOK);
739 		flags |= LK_INTERLOCK;
740 		vholdl(lvp);
741 		VI_UNLOCK(vp);
742 		error = VOP_UNLOCK(lvp, flags);
743 		vdrop(lvp);
744 		if (mtxlkflag == 0)
745 			VI_LOCK(vp);
746 	} else {
747 		if (mtxlkflag == 2)
748 			VI_UNLOCK(vp);
749 		error = vop_stdunlock(ap);
750 	}
751 
752 	return (error);
753 }
754 
755 /*
756  * Do not allow the VOP_INACTIVE to be passed to the lower layer,
757  * since the reference count on the lower vnode is not related to
758  * ours.
759  */
760 static int
null_inactive(struct vop_inactive_args * ap __unused)761 null_inactive(struct vop_inactive_args *ap __unused)
762 {
763 	struct vnode *vp, *lvp;
764 	struct null_node *xp;
765 	struct mount *mp;
766 	struct null_mount *xmp;
767 
768 	vp = ap->a_vp;
769 	xp = VTONULL(vp);
770 	lvp = NULLVPTOLOWERVP(vp);
771 	mp = vp->v_mount;
772 	xmp = MOUNTTONULLMOUNT(mp);
773 	if ((xmp->nullm_flags & NULLM_CACHE) == 0 ||
774 	    (xp->null_flags & NULLV_DROP) != 0 ||
775 	    (lvp->v_vflag & VV_NOSYNC) != 0) {
776 		/*
777 		 * If this is the last reference and caching of the
778 		 * nullfs vnodes is not enabled, or the lower vnode is
779 		 * deleted, then free up the vnode so as not to tie up
780 		 * the lower vnodes.
781 		 */
782 		vp->v_object = NULL;
783 		vrecycle(vp);
784 	}
785 	return (0);
786 }
787 
788 /*
789  * Now, the nullfs vnode and, due to the sharing lock, the lower
790  * vnode, are exclusively locked, and we shall destroy the null vnode.
791  */
792 static int
null_reclaim(struct vop_reclaim_args * ap)793 null_reclaim(struct vop_reclaim_args *ap)
794 {
795 	struct vnode *vp;
796 	struct null_node *xp;
797 	struct vnode *lowervp;
798 
799 	vp = ap->a_vp;
800 	xp = VTONULL(vp);
801 	lowervp = xp->null_lowervp;
802 
803 	KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock,
804 	    ("Reclaiming incomplete null vnode %p", vp));
805 
806 	null_hashrem(xp);
807 	/*
808 	 * Use the interlock to protect the clearing of v_data to
809 	 * prevent faults in null_lock().
810 	 */
811 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
812 	VI_LOCK(vp);
813 	vp->v_data = NULL;
814 	vp->v_object = NULL;
815 	vp->v_vnlock = &vp->v_lock;
816 
817 	/*
818 	 * If we were opened for write, we leased the write reference
819 	 * to the lower vnode.  If this is a reclamation due to the
820 	 * forced unmount, undo the reference now.
821 	 */
822 	if (vp->v_writecount > 0)
823 		VOP_ADD_WRITECOUNT(lowervp, -vp->v_writecount);
824 	else if (vp->v_writecount < 0)
825 		vp->v_writecount = 0;
826 
827 	VI_UNLOCK(vp);
828 
829 	if ((xp->null_flags & NULLV_NOUNLOCK) != 0)
830 		vunref(lowervp);
831 	else
832 		vput(lowervp);
833 	free(xp, M_NULLFSNODE);
834 
835 	return (0);
836 }
837 
838 static int
null_print(struct vop_print_args * ap)839 null_print(struct vop_print_args *ap)
840 {
841 	struct vnode *vp = ap->a_vp;
842 
843 	printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp);
844 	return (0);
845 }
846 
847 /* ARGSUSED */
848 static int
null_getwritemount(struct vop_getwritemount_args * ap)849 null_getwritemount(struct vop_getwritemount_args *ap)
850 {
851 	struct null_node *xp;
852 	struct vnode *lowervp;
853 	struct vnode *vp;
854 
855 	vp = ap->a_vp;
856 	VI_LOCK(vp);
857 	xp = VTONULL(vp);
858 	if (xp && (lowervp = xp->null_lowervp)) {
859 		VI_LOCK_FLAGS(lowervp, MTX_DUPOK);
860 		VI_UNLOCK(vp);
861 		vholdl(lowervp);
862 		VI_UNLOCK(lowervp);
863 		VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);
864 		vdrop(lowervp);
865 	} else {
866 		VI_UNLOCK(vp);
867 		*(ap->a_mpp) = NULL;
868 	}
869 	return (0);
870 }
871 
872 static int
null_vptofh(struct vop_vptofh_args * ap)873 null_vptofh(struct vop_vptofh_args *ap)
874 {
875 	struct vnode *lvp;
876 
877 	lvp = NULLVPTOLOWERVP(ap->a_vp);
878 	return VOP_VPTOFH(lvp, ap->a_fhp);
879 }
880 
881 static int
null_vptocnp(struct vop_vptocnp_args * ap)882 null_vptocnp(struct vop_vptocnp_args *ap)
883 {
884 	struct vnode *vp = ap->a_vp;
885 	struct vnode **dvp = ap->a_vpp;
886 	struct vnode *lvp, *ldvp;
887 	struct ucred *cred = ap->a_cred;
888 	struct mount *mp;
889 	int error, locked;
890 
891 	locked = VOP_ISLOCKED(vp);
892 	lvp = NULLVPTOLOWERVP(vp);
893 	vhold(lvp);
894 	mp = vp->v_mount;
895 	vfs_ref(mp);
896 	VOP_UNLOCK(vp, 0); /* vp is held by vn_vptocnp_locked that called us */
897 	ldvp = lvp;
898 	vref(lvp);
899 	error = vn_vptocnp(&ldvp, cred, ap->a_buf, ap->a_buflen);
900 	vdrop(lvp);
901 	if (error != 0) {
902 		vn_lock(vp, locked | LK_RETRY);
903 		vfs_rel(mp);
904 		return (ENOENT);
905 	}
906 
907 	/*
908 	 * Exclusive lock is required by insmntque1 call in
909 	 * null_nodeget()
910 	 */
911 	error = vn_lock(ldvp, LK_EXCLUSIVE);
912 	if (error != 0) {
913 		vrele(ldvp);
914 		vn_lock(vp, locked | LK_RETRY);
915 		vfs_rel(mp);
916 		return (ENOENT);
917 	}
918 	error = null_nodeget(mp, ldvp, dvp);
919 	if (error == 0) {
920 #ifdef DIAGNOSTIC
921 		NULLVPTOLOWERVP(*dvp);
922 #endif
923 		VOP_UNLOCK(*dvp, 0); /* keep reference on *dvp */
924 	}
925 	vn_lock(vp, locked | LK_RETRY);
926 	vfs_rel(mp);
927 	return (error);
928 }
929 
930 /*
931  * Global vfs data structures
932  */
933 struct vop_vector null_vnodeops = {
934 	.vop_bypass =		null_bypass,
935 	.vop_access =		null_access,
936 	.vop_accessx =		null_accessx,
937 	.vop_advlockpurge =	vop_stdadvlockpurge,
938 	.vop_bmap =		VOP_EOPNOTSUPP,
939 	.vop_getattr =		null_getattr,
940 	.vop_getwritemount =	null_getwritemount,
941 	.vop_inactive =		null_inactive,
942 	.vop_islocked =		vop_stdislocked,
943 	.vop_lock1 =		null_lock,
944 	.vop_lookup =		null_lookup,
945 	.vop_open =		null_open,
946 	.vop_print =		null_print,
947 	.vop_reclaim =		null_reclaim,
948 	.vop_remove =		null_remove,
949 	.vop_rename =		null_rename,
950 	.vop_rmdir =		null_rmdir,
951 	.vop_setattr =		null_setattr,
952 	.vop_strategy =		VOP_EOPNOTSUPP,
953 	.vop_unlock =		null_unlock,
954 	.vop_vptocnp =		null_vptocnp,
955 	.vop_vptofh =		null_vptofh,
956 	.vop_add_writecount =	null_add_writecount,
957 };
958