Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_rename.c revision 1.2.2.4
      1  1.2.2.4  yamt /*	$NetBSD: tmpfs_rename.c,v 1.2.2.4 2014/05/22 11:41:02 yamt Exp $	*/
      2  1.2.2.2  yamt 
      3  1.2.2.2  yamt /*-
      4  1.2.2.2  yamt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.2.2.2  yamt  * All rights reserved.
      6  1.2.2.2  yamt  *
      7  1.2.2.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2  yamt  * by Taylor R Campbell.
      9  1.2.2.2  yamt  *
     10  1.2.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.2.2.2  yamt  * modification, are permitted provided that the following conditions
     12  1.2.2.2  yamt  * are met:
     13  1.2.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.2.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.2.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.2.2.2  yamt  *
     19  1.2.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.2.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.2.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.2.2  yamt  */
     31  1.2.2.2  yamt 
     32  1.2.2.2  yamt /*
     33  1.2.2.2  yamt  * tmpfs rename
     34  1.2.2.2  yamt  */
     35  1.2.2.2  yamt 
     36  1.2.2.2  yamt #include <sys/cdefs.h>
     37  1.2.2.4  yamt __KERNEL_RCSID(0, "$NetBSD: tmpfs_rename.c,v 1.2.2.4 2014/05/22 11:41:02 yamt Exp $");
     38  1.2.2.2  yamt 
     39  1.2.2.2  yamt #include <sys/param.h>
     40  1.2.2.2  yamt #include <sys/errno.h>
     41  1.2.2.2  yamt #include <sys/kauth.h>
     42  1.2.2.2  yamt #include <sys/mount.h>
     43  1.2.2.2  yamt #include <sys/namei.h>
     44  1.2.2.2  yamt #include <sys/stat.h>
     45  1.2.2.2  yamt #include <sys/vnode.h>
     46  1.2.2.2  yamt #include <sys/vnode_if.h>
     47  1.2.2.2  yamt 
     48  1.2.2.2  yamt #include <miscfs/genfs/genfs.h>
     49  1.2.2.2  yamt 
     50  1.2.2.2  yamt #include <fs/tmpfs/tmpfs_vnops.h>
     51  1.2.2.2  yamt #include <fs/tmpfs/tmpfs.h>
     52  1.2.2.2  yamt 
     53  1.2.2.2  yamt /*
     54  1.2.2.2  yamt  * Forward declarations
     55  1.2.2.2  yamt  */
     56  1.2.2.2  yamt 
     57  1.2.2.2  yamt static int tmpfs_sane_rename(struct vnode *, struct componentname *,
     58  1.2.2.2  yamt     struct vnode *, struct componentname *,
     59  1.2.2.2  yamt     kauth_cred_t, bool);
     60  1.2.2.2  yamt static bool tmpfs_rmdired_p(struct vnode *);
     61  1.2.2.2  yamt static int tmpfs_gro_lock_directory(struct mount *, struct vnode *);
     62  1.2.2.2  yamt 
     63  1.2.2.2  yamt static const struct genfs_rename_ops tmpfs_genfs_rename_ops;
     64  1.2.2.2  yamt 
     65  1.2.2.2  yamt /*
     66  1.2.2.2  yamt  * tmpfs_sane_rename: The hairiest vop, with the saner API.
     67  1.2.2.2  yamt  *
     68  1.2.2.2  yamt  * Arguments:
     69  1.2.2.2  yamt  *
     70  1.2.2.2  yamt  * . fdvp (from directory vnode),
     71  1.2.2.2  yamt  * . fcnp (from component name),
     72  1.2.2.2  yamt  * . tdvp (to directory vnode),
     73  1.2.2.2  yamt  * . tcnp (to component name),
     74  1.2.2.2  yamt  * . cred (credentials structure), and
     75  1.2.2.2  yamt  * . posixly_correct (flag for behaviour if target & source link same file).
     76  1.2.2.2  yamt  *
     77  1.2.2.2  yamt  * fdvp and tdvp may be the same, and must be referenced and unlocked.
     78  1.2.2.2  yamt  */
     79  1.2.2.2  yamt static int
     80  1.2.2.2  yamt tmpfs_sane_rename(
     81  1.2.2.2  yamt     struct vnode *fdvp, struct componentname *fcnp,
     82  1.2.2.2  yamt     struct vnode *tdvp, struct componentname *tcnp,
     83  1.2.2.2  yamt     kauth_cred_t cred, bool posixly_correct)
     84  1.2.2.2  yamt {
     85  1.2.2.2  yamt 	struct tmpfs_dirent *fdirent, *tdirent;
     86  1.2.2.2  yamt 
     87  1.2.2.2  yamt 	return genfs_sane_rename(&tmpfs_genfs_rename_ops,
     88  1.2.2.2  yamt 	    fdvp, fcnp, &fdirent, tdvp, tcnp, &tdirent,
     89  1.2.2.2  yamt 	    cred, posixly_correct);
     90  1.2.2.2  yamt }
     91  1.2.2.2  yamt 
     92  1.2.2.2  yamt /*
     93  1.2.2.2  yamt  * tmpfs_rename: The hairiest vop, with the insanest API.  Defer to
     94  1.2.2.2  yamt  * genfs_insane_rename immediately.
     95  1.2.2.2  yamt  */
     96  1.2.2.2  yamt int
     97  1.2.2.2  yamt tmpfs_rename(void *v)
     98  1.2.2.2  yamt {
     99  1.2.2.2  yamt 
    100  1.2.2.2  yamt 	return genfs_insane_rename(v, &tmpfs_sane_rename);
    101  1.2.2.2  yamt }
    102  1.2.2.2  yamt 
    103  1.2.2.2  yamt /*
    104  1.2.2.2  yamt  * tmpfs_gro_directory_empty_p: Return true if the directory vp is
    105  1.2.2.2  yamt  * empty.  dvp is its parent.
    106  1.2.2.2  yamt  *
    107  1.2.2.2  yamt  * vp and dvp must be locked and referenced.
    108  1.2.2.2  yamt  */
    109  1.2.2.2  yamt static bool
    110  1.2.2.2  yamt tmpfs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
    111  1.2.2.2  yamt     struct vnode *vp, struct vnode *dvp)
    112  1.2.2.2  yamt {
    113  1.2.2.2  yamt 
    114  1.2.2.2  yamt 	(void)mp;
    115  1.2.2.2  yamt 	(void)cred;
    116  1.2.2.2  yamt 	(void)dvp;
    117  1.2.2.2  yamt 	KASSERT(mp != NULL);
    118  1.2.2.2  yamt 	KASSERT(vp != NULL);
    119  1.2.2.2  yamt 	KASSERT(dvp != NULL);
    120  1.2.2.2  yamt 	KASSERT(vp != dvp);
    121  1.2.2.2  yamt 	KASSERT(vp->v_mount == mp);
    122  1.2.2.2  yamt 	KASSERT(dvp->v_mount == mp);
    123  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    124  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    125  1.2.2.2  yamt 
    126  1.2.2.2  yamt 	return (VP_TO_TMPFS_NODE(vp)->tn_size == 0);
    127  1.2.2.2  yamt }
    128  1.2.2.2  yamt 
    129  1.2.2.2  yamt /*
    130  1.2.2.2  yamt  * tmpfs_gro_rename_check_possible: Check whether a rename is possible
    131  1.2.2.2  yamt  * independent of credentials.
    132  1.2.2.2  yamt  */
    133  1.2.2.2  yamt static int
    134  1.2.2.2  yamt tmpfs_gro_rename_check_possible(struct mount *mp,
    135  1.2.2.2  yamt     struct vnode *fdvp, struct vnode *fvp,
    136  1.2.2.2  yamt     struct vnode *tdvp, struct vnode *tvp)
    137  1.2.2.2  yamt {
    138  1.2.2.2  yamt 
    139  1.2.2.2  yamt 	(void)mp;
    140  1.2.2.2  yamt 	KASSERT(mp != NULL);
    141  1.2.2.2  yamt 	KASSERT(fdvp != NULL);
    142  1.2.2.2  yamt 	KASSERT(fvp != NULL);
    143  1.2.2.2  yamt 	KASSERT(tdvp != NULL);
    144  1.2.2.2  yamt 	KASSERT(fdvp != fvp);
    145  1.2.2.2  yamt 	KASSERT(fdvp != tvp);
    146  1.2.2.2  yamt 	KASSERT(tdvp != fvp);
    147  1.2.2.2  yamt 	KASSERT(tdvp != tvp);
    148  1.2.2.2  yamt 	KASSERT(fvp != tvp);
    149  1.2.2.2  yamt 	KASSERT(fdvp->v_type == VDIR);
    150  1.2.2.2  yamt 	KASSERT(tdvp->v_type == VDIR);
    151  1.2.2.2  yamt 	KASSERT(fdvp->v_mount == mp);
    152  1.2.2.2  yamt 	KASSERT(fvp->v_mount == mp);
    153  1.2.2.2  yamt 	KASSERT(tdvp->v_mount == mp);
    154  1.2.2.2  yamt 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
    155  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
    156  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
    157  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    158  1.2.2.2  yamt 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    159  1.2.2.2  yamt 
    160  1.2.2.2  yamt 	return genfs_ufslike_rename_check_possible(
    161  1.2.2.2  yamt 	    VP_TO_TMPFS_NODE(fdvp)->tn_flags, VP_TO_TMPFS_NODE(fvp)->tn_flags,
    162  1.2.2.2  yamt 	    VP_TO_TMPFS_NODE(tdvp)->tn_flags,
    163  1.2.2.2  yamt 	    (tvp? VP_TO_TMPFS_NODE(tvp)->tn_flags : 0), (tvp != NULL),
    164  1.2.2.2  yamt 	    IMMUTABLE, APPEND);
    165  1.2.2.2  yamt }
    166  1.2.2.2  yamt 
    167  1.2.2.2  yamt /*
    168  1.2.2.2  yamt  * tmpfs_gro_rename_check_permitted: Check whether a rename is
    169  1.2.2.2  yamt  * permitted given our credentials.
    170  1.2.2.2  yamt  */
    171  1.2.2.2  yamt static int
    172  1.2.2.2  yamt tmpfs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
    173  1.2.2.2  yamt     struct vnode *fdvp, struct vnode *fvp,
    174  1.2.2.2  yamt     struct vnode *tdvp, struct vnode *tvp)
    175  1.2.2.2  yamt {
    176  1.2.2.2  yamt 
    177  1.2.2.2  yamt 	(void)mp;
    178  1.2.2.2  yamt 	KASSERT(mp != NULL);
    179  1.2.2.2  yamt 	KASSERT(fdvp != NULL);
    180  1.2.2.2  yamt 	KASSERT(fvp != NULL);
    181  1.2.2.2  yamt 	KASSERT(tdvp != NULL);
    182  1.2.2.2  yamt 	KASSERT(fdvp != fvp);
    183  1.2.2.2  yamt 	KASSERT(fdvp != tvp);
    184  1.2.2.2  yamt 	KASSERT(tdvp != fvp);
    185  1.2.2.2  yamt 	KASSERT(tdvp != tvp);
    186  1.2.2.2  yamt 	KASSERT(fvp != tvp);
    187  1.2.2.2  yamt 	KASSERT(fdvp->v_type == VDIR);
    188  1.2.2.2  yamt 	KASSERT(tdvp->v_type == VDIR);
    189  1.2.2.2  yamt 	KASSERT(fdvp->v_mount == mp);
    190  1.2.2.2  yamt 	KASSERT(fvp->v_mount == mp);
    191  1.2.2.2  yamt 	KASSERT(tdvp->v_mount == mp);
    192  1.2.2.2  yamt 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
    193  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
    194  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
    195  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    196  1.2.2.2  yamt 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    197  1.2.2.2  yamt 
    198  1.2.2.2  yamt 	return genfs_ufslike_rename_check_permitted(cred,
    199  1.2.2.2  yamt 	    fdvp, VP_TO_TMPFS_NODE(fdvp)->tn_mode,
    200  1.2.2.2  yamt 	    VP_TO_TMPFS_NODE(fdvp)->tn_uid,
    201  1.2.2.2  yamt 	    fvp, VP_TO_TMPFS_NODE(fvp)->tn_uid,
    202  1.2.2.2  yamt 	    tdvp, VP_TO_TMPFS_NODE(tdvp)->tn_mode,
    203  1.2.2.2  yamt 	    VP_TO_TMPFS_NODE(tdvp)->tn_uid,
    204  1.2.2.2  yamt 	    tvp, (tvp? VP_TO_TMPFS_NODE(tvp)->tn_uid : 0));
    205  1.2.2.2  yamt }
    206  1.2.2.2  yamt 
    207  1.2.2.2  yamt /*
    208  1.2.2.2  yamt  * tmpfs_gro_remove_check_possible: Check whether a remove is possible
    209  1.2.2.2  yamt  * independent of credentials.
    210  1.2.2.2  yamt  */
    211  1.2.2.2  yamt static int
    212  1.2.2.2  yamt tmpfs_gro_remove_check_possible(struct mount *mp,
    213  1.2.2.2  yamt     struct vnode *dvp, struct vnode *vp)
    214  1.2.2.2  yamt {
    215  1.2.2.2  yamt 
    216  1.2.2.2  yamt 	(void)mp;
    217  1.2.2.2  yamt 	KASSERT(mp != NULL);
    218  1.2.2.2  yamt 	KASSERT(dvp != NULL);
    219  1.2.2.2  yamt 	KASSERT(vp != NULL);
    220  1.2.2.2  yamt 	KASSERT(dvp != vp);
    221  1.2.2.2  yamt 	KASSERT(dvp->v_type == VDIR);
    222  1.2.2.2  yamt 	KASSERT(vp->v_type != VDIR);
    223  1.2.2.2  yamt 	KASSERT(dvp->v_mount == mp);
    224  1.2.2.2  yamt 	KASSERT(vp->v_mount == mp);
    225  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    226  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    227  1.2.2.2  yamt 
    228  1.2.2.2  yamt 	return genfs_ufslike_remove_check_possible(
    229  1.2.2.2  yamt 	    VP_TO_TMPFS_NODE(dvp)->tn_flags, VP_TO_TMPFS_NODE(vp)->tn_flags,
    230  1.2.2.2  yamt 	    IMMUTABLE, APPEND);
    231  1.2.2.2  yamt }
    232  1.2.2.2  yamt 
    233  1.2.2.2  yamt /*
    234  1.2.2.2  yamt  * tmpfs_gro_remove_check_permitted: Check whether a remove is
    235  1.2.2.2  yamt  * permitted given our credentials.
    236  1.2.2.2  yamt  */
    237  1.2.2.2  yamt static int
    238  1.2.2.2  yamt tmpfs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
    239  1.2.2.2  yamt     struct vnode *dvp, struct vnode *vp)
    240  1.2.2.2  yamt {
    241  1.2.2.2  yamt 
    242  1.2.2.2  yamt 	(void)mp;
    243  1.2.2.2  yamt 	KASSERT(mp != NULL);
    244  1.2.2.2  yamt 	KASSERT(dvp != NULL);
    245  1.2.2.2  yamt 	KASSERT(vp != NULL);
    246  1.2.2.2  yamt 	KASSERT(dvp != vp);
    247  1.2.2.2  yamt 	KASSERT(dvp->v_type == VDIR);
    248  1.2.2.2  yamt 	KASSERT(vp->v_type != VDIR);
    249  1.2.2.2  yamt 	KASSERT(dvp->v_mount == mp);
    250  1.2.2.2  yamt 	KASSERT(vp->v_mount == mp);
    251  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    252  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    253  1.2.2.2  yamt 
    254  1.2.2.2  yamt 	return genfs_ufslike_remove_check_permitted(cred,
    255  1.2.2.2  yamt 	    dvp, VP_TO_TMPFS_NODE(dvp)->tn_mode, VP_TO_TMPFS_NODE(dvp)->tn_uid,
    256  1.2.2.2  yamt 	    vp, VP_TO_TMPFS_NODE(vp)->tn_uid);
    257  1.2.2.2  yamt }
    258  1.2.2.2  yamt 
    259  1.2.2.2  yamt /*
    260  1.2.2.2  yamt  * tmpfs_gro_rename: Actually perform the rename operation.
    261  1.2.2.2  yamt  */
    262  1.2.2.2  yamt static int
    263  1.2.2.2  yamt tmpfs_gro_rename(struct mount *mp, kauth_cred_t cred,
    264  1.2.2.2  yamt     struct vnode *fdvp, struct componentname *fcnp,
    265  1.2.2.2  yamt     void *fde, struct vnode *fvp,
    266  1.2.2.2  yamt     struct vnode *tdvp, struct componentname *tcnp,
    267  1.2.2.2  yamt     void *tde, struct vnode *tvp)
    268  1.2.2.2  yamt {
    269  1.2.2.4  yamt 	tmpfs_node_t *fdnode = VP_TO_TMPFS_DIR(fdvp);
    270  1.2.2.4  yamt 	tmpfs_node_t *tdnode = VP_TO_TMPFS_DIR(tdvp);
    271  1.2.2.2  yamt 	struct tmpfs_dirent **fdep = fde;
    272  1.2.2.2  yamt 	struct tmpfs_dirent **tdep = tde;
    273  1.2.2.2  yamt 	char *newname;
    274  1.2.2.2  yamt 
    275  1.2.2.2  yamt 	(void)cred;
    276  1.2.2.2  yamt 	KASSERT(mp != NULL);
    277  1.2.2.2  yamt 	KASSERT(fdvp != NULL);
    278  1.2.2.2  yamt 	KASSERT(fcnp != NULL);
    279  1.2.2.2  yamt 	KASSERT(fdep != NULL);
    280  1.2.2.2  yamt 	KASSERT(fvp != NULL);
    281  1.2.2.2  yamt 	KASSERT(tdvp != NULL);
    282  1.2.2.2  yamt 	KASSERT(tcnp != NULL);
    283  1.2.2.2  yamt 	KASSERT(tdep != NULL);
    284  1.2.2.2  yamt 	KASSERT(fdep != tdep);
    285  1.2.2.2  yamt 	KASSERT((*fdep) != (*tdep));
    286  1.2.2.2  yamt 	KASSERT((*fdep) != NULL);
    287  1.2.2.2  yamt 	KASSERT((*fdep)->td_node == VP_TO_TMPFS_NODE(fvp));
    288  1.2.2.2  yamt 	KASSERT((tvp == NULL) || ((*tdep) != NULL));
    289  1.2.2.2  yamt 	KASSERT((tvp == NULL) || ((*tdep)->td_node == VP_TO_TMPFS_NODE(tvp)));
    290  1.2.2.2  yamt 	KASSERT(fdvp != fvp);
    291  1.2.2.2  yamt 	KASSERT(fdvp != tvp);
    292  1.2.2.2  yamt 	KASSERT(tdvp != fvp);
    293  1.2.2.2  yamt 	KASSERT(tdvp != tvp);
    294  1.2.2.2  yamt 	KASSERT(fvp != tvp);
    295  1.2.2.2  yamt 	KASSERT(fdvp->v_mount == mp);
    296  1.2.2.2  yamt 	KASSERT(fvp->v_mount == mp);
    297  1.2.2.2  yamt 	KASSERT(tdvp->v_mount == mp);
    298  1.2.2.2  yamt 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
    299  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
    300  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
    301  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    302  1.2.2.2  yamt 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    303  1.2.2.2  yamt 
    304  1.2.2.2  yamt 	if (tmpfs_strname_neqlen(fcnp, tcnp)) {
    305  1.2.2.2  yamt 		newname = tmpfs_strname_alloc(VFS_TO_TMPFS(mp),
    306  1.2.2.2  yamt 		    tcnp->cn_namelen);
    307  1.2.2.2  yamt 		if (newname == NULL)
    308  1.2.2.2  yamt 			return ENOSPC;
    309  1.2.2.2  yamt 	} else {
    310  1.2.2.2  yamt 		newname = NULL;
    311  1.2.2.2  yamt 	}
    312  1.2.2.2  yamt 
    313  1.2.2.2  yamt 	/*
    314  1.2.2.2  yamt 	 * If we are moving from one directory to another, detach the
    315  1.2.2.2  yamt 	 * source entry and reattach it to the target directory.
    316  1.2.2.2  yamt 	 */
    317  1.2.2.2  yamt 	if (fdvp != tdvp) {
    318  1.2.2.4  yamt 		tmpfs_dir_detach(fdnode, *fdep);
    319  1.2.2.4  yamt 		tmpfs_dir_attach(tdnode, *fdep, VP_TO_TMPFS_NODE(fvp));
    320  1.2.2.2  yamt 	} else if (tvp == NULL) {
    321  1.2.2.2  yamt 		/*
    322  1.2.2.2  yamt 		 * We are changing the directory.  tmpfs_dir_attach and
    323  1.2.2.2  yamt 		 * tmpfs_dir_detach note the events for us, but for
    324  1.2.2.2  yamt 		 * this case we don't call them, so we must note the
    325  1.2.2.2  yamt 		 * event explicitly.
    326  1.2.2.2  yamt 		 */
    327  1.2.2.2  yamt 		VN_KNOTE(fdvp, NOTE_WRITE);
    328  1.2.2.2  yamt 	}
    329  1.2.2.2  yamt 
    330  1.2.2.2  yamt 	/*
    331  1.2.2.2  yamt 	 * If we are replacing an existing target entry, delete it.
    332  1.2.2.2  yamt 	 *
    333  1.2.2.2  yamt 	 * XXX What if the target is a directory with whiteout entries?
    334  1.2.2.2  yamt 	 */
    335  1.2.2.2  yamt 	if (tvp != NULL) {
    336  1.2.2.4  yamt 		tdnode = VP_TO_TMPFS_DIR(tdvp);
    337  1.2.2.4  yamt 
    338  1.2.2.2  yamt 		KASSERT((*tdep) != NULL);
    339  1.2.2.2  yamt 		KASSERT((*tdep)->td_node == VP_TO_TMPFS_NODE(tvp));
    340  1.2.2.2  yamt 		KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
    341  1.2.2.2  yamt 		if (tvp->v_type == VDIR) {
    342  1.2.2.2  yamt 			KASSERT(VP_TO_TMPFS_NODE(tvp)->tn_size == 0);
    343  1.2.2.2  yamt 			KASSERT(VP_TO_TMPFS_NODE(tvp)->tn_links == 2);
    344  1.2.2.2  yamt 
    345  1.2.2.2  yamt 			/*
    346  1.2.2.2  yamt 			 * Decrement the extra link count for `.' so
    347  1.2.2.2  yamt 			 * the vnode will be recycled when released.
    348  1.2.2.2  yamt 			 */
    349  1.2.2.2  yamt 			VP_TO_TMPFS_NODE(tvp)->tn_links--;
    350  1.2.2.2  yamt 		}
    351  1.2.2.4  yamt 		tmpfs_dir_detach(tdnode, *tdep);
    352  1.2.2.2  yamt 		tmpfs_free_dirent(VFS_TO_TMPFS(mp), *tdep);
    353  1.2.2.2  yamt 	}
    354  1.2.2.2  yamt 
    355  1.2.2.2  yamt 	/*
    356  1.2.2.2  yamt 	 * Update the directory entry's name if necessary, and flag
    357  1.2.2.2  yamt 	 * metadata updates.  A memory allocation failure here is not
    358  1.2.2.2  yamt 	 * OK because we've already committed some changes that we
    359  1.2.2.2  yamt 	 * can't back out at this point, hence the early allocation
    360  1.2.2.2  yamt 	 * above.
    361  1.2.2.2  yamt 	 */
    362  1.2.2.2  yamt 	if (newname != NULL) {
    363  1.2.2.2  yamt 		KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
    364  1.2.2.2  yamt 
    365  1.2.2.2  yamt 		tmpfs_strname_free(VFS_TO_TMPFS(mp), (*fdep)->td_name,
    366  1.2.2.2  yamt 		    (*fdep)->td_namelen);
    367  1.2.2.2  yamt 		(*fdep)->td_namelen = (uint16_t)tcnp->cn_namelen;
    368  1.2.2.2  yamt 		(void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
    369  1.2.2.2  yamt 		(*fdep)->td_name = newname;
    370  1.2.2.2  yamt 	}
    371  1.2.2.2  yamt 
    372  1.2.2.4  yamt 	/*
    373  1.2.2.4  yamt 	 * Update the timestamps of both parent directories and
    374  1.2.2.4  yamt 	 * the renamed file itself.
    375  1.2.2.4  yamt 	 */
    376  1.2.2.4  yamt 	tmpfs_update(fdvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    377  1.2.2.4  yamt 	tmpfs_update(tdvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    378  1.2.2.4  yamt 	tmpfs_update(fvp, TMPFS_UPDATE_CTIME);
    379  1.2.2.4  yamt 
    380  1.2.2.2  yamt 	VN_KNOTE(fvp, NOTE_RENAME);
    381  1.2.2.2  yamt 
    382  1.2.2.2  yamt 	genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
    383  1.2.2.2  yamt 
    384  1.2.2.2  yamt 	return 0;
    385  1.2.2.2  yamt }
    386  1.2.2.2  yamt 
    387  1.2.2.2  yamt /*
    388  1.2.2.2  yamt  * tmpfs_gro_remove: Rename an object over another link to itself,
    389  1.2.2.2  yamt  * effectively removing just the original link.
    390  1.2.2.2  yamt  */
    391  1.2.2.2  yamt static int
    392  1.2.2.2  yamt tmpfs_gro_remove(struct mount *mp, kauth_cred_t cred,
    393  1.2.2.2  yamt     struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
    394  1.2.2.2  yamt {
    395  1.2.2.4  yamt 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    396  1.2.2.2  yamt 	struct tmpfs_dirent **dep = de;
    397  1.2.2.2  yamt 
    398  1.2.2.2  yamt 	(void)vp;
    399  1.2.2.2  yamt 	KASSERT(mp != NULL);
    400  1.2.2.2  yamt 	KASSERT(dvp != NULL);
    401  1.2.2.2  yamt 	KASSERT(cnp != NULL);
    402  1.2.2.2  yamt 	KASSERT(dep != NULL);
    403  1.2.2.2  yamt 	KASSERT(vp != NULL);
    404  1.2.2.2  yamt 	KASSERT(dvp != vp);
    405  1.2.2.2  yamt 	KASSERT(dvp->v_mount == mp);
    406  1.2.2.2  yamt 	KASSERT(vp->v_mount == mp);
    407  1.2.2.2  yamt 	KASSERT(dvp->v_type == VDIR);
    408  1.2.2.2  yamt 	KASSERT(vp->v_type != VDIR);
    409  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    410  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    411  1.2.2.2  yamt 
    412  1.2.2.4  yamt 	tmpfs_dir_detach(dnode, *dep);
    413  1.2.2.2  yamt 	tmpfs_free_dirent(VFS_TO_TMPFS(mp), *dep);
    414  1.2.2.4  yamt 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
    415  1.2.2.2  yamt 
    416  1.2.2.2  yamt 	return 0;
    417  1.2.2.2  yamt }
    418  1.2.2.2  yamt 
    419  1.2.2.2  yamt /*
    420  1.2.2.2  yamt  * tmpfs_gro_lookup: Look up and save the lookup results.
    421  1.2.2.2  yamt  */
    422  1.2.2.2  yamt static int
    423  1.2.2.2  yamt tmpfs_gro_lookup(struct mount *mp, struct vnode *dvp,
    424  1.2.2.2  yamt     struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
    425  1.2.2.2  yamt {
    426  1.2.2.2  yamt 	struct tmpfs_dirent *dirent, **dep_ret = de_ret;
    427  1.2.2.2  yamt 	struct vnode *vp;
    428  1.2.2.2  yamt 	int error;
    429  1.2.2.2  yamt 
    430  1.2.2.2  yamt 	(void)mp;
    431  1.2.2.2  yamt 	KASSERT(mp != NULL);
    432  1.2.2.2  yamt 	KASSERT(dvp != NULL);
    433  1.2.2.2  yamt 	KASSERT(cnp != NULL);
    434  1.2.2.2  yamt 	KASSERT(dep_ret != NULL);
    435  1.2.2.2  yamt 	KASSERT(vp_ret != NULL);
    436  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    437  1.2.2.2  yamt 
    438  1.2.2.2  yamt 	dirent = tmpfs_dir_lookup(VP_TO_TMPFS_NODE(dvp), cnp);
    439  1.2.2.2  yamt 	if (dirent == NULL)
    440  1.2.2.2  yamt 		return ENOENT;
    441  1.2.2.2  yamt 
    442  1.2.2.2  yamt 	mutex_enter(&dirent->td_node->tn_vlock);
    443  1.2.2.2  yamt 	error = tmpfs_vnode_get(mp, dirent->td_node, &vp);
    444  1.2.2.2  yamt 	/* Note: tmpfs_vnode_get always releases dirent->td_node->tn_vlock.  */
    445  1.2.2.2  yamt 	if (error)
    446  1.2.2.2  yamt 		return error;
    447  1.2.2.2  yamt 
    448  1.2.2.2  yamt 	KASSERT(vp != NULL);
    449  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    450  1.2.2.2  yamt 
    451  1.2.2.2  yamt 	/*
    452  1.2.2.2  yamt 	 * tmpfs_vnode_get returns this locked for us, which would be
    453  1.2.2.2  yamt 	 * convenient but for lossage in other file systems.  So, for
    454  1.2.2.2  yamt 	 * hysterical raisins, we have to unlock it here.  The caller
    455  1.2.2.2  yamt 	 * will lock it again, and we don't rely on any interesting
    456  1.2.2.2  yamt 	 * invariants in the interim, beyond that it won't vanish from
    457  1.2.2.2  yamt 	 * under us, which it won't because it's referenced.
    458  1.2.2.2  yamt 	 *
    459  1.2.2.2  yamt 	 * XXX Once namei is fixed, we can change the genfs_rename
    460  1.2.2.2  yamt 	 * protocol so that this unlock is not necessary.
    461  1.2.2.2  yamt 	 */
    462  1.2.2.2  yamt 	VOP_UNLOCK(vp);
    463  1.2.2.2  yamt 
    464  1.2.2.2  yamt 	*dep_ret = dirent;
    465  1.2.2.2  yamt 	*vp_ret = vp;
    466  1.2.2.2  yamt 	return 0;
    467  1.2.2.2  yamt }
    468  1.2.2.2  yamt 
    469  1.2.2.2  yamt /*
    470  1.2.2.2  yamt  * tmpfs_rmdired_p: Check whether the directory vp has been rmdired.
    471  1.2.2.2  yamt  *
    472  1.2.2.2  yamt  * vp must be locked and referenced.
    473  1.2.2.2  yamt  */
    474  1.2.2.2  yamt static bool
    475  1.2.2.2  yamt tmpfs_rmdired_p(struct vnode *vp)
    476  1.2.2.2  yamt {
    477  1.2.2.2  yamt 
    478  1.2.2.2  yamt 	KASSERT(vp != NULL);
    479  1.2.2.2  yamt 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    480  1.2.2.2  yamt 	KASSERT(vp->v_type == VDIR);
    481  1.2.2.2  yamt 
    482  1.2.2.2  yamt 	return (VP_TO_TMPFS_NODE(vp)->tn_spec.tn_dir.tn_parent == NULL);
    483  1.2.2.2  yamt }
    484  1.2.2.2  yamt 
    485  1.2.2.2  yamt /*
    486  1.2.2.2  yamt  * tmpfs_gro_genealogy: Analyze the genealogy of the source and target
    487  1.2.2.2  yamt  * directories.
    488  1.2.2.2  yamt  */
    489  1.2.2.2  yamt static int
    490  1.2.2.2  yamt tmpfs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
    491  1.2.2.2  yamt     struct vnode *fdvp, struct vnode *tdvp,
    492  1.2.2.2  yamt     struct vnode **intermediate_node_ret)
    493  1.2.2.2  yamt {
    494  1.2.2.2  yamt 	struct vnode *vp;
    495  1.2.2.2  yamt 	struct tmpfs_node *dnode;
    496  1.2.2.2  yamt 	int error;
    497  1.2.2.2  yamt 
    498  1.2.2.2  yamt 	(void)cred;
    499  1.2.2.2  yamt 	KASSERT(mp != NULL);
    500  1.2.2.2  yamt 	KASSERT(fdvp != NULL);
    501  1.2.2.2  yamt 	KASSERT(tdvp != NULL);
    502  1.2.2.2  yamt 	KASSERT(fdvp != tdvp);
    503  1.2.2.2  yamt 	KASSERT(intermediate_node_ret != NULL);
    504  1.2.2.2  yamt 	KASSERT(fdvp->v_mount == mp);
    505  1.2.2.2  yamt 	KASSERT(tdvp->v_mount == mp);
    506  1.2.2.2  yamt 	KASSERT(fdvp->v_type == VDIR);
    507  1.2.2.2  yamt 	KASSERT(tdvp->v_type == VDIR);
    508  1.2.2.2  yamt 
    509  1.2.2.2  yamt 	/*
    510  1.2.2.2  yamt 	 * We need to provisionally lock tdvp to keep rmdir from
    511  1.2.2.2  yamt 	 * deleting it -- or any ancestor -- at an inopportune moment.
    512  1.2.2.2  yamt 	 */
    513  1.2.2.2  yamt 	error = tmpfs_gro_lock_directory(mp, tdvp);
    514  1.2.2.2  yamt 	if (error)
    515  1.2.2.2  yamt 		return error;
    516  1.2.2.2  yamt 
    517  1.2.2.2  yamt 	vp = tdvp;
    518  1.2.2.2  yamt 	vref(vp);
    519  1.2.2.2  yamt 
    520  1.2.2.2  yamt 	for (;;) {
    521  1.2.2.2  yamt 		KASSERT(vp != NULL);
    522  1.2.2.2  yamt 		KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    523  1.2.2.2  yamt 		KASSERT(vp->v_mount == mp);
    524  1.2.2.2  yamt 		KASSERT(vp->v_type == VDIR);
    525  1.2.2.2  yamt 		KASSERT(!tmpfs_rmdired_p(vp));
    526  1.2.2.2  yamt 
    527  1.2.2.2  yamt 		dnode = VP_TO_TMPFS_NODE(vp)->tn_spec.tn_dir.tn_parent;
    528  1.2.2.2  yamt 
    529  1.2.2.2  yamt 		/*
    530  1.2.2.2  yamt 		 * If dnode is null then vp has been rmdir'd, which is
    531  1.2.2.2  yamt 		 * not supposed to happen because we have it locked.
    532  1.2.2.2  yamt 		 */
    533  1.2.2.2  yamt 		KASSERT(dnode != NULL);
    534  1.2.2.2  yamt 
    535  1.2.2.2  yamt 		/* Did we hit the root without finding fdvp?  */
    536  1.2.2.2  yamt 		if (dnode == VP_TO_TMPFS_NODE(vp)) {
    537  1.2.2.2  yamt 			vput(vp);
    538  1.2.2.2  yamt 			*intermediate_node_ret = NULL;
    539  1.2.2.2  yamt 			return 0;
    540  1.2.2.2  yamt 		}
    541  1.2.2.2  yamt 
    542  1.2.2.2  yamt 		/* Did we find that fdvp is an ancestor of tdvp? */
    543  1.2.2.2  yamt 		if (dnode == VP_TO_TMPFS_NODE(fdvp)) {
    544  1.2.2.2  yamt 			KASSERT(dnode->tn_vnode == fdvp);
    545  1.2.2.2  yamt 			/* Unlock vp, but keep it referenced.  */
    546  1.2.2.2  yamt 			VOP_UNLOCK(vp);
    547  1.2.2.2  yamt 			*intermediate_node_ret = vp;
    548  1.2.2.2  yamt 			return 0;
    549  1.2.2.2  yamt 		}
    550  1.2.2.2  yamt 
    551  1.2.2.2  yamt 		/* Neither -- keep ascending the family tree.  */
    552  1.2.2.2  yamt 		mutex_enter(&dnode->tn_vlock);
    553  1.2.2.2  yamt 		vput(vp);
    554  1.2.2.2  yamt 		vp = NULL;	/* Just in case, for the kassert above...  */
    555  1.2.2.2  yamt 		error = tmpfs_vnode_get(mp, dnode, &vp);
    556  1.2.2.2  yamt 		if (error)
    557  1.2.2.2  yamt 			return error;
    558  1.2.2.3  yamt 
    559  1.2.2.3  yamt 		/*
    560  1.2.2.3  yamt 		 * tmpfs_vnode_get only guarantees that dnode will not
    561  1.2.2.3  yamt 		 * be freed while we get a vnode for it.  It does not
    562  1.2.2.3  yamt 		 * preserve any other invariants, so we must check
    563  1.2.2.3  yamt 		 * whether the parent has been removed in the meantime.
    564  1.2.2.3  yamt 		 */
    565  1.2.2.3  yamt 		if (tmpfs_rmdired_p(vp)) {
    566  1.2.2.3  yamt 			vput(vp);
    567  1.2.2.3  yamt 			return ENOENT;
    568  1.2.2.3  yamt 		}
    569  1.2.2.2  yamt 	}
    570  1.2.2.2  yamt }
    571  1.2.2.2  yamt 
    572  1.2.2.2  yamt /*
    573  1.2.2.2  yamt  * tmpfs_gro_lock_directory: Lock the directory vp, but fail if it has
    574  1.2.2.2  yamt  * been rmdir'd.
    575  1.2.2.2  yamt  */
    576  1.2.2.2  yamt static int
    577  1.2.2.2  yamt tmpfs_gro_lock_directory(struct mount *mp, struct vnode *vp)
    578  1.2.2.2  yamt {
    579  1.2.2.2  yamt 
    580  1.2.2.2  yamt 	(void)mp;
    581  1.2.2.2  yamt 	KASSERT(mp != NULL);
    582  1.2.2.2  yamt 	KASSERT(vp != NULL);
    583  1.2.2.2  yamt 	KASSERT(vp->v_mount == mp);
    584  1.2.2.2  yamt 
    585  1.2.2.2  yamt 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    586  1.2.2.2  yamt 
    587  1.2.2.2  yamt 	if (tmpfs_rmdired_p(vp)) {
    588  1.2.2.2  yamt 		VOP_UNLOCK(vp);
    589  1.2.2.2  yamt 		return ENOENT;
    590  1.2.2.2  yamt 	}
    591  1.2.2.2  yamt 
    592  1.2.2.2  yamt 	return 0;
    593  1.2.2.2  yamt }
    594  1.2.2.2  yamt 
    595  1.2.2.2  yamt static const struct genfs_rename_ops tmpfs_genfs_rename_ops = {
    596  1.2.2.2  yamt 	.gro_directory_empty_p		= tmpfs_gro_directory_empty_p,
    597  1.2.2.2  yamt 	.gro_rename_check_possible	= tmpfs_gro_rename_check_possible,
    598  1.2.2.2  yamt 	.gro_rename_check_permitted	= tmpfs_gro_rename_check_permitted,
    599  1.2.2.2  yamt 	.gro_remove_check_possible	= tmpfs_gro_remove_check_possible,
    600  1.2.2.2  yamt 	.gro_remove_check_permitted	= tmpfs_gro_remove_check_permitted,
    601  1.2.2.2  yamt 	.gro_rename			= tmpfs_gro_rename,
    602  1.2.2.2  yamt 	.gro_remove			= tmpfs_gro_remove,
    603  1.2.2.2  yamt 	.gro_lookup			= tmpfs_gro_lookup,
    604  1.2.2.2  yamt 	.gro_genealogy			= tmpfs_gro_genealogy,
    605  1.2.2.2  yamt 	.gro_lock_directory		= tmpfs_gro_lock_directory,
    606  1.2.2.2  yamt };
    607