tmpfs_vnops.c revision 236209
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 236209 2012-05-28 23:31:48Z alc $"); 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; 448236209Salc int error, rv; 449171489Sdelphij 450197850Sdelphij VM_OBJECT_LOCK(tobj); 451197850Sdelphij m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 452231775Salc VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 453197850Sdelphij if (m->valid != VM_PAGE_BITS_ALL) { 454197850Sdelphij if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 455236209Salc rv = vm_pager_get_pages(tobj, &m, 1, 0); 456236209Salc if (rv != VM_PAGER_OK) { 457236209Salc vm_page_lock(m); 458236209Salc vm_page_free(m); 459236209Salc vm_page_unlock(m); 460236209Salc VM_OBJECT_UNLOCK(tobj); 461236209Salc return (EIO); 462197850Sdelphij } 463197850Sdelphij } else 464197850Sdelphij vm_page_zero_invalid(m, TRUE); 465197850Sdelphij } 466197850Sdelphij VM_OBJECT_UNLOCK(tobj); 467197850Sdelphij error = uiomove_fromphys(&m, offset, tlen, uio); 468197850Sdelphij VM_OBJECT_LOCK(tobj); 469207573Salc vm_page_lock(m); 470197850Sdelphij vm_page_unwire(m, TRUE); 471207573Salc vm_page_unlock(m); 472197850Sdelphij vm_page_wakeup(m); 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; 635236209Salc int error, rv; 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 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED | 676231775Salc VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 677171489Sdelphij if (tpg->valid != VM_PAGE_BITS_ALL) { 678194124Salc if (vm_pager_has_page(tobj, idx, NULL, NULL)) { 679236209Salc rv = vm_pager_get_pages(tobj, &tpg, 1, 0); 680236209Salc if (rv != VM_PAGER_OK) { 681236209Salc vm_page_lock(tpg); 682236209Salc vm_page_free(tpg); 683236209Salc vm_page_unlock(tpg); 684236209Salc error = EIO; 685171489Sdelphij goto out; 686171489Sdelphij } 687171489Sdelphij } else 688171489Sdelphij vm_page_zero_invalid(tpg, TRUE); 689171489Sdelphij } 690171489Sdelphij VM_OBJECT_UNLOCK(tobj); 691188929Salc if (vpg == NULL) 692188929Salc error = uiomove_fromphys(&tpg, offset, tlen, uio); 693188929Salc else { 694171489Sdelphij KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid")); 695171489Sdelphij pmap_copy_page(vpg, tpg); 696171489Sdelphij } 697171489Sdelphij VM_OBJECT_LOCK(tobj); 698171489Sdelphij if (error == 0) { 699192917Salc KASSERT(tpg->valid == VM_PAGE_BITS_ALL, 700192917Salc ("parts of tpg invalid")); 701171489Sdelphij vm_page_dirty(tpg); 702171489Sdelphij } 703209226Salc vm_page_lock(tpg); 704188921Salc vm_page_unwire(tpg, TRUE); 705207573Salc vm_page_unlock(tpg); 706171489Sdelphij vm_page_wakeup(tpg); 707236209Salcout: 708236209Salc VM_OBJECT_UNLOCK(tobj); 709236209Salc if (vpg != NULL) { 710236209Salc VM_OBJECT_LOCK(vobj); 711171489Sdelphij vm_page_wakeup(vpg); 712171489Sdelphij VM_OBJECT_UNLOCK(vobj); 713236209Salc } 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) { 758236208Salc error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid, 759236208Salc FALSE); 760170808Sdelphij if (error != 0) 761170808Sdelphij goto out; 762170808Sdelphij } 763170808Sdelphij 764170808Sdelphij uobj = node->tn_reg.tn_aobj; 765171489Sdelphij while ((resid = uio->uio_resid) > 0) { 766171489Sdelphij if (node->tn_size <= uio->uio_offset) 767171489Sdelphij break; 768171489Sdelphij len = MIN(node->tn_size - uio->uio_offset, resid); 769171489Sdelphij if (len == 0) 770171489Sdelphij break; 771171489Sdelphij error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio); 772171489Sdelphij if ((error != 0) || (resid == uio->uio_resid)) 773171489Sdelphij break; 774171489Sdelphij } 775170808Sdelphij 776170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 777170808Sdelphij (extended ? TMPFS_NODE_CHANGED : 0); 778170808Sdelphij 779170808Sdelphij if (node->tn_mode & (S_ISUID | S_ISGID)) { 780170808Sdelphij if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0)) 781170808Sdelphij node->tn_mode &= ~(S_ISUID | S_ISGID); 782170808Sdelphij } 783170808Sdelphij 784170808Sdelphij if (error != 0) 785236208Salc (void)tmpfs_reg_resize(vp, oldsize, TRUE); 786170808Sdelphij 787170808Sdelphijout: 788170808Sdelphij MPASS(IMPLIES(error == 0, uio->uio_resid == 0)); 789170808Sdelphij MPASS(IMPLIES(error != 0, oldsize == node->tn_size)); 790170808Sdelphij 791170808Sdelphij return error; 792170808Sdelphij} 793170808Sdelphij 794170808Sdelphij/* --------------------------------------------------------------------- */ 795170808Sdelphij 796171069Sdelphijstatic int 797170808Sdelphijtmpfs_fsync(struct vop_fsync_args *v) 798170808Sdelphij{ 799170808Sdelphij struct vnode *vp = v->a_vp; 800170808Sdelphij 801176559Sattilio MPASS(VOP_ISLOCKED(vp)); 802170808Sdelphij 803170808Sdelphij tmpfs_update(vp); 804170808Sdelphij 805170808Sdelphij return 0; 806170808Sdelphij} 807170808Sdelphij 808170808Sdelphij/* --------------------------------------------------------------------- */ 809170808Sdelphij 810171069Sdelphijstatic int 811170808Sdelphijtmpfs_remove(struct vop_remove_args *v) 812170808Sdelphij{ 813170808Sdelphij struct vnode *dvp = v->a_dvp; 814170808Sdelphij struct vnode *vp = v->a_vp; 815170808Sdelphij 816170808Sdelphij int error; 817170808Sdelphij struct tmpfs_dirent *de; 818170808Sdelphij struct tmpfs_mount *tmp; 819170808Sdelphij struct tmpfs_node *dnode; 820170808Sdelphij struct tmpfs_node *node; 821170808Sdelphij 822176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 823176559Sattilio MPASS(VOP_ISLOCKED(vp)); 824170808Sdelphij 825170808Sdelphij if (vp->v_type == VDIR) { 826170808Sdelphij error = EISDIR; 827170808Sdelphij goto out; 828170808Sdelphij } 829170808Sdelphij 830170808Sdelphij dnode = VP_TO_TMPFS_DIR(dvp); 831170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 832170808Sdelphij tmp = VFS_TO_TMPFS(vp->v_mount); 833188318Skib de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 834170808Sdelphij MPASS(de != NULL); 835170808Sdelphij 836170808Sdelphij /* Files marked as immutable or append-only cannot be deleted. */ 837170808Sdelphij if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) || 838170808Sdelphij (dnode->tn_flags & APPEND)) { 839170808Sdelphij error = EPERM; 840170808Sdelphij goto out; 841170808Sdelphij } 842170808Sdelphij 843170808Sdelphij /* Remove the entry from the directory; as it is a file, we do not 844170808Sdelphij * have to change the number of hard links of the directory. */ 845170808Sdelphij tmpfs_dir_detach(dvp, de); 846211598Sed if (v->a_cnp->cn_flags & DOWHITEOUT) 847211598Sed tmpfs_dir_whiteout_add(dvp, v->a_cnp); 848170808Sdelphij 849170808Sdelphij /* Free the directory entry we just deleted. Note that the node 850170808Sdelphij * referred by it will not be removed until the vnode is really 851170808Sdelphij * reclaimed. */ 852170808Sdelphij tmpfs_free_dirent(tmp, de, TRUE); 853170808Sdelphij 854218949Salc node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED; 855170808Sdelphij error = 0; 856170808Sdelphij 857170808Sdelphijout: 858170808Sdelphij 859170808Sdelphij return error; 860170808Sdelphij} 861170808Sdelphij 862170808Sdelphij/* --------------------------------------------------------------------- */ 863170808Sdelphij 864171069Sdelphijstatic int 865170808Sdelphijtmpfs_link(struct vop_link_args *v) 866170808Sdelphij{ 867170808Sdelphij struct vnode *dvp = v->a_tdvp; 868170808Sdelphij struct vnode *vp = v->a_vp; 869170808Sdelphij struct componentname *cnp = v->a_cnp; 870170808Sdelphij 871170808Sdelphij int error; 872170808Sdelphij struct tmpfs_dirent *de; 873170808Sdelphij struct tmpfs_node *node; 874170808Sdelphij 875176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 876170808Sdelphij MPASS(cnp->cn_flags & HASBUF); 877170808Sdelphij MPASS(dvp != vp); /* XXX When can this be false? */ 878170808Sdelphij 879170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 880170808Sdelphij 881170808Sdelphij /* XXX: Why aren't the following two tests done by the caller? */ 882170808Sdelphij 883170808Sdelphij /* Hard links of directories are forbidden. */ 884170808Sdelphij if (vp->v_type == VDIR) { 885170808Sdelphij error = EPERM; 886170808Sdelphij goto out; 887170808Sdelphij } 888170808Sdelphij 889170808Sdelphij /* Cannot create cross-device links. */ 890170808Sdelphij if (dvp->v_mount != vp->v_mount) { 891170808Sdelphij error = EXDEV; 892170808Sdelphij goto out; 893170808Sdelphij } 894170808Sdelphij 895170808Sdelphij /* Ensure that we do not overflow the maximum number of links imposed 896170808Sdelphij * by the system. */ 897170808Sdelphij MPASS(node->tn_links <= LINK_MAX); 898170808Sdelphij if (node->tn_links == LINK_MAX) { 899170808Sdelphij error = EMLINK; 900170808Sdelphij goto out; 901170808Sdelphij } 902170808Sdelphij 903170808Sdelphij /* We cannot create links of files marked immutable or append-only. */ 904170808Sdelphij if (node->tn_flags & (IMMUTABLE | APPEND)) { 905170808Sdelphij error = EPERM; 906170808Sdelphij goto out; 907170808Sdelphij } 908170808Sdelphij 909170808Sdelphij /* Allocate a new directory entry to represent the node. */ 910170808Sdelphij error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node, 911170808Sdelphij cnp->cn_nameptr, cnp->cn_namelen, &de); 912170808Sdelphij if (error != 0) 913170808Sdelphij goto out; 914170808Sdelphij 915170808Sdelphij /* Insert the new directory entry into the appropriate directory. */ 916211598Sed if (cnp->cn_flags & ISWHITEOUT) 917211598Sed tmpfs_dir_whiteout_remove(dvp, cnp); 918170808Sdelphij tmpfs_dir_attach(dvp, de); 919170808Sdelphij 920170808Sdelphij /* vp link count has changed, so update node times. */ 921170808Sdelphij node->tn_status |= TMPFS_NODE_CHANGED; 922170808Sdelphij tmpfs_update(vp); 923170808Sdelphij 924170808Sdelphij error = 0; 925171070Sdelphij 926170808Sdelphijout: 927170808Sdelphij return error; 928170808Sdelphij} 929170808Sdelphij 930170808Sdelphij/* --------------------------------------------------------------------- */ 931170808Sdelphij 932233851Sgleb/* 933233851Sgleb * We acquire all but fdvp locks using non-blocking acquisitions. If we 934233851Sgleb * fail to acquire any lock in the path we will drop all held locks, 935233851Sgleb * acquire the new lock in a blocking fashion, and then release it and 936233851Sgleb * restart the rename. This acquire/release step ensures that we do not 937233851Sgleb * spin on a lock waiting for release. On error release all vnode locks 938233851Sgleb * and decrement references the way tmpfs_rename() would do. 939233851Sgleb */ 940171069Sdelphijstatic int 941233851Sglebtmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp, 942233851Sgleb struct vnode *tdvp, struct vnode **tvpp, 943233851Sgleb struct componentname *fcnp, struct componentname *tcnp) 944233851Sgleb{ 945233851Sgleb struct vnode *nvp; 946233851Sgleb struct mount *mp; 947233851Sgleb struct tmpfs_dirent *de; 948233851Sgleb int error, restarts = 0; 949233851Sgleb 950233851Sgleb VOP_UNLOCK(tdvp, 0); 951233851Sgleb if (*tvpp != NULL && *tvpp != tdvp) 952233851Sgleb VOP_UNLOCK(*tvpp, 0); 953233851Sgleb mp = fdvp->v_mount; 954233851Sgleb 955233851Sglebrelock: 956233851Sgleb restarts += 1; 957233851Sgleb error = vn_lock(fdvp, LK_EXCLUSIVE); 958233851Sgleb if (error) 959233851Sgleb goto releout; 960233851Sgleb if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 961233851Sgleb VOP_UNLOCK(fdvp, 0); 962233851Sgleb error = vn_lock(tdvp, LK_EXCLUSIVE); 963233851Sgleb if (error) 964233851Sgleb goto releout; 965233851Sgleb VOP_UNLOCK(tdvp, 0); 966233851Sgleb goto relock; 967233851Sgleb } 968233851Sgleb /* 969233851Sgleb * Re-resolve fvp to be certain it still exists and fetch the 970233851Sgleb * correct vnode. 971233851Sgleb */ 972233851Sgleb de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp); 973233851Sgleb if (de == NULL) { 974233851Sgleb VOP_UNLOCK(fdvp, 0); 975233851Sgleb VOP_UNLOCK(tdvp, 0); 976233851Sgleb if ((fcnp->cn_flags & ISDOTDOT) != 0 || 977233851Sgleb (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 978233851Sgleb error = EINVAL; 979233851Sgleb else 980233851Sgleb error = ENOENT; 981233851Sgleb goto releout; 982233851Sgleb } 983233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp); 984233851Sgleb if (error != 0) { 985233851Sgleb VOP_UNLOCK(fdvp, 0); 986233851Sgleb VOP_UNLOCK(tdvp, 0); 987233851Sgleb if (error != EBUSY) 988233851Sgleb goto releout; 989233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp); 990233851Sgleb if (error != 0) 991233851Sgleb goto releout; 992233851Sgleb VOP_UNLOCK(nvp, 0); 993233851Sgleb /* 994233851Sgleb * Concurrent rename race. 995233851Sgleb */ 996233851Sgleb if (nvp == tdvp) { 997233851Sgleb vrele(nvp); 998233851Sgleb error = EINVAL; 999233851Sgleb goto releout; 1000233851Sgleb } 1001233851Sgleb vrele(*fvpp); 1002233851Sgleb *fvpp = nvp; 1003233851Sgleb goto relock; 1004233851Sgleb } 1005233851Sgleb vrele(*fvpp); 1006233851Sgleb *fvpp = nvp; 1007233851Sgleb VOP_UNLOCK(*fvpp, 0); 1008233851Sgleb /* 1009233851Sgleb * Re-resolve tvp and acquire the vnode lock if present. 1010233851Sgleb */ 1011233851Sgleb de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp); 1012233851Sgleb /* 1013233851Sgleb * If tvp disappeared we just carry on. 1014233851Sgleb */ 1015233851Sgleb if (de == NULL && *tvpp != NULL) { 1016233851Sgleb vrele(*tvpp); 1017233851Sgleb *tvpp = NULL; 1018233851Sgleb } 1019233851Sgleb /* 1020233851Sgleb * Get the tvp ino if the lookup succeeded. We may have to restart 1021233851Sgleb * if the non-blocking acquire fails. 1022233851Sgleb */ 1023233851Sgleb if (de != NULL) { 1024233851Sgleb nvp = NULL; 1025233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, 1026233851Sgleb LK_EXCLUSIVE | LK_NOWAIT, &nvp); 1027233851Sgleb if (*tvpp != NULL) 1028233851Sgleb vrele(*tvpp); 1029233851Sgleb *tvpp = nvp; 1030233851Sgleb if (error != 0) { 1031233851Sgleb VOP_UNLOCK(fdvp, 0); 1032233851Sgleb VOP_UNLOCK(tdvp, 0); 1033233851Sgleb if (error != EBUSY) 1034233851Sgleb goto releout; 1035233851Sgleb error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, 1036233851Sgleb &nvp); 1037233851Sgleb if (error != 0) 1038233851Sgleb goto releout; 1039233851Sgleb VOP_UNLOCK(nvp, 0); 1040233851Sgleb /* 1041233851Sgleb * fdvp contains fvp, thus tvp (=fdvp) is not empty. 1042233851Sgleb */ 1043233851Sgleb if (nvp == fdvp) { 1044233851Sgleb error = ENOTEMPTY; 1045233851Sgleb goto releout; 1046233851Sgleb } 1047233851Sgleb goto relock; 1048233851Sgleb } 1049233851Sgleb } 1050233851Sgleb tmpfs_rename_restarts += restarts; 1051233851Sgleb 1052233851Sgleb return (0); 1053233851Sgleb 1054233851Sglebreleout: 1055233851Sgleb vrele(fdvp); 1056233851Sgleb vrele(*fvpp); 1057233851Sgleb vrele(tdvp); 1058233851Sgleb if (*tvpp != NULL) 1059233851Sgleb vrele(*tvpp); 1060233851Sgleb tmpfs_rename_restarts += restarts; 1061233851Sgleb 1062233851Sgleb return (error); 1063233851Sgleb} 1064233851Sgleb 1065233851Sglebstatic int 1066170808Sdelphijtmpfs_rename(struct vop_rename_args *v) 1067170808Sdelphij{ 1068170808Sdelphij struct vnode *fdvp = v->a_fdvp; 1069170808Sdelphij struct vnode *fvp = v->a_fvp; 1070170808Sdelphij struct componentname *fcnp = v->a_fcnp; 1071170808Sdelphij struct vnode *tdvp = v->a_tdvp; 1072170808Sdelphij struct vnode *tvp = v->a_tvp; 1073170808Sdelphij struct componentname *tcnp = v->a_tcnp; 1074233851Sgleb struct mount *mp = NULL; 1075170808Sdelphij 1076170808Sdelphij char *newname; 1077170808Sdelphij int error; 1078170808Sdelphij struct tmpfs_dirent *de; 1079197953Sdelphij struct tmpfs_mount *tmp; 1080170808Sdelphij struct tmpfs_node *fdnode; 1081170808Sdelphij struct tmpfs_node *fnode; 1082171799Sdelphij struct tmpfs_node *tnode; 1083170808Sdelphij struct tmpfs_node *tdnode; 1084170808Sdelphij 1085176559Sattilio MPASS(VOP_ISLOCKED(tdvp)); 1086176559Sattilio MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp))); 1087170808Sdelphij MPASS(fcnp->cn_flags & HASBUF); 1088170808Sdelphij MPASS(tcnp->cn_flags & HASBUF); 1089170808Sdelphij 1090170808Sdelphij /* Disallow cross-device renames. 1091170808Sdelphij * XXX Why isn't this done by the caller? */ 1092170808Sdelphij if (fvp->v_mount != tdvp->v_mount || 1093170808Sdelphij (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 1094170808Sdelphij error = EXDEV; 1095170808Sdelphij goto out; 1096170808Sdelphij } 1097170808Sdelphij 1098170808Sdelphij /* If source and target are the same file, there is nothing to do. */ 1099170808Sdelphij if (fvp == tvp) { 1100170808Sdelphij error = 0; 1101170808Sdelphij goto out; 1102170808Sdelphij } 1103170808Sdelphij 1104173725Sdelphij /* If we need to move the directory between entries, lock the 1105173725Sdelphij * source so that we can safely operate on it. */ 1106233851Sgleb if (fdvp != tdvp && fdvp != tvp) { 1107233851Sgleb if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1108233851Sgleb mp = tdvp->v_mount; 1109233851Sgleb error = vfs_busy(mp, 0); 1110233851Sgleb if (error != 0) { 1111233851Sgleb mp = NULL; 1112233851Sgleb goto out; 1113233851Sgleb } 1114233851Sgleb error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp, 1115233851Sgleb fcnp, tcnp); 1116233851Sgleb if (error != 0) { 1117233851Sgleb vfs_unbusy(mp); 1118233851Sgleb return (error); 1119233851Sgleb } 1120233851Sgleb ASSERT_VOP_ELOCKED(fdvp, 1121233851Sgleb "tmpfs_rename: fdvp not locked"); 1122233851Sgleb ASSERT_VOP_ELOCKED(tdvp, 1123233851Sgleb "tmpfs_rename: tdvp not locked"); 1124233851Sgleb if (tvp != NULL) 1125233851Sgleb ASSERT_VOP_ELOCKED(tvp, 1126233851Sgleb "tmpfs_rename: tvp not locked"); 1127233851Sgleb if (fvp == tvp) { 1128233851Sgleb error = 0; 1129233851Sgleb goto out_locked; 1130233851Sgleb } 1131233851Sgleb } 1132233851Sgleb } 1133233851Sgleb 1134233851Sgleb tmp = VFS_TO_TMPFS(tdvp->v_mount); 1135233851Sgleb tdnode = VP_TO_TMPFS_DIR(tdvp); 1136233851Sgleb tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp); 1137173725Sdelphij fdnode = VP_TO_TMPFS_DIR(fdvp); 1138173725Sdelphij fnode = VP_TO_TMPFS_NODE(fvp); 1139188318Skib de = tmpfs_dir_lookup(fdnode, fnode, fcnp); 1140173725Sdelphij 1141212305Sivoras /* Entry can disappear before we lock fdvp, 1142212305Sivoras * also avoid manipulating '.' and '..' entries. */ 1143170808Sdelphij if (de == NULL) { 1144212305Sivoras if ((fcnp->cn_flags & ISDOTDOT) != 0 || 1145212305Sivoras (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) 1146212305Sivoras error = EINVAL; 1147212305Sivoras else 1148212305Sivoras error = ENOENT; 1149173725Sdelphij goto out_locked; 1150170808Sdelphij } 1151170808Sdelphij MPASS(de->td_node == fnode); 1152170808Sdelphij 1153171070Sdelphij /* If re-naming a directory to another preexisting directory 1154170808Sdelphij * ensure that the target directory is empty so that its 1155171070Sdelphij * removal causes no side effects. 1156170808Sdelphij * Kern_rename gurantees the destination to be a directory 1157170808Sdelphij * if the source is one. */ 1158170808Sdelphij if (tvp != NULL) { 1159171799Sdelphij MPASS(tnode != NULL); 1160171070Sdelphij 1161170808Sdelphij if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 1162170808Sdelphij (tdnode->tn_flags & (APPEND | IMMUTABLE))) { 1163170808Sdelphij error = EPERM; 1164173725Sdelphij goto out_locked; 1165170808Sdelphij } 1166170808Sdelphij 1167171799Sdelphij if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) { 1168171799Sdelphij if (tnode->tn_size > 0) { 1169171799Sdelphij error = ENOTEMPTY; 1170173725Sdelphij goto out_locked; 1171171799Sdelphij } 1172171799Sdelphij } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) { 1173171799Sdelphij error = ENOTDIR; 1174173725Sdelphij goto out_locked; 1175171799Sdelphij } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) { 1176171799Sdelphij error = EISDIR; 1177173725Sdelphij goto out_locked; 1178171799Sdelphij } else { 1179171799Sdelphij MPASS(fnode->tn_type != VDIR && 1180171799Sdelphij tnode->tn_type != VDIR); 1181170808Sdelphij } 1182170808Sdelphij } 1183170808Sdelphij 1184170808Sdelphij if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) 1185170808Sdelphij || (fdnode->tn_flags & (APPEND | IMMUTABLE))) { 1186170808Sdelphij error = EPERM; 1187170808Sdelphij goto out_locked; 1188170808Sdelphij } 1189170808Sdelphij 1190170808Sdelphij /* Ensure that we have enough memory to hold the new name, if it 1191170808Sdelphij * has to be changed. */ 1192170808Sdelphij if (fcnp->cn_namelen != tcnp->cn_namelen || 1193183299Sobrien bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { 1194171087Sdelphij newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK); 1195170808Sdelphij } else 1196170808Sdelphij newname = NULL; 1197170808Sdelphij 1198170808Sdelphij /* If the node is being moved to another directory, we have to do 1199170808Sdelphij * the move. */ 1200170808Sdelphij if (fdnode != tdnode) { 1201170808Sdelphij /* In case we are moving a directory, we have to adjust its 1202170808Sdelphij * parent to point to the new parent. */ 1203170808Sdelphij if (de->td_node->tn_type == VDIR) { 1204170808Sdelphij struct tmpfs_node *n; 1205170808Sdelphij 1206170808Sdelphij /* Ensure the target directory is not a child of the 1207170808Sdelphij * directory being moved. Otherwise, we'd end up 1208170808Sdelphij * with stale nodes. */ 1209170808Sdelphij n = tdnode; 1210197953Sdelphij /* TMPFS_LOCK garanties that no nodes are freed while 1211197953Sdelphij * traversing the list. Nodes can only be marked as 1212197953Sdelphij * removed: tn_parent == NULL. */ 1213197953Sdelphij TMPFS_LOCK(tmp); 1214197953Sdelphij TMPFS_NODE_LOCK(n); 1215170808Sdelphij while (n != n->tn_dir.tn_parent) { 1216197953Sdelphij struct tmpfs_node *parent; 1217197953Sdelphij 1218170808Sdelphij if (n == fnode) { 1219197953Sdelphij TMPFS_NODE_UNLOCK(n); 1220197953Sdelphij TMPFS_UNLOCK(tmp); 1221170808Sdelphij error = EINVAL; 1222170808Sdelphij if (newname != NULL) 1223171087Sdelphij free(newname, M_TMPFSNAME); 1224170808Sdelphij goto out_locked; 1225170808Sdelphij } 1226197953Sdelphij parent = n->tn_dir.tn_parent; 1227197953Sdelphij TMPFS_NODE_UNLOCK(n); 1228197953Sdelphij if (parent == NULL) { 1229197953Sdelphij n = NULL; 1230197953Sdelphij break; 1231197953Sdelphij } 1232197953Sdelphij TMPFS_NODE_LOCK(parent); 1233197953Sdelphij if (parent->tn_dir.tn_parent == NULL) { 1234197953Sdelphij TMPFS_NODE_UNLOCK(parent); 1235197953Sdelphij n = NULL; 1236197953Sdelphij break; 1237197953Sdelphij } 1238197953Sdelphij n = parent; 1239170808Sdelphij } 1240197953Sdelphij TMPFS_UNLOCK(tmp); 1241197953Sdelphij if (n == NULL) { 1242197953Sdelphij error = EINVAL; 1243197953Sdelphij if (newname != NULL) 1244197953Sdelphij free(newname, M_TMPFSNAME); 1245197953Sdelphij goto out_locked; 1246197953Sdelphij } 1247197953Sdelphij TMPFS_NODE_UNLOCK(n); 1248170808Sdelphij 1249170808Sdelphij /* Adjust the parent pointer. */ 1250170808Sdelphij TMPFS_VALIDATE_DIR(fnode); 1251197953Sdelphij TMPFS_NODE_LOCK(de->td_node); 1252170808Sdelphij de->td_node->tn_dir.tn_parent = tdnode; 1253197953Sdelphij TMPFS_NODE_UNLOCK(de->td_node); 1254170808Sdelphij 1255170808Sdelphij /* As a result of changing the target of the '..' 1256170808Sdelphij * entry, the link count of the source and target 1257170808Sdelphij * directories has to be adjusted. */ 1258197953Sdelphij TMPFS_NODE_LOCK(tdnode); 1259197953Sdelphij TMPFS_ASSERT_LOCKED(tdnode); 1260197953Sdelphij tdnode->tn_links++; 1261197953Sdelphij TMPFS_NODE_UNLOCK(tdnode); 1262197953Sdelphij 1263197953Sdelphij TMPFS_NODE_LOCK(fdnode); 1264197953Sdelphij TMPFS_ASSERT_LOCKED(fdnode); 1265170808Sdelphij fdnode->tn_links--; 1266197953Sdelphij TMPFS_NODE_UNLOCK(fdnode); 1267170808Sdelphij } 1268170808Sdelphij 1269170808Sdelphij /* Do the move: just remove the entry from the source directory 1270170808Sdelphij * and insert it into the target one. */ 1271170808Sdelphij tmpfs_dir_detach(fdvp, de); 1272211598Sed if (fcnp->cn_flags & DOWHITEOUT) 1273211598Sed tmpfs_dir_whiteout_add(fdvp, fcnp); 1274211598Sed if (tcnp->cn_flags & ISWHITEOUT) 1275211598Sed tmpfs_dir_whiteout_remove(tdvp, tcnp); 1276170808Sdelphij tmpfs_dir_attach(tdvp, de); 1277170808Sdelphij } 1278170808Sdelphij 1279170808Sdelphij /* If the name has changed, we need to make it effective by changing 1280170808Sdelphij * it in the directory entry. */ 1281170808Sdelphij if (newname != NULL) { 1282170808Sdelphij MPASS(tcnp->cn_namelen <= MAXNAMLEN); 1283170808Sdelphij 1284171087Sdelphij free(de->td_name, M_TMPFSNAME); 1285170808Sdelphij de->td_namelen = (uint16_t)tcnp->cn_namelen; 1286170808Sdelphij memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen); 1287170808Sdelphij de->td_name = newname; 1288170808Sdelphij 1289170808Sdelphij fnode->tn_status |= TMPFS_NODE_CHANGED; 1290170808Sdelphij tdnode->tn_status |= TMPFS_NODE_MODIFIED; 1291170808Sdelphij } 1292170808Sdelphij 1293170808Sdelphij /* If we are overwriting an entry, we have to remove the old one 1294170808Sdelphij * from the target directory. */ 1295170808Sdelphij if (tvp != NULL) { 1296170808Sdelphij /* Remove the old entry from the target directory. */ 1297188318Skib de = tmpfs_dir_lookup(tdnode, tnode, tcnp); 1298170808Sdelphij tmpfs_dir_detach(tdvp, de); 1299170808Sdelphij 1300170808Sdelphij /* Free the directory entry we just deleted. Note that the 1301170808Sdelphij * node referred by it will not be removed until the vnode is 1302170808Sdelphij * really reclaimed. */ 1303170808Sdelphij tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE); 1304170808Sdelphij } 1305229130Spho cache_purge(fvp); 1306233385Sjhb if (tvp != NULL) 1307233385Sjhb cache_purge(tvp); 1308170808Sdelphij 1309170808Sdelphij error = 0; 1310170808Sdelphij 1311170808Sdelphijout_locked: 1312229855Sivoras if (fdvp != tdvp && fdvp != tvp) 1313175294Sattilio VOP_UNLOCK(fdvp, 0); 1314170808Sdelphij 1315170808Sdelphijout: 1316170808Sdelphij /* Release target nodes. */ 1317170808Sdelphij /* XXX: I don't understand when tdvp can be the same as tvp, but 1318170808Sdelphij * other code takes care of this... */ 1319170808Sdelphij if (tdvp == tvp) 1320170808Sdelphij vrele(tdvp); 1321170808Sdelphij else 1322170808Sdelphij vput(tdvp); 1323170808Sdelphij if (tvp != NULL) 1324170808Sdelphij vput(tvp); 1325170808Sdelphij 1326170808Sdelphij /* Release source nodes. */ 1327170808Sdelphij vrele(fdvp); 1328170808Sdelphij vrele(fvp); 1329170808Sdelphij 1330233851Sgleb if (mp != NULL) 1331233851Sgleb vfs_unbusy(mp); 1332233851Sgleb 1333170808Sdelphij return error; 1334170808Sdelphij} 1335170808Sdelphij 1336170808Sdelphij/* --------------------------------------------------------------------- */ 1337170808Sdelphij 1338171069Sdelphijstatic int 1339170808Sdelphijtmpfs_mkdir(struct vop_mkdir_args *v) 1340170808Sdelphij{ 1341170808Sdelphij struct vnode *dvp = v->a_dvp; 1342170808Sdelphij struct vnode **vpp = v->a_vpp; 1343170808Sdelphij struct componentname *cnp = v->a_cnp; 1344170808Sdelphij struct vattr *vap = v->a_vap; 1345170808Sdelphij 1346170808Sdelphij MPASS(vap->va_type == VDIR); 1347170808Sdelphij 1348170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL); 1349170808Sdelphij} 1350170808Sdelphij 1351170808Sdelphij/* --------------------------------------------------------------------- */ 1352170808Sdelphij 1353171069Sdelphijstatic int 1354170808Sdelphijtmpfs_rmdir(struct vop_rmdir_args *v) 1355170808Sdelphij{ 1356170808Sdelphij struct vnode *dvp = v->a_dvp; 1357170808Sdelphij struct vnode *vp = v->a_vp; 1358170808Sdelphij 1359170808Sdelphij int error; 1360170808Sdelphij struct tmpfs_dirent *de; 1361170808Sdelphij struct tmpfs_mount *tmp; 1362170808Sdelphij struct tmpfs_node *dnode; 1363170808Sdelphij struct tmpfs_node *node; 1364170808Sdelphij 1365176559Sattilio MPASS(VOP_ISLOCKED(dvp)); 1366176559Sattilio MPASS(VOP_ISLOCKED(vp)); 1367170808Sdelphij 1368170808Sdelphij tmp = VFS_TO_TMPFS(dvp->v_mount); 1369170808Sdelphij dnode = VP_TO_TMPFS_DIR(dvp); 1370170808Sdelphij node = VP_TO_TMPFS_DIR(vp); 1371170808Sdelphij 1372171070Sdelphij /* Directories with more than two entries ('.' and '..') cannot be 1373171070Sdelphij * removed. */ 1374171070Sdelphij if (node->tn_size > 0) { 1375171070Sdelphij error = ENOTEMPTY; 1376171070Sdelphij goto out; 1377171070Sdelphij } 1378170808Sdelphij 1379170808Sdelphij if ((dnode->tn_flags & APPEND) 1380170808Sdelphij || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1381170808Sdelphij error = EPERM; 1382170808Sdelphij goto out; 1383170808Sdelphij } 1384170808Sdelphij 1385171070Sdelphij /* This invariant holds only if we are not trying to remove "..". 1386171070Sdelphij * We checked for that above so this is safe now. */ 1387170808Sdelphij MPASS(node->tn_dir.tn_parent == dnode); 1388170808Sdelphij 1389170808Sdelphij /* Get the directory entry associated with node (vp). This was 1390170808Sdelphij * filled by tmpfs_lookup while looking up the entry. */ 1391188318Skib de = tmpfs_dir_lookup(dnode, node, v->a_cnp); 1392170808Sdelphij MPASS(TMPFS_DIRENT_MATCHES(de, 1393170808Sdelphij v->a_cnp->cn_nameptr, 1394170808Sdelphij v->a_cnp->cn_namelen)); 1395170808Sdelphij 1396170808Sdelphij /* Check flags to see if we are allowed to remove the directory. */ 1397170808Sdelphij if (dnode->tn_flags & APPEND 1398170808Sdelphij || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) { 1399170808Sdelphij error = EPERM; 1400170808Sdelphij goto out; 1401170808Sdelphij } 1402170808Sdelphij 1403197953Sdelphij 1404170808Sdelphij /* Detach the directory entry from the directory (dnode). */ 1405170808Sdelphij tmpfs_dir_detach(dvp, de); 1406211598Sed if (v->a_cnp->cn_flags & DOWHITEOUT) 1407211598Sed tmpfs_dir_whiteout_add(dvp, v->a_cnp); 1408170808Sdelphij 1409197953Sdelphij /* No vnode should be allocated for this entry from this point */ 1410197953Sdelphij TMPFS_NODE_LOCK(node); 1411197953Sdelphij TMPFS_ASSERT_ELOCKED(node); 1412170808Sdelphij node->tn_links--; 1413197953Sdelphij node->tn_dir.tn_parent = NULL; 1414170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 1415170808Sdelphij TMPFS_NODE_MODIFIED; 1416197953Sdelphij 1417197953Sdelphij TMPFS_NODE_UNLOCK(node); 1418197953Sdelphij 1419197953Sdelphij TMPFS_NODE_LOCK(dnode); 1420197953Sdelphij TMPFS_ASSERT_ELOCKED(dnode); 1421197953Sdelphij dnode->tn_links--; 1422197953Sdelphij dnode->tn_status |= TMPFS_NODE_ACCESSED | \ 1423170808Sdelphij TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1424197953Sdelphij TMPFS_NODE_UNLOCK(dnode); 1425170808Sdelphij 1426171070Sdelphij cache_purge(dvp); 1427170808Sdelphij cache_purge(vp); 1428170808Sdelphij 1429170808Sdelphij /* Free the directory entry we just deleted. Note that the node 1430170808Sdelphij * referred by it will not be removed until the vnode is really 1431170808Sdelphij * reclaimed. */ 1432170808Sdelphij tmpfs_free_dirent(tmp, de, TRUE); 1433170808Sdelphij 1434170808Sdelphij /* Release the deleted vnode (will destroy the node, notify 1435170808Sdelphij * interested parties and clean it from the cache). */ 1436170808Sdelphij 1437170808Sdelphij dnode->tn_status |= TMPFS_NODE_CHANGED; 1438170808Sdelphij tmpfs_update(dvp); 1439170808Sdelphij 1440170808Sdelphij error = 0; 1441170808Sdelphij 1442170808Sdelphijout: 1443170808Sdelphij return error; 1444170808Sdelphij} 1445170808Sdelphij 1446170808Sdelphij/* --------------------------------------------------------------------- */ 1447170808Sdelphij 1448171069Sdelphijstatic int 1449170808Sdelphijtmpfs_symlink(struct vop_symlink_args *v) 1450170808Sdelphij{ 1451170808Sdelphij struct vnode *dvp = v->a_dvp; 1452170808Sdelphij struct vnode **vpp = v->a_vpp; 1453170808Sdelphij struct componentname *cnp = v->a_cnp; 1454170808Sdelphij struct vattr *vap = v->a_vap; 1455170808Sdelphij char *target = v->a_target; 1456170808Sdelphij 1457170808Sdelphij#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */ 1458170808Sdelphij MPASS(vap->va_type == VLNK); 1459170808Sdelphij#else 1460170808Sdelphij vap->va_type = VLNK; 1461170808Sdelphij#endif 1462170808Sdelphij 1463170808Sdelphij return tmpfs_alloc_file(dvp, vpp, vap, cnp, target); 1464170808Sdelphij} 1465170808Sdelphij 1466170808Sdelphij/* --------------------------------------------------------------------- */ 1467170808Sdelphij 1468171069Sdelphijstatic int 1469170808Sdelphijtmpfs_readdir(struct vop_readdir_args *v) 1470170808Sdelphij{ 1471170808Sdelphij struct vnode *vp = v->a_vp; 1472170808Sdelphij struct uio *uio = v->a_uio; 1473170808Sdelphij int *eofflag = v->a_eofflag; 1474170808Sdelphij u_long **cookies = v->a_cookies; 1475170808Sdelphij int *ncookies = v->a_ncookies; 1476170808Sdelphij 1477170808Sdelphij int error; 1478170808Sdelphij off_t startoff; 1479171802Sdelphij off_t cnt = 0; 1480170808Sdelphij struct tmpfs_node *node; 1481170808Sdelphij 1482170808Sdelphij /* This operation only makes sense on directory nodes. */ 1483171802Sdelphij if (vp->v_type != VDIR) 1484171802Sdelphij return ENOTDIR; 1485170808Sdelphij 1486170808Sdelphij node = VP_TO_TMPFS_DIR(vp); 1487170808Sdelphij 1488170808Sdelphij startoff = uio->uio_offset; 1489170808Sdelphij 1490171862Sdelphij if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) { 1491170808Sdelphij error = tmpfs_dir_getdotdent(node, uio); 1492171862Sdelphij if (error != 0) 1493171862Sdelphij goto outok; 1494171862Sdelphij cnt++; 1495171862Sdelphij } 1496171862Sdelphij 1497171862Sdelphij if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) { 1498170808Sdelphij error = tmpfs_dir_getdotdotdent(node, uio); 1499171862Sdelphij if (error != 0) 1500171862Sdelphij goto outok; 1501171862Sdelphij cnt++; 1502170808Sdelphij } 1503170808Sdelphij 1504171862Sdelphij error = tmpfs_dir_getdents(node, uio, &cnt); 1505171862Sdelphij 1506171862Sdelphijoutok: 1507171862Sdelphij MPASS(error >= -1); 1508171862Sdelphij 1509170808Sdelphij if (error == -1) 1510217633Skib error = (cnt != 0) ? 0 : EINVAL; 1511170808Sdelphij 1512170808Sdelphij if (eofflag != NULL) 1513170808Sdelphij *eofflag = 1514170808Sdelphij (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF); 1515170808Sdelphij 1516170808Sdelphij /* Update NFS-related variables. */ 1517170808Sdelphij if (error == 0 && cookies != NULL && ncookies != NULL) { 1518170808Sdelphij off_t i; 1519170808Sdelphij off_t off = startoff; 1520170808Sdelphij struct tmpfs_dirent *de = NULL; 1521170808Sdelphij 1522170808Sdelphij *ncookies = cnt; 1523170808Sdelphij *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK); 1524170808Sdelphij 1525170808Sdelphij for (i = 0; i < cnt; i++) { 1526170808Sdelphij MPASS(off != TMPFS_DIRCOOKIE_EOF); 1527170808Sdelphij if (off == TMPFS_DIRCOOKIE_DOT) { 1528170808Sdelphij off = TMPFS_DIRCOOKIE_DOTDOT; 1529170808Sdelphij } else { 1530170808Sdelphij if (off == TMPFS_DIRCOOKIE_DOTDOT) { 1531170808Sdelphij de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 1532170808Sdelphij } else if (de != NULL) { 1533170808Sdelphij de = TAILQ_NEXT(de, td_entries); 1534170808Sdelphij } else { 1535170808Sdelphij de = tmpfs_dir_lookupbycookie(node, 1536170808Sdelphij off); 1537170808Sdelphij MPASS(de != NULL); 1538170808Sdelphij de = TAILQ_NEXT(de, td_entries); 1539170808Sdelphij } 1540171802Sdelphij if (de == NULL) 1541170808Sdelphij off = TMPFS_DIRCOOKIE_EOF; 1542171802Sdelphij else 1543171802Sdelphij off = tmpfs_dircookie(de); 1544170808Sdelphij } 1545170808Sdelphij 1546170808Sdelphij (*cookies)[i] = off; 1547170808Sdelphij } 1548170808Sdelphij MPASS(uio->uio_offset == off); 1549170808Sdelphij } 1550170808Sdelphij 1551170808Sdelphij return error; 1552170808Sdelphij} 1553170808Sdelphij 1554170808Sdelphij/* --------------------------------------------------------------------- */ 1555170808Sdelphij 1556171069Sdelphijstatic int 1557170808Sdelphijtmpfs_readlink(struct vop_readlink_args *v) 1558170808Sdelphij{ 1559170808Sdelphij struct vnode *vp = v->a_vp; 1560170808Sdelphij struct uio *uio = v->a_uio; 1561170808Sdelphij 1562170808Sdelphij int error; 1563170808Sdelphij struct tmpfs_node *node; 1564170808Sdelphij 1565170808Sdelphij MPASS(uio->uio_offset == 0); 1566170808Sdelphij MPASS(vp->v_type == VLNK); 1567170808Sdelphij 1568170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1569170808Sdelphij 1570170808Sdelphij error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid), 1571170808Sdelphij uio); 1572170808Sdelphij node->tn_status |= TMPFS_NODE_ACCESSED; 1573170808Sdelphij 1574170808Sdelphij return error; 1575170808Sdelphij} 1576170808Sdelphij 1577170808Sdelphij/* --------------------------------------------------------------------- */ 1578170808Sdelphij 1579171069Sdelphijstatic int 1580170808Sdelphijtmpfs_inactive(struct vop_inactive_args *v) 1581170808Sdelphij{ 1582170808Sdelphij struct vnode *vp = v->a_vp; 1583170808Sdelphij struct thread *l = v->a_td; 1584170808Sdelphij 1585170808Sdelphij struct tmpfs_node *node; 1586170808Sdelphij 1587176559Sattilio MPASS(VOP_ISLOCKED(vp)); 1588170808Sdelphij 1589170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1590170808Sdelphij 1591170808Sdelphij if (node->tn_links == 0) 1592170808Sdelphij vrecycle(vp, l); 1593170808Sdelphij 1594170808Sdelphij return 0; 1595170808Sdelphij} 1596170808Sdelphij 1597170808Sdelphij/* --------------------------------------------------------------------- */ 1598170808Sdelphij 1599170808Sdelphijint 1600170808Sdelphijtmpfs_reclaim(struct vop_reclaim_args *v) 1601170808Sdelphij{ 1602170808Sdelphij struct vnode *vp = v->a_vp; 1603170808Sdelphij 1604170808Sdelphij struct tmpfs_mount *tmp; 1605170808Sdelphij struct tmpfs_node *node; 1606170808Sdelphij 1607170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1608170808Sdelphij tmp = VFS_TO_TMPFS(vp->v_mount); 1609171070Sdelphij 1610170808Sdelphij vnode_destroy_vobject(vp); 1611170808Sdelphij cache_purge(vp); 1612197953Sdelphij 1613197953Sdelphij TMPFS_NODE_LOCK(node); 1614197953Sdelphij TMPFS_ASSERT_ELOCKED(node); 1615170808Sdelphij tmpfs_free_vp(vp); 1616170808Sdelphij 1617170808Sdelphij /* If the node referenced by this vnode was deleted by the user, 1618170808Sdelphij * we must free its associated data structures (now that the vnode 1619170808Sdelphij * is being reclaimed). */ 1620197953Sdelphij if (node->tn_links == 0 && 1621197953Sdelphij (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) { 1622197953Sdelphij node->tn_vpstate = TMPFS_VNODE_DOOMED; 1623197953Sdelphij TMPFS_NODE_UNLOCK(node); 1624170808Sdelphij tmpfs_free_node(tmp, node); 1625197953Sdelphij } else 1626197953Sdelphij TMPFS_NODE_UNLOCK(node); 1627170808Sdelphij 1628170808Sdelphij MPASS(vp->v_data == NULL); 1629170808Sdelphij return 0; 1630170808Sdelphij} 1631170808Sdelphij 1632170808Sdelphij/* --------------------------------------------------------------------- */ 1633170808Sdelphij 1634171069Sdelphijstatic int 1635170808Sdelphijtmpfs_print(struct vop_print_args *v) 1636170808Sdelphij{ 1637170808Sdelphij struct vnode *vp = v->a_vp; 1638170808Sdelphij 1639170808Sdelphij struct tmpfs_node *node; 1640170808Sdelphij 1641170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 1642170808Sdelphij 1643170808Sdelphij printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n", 1644170808Sdelphij node, node->tn_flags, node->tn_links); 1645170808Sdelphij printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX 1646170808Sdelphij ", status 0x%x\n", 1647170808Sdelphij node->tn_mode, node->tn_uid, node->tn_gid, 1648170808Sdelphij (uintmax_t)node->tn_size, node->tn_status); 1649170808Sdelphij 1650170808Sdelphij if (vp->v_type == VFIFO) 1651170808Sdelphij fifo_printinfo(vp); 1652170808Sdelphij 1653170808Sdelphij printf("\n"); 1654170808Sdelphij 1655170808Sdelphij return 0; 1656170808Sdelphij} 1657170808Sdelphij 1658170808Sdelphij/* --------------------------------------------------------------------- */ 1659170808Sdelphij 1660171069Sdelphijstatic int 1661170808Sdelphijtmpfs_pathconf(struct vop_pathconf_args *v) 1662170808Sdelphij{ 1663170808Sdelphij int name = v->a_name; 1664170808Sdelphij register_t *retval = v->a_retval; 1665170808Sdelphij 1666170808Sdelphij int error; 1667170808Sdelphij 1668170808Sdelphij error = 0; 1669170808Sdelphij 1670170808Sdelphij switch (name) { 1671170808Sdelphij case _PC_LINK_MAX: 1672170808Sdelphij *retval = LINK_MAX; 1673170808Sdelphij break; 1674170808Sdelphij 1675170808Sdelphij case _PC_NAME_MAX: 1676170808Sdelphij *retval = NAME_MAX; 1677170808Sdelphij break; 1678170808Sdelphij 1679170808Sdelphij case _PC_PATH_MAX: 1680170808Sdelphij *retval = PATH_MAX; 1681170808Sdelphij break; 1682170808Sdelphij 1683170808Sdelphij case _PC_PIPE_BUF: 1684170808Sdelphij *retval = PIPE_BUF; 1685170808Sdelphij break; 1686170808Sdelphij 1687170808Sdelphij case _PC_CHOWN_RESTRICTED: 1688170808Sdelphij *retval = 1; 1689170808Sdelphij break; 1690170808Sdelphij 1691170808Sdelphij case _PC_NO_TRUNC: 1692170808Sdelphij *retval = 1; 1693170808Sdelphij break; 1694170808Sdelphij 1695170808Sdelphij case _PC_SYNC_IO: 1696170808Sdelphij *retval = 1; 1697170808Sdelphij break; 1698170808Sdelphij 1699170808Sdelphij case _PC_FILESIZEBITS: 1700170808Sdelphij *retval = 0; /* XXX Don't know which value should I return. */ 1701170808Sdelphij break; 1702170808Sdelphij 1703170808Sdelphij default: 1704170808Sdelphij error = EINVAL; 1705170808Sdelphij } 1706170808Sdelphij 1707170808Sdelphij return error; 1708170808Sdelphij} 1709170808Sdelphij 1710171069Sdelphijstatic int 1711170808Sdelphijtmpfs_vptofh(struct vop_vptofh_args *ap) 1712170808Sdelphij{ 1713170808Sdelphij struct tmpfs_fid *tfhp; 1714170808Sdelphij struct tmpfs_node *node; 1715170808Sdelphij 1716170808Sdelphij tfhp = (struct tmpfs_fid *)ap->a_fhp; 1717170808Sdelphij node = VP_TO_TMPFS_NODE(ap->a_vp); 1718170808Sdelphij 1719170808Sdelphij tfhp->tf_len = sizeof(struct tmpfs_fid); 1720170808Sdelphij tfhp->tf_id = node->tn_id; 1721170808Sdelphij tfhp->tf_gen = node->tn_gen; 1722171070Sdelphij 1723170808Sdelphij return (0); 1724170808Sdelphij} 1725171069Sdelphij 1726211598Sedstatic int 1727211598Sedtmpfs_whiteout(struct vop_whiteout_args *ap) 1728211598Sed{ 1729211598Sed struct vnode *dvp = ap->a_dvp; 1730211598Sed struct componentname *cnp = ap->a_cnp; 1731211598Sed struct tmpfs_dirent *de; 1732211598Sed 1733211598Sed switch (ap->a_flags) { 1734211598Sed case LOOKUP: 1735211598Sed return (0); 1736211598Sed case CREATE: 1737211598Sed de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp); 1738211598Sed if (de != NULL) 1739211598Sed return (de->td_node == NULL ? 0 : EEXIST); 1740211598Sed return (tmpfs_dir_whiteout_add(dvp, cnp)); 1741211598Sed case DELETE: 1742211598Sed tmpfs_dir_whiteout_remove(dvp, cnp); 1743211598Sed return (0); 1744211598Sed default: 1745211598Sed panic("tmpfs_whiteout: unknown op"); 1746211598Sed } 1747211598Sed} 1748211598Sed 1749171069Sdelphij/* --------------------------------------------------------------------- */ 1750171069Sdelphij 1751171069Sdelphij/* 1752171069Sdelphij * vnode operations vector used for files stored in a tmpfs file system. 1753171069Sdelphij */ 1754171069Sdelphijstruct vop_vector tmpfs_vnodeop_entries = { 1755171069Sdelphij .vop_default = &default_vnodeops, 1756171069Sdelphij .vop_lookup = vfs_cache_lookup, 1757171069Sdelphij .vop_cachedlookup = tmpfs_lookup, 1758171069Sdelphij .vop_create = tmpfs_create, 1759171069Sdelphij .vop_mknod = tmpfs_mknod, 1760171069Sdelphij .vop_open = tmpfs_open, 1761171069Sdelphij .vop_close = tmpfs_close, 1762171069Sdelphij .vop_access = tmpfs_access, 1763171069Sdelphij .vop_getattr = tmpfs_getattr, 1764171069Sdelphij .vop_setattr = tmpfs_setattr, 1765171069Sdelphij .vop_read = tmpfs_read, 1766171069Sdelphij .vop_write = tmpfs_write, 1767171069Sdelphij .vop_fsync = tmpfs_fsync, 1768171069Sdelphij .vop_remove = tmpfs_remove, 1769171069Sdelphij .vop_link = tmpfs_link, 1770171069Sdelphij .vop_rename = tmpfs_rename, 1771171069Sdelphij .vop_mkdir = tmpfs_mkdir, 1772171069Sdelphij .vop_rmdir = tmpfs_rmdir, 1773171069Sdelphij .vop_symlink = tmpfs_symlink, 1774171069Sdelphij .vop_readdir = tmpfs_readdir, 1775171069Sdelphij .vop_readlink = tmpfs_readlink, 1776171069Sdelphij .vop_inactive = tmpfs_inactive, 1777171069Sdelphij .vop_reclaim = tmpfs_reclaim, 1778171069Sdelphij .vop_print = tmpfs_print, 1779171069Sdelphij .vop_pathconf = tmpfs_pathconf, 1780171069Sdelphij .vop_vptofh = tmpfs_vptofh, 1781211598Sed .vop_whiteout = tmpfs_whiteout, 1782171069Sdelphij .vop_bmap = VOP_EOPNOTSUPP, 1783171069Sdelphij}; 1784171069Sdelphij 1785