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