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