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