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