tmpfs_vnops.c revision 170808
1/* $NetBSD: tmpfs_vnops.c,v 1.20 2006/01/26 20:07:34 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, 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40/* 41 * tmpfs vnode interface. 42 */ 43#include <sys/cdefs.h> 44__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_vnops.c 170808 2007-06-16 01:56:05Z delphij $"); 45 46#include <sys/param.h> 47#include <sys/fcntl.h> 48#include <sys/lockf.h> 49#include <sys/namei.h> 50#include <sys/priv.h> 51#include <sys/proc.h> 52#include <sys/resourcevar.h> 53#include <sys/stat.h> 54#include <sys/systm.h> 55#include <sys/unistd.h> 56#include <sys/vnode.h> 57 58#include <vm/vm.h> 59#include <vm/vm_object.h> 60#include <vm/vm_page.h> 61#include <vm/vm_pager.h> 62#include <sys/sched.h> 63#include <sys/sf_buf.h> 64#include <machine/_inttypes.h> 65 66#include <fs/fifofs/fifo.h> 67#include <fs/tmpfs/tmpfs_vnops.h> 68#include <fs/tmpfs/tmpfs.h> 69 70/* --------------------------------------------------------------------- */ 71 72/* 73 * vnode operations vector used for files stored in a tmpfs file system. 74 */ 75struct vop_vector tmpfs_vnodeop_entries = { 76 .vop_default = &default_vnodeops, 77 .vop_lookup = vfs_cache_lookup, 78 .vop_cachedlookup = tmpfs_lookup, 79 .vop_create = tmpfs_create, 80 .vop_mknod = tmpfs_mknod, 81 .vop_open = tmpfs_open, 82 .vop_close = tmpfs_close, 83 .vop_access = tmpfs_access, 84 .vop_getattr = tmpfs_getattr, 85 .vop_setattr = tmpfs_setattr, 86 .vop_read = tmpfs_read, 87 .vop_write = tmpfs_write, 88 .vop_fsync = tmpfs_fsync, 89 .vop_remove = tmpfs_remove, 90 .vop_link = tmpfs_link, 91 .vop_rename = tmpfs_rename, 92 .vop_mkdir = tmpfs_mkdir, 93 .vop_rmdir = tmpfs_rmdir, 94 .vop_symlink = tmpfs_symlink, 95 .vop_readdir = tmpfs_readdir, 96 .vop_readlink = tmpfs_readlink, 97 .vop_inactive = tmpfs_inactive, 98 .vop_reclaim = tmpfs_reclaim, 99 .vop_print = tmpfs_print, 100 .vop_pathconf = tmpfs_pathconf, 101 .vop_advlock = tmpfs_advlock, 102 .vop_bmap = VOP_EOPNOTSUPP, 103}; 104 105/* --------------------------------------------------------------------- */ 106 107int 108tmpfs_lookup(struct vop_cachedlookup_args *v) 109{ 110 struct vnode *dvp = v->a_dvp; 111 struct vnode **vpp = v->a_vpp; 112 struct componentname *cnp = v->a_cnp; 113 struct thread *td = cnp->cn_thread; 114 115 int error; 116 struct tmpfs_dirent *de; 117 struct tmpfs_node *dnode; 118 119 dnode = VP_TO_TMPFS_DIR(dvp); 120 *vpp = NULLVP; 121 122 /* Check accessibility of requested node as a first step. */ 123 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td); 124 if (error != 0) 125 goto out; 126 127 /* We cannot be requesting the parent directory of the root node. */ 128 MPASS(IMPLIES(dnode->tn_type == VDIR && 129 dnode->tn_dir.tn_parent == dnode, 130 !(cnp->cn_flags & ISDOTDOT))); 131 132 if (cnp->cn_flags & ISDOTDOT) { 133 VOP_UNLOCK(dvp, 0, td); 134 135 /* Allocate a new vnode on the matching entry. */ 136 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent, vpp, td); 137 138 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 139 140 dnode->tn_dir.tn_parent->tn_lookup_dirent = NULL; 141 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 142 VREF(dvp); 143 *vpp = dvp; 144 dnode->tn_lookup_dirent = NULL; 145 error = 0; 146 } else { 147 de = tmpfs_dir_lookup(dnode, cnp); 148 if (de == NULL) { 149 /* The entry was not found in the directory. 150 * This is OK if we are creating or renaming an 151 * entry and are working on the last component of 152 * the path name. */ 153 if ((cnp->cn_flags & ISLASTCN) && 154 (cnp->cn_nameiop == CREATE || \ 155 cnp->cn_nameiop == RENAME)) { 156 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 157 cnp->cn_thread); 158 if (error != 0) 159 goto out; 160 161 /* Keep the component name in the buffer for 162 * future uses. */ 163 cnp->cn_flags |= SAVENAME; 164 165 error = EJUSTRETURN; 166 } else 167 error = ENOENT; 168 } else { 169 struct tmpfs_node *tnode; 170 171 /* The entry was found, so get its associated 172 * tmpfs_node. */ 173 tnode = de->td_node; 174 175 /* If we are not at the last path component and 176 * found a non-directory or non-link entry (which 177 * may itself be pointing to a directory), raise 178 * an error. */ 179 if ((tnode->tn_type != VDIR && 180 tnode->tn_type != VLNK) && 181 !(cnp->cn_flags & ISLASTCN)) { 182 error = ENOTDIR; 183 goto out; 184 } 185 186 /* If we are deleting or renaming the entry, keep 187 * track of its tmpfs_dirent so that it can be 188 * easily deleted later. */ 189 if ((cnp->cn_flags & ISLASTCN) && 190 (cnp->cn_nameiop == DELETE || 191 cnp->cn_nameiop == RENAME)) { 192 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 193 cnp->cn_thread); 194 if (error != 0) 195 goto out; 196 197 /* Allocate a new vnode on the matching entry. */ 198 error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp, td); 199 if (error != 0) 200 goto out; 201 202 if ((dnode->tn_mode & S_ISTXT) && 203 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) && 204 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) { 205 error = EPERM; 206 vput(*vpp); 207 *vpp = NULL; 208 goto out; 209 } 210 tnode->tn_lookup_dirent = de; 211 cnp->cn_flags |= SAVENAME; 212 } 213 else 214 error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp, td); 215 216 } 217 } 218 219 /* Store the result of this lookup in the cache. Avoid this if the 220 * request was for creation, as it does not improve timings on 221 * emprical tests. */ 222 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) 223 cache_enter(dvp, *vpp, cnp); 224 225out: 226 /* If there were no errors, *vpp cannot be null and it must be 227 * locked. */ 228 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp, td))); 229 230 return error; 231} 232 233/* --------------------------------------------------------------------- */ 234 235int 236tmpfs_create(struct vop_create_args *v) 237{ 238 struct vnode *dvp = v->a_dvp; 239 struct vnode **vpp = v->a_vpp; 240 struct componentname *cnp = v->a_cnp; 241 struct vattr *vap = v->a_vap; 242 243 MPASS(vap->va_type == VREG || vap->va_type == VSOCK); 244 245 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 246} 247/* --------------------------------------------------------------------- */ 248 249int 250tmpfs_mknod(struct vop_mknod_args *v) 251{ 252 struct vnode *dvp = v->a_dvp; 253 struct vnode **vpp = v->a_vpp; 254 struct componentname *cnp = v->a_cnp; 255 struct vattr *vap = v->a_vap; 256 257 if (vap->va_type != VBLK && vap->va_type != VCHR && 258 vap->va_type != VFIFO) 259 return EINVAL; 260 261 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 262} 263 264/* --------------------------------------------------------------------- */ 265 266int 267tmpfs_open(struct vop_open_args *v) 268{ 269 struct vnode *vp = v->a_vp; 270 int mode = v->a_mode; 271 272 int error; 273 struct tmpfs_node *node; 274 275 MPASS(VOP_ISLOCKED(vp, v->a_td)); 276 277 node = VP_TO_TMPFS_NODE(vp); 278 279 /* The file is still active but all its names have been removed 280 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as 281 * it is about to die. */ 282 if (node->tn_links < 1) 283 return (ENOENT); 284 285 /* If the file is marked append-only, deny write requests. */ 286 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE) 287 error = EPERM; 288 else { 289 error = 0; 290 vnode_create_vobject(vp, node->tn_size, v->a_td); 291 } 292 293 MPASS(VOP_ISLOCKED(vp, v->a_td)); 294 return error; 295} 296 297/* --------------------------------------------------------------------- */ 298 299int 300tmpfs_close(struct vop_close_args *v) 301{ 302 struct vnode *vp = v->a_vp; 303 304 struct tmpfs_node *node; 305 306 MPASS(VOP_ISLOCKED(vp, v->a_td)); 307 308 node = VP_TO_TMPFS_NODE(vp); 309 310 if (node->tn_links > 0) { 311 /* Update node times. No need to do it if the node has 312 * been deleted, because it will vanish after we return. */ 313 tmpfs_update(vp); 314 } 315 316 return 0; 317} 318 319/* --------------------------------------------------------------------- */ 320 321int 322tmpfs_access(struct vop_access_args *v) 323{ 324 struct vnode *vp = v->a_vp; 325 int mode = v->a_mode; 326 struct ucred *cred = v->a_cred; 327 328 int error; 329 struct tmpfs_node *node; 330 331 MPASS(VOP_ISLOCKED(vp, v->a_td)); 332 333 node = VP_TO_TMPFS_NODE(vp); 334 335 switch (vp->v_type) { 336 case VDIR: 337 /* FALLTHROUGH */ 338 case VLNK: 339 /* FALLTHROUGH */ 340 case VREG: 341 if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) { 342 error = EROFS; 343 goto out; 344 } 345 break; 346 347 case VBLK: 348 /* FALLTHROUGH */ 349 case VCHR: 350 /* FALLTHROUGH */ 351 case VSOCK: 352 /* FALLTHROUGH */ 353 case VFIFO: 354 break; 355 356 default: 357 error = EINVAL; 358 goto out; 359 } 360 361 if (mode & VWRITE && node->tn_flags & IMMUTABLE) { 362 error = EPERM; 363 goto out; 364 } 365 366 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, 367 node->tn_gid, mode, cred, NULL); 368 369out: 370 MPASS(VOP_ISLOCKED(vp, v->a_td)); 371 372 return error; 373} 374 375/* --------------------------------------------------------------------- */ 376 377int 378tmpfs_getattr(struct vop_getattr_args *v) 379{ 380 struct vnode *vp = v->a_vp; 381 struct vattr *vap = v->a_vap; 382 383 struct tmpfs_node *node; 384 385 node = VP_TO_TMPFS_NODE(vp); 386 387 VATTR_NULL(vap); 388 389 tmpfs_update(vp); 390 391 vap->va_type = vp->v_type; 392 vap->va_mode = node->tn_mode; 393 vap->va_nlink = node->tn_links; 394 vap->va_uid = node->tn_uid; 395 vap->va_gid = node->tn_gid; 396 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 397 vap->va_fileid = node->tn_id; 398 vap->va_size = node->tn_size; 399 vap->va_blocksize = PAGE_SIZE; 400 vap->va_atime = node->tn_atime; 401 vap->va_mtime = node->tn_mtime; 402 vap->va_ctime = node->tn_ctime; 403 vap->va_birthtime = node->tn_birthtime; 404 vap->va_gen = node->tn_gen; 405 vap->va_flags = node->tn_flags; 406 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 407 node->tn_rdev : VNOVAL; 408 vap->va_bytes = round_page(node->tn_size); 409 vap->va_filerev = VNOVAL; 410 vap->va_vaflags = 0; 411 vap->va_spare = VNOVAL; /* XXX */ 412 413 return 0; 414} 415 416/* --------------------------------------------------------------------- */ 417 418/* XXX Should this operation be atomic? I think it should, but code in 419 * XXX other places (e.g., ufs) doesn't seem to be... */ 420int 421tmpfs_setattr(struct vop_setattr_args *v) 422{ 423 struct vnode *vp = v->a_vp; 424 struct vattr *vap = v->a_vap; 425 struct ucred *cred = v->a_cred; 426 struct thread *l = v->a_td; 427 428 int error; 429 430 MPASS(VOP_ISLOCKED(vp, l)); 431 432 error = 0; 433 434 /* Abort if any unsettable attribute is given. */ 435 if (vap->va_type != VNON || 436 vap->va_nlink != VNOVAL || 437 vap->va_fsid != VNOVAL || 438 vap->va_fileid != VNOVAL || 439 vap->va_blocksize != VNOVAL || 440 vap->va_gen != VNOVAL || 441 vap->va_rdev != VNOVAL || 442 vap->va_bytes != VNOVAL) 443 error = EINVAL; 444 445 if (error == 0 && (vap->va_flags != VNOVAL)) 446 error = tmpfs_chflags(vp, vap->va_flags, cred, l); 447 448 if (error == 0 && (vap->va_size != VNOVAL)) 449 error = tmpfs_chsize(vp, vap->va_size, cred, l); 450 451 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 452 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, 453 l); 454 455 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) 456 error = tmpfs_chmod(vp, vap->va_mode, cred, l); 457 458 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL && 459 vap->va_atime.tv_nsec != VNOVAL) || 460 (vap->va_mtime.tv_sec != VNOVAL && 461 vap->va_mtime.tv_nsec != VNOVAL) || 462 (vap->va_birthtime.tv_sec != VNOVAL && 463 vap->va_birthtime.tv_nsec != VNOVAL))) 464 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 465 &vap->va_birthtime, vap->va_vaflags, cred, l); 466 467 /* Update the node times. We give preference to the error codes 468 * generated by this function rather than the ones that may arise 469 * from tmpfs_update. */ 470 tmpfs_update(vp); 471 472 MPASS(VOP_ISLOCKED(vp, l)); 473 474 return error; 475} 476 477/* --------------------------------------------------------------------- */ 478static int 479tmpfs_uio_xfer(struct tmpfs_mount *tmp, struct tmpfs_node *node, 480 struct uio *uio, vm_object_t uobj) 481{ 482 struct sf_buf *sf; 483 vm_pindex_t idx; 484 vm_offset_t d; 485 vm_page_t m; 486 size_t len; 487 int error = 0; 488 489 /* uobj - locked by caller */ 490 491 VM_OBJECT_LOCK(uobj); 492 vm_object_pip_add(uobj, 1); 493 while (error == 0 && uio->uio_resid > 0) { 494 if (node->tn_size <= uio->uio_offset) 495 break; 496 497 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid); 498 if (len == 0) 499 break; 500 501 idx = OFF_TO_IDX(uio->uio_offset); 502 d = uio->uio_offset - IDX_TO_OFF(idx); 503 len = MIN(len, (PAGE_SIZE - d)); 504 m = vm_page_grab(uobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 505 if (uio->uio_rw == UIO_READ && m->valid != VM_PAGE_BITS_ALL) 506 if (vm_pager_get_pages(uobj, &m, 1, 0) != VM_PAGER_OK) 507 vm_page_zero_invalid(m, TRUE); 508 VM_OBJECT_UNLOCK(uobj); 509 sched_pin(); 510 sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 511 error = uiomove((void *)(sf_buf_kva(sf) + d), len, uio); 512 sf_buf_free(sf); 513 sched_unpin(); 514 VM_OBJECT_LOCK(uobj); 515 vm_page_lock_queues(); 516 if (error == 0 && uio->uio_rw == UIO_WRITE) { 517 vm_page_set_validclean(m, d, len); 518 vm_page_zero_invalid(m, TRUE); 519 vm_page_dirty(m); 520 } 521 vm_page_activate(m); 522 vm_page_wakeup(m); 523 vm_page_unlock_queues(); 524 } 525 vm_object_pip_subtract(uobj, 1); 526 VM_OBJECT_UNLOCK(uobj); 527 return error; 528} 529 530int 531tmpfs_read(struct vop_read_args *v) 532{ 533 struct vnode *vp = v->a_vp; 534 struct uio *uio = v->a_uio; 535 536 struct tmpfs_node *node; 537 vm_object_t uobj; 538 539 int error; 540 541 node = VP_TO_TMPFS_NODE(vp); 542 543 if (vp->v_type != VREG) { 544 error = EISDIR; 545 goto out; 546 } 547 548 if (uio->uio_offset < 0) { 549 error = EINVAL; 550 goto out; 551 } 552 553 node->tn_status |= TMPFS_NODE_ACCESSED; 554 555 uobj = node->tn_reg.tn_aobj; 556 error = tmpfs_uio_xfer(VFS_TO_TMPFS(vp->v_mount), node, uio, uobj); 557 558out: 559 560 return error; 561} 562 563/* --------------------------------------------------------------------- */ 564 565int 566tmpfs_write(struct vop_write_args *v) 567{ 568 struct vnode *vp = v->a_vp; 569 struct uio *uio = v->a_uio; 570 int ioflag = v->a_ioflag; 571 struct thread *td = uio->uio_td; 572 573 boolean_t extended; 574 int error; 575 off_t oldsize; 576 struct tmpfs_node *node; 577 vm_object_t uobj; 578 579 node = VP_TO_TMPFS_NODE(vp); 580 oldsize = node->tn_size; 581 582 if (uio->uio_offset < 0 || vp->v_type != VREG) { 583 error = EINVAL; 584 goto out; 585 } 586 587 if (uio->uio_resid == 0) { 588 error = 0; 589 goto out; 590 } 591 592 if (ioflag & IO_APPEND) 593 uio->uio_offset = node->tn_size; 594 595 if (uio->uio_offset + uio->uio_resid > 596 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 597 return (EFBIG); 598 599 if (vp->v_type == VREG && td != NULL) { 600 PROC_LOCK(td->td_proc); 601 if (uio->uio_offset + uio->uio_resid > 602 lim_cur(td->td_proc, RLIMIT_FSIZE)) { 603 psignal(td->td_proc, SIGXFSZ); 604 PROC_UNLOCK(td->td_proc); 605 return (EFBIG); 606 } 607 PROC_UNLOCK(td->td_proc); 608 } 609 610 extended = uio->uio_offset + uio->uio_resid > node->tn_size; 611 if (extended) { 612 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid); 613 if (error != 0) 614 goto out; 615 } 616 617 uobj = node->tn_reg.tn_aobj; 618 error = tmpfs_uio_xfer(VFS_TO_TMPFS(vp->v_mount), node, uio, uobj); 619 620 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 621 (extended ? TMPFS_NODE_CHANGED : 0); 622 623 if (node->tn_mode & (S_ISUID | S_ISGID)) { 624 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0)) 625 node->tn_mode &= ~(S_ISUID | S_ISGID); 626 } 627 628 if (error != 0) 629 (void)tmpfs_reg_resize(vp, oldsize); 630 631out: 632 MPASS(IMPLIES(error == 0, uio->uio_resid == 0)); 633 MPASS(IMPLIES(error != 0, oldsize == node->tn_size)); 634 635 return error; 636} 637 638/* --------------------------------------------------------------------- */ 639 640int 641tmpfs_fsync(struct vop_fsync_args *v) 642{ 643 struct vnode *vp = v->a_vp; 644 645 MPASS(VOP_ISLOCKED(vp, v->a_td)); 646 647 tmpfs_update(vp); 648 649 return 0; 650} 651 652/* --------------------------------------------------------------------- */ 653 654int 655tmpfs_remove(struct vop_remove_args *v) 656{ 657 struct vnode *dvp = v->a_dvp; 658 struct vnode *vp = v->a_vp; 659 660 int error; 661 struct tmpfs_dirent *de; 662 struct tmpfs_mount *tmp; 663 struct tmpfs_node *dnode; 664 struct tmpfs_node *node; 665 666 MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread)); 667 MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread)); 668 669 if (vp->v_type == VDIR) { 670 error = EISDIR; 671 goto out; 672 } 673 674 dnode = VP_TO_TMPFS_DIR(dvp); 675 node = VP_TO_TMPFS_NODE(vp); 676 tmp = VFS_TO_TMPFS(vp->v_mount); 677 de = node->tn_lookup_dirent; 678 MPASS(de != NULL); 679 680 /* Files marked as immutable or append-only cannot be deleted. */ 681 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 682 (dnode->tn_flags & APPEND)) { 683 error = EPERM; 684 goto out; 685 } 686 687 /* Remove the entry from the directory; as it is a file, we do not 688 * have to change the number of hard links of the directory. */ 689 tmpfs_dir_detach(dvp, de); 690 691 /* Free the directory entry we just deleted. Note that the node 692 * referred by it will not be removed until the vnode is really 693 * reclaimed. */ 694 tmpfs_free_dirent(tmp, de, TRUE); 695 696 if (node->tn_links > 0) 697 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 698 TMPFS_NODE_MODIFIED; 699 error = 0; 700 701out: 702 703 return error; 704} 705 706/* --------------------------------------------------------------------- */ 707 708int 709tmpfs_link(struct vop_link_args *v) 710{ 711 struct vnode *dvp = v->a_tdvp; 712 struct vnode *vp = v->a_vp; 713 struct componentname *cnp = v->a_cnp; 714 715 int error; 716 struct tmpfs_dirent *de; 717 struct tmpfs_node *dnode; 718 struct tmpfs_node *node; 719 720 MPASS(VOP_ISLOCKED(dvp, cnp->cn_thread)); 721 MPASS(cnp->cn_flags & HASBUF); 722 MPASS(dvp != vp); /* XXX When can this be false? */ 723 724 dnode = VP_TO_TMPFS_DIR(dvp); 725 node = VP_TO_TMPFS_NODE(vp); 726 727 /* XXX: Why aren't the following two tests done by the caller? */ 728 729 /* Hard links of directories are forbidden. */ 730 if (vp->v_type == VDIR) { 731 error = EPERM; 732 goto out; 733 } 734 735 /* Cannot create cross-device links. */ 736 if (dvp->v_mount != vp->v_mount) { 737 error = EXDEV; 738 goto out; 739 } 740 741 /* Ensure that we do not overflow the maximum number of links imposed 742 * by the system. */ 743 MPASS(node->tn_links <= LINK_MAX); 744 if (node->tn_links == LINK_MAX) { 745 error = EMLINK; 746 goto out; 747 } 748 749 /* We cannot create links of files marked immutable or append-only. */ 750 if (node->tn_flags & (IMMUTABLE | APPEND)) { 751 error = EPERM; 752 goto out; 753 } 754 755 /* Allocate a new directory entry to represent the node. */ 756 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 757 cnp->cn_nameptr, cnp->cn_namelen, &de); 758 if (error != 0) 759 goto out; 760 761 /* Insert the new directory entry into the appropriate directory. */ 762 tmpfs_dir_attach(dvp, de); 763 764 /* vp link count has changed, so update node times. */ 765 node->tn_status |= TMPFS_NODE_CHANGED; 766 tmpfs_update(vp); 767 768 error = 0; 769out: 770 771 return error; 772} 773 774/* --------------------------------------------------------------------- */ 775 776int 777tmpfs_rename(struct vop_rename_args *v) 778{ 779 struct vnode *fdvp = v->a_fdvp; 780 struct vnode *fvp = v->a_fvp; 781 struct componentname *fcnp = v->a_fcnp; 782 struct vnode *tdvp = v->a_tdvp; 783 struct vnode *tvp = v->a_tvp; 784 struct componentname *tcnp = v->a_tcnp; 785 struct tmpfs_node *tnode = 0; /* pacify gcc */ 786 787 char *newname; 788 int error; 789 struct tmpfs_dirent *de; 790 struct tmpfs_mount *tmp; 791 struct tmpfs_node *fdnode; 792 struct tmpfs_node *fnode; 793 struct tmpfs_node *tdnode; 794 795 MPASS(VOP_ISLOCKED(tdvp, tcnp->cn_thread)); 796 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp, tcnp->cn_thread))); 797 MPASS(fcnp->cn_flags & HASBUF); 798 MPASS(tcnp->cn_flags & HASBUF); 799 800 fdnode = VP_TO_TMPFS_DIR(fdvp); 801 fnode = VP_TO_TMPFS_NODE(fvp); 802 de = fnode->tn_lookup_dirent; 803 804 /* Disallow cross-device renames. 805 * XXX Why isn't this done by the caller? */ 806 if (fvp->v_mount != tdvp->v_mount || 807 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 808 error = EXDEV; 809 goto out; 810 } 811 812 tmp = VFS_TO_TMPFS(tdvp->v_mount); 813 tdnode = VP_TO_TMPFS_DIR(tdvp); 814 815 /* If source and target are the same file, there is nothing to do. */ 816 if (fvp == tvp) { 817 error = 0; 818 goto out; 819 } 820 821 /* Avoid manipulating '.' and '..' entries. */ 822 if (de == NULL) { 823 MPASS(fvp->v_type == VDIR); 824 error = EINVAL; 825 goto out; 826 } 827 MPASS(de->td_node == fnode); 828 829 /* If re-naming a directory to another preexisting directory 830 * ensure that the target directory is empty so that its 831 * removal causes no side effects. 832 * Kern_rename gurantees the destination to be a directory 833 * if the source is one. */ 834 if (tvp != NULL) { 835 tnode = VP_TO_TMPFS_NODE(tvp); 836 837 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 838 (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 839 error = EPERM; 840 goto out; 841 } 842 843 if ((de->td_node->tn_type == VDIR) && (tnode->tn_size > 0)) { 844 error = ENOTEMPTY; 845 goto out; 846 } 847 } 848 849 /* If we need to move the directory between entries, lock the 850 * source so that we can safely operate on it. */ 851 if (fdnode != tdnode) { 852 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, tcnp->cn_thread); 853 if (error != 0) 854 goto out; 855 } 856 857 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) 858 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 859 error = EPERM; 860 goto out_locked; 861 } 862 863 /* Ensure that we have enough memory to hold the new name, if it 864 * has to be changed. */ 865 if (fcnp->cn_namelen != tcnp->cn_namelen || 866 memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { 867 newname = tmpfs_str_zone_alloc(&tmp->tm_str_pool, M_WAITOK, 868 tcnp->cn_namelen); 869 if (newname == NULL) { 870 error = ENOSPC; 871 goto out_locked; 872 } 873 } else 874 newname = NULL; 875 876 /* If the node is being moved to another directory, we have to do 877 * the move. */ 878 if (fdnode != tdnode) { 879 /* In case we are moving a directory, we have to adjust its 880 * parent to point to the new parent. */ 881 if (de->td_node->tn_type == VDIR) { 882 struct tmpfs_node *n; 883 884 /* Ensure the target directory is not a child of the 885 * directory being moved. Otherwise, we'd end up 886 * with stale nodes. */ 887 n = tdnode; 888 while (n != n->tn_dir.tn_parent) { 889 if (n == fnode) { 890 error = EINVAL; 891 if (newname != NULL) 892 tmpfs_str_zone_free(&tmp->tm_str_pool, 893 newname, tcnp->cn_namelen); 894 goto out_locked; 895 } 896 n = n->tn_dir.tn_parent; 897 } 898 899 /* Adjust the parent pointer. */ 900 TMPFS_VALIDATE_DIR(fnode); 901 de->td_node->tn_dir.tn_parent = tdnode; 902 903 /* As a result of changing the target of the '..' 904 * entry, the link count of the source and target 905 * directories has to be adjusted. */ 906 fdnode->tn_links--; 907 tdnode->tn_links++; 908 } 909 910 /* Do the move: just remove the entry from the source directory 911 * and insert it into the target one. */ 912 tmpfs_dir_detach(fdvp, de); 913 tmpfs_dir_attach(tdvp, de); 914 } 915 916 /* If the name has changed, we need to make it effective by changing 917 * it in the directory entry. */ 918 if (newname != NULL) { 919 MPASS(tcnp->cn_namelen <= MAXNAMLEN); 920 921 tmpfs_str_zone_free(&tmp->tm_str_pool, de->td_name, 922 de->td_namelen); 923 de->td_namelen = (uint16_t)tcnp->cn_namelen; 924 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen); 925 de->td_name = newname; 926 927 fnode->tn_status |= TMPFS_NODE_CHANGED; 928 tdnode->tn_status |= TMPFS_NODE_MODIFIED; 929 } 930 931 /* If we are overwriting an entry, we have to remove the old one 932 * from the target directory. */ 933 if (tvp != NULL) { 934 /* Remove the old entry from the target directory. */ 935 de = tnode->tn_lookup_dirent; 936 tmpfs_dir_detach(tdvp, de); 937 938 /* Free the directory entry we just deleted. Note that the 939 * node referred by it will not be removed until the vnode is 940 * really reclaimed. */ 941 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE); 942 } 943 944 error = 0; 945 946out_locked: 947 if (fdnode != tdnode) 948 VOP_UNLOCK(fdvp, 0, tcnp->cn_thread); 949 950out: 951 /* Release target nodes. */ 952 /* XXX: I don't understand when tdvp can be the same as tvp, but 953 * other code takes care of this... */ 954 if (tdvp == tvp) 955 vrele(tdvp); 956 else 957 vput(tdvp); 958 if (tvp != NULL) 959 vput(tvp); 960 961 /* Release source nodes. */ 962 vrele(fdvp); 963 vrele(fvp); 964 965 return error; 966} 967 968/* --------------------------------------------------------------------- */ 969 970int 971tmpfs_mkdir(struct vop_mkdir_args *v) 972{ 973 struct vnode *dvp = v->a_dvp; 974 struct vnode **vpp = v->a_vpp; 975 struct componentname *cnp = v->a_cnp; 976 struct vattr *vap = v->a_vap; 977 978 MPASS(vap->va_type == VDIR); 979 980 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 981} 982 983/* --------------------------------------------------------------------- */ 984 985int 986tmpfs_rmdir(struct vop_rmdir_args *v) 987{ 988 struct vnode *dvp = v->a_dvp; 989 struct vnode *vp = v->a_vp; 990 991 int error; 992 struct tmpfs_dirent *de; 993 struct tmpfs_mount *tmp; 994 struct tmpfs_node *dnode; 995 struct tmpfs_node *node; 996 997 MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread)); 998 MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread)); 999 1000 tmp = VFS_TO_TMPFS(dvp->v_mount); 1001 dnode = VP_TO_TMPFS_DIR(dvp); 1002 node = VP_TO_TMPFS_DIR(vp); 1003 1004 1005 /* Directories with more than two entries ('.' and '..') cannot be 1006 * removed. */ 1007 if (node->tn_size > 0) { 1008 error = ENOTEMPTY; 1009 goto out; 1010 } 1011 1012 if ((dnode->tn_flags & APPEND) 1013 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1014 error = EPERM; 1015 goto out; 1016 } 1017 1018 /* This invariant holds only if we are not trying to remove "..". 1019 * We checked for that above so this is safe now. */ 1020 MPASS(node->tn_dir.tn_parent == dnode); 1021 1022 /* Get the directory entry associated with node (vp). This was 1023 * filled by tmpfs_lookup while looking up the entry. */ 1024 de = node->tn_lookup_dirent; 1025 MPASS(TMPFS_DIRENT_MATCHES(de, 1026 v->a_cnp->cn_nameptr, 1027 v->a_cnp->cn_namelen)); 1028 1029 /* Check flags to see if we are allowed to remove the directory. */ 1030 if (dnode->tn_flags & APPEND 1031 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1032 error = EPERM; 1033 goto out; 1034 } 1035 1036 /* Detach the directory entry from the directory (dnode). */ 1037 tmpfs_dir_detach(dvp, de); 1038 1039 node->tn_links--; 1040 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1041 TMPFS_NODE_MODIFIED; 1042 node->tn_dir.tn_parent->tn_links--; 1043 node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \ 1044 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1045 1046 cache_purge(dvp); 1047 cache_purge(vp); 1048 1049 /* Free the directory entry we just deleted. Note that the node 1050 * referred by it will not be removed until the vnode is really 1051 * reclaimed. */ 1052 tmpfs_free_dirent(tmp, de, TRUE); 1053 1054 /* Release the deleted vnode (will destroy the node, notify 1055 * interested parties and clean it from the cache). */ 1056 1057 dnode->tn_status |= TMPFS_NODE_CHANGED; 1058 tmpfs_update(dvp); 1059 1060 error = 0; 1061 1062out: 1063 return error; 1064} 1065 1066/* --------------------------------------------------------------------- */ 1067 1068int 1069tmpfs_symlink(struct vop_symlink_args *v) 1070{ 1071 struct vnode *dvp = v->a_dvp; 1072 struct vnode **vpp = v->a_vpp; 1073 struct componentname *cnp = v->a_cnp; 1074 struct vattr *vap = v->a_vap; 1075 char *target = v->a_target; 1076 1077#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */ 1078 MPASS(vap->va_type == VLNK); 1079#else 1080 vap->va_type = VLNK; 1081#endif 1082 1083 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1084} 1085 1086/* --------------------------------------------------------------------- */ 1087 1088int 1089tmpfs_readdir(struct vop_readdir_args *v) 1090{ 1091 struct vnode *vp = v->a_vp; 1092 struct uio *uio = v->a_uio; 1093 int *eofflag = v->a_eofflag; 1094 u_long **cookies = v->a_cookies; 1095 int *ncookies = v->a_ncookies; 1096 1097 int error; 1098 off_t startoff; 1099 off_t cnt; 1100 struct tmpfs_node *node; 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 MPASS(error >= 0); 1137 1138outok: 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 MPASS(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_dir.tn_dirhead); 1163 } else if (de != NULL) { 1164 de = TAILQ_NEXT(de, td_entries); 1165 } else { 1166 de = tmpfs_dir_lookupbycookie(node, 1167 off); 1168 MPASS(de != NULL); 1169 de = TAILQ_NEXT(de, td_entries); 1170 } 1171 if (de == NULL) { 1172 off = TMPFS_DIRCOOKIE_EOF; 1173 } else { 1174 off = TMPFS_DIRCOOKIE(de); 1175 } 1176 } 1177 1178 (*cookies)[i] = off; 1179 } 1180 MPASS(uio->uio_offset == off); 1181 } 1182 1183out: 1184 return error; 1185} 1186 1187/* --------------------------------------------------------------------- */ 1188 1189int 1190tmpfs_readlink(struct vop_readlink_args *v) 1191{ 1192 struct vnode *vp = v->a_vp; 1193 struct uio *uio = v->a_uio; 1194 1195 int error; 1196 struct tmpfs_node *node; 1197 1198 MPASS(uio->uio_offset == 0); 1199 MPASS(vp->v_type == VLNK); 1200 1201 node = VP_TO_TMPFS_NODE(vp); 1202 1203 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid), 1204 uio); 1205 node->tn_status |= TMPFS_NODE_ACCESSED; 1206 1207 return error; 1208} 1209 1210/* --------------------------------------------------------------------- */ 1211 1212int 1213tmpfs_inactive(struct vop_inactive_args *v) 1214{ 1215 struct vnode *vp = v->a_vp; 1216 struct thread *l = v->a_td; 1217 1218 struct tmpfs_node *node; 1219 1220 MPASS(VOP_ISLOCKED(vp, l)); 1221 1222 node = VP_TO_TMPFS_NODE(vp); 1223 1224 if (node->tn_links == 0) 1225 vrecycle(vp, l); 1226 1227 return 0; 1228} 1229 1230/* --------------------------------------------------------------------- */ 1231 1232int 1233tmpfs_reclaim(struct vop_reclaim_args *v) 1234{ 1235 struct vnode *vp = v->a_vp; 1236 1237 struct tmpfs_mount *tmp; 1238 struct tmpfs_node *node; 1239 1240 node = VP_TO_TMPFS_NODE(vp); 1241 tmp = VFS_TO_TMPFS(vp->v_mount); 1242 1243 vnode_destroy_vobject(vp); 1244 cache_purge(vp); 1245 tmpfs_free_vp(vp); 1246 1247 /* If the node referenced by this vnode was deleted by the user, 1248 * we must free its associated data structures (now that the vnode 1249 * is being reclaimed). */ 1250 if (node->tn_links == 0) 1251 tmpfs_free_node(tmp, node); 1252 1253 MPASS(vp->v_data == NULL); 1254 return 0; 1255} 1256 1257/* --------------------------------------------------------------------- */ 1258 1259int 1260tmpfs_print(struct vop_print_args *v) 1261{ 1262 struct vnode *vp = v->a_vp; 1263 1264 struct tmpfs_node *node; 1265 1266 node = VP_TO_TMPFS_NODE(vp); 1267 1268 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n", 1269 node, node->tn_flags, node->tn_links); 1270 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX 1271 ", status 0x%x\n", 1272 node->tn_mode, node->tn_uid, node->tn_gid, 1273 (uintmax_t)node->tn_size, node->tn_status); 1274 1275 if (vp->v_type == VFIFO) 1276 fifo_printinfo(vp); 1277 1278 printf("\n"); 1279 1280 return 0; 1281} 1282 1283/* --------------------------------------------------------------------- */ 1284 1285int 1286tmpfs_pathconf(struct vop_pathconf_args *v) 1287{ 1288 int name = v->a_name; 1289 register_t *retval = v->a_retval; 1290 1291 int error; 1292 1293 error = 0; 1294 1295 switch (name) { 1296 case _PC_LINK_MAX: 1297 *retval = LINK_MAX; 1298 break; 1299 1300 case _PC_NAME_MAX: 1301 *retval = NAME_MAX; 1302 break; 1303 1304 case _PC_PATH_MAX: 1305 *retval = PATH_MAX; 1306 break; 1307 1308 case _PC_PIPE_BUF: 1309 *retval = PIPE_BUF; 1310 break; 1311 1312 case _PC_CHOWN_RESTRICTED: 1313 *retval = 1; 1314 break; 1315 1316 case _PC_NO_TRUNC: 1317 *retval = 1; 1318 break; 1319 1320 case _PC_SYNC_IO: 1321 *retval = 1; 1322 break; 1323 1324 case _PC_FILESIZEBITS: 1325 *retval = 0; /* XXX Don't know which value should I return. */ 1326 break; 1327 1328 default: 1329 error = EINVAL; 1330 } 1331 1332 return error; 1333} 1334 1335/* --------------------------------------------------------------------- */ 1336 1337int 1338tmpfs_advlock(struct vop_advlock_args *v) 1339{ 1340 struct vnode *vp = v->a_vp; 1341 1342 struct tmpfs_node *node; 1343 1344 node = VP_TO_TMPFS_NODE(vp); 1345 1346 return lf_advlock(v, &node->tn_lockf, node->tn_size); 1347} 1348 1349/* --------------------------------------------------------------------- */ 1350 1351int 1352tmpfs_vptofh(struct vop_vptofh_args *ap) 1353{ 1354 struct tmpfs_fid *tfhp; 1355 struct tmpfs_node *node; 1356 1357 tfhp = (struct tmpfs_fid *)ap->a_fhp; 1358 node = VP_TO_TMPFS_NODE(ap->a_vp); 1359 1360 tfhp->tf_len = sizeof(struct tmpfs_fid); 1361 tfhp->tf_id = node->tn_id; 1362 tfhp->tf_gen = node->tn_gen; 1363 1364 return (0); 1365} 1366