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 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 35 */ 36 37 /* 38 * External virtual filesystem routines 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_ddb.h" 45 #include "opt_mac.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/bio.h> 50 #include <sys/buf.h> 51 #include <sys/condvar.h> 52 #include <sys/conf.h> 53 #include <sys/dirent.h> 54 #include <sys/event.h> 55 #include <sys/eventhandler.h> 56 #include <sys/extattr.h> 57 #include <sys/file.h> 58 #include <sys/fcntl.h> 59 #include <sys/jail.h> 60 #include <sys/kdb.h> 61 #include <sys/kernel.h> 62 #include <sys/kthread.h> 63 #include <sys/lockf.h> 64 #include <sys/malloc.h> 65 #include <sys/mount.h> 66 #include <sys/namei.h> 67 #include <sys/priv.h> 68 #include <sys/reboot.h> 69 #include <sys/sleepqueue.h> 70 #include <sys/stat.h> 71 #include <sys/sysctl.h> 72 #include <sys/syslog.h> 73 #include <sys/vmmeter.h> 74 #include <sys/vnode.h> 75 76 #include <machine/stdarg.h> 77 78 #include <security/mac/mac_framework.h> 79 80 #include <vm/vm.h> 81 #include <vm/vm_object.h> 82 #include <vm/vm_extern.h> 83 #include <vm/pmap.h> 84 #include <vm/vm_map.h> 85 #include <vm/vm_page.h> 86 #include <vm/vm_kern.h> 87 #include <vm/uma.h> 88 89 #ifdef DDB 90 #include <ddb/ddb.h> 91 #endif 92 93 #define WI_MPSAFEQ 0 94 #define WI_GIANTQ 1 95 96 static MALLOC_DEFINE(M_NETADDR, "subr_export_host", "Export host address structure"); 97 98 static void delmntque(struct vnode *vp); 99 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, 100 int slpflag, int slptimeo); 101 static void syncer_shutdown(void *arg, int howto); 102 static int vtryrecycle(struct vnode *vp); 103 static void vbusy(struct vnode *vp); 104 static void vinactive(struct vnode *, struct thread *); 105 static void v_incr_usecount(struct vnode *); 106 static void v_decr_usecount(struct vnode *); 107 static void v_decr_useonly(struct vnode *); 108 static void v_upgrade_usecount(struct vnode *); 109 static void vfree(struct vnode *); 110 static void vnlru_free(int); 111 static void vgonel(struct vnode *); 112 static void vfs_knllock(void *arg); 113 static void vfs_knlunlock(void *arg); 114 static int vfs_knllocked(void *arg); 115 static void destroy_vpollinfo(struct vpollinfo *vi); 116 117 /* 118 * Enable Giant pushdown based on whether or not the vm is mpsafe in this 119 * build. Without mpsafevm the buffer cache can not run Giant free. 120 */ 121 int mpsafe_vfs = 1; 122 TUNABLE_INT("debug.mpsafevfs", &mpsafe_vfs); 123 SYSCTL_INT(_debug, OID_AUTO, mpsafevfs, CTLFLAG_RD, &mpsafe_vfs, 0, 124 "MPSAFE VFS"); 125 126 /* 127 * Number of vnodes in existence. Increased whenever getnewvnode() 128 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed 129 * vnode. 130 */ 131 static unsigned long numvnodes; 132 133 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, ""); 134 135 /* 136 * Conversion tables for conversion from vnode types to inode formats 137 * and back. 138 */ 139 enum vtype iftovt_tab[16] = { 140 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 141 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 142 }; 143 int vttoif_tab[10] = { 144 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 145 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT 146 }; 147 148 /* 149 * List of vnodes that are ready for recycling. 150 */ 151 static TAILQ_HEAD(freelst, vnode) vnode_free_list; 152 153 /* 154 * Free vnode target. Free vnodes may simply be files which have been stat'd 155 * but not read. This is somewhat common, and a small cache of such files 156 * should be kept to avoid recreation costs. 157 */ 158 static u_long wantfreevnodes; 159 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, ""); 160 /* Number of vnodes in the free list. */ 161 static u_long freevnodes; 162 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, ""); 163 164 /* 165 * Various variables used for debugging the new implementation of 166 * reassignbuf(). 167 * XXX these are probably of (very) limited utility now. 168 */ 169 static int reassignbufcalls; 170 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, ""); 171 172 /* 173 * Cache for the mount type id assigned to NFS. This is used for 174 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c. 175 */ 176 int nfs_mount_type = -1; 177 178 /* To keep more than one thread at a time from running vfs_getnewfsid */ 179 static struct mtx mntid_mtx; 180 181 /* 182 * Lock for any access to the following: 183 * vnode_free_list 184 * numvnodes 185 * freevnodes 186 */ 187 static struct mtx vnode_free_list_mtx; 188 189 /* Publicly exported FS */ 190 struct nfs_public nfs_pub; 191 192 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 193 static uma_zone_t vnode_zone; 194 static uma_zone_t vnodepoll_zone; 195 196 /* Set to 1 to print out reclaim of active vnodes */ 197 int prtactive; 198 199 /* 200 * The workitem queue. 201 * 202 * It is useful to delay writes of file data and filesystem metadata 203 * for tens of seconds so that quickly created and deleted files need 204 * not waste disk bandwidth being created and removed. To realize this, 205 * we append vnodes to a "workitem" queue. When running with a soft 206 * updates implementation, most pending metadata dependencies should 207 * not wait for more than a few seconds. Thus, mounted on block devices 208 * are delayed only about a half the time that file data is delayed. 209 * Similarly, directory updates are more critical, so are only delayed 210 * about a third the time that file data is delayed. Thus, there are 211 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 212 * one each second (driven off the filesystem syncer process). The 213 * syncer_delayno variable indicates the next queue that is to be processed. 214 * Items that need to be processed soon are placed in this queue: 215 * 216 * syncer_workitem_pending[syncer_delayno] 217 * 218 * A delay of fifteen seconds is done by placing the request fifteen 219 * entries later in the queue: 220 * 221 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 222 * 223 */ 224 static int syncer_delayno; 225 static long syncer_mask; 226 LIST_HEAD(synclist, bufobj); 227 static struct synclist *syncer_workitem_pending[2]; 228 /* 229 * The sync_mtx protects: 230 * bo->bo_synclist 231 * sync_vnode_count 232 * syncer_delayno 233 * syncer_state 234 * syncer_workitem_pending 235 * syncer_worklist_len 236 * rushjob 237 */ 238 static struct mtx sync_mtx; 239 static struct cv sync_wakeup; 240 241 #define SYNCER_MAXDELAY 32 242 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 243 static int syncdelay = 30; /* max time to delay syncing data */ 244 static int filedelay = 30; /* time to delay syncing files */ 245 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, ""); 246 static int dirdelay = 29; /* time to delay syncing directories */ 247 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, ""); 248 static int metadelay = 28; /* time to delay syncing metadata */ 249 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, ""); 250 static int rushjob; /* number of slots to run ASAP */ 251 static int stat_rush_requests; /* number of times I/O speeded up */ 252 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, ""); 253 254 /* 255 * When shutting down the syncer, run it at four times normal speed. 256 */ 257 #define SYNCER_SHUTDOWN_SPEEDUP 4 258 static int sync_vnode_count; 259 static int syncer_worklist_len; 260 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY } 261 syncer_state; 262 263 /* 264 * Number of vnodes we want to exist at any one time. This is mostly used 265 * to size hash tables in vnode-related code. It is normally not used in 266 * getnewvnode(), as wantfreevnodes is normally nonzero.) 267 * 268 * XXX desiredvnodes is historical cruft and should not exist. 269 */ 270 int desiredvnodes; 271 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 272 &desiredvnodes, 0, "Maximum number of vnodes"); 273 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 274 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)"); 275 static int vnlru_nowhere; 276 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, 277 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success"); 278 279 /* 280 * Macros to control when a vnode is freed and recycled. All require 281 * the vnode interlock. 282 */ 283 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt) 284 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt) 285 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt) 286 287 288 /* 289 * Initialize the vnode management data structures. 290 */ 291 #ifndef MAXVNODES_MAX 292 #define MAXVNODES_MAX 100000 293 #endif 294 static void 295 vntblinit(void *dummy __unused) 296 { 297 298 /* 299 * Desiredvnodes is a function of the physical memory size and 300 * the kernel's heap size. Specifically, desiredvnodes scales 301 * in proportion to the physical memory size until two fifths 302 * of the kernel's heap size is consumed by vnodes and vm 303 * objects. 304 */ 305 desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size / 306 (5 * (sizeof(struct vm_object) + sizeof(struct vnode)))); 307 if (desiredvnodes > MAXVNODES_MAX) { 308 if (bootverbose) 309 printf("Reducing kern.maxvnodes %d -> %d\n", 310 desiredvnodes, MAXVNODES_MAX); 311 desiredvnodes = MAXVNODES_MAX; 312 } 313 wantfreevnodes = desiredvnodes / 4; 314 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 315 TAILQ_INIT(&vnode_free_list); 316 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF); 317 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 318 NULL, NULL, UMA_ALIGN_PTR, 0); 319 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 320 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 321 /* 322 * Initialize the filesystem syncer. 323 */ 324 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE, 325 &syncer_mask); 326 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE, 327 &syncer_mask); 328 syncer_maxdelay = syncer_mask + 1; 329 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF); 330 cv_init(&sync_wakeup, "syncer"); 331 } 332 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL); 333 334 335 /* 336 * Mark a mount point as busy. Used to synchronize access and to delay 337 * unmounting. Eventually, mountlist_mtx is not released on failure. 338 */ 339 int 340 vfs_busy(struct mount *mp, int flags) 341 { 342 343 MPASS((flags & ~MBF_MASK) == 0); 344 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags); 345 346 MNT_ILOCK(mp); 347 MNT_REF(mp); 348 /* 349 * If mount point is currenly being unmounted, sleep until the 350 * mount point fate is decided. If thread doing the unmounting fails, 351 * it will clear MNTK_UNMOUNT flag before waking us up, indicating 352 * that this mount point has survived the unmount attempt and vfs_busy 353 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE 354 * flag in addition to MNTK_UNMOUNT, indicating that mount point is 355 * about to be really destroyed. vfs_busy needs to release its 356 * reference on the mount point in this case and return with ENOENT, 357 * telling the caller that mount mount it tried to busy is no longer 358 * valid. 359 */ 360 while (mp->mnt_kern_flag & MNTK_UNMOUNT) { 361 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) { 362 MNT_REL(mp); 363 MNT_IUNLOCK(mp); 364 CTR1(KTR_VFS, "%s: failed busying before sleeping", 365 __func__); 366 return (ENOENT); 367 } 368 if (flags & MBF_MNTLSTLOCK) 369 mtx_unlock(&mountlist_mtx); 370 mp->mnt_kern_flag |= MNTK_MWAIT; 371 msleep(mp, MNT_MTX(mp), PVFS, "vfs_busy", 0); 372 if (flags & MBF_MNTLSTLOCK) 373 mtx_lock(&mountlist_mtx); 374 } 375 if (flags & MBF_MNTLSTLOCK) 376 mtx_unlock(&mountlist_mtx); 377 mp->mnt_lockref++; 378 MNT_IUNLOCK(mp); 379 return (0); 380 } 381 382 /* 383 * Free a busy filesystem. 384 */ 385 void 386 vfs_unbusy(struct mount *mp) 387 { 388 389 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 390 MNT_ILOCK(mp); 391 MNT_REL(mp); 392 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref")); 393 mp->mnt_lockref--; 394 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) { 395 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT); 396 CTR1(KTR_VFS, "%s: waking up waiters", __func__); 397 mp->mnt_kern_flag &= ~MNTK_DRAINING; 398 wakeup(&mp->mnt_lockref); 399 } 400 MNT_IUNLOCK(mp); 401 } 402 403 /* 404 * Lookup a mount point by filesystem identifier. 405 */ 406 struct mount * 407 vfs_getvfs(fsid_t *fsid) 408 { 409 struct mount *mp; 410 411 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 412 mtx_lock(&mountlist_mtx); 413 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 414 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 415 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 416 vfs_ref(mp); 417 mtx_unlock(&mountlist_mtx); 418 return (mp); 419 } 420 } 421 mtx_unlock(&mountlist_mtx); 422 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 423 return ((struct mount *) 0); 424 } 425 426 /* 427 * Lookup a mount point by filesystem identifier, busying it before 428 * returning. 429 */ 430 struct mount * 431 vfs_busyfs(fsid_t *fsid) 432 { 433 struct mount *mp; 434 int error; 435 436 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 437 mtx_lock(&mountlist_mtx); 438 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 439 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 440 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 441 error = vfs_busy(mp, MBF_MNTLSTLOCK); 442 if (error) { 443 mtx_unlock(&mountlist_mtx); 444 return (NULL); 445 } 446 return (mp); 447 } 448 } 449 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 450 mtx_unlock(&mountlist_mtx); 451 return ((struct mount *) 0); 452 } 453 454 /* 455 * Check if a user can access privileged mount options. 456 */ 457 int 458 vfs_suser(struct mount *mp, struct thread *td) 459 { 460 int error; 461 462 /* 463 * If the thread is jailed, but this is not a jail-friendly file 464 * system, deny immediately. 465 */ 466 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred)) 467 return (EPERM); 468 469 /* 470 * If the file system was mounted outside a jail and a jailed thread 471 * tries to access it, deny immediately. 472 */ 473 if (!jailed(mp->mnt_cred) && jailed(td->td_ucred)) 474 return (EPERM); 475 476 /* 477 * If the file system was mounted inside different jail that the jail of 478 * the calling thread, deny immediately. 479 */ 480 if (jailed(mp->mnt_cred) && jailed(td->td_ucred) && 481 mp->mnt_cred->cr_prison != td->td_ucred->cr_prison) { 482 return (EPERM); 483 } 484 485 /* 486 * If file system supports delegated administration, we don't check 487 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified 488 * by the file system itself. 489 * If this is not the user that did original mount, we check for 490 * the PRIV_VFS_MOUNT_OWNER privilege. 491 */ 492 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) && 493 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) { 494 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0) 495 return (error); 496 } 497 return (0); 498 } 499 500 /* 501 * Get a new unique fsid. Try to make its val[0] unique, since this value 502 * will be used to create fake device numbers for stat(). Also try (but 503 * not so hard) make its val[0] unique mod 2^16, since some emulators only 504 * support 16-bit device numbers. We end up with unique val[0]'s for the 505 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 506 * 507 * Keep in mind that several mounts may be running in parallel. Starting 508 * the search one past where the previous search terminated is both a 509 * micro-optimization and a defense against returning the same fsid to 510 * different mounts. 511 */ 512 void 513 vfs_getnewfsid(struct mount *mp) 514 { 515 static u_int16_t mntid_base; 516 struct mount *nmp; 517 fsid_t tfsid; 518 int mtype; 519 520 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 521 mtx_lock(&mntid_mtx); 522 mtype = mp->mnt_vfc->vfc_typenum; 523 tfsid.val[1] = mtype; 524 mtype = (mtype & 0xFF) << 24; 525 for (;;) { 526 tfsid.val[0] = makedev(255, 527 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 528 mntid_base++; 529 if ((nmp = vfs_getvfs(&tfsid)) == NULL) 530 break; 531 vfs_rel(nmp); 532 } 533 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 534 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 535 mtx_unlock(&mntid_mtx); 536 } 537 538 /* 539 * Knob to control the precision of file timestamps: 540 * 541 * 0 = seconds only; nanoseconds zeroed. 542 * 1 = seconds and nanoseconds, accurate within 1/HZ. 543 * 2 = seconds and nanoseconds, truncated to microseconds. 544 * >=3 = seconds and nanoseconds, maximum precision. 545 */ 546 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 547 548 static int timestamp_precision = TSP_SEC; 549 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 550 ×tamp_precision, 0, ""); 551 552 /* 553 * Get a current timestamp. 554 */ 555 void 556 vfs_timestamp(struct timespec *tsp) 557 { 558 struct timeval tv; 559 560 switch (timestamp_precision) { 561 case TSP_SEC: 562 tsp->tv_sec = time_second; 563 tsp->tv_nsec = 0; 564 break; 565 case TSP_HZ: 566 getnanotime(tsp); 567 break; 568 case TSP_USEC: 569 microtime(&tv); 570 TIMEVAL_TO_TIMESPEC(&tv, tsp); 571 break; 572 case TSP_NSEC: 573 default: 574 nanotime(tsp); 575 break; 576 } 577 } 578 579 /* 580 * Set vnode attributes to VNOVAL 581 */ 582 void 583 vattr_null(struct vattr *vap) 584 { 585 586 vap->va_type = VNON; 587 vap->va_size = VNOVAL; 588 vap->va_bytes = VNOVAL; 589 vap->va_mode = VNOVAL; 590 vap->va_nlink = VNOVAL; 591 vap->va_uid = VNOVAL; 592 vap->va_gid = VNOVAL; 593 vap->va_fsid = VNOVAL; 594 vap->va_fileid = VNOVAL; 595 vap->va_blocksize = VNOVAL; 596 vap->va_rdev = VNOVAL; 597 vap->va_atime.tv_sec = VNOVAL; 598 vap->va_atime.tv_nsec = VNOVAL; 599 vap->va_mtime.tv_sec = VNOVAL; 600 vap->va_mtime.tv_nsec = VNOVAL; 601 vap->va_ctime.tv_sec = VNOVAL; 602 vap->va_ctime.tv_nsec = VNOVAL; 603 vap->va_birthtime.tv_sec = VNOVAL; 604 vap->va_birthtime.tv_nsec = VNOVAL; 605 vap->va_flags = VNOVAL; 606 vap->va_gen = VNOVAL; 607 vap->va_vaflags = 0; 608 } 609 610 /* 611 * This routine is called when we have too many vnodes. It attempts 612 * to free <count> vnodes and will potentially free vnodes that still 613 * have VM backing store (VM backing store is typically the cause 614 * of a vnode blowout so we want to do this). Therefore, this operation 615 * is not considered cheap. 616 * 617 * A number of conditions may prevent a vnode from being reclaimed. 618 * the buffer cache may have references on the vnode, a directory 619 * vnode may still have references due to the namei cache representing 620 * underlying files, or the vnode may be in active use. It is not 621 * desireable to reuse such vnodes. These conditions may cause the 622 * number of vnodes to reach some minimum value regardless of what 623 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 624 */ 625 static int 626 vlrureclaim(struct mount *mp) 627 { 628 struct vnode *vp; 629 int done; 630 int trigger; 631 int usevnodes; 632 int count; 633 634 /* 635 * Calculate the trigger point, don't allow user 636 * screwups to blow us up. This prevents us from 637 * recycling vnodes with lots of resident pages. We 638 * aren't trying to free memory, we are trying to 639 * free vnodes. 640 */ 641 usevnodes = desiredvnodes; 642 if (usevnodes <= 0) 643 usevnodes = 1; 644 trigger = cnt.v_page_count * 2 / usevnodes; 645 done = 0; 646 vn_start_write(NULL, &mp, V_WAIT); 647 MNT_ILOCK(mp); 648 count = mp->mnt_nvnodelistsize / 10 + 1; 649 while (count != 0) { 650 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 651 while (vp != NULL && vp->v_type == VMARKER) 652 vp = TAILQ_NEXT(vp, v_nmntvnodes); 653 if (vp == NULL) 654 break; 655 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 656 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 657 --count; 658 if (!VI_TRYLOCK(vp)) 659 goto next_iter; 660 /* 661 * If it's been deconstructed already, it's still 662 * referenced, or it exceeds the trigger, skip it. 663 */ 664 if (vp->v_usecount || !LIST_EMPTY(&(vp)->v_cache_src) || 665 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL && 666 vp->v_object->resident_page_count > trigger)) { 667 VI_UNLOCK(vp); 668 goto next_iter; 669 } 670 MNT_IUNLOCK(mp); 671 vholdl(vp); 672 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) { 673 vdrop(vp); 674 goto next_iter_mntunlocked; 675 } 676 VI_LOCK(vp); 677 /* 678 * v_usecount may have been bumped after VOP_LOCK() dropped 679 * the vnode interlock and before it was locked again. 680 * 681 * It is not necessary to recheck VI_DOOMED because it can 682 * only be set by another thread that holds both the vnode 683 * lock and vnode interlock. If another thread has the 684 * vnode lock before we get to VOP_LOCK() and obtains the 685 * vnode interlock after VOP_LOCK() drops the vnode 686 * interlock, the other thread will be unable to drop the 687 * vnode lock before our VOP_LOCK() call fails. 688 */ 689 if (vp->v_usecount || !LIST_EMPTY(&(vp)->v_cache_src) || 690 (vp->v_object != NULL && 691 vp->v_object->resident_page_count > trigger)) { 692 VOP_UNLOCK(vp, LK_INTERLOCK); 693 goto next_iter_mntunlocked; 694 } 695 KASSERT((vp->v_iflag & VI_DOOMED) == 0, 696 ("VI_DOOMED unexpectedly detected in vlrureclaim()")); 697 vgonel(vp); 698 VOP_UNLOCK(vp, 0); 699 vdropl(vp); 700 done++; 701 next_iter_mntunlocked: 702 if ((count % 256) != 0) 703 goto relock_mnt; 704 goto yield; 705 next_iter: 706 if ((count % 256) != 0) 707 continue; 708 MNT_IUNLOCK(mp); 709 yield: 710 uio_yield(); 711 relock_mnt: 712 MNT_ILOCK(mp); 713 } 714 MNT_IUNLOCK(mp); 715 vn_finished_write(mp); 716 return done; 717 } 718 719 /* 720 * Attempt to keep the free list at wantfreevnodes length. 721 */ 722 static void 723 vnlru_free(int count) 724 { 725 struct vnode *vp; 726 int vfslocked; 727 728 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 729 for (; count > 0; count--) { 730 vp = TAILQ_FIRST(&vnode_free_list); 731 /* 732 * The list can be modified while the free_list_mtx 733 * has been dropped and vp could be NULL here. 734 */ 735 if (!vp) 736 break; 737 VNASSERT(vp->v_op != NULL, vp, 738 ("vnlru_free: vnode already reclaimed.")); 739 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 740 /* 741 * Don't recycle if we can't get the interlock. 742 */ 743 if (!VI_TRYLOCK(vp)) { 744 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 745 continue; 746 } 747 VNASSERT(VCANRECYCLE(vp), vp, 748 ("vp inconsistent on freelist")); 749 freevnodes--; 750 vp->v_iflag &= ~VI_FREE; 751 vholdl(vp); 752 mtx_unlock(&vnode_free_list_mtx); 753 VI_UNLOCK(vp); 754 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 755 vtryrecycle(vp); 756 VFS_UNLOCK_GIANT(vfslocked); 757 /* 758 * If the recycled succeeded this vdrop will actually free 759 * the vnode. If not it will simply place it back on 760 * the free list. 761 */ 762 vdrop(vp); 763 mtx_lock(&vnode_free_list_mtx); 764 } 765 } 766 /* 767 * Attempt to recycle vnodes in a context that is always safe to block. 768 * Calling vlrurecycle() from the bowels of filesystem code has some 769 * interesting deadlock problems. 770 */ 771 static struct proc *vnlruproc; 772 static int vnlruproc_sig; 773 774 static void 775 vnlru_proc(void) 776 { 777 struct mount *mp, *nmp; 778 int done, vfslocked; 779 struct proc *p = vnlruproc; 780 781 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p, 782 SHUTDOWN_PRI_FIRST); 783 784 for (;;) { 785 kproc_suspend_check(p); 786 mtx_lock(&vnode_free_list_mtx); 787 if (freevnodes > wantfreevnodes) 788 vnlru_free(freevnodes - wantfreevnodes); 789 if (numvnodes <= desiredvnodes * 9 / 10) { 790 vnlruproc_sig = 0; 791 wakeup(&vnlruproc_sig); 792 msleep(vnlruproc, &vnode_free_list_mtx, 793 PVFS|PDROP, "vlruwt", hz); 794 continue; 795 } 796 mtx_unlock(&vnode_free_list_mtx); 797 done = 0; 798 mtx_lock(&mountlist_mtx); 799 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 800 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) { 801 nmp = TAILQ_NEXT(mp, mnt_list); 802 continue; 803 } 804 vfslocked = VFS_LOCK_GIANT(mp); 805 done += vlrureclaim(mp); 806 VFS_UNLOCK_GIANT(vfslocked); 807 mtx_lock(&mountlist_mtx); 808 nmp = TAILQ_NEXT(mp, mnt_list); 809 vfs_unbusy(mp); 810 } 811 mtx_unlock(&mountlist_mtx); 812 if (done == 0) { 813 EVENTHANDLER_INVOKE(vfs_lowvnodes, desiredvnodes / 10); 814 #if 0 815 /* These messages are temporary debugging aids */ 816 if (vnlru_nowhere < 5) 817 printf("vnlru process getting nowhere..\n"); 818 else if (vnlru_nowhere == 5) 819 printf("vnlru process messages stopped.\n"); 820 #endif 821 vnlru_nowhere++; 822 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 823 } else 824 uio_yield(); 825 } 826 } 827 828 static struct kproc_desc vnlru_kp = { 829 "vnlru", 830 vnlru_proc, 831 &vnlruproc 832 }; 833 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, 834 &vnlru_kp); 835 836 /* 837 * Routines having to do with the management of the vnode table. 838 */ 839 840 void 841 vdestroy(struct vnode *vp) 842 { 843 struct bufobj *bo; 844 845 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 846 mtx_lock(&vnode_free_list_mtx); 847 numvnodes--; 848 mtx_unlock(&vnode_free_list_mtx); 849 bo = &vp->v_bufobj; 850 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 851 ("cleaned vnode still on the free list.")); 852 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't")); 853 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count")); 854 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count")); 855 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count")); 856 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's")); 857 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0")); 858 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL")); 859 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0")); 860 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL")); 861 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst")); 862 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src")); 863 VI_UNLOCK(vp); 864 #ifdef MAC 865 mac_vnode_destroy(vp); 866 #endif 867 if (vp->v_pollinfo != NULL) 868 destroy_vpollinfo(vp->v_pollinfo); 869 #ifdef INVARIANTS 870 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */ 871 vp->v_op = NULL; 872 #endif 873 lockdestroy(vp->v_vnlock); 874 mtx_destroy(&vp->v_interlock); 875 mtx_destroy(BO_MTX(bo)); 876 uma_zfree(vnode_zone, vp); 877 } 878 879 /* 880 * Try to recycle a freed vnode. We abort if anyone picks up a reference 881 * before we actually vgone(). This function must be called with the vnode 882 * held to prevent the vnode from being returned to the free list midway 883 * through vgone(). 884 */ 885 static int 886 vtryrecycle(struct vnode *vp) 887 { 888 struct mount *vnmp; 889 890 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 891 VNASSERT(vp->v_holdcnt, vp, 892 ("vtryrecycle: Recycling vp %p without a reference.", vp)); 893 /* 894 * This vnode may found and locked via some other list, if so we 895 * can't recycle it yet. 896 */ 897 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 898 CTR2(KTR_VFS, 899 "%s: impossible to recycle, vp %p lock is already held", 900 __func__, vp); 901 return (EWOULDBLOCK); 902 } 903 /* 904 * Don't recycle if its filesystem is being suspended. 905 */ 906 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) { 907 VOP_UNLOCK(vp, 0); 908 CTR2(KTR_VFS, 909 "%s: impossible to recycle, cannot start the write for %p", 910 __func__, vp); 911 return (EBUSY); 912 } 913 /* 914 * If we got this far, we need to acquire the interlock and see if 915 * anyone picked up this vnode from another list. If not, we will 916 * mark it with DOOMED via vgonel() so that anyone who does find it 917 * will skip over it. 918 */ 919 VI_LOCK(vp); 920 if (vp->v_usecount) { 921 VOP_UNLOCK(vp, LK_INTERLOCK); 922 vn_finished_write(vnmp); 923 CTR2(KTR_VFS, 924 "%s: impossible to recycle, %p is already referenced", 925 __func__, vp); 926 return (EBUSY); 927 } 928 if ((vp->v_iflag & VI_DOOMED) == 0) 929 vgonel(vp); 930 VOP_UNLOCK(vp, LK_INTERLOCK); 931 vn_finished_write(vnmp); 932 return (0); 933 } 934 935 /* 936 * Return the next vnode from the free list. 937 */ 938 int 939 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops, 940 struct vnode **vpp) 941 { 942 struct vnode *vp = NULL; 943 struct bufobj *bo; 944 945 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag); 946 mtx_lock(&vnode_free_list_mtx); 947 /* 948 * Lend our context to reclaim vnodes if they've exceeded the max. 949 */ 950 if (freevnodes > wantfreevnodes) 951 vnlru_free(1); 952 /* 953 * Wait for available vnodes. 954 */ 955 if (numvnodes > desiredvnodes) { 956 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) { 957 /* 958 * File system is beeing suspended, we cannot risk a 959 * deadlock here, so allocate new vnode anyway. 960 */ 961 if (freevnodes > wantfreevnodes) 962 vnlru_free(freevnodes - wantfreevnodes); 963 goto alloc; 964 } 965 if (vnlruproc_sig == 0) { 966 vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 967 wakeup(vnlruproc); 968 } 969 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS, 970 "vlruwk", hz); 971 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */ 972 if (numvnodes > desiredvnodes) { 973 mtx_unlock(&vnode_free_list_mtx); 974 return (ENFILE); 975 } 976 #endif 977 } 978 alloc: 979 numvnodes++; 980 mtx_unlock(&vnode_free_list_mtx); 981 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO); 982 /* 983 * Setup locks. 984 */ 985 vp->v_vnlock = &vp->v_lock; 986 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 987 /* 988 * By default, don't allow shared locks unless filesystems 989 * opt-in. 990 */ 991 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE); 992 /* 993 * Initialize bufobj. 994 */ 995 bo = &vp->v_bufobj; 996 bo->__bo_vnode = vp; 997 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF); 998 bo->bo_ops = &buf_ops_bio; 999 bo->bo_private = vp; 1000 TAILQ_INIT(&bo->bo_clean.bv_hd); 1001 TAILQ_INIT(&bo->bo_dirty.bv_hd); 1002 /* 1003 * Initialize namecache. 1004 */ 1005 LIST_INIT(&vp->v_cache_src); 1006 TAILQ_INIT(&vp->v_cache_dst); 1007 /* 1008 * Finalize various vnode identity bits. 1009 */ 1010 vp->v_type = VNON; 1011 vp->v_tag = tag; 1012 vp->v_op = vops; 1013 v_incr_usecount(vp); 1014 vp->v_data = 0; 1015 #ifdef MAC 1016 mac_vnode_init(vp); 1017 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 1018 mac_vnode_associate_singlelabel(mp, vp); 1019 else if (mp == NULL && vops != &dead_vnodeops) 1020 printf("NULL mp in getnewvnode()\n"); 1021 #endif 1022 if (mp != NULL) { 1023 bo->bo_bsize = mp->mnt_stat.f_iosize; 1024 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0) 1025 vp->v_vflag |= VV_NOKNOTE; 1026 } 1027 1028 *vpp = vp; 1029 return (0); 1030 } 1031 1032 /* 1033 * Delete from old mount point vnode list, if on one. 1034 */ 1035 static void 1036 delmntque(struct vnode *vp) 1037 { 1038 struct mount *mp; 1039 1040 mp = vp->v_mount; 1041 if (mp == NULL) 1042 return; 1043 MNT_ILOCK(mp); 1044 vp->v_mount = NULL; 1045 VNASSERT(mp->mnt_nvnodelistsize > 0, vp, 1046 ("bad mount point vnode list size")); 1047 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1048 mp->mnt_nvnodelistsize--; 1049 MNT_REL(mp); 1050 MNT_IUNLOCK(mp); 1051 } 1052 1053 static void 1054 insmntque_stddtr(struct vnode *vp, void *dtr_arg) 1055 { 1056 1057 vp->v_data = NULL; 1058 vp->v_op = &dead_vnodeops; 1059 /* XXX non mp-safe fs may still call insmntque with vnode 1060 unlocked */ 1061 if (!VOP_ISLOCKED(vp)) 1062 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1063 vgone(vp); 1064 vput(vp); 1065 } 1066 1067 /* 1068 * Insert into list of vnodes for the new mount point, if available. 1069 */ 1070 int 1071 insmntque1(struct vnode *vp, struct mount *mp, 1072 void (*dtr)(struct vnode *, void *), void *dtr_arg) 1073 { 1074 int locked; 1075 1076 KASSERT(vp->v_mount == NULL, 1077 ("insmntque: vnode already on per mount vnode list")); 1078 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)")); 1079 #ifdef DEBUG_VFS_LOCKS 1080 if (!VFS_NEEDSGIANT(mp)) 1081 ASSERT_VOP_ELOCKED(vp, 1082 "insmntque: mp-safe fs and non-locked vp"); 1083 #endif 1084 MNT_ILOCK(mp); 1085 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 && 1086 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 || 1087 mp->mnt_nvnodelistsize == 0)) { 1088 locked = VOP_ISLOCKED(vp); 1089 if (!locked || (locked == LK_EXCLUSIVE && 1090 (vp->v_vflag & VV_FORCEINSMQ) == 0)) { 1091 MNT_IUNLOCK(mp); 1092 if (dtr != NULL) 1093 dtr(vp, dtr_arg); 1094 return (EBUSY); 1095 } 1096 } 1097 vp->v_mount = mp; 1098 MNT_REF(mp); 1099 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1100 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp, 1101 ("neg mount point vnode list size")); 1102 mp->mnt_nvnodelistsize++; 1103 MNT_IUNLOCK(mp); 1104 return (0); 1105 } 1106 1107 int 1108 insmntque(struct vnode *vp, struct mount *mp) 1109 { 1110 1111 return (insmntque1(vp, mp, insmntque_stddtr, NULL)); 1112 } 1113 1114 /* 1115 * Flush out and invalidate all buffers associated with a bufobj 1116 * Called with the underlying object locked. 1117 */ 1118 int 1119 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo) 1120 { 1121 int error; 1122 1123 BO_LOCK(bo); 1124 if (flags & V_SAVE) { 1125 error = bufobj_wwait(bo, slpflag, slptimeo); 1126 if (error) { 1127 BO_UNLOCK(bo); 1128 return (error); 1129 } 1130 if (bo->bo_dirty.bv_cnt > 0) { 1131 BO_UNLOCK(bo); 1132 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0) 1133 return (error); 1134 /* 1135 * XXX We could save a lock/unlock if this was only 1136 * enabled under INVARIANTS 1137 */ 1138 BO_LOCK(bo); 1139 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1140 panic("vinvalbuf: dirty bufs"); 1141 } 1142 } 1143 /* 1144 * If you alter this loop please notice that interlock is dropped and 1145 * reacquired in flushbuflist. Special care is needed to ensure that 1146 * no race conditions occur from this. 1147 */ 1148 do { 1149 error = flushbuflist(&bo->bo_clean, 1150 flags, bo, slpflag, slptimeo); 1151 if (error == 0) 1152 error = flushbuflist(&bo->bo_dirty, 1153 flags, bo, slpflag, slptimeo); 1154 if (error != 0 && error != EAGAIN) { 1155 BO_UNLOCK(bo); 1156 return (error); 1157 } 1158 } while (error != 0); 1159 1160 /* 1161 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1162 * have write I/O in-progress but if there is a VM object then the 1163 * VM object can also have read-I/O in-progress. 1164 */ 1165 do { 1166 bufobj_wwait(bo, 0, 0); 1167 BO_UNLOCK(bo); 1168 if (bo->bo_object != NULL) { 1169 VM_OBJECT_LOCK(bo->bo_object); 1170 vm_object_pip_wait(bo->bo_object, "bovlbx"); 1171 VM_OBJECT_UNLOCK(bo->bo_object); 1172 } 1173 BO_LOCK(bo); 1174 } while (bo->bo_numoutput > 0); 1175 BO_UNLOCK(bo); 1176 1177 /* 1178 * Destroy the copy in the VM cache, too. 1179 */ 1180 if (bo->bo_object != NULL && (flags & (V_ALT | V_NORMAL)) == 0) { 1181 VM_OBJECT_LOCK(bo->bo_object); 1182 vm_object_page_remove(bo->bo_object, 0, 0, 1183 (flags & V_SAVE) ? TRUE : FALSE); 1184 VM_OBJECT_UNLOCK(bo->bo_object); 1185 } 1186 1187 #ifdef INVARIANTS 1188 BO_LOCK(bo); 1189 if ((flags & (V_ALT | V_NORMAL)) == 0 && 1190 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0)) 1191 panic("vinvalbuf: flush failed"); 1192 BO_UNLOCK(bo); 1193 #endif 1194 return (0); 1195 } 1196 1197 /* 1198 * Flush out and invalidate all buffers associated with a vnode. 1199 * Called with the underlying object locked. 1200 */ 1201 int 1202 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 1203 { 1204 1205 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 1206 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1207 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo)); 1208 } 1209 1210 /* 1211 * Flush out buffers on the specified list. 1212 * 1213 */ 1214 static int 1215 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 1216 int slptimeo) 1217 { 1218 struct buf *bp, *nbp; 1219 int retval, error; 1220 daddr_t lblkno; 1221 b_xflags_t xflags; 1222 1223 ASSERT_BO_LOCKED(bo); 1224 1225 retval = 0; 1226 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 1227 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 1228 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) { 1229 continue; 1230 } 1231 lblkno = 0; 1232 xflags = 0; 1233 if (nbp != NULL) { 1234 lblkno = nbp->b_lblkno; 1235 xflags = nbp->b_xflags & 1236 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN); 1237 } 1238 retval = EAGAIN; 1239 error = BUF_TIMELOCK(bp, 1240 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo), 1241 "flushbuf", slpflag, slptimeo); 1242 if (error) { 1243 BO_LOCK(bo); 1244 return (error != ENOLCK ? error : EAGAIN); 1245 } 1246 KASSERT(bp->b_bufobj == bo, 1247 ("bp %p wrong b_bufobj %p should be %p", 1248 bp, bp->b_bufobj, bo)); 1249 if (bp->b_bufobj != bo) { /* XXX: necessary ? */ 1250 BUF_UNLOCK(bp); 1251 BO_LOCK(bo); 1252 return (EAGAIN); 1253 } 1254 /* 1255 * XXX Since there are no node locks for NFS, I 1256 * believe there is a slight chance that a delayed 1257 * write will occur while sleeping just above, so 1258 * check for it. 1259 */ 1260 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1261 (flags & V_SAVE)) { 1262 bremfree(bp); 1263 bp->b_flags |= B_ASYNC; 1264 bwrite(bp); 1265 BO_LOCK(bo); 1266 return (EAGAIN); /* XXX: why not loop ? */ 1267 } 1268 bremfree(bp); 1269 bp->b_flags |= (B_INVAL | B_RELBUF); 1270 bp->b_flags &= ~B_ASYNC; 1271 brelse(bp); 1272 BO_LOCK(bo); 1273 if (nbp != NULL && 1274 (nbp->b_bufobj != bo || 1275 nbp->b_lblkno != lblkno || 1276 (nbp->b_xflags & 1277 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags)) 1278 break; /* nbp invalid */ 1279 } 1280 return (retval); 1281 } 1282 1283 /* 1284 * Truncate a file's buffer and pages to a specified length. This 1285 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1286 * sync activity. 1287 */ 1288 int 1289 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td, 1290 off_t length, int blksize) 1291 { 1292 struct buf *bp, *nbp; 1293 int anyfreed; 1294 int trunclbn; 1295 struct bufobj *bo; 1296 1297 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__, 1298 vp, cred, blksize, (uintmax_t)length); 1299 1300 /* 1301 * Round up to the *next* lbn. 1302 */ 1303 trunclbn = (length + blksize - 1) / blksize; 1304 1305 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 1306 restart: 1307 bo = &vp->v_bufobj; 1308 BO_LOCK(bo); 1309 anyfreed = 1; 1310 for (;anyfreed;) { 1311 anyfreed = 0; 1312 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 1313 if (bp->b_lblkno < trunclbn) 1314 continue; 1315 if (BUF_LOCK(bp, 1316 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1317 BO_MTX(bo)) == ENOLCK) 1318 goto restart; 1319 1320 bremfree(bp); 1321 bp->b_flags |= (B_INVAL | B_RELBUF); 1322 bp->b_flags &= ~B_ASYNC; 1323 brelse(bp); 1324 anyfreed = 1; 1325 1326 if (nbp != NULL && 1327 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1328 (nbp->b_vp != vp) || 1329 (nbp->b_flags & B_DELWRI))) { 1330 goto restart; 1331 } 1332 BO_LOCK(bo); 1333 } 1334 1335 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1336 if (bp->b_lblkno < trunclbn) 1337 continue; 1338 if (BUF_LOCK(bp, 1339 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1340 BO_MTX(bo)) == ENOLCK) 1341 goto restart; 1342 bremfree(bp); 1343 bp->b_flags |= (B_INVAL | B_RELBUF); 1344 bp->b_flags &= ~B_ASYNC; 1345 brelse(bp); 1346 anyfreed = 1; 1347 if (nbp != NULL && 1348 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1349 (nbp->b_vp != vp) || 1350 (nbp->b_flags & B_DELWRI) == 0)) { 1351 goto restart; 1352 } 1353 BO_LOCK(bo); 1354 } 1355 } 1356 1357 if (length > 0) { 1358 restartsync: 1359 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1360 if (bp->b_lblkno > 0) 1361 continue; 1362 /* 1363 * Since we hold the vnode lock this should only 1364 * fail if we're racing with the buf daemon. 1365 */ 1366 if (BUF_LOCK(bp, 1367 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1368 BO_MTX(bo)) == ENOLCK) { 1369 goto restart; 1370 } 1371 VNASSERT((bp->b_flags & B_DELWRI), vp, 1372 ("buf(%p) on dirty queue without DELWRI", bp)); 1373 1374 bremfree(bp); 1375 bawrite(bp); 1376 BO_LOCK(bo); 1377 goto restartsync; 1378 } 1379 } 1380 1381 bufobj_wwait(bo, 0, 0); 1382 BO_UNLOCK(bo); 1383 vnode_pager_setsize(vp, length); 1384 1385 return (0); 1386 } 1387 1388 /* 1389 * buf_splay() - splay tree core for the clean/dirty list of buffers in 1390 * a vnode. 1391 * 1392 * NOTE: We have to deal with the special case of a background bitmap 1393 * buffer, a situation where two buffers will have the same logical 1394 * block offset. We want (1) only the foreground buffer to be accessed 1395 * in a lookup and (2) must differentiate between the foreground and 1396 * background buffer in the splay tree algorithm because the splay 1397 * tree cannot normally handle multiple entities with the same 'index'. 1398 * We accomplish this by adding differentiating flags to the splay tree's 1399 * numerical domain. 1400 */ 1401 static 1402 struct buf * 1403 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root) 1404 { 1405 struct buf dummy; 1406 struct buf *lefttreemax, *righttreemin, *y; 1407 1408 if (root == NULL) 1409 return (NULL); 1410 lefttreemax = righttreemin = &dummy; 1411 for (;;) { 1412 if (lblkno < root->b_lblkno || 1413 (lblkno == root->b_lblkno && 1414 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1415 if ((y = root->b_left) == NULL) 1416 break; 1417 if (lblkno < y->b_lblkno) { 1418 /* Rotate right. */ 1419 root->b_left = y->b_right; 1420 y->b_right = root; 1421 root = y; 1422 if ((y = root->b_left) == NULL) 1423 break; 1424 } 1425 /* Link into the new root's right tree. */ 1426 righttreemin->b_left = root; 1427 righttreemin = root; 1428 } else if (lblkno > root->b_lblkno || 1429 (lblkno == root->b_lblkno && 1430 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) { 1431 if ((y = root->b_right) == NULL) 1432 break; 1433 if (lblkno > y->b_lblkno) { 1434 /* Rotate left. */ 1435 root->b_right = y->b_left; 1436 y->b_left = root; 1437 root = y; 1438 if ((y = root->b_right) == NULL) 1439 break; 1440 } 1441 /* Link into the new root's left tree. */ 1442 lefttreemax->b_right = root; 1443 lefttreemax = root; 1444 } else { 1445 break; 1446 } 1447 root = y; 1448 } 1449 /* Assemble the new root. */ 1450 lefttreemax->b_right = root->b_left; 1451 righttreemin->b_left = root->b_right; 1452 root->b_left = dummy.b_right; 1453 root->b_right = dummy.b_left; 1454 return (root); 1455 } 1456 1457 static void 1458 buf_vlist_remove(struct buf *bp) 1459 { 1460 struct buf *root; 1461 struct bufv *bv; 1462 1463 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1464 ASSERT_BO_LOCKED(bp->b_bufobj); 1465 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 1466 (BX_VNDIRTY|BX_VNCLEAN), 1467 ("buf_vlist_remove: Buf %p is on two lists", bp)); 1468 if (bp->b_xflags & BX_VNDIRTY) 1469 bv = &bp->b_bufobj->bo_dirty; 1470 else 1471 bv = &bp->b_bufobj->bo_clean; 1472 if (bp != bv->bv_root) { 1473 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root); 1474 KASSERT(root == bp, ("splay lookup failed in remove")); 1475 } 1476 if (bp->b_left == NULL) { 1477 root = bp->b_right; 1478 } else { 1479 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1480 root->b_right = bp->b_right; 1481 } 1482 bv->bv_root = root; 1483 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 1484 bv->bv_cnt--; 1485 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1486 } 1487 1488 /* 1489 * Add the buffer to the sorted clean or dirty block list using a 1490 * splay tree algorithm. 1491 * 1492 * NOTE: xflags is passed as a constant, optimizing this inline function! 1493 */ 1494 static void 1495 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 1496 { 1497 struct buf *root; 1498 struct bufv *bv; 1499 1500 ASSERT_BO_LOCKED(bo); 1501 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1502 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 1503 bp->b_xflags |= xflags; 1504 if (xflags & BX_VNDIRTY) 1505 bv = &bo->bo_dirty; 1506 else 1507 bv = &bo->bo_clean; 1508 1509 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root); 1510 if (root == NULL) { 1511 bp->b_left = NULL; 1512 bp->b_right = NULL; 1513 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 1514 } else if (bp->b_lblkno < root->b_lblkno || 1515 (bp->b_lblkno == root->b_lblkno && 1516 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1517 bp->b_left = root->b_left; 1518 bp->b_right = root; 1519 root->b_left = NULL; 1520 TAILQ_INSERT_BEFORE(root, bp, b_bobufs); 1521 } else { 1522 bp->b_right = root->b_right; 1523 bp->b_left = root; 1524 root->b_right = NULL; 1525 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs); 1526 } 1527 bv->bv_cnt++; 1528 bv->bv_root = bp; 1529 } 1530 1531 /* 1532 * Lookup a buffer using the splay tree. Note that we specifically avoid 1533 * shadow buffers used in background bitmap writes. 1534 * 1535 * This code isn't quite efficient as it could be because we are maintaining 1536 * two sorted lists and do not know which list the block resides in. 1537 * 1538 * During a "make buildworld" the desired buffer is found at one of 1539 * the roots more than 60% of the time. Thus, checking both roots 1540 * before performing either splay eliminates unnecessary splays on the 1541 * first tree splayed. 1542 */ 1543 struct buf * 1544 gbincore(struct bufobj *bo, daddr_t lblkno) 1545 { 1546 struct buf *bp; 1547 1548 ASSERT_BO_LOCKED(bo); 1549 if ((bp = bo->bo_clean.bv_root) != NULL && 1550 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1551 return (bp); 1552 if ((bp = bo->bo_dirty.bv_root) != NULL && 1553 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1554 return (bp); 1555 if ((bp = bo->bo_clean.bv_root) != NULL) { 1556 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp); 1557 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1558 return (bp); 1559 } 1560 if ((bp = bo->bo_dirty.bv_root) != NULL) { 1561 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp); 1562 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1563 return (bp); 1564 } 1565 return (NULL); 1566 } 1567 1568 /* 1569 * Associate a buffer with a vnode. 1570 */ 1571 void 1572 bgetvp(struct vnode *vp, struct buf *bp) 1573 { 1574 struct bufobj *bo; 1575 1576 bo = &vp->v_bufobj; 1577 ASSERT_BO_LOCKED(bo); 1578 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 1579 1580 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 1581 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 1582 ("bgetvp: bp already attached! %p", bp)); 1583 1584 vhold(vp); 1585 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT) 1586 bp->b_flags |= B_NEEDSGIANT; 1587 bp->b_vp = vp; 1588 bp->b_bufobj = bo; 1589 /* 1590 * Insert onto list for new vnode. 1591 */ 1592 buf_vlist_add(bp, bo, BX_VNCLEAN); 1593 } 1594 1595 /* 1596 * Disassociate a buffer from a vnode. 1597 */ 1598 void 1599 brelvp(struct buf *bp) 1600 { 1601 struct bufobj *bo; 1602 struct vnode *vp; 1603 1604 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1605 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1606 1607 /* 1608 * Delete from old vnode list, if on one. 1609 */ 1610 vp = bp->b_vp; /* XXX */ 1611 bo = bp->b_bufobj; 1612 BO_LOCK(bo); 1613 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1614 buf_vlist_remove(bp); 1615 else 1616 panic("brelvp: Buffer %p not on queue.", bp); 1617 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 1618 bo->bo_flag &= ~BO_ONWORKLST; 1619 mtx_lock(&sync_mtx); 1620 LIST_REMOVE(bo, bo_synclist); 1621 syncer_worklist_len--; 1622 mtx_unlock(&sync_mtx); 1623 } 1624 bp->b_flags &= ~B_NEEDSGIANT; 1625 bp->b_vp = NULL; 1626 bp->b_bufobj = NULL; 1627 BO_UNLOCK(bo); 1628 vdrop(vp); 1629 } 1630 1631 /* 1632 * Add an item to the syncer work queue. 1633 */ 1634 static void 1635 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 1636 { 1637 int queue, slot; 1638 1639 ASSERT_BO_LOCKED(bo); 1640 1641 mtx_lock(&sync_mtx); 1642 if (bo->bo_flag & BO_ONWORKLST) 1643 LIST_REMOVE(bo, bo_synclist); 1644 else { 1645 bo->bo_flag |= BO_ONWORKLST; 1646 syncer_worklist_len++; 1647 } 1648 1649 if (delay > syncer_maxdelay - 2) 1650 delay = syncer_maxdelay - 2; 1651 slot = (syncer_delayno + delay) & syncer_mask; 1652 1653 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ : 1654 WI_MPSAFEQ; 1655 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo, 1656 bo_synclist); 1657 mtx_unlock(&sync_mtx); 1658 } 1659 1660 static int 1661 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 1662 { 1663 int error, len; 1664 1665 mtx_lock(&sync_mtx); 1666 len = syncer_worklist_len - sync_vnode_count; 1667 mtx_unlock(&sync_mtx); 1668 error = SYSCTL_OUT(req, &len, sizeof(len)); 1669 return (error); 1670 } 1671 1672 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 1673 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 1674 1675 static struct proc *updateproc; 1676 static void sched_sync(void); 1677 static struct kproc_desc up_kp = { 1678 "syncer", 1679 sched_sync, 1680 &updateproc 1681 }; 1682 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 1683 1684 static int 1685 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 1686 { 1687 struct vnode *vp; 1688 struct mount *mp; 1689 1690 *bo = LIST_FIRST(slp); 1691 if (*bo == NULL) 1692 return (0); 1693 vp = (*bo)->__bo_vnode; /* XXX */ 1694 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 1695 return (1); 1696 /* 1697 * We use vhold in case the vnode does not 1698 * successfully sync. vhold prevents the vnode from 1699 * going away when we unlock the sync_mtx so that 1700 * we can acquire the vnode interlock. 1701 */ 1702 vholdl(vp); 1703 mtx_unlock(&sync_mtx); 1704 VI_UNLOCK(vp); 1705 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1706 vdrop(vp); 1707 mtx_lock(&sync_mtx); 1708 return (*bo == LIST_FIRST(slp)); 1709 } 1710 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1711 (void) VOP_FSYNC(vp, MNT_LAZY, td); 1712 VOP_UNLOCK(vp, 0); 1713 vn_finished_write(mp); 1714 BO_LOCK(*bo); 1715 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 1716 /* 1717 * Put us back on the worklist. The worklist 1718 * routine will remove us from our current 1719 * position and then add us back in at a later 1720 * position. 1721 */ 1722 vn_syncer_add_to_worklist(*bo, syncdelay); 1723 } 1724 BO_UNLOCK(*bo); 1725 vdrop(vp); 1726 mtx_lock(&sync_mtx); 1727 return (0); 1728 } 1729 1730 /* 1731 * System filesystem synchronizer daemon. 1732 */ 1733 static void 1734 sched_sync(void) 1735 { 1736 struct synclist *gnext, *next; 1737 struct synclist *gslp, *slp; 1738 struct bufobj *bo; 1739 long starttime; 1740 struct thread *td = curthread; 1741 int last_work_seen; 1742 int net_worklist_len; 1743 int syncer_final_iter; 1744 int first_printf; 1745 int error; 1746 1747 last_work_seen = 0; 1748 syncer_final_iter = 0; 1749 first_printf = 1; 1750 syncer_state = SYNCER_RUNNING; 1751 starttime = time_uptime; 1752 td->td_pflags |= TDP_NORUNNINGBUF; 1753 1754 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 1755 SHUTDOWN_PRI_LAST); 1756 1757 mtx_lock(&sync_mtx); 1758 for (;;) { 1759 if (syncer_state == SYNCER_FINAL_DELAY && 1760 syncer_final_iter == 0) { 1761 mtx_unlock(&sync_mtx); 1762 kproc_suspend_check(td->td_proc); 1763 mtx_lock(&sync_mtx); 1764 } 1765 net_worklist_len = syncer_worklist_len - sync_vnode_count; 1766 if (syncer_state != SYNCER_RUNNING && 1767 starttime != time_uptime) { 1768 if (first_printf) { 1769 printf("\nSyncing disks, vnodes remaining..."); 1770 first_printf = 0; 1771 } 1772 printf("%d ", net_worklist_len); 1773 } 1774 starttime = time_uptime; 1775 1776 /* 1777 * Push files whose dirty time has expired. Be careful 1778 * of interrupt race on slp queue. 1779 * 1780 * Skip over empty worklist slots when shutting down. 1781 */ 1782 do { 1783 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno]; 1784 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno]; 1785 syncer_delayno += 1; 1786 if (syncer_delayno == syncer_maxdelay) 1787 syncer_delayno = 0; 1788 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno]; 1789 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno]; 1790 /* 1791 * If the worklist has wrapped since the 1792 * it was emptied of all but syncer vnodes, 1793 * switch to the FINAL_DELAY state and run 1794 * for one more second. 1795 */ 1796 if (syncer_state == SYNCER_SHUTTING_DOWN && 1797 net_worklist_len == 0 && 1798 last_work_seen == syncer_delayno) { 1799 syncer_state = SYNCER_FINAL_DELAY; 1800 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 1801 } 1802 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 1803 LIST_EMPTY(gslp) && syncer_worklist_len > 0); 1804 1805 /* 1806 * Keep track of the last time there was anything 1807 * on the worklist other than syncer vnodes. 1808 * Return to the SHUTTING_DOWN state if any 1809 * new work appears. 1810 */ 1811 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 1812 last_work_seen = syncer_delayno; 1813 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 1814 syncer_state = SYNCER_SHUTTING_DOWN; 1815 while (!LIST_EMPTY(slp)) { 1816 error = sync_vnode(slp, &bo, td); 1817 if (error == 1) { 1818 LIST_REMOVE(bo, bo_synclist); 1819 LIST_INSERT_HEAD(next, bo, bo_synclist); 1820 continue; 1821 } 1822 } 1823 if (!LIST_EMPTY(gslp)) { 1824 mtx_unlock(&sync_mtx); 1825 mtx_lock(&Giant); 1826 mtx_lock(&sync_mtx); 1827 while (!LIST_EMPTY(gslp)) { 1828 error = sync_vnode(gslp, &bo, td); 1829 if (error == 1) { 1830 LIST_REMOVE(bo, bo_synclist); 1831 LIST_INSERT_HEAD(gnext, bo, 1832 bo_synclist); 1833 continue; 1834 } 1835 } 1836 mtx_unlock(&Giant); 1837 } 1838 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 1839 syncer_final_iter--; 1840 /* 1841 * The variable rushjob allows the kernel to speed up the 1842 * processing of the filesystem syncer process. A rushjob 1843 * value of N tells the filesystem syncer to process the next 1844 * N seconds worth of work on its queue ASAP. Currently rushjob 1845 * is used by the soft update code to speed up the filesystem 1846 * syncer process when the incore state is getting so far 1847 * ahead of the disk that the kernel memory pool is being 1848 * threatened with exhaustion. 1849 */ 1850 if (rushjob > 0) { 1851 rushjob -= 1; 1852 continue; 1853 } 1854 /* 1855 * Just sleep for a short period of time between 1856 * iterations when shutting down to allow some I/O 1857 * to happen. 1858 * 1859 * If it has taken us less than a second to process the 1860 * current work, then wait. Otherwise start right over 1861 * again. We can still lose time if any single round 1862 * takes more than two seconds, but it does not really 1863 * matter as we are just trying to generally pace the 1864 * filesystem activity. 1865 */ 1866 if (syncer_state != SYNCER_RUNNING) 1867 cv_timedwait(&sync_wakeup, &sync_mtx, 1868 hz / SYNCER_SHUTDOWN_SPEEDUP); 1869 else if (time_uptime == starttime) 1870 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 1871 } 1872 } 1873 1874 /* 1875 * Request the syncer daemon to speed up its work. 1876 * We never push it to speed up more than half of its 1877 * normal turn time, otherwise it could take over the cpu. 1878 */ 1879 int 1880 speedup_syncer(void) 1881 { 1882 int ret = 0; 1883 1884 mtx_lock(&sync_mtx); 1885 if (rushjob < syncdelay / 2) { 1886 rushjob += 1; 1887 stat_rush_requests += 1; 1888 ret = 1; 1889 } 1890 mtx_unlock(&sync_mtx); 1891 cv_broadcast(&sync_wakeup); 1892 return (ret); 1893 } 1894 1895 /* 1896 * Tell the syncer to speed up its work and run though its work 1897 * list several times, then tell it to shut down. 1898 */ 1899 static void 1900 syncer_shutdown(void *arg, int howto) 1901 { 1902 1903 if (howto & RB_NOSYNC) 1904 return; 1905 mtx_lock(&sync_mtx); 1906 syncer_state = SYNCER_SHUTTING_DOWN; 1907 rushjob = 0; 1908 mtx_unlock(&sync_mtx); 1909 cv_broadcast(&sync_wakeup); 1910 kproc_shutdown(arg, howto); 1911 } 1912 1913 /* 1914 * Reassign a buffer from one vnode to another. 1915 * Used to assign file specific control information 1916 * (indirect blocks) to the vnode to which they belong. 1917 */ 1918 void 1919 reassignbuf(struct buf *bp) 1920 { 1921 struct vnode *vp; 1922 struct bufobj *bo; 1923 int delay; 1924 #ifdef INVARIANTS 1925 struct bufv *bv; 1926 #endif 1927 1928 vp = bp->b_vp; 1929 bo = bp->b_bufobj; 1930 ++reassignbufcalls; 1931 1932 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 1933 bp, bp->b_vp, bp->b_flags); 1934 /* 1935 * B_PAGING flagged buffers cannot be reassigned because their vp 1936 * is not fully linked in. 1937 */ 1938 if (bp->b_flags & B_PAGING) 1939 panic("cannot reassign paging buffer"); 1940 1941 /* 1942 * Delete from old vnode list, if on one. 1943 */ 1944 BO_LOCK(bo); 1945 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1946 buf_vlist_remove(bp); 1947 else 1948 panic("reassignbuf: Buffer %p not on queue.", bp); 1949 /* 1950 * If dirty, put on list of dirty buffers; otherwise insert onto list 1951 * of clean buffers. 1952 */ 1953 if (bp->b_flags & B_DELWRI) { 1954 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 1955 switch (vp->v_type) { 1956 case VDIR: 1957 delay = dirdelay; 1958 break; 1959 case VCHR: 1960 delay = metadelay; 1961 break; 1962 default: 1963 delay = filedelay; 1964 } 1965 vn_syncer_add_to_worklist(bo, delay); 1966 } 1967 buf_vlist_add(bp, bo, BX_VNDIRTY); 1968 } else { 1969 buf_vlist_add(bp, bo, BX_VNCLEAN); 1970 1971 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 1972 mtx_lock(&sync_mtx); 1973 LIST_REMOVE(bo, bo_synclist); 1974 syncer_worklist_len--; 1975 mtx_unlock(&sync_mtx); 1976 bo->bo_flag &= ~BO_ONWORKLST; 1977 } 1978 } 1979 #ifdef INVARIANTS 1980 bv = &bo->bo_clean; 1981 bp = TAILQ_FIRST(&bv->bv_hd); 1982 KASSERT(bp == NULL || bp->b_bufobj == bo, 1983 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1984 bp = TAILQ_LAST(&bv->bv_hd, buflists); 1985 KASSERT(bp == NULL || bp->b_bufobj == bo, 1986 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1987 bv = &bo->bo_dirty; 1988 bp = TAILQ_FIRST(&bv->bv_hd); 1989 KASSERT(bp == NULL || bp->b_bufobj == bo, 1990 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1991 bp = TAILQ_LAST(&bv->bv_hd, buflists); 1992 KASSERT(bp == NULL || bp->b_bufobj == bo, 1993 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1994 #endif 1995 BO_UNLOCK(bo); 1996 } 1997 1998 /* 1999 * Increment the use and hold counts on the vnode, taking care to reference 2000 * the driver's usecount if this is a chardev. The vholdl() will remove 2001 * the vnode from the free list if it is presently free. Requires the 2002 * vnode interlock and returns with it held. 2003 */ 2004 static void 2005 v_incr_usecount(struct vnode *vp) 2006 { 2007 2008 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2009 vp->v_usecount++; 2010 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2011 dev_lock(); 2012 vp->v_rdev->si_usecount++; 2013 dev_unlock(); 2014 } 2015 vholdl(vp); 2016 } 2017 2018 /* 2019 * Turn a holdcnt into a use+holdcnt such that only one call to 2020 * v_decr_usecount is needed. 2021 */ 2022 static void 2023 v_upgrade_usecount(struct vnode *vp) 2024 { 2025 2026 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2027 vp->v_usecount++; 2028 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2029 dev_lock(); 2030 vp->v_rdev->si_usecount++; 2031 dev_unlock(); 2032 } 2033 } 2034 2035 /* 2036 * Decrement the vnode use and hold count along with the driver's usecount 2037 * if this is a chardev. The vdropl() below releases the vnode interlock 2038 * as it may free the vnode. 2039 */ 2040 static void 2041 v_decr_usecount(struct vnode *vp) 2042 { 2043 2044 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2045 VNASSERT(vp->v_usecount > 0, vp, 2046 ("v_decr_usecount: negative usecount")); 2047 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2048 vp->v_usecount--; 2049 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2050 dev_lock(); 2051 vp->v_rdev->si_usecount--; 2052 dev_unlock(); 2053 } 2054 vdropl(vp); 2055 } 2056 2057 /* 2058 * Decrement only the use count and driver use count. This is intended to 2059 * be paired with a follow on vdropl() to release the remaining hold count. 2060 * In this way we may vgone() a vnode with a 0 usecount without risk of 2061 * having it end up on a free list because the hold count is kept above 0. 2062 */ 2063 static void 2064 v_decr_useonly(struct vnode *vp) 2065 { 2066 2067 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2068 VNASSERT(vp->v_usecount > 0, vp, 2069 ("v_decr_useonly: negative usecount")); 2070 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2071 vp->v_usecount--; 2072 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2073 dev_lock(); 2074 vp->v_rdev->si_usecount--; 2075 dev_unlock(); 2076 } 2077 } 2078 2079 /* 2080 * Grab a particular vnode from the free list, increment its 2081 * reference count and lock it. VI_DOOMED is set if the vnode 2082 * is being destroyed. Only callers who specify LK_RETRY will 2083 * see doomed vnodes. If inactive processing was delayed in 2084 * vput try to do it here. 2085 */ 2086 int 2087 vget(struct vnode *vp, int flags, struct thread *td) 2088 { 2089 int error; 2090 2091 error = 0; 2092 VFS_ASSERT_GIANT(vp->v_mount); 2093 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2094 ("vget: invalid lock operation")); 2095 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 2096 2097 if ((flags & LK_INTERLOCK) == 0) 2098 VI_LOCK(vp); 2099 vholdl(vp); 2100 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) { 2101 vdrop(vp); 2102 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2103 vp); 2104 return (error); 2105 } 2106 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0) 2107 panic("vget: vn_lock failed to return ENOENT\n"); 2108 VI_LOCK(vp); 2109 /* Upgrade our holdcnt to a usecount. */ 2110 v_upgrade_usecount(vp); 2111 /* 2112 * We don't guarantee that any particular close will 2113 * trigger inactive processing so just make a best effort 2114 * here at preventing a reference to a removed file. If 2115 * we don't succeed no harm is done. 2116 */ 2117 if (vp->v_iflag & VI_OWEINACT) { 2118 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && 2119 (flags & LK_NOWAIT) == 0) 2120 vinactive(vp, td); 2121 vp->v_iflag &= ~VI_OWEINACT; 2122 } 2123 VI_UNLOCK(vp); 2124 return (0); 2125 } 2126 2127 /* 2128 * Increase the reference count of a vnode. 2129 */ 2130 void 2131 vref(struct vnode *vp) 2132 { 2133 2134 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2135 VI_LOCK(vp); 2136 v_incr_usecount(vp); 2137 VI_UNLOCK(vp); 2138 } 2139 2140 /* 2141 * Return reference count of a vnode. 2142 * 2143 * The results of this call are only guaranteed when some mechanism other 2144 * than the VI lock is used to stop other processes from gaining references 2145 * to the vnode. This may be the case if the caller holds the only reference. 2146 * This is also useful when stale data is acceptable as race conditions may 2147 * be accounted for by some other means. 2148 */ 2149 int 2150 vrefcnt(struct vnode *vp) 2151 { 2152 int usecnt; 2153 2154 VI_LOCK(vp); 2155 usecnt = vp->v_usecount; 2156 VI_UNLOCK(vp); 2157 2158 return (usecnt); 2159 } 2160 2161 2162 /* 2163 * Vnode put/release. 2164 * If count drops to zero, call inactive routine and return to freelist. 2165 */ 2166 void 2167 vrele(struct vnode *vp) 2168 { 2169 struct thread *td = curthread; /* XXX */ 2170 2171 KASSERT(vp != NULL, ("vrele: null vp")); 2172 VFS_ASSERT_GIANT(vp->v_mount); 2173 2174 VI_LOCK(vp); 2175 2176 /* Skip this v_writecount check if we're going to panic below. */ 2177 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp, 2178 ("vrele: missed vn_close")); 2179 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2180 2181 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2182 vp->v_usecount == 1)) { 2183 v_decr_usecount(vp); 2184 return; 2185 } 2186 if (vp->v_usecount != 1) { 2187 #ifdef DIAGNOSTIC 2188 vprint("vrele: negative ref count", vp); 2189 #endif 2190 VI_UNLOCK(vp); 2191 panic("vrele: negative ref cnt"); 2192 } 2193 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp); 2194 /* 2195 * We want to hold the vnode until the inactive finishes to 2196 * prevent vgone() races. We drop the use count here and the 2197 * hold count below when we're done. 2198 */ 2199 v_decr_useonly(vp); 2200 /* 2201 * We must call VOP_INACTIVE with the node locked. Mark 2202 * as VI_DOINGINACT to avoid recursion. 2203 */ 2204 vp->v_iflag |= VI_OWEINACT; 2205 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0) { 2206 VI_LOCK(vp); 2207 if (vp->v_usecount > 0) 2208 vp->v_iflag &= ~VI_OWEINACT; 2209 if (vp->v_iflag & VI_OWEINACT) 2210 vinactive(vp, td); 2211 VOP_UNLOCK(vp, 0); 2212 } else { 2213 VI_LOCK(vp); 2214 if (vp->v_usecount > 0) 2215 vp->v_iflag &= ~VI_OWEINACT; 2216 } 2217 vdropl(vp); 2218 } 2219 2220 /* 2221 * Release an already locked vnode. This give the same effects as 2222 * unlock+vrele(), but takes less time and avoids releasing and 2223 * re-aquiring the lock (as vrele() acquires the lock internally.) 2224 */ 2225 void 2226 vput(struct vnode *vp) 2227 { 2228 struct thread *td = curthread; /* XXX */ 2229 int error; 2230 2231 KASSERT(vp != NULL, ("vput: null vp")); 2232 ASSERT_VOP_LOCKED(vp, "vput"); 2233 VFS_ASSERT_GIANT(vp->v_mount); 2234 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2235 VI_LOCK(vp); 2236 /* Skip this v_writecount check if we're going to panic below. */ 2237 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp, 2238 ("vput: missed vn_close")); 2239 error = 0; 2240 2241 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2242 vp->v_usecount == 1)) { 2243 VOP_UNLOCK(vp, 0); 2244 v_decr_usecount(vp); 2245 return; 2246 } 2247 2248 if (vp->v_usecount != 1) { 2249 #ifdef DIAGNOSTIC 2250 vprint("vput: negative ref count", vp); 2251 #endif 2252 panic("vput: negative ref cnt"); 2253 } 2254 CTR2(KTR_VFS, "%s: return to freelist the vnode %p", __func__, vp); 2255 /* 2256 * We want to hold the vnode until the inactive finishes to 2257 * prevent vgone() races. We drop the use count here and the 2258 * hold count below when we're done. 2259 */ 2260 v_decr_useonly(vp); 2261 vp->v_iflag |= VI_OWEINACT; 2262 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 2263 error = VOP_LOCK(vp, LK_UPGRADE|LK_INTERLOCK|LK_NOWAIT); 2264 VI_LOCK(vp); 2265 if (error) { 2266 if (vp->v_usecount > 0) 2267 vp->v_iflag &= ~VI_OWEINACT; 2268 goto done; 2269 } 2270 } 2271 if (vp->v_usecount > 0) 2272 vp->v_iflag &= ~VI_OWEINACT; 2273 if (vp->v_iflag & VI_OWEINACT) 2274 vinactive(vp, td); 2275 VOP_UNLOCK(vp, 0); 2276 done: 2277 vdropl(vp); 2278 } 2279 2280 /* 2281 * Somebody doesn't want the vnode recycled. 2282 */ 2283 void 2284 vhold(struct vnode *vp) 2285 { 2286 2287 VI_LOCK(vp); 2288 vholdl(vp); 2289 VI_UNLOCK(vp); 2290 } 2291 2292 void 2293 vholdl(struct vnode *vp) 2294 { 2295 2296 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2297 vp->v_holdcnt++; 2298 if (VSHOULDBUSY(vp)) 2299 vbusy(vp); 2300 } 2301 2302 /* 2303 * Note that there is one less who cares about this vnode. vdrop() is the 2304 * opposite of vhold(). 2305 */ 2306 void 2307 vdrop(struct vnode *vp) 2308 { 2309 2310 VI_LOCK(vp); 2311 vdropl(vp); 2312 } 2313 2314 /* 2315 * Drop the hold count of the vnode. If this is the last reference to 2316 * the vnode we will free it if it has been vgone'd otherwise it is 2317 * placed on the free list. 2318 */ 2319 void 2320 vdropl(struct vnode *vp) 2321 { 2322 2323 ASSERT_VI_LOCKED(vp, "vdropl"); 2324 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2325 if (vp->v_holdcnt <= 0) 2326 panic("vdrop: holdcnt %d", vp->v_holdcnt); 2327 vp->v_holdcnt--; 2328 if (vp->v_holdcnt == 0) { 2329 if (vp->v_iflag & VI_DOOMED) { 2330 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, 2331 vp); 2332 vdestroy(vp); 2333 return; 2334 } else 2335 vfree(vp); 2336 } 2337 VI_UNLOCK(vp); 2338 } 2339 2340 /* 2341 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 2342 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 2343 * OWEINACT tracks whether a vnode missed a call to inactive due to a 2344 * failed lock upgrade. 2345 */ 2346 static void 2347 vinactive(struct vnode *vp, struct thread *td) 2348 { 2349 2350 ASSERT_VOP_ELOCKED(vp, "vinactive"); 2351 ASSERT_VI_LOCKED(vp, "vinactive"); 2352 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 2353 ("vinactive: recursed on VI_DOINGINACT")); 2354 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2355 vp->v_iflag |= VI_DOINGINACT; 2356 vp->v_iflag &= ~VI_OWEINACT; 2357 VI_UNLOCK(vp); 2358 VOP_INACTIVE(vp, td); 2359 VI_LOCK(vp); 2360 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 2361 ("vinactive: lost VI_DOINGINACT")); 2362 vp->v_iflag &= ~VI_DOINGINACT; 2363 } 2364 2365 /* 2366 * Remove any vnodes in the vnode table belonging to mount point mp. 2367 * 2368 * If FORCECLOSE is not specified, there should not be any active ones, 2369 * return error if any are found (nb: this is a user error, not a 2370 * system error). If FORCECLOSE is specified, detach any active vnodes 2371 * that are found. 2372 * 2373 * If WRITECLOSE is set, only flush out regular file vnodes open for 2374 * writing. 2375 * 2376 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2377 * 2378 * `rootrefs' specifies the base reference count for the root vnode 2379 * of this filesystem. The root vnode is considered busy if its 2380 * v_usecount exceeds this value. On a successful return, vflush(, td) 2381 * will call vrele() on the root vnode exactly rootrefs times. 2382 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2383 * be zero. 2384 */ 2385 #ifdef DIAGNOSTIC 2386 static int busyprt = 0; /* print out busy vnodes */ 2387 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 2388 #endif 2389 2390 int 2391 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td) 2392 { 2393 struct vnode *vp, *mvp, *rootvp = NULL; 2394 struct vattr vattr; 2395 int busy = 0, error; 2396 2397 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 2398 rootrefs, flags); 2399 if (rootrefs > 0) { 2400 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2401 ("vflush: bad args")); 2402 /* 2403 * Get the filesystem root vnode. We can vput() it 2404 * immediately, since with rootrefs > 0, it won't go away. 2405 */ 2406 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp, td)) != 0) { 2407 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 2408 __func__, error); 2409 return (error); 2410 } 2411 vput(rootvp); 2412 2413 } 2414 MNT_ILOCK(mp); 2415 loop: 2416 MNT_VNODE_FOREACH(vp, mp, mvp) { 2417 2418 VI_LOCK(vp); 2419 vholdl(vp); 2420 MNT_IUNLOCK(mp); 2421 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 2422 if (error) { 2423 vdrop(vp); 2424 MNT_ILOCK(mp); 2425 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp); 2426 goto loop; 2427 } 2428 /* 2429 * Skip over a vnodes marked VV_SYSTEM. 2430 */ 2431 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2432 VOP_UNLOCK(vp, 0); 2433 vdrop(vp); 2434 MNT_ILOCK(mp); 2435 continue; 2436 } 2437 /* 2438 * If WRITECLOSE is set, flush out unlinked but still open 2439 * files (even if open only for reading) and regular file 2440 * vnodes open for writing. 2441 */ 2442 if (flags & WRITECLOSE) { 2443 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 2444 VI_LOCK(vp); 2445 2446 if ((vp->v_type == VNON || 2447 (error == 0 && vattr.va_nlink > 0)) && 2448 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2449 VOP_UNLOCK(vp, 0); 2450 vdropl(vp); 2451 MNT_ILOCK(mp); 2452 continue; 2453 } 2454 } else 2455 VI_LOCK(vp); 2456 /* 2457 * With v_usecount == 0, all we need to do is clear out the 2458 * vnode data structures and we are done. 2459 * 2460 * If FORCECLOSE is set, forcibly close the vnode. 2461 */ 2462 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 2463 VNASSERT(vp->v_usecount == 0 || 2464 (vp->v_type != VCHR && vp->v_type != VBLK), vp, 2465 ("device VNODE %p is FORCECLOSED", vp)); 2466 vgonel(vp); 2467 } else { 2468 busy++; 2469 #ifdef DIAGNOSTIC 2470 if (busyprt) 2471 vprint("vflush: busy vnode", vp); 2472 #endif 2473 } 2474 VOP_UNLOCK(vp, 0); 2475 vdropl(vp); 2476 MNT_ILOCK(mp); 2477 } 2478 MNT_IUNLOCK(mp); 2479 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2480 /* 2481 * If just the root vnode is busy, and if its refcount 2482 * is equal to `rootrefs', then go ahead and kill it. 2483 */ 2484 VI_LOCK(rootvp); 2485 KASSERT(busy > 0, ("vflush: not busy")); 2486 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 2487 ("vflush: usecount %d < rootrefs %d", 2488 rootvp->v_usecount, rootrefs)); 2489 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2490 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 2491 vgone(rootvp); 2492 VOP_UNLOCK(rootvp, 0); 2493 busy = 0; 2494 } else 2495 VI_UNLOCK(rootvp); 2496 } 2497 if (busy) { 2498 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 2499 busy); 2500 return (EBUSY); 2501 } 2502 for (; rootrefs > 0; rootrefs--) 2503 vrele(rootvp); 2504 return (0); 2505 } 2506 2507 /* 2508 * Recycle an unused vnode to the front of the free list. 2509 */ 2510 int 2511 vrecycle(struct vnode *vp, struct thread *td) 2512 { 2513 int recycled; 2514 2515 ASSERT_VOP_ELOCKED(vp, "vrecycle"); 2516 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2517 recycled = 0; 2518 VI_LOCK(vp); 2519 if (vp->v_usecount == 0) { 2520 recycled = 1; 2521 vgonel(vp); 2522 } 2523 VI_UNLOCK(vp); 2524 return (recycled); 2525 } 2526 2527 /* 2528 * Eliminate all activity associated with a vnode 2529 * in preparation for reuse. 2530 */ 2531 void 2532 vgone(struct vnode *vp) 2533 { 2534 VI_LOCK(vp); 2535 vgonel(vp); 2536 VI_UNLOCK(vp); 2537 } 2538 2539 /* 2540 * vgone, with the vp interlock held. 2541 */ 2542 void 2543 vgonel(struct vnode *vp) 2544 { 2545 struct thread *td; 2546 int oweinact; 2547 int active; 2548 struct mount *mp; 2549 2550 ASSERT_VOP_ELOCKED(vp, "vgonel"); 2551 ASSERT_VI_LOCKED(vp, "vgonel"); 2552 VNASSERT(vp->v_holdcnt, vp, 2553 ("vgonel: vp %p has no reference.", vp)); 2554 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2555 td = curthread; 2556 2557 /* 2558 * Don't vgonel if we're already doomed. 2559 */ 2560 if (vp->v_iflag & VI_DOOMED) 2561 return; 2562 vp->v_iflag |= VI_DOOMED; 2563 /* 2564 * Check to see if the vnode is in use. If so, we have to call 2565 * VOP_CLOSE() and VOP_INACTIVE(). 2566 */ 2567 active = vp->v_usecount; 2568 oweinact = (vp->v_iflag & VI_OWEINACT); 2569 VI_UNLOCK(vp); 2570 /* 2571 * Clean out any buffers associated with the vnode. 2572 * If the flush fails, just toss the buffers. 2573 */ 2574 mp = NULL; 2575 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 2576 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 2577 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) 2578 vinvalbuf(vp, 0, 0, 0); 2579 2580 /* 2581 * If purging an active vnode, it must be closed and 2582 * deactivated before being reclaimed. 2583 */ 2584 if (active) 2585 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2586 if (oweinact || active) { 2587 VI_LOCK(vp); 2588 if ((vp->v_iflag & VI_DOINGINACT) == 0) 2589 vinactive(vp, td); 2590 VI_UNLOCK(vp); 2591 } 2592 /* 2593 * Reclaim the vnode. 2594 */ 2595 if (VOP_RECLAIM(vp, td)) 2596 panic("vgone: cannot reclaim"); 2597 if (mp != NULL) 2598 vn_finished_secondary_write(mp); 2599 VNASSERT(vp->v_object == NULL, vp, 2600 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag)); 2601 /* 2602 * Clear the advisory locks and wake up waiting threads. 2603 */ 2604 lf_purgelocks(vp, &(vp->v_lockf)); 2605 /* 2606 * Delete from old mount point vnode list. 2607 */ 2608 delmntque(vp); 2609 cache_purge(vp); 2610 /* 2611 * Done with purge, reset to the standard lock and invalidate 2612 * the vnode. 2613 */ 2614 VI_LOCK(vp); 2615 vp->v_vnlock = &vp->v_lock; 2616 vp->v_op = &dead_vnodeops; 2617 vp->v_tag = "none"; 2618 vp->v_type = VBAD; 2619 } 2620 2621 /* 2622 * Calculate the total number of references to a special device. 2623 */ 2624 int 2625 vcount(struct vnode *vp) 2626 { 2627 int count; 2628 2629 dev_lock(); 2630 count = vp->v_rdev->si_usecount; 2631 dev_unlock(); 2632 return (count); 2633 } 2634 2635 /* 2636 * Same as above, but using the struct cdev *as argument 2637 */ 2638 int 2639 count_dev(struct cdev *dev) 2640 { 2641 int count; 2642 2643 dev_lock(); 2644 count = dev->si_usecount; 2645 dev_unlock(); 2646 return(count); 2647 } 2648 2649 /* 2650 * Print out a description of a vnode. 2651 */ 2652 static char *typename[] = 2653 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 2654 "VMARKER"}; 2655 2656 void 2657 vn_printf(struct vnode *vp, const char *fmt, ...) 2658 { 2659 va_list ap; 2660 char buf[256], buf2[16]; 2661 u_long flags; 2662 2663 va_start(ap, fmt); 2664 vprintf(fmt, ap); 2665 va_end(ap); 2666 printf("%p: ", (void *)vp); 2667 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]); 2668 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n", 2669 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere); 2670 buf[0] = '\0'; 2671 buf[1] = '\0'; 2672 if (vp->v_vflag & VV_ROOT) 2673 strlcat(buf, "|VV_ROOT", sizeof(buf)); 2674 if (vp->v_vflag & VV_ISTTY) 2675 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 2676 if (vp->v_vflag & VV_NOSYNC) 2677 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 2678 if (vp->v_vflag & VV_CACHEDLABEL) 2679 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 2680 if (vp->v_vflag & VV_TEXT) 2681 strlcat(buf, "|VV_TEXT", sizeof(buf)); 2682 if (vp->v_vflag & VV_COPYONWRITE) 2683 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 2684 if (vp->v_vflag & VV_SYSTEM) 2685 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 2686 if (vp->v_vflag & VV_PROCDEP) 2687 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 2688 if (vp->v_vflag & VV_NOKNOTE) 2689 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 2690 if (vp->v_vflag & VV_DELETED) 2691 strlcat(buf, "|VV_DELETED", sizeof(buf)); 2692 if (vp->v_vflag & VV_MD) 2693 strlcat(buf, "|VV_MD", sizeof(buf)); 2694 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | 2695 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP | 2696 VV_NOKNOTE | VV_DELETED | VV_MD); 2697 if (flags != 0) { 2698 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 2699 strlcat(buf, buf2, sizeof(buf)); 2700 } 2701 if (vp->v_iflag & VI_MOUNT) 2702 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 2703 if (vp->v_iflag & VI_AGE) 2704 strlcat(buf, "|VI_AGE", sizeof(buf)); 2705 if (vp->v_iflag & VI_DOOMED) 2706 strlcat(buf, "|VI_DOOMED", sizeof(buf)); 2707 if (vp->v_iflag & VI_FREE) 2708 strlcat(buf, "|VI_FREE", sizeof(buf)); 2709 if (vp->v_iflag & VI_OBJDIRTY) 2710 strlcat(buf, "|VI_OBJDIRTY", sizeof(buf)); 2711 if (vp->v_iflag & VI_DOINGINACT) 2712 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 2713 if (vp->v_iflag & VI_OWEINACT) 2714 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 2715 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE | 2716 VI_OBJDIRTY | VI_DOINGINACT | VI_OWEINACT); 2717 if (flags != 0) { 2718 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 2719 strlcat(buf, buf2, sizeof(buf)); 2720 } 2721 printf(" flags (%s)\n", buf + 1); 2722 if (mtx_owned(VI_MTX(vp))) 2723 printf(" VI_LOCKed"); 2724 if (vp->v_object != NULL) 2725 printf(" v_object %p ref %d pages %d\n", 2726 vp->v_object, vp->v_object->ref_count, 2727 vp->v_object->resident_page_count); 2728 printf(" "); 2729 lockmgr_printinfo(vp->v_vnlock); 2730 if (vp->v_data != NULL) 2731 VOP_PRINT(vp); 2732 } 2733 2734 #ifdef DDB 2735 /* 2736 * List all of the locked vnodes in the system. 2737 * Called when debugging the kernel. 2738 */ 2739 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2740 { 2741 struct mount *mp, *nmp; 2742 struct vnode *vp; 2743 2744 /* 2745 * Note: because this is DDB, we can't obey the locking semantics 2746 * for these structures, which means we could catch an inconsistent 2747 * state and dereference a nasty pointer. Not much to be done 2748 * about that. 2749 */ 2750 db_printf("Locked vnodes\n"); 2751 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2752 nmp = TAILQ_NEXT(mp, mnt_list); 2753 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2754 if (vp->v_type != VMARKER && 2755 VOP_ISLOCKED(vp)) 2756 vprint("", vp); 2757 } 2758 nmp = TAILQ_NEXT(mp, mnt_list); 2759 } 2760 } 2761 2762 /* 2763 * Show details about the given vnode. 2764 */ 2765 DB_SHOW_COMMAND(vnode, db_show_vnode) 2766 { 2767 struct vnode *vp; 2768 2769 if (!have_addr) 2770 return; 2771 vp = (struct vnode *)addr; 2772 vn_printf(vp, "vnode "); 2773 } 2774 2775 /* 2776 * Show details about the given mount point. 2777 */ 2778 DB_SHOW_COMMAND(mount, db_show_mount) 2779 { 2780 struct mount *mp; 2781 struct statfs *sp; 2782 struct vnode *vp; 2783 char buf[512]; 2784 u_int flags; 2785 2786 if (!have_addr) { 2787 /* No address given, print short info about all mount points. */ 2788 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 2789 db_printf("%p %s on %s (%s)\n", mp, 2790 mp->mnt_stat.f_mntfromname, 2791 mp->mnt_stat.f_mntonname, 2792 mp->mnt_stat.f_fstypename); 2793 if (db_pager_quit) 2794 break; 2795 } 2796 db_printf("\nMore info: show mount <addr>\n"); 2797 return; 2798 } 2799 2800 mp = (struct mount *)addr; 2801 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 2802 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 2803 2804 buf[0] = '\0'; 2805 flags = mp->mnt_flag; 2806 #define MNT_FLAG(flag) do { \ 2807 if (flags & (flag)) { \ 2808 if (buf[0] != '\0') \ 2809 strlcat(buf, ", ", sizeof(buf)); \ 2810 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 2811 flags &= ~(flag); \ 2812 } \ 2813 } while (0) 2814 MNT_FLAG(MNT_RDONLY); 2815 MNT_FLAG(MNT_SYNCHRONOUS); 2816 MNT_FLAG(MNT_NOEXEC); 2817 MNT_FLAG(MNT_NOSUID); 2818 MNT_FLAG(MNT_UNION); 2819 MNT_FLAG(MNT_ASYNC); 2820 MNT_FLAG(MNT_SUIDDIR); 2821 MNT_FLAG(MNT_SOFTDEP); 2822 MNT_FLAG(MNT_NOSYMFOLLOW); 2823 MNT_FLAG(MNT_GJOURNAL); 2824 MNT_FLAG(MNT_MULTILABEL); 2825 MNT_FLAG(MNT_ACLS); 2826 MNT_FLAG(MNT_NOATIME); 2827 MNT_FLAG(MNT_NOCLUSTERR); 2828 MNT_FLAG(MNT_NOCLUSTERW); 2829 MNT_FLAG(MNT_EXRDONLY); 2830 MNT_FLAG(MNT_EXPORTED); 2831 MNT_FLAG(MNT_DEFEXPORTED); 2832 MNT_FLAG(MNT_EXPORTANON); 2833 MNT_FLAG(MNT_EXKERB); 2834 MNT_FLAG(MNT_EXPUBLIC); 2835 MNT_FLAG(MNT_LOCAL); 2836 MNT_FLAG(MNT_QUOTA); 2837 MNT_FLAG(MNT_ROOTFS); 2838 MNT_FLAG(MNT_USER); 2839 MNT_FLAG(MNT_IGNORE); 2840 MNT_FLAG(MNT_UPDATE); 2841 MNT_FLAG(MNT_DELEXPORT); 2842 MNT_FLAG(MNT_RELOAD); 2843 MNT_FLAG(MNT_FORCE); 2844 MNT_FLAG(MNT_SNAPSHOT); 2845 MNT_FLAG(MNT_BYFSID); 2846 #undef MNT_FLAG 2847 if (flags != 0) { 2848 if (buf[0] != '\0') 2849 strlcat(buf, ", ", sizeof(buf)); 2850 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 2851 "0x%08x", flags); 2852 } 2853 db_printf(" mnt_flag = %s\n", buf); 2854 2855 buf[0] = '\0'; 2856 flags = mp->mnt_kern_flag; 2857 #define MNT_KERN_FLAG(flag) do { \ 2858 if (flags & (flag)) { \ 2859 if (buf[0] != '\0') \ 2860 strlcat(buf, ", ", sizeof(buf)); \ 2861 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 2862 flags &= ~(flag); \ 2863 } \ 2864 } while (0) 2865 MNT_KERN_FLAG(MNTK_UNMOUNTF); 2866 MNT_KERN_FLAG(MNTK_ASYNC); 2867 MNT_KERN_FLAG(MNTK_SOFTDEP); 2868 MNT_KERN_FLAG(MNTK_NOINSMNTQ); 2869 MNT_KERN_FLAG(MNTK_UNMOUNT); 2870 MNT_KERN_FLAG(MNTK_MWAIT); 2871 MNT_KERN_FLAG(MNTK_SUSPEND); 2872 MNT_KERN_FLAG(MNTK_SUSPEND2); 2873 MNT_KERN_FLAG(MNTK_SUSPENDED); 2874 MNT_KERN_FLAG(MNTK_MPSAFE); 2875 MNT_KERN_FLAG(MNTK_NOKNOTE); 2876 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 2877 #undef MNT_KERN_FLAG 2878 if (flags != 0) { 2879 if (buf[0] != '\0') 2880 strlcat(buf, ", ", sizeof(buf)); 2881 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 2882 "0x%08x", flags); 2883 } 2884 db_printf(" mnt_kern_flag = %s\n", buf); 2885 2886 sp = &mp->mnt_stat; 2887 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 2888 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 2889 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 2890 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 2891 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 2892 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 2893 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 2894 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 2895 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 2896 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 2897 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 2898 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 2899 2900 db_printf(" mnt_cred = { uid=%u ruid=%u", 2901 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 2902 if (mp->mnt_cred->cr_prison != NULL) 2903 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 2904 db_printf(" }\n"); 2905 db_printf(" mnt_ref = %d\n", mp->mnt_ref); 2906 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 2907 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 2908 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount); 2909 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync); 2910 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 2911 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 2912 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 2913 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 2914 db_printf(" mnt_secondary_accwrites = %d\n", 2915 mp->mnt_secondary_accwrites); 2916 db_printf(" mnt_gjprovider = %s\n", 2917 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 2918 db_printf("\n"); 2919 2920 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2921 if (vp->v_type != VMARKER) { 2922 vn_printf(vp, "vnode "); 2923 if (db_pager_quit) 2924 break; 2925 } 2926 } 2927 } 2928 #endif /* DDB */ 2929 2930 /* 2931 * Fill in a struct xvfsconf based on a struct vfsconf. 2932 */ 2933 static void 2934 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2935 { 2936 2937 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2938 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2939 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2940 xvfsp->vfc_flags = vfsp->vfc_flags; 2941 /* 2942 * These are unused in userland, we keep them 2943 * to not break binary compatibility. 2944 */ 2945 xvfsp->vfc_vfsops = NULL; 2946 xvfsp->vfc_next = NULL; 2947 } 2948 2949 /* 2950 * Top level filesystem related information gathering. 2951 */ 2952 static int 2953 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2954 { 2955 struct vfsconf *vfsp; 2956 struct xvfsconf xvfsp; 2957 int error; 2958 2959 error = 0; 2960 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 2961 bzero(&xvfsp, sizeof(xvfsp)); 2962 vfsconf2x(vfsp, &xvfsp); 2963 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp); 2964 if (error) 2965 break; 2966 } 2967 return (error); 2968 } 2969 2970 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2971 "S,xvfsconf", "List of all configured filesystems"); 2972 2973 #ifndef BURN_BRIDGES 2974 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2975 2976 static int 2977 vfs_sysctl(SYSCTL_HANDLER_ARGS) 2978 { 2979 int *name = (int *)arg1 - 1; /* XXX */ 2980 u_int namelen = arg2 + 1; /* XXX */ 2981 struct vfsconf *vfsp; 2982 struct xvfsconf xvfsp; 2983 2984 printf("WARNING: userland calling deprecated sysctl, " 2985 "please rebuild world\n"); 2986 2987 #if 1 || defined(COMPAT_PRELITE2) 2988 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2989 if (namelen == 1) 2990 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2991 #endif 2992 2993 switch (name[1]) { 2994 case VFS_MAXTYPENUM: 2995 if (namelen != 2) 2996 return (ENOTDIR); 2997 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2998 case VFS_CONF: 2999 if (namelen != 3) 3000 return (ENOTDIR); /* overloaded */ 3001 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 3002 if (vfsp->vfc_typenum == name[2]) 3003 break; 3004 if (vfsp == NULL) 3005 return (EOPNOTSUPP); 3006 bzero(&xvfsp, sizeof(xvfsp)); 3007 vfsconf2x(vfsp, &xvfsp); 3008 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3009 } 3010 return (EOPNOTSUPP); 3011 } 3012 3013 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, 3014 vfs_sysctl, "Generic filesystem"); 3015 3016 #if 1 || defined(COMPAT_PRELITE2) 3017 3018 static int 3019 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 3020 { 3021 int error; 3022 struct vfsconf *vfsp; 3023 struct ovfsconf ovfs; 3024 3025 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3026 bzero(&ovfs, sizeof(ovfs)); 3027 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 3028 strcpy(ovfs.vfc_name, vfsp->vfc_name); 3029 ovfs.vfc_index = vfsp->vfc_typenum; 3030 ovfs.vfc_refcount = vfsp->vfc_refcount; 3031 ovfs.vfc_flags = vfsp->vfc_flags; 3032 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 3033 if (error) 3034 return error; 3035 } 3036 return 0; 3037 } 3038 3039 #endif /* 1 || COMPAT_PRELITE2 */ 3040 #endif /* !BURN_BRIDGES */ 3041 3042 #define KINFO_VNODESLOP 10 3043 #ifdef notyet 3044 /* 3045 * Dump vnode list (via sysctl). 3046 */ 3047 /* ARGSUSED */ 3048 static int 3049 sysctl_vnode(SYSCTL_HANDLER_ARGS) 3050 { 3051 struct xvnode *xvn; 3052 struct mount *mp; 3053 struct vnode *vp; 3054 int error, len, n; 3055 3056 /* 3057 * Stale numvnodes access is not fatal here. 3058 */ 3059 req->lock = 0; 3060 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 3061 if (!req->oldptr) 3062 /* Make an estimate */ 3063 return (SYSCTL_OUT(req, 0, len)); 3064 3065 error = sysctl_wire_old_buffer(req, 0); 3066 if (error != 0) 3067 return (error); 3068 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 3069 n = 0; 3070 mtx_lock(&mountlist_mtx); 3071 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3072 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 3073 continue; 3074 MNT_ILOCK(mp); 3075 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3076 if (n == len) 3077 break; 3078 vref(vp); 3079 xvn[n].xv_size = sizeof *xvn; 3080 xvn[n].xv_vnode = vp; 3081 xvn[n].xv_id = 0; /* XXX compat */ 3082 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3083 XV_COPY(usecount); 3084 XV_COPY(writecount); 3085 XV_COPY(holdcnt); 3086 XV_COPY(mount); 3087 XV_COPY(numoutput); 3088 XV_COPY(type); 3089 #undef XV_COPY 3090 xvn[n].xv_flag = vp->v_vflag; 3091 3092 switch (vp->v_type) { 3093 case VREG: 3094 case VDIR: 3095 case VLNK: 3096 break; 3097 case VBLK: 3098 case VCHR: 3099 if (vp->v_rdev == NULL) { 3100 vrele(vp); 3101 continue; 3102 } 3103 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3104 break; 3105 case VSOCK: 3106 xvn[n].xv_socket = vp->v_socket; 3107 break; 3108 case VFIFO: 3109 xvn[n].xv_fifo = vp->v_fifoinfo; 3110 break; 3111 case VNON: 3112 case VBAD: 3113 default: 3114 /* shouldn't happen? */ 3115 vrele(vp); 3116 continue; 3117 } 3118 vrele(vp); 3119 ++n; 3120 } 3121 MNT_IUNLOCK(mp); 3122 mtx_lock(&mountlist_mtx); 3123 vfs_unbusy(mp); 3124 if (n == len) 3125 break; 3126 } 3127 mtx_unlock(&mountlist_mtx); 3128 3129 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 3130 free(xvn, M_TEMP); 3131 return (error); 3132 } 3133 3134 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 3135 0, 0, sysctl_vnode, "S,xvnode", ""); 3136 #endif 3137 3138 /* 3139 * Unmount all filesystems. The list is traversed in reverse order 3140 * of mounting to avoid dependencies. 3141 */ 3142 void 3143 vfs_unmountall(void) 3144 { 3145 struct mount *mp; 3146 struct thread *td; 3147 int error; 3148 3149 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread")); 3150 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 3151 td = curthread; 3152 3153 /* 3154 * Since this only runs when rebooting, it is not interlocked. 3155 */ 3156 while(!TAILQ_EMPTY(&mountlist)) { 3157 mp = TAILQ_LAST(&mountlist, mntlist); 3158 error = dounmount(mp, MNT_FORCE, td); 3159 if (error) { 3160 TAILQ_REMOVE(&mountlist, mp, mnt_list); 3161 /* 3162 * XXX: Due to the way in which we mount the root 3163 * file system off of devfs, devfs will generate a 3164 * "busy" warning when we try to unmount it before 3165 * the root. Don't print a warning as a result in 3166 * order to avoid false positive errors that may 3167 * cause needless upset. 3168 */ 3169 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) { 3170 printf("unmount of %s failed (", 3171 mp->mnt_stat.f_mntonname); 3172 if (error == EBUSY) 3173 printf("BUSY)\n"); 3174 else 3175 printf("%d)\n", error); 3176 } 3177 } else { 3178 /* The unmount has removed mp from the mountlist */ 3179 } 3180 } 3181 } 3182 3183 /* 3184 * perform msync on all vnodes under a mount point 3185 * the mount point must be locked. 3186 */ 3187 void 3188 vfs_msync(struct mount *mp, int flags) 3189 { 3190 struct vnode *vp, *mvp; 3191 struct vm_object *obj; 3192 3193 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 3194 MNT_ILOCK(mp); 3195 MNT_VNODE_FOREACH(vp, mp, mvp) { 3196 VI_LOCK(vp); 3197 if ((vp->v_iflag & VI_OBJDIRTY) && 3198 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) { 3199 MNT_IUNLOCK(mp); 3200 if (!vget(vp, 3201 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 3202 curthread)) { 3203 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 3204 vput(vp); 3205 MNT_ILOCK(mp); 3206 continue; 3207 } 3208 3209 obj = vp->v_object; 3210 if (obj != NULL) { 3211 VM_OBJECT_LOCK(obj); 3212 vm_object_page_clean(obj, 0, 0, 3213 flags == MNT_WAIT ? 3214 OBJPC_SYNC : OBJPC_NOSYNC); 3215 VM_OBJECT_UNLOCK(obj); 3216 } 3217 vput(vp); 3218 } 3219 MNT_ILOCK(mp); 3220 } else 3221 VI_UNLOCK(vp); 3222 } 3223 MNT_IUNLOCK(mp); 3224 } 3225 3226 /* 3227 * Mark a vnode as free, putting it up for recycling. 3228 */ 3229 static void 3230 vfree(struct vnode *vp) 3231 { 3232 3233 ASSERT_VI_LOCKED(vp, "vfree"); 3234 mtx_lock(&vnode_free_list_mtx); 3235 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed.")); 3236 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free")); 3237 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't")); 3238 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp, 3239 ("vfree: Freeing doomed vnode")); 3240 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3241 if (vp->v_iflag & VI_AGE) { 3242 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 3243 } else { 3244 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 3245 } 3246 freevnodes++; 3247 vp->v_iflag &= ~VI_AGE; 3248 vp->v_iflag |= VI_FREE; 3249 mtx_unlock(&vnode_free_list_mtx); 3250 } 3251 3252 /* 3253 * Opposite of vfree() - mark a vnode as in use. 3254 */ 3255 static void 3256 vbusy(struct vnode *vp) 3257 { 3258 ASSERT_VI_LOCKED(vp, "vbusy"); 3259 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free")); 3260 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed.")); 3261 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3262 3263 mtx_lock(&vnode_free_list_mtx); 3264 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 3265 freevnodes--; 3266 vp->v_iflag &= ~(VI_FREE|VI_AGE); 3267 mtx_unlock(&vnode_free_list_mtx); 3268 } 3269 3270 static void 3271 destroy_vpollinfo(struct vpollinfo *vi) 3272 { 3273 knlist_destroy(&vi->vpi_selinfo.si_note); 3274 mtx_destroy(&vi->vpi_lock); 3275 uma_zfree(vnodepoll_zone, vi); 3276 } 3277 3278 /* 3279 * Initalize per-vnode helper structure to hold poll-related state. 3280 */ 3281 void 3282 v_addpollinfo(struct vnode *vp) 3283 { 3284 struct vpollinfo *vi; 3285 3286 if (vp->v_pollinfo != NULL) 3287 return; 3288 vi = uma_zalloc(vnodepoll_zone, M_WAITOK); 3289 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 3290 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 3291 vfs_knlunlock, vfs_knllocked); 3292 VI_LOCK(vp); 3293 if (vp->v_pollinfo != NULL) { 3294 VI_UNLOCK(vp); 3295 destroy_vpollinfo(vi); 3296 return; 3297 } 3298 vp->v_pollinfo = vi; 3299 VI_UNLOCK(vp); 3300 } 3301 3302 /* 3303 * Record a process's interest in events which might happen to 3304 * a vnode. Because poll uses the historic select-style interface 3305 * internally, this routine serves as both the ``check for any 3306 * pending events'' and the ``record my interest in future events'' 3307 * functions. (These are done together, while the lock is held, 3308 * to avoid race conditions.) 3309 */ 3310 int 3311 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 3312 { 3313 3314 v_addpollinfo(vp); 3315 mtx_lock(&vp->v_pollinfo->vpi_lock); 3316 if (vp->v_pollinfo->vpi_revents & events) { 3317 /* 3318 * This leaves events we are not interested 3319 * in available for the other process which 3320 * which presumably had requested them 3321 * (otherwise they would never have been 3322 * recorded). 3323 */ 3324 events &= vp->v_pollinfo->vpi_revents; 3325 vp->v_pollinfo->vpi_revents &= ~events; 3326 3327 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3328 return (events); 3329 } 3330 vp->v_pollinfo->vpi_events |= events; 3331 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3332 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3333 return (0); 3334 } 3335 3336 /* 3337 * Routine to create and manage a filesystem syncer vnode. 3338 */ 3339 #define sync_close ((int (*)(struct vop_close_args *))nullop) 3340 static int sync_fsync(struct vop_fsync_args *); 3341 static int sync_inactive(struct vop_inactive_args *); 3342 static int sync_reclaim(struct vop_reclaim_args *); 3343 3344 static struct vop_vector sync_vnodeops = { 3345 .vop_bypass = VOP_EOPNOTSUPP, 3346 .vop_close = sync_close, /* close */ 3347 .vop_fsync = sync_fsync, /* fsync */ 3348 .vop_inactive = sync_inactive, /* inactive */ 3349 .vop_reclaim = sync_reclaim, /* reclaim */ 3350 .vop_lock1 = vop_stdlock, /* lock */ 3351 .vop_unlock = vop_stdunlock, /* unlock */ 3352 .vop_islocked = vop_stdislocked, /* islocked */ 3353 }; 3354 3355 /* 3356 * Create a new filesystem syncer vnode for the specified mount point. 3357 */ 3358 int 3359 vfs_allocate_syncvnode(struct mount *mp) 3360 { 3361 struct vnode *vp; 3362 struct bufobj *bo; 3363 static long start, incr, next; 3364 int error; 3365 3366 /* Allocate a new vnode */ 3367 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) { 3368 mp->mnt_syncer = NULL; 3369 return (error); 3370 } 3371 vp->v_type = VNON; 3372 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3373 vp->v_vflag |= VV_FORCEINSMQ; 3374 error = insmntque(vp, mp); 3375 if (error != 0) 3376 panic("vfs_allocate_syncvnode: insmntque failed"); 3377 vp->v_vflag &= ~VV_FORCEINSMQ; 3378 VOP_UNLOCK(vp, 0); 3379 /* 3380 * Place the vnode onto the syncer worklist. We attempt to 3381 * scatter them about on the list so that they will go off 3382 * at evenly distributed times even if all the filesystems 3383 * are mounted at once. 3384 */ 3385 next += incr; 3386 if (next == 0 || next > syncer_maxdelay) { 3387 start /= 2; 3388 incr /= 2; 3389 if (start == 0) { 3390 start = syncer_maxdelay / 2; 3391 incr = syncer_maxdelay; 3392 } 3393 next = start; 3394 } 3395 bo = &vp->v_bufobj; 3396 BO_LOCK(bo); 3397 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 3398 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 3399 mtx_lock(&sync_mtx); 3400 sync_vnode_count++; 3401 mtx_unlock(&sync_mtx); 3402 BO_UNLOCK(bo); 3403 mp->mnt_syncer = vp; 3404 return (0); 3405 } 3406 3407 /* 3408 * Do a lazy sync of the filesystem. 3409 */ 3410 static int 3411 sync_fsync(struct vop_fsync_args *ap) 3412 { 3413 struct vnode *syncvp = ap->a_vp; 3414 struct mount *mp = syncvp->v_mount; 3415 int error; 3416 struct bufobj *bo; 3417 3418 /* 3419 * We only need to do something if this is a lazy evaluation. 3420 */ 3421 if (ap->a_waitfor != MNT_LAZY) 3422 return (0); 3423 3424 /* 3425 * Move ourselves to the back of the sync list. 3426 */ 3427 bo = &syncvp->v_bufobj; 3428 BO_LOCK(bo); 3429 vn_syncer_add_to_worklist(bo, syncdelay); 3430 BO_UNLOCK(bo); 3431 3432 /* 3433 * Walk the list of vnodes pushing all that are dirty and 3434 * not already on the sync list. 3435 */ 3436 mtx_lock(&mountlist_mtx); 3437 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) { 3438 mtx_unlock(&mountlist_mtx); 3439 return (0); 3440 } 3441 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3442 vfs_unbusy(mp); 3443 return (0); 3444 } 3445 MNT_ILOCK(mp); 3446 mp->mnt_noasync++; 3447 mp->mnt_kern_flag &= ~MNTK_ASYNC; 3448 MNT_IUNLOCK(mp); 3449 vfs_msync(mp, MNT_NOWAIT); 3450 error = VFS_SYNC(mp, MNT_LAZY, ap->a_td); 3451 MNT_ILOCK(mp); 3452 mp->mnt_noasync--; 3453 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 3454 mp->mnt_kern_flag |= MNTK_ASYNC; 3455 MNT_IUNLOCK(mp); 3456 vn_finished_write(mp); 3457 vfs_unbusy(mp); 3458 return (error); 3459 } 3460 3461 /* 3462 * The syncer vnode is no referenced. 3463 */ 3464 static int 3465 sync_inactive(struct vop_inactive_args *ap) 3466 { 3467 3468 vgone(ap->a_vp); 3469 return (0); 3470 } 3471 3472 /* 3473 * The syncer vnode is no longer needed and is being decommissioned. 3474 * 3475 * Modifications to the worklist must be protected by sync_mtx. 3476 */ 3477 static int 3478 sync_reclaim(struct vop_reclaim_args *ap) 3479 { 3480 struct vnode *vp = ap->a_vp; 3481 struct bufobj *bo; 3482 3483 bo = &vp->v_bufobj; 3484 BO_LOCK(bo); 3485 vp->v_mount->mnt_syncer = NULL; 3486 if (bo->bo_flag & BO_ONWORKLST) { 3487 mtx_lock(&sync_mtx); 3488 LIST_REMOVE(bo, bo_synclist); 3489 syncer_worklist_len--; 3490 sync_vnode_count--; 3491 mtx_unlock(&sync_mtx); 3492 bo->bo_flag &= ~BO_ONWORKLST; 3493 } 3494 BO_UNLOCK(bo); 3495 3496 return (0); 3497 } 3498 3499 /* 3500 * Check if vnode represents a disk device 3501 */ 3502 int 3503 vn_isdisk(struct vnode *vp, int *errp) 3504 { 3505 int error; 3506 3507 error = 0; 3508 dev_lock(); 3509 if (vp->v_type != VCHR) 3510 error = ENOTBLK; 3511 else if (vp->v_rdev == NULL) 3512 error = ENXIO; 3513 else if (vp->v_rdev->si_devsw == NULL) 3514 error = ENXIO; 3515 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 3516 error = ENOTBLK; 3517 dev_unlock(); 3518 if (errp != NULL) 3519 *errp = error; 3520 return (error == 0); 3521 } 3522 3523 /* 3524 * Common filesystem object access control check routine. Accepts a 3525 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3526 * and optional call-by-reference privused argument allowing vaccess() 3527 * to indicate to the caller whether privilege was used to satisfy the 3528 * request (obsoleted). Returns 0 on success, or an errno on failure. 3529 * 3530 * The ifdef'd CAPABILITIES version is here for reference, but is not 3531 * actually used. 3532 */ 3533 int 3534 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 3535 accmode_t accmode, struct ucred *cred, int *privused) 3536 { 3537 accmode_t dac_granted; 3538 accmode_t priv_granted; 3539 3540 /* 3541 * Look for a normal, non-privileged way to access the file/directory 3542 * as requested. If it exists, go with that. 3543 */ 3544 3545 if (privused != NULL) 3546 *privused = 0; 3547 3548 dac_granted = 0; 3549 3550 /* Check the owner. */ 3551 if (cred->cr_uid == file_uid) { 3552 dac_granted |= VADMIN; 3553 if (file_mode & S_IXUSR) 3554 dac_granted |= VEXEC; 3555 if (file_mode & S_IRUSR) 3556 dac_granted |= VREAD; 3557 if (file_mode & S_IWUSR) 3558 dac_granted |= (VWRITE | VAPPEND); 3559 3560 if ((accmode & dac_granted) == accmode) 3561 return (0); 3562 3563 goto privcheck; 3564 } 3565 3566 /* Otherwise, check the groups (first match) */ 3567 if (groupmember(file_gid, cred)) { 3568 if (file_mode & S_IXGRP) 3569 dac_granted |= VEXEC; 3570 if (file_mode & S_IRGRP) 3571 dac_granted |= VREAD; 3572 if (file_mode & S_IWGRP) 3573 dac_granted |= (VWRITE | VAPPEND); 3574 3575 if ((accmode & dac_granted) == accmode) 3576 return (0); 3577 3578 goto privcheck; 3579 } 3580 3581 /* Otherwise, check everyone else. */ 3582 if (file_mode & S_IXOTH) 3583 dac_granted |= VEXEC; 3584 if (file_mode & S_IROTH) 3585 dac_granted |= VREAD; 3586 if (file_mode & S_IWOTH) 3587 dac_granted |= (VWRITE | VAPPEND); 3588 if ((accmode & dac_granted) == accmode) 3589 return (0); 3590 3591 privcheck: 3592 /* 3593 * Build a privilege mask to determine if the set of privileges 3594 * satisfies the requirements when combined with the granted mask 3595 * from above. For each privilege, if the privilege is required, 3596 * bitwise or the request type onto the priv_granted mask. 3597 */ 3598 priv_granted = 0; 3599 3600 if (type == VDIR) { 3601 /* 3602 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 3603 * requests, instead of PRIV_VFS_EXEC. 3604 */ 3605 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3606 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0)) 3607 priv_granted |= VEXEC; 3608 } else { 3609 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3610 !priv_check_cred(cred, PRIV_VFS_EXEC, 0)) 3611 priv_granted |= VEXEC; 3612 } 3613 3614 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 3615 !priv_check_cred(cred, PRIV_VFS_READ, 0)) 3616 priv_granted |= VREAD; 3617 3618 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3619 !priv_check_cred(cred, PRIV_VFS_WRITE, 0)) 3620 priv_granted |= (VWRITE | VAPPEND); 3621 3622 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3623 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0)) 3624 priv_granted |= VADMIN; 3625 3626 if ((accmode & (priv_granted | dac_granted)) == accmode) { 3627 /* XXX audit: privilege used */ 3628 if (privused != NULL) 3629 *privused = 1; 3630 return (0); 3631 } 3632 3633 return ((accmode & VADMIN) ? EPERM : EACCES); 3634 } 3635 3636 /* 3637 * Credential check based on process requesting service, and per-attribute 3638 * permissions. 3639 */ 3640 int 3641 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 3642 struct thread *td, accmode_t accmode) 3643 { 3644 3645 /* 3646 * Kernel-invoked always succeeds. 3647 */ 3648 if (cred == NOCRED) 3649 return (0); 3650 3651 /* 3652 * Do not allow privileged processes in jail to directly manipulate 3653 * system attributes. 3654 */ 3655 switch (attrnamespace) { 3656 case EXTATTR_NAMESPACE_SYSTEM: 3657 /* Potentially should be: return (EPERM); */ 3658 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0)); 3659 case EXTATTR_NAMESPACE_USER: 3660 return (VOP_ACCESS(vp, accmode, cred, td)); 3661 default: 3662 return (EPERM); 3663 } 3664 } 3665 3666 #ifdef DEBUG_VFS_LOCKS 3667 /* 3668 * This only exists to supress warnings from unlocked specfs accesses. It is 3669 * no longer ok to have an unlocked VFS. 3670 */ 3671 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \ 3672 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 3673 3674 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 3675 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, ""); 3676 3677 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 3678 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, ""); 3679 3680 int vfs_badlock_print = 1; /* Print lock violations. */ 3681 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, ""); 3682 3683 #ifdef KDB 3684 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 3685 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, ""); 3686 #endif 3687 3688 static void 3689 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 3690 { 3691 3692 #ifdef KDB 3693 if (vfs_badlock_backtrace) 3694 kdb_backtrace(); 3695 #endif 3696 if (vfs_badlock_print) 3697 printf("%s: %p %s\n", str, (void *)vp, msg); 3698 if (vfs_badlock_ddb) 3699 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 3700 } 3701 3702 void 3703 assert_vi_locked(struct vnode *vp, const char *str) 3704 { 3705 3706 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 3707 vfs_badlock("interlock is not locked but should be", str, vp); 3708 } 3709 3710 void 3711 assert_vi_unlocked(struct vnode *vp, const char *str) 3712 { 3713 3714 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 3715 vfs_badlock("interlock is locked but should not be", str, vp); 3716 } 3717 3718 void 3719 assert_vop_locked(struct vnode *vp, const char *str) 3720 { 3721 3722 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0) 3723 vfs_badlock("is not locked but should be", str, vp); 3724 } 3725 3726 void 3727 assert_vop_unlocked(struct vnode *vp, const char *str) 3728 { 3729 3730 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 3731 vfs_badlock("is locked but should not be", str, vp); 3732 } 3733 3734 void 3735 assert_vop_elocked(struct vnode *vp, const char *str) 3736 { 3737 3738 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 3739 vfs_badlock("is not exclusive locked but should be", str, vp); 3740 } 3741 3742 #if 0 3743 void 3744 assert_vop_elocked_other(struct vnode *vp, const char *str) 3745 { 3746 3747 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER) 3748 vfs_badlock("is not exclusive locked by another thread", 3749 str, vp); 3750 } 3751 3752 void 3753 assert_vop_slocked(struct vnode *vp, const char *str) 3754 { 3755 3756 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED) 3757 vfs_badlock("is not locked shared but should be", str, vp); 3758 } 3759 #endif /* 0 */ 3760 #endif /* DEBUG_VFS_LOCKS */ 3761 3762 void 3763 vop_rename_pre(void *ap) 3764 { 3765 struct vop_rename_args *a = ap; 3766 3767 #ifdef DEBUG_VFS_LOCKS 3768 if (a->a_tvp) 3769 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 3770 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 3771 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 3772 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 3773 3774 /* Check the source (from). */ 3775 if (a->a_tdvp != a->a_fdvp && a->a_tvp != a->a_fdvp) 3776 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 3777 if (a->a_tvp != a->a_fvp) 3778 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 3779 3780 /* Check the target. */ 3781 if (a->a_tvp) 3782 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 3783 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 3784 #endif 3785 if (a->a_tdvp != a->a_fdvp) 3786 vhold(a->a_fdvp); 3787 if (a->a_tvp != a->a_fvp) 3788 vhold(a->a_fvp); 3789 vhold(a->a_tdvp); 3790 if (a->a_tvp) 3791 vhold(a->a_tvp); 3792 } 3793 3794 void 3795 vop_strategy_pre(void *ap) 3796 { 3797 #ifdef DEBUG_VFS_LOCKS 3798 struct vop_strategy_args *a; 3799 struct buf *bp; 3800 3801 a = ap; 3802 bp = a->a_bp; 3803 3804 /* 3805 * Cluster ops lock their component buffers but not the IO container. 3806 */ 3807 if ((bp->b_flags & B_CLUSTER) != 0) 3808 return; 3809 3810 if (!BUF_ISLOCKED(bp)) { 3811 if (vfs_badlock_print) 3812 printf( 3813 "VOP_STRATEGY: bp is not locked but should be\n"); 3814 if (vfs_badlock_ddb) 3815 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 3816 } 3817 #endif 3818 } 3819 3820 void 3821 vop_lookup_pre(void *ap) 3822 { 3823 #ifdef DEBUG_VFS_LOCKS 3824 struct vop_lookup_args *a; 3825 struct vnode *dvp; 3826 3827 a = ap; 3828 dvp = a->a_dvp; 3829 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3830 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 3831 #endif 3832 } 3833 3834 void 3835 vop_lookup_post(void *ap, int rc) 3836 { 3837 #ifdef DEBUG_VFS_LOCKS 3838 struct vop_lookup_args *a; 3839 struct vnode *dvp; 3840 struct vnode *vp; 3841 3842 a = ap; 3843 dvp = a->a_dvp; 3844 vp = *(a->a_vpp); 3845 3846 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3847 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 3848 3849 if (!rc) 3850 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)"); 3851 #endif 3852 } 3853 3854 void 3855 vop_lock_pre(void *ap) 3856 { 3857 #ifdef DEBUG_VFS_LOCKS 3858 struct vop_lock1_args *a = ap; 3859 3860 if ((a->a_flags & LK_INTERLOCK) == 0) 3861 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3862 else 3863 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 3864 #endif 3865 } 3866 3867 void 3868 vop_lock_post(void *ap, int rc) 3869 { 3870 #ifdef DEBUG_VFS_LOCKS 3871 struct vop_lock1_args *a = ap; 3872 3873 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3874 if (rc == 0) 3875 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 3876 #endif 3877 } 3878 3879 void 3880 vop_unlock_pre(void *ap) 3881 { 3882 #ifdef DEBUG_VFS_LOCKS 3883 struct vop_unlock_args *a = ap; 3884 3885 if (a->a_flags & LK_INTERLOCK) 3886 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 3887 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 3888 #endif 3889 } 3890 3891 void 3892 vop_unlock_post(void *ap, int rc) 3893 { 3894 #ifdef DEBUG_VFS_LOCKS 3895 struct vop_unlock_args *a = ap; 3896 3897 if (a->a_flags & LK_INTERLOCK) 3898 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 3899 #endif 3900 } 3901 3902 void 3903 vop_create_post(void *ap, int rc) 3904 { 3905 struct vop_create_args *a = ap; 3906 3907 if (!rc) 3908 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3909 } 3910 3911 void 3912 vop_link_post(void *ap, int rc) 3913 { 3914 struct vop_link_args *a = ap; 3915 3916 if (!rc) { 3917 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 3918 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 3919 } 3920 } 3921 3922 void 3923 vop_mkdir_post(void *ap, int rc) 3924 { 3925 struct vop_mkdir_args *a = ap; 3926 3927 if (!rc) 3928 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 3929 } 3930 3931 void 3932 vop_mknod_post(void *ap, int rc) 3933 { 3934 struct vop_mknod_args *a = ap; 3935 3936 if (!rc) 3937 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3938 } 3939 3940 void 3941 vop_remove_post(void *ap, int rc) 3942 { 3943 struct vop_remove_args *a = ap; 3944 3945 if (!rc) { 3946 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3947 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 3948 } 3949 } 3950 3951 void 3952 vop_rename_post(void *ap, int rc) 3953 { 3954 struct vop_rename_args *a = ap; 3955 3956 if (!rc) { 3957 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE); 3958 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE); 3959 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 3960 if (a->a_tvp) 3961 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 3962 } 3963 if (a->a_tdvp != a->a_fdvp) 3964 vdrop(a->a_fdvp); 3965 if (a->a_tvp != a->a_fvp) 3966 vdrop(a->a_fvp); 3967 vdrop(a->a_tdvp); 3968 if (a->a_tvp) 3969 vdrop(a->a_tvp); 3970 } 3971 3972 void 3973 vop_rmdir_post(void *ap, int rc) 3974 { 3975 struct vop_rmdir_args *a = ap; 3976 3977 if (!rc) { 3978 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 3979 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 3980 } 3981 } 3982 3983 void 3984 vop_setattr_post(void *ap, int rc) 3985 { 3986 struct vop_setattr_args *a = ap; 3987 3988 if (!rc) 3989 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 3990 } 3991 3992 void 3993 vop_symlink_post(void *ap, int rc) 3994 { 3995 struct vop_symlink_args *a = ap; 3996 3997 if (!rc) 3998 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3999 } 4000 4001 static struct knlist fs_knlist; 4002 4003 static void 4004 vfs_event_init(void *arg) 4005 { 4006 knlist_init(&fs_knlist, NULL, NULL, NULL, NULL); 4007 } 4008 /* XXX - correct order? */ 4009 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 4010 4011 void 4012 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused) 4013 { 4014 4015 KNOTE_UNLOCKED(&fs_knlist, event); 4016 } 4017 4018 static int filt_fsattach(struct knote *kn); 4019 static void filt_fsdetach(struct knote *kn); 4020 static int filt_fsevent(struct knote *kn, long hint); 4021 4022 struct filterops fs_filtops = 4023 { 0, filt_fsattach, filt_fsdetach, filt_fsevent }; 4024 4025 static int 4026 filt_fsattach(struct knote *kn) 4027 { 4028 4029 kn->kn_flags |= EV_CLEAR; 4030 knlist_add(&fs_knlist, kn, 0); 4031 return (0); 4032 } 4033 4034 static void 4035 filt_fsdetach(struct knote *kn) 4036 { 4037 4038 knlist_remove(&fs_knlist, kn, 0); 4039 } 4040 4041 static int 4042 filt_fsevent(struct knote *kn, long hint) 4043 { 4044 4045 kn->kn_fflags |= hint; 4046 return (kn->kn_fflags != 0); 4047 } 4048 4049 static int 4050 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 4051 { 4052 struct vfsidctl vc; 4053 int error; 4054 struct mount *mp; 4055 4056 error = SYSCTL_IN(req, &vc, sizeof(vc)); 4057 if (error) 4058 return (error); 4059 if (vc.vc_vers != VFS_CTL_VERS1) 4060 return (EINVAL); 4061 mp = vfs_getvfs(&vc.vc_fsid); 4062 if (mp == NULL) 4063 return (ENOENT); 4064 /* ensure that a specific sysctl goes to the right filesystem. */ 4065 if (strcmp(vc.vc_fstypename, "*") != 0 && 4066 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 4067 vfs_rel(mp); 4068 return (EINVAL); 4069 } 4070 VCTLTOREQ(&vc, req); 4071 error = VFS_SYSCTL(mp, vc.vc_op, req); 4072 vfs_rel(mp); 4073 return (error); 4074 } 4075 4076 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "", 4077 "Sysctl by fsid"); 4078 4079 /* 4080 * Function to initialize a va_filerev field sensibly. 4081 * XXX: Wouldn't a random number make a lot more sense ?? 4082 */ 4083 u_quad_t 4084 init_va_filerev(void) 4085 { 4086 struct bintime bt; 4087 4088 getbinuptime(&bt); 4089 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 4090 } 4091 4092 static int filt_vfsread(struct knote *kn, long hint); 4093 static int filt_vfswrite(struct knote *kn, long hint); 4094 static int filt_vfsvnode(struct knote *kn, long hint); 4095 static void filt_vfsdetach(struct knote *kn); 4096 static struct filterops vfsread_filtops = 4097 { 1, NULL, filt_vfsdetach, filt_vfsread }; 4098 static struct filterops vfswrite_filtops = 4099 { 1, NULL, filt_vfsdetach, filt_vfswrite }; 4100 static struct filterops vfsvnode_filtops = 4101 { 1, NULL, filt_vfsdetach, filt_vfsvnode }; 4102 4103 static void 4104 vfs_knllock(void *arg) 4105 { 4106 struct vnode *vp = arg; 4107 4108 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4109 } 4110 4111 static void 4112 vfs_knlunlock(void *arg) 4113 { 4114 struct vnode *vp = arg; 4115 4116 VOP_UNLOCK(vp, 0); 4117 } 4118 4119 static int 4120 vfs_knllocked(void *arg) 4121 { 4122 struct vnode *vp = arg; 4123 4124 return (VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 4125 } 4126 4127 int 4128 vfs_kqfilter(struct vop_kqfilter_args *ap) 4129 { 4130 struct vnode *vp = ap->a_vp; 4131 struct knote *kn = ap->a_kn; 4132 struct knlist *knl; 4133 4134 switch (kn->kn_filter) { 4135 case EVFILT_READ: 4136 kn->kn_fop = &vfsread_filtops; 4137 break; 4138 case EVFILT_WRITE: 4139 kn->kn_fop = &vfswrite_filtops; 4140 break; 4141 case EVFILT_VNODE: 4142 kn->kn_fop = &vfsvnode_filtops; 4143 break; 4144 default: 4145 return (EINVAL); 4146 } 4147 4148 kn->kn_hook = (caddr_t)vp; 4149 4150 v_addpollinfo(vp); 4151 if (vp->v_pollinfo == NULL) 4152 return (ENOMEM); 4153 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 4154 knlist_add(knl, kn, 0); 4155 4156 return (0); 4157 } 4158 4159 /* 4160 * Detach knote from vnode 4161 */ 4162 static void 4163 filt_vfsdetach(struct knote *kn) 4164 { 4165 struct vnode *vp = (struct vnode *)kn->kn_hook; 4166 4167 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 4168 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 4169 } 4170 4171 /*ARGSUSED*/ 4172 static int 4173 filt_vfsread(struct knote *kn, long hint) 4174 { 4175 struct vnode *vp = (struct vnode *)kn->kn_hook; 4176 struct vattr va; 4177 4178 /* 4179 * filesystem is gone, so set the EOF flag and schedule 4180 * the knote for deletion. 4181 */ 4182 if (hint == NOTE_REVOKE) { 4183 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 4184 return (1); 4185 } 4186 4187 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 4188 return (0); 4189 4190 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 4191 return (kn->kn_data != 0); 4192 } 4193 4194 /*ARGSUSED*/ 4195 static int 4196 filt_vfswrite(struct knote *kn, long hint) 4197 { 4198 /* 4199 * filesystem is gone, so set the EOF flag and schedule 4200 * the knote for deletion. 4201 */ 4202 if (hint == NOTE_REVOKE) 4203 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 4204 4205 kn->kn_data = 0; 4206 return (1); 4207 } 4208 4209 static int 4210 filt_vfsvnode(struct knote *kn, long hint) 4211 { 4212 if (kn->kn_sfflags & hint) 4213 kn->kn_fflags |= hint; 4214 if (hint == NOTE_REVOKE) { 4215 kn->kn_flags |= EV_EOF; 4216 return (1); 4217 } 4218 return (kn->kn_fflags != 0); 4219 } 4220 4221 int 4222 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 4223 { 4224 int error; 4225 4226 if (dp->d_reclen > ap->a_uio->uio_resid) 4227 return (ENAMETOOLONG); 4228 error = uiomove(dp, dp->d_reclen, ap->a_uio); 4229 if (error) { 4230 if (ap->a_ncookies != NULL) { 4231 if (ap->a_cookies != NULL) 4232 free(ap->a_cookies, M_TEMP); 4233 ap->a_cookies = NULL; 4234 *ap->a_ncookies = 0; 4235 } 4236 return (error); 4237 } 4238 if (ap->a_ncookies == NULL) 4239 return (0); 4240 4241 KASSERT(ap->a_cookies, 4242 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 4243 4244 *ap->a_cookies = realloc(*ap->a_cookies, 4245 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 4246 (*ap->a_cookies)[*ap->a_ncookies] = off; 4247 return (0); 4248 } 4249 4250 /* 4251 * Mark for update the access time of the file if the filesystem 4252 * supports VOP_MARKATIME. This functionality is used by execve and 4253 * mmap, so we want to avoid the I/O implied by directly setting 4254 * va_atime for the sake of efficiency. 4255 */ 4256 void 4257 vfs_mark_atime(struct vnode *vp, struct ucred *cred) 4258 { 4259 4260 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 4261 (void)VOP_MARKATIME(vp); 4262 } 4263