1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 39 */ 40 41 /* 42 * External virtual filesystem routines 43 */ 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include "opt_ddb.h" 49 #include "opt_mac.h" 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/bio.h> 54 #include <sys/buf.h> 55 #include <sys/conf.h> 56 #include <sys/eventhandler.h> 57 #include <sys/extattr.h> 58 #include <sys/fcntl.h> 59 #include <sys/kernel.h> 60 #include <sys/kthread.h> 61 #include <sys/mac.h> 62 #include <sys/malloc.h> 63 #include <sys/mount.h> 64 #include <sys/namei.h> 65 #include <sys/stat.h> 66 #include <sys/sysctl.h> 67 #include <sys/syslog.h> 68 #include <sys/vmmeter.h> 69 #include <sys/vnode.h> 70 71 #include <vm/vm.h> 72 #include <vm/vm_object.h> 73 #include <vm/vm_extern.h> 74 #include <vm/pmap.h> 75 #include <vm/vm_map.h> 76 #include <vm/vm_page.h> 77 #include <vm/vm_kern.h> 78 #include <vm/uma.h> 79 80 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure"); 81 82 static void addalias(struct vnode *vp, dev_t nvp_rdev); 83 static void insmntque(struct vnode *vp, struct mount *mp); 84 static void vclean(struct vnode *vp, int flags, struct thread *td); 85 static void vlruvp(struct vnode *vp); 86 static int flushbuflist(struct buf *blist, int flags, struct vnode *vp, 87 int slpflag, int slptimeo, int *errorp); 88 static int vcanrecycle(struct vnode *vp, struct mount **vnmpp); 89 90 91 /* 92 * Number of vnodes in existence. Increased whenever getnewvnode() 93 * allocates a new vnode, never decreased. 94 */ 95 static unsigned long numvnodes; 96 97 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, ""); 98 99 /* 100 * Conversion tables for conversion from vnode types to inode formats 101 * and back. 102 */ 103 enum vtype iftovt_tab[16] = { 104 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 105 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 106 }; 107 int vttoif_tab[9] = { 108 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 109 S_IFSOCK, S_IFIFO, S_IFMT, 110 }; 111 112 /* 113 * List of vnodes that are ready for recycling. 114 */ 115 static TAILQ_HEAD(freelst, vnode) vnode_free_list; 116 117 /* 118 * Minimum number of free vnodes. If there are fewer than this free vnodes, 119 * getnewvnode() will return a newly allocated vnode. 120 */ 121 static u_long wantfreevnodes = 25; 122 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, ""); 123 /* Number of vnodes in the free list. */ 124 static u_long freevnodes; 125 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, ""); 126 127 /* 128 * Various variables used for debugging the new implementation of 129 * reassignbuf(). 130 * XXX these are probably of (very) limited utility now. 131 */ 132 static int reassignbufcalls; 133 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, ""); 134 static int nameileafonly; 135 SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, ""); 136 137 /* 138 * Cache for the mount type id assigned to NFS. This is used for 139 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c. 140 */ 141 int nfs_mount_type = -1; 142 143 /* To keep more than one thread at a time from running vfs_getnewfsid */ 144 static struct mtx mntid_mtx; 145 146 /* 147 * Lock for any access to the following: 148 * vnode_free_list 149 * numvnodes 150 * freevnodes 151 */ 152 static struct mtx vnode_free_list_mtx; 153 154 /* 155 * For any iteration/modification of dev->si_hlist (linked through 156 * v_specnext) 157 */ 158 static struct mtx spechash_mtx; 159 160 /* Publicly exported FS */ 161 struct nfs_public nfs_pub; 162 163 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 164 static uma_zone_t vnode_zone; 165 static uma_zone_t vnodepoll_zone; 166 167 /* Set to 1 to print out reclaim of active vnodes */ 168 int prtactive; 169 170 /* 171 * The workitem queue. 172 * 173 * It is useful to delay writes of file data and filesystem metadata 174 * for tens of seconds so that quickly created and deleted files need 175 * not waste disk bandwidth being created and removed. To realize this, 176 * we append vnodes to a "workitem" queue. When running with a soft 177 * updates implementation, most pending metadata dependencies should 178 * not wait for more than a few seconds. Thus, mounted on block devices 179 * are delayed only about a half the time that file data is delayed. 180 * Similarly, directory updates are more critical, so are only delayed 181 * about a third the time that file data is delayed. Thus, there are 182 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 183 * one each second (driven off the filesystem syncer process). The 184 * syncer_delayno variable indicates the next queue that is to be processed. 185 * Items that need to be processed soon are placed in this queue: 186 * 187 * syncer_workitem_pending[syncer_delayno] 188 * 189 * A delay of fifteen seconds is done by placing the request fifteen 190 * entries later in the queue: 191 * 192 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 193 * 194 */ 195 static int syncer_delayno; 196 static long syncer_mask; 197 LIST_HEAD(synclist, vnode); 198 static struct synclist *syncer_workitem_pending; 199 /* 200 * The sync_mtx protects: 201 * vp->v_synclist 202 * syncer_delayno 203 * syncer_workitem_pending 204 * rushjob 205 */ 206 static struct mtx sync_mtx; 207 208 #define SYNCER_MAXDELAY 32 209 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 210 static int syncdelay = 30; /* max time to delay syncing data */ 211 static int filedelay = 30; /* time to delay syncing files */ 212 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, ""); 213 static int dirdelay = 29; /* time to delay syncing directories */ 214 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, ""); 215 static int metadelay = 28; /* time to delay syncing metadata */ 216 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, ""); 217 static int rushjob; /* number of slots to run ASAP */ 218 static int stat_rush_requests; /* number of times I/O speeded up */ 219 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, ""); 220 221 /* 222 * Number of vnodes we want to exist at any one time. This is mostly used 223 * to size hash tables in vnode-related code. It is normally not used in 224 * getnewvnode(), as wantfreevnodes is normally nonzero.) 225 * 226 * XXX desiredvnodes is historical cruft and should not exist. 227 */ 228 int desiredvnodes; 229 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 230 &desiredvnodes, 0, "Maximum number of vnodes"); 231 static int minvnodes; 232 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 233 &minvnodes, 0, "Minimum number of vnodes"); 234 static int vnlru_nowhere; 235 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0, 236 "Number of times the vnlru process ran without success"); 237 238 /* Hook for calling soft updates */ 239 int (*softdep_process_worklist_hook)(struct mount *); 240 241 /* 242 * This only exists to supress warnings from unlocked specfs accesses. It is 243 * no longer ok to have an unlocked VFS. 244 */ 245 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD) 246 247 /* Print lock violations */ 248 int vfs_badlock_print = 1; 249 250 /* Panic on violation */ 251 int vfs_badlock_panic = 1; 252 253 /* Check for interlock across VOPs */ 254 int vfs_badlock_mutex = 1; 255 256 static void 257 vfs_badlock(char *msg, char *str, struct vnode *vp) 258 { 259 if (vfs_badlock_print) 260 printf("%s: %p %s\n", str, vp, msg); 261 if (vfs_badlock_panic) 262 Debugger("Lock violation.\n"); 263 } 264 265 void 266 assert_vi_unlocked(struct vnode *vp, char *str) 267 { 268 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 269 vfs_badlock("interlock is locked but should not be", str, vp); 270 } 271 272 void 273 assert_vi_locked(struct vnode *vp, char *str) 274 { 275 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 276 vfs_badlock("interlock is not locked but should be", str, vp); 277 } 278 279 void 280 assert_vop_locked(struct vnode *vp, char *str) 281 { 282 if (vp && !IGNORE_LOCK(vp) && !VOP_ISLOCKED(vp, NULL)) 283 vfs_badlock("is not locked but should be", str, vp); 284 } 285 286 void 287 assert_vop_unlocked(struct vnode *vp, char *str) 288 { 289 if (vp && !IGNORE_LOCK(vp) && 290 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) 291 vfs_badlock("is locked but should not be", str, vp); 292 } 293 294 void 295 assert_vop_elocked(struct vnode *vp, char *str) 296 { 297 if (vp && !IGNORE_LOCK(vp) && 298 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE) 299 vfs_badlock("is not exclusive locked but should be", str, vp); 300 } 301 302 void 303 assert_vop_elocked_other(struct vnode *vp, char *str) 304 { 305 if (vp && !IGNORE_LOCK(vp) && 306 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER) 307 vfs_badlock("is not exclusive locked by another thread", 308 str, vp); 309 } 310 311 void 312 assert_vop_slocked(struct vnode *vp, char *str) 313 { 314 if (vp && !IGNORE_LOCK(vp) && 315 VOP_ISLOCKED(vp, curthread) != LK_SHARED) 316 vfs_badlock("is not locked shared but should be", str, vp); 317 } 318 319 void 320 vop_rename_pre(void *ap) 321 { 322 struct vop_rename_args *a = ap; 323 324 if (a->a_tvp) 325 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 326 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 327 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 328 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 329 330 /* Check the source (from) */ 331 if (a->a_tdvp != a->a_fdvp) 332 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked.\n"); 333 if (a->a_tvp != a->a_fvp) 334 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked.\n"); 335 336 /* Check the target */ 337 if (a->a_tvp) 338 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked.\n"); 339 340 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked.\n"); 341 } 342 343 void 344 vop_strategy_pre(void *ap) 345 { 346 struct vop_strategy_args *a = ap; 347 struct buf *bp; 348 349 bp = a->a_bp; 350 351 /* 352 * Cluster ops lock their component buffers but not the IO container. 353 */ 354 if ((bp->b_flags & B_CLUSTER) != 0) 355 return; 356 357 if (BUF_REFCNT(bp) < 1) { 358 if (vfs_badlock_print) 359 printf("VOP_STRATEGY: bp is not locked but should be.\n"); 360 if (vfs_badlock_panic) 361 Debugger("Lock violation.\n"); 362 } 363 } 364 365 void 366 vop_lookup_pre(void *ap) 367 { 368 struct vop_lookup_args *a = ap; 369 struct vnode *dvp; 370 371 dvp = a->a_dvp; 372 373 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 374 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 375 } 376 377 void 378 vop_lookup_post(void *ap, int rc) 379 { 380 struct vop_lookup_args *a = ap; 381 struct componentname *cnp; 382 struct vnode *dvp; 383 struct vnode *vp; 384 int flags; 385 386 dvp = a->a_dvp; 387 cnp = a->a_cnp; 388 vp = *(a->a_vpp); 389 flags = cnp->cn_flags; 390 391 392 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 393 /* 394 * If this is the last path component for this lookup and LOCPARENT 395 * is set, OR if there is an error the directory has to be locked. 396 */ 397 if ((flags & LOCKPARENT) && (flags & ISLASTCN)) 398 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (LOCKPARENT)"); 399 else if (rc != 0) 400 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (error)"); 401 else if (dvp != vp) 402 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (dvp)"); 403 404 if (flags & PDIRUNLOCK) 405 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (PDIRUNLOCK)"); 406 } 407 408 void 409 vop_unlock_pre(void *ap) 410 { 411 struct vop_unlock_args *a = ap; 412 413 if (a->a_flags & LK_INTERLOCK) 414 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 415 416 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 417 } 418 419 void 420 vop_unlock_post(void *ap, int rc) 421 { 422 struct vop_unlock_args *a = ap; 423 424 if (a->a_flags & LK_INTERLOCK) 425 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 426 } 427 428 void 429 vop_lock_pre(void *ap) 430 { 431 struct vop_lock_args *a = ap; 432 433 if ((a->a_flags & LK_INTERLOCK) == 0) 434 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 435 else 436 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 437 } 438 439 void 440 vop_lock_post(void *ap, int rc) 441 { 442 struct vop_lock_args *a; 443 444 a = ap; 445 446 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 447 if (rc == 0) 448 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 449 } 450 451 void 452 v_addpollinfo(struct vnode *vp) 453 { 454 vp->v_pollinfo = uma_zalloc(vnodepoll_zone, M_WAITOK); 455 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 456 } 457 458 /* 459 * Initialize the vnode management data structures. 460 */ 461 static void 462 vntblinit(void *dummy __unused) 463 { 464 465 /* 466 * Desiredvnodes is a function of the physical memory size and 467 * the kernel's heap size. Specifically, desiredvnodes scales 468 * in proportion to the physical memory size until two fifths 469 * of the kernel's heap size is consumed by vnodes and vm 470 * objects. 471 */ 472 desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size / 473 (5 * (sizeof(struct vm_object) + sizeof(struct vnode)))); 474 minvnodes = desiredvnodes / 4; 475 mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF); 476 mtx_init(&mntvnode_mtx, "mntvnode", NULL, MTX_DEF); 477 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 478 mtx_init(&spechash_mtx, "spechash", NULL, MTX_DEF); 479 TAILQ_INIT(&vnode_free_list); 480 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF); 481 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 482 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 483 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 484 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 485 /* 486 * Initialize the filesystem syncer. 487 */ 488 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 489 &syncer_mask); 490 syncer_maxdelay = syncer_mask + 1; 491 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF); 492 } 493 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL) 494 495 496 /* 497 * Mark a mount point as busy. Used to synchronize access and to delay 498 * unmounting. Interlock is not released on failure. 499 */ 500 int 501 vfs_busy(mp, flags, interlkp, td) 502 struct mount *mp; 503 int flags; 504 struct mtx *interlkp; 505 struct thread *td; 506 { 507 int lkflags; 508 509 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 510 if (flags & LK_NOWAIT) 511 return (ENOENT); 512 mp->mnt_kern_flag |= MNTK_MWAIT; 513 /* 514 * Since all busy locks are shared except the exclusive 515 * lock granted when unmounting, the only place that a 516 * wakeup needs to be done is at the release of the 517 * exclusive lock at the end of dounmount. 518 */ 519 msleep(mp, interlkp, PVFS, "vfs_busy", 0); 520 return (ENOENT); 521 } 522 lkflags = LK_SHARED | LK_NOPAUSE; 523 if (interlkp) 524 lkflags |= LK_INTERLOCK; 525 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td)) 526 panic("vfs_busy: unexpected lock failure"); 527 return (0); 528 } 529 530 /* 531 * Free a busy filesystem. 532 */ 533 void 534 vfs_unbusy(mp, td) 535 struct mount *mp; 536 struct thread *td; 537 { 538 539 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 540 } 541 542 /* 543 * Lookup a mount point by filesystem identifier. 544 */ 545 struct mount * 546 vfs_getvfs(fsid) 547 fsid_t *fsid; 548 { 549 register struct mount *mp; 550 551 mtx_lock(&mountlist_mtx); 552 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 553 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 554 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 555 mtx_unlock(&mountlist_mtx); 556 return (mp); 557 } 558 } 559 mtx_unlock(&mountlist_mtx); 560 return ((struct mount *) 0); 561 } 562 563 /* 564 * Get a new unique fsid. Try to make its val[0] unique, since this value 565 * will be used to create fake device numbers for stat(). Also try (but 566 * not so hard) make its val[0] unique mod 2^16, since some emulators only 567 * support 16-bit device numbers. We end up with unique val[0]'s for the 568 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 569 * 570 * Keep in mind that several mounts may be running in parallel. Starting 571 * the search one past where the previous search terminated is both a 572 * micro-optimization and a defense against returning the same fsid to 573 * different mounts. 574 */ 575 void 576 vfs_getnewfsid(mp) 577 struct mount *mp; 578 { 579 static u_int16_t mntid_base; 580 fsid_t tfsid; 581 int mtype; 582 583 mtx_lock(&mntid_mtx); 584 mtype = mp->mnt_vfc->vfc_typenum; 585 tfsid.val[1] = mtype; 586 mtype = (mtype & 0xFF) << 24; 587 for (;;) { 588 tfsid.val[0] = makeudev(255, 589 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 590 mntid_base++; 591 if (vfs_getvfs(&tfsid) == NULL) 592 break; 593 } 594 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 595 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 596 mtx_unlock(&mntid_mtx); 597 } 598 599 /* 600 * Knob to control the precision of file timestamps: 601 * 602 * 0 = seconds only; nanoseconds zeroed. 603 * 1 = seconds and nanoseconds, accurate within 1/HZ. 604 * 2 = seconds and nanoseconds, truncated to microseconds. 605 * >=3 = seconds and nanoseconds, maximum precision. 606 */ 607 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 608 609 static int timestamp_precision = TSP_SEC; 610 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 611 ×tamp_precision, 0, ""); 612 613 /* 614 * Get a current timestamp. 615 */ 616 void 617 vfs_timestamp(tsp) 618 struct timespec *tsp; 619 { 620 struct timeval tv; 621 622 switch (timestamp_precision) { 623 case TSP_SEC: 624 tsp->tv_sec = time_second; 625 tsp->tv_nsec = 0; 626 break; 627 case TSP_HZ: 628 getnanotime(tsp); 629 break; 630 case TSP_USEC: 631 microtime(&tv); 632 TIMEVAL_TO_TIMESPEC(&tv, tsp); 633 break; 634 case TSP_NSEC: 635 default: 636 nanotime(tsp); 637 break; 638 } 639 } 640 641 /* 642 * Set vnode attributes to VNOVAL 643 */ 644 void 645 vattr_null(vap) 646 register struct vattr *vap; 647 { 648 649 vap->va_type = VNON; 650 vap->va_size = VNOVAL; 651 vap->va_bytes = VNOVAL; 652 vap->va_mode = VNOVAL; 653 vap->va_nlink = VNOVAL; 654 vap->va_uid = VNOVAL; 655 vap->va_gid = VNOVAL; 656 vap->va_fsid = VNOVAL; 657 vap->va_fileid = VNOVAL; 658 vap->va_blocksize = VNOVAL; 659 vap->va_rdev = VNOVAL; 660 vap->va_atime.tv_sec = VNOVAL; 661 vap->va_atime.tv_nsec = VNOVAL; 662 vap->va_mtime.tv_sec = VNOVAL; 663 vap->va_mtime.tv_nsec = VNOVAL; 664 vap->va_ctime.tv_sec = VNOVAL; 665 vap->va_ctime.tv_nsec = VNOVAL; 666 vap->va_birthtime.tv_sec = VNOVAL; 667 vap->va_birthtime.tv_nsec = VNOVAL; 668 vap->va_flags = VNOVAL; 669 vap->va_gen = VNOVAL; 670 vap->va_vaflags = 0; 671 } 672 673 /* 674 * This routine is called when we have too many vnodes. It attempts 675 * to free <count> vnodes and will potentially free vnodes that still 676 * have VM backing store (VM backing store is typically the cause 677 * of a vnode blowout so we want to do this). Therefore, this operation 678 * is not considered cheap. 679 * 680 * A number of conditions may prevent a vnode from being reclaimed. 681 * the buffer cache may have references on the vnode, a directory 682 * vnode may still have references due to the namei cache representing 683 * underlying files, or the vnode may be in active use. It is not 684 * desireable to reuse such vnodes. These conditions may cause the 685 * number of vnodes to reach some minimum value regardless of what 686 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 687 */ 688 static int 689 vlrureclaim(struct mount *mp) 690 { 691 struct vnode *vp; 692 int done; 693 int trigger; 694 int usevnodes; 695 int count; 696 697 /* 698 * Calculate the trigger point, don't allow user 699 * screwups to blow us up. This prevents us from 700 * recycling vnodes with lots of resident pages. We 701 * aren't trying to free memory, we are trying to 702 * free vnodes. 703 */ 704 usevnodes = desiredvnodes; 705 if (usevnodes <= 0) 706 usevnodes = 1; 707 trigger = cnt.v_page_count * 2 / usevnodes; 708 709 done = 0; 710 mtx_lock(&mntvnode_mtx); 711 count = mp->mnt_nvnodelistsize / 10 + 1; 712 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) { 713 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 714 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 715 716 if (vp->v_type != VNON && 717 vp->v_type != VBAD && 718 VI_TRYLOCK(vp)) { 719 if (VMIGHTFREE(vp) && /* critical path opt */ 720 (vp->v_object == NULL || 721 vp->v_object->resident_page_count < trigger)) { 722 mtx_unlock(&mntvnode_mtx); 723 vgonel(vp, curthread); 724 done++; 725 mtx_lock(&mntvnode_mtx); 726 } else 727 VI_UNLOCK(vp); 728 } 729 --count; 730 } 731 mtx_unlock(&mntvnode_mtx); 732 return done; 733 } 734 735 /* 736 * Attempt to recycle vnodes in a context that is always safe to block. 737 * Calling vlrurecycle() from the bowels of filesystem code has some 738 * interesting deadlock problems. 739 */ 740 static struct proc *vnlruproc; 741 static int vnlruproc_sig; 742 743 static void 744 vnlru_proc(void) 745 { 746 struct mount *mp, *nmp; 747 int done; 748 struct proc *p = vnlruproc; 749 struct thread *td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */ 750 751 mtx_lock(&Giant); 752 753 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p, 754 SHUTDOWN_PRI_FIRST); 755 756 for (;;) { 757 kthread_suspend_check(p); 758 mtx_lock(&vnode_free_list_mtx); 759 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) { 760 mtx_unlock(&vnode_free_list_mtx); 761 vnlruproc_sig = 0; 762 wakeup(&vnlruproc_sig); 763 tsleep(vnlruproc, PVFS, "vlruwt", hz); 764 continue; 765 } 766 mtx_unlock(&vnode_free_list_mtx); 767 done = 0; 768 mtx_lock(&mountlist_mtx); 769 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 770 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) { 771 nmp = TAILQ_NEXT(mp, mnt_list); 772 continue; 773 } 774 done += vlrureclaim(mp); 775 mtx_lock(&mountlist_mtx); 776 nmp = TAILQ_NEXT(mp, mnt_list); 777 vfs_unbusy(mp, td); 778 } 779 mtx_unlock(&mountlist_mtx); 780 if (done == 0) { 781 #if 0 782 /* These messages are temporary debugging aids */ 783 if (vnlru_nowhere < 5) 784 printf("vnlru process getting nowhere..\n"); 785 else if (vnlru_nowhere == 5) 786 printf("vnlru process messages stopped.\n"); 787 #endif 788 vnlru_nowhere++; 789 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 790 } 791 } 792 } 793 794 static struct kproc_desc vnlru_kp = { 795 "vnlru", 796 vnlru_proc, 797 &vnlruproc 798 }; 799 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp) 800 801 802 /* 803 * Routines having to do with the management of the vnode table. 804 */ 805 806 /* 807 * Check to see if a free vnode can be recycled. If it can, 808 * return it locked with the vn lock, but not interlock. Also 809 * get the vn_start_write lock. Otherwise indicate the error. 810 */ 811 static int 812 vcanrecycle(struct vnode *vp, struct mount **vnmpp) 813 { 814 struct thread *td = curthread; 815 vm_object_t object; 816 int error; 817 818 /* Don't recycle if we can't get the interlock */ 819 if (!VI_TRYLOCK(vp)) 820 return (EWOULDBLOCK); 821 822 /* We should be able to immediately acquire this */ 823 /* XXX This looks like it should panic if it fails */ 824 if (vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td) != 0) { 825 if (VOP_ISLOCKED(vp, td)) 826 panic("vcanrecycle: locked vnode"); 827 return (EWOULDBLOCK); 828 } 829 830 /* 831 * Don't recycle if its filesystem is being suspended. 832 */ 833 if (vn_start_write(vp, vnmpp, V_NOWAIT) != 0) { 834 error = EBUSY; 835 goto done; 836 } 837 838 /* 839 * Don't recycle if we still have cached pages. 840 */ 841 if (VOP_GETVOBJECT(vp, &object) == 0) { 842 VM_OBJECT_LOCK(object); 843 if (object->resident_page_count || 844 object->ref_count) { 845 VM_OBJECT_UNLOCK(object); 846 error = EBUSY; 847 goto done; 848 } 849 VM_OBJECT_UNLOCK(object); 850 } 851 if (LIST_FIRST(&vp->v_cache_src)) { 852 /* 853 * note: nameileafonly sysctl is temporary, 854 * for debugging only, and will eventually be 855 * removed. 856 */ 857 if (nameileafonly > 0) { 858 /* 859 * Do not reuse namei-cached directory 860 * vnodes that have cached 861 * subdirectories. 862 */ 863 if (cache_leaf_test(vp) < 0) { 864 error = EISDIR; 865 goto done; 866 } 867 } else if (nameileafonly < 0 || 868 vmiodirenable == 0) { 869 /* 870 * Do not reuse namei-cached directory 871 * vnodes if nameileafonly is -1 or 872 * if VMIO backing for directories is 873 * turned off (otherwise we reuse them 874 * too quickly). 875 */ 876 error = EBUSY; 877 goto done; 878 } 879 } 880 return (0); 881 done: 882 VOP_UNLOCK(vp, 0, td); 883 return (error); 884 } 885 886 /* 887 * Return the next vnode from the free list. 888 */ 889 int 890 getnewvnode(tag, mp, vops, vpp) 891 const char *tag; 892 struct mount *mp; 893 vop_t **vops; 894 struct vnode **vpp; 895 { 896 struct thread *td = curthread; /* XXX */ 897 struct vnode *vp = NULL; 898 struct vpollinfo *pollinfo = NULL; 899 struct mount *vnmp; 900 901 mtx_lock(&vnode_free_list_mtx); 902 903 /* 904 * Try to reuse vnodes if we hit the max. This situation only 905 * occurs in certain large-memory (2G+) situations. We cannot 906 * attempt to directly reclaim vnodes due to nasty recursion 907 * problems. 908 */ 909 while (numvnodes - freevnodes > desiredvnodes) { 910 if (vnlruproc_sig == 0) { 911 vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 912 wakeup(vnlruproc); 913 } 914 mtx_unlock(&vnode_free_list_mtx); 915 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz); 916 mtx_lock(&vnode_free_list_mtx); 917 } 918 919 /* 920 * Attempt to reuse a vnode already on the free list, allocating 921 * a new vnode if we can't find one or if we have not reached a 922 * good minimum for good LRU performance. 923 */ 924 925 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) { 926 int error; 927 int count; 928 929 for (count = 0; count < freevnodes; count++) { 930 vp = TAILQ_FIRST(&vnode_free_list); 931 932 KASSERT(vp->v_usecount == 0 && 933 (vp->v_iflag & VI_DOINGINACT) == 0, 934 ("getnewvnode: free vnode isn't")); 935 936 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 937 /* 938 * We have to drop the free list mtx to avoid lock 939 * order reversals with interlock. 940 */ 941 mtx_unlock(&vnode_free_list_mtx); 942 error = vcanrecycle(vp, &vnmp); 943 mtx_lock(&vnode_free_list_mtx); 944 if (error == 0) 945 break; 946 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 947 vp = NULL; 948 } 949 } 950 if (vp) { 951 freevnodes--; 952 mtx_unlock(&vnode_free_list_mtx); 953 954 cache_purge(vp); 955 VI_LOCK(vp); 956 vp->v_iflag |= VI_DOOMED; 957 vp->v_iflag &= ~VI_FREE; 958 if (vp->v_type != VBAD) { 959 VOP_UNLOCK(vp, 0, td); 960 vgonel(vp, td); 961 VI_LOCK(vp); 962 } else { 963 VOP_UNLOCK(vp, 0, td); 964 } 965 vn_finished_write(vnmp); 966 967 #ifdef INVARIANTS 968 { 969 if (vp->v_data) 970 panic("cleaned vnode isn't"); 971 if (vp->v_numoutput) 972 panic("Clean vnode has pending I/O's"); 973 if (vp->v_writecount != 0) 974 panic("Non-zero write count"); 975 } 976 #endif 977 if ((pollinfo = vp->v_pollinfo) != NULL) { 978 /* 979 * To avoid lock order reversals, the call to 980 * uma_zfree() must be delayed until the vnode 981 * interlock is released. 982 */ 983 vp->v_pollinfo = NULL; 984 } 985 #ifdef MAC 986 mac_destroy_vnode(vp); 987 #endif 988 vp->v_iflag = 0; 989 vp->v_vflag = 0; 990 vp->v_lastw = 0; 991 vp->v_lasta = 0; 992 vp->v_cstart = 0; 993 vp->v_clen = 0; 994 vp->v_socket = 0; 995 lockdestroy(vp->v_vnlock); 996 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE); 997 KASSERT(vp->v_cleanbufcnt == 0, ("cleanbufcnt not 0")); 998 KASSERT(vp->v_cleanblkroot == NULL, ("cleanblkroot not NULL")); 999 KASSERT(vp->v_dirtybufcnt == 0, ("dirtybufcnt not 0")); 1000 KASSERT(vp->v_dirtyblkroot == NULL, ("dirtyblkroot not NULL")); 1001 } else { 1002 numvnodes++; 1003 mtx_unlock(&vnode_free_list_mtx); 1004 1005 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO); 1006 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 1007 VI_LOCK(vp); 1008 vp->v_dd = vp; 1009 vp->v_vnlock = &vp->v_lock; 1010 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE); 1011 cache_purge(vp); 1012 LIST_INIT(&vp->v_cache_src); 1013 TAILQ_INIT(&vp->v_cache_dst); 1014 } 1015 1016 TAILQ_INIT(&vp->v_cleanblkhd); 1017 TAILQ_INIT(&vp->v_dirtyblkhd); 1018 vp->v_type = VNON; 1019 vp->v_tag = tag; 1020 vp->v_op = vops; 1021 *vpp = vp; 1022 vp->v_usecount = 1; 1023 vp->v_data = 0; 1024 vp->v_cachedid = -1; 1025 VI_UNLOCK(vp); 1026 if (pollinfo != NULL) { 1027 mtx_destroy(&pollinfo->vpi_lock); 1028 uma_zfree(vnodepoll_zone, pollinfo); 1029 } 1030 #ifdef MAC 1031 mac_init_vnode(vp); 1032 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 1033 mac_associate_vnode_singlelabel(mp, vp); 1034 #endif 1035 insmntque(vp, mp); 1036 1037 return (0); 1038 } 1039 1040 /* 1041 * Move a vnode from one mount queue to another. 1042 */ 1043 static void 1044 insmntque(vp, mp) 1045 register struct vnode *vp; 1046 register struct mount *mp; 1047 { 1048 1049 mtx_lock(&mntvnode_mtx); 1050 /* 1051 * Delete from old mount point vnode list, if on one. 1052 */ 1053 if (vp->v_mount != NULL) { 1054 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0, 1055 ("bad mount point vnode list size")); 1056 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes); 1057 vp->v_mount->mnt_nvnodelistsize--; 1058 } 1059 /* 1060 * Insert into list of vnodes for the new mount point, if available. 1061 */ 1062 if ((vp->v_mount = mp) == NULL) { 1063 mtx_unlock(&mntvnode_mtx); 1064 return; 1065 } 1066 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1067 mp->mnt_nvnodelistsize++; 1068 mtx_unlock(&mntvnode_mtx); 1069 } 1070 1071 /* 1072 * Update outstanding I/O count and do wakeup if requested. 1073 */ 1074 void 1075 vwakeup(bp) 1076 register struct buf *bp; 1077 { 1078 register struct vnode *vp; 1079 1080 bp->b_flags &= ~B_WRITEINPROG; 1081 if ((vp = bp->b_vp)) { 1082 VI_LOCK(vp); 1083 vp->v_numoutput--; 1084 if (vp->v_numoutput < 0) 1085 panic("vwakeup: neg numoutput"); 1086 if ((vp->v_numoutput == 0) && (vp->v_iflag & VI_BWAIT)) { 1087 vp->v_iflag &= ~VI_BWAIT; 1088 wakeup(&vp->v_numoutput); 1089 } 1090 VI_UNLOCK(vp); 1091 } 1092 } 1093 1094 /* 1095 * Flush out and invalidate all buffers associated with a vnode. 1096 * Called with the underlying object locked. 1097 */ 1098 int 1099 vinvalbuf(vp, flags, cred, td, slpflag, slptimeo) 1100 struct vnode *vp; 1101 int flags; 1102 struct ucred *cred; 1103 struct thread *td; 1104 int slpflag, slptimeo; 1105 { 1106 struct buf *blist; 1107 int error; 1108 vm_object_t object; 1109 1110 GIANT_REQUIRED; 1111 1112 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1113 1114 VI_LOCK(vp); 1115 if (flags & V_SAVE) { 1116 while (vp->v_numoutput) { 1117 vp->v_iflag |= VI_BWAIT; 1118 error = msleep(&vp->v_numoutput, VI_MTX(vp), 1119 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo); 1120 if (error) { 1121 VI_UNLOCK(vp); 1122 return (error); 1123 } 1124 } 1125 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 1126 VI_UNLOCK(vp); 1127 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, td)) != 0) 1128 return (error); 1129 /* 1130 * XXX We could save a lock/unlock if this was only 1131 * enabled under INVARIANTS 1132 */ 1133 VI_LOCK(vp); 1134 if (vp->v_numoutput > 0 || 1135 !TAILQ_EMPTY(&vp->v_dirtyblkhd)) 1136 panic("vinvalbuf: dirty bufs"); 1137 } 1138 } 1139 /* 1140 * If you alter this loop please notice that interlock is dropped and 1141 * reacquired in flushbuflist. Special care is needed to ensure that 1142 * no race conditions occur from this. 1143 */ 1144 for (error = 0;;) { 1145 if ((blist = TAILQ_FIRST(&vp->v_cleanblkhd)) != 0 && 1146 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 1147 if (error) 1148 break; 1149 continue; 1150 } 1151 if ((blist = TAILQ_FIRST(&vp->v_dirtyblkhd)) != 0 && 1152 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 1153 if (error) 1154 break; 1155 continue; 1156 } 1157 break; 1158 } 1159 if (error) { 1160 VI_UNLOCK(vp); 1161 return (error); 1162 } 1163 1164 /* 1165 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1166 * have write I/O in-progress but if there is a VM object then the 1167 * VM object can also have read-I/O in-progress. 1168 */ 1169 do { 1170 while (vp->v_numoutput > 0) { 1171 vp->v_iflag |= VI_BWAIT; 1172 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vnvlbv", 0); 1173 } 1174 VI_UNLOCK(vp); 1175 if (VOP_GETVOBJECT(vp, &object) == 0) { 1176 VM_OBJECT_LOCK(object); 1177 vm_object_pip_wait(object, "vnvlbx"); 1178 VM_OBJECT_UNLOCK(object); 1179 } 1180 VI_LOCK(vp); 1181 } while (vp->v_numoutput > 0); 1182 VI_UNLOCK(vp); 1183 1184 /* 1185 * Destroy the copy in the VM cache, too. 1186 */ 1187 if (VOP_GETVOBJECT(vp, &object) == 0) { 1188 VM_OBJECT_LOCK(object); 1189 vm_object_page_remove(object, 0, 0, 1190 (flags & V_SAVE) ? TRUE : FALSE); 1191 VM_OBJECT_UNLOCK(object); 1192 } 1193 1194 #ifdef INVARIANTS 1195 VI_LOCK(vp); 1196 if ((flags & (V_ALT | V_NORMAL)) == 0 && 1197 (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || 1198 !TAILQ_EMPTY(&vp->v_cleanblkhd))) 1199 panic("vinvalbuf: flush failed"); 1200 VI_UNLOCK(vp); 1201 #endif 1202 return (0); 1203 } 1204 1205 /* 1206 * Flush out buffers on the specified list. 1207 * 1208 */ 1209 static int 1210 flushbuflist(blist, flags, vp, slpflag, slptimeo, errorp) 1211 struct buf *blist; 1212 int flags; 1213 struct vnode *vp; 1214 int slpflag, slptimeo; 1215 int *errorp; 1216 { 1217 struct buf *bp, *nbp; 1218 int found, error; 1219 1220 ASSERT_VI_LOCKED(vp, "flushbuflist"); 1221 1222 for (found = 0, bp = blist; bp; bp = nbp) { 1223 nbp = TAILQ_NEXT(bp, b_vnbufs); 1224 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 1225 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) { 1226 continue; 1227 } 1228 found += 1; 1229 error = BUF_TIMELOCK(bp, 1230 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, VI_MTX(vp), 1231 "flushbuf", slpflag, slptimeo); 1232 if (error) { 1233 if (error != ENOLCK) 1234 *errorp = error; 1235 goto done; 1236 } 1237 /* 1238 * XXX Since there are no node locks for NFS, I 1239 * believe there is a slight chance that a delayed 1240 * write will occur while sleeping just above, so 1241 * check for it. Note that vfs_bio_awrite expects 1242 * buffers to reside on a queue, while BUF_WRITE and 1243 * brelse do not. 1244 */ 1245 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1246 (flags & V_SAVE)) { 1247 1248 if (bp->b_vp == vp) { 1249 if (bp->b_flags & B_CLUSTEROK) { 1250 vfs_bio_awrite(bp); 1251 } else { 1252 bremfree(bp); 1253 bp->b_flags |= B_ASYNC; 1254 BUF_WRITE(bp); 1255 } 1256 } else { 1257 bremfree(bp); 1258 (void) BUF_WRITE(bp); 1259 } 1260 goto done; 1261 } 1262 bremfree(bp); 1263 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 1264 bp->b_flags &= ~B_ASYNC; 1265 brelse(bp); 1266 VI_LOCK(vp); 1267 } 1268 return (found); 1269 done: 1270 VI_LOCK(vp); 1271 return (found); 1272 } 1273 1274 /* 1275 * Truncate a file's buffer and pages to a specified length. This 1276 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1277 * sync activity. 1278 */ 1279 int 1280 vtruncbuf(vp, cred, td, length, blksize) 1281 register struct vnode *vp; 1282 struct ucred *cred; 1283 struct thread *td; 1284 off_t length; 1285 int blksize; 1286 { 1287 register struct buf *bp; 1288 struct buf *nbp; 1289 int anyfreed; 1290 int trunclbn; 1291 1292 /* 1293 * Round up to the *next* lbn. 1294 */ 1295 trunclbn = (length + blksize - 1) / blksize; 1296 1297 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 1298 restart: 1299 VI_LOCK(vp); 1300 anyfreed = 1; 1301 for (;anyfreed;) { 1302 anyfreed = 0; 1303 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 1304 nbp = TAILQ_NEXT(bp, b_vnbufs); 1305 if (bp->b_lblkno >= trunclbn) { 1306 if (BUF_LOCK(bp, 1307 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1308 VI_MTX(vp)) == ENOLCK) 1309 goto restart; 1310 1311 bremfree(bp); 1312 bp->b_flags |= (B_INVAL | B_RELBUF); 1313 bp->b_flags &= ~B_ASYNC; 1314 brelse(bp); 1315 anyfreed = 1; 1316 1317 if (nbp && 1318 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1319 (nbp->b_vp != vp) || 1320 (nbp->b_flags & B_DELWRI))) { 1321 goto restart; 1322 } 1323 VI_LOCK(vp); 1324 } 1325 } 1326 1327 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1328 nbp = TAILQ_NEXT(bp, b_vnbufs); 1329 if (bp->b_lblkno >= trunclbn) { 1330 if (BUF_LOCK(bp, 1331 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1332 VI_MTX(vp)) == ENOLCK) 1333 goto restart; 1334 bremfree(bp); 1335 bp->b_flags |= (B_INVAL | B_RELBUF); 1336 bp->b_flags &= ~B_ASYNC; 1337 brelse(bp); 1338 anyfreed = 1; 1339 if (nbp && 1340 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1341 (nbp->b_vp != vp) || 1342 (nbp->b_flags & B_DELWRI) == 0)) { 1343 goto restart; 1344 } 1345 VI_LOCK(vp); 1346 } 1347 } 1348 } 1349 1350 if (length > 0) { 1351 restartsync: 1352 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1353 nbp = TAILQ_NEXT(bp, b_vnbufs); 1354 if (bp->b_lblkno > 0) 1355 continue; 1356 /* 1357 * Since we hold the vnode lock this should only 1358 * fail if we're racing with the buf daemon. 1359 */ 1360 if (BUF_LOCK(bp, 1361 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1362 VI_MTX(vp)) == ENOLCK) { 1363 goto restart; 1364 } 1365 KASSERT((bp->b_flags & B_DELWRI), 1366 ("buf(%p) on dirty queue without DELWRI.", bp)); 1367 1368 bremfree(bp); 1369 bawrite(bp); 1370 VI_LOCK(vp); 1371 goto restartsync; 1372 } 1373 } 1374 1375 while (vp->v_numoutput > 0) { 1376 vp->v_iflag |= VI_BWAIT; 1377 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vbtrunc", 0); 1378 } 1379 VI_UNLOCK(vp); 1380 vnode_pager_setsize(vp, length); 1381 1382 return (0); 1383 } 1384 1385 /* 1386 * buf_splay() - splay tree core for the clean/dirty list of buffers in 1387 * a vnode. 1388 * 1389 * NOTE: We have to deal with the special case of a background bitmap 1390 * buffer, a situation where two buffers will have the same logical 1391 * block offset. We want (1) only the foreground buffer to be accessed 1392 * in a lookup and (2) must differentiate between the foreground and 1393 * background buffer in the splay tree algorithm because the splay 1394 * tree cannot normally handle multiple entities with the same 'index'. 1395 * We accomplish this by adding differentiating flags to the splay tree's 1396 * numerical domain. 1397 */ 1398 static 1399 struct buf * 1400 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root) 1401 { 1402 struct buf dummy; 1403 struct buf *lefttreemax, *righttreemin, *y; 1404 1405 if (root == NULL) 1406 return (NULL); 1407 lefttreemax = righttreemin = &dummy; 1408 for (;;) { 1409 if (lblkno < root->b_lblkno || 1410 (lblkno == root->b_lblkno && 1411 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1412 if ((y = root->b_left) == NULL) 1413 break; 1414 if (lblkno < y->b_lblkno) { 1415 /* Rotate right. */ 1416 root->b_left = y->b_right; 1417 y->b_right = root; 1418 root = y; 1419 if ((y = root->b_left) == NULL) 1420 break; 1421 } 1422 /* Link into the new root's right tree. */ 1423 righttreemin->b_left = root; 1424 righttreemin = root; 1425 } else if (lblkno > root->b_lblkno || 1426 (lblkno == root->b_lblkno && 1427 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) { 1428 if ((y = root->b_right) == NULL) 1429 break; 1430 if (lblkno > y->b_lblkno) { 1431 /* Rotate left. */ 1432 root->b_right = y->b_left; 1433 y->b_left = root; 1434 root = y; 1435 if ((y = root->b_right) == NULL) 1436 break; 1437 } 1438 /* Link into the new root's left tree. */ 1439 lefttreemax->b_right = root; 1440 lefttreemax = root; 1441 } else { 1442 break; 1443 } 1444 root = y; 1445 } 1446 /* Assemble the new root. */ 1447 lefttreemax->b_right = root->b_left; 1448 righttreemin->b_left = root->b_right; 1449 root->b_left = dummy.b_right; 1450 root->b_right = dummy.b_left; 1451 return (root); 1452 } 1453 1454 static 1455 void 1456 buf_vlist_remove(struct buf *bp) 1457 { 1458 struct vnode *vp = bp->b_vp; 1459 struct buf *root; 1460 1461 ASSERT_VI_LOCKED(vp, "buf_vlist_remove"); 1462 if (bp->b_xflags & BX_VNDIRTY) { 1463 if (bp != vp->v_dirtyblkroot) { 1464 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 1465 KASSERT(root == bp, ("splay lookup failed during dirty remove")); 1466 } 1467 if (bp->b_left == NULL) { 1468 root = bp->b_right; 1469 } else { 1470 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1471 root->b_right = bp->b_right; 1472 } 1473 vp->v_dirtyblkroot = root; 1474 TAILQ_REMOVE(&vp->v_dirtyblkhd, bp, b_vnbufs); 1475 vp->v_dirtybufcnt--; 1476 } else { 1477 /* KASSERT(bp->b_xflags & BX_VNCLEAN, ("bp wasn't clean")); */ 1478 if (bp != vp->v_cleanblkroot) { 1479 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 1480 KASSERT(root == bp, ("splay lookup failed during clean remove")); 1481 } 1482 if (bp->b_left == NULL) { 1483 root = bp->b_right; 1484 } else { 1485 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1486 root->b_right = bp->b_right; 1487 } 1488 vp->v_cleanblkroot = root; 1489 TAILQ_REMOVE(&vp->v_cleanblkhd, bp, b_vnbufs); 1490 vp->v_cleanbufcnt--; 1491 } 1492 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1493 } 1494 1495 /* 1496 * Add the buffer to the sorted clean or dirty block list using a 1497 * splay tree algorithm. 1498 * 1499 * NOTE: xflags is passed as a constant, optimizing this inline function! 1500 */ 1501 static 1502 void 1503 buf_vlist_add(struct buf *bp, struct vnode *vp, b_xflags_t xflags) 1504 { 1505 struct buf *root; 1506 1507 ASSERT_VI_LOCKED(vp, "buf_vlist_add"); 1508 bp->b_xflags |= xflags; 1509 if (xflags & BX_VNDIRTY) { 1510 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 1511 if (root == NULL) { 1512 bp->b_left = NULL; 1513 bp->b_right = NULL; 1514 TAILQ_INSERT_TAIL(&vp->v_dirtyblkhd, bp, b_vnbufs); 1515 } else if (bp->b_lblkno < root->b_lblkno || 1516 (bp->b_lblkno == root->b_lblkno && 1517 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1518 bp->b_left = root->b_left; 1519 bp->b_right = root; 1520 root->b_left = NULL; 1521 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 1522 } else { 1523 bp->b_right = root->b_right; 1524 bp->b_left = root; 1525 root->b_right = NULL; 1526 TAILQ_INSERT_AFTER(&vp->v_dirtyblkhd, 1527 root, bp, b_vnbufs); 1528 } 1529 vp->v_dirtybufcnt++; 1530 vp->v_dirtyblkroot = bp; 1531 } else { 1532 /* KASSERT(xflags & BX_VNCLEAN, ("xflags not clean")); */ 1533 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 1534 if (root == NULL) { 1535 bp->b_left = NULL; 1536 bp->b_right = NULL; 1537 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs); 1538 } else if (bp->b_lblkno < root->b_lblkno || 1539 (bp->b_lblkno == root->b_lblkno && 1540 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1541 bp->b_left = root->b_left; 1542 bp->b_right = root; 1543 root->b_left = NULL; 1544 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 1545 } else { 1546 bp->b_right = root->b_right; 1547 bp->b_left = root; 1548 root->b_right = NULL; 1549 TAILQ_INSERT_AFTER(&vp->v_cleanblkhd, 1550 root, bp, b_vnbufs); 1551 } 1552 vp->v_cleanbufcnt++; 1553 vp->v_cleanblkroot = bp; 1554 } 1555 } 1556 1557 /* 1558 * Lookup a buffer using the splay tree. Note that we specifically avoid 1559 * shadow buffers used in background bitmap writes. 1560 * 1561 * This code isn't quite efficient as it could be because we are maintaining 1562 * two sorted lists and do not know which list the block resides in. 1563 * 1564 * During a "make buildworld" the desired buffer is found at one of 1565 * the roots more than 60% of the time. Thus, checking both roots 1566 * before performing either splay eliminates unnecessary splays on the 1567 * first tree splayed. 1568 */ 1569 struct buf * 1570 gbincore(struct vnode *vp, daddr_t lblkno) 1571 { 1572 struct buf *bp; 1573 1574 GIANT_REQUIRED; 1575 1576 ASSERT_VI_LOCKED(vp, "gbincore"); 1577 if ((bp = vp->v_cleanblkroot) != NULL && 1578 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1579 return (bp); 1580 if ((bp = vp->v_dirtyblkroot) != NULL && 1581 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1582 return (bp); 1583 if ((bp = vp->v_cleanblkroot) != NULL) { 1584 vp->v_cleanblkroot = bp = buf_splay(lblkno, 0, bp); 1585 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1586 return (bp); 1587 } 1588 if ((bp = vp->v_dirtyblkroot) != NULL) { 1589 vp->v_dirtyblkroot = bp = buf_splay(lblkno, 0, bp); 1590 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1591 return (bp); 1592 } 1593 return (NULL); 1594 } 1595 1596 /* 1597 * Associate a buffer with a vnode. 1598 */ 1599 void 1600 bgetvp(vp, bp) 1601 register struct vnode *vp; 1602 register struct buf *bp; 1603 { 1604 KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 1605 1606 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1607 ("bgetvp: bp already attached! %p", bp)); 1608 1609 ASSERT_VI_LOCKED(vp, "bgetvp"); 1610 vholdl(vp); 1611 bp->b_vp = vp; 1612 bp->b_dev = vn_todev(vp); 1613 /* 1614 * Insert onto list for new vnode. 1615 */ 1616 buf_vlist_add(bp, vp, BX_VNCLEAN); 1617 } 1618 1619 /* 1620 * Disassociate a buffer from a vnode. 1621 */ 1622 void 1623 brelvp(bp) 1624 register struct buf *bp; 1625 { 1626 struct vnode *vp; 1627 1628 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1629 1630 /* 1631 * Delete from old vnode list, if on one. 1632 */ 1633 vp = bp->b_vp; 1634 VI_LOCK(vp); 1635 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1636 buf_vlist_remove(bp); 1637 if ((vp->v_iflag & VI_ONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 1638 vp->v_iflag &= ~VI_ONWORKLST; 1639 mtx_lock(&sync_mtx); 1640 LIST_REMOVE(vp, v_synclist); 1641 mtx_unlock(&sync_mtx); 1642 } 1643 vdropl(vp); 1644 bp->b_vp = (struct vnode *) 0; 1645 if (bp->b_object) 1646 bp->b_object = NULL; 1647 VI_UNLOCK(vp); 1648 } 1649 1650 /* 1651 * Add an item to the syncer work queue. 1652 */ 1653 static void 1654 vn_syncer_add_to_worklist(struct vnode *vp, int delay) 1655 { 1656 int slot; 1657 1658 ASSERT_VI_LOCKED(vp, "vn_syncer_add_to_worklist"); 1659 1660 mtx_lock(&sync_mtx); 1661 if (vp->v_iflag & VI_ONWORKLST) 1662 LIST_REMOVE(vp, v_synclist); 1663 else 1664 vp->v_iflag |= VI_ONWORKLST; 1665 1666 if (delay > syncer_maxdelay - 2) 1667 delay = syncer_maxdelay - 2; 1668 slot = (syncer_delayno + delay) & syncer_mask; 1669 1670 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist); 1671 mtx_unlock(&sync_mtx); 1672 } 1673 1674 struct proc *updateproc; 1675 static void sched_sync(void); 1676 static struct kproc_desc up_kp = { 1677 "syncer", 1678 sched_sync, 1679 &updateproc 1680 }; 1681 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 1682 1683 /* 1684 * System filesystem synchronizer daemon. 1685 */ 1686 static void 1687 sched_sync(void) 1688 { 1689 struct synclist *slp; 1690 struct vnode *vp; 1691 struct mount *mp; 1692 long starttime; 1693 struct thread *td = FIRST_THREAD_IN_PROC(updateproc); /* XXXKSE */ 1694 1695 mtx_lock(&Giant); 1696 1697 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, td->td_proc, 1698 SHUTDOWN_PRI_LAST); 1699 1700 for (;;) { 1701 kthread_suspend_check(td->td_proc); 1702 1703 starttime = time_second; 1704 1705 /* 1706 * Push files whose dirty time has expired. Be careful 1707 * of interrupt race on slp queue. 1708 */ 1709 mtx_lock(&sync_mtx); 1710 slp = &syncer_workitem_pending[syncer_delayno]; 1711 syncer_delayno += 1; 1712 if (syncer_delayno == syncer_maxdelay) 1713 syncer_delayno = 0; 1714 1715 while ((vp = LIST_FIRST(slp)) != NULL) { 1716 mtx_unlock(&sync_mtx); 1717 if (VOP_ISLOCKED(vp, NULL) == 0 && 1718 vn_start_write(vp, &mp, V_NOWAIT) == 0) { 1719 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1720 (void) VOP_FSYNC(vp, td->td_ucred, MNT_LAZY, td); 1721 VOP_UNLOCK(vp, 0, td); 1722 vn_finished_write(mp); 1723 } 1724 mtx_lock(&sync_mtx); 1725 if (LIST_FIRST(slp) == vp) { 1726 mtx_unlock(&sync_mtx); 1727 /* 1728 * Note: VFS vnodes can remain on the 1729 * worklist too with no dirty blocks, but 1730 * since sync_fsync() moves it to a different 1731 * slot we are safe. 1732 */ 1733 VI_LOCK(vp); 1734 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) && 1735 !vn_isdisk(vp, NULL)) { 1736 panic("sched_sync: fsync failed " 1737 "vp %p tag %s", vp, vp->v_tag); 1738 } 1739 /* 1740 * Put us back on the worklist. The worklist 1741 * routine will remove us from our current 1742 * position and then add us back in at a later 1743 * position. 1744 */ 1745 vn_syncer_add_to_worklist(vp, syncdelay); 1746 VI_UNLOCK(vp); 1747 mtx_lock(&sync_mtx); 1748 } 1749 } 1750 mtx_unlock(&sync_mtx); 1751 1752 /* 1753 * Do soft update processing. 1754 */ 1755 if (softdep_process_worklist_hook != NULL) 1756 (*softdep_process_worklist_hook)(NULL); 1757 1758 /* 1759 * The variable rushjob allows the kernel to speed up the 1760 * processing of the filesystem syncer process. A rushjob 1761 * value of N tells the filesystem syncer to process the next 1762 * N seconds worth of work on its queue ASAP. Currently rushjob 1763 * is used by the soft update code to speed up the filesystem 1764 * syncer process when the incore state is getting so far 1765 * ahead of the disk that the kernel memory pool is being 1766 * threatened with exhaustion. 1767 */ 1768 mtx_lock(&sync_mtx); 1769 if (rushjob > 0) { 1770 rushjob -= 1; 1771 mtx_unlock(&sync_mtx); 1772 continue; 1773 } 1774 mtx_unlock(&sync_mtx); 1775 /* 1776 * If it has taken us less than a second to process the 1777 * current work, then wait. Otherwise start right over 1778 * again. We can still lose time if any single round 1779 * takes more than two seconds, but it does not really 1780 * matter as we are just trying to generally pace the 1781 * filesystem activity. 1782 */ 1783 if (time_second == starttime) 1784 tsleep(&lbolt, PPAUSE, "syncer", 0); 1785 } 1786 } 1787 1788 /* 1789 * Request the syncer daemon to speed up its work. 1790 * We never push it to speed up more than half of its 1791 * normal turn time, otherwise it could take over the cpu. 1792 * XXXKSE only one update? 1793 */ 1794 int 1795 speedup_syncer() 1796 { 1797 struct thread *td; 1798 int ret = 0; 1799 1800 td = FIRST_THREAD_IN_PROC(updateproc); 1801 mtx_lock_spin(&sched_lock); 1802 if (td->td_wchan == &lbolt) { 1803 unsleep(td); 1804 TD_CLR_SLEEPING(td); 1805 setrunnable(td); 1806 } 1807 mtx_unlock_spin(&sched_lock); 1808 mtx_lock(&sync_mtx); 1809 if (rushjob < syncdelay / 2) { 1810 rushjob += 1; 1811 stat_rush_requests += 1; 1812 ret = 1; 1813 } 1814 mtx_unlock(&sync_mtx); 1815 return (ret); 1816 } 1817 1818 /* 1819 * Associate a p-buffer with a vnode. 1820 * 1821 * Also sets B_PAGING flag to indicate that vnode is not fully associated 1822 * with the buffer. i.e. the bp has not been linked into the vnode or 1823 * ref-counted. 1824 */ 1825 void 1826 pbgetvp(vp, bp) 1827 register struct vnode *vp; 1828 register struct buf *bp; 1829 { 1830 1831 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 1832 1833 bp->b_vp = vp; 1834 bp->b_flags |= B_PAGING; 1835 bp->b_dev = vn_todev(vp); 1836 } 1837 1838 /* 1839 * Disassociate a p-buffer from a vnode. 1840 */ 1841 void 1842 pbrelvp(bp) 1843 register struct buf *bp; 1844 { 1845 1846 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 1847 1848 /* XXX REMOVE ME */ 1849 VI_LOCK(bp->b_vp); 1850 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) { 1851 panic( 1852 "relpbuf(): b_vp was probably reassignbuf()d %p %x", 1853 bp, 1854 (int)bp->b_flags 1855 ); 1856 } 1857 VI_UNLOCK(bp->b_vp); 1858 bp->b_vp = (struct vnode *) 0; 1859 bp->b_flags &= ~B_PAGING; 1860 } 1861 1862 /* 1863 * Reassign a buffer from one vnode to another. 1864 * Used to assign file specific control information 1865 * (indirect blocks) to the vnode to which they belong. 1866 */ 1867 void 1868 reassignbuf(bp, newvp) 1869 register struct buf *bp; 1870 register struct vnode *newvp; 1871 { 1872 struct vnode *vp; 1873 int delay; 1874 1875 if (newvp == NULL) { 1876 printf("reassignbuf: NULL"); 1877 return; 1878 } 1879 vp = bp->b_vp; 1880 ++reassignbufcalls; 1881 1882 /* 1883 * B_PAGING flagged buffers cannot be reassigned because their vp 1884 * is not fully linked in. 1885 */ 1886 if (bp->b_flags & B_PAGING) 1887 panic("cannot reassign paging buffer"); 1888 1889 /* 1890 * Delete from old vnode list, if on one. 1891 */ 1892 VI_LOCK(vp); 1893 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) { 1894 buf_vlist_remove(bp); 1895 if (vp != newvp) { 1896 vdropl(bp->b_vp); 1897 bp->b_vp = NULL; /* for clarification */ 1898 } 1899 } 1900 if (vp != newvp) { 1901 VI_UNLOCK(vp); 1902 VI_LOCK(newvp); 1903 } 1904 /* 1905 * If dirty, put on list of dirty buffers; otherwise insert onto list 1906 * of clean buffers. 1907 */ 1908 if (bp->b_flags & B_DELWRI) { 1909 if ((newvp->v_iflag & VI_ONWORKLST) == 0) { 1910 switch (newvp->v_type) { 1911 case VDIR: 1912 delay = dirdelay; 1913 break; 1914 case VCHR: 1915 if (newvp->v_rdev->si_mountpoint != NULL) { 1916 delay = metadelay; 1917 break; 1918 } 1919 /* FALLTHROUGH */ 1920 default: 1921 delay = filedelay; 1922 } 1923 vn_syncer_add_to_worklist(newvp, delay); 1924 } 1925 buf_vlist_add(bp, newvp, BX_VNDIRTY); 1926 } else { 1927 buf_vlist_add(bp, newvp, BX_VNCLEAN); 1928 1929 if ((newvp->v_iflag & VI_ONWORKLST) && 1930 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) { 1931 mtx_lock(&sync_mtx); 1932 LIST_REMOVE(newvp, v_synclist); 1933 mtx_unlock(&sync_mtx); 1934 newvp->v_iflag &= ~VI_ONWORKLST; 1935 } 1936 } 1937 if (bp->b_vp != newvp) { 1938 bp->b_vp = newvp; 1939 vholdl(bp->b_vp); 1940 } 1941 VI_UNLOCK(newvp); 1942 } 1943 1944 /* 1945 * Create a vnode for a device. 1946 * Used for mounting the root filesystem. 1947 */ 1948 int 1949 bdevvp(dev, vpp) 1950 dev_t dev; 1951 struct vnode **vpp; 1952 { 1953 register struct vnode *vp; 1954 struct vnode *nvp; 1955 int error; 1956 1957 if (dev == NODEV) { 1958 *vpp = NULLVP; 1959 return (ENXIO); 1960 } 1961 if (vfinddev(dev, VCHR, vpp)) 1962 return (0); 1963 error = getnewvnode("none", (struct mount *)0, spec_vnodeop_p, &nvp); 1964 if (error) { 1965 *vpp = NULLVP; 1966 return (error); 1967 } 1968 vp = nvp; 1969 vp->v_type = VCHR; 1970 addalias(vp, dev); 1971 *vpp = vp; 1972 return (0); 1973 } 1974 1975 static void 1976 v_incr_usecount(struct vnode *vp, int delta) 1977 { 1978 vp->v_usecount += delta; 1979 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 1980 mtx_lock(&spechash_mtx); 1981 vp->v_rdev->si_usecount += delta; 1982 mtx_unlock(&spechash_mtx); 1983 } 1984 } 1985 1986 /* 1987 * Add vnode to the alias list hung off the dev_t. 1988 * 1989 * The reason for this gunk is that multiple vnodes can reference 1990 * the same physical device, so checking vp->v_usecount to see 1991 * how many users there are is inadequate; the v_usecount for 1992 * the vnodes need to be accumulated. vcount() does that. 1993 */ 1994 struct vnode * 1995 addaliasu(nvp, nvp_rdev) 1996 struct vnode *nvp; 1997 udev_t nvp_rdev; 1998 { 1999 struct vnode *ovp; 2000 vop_t **ops; 2001 dev_t dev; 2002 2003 if (nvp->v_type == VBLK) 2004 return (nvp); 2005 if (nvp->v_type != VCHR) 2006 panic("addaliasu on non-special vnode"); 2007 dev = udev2dev(nvp_rdev, 0); 2008 /* 2009 * Check to see if we have a bdevvp vnode with no associated 2010 * filesystem. If so, we want to associate the filesystem of 2011 * the new newly instigated vnode with the bdevvp vnode and 2012 * discard the newly created vnode rather than leaving the 2013 * bdevvp vnode lying around with no associated filesystem. 2014 */ 2015 if (vfinddev(dev, nvp->v_type, &ovp) == 0 || ovp->v_data != NULL) { 2016 addalias(nvp, dev); 2017 return (nvp); 2018 } 2019 /* 2020 * Discard unneeded vnode, but save its node specific data. 2021 * Note that if there is a lock, it is carried over in the 2022 * node specific data to the replacement vnode. 2023 */ 2024 vref(ovp); 2025 ovp->v_data = nvp->v_data; 2026 ovp->v_tag = nvp->v_tag; 2027 nvp->v_data = NULL; 2028 lockdestroy(ovp->v_vnlock); 2029 lockinit(ovp->v_vnlock, PVFS, nvp->v_vnlock->lk_wmesg, 2030 nvp->v_vnlock->lk_timo, nvp->v_vnlock->lk_flags & LK_EXTFLG_MASK); 2031 ops = ovp->v_op; 2032 ovp->v_op = nvp->v_op; 2033 if (VOP_ISLOCKED(nvp, curthread)) { 2034 VOP_UNLOCK(nvp, 0, curthread); 2035 vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, curthread); 2036 } 2037 nvp->v_op = ops; 2038 insmntque(ovp, nvp->v_mount); 2039 vrele(nvp); 2040 vgone(nvp); 2041 return (ovp); 2042 } 2043 2044 /* This is a local helper function that do the same as addaliasu, but for a 2045 * dev_t instead of an udev_t. */ 2046 static void 2047 addalias(nvp, dev) 2048 struct vnode *nvp; 2049 dev_t dev; 2050 { 2051 2052 KASSERT(nvp->v_type == VCHR, ("addalias on non-special vnode")); 2053 nvp->v_rdev = dev; 2054 VI_LOCK(nvp); 2055 mtx_lock(&spechash_mtx); 2056 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext); 2057 dev->si_usecount += nvp->v_usecount; 2058 mtx_unlock(&spechash_mtx); 2059 VI_UNLOCK(nvp); 2060 } 2061 2062 /* 2063 * Grab a particular vnode from the free list, increment its 2064 * reference count and lock it. The vnode lock bit is set if the 2065 * vnode is being eliminated in vgone. The process is awakened 2066 * when the transition is completed, and an error returned to 2067 * indicate that the vnode is no longer usable (possibly having 2068 * been changed to a new filesystem type). 2069 */ 2070 int 2071 vget(vp, flags, td) 2072 register struct vnode *vp; 2073 int flags; 2074 struct thread *td; 2075 { 2076 int error; 2077 2078 /* 2079 * If the vnode is in the process of being cleaned out for 2080 * another use, we wait for the cleaning to finish and then 2081 * return failure. Cleaning is determined by checking that 2082 * the VI_XLOCK flag is set. 2083 */ 2084 if ((flags & LK_INTERLOCK) == 0) 2085 VI_LOCK(vp); 2086 if (vp->v_iflag & VI_XLOCK && vp->v_vxproc != curthread) { 2087 vp->v_iflag |= VI_XWANT; 2088 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vget", 0); 2089 return (ENOENT); 2090 } 2091 2092 v_incr_usecount(vp, 1); 2093 2094 if (VSHOULDBUSY(vp)) 2095 vbusy(vp); 2096 if (flags & LK_TYPE_MASK) { 2097 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) { 2098 /* 2099 * must expand vrele here because we do not want 2100 * to call VOP_INACTIVE if the reference count 2101 * drops back to zero since it was never really 2102 * active. We must remove it from the free list 2103 * before sleeping so that multiple processes do 2104 * not try to recycle it. 2105 */ 2106 VI_LOCK(vp); 2107 v_incr_usecount(vp, -1); 2108 if (VSHOULDFREE(vp)) 2109 vfree(vp); 2110 else 2111 vlruvp(vp); 2112 VI_UNLOCK(vp); 2113 } 2114 return (error); 2115 } 2116 VI_UNLOCK(vp); 2117 return (0); 2118 } 2119 2120 /* 2121 * Increase the reference count of a vnode. 2122 */ 2123 void 2124 vref(struct vnode *vp) 2125 { 2126 VI_LOCK(vp); 2127 v_incr_usecount(vp, 1); 2128 VI_UNLOCK(vp); 2129 } 2130 2131 /* 2132 * Return reference count of a vnode. 2133 * 2134 * The results of this call are only guaranteed when some mechanism other 2135 * than the VI lock is used to stop other processes from gaining references 2136 * to the vnode. This may be the case if the caller holds the only reference. 2137 * This is also useful when stale data is acceptable as race conditions may 2138 * be accounted for by some other means. 2139 */ 2140 int 2141 vrefcnt(struct vnode *vp) 2142 { 2143 int usecnt; 2144 2145 VI_LOCK(vp); 2146 usecnt = vp->v_usecount; 2147 VI_UNLOCK(vp); 2148 2149 return (usecnt); 2150 } 2151 2152 2153 /* 2154 * Vnode put/release. 2155 * If count drops to zero, call inactive routine and return to freelist. 2156 */ 2157 void 2158 vrele(vp) 2159 struct vnode *vp; 2160 { 2161 struct thread *td = curthread; /* XXX */ 2162 2163 KASSERT(vp != NULL, ("vrele: null vp")); 2164 2165 VI_LOCK(vp); 2166 2167 /* Skip this v_writecount check if we're going to panic below. */ 2168 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2169 ("vrele: missed vn_close")); 2170 2171 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2172 vp->v_usecount == 1)) { 2173 v_incr_usecount(vp, -1); 2174 VI_UNLOCK(vp); 2175 2176 return; 2177 } 2178 2179 if (vp->v_usecount == 1) { 2180 v_incr_usecount(vp, -1); 2181 /* 2182 * We must call VOP_INACTIVE with the node locked. Mark 2183 * as VI_DOINGINACT to avoid recursion. 2184 */ 2185 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) { 2186 VI_LOCK(vp); 2187 vp->v_iflag |= VI_DOINGINACT; 2188 VI_UNLOCK(vp); 2189 VOP_INACTIVE(vp, td); 2190 VI_LOCK(vp); 2191 KASSERT(vp->v_iflag & VI_DOINGINACT, 2192 ("vrele: lost VI_DOINGINACT")); 2193 vp->v_iflag &= ~VI_DOINGINACT; 2194 } else 2195 VI_LOCK(vp); 2196 if (VSHOULDFREE(vp)) 2197 vfree(vp); 2198 else 2199 vlruvp(vp); 2200 VI_UNLOCK(vp); 2201 2202 } else { 2203 #ifdef DIAGNOSTIC 2204 vprint("vrele: negative ref count", vp); 2205 #endif 2206 VI_UNLOCK(vp); 2207 panic("vrele: negative ref cnt"); 2208 } 2209 } 2210 2211 /* 2212 * Release an already locked vnode. This give the same effects as 2213 * unlock+vrele(), but takes less time and avoids releasing and 2214 * re-aquiring the lock (as vrele() aquires the lock internally.) 2215 */ 2216 void 2217 vput(vp) 2218 struct vnode *vp; 2219 { 2220 struct thread *td = curthread; /* XXX */ 2221 2222 GIANT_REQUIRED; 2223 2224 KASSERT(vp != NULL, ("vput: null vp")); 2225 VI_LOCK(vp); 2226 /* Skip this v_writecount check if we're going to panic below. */ 2227 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2228 ("vput: missed vn_close")); 2229 2230 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2231 vp->v_usecount == 1)) { 2232 v_incr_usecount(vp, -1); 2233 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2234 return; 2235 } 2236 2237 if (vp->v_usecount == 1) { 2238 v_incr_usecount(vp, -1); 2239 /* 2240 * We must call VOP_INACTIVE with the node locked, so 2241 * we just need to release the vnode mutex. Mark as 2242 * as VI_DOINGINACT to avoid recursion. 2243 */ 2244 vp->v_iflag |= VI_DOINGINACT; 2245 VI_UNLOCK(vp); 2246 VOP_INACTIVE(vp, td); 2247 VI_LOCK(vp); 2248 KASSERT(vp->v_iflag & VI_DOINGINACT, 2249 ("vput: lost VI_DOINGINACT")); 2250 vp->v_iflag &= ~VI_DOINGINACT; 2251 if (VSHOULDFREE(vp)) 2252 vfree(vp); 2253 else 2254 vlruvp(vp); 2255 VI_UNLOCK(vp); 2256 2257 } else { 2258 #ifdef DIAGNOSTIC 2259 vprint("vput: negative ref count", vp); 2260 #endif 2261 panic("vput: negative ref cnt"); 2262 } 2263 } 2264 2265 /* 2266 * Somebody doesn't want the vnode recycled. 2267 */ 2268 void 2269 vhold(struct vnode *vp) 2270 { 2271 VI_LOCK(vp); 2272 vholdl(vp); 2273 VI_UNLOCK(vp); 2274 } 2275 2276 void 2277 vholdl(vp) 2278 register struct vnode *vp; 2279 { 2280 vp->v_holdcnt++; 2281 if (VSHOULDBUSY(vp)) 2282 vbusy(vp); 2283 } 2284 2285 /* 2286 * Note that there is one less who cares about this vnode. vdrop() is the 2287 * opposite of vhold(). 2288 */ 2289 void 2290 vdrop(struct vnode *vp) 2291 { 2292 VI_LOCK(vp); 2293 vdropl(vp); 2294 VI_UNLOCK(vp); 2295 } 2296 2297 void 2298 vdropl(vp) 2299 register struct vnode *vp; 2300 { 2301 if (vp->v_holdcnt <= 0) 2302 panic("vdrop: holdcnt"); 2303 vp->v_holdcnt--; 2304 if (VSHOULDFREE(vp)) 2305 vfree(vp); 2306 else 2307 vlruvp(vp); 2308 } 2309 2310 /* 2311 * Remove any vnodes in the vnode table belonging to mount point mp. 2312 * 2313 * If FORCECLOSE is not specified, there should not be any active ones, 2314 * return error if any are found (nb: this is a user error, not a 2315 * system error). If FORCECLOSE is specified, detach any active vnodes 2316 * that are found. 2317 * 2318 * If WRITECLOSE is set, only flush out regular file vnodes open for 2319 * writing. 2320 * 2321 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2322 * 2323 * `rootrefs' specifies the base reference count for the root vnode 2324 * of this filesystem. The root vnode is considered busy if its 2325 * v_usecount exceeds this value. On a successful return, vflush() 2326 * will call vrele() on the root vnode exactly rootrefs times. 2327 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2328 * be zero. 2329 */ 2330 #ifdef DIAGNOSTIC 2331 static int busyprt = 0; /* print out busy vnodes */ 2332 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 2333 #endif 2334 2335 int 2336 vflush(mp, rootrefs, flags) 2337 struct mount *mp; 2338 int rootrefs; 2339 int flags; 2340 { 2341 struct thread *td = curthread; /* XXX */ 2342 struct vnode *vp, *nvp, *rootvp = NULL; 2343 struct vattr vattr; 2344 int busy = 0, error; 2345 2346 if (rootrefs > 0) { 2347 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2348 ("vflush: bad args")); 2349 /* 2350 * Get the filesystem root vnode. We can vput() it 2351 * immediately, since with rootrefs > 0, it won't go away. 2352 */ 2353 if ((error = VFS_ROOT(mp, &rootvp)) != 0) 2354 return (error); 2355 vput(rootvp); 2356 2357 } 2358 mtx_lock(&mntvnode_mtx); 2359 loop: 2360 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) { 2361 /* 2362 * Make sure this vnode wasn't reclaimed in getnewvnode(). 2363 * Start over if it has (it won't be on the list anymore). 2364 */ 2365 if (vp->v_mount != mp) 2366 goto loop; 2367 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 2368 2369 VI_LOCK(vp); 2370 mtx_unlock(&mntvnode_mtx); 2371 vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, td); 2372 /* 2373 * This vnode could have been reclaimed while we were 2374 * waiting for the lock since we are not holding a 2375 * reference. 2376 * Start over if the vnode was reclaimed. 2377 */ 2378 if (vp->v_mount != mp) { 2379 VOP_UNLOCK(vp, 0, td); 2380 mtx_lock(&mntvnode_mtx); 2381 goto loop; 2382 } 2383 /* 2384 * Skip over a vnodes marked VV_SYSTEM. 2385 */ 2386 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2387 VOP_UNLOCK(vp, 0, td); 2388 mtx_lock(&mntvnode_mtx); 2389 continue; 2390 } 2391 /* 2392 * If WRITECLOSE is set, flush out unlinked but still open 2393 * files (even if open only for reading) and regular file 2394 * vnodes open for writing. 2395 */ 2396 if (flags & WRITECLOSE) { 2397 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td); 2398 VI_LOCK(vp); 2399 2400 if ((vp->v_type == VNON || 2401 (error == 0 && vattr.va_nlink > 0)) && 2402 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2403 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2404 mtx_lock(&mntvnode_mtx); 2405 continue; 2406 } 2407 } else 2408 VI_LOCK(vp); 2409 2410 VOP_UNLOCK(vp, 0, td); 2411 2412 /* 2413 * With v_usecount == 0, all we need to do is clear out the 2414 * vnode data structures and we are done. 2415 */ 2416 if (vp->v_usecount == 0) { 2417 vgonel(vp, td); 2418 mtx_lock(&mntvnode_mtx); 2419 continue; 2420 } 2421 2422 /* 2423 * If FORCECLOSE is set, forcibly close the vnode. For block 2424 * or character devices, revert to an anonymous device. For 2425 * all other files, just kill them. 2426 */ 2427 if (flags & FORCECLOSE) { 2428 if (vp->v_type != VCHR) { 2429 vgonel(vp, td); 2430 } else { 2431 vclean(vp, 0, td); 2432 VI_UNLOCK(vp); 2433 vp->v_op = spec_vnodeop_p; 2434 insmntque(vp, (struct mount *) 0); 2435 } 2436 mtx_lock(&mntvnode_mtx); 2437 continue; 2438 } 2439 #ifdef DIAGNOSTIC 2440 if (busyprt) 2441 vprint("vflush: busy vnode", vp); 2442 #endif 2443 VI_UNLOCK(vp); 2444 mtx_lock(&mntvnode_mtx); 2445 busy++; 2446 } 2447 mtx_unlock(&mntvnode_mtx); 2448 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2449 /* 2450 * If just the root vnode is busy, and if its refcount 2451 * is equal to `rootrefs', then go ahead and kill it. 2452 */ 2453 VI_LOCK(rootvp); 2454 KASSERT(busy > 0, ("vflush: not busy")); 2455 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs")); 2456 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2457 vgonel(rootvp, td); 2458 busy = 0; 2459 } else 2460 VI_UNLOCK(rootvp); 2461 } 2462 if (busy) 2463 return (EBUSY); 2464 for (; rootrefs > 0; rootrefs--) 2465 vrele(rootvp); 2466 return (0); 2467 } 2468 2469 /* 2470 * This moves a now (likely recyclable) vnode to the end of the 2471 * mountlist. XXX However, it is temporarily disabled until we 2472 * can clean up ffs_sync() and friends, which have loop restart 2473 * conditions which this code causes to operate O(N^2). 2474 */ 2475 static void 2476 vlruvp(struct vnode *vp) 2477 { 2478 #if 0 2479 struct mount *mp; 2480 2481 if ((mp = vp->v_mount) != NULL) { 2482 mtx_lock(&mntvnode_mtx); 2483 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2484 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2485 mtx_unlock(&mntvnode_mtx); 2486 } 2487 #endif 2488 } 2489 2490 /* 2491 * Disassociate the underlying filesystem from a vnode. 2492 */ 2493 static void 2494 vclean(vp, flags, td) 2495 struct vnode *vp; 2496 int flags; 2497 struct thread *td; 2498 { 2499 int active; 2500 2501 ASSERT_VI_LOCKED(vp, "vclean"); 2502 /* 2503 * Check to see if the vnode is in use. If so we have to reference it 2504 * before we clean it out so that its count cannot fall to zero and 2505 * generate a race against ourselves to recycle it. 2506 */ 2507 if ((active = vp->v_usecount)) 2508 v_incr_usecount(vp, 1); 2509 2510 /* 2511 * Prevent the vnode from being recycled or brought into use while we 2512 * clean it out. 2513 */ 2514 if (vp->v_iflag & VI_XLOCK) 2515 panic("vclean: deadlock"); 2516 vp->v_iflag |= VI_XLOCK; 2517 vp->v_vxproc = curthread; 2518 /* 2519 * Even if the count is zero, the VOP_INACTIVE routine may still 2520 * have the object locked while it cleans it out. The VOP_LOCK 2521 * ensures that the VOP_INACTIVE routine is done with its work. 2522 * For active vnodes, it ensures that no other activity can 2523 * occur while the underlying object is being cleaned out. 2524 */ 2525 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 2526 2527 /* 2528 * Clean out any buffers associated with the vnode. 2529 * If the flush fails, just toss the buffers. 2530 */ 2531 if (flags & DOCLOSE) { 2532 struct buf *bp; 2533 VI_LOCK(vp); 2534 bp = TAILQ_FIRST(&vp->v_dirtyblkhd); 2535 VI_UNLOCK(vp); 2536 if (bp != NULL) 2537 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 2538 if (vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0) != 0) 2539 vinvalbuf(vp, 0, NOCRED, td, 0, 0); 2540 } 2541 2542 VOP_DESTROYVOBJECT(vp); 2543 2544 /* 2545 * Any other processes trying to obtain this lock must first 2546 * wait for VXLOCK to clear, then call the new lock operation. 2547 */ 2548 VOP_UNLOCK(vp, 0, td); 2549 2550 /* 2551 * If purging an active vnode, it must be closed and 2552 * deactivated before being reclaimed. Note that the 2553 * VOP_INACTIVE will unlock the vnode. 2554 */ 2555 if (active) { 2556 if (flags & DOCLOSE) 2557 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2558 VI_LOCK(vp); 2559 if ((vp->v_iflag & VI_DOINGINACT) == 0) { 2560 vp->v_iflag |= VI_DOINGINACT; 2561 VI_UNLOCK(vp); 2562 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0) 2563 panic("vclean: cannot relock."); 2564 VOP_INACTIVE(vp, td); 2565 VI_LOCK(vp); 2566 KASSERT(vp->v_iflag & VI_DOINGINACT, 2567 ("vclean: lost VI_DOINGINACT")); 2568 vp->v_iflag &= ~VI_DOINGINACT; 2569 } 2570 VI_UNLOCK(vp); 2571 } 2572 2573 /* 2574 * Reclaim the vnode. 2575 */ 2576 if (VOP_RECLAIM(vp, td)) 2577 panic("vclean: cannot reclaim"); 2578 2579 if (active) { 2580 /* 2581 * Inline copy of vrele() since VOP_INACTIVE 2582 * has already been called. 2583 */ 2584 VI_LOCK(vp); 2585 v_incr_usecount(vp, -1); 2586 if (vp->v_usecount <= 0) { 2587 #ifdef INVARIANTS 2588 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 2589 vprint("vclean: bad ref count", vp); 2590 panic("vclean: ref cnt"); 2591 } 2592 #endif 2593 if (VSHOULDFREE(vp)) 2594 vfree(vp); 2595 } 2596 VI_UNLOCK(vp); 2597 } 2598 2599 cache_purge(vp); 2600 VI_LOCK(vp); 2601 if (VSHOULDFREE(vp)) 2602 vfree(vp); 2603 2604 /* 2605 * Done with purge, reset to the standard lock and 2606 * notify sleepers of the grim news. 2607 */ 2608 vp->v_vnlock = &vp->v_lock; 2609 vp->v_op = dead_vnodeop_p; 2610 if (vp->v_pollinfo != NULL) 2611 vn_pollgone(vp); 2612 vp->v_tag = "none"; 2613 vp->v_iflag &= ~VI_XLOCK; 2614 vp->v_vxproc = NULL; 2615 if (vp->v_iflag & VI_XWANT) { 2616 vp->v_iflag &= ~VI_XWANT; 2617 wakeup(vp); 2618 } 2619 } 2620 2621 /* 2622 * Eliminate all activity associated with the requested vnode 2623 * and with all vnodes aliased to the requested vnode. 2624 */ 2625 int 2626 vop_revoke(ap) 2627 struct vop_revoke_args /* { 2628 struct vnode *a_vp; 2629 int a_flags; 2630 } */ *ap; 2631 { 2632 struct vnode *vp, *vq; 2633 dev_t dev; 2634 2635 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke")); 2636 vp = ap->a_vp; 2637 KASSERT((vp->v_type == VCHR), ("vop_revoke: not VCHR")); 2638 2639 VI_LOCK(vp); 2640 /* 2641 * If a vgone (or vclean) is already in progress, 2642 * wait until it is done and return. 2643 */ 2644 if (vp->v_iflag & VI_XLOCK) { 2645 vp->v_iflag |= VI_XWANT; 2646 msleep(vp, VI_MTX(vp), PINOD | PDROP, 2647 "vop_revokeall", 0); 2648 return (0); 2649 } 2650 VI_UNLOCK(vp); 2651 dev = vp->v_rdev; 2652 for (;;) { 2653 mtx_lock(&spechash_mtx); 2654 vq = SLIST_FIRST(&dev->si_hlist); 2655 mtx_unlock(&spechash_mtx); 2656 if (!vq) 2657 break; 2658 vgone(vq); 2659 } 2660 return (0); 2661 } 2662 2663 /* 2664 * Recycle an unused vnode to the front of the free list. 2665 * Release the passed interlock if the vnode will be recycled. 2666 */ 2667 int 2668 vrecycle(vp, inter_lkp, td) 2669 struct vnode *vp; 2670 struct mtx *inter_lkp; 2671 struct thread *td; 2672 { 2673 2674 VI_LOCK(vp); 2675 if (vp->v_usecount == 0) { 2676 if (inter_lkp) { 2677 mtx_unlock(inter_lkp); 2678 } 2679 vgonel(vp, td); 2680 return (1); 2681 } 2682 VI_UNLOCK(vp); 2683 return (0); 2684 } 2685 2686 /* 2687 * Eliminate all activity associated with a vnode 2688 * in preparation for reuse. 2689 */ 2690 void 2691 vgone(vp) 2692 register struct vnode *vp; 2693 { 2694 struct thread *td = curthread; /* XXX */ 2695 2696 VI_LOCK(vp); 2697 vgonel(vp, td); 2698 } 2699 2700 /* 2701 * vgone, with the vp interlock held. 2702 */ 2703 void 2704 vgonel(vp, td) 2705 struct vnode *vp; 2706 struct thread *td; 2707 { 2708 /* 2709 * If a vgone (or vclean) is already in progress, 2710 * wait until it is done and return. 2711 */ 2712 ASSERT_VI_LOCKED(vp, "vgonel"); 2713 if (vp->v_iflag & VI_XLOCK) { 2714 vp->v_iflag |= VI_XWANT; 2715 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vgone", 0); 2716 return; 2717 } 2718 2719 /* 2720 * Clean out the filesystem specific data. 2721 */ 2722 vclean(vp, DOCLOSE, td); 2723 VI_UNLOCK(vp); 2724 2725 /* 2726 * Delete from old mount point vnode list, if on one. 2727 */ 2728 if (vp->v_mount != NULL) 2729 insmntque(vp, (struct mount *)0); 2730 /* 2731 * If special device, remove it from special device alias list 2732 * if it is on one. 2733 */ 2734 if (vp->v_type == VCHR && vp->v_rdev != NULL && vp->v_rdev != NODEV) { 2735 VI_LOCK(vp); 2736 mtx_lock(&spechash_mtx); 2737 SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext); 2738 vp->v_rdev->si_usecount -= vp->v_usecount; 2739 mtx_unlock(&spechash_mtx); 2740 VI_UNLOCK(vp); 2741 vp->v_rdev = NULL; 2742 } 2743 2744 /* 2745 * If it is on the freelist and not already at the head, 2746 * move it to the head of the list. The test of the 2747 * VDOOMED flag and the reference count of zero is because 2748 * it will be removed from the free list by getnewvnode, 2749 * but will not have its reference count incremented until 2750 * after calling vgone. If the reference count were 2751 * incremented first, vgone would (incorrectly) try to 2752 * close the previous instance of the underlying object. 2753 */ 2754 VI_LOCK(vp); 2755 if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) { 2756 mtx_lock(&vnode_free_list_mtx); 2757 if (vp->v_iflag & VI_FREE) { 2758 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2759 } else { 2760 vp->v_iflag |= VI_FREE; 2761 freevnodes++; 2762 } 2763 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2764 mtx_unlock(&vnode_free_list_mtx); 2765 } 2766 2767 vp->v_type = VBAD; 2768 VI_UNLOCK(vp); 2769 } 2770 2771 /* 2772 * Lookup a vnode by device number. 2773 */ 2774 int 2775 vfinddev(dev, type, vpp) 2776 dev_t dev; 2777 enum vtype type; 2778 struct vnode **vpp; 2779 { 2780 struct vnode *vp; 2781 2782 mtx_lock(&spechash_mtx); 2783 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) { 2784 if (type == vp->v_type) { 2785 *vpp = vp; 2786 mtx_unlock(&spechash_mtx); 2787 return (1); 2788 } 2789 } 2790 mtx_unlock(&spechash_mtx); 2791 return (0); 2792 } 2793 2794 /* 2795 * Calculate the total number of references to a special device. 2796 */ 2797 int 2798 vcount(vp) 2799 struct vnode *vp; 2800 { 2801 int count; 2802 2803 mtx_lock(&spechash_mtx); 2804 count = vp->v_rdev->si_usecount; 2805 mtx_unlock(&spechash_mtx); 2806 return (count); 2807 } 2808 2809 /* 2810 * Same as above, but using the dev_t as argument 2811 */ 2812 int 2813 count_dev(dev) 2814 dev_t dev; 2815 { 2816 struct vnode *vp; 2817 2818 vp = SLIST_FIRST(&dev->si_hlist); 2819 if (vp == NULL) 2820 return (0); 2821 return(vcount(vp)); 2822 } 2823 2824 /* 2825 * Print out a description of a vnode. 2826 */ 2827 static char *typename[] = 2828 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 2829 2830 void 2831 vprint(label, vp) 2832 char *label; 2833 struct vnode *vp; 2834 { 2835 char buf[96]; 2836 2837 if (label != NULL) 2838 printf("%s: %p: ", label, (void *)vp); 2839 else 2840 printf("%p: ", (void *)vp); 2841 printf("tag %s, type %s, usecount %d, writecount %d, refcount %d,", 2842 vp->v_tag, typename[vp->v_type], vp->v_usecount, 2843 vp->v_writecount, vp->v_holdcnt); 2844 buf[0] = '\0'; 2845 if (vp->v_vflag & VV_ROOT) 2846 strcat(buf, "|VV_ROOT"); 2847 if (vp->v_vflag & VV_TEXT) 2848 strcat(buf, "|VV_TEXT"); 2849 if (vp->v_vflag & VV_SYSTEM) 2850 strcat(buf, "|VV_SYSTEM"); 2851 if (vp->v_iflag & VI_XLOCK) 2852 strcat(buf, "|VI_XLOCK"); 2853 if (vp->v_iflag & VI_XWANT) 2854 strcat(buf, "|VI_XWANT"); 2855 if (vp->v_iflag & VI_BWAIT) 2856 strcat(buf, "|VI_BWAIT"); 2857 if (vp->v_iflag & VI_DOOMED) 2858 strcat(buf, "|VI_DOOMED"); 2859 if (vp->v_iflag & VI_FREE) 2860 strcat(buf, "|VI_FREE"); 2861 if (vp->v_vflag & VV_OBJBUF) 2862 strcat(buf, "|VV_OBJBUF"); 2863 if (buf[0] != '\0') 2864 printf(" flags (%s),", &buf[1]); 2865 lockmgr_printinfo(vp->v_vnlock); 2866 printf("\n"); 2867 if (vp->v_data != NULL) 2868 VOP_PRINT(vp); 2869 } 2870 2871 #ifdef DDB 2872 #include <ddb/ddb.h> 2873 /* 2874 * List all of the locked vnodes in the system. 2875 * Called when debugging the kernel. 2876 */ 2877 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2878 { 2879 struct mount *mp, *nmp; 2880 struct vnode *vp; 2881 2882 /* 2883 * Note: because this is DDB, we can't obey the locking semantics 2884 * for these structures, which means we could catch an inconsistent 2885 * state and dereference a nasty pointer. Not much to be done 2886 * about that. 2887 */ 2888 printf("Locked vnodes\n"); 2889 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2890 nmp = TAILQ_NEXT(mp, mnt_list); 2891 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2892 if (VOP_ISLOCKED(vp, NULL)) 2893 vprint(NULL, vp); 2894 } 2895 nmp = TAILQ_NEXT(mp, mnt_list); 2896 } 2897 } 2898 #endif 2899 2900 /* 2901 * Fill in a struct xvfsconf based on a struct vfsconf. 2902 */ 2903 static void 2904 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2905 { 2906 2907 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2908 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2909 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2910 xvfsp->vfc_flags = vfsp->vfc_flags; 2911 /* 2912 * These are unused in userland, we keep them 2913 * to not break binary compatibility. 2914 */ 2915 xvfsp->vfc_vfsops = NULL; 2916 xvfsp->vfc_next = NULL; 2917 } 2918 2919 static int 2920 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2921 { 2922 struct vfsconf *vfsp; 2923 struct xvfsconf *xvfsp; 2924 int cnt, error, i; 2925 2926 cnt = 0; 2927 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next) 2928 cnt++; 2929 xvfsp = malloc(sizeof(struct xvfsconf) * cnt, M_TEMP, M_WAITOK); 2930 /* 2931 * Handle the race that we will have here when struct vfsconf 2932 * will be locked down by using both cnt and checking vfc_next 2933 * against NULL to determine the end of the loop. The race will 2934 * happen because we will have to unlock before calling malloc(). 2935 * We are protected by Giant for now. 2936 */ 2937 i = 0; 2938 for (vfsp = vfsconf; vfsp != NULL && i < cnt; vfsp = vfsp->vfc_next) { 2939 vfsconf2x(vfsp, xvfsp + i); 2940 i++; 2941 } 2942 error = SYSCTL_OUT(req, xvfsp, sizeof(struct xvfsconf) * i); 2943 free(xvfsp, M_TEMP); 2944 return (error); 2945 } 2946 2947 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2948 "S,xvfsconf", "List of all configured filesystems"); 2949 2950 /* 2951 * Top level filesystem related information gathering. 2952 */ 2953 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2954 2955 static int 2956 vfs_sysctl(SYSCTL_HANDLER_ARGS) 2957 { 2958 int *name = (int *)arg1 - 1; /* XXX */ 2959 u_int namelen = arg2 + 1; /* XXX */ 2960 struct vfsconf *vfsp; 2961 struct xvfsconf xvfsp; 2962 2963 printf("WARNING: userland calling deprecated sysctl, " 2964 "please rebuild world\n"); 2965 2966 #if 1 || defined(COMPAT_PRELITE2) 2967 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2968 if (namelen == 1) 2969 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2970 #endif 2971 2972 switch (name[1]) { 2973 case VFS_MAXTYPENUM: 2974 if (namelen != 2) 2975 return (ENOTDIR); 2976 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2977 case VFS_CONF: 2978 if (namelen != 3) 2979 return (ENOTDIR); /* overloaded */ 2980 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 2981 if (vfsp->vfc_typenum == name[2]) 2982 break; 2983 if (vfsp == NULL) 2984 return (EOPNOTSUPP); 2985 vfsconf2x(vfsp, &xvfsp); 2986 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 2987 } 2988 return (EOPNOTSUPP); 2989 } 2990 2991 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl, 2992 "Generic filesystem"); 2993 2994 #if 1 || defined(COMPAT_PRELITE2) 2995 2996 static int 2997 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 2998 { 2999 int error; 3000 struct vfsconf *vfsp; 3001 struct ovfsconf ovfs; 3002 3003 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 3004 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 3005 strcpy(ovfs.vfc_name, vfsp->vfc_name); 3006 ovfs.vfc_index = vfsp->vfc_typenum; 3007 ovfs.vfc_refcount = vfsp->vfc_refcount; 3008 ovfs.vfc_flags = vfsp->vfc_flags; 3009 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 3010 if (error) 3011 return error; 3012 } 3013 return 0; 3014 } 3015 3016 #endif /* 1 || COMPAT_PRELITE2 */ 3017 3018 #define KINFO_VNODESLOP 10 3019 #ifdef notyet 3020 /* 3021 * Dump vnode list (via sysctl). 3022 */ 3023 /* ARGSUSED */ 3024 static int 3025 sysctl_vnode(SYSCTL_HANDLER_ARGS) 3026 { 3027 struct xvnode *xvn; 3028 struct thread *td = req->td; 3029 struct mount *mp; 3030 struct vnode *vp; 3031 int error, len, n; 3032 3033 /* 3034 * Stale numvnodes access is not fatal here. 3035 */ 3036 req->lock = 0; 3037 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 3038 if (!req->oldptr) 3039 /* Make an estimate */ 3040 return (SYSCTL_OUT(req, 0, len)); 3041 3042 sysctl_wire_old_buffer(req, 0); 3043 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 3044 n = 0; 3045 mtx_lock(&mountlist_mtx); 3046 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3047 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) 3048 continue; 3049 mtx_lock(&mntvnode_mtx); 3050 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3051 if (n == len) 3052 break; 3053 vref(vp); 3054 xvn[n].xv_size = sizeof *xvn; 3055 xvn[n].xv_vnode = vp; 3056 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3057 XV_COPY(usecount); 3058 XV_COPY(writecount); 3059 XV_COPY(holdcnt); 3060 XV_COPY(id); 3061 XV_COPY(mount); 3062 XV_COPY(numoutput); 3063 XV_COPY(type); 3064 #undef XV_COPY 3065 xvn[n].xv_flag = vp->v_vflag; 3066 3067 switch (vp->v_type) { 3068 case VREG: 3069 case VDIR: 3070 case VLNK: 3071 xvn[n].xv_dev = vp->v_cachedfs; 3072 xvn[n].xv_ino = vp->v_cachedid; 3073 break; 3074 case VBLK: 3075 case VCHR: 3076 if (vp->v_rdev == NULL) { 3077 vrele(vp); 3078 continue; 3079 } 3080 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3081 break; 3082 case VSOCK: 3083 xvn[n].xv_socket = vp->v_socket; 3084 break; 3085 case VFIFO: 3086 xvn[n].xv_fifo = vp->v_fifoinfo; 3087 break; 3088 case VNON: 3089 case VBAD: 3090 default: 3091 /* shouldn't happen? */ 3092 vrele(vp); 3093 continue; 3094 } 3095 vrele(vp); 3096 ++n; 3097 } 3098 mtx_unlock(&mntvnode_mtx); 3099 mtx_lock(&mountlist_mtx); 3100 vfs_unbusy(mp, td); 3101 if (n == len) 3102 break; 3103 } 3104 mtx_unlock(&mountlist_mtx); 3105 3106 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 3107 free(xvn, M_TEMP); 3108 return (error); 3109 } 3110 3111 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 3112 0, 0, sysctl_vnode, "S,xvnode", ""); 3113 #endif 3114 3115 /* 3116 * Check to see if a filesystem is mounted on a block device. 3117 */ 3118 int 3119 vfs_mountedon(vp) 3120 struct vnode *vp; 3121 { 3122 3123 if (vp->v_rdev->si_mountpoint != NULL) 3124 return (EBUSY); 3125 return (0); 3126 } 3127 3128 /* 3129 * Unmount all filesystems. The list is traversed in reverse order 3130 * of mounting to avoid dependencies. 3131 */ 3132 void 3133 vfs_unmountall() 3134 { 3135 struct mount *mp; 3136 struct thread *td; 3137 int error; 3138 3139 if (curthread != NULL) 3140 td = curthread; 3141 else 3142 td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */ 3143 /* 3144 * Since this only runs when rebooting, it is not interlocked. 3145 */ 3146 while(!TAILQ_EMPTY(&mountlist)) { 3147 mp = TAILQ_LAST(&mountlist, mntlist); 3148 error = dounmount(mp, MNT_FORCE, td); 3149 if (error) { 3150 TAILQ_REMOVE(&mountlist, mp, mnt_list); 3151 printf("unmount of %s failed (", 3152 mp->mnt_stat.f_mntonname); 3153 if (error == EBUSY) 3154 printf("BUSY)\n"); 3155 else 3156 printf("%d)\n", error); 3157 } else { 3158 /* The unmount has removed mp from the mountlist */ 3159 } 3160 } 3161 } 3162 3163 /* 3164 * perform msync on all vnodes under a mount point 3165 * the mount point must be locked. 3166 */ 3167 void 3168 vfs_msync(struct mount *mp, int flags) 3169 { 3170 struct vnode *vp, *nvp; 3171 struct vm_object *obj; 3172 int tries; 3173 3174 GIANT_REQUIRED; 3175 3176 tries = 5; 3177 mtx_lock(&mntvnode_mtx); 3178 loop: 3179 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) { 3180 if (vp->v_mount != mp) { 3181 if (--tries > 0) 3182 goto loop; 3183 break; 3184 } 3185 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 3186 3187 VI_LOCK(vp); 3188 if (vp->v_iflag & VI_XLOCK) { /* XXX: what if MNT_WAIT? */ 3189 VI_UNLOCK(vp); 3190 continue; 3191 } 3192 3193 if ((vp->v_iflag & VI_OBJDIRTY) && 3194 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) { 3195 mtx_unlock(&mntvnode_mtx); 3196 if (!vget(vp, 3197 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 3198 curthread)) { 3199 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 3200 vput(vp); 3201 mtx_lock(&mntvnode_mtx); 3202 continue; 3203 } 3204 3205 if (VOP_GETVOBJECT(vp, &obj) == 0) { 3206 VM_OBJECT_LOCK(obj); 3207 vm_object_page_clean(obj, 0, 0, 3208 flags == MNT_WAIT ? 3209 OBJPC_SYNC : OBJPC_NOSYNC); 3210 VM_OBJECT_UNLOCK(obj); 3211 } 3212 vput(vp); 3213 } 3214 mtx_lock(&mntvnode_mtx); 3215 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) { 3216 if (--tries > 0) 3217 goto loop; 3218 break; 3219 } 3220 } else 3221 VI_UNLOCK(vp); 3222 } 3223 mtx_unlock(&mntvnode_mtx); 3224 } 3225 3226 /* 3227 * Create the VM object needed for VMIO and mmap support. This 3228 * is done for all VREG files in the system. Some filesystems might 3229 * afford the additional metadata buffering capability of the 3230 * VMIO code by making the device node be VMIO mode also. 3231 * 3232 * vp must be locked when vfs_object_create is called. 3233 */ 3234 int 3235 vfs_object_create(vp, td, cred) 3236 struct vnode *vp; 3237 struct thread *td; 3238 struct ucred *cred; 3239 { 3240 GIANT_REQUIRED; 3241 return (VOP_CREATEVOBJECT(vp, cred, td)); 3242 } 3243 3244 /* 3245 * Mark a vnode as free, putting it up for recycling. 3246 */ 3247 void 3248 vfree(vp) 3249 struct vnode *vp; 3250 { 3251 ASSERT_VI_LOCKED(vp, "vfree"); 3252 mtx_lock(&vnode_free_list_mtx); 3253 KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free")); 3254 if (vp->v_iflag & VI_AGE) { 3255 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 3256 } else { 3257 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 3258 } 3259 freevnodes++; 3260 mtx_unlock(&vnode_free_list_mtx); 3261 vp->v_iflag &= ~VI_AGE; 3262 vp->v_iflag |= VI_FREE; 3263 } 3264 3265 /* 3266 * Opposite of vfree() - mark a vnode as in use. 3267 */ 3268 void 3269 vbusy(vp) 3270 struct vnode *vp; 3271 { 3272 ASSERT_VI_LOCKED(vp, "vbusy"); 3273 KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free")); 3274 3275 mtx_lock(&vnode_free_list_mtx); 3276 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 3277 freevnodes--; 3278 mtx_unlock(&vnode_free_list_mtx); 3279 3280 vp->v_iflag &= ~(VI_FREE|VI_AGE); 3281 } 3282 3283 /* 3284 * Record a process's interest in events which might happen to 3285 * a vnode. Because poll uses the historic select-style interface 3286 * internally, this routine serves as both the ``check for any 3287 * pending events'' and the ``record my interest in future events'' 3288 * functions. (These are done together, while the lock is held, 3289 * to avoid race conditions.) 3290 */ 3291 int 3292 vn_pollrecord(vp, td, events) 3293 struct vnode *vp; 3294 struct thread *td; 3295 short events; 3296 { 3297 3298 if (vp->v_pollinfo == NULL) 3299 v_addpollinfo(vp); 3300 mtx_lock(&vp->v_pollinfo->vpi_lock); 3301 if (vp->v_pollinfo->vpi_revents & events) { 3302 /* 3303 * This leaves events we are not interested 3304 * in available for the other process which 3305 * which presumably had requested them 3306 * (otherwise they would never have been 3307 * recorded). 3308 */ 3309 events &= vp->v_pollinfo->vpi_revents; 3310 vp->v_pollinfo->vpi_revents &= ~events; 3311 3312 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3313 return events; 3314 } 3315 vp->v_pollinfo->vpi_events |= events; 3316 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3317 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3318 return 0; 3319 } 3320 3321 /* 3322 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 3323 * it is possible for us to miss an event due to race conditions, but 3324 * that condition is expected to be rare, so for the moment it is the 3325 * preferred interface. 3326 */ 3327 void 3328 vn_pollevent(vp, events) 3329 struct vnode *vp; 3330 short events; 3331 { 3332 3333 if (vp->v_pollinfo == NULL) 3334 v_addpollinfo(vp); 3335 mtx_lock(&vp->v_pollinfo->vpi_lock); 3336 if (vp->v_pollinfo->vpi_events & events) { 3337 /* 3338 * We clear vpi_events so that we don't 3339 * call selwakeup() twice if two events are 3340 * posted before the polling process(es) is 3341 * awakened. This also ensures that we take at 3342 * most one selwakeup() if the polling process 3343 * is no longer interested. However, it does 3344 * mean that only one event can be noticed at 3345 * a time. (Perhaps we should only clear those 3346 * event bits which we note?) XXX 3347 */ 3348 vp->v_pollinfo->vpi_events = 0; /* &= ~events ??? */ 3349 vp->v_pollinfo->vpi_revents |= events; 3350 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3351 } 3352 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3353 } 3354 3355 /* 3356 * Wake up anyone polling on vp because it is being revoked. 3357 * This depends on dead_poll() returning POLLHUP for correct 3358 * behavior. 3359 */ 3360 void 3361 vn_pollgone(vp) 3362 struct vnode *vp; 3363 { 3364 3365 mtx_lock(&vp->v_pollinfo->vpi_lock); 3366 VN_KNOTE(vp, NOTE_REVOKE); 3367 if (vp->v_pollinfo->vpi_events) { 3368 vp->v_pollinfo->vpi_events = 0; 3369 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3370 } 3371 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3372 } 3373 3374 3375 3376 /* 3377 * Routine to create and manage a filesystem syncer vnode. 3378 */ 3379 #define sync_close ((int (*)(struct vop_close_args *))nullop) 3380 static int sync_fsync(struct vop_fsync_args *); 3381 static int sync_inactive(struct vop_inactive_args *); 3382 static int sync_reclaim(struct vop_reclaim_args *); 3383 3384 static vop_t **sync_vnodeop_p; 3385 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = { 3386 { &vop_default_desc, (vop_t *) vop_eopnotsupp }, 3387 { &vop_close_desc, (vop_t *) sync_close }, /* close */ 3388 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */ 3389 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */ 3390 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */ 3391 { &vop_lock_desc, (vop_t *) vop_stdlock }, /* lock */ 3392 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, /* unlock */ 3393 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, /* islocked */ 3394 { NULL, NULL } 3395 }; 3396 static struct vnodeopv_desc sync_vnodeop_opv_desc = 3397 { &sync_vnodeop_p, sync_vnodeop_entries }; 3398 3399 VNODEOP_SET(sync_vnodeop_opv_desc); 3400 3401 /* 3402 * Create a new filesystem syncer vnode for the specified mount point. 3403 */ 3404 int 3405 vfs_allocate_syncvnode(mp) 3406 struct mount *mp; 3407 { 3408 struct vnode *vp; 3409 static long start, incr, next; 3410 int error; 3411 3412 /* Allocate a new vnode */ 3413 if ((error = getnewvnode("syncer", mp, sync_vnodeop_p, &vp)) != 0) { 3414 mp->mnt_syncer = NULL; 3415 return (error); 3416 } 3417 vp->v_type = VNON; 3418 /* 3419 * Place the vnode onto the syncer worklist. We attempt to 3420 * scatter them about on the list so that they will go off 3421 * at evenly distributed times even if all the filesystems 3422 * are mounted at once. 3423 */ 3424 next += incr; 3425 if (next == 0 || next > syncer_maxdelay) { 3426 start /= 2; 3427 incr /= 2; 3428 if (start == 0) { 3429 start = syncer_maxdelay / 2; 3430 incr = syncer_maxdelay; 3431 } 3432 next = start; 3433 } 3434 VI_LOCK(vp); 3435 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0); 3436 VI_UNLOCK(vp); 3437 mp->mnt_syncer = vp; 3438 return (0); 3439 } 3440 3441 /* 3442 * Do a lazy sync of the filesystem. 3443 */ 3444 static int 3445 sync_fsync(ap) 3446 struct vop_fsync_args /* { 3447 struct vnode *a_vp; 3448 struct ucred *a_cred; 3449 int a_waitfor; 3450 struct thread *a_td; 3451 } */ *ap; 3452 { 3453 struct vnode *syncvp = ap->a_vp; 3454 struct mount *mp = syncvp->v_mount; 3455 struct thread *td = ap->a_td; 3456 int error, asyncflag; 3457 3458 /* 3459 * We only need to do something if this is a lazy evaluation. 3460 */ 3461 if (ap->a_waitfor != MNT_LAZY) 3462 return (0); 3463 3464 /* 3465 * Move ourselves to the back of the sync list. 3466 */ 3467 VI_LOCK(syncvp); 3468 vn_syncer_add_to_worklist(syncvp, syncdelay); 3469 VI_UNLOCK(syncvp); 3470 3471 /* 3472 * Walk the list of vnodes pushing all that are dirty and 3473 * not already on the sync list. 3474 */ 3475 mtx_lock(&mountlist_mtx); 3476 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) { 3477 mtx_unlock(&mountlist_mtx); 3478 return (0); 3479 } 3480 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3481 vfs_unbusy(mp, td); 3482 return (0); 3483 } 3484 asyncflag = mp->mnt_flag & MNT_ASYNC; 3485 mp->mnt_flag &= ~MNT_ASYNC; 3486 vfs_msync(mp, MNT_NOWAIT); 3487 error = VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td); 3488 if (asyncflag) 3489 mp->mnt_flag |= MNT_ASYNC; 3490 vn_finished_write(mp); 3491 vfs_unbusy(mp, td); 3492 return (error); 3493 } 3494 3495 /* 3496 * The syncer vnode is no referenced. 3497 */ 3498 static int 3499 sync_inactive(ap) 3500 struct vop_inactive_args /* { 3501 struct vnode *a_vp; 3502 struct thread *a_td; 3503 } */ *ap; 3504 { 3505 3506 VOP_UNLOCK(ap->a_vp, 0, ap->a_td); 3507 vgone(ap->a_vp); 3508 return (0); 3509 } 3510 3511 /* 3512 * The syncer vnode is no longer needed and is being decommissioned. 3513 * 3514 * Modifications to the worklist must be protected by sync_mtx. 3515 */ 3516 static int 3517 sync_reclaim(ap) 3518 struct vop_reclaim_args /* { 3519 struct vnode *a_vp; 3520 } */ *ap; 3521 { 3522 struct vnode *vp = ap->a_vp; 3523 3524 VI_LOCK(vp); 3525 vp->v_mount->mnt_syncer = NULL; 3526 if (vp->v_iflag & VI_ONWORKLST) { 3527 mtx_lock(&sync_mtx); 3528 LIST_REMOVE(vp, v_synclist); 3529 mtx_unlock(&sync_mtx); 3530 vp->v_iflag &= ~VI_ONWORKLST; 3531 } 3532 VI_UNLOCK(vp); 3533 3534 return (0); 3535 } 3536 3537 /* 3538 * extract the dev_t from a VCHR 3539 */ 3540 dev_t 3541 vn_todev(vp) 3542 struct vnode *vp; 3543 { 3544 if (vp->v_type != VCHR) 3545 return (NODEV); 3546 return (vp->v_rdev); 3547 } 3548 3549 /* 3550 * Check if vnode represents a disk device 3551 */ 3552 int 3553 vn_isdisk(vp, errp) 3554 struct vnode *vp; 3555 int *errp; 3556 { 3557 struct cdevsw *cdevsw; 3558 3559 if (vp->v_type != VCHR) { 3560 if (errp != NULL) 3561 *errp = ENOTBLK; 3562 return (0); 3563 } 3564 if (vp->v_rdev == NULL) { 3565 if (errp != NULL) 3566 *errp = ENXIO; 3567 return (0); 3568 } 3569 cdevsw = devsw(vp->v_rdev); 3570 if (cdevsw == NULL) { 3571 if (errp != NULL) 3572 *errp = ENXIO; 3573 return (0); 3574 } 3575 if (!(cdevsw->d_flags & D_DISK)) { 3576 if (errp != NULL) 3577 *errp = ENOTBLK; 3578 return (0); 3579 } 3580 if (errp != NULL) 3581 *errp = 0; 3582 return (1); 3583 } 3584 3585 /* 3586 * Free data allocated by namei(); see namei(9) for details. 3587 */ 3588 void 3589 NDFREE(ndp, flags) 3590 struct nameidata *ndp; 3591 const u_int flags; 3592 { 3593 if (!(flags & NDF_NO_FREE_PNBUF) && 3594 (ndp->ni_cnd.cn_flags & HASBUF)) { 3595 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 3596 ndp->ni_cnd.cn_flags &= ~HASBUF; 3597 } 3598 if (!(flags & NDF_NO_DVP_UNLOCK) && 3599 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 3600 ndp->ni_dvp != ndp->ni_vp) 3601 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 3602 if (!(flags & NDF_NO_DVP_RELE) && 3603 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 3604 vrele(ndp->ni_dvp); 3605 ndp->ni_dvp = NULL; 3606 } 3607 if (!(flags & NDF_NO_VP_UNLOCK) && 3608 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 3609 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 3610 if (!(flags & NDF_NO_VP_RELE) && 3611 ndp->ni_vp) { 3612 vrele(ndp->ni_vp); 3613 ndp->ni_vp = NULL; 3614 } 3615 if (!(flags & NDF_NO_STARTDIR_RELE) && 3616 (ndp->ni_cnd.cn_flags & SAVESTART)) { 3617 vrele(ndp->ni_startdir); 3618 ndp->ni_startdir = NULL; 3619 } 3620 } 3621 3622 /* 3623 * Common filesystem object access control check routine. Accepts a 3624 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3625 * and optional call-by-reference privused argument allowing vaccess() 3626 * to indicate to the caller whether privilege was used to satisfy the 3627 * request (obsoleted). Returns 0 on success, or an errno on failure. 3628 */ 3629 int 3630 vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused) 3631 enum vtype type; 3632 mode_t file_mode; 3633 uid_t file_uid; 3634 gid_t file_gid; 3635 mode_t acc_mode; 3636 struct ucred *cred; 3637 int *privused; 3638 { 3639 mode_t dac_granted; 3640 #ifdef CAPABILITIES 3641 mode_t cap_granted; 3642 #endif 3643 3644 /* 3645 * Look for a normal, non-privileged way to access the file/directory 3646 * as requested. If it exists, go with that. 3647 */ 3648 3649 if (privused != NULL) 3650 *privused = 0; 3651 3652 dac_granted = 0; 3653 3654 /* Check the owner. */ 3655 if (cred->cr_uid == file_uid) { 3656 dac_granted |= VADMIN; 3657 if (file_mode & S_IXUSR) 3658 dac_granted |= VEXEC; 3659 if (file_mode & S_IRUSR) 3660 dac_granted |= VREAD; 3661 if (file_mode & S_IWUSR) 3662 dac_granted |= (VWRITE | VAPPEND); 3663 3664 if ((acc_mode & dac_granted) == acc_mode) 3665 return (0); 3666 3667 goto privcheck; 3668 } 3669 3670 /* Otherwise, check the groups (first match) */ 3671 if (groupmember(file_gid, cred)) { 3672 if (file_mode & S_IXGRP) 3673 dac_granted |= VEXEC; 3674 if (file_mode & S_IRGRP) 3675 dac_granted |= VREAD; 3676 if (file_mode & S_IWGRP) 3677 dac_granted |= (VWRITE | VAPPEND); 3678 3679 if ((acc_mode & dac_granted) == acc_mode) 3680 return (0); 3681 3682 goto privcheck; 3683 } 3684 3685 /* Otherwise, check everyone else. */ 3686 if (file_mode & S_IXOTH) 3687 dac_granted |= VEXEC; 3688 if (file_mode & S_IROTH) 3689 dac_granted |= VREAD; 3690 if (file_mode & S_IWOTH) 3691 dac_granted |= (VWRITE | VAPPEND); 3692 if ((acc_mode & dac_granted) == acc_mode) 3693 return (0); 3694 3695 privcheck: 3696 if (!suser_cred(cred, PRISON_ROOT)) { 3697 /* XXX audit: privilege used */ 3698 if (privused != NULL) 3699 *privused = 1; 3700 return (0); 3701 } 3702 3703 #ifdef CAPABILITIES 3704 /* 3705 * Build a capability mask to determine if the set of capabilities 3706 * satisfies the requirements when combined with the granted mask 3707 * from above. 3708 * For each capability, if the capability is required, bitwise 3709 * or the request type onto the cap_granted mask. 3710 */ 3711 cap_granted = 0; 3712 3713 if (type == VDIR) { 3714 /* 3715 * For directories, use CAP_DAC_READ_SEARCH to satisfy 3716 * VEXEC requests, instead of CAP_DAC_EXECUTE. 3717 */ 3718 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3719 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3720 cap_granted |= VEXEC; 3721 } else { 3722 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3723 !cap_check(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT)) 3724 cap_granted |= VEXEC; 3725 } 3726 3727 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3728 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3729 cap_granted |= VREAD; 3730 3731 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3732 !cap_check(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT)) 3733 cap_granted |= (VWRITE | VAPPEND); 3734 3735 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3736 !cap_check(cred, NULL, CAP_FOWNER, PRISON_ROOT)) 3737 cap_granted |= VADMIN; 3738 3739 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) { 3740 /* XXX audit: privilege used */ 3741 if (privused != NULL) 3742 *privused = 1; 3743 return (0); 3744 } 3745 #endif 3746 3747 return ((acc_mode & VADMIN) ? EPERM : EACCES); 3748 } 3749 3750 /* 3751 * Credential check based on process requesting service, and per-attribute 3752 * permissions. 3753 */ 3754 int 3755 extattr_check_cred(struct vnode *vp, int attrnamespace, 3756 struct ucred *cred, struct thread *td, int access) 3757 { 3758 3759 /* 3760 * Kernel-invoked always succeeds. 3761 */ 3762 if (cred == NOCRED) 3763 return (0); 3764 3765 /* 3766 * Do not allow privileged processes in jail to directly 3767 * manipulate system attributes. 3768 * 3769 * XXX What capability should apply here? 3770 * Probably CAP_SYS_SETFFLAG. 3771 */ 3772 switch (attrnamespace) { 3773 case EXTATTR_NAMESPACE_SYSTEM: 3774 /* Potentially should be: return (EPERM); */ 3775 return (suser_cred(cred, 0)); 3776 case EXTATTR_NAMESPACE_USER: 3777 return (VOP_ACCESS(vp, access, cred, td)); 3778 default: 3779 return (EPERM); 3780 } 3781 } 3782