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