1 /* $NetBSD: tmpfs_vnops.c,v 1.73 2010/07/14 16:03:49 pooka 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.73 2010/07/14 16:03:49 pooka 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/proc.h> 47 #include <sys/stat.h> 48 #include <sys/uio.h> 49 #include <sys/unistd.h> 50 #include <sys/vnode.h> 51 #include <sys/lockf.h> 52 #include <sys/kauth.h> 53 54 #include <uvm/uvm.h> 55 56 #include <miscfs/fifofs/fifo.h> 57 #include <miscfs/genfs/genfs.h> 58 #include <fs/tmpfs/tmpfs_vnops.h> 59 #include <fs/tmpfs/tmpfs.h> 60 61 /* --------------------------------------------------------------------- */ 62 63 /* 64 * vnode operations vector used for files stored in a tmpfs file system. 65 */ 66 int (**tmpfs_vnodeop_p)(void *); 67 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = { 68 { &vop_default_desc, vn_default_error }, 69 { &vop_lookup_desc, tmpfs_lookup }, 70 { &vop_create_desc, tmpfs_create }, 71 { &vop_mknod_desc, tmpfs_mknod }, 72 { &vop_open_desc, tmpfs_open }, 73 { &vop_close_desc, tmpfs_close }, 74 { &vop_access_desc, tmpfs_access }, 75 { &vop_getattr_desc, tmpfs_getattr }, 76 { &vop_setattr_desc, tmpfs_setattr }, 77 { &vop_read_desc, tmpfs_read }, 78 { &vop_write_desc, tmpfs_write }, 79 { &vop_ioctl_desc, tmpfs_ioctl }, 80 { &vop_fcntl_desc, tmpfs_fcntl }, 81 { &vop_poll_desc, tmpfs_poll }, 82 { &vop_kqfilter_desc, tmpfs_kqfilter }, 83 { &vop_revoke_desc, tmpfs_revoke }, 84 { &vop_mmap_desc, tmpfs_mmap }, 85 { &vop_fsync_desc, tmpfs_fsync }, 86 { &vop_seek_desc, tmpfs_seek }, 87 { &vop_remove_desc, tmpfs_remove }, 88 { &vop_link_desc, tmpfs_link }, 89 { &vop_rename_desc, tmpfs_rename }, 90 { &vop_mkdir_desc, tmpfs_mkdir }, 91 { &vop_rmdir_desc, tmpfs_rmdir }, 92 { &vop_symlink_desc, tmpfs_symlink }, 93 { &vop_readdir_desc, tmpfs_readdir }, 94 { &vop_readlink_desc, tmpfs_readlink }, 95 { &vop_abortop_desc, tmpfs_abortop }, 96 { &vop_inactive_desc, tmpfs_inactive }, 97 { &vop_reclaim_desc, tmpfs_reclaim }, 98 { &vop_lock_desc, tmpfs_lock }, 99 { &vop_unlock_desc, tmpfs_unlock }, 100 { &vop_bmap_desc, tmpfs_bmap }, 101 { &vop_strategy_desc, tmpfs_strategy }, 102 { &vop_print_desc, tmpfs_print }, 103 { &vop_pathconf_desc, tmpfs_pathconf }, 104 { &vop_islocked_desc, tmpfs_islocked }, 105 { &vop_advlock_desc, tmpfs_advlock }, 106 { &vop_bwrite_desc, tmpfs_bwrite }, 107 { &vop_getpages_desc, tmpfs_getpages }, 108 { &vop_putpages_desc, tmpfs_putpages }, 109 { NULL, NULL } 110 }; 111 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = 112 { &tmpfs_vnodeop_p, tmpfs_vnodeop_entries }; 113 114 /* 115 * tmpfs_lookup: lookup 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 struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp; 132 struct componentname *cnp = ap->a_cnp; 133 struct tmpfs_dirent *de; 134 struct tmpfs_node *dnode; 135 int error; 136 137 KASSERT(VOP_ISLOCKED(dvp)); 138 139 dnode = VP_TO_TMPFS_DIR(dvp); 140 *vpp = NULL; 141 142 /* Check accessibility of requested node as a first step. */ 143 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred); 144 if (error != 0) 145 goto out; 146 147 /* 148 * If requesting the last path component on a read-only file system 149 * with a write operation, deny it. 150 */ 151 if ((cnp->cn_flags & ISLASTCN) && 152 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 153 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 154 error = EROFS; 155 goto out; 156 } 157 158 /* 159 * Avoid doing a linear scan of the directory if the requested 160 * directory/name couple is already in the cache. 161 */ 162 error = cache_lookup(dvp, vpp, cnp); 163 if (error >= 0) 164 goto out; 165 166 /* We cannot be requesting the parent directory of the root node. */ 167 KASSERT(IMPLIES(dnode->tn_type == VDIR && 168 dnode->tn_spec.tn_dir.tn_parent == dnode, 169 !(cnp->cn_flags & ISDOTDOT))); 170 171 if (cnp->cn_flags & ISDOTDOT) { 172 VOP_UNLOCK(dvp); 173 174 /* Allocate a new vnode on the matching entry. */ 175 error = tmpfs_alloc_vp(dvp->v_mount, 176 dnode->tn_spec.tn_dir.tn_parent, vpp); 177 178 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 179 goto done; 180 181 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 182 if ((cnp->cn_flags & ISLASTCN) && 183 (cnp->cn_nameiop == RENAME)) { 184 error = EISDIR; 185 goto out; 186 } 187 vref(dvp); 188 *vpp = dvp; 189 error = 0; 190 goto done; 191 } 192 193 de = tmpfs_dir_lookup(dnode, cnp); 194 if (de == NULL) { 195 /* 196 * The entry was not found in the directory. This is valid 197 * if we are creating or renaming an entry and are working 198 * on the last component of the path name. 199 */ 200 if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == CREATE || 201 cnp->cn_nameiop == RENAME)) { 202 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred); 203 if (error) { 204 goto out; 205 } 206 /* Keep the component name for future uses. */ 207 cnp->cn_flags |= SAVENAME; 208 error = EJUSTRETURN; 209 } else { 210 error = ENOENT; 211 } 212 } else { 213 struct tmpfs_node *tnode = de->td_node; 214 215 /* 216 * If we are not at the last path component and found a 217 * non-directory or non-link entry (which may itself be 218 * pointing to a directory), raise an error. 219 */ 220 if ((tnode->tn_type != VDIR && tnode->tn_type != VLNK) && 221 (cnp->cn_flags & ISLASTCN) == 0) { 222 error = ENOTDIR; 223 goto out; 224 } 225 226 /* Check permissions. */ 227 if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == DELETE || 228 cnp->cn_nameiop == RENAME)) { 229 kauth_action_t action = 0; 230 231 /* This is the file-system's decision. */ 232 if ((dnode->tn_mode & S_ISTXT) != 0 && 233 kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid && 234 kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid) 235 error = EPERM; 236 else 237 error = 0; 238 239 /* Only bother if we are not already failing it. */ 240 if (!error) { 241 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred); 242 } 243 244 if (cnp->cn_nameiop == DELETE) { 245 action |= KAUTH_VNODE_DELETE; 246 } else { 247 KASSERT(cnp->cn_nameiop == RENAME); 248 action |= KAUTH_VNODE_RENAME; 249 } 250 error = kauth_authorize_vnode(cnp->cn_cred, 251 action, *vpp, dvp, error); 252 if (error) { 253 goto out; 254 } 255 cnp->cn_flags |= SAVENAME; 256 } 257 /* Allocate a new vnode on the matching entry. */ 258 error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp); 259 } 260 done: 261 /* 262 * Store the result of this lookup in the cache. Avoid this if the 263 * request was for creation, as it does not improve timings on 264 * emprical tests. 265 */ 266 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE && 267 (cnp->cn_flags & ISDOTDOT) == 0) 268 cache_enter(dvp, *vpp, cnp); 269 270 out: 271 KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp))); 272 KASSERT(VOP_ISLOCKED(dvp)); 273 return error; 274 } 275 276 int 277 tmpfs_create(void *v) 278 { 279 struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp; 280 struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp; 281 struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp; 282 struct vattr *vap = ((struct vop_create_args *)v)->a_vap; 283 284 KASSERT(vap->va_type == VREG || vap->va_type == VSOCK); 285 286 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 287 } 288 /* --------------------------------------------------------------------- */ 289 290 int 291 tmpfs_mknod(void *v) 292 { 293 struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp; 294 struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp; 295 struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp; 296 struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap; 297 298 if (vap->va_type != VBLK && vap->va_type != VCHR && 299 vap->va_type != VFIFO) { 300 vput(dvp); 301 return EINVAL; 302 } 303 304 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 305 } 306 307 /* --------------------------------------------------------------------- */ 308 309 int 310 tmpfs_open(void *v) 311 { 312 struct vnode *vp = ((struct vop_open_args *)v)->a_vp; 313 int mode = ((struct vop_open_args *)v)->a_mode; 314 315 int error; 316 struct tmpfs_node *node; 317 318 KASSERT(VOP_ISLOCKED(vp)); 319 320 node = VP_TO_TMPFS_NODE(vp); 321 322 /* The file is still active but all its names have been removed 323 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as 324 * it is about to die. */ 325 if (node->tn_links < 1) { 326 error = ENOENT; 327 goto out; 328 } 329 330 /* If the file is marked append-only, deny write requests. */ 331 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE) 332 error = EPERM; 333 else 334 error = 0; 335 336 out: 337 KASSERT(VOP_ISLOCKED(vp)); 338 339 return error; 340 } 341 342 /* --------------------------------------------------------------------- */ 343 344 int 345 tmpfs_close(void *v) 346 { 347 struct vnode *vp = ((struct vop_close_args *)v)->a_vp; 348 349 struct tmpfs_node *node; 350 351 KASSERT(VOP_ISLOCKED(vp)); 352 353 node = VP_TO_TMPFS_NODE(vp); 354 355 if (node->tn_links > 0) { 356 /* Update node times. No need to do it if the node has 357 * been deleted, because it will vanish after we return. */ 358 tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE); 359 } 360 361 return 0; 362 } 363 364 /* --------------------------------------------------------------------- */ 365 366 static int 367 tmpfs_check_possible(struct vnode *vp, struct tmpfs_node *node, mode_t mode) 368 { 369 int error = 0; 370 371 switch (vp->v_type) { 372 case VDIR: 373 /* FALLTHROUGH */ 374 case VLNK: 375 /* FALLTHROUGH */ 376 case VREG: 377 if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) { 378 error = EROFS; 379 goto out; 380 } 381 break; 382 383 case VBLK: 384 /* FALLTHROUGH */ 385 case VCHR: 386 /* FALLTHROUGH */ 387 case VSOCK: 388 /* FALLTHROUGH */ 389 case VFIFO: 390 break; 391 392 default: 393 error = EINVAL; 394 goto out; 395 } 396 397 if (mode & VWRITE && node->tn_flags & IMMUTABLE) { 398 error = EPERM; 399 goto out; 400 } 401 402 out: 403 return error; 404 } 405 406 static int 407 tmpfs_check_permitted(struct vnode *vp, struct tmpfs_node *node, mode_t mode, 408 kauth_cred_t cred) 409 { 410 411 return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid, 412 node->tn_gid, mode, cred); 413 } 414 415 int 416 tmpfs_access(void *v) 417 { 418 struct vnode *vp = ((struct vop_access_args *)v)->a_vp; 419 int mode = ((struct vop_access_args *)v)->a_mode; 420 kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred; 421 422 int error; 423 struct tmpfs_node *node; 424 425 KASSERT(VOP_ISLOCKED(vp)); 426 427 node = VP_TO_TMPFS_NODE(vp); 428 429 error = tmpfs_check_possible(vp, node, mode); 430 if (error) 431 goto out; 432 433 error = tmpfs_check_permitted(vp, node, mode, cred); 434 435 error = kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp, 436 NULL, error); 437 438 out: 439 KASSERT(VOP_ISLOCKED(vp)); 440 441 return error; 442 } 443 444 /* --------------------------------------------------------------------- */ 445 446 int 447 tmpfs_getattr(void *v) 448 { 449 struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp; 450 struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap; 451 452 struct tmpfs_node *node; 453 454 node = VP_TO_TMPFS_NODE(vp); 455 456 vattr_null(vap); 457 458 tmpfs_itimes(vp, NULL, NULL, NULL); 459 460 vap->va_type = vp->v_type; 461 vap->va_mode = node->tn_mode; 462 vap->va_nlink = node->tn_links; 463 vap->va_uid = node->tn_uid; 464 vap->va_gid = node->tn_gid; 465 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 466 vap->va_fileid = node->tn_id; 467 vap->va_size = node->tn_size; 468 vap->va_blocksize = PAGE_SIZE; 469 vap->va_atime = node->tn_atime; 470 vap->va_mtime = node->tn_mtime; 471 vap->va_ctime = node->tn_ctime; 472 vap->va_birthtime = node->tn_birthtime; 473 vap->va_gen = node->tn_gen; 474 vap->va_flags = node->tn_flags; 475 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 476 node->tn_spec.tn_dev.tn_rdev : VNOVAL; 477 vap->va_bytes = round_page(node->tn_size); 478 vap->va_filerev = VNOVAL; 479 vap->va_vaflags = 0; 480 vap->va_spare = VNOVAL; /* XXX */ 481 482 return 0; 483 } 484 485 /* --------------------------------------------------------------------- */ 486 487 #define GOODTIME(tv) ((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL) 488 /* XXX Should this operation be atomic? I think it should, but code in 489 * XXX other places (e.g., ufs) doesn't seem to be... */ 490 int 491 tmpfs_setattr(void *v) 492 { 493 struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp; 494 struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap; 495 kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred; 496 struct lwp *l = curlwp; 497 498 int error; 499 500 KASSERT(VOP_ISLOCKED(vp)); 501 502 error = 0; 503 504 /* Abort if any unsettable attribute is given. */ 505 if (vap->va_type != VNON || 506 vap->va_nlink != VNOVAL || 507 vap->va_fsid != VNOVAL || 508 vap->va_fileid != VNOVAL || 509 vap->va_blocksize != VNOVAL || 510 GOODTIME(&vap->va_ctime) || 511 vap->va_gen != VNOVAL || 512 vap->va_rdev != VNOVAL || 513 vap->va_bytes != VNOVAL) 514 error = EINVAL; 515 516 if (error == 0 && (vap->va_flags != VNOVAL)) 517 error = tmpfs_chflags(vp, vap->va_flags, cred, l); 518 519 if (error == 0 && (vap->va_size != VNOVAL)) 520 error = tmpfs_chsize(vp, vap->va_size, cred, l); 521 522 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 523 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l); 524 525 if (error == 0 && (vap->va_mode != VNOVAL)) 526 error = tmpfs_chmod(vp, vap->va_mode, cred, l); 527 528 if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime) 529 || GOODTIME(&vap->va_birthtime))) 530 if ((error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 531 &vap->va_birthtime, vap->va_vaflags, cred, l)) == 0) 532 return 0; 533 534 /* Update the node times. We give preference to the error codes 535 * generated by this function rather than the ones that may arise 536 * from tmpfs_update. */ 537 tmpfs_update(vp, NULL, NULL, NULL, 0); 538 539 KASSERT(VOP_ISLOCKED(vp)); 540 541 return error; 542 } 543 544 /* --------------------------------------------------------------------- */ 545 546 int 547 tmpfs_read(void *v) 548 { 549 struct vnode *vp = ((struct vop_read_args *)v)->a_vp; 550 struct uio *uio = ((struct vop_read_args *)v)->a_uio; 551 int ioflag = ((struct vop_read_args *)v)->a_ioflag; 552 553 int error; 554 struct tmpfs_node *node; 555 struct uvm_object *uobj; 556 557 KASSERT(VOP_ISLOCKED(vp)); 558 559 node = VP_TO_TMPFS_NODE(vp); 560 561 if (vp->v_type != VREG) { 562 error = EISDIR; 563 goto out; 564 } 565 566 if (uio->uio_offset < 0) { 567 error = EINVAL; 568 goto out; 569 } 570 571 node->tn_status |= TMPFS_NODE_ACCESSED; 572 573 uobj = node->tn_spec.tn_reg.tn_aobj; 574 error = 0; 575 while (error == 0 && uio->uio_resid > 0) { 576 vsize_t len; 577 578 if (node->tn_size <= uio->uio_offset) 579 break; 580 581 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid); 582 if (len == 0) 583 break; 584 585 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag), 586 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); 587 } 588 589 out: 590 KASSERT(VOP_ISLOCKED(vp)); 591 592 return error; 593 } 594 595 /* --------------------------------------------------------------------- */ 596 597 int 598 tmpfs_write(void *v) 599 { 600 struct vnode *vp = ((struct vop_write_args *)v)->a_vp; 601 struct uio *uio = ((struct vop_write_args *)v)->a_uio; 602 int ioflag = ((struct vop_write_args *)v)->a_ioflag; 603 604 bool extended; 605 int error; 606 off_t oldsize; 607 struct tmpfs_node *node; 608 struct uvm_object *uobj; 609 610 KASSERT(VOP_ISLOCKED(vp)); 611 612 node = VP_TO_TMPFS_NODE(vp); 613 oldsize = node->tn_size; 614 615 if (uio->uio_offset < 0 || vp->v_type != VREG) { 616 error = EINVAL; 617 goto out; 618 } 619 620 if (uio->uio_resid == 0) { 621 error = 0; 622 goto out; 623 } 624 625 if (ioflag & IO_APPEND) 626 uio->uio_offset = node->tn_size; 627 628 extended = uio->uio_offset + uio->uio_resid > node->tn_size; 629 if (extended) { 630 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid); 631 if (error != 0) 632 goto out; 633 } 634 635 uobj = node->tn_spec.tn_reg.tn_aobj; 636 error = 0; 637 while (error == 0 && uio->uio_resid > 0) { 638 vsize_t len; 639 640 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid); 641 if (len == 0) 642 break; 643 644 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag), 645 UBC_WRITE | UBC_UNMAP_FLAG(vp)); 646 } 647 648 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 649 (extended ? TMPFS_NODE_CHANGED : 0); 650 651 if (error != 0) 652 (void)tmpfs_reg_resize(vp, oldsize); 653 654 VN_KNOTE(vp, NOTE_WRITE); 655 656 out: 657 KASSERT(VOP_ISLOCKED(vp)); 658 KASSERT(IMPLIES(error == 0, uio->uio_resid == 0)); 659 KASSERT(IMPLIES(error != 0, oldsize == node->tn_size)); 660 661 return error; 662 } 663 664 /* --------------------------------------------------------------------- */ 665 666 int 667 tmpfs_fsync(void *v) 668 { 669 struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp; 670 671 KASSERT(VOP_ISLOCKED(vp)); 672 673 tmpfs_update(vp, NULL, NULL, NULL, 0); 674 675 return 0; 676 } 677 678 /* --------------------------------------------------------------------- */ 679 680 int 681 tmpfs_remove(void *v) 682 { 683 struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp; 684 struct vnode *vp = ((struct vop_remove_args *)v)->a_vp; 685 struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp); 686 687 int error; 688 struct tmpfs_dirent *de; 689 struct tmpfs_mount *tmp; 690 struct tmpfs_node *dnode; 691 struct tmpfs_node *node; 692 693 KASSERT(VOP_ISLOCKED(dvp)); 694 KASSERT(VOP_ISLOCKED(vp)); 695 696 if (vp->v_type == VDIR) { 697 error = EPERM; 698 goto out; 699 } 700 701 dnode = VP_TO_TMPFS_DIR(dvp); 702 node = VP_TO_TMPFS_NODE(vp); 703 tmp = VFS_TO_TMPFS(vp->v_mount); 704 de = tmpfs_dir_lookup(dnode, cnp); 705 KASSERT(de); 706 KASSERT(de->td_node == node); 707 708 /* Files marked as immutable or append-only cannot be deleted. */ 709 if (node->tn_flags & (IMMUTABLE | APPEND)) { 710 error = EPERM; 711 goto out; 712 } 713 714 /* Remove the entry from the directory; as it is a file, we do not 715 * have to change the number of hard links of the directory. */ 716 tmpfs_dir_detach(dvp, de); 717 718 /* Free the directory entry we just deleted. Note that the node 719 * referred by it will not be removed until the vnode is really 720 * reclaimed. */ 721 tmpfs_free_dirent(tmp, de, true); 722 723 error = 0; 724 725 out: 726 vput(vp); 727 if (dvp == vp) 728 vrele(dvp); 729 else 730 vput(dvp); 731 if (cnp->cn_flags & HASBUF) { 732 PNBUF_PUT(cnp->cn_pnbuf); 733 cnp->cn_flags &= ~HASBUF; 734 } 735 736 return error; 737 } 738 739 /* --------------------------------------------------------------------- */ 740 741 int 742 tmpfs_link(void *v) 743 { 744 struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp; 745 struct vnode *vp = ((struct vop_link_args *)v)->a_vp; 746 struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp; 747 748 int error; 749 struct tmpfs_dirent *de; 750 struct tmpfs_node *dnode; 751 struct tmpfs_node *node; 752 753 KASSERT(VOP_ISLOCKED(dvp)); 754 KASSERT(cnp->cn_flags & HASBUF); 755 KASSERT(dvp != vp); /* XXX When can this be false? */ 756 757 dnode = VP_TO_TMPFS_DIR(dvp); 758 node = VP_TO_TMPFS_NODE(vp); 759 760 /* Lock vp because we will need to run tmpfs_update over it, which 761 * needs the vnode to be locked. */ 762 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 763 764 /* XXX: Why aren't the following two tests done by the caller? */ 765 766 /* Hard links of directories are forbidden. */ 767 if (vp->v_type == VDIR) { 768 error = EPERM; 769 goto out; 770 } 771 772 /* Cannot create cross-device links. */ 773 if (dvp->v_mount != vp->v_mount) { 774 error = EXDEV; 775 goto out; 776 } 777 778 /* Ensure that we do not overflow the maximum number of links imposed 779 * by the system. */ 780 KASSERT(node->tn_links <= LINK_MAX); 781 if (node->tn_links == LINK_MAX) { 782 error = EMLINK; 783 goto out; 784 } 785 786 /* We cannot create links of files marked immutable or append-only. */ 787 if (node->tn_flags & (IMMUTABLE | APPEND)) { 788 error = EPERM; 789 goto out; 790 } 791 792 /* Allocate a new directory entry to represent the node. */ 793 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 794 cnp->cn_nameptr, cnp->cn_namelen, &de); 795 if (error != 0) 796 goto out; 797 798 /* Insert the new directory entry into the appropriate directory. */ 799 tmpfs_dir_attach(dvp, de); 800 801 /* vp link count has changed, so update node times. */ 802 node->tn_status |= TMPFS_NODE_CHANGED; 803 tmpfs_update(vp, NULL, NULL, NULL, 0); 804 805 error = 0; 806 807 out: 808 VOP_UNLOCK(vp); 809 PNBUF_PUT(cnp->cn_pnbuf); 810 vput(dvp); 811 812 return error; 813 } 814 815 /* 816 * tmpfs_rename: rename routine. 817 * 818 * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent) 819 * and tvp (to-leaf), if exists (NULL if not). 820 * 821 * => Caller holds a reference on fdvp and fvp, they are unlocked. 822 * Note: fdvp and fvp can refer to the same object (i.e. when it is root). 823 * 824 * => Both tdvp and tvp are referenced and locked. It is our responsibility 825 * to release the references and unlock them (or destroy). 826 */ 827 int 828 tmpfs_rename(void *v) 829 { 830 struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp; 831 struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp; 832 struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp; 833 struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp; 834 struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp; 835 struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp; 836 837 char *newname; 838 int error; 839 struct tmpfs_dirent *de, *de2; 840 struct tmpfs_mount *tmp; 841 struct tmpfs_node *fdnode; 842 struct tmpfs_node *fnode; 843 struct tmpfs_node *tnode; 844 struct tmpfs_node *tdnode; 845 size_t namelen; 846 847 KASSERT(VOP_ISLOCKED(tdvp)); 848 KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE)); 849 KASSERT(fcnp->cn_flags & HASBUF); 850 KASSERT(tcnp->cn_flags & HASBUF); 851 852 newname = NULL; 853 namelen = 0; 854 tmp = NULL; 855 856 /* Disallow cross-device renames. */ 857 if (fvp->v_mount != tdvp->v_mount || 858 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 859 error = EXDEV; 860 goto out_unlocked; 861 } 862 863 fnode = VP_TO_TMPFS_NODE(fvp); 864 fdnode = VP_TO_TMPFS_DIR(fdvp); 865 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp); 866 tdnode = VP_TO_TMPFS_DIR(tdvp); 867 tmp = VFS_TO_TMPFS(tdvp->v_mount); 868 869 if (fdvp == tvp) { 870 error = 0; 871 goto out_unlocked; 872 } 873 874 /* Allocate memory, if necessary, for a new name. */ 875 namelen = tcnp->cn_namelen; 876 if (tmpfs_strname_neqlen(fcnp, tcnp)) { 877 newname = tmpfs_strname_alloc(tmp, namelen); 878 if (newname == NULL) { 879 error = ENOSPC; 880 goto out_unlocked; 881 } 882 } 883 884 /* If we need to move the directory between entries, lock the 885 * source so that we can safely operate on it. */ 886 887 /* XXX: this is a potential locking order violation! */ 888 if (fdnode != tdnode) { 889 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 890 } 891 892 /* 893 * If the node we were renaming has scarpered, just give up. 894 */ 895 de = tmpfs_dir_lookup(fdnode, fcnp); 896 if (de == NULL || de->td_node != fnode) { 897 error = ENOENT; 898 goto out; 899 } 900 901 /* If source and target is the same vnode, remove the source link. */ 902 if (fvp == tvp) { 903 /* 904 * Detach and free the directory entry. Drops the link 905 * count on the node. 906 */ 907 tmpfs_dir_detach(fdvp, de); 908 tmpfs_free_dirent(VFS_TO_TMPFS(fvp->v_mount), de, true); 909 VN_KNOTE(fdvp, NOTE_WRITE); 910 goto out_ok; 911 } 912 913 /* If replacing an existing entry, ensure we can do the operation. */ 914 if (tvp != NULL) { 915 KASSERT(tnode != NULL); 916 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 917 if (tnode->tn_size > 0) { 918 error = ENOTEMPTY; 919 goto out; 920 } 921 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 922 error = ENOTDIR; 923 goto out; 924 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 925 error = EISDIR; 926 goto out; 927 } else { 928 KASSERT(fnode->tn_type != VDIR && 929 tnode->tn_type != VDIR); 930 } 931 } 932 933 /* If the node is being moved to another directory, we have to do 934 * the move. */ 935 if (fdnode != tdnode) { 936 /* In case we are moving a directory, we have to adjust its 937 * parent to point to the new parent. */ 938 if (de->td_node->tn_type == VDIR) { 939 struct tmpfs_node *n; 940 941 /* Ensure the target directory is not a child of the 942 * directory being moved. Otherwise, we'd end up 943 * with stale nodes. */ 944 n = tdnode; 945 while (n != n->tn_spec.tn_dir.tn_parent) { 946 if (n == fnode) { 947 error = EINVAL; 948 goto out; 949 } 950 n = n->tn_spec.tn_dir.tn_parent; 951 } 952 953 /* Adjust the parent pointer. */ 954 TMPFS_VALIDATE_DIR(fnode); 955 de->td_node->tn_spec.tn_dir.tn_parent = tdnode; 956 957 /* As a result of changing the target of the '..' 958 * entry, the link count of the source and target 959 * directories has to be adjusted. */ 960 fdnode->tn_links--; 961 tdnode->tn_links++; 962 } 963 964 /* Do the move: just remove the entry from the source directory 965 * and insert it into the target one. */ 966 tmpfs_dir_detach(fdvp, de); 967 tmpfs_dir_attach(tdvp, de); 968 969 /* Notify listeners of fdvp about the change in the directory. 970 * We can do it at this point because we aren't touching fdvp 971 * any more below. */ 972 VN_KNOTE(fdvp, NOTE_WRITE); 973 } 974 975 /* If we are overwriting an entry, we have to remove the old one 976 * from the target directory. */ 977 if (tvp != NULL) { 978 KASSERT(tnode != NULL); 979 980 /* Remove the old entry from the target directory. 981 * Note! This relies on tmpfs_dir_attach() putting the new 982 * node on the end of the target's node list. */ 983 de2 = tmpfs_dir_lookup(tdnode, tcnp); 984 KASSERT(de2 != NULL); 985 KASSERT(de2->td_node == tnode); 986 tmpfs_dir_detach(tdvp, de2); 987 988 /* Free the directory entry we just deleted. Note that the 989 * node referred by it will not be removed until the vnode is 990 * really reclaimed. */ 991 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true); 992 } 993 994 /* If the name has changed, we need to make it effective by changing 995 * it in the directory entry. */ 996 if (newname != NULL) { 997 KASSERT(tcnp->cn_namelen < MAXNAMLEN); 998 KASSERT(tcnp->cn_namelen < 0xffff); 999 1000 tmpfs_strname_free(tmp, de->td_name, de->td_namelen); 1001 de->td_namelen = (uint16_t)namelen; 1002 memcpy(newname, tcnp->cn_nameptr, namelen); 1003 de->td_name = newname; 1004 newname = NULL; 1005 1006 fnode->tn_status |= TMPFS_NODE_CHANGED; 1007 tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1008 } 1009 out_ok: 1010 /* Notify listeners of tdvp about the change in the directory (either 1011 * because a new entry was added or because one was removed) and 1012 * listeners of fvp about the rename. */ 1013 VN_KNOTE(tdvp, NOTE_WRITE); 1014 VN_KNOTE(fvp, NOTE_RENAME); 1015 1016 error = 0; 1017 1018 out: 1019 if (fdnode != tdnode) 1020 VOP_UNLOCK(fdvp); 1021 1022 out_unlocked: 1023 /* Release target nodes. */ 1024 if (tdvp == tvp) 1025 vrele(tdvp); 1026 else 1027 vput(tdvp); 1028 if (tvp != NULL) 1029 vput(tvp); 1030 1031 /* Release source nodes. */ 1032 vrele(fdvp); 1033 vrele(fvp); 1034 1035 if (newname != NULL) { 1036 tmpfs_strname_free(tmp, newname, namelen); 1037 } 1038 return error; 1039 } 1040 1041 /* --------------------------------------------------------------------- */ 1042 1043 int 1044 tmpfs_mkdir(void *v) 1045 { 1046 struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp; 1047 struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp; 1048 struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp; 1049 struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap; 1050 1051 KASSERT(vap->va_type == VDIR); 1052 1053 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 1054 } 1055 1056 /* --------------------------------------------------------------------- */ 1057 1058 int 1059 tmpfs_rmdir(void *v) 1060 { 1061 struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp; 1062 struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp; 1063 struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp; 1064 1065 int error; 1066 struct tmpfs_dirent *de; 1067 struct tmpfs_mount *tmp; 1068 struct tmpfs_node *dnode; 1069 struct tmpfs_node *node; 1070 1071 KASSERT(VOP_ISLOCKED(dvp)); 1072 KASSERT(VOP_ISLOCKED(vp)); 1073 1074 tmp = VFS_TO_TMPFS(dvp->v_mount); 1075 dnode = VP_TO_TMPFS_DIR(dvp); 1076 node = VP_TO_TMPFS_DIR(vp); 1077 error = 0; 1078 1079 /* Directories with more than two entries ('.' and '..') cannot be 1080 * removed. */ 1081 if (node->tn_size > 0) { 1082 error = ENOTEMPTY; 1083 goto out; 1084 } 1085 1086 /* This invariant holds only if we are not trying to remove "..". 1087 * We checked for that above so this is safe now. */ 1088 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode); 1089 1090 /* Get the directory entry associated with node (vp). */ 1091 de = tmpfs_dir_lookup(dnode, cnp); 1092 KASSERT(de); 1093 KASSERT(de->td_node == node); 1094 1095 /* Check flags to see if we are allowed to remove the directory. */ 1096 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) { 1097 error = EPERM; 1098 goto out; 1099 } 1100 1101 /* Detach the directory entry from the directory (dnode). */ 1102 tmpfs_dir_detach(dvp, de); 1103 1104 node->tn_links--; 1105 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1106 TMPFS_NODE_MODIFIED; 1107 node->tn_spec.tn_dir.tn_parent->tn_links--; 1108 node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \ 1109 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1110 1111 /* Release the parent. */ 1112 cache_purge(dvp); /* XXX Is this needed? */ 1113 1114 /* Free the directory entry we just deleted. Note that the node 1115 * referred by it will not be removed until the vnode is really 1116 * reclaimed. */ 1117 tmpfs_free_dirent(tmp, de, true); 1118 1119 KASSERT(node->tn_links == 0); 1120 out: 1121 /* Release the nodes. */ 1122 vput(dvp); 1123 vput(vp); 1124 PNBUF_PUT(cnp->cn_pnbuf); 1125 1126 return error; 1127 } 1128 1129 /* --------------------------------------------------------------------- */ 1130 1131 int 1132 tmpfs_symlink(void *v) 1133 { 1134 struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp; 1135 struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp; 1136 struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp; 1137 struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap; 1138 char *target = ((struct vop_symlink_args *)v)->a_target; 1139 1140 KASSERT(vap->va_type == VLNK); 1141 1142 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1143 } 1144 1145 /* --------------------------------------------------------------------- */ 1146 1147 int 1148 tmpfs_readdir(void *v) 1149 { 1150 struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp; 1151 struct uio *uio = ((struct vop_readdir_args *)v)->a_uio; 1152 int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag; 1153 off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies; 1154 int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies; 1155 1156 int error; 1157 off_t startoff; 1158 off_t cnt; 1159 struct tmpfs_node *node; 1160 1161 KASSERT(VOP_ISLOCKED(vp)); 1162 1163 /* This operation only makes sense on directory nodes. */ 1164 if (vp->v_type != VDIR) { 1165 error = ENOTDIR; 1166 goto out; 1167 } 1168 1169 node = VP_TO_TMPFS_DIR(vp); 1170 1171 startoff = uio->uio_offset; 1172 1173 cnt = 0; 1174 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 1175 error = tmpfs_dir_getdotdent(node, uio); 1176 if (error == -1) { 1177 error = 0; 1178 goto outok; 1179 } else if (error != 0) 1180 goto outok; 1181 cnt++; 1182 } 1183 1184 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 1185 error = tmpfs_dir_getdotdotdent(node, uio); 1186 if (error == -1) { 1187 error = 0; 1188 goto outok; 1189 } else if (error != 0) 1190 goto outok; 1191 cnt++; 1192 } 1193 1194 error = tmpfs_dir_getdents(node, uio, &cnt); 1195 if (error == -1) 1196 error = 0; 1197 KASSERT(error >= 0); 1198 1199 outok: 1200 /* This label assumes that startoff has been 1201 * initialized. If the compiler didn't spit out warnings, we'd 1202 * simply make this one be 'out' and drop 'outok'. */ 1203 1204 if (eofflag != NULL) 1205 *eofflag = 1206 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1207 1208 /* Update NFS-related variables. */ 1209 if (error == 0 && cookies != NULL && ncookies != NULL) { 1210 off_t i; 1211 off_t off = startoff; 1212 struct tmpfs_dirent *de = NULL; 1213 1214 *ncookies = cnt; 1215 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 1216 1217 for (i = 0; i < cnt; i++) { 1218 KASSERT(off != TMPFS_DIRCOOKIE_EOF); 1219 if (off == TMPFS_DIRCOOKIE_DOT) { 1220 off = TMPFS_DIRCOOKIE_DOTDOT; 1221 } else { 1222 if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1223 de = TAILQ_FIRST(&node->tn_spec. 1224 tn_dir.tn_dir); 1225 } else if (de != NULL) { 1226 de = TAILQ_NEXT(de, td_entries); 1227 } else { 1228 de = tmpfs_dir_lookupbycookie(node, 1229 off); 1230 KASSERT(de != NULL); 1231 de = TAILQ_NEXT(de, td_entries); 1232 } 1233 if (de == NULL) { 1234 off = TMPFS_DIRCOOKIE_EOF; 1235 } else { 1236 off = tmpfs_dircookie(de); 1237 } 1238 } 1239 1240 (*cookies)[i] = off; 1241 } 1242 KASSERT(uio->uio_offset == off); 1243 } 1244 1245 out: 1246 KASSERT(VOP_ISLOCKED(vp)); 1247 1248 return error; 1249 } 1250 1251 /* --------------------------------------------------------------------- */ 1252 1253 int 1254 tmpfs_readlink(void *v) 1255 { 1256 struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp; 1257 struct uio *uio = ((struct vop_readlink_args *)v)->a_uio; 1258 1259 int error; 1260 struct tmpfs_node *node; 1261 1262 KASSERT(VOP_ISLOCKED(vp)); 1263 KASSERT(uio->uio_offset == 0); 1264 KASSERT(vp->v_type == VLNK); 1265 1266 node = VP_TO_TMPFS_NODE(vp); 1267 1268 error = uiomove(node->tn_spec.tn_lnk.tn_link, 1269 MIN(node->tn_size, uio->uio_resid), uio); 1270 node->tn_status |= TMPFS_NODE_ACCESSED; 1271 1272 KASSERT(VOP_ISLOCKED(vp)); 1273 1274 return error; 1275 } 1276 1277 /* --------------------------------------------------------------------- */ 1278 1279 int 1280 tmpfs_inactive(void *v) 1281 { 1282 struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp; 1283 1284 struct tmpfs_node *node; 1285 1286 KASSERT(VOP_ISLOCKED(vp)); 1287 1288 node = VP_TO_TMPFS_NODE(vp); 1289 *((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0); 1290 VOP_UNLOCK(vp); 1291 1292 return 0; 1293 } 1294 1295 /* --------------------------------------------------------------------- */ 1296 1297 int 1298 tmpfs_reclaim(void *v) 1299 { 1300 struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp; 1301 1302 struct tmpfs_mount *tmp; 1303 struct tmpfs_node *node; 1304 1305 node = VP_TO_TMPFS_NODE(vp); 1306 tmp = VFS_TO_TMPFS(vp->v_mount); 1307 1308 cache_purge(vp); 1309 tmpfs_free_vp(vp); 1310 1311 /* If the node referenced by this vnode was deleted by the user, 1312 * we must free its associated data structures (now that the vnode 1313 * is being reclaimed). */ 1314 if (node->tn_links == 0) 1315 tmpfs_free_node(tmp, node); 1316 1317 KASSERT(vp->v_data == NULL); 1318 1319 return 0; 1320 } 1321 1322 /* --------------------------------------------------------------------- */ 1323 1324 int 1325 tmpfs_print(void *v) 1326 { 1327 struct vnode *vp = ((struct vop_print_args *)v)->a_vp; 1328 1329 struct tmpfs_node *node; 1330 1331 node = VP_TO_TMPFS_NODE(vp); 1332 1333 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n", 1334 node, node->tn_flags, node->tn_links); 1335 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX 1336 ", status 0x%x", 1337 node->tn_mode, node->tn_uid, node->tn_gid, 1338 (uintmax_t)node->tn_size, node->tn_status); 1339 if (vp->v_type == VFIFO) 1340 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v); 1341 printf("\n"); 1342 1343 return 0; 1344 } 1345 1346 /* --------------------------------------------------------------------- */ 1347 1348 int 1349 tmpfs_pathconf(void *v) 1350 { 1351 int name = ((struct vop_pathconf_args *)v)->a_name; 1352 register_t *retval = ((struct vop_pathconf_args *)v)->a_retval; 1353 1354 int error; 1355 1356 error = 0; 1357 1358 switch (name) { 1359 case _PC_LINK_MAX: 1360 *retval = LINK_MAX; 1361 break; 1362 1363 case _PC_NAME_MAX: 1364 *retval = NAME_MAX; 1365 break; 1366 1367 case _PC_PATH_MAX: 1368 *retval = PATH_MAX; 1369 break; 1370 1371 case _PC_PIPE_BUF: 1372 *retval = PIPE_BUF; 1373 break; 1374 1375 case _PC_CHOWN_RESTRICTED: 1376 *retval = 1; 1377 break; 1378 1379 case _PC_NO_TRUNC: 1380 *retval = 1; 1381 break; 1382 1383 case _PC_SYNC_IO: 1384 *retval = 1; 1385 break; 1386 1387 case _PC_FILESIZEBITS: 1388 *retval = 0; /* XXX Don't know which value should I return. */ 1389 break; 1390 1391 default: 1392 error = EINVAL; 1393 } 1394 1395 return error; 1396 } 1397 1398 /* --------------------------------------------------------------------- */ 1399 1400 int 1401 tmpfs_advlock(void *v) 1402 { 1403 struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp; 1404 1405 struct tmpfs_node *node; 1406 1407 node = VP_TO_TMPFS_NODE(vp); 1408 1409 return lf_advlock(v, &node->tn_lockf, node->tn_size); 1410 } 1411 1412 /* --------------------------------------------------------------------- */ 1413 1414 int 1415 tmpfs_getpages(void *v) 1416 { 1417 struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp; 1418 voff_t offset = ((struct vop_getpages_args *)v)->a_offset; 1419 struct vm_page **m = ((struct vop_getpages_args *)v)->a_m; 1420 int *count = ((struct vop_getpages_args *)v)->a_count; 1421 int centeridx = ((struct vop_getpages_args *)v)->a_centeridx; 1422 vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type; 1423 int advice = ((struct vop_getpages_args *)v)->a_advice; 1424 int flags = ((struct vop_getpages_args *)v)->a_flags; 1425 1426 int error; 1427 int i; 1428 struct tmpfs_node *node; 1429 struct uvm_object *uobj; 1430 int npages = *count; 1431 1432 KASSERT(vp->v_type == VREG); 1433 KASSERT(mutex_owned(&vp->v_interlock)); 1434 1435 node = VP_TO_TMPFS_NODE(vp); 1436 uobj = node->tn_spec.tn_reg.tn_aobj; 1437 1438 /* We currently don't rely on PGO_PASTEOF. */ 1439 1440 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) { 1441 if ((flags & PGO_LOCKED) == 0) 1442 mutex_exit(&vp->v_interlock); 1443 return EINVAL; 1444 } 1445 1446 if (vp->v_size < offset + (npages << PAGE_SHIFT)) { 1447 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT; 1448 } 1449 1450 if ((flags & PGO_LOCKED) != 0) 1451 return EBUSY; 1452 1453 if ((flags & PGO_NOTIMESTAMP) == 0) { 1454 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 1455 node->tn_status |= TMPFS_NODE_ACCESSED; 1456 1457 if ((access_type & VM_PROT_WRITE) != 0) 1458 node->tn_status |= TMPFS_NODE_MODIFIED; 1459 } 1460 1461 mutex_exit(&vp->v_interlock); 1462 1463 /* 1464 * Make sure that the array on which we will store the 1465 * gotten pages is clean. Otherwise uao_get (pointed to by 1466 * the pgo_get below) gets confused and does not return the 1467 * appropriate pages. 1468 * 1469 * XXX This shall be revisited when kern/32166 is addressed 1470 * because the loop to clean m[i] will most likely be redundant 1471 * as well as the PGO_ALLPAGES flag. 1472 */ 1473 if (m != NULL) 1474 for (i = 0; i < npages; i++) 1475 m[i] = NULL; 1476 mutex_enter(&uobj->vmobjlock); 1477 error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx, 1478 access_type, advice, flags | PGO_ALLPAGES); 1479 #if defined(DEBUG) 1480 { 1481 /* Make sure that all the pages we return are valid. */ 1482 int dbgi; 1483 if (error == 0 && m != NULL) 1484 for (dbgi = 0; dbgi < npages; dbgi++) 1485 KASSERT(m[dbgi] != NULL); 1486 } 1487 #endif 1488 1489 return error; 1490 } 1491 1492 /* --------------------------------------------------------------------- */ 1493 1494 int 1495 tmpfs_putpages(void *v) 1496 { 1497 struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp; 1498 voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo; 1499 voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi; 1500 int flags = ((struct vop_putpages_args *)v)->a_flags; 1501 1502 int error; 1503 struct tmpfs_node *node; 1504 struct uvm_object *uobj; 1505 1506 KASSERT(mutex_owned(&vp->v_interlock)); 1507 1508 node = VP_TO_TMPFS_NODE(vp); 1509 1510 if (vp->v_type != VREG) { 1511 mutex_exit(&vp->v_interlock); 1512 return 0; 1513 } 1514 1515 uobj = node->tn_spec.tn_reg.tn_aobj; 1516 mutex_exit(&vp->v_interlock); 1517 1518 mutex_enter(&uobj->vmobjlock); 1519 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags); 1520 1521 /* XXX mtime */ 1522 1523 return error; 1524 } 1525