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