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