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/proc.h>
178 #include <sys/smr.h>
179 #include <sys/sysctl.h>
180 #include <sys/vnode.h>
181 #include <sys/stat.h>
182
183 #include <fs/nullfs/null.h>
184
185 #include <vm/vm.h>
186 #include <vm/vm_extern.h>
187 #include <vm/vm_object.h>
188 #include <vm/vnode_pager.h>
189
190 VFS_SMR_DECLARE;
191
192 static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
193 SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
194 &null_bug_bypass, 0, "");
195
196 /*
197 * Synchronize inotify flags with the lower vnode:
198 * - If the upper vnode has the flag set and the lower does not, then the lower
199 * vnode is unwatched and the upper vnode does not need to go through
200 * VOP_INOTIFY.
201 * - If the lower vnode is watched, then the upper vnode should go through
202 * VOP_INOTIFY, so copy the flag up.
203 */
204 static void
null_copy_inotify(struct vnode * vp,struct vnode * lvp,short flag)205 null_copy_inotify(struct vnode *vp, struct vnode *lvp, short flag)
206 {
207 if ((vn_irflag_read(vp) & flag) != 0) {
208 if (__predict_false((vn_irflag_read(lvp) & flag) == 0))
209 vn_irflag_unset(vp, flag);
210 } else if ((vn_irflag_read(lvp) & flag) != 0) {
211 if (__predict_false((vn_irflag_read(vp) & flag) == 0))
212 vn_irflag_set(vp, flag);
213 }
214 }
215
216 /*
217 * This is the 10-Apr-92 bypass routine.
218 * This version has been optimized for speed, throwing away some
219 * safety checks. It should still always work, but it's not as
220 * robust to programmer errors.
221 *
222 * In general, we map all vnodes going down and unmap them on the way back.
223 * As an exception to this, vnodes can be marked "unmapped" by setting
224 * the Nth bit in operation's vdesc_flags.
225 *
226 * Also, some BSD vnode operations have the side effect of vrele'ing
227 * their arguments. With stacking, the reference counts are held
228 * by the upper node, not the lower one, so we must handle these
229 * side-effects here. This is not of concern in Sun-derived systems
230 * since there are no such side-effects.
231 *
232 * This makes the following assumptions:
233 * - only one returned vpp
234 * - no INOUT vpp's (Sun's vop_open has one of these)
235 * - the vnode operation vector of the first vnode should be used
236 * to determine what implementation of the op should be invoked
237 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
238 * problems on rmdir'ing mount points and renaming?)
239 */
240 int
null_bypass(struct vop_generic_args * ap)241 null_bypass(struct vop_generic_args *ap)
242 {
243 struct vnode **this_vp_p;
244 struct vnode *old_vps[VDESC_MAX_VPS];
245 struct vnode **vps_p[VDESC_MAX_VPS];
246 struct vnode ***vppp;
247 struct vnode *lvp;
248 struct vnodeop_desc *descp = ap->a_desc;
249 int error, i, reles;
250
251 if (null_bug_bypass)
252 printf ("null_bypass: %s\n", descp->vdesc_name);
253
254 #ifdef DIAGNOSTIC
255 /*
256 * We require at least one vp.
257 */
258 if (descp->vdesc_vp_offsets == NULL ||
259 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
260 panic ("null_bypass: no vp's in map");
261 #endif
262
263 /*
264 * Map the vnodes going in.
265 * Later, we'll invoke the operation based on
266 * the first mapped vnode's operation vector.
267 */
268 reles = descp->vdesc_flags;
269 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
270 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
271 break; /* bail out at end of list */
272 vps_p[i] = this_vp_p = VOPARG_OFFSETTO(struct vnode **,
273 descp->vdesc_vp_offsets[i], ap);
274
275 /*
276 * We're not guaranteed that any but the first vnode
277 * are of our type. Check for and don't map any
278 * that aren't. (We must always map first vp or vclean fails.)
279 */
280 if (i != 0 && (*this_vp_p == NULL ||
281 (*this_vp_p)->v_op != &null_vnodeops)) {
282 old_vps[i] = NULL;
283 } else {
284 old_vps[i] = *this_vp_p;
285 *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
286
287 /*
288 * The upper vnode reference to the lower
289 * vnode is the only reference that keeps our
290 * pointer to the lower vnode alive. If lower
291 * vnode is relocked during the VOP call,
292 * upper vnode might become unlocked and
293 * reclaimed, which invalidates our reference.
294 * Add a transient hold around VOP call.
295 */
296 vhold(*this_vp_p);
297
298 /*
299 * XXX - Several operations have the side effect
300 * of vrele'ing their vp's. We must account for
301 * that. (This should go away in the future.)
302 */
303 if (reles & VDESC_VP0_WILLRELE)
304 vref(*this_vp_p);
305 }
306 }
307
308 /*
309 * Call the operation on the lower layer
310 * with the modified argument structure.
311 */
312 if (vps_p[0] != NULL && *vps_p[0] != NULL) {
313 error = ap->a_desc->vdesc_call(ap);
314 } else {
315 printf("null_bypass: no map for %s\n", descp->vdesc_name);
316 error = EINVAL;
317 }
318
319 /*
320 * Maintain the illusion of call-by-value
321 * by restoring vnodes in the argument structure
322 * to their original value.
323 */
324 reles = descp->vdesc_flags;
325 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
326 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
327 break; /* bail out at end of list */
328 if (old_vps[i] != NULL) {
329 lvp = *(vps_p[i]);
330
331 /*
332 * Get rid of the transient hold on lvp. Copy inotify
333 * flags up in case something is watching the lower
334 * layer.
335 *
336 * If lowervp was unlocked during VOP
337 * operation, nullfs upper vnode could have
338 * been reclaimed, which changes its v_vnlock
339 * back to private v_lock. In this case we
340 * must move lock ownership from lower to
341 * upper (reclaimed) vnode.
342 */
343 if (lvp != NULL) {
344 null_copy_inotify(old_vps[i], lvp,
345 VIRF_INOTIFY);
346 null_copy_inotify(old_vps[i], lvp,
347 VIRF_INOTIFY_PARENT);
348 if (VOP_ISLOCKED(lvp) == LK_EXCLUSIVE &&
349 old_vps[i]->v_vnlock != lvp->v_vnlock) {
350 VOP_UNLOCK(lvp);
351 VOP_LOCK(old_vps[i], LK_EXCLUSIVE |
352 LK_RETRY);
353 }
354 vdrop(lvp);
355 }
356
357 *(vps_p[i]) = old_vps[i];
358 #if 0
359 if (reles & VDESC_VP0_WILLUNLOCK)
360 VOP_UNLOCK(*(vps_p[i]), 0);
361 #endif
362 if (reles & VDESC_VP0_WILLRELE)
363 vrele(*(vps_p[i]));
364 }
365 }
366
367 /*
368 * Map the possible out-going vpp
369 * (Assumes that the lower layer always returns
370 * a VREF'ed vpp unless it gets an error.)
371 */
372 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && error == 0) {
373 /*
374 * XXX - even though some ops have vpp returned vp's,
375 * several ops actually vrele this before returning.
376 * We must avoid these ops.
377 * (This should go away when these ops are regularized.)
378 */
379 vppp = VOPARG_OFFSETTO(struct vnode ***,
380 descp->vdesc_vpp_offset, ap);
381 if (*vppp != NULL)
382 error = null_nodeget(old_vps[0]->v_mount, **vppp,
383 *vppp);
384 }
385
386 return (error);
387 }
388
389 static int
null_add_writecount(struct vop_add_writecount_args * ap)390 null_add_writecount(struct vop_add_writecount_args *ap)
391 {
392 struct vnode *lvp, *vp;
393 int error;
394
395 vp = ap->a_vp;
396 lvp = NULLVPTOLOWERVP(vp);
397 VI_LOCK(vp);
398 /* text refs are bypassed to lowervp */
399 VNASSERT(vp->v_writecount >= 0, vp, ("wrong null writecount"));
400 VNASSERT(vp->v_writecount + ap->a_inc >= 0, vp,
401 ("wrong writecount inc %d", ap->a_inc));
402 error = VOP_ADD_WRITECOUNT(lvp, ap->a_inc);
403 if (error == 0)
404 vp->v_writecount += ap->a_inc;
405 VI_UNLOCK(vp);
406 return (error);
407 }
408
409 /*
410 * We have to carry on the locking protocol on the null layer vnodes
411 * as we progress through the tree. We also have to enforce read-only
412 * if this layer is mounted read-only.
413 */
414 static int
null_lookup(struct vop_lookup_args * ap)415 null_lookup(struct vop_lookup_args *ap)
416 {
417 struct componentname *cnp = ap->a_cnp;
418 struct vnode *dvp = ap->a_dvp;
419 uint64_t flags = cnp->cn_flags;
420 struct vnode *vp, *ldvp, *lvp;
421 struct mount *mp;
422 int error;
423
424 mp = dvp->v_mount;
425 if ((flags & ISLASTCN) != 0 && (mp->mnt_flag & MNT_RDONLY) != 0 &&
426 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
427 return (EROFS);
428 /*
429 * Although it is possible to call null_bypass(), we'll do
430 * a direct call to reduce overhead
431 */
432 ldvp = NULLVPTOLOWERVP(dvp);
433 vp = lvp = NULL;
434
435 /*
436 * Renames in the lower mounts might create an inconsistent
437 * configuration where lower vnode is moved out of the directory tree
438 * remounted by our null mount.
439 *
440 * Do not try to handle it fancy, just avoid VOP_LOOKUP() with DOTDOT
441 * name which cannot be handled by the VOP.
442 */
443 if ((flags & ISDOTDOT) != 0) {
444 struct nameidata *ndp;
445
446 if ((ldvp->v_vflag & VV_ROOT) != 0) {
447 KASSERT((dvp->v_vflag & VV_ROOT) == 0,
448 ("ldvp %p fl %#x dvp %p fl %#x flags %#jx",
449 ldvp, ldvp->v_vflag, dvp, dvp->v_vflag,
450 (uintmax_t)flags));
451 return (ENOENT);
452 }
453 ndp = vfs_lookup_nameidata(cnp);
454 if (ndp != NULL && vfs_lookup_isroot(ndp, ldvp))
455 return (ENOENT);
456 }
457
458 /*
459 * Hold ldvp. The reference on it, owned by dvp, is lost in
460 * case of dvp reclamation, and we need ldvp to move our lock
461 * from ldvp to dvp.
462 */
463 vhold(ldvp);
464
465 error = VOP_LOOKUP(ldvp, &lvp, cnp);
466
467 /*
468 * VOP_LOOKUP() on lower vnode may unlock ldvp, which allows
469 * dvp to be reclaimed due to shared v_vnlock. Check for the
470 * doomed state and return error.
471 */
472 if (VN_IS_DOOMED(dvp)) {
473 if (error == 0 || error == EJUSTRETURN) {
474 if (lvp != NULL)
475 vput(lvp);
476 error = ENOENT;
477 }
478
479 /*
480 * If vgone() did reclaimed dvp before curthread
481 * relocked ldvp, the locks of dvp and ldpv are no
482 * longer shared. In this case, relock of ldvp in
483 * lower fs VOP_LOOKUP() does not restore the locking
484 * state of dvp. Compensate for this by unlocking
485 * ldvp and locking dvp, which is also correct if the
486 * locks are still shared.
487 */
488 VOP_UNLOCK(ldvp);
489 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
490 }
491 vdrop(ldvp);
492
493 if (error == EJUSTRETURN && (flags & ISLASTCN) != 0 &&
494 (mp->mnt_flag & MNT_RDONLY) != 0 &&
495 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
496 error = EROFS;
497
498 if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
499 if (ldvp == lvp) {
500 *ap->a_vpp = dvp;
501 vref(dvp);
502 vrele(lvp);
503 } else {
504 error = null_nodeget(mp, lvp, &vp);
505 if (error == 0)
506 *ap->a_vpp = vp;
507 }
508 }
509 return (error);
510 }
511
512 static int
null_open(struct vop_open_args * ap)513 null_open(struct vop_open_args *ap)
514 {
515 int retval;
516 struct vnode *vp, *ldvp;
517
518 vp = ap->a_vp;
519 ldvp = NULLVPTOLOWERVP(vp);
520 retval = null_bypass(&ap->a_gen);
521 if (retval == 0) {
522 vp->v_object = ldvp->v_object;
523 if ((vn_irflag_read(ldvp) & VIRF_PGREAD) != 0) {
524 MPASS(vp->v_object != NULL);
525 if ((vn_irflag_read(vp) & VIRF_PGREAD) == 0) {
526 vn_irflag_set_cond(vp, VIRF_PGREAD);
527 }
528 }
529 }
530 return (retval);
531 }
532
533 /*
534 * Setattr call. Disallow write attempts if the layer is mounted read-only.
535 */
536 static int
null_setattr(struct vop_setattr_args * ap)537 null_setattr(struct vop_setattr_args *ap)
538 {
539 struct vnode *vp = ap->a_vp;
540 struct vattr *vap = ap->a_vap;
541
542 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
543 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
544 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
545 (vp->v_mount->mnt_flag & MNT_RDONLY))
546 return (EROFS);
547 if (vap->va_size != VNOVAL) {
548 switch (vp->v_type) {
549 case VDIR:
550 return (EISDIR);
551 case VCHR:
552 case VBLK:
553 case VSOCK:
554 case VFIFO:
555 if (vap->va_flags != VNOVAL)
556 return (EOPNOTSUPP);
557 return (0);
558 case VREG:
559 case VLNK:
560 default:
561 /*
562 * Disallow write attempts if the filesystem is
563 * mounted read-only.
564 */
565 if (vp->v_mount->mnt_flag & MNT_RDONLY)
566 return (EROFS);
567 }
568 }
569
570 return (null_bypass(&ap->a_gen));
571 }
572
573 /*
574 * We handle stat and getattr only to change the fsid.
575 */
576 static int
null_stat(struct vop_stat_args * ap)577 null_stat(struct vop_stat_args *ap)
578 {
579 int error;
580
581 if ((error = null_bypass(&ap->a_gen)) != 0)
582 return (error);
583
584 ap->a_sb->st_dev = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
585 return (0);
586 }
587
588 static int
null_getattr(struct vop_getattr_args * ap)589 null_getattr(struct vop_getattr_args *ap)
590 {
591 int error;
592
593 if ((error = null_bypass(&ap->a_gen)) != 0)
594 return (error);
595
596 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
597 return (0);
598 }
599
600 /*
601 * Handle to disallow write access if mounted read-only.
602 */
603 static int
null_access(struct vop_access_args * ap)604 null_access(struct vop_access_args *ap)
605 {
606 struct vnode *vp = ap->a_vp;
607 accmode_t accmode = ap->a_accmode;
608
609 /*
610 * Disallow write attempts on read-only layers;
611 * unless the file is a socket, fifo, or a block or
612 * character device resident on the filesystem.
613 */
614 if (accmode & VWRITE) {
615 switch (vp->v_type) {
616 case VDIR:
617 case VLNK:
618 case VREG:
619 if (vp->v_mount->mnt_flag & MNT_RDONLY)
620 return (EROFS);
621 break;
622 default:
623 break;
624 }
625 }
626 return (null_bypass(&ap->a_gen));
627 }
628
629 static int
null_accessx(struct vop_accessx_args * ap)630 null_accessx(struct vop_accessx_args *ap)
631 {
632 struct vnode *vp = ap->a_vp;
633 accmode_t accmode = ap->a_accmode;
634
635 /*
636 * Disallow write attempts on read-only layers;
637 * unless the file is a socket, fifo, or a block or
638 * character device resident on the filesystem.
639 */
640 if (accmode & VWRITE) {
641 switch (vp->v_type) {
642 case VDIR:
643 case VLNK:
644 case VREG:
645 if (vp->v_mount->mnt_flag & MNT_RDONLY)
646 return (EROFS);
647 break;
648 default:
649 break;
650 }
651 }
652 return (null_bypass(&ap->a_gen));
653 }
654
655 /*
656 * Increasing refcount of lower vnode is needed at least for the case
657 * when lower FS is NFS to do sillyrename if the file is in use.
658 * Unfortunately v_usecount is incremented in many places in
659 * the kernel and, as such, there may be races that result in
660 * the NFS client doing an extraneous silly rename, but that seems
661 * preferable to not doing a silly rename when it is needed.
662 */
663 static int
null_remove(struct vop_remove_args * ap)664 null_remove(struct vop_remove_args *ap)
665 {
666 int retval, vreleit;
667 struct vnode *lvp, *vp;
668
669 vp = ap->a_vp;
670 if (vrefcnt(vp) > 1) {
671 lvp = NULLVPTOLOWERVP(vp);
672 vref(lvp);
673 vreleit = 1;
674 } else
675 vreleit = 0;
676 VTONULL(vp)->null_flags |= NULLV_DROP;
677 retval = null_bypass(&ap->a_gen);
678 if (vreleit != 0)
679 vrele(lvp);
680 return (retval);
681 }
682
683 /*
684 * We handle this to eliminate null FS to lower FS
685 * file moving. Don't know why we don't allow this,
686 * possibly we should.
687 */
688 static int
null_rename(struct vop_rename_args * ap)689 null_rename(struct vop_rename_args *ap)
690 {
691 struct vnode *fdvp, *fvp, *tdvp, *tvp;
692 struct vnode *lfdvp, *lfvp, *ltdvp, *ltvp;
693 struct null_node *fdnn, *fnn, *tdnn, *tnn;
694 int error;
695
696 tdvp = ap->a_tdvp;
697 fvp = ap->a_fvp;
698 fdvp = ap->a_fdvp;
699 tvp = ap->a_tvp;
700 lfdvp = NULL;
701
702 /* Check for cross-device rename. */
703 if ((fvp->v_mount != tdvp->v_mount) ||
704 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
705 error = EXDEV;
706 goto upper_err;
707 }
708
709 VI_LOCK(fdvp);
710 fdnn = VTONULL(fdvp);
711 if (fdnn == NULL) { /* fdvp is not locked, can be doomed */
712 VI_UNLOCK(fdvp);
713 error = ENOENT;
714 goto upper_err;
715 }
716 lfdvp = fdnn->null_lowervp;
717 vref(lfdvp);
718 VI_UNLOCK(fdvp);
719
720 VI_LOCK(fvp);
721 fnn = VTONULL(fvp);
722 if (fnn == NULL) {
723 VI_UNLOCK(fvp);
724 error = ENOENT;
725 goto upper_err;
726 }
727 lfvp = fnn->null_lowervp;
728 vref(lfvp);
729 VI_UNLOCK(fvp);
730
731 tdnn = VTONULL(tdvp);
732 ltdvp = tdnn->null_lowervp;
733 vref(ltdvp);
734
735 if (tvp != NULL) {
736 tnn = VTONULL(tvp);
737 ltvp = tnn->null_lowervp;
738 vref(ltvp);
739 tnn->null_flags |= NULLV_DROP;
740 } else {
741 ltvp = NULL;
742 }
743
744 error = VOP_RENAME(lfdvp, lfvp, ap->a_fcnp, ltdvp, ltvp, ap->a_tcnp);
745 vrele(fdvp);
746 vrele(fvp);
747 vrele(tdvp);
748 if (tvp != NULL)
749 vrele(tvp);
750 return (error);
751
752 upper_err:
753 if (tdvp == tvp)
754 vrele(tdvp);
755 else
756 vput(tdvp);
757 if (tvp)
758 vput(tvp);
759 if (lfdvp != NULL)
760 vrele(lfdvp);
761 vrele(fdvp);
762 vrele(fvp);
763 return (error);
764 }
765
766 static int
null_rmdir(struct vop_rmdir_args * ap)767 null_rmdir(struct vop_rmdir_args *ap)
768 {
769
770 VTONULL(ap->a_vp)->null_flags |= NULLV_DROP;
771 return (null_bypass(&ap->a_gen));
772 }
773
774 /*
775 * We need to process our own vnode lock and then clear the interlock flag as
776 * it applies only to our vnode, not the vnodes below us on the stack.
777 *
778 * We have to hold the vnode here to solve a potential reclaim race. If we're
779 * forcibly vgone'd while we still have refs, a thread could be sleeping inside
780 * the lowervp's vop_lock routine. When we vgone we will drop our last ref to
781 * the lowervp, which would allow it to be reclaimed. The lowervp could then
782 * be recycled, in which case it is not legal to be sleeping in its VOP. We
783 * prevent it from being recycled by holding the vnode here.
784 */
785 static struct vnode *
null_lock_prep_with_smr(struct vop_lock1_args * ap)786 null_lock_prep_with_smr(struct vop_lock1_args *ap)
787 {
788 struct null_node *nn;
789 struct vnode *lvp;
790
791 vfs_smr_enter();
792
793 lvp = NULL;
794
795 nn = VTONULL_SMR(ap->a_vp);
796 if (__predict_true(nn != NULL)) {
797 lvp = nn->null_lowervp;
798 if (lvp != NULL && !vhold_smr(lvp))
799 lvp = NULL;
800 }
801
802 vfs_smr_exit();
803 return (lvp);
804 }
805
806 static struct vnode *
null_lock_prep_with_interlock(struct vop_lock1_args * ap)807 null_lock_prep_with_interlock(struct vop_lock1_args *ap)
808 {
809 struct null_node *nn;
810 struct vnode *lvp;
811
812 ASSERT_VI_LOCKED(ap->a_vp, __func__);
813
814 ap->a_flags &= ~LK_INTERLOCK;
815
816 lvp = NULL;
817
818 nn = VTONULL(ap->a_vp);
819 if (__predict_true(nn != NULL)) {
820 lvp = nn->null_lowervp;
821 if (lvp != NULL)
822 vholdnz(lvp);
823 }
824 VI_UNLOCK(ap->a_vp);
825 return (lvp);
826 }
827
828 static int
null_lock(struct vop_lock1_args * ap)829 null_lock(struct vop_lock1_args *ap)
830 {
831 struct vnode *lvp;
832 int error, flags;
833
834 if (__predict_true((ap->a_flags & LK_INTERLOCK) == 0)) {
835 lvp = null_lock_prep_with_smr(ap);
836 if (__predict_false(lvp == NULL)) {
837 VI_LOCK(ap->a_vp);
838 lvp = null_lock_prep_with_interlock(ap);
839 }
840 } else {
841 lvp = null_lock_prep_with_interlock(ap);
842 }
843
844 ASSERT_VI_UNLOCKED(ap->a_vp, __func__);
845
846 if (__predict_false(lvp == NULL))
847 return (vop_stdlock(ap));
848
849 VNPASS(lvp->v_holdcnt > 0, lvp);
850 error = VOP_LOCK(lvp, ap->a_flags);
851 /*
852 * We might have slept to get the lock and someone might have
853 * clean our vnode already, switching vnode lock from one in
854 * lowervp to v_lock in our own vnode structure. Handle this
855 * case by reacquiring correct lock in requested mode.
856 */
857 if (VTONULL(ap->a_vp) == NULL && error == 0) {
858 flags = ap->a_flags;
859 ap->a_flags &= ~LK_TYPE_MASK;
860 switch (flags & LK_TYPE_MASK) {
861 case LK_SHARED:
862 ap->a_flags |= LK_SHARED;
863 break;
864 case LK_UPGRADE:
865 case LK_EXCLUSIVE:
866 ap->a_flags |= LK_EXCLUSIVE;
867 break;
868 default:
869 panic("Unsupported lock request %d\n",
870 flags);
871 }
872 VOP_UNLOCK(lvp);
873 error = vop_stdlock(ap);
874 }
875 vdrop(lvp);
876 return (error);
877 }
878
879 static int
null_unlock(struct vop_unlock_args * ap)880 null_unlock(struct vop_unlock_args *ap)
881 {
882 struct vnode *vp = ap->a_vp;
883 struct null_node *nn;
884 struct vnode *lvp;
885 int error;
886
887 /*
888 * Contrary to null_lock, we don't need to hold the vnode around
889 * unlock.
890 *
891 * We hold the lock, which means we can't be racing against vgone.
892 *
893 * At the same time VOP_UNLOCK promises to not touch anything after
894 * it finishes unlock, just like we don't.
895 *
896 * vop_stdunlock for a doomed vnode matches doomed locking in null_lock.
897 */
898 nn = VTONULL(vp);
899 if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
900 error = VOP_UNLOCK(lvp);
901 } else {
902 error = vop_stdunlock(ap);
903 }
904
905 return (error);
906 }
907
908 /*
909 * Do not allow the VOP_INACTIVE to be passed to the lower layer,
910 * since the reference count on the lower vnode is not related to
911 * ours.
912 */
913 static int
null_want_recycle(struct vnode * vp)914 null_want_recycle(struct vnode *vp)
915 {
916 struct vnode *lvp;
917 struct null_node *xp;
918 struct mount *mp;
919 struct null_mount *xmp;
920
921 xp = VTONULL(vp);
922 lvp = NULLVPTOLOWERVP(vp);
923 mp = vp->v_mount;
924 xmp = MOUNTTONULLMOUNT(mp);
925 if ((xmp->nullm_flags & NULLM_CACHE) == 0 ||
926 (xp->null_flags & NULLV_DROP) != 0 ||
927 (lvp->v_vflag & VV_NOSYNC) != 0) {
928 /*
929 * If this is the last reference and caching of the
930 * nullfs vnodes is not enabled, or the lower vnode is
931 * deleted, then free up the vnode so as not to tie up
932 * the lower vnodes.
933 */
934 return (1);
935 }
936 return (0);
937 }
938
939 static int
null_inactive(struct vop_inactive_args * ap)940 null_inactive(struct vop_inactive_args *ap)
941 {
942 struct vnode *vp;
943
944 vp = ap->a_vp;
945 if (null_want_recycle(vp)) {
946 vp->v_object = NULL;
947 vrecycle(vp);
948 }
949 return (0);
950 }
951
952 static int
null_need_inactive(struct vop_need_inactive_args * ap)953 null_need_inactive(struct vop_need_inactive_args *ap)
954 {
955
956 return (null_want_recycle(ap->a_vp) || vn_need_pageq_flush(ap->a_vp));
957 }
958
959 /*
960 * Now, the nullfs vnode and, due to the sharing lock, the lower
961 * vnode, are exclusively locked, and we shall destroy the null vnode.
962 */
963 static int
null_reclaim(struct vop_reclaim_args * ap)964 null_reclaim(struct vop_reclaim_args *ap)
965 {
966 struct vnode *vp;
967 struct null_node *xp;
968 struct vnode *lowervp;
969
970 vp = ap->a_vp;
971 xp = VTONULL(vp);
972 lowervp = xp->null_lowervp;
973
974 KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock,
975 ("Reclaiming incomplete null vnode %p", vp));
976
977 null_hashrem(xp);
978 /*
979 * Use the interlock to protect the clearing of v_data to
980 * prevent faults in null_lock().
981 */
982 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
983 VI_LOCK(vp);
984 vp->v_data = NULL;
985 vp->v_object = NULL;
986 vp->v_vnlock = &vp->v_lock;
987
988 /*
989 * If we were opened for write, we leased the write reference
990 * to the lower vnode. If this is a reclamation due to the
991 * forced unmount, undo the reference now.
992 */
993 if (vp->v_writecount > 0)
994 VOP_ADD_WRITECOUNT(lowervp, -vp->v_writecount);
995 else if (vp->v_writecount < 0)
996 vp->v_writecount = 0;
997
998 VI_UNLOCK(vp);
999
1000 if ((xp->null_flags & NULLV_NOUNLOCK) != 0)
1001 vunref(lowervp);
1002 else
1003 vput(lowervp);
1004 uma_zfree_smr(null_node_zone, xp);
1005
1006 return (0);
1007 }
1008
1009 static int
null_print(struct vop_print_args * ap)1010 null_print(struct vop_print_args *ap)
1011 {
1012 struct vnode *vp = ap->a_vp;
1013
1014 printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp);
1015 return (0);
1016 }
1017
1018 /* ARGSUSED */
1019 static int
null_getwritemount(struct vop_getwritemount_args * ap)1020 null_getwritemount(struct vop_getwritemount_args *ap)
1021 {
1022 struct null_node *xp;
1023 struct vnode *lowervp;
1024 struct vnode *vp;
1025
1026 vp = ap->a_vp;
1027 VI_LOCK(vp);
1028 xp = VTONULL(vp);
1029 if (xp && (lowervp = xp->null_lowervp)) {
1030 vholdnz(lowervp);
1031 VI_UNLOCK(vp);
1032 VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);
1033 vdrop(lowervp);
1034 } else {
1035 VI_UNLOCK(vp);
1036 *(ap->a_mpp) = NULL;
1037 }
1038 return (0);
1039 }
1040
1041 static int
null_vptofh(struct vop_vptofh_args * ap)1042 null_vptofh(struct vop_vptofh_args *ap)
1043 {
1044 struct vnode *lvp;
1045
1046 lvp = NULLVPTOLOWERVP(ap->a_vp);
1047 return VOP_VPTOFH(lvp, ap->a_fhp);
1048 }
1049
1050 static int
null_vptocnp(struct vop_vptocnp_args * ap)1051 null_vptocnp(struct vop_vptocnp_args *ap)
1052 {
1053 struct vnode *vp = ap->a_vp;
1054 struct vnode **dvp = ap->a_vpp;
1055 struct vnode *lvp, *ldvp;
1056 struct mount *mp;
1057 int error, locked;
1058
1059 locked = VOP_ISLOCKED(vp);
1060 lvp = NULLVPTOLOWERVP(vp);
1061 mp = vp->v_mount;
1062 error = vfs_busy(mp, MBF_NOWAIT);
1063 if (error != 0)
1064 return (error);
1065 vhold(lvp);
1066 VOP_UNLOCK(vp); /* vp is held by vn_vptocnp_locked that called us */
1067 ldvp = lvp;
1068 vref(lvp);
1069 error = vn_vptocnp(&ldvp, ap->a_buf, ap->a_buflen);
1070 vdrop(lvp);
1071 if (error != 0) {
1072 vn_lock(vp, locked | LK_RETRY);
1073 vfs_unbusy(mp);
1074 return (ENOENT);
1075 }
1076
1077 error = vn_lock(ldvp, LK_SHARED);
1078 if (error != 0) {
1079 vrele(ldvp);
1080 vn_lock(vp, locked | LK_RETRY);
1081 vfs_unbusy(mp);
1082 return (ENOENT);
1083 }
1084 error = null_nodeget(mp, ldvp, dvp);
1085 if (error == 0) {
1086 #ifdef DIAGNOSTIC
1087 NULLVPTOLOWERVP(*dvp);
1088 #endif
1089 VOP_UNLOCK(*dvp); /* keep reference on *dvp */
1090 }
1091 vn_lock(vp, locked | LK_RETRY);
1092 vfs_unbusy(mp);
1093 return (error);
1094 }
1095
1096 static int
null_read_pgcache(struct vop_read_pgcache_args * ap)1097 null_read_pgcache(struct vop_read_pgcache_args *ap)
1098 {
1099 struct vnode *lvp, *vp;
1100 struct null_node *xp;
1101 int error;
1102
1103 vp = ap->a_vp;
1104 VI_LOCK(vp);
1105 xp = VTONULL(vp);
1106 if (xp == NULL) {
1107 VI_UNLOCK(vp);
1108 return (EJUSTRETURN);
1109 }
1110 lvp = xp->null_lowervp;
1111 vref(lvp);
1112 VI_UNLOCK(vp);
1113 error = VOP_READ_PGCACHE(lvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1114 vrele(lvp);
1115 return (error);
1116 }
1117
1118 static int
null_advlock(struct vop_advlock_args * ap)1119 null_advlock(struct vop_advlock_args *ap)
1120 {
1121 struct vnode *lvp, *vp;
1122 struct null_node *xp;
1123 int error;
1124
1125 vp = ap->a_vp;
1126 VI_LOCK(vp);
1127 xp = VTONULL(vp);
1128 if (xp == NULL) {
1129 VI_UNLOCK(vp);
1130 return (EBADF);
1131 }
1132 lvp = xp->null_lowervp;
1133 vref(lvp);
1134 VI_UNLOCK(vp);
1135 error = VOP_ADVLOCK(lvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);
1136 vrele(lvp);
1137 return (error);
1138 }
1139
1140 /*
1141 * Avoid standard bypass, since lower dvp and vp could be no longer
1142 * valid after vput().
1143 */
1144 static int
null_vput_pair(struct vop_vput_pair_args * ap)1145 null_vput_pair(struct vop_vput_pair_args *ap)
1146 {
1147 struct mount *mp;
1148 struct vnode *dvp, *ldvp, *lvp, *vp, *vp1, **vpp;
1149 int error, res;
1150
1151 dvp = ap->a_dvp;
1152 ldvp = NULLVPTOLOWERVP(dvp);
1153 vref(ldvp);
1154
1155 vpp = ap->a_vpp;
1156 vp = NULL;
1157 lvp = NULL;
1158 mp = NULL;
1159 if (vpp != NULL)
1160 vp = *vpp;
1161 if (vp != NULL) {
1162 lvp = NULLVPTOLOWERVP(vp);
1163 vref(lvp);
1164 if (!ap->a_unlock_vp) {
1165 vhold(vp);
1166 vhold(lvp);
1167 mp = vp->v_mount;
1168 vfs_ref(mp);
1169 }
1170 }
1171
1172 res = VOP_VPUT_PAIR(ldvp, lvp != NULL ? &lvp : NULL, true);
1173 if (vp != NULL && ap->a_unlock_vp)
1174 vrele(vp);
1175 vrele(dvp);
1176
1177 if (vp == NULL || ap->a_unlock_vp)
1178 return (res);
1179
1180 /* lvp has been unlocked and vp might be reclaimed */
1181 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1182 if (vp->v_data == NULL && vfs_busy(mp, MBF_NOWAIT) == 0) {
1183 vput(vp);
1184 vget(lvp, LK_EXCLUSIVE | LK_RETRY);
1185 if (VN_IS_DOOMED(lvp)) {
1186 vput(lvp);
1187 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1188 } else {
1189 error = null_nodeget(mp, lvp, &vp1);
1190 if (error == 0) {
1191 *vpp = vp1;
1192 } else {
1193 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1194 }
1195 }
1196 vfs_unbusy(mp);
1197 }
1198 vdrop(lvp);
1199 vdrop(vp);
1200 vfs_rel(mp);
1201
1202 return (res);
1203 }
1204
1205 static int
null_getlowvnode(struct vop_getlowvnode_args * ap)1206 null_getlowvnode(struct vop_getlowvnode_args *ap)
1207 {
1208 struct vnode *vp, *vpl;
1209
1210 vp = ap->a_vp;
1211 if (vn_lock(vp, LK_SHARED) != 0)
1212 return (EBADF);
1213
1214 vpl = NULLVPTOLOWERVP(vp);
1215 vhold(vpl);
1216 VOP_UNLOCK(vp);
1217 VOP_GETLOWVNODE(vpl, ap->a_vplp, ap->a_flags);
1218 vdrop(vpl);
1219 return (0);
1220 }
1221
1222 /*
1223 * Global vfs data structures
1224 */
1225 struct vop_vector null_vnodeops = {
1226 .vop_bypass = null_bypass,
1227 .vop_access = null_access,
1228 .vop_accessx = null_accessx,
1229 .vop_advlock = null_advlock,
1230 .vop_advlockpurge = vop_stdadvlockpurge,
1231 .vop_bmap = VOP_EOPNOTSUPP,
1232 .vop_stat = null_stat,
1233 .vop_getattr = null_getattr,
1234 .vop_getlowvnode = null_getlowvnode,
1235 .vop_getwritemount = null_getwritemount,
1236 .vop_inactive = null_inactive,
1237 .vop_need_inactive = null_need_inactive,
1238 .vop_islocked = vop_stdislocked,
1239 .vop_lock1 = null_lock,
1240 .vop_lookup = null_lookup,
1241 .vop_open = null_open,
1242 .vop_print = null_print,
1243 .vop_read_pgcache = null_read_pgcache,
1244 .vop_reclaim = null_reclaim,
1245 .vop_remove = null_remove,
1246 .vop_rename = null_rename,
1247 .vop_rmdir = null_rmdir,
1248 .vop_setattr = null_setattr,
1249 .vop_strategy = VOP_EOPNOTSUPP,
1250 .vop_unlock = null_unlock,
1251 .vop_vptocnp = null_vptocnp,
1252 .vop_vptofh = null_vptofh,
1253 .vop_add_writecount = null_add_writecount,
1254 .vop_vput_pair = null_vput_pair,
1255 .vop_copy_file_range = VOP_PANIC,
1256 };
1257 VFS_VOP_VECTOR_REGISTER(null_vnodeops);
1258