1 /*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 35 */ 36 37 /* 38 * External virtual filesystem routines 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_compat.h" 45 #include "opt_ddb.h" 46 #include "opt_watchdog.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/bio.h> 51 #include <sys/buf.h> 52 #include <sys/condvar.h> 53 #include <sys/conf.h> 54 #include <sys/dirent.h> 55 #include <sys/event.h> 56 #include <sys/eventhandler.h> 57 #include <sys/extattr.h> 58 #include <sys/file.h> 59 #include <sys/fcntl.h> 60 #include <sys/jail.h> 61 #include <sys/kdb.h> 62 #include <sys/kernel.h> 63 #include <sys/kthread.h> 64 #include <sys/lockf.h> 65 #include <sys/malloc.h> 66 #include <sys/mount.h> 67 #include <sys/namei.h> 68 #include <sys/priv.h> 69 #include <sys/reboot.h> 70 #include <sys/sched.h> 71 #include <sys/sleepqueue.h> 72 #include <sys/stat.h> 73 #include <sys/sysctl.h> 74 #include <sys/syslog.h> 75 #include <sys/vmmeter.h> 76 #include <sys/vnode.h> 77 #include <sys/watchdog.h> 78 79 #include <machine/stdarg.h> 80 81 #include <security/mac/mac_framework.h> 82 83 #include <vm/vm.h> 84 #include <vm/vm_object.h> 85 #include <vm/vm_extern.h> 86 #include <vm/pmap.h> 87 #include <vm/vm_map.h> 88 #include <vm/vm_page.h> 89 #include <vm/vm_kern.h> 90 #include <vm/uma.h> 91 92 #ifdef DDB 93 #include <ddb/ddb.h> 94 #endif 95 96 #define WI_MPSAFEQ 0 97 #define WI_GIANTQ 1 98 99 static void delmntque(struct vnode *vp); 100 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, 101 int slpflag, int slptimeo); 102 static void syncer_shutdown(void *arg, int howto); 103 static int vtryrecycle(struct vnode *vp); 104 static void v_incr_usecount(struct vnode *); 105 static void v_decr_usecount(struct vnode *); 106 static void v_decr_useonly(struct vnode *); 107 static void v_upgrade_usecount(struct vnode *); 108 static void vnlru_free(int); 109 static void vgonel(struct vnode *); 110 static void vfs_knllock(void *arg); 111 static void vfs_knlunlock(void *arg); 112 static void vfs_knl_assert_locked(void *arg); 113 static void vfs_knl_assert_unlocked(void *arg); 114 static void destroy_vpollinfo(struct vpollinfo *vi); 115 116 /* 117 * Number of vnodes in existence. Increased whenever getnewvnode() 118 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode. 119 */ 120 static unsigned long numvnodes; 121 122 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, 123 "Number of vnodes in existence"); 124 125 /* 126 * Conversion tables for conversion from vnode types to inode formats 127 * and back. 128 */ 129 enum vtype iftovt_tab[16] = { 130 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 131 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 132 }; 133 int vttoif_tab[10] = { 134 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 135 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT 136 }; 137 138 /* 139 * List of vnodes that are ready for recycling. 140 */ 141 static TAILQ_HEAD(freelst, vnode) vnode_free_list; 142 143 /* 144 * Free vnode target. Free vnodes may simply be files which have been stat'd 145 * but not read. This is somewhat common, and a small cache of such files 146 * should be kept to avoid recreation costs. 147 */ 148 static u_long wantfreevnodes; 149 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, ""); 150 /* Number of vnodes in the free list. */ 151 static u_long freevnodes; 152 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, 153 "Number of vnodes in the free list"); 154 155 static int vlru_allow_cache_src; 156 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW, 157 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode"); 158 159 /* 160 * Various variables used for debugging the new implementation of 161 * reassignbuf(). 162 * XXX these are probably of (very) limited utility now. 163 */ 164 static int reassignbufcalls; 165 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, 166 "Number of calls to reassignbuf"); 167 168 /* 169 * Cache for the mount type id assigned to NFS. This is used for 170 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c. 171 */ 172 int nfs_mount_type = -1; 173 174 /* To keep more than one thread at a time from running vfs_getnewfsid */ 175 static struct mtx mntid_mtx; 176 177 /* 178 * Lock for any access to the following: 179 * vnode_free_list 180 * numvnodes 181 * freevnodes 182 */ 183 static struct mtx vnode_free_list_mtx; 184 185 /* Publicly exported FS */ 186 struct nfs_public nfs_pub; 187 188 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 189 static uma_zone_t vnode_zone; 190 static uma_zone_t vnodepoll_zone; 191 192 /* 193 * The workitem queue. 194 * 195 * It is useful to delay writes of file data and filesystem metadata 196 * for tens of seconds so that quickly created and deleted files need 197 * not waste disk bandwidth being created and removed. To realize this, 198 * we append vnodes to a "workitem" queue. When running with a soft 199 * updates implementation, most pending metadata dependencies should 200 * not wait for more than a few seconds. Thus, mounted on block devices 201 * are delayed only about a half the time that file data is delayed. 202 * Similarly, directory updates are more critical, so are only delayed 203 * about a third the time that file data is delayed. Thus, there are 204 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 205 * one each second (driven off the filesystem syncer process). The 206 * syncer_delayno variable indicates the next queue that is to be processed. 207 * Items that need to be processed soon are placed in this queue: 208 * 209 * syncer_workitem_pending[syncer_delayno] 210 * 211 * A delay of fifteen seconds is done by placing the request fifteen 212 * entries later in the queue: 213 * 214 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 215 * 216 */ 217 static int syncer_delayno; 218 static long syncer_mask; 219 LIST_HEAD(synclist, bufobj); 220 static struct synclist *syncer_workitem_pending[2]; 221 /* 222 * The sync_mtx protects: 223 * bo->bo_synclist 224 * sync_vnode_count 225 * syncer_delayno 226 * syncer_state 227 * syncer_workitem_pending 228 * syncer_worklist_len 229 * rushjob 230 */ 231 static struct mtx sync_mtx; 232 static struct cv sync_wakeup; 233 234 #define SYNCER_MAXDELAY 32 235 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 236 static int syncdelay = 30; /* max time to delay syncing data */ 237 static int filedelay = 30; /* time to delay syncing files */ 238 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, 239 "Time to delay syncing files (in seconds)"); 240 static int dirdelay = 29; /* time to delay syncing directories */ 241 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, 242 "Time to delay syncing directories (in seconds)"); 243 static int metadelay = 28; /* time to delay syncing metadata */ 244 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, 245 "Time to delay syncing metadata (in seconds)"); 246 static int rushjob; /* number of slots to run ASAP */ 247 static int stat_rush_requests; /* number of times I/O speeded up */ 248 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, 249 "Number of times I/O speeded up (rush requests)"); 250 251 /* 252 * When shutting down the syncer, run it at four times normal speed. 253 */ 254 #define SYNCER_SHUTDOWN_SPEEDUP 4 255 static int sync_vnode_count; 256 static int syncer_worklist_len; 257 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY } 258 syncer_state; 259 260 /* 261 * Number of vnodes we want to exist at any one time. This is mostly used 262 * to size hash tables in vnode-related code. It is normally not used in 263 * getnewvnode(), as wantfreevnodes is normally nonzero.) 264 * 265 * XXX desiredvnodes is historical cruft and should not exist. 266 */ 267 int desiredvnodes; 268 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 269 &desiredvnodes, 0, "Maximum number of vnodes"); 270 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 271 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)"); 272 static int vnlru_nowhere; 273 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, 274 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success"); 275 276 /* 277 * Macros to control when a vnode is freed and recycled. All require 278 * the vnode interlock. 279 */ 280 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt) 281 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt) 282 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt) 283 284 285 /* 286 * Initialize the vnode management data structures. 287 * 288 * Reevaluate the following cap on the number of vnodes after the physical 289 * memory size exceeds 512GB. In the limit, as the physical memory size 290 * grows, the ratio of physical pages to vnodes approaches sixteen to one. 291 */ 292 #ifndef MAXVNODES_MAX 293 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16)) 294 #endif 295 static void 296 vntblinit(void *dummy __unused) 297 { 298 int physvnodes, virtvnodes; 299 300 /* 301 * Desiredvnodes is a function of the physical memory size and the 302 * kernel's heap size. Generally speaking, it scales with the 303 * physical memory size. The ratio of desiredvnodes to physical pages 304 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the 305 * marginal ratio of desiredvnodes to physical pages is one to 306 * sixteen. However, desiredvnodes is limited by the kernel's heap 307 * size. The memory required by desiredvnodes vnodes and vm objects 308 * may not exceed one seventh of the kernel's heap size. 309 */ 310 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4, 311 cnt.v_page_count) / 16; 312 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) + 313 sizeof(struct vnode))); 314 desiredvnodes = min(physvnodes, virtvnodes); 315 if (desiredvnodes > MAXVNODES_MAX) { 316 if (bootverbose) 317 printf("Reducing kern.maxvnodes %d -> %d\n", 318 desiredvnodes, MAXVNODES_MAX); 319 desiredvnodes = MAXVNODES_MAX; 320 } 321 wantfreevnodes = desiredvnodes / 4; 322 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 323 TAILQ_INIT(&vnode_free_list); 324 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF); 325 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 326 NULL, NULL, UMA_ALIGN_PTR, 0); 327 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 328 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 329 /* 330 * Initialize the filesystem syncer. 331 */ 332 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE, 333 &syncer_mask); 334 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE, 335 &syncer_mask); 336 syncer_maxdelay = syncer_mask + 1; 337 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF); 338 cv_init(&sync_wakeup, "syncer"); 339 } 340 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL); 341 342 343 /* 344 * Mark a mount point as busy. Used to synchronize access and to delay 345 * unmounting. Eventually, mountlist_mtx is not released on failure. 346 * 347 * vfs_busy() is a custom lock, it can block the caller. 348 * vfs_busy() only sleeps if the unmount is active on the mount point. 349 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any 350 * vnode belonging to mp. 351 * 352 * Lookup uses vfs_busy() to traverse mount points. 353 * root fs var fs 354 * / vnode lock A / vnode lock (/var) D 355 * /var vnode lock B /log vnode lock(/var/log) E 356 * vfs_busy lock C vfs_busy lock F 357 * 358 * Within each file system, the lock order is C->A->B and F->D->E. 359 * 360 * When traversing across mounts, the system follows that lock order: 361 * 362 * C->A->B 363 * | 364 * +->F->D->E 365 * 366 * The lookup() process for namei("/var") illustrates the process: 367 * VOP_LOOKUP() obtains B while A is held 368 * vfs_busy() obtains a shared lock on F while A and B are held 369 * vput() releases lock on B 370 * vput() releases lock on A 371 * VFS_ROOT() obtains lock on D while shared lock on F is held 372 * vfs_unbusy() releases shared lock on F 373 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A. 374 * Attempt to lock A (instead of vp_crossmp) while D is held would 375 * violate the global order, causing deadlocks. 376 * 377 * dounmount() locks B while F is drained. 378 */ 379 int 380 vfs_busy(struct mount *mp, int flags) 381 { 382 383 MPASS((flags & ~MBF_MASK) == 0); 384 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags); 385 386 MNT_ILOCK(mp); 387 MNT_REF(mp); 388 /* 389 * If mount point is currenly being unmounted, sleep until the 390 * mount point fate is decided. If thread doing the unmounting fails, 391 * it will clear MNTK_UNMOUNT flag before waking us up, indicating 392 * that this mount point has survived the unmount attempt and vfs_busy 393 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE 394 * flag in addition to MNTK_UNMOUNT, indicating that mount point is 395 * about to be really destroyed. vfs_busy needs to release its 396 * reference on the mount point in this case and return with ENOENT, 397 * telling the caller that mount mount it tried to busy is no longer 398 * valid. 399 */ 400 while (mp->mnt_kern_flag & MNTK_UNMOUNT) { 401 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) { 402 MNT_REL(mp); 403 MNT_IUNLOCK(mp); 404 CTR1(KTR_VFS, "%s: failed busying before sleeping", 405 __func__); 406 return (ENOENT); 407 } 408 if (flags & MBF_MNTLSTLOCK) 409 mtx_unlock(&mountlist_mtx); 410 mp->mnt_kern_flag |= MNTK_MWAIT; 411 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0); 412 if (flags & MBF_MNTLSTLOCK) 413 mtx_lock(&mountlist_mtx); 414 MNT_ILOCK(mp); 415 } 416 if (flags & MBF_MNTLSTLOCK) 417 mtx_unlock(&mountlist_mtx); 418 mp->mnt_lockref++; 419 MNT_IUNLOCK(mp); 420 return (0); 421 } 422 423 /* 424 * Free a busy filesystem. 425 */ 426 void 427 vfs_unbusy(struct mount *mp) 428 { 429 430 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 431 MNT_ILOCK(mp); 432 MNT_REL(mp); 433 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref")); 434 mp->mnt_lockref--; 435 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) { 436 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT); 437 CTR1(KTR_VFS, "%s: waking up waiters", __func__); 438 mp->mnt_kern_flag &= ~MNTK_DRAINING; 439 wakeup(&mp->mnt_lockref); 440 } 441 MNT_IUNLOCK(mp); 442 } 443 444 /* 445 * Lookup a mount point by filesystem identifier. 446 */ 447 struct mount * 448 vfs_getvfs(fsid_t *fsid) 449 { 450 struct mount *mp; 451 452 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 453 mtx_lock(&mountlist_mtx); 454 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 455 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 456 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 457 vfs_ref(mp); 458 mtx_unlock(&mountlist_mtx); 459 return (mp); 460 } 461 } 462 mtx_unlock(&mountlist_mtx); 463 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 464 return ((struct mount *) 0); 465 } 466 467 /* 468 * Lookup a mount point by filesystem identifier, busying it before 469 * returning. 470 */ 471 struct mount * 472 vfs_busyfs(fsid_t *fsid) 473 { 474 struct mount *mp; 475 int error; 476 477 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid); 478 mtx_lock(&mountlist_mtx); 479 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 480 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 481 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 482 error = vfs_busy(mp, MBF_MNTLSTLOCK); 483 if (error) { 484 mtx_unlock(&mountlist_mtx); 485 return (NULL); 486 } 487 return (mp); 488 } 489 } 490 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid); 491 mtx_unlock(&mountlist_mtx); 492 return ((struct mount *) 0); 493 } 494 495 /* 496 * Check if a user can access privileged mount options. 497 */ 498 int 499 vfs_suser(struct mount *mp, struct thread *td) 500 { 501 int error; 502 503 /* 504 * If the thread is jailed, but this is not a jail-friendly file 505 * system, deny immediately. 506 */ 507 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred)) 508 return (EPERM); 509 510 /* 511 * If the file system was mounted outside the jail of the calling 512 * thread, deny immediately. 513 */ 514 if (prison_check(td->td_ucred, mp->mnt_cred) != 0) 515 return (EPERM); 516 517 /* 518 * If file system supports delegated administration, we don't check 519 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified 520 * by the file system itself. 521 * If this is not the user that did original mount, we check for 522 * the PRIV_VFS_MOUNT_OWNER privilege. 523 */ 524 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) && 525 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) { 526 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0) 527 return (error); 528 } 529 return (0); 530 } 531 532 /* 533 * Get a new unique fsid. Try to make its val[0] unique, since this value 534 * will be used to create fake device numbers for stat(). Also try (but 535 * not so hard) make its val[0] unique mod 2^16, since some emulators only 536 * support 16-bit device numbers. We end up with unique val[0]'s for the 537 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 538 * 539 * Keep in mind that several mounts may be running in parallel. Starting 540 * the search one past where the previous search terminated is both a 541 * micro-optimization and a defense against returning the same fsid to 542 * different mounts. 543 */ 544 void 545 vfs_getnewfsid(struct mount *mp) 546 { 547 static uint16_t mntid_base; 548 struct mount *nmp; 549 fsid_t tfsid; 550 int mtype; 551 552 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 553 mtx_lock(&mntid_mtx); 554 mtype = mp->mnt_vfc->vfc_typenum; 555 tfsid.val[1] = mtype; 556 mtype = (mtype & 0xFF) << 24; 557 for (;;) { 558 tfsid.val[0] = makedev(255, 559 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 560 mntid_base++; 561 if ((nmp = vfs_getvfs(&tfsid)) == NULL) 562 break; 563 vfs_rel(nmp); 564 } 565 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 566 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 567 mtx_unlock(&mntid_mtx); 568 } 569 570 /* 571 * Knob to control the precision of file timestamps: 572 * 573 * 0 = seconds only; nanoseconds zeroed. 574 * 1 = seconds and nanoseconds, accurate within 1/HZ. 575 * 2 = seconds and nanoseconds, truncated to microseconds. 576 * >=3 = seconds and nanoseconds, maximum precision. 577 */ 578 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 579 580 static int timestamp_precision = TSP_SEC; 581 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 582 ×tamp_precision, 0, "File timestamp precision (0: seconds, " 583 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, " 584 "3+: sec + ns (max. precision))"); 585 586 /* 587 * Get a current timestamp. 588 */ 589 void 590 vfs_timestamp(struct timespec *tsp) 591 { 592 struct timeval tv; 593 594 switch (timestamp_precision) { 595 case TSP_SEC: 596 tsp->tv_sec = time_second; 597 tsp->tv_nsec = 0; 598 break; 599 case TSP_HZ: 600 getnanotime(tsp); 601 break; 602 case TSP_USEC: 603 microtime(&tv); 604 TIMEVAL_TO_TIMESPEC(&tv, tsp); 605 break; 606 case TSP_NSEC: 607 default: 608 nanotime(tsp); 609 break; 610 } 611 } 612 613 /* 614 * Set vnode attributes to VNOVAL 615 */ 616 void 617 vattr_null(struct vattr *vap) 618 { 619 620 vap->va_type = VNON; 621 vap->va_size = VNOVAL; 622 vap->va_bytes = VNOVAL; 623 vap->va_mode = VNOVAL; 624 vap->va_nlink = VNOVAL; 625 vap->va_uid = VNOVAL; 626 vap->va_gid = VNOVAL; 627 vap->va_fsid = VNOVAL; 628 vap->va_fileid = VNOVAL; 629 vap->va_blocksize = VNOVAL; 630 vap->va_rdev = VNOVAL; 631 vap->va_atime.tv_sec = VNOVAL; 632 vap->va_atime.tv_nsec = VNOVAL; 633 vap->va_mtime.tv_sec = VNOVAL; 634 vap->va_mtime.tv_nsec = VNOVAL; 635 vap->va_ctime.tv_sec = VNOVAL; 636 vap->va_ctime.tv_nsec = VNOVAL; 637 vap->va_birthtime.tv_sec = VNOVAL; 638 vap->va_birthtime.tv_nsec = VNOVAL; 639 vap->va_flags = VNOVAL; 640 vap->va_gen = VNOVAL; 641 vap->va_vaflags = 0; 642 } 643 644 /* 645 * This routine is called when we have too many vnodes. It attempts 646 * to free <count> vnodes and will potentially free vnodes that still 647 * have VM backing store (VM backing store is typically the cause 648 * of a vnode blowout so we want to do this). Therefore, this operation 649 * is not considered cheap. 650 * 651 * A number of conditions may prevent a vnode from being reclaimed. 652 * the buffer cache may have references on the vnode, a directory 653 * vnode may still have references due to the namei cache representing 654 * underlying files, or the vnode may be in active use. It is not 655 * desireable to reuse such vnodes. These conditions may cause the 656 * number of vnodes to reach some minimum value regardless of what 657 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 658 */ 659 static int 660 vlrureclaim(struct mount *mp) 661 { 662 struct vnode *vp; 663 int done; 664 int trigger; 665 int usevnodes; 666 int count; 667 668 /* 669 * Calculate the trigger point, don't allow user 670 * screwups to blow us up. This prevents us from 671 * recycling vnodes with lots of resident pages. We 672 * aren't trying to free memory, we are trying to 673 * free vnodes. 674 */ 675 usevnodes = desiredvnodes; 676 if (usevnodes <= 0) 677 usevnodes = 1; 678 trigger = cnt.v_page_count * 2 / usevnodes; 679 done = 0; 680 vn_start_write(NULL, &mp, V_WAIT); 681 MNT_ILOCK(mp); 682 count = mp->mnt_nvnodelistsize / 10 + 1; 683 while (count != 0) { 684 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 685 while (vp != NULL && vp->v_type == VMARKER) 686 vp = TAILQ_NEXT(vp, v_nmntvnodes); 687 if (vp == NULL) 688 break; 689 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 690 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 691 --count; 692 if (!VI_TRYLOCK(vp)) 693 goto next_iter; 694 /* 695 * If it's been deconstructed already, it's still 696 * referenced, or it exceeds the trigger, skip it. 697 */ 698 if (vp->v_usecount || 699 (!vlru_allow_cache_src && 700 !LIST_EMPTY(&(vp)->v_cache_src)) || 701 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL && 702 vp->v_object->resident_page_count > trigger)) { 703 VI_UNLOCK(vp); 704 goto next_iter; 705 } 706 MNT_IUNLOCK(mp); 707 vholdl(vp); 708 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) { 709 vdrop(vp); 710 goto next_iter_mntunlocked; 711 } 712 VI_LOCK(vp); 713 /* 714 * v_usecount may have been bumped after VOP_LOCK() dropped 715 * the vnode interlock and before it was locked again. 716 * 717 * It is not necessary to recheck VI_DOOMED because it can 718 * only be set by another thread that holds both the vnode 719 * lock and vnode interlock. If another thread has the 720 * vnode lock before we get to VOP_LOCK() and obtains the 721 * vnode interlock after VOP_LOCK() drops the vnode 722 * interlock, the other thread will be unable to drop the 723 * vnode lock before our VOP_LOCK() call fails. 724 */ 725 if (vp->v_usecount || 726 (!vlru_allow_cache_src && 727 !LIST_EMPTY(&(vp)->v_cache_src)) || 728 (vp->v_object != NULL && 729 vp->v_object->resident_page_count > trigger)) { 730 VOP_UNLOCK(vp, LK_INTERLOCK); 731 goto next_iter_mntunlocked; 732 } 733 KASSERT((vp->v_iflag & VI_DOOMED) == 0, 734 ("VI_DOOMED unexpectedly detected in vlrureclaim()")); 735 vgonel(vp); 736 VOP_UNLOCK(vp, 0); 737 vdropl(vp); 738 done++; 739 next_iter_mntunlocked: 740 if (!should_yield()) 741 goto relock_mnt; 742 goto yield; 743 next_iter: 744 if (!should_yield()) 745 continue; 746 MNT_IUNLOCK(mp); 747 yield: 748 kern_yield(PRI_UNCHANGED); 749 relock_mnt: 750 MNT_ILOCK(mp); 751 } 752 MNT_IUNLOCK(mp); 753 vn_finished_write(mp); 754 return done; 755 } 756 757 /* 758 * Attempt to keep the free list at wantfreevnodes length. 759 */ 760 static void 761 vnlru_free(int count) 762 { 763 struct vnode *vp; 764 int vfslocked; 765 766 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 767 for (; count > 0; count--) { 768 vp = TAILQ_FIRST(&vnode_free_list); 769 /* 770 * The list can be modified while the free_list_mtx 771 * has been dropped and vp could be NULL here. 772 */ 773 if (!vp) 774 break; 775 VNASSERT(vp->v_op != NULL, vp, 776 ("vnlru_free: vnode already reclaimed.")); 777 KASSERT((vp->v_iflag & VI_FREE) != 0, 778 ("Removing vnode not on freelist")); 779 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 780 ("Mangling active vnode")); 781 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist); 782 /* 783 * Don't recycle if we can't get the interlock. 784 */ 785 if (!VI_TRYLOCK(vp)) { 786 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist); 787 continue; 788 } 789 VNASSERT(VCANRECYCLE(vp), vp, 790 ("vp inconsistent on freelist")); 791 freevnodes--; 792 vp->v_iflag &= ~VI_FREE; 793 vholdl(vp); 794 mtx_unlock(&vnode_free_list_mtx); 795 VI_UNLOCK(vp); 796 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 797 vtryrecycle(vp); 798 VFS_UNLOCK_GIANT(vfslocked); 799 /* 800 * If the recycled succeeded this vdrop will actually free 801 * the vnode. If not it will simply place it back on 802 * the free list. 803 */ 804 vdrop(vp); 805 mtx_lock(&vnode_free_list_mtx); 806 } 807 } 808 /* 809 * Attempt to recycle vnodes in a context that is always safe to block. 810 * Calling vlrurecycle() from the bowels of filesystem code has some 811 * interesting deadlock problems. 812 */ 813 static struct proc *vnlruproc; 814 static int vnlruproc_sig; 815 816 static void 817 vnlru_proc(void) 818 { 819 struct mount *mp, *nmp; 820 int done, vfslocked; 821 struct proc *p = vnlruproc; 822 823 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p, 824 SHUTDOWN_PRI_FIRST); 825 826 for (;;) { 827 kproc_suspend_check(p); 828 mtx_lock(&vnode_free_list_mtx); 829 if (freevnodes > wantfreevnodes) 830 vnlru_free(freevnodes - wantfreevnodes); 831 if (numvnodes <= desiredvnodes * 9 / 10) { 832 vnlruproc_sig = 0; 833 wakeup(&vnlruproc_sig); 834 msleep(vnlruproc, &vnode_free_list_mtx, 835 PVFS|PDROP, "vlruwt", hz); 836 continue; 837 } 838 mtx_unlock(&vnode_free_list_mtx); 839 done = 0; 840 mtx_lock(&mountlist_mtx); 841 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 842 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) { 843 nmp = TAILQ_NEXT(mp, mnt_list); 844 continue; 845 } 846 vfslocked = VFS_LOCK_GIANT(mp); 847 done += vlrureclaim(mp); 848 VFS_UNLOCK_GIANT(vfslocked); 849 mtx_lock(&mountlist_mtx); 850 nmp = TAILQ_NEXT(mp, mnt_list); 851 vfs_unbusy(mp); 852 } 853 mtx_unlock(&mountlist_mtx); 854 if (done == 0) { 855 #if 0 856 /* These messages are temporary debugging aids */ 857 if (vnlru_nowhere < 5) 858 printf("vnlru process getting nowhere..\n"); 859 else if (vnlru_nowhere == 5) 860 printf("vnlru process messages stopped.\n"); 861 #endif 862 vnlru_nowhere++; 863 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 864 } else 865 kern_yield(PRI_UNCHANGED); 866 } 867 } 868 869 static struct kproc_desc vnlru_kp = { 870 "vnlru", 871 vnlru_proc, 872 &vnlruproc 873 }; 874 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, 875 &vnlru_kp); 876 877 /* 878 * Routines having to do with the management of the vnode table. 879 */ 880 881 /* 882 * Try to recycle a freed vnode. We abort if anyone picks up a reference 883 * before we actually vgone(). This function must be called with the vnode 884 * held to prevent the vnode from being returned to the free list midway 885 * through vgone(). 886 */ 887 static int 888 vtryrecycle(struct vnode *vp) 889 { 890 struct mount *vnmp; 891 892 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 893 VNASSERT(vp->v_holdcnt, vp, 894 ("vtryrecycle: Recycling vp %p without a reference.", vp)); 895 /* 896 * This vnode may found and locked via some other list, if so we 897 * can't recycle it yet. 898 */ 899 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 900 CTR2(KTR_VFS, 901 "%s: impossible to recycle, vp %p lock is already held", 902 __func__, vp); 903 return (EWOULDBLOCK); 904 } 905 /* 906 * Don't recycle if its filesystem is being suspended. 907 */ 908 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) { 909 VOP_UNLOCK(vp, 0); 910 CTR2(KTR_VFS, 911 "%s: impossible to recycle, cannot start the write for %p", 912 __func__, vp); 913 return (EBUSY); 914 } 915 /* 916 * If we got this far, we need to acquire the interlock and see if 917 * anyone picked up this vnode from another list. If not, we will 918 * mark it with DOOMED via vgonel() so that anyone who does find it 919 * will skip over it. 920 */ 921 VI_LOCK(vp); 922 if (vp->v_usecount) { 923 VOP_UNLOCK(vp, LK_INTERLOCK); 924 vn_finished_write(vnmp); 925 CTR2(KTR_VFS, 926 "%s: impossible to recycle, %p is already referenced", 927 __func__, vp); 928 return (EBUSY); 929 } 930 if ((vp->v_iflag & VI_DOOMED) == 0) 931 vgonel(vp); 932 VOP_UNLOCK(vp, LK_INTERLOCK); 933 vn_finished_write(vnmp); 934 return (0); 935 } 936 937 /* 938 * Wait for available vnodes. 939 */ 940 static int 941 getnewvnode_wait(int suspended) 942 { 943 944 mtx_assert(&vnode_free_list_mtx, MA_OWNED); 945 if (numvnodes > desiredvnodes) { 946 if (suspended) { 947 /* 948 * File system is beeing suspended, we cannot risk a 949 * deadlock here, so allocate new vnode anyway. 950 */ 951 if (freevnodes > wantfreevnodes) 952 vnlru_free(freevnodes - wantfreevnodes); 953 return (0); 954 } 955 if (vnlruproc_sig == 0) { 956 vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 957 wakeup(vnlruproc); 958 } 959 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS, 960 "vlruwk", hz); 961 } 962 return (numvnodes > desiredvnodes ? ENFILE : 0); 963 } 964 965 void 966 getnewvnode_reserve(u_int count) 967 { 968 struct thread *td; 969 970 td = curthread; 971 mtx_lock(&vnode_free_list_mtx); 972 while (count > 0) { 973 if (getnewvnode_wait(0) == 0) { 974 count--; 975 td->td_vp_reserv++; 976 numvnodes++; 977 } 978 } 979 mtx_unlock(&vnode_free_list_mtx); 980 } 981 982 void 983 getnewvnode_drop_reserve(void) 984 { 985 struct thread *td; 986 987 td = curthread; 988 mtx_lock(&vnode_free_list_mtx); 989 KASSERT(numvnodes >= td->td_vp_reserv, ("reserve too large")); 990 numvnodes -= td->td_vp_reserv; 991 mtx_unlock(&vnode_free_list_mtx); 992 td->td_vp_reserv = 0; 993 } 994 995 /* 996 * Return the next vnode from the free list. 997 */ 998 int 999 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops, 1000 struct vnode **vpp) 1001 { 1002 struct vnode *vp; 1003 struct bufobj *bo; 1004 struct thread *td; 1005 int error; 1006 1007 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag); 1008 vp = NULL; 1009 td = curthread; 1010 if (td->td_vp_reserv > 0) { 1011 td->td_vp_reserv -= 1; 1012 goto alloc; 1013 } 1014 mtx_lock(&vnode_free_list_mtx); 1015 /* 1016 * Lend our context to reclaim vnodes if they've exceeded the max. 1017 */ 1018 if (freevnodes > wantfreevnodes) 1019 vnlru_free(1); 1020 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag & 1021 MNTK_SUSPEND)); 1022 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */ 1023 if (error != 0) { 1024 mtx_unlock(&vnode_free_list_mtx); 1025 return (error); 1026 } 1027 #endif 1028 numvnodes++; 1029 mtx_unlock(&vnode_free_list_mtx); 1030 alloc: 1031 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO); 1032 /* 1033 * Setup locks. 1034 */ 1035 vp->v_vnlock = &vp->v_lock; 1036 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 1037 /* 1038 * By default, don't allow shared locks unless filesystems 1039 * opt-in. 1040 */ 1041 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE); 1042 /* 1043 * Initialize bufobj. 1044 */ 1045 bo = &vp->v_bufobj; 1046 bo->__bo_vnode = vp; 1047 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF); 1048 bo->bo_ops = &buf_ops_bio; 1049 bo->bo_private = vp; 1050 TAILQ_INIT(&bo->bo_clean.bv_hd); 1051 TAILQ_INIT(&bo->bo_dirty.bv_hd); 1052 /* 1053 * Initialize namecache. 1054 */ 1055 LIST_INIT(&vp->v_cache_src); 1056 TAILQ_INIT(&vp->v_cache_dst); 1057 /* 1058 * Finalize various vnode identity bits. 1059 */ 1060 vp->v_type = VNON; 1061 vp->v_tag = tag; 1062 vp->v_op = vops; 1063 v_incr_usecount(vp); 1064 vp->v_data = NULL; 1065 #ifdef MAC 1066 mac_vnode_init(vp); 1067 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0) 1068 mac_vnode_associate_singlelabel(mp, vp); 1069 else if (mp == NULL && vops != &dead_vnodeops) 1070 printf("NULL mp in getnewvnode()\n"); 1071 #endif 1072 if (mp != NULL) { 1073 bo->bo_bsize = mp->mnt_stat.f_iosize; 1074 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0) 1075 vp->v_vflag |= VV_NOKNOTE; 1076 } 1077 rangelock_init(&vp->v_rl); 1078 1079 *vpp = vp; 1080 return (0); 1081 } 1082 1083 /* 1084 * Delete from old mount point vnode list, if on one. 1085 */ 1086 static void 1087 delmntque(struct vnode *vp) 1088 { 1089 struct mount *mp; 1090 int active; 1091 1092 mp = vp->v_mount; 1093 if (mp == NULL) 1094 return; 1095 MNT_ILOCK(mp); 1096 VI_LOCK(vp); 1097 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize, 1098 ("Active vnode list size %d > Vnode list size %d", 1099 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize)); 1100 active = vp->v_iflag & VI_ACTIVE; 1101 vp->v_iflag &= ~VI_ACTIVE; 1102 if (active) { 1103 mtx_lock(&vnode_free_list_mtx); 1104 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist); 1105 mp->mnt_activevnodelistsize--; 1106 mtx_unlock(&vnode_free_list_mtx); 1107 } 1108 vp->v_mount = NULL; 1109 VI_UNLOCK(vp); 1110 VNASSERT(mp->mnt_nvnodelistsize > 0, vp, 1111 ("bad mount point vnode list size")); 1112 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1113 mp->mnt_nvnodelistsize--; 1114 MNT_REL(mp); 1115 MNT_IUNLOCK(mp); 1116 } 1117 1118 static void 1119 insmntque_stddtr(struct vnode *vp, void *dtr_arg) 1120 { 1121 1122 vp->v_data = NULL; 1123 vp->v_op = &dead_vnodeops; 1124 /* XXX non mp-safe fs may still call insmntque with vnode 1125 unlocked */ 1126 if (!VOP_ISLOCKED(vp)) 1127 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1128 vgone(vp); 1129 vput(vp); 1130 } 1131 1132 /* 1133 * Insert into list of vnodes for the new mount point, if available. 1134 */ 1135 int 1136 insmntque1(struct vnode *vp, struct mount *mp, 1137 void (*dtr)(struct vnode *, void *), void *dtr_arg) 1138 { 1139 int locked; 1140 1141 KASSERT(vp->v_mount == NULL, 1142 ("insmntque: vnode already on per mount vnode list")); 1143 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)")); 1144 #ifdef DEBUG_VFS_LOCKS 1145 if (!VFS_NEEDSGIANT(mp)) 1146 ASSERT_VOP_ELOCKED(vp, 1147 "insmntque: mp-safe fs and non-locked vp"); 1148 #endif 1149 /* 1150 * We acquire the vnode interlock early to ensure that the 1151 * vnode cannot be recycled by another process releasing a 1152 * holdcnt on it before we get it on both the vnode list 1153 * and the active vnode list. The mount mutex protects only 1154 * manipulation of the vnode list and the vnode freelist 1155 * mutex protects only manipulation of the active vnode list. 1156 * Hence the need to hold the vnode interlock throughout. 1157 */ 1158 MNT_ILOCK(mp); 1159 VI_LOCK(vp); 1160 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 && 1161 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 || 1162 mp->mnt_nvnodelistsize == 0)) { 1163 locked = VOP_ISLOCKED(vp); 1164 if (!locked || (locked == LK_EXCLUSIVE && 1165 (vp->v_vflag & VV_FORCEINSMQ) == 0)) { 1166 VI_UNLOCK(vp); 1167 MNT_IUNLOCK(mp); 1168 if (dtr != NULL) 1169 dtr(vp, dtr_arg); 1170 return (EBUSY); 1171 } 1172 } 1173 vp->v_mount = mp; 1174 MNT_REF(mp); 1175 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1176 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp, 1177 ("neg mount point vnode list size")); 1178 mp->mnt_nvnodelistsize++; 1179 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 1180 ("Activating already active vnode")); 1181 vp->v_iflag |= VI_ACTIVE; 1182 mtx_lock(&vnode_free_list_mtx); 1183 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist); 1184 mp->mnt_activevnodelistsize++; 1185 mtx_unlock(&vnode_free_list_mtx); 1186 VI_UNLOCK(vp); 1187 MNT_IUNLOCK(mp); 1188 return (0); 1189 } 1190 1191 int 1192 insmntque(struct vnode *vp, struct mount *mp) 1193 { 1194 1195 return (insmntque1(vp, mp, insmntque_stddtr, NULL)); 1196 } 1197 1198 /* 1199 * Flush out and invalidate all buffers associated with a bufobj 1200 * Called with the underlying object locked. 1201 */ 1202 int 1203 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo) 1204 { 1205 int error; 1206 1207 BO_LOCK(bo); 1208 if (flags & V_SAVE) { 1209 error = bufobj_wwait(bo, slpflag, slptimeo); 1210 if (error) { 1211 BO_UNLOCK(bo); 1212 return (error); 1213 } 1214 if (bo->bo_dirty.bv_cnt > 0) { 1215 BO_UNLOCK(bo); 1216 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0) 1217 return (error); 1218 /* 1219 * XXX We could save a lock/unlock if this was only 1220 * enabled under INVARIANTS 1221 */ 1222 BO_LOCK(bo); 1223 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1224 panic("vinvalbuf: dirty bufs"); 1225 } 1226 } 1227 /* 1228 * If you alter this loop please notice that interlock is dropped and 1229 * reacquired in flushbuflist. Special care is needed to ensure that 1230 * no race conditions occur from this. 1231 */ 1232 do { 1233 error = flushbuflist(&bo->bo_clean, 1234 flags, bo, slpflag, slptimeo); 1235 if (error == 0 && !(flags & V_CLEANONLY)) 1236 error = flushbuflist(&bo->bo_dirty, 1237 flags, bo, slpflag, slptimeo); 1238 if (error != 0 && error != EAGAIN) { 1239 BO_UNLOCK(bo); 1240 return (error); 1241 } 1242 } while (error != 0); 1243 1244 /* 1245 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1246 * have write I/O in-progress but if there is a VM object then the 1247 * VM object can also have read-I/O in-progress. 1248 */ 1249 do { 1250 bufobj_wwait(bo, 0, 0); 1251 BO_UNLOCK(bo); 1252 if (bo->bo_object != NULL) { 1253 VM_OBJECT_LOCK(bo->bo_object); 1254 vm_object_pip_wait(bo->bo_object, "bovlbx"); 1255 VM_OBJECT_UNLOCK(bo->bo_object); 1256 } 1257 BO_LOCK(bo); 1258 } while (bo->bo_numoutput > 0); 1259 BO_UNLOCK(bo); 1260 1261 /* 1262 * Destroy the copy in the VM cache, too. 1263 */ 1264 if (bo->bo_object != NULL && 1265 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) { 1266 VM_OBJECT_LOCK(bo->bo_object); 1267 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ? 1268 OBJPR_CLEANONLY : 0); 1269 VM_OBJECT_UNLOCK(bo->bo_object); 1270 } 1271 1272 #ifdef INVARIANTS 1273 BO_LOCK(bo); 1274 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 && 1275 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0)) 1276 panic("vinvalbuf: flush failed"); 1277 BO_UNLOCK(bo); 1278 #endif 1279 return (0); 1280 } 1281 1282 /* 1283 * Flush out and invalidate all buffers associated with a vnode. 1284 * Called with the underlying object locked. 1285 */ 1286 int 1287 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 1288 { 1289 1290 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 1291 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1292 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo)); 1293 } 1294 1295 /* 1296 * Flush out buffers on the specified list. 1297 * 1298 */ 1299 static int 1300 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 1301 int slptimeo) 1302 { 1303 struct buf *bp, *nbp; 1304 int retval, error; 1305 daddr_t lblkno; 1306 b_xflags_t xflags; 1307 1308 ASSERT_BO_LOCKED(bo); 1309 1310 retval = 0; 1311 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 1312 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 1313 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) { 1314 continue; 1315 } 1316 lblkno = 0; 1317 xflags = 0; 1318 if (nbp != NULL) { 1319 lblkno = nbp->b_lblkno; 1320 xflags = nbp->b_xflags & 1321 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN); 1322 } 1323 retval = EAGAIN; 1324 error = BUF_TIMELOCK(bp, 1325 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo), 1326 "flushbuf", slpflag, slptimeo); 1327 if (error) { 1328 BO_LOCK(bo); 1329 return (error != ENOLCK ? error : EAGAIN); 1330 } 1331 KASSERT(bp->b_bufobj == bo, 1332 ("bp %p wrong b_bufobj %p should be %p", 1333 bp, bp->b_bufobj, bo)); 1334 if (bp->b_bufobj != bo) { /* XXX: necessary ? */ 1335 BUF_UNLOCK(bp); 1336 BO_LOCK(bo); 1337 return (EAGAIN); 1338 } 1339 /* 1340 * XXX Since there are no node locks for NFS, I 1341 * believe there is a slight chance that a delayed 1342 * write will occur while sleeping just above, so 1343 * check for it. 1344 */ 1345 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1346 (flags & V_SAVE)) { 1347 BO_LOCK(bo); 1348 bremfree(bp); 1349 BO_UNLOCK(bo); 1350 bp->b_flags |= B_ASYNC; 1351 bwrite(bp); 1352 BO_LOCK(bo); 1353 return (EAGAIN); /* XXX: why not loop ? */ 1354 } 1355 BO_LOCK(bo); 1356 bremfree(bp); 1357 BO_UNLOCK(bo); 1358 bp->b_flags |= (B_INVAL | B_RELBUF); 1359 bp->b_flags &= ~B_ASYNC; 1360 brelse(bp); 1361 BO_LOCK(bo); 1362 if (nbp != NULL && 1363 (nbp->b_bufobj != bo || 1364 nbp->b_lblkno != lblkno || 1365 (nbp->b_xflags & 1366 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags)) 1367 break; /* nbp invalid */ 1368 } 1369 return (retval); 1370 } 1371 1372 /* 1373 * Truncate a file's buffer and pages to a specified length. This 1374 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1375 * sync activity. 1376 */ 1377 int 1378 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize) 1379 { 1380 struct buf *bp, *nbp; 1381 int anyfreed; 1382 int trunclbn; 1383 struct bufobj *bo; 1384 1385 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__, 1386 vp, cred, blksize, (uintmax_t)length); 1387 1388 /* 1389 * Round up to the *next* lbn. 1390 */ 1391 trunclbn = (length + blksize - 1) / blksize; 1392 1393 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 1394 restart: 1395 bo = &vp->v_bufobj; 1396 BO_LOCK(bo); 1397 anyfreed = 1; 1398 for (;anyfreed;) { 1399 anyfreed = 0; 1400 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 1401 if (bp->b_lblkno < trunclbn) 1402 continue; 1403 if (BUF_LOCK(bp, 1404 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1405 BO_MTX(bo)) == ENOLCK) 1406 goto restart; 1407 1408 BO_LOCK(bo); 1409 bremfree(bp); 1410 BO_UNLOCK(bo); 1411 bp->b_flags |= (B_INVAL | B_RELBUF); 1412 bp->b_flags &= ~B_ASYNC; 1413 brelse(bp); 1414 anyfreed = 1; 1415 1416 BO_LOCK(bo); 1417 if (nbp != NULL && 1418 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1419 (nbp->b_vp != vp) || 1420 (nbp->b_flags & B_DELWRI))) { 1421 BO_UNLOCK(bo); 1422 goto restart; 1423 } 1424 } 1425 1426 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1427 if (bp->b_lblkno < trunclbn) 1428 continue; 1429 if (BUF_LOCK(bp, 1430 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1431 BO_MTX(bo)) == ENOLCK) 1432 goto restart; 1433 BO_LOCK(bo); 1434 bremfree(bp); 1435 BO_UNLOCK(bo); 1436 bp->b_flags |= (B_INVAL | B_RELBUF); 1437 bp->b_flags &= ~B_ASYNC; 1438 brelse(bp); 1439 anyfreed = 1; 1440 1441 BO_LOCK(bo); 1442 if (nbp != NULL && 1443 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1444 (nbp->b_vp != vp) || 1445 (nbp->b_flags & B_DELWRI) == 0)) { 1446 BO_UNLOCK(bo); 1447 goto restart; 1448 } 1449 } 1450 } 1451 1452 if (length > 0) { 1453 restartsync: 1454 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1455 if (bp->b_lblkno > 0) 1456 continue; 1457 /* 1458 * Since we hold the vnode lock this should only 1459 * fail if we're racing with the buf daemon. 1460 */ 1461 if (BUF_LOCK(bp, 1462 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1463 BO_MTX(bo)) == ENOLCK) { 1464 goto restart; 1465 } 1466 VNASSERT((bp->b_flags & B_DELWRI), vp, 1467 ("buf(%p) on dirty queue without DELWRI", bp)); 1468 1469 BO_LOCK(bo); 1470 bremfree(bp); 1471 BO_UNLOCK(bo); 1472 bawrite(bp); 1473 BO_LOCK(bo); 1474 goto restartsync; 1475 } 1476 } 1477 1478 bufobj_wwait(bo, 0, 0); 1479 BO_UNLOCK(bo); 1480 vnode_pager_setsize(vp, length); 1481 1482 return (0); 1483 } 1484 1485 /* 1486 * buf_splay() - splay tree core for the clean/dirty list of buffers in 1487 * a vnode. 1488 * 1489 * NOTE: We have to deal with the special case of a background bitmap 1490 * buffer, a situation where two buffers will have the same logical 1491 * block offset. We want (1) only the foreground buffer to be accessed 1492 * in a lookup and (2) must differentiate between the foreground and 1493 * background buffer in the splay tree algorithm because the splay 1494 * tree cannot normally handle multiple entities with the same 'index'. 1495 * We accomplish this by adding differentiating flags to the splay tree's 1496 * numerical domain. 1497 */ 1498 static 1499 struct buf * 1500 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root) 1501 { 1502 struct buf dummy; 1503 struct buf *lefttreemax, *righttreemin, *y; 1504 1505 if (root == NULL) 1506 return (NULL); 1507 lefttreemax = righttreemin = &dummy; 1508 for (;;) { 1509 if (lblkno < root->b_lblkno || 1510 (lblkno == root->b_lblkno && 1511 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1512 if ((y = root->b_left) == NULL) 1513 break; 1514 if (lblkno < y->b_lblkno) { 1515 /* Rotate right. */ 1516 root->b_left = y->b_right; 1517 y->b_right = root; 1518 root = y; 1519 if ((y = root->b_left) == NULL) 1520 break; 1521 } 1522 /* Link into the new root's right tree. */ 1523 righttreemin->b_left = root; 1524 righttreemin = root; 1525 } else if (lblkno > root->b_lblkno || 1526 (lblkno == root->b_lblkno && 1527 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) { 1528 if ((y = root->b_right) == NULL) 1529 break; 1530 if (lblkno > y->b_lblkno) { 1531 /* Rotate left. */ 1532 root->b_right = y->b_left; 1533 y->b_left = root; 1534 root = y; 1535 if ((y = root->b_right) == NULL) 1536 break; 1537 } 1538 /* Link into the new root's left tree. */ 1539 lefttreemax->b_right = root; 1540 lefttreemax = root; 1541 } else { 1542 break; 1543 } 1544 root = y; 1545 } 1546 /* Assemble the new root. */ 1547 lefttreemax->b_right = root->b_left; 1548 righttreemin->b_left = root->b_right; 1549 root->b_left = dummy.b_right; 1550 root->b_right = dummy.b_left; 1551 return (root); 1552 } 1553 1554 static void 1555 buf_vlist_remove(struct buf *bp) 1556 { 1557 struct buf *root; 1558 struct bufv *bv; 1559 1560 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1561 ASSERT_BO_LOCKED(bp->b_bufobj); 1562 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 1563 (BX_VNDIRTY|BX_VNCLEAN), 1564 ("buf_vlist_remove: Buf %p is on two lists", bp)); 1565 if (bp->b_xflags & BX_VNDIRTY) 1566 bv = &bp->b_bufobj->bo_dirty; 1567 else 1568 bv = &bp->b_bufobj->bo_clean; 1569 if (bp != bv->bv_root) { 1570 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root); 1571 KASSERT(root == bp, ("splay lookup failed in remove")); 1572 } 1573 if (bp->b_left == NULL) { 1574 root = bp->b_right; 1575 } else { 1576 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1577 root->b_right = bp->b_right; 1578 } 1579 bv->bv_root = root; 1580 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 1581 bv->bv_cnt--; 1582 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1583 } 1584 1585 /* 1586 * Add the buffer to the sorted clean or dirty block list using a 1587 * splay tree algorithm. 1588 * 1589 * NOTE: xflags is passed as a constant, optimizing this inline function! 1590 */ 1591 static void 1592 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 1593 { 1594 struct buf *root; 1595 struct bufv *bv; 1596 1597 ASSERT_BO_LOCKED(bo); 1598 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1599 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 1600 bp->b_xflags |= xflags; 1601 if (xflags & BX_VNDIRTY) 1602 bv = &bo->bo_dirty; 1603 else 1604 bv = &bo->bo_clean; 1605 1606 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root); 1607 if (root == NULL) { 1608 bp->b_left = NULL; 1609 bp->b_right = NULL; 1610 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 1611 } else if (bp->b_lblkno < root->b_lblkno || 1612 (bp->b_lblkno == root->b_lblkno && 1613 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1614 bp->b_left = root->b_left; 1615 bp->b_right = root; 1616 root->b_left = NULL; 1617 TAILQ_INSERT_BEFORE(root, bp, b_bobufs); 1618 } else { 1619 bp->b_right = root->b_right; 1620 bp->b_left = root; 1621 root->b_right = NULL; 1622 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs); 1623 } 1624 bv->bv_cnt++; 1625 bv->bv_root = bp; 1626 } 1627 1628 /* 1629 * Lookup a buffer using the splay tree. Note that we specifically avoid 1630 * shadow buffers used in background bitmap writes. 1631 * 1632 * This code isn't quite efficient as it could be because we are maintaining 1633 * two sorted lists and do not know which list the block resides in. 1634 * 1635 * During a "make buildworld" the desired buffer is found at one of 1636 * the roots more than 60% of the time. Thus, checking both roots 1637 * before performing either splay eliminates unnecessary splays on the 1638 * first tree splayed. 1639 */ 1640 struct buf * 1641 gbincore(struct bufobj *bo, daddr_t lblkno) 1642 { 1643 struct buf *bp; 1644 1645 ASSERT_BO_LOCKED(bo); 1646 if ((bp = bo->bo_clean.bv_root) != NULL && 1647 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1648 return (bp); 1649 if ((bp = bo->bo_dirty.bv_root) != NULL && 1650 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1651 return (bp); 1652 if ((bp = bo->bo_clean.bv_root) != NULL) { 1653 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp); 1654 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1655 return (bp); 1656 } 1657 if ((bp = bo->bo_dirty.bv_root) != NULL) { 1658 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp); 1659 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1660 return (bp); 1661 } 1662 return (NULL); 1663 } 1664 1665 /* 1666 * Associate a buffer with a vnode. 1667 */ 1668 void 1669 bgetvp(struct vnode *vp, struct buf *bp) 1670 { 1671 struct bufobj *bo; 1672 1673 bo = &vp->v_bufobj; 1674 ASSERT_BO_LOCKED(bo); 1675 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 1676 1677 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 1678 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 1679 ("bgetvp: bp already attached! %p", bp)); 1680 1681 vhold(vp); 1682 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT) 1683 bp->b_flags |= B_NEEDSGIANT; 1684 bp->b_vp = vp; 1685 bp->b_bufobj = bo; 1686 /* 1687 * Insert onto list for new vnode. 1688 */ 1689 buf_vlist_add(bp, bo, BX_VNCLEAN); 1690 } 1691 1692 /* 1693 * Disassociate a buffer from a vnode. 1694 */ 1695 void 1696 brelvp(struct buf *bp) 1697 { 1698 struct bufobj *bo; 1699 struct vnode *vp; 1700 1701 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1702 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1703 1704 /* 1705 * Delete from old vnode list, if on one. 1706 */ 1707 vp = bp->b_vp; /* XXX */ 1708 bo = bp->b_bufobj; 1709 BO_LOCK(bo); 1710 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1711 buf_vlist_remove(bp); 1712 else 1713 panic("brelvp: Buffer %p not on queue.", bp); 1714 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 1715 bo->bo_flag &= ~BO_ONWORKLST; 1716 mtx_lock(&sync_mtx); 1717 LIST_REMOVE(bo, bo_synclist); 1718 syncer_worklist_len--; 1719 mtx_unlock(&sync_mtx); 1720 } 1721 bp->b_flags &= ~B_NEEDSGIANT; 1722 bp->b_vp = NULL; 1723 bp->b_bufobj = NULL; 1724 BO_UNLOCK(bo); 1725 vdrop(vp); 1726 } 1727 1728 /* 1729 * Add an item to the syncer work queue. 1730 */ 1731 static void 1732 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 1733 { 1734 int queue, slot; 1735 1736 ASSERT_BO_LOCKED(bo); 1737 1738 mtx_lock(&sync_mtx); 1739 if (bo->bo_flag & BO_ONWORKLST) 1740 LIST_REMOVE(bo, bo_synclist); 1741 else { 1742 bo->bo_flag |= BO_ONWORKLST; 1743 syncer_worklist_len++; 1744 } 1745 1746 if (delay > syncer_maxdelay - 2) 1747 delay = syncer_maxdelay - 2; 1748 slot = (syncer_delayno + delay) & syncer_mask; 1749 1750 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ : 1751 WI_MPSAFEQ; 1752 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo, 1753 bo_synclist); 1754 mtx_unlock(&sync_mtx); 1755 } 1756 1757 static int 1758 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 1759 { 1760 int error, len; 1761 1762 mtx_lock(&sync_mtx); 1763 len = syncer_worklist_len - sync_vnode_count; 1764 mtx_unlock(&sync_mtx); 1765 error = SYSCTL_OUT(req, &len, sizeof(len)); 1766 return (error); 1767 } 1768 1769 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 1770 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 1771 1772 static struct proc *updateproc; 1773 static void sched_sync(void); 1774 static struct kproc_desc up_kp = { 1775 "syncer", 1776 sched_sync, 1777 &updateproc 1778 }; 1779 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp); 1780 1781 static int 1782 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td) 1783 { 1784 struct vnode *vp; 1785 struct mount *mp; 1786 1787 *bo = LIST_FIRST(slp); 1788 if (*bo == NULL) 1789 return (0); 1790 vp = (*bo)->__bo_vnode; /* XXX */ 1791 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0) 1792 return (1); 1793 /* 1794 * We use vhold in case the vnode does not 1795 * successfully sync. vhold prevents the vnode from 1796 * going away when we unlock the sync_mtx so that 1797 * we can acquire the vnode interlock. 1798 */ 1799 vholdl(vp); 1800 mtx_unlock(&sync_mtx); 1801 VI_UNLOCK(vp); 1802 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1803 vdrop(vp); 1804 mtx_lock(&sync_mtx); 1805 return (*bo == LIST_FIRST(slp)); 1806 } 1807 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1808 (void) VOP_FSYNC(vp, MNT_LAZY, td); 1809 VOP_UNLOCK(vp, 0); 1810 vn_finished_write(mp); 1811 BO_LOCK(*bo); 1812 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) { 1813 /* 1814 * Put us back on the worklist. The worklist 1815 * routine will remove us from our current 1816 * position and then add us back in at a later 1817 * position. 1818 */ 1819 vn_syncer_add_to_worklist(*bo, syncdelay); 1820 } 1821 BO_UNLOCK(*bo); 1822 vdrop(vp); 1823 mtx_lock(&sync_mtx); 1824 return (0); 1825 } 1826 1827 /* 1828 * System filesystem synchronizer daemon. 1829 */ 1830 static void 1831 sched_sync(void) 1832 { 1833 struct synclist *gnext, *next; 1834 struct synclist *gslp, *slp; 1835 struct bufobj *bo; 1836 long starttime; 1837 struct thread *td = curthread; 1838 int last_work_seen; 1839 int net_worklist_len; 1840 int syncer_final_iter; 1841 int first_printf; 1842 int error; 1843 1844 last_work_seen = 0; 1845 syncer_final_iter = 0; 1846 first_printf = 1; 1847 syncer_state = SYNCER_RUNNING; 1848 starttime = time_uptime; 1849 td->td_pflags |= TDP_NORUNNINGBUF; 1850 1851 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 1852 SHUTDOWN_PRI_LAST); 1853 1854 mtx_lock(&sync_mtx); 1855 for (;;) { 1856 if (syncer_state == SYNCER_FINAL_DELAY && 1857 syncer_final_iter == 0) { 1858 mtx_unlock(&sync_mtx); 1859 kproc_suspend_check(td->td_proc); 1860 mtx_lock(&sync_mtx); 1861 } 1862 net_worklist_len = syncer_worklist_len - sync_vnode_count; 1863 if (syncer_state != SYNCER_RUNNING && 1864 starttime != time_uptime) { 1865 if (first_printf) { 1866 printf("\nSyncing disks, vnodes remaining..."); 1867 first_printf = 0; 1868 } 1869 printf("%d ", net_worklist_len); 1870 } 1871 starttime = time_uptime; 1872 1873 /* 1874 * Push files whose dirty time has expired. Be careful 1875 * of interrupt race on slp queue. 1876 * 1877 * Skip over empty worklist slots when shutting down. 1878 */ 1879 do { 1880 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno]; 1881 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno]; 1882 syncer_delayno += 1; 1883 if (syncer_delayno == syncer_maxdelay) 1884 syncer_delayno = 0; 1885 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno]; 1886 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno]; 1887 /* 1888 * If the worklist has wrapped since the 1889 * it was emptied of all but syncer vnodes, 1890 * switch to the FINAL_DELAY state and run 1891 * for one more second. 1892 */ 1893 if (syncer_state == SYNCER_SHUTTING_DOWN && 1894 net_worklist_len == 0 && 1895 last_work_seen == syncer_delayno) { 1896 syncer_state = SYNCER_FINAL_DELAY; 1897 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 1898 } 1899 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 1900 LIST_EMPTY(gslp) && syncer_worklist_len > 0); 1901 1902 /* 1903 * Keep track of the last time there was anything 1904 * on the worklist other than syncer vnodes. 1905 * Return to the SHUTTING_DOWN state if any 1906 * new work appears. 1907 */ 1908 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 1909 last_work_seen = syncer_delayno; 1910 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 1911 syncer_state = SYNCER_SHUTTING_DOWN; 1912 while (!LIST_EMPTY(slp)) { 1913 error = sync_vnode(slp, &bo, td); 1914 if (error == 1) { 1915 LIST_REMOVE(bo, bo_synclist); 1916 LIST_INSERT_HEAD(next, bo, bo_synclist); 1917 continue; 1918 } 1919 1920 if (first_printf == 0) 1921 wdog_kern_pat(WD_LASTVAL); 1922 1923 } 1924 if (!LIST_EMPTY(gslp)) { 1925 mtx_unlock(&sync_mtx); 1926 mtx_lock(&Giant); 1927 mtx_lock(&sync_mtx); 1928 while (!LIST_EMPTY(gslp)) { 1929 error = sync_vnode(gslp, &bo, td); 1930 if (error == 1) { 1931 LIST_REMOVE(bo, bo_synclist); 1932 LIST_INSERT_HEAD(gnext, bo, 1933 bo_synclist); 1934 continue; 1935 } 1936 } 1937 mtx_unlock(&Giant); 1938 } 1939 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 1940 syncer_final_iter--; 1941 /* 1942 * The variable rushjob allows the kernel to speed up the 1943 * processing of the filesystem syncer process. A rushjob 1944 * value of N tells the filesystem syncer to process the next 1945 * N seconds worth of work on its queue ASAP. Currently rushjob 1946 * is used by the soft update code to speed up the filesystem 1947 * syncer process when the incore state is getting so far 1948 * ahead of the disk that the kernel memory pool is being 1949 * threatened with exhaustion. 1950 */ 1951 if (rushjob > 0) { 1952 rushjob -= 1; 1953 continue; 1954 } 1955 /* 1956 * Just sleep for a short period of time between 1957 * iterations when shutting down to allow some I/O 1958 * to happen. 1959 * 1960 * If it has taken us less than a second to process the 1961 * current work, then wait. Otherwise start right over 1962 * again. We can still lose time if any single round 1963 * takes more than two seconds, but it does not really 1964 * matter as we are just trying to generally pace the 1965 * filesystem activity. 1966 */ 1967 if (syncer_state != SYNCER_RUNNING || 1968 time_uptime == starttime) { 1969 thread_lock(td); 1970 sched_prio(td, PPAUSE); 1971 thread_unlock(td); 1972 } 1973 if (syncer_state != SYNCER_RUNNING) 1974 cv_timedwait(&sync_wakeup, &sync_mtx, 1975 hz / SYNCER_SHUTDOWN_SPEEDUP); 1976 else if (time_uptime == starttime) 1977 cv_timedwait(&sync_wakeup, &sync_mtx, hz); 1978 } 1979 } 1980 1981 /* 1982 * Request the syncer daemon to speed up its work. 1983 * We never push it to speed up more than half of its 1984 * normal turn time, otherwise it could take over the cpu. 1985 */ 1986 int 1987 speedup_syncer(void) 1988 { 1989 int ret = 0; 1990 1991 mtx_lock(&sync_mtx); 1992 if (rushjob < syncdelay / 2) { 1993 rushjob += 1; 1994 stat_rush_requests += 1; 1995 ret = 1; 1996 } 1997 mtx_unlock(&sync_mtx); 1998 cv_broadcast(&sync_wakeup); 1999 return (ret); 2000 } 2001 2002 /* 2003 * Tell the syncer to speed up its work and run though its work 2004 * list several times, then tell it to shut down. 2005 */ 2006 static void 2007 syncer_shutdown(void *arg, int howto) 2008 { 2009 2010 if (howto & RB_NOSYNC) 2011 return; 2012 mtx_lock(&sync_mtx); 2013 syncer_state = SYNCER_SHUTTING_DOWN; 2014 rushjob = 0; 2015 mtx_unlock(&sync_mtx); 2016 cv_broadcast(&sync_wakeup); 2017 kproc_shutdown(arg, howto); 2018 } 2019 2020 /* 2021 * Reassign a buffer from one vnode to another. 2022 * Used to assign file specific control information 2023 * (indirect blocks) to the vnode to which they belong. 2024 */ 2025 void 2026 reassignbuf(struct buf *bp) 2027 { 2028 struct vnode *vp; 2029 struct bufobj *bo; 2030 int delay; 2031 #ifdef INVARIANTS 2032 struct bufv *bv; 2033 #endif 2034 2035 vp = bp->b_vp; 2036 bo = bp->b_bufobj; 2037 ++reassignbufcalls; 2038 2039 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 2040 bp, bp->b_vp, bp->b_flags); 2041 /* 2042 * B_PAGING flagged buffers cannot be reassigned because their vp 2043 * is not fully linked in. 2044 */ 2045 if (bp->b_flags & B_PAGING) 2046 panic("cannot reassign paging buffer"); 2047 2048 /* 2049 * Delete from old vnode list, if on one. 2050 */ 2051 BO_LOCK(bo); 2052 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 2053 buf_vlist_remove(bp); 2054 else 2055 panic("reassignbuf: Buffer %p not on queue.", bp); 2056 /* 2057 * If dirty, put on list of dirty buffers; otherwise insert onto list 2058 * of clean buffers. 2059 */ 2060 if (bp->b_flags & B_DELWRI) { 2061 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 2062 switch (vp->v_type) { 2063 case VDIR: 2064 delay = dirdelay; 2065 break; 2066 case VCHR: 2067 delay = metadelay; 2068 break; 2069 default: 2070 delay = filedelay; 2071 } 2072 vn_syncer_add_to_worklist(bo, delay); 2073 } 2074 buf_vlist_add(bp, bo, BX_VNDIRTY); 2075 } else { 2076 buf_vlist_add(bp, bo, BX_VNCLEAN); 2077 2078 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 2079 mtx_lock(&sync_mtx); 2080 LIST_REMOVE(bo, bo_synclist); 2081 syncer_worklist_len--; 2082 mtx_unlock(&sync_mtx); 2083 bo->bo_flag &= ~BO_ONWORKLST; 2084 } 2085 } 2086 #ifdef INVARIANTS 2087 bv = &bo->bo_clean; 2088 bp = TAILQ_FIRST(&bv->bv_hd); 2089 KASSERT(bp == NULL || bp->b_bufobj == bo, 2090 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2091 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2092 KASSERT(bp == NULL || bp->b_bufobj == bo, 2093 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2094 bv = &bo->bo_dirty; 2095 bp = TAILQ_FIRST(&bv->bv_hd); 2096 KASSERT(bp == NULL || bp->b_bufobj == bo, 2097 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2098 bp = TAILQ_LAST(&bv->bv_hd, buflists); 2099 KASSERT(bp == NULL || bp->b_bufobj == bo, 2100 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2101 #endif 2102 BO_UNLOCK(bo); 2103 } 2104 2105 /* 2106 * Increment the use and hold counts on the vnode, taking care to reference 2107 * the driver's usecount if this is a chardev. The vholdl() will remove 2108 * the vnode from the free list if it is presently free. Requires the 2109 * vnode interlock and returns with it held. 2110 */ 2111 static void 2112 v_incr_usecount(struct vnode *vp) 2113 { 2114 2115 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2116 vp->v_usecount++; 2117 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2118 dev_lock(); 2119 vp->v_rdev->si_usecount++; 2120 dev_unlock(); 2121 } 2122 vholdl(vp); 2123 } 2124 2125 /* 2126 * Turn a holdcnt into a use+holdcnt such that only one call to 2127 * v_decr_usecount is needed. 2128 */ 2129 static void 2130 v_upgrade_usecount(struct vnode *vp) 2131 { 2132 2133 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2134 vp->v_usecount++; 2135 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2136 dev_lock(); 2137 vp->v_rdev->si_usecount++; 2138 dev_unlock(); 2139 } 2140 } 2141 2142 /* 2143 * Decrement the vnode use and hold count along with the driver's usecount 2144 * if this is a chardev. The vdropl() below releases the vnode interlock 2145 * as it may free the vnode. 2146 */ 2147 static void 2148 v_decr_usecount(struct vnode *vp) 2149 { 2150 2151 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2152 VNASSERT(vp->v_usecount > 0, vp, 2153 ("v_decr_usecount: negative usecount")); 2154 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2155 vp->v_usecount--; 2156 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2157 dev_lock(); 2158 vp->v_rdev->si_usecount--; 2159 dev_unlock(); 2160 } 2161 vdropl(vp); 2162 } 2163 2164 /* 2165 * Decrement only the use count and driver use count. This is intended to 2166 * be paired with a follow on vdropl() to release the remaining hold count. 2167 * In this way we may vgone() a vnode with a 0 usecount without risk of 2168 * having it end up on a free list because the hold count is kept above 0. 2169 */ 2170 static void 2171 v_decr_useonly(struct vnode *vp) 2172 { 2173 2174 ASSERT_VI_LOCKED(vp, __FUNCTION__); 2175 VNASSERT(vp->v_usecount > 0, vp, 2176 ("v_decr_useonly: negative usecount")); 2177 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2178 vp->v_usecount--; 2179 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2180 dev_lock(); 2181 vp->v_rdev->si_usecount--; 2182 dev_unlock(); 2183 } 2184 } 2185 2186 /* 2187 * Grab a particular vnode from the free list, increment its 2188 * reference count and lock it. VI_DOOMED is set if the vnode 2189 * is being destroyed. Only callers who specify LK_RETRY will 2190 * see doomed vnodes. If inactive processing was delayed in 2191 * vput try to do it here. 2192 */ 2193 int 2194 vget(struct vnode *vp, int flags, struct thread *td) 2195 { 2196 int error; 2197 2198 error = 0; 2199 VFS_ASSERT_GIANT(vp->v_mount); 2200 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2201 ("vget: invalid lock operation")); 2202 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags); 2203 2204 if ((flags & LK_INTERLOCK) == 0) 2205 VI_LOCK(vp); 2206 vholdl(vp); 2207 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) { 2208 vdrop(vp); 2209 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__, 2210 vp); 2211 return (error); 2212 } 2213 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0) 2214 panic("vget: vn_lock failed to return ENOENT\n"); 2215 VI_LOCK(vp); 2216 /* Upgrade our holdcnt to a usecount. */ 2217 v_upgrade_usecount(vp); 2218 /* 2219 * We don't guarantee that any particular close will 2220 * trigger inactive processing so just make a best effort 2221 * here at preventing a reference to a removed file. If 2222 * we don't succeed no harm is done. 2223 */ 2224 if (vp->v_iflag & VI_OWEINACT) { 2225 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && 2226 (flags & LK_NOWAIT) == 0) 2227 vinactive(vp, td); 2228 vp->v_iflag &= ~VI_OWEINACT; 2229 } 2230 VI_UNLOCK(vp); 2231 return (0); 2232 } 2233 2234 /* 2235 * Increase the reference count of a vnode. 2236 */ 2237 void 2238 vref(struct vnode *vp) 2239 { 2240 2241 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2242 VI_LOCK(vp); 2243 v_incr_usecount(vp); 2244 VI_UNLOCK(vp); 2245 } 2246 2247 /* 2248 * Return reference count of a vnode. 2249 * 2250 * The results of this call are only guaranteed when some mechanism other 2251 * than the VI lock is used to stop other processes from gaining references 2252 * to the vnode. This may be the case if the caller holds the only reference. 2253 * This is also useful when stale data is acceptable as race conditions may 2254 * be accounted for by some other means. 2255 */ 2256 int 2257 vrefcnt(struct vnode *vp) 2258 { 2259 int usecnt; 2260 2261 VI_LOCK(vp); 2262 usecnt = vp->v_usecount; 2263 VI_UNLOCK(vp); 2264 2265 return (usecnt); 2266 } 2267 2268 #define VPUTX_VRELE 1 2269 #define VPUTX_VPUT 2 2270 #define VPUTX_VUNREF 3 2271 2272 static void 2273 vputx(struct vnode *vp, int func) 2274 { 2275 int error; 2276 2277 KASSERT(vp != NULL, ("vputx: null vp")); 2278 if (func == VPUTX_VUNREF) 2279 ASSERT_VOP_LOCKED(vp, "vunref"); 2280 else if (func == VPUTX_VPUT) 2281 ASSERT_VOP_LOCKED(vp, "vput"); 2282 else 2283 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func")); 2284 VFS_ASSERT_GIANT(vp->v_mount); 2285 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2286 VI_LOCK(vp); 2287 2288 /* Skip this v_writecount check if we're going to panic below. */ 2289 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp, 2290 ("vputx: missed vn_close")); 2291 error = 0; 2292 2293 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2294 vp->v_usecount == 1)) { 2295 if (func == VPUTX_VPUT) 2296 VOP_UNLOCK(vp, 0); 2297 v_decr_usecount(vp); 2298 return; 2299 } 2300 2301 if (vp->v_usecount != 1) { 2302 vprint("vputx: negative ref count", vp); 2303 panic("vputx: negative ref cnt"); 2304 } 2305 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp); 2306 /* 2307 * We want to hold the vnode until the inactive finishes to 2308 * prevent vgone() races. We drop the use count here and the 2309 * hold count below when we're done. 2310 */ 2311 v_decr_useonly(vp); 2312 /* 2313 * We must call VOP_INACTIVE with the node locked. Mark 2314 * as VI_DOINGINACT to avoid recursion. 2315 */ 2316 vp->v_iflag |= VI_OWEINACT; 2317 switch (func) { 2318 case VPUTX_VRELE: 2319 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK); 2320 VI_LOCK(vp); 2321 break; 2322 case VPUTX_VPUT: 2323 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) { 2324 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK | 2325 LK_NOWAIT); 2326 VI_LOCK(vp); 2327 } 2328 break; 2329 case VPUTX_VUNREF: 2330 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 2331 error = EBUSY; 2332 break; 2333 } 2334 if (vp->v_usecount > 0) 2335 vp->v_iflag &= ~VI_OWEINACT; 2336 if (error == 0) { 2337 if (vp->v_iflag & VI_OWEINACT) 2338 vinactive(vp, curthread); 2339 if (func != VPUTX_VUNREF) 2340 VOP_UNLOCK(vp, 0); 2341 } 2342 vdropl(vp); 2343 } 2344 2345 /* 2346 * Vnode put/release. 2347 * If count drops to zero, call inactive routine and return to freelist. 2348 */ 2349 void 2350 vrele(struct vnode *vp) 2351 { 2352 2353 vputx(vp, VPUTX_VRELE); 2354 } 2355 2356 /* 2357 * Release an already locked vnode. This give the same effects as 2358 * unlock+vrele(), but takes less time and avoids releasing and 2359 * re-aquiring the lock (as vrele() acquires the lock internally.) 2360 */ 2361 void 2362 vput(struct vnode *vp) 2363 { 2364 2365 vputx(vp, VPUTX_VPUT); 2366 } 2367 2368 /* 2369 * Release an exclusively locked vnode. Do not unlock the vnode lock. 2370 */ 2371 void 2372 vunref(struct vnode *vp) 2373 { 2374 2375 vputx(vp, VPUTX_VUNREF); 2376 } 2377 2378 /* 2379 * Somebody doesn't want the vnode recycled. 2380 */ 2381 void 2382 vhold(struct vnode *vp) 2383 { 2384 2385 VI_LOCK(vp); 2386 vholdl(vp); 2387 VI_UNLOCK(vp); 2388 } 2389 2390 /* 2391 * Increase the hold count and activate if this is the first reference. 2392 */ 2393 void 2394 vholdl(struct vnode *vp) 2395 { 2396 struct mount *mp; 2397 2398 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2399 vp->v_holdcnt++; 2400 if (!VSHOULDBUSY(vp)) 2401 return; 2402 ASSERT_VI_LOCKED(vp, "vholdl"); 2403 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free")); 2404 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed.")); 2405 /* 2406 * Remove a vnode from the free list, mark it as in use, 2407 * and put it on the active list. 2408 */ 2409 mtx_lock(&vnode_free_list_mtx); 2410 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist); 2411 freevnodes--; 2412 vp->v_iflag &= ~(VI_FREE|VI_AGE); 2413 KASSERT((vp->v_iflag & VI_ACTIVE) == 0, 2414 ("Activating already active vnode")); 2415 vp->v_iflag |= VI_ACTIVE; 2416 mp = vp->v_mount; 2417 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist); 2418 mp->mnt_activevnodelistsize++; 2419 mtx_unlock(&vnode_free_list_mtx); 2420 } 2421 2422 /* 2423 * Note that there is one less who cares about this vnode. 2424 * vdrop() is the opposite of vhold(). 2425 */ 2426 void 2427 vdrop(struct vnode *vp) 2428 { 2429 2430 VI_LOCK(vp); 2431 vdropl(vp); 2432 } 2433 2434 /* 2435 * Drop the hold count of the vnode. If this is the last reference to 2436 * the vnode we place it on the free list unless it has been vgone'd 2437 * (marked VI_DOOMED) in which case we will free it. 2438 */ 2439 void 2440 vdropl(struct vnode *vp) 2441 { 2442 struct bufobj *bo; 2443 struct mount *mp; 2444 int active; 2445 2446 ASSERT_VI_LOCKED(vp, "vdropl"); 2447 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2448 if (vp->v_holdcnt <= 0) 2449 panic("vdrop: holdcnt %d", vp->v_holdcnt); 2450 vp->v_holdcnt--; 2451 if (vp->v_holdcnt > 0) { 2452 VI_UNLOCK(vp); 2453 return; 2454 } 2455 if ((vp->v_iflag & VI_DOOMED) == 0) { 2456 /* 2457 * Mark a vnode as free: remove it from its active list 2458 * and put it up for recycling on the freelist. 2459 */ 2460 VNASSERT(vp->v_op != NULL, vp, 2461 ("vdropl: vnode already reclaimed.")); 2462 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2463 ("vnode already free")); 2464 VNASSERT(VSHOULDFREE(vp), vp, 2465 ("vdropl: freeing when we shouldn't")); 2466 active = vp->v_iflag & VI_ACTIVE; 2467 vp->v_iflag &= ~VI_ACTIVE; 2468 mp = vp->v_mount; 2469 mtx_lock(&vnode_free_list_mtx); 2470 if (active) { 2471 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, 2472 v_actfreelist); 2473 mp->mnt_activevnodelistsize--; 2474 } 2475 if (vp->v_iflag & VI_AGE) { 2476 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist); 2477 } else { 2478 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist); 2479 } 2480 freevnodes++; 2481 vp->v_iflag &= ~VI_AGE; 2482 vp->v_iflag |= VI_FREE; 2483 mtx_unlock(&vnode_free_list_mtx); 2484 VI_UNLOCK(vp); 2485 return; 2486 } 2487 /* 2488 * The vnode has been marked for destruction, so free it. 2489 */ 2490 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp); 2491 mtx_lock(&vnode_free_list_mtx); 2492 numvnodes--; 2493 mtx_unlock(&vnode_free_list_mtx); 2494 bo = &vp->v_bufobj; 2495 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, 2496 ("cleaned vnode still on the free list.")); 2497 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't")); 2498 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count")); 2499 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count")); 2500 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count")); 2501 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's")); 2502 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0")); 2503 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL")); 2504 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0")); 2505 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL")); 2506 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst")); 2507 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src")); 2508 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for ..")); 2509 VI_UNLOCK(vp); 2510 #ifdef MAC 2511 mac_vnode_destroy(vp); 2512 #endif 2513 if (vp->v_pollinfo != NULL) 2514 destroy_vpollinfo(vp->v_pollinfo); 2515 #ifdef INVARIANTS 2516 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */ 2517 vp->v_op = NULL; 2518 #endif 2519 rangelock_destroy(&vp->v_rl); 2520 lockdestroy(vp->v_vnlock); 2521 mtx_destroy(&vp->v_interlock); 2522 mtx_destroy(BO_MTX(bo)); 2523 uma_zfree(vnode_zone, vp); 2524 } 2525 2526 /* 2527 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 2528 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 2529 * OWEINACT tracks whether a vnode missed a call to inactive due to a 2530 * failed lock upgrade. 2531 */ 2532 void 2533 vinactive(struct vnode *vp, struct thread *td) 2534 { 2535 struct vm_object *obj; 2536 2537 ASSERT_VOP_ELOCKED(vp, "vinactive"); 2538 ASSERT_VI_LOCKED(vp, "vinactive"); 2539 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 2540 ("vinactive: recursed on VI_DOINGINACT")); 2541 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2542 vp->v_iflag |= VI_DOINGINACT; 2543 vp->v_iflag &= ~VI_OWEINACT; 2544 VI_UNLOCK(vp); 2545 /* 2546 * Before moving off the active list, we must be sure that any 2547 * modified pages are on the vnode's dirty list since these will 2548 * no longer be checked once the vnode is on the inactive list. 2549 */ 2550 obj = vp->v_object; 2551 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) { 2552 VM_OBJECT_LOCK(obj); 2553 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC); 2554 VM_OBJECT_UNLOCK(obj); 2555 } 2556 VOP_INACTIVE(vp, td); 2557 VI_LOCK(vp); 2558 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 2559 ("vinactive: lost VI_DOINGINACT")); 2560 vp->v_iflag &= ~VI_DOINGINACT; 2561 } 2562 2563 /* 2564 * Remove any vnodes in the vnode table belonging to mount point mp. 2565 * 2566 * If FORCECLOSE is not specified, there should not be any active ones, 2567 * return error if any are found (nb: this is a user error, not a 2568 * system error). If FORCECLOSE is specified, detach any active vnodes 2569 * that are found. 2570 * 2571 * If WRITECLOSE is set, only flush out regular file vnodes open for 2572 * writing. 2573 * 2574 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2575 * 2576 * `rootrefs' specifies the base reference count for the root vnode 2577 * of this filesystem. The root vnode is considered busy if its 2578 * v_usecount exceeds this value. On a successful return, vflush(, td) 2579 * will call vrele() on the root vnode exactly rootrefs times. 2580 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2581 * be zero. 2582 */ 2583 #ifdef DIAGNOSTIC 2584 static int busyprt = 0; /* print out busy vnodes */ 2585 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes"); 2586 #endif 2587 2588 int 2589 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td) 2590 { 2591 struct vnode *vp, *mvp, *rootvp = NULL; 2592 struct vattr vattr; 2593 int busy = 0, error; 2594 2595 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp, 2596 rootrefs, flags); 2597 if (rootrefs > 0) { 2598 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2599 ("vflush: bad args")); 2600 /* 2601 * Get the filesystem root vnode. We can vput() it 2602 * immediately, since with rootrefs > 0, it won't go away. 2603 */ 2604 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) { 2605 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d", 2606 __func__, error); 2607 return (error); 2608 } 2609 vput(rootvp); 2610 } 2611 loop: 2612 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 2613 vholdl(vp); 2614 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE); 2615 if (error) { 2616 vdrop(vp); 2617 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 2618 goto loop; 2619 } 2620 /* 2621 * Skip over a vnodes marked VV_SYSTEM. 2622 */ 2623 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2624 VOP_UNLOCK(vp, 0); 2625 vdrop(vp); 2626 continue; 2627 } 2628 /* 2629 * If WRITECLOSE is set, flush out unlinked but still open 2630 * files (even if open only for reading) and regular file 2631 * vnodes open for writing. 2632 */ 2633 if (flags & WRITECLOSE) { 2634 if (vp->v_object != NULL) { 2635 VM_OBJECT_LOCK(vp->v_object); 2636 vm_object_page_clean(vp->v_object, 0, 0, 0); 2637 VM_OBJECT_UNLOCK(vp->v_object); 2638 } 2639 error = VOP_FSYNC(vp, MNT_WAIT, td); 2640 if (error != 0) { 2641 VOP_UNLOCK(vp, 0); 2642 vdrop(vp); 2643 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 2644 return (error); 2645 } 2646 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 2647 VI_LOCK(vp); 2648 2649 if ((vp->v_type == VNON || 2650 (error == 0 && vattr.va_nlink > 0)) && 2651 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2652 VOP_UNLOCK(vp, 0); 2653 vdropl(vp); 2654 continue; 2655 } 2656 } else 2657 VI_LOCK(vp); 2658 /* 2659 * With v_usecount == 0, all we need to do is clear out the 2660 * vnode data structures and we are done. 2661 * 2662 * If FORCECLOSE is set, forcibly close the vnode. 2663 */ 2664 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 2665 VNASSERT(vp->v_usecount == 0 || 2666 (vp->v_type != VCHR && vp->v_type != VBLK), vp, 2667 ("device VNODE %p is FORCECLOSED", vp)); 2668 vgonel(vp); 2669 } else { 2670 busy++; 2671 #ifdef DIAGNOSTIC 2672 if (busyprt) 2673 vprint("vflush: busy vnode", vp); 2674 #endif 2675 } 2676 VOP_UNLOCK(vp, 0); 2677 vdropl(vp); 2678 } 2679 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2680 /* 2681 * If just the root vnode is busy, and if its refcount 2682 * is equal to `rootrefs', then go ahead and kill it. 2683 */ 2684 VI_LOCK(rootvp); 2685 KASSERT(busy > 0, ("vflush: not busy")); 2686 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 2687 ("vflush: usecount %d < rootrefs %d", 2688 rootvp->v_usecount, rootrefs)); 2689 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2690 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK); 2691 vgone(rootvp); 2692 VOP_UNLOCK(rootvp, 0); 2693 busy = 0; 2694 } else 2695 VI_UNLOCK(rootvp); 2696 } 2697 if (busy) { 2698 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__, 2699 busy); 2700 return (EBUSY); 2701 } 2702 for (; rootrefs > 0; rootrefs--) 2703 vrele(rootvp); 2704 return (0); 2705 } 2706 2707 /* 2708 * Recycle an unused vnode to the front of the free list. 2709 */ 2710 int 2711 vrecycle(struct vnode *vp) 2712 { 2713 int recycled; 2714 2715 ASSERT_VOP_ELOCKED(vp, "vrecycle"); 2716 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2717 recycled = 0; 2718 VI_LOCK(vp); 2719 if (vp->v_usecount == 0) { 2720 recycled = 1; 2721 vgonel(vp); 2722 } 2723 VI_UNLOCK(vp); 2724 return (recycled); 2725 } 2726 2727 /* 2728 * Eliminate all activity associated with a vnode 2729 * in preparation for reuse. 2730 */ 2731 void 2732 vgone(struct vnode *vp) 2733 { 2734 VI_LOCK(vp); 2735 vgonel(vp); 2736 VI_UNLOCK(vp); 2737 } 2738 2739 static void 2740 vgonel_reclaim_lowervp_vfs(struct mount *mp __unused, 2741 struct vnode *lowervp __unused) 2742 { 2743 } 2744 2745 /* 2746 * Notify upper mounts about reclaimed vnode. 2747 */ 2748 static void 2749 vgonel_reclaim_lowervp(struct vnode *vp) 2750 { 2751 static struct vfsops vgonel_vfsops = { 2752 .vfs_reclaim_lowervp = vgonel_reclaim_lowervp_vfs 2753 }; 2754 struct mount *mp, *ump, *mmp; 2755 2756 mp = vp->v_mount; 2757 if (mp == NULL) 2758 return; 2759 2760 MNT_ILOCK(mp); 2761 if (TAILQ_EMPTY(&mp->mnt_uppers)) 2762 goto unlock; 2763 MNT_IUNLOCK(mp); 2764 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO); 2765 mmp->mnt_op = &vgonel_vfsops; 2766 mmp->mnt_kern_flag |= MNTK_MARKER; 2767 MNT_ILOCK(mp); 2768 mp->mnt_kern_flag |= MNTK_VGONE_UPPER; 2769 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) { 2770 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) { 2771 ump = TAILQ_NEXT(ump, mnt_upper_link); 2772 continue; 2773 } 2774 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link); 2775 MNT_IUNLOCK(mp); 2776 VFS_RECLAIM_LOWERVP(ump, vp); 2777 MNT_ILOCK(mp); 2778 ump = TAILQ_NEXT(mmp, mnt_upper_link); 2779 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link); 2780 } 2781 free(mmp, M_TEMP); 2782 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER; 2783 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) { 2784 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER; 2785 wakeup(&mp->mnt_uppers); 2786 } 2787 unlock: 2788 MNT_IUNLOCK(mp); 2789 } 2790 2791 /* 2792 * vgone, with the vp interlock held. 2793 */ 2794 void 2795 vgonel(struct vnode *vp) 2796 { 2797 struct thread *td; 2798 int oweinact; 2799 int active; 2800 struct mount *mp; 2801 2802 ASSERT_VOP_ELOCKED(vp, "vgonel"); 2803 ASSERT_VI_LOCKED(vp, "vgonel"); 2804 VNASSERT(vp->v_holdcnt, vp, 2805 ("vgonel: vp %p has no reference.", vp)); 2806 CTR2(KTR_VFS, "%s: vp %p", __func__, vp); 2807 td = curthread; 2808 2809 /* 2810 * Don't vgonel if we're already doomed. 2811 */ 2812 if (vp->v_iflag & VI_DOOMED) 2813 return; 2814 vp->v_iflag |= VI_DOOMED; 2815 2816 /* 2817 * Check to see if the vnode is in use. If so, we have to call 2818 * VOP_CLOSE() and VOP_INACTIVE(). 2819 */ 2820 active = vp->v_usecount; 2821 oweinact = (vp->v_iflag & VI_OWEINACT); 2822 VI_UNLOCK(vp); 2823 vgonel_reclaim_lowervp(vp); 2824 2825 /* 2826 * Clean out any buffers associated with the vnode. 2827 * If the flush fails, just toss the buffers. 2828 */ 2829 mp = NULL; 2830 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 2831 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 2832 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) 2833 vinvalbuf(vp, 0, 0, 0); 2834 2835 /* 2836 * If purging an active vnode, it must be closed and 2837 * deactivated before being reclaimed. 2838 */ 2839 if (active) 2840 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2841 if (oweinact || active) { 2842 VI_LOCK(vp); 2843 if ((vp->v_iflag & VI_DOINGINACT) == 0) 2844 vinactive(vp, td); 2845 VI_UNLOCK(vp); 2846 } 2847 if (vp->v_type == VSOCK) 2848 vfs_unp_reclaim(vp); 2849 /* 2850 * Reclaim the vnode. 2851 */ 2852 if (VOP_RECLAIM(vp, td)) 2853 panic("vgone: cannot reclaim"); 2854 if (mp != NULL) 2855 vn_finished_secondary_write(mp); 2856 VNASSERT(vp->v_object == NULL, vp, 2857 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag)); 2858 /* 2859 * Clear the advisory locks and wake up waiting threads. 2860 */ 2861 (void)VOP_ADVLOCKPURGE(vp); 2862 /* 2863 * Delete from old mount point vnode list. 2864 */ 2865 delmntque(vp); 2866 cache_purge(vp); 2867 /* 2868 * Done with purge, reset to the standard lock and invalidate 2869 * the vnode. 2870 */ 2871 VI_LOCK(vp); 2872 vp->v_vnlock = &vp->v_lock; 2873 vp->v_op = &dead_vnodeops; 2874 vp->v_tag = "none"; 2875 vp->v_type = VBAD; 2876 } 2877 2878 /* 2879 * Calculate the total number of references to a special device. 2880 */ 2881 int 2882 vcount(struct vnode *vp) 2883 { 2884 int count; 2885 2886 dev_lock(); 2887 count = vp->v_rdev->si_usecount; 2888 dev_unlock(); 2889 return (count); 2890 } 2891 2892 /* 2893 * Same as above, but using the struct cdev *as argument 2894 */ 2895 int 2896 count_dev(struct cdev *dev) 2897 { 2898 int count; 2899 2900 dev_lock(); 2901 count = dev->si_usecount; 2902 dev_unlock(); 2903 return(count); 2904 } 2905 2906 /* 2907 * Print out a description of a vnode. 2908 */ 2909 static char *typename[] = 2910 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 2911 "VMARKER"}; 2912 2913 void 2914 vn_printf(struct vnode *vp, const char *fmt, ...) 2915 { 2916 va_list ap; 2917 char buf[256], buf2[16]; 2918 u_long flags; 2919 2920 va_start(ap, fmt); 2921 vprintf(fmt, ap); 2922 va_end(ap); 2923 printf("%p: ", (void *)vp); 2924 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]); 2925 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n", 2926 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere); 2927 buf[0] = '\0'; 2928 buf[1] = '\0'; 2929 if (vp->v_vflag & VV_ROOT) 2930 strlcat(buf, "|VV_ROOT", sizeof(buf)); 2931 if (vp->v_vflag & VV_ISTTY) 2932 strlcat(buf, "|VV_ISTTY", sizeof(buf)); 2933 if (vp->v_vflag & VV_NOSYNC) 2934 strlcat(buf, "|VV_NOSYNC", sizeof(buf)); 2935 if (vp->v_vflag & VV_CACHEDLABEL) 2936 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf)); 2937 if (vp->v_vflag & VV_TEXT) 2938 strlcat(buf, "|VV_TEXT", sizeof(buf)); 2939 if (vp->v_vflag & VV_COPYONWRITE) 2940 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf)); 2941 if (vp->v_vflag & VV_SYSTEM) 2942 strlcat(buf, "|VV_SYSTEM", sizeof(buf)); 2943 if (vp->v_vflag & VV_PROCDEP) 2944 strlcat(buf, "|VV_PROCDEP", sizeof(buf)); 2945 if (vp->v_vflag & VV_NOKNOTE) 2946 strlcat(buf, "|VV_NOKNOTE", sizeof(buf)); 2947 if (vp->v_vflag & VV_DELETED) 2948 strlcat(buf, "|VV_DELETED", sizeof(buf)); 2949 if (vp->v_vflag & VV_MD) 2950 strlcat(buf, "|VV_MD", sizeof(buf)); 2951 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | 2952 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP | 2953 VV_NOKNOTE | VV_DELETED | VV_MD); 2954 if (flags != 0) { 2955 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags); 2956 strlcat(buf, buf2, sizeof(buf)); 2957 } 2958 if (vp->v_iflag & VI_MOUNT) 2959 strlcat(buf, "|VI_MOUNT", sizeof(buf)); 2960 if (vp->v_iflag & VI_AGE) 2961 strlcat(buf, "|VI_AGE", sizeof(buf)); 2962 if (vp->v_iflag & VI_DOOMED) 2963 strlcat(buf, "|VI_DOOMED", sizeof(buf)); 2964 if (vp->v_iflag & VI_FREE) 2965 strlcat(buf, "|VI_FREE", sizeof(buf)); 2966 if (vp->v_iflag & VI_DOINGINACT) 2967 strlcat(buf, "|VI_DOINGINACT", sizeof(buf)); 2968 if (vp->v_iflag & VI_OWEINACT) 2969 strlcat(buf, "|VI_OWEINACT", sizeof(buf)); 2970 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE | 2971 VI_DOINGINACT | VI_OWEINACT); 2972 if (flags != 0) { 2973 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags); 2974 strlcat(buf, buf2, sizeof(buf)); 2975 } 2976 printf(" flags (%s)\n", buf + 1); 2977 if (mtx_owned(VI_MTX(vp))) 2978 printf(" VI_LOCKed"); 2979 if (vp->v_object != NULL) 2980 printf(" v_object %p ref %d pages %d\n", 2981 vp->v_object, vp->v_object->ref_count, 2982 vp->v_object->resident_page_count); 2983 printf(" "); 2984 lockmgr_printinfo(vp->v_vnlock); 2985 if (vp->v_data != NULL) 2986 VOP_PRINT(vp); 2987 } 2988 2989 #ifdef DDB 2990 /* 2991 * List all of the locked vnodes in the system. 2992 * Called when debugging the kernel. 2993 */ 2994 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2995 { 2996 struct mount *mp, *nmp; 2997 struct vnode *vp; 2998 2999 /* 3000 * Note: because this is DDB, we can't obey the locking semantics 3001 * for these structures, which means we could catch an inconsistent 3002 * state and dereference a nasty pointer. Not much to be done 3003 * about that. 3004 */ 3005 db_printf("Locked vnodes\n"); 3006 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 3007 nmp = TAILQ_NEXT(mp, mnt_list); 3008 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3009 if (vp->v_type != VMARKER && 3010 VOP_ISLOCKED(vp)) 3011 vprint("", vp); 3012 } 3013 nmp = TAILQ_NEXT(mp, mnt_list); 3014 } 3015 } 3016 3017 /* 3018 * Show details about the given vnode. 3019 */ 3020 DB_SHOW_COMMAND(vnode, db_show_vnode) 3021 { 3022 struct vnode *vp; 3023 3024 if (!have_addr) 3025 return; 3026 vp = (struct vnode *)addr; 3027 vn_printf(vp, "vnode "); 3028 } 3029 3030 /* 3031 * Show details about the given mount point. 3032 */ 3033 DB_SHOW_COMMAND(mount, db_show_mount) 3034 { 3035 struct mount *mp; 3036 struct vfsopt *opt; 3037 struct statfs *sp; 3038 struct vnode *vp; 3039 char buf[512]; 3040 uint64_t mflags; 3041 u_int flags; 3042 3043 if (!have_addr) { 3044 /* No address given, print short info about all mount points. */ 3045 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3046 db_printf("%p %s on %s (%s)\n", mp, 3047 mp->mnt_stat.f_mntfromname, 3048 mp->mnt_stat.f_mntonname, 3049 mp->mnt_stat.f_fstypename); 3050 if (db_pager_quit) 3051 break; 3052 } 3053 db_printf("\nMore info: show mount <addr>\n"); 3054 return; 3055 } 3056 3057 mp = (struct mount *)addr; 3058 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname, 3059 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename); 3060 3061 buf[0] = '\0'; 3062 mflags = mp->mnt_flag; 3063 #define MNT_FLAG(flag) do { \ 3064 if (mflags & (flag)) { \ 3065 if (buf[0] != '\0') \ 3066 strlcat(buf, ", ", sizeof(buf)); \ 3067 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 3068 mflags &= ~(flag); \ 3069 } \ 3070 } while (0) 3071 MNT_FLAG(MNT_RDONLY); 3072 MNT_FLAG(MNT_SYNCHRONOUS); 3073 MNT_FLAG(MNT_NOEXEC); 3074 MNT_FLAG(MNT_NOSUID); 3075 MNT_FLAG(MNT_UNION); 3076 MNT_FLAG(MNT_ASYNC); 3077 MNT_FLAG(MNT_SUIDDIR); 3078 MNT_FLAG(MNT_SOFTDEP); 3079 MNT_FLAG(MNT_SUJ); 3080 MNT_FLAG(MNT_NOSYMFOLLOW); 3081 MNT_FLAG(MNT_GJOURNAL); 3082 MNT_FLAG(MNT_MULTILABEL); 3083 MNT_FLAG(MNT_ACLS); 3084 MNT_FLAG(MNT_NOATIME); 3085 MNT_FLAG(MNT_NOCLUSTERR); 3086 MNT_FLAG(MNT_NOCLUSTERW); 3087 MNT_FLAG(MNT_NFS4ACLS); 3088 MNT_FLAG(MNT_EXRDONLY); 3089 MNT_FLAG(MNT_EXPORTED); 3090 MNT_FLAG(MNT_DEFEXPORTED); 3091 MNT_FLAG(MNT_EXPORTANON); 3092 MNT_FLAG(MNT_EXKERB); 3093 MNT_FLAG(MNT_EXPUBLIC); 3094 MNT_FLAG(MNT_LOCAL); 3095 MNT_FLAG(MNT_QUOTA); 3096 MNT_FLAG(MNT_ROOTFS); 3097 MNT_FLAG(MNT_USER); 3098 MNT_FLAG(MNT_IGNORE); 3099 MNT_FLAG(MNT_UPDATE); 3100 MNT_FLAG(MNT_DELEXPORT); 3101 MNT_FLAG(MNT_RELOAD); 3102 MNT_FLAG(MNT_FORCE); 3103 MNT_FLAG(MNT_SNAPSHOT); 3104 MNT_FLAG(MNT_BYFSID); 3105 #undef MNT_FLAG 3106 if (mflags != 0) { 3107 if (buf[0] != '\0') 3108 strlcat(buf, ", ", sizeof(buf)); 3109 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3110 "0x%016jx", mflags); 3111 } 3112 db_printf(" mnt_flag = %s\n", buf); 3113 3114 buf[0] = '\0'; 3115 flags = mp->mnt_kern_flag; 3116 #define MNT_KERN_FLAG(flag) do { \ 3117 if (flags & (flag)) { \ 3118 if (buf[0] != '\0') \ 3119 strlcat(buf, ", ", sizeof(buf)); \ 3120 strlcat(buf, (#flag) + 5, sizeof(buf)); \ 3121 flags &= ~(flag); \ 3122 } \ 3123 } while (0) 3124 MNT_KERN_FLAG(MNTK_UNMOUNTF); 3125 MNT_KERN_FLAG(MNTK_ASYNC); 3126 MNT_KERN_FLAG(MNTK_SOFTDEP); 3127 MNT_KERN_FLAG(MNTK_NOINSMNTQ); 3128 MNT_KERN_FLAG(MNTK_DRAINING); 3129 MNT_KERN_FLAG(MNTK_REFEXPIRE); 3130 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED); 3131 MNT_KERN_FLAG(MNTK_SHARED_WRITES); 3132 MNT_KERN_FLAG(MNTK_NOASYNC); 3133 MNT_KERN_FLAG(MNTK_UNMOUNT); 3134 MNT_KERN_FLAG(MNTK_MWAIT); 3135 MNT_KERN_FLAG(MNTK_SUSPEND); 3136 MNT_KERN_FLAG(MNTK_SUSPEND2); 3137 MNT_KERN_FLAG(MNTK_SUSPENDED); 3138 MNT_KERN_FLAG(MNTK_MPSAFE); 3139 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED); 3140 MNT_KERN_FLAG(MNTK_NOKNOTE); 3141 #undef MNT_KERN_FLAG 3142 if (flags != 0) { 3143 if (buf[0] != '\0') 3144 strlcat(buf, ", ", sizeof(buf)); 3145 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 3146 "0x%08x", flags); 3147 } 3148 db_printf(" mnt_kern_flag = %s\n", buf); 3149 3150 db_printf(" mnt_opt = "); 3151 opt = TAILQ_FIRST(mp->mnt_opt); 3152 if (opt != NULL) { 3153 db_printf("%s", opt->name); 3154 opt = TAILQ_NEXT(opt, link); 3155 while (opt != NULL) { 3156 db_printf(", %s", opt->name); 3157 opt = TAILQ_NEXT(opt, link); 3158 } 3159 } 3160 db_printf("\n"); 3161 3162 sp = &mp->mnt_stat; 3163 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx " 3164 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju " 3165 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju " 3166 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n", 3167 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags, 3168 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize, 3169 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree, 3170 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files, 3171 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites, 3172 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads, 3173 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax, 3174 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]); 3175 3176 db_printf(" mnt_cred = { uid=%u ruid=%u", 3177 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid); 3178 if (jailed(mp->mnt_cred)) 3179 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id); 3180 db_printf(" }\n"); 3181 db_printf(" mnt_ref = %d\n", mp->mnt_ref); 3182 db_printf(" mnt_gen = %d\n", mp->mnt_gen); 3183 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize); 3184 db_printf(" mnt_activevnodelistsize = %d\n", 3185 mp->mnt_activevnodelistsize); 3186 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount); 3187 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen); 3188 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max); 3189 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed); 3190 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes); 3191 db_printf(" mnt_secondary_accwrites = %d\n", 3192 mp->mnt_secondary_accwrites); 3193 db_printf(" mnt_gjprovider = %s\n", 3194 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL"); 3195 3196 db_printf("\n\nList of active vnodes\n"); 3197 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) { 3198 if (vp->v_type != VMARKER) { 3199 vn_printf(vp, "vnode "); 3200 if (db_pager_quit) 3201 break; 3202 } 3203 } 3204 db_printf("\n\nList of inactive vnodes\n"); 3205 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3206 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) { 3207 vn_printf(vp, "vnode "); 3208 if (db_pager_quit) 3209 break; 3210 } 3211 } 3212 } 3213 #endif /* DDB */ 3214 3215 /* 3216 * Fill in a struct xvfsconf based on a struct vfsconf. 3217 */ 3218 static int 3219 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp) 3220 { 3221 struct xvfsconf xvfsp; 3222 3223 bzero(&xvfsp, sizeof(xvfsp)); 3224 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 3225 xvfsp.vfc_typenum = vfsp->vfc_typenum; 3226 xvfsp.vfc_refcount = vfsp->vfc_refcount; 3227 xvfsp.vfc_flags = vfsp->vfc_flags; 3228 /* 3229 * These are unused in userland, we keep them 3230 * to not break binary compatibility. 3231 */ 3232 xvfsp.vfc_vfsops = NULL; 3233 xvfsp.vfc_next = NULL; 3234 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3235 } 3236 3237 #ifdef COMPAT_FREEBSD32 3238 struct xvfsconf32 { 3239 uint32_t vfc_vfsops; 3240 char vfc_name[MFSNAMELEN]; 3241 int32_t vfc_typenum; 3242 int32_t vfc_refcount; 3243 int32_t vfc_flags; 3244 uint32_t vfc_next; 3245 }; 3246 3247 static int 3248 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp) 3249 { 3250 struct xvfsconf32 xvfsp; 3251 3252 strcpy(xvfsp.vfc_name, vfsp->vfc_name); 3253 xvfsp.vfc_typenum = vfsp->vfc_typenum; 3254 xvfsp.vfc_refcount = vfsp->vfc_refcount; 3255 xvfsp.vfc_flags = vfsp->vfc_flags; 3256 xvfsp.vfc_vfsops = 0; 3257 xvfsp.vfc_next = 0; 3258 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 3259 } 3260 #endif 3261 3262 /* 3263 * Top level filesystem related information gathering. 3264 */ 3265 static int 3266 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 3267 { 3268 struct vfsconf *vfsp; 3269 int error; 3270 3271 error = 0; 3272 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3273 #ifdef COMPAT_FREEBSD32 3274 if (req->flags & SCTL_MASK32) 3275 error = vfsconf2x32(req, vfsp); 3276 else 3277 #endif 3278 error = vfsconf2x(req, vfsp); 3279 if (error) 3280 break; 3281 } 3282 return (error); 3283 } 3284 3285 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD, 3286 NULL, 0, sysctl_vfs_conflist, 3287 "S,xvfsconf", "List of all configured filesystems"); 3288 3289 #ifndef BURN_BRIDGES 3290 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 3291 3292 static int 3293 vfs_sysctl(SYSCTL_HANDLER_ARGS) 3294 { 3295 int *name = (int *)arg1 - 1; /* XXX */ 3296 u_int namelen = arg2 + 1; /* XXX */ 3297 struct vfsconf *vfsp; 3298 3299 log(LOG_WARNING, "userland calling deprecated sysctl, " 3300 "please rebuild world\n"); 3301 3302 #if 1 || defined(COMPAT_PRELITE2) 3303 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 3304 if (namelen == 1) 3305 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 3306 #endif 3307 3308 switch (name[1]) { 3309 case VFS_MAXTYPENUM: 3310 if (namelen != 2) 3311 return (ENOTDIR); 3312 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 3313 case VFS_CONF: 3314 if (namelen != 3) 3315 return (ENOTDIR); /* overloaded */ 3316 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 3317 if (vfsp->vfc_typenum == name[2]) 3318 break; 3319 if (vfsp == NULL) 3320 return (EOPNOTSUPP); 3321 #ifdef COMPAT_FREEBSD32 3322 if (req->flags & SCTL_MASK32) 3323 return (vfsconf2x32(req, vfsp)); 3324 else 3325 #endif 3326 return (vfsconf2x(req, vfsp)); 3327 } 3328 return (EOPNOTSUPP); 3329 } 3330 3331 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, 3332 vfs_sysctl, "Generic filesystem"); 3333 3334 #if 1 || defined(COMPAT_PRELITE2) 3335 3336 static int 3337 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 3338 { 3339 int error; 3340 struct vfsconf *vfsp; 3341 struct ovfsconf ovfs; 3342 3343 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 3344 bzero(&ovfs, sizeof(ovfs)); 3345 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 3346 strcpy(ovfs.vfc_name, vfsp->vfc_name); 3347 ovfs.vfc_index = vfsp->vfc_typenum; 3348 ovfs.vfc_refcount = vfsp->vfc_refcount; 3349 ovfs.vfc_flags = vfsp->vfc_flags; 3350 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 3351 if (error) 3352 return error; 3353 } 3354 return 0; 3355 } 3356 3357 #endif /* 1 || COMPAT_PRELITE2 */ 3358 #endif /* !BURN_BRIDGES */ 3359 3360 #define KINFO_VNODESLOP 10 3361 #ifdef notyet 3362 /* 3363 * Dump vnode list (via sysctl). 3364 */ 3365 /* ARGSUSED */ 3366 static int 3367 sysctl_vnode(SYSCTL_HANDLER_ARGS) 3368 { 3369 struct xvnode *xvn; 3370 struct mount *mp; 3371 struct vnode *vp; 3372 int error, len, n; 3373 3374 /* 3375 * Stale numvnodes access is not fatal here. 3376 */ 3377 req->lock = 0; 3378 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 3379 if (!req->oldptr) 3380 /* Make an estimate */ 3381 return (SYSCTL_OUT(req, 0, len)); 3382 3383 error = sysctl_wire_old_buffer(req, 0); 3384 if (error != 0) 3385 return (error); 3386 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 3387 n = 0; 3388 mtx_lock(&mountlist_mtx); 3389 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3390 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 3391 continue; 3392 MNT_ILOCK(mp); 3393 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3394 if (n == len) 3395 break; 3396 vref(vp); 3397 xvn[n].xv_size = sizeof *xvn; 3398 xvn[n].xv_vnode = vp; 3399 xvn[n].xv_id = 0; /* XXX compat */ 3400 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3401 XV_COPY(usecount); 3402 XV_COPY(writecount); 3403 XV_COPY(holdcnt); 3404 XV_COPY(mount); 3405 XV_COPY(numoutput); 3406 XV_COPY(type); 3407 #undef XV_COPY 3408 xvn[n].xv_flag = vp->v_vflag; 3409 3410 switch (vp->v_type) { 3411 case VREG: 3412 case VDIR: 3413 case VLNK: 3414 break; 3415 case VBLK: 3416 case VCHR: 3417 if (vp->v_rdev == NULL) { 3418 vrele(vp); 3419 continue; 3420 } 3421 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3422 break; 3423 case VSOCK: 3424 xvn[n].xv_socket = vp->v_socket; 3425 break; 3426 case VFIFO: 3427 xvn[n].xv_fifo = vp->v_fifoinfo; 3428 break; 3429 case VNON: 3430 case VBAD: 3431 default: 3432 /* shouldn't happen? */ 3433 vrele(vp); 3434 continue; 3435 } 3436 vrele(vp); 3437 ++n; 3438 } 3439 MNT_IUNLOCK(mp); 3440 mtx_lock(&mountlist_mtx); 3441 vfs_unbusy(mp); 3442 if (n == len) 3443 break; 3444 } 3445 mtx_unlock(&mountlist_mtx); 3446 3447 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 3448 free(xvn, M_TEMP); 3449 return (error); 3450 } 3451 3452 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 3453 0, 0, sysctl_vnode, "S,xvnode", ""); 3454 #endif 3455 3456 /* 3457 * Unmount all filesystems. The list is traversed in reverse order 3458 * of mounting to avoid dependencies. 3459 */ 3460 void 3461 vfs_unmountall(void) 3462 { 3463 struct mount *mp; 3464 struct thread *td; 3465 int error; 3466 3467 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__); 3468 td = curthread; 3469 3470 /* 3471 * Since this only runs when rebooting, it is not interlocked. 3472 */ 3473 while(!TAILQ_EMPTY(&mountlist)) { 3474 mp = TAILQ_LAST(&mountlist, mntlist); 3475 error = dounmount(mp, MNT_FORCE, td); 3476 if (error) { 3477 TAILQ_REMOVE(&mountlist, mp, mnt_list); 3478 /* 3479 * XXX: Due to the way in which we mount the root 3480 * file system off of devfs, devfs will generate a 3481 * "busy" warning when we try to unmount it before 3482 * the root. Don't print a warning as a result in 3483 * order to avoid false positive errors that may 3484 * cause needless upset. 3485 */ 3486 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) { 3487 printf("unmount of %s failed (", 3488 mp->mnt_stat.f_mntonname); 3489 if (error == EBUSY) 3490 printf("BUSY)\n"); 3491 else 3492 printf("%d)\n", error); 3493 } 3494 } else { 3495 /* The unmount has removed mp from the mountlist */ 3496 } 3497 } 3498 } 3499 3500 /* 3501 * perform msync on all vnodes under a mount point 3502 * the mount point must be locked. 3503 */ 3504 void 3505 vfs_msync(struct mount *mp, int flags) 3506 { 3507 struct vnode *vp, *mvp; 3508 struct vm_object *obj; 3509 3510 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 3511 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) { 3512 obj = vp->v_object; 3513 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 && 3514 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) { 3515 if (!vget(vp, 3516 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 3517 curthread)) { 3518 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 3519 vput(vp); 3520 continue; 3521 } 3522 3523 obj = vp->v_object; 3524 if (obj != NULL) { 3525 VM_OBJECT_LOCK(obj); 3526 vm_object_page_clean(obj, 0, 0, 3527 flags == MNT_WAIT ? 3528 OBJPC_SYNC : OBJPC_NOSYNC); 3529 VM_OBJECT_UNLOCK(obj); 3530 } 3531 vput(vp); 3532 } 3533 } else 3534 VI_UNLOCK(vp); 3535 } 3536 } 3537 3538 static void 3539 destroy_vpollinfo(struct vpollinfo *vi) 3540 { 3541 seldrain(&vi->vpi_selinfo); 3542 knlist_destroy(&vi->vpi_selinfo.si_note); 3543 mtx_destroy(&vi->vpi_lock); 3544 uma_zfree(vnodepoll_zone, vi); 3545 } 3546 3547 /* 3548 * Initalize per-vnode helper structure to hold poll-related state. 3549 */ 3550 void 3551 v_addpollinfo(struct vnode *vp) 3552 { 3553 struct vpollinfo *vi; 3554 3555 if (vp->v_pollinfo != NULL) 3556 return; 3557 vi = uma_zalloc(vnodepoll_zone, M_WAITOK); 3558 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 3559 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock, 3560 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked); 3561 VI_LOCK(vp); 3562 if (vp->v_pollinfo != NULL) { 3563 VI_UNLOCK(vp); 3564 destroy_vpollinfo(vi); 3565 return; 3566 } 3567 vp->v_pollinfo = vi; 3568 VI_UNLOCK(vp); 3569 } 3570 3571 /* 3572 * Record a process's interest in events which might happen to 3573 * a vnode. Because poll uses the historic select-style interface 3574 * internally, this routine serves as both the ``check for any 3575 * pending events'' and the ``record my interest in future events'' 3576 * functions. (These are done together, while the lock is held, 3577 * to avoid race conditions.) 3578 */ 3579 int 3580 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 3581 { 3582 3583 v_addpollinfo(vp); 3584 mtx_lock(&vp->v_pollinfo->vpi_lock); 3585 if (vp->v_pollinfo->vpi_revents & events) { 3586 /* 3587 * This leaves events we are not interested 3588 * in available for the other process which 3589 * which presumably had requested them 3590 * (otherwise they would never have been 3591 * recorded). 3592 */ 3593 events &= vp->v_pollinfo->vpi_revents; 3594 vp->v_pollinfo->vpi_revents &= ~events; 3595 3596 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3597 return (events); 3598 } 3599 vp->v_pollinfo->vpi_events |= events; 3600 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3601 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3602 return (0); 3603 } 3604 3605 /* 3606 * Routine to create and manage a filesystem syncer vnode. 3607 */ 3608 #define sync_close ((int (*)(struct vop_close_args *))nullop) 3609 static int sync_fsync(struct vop_fsync_args *); 3610 static int sync_inactive(struct vop_inactive_args *); 3611 static int sync_reclaim(struct vop_reclaim_args *); 3612 3613 static struct vop_vector sync_vnodeops = { 3614 .vop_bypass = VOP_EOPNOTSUPP, 3615 .vop_close = sync_close, /* close */ 3616 .vop_fsync = sync_fsync, /* fsync */ 3617 .vop_inactive = sync_inactive, /* inactive */ 3618 .vop_reclaim = sync_reclaim, /* reclaim */ 3619 .vop_lock1 = vop_stdlock, /* lock */ 3620 .vop_unlock = vop_stdunlock, /* unlock */ 3621 .vop_islocked = vop_stdislocked, /* islocked */ 3622 }; 3623 3624 /* 3625 * Create a new filesystem syncer vnode for the specified mount point. 3626 */ 3627 void 3628 vfs_allocate_syncvnode(struct mount *mp) 3629 { 3630 struct vnode *vp; 3631 struct bufobj *bo; 3632 static long start, incr, next; 3633 int error; 3634 3635 /* Allocate a new vnode */ 3636 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp); 3637 if (error != 0) 3638 panic("vfs_allocate_syncvnode: getnewvnode() failed"); 3639 vp->v_type = VNON; 3640 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3641 vp->v_vflag |= VV_FORCEINSMQ; 3642 error = insmntque(vp, mp); 3643 if (error != 0) 3644 panic("vfs_allocate_syncvnode: insmntque() failed"); 3645 vp->v_vflag &= ~VV_FORCEINSMQ; 3646 VOP_UNLOCK(vp, 0); 3647 /* 3648 * Place the vnode onto the syncer worklist. We attempt to 3649 * scatter them about on the list so that they will go off 3650 * at evenly distributed times even if all the filesystems 3651 * are mounted at once. 3652 */ 3653 next += incr; 3654 if (next == 0 || next > syncer_maxdelay) { 3655 start /= 2; 3656 incr /= 2; 3657 if (start == 0) { 3658 start = syncer_maxdelay / 2; 3659 incr = syncer_maxdelay; 3660 } 3661 next = start; 3662 } 3663 bo = &vp->v_bufobj; 3664 BO_LOCK(bo); 3665 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0); 3666 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 3667 mtx_lock(&sync_mtx); 3668 sync_vnode_count++; 3669 if (mp->mnt_syncer == NULL) { 3670 mp->mnt_syncer = vp; 3671 vp = NULL; 3672 } 3673 mtx_unlock(&sync_mtx); 3674 BO_UNLOCK(bo); 3675 if (vp != NULL) { 3676 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3677 vgone(vp); 3678 vput(vp); 3679 } 3680 } 3681 3682 void 3683 vfs_deallocate_syncvnode(struct mount *mp) 3684 { 3685 struct vnode *vp; 3686 3687 mtx_lock(&sync_mtx); 3688 vp = mp->mnt_syncer; 3689 if (vp != NULL) 3690 mp->mnt_syncer = NULL; 3691 mtx_unlock(&sync_mtx); 3692 if (vp != NULL) 3693 vrele(vp); 3694 } 3695 3696 /* 3697 * Do a lazy sync of the filesystem. 3698 */ 3699 static int 3700 sync_fsync(struct vop_fsync_args *ap) 3701 { 3702 struct vnode *syncvp = ap->a_vp; 3703 struct mount *mp = syncvp->v_mount; 3704 int error, save; 3705 struct bufobj *bo; 3706 3707 /* 3708 * We only need to do something if this is a lazy evaluation. 3709 */ 3710 if (ap->a_waitfor != MNT_LAZY) 3711 return (0); 3712 3713 /* 3714 * Move ourselves to the back of the sync list. 3715 */ 3716 bo = &syncvp->v_bufobj; 3717 BO_LOCK(bo); 3718 vn_syncer_add_to_worklist(bo, syncdelay); 3719 BO_UNLOCK(bo); 3720 3721 /* 3722 * Walk the list of vnodes pushing all that are dirty and 3723 * not already on the sync list. 3724 */ 3725 mtx_lock(&mountlist_mtx); 3726 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) { 3727 mtx_unlock(&mountlist_mtx); 3728 return (0); 3729 } 3730 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3731 vfs_unbusy(mp); 3732 return (0); 3733 } 3734 save = curthread_pflags_set(TDP_SYNCIO); 3735 vfs_msync(mp, MNT_NOWAIT); 3736 error = VFS_SYNC(mp, MNT_LAZY); 3737 curthread_pflags_restore(save); 3738 vn_finished_write(mp); 3739 vfs_unbusy(mp); 3740 return (error); 3741 } 3742 3743 /* 3744 * The syncer vnode is no referenced. 3745 */ 3746 static int 3747 sync_inactive(struct vop_inactive_args *ap) 3748 { 3749 3750 vgone(ap->a_vp); 3751 return (0); 3752 } 3753 3754 /* 3755 * The syncer vnode is no longer needed and is being decommissioned. 3756 * 3757 * Modifications to the worklist must be protected by sync_mtx. 3758 */ 3759 static int 3760 sync_reclaim(struct vop_reclaim_args *ap) 3761 { 3762 struct vnode *vp = ap->a_vp; 3763 struct bufobj *bo; 3764 3765 bo = &vp->v_bufobj; 3766 BO_LOCK(bo); 3767 mtx_lock(&sync_mtx); 3768 if (vp->v_mount->mnt_syncer == vp) 3769 vp->v_mount->mnt_syncer = NULL; 3770 if (bo->bo_flag & BO_ONWORKLST) { 3771 LIST_REMOVE(bo, bo_synclist); 3772 syncer_worklist_len--; 3773 sync_vnode_count--; 3774 bo->bo_flag &= ~BO_ONWORKLST; 3775 } 3776 mtx_unlock(&sync_mtx); 3777 BO_UNLOCK(bo); 3778 3779 return (0); 3780 } 3781 3782 /* 3783 * Check if vnode represents a disk device 3784 */ 3785 int 3786 vn_isdisk(struct vnode *vp, int *errp) 3787 { 3788 int error; 3789 3790 error = 0; 3791 dev_lock(); 3792 if (vp->v_type != VCHR) 3793 error = ENOTBLK; 3794 else if (vp->v_rdev == NULL) 3795 error = ENXIO; 3796 else if (vp->v_rdev->si_devsw == NULL) 3797 error = ENXIO; 3798 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 3799 error = ENOTBLK; 3800 dev_unlock(); 3801 if (errp != NULL) 3802 *errp = error; 3803 return (error == 0); 3804 } 3805 3806 /* 3807 * Common filesystem object access control check routine. Accepts a 3808 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3809 * and optional call-by-reference privused argument allowing vaccess() 3810 * to indicate to the caller whether privilege was used to satisfy the 3811 * request (obsoleted). Returns 0 on success, or an errno on failure. 3812 */ 3813 int 3814 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 3815 accmode_t accmode, struct ucred *cred, int *privused) 3816 { 3817 accmode_t dac_granted; 3818 accmode_t priv_granted; 3819 3820 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 3821 ("invalid bit in accmode")); 3822 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 3823 ("VAPPEND without VWRITE")); 3824 3825 /* 3826 * Look for a normal, non-privileged way to access the file/directory 3827 * as requested. If it exists, go with that. 3828 */ 3829 3830 if (privused != NULL) 3831 *privused = 0; 3832 3833 dac_granted = 0; 3834 3835 /* Check the owner. */ 3836 if (cred->cr_uid == file_uid) { 3837 dac_granted |= VADMIN; 3838 if (file_mode & S_IXUSR) 3839 dac_granted |= VEXEC; 3840 if (file_mode & S_IRUSR) 3841 dac_granted |= VREAD; 3842 if (file_mode & S_IWUSR) 3843 dac_granted |= (VWRITE | VAPPEND); 3844 3845 if ((accmode & dac_granted) == accmode) 3846 return (0); 3847 3848 goto privcheck; 3849 } 3850 3851 /* Otherwise, check the groups (first match) */ 3852 if (groupmember(file_gid, cred)) { 3853 if (file_mode & S_IXGRP) 3854 dac_granted |= VEXEC; 3855 if (file_mode & S_IRGRP) 3856 dac_granted |= VREAD; 3857 if (file_mode & S_IWGRP) 3858 dac_granted |= (VWRITE | VAPPEND); 3859 3860 if ((accmode & dac_granted) == accmode) 3861 return (0); 3862 3863 goto privcheck; 3864 } 3865 3866 /* Otherwise, check everyone else. */ 3867 if (file_mode & S_IXOTH) 3868 dac_granted |= VEXEC; 3869 if (file_mode & S_IROTH) 3870 dac_granted |= VREAD; 3871 if (file_mode & S_IWOTH) 3872 dac_granted |= (VWRITE | VAPPEND); 3873 if ((accmode & dac_granted) == accmode) 3874 return (0); 3875 3876 privcheck: 3877 /* 3878 * Build a privilege mask to determine if the set of privileges 3879 * satisfies the requirements when combined with the granted mask 3880 * from above. For each privilege, if the privilege is required, 3881 * bitwise or the request type onto the priv_granted mask. 3882 */ 3883 priv_granted = 0; 3884 3885 if (type == VDIR) { 3886 /* 3887 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 3888 * requests, instead of PRIV_VFS_EXEC. 3889 */ 3890 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3891 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0)) 3892 priv_granted |= VEXEC; 3893 } else { 3894 /* 3895 * Ensure that at least one execute bit is on. Otherwise, 3896 * a privileged user will always succeed, and we don't want 3897 * this to happen unless the file really is executable. 3898 */ 3899 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3900 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 3901 !priv_check_cred(cred, PRIV_VFS_EXEC, 0)) 3902 priv_granted |= VEXEC; 3903 } 3904 3905 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) && 3906 !priv_check_cred(cred, PRIV_VFS_READ, 0)) 3907 priv_granted |= VREAD; 3908 3909 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3910 !priv_check_cred(cred, PRIV_VFS_WRITE, 0)) 3911 priv_granted |= (VWRITE | VAPPEND); 3912 3913 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3914 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0)) 3915 priv_granted |= VADMIN; 3916 3917 if ((accmode & (priv_granted | dac_granted)) == accmode) { 3918 /* XXX audit: privilege used */ 3919 if (privused != NULL) 3920 *privused = 1; 3921 return (0); 3922 } 3923 3924 return ((accmode & VADMIN) ? EPERM : EACCES); 3925 } 3926 3927 /* 3928 * Credential check based on process requesting service, and per-attribute 3929 * permissions. 3930 */ 3931 int 3932 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 3933 struct thread *td, accmode_t accmode) 3934 { 3935 3936 /* 3937 * Kernel-invoked always succeeds. 3938 */ 3939 if (cred == NOCRED) 3940 return (0); 3941 3942 /* 3943 * Do not allow privileged processes in jail to directly manipulate 3944 * system attributes. 3945 */ 3946 switch (attrnamespace) { 3947 case EXTATTR_NAMESPACE_SYSTEM: 3948 /* Potentially should be: return (EPERM); */ 3949 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0)); 3950 case EXTATTR_NAMESPACE_USER: 3951 return (VOP_ACCESS(vp, accmode, cred, td)); 3952 default: 3953 return (EPERM); 3954 } 3955 } 3956 3957 #ifdef DEBUG_VFS_LOCKS 3958 /* 3959 * This only exists to supress warnings from unlocked specfs accesses. It is 3960 * no longer ok to have an unlocked VFS. 3961 */ 3962 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \ 3963 (vp)->v_type == VCHR || (vp)->v_type == VBAD) 3964 3965 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 3966 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, 3967 "Drop into debugger on lock violation"); 3968 3969 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 3970 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 3971 0, "Check for interlock across VOPs"); 3972 3973 int vfs_badlock_print = 1; /* Print lock violations. */ 3974 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 3975 0, "Print lock violations"); 3976 3977 #ifdef KDB 3978 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 3979 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, 3980 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations"); 3981 #endif 3982 3983 static void 3984 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 3985 { 3986 3987 #ifdef KDB 3988 if (vfs_badlock_backtrace) 3989 kdb_backtrace(); 3990 #endif 3991 if (vfs_badlock_print) 3992 printf("%s: %p %s\n", str, (void *)vp, msg); 3993 if (vfs_badlock_ddb) 3994 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 3995 } 3996 3997 void 3998 assert_vi_locked(struct vnode *vp, const char *str) 3999 { 4000 4001 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 4002 vfs_badlock("interlock is not locked but should be", str, vp); 4003 } 4004 4005 void 4006 assert_vi_unlocked(struct vnode *vp, const char *str) 4007 { 4008 4009 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 4010 vfs_badlock("interlock is locked but should not be", str, vp); 4011 } 4012 4013 void 4014 assert_vop_locked(struct vnode *vp, const char *str) 4015 { 4016 4017 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0) 4018 vfs_badlock("is not locked but should be", str, vp); 4019 } 4020 4021 void 4022 assert_vop_unlocked(struct vnode *vp, const char *str) 4023 { 4024 4025 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) 4026 vfs_badlock("is locked but should not be", str, vp); 4027 } 4028 4029 void 4030 assert_vop_elocked(struct vnode *vp, const char *str) 4031 { 4032 4033 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 4034 vfs_badlock("is not exclusive locked but should be", str, vp); 4035 } 4036 4037 #if 0 4038 void 4039 assert_vop_elocked_other(struct vnode *vp, const char *str) 4040 { 4041 4042 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER) 4043 vfs_badlock("is not exclusive locked by another thread", 4044 str, vp); 4045 } 4046 4047 void 4048 assert_vop_slocked(struct vnode *vp, const char *str) 4049 { 4050 4051 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED) 4052 vfs_badlock("is not locked shared but should be", str, vp); 4053 } 4054 #endif /* 0 */ 4055 #endif /* DEBUG_VFS_LOCKS */ 4056 4057 void 4058 vop_rename_fail(struct vop_rename_args *ap) 4059 { 4060 4061 if (ap->a_tvp != NULL) 4062 vput(ap->a_tvp); 4063 if (ap->a_tdvp == ap->a_tvp) 4064 vrele(ap->a_tdvp); 4065 else 4066 vput(ap->a_tdvp); 4067 vrele(ap->a_fdvp); 4068 vrele(ap->a_fvp); 4069 } 4070 4071 void 4072 vop_rename_pre(void *ap) 4073 { 4074 struct vop_rename_args *a = ap; 4075 4076 #ifdef DEBUG_VFS_LOCKS 4077 if (a->a_tvp) 4078 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 4079 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 4080 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 4081 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 4082 4083 /* Check the source (from). */ 4084 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock && 4085 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock)) 4086 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 4087 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock) 4088 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 4089 4090 /* Check the target. */ 4091 if (a->a_tvp) 4092 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 4093 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 4094 #endif 4095 if (a->a_tdvp != a->a_fdvp) 4096 vhold(a->a_fdvp); 4097 if (a->a_tvp != a->a_fvp) 4098 vhold(a->a_fvp); 4099 vhold(a->a_tdvp); 4100 if (a->a_tvp) 4101 vhold(a->a_tvp); 4102 } 4103 4104 void 4105 vop_strategy_pre(void *ap) 4106 { 4107 #ifdef DEBUG_VFS_LOCKS 4108 struct vop_strategy_args *a; 4109 struct buf *bp; 4110 4111 a = ap; 4112 bp = a->a_bp; 4113 4114 /* 4115 * Cluster ops lock their component buffers but not the IO container. 4116 */ 4117 if ((bp->b_flags & B_CLUSTER) != 0) 4118 return; 4119 4120 if (panicstr == NULL && !BUF_ISLOCKED(bp)) { 4121 if (vfs_badlock_print) 4122 printf( 4123 "VOP_STRATEGY: bp is not locked but should be\n"); 4124 if (vfs_badlock_ddb) 4125 kdb_enter(KDB_WHY_VFSLOCK, "lock violation"); 4126 } 4127 #endif 4128 } 4129 4130 void 4131 vop_lookup_pre(void *ap) 4132 { 4133 #ifdef DEBUG_VFS_LOCKS 4134 struct vop_lookup_args *a; 4135 struct vnode *dvp; 4136 4137 a = ap; 4138 dvp = a->a_dvp; 4139 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 4140 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 4141 #endif 4142 } 4143 4144 void 4145 vop_lookup_post(void *ap, int rc) 4146 { 4147 #ifdef DEBUG_VFS_LOCKS 4148 struct vop_lookup_args *a; 4149 struct vnode *dvp; 4150 struct vnode *vp; 4151 4152 a = ap; 4153 dvp = a->a_dvp; 4154 vp = *(a->a_vpp); 4155 4156 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 4157 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 4158 4159 if (!rc) 4160 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)"); 4161 #endif 4162 } 4163 4164 void 4165 vop_lock_pre(void *ap) 4166 { 4167 #ifdef DEBUG_VFS_LOCKS 4168 struct vop_lock1_args *a = ap; 4169 4170 if ((a->a_flags & LK_INTERLOCK) == 0) 4171 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4172 else 4173 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 4174 #endif 4175 } 4176 4177 void 4178 vop_lock_post(void *ap, int rc) 4179 { 4180 #ifdef DEBUG_VFS_LOCKS 4181 struct vop_lock1_args *a = ap; 4182 4183 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 4184 if (rc == 0) 4185 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 4186 #endif 4187 } 4188 4189 void 4190 vop_unlock_pre(void *ap) 4191 { 4192 #ifdef DEBUG_VFS_LOCKS 4193 struct vop_unlock_args *a = ap; 4194 4195 if (a->a_flags & LK_INTERLOCK) 4196 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 4197 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 4198 #endif 4199 } 4200 4201 void 4202 vop_unlock_post(void *ap, int rc) 4203 { 4204 #ifdef DEBUG_VFS_LOCKS 4205 struct vop_unlock_args *a = ap; 4206 4207 if (a->a_flags & LK_INTERLOCK) 4208 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 4209 #endif 4210 } 4211 4212 void 4213 vop_create_post(void *ap, int rc) 4214 { 4215 struct vop_create_args *a = ap; 4216 4217 if (!rc) 4218 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4219 } 4220 4221 void 4222 vop_deleteextattr_post(void *ap, int rc) 4223 { 4224 struct vop_deleteextattr_args *a = ap; 4225 4226 if (!rc) 4227 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4228 } 4229 4230 void 4231 vop_link_post(void *ap, int rc) 4232 { 4233 struct vop_link_args *a = ap; 4234 4235 if (!rc) { 4236 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 4237 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 4238 } 4239 } 4240 4241 void 4242 vop_mkdir_post(void *ap, int rc) 4243 { 4244 struct vop_mkdir_args *a = ap; 4245 4246 if (!rc) 4247 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 4248 } 4249 4250 void 4251 vop_mknod_post(void *ap, int rc) 4252 { 4253 struct vop_mknod_args *a = ap; 4254 4255 if (!rc) 4256 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4257 } 4258 4259 void 4260 vop_remove_post(void *ap, int rc) 4261 { 4262 struct vop_remove_args *a = ap; 4263 4264 if (!rc) { 4265 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4266 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 4267 } 4268 } 4269 4270 void 4271 vop_rename_post(void *ap, int rc) 4272 { 4273 struct vop_rename_args *a = ap; 4274 4275 if (!rc) { 4276 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE); 4277 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE); 4278 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 4279 if (a->a_tvp) 4280 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 4281 } 4282 if (a->a_tdvp != a->a_fdvp) 4283 vdrop(a->a_fdvp); 4284 if (a->a_tvp != a->a_fvp) 4285 vdrop(a->a_fvp); 4286 vdrop(a->a_tdvp); 4287 if (a->a_tvp) 4288 vdrop(a->a_tvp); 4289 } 4290 4291 void 4292 vop_rmdir_post(void *ap, int rc) 4293 { 4294 struct vop_rmdir_args *a = ap; 4295 4296 if (!rc) { 4297 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 4298 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 4299 } 4300 } 4301 4302 void 4303 vop_setattr_post(void *ap, int rc) 4304 { 4305 struct vop_setattr_args *a = ap; 4306 4307 if (!rc) 4308 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4309 } 4310 4311 void 4312 vop_setextattr_post(void *ap, int rc) 4313 { 4314 struct vop_setextattr_args *a = ap; 4315 4316 if (!rc) 4317 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 4318 } 4319 4320 void 4321 vop_symlink_post(void *ap, int rc) 4322 { 4323 struct vop_symlink_args *a = ap; 4324 4325 if (!rc) 4326 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 4327 } 4328 4329 static struct knlist fs_knlist; 4330 4331 static void 4332 vfs_event_init(void *arg) 4333 { 4334 knlist_init_mtx(&fs_knlist, NULL); 4335 } 4336 /* XXX - correct order? */ 4337 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 4338 4339 void 4340 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused) 4341 { 4342 4343 KNOTE_UNLOCKED(&fs_knlist, event); 4344 } 4345 4346 static int filt_fsattach(struct knote *kn); 4347 static void filt_fsdetach(struct knote *kn); 4348 static int filt_fsevent(struct knote *kn, long hint); 4349 4350 struct filterops fs_filtops = { 4351 .f_isfd = 0, 4352 .f_attach = filt_fsattach, 4353 .f_detach = filt_fsdetach, 4354 .f_event = filt_fsevent 4355 }; 4356 4357 static int 4358 filt_fsattach(struct knote *kn) 4359 { 4360 4361 kn->kn_flags |= EV_CLEAR; 4362 knlist_add(&fs_knlist, kn, 0); 4363 return (0); 4364 } 4365 4366 static void 4367 filt_fsdetach(struct knote *kn) 4368 { 4369 4370 knlist_remove(&fs_knlist, kn, 0); 4371 } 4372 4373 static int 4374 filt_fsevent(struct knote *kn, long hint) 4375 { 4376 4377 kn->kn_fflags |= hint; 4378 return (kn->kn_fflags != 0); 4379 } 4380 4381 static int 4382 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 4383 { 4384 struct vfsidctl vc; 4385 int error; 4386 struct mount *mp; 4387 4388 error = SYSCTL_IN(req, &vc, sizeof(vc)); 4389 if (error) 4390 return (error); 4391 if (vc.vc_vers != VFS_CTL_VERS1) 4392 return (EINVAL); 4393 mp = vfs_getvfs(&vc.vc_fsid); 4394 if (mp == NULL) 4395 return (ENOENT); 4396 /* ensure that a specific sysctl goes to the right filesystem. */ 4397 if (strcmp(vc.vc_fstypename, "*") != 0 && 4398 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 4399 vfs_rel(mp); 4400 return (EINVAL); 4401 } 4402 VCTLTOREQ(&vc, req); 4403 error = VFS_SYSCTL(mp, vc.vc_op, req); 4404 vfs_rel(mp); 4405 return (error); 4406 } 4407 4408 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR, 4409 NULL, 0, sysctl_vfs_ctl, "", 4410 "Sysctl by fsid"); 4411 4412 /* 4413 * Function to initialize a va_filerev field sensibly. 4414 * XXX: Wouldn't a random number make a lot more sense ?? 4415 */ 4416 u_quad_t 4417 init_va_filerev(void) 4418 { 4419 struct bintime bt; 4420 4421 getbinuptime(&bt); 4422 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 4423 } 4424 4425 static int filt_vfsread(struct knote *kn, long hint); 4426 static int filt_vfswrite(struct knote *kn, long hint); 4427 static int filt_vfsvnode(struct knote *kn, long hint); 4428 static void filt_vfsdetach(struct knote *kn); 4429 static struct filterops vfsread_filtops = { 4430 .f_isfd = 1, 4431 .f_detach = filt_vfsdetach, 4432 .f_event = filt_vfsread 4433 }; 4434 static struct filterops vfswrite_filtops = { 4435 .f_isfd = 1, 4436 .f_detach = filt_vfsdetach, 4437 .f_event = filt_vfswrite 4438 }; 4439 static struct filterops vfsvnode_filtops = { 4440 .f_isfd = 1, 4441 .f_detach = filt_vfsdetach, 4442 .f_event = filt_vfsvnode 4443 }; 4444 4445 static void 4446 vfs_knllock(void *arg) 4447 { 4448 struct vnode *vp = arg; 4449 4450 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4451 } 4452 4453 static void 4454 vfs_knlunlock(void *arg) 4455 { 4456 struct vnode *vp = arg; 4457 4458 VOP_UNLOCK(vp, 0); 4459 } 4460 4461 static void 4462 vfs_knl_assert_locked(void *arg) 4463 { 4464 #ifdef DEBUG_VFS_LOCKS 4465 struct vnode *vp = arg; 4466 4467 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked"); 4468 #endif 4469 } 4470 4471 static void 4472 vfs_knl_assert_unlocked(void *arg) 4473 { 4474 #ifdef DEBUG_VFS_LOCKS 4475 struct vnode *vp = arg; 4476 4477 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked"); 4478 #endif 4479 } 4480 4481 int 4482 vfs_kqfilter(struct vop_kqfilter_args *ap) 4483 { 4484 struct vnode *vp = ap->a_vp; 4485 struct knote *kn = ap->a_kn; 4486 struct knlist *knl; 4487 4488 switch (kn->kn_filter) { 4489 case EVFILT_READ: 4490 kn->kn_fop = &vfsread_filtops; 4491 break; 4492 case EVFILT_WRITE: 4493 kn->kn_fop = &vfswrite_filtops; 4494 break; 4495 case EVFILT_VNODE: 4496 kn->kn_fop = &vfsvnode_filtops; 4497 break; 4498 default: 4499 return (EINVAL); 4500 } 4501 4502 kn->kn_hook = (caddr_t)vp; 4503 4504 v_addpollinfo(vp); 4505 if (vp->v_pollinfo == NULL) 4506 return (ENOMEM); 4507 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 4508 knlist_add(knl, kn, 0); 4509 4510 return (0); 4511 } 4512 4513 /* 4514 * Detach knote from vnode 4515 */ 4516 static void 4517 filt_vfsdetach(struct knote *kn) 4518 { 4519 struct vnode *vp = (struct vnode *)kn->kn_hook; 4520 4521 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 4522 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 4523 } 4524 4525 /*ARGSUSED*/ 4526 static int 4527 filt_vfsread(struct knote *kn, long hint) 4528 { 4529 struct vnode *vp = (struct vnode *)kn->kn_hook; 4530 struct vattr va; 4531 int res; 4532 4533 /* 4534 * filesystem is gone, so set the EOF flag and schedule 4535 * the knote for deletion. 4536 */ 4537 if (hint == NOTE_REVOKE) { 4538 VI_LOCK(vp); 4539 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 4540 VI_UNLOCK(vp); 4541 return (1); 4542 } 4543 4544 if (VOP_GETATTR(vp, &va, curthread->td_ucred)) 4545 return (0); 4546 4547 VI_LOCK(vp); 4548 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 4549 res = (kn->kn_data != 0); 4550 VI_UNLOCK(vp); 4551 return (res); 4552 } 4553 4554 /*ARGSUSED*/ 4555 static int 4556 filt_vfswrite(struct knote *kn, long hint) 4557 { 4558 struct vnode *vp = (struct vnode *)kn->kn_hook; 4559 4560 VI_LOCK(vp); 4561 4562 /* 4563 * filesystem is gone, so set the EOF flag and schedule 4564 * the knote for deletion. 4565 */ 4566 if (hint == NOTE_REVOKE) 4567 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 4568 4569 kn->kn_data = 0; 4570 VI_UNLOCK(vp); 4571 return (1); 4572 } 4573 4574 static int 4575 filt_vfsvnode(struct knote *kn, long hint) 4576 { 4577 struct vnode *vp = (struct vnode *)kn->kn_hook; 4578 int res; 4579 4580 VI_LOCK(vp); 4581 if (kn->kn_sfflags & hint) 4582 kn->kn_fflags |= hint; 4583 if (hint == NOTE_REVOKE) { 4584 kn->kn_flags |= EV_EOF; 4585 VI_UNLOCK(vp); 4586 return (1); 4587 } 4588 res = (kn->kn_fflags != 0); 4589 VI_UNLOCK(vp); 4590 return (res); 4591 } 4592 4593 int 4594 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 4595 { 4596 int error; 4597 4598 if (dp->d_reclen > ap->a_uio->uio_resid) 4599 return (ENAMETOOLONG); 4600 error = uiomove(dp, dp->d_reclen, ap->a_uio); 4601 if (error) { 4602 if (ap->a_ncookies != NULL) { 4603 if (ap->a_cookies != NULL) 4604 free(ap->a_cookies, M_TEMP); 4605 ap->a_cookies = NULL; 4606 *ap->a_ncookies = 0; 4607 } 4608 return (error); 4609 } 4610 if (ap->a_ncookies == NULL) 4611 return (0); 4612 4613 KASSERT(ap->a_cookies, 4614 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 4615 4616 *ap->a_cookies = realloc(*ap->a_cookies, 4617 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 4618 (*ap->a_cookies)[*ap->a_ncookies] = off; 4619 return (0); 4620 } 4621 4622 /* 4623 * Mark for update the access time of the file if the filesystem 4624 * supports VOP_MARKATIME. This functionality is used by execve and 4625 * mmap, so we want to avoid the I/O implied by directly setting 4626 * va_atime for the sake of efficiency. 4627 */ 4628 void 4629 vfs_mark_atime(struct vnode *vp, struct ucred *cred) 4630 { 4631 struct mount *mp; 4632 4633 mp = vp->v_mount; 4634 VFS_ASSERT_GIANT(mp); 4635 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime"); 4636 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) 4637 (void)VOP_MARKATIME(vp); 4638 } 4639 4640 /* 4641 * The purpose of this routine is to remove granularity from accmode_t, 4642 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE, 4643 * VADMIN and VAPPEND. 4644 * 4645 * If it returns 0, the caller is supposed to continue with the usual 4646 * access checks using 'accmode' as modified by this routine. If it 4647 * returns nonzero value, the caller is supposed to return that value 4648 * as errno. 4649 * 4650 * Note that after this routine runs, accmode may be zero. 4651 */ 4652 int 4653 vfs_unixify_accmode(accmode_t *accmode) 4654 { 4655 /* 4656 * There is no way to specify explicit "deny" rule using 4657 * file mode or POSIX.1e ACLs. 4658 */ 4659 if (*accmode & VEXPLICIT_DENY) { 4660 *accmode = 0; 4661 return (0); 4662 } 4663 4664 /* 4665 * None of these can be translated into usual access bits. 4666 * Also, the common case for NFSv4 ACLs is to not contain 4667 * either of these bits. Caller should check for VWRITE 4668 * on the containing directory instead. 4669 */ 4670 if (*accmode & (VDELETE_CHILD | VDELETE)) 4671 return (EPERM); 4672 4673 if (*accmode & VADMIN_PERMS) { 4674 *accmode &= ~VADMIN_PERMS; 4675 *accmode |= VADMIN; 4676 } 4677 4678 /* 4679 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL 4680 * or VSYNCHRONIZE using file mode or POSIX.1e ACL. 4681 */ 4682 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE); 4683 4684 return (0); 4685 } 4686 4687 /* 4688 * These are helper functions for filesystems to traverse all 4689 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h. 4690 * 4691 * This interface replaces MNT_VNODE_FOREACH. 4692 */ 4693 4694 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker"); 4695 4696 struct vnode * 4697 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp) 4698 { 4699 struct vnode *vp; 4700 4701 if (should_yield()) 4702 kern_yield(PRI_UNCHANGED); 4703 MNT_ILOCK(mp); 4704 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 4705 vp = TAILQ_NEXT(*mvp, v_nmntvnodes); 4706 while (vp != NULL && (vp->v_type == VMARKER || 4707 (vp->v_iflag & VI_DOOMED) != 0)) 4708 vp = TAILQ_NEXT(vp, v_nmntvnodes); 4709 4710 /* Check if we are done */ 4711 if (vp == NULL) { 4712 __mnt_vnode_markerfree_all(mvp, mp); 4713 /* MNT_IUNLOCK(mp); -- done in above function */ 4714 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 4715 return (NULL); 4716 } 4717 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 4718 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 4719 VI_LOCK(vp); 4720 MNT_IUNLOCK(mp); 4721 return (vp); 4722 } 4723 4724 struct vnode * 4725 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp) 4726 { 4727 struct vnode *vp; 4728 4729 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 4730 MNT_ILOCK(mp); 4731 MNT_REF(mp); 4732 (*mvp)->v_type = VMARKER; 4733 4734 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 4735 while (vp != NULL && (vp->v_type == VMARKER || 4736 (vp->v_iflag & VI_DOOMED) != 0)) 4737 vp = TAILQ_NEXT(vp, v_nmntvnodes); 4738 4739 /* Check if we are done */ 4740 if (vp == NULL) { 4741 MNT_REL(mp); 4742 MNT_IUNLOCK(mp); 4743 free(*mvp, M_VNODE_MARKER); 4744 *mvp = NULL; 4745 return (NULL); 4746 } 4747 (*mvp)->v_mount = mp; 4748 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 4749 VI_LOCK(vp); 4750 MNT_IUNLOCK(mp); 4751 return (vp); 4752 } 4753 4754 4755 void 4756 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp) 4757 { 4758 4759 if (*mvp == NULL) { 4760 MNT_IUNLOCK(mp); 4761 return; 4762 } 4763 4764 mtx_assert(MNT_MTX(mp), MA_OWNED); 4765 4766 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 4767 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 4768 MNT_REL(mp); 4769 MNT_IUNLOCK(mp); 4770 free(*mvp, M_VNODE_MARKER); 4771 *mvp = NULL; 4772 } 4773 4774 /* 4775 * These are helper functions for filesystems to traverse their 4776 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h 4777 */ 4778 struct vnode * 4779 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp) 4780 { 4781 struct vnode *vp, *nvp; 4782 4783 if (should_yield()) 4784 kern_yield(PRI_UNCHANGED); 4785 MNT_ILOCK(mp); 4786 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 4787 vp = TAILQ_NEXT(*mvp, v_actfreelist); 4788 while (vp != NULL) { 4789 VI_LOCK(vp); 4790 if (vp->v_mount == mp && vp->v_type != VMARKER && 4791 (vp->v_iflag & VI_DOOMED) == 0) 4792 break; 4793 nvp = TAILQ_NEXT(vp, v_actfreelist); 4794 VI_UNLOCK(vp); 4795 vp = nvp; 4796 } 4797 4798 /* Check if we are done */ 4799 if (vp == NULL) { 4800 __mnt_vnode_markerfree_active(mvp, mp); 4801 /* MNT_IUNLOCK(mp); -- done in above function */ 4802 mtx_assert(MNT_MTX(mp), MA_NOTOWNED); 4803 return (NULL); 4804 } 4805 mtx_lock(&vnode_free_list_mtx); 4806 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 4807 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist); 4808 mtx_unlock(&vnode_free_list_mtx); 4809 MNT_IUNLOCK(mp); 4810 return (vp); 4811 } 4812 4813 struct vnode * 4814 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp) 4815 { 4816 struct vnode *vp, *nvp; 4817 4818 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO); 4819 MNT_ILOCK(mp); 4820 MNT_REF(mp); 4821 (*mvp)->v_type = VMARKER; 4822 4823 vp = TAILQ_NEXT(*mvp, v_actfreelist); 4824 while (vp != NULL) { 4825 VI_LOCK(vp); 4826 if (vp->v_mount == mp && vp->v_type != VMARKER && 4827 (vp->v_iflag & VI_DOOMED) == 0) 4828 break; 4829 nvp = TAILQ_NEXT(vp, v_actfreelist); 4830 VI_UNLOCK(vp); 4831 vp = nvp; 4832 } 4833 4834 /* Check if we are done */ 4835 if (vp == NULL) { 4836 MNT_REL(mp); 4837 MNT_IUNLOCK(mp); 4838 free(*mvp, M_VNODE_MARKER); 4839 *mvp = NULL; 4840 return (NULL); 4841 } 4842 (*mvp)->v_mount = mp; 4843 mtx_lock(&vnode_free_list_mtx); 4844 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist); 4845 mtx_unlock(&vnode_free_list_mtx); 4846 MNT_IUNLOCK(mp); 4847 return (vp); 4848 } 4849 4850 void 4851 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp) 4852 { 4853 4854 if (*mvp == NULL) { 4855 MNT_IUNLOCK(mp); 4856 return; 4857 } 4858 4859 mtx_assert(MNT_MTX(mp), MA_OWNED); 4860 4861 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 4862 mtx_lock(&vnode_free_list_mtx); 4863 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist); 4864 mtx_unlock(&vnode_free_list_mtx); 4865 MNT_REL(mp); 4866 MNT_IUNLOCK(mp); 4867 free(*mvp, M_VNODE_MARKER); 4868 *mvp = NULL; 4869 } 4870