1 /* $OpenBSD: vfs_subr.c,v 1.315 2022/03/27 16:19:39 semarie Exp $ */ 2 /* $NetBSD: vfs_subr.c,v 1.53 1996/04/22 01:39:13 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94 38 */ 39 40 /* 41 * External virtual filesystem routines 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/sysctl.h> 48 #include <sys/mount.h> 49 #include <sys/time.h> 50 #include <sys/fcntl.h> 51 #include <sys/kernel.h> 52 #include <sys/conf.h> 53 #include <sys/vnode.h> 54 #include <sys/lock.h> 55 #include <sys/lockf.h> 56 #include <sys/stat.h> 57 #include <sys/acct.h> 58 #include <sys/namei.h> 59 #include <sys/ucred.h> 60 #include <sys/buf.h> 61 #include <sys/errno.h> 62 #include <sys/malloc.h> 63 #include <sys/mbuf.h> 64 #include <sys/syscallargs.h> 65 #include <sys/pool.h> 66 #include <sys/tree.h> 67 #include <sys/specdev.h> 68 #include <sys/atomic.h> 69 70 #include <netinet/in.h> 71 72 #include <uvm/uvm_extern.h> 73 #include <uvm/uvm_vnode.h> 74 75 #include "softraid.h" 76 77 void sr_quiesce(void); 78 79 enum vtype iftovt_tab[16] = { 80 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 81 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 82 }; 83 84 int vttoif_tab[9] = { 85 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 86 S_IFSOCK, S_IFIFO, S_IFMT, 87 }; 88 89 int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 90 int suid_clear = 1; /* 1 => clear SUID / SGID on owner change */ 91 92 /* 93 * Insq/Remq for the vnode usage lists. 94 */ 95 #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) 96 #define bufremvn(bp) { \ 97 LIST_REMOVE(bp, b_vnbufs); \ 98 LIST_NEXT(bp, b_vnbufs) = NOLIST; \ 99 } 100 101 TAILQ_HEAD(freelst, vnode); 102 struct freelst vnode_hold_list; /* list of vnodes referencing buffers */ 103 struct freelst vnode_free_list; /* vnode free list */ 104 105 struct mntlist mountlist; /* mounted filesystem list */ 106 107 void vclean(struct vnode *, int, struct proc *); 108 109 void insmntque(struct vnode *, struct mount *); 110 int getdevvp(dev_t, struct vnode **, enum vtype); 111 112 int vfs_hang_addrlist(struct mount *, struct netexport *, 113 struct export_args *); 114 int vfs_free_netcred(struct radix_node *, void *, u_int); 115 void vfs_free_addrlist(struct netexport *); 116 void vputonfreelist(struct vnode *); 117 118 int vflush_vnode(struct vnode *, void *); 119 int maxvnodes; 120 121 struct mutex vnode_mtx = MUTEX_INITIALIZER(IPL_BIO); 122 123 void vfs_unmountall(void); 124 125 #ifdef DEBUG 126 void printlockedvnodes(void); 127 #endif 128 129 struct pool vnode_pool; 130 struct pool uvm_vnode_pool; 131 132 static inline int rb_buf_compare(const struct buf *b1, const struct buf *b2); 133 RBT_GENERATE(buf_rb_bufs, buf, b_rbbufs, rb_buf_compare); 134 135 static inline int 136 rb_buf_compare(const struct buf *b1, const struct buf *b2) 137 { 138 if (b1->b_lblkno < b2->b_lblkno) 139 return(-1); 140 if (b1->b_lblkno > b2->b_lblkno) 141 return(1); 142 return(0); 143 } 144 145 /* 146 * Initialize the vnode management data structures. 147 */ 148 void 149 vntblinit(void) 150 { 151 /* buffer cache may need a vnode for each buffer */ 152 maxvnodes = 2 * initialvnodes; 153 pool_init(&vnode_pool, sizeof(struct vnode), 0, IPL_NONE, 154 PR_WAITOK, "vnodes", NULL); 155 pool_init(&uvm_vnode_pool, sizeof(struct uvm_vnode), 0, IPL_NONE, 156 PR_WAITOK, "uvmvnodes", NULL); 157 TAILQ_INIT(&vnode_hold_list); 158 TAILQ_INIT(&vnode_free_list); 159 TAILQ_INIT(&mountlist); 160 /* 161 * Initialize the filesystem syncer. 162 */ 163 vn_initialize_syncerd(); 164 165 #ifdef NFSSERVER 166 rn_init(sizeof(struct sockaddr_in)); 167 #endif /* NFSSERVER */ 168 } 169 170 /* 171 * Allocate a mount point. 172 * 173 * The returned mount point is marked as busy. 174 */ 175 struct mount * 176 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp) 177 { 178 struct mount *mp; 179 180 mp = malloc(sizeof(*mp), M_MOUNT, M_WAITOK|M_ZERO); 181 rw_init_flags(&mp->mnt_lock, "vfslock", RWL_IS_VNODE); 182 (void)vfs_busy(mp, VB_READ|VB_NOWAIT); 183 184 TAILQ_INIT(&mp->mnt_vnodelist); 185 mp->mnt_vnodecovered = vp; 186 187 atomic_inc_int(&vfsp->vfc_refcount); 188 mp->mnt_vfc = vfsp; 189 mp->mnt_op = vfsp->vfc_vfsops; 190 mp->mnt_flag = vfsp->vfc_flags; 191 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 192 193 return (mp); 194 } 195 196 /* 197 * Release a mount point. 198 */ 199 void 200 vfs_mount_free(struct mount *mp) 201 { 202 atomic_dec_int(&mp->mnt_vfc->vfc_refcount); 203 free(mp, M_MOUNT, sizeof(*mp)); 204 } 205 206 /* 207 * Mark a mount point as busy. Used to synchronize access and to delay 208 * unmounting. 209 * 210 * Default behaviour is to attempt getting a READ lock and in case of an 211 * ongoing unmount, to wait for it to finish and then return failure. 212 */ 213 int 214 vfs_busy(struct mount *mp, int flags) 215 { 216 int rwflags = 0; 217 218 if (flags & VB_WRITE) 219 rwflags |= RW_WRITE; 220 else 221 rwflags |= RW_READ; 222 223 if (flags & VB_WAIT) 224 rwflags |= RW_SLEEPFAIL; 225 else 226 rwflags |= RW_NOSLEEP; 227 228 #ifdef WITNESS 229 if (flags & VB_DUPOK) 230 rwflags |= RW_DUPOK; 231 #endif 232 233 if (rw_enter(&mp->mnt_lock, rwflags)) 234 return (EBUSY); 235 236 return (0); 237 } 238 239 /* 240 * Free a busy file system 241 */ 242 void 243 vfs_unbusy(struct mount *mp) 244 { 245 rw_exit(&mp->mnt_lock); 246 } 247 248 int 249 vfs_isbusy(struct mount *mp) 250 { 251 if (RWLOCK_OWNER(&mp->mnt_lock) > 0) 252 return (1); 253 else 254 return (0); 255 } 256 257 /* 258 * Lookup a filesystem type, and if found allocate and initialize 259 * a mount structure for it. 260 * 261 * Devname is usually updated by mount(8) after booting. 262 */ 263 int 264 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp) 265 { 266 struct vfsconf *vfsp; 267 struct mount *mp; 268 269 vfsp = vfs_byname(fstypename); 270 if (vfsp == NULL) 271 return (ENODEV); 272 mp = vfs_mount_alloc(NULLVP, vfsp); 273 mp->mnt_flag |= MNT_RDONLY; 274 mp->mnt_stat.f_mntonname[0] = '/'; 275 copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN, NULL); 276 copystr(devname, mp->mnt_stat.f_mntfromspec, MNAMELEN, NULL); 277 *mpp = mp; 278 return (0); 279 } 280 281 /* 282 * Lookup a mount point by filesystem identifier. 283 */ 284 struct mount * 285 vfs_getvfs(fsid_t *fsid) 286 { 287 struct mount *mp; 288 289 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 290 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 291 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 292 return (mp); 293 } 294 } 295 296 return (NULL); 297 } 298 299 300 /* 301 * Get a new unique fsid 302 */ 303 void 304 vfs_getnewfsid(struct mount *mp) 305 { 306 static u_short xxxfs_mntid; 307 308 fsid_t tfsid; 309 int mtype; 310 311 mtype = mp->mnt_vfc->vfc_typenum; 312 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0); 313 mp->mnt_stat.f_fsid.val[1] = mtype; 314 if (xxxfs_mntid == 0) 315 ++xxxfs_mntid; 316 tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid); 317 tfsid.val[1] = mtype; 318 if (!TAILQ_EMPTY(&mountlist)) { 319 while (vfs_getvfs(&tfsid)) { 320 tfsid.val[0]++; 321 xxxfs_mntid++; 322 } 323 } 324 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 325 } 326 327 /* 328 * Set vnode attributes to VNOVAL 329 */ 330 void 331 vattr_null(struct vattr *vap) 332 { 333 334 vap->va_type = VNON; 335 /* 336 * Don't get fancy: u_quad_t = u_int = VNOVAL leaves the u_quad_t 337 * with 2^31-1 instead of 2^64-1. Just write'm out and let 338 * the compiler do its job. 339 */ 340 vap->va_mode = VNOVAL; 341 vap->va_nlink = VNOVAL; 342 vap->va_uid = VNOVAL; 343 vap->va_gid = VNOVAL; 344 vap->va_fsid = VNOVAL; 345 vap->va_fileid = VNOVAL; 346 vap->va_size = VNOVAL; 347 vap->va_blocksize = VNOVAL; 348 vap->va_atime.tv_sec = VNOVAL; 349 vap->va_atime.tv_nsec = VNOVAL; 350 vap->va_mtime.tv_sec = VNOVAL; 351 vap->va_mtime.tv_nsec = VNOVAL; 352 vap->va_ctime.tv_sec = VNOVAL; 353 vap->va_ctime.tv_nsec = VNOVAL; 354 vap->va_gen = VNOVAL; 355 vap->va_flags = VNOVAL; 356 vap->va_rdev = VNOVAL; 357 vap->va_bytes = VNOVAL; 358 vap->va_filerev = VNOVAL; 359 vap->va_vaflags = 0; 360 } 361 362 /* 363 * Routines having to do with the management of the vnode table. 364 */ 365 long numvnodes; 366 367 /* 368 * Return the next vnode from the free list. 369 */ 370 int 371 getnewvnode(enum vtagtype tag, struct mount *mp, const struct vops *vops, 372 struct vnode **vpp) 373 { 374 struct proc *p = curproc; 375 struct freelst *listhd; 376 static int toggle; 377 struct vnode *vp; 378 int s; 379 380 /* 381 * allow maxvnodes to increase if the buffer cache itself 382 * is big enough to justify it. (we don't shrink it ever) 383 */ 384 maxvnodes = maxvnodes < bcstats.numbufs ? bcstats.numbufs 385 : maxvnodes; 386 387 /* 388 * We must choose whether to allocate a new vnode or recycle an 389 * existing one. The criterion for allocating a new one is that 390 * the total number of vnodes is less than the number desired or 391 * there are no vnodes on either free list. Generally we only 392 * want to recycle vnodes that have no buffers associated with 393 * them, so we look first on the vnode_free_list. If it is empty, 394 * we next consider vnodes with referencing buffers on the 395 * vnode_hold_list. The toggle ensures that half the time we 396 * will use a buffer from the vnode_hold_list, and half the time 397 * we will allocate a new one unless the list has grown to twice 398 * the desired size. We are reticent to recycle vnodes from the 399 * vnode_hold_list because we will lose the identity of all its 400 * referencing buffers. 401 */ 402 toggle ^= 1; 403 if (numvnodes / 2 > maxvnodes) 404 toggle = 0; 405 406 s = splbio(); 407 if ((numvnodes < maxvnodes) || 408 ((TAILQ_FIRST(listhd = &vnode_free_list) == NULL) && 409 ((TAILQ_FIRST(listhd = &vnode_hold_list) == NULL) || toggle))) { 410 splx(s); 411 vp = pool_get(&vnode_pool, PR_WAITOK | PR_ZERO); 412 vp->v_uvm = pool_get(&uvm_vnode_pool, PR_WAITOK | PR_ZERO); 413 vp->v_uvm->u_vnode = vp; 414 uvm_obj_init(&vp->v_uvm->u_obj, &uvm_vnodeops, 0); 415 RBT_INIT(buf_rb_bufs, &vp->v_bufs_tree); 416 cache_tree_init(&vp->v_nc_tree); 417 TAILQ_INIT(&vp->v_cache_dst); 418 numvnodes++; 419 } else { 420 TAILQ_FOREACH(vp, listhd, v_freelist) { 421 if (VOP_ISLOCKED(vp) == 0) 422 break; 423 } 424 /* 425 * Unless this is a bad time of the month, at most 426 * the first NCPUS items on the free list are 427 * locked, so this is close enough to being empty. 428 */ 429 if (vp == NULL) { 430 splx(s); 431 tablefull("vnode"); 432 *vpp = NULL; 433 return (ENFILE); 434 } 435 436 #ifdef DIAGNOSTIC 437 if (vp->v_usecount) { 438 vprint("free vnode", vp); 439 panic("free vnode isn't"); 440 } 441 #endif 442 443 TAILQ_REMOVE(listhd, vp, v_freelist); 444 vp->v_bioflag &= ~VBIOONFREELIST; 445 splx(s); 446 447 if (vp->v_type != VBAD) 448 vgonel(vp, p); 449 #ifdef DIAGNOSTIC 450 if (vp->v_data) { 451 vprint("cleaned vnode", vp); 452 panic("cleaned vnode isn't"); 453 } 454 s = splbio(); 455 if (vp->v_numoutput) 456 panic("Clean vnode has pending I/O's"); 457 splx(s); 458 #endif 459 vp->v_flag = 0; 460 vp->v_socket = NULL; 461 } 462 cache_purge(vp); 463 vp->v_type = VNON; 464 vp->v_tag = tag; 465 vp->v_op = vops; 466 insmntque(vp, mp); 467 *vpp = vp; 468 vp->v_usecount = 1; 469 vp->v_data = NULL; 470 return (0); 471 } 472 473 /* 474 * Move a vnode from one mount queue to another. 475 */ 476 void 477 insmntque(struct vnode *vp, struct mount *mp) 478 { 479 /* 480 * Delete from old mount point vnode list, if on one. 481 */ 482 if (vp->v_mount != NULL) 483 TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vp, v_mntvnodes); 484 /* 485 * Insert into list of vnodes for the new mount point, if available. 486 */ 487 if ((vp->v_mount = mp) != NULL) 488 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes); 489 } 490 491 /* 492 * Create a vnode for a block device. 493 * Used for root filesystem, argdev, and swap areas. 494 * Also used for memory file system special devices. 495 */ 496 int 497 bdevvp(dev_t dev, struct vnode **vpp) 498 { 499 return (getdevvp(dev, vpp, VBLK)); 500 } 501 502 /* 503 * Create a vnode for a character device. 504 * Used for console handling. 505 */ 506 int 507 cdevvp(dev_t dev, struct vnode **vpp) 508 { 509 return (getdevvp(dev, vpp, VCHR)); 510 } 511 512 /* 513 * Create a vnode for a device. 514 * Used by bdevvp (block device) for root file system etc., 515 * and by cdevvp (character device) for console. 516 */ 517 int 518 getdevvp(dev_t dev, struct vnode **vpp, enum vtype type) 519 { 520 struct vnode *vp; 521 struct vnode *nvp; 522 int error; 523 524 if (dev == NODEV) { 525 *vpp = NULLVP; 526 return (0); 527 } 528 error = getnewvnode(VT_NON, NULL, &spec_vops, &nvp); 529 if (error) { 530 *vpp = NULLVP; 531 return (error); 532 } 533 vp = nvp; 534 vp->v_type = type; 535 if ((nvp = checkalias(vp, dev, NULL)) != NULL) { 536 vput(vp); 537 vp = nvp; 538 } 539 if (vp->v_type == VCHR && cdevsw[major(vp->v_rdev)].d_type == D_TTY) 540 vp->v_flag |= VISTTY; 541 *vpp = vp; 542 return (0); 543 } 544 545 /* 546 * Check to see if the new vnode represents a special device 547 * for which we already have a vnode (either because of 548 * bdevvp() or because of a different vnode representing 549 * the same block device). If such an alias exists, deallocate 550 * the existing contents and return the aliased vnode. The 551 * caller is responsible for filling it with its new contents. 552 */ 553 struct vnode * 554 checkalias(struct vnode *nvp, dev_t nvp_rdev, struct mount *mp) 555 { 556 struct proc *p = curproc; 557 struct vnode *vp; 558 struct vnodechain *vchain; 559 560 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 561 return (NULLVP); 562 563 vchain = &speclisth[SPECHASH(nvp_rdev)]; 564 loop: 565 SLIST_FOREACH(vp, vchain, v_specnext) { 566 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type) { 567 continue; 568 } 569 /* 570 * Alias, but not in use, so flush it out. 571 */ 572 if (vp->v_usecount == 0) { 573 vgonel(vp, p); 574 goto loop; 575 } 576 if (vget(vp, LK_EXCLUSIVE)) { 577 goto loop; 578 } 579 break; 580 } 581 582 /* 583 * Common case is actually in the if statement 584 */ 585 if (vp == NULL || !(vp->v_tag == VT_NON && vp->v_type == VBLK)) { 586 nvp->v_specinfo = malloc(sizeof(struct specinfo), M_VNODE, 587 M_WAITOK); 588 nvp->v_rdev = nvp_rdev; 589 nvp->v_hashchain = vchain; 590 nvp->v_specmountpoint = NULL; 591 nvp->v_speclockf = NULL; 592 nvp->v_specbitmap = NULL; 593 if (nvp->v_type == VCHR && 594 (cdevsw[major(nvp_rdev)].d_flags & D_CLONE) && 595 (minor(nvp_rdev) >> CLONE_SHIFT == 0)) { 596 if (vp != NULLVP) 597 nvp->v_specbitmap = vp->v_specbitmap; 598 else 599 nvp->v_specbitmap = malloc(CLONE_MAPSZ, 600 M_VNODE, M_WAITOK | M_ZERO); 601 } 602 SLIST_INSERT_HEAD(vchain, nvp, v_specnext); 603 if (vp != NULLVP) { 604 nvp->v_flag |= VALIASED; 605 vp->v_flag |= VALIASED; 606 vput(vp); 607 } 608 return (NULLVP); 609 } 610 611 /* 612 * This code is the uncommon case. It is called in case 613 * we found an alias that was VT_NON && vtype of VBLK 614 * This means we found a block device that was created 615 * using bdevvp. 616 * An example of such a vnode is the root partition device vnode 617 * created in ffs_mountroot. 618 * 619 * The vnodes created by bdevvp should not be aliased (why?). 620 */ 621 622 VOP_UNLOCK(vp); 623 vclean(vp, 0, p); 624 vp->v_op = nvp->v_op; 625 vp->v_tag = nvp->v_tag; 626 nvp->v_type = VNON; 627 insmntque(vp, mp); 628 return (vp); 629 } 630 631 /* 632 * Grab a particular vnode from the free list, increment its 633 * reference count and lock it. If the vnode lock bit is set, 634 * the vnode is being eliminated in vgone. In that case, we 635 * cannot grab it, so the process is awakened when the 636 * transition is completed, and an error code is returned to 637 * indicate that the vnode is no longer usable, possibly 638 * having been changed to a new file system type. 639 */ 640 int 641 vget(struct vnode *vp, int flags) 642 { 643 int error, s, onfreelist; 644 645 /* 646 * If the vnode is in the process of being cleaned out for 647 * another use, we wait for the cleaning to finish and then 648 * return failure. Cleaning is determined by checking that 649 * the VXLOCK flag is set. 650 */ 651 mtx_enter(&vnode_mtx); 652 if (vp->v_lflag & VXLOCK) { 653 if (flags & LK_NOWAIT) { 654 mtx_leave(&vnode_mtx); 655 return (EBUSY); 656 } 657 658 vp->v_lflag |= VXWANT; 659 msleep_nsec(vp, &vnode_mtx, PINOD, "vget", INFSLP); 660 mtx_leave(&vnode_mtx); 661 return (ENOENT); 662 } 663 mtx_leave(&vnode_mtx); 664 665 onfreelist = vp->v_bioflag & VBIOONFREELIST; 666 if (vp->v_usecount == 0 && onfreelist) { 667 s = splbio(); 668 if (vp->v_holdcnt > 0) 669 TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist); 670 else 671 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 672 vp->v_bioflag &= ~VBIOONFREELIST; 673 splx(s); 674 } 675 676 vp->v_usecount++; 677 if (flags & LK_TYPE_MASK) { 678 if ((error = vn_lock(vp, flags)) != 0) { 679 vp->v_usecount--; 680 if (vp->v_usecount == 0 && onfreelist) 681 vputonfreelist(vp); 682 } 683 return (error); 684 } 685 686 return (0); 687 } 688 689 690 /* Vnode reference. */ 691 void 692 vref(struct vnode *vp) 693 { 694 KERNEL_ASSERT_LOCKED(); 695 696 #ifdef DIAGNOSTIC 697 if (vp->v_usecount == 0) 698 panic("vref used where vget required"); 699 if (vp->v_type == VNON) 700 panic("vref on a VNON vnode"); 701 #endif 702 vp->v_usecount++; 703 } 704 705 void 706 vputonfreelist(struct vnode *vp) 707 { 708 int s; 709 struct freelst *lst; 710 711 s = splbio(); 712 #ifdef DIAGNOSTIC 713 if (vp->v_usecount != 0) 714 panic("Use count is not zero!"); 715 716 /* 717 * If the hold count is still positive, one or many threads could still 718 * be waiting on the vnode lock inside uvn_io(). 719 */ 720 if (vp->v_holdcnt == 0 && vp->v_lockcount != 0) 721 panic("%s: lock count is not zero", __func__); 722 723 if (vp->v_bioflag & VBIOONFREELIST) { 724 vprint("vnode already on free list: ", vp); 725 panic("vnode already on free list"); 726 } 727 #endif 728 729 vp->v_bioflag |= VBIOONFREELIST; 730 vp->v_bioflag &= ~VBIOERROR; 731 732 if (vp->v_holdcnt > 0) 733 lst = &vnode_hold_list; 734 else 735 lst = &vnode_free_list; 736 737 if (vp->v_type == VBAD) 738 TAILQ_INSERT_HEAD(lst, vp, v_freelist); 739 else 740 TAILQ_INSERT_TAIL(lst, vp, v_freelist); 741 742 splx(s); 743 } 744 745 /* 746 * vput(), just unlock and vrele() 747 */ 748 void 749 vput(struct vnode *vp) 750 { 751 struct proc *p = curproc; 752 753 #ifdef DIAGNOSTIC 754 if (vp == NULL) 755 panic("vput: null vp"); 756 #endif 757 758 #ifdef DIAGNOSTIC 759 if (vp->v_usecount == 0) { 760 vprint("vput: bad ref count", vp); 761 panic("vput: ref cnt"); 762 } 763 #endif 764 vp->v_usecount--; 765 KASSERT(vp->v_usecount > 0 || vp->v_uvcount == 0); 766 if (vp->v_usecount > 0) { 767 VOP_UNLOCK(vp); 768 return; 769 } 770 771 #ifdef DIAGNOSTIC 772 if (vp->v_writecount != 0) { 773 vprint("vput: bad writecount", vp); 774 panic("vput: v_writecount != 0"); 775 } 776 #endif 777 778 VOP_INACTIVE(vp, p); 779 780 if (vp->v_usecount == 0 && !(vp->v_bioflag & VBIOONFREELIST)) 781 vputonfreelist(vp); 782 } 783 784 /* 785 * Vnode release - use for active VNODES. 786 * If count drops to zero, call inactive routine and return to freelist. 787 * Returns 0 if it did not sleep. 788 */ 789 int 790 vrele(struct vnode *vp) 791 { 792 struct proc *p = curproc; 793 794 #ifdef DIAGNOSTIC 795 if (vp == NULL) 796 panic("vrele: null vp"); 797 #endif 798 #ifdef DIAGNOSTIC 799 if (vp->v_usecount == 0) { 800 vprint("vrele: bad ref count", vp); 801 panic("vrele: ref cnt"); 802 } 803 #endif 804 vp->v_usecount--; 805 if (vp->v_usecount > 0) { 806 return (0); 807 } 808 809 #ifdef DIAGNOSTIC 810 if (vp->v_writecount != 0) { 811 vprint("vrele: bad writecount", vp); 812 panic("vrele: v_writecount != 0"); 813 } 814 #endif 815 816 if (vn_lock(vp, LK_EXCLUSIVE)) { 817 #ifdef DIAGNOSTIC 818 vprint("vrele: cannot lock", vp); 819 #endif 820 return (1); 821 } 822 823 VOP_INACTIVE(vp, p); 824 825 if (vp->v_usecount == 0 && !(vp->v_bioflag & VBIOONFREELIST)) 826 vputonfreelist(vp); 827 return (1); 828 } 829 830 /* Page or buffer structure gets a reference. */ 831 void 832 vhold(struct vnode *vp) 833 { 834 /* 835 * If it is on the freelist and the hold count is currently 836 * zero, move it to the hold list. 837 */ 838 if ((vp->v_bioflag & VBIOONFREELIST) && 839 vp->v_holdcnt == 0 && vp->v_usecount == 0) { 840 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 841 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist); 842 } 843 vp->v_holdcnt++; 844 } 845 846 /* Lose interest in a vnode. */ 847 void 848 vdrop(struct vnode *vp) 849 { 850 #ifdef DIAGNOSTIC 851 if (vp->v_holdcnt == 0) 852 panic("vdrop: zero holdcnt"); 853 #endif 854 855 vp->v_holdcnt--; 856 857 /* 858 * If it is on the holdlist and the hold count drops to 859 * zero, move it to the free list. 860 */ 861 if ((vp->v_bioflag & VBIOONFREELIST) && 862 vp->v_holdcnt == 0 && vp->v_usecount == 0) { 863 TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist); 864 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 865 } 866 } 867 868 /* 869 * Remove any vnodes in the vnode table belonging to mount point mp. 870 * 871 * If MNT_NOFORCE is specified, there should not be any active ones, 872 * return error if any are found (nb: this is a user error, not a 873 * system error). If MNT_FORCE is specified, detach any active vnodes 874 * that are found. 875 */ 876 #ifdef DEBUG_SYSCTL 877 int busyprt = 0; /* print out busy vnodes */ 878 struct ctldebug debug_vfs_busyprt = { "vfs_busyprt", &busyprt }; 879 #endif 880 881 int 882 vfs_mount_foreach_vnode(struct mount *mp, 883 int (*func)(struct vnode *, void *), void *arg) { 884 struct vnode *vp, *nvp; 885 int error = 0; 886 887 loop: 888 TAILQ_FOREACH_SAFE(vp , &mp->mnt_vnodelist, v_mntvnodes, nvp) { 889 if (vp->v_mount != mp) 890 goto loop; 891 892 error = func(vp, arg); 893 894 if (error != 0) 895 break; 896 } 897 898 return (error); 899 } 900 901 struct vflush_args { 902 struct vnode *skipvp; 903 int busy; 904 int flags; 905 }; 906 907 int 908 vflush_vnode(struct vnode *vp, void *arg) 909 { 910 struct vflush_args *va = arg; 911 struct proc *p = curproc; 912 913 if (vp == va->skipvp) { 914 return (0); 915 } 916 917 if ((va->flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) { 918 return (0); 919 } 920 921 /* 922 * If WRITECLOSE is set, only flush out regular file 923 * vnodes open for writing. 924 */ 925 if ((va->flags & WRITECLOSE) && 926 (vp->v_writecount == 0 || vp->v_type != VREG)) { 927 return (0); 928 } 929 930 /* 931 * With v_usecount == 0, all we need to do is clear 932 * out the vnode data structures and we are done. 933 */ 934 if (vp->v_usecount == 0) { 935 vgonel(vp, p); 936 return (0); 937 } 938 939 /* 940 * If FORCECLOSE is set, forcibly close the vnode. 941 * For block or character devices, revert to an 942 * anonymous device. For all other files, just kill them. 943 */ 944 if (va->flags & FORCECLOSE) { 945 if (vp->v_type != VBLK && vp->v_type != VCHR) { 946 vgonel(vp, p); 947 } else { 948 vclean(vp, 0, p); 949 vp->v_op = &spec_vops; 950 insmntque(vp, NULL); 951 } 952 return (0); 953 } 954 955 /* 956 * If set, this is allowed to ignore vnodes which don't 957 * have changes pending to disk. 958 * XXX Might be nice to check per-fs "inode" flags, but 959 * generally the filesystem is sync'd already, right? 960 */ 961 if ((va->flags & IGNORECLEAN) && 962 LIST_EMPTY(&vp->v_dirtyblkhd)) 963 return (0); 964 965 #ifdef DEBUG_SYSCTL 966 if (busyprt) 967 vprint("vflush: busy vnode", vp); 968 #endif 969 va->busy++; 970 return (0); 971 } 972 973 int 974 vflush(struct mount *mp, struct vnode *skipvp, int flags) 975 { 976 struct vflush_args va; 977 va.skipvp = skipvp; 978 va.busy = 0; 979 va.flags = flags; 980 981 vfs_mount_foreach_vnode(mp, vflush_vnode, &va); 982 983 if (va.busy) 984 return (EBUSY); 985 return (0); 986 } 987 988 /* 989 * Disassociate the underlying file system from a vnode. 990 */ 991 void 992 vclean(struct vnode *vp, int flags, struct proc *p) 993 { 994 int active, do_wakeup = 0; 995 996 /* 997 * Check to see if the vnode is in use. 998 * If so we have to reference it before we clean it out 999 * so that its count cannot fall to zero and generate a 1000 * race against ourselves to recycle it. 1001 */ 1002 if ((active = vp->v_usecount) != 0) 1003 vp->v_usecount++; 1004 1005 /* 1006 * Prevent the vnode from being recycled or 1007 * brought into use while we clean it out. 1008 */ 1009 mtx_enter(&vnode_mtx); 1010 if (vp->v_lflag & VXLOCK) 1011 panic("vclean: deadlock"); 1012 vp->v_lflag |= VXLOCK; 1013 1014 if (vp->v_lockcount > 0) { 1015 /* 1016 * Ensure that any thread currently waiting on the same lock has 1017 * observed that the vnode is about to be exclusively locked 1018 * before continuing. 1019 */ 1020 msleep_nsec(&vp->v_lockcount, &vnode_mtx, PINOD, "vop_lock", 1021 INFSLP); 1022 KASSERT(vp->v_lockcount == 0); 1023 } 1024 mtx_leave(&vnode_mtx); 1025 1026 /* 1027 * Even if the count is zero, the VOP_INACTIVE routine may still 1028 * have the object locked while it cleans it out. The VOP_LOCK 1029 * ensures that the VOP_INACTIVE routine is done with its work. 1030 * For active vnodes, it ensures that no other activity can 1031 * occur while the underlying object is being cleaned out. 1032 */ 1033 VOP_LOCK(vp, LK_EXCLUSIVE | LK_DRAIN); 1034 1035 /* 1036 * Clean out any VM data associated with the vnode. 1037 */ 1038 uvm_vnp_terminate(vp); 1039 /* 1040 * Clean out any buffers associated with the vnode. 1041 */ 1042 if (flags & DOCLOSE) 1043 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, INFSLP); 1044 /* 1045 * If purging an active vnode, it must be closed and 1046 * deactivated before being reclaimed. Note that the 1047 * VOP_INACTIVE will unlock the vnode 1048 */ 1049 if (active) { 1050 if (flags & DOCLOSE) 1051 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p); 1052 VOP_INACTIVE(vp, p); 1053 } else { 1054 /* 1055 * Any other processes trying to obtain this lock must first 1056 * wait for VXLOCK to clear, then call the new lock operation. 1057 */ 1058 VOP_UNLOCK(vp); 1059 } 1060 1061 /* 1062 * Reclaim the vnode. 1063 */ 1064 if (VOP_RECLAIM(vp, p)) 1065 panic("vclean: cannot reclaim"); 1066 if (active) { 1067 vp->v_usecount--; 1068 if (vp->v_usecount == 0) { 1069 if (vp->v_holdcnt > 0) 1070 panic("vclean: not clean"); 1071 vputonfreelist(vp); 1072 } 1073 } 1074 cache_purge(vp); 1075 1076 /* 1077 * Done with purge, notify sleepers of the grim news. 1078 */ 1079 vp->v_op = &dead_vops; 1080 VN_KNOTE(vp, NOTE_REVOKE); 1081 vp->v_tag = VT_NON; 1082 #ifdef VFSLCKDEBUG 1083 vp->v_flag &= ~VLOCKSWORK; 1084 #endif 1085 mtx_enter(&vnode_mtx); 1086 vp->v_lflag &= ~VXLOCK; 1087 if (vp->v_lflag & VXWANT) { 1088 vp->v_lflag &= ~VXWANT; 1089 do_wakeup = 1; 1090 } 1091 mtx_leave(&vnode_mtx); 1092 if (do_wakeup) 1093 wakeup(vp); 1094 } 1095 1096 /* 1097 * Recycle an unused vnode to the front of the free list. 1098 */ 1099 int 1100 vrecycle(struct vnode *vp, struct proc *p) 1101 { 1102 if (vp->v_usecount == 0) { 1103 vgonel(vp, p); 1104 return (1); 1105 } 1106 return (0); 1107 } 1108 1109 /* 1110 * Eliminate all activity associated with a vnode 1111 * in preparation for reuse. 1112 */ 1113 void 1114 vgone(struct vnode *vp) 1115 { 1116 struct proc *p = curproc; 1117 vgonel(vp, p); 1118 } 1119 1120 /* 1121 * vgone, with struct proc. 1122 */ 1123 void 1124 vgonel(struct vnode *vp, struct proc *p) 1125 { 1126 struct vnode *vq; 1127 struct vnode *vx; 1128 1129 KASSERT(vp->v_uvcount == 0); 1130 1131 /* 1132 * If a vgone (or vclean) is already in progress, 1133 * wait until it is done and return. 1134 */ 1135 mtx_enter(&vnode_mtx); 1136 if (vp->v_lflag & VXLOCK) { 1137 vp->v_lflag |= VXWANT; 1138 msleep_nsec(vp, &vnode_mtx, PINOD, "vgone", INFSLP); 1139 mtx_leave(&vnode_mtx); 1140 return; 1141 } 1142 mtx_leave(&vnode_mtx); 1143 1144 /* 1145 * Clean out the filesystem specific data. 1146 */ 1147 vclean(vp, DOCLOSE, p); 1148 /* 1149 * Delete from old mount point vnode list, if on one. 1150 */ 1151 if (vp->v_mount != NULL) 1152 insmntque(vp, NULL); 1153 /* 1154 * If special device, remove it from special device alias list 1155 * if it is on one. 1156 */ 1157 if ((vp->v_type == VBLK || vp->v_type == VCHR) && 1158 vp->v_specinfo != NULL) { 1159 if ((vp->v_flag & VALIASED) == 0 && vp->v_type == VCHR && 1160 (cdevsw[major(vp->v_rdev)].d_flags & D_CLONE) && 1161 (minor(vp->v_rdev) >> CLONE_SHIFT == 0)) { 1162 free(vp->v_specbitmap, M_VNODE, CLONE_MAPSZ); 1163 } 1164 SLIST_REMOVE(vp->v_hashchain, vp, vnode, v_specnext); 1165 if (vp->v_flag & VALIASED) { 1166 vx = NULL; 1167 SLIST_FOREACH(vq, vp->v_hashchain, v_specnext) { 1168 if (vq->v_rdev != vp->v_rdev || 1169 vq->v_type != vp->v_type) 1170 continue; 1171 if (vx) 1172 break; 1173 vx = vq; 1174 } 1175 if (vx == NULL) 1176 panic("missing alias"); 1177 if (vq == NULL) 1178 vx->v_flag &= ~VALIASED; 1179 vp->v_flag &= ~VALIASED; 1180 } 1181 lf_purgelocks(&vp->v_speclockf); 1182 free(vp->v_specinfo, M_VNODE, sizeof(struct specinfo)); 1183 vp->v_specinfo = NULL; 1184 } 1185 /* 1186 * If it is on the freelist and not already at the head, 1187 * move it to the head of the list. 1188 */ 1189 vp->v_type = VBAD; 1190 1191 /* 1192 * Move onto the free list, unless we were called from 1193 * getnewvnode and we're not on any free list 1194 */ 1195 if (vp->v_usecount == 0 && 1196 (vp->v_bioflag & VBIOONFREELIST)) { 1197 int s; 1198 1199 s = splbio(); 1200 1201 if (vp->v_holdcnt > 0) 1202 panic("vgonel: not clean"); 1203 1204 if (TAILQ_FIRST(&vnode_free_list) != vp) { 1205 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1206 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 1207 } 1208 splx(s); 1209 } 1210 } 1211 1212 /* 1213 * Lookup a vnode by device number. 1214 */ 1215 int 1216 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp) 1217 { 1218 struct vnode *vp; 1219 int rc =0; 1220 1221 SLIST_FOREACH(vp, &speclisth[SPECHASH(dev)], v_specnext) { 1222 if (dev != vp->v_rdev || type != vp->v_type) 1223 continue; 1224 *vpp = vp; 1225 rc = 1; 1226 break; 1227 } 1228 return (rc); 1229 } 1230 1231 /* 1232 * Revoke all the vnodes corresponding to the specified minor number 1233 * range (endpoints inclusive) of the specified major. 1234 */ 1235 void 1236 vdevgone(int maj, int minl, int minh, enum vtype type) 1237 { 1238 struct vnode *vp; 1239 int mn; 1240 1241 for (mn = minl; mn <= minh; mn++) 1242 if (vfinddev(makedev(maj, mn), type, &vp)) 1243 VOP_REVOKE(vp, REVOKEALL); 1244 } 1245 1246 /* 1247 * Calculate the total number of references to a special device. 1248 */ 1249 int 1250 vcount(struct vnode *vp) 1251 { 1252 struct vnode *vq; 1253 int count; 1254 1255 loop: 1256 if ((vp->v_flag & VALIASED) == 0) 1257 return (vp->v_usecount); 1258 count = 0; 1259 SLIST_FOREACH(vq, vp->v_hashchain, v_specnext) { 1260 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 1261 continue; 1262 /* 1263 * Alias, but not in use, so flush it out. 1264 */ 1265 if (vq->v_usecount == 0 && vq != vp) { 1266 vgone(vq); 1267 goto loop; 1268 } 1269 count += vq->v_usecount; 1270 } 1271 return (count); 1272 } 1273 1274 #if defined(DEBUG) || defined(DIAGNOSTIC) 1275 /* 1276 * Print out a description of a vnode. 1277 */ 1278 static char *typename[] = 1279 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" }; 1280 1281 void 1282 vprint(char *label, struct vnode *vp) 1283 { 1284 char buf[64]; 1285 1286 if (label != NULL) 1287 printf("%s: ", label); 1288 printf("%p, type %s, use %u, write %u, hold %u,", 1289 vp, typename[vp->v_type], vp->v_usecount, vp->v_writecount, 1290 vp->v_holdcnt); 1291 buf[0] = '\0'; 1292 if (vp->v_flag & VROOT) 1293 strlcat(buf, "|VROOT", sizeof buf); 1294 if (vp->v_flag & VTEXT) 1295 strlcat(buf, "|VTEXT", sizeof buf); 1296 if (vp->v_flag & VSYSTEM) 1297 strlcat(buf, "|VSYSTEM", sizeof buf); 1298 if (vp->v_lflag & VXLOCK) 1299 strlcat(buf, "|VXLOCK", sizeof buf); 1300 if (vp->v_lflag & VXWANT) 1301 strlcat(buf, "|VXWANT", sizeof buf); 1302 if (vp->v_bioflag & VBIOWAIT) 1303 strlcat(buf, "|VBIOWAIT", sizeof buf); 1304 if (vp->v_bioflag & VBIOONFREELIST) 1305 strlcat(buf, "|VBIOONFREELIST", sizeof buf); 1306 if (vp->v_bioflag & VBIOONSYNCLIST) 1307 strlcat(buf, "|VBIOONSYNCLIST", sizeof buf); 1308 if (vp->v_flag & VALIASED) 1309 strlcat(buf, "|VALIASED", sizeof buf); 1310 if (buf[0] != '\0') 1311 printf(" flags (%s)", &buf[1]); 1312 if (vp->v_data == NULL) { 1313 printf("\n"); 1314 } else { 1315 printf("\n\t"); 1316 VOP_PRINT(vp); 1317 } 1318 } 1319 #endif /* DEBUG || DIAGNOSTIC */ 1320 1321 #ifdef DEBUG 1322 /* 1323 * List all of the locked vnodes in the system. 1324 * Called when debugging the kernel. 1325 */ 1326 void 1327 printlockedvnodes(void) 1328 { 1329 struct mount *mp; 1330 struct vnode *vp; 1331 1332 printf("Locked vnodes\n"); 1333 1334 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 1335 if (vfs_busy(mp, VB_READ|VB_NOWAIT)) 1336 continue; 1337 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 1338 if (VOP_ISLOCKED(vp)) 1339 vprint(NULL, vp); 1340 } 1341 vfs_unbusy(mp); 1342 } 1343 1344 } 1345 #endif 1346 1347 /* 1348 * Top level filesystem related information gathering. 1349 */ 1350 int 1351 vfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 1352 size_t newlen, struct proc *p) 1353 { 1354 struct vfsconf *vfsp, *tmpvfsp; 1355 int ret; 1356 1357 /* all sysctl names at this level are at least name and field */ 1358 if (namelen < 2) 1359 return (ENOTDIR); /* overloaded */ 1360 1361 if (name[0] != VFS_GENERIC) { 1362 vfsp = vfs_bytypenum(name[0]); 1363 if (vfsp == NULL || vfsp->vfc_vfsops->vfs_sysctl == NULL) 1364 return (EOPNOTSUPP); 1365 1366 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 1367 oldp, oldlenp, newp, newlen, p)); 1368 } 1369 1370 switch (name[1]) { 1371 case VFS_MAXTYPENUM: 1372 return (sysctl_rdint(oldp, oldlenp, newp, maxvfsconf)); 1373 1374 case VFS_CONF: 1375 if (namelen < 3) 1376 return (ENOTDIR); /* overloaded */ 1377 1378 vfsp = vfs_bytypenum(name[2]); 1379 if (vfsp == NULL) 1380 return (EOPNOTSUPP); 1381 1382 /* Make a copy, clear out kernel pointers */ 1383 tmpvfsp = malloc(sizeof(*tmpvfsp), M_TEMP, M_WAITOK|M_ZERO); 1384 memcpy(tmpvfsp, vfsp, sizeof(*tmpvfsp)); 1385 tmpvfsp->vfc_vfsops = NULL; 1386 1387 ret = sysctl_rdstruct(oldp, oldlenp, newp, tmpvfsp, 1388 sizeof(struct vfsconf)); 1389 1390 free(tmpvfsp, M_TEMP, sizeof(*tmpvfsp)); 1391 return (ret); 1392 case VFS_BCACHESTAT: /* buffer cache statistics */ 1393 ret = sysctl_rdstruct(oldp, oldlenp, newp, &bcstats, 1394 sizeof(struct bcachestats)); 1395 return(ret); 1396 } 1397 return (EOPNOTSUPP); 1398 } 1399 1400 /* 1401 * Check to see if a filesystem is mounted on a block device. 1402 */ 1403 int 1404 vfs_mountedon(struct vnode *vp) 1405 { 1406 struct vnode *vq; 1407 int error = 0; 1408 1409 if (vp->v_specmountpoint != NULL) 1410 return (EBUSY); 1411 if (vp->v_flag & VALIASED) { 1412 SLIST_FOREACH(vq, vp->v_hashchain, v_specnext) { 1413 if (vq->v_rdev != vp->v_rdev || 1414 vq->v_type != vp->v_type) 1415 continue; 1416 if (vq->v_specmountpoint != NULL) { 1417 error = EBUSY; 1418 break; 1419 } 1420 } 1421 } 1422 return (error); 1423 } 1424 1425 #ifdef NFSSERVER 1426 /* 1427 * Build hash lists of net addresses and hang them off the mount point. 1428 * Called by vfs_export() to set up the lists of export addresses. 1429 */ 1430 int 1431 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 1432 struct export_args *argp) 1433 { 1434 struct netcred *np; 1435 struct radix_node_head *rnh; 1436 int nplen, i; 1437 struct radix_node *rn; 1438 struct sockaddr *saddr, *smask = NULL; 1439 int error; 1440 1441 if (argp->ex_addrlen == 0) { 1442 if (mp->mnt_flag & MNT_DEFEXPORTED) 1443 return (EPERM); 1444 np = &nep->ne_defexported; 1445 /* fill in the kernel's ucred from userspace's xucred */ 1446 if ((error = crfromxucred(&np->netc_anon, &argp->ex_anon))) 1447 return (error); 1448 mp->mnt_flag |= MNT_DEFEXPORTED; 1449 goto finish; 1450 } 1451 if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN || 1452 argp->ex_addrlen < 0 || argp->ex_masklen < 0) 1453 return (EINVAL); 1454 nplen = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 1455 np = (struct netcred *)malloc(nplen, M_NETADDR, M_WAITOK|M_ZERO); 1456 np->netc_len = nplen; 1457 saddr = (struct sockaddr *)(np + 1); 1458 error = copyin(argp->ex_addr, saddr, argp->ex_addrlen); 1459 if (error) 1460 goto out; 1461 if (saddr->sa_len > argp->ex_addrlen) 1462 saddr->sa_len = argp->ex_addrlen; 1463 if (argp->ex_masklen) { 1464 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 1465 error = copyin(argp->ex_mask, smask, argp->ex_masklen); 1466 if (error) 1467 goto out; 1468 if (smask->sa_len > argp->ex_masklen) 1469 smask->sa_len = argp->ex_masklen; 1470 } 1471 /* fill in the kernel's ucred from userspace's xucred */ 1472 if ((error = crfromxucred(&np->netc_anon, &argp->ex_anon))) 1473 goto out; 1474 i = saddr->sa_family; 1475 switch (i) { 1476 case AF_INET: 1477 if ((rnh = nep->ne_rtable_inet) == NULL) { 1478 if (!rn_inithead((void **)&nep->ne_rtable_inet, 1479 offsetof(struct sockaddr_in, sin_addr))) { 1480 error = ENOBUFS; 1481 goto out; 1482 } 1483 rnh = nep->ne_rtable_inet; 1484 } 1485 break; 1486 default: 1487 error = EINVAL; 1488 goto out; 1489 } 1490 rn = rn_addroute(saddr, smask, rnh, np->netc_rnodes, 0); 1491 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ 1492 error = EPERM; 1493 goto out; 1494 } 1495 finish: 1496 np->netc_exflags = argp->ex_flags; 1497 return (0); 1498 out: 1499 free(np, M_NETADDR, np->netc_len); 1500 return (error); 1501 } 1502 1503 int 1504 vfs_free_netcred(struct radix_node *rn, void *w, u_int id) 1505 { 1506 struct radix_node_head *rnh = (struct radix_node_head *)w; 1507 struct netcred * np = (struct netcred *)rn; 1508 1509 rn_delete(rn->rn_key, rn->rn_mask, rnh, NULL); 1510 free(np, M_NETADDR, np->netc_len); 1511 return (0); 1512 } 1513 1514 /* 1515 * Free the net address hash lists that are hanging off the mount points. 1516 */ 1517 void 1518 vfs_free_addrlist(struct netexport *nep) 1519 { 1520 struct radix_node_head *rnh; 1521 1522 if ((rnh = nep->ne_rtable_inet) != NULL) { 1523 rn_walktree(rnh, vfs_free_netcred, rnh); 1524 free(rnh, M_RTABLE, sizeof(*rnh)); 1525 nep->ne_rtable_inet = NULL; 1526 } 1527 } 1528 #endif /* NFSSERVER */ 1529 1530 int 1531 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp) 1532 { 1533 #ifdef NFSSERVER 1534 int error; 1535 1536 if (argp->ex_flags & MNT_DELEXPORT) { 1537 vfs_free_addrlist(nep); 1538 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 1539 } 1540 if (argp->ex_flags & MNT_EXPORTED) { 1541 if ((error = vfs_hang_addrlist(mp, nep, argp)) != 0) 1542 return (error); 1543 mp->mnt_flag |= MNT_EXPORTED; 1544 } 1545 return (0); 1546 #else 1547 return (ENOTSUP); 1548 #endif /* NFSSERVER */ 1549 } 1550 1551 struct netcred * 1552 vfs_export_lookup(struct mount *mp, struct netexport *nep, struct mbuf *nam) 1553 { 1554 #ifdef NFSSERVER 1555 struct netcred *np; 1556 struct radix_node_head *rnh; 1557 struct sockaddr *saddr; 1558 1559 np = NULL; 1560 if (mp->mnt_flag & MNT_EXPORTED) { 1561 /* 1562 * Lookup in the export list first. 1563 */ 1564 if (nam != NULL) { 1565 saddr = mtod(nam, struct sockaddr *); 1566 switch(saddr->sa_family) { 1567 case AF_INET: 1568 rnh = nep->ne_rtable_inet; 1569 break; 1570 default: 1571 rnh = NULL; 1572 break; 1573 } 1574 if (rnh != NULL) 1575 np = (struct netcred *)rn_match(saddr, rnh); 1576 } 1577 /* 1578 * If no address match, use the default if it exists. 1579 */ 1580 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 1581 np = &nep->ne_defexported; 1582 } 1583 return (np); 1584 #else 1585 return (NULL); 1586 #endif /* NFSSERVER */ 1587 } 1588 1589 /* 1590 * Do the usual access checking. 1591 * file_mode, uid and gid are from the vnode in question, 1592 * while acc_mode and cred are from the VOP_ACCESS parameter list 1593 */ 1594 int 1595 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 1596 mode_t acc_mode, struct ucred *cred) 1597 { 1598 mode_t mask; 1599 1600 /* User id 0 always gets read/write access. */ 1601 if (cred->cr_uid == 0) { 1602 /* For VEXEC, at least one of the execute bits must be set. */ 1603 if ((acc_mode & VEXEC) && type != VDIR && 1604 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 1605 return EACCES; 1606 return 0; 1607 } 1608 1609 mask = 0; 1610 1611 /* Otherwise, check the owner. */ 1612 if (cred->cr_uid == uid) { 1613 if (acc_mode & VEXEC) 1614 mask |= S_IXUSR; 1615 if (acc_mode & VREAD) 1616 mask |= S_IRUSR; 1617 if (acc_mode & VWRITE) 1618 mask |= S_IWUSR; 1619 return (file_mode & mask) == mask ? 0 : EACCES; 1620 } 1621 1622 /* Otherwise, check the groups. */ 1623 if (groupmember(gid, cred)) { 1624 if (acc_mode & VEXEC) 1625 mask |= S_IXGRP; 1626 if (acc_mode & VREAD) 1627 mask |= S_IRGRP; 1628 if (acc_mode & VWRITE) 1629 mask |= S_IWGRP; 1630 return (file_mode & mask) == mask ? 0 : EACCES; 1631 } 1632 1633 /* Otherwise, check everyone else. */ 1634 if (acc_mode & VEXEC) 1635 mask |= S_IXOTH; 1636 if (acc_mode & VREAD) 1637 mask |= S_IROTH; 1638 if (acc_mode & VWRITE) 1639 mask |= S_IWOTH; 1640 return (file_mode & mask) == mask ? 0 : EACCES; 1641 } 1642 1643 int 1644 vnoperm(struct vnode *vp) 1645 { 1646 if (vp->v_flag & VROOT || vp->v_mount == NULL) 1647 return 0; 1648 1649 return (vp->v_mount->mnt_flag & MNT_NOPERM); 1650 } 1651 1652 struct rwlock vfs_stall_lock = RWLOCK_INITIALIZER("vfs_stall"); 1653 unsigned int vfs_stalling = 0; 1654 1655 int 1656 vfs_stall(struct proc *p, int stall) 1657 { 1658 struct mount *mp; 1659 int allerror = 0, error; 1660 1661 if (stall) { 1662 atomic_inc_int(&vfs_stalling); 1663 rw_enter_write(&vfs_stall_lock); 1664 } 1665 1666 /* 1667 * The loop variable mp is protected by vfs_busy() so that it cannot 1668 * be unmounted while VFS_SYNC() sleeps. Traverse forward to keep the 1669 * lock order consistent with dounmount(). 1670 */ 1671 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 1672 if (stall) { 1673 error = vfs_busy(mp, VB_WRITE|VB_WAIT|VB_DUPOK); 1674 if (error) { 1675 printf("%s: busy\n", mp->mnt_stat.f_mntonname); 1676 allerror = error; 1677 continue; 1678 } 1679 uvm_vnp_sync(mp); 1680 error = VFS_SYNC(mp, MNT_WAIT, stall, p->p_ucred, p); 1681 if (error) { 1682 printf("%s: failed to sync\n", 1683 mp->mnt_stat.f_mntonname); 1684 vfs_unbusy(mp); 1685 allerror = error; 1686 continue; 1687 } 1688 mp->mnt_flag |= MNT_STALLED; 1689 } else { 1690 if (mp->mnt_flag & MNT_STALLED) { 1691 vfs_unbusy(mp); 1692 mp->mnt_flag &= ~MNT_STALLED; 1693 } 1694 } 1695 } 1696 1697 if (!stall) { 1698 rw_exit_write(&vfs_stall_lock); 1699 atomic_dec_int(&vfs_stalling); 1700 } 1701 1702 return (allerror); 1703 } 1704 1705 void 1706 vfs_stall_barrier(void) 1707 { 1708 if (__predict_false(vfs_stalling)) { 1709 rw_enter_read(&vfs_stall_lock); 1710 rw_exit_read(&vfs_stall_lock); 1711 } 1712 } 1713 1714 /* 1715 * Unmount all file systems. 1716 * We traverse the list in reverse order under the assumption that doing so 1717 * will avoid needing to worry about dependencies. 1718 */ 1719 void 1720 vfs_unmountall(void) 1721 { 1722 struct mount *mp, *nmp; 1723 int allerror, error, again = 1; 1724 1725 retry: 1726 allerror = 0; 1727 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, nmp) { 1728 if (vfs_busy(mp, VB_WRITE|VB_NOWAIT)) 1729 continue; 1730 /* XXX Here is a race, the next pointer is not locked. */ 1731 if ((error = dounmount(mp, MNT_FORCE, curproc)) != 0) { 1732 printf("unmount of %s failed with error %d\n", 1733 mp->mnt_stat.f_mntonname, error); 1734 allerror = 1; 1735 } 1736 } 1737 1738 if (allerror) { 1739 printf("WARNING: some file systems would not unmount\n"); 1740 if (again) { 1741 printf("retrying\n"); 1742 again = 0; 1743 goto retry; 1744 } 1745 } 1746 } 1747 1748 /* 1749 * Sync and unmount file systems before shutting down. 1750 */ 1751 void 1752 vfs_shutdown(struct proc *p) 1753 { 1754 #ifdef ACCOUNTING 1755 acct_shutdown(); 1756 #endif 1757 1758 printf("syncing disks..."); 1759 1760 if (panicstr == NULL) { 1761 /* Sync before unmount, in case we hang on something. */ 1762 sys_sync(p, NULL, NULL); 1763 vfs_unmountall(); 1764 } 1765 1766 #if NSOFTRAID > 0 1767 sr_quiesce(); 1768 #endif 1769 1770 if (vfs_syncwait(p, 1)) 1771 printf(" giving up\n"); 1772 else 1773 printf(" done\n"); 1774 } 1775 1776 /* 1777 * perform sync() operation and wait for buffers to flush. 1778 */ 1779 int 1780 vfs_syncwait(struct proc *p, int verbose) 1781 { 1782 struct buf *bp; 1783 int iter, nbusy, dcount, s; 1784 #ifdef MULTIPROCESSOR 1785 int hold_count; 1786 #endif 1787 1788 sys_sync(p, NULL, NULL); 1789 1790 /* Wait for sync to finish. */ 1791 dcount = 10000; 1792 for (iter = 0; iter < 20; iter++) { 1793 nbusy = 0; 1794 LIST_FOREACH(bp, &bufhead, b_list) { 1795 if ((bp->b_flags & (B_BUSY|B_INVAL|B_READ)) == B_BUSY) 1796 nbusy++; 1797 /* 1798 * With soft updates, some buffers that are 1799 * written will be remarked as dirty until other 1800 * buffers are written. 1801 */ 1802 if (bp->b_flags & B_DELWRI) { 1803 s = splbio(); 1804 bremfree(bp); 1805 buf_acquire(bp); 1806 splx(s); 1807 nbusy++; 1808 bawrite(bp); 1809 if (dcount-- <= 0) { 1810 if (verbose) 1811 printf("softdep "); 1812 return 1; 1813 } 1814 } 1815 } 1816 if (nbusy == 0) 1817 break; 1818 if (verbose) 1819 printf("%d ", nbusy); 1820 #ifdef MULTIPROCESSOR 1821 if (_kernel_lock_held()) 1822 hold_count = __mp_release_all(&kernel_lock); 1823 else 1824 hold_count = 0; 1825 #endif 1826 DELAY(40000 * iter); 1827 #ifdef MULTIPROCESSOR 1828 if (hold_count) 1829 __mp_acquire_count(&kernel_lock, hold_count); 1830 #endif 1831 } 1832 1833 return nbusy; 1834 } 1835 1836 /* 1837 * posix file system related system variables. 1838 */ 1839 int 1840 fs_posix_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 1841 void *newp, size_t newlen, struct proc *p) 1842 { 1843 /* all sysctl names at this level are terminal */ 1844 if (namelen != 1) 1845 return (ENOTDIR); 1846 1847 switch (name[0]) { 1848 case FS_POSIX_SETUID: 1849 return (sysctl_securelevel_int(oldp, oldlenp, newp, newlen, 1850 &suid_clear)); 1851 default: 1852 return (EOPNOTSUPP); 1853 } 1854 /* NOTREACHED */ 1855 } 1856 1857 /* 1858 * file system related system variables. 1859 */ 1860 int 1861 fs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 1862 size_t newlen, struct proc *p) 1863 { 1864 sysctlfn *fn; 1865 1866 switch (name[0]) { 1867 case FS_POSIX: 1868 fn = fs_posix_sysctl; 1869 break; 1870 default: 1871 return (EOPNOTSUPP); 1872 } 1873 return (*fn)(name + 1, namelen - 1, oldp, oldlenp, newp, newlen, p); 1874 } 1875 1876 1877 /* 1878 * Routines dealing with vnodes and buffers 1879 */ 1880 1881 /* 1882 * Wait for all outstanding I/Os to complete 1883 * 1884 * Manipulates v_numoutput. Must be called at splbio() 1885 */ 1886 int 1887 vwaitforio(struct vnode *vp, int slpflag, char *wmesg, uint64_t timeo) 1888 { 1889 int error = 0; 1890 1891 splassert(IPL_BIO); 1892 1893 while (vp->v_numoutput) { 1894 vp->v_bioflag |= VBIOWAIT; 1895 error = tsleep_nsec(&vp->v_numoutput, 1896 slpflag | (PRIBIO + 1), wmesg, timeo); 1897 if (error) 1898 break; 1899 } 1900 1901 return (error); 1902 } 1903 1904 /* 1905 * Update outstanding I/O count and do wakeup if requested. 1906 * 1907 * Manipulates v_numoutput. Must be called at splbio() 1908 */ 1909 void 1910 vwakeup(struct vnode *vp) 1911 { 1912 splassert(IPL_BIO); 1913 1914 if (vp != NULL) { 1915 if (vp->v_numoutput-- == 0) 1916 panic("vwakeup: neg numoutput"); 1917 if ((vp->v_bioflag & VBIOWAIT) && vp->v_numoutput == 0) { 1918 vp->v_bioflag &= ~VBIOWAIT; 1919 wakeup(&vp->v_numoutput); 1920 } 1921 } 1922 } 1923 1924 /* 1925 * Flush out and invalidate all buffers associated with a vnode. 1926 * Called with the underlying object locked. 1927 */ 1928 int 1929 vinvalbuf(struct vnode *vp, int flags, struct ucred *cred, struct proc *p, 1930 int slpflag, uint64_t slptimeo) 1931 { 1932 struct buf *bp; 1933 struct buf *nbp, *blist; 1934 int s, error; 1935 1936 #ifdef VFSLCKDEBUG 1937 if ((vp->v_flag & VLOCKSWORK) && !VOP_ISLOCKED(vp)) 1938 panic("%s: vp isn't locked, vp %p", __func__, vp); 1939 #endif 1940 1941 if (flags & V_SAVE) { 1942 s = splbio(); 1943 vwaitforio(vp, 0, "vinvalbuf", INFSLP); 1944 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) { 1945 splx(s); 1946 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0) 1947 return (error); 1948 s = splbio(); 1949 if (vp->v_numoutput > 0 || 1950 !LIST_EMPTY(&vp->v_dirtyblkhd)) 1951 panic("%s: dirty bufs, vp %p", __func__, vp); 1952 } 1953 splx(s); 1954 } 1955 loop: 1956 s = splbio(); 1957 for (;;) { 1958 int count = 0; 1959 if ((blist = LIST_FIRST(&vp->v_cleanblkhd)) && 1960 (flags & V_SAVEMETA)) 1961 while (blist && blist->b_lblkno < 0) 1962 blist = LIST_NEXT(blist, b_vnbufs); 1963 if (blist == NULL && 1964 (blist = LIST_FIRST(&vp->v_dirtyblkhd)) && 1965 (flags & V_SAVEMETA)) 1966 while (blist && blist->b_lblkno < 0) 1967 blist = LIST_NEXT(blist, b_vnbufs); 1968 if (!blist) 1969 break; 1970 1971 for (bp = blist; bp; bp = nbp) { 1972 nbp = LIST_NEXT(bp, b_vnbufs); 1973 if (flags & V_SAVEMETA && bp->b_lblkno < 0) 1974 continue; 1975 if (bp->b_flags & B_BUSY) { 1976 bp->b_flags |= B_WANTED; 1977 error = tsleep_nsec(bp, slpflag | (PRIBIO + 1), 1978 "vinvalbuf", slptimeo); 1979 if (error) { 1980 splx(s); 1981 return (error); 1982 } 1983 break; 1984 } 1985 bremfree(bp); 1986 /* 1987 * XXX Since there are no node locks for NFS, I believe 1988 * there is a slight chance that a delayed write will 1989 * occur while sleeping just above, so check for it. 1990 */ 1991 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) { 1992 buf_acquire(bp); 1993 splx(s); 1994 (void) VOP_BWRITE(bp); 1995 goto loop; 1996 } 1997 buf_acquire_nomap(bp); 1998 bp->b_flags |= B_INVAL; 1999 brelse(bp); 2000 count++; 2001 /* 2002 * XXX Temporary workaround XXX 2003 * 2004 * If this is a gigantisch vnode and we are 2005 * trashing a ton of buffers, drop the lock 2006 * and yield every so often. The longer term 2007 * fix is to add a separate list for these 2008 * invalid buffers so we don't have to do the 2009 * work to free these here. 2010 */ 2011 if (count > 100) { 2012 splx(s); 2013 sched_pause(yield); 2014 goto loop; 2015 } 2016 } 2017 } 2018 if (!(flags & V_SAVEMETA) && 2019 (!LIST_EMPTY(&vp->v_dirtyblkhd) || !LIST_EMPTY(&vp->v_cleanblkhd))) 2020 panic("%s: flush failed, vp %p", __func__, vp); 2021 splx(s); 2022 return (0); 2023 } 2024 2025 void 2026 vflushbuf(struct vnode *vp, int sync) 2027 { 2028 struct buf *bp, *nbp; 2029 int s; 2030 2031 loop: 2032 s = splbio(); 2033 LIST_FOREACH_SAFE(bp, &vp->v_dirtyblkhd, b_vnbufs, nbp) { 2034 if ((bp->b_flags & B_BUSY)) 2035 continue; 2036 if ((bp->b_flags & B_DELWRI) == 0) 2037 panic("vflushbuf: not dirty"); 2038 bremfree(bp); 2039 buf_acquire(bp); 2040 splx(s); 2041 /* 2042 * Wait for I/O associated with indirect blocks to complete, 2043 * since there is no way to quickly wait for them below. 2044 */ 2045 if (bp->b_vp == vp || sync == 0) 2046 (void) bawrite(bp); 2047 else 2048 (void) bwrite(bp); 2049 goto loop; 2050 } 2051 if (sync == 0) { 2052 splx(s); 2053 return; 2054 } 2055 vwaitforio(vp, 0, "vflushbuf", INFSLP); 2056 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) { 2057 splx(s); 2058 #ifdef DIAGNOSTIC 2059 vprint("vflushbuf: dirty", vp); 2060 #endif 2061 goto loop; 2062 } 2063 splx(s); 2064 } 2065 2066 /* 2067 * Associate a buffer with a vnode. 2068 * 2069 * Manipulates buffer vnode queues. Must be called at splbio(). 2070 */ 2071 void 2072 bgetvp(struct vnode *vp, struct buf *bp) 2073 { 2074 splassert(IPL_BIO); 2075 2076 2077 if (bp->b_vp) 2078 panic("bgetvp: not free"); 2079 vhold(vp); 2080 bp->b_vp = vp; 2081 if (vp->v_type == VBLK || vp->v_type == VCHR) 2082 bp->b_dev = vp->v_rdev; 2083 else 2084 bp->b_dev = NODEV; 2085 /* 2086 * Insert onto list for new vnode. 2087 */ 2088 bufinsvn(bp, &vp->v_cleanblkhd); 2089 } 2090 2091 /* 2092 * Disassociate a buffer from a vnode. 2093 * 2094 * Manipulates vnode buffer queues. Must be called at splbio(). 2095 */ 2096 void 2097 brelvp(struct buf *bp) 2098 { 2099 struct vnode *vp; 2100 2101 splassert(IPL_BIO); 2102 2103 if ((vp = bp->b_vp) == (struct vnode *) 0) 2104 panic("brelvp: NULL"); 2105 /* 2106 * Delete from old vnode list, if on one. 2107 */ 2108 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 2109 bufremvn(bp); 2110 if ((vp->v_bioflag & VBIOONSYNCLIST) && 2111 LIST_EMPTY(&vp->v_dirtyblkhd)) { 2112 vp->v_bioflag &= ~VBIOONSYNCLIST; 2113 LIST_REMOVE(vp, v_synclist); 2114 } 2115 bp->b_vp = NULL; 2116 2117 vdrop(vp); 2118 } 2119 2120 /* 2121 * Replaces the current vnode associated with the buffer, if any, 2122 * with a new vnode. 2123 * 2124 * If an output I/O is pending on the buffer, the old vnode 2125 * I/O count is adjusted. 2126 * 2127 * Ignores vnode buffer queues. Must be called at splbio(). 2128 */ 2129 void 2130 buf_replacevnode(struct buf *bp, struct vnode *newvp) 2131 { 2132 struct vnode *oldvp = bp->b_vp; 2133 2134 splassert(IPL_BIO); 2135 2136 if (oldvp) 2137 brelvp(bp); 2138 2139 if ((bp->b_flags & (B_READ | B_DONE)) == 0) { 2140 newvp->v_numoutput++; /* put it on swapdev */ 2141 vwakeup(oldvp); 2142 } 2143 2144 bgetvp(newvp, bp); 2145 bufremvn(bp); 2146 } 2147 2148 /* 2149 * Used to assign buffers to the appropriate clean or dirty list on 2150 * the vnode and to add newly dirty vnodes to the appropriate 2151 * filesystem syncer list. 2152 * 2153 * Manipulates vnode buffer queues. Must be called at splbio(). 2154 */ 2155 void 2156 reassignbuf(struct buf *bp) 2157 { 2158 struct buflists *listheadp; 2159 int delay; 2160 struct vnode *vp = bp->b_vp; 2161 2162 splassert(IPL_BIO); 2163 2164 /* 2165 * Delete from old vnode list, if on one. 2166 */ 2167 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 2168 bufremvn(bp); 2169 2170 /* 2171 * If dirty, put on list of dirty buffers; 2172 * otherwise insert onto list of clean buffers. 2173 */ 2174 if ((bp->b_flags & B_DELWRI) == 0) { 2175 listheadp = &vp->v_cleanblkhd; 2176 if ((vp->v_bioflag & VBIOONSYNCLIST) && 2177 LIST_EMPTY(&vp->v_dirtyblkhd)) { 2178 vp->v_bioflag &= ~VBIOONSYNCLIST; 2179 LIST_REMOVE(vp, v_synclist); 2180 } 2181 } else { 2182 listheadp = &vp->v_dirtyblkhd; 2183 if ((vp->v_bioflag & VBIOONSYNCLIST) == 0) { 2184 switch (vp->v_type) { 2185 case VDIR: 2186 delay = syncdelay / 2; 2187 break; 2188 case VBLK: 2189 if (vp->v_specmountpoint != NULL) { 2190 delay = syncdelay / 3; 2191 break; 2192 } 2193 /* FALLTHROUGH */ 2194 default: 2195 delay = syncdelay; 2196 } 2197 vn_syncer_add_to_worklist(vp, delay); 2198 } 2199 } 2200 bufinsvn(bp, listheadp); 2201 } 2202 2203 /* 2204 * Check if vnode represents a disk device 2205 */ 2206 int 2207 vn_isdisk(struct vnode *vp, int *errp) 2208 { 2209 if (vp->v_type != VBLK && vp->v_type != VCHR) 2210 return (0); 2211 2212 return (1); 2213 } 2214 2215 #ifdef DDB 2216 #include <machine/db_machdep.h> 2217 #include <ddb/db_interface.h> 2218 2219 void 2220 vfs_buf_print(void *b, int full, 2221 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2222 { 2223 struct buf *bp = b; 2224 2225 (*pr)(" vp %p lblkno 0x%llx blkno 0x%llx dev 0x%x\n" 2226 " proc %p error %d flags %lb\n", 2227 bp->b_vp, (int64_t)bp->b_lblkno, (int64_t)bp->b_blkno, bp->b_dev, 2228 bp->b_proc, bp->b_error, bp->b_flags, B_BITS); 2229 2230 (*pr)(" bufsize 0x%lx bcount 0x%lx resid 0x%lx\n" 2231 " data %p saveaddr %p dep %p iodone %p\n", 2232 bp->b_bufsize, bp->b_bcount, (long)bp->b_resid, 2233 bp->b_data, bp->b_saveaddr, 2234 LIST_FIRST(&bp->b_dep), bp->b_iodone); 2235 2236 (*pr)(" dirty {off 0x%x end 0x%x} valid {off 0x%x end 0x%x}\n", 2237 bp->b_dirtyoff, bp->b_dirtyend, bp->b_validoff, bp->b_validend); 2238 2239 #ifdef FFS_SOFTUPDATES 2240 if (full) 2241 softdep_print(bp, full, pr); 2242 #endif 2243 } 2244 2245 const char *vtypes[] = { VTYPE_NAMES }; 2246 const char *vtags[] = { VTAG_NAMES }; 2247 2248 void 2249 vfs_vnode_print(void *v, int full, 2250 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2251 { 2252 struct vnode *vp = v; 2253 2254 (*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n", 2255 (u_int)vp->v_tag >= nitems(vtags)? "<unk>":vtags[vp->v_tag], 2256 vp->v_tag, 2257 (u_int)vp->v_type >= nitems(vtypes)? "<unk>":vtypes[vp->v_type], 2258 vp->v_type, vp->v_mount, vp->v_mountedhere); 2259 2260 (*pr)("data %p usecount %d writecount %d holdcnt %d numoutput %d\n", 2261 vp->v_data, vp->v_usecount, vp->v_writecount, 2262 vp->v_holdcnt, vp->v_numoutput); 2263 2264 /* uvm_object_printit(&vp->v_uobj, full, pr); */ 2265 2266 if (full) { 2267 struct buf *bp; 2268 2269 (*pr)("clean bufs:\n"); 2270 LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) { 2271 (*pr)(" bp %p\n", bp); 2272 vfs_buf_print(bp, full, pr); 2273 } 2274 2275 (*pr)("dirty bufs:\n"); 2276 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) { 2277 (*pr)(" bp %p\n", bp); 2278 vfs_buf_print(bp, full, pr); 2279 } 2280 } 2281 } 2282 2283 void 2284 vfs_mount_print(struct mount *mp, int full, 2285 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2286 { 2287 struct vfsconf *vfc = mp->mnt_vfc; 2288 struct vnode *vp; 2289 int cnt; 2290 2291 (*pr)("flags %b\nvnodecovered %p syncer %p data %p\n", 2292 mp->mnt_flag, MNT_BITS, 2293 mp->mnt_vnodecovered, mp->mnt_syncer, mp->mnt_data); 2294 2295 (*pr)("vfsconf: ops %p name \"%s\" num %d ref %u flags 0x%x\n", 2296 vfc->vfc_vfsops, vfc->vfc_name, vfc->vfc_typenum, 2297 vfc->vfc_refcount, vfc->vfc_flags); 2298 2299 (*pr)("statvfs cache: bsize %x iosize %x\n" 2300 "blocks %llu free %llu avail %lld\n", 2301 mp->mnt_stat.f_bsize, mp->mnt_stat.f_iosize, mp->mnt_stat.f_blocks, 2302 mp->mnt_stat.f_bfree, mp->mnt_stat.f_bavail); 2303 2304 (*pr)(" files %llu ffiles %llu favail %lld\n", mp->mnt_stat.f_files, 2305 mp->mnt_stat.f_ffree, mp->mnt_stat.f_favail); 2306 2307 (*pr)(" f_fsidx {0x%x, 0x%x} owner %u ctime 0x%llx\n", 2308 mp->mnt_stat.f_fsid.val[0], mp->mnt_stat.f_fsid.val[1], 2309 mp->mnt_stat.f_owner, mp->mnt_stat.f_ctime); 2310 2311 (*pr)(" syncwrites %llu asyncwrites = %llu\n", 2312 mp->mnt_stat.f_syncwrites, mp->mnt_stat.f_asyncwrites); 2313 2314 (*pr)(" syncreads %llu asyncreads = %llu\n", 2315 mp->mnt_stat.f_syncreads, mp->mnt_stat.f_asyncreads); 2316 2317 (*pr)(" fstype \"%s\" mnton \"%s\" mntfrom \"%s\" mntspec \"%s\"\n", 2318 mp->mnt_stat.f_fstypename, mp->mnt_stat.f_mntonname, 2319 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntfromspec); 2320 2321 (*pr)("locked vnodes:"); 2322 /* XXX would take mountlist lock, except ddb has no context */ 2323 cnt = 0; 2324 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2325 if (VOP_ISLOCKED(vp)) { 2326 if (cnt == 0) 2327 (*pr)("\n %p", vp); 2328 else if ((cnt % (72 / (sizeof(void *) * 2 + 4))) == 0) 2329 (*pr)(",\n %p", vp); 2330 else 2331 (*pr)(", %p", vp); 2332 cnt++; 2333 } 2334 } 2335 (*pr)("\n"); 2336 2337 if (full) { 2338 (*pr)("all vnodes:"); 2339 /* XXX would take mountlist lock, except ddb has no context */ 2340 cnt = 0; 2341 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2342 if (cnt == 0) 2343 (*pr)("\n %p", vp); 2344 else if ((cnt % (72 / (sizeof(void *) * 2 + 4))) == 0) 2345 (*pr)(",\n %p", vp); 2346 else 2347 (*pr)(", %p", vp); 2348 cnt++; 2349 } 2350 (*pr)("\n"); 2351 } 2352 } 2353 #endif /* DDB */ 2354 2355 void 2356 copy_statfs_info(struct statfs *sbp, const struct mount *mp) 2357 { 2358 const struct statfs *mbp; 2359 2360 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 2361 2362 if (sbp == (mbp = &mp->mnt_stat)) 2363 return; 2364 2365 sbp->f_fsid = mbp->f_fsid; 2366 sbp->f_owner = mbp->f_owner; 2367 sbp->f_flags = mbp->f_flags; 2368 sbp->f_syncwrites = mbp->f_syncwrites; 2369 sbp->f_asyncwrites = mbp->f_asyncwrites; 2370 sbp->f_syncreads = mbp->f_syncreads; 2371 sbp->f_asyncreads = mbp->f_asyncreads; 2372 sbp->f_namemax = mbp->f_namemax; 2373 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN); 2374 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN); 2375 memcpy(sbp->f_mntfromspec, mp->mnt_stat.f_mntfromspec, MNAMELEN); 2376 memcpy(&sbp->mount_info, &mp->mnt_stat.mount_info, 2377 sizeof(union mount_info)); 2378 } 2379