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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 35 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $ 36 */ 37 38 /* 39 * External virtual filesystem routines 40 */ 41 #include "opt_ddb.h" 42 #include "opt_inet.h" 43 #include "opt_inet6.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/uio.h> 48 #include <sys/buf.h> 49 #include <sys/conf.h> 50 #include <sys/dirent.h> 51 #include <sys/endian.h> 52 #include <sys/eventhandler.h> 53 #include <sys/fcntl.h> 54 #include <sys/file.h> 55 #include <sys/kernel.h> 56 #include <sys/kthread.h> 57 #include <sys/malloc.h> 58 #include <sys/mbuf.h> 59 #include <sys/mount.h> 60 #include <sys/priv.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 <vm/vm_page2.h> 86 87 #include <netinet/in.h> 88 89 static MALLOC_DEFINE(M_NETCRED, "Export Host", "Export host address structure"); 90 91 __read_mostly int numvnodes; 92 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, 93 "Number of vnodes allocated"); 94 __read_mostly int verbose_reclaims; 95 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0, 96 "Output filename of reclaimed vnode(s)"); 97 98 __read_mostly enum vtype iftovt_tab[16] = { 99 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 100 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 101 }; 102 __read_mostly int vttoif_tab[9] = { 103 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 104 S_IFSOCK, S_IFIFO, S_IFMT, 105 }; 106 107 static int reassignbufcalls; 108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 109 0, "Number of times buffers have been reassigned to the proper list"); 110 111 __read_mostly static int check_buf_overlap = 2; /* invasive check */ 112 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap, 113 0, "Enable overlapping buffer checks"); 114 115 int nfs_mount_type = -1; 116 static struct lwkt_token spechash_token; 117 struct nfs_public nfs_pub; /* publicly exported FS */ 118 119 __read_mostly int maxvnodes; 120 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 121 &maxvnodes, 0, "Maximum number of vnodes"); 122 123 static struct radix_node_head *vfs_create_addrlist_af(int af, 124 struct netexport *nep); 125 static void vfs_free_addrlist (struct netexport *nep); 126 static int vfs_free_netcred (struct radix_node *rn, void *w); 127 static void vfs_free_addrlist_af (struct radix_node_head **prnh); 128 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep, 129 const struct export_args *argp); 130 static void vclean_vxlocked(struct vnode *vp, int flags); 131 132 __read_mostly int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 133 134 /* 135 * Red black tree functions 136 */ 137 static int rb_buf_compare(struct buf *b1, struct buf *b2); 138 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset); 139 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset); 140 141 static int 142 rb_buf_compare(struct buf *b1, struct buf *b2) 143 { 144 if (b1->b_loffset < b2->b_loffset) 145 return(-1); 146 if (b1->b_loffset > b2->b_loffset) 147 return(1); 148 return(0); 149 } 150 151 /* 152 * Initialize the vnode management data structures. 153 * 154 * Called from vfsinit() 155 */ 156 #define VNBREAKMEM1 (1L * 1024 * 1024 * 1024) 157 #define VNBREAKMEM2 (7L * 1024 * 1024 * 1024) 158 #define MINVNODES 2000 159 #define MAXVNODES 4000000 160 161 void 162 vfs_subr_init(void) 163 { 164 int factor1; /* Limit based on ram (x 2 above 1GB) */ 165 size_t freemem; 166 167 /* 168 * Size maxvnodes non-linearly to available memory. Don't bloat 169 * the count on low-memory systems. Scale up for systems with 170 * more than 1G and more than 8G of ram, but do so non-linearly 171 * because the value of a large maxvnodes count diminishes 172 * significantly beyond a certain point. 173 * 174 * The general minimum is maxproc * 8 (we want someone pushing 175 * up maxproc a lot to also get more vnodes). Usually maxproc 176 * does not affect this calculation. The KvaSize limitation also 177 * typically does not affect this calculation (it is just in case 178 * the kernel VM space is made much smaller than main memory, which 179 * should no longer happen on 64-bit systems). 180 * 181 * There isn't much of a point allowing maxvnodes to exceed a 182 * few million as modern filesystems cache pages in the 183 * underlying block device and not so much hanging off of VM 184 * objects. 185 * 186 * Also, VM objects, vnodes, and filesystem inode and other related 187 * structures have gotten a lot larger in recent years and the kernel 188 * memory use tends to scale with maxvnodes, so we don't want to bloat 189 * it too much. But neither do we want the max set too low because 190 * systems with large amounts of memory and cores are capable of 191 * doing a hell of a lot. 192 */ 193 factor1 = 80 * (sizeof(struct vm_object) + sizeof(struct vnode)); 194 195 freemem = (int64_t)vmstats.v_page_count * PAGE_SIZE; 196 197 maxvnodes = freemem / factor1; 198 if (freemem > VNBREAKMEM1) { 199 freemem -= VNBREAKMEM1; 200 if (freemem < VNBREAKMEM2) { 201 maxvnodes += freemem / factor1 / 2; 202 } else { 203 maxvnodes += VNBREAKMEM2 / factor1 / 2; 204 freemem -= VNBREAKMEM2; 205 maxvnodes += freemem / factor1 / 4; 206 } 207 } 208 maxvnodes = imax(maxvnodes, maxproc * 8); 209 maxvnodes = imin(maxvnodes, KvaSize / factor1); 210 maxvnodes = imin(maxvnodes, MAXVNODES); 211 maxvnodes = imax(maxvnodes, MINVNODES); 212 213 lwkt_token_init(&spechash_token, "spechash"); 214 } 215 216 /* 217 * Knob to control the precision of file timestamps: 218 * 219 * 0 = seconds only; nanoseconds zeroed. 220 * 1 = seconds and nanoseconds, accurate within 1/HZ. 221 * 2 = seconds and nanoseconds, truncated to microseconds. 222 * >=3 = seconds and nanoseconds, maximum precision. 223 * 224 * Note that utimes() precision is microseconds because it takes a timeval 225 * structure, so its probably best to default to USEC and not NSEC. 226 */ 227 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC, 228 TSP_USEC_PRECISE, TSP_NSEC_PRECISE }; 229 230 __read_mostly static int timestamp_precision = TSP_USEC; 231 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 232 ×tamp_precision, 0, "Precision of file timestamps"); 233 234 /* 235 * Get a current timestamp. 236 * 237 * MPSAFE 238 */ 239 void 240 vfs_timestamp(struct timespec *tsp) 241 { 242 switch (timestamp_precision) { 243 case TSP_SEC: /* seconds precision */ 244 getnanotime(tsp); 245 tsp->tv_nsec = 0; 246 break; 247 default: 248 case TSP_HZ: /* ticks precision (limit to microseconds) */ 249 getnanotime(tsp); 250 tsp->tv_nsec -= tsp->tv_nsec % 1000; 251 break; 252 case TSP_USEC: /* microseconds (ticks precision) */ 253 getnanotime(tsp); 254 tsp->tv_nsec -= tsp->tv_nsec % 1000; 255 break; 256 case TSP_NSEC: /* nanoseconds (ticks precision) */ 257 getnanotime(tsp); 258 break; 259 case TSP_USEC_PRECISE: /* microseconds (high preceision) */ 260 nanotime(tsp); 261 tsp->tv_nsec -= tsp->tv_nsec % 1000; 262 break; 263 case TSP_NSEC_PRECISE: /* nanoseconds (high precision) */ 264 nanotime(tsp); 265 break; 266 } 267 } 268 269 /* 270 * Set vnode attributes to VNOVAL 271 */ 272 void 273 vattr_null(struct vattr *vap) 274 { 275 vap->va_type = VNON; 276 vap->va_size = VNOVAL; 277 vap->va_bytes = VNOVAL; 278 vap->va_mode = VNOVAL; 279 vap->va_nlink = VNOVAL; 280 vap->va_uid = VNOVAL; 281 vap->va_gid = VNOVAL; 282 vap->va_fsid = VNOVAL; 283 vap->va_fileid = VNOVAL; 284 vap->va_blocksize = VNOVAL; 285 vap->va_rmajor = VNOVAL; 286 vap->va_rminor = VNOVAL; 287 vap->va_atime.tv_sec = VNOVAL; 288 vap->va_atime.tv_nsec = VNOVAL; 289 vap->va_mtime.tv_sec = VNOVAL; 290 vap->va_mtime.tv_nsec = VNOVAL; 291 vap->va_ctime.tv_sec = VNOVAL; 292 vap->va_ctime.tv_nsec = VNOVAL; 293 vap->va_flags = VNOVAL; 294 vap->va_gen = VNOVAL; 295 vap->va_vaflags = 0; 296 /* va_*_uuid fields are only valid if related flags are set */ 297 } 298 299 /* 300 * Flush out and invalidate all buffers associated with a vnode. 301 * 302 * vp must be locked. 303 */ 304 static int vinvalbuf_bp(struct buf *bp, void *data); 305 306 struct vinvalbuf_bp_info { 307 struct vnode *vp; 308 int slptimeo; 309 int lkflags; 310 int flags; 311 int clean; 312 }; 313 314 int 315 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) 316 { 317 struct vinvalbuf_bp_info info; 318 vm_object_t object; 319 int error; 320 321 lwkt_gettoken(&vp->v_token); 322 323 /* 324 * If we are being asked to save, call fsync to ensure that the inode 325 * is updated. 326 */ 327 if (flags & V_SAVE) { 328 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo); 329 if (error) 330 goto done; 331 if (!RB_EMPTY(&vp->v_rbdirty_tree)) { 332 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0) 333 goto done; 334 #if 0 335 /* 336 * Dirty bufs may be left or generated via races 337 * in circumstances where vinvalbuf() is called on 338 * a vnode not undergoing reclamation. Only 339 * panic if we are trying to reclaim the vnode. 340 */ 341 if ((vp->v_flag & VRECLAIMED) && 342 (bio_track_active(&vp->v_track_write) || 343 !RB_EMPTY(&vp->v_rbdirty_tree))) { 344 panic("vinvalbuf: dirty bufs"); 345 } 346 #endif 347 } 348 } 349 info.slptimeo = slptimeo; 350 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL; 351 if (slpflag & PCATCH) 352 info.lkflags |= LK_PCATCH; 353 info.flags = flags; 354 info.vp = vp; 355 356 /* 357 * Flush the buffer cache until nothing is left, wait for all I/O 358 * to complete. At least one pass is required. We might block 359 * in the pip code so we have to re-check. Order is important. 360 */ 361 do { 362 /* 363 * Flush buffer cache 364 */ 365 if (!RB_EMPTY(&vp->v_rbclean_tree)) { 366 info.clean = 1; 367 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 368 NULL, vinvalbuf_bp, &info); 369 } 370 if (!RB_EMPTY(&vp->v_rbdirty_tree)) { 371 info.clean = 0; 372 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 373 NULL, vinvalbuf_bp, &info); 374 } 375 376 /* 377 * Wait for I/O completion. 378 */ 379 bio_track_wait(&vp->v_track_write, 0, 0); 380 if ((object = vp->v_object) != NULL) 381 refcount_wait(&object->paging_in_progress, "vnvlbx"); 382 } while (bio_track_active(&vp->v_track_write) || 383 !RB_EMPTY(&vp->v_rbclean_tree) || 384 !RB_EMPTY(&vp->v_rbdirty_tree)); 385 386 /* 387 * Destroy the copy in the VM cache, too. 388 */ 389 if ((object = vp->v_object) != NULL) { 390 vm_object_page_remove(object, 0, 0, 391 (flags & V_SAVE) ? TRUE : FALSE); 392 } 393 394 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree)) 395 panic("vinvalbuf: flush failed"); 396 if (!RB_EMPTY(&vp->v_rbhash_tree)) 397 panic("vinvalbuf: flush failed, buffers still present"); 398 error = 0; 399 done: 400 lwkt_reltoken(&vp->v_token); 401 return (error); 402 } 403 404 static int 405 vinvalbuf_bp(struct buf *bp, void *data) 406 { 407 struct vinvalbuf_bp_info *info = data; 408 int error; 409 410 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 411 atomic_add_int(&bp->b_refs, 1); 412 error = BUF_TIMELOCK(bp, info->lkflags, 413 "vinvalbuf", info->slptimeo); 414 atomic_subtract_int(&bp->b_refs, 1); 415 if (error == 0) { 416 BUF_UNLOCK(bp); 417 error = ENOLCK; 418 } 419 if (error == ENOLCK) 420 return(0); 421 return (-error); 422 } 423 KKASSERT(bp->b_vp == info->vp); 424 425 /* 426 * Must check clean/dirty status after successfully locking as 427 * it may race. 428 */ 429 if ((info->clean && (bp->b_flags & B_DELWRI)) || 430 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) { 431 BUF_UNLOCK(bp); 432 return(0); 433 } 434 435 /* 436 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite() 437 * check. This code will write out the buffer, period. 438 */ 439 bremfree(bp); 440 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 441 (info->flags & V_SAVE)) { 442 cluster_awrite(bp); 443 } else if (info->flags & V_SAVE) { 444 /* 445 * Cannot set B_NOCACHE on a clean buffer as this will 446 * destroy the VM backing store which might actually 447 * be dirty (and unsynchronized). 448 */ 449 bp->b_flags |= (B_INVAL | B_RELBUF); 450 brelse(bp); 451 } else { 452 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 453 brelse(bp); 454 } 455 return(0); 456 } 457 458 /* 459 * Truncate a file's buffer and pages to a specified length. This 460 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 461 * sync activity. 462 * 463 * The vnode must be locked. 464 */ 465 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data); 466 static int vtruncbuf_bp_trunc(struct buf *bp, void *data); 467 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data); 468 static int vtruncbuf_bp_metasync(struct buf *bp, void *data); 469 470 struct vtruncbuf_info { 471 struct vnode *vp; 472 off_t truncloffset; 473 int clean; 474 }; 475 476 int 477 vtruncbuf(struct vnode *vp, off_t length, int blksize) 478 { 479 struct vtruncbuf_info info; 480 const char *filename; 481 int count; 482 483 /* 484 * Round up to the *next* block, then destroy the buffers in question. 485 * Since we are only removing some of the buffers we must rely on the 486 * scan count to determine whether a loop is necessary. 487 */ 488 if ((count = (int)(length % blksize)) != 0) 489 info.truncloffset = length + (blksize - count); 490 else 491 info.truncloffset = length; 492 info.vp = vp; 493 494 lwkt_gettoken(&vp->v_token); 495 do { 496 info.clean = 1; 497 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 498 vtruncbuf_bp_trunc_cmp, 499 vtruncbuf_bp_trunc, &info); 500 info.clean = 0; 501 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 502 vtruncbuf_bp_trunc_cmp, 503 vtruncbuf_bp_trunc, &info); 504 } while(count); 505 506 /* 507 * For safety, fsync any remaining metadata if the file is not being 508 * truncated to 0. Since the metadata does not represent the entire 509 * dirty list we have to rely on the hit count to ensure that we get 510 * all of it. 511 */ 512 if (length > 0) { 513 do { 514 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 515 vtruncbuf_bp_metasync_cmp, 516 vtruncbuf_bp_metasync, &info); 517 } while (count); 518 } 519 520 /* 521 * Clean out any left over VM backing store. 522 * 523 * It is possible to have in-progress I/O from buffers that were 524 * not part of the truncation. This should not happen if we 525 * are truncating to 0-length. 526 */ 527 vnode_pager_setsize(vp, length); 528 bio_track_wait(&vp->v_track_write, 0, 0); 529 530 /* 531 * Debugging only 532 */ 533 spin_lock(&vp->v_spin); 534 filename = TAILQ_FIRST(&vp->v_namecache) ? 535 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?"; 536 spin_unlock(&vp->v_spin); 537 538 /* 539 * Make sure no buffers were instantiated while we were trying 540 * to clean out the remaining VM pages. This could occur due 541 * to busy dirty VM pages being flushed out to disk. 542 */ 543 do { 544 info.clean = 1; 545 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, 546 vtruncbuf_bp_trunc_cmp, 547 vtruncbuf_bp_trunc, &info); 548 info.clean = 0; 549 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 550 vtruncbuf_bp_trunc_cmp, 551 vtruncbuf_bp_trunc, &info); 552 if (count) { 553 kprintf("Warning: vtruncbuf(): Had to re-clean %d " 554 "left over buffers in %s\n", count, filename); 555 } 556 } while(count); 557 558 lwkt_reltoken(&vp->v_token); 559 560 return (0); 561 } 562 563 /* 564 * The callback buffer is beyond the new file EOF and must be destroyed. 565 * Note that the compare function must conform to the RB_SCAN's requirements. 566 */ 567 static 568 int 569 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data) 570 { 571 struct vtruncbuf_info *info = data; 572 573 if (bp->b_loffset >= info->truncloffset) 574 return(0); 575 return(-1); 576 } 577 578 static 579 int 580 vtruncbuf_bp_trunc(struct buf *bp, void *data) 581 { 582 struct vtruncbuf_info *info = data; 583 584 /* 585 * Do not try to use a buffer we cannot immediately lock, but sleep 586 * anyway to prevent a livelock. The code will loop until all buffers 587 * can be acted upon. 588 * 589 * We must always revalidate the buffer after locking it to deal 590 * with MP races. 591 */ 592 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 593 atomic_add_int(&bp->b_refs, 1); 594 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) 595 BUF_UNLOCK(bp); 596 atomic_subtract_int(&bp->b_refs, 1); 597 } else if ((info->clean && (bp->b_flags & B_DELWRI)) || 598 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) || 599 bp->b_vp != info->vp || 600 vtruncbuf_bp_trunc_cmp(bp, data)) { 601 BUF_UNLOCK(bp); 602 } else { 603 bremfree(bp); 604 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE); 605 brelse(bp); 606 } 607 return(1); 608 } 609 610 /* 611 * Fsync all meta-data after truncating a file to be non-zero. Only metadata 612 * blocks (with a negative loffset) are scanned. 613 * Note that the compare function must conform to the RB_SCAN's requirements. 614 */ 615 static int 616 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused) 617 { 618 if (bp->b_loffset < 0) 619 return(0); 620 return(1); 621 } 622 623 static int 624 vtruncbuf_bp_metasync(struct buf *bp, void *data) 625 { 626 struct vtruncbuf_info *info = data; 627 628 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 629 atomic_add_int(&bp->b_refs, 1); 630 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) 631 BUF_UNLOCK(bp); 632 atomic_subtract_int(&bp->b_refs, 1); 633 } else if ((bp->b_flags & B_DELWRI) == 0 || 634 bp->b_vp != info->vp || 635 vtruncbuf_bp_metasync_cmp(bp, data)) { 636 BUF_UNLOCK(bp); 637 } else { 638 bremfree(bp); 639 if (bp->b_vp == info->vp) 640 bawrite(bp); 641 else 642 bwrite(bp); 643 } 644 return(1); 645 } 646 647 /* 648 * vfsync - implements a multipass fsync on a file which understands 649 * dependancies and meta-data. The passed vnode must be locked. The 650 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY. 651 * 652 * When fsyncing data asynchronously just do one consolidated pass starting 653 * with the most negative block number. This may not get all the data due 654 * to dependancies. 655 * 656 * When fsyncing data synchronously do a data pass, then a metadata pass, 657 * then do additional data+metadata passes to try to get all the data out. 658 * 659 * Caller must ref the vnode but does not have to lock it. 660 */ 661 static int vfsync_wait_output(struct vnode *vp, 662 int (*waitoutput)(struct vnode *, struct thread *)); 663 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused); 664 static int vfsync_data_only_cmp(struct buf *bp, void *data); 665 static int vfsync_meta_only_cmp(struct buf *bp, void *data); 666 static int vfsync_lazy_range_cmp(struct buf *bp, void *data); 667 static int vfsync_bp(struct buf *bp, void *data); 668 669 struct vfsync_info { 670 struct vnode *vp; 671 int fastpass; 672 int synchronous; 673 int syncdeps; 674 int lazycount; 675 int lazylimit; 676 int skippedbufs; 677 int (*checkdef)(struct buf *); 678 int (*cmpfunc)(struct buf *, void *); 679 }; 680 681 int 682 vfsync(struct vnode *vp, int waitfor, int passes, 683 int (*checkdef)(struct buf *), 684 int (*waitoutput)(struct vnode *, struct thread *)) 685 { 686 struct vfsync_info info; 687 int error; 688 689 bzero(&info, sizeof(info)); 690 info.vp = vp; 691 if ((info.checkdef = checkdef) == NULL) 692 info.syncdeps = 1; 693 694 lwkt_gettoken(&vp->v_token); 695 696 switch(waitfor) { 697 case MNT_LAZY | MNT_NOWAIT: 698 case MNT_LAZY: 699 /* 700 * Lazy (filesystem syncer typ) Asynchronous plus limit the 701 * number of data (not meta) pages we try to flush to 1MB. 702 * A non-zero return means that lazy limit was reached. 703 */ 704 info.lazylimit = 1024 * 1024; 705 info.syncdeps = 1; 706 info.cmpfunc = vfsync_lazy_range_cmp; 707 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 708 vfsync_lazy_range_cmp, vfsync_bp, &info); 709 info.cmpfunc = vfsync_meta_only_cmp; 710 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, 711 vfsync_meta_only_cmp, vfsync_bp, &info); 712 if (error == 0) 713 vp->v_lazyw = 0; 714 else if (!RB_EMPTY(&vp->v_rbdirty_tree)) 715 vn_syncer_add(vp, 1); 716 error = 0; 717 break; 718 case MNT_NOWAIT: 719 /* 720 * Asynchronous. Do a data-only pass and a meta-only pass. 721 */ 722 info.syncdeps = 1; 723 info.cmpfunc = vfsync_data_only_cmp; 724 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, 725 vfsync_bp, &info); 726 info.cmpfunc = vfsync_meta_only_cmp; 727 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp, 728 vfsync_bp, &info); 729 error = 0; 730 break; 731 default: 732 /* 733 * Synchronous. Do a data-only pass, then a meta-data+data 734 * pass, then additional integrated passes to try to get 735 * all the dependancies flushed. 736 */ 737 info.cmpfunc = vfsync_data_only_cmp; 738 info.fastpass = 1; 739 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, 740 vfsync_bp, &info); 741 info.fastpass = 0; 742 error = vfsync_wait_output(vp, waitoutput); 743 if (error == 0) { 744 info.skippedbufs = 0; 745 info.cmpfunc = vfsync_dummy_cmp; 746 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 747 vfsync_bp, &info); 748 error = vfsync_wait_output(vp, waitoutput); 749 if (info.skippedbufs) { 750 kprintf("Warning: vfsync skipped %d dirty " 751 "buf%s in pass2!\n", 752 info.skippedbufs, 753 ((info.skippedbufs > 1) ? "s" : "")); 754 } 755 } 756 while (error == 0 && passes > 0 && 757 !RB_EMPTY(&vp->v_rbdirty_tree) 758 ) { 759 info.skippedbufs = 0; 760 if (--passes == 0) { 761 info.synchronous = 1; 762 info.syncdeps = 1; 763 } 764 info.cmpfunc = vfsync_dummy_cmp; 765 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 766 vfsync_bp, &info); 767 if (error < 0) 768 error = -error; 769 info.syncdeps = 1; 770 if (error == 0) 771 error = vfsync_wait_output(vp, waitoutput); 772 if (info.skippedbufs && passes == 0) { 773 kprintf("Warning: vfsync skipped %d dirty " 774 "buf%s in final pass!\n", 775 info.skippedbufs, 776 ((info.skippedbufs > 1) ? "s" : "")); 777 } 778 } 779 #if 0 780 /* 781 * This case can occur normally because vnode lock might 782 * not be held. 783 */ 784 if (!RB_EMPTY(&vp->v_rbdirty_tree)) 785 kprintf("dirty bufs left after final pass\n"); 786 #endif 787 break; 788 } 789 lwkt_reltoken(&vp->v_token); 790 791 return(error); 792 } 793 794 static int 795 vfsync_wait_output(struct vnode *vp, 796 int (*waitoutput)(struct vnode *, struct thread *)) 797 { 798 int error; 799 800 error = bio_track_wait(&vp->v_track_write, 0, 0); 801 if (waitoutput) 802 error = waitoutput(vp, curthread); 803 return(error); 804 } 805 806 static int 807 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused) 808 { 809 return(0); 810 } 811 812 static int 813 vfsync_data_only_cmp(struct buf *bp, void *data) 814 { 815 if (bp->b_loffset < 0) 816 return(-1); 817 return(0); 818 } 819 820 static int 821 vfsync_meta_only_cmp(struct buf *bp, void *data) 822 { 823 if (bp->b_loffset < 0) 824 return(0); 825 return(1); 826 } 827 828 static int 829 vfsync_lazy_range_cmp(struct buf *bp, void *data) 830 { 831 struct vfsync_info *info = data; 832 833 if (bp->b_loffset < info->vp->v_lazyw) 834 return(-1); 835 return(0); 836 } 837 838 static int 839 vfsync_bp(struct buf *bp, void *data) 840 { 841 struct vfsync_info *info = data; 842 struct vnode *vp = info->vp; 843 int error; 844 845 if (info->fastpass) { 846 /* 847 * Ignore buffers that we cannot immediately lock. 848 */ 849 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 850 /* 851 * Removed BUF_TIMELOCK(..., 1), even a 1-tick 852 * delay can mess up performance 853 * 854 * Another reason is that during a dirty-buffer 855 * scan a clustered write can start I/O on buffers 856 * ahead of the scan, causing the scan to not 857 * get a lock here. Usually this means the write 858 * is already in progress so, in fact, we *want* 859 * to skip the buffer. 860 */ 861 ++info->skippedbufs; 862 return(0); 863 } 864 } else if (info->synchronous == 0) { 865 /* 866 * Normal pass, give the buffer a little time to become 867 * available to us. 868 */ 869 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst2", hz / 10)) { 870 ++info->skippedbufs; 871 return(0); 872 } 873 } else { 874 /* 875 * Synchronous pass, give the buffer a lot of time before 876 * giving up. 877 */ 878 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst3", hz * 10)) { 879 ++info->skippedbufs; 880 return(0); 881 } 882 } 883 884 /* 885 * We must revalidate the buffer after locking. 886 */ 887 if ((bp->b_flags & B_DELWRI) == 0 || 888 bp->b_vp != info->vp || 889 info->cmpfunc(bp, data)) { 890 BUF_UNLOCK(bp); 891 return(0); 892 } 893 894 /* 895 * If syncdeps is not set we do not try to write buffers which have 896 * dependancies. 897 */ 898 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) { 899 BUF_UNLOCK(bp); 900 return(0); 901 } 902 903 /* 904 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer 905 * has been written but an additional handshake with the device 906 * is required before we can dispose of the buffer. We have no idea 907 * how to do this so we have to skip these buffers. 908 */ 909 if (bp->b_flags & B_NEEDCOMMIT) { 910 BUF_UNLOCK(bp); 911 return(0); 912 } 913 914 /* 915 * Ask bioops if it is ok to sync. If not the VFS may have 916 * set B_LOCKED so we have to cycle the buffer. 917 */ 918 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) { 919 bremfree(bp); 920 brelse(bp); 921 return(0); 922 } 923 924 if (info->synchronous) { 925 /* 926 * Synchronous flush. An error may be returned and will 927 * stop the scan. 928 */ 929 bremfree(bp); 930 error = bwrite(bp); 931 } else { 932 /* 933 * Asynchronous flush. We use the error return to support 934 * MNT_LAZY flushes. 935 * 936 * In low-memory situations we revert to synchronous 937 * operation. This should theoretically prevent the I/O 938 * path from exhausting memory in a non-recoverable way. 939 */ 940 vp->v_lazyw = bp->b_loffset; 941 bremfree(bp); 942 if (vm_paging_min()) { 943 /* low memory */ 944 info->lazycount += bp->b_bufsize; 945 bwrite(bp); 946 } else { 947 /* normal */ 948 info->lazycount += cluster_awrite(bp); 949 waitrunningbufspace(); 950 /*vm_wait_nominal();*/ 951 } 952 if (info->lazylimit && info->lazycount >= info->lazylimit) 953 error = 1; 954 else 955 error = 0; 956 } 957 return(-error); 958 } 959 960 /* 961 * Associate a buffer with a vnode. 962 * 963 * MPSAFE 964 */ 965 int 966 bgetvp(struct vnode *vp, struct buf *bp, int testsize) 967 { 968 KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 969 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0); 970 971 /* 972 * Insert onto list for new vnode. 973 */ 974 lwkt_gettoken(&vp->v_token); 975 976 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) { 977 lwkt_reltoken(&vp->v_token); 978 return (EEXIST); 979 } 980 981 /* 982 * Diagnostics (mainly for HAMMER debugging). Check for 983 * overlapping buffers. 984 */ 985 if (check_buf_overlap) { 986 struct buf *bx; 987 bx = buf_rb_hash_RB_PREV(bp); 988 if (bx) { 989 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) { 990 kprintf("bgetvp: overlapl %016jx/%d %016jx " 991 "bx %p bp %p\n", 992 (intmax_t)bx->b_loffset, 993 bx->b_bufsize, 994 (intmax_t)bp->b_loffset, 995 bx, bp); 996 if (check_buf_overlap > 1) 997 panic("bgetvp - overlapping buffer"); 998 } 999 } 1000 bx = buf_rb_hash_RB_NEXT(bp); 1001 if (bx) { 1002 if (bp->b_loffset + testsize > bx->b_loffset) { 1003 kprintf("bgetvp: overlapr %016jx/%d %016jx " 1004 "bp %p bx %p\n", 1005 (intmax_t)bp->b_loffset, 1006 testsize, 1007 (intmax_t)bx->b_loffset, 1008 bp, bx); 1009 if (check_buf_overlap > 1) 1010 panic("bgetvp - overlapping buffer"); 1011 } 1012 } 1013 } 1014 bp->b_vp = vp; 1015 bp->b_flags |= B_HASHED; 1016 bp->b_flags |= B_VNCLEAN; 1017 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) 1018 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp); 1019 /*vhold(vp);*/ 1020 lwkt_reltoken(&vp->v_token); 1021 return(0); 1022 } 1023 1024 /* 1025 * Disassociate a buffer from a vnode. 1026 * 1027 * MPSAFE 1028 */ 1029 void 1030 brelvp(struct buf *bp) 1031 { 1032 struct vnode *vp; 1033 1034 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1035 1036 /* 1037 * Delete from old vnode list, if on one. 1038 */ 1039 vp = bp->b_vp; 1040 lwkt_gettoken(&vp->v_token); 1041 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) { 1042 if (bp->b_flags & B_VNDIRTY) 1043 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); 1044 else 1045 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); 1046 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN); 1047 } 1048 if (bp->b_flags & B_HASHED) { 1049 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp); 1050 bp->b_flags &= ~B_HASHED; 1051 } 1052 1053 /* 1054 * Only remove from synclist when no dirty buffers are left AND 1055 * the VFS has not flagged the vnode's inode as being dirty. 1056 */ 1057 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST && 1058 RB_EMPTY(&vp->v_rbdirty_tree)) { 1059 vn_syncer_remove(vp, 0); 1060 } 1061 bp->b_vp = NULL; 1062 1063 lwkt_reltoken(&vp->v_token); 1064 1065 /*vdrop(vp);*/ 1066 } 1067 1068 /* 1069 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI. 1070 * This routine is called when the state of the B_DELWRI bit is changed. 1071 * 1072 * Must be called with vp->v_token held. 1073 * MPSAFE 1074 */ 1075 void 1076 reassignbuf(struct buf *bp) 1077 { 1078 struct vnode *vp = bp->b_vp; 1079 int delay; 1080 1081 ASSERT_LWKT_TOKEN_HELD(&vp->v_token); 1082 ++reassignbufcalls; 1083 1084 /* 1085 * B_PAGING flagged buffers cannot be reassigned because their vp 1086 * is not fully linked in. 1087 */ 1088 if (bp->b_flags & B_PAGING) 1089 panic("cannot reassign paging buffer"); 1090 1091 if (bp->b_flags & B_DELWRI) { 1092 /* 1093 * Move to the dirty list, add the vnode to the worklist 1094 */ 1095 if (bp->b_flags & B_VNCLEAN) { 1096 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); 1097 bp->b_flags &= ~B_VNCLEAN; 1098 } 1099 if ((bp->b_flags & B_VNDIRTY) == 0) { 1100 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) { 1101 panic("reassignbuf: dup lblk vp %p bp %p", 1102 vp, bp); 1103 } 1104 bp->b_flags |= B_VNDIRTY; 1105 } 1106 if ((vp->v_flag & VONWORKLST) == 0) { 1107 switch (vp->v_type) { 1108 case VDIR: 1109 delay = dirdelay; 1110 break; 1111 case VCHR: 1112 case VBLK: 1113 if (vp->v_rdev && 1114 vp->v_rdev->si_mountpoint != NULL) { 1115 delay = metadelay; 1116 break; 1117 } 1118 /* fall through */ 1119 default: 1120 delay = filedelay; 1121 } 1122 vn_syncer_add(vp, delay); 1123 } 1124 } else { 1125 /* 1126 * Move to the clean list, remove the vnode from the worklist 1127 * if no dirty blocks remain. 1128 */ 1129 if (bp->b_flags & B_VNDIRTY) { 1130 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); 1131 bp->b_flags &= ~B_VNDIRTY; 1132 } 1133 if ((bp->b_flags & B_VNCLEAN) == 0) { 1134 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) { 1135 panic("reassignbuf: dup lblk vp %p bp %p", 1136 vp, bp); 1137 } 1138 bp->b_flags |= B_VNCLEAN; 1139 } 1140 1141 /* 1142 * Only remove from synclist when no dirty buffers are left 1143 * AND the VFS has not flagged the vnode's inode as being 1144 * dirty. 1145 */ 1146 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == 1147 VONWORKLST && 1148 RB_EMPTY(&vp->v_rbdirty_tree)) { 1149 vn_syncer_remove(vp, 0); 1150 } 1151 } 1152 } 1153 1154 /* 1155 * Create a vnode for a block device. Used for mounting the root file 1156 * system. 1157 * 1158 * A vref()'d vnode is returned. 1159 */ 1160 extern struct vop_ops *devfs_vnode_dev_vops_p; 1161 int 1162 bdevvp(cdev_t dev, struct vnode **vpp) 1163 { 1164 struct vnode *vp; 1165 struct vnode *nvp; 1166 int error; 1167 1168 if (dev == NULL) { 1169 *vpp = NULLVP; 1170 return (ENXIO); 1171 } 1172 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p, 1173 &nvp, 0, 0); 1174 if (error) { 1175 *vpp = NULLVP; 1176 return (error); 1177 } 1178 vp = nvp; 1179 vp->v_type = VCHR; 1180 #if 0 1181 vp->v_rdev = dev; 1182 #endif 1183 v_associate_rdev(vp, dev); 1184 vp->v_umajor = dev->si_umajor; 1185 vp->v_uminor = dev->si_uminor; 1186 vx_unlock(vp); 1187 *vpp = vp; 1188 return (0); 1189 } 1190 1191 int 1192 v_associate_rdev(struct vnode *vp, cdev_t dev) 1193 { 1194 if (dev == NULL) 1195 return(ENXIO); 1196 if (dev_is_good(dev) == 0) 1197 return(ENXIO); 1198 KKASSERT(vp->v_rdev == NULL); 1199 vp->v_rdev = reference_dev(dev); 1200 lwkt_gettoken(&spechash_token); 1201 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext); 1202 lwkt_reltoken(&spechash_token); 1203 return(0); 1204 } 1205 1206 void 1207 v_release_rdev(struct vnode *vp) 1208 { 1209 cdev_t dev; 1210 1211 if ((dev = vp->v_rdev) != NULL) { 1212 lwkt_gettoken(&spechash_token); 1213 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext); 1214 vp->v_rdev = NULL; 1215 release_dev(dev); 1216 lwkt_reltoken(&spechash_token); 1217 } 1218 } 1219 1220 /* 1221 * Add a vnode to the alias list hung off the cdev_t. We only associate 1222 * the device number with the vnode. The actual device is not associated 1223 * until the vnode is opened (usually in spec_open()), and will be 1224 * disassociated on last close. 1225 */ 1226 void 1227 addaliasu(struct vnode *nvp, int x, int y) 1228 { 1229 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 1230 panic("addaliasu on non-special vnode"); 1231 nvp->v_umajor = x; 1232 nvp->v_uminor = y; 1233 } 1234 1235 /* 1236 * Simple call that a filesystem can make to try to get rid of a 1237 * vnode. It will fail if anyone is referencing the vnode (including 1238 * the caller). 1239 * 1240 * The filesystem can check whether its in-memory inode structure still 1241 * references the vp on return. 1242 * 1243 * May only be called if the vnode is in a known state (i.e. being prevented 1244 * from being deallocated by some other condition such as a vfs inode hold). 1245 * 1246 * This call might not succeed. 1247 */ 1248 void 1249 vclean_unlocked(struct vnode *vp) 1250 { 1251 vx_get(vp); 1252 if (VREFCNT(vp) <= 1) 1253 vgone_vxlocked(vp); 1254 vx_put(vp); 1255 } 1256 1257 /* 1258 * Disassociate a vnode from its underlying filesystem. 1259 * 1260 * The vnode must be VX locked and referenced. In all normal situations 1261 * there are no active references. If vclean_vxlocked() is called while 1262 * there are active references, the vnode is being ripped out and we have 1263 * to call VOP_CLOSE() as appropriate before we can reclaim it. 1264 */ 1265 static void 1266 vclean_vxlocked(struct vnode *vp, int flags) 1267 { 1268 int active; 1269 int n; 1270 vm_object_t object; 1271 struct namecache *ncp; 1272 1273 /* 1274 * If the vnode has already been reclaimed we have nothing to do. 1275 */ 1276 if (vp->v_flag & VRECLAIMED) 1277 return; 1278 1279 /* 1280 * Set flag to interlock operation, flag finalization to ensure 1281 * that the vnode winds up on the inactive list, and set v_act to 0. 1282 */ 1283 vsetflags(vp, VRECLAIMED); 1284 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE); 1285 vp->v_act = 0; 1286 1287 if (verbose_reclaims) { 1288 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) 1289 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name); 1290 } 1291 1292 /* 1293 * Scrap the vfs cache 1294 */ 1295 while (cache_inval_vp(vp, 0) != 0) { 1296 kprintf("Warning: vnode %p clean/cache_resolution " 1297 "race detected\n", vp); 1298 tsleep(vp, 0, "vclninv", 2); 1299 } 1300 1301 /* 1302 * Check to see if the vnode is in use. If so we have to reference it 1303 * before we clean it out so that its count cannot fall to zero and 1304 * generate a race against ourselves to recycle it. 1305 */ 1306 active = (VREFCNT(vp) > 0); 1307 1308 /* 1309 * Clean out any buffers associated with the vnode and destroy its 1310 * object, if it has one. 1311 */ 1312 vinvalbuf(vp, V_SAVE, 0, 0); 1313 1314 /* 1315 * If purging an active vnode (typically during a forced unmount 1316 * or reboot), it must be closed and deactivated before being 1317 * reclaimed. This isn't really all that safe, but what can 1318 * we do? XXX. 1319 * 1320 * Note that neither of these routines unlocks the vnode. 1321 */ 1322 if (active && (flags & DOCLOSE)) { 1323 while ((n = vp->v_opencount) != 0) { 1324 if (vp->v_writecount) 1325 VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL); 1326 else 1327 VOP_CLOSE(vp, FNONBLOCK, NULL); 1328 if (vp->v_opencount == n) { 1329 kprintf("Warning: unable to force-close" 1330 " vnode %p\n", vp); 1331 break; 1332 } 1333 } 1334 } 1335 1336 /* 1337 * If the vnode has not been deactivated, deactivated it. Deactivation 1338 * can create new buffers and VM pages so we have to call vinvalbuf() 1339 * again to make sure they all get flushed. 1340 * 1341 * This can occur if a file with a link count of 0 needs to be 1342 * truncated. 1343 * 1344 * If the vnode is already dead don't try to deactivate it. 1345 */ 1346 if ((vp->v_flag & VINACTIVE) == 0) { 1347 vsetflags(vp, VINACTIVE); 1348 if (vp->v_mount) 1349 VOP_INACTIVE(vp); 1350 vinvalbuf(vp, V_SAVE, 0, 0); 1351 } 1352 1353 /* 1354 * If the vnode has an object, destroy it. 1355 */ 1356 while ((object = vp->v_object) != NULL) { 1357 vm_object_hold(object); 1358 if (object == vp->v_object) 1359 break; 1360 vm_object_drop(object); 1361 } 1362 1363 if (object != NULL) { 1364 if (object->ref_count == 0) { 1365 if ((object->flags & OBJ_DEAD) == 0) 1366 vm_object_terminate(object); 1367 vm_object_drop(object); 1368 vclrflags(vp, VOBJBUF); 1369 } else { 1370 vm_pager_deallocate(object); 1371 vclrflags(vp, VOBJBUF); 1372 vm_object_drop(object); 1373 } 1374 } 1375 KKASSERT((vp->v_flag & VOBJBUF) == 0); 1376 1377 if (vp->v_flag & VOBJDIRTY) 1378 vclrobjdirty(vp); 1379 1380 /* 1381 * Reclaim the vnode if not already dead. 1382 */ 1383 if (vp->v_mount && VOP_RECLAIM(vp)) 1384 panic("vclean: cannot reclaim"); 1385 1386 /* 1387 * Done with purge, notify sleepers of the grim news. 1388 */ 1389 vp->v_ops = &dead_vnode_vops_p; 1390 vn_gone(vp); 1391 vp->v_tag = VT_NON; 1392 1393 /* 1394 * If we are destroying an active vnode, reactivate it now that 1395 * we have reassociated it with deadfs. This prevents the system 1396 * from crashing on the vnode due to it being unexpectedly marked 1397 * as inactive or reclaimed. 1398 */ 1399 if (active && (flags & DOCLOSE)) { 1400 vclrflags(vp, VINACTIVE | VRECLAIMED); 1401 } 1402 } 1403 1404 /* 1405 * Eliminate all activity associated with the requested vnode 1406 * and with all vnodes aliased to the requested vnode. 1407 * 1408 * The vnode must be referenced but should not be locked. 1409 */ 1410 int 1411 vrevoke(struct vnode *vp, struct ucred *cred) 1412 { 1413 struct vnode *vq; 1414 struct vnode *vqn; 1415 cdev_t dev; 1416 int error; 1417 1418 /* 1419 * If the vnode has a device association, scrap all vnodes associated 1420 * with the device. Don't let the device disappear on us while we 1421 * are scrapping the vnodes. 1422 * 1423 * The passed vp will probably show up in the list, do not VX lock 1424 * it twice! 1425 * 1426 * Releasing the vnode's rdev here can mess up specfs's call to 1427 * device close, so don't do it. The vnode has been disassociated 1428 * and the device will be closed after the last ref on the related 1429 * fp goes away (if not still open by e.g. the kernel). 1430 */ 1431 if (vp->v_type != VCHR) { 1432 error = fdrevoke(vp, DTYPE_VNODE, cred); 1433 return (error); 1434 } 1435 if ((dev = vp->v_rdev) == NULL) { 1436 return(0); 1437 } 1438 reference_dev(dev); 1439 lwkt_gettoken(&spechash_token); 1440 1441 restart: 1442 vqn = SLIST_FIRST(&dev->si_hlist); 1443 if (vqn) 1444 vhold(vqn); 1445 while ((vq = vqn) != NULL) { 1446 if (VREFCNT(vq) > 0) { 1447 vref(vq); 1448 fdrevoke(vq, DTYPE_VNODE, cred); 1449 /*v_release_rdev(vq);*/ 1450 vrele(vq); 1451 if (vq->v_rdev != dev) { 1452 vdrop(vq); 1453 goto restart; 1454 } 1455 } 1456 vqn = SLIST_NEXT(vq, v_cdevnext); 1457 if (vqn) 1458 vhold(vqn); 1459 vdrop(vq); 1460 } 1461 lwkt_reltoken(&spechash_token); 1462 dev_drevoke(dev); 1463 release_dev(dev); 1464 return (0); 1465 } 1466 1467 /* 1468 * This is called when the object underlying a vnode is being destroyed, 1469 * such as in a remove(). Try to recycle the vnode immediately if the 1470 * only active reference is our reference. 1471 * 1472 * Directory vnodes in the namecache with children cannot be immediately 1473 * recycled because numerous VOP_N*() ops require them to be stable. 1474 * 1475 * To avoid recursive recycling from VOP_INACTIVE implemenetations this 1476 * function is a NOP if VRECLAIMED is already set. 1477 */ 1478 int 1479 vrecycle(struct vnode *vp) 1480 { 1481 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) { 1482 if (cache_inval_vp_nonblock(vp)) 1483 return(0); 1484 vgone_vxlocked(vp); 1485 return (1); 1486 } 1487 return (0); 1488 } 1489 1490 /* 1491 * Return the maximum I/O size allowed for strategy calls on VP. 1492 * 1493 * If vp is VCHR or VBLK we dive the device, otherwise we use 1494 * the vp's mount info. 1495 * 1496 * The returned value is clamped at MAXPHYS as most callers cannot use 1497 * buffers larger than that size. 1498 */ 1499 int 1500 vmaxiosize(struct vnode *vp) 1501 { 1502 int maxiosize; 1503 1504 if (vp->v_type == VBLK || vp->v_type == VCHR) 1505 maxiosize = vp->v_rdev->si_iosize_max; 1506 else 1507 maxiosize = vp->v_mount->mnt_iosize_max; 1508 1509 if (maxiosize > MAXPHYS) 1510 maxiosize = MAXPHYS; 1511 return (maxiosize); 1512 } 1513 1514 /* 1515 * Eliminate all activity associated with a vnode in preparation for 1516 * destruction. 1517 * 1518 * The vnode must be VX locked and refd and will remain VX locked and refd 1519 * on return. This routine may be called with the vnode in any state, as 1520 * long as it is VX locked. The vnode will be cleaned out and marked 1521 * VRECLAIMED but will not actually be reused until all existing refs and 1522 * holds go away. 1523 * 1524 * NOTE: This routine may be called on a vnode which has not yet been 1525 * already been deactivated (VOP_INACTIVE), or on a vnode which has 1526 * already been reclaimed. 1527 * 1528 * This routine is not responsible for placing us back on the freelist. 1529 * Instead, it happens automatically when the caller releases the VX lock 1530 * (assuming there aren't any other references). 1531 */ 1532 void 1533 vgone_vxlocked(struct vnode *vp) 1534 { 1535 /* 1536 * assert that the VX lock is held. This is an absolute requirement 1537 * now for vgone_vxlocked() to be called. 1538 */ 1539 KKASSERT(lockinuse(&vp->v_lock)); 1540 1541 /* 1542 * Clean out the filesystem specific data and set the VRECLAIMED 1543 * bit. Also deactivate the vnode if necessary. 1544 * 1545 * The vnode should have automatically been removed from the syncer 1546 * list as syncer/dirty flags cleared during the cleaning. 1547 */ 1548 vclean_vxlocked(vp, DOCLOSE); 1549 1550 /* 1551 * Normally panic if the vnode is still dirty, unless we are doing 1552 * a forced unmount (tmpfs typically). 1553 */ 1554 if (vp->v_flag & VONWORKLST) { 1555 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) { 1556 /* force removal */ 1557 vn_syncer_remove(vp, 1); 1558 } else { 1559 panic("vp %p still dirty in vgone after flush", vp); 1560 } 1561 } 1562 1563 /* 1564 * Delete from old mount point vnode list, if on one. 1565 */ 1566 if (vp->v_mount != NULL) { 1567 KKASSERT(vp->v_data == NULL); 1568 insmntque(vp, NULL); 1569 } 1570 1571 /* 1572 * If special device, remove it from special device alias list 1573 * if it is on one. This should normally only occur if a vnode is 1574 * being revoked as the device should otherwise have been released 1575 * naturally. 1576 */ 1577 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) { 1578 v_release_rdev(vp); 1579 } 1580 1581 /* 1582 * Set us to VBAD 1583 */ 1584 vp->v_type = VBAD; 1585 } 1586 1587 /* 1588 * Calculate the total number of references to a special device. This 1589 * routine may only be called for VBLK and VCHR vnodes since v_rdev is 1590 * an overloaded field. Since dev_from_devid() can now return NULL, we 1591 * have to check for a NULL v_rdev. 1592 */ 1593 int 1594 count_dev(cdev_t dev) 1595 { 1596 struct vnode *vp; 1597 int count = 0; 1598 1599 if (SLIST_FIRST(&dev->si_hlist)) { 1600 lwkt_gettoken(&spechash_token); 1601 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) { 1602 count += vp->v_opencount; 1603 } 1604 lwkt_reltoken(&spechash_token); 1605 } 1606 return(count); 1607 } 1608 1609 int 1610 vcount(struct vnode *vp) 1611 { 1612 if (vp->v_rdev == NULL) 1613 return(0); 1614 return(count_dev(vp->v_rdev)); 1615 } 1616 1617 /* 1618 * Initialize VMIO for a vnode. This routine MUST be called before a 1619 * VFS can issue buffer cache ops on a vnode. It is typically called 1620 * when a vnode is initialized from its inode. 1621 */ 1622 int 1623 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff) 1624 { 1625 vm_object_t object; 1626 int error = 0; 1627 1628 object = vp->v_object; 1629 if (object) { 1630 vm_object_hold(object); 1631 KKASSERT(vp->v_object == object); 1632 } 1633 1634 if (object == NULL) { 1635 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff); 1636 1637 /* 1638 * Dereference the reference we just created. This assumes 1639 * that the object is associated with the vp. Allow it to 1640 * have zero refs. It cannot be destroyed as long as it 1641 * is associated with the vnode. 1642 */ 1643 vm_object_hold(object); 1644 atomic_add_int(&object->ref_count, -1); 1645 vrele(vp); 1646 } else { 1647 KKASSERT((object->flags & OBJ_DEAD) == 0); 1648 } 1649 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object")); 1650 vsetflags(vp, VOBJBUF); 1651 vm_object_drop(object); 1652 1653 return (error); 1654 } 1655 1656 1657 /* 1658 * Print out a description of a vnode. 1659 */ 1660 static char *typename[] = 1661 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 1662 1663 void 1664 vprint(char *label, struct vnode *vp) 1665 { 1666 char buf[96]; 1667 1668 if (label != NULL) 1669 kprintf("%s: %p: ", label, (void *)vp); 1670 else 1671 kprintf("%p: ", (void *)vp); 1672 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,", 1673 typename[vp->v_type], 1674 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs); 1675 buf[0] = '\0'; 1676 if (vp->v_flag & VROOT) 1677 strcat(buf, "|VROOT"); 1678 if (vp->v_flag & VPFSROOT) 1679 strcat(buf, "|VPFSROOT"); 1680 if (vp->v_flag & VTEXT) 1681 strcat(buf, "|VTEXT"); 1682 if (vp->v_flag & VSYSTEM) 1683 strcat(buf, "|VSYSTEM"); 1684 if (vp->v_flag & VOBJBUF) 1685 strcat(buf, "|VOBJBUF"); 1686 if (buf[0] != '\0') 1687 kprintf(" flags (%s)", &buf[1]); 1688 if (vp->v_data == NULL) { 1689 kprintf("\n"); 1690 } else { 1691 kprintf("\n\t"); 1692 VOP_PRINT(vp); 1693 } 1694 } 1695 1696 /* 1697 * Do the usual access checking. 1698 * file_mode, uid and gid are from the vnode in question, 1699 * while acc_mode and cred are from the VOP_ACCESS parameter list 1700 */ 1701 int 1702 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 1703 mode_t acc_mode, struct ucred *cred) 1704 { 1705 mode_t mask; 1706 int ismember; 1707 1708 /* 1709 * Super-user always gets read/write access, but execute access depends 1710 * on at least one execute bit being set. 1711 */ 1712 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) { 1713 if ((acc_mode & VEXEC) && type != VDIR && 1714 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 1715 return (EACCES); 1716 return (0); 1717 } 1718 1719 mask = 0; 1720 1721 /* Otherwise, check the owner. */ 1722 if (cred->cr_uid == uid) { 1723 if (acc_mode & VEXEC) 1724 mask |= S_IXUSR; 1725 if (acc_mode & VREAD) 1726 mask |= S_IRUSR; 1727 if (acc_mode & VWRITE) 1728 mask |= S_IWUSR; 1729 return ((file_mode & mask) == mask ? 0 : EACCES); 1730 } 1731 1732 /* Otherwise, check the groups. */ 1733 ismember = groupmember(gid, cred); 1734 if (cred->cr_svgid == gid || ismember) { 1735 if (acc_mode & VEXEC) 1736 mask |= S_IXGRP; 1737 if (acc_mode & VREAD) 1738 mask |= S_IRGRP; 1739 if (acc_mode & VWRITE) 1740 mask |= S_IWGRP; 1741 return ((file_mode & mask) == mask ? 0 : EACCES); 1742 } 1743 1744 /* Otherwise, check everyone else. */ 1745 if (acc_mode & VEXEC) 1746 mask |= S_IXOTH; 1747 if (acc_mode & VREAD) 1748 mask |= S_IROTH; 1749 if (acc_mode & VWRITE) 1750 mask |= S_IWOTH; 1751 return ((file_mode & mask) == mask ? 0 : EACCES); 1752 } 1753 1754 #ifdef DDB 1755 #include <ddb/ddb.h> 1756 1757 static int db_show_locked_vnodes(struct mount *mp, void *data); 1758 1759 /* 1760 * List all of the locked vnodes in the system. 1761 * Called when debugging the kernel. 1762 */ 1763 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes) 1764 { 1765 kprintf("Locked vnodes\n"); 1766 mountlist_scan(db_show_locked_vnodes, NULL, 1767 MNTSCAN_FORWARD|MNTSCAN_NOBUSY); 1768 } 1769 1770 static int 1771 db_show_locked_vnodes(struct mount *mp, void *data __unused) 1772 { 1773 struct vnode *vp; 1774 1775 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 1776 if (vn_islocked(vp)) 1777 vprint(NULL, vp); 1778 } 1779 return(0); 1780 } 1781 #endif 1782 1783 /* 1784 * Top level filesystem related information gathering. 1785 */ 1786 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS); 1787 1788 static int 1789 vfs_sysctl(SYSCTL_HANDLER_ARGS) 1790 { 1791 int *name = (int *)arg1 - 1; /* XXX */ 1792 u_int namelen = arg2 + 1; /* XXX */ 1793 struct vfsconf *vfsp; 1794 int maxtypenum; 1795 1796 #if 1 || defined(COMPAT_PRELITE2) 1797 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 1798 if (namelen == 1) 1799 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 1800 #endif 1801 1802 #ifdef notyet 1803 /* all sysctl names at this level are at least name and field */ 1804 if (namelen < 2) 1805 return (ENOTDIR); /* overloaded */ 1806 if (name[0] != VFS_GENERIC) { 1807 vfsp = vfsconf_find_by_typenum(name[0]); 1808 if (vfsp == NULL) 1809 return (EOPNOTSUPP); 1810 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 1811 oldp, oldlenp, newp, newlen, p)); 1812 } 1813 #endif 1814 switch (name[1]) { 1815 case VFS_MAXTYPENUM: 1816 if (namelen != 2) 1817 return (ENOTDIR); 1818 maxtypenum = vfsconf_get_maxtypenum(); 1819 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum))); 1820 case VFS_CONF: 1821 if (namelen != 3) 1822 return (ENOTDIR); /* overloaded */ 1823 vfsp = vfsconf_find_by_typenum(name[2]); 1824 if (vfsp == NULL) 1825 return (EOPNOTSUPP); 1826 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp)); 1827 } 1828 return (EOPNOTSUPP); 1829 } 1830 1831 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl, 1832 "Generic filesystem"); 1833 1834 #if 1 || defined(COMPAT_PRELITE2) 1835 1836 static int 1837 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data) 1838 { 1839 int error; 1840 struct ovfsconf ovfs; 1841 struct sysctl_req *req = (struct sysctl_req*) data; 1842 1843 bzero(&ovfs, sizeof(ovfs)); 1844 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 1845 strcpy(ovfs.vfc_name, vfsp->vfc_name); 1846 ovfs.vfc_index = vfsp->vfc_typenum; 1847 ovfs.vfc_refcount = vfsp->vfc_refcount; 1848 ovfs.vfc_flags = vfsp->vfc_flags; 1849 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 1850 if (error) 1851 return error; /* abort iteration with error code */ 1852 else 1853 return 0; /* continue iterating with next element */ 1854 } 1855 1856 static int 1857 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 1858 { 1859 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req); 1860 } 1861 1862 #endif /* 1 || COMPAT_PRELITE2 */ 1863 1864 /* 1865 * Check to see if a filesystem is mounted on a block device. 1866 */ 1867 int 1868 vfs_mountedon(struct vnode *vp) 1869 { 1870 cdev_t dev; 1871 1872 dev = vp->v_rdev; 1873 if (dev != NULL && dev->si_mountpoint) 1874 return (EBUSY); 1875 return (0); 1876 } 1877 1878 /* 1879 * Unmount all filesystems. The list is traversed in reverse order 1880 * of mounting to avoid dependencies. 1881 * 1882 * We want the umountall to be able to break out of its loop if a 1883 * failure occurs, after scanning all possible mounts, so the callback 1884 * returns 0 on error. 1885 * 1886 * NOTE: Do not call mountlist_remove(mp) on error any more, this will 1887 * confuse mountlist_scan()'s unbusy check. 1888 */ 1889 static int vfs_umountall_callback(struct mount *mp, void *data); 1890 1891 void 1892 vfs_unmountall(int halting) 1893 { 1894 int count; 1895 1896 do { 1897 count = mountlist_scan(vfs_umountall_callback, &halting, 1898 MNTSCAN_REVERSE|MNTSCAN_NOBUSY); 1899 } while (count); 1900 } 1901 1902 static 1903 int 1904 vfs_umountall_callback(struct mount *mp, void *data) 1905 { 1906 int error; 1907 int halting = *(int *)data; 1908 1909 /* 1910 * NOTE: When halting, dounmount will disconnect but leave 1911 * certain mount points intact. e.g. devfs. 1912 */ 1913 error = dounmount(mp, MNT_FORCE, halting); 1914 if (error) { 1915 kprintf("unmount of filesystem mounted from %s failed (", 1916 mp->mnt_stat.f_mntfromname); 1917 if (error == EBUSY) 1918 kprintf("BUSY)\n"); 1919 else 1920 kprintf("%d)\n", error); 1921 return 0; 1922 } else { 1923 return 1; 1924 } 1925 } 1926 1927 /* 1928 * Checks the mount flags for parameter mp and put the names comma-separated 1929 * into a string buffer buf with a size limit specified by len. 1930 * 1931 * It returns the number of bytes written into buf, and (*errorp) will be 1932 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was 1933 * not large enough). The buffer will be 0-terminated if len was not 0. 1934 */ 1935 size_t 1936 vfs_flagstostr(int flags, const struct mountctl_opt *optp, 1937 char *buf, size_t len, int *errorp) 1938 { 1939 static const struct mountctl_opt optnames[] = { 1940 { MNT_RDONLY, "read-only" }, 1941 { MNT_SYNCHRONOUS, "synchronous" }, 1942 { MNT_NOEXEC, "noexec" }, 1943 { MNT_NOSUID, "nosuid" }, 1944 { MNT_NODEV, "nodev" }, 1945 { MNT_AUTOMOUNTED, "automounted" }, 1946 { MNT_ASYNC, "asynchronous" }, 1947 { MNT_SUIDDIR, "suiddir" }, 1948 { MNT_SOFTDEP, "soft-updates" }, 1949 { MNT_NOSYMFOLLOW, "nosymfollow" }, 1950 { MNT_TRIM, "trim" }, 1951 { MNT_NOATIME, "noatime" }, 1952 { MNT_NOCLUSTERR, "noclusterr" }, 1953 { MNT_NOCLUSTERW, "noclusterw" }, 1954 { MNT_EXRDONLY, "NFS read-only" }, 1955 { MNT_EXPORTED, "NFS exported" }, 1956 /* Remaining NFS flags could come here */ 1957 { MNT_LOCAL, "local" }, 1958 { MNT_QUOTA, "with-quotas" }, 1959 /* { MNT_ROOTFS, "rootfs" }, */ 1960 /* { MNT_IGNORE, "ignore" }, */ 1961 { 0, NULL} 1962 }; 1963 int bwritten; 1964 int bleft; 1965 int optlen; 1966 int actsize; 1967 1968 *errorp = 0; 1969 bwritten = 0; 1970 bleft = len - 1; /* leave room for trailing \0 */ 1971 1972 /* 1973 * Checks the size of the string. If it contains 1974 * any data, then we will append the new flags to 1975 * it. 1976 */ 1977 actsize = strlen(buf); 1978 if (actsize > 0) 1979 buf += actsize; 1980 1981 /* Default flags if no flags passed */ 1982 if (optp == NULL) 1983 optp = optnames; 1984 1985 if (bleft < 0) { /* degenerate case, 0-length buffer */ 1986 *errorp = EINVAL; 1987 return(0); 1988 } 1989 1990 for (; flags && optp->o_opt; ++optp) { 1991 if ((flags & optp->o_opt) == 0) 1992 continue; 1993 optlen = strlen(optp->o_name); 1994 if (bwritten || actsize > 0) { 1995 if (bleft < 2) { 1996 *errorp = ENOSPC; 1997 break; 1998 } 1999 buf[bwritten++] = ','; 2000 buf[bwritten++] = ' '; 2001 bleft -= 2; 2002 } 2003 if (bleft < optlen) { 2004 *errorp = ENOSPC; 2005 break; 2006 } 2007 bcopy(optp->o_name, buf + bwritten, optlen); 2008 bwritten += optlen; 2009 bleft -= optlen; 2010 flags &= ~optp->o_opt; 2011 } 2012 2013 /* 2014 * Space already reserved for trailing \0 2015 */ 2016 buf[bwritten] = 0; 2017 return (bwritten); 2018 } 2019 2020 /* 2021 * Build hash lists of net addresses and hang them off the mount point. 2022 * Called by ufs_mount() to set up the lists of export addresses. 2023 */ 2024 static int 2025 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 2026 const struct export_args *argp) 2027 { 2028 struct netcred *np; 2029 struct radix_node_head *rnh; 2030 int i; 2031 struct radix_node *rn; 2032 struct sockaddr *saddr, *smask = NULL; 2033 int error; 2034 2035 if (argp->ex_addrlen == 0) { 2036 if (mp->mnt_flag & MNT_DEFEXPORTED) 2037 return (EPERM); 2038 np = &nep->ne_defexported; 2039 np->netc_exflags = argp->ex_flags; 2040 np->netc_anon = argp->ex_anon; 2041 np->netc_anon.cr_ref = 1; 2042 mp->mnt_flag |= MNT_DEFEXPORTED; 2043 return (0); 2044 } 2045 2046 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN) 2047 return (EINVAL); 2048 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN) 2049 return (EINVAL); 2050 2051 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 2052 np = (struct netcred *)kmalloc(i, M_NETCRED, M_WAITOK | M_ZERO); 2053 saddr = (struct sockaddr *) (np + 1); 2054 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen))) 2055 goto out; 2056 if (saddr->sa_len > argp->ex_addrlen) 2057 saddr->sa_len = argp->ex_addrlen; 2058 if (argp->ex_masklen) { 2059 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 2060 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen); 2061 if (error) 2062 goto out; 2063 if (smask->sa_len > argp->ex_masklen) 2064 smask->sa_len = argp->ex_masklen; 2065 } 2066 NE_LOCK(nep); 2067 if (nep->ne_maskhead == NULL) { 2068 if (!rn_inithead((void **)&nep->ne_maskhead, NULL, 0)) { 2069 error = ENOBUFS; 2070 goto out; 2071 } 2072 } 2073 if ((rnh = vfs_create_addrlist_af(saddr->sa_family, nep)) == NULL) { 2074 error = ENOBUFS; 2075 goto out; 2076 } 2077 rn = (*rnh->rnh_addaddr)((char *)saddr, (char *)smask, rnh, 2078 np->netc_rnodes); 2079 NE_UNLOCK(nep); 2080 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ 2081 error = EPERM; 2082 goto out; 2083 } 2084 np->netc_exflags = argp->ex_flags; 2085 np->netc_anon = argp->ex_anon; 2086 np->netc_anon.cr_ref = 1; 2087 return (0); 2088 2089 out: 2090 kfree(np, M_NETCRED); 2091 return (error); 2092 } 2093 2094 /* 2095 * Free netcred structures installed in the netexport 2096 */ 2097 static int 2098 vfs_free_netcred(struct radix_node *rn, void *w) 2099 { 2100 struct radix_node_head *rnh = (struct radix_node_head *)w; 2101 2102 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 2103 kfree(rn, M_NETCRED); 2104 2105 return (0); 2106 } 2107 2108 /* 2109 * callback to free an element of the mask table installed in the 2110 * netexport. These may be created indirectly and are not netcred 2111 * structures. 2112 */ 2113 static int 2114 vfs_free_netcred_mask(struct radix_node *rn, void *w) 2115 { 2116 struct radix_node_head *rnh = (struct radix_node_head *)w; 2117 2118 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 2119 kfree(rn, M_RTABLE); 2120 2121 return (0); 2122 } 2123 2124 static struct radix_node_head * 2125 vfs_create_addrlist_af(int af, struct netexport *nep) 2126 { 2127 struct radix_node_head *rnh = NULL; 2128 #if defined(INET) || defined(INET6) 2129 struct radix_node_head *maskhead = nep->ne_maskhead; 2130 int off; 2131 #endif 2132 2133 NE_ASSERT_LOCKED(nep); 2134 #if defined(INET) || defined(INET6) 2135 KKASSERT(maskhead != NULL); 2136 #endif 2137 switch (af) { 2138 #ifdef INET 2139 case AF_INET: 2140 if ((rnh = nep->ne_inethead) == NULL) { 2141 off = offsetof(struct sockaddr_in, sin_addr) << 3; 2142 if (!rn_inithead((void **)&rnh, maskhead, off)) 2143 return (NULL); 2144 nep->ne_inethead = rnh; 2145 } 2146 break; 2147 #endif 2148 #ifdef INET6 2149 case AF_INET6: 2150 if ((rnh = nep->ne_inet6head) == NULL) { 2151 off = offsetof(struct sockaddr_in6, sin6_addr) << 3; 2152 if (!rn_inithead((void **)&rnh, maskhead, off)) 2153 return (NULL); 2154 nep->ne_inet6head = rnh; 2155 } 2156 break; 2157 #endif 2158 } 2159 return (rnh); 2160 } 2161 2162 /* 2163 * helper function for freeing netcred elements 2164 */ 2165 static void 2166 vfs_free_addrlist_af(struct radix_node_head **prnh) 2167 { 2168 struct radix_node_head *rnh = *prnh; 2169 2170 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh); 2171 kfree(rnh, M_RTABLE); 2172 *prnh = NULL; 2173 } 2174 2175 /* 2176 * helper function for freeing mask elements 2177 */ 2178 static void 2179 vfs_free_addrlist_masks(struct radix_node_head **prnh) 2180 { 2181 struct radix_node_head *rnh = *prnh; 2182 2183 (*rnh->rnh_walktree) (rnh, vfs_free_netcred_mask, rnh); 2184 kfree(rnh, M_RTABLE); 2185 *prnh = NULL; 2186 } 2187 2188 /* 2189 * Free the net address hash lists that are hanging off the mount points. 2190 */ 2191 static void 2192 vfs_free_addrlist(struct netexport *nep) 2193 { 2194 NE_LOCK(nep); 2195 if (nep->ne_inethead != NULL) 2196 vfs_free_addrlist_af(&nep->ne_inethead); 2197 if (nep->ne_inet6head != NULL) 2198 vfs_free_addrlist_af(&nep->ne_inet6head); 2199 if (nep->ne_maskhead) 2200 vfs_free_addrlist_masks(&nep->ne_maskhead); 2201 NE_UNLOCK(nep); 2202 } 2203 2204 int 2205 vfs_export(struct mount *mp, struct netexport *nep, 2206 const struct export_args *argp) 2207 { 2208 int error; 2209 2210 if (argp->ex_flags & MNT_DELEXPORT) { 2211 if (mp->mnt_flag & MNT_EXPUBLIC) { 2212 vfs_setpublicfs(NULL, NULL, NULL); 2213 mp->mnt_flag &= ~MNT_EXPUBLIC; 2214 } 2215 vfs_free_addrlist(nep); 2216 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 2217 } 2218 if (argp->ex_flags & MNT_EXPORTED) { 2219 if (argp->ex_flags & MNT_EXPUBLIC) { 2220 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 2221 return (error); 2222 mp->mnt_flag |= MNT_EXPUBLIC; 2223 } 2224 if ((error = vfs_hang_addrlist(mp, nep, argp))) 2225 return (error); 2226 mp->mnt_flag |= MNT_EXPORTED; 2227 } 2228 return (0); 2229 } 2230 2231 2232 /* 2233 * Set the publicly exported filesystem (WebNFS). Currently, only 2234 * one public filesystem is possible in the spec (RFC 2054 and 2055) 2235 */ 2236 int 2237 vfs_setpublicfs(struct mount *mp, struct netexport *nep, 2238 const struct export_args *argp) 2239 { 2240 int error; 2241 struct vnode *rvp; 2242 char *cp; 2243 2244 /* 2245 * mp == NULL -> invalidate the current info, the FS is 2246 * no longer exported. May be called from either vfs_export 2247 * or unmount, so check if it hasn't already been done. 2248 */ 2249 if (mp == NULL) { 2250 if (nfs_pub.np_valid) { 2251 nfs_pub.np_valid = 0; 2252 if (nfs_pub.np_index != NULL) { 2253 kfree(nfs_pub.np_index, M_TEMP); 2254 nfs_pub.np_index = NULL; 2255 } 2256 } 2257 return (0); 2258 } 2259 2260 /* 2261 * Only one allowed at a time. 2262 */ 2263 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 2264 return (EBUSY); 2265 2266 /* 2267 * Get real filehandle for root of exported FS. 2268 */ 2269 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 2270 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 2271 2272 if ((error = VFS_ROOT(mp, &rvp))) 2273 return (error); 2274 2275 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 2276 return (error); 2277 2278 vput(rvp); 2279 2280 /* 2281 * If an indexfile was specified, pull it in. 2282 */ 2283 if (argp->ex_indexfile != NULL) { 2284 int namelen; 2285 2286 error = vn_get_namelen(rvp, &namelen); 2287 if (error) 2288 return (error); 2289 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK); 2290 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 2291 namelen, NULL); 2292 if (!error) { 2293 /* 2294 * Check for illegal filenames. 2295 */ 2296 for (cp = nfs_pub.np_index; *cp; cp++) { 2297 if (*cp == '/') { 2298 error = EINVAL; 2299 break; 2300 } 2301 } 2302 } 2303 if (error) { 2304 kfree(nfs_pub.np_index, M_TEMP); 2305 return (error); 2306 } 2307 } 2308 2309 nfs_pub.np_mount = mp; 2310 nfs_pub.np_valid = 1; 2311 return (0); 2312 } 2313 2314 struct netcred * 2315 vfs_export_lookup(struct mount *mp, struct netexport *nep, 2316 struct sockaddr *nam) 2317 { 2318 struct netcred *np; 2319 struct radix_node_head *rnh; 2320 struct sockaddr *saddr; 2321 2322 np = NULL; 2323 if (mp->mnt_flag & MNT_EXPORTED) { 2324 /* 2325 * Lookup in the export list first. 2326 */ 2327 NE_LOCK(nep); 2328 if (nam != NULL) { 2329 saddr = nam; 2330 switch (saddr->sa_family) { 2331 #ifdef INET 2332 case AF_INET: 2333 rnh = nep->ne_inethead; 2334 break; 2335 #endif 2336 #ifdef INET6 2337 case AF_INET6: 2338 rnh = nep->ne_inet6head; 2339 break; 2340 #endif 2341 default: 2342 rnh = NULL; 2343 } 2344 if (rnh != NULL) { 2345 np = (struct netcred *) 2346 (*rnh->rnh_matchaddr)((char *)saddr, 2347 rnh); 2348 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 2349 np = NULL; 2350 } 2351 } 2352 NE_UNLOCK(nep); 2353 /* 2354 * If no address match, use the default if it exists. 2355 */ 2356 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 2357 np = &nep->ne_defexported; 2358 } 2359 return (np); 2360 } 2361 2362 /* 2363 * perform msync on all vnodes under a mount point. The mount point must 2364 * be locked. This code is also responsible for lazy-freeing unreferenced 2365 * vnodes whos VM objects no longer contain pages. 2366 * 2367 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state. 2368 * 2369 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked, 2370 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it 2371 * way up in this high level function. 2372 */ 2373 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data); 2374 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data); 2375 2376 void 2377 vfs_msync(struct mount *mp, int flags) 2378 { 2379 int vmsc_flags; 2380 2381 /* 2382 * tmpfs sets this flag to prevent msync(), sync, and the 2383 * filesystem periodic syncer from trying to flush VM pages 2384 * to swap. Only pure memory pressure flushes tmpfs VM pages 2385 * to swap. 2386 */ 2387 if (mp->mnt_kern_flag & MNTK_NOMSYNC) 2388 return; 2389 2390 /* 2391 * Ok, scan the vnodes for work. If the filesystem is using the 2392 * syncer thread feature we can use vsyncscan() instead of 2393 * vmntvnodescan(), which is much faster. 2394 */ 2395 vmsc_flags = VMSC_GETVP; 2396 if (flags != MNT_WAIT) 2397 vmsc_flags |= VMSC_NOWAIT; 2398 2399 if (mp->mnt_kern_flag & MNTK_THR_SYNC) { 2400 vsyncscan(mp, vmsc_flags, vfs_msync_scan2, 2401 (void *)(intptr_t)flags); 2402 } else { 2403 vmntvnodescan(mp, vmsc_flags, 2404 vfs_msync_scan1, vfs_msync_scan2, 2405 (void *)(intptr_t)flags); 2406 } 2407 } 2408 2409 /* 2410 * scan1 is a fast pre-check. There could be hundreds of thousands of 2411 * vnodes, we cannot afford to do anything heavy weight until we have a 2412 * fairly good indication that there is work to do. 2413 * 2414 * The new namecache holds the vnode for each v_namecache association 2415 * so allow these refs. 2416 */ 2417 static 2418 int 2419 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data) 2420 { 2421 int flags = (int)(intptr_t)data; 2422 2423 if ((vp->v_flag & VRECLAIMED) == 0) { 2424 if (vp->v_auxrefs == vp->v_namecache_count && 2425 VREFCNT(vp) <= 0 && vp->v_object) { 2426 return(0); /* call scan2 */ 2427 } 2428 if ((mp->mnt_flag & MNT_RDONLY) == 0 && 2429 (vp->v_flag & VOBJDIRTY) && 2430 (flags == MNT_WAIT || vn_islocked(vp) == 0)) { 2431 return(0); /* call scan2 */ 2432 } 2433 } 2434 2435 /* 2436 * do not call scan2, continue the loop 2437 */ 2438 return(-1); 2439 } 2440 2441 /* 2442 * This callback is handed a locked vnode. 2443 */ 2444 static 2445 int 2446 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data) 2447 { 2448 vm_object_t obj; 2449 int flags = (int)(intptr_t)data; 2450 int opcflags; 2451 2452 if (vp->v_flag & VRECLAIMED) 2453 return(0); 2454 2455 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) { 2456 if ((obj = vp->v_object) != NULL) { 2457 if (flags == MNT_WAIT) { 2458 /* 2459 * VFS_MSYNC is called with MNT_WAIT when 2460 * unmounting. 2461 */ 2462 opcflags = OBJPC_SYNC; 2463 } else if (vp->v_writecount || obj->ref_count) { 2464 /* 2465 * VFS_MSYNC is otherwise called via the 2466 * periodic filesystem sync or the 'sync' 2467 * command. Honor MADV_NOSYNC / MAP_NOSYNC 2468 * if the file is open for writing or memory 2469 * mapped. Pages flagged PG_NOSYNC will not 2470 * be automatically flushed at this time. 2471 * 2472 * The obj->ref_count test is not perfect 2473 * since temporary refs may be present, but 2474 * the periodic filesystem sync will ultimately 2475 * catch it if the file is not open and not 2476 * mapped. 2477 */ 2478 opcflags = OBJPC_NOSYNC; 2479 } else { 2480 /* 2481 * If the file is no longer open for writing 2482 * and also no longer mapped, do not honor 2483 * MAP_NOSYNC. That is, fully synchronize 2484 * the file. 2485 * 2486 * This still occurs on the periodic fs sync, 2487 * so frontend programs which turn the file 2488 * over quickly enough can still avoid the 2489 * sync, but ultimately we do want to flush 2490 * even MADV_NOSYNC pages once it is no longer 2491 * mapped or open for writing. 2492 */ 2493 opcflags = 0; 2494 } 2495 vm_object_page_clean(obj, 0, 0, opcflags); 2496 } 2497 } 2498 return(0); 2499 } 2500 2501 /* 2502 * Wake up anyone interested in vp because it is being revoked. 2503 */ 2504 void 2505 vn_gone(struct vnode *vp) 2506 { 2507 lwkt_gettoken(&vp->v_token); 2508 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE); 2509 lwkt_reltoken(&vp->v_token); 2510 } 2511 2512 /* 2513 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened 2514 * (or v_rdev might be NULL). 2515 */ 2516 cdev_t 2517 vn_todev(struct vnode *vp) 2518 { 2519 if (vp->v_type != VBLK && vp->v_type != VCHR) 2520 return (NULL); 2521 KKASSERT(vp->v_rdev != NULL); 2522 return (vp->v_rdev); 2523 } 2524 2525 /* 2526 * Check if vnode represents a disk device. The vnode does not need to be 2527 * opened. 2528 * 2529 * MPALMOSTSAFE 2530 */ 2531 int 2532 vn_isdisk(struct vnode *vp, int *errp) 2533 { 2534 cdev_t dev; 2535 2536 if (vp->v_type != VCHR) { 2537 if (errp != NULL) 2538 *errp = ENOTBLK; 2539 return (0); 2540 } 2541 2542 dev = vp->v_rdev; 2543 2544 if (dev == NULL) { 2545 if (errp != NULL) 2546 *errp = ENXIO; 2547 return (0); 2548 } 2549 if (dev_is_good(dev) == 0) { 2550 if (errp != NULL) 2551 *errp = ENXIO; 2552 return (0); 2553 } 2554 if ((dev_dflags(dev) & D_DISK) == 0) { 2555 if (errp != NULL) 2556 *errp = ENOTBLK; 2557 return (0); 2558 } 2559 if (errp != NULL) 2560 *errp = 0; 2561 return (1); 2562 } 2563 2564 int 2565 vn_get_namelen(struct vnode *vp, int *namelen) 2566 { 2567 int error; 2568 register_t retval[2]; 2569 2570 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval); 2571 if (error) 2572 return (error); 2573 *namelen = (int)retval[0]; 2574 return (0); 2575 } 2576 2577 int 2578 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type, 2579 uint16_t d_namlen, const char *d_name) 2580 { 2581 struct dirent *dp; 2582 size_t len; 2583 2584 len = _DIRENT_RECLEN(d_namlen); 2585 if (len > uio->uio_resid) 2586 return(1); 2587 2588 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO); 2589 2590 dp->d_ino = d_ino; 2591 dp->d_namlen = d_namlen; 2592 dp->d_type = d_type; 2593 bcopy(d_name, dp->d_name, d_namlen); 2594 2595 *error = uiomove((caddr_t)dp, len, uio); 2596 2597 kfree(dp, M_TEMP); 2598 2599 return(0); 2600 } 2601 2602 void 2603 vn_mark_atime(struct vnode *vp, struct thread *td) 2604 { 2605 struct proc *p = td->td_proc; 2606 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred; 2607 2608 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) { 2609 VOP_MARKATIME(vp, cred); 2610 } 2611 } 2612 2613 /* 2614 * Calculate the number of entries in an inode-related chained hash table. 2615 * With today's memory sizes, maxvnodes can wind up being a very large 2616 * number. There is no reason to waste memory, so tolerate some stacking. 2617 */ 2618 int 2619 vfs_inodehashsize(void) 2620 { 2621 int hsize; 2622 2623 hsize = 32; 2624 while (hsize < maxvnodes) 2625 hsize <<= 1; 2626 while (hsize > maxvnodes * 2) 2627 hsize >>= 1; /* nominal 2x stacking */ 2628 2629 if (maxvnodes > 1024 * 1024) 2630 hsize >>= 1; /* nominal 8x stacking */ 2631 2632 if (maxvnodes > 128 * 1024) 2633 hsize >>= 1; /* nominal 4x stacking */ 2634 2635 if (hsize < 16) 2636 hsize = 16; 2637 2638 return hsize; 2639 } 2640 2641 union _qcvt { 2642 quad_t qcvt; 2643 int32_t val[2]; 2644 }; 2645 2646 #define SETHIGH(q, h) { \ 2647 union _qcvt tmp; \ 2648 tmp.qcvt = (q); \ 2649 tmp.val[_QUAD_HIGHWORD] = (h); \ 2650 (q) = tmp.qcvt; \ 2651 } 2652 #define SETLOW(q, l) { \ 2653 union _qcvt tmp; \ 2654 tmp.qcvt = (q); \ 2655 tmp.val[_QUAD_LOWWORD] = (l); \ 2656 (q) = tmp.qcvt; \ 2657 } 2658 2659 u_quad_t 2660 init_va_filerev(void) 2661 { 2662 struct timeval tv; 2663 u_quad_t ret = 0; 2664 2665 getmicrouptime(&tv); 2666 SETHIGH(ret, tv.tv_sec); 2667 SETLOW(ret, tv.tv_usec * 4294); 2668 2669 return ret; 2670 } 2671