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