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