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