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