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