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