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/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 64 SYSCTL_DECL(_vfs_tmpfs); 65 66 static volatile int tmpfs_rename_restarts; 67 SYSCTL_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 73 static int 74 tmpfs_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 202 out: 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 212 static int 213 tmpfs_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 226 static int 227 tmpfs_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 243 static int 244 tmpfs_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 277 static int 278 tmpfs_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 290 int 291 tmpfs_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 338 out: 339 MPASS(VOP_ISLOCKED(vp)); 340 341 return error; 342 } 343 344 /* --------------------------------------------------------------------- */ 345 346 int 347 tmpfs_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... */ 385 int 386 tmpfs_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 441 static int 442 tmpfs_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 exclusive 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 if (m->valid != VM_PAGE_BITS_ALL) { 462 if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 463 rv = vm_pager_get_pages(tobj, &m, 1, 0); 464 m = vm_page_lookup(tobj, idx); 465 if (m == NULL) { 466 printf( 467 "tmpfs: vm_obj %p idx %jd null lookup rv %d\n", 468 tobj, idx, rv); 469 VM_OBJECT_WUNLOCK(tobj); 470 return (EIO); 471 } 472 if (rv != VM_PAGER_OK) { 473 printf( 474 "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n", 475 tobj, idx, m->valid, rv); 476 vm_page_lock(m); 477 vm_page_free(m); 478 vm_page_unlock(m); 479 VM_OBJECT_WUNLOCK(tobj); 480 return (EIO); 481 } 482 } else 483 vm_page_zero_invalid(m, TRUE); 484 } 485 vm_page_xunbusy(m); 486 vm_page_lock(m); 487 vm_page_hold(m); 488 vm_page_unlock(m); 489 VM_OBJECT_WUNLOCK(tobj); 490 error = uiomove_fromphys(&m, offset, tlen, uio); 491 vm_page_lock(m); 492 vm_page_unhold(m); 493 if (m->queue == PQ_NONE) { 494 vm_page_deactivate(m); 495 } else { 496 /* Requeue to maintain LRU ordering. */ 497 vm_page_requeue(m); 498 } 499 vm_page_unlock(m); 500 501 return (error); 502 } 503 504 static int 505 tmpfs_read(struct vop_read_args *v) 506 { 507 struct vnode *vp = v->a_vp; 508 struct uio *uio = v->a_uio; 509 struct tmpfs_node *node; 510 vm_object_t uobj; 511 size_t len; 512 int resid; 513 int error = 0; 514 vm_pindex_t idx; 515 vm_offset_t offset; 516 off_t addr; 517 size_t tlen; 518 519 node = VP_TO_TMPFS_NODE(vp); 520 521 if (vp->v_type != VREG) { 522 error = EISDIR; 523 goto out; 524 } 525 526 if (uio->uio_offset < 0) { 527 error = EINVAL; 528 goto out; 529 } 530 531 node->tn_status |= TMPFS_NODE_ACCESSED; 532 533 uobj = node->tn_reg.tn_aobj; 534 while ((resid = uio->uio_resid) > 0) { 535 error = 0; 536 if (node->tn_size <= uio->uio_offset) 537 break; 538 len = MIN(node->tn_size - uio->uio_offset, resid); 539 if (len == 0) 540 break; 541 addr = uio->uio_offset; 542 idx = OFF_TO_IDX(addr); 543 offset = addr & PAGE_MASK; 544 tlen = MIN(PAGE_SIZE - offset, len); 545 error = tmpfs_nocacheread(uobj, idx, offset, tlen, uio); 546 if ((error != 0) || (resid == uio->uio_resid)) 547 break; 548 } 549 550 out: 551 552 return error; 553 } 554 555 /* --------------------------------------------------------------------- */ 556 557 static int 558 tmpfs_mappedwrite(vm_object_t tobj, size_t len, struct uio *uio) 559 { 560 vm_pindex_t idx; 561 vm_page_t tpg; 562 vm_offset_t offset; 563 off_t addr; 564 size_t tlen; 565 int error, rv; 566 567 error = 0; 568 569 addr = uio->uio_offset; 570 idx = OFF_TO_IDX(addr); 571 offset = addr & PAGE_MASK; 572 tlen = MIN(PAGE_SIZE - offset, len); 573 574 VM_OBJECT_WLOCK(tobj); 575 tpg = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 576 if (tpg->valid != VM_PAGE_BITS_ALL) { 577 if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 578 rv = vm_pager_get_pages(tobj, &tpg, 1, 0); 579 tpg = vm_page_lookup(tobj, idx); 580 if (tpg == NULL) { 581 printf( 582 "tmpfs: vm_obj %p idx %jd null lookup rv %d\n", 583 tobj, idx, rv); 584 VM_OBJECT_WUNLOCK(tobj); 585 return (EIO); 586 } 587 if (rv != VM_PAGER_OK) { 588 printf( 589 "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n", 590 tobj, idx, tpg->valid, rv); 591 vm_page_lock(tpg); 592 vm_page_free(tpg); 593 vm_page_unlock(tpg); 594 VM_OBJECT_WUNLOCK(tobj); 595 return (EIO); 596 } 597 } else 598 vm_page_zero_invalid(tpg, TRUE); 599 } 600 vm_page_xunbusy(tpg); 601 vm_page_lock(tpg); 602 vm_page_hold(tpg); 603 vm_page_unlock(tpg); 604 VM_OBJECT_WUNLOCK(tobj); 605 error = uiomove_fromphys(&tpg, offset, tlen, uio); 606 VM_OBJECT_WLOCK(tobj); 607 if (error == 0) 608 vm_page_dirty(tpg); 609 vm_page_lock(tpg); 610 vm_page_unhold(tpg); 611 if (tpg->queue == PQ_NONE) { 612 vm_page_deactivate(tpg); 613 } else { 614 /* Requeue to maintain LRU ordering. */ 615 vm_page_requeue(tpg); 616 } 617 vm_page_unlock(tpg); 618 VM_OBJECT_WUNLOCK(tobj); 619 620 return (error); 621 } 622 623 static int 624 tmpfs_write(struct vop_write_args *v) 625 { 626 struct vnode *vp = v->a_vp; 627 struct uio *uio = v->a_uio; 628 int ioflag = v->a_ioflag; 629 630 boolean_t extended; 631 int error = 0; 632 off_t oldsize; 633 struct tmpfs_node *node; 634 vm_object_t uobj; 635 size_t len; 636 int resid; 637 638 node = VP_TO_TMPFS_NODE(vp); 639 oldsize = node->tn_size; 640 641 if (uio->uio_offset < 0 || vp->v_type != VREG) { 642 error = EINVAL; 643 goto out; 644 } 645 646 if (uio->uio_resid == 0) { 647 error = 0; 648 goto out; 649 } 650 651 if (ioflag & IO_APPEND) 652 uio->uio_offset = node->tn_size; 653 654 if (uio->uio_offset + uio->uio_resid > 655 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 656 return (EFBIG); 657 658 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 659 return (EFBIG); 660 661 extended = uio->uio_offset + uio->uio_resid > node->tn_size; 662 if (extended) { 663 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid, 664 FALSE); 665 if (error != 0) 666 goto out; 667 } 668 669 uobj = node->tn_reg.tn_aobj; 670 while ((resid = uio->uio_resid) > 0) { 671 if (node->tn_size <= uio->uio_offset) 672 break; 673 len = MIN(node->tn_size - uio->uio_offset, resid); 674 if (len == 0) 675 break; 676 error = tmpfs_mappedwrite(uobj, len, uio); 677 if ((error != 0) || (resid == uio->uio_resid)) 678 break; 679 } 680 681 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 682 (extended ? TMPFS_NODE_CHANGED : 0); 683 684 if (node->tn_mode & (S_ISUID | S_ISGID)) { 685 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0)) 686 node->tn_mode &= ~(S_ISUID | S_ISGID); 687 } 688 689 if (error != 0) 690 (void)tmpfs_reg_resize(vp, oldsize, TRUE); 691 692 out: 693 MPASS(IMPLIES(error == 0, uio->uio_resid == 0)); 694 MPASS(IMPLIES(error != 0, oldsize == node->tn_size)); 695 696 return error; 697 } 698 699 /* --------------------------------------------------------------------- */ 700 701 static int 702 tmpfs_fsync(struct vop_fsync_args *v) 703 { 704 struct vnode *vp = v->a_vp; 705 706 MPASS(VOP_ISLOCKED(vp)); 707 708 tmpfs_update(vp); 709 710 return 0; 711 } 712 713 /* --------------------------------------------------------------------- */ 714 715 static int 716 tmpfs_remove(struct vop_remove_args *v) 717 { 718 struct vnode *dvp = v->a_dvp; 719 struct vnode *vp = v->a_vp; 720 721 int error; 722 struct tmpfs_dirent *de; 723 struct tmpfs_mount *tmp; 724 struct tmpfs_node *dnode; 725 struct tmpfs_node *node; 726 727 MPASS(VOP_ISLOCKED(dvp)); 728 MPASS(VOP_ISLOCKED(vp)); 729 730 if (vp->v_type == VDIR) { 731 error = EISDIR; 732 goto out; 733 } 734 735 dnode = VP_TO_TMPFS_DIR(dvp); 736 node = VP_TO_TMPFS_NODE(vp); 737 tmp = VFS_TO_TMPFS(vp->v_mount); 738 de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 739 MPASS(de != NULL); 740 741 /* Files marked as immutable or append-only cannot be deleted. */ 742 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 743 (dnode->tn_flags & APPEND)) { 744 error = EPERM; 745 goto out; 746 } 747 748 /* Remove the entry from the directory; as it is a file, we do not 749 * have to change the number of hard links of the directory. */ 750 tmpfs_dir_detach(dvp, de); 751 if (v->a_cnp->cn_flags & DOWHITEOUT) 752 tmpfs_dir_whiteout_add(dvp, v->a_cnp); 753 754 /* Free the directory entry we just deleted. Note that the node 755 * referred by it will not be removed until the vnode is really 756 * reclaimed. */ 757 tmpfs_free_dirent(tmp, de); 758 759 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED; 760 error = 0; 761 762 out: 763 764 return error; 765 } 766 767 /* --------------------------------------------------------------------- */ 768 769 static int 770 tmpfs_link(struct vop_link_args *v) 771 { 772 struct vnode *dvp = v->a_tdvp; 773 struct vnode *vp = v->a_vp; 774 struct componentname *cnp = v->a_cnp; 775 776 int error; 777 struct tmpfs_dirent *de; 778 struct tmpfs_node *node; 779 780 MPASS(VOP_ISLOCKED(dvp)); 781 MPASS(cnp->cn_flags & HASBUF); 782 MPASS(dvp != vp); /* XXX When can this be false? */ 783 784 node = VP_TO_TMPFS_NODE(vp); 785 786 /* XXX: Why aren't the following two tests done by the caller? */ 787 788 /* Hard links of directories are forbidden. */ 789 if (vp->v_type == VDIR) { 790 error = EPERM; 791 goto out; 792 } 793 794 /* Cannot create cross-device links. */ 795 if (dvp->v_mount != vp->v_mount) { 796 error = EXDEV; 797 goto out; 798 } 799 800 /* Ensure that we do not overflow the maximum number of links imposed 801 * by the system. */ 802 MPASS(node->tn_links <= LINK_MAX); 803 if (node->tn_links == LINK_MAX) { 804 error = EMLINK; 805 goto out; 806 } 807 808 /* We cannot create links of files marked immutable or append-only. */ 809 if (node->tn_flags & (IMMUTABLE | APPEND)) { 810 error = EPERM; 811 goto out; 812 } 813 814 /* Allocate a new directory entry to represent the node. */ 815 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 816 cnp->cn_nameptr, cnp->cn_namelen, &de); 817 if (error != 0) 818 goto out; 819 820 /* Insert the new directory entry into the appropriate directory. */ 821 if (cnp->cn_flags & ISWHITEOUT) 822 tmpfs_dir_whiteout_remove(dvp, cnp); 823 tmpfs_dir_attach(dvp, de); 824 825 /* vp link count has changed, so update node times. */ 826 node->tn_status |= TMPFS_NODE_CHANGED; 827 tmpfs_update(vp); 828 829 error = 0; 830 831 out: 832 return error; 833 } 834 835 /* --------------------------------------------------------------------- */ 836 837 /* 838 * We acquire all but fdvp locks using non-blocking acquisitions. If we 839 * fail to acquire any lock in the path we will drop all held locks, 840 * acquire the new lock in a blocking fashion, and then release it and 841 * restart the rename. This acquire/release step ensures that we do not 842 * spin on a lock waiting for release. On error release all vnode locks 843 * and decrement references the way tmpfs_rename() would do. 844 */ 845 static int 846 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp, 847 struct vnode *tdvp, struct vnode **tvpp, 848 struct componentname *fcnp, struct componentname *tcnp) 849 { 850 struct vnode *nvp; 851 struct mount *mp; 852 struct tmpfs_dirent *de; 853 int error, restarts = 0; 854 855 VOP_UNLOCK(tdvp, 0); 856 if (*tvpp != NULL && *tvpp != tdvp) 857 VOP_UNLOCK(*tvpp, 0); 858 mp = fdvp->v_mount; 859 860 relock: 861 restarts += 1; 862 error = vn_lock(fdvp, LK_EXCLUSIVE); 863 if (error) 864 goto releout; 865 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 866 VOP_UNLOCK(fdvp, 0); 867 error = vn_lock(tdvp, LK_EXCLUSIVE); 868 if (error) 869 goto releout; 870 VOP_UNLOCK(tdvp, 0); 871 goto relock; 872 } 873 /* 874 * Re-resolve fvp to be certain it still exists and fetch the 875 * correct vnode. 876 */ 877 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp); 878 if (de == NULL) { 879 VOP_UNLOCK(fdvp, 0); 880 VOP_UNLOCK(tdvp, 0); 881 if ((fcnp->cn_flags & ISDOTDOT) != 0 || 882 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 883 error = EINVAL; 884 else 885 error = ENOENT; 886 goto releout; 887 } 888 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp); 889 if (error != 0) { 890 VOP_UNLOCK(fdvp, 0); 891 VOP_UNLOCK(tdvp, 0); 892 if (error != EBUSY) 893 goto releout; 894 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp); 895 if (error != 0) 896 goto releout; 897 VOP_UNLOCK(nvp, 0); 898 /* 899 * Concurrent rename race. 900 */ 901 if (nvp == tdvp) { 902 vrele(nvp); 903 error = EINVAL; 904 goto releout; 905 } 906 vrele(*fvpp); 907 *fvpp = nvp; 908 goto relock; 909 } 910 vrele(*fvpp); 911 *fvpp = nvp; 912 VOP_UNLOCK(*fvpp, 0); 913 /* 914 * Re-resolve tvp and acquire the vnode lock if present. 915 */ 916 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp); 917 /* 918 * If tvp disappeared we just carry on. 919 */ 920 if (de == NULL && *tvpp != NULL) { 921 vrele(*tvpp); 922 *tvpp = NULL; 923 } 924 /* 925 * Get the tvp ino if the lookup succeeded. We may have to restart 926 * if the non-blocking acquire fails. 927 */ 928 if (de != NULL) { 929 nvp = NULL; 930 error = tmpfs_alloc_vp(mp, de->td_node, 931 LK_EXCLUSIVE | LK_NOWAIT, &nvp); 932 if (*tvpp != NULL) 933 vrele(*tvpp); 934 *tvpp = nvp; 935 if (error != 0) { 936 VOP_UNLOCK(fdvp, 0); 937 VOP_UNLOCK(tdvp, 0); 938 if (error != EBUSY) 939 goto releout; 940 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, 941 &nvp); 942 if (error != 0) 943 goto releout; 944 VOP_UNLOCK(nvp, 0); 945 /* 946 * fdvp contains fvp, thus tvp (=fdvp) is not empty. 947 */ 948 if (nvp == fdvp) { 949 error = ENOTEMPTY; 950 goto releout; 951 } 952 goto relock; 953 } 954 } 955 tmpfs_rename_restarts += restarts; 956 957 return (0); 958 959 releout: 960 vrele(fdvp); 961 vrele(*fvpp); 962 vrele(tdvp); 963 if (*tvpp != NULL) 964 vrele(*tvpp); 965 tmpfs_rename_restarts += restarts; 966 967 return (error); 968 } 969 970 static int 971 tmpfs_rename(struct vop_rename_args *v) 972 { 973 struct vnode *fdvp = v->a_fdvp; 974 struct vnode *fvp = v->a_fvp; 975 struct componentname *fcnp = v->a_fcnp; 976 struct vnode *tdvp = v->a_tdvp; 977 struct vnode *tvp = v->a_tvp; 978 struct componentname *tcnp = v->a_tcnp; 979 struct mount *mp = NULL; 980 981 char *newname; 982 int error; 983 struct tmpfs_dirent *de; 984 struct tmpfs_mount *tmp; 985 struct tmpfs_node *fdnode; 986 struct tmpfs_node *fnode; 987 struct tmpfs_node *tnode; 988 struct tmpfs_node *tdnode; 989 990 MPASS(VOP_ISLOCKED(tdvp)); 991 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp))); 992 MPASS(fcnp->cn_flags & HASBUF); 993 MPASS(tcnp->cn_flags & HASBUF); 994 995 /* Disallow cross-device renames. 996 * XXX Why isn't this done by the caller? */ 997 if (fvp->v_mount != tdvp->v_mount || 998 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 999 error = EXDEV; 1000 goto out; 1001 } 1002 1003 /* If source and target are the same file, there is nothing to do. */ 1004 if (fvp == tvp) { 1005 error = 0; 1006 goto out; 1007 } 1008 1009 /* If we need to move the directory between entries, lock the 1010 * source so that we can safely operate on it. */ 1011 if (fdvp != tdvp && fdvp != tvp) { 1012 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1013 mp = tdvp->v_mount; 1014 error = vfs_busy(mp, 0); 1015 if (error != 0) { 1016 mp = NULL; 1017 goto out; 1018 } 1019 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp, 1020 fcnp, tcnp); 1021 if (error != 0) { 1022 vfs_unbusy(mp); 1023 return (error); 1024 } 1025 ASSERT_VOP_ELOCKED(fdvp, 1026 "tmpfs_rename: fdvp not locked"); 1027 ASSERT_VOP_ELOCKED(tdvp, 1028 "tmpfs_rename: tdvp not locked"); 1029 if (tvp != NULL) 1030 ASSERT_VOP_ELOCKED(tvp, 1031 "tmpfs_rename: tvp not locked"); 1032 if (fvp == tvp) { 1033 error = 0; 1034 goto out_locked; 1035 } 1036 } 1037 } 1038 1039 tmp = VFS_TO_TMPFS(tdvp->v_mount); 1040 tdnode = VP_TO_TMPFS_DIR(tdvp); 1041 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp); 1042 fdnode = VP_TO_TMPFS_DIR(fdvp); 1043 fnode = VP_TO_TMPFS_NODE(fvp); 1044 de = tmpfs_dir_lookup(fdnode, fnode, fcnp); 1045 1046 /* Entry can disappear before we lock fdvp, 1047 * also avoid manipulating '.' and '..' entries. */ 1048 if (de == NULL) { 1049 if ((fcnp->cn_flags & ISDOTDOT) != 0 || 1050 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 1051 error = EINVAL; 1052 else 1053 error = ENOENT; 1054 goto out_locked; 1055 } 1056 MPASS(de->td_node == fnode); 1057 1058 /* If re-naming a directory to another preexisting directory 1059 * ensure that the target directory is empty so that its 1060 * removal causes no side effects. 1061 * Kern_rename gurantees the destination to be a directory 1062 * if the source is one. */ 1063 if (tvp != NULL) { 1064 MPASS(tnode != NULL); 1065 1066 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 1067 (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 1068 error = EPERM; 1069 goto out_locked; 1070 } 1071 1072 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 1073 if (tnode->tn_size > 0) { 1074 error = ENOTEMPTY; 1075 goto out_locked; 1076 } 1077 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 1078 error = ENOTDIR; 1079 goto out_locked; 1080 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 1081 error = EISDIR; 1082 goto out_locked; 1083 } else { 1084 MPASS(fnode->tn_type != VDIR && 1085 tnode->tn_type != VDIR); 1086 } 1087 } 1088 1089 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) 1090 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 1091 error = EPERM; 1092 goto out_locked; 1093 } 1094 1095 /* Ensure that we have enough memory to hold the new name, if it 1096 * has to be changed. */ 1097 if (fcnp->cn_namelen != tcnp->cn_namelen || 1098 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { 1099 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK); 1100 } else 1101 newname = NULL; 1102 1103 /* If the node is being moved to another directory, we have to do 1104 * the move. */ 1105 if (fdnode != tdnode) { 1106 /* In case we are moving a directory, we have to adjust its 1107 * parent to point to the new parent. */ 1108 if (de->td_node->tn_type == VDIR) { 1109 struct tmpfs_node *n; 1110 1111 /* Ensure the target directory is not a child of the 1112 * directory being moved. Otherwise, we'd end up 1113 * with stale nodes. */ 1114 n = tdnode; 1115 /* TMPFS_LOCK garanties that no nodes are freed while 1116 * traversing the list. Nodes can only be marked as 1117 * removed: tn_parent == NULL. */ 1118 TMPFS_LOCK(tmp); 1119 TMPFS_NODE_LOCK(n); 1120 while (n != n->tn_dir.tn_parent) { 1121 struct tmpfs_node *parent; 1122 1123 if (n == fnode) { 1124 TMPFS_NODE_UNLOCK(n); 1125 TMPFS_UNLOCK(tmp); 1126 error = EINVAL; 1127 if (newname != NULL) 1128 free(newname, M_TMPFSNAME); 1129 goto out_locked; 1130 } 1131 parent = n->tn_dir.tn_parent; 1132 TMPFS_NODE_UNLOCK(n); 1133 if (parent == NULL) { 1134 n = NULL; 1135 break; 1136 } 1137 TMPFS_NODE_LOCK(parent); 1138 if (parent->tn_dir.tn_parent == NULL) { 1139 TMPFS_NODE_UNLOCK(parent); 1140 n = NULL; 1141 break; 1142 } 1143 n = parent; 1144 } 1145 TMPFS_UNLOCK(tmp); 1146 if (n == NULL) { 1147 error = EINVAL; 1148 if (newname != NULL) 1149 free(newname, M_TMPFSNAME); 1150 goto out_locked; 1151 } 1152 TMPFS_NODE_UNLOCK(n); 1153 1154 /* Adjust the parent pointer. */ 1155 TMPFS_VALIDATE_DIR(fnode); 1156 TMPFS_NODE_LOCK(de->td_node); 1157 de->td_node->tn_dir.tn_parent = tdnode; 1158 TMPFS_NODE_UNLOCK(de->td_node); 1159 1160 /* As a result of changing the target of the '..' 1161 * entry, the link count of the source and target 1162 * directories has to be adjusted. */ 1163 TMPFS_NODE_LOCK(tdnode); 1164 TMPFS_ASSERT_LOCKED(tdnode); 1165 tdnode->tn_links++; 1166 TMPFS_NODE_UNLOCK(tdnode); 1167 1168 TMPFS_NODE_LOCK(fdnode); 1169 TMPFS_ASSERT_LOCKED(fdnode); 1170 fdnode->tn_links--; 1171 TMPFS_NODE_UNLOCK(fdnode); 1172 } 1173 } 1174 1175 /* Do the move: just remove the entry from the source directory 1176 * and insert it into the target one. */ 1177 tmpfs_dir_detach(fdvp, de); 1178 1179 if (fcnp->cn_flags & DOWHITEOUT) 1180 tmpfs_dir_whiteout_add(fdvp, fcnp); 1181 if (tcnp->cn_flags & ISWHITEOUT) 1182 tmpfs_dir_whiteout_remove(tdvp, tcnp); 1183 1184 /* If the name has changed, we need to make it effective by changing 1185 * it in the directory entry. */ 1186 if (newname != NULL) { 1187 MPASS(tcnp->cn_namelen <= MAXNAMLEN); 1188 1189 free(de->ud.td_name, M_TMPFSNAME); 1190 de->ud.td_name = newname; 1191 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen); 1192 1193 fnode->tn_status |= TMPFS_NODE_CHANGED; 1194 tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1195 } 1196 1197 /* If we are overwriting an entry, we have to remove the old one 1198 * from the target directory. */ 1199 if (tvp != NULL) { 1200 struct tmpfs_dirent *tde; 1201 1202 /* Remove the old entry from the target directory. */ 1203 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp); 1204 tmpfs_dir_detach(tdvp, tde); 1205 1206 /* Free the directory entry we just deleted. Note that the 1207 * node referred by it will not be removed until the vnode is 1208 * really reclaimed. */ 1209 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde); 1210 } 1211 1212 tmpfs_dir_attach(tdvp, de); 1213 1214 cache_purge(fvp); 1215 if (tvp != NULL) 1216 cache_purge(tvp); 1217 cache_purge_negative(tdvp); 1218 1219 error = 0; 1220 1221 out_locked: 1222 if (fdvp != tdvp && fdvp != tvp) 1223 VOP_UNLOCK(fdvp, 0); 1224 1225 out: 1226 /* Release target nodes. */ 1227 /* XXX: I don't understand when tdvp can be the same as tvp, but 1228 * other code takes care of this... */ 1229 if (tdvp == tvp) 1230 vrele(tdvp); 1231 else 1232 vput(tdvp); 1233 if (tvp != NULL) 1234 vput(tvp); 1235 1236 /* Release source nodes. */ 1237 vrele(fdvp); 1238 vrele(fvp); 1239 1240 if (mp != NULL) 1241 vfs_unbusy(mp); 1242 1243 return error; 1244 } 1245 1246 /* --------------------------------------------------------------------- */ 1247 1248 static int 1249 tmpfs_mkdir(struct vop_mkdir_args *v) 1250 { 1251 struct vnode *dvp = v->a_dvp; 1252 struct vnode **vpp = v->a_vpp; 1253 struct componentname *cnp = v->a_cnp; 1254 struct vattr *vap = v->a_vap; 1255 1256 MPASS(vap->va_type == VDIR); 1257 1258 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 1259 } 1260 1261 /* --------------------------------------------------------------------- */ 1262 1263 static int 1264 tmpfs_rmdir(struct vop_rmdir_args *v) 1265 { 1266 struct vnode *dvp = v->a_dvp; 1267 struct vnode *vp = v->a_vp; 1268 1269 int error; 1270 struct tmpfs_dirent *de; 1271 struct tmpfs_mount *tmp; 1272 struct tmpfs_node *dnode; 1273 struct tmpfs_node *node; 1274 1275 MPASS(VOP_ISLOCKED(dvp)); 1276 MPASS(VOP_ISLOCKED(vp)); 1277 1278 tmp = VFS_TO_TMPFS(dvp->v_mount); 1279 dnode = VP_TO_TMPFS_DIR(dvp); 1280 node = VP_TO_TMPFS_DIR(vp); 1281 1282 /* Directories with more than two entries ('.' and '..') cannot be 1283 * removed. */ 1284 if (node->tn_size > 0) { 1285 error = ENOTEMPTY; 1286 goto out; 1287 } 1288 1289 if ((dnode->tn_flags & APPEND) 1290 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1291 error = EPERM; 1292 goto out; 1293 } 1294 1295 /* This invariant holds only if we are not trying to remove "..". 1296 * We checked for that above so this is safe now. */ 1297 MPASS(node->tn_dir.tn_parent == dnode); 1298 1299 /* Get the directory entry associated with node (vp). This was 1300 * filled by tmpfs_lookup while looking up the entry. */ 1301 de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 1302 MPASS(TMPFS_DIRENT_MATCHES(de, 1303 v->a_cnp->cn_nameptr, 1304 v->a_cnp->cn_namelen)); 1305 1306 /* Check flags to see if we are allowed to remove the directory. */ 1307 if (dnode->tn_flags & APPEND 1308 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1309 error = EPERM; 1310 goto out; 1311 } 1312 1313 1314 /* Detach the directory entry from the directory (dnode). */ 1315 tmpfs_dir_detach(dvp, de); 1316 if (v->a_cnp->cn_flags & DOWHITEOUT) 1317 tmpfs_dir_whiteout_add(dvp, v->a_cnp); 1318 1319 /* No vnode should be allocated for this entry from this point */ 1320 TMPFS_NODE_LOCK(node); 1321 TMPFS_ASSERT_ELOCKED(node); 1322 node->tn_links--; 1323 node->tn_dir.tn_parent = NULL; 1324 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1325 TMPFS_NODE_MODIFIED; 1326 1327 TMPFS_NODE_UNLOCK(node); 1328 1329 TMPFS_NODE_LOCK(dnode); 1330 TMPFS_ASSERT_ELOCKED(dnode); 1331 dnode->tn_links--; 1332 dnode->tn_status |= TMPFS_NODE_ACCESSED | \ 1333 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1334 TMPFS_NODE_UNLOCK(dnode); 1335 1336 cache_purge(dvp); 1337 cache_purge(vp); 1338 1339 /* Free the directory entry we just deleted. Note that the node 1340 * referred by it will not be removed until the vnode is really 1341 * reclaimed. */ 1342 tmpfs_free_dirent(tmp, de); 1343 1344 /* Release the deleted vnode (will destroy the node, notify 1345 * interested parties and clean it from the cache). */ 1346 1347 dnode->tn_status |= TMPFS_NODE_CHANGED; 1348 tmpfs_update(dvp); 1349 1350 error = 0; 1351 1352 out: 1353 return error; 1354 } 1355 1356 /* --------------------------------------------------------------------- */ 1357 1358 static int 1359 tmpfs_symlink(struct vop_symlink_args *v) 1360 { 1361 struct vnode *dvp = v->a_dvp; 1362 struct vnode **vpp = v->a_vpp; 1363 struct componentname *cnp = v->a_cnp; 1364 struct vattr *vap = v->a_vap; 1365 char *target = v->a_target; 1366 1367 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */ 1368 MPASS(vap->va_type == VLNK); 1369 #else 1370 vap->va_type = VLNK; 1371 #endif 1372 1373 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1374 } 1375 1376 /* --------------------------------------------------------------------- */ 1377 1378 static int 1379 tmpfs_readdir(struct vop_readdir_args *v) 1380 { 1381 struct vnode *vp = v->a_vp; 1382 struct uio *uio = v->a_uio; 1383 int *eofflag = v->a_eofflag; 1384 u_long **cookies = v->a_cookies; 1385 int *ncookies = v->a_ncookies; 1386 1387 int error; 1388 ssize_t startresid; 1389 int cnt = 0; 1390 struct tmpfs_node *node; 1391 1392 /* This operation only makes sense on directory nodes. */ 1393 if (vp->v_type != VDIR) 1394 return ENOTDIR; 1395 1396 node = VP_TO_TMPFS_DIR(vp); 1397 1398 startresid = uio->uio_resid; 1399 1400 if (cookies != NULL && ncookies != NULL) { 1401 cnt = howmany(node->tn_size, sizeof(struct tmpfs_dirent)) + 2; 1402 *cookies = malloc(cnt * sizeof(**cookies), M_TEMP, M_WAITOK); 1403 *ncookies = 0; 1404 } 1405 1406 if (cnt == 0) 1407 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL); 1408 else 1409 error = tmpfs_dir_getdents(node, uio, cnt, *cookies, ncookies); 1410 1411 if (error == EJUSTRETURN) 1412 error = (uio->uio_resid != startresid) ? 0 : EINVAL; 1413 1414 if (error != 0 && cnt != 0) 1415 free(*cookies, M_TEMP); 1416 1417 if (eofflag != NULL) 1418 *eofflag = 1419 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1420 1421 return error; 1422 } 1423 1424 /* --------------------------------------------------------------------- */ 1425 1426 static int 1427 tmpfs_readlink(struct vop_readlink_args *v) 1428 { 1429 struct vnode *vp = v->a_vp; 1430 struct uio *uio = v->a_uio; 1431 1432 int error; 1433 struct tmpfs_node *node; 1434 1435 MPASS(uio->uio_offset == 0); 1436 MPASS(vp->v_type == VLNK); 1437 1438 node = VP_TO_TMPFS_NODE(vp); 1439 1440 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid), 1441 uio); 1442 node->tn_status |= TMPFS_NODE_ACCESSED; 1443 1444 return error; 1445 } 1446 1447 /* --------------------------------------------------------------------- */ 1448 1449 static int 1450 tmpfs_inactive(struct vop_inactive_args *v) 1451 { 1452 struct vnode *vp = v->a_vp; 1453 1454 struct tmpfs_node *node; 1455 1456 node = VP_TO_TMPFS_NODE(vp); 1457 1458 if (node->tn_links == 0) 1459 vrecycle(vp); 1460 1461 return 0; 1462 } 1463 1464 /* --------------------------------------------------------------------- */ 1465 1466 int 1467 tmpfs_reclaim(struct vop_reclaim_args *v) 1468 { 1469 struct vnode *vp = v->a_vp; 1470 1471 struct tmpfs_mount *tmp; 1472 struct tmpfs_node *node; 1473 1474 node = VP_TO_TMPFS_NODE(vp); 1475 tmp = VFS_TO_TMPFS(vp->v_mount); 1476 1477 if (vp->v_type == VREG) 1478 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj); 1479 else 1480 vnode_destroy_vobject(vp); 1481 vp->v_object = NULL; 1482 cache_purge(vp); 1483 1484 TMPFS_NODE_LOCK(node); 1485 TMPFS_ASSERT_ELOCKED(node); 1486 tmpfs_free_vp(vp); 1487 1488 /* If the node referenced by this vnode was deleted by the user, 1489 * we must free its associated data structures (now that the vnode 1490 * is being reclaimed). */ 1491 if (node->tn_links == 0 && 1492 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) { 1493 node->tn_vpstate = TMPFS_VNODE_DOOMED; 1494 TMPFS_NODE_UNLOCK(node); 1495 tmpfs_free_node(tmp, node); 1496 } else 1497 TMPFS_NODE_UNLOCK(node); 1498 1499 MPASS(vp->v_data == NULL); 1500 return 0; 1501 } 1502 1503 /* --------------------------------------------------------------------- */ 1504 1505 static int 1506 tmpfs_print(struct vop_print_args *v) 1507 { 1508 struct vnode *vp = v->a_vp; 1509 1510 struct tmpfs_node *node; 1511 1512 node = VP_TO_TMPFS_NODE(vp); 1513 1514 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n", 1515 node, node->tn_flags, node->tn_links); 1516 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n", 1517 node->tn_mode, node->tn_uid, node->tn_gid, 1518 (intmax_t)node->tn_size, node->tn_status); 1519 1520 if (vp->v_type == VFIFO) 1521 fifo_printinfo(vp); 1522 1523 printf("\n"); 1524 1525 return 0; 1526 } 1527 1528 /* --------------------------------------------------------------------- */ 1529 1530 static int 1531 tmpfs_pathconf(struct vop_pathconf_args *v) 1532 { 1533 int name = v->a_name; 1534 register_t *retval = v->a_retval; 1535 1536 int error; 1537 1538 error = 0; 1539 1540 switch (name) { 1541 case _PC_LINK_MAX: 1542 *retval = LINK_MAX; 1543 break; 1544 1545 case _PC_NAME_MAX: 1546 *retval = NAME_MAX; 1547 break; 1548 1549 case _PC_PATH_MAX: 1550 *retval = PATH_MAX; 1551 break; 1552 1553 case _PC_PIPE_BUF: 1554 *retval = PIPE_BUF; 1555 break; 1556 1557 case _PC_CHOWN_RESTRICTED: 1558 *retval = 1; 1559 break; 1560 1561 case _PC_NO_TRUNC: 1562 *retval = 1; 1563 break; 1564 1565 case _PC_SYNC_IO: 1566 *retval = 1; 1567 break; 1568 1569 case _PC_FILESIZEBITS: 1570 *retval = 0; /* XXX Don't know which value should I return. */ 1571 break; 1572 1573 default: 1574 error = EINVAL; 1575 } 1576 1577 return error; 1578 } 1579 1580 static int 1581 tmpfs_vptofh(struct vop_vptofh_args *ap) 1582 { 1583 struct tmpfs_fid *tfhp; 1584 struct tmpfs_node *node; 1585 1586 tfhp = (struct tmpfs_fid *)ap->a_fhp; 1587 node = VP_TO_TMPFS_NODE(ap->a_vp); 1588 1589 tfhp->tf_len = sizeof(struct tmpfs_fid); 1590 tfhp->tf_id = node->tn_id; 1591 tfhp->tf_gen = node->tn_gen; 1592 1593 return (0); 1594 } 1595 1596 static int 1597 tmpfs_whiteout(struct vop_whiteout_args *ap) 1598 { 1599 struct vnode *dvp = ap->a_dvp; 1600 struct componentname *cnp = ap->a_cnp; 1601 struct tmpfs_dirent *de; 1602 1603 switch (ap->a_flags) { 1604 case LOOKUP: 1605 return (0); 1606 case CREATE: 1607 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1608 if (de != NULL) 1609 return (de->td_node == NULL ? 0 : EEXIST); 1610 return (tmpfs_dir_whiteout_add(dvp, cnp)); 1611 case DELETE: 1612 tmpfs_dir_whiteout_remove(dvp, cnp); 1613 return (0); 1614 default: 1615 panic("tmpfs_whiteout: unknown op"); 1616 } 1617 } 1618 1619 /* --------------------------------------------------------------------- */ 1620 1621 /* 1622 * vnode operations vector used for files stored in a tmpfs file system. 1623 */ 1624 struct vop_vector tmpfs_vnodeop_entries = { 1625 .vop_default = &default_vnodeops, 1626 .vop_lookup = vfs_cache_lookup, 1627 .vop_cachedlookup = tmpfs_lookup, 1628 .vop_create = tmpfs_create, 1629 .vop_mknod = tmpfs_mknod, 1630 .vop_open = tmpfs_open, 1631 .vop_close = tmpfs_close, 1632 .vop_access = tmpfs_access, 1633 .vop_getattr = tmpfs_getattr, 1634 .vop_setattr = tmpfs_setattr, 1635 .vop_read = tmpfs_read, 1636 .vop_write = tmpfs_write, 1637 .vop_fsync = tmpfs_fsync, 1638 .vop_remove = tmpfs_remove, 1639 .vop_link = tmpfs_link, 1640 .vop_rename = tmpfs_rename, 1641 .vop_mkdir = tmpfs_mkdir, 1642 .vop_rmdir = tmpfs_rmdir, 1643 .vop_symlink = tmpfs_symlink, 1644 .vop_readdir = tmpfs_readdir, 1645 .vop_readlink = tmpfs_readlink, 1646 .vop_inactive = tmpfs_inactive, 1647 .vop_reclaim = tmpfs_reclaim, 1648 .vop_print = tmpfs_print, 1649 .vop_pathconf = tmpfs_pathconf, 1650 .vop_vptofh = tmpfs_vptofh, 1651 .vop_whiteout = tmpfs_whiteout, 1652 .vop_bmap = VOP_EOPNOTSUPP, 1653 }; 1654 1655