1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 37 */ 38 39 /* 40 * External virtual filesystem routines 41 */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include "opt_ddb.h" 47 #include "opt_watchdog.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/bio.h> 52 #include <sys/buf.h> 53 #include <sys/capsicum.h> 54 #include <sys/condvar.h> 55 #include <sys/conf.h> 56 #include <sys/counter.h> 57 #include <sys/dirent.h> 58 #include <sys/event.h> 59 #include <sys/eventhandler.h> 60 #include <sys/extattr.h> 61 #include <sys/file.h> 62 #include <sys/fcntl.h> 63 #include <sys/jail.h> 64 #include <sys/kdb.h> 65 #include <sys/kernel.h> 66 #include <sys/kthread.h> 67 #include <sys/ktr.h> 68 #include <sys/lockf.h> 69 #include <sys/malloc.h> 70 #include <sys/mount.h> 71 #include <sys/namei.h> 72 #include <sys/pctrie.h> 73 #include <sys/priv.h> 74 #include <sys/reboot.h> 75 #include <sys/refcount.h> 76 #include <sys/rwlock.h> 77 #include <sys/sched.h> 78 #include <sys/sleepqueue.h> 79 #include <sys/smp.h> 80 #include <sys/stat.h> 81 #include <sys/sysctl.h> 82 #include <sys/syslog.h> 83 #include <sys/vmmeter.h> 84 #include <sys/vnode.h> 85 #include <sys/watchdog.h> 86 87 #include <machine/stdarg.h> 88 89 #include <security/mac/mac_framework.h> 90 91 #include <vm/vm.h> 92 #include <vm/vm_object.h> 93 #include <vm/vm_extern.h> 94 #include <vm/pmap.h> 95 #include <vm/vm_map.h> 96 #include <vm/vm_page.h> 97 #include <vm/vm_kern.h> 98 #include <vm/uma.h> 99 100 #ifdef DDB 101 #include <ddb/ddb.h> 102 #endif 103 104 static void delmntque(struct vnode *vp); 105 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, 106 int slpflag, int slptimeo); 107 static void syncer_shutdown(void *arg, int howto); 108 static int vtryrecycle(struct vnode *vp); 109 static void v_init_counters(struct vnode *); 110 static void v_incr_devcount(struct vnode *); 111 static void v_decr_devcount(struct vnode *); 112 static void vgonel(struct vnode *); 113 static void vfs_knllock(void *arg); 114 static void vfs_knlunlock(void *arg); 115 static void vfs_knl_assert_locked(void *arg); 116 static void vfs_knl_assert_unlocked(void *arg); 117 static void vnlru_return_batches(struct vfsops *mnt_op); 118 static void destroy_vpollinfo(struct vpollinfo *vi); 119 static int v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo, 120 daddr_t startlbn, daddr_t endlbn); 121 122 /* 123 * These fences are intended for cases where some synchronization is 124 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt 125 * and v_usecount) updates. Access to v_iflags is generally synchronized 126 * by the interlock, but we have some internal assertions that check vnode 127 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only 128 * for now. 129 */ 130 #ifdef INVARIANTS 131 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq() 132 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel() 133 #else 134 #define VNODE_REFCOUNT_FENCE_ACQ() 135 #define VNODE_REFCOUNT_FENCE_REL() 136 #endif 137 138 /* 139 * Number of vnodes in existence. Increased whenever getnewvnode() 140 * allocates a new vnode, decreased in vdropl() for VIRF_DOOMED vnode. 141 */ 142 static unsigned long numvnodes; 143 144 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, 145 "Number of vnodes in existence"); 146 147 static counter_u64_t vnodes_created; 148 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created, 149 "Number of vnodes created by getnewvnode"); 150 151 static u_long mnt_free_list_batch = 128; 152 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW, 153 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list"); 154 155 /* 156 * Conversion tables for conversion from vnode types to inode formats 157 * and back. 158 */ 159 enum vtype iftovt_tab[16] = { 160 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 161 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON 162 }; 163 int vttoif_tab[10] = { 164 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 165 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT 166 }; 167 168 /* 169 * List of vnodes that are ready for recycling. 170 */ 171 static TAILQ_HEAD(freelst, vnode) vnode_free_list; 172 173 /* 174 * "Free" vnode target. Free vnodes are rarely completely free, but are 175 * just ones that are cheap to recycle. Usually they are for files which 176 * have been stat'd but not read; these usually have inode and namecache 177 * data attached to them. This target is the preferred minimum size of a 178 * sub-cache consisting mostly of such files. The system balances the size 179 * of this sub-cache with its complement to try to prevent either from 180 * thrashing while the other is relatively inactive. The targets express 181 * a preference for the best balance. 182 * 183 * "Above" this target there are 2 further targets (watermarks) related 184 * to recyling of free vnodes. In the best-operating case, the cache is 185 * exactly full, the free list has size between vlowat and vhiwat above the 186 * free target, and recycling from it and normal use maintains this state. 187 * Sometimes the free list is below vlowat or even empty, but this state 188 * is even better for immediate use provided the cache is not full. 189 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free 190 * ones) to reach one of these states. The watermarks are currently hard- 191 * coded as 4% and 9% of the available space higher. These and the default 192 * of 25% for wantfreevnodes are too large if the memory size is large. 193 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim 194 * whenever vnlru_proc() becomes active. 195 */ 196 static u_long wantfreevnodes; 197 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, 198 &wantfreevnodes, 0, "Target for minimum number of \"free\" vnodes"); 199 static u_long freevnodes; 200 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, 201 &freevnodes, 0, "Number of \"free\" vnodes"); 202 203 static counter_u64_t recycles_count; 204 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 205 "Number of vnodes recycled to meet vnode cache targets"); 206 207 /* 208 * Various variables used for debugging the new implementation of 209 * reassignbuf(). 210 * XXX these are probably of (very) limited utility now. 211 */ 212 static int reassignbufcalls; 213 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW | CTLFLAG_STATS, 214 &reassignbufcalls, 0, "Number of calls to reassignbuf"); 215 216 static counter_u64_t free_owe_inact; 217 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 218 "Number of times free vnodes kept on active list due to VFS " 219 "owing inactivation"); 220 221 /* To keep more than one thread at a time from running vfs_getnewfsid */ 222 static struct mtx mntid_mtx; 223 224 /* 225 * Lock for any access to the following: 226 * vnode_free_list 227 * numvnodes 228 * freevnodes 229 */ 230 static struct mtx vnode_free_list_mtx; 231 232 /* Publicly exported FS */ 233 struct nfs_public nfs_pub; 234 235 static uma_zone_t buf_trie_zone; 236 237 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 238 static uma_zone_t vnode_zone; 239 static uma_zone_t vnodepoll_zone; 240 241 /* 242 * The workitem queue. 243 * 244 * It is useful to delay writes of file data and filesystem metadata 245 * for tens of seconds so that quickly created and deleted files need 246 * not waste disk bandwidth being created and removed. To realize this, 247 * we append vnodes to a "workitem" queue. When running with a soft 248 * updates implementation, most pending metadata dependencies should 249 * not wait for more than a few seconds. Thus, mounted on block devices 250 * are delayed only about a half the time that file data is delayed. 251 * Similarly, directory updates are more critical, so are only delayed 252 * about a third the time that file data is delayed. Thus, there are 253 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 254 * one each second (driven off the filesystem syncer process). The 255 * syncer_delayno variable indicates the next queue that is to be processed. 256 * Items that need to be processed soon are placed in this queue: 257 * 258 * syncer_workitem_pending[syncer_delayno] 259 * 260 * A delay of fifteen seconds is done by placing the request fifteen 261 * entries later in the queue: 262 * 263 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 264 * 265 */ 266 static int syncer_delayno; 267 static long syncer_mask; 268 LIST_HEAD(synclist, bufobj); 269 static struct synclist *syncer_workitem_pending; 270 /* 271 * The sync_mtx protects: 272 * bo->bo_synclist 273 * sync_vnode_count 274 * syncer_delayno 275 * syncer_state 276 * syncer_workitem_pending 277 * syncer_worklist_len 278 * rushjob 279 */ 280 static struct mtx sync_mtx; 281 static struct cv sync_wakeup; 282 283 #define SYNCER_MAXDELAY 32 284 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 285 static int syncdelay = 30; /* max time to delay syncing data */ 286 static int filedelay = 30; /* time to delay syncing files */ 287 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, 288 "Time to delay syncing files (in seconds)"); 289 static int dirdelay = 29; /* time to delay syncing directories */ 290 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, 291 "Time to delay syncing directories (in seconds)"); 292 static int metadelay = 28; /* time to delay syncing metadata */ 293 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, 294 "Time to delay syncing metadata (in seconds)"); 295 static int rushjob; /* number of slots to run ASAP */ 296 static int stat_rush_requests; /* number of times I/O speeded up */ 297 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, 298 "Number of times I/O speeded up (rush requests)"); 299 300 /* 301 * When shutting down the syncer, run it at four times normal speed. 302 */ 303 #define SYNCER_SHUTDOWN_SPEEDUP 4 304 static int sync_vnode_count; 305 static int syncer_worklist_len; 306 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY } 307 syncer_state; 308 309 /* Target for maximum number of vnodes. */ 310 int desiredvnodes; 311 static int gapvnodes; /* gap between wanted and desired */ 312 static int vhiwat; /* enough extras after expansion */ 313 static int vlowat; /* minimal extras before expansion */ 314 static int vstir; /* nonzero to stir non-free vnodes */ 315 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */ 316 317 static int 318 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS) 319 { 320 int error, old_desiredvnodes; 321 322 old_desiredvnodes = desiredvnodes; 323 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0) 324 return (error); 325 if (old_desiredvnodes != desiredvnodes) { 326 wantfreevnodes = desiredvnodes / 4; 327 /* XXX locking seems to be incomplete. */ 328 vfs_hash_changesize(desiredvnodes); 329 cache_changesize(desiredvnodes); 330 } 331 return (0); 332 } 333 334 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes, 335 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0, 336 sysctl_update_desiredvnodes, "I", "Target for maximum number of vnodes"); 337 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 338 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)"); 339 static int vnlru_nowhere; 340 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, 341 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success"); 342 343 static int 344 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS) 345 { 346 struct vnode *vp; 347 struct nameidata nd; 348 char *buf; 349 unsigned long ndflags; 350 int error; 351 352 if (req->newptr == NULL) 353 return (EINVAL); 354 if (req->newlen >= PATH_MAX) 355 return (E2BIG); 356 357 buf = malloc(PATH_MAX, M_TEMP, M_WAITOK); 358 error = SYSCTL_IN(req, buf, req->newlen); 359 if (error != 0) 360 goto out; 361 362 buf[req->newlen] = '\0'; 363 364 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | NOCACHE | SAVENAME; 365 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread); 366 if ((error = namei(&nd)) != 0) 367 goto out; 368 vp = nd.ni_vp; 369 370 if (VN_IS_DOOMED(vp)) { 371 /* 372 * This vnode is being recycled. Return != 0 to let the caller 373 * know that the sysctl had no effect. Return EAGAIN because a 374 * subsequent call will likely succeed (since namei will create 375 * a new vnode if necessary) 376 */ 377 error = EAGAIN; 378 goto putvnode; 379 } 380 381 counter_u64_add(recycles_count, 1); 382 vgone(vp); 383 putvnode: 384 NDFREE(&nd, 0); 385 out: 386 free(buf, M_TEMP); 387 return (error); 388 } 389 390 static int 391 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS) 392 { 393 struct thread *td = curthread; 394 struct vnode *vp; 395 struct file *fp; 396 int error; 397 int fd; 398 399 if (req->newptr == NULL) 400 return (EBADF); 401 402 error = sysctl_handle_int(oidp, &fd, 0, req); 403 if (error != 0) 404 return (error); 405 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp); 406 if (error != 0) 407 return (error); 408 vp = fp->f_vnode; 409 410 error = vn_lock(vp, LK_EXCLUSIVE); 411 if (error != 0) 412 goto drop; 413 414 counter_u64_add(recycles_count, 1); 415 vgone(vp); 416 VOP_UNLOCK(vp, 0); 417 drop: 418 fdrop(fp, td); 419 return (error); 420 } 421 422 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode, 423 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0, 424 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname"); 425 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode, 426 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0, 427 sysctl_ftry_reclaim_vnode, "I", 428 "Try to reclaim a vnode by its file descriptor"); 429 430 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */ 431 static int vnsz2log; 432 433 /* 434 * Support for the bufobj clean & dirty pctrie. 435 */ 436 static void * 437 buf_trie_alloc(struct pctrie *ptree) 438 { 439 440 return uma_zalloc(buf_trie_zone, M_NOWAIT); 441 } 442 443 static void 444 buf_trie_free(struct pctrie *ptree, void *node) 445 { 446 447 uma_zfree(buf_trie_zone, node); 448 } 449 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free); 450 451 /* 452 * Initialize the vnode management data structures. 453 * 454 * Reevaluate the following cap on the number of vnodes after the physical 455 * memory size exceeds 512GB. In the limit, as the physical memory size 456 * grows, the ratio of the memory size in KB to vnodes approaches 64:1. 457 */ 458 #ifndef MAXVNODES_MAX 459 #define MAXVNODES_MAX (512 * 1024 * 1024 / 64) /* 8M */ 460 #endif 461 462 /* 463 * Initialize a vnode as it first enters the zone. 464 */ 465 static int 466 vnode_init(void *mem, int size, int flags) 467 { 468 struct vnode *vp; 469 470 vp = mem; 471 bzero(vp, size); 472 /* 473 * Setup locks. 474 */ 475 vp->v_vnlock = &vp->v_lock; 476 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 477 /* 478 * By default, don't allow shared locks unless filesystems opt-in. 479 */ 480 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT, 481 LK_NOSHARE | LK_IS_VNODE); 482 /* 483 * Initialize bufobj. 484 */ 485 bufobj_init(&vp->v_bufobj, vp); 486 /* 487 * Initialize namecache. 488 */ 489 LIST_INIT(&vp->v_cache_src); 490 TAILQ_INIT(&vp->v_cache_dst); 491 /* 492 * Initialize rangelocks. 493 */ 494 rangelock_init(&vp->v_rl); 495 return (0); 496 } 497 498 /* 499 * Free a vnode when it is cleared from the zone. 500 */ 501 static void 502 vnode_fini(void *mem, int size) 503 { 504 struct vnode *vp; 505 struct bufobj *bo; 506 507 vp = mem; 508 rangelock_destroy(&vp->v_rl); 509 lockdestroy(vp->v_vnlock); 510 mtx_destroy(&vp->v_interlock); 511 bo = &vp->v_bufobj; 512 rw_destroy(BO_LOCKPTR(bo)); 513 } 514 515 /* 516 * Provide the size of NFS nclnode and NFS fh for calculation of the 517 * vnode memory consumption. The size is specified directly to 518 * eliminate dependency on NFS-private header. 519 * 520 * Other filesystems may use bigger or smaller (like UFS and ZFS) 521 * private inode data, but the NFS-based estimation is ample enough. 522 * Still, we care about differences in the size between 64- and 32-bit 523 * platforms. 524 * 525 * Namecache structure size is heuristically 526 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1. 527 */ 528 #ifdef _LP64 529 #define NFS_NCLNODE_SZ (528 + 64) 530 #define NC_SZ 148 531 #else 532 #define NFS_NCLNODE_SZ (360 + 32) 533 #define NC_SZ 92 534 #endif 535 536 static void 537 vntblinit(void *dummy __unused) 538 { 539 u_int i; 540 int physvnodes, virtvnodes; 541 542 /* 543 * Desiredvnodes is a function of the physical memory size and the 544 * kernel's heap size. Generally speaking, it scales with the 545 * physical memory size. The ratio of desiredvnodes to the physical 546 * memory size is 1:16 until desiredvnodes exceeds 98,304. 547 * Thereafter, the 548 * marginal ratio of desiredvnodes to the physical memory size is 549 * 1:64. However, desiredvnodes is limited by the kernel's heap 550 * size. The memory required by desiredvnodes vnodes and vm objects 551 * must not exceed 1/10th of the kernel's heap size. 552 */ 553 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 + 554 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64; 555 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) + 556 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ)); 557 desiredvnodes = min(physvnodes, virtvnodes); 558 if (desiredvnodes > MAXVNODES_MAX) { 559 if (bootverbose) 560 printf("Reducing kern.maxvnodes %d -> %d\n", 561 desiredvnodes, MAXVNODES_MAX); 562 desiredvnodes = MAXVNODES_MAX; 563 } 564 wantfreevnodes = desiredvnodes / 4; 565 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 566 TAILQ_INIT(&vnode_free_list); 567 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF); 568 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 569 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0); 570 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 571 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 572 /* 573 * Preallocate enough nodes to support one-per buf so that 574 * we can not fail an insert. reassignbuf() callers can not 575 * tolerate the insertion failure. 576 */ 577 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(), 578 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR, 579 UMA_ZONE_NOFREE | UMA_ZONE_VM); 580 uma_prealloc(buf_trie_zone, nbuf); 581 582 vnodes_created = counter_u64_alloc(M_WAITOK); 583 recycles_count = counter_u64_alloc(M_WAITOK); 584 free_owe_inact = counter_u64_alloc(M_WAITOK); 585 586 /* 587 * Initialize the filesystem syncer. 588 */ 589 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 590 &syncer_mask); 591 syncer_maxdelay = syncer_mask + 1; 592 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF); 593 cv_init(&sync_wakeup, "syncer"); 594 for (i = 1; i <= sizeof(struct vnode); i <<= 1) 595 vnsz2log++; 596 vnsz2log--; 597 } 598 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL); 599 600 601 /* 602 * Mark a mount point as busy. Used to synchronize access and to delay 603 * unmounting. Eventually, mountlist_mtx is not released on failure. 604 * 605 * vfs_busy() is a custom lock, it can block the caller. 606 * vfs_busy() only sleeps if the unmount is active on the mount point. 607 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any 608 * vnode belonging to mp. 609 * 610 * Lookup uses vfs_busy() to traverse mount points. 611 * root fs var fs 612 * / vnode lock A / vnode lock (/var) D 613 * /var vnode lock B /log vnode lock(/var/log) E 614 * vfs_busy lock C vfs_busy lock F 615 * 616 * Within each file system, the lock order is C->A->B and F->D->E. 617 * 618 * When traversing across mounts, the system follows that lock order: 619 * 620 * C->A->B 621 * | 622 * +->F->D->E 623 * 624 * The lookup() process for namei("/var") illustrates the process: 625 * VOP_LOOKUP() obtains B while A is held 626 * vfs_busy() obtains a shared lock on F while A and B are held 627 * vput() releases lock on B 628 * vput() releases lock on A 629 * VFS_ROOT() obtains lock on D while shared lock on F is held 630 * vfs_unbusy() releases shared lock on F 631 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A. 632 * Attempt to lock A (instead of vp_crossmp) while D is held would 633 * violate the global order, causing deadlocks. 634 * 635 * dounmount() locks B while F is drained. 636 */ 637 int 638 vfs_busy(struct mount *mp, int flags) 639 { 640 641 MPASS((flags & ~MBF_MASK) == 0); 642 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags); 643 644 if (vfs_op_thread_enter(mp)) { 645 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0); 646 MPASS((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0); 647 MPASS((mp->mnt_kern_flag & MNTK_REFEXPIRE) == 0); 648 vfs_mp_count_add_pcpu(mp, ref, 1); 649 vfs_mp_count_add_pcpu(mp, lockref, 1); 650 vfs_op_thread_exit(mp); 651 if (flags & MBF_MNTLSTLOCK) 652 mtx_unlock(&mountlist_mtx); 653 return (0); 654 } 655 656 MNT_ILOCK(mp); 657 vfs_assert_mount_counters(mp); 658 MNT_REF(mp); 659 /* 660 * If mount point is currently being unmounted, sleep until the 661 * mount point fate is decided. If thread doing the unmounting fails, 662 * it will clear MNTK_UNMOUNT flag before waking us up, indicating 663 * that this mount point has survived the unmount attempt and vfs_busy 664 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE 665 * flag in addition to MNTK_UNMOUNT, indicating that mount point is 666 * about to be really destroyed. vfs_busy needs to release its 667 * reference on the mount point in this case and return with ENOENT, 668 * telling the caller that mount mount it tried to busy is no longer 669 * valid. 670 */ 671 while (mp->mnt_kern_flag & MNTK_UNMOUNT) { 672 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) { 673 MNT_REL(mp); 674 MNT_IUNLOCK(mp); 675 CTR1(KTR_VFS, "%s: failed busying before sleeping", 676 __func__); 677 return (ENOENT); 678 } 679 if (flags & MBF_MNTLSTLOCK) 680 mtx_unlock(&mountlist_mtx); 681 mp->mnt_kern_flag |= MNTK_MWAIT; 682 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0); 683 if (flags & MBF_MNTLSTLOCK) 684 mtx_lock(&mountlist_mtx); 685 MNT_ILOCK(mp); 686 } 687 if (flags & MBF_MNTLSTLOCK) 688 mtx_unlock(&mountlist_mtx); 689 mp->mnt_lockref++; 690 MNT_IUNLOCK(mp); 691 return (0); 692 } 693 694 /* 695 * Free a busy filesystem. 696 */ 697 void 698 vfs_unbusy(struct mount *mp) 699 { 700 int c; 701 702 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 703 704 if (vfs_op_thread_enter(mp)) { 705 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0); 706 vfs_mp_count_sub_pcpu(mp, lockref, 1); 707 vfs_mp_count_sub_pcpu(mp, ref, 1); 708 vfs_op_thread_exit(mp); 709 return; 710 } 711 712 MNT_ILOCK(mp); 713 vfs_assert_mount_counters(mp); 714 MNT_REL(mp); 715 c = --mp->mnt_lockref; 716 if (mp->mnt_vfs_ops == 0) { 717 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0); 718 MNT_IUNLOCK(mp); 719 return; 720 } 721 if (c < 0) 722 vfs_dump_mount_counters(mp); 723 if (c == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) { 724 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT); 725 CTR1(KTR_VFS, "%s: waking up waiters", __func__); 726 mp->mnt_kern_flag &= ~MNTK_DRAINING; 727 wakeup(&mp->mnt_lockref); 728 } 729 MNT_IUNLOCK(mp); 730 } 731 732 /* 733 * Lookup a mount point by filesystem identifier. 734 */ 735 struct mount * 736 vfs_getvfs(fsid_t *fsid) 737 { 738 struct mount *mp; 739 740 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 741 mtx_lock(&mountlist_mtx); 742 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 743 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 744 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 745 vfs_ref(mp); 746 mtx_unlock(&mountlist_mtx); 747 return (mp); 748 } 749 } 750 mtx_unlock(&mountlist_mtx); 751 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 752 return ((struct mount *) 0); 753 } 754 755 /* 756 * Lookup a mount point by filesystem identifier, busying it before 757 * returning. 758 * 759 * To avoid congestion on mountlist_mtx, implement simple direct-mapped 760 * cache for popular filesystem identifiers. The cache is lockess, using 761 * the fact that struct mount's are never freed. In worst case we may 762 * get pointer to unmounted or even different filesystem, so we have to 763 * check what we got, and go slow way if so. 764 */ 765 struct mount * 766 vfs_busyfs(fsid_t *fsid) 767 { 768 #define FSID_CACHE_SIZE 256 769 typedef struct mount * volatile vmp_t; 770 static vmp_t cache[FSID_CACHE_SIZE]; 771 struct mount *mp; 772 int error; 773 uint32_t hash; 774 775 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 776 hash = fsid->val[0] ^ fsid->val[1]; 777 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1); 778 mp = cache[hash]; 779 if (mp == NULL || 780 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] || 781 mp->mnt_stat.f_fsid.val[1] != fsid->val[1]) 782 goto slow; 783 if (vfs_busy(mp, 0) != 0) { 784 cache[hash] = NULL; 785 goto slow; 786 } 787 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 788 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) 789 return (mp); 790 else 791 vfs_unbusy(mp); 792 793 slow: 794 mtx_lock(&mountlist_mtx); 795 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 796 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 797 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 798 error = vfs_busy(mp, MBF_MNTLSTLOCK); 799 if (error) { 800 cache[hash] = NULL; 801 mtx_unlock(&mountlist_mtx); 802 return (NULL); 803 } 804 cache[hash] = mp; 805 return (mp); 806 } 807 } 808 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 809 mtx_unlock(&mountlist_mtx); 810 return ((struct mount *) 0); 811 } 812 813 /* 814 * Check if a user can access privileged mount options. 815 */ 816 int 817 vfs_suser(struct mount *mp, struct thread *td) 818 { 819 int error; 820 821 if (jailed(td->td_ucred)) { 822 /* 823 * If the jail of the calling thread lacks permission for 824 * this type of file system, deny immediately. 825 */ 826 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag)) 827 return (EPERM); 828 829 /* 830 * If the file system was mounted outside the jail of the 831 * calling thread, deny immediately. 832 */ 833 if (prison_check(td->td_ucred, mp->mnt_cred) != 0) 834 return (EPERM); 835 } 836 837 /* 838 * If file system supports delegated administration, we don't check 839 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified 840 * by the file system itself. 841 * If this is not the user that did original mount, we check for 842 * the PRIV_VFS_MOUNT_OWNER privilege. 843 */ 844 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) && 845 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) { 846 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0) 847 return (error); 848 } 849 return (0); 850 } 851 852 /* 853 * Get a new unique fsid. Try to make its val[0] unique, since this value 854 * will be used to create fake device numbers for stat(). Also try (but 855 * not so hard) make its val[0] unique mod 2^16, since some emulators only 856 * support 16-bit device numbers. We end up with unique val[0]'s for the 857 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 858 * 859 * Keep in mind that several mounts may be running in parallel. Starting 860 * the search one past where the previous search terminated is both a 861 * micro-optimization and a defense against returning the same fsid to 862 * different mounts. 863 */ 864 void 865 vfs_getnewfsid(struct mount *mp) 866 { 867 static uint16_t mntid_base; 868 struct mount *nmp; 869 fsid_t tfsid; 870 int mtype; 871 872 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 873 mtx_lock(&mntid_mtx); 874 mtype = mp->mnt_vfc->vfc_typenum; 875 tfsid.val[1] = mtype; 876 mtype = (mtype & 0xFF) << 24; 877 for (;;) { 878 tfsid.val[0] = makedev(255, 879 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 880 mntid_base++; 881 if ((nmp = vfs_getvfs(&tfsid)) == NULL) 882 break; 883 vfs_rel(nmp); 884 } 885 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 886 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 887 mtx_unlock(&mntid_mtx); 888 } 889 890 /* 891 * Knob to control the precision of file timestamps: 892 * 893 * 0 = seconds only; nanoseconds zeroed. 894 * 1 = seconds and nanoseconds, accurate within 1/HZ. 895 * 2 = seconds and nanoseconds, truncated to microseconds. 896 * >=3 = seconds and nanoseconds, maximum precision. 897 */ 898 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 899 900 static int timestamp_precision = TSP_USEC; 901 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 902 ×tamp_precision, 0, "File timestamp precision (0: seconds, " 903 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, " 904 "3+: sec + ns (max. precision))"); 905 906 /* 907 * Get a current timestamp. 908 */ 909 void 910 vfs_timestamp(struct timespec *tsp) 911 { 912 struct timeval tv; 913 914 switch (timestamp_precision) { 915 case TSP_SEC: 916 tsp->tv_sec = time_second; 917 tsp->tv_nsec = 0; 918 break; 919 case TSP_HZ: 920 getnanotime(tsp); 921 break; 922 case TSP_USEC: 923 microtime(&tv); 924 TIMEVAL_TO_TIMESPEC(&tv, tsp); 925 break; 926 case TSP_NSEC: 927 default: 928 nanotime(tsp); 929 break; 930 } 931 } 932 933 /* 934 * Set vnode attributes to VNOVAL 935 */ 936 void 937 vattr_null(struct vattr *vap) 938 { 939 940 vap->va_type = VNON; 941 vap->va_size = VNOVAL; 942 vap->va_bytes = VNOVAL; 943 vap->va_mode = VNOVAL; 944 vap->va_nlink = VNOVAL; 945 vap->va_uid = VNOVAL; 946 vap->va_gid = VNOVAL; 947 vap->va_fsid = VNOVAL; 948 vap->va_fileid = VNOVAL; 949 vap->va_blocksize = VNOVAL; 950 vap->va_rdev = VNOVAL; 951 vap->va_atime.tv_sec = VNOVAL; 952 vap->va_atime.tv_nsec = VNOVAL; 953 vap->va_mtime.tv_sec = VNOVAL; 954 vap->va_mtime.tv_nsec = VNOVAL; 955 vap->va_ctime.tv_sec = VNOVAL; 956 vap->va_ctime.tv_nsec = VNOVAL; 957 vap->va_birthtime.tv_sec = VNOVAL; 958 vap->va_birthtime.tv_nsec = VNOVAL; 959 vap->va_flags = VNOVAL; 960 vap->va_gen = VNOVAL; 961 vap->va_vaflags = 0; 962 } 963 964 /* 965 * This routine is called when we have too many vnodes. It attempts 966 * to free <count> vnodes and will potentially free vnodes that still 967 * have VM backing store (VM backing store is typically the cause 968 * of a vnode blowout so we want to do this). Therefore, this operation 969 * is not considered cheap. 970 * 971 * A number of conditions may prevent a vnode from being reclaimed. 972 * the buffer cache may have references on the vnode, a directory 973 * vnode may still have references due to the namei cache representing 974 * underlying files, or the vnode may be in active use. It is not 975 * desirable to reuse such vnodes. These conditions may cause the 976 * number of vnodes to reach some minimum value regardless of what 977 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 978 * 979 * @param mp Try to reclaim vnodes from this mountpoint 980 * @param reclaim_nc_src Only reclaim directories with outgoing namecache 981 * entries if this argument is strue 982 * @param trigger Only reclaim vnodes with fewer than this many resident 983 * pages. 984 * @return The number of vnodes that were reclaimed. 985 */ 986 static int 987 vlrureclaim(struct mount *mp, bool reclaim_nc_src, int trigger) 988 { 989 struct vnode *vp; 990 int count, done, target; 991 992 done = 0; 993 vn_start_write(NULL, &mp, V_WAIT); 994 MNT_ILOCK(mp); 995 count = mp->mnt_nvnodelistsize; 996 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1); 997 target = target / 10 + 1; 998 while (count != 0 && done < target) { 999 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1000 while (vp != NULL && vp->v_type == VMARKER) 1001 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1002 if (vp == NULL) 1003 break; 1004 /* 1005 * XXX LRU is completely broken for non-free vnodes. First 1006 * by calling here in mountpoint order, then by moving 1007 * unselected vnodes to the end here, and most grossly by 1008 * removing the vlruvp() function that was supposed to 1009 * maintain the order. (This function was born broken 1010 * since syncer problems prevented it doing anything.) The 1011 * order is closer to LRC (C = Created). 1012 * 1013 * LRU reclaiming of vnodes seems to have last worked in 1014 * FreeBSD-3 where LRU wasn't mentioned under any spelling. 1015 * Then there was no hold count, and inactive vnodes were 1016 * simply put on the free list in LRU order. The separate 1017 * lists also break LRU. We prefer to reclaim from the 1018 * free list for technical reasons. This tends to thrash 1019 * the free list to keep very unrecently used held vnodes. 1020 * The problem is mitigated by keeping the free list large. 1021 */ 1022 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1023 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1024 --count; 1025 if (!VI_TRYLOCK(vp)) 1026 goto next_iter; 1027 /* 1028 * If it's been deconstructed already, it's still 1029 * referenced, or it exceeds the trigger, skip it. 1030 * Also skip free vnodes. We are trying to make space 1031 * to expand the free list, not reduce it. 1032 */ 1033 if (vp->v_usecount || 1034 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) || 1035 ((vp->v_iflag & VI_FREE) != 0) || 1036 VN_IS_DOOMED(vp) || (vp->v_object != NULL && 1037 vp->v_object->resident_page_count > trigger)) { 1038 VI_UNLOCK(vp); 1039 goto next_iter; 1040 } 1041 MNT_IUNLOCK(mp); 1042 vholdl(vp); 1043 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) { 1044 vdrop(vp); 1045 goto next_iter_mntunlocked; 1046 } 1047 VI_LOCK(vp); 1048 /* 1049 * v_usecount may have been bumped after VOP_LOCK() dropped 1050 * the vnode interlock and before it was locked again. 1051 * 1052 * It is not necessary to recheck VIRF_DOOMED because it can 1053 * only be set by another thread that holds both the vnode 1054 * lock and vnode interlock. If another thread has the 1055 * vnode lock before we get to VOP_LOCK() and obtains the 1056 * vnode interlock after VOP_LOCK() drops the vnode 1057 * interlock, the other thread will be unable to drop the 1058 * vnode lock before our VOP_LOCK() call fails. 1059 */ 1060 if (vp->v_usecount || 1061 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) || 1062 (vp->v_iflag & VI_FREE) != 0 || 1063 (vp->v_object != NULL && 1064 vp->v_object->resident_page_count > trigger)) { 1065 VOP_UNLOCK(vp, 0); 1066 vdropl(vp); 1067 goto next_iter_mntunlocked; 1068 } 1069 KASSERT(!VN_IS_DOOMED(vp), 1070 ("VIRF_DOOMED unexpectedly detected in vlrureclaim()")); 1071 counter_u64_add(recycles_count, 1); 1072 vgonel(vp); 1073 VOP_UNLOCK(vp, 0); 1074 vdropl(vp); 1075 done++; 1076 next_iter_mntunlocked: 1077 if (!should_yield()) 1078 goto relock_mnt; 1079 goto yield; 1080 next_iter: 1081 if (!should_yield()) 1082 continue; 1083 MNT_IUNLOCK(mp); 1084 yield: 1085 kern_yield(PRI_USER); 1086 relock_mnt: 1087 MNT_ILOCK(mp); 1088 } 1089 MNT_IUNLOCK(mp); 1090 vn_finished_write(mp); 1091 return done; 1092 } 1093 1094 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */ 1095 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free, 1096 0, 1097 "limit on vnode free requests per call to the vnlru_free routine"); 1098 1099 /* 1100 * Attempt to reduce the free list by the requested amount. 1101 */ 1102 static void 1103 vnlru_free_locked(int count, struct vfsops *mnt_op) 1104 { 1105 struct vnode *vp; 1106 struct mount *mp; 1107 bool tried_batches; 1108 1109 tried_batches = false; 1110 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 1111 if (count > max_vnlru_free) 1112 count = max_vnlru_free; 1113 for (; count > 0; count--) { 1114 vp = TAILQ_FIRST(&vnode_free_list); 1115 /* 1116 * The list can be modified while the free_list_mtx 1117 * has been dropped and vp could be NULL here. 1118 */ 1119 if (vp == NULL) { 1120 if (tried_batches) 1121 break; 1122 mtx_unlock(&vnode_free_list_mtx); 1123 vnlru_return_batches(mnt_op); 1124 tried_batches = true; 1125 mtx_lock(&vnode_free_list_mtx); 1126 continue; 1127 } 1128 1129 VNASSERT(vp->v_op != NULL, vp, 1130 ("vnlru_free: vnode already reclaimed.")); 1131 KASSERT((vp->v_iflag & VI_FREE) != 0, 1132 ("Removing vnode not on freelist")); 1133 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 1134 ("Mangling active vnode")); 1135 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist); 1136 1137 /* 1138 * Don't recycle if our vnode is from different type 1139 * of mount point. Note that mp is type-safe, the 1140 * check does not reach unmapped address even if 1141 * vnode is reclaimed. 1142 * Don't recycle if we can't get the interlock without 1143 * blocking. 1144 */ 1145 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL && 1146 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) { 1147 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist); 1148 continue; 1149 } 1150 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0, 1151 vp, ("vp inconsistent on freelist")); 1152 1153 /* 1154 * The clear of VI_FREE prevents activation of the 1155 * vnode. There is no sense in putting the vnode on 1156 * the mount point active list, only to remove it 1157 * later during recycling. Inline the relevant part 1158 * of vholdl(), to avoid triggering assertions or 1159 * activating. 1160 */ 1161 freevnodes--; 1162 vp->v_iflag &= ~VI_FREE; 1163 VNODE_REFCOUNT_FENCE_REL(); 1164 refcount_acquire(&vp->v_holdcnt); 1165 1166 mtx_unlock(&vnode_free_list_mtx); 1167 VI_UNLOCK(vp); 1168 vtryrecycle(vp); 1169 /* 1170 * If the recycled succeeded this vdrop will actually free 1171 * the vnode. If not it will simply place it back on 1172 * the free list. 1173 */ 1174 vdrop(vp); 1175 mtx_lock(&vnode_free_list_mtx); 1176 } 1177 } 1178 1179 void 1180 vnlru_free(int count, struct vfsops *mnt_op) 1181 { 1182 1183 mtx_lock(&vnode_free_list_mtx); 1184 vnlru_free_locked(count, mnt_op); 1185 mtx_unlock(&vnode_free_list_mtx); 1186 } 1187 1188 1189 /* XXX some names and initialization are bad for limits and watermarks. */ 1190 static int 1191 vspace(void) 1192 { 1193 int space; 1194 1195 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100); 1196 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */ 1197 vlowat = vhiwat / 2; 1198 if (numvnodes > desiredvnodes) 1199 return (0); 1200 space = desiredvnodes - numvnodes; 1201 if (freevnodes > wantfreevnodes) 1202 space += freevnodes - wantfreevnodes; 1203 return (space); 1204 } 1205 1206 static void 1207 vnlru_return_batch_locked(struct mount *mp) 1208 { 1209 struct vnode *vp; 1210 1211 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 1212 1213 if (mp->mnt_tmpfreevnodelistsize == 0) 1214 return; 1215 1216 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) { 1217 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp, 1218 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist")); 1219 vp->v_mflag &= ~VMP_TMPMNTFREELIST; 1220 } 1221 mtx_lock(&vnode_free_list_mtx); 1222 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist); 1223 freevnodes += mp->mnt_tmpfreevnodelistsize; 1224 mtx_unlock(&vnode_free_list_mtx); 1225 mp->mnt_tmpfreevnodelistsize = 0; 1226 } 1227 1228 static void 1229 vnlru_return_batch(struct mount *mp) 1230 { 1231 1232 mtx_lock(&mp->mnt_listmtx); 1233 vnlru_return_batch_locked(mp); 1234 mtx_unlock(&mp->mnt_listmtx); 1235 } 1236 1237 static void 1238 vnlru_return_batches(struct vfsops *mnt_op) 1239 { 1240 struct mount *mp, *nmp; 1241 bool need_unbusy; 1242 1243 mtx_lock(&mountlist_mtx); 1244 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 1245 need_unbusy = false; 1246 if (mnt_op != NULL && mp->mnt_op != mnt_op) 1247 goto next; 1248 if (mp->mnt_tmpfreevnodelistsize == 0) 1249 goto next; 1250 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) { 1251 vnlru_return_batch(mp); 1252 need_unbusy = true; 1253 mtx_lock(&mountlist_mtx); 1254 } 1255 next: 1256 nmp = TAILQ_NEXT(mp, mnt_list); 1257 if (need_unbusy) 1258 vfs_unbusy(mp); 1259 } 1260 mtx_unlock(&mountlist_mtx); 1261 } 1262 1263 /* 1264 * Attempt to recycle vnodes in a context that is always safe to block. 1265 * Calling vlrurecycle() from the bowels of filesystem code has some 1266 * interesting deadlock problems. 1267 */ 1268 static struct proc *vnlruproc; 1269 static int vnlruproc_sig; 1270 1271 static void 1272 vnlru_proc(void) 1273 { 1274 struct mount *mp, *nmp; 1275 unsigned long onumvnodes; 1276 int done, force, trigger, usevnodes, vsp; 1277 bool reclaim_nc_src; 1278 1279 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc, 1280 SHUTDOWN_PRI_FIRST); 1281 1282 force = 0; 1283 for (;;) { 1284 kproc_suspend_check(vnlruproc); 1285 mtx_lock(&vnode_free_list_mtx); 1286 /* 1287 * If numvnodes is too large (due to desiredvnodes being 1288 * adjusted using its sysctl, or emergency growth), first 1289 * try to reduce it by discarding from the free list. 1290 */ 1291 if (numvnodes > desiredvnodes) 1292 vnlru_free_locked(numvnodes - desiredvnodes, NULL); 1293 /* 1294 * Sleep if the vnode cache is in a good state. This is 1295 * when it is not over-full and has space for about a 4% 1296 * or 9% expansion (by growing its size or inexcessively 1297 * reducing its free list). Otherwise, try to reclaim 1298 * space for a 10% expansion. 1299 */ 1300 if (vstir && force == 0) { 1301 force = 1; 1302 vstir = 0; 1303 } 1304 vsp = vspace(); 1305 if (vsp >= vlowat && force == 0) { 1306 vnlruproc_sig = 0; 1307 wakeup(&vnlruproc_sig); 1308 msleep(vnlruproc, &vnode_free_list_mtx, 1309 PVFS|PDROP, "vlruwt", hz); 1310 continue; 1311 } 1312 mtx_unlock(&vnode_free_list_mtx); 1313 done = 0; 1314 onumvnodes = numvnodes; 1315 /* 1316 * Calculate parameters for recycling. These are the same 1317 * throughout the loop to give some semblance of fairness. 1318 * The trigger point is to avoid recycling vnodes with lots 1319 * of resident pages. We aren't trying to free memory; we 1320 * are trying to recycle or at least free vnodes. 1321 */ 1322 if (numvnodes <= desiredvnodes) 1323 usevnodes = numvnodes - freevnodes; 1324 else 1325 usevnodes = numvnodes; 1326 if (usevnodes <= 0) 1327 usevnodes = 1; 1328 /* 1329 * The trigger value is is chosen to give a conservatively 1330 * large value to ensure that it alone doesn't prevent 1331 * making progress. The value can easily be so large that 1332 * it is effectively infinite in some congested and 1333 * misconfigured cases, and this is necessary. Normally 1334 * it is about 8 to 100 (pages), which is quite large. 1335 */ 1336 trigger = vm_cnt.v_page_count * 2 / usevnodes; 1337 if (force < 2) 1338 trigger = vsmalltrigger; 1339 reclaim_nc_src = force >= 3; 1340 mtx_lock(&mountlist_mtx); 1341 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 1342 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) { 1343 nmp = TAILQ_NEXT(mp, mnt_list); 1344 continue; 1345 } 1346 done += vlrureclaim(mp, reclaim_nc_src, trigger); 1347 mtx_lock(&mountlist_mtx); 1348 nmp = TAILQ_NEXT(mp, mnt_list); 1349 vfs_unbusy(mp); 1350 } 1351 mtx_unlock(&mountlist_mtx); 1352 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes) 1353 uma_reclaim(UMA_RECLAIM_DRAIN); 1354 if (done == 0) { 1355 if (force == 0 || force == 1) { 1356 force = 2; 1357 continue; 1358 } 1359 if (force == 2) { 1360 force = 3; 1361 continue; 1362 } 1363 force = 0; 1364 vnlru_nowhere++; 1365 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 1366 } else 1367 kern_yield(PRI_USER); 1368 /* 1369 * After becoming active to expand above low water, keep 1370 * active until above high water. 1371 */ 1372 vsp = vspace(); 1373 force = vsp < vhiwat; 1374 } 1375 } 1376 1377 static struct kproc_desc vnlru_kp = { 1378 "vnlru", 1379 vnlru_proc, 1380 &vnlruproc 1381 }; 1382 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, 1383 &vnlru_kp); 1384 1385 /* 1386 * Routines having to do with the management of the vnode table. 1387 */ 1388 1389 /* 1390 * Try to recycle a freed vnode. We abort if anyone picks up a reference 1391 * before we actually vgone(). This function must be called with the vnode 1392 * held to prevent the vnode from being returned to the free list midway 1393 * through vgone(). 1394 */ 1395 static int 1396 vtryrecycle(struct vnode *vp) 1397 { 1398 struct mount *vnmp; 1399 1400 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 1401 VNASSERT(vp->v_holdcnt, vp, 1402 ("vtryrecycle: Recycling vp %p without a reference.", vp)); 1403 /* 1404 * This vnode may found and locked via some other list, if so we 1405 * can't recycle it yet. 1406 */ 1407 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1408 CTR2(KTR_VFS, 1409 "%s: impossible to recycle, vp %p lock is already held", 1410 __func__, vp); 1411 return (EWOULDBLOCK); 1412 } 1413 /* 1414 * Don't recycle if its filesystem is being suspended. 1415 */ 1416 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) { 1417 VOP_UNLOCK(vp, 0); 1418 CTR2(KTR_VFS, 1419 "%s: impossible to recycle, cannot start the write for %p", 1420 __func__, vp); 1421 return (EBUSY); 1422 } 1423 /* 1424 * If we got this far, we need to acquire the interlock and see if 1425 * anyone picked up this vnode from another list. If not, we will 1426 * mark it with DOOMED via vgonel() so that anyone who does find it 1427 * will skip over it. 1428 */ 1429 VI_LOCK(vp); 1430 if (vp->v_usecount) { 1431 VOP_UNLOCK(vp, 0); 1432 VI_UNLOCK(vp); 1433 vn_finished_write(vnmp); 1434 CTR2(KTR_VFS, 1435 "%s: impossible to recycle, %p is already referenced", 1436 __func__, vp); 1437 return (EBUSY); 1438 } 1439 if (!VN_IS_DOOMED(vp)) { 1440 counter_u64_add(recycles_count, 1); 1441 vgonel(vp); 1442 } 1443 VOP_UNLOCK(vp, 0); 1444 VI_UNLOCK(vp); 1445 vn_finished_write(vnmp); 1446 return (0); 1447 } 1448 1449 static void 1450 vcheckspace(void) 1451 { 1452 int vsp; 1453 1454 vsp = vspace(); 1455 if (vsp < vlowat && vnlruproc_sig == 0) { 1456 vnlruproc_sig = 1; 1457 wakeup(vnlruproc); 1458 } 1459 } 1460 1461 /* 1462 * Wait if necessary for space for a new vnode. 1463 */ 1464 static int 1465 getnewvnode_wait(int suspended) 1466 { 1467 1468 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 1469 if (numvnodes >= desiredvnodes) { 1470 if (suspended) { 1471 /* 1472 * The file system is being suspended. We cannot 1473 * risk a deadlock here, so allow allocation of 1474 * another vnode even if this would give too many. 1475 */ 1476 return (0); 1477 } 1478 if (vnlruproc_sig == 0) { 1479 vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 1480 wakeup(vnlruproc); 1481 } 1482 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS, 1483 "vlruwk", hz); 1484 } 1485 /* Post-adjust like the pre-adjust in getnewvnode(). */ 1486 if (numvnodes + 1 > desiredvnodes && freevnodes > 1) 1487 vnlru_free_locked(1, NULL); 1488 return (numvnodes >= desiredvnodes ? ENFILE : 0); 1489 } 1490 1491 /* 1492 * This hack is fragile, and probably not needed any more now that the 1493 * watermark handling works. 1494 */ 1495 void 1496 getnewvnode_reserve(u_int count) 1497 { 1498 struct thread *td; 1499 1500 /* Pre-adjust like the pre-adjust in getnewvnode(), with any count. */ 1501 /* XXX no longer so quick, but this part is not racy. */ 1502 mtx_lock(&vnode_free_list_mtx); 1503 if (numvnodes + count > desiredvnodes && freevnodes > wantfreevnodes) 1504 vnlru_free_locked(ulmin(numvnodes + count - desiredvnodes, 1505 freevnodes - wantfreevnodes), NULL); 1506 mtx_unlock(&vnode_free_list_mtx); 1507 1508 td = curthread; 1509 /* First try to be quick and racy. */ 1510 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) { 1511 td->td_vp_reserv += count; 1512 vcheckspace(); /* XXX no longer so quick, but more racy */ 1513 return; 1514 } else 1515 atomic_subtract_long(&numvnodes, count); 1516 1517 mtx_lock(&vnode_free_list_mtx); 1518 while (count > 0) { 1519 if (getnewvnode_wait(0) == 0) { 1520 count--; 1521 td->td_vp_reserv++; 1522 atomic_add_long(&numvnodes, 1); 1523 } 1524 } 1525 vcheckspace(); 1526 mtx_unlock(&vnode_free_list_mtx); 1527 } 1528 1529 /* 1530 * This hack is fragile, especially if desiredvnodes or wantvnodes are 1531 * misconfgured or changed significantly. Reducing desiredvnodes below 1532 * the reserved amount should cause bizarre behaviour like reducing it 1533 * below the number of active vnodes -- the system will try to reduce 1534 * numvnodes to match, but should fail, so the subtraction below should 1535 * not overflow. 1536 */ 1537 void 1538 getnewvnode_drop_reserve(void) 1539 { 1540 struct thread *td; 1541 1542 td = curthread; 1543 atomic_subtract_long(&numvnodes, td->td_vp_reserv); 1544 td->td_vp_reserv = 0; 1545 } 1546 1547 /* 1548 * Return the next vnode from the free list. 1549 */ 1550 int 1551 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops, 1552 struct vnode **vpp) 1553 { 1554 struct vnode *vp; 1555 struct thread *td; 1556 struct lock_object *lo; 1557 static int cyclecount; 1558 int error __unused; 1559 1560 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag); 1561 vp = NULL; 1562 td = curthread; 1563 if (td->td_vp_reserv > 0) { 1564 td->td_vp_reserv -= 1; 1565 goto alloc; 1566 } 1567 mtx_lock(&vnode_free_list_mtx); 1568 if (numvnodes < desiredvnodes) 1569 cyclecount = 0; 1570 else if (cyclecount++ >= freevnodes) { 1571 cyclecount = 0; 1572 vstir = 1; 1573 } 1574 /* 1575 * Grow the vnode cache if it will not be above its target max 1576 * after growing. Otherwise, if the free list is nonempty, try 1577 * to reclaim 1 item from it before growing the cache (possibly 1578 * above its target max if the reclamation failed or is delayed). 1579 * Otherwise, wait for some space. In all cases, schedule 1580 * vnlru_proc() if we are getting short of space. The watermarks 1581 * should be chosen so that we never wait or even reclaim from 1582 * the free list to below its target minimum. 1583 */ 1584 if (numvnodes + 1 <= desiredvnodes) 1585 ; 1586 else if (freevnodes > 0) 1587 vnlru_free_locked(1, NULL); 1588 else { 1589 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag & 1590 MNTK_SUSPEND)); 1591 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */ 1592 if (error != 0) { 1593 mtx_unlock(&vnode_free_list_mtx); 1594 return (error); 1595 } 1596 #endif 1597 } 1598 vcheckspace(); 1599 atomic_add_long(&numvnodes, 1); 1600 mtx_unlock(&vnode_free_list_mtx); 1601 alloc: 1602 counter_u64_add(vnodes_created, 1); 1603 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK); 1604 /* 1605 * Locks are given the generic name "vnode" when created. 1606 * Follow the historic practice of using the filesystem 1607 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc. 1608 * 1609 * Locks live in a witness group keyed on their name. Thus, 1610 * when a lock is renamed, it must also move from the witness 1611 * group of its old name to the witness group of its new name. 1612 * 1613 * The change only needs to be made when the vnode moves 1614 * from one filesystem type to another. We ensure that each 1615 * filesystem use a single static name pointer for its tag so 1616 * that we can compare pointers rather than doing a strcmp(). 1617 */ 1618 lo = &vp->v_vnlock->lock_object; 1619 if (lo->lo_name != tag) { 1620 lo->lo_name = tag; 1621 WITNESS_DESTROY(lo); 1622 WITNESS_INIT(lo, tag); 1623 } 1624 /* 1625 * By default, don't allow shared locks unless filesystems opt-in. 1626 */ 1627 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE; 1628 /* 1629 * Finalize various vnode identity bits. 1630 */ 1631 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp)); 1632 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp)); 1633 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp)); 1634 vp->v_type = VNON; 1635 vp->v_tag = tag; 1636 vp->v_op = vops; 1637 v_init_counters(vp); 1638 vp->v_bufobj.bo_ops = &buf_ops_bio; 1639 #ifdef DIAGNOSTIC 1640 if (mp == NULL && vops != &dead_vnodeops) 1641 printf("NULL mp in getnewvnode(9), tag %s\n", tag); 1642 #endif 1643 #ifdef MAC 1644 mac_vnode_init(vp); 1645 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 1646 mac_vnode_associate_singlelabel(mp, vp); 1647 #endif 1648 if (mp != NULL) { 1649 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize; 1650 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0) 1651 vp->v_vflag |= VV_NOKNOTE; 1652 } 1653 1654 /* 1655 * For the filesystems which do not use vfs_hash_insert(), 1656 * still initialize v_hash to have vfs_hash_index() useful. 1657 * E.g., nullfs uses vfs_hash_index() on the lower vnode for 1658 * its own hashing. 1659 */ 1660 vp->v_hash = (uintptr_t)vp >> vnsz2log; 1661 1662 *vpp = vp; 1663 return (0); 1664 } 1665 1666 static void 1667 freevnode(struct vnode *vp) 1668 { 1669 struct bufobj *bo; 1670 1671 /* 1672 * The vnode has been marked for destruction, so free it. 1673 * 1674 * The vnode will be returned to the zone where it will 1675 * normally remain until it is needed for another vnode. We 1676 * need to cleanup (or verify that the cleanup has already 1677 * been done) any residual data left from its current use 1678 * so as not to contaminate the freshly allocated vnode. 1679 */ 1680 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp); 1681 atomic_subtract_long(&numvnodes, 1); 1682 bo = &vp->v_bufobj; 1683 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 1684 ("cleaned vnode still on the free list.")); 1685 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't")); 1686 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count")); 1687 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count")); 1688 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count")); 1689 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's")); 1690 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0")); 1691 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp, 1692 ("clean blk trie not empty")); 1693 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0")); 1694 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp, 1695 ("dirty blk trie not empty")); 1696 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst")); 1697 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src")); 1698 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for ..")); 1699 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp, 1700 ("Dangling rangelock waiters")); 1701 VI_UNLOCK(vp); 1702 #ifdef MAC 1703 mac_vnode_destroy(vp); 1704 #endif 1705 if (vp->v_pollinfo != NULL) { 1706 destroy_vpollinfo(vp->v_pollinfo); 1707 vp->v_pollinfo = NULL; 1708 } 1709 #ifdef INVARIANTS 1710 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */ 1711 vp->v_op = NULL; 1712 #endif 1713 vp->v_mountedhere = NULL; 1714 vp->v_unpcb = NULL; 1715 vp->v_rdev = NULL; 1716 vp->v_fifoinfo = NULL; 1717 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 1718 vp->v_irflag = 0; 1719 vp->v_iflag = 0; 1720 vp->v_vflag = 0; 1721 bo->bo_flag = 0; 1722 uma_zfree(vnode_zone, vp); 1723 } 1724 1725 /* 1726 * Delete from old mount point vnode list, if on one. 1727 */ 1728 static void 1729 delmntque(struct vnode *vp) 1730 { 1731 struct mount *mp; 1732 1733 mp = vp->v_mount; 1734 if (mp == NULL) 1735 return; 1736 MNT_ILOCK(mp); 1737 VI_LOCK(vp); 1738 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize, 1739 ("Active vnode list size %d > Vnode list size %d", 1740 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize)); 1741 if (vp->v_iflag & VI_ACTIVE) { 1742 vp->v_iflag &= ~VI_ACTIVE; 1743 mtx_lock(&mp->mnt_listmtx); 1744 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist); 1745 mp->mnt_activevnodelistsize--; 1746 mtx_unlock(&mp->mnt_listmtx); 1747 } 1748 vp->v_mount = NULL; 1749 VI_UNLOCK(vp); 1750 VNASSERT(mp->mnt_nvnodelistsize > 0, vp, 1751 ("bad mount point vnode list size")); 1752 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1753 mp->mnt_nvnodelistsize--; 1754 MNT_REL(mp); 1755 MNT_IUNLOCK(mp); 1756 } 1757 1758 static void 1759 insmntque_stddtr(struct vnode *vp, void *dtr_arg) 1760 { 1761 1762 vp->v_data = NULL; 1763 vp->v_op = &dead_vnodeops; 1764 vgone(vp); 1765 vput(vp); 1766 } 1767 1768 /* 1769 * Insert into list of vnodes for the new mount point, if available. 1770 */ 1771 int 1772 insmntque1(struct vnode *vp, struct mount *mp, 1773 void (*dtr)(struct vnode *, void *), void *dtr_arg) 1774 { 1775 1776 KASSERT(vp->v_mount == NULL, 1777 ("insmntque: vnode already on per mount vnode list")); 1778 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)")); 1779 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp"); 1780 1781 /* 1782 * We acquire the vnode interlock early to ensure that the 1783 * vnode cannot be recycled by another process releasing a 1784 * holdcnt on it before we get it on both the vnode list 1785 * and the active vnode list. The mount mutex protects only 1786 * manipulation of the vnode list and the vnode freelist 1787 * mutex protects only manipulation of the active vnode list. 1788 * Hence the need to hold the vnode interlock throughout. 1789 */ 1790 MNT_ILOCK(mp); 1791 VI_LOCK(vp); 1792 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 && 1793 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 || 1794 mp->mnt_nvnodelistsize == 0)) && 1795 (vp->v_vflag & VV_FORCEINSMQ) == 0) { 1796 VI_UNLOCK(vp); 1797 MNT_IUNLOCK(mp); 1798 if (dtr != NULL) 1799 dtr(vp, dtr_arg); 1800 return (EBUSY); 1801 } 1802 vp->v_mount = mp; 1803 MNT_REF(mp); 1804 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1805 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp, 1806 ("neg mount point vnode list size")); 1807 mp->mnt_nvnodelistsize++; 1808 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 1809 ("Activating already active vnode")); 1810 vp->v_iflag |= VI_ACTIVE; 1811 mtx_lock(&mp->mnt_listmtx); 1812 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist); 1813 mp->mnt_activevnodelistsize++; 1814 mtx_unlock(&mp->mnt_listmtx); 1815 VI_UNLOCK(vp); 1816 MNT_IUNLOCK(mp); 1817 return (0); 1818 } 1819 1820 int 1821 insmntque(struct vnode *vp, struct mount *mp) 1822 { 1823 1824 return (insmntque1(vp, mp, insmntque_stddtr, NULL)); 1825 } 1826 1827 /* 1828 * Flush out and invalidate all buffers associated with a bufobj 1829 * Called with the underlying object locked. 1830 */ 1831 int 1832 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo) 1833 { 1834 int error; 1835 1836 BO_LOCK(bo); 1837 if (flags & V_SAVE) { 1838 error = bufobj_wwait(bo, slpflag, slptimeo); 1839 if (error) { 1840 BO_UNLOCK(bo); 1841 return (error); 1842 } 1843 if (bo->bo_dirty.bv_cnt > 0) { 1844 BO_UNLOCK(bo); 1845 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0) 1846 return (error); 1847 /* 1848 * XXX We could save a lock/unlock if this was only 1849 * enabled under INVARIANTS 1850 */ 1851 BO_LOCK(bo); 1852 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1853 panic("vinvalbuf: dirty bufs"); 1854 } 1855 } 1856 /* 1857 * If you alter this loop please notice that interlock is dropped and 1858 * reacquired in flushbuflist. Special care is needed to ensure that 1859 * no race conditions occur from this. 1860 */ 1861 do { 1862 error = flushbuflist(&bo->bo_clean, 1863 flags, bo, slpflag, slptimeo); 1864 if (error == 0 && !(flags & V_CLEANONLY)) 1865 error = flushbuflist(&bo->bo_dirty, 1866 flags, bo, slpflag, slptimeo); 1867 if (error != 0 && error != EAGAIN) { 1868 BO_UNLOCK(bo); 1869 return (error); 1870 } 1871 } while (error != 0); 1872 1873 /* 1874 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1875 * have write I/O in-progress but if there is a VM object then the 1876 * VM object can also have read-I/O in-progress. 1877 */ 1878 do { 1879 bufobj_wwait(bo, 0, 0); 1880 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) { 1881 BO_UNLOCK(bo); 1882 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx"); 1883 BO_LOCK(bo); 1884 } 1885 } while (bo->bo_numoutput > 0); 1886 BO_UNLOCK(bo); 1887 1888 /* 1889 * Destroy the copy in the VM cache, too. 1890 */ 1891 if (bo->bo_object != NULL && 1892 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) { 1893 VM_OBJECT_WLOCK(bo->bo_object); 1894 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ? 1895 OBJPR_CLEANONLY : 0); 1896 VM_OBJECT_WUNLOCK(bo->bo_object); 1897 } 1898 1899 #ifdef INVARIANTS 1900 BO_LOCK(bo); 1901 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO | 1902 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 || 1903 bo->bo_clean.bv_cnt > 0)) 1904 panic("vinvalbuf: flush failed"); 1905 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 && 1906 bo->bo_dirty.bv_cnt > 0) 1907 panic("vinvalbuf: flush dirty failed"); 1908 BO_UNLOCK(bo); 1909 #endif 1910 return (0); 1911 } 1912 1913 /* 1914 * Flush out and invalidate all buffers associated with a vnode. 1915 * Called with the underlying object locked. 1916 */ 1917 int 1918 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 1919 { 1920 1921 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 1922 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1923 if (vp->v_object != NULL && vp->v_object->handle != vp) 1924 return (0); 1925 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo)); 1926 } 1927 1928 /* 1929 * Flush out buffers on the specified list. 1930 * 1931 */ 1932 static int 1933 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 1934 int slptimeo) 1935 { 1936 struct buf *bp, *nbp; 1937 int retval, error; 1938 daddr_t lblkno; 1939 b_xflags_t xflags; 1940 1941 ASSERT_BO_WLOCKED(bo); 1942 1943 retval = 0; 1944 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 1945 /* 1946 * If we are flushing both V_NORMAL and V_ALT buffers then 1947 * do not skip any buffers. If we are flushing only V_NORMAL 1948 * buffers then skip buffers marked as BX_ALTDATA. If we are 1949 * flushing only V_ALT buffers then skip buffers not marked 1950 * as BX_ALTDATA. 1951 */ 1952 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) && 1953 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) || 1954 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) { 1955 continue; 1956 } 1957 if (nbp != NULL) { 1958 lblkno = nbp->b_lblkno; 1959 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN); 1960 } 1961 retval = EAGAIN; 1962 error = BUF_TIMELOCK(bp, 1963 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo), 1964 "flushbuf", slpflag, slptimeo); 1965 if (error) { 1966 BO_LOCK(bo); 1967 return (error != ENOLCK ? error : EAGAIN); 1968 } 1969 KASSERT(bp->b_bufobj == bo, 1970 ("bp %p wrong b_bufobj %p should be %p", 1971 bp, bp->b_bufobj, bo)); 1972 /* 1973 * XXX Since there are no node locks for NFS, I 1974 * believe there is a slight chance that a delayed 1975 * write will occur while sleeping just above, so 1976 * check for it. 1977 */ 1978 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1979 (flags & V_SAVE)) { 1980 bremfree(bp); 1981 bp->b_flags |= B_ASYNC; 1982 bwrite(bp); 1983 BO_LOCK(bo); 1984 return (EAGAIN); /* XXX: why not loop ? */ 1985 } 1986 bremfree(bp); 1987 bp->b_flags |= (B_INVAL | B_RELBUF); 1988 bp->b_flags &= ~B_ASYNC; 1989 brelse(bp); 1990 BO_LOCK(bo); 1991 if (nbp == NULL) 1992 break; 1993 nbp = gbincore(bo, lblkno); 1994 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1995 != xflags) 1996 break; /* nbp invalid */ 1997 } 1998 return (retval); 1999 } 2000 2001 int 2002 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn) 2003 { 2004 struct buf *bp; 2005 int error; 2006 daddr_t lblkno; 2007 2008 ASSERT_BO_LOCKED(bo); 2009 2010 for (lblkno = startn;;) { 2011 again: 2012 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno); 2013 if (bp == NULL || bp->b_lblkno >= endn || 2014 bp->b_lblkno < startn) 2015 break; 2016 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 2017 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0); 2018 if (error != 0) { 2019 BO_RLOCK(bo); 2020 if (error == ENOLCK) 2021 goto again; 2022 return (error); 2023 } 2024 KASSERT(bp->b_bufobj == bo, 2025 ("bp %p wrong b_bufobj %p should be %p", 2026 bp, bp->b_bufobj, bo)); 2027 lblkno = bp->b_lblkno + 1; 2028 if ((bp->b_flags & B_MANAGED) == 0) 2029 bremfree(bp); 2030 bp->b_flags |= B_RELBUF; 2031 /* 2032 * In the VMIO case, use the B_NOREUSE flag to hint that the 2033 * pages backing each buffer in the range are unlikely to be 2034 * reused. Dirty buffers will have the hint applied once 2035 * they've been written. 2036 */ 2037 if ((bp->b_flags & B_VMIO) != 0) 2038 bp->b_flags |= B_NOREUSE; 2039 brelse(bp); 2040 BO_RLOCK(bo); 2041 } 2042 return (0); 2043 } 2044 2045 /* 2046 * Truncate a file's buffer and pages to a specified length. This 2047 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 2048 * sync activity. 2049 */ 2050 int 2051 vtruncbuf(struct vnode *vp, off_t length, int blksize) 2052 { 2053 struct buf *bp, *nbp; 2054 struct bufobj *bo; 2055 daddr_t startlbn; 2056 2057 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__, 2058 vp, blksize, (uintmax_t)length); 2059 2060 /* 2061 * Round up to the *next* lbn. 2062 */ 2063 startlbn = howmany(length, blksize); 2064 2065 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 2066 2067 bo = &vp->v_bufobj; 2068 restart_unlocked: 2069 BO_LOCK(bo); 2070 2071 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN) 2072 ; 2073 2074 if (length > 0) { 2075 restartsync: 2076 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2077 if (bp->b_lblkno > 0) 2078 continue; 2079 /* 2080 * Since we hold the vnode lock this should only 2081 * fail if we're racing with the buf daemon. 2082 */ 2083 if (BUF_LOCK(bp, 2084 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2085 BO_LOCKPTR(bo)) == ENOLCK) 2086 goto restart_unlocked; 2087 2088 VNASSERT((bp->b_flags & B_DELWRI), vp, 2089 ("buf(%p) on dirty queue without DELWRI", bp)); 2090 2091 bremfree(bp); 2092 bawrite(bp); 2093 BO_LOCK(bo); 2094 goto restartsync; 2095 } 2096 } 2097 2098 bufobj_wwait(bo, 0, 0); 2099 BO_UNLOCK(bo); 2100 vnode_pager_setsize(vp, length); 2101 2102 return (0); 2103 } 2104 2105 /* 2106 * Invalidate the cached pages of a file's buffer within the range of block 2107 * numbers [startlbn, endlbn). 2108 */ 2109 void 2110 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn, 2111 int blksize) 2112 { 2113 struct bufobj *bo; 2114 off_t start, end; 2115 2116 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range"); 2117 2118 start = blksize * startlbn; 2119 end = blksize * endlbn; 2120 2121 bo = &vp->v_bufobj; 2122 BO_LOCK(bo); 2123 MPASS(blksize == bo->bo_bsize); 2124 2125 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN) 2126 ; 2127 2128 BO_UNLOCK(bo); 2129 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1)); 2130 } 2131 2132 static int 2133 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo, 2134 daddr_t startlbn, daddr_t endlbn) 2135 { 2136 struct buf *bp, *nbp; 2137 bool anyfreed; 2138 2139 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked"); 2140 ASSERT_BO_LOCKED(bo); 2141 2142 do { 2143 anyfreed = false; 2144 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 2145 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2146 continue; 2147 if (BUF_LOCK(bp, 2148 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2149 BO_LOCKPTR(bo)) == ENOLCK) { 2150 BO_LOCK(bo); 2151 return (EAGAIN); 2152 } 2153 2154 bremfree(bp); 2155 bp->b_flags |= B_INVAL | B_RELBUF; 2156 bp->b_flags &= ~B_ASYNC; 2157 brelse(bp); 2158 anyfreed = true; 2159 2160 BO_LOCK(bo); 2161 if (nbp != NULL && 2162 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 2163 nbp->b_vp != vp || 2164 (nbp->b_flags & B_DELWRI) != 0)) 2165 return (EAGAIN); 2166 } 2167 2168 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2169 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2170 continue; 2171 if (BUF_LOCK(bp, 2172 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2173 BO_LOCKPTR(bo)) == ENOLCK) { 2174 BO_LOCK(bo); 2175 return (EAGAIN); 2176 } 2177 bremfree(bp); 2178 bp->b_flags |= B_INVAL | B_RELBUF; 2179 bp->b_flags &= ~B_ASYNC; 2180 brelse(bp); 2181 anyfreed = true; 2182 2183 BO_LOCK(bo); 2184 if (nbp != NULL && 2185 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 2186 (nbp->b_vp != vp) || 2187 (nbp->b_flags & B_DELWRI) == 0)) 2188 return (EAGAIN); 2189 } 2190 } while (anyfreed); 2191 return (0); 2192 } 2193 2194 static void 2195 buf_vlist_remove(struct buf *bp) 2196 { 2197 struct bufv *bv; 2198 2199 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 2200 ASSERT_BO_WLOCKED(bp->b_bufobj); 2201 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 2202 (BX_VNDIRTY|BX_VNCLEAN), 2203 ("buf_vlist_remove: Buf %p is on two lists", bp)); 2204 if (bp->b_xflags & BX_VNDIRTY) 2205 bv = &bp->b_bufobj->bo_dirty; 2206 else 2207 bv = &bp->b_bufobj->bo_clean; 2208 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno); 2209 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 2210 bv->bv_cnt--; 2211 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 2212 } 2213 2214 /* 2215 * Add the buffer to the sorted clean or dirty block list. 2216 * 2217 * NOTE: xflags is passed as a constant, optimizing this inline function! 2218 */ 2219 static void 2220 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 2221 { 2222 struct bufv *bv; 2223 struct buf *n; 2224 int error; 2225 2226 ASSERT_BO_WLOCKED(bo); 2227 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0, 2228 ("dead bo %p", bo)); 2229 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 2230 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 2231 bp->b_xflags |= xflags; 2232 if (xflags & BX_VNDIRTY) 2233 bv = &bo->bo_dirty; 2234 else 2235 bv = &bo->bo_clean; 2236 2237 /* 2238 * Keep the list ordered. Optimize empty list insertion. Assume 2239 * we tend to grow at the tail so lookup_le should usually be cheaper 2240 * than _ge. 2241 */ 2242 if (bv->bv_cnt == 0 || 2243 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno) 2244 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 2245 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL) 2246 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs); 2247 else 2248 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs); 2249 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp); 2250 if (error) 2251 panic("buf_vlist_add: Preallocated nodes insufficient."); 2252 bv->bv_cnt++; 2253 } 2254 2255 /* 2256 * Look up a buffer using the buffer tries. 2257 */ 2258 struct buf * 2259 gbincore(struct bufobj *bo, daddr_t lblkno) 2260 { 2261 struct buf *bp; 2262 2263 ASSERT_BO_LOCKED(bo); 2264 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno); 2265 if (bp != NULL) 2266 return (bp); 2267 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno); 2268 } 2269 2270 /* 2271 * Associate a buffer with a vnode. 2272 */ 2273 void 2274 bgetvp(struct vnode *vp, struct buf *bp) 2275 { 2276 struct bufobj *bo; 2277 2278 bo = &vp->v_bufobj; 2279 ASSERT_BO_WLOCKED(bo); 2280 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 2281 2282 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 2283 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 2284 ("bgetvp: bp already attached! %p", bp)); 2285 2286 vhold(vp); 2287 bp->b_vp = vp; 2288 bp->b_bufobj = bo; 2289 /* 2290 * Insert onto list for new vnode. 2291 */ 2292 buf_vlist_add(bp, bo, BX_VNCLEAN); 2293 } 2294 2295 /* 2296 * Disassociate a buffer from a vnode. 2297 */ 2298 void 2299 brelvp(struct buf *bp) 2300 { 2301 struct bufobj *bo; 2302 struct vnode *vp; 2303 2304 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 2305 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 2306 2307 /* 2308 * Delete from old vnode list, if on one. 2309 */ 2310 vp = bp->b_vp; /* XXX */ 2311 bo = bp->b_bufobj; 2312 BO_LOCK(bo); 2313 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2314 buf_vlist_remove(bp); 2315 else 2316 panic("brelvp: Buffer %p not on queue.", bp); 2317 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2318 bo->bo_flag &= ~BO_ONWORKLST; 2319 mtx_lock(&sync_mtx); 2320 LIST_REMOVE(bo, bo_synclist); 2321 syncer_worklist_len--; 2322 mtx_unlock(&sync_mtx); 2323 } 2324 bp->b_vp = NULL; 2325 bp->b_bufobj = NULL; 2326 BO_UNLOCK(bo); 2327 vdrop(vp); 2328 } 2329 2330 /* 2331 * Add an item to the syncer work queue. 2332 */ 2333 static void 2334 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 2335 { 2336 int slot; 2337 2338 ASSERT_BO_WLOCKED(bo); 2339 2340 mtx_lock(&sync_mtx); 2341 if (bo->bo_flag & BO_ONWORKLST) 2342 LIST_REMOVE(bo, bo_synclist); 2343 else { 2344 bo->bo_flag |= BO_ONWORKLST; 2345 syncer_worklist_len++; 2346 } 2347 2348 if (delay > syncer_maxdelay - 2) 2349 delay = syncer_maxdelay - 2; 2350 slot = (syncer_delayno + delay) & syncer_mask; 2351 2352 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist); 2353 mtx_unlock(&sync_mtx); 2354 } 2355 2356 static int 2357 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 2358 { 2359 int error, len; 2360 2361 mtx_lock(&sync_mtx); 2362 len = syncer_worklist_len - sync_vnode_count; 2363 mtx_unlock(&sync_mtx); 2364 error = SYSCTL_OUT(req, &len, sizeof(len)); 2365 return (error); 2366 } 2367 2368 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 2369 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 2370 2371 static struct proc *updateproc; 2372 static void sched_sync(void); 2373 static struct kproc_desc up_kp = { 2374 "syncer", 2375 sched_sync, 2376 &updateproc 2377 }; 2378 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 2379 2380 static int 2381 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 2382 { 2383 struct vnode *vp; 2384 struct mount *mp; 2385 2386 *bo = LIST_FIRST(slp); 2387 if (*bo == NULL) 2388 return (0); 2389 vp = bo2vnode(*bo); 2390 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 2391 return (1); 2392 /* 2393 * We use vhold in case the vnode does not 2394 * successfully sync. vhold prevents the vnode from 2395 * going away when we unlock the sync_mtx so that 2396 * we can acquire the vnode interlock. 2397 */ 2398 vholdl(vp); 2399 mtx_unlock(&sync_mtx); 2400 VI_UNLOCK(vp); 2401 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2402 vdrop(vp); 2403 mtx_lock(&sync_mtx); 2404 return (*bo == LIST_FIRST(slp)); 2405 } 2406 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2407 (void) VOP_FSYNC(vp, MNT_LAZY, td); 2408 VOP_UNLOCK(vp, 0); 2409 vn_finished_write(mp); 2410 BO_LOCK(*bo); 2411 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 2412 /* 2413 * Put us back on the worklist. The worklist 2414 * routine will remove us from our current 2415 * position and then add us back in at a later 2416 * position. 2417 */ 2418 vn_syncer_add_to_worklist(*bo, syncdelay); 2419 } 2420 BO_UNLOCK(*bo); 2421 vdrop(vp); 2422 mtx_lock(&sync_mtx); 2423 return (0); 2424 } 2425 2426 static int first_printf = 1; 2427 2428 /* 2429 * System filesystem synchronizer daemon. 2430 */ 2431 static void 2432 sched_sync(void) 2433 { 2434 struct synclist *next, *slp; 2435 struct bufobj *bo; 2436 long starttime; 2437 struct thread *td = curthread; 2438 int last_work_seen; 2439 int net_worklist_len; 2440 int syncer_final_iter; 2441 int error; 2442 2443 last_work_seen = 0; 2444 syncer_final_iter = 0; 2445 syncer_state = SYNCER_RUNNING; 2446 starttime = time_uptime; 2447 td->td_pflags |= TDP_NORUNNINGBUF; 2448 2449 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 2450 SHUTDOWN_PRI_LAST); 2451 2452 mtx_lock(&sync_mtx); 2453 for (;;) { 2454 if (syncer_state == SYNCER_FINAL_DELAY && 2455 syncer_final_iter == 0) { 2456 mtx_unlock(&sync_mtx); 2457 kproc_suspend_check(td->td_proc); 2458 mtx_lock(&sync_mtx); 2459 } 2460 net_worklist_len = syncer_worklist_len - sync_vnode_count; 2461 if (syncer_state != SYNCER_RUNNING && 2462 starttime != time_uptime) { 2463 if (first_printf) { 2464 printf("\nSyncing disks, vnodes remaining... "); 2465 first_printf = 0; 2466 } 2467 printf("%d ", net_worklist_len); 2468 } 2469 starttime = time_uptime; 2470 2471 /* 2472 * Push files whose dirty time has expired. Be careful 2473 * of interrupt race on slp queue. 2474 * 2475 * Skip over empty worklist slots when shutting down. 2476 */ 2477 do { 2478 slp = &syncer_workitem_pending[syncer_delayno]; 2479 syncer_delayno += 1; 2480 if (syncer_delayno == syncer_maxdelay) 2481 syncer_delayno = 0; 2482 next = &syncer_workitem_pending[syncer_delayno]; 2483 /* 2484 * If the worklist has wrapped since the 2485 * it was emptied of all but syncer vnodes, 2486 * switch to the FINAL_DELAY state and run 2487 * for one more second. 2488 */ 2489 if (syncer_state == SYNCER_SHUTTING_DOWN && 2490 net_worklist_len == 0 && 2491 last_work_seen == syncer_delayno) { 2492 syncer_state = SYNCER_FINAL_DELAY; 2493 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 2494 } 2495 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 2496 syncer_worklist_len > 0); 2497 2498 /* 2499 * Keep track of the last time there was anything 2500 * on the worklist other than syncer vnodes. 2501 * Return to the SHUTTING_DOWN state if any 2502 * new work appears. 2503 */ 2504 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 2505 last_work_seen = syncer_delayno; 2506 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 2507 syncer_state = SYNCER_SHUTTING_DOWN; 2508 while (!LIST_EMPTY(slp)) { 2509 error = sync_vnode(slp, &bo, td); 2510 if (error == 1) { 2511 LIST_REMOVE(bo, bo_synclist); 2512 LIST_INSERT_HEAD(next, bo, bo_synclist); 2513 continue; 2514 } 2515 2516 if (first_printf == 0) { 2517 /* 2518 * Drop the sync mutex, because some watchdog 2519 * drivers need to sleep while patting 2520 */ 2521 mtx_unlock(&sync_mtx); 2522 wdog_kern_pat(WD_LASTVAL); 2523 mtx_lock(&sync_mtx); 2524 } 2525 2526 } 2527 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 2528 syncer_final_iter--; 2529 /* 2530 * The variable rushjob allows the kernel to speed up the 2531 * processing of the filesystem syncer process. A rushjob 2532 * value of N tells the filesystem syncer to process the next 2533 * N seconds worth of work on its queue ASAP. Currently rushjob 2534 * is used by the soft update code to speed up the filesystem 2535 * syncer process when the incore state is getting so far 2536 * ahead of the disk that the kernel memory pool is being 2537 * threatened with exhaustion. 2538 */ 2539 if (rushjob > 0) { 2540 rushjob -= 1; 2541 continue; 2542 } 2543 /* 2544 * Just sleep for a short period of time between 2545 * iterations when shutting down to allow some I/O 2546 * to happen. 2547 * 2548 * If it has taken us less than a second to process the 2549 * current work, then wait. Otherwise start right over 2550 * again. We can still lose time if any single round 2551 * takes more than two seconds, but it does not really 2552 * matter as we are just trying to generally pace the 2553 * filesystem activity. 2554 */ 2555 if (syncer_state != SYNCER_RUNNING || 2556 time_uptime == starttime) { 2557 thread_lock(td); 2558 sched_prio(td, PPAUSE); 2559 thread_unlock(td); 2560 } 2561 if (syncer_state != SYNCER_RUNNING) 2562 cv_timedwait(&sync_wakeup, &sync_mtx, 2563 hz / SYNCER_SHUTDOWN_SPEEDUP); 2564 else if (time_uptime == starttime) 2565 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 2566 } 2567 } 2568 2569 /* 2570 * Request the syncer daemon to speed up its work. 2571 * We never push it to speed up more than half of its 2572 * normal turn time, otherwise it could take over the cpu. 2573 */ 2574 int 2575 speedup_syncer(void) 2576 { 2577 int ret = 0; 2578 2579 mtx_lock(&sync_mtx); 2580 if (rushjob < syncdelay / 2) { 2581 rushjob += 1; 2582 stat_rush_requests += 1; 2583 ret = 1; 2584 } 2585 mtx_unlock(&sync_mtx); 2586 cv_broadcast(&sync_wakeup); 2587 return (ret); 2588 } 2589 2590 /* 2591 * Tell the syncer to speed up its work and run though its work 2592 * list several times, then tell it to shut down. 2593 */ 2594 static void 2595 syncer_shutdown(void *arg, int howto) 2596 { 2597 2598 if (howto & RB_NOSYNC) 2599 return; 2600 mtx_lock(&sync_mtx); 2601 syncer_state = SYNCER_SHUTTING_DOWN; 2602 rushjob = 0; 2603 mtx_unlock(&sync_mtx); 2604 cv_broadcast(&sync_wakeup); 2605 kproc_shutdown(arg, howto); 2606 } 2607 2608 void 2609 syncer_suspend(void) 2610 { 2611 2612 syncer_shutdown(updateproc, 0); 2613 } 2614 2615 void 2616 syncer_resume(void) 2617 { 2618 2619 mtx_lock(&sync_mtx); 2620 first_printf = 1; 2621 syncer_state = SYNCER_RUNNING; 2622 mtx_unlock(&sync_mtx); 2623 cv_broadcast(&sync_wakeup); 2624 kproc_resume(updateproc); 2625 } 2626 2627 /* 2628 * Reassign a buffer from one vnode to another. 2629 * Used to assign file specific control information 2630 * (indirect blocks) to the vnode to which they belong. 2631 */ 2632 void 2633 reassignbuf(struct buf *bp) 2634 { 2635 struct vnode *vp; 2636 struct bufobj *bo; 2637 int delay; 2638 #ifdef INVARIANTS 2639 struct bufv *bv; 2640 #endif 2641 2642 vp = bp->b_vp; 2643 bo = bp->b_bufobj; 2644 ++reassignbufcalls; 2645 2646 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 2647 bp, bp->b_vp, bp->b_flags); 2648 /* 2649 * B_PAGING flagged buffers cannot be reassigned because their vp 2650 * is not fully linked in. 2651 */ 2652 if (bp->b_flags & B_PAGING) 2653 panic("cannot reassign paging buffer"); 2654 2655 /* 2656 * Delete from old vnode list, if on one. 2657 */ 2658 BO_LOCK(bo); 2659 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2660 buf_vlist_remove(bp); 2661 else 2662 panic("reassignbuf: Buffer %p not on queue.", bp); 2663 /* 2664 * If dirty, put on list of dirty buffers; otherwise insert onto list 2665 * of clean buffers. 2666 */ 2667 if (bp->b_flags & B_DELWRI) { 2668 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 2669 switch (vp->v_type) { 2670 case VDIR: 2671 delay = dirdelay; 2672 break; 2673 case VCHR: 2674 delay = metadelay; 2675 break; 2676 default: 2677 delay = filedelay; 2678 } 2679 vn_syncer_add_to_worklist(bo, delay); 2680 } 2681 buf_vlist_add(bp, bo, BX_VNDIRTY); 2682 } else { 2683 buf_vlist_add(bp, bo, BX_VNCLEAN); 2684 2685 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2686 mtx_lock(&sync_mtx); 2687 LIST_REMOVE(bo, bo_synclist); 2688 syncer_worklist_len--; 2689 mtx_unlock(&sync_mtx); 2690 bo->bo_flag &= ~BO_ONWORKLST; 2691 } 2692 } 2693 #ifdef INVARIANTS 2694 bv = &bo->bo_clean; 2695 bp = TAILQ_FIRST(&bv->bv_hd); 2696 KASSERT(bp == NULL || bp->b_bufobj == bo, 2697 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2698 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2699 KASSERT(bp == NULL || bp->b_bufobj == bo, 2700 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2701 bv = &bo->bo_dirty; 2702 bp = TAILQ_FIRST(&bv->bv_hd); 2703 KASSERT(bp == NULL || bp->b_bufobj == bo, 2704 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2705 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2706 KASSERT(bp == NULL || bp->b_bufobj == bo, 2707 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2708 #endif 2709 BO_UNLOCK(bo); 2710 } 2711 2712 static void 2713 v_init_counters(struct vnode *vp) 2714 { 2715 2716 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0, 2717 vp, ("%s called for an initialized vnode", __FUNCTION__)); 2718 ASSERT_VI_UNLOCKED(vp, __FUNCTION__); 2719 2720 refcount_init(&vp->v_holdcnt, 1); 2721 refcount_init(&vp->v_usecount, 1); 2722 } 2723 2724 /* 2725 * Increment si_usecount of the associated device, if any. 2726 */ 2727 static void 2728 v_incr_devcount(struct vnode *vp) 2729 { 2730 2731 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2732 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2733 dev_lock(); 2734 vp->v_rdev->si_usecount++; 2735 dev_unlock(); 2736 } 2737 } 2738 2739 /* 2740 * Decrement si_usecount of the associated device, if any. 2741 */ 2742 static void 2743 v_decr_devcount(struct vnode *vp) 2744 { 2745 2746 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2747 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2748 dev_lock(); 2749 vp->v_rdev->si_usecount--; 2750 dev_unlock(); 2751 } 2752 } 2753 2754 /* 2755 * Grab a particular vnode from the free list, increment its 2756 * reference count and lock it. VIRF_DOOMED is set if the vnode 2757 * is being destroyed. Only callers who specify LK_RETRY will 2758 * see doomed vnodes. If inactive processing was delayed in 2759 * vput try to do it here. 2760 * 2761 * Both holdcnt and usecount can be manipulated using atomics without holding 2762 * any locks except in these cases which require the vnode interlock: 2763 * holdcnt: 1->0 and 0->1 2764 * usecount: 0->1 2765 * 2766 * usecount is permitted to transition 1->0 without the interlock because 2767 * vnode is kept live by holdcnt. 2768 */ 2769 static enum vgetstate __always_inline 2770 _vget_prep(struct vnode *vp, bool interlock) 2771 { 2772 enum vgetstate vs; 2773 2774 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2775 vs = VGET_USECOUNT; 2776 } else { 2777 if (interlock) 2778 vholdl(vp); 2779 else 2780 vhold(vp); 2781 vs = VGET_HOLDCNT; 2782 } 2783 return (vs); 2784 } 2785 2786 enum vgetstate 2787 vget_prep(struct vnode *vp) 2788 { 2789 2790 return (_vget_prep(vp, false)); 2791 } 2792 2793 int 2794 vget(struct vnode *vp, int flags, struct thread *td) 2795 { 2796 enum vgetstate vs; 2797 2798 MPASS(td == curthread); 2799 2800 vs = _vget_prep(vp, (flags & LK_INTERLOCK) != 0); 2801 return (vget_finish(vp, flags, vs)); 2802 } 2803 2804 int 2805 vget_finish(struct vnode *vp, int flags, enum vgetstate vs) 2806 { 2807 int error, oweinact; 2808 2809 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2810 ("%s: invalid lock operation", __func__)); 2811 2812 if ((flags & LK_INTERLOCK) != 0) 2813 ASSERT_VI_LOCKED(vp, __func__); 2814 else 2815 ASSERT_VI_UNLOCKED(vp, __func__); 2816 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__)); 2817 if (vs == VGET_USECOUNT) { 2818 VNASSERT(vp->v_usecount > 0, vp, 2819 ("%s: vnode without usecount when VGET_USECOUNT was passed", 2820 __func__)); 2821 } 2822 2823 if ((error = vn_lock(vp, flags)) != 0) { 2824 if (vs == VGET_USECOUNT) 2825 vrele(vp); 2826 else 2827 vdrop(vp); 2828 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2829 vp); 2830 return (error); 2831 } 2832 2833 if (vs == VGET_USECOUNT) { 2834 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2835 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2836 return (0); 2837 } 2838 2839 /* 2840 * We hold the vnode. If the usecount is 0 it will be utilized to keep 2841 * the vnode around. Otherwise someone else lended their hold count and 2842 * we have to drop ours. 2843 */ 2844 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2845 #ifdef INVARIANTS 2846 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1) - 1; 2847 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__)); 2848 #else 2849 refcount_release(&vp->v_holdcnt); 2850 #endif 2851 VNODE_REFCOUNT_FENCE_ACQ(); 2852 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2853 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2854 return (0); 2855 } 2856 2857 /* 2858 * We don't guarantee that any particular close will 2859 * trigger inactive processing so just make a best effort 2860 * here at preventing a reference to a removed file. If 2861 * we don't succeed no harm is done. 2862 * 2863 * Upgrade our holdcnt to a usecount. 2864 */ 2865 VI_LOCK(vp); 2866 /* 2867 * See the previous section. By the time we get here we may find 2868 * ourselves in the same spot. 2869 */ 2870 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2871 #ifdef INVARIANTS 2872 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1) - 1; 2873 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__)); 2874 #else 2875 refcount_release(&vp->v_holdcnt); 2876 #endif 2877 VNODE_REFCOUNT_FENCE_ACQ(); 2878 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2879 ("%s: vnode with usecount and VI_OWEINACT set", 2880 __func__)); 2881 VI_UNLOCK(vp); 2882 return (0); 2883 } 2884 if ((vp->v_iflag & VI_OWEINACT) == 0) { 2885 oweinact = 0; 2886 } else { 2887 oweinact = 1; 2888 vp->v_iflag &= ~VI_OWEINACT; 2889 VNODE_REFCOUNT_FENCE_REL(); 2890 } 2891 v_incr_devcount(vp); 2892 refcount_acquire(&vp->v_usecount); 2893 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE && 2894 (flags & LK_NOWAIT) == 0) 2895 vinactive(vp, curthread); 2896 VI_UNLOCK(vp); 2897 return (0); 2898 } 2899 2900 /* 2901 * Increase the reference (use) and hold count of a vnode. 2902 * This will also remove the vnode from the free list if it is presently free. 2903 */ 2904 void 2905 vref(struct vnode *vp) 2906 { 2907 2908 ASSERT_VI_UNLOCKED(vp, __func__); 2909 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2910 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2911 VNODE_REFCOUNT_FENCE_ACQ(); 2912 VNASSERT(vp->v_holdcnt > 0, vp, 2913 ("%s: active vnode not held", __func__)); 2914 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2915 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2916 return; 2917 } 2918 VI_LOCK(vp); 2919 vrefl(vp); 2920 VI_UNLOCK(vp); 2921 } 2922 2923 void 2924 vrefl(struct vnode *vp) 2925 { 2926 2927 ASSERT_VI_LOCKED(vp, __func__); 2928 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2929 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2930 VNODE_REFCOUNT_FENCE_ACQ(); 2931 VNASSERT(vp->v_holdcnt > 0, vp, 2932 ("%s: active vnode not held", __func__)); 2933 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 2934 ("%s: vnode with usecount and VI_OWEINACT set", __func__)); 2935 return; 2936 } 2937 vholdl(vp); 2938 if ((vp->v_iflag & VI_OWEINACT) != 0) { 2939 vp->v_iflag &= ~VI_OWEINACT; 2940 VNODE_REFCOUNT_FENCE_REL(); 2941 } 2942 v_incr_devcount(vp); 2943 refcount_acquire(&vp->v_usecount); 2944 } 2945 2946 void 2947 vrefact(struct vnode *vp) 2948 { 2949 2950 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2951 #ifdef INVARIANTS 2952 int old = atomic_fetchadd_int(&vp->v_usecount, 1); 2953 VNASSERT(old > 0, vp, ("%s: wrong use count", __func__)); 2954 #else 2955 refcount_acquire(&vp->v_usecount); 2956 #endif 2957 } 2958 2959 /* 2960 * Return reference count of a vnode. 2961 * 2962 * The results of this call are only guaranteed when some mechanism is used to 2963 * stop other processes from gaining references to the vnode. This may be the 2964 * case if the caller holds the only reference. This is also useful when stale 2965 * data is acceptable as race conditions may be accounted for by some other 2966 * means. 2967 */ 2968 int 2969 vrefcnt(struct vnode *vp) 2970 { 2971 2972 return (vp->v_usecount); 2973 } 2974 2975 enum vputx_op { VPUTX_VRELE, VPUTX_VPUT, VPUTX_VUNREF }; 2976 2977 /* 2978 * Decrement the use and hold counts for a vnode. 2979 * 2980 * See an explanation near vget() as to why atomic operation is safe. 2981 */ 2982 static void 2983 vputx(struct vnode *vp, enum vputx_op func) 2984 { 2985 int error; 2986 2987 KASSERT(vp != NULL, ("vputx: null vp")); 2988 if (func == VPUTX_VUNREF) 2989 ASSERT_VOP_LOCKED(vp, "vunref"); 2990 ASSERT_VI_UNLOCKED(vp, __func__); 2991 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp, 2992 ("%s: wrong ref counts", __func__)); 2993 2994 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2995 2996 /* 2997 * We want to hold the vnode until the inactive finishes to 2998 * prevent vgone() races. We drop the use count here and the 2999 * hold count below when we're done. 3000 * 3001 * If we release the last usecount we take ownership of the hold 3002 * count which provides liveness of the vnode, in which case we 3003 * have to vdrop. 3004 */ 3005 if (!refcount_release(&vp->v_usecount)) 3006 return; 3007 VI_LOCK(vp); 3008 v_decr_devcount(vp); 3009 /* 3010 * By the time we got here someone else might have transitioned 3011 * the count back to > 0. 3012 */ 3013 if (vp->v_usecount > 0) { 3014 vdropl(vp); 3015 return; 3016 } 3017 if (vp->v_iflag & VI_DOINGINACT) { 3018 vdropl(vp); 3019 return; 3020 } 3021 3022 /* 3023 * Check if the fs wants to perform inactive processing. Note we 3024 * may be only holding the interlock, in which case it is possible 3025 * someone else called vgone on the vnode and ->v_data is now NULL. 3026 * Since vgone performs inactive on its own there is nothing to do 3027 * here but to drop our hold count. 3028 */ 3029 if (__predict_false(VN_IS_DOOMED(vp)) || 3030 VOP_NEED_INACTIVE(vp) == 0) { 3031 vdropl(vp); 3032 return; 3033 } 3034 3035 /* 3036 * We must call VOP_INACTIVE with the node locked. Mark 3037 * as VI_DOINGINACT to avoid recursion. 3038 */ 3039 vp->v_iflag |= VI_OWEINACT; 3040 switch (func) { 3041 case VPUTX_VRELE: 3042 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 3043 VI_LOCK(vp); 3044 break; 3045 case VPUTX_VPUT: 3046 error = VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT); 3047 VI_LOCK(vp); 3048 break; 3049 case VPUTX_VUNREF: 3050 error = 0; 3051 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 3052 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK); 3053 VI_LOCK(vp); 3054 } 3055 break; 3056 } 3057 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp, 3058 ("vnode with usecount and VI_OWEINACT set")); 3059 if (error == 0) { 3060 if (vp->v_iflag & VI_OWEINACT) 3061 vinactive(vp, curthread); 3062 if (func != VPUTX_VUNREF) 3063 VOP_UNLOCK(vp, 0); 3064 } 3065 vdropl(vp); 3066 } 3067 3068 /* 3069 * Vnode put/release. 3070 * If count drops to zero, call inactive routine and return to freelist. 3071 */ 3072 void 3073 vrele(struct vnode *vp) 3074 { 3075 3076 vputx(vp, VPUTX_VRELE); 3077 } 3078 3079 /* 3080 * Release an already locked vnode. This give the same effects as 3081 * unlock+vrele(), but takes less time and avoids releasing and 3082 * re-aquiring the lock (as vrele() acquires the lock internally.) 3083 * 3084 * It is an invariant that all VOP_* calls operate on a held vnode. 3085 * We may be only having an implicit hold stemming from our usecount, 3086 * which we are about to release. If we unlock the vnode afterwards we 3087 * open a time window where someone else dropped the last usecount and 3088 * proceeded to free the vnode before our unlock finished. For this 3089 * reason we unlock the vnode early. This is a little bit wasteful as 3090 * it may be the vnode is exclusively locked and inactive processing is 3091 * needed, in which case we are adding work. 3092 */ 3093 void 3094 vput(struct vnode *vp) 3095 { 3096 3097 VOP_UNLOCK(vp, 0); 3098 vputx(vp, VPUTX_VPUT); 3099 } 3100 3101 /* 3102 * Release an exclusively locked vnode. Do not unlock the vnode lock. 3103 */ 3104 void 3105 vunref(struct vnode *vp) 3106 { 3107 3108 vputx(vp, VPUTX_VUNREF); 3109 } 3110 3111 /* 3112 * Increase the hold count and activate if this is the first reference. 3113 */ 3114 static void 3115 vhold_activate(struct vnode *vp) 3116 { 3117 struct mount *mp; 3118 3119 ASSERT_VI_LOCKED(vp, __func__); 3120 VNASSERT(vp->v_holdcnt == 0, vp, 3121 ("%s: wrong hold count", __func__)); 3122 VNASSERT(vp->v_op != NULL, vp, 3123 ("%s: vnode already reclaimed.", __func__)); 3124 /* 3125 * Remove a vnode from the free list, mark it as in use, 3126 * and put it on the active list. 3127 */ 3128 VNASSERT(vp->v_mount != NULL, vp, 3129 ("_vhold: vnode not on per mount vnode list")); 3130 mp = vp->v_mount; 3131 mtx_lock(&mp->mnt_listmtx); 3132 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) { 3133 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist); 3134 mp->mnt_tmpfreevnodelistsize--; 3135 vp->v_mflag &= ~VMP_TMPMNTFREELIST; 3136 } else { 3137 mtx_lock(&vnode_free_list_mtx); 3138 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist); 3139 freevnodes--; 3140 mtx_unlock(&vnode_free_list_mtx); 3141 } 3142 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 3143 ("Activating already active vnode")); 3144 vp->v_iflag &= ~VI_FREE; 3145 vp->v_iflag |= VI_ACTIVE; 3146 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist); 3147 mp->mnt_activevnodelistsize++; 3148 mtx_unlock(&mp->mnt_listmtx); 3149 refcount_acquire(&vp->v_holdcnt); 3150 } 3151 3152 void 3153 vhold(struct vnode *vp) 3154 { 3155 3156 ASSERT_VI_UNLOCKED(vp, __func__); 3157 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3158 if (refcount_acquire_if_not_zero(&vp->v_holdcnt)) { 3159 VNODE_REFCOUNT_FENCE_ACQ(); 3160 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 3161 ("vhold: vnode with holdcnt is free")); 3162 return; 3163 } 3164 VI_LOCK(vp); 3165 vholdl(vp); 3166 VI_UNLOCK(vp); 3167 } 3168 3169 void 3170 vholdl(struct vnode *vp) 3171 { 3172 3173 ASSERT_VI_LOCKED(vp, __func__); 3174 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3175 if ((vp->v_iflag & VI_FREE) == 0) { 3176 refcount_acquire(&vp->v_holdcnt); 3177 return; 3178 } 3179 vhold_activate(vp); 3180 } 3181 3182 void 3183 vholdnz(struct vnode *vp) 3184 { 3185 3186 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3187 #ifdef INVARIANTS 3188 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1); 3189 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__)); 3190 #else 3191 atomic_add_int(&vp->v_holdcnt, 1); 3192 #endif 3193 } 3194 3195 /* 3196 * Drop the hold count of the vnode. If this is the last reference to 3197 * the vnode we place it on the free list unless it has been vgone'd 3198 * (marked VIRF_DOOMED) in which case we will free it. 3199 * 3200 * Because the vnode vm object keeps a hold reference on the vnode if 3201 * there is at least one resident non-cached page, the vnode cannot 3202 * leave the active list without the page cleanup done. 3203 */ 3204 static void 3205 vdrop_deactivate(struct vnode *vp) 3206 { 3207 struct mount *mp; 3208 3209 ASSERT_VI_LOCKED(vp, __func__); 3210 /* 3211 * Mark a vnode as free: remove it from its active list 3212 * and put it up for recycling on the freelist. 3213 */ 3214 VNASSERT(!VN_IS_DOOMED(vp), vp, 3215 ("vdrop: returning doomed vnode")); 3216 VNASSERT(vp->v_op != NULL, vp, 3217 ("vdrop: vnode already reclaimed.")); 3218 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 3219 ("vnode already free")); 3220 VNASSERT(vp->v_holdcnt == 0, vp, 3221 ("vdrop: freeing when we shouldn't")); 3222 if ((vp->v_iflag & VI_OWEINACT) == 0) { 3223 mp = vp->v_mount; 3224 if (mp != NULL) { 3225 mtx_lock(&mp->mnt_listmtx); 3226 if (vp->v_iflag & VI_ACTIVE) { 3227 vp->v_iflag &= ~VI_ACTIVE; 3228 TAILQ_REMOVE(&mp->mnt_activevnodelist, 3229 vp, v_actfreelist); 3230 mp->mnt_activevnodelistsize--; 3231 } 3232 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist, 3233 vp, v_actfreelist); 3234 mp->mnt_tmpfreevnodelistsize++; 3235 vp->v_iflag |= VI_FREE; 3236 vp->v_mflag |= VMP_TMPMNTFREELIST; 3237 VI_UNLOCK(vp); 3238 if (mp->mnt_tmpfreevnodelistsize >= 3239 mnt_free_list_batch) 3240 vnlru_return_batch_locked(mp); 3241 mtx_unlock(&mp->mnt_listmtx); 3242 } else { 3243 VNASSERT((vp->v_iflag & VI_ACTIVE) == 0, vp, 3244 ("vdrop: active vnode not on per mount vnode list")); 3245 mtx_lock(&vnode_free_list_mtx); 3246 TAILQ_INSERT_TAIL(&vnode_free_list, vp, 3247 v_actfreelist); 3248 freevnodes++; 3249 vp->v_iflag |= VI_FREE; 3250 VI_UNLOCK(vp); 3251 mtx_unlock(&vnode_free_list_mtx); 3252 } 3253 } else { 3254 VI_UNLOCK(vp); 3255 counter_u64_add(free_owe_inact, 1); 3256 } 3257 } 3258 3259 void 3260 vdrop(struct vnode *vp) 3261 { 3262 3263 ASSERT_VI_UNLOCKED(vp, __func__); 3264 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3265 if (__predict_false((int)vp->v_holdcnt <= 0)) { 3266 vn_printf(vp, "vdrop: holdcnt %d", vp->v_holdcnt); 3267 panic("vdrop: wrong holdcnt"); 3268 } 3269 if (refcount_release_if_not_last(&vp->v_holdcnt)) 3270 return; 3271 VI_LOCK(vp); 3272 vdropl(vp); 3273 } 3274 3275 void 3276 vdropl(struct vnode *vp) 3277 { 3278 3279 ASSERT_VI_LOCKED(vp, __func__); 3280 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3281 if (__predict_false((int)vp->v_holdcnt <= 0)) { 3282 vn_printf(vp, "vdrop: holdcnt %d", vp->v_holdcnt); 3283 panic("vdrop: wrong holdcnt"); 3284 } 3285 if (!refcount_release(&vp->v_holdcnt)) { 3286 VI_UNLOCK(vp); 3287 return; 3288 } 3289 if (VN_IS_DOOMED(vp)) { 3290 freevnode(vp); 3291 return; 3292 } 3293 vdrop_deactivate(vp); 3294 } 3295 3296 /* 3297 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 3298 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 3299 * OWEINACT tracks whether a vnode missed a call to inactive due to a 3300 * failed lock upgrade. 3301 */ 3302 void 3303 vinactive(struct vnode *vp, struct thread *td) 3304 { 3305 struct vm_object *obj; 3306 3307 ASSERT_VOP_ELOCKED(vp, "vinactive"); 3308 ASSERT_VI_LOCKED(vp, "vinactive"); 3309 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 3310 ("vinactive: recursed on VI_DOINGINACT")); 3311 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3312 vp->v_iflag |= VI_DOINGINACT; 3313 vp->v_iflag &= ~VI_OWEINACT; 3314 VI_UNLOCK(vp); 3315 /* 3316 * Before moving off the active list, we must be sure that any 3317 * modified pages are converted into the vnode's dirty 3318 * buffers, since these will no longer be checked once the 3319 * vnode is on the inactive list. 3320 * 3321 * The write-out of the dirty pages is asynchronous. At the 3322 * point that VOP_INACTIVE() is called, there could still be 3323 * pending I/O and dirty pages in the object. 3324 */ 3325 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 3326 vm_object_mightbedirty(obj)) { 3327 VM_OBJECT_WLOCK(obj); 3328 vm_object_page_clean(obj, 0, 0, 0); 3329 VM_OBJECT_WUNLOCK(obj); 3330 } 3331 VOP_INACTIVE(vp, td); 3332 VI_LOCK(vp); 3333 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 3334 ("vinactive: lost VI_DOINGINACT")); 3335 vp->v_iflag &= ~VI_DOINGINACT; 3336 } 3337 3338 /* 3339 * Remove any vnodes in the vnode table belonging to mount point mp. 3340 * 3341 * If FORCECLOSE is not specified, there should not be any active ones, 3342 * return error if any are found (nb: this is a user error, not a 3343 * system error). If FORCECLOSE is specified, detach any active vnodes 3344 * that are found. 3345 * 3346 * If WRITECLOSE is set, only flush out regular file vnodes open for 3347 * writing. 3348 * 3349 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 3350 * 3351 * `rootrefs' specifies the base reference count for the root vnode 3352 * of this filesystem. The root vnode is considered busy if its 3353 * v_usecount exceeds this value. On a successful return, vflush(, td) 3354 * will call vrele() on the root vnode exactly rootrefs times. 3355 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 3356 * be zero. 3357 */ 3358 #ifdef DIAGNOSTIC 3359 static int busyprt = 0; /* print out busy vnodes */ 3360 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes"); 3361 #endif 3362 3363 int 3364 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td) 3365 { 3366 struct vnode *vp, *mvp, *rootvp = NULL; 3367 struct vattr vattr; 3368 int busy = 0, error; 3369 3370 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 3371 rootrefs, flags); 3372 if (rootrefs > 0) { 3373 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 3374 ("vflush: bad args")); 3375 /* 3376 * Get the filesystem root vnode. We can vput() it 3377 * immediately, since with rootrefs > 0, it won't go away. 3378 */ 3379 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) { 3380 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 3381 __func__, error); 3382 return (error); 3383 } 3384 vput(rootvp); 3385 } 3386 loop: 3387 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 3388 vholdl(vp); 3389 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 3390 if (error) { 3391 vdrop(vp); 3392 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3393 goto loop; 3394 } 3395 /* 3396 * Skip over a vnodes marked VV_SYSTEM. 3397 */ 3398 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 3399 VOP_UNLOCK(vp, 0); 3400 vdrop(vp); 3401 continue; 3402 } 3403 /* 3404 * If WRITECLOSE is set, flush out unlinked but still open 3405 * files (even if open only for reading) and regular file 3406 * vnodes open for writing. 3407 */ 3408 if (flags & WRITECLOSE) { 3409 if (vp->v_object != NULL) { 3410 VM_OBJECT_WLOCK(vp->v_object); 3411 vm_object_page_clean(vp->v_object, 0, 0, 0); 3412 VM_OBJECT_WUNLOCK(vp->v_object); 3413 } 3414 error = VOP_FSYNC(vp, MNT_WAIT, td); 3415 if (error != 0) { 3416 VOP_UNLOCK(vp, 0); 3417 vdrop(vp); 3418 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3419 return (error); 3420 } 3421 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 3422 VI_LOCK(vp); 3423 3424 if ((vp->v_type == VNON || 3425 (error == 0 && vattr.va_nlink > 0)) && 3426 (vp->v_writecount <= 0 || vp->v_type != VREG)) { 3427 VOP_UNLOCK(vp, 0); 3428 vdropl(vp); 3429 continue; 3430 } 3431 } else 3432 VI_LOCK(vp); 3433 /* 3434 * With v_usecount == 0, all we need to do is clear out the 3435 * vnode data structures and we are done. 3436 * 3437 * If FORCECLOSE is set, forcibly close the vnode. 3438 */ 3439 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 3440 vgonel(vp); 3441 } else { 3442 busy++; 3443 #ifdef DIAGNOSTIC 3444 if (busyprt) 3445 vn_printf(vp, "vflush: busy vnode "); 3446 #endif 3447 } 3448 VOP_UNLOCK(vp, 0); 3449 vdropl(vp); 3450 } 3451 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 3452 /* 3453 * If just the root vnode is busy, and if its refcount 3454 * is equal to `rootrefs', then go ahead and kill it. 3455 */ 3456 VI_LOCK(rootvp); 3457 KASSERT(busy > 0, ("vflush: not busy")); 3458 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 3459 ("vflush: usecount %d < rootrefs %d", 3460 rootvp->v_usecount, rootrefs)); 3461 if (busy == 1 && rootvp->v_usecount == rootrefs) { 3462 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 3463 vgone(rootvp); 3464 VOP_UNLOCK(rootvp, 0); 3465 busy = 0; 3466 } else 3467 VI_UNLOCK(rootvp); 3468 } 3469 if (busy) { 3470 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 3471 busy); 3472 return (EBUSY); 3473 } 3474 for (; rootrefs > 0; rootrefs--) 3475 vrele(rootvp); 3476 return (0); 3477 } 3478 3479 /* 3480 * Recycle an unused vnode to the front of the free list. 3481 */ 3482 int 3483 vrecycle(struct vnode *vp) 3484 { 3485 int recycled; 3486 3487 VI_LOCK(vp); 3488 recycled = vrecyclel(vp); 3489 VI_UNLOCK(vp); 3490 return (recycled); 3491 } 3492 3493 /* 3494 * vrecycle, with the vp interlock held. 3495 */ 3496 int 3497 vrecyclel(struct vnode *vp) 3498 { 3499 int recycled; 3500 3501 ASSERT_VOP_ELOCKED(vp, __func__); 3502 ASSERT_VI_LOCKED(vp, __func__); 3503 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3504 recycled = 0; 3505 if (vp->v_usecount == 0) { 3506 recycled = 1; 3507 vgonel(vp); 3508 } 3509 return (recycled); 3510 } 3511 3512 /* 3513 * Eliminate all activity associated with a vnode 3514 * in preparation for reuse. 3515 */ 3516 void 3517 vgone(struct vnode *vp) 3518 { 3519 VI_LOCK(vp); 3520 vgonel(vp); 3521 VI_UNLOCK(vp); 3522 } 3523 3524 static void 3525 notify_lowervp_vfs_dummy(struct mount *mp __unused, 3526 struct vnode *lowervp __unused) 3527 { 3528 } 3529 3530 /* 3531 * Notify upper mounts about reclaimed or unlinked vnode. 3532 */ 3533 void 3534 vfs_notify_upper(struct vnode *vp, int event) 3535 { 3536 static struct vfsops vgonel_vfsops = { 3537 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy, 3538 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy, 3539 }; 3540 struct mount *mp, *ump, *mmp; 3541 3542 mp = vp->v_mount; 3543 if (mp == NULL) 3544 return; 3545 if (TAILQ_EMPTY(&mp->mnt_uppers)) 3546 return; 3547 3548 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO); 3549 mmp->mnt_op = &vgonel_vfsops; 3550 mmp->mnt_kern_flag |= MNTK_MARKER; 3551 MNT_ILOCK(mp); 3552 mp->mnt_kern_flag |= MNTK_VGONE_UPPER; 3553 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) { 3554 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) { 3555 ump = TAILQ_NEXT(ump, mnt_upper_link); 3556 continue; 3557 } 3558 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link); 3559 MNT_IUNLOCK(mp); 3560 switch (event) { 3561 case VFS_NOTIFY_UPPER_RECLAIM: 3562 VFS_RECLAIM_LOWERVP(ump, vp); 3563 break; 3564 case VFS_NOTIFY_UPPER_UNLINK: 3565 VFS_UNLINK_LOWERVP(ump, vp); 3566 break; 3567 default: 3568 KASSERT(0, ("invalid event %d", event)); 3569 break; 3570 } 3571 MNT_ILOCK(mp); 3572 ump = TAILQ_NEXT(mmp, mnt_upper_link); 3573 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link); 3574 } 3575 free(mmp, M_TEMP); 3576 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER; 3577 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) { 3578 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER; 3579 wakeup(&mp->mnt_uppers); 3580 } 3581 MNT_IUNLOCK(mp); 3582 } 3583 3584 /* 3585 * vgone, with the vp interlock held. 3586 */ 3587 static void 3588 vgonel(struct vnode *vp) 3589 { 3590 struct thread *td; 3591 struct mount *mp; 3592 vm_object_t object; 3593 bool active, oweinact; 3594 3595 ASSERT_VOP_ELOCKED(vp, "vgonel"); 3596 ASSERT_VI_LOCKED(vp, "vgonel"); 3597 VNASSERT(vp->v_holdcnt, vp, 3598 ("vgonel: vp %p has no reference.", vp)); 3599 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3600 td = curthread; 3601 3602 /* 3603 * Don't vgonel if we're already doomed. 3604 */ 3605 if (vp->v_irflag & VIRF_DOOMED) 3606 return; 3607 vp->v_irflag |= VIRF_DOOMED; 3608 3609 /* 3610 * Check to see if the vnode is in use. If so, we have to call 3611 * VOP_CLOSE() and VOP_INACTIVE(). 3612 */ 3613 active = vp->v_usecount > 0; 3614 oweinact = (vp->v_iflag & VI_OWEINACT) != 0; 3615 VI_UNLOCK(vp); 3616 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM); 3617 3618 /* 3619 * If purging an active vnode, it must be closed and 3620 * deactivated before being reclaimed. 3621 */ 3622 if (active) 3623 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 3624 if (oweinact || active) { 3625 VI_LOCK(vp); 3626 if ((vp->v_iflag & VI_DOINGINACT) == 0) 3627 vinactive(vp, td); 3628 VI_UNLOCK(vp); 3629 } 3630 if (vp->v_type == VSOCK) 3631 vfs_unp_reclaim(vp); 3632 3633 /* 3634 * Clean out any buffers associated with the vnode. 3635 * If the flush fails, just toss the buffers. 3636 */ 3637 mp = NULL; 3638 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 3639 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 3640 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) { 3641 while (vinvalbuf(vp, 0, 0, 0) != 0) 3642 ; 3643 } 3644 3645 BO_LOCK(&vp->v_bufobj); 3646 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) && 3647 vp->v_bufobj.bo_dirty.bv_cnt == 0 && 3648 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) && 3649 vp->v_bufobj.bo_clean.bv_cnt == 0, 3650 ("vp %p bufobj not invalidated", vp)); 3651 3652 /* 3653 * For VMIO bufobj, BO_DEAD is set later, or in 3654 * vm_object_terminate() after the object's page queue is 3655 * flushed. 3656 */ 3657 object = vp->v_bufobj.bo_object; 3658 if (object == NULL) 3659 vp->v_bufobj.bo_flag |= BO_DEAD; 3660 BO_UNLOCK(&vp->v_bufobj); 3661 3662 /* 3663 * Handle the VM part. Tmpfs handles v_object on its own (the 3664 * OBJT_VNODE check). Nullfs or other bypassing filesystems 3665 * should not touch the object borrowed from the lower vnode 3666 * (the handle check). 3667 */ 3668 if (object != NULL && object->type == OBJT_VNODE && 3669 object->handle == vp) 3670 vnode_destroy_vobject(vp); 3671 3672 /* 3673 * Reclaim the vnode. 3674 */ 3675 if (VOP_RECLAIM(vp, td)) 3676 panic("vgone: cannot reclaim"); 3677 if (mp != NULL) 3678 vn_finished_secondary_write(mp); 3679 VNASSERT(vp->v_object == NULL, vp, 3680 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag)); 3681 /* 3682 * Clear the advisory locks and wake up waiting threads. 3683 */ 3684 (void)VOP_ADVLOCKPURGE(vp); 3685 vp->v_lockf = NULL; 3686 /* 3687 * Delete from old mount point vnode list. 3688 */ 3689 delmntque(vp); 3690 cache_purge(vp); 3691 /* 3692 * Done with purge, reset to the standard lock and invalidate 3693 * the vnode. 3694 */ 3695 VI_LOCK(vp); 3696 vp->v_vnlock = &vp->v_lock; 3697 vp->v_op = &dead_vnodeops; 3698 vp->v_tag = "none"; 3699 vp->v_type = VBAD; 3700 } 3701 3702 /* 3703 * Calculate the total number of references to a special device. 3704 */ 3705 int 3706 vcount(struct vnode *vp) 3707 { 3708 int count; 3709 3710 dev_lock(); 3711 count = vp->v_rdev->si_usecount; 3712 dev_unlock(); 3713 return (count); 3714 } 3715 3716 /* 3717 * Print out a description of a vnode. 3718 */ 3719 static char *typename[] = 3720 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 3721 "VMARKER"}; 3722 3723 void 3724 vn_printf(struct vnode *vp, const char *fmt, ...) 3725 { 3726 va_list ap; 3727 char buf[256], buf2[16]; 3728 u_long flags; 3729 3730 va_start(ap, fmt); 3731 vprintf(fmt, ap); 3732 va_end(ap); 3733 printf("%p: ", (void *)vp); 3734 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]); 3735 printf(" usecount %d, writecount %d, refcount %d", 3736 vp->v_usecount, vp->v_writecount, vp->v_holdcnt); 3737 switch (vp->v_type) { 3738 case VDIR: 3739 printf(" mountedhere %p\n", vp->v_mountedhere); 3740 break; 3741 case VCHR: 3742 printf(" rdev %p\n", vp->v_rdev); 3743 break; 3744 case VSOCK: 3745 printf(" socket %p\n", vp->v_unpcb); 3746 break; 3747 case VFIFO: 3748 printf(" fifoinfo %p\n", vp->v_fifoinfo); 3749 break; 3750 default: 3751 printf("\n"); 3752 break; 3753 } 3754 buf[0] = '\0'; 3755 buf[1] = '\0'; 3756 if (vp->v_irflag & VIRF_DOOMED) 3757 strlcat(buf, "|VIRF_DOOMED", sizeof(buf)); 3758 flags = vp->v_irflag & ~(VIRF_DOOMED); 3759 if (flags != 0) { 3760 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags); 3761 strlcat(buf, buf2, sizeof(buf)); 3762 } 3763 if (vp->v_vflag & VV_ROOT) 3764 strlcat(buf, "|VV_ROOT", sizeof(buf)); 3765 if (vp->v_vflag & VV_ISTTY) 3766 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 3767 if (vp->v_vflag & VV_NOSYNC) 3768 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 3769 if (vp->v_vflag & VV_ETERNALDEV) 3770 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf)); 3771 if (vp->v_vflag & VV_CACHEDLABEL) 3772 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 3773 if (vp->v_vflag & VV_VMSIZEVNLOCK) 3774 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf)); 3775 if (vp->v_vflag & VV_COPYONWRITE) 3776 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 3777 if (vp->v_vflag & VV_SYSTEM) 3778 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 3779 if (vp->v_vflag & VV_PROCDEP) 3780 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 3781 if (vp->v_vflag & VV_NOKNOTE) 3782 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 3783 if (vp->v_vflag & VV_DELETED) 3784 strlcat(buf, "|VV_DELETED", sizeof(buf)); 3785 if (vp->v_vflag & VV_MD) 3786 strlcat(buf, "|VV_MD", sizeof(buf)); 3787 if (vp->v_vflag & VV_FORCEINSMQ) 3788 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf)); 3789 if (vp->v_vflag & VV_READLINK) 3790 strlcat(buf, "|VV_READLINK", sizeof(buf)); 3791 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV | 3792 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP | 3793 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ); 3794 if (flags != 0) { 3795 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 3796 strlcat(buf, buf2, sizeof(buf)); 3797 } 3798 if (vp->v_iflag & VI_TEXT_REF) 3799 strlcat(buf, "|VI_TEXT_REF", sizeof(buf)); 3800 if (vp->v_iflag & VI_MOUNT) 3801 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 3802 if (vp->v_iflag & VI_FREE) 3803 strlcat(buf, "|VI_FREE", sizeof(buf)); 3804 if (vp->v_iflag & VI_ACTIVE) 3805 strlcat(buf, "|VI_ACTIVE", sizeof(buf)); 3806 if (vp->v_iflag & VI_DOINGINACT) 3807 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 3808 if (vp->v_iflag & VI_OWEINACT) 3809 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 3810 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_FREE | VI_ACTIVE | 3811 VI_DOINGINACT | VI_OWEINACT); 3812 if (flags != 0) { 3813 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 3814 strlcat(buf, buf2, sizeof(buf)); 3815 } 3816 if (vp->v_mflag & VMP_TMPMNTFREELIST) 3817 strlcat(buf, "|VMP_TMPMNTFREELIST", sizeof(buf)); 3818 flags = vp->v_mflag & ~(VMP_TMPMNTFREELIST); 3819 if (flags != 0) { 3820 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags); 3821 strlcat(buf, buf2, sizeof(buf)); 3822 } 3823 printf(" flags (%s)\n", buf + 1); 3824 if (mtx_owned(VI_MTX(vp))) 3825 printf(" VI_LOCKed"); 3826 if (vp->v_object != NULL) 3827 printf(" v_object %p ref %d pages %d " 3828 "cleanbuf %d dirtybuf %d\n", 3829 vp->v_object, vp->v_object->ref_count, 3830 vp->v_object->resident_page_count, 3831 vp->v_bufobj.bo_clean.bv_cnt, 3832 vp->v_bufobj.bo_dirty.bv_cnt); 3833 printf(" "); 3834 lockmgr_printinfo(vp->v_vnlock); 3835 if (vp->v_data != NULL) 3836 VOP_PRINT(vp); 3837 } 3838 3839 #ifdef DDB 3840 /* 3841 * List all of the locked vnodes in the system. 3842 * Called when debugging the kernel. 3843 */ 3844 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 3845 { 3846 struct mount *mp; 3847 struct vnode *vp; 3848 3849 /* 3850 * Note: because this is DDB, we can't obey the locking semantics 3851 * for these structures, which means we could catch an inconsistent 3852 * state and dereference a nasty pointer. Not much to be done 3853 * about that. 3854 */ 3855 db_printf("Locked vnodes\n"); 3856 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3857 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3858 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp)) 3859 vn_printf(vp, "vnode "); 3860 } 3861 } 3862 } 3863 3864 /* 3865 * Show details about the given vnode. 3866 */ 3867 DB_SHOW_COMMAND(vnode, db_show_vnode) 3868 { 3869 struct vnode *vp; 3870 3871 if (!have_addr) 3872 return; 3873 vp = (struct vnode *)addr; 3874 vn_printf(vp, "vnode "); 3875 } 3876 3877 /* 3878 * Show details about the given mount point. 3879 */ 3880 DB_SHOW_COMMAND(mount, db_show_mount) 3881 { 3882 struct mount *mp; 3883 struct vfsopt *opt; 3884 struct statfs *sp; 3885 struct vnode *vp; 3886 char buf[512]; 3887 uint64_t mflags; 3888 u_int flags; 3889 3890 if (!have_addr) { 3891 /* No address given, print short info about all mount points. */ 3892 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3893 db_printf("%p %s on %s (%s)\n", mp, 3894 mp->mnt_stat.f_mntfromname, 3895 mp->mnt_stat.f_mntonname, 3896 mp->mnt_stat.f_fstypename); 3897 if (db_pager_quit) 3898 break; 3899 } 3900 db_printf("\nMore info: show mount <addr>\n"); 3901 return; 3902 } 3903 3904 mp = (struct mount *)addr; 3905 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 3906 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 3907 3908 buf[0] = '\0'; 3909 mflags = mp->mnt_flag; 3910 #define MNT_FLAG(flag) do { \ 3911 if (mflags & (flag)) { \ 3912 if (buf[0] != '\0') \ 3913 strlcat(buf, ", ", sizeof(buf)); \ 3914 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 3915 mflags &= ~(flag); \ 3916 } \ 3917 } while (0) 3918 MNT_FLAG(MNT_RDONLY); 3919 MNT_FLAG(MNT_SYNCHRONOUS); 3920 MNT_FLAG(MNT_NOEXEC); 3921 MNT_FLAG(MNT_NOSUID); 3922 MNT_FLAG(MNT_NFS4ACLS); 3923 MNT_FLAG(MNT_UNION); 3924 MNT_FLAG(MNT_ASYNC); 3925 MNT_FLAG(MNT_SUIDDIR); 3926 MNT_FLAG(MNT_SOFTDEP); 3927 MNT_FLAG(MNT_NOSYMFOLLOW); 3928 MNT_FLAG(MNT_GJOURNAL); 3929 MNT_FLAG(MNT_MULTILABEL); 3930 MNT_FLAG(MNT_ACLS); 3931 MNT_FLAG(MNT_NOATIME); 3932 MNT_FLAG(MNT_NOCLUSTERR); 3933 MNT_FLAG(MNT_NOCLUSTERW); 3934 MNT_FLAG(MNT_SUJ); 3935 MNT_FLAG(MNT_EXRDONLY); 3936 MNT_FLAG(MNT_EXPORTED); 3937 MNT_FLAG(MNT_DEFEXPORTED); 3938 MNT_FLAG(MNT_EXPORTANON); 3939 MNT_FLAG(MNT_EXKERB); 3940 MNT_FLAG(MNT_EXPUBLIC); 3941 MNT_FLAG(MNT_LOCAL); 3942 MNT_FLAG(MNT_QUOTA); 3943 MNT_FLAG(MNT_ROOTFS); 3944 MNT_FLAG(MNT_USER); 3945 MNT_FLAG(MNT_IGNORE); 3946 MNT_FLAG(MNT_UPDATE); 3947 MNT_FLAG(MNT_DELEXPORT); 3948 MNT_FLAG(MNT_RELOAD); 3949 MNT_FLAG(MNT_FORCE); 3950 MNT_FLAG(MNT_SNAPSHOT); 3951 MNT_FLAG(MNT_BYFSID); 3952 #undef MNT_FLAG 3953 if (mflags != 0) { 3954 if (buf[0] != '\0') 3955 strlcat(buf, ", ", sizeof(buf)); 3956 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3957 "0x%016jx", mflags); 3958 } 3959 db_printf(" mnt_flag = %s\n", buf); 3960 3961 buf[0] = '\0'; 3962 flags = mp->mnt_kern_flag; 3963 #define MNT_KERN_FLAG(flag) do { \ 3964 if (flags & (flag)) { \ 3965 if (buf[0] != '\0') \ 3966 strlcat(buf, ", ", sizeof(buf)); \ 3967 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 3968 flags &= ~(flag); \ 3969 } \ 3970 } while (0) 3971 MNT_KERN_FLAG(MNTK_UNMOUNTF); 3972 MNT_KERN_FLAG(MNTK_ASYNC); 3973 MNT_KERN_FLAG(MNTK_SOFTDEP); 3974 MNT_KERN_FLAG(MNTK_DRAINING); 3975 MNT_KERN_FLAG(MNTK_REFEXPIRE); 3976 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED); 3977 MNT_KERN_FLAG(MNTK_SHARED_WRITES); 3978 MNT_KERN_FLAG(MNTK_NO_IOPF); 3979 MNT_KERN_FLAG(MNTK_VGONE_UPPER); 3980 MNT_KERN_FLAG(MNTK_VGONE_WAITER); 3981 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT); 3982 MNT_KERN_FLAG(MNTK_MARKER); 3983 MNT_KERN_FLAG(MNTK_USES_BCACHE); 3984 MNT_KERN_FLAG(MNTK_NOASYNC); 3985 MNT_KERN_FLAG(MNTK_UNMOUNT); 3986 MNT_KERN_FLAG(MNTK_MWAIT); 3987 MNT_KERN_FLAG(MNTK_SUSPEND); 3988 MNT_KERN_FLAG(MNTK_SUSPEND2); 3989 MNT_KERN_FLAG(MNTK_SUSPENDED); 3990 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 3991 MNT_KERN_FLAG(MNTK_NOKNOTE); 3992 #undef MNT_KERN_FLAG 3993 if (flags != 0) { 3994 if (buf[0] != '\0') 3995 strlcat(buf, ", ", sizeof(buf)); 3996 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3997 "0x%08x", flags); 3998 } 3999 db_printf(" mnt_kern_flag = %s\n", buf); 4000 4001 db_printf(" mnt_opt = "); 4002 opt = TAILQ_FIRST(mp->mnt_opt); 4003 if (opt != NULL) { 4004 db_printf("%s", opt->name); 4005 opt = TAILQ_NEXT(opt, link); 4006 while (opt != NULL) { 4007 db_printf(", %s", opt->name); 4008 opt = TAILQ_NEXT(opt, link); 4009 } 4010 } 4011 db_printf("\n"); 4012 4013 sp = &mp->mnt_stat; 4014 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 4015 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 4016 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 4017 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 4018 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 4019 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 4020 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 4021 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 4022 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 4023 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 4024 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 4025 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 4026 4027 db_printf(" mnt_cred = { uid=%u ruid=%u", 4028 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 4029 if (jailed(mp->mnt_cred)) 4030 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 4031 db_printf(" }\n"); 4032 db_printf(" mnt_ref = %d (with %d in the struct)\n", 4033 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref); 4034 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 4035 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 4036 db_printf(" mnt_activevnodelistsize = %d\n", 4037 mp->mnt_activevnodelistsize); 4038 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n", 4039 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount); 4040 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 4041 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 4042 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 4043 db_printf(" mnt_lockref = %d (with %d in the struct)\n", 4044 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref); 4045 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 4046 db_printf(" mnt_secondary_accwrites = %d\n", 4047 mp->mnt_secondary_accwrites); 4048 db_printf(" mnt_gjprovider = %s\n", 4049 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 4050 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops); 4051 4052 db_printf("\n\nList of active vnodes\n"); 4053 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) { 4054 if (vp->v_type != VMARKER) { 4055 vn_printf(vp, "vnode "); 4056 if (db_pager_quit) 4057 break; 4058 } 4059 } 4060 db_printf("\n\nList of inactive vnodes\n"); 4061 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4062 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) { 4063 vn_printf(vp, "vnode "); 4064 if (db_pager_quit) 4065 break; 4066 } 4067 } 4068 } 4069 #endif /* DDB */ 4070 4071 /* 4072 * Fill in a struct xvfsconf based on a struct vfsconf. 4073 */ 4074 static int 4075 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp) 4076 { 4077 struct xvfsconf xvfsp; 4078 4079 bzero(&xvfsp, sizeof(xvfsp)); 4080 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4081 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4082 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4083 xvfsp.vfc_flags = vfsp->vfc_flags; 4084 /* 4085 * These are unused in userland, we keep them 4086 * to not break binary compatibility. 4087 */ 4088 xvfsp.vfc_vfsops = NULL; 4089 xvfsp.vfc_next = NULL; 4090 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4091 } 4092 4093 #ifdef COMPAT_FREEBSD32 4094 struct xvfsconf32 { 4095 uint32_t vfc_vfsops; 4096 char vfc_name[MFSNAMELEN]; 4097 int32_t vfc_typenum; 4098 int32_t vfc_refcount; 4099 int32_t vfc_flags; 4100 uint32_t vfc_next; 4101 }; 4102 4103 static int 4104 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp) 4105 { 4106 struct xvfsconf32 xvfsp; 4107 4108 bzero(&xvfsp, sizeof(xvfsp)); 4109 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4110 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4111 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4112 xvfsp.vfc_flags = vfsp->vfc_flags; 4113 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4114 } 4115 #endif 4116 4117 /* 4118 * Top level filesystem related information gathering. 4119 */ 4120 static int 4121 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 4122 { 4123 struct vfsconf *vfsp; 4124 int error; 4125 4126 error = 0; 4127 vfsconf_slock(); 4128 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4129 #ifdef COMPAT_FREEBSD32 4130 if (req->flags & SCTL_MASK32) 4131 error = vfsconf2x32(req, vfsp); 4132 else 4133 #endif 4134 error = vfsconf2x(req, vfsp); 4135 if (error) 4136 break; 4137 } 4138 vfsconf_sunlock(); 4139 return (error); 4140 } 4141 4142 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD | 4143 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist, 4144 "S,xvfsconf", "List of all configured filesystems"); 4145 4146 #ifndef BURN_BRIDGES 4147 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 4148 4149 static int 4150 vfs_sysctl(SYSCTL_HANDLER_ARGS) 4151 { 4152 int *name = (int *)arg1 - 1; /* XXX */ 4153 u_int namelen = arg2 + 1; /* XXX */ 4154 struct vfsconf *vfsp; 4155 4156 log(LOG_WARNING, "userland calling deprecated sysctl, " 4157 "please rebuild world\n"); 4158 4159 #if 1 || defined(COMPAT_PRELITE2) 4160 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 4161 if (namelen == 1) 4162 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 4163 #endif 4164 4165 switch (name[1]) { 4166 case VFS_MAXTYPENUM: 4167 if (namelen != 2) 4168 return (ENOTDIR); 4169 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 4170 case VFS_CONF: 4171 if (namelen != 3) 4172 return (ENOTDIR); /* overloaded */ 4173 vfsconf_slock(); 4174 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4175 if (vfsp->vfc_typenum == name[2]) 4176 break; 4177 } 4178 vfsconf_sunlock(); 4179 if (vfsp == NULL) 4180 return (EOPNOTSUPP); 4181 #ifdef COMPAT_FREEBSD32 4182 if (req->flags & SCTL_MASK32) 4183 return (vfsconf2x32(req, vfsp)); 4184 else 4185 #endif 4186 return (vfsconf2x(req, vfsp)); 4187 } 4188 return (EOPNOTSUPP); 4189 } 4190 4191 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP | 4192 CTLFLAG_MPSAFE, vfs_sysctl, 4193 "Generic filesystem"); 4194 4195 #if 1 || defined(COMPAT_PRELITE2) 4196 4197 static int 4198 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 4199 { 4200 int error; 4201 struct vfsconf *vfsp; 4202 struct ovfsconf ovfs; 4203 4204 vfsconf_slock(); 4205 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4206 bzero(&ovfs, sizeof(ovfs)); 4207 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 4208 strcpy(ovfs.vfc_name, vfsp->vfc_name); 4209 ovfs.vfc_index = vfsp->vfc_typenum; 4210 ovfs.vfc_refcount = vfsp->vfc_refcount; 4211 ovfs.vfc_flags = vfsp->vfc_flags; 4212 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 4213 if (error != 0) { 4214 vfsconf_sunlock(); 4215 return (error); 4216 } 4217 } 4218 vfsconf_sunlock(); 4219 return (0); 4220 } 4221 4222 #endif /* 1 || COMPAT_PRELITE2 */ 4223 #endif /* !BURN_BRIDGES */ 4224 4225 #define KINFO_VNODESLOP 10 4226 #ifdef notyet 4227 /* 4228 * Dump vnode list (via sysctl). 4229 */ 4230 /* ARGSUSED */ 4231 static int 4232 sysctl_vnode(SYSCTL_HANDLER_ARGS) 4233 { 4234 struct xvnode *xvn; 4235 struct mount *mp; 4236 struct vnode *vp; 4237 int error, len, n; 4238 4239 /* 4240 * Stale numvnodes access is not fatal here. 4241 */ 4242 req->lock = 0; 4243 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 4244 if (!req->oldptr) 4245 /* Make an estimate */ 4246 return (SYSCTL_OUT(req, 0, len)); 4247 4248 error = sysctl_wire_old_buffer(req, 0); 4249 if (error != 0) 4250 return (error); 4251 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 4252 n = 0; 4253 mtx_lock(&mountlist_mtx); 4254 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4255 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 4256 continue; 4257 MNT_ILOCK(mp); 4258 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4259 if (n == len) 4260 break; 4261 vref(vp); 4262 xvn[n].xv_size = sizeof *xvn; 4263 xvn[n].xv_vnode = vp; 4264 xvn[n].xv_id = 0; /* XXX compat */ 4265 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 4266 XV_COPY(usecount); 4267 XV_COPY(writecount); 4268 XV_COPY(holdcnt); 4269 XV_COPY(mount); 4270 XV_COPY(numoutput); 4271 XV_COPY(type); 4272 #undef XV_COPY 4273 xvn[n].xv_flag = vp->v_vflag; 4274 4275 switch (vp->v_type) { 4276 case VREG: 4277 case VDIR: 4278 case VLNK: 4279 break; 4280 case VBLK: 4281 case VCHR: 4282 if (vp->v_rdev == NULL) { 4283 vrele(vp); 4284 continue; 4285 } 4286 xvn[n].xv_dev = dev2udev(vp->v_rdev); 4287 break; 4288 case VSOCK: 4289 xvn[n].xv_socket = vp->v_socket; 4290 break; 4291 case VFIFO: 4292 xvn[n].xv_fifo = vp->v_fifoinfo; 4293 break; 4294 case VNON: 4295 case VBAD: 4296 default: 4297 /* shouldn't happen? */ 4298 vrele(vp); 4299 continue; 4300 } 4301 vrele(vp); 4302 ++n; 4303 } 4304 MNT_IUNLOCK(mp); 4305 mtx_lock(&mountlist_mtx); 4306 vfs_unbusy(mp); 4307 if (n == len) 4308 break; 4309 } 4310 mtx_unlock(&mountlist_mtx); 4311 4312 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 4313 free(xvn, M_TEMP); 4314 return (error); 4315 } 4316 4317 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD | 4318 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode", 4319 ""); 4320 #endif 4321 4322 static void 4323 unmount_or_warn(struct mount *mp) 4324 { 4325 int error; 4326 4327 error = dounmount(mp, MNT_FORCE, curthread); 4328 if (error != 0) { 4329 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname); 4330 if (error == EBUSY) 4331 printf("BUSY)\n"); 4332 else 4333 printf("%d)\n", error); 4334 } 4335 } 4336 4337 /* 4338 * Unmount all filesystems. The list is traversed in reverse order 4339 * of mounting to avoid dependencies. 4340 */ 4341 void 4342 vfs_unmountall(void) 4343 { 4344 struct mount *mp, *tmp; 4345 4346 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 4347 4348 /* 4349 * Since this only runs when rebooting, it is not interlocked. 4350 */ 4351 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) { 4352 vfs_ref(mp); 4353 4354 /* 4355 * Forcibly unmounting "/dev" before "/" would prevent clean 4356 * unmount of the latter. 4357 */ 4358 if (mp == rootdevmp) 4359 continue; 4360 4361 unmount_or_warn(mp); 4362 } 4363 4364 if (rootdevmp != NULL) 4365 unmount_or_warn(rootdevmp); 4366 } 4367 4368 /* 4369 * perform msync on all vnodes under a mount point 4370 * the mount point must be locked. 4371 */ 4372 void 4373 vfs_msync(struct mount *mp, int flags) 4374 { 4375 struct vnode *vp, *mvp; 4376 struct vm_object *obj; 4377 4378 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 4379 4380 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0) 4381 return; 4382 4383 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) { 4384 obj = vp->v_object; 4385 if (obj != NULL && vm_object_mightbedirty(obj) && 4386 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) { 4387 if (!vget(vp, 4388 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 4389 curthread)) { 4390 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 4391 vput(vp); 4392 continue; 4393 } 4394 4395 obj = vp->v_object; 4396 if (obj != NULL) { 4397 VM_OBJECT_WLOCK(obj); 4398 vm_object_page_clean(obj, 0, 0, 4399 flags == MNT_WAIT ? 4400 OBJPC_SYNC : OBJPC_NOSYNC); 4401 VM_OBJECT_WUNLOCK(obj); 4402 } 4403 vput(vp); 4404 } 4405 } else 4406 VI_UNLOCK(vp); 4407 } 4408 } 4409 4410 static void 4411 destroy_vpollinfo_free(struct vpollinfo *vi) 4412 { 4413 4414 knlist_destroy(&vi->vpi_selinfo.si_note); 4415 mtx_destroy(&vi->vpi_lock); 4416 uma_zfree(vnodepoll_zone, vi); 4417 } 4418 4419 static void 4420 destroy_vpollinfo(struct vpollinfo *vi) 4421 { 4422 4423 knlist_clear(&vi->vpi_selinfo.si_note, 1); 4424 seldrain(&vi->vpi_selinfo); 4425 destroy_vpollinfo_free(vi); 4426 } 4427 4428 /* 4429 * Initialize per-vnode helper structure to hold poll-related state. 4430 */ 4431 void 4432 v_addpollinfo(struct vnode *vp) 4433 { 4434 struct vpollinfo *vi; 4435 4436 if (vp->v_pollinfo != NULL) 4437 return; 4438 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO); 4439 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 4440 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 4441 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked); 4442 VI_LOCK(vp); 4443 if (vp->v_pollinfo != NULL) { 4444 VI_UNLOCK(vp); 4445 destroy_vpollinfo_free(vi); 4446 return; 4447 } 4448 vp->v_pollinfo = vi; 4449 VI_UNLOCK(vp); 4450 } 4451 4452 /* 4453 * Record a process's interest in events which might happen to 4454 * a vnode. Because poll uses the historic select-style interface 4455 * internally, this routine serves as both the ``check for any 4456 * pending events'' and the ``record my interest in future events'' 4457 * functions. (These are done together, while the lock is held, 4458 * to avoid race conditions.) 4459 */ 4460 int 4461 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 4462 { 4463 4464 v_addpollinfo(vp); 4465 mtx_lock(&vp->v_pollinfo->vpi_lock); 4466 if (vp->v_pollinfo->vpi_revents & events) { 4467 /* 4468 * This leaves events we are not interested 4469 * in available for the other process which 4470 * which presumably had requested them 4471 * (otherwise they would never have been 4472 * recorded). 4473 */ 4474 events &= vp->v_pollinfo->vpi_revents; 4475 vp->v_pollinfo->vpi_revents &= ~events; 4476 4477 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4478 return (events); 4479 } 4480 vp->v_pollinfo->vpi_events |= events; 4481 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 4482 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4483 return (0); 4484 } 4485 4486 /* 4487 * Routine to create and manage a filesystem syncer vnode. 4488 */ 4489 #define sync_close ((int (*)(struct vop_close_args *))nullop) 4490 static int sync_fsync(struct vop_fsync_args *); 4491 static int sync_inactive(struct vop_inactive_args *); 4492 static int sync_reclaim(struct vop_reclaim_args *); 4493 4494 static struct vop_vector sync_vnodeops = { 4495 .vop_bypass = VOP_EOPNOTSUPP, 4496 .vop_close = sync_close, /* close */ 4497 .vop_fsync = sync_fsync, /* fsync */ 4498 .vop_inactive = sync_inactive, /* inactive */ 4499 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */ 4500 .vop_reclaim = sync_reclaim, /* reclaim */ 4501 .vop_lock1 = vop_stdlock, /* lock */ 4502 .vop_unlock = vop_stdunlock, /* unlock */ 4503 .vop_islocked = vop_stdislocked, /* islocked */ 4504 }; 4505 4506 /* 4507 * Create a new filesystem syncer vnode for the specified mount point. 4508 */ 4509 void 4510 vfs_allocate_syncvnode(struct mount *mp) 4511 { 4512 struct vnode *vp; 4513 struct bufobj *bo; 4514 static long start, incr, next; 4515 int error; 4516 4517 /* Allocate a new vnode */ 4518 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp); 4519 if (error != 0) 4520 panic("vfs_allocate_syncvnode: getnewvnode() failed"); 4521 vp->v_type = VNON; 4522 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4523 vp->v_vflag |= VV_FORCEINSMQ; 4524 error = insmntque(vp, mp); 4525 if (error != 0) 4526 panic("vfs_allocate_syncvnode: insmntque() failed"); 4527 vp->v_vflag &= ~VV_FORCEINSMQ; 4528 VOP_UNLOCK(vp, 0); 4529 /* 4530 * Place the vnode onto the syncer worklist. We attempt to 4531 * scatter them about on the list so that they will go off 4532 * at evenly distributed times even if all the filesystems 4533 * are mounted at once. 4534 */ 4535 next += incr; 4536 if (next == 0 || next > syncer_maxdelay) { 4537 start /= 2; 4538 incr /= 2; 4539 if (start == 0) { 4540 start = syncer_maxdelay / 2; 4541 incr = syncer_maxdelay; 4542 } 4543 next = start; 4544 } 4545 bo = &vp->v_bufobj; 4546 BO_LOCK(bo); 4547 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 4548 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 4549 mtx_lock(&sync_mtx); 4550 sync_vnode_count++; 4551 if (mp->mnt_syncer == NULL) { 4552 mp->mnt_syncer = vp; 4553 vp = NULL; 4554 } 4555 mtx_unlock(&sync_mtx); 4556 BO_UNLOCK(bo); 4557 if (vp != NULL) { 4558 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4559 vgone(vp); 4560 vput(vp); 4561 } 4562 } 4563 4564 void 4565 vfs_deallocate_syncvnode(struct mount *mp) 4566 { 4567 struct vnode *vp; 4568 4569 mtx_lock(&sync_mtx); 4570 vp = mp->mnt_syncer; 4571 if (vp != NULL) 4572 mp->mnt_syncer = NULL; 4573 mtx_unlock(&sync_mtx); 4574 if (vp != NULL) 4575 vrele(vp); 4576 } 4577 4578 /* 4579 * Do a lazy sync of the filesystem. 4580 */ 4581 static int 4582 sync_fsync(struct vop_fsync_args *ap) 4583 { 4584 struct vnode *syncvp = ap->a_vp; 4585 struct mount *mp = syncvp->v_mount; 4586 int error, save; 4587 struct bufobj *bo; 4588 4589 /* 4590 * We only need to do something if this is a lazy evaluation. 4591 */ 4592 if (ap->a_waitfor != MNT_LAZY) 4593 return (0); 4594 4595 /* 4596 * Move ourselves to the back of the sync list. 4597 */ 4598 bo = &syncvp->v_bufobj; 4599 BO_LOCK(bo); 4600 vn_syncer_add_to_worklist(bo, syncdelay); 4601 BO_UNLOCK(bo); 4602 4603 /* 4604 * Walk the list of vnodes pushing all that are dirty and 4605 * not already on the sync list. 4606 */ 4607 if (vfs_busy(mp, MBF_NOWAIT) != 0) 4608 return (0); 4609 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 4610 vfs_unbusy(mp); 4611 return (0); 4612 } 4613 save = curthread_pflags_set(TDP_SYNCIO); 4614 /* 4615 * The filesystem at hand may be idle with free vnodes stored in the 4616 * batch. Return them instead of letting them stay there indefinitely. 4617 */ 4618 vnlru_return_batch(mp); 4619 vfs_msync(mp, MNT_NOWAIT); 4620 error = VFS_SYNC(mp, MNT_LAZY); 4621 curthread_pflags_restore(save); 4622 vn_finished_write(mp); 4623 vfs_unbusy(mp); 4624 return (error); 4625 } 4626 4627 /* 4628 * The syncer vnode is no referenced. 4629 */ 4630 static int 4631 sync_inactive(struct vop_inactive_args *ap) 4632 { 4633 4634 vgone(ap->a_vp); 4635 return (0); 4636 } 4637 4638 /* 4639 * The syncer vnode is no longer needed and is being decommissioned. 4640 * 4641 * Modifications to the worklist must be protected by sync_mtx. 4642 */ 4643 static int 4644 sync_reclaim(struct vop_reclaim_args *ap) 4645 { 4646 struct vnode *vp = ap->a_vp; 4647 struct bufobj *bo; 4648 4649 bo = &vp->v_bufobj; 4650 BO_LOCK(bo); 4651 mtx_lock(&sync_mtx); 4652 if (vp->v_mount->mnt_syncer == vp) 4653 vp->v_mount->mnt_syncer = NULL; 4654 if (bo->bo_flag & BO_ONWORKLST) { 4655 LIST_REMOVE(bo, bo_synclist); 4656 syncer_worklist_len--; 4657 sync_vnode_count--; 4658 bo->bo_flag &= ~BO_ONWORKLST; 4659 } 4660 mtx_unlock(&sync_mtx); 4661 BO_UNLOCK(bo); 4662 4663 return (0); 4664 } 4665 4666 int 4667 vn_need_pageq_flush(struct vnode *vp) 4668 { 4669 struct vm_object *obj; 4670 int need; 4671 4672 MPASS(mtx_owned(VI_MTX(vp))); 4673 need = 0; 4674 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 4675 vm_object_mightbedirty(obj)) 4676 need = 1; 4677 return (need); 4678 } 4679 4680 /* 4681 * Check if vnode represents a disk device 4682 */ 4683 int 4684 vn_isdisk(struct vnode *vp, int *errp) 4685 { 4686 int error; 4687 4688 if (vp->v_type != VCHR) { 4689 error = ENOTBLK; 4690 goto out; 4691 } 4692 error = 0; 4693 dev_lock(); 4694 if (vp->v_rdev == NULL) 4695 error = ENXIO; 4696 else if (vp->v_rdev->si_devsw == NULL) 4697 error = ENXIO; 4698 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 4699 error = ENOTBLK; 4700 dev_unlock(); 4701 out: 4702 if (errp != NULL) 4703 *errp = error; 4704 return (error == 0); 4705 } 4706 4707 /* 4708 * Common filesystem object access control check routine. Accepts a 4709 * vnode's type, "mode", uid and gid, requested access mode, credentials, 4710 * and optional call-by-reference privused argument allowing vaccess() 4711 * to indicate to the caller whether privilege was used to satisfy the 4712 * request (obsoleted). Returns 0 on success, or an errno on failure. 4713 */ 4714 int 4715 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 4716 accmode_t accmode, struct ucred *cred, int *privused) 4717 { 4718 accmode_t dac_granted; 4719 accmode_t priv_granted; 4720 4721 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 4722 ("invalid bit in accmode")); 4723 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 4724 ("VAPPEND without VWRITE")); 4725 4726 /* 4727 * Look for a normal, non-privileged way to access the file/directory 4728 * as requested. If it exists, go with that. 4729 */ 4730 4731 if (privused != NULL) 4732 *privused = 0; 4733 4734 dac_granted = 0; 4735 4736 /* Check the owner. */ 4737 if (cred->cr_uid == file_uid) { 4738 dac_granted |= VADMIN; 4739 if (file_mode & S_IXUSR) 4740 dac_granted |= VEXEC; 4741 if (file_mode & S_IRUSR) 4742 dac_granted |= VREAD; 4743 if (file_mode & S_IWUSR) 4744 dac_granted |= (VWRITE | VAPPEND); 4745 4746 if ((accmode & dac_granted) == accmode) 4747 return (0); 4748 4749 goto privcheck; 4750 } 4751 4752 /* Otherwise, check the groups (first match) */ 4753 if (groupmember(file_gid, cred)) { 4754 if (file_mode & S_IXGRP) 4755 dac_granted |= VEXEC; 4756 if (file_mode & S_IRGRP) 4757 dac_granted |= VREAD; 4758 if (file_mode & S_IWGRP) 4759 dac_granted |= (VWRITE | VAPPEND); 4760 4761 if ((accmode & dac_granted) == accmode) 4762 return (0); 4763 4764 goto privcheck; 4765 } 4766 4767 /* Otherwise, check everyone else. */ 4768 if (file_mode & S_IXOTH) 4769 dac_granted |= VEXEC; 4770 if (file_mode & S_IROTH) 4771 dac_granted |= VREAD; 4772 if (file_mode & S_IWOTH) 4773 dac_granted |= (VWRITE | VAPPEND); 4774 if ((accmode & dac_granted) == accmode) 4775 return (0); 4776 4777 privcheck: 4778 /* 4779 * Build a privilege mask to determine if the set of privileges 4780 * satisfies the requirements when combined with the granted mask 4781 * from above. For each privilege, if the privilege is required, 4782 * bitwise or the request type onto the priv_granted mask. 4783 */ 4784 priv_granted = 0; 4785 4786 if (type == VDIR) { 4787 /* 4788 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 4789 * requests, instead of PRIV_VFS_EXEC. 4790 */ 4791 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 4792 !priv_check_cred(cred, PRIV_VFS_LOOKUP)) 4793 priv_granted |= VEXEC; 4794 } else { 4795 /* 4796 * Ensure that at least one execute bit is on. Otherwise, 4797 * a privileged user will always succeed, and we don't want 4798 * this to happen unless the file really is executable. 4799 */ 4800 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 4801 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 4802 !priv_check_cred(cred, PRIV_VFS_EXEC)) 4803 priv_granted |= VEXEC; 4804 } 4805 4806 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 4807 !priv_check_cred(cred, PRIV_VFS_READ)) 4808 priv_granted |= VREAD; 4809 4810 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 4811 !priv_check_cred(cred, PRIV_VFS_WRITE)) 4812 priv_granted |= (VWRITE | VAPPEND); 4813 4814 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 4815 !priv_check_cred(cred, PRIV_VFS_ADMIN)) 4816 priv_granted |= VADMIN; 4817 4818 if ((accmode & (priv_granted | dac_granted)) == accmode) { 4819 /* XXX audit: privilege used */ 4820 if (privused != NULL) 4821 *privused = 1; 4822 return (0); 4823 } 4824 4825 return ((accmode & VADMIN) ? EPERM : EACCES); 4826 } 4827 4828 /* 4829 * Credential check based on process requesting service, and per-attribute 4830 * permissions. 4831 */ 4832 int 4833 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 4834 struct thread *td, accmode_t accmode) 4835 { 4836 4837 /* 4838 * Kernel-invoked always succeeds. 4839 */ 4840 if (cred == NOCRED) 4841 return (0); 4842 4843 /* 4844 * Do not allow privileged processes in jail to directly manipulate 4845 * system attributes. 4846 */ 4847 switch (attrnamespace) { 4848 case EXTATTR_NAMESPACE_SYSTEM: 4849 /* Potentially should be: return (EPERM); */ 4850 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM)); 4851 case EXTATTR_NAMESPACE_USER: 4852 return (VOP_ACCESS(vp, accmode, cred, td)); 4853 default: 4854 return (EPERM); 4855 } 4856 } 4857 4858 #ifdef DEBUG_VFS_LOCKS 4859 /* 4860 * This only exists to suppress warnings from unlocked specfs accesses. It is 4861 * no longer ok to have an unlocked VFS. 4862 */ 4863 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \ 4864 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 4865 4866 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 4867 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, 4868 "Drop into debugger on lock violation"); 4869 4870 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 4871 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 4872 0, "Check for interlock across VOPs"); 4873 4874 int vfs_badlock_print = 1; /* Print lock violations. */ 4875 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 4876 0, "Print lock violations"); 4877 4878 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */ 4879 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode, 4880 0, "Print vnode details on lock violations"); 4881 4882 #ifdef KDB 4883 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 4884 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, 4885 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations"); 4886 #endif 4887 4888 static void 4889 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 4890 { 4891 4892 #ifdef KDB 4893 if (vfs_badlock_backtrace) 4894 kdb_backtrace(); 4895 #endif 4896 if (vfs_badlock_vnode) 4897 vn_printf(vp, "vnode "); 4898 if (vfs_badlock_print) 4899 printf("%s: %p %s\n", str, (void *)vp, msg); 4900 if (vfs_badlock_ddb) 4901 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 4902 } 4903 4904 void 4905 assert_vi_locked(struct vnode *vp, const char *str) 4906 { 4907 4908 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 4909 vfs_badlock("interlock is not locked but should be", str, vp); 4910 } 4911 4912 void 4913 assert_vi_unlocked(struct vnode *vp, const char *str) 4914 { 4915 4916 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 4917 vfs_badlock("interlock is locked but should not be", str, vp); 4918 } 4919 4920 void 4921 assert_vop_locked(struct vnode *vp, const char *str) 4922 { 4923 int locked; 4924 4925 if (!IGNORE_LOCK(vp)) { 4926 locked = VOP_ISLOCKED(vp); 4927 if (locked == 0 || locked == LK_EXCLOTHER) 4928 vfs_badlock("is not locked but should be", str, vp); 4929 } 4930 } 4931 4932 void 4933 assert_vop_unlocked(struct vnode *vp, const char *str) 4934 { 4935 4936 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 4937 vfs_badlock("is locked but should not be", str, vp); 4938 } 4939 4940 void 4941 assert_vop_elocked(struct vnode *vp, const char *str) 4942 { 4943 4944 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 4945 vfs_badlock("is not exclusive locked but should be", str, vp); 4946 } 4947 #endif /* DEBUG_VFS_LOCKS */ 4948 4949 void 4950 vop_rename_fail(struct vop_rename_args *ap) 4951 { 4952 4953 if (ap->a_tvp != NULL) 4954 vput(ap->a_tvp); 4955 if (ap->a_tdvp == ap->a_tvp) 4956 vrele(ap->a_tdvp); 4957 else 4958 vput(ap->a_tdvp); 4959 vrele(ap->a_fdvp); 4960 vrele(ap->a_fvp); 4961 } 4962 4963 void 4964 vop_rename_pre(void *ap) 4965 { 4966 struct vop_rename_args *a = ap; 4967 4968 #ifdef DEBUG_VFS_LOCKS 4969 if (a->a_tvp) 4970 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 4971 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 4972 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 4973 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 4974 4975 /* Check the source (from). */ 4976 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock && 4977 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock)) 4978 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 4979 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock) 4980 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 4981 4982 /* Check the target. */ 4983 if (a->a_tvp) 4984 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 4985 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 4986 #endif 4987 if (a->a_tdvp != a->a_fdvp) 4988 vhold(a->a_fdvp); 4989 if (a->a_tvp != a->a_fvp) 4990 vhold(a->a_fvp); 4991 vhold(a->a_tdvp); 4992 if (a->a_tvp) 4993 vhold(a->a_tvp); 4994 } 4995 4996 #ifdef DEBUG_VFS_LOCKS 4997 void 4998 vop_strategy_pre(void *ap) 4999 { 5000 struct vop_strategy_args *a; 5001 struct buf *bp; 5002 5003 a = ap; 5004 bp = a->a_bp; 5005 5006 /* 5007 * Cluster ops lock their component buffers but not the IO container. 5008 */ 5009 if ((bp->b_flags & B_CLUSTER) != 0) 5010 return; 5011 5012 if (panicstr == NULL && !BUF_ISLOCKED(bp)) { 5013 if (vfs_badlock_print) 5014 printf( 5015 "VOP_STRATEGY: bp is not locked but should be\n"); 5016 if (vfs_badlock_ddb) 5017 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 5018 } 5019 } 5020 5021 void 5022 vop_lock_pre(void *ap) 5023 { 5024 struct vop_lock1_args *a = ap; 5025 5026 if ((a->a_flags & LK_INTERLOCK) == 0) 5027 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5028 else 5029 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 5030 } 5031 5032 void 5033 vop_lock_post(void *ap, int rc) 5034 { 5035 struct vop_lock1_args *a = ap; 5036 5037 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5038 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0) 5039 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 5040 } 5041 5042 void 5043 vop_unlock_pre(void *ap) 5044 { 5045 struct vop_unlock_args *a = ap; 5046 5047 if (a->a_flags & LK_INTERLOCK) 5048 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 5049 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 5050 } 5051 5052 void 5053 vop_unlock_post(void *ap, int rc) 5054 { 5055 struct vop_unlock_args *a = ap; 5056 5057 if (a->a_flags & LK_INTERLOCK) 5058 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 5059 } 5060 5061 void 5062 vop_need_inactive_pre(void *ap) 5063 { 5064 struct vop_need_inactive_args *a = ap; 5065 5066 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5067 } 5068 5069 void 5070 vop_need_inactive_post(void *ap, int rc) 5071 { 5072 struct vop_need_inactive_args *a = ap; 5073 5074 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5075 } 5076 #endif 5077 5078 void 5079 vop_create_post(void *ap, int rc) 5080 { 5081 struct vop_create_args *a = ap; 5082 5083 if (!rc) 5084 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5085 } 5086 5087 void 5088 vop_deleteextattr_post(void *ap, int rc) 5089 { 5090 struct vop_deleteextattr_args *a = ap; 5091 5092 if (!rc) 5093 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5094 } 5095 5096 void 5097 vop_link_post(void *ap, int rc) 5098 { 5099 struct vop_link_args *a = ap; 5100 5101 if (!rc) { 5102 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 5103 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 5104 } 5105 } 5106 5107 void 5108 vop_mkdir_post(void *ap, int rc) 5109 { 5110 struct vop_mkdir_args *a = ap; 5111 5112 if (!rc) 5113 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 5114 } 5115 5116 void 5117 vop_mknod_post(void *ap, int rc) 5118 { 5119 struct vop_mknod_args *a = ap; 5120 5121 if (!rc) 5122 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5123 } 5124 5125 void 5126 vop_reclaim_post(void *ap, int rc) 5127 { 5128 struct vop_reclaim_args *a = ap; 5129 5130 if (!rc) 5131 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE); 5132 } 5133 5134 void 5135 vop_remove_post(void *ap, int rc) 5136 { 5137 struct vop_remove_args *a = ap; 5138 5139 if (!rc) { 5140 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5141 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 5142 } 5143 } 5144 5145 void 5146 vop_rename_post(void *ap, int rc) 5147 { 5148 struct vop_rename_args *a = ap; 5149 long hint; 5150 5151 if (!rc) { 5152 hint = NOTE_WRITE; 5153 if (a->a_fdvp == a->a_tdvp) { 5154 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR) 5155 hint |= NOTE_LINK; 5156 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5157 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5158 } else { 5159 hint |= NOTE_EXTEND; 5160 if (a->a_fvp->v_type == VDIR) 5161 hint |= NOTE_LINK; 5162 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5163 5164 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL && 5165 a->a_tvp->v_type == VDIR) 5166 hint &= ~NOTE_LINK; 5167 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5168 } 5169 5170 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 5171 if (a->a_tvp) 5172 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 5173 } 5174 if (a->a_tdvp != a->a_fdvp) 5175 vdrop(a->a_fdvp); 5176 if (a->a_tvp != a->a_fvp) 5177 vdrop(a->a_fvp); 5178 vdrop(a->a_tdvp); 5179 if (a->a_tvp) 5180 vdrop(a->a_tvp); 5181 } 5182 5183 void 5184 vop_rmdir_post(void *ap, int rc) 5185 { 5186 struct vop_rmdir_args *a = ap; 5187 5188 if (!rc) { 5189 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 5190 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 5191 } 5192 } 5193 5194 void 5195 vop_setattr_post(void *ap, int rc) 5196 { 5197 struct vop_setattr_args *a = ap; 5198 5199 if (!rc) 5200 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5201 } 5202 5203 void 5204 vop_setextattr_post(void *ap, int rc) 5205 { 5206 struct vop_setextattr_args *a = ap; 5207 5208 if (!rc) 5209 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5210 } 5211 5212 void 5213 vop_symlink_post(void *ap, int rc) 5214 { 5215 struct vop_symlink_args *a = ap; 5216 5217 if (!rc) 5218 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 5219 } 5220 5221 void 5222 vop_open_post(void *ap, int rc) 5223 { 5224 struct vop_open_args *a = ap; 5225 5226 if (!rc) 5227 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN); 5228 } 5229 5230 void 5231 vop_close_post(void *ap, int rc) 5232 { 5233 struct vop_close_args *a = ap; 5234 5235 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */ 5236 !VN_IS_DOOMED(a->a_vp))) { 5237 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ? 5238 NOTE_CLOSE_WRITE : NOTE_CLOSE); 5239 } 5240 } 5241 5242 void 5243 vop_read_post(void *ap, int rc) 5244 { 5245 struct vop_read_args *a = ap; 5246 5247 if (!rc) 5248 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5249 } 5250 5251 void 5252 vop_readdir_post(void *ap, int rc) 5253 { 5254 struct vop_readdir_args *a = ap; 5255 5256 if (!rc) 5257 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5258 } 5259 5260 static struct knlist fs_knlist; 5261 5262 static void 5263 vfs_event_init(void *arg) 5264 { 5265 knlist_init_mtx(&fs_knlist, NULL); 5266 } 5267 /* XXX - correct order? */ 5268 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 5269 5270 void 5271 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused) 5272 { 5273 5274 KNOTE_UNLOCKED(&fs_knlist, event); 5275 } 5276 5277 static int filt_fsattach(struct knote *kn); 5278 static void filt_fsdetach(struct knote *kn); 5279 static int filt_fsevent(struct knote *kn, long hint); 5280 5281 struct filterops fs_filtops = { 5282 .f_isfd = 0, 5283 .f_attach = filt_fsattach, 5284 .f_detach = filt_fsdetach, 5285 .f_event = filt_fsevent 5286 }; 5287 5288 static int 5289 filt_fsattach(struct knote *kn) 5290 { 5291 5292 kn->kn_flags |= EV_CLEAR; 5293 knlist_add(&fs_knlist, kn, 0); 5294 return (0); 5295 } 5296 5297 static void 5298 filt_fsdetach(struct knote *kn) 5299 { 5300 5301 knlist_remove(&fs_knlist, kn, 0); 5302 } 5303 5304 static int 5305 filt_fsevent(struct knote *kn, long hint) 5306 { 5307 5308 kn->kn_fflags |= hint; 5309 return (kn->kn_fflags != 0); 5310 } 5311 5312 static int 5313 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 5314 { 5315 struct vfsidctl vc; 5316 int error; 5317 struct mount *mp; 5318 5319 error = SYSCTL_IN(req, &vc, sizeof(vc)); 5320 if (error) 5321 return (error); 5322 if (vc.vc_vers != VFS_CTL_VERS1) 5323 return (EINVAL); 5324 mp = vfs_getvfs(&vc.vc_fsid); 5325 if (mp == NULL) 5326 return (ENOENT); 5327 /* ensure that a specific sysctl goes to the right filesystem. */ 5328 if (strcmp(vc.vc_fstypename, "*") != 0 && 5329 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 5330 vfs_rel(mp); 5331 return (EINVAL); 5332 } 5333 VCTLTOREQ(&vc, req); 5334 error = VFS_SYSCTL(mp, vc.vc_op, req); 5335 vfs_rel(mp); 5336 return (error); 5337 } 5338 5339 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR, 5340 NULL, 0, sysctl_vfs_ctl, "", 5341 "Sysctl by fsid"); 5342 5343 /* 5344 * Function to initialize a va_filerev field sensibly. 5345 * XXX: Wouldn't a random number make a lot more sense ?? 5346 */ 5347 u_quad_t 5348 init_va_filerev(void) 5349 { 5350 struct bintime bt; 5351 5352 getbinuptime(&bt); 5353 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 5354 } 5355 5356 static int filt_vfsread(struct knote *kn, long hint); 5357 static int filt_vfswrite(struct knote *kn, long hint); 5358 static int filt_vfsvnode(struct knote *kn, long hint); 5359 static void filt_vfsdetach(struct knote *kn); 5360 static struct filterops vfsread_filtops = { 5361 .f_isfd = 1, 5362 .f_detach = filt_vfsdetach, 5363 .f_event = filt_vfsread 5364 }; 5365 static struct filterops vfswrite_filtops = { 5366 .f_isfd = 1, 5367 .f_detach = filt_vfsdetach, 5368 .f_event = filt_vfswrite 5369 }; 5370 static struct filterops vfsvnode_filtops = { 5371 .f_isfd = 1, 5372 .f_detach = filt_vfsdetach, 5373 .f_event = filt_vfsvnode 5374 }; 5375 5376 static void 5377 vfs_knllock(void *arg) 5378 { 5379 struct vnode *vp = arg; 5380 5381 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 5382 } 5383 5384 static void 5385 vfs_knlunlock(void *arg) 5386 { 5387 struct vnode *vp = arg; 5388 5389 VOP_UNLOCK(vp, 0); 5390 } 5391 5392 static void 5393 vfs_knl_assert_locked(void *arg) 5394 { 5395 #ifdef DEBUG_VFS_LOCKS 5396 struct vnode *vp = arg; 5397 5398 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked"); 5399 #endif 5400 } 5401 5402 static void 5403 vfs_knl_assert_unlocked(void *arg) 5404 { 5405 #ifdef DEBUG_VFS_LOCKS 5406 struct vnode *vp = arg; 5407 5408 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked"); 5409 #endif 5410 } 5411 5412 int 5413 vfs_kqfilter(struct vop_kqfilter_args *ap) 5414 { 5415 struct vnode *vp = ap->a_vp; 5416 struct knote *kn = ap->a_kn; 5417 struct knlist *knl; 5418 5419 switch (kn->kn_filter) { 5420 case EVFILT_READ: 5421 kn->kn_fop = &vfsread_filtops; 5422 break; 5423 case EVFILT_WRITE: 5424 kn->kn_fop = &vfswrite_filtops; 5425 break; 5426 case EVFILT_VNODE: 5427 kn->kn_fop = &vfsvnode_filtops; 5428 break; 5429 default: 5430 return (EINVAL); 5431 } 5432 5433 kn->kn_hook = (caddr_t)vp; 5434 5435 v_addpollinfo(vp); 5436 if (vp->v_pollinfo == NULL) 5437 return (ENOMEM); 5438 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 5439 vhold(vp); 5440 knlist_add(knl, kn, 0); 5441 5442 return (0); 5443 } 5444 5445 /* 5446 * Detach knote from vnode 5447 */ 5448 static void 5449 filt_vfsdetach(struct knote *kn) 5450 { 5451 struct vnode *vp = (struct vnode *)kn->kn_hook; 5452 5453 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 5454 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 5455 vdrop(vp); 5456 } 5457 5458 /*ARGSUSED*/ 5459 static int 5460 filt_vfsread(struct knote *kn, long hint) 5461 { 5462 struct vnode *vp = (struct vnode *)kn->kn_hook; 5463 struct vattr va; 5464 int res; 5465 5466 /* 5467 * filesystem is gone, so set the EOF flag and schedule 5468 * the knote for deletion. 5469 */ 5470 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5471 VI_LOCK(vp); 5472 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5473 VI_UNLOCK(vp); 5474 return (1); 5475 } 5476 5477 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 5478 return (0); 5479 5480 VI_LOCK(vp); 5481 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 5482 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0; 5483 VI_UNLOCK(vp); 5484 return (res); 5485 } 5486 5487 /*ARGSUSED*/ 5488 static int 5489 filt_vfswrite(struct knote *kn, long hint) 5490 { 5491 struct vnode *vp = (struct vnode *)kn->kn_hook; 5492 5493 VI_LOCK(vp); 5494 5495 /* 5496 * filesystem is gone, so set the EOF flag and schedule 5497 * the knote for deletion. 5498 */ 5499 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) 5500 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 5501 5502 kn->kn_data = 0; 5503 VI_UNLOCK(vp); 5504 return (1); 5505 } 5506 5507 static int 5508 filt_vfsvnode(struct knote *kn, long hint) 5509 { 5510 struct vnode *vp = (struct vnode *)kn->kn_hook; 5511 int res; 5512 5513 VI_LOCK(vp); 5514 if (kn->kn_sfflags & hint) 5515 kn->kn_fflags |= hint; 5516 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 5517 kn->kn_flags |= EV_EOF; 5518 VI_UNLOCK(vp); 5519 return (1); 5520 } 5521 res = (kn->kn_fflags != 0); 5522 VI_UNLOCK(vp); 5523 return (res); 5524 } 5525 5526 /* 5527 * Returns whether the directory is empty or not. 5528 * If it is empty, the return value is 0; otherwise 5529 * the return value is an error value (which may 5530 * be ENOTEMPTY). 5531 */ 5532 int 5533 vfs_emptydir(struct vnode *vp) 5534 { 5535 struct uio uio; 5536 struct iovec iov; 5537 struct dirent *dirent, *dp, *endp; 5538 int error, eof; 5539 5540 error = 0; 5541 eof = 0; 5542 5543 ASSERT_VOP_LOCKED(vp, "vfs_emptydir"); 5544 5545 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK); 5546 iov.iov_base = dirent; 5547 iov.iov_len = sizeof(struct dirent); 5548 5549 uio.uio_iov = &iov; 5550 uio.uio_iovcnt = 1; 5551 uio.uio_offset = 0; 5552 uio.uio_resid = sizeof(struct dirent); 5553 uio.uio_segflg = UIO_SYSSPACE; 5554 uio.uio_rw = UIO_READ; 5555 uio.uio_td = curthread; 5556 5557 while (eof == 0 && error == 0) { 5558 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof, 5559 NULL, NULL); 5560 if (error != 0) 5561 break; 5562 endp = (void *)((uint8_t *)dirent + 5563 sizeof(struct dirent) - uio.uio_resid); 5564 for (dp = dirent; dp < endp; 5565 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) { 5566 if (dp->d_type == DT_WHT) 5567 continue; 5568 if (dp->d_namlen == 0) 5569 continue; 5570 if (dp->d_type != DT_DIR && 5571 dp->d_type != DT_UNKNOWN) { 5572 error = ENOTEMPTY; 5573 break; 5574 } 5575 if (dp->d_namlen > 2) { 5576 error = ENOTEMPTY; 5577 break; 5578 } 5579 if (dp->d_namlen == 1 && 5580 dp->d_name[0] != '.') { 5581 error = ENOTEMPTY; 5582 break; 5583 } 5584 if (dp->d_namlen == 2 && 5585 dp->d_name[1] != '.') { 5586 error = ENOTEMPTY; 5587 break; 5588 } 5589 uio.uio_resid = sizeof(struct dirent); 5590 } 5591 } 5592 free(dirent, M_TEMP); 5593 return (error); 5594 } 5595 5596 int 5597 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 5598 { 5599 int error; 5600 5601 if (dp->d_reclen > ap->a_uio->uio_resid) 5602 return (ENAMETOOLONG); 5603 error = uiomove(dp, dp->d_reclen, ap->a_uio); 5604 if (error) { 5605 if (ap->a_ncookies != NULL) { 5606 if (ap->a_cookies != NULL) 5607 free(ap->a_cookies, M_TEMP); 5608 ap->a_cookies = NULL; 5609 *ap->a_ncookies = 0; 5610 } 5611 return (error); 5612 } 5613 if (ap->a_ncookies == NULL) 5614 return (0); 5615 5616 KASSERT(ap->a_cookies, 5617 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 5618 5619 *ap->a_cookies = realloc(*ap->a_cookies, 5620 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 5621 (*ap->a_cookies)[*ap->a_ncookies] = off; 5622 *ap->a_ncookies += 1; 5623 return (0); 5624 } 5625 5626 /* 5627 * Mark for update the access time of the file if the filesystem 5628 * supports VOP_MARKATIME. This functionality is used by execve and 5629 * mmap, so we want to avoid the I/O implied by directly setting 5630 * va_atime for the sake of efficiency. 5631 */ 5632 void 5633 vfs_mark_atime(struct vnode *vp, struct ucred *cred) 5634 { 5635 struct mount *mp; 5636 5637 mp = vp->v_mount; 5638 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime"); 5639 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 5640 (void)VOP_MARKATIME(vp); 5641 } 5642 5643 /* 5644 * The purpose of this routine is to remove granularity from accmode_t, 5645 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE, 5646 * VADMIN and VAPPEND. 5647 * 5648 * If it returns 0, the caller is supposed to continue with the usual 5649 * access checks using 'accmode' as modified by this routine. If it 5650 * returns nonzero value, the caller is supposed to return that value 5651 * as errno. 5652 * 5653 * Note that after this routine runs, accmode may be zero. 5654 */ 5655 int 5656 vfs_unixify_accmode(accmode_t *accmode) 5657 { 5658 /* 5659 * There is no way to specify explicit "deny" rule using 5660 * file mode or POSIX.1e ACLs. 5661 */ 5662 if (*accmode & VEXPLICIT_DENY) { 5663 *accmode = 0; 5664 return (0); 5665 } 5666 5667 /* 5668 * None of these can be translated into usual access bits. 5669 * Also, the common case for NFSv4 ACLs is to not contain 5670 * either of these bits. Caller should check for VWRITE 5671 * on the containing directory instead. 5672 */ 5673 if (*accmode & (VDELETE_CHILD | VDELETE)) 5674 return (EPERM); 5675 5676 if (*accmode & VADMIN_PERMS) { 5677 *accmode &= ~VADMIN_PERMS; 5678 *accmode |= VADMIN; 5679 } 5680 5681 /* 5682 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL 5683 * or VSYNCHRONIZE using file mode or POSIX.1e ACL. 5684 */ 5685 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE); 5686 5687 return (0); 5688 } 5689 5690 /* 5691 * Clear out a doomed vnode (if any) and replace it with a new one as long 5692 * as the fs is not being unmounted. Return the root vnode to the caller. 5693 */ 5694 static int __noinline 5695 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp) 5696 { 5697 struct vnode *vp; 5698 int error; 5699 5700 restart: 5701 if (mp->mnt_rootvnode != NULL) { 5702 MNT_ILOCK(mp); 5703 vp = mp->mnt_rootvnode; 5704 if (vp != NULL) { 5705 if (!VN_IS_DOOMED(vp)) { 5706 vrefact(vp); 5707 MNT_IUNLOCK(mp); 5708 error = vn_lock(vp, flags); 5709 if (error == 0) { 5710 *vpp = vp; 5711 return (0); 5712 } 5713 vrele(vp); 5714 goto restart; 5715 } 5716 /* 5717 * Clear the old one. 5718 */ 5719 mp->mnt_rootvnode = NULL; 5720 } 5721 MNT_IUNLOCK(mp); 5722 if (vp != NULL) { 5723 /* 5724 * Paired with a fence in vfs_op_thread_exit(). 5725 */ 5726 atomic_thread_fence_acq(); 5727 vfs_op_barrier_wait(mp); 5728 vrele(vp); 5729 } 5730 } 5731 error = VFS_CACHEDROOT(mp, flags, vpp); 5732 if (error != 0) 5733 return (error); 5734 if (mp->mnt_vfs_ops == 0) { 5735 MNT_ILOCK(mp); 5736 if (mp->mnt_vfs_ops != 0) { 5737 MNT_IUNLOCK(mp); 5738 return (0); 5739 } 5740 if (mp->mnt_rootvnode == NULL) { 5741 vrefact(*vpp); 5742 mp->mnt_rootvnode = *vpp; 5743 } else { 5744 if (mp->mnt_rootvnode != *vpp) { 5745 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) { 5746 panic("%s: mismatch between vnode returned " 5747 " by VFS_CACHEDROOT and the one cached " 5748 " (%p != %p)", 5749 __func__, *vpp, mp->mnt_rootvnode); 5750 } 5751 } 5752 } 5753 MNT_IUNLOCK(mp); 5754 } 5755 return (0); 5756 } 5757 5758 int 5759 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp) 5760 { 5761 struct vnode *vp; 5762 int error; 5763 5764 if (!vfs_op_thread_enter(mp)) 5765 return (vfs_cache_root_fallback(mp, flags, vpp)); 5766 vp = (struct vnode *)atomic_load_ptr(&mp->mnt_rootvnode); 5767 if (vp == NULL || VN_IS_DOOMED(vp)) { 5768 vfs_op_thread_exit(mp); 5769 return (vfs_cache_root_fallback(mp, flags, vpp)); 5770 } 5771 vrefact(vp); 5772 vfs_op_thread_exit(mp); 5773 error = vn_lock(vp, flags); 5774 if (error != 0) { 5775 vrele(vp); 5776 return (vfs_cache_root_fallback(mp, flags, vpp)); 5777 } 5778 *vpp = vp; 5779 return (0); 5780 } 5781 5782 struct vnode * 5783 vfs_cache_root_clear(struct mount *mp) 5784 { 5785 struct vnode *vp; 5786 5787 /* 5788 * ops > 0 guarantees there is nobody who can see this vnode 5789 */ 5790 MPASS(mp->mnt_vfs_ops > 0); 5791 vp = mp->mnt_rootvnode; 5792 mp->mnt_rootvnode = NULL; 5793 return (vp); 5794 } 5795 5796 void 5797 vfs_cache_root_set(struct mount *mp, struct vnode *vp) 5798 { 5799 5800 MPASS(mp->mnt_vfs_ops > 0); 5801 vrefact(vp); 5802 mp->mnt_rootvnode = vp; 5803 } 5804 5805 /* 5806 * These are helper functions for filesystems to traverse all 5807 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h. 5808 * 5809 * This interface replaces MNT_VNODE_FOREACH. 5810 */ 5811 5812 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker"); 5813 5814 struct vnode * 5815 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp) 5816 { 5817 struct vnode *vp; 5818 5819 if (should_yield()) 5820 kern_yield(PRI_USER); 5821 MNT_ILOCK(mp); 5822 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5823 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL; 5824 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 5825 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 5826 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 5827 continue; 5828 VI_LOCK(vp); 5829 if (VN_IS_DOOMED(vp)) { 5830 VI_UNLOCK(vp); 5831 continue; 5832 } 5833 break; 5834 } 5835 if (vp == NULL) { 5836 __mnt_vnode_markerfree_all(mvp, mp); 5837 /* MNT_IUNLOCK(mp); -- done in above function */ 5838 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 5839 return (NULL); 5840 } 5841 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 5842 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 5843 MNT_IUNLOCK(mp); 5844 return (vp); 5845 } 5846 5847 struct vnode * 5848 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp) 5849 { 5850 struct vnode *vp; 5851 5852 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 5853 MNT_ILOCK(mp); 5854 MNT_REF(mp); 5855 (*mvp)->v_mount = mp; 5856 (*mvp)->v_type = VMARKER; 5857 5858 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 5859 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 5860 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 5861 continue; 5862 VI_LOCK(vp); 5863 if (VN_IS_DOOMED(vp)) { 5864 VI_UNLOCK(vp); 5865 continue; 5866 } 5867 break; 5868 } 5869 if (vp == NULL) { 5870 MNT_REL(mp); 5871 MNT_IUNLOCK(mp); 5872 free(*mvp, M_VNODE_MARKER); 5873 *mvp = NULL; 5874 return (NULL); 5875 } 5876 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 5877 MNT_IUNLOCK(mp); 5878 return (vp); 5879 } 5880 5881 void 5882 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp) 5883 { 5884 5885 if (*mvp == NULL) { 5886 MNT_IUNLOCK(mp); 5887 return; 5888 } 5889 5890 mtx_assert(MNT_MTX(mp), MA_OWNED); 5891 5892 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5893 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 5894 MNT_REL(mp); 5895 MNT_IUNLOCK(mp); 5896 free(*mvp, M_VNODE_MARKER); 5897 *mvp = NULL; 5898 } 5899 5900 /* 5901 * These are helper functions for filesystems to traverse their 5902 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h 5903 */ 5904 static void 5905 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp) 5906 { 5907 5908 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 5909 5910 MNT_ILOCK(mp); 5911 MNT_REL(mp); 5912 MNT_IUNLOCK(mp); 5913 free(*mvp, M_VNODE_MARKER); 5914 *mvp = NULL; 5915 } 5916 5917 /* 5918 * Relock the mp mount vnode list lock with the vp vnode interlock in the 5919 * conventional lock order during mnt_vnode_next_active iteration. 5920 * 5921 * On entry, the mount vnode list lock is held and the vnode interlock is not. 5922 * The list lock is dropped and reacquired. On success, both locks are held. 5923 * On failure, the mount vnode list lock is held but the vnode interlock is 5924 * not, and the procedure may have yielded. 5925 */ 5926 static bool 5927 mnt_vnode_next_active_relock(struct vnode *mvp, struct mount *mp, 5928 struct vnode *vp) 5929 { 5930 const struct vnode *tmp; 5931 bool held, ret; 5932 5933 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER && 5934 TAILQ_NEXT(mvp, v_actfreelist) != NULL, mvp, 5935 ("%s: bad marker", __func__)); 5936 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp, 5937 ("%s: inappropriate vnode", __func__)); 5938 ASSERT_VI_UNLOCKED(vp, __func__); 5939 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 5940 5941 ret = false; 5942 5943 TAILQ_REMOVE(&mp->mnt_activevnodelist, mvp, v_actfreelist); 5944 TAILQ_INSERT_BEFORE(vp, mvp, v_actfreelist); 5945 5946 /* 5947 * Use a hold to prevent vp from disappearing while the mount vnode 5948 * list lock is dropped and reacquired. Normally a hold would be 5949 * acquired with vhold(), but that might try to acquire the vnode 5950 * interlock, which would be a LOR with the mount vnode list lock. 5951 */ 5952 held = refcount_acquire_if_not_zero(&vp->v_holdcnt); 5953 mtx_unlock(&mp->mnt_listmtx); 5954 if (!held) 5955 goto abort; 5956 VI_LOCK(vp); 5957 if (!refcount_release_if_not_last(&vp->v_holdcnt)) { 5958 vdropl(vp); 5959 goto abort; 5960 } 5961 mtx_lock(&mp->mnt_listmtx); 5962 5963 /* 5964 * Determine whether the vnode is still the next one after the marker, 5965 * excepting any other markers. If the vnode has not been doomed by 5966 * vgone() then the hold should have ensured that it remained on the 5967 * active list. If it has been doomed but is still on the active list, 5968 * don't abort, but rather skip over it (avoid spinning on doomed 5969 * vnodes). 5970 */ 5971 tmp = mvp; 5972 do { 5973 tmp = TAILQ_NEXT(tmp, v_actfreelist); 5974 } while (tmp != NULL && tmp->v_type == VMARKER); 5975 if (tmp != vp) { 5976 mtx_unlock(&mp->mnt_listmtx); 5977 VI_UNLOCK(vp); 5978 goto abort; 5979 } 5980 5981 ret = true; 5982 goto out; 5983 abort: 5984 maybe_yield(); 5985 mtx_lock(&mp->mnt_listmtx); 5986 out: 5987 if (ret) 5988 ASSERT_VI_LOCKED(vp, __func__); 5989 else 5990 ASSERT_VI_UNLOCKED(vp, __func__); 5991 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 5992 return (ret); 5993 } 5994 5995 static struct vnode * 5996 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp) 5997 { 5998 struct vnode *vp, *nvp; 5999 6000 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6001 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6002 restart: 6003 vp = TAILQ_NEXT(*mvp, v_actfreelist); 6004 while (vp != NULL) { 6005 if (vp->v_type == VMARKER) { 6006 vp = TAILQ_NEXT(vp, v_actfreelist); 6007 continue; 6008 } 6009 /* 6010 * Try-lock because this is the wrong lock order. If that does 6011 * not succeed, drop the mount vnode list lock and try to 6012 * reacquire it and the vnode interlock in the right order. 6013 */ 6014 if (!VI_TRYLOCK(vp) && 6015 !mnt_vnode_next_active_relock(*mvp, mp, vp)) 6016 goto restart; 6017 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp)); 6018 KASSERT(vp->v_mount == mp || vp->v_mount == NULL, 6019 ("alien vnode on the active list %p %p", vp, mp)); 6020 if (vp->v_mount == mp && !VN_IS_DOOMED(vp)) 6021 break; 6022 nvp = TAILQ_NEXT(vp, v_actfreelist); 6023 VI_UNLOCK(vp); 6024 vp = nvp; 6025 } 6026 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 6027 6028 /* Check if we are done */ 6029 if (vp == NULL) { 6030 mtx_unlock(&mp->mnt_listmtx); 6031 mnt_vnode_markerfree_active(mvp, mp); 6032 return (NULL); 6033 } 6034 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist); 6035 mtx_unlock(&mp->mnt_listmtx); 6036 ASSERT_VI_LOCKED(vp, "active iter"); 6037 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp)); 6038 return (vp); 6039 } 6040 6041 struct vnode * 6042 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp) 6043 { 6044 6045 if (should_yield()) 6046 kern_yield(PRI_USER); 6047 mtx_lock(&mp->mnt_listmtx); 6048 return (mnt_vnode_next_active(mvp, mp)); 6049 } 6050 6051 struct vnode * 6052 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp) 6053 { 6054 struct vnode *vp; 6055 6056 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 6057 MNT_ILOCK(mp); 6058 MNT_REF(mp); 6059 MNT_IUNLOCK(mp); 6060 (*mvp)->v_type = VMARKER; 6061 (*mvp)->v_mount = mp; 6062 6063 mtx_lock(&mp->mnt_listmtx); 6064 vp = TAILQ_FIRST(&mp->mnt_activevnodelist); 6065 if (vp == NULL) { 6066 mtx_unlock(&mp->mnt_listmtx); 6067 mnt_vnode_markerfree_active(mvp, mp); 6068 return (NULL); 6069 } 6070 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist); 6071 return (mnt_vnode_next_active(mvp, mp)); 6072 } 6073 6074 void 6075 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp) 6076 { 6077 6078 if (*mvp == NULL) 6079 return; 6080 6081 mtx_lock(&mp->mnt_listmtx); 6082 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 6083 mtx_unlock(&mp->mnt_listmtx); 6084 mnt_vnode_markerfree_active(mvp, mp); 6085 } 6086