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