Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.93.4.5
      1  1.93.4.5       mrg /*	$NetBSD: tmpfs_vnops.c,v 1.93.4.5 2012/04/05 21:33:38 mrg Exp $	*/
      2       1.1      jmmv 
      3       1.1      jmmv /*
      4      1.45        ad  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
      5       1.1      jmmv  * All rights reserved.
      6       1.1      jmmv  *
      7       1.1      jmmv  * This code is derived from software contributed to The NetBSD Foundation
      8      1.12      jmmv  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
      9      1.12      jmmv  * 2005 program.
     10       1.1      jmmv  *
     11       1.1      jmmv  * Redistribution and use in source and binary forms, with or without
     12       1.1      jmmv  * modification, are permitted provided that the following conditions
     13       1.1      jmmv  * are met:
     14       1.1      jmmv  * 1. Redistributions of source code must retain the above copyright
     15       1.1      jmmv  *    notice, this list of conditions and the following disclaimer.
     16       1.1      jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1      jmmv  *    notice, this list of conditions and the following disclaimer in the
     18       1.1      jmmv  *    documentation and/or other materials provided with the distribution.
     19       1.1      jmmv  *
     20       1.1      jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1      jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1      jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1      jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1      jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1      jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1      jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1      jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1      jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1      jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1      jmmv  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1      jmmv  */
     32       1.1      jmmv 
     33       1.1      jmmv /*
     34       1.1      jmmv  * tmpfs vnode interface.
     35       1.1      jmmv  */
     36       1.1      jmmv 
     37       1.1      jmmv #include <sys/cdefs.h>
     38  1.93.4.5       mrg __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.93.4.5 2012/04/05 21:33:38 mrg Exp $");
     39       1.1      jmmv 
     40       1.1      jmmv #include <sys/param.h>
     41       1.1      jmmv #include <sys/dirent.h>
     42       1.1      jmmv #include <sys/fcntl.h>
     43       1.1      jmmv #include <sys/event.h>
     44       1.1      jmmv #include <sys/malloc.h>
     45       1.1      jmmv #include <sys/namei.h>
     46       1.1      jmmv #include <sys/stat.h>
     47       1.1      jmmv #include <sys/uio.h>
     48       1.1      jmmv #include <sys/unistd.h>
     49       1.1      jmmv #include <sys/vnode.h>
     50      1.15      jmmv #include <sys/lockf.h>
     51      1.24  christos #include <sys/kauth.h>
     52       1.1      jmmv 
     53       1.1      jmmv #include <uvm/uvm.h>
     54       1.1      jmmv 
     55       1.1      jmmv #include <miscfs/fifofs/fifo.h>
     56      1.60      elad #include <miscfs/genfs/genfs.h>
     57       1.1      jmmv #include <fs/tmpfs/tmpfs_vnops.h>
     58       1.1      jmmv #include <fs/tmpfs/tmpfs.h>
     59       1.1      jmmv 
     60       1.1      jmmv /*
     61       1.2      jmmv  * vnode operations vector used for files stored in a tmpfs file system.
     62       1.1      jmmv  */
     63       1.1      jmmv int (**tmpfs_vnodeop_p)(void *);
     64       1.1      jmmv const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
     65       1.1      jmmv 	{ &vop_default_desc,		vn_default_error },
     66       1.1      jmmv 	{ &vop_lookup_desc,		tmpfs_lookup },
     67       1.1      jmmv 	{ &vop_create_desc,		tmpfs_create },
     68       1.1      jmmv 	{ &vop_mknod_desc,		tmpfs_mknod },
     69       1.1      jmmv 	{ &vop_open_desc,		tmpfs_open },
     70       1.1      jmmv 	{ &vop_close_desc,		tmpfs_close },
     71       1.1      jmmv 	{ &vop_access_desc,		tmpfs_access },
     72       1.1      jmmv 	{ &vop_getattr_desc,		tmpfs_getattr },
     73       1.1      jmmv 	{ &vop_setattr_desc,		tmpfs_setattr },
     74       1.1      jmmv 	{ &vop_read_desc,		tmpfs_read },
     75       1.1      jmmv 	{ &vop_write_desc,		tmpfs_write },
     76       1.1      jmmv 	{ &vop_ioctl_desc,		tmpfs_ioctl },
     77       1.1      jmmv 	{ &vop_fcntl_desc,		tmpfs_fcntl },
     78       1.1      jmmv 	{ &vop_poll_desc,		tmpfs_poll },
     79       1.1      jmmv 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
     80       1.1      jmmv 	{ &vop_revoke_desc,		tmpfs_revoke },
     81       1.1      jmmv 	{ &vop_mmap_desc,		tmpfs_mmap },
     82       1.1      jmmv 	{ &vop_fsync_desc,		tmpfs_fsync },
     83       1.1      jmmv 	{ &vop_seek_desc,		tmpfs_seek },
     84       1.1      jmmv 	{ &vop_remove_desc,		tmpfs_remove },
     85       1.1      jmmv 	{ &vop_link_desc,		tmpfs_link },
     86       1.1      jmmv 	{ &vop_rename_desc,		tmpfs_rename },
     87       1.1      jmmv 	{ &vop_mkdir_desc,		tmpfs_mkdir },
     88       1.1      jmmv 	{ &vop_rmdir_desc,		tmpfs_rmdir },
     89       1.1      jmmv 	{ &vop_symlink_desc,		tmpfs_symlink },
     90       1.1      jmmv 	{ &vop_readdir_desc,		tmpfs_readdir },
     91       1.1      jmmv 	{ &vop_readlink_desc,		tmpfs_readlink },
     92       1.1      jmmv 	{ &vop_abortop_desc,		tmpfs_abortop },
     93       1.1      jmmv 	{ &vop_inactive_desc,		tmpfs_inactive },
     94       1.1      jmmv 	{ &vop_reclaim_desc,		tmpfs_reclaim },
     95       1.1      jmmv 	{ &vop_lock_desc,		tmpfs_lock },
     96       1.1      jmmv 	{ &vop_unlock_desc,		tmpfs_unlock },
     97       1.1      jmmv 	{ &vop_bmap_desc,		tmpfs_bmap },
     98       1.1      jmmv 	{ &vop_strategy_desc,		tmpfs_strategy },
     99       1.1      jmmv 	{ &vop_print_desc,		tmpfs_print },
    100       1.1      jmmv 	{ &vop_pathconf_desc,		tmpfs_pathconf },
    101       1.1      jmmv 	{ &vop_islocked_desc,		tmpfs_islocked },
    102       1.1      jmmv 	{ &vop_advlock_desc,		tmpfs_advlock },
    103       1.1      jmmv 	{ &vop_bwrite_desc,		tmpfs_bwrite },
    104       1.1      jmmv 	{ &vop_getpages_desc,		tmpfs_getpages },
    105       1.1      jmmv 	{ &vop_putpages_desc,		tmpfs_putpages },
    106      1.76     pooka 	{ &vop_whiteout_desc,		tmpfs_whiteout },
    107       1.1      jmmv 	{ NULL, NULL }
    108       1.1      jmmv };
    109      1.83     rmind 
    110      1.83     rmind const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
    111      1.83     rmind 	&tmpfs_vnodeop_p, tmpfs_vnodeop_entries
    112      1.83     rmind };
    113       1.1      jmmv 
    114      1.72     rmind /*
    115      1.82     rmind  * tmpfs_lookup: path name traversal routine.
    116      1.72     rmind  *
    117      1.72     rmind  * Arguments: dvp (directory being searched), vpp (result),
    118      1.72     rmind  * cnp (component name - path).
    119      1.72     rmind  *
    120      1.72     rmind  * => Caller holds a reference and lock on dvp.
    121      1.72     rmind  * => We return looked-up vnode (vpp) locked, with a reference held.
    122      1.72     rmind  */
    123       1.1      jmmv int
    124       1.1      jmmv tmpfs_lookup(void *v)
    125       1.1      jmmv {
    126      1.72     rmind 	struct vop_lookup_args /* {
    127      1.72     rmind 		struct vnode *a_dvp;
    128      1.72     rmind 		struct vnode **a_vpp;
    129      1.72     rmind 		struct componentname *a_cnp;
    130      1.72     rmind 	} */ *ap = v;
    131      1.83     rmind 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    132      1.72     rmind 	struct componentname *cnp = ap->a_cnp;
    133      1.84     rmind 	const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
    134      1.84     rmind 	tmpfs_node_t *dnode, *tnode;
    135      1.83     rmind 	tmpfs_dirent_t *de;
    136      1.72     rmind 	int error;
    137       1.1      jmmv 
    138       1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
    139       1.1      jmmv 
    140       1.1      jmmv 	dnode = VP_TO_TMPFS_DIR(dvp);
    141       1.1      jmmv 	*vpp = NULL;
    142       1.1      jmmv 
    143      1.85     rmind 	/* Check accessibility of directory. */
    144      1.44     pooka 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    145      1.83     rmind 	if (error) {
    146       1.1      jmmv 		goto out;
    147      1.83     rmind 	}
    148      1.85     rmind 
    149      1.72     rmind 	/*
    150      1.72     rmind 	 * If requesting the last path component on a read-only file system
    151      1.72     rmind 	 * with a write operation, deny it.
    152      1.72     rmind 	 */
    153      1.84     rmind 	if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
    154       1.1      jmmv 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    155       1.1      jmmv 		error = EROFS;
    156       1.1      jmmv 		goto out;
    157       1.1      jmmv 	}
    158       1.1      jmmv 
    159      1.72     rmind 	/*
    160      1.72     rmind 	 * Avoid doing a linear scan of the directory if the requested
    161      1.72     rmind 	 * directory/name couple is already in the cache.
    162      1.72     rmind 	 */
    163       1.1      jmmv 	error = cache_lookup(dvp, vpp, cnp);
    164      1.82     rmind 	if (error >= 0) {
    165      1.82     rmind 		/* Both cache-hit or an error case. */
    166       1.1      jmmv 		goto out;
    167      1.82     rmind 	}
    168       1.1      jmmv 
    169       1.1      jmmv 	if (cnp->cn_flags & ISDOTDOT) {
    170      1.83     rmind 		tmpfs_node_t *pnode;
    171      1.85     rmind 
    172      1.82     rmind 		/*
    173      1.82     rmind 		 * Lookup of ".." case.
    174      1.82     rmind 		 */
    175      1.85     rmind 		if (lastcn && cnp->cn_nameiop == RENAME) {
    176      1.85     rmind 			error = EINVAL;
    177      1.85     rmind 			goto out;
    178      1.85     rmind 		}
    179      1.85     rmind 		KASSERT(dnode->tn_type == VDIR);
    180      1.82     rmind 		pnode = dnode->tn_spec.tn_dir.tn_parent;
    181      1.85     rmind 		if (pnode == NULL) {
    182      1.85     rmind 			error = ENOENT;
    183      1.85     rmind 			goto out;
    184      1.85     rmind 		}
    185      1.85     rmind 
    186      1.85     rmind 		/*
    187      1.85     rmind 		 * Lock the parent tn_vlock before releasing the vnode lock,
    188      1.85     rmind 		 * and thus prevents parent from disappearing.
    189      1.85     rmind 		 */
    190      1.85     rmind 		mutex_enter(&pnode->tn_vlock);
    191      1.71   hannken 		VOP_UNLOCK(dvp);
    192       1.1      jmmv 
    193      1.85     rmind 		/*
    194      1.85     rmind 		 * Get a vnode of the '..' entry and re-acquire the lock.
    195      1.85     rmind 		 * Release the tn_vlock.
    196      1.85     rmind 		 */
    197      1.85     rmind 		error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
    198      1.33       chs 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    199      1.82     rmind 		goto out;
    200      1.72     rmind 
    201       1.1      jmmv 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    202      1.82     rmind 		/*
    203      1.82     rmind 		 * Lookup of "." case.
    204      1.82     rmind 		 */
    205      1.84     rmind 		if (lastcn && cnp->cn_nameiop == RENAME) {
    206      1.73     pooka 			error = EISDIR;
    207      1.73     pooka 			goto out;
    208      1.73     pooka 		}
    209      1.66     pooka 		vref(dvp);
    210       1.1      jmmv 		*vpp = dvp;
    211       1.1      jmmv 		error = 0;
    212      1.72     rmind 		goto done;
    213      1.72     rmind 	}
    214       1.1      jmmv 
    215      1.82     rmind 	/*
    216      1.82     rmind 	 * Other lookup cases: perform directory scan.
    217      1.82     rmind 	 */
    218      1.72     rmind 	de = tmpfs_dir_lookup(dnode, cnp);
    219      1.76     pooka 	if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
    220      1.72     rmind 		/*
    221      1.72     rmind 		 * The entry was not found in the directory.  This is valid
    222      1.72     rmind 		 * if we are creating or renaming an entry and are working
    223      1.72     rmind 		 * on the last component of the path name.
    224      1.72     rmind 		 */
    225      1.84     rmind 		if (lastcn && (cnp->cn_nameiop == CREATE ||
    226      1.72     rmind 		    cnp->cn_nameiop == RENAME)) {
    227      1.72     rmind 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    228      1.72     rmind 			if (error) {
    229       1.1      jmmv 				goto out;
    230       1.1      jmmv 			}
    231      1.72     rmind 			error = EJUSTRETURN;
    232      1.72     rmind 		} else {
    233      1.72     rmind 			error = ENOENT;
    234      1.72     rmind 		}
    235      1.76     pooka 		if (de) {
    236      1.76     pooka 			KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
    237      1.76     pooka 			cnp->cn_flags |= ISWHITEOUT;
    238      1.76     pooka 		}
    239      1.84     rmind 		goto done;
    240      1.84     rmind 	}
    241       1.1      jmmv 
    242      1.84     rmind 	tnode = de->td_node;
    243      1.62      elad 
    244      1.84     rmind 	/*
    245      1.84     rmind 	 * If it is not the last path component and found a non-directory
    246      1.84     rmind 	 * or non-link entry (which may itself be pointing to a directory),
    247      1.84     rmind 	 * raise an error.
    248      1.84     rmind 	 */
    249      1.84     rmind 	if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
    250      1.84     rmind 		error = ENOTDIR;
    251      1.84     rmind 		goto out;
    252      1.84     rmind 	}
    253      1.72     rmind 
    254      1.84     rmind 	/* Check the permissions. */
    255      1.84     rmind 	if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    256  1.93.4.5       mrg 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    257  1.93.4.5       mrg 		if (error)
    258      1.84     rmind 			goto out;
    259  1.93.4.5       mrg 
    260  1.93.4.5       mrg 		if ((dnode->tn_mode & S_ISTXT) != 0) {
    261  1.93.4.5       mrg 			error = kauth_authorize_vnode(cnp->cn_cred,
    262  1.93.4.5       mrg 			    KAUTH_VNODE_DELETE, tnode->tn_vnode,
    263  1.93.4.5       mrg 			    dnode->tn_vnode, genfs_can_sticky(cnp->cn_cred,
    264  1.93.4.5       mrg 			    dnode->tn_uid, tnode->tn_uid));
    265  1.93.4.5       mrg 			if (error) {
    266  1.93.4.5       mrg 				error = EPERM;
    267  1.93.4.5       mrg 				goto out;
    268  1.93.4.5       mrg 			}
    269       1.1      jmmv 		}
    270       1.1      jmmv 	}
    271      1.84     rmind 
    272      1.85     rmind 	/* Get a vnode for the matching entry. */
    273      1.85     rmind 	mutex_enter(&tnode->tn_vlock);
    274      1.85     rmind 	error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
    275      1.72     rmind done:
    276      1.72     rmind 	/*
    277      1.82     rmind 	 * Cache the result, unless request was for creation (as it does
    278      1.82     rmind 	 * not improve the performance).
    279      1.72     rmind 	 */
    280      1.82     rmind 	if ((cnp->cn_flags & MAKEENTRY) != 0 && cnp->cn_nameiop != CREATE) {
    281       1.1      jmmv 		cache_enter(dvp, *vpp, cnp);
    282      1.82     rmind 	}
    283       1.1      jmmv out:
    284      1.83     rmind 	KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
    285      1.33       chs 	KASSERT(VOP_ISLOCKED(dvp));
    286      1.76     pooka 
    287       1.1      jmmv 	return error;
    288       1.1      jmmv }
    289       1.1      jmmv 
    290       1.1      jmmv int
    291       1.1      jmmv tmpfs_create(void *v)
    292       1.1      jmmv {
    293      1.83     rmind 	struct vop_create_args /* {
    294      1.83     rmind 		struct vnode		*a_dvp;
    295      1.83     rmind 		struct vnode		**a_vpp;
    296      1.83     rmind 		struct componentname	*a_cnp;
    297      1.83     rmind 		struct vattr		*a_vap;
    298      1.83     rmind 	} */ *ap = v;
    299      1.83     rmind 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    300      1.83     rmind 	struct componentname *cnp = ap->a_cnp;
    301      1.83     rmind 	struct vattr *vap = ap->a_vap;
    302       1.1      jmmv 
    303      1.83     rmind 	KASSERT(VOP_ISLOCKED(dvp));
    304       1.1      jmmv 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    305       1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    306       1.1      jmmv }
    307       1.1      jmmv 
    308       1.1      jmmv int
    309       1.1      jmmv tmpfs_mknod(void *v)
    310       1.1      jmmv {
    311      1.83     rmind 	struct vop_mknod_args /* {
    312      1.83     rmind 		struct vnode		*a_dvp;
    313      1.83     rmind 		struct vnode		**a_vpp;
    314      1.83     rmind 		struct componentname	*a_cnp;
    315      1.83     rmind 		struct vattr		*a_vap;
    316      1.83     rmind 	} */ *ap = v;
    317      1.83     rmind 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    318      1.83     rmind 	struct componentname *cnp = ap->a_cnp;
    319      1.83     rmind 	struct vattr *vap = ap->a_vap;
    320      1.83     rmind 	enum vtype vt = vap->va_type;
    321       1.1      jmmv 
    322      1.83     rmind 	if (vt != VBLK && vt != VCHR && vt != VFIFO) {
    323      1.54     pooka 		vput(dvp);
    324       1.1      jmmv 		return EINVAL;
    325      1.54     pooka 	}
    326       1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    327       1.1      jmmv }
    328       1.1      jmmv 
    329       1.1      jmmv int
    330       1.1      jmmv tmpfs_open(void *v)
    331       1.1      jmmv {
    332      1.83     rmind 	struct vop_open_args /* {
    333      1.83     rmind 		struct vnode	*a_vp;
    334      1.83     rmind 		int		a_mode;
    335      1.83     rmind 		kauth_cred_t	a_cred;
    336      1.83     rmind 	} */ *ap = v;
    337      1.83     rmind 	vnode_t *vp = ap->a_vp;
    338      1.83     rmind 	mode_t mode = ap->a_mode;
    339      1.83     rmind 	tmpfs_node_t *node;
    340       1.1      jmmv 
    341       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    342       1.1      jmmv 
    343       1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    344      1.32      jmmv 	if (node->tn_links < 1) {
    345      1.83     rmind 		/*
    346      1.83     rmind 		 * The file is still active, but all its names have been
    347      1.83     rmind 		 * removed (e.g. by a "rmdir $(pwd)").  It cannot be opened
    348      1.83     rmind 		 * any more, as it is about to be destroyed.
    349      1.83     rmind 		 */
    350      1.83     rmind 		return ENOENT;
    351      1.32      jmmv 	}
    352      1.32      jmmv 
    353       1.1      jmmv 	/* If the file is marked append-only, deny write requests. */
    354      1.83     rmind 	if ((node->tn_flags & APPEND) != 0 &&
    355      1.83     rmind 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
    356      1.83     rmind 		return EPERM;
    357      1.83     rmind 	}
    358      1.83     rmind 	return 0;
    359       1.1      jmmv }
    360       1.1      jmmv 
    361       1.1      jmmv int
    362       1.1      jmmv tmpfs_close(void *v)
    363       1.1      jmmv {
    364      1.83     rmind 	struct vop_close_args /* {
    365      1.83     rmind 		struct vnode	*a_vp;
    366      1.83     rmind 		int		a_fflag;
    367      1.83     rmind 		kauth_cred_t	a_cred;
    368      1.83     rmind 	} */ *ap = v;
    369      1.83     rmind 	vnode_t *vp = ap->a_vp;
    370       1.1      jmmv 
    371       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    372       1.1      jmmv 
    373      1.85     rmind 	tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    374      1.17      yamt 	return 0;
    375       1.1      jmmv }
    376       1.1      jmmv 
    377  1.93.4.1       mrg int
    378  1.93.4.1       mrg tmpfs_access(void *v)
    379       1.1      jmmv {
    380  1.93.4.1       mrg 	struct vop_access_args /* {
    381  1.93.4.1       mrg 		struct vnode	*a_vp;
    382  1.93.4.1       mrg 		int		a_mode;
    383  1.93.4.1       mrg 		kauth_cred_t	a_cred;
    384  1.93.4.1       mrg 	} */ *ap = v;
    385  1.93.4.1       mrg 	vnode_t *vp = ap->a_vp;
    386  1.93.4.1       mrg 	mode_t mode = ap->a_mode;
    387  1.93.4.1       mrg 	kauth_cred_t cred = ap->a_cred;
    388  1.93.4.1       mrg 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    389      1.83     rmind 	const bool writing = (mode & VWRITE) != 0;
    390  1.93.4.1       mrg 
    391  1.93.4.1       mrg 	KASSERT(VOP_ISLOCKED(vp));
    392       1.1      jmmv 
    393  1.93.4.1       mrg 	/* Possible? */
    394       1.1      jmmv 	switch (vp->v_type) {
    395       1.1      jmmv 	case VDIR:
    396       1.1      jmmv 	case VLNK:
    397       1.1      jmmv 	case VREG:
    398      1.83     rmind 		if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
    399      1.83     rmind 			return EROFS;
    400       1.1      jmmv 		}
    401       1.1      jmmv 		break;
    402       1.1      jmmv 	case VBLK:
    403       1.1      jmmv 	case VCHR:
    404       1.1      jmmv 	case VSOCK:
    405       1.1      jmmv 	case VFIFO:
    406       1.1      jmmv 		break;
    407       1.1      jmmv 	default:
    408      1.83     rmind 		return EINVAL;
    409       1.1      jmmv 	}
    410  1.93.4.1       mrg 	if (writing && (node->tn_flags & IMMUTABLE) != 0) {
    411  1.93.4.1       mrg 		return EPERM;
    412  1.93.4.1       mrg 	}
    413      1.61      elad 
    414  1.93.4.5       mrg 	return kauth_authorize_vnode(cred, kauth_access_action(mode,
    415  1.93.4.5       mrg 	    vp->v_type, node->tn_mode), vp, NULL, genfs_can_access(vp->v_type,
    416  1.93.4.5       mrg 	    node->tn_mode, node->tn_uid, node->tn_gid, mode, cred));
    417       1.1      jmmv }
    418       1.1      jmmv 
    419       1.1      jmmv int
    420       1.1      jmmv tmpfs_getattr(void *v)
    421       1.1      jmmv {
    422      1.83     rmind 	struct vop_getattr_args /* {
    423      1.83     rmind 		struct vnode	*a_vp;
    424      1.83     rmind 		struct vattr	*a_vap;
    425      1.83     rmind 		kauth_cred_t	a_cred;
    426      1.83     rmind 	} */ *ap = v;
    427      1.83     rmind 	vnode_t *vp = ap->a_vp;
    428      1.83     rmind 	struct vattr *vap = ap->a_vap;
    429      1.83     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    430       1.1      jmmv 
    431      1.66     pooka 	vattr_null(vap);
    432       1.1      jmmv 
    433      1.86     rmind 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    434      1.86     rmind 
    435       1.1      jmmv 	vap->va_type = vp->v_type;
    436       1.1      jmmv 	vap->va_mode = node->tn_mode;
    437       1.1      jmmv 	vap->va_nlink = node->tn_links;
    438       1.1      jmmv 	vap->va_uid = node->tn_uid;
    439       1.1      jmmv 	vap->va_gid = node->tn_gid;
    440       1.1      jmmv 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    441       1.1      jmmv 	vap->va_fileid = node->tn_id;
    442       1.1      jmmv 	vap->va_size = node->tn_size;
    443       1.1      jmmv 	vap->va_blocksize = PAGE_SIZE;
    444       1.1      jmmv 	vap->va_atime = node->tn_atime;
    445       1.1      jmmv 	vap->va_mtime = node->tn_mtime;
    446       1.1      jmmv 	vap->va_ctime = node->tn_ctime;
    447       1.1      jmmv 	vap->va_birthtime = node->tn_birthtime;
    448      1.85     rmind 	vap->va_gen = TMPFS_NODE_GEN(node);
    449       1.1      jmmv 	vap->va_flags = node->tn_flags;
    450       1.1      jmmv 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    451      1.83     rmind 	    node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    452       1.1      jmmv 	vap->va_bytes = round_page(node->tn_size);
    453       1.1      jmmv 	vap->va_filerev = VNOVAL;
    454       1.1      jmmv 	vap->va_vaflags = 0;
    455       1.1      jmmv 	vap->va_spare = VNOVAL; /* XXX */
    456       1.1      jmmv 
    457       1.1      jmmv 	return 0;
    458       1.1      jmmv }
    459       1.1      jmmv 
    460      1.51  christos #define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
    461       1.1      jmmv /* XXX Should this operation be atomic?  I think it should, but code in
    462       1.1      jmmv  * XXX other places (e.g., ufs) doesn't seem to be... */
    463       1.1      jmmv int
    464       1.1      jmmv tmpfs_setattr(void *v)
    465       1.1      jmmv {
    466      1.83     rmind 	struct vop_setattr_args /* {
    467      1.83     rmind 		struct vnode	*a_vp;
    468      1.83     rmind 		struct vattr	*a_vap;
    469      1.83     rmind 		kauth_cred_t	a_cred;
    470      1.83     rmind 	} */ *ap = v;
    471      1.83     rmind 	vnode_t *vp = ap->a_vp;
    472      1.83     rmind 	struct vattr *vap = ap->a_vap;
    473      1.83     rmind 	kauth_cred_t cred = ap->a_cred;
    474      1.83     rmind 	lwp_t *l = curlwp;
    475      1.83     rmind 	int error = 0;
    476       1.1      jmmv 
    477       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    478       1.1      jmmv 
    479       1.1      jmmv 	/* Abort if any unsettable attribute is given. */
    480      1.83     rmind 	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
    481      1.83     rmind 	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
    482      1.83     rmind 	    vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
    483      1.83     rmind 	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
    484      1.83     rmind 	    vap->va_bytes != VNOVAL) {
    485      1.83     rmind 		return EINVAL;
    486      1.83     rmind 	}
    487       1.1      jmmv 	if (error == 0 && (vap->va_flags != VNOVAL))
    488      1.25        ad 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    489       1.1      jmmv 
    490       1.1      jmmv 	if (error == 0 && (vap->va_size != VNOVAL))
    491      1.25        ad 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    492       1.1      jmmv 
    493       1.1      jmmv 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    494      1.25        ad 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    495       1.1      jmmv 
    496       1.1      jmmv 	if (error == 0 && (vap->va_mode != VNOVAL))
    497      1.25        ad 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    498       1.1      jmmv 
    499      1.51  christos 	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
    500      1.83     rmind 	    || GOODTIME(&vap->va_birthtime))) {
    501      1.83     rmind 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    502      1.83     rmind 		    &vap->va_birthtime, vap->va_vaflags, cred, l);
    503      1.83     rmind 		if (error == 0)
    504      1.51  christos 			return 0;
    505      1.83     rmind 	}
    506      1.51  christos 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    507       1.1      jmmv 	return error;
    508       1.1      jmmv }
    509       1.1      jmmv 
    510       1.1      jmmv int
    511       1.1      jmmv tmpfs_read(void *v)
    512       1.1      jmmv {
    513      1.83     rmind 	struct vop_read_args /* {
    514      1.83     rmind 		struct vnode *a_vp;
    515      1.83     rmind 		struct uio *a_uio;
    516      1.83     rmind 		int a_ioflag;
    517      1.83     rmind 		kauth_cred_t a_cred;
    518      1.83     rmind 	} */ *ap = v;
    519      1.83     rmind 	vnode_t *vp = ap->a_vp;
    520      1.83     rmind 	struct uio *uio = ap->a_uio;
    521      1.83     rmind 	const int ioflag = ap->a_ioflag;
    522      1.83     rmind 	tmpfs_node_t *node;
    523      1.83     rmind 	struct uvm_object *uobj;
    524       1.7      jmmv 	int error;
    525       1.1      jmmv 
    526       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    527       1.1      jmmv 
    528       1.5      yamt 	if (vp->v_type != VREG) {
    529      1.83     rmind 		return EISDIR;
    530       1.5      yamt 	}
    531       1.5      yamt 	if (uio->uio_offset < 0) {
    532      1.83     rmind 		return EINVAL;
    533       1.1      jmmv 	}
    534       1.1      jmmv 
    535      1.83     rmind 	node = VP_TO_TMPFS_NODE(vp);
    536       1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    537      1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
    538       1.6      yamt 	error = 0;
    539      1.83     rmind 
    540       1.7      jmmv 	while (error == 0 && uio->uio_resid > 0) {
    541       1.6      yamt 		vsize_t len;
    542       1.6      yamt 
    543      1.83     rmind 		if (node->tn_size <= uio->uio_offset) {
    544       1.8      yamt 			break;
    545      1.83     rmind 		}
    546       1.6      yamt 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    547      1.83     rmind 		if (len == 0) {
    548       1.6      yamt 			break;
    549      1.83     rmind 		}
    550      1.52     pooka 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    551      1.52     pooka 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    552       1.1      jmmv 	}
    553       1.1      jmmv 	return error;
    554       1.1      jmmv }
    555       1.1      jmmv 
    556       1.1      jmmv int
    557       1.1      jmmv tmpfs_write(void *v)
    558       1.1      jmmv {
    559      1.83     rmind 	struct vop_write_args /* {
    560      1.83     rmind 		struct vnode	*a_vp;
    561      1.83     rmind 		struct uio	*a_uio;
    562      1.83     rmind 		int		a_ioflag;
    563      1.83     rmind 		kauth_cred_t	a_cred;
    564      1.83     rmind 	} */ *ap = v;
    565      1.83     rmind 	vnode_t *vp = ap->a_vp;
    566      1.83     rmind 	struct uio *uio = ap->a_uio;
    567      1.83     rmind 	const int ioflag = ap->a_ioflag;
    568      1.83     rmind 	tmpfs_node_t *node;
    569      1.83     rmind 	struct uvm_object *uobj;
    570      1.83     rmind 	off_t oldsize;
    571      1.36   thorpej 	bool extended;
    572       1.1      jmmv 	int error;
    573       1.1      jmmv 
    574       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    575       1.1      jmmv 
    576       1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    577       1.1      jmmv 	oldsize = node->tn_size;
    578       1.1      jmmv 
    579       1.1      jmmv 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    580       1.1      jmmv 		error = EINVAL;
    581       1.1      jmmv 		goto out;
    582       1.1      jmmv 	}
    583       1.1      jmmv 	if (uio->uio_resid == 0) {
    584       1.1      jmmv 		error = 0;
    585       1.1      jmmv 		goto out;
    586       1.1      jmmv 	}
    587      1.83     rmind 	if (ioflag & IO_APPEND) {
    588       1.1      jmmv 		uio->uio_offset = node->tn_size;
    589      1.83     rmind 	}
    590       1.1      jmmv 
    591       1.1      jmmv 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    592       1.1      jmmv 	if (extended) {
    593       1.1      jmmv 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    594      1.83     rmind 		if (error)
    595       1.1      jmmv 			goto out;
    596       1.1      jmmv 	}
    597       1.1      jmmv 
    598      1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
    599       1.6      yamt 	error = 0;
    600       1.7      jmmv 	while (error == 0 && uio->uio_resid > 0) {
    601       1.6      yamt 		vsize_t len;
    602       1.6      yamt 
    603       1.6      yamt 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    604      1.83     rmind 		if (len == 0) {
    605       1.6      yamt 			break;
    606      1.83     rmind 		}
    607      1.52     pooka 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    608      1.52     pooka 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    609       1.1      jmmv 	}
    610      1.83     rmind 	if (error) {
    611      1.83     rmind 		(void)tmpfs_reg_resize(vp, oldsize);
    612      1.83     rmind 	}
    613       1.6      yamt 
    614       1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    615       1.1      jmmv 	    (extended ? TMPFS_NODE_CHANGED : 0);
    616      1.31      jmmv 	VN_KNOTE(vp, NOTE_WRITE);
    617       1.1      jmmv out:
    618      1.83     rmind 	if (error) {
    619      1.83     rmind 		KASSERT(oldsize == node->tn_size);
    620      1.83     rmind 	} else {
    621      1.83     rmind 		KASSERT(uio->uio_resid == 0);
    622      1.83     rmind 	}
    623       1.1      jmmv 	return error;
    624       1.1      jmmv }
    625       1.1      jmmv 
    626       1.1      jmmv int
    627       1.1      jmmv tmpfs_fsync(void *v)
    628       1.1      jmmv {
    629      1.83     rmind 	struct vop_fsync_args /* {
    630      1.83     rmind 		struct vnode *a_vp;
    631      1.83     rmind 		kauth_cred_t a_cred;
    632      1.83     rmind 		int a_flags;
    633      1.83     rmind 		off_t a_offlo;
    634      1.83     rmind 		off_t a_offhi;
    635      1.83     rmind 		struct lwp *a_l;
    636      1.83     rmind 	} */ *ap = v;
    637      1.83     rmind 	vnode_t *vp = ap->a_vp;
    638       1.1      jmmv 
    639      1.83     rmind 	/* Nothing to do.  Just update. */
    640       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    641      1.51  christos 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    642      1.17      yamt 	return 0;
    643       1.1      jmmv }
    644       1.1      jmmv 
    645      1.83     rmind /*
    646      1.83     rmind  * tmpfs_remove: unlink a file.
    647      1.83     rmind  *
    648      1.83     rmind  * => Both directory (dvp) and file (vp) are locked.
    649      1.83     rmind  * => We unlock and drop the reference on both.
    650      1.83     rmind  */
    651       1.1      jmmv int
    652       1.1      jmmv tmpfs_remove(void *v)
    653       1.1      jmmv {
    654      1.82     rmind 	struct vop_remove_args /* {
    655      1.82     rmind 		struct vnode *a_dvp;
    656      1.82     rmind 		struct vnode *a_vp;
    657      1.82     rmind 		struct componentname *a_cnp;
    658      1.82     rmind 	} */ *ap = v;
    659      1.83     rmind 	vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
    660      1.85     rmind 	tmpfs_node_t *node;
    661      1.83     rmind 	tmpfs_dirent_t *de;
    662      1.82     rmind 	int error;
    663       1.1      jmmv 
    664       1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
    665       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    666       1.1      jmmv 
    667      1.34     pooka 	if (vp->v_type == VDIR) {
    668      1.34     pooka 		error = EPERM;
    669      1.34     pooka 		goto out;
    670      1.34     pooka 	}
    671       1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    672       1.1      jmmv 
    673       1.1      jmmv 	/* Files marked as immutable or append-only cannot be deleted. */
    674       1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    675       1.1      jmmv 		error = EPERM;
    676       1.1      jmmv 		goto out;
    677       1.1      jmmv 	}
    678       1.1      jmmv 
    679      1.85     rmind 	/* Lookup the directory entry (check the cached hint first). */
    680      1.85     rmind 	de = tmpfs_dir_cached(node);
    681      1.85     rmind 	if (de == NULL) {
    682      1.85     rmind 		tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    683      1.85     rmind 		struct componentname *cnp = ap->a_cnp;
    684      1.85     rmind 		de = tmpfs_dir_lookup(dnode, cnp);
    685      1.85     rmind 	}
    686      1.82     rmind 	KASSERT(de && de->td_node == node);
    687       1.1      jmmv 
    688      1.82     rmind 	/*
    689      1.85     rmind 	 * Remove the entry from the directory (drops the link count) and
    690      1.90   hannken 	 * destroy it or replace it with a whiteout.
    691      1.90   hannken 	 * Note: the inode referred by it will not be destroyed
    692      1.85     rmind 	 * until the vnode is reclaimed/recycled.
    693      1.82     rmind 	 */
    694      1.85     rmind 	tmpfs_dir_detach(dvp, de);
    695      1.90   hannken 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
    696      1.90   hannken 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
    697      1.90   hannken 	else
    698      1.90   hannken 		tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
    699       1.1      jmmv 	error = 0;
    700       1.1      jmmv out:
    701      1.83     rmind 	/* Drop the references and unlock the vnodes. */
    702       1.1      jmmv 	vput(vp);
    703      1.83     rmind 	if (dvp == vp) {
    704      1.34     pooka 		vrele(dvp);
    705      1.83     rmind 	} else {
    706      1.34     pooka 		vput(dvp);
    707      1.83     rmind 	}
    708       1.1      jmmv 	return error;
    709       1.1      jmmv }
    710       1.1      jmmv 
    711      1.77     rmind /*
    712      1.83     rmind  * tmpfs_link: create a hard link.
    713      1.77     rmind  */
    714       1.1      jmmv int
    715       1.1      jmmv tmpfs_link(void *v)
    716       1.1      jmmv {
    717      1.77     rmind 	struct vop_link_args /* {
    718      1.77     rmind 		struct vnode *a_dvp;
    719      1.77     rmind 		struct vnode *a_vp;
    720      1.77     rmind 		struct componentname *a_cnp;
    721      1.77     rmind 	} */ *ap = v;
    722      1.83     rmind 	vnode_t *dvp = ap->a_dvp;
    723      1.83     rmind 	vnode_t *vp = ap->a_vp;
    724      1.82     rmind 	struct componentname *cnp = ap->a_cnp;
    725      1.83     rmind 	tmpfs_node_t *dnode, *node;
    726      1.83     rmind 	tmpfs_dirent_t *de;
    727       1.1      jmmv 	int error;
    728       1.1      jmmv 
    729      1.77     rmind 	KASSERT(dvp != vp);
    730       1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
    731      1.77     rmind 	KASSERT(vp->v_type != VDIR);
    732      1.77     rmind 	KASSERT(dvp->v_mount == vp->v_mount);
    733       1.1      jmmv 
    734       1.1      jmmv 	dnode = VP_TO_TMPFS_DIR(dvp);
    735       1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    736       1.1      jmmv 
    737      1.63     rmind 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    738       1.1      jmmv 
    739      1.84     rmind 	/* Check for maximum number of links limit. */
    740       1.1      jmmv 	if (node->tn_links == LINK_MAX) {
    741       1.1      jmmv 		error = EMLINK;
    742       1.1      jmmv 		goto out;
    743       1.1      jmmv 	}
    744      1.85     rmind 	KASSERT(node->tn_links < LINK_MAX);
    745       1.1      jmmv 
    746       1.1      jmmv 	/* We cannot create links of files marked immutable or append-only. */
    747       1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    748       1.1      jmmv 		error = EPERM;
    749       1.1      jmmv 		goto out;
    750       1.1      jmmv 	}
    751       1.1      jmmv 
    752      1.85     rmind 	/* Allocate a new directory entry to represent the inode. */
    753      1.85     rmind 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
    754       1.1      jmmv 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    755      1.83     rmind 	if (error) {
    756       1.1      jmmv 		goto out;
    757      1.83     rmind 	}
    758       1.1      jmmv 
    759      1.85     rmind 	/*
    760      1.85     rmind 	 * Insert the entry into the directory.
    761      1.85     rmind 	 * It will increase the inode link count.
    762      1.85     rmind 	 */
    763      1.85     rmind 	tmpfs_dir_attach(dvp, de, node);
    764       1.1      jmmv 
    765      1.85     rmind 	/* Update the timestamps and trigger the event. */
    766      1.85     rmind 	if (node->tn_vnode) {
    767      1.85     rmind 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
    768      1.85     rmind 	}
    769       1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    770      1.51  christos 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    771       1.1      jmmv 	error = 0;
    772       1.1      jmmv out:
    773      1.71   hannken 	VOP_UNLOCK(vp);
    774       1.1      jmmv 	vput(dvp);
    775       1.1      jmmv 	return error;
    776       1.1      jmmv }
    777       1.1      jmmv 
    778      1.63     rmind /*
    779      1.89  riastrad  * tmpfs_rename: rename routine, the hairiest system call, with the
    780      1.89  riastrad  * insane API.
    781      1.63     rmind  *
    782      1.63     rmind  * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
    783      1.63     rmind  * and tvp (to-leaf), if exists (NULL if not).
    784      1.63     rmind  *
    785      1.63     rmind  * => Caller holds a reference on fdvp and fvp, they are unlocked.
    786      1.63     rmind  *    Note: fdvp and fvp can refer to the same object (i.e. when it is root).
    787      1.63     rmind  *
    788      1.63     rmind  * => Both tdvp and tvp are referenced and locked.  It is our responsibility
    789      1.63     rmind  *    to release the references and unlock them (or destroy).
    790      1.63     rmind  */
    791      1.89  riastrad 
    792      1.89  riastrad /*
    793      1.89  riastrad  * First, some forward declarations of subroutines.
    794      1.89  riastrad  */
    795      1.89  riastrad 
    796      1.89  riastrad static int tmpfs_sane_rename(struct vnode *, struct componentname *,
    797      1.89  riastrad     struct vnode *, struct componentname *, kauth_cred_t, bool);
    798      1.89  riastrad static int tmpfs_rename_enter(struct mount *, struct tmpfs_mount *,
    799      1.89  riastrad     kauth_cred_t,
    800      1.89  riastrad     struct vnode *, struct tmpfs_node *, struct componentname *,
    801      1.89  riastrad     struct tmpfs_dirent **, struct vnode **,
    802      1.89  riastrad     struct vnode *, struct tmpfs_node *, struct componentname *,
    803      1.89  riastrad     struct tmpfs_dirent **, struct vnode **);
    804      1.89  riastrad static int tmpfs_rename_enter_common(struct mount *, struct tmpfs_mount *,
    805      1.89  riastrad     kauth_cred_t,
    806      1.89  riastrad     struct vnode *, struct tmpfs_node *,
    807      1.89  riastrad     struct componentname *, struct tmpfs_dirent **, struct vnode **,
    808      1.89  riastrad     struct componentname *, struct tmpfs_dirent **, struct vnode **);
    809      1.89  riastrad static int tmpfs_rename_enter_separate(struct mount *, struct tmpfs_mount *,
    810      1.89  riastrad     kauth_cred_t,
    811      1.89  riastrad     struct vnode *, struct tmpfs_node *, struct componentname *,
    812      1.89  riastrad     struct tmpfs_dirent **, struct vnode **,
    813      1.89  riastrad     struct vnode *, struct tmpfs_node *, struct componentname *,
    814      1.89  riastrad     struct tmpfs_dirent **, struct vnode **);
    815      1.89  riastrad static void tmpfs_rename_exit(struct tmpfs_mount *,
    816      1.89  riastrad     struct vnode *, struct vnode *, struct vnode *, struct vnode *);
    817      1.89  riastrad static int tmpfs_rename_lock_directory(struct vnode *, struct tmpfs_node *);
    818      1.89  riastrad static int tmpfs_rename_genealogy(struct tmpfs_node *, struct tmpfs_node *,
    819      1.89  riastrad     struct tmpfs_node **);
    820      1.89  riastrad static int tmpfs_rename_lock(struct mount *, kauth_cred_t, int,
    821      1.89  riastrad     struct vnode *, struct tmpfs_node *, struct componentname *, bool,
    822      1.89  riastrad     struct tmpfs_dirent **, struct vnode **,
    823      1.89  riastrad     struct vnode *, struct tmpfs_node *, struct componentname *, bool,
    824      1.89  riastrad     struct tmpfs_dirent **, struct vnode **);
    825      1.89  riastrad static void tmpfs_rename_attachdetach(struct tmpfs_mount *,
    826      1.89  riastrad     struct vnode *, struct tmpfs_dirent *, struct vnode *,
    827      1.89  riastrad     struct vnode *, struct tmpfs_dirent *, struct vnode *);
    828      1.89  riastrad static int tmpfs_do_remove(struct tmpfs_mount *, struct vnode *,
    829      1.89  riastrad     struct tmpfs_node *, struct tmpfs_dirent *, struct vnode *, kauth_cred_t);
    830      1.89  riastrad static int tmpfs_rename_check_possible(struct tmpfs_node *,
    831      1.89  riastrad     struct tmpfs_node *, struct tmpfs_node *, struct tmpfs_node *);
    832      1.89  riastrad static int tmpfs_rename_check_permitted(kauth_cred_t,
    833      1.89  riastrad     struct tmpfs_node *, struct tmpfs_node *,
    834      1.89  riastrad     struct tmpfs_node *, struct tmpfs_node *);
    835      1.89  riastrad static int tmpfs_remove_check_possible(struct tmpfs_node *,
    836      1.89  riastrad     struct tmpfs_node *);
    837      1.89  riastrad static int tmpfs_remove_check_permitted(kauth_cred_t,
    838      1.89  riastrad     struct tmpfs_node *, struct tmpfs_node *);
    839      1.89  riastrad static int tmpfs_check_sticky(kauth_cred_t,
    840      1.89  riastrad     struct tmpfs_node *, struct tmpfs_node *);
    841      1.89  riastrad 
    842       1.1      jmmv int
    843       1.1      jmmv tmpfs_rename(void *v)
    844       1.1      jmmv {
    845      1.83     rmind 	struct vop_rename_args  /* {
    846      1.83     rmind 		struct vnode		*a_fdvp;
    847      1.83     rmind 		struct vnode		*a_fvp;
    848      1.83     rmind 		struct componentname	*a_fcnp;
    849      1.83     rmind 		struct vnode		*a_tdvp;
    850      1.83     rmind 		struct vnode		*a_tvp;
    851      1.83     rmind 		struct componentname	*a_tcnp;
    852      1.83     rmind 	} */ *ap = v;
    853      1.89  riastrad 	struct vnode *fdvp = ap->a_fdvp;
    854      1.89  riastrad 	struct vnode *fvp = ap->a_fvp;
    855      1.83     rmind 	struct componentname *fcnp = ap->a_fcnp;
    856      1.89  riastrad 	struct vnode *tdvp = ap->a_tdvp;
    857      1.89  riastrad 	struct vnode *tvp = ap->a_tvp;
    858      1.83     rmind 	struct componentname *tcnp = ap->a_tcnp;
    859      1.89  riastrad 	kauth_cred_t cred;
    860      1.89  riastrad 	int error;
    861      1.89  riastrad 
    862      1.89  riastrad 	KASSERT(fdvp != NULL);
    863      1.89  riastrad 	KASSERT(fvp != NULL);
    864      1.89  riastrad 	KASSERT(fcnp != NULL);
    865      1.89  riastrad 	KASSERT(fcnp->cn_nameptr != NULL);
    866      1.89  riastrad 	KASSERT(tdvp != NULL);
    867      1.89  riastrad 	KASSERT(tcnp != NULL);
    868      1.89  riastrad 	KASSERT(fcnp->cn_nameptr != NULL);
    869      1.89  riastrad 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
    870      1.89  riastrad 	/* KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
    871      1.89  riastrad 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    872      1.89  riastrad 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    873      1.89  riastrad 	KASSERT(fdvp->v_type == VDIR);
    874      1.89  riastrad 	KASSERT(tdvp->v_type == VDIR);
    875      1.89  riastrad 
    876      1.89  riastrad 	cred = fcnp->cn_cred;
    877      1.89  riastrad 	KASSERT(tcnp->cn_cred == cred);
    878      1.89  riastrad 
    879      1.89  riastrad 	/*
    880      1.89  riastrad 	 * Sanitize our world from the VFS insanity.  Unlock the target
    881      1.89  riastrad 	 * directory and node, which are locked.  Release the children,
    882      1.89  riastrad 	 * which are referenced.  Check for rename("x", "y/."), which
    883      1.89  riastrad 	 * it is our responsibility to reject, not the caller's.  (But
    884      1.89  riastrad 	 * the caller does reject rename("x/.", "y").  Go figure.)
    885      1.89  riastrad 	 */
    886      1.89  riastrad 
    887      1.89  riastrad 	VOP_UNLOCK(tdvp);
    888      1.89  riastrad 	if ((tvp != NULL) && (tvp != tdvp))
    889      1.89  riastrad 		VOP_UNLOCK(tvp);
    890      1.89  riastrad 
    891      1.89  riastrad 	vrele(fvp);
    892      1.89  riastrad 	if (tvp != NULL)
    893      1.89  riastrad 		vrele(tvp);
    894      1.89  riastrad 
    895      1.89  riastrad 	if (tvp == tdvp) {
    896      1.89  riastrad 		error = EINVAL;
    897      1.89  riastrad 		goto out;
    898      1.89  riastrad 	}
    899      1.89  riastrad 
    900      1.89  riastrad 	error = tmpfs_sane_rename(fdvp, fcnp, tdvp, tcnp, cred, false);
    901      1.89  riastrad 
    902      1.89  riastrad out:	/*
    903      1.89  riastrad 	 * All done, whether with success or failure.  Release the
    904      1.89  riastrad 	 * directory nodes now, as the caller expects from the VFS
    905      1.89  riastrad 	 * protocol.
    906      1.89  riastrad 	 */
    907      1.89  riastrad 	vrele(fdvp);
    908      1.89  riastrad 	vrele(tdvp);
    909      1.89  riastrad 
    910      1.89  riastrad 	return error;
    911      1.89  riastrad }
    912      1.89  riastrad 
    913      1.89  riastrad /*
    914      1.89  riastrad  * tmpfs_sane_rename: rename routine, the hairiest system call, with
    915      1.89  riastrad  * the sane API.
    916      1.89  riastrad  *
    917      1.89  riastrad  * Arguments:
    918      1.89  riastrad  *
    919      1.89  riastrad  * . fdvp (from directory vnode),
    920      1.89  riastrad  * . fcnp (from component name),
    921      1.89  riastrad  * . tdvp (to directory vnode), and
    922      1.89  riastrad  * . tcnp (to component name).
    923      1.89  riastrad  *
    924      1.89  riastrad  * fdvp and tdvp must be referenced and unlocked.
    925      1.89  riastrad  */
    926      1.89  riastrad static int
    927      1.89  riastrad tmpfs_sane_rename(struct vnode *fdvp, struct componentname *fcnp,
    928      1.89  riastrad     struct vnode *tdvp, struct componentname *tcnp, kauth_cred_t cred,
    929      1.89  riastrad     bool posixly_correct)
    930      1.89  riastrad {
    931      1.89  riastrad 	struct mount *mount;
    932      1.89  riastrad 	struct tmpfs_mount *tmpfs;
    933      1.89  riastrad 	struct tmpfs_node *fdnode, *tdnode;
    934      1.89  riastrad 	struct tmpfs_dirent *fde, *tde;
    935      1.89  riastrad 	struct vnode *fvp, *tvp;
    936       1.1      jmmv 	char *newname;
    937       1.1      jmmv 	int error;
    938       1.1      jmmv 
    939      1.89  riastrad 	KASSERT(fdvp != NULL);
    940      1.89  riastrad 	KASSERT(fcnp != NULL);
    941      1.89  riastrad 	KASSERT(tdvp != NULL);
    942      1.89  riastrad 	KASSERT(tcnp != NULL);
    943      1.89  riastrad 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
    944      1.89  riastrad 	/* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
    945      1.89  riastrad 	KASSERT(fdvp->v_type == VDIR);
    946      1.89  riastrad 	KASSERT(tdvp->v_type == VDIR);
    947      1.89  riastrad 	KASSERT(fdvp->v_mount == tdvp->v_mount);
    948      1.85     rmind 	KASSERT((fcnp->cn_flags & ISDOTDOT) == 0);
    949      1.85     rmind 	KASSERT((tcnp->cn_flags & ISDOTDOT) == 0);
    950      1.89  riastrad 	KASSERT((fcnp->cn_namelen != 1) || (fcnp->cn_nameptr[0] != '.'));
    951      1.89  riastrad 	KASSERT((tcnp->cn_namelen != 1) || (tcnp->cn_nameptr[0] != '.'));
    952      1.89  riastrad 	KASSERT((fcnp->cn_namelen != 2) || (fcnp->cn_nameptr[0] != '.') ||
    953      1.89  riastrad 	    (fcnp->cn_nameptr[1] != '.'));
    954      1.89  riastrad 	KASSERT((tcnp->cn_namelen != 2) || (tcnp->cn_nameptr[0] != '.') ||
    955      1.89  riastrad 	    (tcnp->cn_nameptr[1] != '.'));
    956       1.1      jmmv 
    957      1.89  riastrad 	/*
    958      1.89  riastrad 	 * Pull out the tmpfs data structures.
    959      1.89  riastrad 	 */
    960      1.89  riastrad 	fdnode = VP_TO_TMPFS_NODE(fdvp);
    961      1.89  riastrad 	tdnode = VP_TO_TMPFS_NODE(tdvp);
    962      1.89  riastrad 	KASSERT(fdnode != NULL);
    963      1.89  riastrad 	KASSERT(tdnode != NULL);
    964      1.89  riastrad 	KASSERT(fdnode->tn_vnode == fdvp);
    965      1.89  riastrad 	KASSERT(tdnode->tn_vnode == tdvp);
    966      1.89  riastrad 	KASSERT(fdnode->tn_type == VDIR);
    967      1.89  riastrad 	KASSERT(tdnode->tn_type == VDIR);
    968      1.89  riastrad 
    969      1.89  riastrad 	mount = fdvp->v_mount;
    970      1.89  riastrad 	KASSERT(mount != NULL);
    971      1.89  riastrad 	KASSERT(mount == tdvp->v_mount);
    972      1.89  riastrad 	/* XXX How can we be sure this stays true?  (Not that you're
    973      1.89  riastrad 	 * likely to mount a tmpfs read-only...)  */
    974      1.89  riastrad 	KASSERT((mount->mnt_flag & MNT_RDONLY) == 0);
    975      1.89  riastrad 	tmpfs = VFS_TO_TMPFS(mount);
    976      1.89  riastrad 	KASSERT(tmpfs != NULL);
    977       1.1      jmmv 
    978      1.89  riastrad 	/*
    979      1.89  riastrad 	 * Decide whether we need a new name, and allocate memory for
    980      1.89  riastrad 	 * it if so.  Do this before locking anything or taking
    981      1.89  riastrad 	 * destructive actions so that we can back out safely and sleep
    982      1.89  riastrad 	 * safely.  XXX Is sleeping an issue here?  Can this just be
    983      1.89  riastrad 	 * moved into tmpfs_rename_attachdetach?
    984      1.89  riastrad 	 */
    985      1.70     rmind 	if (tmpfs_strname_neqlen(fcnp, tcnp)) {
    986      1.89  riastrad 		newname = tmpfs_strname_alloc(tmpfs, tcnp->cn_namelen);
    987      1.70     rmind 		if (newname == NULL) {
    988      1.70     rmind 			error = ENOSPC;
    989      1.70     rmind 			goto out_unlocked;
    990      1.70     rmind 		}
    991      1.89  riastrad 	} else {
    992      1.89  riastrad 		newname = NULL;
    993      1.70     rmind 	}
    994      1.70     rmind 
    995      1.89  riastrad 	/*
    996      1.89  riastrad 	 * Lock and look up everything.  GCC is not very clever.
    997      1.89  riastrad 	 */
    998      1.89  riastrad 	fde = tde = NULL;
    999      1.89  riastrad 	fvp = tvp = NULL;
   1000      1.89  riastrad 	error = tmpfs_rename_enter(mount, tmpfs, cred,
   1001      1.89  riastrad 	    fdvp, fdnode, fcnp, &fde, &fvp,
   1002      1.89  riastrad 	    tdvp, tdnode, tcnp, &tde, &tvp);
   1003      1.89  riastrad 	if (error)
   1004      1.89  riastrad 		goto out_unlocked;
   1005      1.45        ad 
   1006      1.89  riastrad 	/*
   1007      1.89  riastrad 	 * Check that everything is locked and looks right.
   1008      1.89  riastrad 	 */
   1009      1.89  riastrad 	KASSERT(fde != NULL);
   1010      1.89  riastrad 	KASSERT(fvp != NULL);
   1011      1.89  riastrad 	KASSERT(fde->td_node != NULL);
   1012      1.89  riastrad 	KASSERT(fde->td_node->tn_vnode == fvp);
   1013      1.89  riastrad 	KASSERT(fde->td_node->tn_type == fvp->v_type);
   1014      1.89  riastrad 	KASSERT((tde == NULL) == (tvp == NULL));
   1015      1.89  riastrad 	KASSERT((tde == NULL) || (tde->td_node != NULL));
   1016      1.89  riastrad 	KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
   1017      1.89  riastrad 	KASSERT((tde == NULL) || (tde->td_node->tn_type == tvp->v_type));
   1018      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1019      1.89  riastrad 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1020      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1021      1.89  riastrad 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   1022       1.1      jmmv 
   1023      1.85     rmind 	/*
   1024      1.89  riastrad 	 * If the source and destination are the same object, we need
   1025      1.89  riastrad 	 * only at most delete the source entry.
   1026      1.85     rmind 	 */
   1027       1.1      jmmv 	if (fvp == tvp) {
   1028      1.89  riastrad 		KASSERT(tvp != NULL);
   1029      1.89  riastrad 		if (fde->td_node->tn_type == VDIR) {
   1030      1.89  riastrad 			/* XXX How can this possibly happen?  */
   1031      1.85     rmind 			error = EINVAL;
   1032      1.89  riastrad 			goto out_locked;
   1033      1.89  riastrad 		}
   1034      1.89  riastrad 		if (!posixly_correct && (fde != tde)) {
   1035      1.89  riastrad 			/* XXX Doesn't work because of locking.
   1036      1.89  riastrad 			 * error = VOP_REMOVE(fdvp, fvp);
   1037      1.89  riastrad 			 */
   1038      1.89  riastrad 			error = tmpfs_do_remove(tmpfs, fdvp, fdnode, fde, fvp,
   1039      1.89  riastrad 			    cred);
   1040      1.89  riastrad 			if (error)
   1041      1.89  riastrad 				goto out_locked;
   1042      1.85     rmind 		}
   1043      1.89  riastrad 		goto success;
   1044       1.1      jmmv 	}
   1045      1.89  riastrad 	KASSERT(fde != tde);
   1046      1.89  riastrad 	KASSERT(fvp != tvp);
   1047       1.1      jmmv 
   1048      1.89  riastrad 	/*
   1049      1.89  riastrad 	 * If the target exists, refuse to rename a directory over a
   1050      1.89  riastrad 	 * non-directory or vice versa, or to clobber a non-empty
   1051      1.89  riastrad 	 * directory.
   1052      1.89  riastrad 	 */
   1053      1.39      jmmv 	if (tvp != NULL) {
   1054      1.89  riastrad 		KASSERT(tde != NULL);
   1055      1.89  riastrad 		KASSERT(tde->td_node != NULL);
   1056      1.89  riastrad 		if (fvp->v_type == VDIR && tvp->v_type == VDIR)
   1057      1.89  riastrad 			error = ((tde->td_node->tn_size > 0)? ENOTEMPTY : 0);
   1058      1.89  riastrad 		else if (fvp->v_type == VDIR && tvp->v_type != VDIR)
   1059      1.39      jmmv 			error = ENOTDIR;
   1060      1.89  riastrad 		else if (fvp->v_type != VDIR && tvp->v_type == VDIR)
   1061      1.39      jmmv 			error = EISDIR;
   1062      1.89  riastrad 		else
   1063      1.89  riastrad 			error = 0;
   1064      1.89  riastrad 		if (error)
   1065      1.89  riastrad 			goto out_locked;
   1066      1.89  riastrad 		KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
   1067      1.89  riastrad 	}
   1068      1.89  riastrad 
   1069      1.89  riastrad 	/*
   1070      1.89  riastrad 	 * Authorize the rename.
   1071      1.89  riastrad 	 */
   1072      1.89  riastrad 	error = tmpfs_rename_check_possible(fdnode, fde->td_node,
   1073      1.89  riastrad 	    tdnode, (tde? tde->td_node : NULL));
   1074      1.89  riastrad 	if (error)
   1075      1.89  riastrad 		goto out_locked;
   1076      1.89  riastrad 	error = tmpfs_rename_check_permitted(cred, fdnode, fde->td_node,
   1077      1.89  riastrad 	    tdnode, (tde? tde->td_node : NULL));
   1078      1.89  riastrad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, fvp, fdvp,
   1079      1.89  riastrad 	    error);
   1080      1.89  riastrad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_RENAME, tvp, tdvp,
   1081      1.89  riastrad 	    error);
   1082      1.89  riastrad 	if (error)
   1083      1.89  riastrad 		goto out_locked;
   1084      1.89  riastrad 
   1085      1.89  riastrad 	/*
   1086      1.89  riastrad 	 * Everything is hunky-dory.  Shuffle the directory entries.
   1087      1.89  riastrad 	 */
   1088      1.89  riastrad 	tmpfs_rename_attachdetach(tmpfs, fdvp, fde, fvp, tdvp, tde, tvp);
   1089      1.89  riastrad 
   1090      1.89  riastrad 	/*
   1091      1.89  riastrad 	 * Update the directory entry's name necessary, and flag
   1092      1.89  riastrad 	 * metadata updates.  A memory allocation failure here is not
   1093      1.89  riastrad 	 * OK because we've already committed some changes that we
   1094      1.89  riastrad 	 * can't back out at this point, and we have things locked so
   1095      1.89  riastrad 	 * we can't sleep, hence the early allocation above.
   1096      1.89  riastrad 	 */
   1097      1.89  riastrad 	if (newname != NULL) {
   1098      1.91  christos 		KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
   1099      1.89  riastrad 
   1100      1.89  riastrad 		tmpfs_strname_free(tmpfs, fde->td_name, fde->td_namelen);
   1101      1.89  riastrad 		fde->td_namelen = (uint16_t)tcnp->cn_namelen;
   1102      1.89  riastrad 		(void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
   1103      1.89  riastrad 		/* Commit newname and don't free it on the way out.  */
   1104      1.89  riastrad 		fde->td_name = newname;
   1105      1.89  riastrad 		newname = NULL;
   1106      1.89  riastrad 
   1107      1.89  riastrad 		fde->td_node->tn_status |= TMPFS_NODE_CHANGED;
   1108      1.89  riastrad 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
   1109      1.89  riastrad 	}
   1110      1.89  riastrad 
   1111      1.89  riastrad success:
   1112      1.89  riastrad 	VN_KNOTE(fvp, NOTE_RENAME);
   1113      1.89  riastrad 	error = 0;
   1114      1.89  riastrad 
   1115      1.89  riastrad out_locked:
   1116      1.89  riastrad 	tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
   1117      1.89  riastrad 
   1118      1.89  riastrad out_unlocked:
   1119      1.89  riastrad 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
   1120      1.89  riastrad 	/* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
   1121      1.89  riastrad 	/* KASSERT((fvp == NULL) || (VOP_ISLOCKED(fvp) != LK_EXCLUSIVE)); */
   1122      1.89  riastrad 	/* KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
   1123      1.89  riastrad 
   1124      1.89  riastrad 	if (newname != NULL)
   1125      1.89  riastrad 		tmpfs_strname_free(tmpfs, newname, tcnp->cn_namelen);
   1126      1.89  riastrad 
   1127      1.89  riastrad 	return error;
   1128      1.89  riastrad }
   1129      1.89  riastrad 
   1130      1.89  riastrad /*
   1131      1.89  riastrad  * Look up fcnp in fdnode/fdvp and store its directory entry in fde_ret
   1132      1.89  riastrad  * and the associated vnode in fvp_ret; fail if not found.  Look up
   1133      1.89  riastrad  * tcnp in tdnode/tdvp and store its directory entry in tde_ret and the
   1134      1.89  riastrad  * associated vnode in tvp_ret; store null instead if not found.  Fail
   1135      1.89  riastrad  * if anything has been mounted on any of the nodes involved.
   1136      1.89  riastrad  *
   1137      1.89  riastrad  * fdvp and tdvp must be referenced.
   1138      1.89  riastrad  *
   1139      1.89  riastrad  * On entry, nothing is locked.
   1140      1.89  riastrad  *
   1141      1.89  riastrad  * On success, everything is locked, and *fvp_ret, and *tvp_ret if
   1142      1.89  riastrad  * nonnull, are referenced.  The only pairs of vnodes that may be
   1143      1.89  riastrad  * identical are {fdvp, tdvp} and {fvp, tvp}.
   1144      1.89  riastrad  *
   1145      1.89  riastrad  * On failure, everything remains as was.
   1146      1.89  riastrad  *
   1147      1.89  riastrad  * Locking everything including the source and target nodes is
   1148      1.89  riastrad  * necessary to make sure that, e.g., link count updates are OK.  The
   1149      1.89  riastrad  * locking order is, in general, ancestor-first, matching the order you
   1150      1.89  riastrad  * need to use to look up a descendant anyway.
   1151      1.89  riastrad  */
   1152      1.89  riastrad static int
   1153      1.89  riastrad tmpfs_rename_enter(struct mount *mount, struct tmpfs_mount *tmpfs,
   1154      1.89  riastrad     kauth_cred_t cred,
   1155      1.89  riastrad     struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
   1156      1.89  riastrad     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
   1157      1.89  riastrad     struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
   1158      1.89  riastrad     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
   1159      1.89  riastrad {
   1160      1.89  riastrad 	int error;
   1161      1.89  riastrad 
   1162      1.89  riastrad 	KASSERT(mount != NULL);
   1163      1.89  riastrad 	KASSERT(tmpfs != NULL);
   1164      1.89  riastrad 	KASSERT(fdvp != NULL);
   1165      1.89  riastrad 	KASSERT(fdnode != NULL);
   1166      1.89  riastrad 	KASSERT(fcnp != NULL);
   1167      1.89  riastrad 	KASSERT(fde_ret != NULL);
   1168      1.89  riastrad 	KASSERT(fvp_ret != NULL);
   1169      1.89  riastrad 	KASSERT(tdvp != NULL);
   1170      1.89  riastrad 	KASSERT(tdnode != NULL);
   1171      1.89  riastrad 	KASSERT(tcnp != NULL);
   1172      1.89  riastrad 	KASSERT(tde_ret != NULL);
   1173      1.89  riastrad 	KASSERT(tvp_ret != NULL);
   1174      1.89  riastrad 	KASSERT(fdnode->tn_vnode == fdvp);
   1175      1.89  riastrad 	KASSERT(tdnode->tn_vnode == tdvp);
   1176      1.89  riastrad 	KASSERT(fdnode->tn_type == VDIR);
   1177      1.89  riastrad 	KASSERT(tdnode->tn_type == VDIR);
   1178      1.89  riastrad 
   1179      1.89  riastrad 	if (fdvp == tdvp) {
   1180      1.89  riastrad 		KASSERT(fdnode == tdnode);
   1181      1.89  riastrad 		error = tmpfs_rename_enter_common(mount, tmpfs, cred, fdvp,
   1182      1.89  riastrad 		    fdnode, fcnp, fde_ret, fvp_ret, tcnp, tde_ret, tvp_ret);
   1183      1.89  riastrad 	} else {
   1184      1.89  riastrad 		KASSERT(fdnode != tdnode);
   1185      1.89  riastrad 		error = tmpfs_rename_enter_separate(mount, tmpfs, cred,
   1186      1.89  riastrad 		    fdvp, fdnode, fcnp, fde_ret, fvp_ret,
   1187      1.89  riastrad 		    tdvp, tdnode, tcnp, tde_ret, tvp_ret);
   1188      1.89  riastrad 	}
   1189      1.89  riastrad 
   1190      1.89  riastrad 	if (error)
   1191      1.89  riastrad 		return error;
   1192      1.89  riastrad 
   1193      1.89  riastrad 	KASSERT(*fde_ret != NULL);
   1194      1.89  riastrad 	KASSERT(*fvp_ret != NULL);
   1195      1.89  riastrad 	KASSERT((*tde_ret == NULL) == (*tvp_ret == NULL));
   1196      1.89  riastrad 	KASSERT((*tde_ret == NULL) || ((*tde_ret)->td_node != NULL));
   1197      1.89  riastrad 	KASSERT((*tde_ret == NULL) ||
   1198      1.89  riastrad 	    ((*tde_ret)->td_node->tn_vnode == *tvp_ret));
   1199      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1200      1.89  riastrad 	KASSERT(VOP_ISLOCKED(*fvp_ret) == LK_EXCLUSIVE);
   1201      1.89  riastrad 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1202      1.89  riastrad 	KASSERT((*tvp_ret == NULL) ||
   1203      1.89  riastrad 	    (VOP_ISLOCKED(*tvp_ret) == LK_EXCLUSIVE));
   1204      1.89  riastrad 	KASSERT(*fvp_ret != fdvp);
   1205      1.89  riastrad 	KASSERT(*fvp_ret != tdvp);
   1206      1.89  riastrad 	KASSERT(*tvp_ret != fdvp);
   1207      1.89  riastrad 	KASSERT(*tvp_ret != tdvp);
   1208      1.89  riastrad 	return 0;
   1209      1.89  riastrad }
   1210      1.89  riastrad 
   1211      1.89  riastrad /*
   1212      1.89  riastrad  * Lock and look up with a common source/target directory.
   1213      1.89  riastrad  */
   1214      1.89  riastrad static int
   1215      1.89  riastrad tmpfs_rename_enter_common(struct mount *mount, struct tmpfs_mount *tmpfs,
   1216      1.89  riastrad     kauth_cred_t cred,
   1217      1.89  riastrad     struct vnode *dvp, struct tmpfs_node *dnode,
   1218      1.89  riastrad     struct componentname *fcnp,
   1219      1.89  riastrad     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
   1220      1.89  riastrad     struct componentname *tcnp,
   1221      1.89  riastrad     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
   1222      1.89  riastrad {
   1223      1.89  riastrad 	struct tmpfs_dirent *fde, *tde;
   1224      1.89  riastrad 	struct vnode *fvp, *tvp;
   1225      1.89  riastrad 	int error;
   1226      1.89  riastrad 
   1227      1.89  riastrad 	error = tmpfs_rename_lock_directory(dvp, dnode);
   1228      1.89  riastrad 	if (error)
   1229      1.89  riastrad 		goto fail0;
   1230      1.89  riastrad 
   1231      1.89  riastrad 	/* Did we lose a race with mount?  */
   1232      1.89  riastrad 	if (dvp->v_mountedhere != NULL) {
   1233      1.89  riastrad 		error = EBUSY;
   1234      1.89  riastrad 		goto fail1;
   1235      1.89  riastrad 	}
   1236      1.89  riastrad 
   1237      1.89  riastrad 	/* Make sure the caller may read the directory.  */
   1238      1.89  riastrad 	error = VOP_ACCESS(dvp, VEXEC, cred);
   1239      1.89  riastrad 	if (error)
   1240      1.89  riastrad 		goto fail1;
   1241      1.89  riastrad 
   1242      1.89  riastrad 	/*
   1243      1.89  riastrad 	 * The order in which we lock the source and target nodes is
   1244      1.89  riastrad 	 * irrelevant because there can only be one rename on this
   1245      1.89  riastrad 	 * directory in flight at a time, and we have it locked.
   1246      1.89  riastrad 	 */
   1247      1.89  riastrad 
   1248      1.89  riastrad 	fde = tmpfs_dir_lookup(dnode, fcnp);
   1249      1.89  riastrad 	if (fde == NULL) {
   1250      1.89  riastrad 		error = ENOENT;
   1251      1.89  riastrad 		goto fail1;
   1252      1.89  riastrad 	}
   1253      1.89  riastrad 
   1254      1.89  riastrad 	KASSERT(fde->td_node != NULL);
   1255      1.89  riastrad 	/* We ruled out `.' earlier.  */
   1256      1.89  riastrad 	KASSERT(fde->td_node != dnode);
   1257      1.89  riastrad 	/* We ruled out `..' earlier.  */
   1258      1.89  riastrad 	KASSERT(fde->td_node != dnode->tn_spec.tn_dir.tn_parent);
   1259      1.89  riastrad 	mutex_enter(&fde->td_node->tn_vlock);
   1260      1.89  riastrad 	error = tmpfs_vnode_get(mount, fde->td_node, &fvp);
   1261      1.89  riastrad 	if (error)
   1262      1.89  riastrad 		goto fail1;
   1263      1.89  riastrad 	KASSERT(fvp != NULL);
   1264      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1265      1.89  riastrad 	KASSERT(fvp != dvp);
   1266      1.89  riastrad 	KASSERT(fvp->v_mount == mount);
   1267      1.89  riastrad 
   1268      1.89  riastrad 	/* Refuse to rename a mount point.  */
   1269      1.89  riastrad 	if ((fvp->v_type == VDIR) && (fvp->v_mountedhere != NULL)) {
   1270      1.89  riastrad 		error = EBUSY;
   1271      1.89  riastrad 		goto fail2;
   1272      1.89  riastrad 	}
   1273      1.89  riastrad 
   1274      1.89  riastrad 	tde = tmpfs_dir_lookup(dnode, tcnp);
   1275      1.89  riastrad 	if (tde == NULL) {
   1276      1.89  riastrad 		tvp = NULL;
   1277      1.89  riastrad 	} else {
   1278      1.89  riastrad 		KASSERT(tde->td_node != NULL);
   1279      1.89  riastrad 		/* We ruled out `.' earlier.  */
   1280      1.89  riastrad 		KASSERT(tde->td_node != dnode);
   1281      1.89  riastrad 		/* We ruled out `..' earlier.  */
   1282      1.89  riastrad 		KASSERT(tde->td_node != dnode->tn_spec.tn_dir.tn_parent);
   1283      1.89  riastrad 		if (tde->td_node != fde->td_node) {
   1284      1.89  riastrad 			mutex_enter(&tde->td_node->tn_vlock);
   1285      1.89  riastrad 			error = tmpfs_vnode_get(mount, tde->td_node, &tvp);
   1286      1.89  riastrad 			if (error)
   1287      1.89  riastrad 				goto fail2;
   1288      1.89  riastrad 			KASSERT(tvp->v_mount == mount);
   1289      1.89  riastrad 			/* Refuse to rename over a mount point.  */
   1290      1.89  riastrad 			if ((tvp->v_type == VDIR) &&
   1291      1.89  riastrad 			    (tvp->v_mountedhere != NULL)) {
   1292      1.89  riastrad 				error = EBUSY;
   1293      1.89  riastrad 				goto fail3;
   1294      1.89  riastrad 			}
   1295      1.39      jmmv 		} else {
   1296      1.89  riastrad 			tvp = fvp;
   1297      1.89  riastrad 			vref(tvp);
   1298      1.39      jmmv 		}
   1299      1.89  riastrad 		KASSERT(tvp != NULL);
   1300      1.89  riastrad 		KASSERT(VOP_ISLOCKED(tvp) == LK_EXCLUSIVE);
   1301      1.89  riastrad 	}
   1302      1.89  riastrad 	KASSERT(tvp != dvp);
   1303      1.89  riastrad 
   1304      1.89  riastrad 	*fde_ret = fde;
   1305      1.89  riastrad 	*fvp_ret = fvp;
   1306      1.89  riastrad 	*tde_ret = tde;
   1307      1.89  riastrad 	*tvp_ret = tvp;
   1308      1.89  riastrad 	return 0;
   1309      1.89  riastrad 
   1310      1.89  riastrad fail3:	if (tvp != NULL) {
   1311      1.89  riastrad 		if (tvp != fvp)
   1312      1.89  riastrad 			vput(tvp);
   1313      1.89  riastrad 		else
   1314      1.89  riastrad 			vrele(tvp);
   1315      1.89  riastrad 	}
   1316      1.89  riastrad 
   1317      1.89  riastrad fail2:	vput(fvp);
   1318      1.89  riastrad fail1:	VOP_UNLOCK(dvp);
   1319      1.89  riastrad fail0:	return error;
   1320      1.89  riastrad }
   1321      1.89  riastrad 
   1322      1.89  riastrad /*
   1323      1.89  riastrad  * Lock and look up with separate source and target directories.
   1324      1.89  riastrad  */
   1325      1.89  riastrad static int
   1326      1.89  riastrad tmpfs_rename_enter_separate(struct mount *mount, struct tmpfs_mount *tmpfs,
   1327      1.89  riastrad     kauth_cred_t cred,
   1328      1.89  riastrad     struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
   1329      1.89  riastrad     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
   1330      1.89  riastrad     struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
   1331      1.89  riastrad     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
   1332      1.89  riastrad {
   1333      1.89  riastrad 	struct tmpfs_node *intermediate_node;
   1334      1.89  riastrad 	struct tmpfs_dirent *fde, *tde;
   1335      1.89  riastrad 	struct vnode *fvp, *tvp;
   1336      1.89  riastrad 	int error;
   1337      1.89  riastrad 
   1338      1.89  riastrad 	KASSERT(fdvp != tdvp);
   1339      1.89  riastrad 	KASSERT(fdnode != tdnode);
   1340      1.89  riastrad 
   1341      1.89  riastrad #if 0				/* XXX */
   1342      1.89  riastrad 	mutex_enter(&tmpfs->tm_rename_lock);
   1343      1.89  riastrad #endif
   1344      1.89  riastrad 
   1345      1.89  riastrad 	error = tmpfs_rename_genealogy(fdnode, tdnode, &intermediate_node);
   1346      1.89  riastrad 	if (error)
   1347      1.89  riastrad 		goto fail;
   1348      1.89  riastrad 
   1349      1.89  riastrad 	/*
   1350      1.89  riastrad 	 * intermediate_node == NULL means fdnode is not an ancestor of
   1351      1.89  riastrad 	 * tdnode.
   1352      1.89  riastrad 	 */
   1353      1.89  riastrad 	if (intermediate_node == NULL)
   1354      1.89  riastrad 		error = tmpfs_rename_lock(mount, cred, ENOTEMPTY,
   1355      1.89  riastrad 		    tdvp, tdnode, tcnp, true, &tde, &tvp,
   1356      1.89  riastrad 		    fdvp, fdnode, fcnp, false, &fde, &fvp);
   1357      1.89  riastrad 	else
   1358      1.89  riastrad 		error = tmpfs_rename_lock(mount, cred, EINVAL,
   1359      1.89  riastrad 		    fdvp, fdnode, fcnp, false, &fde, &fvp,
   1360      1.89  riastrad 		    tdvp, tdnode, tcnp, true, &tde, &tvp);
   1361      1.89  riastrad 	if (error)
   1362      1.89  riastrad 		goto fail;
   1363      1.89  riastrad 
   1364      1.89  riastrad 	KASSERT(fde != NULL);
   1365      1.89  riastrad 	KASSERT(fde->td_node != NULL);
   1366      1.89  riastrad 
   1367      1.89  riastrad 	/*
   1368      1.89  riastrad 	 * Reject rename("foo/bar", "foo/bar/baz/quux/zot").
   1369      1.89  riastrad 	 */
   1370      1.89  riastrad 	if (fde->td_node == intermediate_node) {
   1371      1.89  riastrad 		tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
   1372      1.89  riastrad 		return EINVAL;
   1373      1.89  riastrad 	}
   1374      1.89  riastrad 
   1375      1.89  riastrad 	*fde_ret = fde;
   1376      1.89  riastrad 	*fvp_ret = fvp;
   1377      1.89  riastrad 	*tde_ret = tde;
   1378      1.89  riastrad 	*tvp_ret = tvp;
   1379      1.89  riastrad 	return 0;
   1380      1.89  riastrad 
   1381      1.89  riastrad fail:
   1382      1.89  riastrad #if 0				/* XXX */
   1383      1.89  riastrad 	mutex_exit(&tmpfs->tm_rename_lock);
   1384      1.89  riastrad #endif
   1385      1.89  riastrad 	return error;
   1386      1.89  riastrad }
   1387      1.89  riastrad 
   1388      1.89  riastrad /*
   1389      1.89  riastrad  * Unlock everything we locked for rename.
   1390      1.89  riastrad  *
   1391      1.89  riastrad  * fdvp and tdvp must be referenced.
   1392      1.89  riastrad  *
   1393      1.89  riastrad  * On entry, everything is locked, and fvp and tvp referenced.
   1394      1.89  riastrad  *
   1395      1.89  riastrad  * On exit, everything is unlocked, and fvp and tvp are released.
   1396      1.89  riastrad  */
   1397      1.89  riastrad static void
   1398      1.89  riastrad tmpfs_rename_exit(struct tmpfs_mount *tmpfs,
   1399      1.89  riastrad     struct vnode *fdvp, struct vnode *fvp,
   1400      1.89  riastrad     struct vnode *tdvp, struct vnode *tvp)
   1401      1.89  riastrad {
   1402      1.89  riastrad 
   1403      1.89  riastrad 	KASSERT(tmpfs != NULL);
   1404      1.89  riastrad 	KASSERT(fdvp != NULL);
   1405      1.89  riastrad 	KASSERT(fvp != NULL);
   1406      1.89  riastrad 	KASSERT(fdvp != fvp);
   1407      1.89  riastrad 	KASSERT(fdvp != tvp);
   1408      1.89  riastrad 	KASSERT(tdvp != tvp);
   1409      1.89  riastrad 	KASSERT(tdvp != fvp);
   1410      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1411      1.89  riastrad 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1412      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1413      1.89  riastrad 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   1414      1.89  riastrad 
   1415      1.89  riastrad 	if (tvp != NULL) {
   1416      1.89  riastrad 		if (tvp != fvp)
   1417      1.89  riastrad 			vput(tvp);
   1418      1.89  riastrad 		else
   1419      1.89  riastrad 			vrele(tvp);
   1420      1.89  riastrad 	}
   1421      1.89  riastrad 	VOP_UNLOCK(tdvp);
   1422      1.89  riastrad 	vput(fvp);
   1423      1.89  riastrad 	if (fdvp != tdvp)
   1424      1.89  riastrad 		VOP_UNLOCK(fdvp);
   1425      1.89  riastrad 
   1426      1.89  riastrad #if 0				/* XXX */
   1427      1.89  riastrad 	if (fdvp != tdvp)
   1428      1.89  riastrad 		mutex_exit(&tmpfs->tm_rename_lock);
   1429      1.89  riastrad #endif
   1430      1.89  riastrad }
   1431      1.89  riastrad 
   1432      1.89  riastrad /*
   1433      1.89  riastrad  * Lock a directory, but fail if it has been rmdir'd.
   1434      1.89  riastrad  *
   1435      1.89  riastrad  * vp must be referenced.
   1436      1.89  riastrad  */
   1437      1.89  riastrad static int
   1438      1.89  riastrad tmpfs_rename_lock_directory(struct vnode *vp, struct tmpfs_node *node)
   1439      1.89  riastrad {
   1440      1.89  riastrad 
   1441      1.89  riastrad 	KASSERT(vp != NULL);
   1442      1.89  riastrad 	KASSERT(node != NULL);
   1443      1.89  riastrad 	KASSERT(node->tn_vnode == vp);
   1444      1.89  riastrad 	KASSERT(node->tn_type == VDIR);
   1445      1.89  riastrad 
   1446      1.89  riastrad 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1447      1.89  riastrad 	if (node->tn_spec.tn_dir.tn_parent == NULL) {
   1448      1.89  riastrad 		VOP_UNLOCK(vp);
   1449      1.89  riastrad 		return ENOENT;
   1450      1.39      jmmv 	}
   1451      1.39      jmmv 
   1452      1.89  riastrad 	return 0;
   1453      1.89  riastrad }
   1454      1.89  riastrad 
   1455      1.89  riastrad /*
   1456      1.89  riastrad  * Analyze the genealogy of the source and target nodes.
   1457      1.89  riastrad  *
   1458      1.89  riastrad  * On success, stores in *intermediate_node_ret either the child of
   1459      1.89  riastrad  * fdnode of which tdnode is a descendant, or null if tdnode is not a
   1460      1.89  riastrad  * descendant of fdnode at all.
   1461      1.89  riastrad  *
   1462      1.89  riastrad  * fdnode and tdnode must be unlocked and referenced.  The file
   1463      1.89  riastrad  * system's rename lock must also be held, to exclude concurrent
   1464      1.89  riastrad  * changes to the file system's genealogy other than rmdir.
   1465      1.89  riastrad  *
   1466      1.89  riastrad  * XXX This causes an extra lock/unlock of tdnode in the case when
   1467      1.89  riastrad  * we're just about to lock it again before locking anything else.
   1468      1.89  riastrad  * However, changing that requires reorganizing the code to make it
   1469      1.89  riastrad  * even more horrifically obscure.
   1470      1.89  riastrad  */
   1471      1.89  riastrad static int
   1472      1.89  riastrad tmpfs_rename_genealogy(struct tmpfs_node *fdnode, struct tmpfs_node *tdnode,
   1473      1.89  riastrad     struct tmpfs_node **intermediate_node_ret)
   1474      1.89  riastrad {
   1475      1.89  riastrad 	struct tmpfs_node *node = tdnode, *parent;
   1476      1.89  riastrad 	int error;
   1477      1.89  riastrad 
   1478      1.89  riastrad 	KASSERT(fdnode != NULL);
   1479      1.89  riastrad 	KASSERT(tdnode != NULL);
   1480      1.89  riastrad 	KASSERT(fdnode != tdnode);
   1481      1.89  riastrad 	KASSERT(intermediate_node_ret != NULL);
   1482      1.89  riastrad 
   1483      1.89  riastrad 	KASSERT(fdnode->tn_vnode != NULL);
   1484      1.89  riastrad 	KASSERT(tdnode->tn_vnode != NULL);
   1485      1.89  riastrad 	KASSERT(fdnode->tn_type == VDIR);
   1486      1.89  riastrad 	KASSERT(tdnode->tn_type == VDIR);
   1487      1.89  riastrad 
   1488      1.89  riastrad 	/*
   1489      1.89  riastrad 	 * We need to provisionally lock tdnode->tn_vnode to keep rmdir
   1490      1.89  riastrad 	 * from deleting it -- or any ancestor -- at an inopportune
   1491      1.89  riastrad 	 * moment.
   1492      1.89  riastrad 	 */
   1493      1.89  riastrad 	error = tmpfs_rename_lock_directory(tdnode->tn_vnode, tdnode);
   1494      1.89  riastrad 	if (error)
   1495      1.89  riastrad 		return error;
   1496      1.89  riastrad 
   1497      1.89  riastrad 	for (;;) {
   1498      1.89  riastrad 		parent = node->tn_spec.tn_dir.tn_parent;
   1499      1.89  riastrad 		KASSERT(parent != NULL);
   1500      1.89  riastrad 		KASSERT(parent->tn_type == VDIR);
   1501      1.89  riastrad 
   1502      1.89  riastrad 		/* Did we hit the root without finding fdnode?  */
   1503      1.89  riastrad 		if (parent == node) {
   1504      1.89  riastrad 			*intermediate_node_ret = NULL;
   1505      1.89  riastrad 			break;
   1506       1.1      jmmv 		}
   1507       1.1      jmmv 
   1508      1.89  riastrad 		/* Did we find that fdnode is an ancestor?  */
   1509      1.89  riastrad 		if (parent == fdnode) {
   1510      1.89  riastrad 			*intermediate_node_ret = node;
   1511      1.89  riastrad 			break;
   1512      1.89  riastrad 		}
   1513       1.1      jmmv 
   1514      1.89  riastrad 		/* Neither -- keep ascending the family tree.  */
   1515      1.89  riastrad 		node = parent;
   1516       1.1      jmmv 	}
   1517       1.1      jmmv 
   1518      1.89  riastrad 	VOP_UNLOCK(tdnode->tn_vnode);
   1519      1.89  riastrad 	return 0;
   1520      1.89  riastrad }
   1521      1.89  riastrad 
   1522      1.89  riastrad /*
   1523      1.89  riastrad  * Lock directories a and b, which must be distinct, and look up and
   1524      1.89  riastrad  * lock nodes a and b.  Do a first and then b.  Directory b may not be
   1525      1.89  riastrad  * an ancestor of directory a, although directory a may be an ancestor
   1526      1.89  riastrad  * of directory b.  Fail with overlap_error if node a is directory b.
   1527      1.89  riastrad  * Neither componentname may be `.' or `..'.
   1528      1.89  riastrad  *
   1529      1.89  riastrad  * a_dvp and b_dvp must be referenced.
   1530      1.89  riastrad  *
   1531      1.89  riastrad  * On entry, a_dvp and b_dvp are unlocked.
   1532      1.89  riastrad  *
   1533      1.89  riastrad  * On success,
   1534      1.89  riastrad  * . a_dvp and b_dvp are locked,
   1535      1.89  riastrad  * . *a_dirent_ret is filled with a directory entry whose node is
   1536      1.89  riastrad  *     locked and referenced,
   1537      1.89  riastrad  * . *b_vp_ret is filled with the corresponding vnode,
   1538      1.89  riastrad  * . *b_dirent_ret is filled either with null or with a directory entry
   1539      1.89  riastrad  *     whose node is locked and referenced,
   1540      1.89  riastrad  * . *b_vp is filled either with null or with the corresponding vnode,
   1541      1.89  riastrad  *     and
   1542      1.89  riastrad  * . the only pair of vnodes that may be identical is a_vp and b_vp.
   1543      1.89  riastrad  *
   1544      1.89  riastrad  * On failure, a_dvp and b_dvp are left unlocked, and *a_dirent_ret,
   1545      1.89  riastrad  * *a_vp, *b_dirent_ret, and *b_vp are left alone.
   1546      1.89  riastrad  */
   1547      1.89  riastrad static int
   1548      1.89  riastrad tmpfs_rename_lock(struct mount *mount, kauth_cred_t cred, int overlap_error,
   1549      1.89  riastrad     struct vnode *a_dvp, struct tmpfs_node *a_dnode,
   1550      1.89  riastrad     struct componentname *a_cnp, bool a_missing_ok,
   1551      1.89  riastrad     struct tmpfs_dirent **a_dirent_ret, struct vnode **a_vp_ret,
   1552      1.89  riastrad     struct vnode *b_dvp, struct tmpfs_node *b_dnode,
   1553      1.89  riastrad     struct componentname *b_cnp, bool b_missing_ok,
   1554      1.89  riastrad     struct tmpfs_dirent **b_dirent_ret, struct vnode **b_vp_ret)
   1555      1.89  riastrad {
   1556      1.89  riastrad 	struct tmpfs_dirent *a_dirent, *b_dirent;
   1557      1.89  riastrad 	struct vnode *a_vp, *b_vp;
   1558      1.89  riastrad 	int error;
   1559      1.85     rmind 
   1560      1.89  riastrad 	KASSERT(a_dvp != NULL);
   1561      1.89  riastrad 	KASSERT(a_dnode != NULL);
   1562      1.89  riastrad 	KASSERT(a_cnp != NULL);
   1563      1.89  riastrad 	KASSERT(a_dirent_ret != NULL);
   1564      1.89  riastrad 	KASSERT(a_vp_ret != NULL);
   1565      1.89  riastrad 	KASSERT(b_dvp != NULL);
   1566      1.89  riastrad 	KASSERT(b_dnode != NULL);
   1567      1.89  riastrad 	KASSERT(b_cnp != NULL);
   1568      1.89  riastrad 	KASSERT(b_dirent_ret != NULL);
   1569      1.89  riastrad 	KASSERT(b_vp_ret != NULL);
   1570      1.89  riastrad 	KASSERT(a_dvp != b_dvp);
   1571      1.89  riastrad 	KASSERT(a_dnode != b_dnode);
   1572      1.89  riastrad 	KASSERT(a_dnode->tn_vnode == a_dvp);
   1573      1.89  riastrad 	KASSERT(b_dnode->tn_vnode == b_dvp);
   1574      1.89  riastrad 	KASSERT(a_dnode->tn_type == VDIR);
   1575      1.89  riastrad 	KASSERT(b_dnode->tn_type == VDIR);
   1576      1.89  riastrad 	KASSERT(a_missing_ok != b_missing_ok);
   1577      1.89  riastrad 
   1578      1.89  riastrad 	error = tmpfs_rename_lock_directory(a_dvp, a_dnode);
   1579      1.89  riastrad 	if (error)
   1580      1.89  riastrad 		goto fail0;
   1581      1.89  riastrad 
   1582      1.89  riastrad 	/* Did we lose a race with mount?  */
   1583      1.89  riastrad 	if (a_dvp->v_mountedhere != NULL) {
   1584      1.89  riastrad 		error = EBUSY;
   1585      1.89  riastrad 		goto fail1;
   1586      1.89  riastrad 	}
   1587      1.89  riastrad 
   1588      1.89  riastrad 	/* Make sure the caller may read the directory.  */
   1589      1.89  riastrad 	error = VOP_ACCESS(a_dvp, VEXEC, cred);
   1590      1.89  riastrad 	if (error)
   1591      1.89  riastrad 		goto fail1;
   1592      1.89  riastrad 
   1593      1.89  riastrad 	a_dirent = tmpfs_dir_lookup(a_dnode, a_cnp);
   1594      1.89  riastrad 	if (a_dirent != NULL) {
   1595      1.89  riastrad 		KASSERT(a_dirent->td_node != NULL);
   1596      1.89  riastrad 		/* We ruled out `.' earlier.  */
   1597      1.89  riastrad 		KASSERT(a_dirent->td_node != a_dnode);
   1598      1.89  riastrad 		/* We ruled out `..' earlier.  */
   1599      1.89  riastrad 		KASSERT(a_dirent->td_node !=
   1600      1.89  riastrad 		    a_dnode->tn_spec.tn_dir.tn_parent);
   1601      1.89  riastrad 		if (a_dirent->td_node == b_dnode) {
   1602      1.89  riastrad 			error = overlap_error;
   1603      1.89  riastrad 			goto fail1;
   1604      1.89  riastrad 		}
   1605      1.89  riastrad 		mutex_enter(&a_dirent->td_node->tn_vlock);
   1606      1.89  riastrad 		error = tmpfs_vnode_get(mount, a_dirent->td_node, &a_vp);
   1607      1.89  riastrad 		if (error)
   1608      1.89  riastrad 			goto fail1;
   1609      1.89  riastrad 		KASSERT(a_vp->v_mount == mount);
   1610      1.89  riastrad 		/* Refuse to rename (over) a mount point.  */
   1611      1.89  riastrad 		if ((a_vp->v_type == VDIR) && (a_vp->v_mountedhere != NULL)) {
   1612      1.89  riastrad 			error = EBUSY;
   1613      1.89  riastrad 			goto fail2;
   1614      1.85     rmind 		}
   1615      1.89  riastrad 	} else if (!a_missing_ok) {
   1616      1.89  riastrad 		error = ENOENT;
   1617      1.89  riastrad 		goto fail1;
   1618      1.89  riastrad 	} else {
   1619      1.89  riastrad 		a_vp = NULL;
   1620      1.89  riastrad 	}
   1621      1.89  riastrad 	KASSERT(a_vp != a_dvp);
   1622      1.89  riastrad 	KASSERT(a_vp != b_dvp);
   1623      1.45        ad 
   1624      1.89  riastrad 	error = tmpfs_rename_lock_directory(b_dvp, b_dnode);
   1625      1.89  riastrad 	if (error)
   1626      1.89  riastrad 		goto fail2;
   1627      1.89  riastrad 
   1628      1.89  riastrad 	/* Did we lose a race with mount?  */
   1629      1.89  riastrad 	if (b_dvp->v_mountedhere != NULL) {
   1630      1.89  riastrad 		error = EBUSY;
   1631      1.89  riastrad 		goto fail3;
   1632      1.89  riastrad 	}
   1633      1.89  riastrad 
   1634      1.89  riastrad 	/* Make sure the caller may read the directory.  */
   1635      1.89  riastrad 	error = VOP_ACCESS(b_dvp, VEXEC, cred);
   1636      1.89  riastrad 	if (error)
   1637      1.89  riastrad 		goto fail3;
   1638      1.89  riastrad 
   1639      1.89  riastrad 	b_dirent = tmpfs_dir_lookup(b_dnode, b_cnp);
   1640      1.89  riastrad 	if (b_dirent != NULL) {
   1641      1.89  riastrad 		KASSERT(b_dirent->td_node != NULL);
   1642      1.89  riastrad 		/* We ruled out `.' earlier.  */
   1643      1.89  riastrad 		KASSERT(b_dirent->td_node != b_dnode);
   1644      1.89  riastrad 		/* We ruled out `..' earlier.  */
   1645      1.89  riastrad 		KASSERT(b_dirent->td_node !=
   1646      1.89  riastrad 		    b_dnode->tn_spec.tn_dir.tn_parent);
   1647      1.89  riastrad 		/* b is not an ancestor of a.  */
   1648      1.89  riastrad 		KASSERT(b_dirent->td_node != a_dnode);
   1649      1.89  riastrad 		/* But the source and target nodes might be the same.  */
   1650      1.89  riastrad 		if ((a_dirent == NULL) ||
   1651      1.89  riastrad 		    (a_dirent->td_node != b_dirent->td_node)) {
   1652      1.89  riastrad 			mutex_enter(&b_dirent->td_node->tn_vlock);
   1653      1.89  riastrad 			error = tmpfs_vnode_get(mount, b_dirent->td_node,
   1654      1.89  riastrad 			    &b_vp);
   1655      1.89  riastrad 			if (error)
   1656      1.89  riastrad 				goto fail3;
   1657      1.89  riastrad 			KASSERT(b_vp->v_mount == mount);
   1658      1.89  riastrad 			KASSERT(a_vp != b_vp);
   1659      1.89  riastrad 			/* Refuse to rename (over) a mount point.  */
   1660      1.89  riastrad 			if ((b_vp->v_type == VDIR) &&
   1661      1.89  riastrad 			    (b_vp->v_mountedhere != NULL)) {
   1662      1.89  riastrad 				error = EBUSY;
   1663      1.89  riastrad 				goto fail4;
   1664      1.89  riastrad 			}
   1665      1.89  riastrad 		} else {
   1666      1.89  riastrad 			b_vp = a_vp;
   1667      1.89  riastrad 			vref(b_vp);
   1668      1.89  riastrad 		}
   1669      1.89  riastrad 	} else if (!b_missing_ok) {
   1670      1.89  riastrad 		error = ENOENT;
   1671      1.89  riastrad 		goto fail3;
   1672      1.89  riastrad 	} else {
   1673      1.89  riastrad 		b_vp = NULL;
   1674      1.45        ad 	}
   1675      1.89  riastrad 	KASSERT(b_vp != a_dvp);
   1676      1.89  riastrad 	KASSERT(b_vp != b_dvp);
   1677      1.89  riastrad 
   1678      1.89  riastrad 	KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
   1679      1.89  riastrad 	KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
   1680      1.89  riastrad 	KASSERT(a_missing_ok || (a_dirent != NULL));
   1681      1.89  riastrad 	KASSERT(a_missing_ok || (a_dirent->td_node != NULL));
   1682      1.89  riastrad 	KASSERT(b_missing_ok || (b_dirent != NULL));
   1683      1.89  riastrad 	KASSERT(b_missing_ok || (b_dirent->td_node != NULL));
   1684      1.89  riastrad 	KASSERT((a_dirent == NULL) || (a_dirent->td_node != NULL));
   1685      1.89  riastrad 	KASSERT((a_dirent == NULL) || (a_dirent->td_node->tn_vnode == a_vp));
   1686      1.89  riastrad 	KASSERT((b_dirent == NULL) || (b_dirent->td_node != NULL));
   1687      1.89  riastrad 	KASSERT((b_dirent == NULL) || (b_dirent->td_node->tn_vnode == b_vp));
   1688      1.89  riastrad 	KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE));
   1689      1.89  riastrad 	KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE));
   1690      1.89  riastrad 
   1691      1.89  riastrad 	*a_dirent_ret = a_dirent;
   1692      1.89  riastrad 	*b_dirent_ret = b_dirent;
   1693      1.89  riastrad 	*a_vp_ret = a_vp;
   1694      1.89  riastrad 	*b_vp_ret = b_vp;
   1695      1.89  riastrad 	return 0;
   1696      1.45        ad 
   1697      1.89  riastrad fail4:	if (b_vp != NULL) {
   1698      1.89  riastrad 		KASSERT(VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE);
   1699      1.89  riastrad 		if (b_vp != a_vp)
   1700      1.89  riastrad 			vput(b_vp);
   1701      1.89  riastrad 		else
   1702      1.89  riastrad 			vrele(a_vp);
   1703      1.89  riastrad 	}
   1704       1.1      jmmv 
   1705      1.89  riastrad fail3:	KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
   1706      1.89  riastrad 	VOP_UNLOCK(b_dvp);
   1707       1.1      jmmv 
   1708      1.89  riastrad fail2:	if (a_vp != NULL) {
   1709      1.89  riastrad 		KASSERT(VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE);
   1710      1.89  riastrad 		vput(a_vp);
   1711       1.1      jmmv 	}
   1712      1.89  riastrad 
   1713      1.89  riastrad fail1:	KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
   1714      1.89  riastrad 	VOP_UNLOCK(a_dvp);
   1715      1.89  riastrad 
   1716      1.89  riastrad fail0:	/* KASSERT(VOP_ISLOCKED(a_dvp) != LK_EXCLUSIVE); */
   1717      1.89  riastrad 	/* KASSERT(VOP_ISLOCKED(b_dvp) != LK_EXCLUSIVE); */
   1718      1.89  riastrad 	/* KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) != LK_EXCLUSIVE)); */
   1719      1.89  riastrad 	/* KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) != LK_EXCLUSIVE)); */
   1720      1.89  riastrad 	return error;
   1721      1.89  riastrad }
   1722      1.89  riastrad 
   1723      1.89  riastrad /*
   1724      1.89  riastrad  * Shuffle the directory entries to move fvp from the directory fdvp
   1725      1.89  riastrad  * into the directory tdvp.  fde is fvp's directory entry in fdvp.  If
   1726      1.89  riastrad  * we are overwriting a target node, it is tvp, and tde is its
   1727      1.89  riastrad  * directory entry in tdvp.
   1728      1.89  riastrad  *
   1729      1.89  riastrad  * fdvp, fvp, tdvp, and tvp must all be locked and referenced.
   1730      1.89  riastrad  */
   1731      1.89  riastrad static void
   1732      1.89  riastrad tmpfs_rename_attachdetach(struct tmpfs_mount *tmpfs,
   1733      1.89  riastrad     struct vnode *fdvp, struct tmpfs_dirent *fde, struct vnode *fvp,
   1734      1.89  riastrad     struct vnode *tdvp, struct tmpfs_dirent *tde, struct vnode *tvp)
   1735      1.89  riastrad {
   1736      1.89  riastrad 
   1737      1.89  riastrad 	KASSERT(tmpfs != NULL);
   1738      1.89  riastrad 	KASSERT(fdvp != NULL);
   1739      1.89  riastrad 	KASSERT(fde != NULL);
   1740      1.89  riastrad 	KASSERT(fvp != NULL);
   1741      1.89  riastrad 	KASSERT(tdvp != NULL);
   1742      1.89  riastrad 	KASSERT(fde->td_node != NULL);
   1743      1.89  riastrad 	KASSERT(fde->td_node->tn_vnode == fvp);
   1744      1.89  riastrad 	KASSERT((tde == NULL) == (tvp == NULL));
   1745      1.89  riastrad 	KASSERT((tde == NULL) || (tde->td_node != NULL));
   1746      1.89  riastrad 	KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
   1747      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1748      1.89  riastrad 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1749      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1750      1.89  riastrad 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   1751      1.89  riastrad 
   1752      1.89  riastrad 	/*
   1753      1.89  riastrad 	 * If we are moving from one directory to another, detach the
   1754      1.89  riastrad 	 * source entry and reattach it to the target directory.
   1755      1.89  riastrad 	 */
   1756      1.85     rmind 	if (fdvp != tdvp) {
   1757      1.89  riastrad 		/* tmpfs_dir_detach clobbers fde->td_node, so save it.  */
   1758      1.89  riastrad 		struct tmpfs_node *fnode = fde->td_node;
   1759      1.89  riastrad 		tmpfs_dir_detach(fdvp, fde);
   1760      1.89  riastrad 		tmpfs_dir_attach(tdvp, fde, fnode);
   1761      1.89  riastrad 	} else if (tvp == NULL) {
   1762      1.89  riastrad 		/*
   1763      1.89  riastrad 		 * We are changing the directory.  tmpfs_dir_attach and
   1764      1.89  riastrad 		 * tmpfs_dir_detach note the events for us, but for
   1765      1.89  riastrad 		 * this case we don't call them, so we must note the
   1766      1.89  riastrad 		 * event explicitly.
   1767      1.89  riastrad 		 */
   1768      1.89  riastrad 		VN_KNOTE(fdvp, NOTE_WRITE);
   1769      1.83     rmind 	}
   1770      1.89  riastrad 
   1771      1.89  riastrad 	/*
   1772      1.89  riastrad 	 * If we are replacing an existing target entry, delete it.
   1773      1.89  riastrad 	 */
   1774      1.89  riastrad 	if (tde != NULL) {
   1775      1.89  riastrad 		KASSERT(tvp != NULL);
   1776      1.89  riastrad 		KASSERT(tde->td_node != NULL);
   1777      1.89  riastrad 		KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
   1778      1.89  riastrad 		if (tde->td_node->tn_type == VDIR) {
   1779      1.89  riastrad 			KASSERT(tde->td_node->tn_size == 0);
   1780      1.89  riastrad 			KASSERT(tde->td_node->tn_links == 2);
   1781      1.89  riastrad 			/* Decrement the extra link count for `.' so
   1782      1.89  riastrad 			 * the vnode will be recycled when released.  */
   1783      1.89  riastrad 			tde->td_node->tn_links--;
   1784      1.89  riastrad 		}
   1785      1.89  riastrad 		tmpfs_dir_detach(tdvp, tde);
   1786      1.89  riastrad 		tmpfs_free_dirent(tmpfs, tde);
   1787      1.85     rmind 	}
   1788      1.89  riastrad }
   1789      1.89  riastrad 
   1790      1.89  riastrad /*
   1791      1.89  riastrad  * Remove the entry de for the non-directory vp from the directory dvp.
   1792      1.89  riastrad  *
   1793      1.89  riastrad  * Everything must be locked and referenced.
   1794      1.89  riastrad  */
   1795      1.89  riastrad static int
   1796      1.89  riastrad tmpfs_do_remove(struct tmpfs_mount *tmpfs, struct vnode *dvp,
   1797      1.89  riastrad     struct tmpfs_node *dnode, struct tmpfs_dirent *de, struct vnode *vp,
   1798      1.89  riastrad     kauth_cred_t cred)
   1799      1.89  riastrad {
   1800      1.89  riastrad 	int error;
   1801      1.89  riastrad 
   1802      1.89  riastrad 	KASSERT(tmpfs != NULL);
   1803      1.89  riastrad 	KASSERT(dvp != NULL);
   1804      1.89  riastrad 	KASSERT(dnode != NULL);
   1805      1.89  riastrad 	KASSERT(de != NULL);
   1806      1.89  riastrad 	KASSERT(vp != NULL);
   1807      1.89  riastrad 	KASSERT(dnode->tn_vnode == dvp);
   1808      1.89  riastrad 	KASSERT(de->td_node != NULL);
   1809      1.89  riastrad 	KASSERT(de->td_node->tn_vnode == vp);
   1810      1.89  riastrad 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
   1811      1.89  riastrad 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1812      1.89  riastrad 
   1813      1.89  riastrad 	error = tmpfs_remove_check_possible(dnode, de->td_node);
   1814      1.89  riastrad 	if (error)
   1815      1.89  riastrad 		return error;
   1816      1.89  riastrad 
   1817      1.89  riastrad 	error = tmpfs_remove_check_permitted(cred, dnode, de->td_node);
   1818      1.89  riastrad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, vp, dvp,
   1819      1.89  riastrad 	    error);
   1820      1.89  riastrad 	if (error)
   1821      1.89  riastrad 		return error;
   1822      1.89  riastrad 
   1823      1.89  riastrad 	tmpfs_dir_detach(dvp, de);
   1824      1.89  riastrad 	tmpfs_free_dirent(tmpfs, de);
   1825      1.89  riastrad 
   1826      1.89  riastrad 	return 0;
   1827      1.89  riastrad }
   1828      1.89  riastrad 
   1829      1.89  riastrad /*
   1830      1.89  riastrad  * Check whether a rename is possible independent of credentials.
   1831      1.89  riastrad  *
   1832      1.89  riastrad  * Everything must be locked and referenced.
   1833      1.89  riastrad  */
   1834      1.89  riastrad static int
   1835      1.89  riastrad tmpfs_rename_check_possible(
   1836      1.89  riastrad     struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
   1837      1.89  riastrad     struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
   1838      1.89  riastrad {
   1839      1.89  riastrad 
   1840      1.89  riastrad 	KASSERT(fdnode != NULL);
   1841      1.89  riastrad 	KASSERT(fnode != NULL);
   1842      1.89  riastrad 	KASSERT(tdnode != NULL);
   1843      1.89  riastrad 	KASSERT(fdnode != fnode);
   1844      1.89  riastrad 	KASSERT(tdnode != tnode);
   1845      1.89  riastrad 	KASSERT(fnode != tnode);
   1846      1.89  riastrad 	KASSERT(fdnode->tn_vnode != NULL);
   1847      1.89  riastrad 	KASSERT(fnode->tn_vnode != NULL);
   1848      1.89  riastrad 	KASSERT(tdnode->tn_vnode != NULL);
   1849      1.89  riastrad 	KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
   1850      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
   1851      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
   1852      1.89  riastrad 	KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
   1853      1.89  riastrad 	KASSERT((tnode == NULL) ||
   1854      1.89  riastrad 	    (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
   1855      1.89  riastrad 
   1856      1.89  riastrad 	/*
   1857      1.89  riastrad 	 * If fdnode is immutable, we can't write to it.  If fdnode is
   1858      1.89  riastrad 	 * append-only, the only change we can make is to add entries
   1859      1.89  riastrad 	 * to it.  If fnode is immutable, we can't change the links to
   1860      1.89  riastrad 	 * it.  If fnode is append-only...well, this is what UFS does.
   1861      1.89  riastrad 	 */
   1862      1.89  riastrad 	if ((fdnode->tn_flags | fnode->tn_flags) & (IMMUTABLE | APPEND))
   1863      1.89  riastrad 		return EPERM;
   1864      1.89  riastrad 
   1865      1.89  riastrad 	/*
   1866      1.89  riastrad 	 * If tdnode is immutable, we can't write to it.  If tdnode is
   1867      1.89  riastrad 	 * append-only, we can add entries, but we can't change
   1868      1.89  riastrad 	 * existing entries.
   1869      1.89  riastrad 	 */
   1870      1.89  riastrad 	if (tdnode->tn_flags & (IMMUTABLE | (tnode? APPEND : 0)))
   1871      1.89  riastrad 		return EPERM;
   1872      1.89  riastrad 
   1873      1.89  riastrad 	/*
   1874      1.89  riastrad 	 * If tnode is immutable, we can't replace links to it.  If
   1875      1.89  riastrad 	 * tnode is append-only...well, this is what UFS does.
   1876      1.89  riastrad 	 */
   1877      1.89  riastrad 	if (tnode != NULL) {
   1878      1.89  riastrad 		KASSERT(tnode != NULL);
   1879      1.89  riastrad 		if ((tnode->tn_flags & (IMMUTABLE | APPEND)) != 0)
   1880      1.89  riastrad 			return EPERM;
   1881      1.83     rmind 	}
   1882      1.89  riastrad 
   1883      1.89  riastrad 	return 0;
   1884      1.89  riastrad }
   1885      1.89  riastrad 
   1886      1.89  riastrad /*
   1887      1.89  riastrad  * Check whether a rename is permitted given our credentials.
   1888      1.89  riastrad  *
   1889      1.89  riastrad  * Everything must be locked and referenced.
   1890      1.89  riastrad  */
   1891      1.89  riastrad static int
   1892      1.89  riastrad tmpfs_rename_check_permitted(kauth_cred_t cred,
   1893      1.89  riastrad     struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
   1894      1.89  riastrad     struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
   1895      1.89  riastrad {
   1896      1.89  riastrad 	int error;
   1897      1.89  riastrad 
   1898      1.89  riastrad 	KASSERT(fdnode != NULL);
   1899      1.89  riastrad 	KASSERT(fnode != NULL);
   1900      1.89  riastrad 	KASSERT(tdnode != NULL);
   1901      1.89  riastrad 	KASSERT(fdnode != fnode);
   1902      1.89  riastrad 	KASSERT(tdnode != tnode);
   1903      1.89  riastrad 	KASSERT(fnode != tnode);
   1904      1.89  riastrad 	KASSERT(fdnode->tn_vnode != NULL);
   1905      1.89  riastrad 	KASSERT(fnode->tn_vnode != NULL);
   1906      1.89  riastrad 	KASSERT(tdnode->tn_vnode != NULL);
   1907      1.89  riastrad 	KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
   1908      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
   1909      1.89  riastrad 	KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
   1910      1.89  riastrad 	KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
   1911      1.89  riastrad 	KASSERT((tnode == NULL) ||
   1912      1.89  riastrad 	    (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
   1913      1.89  riastrad 
   1914      1.89  riastrad 	/*
   1915      1.89  riastrad 	 * We need to remove or change an entry in the source directory.
   1916      1.89  riastrad 	 */
   1917      1.89  riastrad 	error = VOP_ACCESS(fdnode->tn_vnode, VWRITE, cred);
   1918      1.89  riastrad 	if (error)
   1919      1.89  riastrad 		return error;
   1920      1.89  riastrad 
   1921      1.89  riastrad 	/*
   1922      1.89  riastrad 	 * If we are changing directories, then we need to write to the
   1923      1.89  riastrad 	 * target directory to add or change an entry.  Also, if fnode
   1924      1.89  riastrad 	 * is a directory, we need to write to it to change its `..'
   1925      1.89  riastrad 	 * entry.
   1926      1.89  riastrad 	 */
   1927      1.89  riastrad 	if (fdnode != tdnode) {
   1928      1.89  riastrad 		error = VOP_ACCESS(tdnode->tn_vnode, VWRITE, cred);
   1929      1.89  riastrad 		if (error)
   1930      1.89  riastrad 			return error;
   1931      1.89  riastrad 		if (fnode->tn_type == VDIR) {
   1932      1.89  riastrad 			error = VOP_ACCESS(fnode->tn_vnode, VWRITE, cred);
   1933      1.89  riastrad 			if (error)
   1934      1.89  riastrad 				return error;
   1935      1.89  riastrad 		}
   1936      1.83     rmind 	}
   1937       1.1      jmmv 
   1938      1.89  riastrad 	error = tmpfs_check_sticky(cred, fdnode, fnode);
   1939      1.89  riastrad 	if (error)
   1940      1.89  riastrad 		return error;
   1941      1.89  riastrad 
   1942      1.89  riastrad 	error = tmpfs_check_sticky(cred, tdnode, tnode);
   1943      1.89  riastrad 	if (error)
   1944      1.89  riastrad 		return error;
   1945      1.89  riastrad 
   1946      1.89  riastrad 	return 0;
   1947      1.89  riastrad }
   1948      1.89  riastrad 
   1949      1.89  riastrad /*
   1950      1.89  riastrad  * Check whether removing node's entry in dnode is possible independent
   1951      1.89  riastrad  * of credentials.
   1952      1.89  riastrad  *
   1953      1.89  riastrad  * Everything must be locked and referenced.
   1954      1.89  riastrad  */
   1955      1.89  riastrad static int
   1956      1.89  riastrad tmpfs_remove_check_possible(struct tmpfs_node *dnode, struct tmpfs_node *node)
   1957      1.89  riastrad {
   1958      1.89  riastrad 
   1959      1.89  riastrad 	KASSERT(dnode != NULL);
   1960      1.89  riastrad 	KASSERT(dnode->tn_vnode != NULL);
   1961      1.89  riastrad 	KASSERT(node != NULL);
   1962      1.89  riastrad 	KASSERT(dnode != node);
   1963      1.89  riastrad 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
   1964      1.89  riastrad 	KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
   1965      1.89  riastrad 
   1966      1.89  riastrad 	/*
   1967      1.89  riastrad 	 * We want to delete the entry.  If dnode is immutable, we
   1968      1.89  riastrad 	 * can't write to it to delete the entry.  If dnode is
   1969      1.89  riastrad 	 * append-only, the only change we can make is to add entries,
   1970      1.89  riastrad 	 * so we can't delete entries.  If node is immutable, we can't
   1971      1.89  riastrad 	 * change the links to it, so we can't delete the entry.  If
   1972      1.89  riastrad 	 * node is append-only...well, this is what UFS does.
   1973      1.89  riastrad 	 */
   1974      1.89  riastrad 	if ((dnode->tn_flags | node->tn_flags) & (IMMUTABLE | APPEND))
   1975      1.89  riastrad 		return EPERM;
   1976      1.89  riastrad 
   1977      1.89  riastrad 	return 0;
   1978      1.89  riastrad }
   1979      1.89  riastrad 
   1980      1.89  riastrad /*
   1981      1.89  riastrad  * Check whether removing node's entry in dnode is permitted given our
   1982      1.89  riastrad  * credentials.
   1983      1.89  riastrad  *
   1984      1.89  riastrad  * Everything must be locked and referenced.
   1985      1.89  riastrad  */
   1986      1.89  riastrad static int
   1987      1.89  riastrad tmpfs_remove_check_permitted(kauth_cred_t cred,
   1988      1.89  riastrad     struct tmpfs_node *dnode, struct tmpfs_node *node)
   1989      1.89  riastrad {
   1990      1.89  riastrad 	int error;
   1991      1.89  riastrad 
   1992      1.89  riastrad 	KASSERT(dnode != NULL);
   1993      1.89  riastrad 	KASSERT(dnode->tn_vnode != NULL);
   1994      1.89  riastrad 	KASSERT(node != NULL);
   1995      1.89  riastrad 	KASSERT(dnode != node);
   1996      1.89  riastrad 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
   1997      1.89  riastrad 	KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
   1998      1.89  riastrad 
   1999      1.89  riastrad 	/*
   2000      1.89  riastrad 	 * Check whether we are permitted to write to the source
   2001      1.89  riastrad 	 * directory in order to delete an entry from it.
   2002      1.89  riastrad 	 */
   2003      1.89  riastrad 	error = VOP_ACCESS(dnode->tn_vnode, VWRITE, cred);
   2004      1.89  riastrad 	if (error)
   2005      1.89  riastrad 		return error;
   2006      1.89  riastrad 
   2007      1.89  riastrad 	error = tmpfs_check_sticky(cred, dnode, node);
   2008      1.89  riastrad 	if (error)
   2009      1.89  riastrad 		return error;
   2010      1.89  riastrad 
   2011      1.89  riastrad 	return 0;
   2012      1.89  riastrad }
   2013      1.89  riastrad 
   2014      1.89  riastrad /*
   2015      1.89  riastrad  * Check whether we may change an entry in a sticky directory.  If the
   2016      1.89  riastrad  * directory is sticky, the user must own either the directory or, if
   2017      1.89  riastrad  * it exists, the node, in order to change the entry.
   2018      1.89  riastrad  *
   2019      1.89  riastrad  * Everything must be locked and referenced.
   2020      1.89  riastrad  */
   2021      1.89  riastrad static int
   2022      1.89  riastrad tmpfs_check_sticky(kauth_cred_t cred,
   2023      1.89  riastrad     struct tmpfs_node *dnode, struct tmpfs_node *node)
   2024      1.89  riastrad {
   2025       1.1      jmmv 
   2026      1.89  riastrad 	KASSERT(dnode != NULL);
   2027      1.89  riastrad 	KASSERT(dnode->tn_vnode != NULL);
   2028      1.89  riastrad 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
   2029      1.89  riastrad 	KASSERT((node == NULL) || (node->tn_vnode != NULL));
   2030      1.89  riastrad 	KASSERT((node == NULL) ||
   2031      1.89  riastrad 	    (VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE));
   2032      1.89  riastrad 
   2033  1.93.4.5       mrg 	if (node == NULL)
   2034  1.93.4.5       mrg 		return 0;
   2035  1.93.4.5       mrg 
   2036      1.89  riastrad 	if (dnode->tn_mode & S_ISTXT) {
   2037  1.93.4.5       mrg 		if (kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
   2038  1.93.4.5       mrg 		    node->tn_vnode, dnode->tn_vnode, genfs_can_sticky(cred,
   2039  1.93.4.5       mrg 		    dnode->tn_uid, node->tn_uid)) != 0)
   2040  1.93.4.5       mrg 			return EPERM;
   2041      1.70     rmind 	}
   2042      1.89  riastrad 
   2043      1.89  riastrad 	return 0;
   2044       1.1      jmmv }
   2045       1.1      jmmv 
   2046       1.1      jmmv int
   2047       1.1      jmmv tmpfs_mkdir(void *v)
   2048       1.1      jmmv {
   2049      1.83     rmind 	struct vop_mkdir_args /* {
   2050      1.83     rmind 		struct vnode		*a_dvp;
   2051      1.83     rmind 		struct vnode		**a_vpp;
   2052      1.83     rmind 		struct componentname	*a_cnp;
   2053      1.83     rmind 		struct vattr		*a_vap;
   2054      1.83     rmind 	} */ *ap = v;
   2055      1.83     rmind 	vnode_t *dvp = ap->a_dvp;
   2056      1.83     rmind 	vnode_t **vpp = ap->a_vpp;
   2057      1.83     rmind 	struct componentname *cnp = ap->a_cnp;
   2058      1.83     rmind 	struct vattr *vap = ap->a_vap;
   2059       1.1      jmmv 
   2060       1.1      jmmv 	KASSERT(vap->va_type == VDIR);
   2061       1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
   2062       1.1      jmmv }
   2063       1.1      jmmv 
   2064       1.1      jmmv int
   2065       1.1      jmmv tmpfs_rmdir(void *v)
   2066       1.1      jmmv {
   2067      1.83     rmind 	struct vop_rmdir_args /* {
   2068      1.83     rmind 		struct vnode		*a_dvp;
   2069      1.83     rmind 		struct vnode		*a_vp;
   2070      1.83     rmind 		struct componentname	*a_cnp;
   2071      1.83     rmind 	} */ *ap = v;
   2072      1.83     rmind 	vnode_t *dvp = ap->a_dvp;
   2073      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2074      1.83     rmind 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   2075      1.83     rmind 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
   2076      1.83     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
   2077      1.83     rmind 	tmpfs_dirent_t *de;
   2078      1.83     rmind 	int error = 0;
   2079       1.1      jmmv 
   2080       1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
   2081       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   2082      1.83     rmind 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
   2083       1.1      jmmv 
   2084      1.83     rmind 	/*
   2085      1.90   hannken 	 * Directories with more than two non-whiteout
   2086      1.90   hannken 	 * entries ('.' and '..') cannot be removed.
   2087      1.83     rmind 	 */
   2088      1.34     pooka 	if (node->tn_size > 0) {
   2089      1.90   hannken 		KASSERT(error == 0);
   2090      1.90   hannken 		TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
   2091      1.90   hannken 			if (de->td_node != TMPFS_NODE_WHITEOUT) {
   2092      1.90   hannken 				error = ENOTEMPTY;
   2093      1.90   hannken 				break;
   2094      1.90   hannken 			}
   2095      1.90   hannken 		}
   2096      1.90   hannken 		if (error)
   2097      1.90   hannken 			goto out;
   2098      1.34     pooka 	}
   2099      1.34     pooka 
   2100      1.85     rmind 	/* Lookup the directory entry (check the cached hint first). */
   2101      1.85     rmind 	de = tmpfs_dir_cached(node);
   2102      1.85     rmind 	if (de == NULL) {
   2103      1.85     rmind 		struct componentname *cnp = ap->a_cnp;
   2104      1.85     rmind 		de = tmpfs_dir_lookup(dnode, cnp);
   2105      1.85     rmind 	}
   2106      1.83     rmind 	KASSERT(de && de->td_node == node);
   2107       1.1      jmmv 
   2108       1.1      jmmv 	/* Check flags to see if we are allowed to remove the directory. */
   2109       1.1      jmmv 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
   2110       1.1      jmmv 		error = EPERM;
   2111       1.1      jmmv 		goto out;
   2112       1.1      jmmv 	}
   2113       1.1      jmmv 
   2114      1.85     rmind 	/* Decrement the link count for the virtual '.' entry. */
   2115       1.1      jmmv 	node->tn_links--;
   2116      1.83     rmind 	node->tn_status |= TMPFS_NODE_STATUSALL;
   2117       1.1      jmmv 
   2118      1.86     rmind 	/* Detach the directory entry from the directory. */
   2119      1.86     rmind 	tmpfs_dir_detach(dvp, de);
   2120      1.86     rmind 
   2121      1.83     rmind 	/* Purge the cache for parent. */
   2122      1.83     rmind 	cache_purge(dvp);
   2123       1.1      jmmv 
   2124      1.83     rmind 	/*
   2125      1.90   hannken 	 * Destroy the directory entry or replace it with a whiteout.
   2126      1.90   hannken 	 * Note: the inode referred by it will not be destroyed
   2127      1.90   hannken 	 * until the vnode is reclaimed.
   2128      1.83     rmind 	 */
   2129      1.90   hannken 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
   2130      1.90   hannken 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
   2131      1.90   hannken 	else
   2132      1.90   hannken 		tmpfs_free_dirent(tmp, de);
   2133      1.90   hannken 
   2134      1.90   hannken 	/* Destroy the whiteout entries from the node. */
   2135      1.90   hannken 	while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
   2136      1.90   hannken 		KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
   2137      1.90   hannken 		tmpfs_dir_detach(vp, de);
   2138      1.90   hannken 		tmpfs_free_dirent(tmp, de);
   2139      1.90   hannken 	}
   2140      1.90   hannken 
   2141      1.45        ad 	KASSERT(node->tn_links == 0);
   2142      1.83     rmind out:
   2143      1.40    dyoung 	/* Release the nodes. */
   2144      1.40    dyoung 	vput(dvp);
   2145       1.1      jmmv 	vput(vp);
   2146       1.1      jmmv 	return error;
   2147       1.1      jmmv }
   2148       1.1      jmmv 
   2149       1.1      jmmv int
   2150       1.1      jmmv tmpfs_symlink(void *v)
   2151       1.1      jmmv {
   2152      1.83     rmind 	struct vop_symlink_args /* {
   2153      1.83     rmind 		struct vnode		*a_dvp;
   2154      1.83     rmind 		struct vnode		**a_vpp;
   2155      1.83     rmind 		struct componentname	*a_cnp;
   2156      1.83     rmind 		struct vattr		*a_vap;
   2157      1.83     rmind 		char			*a_target;
   2158      1.83     rmind 	} */ *ap = v;
   2159      1.83     rmind 	vnode_t *dvp = ap->a_dvp;
   2160      1.83     rmind 	vnode_t **vpp = ap->a_vpp;
   2161      1.83     rmind 	struct componentname *cnp = ap->a_cnp;
   2162      1.83     rmind 	struct vattr *vap = ap->a_vap;
   2163      1.83     rmind 	char *target = ap->a_target;
   2164       1.1      jmmv 
   2165       1.1      jmmv 	KASSERT(vap->va_type == VLNK);
   2166       1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
   2167       1.1      jmmv }
   2168       1.1      jmmv 
   2169       1.1      jmmv int
   2170       1.1      jmmv tmpfs_readdir(void *v)
   2171       1.1      jmmv {
   2172      1.83     rmind 	struct vop_readdir_args /* {
   2173      1.83     rmind 		struct vnode	*a_vp;
   2174      1.83     rmind 		struct uio	*a_uio;
   2175      1.83     rmind 		kauth_cred_t	a_cred;
   2176      1.83     rmind 		int		*a_eofflag;
   2177      1.83     rmind 		off_t		**a_cookies;
   2178      1.83     rmind 		int		*ncookies;
   2179      1.83     rmind 	} */ *ap = v;
   2180      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2181      1.83     rmind 	struct uio *uio = ap->a_uio;
   2182      1.83     rmind 	int *eofflag = ap->a_eofflag;
   2183      1.83     rmind 	off_t **cookies = ap->a_cookies;
   2184      1.83     rmind 	int *ncookies = ap->a_ncookies;
   2185      1.83     rmind 	off_t startoff, cnt;
   2186      1.83     rmind 	tmpfs_node_t *node;
   2187       1.1      jmmv 	int error;
   2188       1.1      jmmv 
   2189       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   2190       1.1      jmmv 
   2191       1.1      jmmv 	/* This operation only makes sense on directory nodes. */
   2192       1.1      jmmv 	if (vp->v_type != VDIR) {
   2193      1.83     rmind 		return ENOTDIR;
   2194       1.1      jmmv 	}
   2195       1.1      jmmv 	node = VP_TO_TMPFS_DIR(vp);
   2196       1.1      jmmv 	startoff = uio->uio_offset;
   2197      1.83     rmind 	cnt = 0;
   2198  1.93.4.4       mrg 	if (node->tn_links == 0) {
   2199  1.93.4.4       mrg 		error = 0;
   2200  1.93.4.4       mrg 		goto out;
   2201  1.93.4.4       mrg 	}
   2202       1.1      jmmv 
   2203      1.10      yamt 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
   2204       1.1      jmmv 		error = tmpfs_dir_getdotdent(node, uio);
   2205      1.83     rmind 		if (error != 0) {
   2206      1.83     rmind 			if (error == -1)
   2207      1.83     rmind 				error = 0;
   2208      1.83     rmind 			goto out;
   2209      1.83     rmind 		}
   2210      1.10      yamt 		cnt++;
   2211       1.1      jmmv 	}
   2212      1.10      yamt 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
   2213       1.1      jmmv 		error = tmpfs_dir_getdotdotdent(node, uio);
   2214      1.83     rmind 		if (error != 0) {
   2215      1.83     rmind 			if (error == -1)
   2216      1.83     rmind 				error = 0;
   2217      1.83     rmind 			goto out;
   2218      1.83     rmind 		}
   2219      1.10      yamt 		cnt++;
   2220       1.1      jmmv 	}
   2221      1.10      yamt 	error = tmpfs_dir_getdents(node, uio, &cnt);
   2222      1.83     rmind 	if (error == -1) {
   2223       1.1      jmmv 		error = 0;
   2224      1.83     rmind 	}
   2225       1.1      jmmv 	KASSERT(error >= 0);
   2226      1.83     rmind out:
   2227      1.83     rmind 	if (eofflag != NULL) {
   2228      1.83     rmind 		*eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
   2229      1.83     rmind 	}
   2230      1.83     rmind 	if (error || cookies == NULL || ncookies == NULL) {
   2231      1.83     rmind 		return error;
   2232      1.83     rmind 	}
   2233       1.1      jmmv 
   2234      1.83     rmind 	/* Update NFS-related variables, if any. */
   2235      1.83     rmind 	off_t i, off = startoff;
   2236      1.83     rmind 	tmpfs_dirent_t *de = NULL;
   2237      1.83     rmind 
   2238      1.83     rmind 	*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   2239      1.83     rmind 	*ncookies = cnt;
   2240      1.83     rmind 
   2241      1.83     rmind 	for (i = 0; i < cnt; i++) {
   2242      1.83     rmind 		KASSERT(off != TMPFS_DIRCOOKIE_EOF);
   2243      1.83     rmind 		if (off != TMPFS_DIRCOOKIE_DOT) {
   2244      1.83     rmind 			if (off == TMPFS_DIRCOOKIE_DOTDOT) {
   2245      1.83     rmind 				de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
   2246      1.83     rmind 			} else if (de != NULL) {
   2247      1.83     rmind 				de = TAILQ_NEXT(de, td_entries);
   2248      1.10      yamt 			} else {
   2249      1.83     rmind 				de = tmpfs_dir_lookupbycookie(node, off);
   2250      1.83     rmind 				KASSERT(de != NULL);
   2251      1.83     rmind 				de = TAILQ_NEXT(de, td_entries);
   2252      1.83     rmind 			}
   2253      1.83     rmind 			if (de == NULL) {
   2254      1.83     rmind 				off = TMPFS_DIRCOOKIE_EOF;
   2255      1.83     rmind 			} else {
   2256      1.83     rmind 				off = tmpfs_dircookie(de);
   2257      1.10      yamt 			}
   2258      1.83     rmind 		} else {
   2259      1.83     rmind 			off = TMPFS_DIRCOOKIE_DOTDOT;
   2260      1.10      yamt 		}
   2261      1.83     rmind 		(*cookies)[i] = off;
   2262       1.1      jmmv 	}
   2263      1.83     rmind 	KASSERT(uio->uio_offset == off);
   2264       1.1      jmmv 	return error;
   2265       1.1      jmmv }
   2266       1.1      jmmv 
   2267       1.1      jmmv int
   2268       1.1      jmmv tmpfs_readlink(void *v)
   2269       1.1      jmmv {
   2270      1.83     rmind 	struct vop_readlink_args /* {
   2271      1.83     rmind 		struct vnode	*a_vp;
   2272      1.83     rmind 		struct uio	*a_uio;
   2273      1.83     rmind 		kauth_cred_t	a_cred;
   2274      1.83     rmind 	} */ *ap = v;
   2275      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2276      1.83     rmind 	struct uio *uio = ap->a_uio;
   2277      1.83     rmind 	tmpfs_node_t *node;
   2278       1.1      jmmv 	int error;
   2279       1.1      jmmv 
   2280       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   2281       1.1      jmmv 	KASSERT(uio->uio_offset == 0);
   2282       1.1      jmmv 	KASSERT(vp->v_type == VLNK);
   2283       1.1      jmmv 
   2284       1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   2285      1.21      jmmv 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   2286      1.21      jmmv 	    MIN(node->tn_size, uio->uio_resid), uio);
   2287       1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
   2288       1.1      jmmv 
   2289       1.1      jmmv 	return error;
   2290       1.1      jmmv }
   2291       1.1      jmmv 
   2292       1.1      jmmv int
   2293       1.1      jmmv tmpfs_inactive(void *v)
   2294       1.1      jmmv {
   2295      1.82     rmind 	struct vop_inactive_args /* {
   2296      1.82     rmind 		struct vnode *a_vp;
   2297      1.82     rmind 		bool *a_recycle;
   2298      1.82     rmind 	} */ *ap = v;
   2299      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2300      1.83     rmind 	tmpfs_node_t *node;
   2301       1.1      jmmv 
   2302       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   2303       1.1      jmmv 
   2304       1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   2305      1.82     rmind 	*ap->a_recycle = (node->tn_links == 0);
   2306      1.71   hannken 	VOP_UNLOCK(vp);
   2307       1.1      jmmv 
   2308       1.1      jmmv 	return 0;
   2309       1.1      jmmv }
   2310       1.1      jmmv 
   2311       1.1      jmmv int
   2312       1.1      jmmv tmpfs_reclaim(void *v)
   2313       1.1      jmmv {
   2314      1.82     rmind 	struct vop_reclaim_args /* {
   2315      1.82     rmind 		struct vnode *a_vp;
   2316      1.82     rmind 	} */ *ap = v;
   2317      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2318      1.83     rmind 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
   2319      1.83     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   2320      1.85     rmind 	bool racing;
   2321       1.1      jmmv 
   2322      1.82     rmind 	/* Disassociate inode from vnode. */
   2323      1.85     rmind 	mutex_enter(&node->tn_vlock);
   2324      1.85     rmind 	node->tn_vnode = NULL;
   2325      1.85     rmind 	vp->v_data = NULL;
   2326      1.85     rmind 	/* Check if tmpfs_vnode_get() is racing with us. */
   2327      1.85     rmind 	racing = TMPFS_NODE_RECLAIMING(node);
   2328      1.85     rmind 	mutex_exit(&node->tn_vlock);
   2329       1.1      jmmv 
   2330      1.85     rmind 	/*
   2331      1.85     rmind 	 * If inode is not referenced, i.e. no links, then destroy it.
   2332      1.85     rmind 	 * Note: if racing - inode is about to get a new vnode, leave it.
   2333      1.85     rmind 	 */
   2334      1.85     rmind 	if (node->tn_links == 0 && !racing) {
   2335       1.1      jmmv 		tmpfs_free_node(tmp, node);
   2336      1.82     rmind 	}
   2337       1.1      jmmv 	return 0;
   2338       1.1      jmmv }
   2339       1.1      jmmv 
   2340       1.1      jmmv int
   2341       1.1      jmmv tmpfs_pathconf(void *v)
   2342       1.1      jmmv {
   2343      1.83     rmind 	struct vop_pathconf_args /* {
   2344      1.83     rmind 		struct vnode	*a_vp;
   2345      1.83     rmind 		int		a_name;
   2346      1.83     rmind 		register_t	*a_retval;
   2347      1.83     rmind 	} */ *ap = v;
   2348      1.83     rmind 	const int name = ap->a_name;
   2349      1.83     rmind 	register_t *retval = ap->a_retval;
   2350      1.83     rmind 	int error = 0;
   2351       1.1      jmmv 
   2352       1.1      jmmv 	switch (name) {
   2353       1.1      jmmv 	case _PC_LINK_MAX:
   2354       1.1      jmmv 		*retval = LINK_MAX;
   2355       1.1      jmmv 		break;
   2356       1.1      jmmv 	case _PC_NAME_MAX:
   2357      1.92  christos 		*retval = TMPFS_MAXNAMLEN;
   2358       1.1      jmmv 		break;
   2359       1.1      jmmv 	case _PC_PATH_MAX:
   2360       1.1      jmmv 		*retval = PATH_MAX;
   2361       1.1      jmmv 		break;
   2362       1.1      jmmv 	case _PC_PIPE_BUF:
   2363       1.1      jmmv 		*retval = PIPE_BUF;
   2364       1.1      jmmv 		break;
   2365       1.1      jmmv 	case _PC_CHOWN_RESTRICTED:
   2366       1.1      jmmv 		*retval = 1;
   2367       1.1      jmmv 		break;
   2368       1.1      jmmv 	case _PC_NO_TRUNC:
   2369       1.1      jmmv 		*retval = 1;
   2370       1.1      jmmv 		break;
   2371       1.1      jmmv 	case _PC_SYNC_IO:
   2372       1.1      jmmv 		*retval = 1;
   2373       1.1      jmmv 		break;
   2374       1.1      jmmv 	case _PC_FILESIZEBITS:
   2375      1.85     rmind 		*retval = sizeof(off_t) * CHAR_BIT;
   2376       1.1      jmmv 		break;
   2377       1.1      jmmv 	default:
   2378       1.1      jmmv 		error = EINVAL;
   2379       1.1      jmmv 	}
   2380       1.1      jmmv 	return error;
   2381       1.1      jmmv }
   2382       1.1      jmmv 
   2383       1.1      jmmv int
   2384      1.15      jmmv tmpfs_advlock(void *v)
   2385      1.15      jmmv {
   2386      1.83     rmind 	struct vop_advlock_args /* {
   2387      1.83     rmind 		struct vnode	*a_vp;
   2388      1.83     rmind 		void *		a_id;
   2389      1.83     rmind 		int		a_op;
   2390      1.83     rmind 		struct flock	*a_fl;
   2391      1.83     rmind 		int		a_flags;
   2392      1.83     rmind 	} */ *ap = v;
   2393      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2394      1.83     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   2395      1.15      jmmv 
   2396      1.15      jmmv 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   2397      1.15      jmmv }
   2398      1.15      jmmv 
   2399      1.15      jmmv int
   2400       1.1      jmmv tmpfs_getpages(void *v)
   2401       1.1      jmmv {
   2402      1.78     rmind 	struct vop_getpages_args /* {
   2403      1.78     rmind 		struct vnode *a_vp;
   2404      1.78     rmind 		voff_t a_offset;
   2405      1.78     rmind 		struct vm_page **a_m;
   2406      1.78     rmind 		int *a_count;
   2407      1.78     rmind 		int a_centeridx;
   2408      1.78     rmind 		vm_prot_t a_access_type;
   2409      1.78     rmind 		int a_advice;
   2410      1.78     rmind 		int a_flags;
   2411      1.78     rmind 	} */ * const ap = v;
   2412      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2413      1.78     rmind 	const voff_t offset = ap->a_offset;
   2414      1.78     rmind 	struct vm_page **pgs = ap->a_m;
   2415      1.78     rmind 	const int centeridx = ap->a_centeridx;
   2416      1.78     rmind 	const vm_prot_t access_type = ap->a_access_type;
   2417      1.78     rmind 	const int advice = ap->a_advice;
   2418      1.78     rmind 	const int flags = ap->a_flags;
   2419      1.78     rmind 	int error, npages = *ap->a_count;
   2420      1.83     rmind 	tmpfs_node_t *node;
   2421       1.6      yamt 	struct uvm_object *uobj;
   2422       1.1      jmmv 
   2423       1.6      yamt 	KASSERT(vp->v_type == VREG);
   2424      1.87     rmind 	KASSERT(mutex_owned(vp->v_interlock));
   2425       1.1      jmmv 
   2426       1.7      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   2427      1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
   2428       1.1      jmmv 
   2429      1.78     rmind 	/*
   2430      1.78     rmind 	 * Currently, PGO_PASTEOF is not supported.
   2431      1.78     rmind 	 */
   2432       1.9      yamt 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   2433       1.9      yamt 		if ((flags & PGO_LOCKED) == 0)
   2434      1.87     rmind 			mutex_exit(vp->v_interlock);
   2435       1.9      yamt 		return EINVAL;
   2436       1.9      yamt 	}
   2437       1.9      yamt 
   2438       1.9      yamt 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   2439       1.9      yamt 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   2440       1.9      yamt 	}
   2441       1.9      yamt 
   2442       1.7      jmmv 	if ((flags & PGO_LOCKED) != 0)
   2443       1.6      yamt 		return EBUSY;
   2444       1.1      jmmv 
   2445       1.6      yamt 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   2446       1.7      jmmv 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   2447       1.6      yamt 			node->tn_status |= TMPFS_NODE_ACCESSED;
   2448       1.7      jmmv 
   2449      1.93  christos 		if ((access_type & VM_PROT_WRITE) != 0) {
   2450       1.6      yamt 			node->tn_status |= TMPFS_NODE_MODIFIED;
   2451      1.93  christos 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
   2452      1.93  christos 				node->tn_status |= TMPFS_NODE_ACCESSED;
   2453      1.93  christos 		}
   2454       1.1      jmmv 	}
   2455       1.1      jmmv 
   2456      1.28      jmmv 	/*
   2457      1.78     rmind 	 * Invoke the pager.
   2458      1.49      jmmv 	 *
   2459      1.78     rmind 	 * Clean the array of pages before.  XXX: PR/32166
   2460      1.78     rmind 	 * Note that vnode lock is shared with underlying UVM object.
   2461      1.28      jmmv 	 */
   2462      1.78     rmind 	if (pgs) {
   2463      1.78     rmind 		memset(pgs, 0, sizeof(struct vm_pages *) * npages);
   2464      1.78     rmind 	}
   2465      1.87     rmind 	KASSERT(vp->v_interlock == uobj->vmobjlock);
   2466      1.87     rmind 
   2467      1.78     rmind 	error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
   2468      1.28      jmmv 	    access_type, advice, flags | PGO_ALLPAGES);
   2469      1.78     rmind 
   2470      1.28      jmmv #if defined(DEBUG)
   2471      1.78     rmind 	if (!error && pgs) {
   2472      1.80      matt 		for (int i = 0; i < npages; i++) {
   2473      1.78     rmind 			KASSERT(pgs[i] != NULL);
   2474      1.78     rmind 		}
   2475      1.28      jmmv 	}
   2476      1.28      jmmv #endif
   2477       1.6      yamt 	return error;
   2478       1.6      yamt }
   2479       1.6      yamt 
   2480       1.6      yamt int
   2481       1.6      yamt tmpfs_putpages(void *v)
   2482       1.6      yamt {
   2483      1.78     rmind 	struct vop_putpages_args /* {
   2484      1.78     rmind 		struct vnode *a_vp;
   2485      1.78     rmind 		voff_t a_offlo;
   2486      1.78     rmind 		voff_t a_offhi;
   2487      1.78     rmind 		int a_flags;
   2488      1.78     rmind 	} */ * const ap = v;
   2489      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2490      1.78     rmind 	const voff_t offlo = ap->a_offlo;
   2491      1.78     rmind 	const voff_t offhi = ap->a_offhi;
   2492      1.78     rmind 	const int flags = ap->a_flags;
   2493      1.83     rmind 	tmpfs_node_t *node;
   2494       1.6      yamt 	struct uvm_object *uobj;
   2495      1.78     rmind 	int error;
   2496       1.6      yamt 
   2497      1.87     rmind 	KASSERT(mutex_owned(vp->v_interlock));
   2498       1.7      jmmv 
   2499       1.6      yamt 	if (vp->v_type != VREG) {
   2500      1.87     rmind 		mutex_exit(vp->v_interlock);
   2501       1.6      yamt 		return 0;
   2502       1.1      jmmv 	}
   2503       1.1      jmmv 
   2504      1.87     rmind 	node = VP_TO_TMPFS_NODE(vp);
   2505      1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
   2506       1.6      yamt 
   2507      1.87     rmind 	KASSERT(vp->v_interlock == uobj->vmobjlock);
   2508       1.7      jmmv 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   2509       1.6      yamt 
   2510       1.6      yamt 	/* XXX mtime */
   2511       1.1      jmmv 
   2512       1.1      jmmv 	return error;
   2513       1.1      jmmv }
   2514      1.76     pooka 
   2515      1.76     pooka int
   2516      1.76     pooka tmpfs_whiteout(void *v)
   2517      1.76     pooka {
   2518      1.83     rmind 	struct vop_whiteout_args /* {
   2519      1.83     rmind 		struct vnode		*a_dvp;
   2520      1.83     rmind 		struct componentname	*a_cnp;
   2521      1.83     rmind 		int			a_flags;
   2522      1.83     rmind 	} */ *ap = v;
   2523      1.83     rmind 	vnode_t *dvp = ap->a_dvp;
   2524      1.83     rmind 	struct componentname *cnp = ap->a_cnp;
   2525      1.83     rmind 	const int flags = ap->a_flags;
   2526      1.83     rmind 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
   2527      1.83     rmind 	tmpfs_dirent_t *de;
   2528      1.76     pooka 	int error;
   2529      1.76     pooka 
   2530      1.76     pooka 	switch (flags) {
   2531      1.76     pooka 	case LOOKUP:
   2532      1.76     pooka 		break;
   2533      1.76     pooka 	case CREATE:
   2534      1.85     rmind 		error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
   2535      1.85     rmind 		    cnp->cn_namelen, &de);
   2536      1.76     pooka 		if (error)
   2537      1.76     pooka 			return error;
   2538      1.85     rmind 		tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
   2539      1.76     pooka 		break;
   2540      1.76     pooka 	case DELETE:
   2541      1.76     pooka 		cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
   2542      1.76     pooka 		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
   2543      1.76     pooka 		if (de == NULL)
   2544      1.76     pooka 			return ENOENT;
   2545      1.76     pooka 		tmpfs_dir_detach(dvp, de);
   2546      1.85     rmind 		tmpfs_free_dirent(tmp, de);
   2547      1.76     pooka 		break;
   2548      1.76     pooka 	}
   2549      1.83     rmind 	return 0;
   2550      1.83     rmind }
   2551      1.76     pooka 
   2552      1.83     rmind int
   2553      1.83     rmind tmpfs_print(void *v)
   2554      1.83     rmind {
   2555      1.83     rmind 	struct vop_print_args /* {
   2556      1.83     rmind 		struct vnode	*a_vp;
   2557      1.83     rmind 	} */ *ap = v;
   2558      1.83     rmind 	vnode_t *vp = ap->a_vp;
   2559      1.83     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   2560      1.83     rmind 
   2561      1.83     rmind 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
   2562      1.83     rmind 	    "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
   2563      1.83     rmind 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
   2564      1.83     rmind 	    node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
   2565      1.83     rmind 	if (vp->v_type == VFIFO) {
   2566      1.83     rmind 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   2567      1.83     rmind 	}
   2568      1.83     rmind 	printf("\n");
   2569      1.76     pooka 	return 0;
   2570      1.76     pooka }
   2571