1 /*- 2 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 7 * 2005 program. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ 31 */ 32 33 /* 34 * tmpfs vnode interface. 35 */ 36 37 #include <sys/kernel.h> 38 #include <sys/kern_syscall.h> 39 #include <sys/param.h> 40 #include <sys/fcntl.h> 41 #include <sys/lockf.h> 42 #include <sys/priv.h> 43 #include <sys/proc.h> 44 #include <sys/resourcevar.h> 45 #include <sys/sched.h> 46 #include <sys/stat.h> 47 #include <sys/systm.h> 48 #include <sys/unistd.h> 49 #include <sys/vfsops.h> 50 #include <sys/vnode.h> 51 #include <sys/mountctl.h> 52 53 #include <vm/vm.h> 54 #include <vm/vm_extern.h> 55 #include <vm/vm_object.h> 56 #include <vm/vm_page.h> 57 #include <vm/vm_pageout.h> 58 #include <vm/vm_pager.h> 59 #include <vm/swap_pager.h> 60 61 #include <sys/buf2.h> 62 #include <vm/vm_page2.h> 63 64 #include <vfs/fifofs/fifo.h> 65 #include <vfs/tmpfs/tmpfs_vnops.h> 66 #include "tmpfs.h" 67 68 static void tmpfs_strategy_done(struct bio *bio); 69 70 static __inline 71 void 72 tmpfs_knote(struct vnode *vp, int flags) 73 { 74 if (flags) 75 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags); 76 } 77 78 79 /* --------------------------------------------------------------------- */ 80 81 static int 82 tmpfs_nresolve(struct vop_nresolve_args *ap) 83 { 84 struct vnode *dvp = ap->a_dvp; 85 struct vnode *vp = NULL; 86 struct namecache *ncp = ap->a_nch->ncp; 87 struct tmpfs_node *tnode; 88 struct tmpfs_dirent *de; 89 struct tmpfs_node *dnode; 90 int error; 91 92 dnode = VP_TO_TMPFS_DIR(dvp); 93 94 TMPFS_NODE_LOCK_SH(dnode); 95 de = tmpfs_dir_lookup(dnode, NULL, ncp); 96 if (de == NULL) { 97 error = ENOENT; 98 } else { 99 /* 100 * Allocate a vnode for the node we found. 101 */ 102 tnode = de->td_node; 103 error = tmpfs_alloc_vp(dvp->v_mount, tnode, 104 LK_EXCLUSIVE | LK_RETRY, &vp); 105 if (error) 106 goto out; 107 KKASSERT(vp); 108 } 109 110 out: 111 TMPFS_NODE_UNLOCK(dnode); 112 113 if ((dnode->tn_status & TMPFS_NODE_ACCESSED) == 0) { 114 TMPFS_NODE_LOCK(dnode); 115 dnode->tn_status |= TMPFS_NODE_ACCESSED; 116 TMPFS_NODE_UNLOCK(dnode); 117 } 118 119 /* 120 * Store the result of this lookup in the cache. Avoid this if the 121 * request was for creation, as it does not improve timings on 122 * emprical tests. 123 */ 124 if (vp) { 125 vn_unlock(vp); 126 cache_setvp(ap->a_nch, vp); 127 vrele(vp); 128 } else if (error == ENOENT) { 129 cache_setvp(ap->a_nch, NULL); 130 } 131 return (error); 132 } 133 134 static int 135 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *ap) 136 { 137 struct vnode *dvp = ap->a_dvp; 138 struct vnode **vpp = ap->a_vpp; 139 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp); 140 struct ucred *cred = ap->a_cred; 141 int error; 142 143 *vpp = NULL; 144 145 /* Check accessibility of requested node as a first step. */ 146 error = VOP_ACCESS(dvp, VEXEC, cred); 147 if (error != 0) 148 return error; 149 150 if (dnode->tn_dir.tn_parent != NULL) { 151 /* Allocate a new vnode on the matching entry. */ 152 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent, 153 LK_EXCLUSIVE | LK_RETRY, vpp); 154 155 if (*vpp) 156 vn_unlock(*vpp); 157 } 158 return (*vpp == NULL) ? ENOENT : 0; 159 } 160 161 /* --------------------------------------------------------------------- */ 162 163 static int 164 tmpfs_ncreate(struct vop_ncreate_args *ap) 165 { 166 struct vnode *dvp = ap->a_dvp; 167 struct vnode **vpp = ap->a_vpp; 168 struct namecache *ncp = ap->a_nch->ncp; 169 struct vattr *vap = ap->a_vap; 170 struct ucred *cred = ap->a_cred; 171 int error; 172 173 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK); 174 175 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL); 176 if (error == 0) { 177 cache_setunresolved(ap->a_nch); 178 cache_setvp(ap->a_nch, *vpp); 179 tmpfs_knote(dvp, NOTE_WRITE); 180 } 181 return (error); 182 } 183 /* --------------------------------------------------------------------- */ 184 185 static int 186 tmpfs_nmknod(struct vop_nmknod_args *ap) 187 { 188 struct vnode *dvp = ap->a_dvp; 189 struct vnode **vpp = ap->a_vpp; 190 struct namecache *ncp = ap->a_nch->ncp; 191 struct vattr *vap = ap->a_vap; 192 struct ucred *cred = ap->a_cred; 193 int error; 194 195 if (vap->va_type != VBLK && vap->va_type != VCHR && 196 vap->va_type != VFIFO) { 197 return (EINVAL); 198 } 199 200 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL); 201 if (error == 0) { 202 cache_setunresolved(ap->a_nch); 203 cache_setvp(ap->a_nch, *vpp); 204 tmpfs_knote(dvp, NOTE_WRITE); 205 } 206 return error; 207 } 208 209 /* --------------------------------------------------------------------- */ 210 211 static int 212 tmpfs_open(struct vop_open_args *ap) 213 { 214 struct vnode *vp = ap->a_vp; 215 int mode = ap->a_mode; 216 struct tmpfs_node *node; 217 int error; 218 219 node = VP_TO_TMPFS_NODE(vp); 220 221 #if 0 222 /* The file is still active but all its names have been removed 223 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as 224 * it is about to die. */ 225 if (node->tn_links < 1) 226 return (ENOENT); 227 #endif 228 229 /* If the file is marked append-only, deny write requests. */ 230 if ((node->tn_flags & APPEND) && 231 (mode & (FWRITE | O_APPEND)) == FWRITE) { 232 error = EPERM; 233 } else { 234 error = (vop_stdopen(ap)); 235 } 236 237 return (error); 238 } 239 240 /* --------------------------------------------------------------------- */ 241 242 static int 243 tmpfs_close(struct vop_close_args *ap) 244 { 245 struct vnode *vp = ap->a_vp; 246 struct tmpfs_node *node; 247 int error; 248 249 node = VP_TO_TMPFS_NODE(vp); 250 251 if (node->tn_links > 0) { 252 /* 253 * Update node times. No need to do it if the node has 254 * been deleted, because it will vanish after we return. 255 */ 256 tmpfs_update(vp); 257 } 258 259 error = vop_stdclose(ap); 260 261 return (error); 262 } 263 264 /* --------------------------------------------------------------------- */ 265 266 int 267 tmpfs_access(struct vop_access_args *ap) 268 { 269 struct vnode *vp = ap->a_vp; 270 int error; 271 struct tmpfs_node *node; 272 273 node = VP_TO_TMPFS_NODE(vp); 274 275 switch (vp->v_type) { 276 case VDIR: 277 /* FALLTHROUGH */ 278 case VLNK: 279 /* FALLTHROUGH */ 280 case VREG: 281 if ((ap->a_mode & VWRITE) && 282 (vp->v_mount->mnt_flag & MNT_RDONLY)) { 283 error = EROFS; 284 goto out; 285 } 286 break; 287 288 case VBLK: 289 /* FALLTHROUGH */ 290 case VCHR: 291 /* FALLTHROUGH */ 292 case VSOCK: 293 /* FALLTHROUGH */ 294 case VFIFO: 295 break; 296 297 default: 298 error = EINVAL; 299 goto out; 300 } 301 302 if ((ap->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) { 303 error = EPERM; 304 goto out; 305 } 306 307 error = vop_helper_access(ap, node->tn_uid, node->tn_gid, 308 node->tn_mode, 0); 309 out: 310 return error; 311 } 312 313 /* --------------------------------------------------------------------- */ 314 315 int 316 tmpfs_getattr(struct vop_getattr_args *ap) 317 { 318 struct vnode *vp = ap->a_vp; 319 struct vattr *vap = ap->a_vap; 320 struct tmpfs_node *node; 321 322 node = VP_TO_TMPFS_NODE(vp); 323 324 tmpfs_update(vp); 325 326 TMPFS_NODE_LOCK_SH(node); 327 vap->va_type = vp->v_type; 328 vap->va_mode = node->tn_mode; 329 vap->va_nlink = node->tn_links; 330 vap->va_uid = node->tn_uid; 331 vap->va_gid = node->tn_gid; 332 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 333 vap->va_fileid = node->tn_id; 334 vap->va_size = node->tn_size; 335 vap->va_blocksize = PAGE_SIZE; 336 vap->va_atime.tv_sec = node->tn_atime; 337 vap->va_atime.tv_nsec = node->tn_atimensec; 338 vap->va_mtime.tv_sec = node->tn_mtime; 339 vap->va_mtime.tv_nsec = node->tn_mtimensec; 340 vap->va_ctime.tv_sec = node->tn_ctime; 341 vap->va_ctime.tv_nsec = node->tn_ctimensec; 342 vap->va_gen = node->tn_gen; 343 vap->va_flags = node->tn_flags; 344 if (vp->v_type == VBLK || vp->v_type == VCHR) { 345 vap->va_rmajor = umajor(node->tn_rdev); 346 vap->va_rminor = uminor(node->tn_rdev); 347 } 348 vap->va_bytes = round_page(node->tn_size); 349 vap->va_filerev = 0; 350 TMPFS_NODE_UNLOCK(node); 351 352 return 0; 353 } 354 355 /* --------------------------------------------------------------------- */ 356 357 int 358 tmpfs_setattr(struct vop_setattr_args *ap) 359 { 360 struct vnode *vp = ap->a_vp; 361 struct vattr *vap = ap->a_vap; 362 struct ucred *cred = ap->a_cred; 363 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp); 364 int error = 0; 365 int kflags = 0; 366 367 TMPFS_NODE_LOCK(node); 368 if (error == 0 && (vap->va_flags != VNOVAL)) { 369 error = tmpfs_chflags(vp, vap->va_flags, cred); 370 kflags |= NOTE_ATTRIB; 371 } 372 373 if (error == 0 && (vap->va_size != VNOVAL)) { 374 if (vap->va_size > node->tn_size) 375 kflags |= NOTE_WRITE | NOTE_EXTEND; 376 else 377 kflags |= NOTE_WRITE; 378 error = tmpfs_chsize(vp, vap->va_size, cred); 379 } 380 381 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL || 382 vap->va_gid != (gid_t)VNOVAL)) { 383 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred); 384 kflags |= NOTE_ATTRIB; 385 } 386 387 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) { 388 error = tmpfs_chmod(vp, vap->va_mode, cred); 389 kflags |= NOTE_ATTRIB; 390 } 391 392 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL && 393 vap->va_atime.tv_nsec != VNOVAL) || 394 (vap->va_mtime.tv_sec != VNOVAL && 395 vap->va_mtime.tv_nsec != VNOVAL) )) { 396 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 397 vap->va_vaflags, cred); 398 kflags |= NOTE_ATTRIB; 399 } 400 401 /* 402 * Update the node times. We give preference to the error codes 403 * generated by this function rather than the ones that may arise 404 * from tmpfs_update. 405 */ 406 tmpfs_update(vp); 407 TMPFS_NODE_UNLOCK(node); 408 tmpfs_knote(vp, kflags); 409 410 return (error); 411 } 412 413 /* --------------------------------------------------------------------- */ 414 415 /* 416 * fsync is usually a NOP, but we must take action when unmounting or 417 * when recycling. 418 */ 419 static int 420 tmpfs_fsync(struct vop_fsync_args *ap) 421 { 422 struct tmpfs_node *node; 423 struct vnode *vp = ap->a_vp; 424 425 node = VP_TO_TMPFS_NODE(vp); 426 427 tmpfs_update(vp); 428 if (vp->v_type == VREG) { 429 if (vp->v_flag & VRECLAIMED) { 430 if (node->tn_links == 0) 431 tmpfs_truncate(vp, 0); 432 else 433 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL); 434 } 435 } 436 return 0; 437 } 438 439 /* --------------------------------------------------------------------- */ 440 441 static int 442 tmpfs_read(struct vop_read_args *ap) 443 { 444 struct buf *bp; 445 struct vnode *vp = ap->a_vp; 446 struct uio *uio = ap->a_uio; 447 struct tmpfs_node *node; 448 off_t base_offset; 449 size_t offset; 450 size_t len; 451 size_t resid; 452 int error; 453 454 /* 455 * Check the basics 456 */ 457 if (uio->uio_offset < 0) 458 return (EINVAL); 459 if (vp->v_type != VREG) 460 return (EINVAL); 461 462 /* 463 * Extract node, try to shortcut the operation through 464 * the VM page cache, allowing us to avoid buffer cache 465 * overheads. 466 */ 467 node = VP_TO_TMPFS_NODE(vp); 468 resid = uio->uio_resid; 469 error = vop_helper_read_shortcut(ap); 470 if (error) 471 return error; 472 if (uio->uio_resid == 0) { 473 if (resid) 474 goto finished; 475 return error; 476 } 477 478 /* 479 * Fall-through to our normal read code. 480 */ 481 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) { 482 /* 483 * Use buffer cache I/O (via tmpfs_strategy) 484 */ 485 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64; 486 base_offset = (off_t)uio->uio_offset - offset; 487 bp = getcacheblk(vp, base_offset, TMPFS_BLKSIZE, GETBLK_KVABIO); 488 if (bp == NULL) { 489 error = bread_kvabio(vp, base_offset, 490 TMPFS_BLKSIZE, &bp); 491 if (error) { 492 brelse(bp); 493 kprintf("tmpfs_read bread error %d\n", error); 494 break; 495 } 496 497 /* 498 * tmpfs pretty much fiddles directly with the VM 499 * system, don't let it exhaust it or we won't play 500 * nice with other processes. 501 * 502 * Only do this if the VOP is coming from a normal 503 * read/write. The VM system handles the case for 504 * UIO_NOCOPY. 505 */ 506 if (uio->uio_segflg != UIO_NOCOPY) 507 vm_wait_nominal(); 508 } 509 bp->b_flags |= B_CLUSTEROK; 510 bkvasync(bp); 511 512 /* 513 * Figure out how many bytes we can actually copy this loop. 514 */ 515 len = TMPFS_BLKSIZE - offset; 516 if (len > uio->uio_resid) 517 len = uio->uio_resid; 518 if (len > node->tn_size - uio->uio_offset) 519 len = (size_t)(node->tn_size - uio->uio_offset); 520 521 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio); 522 bqrelse(bp); 523 if (error) { 524 kprintf("tmpfs_read uiomove error %d\n", error); 525 break; 526 } 527 } 528 529 finished: 530 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) { 531 TMPFS_NODE_LOCK(node); 532 node->tn_status |= TMPFS_NODE_ACCESSED; 533 TMPFS_NODE_UNLOCK(node); 534 } 535 return (error); 536 } 537 538 static int 539 tmpfs_write(struct vop_write_args *ap) 540 { 541 struct buf *bp; 542 struct vnode *vp = ap->a_vp; 543 struct uio *uio = ap->a_uio; 544 struct thread *td = uio->uio_td; 545 struct tmpfs_node *node; 546 boolean_t extended; 547 off_t oldsize; 548 int error; 549 off_t base_offset; 550 size_t offset; 551 size_t len; 552 struct rlimit limit; 553 int trivial = 0; 554 int kflags = 0; 555 int seqcount; 556 557 error = 0; 558 if (uio->uio_resid == 0) { 559 return error; 560 } 561 562 node = VP_TO_TMPFS_NODE(vp); 563 564 if (vp->v_type != VREG) 565 return (EINVAL); 566 seqcount = ap->a_ioflag >> 16; 567 568 TMPFS_NODE_LOCK(node); 569 570 oldsize = node->tn_size; 571 if (ap->a_ioflag & IO_APPEND) 572 uio->uio_offset = node->tn_size; 573 574 /* 575 * Check for illegal write offsets. 576 */ 577 if (uio->uio_offset + uio->uio_resid > 578 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) { 579 error = EFBIG; 580 goto done; 581 } 582 583 /* 584 * NOTE: Ignore if UIO does not come from a user thread (e.g. VN). 585 */ 586 if (vp->v_type == VREG && td != NULL && td->td_lwp != NULL) { 587 error = kern_getrlimit(RLIMIT_FSIZE, &limit); 588 if (error) 589 goto done; 590 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) { 591 ksignal(td->td_proc, SIGXFSZ); 592 error = EFBIG; 593 goto done; 594 } 595 } 596 597 /* 598 * Extend the file's size if necessary 599 */ 600 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size); 601 602 while (uio->uio_resid > 0) { 603 /* 604 * Don't completely blow out running buffer I/O 605 * when being hit from the pageout daemon. 606 */ 607 if (uio->uio_segflg == UIO_NOCOPY && 608 (ap->a_ioflag & IO_RECURSE) == 0) { 609 bwillwrite(TMPFS_BLKSIZE); 610 } 611 612 /* 613 * Use buffer cache I/O (via tmpfs_strategy) 614 */ 615 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64; 616 base_offset = (off_t)uio->uio_offset - offset; 617 len = TMPFS_BLKSIZE - offset; 618 if (len > uio->uio_resid) 619 len = uio->uio_resid; 620 621 if ((uio->uio_offset + len) > node->tn_size) { 622 trivial = (uio->uio_offset <= node->tn_size); 623 error = tmpfs_reg_resize(vp, uio->uio_offset + len, 624 trivial); 625 if (error) 626 break; 627 } 628 629 /* 630 * Read to fill in any gaps. Theoretically we could 631 * optimize this if the write covers the entire buffer 632 * and is not a UIO_NOCOPY write, however this can lead 633 * to a security violation exposing random kernel memory 634 * (whatever junk was in the backing VM pages before). 635 * 636 * So just use bread() to do the right thing. 637 */ 638 error = bread_kvabio(vp, base_offset, TMPFS_BLKSIZE, &bp); 639 bkvasync(bp); 640 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio); 641 if (error) { 642 kprintf("tmpfs_write uiomove error %d\n", error); 643 brelse(bp); 644 break; 645 } 646 647 if (uio->uio_offset > node->tn_size) { 648 node->tn_size = uio->uio_offset; 649 kflags |= NOTE_EXTEND; 650 } 651 kflags |= NOTE_WRITE; 652 653 /* 654 * Always try to flush the page in the UIO_NOCOPY case. This 655 * can come from the pageout daemon or during vnode eviction. 656 * It is not necessarily going to be marked IO_ASYNC/IO_SYNC. 657 * 658 * For the normal case we buwrite(), dirtying the underlying 659 * VM pages instead of dirtying the buffer and releasing the 660 * buffer as a clean buffer. This allows tmpfs to use 661 * essentially all available memory to cache file data. 662 * If we used bdwrite() the buffer cache would wind up 663 * flushing the data to swap too quickly. 664 * 665 * But because tmpfs can seriously load the VM system we 666 * fall-back to using bdwrite() when free memory starts 667 * to get low. This shifts the load away from the VM system 668 * and makes tmpfs act more like a normal filesystem with 669 * regards to disk activity. 670 * 671 * tmpfs pretty much fiddles directly with the VM 672 * system, don't let it exhaust it or we won't play 673 * nice with other processes. Only do this if the 674 * VOP is coming from a normal read/write. The VM system 675 * handles the case for UIO_NOCOPY. 676 */ 677 bp->b_flags |= B_CLUSTEROK; 678 if (uio->uio_segflg == UIO_NOCOPY) { 679 /* 680 * Flush from the pageout daemon, deal with 681 * potentially very heavy tmpfs write activity 682 * causing long stalls in the pageout daemon 683 * before pages get to free/cache. 684 * 685 * (a) Under severe pressure setting B_DIRECT will 686 * cause a buffer release to try to free the 687 * underlying pages. 688 * 689 * (b) Under modest memory pressure the B_RELBUF 690 * alone is sufficient to get the pages moved 691 * to the cache. We could also force this by 692 * setting B_NOTMETA but that might have other 693 * unintended side-effects (e.g. setting 694 * PG_NOTMETA on the VM page). 695 * 696 * Hopefully this will unblock the VM system more 697 * quickly under extreme tmpfs write load. 698 */ 699 if (vm_page_count_min(vm_page_free_hysteresis)) 700 bp->b_flags |= B_DIRECT; 701 bp->b_flags |= B_AGE | B_RELBUF; 702 bp->b_act_count = 0; /* buffer->deactivate pgs */ 703 cluster_awrite(bp); 704 } else if (vm_page_count_target()) { 705 /* 706 * Normal (userland) write but we are low on memory, 707 * run the buffer the buffer cache. 708 */ 709 bp->b_act_count = 0; /* buffer->deactivate pgs */ 710 bdwrite(bp); 711 } else { 712 /* 713 * Otherwise run the buffer directly through to the 714 * backing VM store. 715 */ 716 buwrite(bp); 717 /*vm_wait_nominal();*/ 718 } 719 720 if (bp->b_error) { 721 kprintf("tmpfs_write bwrite error %d\n", bp->b_error); 722 break; 723 } 724 } 725 726 if (error) { 727 if (extended) { 728 (void)tmpfs_reg_resize(vp, oldsize, trivial); 729 kflags &= ~NOTE_EXTEND; 730 } 731 goto done; 732 } 733 734 /* 735 * Currently we don't set the mtime on files modified via mmap() 736 * because we can't tell the difference between those modifications 737 * and an attempt by the pageout daemon to flush tmpfs pages to 738 * swap. 739 * 740 * This is because in order to defer flushes as long as possible 741 * buwrite() works by marking the underlying VM pages dirty in 742 * order to be able to dispose of the buffer cache buffer without 743 * flushing it. 744 */ 745 if (uio->uio_segflg != UIO_NOCOPY) 746 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED; 747 if (extended) 748 node->tn_status |= TMPFS_NODE_CHANGED; 749 750 if (node->tn_mode & (S_ISUID | S_ISGID)) { 751 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0)) 752 node->tn_mode &= ~(S_ISUID | S_ISGID); 753 } 754 done: 755 TMPFS_NODE_UNLOCK(node); 756 if (kflags) 757 tmpfs_knote(vp, kflags); 758 759 return(error); 760 } 761 762 static int 763 tmpfs_advlock(struct vop_advlock_args *ap) 764 { 765 struct tmpfs_node *node; 766 struct vnode *vp = ap->a_vp; 767 int error; 768 769 node = VP_TO_TMPFS_NODE(vp); 770 error = (lf_advlock(ap, &node->tn_advlock, node->tn_size)); 771 772 return (error); 773 } 774 775 /* 776 * The strategy function is typically only called when memory pressure 777 * forces the system to attempt to pageout pages. It can also be called 778 * by [n]vtruncbuf() when a truncation cuts a page in half. Normal write 779 * operations 780 * 781 * We set VKVABIO for VREG files so bp->b_data may not be synchronized to 782 * our cpu. swap_pager_strategy() is all we really use, and it directly 783 * supports this. 784 */ 785 static int 786 tmpfs_strategy(struct vop_strategy_args *ap) 787 { 788 struct bio *bio = ap->a_bio; 789 struct bio *nbio; 790 struct buf *bp = bio->bio_buf; 791 struct vnode *vp = ap->a_vp; 792 struct tmpfs_node *node; 793 vm_object_t uobj; 794 vm_page_t m; 795 int i; 796 797 if (vp->v_type != VREG) { 798 bp->b_resid = bp->b_bcount; 799 bp->b_flags |= B_ERROR | B_INVAL; 800 bp->b_error = EINVAL; 801 biodone(bio); 802 return(0); 803 } 804 805 node = VP_TO_TMPFS_NODE(vp); 806 807 uobj = node->tn_reg.tn_aobj; 808 809 /* 810 * Don't bother flushing to swap if there is no swap, just 811 * ensure that the pages are marked as needing a commit (still). 812 */ 813 if (bp->b_cmd == BUF_CMD_WRITE && vm_swap_size == 0) { 814 for (i = 0; i < bp->b_xio.xio_npages; ++i) { 815 m = bp->b_xio.xio_pages[i]; 816 vm_page_need_commit(m); 817 } 818 bp->b_resid = 0; 819 bp->b_error = 0; 820 biodone(bio); 821 } else { 822 nbio = push_bio(bio); 823 nbio->bio_done = tmpfs_strategy_done; 824 nbio->bio_offset = bio->bio_offset; 825 swap_pager_strategy(uobj, nbio); 826 } 827 return 0; 828 } 829 830 /* 831 * If we were unable to commit the pages to swap make sure they are marked 832 * as needing a commit (again). If we were, clear the flag to allow the 833 * pages to be freed. 834 */ 835 static void 836 tmpfs_strategy_done(struct bio *bio) 837 { 838 struct buf *bp; 839 vm_page_t m; 840 int i; 841 842 bp = bio->bio_buf; 843 844 if (bp->b_flags & B_ERROR) { 845 bp->b_flags &= ~B_ERROR; 846 bp->b_error = 0; 847 bp->b_resid = 0; 848 for (i = 0; i < bp->b_xio.xio_npages; ++i) { 849 m = bp->b_xio.xio_pages[i]; 850 vm_page_need_commit(m); 851 } 852 } else { 853 for (i = 0; i < bp->b_xio.xio_npages; ++i) { 854 m = bp->b_xio.xio_pages[i]; 855 vm_page_clear_commit(m); 856 } 857 } 858 bio = pop_bio(bio); 859 biodone(bio); 860 } 861 862 static int 863 tmpfs_bmap(struct vop_bmap_args *ap) 864 { 865 if (ap->a_doffsetp != NULL) 866 *ap->a_doffsetp = ap->a_loffset; 867 if (ap->a_runp != NULL) 868 *ap->a_runp = 0; 869 if (ap->a_runb != NULL) 870 *ap->a_runb = 0; 871 872 return 0; 873 } 874 875 /* --------------------------------------------------------------------- */ 876 877 static int 878 tmpfs_nremove(struct vop_nremove_args *ap) 879 { 880 struct vnode *dvp = ap->a_dvp; 881 struct namecache *ncp = ap->a_nch->ncp; 882 struct vnode *vp; 883 int error; 884 struct tmpfs_dirent *de; 885 struct tmpfs_mount *tmp; 886 struct tmpfs_node *dnode; 887 struct tmpfs_node *node; 888 889 /* 890 * We have to acquire the vp from ap->a_nch because we will likely 891 * unresolve the namecache entry, and a vrele/vput is needed to 892 * trigger the tmpfs_inactive/tmpfs_reclaim sequence. 893 * 894 * We have to use vget to clear any inactive state on the vnode, 895 * otherwise the vnode may remain inactive and thus tmpfs_inactive 896 * will not get called when we release it. 897 */ 898 error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp); 899 KKASSERT(vp->v_mount == dvp->v_mount); 900 KKASSERT(error == 0); 901 vn_unlock(vp); 902 903 if (vp->v_type == VDIR) { 904 error = EISDIR; 905 goto out2; 906 } 907 908 dnode = VP_TO_TMPFS_DIR(dvp); 909 node = VP_TO_TMPFS_NODE(vp); 910 tmp = VFS_TO_TMPFS(vp->v_mount); 911 912 TMPFS_NODE_LOCK(dnode); 913 de = tmpfs_dir_lookup(dnode, node, ncp); 914 if (de == NULL) { 915 error = ENOENT; 916 goto out; 917 } 918 919 /* Files marked as immutable or append-only cannot be deleted. */ 920 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 921 (dnode->tn_flags & APPEND)) { 922 error = EPERM; 923 goto out; 924 } 925 926 /* Remove the entry from the directory; as it is a file, we do not 927 * have to change the number of hard links of the directory. */ 928 tmpfs_dir_detach(dnode, de); 929 930 /* Free the directory entry we just deleted. Note that the node 931 * referred by it will not be removed until the vnode is really 932 * reclaimed. */ 933 tmpfs_free_dirent(tmp, de); 934 935 if (node->tn_links > 0) { 936 TMPFS_NODE_LOCK(node); 937 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 938 TMPFS_NODE_MODIFIED; 939 TMPFS_NODE_UNLOCK(node); 940 } 941 942 cache_unlink(ap->a_nch); 943 tmpfs_knote(vp, NOTE_DELETE); 944 error = 0; 945 946 out: 947 TMPFS_NODE_UNLOCK(dnode); 948 if (error == 0) 949 tmpfs_knote(dvp, NOTE_WRITE); 950 out2: 951 vrele(vp); 952 953 return error; 954 } 955 956 /* --------------------------------------------------------------------- */ 957 958 static int 959 tmpfs_nlink(struct vop_nlink_args *ap) 960 { 961 struct vnode *dvp = ap->a_dvp; 962 struct vnode *vp = ap->a_vp; 963 struct namecache *ncp = ap->a_nch->ncp; 964 struct tmpfs_dirent *de; 965 struct tmpfs_node *node; 966 struct tmpfs_node *dnode; 967 int error; 968 969 KKASSERT(dvp != vp); /* XXX When can this be false? */ 970 971 node = VP_TO_TMPFS_NODE(vp); 972 dnode = VP_TO_TMPFS_NODE(dvp); 973 TMPFS_NODE_LOCK(dnode); 974 975 /* XXX: Why aren't the following two tests done by the caller? */ 976 977 /* Hard links of directories are forbidden. */ 978 if (vp->v_type == VDIR) { 979 error = EPERM; 980 goto out; 981 } 982 983 /* Cannot create cross-device links. */ 984 if (dvp->v_mount != vp->v_mount) { 985 error = EXDEV; 986 goto out; 987 } 988 989 /* Ensure that we do not overflow the maximum number of links imposed 990 * by the system. */ 991 KKASSERT(node->tn_links <= LINK_MAX); 992 if (node->tn_links >= LINK_MAX) { 993 error = EMLINK; 994 goto out; 995 } 996 997 /* We cannot create links of files marked immutable or append-only. */ 998 if (node->tn_flags & (IMMUTABLE | APPEND)) { 999 error = EPERM; 1000 goto out; 1001 } 1002 1003 /* Allocate a new directory entry to represent the node. */ 1004 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 1005 ncp->nc_name, ncp->nc_nlen, &de); 1006 if (error != 0) 1007 goto out; 1008 1009 /* Insert the new directory entry into the appropriate directory. */ 1010 tmpfs_dir_attach(dnode, de); 1011 1012 /* vp link count has changed, so update node times. */ 1013 1014 TMPFS_NODE_LOCK(node); 1015 node->tn_status |= TMPFS_NODE_CHANGED; 1016 TMPFS_NODE_UNLOCK(node); 1017 tmpfs_update(vp); 1018 1019 tmpfs_knote(vp, NOTE_LINK); 1020 cache_setunresolved(ap->a_nch); 1021 cache_setvp(ap->a_nch, vp); 1022 error = 0; 1023 1024 out: 1025 TMPFS_NODE_UNLOCK(dnode); 1026 if (error == 0) 1027 tmpfs_knote(dvp, NOTE_WRITE); 1028 return error; 1029 } 1030 1031 /* --------------------------------------------------------------------- */ 1032 1033 static int 1034 tmpfs_nrename(struct vop_nrename_args *ap) 1035 { 1036 struct vnode *fdvp = ap->a_fdvp; 1037 struct namecache *fncp = ap->a_fnch->ncp; 1038 struct vnode *fvp = fncp->nc_vp; 1039 struct vnode *tdvp = ap->a_tdvp; 1040 struct namecache *tncp = ap->a_tnch->ncp; 1041 struct vnode *tvp; 1042 struct tmpfs_dirent *de, *tde; 1043 struct tmpfs_mount *tmp; 1044 struct tmpfs_node *fdnode; 1045 struct tmpfs_node *fnode; 1046 struct tmpfs_node *tnode; 1047 struct tmpfs_node *tdnode; 1048 char *newname; 1049 char *oldname; 1050 int error; 1051 1052 KKASSERT(fdvp->v_mount == fvp->v_mount); 1053 1054 /* 1055 * Because tvp can get overwritten we have to vget it instead of 1056 * just vref or use it, otherwise it's VINACTIVE flag may not get 1057 * cleared and the node won't get destroyed. 1058 */ 1059 error = cache_vget(ap->a_tnch, ap->a_cred, LK_SHARED, &tvp); 1060 if (error == 0) { 1061 tnode = VP_TO_TMPFS_NODE(tvp); 1062 vn_unlock(tvp); 1063 } else { 1064 tnode = NULL; 1065 } 1066 1067 /* Disallow cross-device renames. 1068 * XXX Why isn't this done by the caller? */ 1069 if (fvp->v_mount != tdvp->v_mount || 1070 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 1071 error = EXDEV; 1072 goto out; 1073 } 1074 1075 tmp = VFS_TO_TMPFS(tdvp->v_mount); 1076 tdnode = VP_TO_TMPFS_DIR(tdvp); 1077 1078 /* If source and target are the same file, there is nothing to do. */ 1079 if (fvp == tvp) { 1080 error = 0; 1081 goto out; 1082 } 1083 1084 fdnode = VP_TO_TMPFS_DIR(fdvp); 1085 fnode = VP_TO_TMPFS_NODE(fvp); 1086 TMPFS_NODE_LOCK(fdnode); 1087 de = tmpfs_dir_lookup(fdnode, fnode, fncp); 1088 TMPFS_NODE_UNLOCK(fdnode); /* XXX depend on namecache lock */ 1089 1090 /* Avoid manipulating '.' and '..' entries. */ 1091 if (de == NULL) { 1092 error = ENOENT; 1093 goto out_locked; 1094 } 1095 KKASSERT(de->td_node == fnode); 1096 1097 /* 1098 * If replacing an entry in the target directory and that entry 1099 * is a directory, it must be empty. 1100 * 1101 * Kern_rename gurantees the destination to be a directory 1102 * if the source is one (it does?). 1103 */ 1104 if (tvp != NULL) { 1105 KKASSERT(tnode != NULL); 1106 1107 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 1108 (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 1109 error = EPERM; 1110 goto out_locked; 1111 } 1112 1113 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 1114 if (tnode->tn_size > 0) { 1115 error = ENOTEMPTY; 1116 goto out_locked; 1117 } 1118 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 1119 error = ENOTDIR; 1120 goto out_locked; 1121 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 1122 error = EISDIR; 1123 goto out_locked; 1124 } else { 1125 KKASSERT(fnode->tn_type != VDIR && 1126 tnode->tn_type != VDIR); 1127 } 1128 } 1129 1130 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 1131 (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 1132 error = EPERM; 1133 goto out_locked; 1134 } 1135 1136 /* 1137 * Ensure that we have enough memory to hold the new name, if it 1138 * has to be changed. 1139 */ 1140 if (fncp->nc_nlen != tncp->nc_nlen || 1141 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) { 1142 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone, 1143 M_WAITOK | M_NULLOK); 1144 if (newname == NULL) { 1145 error = ENOSPC; 1146 goto out_locked; 1147 } 1148 bcopy(tncp->nc_name, newname, tncp->nc_nlen); 1149 newname[tncp->nc_nlen] = '\0'; 1150 } else { 1151 newname = NULL; 1152 } 1153 1154 /* 1155 * Unlink entry from source directory. Note that the kernel has 1156 * already checked for illegal recursion cases (renaming a directory 1157 * into a subdirectory of itself). 1158 */ 1159 if (fdnode != tdnode) { 1160 tmpfs_dir_detach(fdnode, de); 1161 } else { 1162 /* XXX depend on namecache lock */ 1163 TMPFS_NODE_LOCK(fdnode); 1164 KKASSERT(de == tmpfs_dir_lookup(fdnode, fnode, fncp)); 1165 RB_REMOVE(tmpfs_dirtree, &fdnode->tn_dir.tn_dirtree, de); 1166 RB_REMOVE(tmpfs_dirtree_cookie, 1167 &fdnode->tn_dir.tn_cookietree, de); 1168 TMPFS_NODE_UNLOCK(fdnode); 1169 } 1170 1171 /* 1172 * Handle any name change. Swap with newname, we will 1173 * deallocate it at the end. 1174 */ 1175 if (newname != NULL) { 1176 #if 0 1177 TMPFS_NODE_LOCK(fnode); 1178 fnode->tn_status |= TMPFS_NODE_CHANGED; 1179 TMPFS_NODE_UNLOCK(fnode); 1180 #endif 1181 oldname = de->td_name; 1182 de->td_name = newname; 1183 de->td_namelen = (uint16_t)tncp->nc_nlen; 1184 newname = oldname; 1185 } 1186 1187 /* 1188 * If we are overwriting an entry, we have to remove the old one 1189 * from the target directory. 1190 */ 1191 if (tvp != NULL) { 1192 /* Remove the old entry from the target directory. */ 1193 TMPFS_NODE_LOCK(tdnode); 1194 tde = tmpfs_dir_lookup(tdnode, tnode, tncp); 1195 tmpfs_dir_detach(tdnode, tde); 1196 TMPFS_NODE_UNLOCK(tdnode); 1197 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE); 1198 1199 /* 1200 * Free the directory entry we just deleted. Note that the 1201 * node referred by it will not be removed until the vnode is 1202 * really reclaimed. 1203 */ 1204 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde); 1205 /*cache_inval_vp(tvp, CINV_DESTROY);*/ 1206 } 1207 1208 /* 1209 * Link entry to target directory. If the entry 1210 * represents a directory move the parent linkage 1211 * as well. 1212 */ 1213 if (fdnode != tdnode) { 1214 if (de->td_node->tn_type == VDIR) { 1215 TMPFS_VALIDATE_DIR(fnode); 1216 } 1217 tmpfs_dir_attach(tdnode, de); 1218 } else { 1219 TMPFS_NODE_LOCK(tdnode); 1220 tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1221 RB_INSERT(tmpfs_dirtree, &tdnode->tn_dir.tn_dirtree, de); 1222 RB_INSERT(tmpfs_dirtree_cookie, 1223 &tdnode->tn_dir.tn_cookietree, de); 1224 TMPFS_NODE_UNLOCK(tdnode); 1225 } 1226 1227 /* 1228 * Finish up 1229 */ 1230 if (newname) { 1231 kfree(newname, tmp->tm_name_zone); 1232 newname = NULL; 1233 } 1234 cache_rename(ap->a_fnch, ap->a_tnch); 1235 tmpfs_knote(ap->a_fdvp, NOTE_WRITE); 1236 tmpfs_knote(ap->a_tdvp, NOTE_WRITE); 1237 if (fnode->tn_vnode) 1238 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME); 1239 error = 0; 1240 1241 out_locked: 1242 ; 1243 out: 1244 if (tvp) 1245 vrele(tvp); 1246 return error; 1247 } 1248 1249 /* --------------------------------------------------------------------- */ 1250 1251 static int 1252 tmpfs_nmkdir(struct vop_nmkdir_args *ap) 1253 { 1254 struct vnode *dvp = ap->a_dvp; 1255 struct vnode **vpp = ap->a_vpp; 1256 struct namecache *ncp = ap->a_nch->ncp; 1257 struct vattr *vap = ap->a_vap; 1258 struct ucred *cred = ap->a_cred; 1259 int error; 1260 1261 KKASSERT(vap->va_type == VDIR); 1262 1263 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL); 1264 if (error == 0) { 1265 cache_setunresolved(ap->a_nch); 1266 cache_setvp(ap->a_nch, *vpp); 1267 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK); 1268 } 1269 return error; 1270 } 1271 1272 /* --------------------------------------------------------------------- */ 1273 1274 static int 1275 tmpfs_nrmdir(struct vop_nrmdir_args *ap) 1276 { 1277 struct vnode *dvp = ap->a_dvp; 1278 struct namecache *ncp = ap->a_nch->ncp; 1279 struct vnode *vp; 1280 struct tmpfs_dirent *de; 1281 struct tmpfs_mount *tmp; 1282 struct tmpfs_node *dnode; 1283 struct tmpfs_node *node; 1284 int error; 1285 1286 /* 1287 * We have to acquire the vp from ap->a_nch because we will likely 1288 * unresolve the namecache entry, and a vrele/vput is needed to 1289 * trigger the tmpfs_inactive/tmpfs_reclaim sequence. 1290 * 1291 * We have to use vget to clear any inactive state on the vnode, 1292 * otherwise the vnode may remain inactive and thus tmpfs_inactive 1293 * will not get called when we release it. 1294 */ 1295 error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp); 1296 KKASSERT(error == 0); 1297 vn_unlock(vp); 1298 1299 /* 1300 * Prevalidate so we don't hit an assertion later 1301 */ 1302 if (vp->v_type != VDIR) { 1303 error = ENOTDIR; 1304 goto out; 1305 } 1306 1307 tmp = VFS_TO_TMPFS(dvp->v_mount); 1308 dnode = VP_TO_TMPFS_DIR(dvp); 1309 node = VP_TO_TMPFS_DIR(vp); 1310 1311 /* 1312 * Directories with more than two entries ('.' and '..') cannot 1313 * be removed. 1314 */ 1315 if (node->tn_size > 0) { 1316 error = ENOTEMPTY; 1317 goto out; 1318 } 1319 1320 if ((dnode->tn_flags & APPEND) 1321 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1322 error = EPERM; 1323 goto out; 1324 } 1325 1326 /* 1327 * This invariant holds only if we are not trying to 1328 * remove "..". We checked for that above so this is safe now. 1329 */ 1330 KKASSERT(node->tn_dir.tn_parent == dnode); 1331 1332 /* 1333 * Get the directory entry associated with node (vp). This 1334 * was filled by tmpfs_lookup while looking up the entry. 1335 */ 1336 TMPFS_NODE_LOCK(dnode); 1337 de = tmpfs_dir_lookup(dnode, node, ncp); 1338 KKASSERT(TMPFS_DIRENT_MATCHES(de, ncp->nc_name, ncp->nc_nlen)); 1339 1340 /* Check flags to see if we are allowed to remove the directory. */ 1341 if ((dnode->tn_flags & APPEND) || 1342 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1343 error = EPERM; 1344 TMPFS_NODE_UNLOCK(dnode); 1345 goto out; 1346 } 1347 1348 /* Detach the directory entry from the directory (dnode). */ 1349 tmpfs_dir_detach(dnode, de); 1350 TMPFS_NODE_UNLOCK(dnode); 1351 1352 /* No vnode should be allocated for this entry from this point */ 1353 TMPFS_NODE_LOCK(dnode); 1354 TMPFS_ASSERT_ELOCKED(dnode); 1355 TMPFS_NODE_LOCK(node); 1356 TMPFS_ASSERT_ELOCKED(node); 1357 1358 /* 1359 * Must set parent linkage to NULL (tested by ncreate to disallow 1360 * the creation of new files/dirs in a deleted directory) 1361 */ 1362 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | 1363 TMPFS_NODE_MODIFIED; 1364 1365 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | 1366 TMPFS_NODE_MODIFIED; 1367 1368 TMPFS_NODE_UNLOCK(node); 1369 TMPFS_NODE_UNLOCK(dnode); 1370 1371 /* Free the directory entry we just deleted. Note that the node 1372 * referred by it will not be removed until the vnode is really 1373 * reclaimed. */ 1374 tmpfs_free_dirent(tmp, de); 1375 1376 /* Release the deleted vnode (will destroy the node, notify 1377 * interested parties and clean it from the cache). */ 1378 1379 TMPFS_NODE_LOCK(dnode); 1380 dnode->tn_status |= TMPFS_NODE_CHANGED; 1381 TMPFS_NODE_UNLOCK(dnode); 1382 tmpfs_update(dvp); 1383 1384 cache_unlink(ap->a_nch); 1385 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK); 1386 error = 0; 1387 1388 out: 1389 vrele(vp); 1390 1391 return error; 1392 } 1393 1394 /* --------------------------------------------------------------------- */ 1395 1396 static int 1397 tmpfs_nsymlink(struct vop_nsymlink_args *ap) 1398 { 1399 struct vnode *dvp = ap->a_dvp; 1400 struct vnode **vpp = ap->a_vpp; 1401 struct namecache *ncp = ap->a_nch->ncp; 1402 struct vattr *vap = ap->a_vap; 1403 struct ucred *cred = ap->a_cred; 1404 char *target = ap->a_target; 1405 int error; 1406 1407 vap->va_type = VLNK; 1408 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target); 1409 if (error == 0) { 1410 tmpfs_knote(*vpp, NOTE_WRITE); 1411 cache_setunresolved(ap->a_nch); 1412 cache_setvp(ap->a_nch, *vpp); 1413 } 1414 return error; 1415 } 1416 1417 /* --------------------------------------------------------------------- */ 1418 1419 static int 1420 tmpfs_readdir(struct vop_readdir_args *ap) 1421 { 1422 struct vnode *vp = ap->a_vp; 1423 struct uio *uio = ap->a_uio; 1424 int *eofflag = ap->a_eofflag; 1425 off_t **cookies = ap->a_cookies; 1426 int *ncookies = ap->a_ncookies; 1427 struct tmpfs_mount *tmp; 1428 int error; 1429 off_t startoff; 1430 off_t cnt = 0; 1431 struct tmpfs_node *node; 1432 1433 /* This operation only makes sense on directory nodes. */ 1434 if (vp->v_type != VDIR) { 1435 return ENOTDIR; 1436 } 1437 1438 tmp = VFS_TO_TMPFS(vp->v_mount); 1439 node = VP_TO_TMPFS_DIR(vp); 1440 startoff = uio->uio_offset; 1441 1442 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 1443 error = tmpfs_dir_getdotdent(node, uio); 1444 if (error != 0) { 1445 TMPFS_NODE_LOCK_SH(node); 1446 goto outok; 1447 } 1448 cnt++; 1449 } 1450 1451 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 1452 /* may lock parent, cannot hold node lock */ 1453 error = tmpfs_dir_getdotdotdent(tmp, node, uio); 1454 if (error != 0) { 1455 TMPFS_NODE_LOCK_SH(node); 1456 goto outok; 1457 } 1458 cnt++; 1459 } 1460 1461 TMPFS_NODE_LOCK_SH(node); 1462 error = tmpfs_dir_getdents(node, uio, &cnt); 1463 1464 outok: 1465 KKASSERT(error >= -1); 1466 1467 if (error == -1) 1468 error = 0; 1469 1470 if (eofflag != NULL) 1471 *eofflag = 1472 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1473 1474 /* Update NFS-related variables. */ 1475 if (error == 0 && cookies != NULL && ncookies != NULL) { 1476 off_t i; 1477 off_t off = startoff; 1478 struct tmpfs_dirent *de = NULL; 1479 1480 *ncookies = cnt; 1481 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 1482 1483 for (i = 0; i < cnt; i++) { 1484 KKASSERT(off != TMPFS_DIRCOOKIE_EOF); 1485 if (off == TMPFS_DIRCOOKIE_DOT) { 1486 off = TMPFS_DIRCOOKIE_DOTDOT; 1487 } else { 1488 if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1489 de = RB_MIN(tmpfs_dirtree_cookie, 1490 &node->tn_dir.tn_cookietree); 1491 } else if (de != NULL) { 1492 de = RB_NEXT(tmpfs_dirtree_cookie, 1493 &node->tn_dir.tn_cookietree, de); 1494 } else { 1495 de = tmpfs_dir_lookupbycookie(node, 1496 off); 1497 KKASSERT(de != NULL); 1498 de = RB_NEXT(tmpfs_dirtree_cookie, 1499 &node->tn_dir.tn_cookietree, de); 1500 } 1501 if (de == NULL) 1502 off = TMPFS_DIRCOOKIE_EOF; 1503 else 1504 off = tmpfs_dircookie(de); 1505 } 1506 (*cookies)[i] = off; 1507 } 1508 KKASSERT(uio->uio_offset == off); 1509 } 1510 TMPFS_NODE_UNLOCK(node); 1511 1512 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) { 1513 TMPFS_NODE_LOCK(node); 1514 node->tn_status |= TMPFS_NODE_ACCESSED; 1515 TMPFS_NODE_UNLOCK(node); 1516 } 1517 return error; 1518 } 1519 1520 /* --------------------------------------------------------------------- */ 1521 1522 static int 1523 tmpfs_readlink(struct vop_readlink_args *ap) 1524 { 1525 struct vnode *vp = ap->a_vp; 1526 struct uio *uio = ap->a_uio; 1527 int error; 1528 struct tmpfs_node *node; 1529 1530 KKASSERT(uio->uio_offset == 0); 1531 KKASSERT(vp->v_type == VLNK); 1532 1533 node = VP_TO_TMPFS_NODE(vp); 1534 TMPFS_NODE_LOCK_SH(node); 1535 error = uiomove(node->tn_link, 1536 MIN(node->tn_size, uio->uio_resid), uio); 1537 TMPFS_NODE_UNLOCK(node); 1538 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) { 1539 TMPFS_NODE_LOCK(node); 1540 node->tn_status |= TMPFS_NODE_ACCESSED; 1541 TMPFS_NODE_UNLOCK(node); 1542 } 1543 return error; 1544 } 1545 1546 /* --------------------------------------------------------------------- */ 1547 1548 static int 1549 tmpfs_inactive(struct vop_inactive_args *ap) 1550 { 1551 struct vnode *vp = ap->a_vp; 1552 struct tmpfs_node *node; 1553 struct mount *mp; 1554 1555 mp = vp->v_mount; 1556 lwkt_gettoken(&mp->mnt_token); 1557 node = VP_TO_TMPFS_NODE(vp); 1558 1559 /* 1560 * Degenerate case 1561 */ 1562 if (node == NULL) { 1563 vrecycle(vp); 1564 lwkt_reltoken(&mp->mnt_token); 1565 return(0); 1566 } 1567 1568 /* 1569 * Get rid of unreferenced deleted vnodes sooner rather than 1570 * later so the data memory can be recovered immediately. 1571 * 1572 * We must truncate the vnode to prevent the normal reclamation 1573 * path from flushing the data for the removed file to disk. 1574 */ 1575 TMPFS_NODE_LOCK(node); 1576 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 && 1577 node->tn_links == 0) 1578 { 1579 node->tn_vpstate = TMPFS_VNODE_DOOMED; 1580 TMPFS_NODE_UNLOCK(node); 1581 if (node->tn_type == VREG) 1582 tmpfs_truncate(vp, 0); 1583 vrecycle(vp); 1584 } else { 1585 TMPFS_NODE_UNLOCK(node); 1586 } 1587 lwkt_reltoken(&mp->mnt_token); 1588 1589 return 0; 1590 } 1591 1592 /* --------------------------------------------------------------------- */ 1593 1594 int 1595 tmpfs_reclaim(struct vop_reclaim_args *ap) 1596 { 1597 struct vnode *vp = ap->a_vp; 1598 struct tmpfs_mount *tmp; 1599 struct tmpfs_node *node; 1600 struct mount *mp; 1601 1602 mp = vp->v_mount; 1603 lwkt_gettoken(&mp->mnt_token); 1604 1605 node = VP_TO_TMPFS_NODE(vp); 1606 tmp = VFS_TO_TMPFS(vp->v_mount); 1607 KKASSERT(mp == tmp->tm_mount); 1608 1609 tmpfs_free_vp(vp); 1610 1611 /* 1612 * If the node referenced by this vnode was deleted by the 1613 * user, we must free its associated data structures now that 1614 * the vnode is being reclaimed. 1615 * 1616 * Directories have an extra link ref. 1617 */ 1618 TMPFS_NODE_LOCK(node); 1619 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 && 1620 node->tn_links == 0) { 1621 node->tn_vpstate = TMPFS_VNODE_DOOMED; 1622 tmpfs_free_node(tmp, node); 1623 /* eats the lock */ 1624 } else { 1625 TMPFS_NODE_UNLOCK(node); 1626 } 1627 lwkt_reltoken(&mp->mnt_token); 1628 1629 KKASSERT(vp->v_data == NULL); 1630 return 0; 1631 } 1632 1633 /* --------------------------------------------------------------------- */ 1634 1635 static int 1636 tmpfs_mountctl(struct vop_mountctl_args *ap) 1637 { 1638 struct tmpfs_mount *tmp; 1639 struct mount *mp; 1640 int rc; 1641 1642 mp = ap->a_head.a_ops->head.vv_mount; 1643 lwkt_gettoken(&mp->mnt_token); 1644 1645 switch (ap->a_op) { 1646 case (MOUNTCTL_SET_EXPORT): 1647 tmp = (struct tmpfs_mount *) mp->mnt_data; 1648 1649 if (ap->a_ctllen != sizeof(struct export_args)) 1650 rc = (EINVAL); 1651 else 1652 rc = vfs_export(mp, &tmp->tm_export, 1653 (const struct export_args *) ap->a_ctl); 1654 break; 1655 default: 1656 rc = vop_stdmountctl(ap); 1657 break; 1658 } 1659 1660 lwkt_reltoken(&mp->mnt_token); 1661 return (rc); 1662 } 1663 1664 /* --------------------------------------------------------------------- */ 1665 1666 static int 1667 tmpfs_print(struct vop_print_args *ap) 1668 { 1669 struct vnode *vp = ap->a_vp; 1670 1671 struct tmpfs_node *node; 1672 1673 node = VP_TO_TMPFS_NODE(vp); 1674 1675 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n", 1676 node, node->tn_flags, node->tn_links); 1677 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n", 1678 node->tn_mode, node->tn_uid, node->tn_gid, 1679 (uintmax_t)node->tn_size, node->tn_status); 1680 1681 if (vp->v_type == VFIFO) 1682 fifo_printinfo(vp); 1683 1684 kprintf("\n"); 1685 1686 return 0; 1687 } 1688 1689 /* --------------------------------------------------------------------- */ 1690 1691 static int 1692 tmpfs_pathconf(struct vop_pathconf_args *ap) 1693 { 1694 struct vnode *vp = ap->a_vp; 1695 int name = ap->a_name; 1696 register_t *retval = ap->a_retval; 1697 struct tmpfs_mount *tmp; 1698 int error; 1699 1700 error = 0; 1701 1702 switch (name) { 1703 case _PC_CHOWN_RESTRICTED: 1704 *retval = 1; 1705 break; 1706 1707 case _PC_FILESIZEBITS: 1708 tmp = VFS_TO_TMPFS(vp->v_mount); 1709 *retval = max(32, flsll(tmp->tm_pages_max * PAGE_SIZE) + 1); 1710 break; 1711 1712 case _PC_LINK_MAX: 1713 *retval = LINK_MAX; 1714 break; 1715 1716 case _PC_NAME_MAX: 1717 *retval = NAME_MAX; 1718 break; 1719 1720 case _PC_NO_TRUNC: 1721 *retval = 1; 1722 break; 1723 1724 case _PC_PATH_MAX: 1725 *retval = PATH_MAX; 1726 break; 1727 1728 case _PC_PIPE_BUF: 1729 *retval = PIPE_BUF; 1730 break; 1731 1732 case _PC_SYNC_IO: 1733 *retval = 1; 1734 break; 1735 1736 case _PC_2_SYMLINKS: 1737 *retval = 1; 1738 break; 1739 1740 default: 1741 error = EINVAL; 1742 } 1743 1744 return error; 1745 } 1746 1747 /************************************************************************ 1748 * KQFILTER OPS * 1749 ************************************************************************/ 1750 1751 static void filt_tmpfsdetach(struct knote *kn); 1752 static int filt_tmpfsread(struct knote *kn, long hint); 1753 static int filt_tmpfswrite(struct knote *kn, long hint); 1754 static int filt_tmpfsvnode(struct knote *kn, long hint); 1755 1756 static struct filterops tmpfsread_filtops = 1757 { FILTEROP_ISFD | FILTEROP_MPSAFE, 1758 NULL, filt_tmpfsdetach, filt_tmpfsread }; 1759 static struct filterops tmpfswrite_filtops = 1760 { FILTEROP_ISFD | FILTEROP_MPSAFE, 1761 NULL, filt_tmpfsdetach, filt_tmpfswrite }; 1762 static struct filterops tmpfsvnode_filtops = 1763 { FILTEROP_ISFD | FILTEROP_MPSAFE, 1764 NULL, filt_tmpfsdetach, filt_tmpfsvnode }; 1765 1766 static int 1767 tmpfs_kqfilter (struct vop_kqfilter_args *ap) 1768 { 1769 struct vnode *vp = ap->a_vp; 1770 struct knote *kn = ap->a_kn; 1771 1772 switch (kn->kn_filter) { 1773 case EVFILT_READ: 1774 kn->kn_fop = &tmpfsread_filtops; 1775 break; 1776 case EVFILT_WRITE: 1777 kn->kn_fop = &tmpfswrite_filtops; 1778 break; 1779 case EVFILT_VNODE: 1780 kn->kn_fop = &tmpfsvnode_filtops; 1781 break; 1782 default: 1783 return (EOPNOTSUPP); 1784 } 1785 1786 kn->kn_hook = (caddr_t)vp; 1787 1788 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 1789 1790 return(0); 1791 } 1792 1793 static void 1794 filt_tmpfsdetach(struct knote *kn) 1795 { 1796 struct vnode *vp = (void *)kn->kn_hook; 1797 1798 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 1799 } 1800 1801 static int 1802 filt_tmpfsread(struct knote *kn, long hint) 1803 { 1804 struct vnode *vp = (void *)kn->kn_hook; 1805 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp); 1806 off_t off; 1807 1808 if (hint == NOTE_REVOKE) { 1809 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 1810 return(1); 1811 } 1812 1813 /* 1814 * Interlock against MP races when performing this function. 1815 */ 1816 TMPFS_NODE_LOCK_SH(node); 1817 off = node->tn_size - kn->kn_fp->f_offset; 1818 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; 1819 if (kn->kn_sfflags & NOTE_OLDAPI) { 1820 TMPFS_NODE_UNLOCK(node); 1821 return(1); 1822 } 1823 if (kn->kn_data == 0) { 1824 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; 1825 } 1826 TMPFS_NODE_UNLOCK(node); 1827 return (kn->kn_data != 0); 1828 } 1829 1830 static int 1831 filt_tmpfswrite(struct knote *kn, long hint) 1832 { 1833 if (hint == NOTE_REVOKE) 1834 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 1835 kn->kn_data = 0; 1836 return (1); 1837 } 1838 1839 static int 1840 filt_tmpfsvnode(struct knote *kn, long hint) 1841 { 1842 if (kn->kn_sfflags & hint) 1843 kn->kn_fflags |= hint; 1844 if (hint == NOTE_REVOKE) { 1845 kn->kn_flags |= (EV_EOF | EV_NODATA); 1846 return (1); 1847 } 1848 return (kn->kn_fflags != 0); 1849 } 1850 1851 1852 /* --------------------------------------------------------------------- */ 1853 1854 /* 1855 * vnode operations vector used for files stored in a tmpfs file system. 1856 */ 1857 struct vop_ops tmpfs_vnode_vops = { 1858 .vop_default = vop_defaultop, 1859 .vop_getpages = vop_stdgetpages, 1860 .vop_putpages = vop_stdputpages, 1861 .vop_ncreate = tmpfs_ncreate, 1862 .vop_nresolve = tmpfs_nresolve, 1863 .vop_nlookupdotdot = tmpfs_nlookupdotdot, 1864 .vop_nmknod = tmpfs_nmknod, 1865 .vop_open = tmpfs_open, 1866 .vop_close = tmpfs_close, 1867 .vop_access = tmpfs_access, 1868 .vop_getattr = tmpfs_getattr, 1869 .vop_setattr = tmpfs_setattr, 1870 .vop_read = tmpfs_read, 1871 .vop_write = tmpfs_write, 1872 .vop_fsync = tmpfs_fsync, 1873 .vop_mountctl = tmpfs_mountctl, 1874 .vop_nremove = tmpfs_nremove, 1875 .vop_nlink = tmpfs_nlink, 1876 .vop_nrename = tmpfs_nrename, 1877 .vop_nmkdir = tmpfs_nmkdir, 1878 .vop_nrmdir = tmpfs_nrmdir, 1879 .vop_nsymlink = tmpfs_nsymlink, 1880 .vop_readdir = tmpfs_readdir, 1881 .vop_readlink = tmpfs_readlink, 1882 .vop_inactive = tmpfs_inactive, 1883 .vop_reclaim = tmpfs_reclaim, 1884 .vop_print = tmpfs_print, 1885 .vop_pathconf = tmpfs_pathconf, 1886 .vop_bmap = tmpfs_bmap, 1887 .vop_strategy = tmpfs_strategy, 1888 .vop_advlock = tmpfs_advlock, 1889 .vop_kqfilter = tmpfs_kqfilter 1890 }; 1891