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