1 /* $OpenBSD: vfs_subr.c,v 1.300 2020/02/13 08:47:10 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 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 if (vp->v_lockcount != 0) 708 panic("%s: lock count is not zero", __func__); 709 710 if (vp->v_bioflag & VBIOONFREELIST) { 711 vprint("vnode already on free list: ", vp); 712 panic("vnode already on free list"); 713 } 714 #endif 715 716 vp->v_bioflag |= VBIOONFREELIST; 717 vp->v_bioflag &= ~VBIOERROR; 718 719 if (vp->v_holdcnt > 0) 720 lst = &vnode_hold_list; 721 else 722 lst = &vnode_free_list; 723 724 if (vp->v_type == VBAD) 725 TAILQ_INSERT_HEAD(lst, vp, v_freelist); 726 else 727 TAILQ_INSERT_TAIL(lst, vp, v_freelist); 728 729 splx(s); 730 } 731 732 /* 733 * vput(), just unlock and vrele() 734 */ 735 void 736 vput(struct vnode *vp) 737 { 738 struct proc *p = curproc; 739 740 #ifdef DIAGNOSTIC 741 if (vp == NULL) 742 panic("vput: null vp"); 743 #endif 744 745 #ifdef DIAGNOSTIC 746 if (vp->v_usecount == 0) { 747 vprint("vput: bad ref count", vp); 748 panic("vput: ref cnt"); 749 } 750 #endif 751 vp->v_usecount--; 752 KASSERT(vp->v_usecount > 0 || vp->v_uvcount == 0); 753 if (vp->v_usecount > 0) { 754 VOP_UNLOCK(vp); 755 return; 756 } 757 758 #ifdef DIAGNOSTIC 759 if (vp->v_writecount != 0) { 760 vprint("vput: bad writecount", vp); 761 panic("vput: v_writecount != 0"); 762 } 763 #endif 764 765 VOP_INACTIVE(vp, p); 766 767 if (vp->v_usecount == 0 && !(vp->v_bioflag & VBIOONFREELIST)) 768 vputonfreelist(vp); 769 } 770 771 /* 772 * Vnode release - use for active VNODES. 773 * If count drops to zero, call inactive routine and return to freelist. 774 * Returns 0 if it did not sleep. 775 */ 776 int 777 vrele(struct vnode *vp) 778 { 779 struct proc *p = curproc; 780 781 #ifdef DIAGNOSTIC 782 if (vp == NULL) 783 panic("vrele: null vp"); 784 #endif 785 #ifdef DIAGNOSTIC 786 if (vp->v_usecount == 0) { 787 vprint("vrele: bad ref count", vp); 788 panic("vrele: ref cnt"); 789 } 790 #endif 791 vp->v_usecount--; 792 if (vp->v_usecount > 0) { 793 return (0); 794 } 795 796 #ifdef DIAGNOSTIC 797 if (vp->v_writecount != 0) { 798 vprint("vrele: bad writecount", vp); 799 panic("vrele: v_writecount != 0"); 800 } 801 #endif 802 803 if (vn_lock(vp, LK_EXCLUSIVE)) { 804 #ifdef DIAGNOSTIC 805 vprint("vrele: cannot lock", vp); 806 #endif 807 return (1); 808 } 809 810 VOP_INACTIVE(vp, p); 811 812 if (vp->v_usecount == 0 && !(vp->v_bioflag & VBIOONFREELIST)) 813 vputonfreelist(vp); 814 return (1); 815 } 816 817 /* Page or buffer structure gets a reference. */ 818 void 819 vhold(struct vnode *vp) 820 { 821 /* 822 * If it is on the freelist and the hold count is currently 823 * zero, move it to the hold list. 824 */ 825 if ((vp->v_bioflag & VBIOONFREELIST) && 826 vp->v_holdcnt == 0 && vp->v_usecount == 0) { 827 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 828 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist); 829 } 830 vp->v_holdcnt++; 831 } 832 833 /* Lose interest in a vnode. */ 834 void 835 vdrop(struct vnode *vp) 836 { 837 #ifdef DIAGNOSTIC 838 if (vp->v_holdcnt == 0) 839 panic("vdrop: zero holdcnt"); 840 #endif 841 842 vp->v_holdcnt--; 843 844 /* 845 * If it is on the holdlist and the hold count drops to 846 * zero, move it to the free list. 847 */ 848 if ((vp->v_bioflag & VBIOONFREELIST) && 849 vp->v_holdcnt == 0 && vp->v_usecount == 0) { 850 TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist); 851 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 852 } 853 } 854 855 /* 856 * Remove any vnodes in the vnode table belonging to mount point mp. 857 * 858 * If MNT_NOFORCE is specified, there should not be any active ones, 859 * return error if any are found (nb: this is a user error, not a 860 * system error). If MNT_FORCE is specified, detach any active vnodes 861 * that are found. 862 */ 863 #ifdef DEBUG 864 int busyprt = 0; /* print out busy vnodes */ 865 struct ctldebug debug1 = { "busyprt", &busyprt }; 866 #endif 867 868 int 869 vfs_mount_foreach_vnode(struct mount *mp, 870 int (*func)(struct vnode *, void *), void *arg) { 871 struct vnode *vp, *nvp; 872 int error = 0; 873 874 loop: 875 TAILQ_FOREACH_SAFE(vp , &mp->mnt_vnodelist, v_mntvnodes, nvp) { 876 if (vp->v_mount != mp) 877 goto loop; 878 879 error = func(vp, arg); 880 881 if (error != 0) 882 break; 883 } 884 885 return (error); 886 } 887 888 struct vflush_args { 889 struct vnode *skipvp; 890 int busy; 891 int flags; 892 }; 893 894 int 895 vflush_vnode(struct vnode *vp, void *arg) 896 { 897 struct vflush_args *va = arg; 898 struct proc *p = curproc; 899 900 if (vp == va->skipvp) { 901 return (0); 902 } 903 904 if ((va->flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) { 905 return (0); 906 } 907 908 /* 909 * If WRITECLOSE is set, only flush out regular file 910 * vnodes open for writing. 911 */ 912 if ((va->flags & WRITECLOSE) && 913 (vp->v_writecount == 0 || vp->v_type != VREG)) { 914 return (0); 915 } 916 917 /* 918 * With v_usecount == 0, all we need to do is clear 919 * out the vnode data structures and we are done. 920 */ 921 if (vp->v_usecount == 0) { 922 vgonel(vp, p); 923 return (0); 924 } 925 926 /* 927 * If FORCECLOSE is set, forcibly close the vnode. 928 * For block or character devices, revert to an 929 * anonymous device. For all other files, just kill them. 930 */ 931 if (va->flags & FORCECLOSE) { 932 if (vp->v_type != VBLK && vp->v_type != VCHR) { 933 vgonel(vp, p); 934 } else { 935 vclean(vp, 0, p); 936 vp->v_op = &spec_vops; 937 insmntque(vp, NULL); 938 } 939 return (0); 940 } 941 942 /* 943 * If set, this is allowed to ignore vnodes which don't 944 * have changes pending to disk. 945 * XXX Might be nice to check per-fs "inode" flags, but 946 * generally the filesystem is sync'd already, right? 947 */ 948 if ((va->flags & IGNORECLEAN) && 949 LIST_EMPTY(&vp->v_dirtyblkhd)) 950 return (0); 951 952 #ifdef DEBUG 953 if (busyprt) 954 vprint("vflush: busy vnode", vp); 955 #endif 956 va->busy++; 957 return (0); 958 } 959 960 int 961 vflush(struct mount *mp, struct vnode *skipvp, int flags) 962 { 963 struct vflush_args va; 964 va.skipvp = skipvp; 965 va.busy = 0; 966 va.flags = flags; 967 968 vfs_mount_foreach_vnode(mp, vflush_vnode, &va); 969 970 if (va.busy) 971 return (EBUSY); 972 return (0); 973 } 974 975 /* 976 * Disassociate the underlying file system from a vnode. 977 */ 978 void 979 vclean(struct vnode *vp, int flags, struct proc *p) 980 { 981 int active; 982 983 /* 984 * Check to see if the vnode is in use. 985 * If so we have to reference it before we clean it out 986 * so that its count cannot fall to zero and generate a 987 * race against ourselves to recycle it. 988 */ 989 if ((active = vp->v_usecount) != 0) 990 vp->v_usecount++; 991 992 /* 993 * Prevent the vnode from being recycled or 994 * brought into use while we clean it out. 995 */ 996 if (vp->v_flag & VXLOCK) 997 panic("vclean: deadlock"); 998 vp->v_flag |= VXLOCK; 999 1000 if (vp->v_lockcount > 0) { 1001 /* 1002 * Ensure that any thread currently waiting on the same lock has 1003 * observed that the vnode is about to be exclusively locked 1004 * before continuing. 1005 */ 1006 tsleep_nsec(&vp->v_lockcount, PINOD, "vop_lock", INFSLP); 1007 KASSERT(vp->v_lockcount == 0); 1008 } 1009 1010 /* 1011 * Even if the count is zero, the VOP_INACTIVE routine may still 1012 * have the object locked while it cleans it out. The VOP_LOCK 1013 * ensures that the VOP_INACTIVE routine is done with its work. 1014 * For active vnodes, it ensures that no other activity can 1015 * occur while the underlying object is being cleaned out. 1016 */ 1017 VOP_LOCK(vp, LK_DRAIN | LK_EXCLUSIVE); 1018 1019 /* 1020 * Clean out any VM data associated with the vnode. 1021 */ 1022 uvm_vnp_terminate(vp); 1023 /* 1024 * Clean out any buffers associated with the vnode. 1025 */ 1026 if (flags & DOCLOSE) 1027 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, INFSLP); 1028 /* 1029 * If purging an active vnode, it must be closed and 1030 * deactivated before being reclaimed. Note that the 1031 * VOP_INACTIVE will unlock the vnode 1032 */ 1033 if (active) { 1034 if (flags & DOCLOSE) 1035 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p); 1036 VOP_INACTIVE(vp, p); 1037 } else { 1038 /* 1039 * Any other processes trying to obtain this lock must first 1040 * wait for VXLOCK to clear, then call the new lock operation. 1041 */ 1042 VOP_UNLOCK(vp); 1043 } 1044 1045 /* 1046 * Reclaim the vnode. 1047 */ 1048 if (VOP_RECLAIM(vp, p)) 1049 panic("vclean: cannot reclaim"); 1050 if (active) { 1051 vp->v_usecount--; 1052 if (vp->v_usecount == 0) { 1053 if (vp->v_holdcnt > 0) 1054 panic("vclean: not clean"); 1055 vputonfreelist(vp); 1056 } 1057 } 1058 cache_purge(vp); 1059 1060 /* 1061 * Done with purge, notify sleepers of the grim news. 1062 */ 1063 vp->v_op = &dead_vops; 1064 VN_KNOTE(vp, NOTE_REVOKE); 1065 vp->v_tag = VT_NON; 1066 vp->v_flag &= ~VXLOCK; 1067 #ifdef VFSLCKDEBUG 1068 vp->v_flag &= ~VLOCKSWORK; 1069 #endif 1070 if (vp->v_flag & VXWANT) { 1071 vp->v_flag &= ~VXWANT; 1072 wakeup(vp); 1073 } 1074 } 1075 1076 /* 1077 * Recycle an unused vnode to the front of the free list. 1078 */ 1079 int 1080 vrecycle(struct vnode *vp, struct proc *p) 1081 { 1082 if (vp->v_usecount == 0) { 1083 vgonel(vp, p); 1084 return (1); 1085 } 1086 return (0); 1087 } 1088 1089 /* 1090 * Eliminate all activity associated with a vnode 1091 * in preparation for reuse. 1092 */ 1093 void 1094 vgone(struct vnode *vp) 1095 { 1096 struct proc *p = curproc; 1097 vgonel(vp, p); 1098 } 1099 1100 /* 1101 * vgone, with struct proc. 1102 */ 1103 void 1104 vgonel(struct vnode *vp, struct proc *p) 1105 { 1106 struct vnode *vq; 1107 struct vnode *vx; 1108 1109 KASSERT(vp->v_uvcount == 0); 1110 1111 /* 1112 * If a vgone (or vclean) is already in progress, 1113 * wait until it is done and return. 1114 */ 1115 if (vp->v_flag & VXLOCK) { 1116 vp->v_flag |= VXWANT; 1117 tsleep_nsec(vp, PINOD, "vgone", INFSLP); 1118 return; 1119 } 1120 1121 /* 1122 * Clean out the filesystem specific data. 1123 */ 1124 vclean(vp, DOCLOSE, p); 1125 /* 1126 * Delete from old mount point vnode list, if on one. 1127 */ 1128 if (vp->v_mount != NULL) 1129 insmntque(vp, NULL); 1130 /* 1131 * If special device, remove it from special device alias list 1132 * if it is on one. 1133 */ 1134 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) { 1135 if ((vp->v_flag & VALIASED) == 0 && vp->v_type == VCHR && 1136 (cdevsw[major(vp->v_rdev)].d_flags & D_CLONE) && 1137 (minor(vp->v_rdev) >> CLONE_SHIFT == 0)) { 1138 free(vp->v_specbitmap, M_VNODE, CLONE_MAPSZ); 1139 } 1140 SLIST_REMOVE(vp->v_hashchain, vp, vnode, v_specnext); 1141 if (vp->v_flag & VALIASED) { 1142 vx = NULL; 1143 SLIST_FOREACH(vq, vp->v_hashchain, v_specnext) { 1144 if (vq->v_rdev != vp->v_rdev || 1145 vq->v_type != vp->v_type) 1146 continue; 1147 if (vx) 1148 break; 1149 vx = vq; 1150 } 1151 if (vx == NULL) 1152 panic("missing alias"); 1153 if (vq == NULL) 1154 vx->v_flag &= ~VALIASED; 1155 vp->v_flag &= ~VALIASED; 1156 } 1157 lf_purgelocks(&vp->v_speclockf); 1158 free(vp->v_specinfo, M_VNODE, sizeof(struct specinfo)); 1159 vp->v_specinfo = NULL; 1160 } 1161 /* 1162 * If it is on the freelist and not already at the head, 1163 * move it to the head of the list. 1164 */ 1165 vp->v_type = VBAD; 1166 1167 /* 1168 * Move onto the free list, unless we were called from 1169 * getnewvnode and we're not on any free list 1170 */ 1171 if (vp->v_usecount == 0 && 1172 (vp->v_bioflag & VBIOONFREELIST)) { 1173 int s; 1174 1175 s = splbio(); 1176 1177 if (vp->v_holdcnt > 0) 1178 panic("vgonel: not clean"); 1179 1180 if (TAILQ_FIRST(&vnode_free_list) != vp) { 1181 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1182 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 1183 } 1184 splx(s); 1185 } 1186 } 1187 1188 /* 1189 * Lookup a vnode by device number. 1190 */ 1191 int 1192 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp) 1193 { 1194 struct vnode *vp; 1195 int rc =0; 1196 1197 SLIST_FOREACH(vp, &speclisth[SPECHASH(dev)], v_specnext) { 1198 if (dev != vp->v_rdev || type != vp->v_type) 1199 continue; 1200 *vpp = vp; 1201 rc = 1; 1202 break; 1203 } 1204 return (rc); 1205 } 1206 1207 /* 1208 * Revoke all the vnodes corresponding to the specified minor number 1209 * range (endpoints inclusive) of the specified major. 1210 */ 1211 void 1212 vdevgone(int maj, int minl, int minh, enum vtype type) 1213 { 1214 struct vnode *vp; 1215 int mn; 1216 1217 for (mn = minl; mn <= minh; mn++) 1218 if (vfinddev(makedev(maj, mn), type, &vp)) 1219 VOP_REVOKE(vp, REVOKEALL); 1220 } 1221 1222 /* 1223 * Calculate the total number of references to a special device. 1224 */ 1225 int 1226 vcount(struct vnode *vp) 1227 { 1228 struct vnode *vq; 1229 int count; 1230 1231 loop: 1232 if ((vp->v_flag & VALIASED) == 0) 1233 return (vp->v_usecount); 1234 count = 0; 1235 SLIST_FOREACH(vq, vp->v_hashchain, v_specnext) { 1236 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 1237 continue; 1238 /* 1239 * Alias, but not in use, so flush it out. 1240 */ 1241 if (vq->v_usecount == 0 && vq != vp) { 1242 vgone(vq); 1243 goto loop; 1244 } 1245 count += vq->v_usecount; 1246 } 1247 return (count); 1248 } 1249 1250 #if defined(DEBUG) || defined(DIAGNOSTIC) 1251 /* 1252 * Print out a description of a vnode. 1253 */ 1254 static char *typename[] = 1255 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" }; 1256 1257 void 1258 vprint(char *label, struct vnode *vp) 1259 { 1260 char buf[64]; 1261 1262 if (label != NULL) 1263 printf("%s: ", label); 1264 printf("%p, type %s, use %u, write %u, hold %u,", 1265 vp, typename[vp->v_type], vp->v_usecount, vp->v_writecount, 1266 vp->v_holdcnt); 1267 buf[0] = '\0'; 1268 if (vp->v_flag & VROOT) 1269 strlcat(buf, "|VROOT", sizeof buf); 1270 if (vp->v_flag & VTEXT) 1271 strlcat(buf, "|VTEXT", sizeof buf); 1272 if (vp->v_flag & VSYSTEM) 1273 strlcat(buf, "|VSYSTEM", sizeof buf); 1274 if (vp->v_flag & VXLOCK) 1275 strlcat(buf, "|VXLOCK", sizeof buf); 1276 if (vp->v_flag & VXWANT) 1277 strlcat(buf, "|VXWANT", sizeof buf); 1278 if (vp->v_bioflag & VBIOWAIT) 1279 strlcat(buf, "|VBIOWAIT", sizeof buf); 1280 if (vp->v_bioflag & VBIOONFREELIST) 1281 strlcat(buf, "|VBIOONFREELIST", sizeof buf); 1282 if (vp->v_bioflag & VBIOONSYNCLIST) 1283 strlcat(buf, "|VBIOONSYNCLIST", sizeof buf); 1284 if (vp->v_flag & VALIASED) 1285 strlcat(buf, "|VALIASED", sizeof buf); 1286 if (buf[0] != '\0') 1287 printf(" flags (%s)", &buf[1]); 1288 if (vp->v_data == NULL) { 1289 printf("\n"); 1290 } else { 1291 printf("\n\t"); 1292 VOP_PRINT(vp); 1293 } 1294 } 1295 #endif /* DEBUG || DIAGNOSTIC */ 1296 1297 #ifdef DEBUG 1298 /* 1299 * List all of the locked vnodes in the system. 1300 * Called when debugging the kernel. 1301 */ 1302 void 1303 printlockedvnodes(void) 1304 { 1305 struct mount *mp; 1306 struct vnode *vp; 1307 1308 printf("Locked vnodes\n"); 1309 1310 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 1311 if (vfs_busy(mp, VB_READ|VB_NOWAIT)) 1312 continue; 1313 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 1314 if (VOP_ISLOCKED(vp)) 1315 vprint(NULL, vp); 1316 } 1317 vfs_unbusy(mp); 1318 } 1319 1320 } 1321 #endif 1322 1323 /* 1324 * Top level filesystem related information gathering. 1325 */ 1326 int 1327 vfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 1328 size_t newlen, struct proc *p) 1329 { 1330 struct vfsconf *vfsp, *tmpvfsp; 1331 int ret; 1332 1333 /* all sysctl names at this level are at least name and field */ 1334 if (namelen < 2) 1335 return (ENOTDIR); /* overloaded */ 1336 1337 if (name[0] != VFS_GENERIC) { 1338 vfsp = vfs_bytypenum(name[0]); 1339 if (vfsp == NULL || vfsp->vfc_vfsops->vfs_sysctl == NULL) 1340 return (EOPNOTSUPP); 1341 1342 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 1343 oldp, oldlenp, newp, newlen, p)); 1344 } 1345 1346 switch (name[1]) { 1347 case VFS_MAXTYPENUM: 1348 return (sysctl_rdint(oldp, oldlenp, newp, maxvfsconf)); 1349 1350 case VFS_CONF: 1351 if (namelen < 3) 1352 return (ENOTDIR); /* overloaded */ 1353 1354 vfsp = vfs_bytypenum(name[2]); 1355 if (vfsp == NULL) 1356 return (EOPNOTSUPP); 1357 1358 /* Make a copy, clear out kernel pointers */ 1359 tmpvfsp = malloc(sizeof(*tmpvfsp), M_TEMP, M_WAITOK|M_ZERO); 1360 memcpy(tmpvfsp, vfsp, sizeof(*tmpvfsp)); 1361 tmpvfsp->vfc_vfsops = NULL; 1362 1363 ret = sysctl_rdstruct(oldp, oldlenp, newp, tmpvfsp, 1364 sizeof(struct vfsconf)); 1365 1366 free(tmpvfsp, M_TEMP, sizeof(*tmpvfsp)); 1367 return (ret); 1368 case VFS_BCACHESTAT: /* buffer cache statistics */ 1369 ret = sysctl_rdstruct(oldp, oldlenp, newp, &bcstats, 1370 sizeof(struct bcachestats)); 1371 return(ret); 1372 } 1373 return (EOPNOTSUPP); 1374 } 1375 1376 /* 1377 * Check to see if a filesystem is mounted on a block device. 1378 */ 1379 int 1380 vfs_mountedon(struct vnode *vp) 1381 { 1382 struct vnode *vq; 1383 int error = 0; 1384 1385 if (vp->v_specmountpoint != NULL) 1386 return (EBUSY); 1387 if (vp->v_flag & VALIASED) { 1388 SLIST_FOREACH(vq, vp->v_hashchain, v_specnext) { 1389 if (vq->v_rdev != vp->v_rdev || 1390 vq->v_type != vp->v_type) 1391 continue; 1392 if (vq->v_specmountpoint != NULL) { 1393 error = EBUSY; 1394 break; 1395 } 1396 } 1397 } 1398 return (error); 1399 } 1400 1401 #ifdef NFSSERVER 1402 /* 1403 * Build hash lists of net addresses and hang them off the mount point. 1404 * Called by vfs_export() to set up the lists of export addresses. 1405 */ 1406 int 1407 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 1408 struct export_args *argp) 1409 { 1410 struct netcred *np; 1411 struct radix_node_head *rnh; 1412 int nplen, i; 1413 struct radix_node *rn; 1414 struct sockaddr *saddr, *smask = 0; 1415 int error; 1416 1417 if (argp->ex_addrlen == 0) { 1418 if (mp->mnt_flag & MNT_DEFEXPORTED) 1419 return (EPERM); 1420 np = &nep->ne_defexported; 1421 /* fill in the kernel's ucred from userspace's xucred */ 1422 if ((error = crfromxucred(&np->netc_anon, &argp->ex_anon))) 1423 return (error); 1424 mp->mnt_flag |= MNT_DEFEXPORTED; 1425 goto finish; 1426 } 1427 if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN || 1428 argp->ex_addrlen < 0 || argp->ex_masklen < 0) 1429 return (EINVAL); 1430 nplen = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 1431 np = (struct netcred *)malloc(nplen, M_NETADDR, M_WAITOK|M_ZERO); 1432 np->netc_len = nplen; 1433 saddr = (struct sockaddr *)(np + 1); 1434 error = copyin(argp->ex_addr, saddr, argp->ex_addrlen); 1435 if (error) 1436 goto out; 1437 if (saddr->sa_len > argp->ex_addrlen) 1438 saddr->sa_len = argp->ex_addrlen; 1439 if (argp->ex_masklen) { 1440 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 1441 error = copyin(argp->ex_mask, smask, argp->ex_masklen); 1442 if (error) 1443 goto out; 1444 if (smask->sa_len > argp->ex_masklen) 1445 smask->sa_len = argp->ex_masklen; 1446 } 1447 /* fill in the kernel's ucred from userspace's xucred */ 1448 if ((error = crfromxucred(&np->netc_anon, &argp->ex_anon))) 1449 goto out; 1450 i = saddr->sa_family; 1451 switch (i) { 1452 case AF_INET: 1453 if ((rnh = nep->ne_rtable_inet) == NULL) { 1454 if (!rn_inithead((void **)&nep->ne_rtable_inet, 1455 offsetof(struct sockaddr_in, sin_addr))) { 1456 error = ENOBUFS; 1457 goto out; 1458 } 1459 rnh = nep->ne_rtable_inet; 1460 } 1461 break; 1462 default: 1463 error = EINVAL; 1464 goto out; 1465 } 1466 rn = rn_addroute(saddr, smask, rnh, np->netc_rnodes, 0); 1467 if (rn == 0 || np != (struct netcred *)rn) { /* already exists */ 1468 error = EPERM; 1469 goto out; 1470 } 1471 finish: 1472 np->netc_exflags = argp->ex_flags; 1473 return (0); 1474 out: 1475 free(np, M_NETADDR, np->netc_len); 1476 return (error); 1477 } 1478 1479 int 1480 vfs_free_netcred(struct radix_node *rn, void *w, u_int id) 1481 { 1482 struct radix_node_head *rnh = (struct radix_node_head *)w; 1483 struct netcred * np = (struct netcred *)rn; 1484 1485 rn_delete(rn->rn_key, rn->rn_mask, rnh, NULL); 1486 free(np, M_NETADDR, np->netc_len); 1487 return (0); 1488 } 1489 1490 /* 1491 * Free the net address hash lists that are hanging off the mount points. 1492 */ 1493 void 1494 vfs_free_addrlist(struct netexport *nep) 1495 { 1496 struct radix_node_head *rnh; 1497 1498 if ((rnh = nep->ne_rtable_inet) != NULL) { 1499 rn_walktree(rnh, vfs_free_netcred, rnh); 1500 free(rnh, M_RTABLE, sizeof(*rnh)); 1501 nep->ne_rtable_inet = NULL; 1502 } 1503 } 1504 #endif /* NFSSERVER */ 1505 1506 int 1507 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp) 1508 { 1509 #ifdef NFSSERVER 1510 int error; 1511 1512 if (argp->ex_flags & MNT_DELEXPORT) { 1513 vfs_free_addrlist(nep); 1514 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 1515 } 1516 if (argp->ex_flags & MNT_EXPORTED) { 1517 if ((error = vfs_hang_addrlist(mp, nep, argp)) != 0) 1518 return (error); 1519 mp->mnt_flag |= MNT_EXPORTED; 1520 } 1521 return (0); 1522 #else 1523 return (ENOTSUP); 1524 #endif /* NFSSERVER */ 1525 } 1526 1527 struct netcred * 1528 vfs_export_lookup(struct mount *mp, struct netexport *nep, struct mbuf *nam) 1529 { 1530 #ifdef NFSSERVER 1531 struct netcred *np; 1532 struct radix_node_head *rnh; 1533 struct sockaddr *saddr; 1534 1535 np = NULL; 1536 if (mp->mnt_flag & MNT_EXPORTED) { 1537 /* 1538 * Lookup in the export list first. 1539 */ 1540 if (nam != NULL) { 1541 saddr = mtod(nam, struct sockaddr *); 1542 switch(saddr->sa_family) { 1543 case AF_INET: 1544 rnh = nep->ne_rtable_inet; 1545 break; 1546 default: 1547 rnh = NULL; 1548 break; 1549 } 1550 if (rnh != NULL) 1551 np = (struct netcred *)rn_match(saddr, rnh); 1552 } 1553 /* 1554 * If no address match, use the default if it exists. 1555 */ 1556 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 1557 np = &nep->ne_defexported; 1558 } 1559 return (np); 1560 #else 1561 return (NULL); 1562 #endif /* NFSSERVER */ 1563 } 1564 1565 /* 1566 * Do the usual access checking. 1567 * file_mode, uid and gid are from the vnode in question, 1568 * while acc_mode and cred are from the VOP_ACCESS parameter list 1569 */ 1570 int 1571 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 1572 mode_t acc_mode, struct ucred *cred) 1573 { 1574 mode_t mask; 1575 1576 /* User id 0 always gets read/write access. */ 1577 if (cred->cr_uid == 0) { 1578 /* For VEXEC, at least one of the execute bits must be set. */ 1579 if ((acc_mode & VEXEC) && type != VDIR && 1580 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 1581 return EACCES; 1582 return 0; 1583 } 1584 1585 mask = 0; 1586 1587 /* Otherwise, check the owner. */ 1588 if (cred->cr_uid == uid) { 1589 if (acc_mode & VEXEC) 1590 mask |= S_IXUSR; 1591 if (acc_mode & VREAD) 1592 mask |= S_IRUSR; 1593 if (acc_mode & VWRITE) 1594 mask |= S_IWUSR; 1595 return (file_mode & mask) == mask ? 0 : EACCES; 1596 } 1597 1598 /* Otherwise, check the groups. */ 1599 if (groupmember(gid, cred)) { 1600 if (acc_mode & VEXEC) 1601 mask |= S_IXGRP; 1602 if (acc_mode & VREAD) 1603 mask |= S_IRGRP; 1604 if (acc_mode & VWRITE) 1605 mask |= S_IWGRP; 1606 return (file_mode & mask) == mask ? 0 : EACCES; 1607 } 1608 1609 /* Otherwise, check everyone else. */ 1610 if (acc_mode & VEXEC) 1611 mask |= S_IXOTH; 1612 if (acc_mode & VREAD) 1613 mask |= S_IROTH; 1614 if (acc_mode & VWRITE) 1615 mask |= S_IWOTH; 1616 return (file_mode & mask) == mask ? 0 : EACCES; 1617 } 1618 1619 int 1620 vnoperm(struct vnode *vp) 1621 { 1622 if (vp->v_flag & VROOT || vp->v_mount == NULL) 1623 return 0; 1624 1625 return (vp->v_mount->mnt_flag & MNT_NOPERM); 1626 } 1627 1628 struct rwlock vfs_stall_lock = RWLOCK_INITIALIZER("vfs_stall"); 1629 unsigned int vfs_stalling = 0; 1630 1631 int 1632 vfs_stall(struct proc *p, int stall) 1633 { 1634 struct mount *mp; 1635 int allerror = 0, error; 1636 1637 if (stall) { 1638 atomic_inc_int(&vfs_stalling); 1639 rw_enter_write(&vfs_stall_lock); 1640 } 1641 1642 /* 1643 * The loop variable mp is protected by vfs_busy() so that it cannot 1644 * be unmounted while VFS_SYNC() sleeps. Traverse forward to keep the 1645 * lock order consistent with dounmount(). 1646 */ 1647 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 1648 if (stall) { 1649 error = vfs_busy(mp, VB_WRITE|VB_WAIT|VB_DUPOK); 1650 if (error) { 1651 printf("%s: busy\n", mp->mnt_stat.f_mntonname); 1652 allerror = error; 1653 continue; 1654 } 1655 uvm_vnp_sync(mp); 1656 error = VFS_SYNC(mp, MNT_WAIT, stall, p->p_ucred, p); 1657 if (error) { 1658 printf("%s: failed to sync\n", 1659 mp->mnt_stat.f_mntonname); 1660 vfs_unbusy(mp); 1661 allerror = error; 1662 continue; 1663 } 1664 mp->mnt_flag |= MNT_STALLED; 1665 } else { 1666 if (mp->mnt_flag & MNT_STALLED) { 1667 vfs_unbusy(mp); 1668 mp->mnt_flag &= ~MNT_STALLED; 1669 } 1670 } 1671 } 1672 1673 if (!stall) { 1674 rw_exit_write(&vfs_stall_lock); 1675 atomic_dec_int(&vfs_stalling); 1676 } 1677 1678 return (allerror); 1679 } 1680 1681 void 1682 vfs_stall_barrier(void) 1683 { 1684 if (__predict_false(vfs_stalling)) { 1685 rw_enter_read(&vfs_stall_lock); 1686 rw_exit_read(&vfs_stall_lock); 1687 } 1688 } 1689 1690 /* 1691 * Unmount all file systems. 1692 * We traverse the list in reverse order under the assumption that doing so 1693 * will avoid needing to worry about dependencies. 1694 */ 1695 void 1696 vfs_unmountall(void) 1697 { 1698 struct mount *mp, *nmp; 1699 int allerror, error, again = 1; 1700 1701 retry: 1702 allerror = 0; 1703 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, nmp) { 1704 if (vfs_busy(mp, VB_WRITE|VB_NOWAIT)) 1705 continue; 1706 /* XXX Here is a race, the next pointer is not locked. */ 1707 if ((error = dounmount(mp, MNT_FORCE, curproc)) != 0) { 1708 printf("unmount of %s failed with error %d\n", 1709 mp->mnt_stat.f_mntonname, error); 1710 allerror = 1; 1711 } 1712 } 1713 1714 if (allerror) { 1715 printf("WARNING: some file systems would not unmount\n"); 1716 if (again) { 1717 printf("retrying\n"); 1718 again = 0; 1719 goto retry; 1720 } 1721 } 1722 } 1723 1724 /* 1725 * Sync and unmount file systems before shutting down. 1726 */ 1727 void 1728 vfs_shutdown(struct proc *p) 1729 { 1730 #ifdef ACCOUNTING 1731 acct_shutdown(); 1732 #endif 1733 1734 printf("syncing disks..."); 1735 1736 if (panicstr == 0) { 1737 /* Sync before unmount, in case we hang on something. */ 1738 sys_sync(p, NULL, NULL); 1739 vfs_unmountall(); 1740 } 1741 1742 #if NSOFTRAID > 0 1743 sr_quiesce(); 1744 #endif 1745 1746 if (vfs_syncwait(p, 1)) 1747 printf(" giving up\n"); 1748 else 1749 printf(" done\n"); 1750 } 1751 1752 /* 1753 * perform sync() operation and wait for buffers to flush. 1754 */ 1755 int 1756 vfs_syncwait(struct proc *p, int verbose) 1757 { 1758 struct buf *bp; 1759 int iter, nbusy, dcount, s; 1760 #ifdef MULTIPROCESSOR 1761 int hold_count; 1762 #endif 1763 1764 sys_sync(p, NULL, NULL); 1765 1766 /* Wait for sync to finish. */ 1767 dcount = 10000; 1768 for (iter = 0; iter < 20; iter++) { 1769 nbusy = 0; 1770 LIST_FOREACH(bp, &bufhead, b_list) { 1771 if ((bp->b_flags & (B_BUSY|B_INVAL|B_READ)) == B_BUSY) 1772 nbusy++; 1773 /* 1774 * With soft updates, some buffers that are 1775 * written will be remarked as dirty until other 1776 * buffers are written. 1777 */ 1778 if (bp->b_flags & B_DELWRI) { 1779 s = splbio(); 1780 bremfree(bp); 1781 buf_acquire(bp); 1782 splx(s); 1783 nbusy++; 1784 bawrite(bp); 1785 if (dcount-- <= 0) { 1786 if (verbose) 1787 printf("softdep "); 1788 return 1; 1789 } 1790 } 1791 } 1792 if (nbusy == 0) 1793 break; 1794 if (verbose) 1795 printf("%d ", nbusy); 1796 #ifdef MULTIPROCESSOR 1797 if (_kernel_lock_held()) 1798 hold_count = __mp_release_all(&kernel_lock); 1799 else 1800 hold_count = 0; 1801 #endif 1802 DELAY(40000 * iter); 1803 #ifdef MULTIPROCESSOR 1804 if (hold_count) 1805 __mp_acquire_count(&kernel_lock, hold_count); 1806 #endif 1807 } 1808 1809 return nbusy; 1810 } 1811 1812 /* 1813 * posix file system related system variables. 1814 */ 1815 int 1816 fs_posix_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 1817 void *newp, size_t newlen, struct proc *p) 1818 { 1819 /* all sysctl names at this level are terminal */ 1820 if (namelen != 1) 1821 return (ENOTDIR); 1822 1823 switch (name[0]) { 1824 case FS_POSIX_SETUID: 1825 if (newp && securelevel > 0) 1826 return (EPERM); 1827 return(sysctl_int(oldp, oldlenp, newp, newlen, &suid_clear)); 1828 default: 1829 return (EOPNOTSUPP); 1830 } 1831 /* NOTREACHED */ 1832 } 1833 1834 /* 1835 * file system related system variables. 1836 */ 1837 int 1838 fs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 1839 size_t newlen, struct proc *p) 1840 { 1841 sysctlfn *fn; 1842 1843 switch (name[0]) { 1844 case FS_POSIX: 1845 fn = fs_posix_sysctl; 1846 break; 1847 default: 1848 return (EOPNOTSUPP); 1849 } 1850 return (*fn)(name + 1, namelen - 1, oldp, oldlenp, newp, newlen, p); 1851 } 1852 1853 1854 /* 1855 * Routines dealing with vnodes and buffers 1856 */ 1857 1858 /* 1859 * Wait for all outstanding I/Os to complete 1860 * 1861 * Manipulates v_numoutput. Must be called at splbio() 1862 */ 1863 int 1864 vwaitforio(struct vnode *vp, int slpflag, char *wmesg, uint64_t timeo) 1865 { 1866 int error = 0; 1867 1868 splassert(IPL_BIO); 1869 1870 while (vp->v_numoutput) { 1871 vp->v_bioflag |= VBIOWAIT; 1872 error = tsleep_nsec(&vp->v_numoutput, 1873 slpflag | (PRIBIO + 1), wmesg, timeo); 1874 if (error) 1875 break; 1876 } 1877 1878 return (error); 1879 } 1880 1881 /* 1882 * Update outstanding I/O count and do wakeup if requested. 1883 * 1884 * Manipulates v_numoutput. Must be called at splbio() 1885 */ 1886 void 1887 vwakeup(struct vnode *vp) 1888 { 1889 splassert(IPL_BIO); 1890 1891 if (vp != NULL) { 1892 if (vp->v_numoutput-- == 0) 1893 panic("vwakeup: neg numoutput"); 1894 if ((vp->v_bioflag & VBIOWAIT) && vp->v_numoutput == 0) { 1895 vp->v_bioflag &= ~VBIOWAIT; 1896 wakeup(&vp->v_numoutput); 1897 } 1898 } 1899 } 1900 1901 /* 1902 * Flush out and invalidate all buffers associated with a vnode. 1903 * Called with the underlying object locked. 1904 */ 1905 int 1906 vinvalbuf(struct vnode *vp, int flags, struct ucred *cred, struct proc *p, 1907 int slpflag, uint64_t slptimeo) 1908 { 1909 struct buf *bp; 1910 struct buf *nbp, *blist; 1911 int s, error; 1912 1913 #ifdef VFSLCKDEBUG 1914 if ((vp->v_flag & VLOCKSWORK) && !VOP_ISLOCKED(vp)) 1915 panic("%s: vp isn't locked, vp %p", __func__, vp); 1916 #endif 1917 1918 if (flags & V_SAVE) { 1919 s = splbio(); 1920 vwaitforio(vp, 0, "vinvalbuf", INFSLP); 1921 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) { 1922 splx(s); 1923 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0) 1924 return (error); 1925 s = splbio(); 1926 if (vp->v_numoutput > 0 || 1927 !LIST_EMPTY(&vp->v_dirtyblkhd)) 1928 panic("%s: dirty bufs, vp %p", __func__, vp); 1929 } 1930 splx(s); 1931 } 1932 loop: 1933 s = splbio(); 1934 for (;;) { 1935 int count = 0; 1936 if ((blist = LIST_FIRST(&vp->v_cleanblkhd)) && 1937 (flags & V_SAVEMETA)) 1938 while (blist && blist->b_lblkno < 0) 1939 blist = LIST_NEXT(blist, b_vnbufs); 1940 if (blist == NULL && 1941 (blist = LIST_FIRST(&vp->v_dirtyblkhd)) && 1942 (flags & V_SAVEMETA)) 1943 while (blist && blist->b_lblkno < 0) 1944 blist = LIST_NEXT(blist, b_vnbufs); 1945 if (!blist) 1946 break; 1947 1948 for (bp = blist; bp; bp = nbp) { 1949 nbp = LIST_NEXT(bp, b_vnbufs); 1950 if (flags & V_SAVEMETA && bp->b_lblkno < 0) 1951 continue; 1952 if (bp->b_flags & B_BUSY) { 1953 bp->b_flags |= B_WANTED; 1954 error = tsleep_nsec(bp, slpflag | (PRIBIO + 1), 1955 "vinvalbuf", slptimeo); 1956 if (error) { 1957 splx(s); 1958 return (error); 1959 } 1960 break; 1961 } 1962 bremfree(bp); 1963 /* 1964 * XXX Since there are no node locks for NFS, I believe 1965 * there is a slight chance that a delayed write will 1966 * occur while sleeping just above, so check for it. 1967 */ 1968 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) { 1969 buf_acquire(bp); 1970 splx(s); 1971 (void) VOP_BWRITE(bp); 1972 goto loop; 1973 } 1974 buf_acquire_nomap(bp); 1975 bp->b_flags |= B_INVAL; 1976 brelse(bp); 1977 count++; 1978 /* 1979 * XXX Temporary workaround XXX 1980 * 1981 * If this is a gigantisch vnode and we are 1982 * trashing a ton of buffers, drop the lock 1983 * and yield every so often. The longer term 1984 * fix is to add a separate list for these 1985 * invalid buffers so we don't have to do the 1986 * work to free these here. 1987 */ 1988 if (count > 100) { 1989 splx(s); 1990 sched_pause(yield); 1991 goto loop; 1992 } 1993 } 1994 } 1995 if (!(flags & V_SAVEMETA) && 1996 (!LIST_EMPTY(&vp->v_dirtyblkhd) || !LIST_EMPTY(&vp->v_cleanblkhd))) 1997 panic("%s: flush failed, vp %p", __func__, vp); 1998 splx(s); 1999 return (0); 2000 } 2001 2002 void 2003 vflushbuf(struct vnode *vp, int sync) 2004 { 2005 struct buf *bp, *nbp; 2006 int s; 2007 2008 loop: 2009 s = splbio(); 2010 LIST_FOREACH_SAFE(bp, &vp->v_dirtyblkhd, b_vnbufs, nbp) { 2011 if ((bp->b_flags & B_BUSY)) 2012 continue; 2013 if ((bp->b_flags & B_DELWRI) == 0) 2014 panic("vflushbuf: not dirty"); 2015 bremfree(bp); 2016 buf_acquire(bp); 2017 splx(s); 2018 /* 2019 * Wait for I/O associated with indirect blocks to complete, 2020 * since there is no way to quickly wait for them below. 2021 */ 2022 if (bp->b_vp == vp || sync == 0) 2023 (void) bawrite(bp); 2024 else 2025 (void) bwrite(bp); 2026 goto loop; 2027 } 2028 if (sync == 0) { 2029 splx(s); 2030 return; 2031 } 2032 vwaitforio(vp, 0, "vflushbuf", INFSLP); 2033 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) { 2034 splx(s); 2035 #ifdef DIAGNOSTIC 2036 vprint("vflushbuf: dirty", vp); 2037 #endif 2038 goto loop; 2039 } 2040 splx(s); 2041 } 2042 2043 /* 2044 * Associate a buffer with a vnode. 2045 * 2046 * Manipulates buffer vnode queues. Must be called at splbio(). 2047 */ 2048 void 2049 bgetvp(struct vnode *vp, struct buf *bp) 2050 { 2051 splassert(IPL_BIO); 2052 2053 2054 if (bp->b_vp) 2055 panic("bgetvp: not free"); 2056 vhold(vp); 2057 bp->b_vp = vp; 2058 if (vp->v_type == VBLK || vp->v_type == VCHR) 2059 bp->b_dev = vp->v_rdev; 2060 else 2061 bp->b_dev = NODEV; 2062 /* 2063 * Insert onto list for new vnode. 2064 */ 2065 bufinsvn(bp, &vp->v_cleanblkhd); 2066 } 2067 2068 /* 2069 * Disassociate a buffer from a vnode. 2070 * 2071 * Manipulates vnode buffer queues. Must be called at splbio(). 2072 */ 2073 void 2074 brelvp(struct buf *bp) 2075 { 2076 struct vnode *vp; 2077 2078 splassert(IPL_BIO); 2079 2080 if ((vp = bp->b_vp) == (struct vnode *) 0) 2081 panic("brelvp: NULL"); 2082 /* 2083 * Delete from old vnode list, if on one. 2084 */ 2085 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 2086 bufremvn(bp); 2087 if ((vp->v_bioflag & VBIOONSYNCLIST) && 2088 LIST_EMPTY(&vp->v_dirtyblkhd)) { 2089 vp->v_bioflag &= ~VBIOONSYNCLIST; 2090 LIST_REMOVE(vp, v_synclist); 2091 } 2092 bp->b_vp = NULL; 2093 2094 vdrop(vp); 2095 } 2096 2097 /* 2098 * Replaces the current vnode associated with the buffer, if any, 2099 * with a new vnode. 2100 * 2101 * If an output I/O is pending on the buffer, the old vnode 2102 * I/O count is adjusted. 2103 * 2104 * Ignores vnode buffer queues. Must be called at splbio(). 2105 */ 2106 void 2107 buf_replacevnode(struct buf *bp, struct vnode *newvp) 2108 { 2109 struct vnode *oldvp = bp->b_vp; 2110 2111 splassert(IPL_BIO); 2112 2113 if (oldvp) 2114 brelvp(bp); 2115 2116 if ((bp->b_flags & (B_READ | B_DONE)) == 0) { 2117 newvp->v_numoutput++; /* put it on swapdev */ 2118 vwakeup(oldvp); 2119 } 2120 2121 bgetvp(newvp, bp); 2122 bufremvn(bp); 2123 } 2124 2125 /* 2126 * Used to assign buffers to the appropriate clean or dirty list on 2127 * the vnode and to add newly dirty vnodes to the appropriate 2128 * filesystem syncer list. 2129 * 2130 * Manipulates vnode buffer queues. Must be called at splbio(). 2131 */ 2132 void 2133 reassignbuf(struct buf *bp) 2134 { 2135 struct buflists *listheadp; 2136 int delay; 2137 struct vnode *vp = bp->b_vp; 2138 2139 splassert(IPL_BIO); 2140 2141 /* 2142 * Delete from old vnode list, if on one. 2143 */ 2144 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 2145 bufremvn(bp); 2146 2147 /* 2148 * If dirty, put on list of dirty buffers; 2149 * otherwise insert onto list of clean buffers. 2150 */ 2151 if ((bp->b_flags & B_DELWRI) == 0) { 2152 listheadp = &vp->v_cleanblkhd; 2153 if ((vp->v_bioflag & VBIOONSYNCLIST) && 2154 LIST_EMPTY(&vp->v_dirtyblkhd)) { 2155 vp->v_bioflag &= ~VBIOONSYNCLIST; 2156 LIST_REMOVE(vp, v_synclist); 2157 } 2158 } else { 2159 listheadp = &vp->v_dirtyblkhd; 2160 if ((vp->v_bioflag & VBIOONSYNCLIST) == 0) { 2161 switch (vp->v_type) { 2162 case VDIR: 2163 delay = syncdelay / 2; 2164 break; 2165 case VBLK: 2166 if (vp->v_specmountpoint != NULL) { 2167 delay = syncdelay / 3; 2168 break; 2169 } 2170 /* FALLTHROUGH */ 2171 default: 2172 delay = syncdelay; 2173 } 2174 vn_syncer_add_to_worklist(vp, delay); 2175 } 2176 } 2177 bufinsvn(bp, listheadp); 2178 } 2179 2180 /* 2181 * Check if vnode represents a disk device 2182 */ 2183 int 2184 vn_isdisk(struct vnode *vp, int *errp) 2185 { 2186 if (vp->v_type != VBLK && vp->v_type != VCHR) 2187 return (0); 2188 2189 return (1); 2190 } 2191 2192 #ifdef DDB 2193 #include <machine/db_machdep.h> 2194 #include <ddb/db_interface.h> 2195 2196 void 2197 vfs_buf_print(void *b, int full, 2198 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2199 { 2200 struct buf *bp = b; 2201 2202 (*pr)(" vp %p lblkno 0x%llx blkno 0x%llx dev 0x%x\n" 2203 " proc %p error %d flags %lb\n", 2204 bp->b_vp, (int64_t)bp->b_lblkno, (int64_t)bp->b_blkno, bp->b_dev, 2205 bp->b_proc, bp->b_error, bp->b_flags, B_BITS); 2206 2207 (*pr)(" bufsize 0x%lx bcount 0x%lx resid 0x%lx\n" 2208 " data %p saveaddr %p dep %p iodone %p\n", 2209 bp->b_bufsize, bp->b_bcount, (long)bp->b_resid, 2210 bp->b_data, bp->b_saveaddr, 2211 LIST_FIRST(&bp->b_dep), bp->b_iodone); 2212 2213 (*pr)(" dirty {off 0x%x end 0x%x} valid {off 0x%x end 0x%x}\n", 2214 bp->b_dirtyoff, bp->b_dirtyend, bp->b_validoff, bp->b_validend); 2215 2216 #ifdef FFS_SOFTUPDATES 2217 if (full) 2218 softdep_print(bp, full, pr); 2219 #endif 2220 } 2221 2222 const char *vtypes[] = { VTYPE_NAMES }; 2223 const char *vtags[] = { VTAG_NAMES }; 2224 2225 void 2226 vfs_vnode_print(void *v, int full, 2227 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2228 { 2229 struct vnode *vp = v; 2230 2231 (*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n", 2232 (u_int)vp->v_tag >= nitems(vtags)? "<unk>":vtags[vp->v_tag], 2233 vp->v_tag, 2234 (u_int)vp->v_type >= nitems(vtypes)? "<unk>":vtypes[vp->v_type], 2235 vp->v_type, vp->v_mount, vp->v_mountedhere); 2236 2237 (*pr)("data %p usecount %d writecount %d holdcnt %d numoutput %d\n", 2238 vp->v_data, vp->v_usecount, vp->v_writecount, 2239 vp->v_holdcnt, vp->v_numoutput); 2240 2241 /* uvm_object_printit(&vp->v_uobj, full, pr); */ 2242 2243 if (full) { 2244 struct buf *bp; 2245 2246 (*pr)("clean bufs:\n"); 2247 LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) { 2248 (*pr)(" bp %p\n", bp); 2249 vfs_buf_print(bp, full, pr); 2250 } 2251 2252 (*pr)("dirty bufs:\n"); 2253 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) { 2254 (*pr)(" bp %p\n", bp); 2255 vfs_buf_print(bp, full, pr); 2256 } 2257 } 2258 } 2259 2260 void 2261 vfs_mount_print(struct mount *mp, int full, 2262 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2263 { 2264 struct vfsconf *vfc = mp->mnt_vfc; 2265 struct vnode *vp; 2266 int cnt; 2267 2268 (*pr)("flags %b\nvnodecovered %p syncer %p data %p\n", 2269 mp->mnt_flag, MNT_BITS, 2270 mp->mnt_vnodecovered, mp->mnt_syncer, mp->mnt_data); 2271 2272 (*pr)("vfsconf: ops %p name \"%s\" num %d ref %u flags 0x%x\n", 2273 vfc->vfc_vfsops, vfc->vfc_name, vfc->vfc_typenum, 2274 vfc->vfc_refcount, vfc->vfc_flags); 2275 2276 (*pr)("statvfs cache: bsize %x iosize %x\n" 2277 "blocks %llu free %llu avail %lld\n", 2278 mp->mnt_stat.f_bsize, mp->mnt_stat.f_iosize, mp->mnt_stat.f_blocks, 2279 mp->mnt_stat.f_bfree, mp->mnt_stat.f_bavail); 2280 2281 (*pr)(" files %llu ffiles %llu favail %lld\n", mp->mnt_stat.f_files, 2282 mp->mnt_stat.f_ffree, mp->mnt_stat.f_favail); 2283 2284 (*pr)(" f_fsidx {0x%x, 0x%x} owner %u ctime 0x%llx\n", 2285 mp->mnt_stat.f_fsid.val[0], mp->mnt_stat.f_fsid.val[1], 2286 mp->mnt_stat.f_owner, mp->mnt_stat.f_ctime); 2287 2288 (*pr)(" syncwrites %llu asyncwrites = %llu\n", 2289 mp->mnt_stat.f_syncwrites, mp->mnt_stat.f_asyncwrites); 2290 2291 (*pr)(" syncreads %llu asyncreads = %llu\n", 2292 mp->mnt_stat.f_syncreads, mp->mnt_stat.f_asyncreads); 2293 2294 (*pr)(" fstype \"%s\" mnton \"%s\" mntfrom \"%s\" mntspec \"%s\"\n", 2295 mp->mnt_stat.f_fstypename, mp->mnt_stat.f_mntonname, 2296 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntfromspec); 2297 2298 (*pr)("locked vnodes:"); 2299 /* XXX would take mountlist lock, except ddb has no context */ 2300 cnt = 0; 2301 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2302 if (VOP_ISLOCKED(vp)) { 2303 if (cnt == 0) 2304 (*pr)("\n %p", vp); 2305 else if ((cnt % (72 / (sizeof(void *) * 2 + 4))) == 0) 2306 (*pr)(",\n %p", vp); 2307 else 2308 (*pr)(", %p", vp); 2309 cnt++; 2310 } 2311 } 2312 (*pr)("\n"); 2313 2314 if (full) { 2315 (*pr)("all vnodes:"); 2316 /* XXX would take mountlist lock, except ddb has no context */ 2317 cnt = 0; 2318 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2319 if (cnt == 0) 2320 (*pr)("\n %p", vp); 2321 else if ((cnt % (72 / (sizeof(void *) * 2 + 4))) == 0) 2322 (*pr)(",\n %p", vp); 2323 else 2324 (*pr)(", %p", vp); 2325 cnt++; 2326 } 2327 (*pr)("\n"); 2328 } 2329 } 2330 #endif /* DDB */ 2331 2332 void 2333 copy_statfs_info(struct statfs *sbp, const struct mount *mp) 2334 { 2335 const struct statfs *mbp; 2336 2337 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 2338 2339 if (sbp == (mbp = &mp->mnt_stat)) 2340 return; 2341 2342 sbp->f_fsid = mbp->f_fsid; 2343 sbp->f_owner = mbp->f_owner; 2344 sbp->f_flags = mbp->f_flags; 2345 sbp->f_syncwrites = mbp->f_syncwrites; 2346 sbp->f_asyncwrites = mbp->f_asyncwrites; 2347 sbp->f_syncreads = mbp->f_syncreads; 2348 sbp->f_asyncreads = mbp->f_asyncreads; 2349 sbp->f_namemax = mbp->f_namemax; 2350 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN); 2351 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN); 2352 memcpy(sbp->f_mntfromspec, mp->mnt_stat.f_mntfromspec, MNAMELEN); 2353 memcpy(&sbp->mount_info, &mp->mnt_stat.mount_info, 2354 sizeof(union mount_info)); 2355 } 2356