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