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