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