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