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