1 /* $NetBSD: tmpfs_vnops.c,v 1.103 2013/10/04 15:14:11 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.103 2013/10/04 15:14:11 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(dvp, de); 714 if (ap->a_cnp->cn_flags & DOWHITEOUT) 715 tmpfs_dir_attach(dvp, 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 *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 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 int 803 tmpfs_mkdir(void *v) 804 { 805 struct vop_mkdir_args /* { 806 struct vnode *a_dvp; 807 struct vnode **a_vpp; 808 struct componentname *a_cnp; 809 struct vattr *a_vap; 810 } */ *ap = v; 811 vnode_t *dvp = ap->a_dvp; 812 vnode_t **vpp = ap->a_vpp; 813 struct componentname *cnp = ap->a_cnp; 814 struct vattr *vap = ap->a_vap; 815 816 KASSERT(vap->va_type == VDIR); 817 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 818 } 819 820 int 821 tmpfs_rmdir(void *v) 822 { 823 struct vop_rmdir_args /* { 824 struct vnode *a_dvp; 825 struct vnode *a_vp; 826 struct componentname *a_cnp; 827 } */ *ap = v; 828 vnode_t *dvp = ap->a_dvp; 829 vnode_t *vp = ap->a_vp; 830 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount); 831 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp); 832 tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp); 833 tmpfs_dirent_t *de; 834 int error = 0; 835 836 KASSERT(VOP_ISLOCKED(dvp)); 837 KASSERT(VOP_ISLOCKED(vp)); 838 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode); 839 840 /* 841 * Directories with more than two non-whiteout 842 * entries ('.' and '..') cannot be removed. 843 */ 844 if (node->tn_size > 0) { 845 KASSERT(error == 0); 846 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) { 847 if (de->td_node != TMPFS_NODE_WHITEOUT) { 848 error = ENOTEMPTY; 849 break; 850 } 851 } 852 if (error) 853 goto out; 854 } 855 856 /* Lookup the directory entry (check the cached hint first). */ 857 de = tmpfs_dir_cached(node); 858 if (de == NULL) { 859 struct componentname *cnp = ap->a_cnp; 860 de = tmpfs_dir_lookup(dnode, cnp); 861 } 862 KASSERT(de && de->td_node == node); 863 864 /* Check flags to see if we are allowed to remove the directory. */ 865 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) { 866 error = EPERM; 867 goto out; 868 } 869 870 /* Decrement the link count for the virtual '.' entry. */ 871 node->tn_links--; 872 node->tn_status |= TMPFS_NODE_STATUSALL; 873 874 /* Detach the directory entry from the directory. */ 875 tmpfs_dir_detach(dvp, de); 876 877 /* Purge the cache for parent. */ 878 cache_purge(dvp); 879 880 /* 881 * Destroy the directory entry or replace it with a whiteout. 882 * Note: the inode referred by it will not be destroyed 883 * until the vnode is reclaimed. 884 */ 885 if (ap->a_cnp->cn_flags & DOWHITEOUT) 886 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT); 887 else 888 tmpfs_free_dirent(tmp, de); 889 890 /* Destroy the whiteout entries from the node. */ 891 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) { 892 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT); 893 tmpfs_dir_detach(vp, de); 894 tmpfs_free_dirent(tmp, de); 895 } 896 897 KASSERT(node->tn_links == 0); 898 out: 899 /* Release the nodes. */ 900 vput(dvp); 901 vput(vp); 902 return error; 903 } 904 905 int 906 tmpfs_symlink(void *v) 907 { 908 struct vop_symlink_args /* { 909 struct vnode *a_dvp; 910 struct vnode **a_vpp; 911 struct componentname *a_cnp; 912 struct vattr *a_vap; 913 char *a_target; 914 } */ *ap = v; 915 vnode_t *dvp = ap->a_dvp; 916 vnode_t **vpp = ap->a_vpp; 917 struct componentname *cnp = ap->a_cnp; 918 struct vattr *vap = ap->a_vap; 919 char *target = ap->a_target; 920 921 KASSERT(vap->va_type == VLNK); 922 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 923 } 924 925 int 926 tmpfs_readdir(void *v) 927 { 928 struct vop_readdir_args /* { 929 struct vnode *a_vp; 930 struct uio *a_uio; 931 kauth_cred_t a_cred; 932 int *a_eofflag; 933 off_t **a_cookies; 934 int *ncookies; 935 } */ *ap = v; 936 vnode_t *vp = ap->a_vp; 937 struct uio *uio = ap->a_uio; 938 int *eofflag = ap->a_eofflag; 939 off_t **cookies = ap->a_cookies; 940 int *ncookies = ap->a_ncookies; 941 off_t startoff, cnt; 942 tmpfs_node_t *node; 943 int error; 944 945 KASSERT(VOP_ISLOCKED(vp)); 946 947 /* This operation only makes sense on directory nodes. */ 948 if (vp->v_type != VDIR) { 949 return ENOTDIR; 950 } 951 node = VP_TO_TMPFS_DIR(vp); 952 startoff = uio->uio_offset; 953 cnt = 0; 954 if (node->tn_links == 0) { 955 error = 0; 956 goto out; 957 } 958 959 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 960 error = tmpfs_dir_getdotdent(node, uio); 961 if (error != 0) { 962 if (error == -1) 963 error = 0; 964 goto out; 965 } 966 cnt++; 967 } 968 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 969 error = tmpfs_dir_getdotdotdent(node, uio); 970 if (error != 0) { 971 if (error == -1) 972 error = 0; 973 goto out; 974 } 975 cnt++; 976 } 977 error = tmpfs_dir_getdents(node, uio, &cnt); 978 if (error == -1) { 979 error = 0; 980 } 981 KASSERT(error >= 0); 982 out: 983 if (eofflag != NULL) { 984 *eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 985 } 986 if (error || cookies == NULL || ncookies == NULL) { 987 return error; 988 } 989 990 /* Update NFS-related variables, if any. */ 991 off_t i, off = startoff; 992 tmpfs_dirent_t *de = NULL; 993 994 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 995 *ncookies = cnt; 996 997 for (i = 0; i < cnt; i++) { 998 KASSERT(off != TMPFS_DIRCOOKIE_EOF); 999 if (off != TMPFS_DIRCOOKIE_DOT) { 1000 if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1001 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir); 1002 } else if (de != NULL) { 1003 de = TAILQ_NEXT(de, td_entries); 1004 } else { 1005 de = tmpfs_dir_lookupbycookie(node, off); 1006 KASSERT(de != NULL); 1007 de = TAILQ_NEXT(de, td_entries); 1008 } 1009 if (de == NULL) { 1010 off = TMPFS_DIRCOOKIE_EOF; 1011 } else { 1012 off = tmpfs_dircookie(de); 1013 } 1014 } else { 1015 off = TMPFS_DIRCOOKIE_DOTDOT; 1016 } 1017 (*cookies)[i] = off; 1018 } 1019 KASSERT(uio->uio_offset == off); 1020 return error; 1021 } 1022 1023 int 1024 tmpfs_readlink(void *v) 1025 { 1026 struct vop_readlink_args /* { 1027 struct vnode *a_vp; 1028 struct uio *a_uio; 1029 kauth_cred_t a_cred; 1030 } */ *ap = v; 1031 vnode_t *vp = ap->a_vp; 1032 struct uio *uio = ap->a_uio; 1033 tmpfs_node_t *node; 1034 int error; 1035 1036 KASSERT(VOP_ISLOCKED(vp)); 1037 KASSERT(uio->uio_offset == 0); 1038 KASSERT(vp->v_type == VLNK); 1039 1040 node = VP_TO_TMPFS_NODE(vp); 1041 error = uiomove(node->tn_spec.tn_lnk.tn_link, 1042 MIN(node->tn_size, uio->uio_resid), uio); 1043 node->tn_status |= TMPFS_NODE_ACCESSED; 1044 1045 return error; 1046 } 1047 1048 int 1049 tmpfs_inactive(void *v) 1050 { 1051 struct vop_inactive_args /* { 1052 struct vnode *a_vp; 1053 bool *a_recycle; 1054 } */ *ap = v; 1055 vnode_t *vp = ap->a_vp; 1056 tmpfs_node_t *node; 1057 1058 KASSERT(VOP_ISLOCKED(vp)); 1059 1060 node = VP_TO_TMPFS_NODE(vp); 1061 *ap->a_recycle = (node->tn_links == 0); 1062 VOP_UNLOCK(vp); 1063 1064 return 0; 1065 } 1066 1067 int 1068 tmpfs_reclaim(void *v) 1069 { 1070 struct vop_reclaim_args /* { 1071 struct vnode *a_vp; 1072 } */ *ap = v; 1073 vnode_t *vp = ap->a_vp; 1074 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount); 1075 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1076 bool racing; 1077 1078 /* Disassociate inode from vnode. */ 1079 mutex_enter(&node->tn_vlock); 1080 node->tn_vnode = NULL; 1081 vp->v_data = NULL; 1082 /* Check if tmpfs_vnode_get() is racing with us. */ 1083 racing = TMPFS_NODE_RECLAIMING(node); 1084 mutex_exit(&node->tn_vlock); 1085 1086 /* 1087 * If inode is not referenced, i.e. no links, then destroy it. 1088 * Note: if racing - inode is about to get a new vnode, leave it. 1089 */ 1090 if (node->tn_links == 0 && !racing) { 1091 tmpfs_free_node(tmp, node); 1092 } 1093 return 0; 1094 } 1095 1096 int 1097 tmpfs_pathconf(void *v) 1098 { 1099 struct vop_pathconf_args /* { 1100 struct vnode *a_vp; 1101 int a_name; 1102 register_t *a_retval; 1103 } */ *ap = v; 1104 const int name = ap->a_name; 1105 register_t *retval = ap->a_retval; 1106 int error = 0; 1107 1108 switch (name) { 1109 case _PC_LINK_MAX: 1110 *retval = LINK_MAX; 1111 break; 1112 case _PC_NAME_MAX: 1113 *retval = TMPFS_MAXNAMLEN; 1114 break; 1115 case _PC_PATH_MAX: 1116 *retval = PATH_MAX; 1117 break; 1118 case _PC_PIPE_BUF: 1119 *retval = PIPE_BUF; 1120 break; 1121 case _PC_CHOWN_RESTRICTED: 1122 *retval = 1; 1123 break; 1124 case _PC_NO_TRUNC: 1125 *retval = 1; 1126 break; 1127 case _PC_SYNC_IO: 1128 *retval = 1; 1129 break; 1130 case _PC_FILESIZEBITS: 1131 *retval = sizeof(off_t) * CHAR_BIT; 1132 break; 1133 default: 1134 error = EINVAL; 1135 } 1136 return error; 1137 } 1138 1139 int 1140 tmpfs_advlock(void *v) 1141 { 1142 struct vop_advlock_args /* { 1143 struct vnode *a_vp; 1144 void * a_id; 1145 int a_op; 1146 struct flock *a_fl; 1147 int a_flags; 1148 } */ *ap = v; 1149 vnode_t *vp = ap->a_vp; 1150 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1151 1152 return lf_advlock(v, &node->tn_lockf, node->tn_size); 1153 } 1154 1155 int 1156 tmpfs_getpages(void *v) 1157 { 1158 struct vop_getpages_args /* { 1159 struct vnode *a_vp; 1160 voff_t a_offset; 1161 struct vm_page **a_m; 1162 int *a_count; 1163 int a_centeridx; 1164 vm_prot_t a_access_type; 1165 int a_advice; 1166 int a_flags; 1167 } */ * const ap = v; 1168 vnode_t *vp = ap->a_vp; 1169 const voff_t offset = ap->a_offset; 1170 struct vm_page **pgs = ap->a_m; 1171 const int centeridx = ap->a_centeridx; 1172 const vm_prot_t access_type = ap->a_access_type; 1173 const int advice = ap->a_advice; 1174 const int flags = ap->a_flags; 1175 int error, npages = *ap->a_count; 1176 tmpfs_node_t *node; 1177 struct uvm_object *uobj; 1178 1179 KASSERT(vp->v_type == VREG); 1180 KASSERT(mutex_owned(vp->v_interlock)); 1181 1182 node = VP_TO_TMPFS_NODE(vp); 1183 uobj = node->tn_spec.tn_reg.tn_aobj; 1184 1185 /* 1186 * Currently, PGO_PASTEOF is not supported. 1187 */ 1188 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) { 1189 if ((flags & PGO_LOCKED) == 0) 1190 mutex_exit(vp->v_interlock); 1191 return EINVAL; 1192 } 1193 1194 if (vp->v_size < offset + (npages << PAGE_SHIFT)) { 1195 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT; 1196 } 1197 1198 if ((flags & PGO_LOCKED) != 0) 1199 return EBUSY; 1200 1201 if ((flags & PGO_NOTIMESTAMP) == 0) { 1202 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 1203 node->tn_status |= TMPFS_NODE_ACCESSED; 1204 1205 if ((access_type & VM_PROT_WRITE) != 0) { 1206 node->tn_status |= TMPFS_NODE_MODIFIED; 1207 if (vp->v_mount->mnt_flag & MNT_RELATIME) 1208 node->tn_status |= TMPFS_NODE_ACCESSED; 1209 } 1210 } 1211 1212 /* 1213 * Invoke the pager. 1214 * 1215 * Clean the array of pages before. XXX: PR/32166 1216 * Note that vnode lock is shared with underlying UVM object. 1217 */ 1218 if (pgs) { 1219 memset(pgs, 0, sizeof(struct vm_pages *) * npages); 1220 } 1221 KASSERT(vp->v_interlock == uobj->vmobjlock); 1222 1223 error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx, 1224 access_type, advice, flags | PGO_ALLPAGES); 1225 1226 #if defined(DEBUG) 1227 if (!error && pgs) { 1228 for (int i = 0; i < npages; i++) { 1229 KASSERT(pgs[i] != NULL); 1230 } 1231 } 1232 #endif 1233 return error; 1234 } 1235 1236 int 1237 tmpfs_putpages(void *v) 1238 { 1239 struct vop_putpages_args /* { 1240 struct vnode *a_vp; 1241 voff_t a_offlo; 1242 voff_t a_offhi; 1243 int a_flags; 1244 } */ * const ap = v; 1245 vnode_t *vp = ap->a_vp; 1246 const voff_t offlo = ap->a_offlo; 1247 const voff_t offhi = ap->a_offhi; 1248 const int flags = ap->a_flags; 1249 tmpfs_node_t *node; 1250 struct uvm_object *uobj; 1251 int error; 1252 1253 KASSERT(mutex_owned(vp->v_interlock)); 1254 1255 if (vp->v_type != VREG) { 1256 mutex_exit(vp->v_interlock); 1257 return 0; 1258 } 1259 1260 node = VP_TO_TMPFS_NODE(vp); 1261 uobj = node->tn_spec.tn_reg.tn_aobj; 1262 1263 KASSERT(vp->v_interlock == uobj->vmobjlock); 1264 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags); 1265 1266 /* XXX mtime */ 1267 1268 return error; 1269 } 1270 1271 int 1272 tmpfs_whiteout(void *v) 1273 { 1274 struct vop_whiteout_args /* { 1275 struct vnode *a_dvp; 1276 struct componentname *a_cnp; 1277 int a_flags; 1278 } */ *ap = v; 1279 vnode_t *dvp = ap->a_dvp; 1280 struct componentname *cnp = ap->a_cnp; 1281 const int flags = ap->a_flags; 1282 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount); 1283 tmpfs_dirent_t *de; 1284 int error; 1285 1286 switch (flags) { 1287 case LOOKUP: 1288 break; 1289 case CREATE: 1290 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, 1291 cnp->cn_namelen, &de); 1292 if (error) 1293 return error; 1294 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT); 1295 break; 1296 case DELETE: 1297 cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */ 1298 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp); 1299 if (de == NULL) 1300 return ENOENT; 1301 tmpfs_dir_detach(dvp, de); 1302 tmpfs_free_dirent(tmp, de); 1303 break; 1304 } 1305 return 0; 1306 } 1307 1308 int 1309 tmpfs_print(void *v) 1310 { 1311 struct vop_print_args /* { 1312 struct vnode *a_vp; 1313 } */ *ap = v; 1314 vnode_t *vp = ap->a_vp; 1315 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp); 1316 1317 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n" 1318 "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x", 1319 node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid, 1320 node->tn_gid, (uintmax_t)node->tn_size, node->tn_status); 1321 if (vp->v_type == VFIFO) { 1322 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v); 1323 } 1324 printf("\n"); 1325 return 0; 1326 } 1327