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 do { 1941 error = BO_SYNC(bo, MNT_WAIT); 1942 } while (error == ERELOOKUP); 1943 if (error != 0) 1944 return (error); 1945 /* 1946 * XXX We could save a lock/unlock if this was only 1947 * enabled under INVARIANTS 1948 */ 1949 BO_LOCK(bo); 1950 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1951 panic("vinvalbuf: dirty bufs"); 1952 } 1953 } 1954 /* 1955 * If you alter this loop please notice that interlock is dropped and 1956 * reacquired in flushbuflist. Special care is needed to ensure that 1957 * no race conditions occur from this. 1958 */ 1959 do { 1960 error = flushbuflist(&bo->bo_clean, 1961 flags, bo, slpflag, slptimeo); 1962 if (error == 0 && !(flags & V_CLEANONLY)) 1963 error = flushbuflist(&bo->bo_dirty, 1964 flags, bo, slpflag, slptimeo); 1965 if (error != 0 && error != EAGAIN) { 1966 BO_UNLOCK(bo); 1967 return (error); 1968 } 1969 } while (error != 0); 1970 1971 /* 1972 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1973 * have write I/O in-progress but if there is a VM object then the 1974 * VM object can also have read-I/O in-progress. 1975 */ 1976 do { 1977 bufobj_wwait(bo, 0, 0); 1978 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) { 1979 BO_UNLOCK(bo); 1980 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx"); 1981 BO_LOCK(bo); 1982 } 1983 } while (bo->bo_numoutput > 0); 1984 BO_UNLOCK(bo); 1985 1986 /* 1987 * Destroy the copy in the VM cache, too. 1988 */ 1989 if (bo->bo_object != NULL && 1990 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) { 1991 VM_OBJECT_WLOCK(bo->bo_object); 1992 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ? 1993 OBJPR_CLEANONLY : 0); 1994 VM_OBJECT_WUNLOCK(bo->bo_object); 1995 } 1996 1997 #ifdef INVARIANTS 1998 BO_LOCK(bo); 1999 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO | 2000 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 || 2001 bo->bo_clean.bv_cnt > 0)) 2002 panic("vinvalbuf: flush failed"); 2003 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 && 2004 bo->bo_dirty.bv_cnt > 0) 2005 panic("vinvalbuf: flush dirty failed"); 2006 BO_UNLOCK(bo); 2007 #endif 2008 return (0); 2009 } 2010 2011 /* 2012 * Flush out and invalidate all buffers associated with a vnode. 2013 * Called with the underlying object locked. 2014 */ 2015 int 2016 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 2017 { 2018 2019 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 2020 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 2021 if (vp->v_object != NULL && vp->v_object->handle != vp) 2022 return (0); 2023 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo)); 2024 } 2025 2026 /* 2027 * Flush out buffers on the specified list. 2028 * 2029 */ 2030 static int 2031 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 2032 int slptimeo) 2033 { 2034 struct buf *bp, *nbp; 2035 int retval, error; 2036 daddr_t lblkno; 2037 b_xflags_t xflags; 2038 2039 ASSERT_BO_WLOCKED(bo); 2040 2041 retval = 0; 2042 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 2043 /* 2044 * If we are flushing both V_NORMAL and V_ALT buffers then 2045 * do not skip any buffers. If we are flushing only V_NORMAL 2046 * buffers then skip buffers marked as BX_ALTDATA. If we are 2047 * flushing only V_ALT buffers then skip buffers not marked 2048 * as BX_ALTDATA. 2049 */ 2050 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) && 2051 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) || 2052 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) { 2053 continue; 2054 } 2055 if (nbp != NULL) { 2056 lblkno = nbp->b_lblkno; 2057 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN); 2058 } 2059 retval = EAGAIN; 2060 error = BUF_TIMELOCK(bp, 2061 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo), 2062 "flushbuf", slpflag, slptimeo); 2063 if (error) { 2064 BO_LOCK(bo); 2065 return (error != ENOLCK ? error : EAGAIN); 2066 } 2067 KASSERT(bp->b_bufobj == bo, 2068 ("bp %p wrong b_bufobj %p should be %p", 2069 bp, bp->b_bufobj, bo)); 2070 /* 2071 * XXX Since there are no node locks for NFS, I 2072 * believe there is a slight chance that a delayed 2073 * write will occur while sleeping just above, so 2074 * check for it. 2075 */ 2076 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 2077 (flags & V_SAVE)) { 2078 bremfree(bp); 2079 bp->b_flags |= B_ASYNC; 2080 bwrite(bp); 2081 BO_LOCK(bo); 2082 return (EAGAIN); /* XXX: why not loop ? */ 2083 } 2084 bremfree(bp); 2085 bp->b_flags |= (B_INVAL | B_RELBUF); 2086 bp->b_flags &= ~B_ASYNC; 2087 brelse(bp); 2088 BO_LOCK(bo); 2089 if (nbp == NULL) 2090 break; 2091 nbp = gbincore(bo, lblkno); 2092 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2093 != xflags) 2094 break; /* nbp invalid */ 2095 } 2096 return (retval); 2097 } 2098 2099 int 2100 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn) 2101 { 2102 struct buf *bp; 2103 int error; 2104 daddr_t lblkno; 2105 2106 ASSERT_BO_LOCKED(bo); 2107 2108 for (lblkno = startn;;) { 2109 again: 2110 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno); 2111 if (bp == NULL || bp->b_lblkno >= endn || 2112 bp->b_lblkno < startn) 2113 break; 2114 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 2115 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0); 2116 if (error != 0) { 2117 BO_RLOCK(bo); 2118 if (error == ENOLCK) 2119 goto again; 2120 return (error); 2121 } 2122 KASSERT(bp->b_bufobj == bo, 2123 ("bp %p wrong b_bufobj %p should be %p", 2124 bp, bp->b_bufobj, bo)); 2125 lblkno = bp->b_lblkno + 1; 2126 if ((bp->b_flags & B_MANAGED) == 0) 2127 bremfree(bp); 2128 bp->b_flags |= B_RELBUF; 2129 /* 2130 * In the VMIO case, use the B_NOREUSE flag to hint that the 2131 * pages backing each buffer in the range are unlikely to be 2132 * reused. Dirty buffers will have the hint applied once 2133 * they've been written. 2134 */ 2135 if ((bp->b_flags & B_VMIO) != 0) 2136 bp->b_flags |= B_NOREUSE; 2137 brelse(bp); 2138 BO_RLOCK(bo); 2139 } 2140 return (0); 2141 } 2142 2143 /* 2144 * Truncate a file's buffer and pages to a specified length. This 2145 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 2146 * sync activity. 2147 */ 2148 int 2149 vtruncbuf(struct vnode *vp, off_t length, int blksize) 2150 { 2151 struct buf *bp, *nbp; 2152 struct bufobj *bo; 2153 daddr_t startlbn; 2154 2155 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__, 2156 vp, blksize, (uintmax_t)length); 2157 2158 /* 2159 * Round up to the *next* lbn. 2160 */ 2161 startlbn = howmany(length, blksize); 2162 2163 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 2164 2165 bo = &vp->v_bufobj; 2166 restart_unlocked: 2167 BO_LOCK(bo); 2168 2169 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN) 2170 ; 2171 2172 if (length > 0) { 2173 restartsync: 2174 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2175 if (bp->b_lblkno > 0) 2176 continue; 2177 /* 2178 * Since we hold the vnode lock this should only 2179 * fail if we're racing with the buf daemon. 2180 */ 2181 if (BUF_LOCK(bp, 2182 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2183 BO_LOCKPTR(bo)) == ENOLCK) 2184 goto restart_unlocked; 2185 2186 VNASSERT((bp->b_flags & B_DELWRI), vp, 2187 ("buf(%p) on dirty queue without DELWRI", bp)); 2188 2189 bremfree(bp); 2190 bawrite(bp); 2191 BO_LOCK(bo); 2192 goto restartsync; 2193 } 2194 } 2195 2196 bufobj_wwait(bo, 0, 0); 2197 BO_UNLOCK(bo); 2198 vnode_pager_setsize(vp, length); 2199 2200 return (0); 2201 } 2202 2203 /* 2204 * Invalidate the cached pages of a file's buffer within the range of block 2205 * numbers [startlbn, endlbn). 2206 */ 2207 void 2208 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn, 2209 int blksize) 2210 { 2211 struct bufobj *bo; 2212 off_t start, end; 2213 2214 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range"); 2215 2216 start = blksize * startlbn; 2217 end = blksize * endlbn; 2218 2219 bo = &vp->v_bufobj; 2220 BO_LOCK(bo); 2221 MPASS(blksize == bo->bo_bsize); 2222 2223 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN) 2224 ; 2225 2226 BO_UNLOCK(bo); 2227 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1)); 2228 } 2229 2230 static int 2231 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo, 2232 daddr_t startlbn, daddr_t endlbn) 2233 { 2234 struct buf *bp, *nbp; 2235 bool anyfreed; 2236 2237 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked"); 2238 ASSERT_BO_LOCKED(bo); 2239 2240 do { 2241 anyfreed = false; 2242 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 2243 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2244 continue; 2245 if (BUF_LOCK(bp, 2246 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2247 BO_LOCKPTR(bo)) == ENOLCK) { 2248 BO_LOCK(bo); 2249 return (EAGAIN); 2250 } 2251 2252 bremfree(bp); 2253 bp->b_flags |= B_INVAL | B_RELBUF; 2254 bp->b_flags &= ~B_ASYNC; 2255 brelse(bp); 2256 anyfreed = true; 2257 2258 BO_LOCK(bo); 2259 if (nbp != NULL && 2260 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 2261 nbp->b_vp != vp || 2262 (nbp->b_flags & B_DELWRI) != 0)) 2263 return (EAGAIN); 2264 } 2265 2266 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2267 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn) 2268 continue; 2269 if (BUF_LOCK(bp, 2270 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2271 BO_LOCKPTR(bo)) == ENOLCK) { 2272 BO_LOCK(bo); 2273 return (EAGAIN); 2274 } 2275 bremfree(bp); 2276 bp->b_flags |= B_INVAL | B_RELBUF; 2277 bp->b_flags &= ~B_ASYNC; 2278 brelse(bp); 2279 anyfreed = true; 2280 2281 BO_LOCK(bo); 2282 if (nbp != NULL && 2283 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 2284 (nbp->b_vp != vp) || 2285 (nbp->b_flags & B_DELWRI) == 0)) 2286 return (EAGAIN); 2287 } 2288 } while (anyfreed); 2289 return (0); 2290 } 2291 2292 static void 2293 buf_vlist_remove(struct buf *bp) 2294 { 2295 struct bufv *bv; 2296 b_xflags_t flags; 2297 2298 flags = bp->b_xflags; 2299 2300 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 2301 ASSERT_BO_WLOCKED(bp->b_bufobj); 2302 KASSERT((flags & (BX_VNDIRTY | BX_VNCLEAN)) != 0 && 2303 (flags & (BX_VNDIRTY | BX_VNCLEAN)) != (BX_VNDIRTY | BX_VNCLEAN), 2304 ("%s: buffer %p has invalid queue state", __func__, bp)); 2305 2306 if ((flags & BX_VNDIRTY) != 0) 2307 bv = &bp->b_bufobj->bo_dirty; 2308 else 2309 bv = &bp->b_bufobj->bo_clean; 2310 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno); 2311 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 2312 bv->bv_cnt--; 2313 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 2314 } 2315 2316 /* 2317 * Add the buffer to the sorted clean or dirty block list. 2318 * 2319 * NOTE: xflags is passed as a constant, optimizing this inline function! 2320 */ 2321 static void 2322 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 2323 { 2324 struct bufv *bv; 2325 struct buf *n; 2326 int error; 2327 2328 ASSERT_BO_WLOCKED(bo); 2329 KASSERT((bo->bo_flag & BO_NOBUFS) == 0, 2330 ("buf_vlist_add: bo %p does not allow bufs", bo)); 2331 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0, 2332 ("dead bo %p", bo)); 2333 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 2334 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 2335 bp->b_xflags |= xflags; 2336 if (xflags & BX_VNDIRTY) 2337 bv = &bo->bo_dirty; 2338 else 2339 bv = &bo->bo_clean; 2340 2341 /* 2342 * Keep the list ordered. Optimize empty list insertion. Assume 2343 * we tend to grow at the tail so lookup_le should usually be cheaper 2344 * than _ge. 2345 */ 2346 if (bv->bv_cnt == 0 || 2347 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno) 2348 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 2349 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL) 2350 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs); 2351 else 2352 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs); 2353 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp); 2354 if (error) 2355 panic("buf_vlist_add: Preallocated nodes insufficient."); 2356 bv->bv_cnt++; 2357 } 2358 2359 /* 2360 * Look up a buffer using the buffer tries. 2361 */ 2362 struct buf * 2363 gbincore(struct bufobj *bo, daddr_t lblkno) 2364 { 2365 struct buf *bp; 2366 2367 ASSERT_BO_LOCKED(bo); 2368 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno); 2369 if (bp != NULL) 2370 return (bp); 2371 return (BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno)); 2372 } 2373 2374 /* 2375 * Look up a buf using the buffer tries, without the bufobj lock. This relies 2376 * on SMR for safe lookup, and bufs being in a no-free zone to provide type 2377 * stability of the result. Like other lockless lookups, the found buf may 2378 * already be invalid by the time this function returns. 2379 */ 2380 struct buf * 2381 gbincore_unlocked(struct bufobj *bo, daddr_t lblkno) 2382 { 2383 struct buf *bp; 2384 2385 ASSERT_BO_UNLOCKED(bo); 2386 bp = BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_clean.bv_root, lblkno); 2387 if (bp != NULL) 2388 return (bp); 2389 return (BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_dirty.bv_root, lblkno)); 2390 } 2391 2392 /* 2393 * Associate a buffer with a vnode. 2394 */ 2395 void 2396 bgetvp(struct vnode *vp, struct buf *bp) 2397 { 2398 struct bufobj *bo; 2399 2400 bo = &vp->v_bufobj; 2401 ASSERT_BO_WLOCKED(bo); 2402 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 2403 2404 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 2405 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 2406 ("bgetvp: bp already attached! %p", bp)); 2407 2408 vhold(vp); 2409 bp->b_vp = vp; 2410 bp->b_bufobj = bo; 2411 /* 2412 * Insert onto list for new vnode. 2413 */ 2414 buf_vlist_add(bp, bo, BX_VNCLEAN); 2415 } 2416 2417 /* 2418 * Disassociate a buffer from a vnode. 2419 */ 2420 void 2421 brelvp(struct buf *bp) 2422 { 2423 struct bufobj *bo; 2424 struct vnode *vp; 2425 2426 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 2427 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 2428 2429 /* 2430 * Delete from old vnode list, if on one. 2431 */ 2432 vp = bp->b_vp; /* XXX */ 2433 bo = bp->b_bufobj; 2434 BO_LOCK(bo); 2435 buf_vlist_remove(bp); 2436 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2437 bo->bo_flag &= ~BO_ONWORKLST; 2438 mtx_lock(&sync_mtx); 2439 LIST_REMOVE(bo, bo_synclist); 2440 syncer_worklist_len--; 2441 mtx_unlock(&sync_mtx); 2442 } 2443 bp->b_vp = NULL; 2444 bp->b_bufobj = NULL; 2445 BO_UNLOCK(bo); 2446 vdrop(vp); 2447 } 2448 2449 /* 2450 * Add an item to the syncer work queue. 2451 */ 2452 static void 2453 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 2454 { 2455 int slot; 2456 2457 ASSERT_BO_WLOCKED(bo); 2458 2459 mtx_lock(&sync_mtx); 2460 if (bo->bo_flag & BO_ONWORKLST) 2461 LIST_REMOVE(bo, bo_synclist); 2462 else { 2463 bo->bo_flag |= BO_ONWORKLST; 2464 syncer_worklist_len++; 2465 } 2466 2467 if (delay > syncer_maxdelay - 2) 2468 delay = syncer_maxdelay - 2; 2469 slot = (syncer_delayno + delay) & syncer_mask; 2470 2471 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist); 2472 mtx_unlock(&sync_mtx); 2473 } 2474 2475 static int 2476 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 2477 { 2478 int error, len; 2479 2480 mtx_lock(&sync_mtx); 2481 len = syncer_worklist_len - sync_vnode_count; 2482 mtx_unlock(&sync_mtx); 2483 error = SYSCTL_OUT(req, &len, sizeof(len)); 2484 return (error); 2485 } 2486 2487 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, 2488 CTLTYPE_INT | CTLFLAG_MPSAFE| CTLFLAG_RD, NULL, 0, 2489 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 2490 2491 static struct proc *updateproc; 2492 static void sched_sync(void); 2493 static struct kproc_desc up_kp = { 2494 "syncer", 2495 sched_sync, 2496 &updateproc 2497 }; 2498 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 2499 2500 static int 2501 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 2502 { 2503 struct vnode *vp; 2504 struct mount *mp; 2505 2506 *bo = LIST_FIRST(slp); 2507 if (*bo == NULL) 2508 return (0); 2509 vp = bo2vnode(*bo); 2510 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 2511 return (1); 2512 /* 2513 * We use vhold in case the vnode does not 2514 * successfully sync. vhold prevents the vnode from 2515 * going away when we unlock the sync_mtx so that 2516 * we can acquire the vnode interlock. 2517 */ 2518 vholdl(vp); 2519 mtx_unlock(&sync_mtx); 2520 VI_UNLOCK(vp); 2521 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2522 vdrop(vp); 2523 mtx_lock(&sync_mtx); 2524 return (*bo == LIST_FIRST(slp)); 2525 } 2526 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2527 (void) VOP_FSYNC(vp, MNT_LAZY, td); 2528 VOP_UNLOCK(vp); 2529 vn_finished_write(mp); 2530 BO_LOCK(*bo); 2531 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 2532 /* 2533 * Put us back on the worklist. The worklist 2534 * routine will remove us from our current 2535 * position and then add us back in at a later 2536 * position. 2537 */ 2538 vn_syncer_add_to_worklist(*bo, syncdelay); 2539 } 2540 BO_UNLOCK(*bo); 2541 vdrop(vp); 2542 mtx_lock(&sync_mtx); 2543 return (0); 2544 } 2545 2546 static int first_printf = 1; 2547 2548 /* 2549 * System filesystem synchronizer daemon. 2550 */ 2551 static void 2552 sched_sync(void) 2553 { 2554 struct synclist *next, *slp; 2555 struct bufobj *bo; 2556 long starttime; 2557 struct thread *td = curthread; 2558 int last_work_seen; 2559 int net_worklist_len; 2560 int syncer_final_iter; 2561 int error; 2562 2563 last_work_seen = 0; 2564 syncer_final_iter = 0; 2565 syncer_state = SYNCER_RUNNING; 2566 starttime = time_uptime; 2567 td->td_pflags |= TDP_NORUNNINGBUF; 2568 2569 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 2570 SHUTDOWN_PRI_LAST); 2571 2572 mtx_lock(&sync_mtx); 2573 for (;;) { 2574 if (syncer_state == SYNCER_FINAL_DELAY && 2575 syncer_final_iter == 0) { 2576 mtx_unlock(&sync_mtx); 2577 kproc_suspend_check(td->td_proc); 2578 mtx_lock(&sync_mtx); 2579 } 2580 net_worklist_len = syncer_worklist_len - sync_vnode_count; 2581 if (syncer_state != SYNCER_RUNNING && 2582 starttime != time_uptime) { 2583 if (first_printf) { 2584 printf("\nSyncing disks, vnodes remaining... "); 2585 first_printf = 0; 2586 } 2587 printf("%d ", net_worklist_len); 2588 } 2589 starttime = time_uptime; 2590 2591 /* 2592 * Push files whose dirty time has expired. Be careful 2593 * of interrupt race on slp queue. 2594 * 2595 * Skip over empty worklist slots when shutting down. 2596 */ 2597 do { 2598 slp = &syncer_workitem_pending[syncer_delayno]; 2599 syncer_delayno += 1; 2600 if (syncer_delayno == syncer_maxdelay) 2601 syncer_delayno = 0; 2602 next = &syncer_workitem_pending[syncer_delayno]; 2603 /* 2604 * If the worklist has wrapped since the 2605 * it was emptied of all but syncer vnodes, 2606 * switch to the FINAL_DELAY state and run 2607 * for one more second. 2608 */ 2609 if (syncer_state == SYNCER_SHUTTING_DOWN && 2610 net_worklist_len == 0 && 2611 last_work_seen == syncer_delayno) { 2612 syncer_state = SYNCER_FINAL_DELAY; 2613 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 2614 } 2615 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 2616 syncer_worklist_len > 0); 2617 2618 /* 2619 * Keep track of the last time there was anything 2620 * on the worklist other than syncer vnodes. 2621 * Return to the SHUTTING_DOWN state if any 2622 * new work appears. 2623 */ 2624 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 2625 last_work_seen = syncer_delayno; 2626 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 2627 syncer_state = SYNCER_SHUTTING_DOWN; 2628 while (!LIST_EMPTY(slp)) { 2629 error = sync_vnode(slp, &bo, td); 2630 if (error == 1) { 2631 LIST_REMOVE(bo, bo_synclist); 2632 LIST_INSERT_HEAD(next, bo, bo_synclist); 2633 continue; 2634 } 2635 2636 if (first_printf == 0) { 2637 /* 2638 * Drop the sync mutex, because some watchdog 2639 * drivers need to sleep while patting 2640 */ 2641 mtx_unlock(&sync_mtx); 2642 wdog_kern_pat(WD_LASTVAL); 2643 mtx_lock(&sync_mtx); 2644 } 2645 } 2646 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 2647 syncer_final_iter--; 2648 /* 2649 * The variable rushjob allows the kernel to speed up the 2650 * processing of the filesystem syncer process. A rushjob 2651 * value of N tells the filesystem syncer to process the next 2652 * N seconds worth of work on its queue ASAP. Currently rushjob 2653 * is used by the soft update code to speed up the filesystem 2654 * syncer process when the incore state is getting so far 2655 * ahead of the disk that the kernel memory pool is being 2656 * threatened with exhaustion. 2657 */ 2658 if (rushjob > 0) { 2659 rushjob -= 1; 2660 continue; 2661 } 2662 /* 2663 * Just sleep for a short period of time between 2664 * iterations when shutting down to allow some I/O 2665 * to happen. 2666 * 2667 * If it has taken us less than a second to process the 2668 * current work, then wait. Otherwise start right over 2669 * again. We can still lose time if any single round 2670 * takes more than two seconds, but it does not really 2671 * matter as we are just trying to generally pace the 2672 * filesystem activity. 2673 */ 2674 if (syncer_state != SYNCER_RUNNING || 2675 time_uptime == starttime) { 2676 thread_lock(td); 2677 sched_prio(td, PPAUSE); 2678 thread_unlock(td); 2679 } 2680 if (syncer_state != SYNCER_RUNNING) 2681 cv_timedwait(&sync_wakeup, &sync_mtx, 2682 hz / SYNCER_SHUTDOWN_SPEEDUP); 2683 else if (time_uptime == starttime) 2684 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 2685 } 2686 } 2687 2688 /* 2689 * Request the syncer daemon to speed up its work. 2690 * We never push it to speed up more than half of its 2691 * normal turn time, otherwise it could take over the cpu. 2692 */ 2693 int 2694 speedup_syncer(void) 2695 { 2696 int ret = 0; 2697 2698 mtx_lock(&sync_mtx); 2699 if (rushjob < syncdelay / 2) { 2700 rushjob += 1; 2701 stat_rush_requests += 1; 2702 ret = 1; 2703 } 2704 mtx_unlock(&sync_mtx); 2705 cv_broadcast(&sync_wakeup); 2706 return (ret); 2707 } 2708 2709 /* 2710 * Tell the syncer to speed up its work and run though its work 2711 * list several times, then tell it to shut down. 2712 */ 2713 static void 2714 syncer_shutdown(void *arg, int howto) 2715 { 2716 2717 if (howto & RB_NOSYNC) 2718 return; 2719 mtx_lock(&sync_mtx); 2720 syncer_state = SYNCER_SHUTTING_DOWN; 2721 rushjob = 0; 2722 mtx_unlock(&sync_mtx); 2723 cv_broadcast(&sync_wakeup); 2724 kproc_shutdown(arg, howto); 2725 } 2726 2727 void 2728 syncer_suspend(void) 2729 { 2730 2731 syncer_shutdown(updateproc, 0); 2732 } 2733 2734 void 2735 syncer_resume(void) 2736 { 2737 2738 mtx_lock(&sync_mtx); 2739 first_printf = 1; 2740 syncer_state = SYNCER_RUNNING; 2741 mtx_unlock(&sync_mtx); 2742 cv_broadcast(&sync_wakeup); 2743 kproc_resume(updateproc); 2744 } 2745 2746 /* 2747 * Move the buffer between the clean and dirty lists of its vnode. 2748 */ 2749 void 2750 reassignbuf(struct buf *bp) 2751 { 2752 struct vnode *vp; 2753 struct bufobj *bo; 2754 int delay; 2755 #ifdef INVARIANTS 2756 struct bufv *bv; 2757 #endif 2758 2759 vp = bp->b_vp; 2760 bo = bp->b_bufobj; 2761 2762 KASSERT((bp->b_flags & B_PAGING) == 0, 2763 ("%s: cannot reassign paging buffer %p", __func__, bp)); 2764 2765 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 2766 bp, bp->b_vp, bp->b_flags); 2767 2768 BO_LOCK(bo); 2769 buf_vlist_remove(bp); 2770 2771 /* 2772 * If dirty, put on list of dirty buffers; otherwise insert onto list 2773 * of clean buffers. 2774 */ 2775 if (bp->b_flags & B_DELWRI) { 2776 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 2777 switch (vp->v_type) { 2778 case VDIR: 2779 delay = dirdelay; 2780 break; 2781 case VCHR: 2782 delay = metadelay; 2783 break; 2784 default: 2785 delay = filedelay; 2786 } 2787 vn_syncer_add_to_worklist(bo, delay); 2788 } 2789 buf_vlist_add(bp, bo, BX_VNDIRTY); 2790 } else { 2791 buf_vlist_add(bp, bo, BX_VNCLEAN); 2792 2793 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2794 mtx_lock(&sync_mtx); 2795 LIST_REMOVE(bo, bo_synclist); 2796 syncer_worklist_len--; 2797 mtx_unlock(&sync_mtx); 2798 bo->bo_flag &= ~BO_ONWORKLST; 2799 } 2800 } 2801 #ifdef INVARIANTS 2802 bv = &bo->bo_clean; 2803 bp = TAILQ_FIRST(&bv->bv_hd); 2804 KASSERT(bp == NULL || bp->b_bufobj == bo, 2805 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2806 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2807 KASSERT(bp == NULL || bp->b_bufobj == bo, 2808 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2809 bv = &bo->bo_dirty; 2810 bp = TAILQ_FIRST(&bv->bv_hd); 2811 KASSERT(bp == NULL || bp->b_bufobj == bo, 2812 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2813 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2814 KASSERT(bp == NULL || bp->b_bufobj == bo, 2815 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2816 #endif 2817 BO_UNLOCK(bo); 2818 } 2819 2820 static void 2821 v_init_counters(struct vnode *vp) 2822 { 2823 2824 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0, 2825 vp, ("%s called for an initialized vnode", __FUNCTION__)); 2826 ASSERT_VI_UNLOCKED(vp, __FUNCTION__); 2827 2828 refcount_init(&vp->v_holdcnt, 1); 2829 refcount_init(&vp->v_usecount, 1); 2830 } 2831 2832 /* 2833 * Grab a particular vnode from the free list, increment its 2834 * reference count and lock it. VIRF_DOOMED is set if the vnode 2835 * is being destroyed. Only callers who specify LK_RETRY will 2836 * see doomed vnodes. If inactive processing was delayed in 2837 * vput try to do it here. 2838 * 2839 * usecount is manipulated using atomics without holding any locks. 2840 * 2841 * holdcnt can be manipulated using atomics without holding any locks, 2842 * except when transitioning 1<->0, in which case the interlock is held. 2843 * 2844 * Consumers which don't guarantee liveness of the vnode can use SMR to 2845 * try to get a reference. Note this operation can fail since the vnode 2846 * may be awaiting getting freed by the time they get to it. 2847 */ 2848 enum vgetstate 2849 vget_prep_smr(struct vnode *vp) 2850 { 2851 enum vgetstate vs; 2852 2853 VFS_SMR_ASSERT_ENTERED(); 2854 2855 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2856 vs = VGET_USECOUNT; 2857 } else { 2858 if (vhold_smr(vp)) 2859 vs = VGET_HOLDCNT; 2860 else 2861 vs = VGET_NONE; 2862 } 2863 return (vs); 2864 } 2865 2866 enum vgetstate 2867 vget_prep(struct vnode *vp) 2868 { 2869 enum vgetstate vs; 2870 2871 if (refcount_acquire_if_not_zero(&vp->v_usecount)) { 2872 vs = VGET_USECOUNT; 2873 } else { 2874 vhold(vp); 2875 vs = VGET_HOLDCNT; 2876 } 2877 return (vs); 2878 } 2879 2880 void 2881 vget_abort(struct vnode *vp, enum vgetstate vs) 2882 { 2883 2884 switch (vs) { 2885 case VGET_USECOUNT: 2886 vrele(vp); 2887 break; 2888 case VGET_HOLDCNT: 2889 vdrop(vp); 2890 break; 2891 default: 2892 __assert_unreachable(); 2893 } 2894 } 2895 2896 int 2897 vget(struct vnode *vp, int flags) 2898 { 2899 enum vgetstate vs; 2900 2901 vs = vget_prep(vp); 2902 return (vget_finish(vp, flags, vs)); 2903 } 2904 2905 int 2906 vget_finish(struct vnode *vp, int flags, enum vgetstate vs) 2907 { 2908 int error; 2909 2910 if ((flags & LK_INTERLOCK) != 0) 2911 ASSERT_VI_LOCKED(vp, __func__); 2912 else 2913 ASSERT_VI_UNLOCKED(vp, __func__); 2914 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp); 2915 VNPASS(vp->v_holdcnt > 0, vp); 2916 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp); 2917 2918 error = vn_lock(vp, flags); 2919 if (__predict_false(error != 0)) { 2920 vget_abort(vp, vs); 2921 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2922 vp); 2923 return (error); 2924 } 2925 2926 vget_finish_ref(vp, vs); 2927 return (0); 2928 } 2929 2930 void 2931 vget_finish_ref(struct vnode *vp, enum vgetstate vs) 2932 { 2933 int old; 2934 2935 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp); 2936 VNPASS(vp->v_holdcnt > 0, vp); 2937 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp); 2938 2939 if (vs == VGET_USECOUNT) 2940 return; 2941 2942 /* 2943 * We hold the vnode. If the usecount is 0 it will be utilized to keep 2944 * the vnode around. Otherwise someone else lended their hold count and 2945 * we have to drop ours. 2946 */ 2947 old = atomic_fetchadd_int(&vp->v_usecount, 1); 2948 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old)); 2949 if (old != 0) { 2950 #ifdef INVARIANTS 2951 old = atomic_fetchadd_int(&vp->v_holdcnt, -1); 2952 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old)); 2953 #else 2954 refcount_release(&vp->v_holdcnt); 2955 #endif 2956 } 2957 } 2958 2959 void 2960 vref(struct vnode *vp) 2961 { 2962 enum vgetstate vs; 2963 2964 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2965 vs = vget_prep(vp); 2966 vget_finish_ref(vp, vs); 2967 } 2968 2969 void 2970 vrefact(struct vnode *vp) 2971 { 2972 2973 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2974 #ifdef INVARIANTS 2975 int old = atomic_fetchadd_int(&vp->v_usecount, 1); 2976 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old)); 2977 #else 2978 refcount_acquire(&vp->v_usecount); 2979 #endif 2980 } 2981 2982 void 2983 vlazy(struct vnode *vp) 2984 { 2985 struct mount *mp; 2986 2987 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__)); 2988 2989 if ((vp->v_mflag & VMP_LAZYLIST) != 0) 2990 return; 2991 /* 2992 * We may get here for inactive routines after the vnode got doomed. 2993 */ 2994 if (VN_IS_DOOMED(vp)) 2995 return; 2996 mp = vp->v_mount; 2997 mtx_lock(&mp->mnt_listmtx); 2998 if ((vp->v_mflag & VMP_LAZYLIST) == 0) { 2999 vp->v_mflag |= VMP_LAZYLIST; 3000 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist); 3001 mp->mnt_lazyvnodelistsize++; 3002 } 3003 mtx_unlock(&mp->mnt_listmtx); 3004 } 3005 3006 /* 3007 * This routine is only meant to be called from vgonel prior to dooming 3008 * the vnode. 3009 */ 3010 static void 3011 vunlazy_gone(struct vnode *vp) 3012 { 3013 struct mount *mp; 3014 3015 ASSERT_VOP_ELOCKED(vp, __func__); 3016 ASSERT_VI_LOCKED(vp, __func__); 3017 VNPASS(!VN_IS_DOOMED(vp), vp); 3018 3019 if (vp->v_mflag & VMP_LAZYLIST) { 3020 mp = vp->v_mount; 3021 mtx_lock(&mp->mnt_listmtx); 3022 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp); 3023 vp->v_mflag &= ~VMP_LAZYLIST; 3024 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist); 3025 mp->mnt_lazyvnodelistsize--; 3026 mtx_unlock(&mp->mnt_listmtx); 3027 } 3028 } 3029 3030 static void 3031 vdefer_inactive(struct vnode *vp) 3032 { 3033 3034 ASSERT_VI_LOCKED(vp, __func__); 3035 VNASSERT(vp->v_holdcnt > 0, vp, 3036 ("%s: vnode without hold count", __func__)); 3037 if (VN_IS_DOOMED(vp)) { 3038 vdropl(vp); 3039 return; 3040 } 3041 if (vp->v_iflag & VI_DEFINACT) { 3042 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count")); 3043 vdropl(vp); 3044 return; 3045 } 3046 if (vp->v_usecount > 0) { 3047 vp->v_iflag &= ~VI_OWEINACT; 3048 vdropl(vp); 3049 return; 3050 } 3051 vlazy(vp); 3052 vp->v_iflag |= VI_DEFINACT; 3053 VI_UNLOCK(vp); 3054 counter_u64_add(deferred_inact, 1); 3055 } 3056 3057 static void 3058 vdefer_inactive_unlocked(struct vnode *vp) 3059 { 3060 3061 VI_LOCK(vp); 3062 if ((vp->v_iflag & VI_OWEINACT) == 0) { 3063 vdropl(vp); 3064 return; 3065 } 3066 vdefer_inactive(vp); 3067 } 3068 3069 enum vput_op { VRELE, VPUT, VUNREF }; 3070 3071 /* 3072 * Handle ->v_usecount transitioning to 0. 3073 * 3074 * By releasing the last usecount we take ownership of the hold count which 3075 * provides liveness of the vnode, meaning we have to vdrop. 3076 * 3077 * For all vnodes we may need to perform inactive processing. It requires an 3078 * exclusive lock on the vnode, while it is legal to call here with only a 3079 * shared lock (or no locks). If locking the vnode in an expected manner fails, 3080 * inactive processing gets deferred to the syncer. 3081 * 3082 * XXX Some filesystems pass in an exclusively locked vnode and strongly depend 3083 * on the lock being held all the way until VOP_INACTIVE. This in particular 3084 * happens with UFS which adds half-constructed vnodes to the hash, where they 3085 * can be found by other code. 3086 */ 3087 static void 3088 vput_final(struct vnode *vp, enum vput_op func) 3089 { 3090 int error; 3091 bool want_unlock; 3092 3093 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3094 VNPASS(vp->v_holdcnt > 0, vp); 3095 3096 VI_LOCK(vp); 3097 3098 /* 3099 * By the time we got here someone else might have transitioned 3100 * the count back to > 0. 3101 */ 3102 if (vp->v_usecount > 0) 3103 goto out; 3104 3105 /* 3106 * If the vnode is doomed vgone already performed inactive processing 3107 * (if needed). 3108 */ 3109 if (VN_IS_DOOMED(vp)) 3110 goto out; 3111 3112 if (__predict_true(VOP_NEED_INACTIVE(vp) == 0)) 3113 goto out; 3114 3115 if (vp->v_iflag & VI_DOINGINACT) 3116 goto out; 3117 3118 /* 3119 * Locking operations here will drop the interlock and possibly the 3120 * vnode lock, opening a window where the vnode can get doomed all the 3121 * while ->v_usecount is 0. Set VI_OWEINACT to let vgone know to 3122 * perform inactive. 3123 */ 3124 vp->v_iflag |= VI_OWEINACT; 3125 want_unlock = false; 3126 error = 0; 3127 switch (func) { 3128 case VRELE: 3129 switch (VOP_ISLOCKED(vp)) { 3130 case LK_EXCLUSIVE: 3131 break; 3132 case LK_EXCLOTHER: 3133 case 0: 3134 want_unlock = true; 3135 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 3136 VI_LOCK(vp); 3137 break; 3138 default: 3139 /* 3140 * The lock has at least one sharer, but we have no way 3141 * to conclude whether this is us. Play it safe and 3142 * defer processing. 3143 */ 3144 error = EAGAIN; 3145 break; 3146 } 3147 break; 3148 case VPUT: 3149 want_unlock = true; 3150 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 3151 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK | 3152 LK_NOWAIT); 3153 VI_LOCK(vp); 3154 } 3155 break; 3156 case VUNREF: 3157 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 3158 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK); 3159 VI_LOCK(vp); 3160 } 3161 break; 3162 } 3163 if (error == 0) { 3164 vinactive(vp); 3165 if (want_unlock) 3166 VOP_UNLOCK(vp); 3167 vdropl(vp); 3168 } else { 3169 vdefer_inactive(vp); 3170 } 3171 return; 3172 out: 3173 if (func == VPUT) 3174 VOP_UNLOCK(vp); 3175 vdropl(vp); 3176 } 3177 3178 /* 3179 * Decrement ->v_usecount for a vnode. 3180 * 3181 * Releasing the last use count requires additional processing, see vput_final 3182 * above for details. 3183 * 3184 * Comment above each variant denotes lock state on entry and exit. 3185 */ 3186 3187 /* 3188 * in: any 3189 * out: same as passed in 3190 */ 3191 void 3192 vrele(struct vnode *vp) 3193 { 3194 3195 ASSERT_VI_UNLOCKED(vp, __func__); 3196 if (!refcount_release(&vp->v_usecount)) 3197 return; 3198 vput_final(vp, VRELE); 3199 } 3200 3201 /* 3202 * in: locked 3203 * out: unlocked 3204 */ 3205 void 3206 vput(struct vnode *vp) 3207 { 3208 3209 ASSERT_VOP_LOCKED(vp, __func__); 3210 ASSERT_VI_UNLOCKED(vp, __func__); 3211 if (!refcount_release(&vp->v_usecount)) { 3212 VOP_UNLOCK(vp); 3213 return; 3214 } 3215 vput_final(vp, VPUT); 3216 } 3217 3218 /* 3219 * in: locked 3220 * out: locked 3221 */ 3222 void 3223 vunref(struct vnode *vp) 3224 { 3225 3226 ASSERT_VOP_LOCKED(vp, __func__); 3227 ASSERT_VI_UNLOCKED(vp, __func__); 3228 if (!refcount_release(&vp->v_usecount)) 3229 return; 3230 vput_final(vp, VUNREF); 3231 } 3232 3233 void 3234 vhold(struct vnode *vp) 3235 { 3236 int old; 3237 3238 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3239 old = atomic_fetchadd_int(&vp->v_holdcnt, 1); 3240 VNASSERT(old >= 0 && (old & VHOLD_ALL_FLAGS) == 0, vp, 3241 ("%s: wrong hold count %d", __func__, old)); 3242 if (old == 0) 3243 vn_freevnodes_dec(); 3244 } 3245 3246 void 3247 vholdnz(struct vnode *vp) 3248 { 3249 3250 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3251 #ifdef INVARIANTS 3252 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1); 3253 VNASSERT(old > 0 && (old & VHOLD_ALL_FLAGS) == 0, vp, 3254 ("%s: wrong hold count %d", __func__, old)); 3255 #else 3256 atomic_add_int(&vp->v_holdcnt, 1); 3257 #endif 3258 } 3259 3260 /* 3261 * Grab a hold count unless the vnode is freed. 3262 * 3263 * Only use this routine if vfs smr is the only protection you have against 3264 * freeing the vnode. 3265 * 3266 * The code loops trying to add a hold count as long as the VHOLD_NO_SMR flag 3267 * is not set. After the flag is set the vnode becomes immutable to anyone but 3268 * the thread which managed to set the flag. 3269 * 3270 * It may be tempting to replace the loop with: 3271 * count = atomic_fetchadd_int(&vp->v_holdcnt, 1); 3272 * if (count & VHOLD_NO_SMR) { 3273 * backpedal and error out; 3274 * } 3275 * 3276 * However, while this is more performant, it hinders debugging by eliminating 3277 * the previously mentioned invariant. 3278 */ 3279 bool 3280 vhold_smr(struct vnode *vp) 3281 { 3282 int count; 3283 3284 VFS_SMR_ASSERT_ENTERED(); 3285 3286 count = atomic_load_int(&vp->v_holdcnt); 3287 for (;;) { 3288 if (count & VHOLD_NO_SMR) { 3289 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp, 3290 ("non-zero hold count with flags %d\n", count)); 3291 return (false); 3292 } 3293 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count)); 3294 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1)) { 3295 if (count == 0) 3296 vn_freevnodes_dec(); 3297 return (true); 3298 } 3299 } 3300 } 3301 3302 /* 3303 * Hold a free vnode for recycling. 3304 * 3305 * Note: vnode_init references this comment. 3306 * 3307 * Attempts to recycle only need the global vnode list lock and have no use for 3308 * SMR. 3309 * 3310 * However, vnodes get inserted into the global list before they get fully 3311 * initialized and stay there until UMA decides to free the memory. This in 3312 * particular means the target can be found before it becomes usable and after 3313 * it becomes recycled. Picking up such vnodes is guarded with v_holdcnt set to 3314 * VHOLD_NO_SMR. 3315 * 3316 * Note: the vnode may gain more references after we transition the count 0->1. 3317 */ 3318 static bool 3319 vhold_recycle_free(struct vnode *vp) 3320 { 3321 int count; 3322 3323 mtx_assert(&vnode_list_mtx, MA_OWNED); 3324 3325 count = atomic_load_int(&vp->v_holdcnt); 3326 for (;;) { 3327 if (count & VHOLD_NO_SMR) { 3328 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp, 3329 ("non-zero hold count with flags %d\n", count)); 3330 return (false); 3331 } 3332 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count)); 3333 if (count > 0) { 3334 return (false); 3335 } 3336 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1)) { 3337 vn_freevnodes_dec(); 3338 return (true); 3339 } 3340 } 3341 } 3342 3343 static void __noinline 3344 vdbatch_process(struct vdbatch *vd) 3345 { 3346 struct vnode *vp; 3347 int i; 3348 3349 mtx_assert(&vd->lock, MA_OWNED); 3350 MPASS(curthread->td_pinned > 0); 3351 MPASS(vd->index == VDBATCH_SIZE); 3352 3353 mtx_lock(&vnode_list_mtx); 3354 critical_enter(); 3355 freevnodes += vd->freevnodes; 3356 for (i = 0; i < VDBATCH_SIZE; i++) { 3357 vp = vd->tab[i]; 3358 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist); 3359 TAILQ_INSERT_TAIL(&vnode_list, vp, v_vnodelist); 3360 MPASS(vp->v_dbatchcpu != NOCPU); 3361 vp->v_dbatchcpu = NOCPU; 3362 } 3363 mtx_unlock(&vnode_list_mtx); 3364 vd->freevnodes = 0; 3365 bzero(vd->tab, sizeof(vd->tab)); 3366 vd->index = 0; 3367 critical_exit(); 3368 } 3369 3370 static void 3371 vdbatch_enqueue(struct vnode *vp) 3372 { 3373 struct vdbatch *vd; 3374 3375 ASSERT_VI_LOCKED(vp, __func__); 3376 VNASSERT(!VN_IS_DOOMED(vp), vp, 3377 ("%s: deferring requeue of a doomed vnode", __func__)); 3378 3379 if (vp->v_dbatchcpu != NOCPU) { 3380 VI_UNLOCK(vp); 3381 return; 3382 } 3383 3384 sched_pin(); 3385 vd = DPCPU_PTR(vd); 3386 mtx_lock(&vd->lock); 3387 MPASS(vd->index < VDBATCH_SIZE); 3388 MPASS(vd->tab[vd->index] == NULL); 3389 /* 3390 * A hack: we depend on being pinned so that we know what to put in 3391 * ->v_dbatchcpu. 3392 */ 3393 vp->v_dbatchcpu = curcpu; 3394 vd->tab[vd->index] = vp; 3395 vd->index++; 3396 VI_UNLOCK(vp); 3397 if (vd->index == VDBATCH_SIZE) 3398 vdbatch_process(vd); 3399 mtx_unlock(&vd->lock); 3400 sched_unpin(); 3401 } 3402 3403 /* 3404 * This routine must only be called for vnodes which are about to be 3405 * deallocated. Supporting dequeue for arbitrary vndoes would require 3406 * validating that the locked batch matches. 3407 */ 3408 static void 3409 vdbatch_dequeue(struct vnode *vp) 3410 { 3411 struct vdbatch *vd; 3412 int i; 3413 short cpu; 3414 3415 VNASSERT(vp->v_type == VBAD || vp->v_type == VNON, vp, 3416 ("%s: called for a used vnode\n", __func__)); 3417 3418 cpu = vp->v_dbatchcpu; 3419 if (cpu == NOCPU) 3420 return; 3421 3422 vd = DPCPU_ID_PTR(cpu, vd); 3423 mtx_lock(&vd->lock); 3424 for (i = 0; i < vd->index; i++) { 3425 if (vd->tab[i] != vp) 3426 continue; 3427 vp->v_dbatchcpu = NOCPU; 3428 vd->index--; 3429 vd->tab[i] = vd->tab[vd->index]; 3430 vd->tab[vd->index] = NULL; 3431 break; 3432 } 3433 mtx_unlock(&vd->lock); 3434 /* 3435 * Either we dequeued the vnode above or the target CPU beat us to it. 3436 */ 3437 MPASS(vp->v_dbatchcpu == NOCPU); 3438 } 3439 3440 /* 3441 * Drop the hold count of the vnode. If this is the last reference to 3442 * the vnode we place it on the free list unless it has been vgone'd 3443 * (marked VIRF_DOOMED) in which case we will free it. 3444 * 3445 * Because the vnode vm object keeps a hold reference on the vnode if 3446 * there is at least one resident non-cached page, the vnode cannot 3447 * leave the active list without the page cleanup done. 3448 */ 3449 static void 3450 vdrop_deactivate(struct vnode *vp) 3451 { 3452 struct mount *mp; 3453 3454 ASSERT_VI_LOCKED(vp, __func__); 3455 /* 3456 * Mark a vnode as free: remove it from its active list 3457 * and put it up for recycling on the freelist. 3458 */ 3459 VNASSERT(!VN_IS_DOOMED(vp), vp, 3460 ("vdrop: returning doomed vnode")); 3461 VNASSERT(vp->v_op != NULL, vp, 3462 ("vdrop: vnode already reclaimed.")); 3463 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp, 3464 ("vnode with VI_OWEINACT set")); 3465 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, 3466 ("vnode with VI_DEFINACT set")); 3467 if (vp->v_mflag & VMP_LAZYLIST) { 3468 mp = vp->v_mount; 3469 mtx_lock(&mp->mnt_listmtx); 3470 VNASSERT(vp->v_mflag & VMP_LAZYLIST, vp, ("lost VMP_LAZYLIST")); 3471 /* 3472 * Don't remove the vnode from the lazy list if another thread 3473 * has increased the hold count. It may have re-enqueued the 3474 * vnode to the lazy list and is now responsible for its 3475 * removal. 3476 */ 3477 if (vp->v_holdcnt == 0) { 3478 vp->v_mflag &= ~VMP_LAZYLIST; 3479 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist); 3480 mp->mnt_lazyvnodelistsize--; 3481 } 3482 mtx_unlock(&mp->mnt_listmtx); 3483 } 3484 vdbatch_enqueue(vp); 3485 } 3486 3487 static void __noinline 3488 vdropl_final(struct vnode *vp) 3489 { 3490 3491 ASSERT_VI_LOCKED(vp, __func__); 3492 VNPASS(VN_IS_DOOMED(vp), vp); 3493 /* 3494 * Set the VHOLD_NO_SMR flag. 3495 * 3496 * We may be racing against vhold_smr. If they win we can just pretend 3497 * we never got this far, they will vdrop later. 3498 */ 3499 if (__predict_false(!atomic_cmpset_int(&vp->v_holdcnt, 0, VHOLD_NO_SMR))) { 3500 vn_freevnodes_inc(); 3501 VI_UNLOCK(vp); 3502 /* 3503 * We lost the aforementioned race. Any subsequent access is 3504 * invalid as they might have managed to vdropl on their own. 3505 */ 3506 return; 3507 } 3508 /* 3509 * Don't bump freevnodes as this one is going away. 3510 */ 3511 freevnode(vp); 3512 } 3513 3514 void 3515 vdrop(struct vnode *vp) 3516 { 3517 3518 ASSERT_VI_UNLOCKED(vp, __func__); 3519 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3520 if (refcount_release_if_not_last(&vp->v_holdcnt)) 3521 return; 3522 VI_LOCK(vp); 3523 vdropl(vp); 3524 } 3525 3526 void 3527 vdropl(struct vnode *vp) 3528 { 3529 3530 ASSERT_VI_LOCKED(vp, __func__); 3531 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3532 if (!refcount_release(&vp->v_holdcnt)) { 3533 VI_UNLOCK(vp); 3534 return; 3535 } 3536 if (!VN_IS_DOOMED(vp)) { 3537 vn_freevnodes_inc(); 3538 vdrop_deactivate(vp); 3539 /* 3540 * Also unlocks the interlock. We can't assert on it as we 3541 * released our hold and by now the vnode might have been 3542 * freed. 3543 */ 3544 return; 3545 } 3546 vdropl_final(vp); 3547 } 3548 3549 /* 3550 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 3551 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 3552 */ 3553 static void 3554 vinactivef(struct vnode *vp) 3555 { 3556 struct vm_object *obj; 3557 3558 ASSERT_VOP_ELOCKED(vp, "vinactive"); 3559 ASSERT_VI_LOCKED(vp, "vinactive"); 3560 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 3561 ("vinactive: recursed on VI_DOINGINACT")); 3562 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3563 vp->v_iflag |= VI_DOINGINACT; 3564 vp->v_iflag &= ~VI_OWEINACT; 3565 VI_UNLOCK(vp); 3566 /* 3567 * Before moving off the active list, we must be sure that any 3568 * modified pages are converted into the vnode's dirty 3569 * buffers, since these will no longer be checked once the 3570 * vnode is on the inactive list. 3571 * 3572 * The write-out of the dirty pages is asynchronous. At the 3573 * point that VOP_INACTIVE() is called, there could still be 3574 * pending I/O and dirty pages in the object. 3575 */ 3576 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 3577 vm_object_mightbedirty(obj)) { 3578 VM_OBJECT_WLOCK(obj); 3579 vm_object_page_clean(obj, 0, 0, 0); 3580 VM_OBJECT_WUNLOCK(obj); 3581 } 3582 VOP_INACTIVE(vp); 3583 VI_LOCK(vp); 3584 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 3585 ("vinactive: lost VI_DOINGINACT")); 3586 vp->v_iflag &= ~VI_DOINGINACT; 3587 } 3588 3589 void 3590 vinactive(struct vnode *vp) 3591 { 3592 3593 ASSERT_VOP_ELOCKED(vp, "vinactive"); 3594 ASSERT_VI_LOCKED(vp, "vinactive"); 3595 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3596 3597 if ((vp->v_iflag & VI_OWEINACT) == 0) 3598 return; 3599 if (vp->v_iflag & VI_DOINGINACT) 3600 return; 3601 if (vp->v_usecount > 0) { 3602 vp->v_iflag &= ~VI_OWEINACT; 3603 return; 3604 } 3605 vinactivef(vp); 3606 } 3607 3608 /* 3609 * Remove any vnodes in the vnode table belonging to mount point mp. 3610 * 3611 * If FORCECLOSE is not specified, there should not be any active ones, 3612 * return error if any are found (nb: this is a user error, not a 3613 * system error). If FORCECLOSE is specified, detach any active vnodes 3614 * that are found. 3615 * 3616 * If WRITECLOSE is set, only flush out regular file vnodes open for 3617 * writing. 3618 * 3619 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 3620 * 3621 * `rootrefs' specifies the base reference count for the root vnode 3622 * of this filesystem. The root vnode is considered busy if its 3623 * v_usecount exceeds this value. On a successful return, vflush(, td) 3624 * will call vrele() on the root vnode exactly rootrefs times. 3625 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 3626 * be zero. 3627 */ 3628 #ifdef DIAGNOSTIC 3629 static int busyprt = 0; /* print out busy vnodes */ 3630 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes"); 3631 #endif 3632 3633 int 3634 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td) 3635 { 3636 struct vnode *vp, *mvp, *rootvp = NULL; 3637 struct vattr vattr; 3638 int busy = 0, error; 3639 3640 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 3641 rootrefs, flags); 3642 if (rootrefs > 0) { 3643 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 3644 ("vflush: bad args")); 3645 /* 3646 * Get the filesystem root vnode. We can vput() it 3647 * immediately, since with rootrefs > 0, it won't go away. 3648 */ 3649 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) { 3650 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 3651 __func__, error); 3652 return (error); 3653 } 3654 vput(rootvp); 3655 } 3656 loop: 3657 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 3658 vholdl(vp); 3659 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 3660 if (error) { 3661 vdrop(vp); 3662 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3663 goto loop; 3664 } 3665 /* 3666 * Skip over a vnodes marked VV_SYSTEM. 3667 */ 3668 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 3669 VOP_UNLOCK(vp); 3670 vdrop(vp); 3671 continue; 3672 } 3673 /* 3674 * If WRITECLOSE is set, flush out unlinked but still open 3675 * files (even if open only for reading) and regular file 3676 * vnodes open for writing. 3677 */ 3678 if (flags & WRITECLOSE) { 3679 if (vp->v_object != NULL) { 3680 VM_OBJECT_WLOCK(vp->v_object); 3681 vm_object_page_clean(vp->v_object, 0, 0, 0); 3682 VM_OBJECT_WUNLOCK(vp->v_object); 3683 } 3684 do { 3685 error = VOP_FSYNC(vp, MNT_WAIT, td); 3686 } while (error == ERELOOKUP); 3687 if (error != 0) { 3688 VOP_UNLOCK(vp); 3689 vdrop(vp); 3690 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 3691 return (error); 3692 } 3693 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 3694 VI_LOCK(vp); 3695 3696 if ((vp->v_type == VNON || 3697 (error == 0 && vattr.va_nlink > 0)) && 3698 (vp->v_writecount <= 0 || vp->v_type != VREG)) { 3699 VOP_UNLOCK(vp); 3700 vdropl(vp); 3701 continue; 3702 } 3703 } else 3704 VI_LOCK(vp); 3705 /* 3706 * With v_usecount == 0, all we need to do is clear out the 3707 * vnode data structures and we are done. 3708 * 3709 * If FORCECLOSE is set, forcibly close the vnode. 3710 */ 3711 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 3712 vgonel(vp); 3713 } else { 3714 busy++; 3715 #ifdef DIAGNOSTIC 3716 if (busyprt) 3717 vn_printf(vp, "vflush: busy vnode "); 3718 #endif 3719 } 3720 VOP_UNLOCK(vp); 3721 vdropl(vp); 3722 } 3723 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 3724 /* 3725 * If just the root vnode is busy, and if its refcount 3726 * is equal to `rootrefs', then go ahead and kill it. 3727 */ 3728 VI_LOCK(rootvp); 3729 KASSERT(busy > 0, ("vflush: not busy")); 3730 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 3731 ("vflush: usecount %d < rootrefs %d", 3732 rootvp->v_usecount, rootrefs)); 3733 if (busy == 1 && rootvp->v_usecount == rootrefs) { 3734 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 3735 vgone(rootvp); 3736 VOP_UNLOCK(rootvp); 3737 busy = 0; 3738 } else 3739 VI_UNLOCK(rootvp); 3740 } 3741 if (busy) { 3742 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 3743 busy); 3744 return (EBUSY); 3745 } 3746 for (; rootrefs > 0; rootrefs--) 3747 vrele(rootvp); 3748 return (0); 3749 } 3750 3751 /* 3752 * Recycle an unused vnode to the front of the free list. 3753 */ 3754 int 3755 vrecycle(struct vnode *vp) 3756 { 3757 int recycled; 3758 3759 VI_LOCK(vp); 3760 recycled = vrecyclel(vp); 3761 VI_UNLOCK(vp); 3762 return (recycled); 3763 } 3764 3765 /* 3766 * vrecycle, with the vp interlock held. 3767 */ 3768 int 3769 vrecyclel(struct vnode *vp) 3770 { 3771 int recycled; 3772 3773 ASSERT_VOP_ELOCKED(vp, __func__); 3774 ASSERT_VI_LOCKED(vp, __func__); 3775 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3776 recycled = 0; 3777 if (vp->v_usecount == 0) { 3778 recycled = 1; 3779 vgonel(vp); 3780 } 3781 return (recycled); 3782 } 3783 3784 /* 3785 * Eliminate all activity associated with a vnode 3786 * in preparation for reuse. 3787 */ 3788 void 3789 vgone(struct vnode *vp) 3790 { 3791 VI_LOCK(vp); 3792 vgonel(vp); 3793 VI_UNLOCK(vp); 3794 } 3795 3796 static void 3797 notify_lowervp_vfs_dummy(struct mount *mp __unused, 3798 struct vnode *lowervp __unused) 3799 { 3800 } 3801 3802 /* 3803 * Notify upper mounts about reclaimed or unlinked vnode. 3804 */ 3805 void 3806 vfs_notify_upper(struct vnode *vp, int event) 3807 { 3808 static struct vfsops vgonel_vfsops = { 3809 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy, 3810 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy, 3811 }; 3812 struct mount *mp, *ump, *mmp; 3813 3814 mp = vp->v_mount; 3815 if (mp == NULL) 3816 return; 3817 if (TAILQ_EMPTY(&mp->mnt_uppers)) 3818 return; 3819 3820 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO); 3821 mmp->mnt_op = &vgonel_vfsops; 3822 mmp->mnt_kern_flag |= MNTK_MARKER; 3823 MNT_ILOCK(mp); 3824 mp->mnt_kern_flag |= MNTK_VGONE_UPPER; 3825 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) { 3826 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) { 3827 ump = TAILQ_NEXT(ump, mnt_upper_link); 3828 continue; 3829 } 3830 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link); 3831 MNT_IUNLOCK(mp); 3832 switch (event) { 3833 case VFS_NOTIFY_UPPER_RECLAIM: 3834 VFS_RECLAIM_LOWERVP(ump, vp); 3835 break; 3836 case VFS_NOTIFY_UPPER_UNLINK: 3837 VFS_UNLINK_LOWERVP(ump, vp); 3838 break; 3839 default: 3840 KASSERT(0, ("invalid event %d", event)); 3841 break; 3842 } 3843 MNT_ILOCK(mp); 3844 ump = TAILQ_NEXT(mmp, mnt_upper_link); 3845 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link); 3846 } 3847 free(mmp, M_TEMP); 3848 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER; 3849 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) { 3850 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER; 3851 wakeup(&mp->mnt_uppers); 3852 } 3853 MNT_IUNLOCK(mp); 3854 } 3855 3856 /* 3857 * vgone, with the vp interlock held. 3858 */ 3859 static void 3860 vgonel(struct vnode *vp) 3861 { 3862 struct thread *td; 3863 struct mount *mp; 3864 vm_object_t object; 3865 bool active, doinginact, oweinact; 3866 3867 ASSERT_VOP_ELOCKED(vp, "vgonel"); 3868 ASSERT_VI_LOCKED(vp, "vgonel"); 3869 VNASSERT(vp->v_holdcnt, vp, 3870 ("vgonel: vp %p has no reference.", vp)); 3871 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 3872 td = curthread; 3873 3874 /* 3875 * Don't vgonel if we're already doomed. 3876 */ 3877 if (vp->v_irflag & VIRF_DOOMED) 3878 return; 3879 /* 3880 * Paired with freevnode. 3881 */ 3882 vn_seqc_write_begin_locked(vp); 3883 vunlazy_gone(vp); 3884 vp->v_irflag |= VIRF_DOOMED; 3885 3886 /* 3887 * Check to see if the vnode is in use. If so, we have to 3888 * call VOP_CLOSE() and VOP_INACTIVE(). 3889 * 3890 * It could be that VOP_INACTIVE() requested reclamation, in 3891 * which case we should avoid recursion, so check 3892 * VI_DOINGINACT. This is not precise but good enough. 3893 */ 3894 active = vp->v_usecount > 0; 3895 oweinact = (vp->v_iflag & VI_OWEINACT) != 0; 3896 doinginact = (vp->v_iflag & VI_DOINGINACT) != 0; 3897 3898 /* 3899 * If we need to do inactive VI_OWEINACT will be set. 3900 */ 3901 if (vp->v_iflag & VI_DEFINACT) { 3902 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count")); 3903 vp->v_iflag &= ~VI_DEFINACT; 3904 vdropl(vp); 3905 } else { 3906 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count")); 3907 VI_UNLOCK(vp); 3908 } 3909 cache_purge_vgone(vp); 3910 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM); 3911 3912 /* 3913 * If purging an active vnode, it must be closed and 3914 * deactivated before being reclaimed. 3915 */ 3916 if (active) 3917 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 3918 if ((oweinact || active) && !doinginact) { 3919 VI_LOCK(vp); 3920 vinactivef(vp); 3921 VI_UNLOCK(vp); 3922 } 3923 if (vp->v_type == VSOCK) 3924 vfs_unp_reclaim(vp); 3925 3926 /* 3927 * Clean out any buffers associated with the vnode. 3928 * If the flush fails, just toss the buffers. 3929 */ 3930 mp = NULL; 3931 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 3932 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 3933 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) { 3934 while (vinvalbuf(vp, 0, 0, 0) != 0) 3935 ; 3936 } 3937 3938 BO_LOCK(&vp->v_bufobj); 3939 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) && 3940 vp->v_bufobj.bo_dirty.bv_cnt == 0 && 3941 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) && 3942 vp->v_bufobj.bo_clean.bv_cnt == 0, 3943 ("vp %p bufobj not invalidated", vp)); 3944 3945 /* 3946 * For VMIO bufobj, BO_DEAD is set later, or in 3947 * vm_object_terminate() after the object's page queue is 3948 * flushed. 3949 */ 3950 object = vp->v_bufobj.bo_object; 3951 if (object == NULL) 3952 vp->v_bufobj.bo_flag |= BO_DEAD; 3953 BO_UNLOCK(&vp->v_bufobj); 3954 3955 /* 3956 * Handle the VM part. Tmpfs handles v_object on its own (the 3957 * OBJT_VNODE check). Nullfs or other bypassing filesystems 3958 * should not touch the object borrowed from the lower vnode 3959 * (the handle check). 3960 */ 3961 if (object != NULL && object->type == OBJT_VNODE && 3962 object->handle == vp) 3963 vnode_destroy_vobject(vp); 3964 3965 /* 3966 * Reclaim the vnode. 3967 */ 3968 if (VOP_RECLAIM(vp)) 3969 panic("vgone: cannot reclaim"); 3970 if (mp != NULL) 3971 vn_finished_secondary_write(mp); 3972 VNASSERT(vp->v_object == NULL, vp, 3973 ("vop_reclaim left v_object vp=%p", vp)); 3974 /* 3975 * Clear the advisory locks and wake up waiting threads. 3976 */ 3977 (void)VOP_ADVLOCKPURGE(vp); 3978 vp->v_lockf = NULL; 3979 /* 3980 * Delete from old mount point vnode list. 3981 */ 3982 delmntque(vp); 3983 /* 3984 * Done with purge, reset to the standard lock and invalidate 3985 * the vnode. 3986 */ 3987 VI_LOCK(vp); 3988 vp->v_vnlock = &vp->v_lock; 3989 vp->v_op = &dead_vnodeops; 3990 vp->v_type = VBAD; 3991 } 3992 3993 /* 3994 * Print out a description of a vnode. 3995 */ 3996 static const char * const typename[] = 3997 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 3998 "VMARKER"}; 3999 4000 _Static_assert((VHOLD_ALL_FLAGS & ~VHOLD_NO_SMR) == 0, 4001 "new hold count flag not added to vn_printf"); 4002 4003 void 4004 vn_printf(struct vnode *vp, const char *fmt, ...) 4005 { 4006 va_list ap; 4007 char buf[256], buf2[16]; 4008 u_long flags; 4009 u_int holdcnt; 4010 4011 va_start(ap, fmt); 4012 vprintf(fmt, ap); 4013 va_end(ap); 4014 printf("%p: ", (void *)vp); 4015 printf("type %s\n", typename[vp->v_type]); 4016 holdcnt = atomic_load_int(&vp->v_holdcnt); 4017 printf(" usecount %d, writecount %d, refcount %d seqc users %d", 4018 vp->v_usecount, vp->v_writecount, holdcnt & ~VHOLD_ALL_FLAGS, 4019 vp->v_seqc_users); 4020 switch (vp->v_type) { 4021 case VDIR: 4022 printf(" mountedhere %p\n", vp->v_mountedhere); 4023 break; 4024 case VCHR: 4025 printf(" rdev %p\n", vp->v_rdev); 4026 break; 4027 case VSOCK: 4028 printf(" socket %p\n", vp->v_unpcb); 4029 break; 4030 case VFIFO: 4031 printf(" fifoinfo %p\n", vp->v_fifoinfo); 4032 break; 4033 default: 4034 printf("\n"); 4035 break; 4036 } 4037 buf[0] = '\0'; 4038 buf[1] = '\0'; 4039 if (holdcnt & VHOLD_NO_SMR) 4040 strlcat(buf, "|VHOLD_NO_SMR", sizeof(buf)); 4041 printf(" hold count flags (%s)\n", buf + 1); 4042 4043 buf[0] = '\0'; 4044 buf[1] = '\0'; 4045 if (vp->v_irflag & VIRF_DOOMED) 4046 strlcat(buf, "|VIRF_DOOMED", sizeof(buf)); 4047 if (vp->v_irflag & VIRF_PGREAD) 4048 strlcat(buf, "|VIRF_PGREAD", sizeof(buf)); 4049 flags = vp->v_irflag & ~(VIRF_DOOMED | VIRF_PGREAD); 4050 if (flags != 0) { 4051 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags); 4052 strlcat(buf, buf2, sizeof(buf)); 4053 } 4054 if (vp->v_vflag & VV_ROOT) 4055 strlcat(buf, "|VV_ROOT", sizeof(buf)); 4056 if (vp->v_vflag & VV_ISTTY) 4057 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 4058 if (vp->v_vflag & VV_NOSYNC) 4059 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 4060 if (vp->v_vflag & VV_ETERNALDEV) 4061 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf)); 4062 if (vp->v_vflag & VV_CACHEDLABEL) 4063 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 4064 if (vp->v_vflag & VV_VMSIZEVNLOCK) 4065 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf)); 4066 if (vp->v_vflag & VV_COPYONWRITE) 4067 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 4068 if (vp->v_vflag & VV_SYSTEM) 4069 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 4070 if (vp->v_vflag & VV_PROCDEP) 4071 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 4072 if (vp->v_vflag & VV_NOKNOTE) 4073 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 4074 if (vp->v_vflag & VV_DELETED) 4075 strlcat(buf, "|VV_DELETED", sizeof(buf)); 4076 if (vp->v_vflag & VV_MD) 4077 strlcat(buf, "|VV_MD", sizeof(buf)); 4078 if (vp->v_vflag & VV_FORCEINSMQ) 4079 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf)); 4080 if (vp->v_vflag & VV_READLINK) 4081 strlcat(buf, "|VV_READLINK", sizeof(buf)); 4082 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV | 4083 VV_CACHEDLABEL | VV_VMSIZEVNLOCK | VV_COPYONWRITE | VV_SYSTEM | 4084 VV_PROCDEP | VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ | 4085 VV_READLINK); 4086 if (flags != 0) { 4087 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 4088 strlcat(buf, buf2, sizeof(buf)); 4089 } 4090 if (vp->v_iflag & VI_TEXT_REF) 4091 strlcat(buf, "|VI_TEXT_REF", sizeof(buf)); 4092 if (vp->v_iflag & VI_MOUNT) 4093 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 4094 if (vp->v_iflag & VI_DOINGINACT) 4095 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 4096 if (vp->v_iflag & VI_OWEINACT) 4097 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 4098 if (vp->v_iflag & VI_DEFINACT) 4099 strlcat(buf, "|VI_DEFINACT", sizeof(buf)); 4100 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_DOINGINACT | 4101 VI_OWEINACT | VI_DEFINACT); 4102 if (flags != 0) { 4103 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 4104 strlcat(buf, buf2, sizeof(buf)); 4105 } 4106 if (vp->v_mflag & VMP_LAZYLIST) 4107 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf)); 4108 flags = vp->v_mflag & ~(VMP_LAZYLIST); 4109 if (flags != 0) { 4110 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags); 4111 strlcat(buf, buf2, sizeof(buf)); 4112 } 4113 printf(" flags (%s)", buf + 1); 4114 if (mtx_owned(VI_MTX(vp))) 4115 printf(" VI_LOCKed"); 4116 printf("\n"); 4117 if (vp->v_object != NULL) 4118 printf(" v_object %p ref %d pages %d " 4119 "cleanbuf %d dirtybuf %d\n", 4120 vp->v_object, vp->v_object->ref_count, 4121 vp->v_object->resident_page_count, 4122 vp->v_bufobj.bo_clean.bv_cnt, 4123 vp->v_bufobj.bo_dirty.bv_cnt); 4124 printf(" "); 4125 lockmgr_printinfo(vp->v_vnlock); 4126 if (vp->v_data != NULL) 4127 VOP_PRINT(vp); 4128 } 4129 4130 #ifdef DDB 4131 /* 4132 * List all of the locked vnodes in the system. 4133 * Called when debugging the kernel. 4134 */ 4135 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 4136 { 4137 struct mount *mp; 4138 struct vnode *vp; 4139 4140 /* 4141 * Note: because this is DDB, we can't obey the locking semantics 4142 * for these structures, which means we could catch an inconsistent 4143 * state and dereference a nasty pointer. Not much to be done 4144 * about that. 4145 */ 4146 db_printf("Locked vnodes\n"); 4147 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4148 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4149 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp)) 4150 vn_printf(vp, "vnode "); 4151 } 4152 } 4153 } 4154 4155 /* 4156 * Show details about the given vnode. 4157 */ 4158 DB_SHOW_COMMAND(vnode, db_show_vnode) 4159 { 4160 struct vnode *vp; 4161 4162 if (!have_addr) 4163 return; 4164 vp = (struct vnode *)addr; 4165 vn_printf(vp, "vnode "); 4166 } 4167 4168 /* 4169 * Show details about the given mount point. 4170 */ 4171 DB_SHOW_COMMAND(mount, db_show_mount) 4172 { 4173 struct mount *mp; 4174 struct vfsopt *opt; 4175 struct statfs *sp; 4176 struct vnode *vp; 4177 char buf[512]; 4178 uint64_t mflags; 4179 u_int flags; 4180 4181 if (!have_addr) { 4182 /* No address given, print short info about all mount points. */ 4183 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4184 db_printf("%p %s on %s (%s)\n", mp, 4185 mp->mnt_stat.f_mntfromname, 4186 mp->mnt_stat.f_mntonname, 4187 mp->mnt_stat.f_fstypename); 4188 if (db_pager_quit) 4189 break; 4190 } 4191 db_printf("\nMore info: show mount <addr>\n"); 4192 return; 4193 } 4194 4195 mp = (struct mount *)addr; 4196 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 4197 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 4198 4199 buf[0] = '\0'; 4200 mflags = mp->mnt_flag; 4201 #define MNT_FLAG(flag) do { \ 4202 if (mflags & (flag)) { \ 4203 if (buf[0] != '\0') \ 4204 strlcat(buf, ", ", sizeof(buf)); \ 4205 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 4206 mflags &= ~(flag); \ 4207 } \ 4208 } while (0) 4209 MNT_FLAG(MNT_RDONLY); 4210 MNT_FLAG(MNT_SYNCHRONOUS); 4211 MNT_FLAG(MNT_NOEXEC); 4212 MNT_FLAG(MNT_NOSUID); 4213 MNT_FLAG(MNT_NFS4ACLS); 4214 MNT_FLAG(MNT_UNION); 4215 MNT_FLAG(MNT_ASYNC); 4216 MNT_FLAG(MNT_SUIDDIR); 4217 MNT_FLAG(MNT_SOFTDEP); 4218 MNT_FLAG(MNT_NOSYMFOLLOW); 4219 MNT_FLAG(MNT_GJOURNAL); 4220 MNT_FLAG(MNT_MULTILABEL); 4221 MNT_FLAG(MNT_ACLS); 4222 MNT_FLAG(MNT_NOATIME); 4223 MNT_FLAG(MNT_NOCLUSTERR); 4224 MNT_FLAG(MNT_NOCLUSTERW); 4225 MNT_FLAG(MNT_SUJ); 4226 MNT_FLAG(MNT_EXRDONLY); 4227 MNT_FLAG(MNT_EXPORTED); 4228 MNT_FLAG(MNT_DEFEXPORTED); 4229 MNT_FLAG(MNT_EXPORTANON); 4230 MNT_FLAG(MNT_EXKERB); 4231 MNT_FLAG(MNT_EXPUBLIC); 4232 MNT_FLAG(MNT_LOCAL); 4233 MNT_FLAG(MNT_QUOTA); 4234 MNT_FLAG(MNT_ROOTFS); 4235 MNT_FLAG(MNT_USER); 4236 MNT_FLAG(MNT_IGNORE); 4237 MNT_FLAG(MNT_UPDATE); 4238 MNT_FLAG(MNT_DELEXPORT); 4239 MNT_FLAG(MNT_RELOAD); 4240 MNT_FLAG(MNT_FORCE); 4241 MNT_FLAG(MNT_SNAPSHOT); 4242 MNT_FLAG(MNT_BYFSID); 4243 #undef MNT_FLAG 4244 if (mflags != 0) { 4245 if (buf[0] != '\0') 4246 strlcat(buf, ", ", sizeof(buf)); 4247 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 4248 "0x%016jx", mflags); 4249 } 4250 db_printf(" mnt_flag = %s\n", buf); 4251 4252 buf[0] = '\0'; 4253 flags = mp->mnt_kern_flag; 4254 #define MNT_KERN_FLAG(flag) do { \ 4255 if (flags & (flag)) { \ 4256 if (buf[0] != '\0') \ 4257 strlcat(buf, ", ", sizeof(buf)); \ 4258 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 4259 flags &= ~(flag); \ 4260 } \ 4261 } while (0) 4262 MNT_KERN_FLAG(MNTK_UNMOUNTF); 4263 MNT_KERN_FLAG(MNTK_ASYNC); 4264 MNT_KERN_FLAG(MNTK_SOFTDEP); 4265 MNT_KERN_FLAG(MNTK_DRAINING); 4266 MNT_KERN_FLAG(MNTK_REFEXPIRE); 4267 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED); 4268 MNT_KERN_FLAG(MNTK_SHARED_WRITES); 4269 MNT_KERN_FLAG(MNTK_NO_IOPF); 4270 MNT_KERN_FLAG(MNTK_VGONE_UPPER); 4271 MNT_KERN_FLAG(MNTK_VGONE_WAITER); 4272 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT); 4273 MNT_KERN_FLAG(MNTK_MARKER); 4274 MNT_KERN_FLAG(MNTK_USES_BCACHE); 4275 MNT_KERN_FLAG(MNTK_FPLOOKUP); 4276 MNT_KERN_FLAG(MNTK_NOASYNC); 4277 MNT_KERN_FLAG(MNTK_UNMOUNT); 4278 MNT_KERN_FLAG(MNTK_MWAIT); 4279 MNT_KERN_FLAG(MNTK_SUSPEND); 4280 MNT_KERN_FLAG(MNTK_SUSPEND2); 4281 MNT_KERN_FLAG(MNTK_SUSPENDED); 4282 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 4283 MNT_KERN_FLAG(MNTK_NOKNOTE); 4284 #undef MNT_KERN_FLAG 4285 if (flags != 0) { 4286 if (buf[0] != '\0') 4287 strlcat(buf, ", ", sizeof(buf)); 4288 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 4289 "0x%08x", flags); 4290 } 4291 db_printf(" mnt_kern_flag = %s\n", buf); 4292 4293 db_printf(" mnt_opt = "); 4294 opt = TAILQ_FIRST(mp->mnt_opt); 4295 if (opt != NULL) { 4296 db_printf("%s", opt->name); 4297 opt = TAILQ_NEXT(opt, link); 4298 while (opt != NULL) { 4299 db_printf(", %s", opt->name); 4300 opt = TAILQ_NEXT(opt, link); 4301 } 4302 } 4303 db_printf("\n"); 4304 4305 sp = &mp->mnt_stat; 4306 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 4307 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 4308 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 4309 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 4310 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 4311 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 4312 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 4313 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 4314 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 4315 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 4316 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 4317 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 4318 4319 db_printf(" mnt_cred = { uid=%u ruid=%u", 4320 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 4321 if (jailed(mp->mnt_cred)) 4322 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 4323 db_printf(" }\n"); 4324 db_printf(" mnt_ref = %d (with %d in the struct)\n", 4325 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref); 4326 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 4327 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 4328 db_printf(" mnt_lazyvnodelistsize = %d\n", 4329 mp->mnt_lazyvnodelistsize); 4330 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n", 4331 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount); 4332 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 4333 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 4334 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 4335 db_printf(" mnt_lockref = %d (with %d in the struct)\n", 4336 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref); 4337 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 4338 db_printf(" mnt_secondary_accwrites = %d\n", 4339 mp->mnt_secondary_accwrites); 4340 db_printf(" mnt_gjprovider = %s\n", 4341 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 4342 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops); 4343 4344 db_printf("\n\nList of active vnodes\n"); 4345 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4346 if (vp->v_type != VMARKER && vp->v_holdcnt > 0) { 4347 vn_printf(vp, "vnode "); 4348 if (db_pager_quit) 4349 break; 4350 } 4351 } 4352 db_printf("\n\nList of inactive vnodes\n"); 4353 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4354 if (vp->v_type != VMARKER && vp->v_holdcnt == 0) { 4355 vn_printf(vp, "vnode "); 4356 if (db_pager_quit) 4357 break; 4358 } 4359 } 4360 } 4361 #endif /* DDB */ 4362 4363 /* 4364 * Fill in a struct xvfsconf based on a struct vfsconf. 4365 */ 4366 static int 4367 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp) 4368 { 4369 struct xvfsconf xvfsp; 4370 4371 bzero(&xvfsp, sizeof(xvfsp)); 4372 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4373 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4374 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4375 xvfsp.vfc_flags = vfsp->vfc_flags; 4376 /* 4377 * These are unused in userland, we keep them 4378 * to not break binary compatibility. 4379 */ 4380 xvfsp.vfc_vfsops = NULL; 4381 xvfsp.vfc_next = NULL; 4382 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4383 } 4384 4385 #ifdef COMPAT_FREEBSD32 4386 struct xvfsconf32 { 4387 uint32_t vfc_vfsops; 4388 char vfc_name[MFSNAMELEN]; 4389 int32_t vfc_typenum; 4390 int32_t vfc_refcount; 4391 int32_t vfc_flags; 4392 uint32_t vfc_next; 4393 }; 4394 4395 static int 4396 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp) 4397 { 4398 struct xvfsconf32 xvfsp; 4399 4400 bzero(&xvfsp, sizeof(xvfsp)); 4401 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 4402 xvfsp.vfc_typenum = vfsp->vfc_typenum; 4403 xvfsp.vfc_refcount = vfsp->vfc_refcount; 4404 xvfsp.vfc_flags = vfsp->vfc_flags; 4405 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 4406 } 4407 #endif 4408 4409 /* 4410 * Top level filesystem related information gathering. 4411 */ 4412 static int 4413 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 4414 { 4415 struct vfsconf *vfsp; 4416 int error; 4417 4418 error = 0; 4419 vfsconf_slock(); 4420 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4421 #ifdef COMPAT_FREEBSD32 4422 if (req->flags & SCTL_MASK32) 4423 error = vfsconf2x32(req, vfsp); 4424 else 4425 #endif 4426 error = vfsconf2x(req, vfsp); 4427 if (error) 4428 break; 4429 } 4430 vfsconf_sunlock(); 4431 return (error); 4432 } 4433 4434 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD | 4435 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist, 4436 "S,xvfsconf", "List of all configured filesystems"); 4437 4438 #ifndef BURN_BRIDGES 4439 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 4440 4441 static int 4442 vfs_sysctl(SYSCTL_HANDLER_ARGS) 4443 { 4444 int *name = (int *)arg1 - 1; /* XXX */ 4445 u_int namelen = arg2 + 1; /* XXX */ 4446 struct vfsconf *vfsp; 4447 4448 log(LOG_WARNING, "userland calling deprecated sysctl, " 4449 "please rebuild world\n"); 4450 4451 #if 1 || defined(COMPAT_PRELITE2) 4452 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 4453 if (namelen == 1) 4454 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 4455 #endif 4456 4457 switch (name[1]) { 4458 case VFS_MAXTYPENUM: 4459 if (namelen != 2) 4460 return (ENOTDIR); 4461 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 4462 case VFS_CONF: 4463 if (namelen != 3) 4464 return (ENOTDIR); /* overloaded */ 4465 vfsconf_slock(); 4466 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4467 if (vfsp->vfc_typenum == name[2]) 4468 break; 4469 } 4470 vfsconf_sunlock(); 4471 if (vfsp == NULL) 4472 return (EOPNOTSUPP); 4473 #ifdef COMPAT_FREEBSD32 4474 if (req->flags & SCTL_MASK32) 4475 return (vfsconf2x32(req, vfsp)); 4476 else 4477 #endif 4478 return (vfsconf2x(req, vfsp)); 4479 } 4480 return (EOPNOTSUPP); 4481 } 4482 4483 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP | 4484 CTLFLAG_MPSAFE, vfs_sysctl, 4485 "Generic filesystem"); 4486 4487 #if 1 || defined(COMPAT_PRELITE2) 4488 4489 static int 4490 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 4491 { 4492 int error; 4493 struct vfsconf *vfsp; 4494 struct ovfsconf ovfs; 4495 4496 vfsconf_slock(); 4497 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 4498 bzero(&ovfs, sizeof(ovfs)); 4499 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 4500 strcpy(ovfs.vfc_name, vfsp->vfc_name); 4501 ovfs.vfc_index = vfsp->vfc_typenum; 4502 ovfs.vfc_refcount = vfsp->vfc_refcount; 4503 ovfs.vfc_flags = vfsp->vfc_flags; 4504 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 4505 if (error != 0) { 4506 vfsconf_sunlock(); 4507 return (error); 4508 } 4509 } 4510 vfsconf_sunlock(); 4511 return (0); 4512 } 4513 4514 #endif /* 1 || COMPAT_PRELITE2 */ 4515 #endif /* !BURN_BRIDGES */ 4516 4517 #define KINFO_VNODESLOP 10 4518 #ifdef notyet 4519 /* 4520 * Dump vnode list (via sysctl). 4521 */ 4522 /* ARGSUSED */ 4523 static int 4524 sysctl_vnode(SYSCTL_HANDLER_ARGS) 4525 { 4526 struct xvnode *xvn; 4527 struct mount *mp; 4528 struct vnode *vp; 4529 int error, len, n; 4530 4531 /* 4532 * Stale numvnodes access is not fatal here. 4533 */ 4534 req->lock = 0; 4535 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 4536 if (!req->oldptr) 4537 /* Make an estimate */ 4538 return (SYSCTL_OUT(req, 0, len)); 4539 4540 error = sysctl_wire_old_buffer(req, 0); 4541 if (error != 0) 4542 return (error); 4543 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 4544 n = 0; 4545 mtx_lock(&mountlist_mtx); 4546 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 4547 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 4548 continue; 4549 MNT_ILOCK(mp); 4550 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 4551 if (n == len) 4552 break; 4553 vref(vp); 4554 xvn[n].xv_size = sizeof *xvn; 4555 xvn[n].xv_vnode = vp; 4556 xvn[n].xv_id = 0; /* XXX compat */ 4557 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 4558 XV_COPY(usecount); 4559 XV_COPY(writecount); 4560 XV_COPY(holdcnt); 4561 XV_COPY(mount); 4562 XV_COPY(numoutput); 4563 XV_COPY(type); 4564 #undef XV_COPY 4565 xvn[n].xv_flag = vp->v_vflag; 4566 4567 switch (vp->v_type) { 4568 case VREG: 4569 case VDIR: 4570 case VLNK: 4571 break; 4572 case VBLK: 4573 case VCHR: 4574 if (vp->v_rdev == NULL) { 4575 vrele(vp); 4576 continue; 4577 } 4578 xvn[n].xv_dev = dev2udev(vp->v_rdev); 4579 break; 4580 case VSOCK: 4581 xvn[n].xv_socket = vp->v_socket; 4582 break; 4583 case VFIFO: 4584 xvn[n].xv_fifo = vp->v_fifoinfo; 4585 break; 4586 case VNON: 4587 case VBAD: 4588 default: 4589 /* shouldn't happen? */ 4590 vrele(vp); 4591 continue; 4592 } 4593 vrele(vp); 4594 ++n; 4595 } 4596 MNT_IUNLOCK(mp); 4597 mtx_lock(&mountlist_mtx); 4598 vfs_unbusy(mp); 4599 if (n == len) 4600 break; 4601 } 4602 mtx_unlock(&mountlist_mtx); 4603 4604 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 4605 free(xvn, M_TEMP); 4606 return (error); 4607 } 4608 4609 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD | 4610 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode", 4611 ""); 4612 #endif 4613 4614 static void 4615 unmount_or_warn(struct mount *mp) 4616 { 4617 int error; 4618 4619 error = dounmount(mp, MNT_FORCE, curthread); 4620 if (error != 0) { 4621 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname); 4622 if (error == EBUSY) 4623 printf("BUSY)\n"); 4624 else 4625 printf("%d)\n", error); 4626 } 4627 } 4628 4629 /* 4630 * Unmount all filesystems. The list is traversed in reverse order 4631 * of mounting to avoid dependencies. 4632 */ 4633 void 4634 vfs_unmountall(void) 4635 { 4636 struct mount *mp, *tmp; 4637 4638 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 4639 4640 /* 4641 * Since this only runs when rebooting, it is not interlocked. 4642 */ 4643 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) { 4644 vfs_ref(mp); 4645 4646 /* 4647 * Forcibly unmounting "/dev" before "/" would prevent clean 4648 * unmount of the latter. 4649 */ 4650 if (mp == rootdevmp) 4651 continue; 4652 4653 unmount_or_warn(mp); 4654 } 4655 4656 if (rootdevmp != NULL) 4657 unmount_or_warn(rootdevmp); 4658 } 4659 4660 static void 4661 vfs_deferred_inactive(struct vnode *vp, int lkflags) 4662 { 4663 4664 ASSERT_VI_LOCKED(vp, __func__); 4665 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set")); 4666 if ((vp->v_iflag & VI_OWEINACT) == 0) { 4667 vdropl(vp); 4668 return; 4669 } 4670 if (vn_lock(vp, lkflags) == 0) { 4671 VI_LOCK(vp); 4672 vinactive(vp); 4673 VOP_UNLOCK(vp); 4674 vdropl(vp); 4675 return; 4676 } 4677 vdefer_inactive_unlocked(vp); 4678 } 4679 4680 static int 4681 vfs_periodic_inactive_filter(struct vnode *vp, void *arg) 4682 { 4683 4684 return (vp->v_iflag & VI_DEFINACT); 4685 } 4686 4687 static void __noinline 4688 vfs_periodic_inactive(struct mount *mp, int flags) 4689 { 4690 struct vnode *vp, *mvp; 4691 int lkflags; 4692 4693 lkflags = LK_EXCLUSIVE | LK_INTERLOCK; 4694 if (flags != MNT_WAIT) 4695 lkflags |= LK_NOWAIT; 4696 4697 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) { 4698 if ((vp->v_iflag & VI_DEFINACT) == 0) { 4699 VI_UNLOCK(vp); 4700 continue; 4701 } 4702 vp->v_iflag &= ~VI_DEFINACT; 4703 vfs_deferred_inactive(vp, lkflags); 4704 } 4705 } 4706 4707 static inline bool 4708 vfs_want_msync(struct vnode *vp) 4709 { 4710 struct vm_object *obj; 4711 4712 /* 4713 * This test may be performed without any locks held. 4714 * We rely on vm_object's type stability. 4715 */ 4716 if (vp->v_vflag & VV_NOSYNC) 4717 return (false); 4718 obj = vp->v_object; 4719 return (obj != NULL && vm_object_mightbedirty(obj)); 4720 } 4721 4722 static int 4723 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused) 4724 { 4725 4726 if (vp->v_vflag & VV_NOSYNC) 4727 return (false); 4728 if (vp->v_iflag & VI_DEFINACT) 4729 return (true); 4730 return (vfs_want_msync(vp)); 4731 } 4732 4733 static void __noinline 4734 vfs_periodic_msync_inactive(struct mount *mp, int flags) 4735 { 4736 struct vnode *vp, *mvp; 4737 struct vm_object *obj; 4738 int lkflags, objflags; 4739 bool seen_defer; 4740 4741 lkflags = LK_EXCLUSIVE | LK_INTERLOCK; 4742 if (flags != MNT_WAIT) { 4743 lkflags |= LK_NOWAIT; 4744 objflags = OBJPC_NOSYNC; 4745 } else { 4746 objflags = OBJPC_SYNC; 4747 } 4748 4749 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) { 4750 seen_defer = false; 4751 if (vp->v_iflag & VI_DEFINACT) { 4752 vp->v_iflag &= ~VI_DEFINACT; 4753 seen_defer = true; 4754 } 4755 if (!vfs_want_msync(vp)) { 4756 if (seen_defer) 4757 vfs_deferred_inactive(vp, lkflags); 4758 else 4759 VI_UNLOCK(vp); 4760 continue; 4761 } 4762 if (vget(vp, lkflags) == 0) { 4763 obj = vp->v_object; 4764 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) { 4765 VM_OBJECT_WLOCK(obj); 4766 vm_object_page_clean(obj, 0, 0, objflags); 4767 VM_OBJECT_WUNLOCK(obj); 4768 } 4769 vput(vp); 4770 if (seen_defer) 4771 vdrop(vp); 4772 } else { 4773 if (seen_defer) 4774 vdefer_inactive_unlocked(vp); 4775 } 4776 } 4777 } 4778 4779 void 4780 vfs_periodic(struct mount *mp, int flags) 4781 { 4782 4783 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 4784 4785 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0) 4786 vfs_periodic_inactive(mp, flags); 4787 else 4788 vfs_periodic_msync_inactive(mp, flags); 4789 } 4790 4791 static void 4792 destroy_vpollinfo_free(struct vpollinfo *vi) 4793 { 4794 4795 knlist_destroy(&vi->vpi_selinfo.si_note); 4796 mtx_destroy(&vi->vpi_lock); 4797 free(vi, M_VNODEPOLL); 4798 } 4799 4800 static void 4801 destroy_vpollinfo(struct vpollinfo *vi) 4802 { 4803 4804 knlist_clear(&vi->vpi_selinfo.si_note, 1); 4805 seldrain(&vi->vpi_selinfo); 4806 destroy_vpollinfo_free(vi); 4807 } 4808 4809 /* 4810 * Initialize per-vnode helper structure to hold poll-related state. 4811 */ 4812 void 4813 v_addpollinfo(struct vnode *vp) 4814 { 4815 struct vpollinfo *vi; 4816 4817 if (vp->v_pollinfo != NULL) 4818 return; 4819 vi = malloc(sizeof(*vi), M_VNODEPOLL, M_WAITOK | M_ZERO); 4820 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 4821 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 4822 vfs_knlunlock, vfs_knl_assert_lock); 4823 VI_LOCK(vp); 4824 if (vp->v_pollinfo != NULL) { 4825 VI_UNLOCK(vp); 4826 destroy_vpollinfo_free(vi); 4827 return; 4828 } 4829 vp->v_pollinfo = vi; 4830 VI_UNLOCK(vp); 4831 } 4832 4833 /* 4834 * Record a process's interest in events which might happen to 4835 * a vnode. Because poll uses the historic select-style interface 4836 * internally, this routine serves as both the ``check for any 4837 * pending events'' and the ``record my interest in future events'' 4838 * functions. (These are done together, while the lock is held, 4839 * to avoid race conditions.) 4840 */ 4841 int 4842 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 4843 { 4844 4845 v_addpollinfo(vp); 4846 mtx_lock(&vp->v_pollinfo->vpi_lock); 4847 if (vp->v_pollinfo->vpi_revents & events) { 4848 /* 4849 * This leaves events we are not interested 4850 * in available for the other process which 4851 * which presumably had requested them 4852 * (otherwise they would never have been 4853 * recorded). 4854 */ 4855 events &= vp->v_pollinfo->vpi_revents; 4856 vp->v_pollinfo->vpi_revents &= ~events; 4857 4858 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4859 return (events); 4860 } 4861 vp->v_pollinfo->vpi_events |= events; 4862 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 4863 mtx_unlock(&vp->v_pollinfo->vpi_lock); 4864 return (0); 4865 } 4866 4867 /* 4868 * Routine to create and manage a filesystem syncer vnode. 4869 */ 4870 #define sync_close ((int (*)(struct vop_close_args *))nullop) 4871 static int sync_fsync(struct vop_fsync_args *); 4872 static int sync_inactive(struct vop_inactive_args *); 4873 static int sync_reclaim(struct vop_reclaim_args *); 4874 4875 static struct vop_vector sync_vnodeops = { 4876 .vop_bypass = VOP_EOPNOTSUPP, 4877 .vop_close = sync_close, /* close */ 4878 .vop_fsync = sync_fsync, /* fsync */ 4879 .vop_inactive = sync_inactive, /* inactive */ 4880 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */ 4881 .vop_reclaim = sync_reclaim, /* reclaim */ 4882 .vop_lock1 = vop_stdlock, /* lock */ 4883 .vop_unlock = vop_stdunlock, /* unlock */ 4884 .vop_islocked = vop_stdislocked, /* islocked */ 4885 }; 4886 VFS_VOP_VECTOR_REGISTER(sync_vnodeops); 4887 4888 /* 4889 * Create a new filesystem syncer vnode for the specified mount point. 4890 */ 4891 void 4892 vfs_allocate_syncvnode(struct mount *mp) 4893 { 4894 struct vnode *vp; 4895 struct bufobj *bo; 4896 static long start, incr, next; 4897 int error; 4898 4899 /* Allocate a new vnode */ 4900 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp); 4901 if (error != 0) 4902 panic("vfs_allocate_syncvnode: getnewvnode() failed"); 4903 vp->v_type = VNON; 4904 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4905 vp->v_vflag |= VV_FORCEINSMQ; 4906 error = insmntque(vp, mp); 4907 if (error != 0) 4908 panic("vfs_allocate_syncvnode: insmntque() failed"); 4909 vp->v_vflag &= ~VV_FORCEINSMQ; 4910 VOP_UNLOCK(vp); 4911 /* 4912 * Place the vnode onto the syncer worklist. We attempt to 4913 * scatter them about on the list so that they will go off 4914 * at evenly distributed times even if all the filesystems 4915 * are mounted at once. 4916 */ 4917 next += incr; 4918 if (next == 0 || next > syncer_maxdelay) { 4919 start /= 2; 4920 incr /= 2; 4921 if (start == 0) { 4922 start = syncer_maxdelay / 2; 4923 incr = syncer_maxdelay; 4924 } 4925 next = start; 4926 } 4927 bo = &vp->v_bufobj; 4928 BO_LOCK(bo); 4929 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 4930 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 4931 mtx_lock(&sync_mtx); 4932 sync_vnode_count++; 4933 if (mp->mnt_syncer == NULL) { 4934 mp->mnt_syncer = vp; 4935 vp = NULL; 4936 } 4937 mtx_unlock(&sync_mtx); 4938 BO_UNLOCK(bo); 4939 if (vp != NULL) { 4940 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4941 vgone(vp); 4942 vput(vp); 4943 } 4944 } 4945 4946 void 4947 vfs_deallocate_syncvnode(struct mount *mp) 4948 { 4949 struct vnode *vp; 4950 4951 mtx_lock(&sync_mtx); 4952 vp = mp->mnt_syncer; 4953 if (vp != NULL) 4954 mp->mnt_syncer = NULL; 4955 mtx_unlock(&sync_mtx); 4956 if (vp != NULL) 4957 vrele(vp); 4958 } 4959 4960 /* 4961 * Do a lazy sync of the filesystem. 4962 */ 4963 static int 4964 sync_fsync(struct vop_fsync_args *ap) 4965 { 4966 struct vnode *syncvp = ap->a_vp; 4967 struct mount *mp = syncvp->v_mount; 4968 int error, save; 4969 struct bufobj *bo; 4970 4971 /* 4972 * We only need to do something if this is a lazy evaluation. 4973 */ 4974 if (ap->a_waitfor != MNT_LAZY) 4975 return (0); 4976 4977 /* 4978 * Move ourselves to the back of the sync list. 4979 */ 4980 bo = &syncvp->v_bufobj; 4981 BO_LOCK(bo); 4982 vn_syncer_add_to_worklist(bo, syncdelay); 4983 BO_UNLOCK(bo); 4984 4985 /* 4986 * Walk the list of vnodes pushing all that are dirty and 4987 * not already on the sync list. 4988 */ 4989 if (vfs_busy(mp, MBF_NOWAIT) != 0) 4990 return (0); 4991 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 4992 vfs_unbusy(mp); 4993 return (0); 4994 } 4995 save = curthread_pflags_set(TDP_SYNCIO); 4996 /* 4997 * The filesystem at hand may be idle with free vnodes stored in the 4998 * batch. Return them instead of letting them stay there indefinitely. 4999 */ 5000 vfs_periodic(mp, MNT_NOWAIT); 5001 error = VFS_SYNC(mp, MNT_LAZY); 5002 curthread_pflags_restore(save); 5003 vn_finished_write(mp); 5004 vfs_unbusy(mp); 5005 return (error); 5006 } 5007 5008 /* 5009 * The syncer vnode is no referenced. 5010 */ 5011 static int 5012 sync_inactive(struct vop_inactive_args *ap) 5013 { 5014 5015 vgone(ap->a_vp); 5016 return (0); 5017 } 5018 5019 /* 5020 * The syncer vnode is no longer needed and is being decommissioned. 5021 * 5022 * Modifications to the worklist must be protected by sync_mtx. 5023 */ 5024 static int 5025 sync_reclaim(struct vop_reclaim_args *ap) 5026 { 5027 struct vnode *vp = ap->a_vp; 5028 struct bufobj *bo; 5029 5030 bo = &vp->v_bufobj; 5031 BO_LOCK(bo); 5032 mtx_lock(&sync_mtx); 5033 if (vp->v_mount->mnt_syncer == vp) 5034 vp->v_mount->mnt_syncer = NULL; 5035 if (bo->bo_flag & BO_ONWORKLST) { 5036 LIST_REMOVE(bo, bo_synclist); 5037 syncer_worklist_len--; 5038 sync_vnode_count--; 5039 bo->bo_flag &= ~BO_ONWORKLST; 5040 } 5041 mtx_unlock(&sync_mtx); 5042 BO_UNLOCK(bo); 5043 5044 return (0); 5045 } 5046 5047 int 5048 vn_need_pageq_flush(struct vnode *vp) 5049 { 5050 struct vm_object *obj; 5051 int need; 5052 5053 MPASS(mtx_owned(VI_MTX(vp))); 5054 need = 0; 5055 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 && 5056 vm_object_mightbedirty(obj)) 5057 need = 1; 5058 return (need); 5059 } 5060 5061 /* 5062 * Check if vnode represents a disk device 5063 */ 5064 bool 5065 vn_isdisk_error(struct vnode *vp, int *errp) 5066 { 5067 int error; 5068 5069 if (vp->v_type != VCHR) { 5070 error = ENOTBLK; 5071 goto out; 5072 } 5073 error = 0; 5074 dev_lock(); 5075 if (vp->v_rdev == NULL) 5076 error = ENXIO; 5077 else if (vp->v_rdev->si_devsw == NULL) 5078 error = ENXIO; 5079 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 5080 error = ENOTBLK; 5081 dev_unlock(); 5082 out: 5083 *errp = error; 5084 return (error == 0); 5085 } 5086 5087 bool 5088 vn_isdisk(struct vnode *vp) 5089 { 5090 int error; 5091 5092 return (vn_isdisk_error(vp, &error)); 5093 } 5094 5095 /* 5096 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see 5097 * the comment above cache_fplookup for details. 5098 */ 5099 int 5100 vaccess_vexec_smr(mode_t file_mode, uid_t file_uid, gid_t file_gid, struct ucred *cred) 5101 { 5102 int error; 5103 5104 VFS_SMR_ASSERT_ENTERED(); 5105 5106 /* Check the owner. */ 5107 if (cred->cr_uid == file_uid) { 5108 if (file_mode & S_IXUSR) 5109 return (0); 5110 goto out_error; 5111 } 5112 5113 /* Otherwise, check the groups (first match) */ 5114 if (groupmember(file_gid, cred)) { 5115 if (file_mode & S_IXGRP) 5116 return (0); 5117 goto out_error; 5118 } 5119 5120 /* Otherwise, check everyone else. */ 5121 if (file_mode & S_IXOTH) 5122 return (0); 5123 out_error: 5124 /* 5125 * Permission check failed, but it is possible denial will get overwritten 5126 * (e.g., when root is traversing through a 700 directory owned by someone 5127 * else). 5128 * 5129 * vaccess() calls priv_check_cred which in turn can descent into MAC 5130 * modules overriding this result. It's quite unclear what semantics 5131 * are allowed for them to operate, thus for safety we don't call them 5132 * from within the SMR section. This also means if any such modules 5133 * are present, we have to let the regular lookup decide. 5134 */ 5135 error = priv_check_cred_vfs_lookup_nomac(cred); 5136 switch (error) { 5137 case 0: 5138 return (0); 5139 case EAGAIN: 5140 /* 5141 * MAC modules present. 5142 */ 5143 return (EAGAIN); 5144 case EPERM: 5145 return (EACCES); 5146 default: 5147 return (error); 5148 } 5149 } 5150 5151 /* 5152 * Common filesystem object access control check routine. Accepts a 5153 * vnode's type, "mode", uid and gid, requested access mode, and credentials. 5154 * Returns 0 on success, or an errno on failure. 5155 */ 5156 int 5157 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 5158 accmode_t accmode, struct ucred *cred) 5159 { 5160 accmode_t dac_granted; 5161 accmode_t priv_granted; 5162 5163 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 5164 ("invalid bit in accmode")); 5165 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 5166 ("VAPPEND without VWRITE")); 5167 5168 /* 5169 * Look for a normal, non-privileged way to access the file/directory 5170 * as requested. If it exists, go with that. 5171 */ 5172 5173 dac_granted = 0; 5174 5175 /* Check the owner. */ 5176 if (cred->cr_uid == file_uid) { 5177 dac_granted |= VADMIN; 5178 if (file_mode & S_IXUSR) 5179 dac_granted |= VEXEC; 5180 if (file_mode & S_IRUSR) 5181 dac_granted |= VREAD; 5182 if (file_mode & S_IWUSR) 5183 dac_granted |= (VWRITE | VAPPEND); 5184 5185 if ((accmode & dac_granted) == accmode) 5186 return (0); 5187 5188 goto privcheck; 5189 } 5190 5191 /* Otherwise, check the groups (first match) */ 5192 if (groupmember(file_gid, cred)) { 5193 if (file_mode & S_IXGRP) 5194 dac_granted |= VEXEC; 5195 if (file_mode & S_IRGRP) 5196 dac_granted |= VREAD; 5197 if (file_mode & S_IWGRP) 5198 dac_granted |= (VWRITE | VAPPEND); 5199 5200 if ((accmode & dac_granted) == accmode) 5201 return (0); 5202 5203 goto privcheck; 5204 } 5205 5206 /* Otherwise, check everyone else. */ 5207 if (file_mode & S_IXOTH) 5208 dac_granted |= VEXEC; 5209 if (file_mode & S_IROTH) 5210 dac_granted |= VREAD; 5211 if (file_mode & S_IWOTH) 5212 dac_granted |= (VWRITE | VAPPEND); 5213 if ((accmode & dac_granted) == accmode) 5214 return (0); 5215 5216 privcheck: 5217 /* 5218 * Build a privilege mask to determine if the set of privileges 5219 * satisfies the requirements when combined with the granted mask 5220 * from above. For each privilege, if the privilege is required, 5221 * bitwise or the request type onto the priv_granted mask. 5222 */ 5223 priv_granted = 0; 5224 5225 if (type == VDIR) { 5226 /* 5227 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 5228 * requests, instead of PRIV_VFS_EXEC. 5229 */ 5230 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 5231 !priv_check_cred(cred, PRIV_VFS_LOOKUP)) 5232 priv_granted |= VEXEC; 5233 } else { 5234 /* 5235 * Ensure that at least one execute bit is on. Otherwise, 5236 * a privileged user will always succeed, and we don't want 5237 * this to happen unless the file really is executable. 5238 */ 5239 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 5240 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 5241 !priv_check_cred(cred, PRIV_VFS_EXEC)) 5242 priv_granted |= VEXEC; 5243 } 5244 5245 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 5246 !priv_check_cred(cred, PRIV_VFS_READ)) 5247 priv_granted |= VREAD; 5248 5249 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 5250 !priv_check_cred(cred, PRIV_VFS_WRITE)) 5251 priv_granted |= (VWRITE | VAPPEND); 5252 5253 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 5254 !priv_check_cred(cred, PRIV_VFS_ADMIN)) 5255 priv_granted |= VADMIN; 5256 5257 if ((accmode & (priv_granted | dac_granted)) == accmode) { 5258 return (0); 5259 } 5260 5261 return ((accmode & VADMIN) ? EPERM : EACCES); 5262 } 5263 5264 /* 5265 * Credential check based on process requesting service, and per-attribute 5266 * permissions. 5267 */ 5268 int 5269 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 5270 struct thread *td, accmode_t accmode) 5271 { 5272 5273 /* 5274 * Kernel-invoked always succeeds. 5275 */ 5276 if (cred == NOCRED) 5277 return (0); 5278 5279 /* 5280 * Do not allow privileged processes in jail to directly manipulate 5281 * system attributes. 5282 */ 5283 switch (attrnamespace) { 5284 case EXTATTR_NAMESPACE_SYSTEM: 5285 /* Potentially should be: return (EPERM); */ 5286 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM)); 5287 case EXTATTR_NAMESPACE_USER: 5288 return (VOP_ACCESS(vp, accmode, cred, td)); 5289 default: 5290 return (EPERM); 5291 } 5292 } 5293 5294 #ifdef DEBUG_VFS_LOCKS 5295 /* 5296 * This only exists to suppress warnings from unlocked specfs accesses. It is 5297 * no longer ok to have an unlocked VFS. 5298 */ 5299 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \ 5300 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 5301 5302 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 5303 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, 5304 "Drop into debugger on lock violation"); 5305 5306 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 5307 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 5308 0, "Check for interlock across VOPs"); 5309 5310 int vfs_badlock_print = 1; /* Print lock violations. */ 5311 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 5312 0, "Print lock violations"); 5313 5314 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */ 5315 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode, 5316 0, "Print vnode details on lock violations"); 5317 5318 #ifdef KDB 5319 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 5320 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, 5321 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations"); 5322 #endif 5323 5324 static void 5325 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 5326 { 5327 5328 #ifdef KDB 5329 if (vfs_badlock_backtrace) 5330 kdb_backtrace(); 5331 #endif 5332 if (vfs_badlock_vnode) 5333 vn_printf(vp, "vnode "); 5334 if (vfs_badlock_print) 5335 printf("%s: %p %s\n", str, (void *)vp, msg); 5336 if (vfs_badlock_ddb) 5337 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 5338 } 5339 5340 void 5341 assert_vi_locked(struct vnode *vp, const char *str) 5342 { 5343 5344 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 5345 vfs_badlock("interlock is not locked but should be", str, vp); 5346 } 5347 5348 void 5349 assert_vi_unlocked(struct vnode *vp, const char *str) 5350 { 5351 5352 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 5353 vfs_badlock("interlock is locked but should not be", str, vp); 5354 } 5355 5356 void 5357 assert_vop_locked(struct vnode *vp, const char *str) 5358 { 5359 int locked; 5360 5361 if (!IGNORE_LOCK(vp)) { 5362 locked = VOP_ISLOCKED(vp); 5363 if (locked == 0 || locked == LK_EXCLOTHER) 5364 vfs_badlock("is not locked but should be", str, vp); 5365 } 5366 } 5367 5368 void 5369 assert_vop_unlocked(struct vnode *vp, const char *str) 5370 { 5371 5372 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 5373 vfs_badlock("is locked but should not be", str, vp); 5374 } 5375 5376 void 5377 assert_vop_elocked(struct vnode *vp, const char *str) 5378 { 5379 5380 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 5381 vfs_badlock("is not exclusive locked but should be", str, vp); 5382 } 5383 #endif /* DEBUG_VFS_LOCKS */ 5384 5385 void 5386 vop_rename_fail(struct vop_rename_args *ap) 5387 { 5388 5389 if (ap->a_tvp != NULL) 5390 vput(ap->a_tvp); 5391 if (ap->a_tdvp == ap->a_tvp) 5392 vrele(ap->a_tdvp); 5393 else 5394 vput(ap->a_tdvp); 5395 vrele(ap->a_fdvp); 5396 vrele(ap->a_fvp); 5397 } 5398 5399 void 5400 vop_rename_pre(void *ap) 5401 { 5402 struct vop_rename_args *a = ap; 5403 5404 #ifdef DEBUG_VFS_LOCKS 5405 if (a->a_tvp) 5406 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 5407 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 5408 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 5409 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 5410 5411 /* Check the source (from). */ 5412 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock && 5413 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock)) 5414 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 5415 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock) 5416 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 5417 5418 /* Check the target. */ 5419 if (a->a_tvp) 5420 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 5421 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 5422 #endif 5423 /* 5424 * It may be tempting to add vn_seqc_write_begin/end calls here and 5425 * in vop_rename_post but that's not going to work out since some 5426 * filesystems relookup vnodes mid-rename. This is probably a bug. 5427 * 5428 * For now filesystems are expected to do the relevant calls after they 5429 * decide what vnodes to operate on. 5430 */ 5431 if (a->a_tdvp != a->a_fdvp) 5432 vhold(a->a_fdvp); 5433 if (a->a_tvp != a->a_fvp) 5434 vhold(a->a_fvp); 5435 vhold(a->a_tdvp); 5436 if (a->a_tvp) 5437 vhold(a->a_tvp); 5438 } 5439 5440 #ifdef DEBUG_VFS_LOCKS 5441 void 5442 vop_fplookup_vexec_debugpre(void *ap __unused) 5443 { 5444 5445 VFS_SMR_ASSERT_ENTERED(); 5446 } 5447 5448 void 5449 vop_fplookup_vexec_debugpost(void *ap __unused, int rc __unused) 5450 { 5451 5452 VFS_SMR_ASSERT_ENTERED(); 5453 } 5454 5455 void 5456 vop_strategy_debugpre(void *ap) 5457 { 5458 struct vop_strategy_args *a; 5459 struct buf *bp; 5460 5461 a = ap; 5462 bp = a->a_bp; 5463 5464 /* 5465 * Cluster ops lock their component buffers but not the IO container. 5466 */ 5467 if ((bp->b_flags & B_CLUSTER) != 0) 5468 return; 5469 5470 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) { 5471 if (vfs_badlock_print) 5472 printf( 5473 "VOP_STRATEGY: bp is not locked but should be\n"); 5474 if (vfs_badlock_ddb) 5475 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 5476 } 5477 } 5478 5479 void 5480 vop_lock_debugpre(void *ap) 5481 { 5482 struct vop_lock1_args *a = ap; 5483 5484 if ((a->a_flags & LK_INTERLOCK) == 0) 5485 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5486 else 5487 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 5488 } 5489 5490 void 5491 vop_lock_debugpost(void *ap, int rc) 5492 { 5493 struct vop_lock1_args *a = ap; 5494 5495 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 5496 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0) 5497 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 5498 } 5499 5500 void 5501 vop_unlock_debugpre(void *ap) 5502 { 5503 struct vop_unlock_args *a = ap; 5504 5505 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 5506 } 5507 5508 void 5509 vop_need_inactive_debugpre(void *ap) 5510 { 5511 struct vop_need_inactive_args *a = ap; 5512 5513 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5514 } 5515 5516 void 5517 vop_need_inactive_debugpost(void *ap, int rc) 5518 { 5519 struct vop_need_inactive_args *a = ap; 5520 5521 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE"); 5522 } 5523 #endif 5524 5525 void 5526 vop_create_pre(void *ap) 5527 { 5528 struct vop_create_args *a; 5529 struct vnode *dvp; 5530 5531 a = ap; 5532 dvp = a->a_dvp; 5533 vn_seqc_write_begin(dvp); 5534 } 5535 5536 void 5537 vop_create_post(void *ap, int rc) 5538 { 5539 struct vop_create_args *a; 5540 struct vnode *dvp; 5541 5542 a = ap; 5543 dvp = a->a_dvp; 5544 vn_seqc_write_end(dvp); 5545 if (!rc) 5546 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE); 5547 } 5548 5549 void 5550 vop_whiteout_pre(void *ap) 5551 { 5552 struct vop_whiteout_args *a; 5553 struct vnode *dvp; 5554 5555 a = ap; 5556 dvp = a->a_dvp; 5557 vn_seqc_write_begin(dvp); 5558 } 5559 5560 void 5561 vop_whiteout_post(void *ap, int rc) 5562 { 5563 struct vop_whiteout_args *a; 5564 struct vnode *dvp; 5565 5566 a = ap; 5567 dvp = a->a_dvp; 5568 vn_seqc_write_end(dvp); 5569 } 5570 5571 void 5572 vop_deleteextattr_pre(void *ap) 5573 { 5574 struct vop_deleteextattr_args *a; 5575 struct vnode *vp; 5576 5577 a = ap; 5578 vp = a->a_vp; 5579 vn_seqc_write_begin(vp); 5580 } 5581 5582 void 5583 vop_deleteextattr_post(void *ap, int rc) 5584 { 5585 struct vop_deleteextattr_args *a; 5586 struct vnode *vp; 5587 5588 a = ap; 5589 vp = a->a_vp; 5590 vn_seqc_write_end(vp); 5591 if (!rc) 5592 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 5593 } 5594 5595 void 5596 vop_link_pre(void *ap) 5597 { 5598 struct vop_link_args *a; 5599 struct vnode *vp, *tdvp; 5600 5601 a = ap; 5602 vp = a->a_vp; 5603 tdvp = a->a_tdvp; 5604 vn_seqc_write_begin(vp); 5605 vn_seqc_write_begin(tdvp); 5606 } 5607 5608 void 5609 vop_link_post(void *ap, int rc) 5610 { 5611 struct vop_link_args *a; 5612 struct vnode *vp, *tdvp; 5613 5614 a = ap; 5615 vp = a->a_vp; 5616 tdvp = a->a_tdvp; 5617 vn_seqc_write_end(vp); 5618 vn_seqc_write_end(tdvp); 5619 if (!rc) { 5620 VFS_KNOTE_LOCKED(vp, NOTE_LINK); 5621 VFS_KNOTE_LOCKED(tdvp, NOTE_WRITE); 5622 } 5623 } 5624 5625 void 5626 vop_mkdir_pre(void *ap) 5627 { 5628 struct vop_mkdir_args *a; 5629 struct vnode *dvp; 5630 5631 a = ap; 5632 dvp = a->a_dvp; 5633 vn_seqc_write_begin(dvp); 5634 } 5635 5636 void 5637 vop_mkdir_post(void *ap, int rc) 5638 { 5639 struct vop_mkdir_args *a; 5640 struct vnode *dvp; 5641 5642 a = ap; 5643 dvp = a->a_dvp; 5644 vn_seqc_write_end(dvp); 5645 if (!rc) 5646 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK); 5647 } 5648 5649 #ifdef DEBUG_VFS_LOCKS 5650 void 5651 vop_mkdir_debugpost(void *ap, int rc) 5652 { 5653 struct vop_mkdir_args *a; 5654 5655 a = ap; 5656 if (!rc) 5657 cache_validate(a->a_dvp, *a->a_vpp, a->a_cnp); 5658 } 5659 #endif 5660 5661 void 5662 vop_mknod_pre(void *ap) 5663 { 5664 struct vop_mknod_args *a; 5665 struct vnode *dvp; 5666 5667 a = ap; 5668 dvp = a->a_dvp; 5669 vn_seqc_write_begin(dvp); 5670 } 5671 5672 void 5673 vop_mknod_post(void *ap, int rc) 5674 { 5675 struct vop_mknod_args *a; 5676 struct vnode *dvp; 5677 5678 a = ap; 5679 dvp = a->a_dvp; 5680 vn_seqc_write_end(dvp); 5681 if (!rc) 5682 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE); 5683 } 5684 5685 void 5686 vop_reclaim_post(void *ap, int rc) 5687 { 5688 struct vop_reclaim_args *a; 5689 struct vnode *vp; 5690 5691 a = ap; 5692 vp = a->a_vp; 5693 ASSERT_VOP_IN_SEQC(vp); 5694 if (!rc) 5695 VFS_KNOTE_LOCKED(vp, NOTE_REVOKE); 5696 } 5697 5698 void 5699 vop_remove_pre(void *ap) 5700 { 5701 struct vop_remove_args *a; 5702 struct vnode *dvp, *vp; 5703 5704 a = ap; 5705 dvp = a->a_dvp; 5706 vp = a->a_vp; 5707 vn_seqc_write_begin(dvp); 5708 vn_seqc_write_begin(vp); 5709 } 5710 5711 void 5712 vop_remove_post(void *ap, int rc) 5713 { 5714 struct vop_remove_args *a; 5715 struct vnode *dvp, *vp; 5716 5717 a = ap; 5718 dvp = a->a_dvp; 5719 vp = a->a_vp; 5720 vn_seqc_write_end(dvp); 5721 vn_seqc_write_end(vp); 5722 if (!rc) { 5723 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE); 5724 VFS_KNOTE_LOCKED(vp, NOTE_DELETE); 5725 } 5726 } 5727 5728 void 5729 vop_rename_post(void *ap, int rc) 5730 { 5731 struct vop_rename_args *a = ap; 5732 long hint; 5733 5734 if (!rc) { 5735 hint = NOTE_WRITE; 5736 if (a->a_fdvp == a->a_tdvp) { 5737 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR) 5738 hint |= NOTE_LINK; 5739 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5740 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5741 } else { 5742 hint |= NOTE_EXTEND; 5743 if (a->a_fvp->v_type == VDIR) 5744 hint |= NOTE_LINK; 5745 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint); 5746 5747 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL && 5748 a->a_tvp->v_type == VDIR) 5749 hint &= ~NOTE_LINK; 5750 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint); 5751 } 5752 5753 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 5754 if (a->a_tvp) 5755 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 5756 } 5757 if (a->a_tdvp != a->a_fdvp) 5758 vdrop(a->a_fdvp); 5759 if (a->a_tvp != a->a_fvp) 5760 vdrop(a->a_fvp); 5761 vdrop(a->a_tdvp); 5762 if (a->a_tvp) 5763 vdrop(a->a_tvp); 5764 } 5765 5766 void 5767 vop_rmdir_pre(void *ap) 5768 { 5769 struct vop_rmdir_args *a; 5770 struct vnode *dvp, *vp; 5771 5772 a = ap; 5773 dvp = a->a_dvp; 5774 vp = a->a_vp; 5775 vn_seqc_write_begin(dvp); 5776 vn_seqc_write_begin(vp); 5777 } 5778 5779 void 5780 vop_rmdir_post(void *ap, int rc) 5781 { 5782 struct vop_rmdir_args *a; 5783 struct vnode *dvp, *vp; 5784 5785 a = ap; 5786 dvp = a->a_dvp; 5787 vp = a->a_vp; 5788 vn_seqc_write_end(dvp); 5789 vn_seqc_write_end(vp); 5790 if (!rc) { 5791 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK); 5792 VFS_KNOTE_LOCKED(vp, NOTE_DELETE); 5793 } 5794 } 5795 5796 void 5797 vop_setattr_pre(void *ap) 5798 { 5799 struct vop_setattr_args *a; 5800 struct vnode *vp; 5801 5802 a = ap; 5803 vp = a->a_vp; 5804 vn_seqc_write_begin(vp); 5805 } 5806 5807 void 5808 vop_setattr_post(void *ap, int rc) 5809 { 5810 struct vop_setattr_args *a; 5811 struct vnode *vp; 5812 5813 a = ap; 5814 vp = a->a_vp; 5815 vn_seqc_write_end(vp); 5816 if (!rc) 5817 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB); 5818 } 5819 5820 void 5821 vop_setacl_pre(void *ap) 5822 { 5823 struct vop_setacl_args *a; 5824 struct vnode *vp; 5825 5826 a = ap; 5827 vp = a->a_vp; 5828 vn_seqc_write_begin(vp); 5829 } 5830 5831 void 5832 vop_setacl_post(void *ap, int rc __unused) 5833 { 5834 struct vop_setacl_args *a; 5835 struct vnode *vp; 5836 5837 a = ap; 5838 vp = a->a_vp; 5839 vn_seqc_write_end(vp); 5840 } 5841 5842 void 5843 vop_setextattr_pre(void *ap) 5844 { 5845 struct vop_setextattr_args *a; 5846 struct vnode *vp; 5847 5848 a = ap; 5849 vp = a->a_vp; 5850 vn_seqc_write_begin(vp); 5851 } 5852 5853 void 5854 vop_setextattr_post(void *ap, int rc) 5855 { 5856 struct vop_setextattr_args *a; 5857 struct vnode *vp; 5858 5859 a = ap; 5860 vp = a->a_vp; 5861 vn_seqc_write_end(vp); 5862 if (!rc) 5863 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB); 5864 } 5865 5866 void 5867 vop_symlink_pre(void *ap) 5868 { 5869 struct vop_symlink_args *a; 5870 struct vnode *dvp; 5871 5872 a = ap; 5873 dvp = a->a_dvp; 5874 vn_seqc_write_begin(dvp); 5875 } 5876 5877 void 5878 vop_symlink_post(void *ap, int rc) 5879 { 5880 struct vop_symlink_args *a; 5881 struct vnode *dvp; 5882 5883 a = ap; 5884 dvp = a->a_dvp; 5885 vn_seqc_write_end(dvp); 5886 if (!rc) 5887 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE); 5888 } 5889 5890 void 5891 vop_open_post(void *ap, int rc) 5892 { 5893 struct vop_open_args *a = ap; 5894 5895 if (!rc) 5896 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN); 5897 } 5898 5899 void 5900 vop_close_post(void *ap, int rc) 5901 { 5902 struct vop_close_args *a = ap; 5903 5904 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */ 5905 !VN_IS_DOOMED(a->a_vp))) { 5906 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ? 5907 NOTE_CLOSE_WRITE : NOTE_CLOSE); 5908 } 5909 } 5910 5911 void 5912 vop_read_post(void *ap, int rc) 5913 { 5914 struct vop_read_args *a = ap; 5915 5916 if (!rc) 5917 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5918 } 5919 5920 void 5921 vop_read_pgcache_post(void *ap, int rc) 5922 { 5923 struct vop_read_pgcache_args *a = ap; 5924 5925 if (!rc) 5926 VFS_KNOTE_UNLOCKED(a->a_vp, NOTE_READ); 5927 } 5928 5929 void 5930 vop_readdir_post(void *ap, int rc) 5931 { 5932 struct vop_readdir_args *a = ap; 5933 5934 if (!rc) 5935 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ); 5936 } 5937 5938 static struct knlist fs_knlist; 5939 5940 static void 5941 vfs_event_init(void *arg) 5942 { 5943 knlist_init_mtx(&fs_knlist, NULL); 5944 } 5945 /* XXX - correct order? */ 5946 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 5947 5948 void 5949 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused) 5950 { 5951 5952 KNOTE_UNLOCKED(&fs_knlist, event); 5953 } 5954 5955 static int filt_fsattach(struct knote *kn); 5956 static void filt_fsdetach(struct knote *kn); 5957 static int filt_fsevent(struct knote *kn, long hint); 5958 5959 struct filterops fs_filtops = { 5960 .f_isfd = 0, 5961 .f_attach = filt_fsattach, 5962 .f_detach = filt_fsdetach, 5963 .f_event = filt_fsevent 5964 }; 5965 5966 static int 5967 filt_fsattach(struct knote *kn) 5968 { 5969 5970 kn->kn_flags |= EV_CLEAR; 5971 knlist_add(&fs_knlist, kn, 0); 5972 return (0); 5973 } 5974 5975 static void 5976 filt_fsdetach(struct knote *kn) 5977 { 5978 5979 knlist_remove(&fs_knlist, kn, 0); 5980 } 5981 5982 static int 5983 filt_fsevent(struct knote *kn, long hint) 5984 { 5985 5986 kn->kn_fflags |= hint; 5987 return (kn->kn_fflags != 0); 5988 } 5989 5990 static int 5991 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 5992 { 5993 struct vfsidctl vc; 5994 int error; 5995 struct mount *mp; 5996 5997 error = SYSCTL_IN(req, &vc, sizeof(vc)); 5998 if (error) 5999 return (error); 6000 if (vc.vc_vers != VFS_CTL_VERS1) 6001 return (EINVAL); 6002 mp = vfs_getvfs(&vc.vc_fsid); 6003 if (mp == NULL) 6004 return (ENOENT); 6005 /* ensure that a specific sysctl goes to the right filesystem. */ 6006 if (strcmp(vc.vc_fstypename, "*") != 0 && 6007 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 6008 vfs_rel(mp); 6009 return (EINVAL); 6010 } 6011 VCTLTOREQ(&vc, req); 6012 error = VFS_SYSCTL(mp, vc.vc_op, req); 6013 vfs_rel(mp); 6014 return (error); 6015 } 6016 6017 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR, 6018 NULL, 0, sysctl_vfs_ctl, "", 6019 "Sysctl by fsid"); 6020 6021 /* 6022 * Function to initialize a va_filerev field sensibly. 6023 * XXX: Wouldn't a random number make a lot more sense ?? 6024 */ 6025 u_quad_t 6026 init_va_filerev(void) 6027 { 6028 struct bintime bt; 6029 6030 getbinuptime(&bt); 6031 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 6032 } 6033 6034 static int filt_vfsread(struct knote *kn, long hint); 6035 static int filt_vfswrite(struct knote *kn, long hint); 6036 static int filt_vfsvnode(struct knote *kn, long hint); 6037 static void filt_vfsdetach(struct knote *kn); 6038 static struct filterops vfsread_filtops = { 6039 .f_isfd = 1, 6040 .f_detach = filt_vfsdetach, 6041 .f_event = filt_vfsread 6042 }; 6043 static struct filterops vfswrite_filtops = { 6044 .f_isfd = 1, 6045 .f_detach = filt_vfsdetach, 6046 .f_event = filt_vfswrite 6047 }; 6048 static struct filterops vfsvnode_filtops = { 6049 .f_isfd = 1, 6050 .f_detach = filt_vfsdetach, 6051 .f_event = filt_vfsvnode 6052 }; 6053 6054 static void 6055 vfs_knllock(void *arg) 6056 { 6057 struct vnode *vp = arg; 6058 6059 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 6060 } 6061 6062 static void 6063 vfs_knlunlock(void *arg) 6064 { 6065 struct vnode *vp = arg; 6066 6067 VOP_UNLOCK(vp); 6068 } 6069 6070 static void 6071 vfs_knl_assert_lock(void *arg, int what) 6072 { 6073 #ifdef DEBUG_VFS_LOCKS 6074 struct vnode *vp = arg; 6075 6076 if (what == LA_LOCKED) 6077 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked"); 6078 else 6079 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked"); 6080 #endif 6081 } 6082 6083 int 6084 vfs_kqfilter(struct vop_kqfilter_args *ap) 6085 { 6086 struct vnode *vp = ap->a_vp; 6087 struct knote *kn = ap->a_kn; 6088 struct knlist *knl; 6089 6090 switch (kn->kn_filter) { 6091 case EVFILT_READ: 6092 kn->kn_fop = &vfsread_filtops; 6093 break; 6094 case EVFILT_WRITE: 6095 kn->kn_fop = &vfswrite_filtops; 6096 break; 6097 case EVFILT_VNODE: 6098 kn->kn_fop = &vfsvnode_filtops; 6099 break; 6100 default: 6101 return (EINVAL); 6102 } 6103 6104 kn->kn_hook = (caddr_t)vp; 6105 6106 v_addpollinfo(vp); 6107 if (vp->v_pollinfo == NULL) 6108 return (ENOMEM); 6109 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 6110 vhold(vp); 6111 knlist_add(knl, kn, 0); 6112 6113 return (0); 6114 } 6115 6116 /* 6117 * Detach knote from vnode 6118 */ 6119 static void 6120 filt_vfsdetach(struct knote *kn) 6121 { 6122 struct vnode *vp = (struct vnode *)kn->kn_hook; 6123 6124 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 6125 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 6126 vdrop(vp); 6127 } 6128 6129 /*ARGSUSED*/ 6130 static int 6131 filt_vfsread(struct knote *kn, long hint) 6132 { 6133 struct vnode *vp = (struct vnode *)kn->kn_hook; 6134 struct vattr va; 6135 int res; 6136 6137 /* 6138 * filesystem is gone, so set the EOF flag and schedule 6139 * the knote for deletion. 6140 */ 6141 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 6142 VI_LOCK(vp); 6143 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 6144 VI_UNLOCK(vp); 6145 return (1); 6146 } 6147 6148 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 6149 return (0); 6150 6151 VI_LOCK(vp); 6152 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 6153 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0; 6154 VI_UNLOCK(vp); 6155 return (res); 6156 } 6157 6158 /*ARGSUSED*/ 6159 static int 6160 filt_vfswrite(struct knote *kn, long hint) 6161 { 6162 struct vnode *vp = (struct vnode *)kn->kn_hook; 6163 6164 VI_LOCK(vp); 6165 6166 /* 6167 * filesystem is gone, so set the EOF flag and schedule 6168 * the knote for deletion. 6169 */ 6170 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) 6171 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 6172 6173 kn->kn_data = 0; 6174 VI_UNLOCK(vp); 6175 return (1); 6176 } 6177 6178 static int 6179 filt_vfsvnode(struct knote *kn, long hint) 6180 { 6181 struct vnode *vp = (struct vnode *)kn->kn_hook; 6182 int res; 6183 6184 VI_LOCK(vp); 6185 if (kn->kn_sfflags & hint) 6186 kn->kn_fflags |= hint; 6187 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) { 6188 kn->kn_flags |= EV_EOF; 6189 VI_UNLOCK(vp); 6190 return (1); 6191 } 6192 res = (kn->kn_fflags != 0); 6193 VI_UNLOCK(vp); 6194 return (res); 6195 } 6196 6197 /* 6198 * Returns whether the directory is empty or not. 6199 * If it is empty, the return value is 0; otherwise 6200 * the return value is an error value (which may 6201 * be ENOTEMPTY). 6202 */ 6203 int 6204 vfs_emptydir(struct vnode *vp) 6205 { 6206 struct uio uio; 6207 struct iovec iov; 6208 struct dirent *dirent, *dp, *endp; 6209 int error, eof; 6210 6211 error = 0; 6212 eof = 0; 6213 6214 ASSERT_VOP_LOCKED(vp, "vfs_emptydir"); 6215 6216 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK); 6217 iov.iov_base = dirent; 6218 iov.iov_len = sizeof(struct dirent); 6219 6220 uio.uio_iov = &iov; 6221 uio.uio_iovcnt = 1; 6222 uio.uio_offset = 0; 6223 uio.uio_resid = sizeof(struct dirent); 6224 uio.uio_segflg = UIO_SYSSPACE; 6225 uio.uio_rw = UIO_READ; 6226 uio.uio_td = curthread; 6227 6228 while (eof == 0 && error == 0) { 6229 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof, 6230 NULL, NULL); 6231 if (error != 0) 6232 break; 6233 endp = (void *)((uint8_t *)dirent + 6234 sizeof(struct dirent) - uio.uio_resid); 6235 for (dp = dirent; dp < endp; 6236 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) { 6237 if (dp->d_type == DT_WHT) 6238 continue; 6239 if (dp->d_namlen == 0) 6240 continue; 6241 if (dp->d_type != DT_DIR && 6242 dp->d_type != DT_UNKNOWN) { 6243 error = ENOTEMPTY; 6244 break; 6245 } 6246 if (dp->d_namlen > 2) { 6247 error = ENOTEMPTY; 6248 break; 6249 } 6250 if (dp->d_namlen == 1 && 6251 dp->d_name[0] != '.') { 6252 error = ENOTEMPTY; 6253 break; 6254 } 6255 if (dp->d_namlen == 2 && 6256 dp->d_name[1] != '.') { 6257 error = ENOTEMPTY; 6258 break; 6259 } 6260 uio.uio_resid = sizeof(struct dirent); 6261 } 6262 } 6263 free(dirent, M_TEMP); 6264 return (error); 6265 } 6266 6267 int 6268 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 6269 { 6270 int error; 6271 6272 if (dp->d_reclen > ap->a_uio->uio_resid) 6273 return (ENAMETOOLONG); 6274 error = uiomove(dp, dp->d_reclen, ap->a_uio); 6275 if (error) { 6276 if (ap->a_ncookies != NULL) { 6277 if (ap->a_cookies != NULL) 6278 free(ap->a_cookies, M_TEMP); 6279 ap->a_cookies = NULL; 6280 *ap->a_ncookies = 0; 6281 } 6282 return (error); 6283 } 6284 if (ap->a_ncookies == NULL) 6285 return (0); 6286 6287 KASSERT(ap->a_cookies, 6288 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 6289 6290 *ap->a_cookies = realloc(*ap->a_cookies, 6291 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 6292 (*ap->a_cookies)[*ap->a_ncookies] = off; 6293 *ap->a_ncookies += 1; 6294 return (0); 6295 } 6296 6297 /* 6298 * The purpose of this routine is to remove granularity from accmode_t, 6299 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE, 6300 * VADMIN and VAPPEND. 6301 * 6302 * If it returns 0, the caller is supposed to continue with the usual 6303 * access checks using 'accmode' as modified by this routine. If it 6304 * returns nonzero value, the caller is supposed to return that value 6305 * as errno. 6306 * 6307 * Note that after this routine runs, accmode may be zero. 6308 */ 6309 int 6310 vfs_unixify_accmode(accmode_t *accmode) 6311 { 6312 /* 6313 * There is no way to specify explicit "deny" rule using 6314 * file mode or POSIX.1e ACLs. 6315 */ 6316 if (*accmode & VEXPLICIT_DENY) { 6317 *accmode = 0; 6318 return (0); 6319 } 6320 6321 /* 6322 * None of these can be translated into usual access bits. 6323 * Also, the common case for NFSv4 ACLs is to not contain 6324 * either of these bits. Caller should check for VWRITE 6325 * on the containing directory instead. 6326 */ 6327 if (*accmode & (VDELETE_CHILD | VDELETE)) 6328 return (EPERM); 6329 6330 if (*accmode & VADMIN_PERMS) { 6331 *accmode &= ~VADMIN_PERMS; 6332 *accmode |= VADMIN; 6333 } 6334 6335 /* 6336 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL 6337 * or VSYNCHRONIZE using file mode or POSIX.1e ACL. 6338 */ 6339 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE); 6340 6341 return (0); 6342 } 6343 6344 /* 6345 * Clear out a doomed vnode (if any) and replace it with a new one as long 6346 * as the fs is not being unmounted. Return the root vnode to the caller. 6347 */ 6348 static int __noinline 6349 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp) 6350 { 6351 struct vnode *vp; 6352 int error; 6353 6354 restart: 6355 if (mp->mnt_rootvnode != NULL) { 6356 MNT_ILOCK(mp); 6357 vp = mp->mnt_rootvnode; 6358 if (vp != NULL) { 6359 if (!VN_IS_DOOMED(vp)) { 6360 vrefact(vp); 6361 MNT_IUNLOCK(mp); 6362 error = vn_lock(vp, flags); 6363 if (error == 0) { 6364 *vpp = vp; 6365 return (0); 6366 } 6367 vrele(vp); 6368 goto restart; 6369 } 6370 /* 6371 * Clear the old one. 6372 */ 6373 mp->mnt_rootvnode = NULL; 6374 } 6375 MNT_IUNLOCK(mp); 6376 if (vp != NULL) { 6377 vfs_op_barrier_wait(mp); 6378 vrele(vp); 6379 } 6380 } 6381 error = VFS_CACHEDROOT(mp, flags, vpp); 6382 if (error != 0) 6383 return (error); 6384 if (mp->mnt_vfs_ops == 0) { 6385 MNT_ILOCK(mp); 6386 if (mp->mnt_vfs_ops != 0) { 6387 MNT_IUNLOCK(mp); 6388 return (0); 6389 } 6390 if (mp->mnt_rootvnode == NULL) { 6391 vrefact(*vpp); 6392 mp->mnt_rootvnode = *vpp; 6393 } else { 6394 if (mp->mnt_rootvnode != *vpp) { 6395 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) { 6396 panic("%s: mismatch between vnode returned " 6397 " by VFS_CACHEDROOT and the one cached " 6398 " (%p != %p)", 6399 __func__, *vpp, mp->mnt_rootvnode); 6400 } 6401 } 6402 } 6403 MNT_IUNLOCK(mp); 6404 } 6405 return (0); 6406 } 6407 6408 int 6409 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp) 6410 { 6411 struct mount_pcpu *mpcpu; 6412 struct vnode *vp; 6413 int error; 6414 6415 if (!vfs_op_thread_enter(mp, mpcpu)) 6416 return (vfs_cache_root_fallback(mp, flags, vpp)); 6417 vp = atomic_load_ptr(&mp->mnt_rootvnode); 6418 if (vp == NULL || VN_IS_DOOMED(vp)) { 6419 vfs_op_thread_exit(mp, mpcpu); 6420 return (vfs_cache_root_fallback(mp, flags, vpp)); 6421 } 6422 vrefact(vp); 6423 vfs_op_thread_exit(mp, mpcpu); 6424 error = vn_lock(vp, flags); 6425 if (error != 0) { 6426 vrele(vp); 6427 return (vfs_cache_root_fallback(mp, flags, vpp)); 6428 } 6429 *vpp = vp; 6430 return (0); 6431 } 6432 6433 struct vnode * 6434 vfs_cache_root_clear(struct mount *mp) 6435 { 6436 struct vnode *vp; 6437 6438 /* 6439 * ops > 0 guarantees there is nobody who can see this vnode 6440 */ 6441 MPASS(mp->mnt_vfs_ops > 0); 6442 vp = mp->mnt_rootvnode; 6443 if (vp != NULL) 6444 vn_seqc_write_begin(vp); 6445 mp->mnt_rootvnode = NULL; 6446 return (vp); 6447 } 6448 6449 void 6450 vfs_cache_root_set(struct mount *mp, struct vnode *vp) 6451 { 6452 6453 MPASS(mp->mnt_vfs_ops > 0); 6454 vrefact(vp); 6455 mp->mnt_rootvnode = vp; 6456 } 6457 6458 /* 6459 * These are helper functions for filesystems to traverse all 6460 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h. 6461 * 6462 * This interface replaces MNT_VNODE_FOREACH. 6463 */ 6464 6465 struct vnode * 6466 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp) 6467 { 6468 struct vnode *vp; 6469 6470 if (should_yield()) 6471 kern_yield(PRI_USER); 6472 MNT_ILOCK(mp); 6473 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6474 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL; 6475 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 6476 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 6477 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 6478 continue; 6479 VI_LOCK(vp); 6480 if (VN_IS_DOOMED(vp)) { 6481 VI_UNLOCK(vp); 6482 continue; 6483 } 6484 break; 6485 } 6486 if (vp == NULL) { 6487 __mnt_vnode_markerfree_all(mvp, mp); 6488 /* MNT_IUNLOCK(mp); -- done in above function */ 6489 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 6490 return (NULL); 6491 } 6492 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 6493 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 6494 MNT_IUNLOCK(mp); 6495 return (vp); 6496 } 6497 6498 struct vnode * 6499 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp) 6500 { 6501 struct vnode *vp; 6502 6503 *mvp = vn_alloc_marker(mp); 6504 MNT_ILOCK(mp); 6505 MNT_REF(mp); 6506 6507 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 6508 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */ 6509 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp)) 6510 continue; 6511 VI_LOCK(vp); 6512 if (VN_IS_DOOMED(vp)) { 6513 VI_UNLOCK(vp); 6514 continue; 6515 } 6516 break; 6517 } 6518 if (vp == NULL) { 6519 MNT_REL(mp); 6520 MNT_IUNLOCK(mp); 6521 vn_free_marker(*mvp); 6522 *mvp = NULL; 6523 return (NULL); 6524 } 6525 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 6526 MNT_IUNLOCK(mp); 6527 return (vp); 6528 } 6529 6530 void 6531 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp) 6532 { 6533 6534 if (*mvp == NULL) { 6535 MNT_IUNLOCK(mp); 6536 return; 6537 } 6538 6539 mtx_assert(MNT_MTX(mp), MA_OWNED); 6540 6541 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6542 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 6543 MNT_REL(mp); 6544 MNT_IUNLOCK(mp); 6545 vn_free_marker(*mvp); 6546 *mvp = NULL; 6547 } 6548 6549 /* 6550 * These are helper functions for filesystems to traverse their 6551 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h 6552 */ 6553 static void 6554 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp) 6555 { 6556 6557 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6558 6559 MNT_ILOCK(mp); 6560 MNT_REL(mp); 6561 MNT_IUNLOCK(mp); 6562 vn_free_marker(*mvp); 6563 *mvp = NULL; 6564 } 6565 6566 /* 6567 * Relock the mp mount vnode list lock with the vp vnode interlock in the 6568 * conventional lock order during mnt_vnode_next_lazy iteration. 6569 * 6570 * On entry, the mount vnode list lock is held and the vnode interlock is not. 6571 * The list lock is dropped and reacquired. On success, both locks are held. 6572 * On failure, the mount vnode list lock is held but the vnode interlock is 6573 * not, and the procedure may have yielded. 6574 */ 6575 static bool 6576 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp, 6577 struct vnode *vp) 6578 { 6579 6580 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER && 6581 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp, 6582 ("%s: bad marker", __func__)); 6583 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp, 6584 ("%s: inappropriate vnode", __func__)); 6585 ASSERT_VI_UNLOCKED(vp, __func__); 6586 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6587 6588 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist); 6589 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist); 6590 6591 /* 6592 * Note we may be racing against vdrop which transitioned the hold 6593 * count to 0 and now waits for the ->mnt_listmtx lock. This is fine, 6594 * if we are the only user after we get the interlock we will just 6595 * vdrop. 6596 */ 6597 vhold(vp); 6598 mtx_unlock(&mp->mnt_listmtx); 6599 VI_LOCK(vp); 6600 if (VN_IS_DOOMED(vp)) { 6601 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp); 6602 goto out_lost; 6603 } 6604 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp); 6605 /* 6606 * There is nothing to do if we are the last user. 6607 */ 6608 if (!refcount_release_if_not_last(&vp->v_holdcnt)) 6609 goto out_lost; 6610 mtx_lock(&mp->mnt_listmtx); 6611 return (true); 6612 out_lost: 6613 vdropl(vp); 6614 maybe_yield(); 6615 mtx_lock(&mp->mnt_listmtx); 6616 return (false); 6617 } 6618 6619 static struct vnode * 6620 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6621 void *cbarg) 6622 { 6623 struct vnode *vp; 6624 6625 mtx_assert(&mp->mnt_listmtx, MA_OWNED); 6626 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 6627 restart: 6628 vp = TAILQ_NEXT(*mvp, v_lazylist); 6629 while (vp != NULL) { 6630 if (vp->v_type == VMARKER) { 6631 vp = TAILQ_NEXT(vp, v_lazylist); 6632 continue; 6633 } 6634 /* 6635 * See if we want to process the vnode. Note we may encounter a 6636 * long string of vnodes we don't care about and hog the list 6637 * as a result. Check for it and requeue the marker. 6638 */ 6639 VNPASS(!VN_IS_DOOMED(vp), vp); 6640 if (!cb(vp, cbarg)) { 6641 if (!should_yield()) { 6642 vp = TAILQ_NEXT(vp, v_lazylist); 6643 continue; 6644 } 6645 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, 6646 v_lazylist); 6647 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, 6648 v_lazylist); 6649 mtx_unlock(&mp->mnt_listmtx); 6650 kern_yield(PRI_USER); 6651 mtx_lock(&mp->mnt_listmtx); 6652 goto restart; 6653 } 6654 /* 6655 * Try-lock because this is the wrong lock order. 6656 */ 6657 if (!VI_TRYLOCK(vp) && 6658 !mnt_vnode_next_lazy_relock(*mvp, mp, vp)) 6659 goto restart; 6660 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp)); 6661 KASSERT(vp->v_mount == mp || vp->v_mount == NULL, 6662 ("alien vnode on the lazy list %p %p", vp, mp)); 6663 VNPASS(vp->v_mount == mp, vp); 6664 VNPASS(!VN_IS_DOOMED(vp), vp); 6665 break; 6666 } 6667 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist); 6668 6669 /* Check if we are done */ 6670 if (vp == NULL) { 6671 mtx_unlock(&mp->mnt_listmtx); 6672 mnt_vnode_markerfree_lazy(mvp, mp); 6673 return (NULL); 6674 } 6675 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist); 6676 mtx_unlock(&mp->mnt_listmtx); 6677 ASSERT_VI_LOCKED(vp, "lazy iter"); 6678 return (vp); 6679 } 6680 6681 struct vnode * 6682 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6683 void *cbarg) 6684 { 6685 6686 if (should_yield()) 6687 kern_yield(PRI_USER); 6688 mtx_lock(&mp->mnt_listmtx); 6689 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg)); 6690 } 6691 6692 struct vnode * 6693 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb, 6694 void *cbarg) 6695 { 6696 struct vnode *vp; 6697 6698 if (TAILQ_EMPTY(&mp->mnt_lazyvnodelist)) 6699 return (NULL); 6700 6701 *mvp = vn_alloc_marker(mp); 6702 MNT_ILOCK(mp); 6703 MNT_REF(mp); 6704 MNT_IUNLOCK(mp); 6705 6706 mtx_lock(&mp->mnt_listmtx); 6707 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist); 6708 if (vp == NULL) { 6709 mtx_unlock(&mp->mnt_listmtx); 6710 mnt_vnode_markerfree_lazy(mvp, mp); 6711 return (NULL); 6712 } 6713 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist); 6714 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg)); 6715 } 6716 6717 void 6718 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp) 6719 { 6720 6721 if (*mvp == NULL) 6722 return; 6723 6724 mtx_lock(&mp->mnt_listmtx); 6725 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist); 6726 mtx_unlock(&mp->mnt_listmtx); 6727 mnt_vnode_markerfree_lazy(mvp, mp); 6728 } 6729 6730 int 6731 vn_dir_check_exec(struct vnode *vp, struct componentname *cnp) 6732 { 6733 6734 if ((cnp->cn_flags & NOEXECCHECK) != 0) { 6735 cnp->cn_flags &= ~NOEXECCHECK; 6736 return (0); 6737 } 6738 6739 return (VOP_ACCESS(vp, VEXEC, cnp->cn_cred, cnp->cn_thread)); 6740 } 6741 6742 /* 6743 * Do not use this variant unless you have means other than the hold count 6744 * to prevent the vnode from getting freed. 6745 */ 6746 void 6747 vn_seqc_write_begin_unheld_locked(struct vnode *vp) 6748 { 6749 6750 ASSERT_VI_LOCKED(vp, __func__); 6751 VNPASS(vp->v_seqc_users >= 0, vp); 6752 vp->v_seqc_users++; 6753 if (vp->v_seqc_users == 1) 6754 seqc_sleepable_write_begin(&vp->v_seqc); 6755 } 6756 6757 void 6758 vn_seqc_write_begin_locked(struct vnode *vp) 6759 { 6760 6761 ASSERT_VI_LOCKED(vp, __func__); 6762 VNPASS(vp->v_holdcnt > 0, vp); 6763 vn_seqc_write_begin_unheld_locked(vp); 6764 } 6765 6766 void 6767 vn_seqc_write_begin(struct vnode *vp) 6768 { 6769 6770 VI_LOCK(vp); 6771 vn_seqc_write_begin_locked(vp); 6772 VI_UNLOCK(vp); 6773 } 6774 6775 void 6776 vn_seqc_write_begin_unheld(struct vnode *vp) 6777 { 6778 6779 VI_LOCK(vp); 6780 vn_seqc_write_begin_unheld_locked(vp); 6781 VI_UNLOCK(vp); 6782 } 6783 6784 void 6785 vn_seqc_write_end_locked(struct vnode *vp) 6786 { 6787 6788 ASSERT_VI_LOCKED(vp, __func__); 6789 VNPASS(vp->v_seqc_users > 0, vp); 6790 vp->v_seqc_users--; 6791 if (vp->v_seqc_users == 0) 6792 seqc_sleepable_write_end(&vp->v_seqc); 6793 } 6794 6795 void 6796 vn_seqc_write_end(struct vnode *vp) 6797 { 6798 6799 VI_LOCK(vp); 6800 vn_seqc_write_end_locked(vp); 6801 VI_UNLOCK(vp); 6802 } 6803