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