tmpfs_vnops.c revision 250190
1171802Sdelphij/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */ 2170808Sdelphij 3182739Sdelphij/*- 4171802Sdelphij * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 5170808Sdelphij * All rights reserved. 6170808Sdelphij * 7170808Sdelphij * This code is derived from software contributed to The NetBSD Foundation 8170808Sdelphij * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9170808Sdelphij * 2005 program. 10170808Sdelphij * 11170808Sdelphij * Redistribution and use in source and binary forms, with or without 12170808Sdelphij * modification, are permitted provided that the following conditions 13170808Sdelphij * are met: 14170808Sdelphij * 1. Redistributions of source code must retain the above copyright 15170808Sdelphij * notice, this list of conditions and the following disclaimer. 16170808Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 17170808Sdelphij * notice, this list of conditions and the following disclaimer in the 18170808Sdelphij * documentation and/or other materials provided with the distribution. 19170808Sdelphij * 20170808Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21170808Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22170808Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23170808Sdelphij * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24170808Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25170808Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26170808Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27170808Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28170808Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29170808Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30170808Sdelphij * POSSIBILITY OF SUCH DAMAGE. 31170808Sdelphij */ 32170808Sdelphij 33170808Sdelphij/* 34170808Sdelphij * tmpfs vnode interface. 35170808Sdelphij */ 36170808Sdelphij#include <sys/cdefs.h> 37170808Sdelphij__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_vnops.c 250190 2013-05-02 18:46:31Z kib $"); 38170808Sdelphij 39170808Sdelphij#include <sys/param.h> 40170808Sdelphij#include <sys/fcntl.h> 41170808Sdelphij#include <sys/lockf.h> 42248084Sattilio#include <sys/lock.h> 43170808Sdelphij#include <sys/namei.h> 44170808Sdelphij#include <sys/priv.h> 45170808Sdelphij#include <sys/proc.h> 46248084Sattilio#include <sys/rwlock.h> 47197850Sdelphij#include <sys/sched.h> 48197850Sdelphij#include <sys/sf_buf.h> 49170808Sdelphij#include <sys/stat.h> 50170808Sdelphij#include <sys/systm.h> 51232960Sgleb#include <sys/sysctl.h> 52170808Sdelphij#include <sys/unistd.h> 53170808Sdelphij#include <sys/vnode.h> 54170808Sdelphij 55170808Sdelphij#include <vm/vm.h> 56239065Skib#include <vm/vm_param.h> 57170808Sdelphij#include <vm/vm_object.h> 58170808Sdelphij#include <vm/vm_page.h> 59170808Sdelphij#include <vm/vm_pager.h> 60188929Salc 61170808Sdelphij#include <fs/tmpfs/tmpfs_vnops.h> 62170808Sdelphij#include <fs/tmpfs/tmpfs.h> 63170808Sdelphij 64232960SglebSYSCTL_DECL(_vfs_tmpfs); 65232960Sgleb 66232960Sglebstatic volatile int tmpfs_rename_restarts; 67232960SglebSYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD, 68232960Sgleb __DEVOLATILE(int *, &tmpfs_rename_restarts), 0, 69232960Sgleb "Times rename had to restart due to lock contention"); 70232960Sgleb 71170808Sdelphij/* --------------------------------------------------------------------- */ 72170808Sdelphij 73171069Sdelphijstatic int 74170808Sdelphijtmpfs_lookup(struct vop_cachedlookup_args *v) 75170808Sdelphij{ 76170808Sdelphij struct vnode *dvp = v->a_dvp; 77170808Sdelphij struct vnode **vpp = v->a_vpp; 78170808Sdelphij struct componentname *cnp = v->a_cnp; 79170808Sdelphij 80170808Sdelphij int error; 81170808Sdelphij struct tmpfs_dirent *de; 82170808Sdelphij struct tmpfs_node *dnode; 83170808Sdelphij 84170808Sdelphij dnode = VP_TO_TMPFS_DIR(dvp); 85170808Sdelphij *vpp = NULLVP; 86170808Sdelphij 87170808Sdelphij /* Check accessibility of requested node as a first step. */ 88191990Sattilio error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread); 89170808Sdelphij if (error != 0) 90170808Sdelphij goto out; 91170808Sdelphij 92170808Sdelphij /* We cannot be requesting the parent directory of the root node. */ 93170808Sdelphij MPASS(IMPLIES(dnode->tn_type == VDIR && 94170808Sdelphij dnode->tn_dir.tn_parent == dnode, 95170808Sdelphij !(cnp->cn_flags & ISDOTDOT))); 96170808Sdelphij 97197953Sdelphij TMPFS_ASSERT_LOCKED(dnode); 98197953Sdelphij if (dnode->tn_dir.tn_parent == NULL) { 99197953Sdelphij error = ENOENT; 100197953Sdelphij goto out; 101197953Sdelphij } 102170808Sdelphij if (cnp->cn_flags & ISDOTDOT) { 103171799Sdelphij int ltype = 0; 104171799Sdelphij 105176559Sattilio ltype = VOP_ISLOCKED(dvp); 106171802Sdelphij vhold(dvp); 107175294Sattilio VOP_UNLOCK(dvp, 0); 108170808Sdelphij /* Allocate a new vnode on the matching entry. */ 109171799Sdelphij error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent, 110191990Sattilio cnp->cn_lkflags, vpp); 111170808Sdelphij 112175202Sattilio vn_lock(dvp, ltype | LK_RETRY); 113171802Sdelphij vdrop(dvp); 114170808Sdelphij } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 115170808Sdelphij VREF(dvp); 116170808Sdelphij *vpp = dvp; 117170808Sdelphij error = 0; 118170808Sdelphij } else { 119188318Skib de = tmpfs_dir_lookup(dnode, NULL, cnp); 120211598Sed if (de != NULL && de->td_node == NULL) 121211598Sed cnp->cn_flags |= ISWHITEOUT; 122211598Sed if (de == NULL || de->td_node == NULL) { 123170808Sdelphij /* The entry was not found in the directory. 124170808Sdelphij * This is OK if we are creating or renaming an 125170808Sdelphij * entry and are working on the last component of 126170808Sdelphij * the path name. */ 127170808Sdelphij if ((cnp->cn_flags & ISLASTCN) && 128170808Sdelphij (cnp->cn_nameiop == CREATE || \ 129211598Sed cnp->cn_nameiop == RENAME || 130211598Sed (cnp->cn_nameiop == DELETE && 131211598Sed cnp->cn_flags & DOWHITEOUT && 132211598Sed cnp->cn_flags & ISWHITEOUT))) { 133170808Sdelphij error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 134170808Sdelphij cnp->cn_thread); 135170808Sdelphij if (error != 0) 136170808Sdelphij goto out; 137170808Sdelphij 138170808Sdelphij /* Keep the component name in the buffer for 139170808Sdelphij * future uses. */ 140170808Sdelphij cnp->cn_flags |= SAVENAME; 141170808Sdelphij 142170808Sdelphij error = EJUSTRETURN; 143170808Sdelphij } else 144170808Sdelphij error = ENOENT; 145170808Sdelphij } else { 146170808Sdelphij struct tmpfs_node *tnode; 147170808Sdelphij 148170808Sdelphij /* The entry was found, so get its associated 149170808Sdelphij * tmpfs_node. */ 150170808Sdelphij tnode = de->td_node; 151170808Sdelphij 152170808Sdelphij /* If we are not at the last path component and 153170808Sdelphij * found a non-directory or non-link entry (which 154170808Sdelphij * may itself be pointing to a directory), raise 155170808Sdelphij * an error. */ 156170808Sdelphij if ((tnode->tn_type != VDIR && 157170808Sdelphij tnode->tn_type != VLNK) && 158170808Sdelphij !(cnp->cn_flags & ISLASTCN)) { 159170808Sdelphij error = ENOTDIR; 160170808Sdelphij goto out; 161170808Sdelphij } 162170808Sdelphij 163170808Sdelphij /* If we are deleting or renaming the entry, keep 164170808Sdelphij * track of its tmpfs_dirent so that it can be 165170808Sdelphij * easily deleted later. */ 166170808Sdelphij if ((cnp->cn_flags & ISLASTCN) && 167170808Sdelphij (cnp->cn_nameiop == DELETE || 168170808Sdelphij cnp->cn_nameiop == RENAME)) { 169170808Sdelphij error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 170170808Sdelphij cnp->cn_thread); 171170808Sdelphij if (error != 0) 172170808Sdelphij goto out; 173171070Sdelphij 174170808Sdelphij /* Allocate a new vnode on the matching entry. */ 175171799Sdelphij error = tmpfs_alloc_vp(dvp->v_mount, tnode, 176191990Sattilio cnp->cn_lkflags, vpp); 177170808Sdelphij if (error != 0) 178170808Sdelphij goto out; 179170808Sdelphij 180170808Sdelphij if ((dnode->tn_mode & S_ISTXT) && 181170808Sdelphij VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) && 182170808Sdelphij VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) { 183170808Sdelphij error = EPERM; 184170808Sdelphij vput(*vpp); 185170808Sdelphij *vpp = NULL; 186170808Sdelphij goto out; 187171070Sdelphij } 188170808Sdelphij cnp->cn_flags |= SAVENAME; 189171799Sdelphij } else { 190171799Sdelphij error = tmpfs_alloc_vp(dvp->v_mount, tnode, 191191990Sattilio cnp->cn_lkflags, vpp); 192170808Sdelphij } 193170808Sdelphij } 194170808Sdelphij } 195170808Sdelphij 196170808Sdelphij /* Store the result of this lookup in the cache. Avoid this if the 197170808Sdelphij * request was for creation, as it does not improve timings on 198170808Sdelphij * emprical tests. */ 199170808Sdelphij if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) 200170808Sdelphij cache_enter(dvp, *vpp, cnp); 201170808Sdelphij 202170808Sdelphijout: 203170808Sdelphij /* If there were no errors, *vpp cannot be null and it must be 204170808Sdelphij * locked. */ 205176559Sattilio MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp))); 206170808Sdelphij 207170808Sdelphij return error; 208170808Sdelphij} 209170808Sdelphij 210170808Sdelphij/* --------------------------------------------------------------------- */ 211170808Sdelphij 212171069Sdelphijstatic int 213170808Sdelphijtmpfs_create(struct vop_create_args *v) 214170808Sdelphij{ 215170808Sdelphij struct vnode *dvp = v->a_dvp; 216170808Sdelphij struct vnode **vpp = v->a_vpp; 217170808Sdelphij struct componentname *cnp = v->a_cnp; 218170808Sdelphij struct vattr *vap = v->a_vap; 219170808Sdelphij 220170808Sdelphij MPASS(vap->va_type == VREG || vap->va_type == VSOCK); 221170808Sdelphij 222170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 223170808Sdelphij} 224170808Sdelphij/* --------------------------------------------------------------------- */ 225170808Sdelphij 226171069Sdelphijstatic int 227170808Sdelphijtmpfs_mknod(struct vop_mknod_args *v) 228170808Sdelphij{ 229170808Sdelphij struct vnode *dvp = v->a_dvp; 230170808Sdelphij struct vnode **vpp = v->a_vpp; 231170808Sdelphij struct componentname *cnp = v->a_cnp; 232170808Sdelphij struct vattr *vap = v->a_vap; 233170808Sdelphij 234170808Sdelphij if (vap->va_type != VBLK && vap->va_type != VCHR && 235170808Sdelphij vap->va_type != VFIFO) 236170808Sdelphij return EINVAL; 237170808Sdelphij 238170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 239170808Sdelphij} 240170808Sdelphij 241170808Sdelphij/* --------------------------------------------------------------------- */ 242170808Sdelphij 243171069Sdelphijstatic int 244170808Sdelphijtmpfs_open(struct vop_open_args *v) 245170808Sdelphij{ 246170808Sdelphij struct vnode *vp = v->a_vp; 247170808Sdelphij int mode = v->a_mode; 248170808Sdelphij 249170808Sdelphij int error; 250170808Sdelphij struct tmpfs_node *node; 251170808Sdelphij 252176559Sattilio MPASS(VOP_ISLOCKED(vp)); 253170808Sdelphij 254170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 255171070Sdelphij 256170808Sdelphij /* The file is still active but all its names have been removed 257170808Sdelphij * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as 258170808Sdelphij * it is about to die. */ 259170808Sdelphij if (node->tn_links < 1) 260170808Sdelphij return (ENOENT); 261170808Sdelphij 262170808Sdelphij /* If the file is marked append-only, deny write requests. */ 263170808Sdelphij if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE) 264170808Sdelphij error = EPERM; 265170808Sdelphij else { 266170808Sdelphij error = 0; 267250190Skib /* For regular files, the call below is nop. */ 268171070Sdelphij vnode_create_vobject(vp, node->tn_size, v->a_td); 269170808Sdelphij } 270170808Sdelphij 271176559Sattilio MPASS(VOP_ISLOCKED(vp)); 272170808Sdelphij return error; 273170808Sdelphij} 274170808Sdelphij 275170808Sdelphij/* --------------------------------------------------------------------- */ 276170808Sdelphij 277171069Sdelphijstatic int 278170808Sdelphijtmpfs_close(struct vop_close_args *v) 279170808Sdelphij{ 280170808Sdelphij struct vnode *vp = v->a_vp; 281170808Sdelphij 282218949Salc /* Update node times. */ 283218949Salc tmpfs_update(vp); 284170808Sdelphij 285218949Salc return (0); 286170808Sdelphij} 287170808Sdelphij 288170808Sdelphij/* --------------------------------------------------------------------- */ 289170808Sdelphij 290170808Sdelphijint 291170808Sdelphijtmpfs_access(struct vop_access_args *v) 292170808Sdelphij{ 293170808Sdelphij struct vnode *vp = v->a_vp; 294184413Strasz accmode_t accmode = v->a_accmode; 295170808Sdelphij struct ucred *cred = v->a_cred; 296170808Sdelphij 297170808Sdelphij int error; 298170808Sdelphij struct tmpfs_node *node; 299170808Sdelphij 300176559Sattilio MPASS(VOP_ISLOCKED(vp)); 301170808Sdelphij 302170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 303170808Sdelphij 304170808Sdelphij switch (vp->v_type) { 305170808Sdelphij case VDIR: 306170808Sdelphij /* FALLTHROUGH */ 307170808Sdelphij case VLNK: 308170808Sdelphij /* FALLTHROUGH */ 309170808Sdelphij case VREG: 310184413Strasz if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) { 311170808Sdelphij error = EROFS; 312170808Sdelphij goto out; 313170808Sdelphij } 314170808Sdelphij break; 315170808Sdelphij 316170808Sdelphij case VBLK: 317170808Sdelphij /* FALLTHROUGH */ 318170808Sdelphij case VCHR: 319170808Sdelphij /* FALLTHROUGH */ 320170808Sdelphij case VSOCK: 321170808Sdelphij /* FALLTHROUGH */ 322170808Sdelphij case VFIFO: 323170808Sdelphij break; 324170808Sdelphij 325170808Sdelphij default: 326170808Sdelphij error = EINVAL; 327170808Sdelphij goto out; 328170808Sdelphij } 329170808Sdelphij 330184413Strasz if (accmode & VWRITE && node->tn_flags & IMMUTABLE) { 331170808Sdelphij error = EPERM; 332170808Sdelphij goto out; 333170808Sdelphij } 334170808Sdelphij 335170808Sdelphij error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, 336184413Strasz node->tn_gid, accmode, cred, NULL); 337170808Sdelphij 338170808Sdelphijout: 339176559Sattilio MPASS(VOP_ISLOCKED(vp)); 340170808Sdelphij 341170808Sdelphij return error; 342170808Sdelphij} 343170808Sdelphij 344170808Sdelphij/* --------------------------------------------------------------------- */ 345170808Sdelphij 346170808Sdelphijint 347170808Sdelphijtmpfs_getattr(struct vop_getattr_args *v) 348170808Sdelphij{ 349170808Sdelphij struct vnode *vp = v->a_vp; 350170808Sdelphij struct vattr *vap = v->a_vap; 351170808Sdelphij 352170808Sdelphij struct tmpfs_node *node; 353170808Sdelphij 354170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 355170808Sdelphij 356170808Sdelphij tmpfs_update(vp); 357170808Sdelphij 358170808Sdelphij vap->va_type = vp->v_type; 359170808Sdelphij vap->va_mode = node->tn_mode; 360170808Sdelphij vap->va_nlink = node->tn_links; 361170808Sdelphij vap->va_uid = node->tn_uid; 362170808Sdelphij vap->va_gid = node->tn_gid; 363170808Sdelphij vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 364170808Sdelphij vap->va_fileid = node->tn_id; 365170808Sdelphij vap->va_size = node->tn_size; 366170808Sdelphij vap->va_blocksize = PAGE_SIZE; 367170808Sdelphij vap->va_atime = node->tn_atime; 368170808Sdelphij vap->va_mtime = node->tn_mtime; 369170808Sdelphij vap->va_ctime = node->tn_ctime; 370170808Sdelphij vap->va_birthtime = node->tn_birthtime; 371170808Sdelphij vap->va_gen = node->tn_gen; 372170808Sdelphij vap->va_flags = node->tn_flags; 373170808Sdelphij vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 374183214Skib node->tn_rdev : NODEV; 375170808Sdelphij vap->va_bytes = round_page(node->tn_size); 376183212Skib vap->va_filerev = 0; 377170808Sdelphij 378170808Sdelphij return 0; 379170808Sdelphij} 380170808Sdelphij 381170808Sdelphij/* --------------------------------------------------------------------- */ 382170808Sdelphij 383170808Sdelphij/* XXX Should this operation be atomic? I think it should, but code in 384170808Sdelphij * XXX other places (e.g., ufs) doesn't seem to be... */ 385170808Sdelphijint 386170808Sdelphijtmpfs_setattr(struct vop_setattr_args *v) 387170808Sdelphij{ 388170808Sdelphij struct vnode *vp = v->a_vp; 389170808Sdelphij struct vattr *vap = v->a_vap; 390170808Sdelphij struct ucred *cred = v->a_cred; 391182371Sattilio struct thread *td = curthread; 392170808Sdelphij 393170808Sdelphij int error; 394170808Sdelphij 395176559Sattilio MPASS(VOP_ISLOCKED(vp)); 396170808Sdelphij 397170808Sdelphij error = 0; 398170808Sdelphij 399170808Sdelphij /* Abort if any unsettable attribute is given. */ 400170808Sdelphij if (vap->va_type != VNON || 401170808Sdelphij vap->va_nlink != VNOVAL || 402170808Sdelphij vap->va_fsid != VNOVAL || 403170808Sdelphij vap->va_fileid != VNOVAL || 404170808Sdelphij vap->va_blocksize != VNOVAL || 405170808Sdelphij vap->va_gen != VNOVAL || 406170808Sdelphij vap->va_rdev != VNOVAL || 407170808Sdelphij vap->va_bytes != VNOVAL) 408170808Sdelphij error = EINVAL; 409170808Sdelphij 410170808Sdelphij if (error == 0 && (vap->va_flags != VNOVAL)) 411182371Sattilio error = tmpfs_chflags(vp, vap->va_flags, cred, td); 412170808Sdelphij 413170808Sdelphij if (error == 0 && (vap->va_size != VNOVAL)) 414182371Sattilio error = tmpfs_chsize(vp, vap->va_size, cred, td); 415170808Sdelphij 416170808Sdelphij if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 417182371Sattilio error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td); 418170808Sdelphij 419170808Sdelphij if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) 420182371Sattilio error = tmpfs_chmod(vp, vap->va_mode, cred, td); 421170808Sdelphij 422170808Sdelphij if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL && 423170808Sdelphij vap->va_atime.tv_nsec != VNOVAL) || 424170808Sdelphij (vap->va_mtime.tv_sec != VNOVAL && 425170808Sdelphij vap->va_mtime.tv_nsec != VNOVAL) || 426170808Sdelphij (vap->va_birthtime.tv_sec != VNOVAL && 427170808Sdelphij vap->va_birthtime.tv_nsec != VNOVAL))) 428171070Sdelphij error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 429182371Sattilio &vap->va_birthtime, vap->va_vaflags, cred, td); 430170808Sdelphij 431170808Sdelphij /* Update the node times. We give preference to the error codes 432170808Sdelphij * generated by this function rather than the ones that may arise 433170808Sdelphij * from tmpfs_update. */ 434170808Sdelphij tmpfs_update(vp); 435170808Sdelphij 436176559Sattilio MPASS(VOP_ISLOCKED(vp)); 437170808Sdelphij 438170808Sdelphij return error; 439170808Sdelphij} 440170808Sdelphij 441197850Sdelphijstatic int 442197850Sdelphijtmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx, 443197850Sdelphij vm_offset_t offset, size_t tlen, struct uio *uio) 444197850Sdelphij{ 445197850Sdelphij vm_page_t m; 446230120Salc int error, rv; 447171489Sdelphij 448248084Sattilio VM_OBJECT_WLOCK(tobj); 449250030Skib 450250030Skib /* 451250030Skib * The kern_sendfile() code calls vn_rdwr() with the page 452250030Skib * soft-busied. Ignore the soft-busy state here. Parallel 453250030Skib * reads of the page content from disk are prevented by 454250030Skib * VPO_BUSY. 455250030Skib * 456250030Skib * Although the tmpfs vnode lock is held here, it is 457250030Skib * nonetheless safe to sleep waiting for a free page. The 458250030Skib * pageout daemon does not need to acquire the tmpfs vnode 459250030Skib * lock to page out tobj's pages because tobj is a OBJT_SWAP 460250030Skib * type object. 461250030Skib */ 462250030Skib m = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY | 463250030Skib VM_ALLOC_IGN_SBUSY); 464197850Sdelphij if (m->valid != VM_PAGE_BITS_ALL) { 465197850Sdelphij if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 466230120Salc rv = vm_pager_get_pages(tobj, &m, 1, 0); 467250030Skib m = vm_page_lookup(tobj, idx); 468250030Skib if (m == NULL) { 469250030Skib printf( 470250030Skib "tmpfs: vm_obj %p idx %jd null lookup rv %d\n", 471250030Skib tobj, idx, rv); 472250030Skib return (EIO); 473250030Skib } 474230120Salc if (rv != VM_PAGER_OK) { 475250030Skib printf( 476250030Skib "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n", 477250030Skib tobj, idx, m->valid, rv); 478230120Salc vm_page_lock(m); 479230120Salc vm_page_free(m); 480230120Salc vm_page_unlock(m); 481248084Sattilio VM_OBJECT_WUNLOCK(tobj); 482230120Salc return (EIO); 483197850Sdelphij } 484197850Sdelphij } else 485197850Sdelphij vm_page_zero_invalid(m, TRUE); 486197850Sdelphij } 487250030Skib vm_page_lock(m); 488250030Skib vm_page_hold(m); 489250030Skib vm_page_wakeup(m); 490250030Skib vm_page_unlock(m); 491248084Sattilio VM_OBJECT_WUNLOCK(tobj); 492197850Sdelphij error = uiomove_fromphys(&m, offset, tlen, uio); 493248084Sattilio VM_OBJECT_WLOCK(tobj); 494207573Salc vm_page_lock(m); 495250030Skib vm_page_unhold(m); 496250030Skib vm_page_deactivate(m); 497250030Skib /* Requeue to maintain LRU ordering. */ 498250188Skib if (m->queue != PQ_NONE) 499250188Skib vm_page_requeue(m); 500207573Salc vm_page_unlock(m); 501248084Sattilio VM_OBJECT_WUNLOCK(tobj); 502197850Sdelphij 503197850Sdelphij return (error); 504197850Sdelphij} 505197850Sdelphij 506170808Sdelphijstatic int 507170808Sdelphijtmpfs_read(struct vop_read_args *v) 508170808Sdelphij{ 509170808Sdelphij struct vnode *vp = v->a_vp; 510170808Sdelphij struct uio *uio = v->a_uio; 511170808Sdelphij struct tmpfs_node *node; 512170808Sdelphij vm_object_t uobj; 513171489Sdelphij size_t len; 514171489Sdelphij int resid; 515174265Swkoszek int error = 0; 516250030Skib vm_pindex_t idx; 517250030Skib vm_offset_t offset; 518250030Skib off_t addr; 519250030Skib size_t tlen; 520170808Sdelphij 521170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 522170808Sdelphij 523170808Sdelphij if (vp->v_type != VREG) { 524170808Sdelphij error = EISDIR; 525170808Sdelphij goto out; 526170808Sdelphij } 527170808Sdelphij 528170808Sdelphij if (uio->uio_offset < 0) { 529170808Sdelphij error = EINVAL; 530170808Sdelphij goto out; 531170808Sdelphij } 532170808Sdelphij 533170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED; 534170808Sdelphij 535170808Sdelphij uobj = node->tn_reg.tn_aobj; 536171489Sdelphij while ((resid = uio->uio_resid) > 0) { 537171489Sdelphij error = 0; 538171489Sdelphij if (node->tn_size <= uio->uio_offset) 539171489Sdelphij break; 540171489Sdelphij len = MIN(node->tn_size - uio->uio_offset, resid); 541171489Sdelphij if (len == 0) 542171489Sdelphij break; 543250030Skib addr = uio->uio_offset; 544250030Skib idx = OFF_TO_IDX(addr); 545250030Skib offset = addr & PAGE_MASK; 546250030Skib tlen = MIN(PAGE_SIZE - offset, len); 547250030Skib error = tmpfs_nocacheread(uobj, idx, offset, tlen, uio); 548171489Sdelphij if ((error != 0) || (resid == uio->uio_resid)) 549171489Sdelphij break; 550171489Sdelphij } 551170808Sdelphij 552170808Sdelphijout: 553170808Sdelphij 554170808Sdelphij return error; 555170808Sdelphij} 556170808Sdelphij 557170808Sdelphij/* --------------------------------------------------------------------- */ 558170808Sdelphij 559171069Sdelphijstatic int 560250030Skibtmpfs_mappedwrite(vm_object_t tobj, size_t len, struct uio *uio) 561171489Sdelphij{ 562171489Sdelphij vm_pindex_t idx; 563250030Skib vm_page_t tpg; 564188929Salc vm_offset_t offset; 565188929Salc off_t addr; 566171489Sdelphij size_t tlen; 567230120Salc int error, rv; 568171489Sdelphij 569174265Swkoszek error = 0; 570174265Swkoszek 571171489Sdelphij addr = uio->uio_offset; 572171489Sdelphij idx = OFF_TO_IDX(addr); 573171489Sdelphij offset = addr & PAGE_MASK; 574171489Sdelphij tlen = MIN(PAGE_SIZE - offset, len); 575171489Sdelphij 576248084Sattilio VM_OBJECT_WLOCK(tobj); 577250030Skib tpg = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 578171489Sdelphij if (tpg->valid != VM_PAGE_BITS_ALL) { 579194124Salc if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 580230120Salc rv = vm_pager_get_pages(tobj, &tpg, 1, 0); 581250030Skib tpg = vm_page_lookup(tobj, idx); 582250030Skib if (tpg == NULL) { 583250030Skib printf( 584250030Skib "tmpfs: vm_obj %p idx %jd null lookup rv %d\n", 585250030Skib tobj, idx, rv); 586250030Skib return (EIO); 587250030Skib } 588230120Salc if (rv != VM_PAGER_OK) { 589250030Skib printf( 590250030Skib "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n", 591250030Skib tobj, idx, tpg->valid, rv); 592230120Salc vm_page_lock(tpg); 593230120Salc vm_page_free(tpg); 594230120Salc vm_page_unlock(tpg); 595250030Skib VM_OBJECT_WUNLOCK(tobj); 596250030Skib return (EIO); 597171489Sdelphij } 598171489Sdelphij } else 599171489Sdelphij vm_page_zero_invalid(tpg, TRUE); 600171489Sdelphij } 601250030Skib vm_page_lock(tpg); 602250030Skib vm_page_hold(tpg); 603250030Skib vm_page_wakeup(tpg); 604250030Skib vm_page_unlock(tpg); 605248084Sattilio VM_OBJECT_WUNLOCK(tobj); 606250030Skib error = uiomove_fromphys(&tpg, offset, tlen, uio); 607248084Sattilio VM_OBJECT_WLOCK(tobj); 608250030Skib if (error == 0) 609171489Sdelphij vm_page_dirty(tpg); 610209226Salc vm_page_lock(tpg); 611250030Skib vm_page_unhold(tpg); 612250030Skib vm_page_deactivate(tpg); 613250030Skib /* Requeue to maintain LRU ordering. */ 614250188Skib if (tpg->queue != PQ_NONE) 615250188Skib vm_page_requeue(tpg); 616207573Salc vm_page_unlock(tpg); 617248084Sattilio VM_OBJECT_WUNLOCK(tobj); 618171489Sdelphij 619171489Sdelphij return (error); 620171489Sdelphij} 621171489Sdelphij 622171489Sdelphijstatic int 623170808Sdelphijtmpfs_write(struct vop_write_args *v) 624170808Sdelphij{ 625170808Sdelphij struct vnode *vp = v->a_vp; 626170808Sdelphij struct uio *uio = v->a_uio; 627170808Sdelphij int ioflag = v->a_ioflag; 628170808Sdelphij 629170808Sdelphij boolean_t extended; 630171489Sdelphij int error = 0; 631170808Sdelphij off_t oldsize; 632170808Sdelphij struct tmpfs_node *node; 633170808Sdelphij vm_object_t uobj; 634171489Sdelphij size_t len; 635171489Sdelphij int resid; 636170808Sdelphij 637170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 638170808Sdelphij oldsize = node->tn_size; 639170808Sdelphij 640170808Sdelphij if (uio->uio_offset < 0 || vp->v_type != VREG) { 641170808Sdelphij error = EINVAL; 642170808Sdelphij goto out; 643170808Sdelphij } 644170808Sdelphij 645170808Sdelphij if (uio->uio_resid == 0) { 646170808Sdelphij error = 0; 647170808Sdelphij goto out; 648170808Sdelphij } 649170808Sdelphij 650170808Sdelphij if (ioflag & IO_APPEND) 651170808Sdelphij uio->uio_offset = node->tn_size; 652171070Sdelphij 653171070Sdelphij if (uio->uio_offset + uio->uio_resid > 654170808Sdelphij VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 655170808Sdelphij return (EFBIG); 656170808Sdelphij 657207719Strasz if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 658207662Strasz return (EFBIG); 659170808Sdelphij 660170808Sdelphij extended = uio->uio_offset + uio->uio_resid > node->tn_size; 661170808Sdelphij if (extended) { 662230180Salc error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid, 663230180Salc FALSE); 664170808Sdelphij if (error != 0) 665170808Sdelphij goto out; 666170808Sdelphij } 667170808Sdelphij 668170808Sdelphij uobj = node->tn_reg.tn_aobj; 669171489Sdelphij while ((resid = uio->uio_resid) > 0) { 670171489Sdelphij if (node->tn_size <= uio->uio_offset) 671171489Sdelphij break; 672171489Sdelphij len = MIN(node->tn_size - uio->uio_offset, resid); 673171489Sdelphij if (len == 0) 674171489Sdelphij break; 675250030Skib error = tmpfs_mappedwrite(uobj, len, uio); 676171489Sdelphij if ((error != 0) || (resid == uio->uio_resid)) 677171489Sdelphij break; 678171489Sdelphij } 679170808Sdelphij 680170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 681170808Sdelphij (extended ? TMPFS_NODE_CHANGED : 0); 682170808Sdelphij 683170808Sdelphij if (node->tn_mode & (S_ISUID | S_ISGID)) { 684170808Sdelphij if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0)) 685170808Sdelphij node->tn_mode &= ~(S_ISUID | S_ISGID); 686170808Sdelphij } 687170808Sdelphij 688170808Sdelphij if (error != 0) 689230180Salc (void)tmpfs_reg_resize(vp, oldsize, TRUE); 690170808Sdelphij 691170808Sdelphijout: 692170808Sdelphij MPASS(IMPLIES(error == 0, uio->uio_resid == 0)); 693170808Sdelphij MPASS(IMPLIES(error != 0, oldsize == node->tn_size)); 694170808Sdelphij 695170808Sdelphij return error; 696170808Sdelphij} 697170808Sdelphij 698170808Sdelphij/* --------------------------------------------------------------------- */ 699170808Sdelphij 700171069Sdelphijstatic int 701170808Sdelphijtmpfs_fsync(struct vop_fsync_args *v) 702170808Sdelphij{ 703170808Sdelphij struct vnode *vp = v->a_vp; 704170808Sdelphij 705176559Sattilio MPASS(VOP_ISLOCKED(vp)); 706170808Sdelphij 707170808Sdelphij tmpfs_update(vp); 708170808Sdelphij 709170808Sdelphij return 0; 710170808Sdelphij} 711170808Sdelphij 712170808Sdelphij/* --------------------------------------------------------------------- */ 713170808Sdelphij 714171069Sdelphijstatic int 715170808Sdelphijtmpfs_remove(struct vop_remove_args *v) 716170808Sdelphij{ 717170808Sdelphij struct vnode *dvp = v->a_dvp; 718170808Sdelphij struct vnode *vp = v->a_vp; 719170808Sdelphij 720170808Sdelphij int error; 721170808Sdelphij struct tmpfs_dirent *de; 722170808Sdelphij struct tmpfs_mount *tmp; 723170808Sdelphij struct tmpfs_node *dnode; 724170808Sdelphij struct tmpfs_node *node; 725170808Sdelphij 726176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 727176559Sattilio MPASS(VOP_ISLOCKED(vp)); 728170808Sdelphij 729170808Sdelphij if (vp->v_type == VDIR) { 730170808Sdelphij error = EISDIR; 731170808Sdelphij goto out; 732170808Sdelphij } 733170808Sdelphij 734170808Sdelphij dnode = VP_TO_TMPFS_DIR(dvp); 735170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 736170808Sdelphij tmp = VFS_TO_TMPFS(vp->v_mount); 737188318Skib de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 738170808Sdelphij MPASS(de != NULL); 739170808Sdelphij 740170808Sdelphij /* Files marked as immutable or append-only cannot be deleted. */ 741170808Sdelphij if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 742170808Sdelphij (dnode->tn_flags & APPEND)) { 743170808Sdelphij error = EPERM; 744170808Sdelphij goto out; 745170808Sdelphij } 746170808Sdelphij 747170808Sdelphij /* Remove the entry from the directory; as it is a file, we do not 748170808Sdelphij * have to change the number of hard links of the directory. */ 749170808Sdelphij tmpfs_dir_detach(dvp, de); 750211598Sed if (v->a_cnp->cn_flags & DOWHITEOUT) 751211598Sed tmpfs_dir_whiteout_add(dvp, v->a_cnp); 752170808Sdelphij 753170808Sdelphij /* Free the directory entry we just deleted. Note that the node 754170808Sdelphij * referred by it will not be removed until the vnode is really 755170808Sdelphij * reclaimed. */ 756245115Sgleb tmpfs_free_dirent(tmp, de); 757170808Sdelphij 758218949Salc node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED; 759170808Sdelphij error = 0; 760170808Sdelphij 761170808Sdelphijout: 762170808Sdelphij 763170808Sdelphij return error; 764170808Sdelphij} 765170808Sdelphij 766170808Sdelphij/* --------------------------------------------------------------------- */ 767170808Sdelphij 768171069Sdelphijstatic int 769170808Sdelphijtmpfs_link(struct vop_link_args *v) 770170808Sdelphij{ 771170808Sdelphij struct vnode *dvp = v->a_tdvp; 772170808Sdelphij struct vnode *vp = v->a_vp; 773170808Sdelphij struct componentname *cnp = v->a_cnp; 774170808Sdelphij 775170808Sdelphij int error; 776170808Sdelphij struct tmpfs_dirent *de; 777170808Sdelphij struct tmpfs_node *node; 778170808Sdelphij 779176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 780170808Sdelphij MPASS(cnp->cn_flags & HASBUF); 781170808Sdelphij MPASS(dvp != vp); /* XXX When can this be false? */ 782170808Sdelphij 783170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 784170808Sdelphij 785170808Sdelphij /* XXX: Why aren't the following two tests done by the caller? */ 786170808Sdelphij 787170808Sdelphij /* Hard links of directories are forbidden. */ 788170808Sdelphij if (vp->v_type == VDIR) { 789170808Sdelphij error = EPERM; 790170808Sdelphij goto out; 791170808Sdelphij } 792170808Sdelphij 793170808Sdelphij /* Cannot create cross-device links. */ 794170808Sdelphij if (dvp->v_mount != vp->v_mount) { 795170808Sdelphij error = EXDEV; 796170808Sdelphij goto out; 797170808Sdelphij } 798170808Sdelphij 799170808Sdelphij /* Ensure that we do not overflow the maximum number of links imposed 800170808Sdelphij * by the system. */ 801170808Sdelphij MPASS(node->tn_links <= LINK_MAX); 802170808Sdelphij if (node->tn_links == LINK_MAX) { 803170808Sdelphij error = EMLINK; 804170808Sdelphij goto out; 805170808Sdelphij } 806170808Sdelphij 807170808Sdelphij /* We cannot create links of files marked immutable or append-only. */ 808170808Sdelphij if (node->tn_flags & (IMMUTABLE | APPEND)) { 809170808Sdelphij error = EPERM; 810170808Sdelphij goto out; 811170808Sdelphij } 812170808Sdelphij 813170808Sdelphij /* Allocate a new directory entry to represent the node. */ 814170808Sdelphij error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 815170808Sdelphij cnp->cn_nameptr, cnp->cn_namelen, &de); 816170808Sdelphij if (error != 0) 817170808Sdelphij goto out; 818170808Sdelphij 819170808Sdelphij /* Insert the new directory entry into the appropriate directory. */ 820211598Sed if (cnp->cn_flags & ISWHITEOUT) 821211598Sed tmpfs_dir_whiteout_remove(dvp, cnp); 822170808Sdelphij tmpfs_dir_attach(dvp, de); 823170808Sdelphij 824170808Sdelphij /* vp link count has changed, so update node times. */ 825170808Sdelphij node->tn_status |= TMPFS_NODE_CHANGED; 826170808Sdelphij tmpfs_update(vp); 827170808Sdelphij 828170808Sdelphij error = 0; 829171070Sdelphij 830170808Sdelphijout: 831170808Sdelphij return error; 832170808Sdelphij} 833170808Sdelphij 834170808Sdelphij/* --------------------------------------------------------------------- */ 835170808Sdelphij 836232960Sgleb/* 837232960Sgleb * We acquire all but fdvp locks using non-blocking acquisitions. If we 838232960Sgleb * fail to acquire any lock in the path we will drop all held locks, 839232960Sgleb * acquire the new lock in a blocking fashion, and then release it and 840232960Sgleb * restart the rename. This acquire/release step ensures that we do not 841232960Sgleb * spin on a lock waiting for release. On error release all vnode locks 842232960Sgleb * and decrement references the way tmpfs_rename() would do. 843232960Sgleb */ 844171069Sdelphijstatic int 845232960Sglebtmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp, 846232960Sgleb struct vnode *tdvp, struct vnode **tvpp, 847232960Sgleb struct componentname *fcnp, struct componentname *tcnp) 848232960Sgleb{ 849232960Sgleb struct vnode *nvp; 850232960Sgleb struct mount *mp; 851232960Sgleb struct tmpfs_dirent *de; 852232960Sgleb int error, restarts = 0; 853232960Sgleb 854232960Sgleb VOP_UNLOCK(tdvp, 0); 855232960Sgleb if (*tvpp != NULL && *tvpp != tdvp) 856232960Sgleb VOP_UNLOCK(*tvpp, 0); 857232960Sgleb mp = fdvp->v_mount; 858232960Sgleb 859232960Sglebrelock: 860232960Sgleb restarts += 1; 861232960Sgleb error = vn_lock(fdvp, LK_EXCLUSIVE); 862232960Sgleb if (error) 863232960Sgleb goto releout; 864232960Sgleb if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 865232960Sgleb VOP_UNLOCK(fdvp, 0); 866232960Sgleb error = vn_lock(tdvp, LK_EXCLUSIVE); 867232960Sgleb if (error) 868232960Sgleb goto releout; 869232960Sgleb VOP_UNLOCK(tdvp, 0); 870232960Sgleb goto relock; 871232960Sgleb } 872232960Sgleb /* 873232960Sgleb * Re-resolve fvp to be certain it still exists and fetch the 874232960Sgleb * correct vnode. 875232960Sgleb */ 876232960Sgleb de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp); 877232960Sgleb if (de == NULL) { 878232960Sgleb VOP_UNLOCK(fdvp, 0); 879232960Sgleb VOP_UNLOCK(tdvp, 0); 880232960Sgleb if ((fcnp->cn_flags & ISDOTDOT) != 0 || 881232960Sgleb (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 882232960Sgleb error = EINVAL; 883232960Sgleb else 884232960Sgleb error = ENOENT; 885232960Sgleb goto releout; 886232960Sgleb } 887232960Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp); 888232960Sgleb if (error != 0) { 889232960Sgleb VOP_UNLOCK(fdvp, 0); 890232960Sgleb VOP_UNLOCK(tdvp, 0); 891232960Sgleb if (error != EBUSY) 892232960Sgleb goto releout; 893232960Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp); 894232960Sgleb if (error != 0) 895232960Sgleb goto releout; 896232960Sgleb VOP_UNLOCK(nvp, 0); 897232960Sgleb /* 898232960Sgleb * Concurrent rename race. 899232960Sgleb */ 900232960Sgleb if (nvp == tdvp) { 901232960Sgleb vrele(nvp); 902232960Sgleb error = EINVAL; 903232960Sgleb goto releout; 904232960Sgleb } 905232960Sgleb vrele(*fvpp); 906232960Sgleb *fvpp = nvp; 907232960Sgleb goto relock; 908232960Sgleb } 909232960Sgleb vrele(*fvpp); 910232960Sgleb *fvpp = nvp; 911232960Sgleb VOP_UNLOCK(*fvpp, 0); 912232960Sgleb /* 913232960Sgleb * Re-resolve tvp and acquire the vnode lock if present. 914232960Sgleb */ 915232960Sgleb de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp); 916232960Sgleb /* 917232960Sgleb * If tvp disappeared we just carry on. 918232960Sgleb */ 919232960Sgleb if (de == NULL && *tvpp != NULL) { 920232960Sgleb vrele(*tvpp); 921232960Sgleb *tvpp = NULL; 922232960Sgleb } 923232960Sgleb /* 924232960Sgleb * Get the tvp ino if the lookup succeeded. We may have to restart 925232960Sgleb * if the non-blocking acquire fails. 926232960Sgleb */ 927232960Sgleb if (de != NULL) { 928232960Sgleb nvp = NULL; 929232960Sgleb error = tmpfs_alloc_vp(mp, de->td_node, 930232960Sgleb LK_EXCLUSIVE | LK_NOWAIT, &nvp); 931232960Sgleb if (*tvpp != NULL) 932232960Sgleb vrele(*tvpp); 933232960Sgleb *tvpp = nvp; 934232960Sgleb if (error != 0) { 935232960Sgleb VOP_UNLOCK(fdvp, 0); 936232960Sgleb VOP_UNLOCK(tdvp, 0); 937232960Sgleb if (error != EBUSY) 938232960Sgleb goto releout; 939232960Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, 940232960Sgleb &nvp); 941232960Sgleb if (error != 0) 942232960Sgleb goto releout; 943232960Sgleb VOP_UNLOCK(nvp, 0); 944232960Sgleb /* 945232960Sgleb * fdvp contains fvp, thus tvp (=fdvp) is not empty. 946232960Sgleb */ 947232960Sgleb if (nvp == fdvp) { 948232960Sgleb error = ENOTEMPTY; 949232960Sgleb goto releout; 950232960Sgleb } 951232960Sgleb goto relock; 952232960Sgleb } 953232960Sgleb } 954232960Sgleb tmpfs_rename_restarts += restarts; 955232960Sgleb 956232960Sgleb return (0); 957232960Sgleb 958232960Sglebreleout: 959232960Sgleb vrele(fdvp); 960232960Sgleb vrele(*fvpp); 961232960Sgleb vrele(tdvp); 962232960Sgleb if (*tvpp != NULL) 963232960Sgleb vrele(*tvpp); 964232960Sgleb tmpfs_rename_restarts += restarts; 965232960Sgleb 966232960Sgleb return (error); 967232960Sgleb} 968232960Sgleb 969232960Sglebstatic int 970170808Sdelphijtmpfs_rename(struct vop_rename_args *v) 971170808Sdelphij{ 972170808Sdelphij struct vnode *fdvp = v->a_fdvp; 973170808Sdelphij struct vnode *fvp = v->a_fvp; 974170808Sdelphij struct componentname *fcnp = v->a_fcnp; 975170808Sdelphij struct vnode *tdvp = v->a_tdvp; 976170808Sdelphij struct vnode *tvp = v->a_tvp; 977170808Sdelphij struct componentname *tcnp = v->a_tcnp; 978232960Sgleb struct mount *mp = NULL; 979170808Sdelphij 980170808Sdelphij char *newname; 981170808Sdelphij int error; 982170808Sdelphij struct tmpfs_dirent *de; 983197953Sdelphij struct tmpfs_mount *tmp; 984170808Sdelphij struct tmpfs_node *fdnode; 985170808Sdelphij struct tmpfs_node *fnode; 986171799Sdelphij struct tmpfs_node *tnode; 987170808Sdelphij struct tmpfs_node *tdnode; 988170808Sdelphij 989176559Sattilio MPASS(VOP_ISLOCKED(tdvp)); 990176559Sattilio MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp))); 991170808Sdelphij MPASS(fcnp->cn_flags & HASBUF); 992170808Sdelphij MPASS(tcnp->cn_flags & HASBUF); 993170808Sdelphij 994170808Sdelphij /* Disallow cross-device renames. 995170808Sdelphij * XXX Why isn't this done by the caller? */ 996170808Sdelphij if (fvp->v_mount != tdvp->v_mount || 997170808Sdelphij (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 998170808Sdelphij error = EXDEV; 999170808Sdelphij goto out; 1000170808Sdelphij } 1001170808Sdelphij 1002170808Sdelphij /* If source and target are the same file, there is nothing to do. */ 1003170808Sdelphij if (fvp == tvp) { 1004170808Sdelphij error = 0; 1005170808Sdelphij goto out; 1006170808Sdelphij } 1007170808Sdelphij 1008173725Sdelphij /* If we need to move the directory between entries, lock the 1009173725Sdelphij * source so that we can safely operate on it. */ 1010232960Sgleb if (fdvp != tdvp && fdvp != tvp) { 1011232960Sgleb if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1012232960Sgleb mp = tdvp->v_mount; 1013232960Sgleb error = vfs_busy(mp, 0); 1014232960Sgleb if (error != 0) { 1015232960Sgleb mp = NULL; 1016232960Sgleb goto out; 1017232960Sgleb } 1018232960Sgleb error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp, 1019232960Sgleb fcnp, tcnp); 1020232960Sgleb if (error != 0) { 1021232960Sgleb vfs_unbusy(mp); 1022232960Sgleb return (error); 1023232960Sgleb } 1024232960Sgleb ASSERT_VOP_ELOCKED(fdvp, 1025232960Sgleb "tmpfs_rename: fdvp not locked"); 1026232960Sgleb ASSERT_VOP_ELOCKED(tdvp, 1027232960Sgleb "tmpfs_rename: tdvp not locked"); 1028232960Sgleb if (tvp != NULL) 1029232960Sgleb ASSERT_VOP_ELOCKED(tvp, 1030232960Sgleb "tmpfs_rename: tvp not locked"); 1031232960Sgleb if (fvp == tvp) { 1032232960Sgleb error = 0; 1033232960Sgleb goto out_locked; 1034232960Sgleb } 1035232960Sgleb } 1036232960Sgleb } 1037232960Sgleb 1038232960Sgleb tmp = VFS_TO_TMPFS(tdvp->v_mount); 1039232960Sgleb tdnode = VP_TO_TMPFS_DIR(tdvp); 1040232960Sgleb tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp); 1041173725Sdelphij fdnode = VP_TO_TMPFS_DIR(fdvp); 1042173725Sdelphij fnode = VP_TO_TMPFS_NODE(fvp); 1043188318Skib de = tmpfs_dir_lookup(fdnode, fnode, fcnp); 1044173725Sdelphij 1045212305Sivoras /* Entry can disappear before we lock fdvp, 1046212305Sivoras * also avoid manipulating '.' and '..' entries. */ 1047170808Sdelphij if (de == NULL) { 1048212305Sivoras if ((fcnp->cn_flags & ISDOTDOT) != 0 || 1049212305Sivoras (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 1050212305Sivoras error = EINVAL; 1051212305Sivoras else 1052212305Sivoras error = ENOENT; 1053173725Sdelphij goto out_locked; 1054170808Sdelphij } 1055170808Sdelphij MPASS(de->td_node == fnode); 1056170808Sdelphij 1057171070Sdelphij /* If re-naming a directory to another preexisting directory 1058170808Sdelphij * ensure that the target directory is empty so that its 1059171070Sdelphij * removal causes no side effects. 1060170808Sdelphij * Kern_rename gurantees the destination to be a directory 1061170808Sdelphij * if the source is one. */ 1062170808Sdelphij if (tvp != NULL) { 1063171799Sdelphij MPASS(tnode != NULL); 1064171070Sdelphij 1065170808Sdelphij if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 1066170808Sdelphij (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 1067170808Sdelphij error = EPERM; 1068173725Sdelphij goto out_locked; 1069170808Sdelphij } 1070170808Sdelphij 1071171799Sdelphij if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 1072171799Sdelphij if (tnode->tn_size > 0) { 1073171799Sdelphij error = ENOTEMPTY; 1074173725Sdelphij goto out_locked; 1075171799Sdelphij } 1076171799Sdelphij } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 1077171799Sdelphij error = ENOTDIR; 1078173725Sdelphij goto out_locked; 1079171799Sdelphij } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 1080171799Sdelphij error = EISDIR; 1081173725Sdelphij goto out_locked; 1082171799Sdelphij } else { 1083171799Sdelphij MPASS(fnode->tn_type != VDIR && 1084171799Sdelphij tnode->tn_type != VDIR); 1085170808Sdelphij } 1086170808Sdelphij } 1087170808Sdelphij 1088170808Sdelphij if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) 1089170808Sdelphij || (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 1090170808Sdelphij error = EPERM; 1091170808Sdelphij goto out_locked; 1092170808Sdelphij } 1093170808Sdelphij 1094170808Sdelphij /* Ensure that we have enough memory to hold the new name, if it 1095170808Sdelphij * has to be changed. */ 1096170808Sdelphij if (fcnp->cn_namelen != tcnp->cn_namelen || 1097183299Sobrien bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { 1098171087Sdelphij newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK); 1099170808Sdelphij } else 1100170808Sdelphij newname = NULL; 1101170808Sdelphij 1102170808Sdelphij /* If the node is being moved to another directory, we have to do 1103170808Sdelphij * the move. */ 1104170808Sdelphij if (fdnode != tdnode) { 1105170808Sdelphij /* In case we are moving a directory, we have to adjust its 1106170808Sdelphij * parent to point to the new parent. */ 1107170808Sdelphij if (de->td_node->tn_type == VDIR) { 1108170808Sdelphij struct tmpfs_node *n; 1109170808Sdelphij 1110170808Sdelphij /* Ensure the target directory is not a child of the 1111170808Sdelphij * directory being moved. Otherwise, we'd end up 1112170808Sdelphij * with stale nodes. */ 1113170808Sdelphij n = tdnode; 1114197953Sdelphij /* TMPFS_LOCK garanties that no nodes are freed while 1115197953Sdelphij * traversing the list. Nodes can only be marked as 1116197953Sdelphij * removed: tn_parent == NULL. */ 1117197953Sdelphij TMPFS_LOCK(tmp); 1118197953Sdelphij TMPFS_NODE_LOCK(n); 1119170808Sdelphij while (n != n->tn_dir.tn_parent) { 1120197953Sdelphij struct tmpfs_node *parent; 1121197953Sdelphij 1122170808Sdelphij if (n == fnode) { 1123197953Sdelphij TMPFS_NODE_UNLOCK(n); 1124197953Sdelphij TMPFS_UNLOCK(tmp); 1125170808Sdelphij error = EINVAL; 1126170808Sdelphij if (newname != NULL) 1127171087Sdelphij free(newname, M_TMPFSNAME); 1128170808Sdelphij goto out_locked; 1129170808Sdelphij } 1130197953Sdelphij parent = n->tn_dir.tn_parent; 1131197953Sdelphij TMPFS_NODE_UNLOCK(n); 1132197953Sdelphij if (parent == NULL) { 1133197953Sdelphij n = NULL; 1134197953Sdelphij break; 1135197953Sdelphij } 1136197953Sdelphij TMPFS_NODE_LOCK(parent); 1137197953Sdelphij if (parent->tn_dir.tn_parent == NULL) { 1138197953Sdelphij TMPFS_NODE_UNLOCK(parent); 1139197953Sdelphij n = NULL; 1140197953Sdelphij break; 1141197953Sdelphij } 1142197953Sdelphij n = parent; 1143170808Sdelphij } 1144197953Sdelphij TMPFS_UNLOCK(tmp); 1145197953Sdelphij if (n == NULL) { 1146197953Sdelphij error = EINVAL; 1147197953Sdelphij if (newname != NULL) 1148197953Sdelphij free(newname, M_TMPFSNAME); 1149197953Sdelphij goto out_locked; 1150197953Sdelphij } 1151197953Sdelphij TMPFS_NODE_UNLOCK(n); 1152170808Sdelphij 1153170808Sdelphij /* Adjust the parent pointer. */ 1154170808Sdelphij TMPFS_VALIDATE_DIR(fnode); 1155197953Sdelphij TMPFS_NODE_LOCK(de->td_node); 1156170808Sdelphij de->td_node->tn_dir.tn_parent = tdnode; 1157197953Sdelphij TMPFS_NODE_UNLOCK(de->td_node); 1158170808Sdelphij 1159170808Sdelphij /* As a result of changing the target of the '..' 1160170808Sdelphij * entry, the link count of the source and target 1161170808Sdelphij * directories has to be adjusted. */ 1162197953Sdelphij TMPFS_NODE_LOCK(tdnode); 1163197953Sdelphij TMPFS_ASSERT_LOCKED(tdnode); 1164197953Sdelphij tdnode->tn_links++; 1165197953Sdelphij TMPFS_NODE_UNLOCK(tdnode); 1166197953Sdelphij 1167197953Sdelphij TMPFS_NODE_LOCK(fdnode); 1168197953Sdelphij TMPFS_ASSERT_LOCKED(fdnode); 1169170808Sdelphij fdnode->tn_links--; 1170197953Sdelphij TMPFS_NODE_UNLOCK(fdnode); 1171170808Sdelphij } 1172170808Sdelphij } 1173170808Sdelphij 1174245115Sgleb /* Do the move: just remove the entry from the source directory 1175245115Sgleb * and insert it into the target one. */ 1176245115Sgleb tmpfs_dir_detach(fdvp, de); 1177245115Sgleb 1178245115Sgleb if (fcnp->cn_flags & DOWHITEOUT) 1179245115Sgleb tmpfs_dir_whiteout_add(fdvp, fcnp); 1180245115Sgleb if (tcnp->cn_flags & ISWHITEOUT) 1181245115Sgleb tmpfs_dir_whiteout_remove(tdvp, tcnp); 1182245115Sgleb 1183170808Sdelphij /* If the name has changed, we need to make it effective by changing 1184170808Sdelphij * it in the directory entry. */ 1185170808Sdelphij if (newname != NULL) { 1186170808Sdelphij MPASS(tcnp->cn_namelen <= MAXNAMLEN); 1187170808Sdelphij 1188245115Sgleb free(de->ud.td_name, M_TMPFSNAME); 1189245115Sgleb de->ud.td_name = newname; 1190245115Sgleb tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen); 1191170808Sdelphij 1192170808Sdelphij fnode->tn_status |= TMPFS_NODE_CHANGED; 1193170808Sdelphij tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1194170808Sdelphij } 1195170808Sdelphij 1196170808Sdelphij /* If we are overwriting an entry, we have to remove the old one 1197170808Sdelphij * from the target directory. */ 1198170808Sdelphij if (tvp != NULL) { 1199245115Sgleb struct tmpfs_dirent *tde; 1200245115Sgleb 1201170808Sdelphij /* Remove the old entry from the target directory. */ 1202245115Sgleb tde = tmpfs_dir_lookup(tdnode, tnode, tcnp); 1203245115Sgleb tmpfs_dir_detach(tdvp, tde); 1204170808Sdelphij 1205170808Sdelphij /* Free the directory entry we just deleted. Note that the 1206170808Sdelphij * node referred by it will not be removed until the vnode is 1207170808Sdelphij * really reclaimed. */ 1208245115Sgleb tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde); 1209170808Sdelphij } 1210245115Sgleb 1211245115Sgleb tmpfs_dir_attach(tdvp, de); 1212245115Sgleb 1213226987Spho cache_purge(fvp); 1214232401Sjhb if (tvp != NULL) 1215232401Sjhb cache_purge(tvp); 1216248422Skib cache_purge_negative(tdvp); 1217170808Sdelphij 1218170808Sdelphij error = 0; 1219170808Sdelphij 1220170808Sdelphijout_locked: 1221227822Sivoras if (fdvp != tdvp && fdvp != tvp) 1222175294Sattilio VOP_UNLOCK(fdvp, 0); 1223170808Sdelphij 1224170808Sdelphijout: 1225170808Sdelphij /* Release target nodes. */ 1226170808Sdelphij /* XXX: I don't understand when tdvp can be the same as tvp, but 1227170808Sdelphij * other code takes care of this... */ 1228170808Sdelphij if (tdvp == tvp) 1229170808Sdelphij vrele(tdvp); 1230170808Sdelphij else 1231170808Sdelphij vput(tdvp); 1232170808Sdelphij if (tvp != NULL) 1233170808Sdelphij vput(tvp); 1234170808Sdelphij 1235170808Sdelphij /* Release source nodes. */ 1236170808Sdelphij vrele(fdvp); 1237170808Sdelphij vrele(fvp); 1238170808Sdelphij 1239232960Sgleb if (mp != NULL) 1240232960Sgleb vfs_unbusy(mp); 1241232960Sgleb 1242170808Sdelphij return error; 1243170808Sdelphij} 1244170808Sdelphij 1245170808Sdelphij/* --------------------------------------------------------------------- */ 1246170808Sdelphij 1247171069Sdelphijstatic int 1248170808Sdelphijtmpfs_mkdir(struct vop_mkdir_args *v) 1249170808Sdelphij{ 1250170808Sdelphij struct vnode *dvp = v->a_dvp; 1251170808Sdelphij struct vnode **vpp = v->a_vpp; 1252170808Sdelphij struct componentname *cnp = v->a_cnp; 1253170808Sdelphij struct vattr *vap = v->a_vap; 1254170808Sdelphij 1255170808Sdelphij MPASS(vap->va_type == VDIR); 1256170808Sdelphij 1257170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 1258170808Sdelphij} 1259170808Sdelphij 1260170808Sdelphij/* --------------------------------------------------------------------- */ 1261170808Sdelphij 1262171069Sdelphijstatic int 1263170808Sdelphijtmpfs_rmdir(struct vop_rmdir_args *v) 1264170808Sdelphij{ 1265170808Sdelphij struct vnode *dvp = v->a_dvp; 1266170808Sdelphij struct vnode *vp = v->a_vp; 1267170808Sdelphij 1268170808Sdelphij int error; 1269170808Sdelphij struct tmpfs_dirent *de; 1270170808Sdelphij struct tmpfs_mount *tmp; 1271170808Sdelphij struct tmpfs_node *dnode; 1272170808Sdelphij struct tmpfs_node *node; 1273170808Sdelphij 1274176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 1275176559Sattilio MPASS(VOP_ISLOCKED(vp)); 1276170808Sdelphij 1277170808Sdelphij tmp = VFS_TO_TMPFS(dvp->v_mount); 1278170808Sdelphij dnode = VP_TO_TMPFS_DIR(dvp); 1279170808Sdelphij node = VP_TO_TMPFS_DIR(vp); 1280170808Sdelphij 1281171070Sdelphij /* Directories with more than two entries ('.' and '..') cannot be 1282171070Sdelphij * removed. */ 1283171070Sdelphij if (node->tn_size > 0) { 1284171070Sdelphij error = ENOTEMPTY; 1285171070Sdelphij goto out; 1286171070Sdelphij } 1287170808Sdelphij 1288170808Sdelphij if ((dnode->tn_flags & APPEND) 1289170808Sdelphij || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1290170808Sdelphij error = EPERM; 1291170808Sdelphij goto out; 1292170808Sdelphij } 1293170808Sdelphij 1294171070Sdelphij /* This invariant holds only if we are not trying to remove "..". 1295171070Sdelphij * We checked for that above so this is safe now. */ 1296170808Sdelphij MPASS(node->tn_dir.tn_parent == dnode); 1297170808Sdelphij 1298170808Sdelphij /* Get the directory entry associated with node (vp). This was 1299170808Sdelphij * filled by tmpfs_lookup while looking up the entry. */ 1300188318Skib de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 1301170808Sdelphij MPASS(TMPFS_DIRENT_MATCHES(de, 1302170808Sdelphij v->a_cnp->cn_nameptr, 1303170808Sdelphij v->a_cnp->cn_namelen)); 1304170808Sdelphij 1305170808Sdelphij /* Check flags to see if we are allowed to remove the directory. */ 1306170808Sdelphij if (dnode->tn_flags & APPEND 1307170808Sdelphij || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1308170808Sdelphij error = EPERM; 1309170808Sdelphij goto out; 1310170808Sdelphij } 1311170808Sdelphij 1312197953Sdelphij 1313170808Sdelphij /* Detach the directory entry from the directory (dnode). */ 1314170808Sdelphij tmpfs_dir_detach(dvp, de); 1315211598Sed if (v->a_cnp->cn_flags & DOWHITEOUT) 1316211598Sed tmpfs_dir_whiteout_add(dvp, v->a_cnp); 1317170808Sdelphij 1318197953Sdelphij /* No vnode should be allocated for this entry from this point */ 1319197953Sdelphij TMPFS_NODE_LOCK(node); 1320197953Sdelphij TMPFS_ASSERT_ELOCKED(node); 1321170808Sdelphij node->tn_links--; 1322197953Sdelphij node->tn_dir.tn_parent = NULL; 1323170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1324170808Sdelphij TMPFS_NODE_MODIFIED; 1325197953Sdelphij 1326197953Sdelphij TMPFS_NODE_UNLOCK(node); 1327197953Sdelphij 1328197953Sdelphij TMPFS_NODE_LOCK(dnode); 1329197953Sdelphij TMPFS_ASSERT_ELOCKED(dnode); 1330197953Sdelphij dnode->tn_links--; 1331197953Sdelphij dnode->tn_status |= TMPFS_NODE_ACCESSED | \ 1332170808Sdelphij TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1333197953Sdelphij TMPFS_NODE_UNLOCK(dnode); 1334170808Sdelphij 1335171070Sdelphij cache_purge(dvp); 1336170808Sdelphij cache_purge(vp); 1337170808Sdelphij 1338170808Sdelphij /* Free the directory entry we just deleted. Note that the node 1339170808Sdelphij * referred by it will not be removed until the vnode is really 1340170808Sdelphij * reclaimed. */ 1341245115Sgleb tmpfs_free_dirent(tmp, de); 1342170808Sdelphij 1343170808Sdelphij /* Release the deleted vnode (will destroy the node, notify 1344170808Sdelphij * interested parties and clean it from the cache). */ 1345170808Sdelphij 1346170808Sdelphij dnode->tn_status |= TMPFS_NODE_CHANGED; 1347170808Sdelphij tmpfs_update(dvp); 1348170808Sdelphij 1349170808Sdelphij error = 0; 1350170808Sdelphij 1351170808Sdelphijout: 1352170808Sdelphij return error; 1353170808Sdelphij} 1354170808Sdelphij 1355170808Sdelphij/* --------------------------------------------------------------------- */ 1356170808Sdelphij 1357171069Sdelphijstatic int 1358170808Sdelphijtmpfs_symlink(struct vop_symlink_args *v) 1359170808Sdelphij{ 1360170808Sdelphij struct vnode *dvp = v->a_dvp; 1361170808Sdelphij struct vnode **vpp = v->a_vpp; 1362170808Sdelphij struct componentname *cnp = v->a_cnp; 1363170808Sdelphij struct vattr *vap = v->a_vap; 1364170808Sdelphij char *target = v->a_target; 1365170808Sdelphij 1366170808Sdelphij#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */ 1367170808Sdelphij MPASS(vap->va_type == VLNK); 1368170808Sdelphij#else 1369170808Sdelphij vap->va_type = VLNK; 1370170808Sdelphij#endif 1371170808Sdelphij 1372170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1373170808Sdelphij} 1374170808Sdelphij 1375170808Sdelphij/* --------------------------------------------------------------------- */ 1376170808Sdelphij 1377171069Sdelphijstatic int 1378170808Sdelphijtmpfs_readdir(struct vop_readdir_args *v) 1379170808Sdelphij{ 1380170808Sdelphij struct vnode *vp = v->a_vp; 1381170808Sdelphij struct uio *uio = v->a_uio; 1382170808Sdelphij int *eofflag = v->a_eofflag; 1383170808Sdelphij u_long **cookies = v->a_cookies; 1384170808Sdelphij int *ncookies = v->a_ncookies; 1385170808Sdelphij 1386170808Sdelphij int error; 1387245115Sgleb ssize_t startresid; 1388245115Sgleb int cnt = 0; 1389170808Sdelphij struct tmpfs_node *node; 1390170808Sdelphij 1391170808Sdelphij /* This operation only makes sense on directory nodes. */ 1392171802Sdelphij if (vp->v_type != VDIR) 1393171802Sdelphij return ENOTDIR; 1394170808Sdelphij 1395170808Sdelphij node = VP_TO_TMPFS_DIR(vp); 1396170808Sdelphij 1397245115Sgleb startresid = uio->uio_resid; 1398170808Sdelphij 1399245115Sgleb if (cookies != NULL && ncookies != NULL) { 1400245115Sgleb cnt = howmany(node->tn_size, sizeof(struct tmpfs_dirent)) + 2; 1401245115Sgleb *cookies = malloc(cnt * sizeof(**cookies), M_TEMP, M_WAITOK); 1402245115Sgleb *ncookies = 0; 1403171862Sdelphij } 1404171862Sdelphij 1405245115Sgleb if (cnt == 0) 1406245115Sgleb error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL); 1407245115Sgleb else 1408245115Sgleb error = tmpfs_dir_getdents(node, uio, cnt, *cookies, ncookies); 1409170808Sdelphij 1410245115Sgleb if (error == EJUSTRETURN) 1411245115Sgleb error = (uio->uio_resid != startresid) ? 0 : EINVAL; 1412171862Sdelphij 1413245115Sgleb if (error != 0 && cnt != 0) 1414245115Sgleb free(*cookies, M_TEMP); 1415171862Sdelphij 1416170808Sdelphij if (eofflag != NULL) 1417170808Sdelphij *eofflag = 1418170808Sdelphij (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1419170808Sdelphij 1420170808Sdelphij return error; 1421170808Sdelphij} 1422170808Sdelphij 1423170808Sdelphij/* --------------------------------------------------------------------- */ 1424170808Sdelphij 1425171069Sdelphijstatic int 1426170808Sdelphijtmpfs_readlink(struct vop_readlink_args *v) 1427170808Sdelphij{ 1428170808Sdelphij struct vnode *vp = v->a_vp; 1429170808Sdelphij struct uio *uio = v->a_uio; 1430170808Sdelphij 1431170808Sdelphij int error; 1432170808Sdelphij struct tmpfs_node *node; 1433170808Sdelphij 1434170808Sdelphij MPASS(uio->uio_offset == 0); 1435170808Sdelphij MPASS(vp->v_type == VLNK); 1436170808Sdelphij 1437170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1438170808Sdelphij 1439170808Sdelphij error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid), 1440170808Sdelphij uio); 1441170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED; 1442170808Sdelphij 1443170808Sdelphij return error; 1444170808Sdelphij} 1445170808Sdelphij 1446170808Sdelphij/* --------------------------------------------------------------------- */ 1447170808Sdelphij 1448171069Sdelphijstatic int 1449170808Sdelphijtmpfs_inactive(struct vop_inactive_args *v) 1450170808Sdelphij{ 1451170808Sdelphij struct vnode *vp = v->a_vp; 1452170808Sdelphij 1453170808Sdelphij struct tmpfs_node *node; 1454170808Sdelphij 1455170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1456170808Sdelphij 1457170808Sdelphij if (node->tn_links == 0) 1458234607Strasz vrecycle(vp); 1459170808Sdelphij 1460170808Sdelphij return 0; 1461170808Sdelphij} 1462170808Sdelphij 1463170808Sdelphij/* --------------------------------------------------------------------- */ 1464170808Sdelphij 1465170808Sdelphijint 1466170808Sdelphijtmpfs_reclaim(struct vop_reclaim_args *v) 1467170808Sdelphij{ 1468170808Sdelphij struct vnode *vp = v->a_vp; 1469170808Sdelphij 1470170808Sdelphij struct tmpfs_mount *tmp; 1471170808Sdelphij struct tmpfs_node *node; 1472170808Sdelphij 1473170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1474170808Sdelphij tmp = VFS_TO_TMPFS(vp->v_mount); 1475171070Sdelphij 1476250189Skib if (vp->v_type == VREG) 1477250189Skib tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj); 1478250190Skib else 1479250190Skib vnode_destroy_vobject(vp); 1480250030Skib vp->v_object = NULL; 1481170808Sdelphij cache_purge(vp); 1482197953Sdelphij 1483197953Sdelphij TMPFS_NODE_LOCK(node); 1484197953Sdelphij TMPFS_ASSERT_ELOCKED(node); 1485170808Sdelphij tmpfs_free_vp(vp); 1486170808Sdelphij 1487170808Sdelphij /* If the node referenced by this vnode was deleted by the user, 1488170808Sdelphij * we must free its associated data structures (now that the vnode 1489170808Sdelphij * is being reclaimed). */ 1490197953Sdelphij if (node->tn_links == 0 && 1491197953Sdelphij (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) { 1492197953Sdelphij node->tn_vpstate = TMPFS_VNODE_DOOMED; 1493197953Sdelphij TMPFS_NODE_UNLOCK(node); 1494170808Sdelphij tmpfs_free_node(tmp, node); 1495197953Sdelphij } else 1496197953Sdelphij TMPFS_NODE_UNLOCK(node); 1497170808Sdelphij 1498170808Sdelphij MPASS(vp->v_data == NULL); 1499170808Sdelphij return 0; 1500170808Sdelphij} 1501170808Sdelphij 1502170808Sdelphij/* --------------------------------------------------------------------- */ 1503170808Sdelphij 1504171069Sdelphijstatic int 1505170808Sdelphijtmpfs_print(struct vop_print_args *v) 1506170808Sdelphij{ 1507170808Sdelphij struct vnode *vp = v->a_vp; 1508170808Sdelphij 1509170808Sdelphij struct tmpfs_node *node; 1510170808Sdelphij 1511170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1512170808Sdelphij 1513248610Spjd printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n", 1514170808Sdelphij node, node->tn_flags, node->tn_links); 1515231669Stijl printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n", 1516170808Sdelphij node->tn_mode, node->tn_uid, node->tn_gid, 1517231669Stijl (intmax_t)node->tn_size, node->tn_status); 1518170808Sdelphij 1519170808Sdelphij if (vp->v_type == VFIFO) 1520170808Sdelphij fifo_printinfo(vp); 1521170808Sdelphij 1522170808Sdelphij printf("\n"); 1523170808Sdelphij 1524170808Sdelphij return 0; 1525170808Sdelphij} 1526170808Sdelphij 1527170808Sdelphij/* --------------------------------------------------------------------- */ 1528170808Sdelphij 1529171069Sdelphijstatic int 1530170808Sdelphijtmpfs_pathconf(struct vop_pathconf_args *v) 1531170808Sdelphij{ 1532170808Sdelphij int name = v->a_name; 1533170808Sdelphij register_t *retval = v->a_retval; 1534170808Sdelphij 1535170808Sdelphij int error; 1536170808Sdelphij 1537170808Sdelphij error = 0; 1538170808Sdelphij 1539170808Sdelphij switch (name) { 1540170808Sdelphij case _PC_LINK_MAX: 1541170808Sdelphij *retval = LINK_MAX; 1542170808Sdelphij break; 1543170808Sdelphij 1544170808Sdelphij case _PC_NAME_MAX: 1545170808Sdelphij *retval = NAME_MAX; 1546170808Sdelphij break; 1547170808Sdelphij 1548170808Sdelphij case _PC_PATH_MAX: 1549170808Sdelphij *retval = PATH_MAX; 1550170808Sdelphij break; 1551170808Sdelphij 1552170808Sdelphij case _PC_PIPE_BUF: 1553170808Sdelphij *retval = PIPE_BUF; 1554170808Sdelphij break; 1555170808Sdelphij 1556170808Sdelphij case _PC_CHOWN_RESTRICTED: 1557170808Sdelphij *retval = 1; 1558170808Sdelphij break; 1559170808Sdelphij 1560170808Sdelphij case _PC_NO_TRUNC: 1561170808Sdelphij *retval = 1; 1562170808Sdelphij break; 1563170808Sdelphij 1564170808Sdelphij case _PC_SYNC_IO: 1565170808Sdelphij *retval = 1; 1566170808Sdelphij break; 1567170808Sdelphij 1568170808Sdelphij case _PC_FILESIZEBITS: 1569170808Sdelphij *retval = 0; /* XXX Don't know which value should I return. */ 1570170808Sdelphij break; 1571170808Sdelphij 1572170808Sdelphij default: 1573170808Sdelphij error = EINVAL; 1574170808Sdelphij } 1575170808Sdelphij 1576170808Sdelphij return error; 1577170808Sdelphij} 1578170808Sdelphij 1579171069Sdelphijstatic int 1580170808Sdelphijtmpfs_vptofh(struct vop_vptofh_args *ap) 1581170808Sdelphij{ 1582170808Sdelphij struct tmpfs_fid *tfhp; 1583170808Sdelphij struct tmpfs_node *node; 1584170808Sdelphij 1585170808Sdelphij tfhp = (struct tmpfs_fid *)ap->a_fhp; 1586170808Sdelphij node = VP_TO_TMPFS_NODE(ap->a_vp); 1587170808Sdelphij 1588170808Sdelphij tfhp->tf_len = sizeof(struct tmpfs_fid); 1589170808Sdelphij tfhp->tf_id = node->tn_id; 1590170808Sdelphij tfhp->tf_gen = node->tn_gen; 1591171070Sdelphij 1592170808Sdelphij return (0); 1593170808Sdelphij} 1594171069Sdelphij 1595211598Sedstatic int 1596211598Sedtmpfs_whiteout(struct vop_whiteout_args *ap) 1597211598Sed{ 1598211598Sed struct vnode *dvp = ap->a_dvp; 1599211598Sed struct componentname *cnp = ap->a_cnp; 1600211598Sed struct tmpfs_dirent *de; 1601211598Sed 1602211598Sed switch (ap->a_flags) { 1603211598Sed case LOOKUP: 1604211598Sed return (0); 1605211598Sed case CREATE: 1606211598Sed de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1607211598Sed if (de != NULL) 1608211598Sed return (de->td_node == NULL ? 0 : EEXIST); 1609211598Sed return (tmpfs_dir_whiteout_add(dvp, cnp)); 1610211598Sed case DELETE: 1611211598Sed tmpfs_dir_whiteout_remove(dvp, cnp); 1612211598Sed return (0); 1613211598Sed default: 1614211598Sed panic("tmpfs_whiteout: unknown op"); 1615211598Sed } 1616211598Sed} 1617211598Sed 1618171069Sdelphij/* --------------------------------------------------------------------- */ 1619171069Sdelphij 1620171069Sdelphij/* 1621171069Sdelphij * vnode operations vector used for files stored in a tmpfs file system. 1622171069Sdelphij */ 1623171069Sdelphijstruct vop_vector tmpfs_vnodeop_entries = { 1624171069Sdelphij .vop_default = &default_vnodeops, 1625171069Sdelphij .vop_lookup = vfs_cache_lookup, 1626171069Sdelphij .vop_cachedlookup = tmpfs_lookup, 1627171069Sdelphij .vop_create = tmpfs_create, 1628171069Sdelphij .vop_mknod = tmpfs_mknod, 1629171069Sdelphij .vop_open = tmpfs_open, 1630171069Sdelphij .vop_close = tmpfs_close, 1631171069Sdelphij .vop_access = tmpfs_access, 1632171069Sdelphij .vop_getattr = tmpfs_getattr, 1633171069Sdelphij .vop_setattr = tmpfs_setattr, 1634171069Sdelphij .vop_read = tmpfs_read, 1635171069Sdelphij .vop_write = tmpfs_write, 1636171069Sdelphij .vop_fsync = tmpfs_fsync, 1637171069Sdelphij .vop_remove = tmpfs_remove, 1638171069Sdelphij .vop_link = tmpfs_link, 1639171069Sdelphij .vop_rename = tmpfs_rename, 1640171069Sdelphij .vop_mkdir = tmpfs_mkdir, 1641171069Sdelphij .vop_rmdir = tmpfs_rmdir, 1642171069Sdelphij .vop_symlink = tmpfs_symlink, 1643171069Sdelphij .vop_readdir = tmpfs_readdir, 1644171069Sdelphij .vop_readlink = tmpfs_readlink, 1645171069Sdelphij .vop_inactive = tmpfs_inactive, 1646171069Sdelphij .vop_reclaim = tmpfs_reclaim, 1647171069Sdelphij .vop_print = tmpfs_print, 1648171069Sdelphij .vop_pathconf = tmpfs_pathconf, 1649171069Sdelphij .vop_vptofh = tmpfs_vptofh, 1650211598Sed .vop_whiteout = tmpfs_whiteout, 1651171069Sdelphij .vop_bmap = VOP_EOPNOTSUPP, 1652171069Sdelphij}; 1653171069Sdelphij 1654