1 /* $OpenBSD: tmpfs_vnops.c,v 1.4 2013/06/04 09:12:20 espie Exp $ */ 2 /* $NetBSD: tmpfs_vnops.c,v 1.100 2012/11/05 17:27:39 dholland Exp $ */ 3 4 /* 5 * Copyright (c) 2005, 2006, 2007, 2012 The NetBSD Foundation, Inc. 6 * Copyright (c) 2013 Pedro Martelletto 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 11 * 2005 program, and by Taylor R Campbell. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * tmpfs vnode interface. 37 */ 38 39 #if 0 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.100 2012/11/05 17:27:39 dholland Exp $"); 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/dirent.h> 46 #include <sys/fcntl.h> 47 #include <sys/event.h> 48 #include <sys/malloc.h> 49 #include <sys/namei.h> 50 #include <sys/stat.h> 51 #include <sys/uio.h> 52 #include <sys/unistd.h> 53 #include <sys/vnode.h> 54 #include <sys/lockf.h> 55 #include <sys/poll.h> 56 57 #include <uvm/uvm.h> 58 59 #include <miscfs/fifofs/fifo.h> 60 #include <tmpfs/tmpfs_vnops.h> 61 #include <tmpfs/tmpfs.h> 62 63 /* 64 * vnode operations vector used for files stored in a tmpfs file system. 65 */ 66 struct vops tmpfs_vops = { 67 .vop_lookup = tmpfs_lookup, 68 .vop_create = tmpfs_create, 69 .vop_mknod = tmpfs_mknod, 70 .vop_open = tmpfs_open, 71 .vop_close = tmpfs_close, 72 .vop_access = tmpfs_access, 73 .vop_getattr = tmpfs_getattr, 74 .vop_setattr = tmpfs_setattr, 75 .vop_read = tmpfs_read, 76 .vop_write = tmpfs_write, 77 .vop_ioctl = tmpfs_ioctl, 78 .vop_poll = tmpfs_poll, 79 .vop_kqfilter = vop_generic_kqfilter, 80 .vop_revoke = vop_generic_revoke, 81 .vop_fsync = tmpfs_fsync, 82 .vop_remove = tmpfs_remove, 83 .vop_link = tmpfs_link, 84 .vop_rename = tmpfs_rename, 85 .vop_mkdir = tmpfs_mkdir, 86 .vop_rmdir = tmpfs_rmdir, 87 .vop_symlink = tmpfs_symlink, 88 .vop_readdir = tmpfs_readdir, 89 .vop_readlink = tmpfs_readlink, 90 .vop_abortop = vop_generic_abortop, 91 .vop_inactive = tmpfs_inactive, 92 .vop_reclaim = tmpfs_reclaim, 93 .vop_lock = tmpfs_lock, 94 .vop_unlock = tmpfs_unlock, 95 .vop_bmap = vop_generic_bmap, 96 .vop_strategy = tmpfs_strategy, 97 .vop_print = tmpfs_print, 98 .vop_islocked = tmpfs_islocked, 99 .vop_pathconf = tmpfs_pathconf, 100 .vop_advlock = tmpfs_advlock, 101 .vop_bwrite = tmpfs_bwrite, 102 }; 103 104 /* 105 * tmpfs_lookup: path name traversal routine. 106 * 107 * Arguments: dvp (directory being searched), vpp (result), 108 * cnp (component name - path). 109 * 110 * => Caller holds a reference and lock on dvp. 111 * => We return looked-up vnode (vpp) locked, with a reference held. 112 */ 113 int 114 tmpfs_lookup(void *v) 115 { 116 struct vop_lookup_args /* { 117 struct vnode *a_dvp; 118 struct vnode **a_vpp; 119 struct componentname *a_cnp; 120 } */ *ap = v; 121 struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp; 122 struct componentname *cnp = ap->a_cnp; 123 struct ucred *cred = cnp->cn_cred; 124 const int lastcn = (cnp->cn_flags & ISLASTCN) != 0; 125 const int lockparent = (cnp->cn_flags & LOCKPARENT) != 0; 126 tmpfs_node_t *dnode, *tnode; 127 tmpfs_dirent_t *de; 128 int cachefound; 129 int error; 130 131 KASSERT(VOP_ISLOCKED(dvp)); 132 133 dnode = VP_TO_TMPFS_DIR(dvp); 134 cnp->cn_flags &= ~PDIRUNLOCK; 135 *vpp = NULL; 136 137 /* Check accessibility of directory. */ 138 error = VOP_ACCESS(dvp, VEXEC, cred, curproc); 139 if (error) { 140 goto out; 141 } 142 143 /* 144 * If requesting the last path component on a read-only file system 145 * with a write operation, deny it. 146 */ 147 if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 && 148 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 149 error = EROFS; 150 goto out; 151 } 152 153 /* 154 * Avoid doing a linear scan of the directory if the requested 155 * directory/name couple is already in the cache. 156 */ 157 cachefound = cache_lookup(dvp, vpp, cnp); 158 if (cachefound == ENOENT /* && *vpp == NULLVP */) 159 return ENOENT; /* Negative cache hit. */ 160 else if (cachefound != -1) 161 return 0; /* Found in cache. */ 162 163 if (cnp->cn_flags & ISDOTDOT) { 164 tmpfs_node_t *pnode; 165 166 /* 167 * Lookup of ".." case. 168 */ 169 if (lastcn && cnp->cn_nameiop == RENAME) { 170 error = EINVAL; 171 goto out; 172 } 173 KASSERT(dnode->tn_type == VDIR); 174 pnode = dnode->tn_spec.tn_dir.tn_parent; 175 if (pnode == NULL) { 176 error = ENOENT; 177 goto out; 178 } 179 180 /* 181 * Lock the parent tn_nlock before releasing the vnode lock, 182 * and thus prevents parent from disappearing. 183 */ 184 rw_enter_write(&pnode->tn_nlock); 185 VOP_UNLOCK(dvp, 0, curproc); 186 187 /* 188 * Get a vnode of the '..' entry and re-acquire the lock. 189 * Release the tn_nlock. 190 */ 191 error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp); 192 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, curproc); 193 goto out; 194 195 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 196 /* 197 * Lookup of "." case. 198 */ 199 if (lastcn && cnp->cn_nameiop == RENAME) { 200 error = EISDIR; 201 goto out; 202 } 203 vref(dvp); 204 *vpp = dvp; 205 error = 0; 206 goto done; 207 } 208 209 /* 210 * Other lookup cases: perform directory scan. 211 */ 212 de = tmpfs_dir_lookup(dnode, cnp); 213 if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) { 214 /* 215 * The entry was not found in the directory. This is valid 216 * if we are creating or renaming an entry and are working 217 * on the last component of the path name. 218 */ 219 if (lastcn && (cnp->cn_nameiop == CREATE || 220 cnp->cn_nameiop == RENAME)) { 221 error = VOP_ACCESS(dvp, VWRITE, cred, curproc); 222 if (error) { 223 goto out; 224 } 225 /* 226 * We are creating an entry in the file system, so 227 * save its name for further use by tmpfs_create(). 228 */ 229 cnp->cn_flags |= SAVENAME; 230 error = EJUSTRETURN; 231 } else { 232 error = ENOENT; 233 } 234 if (de) { 235 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT); 236 /* cnp->cn_flags |= ISWHITEOUT; */ 237 } 238 goto done; 239 } 240 241 tnode = de->td_node; 242 243 /* 244 * If it is not the last path component and found a non-directory 245 * or non-link entry (which may itself be pointing to a directory), 246 * raise an error. 247 */ 248 if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) { 249 error = ENOTDIR; 250 goto out; 251 } 252 253 /* Check the permissions. */ 254 if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 255 error = VOP_ACCESS(dvp, VWRITE, cred, curproc); 256 if (error) 257 goto out; 258 259 /* 260 * If not root and directory is sticky, check for permission 261 * on directory or on file. This implements append-only 262 * directories. 263 */ 264 if ((dnode->tn_mode & S_ISTXT) != 0) { 265 if (cred->cr_uid != 0 && 266 cred->cr_uid != dnode->tn_uid && 267 cred->cr_uid != tnode->tn_uid) { 268 error = EPERM; 269 goto out; 270 } 271 } 272 273 /* 274 * XXX pedro: We might need cn_nameptr later in tmpfs_remove() 275 * or tmpfs_rmdir() for a tmpfs_dir_lookup(). We should really 276 * get rid of SAVENAME at some point. 277 */ 278 if (cnp->cn_nameiop == DELETE) 279 cnp->cn_flags |= SAVENAME; 280 } 281 282 /* Get a vnode for the matching entry. */ 283 rw_enter_write(&tnode->tn_nlock); 284 error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp); 285 done: 286 /* 287 * Cache the result, unless request was for creation (as it does 288 * not improve the performance). 289 */ 290 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) { 291 cache_enter(dvp, *vpp, cnp); 292 } 293 out: 294 /* 295 * If (1) we succeded, (2) found a distinct vnode to return and (3) were 296 * either explicitely told to keep the parent locked or are in the 297 * middle of a lookup, unlock the parent vnode. 298 */ 299 if ((error == 0 || error == EJUSTRETURN) && /* (1) */ 300 *vpp != dvp && /* (2) */ 301 (!lockparent || !lastcn)) { /* (3) */ 302 VOP_UNLOCK(dvp, 0, curproc); 303 cnp->cn_flags |= PDIRUNLOCK; 304 } else 305 KASSERT(VOP_ISLOCKED(dvp)); 306 307 KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error); 308 309 return error; 310 } 311 312 int 313 tmpfs_create(void *v) 314 { 315 struct vop_create_args /* { 316 struct vnode *a_dvp; 317 struct vnode **a_vpp; 318 struct componentname *a_cnp; 319 struct vattr *a_vap; 320 } */ *ap = v; 321 struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp; 322 struct componentname *cnp = ap->a_cnp; 323 struct vattr *vap = ap->a_vap; 324 325 KASSERT(VOP_ISLOCKED(dvp)); 326 KASSERT(cnp->cn_flags & HASBUF); 327 KASSERT(vap->va_type == VREG || vap->va_type == VSOCK); 328 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 329 } 330 331 int 332 tmpfs_mknod(void *v) 333 { 334 struct vop_mknod_args /* { 335 struct vnode *a_dvp; 336 struct vnode **a_vpp; 337 struct componentname *a_cnp; 338 struct vattr *a_vap; 339 } */ *ap = v; 340 struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp; 341 struct componentname *cnp = ap->a_cnp; 342 struct vattr *vap = ap->a_vap; 343 enum vtype vt = vap->va_type; 344 int error; 345 346 if (vt != VBLK && vt != VCHR && vt != VFIFO) { 347 vput(dvp); 348 return EINVAL; 349 } 350 351 /* tmpfs_alloc_file() will unlock 'dvp'. */ 352 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 353 if (error) 354 return error; 355 356 /* 357 * As in ufs_mknod(), remove inode so that it will be reloaded by 358 * VFS_VGET and checked to see if it is an alias of an existing entry 359 * in the vnode cache. 360 */ 361 vput(*vpp); 362 (*vpp)->v_type = VNON; 363 vgone(*vpp); 364 *vpp = NULL; 365 366 return 0; 367 } 368 369 int 370 tmpfs_open(void *v) 371 { 372 struct vop_open_args /* { 373 struct vnode *a_vp; 374 int a_mode; 375 kauth_cred_t a_cred; 376 } */ *ap = v; 377 struct vnode *vp = ap->a_vp; 378 mode_t mode = ap->a_mode; 379 tmpfs_node_t *node; 380 381 KASSERT(VOP_ISLOCKED(vp)); 382 383 node = VP_TO_TMPFS_NODE(vp); 384 if (node->tn_links < 1) { 385 /* 386 * The file is still active, but all its names have been 387 * removed (e.g. by a "rmdir $(pwd)"). It cannot be opened 388 * any more, as it is about to be destroyed. 389 */ 390 return ENOENT; 391 } 392 393 /* If the file is marked append-only, deny write requests. */ 394 if ((node->tn_flags & APPEND) != 0 && 395 (mode & (FWRITE | O_APPEND)) == FWRITE) { 396 return EPERM; 397 } 398 return 0; 399 } 400 401 int 402 tmpfs_close(void *v) 403 { 404 struct vop_close_args /* { 405 struct vnode *a_vp; 406 int a_fflag; 407 kauth_cred_t a_cred; 408 } */ *ap = v; 409 struct vnode *vp = ap->a_vp; 410 411 KASSERT(VOP_ISLOCKED(vp)); 412 413 return 0; 414 } 415 416 int 417 tmpfs_access(void *v) 418 { 419 struct vop_access_args /* { 420 struct vnode *a_vp; 421 int a_mode; 422 kauth_cred_t a_cred; 423 } */ *ap = v; 424 struct vnode *vp = ap->a_vp; 425 mode_t mode = ap->a_mode; 426 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 427 const int writing = (mode & VWRITE) != 0; 428 429 KASSERT(VOP_ISLOCKED(vp)); 430 431 /* Possible? */ 432 switch (vp->v_type) { 433 case VDIR: 434 case VLNK: 435 case VREG: 436 if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) { 437 return EROFS; 438 } 439 break; 440 case VBLK: 441 case VCHR: 442 case VSOCK: 443 case VFIFO: 444 break; 445 default: 446 return EINVAL; 447 } 448 if (writing && (node->tn_flags & IMMUTABLE) != 0) { 449 return EPERM; 450 } 451 452 return (vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid, 453 mode, ap->a_cred)); 454 } 455 456 int 457 tmpfs_getattr(void *v) 458 { 459 struct vop_getattr_args /* { 460 struct vnode *a_vp; 461 struct vattr *a_vap; 462 kauth_cred_t a_cred; 463 } */ *ap = v; 464 struct vnode *vp = ap->a_vp; 465 struct vattr *vap = ap->a_vap; 466 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 467 468 vattr_null(vap); 469 470 vap->va_type = vp->v_type; 471 vap->va_mode = node->tn_mode; 472 vap->va_nlink = node->tn_links; 473 vap->va_uid = node->tn_uid; 474 vap->va_gid = node->tn_gid; 475 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 476 vap->va_fileid = node->tn_id; 477 vap->va_size = node->tn_size; 478 vap->va_blocksize = PAGE_SIZE; 479 vap->va_atime = node->tn_atime; 480 vap->va_mtime = node->tn_mtime; 481 vap->va_ctime = node->tn_ctime; 482 /* vap->va_birthtime = node->tn_birthtime; */ 483 vap->va_gen = TMPFS_NODE_GEN(node); 484 vap->va_flags = node->tn_flags; 485 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 486 node->tn_spec.tn_dev.tn_rdev : VNOVAL; 487 vap->va_bytes = round_page(node->tn_size); 488 vap->va_filerev = VNOVAL; 489 vap->va_vaflags = 0; 490 vap->va_spare = VNOVAL; /* XXX */ 491 492 return 0; 493 } 494 495 #define GOODTIME(tv) ((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL) 496 /* XXX Should this operation be atomic? I think it should, but code in 497 * XXX other places (e.g., ufs) doesn't seem to be... */ 498 int 499 tmpfs_setattr(void *v) 500 { 501 struct vop_setattr_args /* { 502 struct vnode *a_vp; 503 struct vattr *a_vap; 504 kauth_cred_t a_cred; 505 } */ *ap = v; 506 struct vnode *vp = ap->a_vp; 507 struct vattr *vap = ap->a_vap; 508 struct ucred *cred = ap->a_cred; 509 struct proc *p = curproc; 510 int error = 0; 511 512 KASSERT(VOP_ISLOCKED(vp)); 513 514 /* Abort if any unsettable attribute is given. */ 515 if (vap->va_type != VNON || vap->va_nlink != VNOVAL || 516 vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL || 517 vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) || 518 vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL || 519 vap->va_bytes != VNOVAL) { 520 return EINVAL; 521 } 522 if (error == 0 && (vap->va_flags != VNOVAL)) 523 error = tmpfs_chflags(vp, vap->va_flags, cred, p); 524 525 if (error == 0 && (vap->va_size != VNOVAL)) 526 error = tmpfs_chsize(vp, vap->va_size, cred, p); 527 528 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 529 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, p); 530 531 if (error == 0 && (vap->va_mode != VNOVAL)) 532 error = tmpfs_chmod(vp, vap->va_mode, cred, p); 533 534 if (error == 0 && (GOODTIME(&vap->va_atime) 535 || GOODTIME(&vap->va_mtime))) { 536 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 537 vap->va_vaflags, cred, p); 538 if (error == 0) 539 return 0; 540 } 541 return error; 542 } 543 544 int 545 tmpfs_read(void *v) 546 { 547 struct vop_read_args /* { 548 struct vnode *a_vp; 549 struct uio *a_uio; 550 int a_ioflag; 551 struct ucred *a_cred; 552 } */ *ap = v; 553 struct vnode *vp = ap->a_vp; 554 struct uio *uio = ap->a_uio; 555 /* const int ioflag = ap->a_ioflag; */ 556 tmpfs_node_t *node; 557 int error; 558 559 KASSERT(VOP_ISLOCKED(vp)); 560 561 if (vp->v_type != VREG) { 562 return EISDIR; 563 } 564 if (uio->uio_offset < 0) { 565 return EINVAL; 566 } 567 568 node = VP_TO_TMPFS_NODE(vp); 569 tmpfs_update(node, TMPFS_NODE_ACCESSED); 570 error = 0; 571 572 while (error == 0 && uio->uio_resid > 0) { 573 vsize_t len; 574 575 if (node->tn_size <= uio->uio_offset) { 576 break; 577 } 578 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid); 579 if (len == 0) { 580 break; 581 } 582 error = tmpfs_uiomove(node, uio, len); 583 } 584 585 return error; 586 } 587 588 int 589 tmpfs_write(void *v) 590 { 591 struct vop_write_args /* { 592 struct vnode *a_vp; 593 struct uio *a_uio; 594 int a_ioflag; 595 kauth_cred_t a_cred; 596 } */ *ap = v; 597 struct vnode *vp = ap->a_vp; 598 struct uio *uio = ap->a_uio; 599 const int ioflag = ap->a_ioflag; 600 tmpfs_node_t *node; 601 off_t oldsize; 602 int extended; 603 int error; 604 605 KASSERT(VOP_ISLOCKED(vp)); 606 607 node = VP_TO_TMPFS_NODE(vp); 608 oldsize = node->tn_size; 609 610 if (uio->uio_offset < 0 || vp->v_type != VREG) { 611 error = EINVAL; 612 goto out; 613 } 614 if (uio->uio_resid == 0) { 615 error = 0; 616 goto out; 617 } 618 if (ioflag & IO_APPEND) { 619 uio->uio_offset = node->tn_size; 620 } 621 622 extended = uio->uio_offset + uio->uio_resid > node->tn_size; 623 if (extended) { 624 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid); 625 if (error) 626 goto out; 627 } 628 629 error = 0; 630 while (error == 0 && uio->uio_resid > 0) { 631 vsize_t len; 632 633 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid); 634 if (len == 0) { 635 break; 636 } 637 error = tmpfs_uiomove(node, uio, len); 638 } 639 if (error) { 640 (void)tmpfs_reg_resize(vp, oldsize); 641 } 642 643 tmpfs_update(node, TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 644 (extended ? TMPFS_NODE_CHANGED : 0)); 645 if (extended) 646 VN_KNOTE(vp, NOTE_WRITE | NOTE_EXTEND); 647 else 648 VN_KNOTE(vp, NOTE_WRITE); 649 out: 650 if (error) { 651 KASSERT(oldsize == node->tn_size); 652 } else { 653 KASSERT(uio->uio_resid == 0); 654 } 655 return error; 656 } 657 658 int 659 tmpfs_fsync(void *v) 660 { 661 struct vop_fsync_args /* { 662 struct vnode *a_vp; 663 struct ucred *a_cred; 664 int a_flags; 665 off_t a_offlo; 666 off_t a_offhi; 667 struct lwp *a_l; 668 } */ *ap = v; 669 struct vnode *vp = ap->a_vp; 670 671 /* Nothing to do. Just update. */ 672 KASSERT(VOP_ISLOCKED(vp)); 673 return 0; 674 } 675 676 /* 677 * tmpfs_remove: unlink a file. 678 * 679 * => Both directory (dvp) and file (vp) are locked. 680 * => We unlock and drop the reference on both. 681 */ 682 int 683 tmpfs_remove(void *v) 684 { 685 struct vop_remove_args /* { 686 struct vnode *a_dvp; 687 struct vnode *a_vp; 688 struct componentname *a_cnp; 689 } */ *ap = v; 690 struct vnode *dvp = ap->a_dvp, *vp = ap->a_vp; 691 struct componentname *cnp = ap->a_cnp; 692 tmpfs_node_t *node; 693 tmpfs_dirent_t *de; 694 int error; 695 696 KASSERT(VOP_ISLOCKED(dvp)); 697 KASSERT(VOP_ISLOCKED(vp)); 698 KASSERT(cnp->cn_flags & HASBUF); 699 700 if (vp->v_type == VDIR) { 701 error = EPERM; 702 goto out; 703 } 704 node = VP_TO_TMPFS_NODE(vp); 705 706 /* Files marked as immutable or append-only cannot be deleted. */ 707 if (node->tn_flags & (IMMUTABLE | APPEND)) { 708 error = EPERM; 709 goto out; 710 } 711 712 /* Lookup the directory entry (check the cached hint first). */ 713 de = tmpfs_dir_cached(node); 714 if (de == NULL) { 715 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp); 716 de = tmpfs_dir_lookup(dnode, cnp); 717 } 718 719 KASSERT(de && de->td_node == node); 720 721 /* 722 * Remove the entry from the directory (drops the link count) and 723 * destroy it or replace it with a whiteout. 724 * Note: the inode referred by it will not be destroyed 725 * until the vnode is reclaimed/recycled. 726 */ 727 tmpfs_dir_detach(dvp, de); 728 if (0 /* ap->a_cnp->cn_flags & DOWHITEOUT */) 729 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT); 730 else 731 tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de); 732 error = 0; 733 out: 734 pool_put(&namei_pool, cnp->cn_pnbuf); 735 /* Drop the references and unlock the vnodes. */ 736 vput(vp); 737 if (dvp == vp) { 738 vrele(dvp); 739 } else { 740 vput(dvp); 741 } 742 return error; 743 } 744 745 /* 746 * tmpfs_link: create a hard link. 747 */ 748 int 749 tmpfs_link(void *v) 750 { 751 struct vop_link_args /* { 752 struct vnode *a_dvp; 753 struct vnode *a_vp; 754 struct componentname *a_cnp; 755 } */ *ap = v; 756 struct vnode *dvp = ap->a_dvp; 757 struct vnode *vp = ap->a_vp; 758 struct componentname *cnp = ap->a_cnp; 759 tmpfs_node_t *dnode, *node; 760 tmpfs_dirent_t *de; 761 int error; 762 763 KASSERT(dvp != vp); 764 KASSERT(VOP_ISLOCKED(dvp)); 765 766 if (vp->v_type == VDIR) { 767 VOP_ABORTOP(dvp, cnp); 768 vput(dvp); 769 return EPERM; 770 } 771 772 if (dvp->v_mount != vp->v_mount) { 773 VOP_ABORTOP(dvp, cnp); 774 vput(dvp); 775 return EXDEV; 776 } 777 778 dnode = VP_TO_TMPFS_DIR(dvp); 779 node = VP_TO_TMPFS_NODE(vp); 780 781 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc); 782 783 /* Check for maximum number of links limit. */ 784 if (node->tn_links == LINK_MAX) { 785 error = EMLINK; 786 goto out; 787 } 788 KASSERT(node->tn_links < LINK_MAX); 789 790 /* We cannot create links of files marked immutable or append-only. */ 791 if (node->tn_flags & (IMMUTABLE | APPEND)) { 792 error = EPERM; 793 goto out; 794 } 795 796 /* Allocate a new directory entry to represent the inode. */ 797 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), 798 cnp->cn_nameptr, cnp->cn_namelen, &de); 799 if (error) { 800 goto out; 801 } 802 803 /* 804 * Insert the entry into the directory. 805 * It will increase the inode link count. 806 */ 807 tmpfs_dir_attach(dvp, de, node); 808 809 /* Update the timestamps and trigger the event. */ 810 if (node->tn_vnode) { 811 VN_KNOTE(node->tn_vnode, NOTE_LINK); 812 } 813 tmpfs_update(node, TMPFS_NODE_CHANGED); 814 error = 0; 815 out: 816 VOP_UNLOCK(vp, 0, curproc); 817 vput(dvp); 818 return error; 819 } 820 821 int 822 tmpfs_mkdir(void *v) 823 { 824 struct vop_mkdir_args /* { 825 struct vnode *a_dvp; 826 struct vnode **a_vpp; 827 struct componentname *a_cnp; 828 struct vattr *a_vap; 829 } */ *ap = v; 830 struct vnode *dvp = ap->a_dvp; 831 struct vnode **vpp = ap->a_vpp; 832 struct componentname *cnp = ap->a_cnp; 833 struct vattr *vap = ap->a_vap; 834 835 KASSERT(vap->va_type == VDIR); 836 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 837 } 838 839 int 840 tmpfs_rmdir(void *v) 841 { 842 struct vop_rmdir_args /* { 843 struct vnode *a_dvp; 844 struct vnode *a_vp; 845 struct componentname *a_cnp; 846 } */ *ap = v; 847 struct vnode *dvp = ap->a_dvp; 848 struct vnode *vp = ap->a_vp; 849 struct componentname *cnp = ap->a_cnp; 850 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount); 851 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp); 852 tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp); 853 tmpfs_dirent_t *de; 854 int error = 0; 855 856 KASSERT(VOP_ISLOCKED(dvp)); 857 KASSERT(VOP_ISLOCKED(vp)); 858 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode); 859 KASSERT(cnp->cn_flags & HASBUF); 860 861 /* 862 * Directories with more than two non-whiteout 863 * entries ('.' and '..') cannot be removed. 864 */ 865 if (node->tn_size > 0) { 866 KASSERT(error == 0); 867 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) { 868 if (de->td_node != TMPFS_NODE_WHITEOUT) { 869 error = ENOTEMPTY; 870 break; 871 } 872 } 873 if (error) 874 goto out; 875 } 876 877 /* Lookup the directory entry (check the cached hint first). */ 878 de = tmpfs_dir_cached(node); 879 if (de == NULL) 880 de = tmpfs_dir_lookup(dnode, cnp); 881 882 KASSERT(de && de->td_node == node); 883 884 /* Check flags to see if we are allowed to remove the directory. */ 885 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) { 886 error = EPERM; 887 goto out; 888 } 889 890 /* Decrement the link count for the virtual '.' entry. */ 891 node->tn_links--; 892 tmpfs_update(node, TMPFS_NODE_STATUSALL); 893 894 /* Detach the directory entry from the directory. */ 895 tmpfs_dir_detach(dvp, de); 896 897 /* Purge the cache for parent. */ 898 cache_purge(dvp); 899 900 /* 901 * Destroy the directory entry or replace it with a whiteout. 902 * Note: the inode referred by it will not be destroyed 903 * until the vnode is reclaimed. 904 */ 905 if (0 /* ap->a_cnp->cn_flags & DOWHITEOUT */) 906 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT); 907 else 908 tmpfs_free_dirent(tmp, de); 909 910 /* Destroy the whiteout entries from the node. */ 911 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) { 912 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT); 913 tmpfs_dir_detach(vp, de); 914 tmpfs_free_dirent(tmp, de); 915 } 916 917 KASSERT(node->tn_links == 0); 918 out: 919 pool_put(&namei_pool, cnp->cn_pnbuf); 920 /* Release the nodes. */ 921 vput(dvp); 922 vput(vp); 923 return error; 924 } 925 926 int 927 tmpfs_symlink(void *v) 928 { 929 struct vop_symlink_args /* { 930 struct vnode *a_dvp; 931 struct vnode **a_vpp; 932 struct componentname *a_cnp; 933 struct vattr *a_vap; 934 char *a_target; 935 } */ *ap = v; 936 struct vnode *dvp = ap->a_dvp; 937 struct vnode **vpp = ap->a_vpp; 938 struct componentname *cnp = ap->a_cnp; 939 struct vattr *vap = ap->a_vap; 940 char *target = ap->a_target; 941 int error; 942 943 KASSERT(vap->va_type == 0); 944 vap->va_type = VLNK; 945 946 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 947 if (error == 0) 948 vput(*vpp); 949 950 return 0; 951 } 952 953 int 954 tmpfs_readdir(void *v) 955 { 956 struct vop_readdir_args /* { 957 struct vnode *a_vp; 958 struct uio *a_uio; 959 kauth_cred_t a_cred; 960 int *a_eofflag; 961 off_t **a_cookies; 962 int *ncookies; 963 } */ *ap = v; 964 struct vnode *vp = ap->a_vp; 965 struct uio *uio = ap->a_uio; 966 int *eofflag = ap->a_eofflag; 967 u_long **cookies = ap->a_cookies; 968 int *ncookies = ap->a_ncookies; 969 off_t startoff, cnt; 970 tmpfs_node_t *node; 971 int error; 972 973 KASSERT(VOP_ISLOCKED(vp)); 974 975 /* This operation only makes sense on directory nodes. */ 976 if (vp->v_type != VDIR) { 977 return ENOTDIR; 978 } 979 node = VP_TO_TMPFS_DIR(vp); 980 startoff = uio->uio_offset; 981 cnt = 0; 982 if (node->tn_links == 0) { 983 error = 0; 984 goto out; 985 } 986 987 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 988 error = tmpfs_dir_getdotdent(node, uio); 989 if (error != 0) { 990 if (error == -1) 991 error = 0; 992 goto out; 993 } 994 cnt++; 995 } 996 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 997 error = tmpfs_dir_getdotdotdent(node, uio); 998 if (error != 0) { 999 if (error == -1) 1000 error = 0; 1001 goto out; 1002 } 1003 cnt++; 1004 } 1005 error = tmpfs_dir_getdents(node, uio, &cnt); 1006 if (error == -1) { 1007 error = 0; 1008 } 1009 KASSERT(error >= 0); 1010 out: 1011 if (eofflag != NULL) { 1012 *eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1013 } 1014 if (error || cookies == NULL || ncookies == NULL) { 1015 return error; 1016 } 1017 1018 /* Update NFS-related variables, if any. */ 1019 off_t i, off = startoff; 1020 tmpfs_dirent_t *de = NULL; 1021 1022 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 1023 *ncookies = cnt; 1024 1025 for (i = 0; i < cnt; i++) { 1026 KASSERT(off != TMPFS_DIRCOOKIE_EOF); 1027 if (off != TMPFS_DIRCOOKIE_DOT) { 1028 if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1029 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir); 1030 } else if (de != NULL) { 1031 de = TAILQ_NEXT(de, td_entries); 1032 } else { 1033 de = tmpfs_dir_lookupbycookie(node, off); 1034 KASSERT(de != NULL); 1035 de = TAILQ_NEXT(de, td_entries); 1036 } 1037 if (de == NULL) { 1038 off = TMPFS_DIRCOOKIE_EOF; 1039 } else { 1040 off = tmpfs_dircookie(de); 1041 } 1042 } else { 1043 off = TMPFS_DIRCOOKIE_DOTDOT; 1044 } 1045 (*cookies)[i] = off; 1046 } 1047 KASSERT(uio->uio_offset == off); 1048 return error; 1049 } 1050 1051 int 1052 tmpfs_readlink(void *v) 1053 { 1054 struct vop_readlink_args /* { 1055 struct vnode *a_vp; 1056 struct uio *a_uio; 1057 kauth_cred_t a_cred; 1058 } */ *ap = v; 1059 struct vnode *vp = ap->a_vp; 1060 struct uio *uio = ap->a_uio; 1061 tmpfs_node_t *node; 1062 int error; 1063 1064 KASSERT(VOP_ISLOCKED(vp)); 1065 KASSERT(uio->uio_offset == 0); 1066 KASSERT(vp->v_type == VLNK); 1067 1068 node = VP_TO_TMPFS_NODE(vp); 1069 error = uiomove(node->tn_spec.tn_lnk.tn_link, 1070 MIN(node->tn_size, uio->uio_resid), uio); 1071 tmpfs_update(node, TMPFS_NODE_ACCESSED); 1072 1073 return error; 1074 } 1075 1076 int 1077 tmpfs_inactive(void *v) 1078 { 1079 struct vop_inactive_args /* { 1080 struct vnode *a_vp; 1081 int *a_recycle; 1082 } */ *ap = v; 1083 struct vnode *vp = ap->a_vp; 1084 tmpfs_node_t *node; 1085 1086 KASSERT(VOP_ISLOCKED(vp)); 1087 1088 node = VP_TO_TMPFS_NODE(vp); 1089 1090 if (vp->v_type == VREG && tmpfs_uio_cached(node)) 1091 tmpfs_uio_uncache(node); 1092 1093 VOP_UNLOCK(vp, 0, curproc); 1094 1095 /* 1096 * If we are done with the node, reclaim it so that it can be reused 1097 * immediately. 1098 */ 1099 if (node->tn_links == 0) 1100 vrecycle(vp, curproc); 1101 1102 return 0; 1103 } 1104 1105 int 1106 tmpfs_reclaim(void *v) 1107 { 1108 struct vop_reclaim_args /* { 1109 struct vnode *a_vp; 1110 } */ *ap = v; 1111 struct vnode *vp = ap->a_vp; 1112 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount); 1113 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1114 int racing; 1115 1116 /* Disassociate inode from vnode. */ 1117 rw_enter_write(&node->tn_nlock); 1118 node->tn_vnode = NULL; 1119 vp->v_data = NULL; 1120 /* Check if tmpfs_vnode_get() is racing with us. */ 1121 racing = TMPFS_NODE_RECLAIMING(node); 1122 rw_exit_write(&node->tn_nlock); 1123 1124 /* 1125 * If inode is not referenced, i.e. no links, then destroy it. 1126 * Note: if racing - inode is about to get a new vnode, leave it. 1127 */ 1128 if (node->tn_links == 0 && !racing) { 1129 tmpfs_free_node(tmp, node); 1130 } 1131 return 0; 1132 } 1133 1134 int 1135 tmpfs_pathconf(void *v) 1136 { 1137 struct vop_pathconf_args /* { 1138 struct vnode *a_vp; 1139 int a_name; 1140 register_t *a_retval; 1141 } */ *ap = v; 1142 const int name = ap->a_name; 1143 register_t *retval = ap->a_retval; 1144 int error = 0; 1145 1146 switch (name) { 1147 case _PC_LINK_MAX: 1148 *retval = LINK_MAX; 1149 break; 1150 case _PC_NAME_MAX: 1151 *retval = TMPFS_MAXNAMLEN; 1152 break; 1153 case _PC_CHOWN_RESTRICTED: 1154 *retval = 1; 1155 break; 1156 case _PC_NO_TRUNC: 1157 *retval = 1; 1158 break; 1159 case _PC_FILESIZEBITS: 1160 *retval = 64; 1161 break; 1162 case _PC_TIMESTAMP_RESOLUTION: 1163 *retval = 1; 1164 break; 1165 default: 1166 error = EINVAL; 1167 } 1168 return error; 1169 } 1170 1171 int 1172 tmpfs_advlock(void *v) 1173 { 1174 struct vop_advlock_args /* { 1175 struct vnode *a_vp; 1176 void * a_id; 1177 int a_op; 1178 struct flock *a_fl; 1179 int a_flags; 1180 } */ *ap = v; 1181 struct vnode *vp = ap->a_vp; 1182 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1183 1184 return lf_advlock(&node->tn_lockf, node->tn_size, ap->a_id, ap->a_op, 1185 ap->a_fl, ap->a_flags); 1186 } 1187 1188 #if 0 1189 int 1190 tmpfs_getpages(void *v) 1191 { 1192 struct vop_getpages_args /* { 1193 struct vnode *a_vp; 1194 voff_t a_offset; 1195 struct vm_page **a_m; 1196 int *a_count; 1197 int a_centeridx; 1198 vm_prot_t a_access_type; 1199 int a_advice; 1200 int a_flags; 1201 } */ * const ap = v; 1202 struct vnode *vp = ap->a_vp; 1203 const voff_t offset = ap->a_offset; 1204 struct vm_page **pgs = ap->a_m; 1205 const int centeridx = ap->a_centeridx; 1206 const vm_prot_t access_type = ap->a_access_type; 1207 const int advice = ap->a_advice; 1208 const int flags = ap->a_flags; 1209 int error, npages = *ap->a_count; 1210 tmpfs_node_t *node; 1211 struct uvm_object *uobj; 1212 1213 KASSERT(vp->v_type == VREG); 1214 KASSERT(mutex_owned(vp->v_interlock)); 1215 1216 node = VP_TO_TMPFS_NODE(vp); 1217 uobj = node->tn_spec.tn_reg.tn_aobj; 1218 1219 /* 1220 * Currently, PGO_PASTEOF is not supported. 1221 */ 1222 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) { 1223 if ((flags & PGO_LOCKED) == 0) 1224 mutex_exit(vp->v_interlock); 1225 return EINVAL; 1226 } 1227 1228 if (vp->v_size < offset + (npages << PAGE_SHIFT)) { 1229 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT; 1230 } 1231 1232 if ((flags & PGO_LOCKED) != 0) 1233 return EBUSY; 1234 1235 if ((flags & PGO_NOTIMESTAMP) == 0) { 1236 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 1237 node->tn_status |= TMPFS_NODE_ACCESSED; 1238 1239 if ((access_type & VM_PROT_WRITE) != 0) { 1240 node->tn_status |= TMPFS_NODE_MODIFIED; 1241 if (vp->v_mount->mnt_flag & MNT_RELATIME) 1242 node->tn_status |= TMPFS_NODE_ACCESSED; 1243 } 1244 } 1245 1246 /* 1247 * Invoke the pager. 1248 * 1249 * Clean the array of pages before. XXX: PR/32166 1250 * Note that vnode lock is shared with underlying UVM object. 1251 */ 1252 if (pgs) { 1253 memset(pgs, 0, sizeof(struct vm_pages *) * npages); 1254 } 1255 KASSERT(vp->v_interlock == uobj->vmobjlock); 1256 1257 error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx, 1258 access_type, advice, flags | PGO_ALLPAGES); 1259 1260 #if defined(DEBUG) 1261 if (!error && pgs) { 1262 for (int i = 0; i < npages; i++) { 1263 KASSERT(pgs[i] != NULL); 1264 } 1265 } 1266 #endif 1267 return error; 1268 } 1269 1270 int 1271 tmpfs_putpages(void *v) 1272 { 1273 struct vop_putpages_args /* { 1274 struct vnode *a_vp; 1275 voff_t a_offlo; 1276 voff_t a_offhi; 1277 int a_flags; 1278 } */ * const ap = v; 1279 struct vnode *vp = ap->a_vp; 1280 const voff_t offlo = ap->a_offlo; 1281 const voff_t offhi = ap->a_offhi; 1282 const int flags = ap->a_flags; 1283 tmpfs_node_t *node; 1284 struct uvm_object *uobj; 1285 int error; 1286 1287 KASSERT(mutex_owned(vp->v_interlock)); 1288 1289 if (vp->v_type != VREG) { 1290 mutex_exit(vp->v_interlock); 1291 return 0; 1292 } 1293 1294 node = VP_TO_TMPFS_NODE(vp); 1295 uobj = node->tn_spec.tn_reg.tn_aobj; 1296 1297 KASSERT(vp->v_interlock == uobj->vmobjlock); 1298 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags); 1299 1300 /* XXX mtime */ 1301 1302 return error; 1303 } 1304 1305 int 1306 tmpfs_whiteout(void *v) 1307 { 1308 struct vop_whiteout_args /* { 1309 struct vnode *a_dvp; 1310 struct componentname *a_cnp; 1311 int a_flags; 1312 } */ *ap = v; 1313 struct vnode *dvp = ap->a_dvp; 1314 struct componentname *cnp = ap->a_cnp; 1315 const int flags = ap->a_flags; 1316 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount); 1317 tmpfs_dirent_t *de; 1318 int error; 1319 1320 switch (flags) { 1321 case LOOKUP: 1322 break; 1323 case CREATE: 1324 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, 1325 cnp->cn_namelen, &de); 1326 if (error) 1327 return error; 1328 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT); 1329 break; 1330 case DELETE: 1331 cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */ 1332 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp); 1333 if (de == NULL) 1334 return ENOENT; 1335 tmpfs_dir_detach(dvp, de); 1336 tmpfs_free_dirent(tmp, de); 1337 break; 1338 } 1339 return 0; 1340 } 1341 #endif 1342 1343 int 1344 tmpfs_print(void *v) 1345 { 1346 struct vop_print_args /* { 1347 struct vnode *a_vp; 1348 } */ *ap = v; 1349 struct vnode *vp = ap->a_vp; 1350 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1351 1352 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n" 1353 "\tmode 0%o, owner %d, group %d, size %lld", 1354 node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid, 1355 node->tn_gid, node->tn_size); 1356 #ifdef FIFO 1357 if (vp->v_type == VFIFO) 1358 fifo_printinfo(vp); 1359 #endif 1360 printf("\n"); 1361 return 0; 1362 } 1363 1364 /* a null op */ 1365 int 1366 tmpfs_bwrite(void *v) 1367 { 1368 return 0; 1369 } 1370 1371 int 1372 tmpfs_poll(void *v) 1373 { 1374 struct vop_poll_args *ap = v; 1375 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1376 } 1377 1378 int 1379 tmpfs_strategy(void *v) 1380 { 1381 return EOPNOTSUPP; 1382 } 1383 1384 int 1385 tmpfs_ioctl(void *v) 1386 { 1387 return ENOTTY; 1388 } 1389 1390 int 1391 tmpfs_lock(void *v) 1392 { 1393 struct vop_lock_args *ap = v; 1394 tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp); 1395 1396 return lockmgr(&tnp->tn_vlock, ap->a_flags, NULL); 1397 } 1398 1399 int 1400 tmpfs_unlock(void *v) 1401 { 1402 struct vop_unlock_args *ap = v; 1403 tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp); 1404 1405 return lockmgr(&tnp->tn_vlock, ap->a_flags | LK_RELEASE, NULL); 1406 } 1407 1408 int 1409 tmpfs_islocked(void *v) 1410 { 1411 struct vop_islocked_args *ap = v; 1412 tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp); 1413 1414 return lockstatus(&tnp->tn_vlock); 1415 } 1416 1417 /* 1418 * tmpfs_rename: rename routine, the hairiest system call, with the 1419 * insane API. 1420 * 1421 * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent) 1422 * and tvp (to-leaf), if exists (NULL if not). 1423 * 1424 * => Caller holds a reference on fdvp and fvp, they are unlocked. 1425 * Note: fdvp and fvp can refer to the same object (i.e. when it is root). 1426 * 1427 * => Both tdvp and tvp are referenced and locked. It is our responsibility 1428 * to release the references and unlock them (or destroy). 1429 */ 1430 1431 /* 1432 * First, some forward declarations of subroutines. 1433 */ 1434 1435 int tmpfs_sane_rename(struct vnode *, struct componentname *, 1436 struct vnode *, struct componentname *, struct ucred *, int); 1437 int tmpfs_rename_enter(struct mount *, struct tmpfs_mount *, 1438 struct ucred *, 1439 struct vnode *, struct tmpfs_node *, struct componentname *, 1440 struct tmpfs_dirent **, struct vnode **, 1441 struct vnode *, struct tmpfs_node *, struct componentname *, 1442 struct tmpfs_dirent **, struct vnode **); 1443 int tmpfs_rename_enter_common(struct mount *, struct tmpfs_mount *, 1444 struct ucred *, 1445 struct vnode *, struct tmpfs_node *, 1446 struct componentname *, struct tmpfs_dirent **, struct vnode **, 1447 struct componentname *, struct tmpfs_dirent **, struct vnode **); 1448 int tmpfs_rename_enter_separate(struct mount *, struct tmpfs_mount *, 1449 struct ucred *, 1450 struct vnode *, struct tmpfs_node *, struct componentname *, 1451 struct tmpfs_dirent **, struct vnode **, 1452 struct vnode *, struct tmpfs_node *, struct componentname *, 1453 struct tmpfs_dirent **, struct vnode **); 1454 void tmpfs_rename_exit(struct tmpfs_mount *, 1455 struct vnode *, struct vnode *, struct vnode *, struct vnode *); 1456 int tmpfs_rename_lock_directory(struct vnode *, struct tmpfs_node *); 1457 int tmpfs_rename_genealogy(struct tmpfs_node *, struct tmpfs_node *, 1458 struct tmpfs_node **); 1459 int tmpfs_rename_lock(struct mount *, struct ucred *, int, 1460 struct vnode *, struct tmpfs_node *, struct componentname *, int, 1461 struct tmpfs_dirent **, struct vnode **, 1462 struct vnode *, struct tmpfs_node *, struct componentname *, int, 1463 struct tmpfs_dirent **, struct vnode **); 1464 void tmpfs_rename_attachdetach(struct tmpfs_mount *, 1465 struct vnode *, struct tmpfs_dirent *, struct vnode *, 1466 struct vnode *, struct tmpfs_dirent *, struct vnode *); 1467 int tmpfs_do_remove(struct tmpfs_mount *, struct vnode *, 1468 struct tmpfs_node *, struct tmpfs_dirent *, struct vnode *, struct ucred *); 1469 int tmpfs_rename_check_possible(struct tmpfs_node *, 1470 struct tmpfs_node *, struct tmpfs_node *, struct tmpfs_node *); 1471 int tmpfs_rename_check_permitted(struct ucred *, 1472 struct tmpfs_node *, struct tmpfs_node *, 1473 struct tmpfs_node *, struct tmpfs_node *); 1474 int tmpfs_remove_check_possible(struct tmpfs_node *, 1475 struct tmpfs_node *); 1476 int tmpfs_remove_check_permitted(struct ucred *, 1477 struct tmpfs_node *, struct tmpfs_node *); 1478 int tmpfs_check_sticky(struct ucred *, 1479 struct tmpfs_node *, struct tmpfs_node *); 1480 void tmpfs_rename_cache_purge(struct vnode *, struct vnode *, struct vnode *, 1481 struct vnode *); 1482 1483 int 1484 tmpfs_rename(void *v) 1485 { 1486 struct vop_rename_args /* { 1487 struct vnode *a_fdvp; 1488 struct vnode *a_fvp; 1489 struct componentname *a_fcnp; 1490 struct vnode *a_tdvp; 1491 struct vnode *a_tvp; 1492 struct componentname *a_tcnp; 1493 } */ *ap = v; 1494 struct vnode *fdvp = ap->a_fdvp; 1495 struct vnode *fvp = ap->a_fvp; 1496 struct componentname *fcnp = ap->a_fcnp; 1497 struct vnode *tdvp = ap->a_tdvp; 1498 struct vnode *tvp = ap->a_tvp; 1499 struct componentname *tcnp = ap->a_tcnp; 1500 struct ucred *cred; 1501 int error; 1502 1503 KASSERT(fdvp != NULL); 1504 KASSERT(fvp != NULL); 1505 KASSERT(fcnp != NULL); 1506 KASSERT(fcnp->cn_nameptr != NULL); 1507 KASSERT(tdvp != NULL); 1508 KASSERT(tcnp != NULL); 1509 KASSERT(fcnp->cn_nameptr != NULL); 1510 /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */ 1511 /* KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */ 1512 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE); 1513 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE)); 1514 KASSERT(fdvp->v_type == VDIR); 1515 KASSERT(tdvp->v_type == VDIR); 1516 KASSERT(fcnp->cn_flags & HASBUF); 1517 KASSERT(tcnp->cn_flags & HASBUF); 1518 1519 cred = fcnp->cn_cred; 1520 KASSERT(tcnp->cn_cred == cred); 1521 1522 /* 1523 * Check for cross-device rename. 1524 */ 1525 if (fvp->v_mount != tdvp->v_mount || 1526 (tvp != NULL && (fvp->v_mount != tvp->v_mount))) { 1527 VOP_ABORTOP(tdvp, tcnp); 1528 if (tdvp == tvp) 1529 vrele(tdvp); 1530 else 1531 vput(tdvp); 1532 if (tvp != NULL) 1533 vput(tvp); 1534 VOP_ABORTOP(fdvp, fcnp); 1535 vrele(fdvp); 1536 vrele(fvp); 1537 return EXDEV; 1538 } 1539 1540 /* 1541 * Sanitize our world from the VFS insanity. Unlock the target 1542 * directory and node, which are locked. Release the children, 1543 * which are referenced. Check for rename("x", "y/."), which 1544 * it is our responsibility to reject, not the caller's. (But 1545 * the caller does reject rename("x/.", "y"). Go figure.) 1546 */ 1547 1548 VOP_UNLOCK(tdvp, 0, curproc); 1549 if ((tvp != NULL) && (tvp != tdvp)) 1550 VOP_UNLOCK(tvp, 0, curproc); 1551 1552 vrele(fvp); 1553 if (tvp != NULL) 1554 vrele(tvp); 1555 1556 if (tvp == tdvp) { 1557 error = EINVAL; 1558 goto out; 1559 } 1560 1561 error = tmpfs_sane_rename(fdvp, fcnp, tdvp, tcnp, cred, 0); 1562 1563 out: /* 1564 * All done, whether with success or failure. Release the 1565 * directory nodes now, as the caller expects from the VFS 1566 * protocol. 1567 */ 1568 vrele(fdvp); 1569 vrele(tdvp); 1570 1571 return error; 1572 } 1573 1574 /* 1575 * tmpfs_sane_rename: rename routine, the hairiest system call, with 1576 * the sane API. 1577 * 1578 * Arguments: 1579 * 1580 * . fdvp (from directory vnode), 1581 * . fcnp (from component name), 1582 * . tdvp (to directory vnode), and 1583 * . tcnp (to component name). 1584 * 1585 * fdvp and tdvp must be referenced and unlocked. 1586 */ 1587 int 1588 tmpfs_sane_rename(struct vnode *fdvp, struct componentname *fcnp, 1589 struct vnode *tdvp, struct componentname *tcnp, struct ucred *cred, 1590 int posixly_correct) 1591 { 1592 struct mount *mount; 1593 struct tmpfs_mount *tmpfs; 1594 struct tmpfs_node *fdnode, *tdnode; 1595 struct tmpfs_dirent *fde, *tde; 1596 struct vnode *fvp, *tvp; 1597 char *newname; 1598 int error; 1599 1600 KASSERT(fdvp != NULL); 1601 KASSERT(fcnp != NULL); 1602 KASSERT(tdvp != NULL); 1603 KASSERT(tcnp != NULL); 1604 /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */ 1605 /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */ 1606 KASSERT(fdvp->v_type == VDIR); 1607 KASSERT(tdvp->v_type == VDIR); 1608 KASSERT(fdvp->v_mount == tdvp->v_mount); 1609 KASSERT((fcnp->cn_flags & ISDOTDOT) == 0); 1610 KASSERT((tcnp->cn_flags & ISDOTDOT) == 0); 1611 KASSERT((fcnp->cn_namelen != 1) || (fcnp->cn_nameptr[0] != '.')); 1612 KASSERT((tcnp->cn_namelen != 1) || (tcnp->cn_nameptr[0] != '.')); 1613 KASSERT((fcnp->cn_namelen != 2) || (fcnp->cn_nameptr[0] != '.') || 1614 (fcnp->cn_nameptr[1] != '.')); 1615 KASSERT((tcnp->cn_namelen != 2) || (tcnp->cn_nameptr[0] != '.') || 1616 (tcnp->cn_nameptr[1] != '.')); 1617 1618 /* 1619 * Pull out the tmpfs data structures. 1620 */ 1621 fdnode = VP_TO_TMPFS_NODE(fdvp); 1622 tdnode = VP_TO_TMPFS_NODE(tdvp); 1623 KASSERT(fdnode != NULL); 1624 KASSERT(tdnode != NULL); 1625 KASSERT(fdnode->tn_vnode == fdvp); 1626 KASSERT(tdnode->tn_vnode == tdvp); 1627 KASSERT(fdnode->tn_type == VDIR); 1628 KASSERT(tdnode->tn_type == VDIR); 1629 1630 mount = fdvp->v_mount; 1631 KASSERT(mount != NULL); 1632 KASSERT(mount == tdvp->v_mount); 1633 /* XXX How can we be sure this stays true? (Not that you're 1634 * likely to mount a tmpfs read-only...) */ 1635 KASSERT((mount->mnt_flag & MNT_RDONLY) == 0); 1636 tmpfs = VFS_TO_TMPFS(mount); 1637 KASSERT(tmpfs != NULL); 1638 1639 /* 1640 * Decide whether we need a new name, and allocate memory for 1641 * it if so. Do this before locking anything or taking 1642 * destructive actions so that we can back out safely and sleep 1643 * safely. XXX Is sleeping an issue here? Can this just be 1644 * moved into tmpfs_rename_attachdetach? 1645 */ 1646 if (tmpfs_strname_neqlen(fcnp, tcnp)) { 1647 newname = tmpfs_strname_alloc(tmpfs, tcnp->cn_namelen); 1648 if (newname == NULL) { 1649 error = ENOSPC; 1650 goto out_unlocked; 1651 } 1652 } else { 1653 newname = NULL; 1654 } 1655 1656 /* 1657 * Lock and look up everything. GCC is not very clever. 1658 */ 1659 fde = tde = NULL; 1660 fvp = tvp = NULL; 1661 error = tmpfs_rename_enter(mount, tmpfs, cred, 1662 fdvp, fdnode, fcnp, &fde, &fvp, 1663 tdvp, tdnode, tcnp, &tde, &tvp); 1664 if (error) 1665 goto out_unlocked; 1666 1667 /* 1668 * Check that everything is locked and looks right. 1669 */ 1670 KASSERT(fde != NULL); 1671 KASSERT(fvp != NULL); 1672 KASSERT(fde->td_node != NULL); 1673 KASSERT(fde->td_node->tn_vnode == fvp); 1674 KASSERT(fde->td_node->tn_type == fvp->v_type); 1675 KASSERT((tde == NULL) == (tvp == NULL)); 1676 KASSERT((tde == NULL) || (tde->td_node != NULL)); 1677 KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp)); 1678 KASSERT((tde == NULL) || (tde->td_node->tn_type == tvp->v_type)); 1679 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE); 1680 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE); 1681 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE); 1682 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE)); 1683 1684 /* 1685 * If the source and destination are the same object, we need 1686 * only at most delete the source entry. 1687 */ 1688 if (fvp == tvp) { 1689 KASSERT(tvp != NULL); 1690 if (fde->td_node->tn_type == VDIR) { 1691 /* XXX How can this possibly happen? */ 1692 error = EINVAL; 1693 goto out_locked; 1694 } 1695 if (!posixly_correct && (fde != tde)) { 1696 /* XXX Doesn't work because of locking. 1697 * error = VOP_REMOVE(fdvp, fvp); 1698 */ 1699 error = tmpfs_do_remove(tmpfs, fdvp, fdnode, fde, fvp, 1700 cred); 1701 if (error) 1702 goto out_locked; 1703 } 1704 goto success; 1705 } 1706 KASSERT(fde != tde); 1707 KASSERT(fvp != tvp); 1708 1709 /* 1710 * If the target exists, refuse to rename a directory over a 1711 * non-directory or vice versa, or to clobber a non-empty 1712 * directory. 1713 */ 1714 if (tvp != NULL) { 1715 KASSERT(tde != NULL); 1716 KASSERT(tde->td_node != NULL); 1717 if (fvp->v_type == VDIR && tvp->v_type == VDIR) 1718 error = ((tde->td_node->tn_size > 0)? ENOTEMPTY : 0); 1719 else if (fvp->v_type == VDIR && tvp->v_type != VDIR) 1720 error = ENOTDIR; 1721 else if (fvp->v_type != VDIR && tvp->v_type == VDIR) 1722 error = EISDIR; 1723 else 1724 error = 0; 1725 if (error) 1726 goto out_locked; 1727 KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR)); 1728 } 1729 1730 /* 1731 * Authorize the rename. 1732 */ 1733 error = tmpfs_rename_check_possible(fdnode, fde->td_node, 1734 tdnode, (tde? tde->td_node : NULL)); 1735 if (error) 1736 goto out_locked; 1737 error = tmpfs_rename_check_permitted(cred, fdnode, fde->td_node, 1738 tdnode, (tde? tde->td_node : NULL)); 1739 if (error) 1740 goto out_locked; 1741 1742 /* 1743 * Everything is hunky-dory. Shuffle the directory entries. 1744 */ 1745 tmpfs_rename_attachdetach(tmpfs, fdvp, fde, fvp, tdvp, tde, tvp); 1746 1747 /* 1748 * Update the directory entry's name necessary, and flag 1749 * metadata updates. A memory allocation failure here is not 1750 * OK because we've already committed some changes that we 1751 * can't back out at this point, and we have things locked so 1752 * we can't sleep, hence the early allocation above. 1753 */ 1754 if (newname != NULL) { 1755 KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN); 1756 1757 tmpfs_strname_free(tmpfs, fde->td_name, fde->td_namelen); 1758 fde->td_namelen = (uint16_t)tcnp->cn_namelen; 1759 (void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen); 1760 /* Commit newname and don't free it on the way out. */ 1761 fde->td_name = newname; 1762 newname = NULL; 1763 1764 tmpfs_update(fde->td_node, TMPFS_NODE_CHANGED); 1765 tmpfs_update(tdnode, TMPFS_NODE_MODIFIED); 1766 } 1767 1768 success: 1769 VN_KNOTE(fvp, NOTE_RENAME); 1770 tmpfs_rename_cache_purge(fdvp, fvp, tdvp, tvp); 1771 error = 0; 1772 1773 out_locked: 1774 tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp); 1775 1776 out_unlocked: 1777 /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */ 1778 /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */ 1779 /* KASSERT((fvp == NULL) || (VOP_ISLOCKED(fvp) != LK_EXCLUSIVE)); */ 1780 /* KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */ 1781 1782 if (newname != NULL) 1783 tmpfs_strname_free(tmpfs, newname, tcnp->cn_namelen); 1784 1785 return error; 1786 } 1787 1788 /* 1789 * Look up fcnp in fdnode/fdvp and store its directory entry in fde_ret 1790 * and the associated vnode in fvp_ret; fail if not found. Look up 1791 * tcnp in tdnode/tdvp and store its directory entry in tde_ret and the 1792 * associated vnode in tvp_ret; store null instead if not found. Fail 1793 * if anything has been mounted on any of the nodes involved. 1794 * 1795 * fdvp and tdvp must be referenced. 1796 * 1797 * On entry, nothing is locked. 1798 * 1799 * On success, everything is locked, and *fvp_ret, and *tvp_ret if 1800 * nonnull, are referenced. The only pairs of vnodes that may be 1801 * identical are {fdvp, tdvp} and {fvp, tvp}. 1802 * 1803 * On failure, everything remains as was. 1804 * 1805 * Locking everything including the source and target nodes is 1806 * necessary to make sure that, e.g., link count updates are OK. The 1807 * locking order is, in general, ancestor-first, matching the order you 1808 * need to use to look up a descendant anyway. 1809 */ 1810 int 1811 tmpfs_rename_enter(struct mount *mount, struct tmpfs_mount *tmpfs, 1812 struct ucred *cred, 1813 struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp, 1814 struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret, 1815 struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp, 1816 struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret) 1817 { 1818 int error; 1819 1820 KASSERT(mount != NULL); 1821 KASSERT(tmpfs != NULL); 1822 KASSERT(fdvp != NULL); 1823 KASSERT(fdnode != NULL); 1824 KASSERT(fcnp != NULL); 1825 KASSERT(fde_ret != NULL); 1826 KASSERT(fvp_ret != NULL); 1827 KASSERT(tdvp != NULL); 1828 KASSERT(tdnode != NULL); 1829 KASSERT(tcnp != NULL); 1830 KASSERT(tde_ret != NULL); 1831 KASSERT(tvp_ret != NULL); 1832 KASSERT(fdnode->tn_vnode == fdvp); 1833 KASSERT(tdnode->tn_vnode == tdvp); 1834 KASSERT(fdnode->tn_type == VDIR); 1835 KASSERT(tdnode->tn_type == VDIR); 1836 1837 if (fdvp == tdvp) { 1838 KASSERT(fdnode == tdnode); 1839 error = tmpfs_rename_enter_common(mount, tmpfs, cred, fdvp, 1840 fdnode, fcnp, fde_ret, fvp_ret, tcnp, tde_ret, tvp_ret); 1841 } else { 1842 KASSERT(fdnode != tdnode); 1843 error = tmpfs_rename_enter_separate(mount, tmpfs, cred, 1844 fdvp, fdnode, fcnp, fde_ret, fvp_ret, 1845 tdvp, tdnode, tcnp, tde_ret, tvp_ret); 1846 } 1847 1848 if (error) 1849 return error; 1850 1851 KASSERT(*fde_ret != NULL); 1852 KASSERT(*fvp_ret != NULL); 1853 KASSERT((*tde_ret == NULL) == (*tvp_ret == NULL)); 1854 KASSERT((*tde_ret == NULL) || ((*tde_ret)->td_node != NULL)); 1855 KASSERT((*tde_ret == NULL) || 1856 ((*tde_ret)->td_node->tn_vnode == *tvp_ret)); 1857 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE); 1858 KASSERT(VOP_ISLOCKED(*fvp_ret) == LK_EXCLUSIVE); 1859 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE); 1860 KASSERT((*tvp_ret == NULL) || 1861 (VOP_ISLOCKED(*tvp_ret) == LK_EXCLUSIVE)); 1862 KASSERT(*fvp_ret != fdvp); 1863 KASSERT(*fvp_ret != tdvp); 1864 KASSERT(*tvp_ret != fdvp); 1865 KASSERT(*tvp_ret != tdvp); 1866 return 0; 1867 } 1868 1869 /* 1870 * Lock and look up with a common source/target directory. 1871 */ 1872 int 1873 tmpfs_rename_enter_common(struct mount *mount, struct tmpfs_mount *tmpfs, 1874 struct ucred *cred, 1875 struct vnode *dvp, struct tmpfs_node *dnode, 1876 struct componentname *fcnp, 1877 struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret, 1878 struct componentname *tcnp, 1879 struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret) 1880 { 1881 struct tmpfs_dirent *fde, *tde; 1882 struct vnode *fvp, *tvp; 1883 int error; 1884 1885 error = tmpfs_rename_lock_directory(dvp, dnode); 1886 if (error) 1887 goto fail0; 1888 1889 /* Did we lose a race with mount? */ 1890 if (dvp->v_mountedhere != NULL) { 1891 error = EBUSY; 1892 goto fail1; 1893 } 1894 1895 /* Make sure the caller may read the directory. */ 1896 error = VOP_ACCESS(dvp, VEXEC, cred, curproc); 1897 if (error) 1898 goto fail1; 1899 1900 /* 1901 * The order in which we lock the source and target nodes is 1902 * irrelevant because there can only be one rename on this 1903 * directory in flight at a time, and we have it locked. 1904 */ 1905 1906 fde = tmpfs_dir_lookup(dnode, fcnp); 1907 if (fde == NULL) { 1908 error = ENOENT; 1909 goto fail1; 1910 } 1911 1912 KASSERT(fde->td_node != NULL); 1913 /* We ruled out `.' earlier. */ 1914 KASSERT(fde->td_node != dnode); 1915 /* We ruled out `..' earlier. */ 1916 KASSERT(fde->td_node != dnode->tn_spec.tn_dir.tn_parent); 1917 rw_enter_write(&fde->td_node->tn_nlock); 1918 error = tmpfs_vnode_get(mount, fde->td_node, &fvp); 1919 if (error) 1920 goto fail1; 1921 KASSERT(fvp != NULL); 1922 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE); 1923 KASSERT(fvp != dvp); 1924 KASSERT(fvp->v_mount == mount); 1925 1926 /* Refuse to rename a mount point. */ 1927 if ((fvp->v_type == VDIR) && (fvp->v_mountedhere != NULL)) { 1928 error = EBUSY; 1929 goto fail2; 1930 } 1931 1932 tde = tmpfs_dir_lookup(dnode, tcnp); 1933 if (tde == NULL) { 1934 tvp = NULL; 1935 } else { 1936 KASSERT(tde->td_node != NULL); 1937 /* We ruled out `.' earlier. */ 1938 KASSERT(tde->td_node != dnode); 1939 /* We ruled out `..' earlier. */ 1940 KASSERT(tde->td_node != dnode->tn_spec.tn_dir.tn_parent); 1941 if (tde->td_node != fde->td_node) { 1942 rw_enter_write(&tde->td_node->tn_nlock); 1943 error = tmpfs_vnode_get(mount, tde->td_node, &tvp); 1944 if (error) 1945 goto fail2; 1946 KASSERT(tvp->v_mount == mount); 1947 /* Refuse to rename over a mount point. */ 1948 if ((tvp->v_type == VDIR) && 1949 (tvp->v_mountedhere != NULL)) { 1950 error = EBUSY; 1951 goto fail3; 1952 } 1953 } else { 1954 tvp = fvp; 1955 vref(tvp); 1956 } 1957 KASSERT(tvp != NULL); 1958 KASSERT(VOP_ISLOCKED(tvp) == LK_EXCLUSIVE); 1959 } 1960 KASSERT(tvp != dvp); 1961 1962 *fde_ret = fde; 1963 *fvp_ret = fvp; 1964 *tde_ret = tde; 1965 *tvp_ret = tvp; 1966 return 0; 1967 1968 fail3: if (tvp != NULL) { 1969 if (tvp != fvp) 1970 vput(tvp); 1971 else 1972 vrele(tvp); 1973 } 1974 1975 fail2: vput(fvp); 1976 fail1: VOP_UNLOCK(dvp, 0, curproc); 1977 fail0: return error; 1978 } 1979 1980 /* 1981 * Lock and look up with separate source and target directories. 1982 */ 1983 int 1984 tmpfs_rename_enter_separate(struct mount *mount, struct tmpfs_mount *tmpfs, 1985 struct ucred *cred, 1986 struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp, 1987 struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret, 1988 struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp, 1989 struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret) 1990 { 1991 struct tmpfs_node *intermediate_node; 1992 struct tmpfs_dirent *fde, *tde; 1993 struct vnode *fvp, *tvp; 1994 int error; 1995 1996 KASSERT(fdvp != tdvp); 1997 KASSERT(fdnode != tdnode); 1998 1999 #if 0 /* XXX */ 2000 mutex_enter(&tmpfs->tm_rename_lock); 2001 #endif 2002 2003 error = tmpfs_rename_genealogy(fdnode, tdnode, &intermediate_node); 2004 if (error) 2005 goto fail; 2006 2007 /* 2008 * intermediate_node == NULL means fdnode is not an ancestor of 2009 * tdnode. 2010 */ 2011 if (intermediate_node == NULL) 2012 error = tmpfs_rename_lock(mount, cred, ENOTEMPTY, 2013 tdvp, tdnode, tcnp, 1, &tde, &tvp, 2014 fdvp, fdnode, fcnp, 0, &fde, &fvp); 2015 else 2016 error = tmpfs_rename_lock(mount, cred, EINVAL, 2017 fdvp, fdnode, fcnp, 0, &fde, &fvp, 2018 tdvp, tdnode, tcnp, 1, &tde, &tvp); 2019 if (error) 2020 goto fail; 2021 2022 KASSERT(fde != NULL); 2023 KASSERT(fde->td_node != NULL); 2024 2025 /* 2026 * Reject rename("foo/bar", "foo/bar/baz/quux/zot"). 2027 */ 2028 if (fde->td_node == intermediate_node) { 2029 tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp); 2030 return EINVAL; 2031 } 2032 2033 *fde_ret = fde; 2034 *fvp_ret = fvp; 2035 *tde_ret = tde; 2036 *tvp_ret = tvp; 2037 return 0; 2038 2039 fail: 2040 #if 0 /* XXX */ 2041 mutex_exit(&tmpfs->tm_rename_lock); 2042 #endif 2043 return error; 2044 } 2045 2046 /* 2047 * Unlock everything we locked for rename. 2048 * 2049 * fdvp and tdvp must be referenced. 2050 * 2051 * On entry, everything is locked, and fvp and tvp referenced. 2052 * 2053 * On exit, everything is unlocked, and fvp and tvp are released. 2054 */ 2055 void 2056 tmpfs_rename_exit(struct tmpfs_mount *tmpfs, 2057 struct vnode *fdvp, struct vnode *fvp, 2058 struct vnode *tdvp, struct vnode *tvp) 2059 { 2060 2061 KASSERT(tmpfs != NULL); 2062 KASSERT(fdvp != NULL); 2063 KASSERT(fvp != NULL); 2064 KASSERT(fdvp != fvp); 2065 KASSERT(fdvp != tvp); 2066 KASSERT(tdvp != tvp); 2067 KASSERT(tdvp != fvp); 2068 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE); 2069 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE); 2070 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE); 2071 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE)); 2072 2073 if (tvp != NULL) { 2074 if (tvp != fvp) 2075 vput(tvp); 2076 else 2077 vrele(tvp); 2078 } 2079 VOP_UNLOCK(tdvp, 0, curproc); 2080 vput(fvp); 2081 if (fdvp != tdvp) 2082 VOP_UNLOCK(fdvp, 0, curproc); 2083 2084 #if 0 /* XXX */ 2085 if (fdvp != tdvp) 2086 mutex_exit(&tmpfs->tm_rename_lock); 2087 #endif 2088 } 2089 2090 /* 2091 * Lock a directory, but fail if it has been rmdir'd. 2092 * 2093 * vp must be referenced. 2094 */ 2095 int 2096 tmpfs_rename_lock_directory(struct vnode *vp, struct tmpfs_node *node) 2097 { 2098 2099 KASSERT(vp != NULL); 2100 KASSERT(node != NULL); 2101 KASSERT(node->tn_vnode == vp); 2102 KASSERT(node->tn_type == VDIR); 2103 2104 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc); 2105 if (node->tn_spec.tn_dir.tn_parent == NULL) { 2106 VOP_UNLOCK(vp, 0, curproc); 2107 return ENOENT; 2108 } 2109 2110 return 0; 2111 } 2112 2113 /* 2114 * Analyze the genealogy of the source and target nodes. 2115 * 2116 * On success, stores in *intermediate_node_ret either the child of 2117 * fdnode of which tdnode is a descendant, or null if tdnode is not a 2118 * descendant of fdnode at all. 2119 * 2120 * fdnode and tdnode must be unlocked and referenced. The file 2121 * system's rename lock must also be held, to exclude concurrent 2122 * changes to the file system's genealogy other than rmdir. 2123 * 2124 * XXX This causes an extra lock/unlock of tdnode in the case when 2125 * we're just about to lock it again before locking anything else. 2126 * However, changing that requires reorganizing the code to make it 2127 * even more horrifically obscure. 2128 */ 2129 int 2130 tmpfs_rename_genealogy(struct tmpfs_node *fdnode, struct tmpfs_node *tdnode, 2131 struct tmpfs_node **intermediate_node_ret) 2132 { 2133 struct tmpfs_node *node = tdnode, *parent; 2134 int error; 2135 2136 KASSERT(fdnode != NULL); 2137 KASSERT(tdnode != NULL); 2138 KASSERT(fdnode != tdnode); 2139 KASSERT(intermediate_node_ret != NULL); 2140 2141 KASSERT(fdnode->tn_vnode != NULL); 2142 KASSERT(tdnode->tn_vnode != NULL); 2143 KASSERT(fdnode->tn_type == VDIR); 2144 KASSERT(tdnode->tn_type == VDIR); 2145 2146 /* 2147 * We need to provisionally lock tdnode->tn_vnode to keep rmdir 2148 * from deleting it -- or any ancestor -- at an inopportune 2149 * moment. 2150 */ 2151 error = tmpfs_rename_lock_directory(tdnode->tn_vnode, tdnode); 2152 if (error) 2153 return error; 2154 2155 for (;;) { 2156 parent = node->tn_spec.tn_dir.tn_parent; 2157 KASSERT(parent != NULL); 2158 KASSERT(parent->tn_type == VDIR); 2159 2160 /* Did we hit the root without finding fdnode? */ 2161 if (parent == node) { 2162 *intermediate_node_ret = NULL; 2163 break; 2164 } 2165 2166 /* Did we find that fdnode is an ancestor? */ 2167 if (parent == fdnode) { 2168 *intermediate_node_ret = node; 2169 break; 2170 } 2171 2172 /* Neither -- keep ascending the family tree. */ 2173 node = parent; 2174 } 2175 2176 VOP_UNLOCK(tdnode->tn_vnode, 0, curproc); 2177 return 0; 2178 } 2179 2180 /* 2181 * Lock directories a and b, which must be distinct, and look up and 2182 * lock nodes a and b. Do a first and then b. Directory b may not be 2183 * an ancestor of directory a, although directory a may be an ancestor 2184 * of directory b. Fail with overlap_error if node a is directory b. 2185 * Neither componentname may be `.' or `..'. 2186 * 2187 * a_dvp and b_dvp must be referenced. 2188 * 2189 * On entry, a_dvp and b_dvp are unlocked. 2190 * 2191 * On success, 2192 * . a_dvp and b_dvp are locked, 2193 * . *a_dirent_ret is filled with a directory entry whose node is 2194 * locked and referenced, 2195 * . *b_vp_ret is filled with the corresponding vnode, 2196 * . *b_dirent_ret is filled either with null or with a directory entry 2197 * whose node is locked and referenced, 2198 * . *b_vp is filled either with null or with the corresponding vnode, 2199 * and 2200 * . the only pair of vnodes that may be identical is a_vp and b_vp. 2201 * 2202 * On failure, a_dvp and b_dvp are left unlocked, and *a_dirent_ret, 2203 * *a_vp, *b_dirent_ret, and *b_vp are left alone. 2204 */ 2205 int 2206 tmpfs_rename_lock(struct mount *mount, struct ucred *cred, int overlap_error, 2207 struct vnode *a_dvp, struct tmpfs_node *a_dnode, 2208 struct componentname *a_cnp, int a_missing_ok, 2209 struct tmpfs_dirent **a_dirent_ret, struct vnode **a_vp_ret, 2210 struct vnode *b_dvp, struct tmpfs_node *b_dnode, 2211 struct componentname *b_cnp, int b_missing_ok, 2212 struct tmpfs_dirent **b_dirent_ret, struct vnode **b_vp_ret) 2213 { 2214 struct tmpfs_dirent *a_dirent, *b_dirent; 2215 struct vnode *a_vp, *b_vp; 2216 int error; 2217 2218 KASSERT(a_dvp != NULL); 2219 KASSERT(a_dnode != NULL); 2220 KASSERT(a_cnp != NULL); 2221 KASSERT(a_dirent_ret != NULL); 2222 KASSERT(a_vp_ret != NULL); 2223 KASSERT(b_dvp != NULL); 2224 KASSERT(b_dnode != NULL); 2225 KASSERT(b_cnp != NULL); 2226 KASSERT(b_dirent_ret != NULL); 2227 KASSERT(b_vp_ret != NULL); 2228 KASSERT(a_dvp != b_dvp); 2229 KASSERT(a_dnode != b_dnode); 2230 KASSERT(a_dnode->tn_vnode == a_dvp); 2231 KASSERT(b_dnode->tn_vnode == b_dvp); 2232 KASSERT(a_dnode->tn_type == VDIR); 2233 KASSERT(b_dnode->tn_type == VDIR); 2234 KASSERT(a_missing_ok != b_missing_ok); 2235 2236 error = tmpfs_rename_lock_directory(a_dvp, a_dnode); 2237 if (error) 2238 goto fail0; 2239 2240 /* Did we lose a race with mount? */ 2241 if (a_dvp->v_mountedhere != NULL) { 2242 error = EBUSY; 2243 goto fail1; 2244 } 2245 2246 /* Make sure the caller may read the directory. */ 2247 error = VOP_ACCESS(a_dvp, VEXEC, cred, curproc); 2248 if (error) 2249 goto fail1; 2250 2251 a_dirent = tmpfs_dir_lookup(a_dnode, a_cnp); 2252 if (a_dirent != NULL) { 2253 KASSERT(a_dirent->td_node != NULL); 2254 /* We ruled out `.' earlier. */ 2255 KASSERT(a_dirent->td_node != a_dnode); 2256 /* We ruled out `..' earlier. */ 2257 KASSERT(a_dirent->td_node != 2258 a_dnode->tn_spec.tn_dir.tn_parent); 2259 if (a_dirent->td_node == b_dnode) { 2260 error = overlap_error; 2261 goto fail1; 2262 } 2263 rw_enter_write(&a_dirent->td_node->tn_nlock); 2264 error = tmpfs_vnode_get(mount, a_dirent->td_node, &a_vp); 2265 if (error) 2266 goto fail1; 2267 KASSERT(a_vp->v_mount == mount); 2268 /* Refuse to rename (over) a mount point. */ 2269 if ((a_vp->v_type == VDIR) && (a_vp->v_mountedhere != NULL)) { 2270 error = EBUSY; 2271 goto fail2; 2272 } 2273 } else if (!a_missing_ok) { 2274 error = ENOENT; 2275 goto fail1; 2276 } else { 2277 a_vp = NULL; 2278 } 2279 KASSERT(a_vp != a_dvp); 2280 KASSERT(a_vp != b_dvp); 2281 2282 error = tmpfs_rename_lock_directory(b_dvp, b_dnode); 2283 if (error) 2284 goto fail2; 2285 2286 /* Did we lose a race with mount? */ 2287 if (b_dvp->v_mountedhere != NULL) { 2288 error = EBUSY; 2289 goto fail3; 2290 } 2291 2292 /* Make sure the caller may read the directory. */ 2293 error = VOP_ACCESS(b_dvp, VEXEC, cred, curproc); 2294 if (error) 2295 goto fail3; 2296 2297 b_dirent = tmpfs_dir_lookup(b_dnode, b_cnp); 2298 if (b_dirent != NULL) { 2299 KASSERT(b_dirent->td_node != NULL); 2300 /* We ruled out `.' earlier. */ 2301 KASSERT(b_dirent->td_node != b_dnode); 2302 /* We ruled out `..' earlier. */ 2303 KASSERT(b_dirent->td_node != 2304 b_dnode->tn_spec.tn_dir.tn_parent); 2305 /* b is not an ancestor of a. */ 2306 KASSERT(b_dirent->td_node != a_dnode); 2307 /* But the source and target nodes might be the same. */ 2308 if ((a_dirent == NULL) || 2309 (a_dirent->td_node != b_dirent->td_node)) { 2310 rw_enter_write(&b_dirent->td_node->tn_nlock); 2311 error = tmpfs_vnode_get(mount, b_dirent->td_node, 2312 &b_vp); 2313 if (error) 2314 goto fail3; 2315 KASSERT(b_vp->v_mount == mount); 2316 KASSERT(a_vp != b_vp); 2317 /* Refuse to rename (over) a mount point. */ 2318 if ((b_vp->v_type == VDIR) && 2319 (b_vp->v_mountedhere != NULL)) { 2320 error = EBUSY; 2321 goto fail4; 2322 } 2323 } else { 2324 b_vp = a_vp; 2325 vref(b_vp); 2326 } 2327 } else if (!b_missing_ok) { 2328 error = ENOENT; 2329 goto fail3; 2330 } else { 2331 b_vp = NULL; 2332 } 2333 KASSERT(b_vp != a_dvp); 2334 KASSERT(b_vp != b_dvp); 2335 2336 KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE); 2337 KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE); 2338 KASSERT(a_missing_ok || (a_dirent != NULL)); 2339 KASSERT(a_missing_ok || (a_dirent->td_node != NULL)); 2340 KASSERT(b_missing_ok || (b_dirent != NULL)); 2341 KASSERT(b_missing_ok || (b_dirent->td_node != NULL)); 2342 KASSERT((a_dirent == NULL) || (a_dirent->td_node != NULL)); 2343 KASSERT((a_dirent == NULL) || (a_dirent->td_node->tn_vnode == a_vp)); 2344 KASSERT((b_dirent == NULL) || (b_dirent->td_node != NULL)); 2345 KASSERT((b_dirent == NULL) || (b_dirent->td_node->tn_vnode == b_vp)); 2346 KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE)); 2347 KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE)); 2348 2349 *a_dirent_ret = a_dirent; 2350 *b_dirent_ret = b_dirent; 2351 *a_vp_ret = a_vp; 2352 *b_vp_ret = b_vp; 2353 return 0; 2354 2355 fail4: if (b_vp != NULL) { 2356 KASSERT(VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE); 2357 if (b_vp != a_vp) 2358 vput(b_vp); 2359 else 2360 vrele(a_vp); 2361 } 2362 2363 fail3: KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE); 2364 VOP_UNLOCK(b_dvp, 0, curproc); 2365 2366 fail2: if (a_vp != NULL) { 2367 KASSERT(VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE); 2368 vput(a_vp); 2369 } 2370 2371 fail1: KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE); 2372 VOP_UNLOCK(a_dvp, 0, curproc); 2373 2374 fail0: /* KASSERT(VOP_ISLOCKED(a_dvp) != LK_EXCLUSIVE); */ 2375 /* KASSERT(VOP_ISLOCKED(b_dvp) != LK_EXCLUSIVE); */ 2376 /* KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) != LK_EXCLUSIVE)); */ 2377 /* KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) != LK_EXCLUSIVE)); */ 2378 return error; 2379 } 2380 2381 /* 2382 * Shuffle the directory entries to move fvp from the directory fdvp 2383 * into the directory tdvp. fde is fvp's directory entry in fdvp. If 2384 * we are overwriting a target node, it is tvp, and tde is its 2385 * directory entry in tdvp. 2386 * 2387 * fdvp, fvp, tdvp, and tvp must all be locked and referenced. 2388 */ 2389 void 2390 tmpfs_rename_attachdetach(struct tmpfs_mount *tmpfs, 2391 struct vnode *fdvp, struct tmpfs_dirent *fde, struct vnode *fvp, 2392 struct vnode *tdvp, struct tmpfs_dirent *tde, struct vnode *tvp) 2393 { 2394 2395 KASSERT(tmpfs != NULL); 2396 KASSERT(fdvp != NULL); 2397 KASSERT(fde != NULL); 2398 KASSERT(fvp != NULL); 2399 KASSERT(tdvp != NULL); 2400 KASSERT(fde->td_node != NULL); 2401 KASSERT(fde->td_node->tn_vnode == fvp); 2402 KASSERT((tde == NULL) == (tvp == NULL)); 2403 KASSERT((tde == NULL) || (tde->td_node != NULL)); 2404 KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp)); 2405 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE); 2406 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE); 2407 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE); 2408 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE)); 2409 2410 /* 2411 * If we are moving from one directory to another, detach the 2412 * source entry and reattach it to the target directory. 2413 */ 2414 if (fdvp != tdvp) { 2415 /* tmpfs_dir_detach clobbers fde->td_node, so save it. */ 2416 struct tmpfs_node *fnode = fde->td_node; 2417 tmpfs_dir_detach(fdvp, fde); 2418 tmpfs_dir_attach(tdvp, fde, fnode); 2419 } else if (tvp == NULL) { 2420 /* 2421 * We are changing the directory. tmpfs_dir_attach and 2422 * tmpfs_dir_detach note the events for us, but for 2423 * this case we don't call them, so we must note the 2424 * event explicitly. 2425 */ 2426 VN_KNOTE(fdvp, NOTE_WRITE); 2427 } 2428 2429 /* 2430 * If we are replacing an existing target entry, delete it. 2431 */ 2432 if (tde != NULL) { 2433 KASSERT(tvp != NULL); 2434 KASSERT(tde->td_node != NULL); 2435 KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR)); 2436 if (tde->td_node->tn_type == VDIR) { 2437 KASSERT(tde->td_node->tn_size == 0); 2438 KASSERT(tde->td_node->tn_links == 2); 2439 /* Decrement the extra link count for `.' so 2440 * the vnode will be recycled when released. */ 2441 tde->td_node->tn_links--; 2442 } 2443 tmpfs_dir_detach(tdvp, tde); 2444 tmpfs_free_dirent(tmpfs, tde); 2445 } 2446 } 2447 2448 /* 2449 * Remove the entry de for the non-directory vp from the directory dvp. 2450 * 2451 * Everything must be locked and referenced. 2452 */ 2453 int 2454 tmpfs_do_remove(struct tmpfs_mount *tmpfs, struct vnode *dvp, 2455 struct tmpfs_node *dnode, struct tmpfs_dirent *de, struct vnode *vp, 2456 struct ucred *cred) 2457 { 2458 int error; 2459 2460 KASSERT(tmpfs != NULL); 2461 KASSERT(dvp != NULL); 2462 KASSERT(dnode != NULL); 2463 KASSERT(de != NULL); 2464 KASSERT(vp != NULL); 2465 KASSERT(dnode->tn_vnode == dvp); 2466 KASSERT(de->td_node != NULL); 2467 KASSERT(de->td_node->tn_vnode == vp); 2468 KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE); 2469 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 2470 2471 error = tmpfs_remove_check_possible(dnode, de->td_node); 2472 if (error) 2473 return error; 2474 2475 error = tmpfs_remove_check_permitted(cred, dnode, de->td_node); 2476 if (error) 2477 return error; 2478 2479 /* 2480 * If not root and directory is sticky, check for permission on 2481 * directory or on file. This implements append-only directories. 2482 */ 2483 if ((dnode->tn_mode & S_ISTXT) != 0) 2484 if (cred->cr_uid != 0 && cred->cr_uid != dnode->tn_uid && 2485 cred->cr_uid != de->td_node->tn_uid) 2486 return EPERM; 2487 2488 tmpfs_dir_detach(dvp, de); 2489 tmpfs_free_dirent(tmpfs, de); 2490 2491 return 0; 2492 } 2493 2494 /* 2495 * Check whether a rename is possible independent of credentials. 2496 * 2497 * Everything must be locked and referenced. 2498 */ 2499 int 2500 tmpfs_rename_check_possible( 2501 struct tmpfs_node *fdnode, struct tmpfs_node *fnode, 2502 struct tmpfs_node *tdnode, struct tmpfs_node *tnode) 2503 { 2504 2505 KASSERT(fdnode != NULL); 2506 KASSERT(fnode != NULL); 2507 KASSERT(tdnode != NULL); 2508 KASSERT(fdnode != fnode); 2509 KASSERT(tdnode != tnode); 2510 KASSERT(fnode != tnode); 2511 KASSERT(fdnode->tn_vnode != NULL); 2512 KASSERT(fnode->tn_vnode != NULL); 2513 KASSERT(tdnode->tn_vnode != NULL); 2514 KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL)); 2515 KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE); 2516 KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE); 2517 KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE); 2518 KASSERT((tnode == NULL) || 2519 (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE)); 2520 2521 /* 2522 * If fdnode is immutable, we can't write to it. If fdnode is 2523 * append-only, the only change we can make is to add entries 2524 * to it. If fnode is immutable, we can't change the links to 2525 * it. If fnode is append-only...well, this is what UFS does. 2526 */ 2527 if ((fdnode->tn_flags | fnode->tn_flags) & (IMMUTABLE | APPEND)) 2528 return EPERM; 2529 2530 /* 2531 * If tdnode is immutable, we can't write to it. If tdnode is 2532 * append-only, we can add entries, but we can't change 2533 * existing entries. 2534 */ 2535 if (tdnode->tn_flags & (IMMUTABLE | (tnode? APPEND : 0))) 2536 return EPERM; 2537 2538 /* 2539 * If tnode is immutable, we can't replace links to it. If 2540 * tnode is append-only...well, this is what UFS does. 2541 */ 2542 if (tnode != NULL) { 2543 KASSERT(tnode != NULL); 2544 if ((tnode->tn_flags & (IMMUTABLE | APPEND)) != 0) 2545 return EPERM; 2546 } 2547 2548 return 0; 2549 } 2550 2551 /* 2552 * Check whether a rename is permitted given our credentials. 2553 * 2554 * Everything must be locked and referenced. 2555 */ 2556 int 2557 tmpfs_rename_check_permitted(struct ucred *cred, 2558 struct tmpfs_node *fdnode, struct tmpfs_node *fnode, 2559 struct tmpfs_node *tdnode, struct tmpfs_node *tnode) 2560 { 2561 int error; 2562 2563 KASSERT(fdnode != NULL); 2564 KASSERT(fnode != NULL); 2565 KASSERT(tdnode != NULL); 2566 KASSERT(fdnode != fnode); 2567 KASSERT(tdnode != tnode); 2568 KASSERT(fnode != tnode); 2569 KASSERT(fdnode->tn_vnode != NULL); 2570 KASSERT(fnode->tn_vnode != NULL); 2571 KASSERT(tdnode->tn_vnode != NULL); 2572 KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL)); 2573 KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE); 2574 KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE); 2575 KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE); 2576 KASSERT((tnode == NULL) || 2577 (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE)); 2578 2579 /* 2580 * We need to remove or change an entry in the source directory. 2581 */ 2582 error = VOP_ACCESS(fdnode->tn_vnode, VWRITE, cred, curproc); 2583 if (error) 2584 return error; 2585 2586 /* 2587 * If we are changing directories, then we need to write to the 2588 * target directory to add or change an entry. Also, if fnode 2589 * is a directory, we need to write to it to change its `..' 2590 * entry. 2591 */ 2592 if (fdnode != tdnode) { 2593 error = VOP_ACCESS(tdnode->tn_vnode, VWRITE, cred, curproc); 2594 if (error) 2595 return error; 2596 if (fnode->tn_type == VDIR) { 2597 error = VOP_ACCESS(fnode->tn_vnode, VWRITE, cred, 2598 curproc); 2599 if (error) 2600 return error; 2601 } 2602 } 2603 2604 error = tmpfs_check_sticky(cred, fdnode, fnode); 2605 if (error) 2606 return error; 2607 2608 error = tmpfs_check_sticky(cred, tdnode, tnode); 2609 if (error) 2610 return error; 2611 2612 return 0; 2613 } 2614 2615 /* 2616 * Check whether removing node's entry in dnode is possible independent 2617 * of credentials. 2618 * 2619 * Everything must be locked and referenced. 2620 */ 2621 int 2622 tmpfs_remove_check_possible(struct tmpfs_node *dnode, struct tmpfs_node *node) 2623 { 2624 2625 KASSERT(dnode != NULL); 2626 KASSERT(dnode->tn_vnode != NULL); 2627 KASSERT(node != NULL); 2628 KASSERT(dnode != node); 2629 KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE); 2630 KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE); 2631 2632 /* 2633 * We want to delete the entry. If dnode is immutable, we 2634 * can't write to it to delete the entry. If dnode is 2635 * append-only, the only change we can make is to add entries, 2636 * so we can't delete entries. If node is immutable, we can't 2637 * change the links to it, so we can't delete the entry. If 2638 * node is append-only...well, this is what UFS does. 2639 */ 2640 if ((dnode->tn_flags | node->tn_flags) & (IMMUTABLE | APPEND)) 2641 return EPERM; 2642 2643 return 0; 2644 } 2645 2646 /* 2647 * Check whether removing node's entry in dnode is permitted given our 2648 * credentials. 2649 * 2650 * Everything must be locked and referenced. 2651 */ 2652 int 2653 tmpfs_remove_check_permitted(struct ucred *cred, 2654 struct tmpfs_node *dnode, struct tmpfs_node *node) 2655 { 2656 int error; 2657 2658 KASSERT(dnode != NULL); 2659 KASSERT(dnode->tn_vnode != NULL); 2660 KASSERT(node != NULL); 2661 KASSERT(dnode != node); 2662 KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE); 2663 KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE); 2664 2665 /* 2666 * Check whether we are permitted to write to the source 2667 * directory in order to delete an entry from it. 2668 */ 2669 error = VOP_ACCESS(dnode->tn_vnode, VWRITE, cred, curproc); 2670 if (error) 2671 return error; 2672 2673 error = tmpfs_check_sticky(cred, dnode, node); 2674 if (error) 2675 return error; 2676 2677 return 0; 2678 } 2679 2680 /* 2681 * Check whether we may change an entry in a sticky directory. If the 2682 * directory is sticky, the user must own either the directory or, if 2683 * it exists, the node, in order to change the entry. 2684 * 2685 * Everything must be locked and referenced. 2686 */ 2687 int 2688 tmpfs_check_sticky(struct ucred *cred, 2689 struct tmpfs_node *dnode, struct tmpfs_node *node) 2690 { 2691 2692 KASSERT(dnode != NULL); 2693 KASSERT(dnode->tn_vnode != NULL); 2694 KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE); 2695 KASSERT((node == NULL) || (node->tn_vnode != NULL)); 2696 KASSERT((node == NULL) || 2697 (VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE)); 2698 2699 if (node == NULL) 2700 return 0; 2701 2702 if (dnode->tn_mode & S_ISTXT) { 2703 if (cred->cr_uid != 0 && 2704 cred->cr_uid != dnode->tn_uid && 2705 cred->cr_uid != node->tn_uid) 2706 return EPERM; 2707 } 2708 2709 return 0; 2710 } 2711 2712 void 2713 tmpfs_rename_cache_purge(struct vnode *fdvp, struct vnode *fvp, 2714 struct vnode *tdvp, struct vnode *tvp) 2715 { 2716 2717 KASSERT(fdvp != NULL); 2718 KASSERT(fvp != NULL); 2719 KASSERT(tdvp != NULL); 2720 KASSERT(fdvp != fvp); 2721 KASSERT(fdvp != tvp); 2722 KASSERT(tdvp != fvp); 2723 KASSERT(tdvp != tvp); 2724 KASSERT(fvp != tvp); 2725 KASSERT(fdvp->v_type == VDIR); 2726 KASSERT(tdvp->v_type == VDIR); 2727 2728 /* 2729 * XXX What actually needs to be purged? 2730 */ 2731 2732 cache_purge(fdvp); 2733 2734 if (fvp->v_type == VDIR) 2735 cache_purge(fvp); 2736 2737 if (tdvp != fdvp) 2738 cache_purge(tdvp); 2739 2740 if ((tvp != NULL) && (tvp->v_type == VDIR)) 2741 cache_purge(tvp); 2742 } 2743