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