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