xref: /freebsd/sys/fs/nullfs/null_vnops.c (revision e0f4540a)
1d167cf6fSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * John Heidemann of the UCLA Ficus project.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34996c772fSJohn Dyson  *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
35996c772fSJohn Dyson  *
36996c772fSJohn Dyson  * Ancestors:
37996c772fSJohn Dyson  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
38996c772fSJohn Dyson  *	...and...
39996c772fSJohn Dyson  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
40df8bae1dSRodney W. Grimes  *
41c3aac50fSPeter Wemm  * $FreeBSD$
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Null Layer
46df8bae1dSRodney W. Grimes  *
4757a523aeSRuslan Ermilov  * (See mount_nullfs(8) for more information.)
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * The null layer duplicates a portion of the filesystem
50df8bae1dSRodney W. Grimes  * name space under a new name.  In this respect, it is
51df8bae1dSRodney W. Grimes  * similar to the loopback filesystem.  It differs from
52df8bae1dSRodney W. Grimes  * the loopback fs in two respects:  it is implemented using
53dc733423SDag-Erling Smørgrav  * a stackable layers techniques, and its "null-node"s stack above
54df8bae1dSRodney W. Grimes  * all lower-layer vnodes, not just over directory vnodes.
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  * The null layer has two purposes.  First, it serves as a demonstration
57df8bae1dSRodney W. Grimes  * of layering by proving a layer which does nothing.  (It actually
58df8bae1dSRodney W. Grimes  * does everything the loopback filesystem does, which is slightly
59df8bae1dSRodney W. Grimes  * more than nothing.)  Second, the null layer can serve as a prototype
60df8bae1dSRodney W. Grimes  * layer.  Since it provides all necessary layer framework,
61df8bae1dSRodney W. Grimes  * new filesystem layers can be created very easily be starting
62df8bae1dSRodney W. Grimes  * with a null layer.
63df8bae1dSRodney W. Grimes  *
64df8bae1dSRodney W. Grimes  * The remainder of this man page examines the null layer as a basis
65df8bae1dSRodney W. Grimes  * for constructing new layers.
66df8bae1dSRodney W. Grimes  *
67df8bae1dSRodney W. Grimes  *
68df8bae1dSRodney W. Grimes  * INSTANTIATING NEW NULL LAYERS
69df8bae1dSRodney W. Grimes  *
7057a523aeSRuslan Ermilov  * New null layers are created with mount_nullfs(8).
7157a523aeSRuslan Ermilov  * Mount_nullfs(8) takes two arguments, the pathname
72df8bae1dSRodney W. Grimes  * of the lower vfs (target-pn) and the pathname where the null
73df8bae1dSRodney W. Grimes  * layer will appear in the namespace (alias-pn).  After
74df8bae1dSRodney W. Grimes  * the null layer is put into place, the contents
75df8bae1dSRodney W. Grimes  * of target-pn subtree will be aliased under alias-pn.
76df8bae1dSRodney W. Grimes  *
77df8bae1dSRodney W. Grimes  *
78df8bae1dSRodney W. Grimes  * OPERATION OF A NULL LAYER
79df8bae1dSRodney W. Grimes  *
80df8bae1dSRodney W. Grimes  * The null layer is the minimum filesystem layer,
81df8bae1dSRodney W. Grimes  * simply bypassing all possible operations to the lower layer
82df8bae1dSRodney W. Grimes  * for processing there.  The majority of its activity centers
8309c8ff4aSAlexander Langer  * on the bypass routine, through which nearly all vnode operations
84df8bae1dSRodney W. Grimes  * pass.
85df8bae1dSRodney W. Grimes  *
86df8bae1dSRodney W. Grimes  * The bypass routine accepts arbitrary vnode operations for
87df8bae1dSRodney W. Grimes  * handling by the lower layer.  It begins by examing vnode
88df8bae1dSRodney W. Grimes  * operation arguments and replacing any null-nodes by their
89df8bae1dSRodney W. Grimes  * lower-layer equivlants.  It then invokes the operation
90df8bae1dSRodney W. Grimes  * on the lower layer.  Finally, it replaces the null-nodes
91df8bae1dSRodney W. Grimes  * in the arguments and, if a vnode is return by the operation,
92df8bae1dSRodney W. Grimes  * stacks a null-node on top of the returned vnode.
93df8bae1dSRodney W. Grimes  *
94996c772fSJohn Dyson  * Although bypass handles most operations, vop_getattr, vop_lock,
95996c772fSJohn Dyson  * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
96996c772fSJohn Dyson  * bypassed. Vop_getattr must change the fsid being returned.
97996c772fSJohn Dyson  * Vop_lock and vop_unlock must handle any locking for the
98996c772fSJohn Dyson  * current vnode as well as pass the lock request down.
99df8bae1dSRodney W. Grimes  * Vop_inactive and vop_reclaim are not bypassed so that
100996c772fSJohn Dyson  * they can handle freeing null-layer specific data. Vop_print
101996c772fSJohn Dyson  * is not bypassed to avoid excessive debugging information.
102996c772fSJohn Dyson  * Also, certain vnode operations change the locking state within
103996c772fSJohn Dyson  * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
104996c772fSJohn Dyson  * and symlink). Ideally these operations should not change the
105996c772fSJohn Dyson  * lock state, but should be changed to let the caller of the
106996c772fSJohn Dyson  * function unlock them. Otherwise all intermediate vnode layers
107996c772fSJohn Dyson  * (such as union, umapfs, etc) must catch these functions to do
108996c772fSJohn Dyson  * the necessary locking at their layer.
109df8bae1dSRodney W. Grimes  *
110df8bae1dSRodney W. Grimes  *
111df8bae1dSRodney W. Grimes  * INSTANTIATING VNODE STACKS
112df8bae1dSRodney W. Grimes  *
113df8bae1dSRodney W. Grimes  * Mounting associates the null layer with a lower layer,
114df8bae1dSRodney W. Grimes  * effect stacking two VFSes.  Vnode stacks are instead
115df8bae1dSRodney W. Grimes  * created on demand as files are accessed.
116df8bae1dSRodney W. Grimes  *
117df8bae1dSRodney W. Grimes  * The initial mount creates a single vnode stack for the
118df8bae1dSRodney W. Grimes  * root of the new null layer.  All other vnode stacks
119df8bae1dSRodney W. Grimes  * are created as a result of vnode operations on
120df8bae1dSRodney W. Grimes  * this or other null vnode stacks.
121df8bae1dSRodney W. Grimes  *
122b3a15dddSPedro F. Giffuni  * New vnode stacks come into existence as a result of
123df8bae1dSRodney W. Grimes  * an operation which returns a vnode.
124df8bae1dSRodney W. Grimes  * The bypass routine stacks a null-node above the new
125df8bae1dSRodney W. Grimes  * vnode before returning it to the caller.
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  * For example, imagine mounting a null layer with
12857a523aeSRuslan Ermilov  * "mount_nullfs /usr/include /dev/layer/null".
129df8bae1dSRodney W. Grimes  * Changing directory to /dev/layer/null will assign
130df8bae1dSRodney W. Grimes  * the root null-node (which was created when the null layer was mounted).
131df8bae1dSRodney W. Grimes  * Now consider opening "sys".  A vop_lookup would be
132df8bae1dSRodney W. Grimes  * done on the root null-node.  This operation would bypass through
133df8bae1dSRodney W. Grimes  * to the lower layer which would return a vnode representing
134df8bae1dSRodney W. Grimes  * the UFS "sys".  Null_bypass then builds a null-node
135df8bae1dSRodney W. Grimes  * aliasing the UFS "sys" and returns this to the caller.
136df8bae1dSRodney W. Grimes  * Later operations on the null-node "sys" will repeat this
137df8bae1dSRodney W. Grimes  * process when constructing other vnode stacks.
138df8bae1dSRodney W. Grimes  *
139df8bae1dSRodney W. Grimes  *
140df8bae1dSRodney W. Grimes  * CREATING OTHER FILE SYSTEM LAYERS
141df8bae1dSRodney W. Grimes  *
142df8bae1dSRodney W. Grimes  * One of the easiest ways to construct new filesystem layers is to make
143df8bae1dSRodney W. Grimes  * a copy of the null layer, rename all files and variables, and
144df8bae1dSRodney W. Grimes  * then begin modifing the copy.  Sed can be used to easily rename
145df8bae1dSRodney W. Grimes  * all variables.
146df8bae1dSRodney W. Grimes  *
147df8bae1dSRodney W. Grimes  * The umap layer is an example of a layer descended from the
148df8bae1dSRodney W. Grimes  * null layer.
149df8bae1dSRodney W. Grimes  *
150df8bae1dSRodney W. Grimes  *
151df8bae1dSRodney W. Grimes  * INVOKING OPERATIONS ON LOWER LAYERS
152df8bae1dSRodney W. Grimes  *
153df8bae1dSRodney W. Grimes  * There are two techniques to invoke operations on a lower layer
154df8bae1dSRodney W. Grimes  * when the operation cannot be completely bypassed.  Each method
155df8bae1dSRodney W. Grimes  * is appropriate in different situations.  In both cases,
156df8bae1dSRodney W. Grimes  * it is the responsibility of the aliasing layer to make
157df8bae1dSRodney W. Grimes  * the operation arguments "correct" for the lower layer
158d64ada50SJens Schweikhardt  * by mapping a vnode arguments to the lower layer.
159df8bae1dSRodney W. Grimes  *
160df8bae1dSRodney W. Grimes  * The first approach is to call the aliasing layer's bypass routine.
161df8bae1dSRodney W. Grimes  * This method is most suitable when you wish to invoke the operation
162ee97e537SAlexander Langer  * currently being handled on the lower layer.  It has the advantage
163df8bae1dSRodney W. Grimes  * that the bypass routine already must do argument mapping.
164df8bae1dSRodney W. Grimes  * An example of this is null_getattrs in the null layer.
165df8bae1dSRodney W. Grimes  *
166ee97e537SAlexander Langer  * A second approach is to directly invoke vnode operations on
167df8bae1dSRodney W. Grimes  * the lower layer with the VOP_OPERATIONNAME interface.
168df8bae1dSRodney W. Grimes  * The advantage of this method is that it is easy to invoke
169df8bae1dSRodney W. Grimes  * arbitrary operations on the lower layer.  The disadvantage
170ee97e537SAlexander Langer  * is that vnode arguments must be manualy mapped.
171df8bae1dSRodney W. Grimes  *
172df8bae1dSRodney W. Grimes  */
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes #include <sys/param.h>
175df8bae1dSRodney W. Grimes #include <sys/systm.h>
1764451405fSBoris Popov #include <sys/conf.h>
177fb919e4dSMark Murray #include <sys/kernel.h>
178fb919e4dSMark Murray #include <sys/lock.h>
179fb919e4dSMark Murray #include <sys/malloc.h>
180fb919e4dSMark Murray #include <sys/mount.h>
181fb919e4dSMark Murray #include <sys/mutex.h>
182fb919e4dSMark Murray #include <sys/namei.h>
183d4b7a369SPoul-Henning Kamp #include <sys/sysctl.h>
184df8bae1dSRodney W. Grimes #include <sys/vnode.h>
185fb919e4dSMark Murray 
18699d300a1SRuslan Ermilov #include <fs/nullfs/null.h>
187df8bae1dSRodney W. Grimes 
1884451405fSBoris Popov #include <vm/vm.h>
1894451405fSBoris Popov #include <vm/vm_extern.h>
1904451405fSBoris Popov #include <vm/vm_object.h>
1914451405fSBoris Popov #include <vm/vnode_pager.h>
1924451405fSBoris Popov 
193d4b7a369SPoul-Henning Kamp static int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
194d4b7a369SPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
195d4b7a369SPoul-Henning Kamp 	&null_bug_bypass, 0, "");
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes /*
198df8bae1dSRodney W. Grimes  * This is the 10-Apr-92 bypass routine.
199df8bae1dSRodney W. Grimes  *    This version has been optimized for speed, throwing away some
200df8bae1dSRodney W. Grimes  * safety checks.  It should still always work, but it's not as
201df8bae1dSRodney W. Grimes  * robust to programmer errors.
202df8bae1dSRodney W. Grimes  *
203df8bae1dSRodney W. Grimes  * In general, we map all vnodes going down and unmap them on the way back.
204df8bae1dSRodney W. Grimes  * As an exception to this, vnodes can be marked "unmapped" by setting
205df8bae1dSRodney W. Grimes  * the Nth bit in operation's vdesc_flags.
206df8bae1dSRodney W. Grimes  *
207df8bae1dSRodney W. Grimes  * Also, some BSD vnode operations have the side effect of vrele'ing
208df8bae1dSRodney W. Grimes  * their arguments.  With stacking, the reference counts are held
209df8bae1dSRodney W. Grimes  * by the upper node, not the lower one, so we must handle these
210df8bae1dSRodney W. Grimes  * side-effects here.  This is not of concern in Sun-derived systems
211df8bae1dSRodney W. Grimes  * since there are no such side-effects.
212df8bae1dSRodney W. Grimes  *
213df8bae1dSRodney W. Grimes  * This makes the following assumptions:
214df8bae1dSRodney W. Grimes  * - only one returned vpp
215df8bae1dSRodney W. Grimes  * - no INOUT vpp's (Sun's vop_open has one of these)
216df8bae1dSRodney W. Grimes  * - the vnode operation vector of the first vnode should be used
217df8bae1dSRodney W. Grimes  *   to determine what implementation of the op should be invoked
218df8bae1dSRodney W. Grimes  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
219df8bae1dSRodney W. Grimes  *   problems on rmdir'ing mount points and renaming?)
220df8bae1dSRodney W. Grimes  */
221996c772fSJohn Dyson int
222a515233fSPoul-Henning Kamp null_bypass(struct vop_generic_args *ap)
223df8bae1dSRodney W. Grimes {
224d34dd851SPoul-Henning Kamp 	struct vnode **this_vp_p;
225df8bae1dSRodney W. Grimes 	int error;
226df8bae1dSRodney W. Grimes 	struct vnode *old_vps[VDESC_MAX_VPS];
227df8bae1dSRodney W. Grimes 	struct vnode **vps_p[VDESC_MAX_VPS];
228df8bae1dSRodney W. Grimes 	struct vnode ***vppp;
229df8bae1dSRodney W. Grimes 	struct vnodeop_desc *descp = ap->a_desc;
230df8bae1dSRodney W. Grimes 	int reles, i;
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes 	if (null_bug_bypass)
233df8bae1dSRodney W. Grimes 		printf ("null_bypass: %s\n", descp->vdesc_name);
234df8bae1dSRodney W. Grimes 
2354047cd0bSBruce Evans #ifdef DIAGNOSTIC
236df8bae1dSRodney W. Grimes 	/*
237df8bae1dSRodney W. Grimes 	 * We require at least one vp.
238df8bae1dSRodney W. Grimes 	 */
239df8bae1dSRodney W. Grimes 	if (descp->vdesc_vp_offsets == NULL ||
240df8bae1dSRodney W. Grimes 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
2414047cd0bSBruce Evans 		panic ("null_bypass: no vp's in map");
242df8bae1dSRodney W. Grimes #endif
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes 	/*
245df8bae1dSRodney W. Grimes 	 * Map the vnodes going in.
246df8bae1dSRodney W. Grimes 	 * Later, we'll invoke the operation based on
247df8bae1dSRodney W. Grimes 	 * the first mapped vnode's operation vector.
248df8bae1dSRodney W. Grimes 	 */
249df8bae1dSRodney W. Grimes 	reles = descp->vdesc_flags;
250df8bae1dSRodney W. Grimes 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
251df8bae1dSRodney W. Grimes 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
252df8bae1dSRodney W. Grimes 			break;   /* bail out at end of list */
253df8bae1dSRodney W. Grimes 		vps_p[i] = this_vp_p =
254df8bae1dSRodney W. Grimes 			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
255df8bae1dSRodney W. Grimes 		/*
256df8bae1dSRodney W. Grimes 		 * We're not guaranteed that any but the first vnode
257df8bae1dSRodney W. Grimes 		 * are of our type.  Check for and don't map any
258df8bae1dSRodney W. Grimes 		 * that aren't.  (We must always map first vp or vclean fails.)
259df8bae1dSRodney W. Grimes 		 */
260c5e17d9eSKATO Takenori 		if (i && (*this_vp_p == NULLVP ||
261aec0fb7bSPoul-Henning Kamp 		    (*this_vp_p)->v_op != &null_vnodeops)) {
262c5e17d9eSKATO Takenori 			old_vps[i] = NULLVP;
263df8bae1dSRodney W. Grimes 		} else {
264df8bae1dSRodney W. Grimes 			old_vps[i] = *this_vp_p;
265df8bae1dSRodney W. Grimes 			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
266df8bae1dSRodney W. Grimes 			/*
267df8bae1dSRodney W. Grimes 			 * XXX - Several operations have the side effect
268df8bae1dSRodney W. Grimes 			 * of vrele'ing their vp's.  We must account for
269df8bae1dSRodney W. Grimes 			 * that.  (This should go away in the future.)
270df8bae1dSRodney W. Grimes 			 */
2714451405fSBoris Popov 			if (reles & VDESC_VP0_WILLRELE)
272df8bae1dSRodney W. Grimes 				VREF(*this_vp_p);
273df8bae1dSRodney W. Grimes 		}
274df8bae1dSRodney W. Grimes 
275df8bae1dSRodney W. Grimes 	}
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes 	/*
278df8bae1dSRodney W. Grimes 	 * Call the operation on the lower layer
279df8bae1dSRodney W. Grimes 	 * with the modified argument structure.
280df8bae1dSRodney W. Grimes 	 */
2814451405fSBoris Popov 	if (vps_p[0] && *vps_p[0])
28263f89abfSPoul-Henning Kamp 		error = VCALL(ap);
2834451405fSBoris Popov 	else {
2844451405fSBoris Popov 		printf("null_bypass: no map for %s\n", descp->vdesc_name);
2854451405fSBoris Popov 		error = EINVAL;
2864451405fSBoris Popov 	}
287df8bae1dSRodney W. Grimes 
288df8bae1dSRodney W. Grimes 	/*
289df8bae1dSRodney W. Grimes 	 * Maintain the illusion of call-by-value
290df8bae1dSRodney W. Grimes 	 * by restoring vnodes in the argument structure
291df8bae1dSRodney W. Grimes 	 * to their original value.
292df8bae1dSRodney W. Grimes 	 */
293df8bae1dSRodney W. Grimes 	reles = descp->vdesc_flags;
294df8bae1dSRodney W. Grimes 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
295df8bae1dSRodney W. Grimes 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
296df8bae1dSRodney W. Grimes 			break;   /* bail out at end of list */
297df8bae1dSRodney W. Grimes 		if (old_vps[i]) {
298df8bae1dSRodney W. Grimes 			*(vps_p[i]) = old_vps[i];
2994451405fSBoris Popov #if 0
3004451405fSBoris Popov 			if (reles & VDESC_VP0_WILLUNLOCK)
30122db15c0SAttilio Rao 				VOP_UNLOCK(*(vps_p[i]), 0);
3024451405fSBoris Popov #endif
3034451405fSBoris Popov 			if (reles & VDESC_VP0_WILLRELE)
304df8bae1dSRodney W. Grimes 				vrele(*(vps_p[i]));
305df8bae1dSRodney W. Grimes 		}
306df8bae1dSRodney W. Grimes 	}
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 	/*
309df8bae1dSRodney W. Grimes 	 * Map the possible out-going vpp
310df8bae1dSRodney W. Grimes 	 * (Assumes that the lower layer always returns
311df8bae1dSRodney W. Grimes 	 * a VREF'ed vpp unless it gets an error.)
312df8bae1dSRodney W. Grimes 	 */
313df8bae1dSRodney W. Grimes 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
314df8bae1dSRodney W. Grimes 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
315df8bae1dSRodney W. Grimes 	    !error) {
316df8bae1dSRodney W. Grimes 		/*
317df8bae1dSRodney W. Grimes 		 * XXX - even though some ops have vpp returned vp's,
318df8bae1dSRodney W. Grimes 		 * several ops actually vrele this before returning.
319df8bae1dSRodney W. Grimes 		 * We must avoid these ops.
320df8bae1dSRodney W. Grimes 		 * (This should go away when these ops are regularized.)
321df8bae1dSRodney W. Grimes 		 */
322df8bae1dSRodney W. Grimes 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
323df8bae1dSRodney W. Grimes 			goto out;
324df8bae1dSRodney W. Grimes 		vppp = VOPARG_OFFSETTO(struct vnode***,
325df8bae1dSRodney W. Grimes 				 descp->vdesc_vpp_offset,ap);
32618ecdb58SPoul-Henning Kamp 		if (*vppp)
3271cfdefbbSSemen Ustimenko 			error = null_nodeget(old_vps[0]->v_mount, **vppp, *vppp);
328df8bae1dSRodney W. Grimes 	}
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes  out:
331df8bae1dSRodney W. Grimes 	return (error);
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334140dedb8SKonstantin Belousov static int
335140dedb8SKonstantin Belousov null_add_writecount(struct vop_add_writecount_args *ap)
336140dedb8SKonstantin Belousov {
337140dedb8SKonstantin Belousov 	struct vnode *lvp, *vp;
338140dedb8SKonstantin Belousov 	int error;
339140dedb8SKonstantin Belousov 
340140dedb8SKonstantin Belousov 	vp = ap->a_vp;
341140dedb8SKonstantin Belousov 	lvp = NULLVPTOLOWERVP(vp);
34278022527SKonstantin Belousov 	VI_LOCK(vp);
34378022527SKonstantin Belousov 	/* text refs are bypassed to lowervp */
34478022527SKonstantin Belousov 	VNASSERT(vp->v_writecount >= 0, vp, ("wrong null writecount"));
34578022527SKonstantin Belousov 	VNASSERT(vp->v_writecount + ap->a_inc >= 0, vp,
34678022527SKonstantin Belousov 	    ("wrong writecount inc %d", ap->a_inc));
34778022527SKonstantin Belousov 	error = VOP_ADD_WRITECOUNT(lvp, ap->a_inc);
348140dedb8SKonstantin Belousov 	if (error == 0)
349140dedb8SKonstantin Belousov 		vp->v_writecount += ap->a_inc;
35078022527SKonstantin Belousov 	VI_UNLOCK(vp);
351140dedb8SKonstantin Belousov 	return (error);
352140dedb8SKonstantin Belousov }
353140dedb8SKonstantin Belousov 
354996c772fSJohn Dyson /*
355996c772fSJohn Dyson  * We have to carry on the locking protocol on the null layer vnodes
356996c772fSJohn Dyson  * as we progress through the tree. We also have to enforce read-only
357996c772fSJohn Dyson  * if this layer is mounted read-only.
358996c772fSJohn Dyson  */
359996c772fSJohn Dyson static int
360a515233fSPoul-Henning Kamp null_lookup(struct vop_lookup_args *ap)
361996c772fSJohn Dyson {
362996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
3634451405fSBoris Popov 	struct vnode *dvp = ap->a_dvp;
364996c772fSJohn Dyson 	int flags = cnp->cn_flags;
3654451405fSBoris Popov 	struct vnode *vp, *ldvp, *lvp;
366effc6a35SKonstantin Belousov 	struct mount *mp;
367996c772fSJohn Dyson 	int error;
368996c772fSJohn Dyson 
369effc6a35SKonstantin Belousov 	mp = dvp->v_mount;
370effc6a35SKonstantin Belousov 	if ((flags & ISLASTCN) != 0 && (mp->mnt_flag & MNT_RDONLY) != 0 &&
371996c772fSJohn Dyson 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
372996c772fSJohn Dyson 		return (EROFS);
3734451405fSBoris Popov 	/*
3744451405fSBoris Popov 	 * Although it is possible to call null_bypass(), we'll do
3754451405fSBoris Popov 	 * a direct call to reduce overhead
3764451405fSBoris Popov 	 */
3774451405fSBoris Popov 	ldvp = NULLVPTOLOWERVP(dvp);
3784451405fSBoris Popov 	vp = lvp = NULL;
3790ebe0000SKonstantin Belousov 	KASSERT((ldvp->v_vflag & VV_ROOT) == 0 ||
3800ebe0000SKonstantin Belousov 	    ((dvp->v_vflag & VV_ROOT) != 0 && (flags & ISDOTDOT) == 0),
3810ebe0000SKonstantin Belousov 	    ("ldvp %p fl %#x dvp %p fl %#x flags %#x", ldvp, ldvp->v_vflag,
3820ebe0000SKonstantin Belousov 	     dvp, dvp->v_vflag, flags));
383effc6a35SKonstantin Belousov 
384effc6a35SKonstantin Belousov 	/*
385effc6a35SKonstantin Belousov 	 * Hold ldvp.  The reference on it, owned by dvp, is lost in
386effc6a35SKonstantin Belousov 	 * case of dvp reclamation, and we need ldvp to move our lock
387effc6a35SKonstantin Belousov 	 * from ldvp to dvp.
388effc6a35SKonstantin Belousov 	 */
389effc6a35SKonstantin Belousov 	vhold(ldvp);
390effc6a35SKonstantin Belousov 
3914451405fSBoris Popov 	error = VOP_LOOKUP(ldvp, &lvp, cnp);
392effc6a35SKonstantin Belousov 
393effc6a35SKonstantin Belousov 	/*
394effc6a35SKonstantin Belousov 	 * VOP_LOOKUP() on lower vnode may unlock ldvp, which allows
395effc6a35SKonstantin Belousov 	 * dvp to be reclaimed due to shared v_vnlock.  Check for the
396effc6a35SKonstantin Belousov 	 * doomed state and return error.
397effc6a35SKonstantin Belousov 	 */
398effc6a35SKonstantin Belousov 	if ((error == 0 || error == EJUSTRETURN) &&
399effc6a35SKonstantin Belousov 	    (dvp->v_iflag & VI_DOOMED) != 0) {
400effc6a35SKonstantin Belousov 		error = ENOENT;
401effc6a35SKonstantin Belousov 		if (lvp != NULL)
402effc6a35SKonstantin Belousov 			vput(lvp);
403effc6a35SKonstantin Belousov 
404effc6a35SKonstantin Belousov 		/*
405effc6a35SKonstantin Belousov 		 * If vgone() did reclaimed dvp before curthread
406effc6a35SKonstantin Belousov 		 * relocked ldvp, the locks of dvp and ldpv are no
407effc6a35SKonstantin Belousov 		 * longer shared.  In this case, relock of ldvp in
408effc6a35SKonstantin Belousov 		 * lower fs VOP_LOOKUP() does not restore the locking
409effc6a35SKonstantin Belousov 		 * state of dvp.  Compensate for this by unlocking
410effc6a35SKonstantin Belousov 		 * ldvp and locking dvp, which is also correct if the
411effc6a35SKonstantin Belousov 		 * locks are still shared.
412effc6a35SKonstantin Belousov 		 */
413effc6a35SKonstantin Belousov 		VOP_UNLOCK(ldvp, 0);
414effc6a35SKonstantin Belousov 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
415effc6a35SKonstantin Belousov 	}
416effc6a35SKonstantin Belousov 	vdrop(ldvp);
417effc6a35SKonstantin Belousov 
418effc6a35SKonstantin Belousov 	if (error == EJUSTRETURN && (flags & ISLASTCN) != 0 &&
419effc6a35SKonstantin Belousov 	    (mp->mnt_flag & MNT_RDONLY) != 0 &&
420996c772fSJohn Dyson 	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
421996c772fSJohn Dyson 		error = EROFS;
4224451405fSBoris Popov 
4234451405fSBoris Popov 	if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
4244451405fSBoris Popov 		if (ldvp == lvp) {
4254451405fSBoris Popov 			*ap->a_vpp = dvp;
4264451405fSBoris Popov 			VREF(dvp);
4274451405fSBoris Popov 			vrele(lvp);
4284451405fSBoris Popov 		} else {
429effc6a35SKonstantin Belousov 			error = null_nodeget(mp, lvp, &vp);
430dd0f9532SKonstantin Belousov 			if (error == 0)
4314451405fSBoris Popov 				*ap->a_vpp = vp;
432996c772fSJohn Dyson 		}
433996c772fSJohn Dyson 	}
434996c772fSJohn Dyson 	return (error);
435996c772fSJohn Dyson }
436996c772fSJohn Dyson 
437c683c4eeSPoul-Henning Kamp static int
438c683c4eeSPoul-Henning Kamp null_open(struct vop_open_args *ap)
439c683c4eeSPoul-Henning Kamp {
440c683c4eeSPoul-Henning Kamp 	int retval;
441c683c4eeSPoul-Henning Kamp 	struct vnode *vp, *ldvp;
442c683c4eeSPoul-Henning Kamp 
443c683c4eeSPoul-Henning Kamp 	vp = ap->a_vp;
444c683c4eeSPoul-Henning Kamp 	ldvp = NULLVPTOLOWERVP(vp);
445c683c4eeSPoul-Henning Kamp 	retval = null_bypass(&ap->a_gen);
446c683c4eeSPoul-Henning Kamp 	if (retval == 0)
447c683c4eeSPoul-Henning Kamp 		vp->v_object = ldvp->v_object;
448c683c4eeSPoul-Henning Kamp 	return (retval);
449c683c4eeSPoul-Henning Kamp }
450c683c4eeSPoul-Henning Kamp 
451996c772fSJohn Dyson /*
452996c772fSJohn Dyson  * Setattr call. Disallow write attempts if the layer is mounted read-only.
453996c772fSJohn Dyson  */
454fcf54942SPoul-Henning Kamp static int
455a515233fSPoul-Henning Kamp null_setattr(struct vop_setattr_args *ap)
456996c772fSJohn Dyson {
457996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
458996c772fSJohn Dyson 	struct vattr *vap = ap->a_vap;
459996c772fSJohn Dyson 
460996c772fSJohn Dyson   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
46163f50488SMike Pritchard 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
46263f50488SMike Pritchard 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
463996c772fSJohn Dyson 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
464996c772fSJohn Dyson 		return (EROFS);
465996c772fSJohn Dyson 	if (vap->va_size != VNOVAL) {
466996c772fSJohn Dyson  		switch (vp->v_type) {
467996c772fSJohn Dyson  		case VDIR:
468996c772fSJohn Dyson  			return (EISDIR);
469996c772fSJohn Dyson  		case VCHR:
470996c772fSJohn Dyson  		case VBLK:
471996c772fSJohn Dyson  		case VSOCK:
472996c772fSJohn Dyson  		case VFIFO:
4737a204420SPeter Wemm 			if (vap->va_flags != VNOVAL)
4747a204420SPeter Wemm 				return (EOPNOTSUPP);
475996c772fSJohn Dyson 			return (0);
476996c772fSJohn Dyson 		case VREG:
477996c772fSJohn Dyson 		case VLNK:
478996c772fSJohn Dyson  		default:
479996c772fSJohn Dyson 			/*
480996c772fSJohn Dyson 			 * Disallow write attempts if the filesystem is
481996c772fSJohn Dyson 			 * mounted read-only.
482996c772fSJohn Dyson 			 */
483996c772fSJohn Dyson 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
484996c772fSJohn Dyson 				return (EROFS);
485996c772fSJohn Dyson 		}
486996c772fSJohn Dyson 	}
4874451405fSBoris Popov 
488f3a778f2SMike Pritchard 	return (null_bypass((struct vop_generic_args *)ap));
489996c772fSJohn Dyson }
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes /*
492df8bae1dSRodney W. Grimes  *  We handle getattr only to change the fsid.
493df8bae1dSRodney W. Grimes  */
494d4b7a369SPoul-Henning Kamp static int
495a515233fSPoul-Henning Kamp null_getattr(struct vop_getattr_args *ap)
496df8bae1dSRodney W. Grimes {
497df8bae1dSRodney W. Grimes 	int error;
498996c772fSJohn Dyson 
4998aef1712SMatthew Dillon 	if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
500df8bae1dSRodney W. Grimes 		return (error);
5018da80660SBoris Popov 
5028da80660SBoris Popov 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
503df8bae1dSRodney W. Grimes 	return (0);
504df8bae1dSRodney W. Grimes }
505df8bae1dSRodney W. Grimes 
5064451405fSBoris Popov /*
5074451405fSBoris Popov  * Handle to disallow write access if mounted read-only.
5084451405fSBoris Popov  */
509d4b7a369SPoul-Henning Kamp static int
510a515233fSPoul-Henning Kamp null_access(struct vop_access_args *ap)
511996c772fSJohn Dyson {
512996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
51315bc6b2bSEdward Tomasz Napierala 	accmode_t accmode = ap->a_accmode;
514996c772fSJohn Dyson 
515996c772fSJohn Dyson 	/*
516996c772fSJohn Dyson 	 * Disallow write attempts on read-only layers;
517996c772fSJohn Dyson 	 * unless the file is a socket, fifo, or a block or
518996c772fSJohn Dyson 	 * character device resident on the filesystem.
519996c772fSJohn Dyson 	 */
52015bc6b2bSEdward Tomasz Napierala 	if (accmode & VWRITE) {
521996c772fSJohn Dyson 		switch (vp->v_type) {
522996c772fSJohn Dyson 		case VDIR:
523996c772fSJohn Dyson 		case VLNK:
524996c772fSJohn Dyson 		case VREG:
525996c772fSJohn Dyson 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
526996c772fSJohn Dyson 				return (EROFS);
527996c772fSJohn Dyson 			break;
528831a80b0SMatthew Dillon 		default:
529831a80b0SMatthew Dillon 			break;
530996c772fSJohn Dyson 		}
531996c772fSJohn Dyson 	}
532f3a778f2SMike Pritchard 	return (null_bypass((struct vop_generic_args *)ap));
533996c772fSJohn Dyson }
534996c772fSJohn Dyson 
535c97fcdbaSEdward Tomasz Napierala static int
536c97fcdbaSEdward Tomasz Napierala null_accessx(struct vop_accessx_args *ap)
537c97fcdbaSEdward Tomasz Napierala {
538c97fcdbaSEdward Tomasz Napierala 	struct vnode *vp = ap->a_vp;
539c97fcdbaSEdward Tomasz Napierala 	accmode_t accmode = ap->a_accmode;
540c97fcdbaSEdward Tomasz Napierala 
541c97fcdbaSEdward Tomasz Napierala 	/*
542c97fcdbaSEdward Tomasz Napierala 	 * Disallow write attempts on read-only layers;
543c97fcdbaSEdward Tomasz Napierala 	 * unless the file is a socket, fifo, or a block or
544c97fcdbaSEdward Tomasz Napierala 	 * character device resident on the filesystem.
545c97fcdbaSEdward Tomasz Napierala 	 */
546c97fcdbaSEdward Tomasz Napierala 	if (accmode & VWRITE) {
547c97fcdbaSEdward Tomasz Napierala 		switch (vp->v_type) {
548c97fcdbaSEdward Tomasz Napierala 		case VDIR:
549c97fcdbaSEdward Tomasz Napierala 		case VLNK:
550c97fcdbaSEdward Tomasz Napierala 		case VREG:
551c97fcdbaSEdward Tomasz Napierala 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
552c97fcdbaSEdward Tomasz Napierala 				return (EROFS);
553c97fcdbaSEdward Tomasz Napierala 			break;
554c97fcdbaSEdward Tomasz Napierala 		default:
555c97fcdbaSEdward Tomasz Napierala 			break;
556c97fcdbaSEdward Tomasz Napierala 		}
557c97fcdbaSEdward Tomasz Napierala 	}
558c97fcdbaSEdward Tomasz Napierala 	return (null_bypass((struct vop_generic_args *)ap));
559c97fcdbaSEdward Tomasz Napierala }
560c97fcdbaSEdward Tomasz Napierala 
561996c772fSJohn Dyson /*
5622d0c83b1SRick Macklem  * Increasing refcount of lower vnode is needed at least for the case
5632d0c83b1SRick Macklem  * when lower FS is NFS to do sillyrename if the file is in use.
5642d0c83b1SRick Macklem  * Unfortunately v_usecount is incremented in many places in
5652d0c83b1SRick Macklem  * the kernel and, as such, there may be races that result in
5662d0c83b1SRick Macklem  * the NFS client doing an extraneous silly rename, but that seems
5672d0c83b1SRick Macklem  * preferable to not doing a silly rename when it is needed.
5682d0c83b1SRick Macklem  */
5692d0c83b1SRick Macklem static int
5702d0c83b1SRick Macklem null_remove(struct vop_remove_args *ap)
5712d0c83b1SRick Macklem {
5722d0c83b1SRick Macklem 	int retval, vreleit;
5736f73b583SKonstantin Belousov 	struct vnode *lvp, *vp;
5742d0c83b1SRick Macklem 
5756f73b583SKonstantin Belousov 	vp = ap->a_vp;
5766f73b583SKonstantin Belousov 	if (vrefcnt(vp) > 1) {
5776f73b583SKonstantin Belousov 		lvp = NULLVPTOLOWERVP(vp);
5782d0c83b1SRick Macklem 		VREF(lvp);
5792d0c83b1SRick Macklem 		vreleit = 1;
5802d0c83b1SRick Macklem 	} else
5812d0c83b1SRick Macklem 		vreleit = 0;
5826f73b583SKonstantin Belousov 	VTONULL(vp)->null_flags |= NULLV_DROP;
5832d0c83b1SRick Macklem 	retval = null_bypass(&ap->a_gen);
5842d0c83b1SRick Macklem 	if (vreleit != 0)
5852d0c83b1SRick Macklem 		vrele(lvp);
5862d0c83b1SRick Macklem 	return (retval);
5872d0c83b1SRick Macklem }
5882d0c83b1SRick Macklem 
5892d0c83b1SRick Macklem /*
5908da80660SBoris Popov  * We handle this to eliminate null FS to lower FS
5918da80660SBoris Popov  * file moving. Don't know why we don't allow this,
5928da80660SBoris Popov  * possibly we should.
5938da80660SBoris Popov  */
5948da80660SBoris Popov static int
595a515233fSPoul-Henning Kamp null_rename(struct vop_rename_args *ap)
5968da80660SBoris Popov {
5978da80660SBoris Popov 	struct vnode *tdvp = ap->a_tdvp;
5988da80660SBoris Popov 	struct vnode *fvp = ap->a_fvp;
5998da80660SBoris Popov 	struct vnode *fdvp = ap->a_fdvp;
6008da80660SBoris Popov 	struct vnode *tvp = ap->a_tvp;
60118a8d3d7SKonstantin Belousov 	struct null_node *tnn;
6028da80660SBoris Popov 
6038da80660SBoris Popov 	/* Check for cross-device rename. */
6048da80660SBoris Popov 	if ((fvp->v_mount != tdvp->v_mount) ||
6058da80660SBoris Popov 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
6068da80660SBoris Popov 		if (tdvp == tvp)
6078da80660SBoris Popov 			vrele(tdvp);
6088da80660SBoris Popov 		else
6098da80660SBoris Popov 			vput(tdvp);
6108da80660SBoris Popov 		if (tvp)
6118da80660SBoris Popov 			vput(tvp);
6128da80660SBoris Popov 		vrele(fdvp);
6138da80660SBoris Popov 		vrele(fvp);
6148da80660SBoris Popov 		return (EXDEV);
6158da80660SBoris Popov 	}
6168da80660SBoris Popov 
61718a8d3d7SKonstantin Belousov 	if (tvp != NULL) {
61818a8d3d7SKonstantin Belousov 		tnn = VTONULL(tvp);
61918a8d3d7SKonstantin Belousov 		tnn->null_flags |= NULLV_DROP;
62018a8d3d7SKonstantin Belousov 	}
6218da80660SBoris Popov 	return (null_bypass((struct vop_generic_args *)ap));
6228da80660SBoris Popov }
6238da80660SBoris Popov 
624830cd4b8SKonstantin Belousov static int
625830cd4b8SKonstantin Belousov null_rmdir(struct vop_rmdir_args *ap)
626830cd4b8SKonstantin Belousov {
627830cd4b8SKonstantin Belousov 
628830cd4b8SKonstantin Belousov 	VTONULL(ap->a_vp)->null_flags |= NULLV_DROP;
629830cd4b8SKonstantin Belousov 	return (null_bypass(&ap->a_gen));
630830cd4b8SKonstantin Belousov }
631830cd4b8SKonstantin Belousov 
6328da80660SBoris Popov /*
633996c772fSJohn Dyson  * We need to process our own vnode lock and then clear the
634996c772fSJohn Dyson  * interlock flag as it applies only to our vnode, not the
635996c772fSJohn Dyson  * vnodes below us on the stack.
636996c772fSJohn Dyson  */
63763f50488SMike Pritchard static int
638d413d210SKonstantin Belousov null_lock(struct vop_lock1_args *ap)
639996c772fSJohn Dyson {
6404451405fSBoris Popov 	struct vnode *vp = ap->a_vp;
641e0f4540aSMateusz Guzik 	int flags;
642bc855512SJeff Roberson 	struct null_node *nn;
6434451405fSBoris Popov 	struct vnode *lvp;
6444451405fSBoris Popov 	int error;
645996c772fSJohn Dyson 
646e0f4540aSMateusz Guzik 	if ((ap->a_flags & LK_INTERLOCK) == 0)
6478da00465SJeff Roberson 		VI_LOCK(vp);
648e0f4540aSMateusz Guzik 	else
649e0f4540aSMateusz Guzik 		ap->a_flags &= ~LK_INTERLOCK;
650e0f4540aSMateusz Guzik 	flags = ap->a_flags;
65154939875STim J. Robbins 	nn = VTONULL(vp);
65254939875STim J. Robbins 	/*
653bc855512SJeff Roberson 	 * If we're still active we must ask the lower layer to
65428323addSBryan Drewery 	 * lock as ffs has special lock considerations in its
655bc855512SJeff Roberson 	 * vop lock.
65654939875STim J. Robbins 	 */
657bc855512SJeff Roberson 	if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
6584451405fSBoris Popov 		/*
659bc855512SJeff Roberson 		 * We have to hold the vnode here to solve a potential
660bc855512SJeff Roberson 		 * reclaim race.  If we're forcibly vgone'd while we
661bc855512SJeff Roberson 		 * still have refs, a thread could be sleeping inside
662bc855512SJeff Roberson 		 * the lowervp's vop_lock routine.  When we vgone we will
663bc855512SJeff Roberson 		 * drop our last ref to the lowervp, which would allow it
664bc855512SJeff Roberson 		 * to be reclaimed.  The lowervp could then be recycled,
66528323addSBryan Drewery 		 * in which case it is not legal to be sleeping in its VOP.
666bc855512SJeff Roberson 		 * We prevent it from being recycled by holding the vnode
667bc855512SJeff Roberson 		 * here.
6684451405fSBoris Popov 		 */
66933d46a3cSMateusz Guzik 		vholdnz(lvp);
670e0f4540aSMateusz Guzik 		VI_UNLOCK(vp);
67122db15c0SAttilio Rao 		error = VOP_LOCK(lvp, flags);
672d11c07baSAlexander Kabaev 
673d11c07baSAlexander Kabaev 		/*
674d11c07baSAlexander Kabaev 		 * We might have slept to get the lock and someone might have
675d11c07baSAlexander Kabaev 		 * clean our vnode already, switching vnode lock from one in
676d11c07baSAlexander Kabaev 		 * lowervp to v_lock in our own vnode structure.  Handle this
677d11c07baSAlexander Kabaev 		 * case by reacquiring correct lock in requested mode.
678d11c07baSAlexander Kabaev 		 */
679d11c07baSAlexander Kabaev 		if (VTONULL(vp) == NULL && error == 0) {
680e0f4540aSMateusz Guzik 			ap->a_flags &= ~LK_TYPE_MASK;
681d11c07baSAlexander Kabaev 			switch (flags & LK_TYPE_MASK) {
682d11c07baSAlexander Kabaev 			case LK_SHARED:
683d11c07baSAlexander Kabaev 				ap->a_flags |= LK_SHARED;
684d11c07baSAlexander Kabaev 				break;
685d11c07baSAlexander Kabaev 			case LK_UPGRADE:
686d11c07baSAlexander Kabaev 			case LK_EXCLUSIVE:
687d11c07baSAlexander Kabaev 				ap->a_flags |= LK_EXCLUSIVE;
688d11c07baSAlexander Kabaev 				break;
689d11c07baSAlexander Kabaev 			default:
690d11c07baSAlexander Kabaev 				panic("Unsupported lock request %d\n",
691d11c07baSAlexander Kabaev 				    ap->a_flags);
692d11c07baSAlexander Kabaev 			}
69322db15c0SAttilio Rao 			VOP_UNLOCK(lvp, 0);
694d11c07baSAlexander Kabaev 			error = vop_stdlock(ap);
695d11c07baSAlexander Kabaev 		}
696bc855512SJeff Roberson 		vdrop(lvp);
697e0f4540aSMateusz Guzik 	} else {
698e0f4540aSMateusz Guzik 		VI_UNLOCK(vp);
699bc855512SJeff Roberson 		error = vop_stdlock(ap);
700e0f4540aSMateusz Guzik 	}
701bc855512SJeff Roberson 
7024451405fSBoris Popov 	return (error);
703996c772fSJohn Dyson }
704996c772fSJohn Dyson 
705996c772fSJohn Dyson /*
706996c772fSJohn Dyson  * We need to process our own vnode unlock and then clear the
707996c772fSJohn Dyson  * interlock flag as it applies only to our vnode, not the
708996c772fSJohn Dyson  * vnodes below us on the stack.
709996c772fSJohn Dyson  */
71063f50488SMike Pritchard static int
711a515233fSPoul-Henning Kamp null_unlock(struct vop_unlock_args *ap)
712996c772fSJohn Dyson {
7134451405fSBoris Popov 	struct vnode *vp = ap->a_vp;
714bc855512SJeff Roberson 	struct null_node *nn;
715611025ecSBoris Popov 	struct vnode *lvp;
716bc855512SJeff Roberson 	int error;
7174451405fSBoris Popov 
718bc855512SJeff Roberson 	nn = VTONULL(vp);
71910166260SDaichi GOTO 	if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
72033d46a3cSMateusz Guzik 		vholdnz(lvp);
72133d46a3cSMateusz Guzik 		error = VOP_UNLOCK(lvp, 0);
72210166260SDaichi GOTO 		vdrop(lvp);
72310166260SDaichi GOTO 	} else {
724bc855512SJeff Roberson 		error = vop_stdunlock(ap);
72510166260SDaichi GOTO 	}
726bc855512SJeff Roberson 
727bc855512SJeff Roberson 	return (error);
728996c772fSJohn Dyson }
729996c772fSJohn Dyson 
7304451405fSBoris Popov /*
73182ed933cSKonstantin Belousov  * Do not allow the VOP_INACTIVE to be passed to the lower layer,
73282ed933cSKonstantin Belousov  * since the reference count on the lower vnode is not related to
73382ed933cSKonstantin Belousov  * ours.
7344451405fSBoris Popov  */
7354451405fSBoris Popov static int
73613c73428SMateusz Guzik null_want_recycle(struct vnode *vp)
737df8bae1dSRodney W. Grimes {
73813c73428SMateusz Guzik 	struct vnode *lvp;
7390fc6daa7SKonstantin Belousov 	struct null_node *xp;
7409cf4c952SKonstantin Belousov 	struct mount *mp;
7419cf4c952SKonstantin Belousov 	struct null_mount *xmp;
742ebe0bdddSSemen Ustimenko 
7430fc6daa7SKonstantin Belousov 	xp = VTONULL(vp);
7440fc6daa7SKonstantin Belousov 	lvp = NULLVPTOLOWERVP(vp);
7459cf4c952SKonstantin Belousov 	mp = vp->v_mount;
7469cf4c952SKonstantin Belousov 	xmp = MOUNTTONULLMOUNT(mp);
7470fc6daa7SKonstantin Belousov 	if ((xmp->nullm_flags & NULLM_CACHE) == 0 ||
7480fc6daa7SKonstantin Belousov 	    (xp->null_flags & NULLV_DROP) != 0 ||
7490fc6daa7SKonstantin Belousov 	    (lvp->v_vflag & VV_NOSYNC) != 0) {
7509cf4c952SKonstantin Belousov 		/*
7519cf4c952SKonstantin Belousov 		 * If this is the last reference and caching of the
7520fc6daa7SKonstantin Belousov 		 * nullfs vnodes is not enabled, or the lower vnode is
7530fc6daa7SKonstantin Belousov 		 * deleted, then free up the vnode so as not to tie up
7540fc6daa7SKonstantin Belousov 		 * the lower vnodes.
7559cf4c952SKonstantin Belousov 		 */
75613c73428SMateusz Guzik 		return (1);
75713c73428SMateusz Guzik 	}
75813c73428SMateusz Guzik 	return (0);
75913c73428SMateusz Guzik }
76013c73428SMateusz Guzik 
76113c73428SMateusz Guzik static int
76213c73428SMateusz Guzik null_inactive(struct vop_inactive_args *ap)
76313c73428SMateusz Guzik {
76413c73428SMateusz Guzik 	struct vnode *vp;
76513c73428SMateusz Guzik 
76613c73428SMateusz Guzik 	vp = ap->a_vp;
76713c73428SMateusz Guzik 	if (null_want_recycle(vp)) {
7689cf4c952SKonstantin Belousov 		vp->v_object = NULL;
7699cf4c952SKonstantin Belousov 		vrecycle(vp);
7709cf4c952SKonstantin Belousov 	}
771df8bae1dSRodney W. Grimes 	return (0);
772df8bae1dSRodney W. Grimes }
773df8bae1dSRodney W. Grimes 
77413c73428SMateusz Guzik static int
77513c73428SMateusz Guzik null_need_inactive(struct vop_need_inactive_args *ap)
77613c73428SMateusz Guzik {
77713c73428SMateusz Guzik 
77813c73428SMateusz Guzik 	return (null_want_recycle(ap->a_vp));
77913c73428SMateusz Guzik }
78013c73428SMateusz Guzik 
7814451405fSBoris Popov /*
782d9e9650aSKonstantin Belousov  * Now, the nullfs vnode and, due to the sharing lock, the lower
783d9e9650aSKonstantin Belousov  * vnode, are exclusively locked, and we shall destroy the null vnode.
7844451405fSBoris Popov  */
785d4b7a369SPoul-Henning Kamp static int
786a515233fSPoul-Henning Kamp null_reclaim(struct vop_reclaim_args *ap)
787df8bae1dSRodney W. Grimes {
788409b12c0SKonstantin Belousov 	struct vnode *vp;
789409b12c0SKonstantin Belousov 	struct null_node *xp;
790409b12c0SKonstantin Belousov 	struct vnode *lowervp;
791df8bae1dSRodney W. Grimes 
792409b12c0SKonstantin Belousov 	vp = ap->a_vp;
793409b12c0SKonstantin Belousov 	xp = VTONULL(vp);
794409b12c0SKonstantin Belousov 	lowervp = xp->null_lowervp;
795409b12c0SKonstantin Belousov 
796409b12c0SKonstantin Belousov 	KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock,
797289dd6ddSKonstantin Belousov 	    ("Reclaiming incomplete null vnode %p", vp));
798409b12c0SKonstantin Belousov 
799f5cacb39SJeff Roberson 	null_hashrem(xp);
800ba731053SJeff Roberson 	/*
801ba731053SJeff Roberson 	 * Use the interlock to protect the clearing of v_data to
802ba731053SJeff Roberson 	 * prevent faults in null_lock().
803ba731053SJeff Roberson 	 */
804cec9ed6dSKonstantin Belousov 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
805ba731053SJeff Roberson 	VI_LOCK(vp);
806ba731053SJeff Roberson 	vp->v_data = NULL;
807f5cacb39SJeff Roberson 	vp->v_object = NULL;
808d11c07baSAlexander Kabaev 	vp->v_vnlock = &vp->v_lock;
8096b175951SKonstantin Belousov 
8106b175951SKonstantin Belousov 	/*
81178022527SKonstantin Belousov 	 * If we were opened for write, we leased the write reference
8126b175951SKonstantin Belousov 	 * to the lower vnode.  If this is a reclamation due to the
8136b175951SKonstantin Belousov 	 * forced unmount, undo the reference now.
8146b175951SKonstantin Belousov 	 */
8156b175951SKonstantin Belousov 	if (vp->v_writecount > 0)
81678022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT(lowervp, -vp->v_writecount);
8173c93d227SKonstantin Belousov 	else if (vp->v_writecount < 0)
8183c93d227SKonstantin Belousov 		vp->v_writecount = 0;
81978022527SKonstantin Belousov 
82078022527SKonstantin Belousov 	VI_UNLOCK(vp);
82178022527SKonstantin Belousov 
8220fc6daa7SKonstantin Belousov 	if ((xp->null_flags & NULLV_NOUNLOCK) != 0)
8230fc6daa7SKonstantin Belousov 		vunref(lowervp);
8240fc6daa7SKonstantin Belousov 	else
825cdeb7204SSuleiman Souhlal 		vput(lowervp);
8261ede983cSDag-Erling Smørgrav 	free(xp, M_NULLFSNODE);
8274451405fSBoris Popov 
828df8bae1dSRodney W. Grimes 	return (0);
829df8bae1dSRodney W. Grimes }
830df8bae1dSRodney W. Grimes 
831d4b7a369SPoul-Henning Kamp static int
832a515233fSPoul-Henning Kamp null_print(struct vop_print_args *ap)
833df8bae1dSRodney W. Grimes {
834d34dd851SPoul-Henning Kamp 	struct vnode *vp = ap->a_vp;
835f5cacb39SJeff Roberson 
836f82ee01cSKonstantin Belousov 	printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp);
837df8bae1dSRodney W. Grimes 	return (0);
838df8bae1dSRodney W. Grimes }
839df8bae1dSRodney W. Grimes 
8404bf5133bSJeff Roberson /* ARGSUSED */
8414bf5133bSJeff Roberson static int
8424bf5133bSJeff Roberson null_getwritemount(struct vop_getwritemount_args *ap)
8434bf5133bSJeff Roberson {
8444bf5133bSJeff Roberson 	struct null_node *xp;
8454bf5133bSJeff Roberson 	struct vnode *lowervp;
8464bf5133bSJeff Roberson 	struct vnode *vp;
8474bf5133bSJeff Roberson 
8484bf5133bSJeff Roberson 	vp = ap->a_vp;
8494bf5133bSJeff Roberson 	VI_LOCK(vp);
8504bf5133bSJeff Roberson 	xp = VTONULL(vp);
8514bf5133bSJeff Roberson 	if (xp && (lowervp = xp->null_lowervp)) {
85233d46a3cSMateusz Guzik 		vholdnz(lowervp);
8534bf5133bSJeff Roberson 		VI_UNLOCK(vp);
8544bf5133bSJeff Roberson 		VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);
8554bf5133bSJeff Roberson 		vdrop(lowervp);
8564bf5133bSJeff Roberson 	} else {
8574bf5133bSJeff Roberson 		VI_UNLOCK(vp);
8584bf5133bSJeff Roberson 		*(ap->a_mpp) = NULL;
8594bf5133bSJeff Roberson 	}
8604bf5133bSJeff Roberson 	return (0);
8614bf5133bSJeff Roberson }
8624bf5133bSJeff Roberson 
86310bcafe9SPawel Jakub Dawidek static int
86410bcafe9SPawel Jakub Dawidek null_vptofh(struct vop_vptofh_args *ap)
86510bcafe9SPawel Jakub Dawidek {
86610bcafe9SPawel Jakub Dawidek 	struct vnode *lvp;
86710bcafe9SPawel Jakub Dawidek 
86810bcafe9SPawel Jakub Dawidek 	lvp = NULLVPTOLOWERVP(ap->a_vp);
86910bcafe9SPawel Jakub Dawidek 	return VOP_VPTOFH(lvp, ap->a_fhp);
87010bcafe9SPawel Jakub Dawidek }
87110bcafe9SPawel Jakub Dawidek 
872b0f34bb6SKonstantin Belousov static int
873b0f34bb6SKonstantin Belousov null_vptocnp(struct vop_vptocnp_args *ap)
874b0f34bb6SKonstantin Belousov {
875b0f34bb6SKonstantin Belousov 	struct vnode *vp = ap->a_vp;
876b0f34bb6SKonstantin Belousov 	struct vnode **dvp = ap->a_vpp;
877b0f34bb6SKonstantin Belousov 	struct vnode *lvp, *ldvp;
878c808c963SKonstantin Belousov 	struct ucred *cred = ap->a_cred;
879b9662886SKonstantin Belousov 	struct mount *mp;
880b0f34bb6SKonstantin Belousov 	int error, locked;
881b0f34bb6SKonstantin Belousov 
882b0f34bb6SKonstantin Belousov 	locked = VOP_ISLOCKED(vp);
883b0f34bb6SKonstantin Belousov 	lvp = NULLVPTOLOWERVP(vp);
884b0f34bb6SKonstantin Belousov 	vhold(lvp);
885b9662886SKonstantin Belousov 	mp = vp->v_mount;
886b9662886SKonstantin Belousov 	vfs_ref(mp);
887b0f34bb6SKonstantin Belousov 	VOP_UNLOCK(vp, 0); /* vp is held by vn_vptocnp_locked that called us */
888b0f34bb6SKonstantin Belousov 	ldvp = lvp;
889f82360acSKonstantin Belousov 	vref(lvp);
890c808c963SKonstantin Belousov 	error = vn_vptocnp(&ldvp, cred, ap->a_buf, ap->a_buflen);
891b0f34bb6SKonstantin Belousov 	vdrop(lvp);
892b0f34bb6SKonstantin Belousov 	if (error != 0) {
893b0f34bb6SKonstantin Belousov 		vn_lock(vp, locked | LK_RETRY);
894b9662886SKonstantin Belousov 		vfs_rel(mp);
895b0f34bb6SKonstantin Belousov 		return (ENOENT);
896b0f34bb6SKonstantin Belousov 	}
897b0f34bb6SKonstantin Belousov 
89881f666e7SMateusz Guzik 	error = vn_lock(ldvp, LK_SHARED);
899b0f34bb6SKonstantin Belousov 	if (error != 0) {
900f82360acSKonstantin Belousov 		vrele(ldvp);
901b0f34bb6SKonstantin Belousov 		vn_lock(vp, locked | LK_RETRY);
902b9662886SKonstantin Belousov 		vfs_rel(mp);
903b0f34bb6SKonstantin Belousov 		return (ENOENT);
904b0f34bb6SKonstantin Belousov 	}
905b9662886SKonstantin Belousov 	error = null_nodeget(mp, ldvp, dvp);
906b0f34bb6SKonstantin Belousov 	if (error == 0) {
907b0f34bb6SKonstantin Belousov #ifdef DIAGNOSTIC
908b0f34bb6SKonstantin Belousov 		NULLVPTOLOWERVP(*dvp);
909b0f34bb6SKonstantin Belousov #endif
910f82360acSKonstantin Belousov 		VOP_UNLOCK(*dvp, 0); /* keep reference on *dvp */
911dd0f9532SKonstantin Belousov 	}
912b0f34bb6SKonstantin Belousov 	vn_lock(vp, locked | LK_RETRY);
913b9662886SKonstantin Belousov 	vfs_rel(mp);
914b0f34bb6SKonstantin Belousov 	return (error);
915b0f34bb6SKonstantin Belousov }
916b0f34bb6SKonstantin Belousov 
917df8bae1dSRodney W. Grimes /*
918df8bae1dSRodney W. Grimes  * Global vfs data structures
919df8bae1dSRodney W. Grimes  */
920aec0fb7bSPoul-Henning Kamp struct vop_vector null_vnodeops = {
921aec0fb7bSPoul-Henning Kamp 	.vop_bypass =		null_bypass,
922aec0fb7bSPoul-Henning Kamp 	.vop_access =		null_access,
923c97fcdbaSEdward Tomasz Napierala 	.vop_accessx =		null_accessx,
924de082cd1SKonstantin Belousov 	.vop_advlockpurge =	vop_stdadvlockpurge,
925aec0fb7bSPoul-Henning Kamp 	.vop_bmap =		VOP_EOPNOTSUPP,
926aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		null_getattr,
9274bf5133bSJeff Roberson 	.vop_getwritemount =	null_getwritemount,
928aec0fb7bSPoul-Henning Kamp 	.vop_inactive =		null_inactive,
92913c73428SMateusz Guzik 	.vop_need_inactive =	null_need_inactive,
930aa73f8c7SPeter Holm 	.vop_islocked =		vop_stdislocked,
931d413d210SKonstantin Belousov 	.vop_lock1 =		null_lock,
932aec0fb7bSPoul-Henning Kamp 	.vop_lookup =		null_lookup,
933c683c4eeSPoul-Henning Kamp 	.vop_open =		null_open,
934aec0fb7bSPoul-Henning Kamp 	.vop_print =		null_print,
935aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		null_reclaim,
9362d0c83b1SRick Macklem 	.vop_remove =		null_remove,
937aec0fb7bSPoul-Henning Kamp 	.vop_rename =		null_rename,
938830cd4b8SKonstantin Belousov 	.vop_rmdir =		null_rmdir,
939aec0fb7bSPoul-Henning Kamp 	.vop_setattr =		null_setattr,
940aec0fb7bSPoul-Henning Kamp 	.vop_strategy =		VOP_EOPNOTSUPP,
941aec0fb7bSPoul-Henning Kamp 	.vop_unlock =		null_unlock,
942b0f34bb6SKonstantin Belousov 	.vop_vptocnp =		null_vptocnp,
94310bcafe9SPawel Jakub Dawidek 	.vop_vptofh =		null_vptofh,
944140dedb8SKonstantin Belousov 	.vop_add_writecount =	null_add_writecount,
945df8bae1dSRodney W. Grimes };
946