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