tmpfs_vnops.c revision 231669
1/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */ 2 3/*- 4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9 * 2005 program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/* 34 * tmpfs vnode interface. 35 */ 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_vnops.c 231669 2012-02-14 11:24:24Z tijl $"); 38 39#include <sys/param.h> 40#include <sys/fcntl.h> 41#include <sys/lockf.h> 42#include <sys/namei.h> 43#include <sys/priv.h> 44#include <sys/proc.h> 45#include <sys/sched.h> 46#include <sys/sf_buf.h> 47#include <sys/stat.h> 48#include <sys/systm.h> 49#include <sys/unistd.h> 50#include <sys/vnode.h> 51 52#include <vm/vm.h> 53#include <vm/vm_object.h> 54#include <vm/vm_page.h> 55#include <vm/vm_pager.h> 56 57#include <fs/fifofs/fifo.h> 58#include <fs/tmpfs/tmpfs_vnops.h> 59#include <fs/tmpfs/tmpfs.h> 60 61/* --------------------------------------------------------------------- */ 62 63static int 64tmpfs_lookup(struct vop_cachedlookup_args *v) 65{ 66 struct vnode *dvp = v->a_dvp; 67 struct vnode **vpp = v->a_vpp; 68 struct componentname *cnp = v->a_cnp; 69 70 int error; 71 struct tmpfs_dirent *de; 72 struct tmpfs_node *dnode; 73 74 dnode = VP_TO_TMPFS_DIR(dvp); 75 *vpp = NULLVP; 76 77 /* Check accessibility of requested node as a first step. */ 78 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread); 79 if (error != 0) 80 goto out; 81 82 /* We cannot be requesting the parent directory of the root node. */ 83 MPASS(IMPLIES(dnode->tn_type == VDIR && 84 dnode->tn_dir.tn_parent == dnode, 85 !(cnp->cn_flags & ISDOTDOT))); 86 87 TMPFS_ASSERT_LOCKED(dnode); 88 if (dnode->tn_dir.tn_parent == NULL) { 89 error = ENOENT; 90 goto out; 91 } 92 if (cnp->cn_flags & ISDOTDOT) { 93 int ltype = 0; 94 95 ltype = VOP_ISLOCKED(dvp); 96 vhold(dvp); 97 VOP_UNLOCK(dvp, 0); 98 /* Allocate a new vnode on the matching entry. */ 99 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent, 100 cnp->cn_lkflags, vpp); 101 102 vn_lock(dvp, ltype | LK_RETRY); 103 vdrop(dvp); 104 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 105 VREF(dvp); 106 *vpp = dvp; 107 error = 0; 108 } else { 109 de = tmpfs_dir_lookup(dnode, NULL, cnp); 110 if (de != NULL && de->td_node == NULL) 111 cnp->cn_flags |= ISWHITEOUT; 112 if (de == NULL || de->td_node == NULL) { 113 /* The entry was not found in the directory. 114 * This is OK if we are creating or renaming an 115 * entry and are working on the last component of 116 * the path name. */ 117 if ((cnp->cn_flags & ISLASTCN) && 118 (cnp->cn_nameiop == CREATE || \ 119 cnp->cn_nameiop == RENAME || 120 (cnp->cn_nameiop == DELETE && 121 cnp->cn_flags & DOWHITEOUT && 122 cnp->cn_flags & ISWHITEOUT))) { 123 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 124 cnp->cn_thread); 125 if (error != 0) 126 goto out; 127 128 /* Keep the component name in the buffer for 129 * future uses. */ 130 cnp->cn_flags |= SAVENAME; 131 132 error = EJUSTRETURN; 133 } else 134 error = ENOENT; 135 } else { 136 struct tmpfs_node *tnode; 137 138 /* The entry was found, so get its associated 139 * tmpfs_node. */ 140 tnode = de->td_node; 141 142 /* If we are not at the last path component and 143 * found a non-directory or non-link entry (which 144 * may itself be pointing to a directory), raise 145 * an error. */ 146 if ((tnode->tn_type != VDIR && 147 tnode->tn_type != VLNK) && 148 !(cnp->cn_flags & ISLASTCN)) { 149 error = ENOTDIR; 150 goto out; 151 } 152 153 /* If we are deleting or renaming the entry, keep 154 * track of its tmpfs_dirent so that it can be 155 * easily deleted later. */ 156 if ((cnp->cn_flags & ISLASTCN) && 157 (cnp->cn_nameiop == DELETE || 158 cnp->cn_nameiop == RENAME)) { 159 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 160 cnp->cn_thread); 161 if (error != 0) 162 goto out; 163 164 /* Allocate a new vnode on the matching entry. */ 165 error = tmpfs_alloc_vp(dvp->v_mount, tnode, 166 cnp->cn_lkflags, vpp); 167 if (error != 0) 168 goto out; 169 170 if ((dnode->tn_mode & S_ISTXT) && 171 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) && 172 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) { 173 error = EPERM; 174 vput(*vpp); 175 *vpp = NULL; 176 goto out; 177 } 178 cnp->cn_flags |= SAVENAME; 179 } else { 180 error = tmpfs_alloc_vp(dvp->v_mount, tnode, 181 cnp->cn_lkflags, vpp); 182 } 183 } 184 } 185 186 /* Store the result of this lookup in the cache. Avoid this if the 187 * request was for creation, as it does not improve timings on 188 * emprical tests. */ 189 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) 190 cache_enter(dvp, *vpp, cnp); 191 192out: 193 /* If there were no errors, *vpp cannot be null and it must be 194 * locked. */ 195 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp))); 196 197 return error; 198} 199 200/* --------------------------------------------------------------------- */ 201 202static int 203tmpfs_create(struct vop_create_args *v) 204{ 205 struct vnode *dvp = v->a_dvp; 206 struct vnode **vpp = v->a_vpp; 207 struct componentname *cnp = v->a_cnp; 208 struct vattr *vap = v->a_vap; 209 210 MPASS(vap->va_type == VREG || vap->va_type == VSOCK); 211 212 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 213} 214/* --------------------------------------------------------------------- */ 215 216static int 217tmpfs_mknod(struct vop_mknod_args *v) 218{ 219 struct vnode *dvp = v->a_dvp; 220 struct vnode **vpp = v->a_vpp; 221 struct componentname *cnp = v->a_cnp; 222 struct vattr *vap = v->a_vap; 223 224 if (vap->va_type != VBLK && vap->va_type != VCHR && 225 vap->va_type != VFIFO) 226 return EINVAL; 227 228 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 229} 230 231/* --------------------------------------------------------------------- */ 232 233static int 234tmpfs_open(struct vop_open_args *v) 235{ 236 struct vnode *vp = v->a_vp; 237 int mode = v->a_mode; 238 239 int error; 240 struct tmpfs_node *node; 241 242 MPASS(VOP_ISLOCKED(vp)); 243 244 node = VP_TO_TMPFS_NODE(vp); 245 246 /* The file is still active but all its names have been removed 247 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as 248 * it is about to die. */ 249 if (node->tn_links < 1) 250 return (ENOENT); 251 252 /* If the file is marked append-only, deny write requests. */ 253 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE) 254 error = EPERM; 255 else { 256 error = 0; 257 vnode_create_vobject(vp, node->tn_size, v->a_td); 258 } 259 260 MPASS(VOP_ISLOCKED(vp)); 261 return error; 262} 263 264/* --------------------------------------------------------------------- */ 265 266static int 267tmpfs_close(struct vop_close_args *v) 268{ 269 struct vnode *vp = v->a_vp; 270 271 MPASS(VOP_ISLOCKED(vp)); 272 273 /* Update node times. */ 274 tmpfs_update(vp); 275 276 return (0); 277} 278 279/* --------------------------------------------------------------------- */ 280 281int 282tmpfs_access(struct vop_access_args *v) 283{ 284 struct vnode *vp = v->a_vp; 285 accmode_t accmode = v->a_accmode; 286 struct ucred *cred = v->a_cred; 287 288 int error; 289 struct tmpfs_node *node; 290 291 MPASS(VOP_ISLOCKED(vp)); 292 293 node = VP_TO_TMPFS_NODE(vp); 294 295 switch (vp->v_type) { 296 case VDIR: 297 /* FALLTHROUGH */ 298 case VLNK: 299 /* FALLTHROUGH */ 300 case VREG: 301 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) { 302 error = EROFS; 303 goto out; 304 } 305 break; 306 307 case VBLK: 308 /* FALLTHROUGH */ 309 case VCHR: 310 /* FALLTHROUGH */ 311 case VSOCK: 312 /* FALLTHROUGH */ 313 case VFIFO: 314 break; 315 316 default: 317 error = EINVAL; 318 goto out; 319 } 320 321 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) { 322 error = EPERM; 323 goto out; 324 } 325 326 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, 327 node->tn_gid, accmode, cred, NULL); 328 329out: 330 MPASS(VOP_ISLOCKED(vp)); 331 332 return error; 333} 334 335/* --------------------------------------------------------------------- */ 336 337int 338tmpfs_getattr(struct vop_getattr_args *v) 339{ 340 struct vnode *vp = v->a_vp; 341 struct vattr *vap = v->a_vap; 342 343 struct tmpfs_node *node; 344 345 node = VP_TO_TMPFS_NODE(vp); 346 347 tmpfs_update(vp); 348 349 vap->va_type = vp->v_type; 350 vap->va_mode = node->tn_mode; 351 vap->va_nlink = node->tn_links; 352 vap->va_uid = node->tn_uid; 353 vap->va_gid = node->tn_gid; 354 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 355 vap->va_fileid = node->tn_id; 356 vap->va_size = node->tn_size; 357 vap->va_blocksize = PAGE_SIZE; 358 vap->va_atime = node->tn_atime; 359 vap->va_mtime = node->tn_mtime; 360 vap->va_ctime = node->tn_ctime; 361 vap->va_birthtime = node->tn_birthtime; 362 vap->va_gen = node->tn_gen; 363 vap->va_flags = node->tn_flags; 364 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 365 node->tn_rdev : NODEV; 366 vap->va_bytes = round_page(node->tn_size); 367 vap->va_filerev = 0; 368 369 return 0; 370} 371 372/* --------------------------------------------------------------------- */ 373 374/* XXX Should this operation be atomic? I think it should, but code in 375 * XXX other places (e.g., ufs) doesn't seem to be... */ 376int 377tmpfs_setattr(struct vop_setattr_args *v) 378{ 379 struct vnode *vp = v->a_vp; 380 struct vattr *vap = v->a_vap; 381 struct ucred *cred = v->a_cred; 382 struct thread *td = curthread; 383 384 int error; 385 386 MPASS(VOP_ISLOCKED(vp)); 387 388 error = 0; 389 390 /* Abort if any unsettable attribute is given. */ 391 if (vap->va_type != VNON || 392 vap->va_nlink != VNOVAL || 393 vap->va_fsid != VNOVAL || 394 vap->va_fileid != VNOVAL || 395 vap->va_blocksize != VNOVAL || 396 vap->va_gen != VNOVAL || 397 vap->va_rdev != VNOVAL || 398 vap->va_bytes != VNOVAL) 399 error = EINVAL; 400 401 if (error == 0 && (vap->va_flags != VNOVAL)) 402 error = tmpfs_chflags(vp, vap->va_flags, cred, td); 403 404 if (error == 0 && (vap->va_size != VNOVAL)) 405 error = tmpfs_chsize(vp, vap->va_size, cred, td); 406 407 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 408 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td); 409 410 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) 411 error = tmpfs_chmod(vp, vap->va_mode, cred, td); 412 413 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL && 414 vap->va_atime.tv_nsec != VNOVAL) || 415 (vap->va_mtime.tv_sec != VNOVAL && 416 vap->va_mtime.tv_nsec != VNOVAL) || 417 (vap->va_birthtime.tv_sec != VNOVAL && 418 vap->va_birthtime.tv_nsec != VNOVAL))) 419 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 420 &vap->va_birthtime, vap->va_vaflags, cred, td); 421 422 /* Update the node times. We give preference to the error codes 423 * generated by this function rather than the ones that may arise 424 * from tmpfs_update. */ 425 tmpfs_update(vp); 426 427 MPASS(VOP_ISLOCKED(vp)); 428 429 return error; 430} 431 432/* --------------------------------------------------------------------- */ 433static int 434tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx, 435 vm_offset_t offset, size_t tlen, struct uio *uio) 436{ 437 vm_page_t m; 438 int error, rv; 439 440 VM_OBJECT_LOCK(tobj); 441 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 442 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 443 if (m->valid != VM_PAGE_BITS_ALL) { 444 if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 445 rv = vm_pager_get_pages(tobj, &m, 1, 0); 446 if (rv != VM_PAGER_OK) { 447 vm_page_lock(m); 448 vm_page_free(m); 449 vm_page_unlock(m); 450 VM_OBJECT_UNLOCK(tobj); 451 return (EIO); 452 } 453 } else 454 vm_page_zero_invalid(m, TRUE); 455 } 456 VM_OBJECT_UNLOCK(tobj); 457 error = uiomove_fromphys(&m, offset, tlen, uio); 458 VM_OBJECT_LOCK(tobj); 459 vm_page_lock(m); 460 vm_page_unwire(m, TRUE); 461 vm_page_unlock(m); 462 vm_page_wakeup(m); 463 VM_OBJECT_UNLOCK(tobj); 464 465 return (error); 466} 467 468static __inline int 469tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx, 470 vm_offset_t offset, size_t tlen, void *buf) 471{ 472 struct uio uio; 473 struct iovec iov; 474 475 uio.uio_iovcnt = 1; 476 uio.uio_iov = &iov; 477 iov.iov_base = buf; 478 iov.iov_len = tlen; 479 480 uio.uio_offset = 0; 481 uio.uio_resid = tlen; 482 uio.uio_rw = UIO_READ; 483 uio.uio_segflg = UIO_SYSSPACE; 484 uio.uio_td = curthread; 485 486 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio)); 487} 488 489static int 490tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio) 491{ 492 struct sf_buf *sf; 493 vm_pindex_t idx; 494 vm_page_t m; 495 vm_offset_t offset; 496 off_t addr; 497 size_t tlen; 498 char *ma; 499 int error; 500 501 addr = uio->uio_offset; 502 idx = OFF_TO_IDX(addr); 503 offset = addr & PAGE_MASK; 504 tlen = MIN(PAGE_SIZE - offset, len); 505 506 if ((vobj == NULL) || 507 (vobj->resident_page_count == 0 && vobj->cache == NULL)) 508 goto nocache; 509 510 VM_OBJECT_LOCK(vobj); 511lookupvpg: 512 if (((m = vm_page_lookup(vobj, idx)) != NULL) && 513 vm_page_is_valid(m, offset, tlen)) { 514 if ((m->oflags & VPO_BUSY) != 0) { 515 /* 516 * Reference the page before unlocking and sleeping so 517 * that the page daemon is less likely to reclaim it. 518 */ 519 vm_page_reference(m); 520 vm_page_sleep(m, "tmfsmr"); 521 goto lookupvpg; 522 } 523 vm_page_busy(m); 524 VM_OBJECT_UNLOCK(vobj); 525 error = uiomove_fromphys(&m, offset, tlen, uio); 526 VM_OBJECT_LOCK(vobj); 527 vm_page_wakeup(m); 528 VM_OBJECT_UNLOCK(vobj); 529 return (error); 530 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) { 531 KASSERT(offset == 0, 532 ("unexpected offset in tmpfs_mappedread for sendfile")); 533 if ((m->oflags & VPO_BUSY) != 0) { 534 /* 535 * Reference the page before unlocking and sleeping so 536 * that the page daemon is less likely to reclaim it. 537 */ 538 vm_page_reference(m); 539 vm_page_sleep(m, "tmfsmr"); 540 goto lookupvpg; 541 } 542 vm_page_busy(m); 543 VM_OBJECT_UNLOCK(vobj); 544 sched_pin(); 545 sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 546 ma = (char *)sf_buf_kva(sf); 547 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma); 548 if (error == 0) { 549 if (tlen != PAGE_SIZE) 550 bzero(ma + tlen, PAGE_SIZE - tlen); 551 uio->uio_offset += tlen; 552 uio->uio_resid -= tlen; 553 } 554 sf_buf_free(sf); 555 sched_unpin(); 556 VM_OBJECT_LOCK(vobj); 557 if (error == 0) 558 m->valid = VM_PAGE_BITS_ALL; 559 vm_page_wakeup(m); 560 VM_OBJECT_UNLOCK(vobj); 561 return (error); 562 } 563 VM_OBJECT_UNLOCK(vobj); 564nocache: 565 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio); 566 567 return (error); 568} 569 570static int 571tmpfs_read(struct vop_read_args *v) 572{ 573 struct vnode *vp = v->a_vp; 574 struct uio *uio = v->a_uio; 575 576 struct tmpfs_node *node; 577 vm_object_t uobj; 578 size_t len; 579 int resid; 580 581 int error = 0; 582 583 node = VP_TO_TMPFS_NODE(vp); 584 585 if (vp->v_type != VREG) { 586 error = EISDIR; 587 goto out; 588 } 589 590 if (uio->uio_offset < 0) { 591 error = EINVAL; 592 goto out; 593 } 594 595 node->tn_status |= TMPFS_NODE_ACCESSED; 596 597 uobj = node->tn_reg.tn_aobj; 598 while ((resid = uio->uio_resid) > 0) { 599 error = 0; 600 if (node->tn_size <= uio->uio_offset) 601 break; 602 len = MIN(node->tn_size - uio->uio_offset, resid); 603 if (len == 0) 604 break; 605 error = tmpfs_mappedread(vp->v_object, uobj, len, uio); 606 if ((error != 0) || (resid == uio->uio_resid)) 607 break; 608 } 609 610out: 611 612 return error; 613} 614 615/* --------------------------------------------------------------------- */ 616 617static int 618tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio) 619{ 620 vm_pindex_t idx; 621 vm_page_t vpg, tpg; 622 vm_offset_t offset; 623 off_t addr; 624 size_t tlen; 625 int error, rv; 626 627 error = 0; 628 629 addr = uio->uio_offset; 630 idx = OFF_TO_IDX(addr); 631 offset = addr & PAGE_MASK; 632 tlen = MIN(PAGE_SIZE - offset, len); 633 634 if ((vobj == NULL) || 635 (vobj->resident_page_count == 0 && vobj->cache == NULL)) { 636 vpg = NULL; 637 goto nocache; 638 } 639 640 VM_OBJECT_LOCK(vobj); 641lookupvpg: 642 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) && 643 vm_page_is_valid(vpg, offset, tlen)) { 644 if ((vpg->oflags & VPO_BUSY) != 0) { 645 /* 646 * Reference the page before unlocking and sleeping so 647 * that the page daemon is less likely to reclaim it. 648 */ 649 vm_page_reference(vpg); 650 vm_page_sleep(vpg, "tmfsmw"); 651 goto lookupvpg; 652 } 653 vm_page_busy(vpg); 654 vm_page_undirty(vpg); 655 VM_OBJECT_UNLOCK(vobj); 656 error = uiomove_fromphys(&vpg, offset, tlen, uio); 657 } else { 658 if (__predict_false(vobj->cache != NULL)) 659 vm_page_cache_free(vobj, idx, idx + 1); 660 VM_OBJECT_UNLOCK(vobj); 661 vpg = NULL; 662 } 663nocache: 664 VM_OBJECT_LOCK(tobj); 665 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 666 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 667 if (tpg->valid != VM_PAGE_BITS_ALL) { 668 if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 669 rv = vm_pager_get_pages(tobj, &tpg, 1, 0); 670 if (rv != VM_PAGER_OK) { 671 vm_page_lock(tpg); 672 vm_page_free(tpg); 673 vm_page_unlock(tpg); 674 error = EIO; 675 goto out; 676 } 677 } else 678 vm_page_zero_invalid(tpg, TRUE); 679 } 680 VM_OBJECT_UNLOCK(tobj); 681 if (vpg == NULL) 682 error = uiomove_fromphys(&tpg, offset, tlen, uio); 683 else { 684 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid")); 685 pmap_copy_page(vpg, tpg); 686 } 687 VM_OBJECT_LOCK(tobj); 688 if (error == 0) { 689 KASSERT(tpg->valid == VM_PAGE_BITS_ALL, 690 ("parts of tpg invalid")); 691 vm_page_dirty(tpg); 692 } 693 vm_page_lock(tpg); 694 vm_page_unwire(tpg, TRUE); 695 vm_page_unlock(tpg); 696 vm_page_wakeup(tpg); 697out: 698 VM_OBJECT_UNLOCK(tobj); 699 if (vpg != NULL) { 700 VM_OBJECT_LOCK(vobj); 701 vm_page_wakeup(vpg); 702 VM_OBJECT_UNLOCK(vobj); 703 } 704 705 return (error); 706} 707 708static int 709tmpfs_write(struct vop_write_args *v) 710{ 711 struct vnode *vp = v->a_vp; 712 struct uio *uio = v->a_uio; 713 int ioflag = v->a_ioflag; 714 715 boolean_t extended; 716 int error = 0; 717 off_t oldsize; 718 struct tmpfs_node *node; 719 vm_object_t uobj; 720 size_t len; 721 int resid; 722 723 node = VP_TO_TMPFS_NODE(vp); 724 oldsize = node->tn_size; 725 726 if (uio->uio_offset < 0 || vp->v_type != VREG) { 727 error = EINVAL; 728 goto out; 729 } 730 731 if (uio->uio_resid == 0) { 732 error = 0; 733 goto out; 734 } 735 736 if (ioflag & IO_APPEND) 737 uio->uio_offset = node->tn_size; 738 739 if (uio->uio_offset + uio->uio_resid > 740 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 741 return (EFBIG); 742 743 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 744 return (EFBIG); 745 746 extended = uio->uio_offset + uio->uio_resid > node->tn_size; 747 if (extended) { 748 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid, 749 FALSE); 750 if (error != 0) 751 goto out; 752 } 753 754 uobj = node->tn_reg.tn_aobj; 755 while ((resid = uio->uio_resid) > 0) { 756 if (node->tn_size <= uio->uio_offset) 757 break; 758 len = MIN(node->tn_size - uio->uio_offset, resid); 759 if (len == 0) 760 break; 761 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio); 762 if ((error != 0) || (resid == uio->uio_resid)) 763 break; 764 } 765 766 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 767 (extended ? TMPFS_NODE_CHANGED : 0); 768 769 if (node->tn_mode & (S_ISUID | S_ISGID)) { 770 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0)) 771 node->tn_mode &= ~(S_ISUID | S_ISGID); 772 } 773 774 if (error != 0) 775 (void)tmpfs_reg_resize(vp, oldsize, TRUE); 776 777out: 778 MPASS(IMPLIES(error == 0, uio->uio_resid == 0)); 779 MPASS(IMPLIES(error != 0, oldsize == node->tn_size)); 780 781 return error; 782} 783 784/* --------------------------------------------------------------------- */ 785 786static int 787tmpfs_fsync(struct vop_fsync_args *v) 788{ 789 struct vnode *vp = v->a_vp; 790 791 MPASS(VOP_ISLOCKED(vp)); 792 793 tmpfs_update(vp); 794 795 return 0; 796} 797 798/* --------------------------------------------------------------------- */ 799 800static int 801tmpfs_remove(struct vop_remove_args *v) 802{ 803 struct vnode *dvp = v->a_dvp; 804 struct vnode *vp = v->a_vp; 805 806 int error; 807 struct tmpfs_dirent *de; 808 struct tmpfs_mount *tmp; 809 struct tmpfs_node *dnode; 810 struct tmpfs_node *node; 811 812 MPASS(VOP_ISLOCKED(dvp)); 813 MPASS(VOP_ISLOCKED(vp)); 814 815 if (vp->v_type == VDIR) { 816 error = EISDIR; 817 goto out; 818 } 819 820 dnode = VP_TO_TMPFS_DIR(dvp); 821 node = VP_TO_TMPFS_NODE(vp); 822 tmp = VFS_TO_TMPFS(vp->v_mount); 823 de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 824 MPASS(de != NULL); 825 826 /* Files marked as immutable or append-only cannot be deleted. */ 827 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 828 (dnode->tn_flags & APPEND)) { 829 error = EPERM; 830 goto out; 831 } 832 833 /* Remove the entry from the directory; as it is a file, we do not 834 * have to change the number of hard links of the directory. */ 835 tmpfs_dir_detach(dvp, de); 836 if (v->a_cnp->cn_flags & DOWHITEOUT) 837 tmpfs_dir_whiteout_add(dvp, v->a_cnp); 838 839 /* Free the directory entry we just deleted. Note that the node 840 * referred by it will not be removed until the vnode is really 841 * reclaimed. */ 842 tmpfs_free_dirent(tmp, de, TRUE); 843 844 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED; 845 error = 0; 846 847out: 848 849 return error; 850} 851 852/* --------------------------------------------------------------------- */ 853 854static int 855tmpfs_link(struct vop_link_args *v) 856{ 857 struct vnode *dvp = v->a_tdvp; 858 struct vnode *vp = v->a_vp; 859 struct componentname *cnp = v->a_cnp; 860 861 int error; 862 struct tmpfs_dirent *de; 863 struct tmpfs_node *node; 864 865 MPASS(VOP_ISLOCKED(dvp)); 866 MPASS(cnp->cn_flags & HASBUF); 867 MPASS(dvp != vp); /* XXX When can this be false? */ 868 869 node = VP_TO_TMPFS_NODE(vp); 870 871 /* XXX: Why aren't the following two tests done by the caller? */ 872 873 /* Hard links of directories are forbidden. */ 874 if (vp->v_type == VDIR) { 875 error = EPERM; 876 goto out; 877 } 878 879 /* Cannot create cross-device links. */ 880 if (dvp->v_mount != vp->v_mount) { 881 error = EXDEV; 882 goto out; 883 } 884 885 /* Ensure that we do not overflow the maximum number of links imposed 886 * by the system. */ 887 MPASS(node->tn_links <= LINK_MAX); 888 if (node->tn_links == LINK_MAX) { 889 error = EMLINK; 890 goto out; 891 } 892 893 /* We cannot create links of files marked immutable or append-only. */ 894 if (node->tn_flags & (IMMUTABLE | APPEND)) { 895 error = EPERM; 896 goto out; 897 } 898 899 /* Allocate a new directory entry to represent the node. */ 900 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 901 cnp->cn_nameptr, cnp->cn_namelen, &de); 902 if (error != 0) 903 goto out; 904 905 /* Insert the new directory entry into the appropriate directory. */ 906 if (cnp->cn_flags & ISWHITEOUT) 907 tmpfs_dir_whiteout_remove(dvp, cnp); 908 tmpfs_dir_attach(dvp, de); 909 910 /* vp link count has changed, so update node times. */ 911 node->tn_status |= TMPFS_NODE_CHANGED; 912 tmpfs_update(vp); 913 914 error = 0; 915 916out: 917 return error; 918} 919 920/* --------------------------------------------------------------------- */ 921 922static int 923tmpfs_rename(struct vop_rename_args *v) 924{ 925 struct vnode *fdvp = v->a_fdvp; 926 struct vnode *fvp = v->a_fvp; 927 struct componentname *fcnp = v->a_fcnp; 928 struct vnode *tdvp = v->a_tdvp; 929 struct vnode *tvp = v->a_tvp; 930 struct componentname *tcnp = v->a_tcnp; 931 932 char *newname; 933 int error; 934 struct tmpfs_dirent *de; 935 struct tmpfs_mount *tmp; 936 struct tmpfs_node *fdnode; 937 struct tmpfs_node *fnode; 938 struct tmpfs_node *tnode; 939 struct tmpfs_node *tdnode; 940 941 MPASS(VOP_ISLOCKED(tdvp)); 942 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp))); 943 MPASS(fcnp->cn_flags & HASBUF); 944 MPASS(tcnp->cn_flags & HASBUF); 945 946 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp); 947 948 /* Disallow cross-device renames. 949 * XXX Why isn't this done by the caller? */ 950 if (fvp->v_mount != tdvp->v_mount || 951 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 952 error = EXDEV; 953 goto out; 954 } 955 956 tmp = VFS_TO_TMPFS(tdvp->v_mount); 957 tdnode = VP_TO_TMPFS_DIR(tdvp); 958 959 /* If source and target are the same file, there is nothing to do. */ 960 if (fvp == tvp) { 961 error = 0; 962 goto out; 963 } 964 965 /* If we need to move the directory between entries, lock the 966 * source so that we can safely operate on it. */ 967 if (fdvp != tdvp && fdvp != tvp) 968 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 969 fdnode = VP_TO_TMPFS_DIR(fdvp); 970 fnode = VP_TO_TMPFS_NODE(fvp); 971 de = tmpfs_dir_lookup(fdnode, fnode, fcnp); 972 973 /* Entry can disappear before we lock fdvp, 974 * also avoid manipulating '.' and '..' entries. */ 975 if (de == NULL) { 976 if ((fcnp->cn_flags & ISDOTDOT) != 0 || 977 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 978 error = EINVAL; 979 else 980 error = ENOENT; 981 goto out_locked; 982 } 983 MPASS(de->td_node == fnode); 984 985 /* If re-naming a directory to another preexisting directory 986 * ensure that the target directory is empty so that its 987 * removal causes no side effects. 988 * Kern_rename gurantees the destination to be a directory 989 * if the source is one. */ 990 if (tvp != NULL) { 991 MPASS(tnode != NULL); 992 993 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 994 (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 995 error = EPERM; 996 goto out_locked; 997 } 998 999 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 1000 if (tnode->tn_size > 0) { 1001 error = ENOTEMPTY; 1002 goto out_locked; 1003 } 1004 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 1005 error = ENOTDIR; 1006 goto out_locked; 1007 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 1008 error = EISDIR; 1009 goto out_locked; 1010 } else { 1011 MPASS(fnode->tn_type != VDIR && 1012 tnode->tn_type != VDIR); 1013 } 1014 } 1015 1016 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) 1017 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 1018 error = EPERM; 1019 goto out_locked; 1020 } 1021 1022 /* Ensure that we have enough memory to hold the new name, if it 1023 * has to be changed. */ 1024 if (fcnp->cn_namelen != tcnp->cn_namelen || 1025 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { 1026 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK); 1027 } else 1028 newname = NULL; 1029 1030 /* If the node is being moved to another directory, we have to do 1031 * the move. */ 1032 if (fdnode != tdnode) { 1033 /* In case we are moving a directory, we have to adjust its 1034 * parent to point to the new parent. */ 1035 if (de->td_node->tn_type == VDIR) { 1036 struct tmpfs_node *n; 1037 1038 /* Ensure the target directory is not a child of the 1039 * directory being moved. Otherwise, we'd end up 1040 * with stale nodes. */ 1041 n = tdnode; 1042 /* TMPFS_LOCK garanties that no nodes are freed while 1043 * traversing the list. Nodes can only be marked as 1044 * removed: tn_parent == NULL. */ 1045 TMPFS_LOCK(tmp); 1046 TMPFS_NODE_LOCK(n); 1047 while (n != n->tn_dir.tn_parent) { 1048 struct tmpfs_node *parent; 1049 1050 if (n == fnode) { 1051 TMPFS_NODE_UNLOCK(n); 1052 TMPFS_UNLOCK(tmp); 1053 error = EINVAL; 1054 if (newname != NULL) 1055 free(newname, M_TMPFSNAME); 1056 goto out_locked; 1057 } 1058 parent = n->tn_dir.tn_parent; 1059 TMPFS_NODE_UNLOCK(n); 1060 if (parent == NULL) { 1061 n = NULL; 1062 break; 1063 } 1064 TMPFS_NODE_LOCK(parent); 1065 if (parent->tn_dir.tn_parent == NULL) { 1066 TMPFS_NODE_UNLOCK(parent); 1067 n = NULL; 1068 break; 1069 } 1070 n = parent; 1071 } 1072 TMPFS_UNLOCK(tmp); 1073 if (n == NULL) { 1074 error = EINVAL; 1075 if (newname != NULL) 1076 free(newname, M_TMPFSNAME); 1077 goto out_locked; 1078 } 1079 TMPFS_NODE_UNLOCK(n); 1080 1081 /* Adjust the parent pointer. */ 1082 TMPFS_VALIDATE_DIR(fnode); 1083 TMPFS_NODE_LOCK(de->td_node); 1084 de->td_node->tn_dir.tn_parent = tdnode; 1085 TMPFS_NODE_UNLOCK(de->td_node); 1086 1087 /* As a result of changing the target of the '..' 1088 * entry, the link count of the source and target 1089 * directories has to be adjusted. */ 1090 TMPFS_NODE_LOCK(tdnode); 1091 TMPFS_ASSERT_LOCKED(tdnode); 1092 tdnode->tn_links++; 1093 TMPFS_NODE_UNLOCK(tdnode); 1094 1095 TMPFS_NODE_LOCK(fdnode); 1096 TMPFS_ASSERT_LOCKED(fdnode); 1097 fdnode->tn_links--; 1098 TMPFS_NODE_UNLOCK(fdnode); 1099 } 1100 1101 /* Do the move: just remove the entry from the source directory 1102 * and insert it into the target one. */ 1103 tmpfs_dir_detach(fdvp, de); 1104 if (fcnp->cn_flags & DOWHITEOUT) 1105 tmpfs_dir_whiteout_add(fdvp, fcnp); 1106 if (tcnp->cn_flags & ISWHITEOUT) 1107 tmpfs_dir_whiteout_remove(tdvp, tcnp); 1108 tmpfs_dir_attach(tdvp, de); 1109 } 1110 1111 /* If the name has changed, we need to make it effective by changing 1112 * it in the directory entry. */ 1113 if (newname != NULL) { 1114 MPASS(tcnp->cn_namelen <= MAXNAMLEN); 1115 1116 free(de->td_name, M_TMPFSNAME); 1117 de->td_namelen = (uint16_t)tcnp->cn_namelen; 1118 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen); 1119 de->td_name = newname; 1120 1121 fnode->tn_status |= TMPFS_NODE_CHANGED; 1122 tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1123 } 1124 1125 /* If we are overwriting an entry, we have to remove the old one 1126 * from the target directory. */ 1127 if (tvp != NULL) { 1128 /* Remove the old entry from the target directory. */ 1129 de = tmpfs_dir_lookup(tdnode, tnode, tcnp); 1130 tmpfs_dir_detach(tdvp, de); 1131 1132 /* Free the directory entry we just deleted. Note that the 1133 * node referred by it will not be removed until the vnode is 1134 * really reclaimed. */ 1135 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE); 1136 } 1137 cache_purge(fvp); 1138 1139 error = 0; 1140 1141out_locked: 1142 if (fdvp != tdvp && fdvp != tvp) 1143 VOP_UNLOCK(fdvp, 0); 1144 1145out: 1146 /* Release target nodes. */ 1147 /* XXX: I don't understand when tdvp can be the same as tvp, but 1148 * other code takes care of this... */ 1149 if (tdvp == tvp) 1150 vrele(tdvp); 1151 else 1152 vput(tdvp); 1153 if (tvp != NULL) 1154 vput(tvp); 1155 1156 /* Release source nodes. */ 1157 vrele(fdvp); 1158 vrele(fvp); 1159 1160 return error; 1161} 1162 1163/* --------------------------------------------------------------------- */ 1164 1165static int 1166tmpfs_mkdir(struct vop_mkdir_args *v) 1167{ 1168 struct vnode *dvp = v->a_dvp; 1169 struct vnode **vpp = v->a_vpp; 1170 struct componentname *cnp = v->a_cnp; 1171 struct vattr *vap = v->a_vap; 1172 1173 MPASS(vap->va_type == VDIR); 1174 1175 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 1176} 1177 1178/* --------------------------------------------------------------------- */ 1179 1180static int 1181tmpfs_rmdir(struct vop_rmdir_args *v) 1182{ 1183 struct vnode *dvp = v->a_dvp; 1184 struct vnode *vp = v->a_vp; 1185 1186 int error; 1187 struct tmpfs_dirent *de; 1188 struct tmpfs_mount *tmp; 1189 struct tmpfs_node *dnode; 1190 struct tmpfs_node *node; 1191 1192 MPASS(VOP_ISLOCKED(dvp)); 1193 MPASS(VOP_ISLOCKED(vp)); 1194 1195 tmp = VFS_TO_TMPFS(dvp->v_mount); 1196 dnode = VP_TO_TMPFS_DIR(dvp); 1197 node = VP_TO_TMPFS_DIR(vp); 1198 1199 /* Directories with more than two entries ('.' and '..') cannot be 1200 * removed. */ 1201 if (node->tn_size > 0) { 1202 error = ENOTEMPTY; 1203 goto out; 1204 } 1205 1206 if ((dnode->tn_flags & APPEND) 1207 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1208 error = EPERM; 1209 goto out; 1210 } 1211 1212 /* This invariant holds only if we are not trying to remove "..". 1213 * We checked for that above so this is safe now. */ 1214 MPASS(node->tn_dir.tn_parent == dnode); 1215 1216 /* Get the directory entry associated with node (vp). This was 1217 * filled by tmpfs_lookup while looking up the entry. */ 1218 de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 1219 MPASS(TMPFS_DIRENT_MATCHES(de, 1220 v->a_cnp->cn_nameptr, 1221 v->a_cnp->cn_namelen)); 1222 1223 /* Check flags to see if we are allowed to remove the directory. */ 1224 if (dnode->tn_flags & APPEND 1225 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1226 error = EPERM; 1227 goto out; 1228 } 1229 1230 1231 /* Detach the directory entry from the directory (dnode). */ 1232 tmpfs_dir_detach(dvp, de); 1233 if (v->a_cnp->cn_flags & DOWHITEOUT) 1234 tmpfs_dir_whiteout_add(dvp, v->a_cnp); 1235 1236 /* No vnode should be allocated for this entry from this point */ 1237 TMPFS_NODE_LOCK(node); 1238 TMPFS_ASSERT_ELOCKED(node); 1239 node->tn_links--; 1240 node->tn_dir.tn_parent = NULL; 1241 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1242 TMPFS_NODE_MODIFIED; 1243 1244 TMPFS_NODE_UNLOCK(node); 1245 1246 TMPFS_NODE_LOCK(dnode); 1247 TMPFS_ASSERT_ELOCKED(dnode); 1248 dnode->tn_links--; 1249 dnode->tn_status |= TMPFS_NODE_ACCESSED | \ 1250 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1251 TMPFS_NODE_UNLOCK(dnode); 1252 1253 cache_purge(dvp); 1254 cache_purge(vp); 1255 1256 /* Free the directory entry we just deleted. Note that the node 1257 * referred by it will not be removed until the vnode is really 1258 * reclaimed. */ 1259 tmpfs_free_dirent(tmp, de, TRUE); 1260 1261 /* Release the deleted vnode (will destroy the node, notify 1262 * interested parties and clean it from the cache). */ 1263 1264 dnode->tn_status |= TMPFS_NODE_CHANGED; 1265 tmpfs_update(dvp); 1266 1267 error = 0; 1268 1269out: 1270 return error; 1271} 1272 1273/* --------------------------------------------------------------------- */ 1274 1275static int 1276tmpfs_symlink(struct vop_symlink_args *v) 1277{ 1278 struct vnode *dvp = v->a_dvp; 1279 struct vnode **vpp = v->a_vpp; 1280 struct componentname *cnp = v->a_cnp; 1281 struct vattr *vap = v->a_vap; 1282 char *target = v->a_target; 1283 1284#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */ 1285 MPASS(vap->va_type == VLNK); 1286#else 1287 vap->va_type = VLNK; 1288#endif 1289 1290 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1291} 1292 1293/* --------------------------------------------------------------------- */ 1294 1295static int 1296tmpfs_readdir(struct vop_readdir_args *v) 1297{ 1298 struct vnode *vp = v->a_vp; 1299 struct uio *uio = v->a_uio; 1300 int *eofflag = v->a_eofflag; 1301 u_long **cookies = v->a_cookies; 1302 int *ncookies = v->a_ncookies; 1303 1304 int error; 1305 off_t startoff; 1306 off_t cnt = 0; 1307 struct tmpfs_node *node; 1308 1309 /* This operation only makes sense on directory nodes. */ 1310 if (vp->v_type != VDIR) 1311 return ENOTDIR; 1312 1313 node = VP_TO_TMPFS_DIR(vp); 1314 1315 startoff = uio->uio_offset; 1316 1317 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 1318 error = tmpfs_dir_getdotdent(node, uio); 1319 if (error != 0) 1320 goto outok; 1321 cnt++; 1322 } 1323 1324 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 1325 error = tmpfs_dir_getdotdotdent(node, uio); 1326 if (error != 0) 1327 goto outok; 1328 cnt++; 1329 } 1330 1331 error = tmpfs_dir_getdents(node, uio, &cnt); 1332 1333outok: 1334 MPASS(error >= -1); 1335 1336 if (error == -1) 1337 error = (cnt != 0) ? 0 : EINVAL; 1338 1339 if (eofflag != NULL) 1340 *eofflag = 1341 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1342 1343 /* Update NFS-related variables. */ 1344 if (error == 0 && cookies != NULL && ncookies != NULL) { 1345 off_t i; 1346 off_t off = startoff; 1347 struct tmpfs_dirent *de = NULL; 1348 1349 *ncookies = cnt; 1350 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 1351 1352 for (i = 0; i < cnt; i++) { 1353 MPASS(off != TMPFS_DIRCOOKIE_EOF); 1354 if (off == TMPFS_DIRCOOKIE_DOT) { 1355 off = TMPFS_DIRCOOKIE_DOTDOT; 1356 } else { 1357 if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1358 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 1359 } else if (de != NULL) { 1360 de = TAILQ_NEXT(de, td_entries); 1361 } else { 1362 de = tmpfs_dir_lookupbycookie(node, 1363 off); 1364 MPASS(de != NULL); 1365 de = TAILQ_NEXT(de, td_entries); 1366 } 1367 if (de == NULL) 1368 off = TMPFS_DIRCOOKIE_EOF; 1369 else 1370 off = tmpfs_dircookie(de); 1371 } 1372 1373 (*cookies)[i] = off; 1374 } 1375 MPASS(uio->uio_offset == off); 1376 } 1377 1378 return error; 1379} 1380 1381/* --------------------------------------------------------------------- */ 1382 1383static int 1384tmpfs_readlink(struct vop_readlink_args *v) 1385{ 1386 struct vnode *vp = v->a_vp; 1387 struct uio *uio = v->a_uio; 1388 1389 int error; 1390 struct tmpfs_node *node; 1391 1392 MPASS(uio->uio_offset == 0); 1393 MPASS(vp->v_type == VLNK); 1394 1395 node = VP_TO_TMPFS_NODE(vp); 1396 1397 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid), 1398 uio); 1399 node->tn_status |= TMPFS_NODE_ACCESSED; 1400 1401 return error; 1402} 1403 1404/* --------------------------------------------------------------------- */ 1405 1406static int 1407tmpfs_inactive(struct vop_inactive_args *v) 1408{ 1409 struct vnode *vp = v->a_vp; 1410 struct thread *l = v->a_td; 1411 1412 struct tmpfs_node *node; 1413 1414 MPASS(VOP_ISLOCKED(vp)); 1415 1416 node = VP_TO_TMPFS_NODE(vp); 1417 1418 if (node->tn_links == 0) 1419 vrecycle(vp, l); 1420 1421 return 0; 1422} 1423 1424/* --------------------------------------------------------------------- */ 1425 1426int 1427tmpfs_reclaim(struct vop_reclaim_args *v) 1428{ 1429 struct vnode *vp = v->a_vp; 1430 1431 struct tmpfs_mount *tmp; 1432 struct tmpfs_node *node; 1433 1434 node = VP_TO_TMPFS_NODE(vp); 1435 tmp = VFS_TO_TMPFS(vp->v_mount); 1436 1437 vnode_destroy_vobject(vp); 1438 cache_purge(vp); 1439 1440 TMPFS_NODE_LOCK(node); 1441 TMPFS_ASSERT_ELOCKED(node); 1442 tmpfs_free_vp(vp); 1443 1444 /* If the node referenced by this vnode was deleted by the user, 1445 * we must free its associated data structures (now that the vnode 1446 * is being reclaimed). */ 1447 if (node->tn_links == 0 && 1448 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) { 1449 node->tn_vpstate = TMPFS_VNODE_DOOMED; 1450 TMPFS_NODE_UNLOCK(node); 1451 tmpfs_free_node(tmp, node); 1452 } else 1453 TMPFS_NODE_UNLOCK(node); 1454 1455 MPASS(vp->v_data == NULL); 1456 return 0; 1457} 1458 1459/* --------------------------------------------------------------------- */ 1460 1461static int 1462tmpfs_print(struct vop_print_args *v) 1463{ 1464 struct vnode *vp = v->a_vp; 1465 1466 struct tmpfs_node *node; 1467 1468 node = VP_TO_TMPFS_NODE(vp); 1469 1470 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n", 1471 node, node->tn_flags, node->tn_links); 1472 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n", 1473 node->tn_mode, node->tn_uid, node->tn_gid, 1474 (intmax_t)node->tn_size, node->tn_status); 1475 1476 if (vp->v_type == VFIFO) 1477 fifo_printinfo(vp); 1478 1479 printf("\n"); 1480 1481 return 0; 1482} 1483 1484/* --------------------------------------------------------------------- */ 1485 1486static int 1487tmpfs_pathconf(struct vop_pathconf_args *v) 1488{ 1489 int name = v->a_name; 1490 register_t *retval = v->a_retval; 1491 1492 int error; 1493 1494 error = 0; 1495 1496 switch (name) { 1497 case _PC_LINK_MAX: 1498 *retval = LINK_MAX; 1499 break; 1500 1501 case _PC_NAME_MAX: 1502 *retval = NAME_MAX; 1503 break; 1504 1505 case _PC_PATH_MAX: 1506 *retval = PATH_MAX; 1507 break; 1508 1509 case _PC_PIPE_BUF: 1510 *retval = PIPE_BUF; 1511 break; 1512 1513 case _PC_CHOWN_RESTRICTED: 1514 *retval = 1; 1515 break; 1516 1517 case _PC_NO_TRUNC: 1518 *retval = 1; 1519 break; 1520 1521 case _PC_SYNC_IO: 1522 *retval = 1; 1523 break; 1524 1525 case _PC_FILESIZEBITS: 1526 *retval = 0; /* XXX Don't know which value should I return. */ 1527 break; 1528 1529 default: 1530 error = EINVAL; 1531 } 1532 1533 return error; 1534} 1535 1536static int 1537tmpfs_vptofh(struct vop_vptofh_args *ap) 1538{ 1539 struct tmpfs_fid *tfhp; 1540 struct tmpfs_node *node; 1541 1542 tfhp = (struct tmpfs_fid *)ap->a_fhp; 1543 node = VP_TO_TMPFS_NODE(ap->a_vp); 1544 1545 tfhp->tf_len = sizeof(struct tmpfs_fid); 1546 tfhp->tf_id = node->tn_id; 1547 tfhp->tf_gen = node->tn_gen; 1548 1549 return (0); 1550} 1551 1552static int 1553tmpfs_whiteout(struct vop_whiteout_args *ap) 1554{ 1555 struct vnode *dvp = ap->a_dvp; 1556 struct componentname *cnp = ap->a_cnp; 1557 struct tmpfs_dirent *de; 1558 1559 switch (ap->a_flags) { 1560 case LOOKUP: 1561 return (0); 1562 case CREATE: 1563 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1564 if (de != NULL) 1565 return (de->td_node == NULL ? 0 : EEXIST); 1566 return (tmpfs_dir_whiteout_add(dvp, cnp)); 1567 case DELETE: 1568 tmpfs_dir_whiteout_remove(dvp, cnp); 1569 return (0); 1570 default: 1571 panic("tmpfs_whiteout: unknown op"); 1572 } 1573} 1574 1575/* --------------------------------------------------------------------- */ 1576 1577/* 1578 * vnode operations vector used for files stored in a tmpfs file system. 1579 */ 1580struct vop_vector tmpfs_vnodeop_entries = { 1581 .vop_default = &default_vnodeops, 1582 .vop_lookup = vfs_cache_lookup, 1583 .vop_cachedlookup = tmpfs_lookup, 1584 .vop_create = tmpfs_create, 1585 .vop_mknod = tmpfs_mknod, 1586 .vop_open = tmpfs_open, 1587 .vop_close = tmpfs_close, 1588 .vop_access = tmpfs_access, 1589 .vop_getattr = tmpfs_getattr, 1590 .vop_setattr = tmpfs_setattr, 1591 .vop_read = tmpfs_read, 1592 .vop_write = tmpfs_write, 1593 .vop_fsync = tmpfs_fsync, 1594 .vop_remove = tmpfs_remove, 1595 .vop_link = tmpfs_link, 1596 .vop_rename = tmpfs_rename, 1597 .vop_mkdir = tmpfs_mkdir, 1598 .vop_rmdir = tmpfs_rmdir, 1599 .vop_symlink = tmpfs_symlink, 1600 .vop_readdir = tmpfs_readdir, 1601 .vop_readlink = tmpfs_readlink, 1602 .vop_inactive = tmpfs_inactive, 1603 .vop_reclaim = tmpfs_reclaim, 1604 .vop_print = tmpfs_print, 1605 .vop_pathconf = tmpfs_pathconf, 1606 .vop_vptofh = tmpfs_vptofh, 1607 .vop_whiteout = tmpfs_whiteout, 1608 .vop_bmap = VOP_EOPNOTSUPP, 1609}; 1610 1611