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