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