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 * 3. 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 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $ 36 */ 37 38 /* 39 * External virtual filesystem routines 40 */ 41 #include "opt_ddb.h" 42 #include "opt_inet.h" 43 #include "opt_inet6.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/uio.h> 48 #include <sys/buf.h> 49 #include <sys/conf.h> 50 #include <sys/dirent.h> 51 #include <sys/endian.h> 52 #include <sys/eventhandler.h> 53 #include <sys/fcntl.h> 54 #include <sys/file.h> 55 #include <sys/kernel.h> 56 #include <sys/kthread.h> 57 #include <sys/malloc.h> 58 #include <sys/mbuf.h> 59 #include <sys/mount.h> 60 #include <sys/priv.h> 61 #include <sys/proc.h> 62 #include <sys/reboot.h> 63 #include <sys/socket.h> 64 #include <sys/stat.h> 65 #include <sys/sysctl.h> 66 #include <sys/syslog.h> 67 #include <sys/unistd.h> 68 #include <sys/vmmeter.h> 69 #include <sys/vnode.h> 70 71 #include <machine/limits.h> 72 73 #include <vm/vm.h> 74 #include <vm/vm_object.h> 75 #include <vm/vm_extern.h> 76 #include <vm/vm_kern.h> 77 #include <vm/pmap.h> 78 #include <vm/vm_map.h> 79 #include <vm/vm_page.h> 80 #include <vm/vm_pager.h> 81 #include <vm/vnode_pager.h> 82 #include <vm/vm_zone.h> 83 84 #include <sys/buf2.h> 85 #include <vm/vm_page2.h> 86 87 #include <netinet/in.h> 88 89 static MALLOC_DEFINE(M_NETCRED, "Export Host", "Export host address structure"); 90 91 __read_mostly int numvnodes; 92 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, 93 "Number of vnodes allocated"); 94 __read_mostly int verbose_reclaims; 95 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0, 96 "Output filename of reclaimed vnode(s)"); 97 98 __read_mostly enum vtype iftovt_tab[16] = { 99 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 100 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 101 }; 102 __read_mostly int vttoif_tab[9] = { 103 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 104 S_IFSOCK, S_IFIFO, S_IFMT, 105 }; 106 107 static int reassignbufcalls; 108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 109 0, "Number of times buffers have been reassigned to the proper list"); 110 111 __read_mostly static int check_buf_overlap = 2; /* invasive check */ 112 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap, 113 0, "Enable overlapping buffer checks"); 114 115 int nfs_mount_type = -1; 116 static struct lwkt_token spechash_token; 117 struct nfs_public nfs_pub; /* publicly exported FS */ 118 119 __read_mostly int maxvnodes; 120 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 121 &maxvnodes, 0, "Maximum number of vnodes"); 122 123 static struct radix_node_head *vfs_create_addrlist_af(int af, 124 struct netexport *nep); 125 static void vfs_free_addrlist (struct netexport *nep); 126 static int vfs_free_netcred (struct radix_node *rn, void *w); 127 static void vfs_free_addrlist_af (struct radix_node_head **prnh); 128 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep, 129 const struct export_args *argp); 130 static void vclean_vxlocked(struct vnode *vp, int flags); 131 132 __read_mostly int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 133 134 /* 135 * Red black tree functions 136 */ 137 static int rb_buf_compare(struct buf *b1, struct buf *b2); 138 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset); 139 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset); 140 141 static int 142 rb_buf_compare(struct buf *b1, struct buf *b2) 143 { 144 if (b1->b_loffset < b2->b_loffset) 145 return(-1); 146 if (b1->b_loffset > b2->b_loffset) 147 return(1); 148 return(0); 149 } 150 151 /* 152 * Initialize the vnode management data structures. 153 * 154 * Called from vfsinit() 155 */ 156 #define VNBREAKMEM1 (1L * 1024 * 1024 * 1024) 157 #define VNBREAKMEM2 (7L * 1024 * 1024 * 1024) 158 #define MINVNODES 2000 159 #define MAXVNODES 4000000 160 161 void 162 vfs_subr_init(void) 163 { 164 int factor1; /* Limit based on ram (x 2 above 1GB) */ 165 size_t freemem; 166 167 /* 168 * Size maxvnodes non-linearly to available memory. Don't bloat 169 * the count on low-memory systems. Scale up for systems with 170 * more than 1G and more than 8G of ram, but do so non-linearly 171 * because the value of a large maxvnodes count diminishes 172 * significantly beyond a certain point. 173 * 174 * The general minimum is maxproc * 8 (we want someone pushing 175 * up maxproc a lot to also get more vnodes). Usually maxproc 176 * does not affect this calculation. The KvaSize limitation also 177 * typically does not affect this calculation (it is just in case 178 * the kernel VM space is made much smaller than main memory, which 179 * should no longer happen on 64-bit systems). 180 * 181 * There isn't much of a point allowing maxvnodes to exceed a 182 * few million as modern filesystems cache pages in the 183 * underlying block device and not so much hanging off of VM 184 * objects. 185 * 186 * Also, VM objects, vnodes, and filesystem inode and other related 187 * structures have gotten a lot larger in recent years and the kernel 188 * memory use tends to scale with maxvnodes, so we don't want to bloat 189 * it too much. But neither do we want the max set too low because 190 * systems with large amounts of memory and cores are capable of 191 * doing a hell of a lot. 192 */ 193 factor1 = 80 * (sizeof(struct vm_object) + sizeof(struct vnode)); 194 195 freemem = (int64_t)vmstats.v_page_count * PAGE_SIZE; 196 197 maxvnodes = freemem / factor1; 198 if (freemem > VNBREAKMEM1) { 199 freemem -= VNBREAKMEM1; 200 if (freemem < VNBREAKMEM2) { 201 maxvnodes += freemem / factor1 / 2; 202 } else { 203 maxvnodes += VNBREAKMEM2 / factor1 / 2; 204 freemem -= VNBREAKMEM2; 205 maxvnodes += freemem / factor1 / 4; 206 } 207 } 208 maxvnodes = imax(maxvnodes, maxproc * 8); 209 maxvnodes = imin(maxvnodes, KvaSize / factor1); 210 maxvnodes = imin(maxvnodes, MAXVNODES); 211 maxvnodes = imax(maxvnodes, MINVNODES); 212 213 lwkt_token_init(&spechash_token, "spechash"); 214 } 215 216 /* 217 * Knob to control the precision of file timestamps: 218 * 219 * 0 = seconds only; nanoseconds zeroed. 220 * 1 = microseconds accurate to tick precision 221 * 2 = microseconds accurate to tick precision (default, hz >= 100) 222 * 3 = nanoseconds accurate to tick precision 223 * 4 = microseconds, maximum precision (default, hz < 100) 224 * 5 = nanoseconds, maximum precision 225 * 226 * Note that utimes() precision is microseconds because it takes a timeval 227 * structure, so its probably best to default to USEC or USEC_PRECISE, and 228 * not NSEC. 229 */ 230 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC, 231 TSP_USEC_PRECISE, TSP_NSEC_PRECISE }; 232 233 __read_mostly static int timestamp_precision = -1; 234 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 235 ×tamp_precision, 0, "Precision of file timestamps"); 236 237 /* 238 * Get a current timestamp. 239 * 240 * MPSAFE 241 */ 242 void 243 vfs_timestamp(struct timespec *tsp) 244 { 245 switch (timestamp_precision) { 246 case TSP_SEC: /* seconds precision */ 247 getnanotime(tsp); 248 tsp->tv_nsec = 0; 249 break; 250 case TSP_HZ: /* ticks precision (limit to microseconds) */ 251 getnanotime(tsp); 252 tsp->tv_nsec -= tsp->tv_nsec % 1000; 253 break; 254 default: 255 case TSP_USEC: /* microseconds (ticks precision) */ 256 getnanotime(tsp); 257 tsp->tv_nsec -= tsp->tv_nsec % 1000; 258 break; 259 case TSP_NSEC: /* nanoseconds (ticks precision) */ 260 getnanotime(tsp); 261 break; 262 case TSP_USEC_PRECISE: /* microseconds (high preceision) */ 263 nanotime(tsp); 264 tsp->tv_nsec -= tsp->tv_nsec % 1000; 265 break; 266 case TSP_NSEC_PRECISE: /* nanoseconds (high precision) */ 267 nanotime(tsp); 268 break; 269 } 270 } 271 272 /* 273 * Set vnode attributes to VNOVAL 274 */ 275 void 276 vattr_null(struct vattr *vap) 277 { 278 vap->va_type = VNON; 279 vap->va_size = VNOVAL; 280 vap->va_bytes = VNOVAL; 281 vap->va_mode = VNOVAL; 282 vap->va_nlink = VNOVAL; 283 vap->va_uid = VNOVAL; 284 vap->va_gid = VNOVAL; 285 vap->va_fsid = VNOVAL; 286 vap->va_fileid = VNOVAL; 287 vap->va_blocksize = VNOVAL; 288 vap->va_rmajor = VNOVAL; 289 vap->va_rminor = VNOVAL; 290 vap->va_atime.tv_sec = VNOVAL; 291 vap->va_atime.tv_nsec = VNOVAL; 292 vap->va_mtime.tv_sec = VNOVAL; 293 vap->va_mtime.tv_nsec = VNOVAL; 294 vap->va_ctime.tv_sec = VNOVAL; 295 vap->va_ctime.tv_nsec = VNOVAL; 296 vap->va_flags = VNOVAL; 297 vap->va_gen = VNOVAL; 298 vap->va_vaflags = 0; 299 /* va_*_uuid fields are only valid if related flags are set */ 300 } 301 302 /* 303 * Flush out and invalidate all buffers associated with a vnode. 304 * 305 * vp must be locked. 306 */ 307 static int vinvalbuf_bp(struct buf *bp, void *data); 308 309 struct vinvalbuf_bp_info { 310 struct vnode *vp; 311 int slptimeo; 312 int lkflags; 313 int flags; 314 int clean; 315 }; 316 317 int 318 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 319 { 320 struct vinvalbuf_bp_info info; 321 vm_object_t object; 322 int error; 323 324 lwkt_gettoken(&vp->v_token); 325 326 /* 327 * If we are being asked to save, call fsync to ensure that the inode 328 * is updated. 329 */ 330 if (flags & V_SAVE) { 331 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo); 332 if (error) 333 goto done; 334 if (!RB_EMPTY(&vp->v_rbdirty_tree)) { 335 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0) 336 goto done; 337 #if 0 338 /* 339 * Dirty bufs may be left or generated via races 340 * in circumstances where vinvalbuf() is called on 341 * a vnode not undergoing reclamation. Only 342 * panic if we are trying to reclaim the vnode. 343 */ 344 if ((vp->v_flag & VRECLAIMED) && 345 (bio_track_active(&vp->v_track_write) || 346 !RB_EMPTY(&vp->v_rbdirty_tree))) { 347 panic("vinvalbuf: dirty bufs"); 348 } 349 #endif 350 } 351 } 352 info.slptimeo = slptimeo; 353 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL; 354 if (slpflag & PCATCH) 355 info.lkflags |= LK_PCATCH; 356 info.flags = flags; 357 info.vp = vp; 358 359 /* 360 * Flush the buffer cache until nothing is left, wait for all I/O 361 * to complete. At least one pass is required. We might block 362 * in the pip code so we have to re-check. Order is important. 363 */ 364 do { 365 /* 366 * Flush buffer cache 367 */ 368 if (!RB_EMPTY(&vp->v_rbclean_tree)) { 369 info.clean = 1; 370 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 371 NULL, vinvalbuf_bp, &info); 372 } 373 if (!RB_EMPTY(&vp->v_rbdirty_tree)) { 374 info.clean = 0; 375 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 376 NULL, vinvalbuf_bp, &info); 377 } 378 379 /* 380 * Wait for I/O completion. 381 */ 382 bio_track_wait(&vp->v_track_write, 0, 0); 383 if ((object = vp->v_object) != NULL) 384 refcount_wait(&object->paging_in_progress, "vnvlbx"); 385 } while (bio_track_active(&vp->v_track_write) || 386 !RB_EMPTY(&vp->v_rbclean_tree) || 387 !RB_EMPTY(&vp->v_rbdirty_tree)); 388 389 /* 390 * Destroy the copy in the VM cache, too. 391 */ 392 if ((object = vp->v_object) != NULL) { 393 vm_object_page_remove(object, 0, 0, 394 (flags & V_SAVE) ? TRUE : FALSE); 395 } 396 397 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree)) 398 panic("vinvalbuf: flush failed"); 399 if (!RB_EMPTY(&vp->v_rbhash_tree)) 400 panic("vinvalbuf: flush failed, buffers still present"); 401 error = 0; 402 done: 403 lwkt_reltoken(&vp->v_token); 404 return (error); 405 } 406 407 static int 408 vinvalbuf_bp(struct buf *bp, void *data) 409 { 410 struct vinvalbuf_bp_info *info = data; 411 int error; 412 413 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 414 atomic_add_int(&bp->b_refs, 1); 415 error = BUF_TIMELOCK(bp, info->lkflags, 416 "vinvalbuf", info->slptimeo); 417 atomic_subtract_int(&bp->b_refs, 1); 418 if (error == 0) { 419 BUF_UNLOCK(bp); 420 error = ENOLCK; 421 } 422 if (error == ENOLCK) 423 return(0); 424 return (-error); 425 } 426 KKASSERT(bp->b_vp == info->vp); 427 428 /* 429 * Must check clean/dirty status after successfully locking as 430 * it may race. 431 */ 432 if ((info->clean && (bp->b_flags & B_DELWRI)) || 433 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) { 434 BUF_UNLOCK(bp); 435 return(0); 436 } 437 438 /* 439 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite() 440 * check. This code will write out the buffer, period. 441 */ 442 bremfree(bp); 443 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 444 (info->flags & V_SAVE)) { 445 cluster_awrite(bp); 446 } else if (info->flags & V_SAVE) { 447 /* 448 * Cannot set B_NOCACHE on a clean buffer as this will 449 * destroy the VM backing store which might actually 450 * be dirty (and unsynchronized). 451 */ 452 bp->b_flags |= (B_INVAL | B_RELBUF); 453 brelse(bp); 454 } else { 455 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 456 brelse(bp); 457 } 458 return(0); 459 } 460 461 /* 462 * Truncate a file's buffer and pages to a specified length. This 463 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 464 * sync activity. 465 * 466 * The vnode must be locked. 467 */ 468 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data); 469 static int vtruncbuf_bp_trunc(struct buf *bp, void *data); 470 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data); 471 static int vtruncbuf_bp_metasync(struct buf *bp, void *data); 472 473 struct vtruncbuf_info { 474 struct vnode *vp; 475 off_t truncloffset; 476 int clean; 477 }; 478 479 int 480 vtruncbuf(struct vnode *vp, off_t length, int blksize) 481 { 482 struct vtruncbuf_info info; 483 const char *filename; 484 int count; 485 486 /* 487 * Round up to the *next* block, then destroy the buffers in question. 488 * Since we are only removing some of the buffers we must rely on the 489 * scan count to determine whether a loop is necessary. 490 */ 491 if ((count = (int)(length % blksize)) != 0) 492 info.truncloffset = length + (blksize - count); 493 else 494 info.truncloffset = length; 495 info.vp = vp; 496 497 lwkt_gettoken(&vp->v_token); 498 do { 499 info.clean = 1; 500 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 501 vtruncbuf_bp_trunc_cmp, 502 vtruncbuf_bp_trunc, &info); 503 info.clean = 0; 504 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 505 vtruncbuf_bp_trunc_cmp, 506 vtruncbuf_bp_trunc, &info); 507 } while(count); 508 509 /* 510 * For safety, fsync any remaining metadata if the file is not being 511 * truncated to 0. Since the metadata does not represent the entire 512 * dirty list we have to rely on the hit count to ensure that we get 513 * all of it. 514 */ 515 if (length > 0) { 516 do { 517 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 518 vtruncbuf_bp_metasync_cmp, 519 vtruncbuf_bp_metasync, &info); 520 } while (count); 521 } 522 523 /* 524 * Clean out any left over VM backing store. 525 * 526 * It is possible to have in-progress I/O from buffers that were 527 * not part of the truncation. This should not happen if we 528 * are truncating to 0-length. 529 */ 530 vnode_pager_setsize(vp, length); 531 bio_track_wait(&vp->v_track_write, 0, 0); 532 533 /* 534 * Debugging only 535 */ 536 spin_lock(&vp->v_spin); 537 filename = TAILQ_FIRST(&vp->v_namecache) ? 538 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?"; 539 spin_unlock(&vp->v_spin); 540 541 /* 542 * Make sure no buffers were instantiated while we were trying 543 * to clean out the remaining VM pages. This could occur due 544 * to busy dirty VM pages being flushed out to disk. 545 */ 546 do { 547 info.clean = 1; 548 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 549 vtruncbuf_bp_trunc_cmp, 550 vtruncbuf_bp_trunc, &info); 551 info.clean = 0; 552 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 553 vtruncbuf_bp_trunc_cmp, 554 vtruncbuf_bp_trunc, &info); 555 if (count) { 556 kprintf("Warning: vtruncbuf(): Had to re-clean %d " 557 "left over buffers in %s\n", count, filename); 558 } 559 } while(count); 560 561 lwkt_reltoken(&vp->v_token); 562 563 return (0); 564 } 565 566 /* 567 * The callback buffer is beyond the new file EOF and must be destroyed. 568 * Note that the compare function must conform to the RB_SCAN's requirements. 569 */ 570 static 571 int 572 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data) 573 { 574 struct vtruncbuf_info *info = data; 575 576 if (bp->b_loffset >= info->truncloffset) 577 return(0); 578 return(-1); 579 } 580 581 static 582 int 583 vtruncbuf_bp_trunc(struct buf *bp, void *data) 584 { 585 struct vtruncbuf_info *info = data; 586 587 /* 588 * Do not try to use a buffer we cannot immediately lock, but sleep 589 * anyway to prevent a livelock. The code will loop until all buffers 590 * can be acted upon. 591 * 592 * We must always revalidate the buffer after locking it to deal 593 * with MP races. 594 */ 595 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 596 atomic_add_int(&bp->b_refs, 1); 597 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) 598 BUF_UNLOCK(bp); 599 atomic_subtract_int(&bp->b_refs, 1); 600 } else if ((info->clean && (bp->b_flags & B_DELWRI)) || 601 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) || 602 bp->b_vp != info->vp || 603 vtruncbuf_bp_trunc_cmp(bp, data)) { 604 BUF_UNLOCK(bp); 605 } else { 606 bremfree(bp); 607 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE); 608 brelse(bp); 609 } 610 return(1); 611 } 612 613 /* 614 * Fsync all meta-data after truncating a file to be non-zero. Only metadata 615 * blocks (with a negative loffset) are scanned. 616 * Note that the compare function must conform to the RB_SCAN's requirements. 617 */ 618 static int 619 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused) 620 { 621 if (bp->b_loffset < 0) 622 return(0); 623 return(1); 624 } 625 626 static int 627 vtruncbuf_bp_metasync(struct buf *bp, void *data) 628 { 629 struct vtruncbuf_info *info = data; 630 631 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 632 atomic_add_int(&bp->b_refs, 1); 633 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) 634 BUF_UNLOCK(bp); 635 atomic_subtract_int(&bp->b_refs, 1); 636 } else if ((bp->b_flags & B_DELWRI) == 0 || 637 bp->b_vp != info->vp || 638 vtruncbuf_bp_metasync_cmp(bp, data)) { 639 BUF_UNLOCK(bp); 640 } else { 641 bremfree(bp); 642 if (bp->b_vp == info->vp) 643 bawrite(bp); 644 else 645 bwrite(bp); 646 } 647 return(1); 648 } 649 650 /* 651 * vfsync - implements a multipass fsync on a file which understands 652 * dependancies and meta-data. The passed vnode must be locked. The 653 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY. 654 * 655 * When fsyncing data asynchronously just do one consolidated pass starting 656 * with the most negative block number. This may not get all the data due 657 * to dependancies. 658 * 659 * When fsyncing data synchronously do a data pass, then a metadata pass, 660 * then do additional data+metadata passes to try to get all the data out. 661 * 662 * Caller must ref the vnode but does not have to lock it. 663 */ 664 static int vfsync_wait_output(struct vnode *vp, 665 int (*waitoutput)(struct vnode *, struct thread *)); 666 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused); 667 static int vfsync_data_only_cmp(struct buf *bp, void *data); 668 static int vfsync_meta_only_cmp(struct buf *bp, void *data); 669 static int vfsync_lazy_range_cmp(struct buf *bp, void *data); 670 static int vfsync_bp(struct buf *bp, void *data); 671 672 struct vfsync_info { 673 struct vnode *vp; 674 int fastpass; 675 int synchronous; 676 int syncdeps; 677 int lazycount; 678 int lazylimit; 679 int skippedbufs; 680 int (*checkdef)(struct buf *); 681 int (*cmpfunc)(struct buf *, void *); 682 }; 683 684 int 685 vfsync(struct vnode *vp, int waitfor, int passes, 686 int (*checkdef)(struct buf *), 687 int (*waitoutput)(struct vnode *, struct thread *)) 688 { 689 struct vfsync_info info; 690 int error; 691 692 bzero(&info, sizeof(info)); 693 info.vp = vp; 694 if ((info.checkdef = checkdef) == NULL) 695 info.syncdeps = 1; 696 697 lwkt_gettoken(&vp->v_token); 698 699 switch(waitfor) { 700 case MNT_LAZY | MNT_NOWAIT: 701 case MNT_LAZY: 702 /* 703 * Lazy (filesystem syncer typ) Asynchronous plus limit the 704 * number of data (not meta) pages we try to flush to 1MB. 705 * A non-zero return means that lazy limit was reached. 706 */ 707 info.lazylimit = 1024 * 1024; 708 info.syncdeps = 1; 709 info.cmpfunc = vfsync_lazy_range_cmp; 710 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 711 vfsync_lazy_range_cmp, vfsync_bp, &info); 712 info.cmpfunc = vfsync_meta_only_cmp; 713 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 714 vfsync_meta_only_cmp, vfsync_bp, &info); 715 if (error == 0) 716 vp->v_lazyw = 0; 717 else if (!RB_EMPTY(&vp->v_rbdirty_tree)) 718 vn_syncer_add(vp, 1); 719 error = 0; 720 break; 721 case MNT_NOWAIT: 722 /* 723 * Asynchronous. Do a data-only pass and a meta-only pass. 724 */ 725 info.syncdeps = 1; 726 info.cmpfunc = vfsync_data_only_cmp; 727 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, 728 vfsync_bp, &info); 729 info.cmpfunc = vfsync_meta_only_cmp; 730 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp, 731 vfsync_bp, &info); 732 error = 0; 733 break; 734 default: 735 /* 736 * Synchronous. Do a data-only pass, then a meta-data+data 737 * pass, then additional integrated passes to try to get 738 * all the dependancies flushed. 739 */ 740 info.cmpfunc = vfsync_data_only_cmp; 741 info.fastpass = 1; 742 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, 743 vfsync_bp, &info); 744 info.fastpass = 0; 745 error = vfsync_wait_output(vp, waitoutput); 746 if (error == 0) { 747 info.skippedbufs = 0; 748 info.cmpfunc = vfsync_dummy_cmp; 749 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 750 vfsync_bp, &info); 751 error = vfsync_wait_output(vp, waitoutput); 752 if (info.skippedbufs) { 753 kprintf("Warning: vfsync skipped %d dirty " 754 "buf%s in pass2!\n", 755 info.skippedbufs, 756 ((info.skippedbufs > 1) ? "s" : "")); 757 } 758 } 759 while (error == 0 && passes > 0 && 760 !RB_EMPTY(&vp->v_rbdirty_tree) 761 ) { 762 info.skippedbufs = 0; 763 if (--passes == 0) { 764 info.synchronous = 1; 765 info.syncdeps = 1; 766 } 767 info.cmpfunc = vfsync_dummy_cmp; 768 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 769 vfsync_bp, &info); 770 if (error < 0) 771 error = -error; 772 info.syncdeps = 1; 773 if (error == 0) 774 error = vfsync_wait_output(vp, waitoutput); 775 if (info.skippedbufs && passes == 0) { 776 kprintf("Warning: vfsync skipped %d dirty " 777 "buf%s in final pass!\n", 778 info.skippedbufs, 779 ((info.skippedbufs > 1) ? "s" : "")); 780 } 781 } 782 #if 0 783 /* 784 * This case can occur normally because vnode lock might 785 * not be held. 786 */ 787 if (!RB_EMPTY(&vp->v_rbdirty_tree)) 788 kprintf("dirty bufs left after final pass\n"); 789 #endif 790 break; 791 } 792 lwkt_reltoken(&vp->v_token); 793 794 return(error); 795 } 796 797 static int 798 vfsync_wait_output(struct vnode *vp, 799 int (*waitoutput)(struct vnode *, struct thread *)) 800 { 801 int error; 802 803 error = bio_track_wait(&vp->v_track_write, 0, 0); 804 if (waitoutput) 805 error = waitoutput(vp, curthread); 806 return(error); 807 } 808 809 static int 810 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused) 811 { 812 return(0); 813 } 814 815 static int 816 vfsync_data_only_cmp(struct buf *bp, void *data) 817 { 818 if (bp->b_loffset < 0) 819 return(-1); 820 return(0); 821 } 822 823 static int 824 vfsync_meta_only_cmp(struct buf *bp, void *data) 825 { 826 if (bp->b_loffset < 0) 827 return(0); 828 return(1); 829 } 830 831 static int 832 vfsync_lazy_range_cmp(struct buf *bp, void *data) 833 { 834 struct vfsync_info *info = data; 835 836 if (bp->b_loffset < info->vp->v_lazyw) 837 return(-1); 838 return(0); 839 } 840 841 static int 842 vfsync_bp(struct buf *bp, void *data) 843 { 844 struct vfsync_info *info = data; 845 struct vnode *vp = info->vp; 846 int error; 847 848 if (info->fastpass) { 849 /* 850 * Ignore buffers that we cannot immediately lock. 851 */ 852 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 853 /* 854 * Removed BUF_TIMELOCK(..., 1), even a 1-tick 855 * delay can mess up performance 856 * 857 * Another reason is that during a dirty-buffer 858 * scan a clustered write can start I/O on buffers 859 * ahead of the scan, causing the scan to not 860 * get a lock here. Usually this means the write 861 * is already in progress so, in fact, we *want* 862 * to skip the buffer. 863 */ 864 ++info->skippedbufs; 865 return(0); 866 } 867 } else if (info->synchronous == 0) { 868 /* 869 * Normal pass, give the buffer a little time to become 870 * available to us. 871 */ 872 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst2", hz / 10)) { 873 ++info->skippedbufs; 874 return(0); 875 } 876 } else { 877 /* 878 * Synchronous pass, give the buffer a lot of time before 879 * giving up. 880 */ 881 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst3", hz * 10)) { 882 ++info->skippedbufs; 883 return(0); 884 } 885 } 886 887 /* 888 * We must revalidate the buffer after locking. 889 */ 890 if ((bp->b_flags & B_DELWRI) == 0 || 891 bp->b_vp != info->vp || 892 info->cmpfunc(bp, data)) { 893 BUF_UNLOCK(bp); 894 return(0); 895 } 896 897 /* 898 * If syncdeps is not set we do not try to write buffers which have 899 * dependancies. 900 */ 901 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) { 902 BUF_UNLOCK(bp); 903 return(0); 904 } 905 906 /* 907 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer 908 * has been written but an additional handshake with the device 909 * is required before we can dispose of the buffer. We have no idea 910 * how to do this so we have to skip these buffers. 911 */ 912 if (bp->b_flags & B_NEEDCOMMIT) { 913 BUF_UNLOCK(bp); 914 return(0); 915 } 916 917 /* 918 * Ask bioops if it is ok to sync. If not the VFS may have 919 * set B_LOCKED so we have to cycle the buffer. 920 */ 921 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) { 922 bremfree(bp); 923 brelse(bp); 924 return(0); 925 } 926 927 if (info->synchronous) { 928 /* 929 * Synchronous flush. An error may be returned and will 930 * stop the scan. 931 */ 932 bremfree(bp); 933 error = bwrite(bp); 934 } else { 935 /* 936 * Asynchronous flush. We use the error return to support 937 * MNT_LAZY flushes. 938 * 939 * In low-memory situations we revert to synchronous 940 * operation. This should theoretically prevent the I/O 941 * path from exhausting memory in a non-recoverable way. 942 */ 943 vp->v_lazyw = bp->b_loffset; 944 bremfree(bp); 945 if (vm_paging_min()) { 946 /* low memory */ 947 info->lazycount += bp->b_bufsize; 948 bwrite(bp); 949 } else { 950 /* normal */ 951 info->lazycount += cluster_awrite(bp); 952 waitrunningbufspace(); 953 /*vm_wait_nominal();*/ 954 } 955 if (info->lazylimit && info->lazycount >= info->lazylimit) 956 error = 1; 957 else 958 error = 0; 959 } 960 return(-error); 961 } 962 963 /* 964 * Associate a buffer with a vnode. 965 * 966 * MPSAFE 967 */ 968 int 969 bgetvp(struct vnode *vp, struct buf *bp, int testsize) 970 { 971 KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 972 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0); 973 974 /* 975 * Insert onto list for new vnode. 976 */ 977 lwkt_gettoken(&vp->v_token); 978 979 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) { 980 lwkt_reltoken(&vp->v_token); 981 return (EEXIST); 982 } 983 984 /* 985 * Diagnostics (mainly for HAMMER debugging). Check for 986 * overlapping buffers. 987 */ 988 if (check_buf_overlap) { 989 struct buf *bx; 990 bx = buf_rb_hash_RB_PREV(bp); 991 if (bx) { 992 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) { 993 kprintf("bgetvp: overlapl %016jx/%d %016jx " 994 "bx %p bp %p\n", 995 (intmax_t)bx->b_loffset, 996 bx->b_bufsize, 997 (intmax_t)bp->b_loffset, 998 bx, bp); 999 if (check_buf_overlap > 1) 1000 panic("bgetvp - overlapping buffer"); 1001 } 1002 } 1003 bx = buf_rb_hash_RB_NEXT(bp); 1004 if (bx) { 1005 if (bp->b_loffset + testsize > bx->b_loffset) { 1006 kprintf("bgetvp: overlapr %016jx/%d %016jx " 1007 "bp %p bx %p\n", 1008 (intmax_t)bp->b_loffset, 1009 testsize, 1010 (intmax_t)bx->b_loffset, 1011 bp, bx); 1012 if (check_buf_overlap > 1) 1013 panic("bgetvp - overlapping buffer"); 1014 } 1015 } 1016 } 1017 bp->b_vp = vp; 1018 bp->b_flags |= B_HASHED; 1019 bp->b_flags |= B_VNCLEAN; 1020 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) 1021 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp); 1022 /*vhold(vp);*/ 1023 lwkt_reltoken(&vp->v_token); 1024 return(0); 1025 } 1026 1027 /* 1028 * Disassociate a buffer from a vnode. 1029 * 1030 * MPSAFE 1031 */ 1032 void 1033 brelvp(struct buf *bp) 1034 { 1035 struct vnode *vp; 1036 1037 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1038 1039 /* 1040 * Delete from old vnode list, if on one. 1041 */ 1042 vp = bp->b_vp; 1043 lwkt_gettoken(&vp->v_token); 1044 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) { 1045 if (bp->b_flags & B_VNDIRTY) 1046 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); 1047 else 1048 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); 1049 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN); 1050 } 1051 if (bp->b_flags & B_HASHED) { 1052 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp); 1053 bp->b_flags &= ~B_HASHED; 1054 } 1055 1056 /* 1057 * Only remove from synclist when no dirty buffers are left AND 1058 * the VFS has not flagged the vnode's inode as being dirty. 1059 */ 1060 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST && 1061 RB_EMPTY(&vp->v_rbdirty_tree)) { 1062 vn_syncer_remove(vp, 0); 1063 } 1064 bp->b_vp = NULL; 1065 1066 lwkt_reltoken(&vp->v_token); 1067 1068 /*vdrop(vp);*/ 1069 } 1070 1071 /* 1072 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI. 1073 * This routine is called when the state of the B_DELWRI bit is changed. 1074 * 1075 * Must be called with vp->v_token held. 1076 * MPSAFE 1077 */ 1078 void 1079 reassignbuf(struct buf *bp) 1080 { 1081 struct vnode *vp = bp->b_vp; 1082 int delay; 1083 1084 ASSERT_LWKT_TOKEN_HELD(&vp->v_token); 1085 ++reassignbufcalls; 1086 1087 /* 1088 * B_PAGING flagged buffers cannot be reassigned because their vp 1089 * is not fully linked in. 1090 */ 1091 if (bp->b_flags & B_PAGING) 1092 panic("cannot reassign paging buffer"); 1093 1094 if (bp->b_flags & B_DELWRI) { 1095 /* 1096 * Move to the dirty list, add the vnode to the worklist 1097 */ 1098 if (bp->b_flags & B_VNCLEAN) { 1099 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); 1100 bp->b_flags &= ~B_VNCLEAN; 1101 } 1102 if ((bp->b_flags & B_VNDIRTY) == 0) { 1103 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) { 1104 panic("reassignbuf: dup lblk vp %p bp %p", 1105 vp, bp); 1106 } 1107 bp->b_flags |= B_VNDIRTY; 1108 } 1109 if ((vp->v_flag & VONWORKLST) == 0) { 1110 switch (vp->v_type) { 1111 case VDIR: 1112 delay = dirdelay; 1113 break; 1114 case VCHR: 1115 case VBLK: 1116 if (vp->v_rdev && 1117 vp->v_rdev->si_mountpoint != NULL) { 1118 delay = metadelay; 1119 break; 1120 } 1121 /* fall through */ 1122 default: 1123 delay = filedelay; 1124 } 1125 vn_syncer_add(vp, delay); 1126 } 1127 } else { 1128 /* 1129 * Move to the clean list, remove the vnode from the worklist 1130 * if no dirty blocks remain. 1131 */ 1132 if (bp->b_flags & B_VNDIRTY) { 1133 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); 1134 bp->b_flags &= ~B_VNDIRTY; 1135 } 1136 if ((bp->b_flags & B_VNCLEAN) == 0) { 1137 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) { 1138 panic("reassignbuf: dup lblk vp %p bp %p", 1139 vp, bp); 1140 } 1141 bp->b_flags |= B_VNCLEAN; 1142 } 1143 1144 /* 1145 * Only remove from synclist when no dirty buffers are left 1146 * AND the VFS has not flagged the vnode's inode as being 1147 * dirty. 1148 */ 1149 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == 1150 VONWORKLST && 1151 RB_EMPTY(&vp->v_rbdirty_tree)) { 1152 vn_syncer_remove(vp, 0); 1153 } 1154 } 1155 } 1156 1157 /* 1158 * Create a vnode for a block device. Used for mounting the root file 1159 * system. 1160 * 1161 * A vref()'d vnode is returned. 1162 */ 1163 extern struct vop_ops *devfs_vnode_dev_vops_p; 1164 int 1165 bdevvp(cdev_t dev, struct vnode **vpp) 1166 { 1167 struct vnode *vp; 1168 struct vnode *nvp; 1169 int error; 1170 1171 if (dev == NULL) { 1172 *vpp = NULLVP; 1173 return (ENXIO); 1174 } 1175 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p, 1176 &nvp, 0, 0); 1177 if (error) { 1178 *vpp = NULLVP; 1179 return (error); 1180 } 1181 vp = nvp; 1182 vp->v_type = VCHR; 1183 #if 0 1184 vp->v_rdev = dev; 1185 #endif 1186 v_associate_rdev(vp, dev); 1187 vp->v_umajor = dev->si_umajor; 1188 vp->v_uminor = dev->si_uminor; 1189 vx_unlock(vp); 1190 *vpp = vp; 1191 return (0); 1192 } 1193 1194 int 1195 v_associate_rdev(struct vnode *vp, cdev_t dev) 1196 { 1197 if (dev == NULL) 1198 return(ENXIO); 1199 if (dev_is_good(dev) == 0) 1200 return(ENXIO); 1201 KKASSERT(vp->v_rdev == NULL); 1202 vp->v_rdev = reference_dev(dev); 1203 lwkt_gettoken(&spechash_token); 1204 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext); 1205 lwkt_reltoken(&spechash_token); 1206 return(0); 1207 } 1208 1209 void 1210 v_release_rdev(struct vnode *vp) 1211 { 1212 cdev_t dev; 1213 1214 if ((dev = vp->v_rdev) != NULL) { 1215 lwkt_gettoken(&spechash_token); 1216 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext); 1217 vp->v_rdev = NULL; 1218 release_dev(dev); 1219 lwkt_reltoken(&spechash_token); 1220 } 1221 } 1222 1223 /* 1224 * Add a vnode to the alias list hung off the cdev_t. We only associate 1225 * the device number with the vnode. The actual device is not associated 1226 * until the vnode is opened (usually in spec_open()), and will be 1227 * disassociated on last close. 1228 */ 1229 void 1230 addaliasu(struct vnode *nvp, int x, int y) 1231 { 1232 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 1233 panic("addaliasu on non-special vnode"); 1234 nvp->v_umajor = x; 1235 nvp->v_uminor = y; 1236 } 1237 1238 /* 1239 * Simple call that a filesystem can make to try to get rid of a 1240 * vnode. It will fail if anyone is referencing the vnode (including 1241 * the caller). 1242 * 1243 * The filesystem can check whether its in-memory inode structure still 1244 * references the vp on return. 1245 * 1246 * May only be called if the vnode is in a known state (i.e. being prevented 1247 * from being deallocated by some other condition such as a vfs inode hold). 1248 * 1249 * This call might not succeed. 1250 */ 1251 void 1252 vclean_unlocked(struct vnode *vp) 1253 { 1254 vx_get(vp); 1255 if (VREFCNT(vp) <= 1) 1256 vgone_vxlocked(vp); 1257 vx_put(vp); 1258 } 1259 1260 /* 1261 * Disassociate a vnode from its underlying filesystem. 1262 * 1263 * The vnode must be VX locked and referenced. In all normal situations 1264 * there are no active references. If vclean_vxlocked() is called while 1265 * there are active references, the vnode is being ripped out and we have 1266 * to call VOP_CLOSE() as appropriate before we can reclaim it. 1267 */ 1268 static void 1269 vclean_vxlocked(struct vnode *vp, int flags) 1270 { 1271 int active; 1272 int n; 1273 vm_object_t object; 1274 struct namecache *ncp; 1275 1276 /* 1277 * If the vnode has already been reclaimed we have nothing to do. 1278 */ 1279 if (vp->v_flag & VRECLAIMED) 1280 return; 1281 1282 /* 1283 * Set flag to interlock operation, flag finalization to ensure 1284 * that the vnode winds up on the inactive list, and set v_act to 0. 1285 */ 1286 vsetflags(vp, VRECLAIMED); 1287 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE); 1288 vp->v_act = 0; 1289 1290 if (verbose_reclaims) { 1291 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) 1292 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name); 1293 } 1294 1295 /* 1296 * Scrap the vfs cache 1297 */ 1298 while (cache_inval_vp(vp, 0) != 0) { 1299 kprintf("Warning: vnode %p clean/cache_resolution " 1300 "race detected\n", vp); 1301 tsleep(vp, 0, "vclninv", 2); 1302 } 1303 1304 /* 1305 * Check to see if the vnode is in use. If so we have to reference it 1306 * before we clean it out so that its count cannot fall to zero and 1307 * generate a race against ourselves to recycle it. 1308 */ 1309 active = (VREFCNT(vp) > 0); 1310 1311 /* 1312 * Clean out any buffers associated with the vnode and destroy its 1313 * object, if it has one. 1314 */ 1315 vinvalbuf(vp, V_SAVE, 0, 0); 1316 1317 /* 1318 * If purging an active vnode (typically during a forced unmount 1319 * or reboot), it must be closed and deactivated before being 1320 * reclaimed. This isn't really all that safe, but what can 1321 * we do? XXX. 1322 * 1323 * Note that neither of these routines unlocks the vnode. 1324 */ 1325 if (active && (flags & DOCLOSE)) { 1326 while ((n = vp->v_opencount) != 0) { 1327 if (vp->v_writecount) 1328 VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL); 1329 else 1330 VOP_CLOSE(vp, FNONBLOCK, NULL); 1331 if (vp->v_opencount == n) { 1332 kprintf("Warning: unable to force-close" 1333 " vnode %p\n", vp); 1334 break; 1335 } 1336 } 1337 } 1338 1339 /* 1340 * If the vnode has not been deactivated, deactivated it. Deactivation 1341 * can create new buffers and VM pages so we have to call vinvalbuf() 1342 * again to make sure they all get flushed. 1343 * 1344 * This can occur if a file with a link count of 0 needs to be 1345 * truncated. 1346 * 1347 * If the vnode is already dead don't try to deactivate it. 1348 */ 1349 if ((vp->v_flag & VINACTIVE) == 0) { 1350 vsetflags(vp, VINACTIVE); 1351 if (vp->v_mount) 1352 VOP_INACTIVE(vp); 1353 vinvalbuf(vp, V_SAVE, 0, 0); 1354 } 1355 1356 /* 1357 * If the vnode has an object, destroy it. 1358 */ 1359 while ((object = vp->v_object) != NULL) { 1360 vm_object_hold(object); 1361 if (object == vp->v_object) 1362 break; 1363 vm_object_drop(object); 1364 } 1365 1366 if (object != NULL) { 1367 if (object->ref_count == 0) { 1368 if ((object->flags & OBJ_DEAD) == 0) 1369 vm_object_terminate(object); 1370 vm_object_drop(object); 1371 vclrflags(vp, VOBJBUF); 1372 } else { 1373 vm_pager_deallocate(object); 1374 vclrflags(vp, VOBJBUF); 1375 vm_object_drop(object); 1376 } 1377 } 1378 KKASSERT((vp->v_flag & VOBJBUF) == 0); 1379 1380 if (vp->v_flag & VOBJDIRTY) 1381 vclrobjdirty(vp); 1382 1383 /* 1384 * Reclaim the vnode if not already dead. 1385 */ 1386 if (vp->v_mount && VOP_RECLAIM(vp)) 1387 panic("vclean: cannot reclaim"); 1388 1389 /* 1390 * Done with purge, notify sleepers of the grim news. 1391 */ 1392 vp->v_ops = &dead_vnode_vops_p; 1393 vn_gone(vp); 1394 vp->v_tag = VT_NON; 1395 1396 /* 1397 * If we are destroying an active vnode, reactivate it now that 1398 * we have reassociated it with deadfs. This prevents the system 1399 * from crashing on the vnode due to it being unexpectedly marked 1400 * as inactive or reclaimed. 1401 */ 1402 if (active && (flags & DOCLOSE)) { 1403 vclrflags(vp, VINACTIVE | VRECLAIMED); 1404 } 1405 } 1406 1407 /* 1408 * Eliminate all activity associated with the requested vnode 1409 * and with all vnodes aliased to the requested vnode. 1410 * 1411 * The vnode must be referenced but should not be locked. 1412 */ 1413 int 1414 vrevoke(struct vnode *vp, struct ucred *cred) 1415 { 1416 struct vnode *vq; 1417 struct vnode *vqn; 1418 cdev_t dev; 1419 int error; 1420 1421 /* 1422 * If the vnode has a device association, scrap all vnodes associated 1423 * with the device. Don't let the device disappear on us while we 1424 * are scrapping the vnodes. 1425 * 1426 * The passed vp will probably show up in the list, do not VX lock 1427 * it twice! 1428 * 1429 * Releasing the vnode's rdev here can mess up specfs's call to 1430 * device close, so don't do it. The vnode has been disassociated 1431 * and the device will be closed after the last ref on the related 1432 * fp goes away (if not still open by e.g. the kernel). 1433 */ 1434 if (vp->v_type != VCHR) { 1435 error = fdrevoke(vp, DTYPE_VNODE, cred); 1436 return (error); 1437 } 1438 if ((dev = vp->v_rdev) == NULL) { 1439 return(0); 1440 } 1441 reference_dev(dev); 1442 lwkt_gettoken(&spechash_token); 1443 1444 restart: 1445 vqn = SLIST_FIRST(&dev->si_hlist); 1446 if (vqn) 1447 vhold(vqn); 1448 while ((vq = vqn) != NULL) { 1449 if (VREFCNT(vq) > 0) { 1450 vref(vq); 1451 fdrevoke(vq, DTYPE_VNODE, cred); 1452 /*v_release_rdev(vq);*/ 1453 vrele(vq); 1454 if (vq->v_rdev != dev) { 1455 vdrop(vq); 1456 goto restart; 1457 } 1458 } 1459 vqn = SLIST_NEXT(vq, v_cdevnext); 1460 if (vqn) 1461 vhold(vqn); 1462 vdrop(vq); 1463 } 1464 lwkt_reltoken(&spechash_token); 1465 dev_drevoke(dev); 1466 release_dev(dev); 1467 return (0); 1468 } 1469 1470 /* 1471 * This is called when the object underlying a vnode is being destroyed, 1472 * such as in a remove(). Try to recycle the vnode immediately if the 1473 * only active reference is our reference. 1474 * 1475 * Directory vnodes in the namecache with children cannot be immediately 1476 * recycled because numerous VOP_N*() ops require them to be stable. 1477 * 1478 * To avoid recursive recycling from VOP_INACTIVE implemenetations this 1479 * function is a NOP if VRECLAIMED is already set. 1480 */ 1481 int 1482 vrecycle(struct vnode *vp) 1483 { 1484 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) { 1485 if (cache_inval_vp_nonblock(vp)) 1486 return(0); 1487 vgone_vxlocked(vp); 1488 return (1); 1489 } 1490 return (0); 1491 } 1492 1493 /* 1494 * Return the maximum I/O size allowed for strategy calls on VP. 1495 * 1496 * If vp is VCHR or VBLK we dive the device, otherwise we use 1497 * the vp's mount info. 1498 * 1499 * The returned value is clamped at MAXPHYS as most callers cannot use 1500 * buffers larger than that size. 1501 */ 1502 int 1503 vmaxiosize(struct vnode *vp) 1504 { 1505 int maxiosize; 1506 1507 if (vp->v_type == VBLK || vp->v_type == VCHR) 1508 maxiosize = vp->v_rdev->si_iosize_max; 1509 else 1510 maxiosize = vp->v_mount->mnt_iosize_max; 1511 1512 if (maxiosize > MAXPHYS) 1513 maxiosize = MAXPHYS; 1514 return (maxiosize); 1515 } 1516 1517 /* 1518 * Eliminate all activity associated with a vnode in preparation for 1519 * destruction. 1520 * 1521 * The vnode must be VX locked and refd and will remain VX locked and refd 1522 * on return. This routine may be called with the vnode in any state, as 1523 * long as it is VX locked. The vnode will be cleaned out and marked 1524 * VRECLAIMED but will not actually be reused until all existing refs and 1525 * holds go away. 1526 * 1527 * NOTE: This routine may be called on a vnode which has not yet been 1528 * already been deactivated (VOP_INACTIVE), or on a vnode which has 1529 * already been reclaimed. 1530 * 1531 * This routine is not responsible for placing us back on the freelist. 1532 * Instead, it happens automatically when the caller releases the VX lock 1533 * (assuming there aren't any other references). 1534 */ 1535 void 1536 vgone_vxlocked(struct vnode *vp) 1537 { 1538 /* 1539 * assert that the VX lock is held. This is an absolute requirement 1540 * now for vgone_vxlocked() to be called. 1541 */ 1542 KKASSERT(lockinuse(&vp->v_lock)); 1543 1544 /* 1545 * Clean out the filesystem specific data and set the VRECLAIMED 1546 * bit. Also deactivate the vnode if necessary. 1547 * 1548 * The vnode should have automatically been removed from the syncer 1549 * list as syncer/dirty flags cleared during the cleaning. 1550 */ 1551 vclean_vxlocked(vp, DOCLOSE); 1552 1553 /* 1554 * Normally panic if the vnode is still dirty, unless we are doing 1555 * a forced unmount (tmpfs typically). 1556 */ 1557 if (vp->v_flag & VONWORKLST) { 1558 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) { 1559 /* force removal */ 1560 vn_syncer_remove(vp, 1); 1561 } else { 1562 panic("vp %p still dirty in vgone after flush", vp); 1563 } 1564 } 1565 1566 /* 1567 * Delete from old mount point vnode list, if on one. 1568 */ 1569 if (vp->v_mount != NULL) { 1570 KKASSERT(vp->v_data == NULL); 1571 insmntque(vp, NULL); 1572 } 1573 1574 /* 1575 * If special device, remove it from special device alias list 1576 * if it is on one. This should normally only occur if a vnode is 1577 * being revoked as the device should otherwise have been released 1578 * naturally. 1579 */ 1580 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) { 1581 v_release_rdev(vp); 1582 } 1583 1584 /* 1585 * Set us to VBAD 1586 */ 1587 vp->v_type = VBAD; 1588 } 1589 1590 /* 1591 * Calculate the total number of references to a special device. This 1592 * routine may only be called for VBLK and VCHR vnodes since v_rdev is 1593 * an overloaded field. Since dev_from_devid() can now return NULL, we 1594 * have to check for a NULL v_rdev. 1595 */ 1596 int 1597 count_dev(cdev_t dev) 1598 { 1599 struct vnode *vp; 1600 int count = 0; 1601 1602 if (SLIST_FIRST(&dev->si_hlist)) { 1603 lwkt_gettoken(&spechash_token); 1604 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) { 1605 count += vp->v_opencount; 1606 } 1607 lwkt_reltoken(&spechash_token); 1608 } 1609 return(count); 1610 } 1611 1612 int 1613 vcount(struct vnode *vp) 1614 { 1615 if (vp->v_rdev == NULL) 1616 return(0); 1617 return(count_dev(vp->v_rdev)); 1618 } 1619 1620 /* 1621 * Initialize VMIO for a vnode. This routine MUST be called before a 1622 * VFS can issue buffer cache ops on a vnode. It is typically called 1623 * when a vnode is initialized from its inode. 1624 */ 1625 int 1626 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff) 1627 { 1628 vm_object_t object; 1629 int error = 0; 1630 1631 object = vp->v_object; 1632 if (object) { 1633 vm_object_hold(object); 1634 KKASSERT(vp->v_object == object); 1635 } 1636 1637 if (object == NULL) { 1638 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff); 1639 1640 /* 1641 * Dereference the reference we just created. This assumes 1642 * that the object is associated with the vp. Allow it to 1643 * have zero refs. It cannot be destroyed as long as it 1644 * is associated with the vnode. 1645 */ 1646 vm_object_hold(object); 1647 atomic_add_int(&object->ref_count, -1); 1648 vrele(vp); 1649 } else { 1650 KKASSERT((object->flags & OBJ_DEAD) == 0); 1651 } 1652 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object")); 1653 vsetflags(vp, VOBJBUF); 1654 vm_object_drop(object); 1655 1656 return (error); 1657 } 1658 1659 1660 /* 1661 * Print out a description of a vnode. 1662 */ 1663 static char *typename[] = 1664 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 1665 1666 void 1667 vprint(char *label, struct vnode *vp) 1668 { 1669 char buf[96]; 1670 1671 if (label != NULL) 1672 kprintf("%s: %p: ", label, (void *)vp); 1673 else 1674 kprintf("%p: ", (void *)vp); 1675 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,", 1676 typename[vp->v_type], 1677 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs); 1678 buf[0] = '\0'; 1679 if (vp->v_flag & VROOT) 1680 strcat(buf, "|VROOT"); 1681 if (vp->v_flag & VPFSROOT) 1682 strcat(buf, "|VPFSROOT"); 1683 if (vp->v_flag & VTEXT) 1684 strcat(buf, "|VTEXT"); 1685 if (vp->v_flag & VSYSTEM) 1686 strcat(buf, "|VSYSTEM"); 1687 if (vp->v_flag & VOBJBUF) 1688 strcat(buf, "|VOBJBUF"); 1689 if (buf[0] != '\0') 1690 kprintf(" flags (%s)", &buf[1]); 1691 if (vp->v_data == NULL) { 1692 kprintf("\n"); 1693 } else { 1694 kprintf("\n\t"); 1695 VOP_PRINT(vp); 1696 } 1697 } 1698 1699 /* 1700 * Do the usual access checking. 1701 * file_mode, uid and gid are from the vnode in question, 1702 * while acc_mode and cred are from the VOP_ACCESS parameter list 1703 */ 1704 int 1705 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 1706 mode_t acc_mode, struct ucred *cred) 1707 { 1708 mode_t mask; 1709 int ismember; 1710 1711 /* 1712 * Super-user always gets read/write access, but execute access depends 1713 * on at least one execute bit being set. 1714 */ 1715 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) { 1716 if ((acc_mode & VEXEC) && type != VDIR && 1717 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 1718 return (EACCES); 1719 return (0); 1720 } 1721 1722 mask = 0; 1723 1724 /* Otherwise, check the owner. */ 1725 if (cred->cr_uid == uid) { 1726 if (acc_mode & VEXEC) 1727 mask |= S_IXUSR; 1728 if (acc_mode & VREAD) 1729 mask |= S_IRUSR; 1730 if (acc_mode & VWRITE) 1731 mask |= S_IWUSR; 1732 return ((file_mode & mask) == mask ? 0 : EACCES); 1733 } 1734 1735 /* Otherwise, check the groups. */ 1736 ismember = groupmember(gid, cred); 1737 if (cred->cr_svgid == gid || ismember) { 1738 if (acc_mode & VEXEC) 1739 mask |= S_IXGRP; 1740 if (acc_mode & VREAD) 1741 mask |= S_IRGRP; 1742 if (acc_mode & VWRITE) 1743 mask |= S_IWGRP; 1744 return ((file_mode & mask) == mask ? 0 : EACCES); 1745 } 1746 1747 /* Otherwise, check everyone else. */ 1748 if (acc_mode & VEXEC) 1749 mask |= S_IXOTH; 1750 if (acc_mode & VREAD) 1751 mask |= S_IROTH; 1752 if (acc_mode & VWRITE) 1753 mask |= S_IWOTH; 1754 return ((file_mode & mask) == mask ? 0 : EACCES); 1755 } 1756 1757 #ifdef DDB 1758 #include <ddb/ddb.h> 1759 1760 static int db_show_locked_vnodes(struct mount *mp, void *data); 1761 1762 /* 1763 * List all of the locked vnodes in the system. 1764 * Called when debugging the kernel. 1765 */ 1766 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes) 1767 { 1768 kprintf("Locked vnodes\n"); 1769 mountlist_scan(db_show_locked_vnodes, NULL, 1770 MNTSCAN_FORWARD|MNTSCAN_NOBUSY); 1771 } 1772 1773 static int 1774 db_show_locked_vnodes(struct mount *mp, void *data __unused) 1775 { 1776 struct vnode *vp; 1777 1778 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 1779 if (vn_islocked(vp)) 1780 vprint(NULL, vp); 1781 } 1782 return(0); 1783 } 1784 #endif 1785 1786 /* 1787 * Top level filesystem related information gathering. 1788 */ 1789 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS); 1790 1791 static int 1792 vfs_sysctl(SYSCTL_HANDLER_ARGS) 1793 { 1794 int *name = (int *)arg1 - 1; /* XXX */ 1795 u_int namelen = arg2 + 1; /* XXX */ 1796 struct vfsconf *vfsp; 1797 int maxtypenum; 1798 1799 #if 1 || defined(COMPAT_PRELITE2) 1800 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 1801 if (namelen == 1) 1802 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 1803 #endif 1804 1805 #ifdef notyet 1806 /* all sysctl names at this level are at least name and field */ 1807 if (namelen < 2) 1808 return (ENOTDIR); /* overloaded */ 1809 if (name[0] != VFS_GENERIC) { 1810 vfsp = vfsconf_find_by_typenum(name[0]); 1811 if (vfsp == NULL) 1812 return (EOPNOTSUPP); 1813 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 1814 oldp, oldlenp, newp, newlen, p)); 1815 } 1816 #endif 1817 switch (name[1]) { 1818 case VFS_MAXTYPENUM: 1819 if (namelen != 2) 1820 return (ENOTDIR); 1821 maxtypenum = vfsconf_get_maxtypenum(); 1822 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum))); 1823 case VFS_CONF: 1824 if (namelen != 3) 1825 return (ENOTDIR); /* overloaded */ 1826 vfsp = vfsconf_find_by_typenum(name[2]); 1827 if (vfsp == NULL) 1828 return (EOPNOTSUPP); 1829 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp)); 1830 } 1831 return (EOPNOTSUPP); 1832 } 1833 1834 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl, 1835 "Generic filesystem"); 1836 1837 #if 1 || defined(COMPAT_PRELITE2) 1838 1839 static int 1840 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data) 1841 { 1842 int error; 1843 struct ovfsconf ovfs; 1844 struct sysctl_req *req = (struct sysctl_req*) data; 1845 1846 bzero(&ovfs, sizeof(ovfs)); 1847 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 1848 strcpy(ovfs.vfc_name, vfsp->vfc_name); 1849 ovfs.vfc_index = vfsp->vfc_typenum; 1850 ovfs.vfc_refcount = vfsp->vfc_refcount; 1851 ovfs.vfc_flags = vfsp->vfc_flags; 1852 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 1853 if (error) 1854 return error; /* abort iteration with error code */ 1855 else 1856 return 0; /* continue iterating with next element */ 1857 } 1858 1859 static int 1860 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 1861 { 1862 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req); 1863 } 1864 1865 #endif /* 1 || COMPAT_PRELITE2 */ 1866 1867 /* 1868 * Check to see if a filesystem is mounted on a block device. 1869 */ 1870 int 1871 vfs_mountedon(struct vnode *vp) 1872 { 1873 cdev_t dev; 1874 1875 dev = vp->v_rdev; 1876 if (dev != NULL && dev->si_mountpoint) 1877 return (EBUSY); 1878 return (0); 1879 } 1880 1881 /* 1882 * Unmount all filesystems. The list is traversed in reverse order 1883 * of mounting to avoid dependencies. 1884 * 1885 * We want the umountall to be able to break out of its loop if a 1886 * failure occurs, after scanning all possible mounts, so the callback 1887 * returns 0 on error. 1888 * 1889 * NOTE: Do not call mountlist_remove(mp) on error any more, this will 1890 * confuse mountlist_scan()'s unbusy check. 1891 */ 1892 static int vfs_umountall_callback(struct mount *mp, void *data); 1893 1894 void 1895 vfs_unmountall(int halting) 1896 { 1897 int count; 1898 1899 do { 1900 count = mountlist_scan(vfs_umountall_callback, &halting, 1901 MNTSCAN_REVERSE|MNTSCAN_NOBUSY); 1902 } while (count); 1903 } 1904 1905 static 1906 int 1907 vfs_umountall_callback(struct mount *mp, void *data) 1908 { 1909 int error; 1910 int halting = *(int *)data; 1911 1912 /* 1913 * NOTE: When halting, dounmount will disconnect but leave 1914 * certain mount points intact. e.g. devfs. 1915 */ 1916 error = dounmount(mp, MNT_FORCE, halting); 1917 if (error) { 1918 kprintf("unmount of filesystem mounted from %s failed (", 1919 mp->mnt_stat.f_mntfromname); 1920 if (error == EBUSY) 1921 kprintf("BUSY)\n"); 1922 else 1923 kprintf("%d)\n", error); 1924 return 0; 1925 } else { 1926 return 1; 1927 } 1928 } 1929 1930 /* 1931 * Checks the mount flags for parameter mp and put the names comma-separated 1932 * into a string buffer buf with a size limit specified by len. 1933 * 1934 * It returns the number of bytes written into buf, and (*errorp) will be 1935 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was 1936 * not large enough). The buffer will be 0-terminated if len was not 0. 1937 */ 1938 size_t 1939 vfs_flagstostr(int flags, const struct mountctl_opt *optp, 1940 char *buf, size_t len, int *errorp) 1941 { 1942 static const struct mountctl_opt optnames[] = { 1943 { MNT_RDONLY, "read-only" }, 1944 { MNT_SYNCHRONOUS, "synchronous" }, 1945 { MNT_NOEXEC, "noexec" }, 1946 { MNT_NOSUID, "nosuid" }, 1947 { MNT_NODEV, "nodev" }, 1948 { MNT_AUTOMOUNTED, "automounted" }, 1949 { MNT_ASYNC, "asynchronous" }, 1950 { MNT_SUIDDIR, "suiddir" }, 1951 { MNT_SOFTDEP, "soft-updates" }, 1952 { MNT_NOSYMFOLLOW, "nosymfollow" }, 1953 { MNT_TRIM, "trim" }, 1954 { MNT_NOATIME, "noatime" }, 1955 { MNT_NOCLUSTERR, "noclusterr" }, 1956 { MNT_NOCLUSTERW, "noclusterw" }, 1957 { MNT_EXRDONLY, "NFS read-only" }, 1958 { MNT_EXPORTED, "NFS exported" }, 1959 /* Remaining NFS flags could come here */ 1960 { MNT_LOCAL, "local" }, 1961 { MNT_QUOTA, "with-quotas" }, 1962 /* { MNT_ROOTFS, "rootfs" }, */ 1963 /* { MNT_IGNORE, "ignore" }, */ 1964 { 0, NULL} 1965 }; 1966 int bwritten; 1967 int bleft; 1968 int optlen; 1969 int actsize; 1970 1971 *errorp = 0; 1972 bwritten = 0; 1973 bleft = len - 1; /* leave room for trailing \0 */ 1974 1975 /* 1976 * Checks the size of the string. If it contains 1977 * any data, then we will append the new flags to 1978 * it. 1979 */ 1980 actsize = strlen(buf); 1981 if (actsize > 0) 1982 buf += actsize; 1983 1984 /* Default flags if no flags passed */ 1985 if (optp == NULL) 1986 optp = optnames; 1987 1988 if (bleft < 0) { /* degenerate case, 0-length buffer */ 1989 *errorp = EINVAL; 1990 return(0); 1991 } 1992 1993 for (; flags && optp->o_opt; ++optp) { 1994 if ((flags & optp->o_opt) == 0) 1995 continue; 1996 optlen = strlen(optp->o_name); 1997 if (bwritten || actsize > 0) { 1998 if (bleft < 2) { 1999 *errorp = ENOSPC; 2000 break; 2001 } 2002 buf[bwritten++] = ','; 2003 buf[bwritten++] = ' '; 2004 bleft -= 2; 2005 } 2006 if (bleft < optlen) { 2007 *errorp = ENOSPC; 2008 break; 2009 } 2010 bcopy(optp->o_name, buf + bwritten, optlen); 2011 bwritten += optlen; 2012 bleft -= optlen; 2013 flags &= ~optp->o_opt; 2014 } 2015 2016 /* 2017 * Space already reserved for trailing \0 2018 */ 2019 buf[bwritten] = 0; 2020 return (bwritten); 2021 } 2022 2023 /* 2024 * Build hash lists of net addresses and hang them off the mount point. 2025 * Called by ufs_mount() to set up the lists of export addresses. 2026 */ 2027 static int 2028 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 2029 const struct export_args *argp) 2030 { 2031 struct netcred *np; 2032 struct radix_node_head *rnh; 2033 int i; 2034 struct radix_node *rn; 2035 struct sockaddr *saddr, *smask = NULL; 2036 int error; 2037 2038 if (argp->ex_addrlen == 0) { 2039 if (mp->mnt_flag & MNT_DEFEXPORTED) 2040 return (EPERM); 2041 np = &nep->ne_defexported; 2042 np->netc_exflags = argp->ex_flags; 2043 np->netc_anon = argp->ex_anon; 2044 np->netc_anon.cr_ref = 1; 2045 mp->mnt_flag |= MNT_DEFEXPORTED; 2046 return (0); 2047 } 2048 2049 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN) 2050 return (EINVAL); 2051 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN) 2052 return (EINVAL); 2053 2054 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 2055 np = (struct netcred *)kmalloc(i, M_NETCRED, M_WAITOK | M_ZERO); 2056 saddr = (struct sockaddr *) (np + 1); 2057 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen))) 2058 goto out; 2059 if (saddr->sa_len > argp->ex_addrlen) 2060 saddr->sa_len = argp->ex_addrlen; 2061 if (argp->ex_masklen) { 2062 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 2063 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen); 2064 if (error) 2065 goto out; 2066 if (smask->sa_len > argp->ex_masklen) 2067 smask->sa_len = argp->ex_masklen; 2068 } 2069 NE_LOCK(nep); 2070 if (nep->ne_maskhead == NULL) { 2071 if (!rn_inithead((void **)&nep->ne_maskhead, NULL, 0)) { 2072 error = ENOBUFS; 2073 goto out; 2074 } 2075 } 2076 if ((rnh = vfs_create_addrlist_af(saddr->sa_family, nep)) == NULL) { 2077 error = ENOBUFS; 2078 goto out; 2079 } 2080 rn = (*rnh->rnh_addaddr)((char *)saddr, (char *)smask, rnh, 2081 np->netc_rnodes); 2082 NE_UNLOCK(nep); 2083 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ 2084 error = EPERM; 2085 goto out; 2086 } 2087 np->netc_exflags = argp->ex_flags; 2088 np->netc_anon = argp->ex_anon; 2089 np->netc_anon.cr_ref = 1; 2090 return (0); 2091 2092 out: 2093 kfree(np, M_NETCRED); 2094 return (error); 2095 } 2096 2097 /* 2098 * Free netcred structures installed in the netexport 2099 */ 2100 static int 2101 vfs_free_netcred(struct radix_node *rn, void *w) 2102 { 2103 struct radix_node_head *rnh = (struct radix_node_head *)w; 2104 2105 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 2106 kfree(rn, M_NETCRED); 2107 2108 return (0); 2109 } 2110 2111 /* 2112 * callback to free an element of the mask table installed in the 2113 * netexport. These may be created indirectly and are not netcred 2114 * structures. 2115 */ 2116 static int 2117 vfs_free_netcred_mask(struct radix_node *rn, void *w) 2118 { 2119 struct radix_node_head *rnh = (struct radix_node_head *)w; 2120 2121 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 2122 kfree(rn, M_RTABLE); 2123 2124 return (0); 2125 } 2126 2127 static struct radix_node_head * 2128 vfs_create_addrlist_af(int af, struct netexport *nep) 2129 { 2130 struct radix_node_head *rnh = NULL; 2131 #if defined(INET) || defined(INET6) 2132 struct radix_node_head *maskhead = nep->ne_maskhead; 2133 int off; 2134 #endif 2135 2136 NE_ASSERT_LOCKED(nep); 2137 #if defined(INET) || defined(INET6) 2138 KKASSERT(maskhead != NULL); 2139 #endif 2140 switch (af) { 2141 #ifdef INET 2142 case AF_INET: 2143 if ((rnh = nep->ne_inethead) == NULL) { 2144 off = offsetof(struct sockaddr_in, sin_addr) << 3; 2145 if (!rn_inithead((void **)&rnh, maskhead, off)) 2146 return (NULL); 2147 nep->ne_inethead = rnh; 2148 } 2149 break; 2150 #endif 2151 #ifdef INET6 2152 case AF_INET6: 2153 if ((rnh = nep->ne_inet6head) == NULL) { 2154 off = offsetof(struct sockaddr_in6, sin6_addr) << 3; 2155 if (!rn_inithead((void **)&rnh, maskhead, off)) 2156 return (NULL); 2157 nep->ne_inet6head = rnh; 2158 } 2159 break; 2160 #endif 2161 } 2162 return (rnh); 2163 } 2164 2165 /* 2166 * helper function for freeing netcred elements 2167 */ 2168 static void 2169 vfs_free_addrlist_af(struct radix_node_head **prnh) 2170 { 2171 struct radix_node_head *rnh = *prnh; 2172 2173 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh); 2174 kfree(rnh, M_RTABLE); 2175 *prnh = NULL; 2176 } 2177 2178 /* 2179 * helper function for freeing mask elements 2180 */ 2181 static void 2182 vfs_free_addrlist_masks(struct radix_node_head **prnh) 2183 { 2184 struct radix_node_head *rnh = *prnh; 2185 2186 (*rnh->rnh_walktree) (rnh, vfs_free_netcred_mask, rnh); 2187 kfree(rnh, M_RTABLE); 2188 *prnh = NULL; 2189 } 2190 2191 /* 2192 * Free the net address hash lists that are hanging off the mount points. 2193 */ 2194 static void 2195 vfs_free_addrlist(struct netexport *nep) 2196 { 2197 NE_LOCK(nep); 2198 if (nep->ne_inethead != NULL) 2199 vfs_free_addrlist_af(&nep->ne_inethead); 2200 if (nep->ne_inet6head != NULL) 2201 vfs_free_addrlist_af(&nep->ne_inet6head); 2202 if (nep->ne_maskhead) 2203 vfs_free_addrlist_masks(&nep->ne_maskhead); 2204 NE_UNLOCK(nep); 2205 } 2206 2207 int 2208 vfs_export(struct mount *mp, struct netexport *nep, 2209 const struct export_args *argp) 2210 { 2211 int error; 2212 2213 if (argp->ex_flags & MNT_DELEXPORT) { 2214 if (mp->mnt_flag & MNT_EXPUBLIC) { 2215 vfs_setpublicfs(NULL, NULL, NULL); 2216 mp->mnt_flag &= ~MNT_EXPUBLIC; 2217 } 2218 vfs_free_addrlist(nep); 2219 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 2220 } 2221 if (argp->ex_flags & MNT_EXPORTED) { 2222 if (argp->ex_flags & MNT_EXPUBLIC) { 2223 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 2224 return (error); 2225 mp->mnt_flag |= MNT_EXPUBLIC; 2226 } 2227 if ((error = vfs_hang_addrlist(mp, nep, argp))) 2228 return (error); 2229 mp->mnt_flag |= MNT_EXPORTED; 2230 } 2231 return (0); 2232 } 2233 2234 2235 /* 2236 * Set the publicly exported filesystem (WebNFS). Currently, only 2237 * one public filesystem is possible in the spec (RFC 2054 and 2055) 2238 */ 2239 int 2240 vfs_setpublicfs(struct mount *mp, struct netexport *nep, 2241 const struct export_args *argp) 2242 { 2243 int error; 2244 struct vnode *rvp; 2245 char *cp; 2246 2247 /* 2248 * mp == NULL -> invalidate the current info, the FS is 2249 * no longer exported. May be called from either vfs_export 2250 * or unmount, so check if it hasn't already been done. 2251 */ 2252 if (mp == NULL) { 2253 if (nfs_pub.np_valid) { 2254 nfs_pub.np_valid = 0; 2255 if (nfs_pub.np_index != NULL) { 2256 kfree(nfs_pub.np_index, M_TEMP); 2257 nfs_pub.np_index = NULL; 2258 } 2259 } 2260 return (0); 2261 } 2262 2263 /* 2264 * Only one allowed at a time. 2265 */ 2266 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 2267 return (EBUSY); 2268 2269 /* 2270 * Get real filehandle for root of exported FS. 2271 */ 2272 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 2273 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 2274 2275 if ((error = VFS_ROOT(mp, &rvp))) 2276 return (error); 2277 2278 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 2279 return (error); 2280 2281 vput(rvp); 2282 2283 /* 2284 * If an indexfile was specified, pull it in. 2285 */ 2286 if (argp->ex_indexfile != NULL) { 2287 int namelen; 2288 2289 error = vn_get_namelen(rvp, &namelen); 2290 if (error) 2291 return (error); 2292 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK); 2293 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 2294 namelen, NULL); 2295 if (!error) { 2296 /* 2297 * Check for illegal filenames. 2298 */ 2299 for (cp = nfs_pub.np_index; *cp; cp++) { 2300 if (*cp == '/') { 2301 error = EINVAL; 2302 break; 2303 } 2304 } 2305 } 2306 if (error) { 2307 kfree(nfs_pub.np_index, M_TEMP); 2308 return (error); 2309 } 2310 } 2311 2312 nfs_pub.np_mount = mp; 2313 nfs_pub.np_valid = 1; 2314 return (0); 2315 } 2316 2317 struct netcred * 2318 vfs_export_lookup(struct mount *mp, struct netexport *nep, 2319 struct sockaddr *nam) 2320 { 2321 struct netcred *np; 2322 struct radix_node_head *rnh; 2323 struct sockaddr *saddr; 2324 2325 np = NULL; 2326 if (mp->mnt_flag & MNT_EXPORTED) { 2327 /* 2328 * Lookup in the export list first. 2329 */ 2330 NE_LOCK(nep); 2331 if (nam != NULL) { 2332 saddr = nam; 2333 switch (saddr->sa_family) { 2334 #ifdef INET 2335 case AF_INET: 2336 rnh = nep->ne_inethead; 2337 break; 2338 #endif 2339 #ifdef INET6 2340 case AF_INET6: 2341 rnh = nep->ne_inet6head; 2342 break; 2343 #endif 2344 default: 2345 rnh = NULL; 2346 } 2347 if (rnh != NULL) { 2348 np = (struct netcred *) 2349 (*rnh->rnh_matchaddr)((char *)saddr, 2350 rnh); 2351 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 2352 np = NULL; 2353 } 2354 } 2355 NE_UNLOCK(nep); 2356 /* 2357 * If no address match, use the default if it exists. 2358 */ 2359 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 2360 np = &nep->ne_defexported; 2361 } 2362 return (np); 2363 } 2364 2365 /* 2366 * perform msync on all vnodes under a mount point. The mount point must 2367 * be locked. This code is also responsible for lazy-freeing unreferenced 2368 * vnodes whos VM objects no longer contain pages. 2369 * 2370 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state. 2371 * 2372 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked, 2373 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it 2374 * way up in this high level function. 2375 */ 2376 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data); 2377 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data); 2378 2379 void 2380 vfs_msync(struct mount *mp, int flags) 2381 { 2382 int vmsc_flags; 2383 2384 /* 2385 * tmpfs sets this flag to prevent msync(), sync, and the 2386 * filesystem periodic syncer from trying to flush VM pages 2387 * to swap. Only pure memory pressure flushes tmpfs VM pages 2388 * to swap. 2389 */ 2390 if (mp->mnt_kern_flag & MNTK_NOMSYNC) 2391 return; 2392 2393 /* 2394 * Ok, scan the vnodes for work. If the filesystem is using the 2395 * syncer thread feature we can use vsyncscan() instead of 2396 * vmntvnodescan(), which is much faster. 2397 */ 2398 vmsc_flags = VMSC_GETVP; 2399 if (flags != MNT_WAIT) 2400 vmsc_flags |= VMSC_NOWAIT; 2401 2402 if (mp->mnt_kern_flag & MNTK_THR_SYNC) { 2403 vsyncscan(mp, vmsc_flags, vfs_msync_scan2, 2404 (void *)(intptr_t)flags); 2405 } else { 2406 vmntvnodescan(mp, vmsc_flags, 2407 vfs_msync_scan1, vfs_msync_scan2, 2408 (void *)(intptr_t)flags); 2409 } 2410 } 2411 2412 /* 2413 * scan1 is a fast pre-check. There could be hundreds of thousands of 2414 * vnodes, we cannot afford to do anything heavy weight until we have a 2415 * fairly good indication that there is work to do. 2416 * 2417 * The new namecache holds the vnode for each v_namecache association 2418 * so allow these refs. 2419 */ 2420 static 2421 int 2422 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data) 2423 { 2424 int flags = (int)(intptr_t)data; 2425 2426 if ((vp->v_flag & VRECLAIMED) == 0) { 2427 if (vp->v_auxrefs == vp->v_namecache_count && 2428 VREFCNT(vp) <= 0 && vp->v_object) { 2429 return(0); /* call scan2 */ 2430 } 2431 if ((mp->mnt_flag & MNT_RDONLY) == 0 && 2432 (vp->v_flag & VOBJDIRTY) && 2433 (flags == MNT_WAIT || vn_islocked(vp) == 0)) { 2434 return(0); /* call scan2 */ 2435 } 2436 } 2437 2438 /* 2439 * do not call scan2, continue the loop 2440 */ 2441 return(-1); 2442 } 2443 2444 /* 2445 * This callback is handed a locked vnode. 2446 */ 2447 static 2448 int 2449 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data) 2450 { 2451 vm_object_t obj; 2452 int flags = (int)(intptr_t)data; 2453 int opcflags; 2454 2455 if (vp->v_flag & VRECLAIMED) 2456 return(0); 2457 2458 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) { 2459 if ((obj = vp->v_object) != NULL) { 2460 if (flags == MNT_WAIT) { 2461 /* 2462 * VFS_MSYNC is called with MNT_WAIT when 2463 * unmounting. 2464 */ 2465 opcflags = OBJPC_SYNC; 2466 } else if (vp->v_writecount || obj->ref_count) { 2467 /* 2468 * VFS_MSYNC is otherwise called via the 2469 * periodic filesystem sync or the 'sync' 2470 * command. Honor MADV_NOSYNC / MAP_NOSYNC 2471 * if the file is open for writing or memory 2472 * mapped. Pages flagged PG_NOSYNC will not 2473 * be automatically flushed at this time. 2474 * 2475 * The obj->ref_count test is not perfect 2476 * since temporary refs may be present, but 2477 * the periodic filesystem sync will ultimately 2478 * catch it if the file is not open and not 2479 * mapped. 2480 */ 2481 opcflags = OBJPC_NOSYNC; 2482 } else { 2483 /* 2484 * If the file is no longer open for writing 2485 * and also no longer mapped, do not honor 2486 * MAP_NOSYNC. That is, fully synchronize 2487 * the file. 2488 * 2489 * This still occurs on the periodic fs sync, 2490 * so frontend programs which turn the file 2491 * over quickly enough can still avoid the 2492 * sync, but ultimately we do want to flush 2493 * even MADV_NOSYNC pages once it is no longer 2494 * mapped or open for writing. 2495 */ 2496 opcflags = 0; 2497 } 2498 vm_object_page_clean(obj, 0, 0, opcflags); 2499 } 2500 } 2501 return(0); 2502 } 2503 2504 /* 2505 * Wake up anyone interested in vp because it is being revoked. 2506 */ 2507 void 2508 vn_gone(struct vnode *vp) 2509 { 2510 lwkt_gettoken(&vp->v_token); 2511 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE); 2512 lwkt_reltoken(&vp->v_token); 2513 } 2514 2515 /* 2516 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened 2517 * (or v_rdev might be NULL). 2518 */ 2519 cdev_t 2520 vn_todev(struct vnode *vp) 2521 { 2522 if (vp->v_type != VBLK && vp->v_type != VCHR) 2523 return (NULL); 2524 KKASSERT(vp->v_rdev != NULL); 2525 return (vp->v_rdev); 2526 } 2527 2528 /* 2529 * Check if vnode represents a disk device. The vnode does not need to be 2530 * opened. 2531 * 2532 * MPALMOSTSAFE 2533 */ 2534 int 2535 vn_isdisk(struct vnode *vp, int *errp) 2536 { 2537 cdev_t dev; 2538 2539 if (vp->v_type != VCHR) { 2540 if (errp != NULL) 2541 *errp = ENOTBLK; 2542 return (0); 2543 } 2544 2545 dev = vp->v_rdev; 2546 2547 if (dev == NULL) { 2548 if (errp != NULL) 2549 *errp = ENXIO; 2550 return (0); 2551 } 2552 if (dev_is_good(dev) == 0) { 2553 if (errp != NULL) 2554 *errp = ENXIO; 2555 return (0); 2556 } 2557 if ((dev_dflags(dev) & D_DISK) == 0) { 2558 if (errp != NULL) 2559 *errp = ENOTBLK; 2560 return (0); 2561 } 2562 if (errp != NULL) 2563 *errp = 0; 2564 return (1); 2565 } 2566 2567 int 2568 vn_get_namelen(struct vnode *vp, int *namelen) 2569 { 2570 int error; 2571 register_t retval[2]; 2572 2573 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval); 2574 if (error) 2575 return (error); 2576 *namelen = (int)retval[0]; 2577 return (0); 2578 } 2579 2580 int 2581 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type, 2582 uint16_t d_namlen, const char *d_name) 2583 { 2584 struct dirent *dp; 2585 size_t len; 2586 2587 len = _DIRENT_RECLEN(d_namlen); 2588 if (len > uio->uio_resid) 2589 return(1); 2590 2591 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO); 2592 2593 dp->d_ino = d_ino; 2594 dp->d_namlen = d_namlen; 2595 dp->d_type = d_type; 2596 bcopy(d_name, dp->d_name, d_namlen); 2597 2598 *error = uiomove((caddr_t)dp, len, uio); 2599 2600 kfree(dp, M_TEMP); 2601 2602 return(0); 2603 } 2604 2605 void 2606 vn_mark_atime(struct vnode *vp, struct thread *td) 2607 { 2608 struct proc *p = td->td_proc; 2609 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred; 2610 2611 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) { 2612 VOP_MARKATIME(vp, cred); 2613 } 2614 } 2615 2616 /* 2617 * Calculate the number of entries in an inode-related chained hash table. 2618 * With today's memory sizes, maxvnodes can wind up being a very large 2619 * number. There is no reason to waste memory, so tolerate some stacking. 2620 */ 2621 int 2622 vfs_inodehashsize(void) 2623 { 2624 int hsize; 2625 2626 hsize = 32; 2627 while (hsize < maxvnodes) 2628 hsize <<= 1; 2629 while (hsize > maxvnodes * 2) 2630 hsize >>= 1; /* nominal 2x stacking */ 2631 2632 if (maxvnodes > 1024 * 1024) 2633 hsize >>= 1; /* nominal 8x stacking */ 2634 2635 if (maxvnodes > 128 * 1024) 2636 hsize >>= 1; /* nominal 4x stacking */ 2637 2638 if (hsize < 16) 2639 hsize = 16; 2640 2641 return hsize; 2642 } 2643 2644 union _qcvt { 2645 quad_t qcvt; 2646 int32_t val[2]; 2647 }; 2648 2649 #define SETHIGH(q, h) { \ 2650 union _qcvt tmp; \ 2651 tmp.qcvt = (q); \ 2652 tmp.val[_QUAD_HIGHWORD] = (h); \ 2653 (q) = tmp.qcvt; \ 2654 } 2655 #define SETLOW(q, l) { \ 2656 union _qcvt tmp; \ 2657 tmp.qcvt = (q); \ 2658 tmp.val[_QUAD_LOWWORD] = (l); \ 2659 (q) = tmp.qcvt; \ 2660 } 2661 2662 u_quad_t 2663 init_va_filerev(void) 2664 { 2665 struct timeval tv; 2666 u_quad_t ret = 0; 2667 2668 getmicrouptime(&tv); 2669 SETHIGH(ret, tv.tv_sec); 2670 SETLOW(ret, tv.tv_usec * 4294); 2671 2672 return ret; 2673 } 2674 2675 /* 2676 * Set default timestamp_precision. If hz is reasonably high we go for 2677 * performance and limit vfs timestamps to microseconds with tick resolution. 2678 * If hz is too low, however, we lose a bit of performance to get a more 2679 * precise timestamp, because the mtime/ctime granularity might just be too 2680 * rough otherwise (for make and Makefile's, for example). 2681 */ 2682 static void 2683 vfs_ts_prec_init(void *dummy) 2684 { 2685 if (timestamp_precision < 0) { 2686 if (hz >= 100) 2687 timestamp_precision = TSP_USEC; 2688 else 2689 timestamp_precision = TSP_USEC_PRECISE; 2690 } 2691 } 2692 SYSINIT(vfs_ts_prec_init, SI_SUB_VFS, SI_ORDER_ANY, vfs_ts_prec_init, NULL); 2693