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: NO B_LOCKED CHECK. Also no buf_checkwrite() 405 * check. This code will write out the buffer, period. 406 */ 407 bremfree(bp); 408 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 409 (info->flags & V_SAVE)) { 410 cluster_awrite(bp); 411 } else if (info->flags & V_SAVE) { 412 /* 413 * Cannot set B_NOCACHE on a clean buffer as this will 414 * destroy the VM backing store which might actually 415 * be dirty (and unsynchronized). 416 */ 417 bp->b_flags |= (B_INVAL | B_RELBUF); 418 brelse(bp); 419 } else { 420 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 421 brelse(bp); 422 } 423 return(0); 424 } 425 426 /* 427 * Truncate a file's buffer and pages to a specified length. This 428 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 429 * sync activity. 430 * 431 * The vnode must be locked. 432 */ 433 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data); 434 static int vtruncbuf_bp_trunc(struct buf *bp, void *data); 435 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data); 436 static int vtruncbuf_bp_metasync(struct buf *bp, void *data); 437 438 struct vtruncbuf_info { 439 struct vnode *vp; 440 off_t truncloffset; 441 int clean; 442 }; 443 444 int 445 vtruncbuf(struct vnode *vp, off_t length, int blksize) 446 { 447 struct vtruncbuf_info info; 448 const char *filename; 449 int count; 450 451 /* 452 * Round up to the *next* block, then destroy the buffers in question. 453 * Since we are only removing some of the buffers we must rely on the 454 * scan count to determine whether a loop is necessary. 455 */ 456 if ((count = (int)(length % blksize)) != 0) 457 info.truncloffset = length + (blksize - count); 458 else 459 info.truncloffset = length; 460 info.vp = vp; 461 462 lwkt_gettoken(&vp->v_token); 463 do { 464 info.clean = 1; 465 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 466 vtruncbuf_bp_trunc_cmp, 467 vtruncbuf_bp_trunc, &info); 468 info.clean = 0; 469 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 470 vtruncbuf_bp_trunc_cmp, 471 vtruncbuf_bp_trunc, &info); 472 } while(count); 473 474 /* 475 * For safety, fsync any remaining metadata if the file is not being 476 * truncated to 0. Since the metadata does not represent the entire 477 * dirty list we have to rely on the hit count to ensure that we get 478 * all of it. 479 */ 480 if (length > 0) { 481 do { 482 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 483 vtruncbuf_bp_metasync_cmp, 484 vtruncbuf_bp_metasync, &info); 485 } while (count); 486 } 487 488 /* 489 * Clean out any left over VM backing store. 490 * 491 * It is possible to have in-progress I/O from buffers that were 492 * not part of the truncation. This should not happen if we 493 * are truncating to 0-length. 494 */ 495 vnode_pager_setsize(vp, length); 496 bio_track_wait(&vp->v_track_write, 0, 0); 497 498 /* 499 * Debugging only 500 */ 501 spin_lock(&vp->v_spin); 502 filename = TAILQ_FIRST(&vp->v_namecache) ? 503 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?"; 504 spin_unlock(&vp->v_spin); 505 506 /* 507 * Make sure no buffers were instantiated while we were trying 508 * to clean out the remaining VM pages. This could occur due 509 * to busy dirty VM pages being flushed out to disk. 510 */ 511 do { 512 info.clean = 1; 513 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 514 vtruncbuf_bp_trunc_cmp, 515 vtruncbuf_bp_trunc, &info); 516 info.clean = 0; 517 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 518 vtruncbuf_bp_trunc_cmp, 519 vtruncbuf_bp_trunc, &info); 520 if (count) { 521 kprintf("Warning: vtruncbuf(): Had to re-clean %d " 522 "left over buffers in %s\n", count, filename); 523 } 524 } while(count); 525 526 lwkt_reltoken(&vp->v_token); 527 528 return (0); 529 } 530 531 /* 532 * The callback buffer is beyond the new file EOF and must be destroyed. 533 * Note that the compare function must conform to the RB_SCAN's requirements. 534 */ 535 static 536 int 537 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data) 538 { 539 struct vtruncbuf_info *info = data; 540 541 if (bp->b_loffset >= info->truncloffset) 542 return(0); 543 return(-1); 544 } 545 546 static 547 int 548 vtruncbuf_bp_trunc(struct buf *bp, void *data) 549 { 550 struct vtruncbuf_info *info = data; 551 552 /* 553 * Do not try to use a buffer we cannot immediately lock, but sleep 554 * anyway to prevent a livelock. The code will loop until all buffers 555 * can be acted upon. 556 * 557 * We must always revalidate the buffer after locking it to deal 558 * with MP races. 559 */ 560 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 561 atomic_add_int(&bp->b_refs, 1); 562 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) 563 BUF_UNLOCK(bp); 564 atomic_subtract_int(&bp->b_refs, 1); 565 } else if ((info->clean && (bp->b_flags & B_DELWRI)) || 566 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) || 567 bp->b_vp != info->vp || 568 vtruncbuf_bp_trunc_cmp(bp, data)) { 569 BUF_UNLOCK(bp); 570 } else { 571 bremfree(bp); 572 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE); 573 brelse(bp); 574 } 575 return(1); 576 } 577 578 /* 579 * Fsync all meta-data after truncating a file to be non-zero. Only metadata 580 * blocks (with a negative loffset) are scanned. 581 * Note that the compare function must conform to the RB_SCAN's requirements. 582 */ 583 static int 584 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused) 585 { 586 if (bp->b_loffset < 0) 587 return(0); 588 return(1); 589 } 590 591 static int 592 vtruncbuf_bp_metasync(struct buf *bp, void *data) 593 { 594 struct vtruncbuf_info *info = data; 595 596 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 597 atomic_add_int(&bp->b_refs, 1); 598 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) 599 BUF_UNLOCK(bp); 600 atomic_subtract_int(&bp->b_refs, 1); 601 } else if ((bp->b_flags & B_DELWRI) == 0 || 602 bp->b_vp != info->vp || 603 vtruncbuf_bp_metasync_cmp(bp, data)) { 604 BUF_UNLOCK(bp); 605 } else { 606 bremfree(bp); 607 if (bp->b_vp == info->vp) 608 bawrite(bp); 609 else 610 bwrite(bp); 611 } 612 return(1); 613 } 614 615 /* 616 * vfsync - implements a multipass fsync on a file which understands 617 * dependancies and meta-data. The passed vnode must be locked. The 618 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY. 619 * 620 * When fsyncing data asynchronously just do one consolidated pass starting 621 * with the most negative block number. This may not get all the data due 622 * to dependancies. 623 * 624 * When fsyncing data synchronously do a data pass, then a metadata pass, 625 * then do additional data+metadata passes to try to get all the data out. 626 */ 627 static int vfsync_wait_output(struct vnode *vp, 628 int (*waitoutput)(struct vnode *, struct thread *)); 629 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused); 630 static int vfsync_data_only_cmp(struct buf *bp, void *data); 631 static int vfsync_meta_only_cmp(struct buf *bp, void *data); 632 static int vfsync_lazy_range_cmp(struct buf *bp, void *data); 633 static int vfsync_bp(struct buf *bp, void *data); 634 635 struct vfsync_info { 636 struct vnode *vp; 637 int synchronous; 638 int syncdeps; 639 int lazycount; 640 int lazylimit; 641 int skippedbufs; 642 int (*checkdef)(struct buf *); 643 int (*cmpfunc)(struct buf *, void *); 644 }; 645 646 int 647 vfsync(struct vnode *vp, int waitfor, int passes, 648 int (*checkdef)(struct buf *), 649 int (*waitoutput)(struct vnode *, struct thread *)) 650 { 651 struct vfsync_info info; 652 int error; 653 654 bzero(&info, sizeof(info)); 655 info.vp = vp; 656 if ((info.checkdef = checkdef) == NULL) 657 info.syncdeps = 1; 658 659 lwkt_gettoken(&vp->v_token); 660 661 switch(waitfor) { 662 case MNT_LAZY | MNT_NOWAIT: 663 case MNT_LAZY: 664 /* 665 * Lazy (filesystem syncer typ) Asynchronous plus limit the 666 * number of data (not meta) pages we try to flush to 1MB. 667 * A non-zero return means that lazy limit was reached. 668 */ 669 info.lazylimit = 1024 * 1024; 670 info.syncdeps = 1; 671 info.cmpfunc = vfsync_lazy_range_cmp; 672 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 673 vfsync_lazy_range_cmp, vfsync_bp, &info); 674 info.cmpfunc = vfsync_meta_only_cmp; 675 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 676 vfsync_meta_only_cmp, vfsync_bp, &info); 677 if (error == 0) 678 vp->v_lazyw = 0; 679 else if (!RB_EMPTY(&vp->v_rbdirty_tree)) 680 vn_syncer_add(vp, 1); 681 error = 0; 682 break; 683 case MNT_NOWAIT: 684 /* 685 * Asynchronous. Do a data-only pass and a meta-only pass. 686 */ 687 info.syncdeps = 1; 688 info.cmpfunc = vfsync_data_only_cmp; 689 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, 690 vfsync_bp, &info); 691 info.cmpfunc = vfsync_meta_only_cmp; 692 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp, 693 vfsync_bp, &info); 694 error = 0; 695 break; 696 default: 697 /* 698 * Synchronous. Do a data-only pass, then a meta-data+data 699 * pass, then additional integrated passes to try to get 700 * all the dependancies flushed. 701 */ 702 info.cmpfunc = vfsync_data_only_cmp; 703 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, 704 vfsync_bp, &info); 705 error = vfsync_wait_output(vp, waitoutput); 706 if (error == 0) { 707 info.skippedbufs = 0; 708 info.cmpfunc = vfsync_dummy_cmp; 709 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 710 vfsync_bp, &info); 711 error = vfsync_wait_output(vp, waitoutput); 712 if (info.skippedbufs) { 713 kprintf("Warning: vfsync skipped %d dirty " 714 "bufs in pass2!\n", info.skippedbufs); 715 } 716 } 717 while (error == 0 && passes > 0 && 718 !RB_EMPTY(&vp->v_rbdirty_tree) 719 ) { 720 if (--passes == 0) { 721 info.synchronous = 1; 722 info.syncdeps = 1; 723 } 724 info.cmpfunc = vfsync_dummy_cmp; 725 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 726 vfsync_bp, &info); 727 if (error < 0) 728 error = -error; 729 info.syncdeps = 1; 730 if (error == 0) 731 error = vfsync_wait_output(vp, waitoutput); 732 } 733 break; 734 } 735 lwkt_reltoken(&vp->v_token); 736 return(error); 737 } 738 739 static int 740 vfsync_wait_output(struct vnode *vp, 741 int (*waitoutput)(struct vnode *, struct thread *)) 742 { 743 int error; 744 745 error = bio_track_wait(&vp->v_track_write, 0, 0); 746 if (waitoutput) 747 error = waitoutput(vp, curthread); 748 return(error); 749 } 750 751 static int 752 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused) 753 { 754 return(0); 755 } 756 757 static int 758 vfsync_data_only_cmp(struct buf *bp, void *data) 759 { 760 if (bp->b_loffset < 0) 761 return(-1); 762 return(0); 763 } 764 765 static int 766 vfsync_meta_only_cmp(struct buf *bp, void *data) 767 { 768 if (bp->b_loffset < 0) 769 return(0); 770 return(1); 771 } 772 773 static int 774 vfsync_lazy_range_cmp(struct buf *bp, void *data) 775 { 776 struct vfsync_info *info = data; 777 778 if (bp->b_loffset < info->vp->v_lazyw) 779 return(-1); 780 return(0); 781 } 782 783 static int 784 vfsync_bp(struct buf *bp, void *data) 785 { 786 struct vfsync_info *info = data; 787 struct vnode *vp = info->vp; 788 int error; 789 790 /* 791 * Ignore buffers that we cannot immediately lock. 792 */ 793 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 794 ++info->skippedbufs; 795 return(0); 796 } 797 798 /* 799 * We must revalidate the buffer after locking. 800 */ 801 if ((bp->b_flags & B_DELWRI) == 0 || 802 bp->b_vp != info->vp || 803 info->cmpfunc(bp, data)) { 804 BUF_UNLOCK(bp); 805 return(0); 806 } 807 808 /* 809 * If syncdeps is not set we do not try to write buffers which have 810 * dependancies. 811 */ 812 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) { 813 BUF_UNLOCK(bp); 814 return(0); 815 } 816 817 /* 818 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer 819 * has been written but an additional handshake with the device 820 * is required before we can dispose of the buffer. We have no idea 821 * how to do this so we have to skip these buffers. 822 */ 823 if (bp->b_flags & B_NEEDCOMMIT) { 824 BUF_UNLOCK(bp); 825 return(0); 826 } 827 828 /* 829 * Ask bioops if it is ok to sync. If not the VFS may have 830 * set B_LOCKED so we have to cycle the buffer. 831 */ 832 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) { 833 bremfree(bp); 834 brelse(bp); 835 return(0); 836 } 837 838 if (info->synchronous) { 839 /* 840 * Synchronous flushing. An error may be returned. 841 */ 842 bremfree(bp); 843 error = bwrite(bp); 844 } else { 845 /* 846 * Asynchronous flushing. A negative return value simply 847 * stops the scan and is not considered an error. We use 848 * this to support limited MNT_LAZY flushes. 849 */ 850 vp->v_lazyw = bp->b_loffset; 851 bremfree(bp); 852 info->lazycount += cluster_awrite(bp); 853 waitrunningbufspace(); 854 vm_wait_nominal(); 855 if (info->lazylimit && info->lazycount >= info->lazylimit) 856 error = 1; 857 else 858 error = 0; 859 } 860 return(-error); 861 } 862 863 /* 864 * Associate a buffer with a vnode. 865 * 866 * MPSAFE 867 */ 868 int 869 bgetvp(struct vnode *vp, struct buf *bp, int testsize) 870 { 871 KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 872 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0); 873 874 /* 875 * Insert onto list for new vnode. 876 */ 877 lwkt_gettoken(&vp->v_token); 878 879 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) { 880 lwkt_reltoken(&vp->v_token); 881 return (EEXIST); 882 } 883 884 /* 885 * Diagnostics (mainly for HAMMER debugging). Check for 886 * overlapping buffers. 887 */ 888 if (check_buf_overlap) { 889 struct buf *bx; 890 bx = buf_rb_hash_RB_PREV(bp); 891 if (bx) { 892 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) { 893 kprintf("bgetvp: overlapl %016jx/%d %016jx " 894 "bx %p bp %p\n", 895 (intmax_t)bx->b_loffset, 896 bx->b_bufsize, 897 (intmax_t)bp->b_loffset, 898 bx, bp); 899 if (check_buf_overlap > 1) 900 panic("bgetvp - overlapping buffer"); 901 } 902 } 903 bx = buf_rb_hash_RB_NEXT(bp); 904 if (bx) { 905 if (bp->b_loffset + testsize > bx->b_loffset) { 906 kprintf("bgetvp: overlapr %016jx/%d %016jx " 907 "bp %p bx %p\n", 908 (intmax_t)bp->b_loffset, 909 testsize, 910 (intmax_t)bx->b_loffset, 911 bp, bx); 912 if (check_buf_overlap > 1) 913 panic("bgetvp - overlapping buffer"); 914 } 915 } 916 } 917 bp->b_vp = vp; 918 bp->b_flags |= B_HASHED; 919 bp->b_flags |= B_VNCLEAN; 920 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) 921 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp); 922 vhold(vp); 923 lwkt_reltoken(&vp->v_token); 924 return(0); 925 } 926 927 /* 928 * Disassociate a buffer from a vnode. 929 * 930 * MPSAFE 931 */ 932 void 933 brelvp(struct buf *bp) 934 { 935 struct vnode *vp; 936 937 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 938 939 /* 940 * Delete from old vnode list, if on one. 941 */ 942 vp = bp->b_vp; 943 lwkt_gettoken(&vp->v_token); 944 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) { 945 if (bp->b_flags & B_VNDIRTY) 946 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); 947 else 948 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); 949 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN); 950 } 951 if (bp->b_flags & B_HASHED) { 952 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp); 953 bp->b_flags &= ~B_HASHED; 954 } 955 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) 956 vn_syncer_remove(vp); 957 bp->b_vp = NULL; 958 959 lwkt_reltoken(&vp->v_token); 960 961 vdrop(vp); 962 } 963 964 /* 965 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI. 966 * This routine is called when the state of the B_DELWRI bit is changed. 967 * 968 * Must be called with vp->v_token held. 969 * MPSAFE 970 */ 971 void 972 reassignbuf(struct buf *bp) 973 { 974 struct vnode *vp = bp->b_vp; 975 int delay; 976 977 ASSERT_LWKT_TOKEN_HELD(&vp->v_token); 978 ++reassignbufcalls; 979 980 /* 981 * B_PAGING flagged buffers cannot be reassigned because their vp 982 * is not fully linked in. 983 */ 984 if (bp->b_flags & B_PAGING) 985 panic("cannot reassign paging buffer"); 986 987 if (bp->b_flags & B_DELWRI) { 988 /* 989 * Move to the dirty list, add the vnode to the worklist 990 */ 991 if (bp->b_flags & B_VNCLEAN) { 992 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); 993 bp->b_flags &= ~B_VNCLEAN; 994 } 995 if ((bp->b_flags & B_VNDIRTY) == 0) { 996 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) { 997 panic("reassignbuf: dup lblk vp %p bp %p", 998 vp, bp); 999 } 1000 bp->b_flags |= B_VNDIRTY; 1001 } 1002 if ((vp->v_flag & VONWORKLST) == 0) { 1003 switch (vp->v_type) { 1004 case VDIR: 1005 delay = dirdelay; 1006 break; 1007 case VCHR: 1008 case VBLK: 1009 if (vp->v_rdev && 1010 vp->v_rdev->si_mountpoint != NULL) { 1011 delay = metadelay; 1012 break; 1013 } 1014 /* fall through */ 1015 default: 1016 delay = filedelay; 1017 } 1018 vn_syncer_add(vp, delay); 1019 } 1020 } else { 1021 /* 1022 * Move to the clean list, remove the vnode from the worklist 1023 * if no dirty blocks remain. 1024 */ 1025 if (bp->b_flags & B_VNDIRTY) { 1026 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); 1027 bp->b_flags &= ~B_VNDIRTY; 1028 } 1029 if ((bp->b_flags & B_VNCLEAN) == 0) { 1030 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) { 1031 panic("reassignbuf: dup lblk vp %p bp %p", 1032 vp, bp); 1033 } 1034 bp->b_flags |= B_VNCLEAN; 1035 } 1036 if ((vp->v_flag & VONWORKLST) && 1037 RB_EMPTY(&vp->v_rbdirty_tree)) { 1038 vn_syncer_remove(vp); 1039 } 1040 } 1041 } 1042 1043 /* 1044 * Create a vnode for a block device. Used for mounting the root file 1045 * system. 1046 * 1047 * A vref()'d vnode is returned. 1048 */ 1049 extern struct vop_ops *devfs_vnode_dev_vops_p; 1050 int 1051 bdevvp(cdev_t dev, struct vnode **vpp) 1052 { 1053 struct vnode *vp; 1054 struct vnode *nvp; 1055 int error; 1056 1057 if (dev == NULL) { 1058 *vpp = NULLVP; 1059 return (ENXIO); 1060 } 1061 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p, 1062 &nvp, 0, 0); 1063 if (error) { 1064 *vpp = NULLVP; 1065 return (error); 1066 } 1067 vp = nvp; 1068 vp->v_type = VCHR; 1069 #if 0 1070 vp->v_rdev = dev; 1071 #endif 1072 v_associate_rdev(vp, dev); 1073 vp->v_umajor = dev->si_umajor; 1074 vp->v_uminor = dev->si_uminor; 1075 vx_unlock(vp); 1076 *vpp = vp; 1077 return (0); 1078 } 1079 1080 int 1081 v_associate_rdev(struct vnode *vp, cdev_t dev) 1082 { 1083 if (dev == NULL) 1084 return(ENXIO); 1085 if (dev_is_good(dev) == 0) 1086 return(ENXIO); 1087 KKASSERT(vp->v_rdev == NULL); 1088 vp->v_rdev = reference_dev(dev); 1089 lwkt_gettoken(&spechash_token); 1090 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext); 1091 lwkt_reltoken(&spechash_token); 1092 return(0); 1093 } 1094 1095 void 1096 v_release_rdev(struct vnode *vp) 1097 { 1098 cdev_t dev; 1099 1100 if ((dev = vp->v_rdev) != NULL) { 1101 lwkt_gettoken(&spechash_token); 1102 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext); 1103 vp->v_rdev = NULL; 1104 release_dev(dev); 1105 lwkt_reltoken(&spechash_token); 1106 } 1107 } 1108 1109 /* 1110 * Add a vnode to the alias list hung off the cdev_t. We only associate 1111 * the device number with the vnode. The actual device is not associated 1112 * until the vnode is opened (usually in spec_open()), and will be 1113 * disassociated on last close. 1114 */ 1115 void 1116 addaliasu(struct vnode *nvp, int x, int y) 1117 { 1118 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 1119 panic("addaliasu on non-special vnode"); 1120 nvp->v_umajor = x; 1121 nvp->v_uminor = y; 1122 } 1123 1124 /* 1125 * Simple call that a filesystem can make to try to get rid of a 1126 * vnode. It will fail if anyone is referencing the vnode (including 1127 * the caller). 1128 * 1129 * The filesystem can check whether its in-memory inode structure still 1130 * references the vp on return. 1131 */ 1132 void 1133 vclean_unlocked(struct vnode *vp) 1134 { 1135 vx_get(vp); 1136 if (sysref_isactive(&vp->v_sysref) == 0) 1137 vgone_vxlocked(vp); 1138 vx_put(vp); 1139 } 1140 1141 /* 1142 * Disassociate a vnode from its underlying filesystem. 1143 * 1144 * The vnode must be VX locked and referenced. In all normal situations 1145 * there are no active references. If vclean_vxlocked() is called while 1146 * there are active references, the vnode is being ripped out and we have 1147 * to call VOP_CLOSE() as appropriate before we can reclaim it. 1148 */ 1149 void 1150 vclean_vxlocked(struct vnode *vp, int flags) 1151 { 1152 int active; 1153 int n; 1154 vm_object_t object; 1155 struct namecache *ncp; 1156 1157 /* 1158 * If the vnode has already been reclaimed we have nothing to do. 1159 */ 1160 if (vp->v_flag & VRECLAIMED) 1161 return; 1162 vsetflags(vp, VRECLAIMED); 1163 1164 if (verbose_reclaims) { 1165 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) 1166 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name); 1167 } 1168 1169 /* 1170 * Scrap the vfs cache 1171 */ 1172 while (cache_inval_vp(vp, 0) != 0) { 1173 kprintf("Warning: vnode %p clean/cache_resolution " 1174 "race detected\n", vp); 1175 tsleep(vp, 0, "vclninv", 2); 1176 } 1177 1178 /* 1179 * Check to see if the vnode is in use. If so we have to reference it 1180 * before we clean it out so that its count cannot fall to zero and 1181 * generate a race against ourselves to recycle it. 1182 */ 1183 active = sysref_isactive(&vp->v_sysref); 1184 1185 /* 1186 * Clean out any buffers associated with the vnode and destroy its 1187 * object, if it has one. 1188 */ 1189 vinvalbuf(vp, V_SAVE, 0, 0); 1190 1191 /* 1192 * If purging an active vnode (typically during a forced unmount 1193 * or reboot), it must be closed and deactivated before being 1194 * reclaimed. This isn't really all that safe, but what can 1195 * we do? XXX. 1196 * 1197 * Note that neither of these routines unlocks the vnode. 1198 */ 1199 if (active && (flags & DOCLOSE)) { 1200 while ((n = vp->v_opencount) != 0) { 1201 if (vp->v_writecount) 1202 VOP_CLOSE(vp, FWRITE|FNONBLOCK); 1203 else 1204 VOP_CLOSE(vp, FNONBLOCK); 1205 if (vp->v_opencount == n) { 1206 kprintf("Warning: unable to force-close" 1207 " vnode %p\n", vp); 1208 break; 1209 } 1210 } 1211 } 1212 1213 /* 1214 * If the vnode has not been deactivated, deactivated it. Deactivation 1215 * can create new buffers and VM pages so we have to call vinvalbuf() 1216 * again to make sure they all get flushed. 1217 * 1218 * This can occur if a file with a link count of 0 needs to be 1219 * truncated. 1220 * 1221 * If the vnode is already dead don't try to deactivate it. 1222 */ 1223 if ((vp->v_flag & VINACTIVE) == 0) { 1224 vsetflags(vp, VINACTIVE); 1225 if (vp->v_mount) 1226 VOP_INACTIVE(vp); 1227 vinvalbuf(vp, V_SAVE, 0, 0); 1228 } 1229 1230 /* 1231 * If the vnode has an object, destroy it. 1232 */ 1233 while ((object = vp->v_object) != NULL) { 1234 vm_object_hold(object); 1235 if (object == vp->v_object) 1236 break; 1237 vm_object_drop(object); 1238 } 1239 1240 if (object != NULL) { 1241 if (object->ref_count == 0) { 1242 if ((object->flags & OBJ_DEAD) == 0) 1243 vm_object_terminate(object); 1244 vm_object_drop(object); 1245 vclrflags(vp, VOBJBUF); 1246 } else { 1247 vm_pager_deallocate(object); 1248 vclrflags(vp, VOBJBUF); 1249 vm_object_drop(object); 1250 } 1251 } 1252 KKASSERT((vp->v_flag & VOBJBUF) == 0); 1253 1254 /* 1255 * Reclaim the vnode if not already dead. 1256 */ 1257 if (vp->v_mount && VOP_RECLAIM(vp)) 1258 panic("vclean: cannot reclaim"); 1259 1260 /* 1261 * Done with purge, notify sleepers of the grim news. 1262 */ 1263 vp->v_ops = &dead_vnode_vops_p; 1264 vn_gone(vp); 1265 vp->v_tag = VT_NON; 1266 1267 /* 1268 * If we are destroying an active vnode, reactivate it now that 1269 * we have reassociated it with deadfs. This prevents the system 1270 * from crashing on the vnode due to it being unexpectedly marked 1271 * as inactive or reclaimed. 1272 */ 1273 if (active && (flags & DOCLOSE)) { 1274 vclrflags(vp, VINACTIVE | VRECLAIMED); 1275 } 1276 } 1277 1278 /* 1279 * Eliminate all activity associated with the requested vnode 1280 * and with all vnodes aliased to the requested vnode. 1281 * 1282 * The vnode must be referenced but should not be locked. 1283 */ 1284 int 1285 vrevoke(struct vnode *vp, struct ucred *cred) 1286 { 1287 struct vnode *vq; 1288 struct vnode *vqn; 1289 cdev_t dev; 1290 int error; 1291 1292 /* 1293 * If the vnode has a device association, scrap all vnodes associated 1294 * with the device. Don't let the device disappear on us while we 1295 * are scrapping the vnodes. 1296 * 1297 * The passed vp will probably show up in the list, do not VX lock 1298 * it twice! 1299 * 1300 * Releasing the vnode's rdev here can mess up specfs's call to 1301 * device close, so don't do it. The vnode has been disassociated 1302 * and the device will be closed after the last ref on the related 1303 * fp goes away (if not still open by e.g. the kernel). 1304 */ 1305 if (vp->v_type != VCHR) { 1306 error = fdrevoke(vp, DTYPE_VNODE, cred); 1307 return (error); 1308 } 1309 if ((dev = vp->v_rdev) == NULL) { 1310 return(0); 1311 } 1312 reference_dev(dev); 1313 lwkt_gettoken(&spechash_token); 1314 1315 restart: 1316 vqn = SLIST_FIRST(&dev->si_hlist); 1317 if (vqn) 1318 vhold(vqn); 1319 while ((vq = vqn) != NULL) { 1320 if (sysref_isactive(&vq->v_sysref)) { 1321 vref(vq); 1322 fdrevoke(vq, DTYPE_VNODE, cred); 1323 /*v_release_rdev(vq);*/ 1324 vrele(vq); 1325 if (vq->v_rdev != dev) { 1326 vdrop(vq); 1327 goto restart; 1328 } 1329 } 1330 vqn = SLIST_NEXT(vq, v_cdevnext); 1331 if (vqn) 1332 vhold(vqn); 1333 vdrop(vq); 1334 } 1335 lwkt_reltoken(&spechash_token); 1336 dev_drevoke(dev); 1337 release_dev(dev); 1338 return (0); 1339 } 1340 1341 /* 1342 * This is called when the object underlying a vnode is being destroyed, 1343 * such as in a remove(). Try to recycle the vnode immediately if the 1344 * only active reference is our reference. 1345 * 1346 * Directory vnodes in the namecache with children cannot be immediately 1347 * recycled because numerous VOP_N*() ops require them to be stable. 1348 * 1349 * To avoid recursive recycling from VOP_INACTIVE implemenetations this 1350 * function is a NOP if VRECLAIMED is already set. 1351 */ 1352 int 1353 vrecycle(struct vnode *vp) 1354 { 1355 if (vp->v_sysref.refcnt <= 1 && (vp->v_flag & VRECLAIMED) == 0) { 1356 if (cache_inval_vp_nonblock(vp)) 1357 return(0); 1358 vgone_vxlocked(vp); 1359 return (1); 1360 } 1361 return (0); 1362 } 1363 1364 /* 1365 * Return the maximum I/O size allowed for strategy calls on VP. 1366 * 1367 * If vp is VCHR or VBLK we dive the device, otherwise we use 1368 * the vp's mount info. 1369 */ 1370 int 1371 vmaxiosize(struct vnode *vp) 1372 { 1373 if (vp->v_type == VBLK || vp->v_type == VCHR) { 1374 return(vp->v_rdev->si_iosize_max); 1375 } else { 1376 return(vp->v_mount->mnt_iosize_max); 1377 } 1378 } 1379 1380 /* 1381 * Eliminate all activity associated with a vnode in preparation for reuse. 1382 * 1383 * The vnode must be VX locked and refd and will remain VX locked and refd 1384 * on return. This routine may be called with the vnode in any state, as 1385 * long as it is VX locked. The vnode will be cleaned out and marked 1386 * VRECLAIMED but will not actually be reused until all existing refs and 1387 * holds go away. 1388 * 1389 * NOTE: This routine may be called on a vnode which has not yet been 1390 * already been deactivated (VOP_INACTIVE), or on a vnode which has 1391 * already been reclaimed. 1392 * 1393 * This routine is not responsible for placing us back on the freelist. 1394 * Instead, it happens automatically when the caller releases the VX lock 1395 * (assuming there aren't any other references). 1396 */ 1397 void 1398 vgone_vxlocked(struct vnode *vp) 1399 { 1400 /* 1401 * assert that the VX lock is held. This is an absolute requirement 1402 * now for vgone_vxlocked() to be called. 1403 */ 1404 KKASSERT(vp->v_lock.lk_exclusivecount == 1); 1405 1406 /* 1407 * Clean out the filesystem specific data and set the VRECLAIMED 1408 * bit. Also deactivate the vnode if necessary. 1409 */ 1410 vclean_vxlocked(vp, DOCLOSE); 1411 1412 /* 1413 * Delete from old mount point vnode list, if on one. 1414 */ 1415 if (vp->v_mount != NULL) { 1416 KKASSERT(vp->v_data == NULL); 1417 insmntque(vp, NULL); 1418 } 1419 1420 /* 1421 * If special device, remove it from special device alias list 1422 * if it is on one. This should normally only occur if a vnode is 1423 * being revoked as the device should otherwise have been released 1424 * naturally. 1425 */ 1426 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) { 1427 v_release_rdev(vp); 1428 } 1429 1430 /* 1431 * Set us to VBAD 1432 */ 1433 vp->v_type = VBAD; 1434 } 1435 1436 /* 1437 * Lookup a vnode by device number. 1438 * 1439 * Returns non-zero and *vpp set to a vref'd vnode on success. 1440 * Returns zero on failure. 1441 */ 1442 int 1443 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp) 1444 { 1445 struct vnode *vp; 1446 1447 lwkt_gettoken(&spechash_token); 1448 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) { 1449 if (type == vp->v_type) { 1450 *vpp = vp; 1451 vref(vp); 1452 lwkt_reltoken(&spechash_token); 1453 return (1); 1454 } 1455 } 1456 lwkt_reltoken(&spechash_token); 1457 return (0); 1458 } 1459 1460 /* 1461 * Calculate the total number of references to a special device. This 1462 * routine may only be called for VBLK and VCHR vnodes since v_rdev is 1463 * an overloaded field. Since udev2dev can now return NULL, we have 1464 * to check for a NULL v_rdev. 1465 */ 1466 int 1467 count_dev(cdev_t dev) 1468 { 1469 struct vnode *vp; 1470 int count = 0; 1471 1472 if (SLIST_FIRST(&dev->si_hlist)) { 1473 lwkt_gettoken(&spechash_token); 1474 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) { 1475 count += vp->v_opencount; 1476 } 1477 lwkt_reltoken(&spechash_token); 1478 } 1479 return(count); 1480 } 1481 1482 int 1483 vcount(struct vnode *vp) 1484 { 1485 if (vp->v_rdev == NULL) 1486 return(0); 1487 return(count_dev(vp->v_rdev)); 1488 } 1489 1490 /* 1491 * Initialize VMIO for a vnode. This routine MUST be called before a 1492 * VFS can issue buffer cache ops on a vnode. It is typically called 1493 * when a vnode is initialized from its inode. 1494 */ 1495 int 1496 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff) 1497 { 1498 vm_object_t object; 1499 int error = 0; 1500 1501 retry: 1502 while ((object = vp->v_object) != NULL) { 1503 vm_object_hold(object); 1504 if (object == vp->v_object) 1505 break; 1506 vm_object_drop(object); 1507 } 1508 1509 if (object == NULL) { 1510 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff); 1511 1512 /* 1513 * Dereference the reference we just created. This assumes 1514 * that the object is associated with the vp. 1515 */ 1516 vm_object_hold(object); 1517 object->ref_count--; 1518 vrele(vp); 1519 } else { 1520 if (object->flags & OBJ_DEAD) { 1521 vn_unlock(vp); 1522 if (vp->v_object == object) 1523 vm_object_dead_sleep(object, "vodead"); 1524 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1525 vm_object_drop(object); 1526 goto retry; 1527 } 1528 } 1529 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object")); 1530 vsetflags(vp, VOBJBUF); 1531 vm_object_drop(object); 1532 1533 return (error); 1534 } 1535 1536 1537 /* 1538 * Print out a description of a vnode. 1539 */ 1540 static char *typename[] = 1541 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 1542 1543 void 1544 vprint(char *label, struct vnode *vp) 1545 { 1546 char buf[96]; 1547 1548 if (label != NULL) 1549 kprintf("%s: %p: ", label, (void *)vp); 1550 else 1551 kprintf("%p: ", (void *)vp); 1552 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,", 1553 typename[vp->v_type], 1554 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs); 1555 buf[0] = '\0'; 1556 if (vp->v_flag & VROOT) 1557 strcat(buf, "|VROOT"); 1558 if (vp->v_flag & VPFSROOT) 1559 strcat(buf, "|VPFSROOT"); 1560 if (vp->v_flag & VTEXT) 1561 strcat(buf, "|VTEXT"); 1562 if (vp->v_flag & VSYSTEM) 1563 strcat(buf, "|VSYSTEM"); 1564 if (vp->v_flag & VFREE) 1565 strcat(buf, "|VFREE"); 1566 if (vp->v_flag & VOBJBUF) 1567 strcat(buf, "|VOBJBUF"); 1568 if (buf[0] != '\0') 1569 kprintf(" flags (%s)", &buf[1]); 1570 if (vp->v_data == NULL) { 1571 kprintf("\n"); 1572 } else { 1573 kprintf("\n\t"); 1574 VOP_PRINT(vp); 1575 } 1576 } 1577 1578 /* 1579 * Do the usual access checking. 1580 * file_mode, uid and gid are from the vnode in question, 1581 * while acc_mode and cred are from the VOP_ACCESS parameter list 1582 */ 1583 int 1584 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 1585 mode_t acc_mode, struct ucred *cred) 1586 { 1587 mode_t mask; 1588 int ismember; 1589 1590 /* 1591 * Super-user always gets read/write access, but execute access depends 1592 * on at least one execute bit being set. 1593 */ 1594 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) { 1595 if ((acc_mode & VEXEC) && type != VDIR && 1596 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 1597 return (EACCES); 1598 return (0); 1599 } 1600 1601 mask = 0; 1602 1603 /* Otherwise, check the owner. */ 1604 if (cred->cr_uid == uid) { 1605 if (acc_mode & VEXEC) 1606 mask |= S_IXUSR; 1607 if (acc_mode & VREAD) 1608 mask |= S_IRUSR; 1609 if (acc_mode & VWRITE) 1610 mask |= S_IWUSR; 1611 return ((file_mode & mask) == mask ? 0 : EACCES); 1612 } 1613 1614 /* Otherwise, check the groups. */ 1615 ismember = groupmember(gid, cred); 1616 if (cred->cr_svgid == gid || ismember) { 1617 if (acc_mode & VEXEC) 1618 mask |= S_IXGRP; 1619 if (acc_mode & VREAD) 1620 mask |= S_IRGRP; 1621 if (acc_mode & VWRITE) 1622 mask |= S_IWGRP; 1623 return ((file_mode & mask) == mask ? 0 : EACCES); 1624 } 1625 1626 /* Otherwise, check everyone else. */ 1627 if (acc_mode & VEXEC) 1628 mask |= S_IXOTH; 1629 if (acc_mode & VREAD) 1630 mask |= S_IROTH; 1631 if (acc_mode & VWRITE) 1632 mask |= S_IWOTH; 1633 return ((file_mode & mask) == mask ? 0 : EACCES); 1634 } 1635 1636 #ifdef DDB 1637 #include <ddb/ddb.h> 1638 1639 static int db_show_locked_vnodes(struct mount *mp, void *data); 1640 1641 /* 1642 * List all of the locked vnodes in the system. 1643 * Called when debugging the kernel. 1644 */ 1645 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes) 1646 { 1647 kprintf("Locked vnodes\n"); 1648 mountlist_scan(db_show_locked_vnodes, NULL, 1649 MNTSCAN_FORWARD|MNTSCAN_NOBUSY); 1650 } 1651 1652 static int 1653 db_show_locked_vnodes(struct mount *mp, void *data __unused) 1654 { 1655 struct vnode *vp; 1656 1657 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 1658 if (vn_islocked(vp)) 1659 vprint(NULL, vp); 1660 } 1661 return(0); 1662 } 1663 #endif 1664 1665 /* 1666 * Top level filesystem related information gathering. 1667 */ 1668 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS); 1669 1670 static int 1671 vfs_sysctl(SYSCTL_HANDLER_ARGS) 1672 { 1673 int *name = (int *)arg1 - 1; /* XXX */ 1674 u_int namelen = arg2 + 1; /* XXX */ 1675 struct vfsconf *vfsp; 1676 int maxtypenum; 1677 1678 #if 1 || defined(COMPAT_PRELITE2) 1679 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 1680 if (namelen == 1) 1681 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 1682 #endif 1683 1684 #ifdef notyet 1685 /* all sysctl names at this level are at least name and field */ 1686 if (namelen < 2) 1687 return (ENOTDIR); /* overloaded */ 1688 if (name[0] != VFS_GENERIC) { 1689 vfsp = vfsconf_find_by_typenum(name[0]); 1690 if (vfsp == NULL) 1691 return (EOPNOTSUPP); 1692 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 1693 oldp, oldlenp, newp, newlen, p)); 1694 } 1695 #endif 1696 switch (name[1]) { 1697 case VFS_MAXTYPENUM: 1698 if (namelen != 2) 1699 return (ENOTDIR); 1700 maxtypenum = vfsconf_get_maxtypenum(); 1701 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum))); 1702 case VFS_CONF: 1703 if (namelen != 3) 1704 return (ENOTDIR); /* overloaded */ 1705 vfsp = vfsconf_find_by_typenum(name[2]); 1706 if (vfsp == NULL) 1707 return (EOPNOTSUPP); 1708 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp)); 1709 } 1710 return (EOPNOTSUPP); 1711 } 1712 1713 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl, 1714 "Generic filesystem"); 1715 1716 #if 1 || defined(COMPAT_PRELITE2) 1717 1718 static int 1719 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data) 1720 { 1721 int error; 1722 struct ovfsconf ovfs; 1723 struct sysctl_req *req = (struct sysctl_req*) data; 1724 1725 bzero(&ovfs, sizeof(ovfs)); 1726 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 1727 strcpy(ovfs.vfc_name, vfsp->vfc_name); 1728 ovfs.vfc_index = vfsp->vfc_typenum; 1729 ovfs.vfc_refcount = vfsp->vfc_refcount; 1730 ovfs.vfc_flags = vfsp->vfc_flags; 1731 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 1732 if (error) 1733 return error; /* abort iteration with error code */ 1734 else 1735 return 0; /* continue iterating with next element */ 1736 } 1737 1738 static int 1739 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 1740 { 1741 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req); 1742 } 1743 1744 #endif /* 1 || COMPAT_PRELITE2 */ 1745 1746 /* 1747 * Check to see if a filesystem is mounted on a block device. 1748 */ 1749 int 1750 vfs_mountedon(struct vnode *vp) 1751 { 1752 cdev_t dev; 1753 1754 if ((dev = vp->v_rdev) == NULL) { 1755 /* if (vp->v_type != VBLK) 1756 dev = get_dev(vp->v_uminor, vp->v_umajor); */ 1757 } 1758 if (dev != NULL && dev->si_mountpoint) 1759 return (EBUSY); 1760 return (0); 1761 } 1762 1763 /* 1764 * Unmount all filesystems. The list is traversed in reverse order 1765 * of mounting to avoid dependencies. 1766 */ 1767 1768 static int vfs_umountall_callback(struct mount *mp, void *data); 1769 1770 void 1771 vfs_unmountall(void) 1772 { 1773 int count; 1774 1775 do { 1776 count = mountlist_scan(vfs_umountall_callback, 1777 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY); 1778 } while (count); 1779 } 1780 1781 static 1782 int 1783 vfs_umountall_callback(struct mount *mp, void *data) 1784 { 1785 int error; 1786 1787 error = dounmount(mp, MNT_FORCE); 1788 if (error) { 1789 mountlist_remove(mp); 1790 kprintf("unmount of filesystem mounted from %s failed (", 1791 mp->mnt_stat.f_mntfromname); 1792 if (error == EBUSY) 1793 kprintf("BUSY)\n"); 1794 else 1795 kprintf("%d)\n", error); 1796 } 1797 return(1); 1798 } 1799 1800 /* 1801 * Checks the mount flags for parameter mp and put the names comma-separated 1802 * into a string buffer buf with a size limit specified by len. 1803 * 1804 * It returns the number of bytes written into buf, and (*errorp) will be 1805 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was 1806 * not large enough). The buffer will be 0-terminated if len was not 0. 1807 */ 1808 size_t 1809 vfs_flagstostr(int flags, const struct mountctl_opt *optp, 1810 char *buf, size_t len, int *errorp) 1811 { 1812 static const struct mountctl_opt optnames[] = { 1813 { MNT_ASYNC, "asynchronous" }, 1814 { MNT_EXPORTED, "NFS exported" }, 1815 { MNT_LOCAL, "local" }, 1816 { MNT_NOATIME, "noatime" }, 1817 { MNT_NODEV, "nodev" }, 1818 { MNT_NOEXEC, "noexec" }, 1819 { MNT_NOSUID, "nosuid" }, 1820 { MNT_NOSYMFOLLOW, "nosymfollow" }, 1821 { MNT_QUOTA, "with-quotas" }, 1822 { MNT_RDONLY, "read-only" }, 1823 { MNT_SYNCHRONOUS, "synchronous" }, 1824 { MNT_UNION, "union" }, 1825 { MNT_NOCLUSTERR, "noclusterr" }, 1826 { MNT_NOCLUSTERW, "noclusterw" }, 1827 { MNT_SUIDDIR, "suiddir" }, 1828 { MNT_SOFTDEP, "soft-updates" }, 1829 { MNT_IGNORE, "ignore" }, 1830 { 0, NULL} 1831 }; 1832 int bwritten; 1833 int bleft; 1834 int optlen; 1835 int actsize; 1836 1837 *errorp = 0; 1838 bwritten = 0; 1839 bleft = len - 1; /* leave room for trailing \0 */ 1840 1841 /* 1842 * Checks the size of the string. If it contains 1843 * any data, then we will append the new flags to 1844 * it. 1845 */ 1846 actsize = strlen(buf); 1847 if (actsize > 0) 1848 buf += actsize; 1849 1850 /* Default flags if no flags passed */ 1851 if (optp == NULL) 1852 optp = optnames; 1853 1854 if (bleft < 0) { /* degenerate case, 0-length buffer */ 1855 *errorp = EINVAL; 1856 return(0); 1857 } 1858 1859 for (; flags && optp->o_opt; ++optp) { 1860 if ((flags & optp->o_opt) == 0) 1861 continue; 1862 optlen = strlen(optp->o_name); 1863 if (bwritten || actsize > 0) { 1864 if (bleft < 2) { 1865 *errorp = ENOSPC; 1866 break; 1867 } 1868 buf[bwritten++] = ','; 1869 buf[bwritten++] = ' '; 1870 bleft -= 2; 1871 } 1872 if (bleft < optlen) { 1873 *errorp = ENOSPC; 1874 break; 1875 } 1876 bcopy(optp->o_name, buf + bwritten, optlen); 1877 bwritten += optlen; 1878 bleft -= optlen; 1879 flags &= ~optp->o_opt; 1880 } 1881 1882 /* 1883 * Space already reserved for trailing \0 1884 */ 1885 buf[bwritten] = 0; 1886 return (bwritten); 1887 } 1888 1889 /* 1890 * Build hash lists of net addresses and hang them off the mount point. 1891 * Called by ufs_mount() to set up the lists of export addresses. 1892 */ 1893 static int 1894 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 1895 const struct export_args *argp) 1896 { 1897 struct netcred *np; 1898 struct radix_node_head *rnh; 1899 int i; 1900 struct radix_node *rn; 1901 struct sockaddr *saddr, *smask = NULL; 1902 struct domain *dom; 1903 int error; 1904 1905 if (argp->ex_addrlen == 0) { 1906 if (mp->mnt_flag & MNT_DEFEXPORTED) 1907 return (EPERM); 1908 np = &nep->ne_defexported; 1909 np->netc_exflags = argp->ex_flags; 1910 np->netc_anon = argp->ex_anon; 1911 np->netc_anon.cr_ref = 1; 1912 mp->mnt_flag |= MNT_DEFEXPORTED; 1913 return (0); 1914 } 1915 1916 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN) 1917 return (EINVAL); 1918 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN) 1919 return (EINVAL); 1920 1921 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 1922 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO); 1923 saddr = (struct sockaddr *) (np + 1); 1924 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen))) 1925 goto out; 1926 if (saddr->sa_len > argp->ex_addrlen) 1927 saddr->sa_len = argp->ex_addrlen; 1928 if (argp->ex_masklen) { 1929 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 1930 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen); 1931 if (error) 1932 goto out; 1933 if (smask->sa_len > argp->ex_masklen) 1934 smask->sa_len = argp->ex_masklen; 1935 } 1936 i = saddr->sa_family; 1937 if ((rnh = nep->ne_rtable[i]) == NULL) { 1938 /* 1939 * Seems silly to initialize every AF when most are not used, 1940 * do so on demand here 1941 */ 1942 SLIST_FOREACH(dom, &domains, dom_next) 1943 if (dom->dom_family == i && dom->dom_rtattach) { 1944 dom->dom_rtattach((void **) &nep->ne_rtable[i], 1945 dom->dom_rtoffset); 1946 break; 1947 } 1948 if ((rnh = nep->ne_rtable[i]) == NULL) { 1949 error = ENOBUFS; 1950 goto out; 1951 } 1952 } 1953 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh, 1954 np->netc_rnodes); 1955 if (rn == NULL || np != (struct netcred *) rn) { /* already exists */ 1956 error = EPERM; 1957 goto out; 1958 } 1959 np->netc_exflags = argp->ex_flags; 1960 np->netc_anon = argp->ex_anon; 1961 np->netc_anon.cr_ref = 1; 1962 return (0); 1963 out: 1964 kfree(np, M_NETADDR); 1965 return (error); 1966 } 1967 1968 /* ARGSUSED */ 1969 static int 1970 vfs_free_netcred(struct radix_node *rn, void *w) 1971 { 1972 struct radix_node_head *rnh = (struct radix_node_head *) w; 1973 1974 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 1975 kfree((caddr_t) rn, M_NETADDR); 1976 return (0); 1977 } 1978 1979 /* 1980 * Free the net address hash lists that are hanging off the mount points. 1981 */ 1982 static void 1983 vfs_free_addrlist(struct netexport *nep) 1984 { 1985 int i; 1986 struct radix_node_head *rnh; 1987 1988 for (i = 0; i <= AF_MAX; i++) 1989 if ((rnh = nep->ne_rtable[i])) { 1990 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, 1991 (caddr_t) rnh); 1992 kfree((caddr_t) rnh, M_RTABLE); 1993 nep->ne_rtable[i] = 0; 1994 } 1995 } 1996 1997 int 1998 vfs_export(struct mount *mp, struct netexport *nep, 1999 const struct export_args *argp) 2000 { 2001 int error; 2002 2003 if (argp->ex_flags & MNT_DELEXPORT) { 2004 if (mp->mnt_flag & MNT_EXPUBLIC) { 2005 vfs_setpublicfs(NULL, NULL, NULL); 2006 mp->mnt_flag &= ~MNT_EXPUBLIC; 2007 } 2008 vfs_free_addrlist(nep); 2009 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 2010 } 2011 if (argp->ex_flags & MNT_EXPORTED) { 2012 if (argp->ex_flags & MNT_EXPUBLIC) { 2013 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 2014 return (error); 2015 mp->mnt_flag |= MNT_EXPUBLIC; 2016 } 2017 if ((error = vfs_hang_addrlist(mp, nep, argp))) 2018 return (error); 2019 mp->mnt_flag |= MNT_EXPORTED; 2020 } 2021 return (0); 2022 } 2023 2024 2025 /* 2026 * Set the publicly exported filesystem (WebNFS). Currently, only 2027 * one public filesystem is possible in the spec (RFC 2054 and 2055) 2028 */ 2029 int 2030 vfs_setpublicfs(struct mount *mp, struct netexport *nep, 2031 const struct export_args *argp) 2032 { 2033 int error; 2034 struct vnode *rvp; 2035 char *cp; 2036 2037 /* 2038 * mp == NULL -> invalidate the current info, the FS is 2039 * no longer exported. May be called from either vfs_export 2040 * or unmount, so check if it hasn't already been done. 2041 */ 2042 if (mp == NULL) { 2043 if (nfs_pub.np_valid) { 2044 nfs_pub.np_valid = 0; 2045 if (nfs_pub.np_index != NULL) { 2046 kfree(nfs_pub.np_index, M_TEMP); 2047 nfs_pub.np_index = NULL; 2048 } 2049 } 2050 return (0); 2051 } 2052 2053 /* 2054 * Only one allowed at a time. 2055 */ 2056 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 2057 return (EBUSY); 2058 2059 /* 2060 * Get real filehandle for root of exported FS. 2061 */ 2062 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 2063 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 2064 2065 if ((error = VFS_ROOT(mp, &rvp))) 2066 return (error); 2067 2068 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 2069 return (error); 2070 2071 vput(rvp); 2072 2073 /* 2074 * If an indexfile was specified, pull it in. 2075 */ 2076 if (argp->ex_indexfile != NULL) { 2077 int namelen; 2078 2079 error = vn_get_namelen(rvp, &namelen); 2080 if (error) 2081 return (error); 2082 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK); 2083 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 2084 namelen, NULL); 2085 if (!error) { 2086 /* 2087 * Check for illegal filenames. 2088 */ 2089 for (cp = nfs_pub.np_index; *cp; cp++) { 2090 if (*cp == '/') { 2091 error = EINVAL; 2092 break; 2093 } 2094 } 2095 } 2096 if (error) { 2097 kfree(nfs_pub.np_index, M_TEMP); 2098 return (error); 2099 } 2100 } 2101 2102 nfs_pub.np_mount = mp; 2103 nfs_pub.np_valid = 1; 2104 return (0); 2105 } 2106 2107 struct netcred * 2108 vfs_export_lookup(struct mount *mp, struct netexport *nep, 2109 struct sockaddr *nam) 2110 { 2111 struct netcred *np; 2112 struct radix_node_head *rnh; 2113 struct sockaddr *saddr; 2114 2115 np = NULL; 2116 if (mp->mnt_flag & MNT_EXPORTED) { 2117 /* 2118 * Lookup in the export list first. 2119 */ 2120 if (nam != NULL) { 2121 saddr = nam; 2122 rnh = nep->ne_rtable[saddr->sa_family]; 2123 if (rnh != NULL) { 2124 np = (struct netcred *) 2125 (*rnh->rnh_matchaddr)((char *)saddr, 2126 rnh); 2127 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 2128 np = NULL; 2129 } 2130 } 2131 /* 2132 * If no address match, use the default if it exists. 2133 */ 2134 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 2135 np = &nep->ne_defexported; 2136 } 2137 return (np); 2138 } 2139 2140 /* 2141 * perform msync on all vnodes under a mount point. The mount point must 2142 * be locked. This code is also responsible for lazy-freeing unreferenced 2143 * vnodes whos VM objects no longer contain pages. 2144 * 2145 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state. 2146 * 2147 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked, 2148 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it 2149 * way up in this high level function. 2150 */ 2151 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data); 2152 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data); 2153 2154 void 2155 vfs_msync(struct mount *mp, int flags) 2156 { 2157 int vmsc_flags; 2158 2159 /* 2160 * tmpfs sets this flag to prevent msync(), sync, and the 2161 * filesystem periodic syncer from trying to flush VM pages 2162 * to swap. Only pure memory pressure flushes tmpfs VM pages 2163 * to swap. 2164 */ 2165 if (mp->mnt_kern_flag & MNTK_NOMSYNC) 2166 return; 2167 2168 /* 2169 * Ok, scan the vnodes for work. 2170 */ 2171 vmsc_flags = VMSC_GETVP; 2172 if (flags != MNT_WAIT) 2173 vmsc_flags |= VMSC_NOWAIT; 2174 vmntvnodescan(mp, vmsc_flags, 2175 vfs_msync_scan1, vfs_msync_scan2, 2176 (void *)(intptr_t)flags); 2177 } 2178 2179 /* 2180 * scan1 is a fast pre-check. There could be hundreds of thousands of 2181 * vnodes, we cannot afford to do anything heavy weight until we have a 2182 * fairly good indication that there is work to do. 2183 */ 2184 static 2185 int 2186 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data) 2187 { 2188 int flags = (int)(intptr_t)data; 2189 2190 if ((vp->v_flag & VRECLAIMED) == 0) { 2191 if (vshouldmsync(vp)) 2192 return(0); /* call scan2 */ 2193 if ((mp->mnt_flag & MNT_RDONLY) == 0 && 2194 (vp->v_flag & VOBJDIRTY) && 2195 (flags == MNT_WAIT || vn_islocked(vp) == 0)) { 2196 return(0); /* call scan2 */ 2197 } 2198 } 2199 2200 /* 2201 * do not call scan2, continue the loop 2202 */ 2203 return(-1); 2204 } 2205 2206 /* 2207 * This callback is handed a locked vnode. 2208 */ 2209 static 2210 int 2211 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data) 2212 { 2213 vm_object_t obj; 2214 int flags = (int)(intptr_t)data; 2215 2216 if (vp->v_flag & VRECLAIMED) 2217 return(0); 2218 2219 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) { 2220 if ((obj = vp->v_object) != NULL) { 2221 vm_object_page_clean(obj, 0, 0, 2222 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC); 2223 } 2224 } 2225 return(0); 2226 } 2227 2228 /* 2229 * Wake up anyone interested in vp because it is being revoked. 2230 */ 2231 void 2232 vn_gone(struct vnode *vp) 2233 { 2234 lwkt_gettoken(&vp->v_token); 2235 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE); 2236 lwkt_reltoken(&vp->v_token); 2237 } 2238 2239 /* 2240 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened 2241 * (or v_rdev might be NULL). 2242 */ 2243 cdev_t 2244 vn_todev(struct vnode *vp) 2245 { 2246 if (vp->v_type != VBLK && vp->v_type != VCHR) 2247 return (NULL); 2248 KKASSERT(vp->v_rdev != NULL); 2249 return (vp->v_rdev); 2250 } 2251 2252 /* 2253 * Check if vnode represents a disk device. The vnode does not need to be 2254 * opened. 2255 * 2256 * MPALMOSTSAFE 2257 */ 2258 int 2259 vn_isdisk(struct vnode *vp, int *errp) 2260 { 2261 cdev_t dev; 2262 2263 if (vp->v_type != VCHR) { 2264 if (errp != NULL) 2265 *errp = ENOTBLK; 2266 return (0); 2267 } 2268 2269 dev = vp->v_rdev; 2270 2271 if (dev == NULL) { 2272 if (errp != NULL) 2273 *errp = ENXIO; 2274 return (0); 2275 } 2276 if (dev_is_good(dev) == 0) { 2277 if (errp != NULL) 2278 *errp = ENXIO; 2279 return (0); 2280 } 2281 if ((dev_dflags(dev) & D_DISK) == 0) { 2282 if (errp != NULL) 2283 *errp = ENOTBLK; 2284 return (0); 2285 } 2286 if (errp != NULL) 2287 *errp = 0; 2288 return (1); 2289 } 2290 2291 int 2292 vn_get_namelen(struct vnode *vp, int *namelen) 2293 { 2294 int error; 2295 register_t retval[2]; 2296 2297 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval); 2298 if (error) 2299 return (error); 2300 *namelen = (int)retval[0]; 2301 return (0); 2302 } 2303 2304 int 2305 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type, 2306 uint16_t d_namlen, const char *d_name) 2307 { 2308 struct dirent *dp; 2309 size_t len; 2310 2311 len = _DIRENT_RECLEN(d_namlen); 2312 if (len > uio->uio_resid) 2313 return(1); 2314 2315 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO); 2316 2317 dp->d_ino = d_ino; 2318 dp->d_namlen = d_namlen; 2319 dp->d_type = d_type; 2320 bcopy(d_name, dp->d_name, d_namlen); 2321 2322 *error = uiomove((caddr_t)dp, len, uio); 2323 2324 kfree(dp, M_TEMP); 2325 2326 return(0); 2327 } 2328 2329 void 2330 vn_mark_atime(struct vnode *vp, struct thread *td) 2331 { 2332 struct proc *p = td->td_proc; 2333 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred; 2334 2335 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) { 2336 VOP_MARKATIME(vp, cred); 2337 } 2338 } 2339