tmpfs_vnops.c revision 233851
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: stable/9/sys/fs/tmpfs/tmpfs_vnops.c 233851 2012-04-03 19:34:00Z gleb $"); 38170808Sdelphij 39170808Sdelphij#include <sys/param.h> 40170808Sdelphij#include <sys/fcntl.h> 41170808Sdelphij#include <sys/lockf.h> 42170808Sdelphij#include <sys/namei.h> 43170808Sdelphij#include <sys/priv.h> 44170808Sdelphij#include <sys/proc.h> 45197850Sdelphij#include <sys/sched.h> 46197850Sdelphij#include <sys/sf_buf.h> 47170808Sdelphij#include <sys/stat.h> 48170808Sdelphij#include <sys/systm.h> 49233851Sgleb#include <sys/sysctl.h> 50170808Sdelphij#include <sys/unistd.h> 51170808Sdelphij#include <sys/vnode.h> 52170808Sdelphij 53170808Sdelphij#include <vm/vm.h> 54170808Sdelphij#include <vm/vm_object.h> 55170808Sdelphij#include <vm/vm_page.h> 56170808Sdelphij#include <vm/vm_pager.h> 57188929Salc 58170808Sdelphij#include <machine/_inttypes.h> 59170808Sdelphij 60170808Sdelphij#include <fs/fifofs/fifo.h> 61170808Sdelphij#include <fs/tmpfs/tmpfs_vnops.h> 62170808Sdelphij#include <fs/tmpfs/tmpfs.h> 63170808Sdelphij 64233851SglebSYSCTL_DECL(_vfs_tmpfs); 65233851Sgleb 66233851Sglebstatic volatile int tmpfs_rename_restarts; 67233851SglebSYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD, 68233851Sgleb __DEVOLATILE(int *, &tmpfs_rename_restarts), 0, 69233851Sgleb "Times rename had to restart due to lock contention"); 70233851Sgleb 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; 267171070Sdelphij vnode_create_vobject(vp, node->tn_size, v->a_td); 268170808Sdelphij } 269170808Sdelphij 270176559Sattilio MPASS(VOP_ISLOCKED(vp)); 271170808Sdelphij return error; 272170808Sdelphij} 273170808Sdelphij 274170808Sdelphij/* --------------------------------------------------------------------- */ 275170808Sdelphij 276171069Sdelphijstatic int 277170808Sdelphijtmpfs_close(struct vop_close_args *v) 278170808Sdelphij{ 279170808Sdelphij struct vnode *vp = v->a_vp; 280170808Sdelphij 281176559Sattilio MPASS(VOP_ISLOCKED(vp)); 282170808Sdelphij 283218949Salc /* Update node times. */ 284218949Salc tmpfs_update(vp); 285170808Sdelphij 286218949Salc return (0); 287170808Sdelphij} 288170808Sdelphij 289170808Sdelphij/* --------------------------------------------------------------------- */ 290170808Sdelphij 291170808Sdelphijint 292170808Sdelphijtmpfs_access(struct vop_access_args *v) 293170808Sdelphij{ 294170808Sdelphij struct vnode *vp = v->a_vp; 295184413Strasz accmode_t accmode = v->a_accmode; 296170808Sdelphij struct ucred *cred = v->a_cred; 297170808Sdelphij 298170808Sdelphij int error; 299170808Sdelphij struct tmpfs_node *node; 300170808Sdelphij 301176559Sattilio MPASS(VOP_ISLOCKED(vp)); 302170808Sdelphij 303170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 304170808Sdelphij 305170808Sdelphij switch (vp->v_type) { 306170808Sdelphij case VDIR: 307170808Sdelphij /* FALLTHROUGH */ 308170808Sdelphij case VLNK: 309170808Sdelphij /* FALLTHROUGH */ 310170808Sdelphij case VREG: 311184413Strasz if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) { 312170808Sdelphij error = EROFS; 313170808Sdelphij goto out; 314170808Sdelphij } 315170808Sdelphij break; 316170808Sdelphij 317170808Sdelphij case VBLK: 318170808Sdelphij /* FALLTHROUGH */ 319170808Sdelphij case VCHR: 320170808Sdelphij /* FALLTHROUGH */ 321170808Sdelphij case VSOCK: 322170808Sdelphij /* FALLTHROUGH */ 323170808Sdelphij case VFIFO: 324170808Sdelphij break; 325170808Sdelphij 326170808Sdelphij default: 327170808Sdelphij error = EINVAL; 328170808Sdelphij goto out; 329170808Sdelphij } 330170808Sdelphij 331184413Strasz if (accmode & VWRITE && node->tn_flags & IMMUTABLE) { 332170808Sdelphij error = EPERM; 333170808Sdelphij goto out; 334170808Sdelphij } 335170808Sdelphij 336170808Sdelphij error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, 337184413Strasz node->tn_gid, accmode, cred, NULL); 338170808Sdelphij 339170808Sdelphijout: 340176559Sattilio MPASS(VOP_ISLOCKED(vp)); 341170808Sdelphij 342170808Sdelphij return error; 343170808Sdelphij} 344170808Sdelphij 345170808Sdelphij/* --------------------------------------------------------------------- */ 346170808Sdelphij 347170808Sdelphijint 348170808Sdelphijtmpfs_getattr(struct vop_getattr_args *v) 349170808Sdelphij{ 350170808Sdelphij struct vnode *vp = v->a_vp; 351170808Sdelphij struct vattr *vap = v->a_vap; 352170808Sdelphij 353170808Sdelphij struct tmpfs_node *node; 354170808Sdelphij 355170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 356170808Sdelphij 357170808Sdelphij tmpfs_update(vp); 358170808Sdelphij 359170808Sdelphij vap->va_type = vp->v_type; 360170808Sdelphij vap->va_mode = node->tn_mode; 361170808Sdelphij vap->va_nlink = node->tn_links; 362170808Sdelphij vap->va_uid = node->tn_uid; 363170808Sdelphij vap->va_gid = node->tn_gid; 364170808Sdelphij vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 365170808Sdelphij vap->va_fileid = node->tn_id; 366170808Sdelphij vap->va_size = node->tn_size; 367170808Sdelphij vap->va_blocksize = PAGE_SIZE; 368170808Sdelphij vap->va_atime = node->tn_atime; 369170808Sdelphij vap->va_mtime = node->tn_mtime; 370170808Sdelphij vap->va_ctime = node->tn_ctime; 371170808Sdelphij vap->va_birthtime = node->tn_birthtime; 372170808Sdelphij vap->va_gen = node->tn_gen; 373170808Sdelphij vap->va_flags = node->tn_flags; 374170808Sdelphij vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ? 375183214Skib node->tn_rdev : NODEV; 376170808Sdelphij vap->va_bytes = round_page(node->tn_size); 377183212Skib vap->va_filerev = 0; 378170808Sdelphij 379170808Sdelphij return 0; 380170808Sdelphij} 381170808Sdelphij 382170808Sdelphij/* --------------------------------------------------------------------- */ 383170808Sdelphij 384170808Sdelphij/* XXX Should this operation be atomic? I think it should, but code in 385170808Sdelphij * XXX other places (e.g., ufs) doesn't seem to be... */ 386170808Sdelphijint 387170808Sdelphijtmpfs_setattr(struct vop_setattr_args *v) 388170808Sdelphij{ 389170808Sdelphij struct vnode *vp = v->a_vp; 390170808Sdelphij struct vattr *vap = v->a_vap; 391170808Sdelphij struct ucred *cred = v->a_cred; 392182371Sattilio struct thread *td = curthread; 393170808Sdelphij 394170808Sdelphij int error; 395170808Sdelphij 396176559Sattilio MPASS(VOP_ISLOCKED(vp)); 397170808Sdelphij 398170808Sdelphij error = 0; 399170808Sdelphij 400170808Sdelphij /* Abort if any unsettable attribute is given. */ 401170808Sdelphij if (vap->va_type != VNON || 402170808Sdelphij vap->va_nlink != VNOVAL || 403170808Sdelphij vap->va_fsid != VNOVAL || 404170808Sdelphij vap->va_fileid != VNOVAL || 405170808Sdelphij vap->va_blocksize != VNOVAL || 406170808Sdelphij vap->va_gen != VNOVAL || 407170808Sdelphij vap->va_rdev != VNOVAL || 408170808Sdelphij vap->va_bytes != VNOVAL) 409170808Sdelphij error = EINVAL; 410170808Sdelphij 411170808Sdelphij if (error == 0 && (vap->va_flags != VNOVAL)) 412182371Sattilio error = tmpfs_chflags(vp, vap->va_flags, cred, td); 413170808Sdelphij 414170808Sdelphij if (error == 0 && (vap->va_size != VNOVAL)) 415182371Sattilio error = tmpfs_chsize(vp, vap->va_size, cred, td); 416170808Sdelphij 417170808Sdelphij if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) 418182371Sattilio error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td); 419170808Sdelphij 420170808Sdelphij if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) 421182371Sattilio error = tmpfs_chmod(vp, vap->va_mode, cred, td); 422170808Sdelphij 423170808Sdelphij if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL && 424170808Sdelphij vap->va_atime.tv_nsec != VNOVAL) || 425170808Sdelphij (vap->va_mtime.tv_sec != VNOVAL && 426170808Sdelphij vap->va_mtime.tv_nsec != VNOVAL) || 427170808Sdelphij (vap->va_birthtime.tv_sec != VNOVAL && 428170808Sdelphij vap->va_birthtime.tv_nsec != VNOVAL))) 429171070Sdelphij error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime, 430182371Sattilio &vap->va_birthtime, vap->va_vaflags, cred, td); 431170808Sdelphij 432170808Sdelphij /* Update the node times. We give preference to the error codes 433170808Sdelphij * generated by this function rather than the ones that may arise 434170808Sdelphij * from tmpfs_update. */ 435170808Sdelphij tmpfs_update(vp); 436170808Sdelphij 437176559Sattilio MPASS(VOP_ISLOCKED(vp)); 438170808Sdelphij 439170808Sdelphij return error; 440170808Sdelphij} 441170808Sdelphij 442170808Sdelphij/* --------------------------------------------------------------------- */ 443197850Sdelphijstatic int 444197850Sdelphijtmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx, 445197850Sdelphij vm_offset_t offset, size_t tlen, struct uio *uio) 446197850Sdelphij{ 447197850Sdelphij vm_page_t m; 448197850Sdelphij int error; 449171489Sdelphij 450197850Sdelphij VM_OBJECT_LOCK(tobj); 451197850Sdelphij vm_object_pip_add(tobj, 1); 452197850Sdelphij m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 453231775Salc VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 454197850Sdelphij if (m->valid != VM_PAGE_BITS_ALL) { 455197850Sdelphij if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 456197850Sdelphij error = vm_pager_get_pages(tobj, &m, 1, 0); 457197850Sdelphij if (error != 0) { 458197850Sdelphij printf("tmpfs get pages from pager error [read]\n"); 459197850Sdelphij goto out; 460197850Sdelphij } 461197850Sdelphij } else 462197850Sdelphij vm_page_zero_invalid(m, TRUE); 463197850Sdelphij } 464197850Sdelphij VM_OBJECT_UNLOCK(tobj); 465197850Sdelphij error = uiomove_fromphys(&m, offset, tlen, uio); 466197850Sdelphij VM_OBJECT_LOCK(tobj); 467197850Sdelphijout: 468207573Salc vm_page_lock(m); 469197850Sdelphij vm_page_unwire(m, TRUE); 470207573Salc vm_page_unlock(m); 471197850Sdelphij vm_page_wakeup(m); 472197850Sdelphij vm_object_pip_subtract(tobj, 1); 473197850Sdelphij VM_OBJECT_UNLOCK(tobj); 474197850Sdelphij 475197850Sdelphij return (error); 476197850Sdelphij} 477197850Sdelphij 478197850Sdelphijstatic __inline int 479197850Sdelphijtmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx, 480197850Sdelphij vm_offset_t offset, size_t tlen, void *buf) 481197850Sdelphij{ 482197850Sdelphij struct uio uio; 483197850Sdelphij struct iovec iov; 484197850Sdelphij 485197850Sdelphij uio.uio_iovcnt = 1; 486197850Sdelphij uio.uio_iov = &iov; 487197850Sdelphij iov.iov_base = buf; 488197850Sdelphij iov.iov_len = tlen; 489197850Sdelphij 490197850Sdelphij uio.uio_offset = 0; 491197850Sdelphij uio.uio_resid = tlen; 492197850Sdelphij uio.uio_rw = UIO_READ; 493197850Sdelphij uio.uio_segflg = UIO_SYSSPACE; 494197850Sdelphij uio.uio_td = curthread; 495197850Sdelphij 496197850Sdelphij return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio)); 497197850Sdelphij} 498197850Sdelphij 499170808Sdelphijstatic int 500171489Sdelphijtmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio) 501170808Sdelphij{ 502197850Sdelphij struct sf_buf *sf; 503171489Sdelphij vm_pindex_t idx; 504171489Sdelphij vm_page_t m; 505188929Salc vm_offset_t offset; 506188929Salc off_t addr; 507171489Sdelphij size_t tlen; 508197850Sdelphij char *ma; 509171489Sdelphij int error; 510170808Sdelphij 511171489Sdelphij addr = uio->uio_offset; 512171489Sdelphij idx = OFF_TO_IDX(addr); 513171489Sdelphij offset = addr & PAGE_MASK; 514171489Sdelphij tlen = MIN(PAGE_SIZE - offset, len); 515170808Sdelphij 516197740Sdelphij if ((vobj == NULL) || 517197740Sdelphij (vobj->resident_page_count == 0 && vobj->cache == NULL)) 518171489Sdelphij goto nocache; 519170808Sdelphij 520171489Sdelphij VM_OBJECT_LOCK(vobj); 521171489Sdelphijlookupvpg: 522171489Sdelphij if (((m = vm_page_lookup(vobj, idx)) != NULL) && 523171489Sdelphij vm_page_is_valid(m, offset, tlen)) { 524207530Salc if ((m->oflags & VPO_BUSY) != 0) { 525207530Salc /* 526207530Salc * Reference the page before unlocking and sleeping so 527207530Salc * that the page daemon is less likely to reclaim it. 528207530Salc */ 529225418Skib vm_page_reference(m); 530207530Salc vm_page_sleep(m, "tmfsmr"); 531171489Sdelphij goto lookupvpg; 532207530Salc } 533171489Sdelphij vm_page_busy(m); 534171489Sdelphij VM_OBJECT_UNLOCK(vobj); 535188929Salc error = uiomove_fromphys(&m, offset, tlen, uio); 536171489Sdelphij VM_OBJECT_LOCK(vobj); 537171489Sdelphij vm_page_wakeup(m); 538171489Sdelphij VM_OBJECT_UNLOCK(vobj); 539171489Sdelphij return (error); 540197850Sdelphij } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) { 541213735Savg KASSERT(offset == 0, 542213735Savg ("unexpected offset in tmpfs_mappedread for sendfile")); 543207530Salc if ((m->oflags & VPO_BUSY) != 0) { 544207530Salc /* 545207530Salc * Reference the page before unlocking and sleeping so 546207530Salc * that the page daemon is less likely to reclaim it. 547207530Salc */ 548225418Skib vm_page_reference(m); 549207530Salc vm_page_sleep(m, "tmfsmr"); 550197850Sdelphij goto lookupvpg; 551207530Salc } 552197850Sdelphij vm_page_busy(m); 553197850Sdelphij VM_OBJECT_UNLOCK(vobj); 554197850Sdelphij sched_pin(); 555197850Sdelphij sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 556197850Sdelphij ma = (char *)sf_buf_kva(sf); 557213735Savg error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma); 558197850Sdelphij if (error == 0) { 559213735Savg if (tlen != PAGE_SIZE) 560213735Savg bzero(ma + tlen, PAGE_SIZE - tlen); 561197850Sdelphij uio->uio_offset += tlen; 562197850Sdelphij uio->uio_resid -= tlen; 563197850Sdelphij } 564197850Sdelphij sf_buf_free(sf); 565197850Sdelphij sched_unpin(); 566197850Sdelphij VM_OBJECT_LOCK(vobj); 567212650Savg if (error == 0) 568213735Savg m->valid = VM_PAGE_BITS_ALL; 569197850Sdelphij vm_page_wakeup(m); 570197850Sdelphij VM_OBJECT_UNLOCK(vobj); 571197850Sdelphij return (error); 572171799Sdelphij } 573171489Sdelphij VM_OBJECT_UNLOCK(vobj); 574171489Sdelphijnocache: 575197850Sdelphij error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio); 576171489Sdelphij 577171489Sdelphij return (error); 578170808Sdelphij} 579170808Sdelphij 580171069Sdelphijstatic int 581170808Sdelphijtmpfs_read(struct vop_read_args *v) 582170808Sdelphij{ 583170808Sdelphij struct vnode *vp = v->a_vp; 584170808Sdelphij struct uio *uio = v->a_uio; 585170808Sdelphij 586170808Sdelphij struct tmpfs_node *node; 587170808Sdelphij vm_object_t uobj; 588171489Sdelphij size_t len; 589171489Sdelphij int resid; 590170808Sdelphij 591174265Swkoszek int error = 0; 592170808Sdelphij 593170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 594170808Sdelphij 595170808Sdelphij if (vp->v_type != VREG) { 596170808Sdelphij error = EISDIR; 597170808Sdelphij goto out; 598170808Sdelphij } 599170808Sdelphij 600170808Sdelphij if (uio->uio_offset < 0) { 601170808Sdelphij error = EINVAL; 602170808Sdelphij goto out; 603170808Sdelphij } 604170808Sdelphij 605170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED; 606170808Sdelphij 607170808Sdelphij uobj = node->tn_reg.tn_aobj; 608171489Sdelphij while ((resid = uio->uio_resid) > 0) { 609171489Sdelphij error = 0; 610171489Sdelphij if (node->tn_size <= uio->uio_offset) 611171489Sdelphij break; 612171489Sdelphij len = MIN(node->tn_size - uio->uio_offset, resid); 613171489Sdelphij if (len == 0) 614171489Sdelphij break; 615171489Sdelphij error = tmpfs_mappedread(vp->v_object, uobj, len, uio); 616171489Sdelphij if ((error != 0) || (resid == uio->uio_resid)) 617171489Sdelphij break; 618171489Sdelphij } 619170808Sdelphij 620170808Sdelphijout: 621170808Sdelphij 622170808Sdelphij return error; 623170808Sdelphij} 624170808Sdelphij 625170808Sdelphij/* --------------------------------------------------------------------- */ 626170808Sdelphij 627171069Sdelphijstatic int 628171489Sdelphijtmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio) 629171489Sdelphij{ 630171489Sdelphij vm_pindex_t idx; 631171489Sdelphij vm_page_t vpg, tpg; 632188929Salc vm_offset_t offset; 633188929Salc off_t addr; 634171489Sdelphij size_t tlen; 635171489Sdelphij int error; 636171489Sdelphij 637174265Swkoszek error = 0; 638174265Swkoszek 639171489Sdelphij addr = uio->uio_offset; 640171489Sdelphij idx = OFF_TO_IDX(addr); 641171489Sdelphij offset = addr & PAGE_MASK; 642171489Sdelphij tlen = MIN(PAGE_SIZE - offset, len); 643171489Sdelphij 644197740Sdelphij if ((vobj == NULL) || 645197740Sdelphij (vobj->resident_page_count == 0 && vobj->cache == NULL)) { 646171489Sdelphij vpg = NULL; 647171489Sdelphij goto nocache; 648171489Sdelphij } 649171489Sdelphij 650171489Sdelphij VM_OBJECT_LOCK(vobj); 651171489Sdelphijlookupvpg: 652171489Sdelphij if (((vpg = vm_page_lookup(vobj, idx)) != NULL) && 653171489Sdelphij vm_page_is_valid(vpg, offset, tlen)) { 654207530Salc if ((vpg->oflags & VPO_BUSY) != 0) { 655207530Salc /* 656207530Salc * Reference the page before unlocking and sleeping so 657207530Salc * that the page daemon is less likely to reclaim it. 658207530Salc */ 659225418Skib vm_page_reference(vpg); 660207530Salc vm_page_sleep(vpg, "tmfsmw"); 661171489Sdelphij goto lookupvpg; 662207530Salc } 663171489Sdelphij vm_page_busy(vpg); 664171489Sdelphij vm_page_undirty(vpg); 665171489Sdelphij VM_OBJECT_UNLOCK(vobj); 666188929Salc error = uiomove_fromphys(&vpg, offset, tlen, uio); 667171489Sdelphij } else { 668197740Sdelphij if (__predict_false(vobj->cache != NULL)) 669197740Sdelphij vm_page_cache_free(vobj, idx, idx + 1); 670171489Sdelphij VM_OBJECT_UNLOCK(vobj); 671171489Sdelphij vpg = NULL; 672171489Sdelphij } 673171489Sdelphijnocache: 674171489Sdelphij VM_OBJECT_LOCK(tobj); 675171489Sdelphij vm_object_pip_add(tobj, 1); 676171489Sdelphij tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 677231775Salc VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 678171489Sdelphij if (tpg->valid != VM_PAGE_BITS_ALL) { 679194124Salc if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 680171489Sdelphij error = vm_pager_get_pages(tobj, &tpg, 1, 0); 681171489Sdelphij if (error != 0) { 682171489Sdelphij printf("tmpfs get pages from pager error [write]\n"); 683171489Sdelphij goto out; 684171489Sdelphij } 685171489Sdelphij } else 686171489Sdelphij vm_page_zero_invalid(tpg, TRUE); 687171489Sdelphij } 688171489Sdelphij VM_OBJECT_UNLOCK(tobj); 689188929Salc if (vpg == NULL) 690188929Salc error = uiomove_fromphys(&tpg, offset, tlen, uio); 691188929Salc else { 692171489Sdelphij KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid")); 693171489Sdelphij pmap_copy_page(vpg, tpg); 694171489Sdelphij } 695171489Sdelphij VM_OBJECT_LOCK(tobj); 696171489Sdelphijout: 697171489Sdelphij if (vobj != NULL) 698171489Sdelphij VM_OBJECT_LOCK(vobj); 699171489Sdelphij if (error == 0) { 700192917Salc KASSERT(tpg->valid == VM_PAGE_BITS_ALL, 701192917Salc ("parts of tpg invalid")); 702171489Sdelphij vm_page_dirty(tpg); 703171489Sdelphij } 704209226Salc vm_page_lock(tpg); 705188921Salc vm_page_unwire(tpg, TRUE); 706207573Salc vm_page_unlock(tpg); 707171489Sdelphij vm_page_wakeup(tpg); 708171489Sdelphij if (vpg != NULL) 709171489Sdelphij vm_page_wakeup(vpg); 710171489Sdelphij if (vobj != NULL) 711171489Sdelphij VM_OBJECT_UNLOCK(vobj); 712171489Sdelphij vm_object_pip_subtract(tobj, 1); 713171489Sdelphij VM_OBJECT_UNLOCK(tobj); 714171489Sdelphij 715171489Sdelphij return (error); 716171489Sdelphij} 717171489Sdelphij 718171489Sdelphijstatic int 719170808Sdelphijtmpfs_write(struct vop_write_args *v) 720170808Sdelphij{ 721170808Sdelphij struct vnode *vp = v->a_vp; 722170808Sdelphij struct uio *uio = v->a_uio; 723170808Sdelphij int ioflag = v->a_ioflag; 724170808Sdelphij 725170808Sdelphij boolean_t extended; 726171489Sdelphij int error = 0; 727170808Sdelphij off_t oldsize; 728170808Sdelphij struct tmpfs_node *node; 729170808Sdelphij vm_object_t uobj; 730171489Sdelphij size_t len; 731171489Sdelphij int resid; 732170808Sdelphij 733170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 734170808Sdelphij oldsize = node->tn_size; 735170808Sdelphij 736170808Sdelphij if (uio->uio_offset < 0 || vp->v_type != VREG) { 737170808Sdelphij error = EINVAL; 738170808Sdelphij goto out; 739170808Sdelphij } 740170808Sdelphij 741170808Sdelphij if (uio->uio_resid == 0) { 742170808Sdelphij error = 0; 743170808Sdelphij goto out; 744170808Sdelphij } 745170808Sdelphij 746170808Sdelphij if (ioflag & IO_APPEND) 747170808Sdelphij uio->uio_offset = node->tn_size; 748171070Sdelphij 749171070Sdelphij if (uio->uio_offset + uio->uio_resid > 750170808Sdelphij VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 751170808Sdelphij return (EFBIG); 752170808Sdelphij 753207719Strasz if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 754207662Strasz return (EFBIG); 755170808Sdelphij 756170808Sdelphij extended = uio->uio_offset + uio->uio_resid > node->tn_size; 757170808Sdelphij if (extended) { 758170808Sdelphij error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid); 759170808Sdelphij if (error != 0) 760170808Sdelphij goto out; 761170808Sdelphij } 762170808Sdelphij 763170808Sdelphij uobj = node->tn_reg.tn_aobj; 764171489Sdelphij while ((resid = uio->uio_resid) > 0) { 765171489Sdelphij if (node->tn_size <= uio->uio_offset) 766171489Sdelphij break; 767171489Sdelphij len = MIN(node->tn_size - uio->uio_offset, resid); 768171489Sdelphij if (len == 0) 769171489Sdelphij break; 770171489Sdelphij error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio); 771171489Sdelphij if ((error != 0) || (resid == uio->uio_resid)) 772171489Sdelphij break; 773171489Sdelphij } 774170808Sdelphij 775170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 776170808Sdelphij (extended ? TMPFS_NODE_CHANGED : 0); 777170808Sdelphij 778170808Sdelphij if (node->tn_mode & (S_ISUID | S_ISGID)) { 779170808Sdelphij if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0)) 780170808Sdelphij node->tn_mode &= ~(S_ISUID | S_ISGID); 781170808Sdelphij } 782170808Sdelphij 783170808Sdelphij if (error != 0) 784170808Sdelphij (void)tmpfs_reg_resize(vp, oldsize); 785170808Sdelphij 786170808Sdelphijout: 787170808Sdelphij MPASS(IMPLIES(error == 0, uio->uio_resid == 0)); 788170808Sdelphij MPASS(IMPLIES(error != 0, oldsize == node->tn_size)); 789170808Sdelphij 790170808Sdelphij return error; 791170808Sdelphij} 792170808Sdelphij 793170808Sdelphij/* --------------------------------------------------------------------- */ 794170808Sdelphij 795171069Sdelphijstatic int 796170808Sdelphijtmpfs_fsync(struct vop_fsync_args *v) 797170808Sdelphij{ 798170808Sdelphij struct vnode *vp = v->a_vp; 799170808Sdelphij 800176559Sattilio MPASS(VOP_ISLOCKED(vp)); 801170808Sdelphij 802170808Sdelphij tmpfs_update(vp); 803170808Sdelphij 804170808Sdelphij return 0; 805170808Sdelphij} 806170808Sdelphij 807170808Sdelphij/* --------------------------------------------------------------------- */ 808170808Sdelphij 809171069Sdelphijstatic int 810170808Sdelphijtmpfs_remove(struct vop_remove_args *v) 811170808Sdelphij{ 812170808Sdelphij struct vnode *dvp = v->a_dvp; 813170808Sdelphij struct vnode *vp = v->a_vp; 814170808Sdelphij 815170808Sdelphij int error; 816170808Sdelphij struct tmpfs_dirent *de; 817170808Sdelphij struct tmpfs_mount *tmp; 818170808Sdelphij struct tmpfs_node *dnode; 819170808Sdelphij struct tmpfs_node *node; 820170808Sdelphij 821176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 822176559Sattilio MPASS(VOP_ISLOCKED(vp)); 823170808Sdelphij 824170808Sdelphij if (vp->v_type == VDIR) { 825170808Sdelphij error = EISDIR; 826170808Sdelphij goto out; 827170808Sdelphij } 828170808Sdelphij 829170808Sdelphij dnode = VP_TO_TMPFS_DIR(dvp); 830170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 831170808Sdelphij tmp = VFS_TO_TMPFS(vp->v_mount); 832188318Skib de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 833170808Sdelphij MPASS(de != NULL); 834170808Sdelphij 835170808Sdelphij /* Files marked as immutable or append-only cannot be deleted. */ 836170808Sdelphij if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 837170808Sdelphij (dnode->tn_flags & APPEND)) { 838170808Sdelphij error = EPERM; 839170808Sdelphij goto out; 840170808Sdelphij } 841170808Sdelphij 842170808Sdelphij /* Remove the entry from the directory; as it is a file, we do not 843170808Sdelphij * have to change the number of hard links of the directory. */ 844170808Sdelphij tmpfs_dir_detach(dvp, de); 845211598Sed if (v->a_cnp->cn_flags & DOWHITEOUT) 846211598Sed tmpfs_dir_whiteout_add(dvp, v->a_cnp); 847170808Sdelphij 848170808Sdelphij /* Free the directory entry we just deleted. Note that the node 849170808Sdelphij * referred by it will not be removed until the vnode is really 850170808Sdelphij * reclaimed. */ 851170808Sdelphij tmpfs_free_dirent(tmp, de, TRUE); 852170808Sdelphij 853218949Salc node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED; 854170808Sdelphij error = 0; 855170808Sdelphij 856170808Sdelphijout: 857170808Sdelphij 858170808Sdelphij return error; 859170808Sdelphij} 860170808Sdelphij 861170808Sdelphij/* --------------------------------------------------------------------- */ 862170808Sdelphij 863171069Sdelphijstatic int 864170808Sdelphijtmpfs_link(struct vop_link_args *v) 865170808Sdelphij{ 866170808Sdelphij struct vnode *dvp = v->a_tdvp; 867170808Sdelphij struct vnode *vp = v->a_vp; 868170808Sdelphij struct componentname *cnp = v->a_cnp; 869170808Sdelphij 870170808Sdelphij int error; 871170808Sdelphij struct tmpfs_dirent *de; 872170808Sdelphij struct tmpfs_node *node; 873170808Sdelphij 874176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 875170808Sdelphij MPASS(cnp->cn_flags & HASBUF); 876170808Sdelphij MPASS(dvp != vp); /* XXX When can this be false? */ 877170808Sdelphij 878170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 879170808Sdelphij 880170808Sdelphij /* XXX: Why aren't the following two tests done by the caller? */ 881170808Sdelphij 882170808Sdelphij /* Hard links of directories are forbidden. */ 883170808Sdelphij if (vp->v_type == VDIR) { 884170808Sdelphij error = EPERM; 885170808Sdelphij goto out; 886170808Sdelphij } 887170808Sdelphij 888170808Sdelphij /* Cannot create cross-device links. */ 889170808Sdelphij if (dvp->v_mount != vp->v_mount) { 890170808Sdelphij error = EXDEV; 891170808Sdelphij goto out; 892170808Sdelphij } 893170808Sdelphij 894170808Sdelphij /* Ensure that we do not overflow the maximum number of links imposed 895170808Sdelphij * by the system. */ 896170808Sdelphij MPASS(node->tn_links <= LINK_MAX); 897170808Sdelphij if (node->tn_links == LINK_MAX) { 898170808Sdelphij error = EMLINK; 899170808Sdelphij goto out; 900170808Sdelphij } 901170808Sdelphij 902170808Sdelphij /* We cannot create links of files marked immutable or append-only. */ 903170808Sdelphij if (node->tn_flags & (IMMUTABLE | APPEND)) { 904170808Sdelphij error = EPERM; 905170808Sdelphij goto out; 906170808Sdelphij } 907170808Sdelphij 908170808Sdelphij /* Allocate a new directory entry to represent the node. */ 909170808Sdelphij error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 910170808Sdelphij cnp->cn_nameptr, cnp->cn_namelen, &de); 911170808Sdelphij if (error != 0) 912170808Sdelphij goto out; 913170808Sdelphij 914170808Sdelphij /* Insert the new directory entry into the appropriate directory. */ 915211598Sed if (cnp->cn_flags & ISWHITEOUT) 916211598Sed tmpfs_dir_whiteout_remove(dvp, cnp); 917170808Sdelphij tmpfs_dir_attach(dvp, de); 918170808Sdelphij 919170808Sdelphij /* vp link count has changed, so update node times. */ 920170808Sdelphij node->tn_status |= TMPFS_NODE_CHANGED; 921170808Sdelphij tmpfs_update(vp); 922170808Sdelphij 923170808Sdelphij error = 0; 924171070Sdelphij 925170808Sdelphijout: 926170808Sdelphij return error; 927170808Sdelphij} 928170808Sdelphij 929170808Sdelphij/* --------------------------------------------------------------------- */ 930170808Sdelphij 931233851Sgleb/* 932233851Sgleb * We acquire all but fdvp locks using non-blocking acquisitions. If we 933233851Sgleb * fail to acquire any lock in the path we will drop all held locks, 934233851Sgleb * acquire the new lock in a blocking fashion, and then release it and 935233851Sgleb * restart the rename. This acquire/release step ensures that we do not 936233851Sgleb * spin on a lock waiting for release. On error release all vnode locks 937233851Sgleb * and decrement references the way tmpfs_rename() would do. 938233851Sgleb */ 939171069Sdelphijstatic int 940233851Sglebtmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp, 941233851Sgleb struct vnode *tdvp, struct vnode **tvpp, 942233851Sgleb struct componentname *fcnp, struct componentname *tcnp) 943233851Sgleb{ 944233851Sgleb struct vnode *nvp; 945233851Sgleb struct mount *mp; 946233851Sgleb struct tmpfs_dirent *de; 947233851Sgleb int error, restarts = 0; 948233851Sgleb 949233851Sgleb VOP_UNLOCK(tdvp, 0); 950233851Sgleb if (*tvpp != NULL && *tvpp != tdvp) 951233851Sgleb VOP_UNLOCK(*tvpp, 0); 952233851Sgleb mp = fdvp->v_mount; 953233851Sgleb 954233851Sglebrelock: 955233851Sgleb restarts += 1; 956233851Sgleb error = vn_lock(fdvp, LK_EXCLUSIVE); 957233851Sgleb if (error) 958233851Sgleb goto releout; 959233851Sgleb if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 960233851Sgleb VOP_UNLOCK(fdvp, 0); 961233851Sgleb error = vn_lock(tdvp, LK_EXCLUSIVE); 962233851Sgleb if (error) 963233851Sgleb goto releout; 964233851Sgleb VOP_UNLOCK(tdvp, 0); 965233851Sgleb goto relock; 966233851Sgleb } 967233851Sgleb /* 968233851Sgleb * Re-resolve fvp to be certain it still exists and fetch the 969233851Sgleb * correct vnode. 970233851Sgleb */ 971233851Sgleb de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp); 972233851Sgleb if (de == NULL) { 973233851Sgleb VOP_UNLOCK(fdvp, 0); 974233851Sgleb VOP_UNLOCK(tdvp, 0); 975233851Sgleb if ((fcnp->cn_flags & ISDOTDOT) != 0 || 976233851Sgleb (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 977233851Sgleb error = EINVAL; 978233851Sgleb else 979233851Sgleb error = ENOENT; 980233851Sgleb goto releout; 981233851Sgleb } 982233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp); 983233851Sgleb if (error != 0) { 984233851Sgleb VOP_UNLOCK(fdvp, 0); 985233851Sgleb VOP_UNLOCK(tdvp, 0); 986233851Sgleb if (error != EBUSY) 987233851Sgleb goto releout; 988233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp); 989233851Sgleb if (error != 0) 990233851Sgleb goto releout; 991233851Sgleb VOP_UNLOCK(nvp, 0); 992233851Sgleb /* 993233851Sgleb * Concurrent rename race. 994233851Sgleb */ 995233851Sgleb if (nvp == tdvp) { 996233851Sgleb vrele(nvp); 997233851Sgleb error = EINVAL; 998233851Sgleb goto releout; 999233851Sgleb } 1000233851Sgleb vrele(*fvpp); 1001233851Sgleb *fvpp = nvp; 1002233851Sgleb goto relock; 1003233851Sgleb } 1004233851Sgleb vrele(*fvpp); 1005233851Sgleb *fvpp = nvp; 1006233851Sgleb VOP_UNLOCK(*fvpp, 0); 1007233851Sgleb /* 1008233851Sgleb * Re-resolve tvp and acquire the vnode lock if present. 1009233851Sgleb */ 1010233851Sgleb de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp); 1011233851Sgleb /* 1012233851Sgleb * If tvp disappeared we just carry on. 1013233851Sgleb */ 1014233851Sgleb if (de == NULL && *tvpp != NULL) { 1015233851Sgleb vrele(*tvpp); 1016233851Sgleb *tvpp = NULL; 1017233851Sgleb } 1018233851Sgleb /* 1019233851Sgleb * Get the tvp ino if the lookup succeeded. We may have to restart 1020233851Sgleb * if the non-blocking acquire fails. 1021233851Sgleb */ 1022233851Sgleb if (de != NULL) { 1023233851Sgleb nvp = NULL; 1024233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, 1025233851Sgleb LK_EXCLUSIVE | LK_NOWAIT, &nvp); 1026233851Sgleb if (*tvpp != NULL) 1027233851Sgleb vrele(*tvpp); 1028233851Sgleb *tvpp = nvp; 1029233851Sgleb if (error != 0) { 1030233851Sgleb VOP_UNLOCK(fdvp, 0); 1031233851Sgleb VOP_UNLOCK(tdvp, 0); 1032233851Sgleb if (error != EBUSY) 1033233851Sgleb goto releout; 1034233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, 1035233851Sgleb &nvp); 1036233851Sgleb if (error != 0) 1037233851Sgleb goto releout; 1038233851Sgleb VOP_UNLOCK(nvp, 0); 1039233851Sgleb /* 1040233851Sgleb * fdvp contains fvp, thus tvp (=fdvp) is not empty. 1041233851Sgleb */ 1042233851Sgleb if (nvp == fdvp) { 1043233851Sgleb error = ENOTEMPTY; 1044233851Sgleb goto releout; 1045233851Sgleb } 1046233851Sgleb goto relock; 1047233851Sgleb } 1048233851Sgleb } 1049233851Sgleb tmpfs_rename_restarts += restarts; 1050233851Sgleb 1051233851Sgleb return (0); 1052233851Sgleb 1053233851Sglebreleout: 1054233851Sgleb vrele(fdvp); 1055233851Sgleb vrele(*fvpp); 1056233851Sgleb vrele(tdvp); 1057233851Sgleb if (*tvpp != NULL) 1058233851Sgleb vrele(*tvpp); 1059233851Sgleb tmpfs_rename_restarts += restarts; 1060233851Sgleb 1061233851Sgleb return (error); 1062233851Sgleb} 1063233851Sgleb 1064233851Sglebstatic int 1065170808Sdelphijtmpfs_rename(struct vop_rename_args *v) 1066170808Sdelphij{ 1067170808Sdelphij struct vnode *fdvp = v->a_fdvp; 1068170808Sdelphij struct vnode *fvp = v->a_fvp; 1069170808Sdelphij struct componentname *fcnp = v->a_fcnp; 1070170808Sdelphij struct vnode *tdvp = v->a_tdvp; 1071170808Sdelphij struct vnode *tvp = v->a_tvp; 1072170808Sdelphij struct componentname *tcnp = v->a_tcnp; 1073233851Sgleb struct mount *mp = NULL; 1074170808Sdelphij 1075170808Sdelphij char *newname; 1076170808Sdelphij int error; 1077170808Sdelphij struct tmpfs_dirent *de; 1078197953Sdelphij struct tmpfs_mount *tmp; 1079170808Sdelphij struct tmpfs_node *fdnode; 1080170808Sdelphij struct tmpfs_node *fnode; 1081171799Sdelphij struct tmpfs_node *tnode; 1082170808Sdelphij struct tmpfs_node *tdnode; 1083170808Sdelphij 1084176559Sattilio MPASS(VOP_ISLOCKED(tdvp)); 1085176559Sattilio MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp))); 1086170808Sdelphij MPASS(fcnp->cn_flags & HASBUF); 1087170808Sdelphij MPASS(tcnp->cn_flags & HASBUF); 1088170808Sdelphij 1089170808Sdelphij /* Disallow cross-device renames. 1090170808Sdelphij * XXX Why isn't this done by the caller? */ 1091170808Sdelphij if (fvp->v_mount != tdvp->v_mount || 1092170808Sdelphij (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 1093170808Sdelphij error = EXDEV; 1094170808Sdelphij goto out; 1095170808Sdelphij } 1096170808Sdelphij 1097170808Sdelphij /* If source and target are the same file, there is nothing to do. */ 1098170808Sdelphij if (fvp == tvp) { 1099170808Sdelphij error = 0; 1100170808Sdelphij goto out; 1101170808Sdelphij } 1102170808Sdelphij 1103173725Sdelphij /* If we need to move the directory between entries, lock the 1104173725Sdelphij * source so that we can safely operate on it. */ 1105233851Sgleb if (fdvp != tdvp && fdvp != tvp) { 1106233851Sgleb if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1107233851Sgleb mp = tdvp->v_mount; 1108233851Sgleb error = vfs_busy(mp, 0); 1109233851Sgleb if (error != 0) { 1110233851Sgleb mp = NULL; 1111233851Sgleb goto out; 1112233851Sgleb } 1113233851Sgleb error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp, 1114233851Sgleb fcnp, tcnp); 1115233851Sgleb if (error != 0) { 1116233851Sgleb vfs_unbusy(mp); 1117233851Sgleb return (error); 1118233851Sgleb } 1119233851Sgleb ASSERT_VOP_ELOCKED(fdvp, 1120233851Sgleb "tmpfs_rename: fdvp not locked"); 1121233851Sgleb ASSERT_VOP_ELOCKED(tdvp, 1122233851Sgleb "tmpfs_rename: tdvp not locked"); 1123233851Sgleb if (tvp != NULL) 1124233851Sgleb ASSERT_VOP_ELOCKED(tvp, 1125233851Sgleb "tmpfs_rename: tvp not locked"); 1126233851Sgleb if (fvp == tvp) { 1127233851Sgleb error = 0; 1128233851Sgleb goto out_locked; 1129233851Sgleb } 1130233851Sgleb } 1131233851Sgleb } 1132233851Sgleb 1133233851Sgleb tmp = VFS_TO_TMPFS(tdvp->v_mount); 1134233851Sgleb tdnode = VP_TO_TMPFS_DIR(tdvp); 1135233851Sgleb tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp); 1136173725Sdelphij fdnode = VP_TO_TMPFS_DIR(fdvp); 1137173725Sdelphij fnode = VP_TO_TMPFS_NODE(fvp); 1138188318Skib de = tmpfs_dir_lookup(fdnode, fnode, fcnp); 1139173725Sdelphij 1140212305Sivoras /* Entry can disappear before we lock fdvp, 1141212305Sivoras * also avoid manipulating '.' and '..' entries. */ 1142170808Sdelphij if (de == NULL) { 1143212305Sivoras if ((fcnp->cn_flags & ISDOTDOT) != 0 || 1144212305Sivoras (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 1145212305Sivoras error = EINVAL; 1146212305Sivoras else 1147212305Sivoras error = ENOENT; 1148173725Sdelphij goto out_locked; 1149170808Sdelphij } 1150170808Sdelphij MPASS(de->td_node == fnode); 1151170808Sdelphij 1152171070Sdelphij /* If re-naming a directory to another preexisting directory 1153170808Sdelphij * ensure that the target directory is empty so that its 1154171070Sdelphij * removal causes no side effects. 1155170808Sdelphij * Kern_rename gurantees the destination to be a directory 1156170808Sdelphij * if the source is one. */ 1157170808Sdelphij if (tvp != NULL) { 1158171799Sdelphij MPASS(tnode != NULL); 1159171070Sdelphij 1160170808Sdelphij if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 1161170808Sdelphij (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 1162170808Sdelphij error = EPERM; 1163173725Sdelphij goto out_locked; 1164170808Sdelphij } 1165170808Sdelphij 1166171799Sdelphij if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 1167171799Sdelphij if (tnode->tn_size > 0) { 1168171799Sdelphij error = ENOTEMPTY; 1169173725Sdelphij goto out_locked; 1170171799Sdelphij } 1171171799Sdelphij } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 1172171799Sdelphij error = ENOTDIR; 1173173725Sdelphij goto out_locked; 1174171799Sdelphij } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 1175171799Sdelphij error = EISDIR; 1176173725Sdelphij goto out_locked; 1177171799Sdelphij } else { 1178171799Sdelphij MPASS(fnode->tn_type != VDIR && 1179171799Sdelphij tnode->tn_type != VDIR); 1180170808Sdelphij } 1181170808Sdelphij } 1182170808Sdelphij 1183170808Sdelphij if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) 1184170808Sdelphij || (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 1185170808Sdelphij error = EPERM; 1186170808Sdelphij goto out_locked; 1187170808Sdelphij } 1188170808Sdelphij 1189170808Sdelphij /* Ensure that we have enough memory to hold the new name, if it 1190170808Sdelphij * has to be changed. */ 1191170808Sdelphij if (fcnp->cn_namelen != tcnp->cn_namelen || 1192183299Sobrien bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { 1193171087Sdelphij newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK); 1194170808Sdelphij } else 1195170808Sdelphij newname = NULL; 1196170808Sdelphij 1197170808Sdelphij /* If the node is being moved to another directory, we have to do 1198170808Sdelphij * the move. */ 1199170808Sdelphij if (fdnode != tdnode) { 1200170808Sdelphij /* In case we are moving a directory, we have to adjust its 1201170808Sdelphij * parent to point to the new parent. */ 1202170808Sdelphij if (de->td_node->tn_type == VDIR) { 1203170808Sdelphij struct tmpfs_node *n; 1204170808Sdelphij 1205170808Sdelphij /* Ensure the target directory is not a child of the 1206170808Sdelphij * directory being moved. Otherwise, we'd end up 1207170808Sdelphij * with stale nodes. */ 1208170808Sdelphij n = tdnode; 1209197953Sdelphij /* TMPFS_LOCK garanties that no nodes are freed while 1210197953Sdelphij * traversing the list. Nodes can only be marked as 1211197953Sdelphij * removed: tn_parent == NULL. */ 1212197953Sdelphij TMPFS_LOCK(tmp); 1213197953Sdelphij TMPFS_NODE_LOCK(n); 1214170808Sdelphij while (n != n->tn_dir.tn_parent) { 1215197953Sdelphij struct tmpfs_node *parent; 1216197953Sdelphij 1217170808Sdelphij if (n == fnode) { 1218197953Sdelphij TMPFS_NODE_UNLOCK(n); 1219197953Sdelphij TMPFS_UNLOCK(tmp); 1220170808Sdelphij error = EINVAL; 1221170808Sdelphij if (newname != NULL) 1222171087Sdelphij free(newname, M_TMPFSNAME); 1223170808Sdelphij goto out_locked; 1224170808Sdelphij } 1225197953Sdelphij parent = n->tn_dir.tn_parent; 1226197953Sdelphij TMPFS_NODE_UNLOCK(n); 1227197953Sdelphij if (parent == NULL) { 1228197953Sdelphij n = NULL; 1229197953Sdelphij break; 1230197953Sdelphij } 1231197953Sdelphij TMPFS_NODE_LOCK(parent); 1232197953Sdelphij if (parent->tn_dir.tn_parent == NULL) { 1233197953Sdelphij TMPFS_NODE_UNLOCK(parent); 1234197953Sdelphij n = NULL; 1235197953Sdelphij break; 1236197953Sdelphij } 1237197953Sdelphij n = parent; 1238170808Sdelphij } 1239197953Sdelphij TMPFS_UNLOCK(tmp); 1240197953Sdelphij if (n == NULL) { 1241197953Sdelphij error = EINVAL; 1242197953Sdelphij if (newname != NULL) 1243197953Sdelphij free(newname, M_TMPFSNAME); 1244197953Sdelphij goto out_locked; 1245197953Sdelphij } 1246197953Sdelphij TMPFS_NODE_UNLOCK(n); 1247170808Sdelphij 1248170808Sdelphij /* Adjust the parent pointer. */ 1249170808Sdelphij TMPFS_VALIDATE_DIR(fnode); 1250197953Sdelphij TMPFS_NODE_LOCK(de->td_node); 1251170808Sdelphij de->td_node->tn_dir.tn_parent = tdnode; 1252197953Sdelphij TMPFS_NODE_UNLOCK(de->td_node); 1253170808Sdelphij 1254170808Sdelphij /* As a result of changing the target of the '..' 1255170808Sdelphij * entry, the link count of the source and target 1256170808Sdelphij * directories has to be adjusted. */ 1257197953Sdelphij TMPFS_NODE_LOCK(tdnode); 1258197953Sdelphij TMPFS_ASSERT_LOCKED(tdnode); 1259197953Sdelphij tdnode->tn_links++; 1260197953Sdelphij TMPFS_NODE_UNLOCK(tdnode); 1261197953Sdelphij 1262197953Sdelphij TMPFS_NODE_LOCK(fdnode); 1263197953Sdelphij TMPFS_ASSERT_LOCKED(fdnode); 1264170808Sdelphij fdnode->tn_links--; 1265197953Sdelphij TMPFS_NODE_UNLOCK(fdnode); 1266170808Sdelphij } 1267170808Sdelphij 1268170808Sdelphij /* Do the move: just remove the entry from the source directory 1269170808Sdelphij * and insert it into the target one. */ 1270170808Sdelphij tmpfs_dir_detach(fdvp, de); 1271211598Sed if (fcnp->cn_flags & DOWHITEOUT) 1272211598Sed tmpfs_dir_whiteout_add(fdvp, fcnp); 1273211598Sed if (tcnp->cn_flags & ISWHITEOUT) 1274211598Sed tmpfs_dir_whiteout_remove(tdvp, tcnp); 1275170808Sdelphij tmpfs_dir_attach(tdvp, de); 1276170808Sdelphij } 1277170808Sdelphij 1278170808Sdelphij /* If the name has changed, we need to make it effective by changing 1279170808Sdelphij * it in the directory entry. */ 1280170808Sdelphij if (newname != NULL) { 1281170808Sdelphij MPASS(tcnp->cn_namelen <= MAXNAMLEN); 1282170808Sdelphij 1283171087Sdelphij free(de->td_name, M_TMPFSNAME); 1284170808Sdelphij de->td_namelen = (uint16_t)tcnp->cn_namelen; 1285170808Sdelphij memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen); 1286170808Sdelphij de->td_name = newname; 1287170808Sdelphij 1288170808Sdelphij fnode->tn_status |= TMPFS_NODE_CHANGED; 1289170808Sdelphij tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1290170808Sdelphij } 1291170808Sdelphij 1292170808Sdelphij /* If we are overwriting an entry, we have to remove the old one 1293170808Sdelphij * from the target directory. */ 1294170808Sdelphij if (tvp != NULL) { 1295170808Sdelphij /* Remove the old entry from the target directory. */ 1296188318Skib de = tmpfs_dir_lookup(tdnode, tnode, tcnp); 1297170808Sdelphij tmpfs_dir_detach(tdvp, de); 1298170808Sdelphij 1299170808Sdelphij /* Free the directory entry we just deleted. Note that the 1300170808Sdelphij * node referred by it will not be removed until the vnode is 1301170808Sdelphij * really reclaimed. */ 1302170808Sdelphij tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE); 1303170808Sdelphij } 1304229130Spho cache_purge(fvp); 1305233385Sjhb if (tvp != NULL) 1306233385Sjhb cache_purge(tvp); 1307170808Sdelphij 1308170808Sdelphij error = 0; 1309170808Sdelphij 1310170808Sdelphijout_locked: 1311229855Sivoras if (fdvp != tdvp && fdvp != tvp) 1312175294Sattilio VOP_UNLOCK(fdvp, 0); 1313170808Sdelphij 1314170808Sdelphijout: 1315170808Sdelphij /* Release target nodes. */ 1316170808Sdelphij /* XXX: I don't understand when tdvp can be the same as tvp, but 1317170808Sdelphij * other code takes care of this... */ 1318170808Sdelphij if (tdvp == tvp) 1319170808Sdelphij vrele(tdvp); 1320170808Sdelphij else 1321170808Sdelphij vput(tdvp); 1322170808Sdelphij if (tvp != NULL) 1323170808Sdelphij vput(tvp); 1324170808Sdelphij 1325170808Sdelphij /* Release source nodes. */ 1326170808Sdelphij vrele(fdvp); 1327170808Sdelphij vrele(fvp); 1328170808Sdelphij 1329233851Sgleb if (mp != NULL) 1330233851Sgleb vfs_unbusy(mp); 1331233851Sgleb 1332170808Sdelphij return error; 1333170808Sdelphij} 1334170808Sdelphij 1335170808Sdelphij/* --------------------------------------------------------------------- */ 1336170808Sdelphij 1337171069Sdelphijstatic int 1338170808Sdelphijtmpfs_mkdir(struct vop_mkdir_args *v) 1339170808Sdelphij{ 1340170808Sdelphij struct vnode *dvp = v->a_dvp; 1341170808Sdelphij struct vnode **vpp = v->a_vpp; 1342170808Sdelphij struct componentname *cnp = v->a_cnp; 1343170808Sdelphij struct vattr *vap = v->a_vap; 1344170808Sdelphij 1345170808Sdelphij MPASS(vap->va_type == VDIR); 1346170808Sdelphij 1347170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 1348170808Sdelphij} 1349170808Sdelphij 1350170808Sdelphij/* --------------------------------------------------------------------- */ 1351170808Sdelphij 1352171069Sdelphijstatic int 1353170808Sdelphijtmpfs_rmdir(struct vop_rmdir_args *v) 1354170808Sdelphij{ 1355170808Sdelphij struct vnode *dvp = v->a_dvp; 1356170808Sdelphij struct vnode *vp = v->a_vp; 1357170808Sdelphij 1358170808Sdelphij int error; 1359170808Sdelphij struct tmpfs_dirent *de; 1360170808Sdelphij struct tmpfs_mount *tmp; 1361170808Sdelphij struct tmpfs_node *dnode; 1362170808Sdelphij struct tmpfs_node *node; 1363170808Sdelphij 1364176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 1365176559Sattilio MPASS(VOP_ISLOCKED(vp)); 1366170808Sdelphij 1367170808Sdelphij tmp = VFS_TO_TMPFS(dvp->v_mount); 1368170808Sdelphij dnode = VP_TO_TMPFS_DIR(dvp); 1369170808Sdelphij node = VP_TO_TMPFS_DIR(vp); 1370170808Sdelphij 1371171070Sdelphij /* Directories with more than two entries ('.' and '..') cannot be 1372171070Sdelphij * removed. */ 1373171070Sdelphij if (node->tn_size > 0) { 1374171070Sdelphij error = ENOTEMPTY; 1375171070Sdelphij goto out; 1376171070Sdelphij } 1377170808Sdelphij 1378170808Sdelphij if ((dnode->tn_flags & APPEND) 1379170808Sdelphij || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1380170808Sdelphij error = EPERM; 1381170808Sdelphij goto out; 1382170808Sdelphij } 1383170808Sdelphij 1384171070Sdelphij /* This invariant holds only if we are not trying to remove "..". 1385171070Sdelphij * We checked for that above so this is safe now. */ 1386170808Sdelphij MPASS(node->tn_dir.tn_parent == dnode); 1387170808Sdelphij 1388170808Sdelphij /* Get the directory entry associated with node (vp). This was 1389170808Sdelphij * filled by tmpfs_lookup while looking up the entry. */ 1390188318Skib de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 1391170808Sdelphij MPASS(TMPFS_DIRENT_MATCHES(de, 1392170808Sdelphij v->a_cnp->cn_nameptr, 1393170808Sdelphij v->a_cnp->cn_namelen)); 1394170808Sdelphij 1395170808Sdelphij /* Check flags to see if we are allowed to remove the directory. */ 1396170808Sdelphij if (dnode->tn_flags & APPEND 1397170808Sdelphij || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1398170808Sdelphij error = EPERM; 1399170808Sdelphij goto out; 1400170808Sdelphij } 1401170808Sdelphij 1402197953Sdelphij 1403170808Sdelphij /* Detach the directory entry from the directory (dnode). */ 1404170808Sdelphij tmpfs_dir_detach(dvp, de); 1405211598Sed if (v->a_cnp->cn_flags & DOWHITEOUT) 1406211598Sed tmpfs_dir_whiteout_add(dvp, v->a_cnp); 1407170808Sdelphij 1408197953Sdelphij /* No vnode should be allocated for this entry from this point */ 1409197953Sdelphij TMPFS_NODE_LOCK(node); 1410197953Sdelphij TMPFS_ASSERT_ELOCKED(node); 1411170808Sdelphij node->tn_links--; 1412197953Sdelphij node->tn_dir.tn_parent = NULL; 1413170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1414170808Sdelphij TMPFS_NODE_MODIFIED; 1415197953Sdelphij 1416197953Sdelphij TMPFS_NODE_UNLOCK(node); 1417197953Sdelphij 1418197953Sdelphij TMPFS_NODE_LOCK(dnode); 1419197953Sdelphij TMPFS_ASSERT_ELOCKED(dnode); 1420197953Sdelphij dnode->tn_links--; 1421197953Sdelphij dnode->tn_status |= TMPFS_NODE_ACCESSED | \ 1422170808Sdelphij TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1423197953Sdelphij TMPFS_NODE_UNLOCK(dnode); 1424170808Sdelphij 1425171070Sdelphij cache_purge(dvp); 1426170808Sdelphij cache_purge(vp); 1427170808Sdelphij 1428170808Sdelphij /* Free the directory entry we just deleted. Note that the node 1429170808Sdelphij * referred by it will not be removed until the vnode is really 1430170808Sdelphij * reclaimed. */ 1431170808Sdelphij tmpfs_free_dirent(tmp, de, TRUE); 1432170808Sdelphij 1433170808Sdelphij /* Release the deleted vnode (will destroy the node, notify 1434170808Sdelphij * interested parties and clean it from the cache). */ 1435170808Sdelphij 1436170808Sdelphij dnode->tn_status |= TMPFS_NODE_CHANGED; 1437170808Sdelphij tmpfs_update(dvp); 1438170808Sdelphij 1439170808Sdelphij error = 0; 1440170808Sdelphij 1441170808Sdelphijout: 1442170808Sdelphij return error; 1443170808Sdelphij} 1444170808Sdelphij 1445170808Sdelphij/* --------------------------------------------------------------------- */ 1446170808Sdelphij 1447171069Sdelphijstatic int 1448170808Sdelphijtmpfs_symlink(struct vop_symlink_args *v) 1449170808Sdelphij{ 1450170808Sdelphij struct vnode *dvp = v->a_dvp; 1451170808Sdelphij struct vnode **vpp = v->a_vpp; 1452170808Sdelphij struct componentname *cnp = v->a_cnp; 1453170808Sdelphij struct vattr *vap = v->a_vap; 1454170808Sdelphij char *target = v->a_target; 1455170808Sdelphij 1456170808Sdelphij#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */ 1457170808Sdelphij MPASS(vap->va_type == VLNK); 1458170808Sdelphij#else 1459170808Sdelphij vap->va_type = VLNK; 1460170808Sdelphij#endif 1461170808Sdelphij 1462170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1463170808Sdelphij} 1464170808Sdelphij 1465170808Sdelphij/* --------------------------------------------------------------------- */ 1466170808Sdelphij 1467171069Sdelphijstatic int 1468170808Sdelphijtmpfs_readdir(struct vop_readdir_args *v) 1469170808Sdelphij{ 1470170808Sdelphij struct vnode *vp = v->a_vp; 1471170808Sdelphij struct uio *uio = v->a_uio; 1472170808Sdelphij int *eofflag = v->a_eofflag; 1473170808Sdelphij u_long **cookies = v->a_cookies; 1474170808Sdelphij int *ncookies = v->a_ncookies; 1475170808Sdelphij 1476170808Sdelphij int error; 1477170808Sdelphij off_t startoff; 1478171802Sdelphij off_t cnt = 0; 1479170808Sdelphij struct tmpfs_node *node; 1480170808Sdelphij 1481170808Sdelphij /* This operation only makes sense on directory nodes. */ 1482171802Sdelphij if (vp->v_type != VDIR) 1483171802Sdelphij return ENOTDIR; 1484170808Sdelphij 1485170808Sdelphij node = VP_TO_TMPFS_DIR(vp); 1486170808Sdelphij 1487170808Sdelphij startoff = uio->uio_offset; 1488170808Sdelphij 1489171862Sdelphij if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 1490170808Sdelphij error = tmpfs_dir_getdotdent(node, uio); 1491171862Sdelphij if (error != 0) 1492171862Sdelphij goto outok; 1493171862Sdelphij cnt++; 1494171862Sdelphij } 1495171862Sdelphij 1496171862Sdelphij if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 1497170808Sdelphij error = tmpfs_dir_getdotdotdent(node, uio); 1498171862Sdelphij if (error != 0) 1499171862Sdelphij goto outok; 1500171862Sdelphij cnt++; 1501170808Sdelphij } 1502170808Sdelphij 1503171862Sdelphij error = tmpfs_dir_getdents(node, uio, &cnt); 1504171862Sdelphij 1505171862Sdelphijoutok: 1506171862Sdelphij MPASS(error >= -1); 1507171862Sdelphij 1508170808Sdelphij if (error == -1) 1509217633Skib error = (cnt != 0) ? 0 : EINVAL; 1510170808Sdelphij 1511170808Sdelphij if (eofflag != NULL) 1512170808Sdelphij *eofflag = 1513170808Sdelphij (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1514170808Sdelphij 1515170808Sdelphij /* Update NFS-related variables. */ 1516170808Sdelphij if (error == 0 && cookies != NULL && ncookies != NULL) { 1517170808Sdelphij off_t i; 1518170808Sdelphij off_t off = startoff; 1519170808Sdelphij struct tmpfs_dirent *de = NULL; 1520170808Sdelphij 1521170808Sdelphij *ncookies = cnt; 1522170808Sdelphij *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 1523170808Sdelphij 1524170808Sdelphij for (i = 0; i < cnt; i++) { 1525170808Sdelphij MPASS(off != TMPFS_DIRCOOKIE_EOF); 1526170808Sdelphij if (off == TMPFS_DIRCOOKIE_DOT) { 1527170808Sdelphij off = TMPFS_DIRCOOKIE_DOTDOT; 1528170808Sdelphij } else { 1529170808Sdelphij if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1530170808Sdelphij de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 1531170808Sdelphij } else if (de != NULL) { 1532170808Sdelphij de = TAILQ_NEXT(de, td_entries); 1533170808Sdelphij } else { 1534170808Sdelphij de = tmpfs_dir_lookupbycookie(node, 1535170808Sdelphij off); 1536170808Sdelphij MPASS(de != NULL); 1537170808Sdelphij de = TAILQ_NEXT(de, td_entries); 1538170808Sdelphij } 1539171802Sdelphij if (de == NULL) 1540170808Sdelphij off = TMPFS_DIRCOOKIE_EOF; 1541171802Sdelphij else 1542171802Sdelphij off = tmpfs_dircookie(de); 1543170808Sdelphij } 1544170808Sdelphij 1545170808Sdelphij (*cookies)[i] = off; 1546170808Sdelphij } 1547170808Sdelphij MPASS(uio->uio_offset == off); 1548170808Sdelphij } 1549170808Sdelphij 1550170808Sdelphij return error; 1551170808Sdelphij} 1552170808Sdelphij 1553170808Sdelphij/* --------------------------------------------------------------------- */ 1554170808Sdelphij 1555171069Sdelphijstatic int 1556170808Sdelphijtmpfs_readlink(struct vop_readlink_args *v) 1557170808Sdelphij{ 1558170808Sdelphij struct vnode *vp = v->a_vp; 1559170808Sdelphij struct uio *uio = v->a_uio; 1560170808Sdelphij 1561170808Sdelphij int error; 1562170808Sdelphij struct tmpfs_node *node; 1563170808Sdelphij 1564170808Sdelphij MPASS(uio->uio_offset == 0); 1565170808Sdelphij MPASS(vp->v_type == VLNK); 1566170808Sdelphij 1567170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1568170808Sdelphij 1569170808Sdelphij error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid), 1570170808Sdelphij uio); 1571170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED; 1572170808Sdelphij 1573170808Sdelphij return error; 1574170808Sdelphij} 1575170808Sdelphij 1576170808Sdelphij/* --------------------------------------------------------------------- */ 1577170808Sdelphij 1578171069Sdelphijstatic int 1579170808Sdelphijtmpfs_inactive(struct vop_inactive_args *v) 1580170808Sdelphij{ 1581170808Sdelphij struct vnode *vp = v->a_vp; 1582170808Sdelphij struct thread *l = v->a_td; 1583170808Sdelphij 1584170808Sdelphij struct tmpfs_node *node; 1585170808Sdelphij 1586176559Sattilio MPASS(VOP_ISLOCKED(vp)); 1587170808Sdelphij 1588170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1589170808Sdelphij 1590170808Sdelphij if (node->tn_links == 0) 1591170808Sdelphij vrecycle(vp, l); 1592170808Sdelphij 1593170808Sdelphij return 0; 1594170808Sdelphij} 1595170808Sdelphij 1596170808Sdelphij/* --------------------------------------------------------------------- */ 1597170808Sdelphij 1598170808Sdelphijint 1599170808Sdelphijtmpfs_reclaim(struct vop_reclaim_args *v) 1600170808Sdelphij{ 1601170808Sdelphij struct vnode *vp = v->a_vp; 1602170808Sdelphij 1603170808Sdelphij struct tmpfs_mount *tmp; 1604170808Sdelphij struct tmpfs_node *node; 1605170808Sdelphij 1606170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1607170808Sdelphij tmp = VFS_TO_TMPFS(vp->v_mount); 1608171070Sdelphij 1609170808Sdelphij vnode_destroy_vobject(vp); 1610170808Sdelphij cache_purge(vp); 1611197953Sdelphij 1612197953Sdelphij TMPFS_NODE_LOCK(node); 1613197953Sdelphij TMPFS_ASSERT_ELOCKED(node); 1614170808Sdelphij tmpfs_free_vp(vp); 1615170808Sdelphij 1616170808Sdelphij /* If the node referenced by this vnode was deleted by the user, 1617170808Sdelphij * we must free its associated data structures (now that the vnode 1618170808Sdelphij * is being reclaimed). */ 1619197953Sdelphij if (node->tn_links == 0 && 1620197953Sdelphij (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) { 1621197953Sdelphij node->tn_vpstate = TMPFS_VNODE_DOOMED; 1622197953Sdelphij TMPFS_NODE_UNLOCK(node); 1623170808Sdelphij tmpfs_free_node(tmp, node); 1624197953Sdelphij } else 1625197953Sdelphij TMPFS_NODE_UNLOCK(node); 1626170808Sdelphij 1627170808Sdelphij MPASS(vp->v_data == NULL); 1628170808Sdelphij return 0; 1629170808Sdelphij} 1630170808Sdelphij 1631170808Sdelphij/* --------------------------------------------------------------------- */ 1632170808Sdelphij 1633171069Sdelphijstatic int 1634170808Sdelphijtmpfs_print(struct vop_print_args *v) 1635170808Sdelphij{ 1636170808Sdelphij struct vnode *vp = v->a_vp; 1637170808Sdelphij 1638170808Sdelphij struct tmpfs_node *node; 1639170808Sdelphij 1640170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1641170808Sdelphij 1642170808Sdelphij printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n", 1643170808Sdelphij node, node->tn_flags, node->tn_links); 1644170808Sdelphij printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX 1645170808Sdelphij ", status 0x%x\n", 1646170808Sdelphij node->tn_mode, node->tn_uid, node->tn_gid, 1647170808Sdelphij (uintmax_t)node->tn_size, node->tn_status); 1648170808Sdelphij 1649170808Sdelphij if (vp->v_type == VFIFO) 1650170808Sdelphij fifo_printinfo(vp); 1651170808Sdelphij 1652170808Sdelphij printf("\n"); 1653170808Sdelphij 1654170808Sdelphij return 0; 1655170808Sdelphij} 1656170808Sdelphij 1657170808Sdelphij/* --------------------------------------------------------------------- */ 1658170808Sdelphij 1659171069Sdelphijstatic int 1660170808Sdelphijtmpfs_pathconf(struct vop_pathconf_args *v) 1661170808Sdelphij{ 1662170808Sdelphij int name = v->a_name; 1663170808Sdelphij register_t *retval = v->a_retval; 1664170808Sdelphij 1665170808Sdelphij int error; 1666170808Sdelphij 1667170808Sdelphij error = 0; 1668170808Sdelphij 1669170808Sdelphij switch (name) { 1670170808Sdelphij case _PC_LINK_MAX: 1671170808Sdelphij *retval = LINK_MAX; 1672170808Sdelphij break; 1673170808Sdelphij 1674170808Sdelphij case _PC_NAME_MAX: 1675170808Sdelphij *retval = NAME_MAX; 1676170808Sdelphij break; 1677170808Sdelphij 1678170808Sdelphij case _PC_PATH_MAX: 1679170808Sdelphij *retval = PATH_MAX; 1680170808Sdelphij break; 1681170808Sdelphij 1682170808Sdelphij case _PC_PIPE_BUF: 1683170808Sdelphij *retval = PIPE_BUF; 1684170808Sdelphij break; 1685170808Sdelphij 1686170808Sdelphij case _PC_CHOWN_RESTRICTED: 1687170808Sdelphij *retval = 1; 1688170808Sdelphij break; 1689170808Sdelphij 1690170808Sdelphij case _PC_NO_TRUNC: 1691170808Sdelphij *retval = 1; 1692170808Sdelphij break; 1693170808Sdelphij 1694170808Sdelphij case _PC_SYNC_IO: 1695170808Sdelphij *retval = 1; 1696170808Sdelphij break; 1697170808Sdelphij 1698170808Sdelphij case _PC_FILESIZEBITS: 1699170808Sdelphij *retval = 0; /* XXX Don't know which value should I return. */ 1700170808Sdelphij break; 1701170808Sdelphij 1702170808Sdelphij default: 1703170808Sdelphij error = EINVAL; 1704170808Sdelphij } 1705170808Sdelphij 1706170808Sdelphij return error; 1707170808Sdelphij} 1708170808Sdelphij 1709171069Sdelphijstatic int 1710170808Sdelphijtmpfs_vptofh(struct vop_vptofh_args *ap) 1711170808Sdelphij{ 1712170808Sdelphij struct tmpfs_fid *tfhp; 1713170808Sdelphij struct tmpfs_node *node; 1714170808Sdelphij 1715170808Sdelphij tfhp = (struct tmpfs_fid *)ap->a_fhp; 1716170808Sdelphij node = VP_TO_TMPFS_NODE(ap->a_vp); 1717170808Sdelphij 1718170808Sdelphij tfhp->tf_len = sizeof(struct tmpfs_fid); 1719170808Sdelphij tfhp->tf_id = node->tn_id; 1720170808Sdelphij tfhp->tf_gen = node->tn_gen; 1721171070Sdelphij 1722170808Sdelphij return (0); 1723170808Sdelphij} 1724171069Sdelphij 1725211598Sedstatic int 1726211598Sedtmpfs_whiteout(struct vop_whiteout_args *ap) 1727211598Sed{ 1728211598Sed struct vnode *dvp = ap->a_dvp; 1729211598Sed struct componentname *cnp = ap->a_cnp; 1730211598Sed struct tmpfs_dirent *de; 1731211598Sed 1732211598Sed switch (ap->a_flags) { 1733211598Sed case LOOKUP: 1734211598Sed return (0); 1735211598Sed case CREATE: 1736211598Sed de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1737211598Sed if (de != NULL) 1738211598Sed return (de->td_node == NULL ? 0 : EEXIST); 1739211598Sed return (tmpfs_dir_whiteout_add(dvp, cnp)); 1740211598Sed case DELETE: 1741211598Sed tmpfs_dir_whiteout_remove(dvp, cnp); 1742211598Sed return (0); 1743211598Sed default: 1744211598Sed panic("tmpfs_whiteout: unknown op"); 1745211598Sed } 1746211598Sed} 1747211598Sed 1748171069Sdelphij/* --------------------------------------------------------------------- */ 1749171069Sdelphij 1750171069Sdelphij/* 1751171069Sdelphij * vnode operations vector used for files stored in a tmpfs file system. 1752171069Sdelphij */ 1753171069Sdelphijstruct vop_vector tmpfs_vnodeop_entries = { 1754171069Sdelphij .vop_default = &default_vnodeops, 1755171069Sdelphij .vop_lookup = vfs_cache_lookup, 1756171069Sdelphij .vop_cachedlookup = tmpfs_lookup, 1757171069Sdelphij .vop_create = tmpfs_create, 1758171069Sdelphij .vop_mknod = tmpfs_mknod, 1759171069Sdelphij .vop_open = tmpfs_open, 1760171069Sdelphij .vop_close = tmpfs_close, 1761171069Sdelphij .vop_access = tmpfs_access, 1762171069Sdelphij .vop_getattr = tmpfs_getattr, 1763171069Sdelphij .vop_setattr = tmpfs_setattr, 1764171069Sdelphij .vop_read = tmpfs_read, 1765171069Sdelphij .vop_write = tmpfs_write, 1766171069Sdelphij .vop_fsync = tmpfs_fsync, 1767171069Sdelphij .vop_remove = tmpfs_remove, 1768171069Sdelphij .vop_link = tmpfs_link, 1769171069Sdelphij .vop_rename = tmpfs_rename, 1770171069Sdelphij .vop_mkdir = tmpfs_mkdir, 1771171069Sdelphij .vop_rmdir = tmpfs_rmdir, 1772171069Sdelphij .vop_symlink = tmpfs_symlink, 1773171069Sdelphij .vop_readdir = tmpfs_readdir, 1774171069Sdelphij .vop_readlink = tmpfs_readlink, 1775171069Sdelphij .vop_inactive = tmpfs_inactive, 1776171069Sdelphij .vop_reclaim = tmpfs_reclaim, 1777171069Sdelphij .vop_print = tmpfs_print, 1778171069Sdelphij .vop_pathconf = tmpfs_pathconf, 1779171069Sdelphij .vop_vptofh = tmpfs_vptofh, 1780211598Sed .vop_whiteout = tmpfs_whiteout, 1781171069Sdelphij .vop_bmap = VOP_EOPNOTSUPP, 1782171069Sdelphij}; 1783171069Sdelphij 1784