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