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