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