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