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