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