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