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