xref: /freebsd/sys/fs/nullfs/null_vnops.c (revision 326836a1)
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  * Ancestors:
35996c772fSJohn Dyson  *	...and...
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
38df8bae1dSRodney W. Grimes /*
39df8bae1dSRodney W. Grimes  * Null Layer
40df8bae1dSRodney W. Grimes  *
4157a523aeSRuslan Ermilov  * (See mount_nullfs(8) for more information.)
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  * The null layer duplicates a portion of the filesystem
44df8bae1dSRodney W. Grimes  * name space under a new name.  In this respect, it is
45df8bae1dSRodney W. Grimes  * similar to the loopback filesystem.  It differs from
46df8bae1dSRodney W. Grimes  * the loopback fs in two respects:  it is implemented using
47dc733423SDag-Erling Smørgrav  * a stackable layers techniques, and its "null-node"s stack above
48df8bae1dSRodney W. Grimes  * all lower-layer vnodes, not just over directory vnodes.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * The null layer has two purposes.  First, it serves as a demonstration
51df8bae1dSRodney W. Grimes  * of layering by proving a layer which does nothing.  (It actually
52df8bae1dSRodney W. Grimes  * does everything the loopback filesystem does, which is slightly
53df8bae1dSRodney W. Grimes  * more than nothing.)  Second, the null layer can serve as a prototype
54df8bae1dSRodney W. Grimes  * layer.  Since it provides all necessary layer framework,
55df8bae1dSRodney W. Grimes  * new filesystem layers can be created very easily be starting
56df8bae1dSRodney W. Grimes  * with a null layer.
57df8bae1dSRodney W. Grimes  *
58df8bae1dSRodney W. Grimes  * The remainder of this man page examines the null layer as a basis
59df8bae1dSRodney W. Grimes  * for constructing new layers.
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  *
62df8bae1dSRodney W. Grimes  * INSTANTIATING NEW NULL LAYERS
63df8bae1dSRodney W. Grimes  *
6457a523aeSRuslan Ermilov  * New null layers are created with mount_nullfs(8).
6557a523aeSRuslan Ermilov  * Mount_nullfs(8) takes two arguments, the pathname
66df8bae1dSRodney W. Grimes  * of the lower vfs (target-pn) and the pathname where the null
67df8bae1dSRodney W. Grimes  * layer will appear in the namespace (alias-pn).  After
68df8bae1dSRodney W. Grimes  * the null layer is put into place, the contents
69df8bae1dSRodney W. Grimes  * of target-pn subtree will be aliased under alias-pn.
70df8bae1dSRodney W. Grimes  *
71df8bae1dSRodney W. Grimes  *
72df8bae1dSRodney W. Grimes  * OPERATION OF A NULL LAYER
73df8bae1dSRodney W. Grimes  *
74df8bae1dSRodney W. Grimes  * The null layer is the minimum filesystem layer,
75df8bae1dSRodney W. Grimes  * simply bypassing all possible operations to the lower layer
76df8bae1dSRodney W. Grimes  * for processing there.  The majority of its activity centers
7709c8ff4aSAlexander Langer  * on the bypass routine, through which nearly all vnode operations
78df8bae1dSRodney W. Grimes  * pass.
79df8bae1dSRodney W. Grimes  *
80df8bae1dSRodney W. Grimes  * The bypass routine accepts arbitrary vnode operations for
814c399b04SGordon Bergling  * handling by the lower layer.  It begins by examining vnode
82df8bae1dSRodney W. Grimes  * operation arguments and replacing any null-nodes by their
83df8bae1dSRodney W. Grimes  * lower-layer equivlants.  It then invokes the operation
84df8bae1dSRodney W. Grimes  * on the lower layer.  Finally, it replaces the null-nodes
85df8bae1dSRodney W. Grimes  * in the arguments and, if a vnode is return by the operation,
86df8bae1dSRodney W. Grimes  * stacks a null-node on top of the returned vnode.
87df8bae1dSRodney W. Grimes  *
88996c772fSJohn Dyson  * Although bypass handles most operations, vop_getattr, vop_lock,
89996c772fSJohn Dyson  * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
90996c772fSJohn Dyson  * bypassed. Vop_getattr must change the fsid being returned.
91996c772fSJohn Dyson  * Vop_lock and vop_unlock must handle any locking for the
92996c772fSJohn Dyson  * current vnode as well as pass the lock request down.
93df8bae1dSRodney W. Grimes  * Vop_inactive and vop_reclaim are not bypassed so that
94996c772fSJohn Dyson  * they can handle freeing null-layer specific data. Vop_print
95996c772fSJohn Dyson  * is not bypassed to avoid excessive debugging information.
96996c772fSJohn Dyson  * Also, certain vnode operations change the locking state within
97996c772fSJohn Dyson  * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
98996c772fSJohn Dyson  * and symlink). Ideally these operations should not change the
99996c772fSJohn Dyson  * lock state, but should be changed to let the caller of the
100996c772fSJohn Dyson  * function unlock them. Otherwise all intermediate vnode layers
101996c772fSJohn Dyson  * (such as union, umapfs, etc) must catch these functions to do
102996c772fSJohn Dyson  * the necessary locking at their layer.
103df8bae1dSRodney W. Grimes  *
104df8bae1dSRodney W. Grimes  *
105df8bae1dSRodney W. Grimes  * INSTANTIATING VNODE STACKS
106df8bae1dSRodney W. Grimes  *
107df8bae1dSRodney W. Grimes  * Mounting associates the null layer with a lower layer,
108df8bae1dSRodney W. Grimes  * effect stacking two VFSes.  Vnode stacks are instead
109df8bae1dSRodney W. Grimes  * created on demand as files are accessed.
110df8bae1dSRodney W. Grimes  *
111df8bae1dSRodney W. Grimes  * The initial mount creates a single vnode stack for the
112df8bae1dSRodney W. Grimes  * root of the new null layer.  All other vnode stacks
113df8bae1dSRodney W. Grimes  * are created as a result of vnode operations on
114df8bae1dSRodney W. Grimes  * this or other null vnode stacks.
115df8bae1dSRodney W. Grimes  *
116b3a15dddSPedro F. Giffuni  * New vnode stacks come into existence as a result of
117df8bae1dSRodney W. Grimes  * an operation which returns a vnode.
118df8bae1dSRodney W. Grimes  * The bypass routine stacks a null-node above the new
119df8bae1dSRodney W. Grimes  * vnode before returning it to the caller.
120df8bae1dSRodney W. Grimes  *
121df8bae1dSRodney W. Grimes  * For example, imagine mounting a null layer with
12257a523aeSRuslan Ermilov  * "mount_nullfs /usr/include /dev/layer/null".
123df8bae1dSRodney W. Grimes  * Changing directory to /dev/layer/null will assign
124df8bae1dSRodney W. Grimes  * the root null-node (which was created when the null layer was mounted).
125df8bae1dSRodney W. Grimes  * Now consider opening "sys".  A vop_lookup would be
126df8bae1dSRodney W. Grimes  * done on the root null-node.  This operation would bypass through
127df8bae1dSRodney W. Grimes  * to the lower layer which would return a vnode representing
128df8bae1dSRodney W. Grimes  * the UFS "sys".  Null_bypass then builds a null-node
129df8bae1dSRodney W. Grimes  * aliasing the UFS "sys" and returns this to the caller.
130df8bae1dSRodney W. Grimes  * Later operations on the null-node "sys" will repeat this
131df8bae1dSRodney W. Grimes  * process when constructing other vnode stacks.
132df8bae1dSRodney W. Grimes  *
133df8bae1dSRodney W. Grimes  *
134df8bae1dSRodney W. Grimes  * CREATING OTHER FILE SYSTEM LAYERS
135df8bae1dSRodney W. Grimes  *
136df8bae1dSRodney W. Grimes  * One of the easiest ways to construct new filesystem layers is to make
137df8bae1dSRodney W. Grimes  * a copy of the null layer, rename all files and variables, and
138df8bae1dSRodney W. Grimes  * then begin modifing the copy.  Sed can be used to easily rename
139df8bae1dSRodney W. Grimes  * all variables.
140df8bae1dSRodney W. Grimes  *
141df8bae1dSRodney W. Grimes  * The umap layer is an example of a layer descended from the
142df8bae1dSRodney W. Grimes  * null layer.
143df8bae1dSRodney W. Grimes  *
144df8bae1dSRodney W. Grimes  *
145df8bae1dSRodney W. Grimes  * INVOKING OPERATIONS ON LOWER LAYERS
146df8bae1dSRodney W. Grimes  *
147df8bae1dSRodney W. Grimes  * There are two techniques to invoke operations on a lower layer
148df8bae1dSRodney W. Grimes  * when the operation cannot be completely bypassed.  Each method
149df8bae1dSRodney W. Grimes  * is appropriate in different situations.  In both cases,
150df8bae1dSRodney W. Grimes  * it is the responsibility of the aliasing layer to make
151df8bae1dSRodney W. Grimes  * the operation arguments "correct" for the lower layer
152d64ada50SJens Schweikhardt  * by mapping a vnode arguments to the lower layer.
153df8bae1dSRodney W. Grimes  *
154df8bae1dSRodney W. Grimes  * The first approach is to call the aliasing layer's bypass routine.
155df8bae1dSRodney W. Grimes  * This method is most suitable when you wish to invoke the operation
156ee97e537SAlexander Langer  * currently being handled on the lower layer.  It has the advantage
157df8bae1dSRodney W. Grimes  * that the bypass routine already must do argument mapping.
158df8bae1dSRodney W. Grimes  * An example of this is null_getattrs in the null layer.
159df8bae1dSRodney W. Grimes  *
160ee97e537SAlexander Langer  * A second approach is to directly invoke vnode operations on
161df8bae1dSRodney W. Grimes  * the lower layer with the VOP_OPERATIONNAME interface.
162df8bae1dSRodney W. Grimes  * The advantage of this method is that it is easy to invoke
163df8bae1dSRodney W. Grimes  * arbitrary operations on the lower layer.  The disadvantage
164ee97e537SAlexander Langer  * is that vnode arguments must be manualy mapped.
165df8bae1dSRodney W. Grimes  *
166df8bae1dSRodney W. Grimes  */
167df8bae1dSRodney W. Grimes 
168df8bae1dSRodney W. Grimes #include <sys/param.h>
169df8bae1dSRodney W. Grimes #include <sys/systm.h>
1704451405fSBoris Popov #include <sys/conf.h>
171fb919e4dSMark Murray #include <sys/kernel.h>
172fb919e4dSMark Murray #include <sys/lock.h>
173fb919e4dSMark Murray #include <sys/malloc.h>
174fb919e4dSMark Murray #include <sys/mount.h>
175fb919e4dSMark Murray #include <sys/mutex.h>
176fb919e4dSMark Murray #include <sys/namei.h>
177d4b7a369SPoul-Henning Kamp #include <sys/sysctl.h>
178df8bae1dSRodney W. Grimes #include <sys/vnode.h>
179fc9fcee0SMateusz Guzik #include <sys/stat.h>
180fb919e4dSMark Murray 
18199d300a1SRuslan Ermilov #include <fs/nullfs/null.h>
182df8bae1dSRodney W. Grimes 
1834451405fSBoris Popov #include <vm/vm.h>
1844451405fSBoris Popov #include <vm/vm_extern.h>
1854451405fSBoris Popov #include <vm/vm_object.h>
1864451405fSBoris Popov #include <vm/vnode_pager.h>
1874451405fSBoris Popov 
188d4b7a369SPoul-Henning Kamp static int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
189d4b7a369SPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
190d4b7a369SPoul-Henning Kamp 	&null_bug_bypass, 0, "");
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes /*
193df8bae1dSRodney W. Grimes  * This is the 10-Apr-92 bypass routine.
194df8bae1dSRodney W. Grimes  *    This version has been optimized for speed, throwing away some
195df8bae1dSRodney W. Grimes  * safety checks.  It should still always work, but it's not as
196df8bae1dSRodney W. Grimes  * robust to programmer errors.
197df8bae1dSRodney W. Grimes  *
198df8bae1dSRodney W. Grimes  * In general, we map all vnodes going down and unmap them on the way back.
199df8bae1dSRodney W. Grimes  * As an exception to this, vnodes can be marked "unmapped" by setting
200df8bae1dSRodney W. Grimes  * the Nth bit in operation's vdesc_flags.
201df8bae1dSRodney W. Grimes  *
202df8bae1dSRodney W. Grimes  * Also, some BSD vnode operations have the side effect of vrele'ing
203df8bae1dSRodney W. Grimes  * their arguments.  With stacking, the reference counts are held
204df8bae1dSRodney W. Grimes  * by the upper node, not the lower one, so we must handle these
205df8bae1dSRodney W. Grimes  * side-effects here.  This is not of concern in Sun-derived systems
206df8bae1dSRodney W. Grimes  * since there are no such side-effects.
207df8bae1dSRodney W. Grimes  *
208df8bae1dSRodney W. Grimes  * This makes the following assumptions:
209df8bae1dSRodney W. Grimes  * - only one returned vpp
210df8bae1dSRodney W. Grimes  * - no INOUT vpp's (Sun's vop_open has one of these)
211df8bae1dSRodney W. Grimes  * - the vnode operation vector of the first vnode should be used
212df8bae1dSRodney W. Grimes  *   to determine what implementation of the op should be invoked
213df8bae1dSRodney W. Grimes  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
214df8bae1dSRodney W. Grimes  *   problems on rmdir'ing mount points and renaming?)
215df8bae1dSRodney W. Grimes  */
216996c772fSJohn Dyson int
null_bypass(struct vop_generic_args * ap)217a515233fSPoul-Henning Kamp null_bypass(struct vop_generic_args *ap)
218df8bae1dSRodney W. Grimes {
219d34dd851SPoul-Henning Kamp 	struct vnode **this_vp_p;
220df8bae1dSRodney W. Grimes 	struct vnode *old_vps[VDESC_MAX_VPS];
221df8bae1dSRodney W. Grimes 	struct vnode **vps_p[VDESC_MAX_VPS];
222df8bae1dSRodney W. Grimes 	struct vnode ***vppp;
2236b56b0caSKonstantin Belousov 	struct vnode *lvp;
224df8bae1dSRodney W. Grimes 	struct vnodeop_desc *descp = ap->a_desc;
2257b7227c4SKonstantin Belousov 	int error, i, reles;
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes 	if (null_bug_bypass)
228df8bae1dSRodney W. Grimes 		printf ("null_bypass: %s\n", descp->vdesc_name);
229df8bae1dSRodney W. Grimes 
2304047cd0bSBruce Evans #ifdef DIAGNOSTIC
231df8bae1dSRodney W. Grimes 	/*
232df8bae1dSRodney W. Grimes 	 * We require at least one vp.
233df8bae1dSRodney W. Grimes 	 */
234df8bae1dSRodney W. Grimes 	if (descp->vdesc_vp_offsets == NULL ||
235df8bae1dSRodney W. Grimes 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
2364047cd0bSBruce Evans 		panic ("null_bypass: no vp's in map");
237df8bae1dSRodney W. Grimes #endif
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 	/*
240df8bae1dSRodney W. Grimes 	 * Map the vnodes going in.
241df8bae1dSRodney W. Grimes 	 * Later, we'll invoke the operation based on
242df8bae1dSRodney W. Grimes 	 * the first mapped vnode's operation vector.
243df8bae1dSRodney W. Grimes 	 */
244df8bae1dSRodney W. Grimes 	reles = descp->vdesc_flags;
245df8bae1dSRodney W. Grimes 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
246df8bae1dSRodney W. Grimes 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
247df8bae1dSRodney W. Grimes 			break;   /* bail out at end of list */
2487b7227c4SKonstantin Belousov 		vps_p[i] = this_vp_p = VOPARG_OFFSETTO(struct vnode **,
2497b7227c4SKonstantin Belousov 		    descp->vdesc_vp_offsets[i], ap);
2507b7227c4SKonstantin Belousov 
251df8bae1dSRodney W. Grimes 		/*
252df8bae1dSRodney W. Grimes 		 * We're not guaranteed that any but the first vnode
253df8bae1dSRodney W. Grimes 		 * are of our type.  Check for and don't map any
254df8bae1dSRodney W. Grimes 		 * that aren't.  (We must always map first vp or vclean fails.)
255df8bae1dSRodney W. Grimes 		 */
2567b7227c4SKonstantin Belousov 		if (i != 0 && (*this_vp_p == NULLVP ||
257aec0fb7bSPoul-Henning Kamp 		    (*this_vp_p)->v_op != &null_vnodeops)) {
258c5e17d9eSKATO Takenori 			old_vps[i] = NULLVP;
259df8bae1dSRodney W. Grimes 		} else {
260df8bae1dSRodney W. Grimes 			old_vps[i] = *this_vp_p;
261df8bae1dSRodney W. Grimes 			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
2627b7227c4SKonstantin Belousov 
263df8bae1dSRodney W. Grimes 			/*
264d5b07816SKonstantin Belousov 			 * The upper vnode reference to the lower
265d5b07816SKonstantin Belousov 			 * vnode is the only reference that keeps our
266d5b07816SKonstantin Belousov 			 * pointer to the lower vnode alive.  If lower
267d5b07816SKonstantin Belousov 			 * vnode is relocked during the VOP call,
268d5b07816SKonstantin Belousov 			 * upper vnode might become unlocked and
269d5b07816SKonstantin Belousov 			 * reclaimed, which invalidates our reference.
270d5b07816SKonstantin Belousov 			 * Add a transient hold around VOP call.
271d5b07816SKonstantin Belousov 			 */
272d5b07816SKonstantin Belousov 			vhold(*this_vp_p);
273d5b07816SKonstantin Belousov 
274d5b07816SKonstantin Belousov 			/*
275df8bae1dSRodney W. Grimes 			 * XXX - Several operations have the side effect
276df8bae1dSRodney W. Grimes 			 * of vrele'ing their vp's.  We must account for
277df8bae1dSRodney W. Grimes 			 * that.  (This should go away in the future.)
278df8bae1dSRodney W. Grimes 			 */
2794451405fSBoris Popov 			if (reles & VDESC_VP0_WILLRELE)
2807b7227c4SKonstantin Belousov 				vref(*this_vp_p);
281df8bae1dSRodney W. Grimes 		}
282df8bae1dSRodney W. Grimes 	}
283df8bae1dSRodney W. Grimes 
284df8bae1dSRodney W. Grimes 	/*
285df8bae1dSRodney W. Grimes 	 * Call the operation on the lower layer
286df8bae1dSRodney W. Grimes 	 * with the modified argument structure.
287df8bae1dSRodney W. Grimes 	 */
2887b7227c4SKonstantin Belousov 	if (vps_p[0] != NULL && *vps_p[0] != NULL) {
28963f89abfSPoul-Henning Kamp 		error = VCALL(ap);
2907b7227c4SKonstantin Belousov 	} else {
2914451405fSBoris Popov 		printf("null_bypass: no map for %s\n", descp->vdesc_name);
2924451405fSBoris Popov 		error = EINVAL;
2934451405fSBoris Popov 	}
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes 	/*
296df8bae1dSRodney W. Grimes 	 * Maintain the illusion of call-by-value
297df8bae1dSRodney W. Grimes 	 * by restoring vnodes in the argument structure
298df8bae1dSRodney W. Grimes 	 * to their original value.
299df8bae1dSRodney W. Grimes 	 */
300df8bae1dSRodney W. Grimes 	reles = descp->vdesc_flags;
301df8bae1dSRodney W. Grimes 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
302df8bae1dSRodney W. Grimes 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
303df8bae1dSRodney W. Grimes 			break;   /* bail out at end of list */
3047b7227c4SKonstantin Belousov 		if (old_vps[i] != NULL) {
3056b56b0caSKonstantin Belousov 			lvp = *(vps_p[i]);
3066b56b0caSKonstantin Belousov 
3076b56b0caSKonstantin Belousov 			/*
308d5b07816SKonstantin Belousov 			 * Get rid of the transient hold on lvp.
3096b56b0caSKonstantin Belousov 			 * If lowervp was unlocked during VOP
3106b56b0caSKonstantin Belousov 			 * operation, nullfs upper vnode could have
3116b56b0caSKonstantin Belousov 			 * been reclaimed, which changes its v_vnlock
3126b56b0caSKonstantin Belousov 			 * back to private v_lock.  In this case we
3136b56b0caSKonstantin Belousov 			 * must move lock ownership from lower to
3146b56b0caSKonstantin Belousov 			 * upper (reclaimed) vnode.
3156b56b0caSKonstantin Belousov 			 */
316d5b07816SKonstantin Belousov 			if (lvp != NULLVP) {
317d5b07816SKonstantin Belousov 				if (VOP_ISLOCKED(lvp) == LK_EXCLUSIVE &&
3186b56b0caSKonstantin Belousov 				    old_vps[i]->v_vnlock != lvp->v_vnlock) {
3196b56b0caSKonstantin Belousov 					VOP_UNLOCK(lvp);
320d5b07816SKonstantin Belousov 					VOP_LOCK(old_vps[i], LK_EXCLUSIVE |
321d5b07816SKonstantin Belousov 					    LK_RETRY);
322d5b07816SKonstantin Belousov 				}
323d5b07816SKonstantin Belousov 				vdrop(lvp);
3246b56b0caSKonstantin Belousov 			}
3256b56b0caSKonstantin Belousov 
326df8bae1dSRodney W. Grimes 			*(vps_p[i]) = old_vps[i];
3274451405fSBoris Popov #if 0
3284451405fSBoris Popov 			if (reles & VDESC_VP0_WILLUNLOCK)
32922db15c0SAttilio Rao 				VOP_UNLOCK(*(vps_p[i]), 0);
3304451405fSBoris Popov #endif
3314451405fSBoris Popov 			if (reles & VDESC_VP0_WILLRELE)
332df8bae1dSRodney W. Grimes 				vrele(*(vps_p[i]));
333df8bae1dSRodney W. Grimes 		}
334df8bae1dSRodney W. Grimes 	}
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	/*
337df8bae1dSRodney W. Grimes 	 * Map the possible out-going vpp
338df8bae1dSRodney W. Grimes 	 * (Assumes that the lower layer always returns
339df8bae1dSRodney W. Grimes 	 * a VREF'ed vpp unless it gets an error.)
340df8bae1dSRodney W. Grimes 	 */
3417b7227c4SKonstantin Belousov 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && error == 0) {
342df8bae1dSRodney W. Grimes 		/*
343df8bae1dSRodney W. Grimes 		 * XXX - even though some ops have vpp returned vp's,
344df8bae1dSRodney W. Grimes 		 * several ops actually vrele this before returning.
345df8bae1dSRodney W. Grimes 		 * We must avoid these ops.
346df8bae1dSRodney W. Grimes 		 * (This should go away when these ops are regularized.)
347df8bae1dSRodney W. Grimes 		 */
348df8bae1dSRodney W. Grimes 		vppp = VOPARG_OFFSETTO(struct vnode ***,
349df8bae1dSRodney W. Grimes 		    descp->vdesc_vpp_offset, ap);
3507b7227c4SKonstantin Belousov 		if (*vppp != NULL)
3517b7227c4SKonstantin Belousov 			error = null_nodeget(old_vps[0]->v_mount, **vppp,
3527b7227c4SKonstantin Belousov 			    *vppp);
353df8bae1dSRodney W. Grimes 	}
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes 	return (error);
356df8bae1dSRodney W. Grimes }
357df8bae1dSRodney W. Grimes 
358140dedb8SKonstantin Belousov static int
null_add_writecount(struct vop_add_writecount_args * ap)359140dedb8SKonstantin Belousov null_add_writecount(struct vop_add_writecount_args *ap)
360140dedb8SKonstantin Belousov {
361140dedb8SKonstantin Belousov 	struct vnode *lvp, *vp;
362140dedb8SKonstantin Belousov 	int error;
363140dedb8SKonstantin Belousov 
364140dedb8SKonstantin Belousov 	vp = ap->a_vp;
365140dedb8SKonstantin Belousov 	lvp = NULLVPTOLOWERVP(vp);
36678022527SKonstantin Belousov 	VI_LOCK(vp);
36778022527SKonstantin Belousov 	/* text refs are bypassed to lowervp */
36878022527SKonstantin Belousov 	VNASSERT(vp->v_writecount >= 0, vp, ("wrong null writecount"));
36978022527SKonstantin Belousov 	VNASSERT(vp->v_writecount + ap->a_inc >= 0, vp,
37078022527SKonstantin Belousov 	    ("wrong writecount inc %d", ap->a_inc));
37178022527SKonstantin Belousov 	error = VOP_ADD_WRITECOUNT(lvp, ap->a_inc);
372140dedb8SKonstantin Belousov 	if (error == 0)
373140dedb8SKonstantin Belousov 		vp->v_writecount += ap->a_inc;
37478022527SKonstantin Belousov 	VI_UNLOCK(vp);
375140dedb8SKonstantin Belousov 	return (error);
376140dedb8SKonstantin Belousov }
377140dedb8SKonstantin Belousov 
378996c772fSJohn Dyson /*
379996c772fSJohn Dyson  * We have to carry on the locking protocol on the null layer vnodes
380996c772fSJohn Dyson  * as we progress through the tree. We also have to enforce read-only
381996c772fSJohn Dyson  * if this layer is mounted read-only.
382996c772fSJohn Dyson  */
383996c772fSJohn Dyson static int
null_lookup(struct vop_lookup_args * ap)384a515233fSPoul-Henning Kamp null_lookup(struct vop_lookup_args *ap)
385996c772fSJohn Dyson {
386996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
3874451405fSBoris Popov 	struct vnode *dvp = ap->a_dvp;
388996c772fSJohn Dyson 	int flags = cnp->cn_flags;
3894451405fSBoris Popov 	struct vnode *vp, *ldvp, *lvp;
390effc6a35SKonstantin Belousov 	struct mount *mp;
391996c772fSJohn Dyson 	int error;
392996c772fSJohn Dyson 
393effc6a35SKonstantin Belousov 	mp = dvp->v_mount;
394effc6a35SKonstantin Belousov 	if ((flags & ISLASTCN) != 0 && (mp->mnt_flag & MNT_RDONLY) != 0 &&
395996c772fSJohn Dyson 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
396996c772fSJohn Dyson 		return (EROFS);
3974451405fSBoris Popov 	/*
3984451405fSBoris Popov 	 * Although it is possible to call null_bypass(), we'll do
3994451405fSBoris Popov 	 * a direct call to reduce overhead
4004451405fSBoris Popov 	 */
4014451405fSBoris Popov 	ldvp = NULLVPTOLOWERVP(dvp);
4024451405fSBoris Popov 	vp = lvp = NULL;
40376b1b5ceSKonstantin Belousov 
40476b1b5ceSKonstantin Belousov 	/*
40576b1b5ceSKonstantin Belousov 	 * Renames in the lower mounts might create an inconsistent
40676b1b5ceSKonstantin Belousov 	 * configuration where lower vnode is moved out of the
40776b1b5ceSKonstantin Belousov 	 * directory tree remounted by our null mount.  Do not try to
40876b1b5ceSKonstantin Belousov 	 * handle it fancy, just avoid VOP_LOOKUP() with DOTDOT name
40976b1b5ceSKonstantin Belousov 	 * which cannot be handled by VOP, at least passing over lower
41076b1b5ceSKonstantin Belousov 	 * root.
41176b1b5ceSKonstantin Belousov 	 */
41276b1b5ceSKonstantin Belousov 	if ((ldvp->v_vflag & VV_ROOT) != 0 && (flags & ISDOTDOT) != 0) {
41376b1b5ceSKonstantin Belousov 		KASSERT((dvp->v_vflag & VV_ROOT) == 0,
41476b1b5ceSKonstantin Belousov 		    ("ldvp %p fl %#x dvp %p fl %#x flags %#x",
41576b1b5ceSKonstantin Belousov 		    ldvp, ldvp->v_vflag, dvp, dvp->v_vflag, flags));
41676b1b5ceSKonstantin Belousov 		return (ENOENT);
41776b1b5ceSKonstantin Belousov 	}
418effc6a35SKonstantin Belousov 
419effc6a35SKonstantin Belousov 	/*
420effc6a35SKonstantin Belousov 	 * Hold ldvp.  The reference on it, owned by dvp, is lost in
421effc6a35SKonstantin Belousov 	 * case of dvp reclamation, and we need ldvp to move our lock
422effc6a35SKonstantin Belousov 	 * from ldvp to dvp.
423effc6a35SKonstantin Belousov 	 */
424effc6a35SKonstantin Belousov 	vhold(ldvp);
425effc6a35SKonstantin Belousov 
4264451405fSBoris Popov 	error = VOP_LOOKUP(ldvp, &lvp, cnp);
427effc6a35SKonstantin Belousov 
428effc6a35SKonstantin Belousov 	/*
429effc6a35SKonstantin Belousov 	 * VOP_LOOKUP() on lower vnode may unlock ldvp, which allows
430effc6a35SKonstantin Belousov 	 * dvp to be reclaimed due to shared v_vnlock.  Check for the
431effc6a35SKonstantin Belousov 	 * doomed state and return error.
432effc6a35SKonstantin Belousov 	 */
4334f21442eSKonstantin Belousov 	if (VN_IS_DOOMED(dvp)) {
4344f21442eSKonstantin Belousov 		if (error == 0 || error == EJUSTRETURN) {
435effc6a35SKonstantin Belousov 			if (lvp != NULL)
436effc6a35SKonstantin Belousov 				vput(lvp);
4374f21442eSKonstantin Belousov 			error = ENOENT;
4384f21442eSKonstantin Belousov 		}
439effc6a35SKonstantin Belousov 
440effc6a35SKonstantin Belousov 		/*
441effc6a35SKonstantin Belousov 		 * If vgone() did reclaimed dvp before curthread
442effc6a35SKonstantin Belousov 		 * relocked ldvp, the locks of dvp and ldpv are no
443effc6a35SKonstantin Belousov 		 * longer shared.  In this case, relock of ldvp in
444effc6a35SKonstantin Belousov 		 * lower fs VOP_LOOKUP() does not restore the locking
445effc6a35SKonstantin Belousov 		 * state of dvp.  Compensate for this by unlocking
446effc6a35SKonstantin Belousov 		 * ldvp and locking dvp, which is also correct if the
447effc6a35SKonstantin Belousov 		 * locks are still shared.
448effc6a35SKonstantin Belousov 		 */
449b249ce48SMateusz Guzik 		VOP_UNLOCK(ldvp);
450effc6a35SKonstantin Belousov 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
451effc6a35SKonstantin Belousov 	}
452effc6a35SKonstantin Belousov 	vdrop(ldvp);
453effc6a35SKonstantin Belousov 
454effc6a35SKonstantin Belousov 	if (error == EJUSTRETURN && (flags & ISLASTCN) != 0 &&
455effc6a35SKonstantin Belousov 	    (mp->mnt_flag & MNT_RDONLY) != 0 &&
456996c772fSJohn Dyson 	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
457996c772fSJohn Dyson 		error = EROFS;
4584451405fSBoris Popov 
4594451405fSBoris Popov 	if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
4604451405fSBoris Popov 		if (ldvp == lvp) {
4614451405fSBoris Popov 			*ap->a_vpp = dvp;
4624451405fSBoris Popov 			VREF(dvp);
4634451405fSBoris Popov 			vrele(lvp);
4644451405fSBoris Popov 		} else {
465effc6a35SKonstantin Belousov 			error = null_nodeget(mp, lvp, &vp);
466dd0f9532SKonstantin Belousov 			if (error == 0)
4674451405fSBoris Popov 				*ap->a_vpp = vp;
468996c772fSJohn Dyson 		}
469996c772fSJohn Dyson 	}
470996c772fSJohn Dyson 	return (error);
471996c772fSJohn Dyson }
472996c772fSJohn Dyson 
473c683c4eeSPoul-Henning Kamp static int
null_open(struct vop_open_args * ap)474c683c4eeSPoul-Henning Kamp null_open(struct vop_open_args *ap)
475c683c4eeSPoul-Henning Kamp {
476c683c4eeSPoul-Henning Kamp 	int retval;
477c683c4eeSPoul-Henning Kamp 	struct vnode *vp, *ldvp;
478c683c4eeSPoul-Henning Kamp 
479c683c4eeSPoul-Henning Kamp 	vp = ap->a_vp;
480c683c4eeSPoul-Henning Kamp 	ldvp = NULLVPTOLOWERVP(vp);
481c683c4eeSPoul-Henning Kamp 	retval = null_bypass(&ap->a_gen);
482685cb01aSKonstantin Belousov 	if (retval == 0) {
483c683c4eeSPoul-Henning Kamp 		vp->v_object = ldvp->v_object;
4843e506a67SMateusz Guzik 		if ((vn_irflag_read(ldvp) & VIRF_PGREAD) != 0) {
485685cb01aSKonstantin Belousov 			MPASS(vp->v_object != NULL);
4863e506a67SMateusz Guzik 			if ((vn_irflag_read(vp) & VIRF_PGREAD) == 0) {
4873e506a67SMateusz Guzik 				vn_irflag_set_cond(vp, VIRF_PGREAD);
488685cb01aSKonstantin Belousov 			}
489685cb01aSKonstantin Belousov 		}
490685cb01aSKonstantin Belousov 	}
491c683c4eeSPoul-Henning Kamp 	return (retval);
492c683c4eeSPoul-Henning Kamp }
493c683c4eeSPoul-Henning Kamp 
494996c772fSJohn Dyson /*
495996c772fSJohn Dyson  * Setattr call. Disallow write attempts if the layer is mounted read-only.
496996c772fSJohn Dyson  */
497fcf54942SPoul-Henning Kamp static int
null_setattr(struct vop_setattr_args * ap)498a515233fSPoul-Henning Kamp null_setattr(struct vop_setattr_args *ap)
499996c772fSJohn Dyson {
500996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
501996c772fSJohn Dyson 	struct vattr *vap = ap->a_vap;
502996c772fSJohn Dyson 
503996c772fSJohn Dyson   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
50463f50488SMike Pritchard 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
50563f50488SMike Pritchard 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
506996c772fSJohn Dyson 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
507996c772fSJohn Dyson 		return (EROFS);
508996c772fSJohn Dyson 	if (vap->va_size != VNOVAL) {
509996c772fSJohn Dyson  		switch (vp->v_type) {
510996c772fSJohn Dyson  		case VDIR:
511996c772fSJohn Dyson  			return (EISDIR);
512996c772fSJohn Dyson  		case VCHR:
513996c772fSJohn Dyson  		case VBLK:
514996c772fSJohn Dyson  		case VSOCK:
515996c772fSJohn Dyson  		case VFIFO:
5167a204420SPeter Wemm 			if (vap->va_flags != VNOVAL)
5177a204420SPeter Wemm 				return (EOPNOTSUPP);
518996c772fSJohn Dyson 			return (0);
519996c772fSJohn Dyson 		case VREG:
520996c772fSJohn Dyson 		case VLNK:
521996c772fSJohn Dyson  		default:
522996c772fSJohn Dyson 			/*
523996c772fSJohn Dyson 			 * Disallow write attempts if the filesystem is
524996c772fSJohn Dyson 			 * mounted read-only.
525996c772fSJohn Dyson 			 */
526996c772fSJohn Dyson 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
527996c772fSJohn Dyson 				return (EROFS);
528996c772fSJohn Dyson 		}
529996c772fSJohn Dyson 	}
5304451405fSBoris Popov 
531f3a778f2SMike Pritchard 	return (null_bypass((struct vop_generic_args *)ap));
532996c772fSJohn Dyson }
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes /*
535fc9fcee0SMateusz Guzik  *  We handle stat and getattr only to change the fsid.
536df8bae1dSRodney W. Grimes  */
537d4b7a369SPoul-Henning Kamp static int
null_stat(struct vop_stat_args * ap)538fc9fcee0SMateusz Guzik null_stat(struct vop_stat_args *ap)
539fc9fcee0SMateusz Guzik {
540fc9fcee0SMateusz Guzik 	int error;
541fc9fcee0SMateusz Guzik 
542fc9fcee0SMateusz Guzik 	if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
543fc9fcee0SMateusz Guzik 		return (error);
544fc9fcee0SMateusz Guzik 
545fc9fcee0SMateusz Guzik 	ap->a_sb->st_dev = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
546fc9fcee0SMateusz Guzik 	return (0);
547fc9fcee0SMateusz Guzik }
548fc9fcee0SMateusz Guzik 
549fc9fcee0SMateusz Guzik static int
null_getattr(struct vop_getattr_args * ap)550a515233fSPoul-Henning Kamp null_getattr(struct vop_getattr_args *ap)
551df8bae1dSRodney W. Grimes {
552df8bae1dSRodney W. Grimes 	int error;
553996c772fSJohn Dyson 
5548aef1712SMatthew Dillon 	if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
555df8bae1dSRodney W. Grimes 		return (error);
5568da80660SBoris Popov 
5578da80660SBoris Popov 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
558df8bae1dSRodney W. Grimes 	return (0);
559df8bae1dSRodney W. Grimes }
560df8bae1dSRodney W. Grimes 
5614451405fSBoris Popov /*
5624451405fSBoris Popov  * Handle to disallow write access if mounted read-only.
5634451405fSBoris Popov  */
564d4b7a369SPoul-Henning Kamp static int
null_access(struct vop_access_args * ap)565a515233fSPoul-Henning Kamp null_access(struct vop_access_args *ap)
566996c772fSJohn Dyson {
567996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
56815bc6b2bSEdward Tomasz Napierala 	accmode_t accmode = ap->a_accmode;
569996c772fSJohn Dyson 
570996c772fSJohn Dyson 	/*
571996c772fSJohn Dyson 	 * Disallow write attempts on read-only layers;
572996c772fSJohn Dyson 	 * unless the file is a socket, fifo, or a block or
573996c772fSJohn Dyson 	 * character device resident on the filesystem.
574996c772fSJohn Dyson 	 */
57515bc6b2bSEdward Tomasz Napierala 	if (accmode & VWRITE) {
576996c772fSJohn Dyson 		switch (vp->v_type) {
577996c772fSJohn Dyson 		case VDIR:
578996c772fSJohn Dyson 		case VLNK:
579996c772fSJohn Dyson 		case VREG:
580996c772fSJohn Dyson 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
581996c772fSJohn Dyson 				return (EROFS);
582996c772fSJohn Dyson 			break;
583831a80b0SMatthew Dillon 		default:
584831a80b0SMatthew Dillon 			break;
585996c772fSJohn Dyson 		}
586996c772fSJohn Dyson 	}
587f3a778f2SMike Pritchard 	return (null_bypass((struct vop_generic_args *)ap));
588996c772fSJohn Dyson }
589996c772fSJohn Dyson 
590c97fcdbaSEdward Tomasz Napierala static int
null_accessx(struct vop_accessx_args * ap)591c97fcdbaSEdward Tomasz Napierala null_accessx(struct vop_accessx_args *ap)
592c97fcdbaSEdward Tomasz Napierala {
593c97fcdbaSEdward Tomasz Napierala 	struct vnode *vp = ap->a_vp;
594c97fcdbaSEdward Tomasz Napierala 	accmode_t accmode = ap->a_accmode;
595c97fcdbaSEdward Tomasz Napierala 
596c97fcdbaSEdward Tomasz Napierala 	/*
597c97fcdbaSEdward Tomasz Napierala 	 * Disallow write attempts on read-only layers;
598c97fcdbaSEdward Tomasz Napierala 	 * unless the file is a socket, fifo, or a block or
599c97fcdbaSEdward Tomasz Napierala 	 * character device resident on the filesystem.
600c97fcdbaSEdward Tomasz Napierala 	 */
601c97fcdbaSEdward Tomasz Napierala 	if (accmode & VWRITE) {
602c97fcdbaSEdward Tomasz Napierala 		switch (vp->v_type) {
603c97fcdbaSEdward Tomasz Napierala 		case VDIR:
604c97fcdbaSEdward Tomasz Napierala 		case VLNK:
605c97fcdbaSEdward Tomasz Napierala 		case VREG:
606c97fcdbaSEdward Tomasz Napierala 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
607c97fcdbaSEdward Tomasz Napierala 				return (EROFS);
608c97fcdbaSEdward Tomasz Napierala 			break;
609c97fcdbaSEdward Tomasz Napierala 		default:
610c97fcdbaSEdward Tomasz Napierala 			break;
611c97fcdbaSEdward Tomasz Napierala 		}
612c97fcdbaSEdward Tomasz Napierala 	}
613c97fcdbaSEdward Tomasz Napierala 	return (null_bypass((struct vop_generic_args *)ap));
614c97fcdbaSEdward Tomasz Napierala }
615c97fcdbaSEdward Tomasz Napierala 
616996c772fSJohn Dyson /*
6172d0c83b1SRick Macklem  * Increasing refcount of lower vnode is needed at least for the case
6182d0c83b1SRick Macklem  * when lower FS is NFS to do sillyrename if the file is in use.
6192d0c83b1SRick Macklem  * Unfortunately v_usecount is incremented in many places in
6202d0c83b1SRick Macklem  * the kernel and, as such, there may be races that result in
6212d0c83b1SRick Macklem  * the NFS client doing an extraneous silly rename, but that seems
6222d0c83b1SRick Macklem  * preferable to not doing a silly rename when it is needed.
6232d0c83b1SRick Macklem  */
6242d0c83b1SRick Macklem static int
null_remove(struct vop_remove_args * ap)6252d0c83b1SRick Macklem null_remove(struct vop_remove_args *ap)
6262d0c83b1SRick Macklem {
6272d0c83b1SRick Macklem 	int retval, vreleit;
6286f73b583SKonstantin Belousov 	struct vnode *lvp, *vp;
6292d0c83b1SRick Macklem 
6306f73b583SKonstantin Belousov 	vp = ap->a_vp;
6316f73b583SKonstantin Belousov 	if (vrefcnt(vp) > 1) {
6326f73b583SKonstantin Belousov 		lvp = NULLVPTOLOWERVP(vp);
6332d0c83b1SRick Macklem 		VREF(lvp);
6342d0c83b1SRick Macklem 		vreleit = 1;
6352d0c83b1SRick Macklem 	} else
6362d0c83b1SRick Macklem 		vreleit = 0;
6376f73b583SKonstantin Belousov 	VTONULL(vp)->null_flags |= NULLV_DROP;
6382d0c83b1SRick Macklem 	retval = null_bypass(&ap->a_gen);
6392d0c83b1SRick Macklem 	if (vreleit != 0)
6402d0c83b1SRick Macklem 		vrele(lvp);
6412d0c83b1SRick Macklem 	return (retval);
6422d0c83b1SRick Macklem }
6432d0c83b1SRick Macklem 
6442d0c83b1SRick Macklem /*
6458da80660SBoris Popov  * We handle this to eliminate null FS to lower FS
6468da80660SBoris Popov  * file moving. Don't know why we don't allow this,
6478da80660SBoris Popov  * possibly we should.
6488da80660SBoris Popov  */
6498da80660SBoris Popov static int
null_rename(struct vop_rename_args * ap)650a515233fSPoul-Henning Kamp null_rename(struct vop_rename_args *ap)
6518da80660SBoris Popov {
65226e72728SKonstantin Belousov 	struct vnode *fdvp, *fvp, *tdvp, *tvp;
6534eaf9609SKonstantin Belousov 	struct vnode *lfdvp, *lfvp, *ltdvp, *ltvp;
6544eaf9609SKonstantin Belousov 	struct null_node *fdnn, *fnn, *tdnn, *tnn;
6554eaf9609SKonstantin Belousov 	int error;
6568da80660SBoris Popov 
65726e72728SKonstantin Belousov 	tdvp = ap->a_tdvp;
65826e72728SKonstantin Belousov 	fvp = ap->a_fvp;
65926e72728SKonstantin Belousov 	fdvp = ap->a_fdvp;
66026e72728SKonstantin Belousov 	tvp = ap->a_tvp;
6614eaf9609SKonstantin Belousov 	lfdvp = NULL;
66226e72728SKonstantin Belousov 
6638da80660SBoris Popov 	/* Check for cross-device rename. */
6648da80660SBoris Popov 	if ((fvp->v_mount != tdvp->v_mount) ||
66526e72728SKonstantin Belousov 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
6664eaf9609SKonstantin Belousov 		error = EXDEV;
6674eaf9609SKonstantin Belousov 		goto upper_err;
6684eaf9609SKonstantin Belousov 	}
6694eaf9609SKonstantin Belousov 
6704eaf9609SKonstantin Belousov 	VI_LOCK(fdvp);
6714eaf9609SKonstantin Belousov 	fdnn = VTONULL(fdvp);
6724eaf9609SKonstantin Belousov 	if (fdnn == NULL) {	/* fdvp is not locked, can be doomed */
6734eaf9609SKonstantin Belousov 		VI_UNLOCK(fdvp);
6744eaf9609SKonstantin Belousov 		error = ENOENT;
6754eaf9609SKonstantin Belousov 		goto upper_err;
6764eaf9609SKonstantin Belousov 	}
6774eaf9609SKonstantin Belousov 	lfdvp = fdnn->null_lowervp;
6784eaf9609SKonstantin Belousov 	vref(lfdvp);
6794eaf9609SKonstantin Belousov 	VI_UNLOCK(fdvp);
6804eaf9609SKonstantin Belousov 
6814eaf9609SKonstantin Belousov 	VI_LOCK(fvp);
6824eaf9609SKonstantin Belousov 	fnn = VTONULL(fvp);
6834eaf9609SKonstantin Belousov 	if (fnn == NULL) {
6844eaf9609SKonstantin Belousov 		VI_UNLOCK(fvp);
6854eaf9609SKonstantin Belousov 		error = ENOENT;
6864eaf9609SKonstantin Belousov 		goto upper_err;
6874eaf9609SKonstantin Belousov 	}
6884eaf9609SKonstantin Belousov 	lfvp = fnn->null_lowervp;
6894eaf9609SKonstantin Belousov 	vref(lfvp);
6904eaf9609SKonstantin Belousov 	VI_UNLOCK(fvp);
6914eaf9609SKonstantin Belousov 
6924eaf9609SKonstantin Belousov 	tdnn = VTONULL(tdvp);
6934eaf9609SKonstantin Belousov 	ltdvp = tdnn->null_lowervp;
6944eaf9609SKonstantin Belousov 	vref(ltdvp);
6954eaf9609SKonstantin Belousov 
6964eaf9609SKonstantin Belousov 	if (tvp != NULL) {
6974eaf9609SKonstantin Belousov 		tnn = VTONULL(tvp);
6984eaf9609SKonstantin Belousov 		ltvp = tnn->null_lowervp;
6994eaf9609SKonstantin Belousov 		vref(ltvp);
7004eaf9609SKonstantin Belousov 		tnn->null_flags |= NULLV_DROP;
7014eaf9609SKonstantin Belousov 	} else {
7024eaf9609SKonstantin Belousov 		ltvp = NULL;
7034eaf9609SKonstantin Belousov 	}
7044eaf9609SKonstantin Belousov 
7054eaf9609SKonstantin Belousov 	error = VOP_RENAME(lfdvp, lfvp, ap->a_fcnp, ltdvp, ltvp, ap->a_tcnp);
7064eaf9609SKonstantin Belousov 	vrele(fdvp);
7074eaf9609SKonstantin Belousov 	vrele(fvp);
7084eaf9609SKonstantin Belousov 	vrele(tdvp);
7094eaf9609SKonstantin Belousov 	if (tvp != NULL)
7104eaf9609SKonstantin Belousov 		vrele(tvp);
7114eaf9609SKonstantin Belousov 	return (error);
7124eaf9609SKonstantin Belousov 
7134eaf9609SKonstantin Belousov upper_err:
7148da80660SBoris Popov 	if (tdvp == tvp)
7158da80660SBoris Popov 		vrele(tdvp);
7168da80660SBoris Popov 	else
7178da80660SBoris Popov 		vput(tdvp);
7188da80660SBoris Popov 	if (tvp)
7198da80660SBoris Popov 		vput(tvp);
7204eaf9609SKonstantin Belousov 	if (lfdvp != NULL)
7214eaf9609SKonstantin Belousov 		vrele(lfdvp);
7228da80660SBoris Popov 	vrele(fdvp);
7238da80660SBoris Popov 	vrele(fvp);
7244eaf9609SKonstantin Belousov 	return (error);
7258da80660SBoris Popov }
7268da80660SBoris Popov 
727830cd4b8SKonstantin Belousov static int
null_rmdir(struct vop_rmdir_args * ap)728830cd4b8SKonstantin Belousov null_rmdir(struct vop_rmdir_args *ap)
729830cd4b8SKonstantin Belousov {
730830cd4b8SKonstantin Belousov 
731830cd4b8SKonstantin Belousov 	VTONULL(ap->a_vp)->null_flags |= NULLV_DROP;
732830cd4b8SKonstantin Belousov 	return (null_bypass(&ap->a_gen));
733830cd4b8SKonstantin Belousov }
734830cd4b8SKonstantin Belousov 
7358da80660SBoris Popov /*
736996c772fSJohn Dyson  * We need to process our own vnode lock and then clear the
737996c772fSJohn Dyson  * interlock flag as it applies only to our vnode, not the
738996c772fSJohn Dyson  * vnodes below us on the stack.
739996c772fSJohn Dyson  */
74063f50488SMike Pritchard static int
null_lock(struct vop_lock1_args * ap)741d413d210SKonstantin Belousov null_lock(struct vop_lock1_args *ap)
742996c772fSJohn Dyson {
7434451405fSBoris Popov 	struct vnode *vp = ap->a_vp;
744e0f4540aSMateusz Guzik 	int flags;
745bc855512SJeff Roberson 	struct null_node *nn;
7464451405fSBoris Popov 	struct vnode *lvp;
7474451405fSBoris Popov 	int error;
748996c772fSJohn Dyson 
749e0f4540aSMateusz Guzik 	if ((ap->a_flags & LK_INTERLOCK) == 0)
7508da00465SJeff Roberson 		VI_LOCK(vp);
751e0f4540aSMateusz Guzik 	else
752e0f4540aSMateusz Guzik 		ap->a_flags &= ~LK_INTERLOCK;
753e0f4540aSMateusz Guzik 	flags = ap->a_flags;
75454939875STim J. Robbins 	nn = VTONULL(vp);
75554939875STim J. Robbins 	/*
756bc855512SJeff Roberson 	 * If we're still active we must ask the lower layer to
75728323addSBryan Drewery 	 * lock as ffs has special lock considerations in its
758bc855512SJeff Roberson 	 * vop lock.
75954939875STim J. Robbins 	 */
760bc855512SJeff Roberson 	if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
7614451405fSBoris Popov 		/*
762bc855512SJeff Roberson 		 * We have to hold the vnode here to solve a potential
763bc855512SJeff Roberson 		 * reclaim race.  If we're forcibly vgone'd while we
764bc855512SJeff Roberson 		 * still have refs, a thread could be sleeping inside
765bc855512SJeff Roberson 		 * the lowervp's vop_lock routine.  When we vgone we will
766bc855512SJeff Roberson 		 * drop our last ref to the lowervp, which would allow it
767bc855512SJeff Roberson 		 * to be reclaimed.  The lowervp could then be recycled,
76828323addSBryan Drewery 		 * in which case it is not legal to be sleeping in its VOP.
769bc855512SJeff Roberson 		 * We prevent it from being recycled by holding the vnode
770bc855512SJeff Roberson 		 * here.
7714451405fSBoris Popov 		 */
77233d46a3cSMateusz Guzik 		vholdnz(lvp);
773e0f4540aSMateusz Guzik 		VI_UNLOCK(vp);
77422db15c0SAttilio Rao 		error = VOP_LOCK(lvp, flags);
775d11c07baSAlexander Kabaev 
776d11c07baSAlexander Kabaev 		/*
777d11c07baSAlexander Kabaev 		 * We might have slept to get the lock and someone might have
778d11c07baSAlexander Kabaev 		 * clean our vnode already, switching vnode lock from one in
779d11c07baSAlexander Kabaev 		 * lowervp to v_lock in our own vnode structure.  Handle this
780d11c07baSAlexander Kabaev 		 * case by reacquiring correct lock in requested mode.
781d11c07baSAlexander Kabaev 		 */
782d11c07baSAlexander Kabaev 		if (VTONULL(vp) == NULL && error == 0) {
783e0f4540aSMateusz Guzik 			ap->a_flags &= ~LK_TYPE_MASK;
784d11c07baSAlexander Kabaev 			switch (flags & LK_TYPE_MASK) {
785d11c07baSAlexander Kabaev 			case LK_SHARED:
786d11c07baSAlexander Kabaev 				ap->a_flags |= LK_SHARED;
787d11c07baSAlexander Kabaev 				break;
788d11c07baSAlexander Kabaev 			case LK_UPGRADE:
789d11c07baSAlexander Kabaev 			case LK_EXCLUSIVE:
790d11c07baSAlexander Kabaev 				ap->a_flags |= LK_EXCLUSIVE;
791d11c07baSAlexander Kabaev 				break;
792d11c07baSAlexander Kabaev 			default:
793d11c07baSAlexander Kabaev 				panic("Unsupported lock request %d\n",
794d11c07baSAlexander Kabaev 				    ap->a_flags);
795d11c07baSAlexander Kabaev 			}
796b249ce48SMateusz Guzik 			VOP_UNLOCK(lvp);
797d11c07baSAlexander Kabaev 			error = vop_stdlock(ap);
798d11c07baSAlexander Kabaev 		}
799bc855512SJeff Roberson 		vdrop(lvp);
800e0f4540aSMateusz Guzik 	} else {
801e0f4540aSMateusz Guzik 		VI_UNLOCK(vp);
802bc855512SJeff Roberson 		error = vop_stdlock(ap);
803e0f4540aSMateusz Guzik 	}
804bc855512SJeff Roberson 
8054451405fSBoris Popov 	return (error);
806996c772fSJohn Dyson }
807996c772fSJohn Dyson 
808996c772fSJohn Dyson /*
809996c772fSJohn Dyson  * We need to process our own vnode unlock and then clear the
810996c772fSJohn Dyson  * interlock flag as it applies only to our vnode, not the
811996c772fSJohn Dyson  * vnodes below us on the stack.
812996c772fSJohn Dyson  */
81363f50488SMike Pritchard static int
null_unlock(struct vop_unlock_args * ap)814a515233fSPoul-Henning Kamp null_unlock(struct vop_unlock_args *ap)
815996c772fSJohn Dyson {
8164451405fSBoris Popov 	struct vnode *vp = ap->a_vp;
817bc855512SJeff Roberson 	struct null_node *nn;
818611025ecSBoris Popov 	struct vnode *lvp;
819bc855512SJeff Roberson 	int error;
8204451405fSBoris Popov 
821bc855512SJeff Roberson 	nn = VTONULL(vp);
82210166260SDaichi GOTO 	if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
82333d46a3cSMateusz Guzik 		vholdnz(lvp);
824b249ce48SMateusz Guzik 		error = VOP_UNLOCK(lvp);
82510166260SDaichi GOTO 		vdrop(lvp);
82610166260SDaichi GOTO 	} else {
827bc855512SJeff Roberson 		error = vop_stdunlock(ap);
82810166260SDaichi GOTO 	}
829bc855512SJeff Roberson 
830bc855512SJeff Roberson 	return (error);
831996c772fSJohn Dyson }
832996c772fSJohn Dyson 
8334451405fSBoris Popov /*
83482ed933cSKonstantin Belousov  * Do not allow the VOP_INACTIVE to be passed to the lower layer,
83582ed933cSKonstantin Belousov  * since the reference count on the lower vnode is not related to
83682ed933cSKonstantin Belousov  * ours.
8374451405fSBoris Popov  */
8384451405fSBoris Popov static int
null_want_recycle(struct vnode * vp)83913c73428SMateusz Guzik null_want_recycle(struct vnode *vp)
840df8bae1dSRodney W. Grimes {
84113c73428SMateusz Guzik 	struct vnode *lvp;
8420fc6daa7SKonstantin Belousov 	struct null_node *xp;
8439cf4c952SKonstantin Belousov 	struct mount *mp;
8449cf4c952SKonstantin Belousov 	struct null_mount *xmp;
845ebe0bdddSSemen Ustimenko 
8460fc6daa7SKonstantin Belousov 	xp = VTONULL(vp);
8470fc6daa7SKonstantin Belousov 	lvp = NULLVPTOLOWERVP(vp);
8489cf4c952SKonstantin Belousov 	mp = vp->v_mount;
8499cf4c952SKonstantin Belousov 	xmp = MOUNTTONULLMOUNT(mp);
8500fc6daa7SKonstantin Belousov 	if ((xmp->nullm_flags & NULLM_CACHE) == 0 ||
8510fc6daa7SKonstantin Belousov 	    (xp->null_flags & NULLV_DROP) != 0 ||
8520fc6daa7SKonstantin Belousov 	    (lvp->v_vflag & VV_NOSYNC) != 0) {
8539cf4c952SKonstantin Belousov 		/*
8549cf4c952SKonstantin Belousov 		 * If this is the last reference and caching of the
8550fc6daa7SKonstantin Belousov 		 * nullfs vnodes is not enabled, or the lower vnode is
8560fc6daa7SKonstantin Belousov 		 * deleted, then free up the vnode so as not to tie up
8570fc6daa7SKonstantin Belousov 		 * the lower vnodes.
8589cf4c952SKonstantin Belousov 		 */
85913c73428SMateusz Guzik 		return (1);
86013c73428SMateusz Guzik 	}
86113c73428SMateusz Guzik 	return (0);
86213c73428SMateusz Guzik }
86313c73428SMateusz Guzik 
86413c73428SMateusz Guzik static int
null_inactive(struct vop_inactive_args * ap)86513c73428SMateusz Guzik null_inactive(struct vop_inactive_args *ap)
86613c73428SMateusz Guzik {
86713c73428SMateusz Guzik 	struct vnode *vp;
86813c73428SMateusz Guzik 
86913c73428SMateusz Guzik 	vp = ap->a_vp;
87013c73428SMateusz Guzik 	if (null_want_recycle(vp)) {
8719cf4c952SKonstantin Belousov 		vp->v_object = NULL;
8729cf4c952SKonstantin Belousov 		vrecycle(vp);
8739cf4c952SKonstantin Belousov 	}
874df8bae1dSRodney W. Grimes 	return (0);
875df8bae1dSRodney W. Grimes }
876df8bae1dSRodney W. Grimes 
87713c73428SMateusz Guzik static int
null_need_inactive(struct vop_need_inactive_args * ap)87813c73428SMateusz Guzik null_need_inactive(struct vop_need_inactive_args *ap)
87913c73428SMateusz Guzik {
88013c73428SMateusz Guzik 
88142881526SKonstantin Belousov 	return (null_want_recycle(ap->a_vp) || vn_need_pageq_flush(ap->a_vp));
88213c73428SMateusz Guzik }
88313c73428SMateusz Guzik 
8844451405fSBoris Popov /*
885d9e9650aSKonstantin Belousov  * Now, the nullfs vnode and, due to the sharing lock, the lower
886d9e9650aSKonstantin Belousov  * vnode, are exclusively locked, and we shall destroy the null vnode.
8874451405fSBoris Popov  */
888d4b7a369SPoul-Henning Kamp static int
null_reclaim(struct vop_reclaim_args * ap)889a515233fSPoul-Henning Kamp null_reclaim(struct vop_reclaim_args *ap)
890df8bae1dSRodney W. Grimes {
891409b12c0SKonstantin Belousov 	struct vnode *vp;
892409b12c0SKonstantin Belousov 	struct null_node *xp;
893409b12c0SKonstantin Belousov 	struct vnode *lowervp;
894df8bae1dSRodney W. Grimes 
895409b12c0SKonstantin Belousov 	vp = ap->a_vp;
896409b12c0SKonstantin Belousov 	xp = VTONULL(vp);
897409b12c0SKonstantin Belousov 	lowervp = xp->null_lowervp;
898409b12c0SKonstantin Belousov 
899409b12c0SKonstantin Belousov 	KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock,
900289dd6ddSKonstantin Belousov 	    ("Reclaiming incomplete null vnode %p", vp));
901409b12c0SKonstantin Belousov 
902f5cacb39SJeff Roberson 	null_hashrem(xp);
903ba731053SJeff Roberson 	/*
904ba731053SJeff Roberson 	 * Use the interlock to protect the clearing of v_data to
905ba731053SJeff Roberson 	 * prevent faults in null_lock().
906ba731053SJeff Roberson 	 */
907cec9ed6dSKonstantin Belousov 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
908ba731053SJeff Roberson 	VI_LOCK(vp);
909ba731053SJeff Roberson 	vp->v_data = NULL;
910f5cacb39SJeff Roberson 	vp->v_object = NULL;
911d11c07baSAlexander Kabaev 	vp->v_vnlock = &vp->v_lock;
9126b175951SKonstantin Belousov 
9136b175951SKonstantin Belousov 	/*
91478022527SKonstantin Belousov 	 * If we were opened for write, we leased the write reference
9156b175951SKonstantin Belousov 	 * to the lower vnode.  If this is a reclamation due to the
9166b175951SKonstantin Belousov 	 * forced unmount, undo the reference now.
9176b175951SKonstantin Belousov 	 */
9186b175951SKonstantin Belousov 	if (vp->v_writecount > 0)
91978022527SKonstantin Belousov 		VOP_ADD_WRITECOUNT(lowervp, -vp->v_writecount);
9203c93d227SKonstantin Belousov 	else if (vp->v_writecount < 0)
9213c93d227SKonstantin Belousov 		vp->v_writecount = 0;
92278022527SKonstantin Belousov 
92378022527SKonstantin Belousov 	VI_UNLOCK(vp);
92478022527SKonstantin Belousov 
9250fc6daa7SKonstantin Belousov 	if ((xp->null_flags & NULLV_NOUNLOCK) != 0)
9260fc6daa7SKonstantin Belousov 		vunref(lowervp);
9270fc6daa7SKonstantin Belousov 	else
928cdeb7204SSuleiman Souhlal 		vput(lowervp);
9291ede983cSDag-Erling Smørgrav 	free(xp, M_NULLFSNODE);
9304451405fSBoris Popov 
931df8bae1dSRodney W. Grimes 	return (0);
932df8bae1dSRodney W. Grimes }
933df8bae1dSRodney W. Grimes 
934d4b7a369SPoul-Henning Kamp static int
null_print(struct vop_print_args * ap)935a515233fSPoul-Henning Kamp null_print(struct vop_print_args *ap)
936df8bae1dSRodney W. Grimes {
937d34dd851SPoul-Henning Kamp 	struct vnode *vp = ap->a_vp;
938f5cacb39SJeff Roberson 
939f82ee01cSKonstantin Belousov 	printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp);
940df8bae1dSRodney W. Grimes 	return (0);
941df8bae1dSRodney W. Grimes }
942df8bae1dSRodney W. Grimes 
9434bf5133bSJeff Roberson /* ARGSUSED */
9444bf5133bSJeff Roberson static int
null_getwritemount(struct vop_getwritemount_args * ap)9454bf5133bSJeff Roberson null_getwritemount(struct vop_getwritemount_args *ap)
9464bf5133bSJeff Roberson {
9474bf5133bSJeff Roberson 	struct null_node *xp;
9484bf5133bSJeff Roberson 	struct vnode *lowervp;
9494bf5133bSJeff Roberson 	struct vnode *vp;
9504bf5133bSJeff Roberson 
9514bf5133bSJeff Roberson 	vp = ap->a_vp;
9524bf5133bSJeff Roberson 	VI_LOCK(vp);
9534bf5133bSJeff Roberson 	xp = VTONULL(vp);
9544bf5133bSJeff Roberson 	if (xp && (lowervp = xp->null_lowervp)) {
95533d46a3cSMateusz Guzik 		vholdnz(lowervp);
9564bf5133bSJeff Roberson 		VI_UNLOCK(vp);
9574bf5133bSJeff Roberson 		VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);
9584bf5133bSJeff Roberson 		vdrop(lowervp);
9594bf5133bSJeff Roberson 	} else {
9604bf5133bSJeff Roberson 		VI_UNLOCK(vp);
9614bf5133bSJeff Roberson 		*(ap->a_mpp) = NULL;
9624bf5133bSJeff Roberson 	}
9634bf5133bSJeff Roberson 	return (0);
9644bf5133bSJeff Roberson }
9654bf5133bSJeff Roberson 
96610bcafe9SPawel Jakub Dawidek static int
null_vptofh(struct vop_vptofh_args * ap)96710bcafe9SPawel Jakub Dawidek null_vptofh(struct vop_vptofh_args *ap)
96810bcafe9SPawel Jakub Dawidek {
96910bcafe9SPawel Jakub Dawidek 	struct vnode *lvp;
97010bcafe9SPawel Jakub Dawidek 
97110bcafe9SPawel Jakub Dawidek 	lvp = NULLVPTOLOWERVP(ap->a_vp);
97210bcafe9SPawel Jakub Dawidek 	return VOP_VPTOFH(lvp, ap->a_fhp);
97310bcafe9SPawel Jakub Dawidek }
97410bcafe9SPawel Jakub Dawidek 
975b0f34bb6SKonstantin Belousov static int
null_vptocnp(struct vop_vptocnp_args * ap)976b0f34bb6SKonstantin Belousov null_vptocnp(struct vop_vptocnp_args *ap)
977b0f34bb6SKonstantin Belousov {
978b0f34bb6SKonstantin Belousov 	struct vnode *vp = ap->a_vp;
979b0f34bb6SKonstantin Belousov 	struct vnode **dvp = ap->a_vpp;
980b0f34bb6SKonstantin Belousov 	struct vnode *lvp, *ldvp;
981b9662886SKonstantin Belousov 	struct mount *mp;
982b0f34bb6SKonstantin Belousov 	int error, locked;
983b0f34bb6SKonstantin Belousov 
984b0f34bb6SKonstantin Belousov 	locked = VOP_ISLOCKED(vp);
985b0f34bb6SKonstantin Belousov 	lvp = NULLVPTOLOWERVP(vp);
986b9662886SKonstantin Belousov 	mp = vp->v_mount;
9877fd37611SKonstantin Belousov 	error = vfs_busy(mp, MBF_NOWAIT);
9887fd37611SKonstantin Belousov 	if (error != 0)
9897fd37611SKonstantin Belousov 		return (error);
9907fd37611SKonstantin Belousov 	vhold(lvp);
991b249ce48SMateusz Guzik 	VOP_UNLOCK(vp); /* vp is held by vn_vptocnp_locked that called us */
992b0f34bb6SKonstantin Belousov 	ldvp = lvp;
993f82360acSKonstantin Belousov 	vref(lvp);
9948ecd87a3SMateusz Guzik 	error = vn_vptocnp(&ldvp, ap->a_buf, ap->a_buflen);
995b0f34bb6SKonstantin Belousov 	vdrop(lvp);
996b0f34bb6SKonstantin Belousov 	if (error != 0) {
997b0f34bb6SKonstantin Belousov 		vn_lock(vp, locked | LK_RETRY);
9987fd37611SKonstantin Belousov 		vfs_unbusy(mp);
999b0f34bb6SKonstantin Belousov 		return (ENOENT);
1000b0f34bb6SKonstantin Belousov 	}
1001b0f34bb6SKonstantin Belousov 
100281f666e7SMateusz Guzik 	error = vn_lock(ldvp, LK_SHARED);
1003b0f34bb6SKonstantin Belousov 	if (error != 0) {
1004f82360acSKonstantin Belousov 		vrele(ldvp);
1005b0f34bb6SKonstantin Belousov 		vn_lock(vp, locked | LK_RETRY);
10067fd37611SKonstantin Belousov 		vfs_unbusy(mp);
1007b0f34bb6SKonstantin Belousov 		return (ENOENT);
1008b0f34bb6SKonstantin Belousov 	}
1009b9662886SKonstantin Belousov 	error = null_nodeget(mp, ldvp, dvp);
1010b0f34bb6SKonstantin Belousov 	if (error == 0) {
1011b0f34bb6SKonstantin Belousov #ifdef DIAGNOSTIC
1012b0f34bb6SKonstantin Belousov 		NULLVPTOLOWERVP(*dvp);
1013b0f34bb6SKonstantin Belousov #endif
1014b249ce48SMateusz Guzik 		VOP_UNLOCK(*dvp); /* keep reference on *dvp */
1015dd0f9532SKonstantin Belousov 	}
1016b0f34bb6SKonstantin Belousov 	vn_lock(vp, locked | LK_RETRY);
10177fd37611SKonstantin Belousov 	vfs_unbusy(mp);
1018b0f34bb6SKonstantin Belousov 	return (error);
1019b0f34bb6SKonstantin Belousov }
1020b0f34bb6SKonstantin Belousov 
1021f7af6e5eSKonstantin Belousov static int
null_read_pgcache(struct vop_read_pgcache_args * ap)1022f7af6e5eSKonstantin Belousov null_read_pgcache(struct vop_read_pgcache_args *ap)
1023f7af6e5eSKonstantin Belousov {
1024f7af6e5eSKonstantin Belousov 	struct vnode *lvp, *vp;
1025f7af6e5eSKonstantin Belousov 	struct null_node *xp;
1026f7af6e5eSKonstantin Belousov 	int error;
1027f7af6e5eSKonstantin Belousov 
1028f7af6e5eSKonstantin Belousov 	vp = ap->a_vp;
1029f7af6e5eSKonstantin Belousov 	VI_LOCK(vp);
1030f7af6e5eSKonstantin Belousov 	xp = VTONULL(vp);
1031f7af6e5eSKonstantin Belousov 	if (xp == NULL) {
1032f7af6e5eSKonstantin Belousov 		VI_UNLOCK(vp);
1033f7af6e5eSKonstantin Belousov 		return (EJUSTRETURN);
1034f7af6e5eSKonstantin Belousov 	}
1035f7af6e5eSKonstantin Belousov 	lvp = xp->null_lowervp;
1036f7af6e5eSKonstantin Belousov 	vref(lvp);
1037f7af6e5eSKonstantin Belousov 	VI_UNLOCK(vp);
1038f7af6e5eSKonstantin Belousov 	error = VOP_READ_PGCACHE(lvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1039f7af6e5eSKonstantin Belousov 	vrele(lvp);
1040f7af6e5eSKonstantin Belousov 	return (error);
1041f7af6e5eSKonstantin Belousov }
1042f7af6e5eSKonstantin Belousov 
1043161e9a97SKonstantin Belousov static int
null_advlock(struct vop_advlock_args * ap)1044161e9a97SKonstantin Belousov null_advlock(struct vop_advlock_args *ap)
1045161e9a97SKonstantin Belousov {
1046161e9a97SKonstantin Belousov 	struct vnode *lvp, *vp;
1047161e9a97SKonstantin Belousov 	struct null_node *xp;
1048161e9a97SKonstantin Belousov 	int error;
1049161e9a97SKonstantin Belousov 
1050161e9a97SKonstantin Belousov 	vp = ap->a_vp;
1051161e9a97SKonstantin Belousov 	VI_LOCK(vp);
1052161e9a97SKonstantin Belousov 	xp = VTONULL(vp);
1053161e9a97SKonstantin Belousov 	if (xp == NULL) {
1054161e9a97SKonstantin Belousov 		VI_UNLOCK(vp);
1055161e9a97SKonstantin Belousov 		return (EBADF);
1056161e9a97SKonstantin Belousov 	}
1057161e9a97SKonstantin Belousov 	lvp = xp->null_lowervp;
1058161e9a97SKonstantin Belousov 	vref(lvp);
1059161e9a97SKonstantin Belousov 	VI_UNLOCK(vp);
1060161e9a97SKonstantin Belousov 	error = VOP_ADVLOCK(lvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);
1061161e9a97SKonstantin Belousov 	vrele(lvp);
1062161e9a97SKonstantin Belousov 	return (error);
1063161e9a97SKonstantin Belousov }
1064161e9a97SKonstantin Belousov 
1065df8bae1dSRodney W. Grimes /*
1066e4aaf35aSKonstantin Belousov  * Avoid standard bypass, since lower dvp and vp could be no longer
1067e4aaf35aSKonstantin Belousov  * valid after vput().
1068e4aaf35aSKonstantin Belousov  */
1069e4aaf35aSKonstantin Belousov static int
null_vput_pair(struct vop_vput_pair_args * ap)1070e4aaf35aSKonstantin Belousov null_vput_pair(struct vop_vput_pair_args *ap)
1071e4aaf35aSKonstantin Belousov {
1072e4aaf35aSKonstantin Belousov 	struct mount *mp;
1073e4aaf35aSKonstantin Belousov 	struct vnode *dvp, *ldvp, *lvp, *vp, *vp1, **vpp;
1074e4aaf35aSKonstantin Belousov 	int error, res;
1075e4aaf35aSKonstantin Belousov 
1076e4aaf35aSKonstantin Belousov 	dvp = ap->a_dvp;
1077e4aaf35aSKonstantin Belousov 	ldvp = NULLVPTOLOWERVP(dvp);
1078e4aaf35aSKonstantin Belousov 	vref(ldvp);
1079e4aaf35aSKonstantin Belousov 
1080e4aaf35aSKonstantin Belousov 	vpp = ap->a_vpp;
1081e4aaf35aSKonstantin Belousov 	vp = NULL;
1082e4aaf35aSKonstantin Belousov 	lvp = NULL;
108316dea834SKonstantin Belousov 	mp = NULL;
108416dea834SKonstantin Belousov 	if (vpp != NULL)
1085e4aaf35aSKonstantin Belousov 		vp = *vpp;
1086e4aaf35aSKonstantin Belousov 	if (vp != NULL) {
1087e4aaf35aSKonstantin Belousov 		lvp = NULLVPTOLOWERVP(vp);
1088e4aaf35aSKonstantin Belousov 		vref(lvp);
108916dea834SKonstantin Belousov 		if (!ap->a_unlock_vp) {
109016dea834SKonstantin Belousov 			vhold(vp);
109116dea834SKonstantin Belousov 			vhold(lvp);
109216dea834SKonstantin Belousov 			mp = vp->v_mount;
109316dea834SKonstantin Belousov 			vfs_ref(mp);
1094e4aaf35aSKonstantin Belousov 		}
1095e4aaf35aSKonstantin Belousov 	}
1096e4aaf35aSKonstantin Belousov 
109716dea834SKonstantin Belousov 	res = VOP_VPUT_PAIR(ldvp, lvp != NULL ? &lvp : NULL, true);
109816dea834SKonstantin Belousov 	if (vp != NULL && ap->a_unlock_vp)
109916dea834SKonstantin Belousov 		vrele(vp);
110016dea834SKonstantin Belousov 	vrele(dvp);
1101e4aaf35aSKonstantin Belousov 
110216dea834SKonstantin Belousov 	if (vp == NULL || ap->a_unlock_vp)
110316dea834SKonstantin Belousov 		return (res);
110416dea834SKonstantin Belousov 
110516dea834SKonstantin Belousov 	/* lvp has been unlocked and vp might be reclaimed */
110616dea834SKonstantin Belousov 	VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
110716dea834SKonstantin Belousov 	if (vp->v_data == NULL && vfs_busy(mp, MBF_NOWAIT) == 0) {
110816dea834SKonstantin Belousov 		vput(vp);
110916dea834SKonstantin Belousov 		vget(lvp, LK_EXCLUSIVE | LK_RETRY);
111016dea834SKonstantin Belousov 		if (VN_IS_DOOMED(lvp)) {
111116dea834SKonstantin Belousov 			vput(lvp);
111216dea834SKonstantin Belousov 			vget(vp, LK_EXCLUSIVE | LK_RETRY);
111316dea834SKonstantin Belousov 		} else {
1114e4aaf35aSKonstantin Belousov 			error = null_nodeget(mp, lvp, &vp1);
1115e4aaf35aSKonstantin Belousov 			if (error == 0) {
1116e4aaf35aSKonstantin Belousov 				*vpp = vp1;
111716dea834SKonstantin Belousov 			} else {
111816dea834SKonstantin Belousov 				vget(vp, LK_EXCLUSIVE | LK_RETRY);
1119e4aaf35aSKonstantin Belousov 			}
1120e4aaf35aSKonstantin Belousov 		}
112116dea834SKonstantin Belousov 		vfs_unbusy(mp);
112216dea834SKonstantin Belousov 	}
112316dea834SKonstantin Belousov 	vdrop(lvp);
1124e4aaf35aSKonstantin Belousov 	vdrop(vp);
112516dea834SKonstantin Belousov 	vfs_rel(mp);
112616dea834SKonstantin Belousov 
1127e4aaf35aSKonstantin Belousov 	return (res);
1128e4aaf35aSKonstantin Belousov }
1129e4aaf35aSKonstantin Belousov 
11304cbe4c48SKonstantin Belousov static int
null_getlowvnode(struct vop_getlowvnode_args * ap)11314cbe4c48SKonstantin Belousov null_getlowvnode(struct vop_getlowvnode_args *ap)
11324cbe4c48SKonstantin Belousov {
11334cbe4c48SKonstantin Belousov 	struct vnode *vp, *vpl;
11344cbe4c48SKonstantin Belousov 
11354cbe4c48SKonstantin Belousov 	vp = ap->a_vp;
11364cbe4c48SKonstantin Belousov 	if (vn_lock(vp, LK_SHARED) != 0)
11374cbe4c48SKonstantin Belousov 		return (EBADF);
11384cbe4c48SKonstantin Belousov 
11394cbe4c48SKonstantin Belousov 	vpl = NULLVPTOLOWERVP(vp);
11404cbe4c48SKonstantin Belousov 	vhold(vpl);
11414cbe4c48SKonstantin Belousov 	VOP_UNLOCK(vp);
11424cbe4c48SKonstantin Belousov 	VOP_GETLOWVNODE(vpl, ap->a_vplp, ap->a_flags);
11434cbe4c48SKonstantin Belousov 	vdrop(vpl);
11444cbe4c48SKonstantin Belousov 	return (0);
11454cbe4c48SKonstantin Belousov }
11464cbe4c48SKonstantin Belousov 
1147e4aaf35aSKonstantin Belousov /*
1148df8bae1dSRodney W. Grimes  * Global vfs data structures
1149df8bae1dSRodney W. Grimes  */
1150aec0fb7bSPoul-Henning Kamp struct vop_vector null_vnodeops = {
1151aec0fb7bSPoul-Henning Kamp 	.vop_bypass =		null_bypass,
1152aec0fb7bSPoul-Henning Kamp 	.vop_access =		null_access,
1153c97fcdbaSEdward Tomasz Napierala 	.vop_accessx =		null_accessx,
1154161e9a97SKonstantin Belousov 	.vop_advlock =		null_advlock,
1155de082cd1SKonstantin Belousov 	.vop_advlockpurge =	vop_stdadvlockpurge,
1156aec0fb7bSPoul-Henning Kamp 	.vop_bmap =		VOP_EOPNOTSUPP,
1157fc9fcee0SMateusz Guzik 	.vop_stat =		null_stat,
1158aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		null_getattr,
11594cbe4c48SKonstantin Belousov 	.vop_getlowvnode =	null_getlowvnode,
11604bf5133bSJeff Roberson 	.vop_getwritemount =	null_getwritemount,
1161aec0fb7bSPoul-Henning Kamp 	.vop_inactive =		null_inactive,
116213c73428SMateusz Guzik 	.vop_need_inactive =	null_need_inactive,
1163aa73f8c7SPeter Holm 	.vop_islocked =		vop_stdislocked,
1164d413d210SKonstantin Belousov 	.vop_lock1 =		null_lock,
1165aec0fb7bSPoul-Henning Kamp 	.vop_lookup =		null_lookup,
1166c683c4eeSPoul-Henning Kamp 	.vop_open =		null_open,
1167aec0fb7bSPoul-Henning Kamp 	.vop_print =		null_print,
1168f7af6e5eSKonstantin Belousov 	.vop_read_pgcache =	null_read_pgcache,
1169aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		null_reclaim,
11702d0c83b1SRick Macklem 	.vop_remove =		null_remove,
1171aec0fb7bSPoul-Henning Kamp 	.vop_rename =		null_rename,
1172830cd4b8SKonstantin Belousov 	.vop_rmdir =		null_rmdir,
1173aec0fb7bSPoul-Henning Kamp 	.vop_setattr =		null_setattr,
1174aec0fb7bSPoul-Henning Kamp 	.vop_strategy =		VOP_EOPNOTSUPP,
1175aec0fb7bSPoul-Henning Kamp 	.vop_unlock =		null_unlock,
1176b0f34bb6SKonstantin Belousov 	.vop_vptocnp =		null_vptocnp,
117710bcafe9SPawel Jakub Dawidek 	.vop_vptofh =		null_vptofh,
1178140dedb8SKonstantin Belousov 	.vop_add_writecount =	null_add_writecount,
1179e4aaf35aSKonstantin Belousov 	.vop_vput_pair =	null_vput_pair,
1180326836a1SKonstantin Belousov 	.vop_copy_file_range =	VOP_PANIC,
1181df8bae1dSRodney W. Grimes };
11826fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(null_vnodeops);
1183