tmpfs_vnops.c revision 233385
1202719Sgabor/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */ 2202719Sgabor 3202719Sgabor/*- 4202719Sgabor * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 5202719Sgabor * All rights reserved. 6202719Sgabor * 7202719Sgabor * This code is derived from software contributed to The NetBSD Foundation 8202719Sgabor * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9202719Sgabor * 2005 program. 10202719Sgabor * 11202719Sgabor * Redistribution and use in source and binary forms, with or without 12202719Sgabor * modification, are permitted provided that the following conditions 13202719Sgabor * are met: 14202719Sgabor * 1. Redistributions of source code must retain the above copyright 15202719Sgabor * notice, this list of conditions and the following disclaimer. 16202719Sgabor * 2. Redistributions in binary form must reproduce the above copyright 17202719Sgabor * notice, this list of conditions and the following disclaimer in the 18202719Sgabor * documentation and/or other materials provided with the distribution. 19202719Sgabor * 20202719Sgabor * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21202719Sgabor * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22202719Sgabor * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23202719Sgabor * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24202719Sgabor * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25202719Sgabor * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26202719Sgabor * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27202719Sgabor * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28202719Sgabor * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29202719Sgabor * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30202719Sgabor * POSSIBILITY OF SUCH DAMAGE. 31202719Sgabor */ 32202719Sgabor 33202719Sgabor/* 34202719Sgabor * tmpfs vnode interface. 35202719Sgabor */ 36202719Sgabor#include <sys/cdefs.h> 37202719Sgabor__FBSDID("$FreeBSD: stable/9/sys/fs/tmpfs/tmpfs_vnops.c 233385 2012-03-23 18:29:09Z jhb $"); 38202719Sgabor 39202719Sgabor#include <sys/param.h> 40202719Sgabor#include <sys/fcntl.h> 41202719Sgabor#include <sys/lockf.h> 42202719Sgabor#include <sys/namei.h> 43202719Sgabor#include <sys/priv.h> 44202719Sgabor#include <sys/proc.h> 45202719Sgabor#include <sys/sched.h> 46202719Sgabor#include <sys/sf_buf.h> 47202719Sgabor#include <sys/stat.h> 48202719Sgabor#include <sys/systm.h> 49202719Sgabor#include <sys/unistd.h> 50202719Sgabor#include <sys/vnode.h> 51202719Sgabor 52202719Sgabor#include <vm/vm.h> 53202719Sgabor#include <vm/vm_object.h> 54202719Sgabor#include <vm/vm_page.h> 55202719Sgabor#include <vm/vm_pager.h> 56202719Sgabor 57202719Sgabor#include <machine/_inttypes.h> 58202719Sgabor 59202719Sgabor#include <fs/fifofs/fifo.h> 60202719Sgabor#include <fs/tmpfs/tmpfs_vnops.h> 61202719Sgabor#include <fs/tmpfs/tmpfs.h> 62202719Sgabor 63202719Sgabor/* --------------------------------------------------------------------- */ 64202719Sgabor 65202719Sgaborstatic int 66202719Sgabortmpfs_lookup(struct vop_cachedlookup_args *v) 67202719Sgabor{ 68202719Sgabor struct vnode *dvp = v->a_dvp; 69202719Sgabor struct vnode **vpp = v->a_vpp; 70202719Sgabor struct componentname *cnp = v->a_cnp; 71202719Sgabor 72202719Sgabor int error; 73202719Sgabor struct tmpfs_dirent *de; 74202719Sgabor struct tmpfs_node *dnode; 75202719Sgabor 76202719Sgabor dnode = VP_TO_TMPFS_DIR(dvp); 77202719Sgabor *vpp = NULLVP; 78202719Sgabor 79202719Sgabor /* Check accessibility of requested node as a first step. */ 80202719Sgabor error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread); 81202719Sgabor if (error != 0) 82202719Sgabor goto out; 83202719Sgabor 84202719Sgabor /* We cannot be requesting the parent directory of the root node. */ 85202719Sgabor MPASS(IMPLIES(dnode->tn_type == VDIR && 86202719Sgabor dnode->tn_dir.tn_parent == dnode, 87202719Sgabor !(cnp->cn_flags & ISDOTDOT))); 88202719Sgabor 89202719Sgabor TMPFS_ASSERT_LOCKED(dnode); 90202719Sgabor if (dnode->tn_dir.tn_parent == NULL) { 91202719Sgabor error = ENOENT; 92202719Sgabor goto out; 93202719Sgabor } 94202719Sgabor if (cnp->cn_flags & ISDOTDOT) { 95202719Sgabor int ltype = 0; 96202719Sgabor 97202719Sgabor ltype = VOP_ISLOCKED(dvp); 98202719Sgabor vhold(dvp); 99202719Sgabor VOP_UNLOCK(dvp, 0); 100202719Sgabor /* Allocate a new vnode on the matching entry. */ 101202719Sgabor error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent, 102202719Sgabor cnp->cn_lkflags, vpp); 103202719Sgabor 104202719Sgabor vn_lock(dvp, ltype | LK_RETRY); 105202719Sgabor vdrop(dvp); 106202719Sgabor } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 107202719Sgabor VREF(dvp); 108202719Sgabor *vpp = dvp; 109202719Sgabor error = 0; 110202719Sgabor } else { 111202719Sgabor de = tmpfs_dir_lookup(dnode, NULL, cnp); 112202719Sgabor if (de != NULL && de->td_node == NULL) 113202719Sgabor cnp->cn_flags |= ISWHITEOUT; 114202719Sgabor if (de == NULL || de->td_node == NULL) { 115202719Sgabor /* The entry was not found in the directory. 116202719Sgabor * This is OK if we are creating or renaming an 117202719Sgabor * entry and are working on the last component of 118202719Sgabor * the path name. */ 119202719Sgabor if ((cnp->cn_flags & ISLASTCN) && 120202719Sgabor (cnp->cn_nameiop == CREATE || \ 121202719Sgabor cnp->cn_nameiop == RENAME || 122202719Sgabor (cnp->cn_nameiop == DELETE && 123202719Sgabor cnp->cn_flags & DOWHITEOUT && 124202719Sgabor cnp->cn_flags & ISWHITEOUT))) { 125202719Sgabor error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 126202719Sgabor cnp->cn_thread); 127202719Sgabor if (error != 0) 128202719Sgabor goto out; 129202719Sgabor 130202719Sgabor /* Keep the component name in the buffer for 131202719Sgabor * future uses. */ 132202719Sgabor cnp->cn_flags |= SAVENAME; 133202719Sgabor 134202719Sgabor error = EJUSTRETURN; 135202719Sgabor } else 136202719Sgabor error = ENOENT; 137202719Sgabor } else { 138202719Sgabor struct tmpfs_node *tnode; 139202719Sgabor 140202719Sgabor /* The entry was found, so get its associated 141202719Sgabor * tmpfs_node. */ 142202719Sgabor tnode = de->td_node; 143202719Sgabor 144202719Sgabor /* If we are not at the last path component and 145202719Sgabor * found a non-directory or non-link entry (which 146202719Sgabor * may itself be pointing to a directory), raise 147202719Sgabor * an error. */ 148202719Sgabor if ((tnode->tn_type != VDIR && 149202719Sgabor tnode->tn_type != VLNK) && 150202719Sgabor !(cnp->cn_flags & ISLASTCN)) { 151202719Sgabor error = ENOTDIR; 152202719Sgabor goto out; 153202719Sgabor } 154202719Sgabor 155202719Sgabor /* If we are deleting or renaming the entry, keep 156202719Sgabor * track of its tmpfs_dirent so that it can be 157202719Sgabor * easily deleted later. */ 158202719Sgabor if ((cnp->cn_flags & ISLASTCN) && 159202719Sgabor (cnp->cn_nameiop == DELETE || 160202719Sgabor cnp->cn_nameiop == RENAME)) { 161202719Sgabor error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 162202719Sgabor cnp->cn_thread); 163202719Sgabor if (error != 0) 164202719Sgabor goto out; 165202719Sgabor 166202719Sgabor /* Allocate a new vnode on the matching entry. */ 167202719Sgabor error = tmpfs_alloc_vp(dvp->v_mount, tnode, 168202719Sgabor cnp->cn_lkflags, vpp); 169202719Sgabor if (error != 0) 170202719Sgabor goto out; 171202719Sgabor 172202719Sgabor if ((dnode->tn_mode & S_ISTXT) && 173202719Sgabor VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) && 174202719Sgabor VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) { 175202719Sgabor error = EPERM; 176202719Sgabor vput(*vpp); 177202719Sgabor *vpp = NULL; 178202719Sgabor goto out; 179202719Sgabor } 180202719Sgabor cnp->cn_flags |= SAVENAME; 181202719Sgabor } else { 182202719Sgabor error = tmpfs_alloc_vp(dvp->v_mount, tnode, 183202719Sgabor cnp->cn_lkflags, vpp); 184202719Sgabor } 185202719Sgabor } 186202719Sgabor } 187202719Sgabor 188202719Sgabor /* Store the result of this lookup in the cache. Avoid this if the 189202719Sgabor * request was for creation, as it does not improve timings on 190202719Sgabor * emprical tests. */ 191202719Sgabor if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) 192202719Sgabor cache_enter(dvp, *vpp, cnp); 193202719Sgabor 194202719Sgaborout: 195202719Sgabor /* If there were no errors, *vpp cannot be null and it must be 196202719Sgabor * locked. */ 197202719Sgabor MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp))); 198202719Sgabor 199202719Sgabor return error; 200202719Sgabor} 201202719Sgabor 202202719Sgabor/* --------------------------------------------------------------------- */ 203202719Sgabor 204202719Sgaborstatic int 205202719Sgabortmpfs_create(struct vop_create_args *v) 206202719Sgabor{ 207202719Sgabor struct vnode *dvp = v->a_dvp; 208202719Sgabor struct vnode **vpp = v->a_vpp; 209202719Sgabor struct componentname *cnp = v->a_cnp; 210202719Sgabor struct vattr *vap = v->a_vap; 211202719Sgabor 212202719Sgabor MPASS(vap->va_type == VREG || vap->va_type == VSOCK); 213202719Sgabor 214202719Sgabor return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 215202719Sgabor} 216202719Sgabor/* --------------------------------------------------------------------- */ 217202719Sgabor 218202719Sgaborstatic int 219202719Sgabortmpfs_mknod(struct vop_mknod_args *v) 220202719Sgabor{ 221202719Sgabor struct vnode *dvp = v->a_dvp; 222202719Sgabor struct vnode **vpp = v->a_vpp; 223202719Sgabor struct componentname *cnp = v->a_cnp; 224202719Sgabor struct vattr *vap = v->a_vap; 225202719Sgabor 226202719Sgabor if (vap->va_type != VBLK && vap->va_type != VCHR && 227202719Sgabor vap->va_type != VFIFO) 228202719Sgabor return EINVAL; 229202719Sgabor 230202719Sgabor return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 231202719Sgabor} 232202719Sgabor 233202719Sgabor/* --------------------------------------------------------------------- */ 234202719Sgabor 235202719Sgaborstatic int 236202719Sgabortmpfs_open(struct vop_open_args *v) 237202719Sgabor{ 238202719Sgabor struct vnode *vp = v->a_vp; 239202719Sgabor int mode = v->a_mode; 240202719Sgabor 241202719Sgabor int error; 242202719Sgabor struct tmpfs_node *node; 243202719Sgabor 244202719Sgabor MPASS(VOP_ISLOCKED(vp)); 245202719Sgabor 246202719Sgabor node = VP_TO_TMPFS_NODE(vp); 247202719Sgabor 248202719Sgabor /* The file is still active but all its names have been removed 249202719Sgabor * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as 250202719Sgabor * it is about to die. */ 251202719Sgabor if (node->tn_links < 1) 252202719Sgabor return (ENOENT); 253202719Sgabor 254202719Sgabor /* If the file is marked append-only, deny write requests. */ 255202719Sgabor if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE) 256202719Sgabor error = EPERM; 257202719Sgabor else { 258202719Sgabor error = 0; 259202719Sgabor vnode_create_vobject(vp, node->tn_size, v->a_td); 260202719Sgabor } 261202719Sgabor 262202719Sgabor MPASS(VOP_ISLOCKED(vp)); 263202719Sgabor return error; 264202719Sgabor} 265202719Sgabor 266202719Sgabor/* --------------------------------------------------------------------- */ 267202719Sgabor 268202719Sgaborstatic int 269202719Sgabortmpfs_close(struct vop_close_args *v) 270202719Sgabor{ 271202719Sgabor struct vnode *vp = v->a_vp; 272202719Sgabor 273202719Sgabor MPASS(VOP_ISLOCKED(vp)); 274202719Sgabor 275202719Sgabor /* Update node times. */ 276202719Sgabor tmpfs_update(vp); 277202719Sgabor 278202719Sgabor return (0); 279202719Sgabor} 280202719Sgabor 281202719Sgabor/* --------------------------------------------------------------------- */ 282202719Sgabor 283202719Sgaborint 284202719Sgabortmpfs_access(struct vop_access_args *v) 285202719Sgabor{ 286202719Sgabor struct vnode *vp = v->a_vp; 287202719Sgabor accmode_t accmode = v->a_accmode; 288202719Sgabor struct ucred *cred = v->a_cred; 289202719Sgabor 290202719Sgabor int error; 291202719Sgabor struct tmpfs_node *node; 292202719Sgabor 293202719Sgabor MPASS(VOP_ISLOCKED(vp)); 294202719Sgabor 295202719Sgabor node = VP_TO_TMPFS_NODE(vp); 296202719Sgabor 297202719Sgabor switch (vp->v_type) { 298202719Sgabor case VDIR: 299202719Sgabor /* FALLTHROUGH */ 300202719Sgabor case VLNK: 301202719Sgabor /* FALLTHROUGH */ 302202719Sgabor case VREG: 303202719Sgabor if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) { 304202719Sgabor error = EROFS; 305202719Sgabor goto out; 306202719Sgabor } 307202719Sgabor break; 308202719Sgabor 309202719Sgabor case VBLK: 310202719Sgabor /* FALLTHROUGH */ 311202719Sgabor case VCHR: 312202719Sgabor /* FALLTHROUGH */ 313202719Sgabor case VSOCK: 314202719Sgabor /* FALLTHROUGH */ 315202719Sgabor case VFIFO: 316202719Sgabor break; 317202719Sgabor 318202719Sgabor default: 319202719Sgabor error = EINVAL; 320202719Sgabor goto out; 321202719Sgabor } 322202719Sgabor 323202719Sgabor if (accmode & VWRITE && node->tn_flags & IMMUTABLE) { 324202719Sgabor error = EPERM; 325202719Sgabor goto out; 326202719Sgabor } 327202719Sgabor 328202719Sgabor error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, 329202719Sgabor node->tn_gid, accmode, cred, NULL); 330202719Sgabor 331202719Sgaborout: 332202719Sgabor MPASS(VOP_ISLOCKED(vp)); 333202719Sgabor 334202719Sgabor return error; 335202719Sgabor} 336202719Sgabor 337202719Sgabor/* --------------------------------------------------------------------- */ 338202719Sgabor 339202719Sgaborint 340202719Sgabortmpfs_getattr(struct vop_getattr_args *v) 341202719Sgabor{ 342202719Sgabor struct vnode *vp = v->a_vp; 343202719Sgabor struct vattr *vap = v->a_vap; 344202719Sgabor 345202719Sgabor struct tmpfs_node *node; 346202719Sgabor 347202719Sgabor node = VP_TO_TMPFS_NODE(vp); 348202719Sgabor 349202719Sgabor tmpfs_update(vp); 350202719Sgabor 351202719Sgabor vap->va_type = vp->v_type; 352202719Sgabor vap->va_mode = node->tn_mode; 353202719Sgabor vap->va_nlink = node->tn_links; 354202719Sgabor vap->va_uid = node->tn_uid; 355202719Sgabor vap->va_gid = node->tn_gid; 356202719Sgabor vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 357202719Sgabor vap->va_fileid = node->tn_id; 358202719Sgabor vap->va_size = node->tn_size; 359202719Sgabor vap->va_blocksize = PAGE_SIZE; 360202719Sgabor vap->va_atime = node->tn_atime; 361202719Sgabor vap->va_mtime = node->tn_mtime; 362202719Sgabor vap->va_ctime = node->tn_ctime; 363202719Sgabor vap->va_birthtime = node->tn_birthtime; 364202719Sgabor vap->va_gen = node->tn_gen; 365202719Sgabor vap->va_flags = node->tn_flags; 366202719Sgabor vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 367202719Sgabor node->tn_rdev : NODEV; 368202719Sgabor vap->va_bytes = round_page(node->tn_size); 369202719Sgabor vap->va_filerev = 0; 370202719Sgabor 371202719Sgabor return 0; 372202719Sgabor} 373202719Sgabor 374202719Sgabor/* --------------------------------------------------------------------- */ 375202719Sgabor 376202719Sgabor/* XXX Should this operation be atomic? I think it should, but code in 377202719Sgabor * XXX other places (e.g., ufs) doesn't seem to be... */ 378202719Sgaborint 379202719Sgabortmpfs_setattr(struct vop_setattr_args *v) 380202719Sgabor{ 381202719Sgabor struct vnode *vp = v->a_vp; 382202719Sgabor struct vattr *vap = v->a_vap; 383202719Sgabor struct ucred *cred = v->a_cred; 384202719Sgabor struct thread *td = curthread; 385202719Sgabor 386202719Sgabor int error; 387202719Sgabor 388202719Sgabor MPASS(VOP_ISLOCKED(vp)); 389202719Sgabor 390202719Sgabor error = 0; 391202719Sgabor 392202719Sgabor /* Abort if any unsettable attribute is given. */ 393202719Sgabor if (vap->va_type != VNON || 394202719Sgabor vap->va_nlink != VNOVAL || 395202719Sgabor vap->va_fsid != VNOVAL || 396202719Sgabor vap->va_fileid != VNOVAL || 397202719Sgabor vap->va_blocksize != VNOVAL || 398202719Sgabor vap->va_gen != VNOVAL || 399202719Sgabor vap->va_rdev != VNOVAL || 400202719Sgabor vap->va_bytes != VNOVAL) 401202719Sgabor error = EINVAL; 402202719Sgabor 403202719Sgabor if (error == 0 && (vap->va_flags != VNOVAL)) 404202719Sgabor error = tmpfs_chflags(vp, vap->va_flags, cred, td); 405202719Sgabor 406202719Sgabor if (error == 0 && (vap->va_size != VNOVAL)) 407202719Sgabor error = tmpfs_chsize(vp, vap->va_size, cred, td); 408202719Sgabor 409202719Sgabor if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 410202719Sgabor error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td); 411202719Sgabor 412202719Sgabor if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) 413202719Sgabor error = tmpfs_chmod(vp, vap->va_mode, cred, td); 414202719Sgabor 415202719Sgabor if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL && 416202719Sgabor vap->va_atime.tv_nsec != VNOVAL) || 417202719Sgabor (vap->va_mtime.tv_sec != VNOVAL && 418 vap->va_mtime.tv_nsec != VNOVAL) || 419 (vap->va_birthtime.tv_sec != VNOVAL && 420 vap->va_birthtime.tv_nsec != VNOVAL))) 421 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 422 &vap->va_birthtime, vap->va_vaflags, cred, td); 423 424 /* Update the node times. We give preference to the error codes 425 * generated by this function rather than the ones that may arise 426 * from tmpfs_update. */ 427 tmpfs_update(vp); 428 429 MPASS(VOP_ISLOCKED(vp)); 430 431 return error; 432} 433 434/* --------------------------------------------------------------------- */ 435static int 436tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx, 437 vm_offset_t offset, size_t tlen, struct uio *uio) 438{ 439 vm_page_t m; 440 int error; 441 442 VM_OBJECT_LOCK(tobj); 443 vm_object_pip_add(tobj, 1); 444 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 445 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 446 if (m->valid != VM_PAGE_BITS_ALL) { 447 if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 448 error = vm_pager_get_pages(tobj, &m, 1, 0); 449 if (error != 0) { 450 printf("tmpfs get pages from pager error [read]\n"); 451 goto out; 452 } 453 } else 454 vm_page_zero_invalid(m, TRUE); 455 } 456 VM_OBJECT_UNLOCK(tobj); 457 error = uiomove_fromphys(&m, offset, tlen, uio); 458 VM_OBJECT_LOCK(tobj); 459out: 460 vm_page_lock(m); 461 vm_page_unwire(m, TRUE); 462 vm_page_unlock(m); 463 vm_page_wakeup(m); 464 vm_object_pip_subtract(tobj, 1); 465 VM_OBJECT_UNLOCK(tobj); 466 467 return (error); 468} 469 470static __inline int 471tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx, 472 vm_offset_t offset, size_t tlen, void *buf) 473{ 474 struct uio uio; 475 struct iovec iov; 476 477 uio.uio_iovcnt = 1; 478 uio.uio_iov = &iov; 479 iov.iov_base = buf; 480 iov.iov_len = tlen; 481 482 uio.uio_offset = 0; 483 uio.uio_resid = tlen; 484 uio.uio_rw = UIO_READ; 485 uio.uio_segflg = UIO_SYSSPACE; 486 uio.uio_td = curthread; 487 488 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio)); 489} 490 491static int 492tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio) 493{ 494 struct sf_buf *sf; 495 vm_pindex_t idx; 496 vm_page_t m; 497 vm_offset_t offset; 498 off_t addr; 499 size_t tlen; 500 char *ma; 501 int error; 502 503 addr = uio->uio_offset; 504 idx = OFF_TO_IDX(addr); 505 offset = addr & PAGE_MASK; 506 tlen = MIN(PAGE_SIZE - offset, len); 507 508 if ((vobj == NULL) || 509 (vobj->resident_page_count == 0 && vobj->cache == NULL)) 510 goto nocache; 511 512 VM_OBJECT_LOCK(vobj); 513lookupvpg: 514 if (((m = vm_page_lookup(vobj, idx)) != NULL) && 515 vm_page_is_valid(m, offset, tlen)) { 516 if ((m->oflags & VPO_BUSY) != 0) { 517 /* 518 * Reference the page before unlocking and sleeping so 519 * that the page daemon is less likely to reclaim it. 520 */ 521 vm_page_reference(m); 522 vm_page_sleep(m, "tmfsmr"); 523 goto lookupvpg; 524 } 525 vm_page_busy(m); 526 VM_OBJECT_UNLOCK(vobj); 527 error = uiomove_fromphys(&m, offset, tlen, uio); 528 VM_OBJECT_LOCK(vobj); 529 vm_page_wakeup(m); 530 VM_OBJECT_UNLOCK(vobj); 531 return (error); 532 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) { 533 KASSERT(offset == 0, 534 ("unexpected offset in tmpfs_mappedread for sendfile")); 535 if ((m->oflags & VPO_BUSY) != 0) { 536 /* 537 * Reference the page before unlocking and sleeping so 538 * that the page daemon is less likely to reclaim it. 539 */ 540 vm_page_reference(m); 541 vm_page_sleep(m, "tmfsmr"); 542 goto lookupvpg; 543 } 544 vm_page_busy(m); 545 VM_OBJECT_UNLOCK(vobj); 546 sched_pin(); 547 sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 548 ma = (char *)sf_buf_kva(sf); 549 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma); 550 if (error == 0) { 551 if (tlen != PAGE_SIZE) 552 bzero(ma + tlen, PAGE_SIZE - tlen); 553 uio->uio_offset += tlen; 554 uio->uio_resid -= tlen; 555 } 556 sf_buf_free(sf); 557 sched_unpin(); 558 VM_OBJECT_LOCK(vobj); 559 if (error == 0) 560 m->valid = VM_PAGE_BITS_ALL; 561 vm_page_wakeup(m); 562 VM_OBJECT_UNLOCK(vobj); 563 return (error); 564 } 565 VM_OBJECT_UNLOCK(vobj); 566nocache: 567 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio); 568 569 return (error); 570} 571 572static int 573tmpfs_read(struct vop_read_args *v) 574{ 575 struct vnode *vp = v->a_vp; 576 struct uio *uio = v->a_uio; 577 578 struct tmpfs_node *node; 579 vm_object_t uobj; 580 size_t len; 581 int resid; 582 583 int error = 0; 584 585 node = VP_TO_TMPFS_NODE(vp); 586 587 if (vp->v_type != VREG) { 588 error = EISDIR; 589 goto out; 590 } 591 592 if (uio->uio_offset < 0) { 593 error = EINVAL; 594 goto out; 595 } 596 597 node->tn_status |= TMPFS_NODE_ACCESSED; 598 599 uobj = node->tn_reg.tn_aobj; 600 while ((resid = uio->uio_resid) > 0) { 601 error = 0; 602 if (node->tn_size <= uio->uio_offset) 603 break; 604 len = MIN(node->tn_size - uio->uio_offset, resid); 605 if (len == 0) 606 break; 607 error = tmpfs_mappedread(vp->v_object, uobj, len, uio); 608 if ((error != 0) || (resid == uio->uio_resid)) 609 break; 610 } 611 612out: 613 614 return error; 615} 616 617/* --------------------------------------------------------------------- */ 618 619static int 620tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio) 621{ 622 vm_pindex_t idx; 623 vm_page_t vpg, tpg; 624 vm_offset_t offset; 625 off_t addr; 626 size_t tlen; 627 int error; 628 629 error = 0; 630 631 addr = uio->uio_offset; 632 idx = OFF_TO_IDX(addr); 633 offset = addr & PAGE_MASK; 634 tlen = MIN(PAGE_SIZE - offset, len); 635 636 if ((vobj == NULL) || 637 (vobj->resident_page_count == 0 && vobj->cache == NULL)) { 638 vpg = NULL; 639 goto nocache; 640 } 641 642 VM_OBJECT_LOCK(vobj); 643lookupvpg: 644 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) && 645 vm_page_is_valid(vpg, offset, tlen)) { 646 if ((vpg->oflags & VPO_BUSY) != 0) { 647 /* 648 * Reference the page before unlocking and sleeping so 649 * that the page daemon is less likely to reclaim it. 650 */ 651 vm_page_reference(vpg); 652 vm_page_sleep(vpg, "tmfsmw"); 653 goto lookupvpg; 654 } 655 vm_page_busy(vpg); 656 vm_page_undirty(vpg); 657 VM_OBJECT_UNLOCK(vobj); 658 error = uiomove_fromphys(&vpg, offset, tlen, uio); 659 } else { 660 if (__predict_false(vobj->cache != NULL)) 661 vm_page_cache_free(vobj, idx, idx + 1); 662 VM_OBJECT_UNLOCK(vobj); 663 vpg = NULL; 664 } 665nocache: 666 VM_OBJECT_LOCK(tobj); 667 vm_object_pip_add(tobj, 1); 668 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 669 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 670 if (tpg->valid != VM_PAGE_BITS_ALL) { 671 if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 672 error = vm_pager_get_pages(tobj, &tpg, 1, 0); 673 if (error != 0) { 674 printf("tmpfs get pages from pager error [write]\n"); 675 goto out; 676 } 677 } else 678 vm_page_zero_invalid(tpg, TRUE); 679 } 680 VM_OBJECT_UNLOCK(tobj); 681 if (vpg == NULL) 682 error = uiomove_fromphys(&tpg, offset, tlen, uio); 683 else { 684 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid")); 685 pmap_copy_page(vpg, tpg); 686 } 687 VM_OBJECT_LOCK(tobj); 688out: 689 if (vobj != NULL) 690 VM_OBJECT_LOCK(vobj); 691 if (error == 0) { 692 KASSERT(tpg->valid == VM_PAGE_BITS_ALL, 693 ("parts of tpg invalid")); 694 vm_page_dirty(tpg); 695 } 696 vm_page_lock(tpg); 697 vm_page_unwire(tpg, TRUE); 698 vm_page_unlock(tpg); 699 vm_page_wakeup(tpg); 700 if (vpg != NULL) 701 vm_page_wakeup(vpg); 702 if (vobj != NULL) 703 VM_OBJECT_UNLOCK(vobj); 704 vm_object_pip_subtract(tobj, 1); 705 VM_OBJECT_UNLOCK(tobj); 706 707 return (error); 708} 709 710static int 711tmpfs_write(struct vop_write_args *v) 712{ 713 struct vnode *vp = v->a_vp; 714 struct uio *uio = v->a_uio; 715 int ioflag = v->a_ioflag; 716 717 boolean_t extended; 718 int error = 0; 719 off_t oldsize; 720 struct tmpfs_node *node; 721 vm_object_t uobj; 722 size_t len; 723 int resid; 724 725 node = VP_TO_TMPFS_NODE(vp); 726 oldsize = node->tn_size; 727 728 if (uio->uio_offset < 0 || vp->v_type != VREG) { 729 error = EINVAL; 730 goto out; 731 } 732 733 if (uio->uio_resid == 0) { 734 error = 0; 735 goto out; 736 } 737 738 if (ioflag & IO_APPEND) 739 uio->uio_offset = node->tn_size; 740 741 if (uio->uio_offset + uio->uio_resid > 742 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 743 return (EFBIG); 744 745 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 746 return (EFBIG); 747 748 extended = uio->uio_offset + uio->uio_resid > node->tn_size; 749 if (extended) { 750 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid); 751 if (error != 0) 752 goto out; 753 } 754 755 uobj = node->tn_reg.tn_aobj; 756 while ((resid = uio->uio_resid) > 0) { 757 if (node->tn_size <= uio->uio_offset) 758 break; 759 len = MIN(node->tn_size - uio->uio_offset, resid); 760 if (len == 0) 761 break; 762 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio); 763 if ((error != 0) || (resid == uio->uio_resid)) 764 break; 765 } 766 767 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 768 (extended ? TMPFS_NODE_CHANGED : 0); 769 770 if (node->tn_mode & (S_ISUID | S_ISGID)) { 771 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0)) 772 node->tn_mode &= ~(S_ISUID | S_ISGID); 773 } 774 775 if (error != 0) 776 (void)tmpfs_reg_resize(vp, oldsize); 777 778out: 779 MPASS(IMPLIES(error == 0, uio->uio_resid == 0)); 780 MPASS(IMPLIES(error != 0, oldsize == node->tn_size)); 781 782 return error; 783} 784 785/* --------------------------------------------------------------------- */ 786 787static int 788tmpfs_fsync(struct vop_fsync_args *v) 789{ 790 struct vnode *vp = v->a_vp; 791 792 MPASS(VOP_ISLOCKED(vp)); 793 794 tmpfs_update(vp); 795 796 return 0; 797} 798 799/* --------------------------------------------------------------------- */ 800 801static int 802tmpfs_remove(struct vop_remove_args *v) 803{ 804 struct vnode *dvp = v->a_dvp; 805 struct vnode *vp = v->a_vp; 806 807 int error; 808 struct tmpfs_dirent *de; 809 struct tmpfs_mount *tmp; 810 struct tmpfs_node *dnode; 811 struct tmpfs_node *node; 812 813 MPASS(VOP_ISLOCKED(dvp)); 814 MPASS(VOP_ISLOCKED(vp)); 815 816 if (vp->v_type == VDIR) { 817 error = EISDIR; 818 goto out; 819 } 820 821 dnode = VP_TO_TMPFS_DIR(dvp); 822 node = VP_TO_TMPFS_NODE(vp); 823 tmp = VFS_TO_TMPFS(vp->v_mount); 824 de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 825 MPASS(de != NULL); 826 827 /* Files marked as immutable or append-only cannot be deleted. */ 828 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 829 (dnode->tn_flags & APPEND)) { 830 error = EPERM; 831 goto out; 832 } 833 834 /* Remove the entry from the directory; as it is a file, we do not 835 * have to change the number of hard links of the directory. */ 836 tmpfs_dir_detach(dvp, de); 837 if (v->a_cnp->cn_flags & DOWHITEOUT) 838 tmpfs_dir_whiteout_add(dvp, v->a_cnp); 839 840 /* Free the directory entry we just deleted. Note that the node 841 * referred by it will not be removed until the vnode is really 842 * reclaimed. */ 843 tmpfs_free_dirent(tmp, de, TRUE); 844 845 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED; 846 error = 0; 847 848out: 849 850 return error; 851} 852 853/* --------------------------------------------------------------------- */ 854 855static int 856tmpfs_link(struct vop_link_args *v) 857{ 858 struct vnode *dvp = v->a_tdvp; 859 struct vnode *vp = v->a_vp; 860 struct componentname *cnp = v->a_cnp; 861 862 int error; 863 struct tmpfs_dirent *de; 864 struct tmpfs_node *node; 865 866 MPASS(VOP_ISLOCKED(dvp)); 867 MPASS(cnp->cn_flags & HASBUF); 868 MPASS(dvp != vp); /* XXX When can this be false? */ 869 870 node = VP_TO_TMPFS_NODE(vp); 871 872 /* XXX: Why aren't the following two tests done by the caller? */ 873 874 /* Hard links of directories are forbidden. */ 875 if (vp->v_type == VDIR) { 876 error = EPERM; 877 goto out; 878 } 879 880 /* Cannot create cross-device links. */ 881 if (dvp->v_mount != vp->v_mount) { 882 error = EXDEV; 883 goto out; 884 } 885 886 /* Ensure that we do not overflow the maximum number of links imposed 887 * by the system. */ 888 MPASS(node->tn_links <= LINK_MAX); 889 if (node->tn_links == LINK_MAX) { 890 error = EMLINK; 891 goto out; 892 } 893 894 /* We cannot create links of files marked immutable or append-only. */ 895 if (node->tn_flags & (IMMUTABLE | APPEND)) { 896 error = EPERM; 897 goto out; 898 } 899 900 /* Allocate a new directory entry to represent the node. */ 901 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 902 cnp->cn_nameptr, cnp->cn_namelen, &de); 903 if (error != 0) 904 goto out; 905 906 /* Insert the new directory entry into the appropriate directory. */ 907 if (cnp->cn_flags & ISWHITEOUT) 908 tmpfs_dir_whiteout_remove(dvp, cnp); 909 tmpfs_dir_attach(dvp, de); 910 911 /* vp link count has changed, so update node times. */ 912 node->tn_status |= TMPFS_NODE_CHANGED; 913 tmpfs_update(vp); 914 915 error = 0; 916 917out: 918 return error; 919} 920 921/* --------------------------------------------------------------------- */ 922 923static int 924tmpfs_rename(struct vop_rename_args *v) 925{ 926 struct vnode *fdvp = v->a_fdvp; 927 struct vnode *fvp = v->a_fvp; 928 struct componentname *fcnp = v->a_fcnp; 929 struct vnode *tdvp = v->a_tdvp; 930 struct vnode *tvp = v->a_tvp; 931 struct componentname *tcnp = v->a_tcnp; 932 933 char *newname; 934 int error; 935 struct tmpfs_dirent *de; 936 struct tmpfs_mount *tmp; 937 struct tmpfs_node *fdnode; 938 struct tmpfs_node *fnode; 939 struct tmpfs_node *tnode; 940 struct tmpfs_node *tdnode; 941 942 MPASS(VOP_ISLOCKED(tdvp)); 943 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp))); 944 MPASS(fcnp->cn_flags & HASBUF); 945 MPASS(tcnp->cn_flags & HASBUF); 946 947 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp); 948 949 /* Disallow cross-device renames. 950 * XXX Why isn't this done by the caller? */ 951 if (fvp->v_mount != tdvp->v_mount || 952 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 953 error = EXDEV; 954 goto out; 955 } 956 957 tmp = VFS_TO_TMPFS(tdvp->v_mount); 958 tdnode = VP_TO_TMPFS_DIR(tdvp); 959 960 /* If source and target are the same file, there is nothing to do. */ 961 if (fvp == tvp) { 962 error = 0; 963 goto out; 964 } 965 966 /* If we need to move the directory between entries, lock the 967 * source so that we can safely operate on it. */ 968 if (fdvp != tdvp && fdvp != tvp) 969 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 970 fdnode = VP_TO_TMPFS_DIR(fdvp); 971 fnode = VP_TO_TMPFS_NODE(fvp); 972 de = tmpfs_dir_lookup(fdnode, fnode, fcnp); 973 974 /* Entry can disappear before we lock fdvp, 975 * also avoid manipulating '.' and '..' entries. */ 976 if (de == NULL) { 977 if ((fcnp->cn_flags & ISDOTDOT) != 0 || 978 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 979 error = EINVAL; 980 else 981 error = ENOENT; 982 goto out_locked; 983 } 984 MPASS(de->td_node == fnode); 985 986 /* If re-naming a directory to another preexisting directory 987 * ensure that the target directory is empty so that its 988 * removal causes no side effects. 989 * Kern_rename gurantees the destination to be a directory 990 * if the source is one. */ 991 if (tvp != NULL) { 992 MPASS(tnode != NULL); 993 994 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 995 (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 996 error = EPERM; 997 goto out_locked; 998 } 999 1000 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 1001 if (tnode->tn_size > 0) { 1002 error = ENOTEMPTY; 1003 goto out_locked; 1004 } 1005 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 1006 error = ENOTDIR; 1007 goto out_locked; 1008 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 1009 error = EISDIR; 1010 goto out_locked; 1011 } else { 1012 MPASS(fnode->tn_type != VDIR && 1013 tnode->tn_type != VDIR); 1014 } 1015 } 1016 1017 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) 1018 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 1019 error = EPERM; 1020 goto out_locked; 1021 } 1022 1023 /* Ensure that we have enough memory to hold the new name, if it 1024 * has to be changed. */ 1025 if (fcnp->cn_namelen != tcnp->cn_namelen || 1026 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { 1027 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK); 1028 } else 1029 newname = NULL; 1030 1031 /* If the node is being moved to another directory, we have to do 1032 * the move. */ 1033 if (fdnode != tdnode) { 1034 /* In case we are moving a directory, we have to adjust its 1035 * parent to point to the new parent. */ 1036 if (de->td_node->tn_type == VDIR) { 1037 struct tmpfs_node *n; 1038 1039 /* Ensure the target directory is not a child of the 1040 * directory being moved. Otherwise, we'd end up 1041 * with stale nodes. */ 1042 n = tdnode; 1043 /* TMPFS_LOCK garanties that no nodes are freed while 1044 * traversing the list. Nodes can only be marked as 1045 * removed: tn_parent == NULL. */ 1046 TMPFS_LOCK(tmp); 1047 TMPFS_NODE_LOCK(n); 1048 while (n != n->tn_dir.tn_parent) { 1049 struct tmpfs_node *parent; 1050 1051 if (n == fnode) { 1052 TMPFS_NODE_UNLOCK(n); 1053 TMPFS_UNLOCK(tmp); 1054 error = EINVAL; 1055 if (newname != NULL) 1056 free(newname, M_TMPFSNAME); 1057 goto out_locked; 1058 } 1059 parent = n->tn_dir.tn_parent; 1060 TMPFS_NODE_UNLOCK(n); 1061 if (parent == NULL) { 1062 n = NULL; 1063 break; 1064 } 1065 TMPFS_NODE_LOCK(parent); 1066 if (parent->tn_dir.tn_parent == NULL) { 1067 TMPFS_NODE_UNLOCK(parent); 1068 n = NULL; 1069 break; 1070 } 1071 n = parent; 1072 } 1073 TMPFS_UNLOCK(tmp); 1074 if (n == NULL) { 1075 error = EINVAL; 1076 if (newname != NULL) 1077 free(newname, M_TMPFSNAME); 1078 goto out_locked; 1079 } 1080 TMPFS_NODE_UNLOCK(n); 1081 1082 /* Adjust the parent pointer. */ 1083 TMPFS_VALIDATE_DIR(fnode); 1084 TMPFS_NODE_LOCK(de->td_node); 1085 de->td_node->tn_dir.tn_parent = tdnode; 1086 TMPFS_NODE_UNLOCK(de->td_node); 1087 1088 /* As a result of changing the target of the '..' 1089 * entry, the link count of the source and target 1090 * directories has to be adjusted. */ 1091 TMPFS_NODE_LOCK(tdnode); 1092 TMPFS_ASSERT_LOCKED(tdnode); 1093 tdnode->tn_links++; 1094 TMPFS_NODE_UNLOCK(tdnode); 1095 1096 TMPFS_NODE_LOCK(fdnode); 1097 TMPFS_ASSERT_LOCKED(fdnode); 1098 fdnode->tn_links--; 1099 TMPFS_NODE_UNLOCK(fdnode); 1100 } 1101 1102 /* Do the move: just remove the entry from the source directory 1103 * and insert it into the target one. */ 1104 tmpfs_dir_detach(fdvp, de); 1105 if (fcnp->cn_flags & DOWHITEOUT) 1106 tmpfs_dir_whiteout_add(fdvp, fcnp); 1107 if (tcnp->cn_flags & ISWHITEOUT) 1108 tmpfs_dir_whiteout_remove(tdvp, tcnp); 1109 tmpfs_dir_attach(tdvp, de); 1110 } 1111 1112 /* If the name has changed, we need to make it effective by changing 1113 * it in the directory entry. */ 1114 if (newname != NULL) { 1115 MPASS(tcnp->cn_namelen <= MAXNAMLEN); 1116 1117 free(de->td_name, M_TMPFSNAME); 1118 de->td_namelen = (uint16_t)tcnp->cn_namelen; 1119 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen); 1120 de->td_name = newname; 1121 1122 fnode->tn_status |= TMPFS_NODE_CHANGED; 1123 tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1124 } 1125 1126 /* If we are overwriting an entry, we have to remove the old one 1127 * from the target directory. */ 1128 if (tvp != NULL) { 1129 /* Remove the old entry from the target directory. */ 1130 de = tmpfs_dir_lookup(tdnode, tnode, tcnp); 1131 tmpfs_dir_detach(tdvp, de); 1132 1133 /* Free the directory entry we just deleted. Note that the 1134 * node referred by it will not be removed until the vnode is 1135 * really reclaimed. */ 1136 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE); 1137 } 1138 cache_purge(fvp); 1139 if (tvp != NULL) 1140 cache_purge(tvp); 1141 1142 error = 0; 1143 1144out_locked: 1145 if (fdvp != tdvp && fdvp != tvp) 1146 VOP_UNLOCK(fdvp, 0); 1147 1148out: 1149 /* Release target nodes. */ 1150 /* XXX: I don't understand when tdvp can be the same as tvp, but 1151 * other code takes care of this... */ 1152 if (tdvp == tvp) 1153 vrele(tdvp); 1154 else 1155 vput(tdvp); 1156 if (tvp != NULL) 1157 vput(tvp); 1158 1159 /* Release source nodes. */ 1160 vrele(fdvp); 1161 vrele(fvp); 1162 1163 return error; 1164} 1165 1166/* --------------------------------------------------------------------- */ 1167 1168static int 1169tmpfs_mkdir(struct vop_mkdir_args *v) 1170{ 1171 struct vnode *dvp = v->a_dvp; 1172 struct vnode **vpp = v->a_vpp; 1173 struct componentname *cnp = v->a_cnp; 1174 struct vattr *vap = v->a_vap; 1175 1176 MPASS(vap->va_type == VDIR); 1177 1178 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 1179} 1180 1181/* --------------------------------------------------------------------- */ 1182 1183static int 1184tmpfs_rmdir(struct vop_rmdir_args *v) 1185{ 1186 struct vnode *dvp = v->a_dvp; 1187 struct vnode *vp = v->a_vp; 1188 1189 int error; 1190 struct tmpfs_dirent *de; 1191 struct tmpfs_mount *tmp; 1192 struct tmpfs_node *dnode; 1193 struct tmpfs_node *node; 1194 1195 MPASS(VOP_ISLOCKED(dvp)); 1196 MPASS(VOP_ISLOCKED(vp)); 1197 1198 tmp = VFS_TO_TMPFS(dvp->v_mount); 1199 dnode = VP_TO_TMPFS_DIR(dvp); 1200 node = VP_TO_TMPFS_DIR(vp); 1201 1202 /* Directories with more than two entries ('.' and '..') cannot be 1203 * removed. */ 1204 if (node->tn_size > 0) { 1205 error = ENOTEMPTY; 1206 goto out; 1207 } 1208 1209 if ((dnode->tn_flags & APPEND) 1210 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1211 error = EPERM; 1212 goto out; 1213 } 1214 1215 /* This invariant holds only if we are not trying to remove "..". 1216 * We checked for that above so this is safe now. */ 1217 MPASS(node->tn_dir.tn_parent == dnode); 1218 1219 /* Get the directory entry associated with node (vp). This was 1220 * filled by tmpfs_lookup while looking up the entry. */ 1221 de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 1222 MPASS(TMPFS_DIRENT_MATCHES(de, 1223 v->a_cnp->cn_nameptr, 1224 v->a_cnp->cn_namelen)); 1225 1226 /* Check flags to see if we are allowed to remove the directory. */ 1227 if (dnode->tn_flags & APPEND 1228 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1229 error = EPERM; 1230 goto out; 1231 } 1232 1233 1234 /* Detach the directory entry from the directory (dnode). */ 1235 tmpfs_dir_detach(dvp, de); 1236 if (v->a_cnp->cn_flags & DOWHITEOUT) 1237 tmpfs_dir_whiteout_add(dvp, v->a_cnp); 1238 1239 /* No vnode should be allocated for this entry from this point */ 1240 TMPFS_NODE_LOCK(node); 1241 TMPFS_ASSERT_ELOCKED(node); 1242 node->tn_links--; 1243 node->tn_dir.tn_parent = NULL; 1244 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1245 TMPFS_NODE_MODIFIED; 1246 1247 TMPFS_NODE_UNLOCK(node); 1248 1249 TMPFS_NODE_LOCK(dnode); 1250 TMPFS_ASSERT_ELOCKED(dnode); 1251 dnode->tn_links--; 1252 dnode->tn_status |= TMPFS_NODE_ACCESSED | \ 1253 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1254 TMPFS_NODE_UNLOCK(dnode); 1255 1256 cache_purge(dvp); 1257 cache_purge(vp); 1258 1259 /* Free the directory entry we just deleted. Note that the node 1260 * referred by it will not be removed until the vnode is really 1261 * reclaimed. */ 1262 tmpfs_free_dirent(tmp, de, TRUE); 1263 1264 /* Release the deleted vnode (will destroy the node, notify 1265 * interested parties and clean it from the cache). */ 1266 1267 dnode->tn_status |= TMPFS_NODE_CHANGED; 1268 tmpfs_update(dvp); 1269 1270 error = 0; 1271 1272out: 1273 return error; 1274} 1275 1276/* --------------------------------------------------------------------- */ 1277 1278static int 1279tmpfs_symlink(struct vop_symlink_args *v) 1280{ 1281 struct vnode *dvp = v->a_dvp; 1282 struct vnode **vpp = v->a_vpp; 1283 struct componentname *cnp = v->a_cnp; 1284 struct vattr *vap = v->a_vap; 1285 char *target = v->a_target; 1286 1287#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */ 1288 MPASS(vap->va_type == VLNK); 1289#else 1290 vap->va_type = VLNK; 1291#endif 1292 1293 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1294} 1295 1296/* --------------------------------------------------------------------- */ 1297 1298static int 1299tmpfs_readdir(struct vop_readdir_args *v) 1300{ 1301 struct vnode *vp = v->a_vp; 1302 struct uio *uio = v->a_uio; 1303 int *eofflag = v->a_eofflag; 1304 u_long **cookies = v->a_cookies; 1305 int *ncookies = v->a_ncookies; 1306 1307 int error; 1308 off_t startoff; 1309 off_t cnt = 0; 1310 struct tmpfs_node *node; 1311 1312 /* This operation only makes sense on directory nodes. */ 1313 if (vp->v_type != VDIR) 1314 return ENOTDIR; 1315 1316 node = VP_TO_TMPFS_DIR(vp); 1317 1318 startoff = uio->uio_offset; 1319 1320 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 1321 error = tmpfs_dir_getdotdent(node, uio); 1322 if (error != 0) 1323 goto outok; 1324 cnt++; 1325 } 1326 1327 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 1328 error = tmpfs_dir_getdotdotdent(node, uio); 1329 if (error != 0) 1330 goto outok; 1331 cnt++; 1332 } 1333 1334 error = tmpfs_dir_getdents(node, uio, &cnt); 1335 1336outok: 1337 MPASS(error >= -1); 1338 1339 if (error == -1) 1340 error = (cnt != 0) ? 0 : EINVAL; 1341 1342 if (eofflag != NULL) 1343 *eofflag = 1344 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1345 1346 /* Update NFS-related variables. */ 1347 if (error == 0 && cookies != NULL && ncookies != NULL) { 1348 off_t i; 1349 off_t off = startoff; 1350 struct tmpfs_dirent *de = NULL; 1351 1352 *ncookies = cnt; 1353 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 1354 1355 for (i = 0; i < cnt; i++) { 1356 MPASS(off != TMPFS_DIRCOOKIE_EOF); 1357 if (off == TMPFS_DIRCOOKIE_DOT) { 1358 off = TMPFS_DIRCOOKIE_DOTDOT; 1359 } else { 1360 if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1361 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 1362 } else if (de != NULL) { 1363 de = TAILQ_NEXT(de, td_entries); 1364 } else { 1365 de = tmpfs_dir_lookupbycookie(node, 1366 off); 1367 MPASS(de != NULL); 1368 de = TAILQ_NEXT(de, td_entries); 1369 } 1370 if (de == NULL) 1371 off = TMPFS_DIRCOOKIE_EOF; 1372 else 1373 off = tmpfs_dircookie(de); 1374 } 1375 1376 (*cookies)[i] = off; 1377 } 1378 MPASS(uio->uio_offset == off); 1379 } 1380 1381 return error; 1382} 1383 1384/* --------------------------------------------------------------------- */ 1385 1386static int 1387tmpfs_readlink(struct vop_readlink_args *v) 1388{ 1389 struct vnode *vp = v->a_vp; 1390 struct uio *uio = v->a_uio; 1391 1392 int error; 1393 struct tmpfs_node *node; 1394 1395 MPASS(uio->uio_offset == 0); 1396 MPASS(vp->v_type == VLNK); 1397 1398 node = VP_TO_TMPFS_NODE(vp); 1399 1400 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid), 1401 uio); 1402 node->tn_status |= TMPFS_NODE_ACCESSED; 1403 1404 return error; 1405} 1406 1407/* --------------------------------------------------------------------- */ 1408 1409static int 1410tmpfs_inactive(struct vop_inactive_args *v) 1411{ 1412 struct vnode *vp = v->a_vp; 1413 struct thread *l = v->a_td; 1414 1415 struct tmpfs_node *node; 1416 1417 MPASS(VOP_ISLOCKED(vp)); 1418 1419 node = VP_TO_TMPFS_NODE(vp); 1420 1421 if (node->tn_links == 0) 1422 vrecycle(vp, l); 1423 1424 return 0; 1425} 1426 1427/* --------------------------------------------------------------------- */ 1428 1429int 1430tmpfs_reclaim(struct vop_reclaim_args *v) 1431{ 1432 struct vnode *vp = v->a_vp; 1433 1434 struct tmpfs_mount *tmp; 1435 struct tmpfs_node *node; 1436 1437 node = VP_TO_TMPFS_NODE(vp); 1438 tmp = VFS_TO_TMPFS(vp->v_mount); 1439 1440 vnode_destroy_vobject(vp); 1441 cache_purge(vp); 1442 1443 TMPFS_NODE_LOCK(node); 1444 TMPFS_ASSERT_ELOCKED(node); 1445 tmpfs_free_vp(vp); 1446 1447 /* If the node referenced by this vnode was deleted by the user, 1448 * we must free its associated data structures (now that the vnode 1449 * is being reclaimed). */ 1450 if (node->tn_links == 0 && 1451 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) { 1452 node->tn_vpstate = TMPFS_VNODE_DOOMED; 1453 TMPFS_NODE_UNLOCK(node); 1454 tmpfs_free_node(tmp, node); 1455 } else 1456 TMPFS_NODE_UNLOCK(node); 1457 1458 MPASS(vp->v_data == NULL); 1459 return 0; 1460} 1461 1462/* --------------------------------------------------------------------- */ 1463 1464static int 1465tmpfs_print(struct vop_print_args *v) 1466{ 1467 struct vnode *vp = v->a_vp; 1468 1469 struct tmpfs_node *node; 1470 1471 node = VP_TO_TMPFS_NODE(vp); 1472 1473 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n", 1474 node, node->tn_flags, node->tn_links); 1475 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX 1476 ", status 0x%x\n", 1477 node->tn_mode, node->tn_uid, node->tn_gid, 1478 (uintmax_t)node->tn_size, node->tn_status); 1479 1480 if (vp->v_type == VFIFO) 1481 fifo_printinfo(vp); 1482 1483 printf("\n"); 1484 1485 return 0; 1486} 1487 1488/* --------------------------------------------------------------------- */ 1489 1490static int 1491tmpfs_pathconf(struct vop_pathconf_args *v) 1492{ 1493 int name = v->a_name; 1494 register_t *retval = v->a_retval; 1495 1496 int error; 1497 1498 error = 0; 1499 1500 switch (name) { 1501 case _PC_LINK_MAX: 1502 *retval = LINK_MAX; 1503 break; 1504 1505 case _PC_NAME_MAX: 1506 *retval = NAME_MAX; 1507 break; 1508 1509 case _PC_PATH_MAX: 1510 *retval = PATH_MAX; 1511 break; 1512 1513 case _PC_PIPE_BUF: 1514 *retval = PIPE_BUF; 1515 break; 1516 1517 case _PC_CHOWN_RESTRICTED: 1518 *retval = 1; 1519 break; 1520 1521 case _PC_NO_TRUNC: 1522 *retval = 1; 1523 break; 1524 1525 case _PC_SYNC_IO: 1526 *retval = 1; 1527 break; 1528 1529 case _PC_FILESIZEBITS: 1530 *retval = 0; /* XXX Don't know which value should I return. */ 1531 break; 1532 1533 default: 1534 error = EINVAL; 1535 } 1536 1537 return error; 1538} 1539 1540static int 1541tmpfs_vptofh(struct vop_vptofh_args *ap) 1542{ 1543 struct tmpfs_fid *tfhp; 1544 struct tmpfs_node *node; 1545 1546 tfhp = (struct tmpfs_fid *)ap->a_fhp; 1547 node = VP_TO_TMPFS_NODE(ap->a_vp); 1548 1549 tfhp->tf_len = sizeof(struct tmpfs_fid); 1550 tfhp->tf_id = node->tn_id; 1551 tfhp->tf_gen = node->tn_gen; 1552 1553 return (0); 1554} 1555 1556static int 1557tmpfs_whiteout(struct vop_whiteout_args *ap) 1558{ 1559 struct vnode *dvp = ap->a_dvp; 1560 struct componentname *cnp = ap->a_cnp; 1561 struct tmpfs_dirent *de; 1562 1563 switch (ap->a_flags) { 1564 case LOOKUP: 1565 return (0); 1566 case CREATE: 1567 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1568 if (de != NULL) 1569 return (de->td_node == NULL ? 0 : EEXIST); 1570 return (tmpfs_dir_whiteout_add(dvp, cnp)); 1571 case DELETE: 1572 tmpfs_dir_whiteout_remove(dvp, cnp); 1573 return (0); 1574 default: 1575 panic("tmpfs_whiteout: unknown op"); 1576 } 1577} 1578 1579/* --------------------------------------------------------------------- */ 1580 1581/* 1582 * vnode operations vector used for files stored in a tmpfs file system. 1583 */ 1584struct vop_vector tmpfs_vnodeop_entries = { 1585 .vop_default = &default_vnodeops, 1586 .vop_lookup = vfs_cache_lookup, 1587 .vop_cachedlookup = tmpfs_lookup, 1588 .vop_create = tmpfs_create, 1589 .vop_mknod = tmpfs_mknod, 1590 .vop_open = tmpfs_open, 1591 .vop_close = tmpfs_close, 1592 .vop_access = tmpfs_access, 1593 .vop_getattr = tmpfs_getattr, 1594 .vop_setattr = tmpfs_setattr, 1595 .vop_read = tmpfs_read, 1596 .vop_write = tmpfs_write, 1597 .vop_fsync = tmpfs_fsync, 1598 .vop_remove = tmpfs_remove, 1599 .vop_link = tmpfs_link, 1600 .vop_rename = tmpfs_rename, 1601 .vop_mkdir = tmpfs_mkdir, 1602 .vop_rmdir = tmpfs_rmdir, 1603 .vop_symlink = tmpfs_symlink, 1604 .vop_readdir = tmpfs_readdir, 1605 .vop_readlink = tmpfs_readlink, 1606 .vop_inactive = tmpfs_inactive, 1607 .vop_reclaim = tmpfs_reclaim, 1608 .vop_print = tmpfs_print, 1609 .vop_pathconf = tmpfs_pathconf, 1610 .vop_vptofh = tmpfs_vptofh, 1611 .vop_whiteout = tmpfs_whiteout, 1612 .vop_bmap = VOP_EOPNOTSUPP, 1613}; 1614 1615