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