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