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