1 /* $NetBSD: tmpfs_vnops.c,v 1.106 2013/11/08 15:44:23 rmind 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.106 2013/11/08 15:44:23 rmind 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 cachefound, iswhiteout; 137 int error; 138 139 KASSERT(VOP_ISLOCKED(dvp)); 140 141 dnode = VP_TO_TMPFS_DIR(dvp); 142 *vpp = NULL; 143 144 /* Check accessibility of directory. */ 145 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred); 146 if (error) { 147 goto out; 148 } 149 150 /* 151 * If requesting the last path component on a read-only file system 152 * with a write operation, deny it. 153 */ 154 if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 && 155 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 156 error = EROFS; 157 goto out; 158 } 159 160 /* 161 * Avoid doing a linear scan of the directory if the requested 162 * directory/name couple is already in the cache. 163 */ 164 cachefound = cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen, 165 cnp->cn_nameiop, cnp->cn_flags, 166 &iswhiteout, vpp); 167 if (iswhiteout) { 168 cnp->cn_flags |= ISWHITEOUT; 169 } 170 if (cachefound && *vpp == NULLVP) { 171 /* Negative cache hit. */ 172 error = ENOENT; 173 goto out; 174 } else if (cachefound) { 175 error = 0; 176 goto out; 177 } 178 179 if (cnp->cn_flags & ISDOTDOT) { 180 tmpfs_node_t *pnode; 181 182 /* 183 * Lookup of ".." case. 184 */ 185 if (lastcn && cnp->cn_nameiop == RENAME) { 186 error = EINVAL; 187 goto out; 188 } 189 KASSERT(dnode->tn_type == VDIR); 190 pnode = dnode->tn_spec.tn_dir.tn_parent; 191 if (pnode == NULL) { 192 error = ENOENT; 193 goto out; 194 } 195 196 /* 197 * Lock the parent tn_vlock before releasing the vnode lock, 198 * and thus prevents parent from disappearing. 199 */ 200 mutex_enter(&pnode->tn_vlock); 201 VOP_UNLOCK(dvp); 202 203 /* 204 * Get a vnode of the '..' entry and re-acquire the lock. 205 * Release the tn_vlock. 206 */ 207 error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp); 208 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 209 goto out; 210 211 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 212 /* 213 * Lookup of "." case. 214 */ 215 if (lastcn && cnp->cn_nameiop == RENAME) { 216 error = EISDIR; 217 goto out; 218 } 219 vref(dvp); 220 *vpp = dvp; 221 error = 0; 222 goto done; 223 } 224 225 /* 226 * Other lookup cases: perform directory scan. 227 */ 228 de = tmpfs_dir_lookup(dnode, cnp); 229 if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) { 230 /* 231 * The entry was not found in the directory. This is valid 232 * if we are creating or renaming an entry and are working 233 * on the last component of the path name. 234 */ 235 if (lastcn && (cnp->cn_nameiop == CREATE || 236 cnp->cn_nameiop == RENAME)) { 237 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred); 238 if (error) { 239 goto out; 240 } 241 error = EJUSTRETURN; 242 } else { 243 error = ENOENT; 244 } 245 if (de) { 246 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT); 247 cnp->cn_flags |= ISWHITEOUT; 248 } 249 goto done; 250 } 251 252 tnode = de->td_node; 253 254 /* 255 * If it is not the last path component and found a non-directory 256 * or non-link entry (which may itself be pointing to a directory), 257 * raise an error. 258 */ 259 if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) { 260 error = ENOTDIR; 261 goto out; 262 } 263 264 /* Check the permissions. */ 265 if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 266 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred); 267 if (error) 268 goto out; 269 270 if ((dnode->tn_mode & S_ISTXT) != 0) { 271 error = kauth_authorize_vnode(cnp->cn_cred, 272 KAUTH_VNODE_DELETE, tnode->tn_vnode, 273 dnode->tn_vnode, genfs_can_sticky(cnp->cn_cred, 274 dnode->tn_uid, tnode->tn_uid)); 275 if (error) { 276 error = EPERM; 277 goto out; 278 } 279 } 280 } 281 282 /* Get a vnode for the matching entry. */ 283 mutex_enter(&tnode->tn_vlock); 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_nameiop != CREATE) { 291 cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, 292 cnp->cn_flags); 293 } 294 out: 295 KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error); 296 KASSERT(VOP_ISLOCKED(dvp)); 297 298 return error; 299 } 300 301 int 302 tmpfs_create(void *v) 303 { 304 struct vop_create_args /* { 305 struct vnode *a_dvp; 306 struct vnode **a_vpp; 307 struct componentname *a_cnp; 308 struct vattr *a_vap; 309 } */ *ap = v; 310 vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp; 311 struct componentname *cnp = ap->a_cnp; 312 struct vattr *vap = ap->a_vap; 313 314 KASSERT(VOP_ISLOCKED(dvp)); 315 KASSERT(vap->va_type == VREG || vap->va_type == VSOCK); 316 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 317 } 318 319 int 320 tmpfs_mknod(void *v) 321 { 322 struct vop_mknod_args /* { 323 struct vnode *a_dvp; 324 struct vnode **a_vpp; 325 struct componentname *a_cnp; 326 struct vattr *a_vap; 327 } */ *ap = v; 328 vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp; 329 struct componentname *cnp = ap->a_cnp; 330 struct vattr *vap = ap->a_vap; 331 enum vtype vt = vap->va_type; 332 333 if (vt != VBLK && vt != VCHR && vt != VFIFO) { 334 vput(dvp); 335 return EINVAL; 336 } 337 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 338 } 339 340 int 341 tmpfs_open(void *v) 342 { 343 struct vop_open_args /* { 344 struct vnode *a_vp; 345 int a_mode; 346 kauth_cred_t a_cred; 347 } */ *ap = v; 348 vnode_t *vp = ap->a_vp; 349 mode_t mode = ap->a_mode; 350 tmpfs_node_t *node; 351 352 KASSERT(VOP_ISLOCKED(vp)); 353 354 node = VP_TO_TMPFS_NODE(vp); 355 if (node->tn_links < 1) { 356 /* 357 * The file is still active, but all its names have been 358 * removed (e.g. by a "rmdir $(pwd)"). It cannot be opened 359 * any more, as it is about to be destroyed. 360 */ 361 return ENOENT; 362 } 363 364 /* If the file is marked append-only, deny write requests. */ 365 if ((node->tn_flags & APPEND) != 0 && 366 (mode & (FWRITE | O_APPEND)) == FWRITE) { 367 return EPERM; 368 } 369 return 0; 370 } 371 372 int 373 tmpfs_close(void *v) 374 { 375 struct vop_close_args /* { 376 struct vnode *a_vp; 377 int a_fflag; 378 kauth_cred_t a_cred; 379 } */ *ap = v; 380 vnode_t *vp = ap->a_vp; 381 382 KASSERT(VOP_ISLOCKED(vp)); 383 384 tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE); 385 return 0; 386 } 387 388 int 389 tmpfs_access(void *v) 390 { 391 struct vop_access_args /* { 392 struct vnode *a_vp; 393 int a_mode; 394 kauth_cred_t a_cred; 395 } */ *ap = v; 396 vnode_t *vp = ap->a_vp; 397 mode_t mode = ap->a_mode; 398 kauth_cred_t cred = ap->a_cred; 399 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 400 const bool writing = (mode & VWRITE) != 0; 401 402 KASSERT(VOP_ISLOCKED(vp)); 403 404 /* Possible? */ 405 switch (vp->v_type) { 406 case VDIR: 407 case VLNK: 408 case VREG: 409 if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) { 410 return EROFS; 411 } 412 break; 413 case VBLK: 414 case VCHR: 415 case VSOCK: 416 case VFIFO: 417 break; 418 default: 419 return EINVAL; 420 } 421 if (writing && (node->tn_flags & IMMUTABLE) != 0) { 422 return EPERM; 423 } 424 425 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, 426 vp->v_type, node->tn_mode), vp, NULL, genfs_can_access(vp->v_type, 427 node->tn_mode, node->tn_uid, node->tn_gid, mode, cred)); 428 } 429 430 int 431 tmpfs_getattr(void *v) 432 { 433 struct vop_getattr_args /* { 434 struct vnode *a_vp; 435 struct vattr *a_vap; 436 kauth_cred_t a_cred; 437 } */ *ap = v; 438 vnode_t *vp = ap->a_vp; 439 struct vattr *vap = ap->a_vap; 440 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 441 442 vattr_null(vap); 443 444 tmpfs_update(vp, NULL, NULL, NULL, 0); 445 446 vap->va_type = vp->v_type; 447 vap->va_mode = node->tn_mode; 448 vap->va_nlink = node->tn_links; 449 vap->va_uid = node->tn_uid; 450 vap->va_gid = node->tn_gid; 451 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 452 vap->va_fileid = node->tn_id; 453 vap->va_size = node->tn_size; 454 vap->va_blocksize = PAGE_SIZE; 455 vap->va_atime = node->tn_atime; 456 vap->va_mtime = node->tn_mtime; 457 vap->va_ctime = node->tn_ctime; 458 vap->va_birthtime = node->tn_birthtime; 459 vap->va_gen = TMPFS_NODE_GEN(node); 460 vap->va_flags = node->tn_flags; 461 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 462 node->tn_spec.tn_dev.tn_rdev : VNOVAL; 463 vap->va_bytes = round_page(node->tn_size); 464 vap->va_filerev = VNOVAL; 465 vap->va_vaflags = 0; 466 vap->va_spare = VNOVAL; /* XXX */ 467 468 return 0; 469 } 470 471 #define GOODTIME(tv) ((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL) 472 /* XXX Should this operation be atomic? I think it should, but code in 473 * XXX other places (e.g., ufs) doesn't seem to be... */ 474 int 475 tmpfs_setattr(void *v) 476 { 477 struct vop_setattr_args /* { 478 struct vnode *a_vp; 479 struct vattr *a_vap; 480 kauth_cred_t a_cred; 481 } */ *ap = v; 482 vnode_t *vp = ap->a_vp; 483 struct vattr *vap = ap->a_vap; 484 kauth_cred_t cred = ap->a_cred; 485 lwp_t *l = curlwp; 486 int error = 0; 487 488 KASSERT(VOP_ISLOCKED(vp)); 489 490 /* Abort if any unsettable attribute is given. */ 491 if (vap->va_type != VNON || vap->va_nlink != VNOVAL || 492 vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL || 493 vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) || 494 vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL || 495 vap->va_bytes != VNOVAL) { 496 return EINVAL; 497 } 498 if (error == 0 && (vap->va_flags != VNOVAL)) 499 error = tmpfs_chflags(vp, vap->va_flags, cred, l); 500 501 if (error == 0 && (vap->va_size != VNOVAL)) 502 error = tmpfs_chsize(vp, vap->va_size, cred, l); 503 504 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 505 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l); 506 507 if (error == 0 && (vap->va_mode != VNOVAL)) 508 error = tmpfs_chmod(vp, vap->va_mode, cred, l); 509 510 if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime) 511 || GOODTIME(&vap->va_birthtime))) { 512 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 513 &vap->va_birthtime, vap->va_vaflags, cred, l); 514 if (error == 0) 515 return 0; 516 } 517 tmpfs_update(vp, NULL, NULL, NULL, 0); 518 return error; 519 } 520 521 int 522 tmpfs_read(void *v) 523 { 524 struct vop_read_args /* { 525 struct vnode *a_vp; 526 struct uio *a_uio; 527 int a_ioflag; 528 kauth_cred_t a_cred; 529 } */ *ap = v; 530 vnode_t *vp = ap->a_vp; 531 struct uio *uio = ap->a_uio; 532 const int ioflag = ap->a_ioflag; 533 tmpfs_node_t *node; 534 struct uvm_object *uobj; 535 int error; 536 537 KASSERT(VOP_ISLOCKED(vp)); 538 539 if (vp->v_type != VREG) { 540 return EISDIR; 541 } 542 if (uio->uio_offset < 0) { 543 return EINVAL; 544 } 545 546 node = VP_TO_TMPFS_NODE(vp); 547 node->tn_status |= TMPFS_NODE_ACCESSED; 548 uobj = node->tn_spec.tn_reg.tn_aobj; 549 error = 0; 550 551 while (error == 0 && uio->uio_resid > 0) { 552 vsize_t len; 553 554 if (node->tn_size <= uio->uio_offset) { 555 break; 556 } 557 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid); 558 if (len == 0) { 559 break; 560 } 561 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag), 562 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); 563 } 564 return error; 565 } 566 567 int 568 tmpfs_write(void *v) 569 { 570 struct vop_write_args /* { 571 struct vnode *a_vp; 572 struct uio *a_uio; 573 int a_ioflag; 574 kauth_cred_t a_cred; 575 } */ *ap = v; 576 vnode_t *vp = ap->a_vp; 577 struct uio *uio = ap->a_uio; 578 const int ioflag = ap->a_ioflag; 579 tmpfs_node_t *node; 580 struct uvm_object *uobj; 581 off_t oldsize; 582 bool extended; 583 int error; 584 585 KASSERT(VOP_ISLOCKED(vp)); 586 587 node = VP_TO_TMPFS_NODE(vp); 588 oldsize = node->tn_size; 589 590 if (uio->uio_offset < 0 || vp->v_type != VREG) { 591 error = EINVAL; 592 goto out; 593 } 594 if (uio->uio_resid == 0) { 595 error = 0; 596 goto out; 597 } 598 if (ioflag & IO_APPEND) { 599 uio->uio_offset = node->tn_size; 600 } 601 602 extended = uio->uio_offset + uio->uio_resid > node->tn_size; 603 if (extended) { 604 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid); 605 if (error) 606 goto out; 607 } 608 609 uobj = node->tn_spec.tn_reg.tn_aobj; 610 error = 0; 611 while (error == 0 && uio->uio_resid > 0) { 612 vsize_t len; 613 614 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid); 615 if (len == 0) { 616 break; 617 } 618 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag), 619 UBC_WRITE | UBC_UNMAP_FLAG(vp)); 620 } 621 if (error) { 622 (void)tmpfs_reg_resize(vp, oldsize); 623 } 624 625 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 626 (extended ? TMPFS_NODE_CHANGED : 0); 627 VN_KNOTE(vp, NOTE_WRITE); 628 out: 629 if (error) { 630 KASSERT(oldsize == node->tn_size); 631 } else { 632 KASSERT(uio->uio_resid == 0); 633 } 634 return error; 635 } 636 637 int 638 tmpfs_fsync(void *v) 639 { 640 struct vop_fsync_args /* { 641 struct vnode *a_vp; 642 kauth_cred_t a_cred; 643 int a_flags; 644 off_t a_offlo; 645 off_t a_offhi; 646 struct lwp *a_l; 647 } */ *ap = v; 648 vnode_t *vp = ap->a_vp; 649 650 /* Nothing to do. Just update. */ 651 KASSERT(VOP_ISLOCKED(vp)); 652 tmpfs_update(vp, NULL, NULL, NULL, 0); 653 return 0; 654 } 655 656 /* 657 * tmpfs_remove: unlink a file. 658 * 659 * => Both directory (dvp) and file (vp) are locked. 660 * => We unlock and drop the reference on both. 661 */ 662 int 663 tmpfs_remove(void *v) 664 { 665 struct vop_remove_args /* { 666 struct vnode *a_dvp; 667 struct vnode *a_vp; 668 struct componentname *a_cnp; 669 } */ *ap = v; 670 vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp; 671 tmpfs_node_t *dnode, *node; 672 tmpfs_dirent_t *de; 673 int error; 674 675 KASSERT(VOP_ISLOCKED(dvp)); 676 KASSERT(VOP_ISLOCKED(vp)); 677 678 if (vp->v_type == VDIR) { 679 error = EPERM; 680 goto out; 681 } 682 dnode = VP_TO_TMPFS_DIR(dvp); 683 node = VP_TO_TMPFS_NODE(vp); 684 685 /* 686 * Files marked as immutable or append-only cannot be deleted. 687 * Likewise, files residing on directories marked as append-only 688 * cannot be deleted. 689 */ 690 if (node->tn_flags & (IMMUTABLE | APPEND)) { 691 error = EPERM; 692 goto out; 693 } 694 if (dnode->tn_flags & APPEND) { 695 error = EPERM; 696 goto out; 697 } 698 699 /* Lookup the directory entry (check the cached hint first). */ 700 de = tmpfs_dir_cached(node); 701 if (de == NULL) { 702 struct componentname *cnp = ap->a_cnp; 703 de = tmpfs_dir_lookup(dnode, cnp); 704 } 705 KASSERT(de && de->td_node == node); 706 707 /* 708 * Remove the entry from the directory (drops the link count) and 709 * destroy it or replace it with a whiteout. 710 * Note: the inode referred by it will not be destroyed 711 * until the vnode is reclaimed/recycled. 712 */ 713 tmpfs_dir_detach(dnode, de); 714 if (ap->a_cnp->cn_flags & DOWHITEOUT) 715 tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT); 716 else 717 tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de); 718 719 if (node->tn_links > 0) { 720 /* We removed a hard link. */ 721 node->tn_status |= TMPFS_NODE_CHANGED; 722 tmpfs_update(vp, NULL, NULL, NULL, 0); 723 } 724 error = 0; 725 out: 726 /* Drop the references and unlock the vnodes. */ 727 vput(vp); 728 if (dvp == vp) { 729 vrele(dvp); 730 } else { 731 vput(dvp); 732 } 733 return error; 734 } 735 736 /* 737 * tmpfs_link: create a hard link. 738 */ 739 int 740 tmpfs_link(void *v) 741 { 742 struct vop_link_args /* { 743 struct vnode *a_dvp; 744 struct vnode *a_vp; 745 struct componentname *a_cnp; 746 } */ *ap = v; 747 vnode_t *dvp = ap->a_dvp; 748 vnode_t *vp = ap->a_vp; 749 struct componentname *cnp = ap->a_cnp; 750 tmpfs_node_t *dnode, *node; 751 tmpfs_dirent_t *de; 752 int error; 753 754 KASSERT(dvp != vp); 755 KASSERT(VOP_ISLOCKED(dvp)); 756 KASSERT(vp->v_type != VDIR); 757 KASSERT(dvp->v_mount == vp->v_mount); 758 759 dnode = VP_TO_TMPFS_DIR(dvp); 760 node = VP_TO_TMPFS_NODE(vp); 761 762 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 763 764 /* Check for maximum number of links limit. */ 765 if (node->tn_links == LINK_MAX) { 766 error = EMLINK; 767 goto out; 768 } 769 KASSERT(node->tn_links < LINK_MAX); 770 771 /* We cannot create links of files marked immutable or append-only. */ 772 if (node->tn_flags & (IMMUTABLE | APPEND)) { 773 error = EPERM; 774 goto out; 775 } 776 777 /* Allocate a new directory entry to represent the inode. */ 778 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), 779 cnp->cn_nameptr, cnp->cn_namelen, &de); 780 if (error) { 781 goto out; 782 } 783 784 /* 785 * Insert the entry into the directory. 786 * It will increase the inode link count. 787 */ 788 tmpfs_dir_attach(dnode, de, node); 789 790 /* Update the timestamps and trigger the event. */ 791 if (node->tn_vnode) { 792 VN_KNOTE(node->tn_vnode, NOTE_LINK); 793 } 794 node->tn_status |= TMPFS_NODE_CHANGED; 795 tmpfs_update(vp, NULL, NULL, NULL, 0); 796 error = 0; 797 out: 798 VOP_UNLOCK(vp); 799 vput(dvp); 800 return error; 801 } 802 803 int 804 tmpfs_mkdir(void *v) 805 { 806 struct vop_mkdir_args /* { 807 struct vnode *a_dvp; 808 struct vnode **a_vpp; 809 struct componentname *a_cnp; 810 struct vattr *a_vap; 811 } */ *ap = v; 812 vnode_t *dvp = ap->a_dvp; 813 vnode_t **vpp = ap->a_vpp; 814 struct componentname *cnp = ap->a_cnp; 815 struct vattr *vap = ap->a_vap; 816 817 KASSERT(vap->va_type == VDIR); 818 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 819 } 820 821 int 822 tmpfs_rmdir(void *v) 823 { 824 struct vop_rmdir_args /* { 825 struct vnode *a_dvp; 826 struct vnode *a_vp; 827 struct componentname *a_cnp; 828 } */ *ap = v; 829 vnode_t *dvp = ap->a_dvp; 830 vnode_t *vp = ap->a_vp; 831 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount); 832 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp); 833 tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp); 834 tmpfs_dirent_t *de; 835 int error = 0; 836 837 KASSERT(VOP_ISLOCKED(dvp)); 838 KASSERT(VOP_ISLOCKED(vp)); 839 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode); 840 841 /* 842 * Directories with more than two non-whiteout 843 * entries ('.' and '..') cannot be removed. 844 */ 845 if (node->tn_size > 0) { 846 KASSERT(error == 0); 847 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) { 848 if (de->td_node != TMPFS_NODE_WHITEOUT) { 849 error = ENOTEMPTY; 850 break; 851 } 852 } 853 if (error) 854 goto out; 855 } 856 857 /* Lookup the directory entry (check the cached hint first). */ 858 de = tmpfs_dir_cached(node); 859 if (de == NULL) { 860 struct componentname *cnp = ap->a_cnp; 861 de = tmpfs_dir_lookup(dnode, cnp); 862 } 863 KASSERT(de && de->td_node == node); 864 865 /* Check flags to see if we are allowed to remove the directory. */ 866 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) { 867 error = EPERM; 868 goto out; 869 } 870 871 /* Decrement the link count for the virtual '.' entry. */ 872 node->tn_links--; 873 node->tn_status |= TMPFS_NODE_STATUSALL; 874 875 /* Detach the directory entry from the directory. */ 876 tmpfs_dir_detach(dnode, de); 877 878 /* Purge the cache for parent. */ 879 cache_purge(dvp); 880 881 /* 882 * Destroy the directory entry or replace it with a whiteout. 883 * Note: the inode referred by it will not be destroyed 884 * until the vnode is reclaimed. 885 */ 886 if (ap->a_cnp->cn_flags & DOWHITEOUT) 887 tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT); 888 else 889 tmpfs_free_dirent(tmp, de); 890 891 /* Destroy the whiteout entries from the node. */ 892 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) { 893 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT); 894 tmpfs_dir_detach(node, de); 895 tmpfs_free_dirent(tmp, de); 896 } 897 898 KASSERT(node->tn_links == 0); 899 out: 900 /* Release the nodes. */ 901 vput(dvp); 902 vput(vp); 903 return error; 904 } 905 906 int 907 tmpfs_symlink(void *v) 908 { 909 struct vop_symlink_args /* { 910 struct vnode *a_dvp; 911 struct vnode **a_vpp; 912 struct componentname *a_cnp; 913 struct vattr *a_vap; 914 char *a_target; 915 } */ *ap = v; 916 vnode_t *dvp = ap->a_dvp; 917 vnode_t **vpp = ap->a_vpp; 918 struct componentname *cnp = ap->a_cnp; 919 struct vattr *vap = ap->a_vap; 920 char *target = ap->a_target; 921 922 KASSERT(vap->va_type == VLNK); 923 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 924 } 925 926 int 927 tmpfs_readdir(void *v) 928 { 929 struct vop_readdir_args /* { 930 struct vnode *a_vp; 931 struct uio *a_uio; 932 kauth_cred_t a_cred; 933 int *a_eofflag; 934 off_t **a_cookies; 935 int *ncookies; 936 } */ *ap = v; 937 vnode_t *vp = ap->a_vp; 938 struct uio *uio = ap->a_uio; 939 int *eofflag = ap->a_eofflag; 940 off_t **cookies = ap->a_cookies; 941 int *ncookies = ap->a_ncookies; 942 off_t startoff, cnt; 943 tmpfs_node_t *node; 944 int error; 945 946 KASSERT(VOP_ISLOCKED(vp)); 947 948 /* This operation only makes sense on directory nodes. */ 949 if (vp->v_type != VDIR) { 950 return ENOTDIR; 951 } 952 node = VP_TO_TMPFS_DIR(vp); 953 startoff = uio->uio_offset; 954 cnt = 0; 955 956 /* 957 * Retrieve the directory entries, unless it is being destroyed. 958 */ 959 if (node->tn_links) { 960 error = tmpfs_dir_getdents(node, uio, &cnt); 961 } else { 962 error = 0; 963 } 964 965 if (eofflag != NULL) { 966 *eofflag = !error && uio->uio_offset == TMPFS_DIRSEQ_EOF; 967 } 968 if (error || cookies == NULL || ncookies == NULL) { 969 return error; 970 } 971 972 /* Update NFS-related variables, if any. */ 973 tmpfs_dirent_t *de = NULL; 974 off_t i, off = startoff; 975 976 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 977 *ncookies = cnt; 978 979 for (i = 0; i < cnt; i++) { 980 KASSERT(off != TMPFS_DIRSEQ_EOF); 981 if (off != TMPFS_DIRSEQ_DOT) { 982 if (off == TMPFS_DIRSEQ_DOTDOT) { 983 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir); 984 } else if (de != NULL) { 985 de = TAILQ_NEXT(de, td_entries); 986 } else { 987 de = tmpfs_dir_lookupbyseq(node, off); 988 KASSERT(de != NULL); 989 de = TAILQ_NEXT(de, td_entries); 990 } 991 if (de == NULL) { 992 off = TMPFS_DIRSEQ_EOF; 993 } else { 994 off = tmpfs_dir_getseq(node, de); 995 } 996 } else { 997 off = TMPFS_DIRSEQ_DOTDOT; 998 } 999 (*cookies)[i] = off; 1000 } 1001 KASSERT(uio->uio_offset == off); 1002 return error; 1003 } 1004 1005 int 1006 tmpfs_readlink(void *v) 1007 { 1008 struct vop_readlink_args /* { 1009 struct vnode *a_vp; 1010 struct uio *a_uio; 1011 kauth_cred_t a_cred; 1012 } */ *ap = v; 1013 vnode_t *vp = ap->a_vp; 1014 struct uio *uio = ap->a_uio; 1015 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1016 int error; 1017 1018 KASSERT(VOP_ISLOCKED(vp)); 1019 KASSERT(uio->uio_offset == 0); 1020 KASSERT(vp->v_type == VLNK); 1021 1022 /* Note: readlink(2) returns the path without NUL terminator. */ 1023 if (node->tn_size > 0) { 1024 error = uiomove(node->tn_spec.tn_lnk.tn_link, 1025 MIN(node->tn_size - 1, uio->uio_resid), uio); 1026 } else { 1027 error = 0; 1028 } 1029 node->tn_status |= TMPFS_NODE_ACCESSED; 1030 1031 return error; 1032 } 1033 1034 int 1035 tmpfs_inactive(void *v) 1036 { 1037 struct vop_inactive_args /* { 1038 struct vnode *a_vp; 1039 bool *a_recycle; 1040 } */ *ap = v; 1041 vnode_t *vp = ap->a_vp; 1042 tmpfs_node_t *node; 1043 1044 KASSERT(VOP_ISLOCKED(vp)); 1045 1046 node = VP_TO_TMPFS_NODE(vp); 1047 *ap->a_recycle = (node->tn_links == 0); 1048 VOP_UNLOCK(vp); 1049 1050 return 0; 1051 } 1052 1053 int 1054 tmpfs_reclaim(void *v) 1055 { 1056 struct vop_reclaim_args /* { 1057 struct vnode *a_vp; 1058 } */ *ap = v; 1059 vnode_t *vp = ap->a_vp; 1060 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount); 1061 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1062 bool racing; 1063 1064 /* Disassociate inode from vnode. */ 1065 mutex_enter(&node->tn_vlock); 1066 node->tn_vnode = NULL; 1067 vp->v_data = NULL; 1068 /* Check if tmpfs_vnode_get() is racing with us. */ 1069 racing = TMPFS_NODE_RECLAIMING(node); 1070 mutex_exit(&node->tn_vlock); 1071 1072 /* 1073 * If inode is not referenced, i.e. no links, then destroy it. 1074 * Note: if racing - inode is about to get a new vnode, leave it. 1075 */ 1076 if (node->tn_links == 0 && !racing) { 1077 tmpfs_free_node(tmp, node); 1078 } 1079 return 0; 1080 } 1081 1082 int 1083 tmpfs_pathconf(void *v) 1084 { 1085 struct vop_pathconf_args /* { 1086 struct vnode *a_vp; 1087 int a_name; 1088 register_t *a_retval; 1089 } */ *ap = v; 1090 const int name = ap->a_name; 1091 register_t *retval = ap->a_retval; 1092 int error = 0; 1093 1094 switch (name) { 1095 case _PC_LINK_MAX: 1096 *retval = LINK_MAX; 1097 break; 1098 case _PC_NAME_MAX: 1099 *retval = TMPFS_MAXNAMLEN; 1100 break; 1101 case _PC_PATH_MAX: 1102 *retval = PATH_MAX; 1103 break; 1104 case _PC_PIPE_BUF: 1105 *retval = PIPE_BUF; 1106 break; 1107 case _PC_CHOWN_RESTRICTED: 1108 *retval = 1; 1109 break; 1110 case _PC_NO_TRUNC: 1111 *retval = 1; 1112 break; 1113 case _PC_SYNC_IO: 1114 *retval = 1; 1115 break; 1116 case _PC_FILESIZEBITS: 1117 *retval = sizeof(off_t) * CHAR_BIT; 1118 break; 1119 default: 1120 error = EINVAL; 1121 } 1122 return error; 1123 } 1124 1125 int 1126 tmpfs_advlock(void *v) 1127 { 1128 struct vop_advlock_args /* { 1129 struct vnode *a_vp; 1130 void * a_id; 1131 int a_op; 1132 struct flock *a_fl; 1133 int a_flags; 1134 } */ *ap = v; 1135 vnode_t *vp = ap->a_vp; 1136 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1137 1138 return lf_advlock(v, &node->tn_lockf, node->tn_size); 1139 } 1140 1141 int 1142 tmpfs_getpages(void *v) 1143 { 1144 struct vop_getpages_args /* { 1145 struct vnode *a_vp; 1146 voff_t a_offset; 1147 struct vm_page **a_m; 1148 int *a_count; 1149 int a_centeridx; 1150 vm_prot_t a_access_type; 1151 int a_advice; 1152 int a_flags; 1153 } */ * const ap = v; 1154 vnode_t *vp = ap->a_vp; 1155 const voff_t offset = ap->a_offset; 1156 struct vm_page **pgs = ap->a_m; 1157 const int centeridx = ap->a_centeridx; 1158 const vm_prot_t access_type = ap->a_access_type; 1159 const int advice = ap->a_advice; 1160 const int flags = ap->a_flags; 1161 int error, npages = *ap->a_count; 1162 tmpfs_node_t *node; 1163 struct uvm_object *uobj; 1164 1165 KASSERT(vp->v_type == VREG); 1166 KASSERT(mutex_owned(vp->v_interlock)); 1167 1168 node = VP_TO_TMPFS_NODE(vp); 1169 uobj = node->tn_spec.tn_reg.tn_aobj; 1170 1171 /* 1172 * Currently, PGO_PASTEOF is not supported. 1173 */ 1174 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) { 1175 if ((flags & PGO_LOCKED) == 0) 1176 mutex_exit(vp->v_interlock); 1177 return EINVAL; 1178 } 1179 1180 if (vp->v_size < offset + (npages << PAGE_SHIFT)) { 1181 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT; 1182 } 1183 1184 if ((flags & PGO_LOCKED) != 0) 1185 return EBUSY; 1186 1187 if ((flags & PGO_NOTIMESTAMP) == 0) { 1188 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 1189 node->tn_status |= TMPFS_NODE_ACCESSED; 1190 1191 if ((access_type & VM_PROT_WRITE) != 0) { 1192 node->tn_status |= TMPFS_NODE_MODIFIED; 1193 if (vp->v_mount->mnt_flag & MNT_RELATIME) 1194 node->tn_status |= TMPFS_NODE_ACCESSED; 1195 } 1196 } 1197 1198 /* 1199 * Invoke the pager. 1200 * 1201 * Clean the array of pages before. XXX: PR/32166 1202 * Note that vnode lock is shared with underlying UVM object. 1203 */ 1204 if (pgs) { 1205 memset(pgs, 0, sizeof(struct vm_pages *) * npages); 1206 } 1207 KASSERT(vp->v_interlock == uobj->vmobjlock); 1208 1209 error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx, 1210 access_type, advice, flags | PGO_ALLPAGES); 1211 1212 #if defined(DEBUG) 1213 if (!error && pgs) { 1214 for (int i = 0; i < npages; i++) { 1215 KASSERT(pgs[i] != NULL); 1216 } 1217 } 1218 #endif 1219 return error; 1220 } 1221 1222 int 1223 tmpfs_putpages(void *v) 1224 { 1225 struct vop_putpages_args /* { 1226 struct vnode *a_vp; 1227 voff_t a_offlo; 1228 voff_t a_offhi; 1229 int a_flags; 1230 } */ * const ap = v; 1231 vnode_t *vp = ap->a_vp; 1232 const voff_t offlo = ap->a_offlo; 1233 const voff_t offhi = ap->a_offhi; 1234 const int flags = ap->a_flags; 1235 tmpfs_node_t *node; 1236 struct uvm_object *uobj; 1237 int error; 1238 1239 KASSERT(mutex_owned(vp->v_interlock)); 1240 1241 if (vp->v_type != VREG) { 1242 mutex_exit(vp->v_interlock); 1243 return 0; 1244 } 1245 1246 node = VP_TO_TMPFS_NODE(vp); 1247 uobj = node->tn_spec.tn_reg.tn_aobj; 1248 1249 KASSERT(vp->v_interlock == uobj->vmobjlock); 1250 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags); 1251 1252 /* XXX mtime */ 1253 1254 return error; 1255 } 1256 1257 int 1258 tmpfs_whiteout(void *v) 1259 { 1260 struct vop_whiteout_args /* { 1261 struct vnode *a_dvp; 1262 struct componentname *a_cnp; 1263 int a_flags; 1264 } */ *ap = v; 1265 vnode_t *dvp = ap->a_dvp; 1266 struct componentname *cnp = ap->a_cnp; 1267 const int flags = ap->a_flags; 1268 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount); 1269 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp); 1270 tmpfs_dirent_t *de; 1271 int error; 1272 1273 switch (flags) { 1274 case LOOKUP: 1275 break; 1276 case CREATE: 1277 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, 1278 cnp->cn_namelen, &de); 1279 if (error) 1280 return error; 1281 tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT); 1282 break; 1283 case DELETE: 1284 cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */ 1285 de = tmpfs_dir_lookup(dnode, cnp); 1286 if (de == NULL) 1287 return ENOENT; 1288 tmpfs_dir_detach(dnode, de); 1289 tmpfs_free_dirent(tmp, de); 1290 break; 1291 } 1292 return 0; 1293 } 1294 1295 int 1296 tmpfs_print(void *v) 1297 { 1298 struct vop_print_args /* { 1299 struct vnode *a_vp; 1300 } */ *ap = v; 1301 vnode_t *vp = ap->a_vp; 1302 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1303 1304 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n" 1305 "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x", 1306 node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid, 1307 node->tn_gid, (uintmax_t)node->tn_size, node->tn_status); 1308 if (vp->v_type == VFIFO) { 1309 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v); 1310 } 1311 printf("\n"); 1312 return 0; 1313 } 1314