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