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