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