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