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