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