xref: /minix3/sys/ufs/ext2fs/ext2fs_rename.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ext2fs_rename.c,v 1.8 2015/03/27 17:27:56 riastradh Exp $	*/
284d9c625SLionel Sambuc 
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc  * Copyright (c) 2012 The NetBSD Foundation, Inc.
584d9c625SLionel Sambuc  * All rights reserved.
684d9c625SLionel Sambuc  *
784d9c625SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
884d9c625SLionel Sambuc  * by Taylor R Campbell.
984d9c625SLionel Sambuc  *
1084d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1184d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1284d9c625SLionel Sambuc  * are met:
1384d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1484d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1584d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1684d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1784d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1884d9c625SLionel Sambuc  *
1984d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2084d9c625SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2184d9c625SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2284d9c625SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2384d9c625SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2484d9c625SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2584d9c625SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2684d9c625SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2784d9c625SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2884d9c625SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2984d9c625SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3084d9c625SLionel Sambuc  */
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc /*
3384d9c625SLionel Sambuc  * Ext2fs Rename
3484d9c625SLionel Sambuc  */
3584d9c625SLionel Sambuc 
3684d9c625SLionel Sambuc #include <sys/cdefs.h>
37*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ext2fs_rename.c,v 1.8 2015/03/27 17:27:56 riastradh Exp $");
3884d9c625SLionel Sambuc 
3984d9c625SLionel Sambuc #include <sys/param.h>
4084d9c625SLionel Sambuc #include <sys/buf.h>
4184d9c625SLionel Sambuc #include <sys/errno.h>
4284d9c625SLionel Sambuc #include <sys/kauth.h>
4384d9c625SLionel Sambuc #include <sys/mount.h>
4484d9c625SLionel Sambuc #include <sys/namei.h>
4584d9c625SLionel Sambuc #include <sys/vnode.h>
4684d9c625SLionel Sambuc #include <sys/vnode_if.h>
4784d9c625SLionel Sambuc 
4884d9c625SLionel Sambuc #include <miscfs/genfs/genfs.h>
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc #include <ufs/ext2fs/ext2fs.h>
5184d9c625SLionel Sambuc #include <ufs/ext2fs/ext2fs_dir.h>
5284d9c625SLionel Sambuc #include <ufs/ext2fs/ext2fs_extern.h>
5384d9c625SLionel Sambuc #include <ufs/ufs/inode.h>
5484d9c625SLionel Sambuc #include <ufs/ufs/ufs_extern.h>
5584d9c625SLionel Sambuc #include <ufs/ufs/ufsmount.h>
5684d9c625SLionel Sambuc 
5784d9c625SLionel Sambuc /*
5884d9c625SLionel Sambuc  * Forward declarations
5984d9c625SLionel Sambuc  */
6084d9c625SLionel Sambuc static int ext2fs_sane_rename(struct vnode *, struct componentname *,
6184d9c625SLionel Sambuc     struct vnode *, struct componentname *,
6284d9c625SLionel Sambuc     kauth_cred_t, bool);
6384d9c625SLionel Sambuc static bool ext2fs_rename_ulr_overlap_p(const struct ufs_lookup_results *,
6484d9c625SLionel Sambuc     const struct ufs_lookup_results *);
6584d9c625SLionel Sambuc static int ext2fs_rename_recalculate_fulr(struct vnode *,
6684d9c625SLionel Sambuc     struct ufs_lookup_results *, const struct ufs_lookup_results *,
6784d9c625SLionel Sambuc     const struct componentname *);
6884d9c625SLionel Sambuc static bool ext2fs_rmdired_p(struct vnode *);
6984d9c625SLionel Sambuc static int ext2fs_read_dotdot(struct vnode *, kauth_cred_t, ino_t *);
7084d9c625SLionel Sambuc static int ext2fs_rename_replace_dotdot(struct vnode *,
7184d9c625SLionel Sambuc     struct vnode *, struct vnode *, kauth_cred_t);
7284d9c625SLionel Sambuc static int ext2fs_gro_lock_directory(struct mount *, struct vnode *);
7384d9c625SLionel Sambuc 
7484d9c625SLionel Sambuc static const struct genfs_rename_ops ext2fs_genfs_rename_ops;
7584d9c625SLionel Sambuc 
7684d9c625SLionel Sambuc /*
7784d9c625SLionel Sambuc  * ext2fs_sane_rename: The hairiest vop, with the saner API.
7884d9c625SLionel Sambuc  *
7984d9c625SLionel Sambuc  * Arguments:
8084d9c625SLionel Sambuc  *
8184d9c625SLionel Sambuc  * . fdvp (from directory vnode),
8284d9c625SLionel Sambuc  * . fcnp (from component name),
8384d9c625SLionel Sambuc  * . tdvp (to directory vnode),
8484d9c625SLionel Sambuc  * . tcnp (to component name),
8584d9c625SLionel Sambuc  * . cred (credentials structure), and
8684d9c625SLionel Sambuc  * . posixly_correct (flag for behaviour if target & source link same file).
8784d9c625SLionel Sambuc  *
8884d9c625SLionel Sambuc  * fdvp and tdvp may be the same, and must be referenced and unlocked.
8984d9c625SLionel Sambuc  */
9084d9c625SLionel Sambuc static int
ext2fs_sane_rename(struct vnode * fdvp,struct componentname * fcnp,struct vnode * tdvp,struct componentname * tcnp,kauth_cred_t cred,bool posixly_correct)9184d9c625SLionel Sambuc ext2fs_sane_rename(
9284d9c625SLionel Sambuc     struct vnode *fdvp, struct componentname *fcnp,
9384d9c625SLionel Sambuc     struct vnode *tdvp, struct componentname *tcnp,
9484d9c625SLionel Sambuc     kauth_cred_t cred, bool posixly_correct)
9584d9c625SLionel Sambuc {
9684d9c625SLionel Sambuc 	struct ufs_lookup_results fulr, tulr;
9784d9c625SLionel Sambuc 
9884d9c625SLionel Sambuc 	return genfs_sane_rename(&ext2fs_genfs_rename_ops,
9984d9c625SLionel Sambuc 	    fdvp, fcnp, &fulr, tdvp, tcnp, &tulr,
10084d9c625SLionel Sambuc 	    cred, posixly_correct);
10184d9c625SLionel Sambuc }
10284d9c625SLionel Sambuc 
10384d9c625SLionel Sambuc /*
10484d9c625SLionel Sambuc  * ext2fs_rename: The hairiest vop, with the insanest API.  Defer to
10584d9c625SLionel Sambuc  * genfs_insane_rename immediately.
10684d9c625SLionel Sambuc  */
10784d9c625SLionel Sambuc int
ext2fs_rename(void * v)10884d9c625SLionel Sambuc ext2fs_rename(void *v)
10984d9c625SLionel Sambuc {
11084d9c625SLionel Sambuc 
11184d9c625SLionel Sambuc 	return genfs_insane_rename(v, &ext2fs_sane_rename);
11284d9c625SLionel Sambuc }
11384d9c625SLionel Sambuc 
11484d9c625SLionel Sambuc /*
11584d9c625SLionel Sambuc  * ext2fs_gro_directory_empty_p: Return true if the directory vp is
11684d9c625SLionel Sambuc  * empty.  dvp is its parent.
11784d9c625SLionel Sambuc  *
11884d9c625SLionel Sambuc  * vp and dvp must be locked and referenced.
11984d9c625SLionel Sambuc  */
12084d9c625SLionel Sambuc static bool
ext2fs_gro_directory_empty_p(struct mount * mp,kauth_cred_t cred,struct vnode * vp,struct vnode * dvp)12184d9c625SLionel Sambuc ext2fs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
12284d9c625SLionel Sambuc     struct vnode *vp, struct vnode *dvp)
12384d9c625SLionel Sambuc {
12484d9c625SLionel Sambuc 
12584d9c625SLionel Sambuc 	(void)mp;
12684d9c625SLionel Sambuc 	KASSERT(mp != NULL);
12784d9c625SLionel Sambuc 	KASSERT(vp != NULL);
12884d9c625SLionel Sambuc 	KASSERT(dvp != NULL);
12984d9c625SLionel Sambuc 	KASSERT(vp != dvp);
13084d9c625SLionel Sambuc 	KASSERT(vp->v_mount == mp);
13184d9c625SLionel Sambuc 	KASSERT(dvp->v_mount == mp);
13284d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
13384d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
13484d9c625SLionel Sambuc 
13584d9c625SLionel Sambuc 	return ext2fs_dirempty(VTOI(vp), VTOI(dvp)->i_number, cred);
13684d9c625SLionel Sambuc }
13784d9c625SLionel Sambuc 
13884d9c625SLionel Sambuc /*
13984d9c625SLionel Sambuc  * ext2fs_gro_rename_check_possible: Check whether a rename is possible
14084d9c625SLionel Sambuc  * independent of credentials.
14184d9c625SLionel Sambuc  */
14284d9c625SLionel Sambuc static int
ext2fs_gro_rename_check_possible(struct mount * mp,struct vnode * fdvp,struct vnode * fvp,struct vnode * tdvp,struct vnode * tvp)14384d9c625SLionel Sambuc ext2fs_gro_rename_check_possible(struct mount *mp,
14484d9c625SLionel Sambuc     struct vnode *fdvp, struct vnode *fvp,
14584d9c625SLionel Sambuc     struct vnode *tdvp, struct vnode *tvp)
14684d9c625SLionel Sambuc {
14784d9c625SLionel Sambuc 
14884d9c625SLionel Sambuc 	(void)mp;
14984d9c625SLionel Sambuc 	KASSERT(mp != NULL);
15084d9c625SLionel Sambuc 	KASSERT(fdvp != NULL);
15184d9c625SLionel Sambuc 	KASSERT(fvp != NULL);
15284d9c625SLionel Sambuc 	KASSERT(tdvp != NULL);
15384d9c625SLionel Sambuc 	KASSERT(fdvp != fvp);
15484d9c625SLionel Sambuc 	KASSERT(fdvp != tvp);
15584d9c625SLionel Sambuc 	KASSERT(tdvp != fvp);
15684d9c625SLionel Sambuc 	KASSERT(tdvp != tvp);
15784d9c625SLionel Sambuc 	KASSERT(fvp != tvp);
15884d9c625SLionel Sambuc 	KASSERT(fdvp->v_type == VDIR);
15984d9c625SLionel Sambuc 	KASSERT(tdvp->v_type == VDIR);
16084d9c625SLionel Sambuc 	KASSERT(fdvp->v_mount == mp);
16184d9c625SLionel Sambuc 	KASSERT(fvp->v_mount == mp);
16284d9c625SLionel Sambuc 	KASSERT(tdvp->v_mount == mp);
16384d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
16484d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
16584d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
16684d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
16784d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
16884d9c625SLionel Sambuc 
16984d9c625SLionel Sambuc 	return genfs_ufslike_rename_check_possible(
17084d9c625SLionel Sambuc 	    VTOI(fdvp)->i_e2fs_flags, VTOI(fvp)->i_e2fs_flags,
17184d9c625SLionel Sambuc 	    VTOI(tdvp)->i_e2fs_flags, (tvp? VTOI(tvp)->i_e2fs_flags : 0),
17284d9c625SLionel Sambuc 	    (tvp != NULL),
17384d9c625SLionel Sambuc 	    EXT2_IMMUTABLE, EXT2_APPEND);
17484d9c625SLionel Sambuc }
17584d9c625SLionel Sambuc 
17684d9c625SLionel Sambuc /*
17784d9c625SLionel Sambuc  * ext2fs_gro_rename_check_permitted: Check whether a rename is
17884d9c625SLionel Sambuc  * permitted given our credentials.
17984d9c625SLionel Sambuc  */
18084d9c625SLionel Sambuc static int
ext2fs_gro_rename_check_permitted(struct mount * mp,kauth_cred_t cred,struct vnode * fdvp,struct vnode * fvp,struct vnode * tdvp,struct vnode * tvp)18184d9c625SLionel Sambuc ext2fs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
18284d9c625SLionel Sambuc     struct vnode *fdvp, struct vnode *fvp,
18384d9c625SLionel Sambuc     struct vnode *tdvp, struct vnode *tvp)
18484d9c625SLionel Sambuc {
18584d9c625SLionel Sambuc 
18684d9c625SLionel Sambuc 	(void)mp;
18784d9c625SLionel Sambuc 	KASSERT(mp != NULL);
18884d9c625SLionel Sambuc 	KASSERT(fdvp != NULL);
18984d9c625SLionel Sambuc 	KASSERT(fvp != NULL);
19084d9c625SLionel Sambuc 	KASSERT(tdvp != NULL);
19184d9c625SLionel Sambuc 	KASSERT(fdvp != fvp);
19284d9c625SLionel Sambuc 	KASSERT(fdvp != tvp);
19384d9c625SLionel Sambuc 	KASSERT(tdvp != fvp);
19484d9c625SLionel Sambuc 	KASSERT(tdvp != tvp);
19584d9c625SLionel Sambuc 	KASSERT(fvp != tvp);
19684d9c625SLionel Sambuc 	KASSERT(fdvp->v_type == VDIR);
19784d9c625SLionel Sambuc 	KASSERT(tdvp->v_type == VDIR);
19884d9c625SLionel Sambuc 	KASSERT(fdvp->v_mount == mp);
19984d9c625SLionel Sambuc 	KASSERT(fvp->v_mount == mp);
20084d9c625SLionel Sambuc 	KASSERT(tdvp->v_mount == mp);
20184d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
20284d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
20384d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
20484d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
20584d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
20684d9c625SLionel Sambuc 
20784d9c625SLionel Sambuc 	return genfs_ufslike_rename_check_permitted(cred,
20884d9c625SLionel Sambuc 	    fdvp, VTOI(fdvp)->i_e2fs_mode, VTOI(fdvp)->i_uid,
20984d9c625SLionel Sambuc 	    fvp, VTOI(fvp)->i_uid,
21084d9c625SLionel Sambuc 	    tdvp, VTOI(tdvp)->i_e2fs_mode, VTOI(tdvp)->i_uid,
21184d9c625SLionel Sambuc 	    tvp, (tvp? VTOI(tvp)->i_uid : 0));
21284d9c625SLionel Sambuc }
21384d9c625SLionel Sambuc 
21484d9c625SLionel Sambuc /*
21584d9c625SLionel Sambuc  * ext2fs_gro_remove_check_possible: Check whether a remove is possible
21684d9c625SLionel Sambuc  * independent of credentials.
21784d9c625SLionel Sambuc  */
21884d9c625SLionel Sambuc static int
ext2fs_gro_remove_check_possible(struct mount * mp,struct vnode * dvp,struct vnode * vp)21984d9c625SLionel Sambuc ext2fs_gro_remove_check_possible(struct mount *mp,
22084d9c625SLionel Sambuc     struct vnode *dvp, struct vnode *vp)
22184d9c625SLionel Sambuc {
22284d9c625SLionel Sambuc 
22384d9c625SLionel Sambuc 	(void)mp;
22484d9c625SLionel Sambuc 	KASSERT(mp != NULL);
22584d9c625SLionel Sambuc 	KASSERT(dvp != NULL);
22684d9c625SLionel Sambuc 	KASSERT(vp != NULL);
22784d9c625SLionel Sambuc 	KASSERT(dvp != vp);
22884d9c625SLionel Sambuc 	KASSERT(dvp->v_type == VDIR);
22984d9c625SLionel Sambuc 	KASSERT(vp->v_type != VDIR);
23084d9c625SLionel Sambuc 	KASSERT(dvp->v_mount == mp);
23184d9c625SLionel Sambuc 	KASSERT(vp->v_mount == mp);
23284d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
23384d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
23484d9c625SLionel Sambuc 
23584d9c625SLionel Sambuc 	return genfs_ufslike_remove_check_possible(
23684d9c625SLionel Sambuc 	    VTOI(dvp)->i_e2fs_flags, VTOI(vp)->i_e2fs_flags,
23784d9c625SLionel Sambuc 	    EXT2_IMMUTABLE, EXT2_APPEND);
23884d9c625SLionel Sambuc }
23984d9c625SLionel Sambuc 
24084d9c625SLionel Sambuc /*
24184d9c625SLionel Sambuc  * ext2fs_gro_remove_check_permitted: Check whether a remove is
24284d9c625SLionel Sambuc  * permitted given our credentials.
24384d9c625SLionel Sambuc  */
24484d9c625SLionel Sambuc static int
ext2fs_gro_remove_check_permitted(struct mount * mp,kauth_cred_t cred,struct vnode * dvp,struct vnode * vp)24584d9c625SLionel Sambuc ext2fs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
24684d9c625SLionel Sambuc     struct vnode *dvp, struct vnode *vp)
24784d9c625SLionel Sambuc {
24884d9c625SLionel Sambuc 
24984d9c625SLionel Sambuc 	(void)mp;
25084d9c625SLionel Sambuc 	KASSERT(mp != NULL);
25184d9c625SLionel Sambuc 	KASSERT(dvp != NULL);
25284d9c625SLionel Sambuc 	KASSERT(vp != NULL);
25384d9c625SLionel Sambuc 	KASSERT(dvp != vp);
25484d9c625SLionel Sambuc 	KASSERT(dvp->v_type == VDIR);
25584d9c625SLionel Sambuc 	KASSERT(vp->v_type != VDIR);
25684d9c625SLionel Sambuc 	KASSERT(dvp->v_mount == mp);
25784d9c625SLionel Sambuc 	KASSERT(vp->v_mount == mp);
25884d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
25984d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
26084d9c625SLionel Sambuc 
26184d9c625SLionel Sambuc 	return genfs_ufslike_remove_check_permitted(cred,
26284d9c625SLionel Sambuc 	    dvp, VTOI(dvp)->i_e2fs_mode, VTOI(dvp)->i_uid,
26384d9c625SLionel Sambuc 	    vp, VTOI(vp)->i_uid);
26484d9c625SLionel Sambuc }
26584d9c625SLionel Sambuc 
26684d9c625SLionel Sambuc /*
26784d9c625SLionel Sambuc  * ext2fs_gro_rename: Actually perform the rename operation.
26884d9c625SLionel Sambuc  */
26984d9c625SLionel Sambuc static int
ext2fs_gro_rename(struct mount * mp,kauth_cred_t cred,struct vnode * fdvp,struct componentname * fcnp,void * fde,struct vnode * fvp,struct vnode * tdvp,struct componentname * tcnp,void * tde,struct vnode * tvp)27084d9c625SLionel Sambuc ext2fs_gro_rename(struct mount *mp, kauth_cred_t cred,
27184d9c625SLionel Sambuc     struct vnode *fdvp, struct componentname *fcnp,
27284d9c625SLionel Sambuc     void *fde, struct vnode *fvp,
27384d9c625SLionel Sambuc     struct vnode *tdvp, struct componentname *tcnp,
27484d9c625SLionel Sambuc     void *tde, struct vnode *tvp)
27584d9c625SLionel Sambuc {
27684d9c625SLionel Sambuc 	struct ufs_lookup_results *fulr = fde;
27784d9c625SLionel Sambuc 	struct ufs_lookup_results *tulr = tde;
27884d9c625SLionel Sambuc 	bool directory_p, reparent_p;
27984d9c625SLionel Sambuc 	int error;
28084d9c625SLionel Sambuc 
28184d9c625SLionel Sambuc 	(void)mp;
28284d9c625SLionel Sambuc 	KASSERT(mp != NULL);
28384d9c625SLionel Sambuc 	KASSERT(fdvp != NULL);
28484d9c625SLionel Sambuc 	KASSERT(fcnp != NULL);
28584d9c625SLionel Sambuc 	KASSERT(fulr != NULL);
28684d9c625SLionel Sambuc 	KASSERT(fvp != NULL);
28784d9c625SLionel Sambuc 	KASSERT(tdvp != NULL);
28884d9c625SLionel Sambuc 	KASSERT(tcnp != NULL);
28984d9c625SLionel Sambuc 	KASSERT(tulr != NULL);
29084d9c625SLionel Sambuc 	KASSERT(fulr != tulr);
29184d9c625SLionel Sambuc 	KASSERT(fdvp != fvp);
29284d9c625SLionel Sambuc 	KASSERT(fdvp != tvp);
29384d9c625SLionel Sambuc 	KASSERT(tdvp != fvp);
29484d9c625SLionel Sambuc 	KASSERT(tdvp != tvp);
29584d9c625SLionel Sambuc 	KASSERT(fvp != tvp);
29684d9c625SLionel Sambuc 	KASSERT(fdvp->v_mount == mp);
29784d9c625SLionel Sambuc 	KASSERT(fvp->v_mount == mp);
29884d9c625SLionel Sambuc 	KASSERT(tdvp->v_mount == mp);
29984d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
30084d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
30184d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
30284d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
30384d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
30484d9c625SLionel Sambuc 
30584d9c625SLionel Sambuc 	/*
30684d9c625SLionel Sambuc 	 * We shall need to temporarily bump the link count, so make
30784d9c625SLionel Sambuc 	 * sure there is room to do so.
30884d9c625SLionel Sambuc 	 */
30984d9c625SLionel Sambuc 	if ((nlink_t)VTOI(fvp)->i_e2fs_nlink >= LINK_MAX)
31084d9c625SLionel Sambuc 		return EMLINK;
31184d9c625SLionel Sambuc 
31284d9c625SLionel Sambuc 	directory_p = (fvp->v_type == VDIR);
31384d9c625SLionel Sambuc 	KASSERT(directory_p == ((VTOI(fvp)->i_e2fs_mode & IFMT) == IFDIR));
31484d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (directory_p == (tvp->v_type == VDIR)));
31584d9c625SLionel Sambuc 	KASSERT((tvp == NULL) || (directory_p ==
31684d9c625SLionel Sambuc 		((VTOI(tvp)->i_e2fs_mode & IFMT) == IFDIR)));
31784d9c625SLionel Sambuc 
31884d9c625SLionel Sambuc 	reparent_p = (fdvp != tdvp);
31984d9c625SLionel Sambuc 	KASSERT(reparent_p == (VTOI(fdvp)->i_number != VTOI(tdvp)->i_number));
32084d9c625SLionel Sambuc 
32184d9c625SLionel Sambuc 	/*
32284d9c625SLionel Sambuc 	 * Commence hacking of the data on disk.
32384d9c625SLionel Sambuc 	 */
32484d9c625SLionel Sambuc 
32584d9c625SLionel Sambuc 	/*
32684d9c625SLionel Sambuc 	 * 1) Bump link count while we're moving stuff
32784d9c625SLionel Sambuc 	 *    around.  If we crash somewhere before
32884d9c625SLionel Sambuc 	 *    completing our work, the link count
32984d9c625SLionel Sambuc 	 *    may be wrong, but correctable.
33084d9c625SLionel Sambuc 	 */
33184d9c625SLionel Sambuc 
33284d9c625SLionel Sambuc 	KASSERT((nlink_t)VTOI(fvp)->i_e2fs_nlink < LINK_MAX);
33384d9c625SLionel Sambuc 	VTOI(fvp)->i_e2fs_nlink++;
33484d9c625SLionel Sambuc 	VTOI(fvp)->i_flag |= IN_CHANGE;
33584d9c625SLionel Sambuc 	error = ext2fs_update(fvp, NULL, NULL, UPDATE_WAIT);
33684d9c625SLionel Sambuc 	if (error)
33784d9c625SLionel Sambuc 		goto whymustithurtsomuch;
33884d9c625SLionel Sambuc 
33984d9c625SLionel Sambuc 	/*
34084d9c625SLionel Sambuc 	 * 2) If target doesn't exist, link the target
34184d9c625SLionel Sambuc 	 *    to the source and unlink the source.
34284d9c625SLionel Sambuc 	 *    Otherwise, rewrite the target directory
34384d9c625SLionel Sambuc 	 *    entry to reference the source inode and
34484d9c625SLionel Sambuc 	 *    expunge the original entry's existence.
34584d9c625SLionel Sambuc 	 */
34684d9c625SLionel Sambuc 
34784d9c625SLionel Sambuc 	if (tvp == NULL) {
34884d9c625SLionel Sambuc 		/*
34984d9c625SLionel Sambuc 		 * Account for ".." in new directory.
35084d9c625SLionel Sambuc 		 * When source and destination have the same
35184d9c625SLionel Sambuc 		 * parent we don't fool with the link count.
35284d9c625SLionel Sambuc 		 */
35384d9c625SLionel Sambuc 		if (directory_p && reparent_p) {
35484d9c625SLionel Sambuc 			if ((nlink_t)VTOI(tdvp)->i_e2fs_nlink >= LINK_MAX) {
35584d9c625SLionel Sambuc 				error = EMLINK;
35684d9c625SLionel Sambuc 				goto whymustithurtsomuch;
35784d9c625SLionel Sambuc 			}
35884d9c625SLionel Sambuc 			KASSERT((nlink_t)VTOI(tdvp)->i_e2fs_nlink < LINK_MAX);
35984d9c625SLionel Sambuc 			VTOI(tdvp)->i_e2fs_nlink++;
36084d9c625SLionel Sambuc 			VTOI(tdvp)->i_flag |= IN_CHANGE;
36184d9c625SLionel Sambuc 			error = ext2fs_update(tdvp, NULL, NULL, UPDATE_WAIT);
36284d9c625SLionel Sambuc 			if (error) {
36384d9c625SLionel Sambuc 				/*
36484d9c625SLionel Sambuc 				 * Link count update didn't take --
36584d9c625SLionel Sambuc 				 * back out the in-memory link count.
36684d9c625SLionel Sambuc 				 */
36784d9c625SLionel Sambuc 				KASSERT(0 < VTOI(tdvp)->i_e2fs_nlink);
36884d9c625SLionel Sambuc 				VTOI(tdvp)->i_e2fs_nlink--;
36984d9c625SLionel Sambuc 				VTOI(tdvp)->i_flag |= IN_CHANGE;
37084d9c625SLionel Sambuc 				goto whymustithurtsomuch;
37184d9c625SLionel Sambuc 			}
37284d9c625SLionel Sambuc 		}
37384d9c625SLionel Sambuc 
37484d9c625SLionel Sambuc 		error = ext2fs_direnter(VTOI(fvp), tdvp, tulr, tcnp);
37584d9c625SLionel Sambuc 		if (error) {
37684d9c625SLionel Sambuc 			if (directory_p && reparent_p) {
37784d9c625SLionel Sambuc 				/*
37884d9c625SLionel Sambuc 				 * Directory update didn't take, but
37984d9c625SLionel Sambuc 				 * the link count update did -- back
38084d9c625SLionel Sambuc 				 * out the in-memory link count and the
38184d9c625SLionel Sambuc 				 * on-disk link count.
38284d9c625SLionel Sambuc 				 */
38384d9c625SLionel Sambuc 				KASSERT(0 < VTOI(tdvp)->i_e2fs_nlink);
38484d9c625SLionel Sambuc 				VTOI(tdvp)->i_e2fs_nlink--;
38584d9c625SLionel Sambuc 				VTOI(tdvp)->i_flag |= IN_CHANGE;
38684d9c625SLionel Sambuc 				(void)ext2fs_update(tdvp, NULL, NULL,
38784d9c625SLionel Sambuc 				    UPDATE_WAIT);
38884d9c625SLionel Sambuc 			}
38984d9c625SLionel Sambuc 			goto whymustithurtsomuch;
39084d9c625SLionel Sambuc 		}
39184d9c625SLionel Sambuc 	} else {
39284d9c625SLionel Sambuc 		if (directory_p)
39384d9c625SLionel Sambuc 			/* XXX WTF?  Why purge here?  Why not purge others?  */
39484d9c625SLionel Sambuc 			cache_purge(tdvp);
39584d9c625SLionel Sambuc 
39684d9c625SLionel Sambuc 		/*
39784d9c625SLionel Sambuc 		 * Make the target directory's entry for tcnp point at
39884d9c625SLionel Sambuc 		 * the source node.
39984d9c625SLionel Sambuc 		 */
40084d9c625SLionel Sambuc 		error = ext2fs_dirrewrite(VTOI(tdvp), tulr, VTOI(fvp), tcnp);
40184d9c625SLionel Sambuc 		if (error)
40284d9c625SLionel Sambuc 			goto whymustithurtsomuch;
40384d9c625SLionel Sambuc 
40484d9c625SLionel Sambuc 		/*
40584d9c625SLionel Sambuc 		 * If the source and target are directories, and the
40684d9c625SLionel Sambuc 		 * target is in the same directory as the source,
40784d9c625SLionel Sambuc 		 * decrement the link count of the common parent
40884d9c625SLionel Sambuc 		 * directory, since we are removing the target from
40984d9c625SLionel Sambuc 		 * that directory.
41084d9c625SLionel Sambuc 		 */
41184d9c625SLionel Sambuc 		if (directory_p && !reparent_p) {
41284d9c625SLionel Sambuc 			KASSERT(fdvp == tdvp);
41384d9c625SLionel Sambuc 			/* XXX check, don't kassert */
41484d9c625SLionel Sambuc 			KASSERT(0 < VTOI(tdvp)->i_e2fs_nlink);
41584d9c625SLionel Sambuc 			VTOI(tdvp)->i_e2fs_nlink--;
41684d9c625SLionel Sambuc 			VTOI(tdvp)->i_flag |= IN_CHANGE;
41784d9c625SLionel Sambuc 		}
41884d9c625SLionel Sambuc 
41984d9c625SLionel Sambuc 		/*
42084d9c625SLionel Sambuc 		 * Adjust the link count of the target to
42184d9c625SLionel Sambuc 		 * reflect the dirrewrite above.  If this is
42284d9c625SLionel Sambuc 		 * a directory it is empty and there are
42384d9c625SLionel Sambuc 		 * no links to it, so we can squash the inode and
42484d9c625SLionel Sambuc 		 * any space associated with it.  We disallowed
42584d9c625SLionel Sambuc 		 * renaming over top of a directory with links to
42684d9c625SLionel Sambuc 		 * it above, as the remaining link would point to
42784d9c625SLionel Sambuc 		 * a directory without "." or ".." entries.
42884d9c625SLionel Sambuc 		 */
42984d9c625SLionel Sambuc 		/* XXX check, don't kassert */
43084d9c625SLionel Sambuc 		KASSERT(0 < VTOI(tvp)->i_e2fs_nlink);
43184d9c625SLionel Sambuc 		VTOI(tvp)->i_e2fs_nlink--;
43284d9c625SLionel Sambuc 		if (directory_p) {
43384d9c625SLionel Sambuc 			/*
43484d9c625SLionel Sambuc 			 * XXX The ext2fs_dirempty call earlier does
43584d9c625SLionel Sambuc 			 * not guarantee anything about nlink.
43684d9c625SLionel Sambuc 			 */
43784d9c625SLionel Sambuc 			if (VTOI(tvp)->i_e2fs_nlink != 1)
43884d9c625SLionel Sambuc 				ufs_dirbad(VTOI(tvp), (doff_t)0,
43984d9c625SLionel Sambuc 				    "hard-linked directory");
44084d9c625SLionel Sambuc 			VTOI(tvp)->i_e2fs_nlink = 0;
44184d9c625SLionel Sambuc 			error = ext2fs_truncate(tvp, (off_t)0, IO_SYNC, cred);
44284d9c625SLionel Sambuc #if 0			/* XXX This branch was not in ext2fs_rename!  */
44384d9c625SLionel Sambuc 			if (error)
44484d9c625SLionel Sambuc 				goto whymustithurtsomuch;
44584d9c625SLionel Sambuc #endif
44684d9c625SLionel Sambuc 		}
44784d9c625SLionel Sambuc 		/*
44884d9c625SLionel Sambuc 		 * XXX Why is this here, and not above the preceding
44984d9c625SLionel Sambuc 		 * conditional?
45084d9c625SLionel Sambuc 		 */
45184d9c625SLionel Sambuc 		VTOI(tvp)->i_flag |= IN_CHANGE;
45284d9c625SLionel Sambuc 	}
45384d9c625SLionel Sambuc 
45484d9c625SLionel Sambuc 	/*
45584d9c625SLionel Sambuc 	 * If the source is a directory with a new parent, the link
45684d9c625SLionel Sambuc 	 * count of the old parent directory must be decremented and
45784d9c625SLionel Sambuc 	 * ".." set to point to the new parent.
45884d9c625SLionel Sambuc 	 */
45984d9c625SLionel Sambuc 	if (directory_p && reparent_p) {
46084d9c625SLionel Sambuc 		error = ext2fs_rename_replace_dotdot(fvp, fdvp, tdvp, cred);
46184d9c625SLionel Sambuc 		if (error)
46284d9c625SLionel Sambuc 			goto whymustithurtsomuch;
46384d9c625SLionel Sambuc 
46484d9c625SLionel Sambuc 		/* XXX WTF?  Why purge here?  Why not purge others?  */
46584d9c625SLionel Sambuc 		cache_purge(fdvp);
46684d9c625SLionel Sambuc 	}
46784d9c625SLionel Sambuc 
46884d9c625SLionel Sambuc 	/*
46984d9c625SLionel Sambuc 	 * 3) Unlink the source.
47084d9c625SLionel Sambuc 	 */
47184d9c625SLionel Sambuc 
47284d9c625SLionel Sambuc 	/*
47384d9c625SLionel Sambuc 	 * ext2fs_direnter may compact the directory in the process of
47484d9c625SLionel Sambuc 	 * inserting a new entry.  That may invalidate fulr, which we
47584d9c625SLionel Sambuc 	 * need in order to remove the old entry.  In that case, we
47684d9c625SLionel Sambuc 	 * need to recalculate what fulr should be.
47784d9c625SLionel Sambuc 	 */
47884d9c625SLionel Sambuc 	if (!reparent_p && (tvp == NULL) &&
47984d9c625SLionel Sambuc 	    ext2fs_rename_ulr_overlap_p(fulr, tulr)) {
48084d9c625SLionel Sambuc 		error = ext2fs_rename_recalculate_fulr(fdvp, fulr, tulr, fcnp);
48184d9c625SLionel Sambuc #if 0				/* XXX */
48284d9c625SLionel Sambuc 		if (error)	/* XXX Try to back out changes?  */
48384d9c625SLionel Sambuc 			goto whymustithurtsomuch;
48484d9c625SLionel Sambuc #endif
48584d9c625SLionel Sambuc 	}
48684d9c625SLionel Sambuc 
48784d9c625SLionel Sambuc 	error = ext2fs_dirremove(fdvp, fulr, fcnp);
48884d9c625SLionel Sambuc 	if (error)
48984d9c625SLionel Sambuc 		goto whymustithurtsomuch;
49084d9c625SLionel Sambuc 
49184d9c625SLionel Sambuc 	/*
49284d9c625SLionel Sambuc 	 * XXX Perhaps this should go at the top, in case the file
49384d9c625SLionel Sambuc 	 * system is modified but incompletely so because of an
49484d9c625SLionel Sambuc 	 * intermediate error.
49584d9c625SLionel Sambuc 	 */
49684d9c625SLionel Sambuc 	genfs_rename_knote(fdvp, fvp, tdvp, tvp,
49784d9c625SLionel Sambuc 	    ((tvp != NULL) && (VTOI(tvp)->i_e2fs_nlink == 0)));
49884d9c625SLionel Sambuc #if 0				/* XXX */
49984d9c625SLionel Sambuc 	genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
50084d9c625SLionel Sambuc #endif
50184d9c625SLionel Sambuc 
50284d9c625SLionel Sambuc whymustithurtsomuch:
50384d9c625SLionel Sambuc 	KASSERT(0 < VTOI(fvp)->i_e2fs_nlink);
50484d9c625SLionel Sambuc 	VTOI(fvp)->i_e2fs_nlink--;
50584d9c625SLionel Sambuc 	VTOI(fvp)->i_flag |= IN_CHANGE;
50684d9c625SLionel Sambuc 	return error;
50784d9c625SLionel Sambuc }
50884d9c625SLionel Sambuc 
50984d9c625SLionel Sambuc /*
51084d9c625SLionel Sambuc  * ext2fs_rename_ulr_overlap_p: True iff tulr overlaps with fulr so
51184d9c625SLionel Sambuc  * that entering a directory entry at tulr may move fulr.
51284d9c625SLionel Sambuc  */
51384d9c625SLionel Sambuc static bool
ext2fs_rename_ulr_overlap_p(const struct ufs_lookup_results * fulr,const struct ufs_lookup_results * tulr)51484d9c625SLionel Sambuc ext2fs_rename_ulr_overlap_p(const struct ufs_lookup_results *fulr,
51584d9c625SLionel Sambuc     const struct ufs_lookup_results *tulr)
51684d9c625SLionel Sambuc {
51784d9c625SLionel Sambuc 	doff_t from_prev_start, from_prev_end, to_start, to_end;
51884d9c625SLionel Sambuc 
51984d9c625SLionel Sambuc 	KASSERT(fulr != NULL);
52084d9c625SLionel Sambuc 	KASSERT(tulr != NULL);
52184d9c625SLionel Sambuc 	KASSERT(fulr != tulr);
52284d9c625SLionel Sambuc 
52384d9c625SLionel Sambuc 	/*
52484d9c625SLionel Sambuc 	 * fulr is from a DELETE lookup, so fulr->ulr_count is the size
52584d9c625SLionel Sambuc 	 * of the preceding entry (d_reclen).
52684d9c625SLionel Sambuc 	 */
52784d9c625SLionel Sambuc 	from_prev_end = fulr->ulr_offset;
52884d9c625SLionel Sambuc 	KASSERT(fulr->ulr_count <= from_prev_end);
52984d9c625SLionel Sambuc 	from_prev_start = (from_prev_end - fulr->ulr_count);
53084d9c625SLionel Sambuc 
53184d9c625SLionel Sambuc 	/*
53284d9c625SLionel Sambuc 	 * tulr is from a RENAME lookup, so tulr->ulr_count is the size
53384d9c625SLionel Sambuc 	 * of the free space for an entry that we are about to fill.
53484d9c625SLionel Sambuc 	 */
53584d9c625SLionel Sambuc 	to_start = tulr->ulr_offset;
53684d9c625SLionel Sambuc 	KASSERT(tulr->ulr_count < (EXT2FS_MAXDIRSIZE - to_start));
53784d9c625SLionel Sambuc 	to_end = (to_start + tulr->ulr_count);
53884d9c625SLionel Sambuc 
53984d9c625SLionel Sambuc 	return
54084d9c625SLionel Sambuc 	    (((to_start <= from_prev_start) && (from_prev_start < to_end)) ||
54184d9c625SLionel Sambuc 		((to_start <= from_prev_end) && (from_prev_end < to_end)));
54284d9c625SLionel Sambuc }
54384d9c625SLionel Sambuc 
54484d9c625SLionel Sambuc /*
54584d9c625SLionel Sambuc  * ext2fs_rename_recalculate_fulr: If we have just entered a directory
54684d9c625SLionel Sambuc  * into dvp at tulr, and we were about to remove one at fulr for an
54784d9c625SLionel Sambuc  * entry named fcnp, fulr may be invalid.  So, if necessary,
54884d9c625SLionel Sambuc  * recalculate it.
54984d9c625SLionel Sambuc  */
55084d9c625SLionel Sambuc static int
ext2fs_rename_recalculate_fulr(struct vnode * dvp,struct ufs_lookup_results * fulr,const struct ufs_lookup_results * tulr,const struct componentname * fcnp)55184d9c625SLionel Sambuc ext2fs_rename_recalculate_fulr(struct vnode *dvp,
55284d9c625SLionel Sambuc     struct ufs_lookup_results *fulr, const struct ufs_lookup_results *tulr,
55384d9c625SLionel Sambuc     const struct componentname *fcnp)
55484d9c625SLionel Sambuc {
55584d9c625SLionel Sambuc 	struct mount *mp;
55684d9c625SLionel Sambuc 	struct ufsmount *ump;
55784d9c625SLionel Sambuc 	/* XXX int is a silly type for this; blame ufsmount::um_dirblksiz.  */
55884d9c625SLionel Sambuc 	int dirblksiz;
55984d9c625SLionel Sambuc 	doff_t search_start, search_end;
56084d9c625SLionel Sambuc 	doff_t offset;		/* Offset of entry we're examining.  */
56184d9c625SLionel Sambuc 	struct buf *bp;		/* I/O block we're examining.  */
56284d9c625SLionel Sambuc 	char *dirbuf;		/* Pointer into directory at search_start.  */
56384d9c625SLionel Sambuc 	struct ext2fs_direct *ep; /* Pointer to the entry we're examining.  */
56484d9c625SLionel Sambuc 	/* XXX direct::d_reclen is 16-bit;
56584d9c625SLionel Sambuc 	 * ufs_lookup_results::ulr_reclen is 32-bit.  Blah.  */
56684d9c625SLionel Sambuc 	uint32_t reclen;	/* Length of the entry we're examining.  */
56784d9c625SLionel Sambuc 	uint32_t prev_reclen;	/* Length of the preceding entry.  */
56884d9c625SLionel Sambuc 	int error;
56984d9c625SLionel Sambuc 
57084d9c625SLionel Sambuc 	KASSERT(dvp != NULL);
57184d9c625SLionel Sambuc 	KASSERT(dvp->v_mount != NULL);
57284d9c625SLionel Sambuc 	KASSERT(VTOI(dvp) != NULL);
57384d9c625SLionel Sambuc 	KASSERT(fulr != NULL);
57484d9c625SLionel Sambuc 	KASSERT(tulr != NULL);
57584d9c625SLionel Sambuc 	KASSERT(fulr != tulr);
57684d9c625SLionel Sambuc 	KASSERT(ext2fs_rename_ulr_overlap_p(fulr, tulr));
57784d9c625SLionel Sambuc 
57884d9c625SLionel Sambuc 	mp = dvp->v_mount;
57984d9c625SLionel Sambuc 	ump = VFSTOUFS(mp);
58084d9c625SLionel Sambuc 	KASSERT(ump != NULL);
58184d9c625SLionel Sambuc 	KASSERT(ump == VTOI(dvp)->i_ump);
58284d9c625SLionel Sambuc 
58384d9c625SLionel Sambuc 	dirblksiz = ump->um_dirblksiz;
58484d9c625SLionel Sambuc 	KASSERT(0 < dirblksiz);
58584d9c625SLionel Sambuc 	KASSERT((dirblksiz & (dirblksiz - 1)) == 0);
58684d9c625SLionel Sambuc 
58784d9c625SLionel Sambuc 	/* A directory block may not span across multiple I/O blocks.  */
58884d9c625SLionel Sambuc 	KASSERT(dirblksiz <= mp->mnt_stat.f_iosize);
58984d9c625SLionel Sambuc 
59084d9c625SLionel Sambuc 	/* Find the bounds of the search.  */
59184d9c625SLionel Sambuc 	search_start = tulr->ulr_offset;
59284d9c625SLionel Sambuc 	KASSERT(fulr->ulr_reclen < (EXT2FS_MAXDIRSIZE - fulr->ulr_offset));
59384d9c625SLionel Sambuc 	search_end = (fulr->ulr_offset + fulr->ulr_reclen);
59484d9c625SLionel Sambuc 
59584d9c625SLionel Sambuc 	/* Compaction must happen only within a directory block. (*)  */
59684d9c625SLionel Sambuc 	KASSERT(search_start <= search_end);
59784d9c625SLionel Sambuc 	KASSERT((search_end - (search_start &~ (dirblksiz - 1))) <= dirblksiz);
59884d9c625SLionel Sambuc 
59984d9c625SLionel Sambuc 	dirbuf = NULL;
60084d9c625SLionel Sambuc 	bp = NULL;
60184d9c625SLionel Sambuc 	error = ext2fs_blkatoff(dvp, (off_t)search_start, &dirbuf, &bp);
60284d9c625SLionel Sambuc 	if (error)
60384d9c625SLionel Sambuc 		return error;
60484d9c625SLionel Sambuc 	KASSERT(dirbuf != NULL);
60584d9c625SLionel Sambuc 	KASSERT(bp != NULL);
60684d9c625SLionel Sambuc 
60784d9c625SLionel Sambuc 	/*
60884d9c625SLionel Sambuc 	 * Guarantee we sha'n't go past the end of the buffer we got.
60984d9c625SLionel Sambuc 	 * dirbuf is bp->b_data + (search_start & (iosize - 1)), and
61084d9c625SLionel Sambuc 	 * the valid range is [bp->b_data, bp->b_data + bp->b_bcount).
61184d9c625SLionel Sambuc 	 */
61284d9c625SLionel Sambuc 	KASSERT((search_end - search_start) <=
61384d9c625SLionel Sambuc 	    (bp->b_bcount - (search_start & (mp->mnt_stat.f_iosize - 1))));
61484d9c625SLionel Sambuc 
61584d9c625SLionel Sambuc 	prev_reclen = fulr->ulr_count;
61684d9c625SLionel Sambuc 	offset = search_start;
61784d9c625SLionel Sambuc 
61884d9c625SLionel Sambuc 	/*
61984d9c625SLionel Sambuc 	 * Search from search_start to search_end for the entry matching
62084d9c625SLionel Sambuc 	 * fcnp, which must be there because we found it before and it
62184d9c625SLionel Sambuc 	 * should only at most have moved earlier.
62284d9c625SLionel Sambuc 	 */
62384d9c625SLionel Sambuc 	for (;;) {
62484d9c625SLionel Sambuc 		KASSERT(search_start <= offset);
62584d9c625SLionel Sambuc 		KASSERT(offset < search_end);
62684d9c625SLionel Sambuc 
62784d9c625SLionel Sambuc 		/*
62884d9c625SLionel Sambuc 		 * Examine the directory entry at offset.
62984d9c625SLionel Sambuc 		 */
63084d9c625SLionel Sambuc 		ep = (struct ext2fs_direct *)
63184d9c625SLionel Sambuc 		    (dirbuf + (offset - search_start));
63284d9c625SLionel Sambuc 		reclen = fs2h16(ep->e2d_reclen);
63384d9c625SLionel Sambuc 
63484d9c625SLionel Sambuc 		if (ep->e2d_ino == 0)
63584d9c625SLionel Sambuc 			goto next;	/* Entry is unused.  */
63684d9c625SLionel Sambuc 
63784d9c625SLionel Sambuc 		if (fs2h32(ep->e2d_ino) == UFS_WINO)
63884d9c625SLionel Sambuc 			goto next;	/* Entry is whiteout.  */
63984d9c625SLionel Sambuc 
64084d9c625SLionel Sambuc 		if (fcnp->cn_namelen != ep->e2d_namlen)
64184d9c625SLionel Sambuc 			goto next;	/* Wrong name length.  */
64284d9c625SLionel Sambuc 
64384d9c625SLionel Sambuc 		if (memcmp(ep->e2d_name, fcnp->cn_nameptr, fcnp->cn_namelen))
64484d9c625SLionel Sambuc 			goto next;	/* Wrong name.  */
64584d9c625SLionel Sambuc 
64684d9c625SLionel Sambuc 		/* Got it!  */
64784d9c625SLionel Sambuc 		break;
64884d9c625SLionel Sambuc 
64984d9c625SLionel Sambuc next:
65084d9c625SLionel Sambuc 		if (! ((reclen < search_end) &&
65184d9c625SLionel Sambuc 			(offset < (search_end - reclen)))) {
65284d9c625SLionel Sambuc 			brelse(bp, 0);
65384d9c625SLionel Sambuc 			return EIO;	/* XXX Panic?  What?  */
65484d9c625SLionel Sambuc 		}
65584d9c625SLionel Sambuc 
65684d9c625SLionel Sambuc 		/* We may not move past the search end.  */
65784d9c625SLionel Sambuc 		KASSERT(reclen < search_end);
65884d9c625SLionel Sambuc 		KASSERT(offset < (search_end - reclen));
65984d9c625SLionel Sambuc 
66084d9c625SLionel Sambuc 		/*
66184d9c625SLionel Sambuc 		 * We may not move across a directory block boundary;
66284d9c625SLionel Sambuc 		 * see (*) above.
66384d9c625SLionel Sambuc 		 */
66484d9c625SLionel Sambuc 		KASSERT((offset &~ (dirblksiz - 1)) ==
66584d9c625SLionel Sambuc 		    ((offset + reclen) &~ (dirblksiz - 1)));
66684d9c625SLionel Sambuc 
66784d9c625SLionel Sambuc 		prev_reclen = reclen;
66884d9c625SLionel Sambuc 		offset += reclen;
66984d9c625SLionel Sambuc 	}
67084d9c625SLionel Sambuc 
67184d9c625SLionel Sambuc 	/*
67284d9c625SLionel Sambuc 	 * Found the entry.  Record where.
67384d9c625SLionel Sambuc 	 */
67484d9c625SLionel Sambuc 	fulr->ulr_offset = offset;
67584d9c625SLionel Sambuc 	fulr->ulr_reclen = reclen;
67684d9c625SLionel Sambuc 
67784d9c625SLionel Sambuc 	/*
67884d9c625SLionel Sambuc 	 * Record the preceding record length, but not if we're at the
67984d9c625SLionel Sambuc 	 * start of a directory block.
68084d9c625SLionel Sambuc 	 */
68184d9c625SLionel Sambuc 	fulr->ulr_count = ((offset & (dirblksiz - 1))? prev_reclen : 0);
68284d9c625SLionel Sambuc 
68384d9c625SLionel Sambuc 	brelse(bp, 0);
68484d9c625SLionel Sambuc 	return 0;
68584d9c625SLionel Sambuc }
68684d9c625SLionel Sambuc 
68784d9c625SLionel Sambuc /*
68884d9c625SLionel Sambuc  * ext2fs_gro_remove: Rename an object over another link to itself,
68984d9c625SLionel Sambuc  * effectively removing just the original link.
69084d9c625SLionel Sambuc  */
69184d9c625SLionel Sambuc static int
ext2fs_gro_remove(struct mount * mp,kauth_cred_t cred,struct vnode * dvp,struct componentname * cnp,void * de,struct vnode * vp)69284d9c625SLionel Sambuc ext2fs_gro_remove(struct mount *mp, kauth_cred_t cred,
69384d9c625SLionel Sambuc     struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
69484d9c625SLionel Sambuc {
69584d9c625SLionel Sambuc 	struct ufs_lookup_results *ulr = de;
69684d9c625SLionel Sambuc 	int error;
69784d9c625SLionel Sambuc 
69884d9c625SLionel Sambuc 	(void)mp;
69984d9c625SLionel Sambuc 	KASSERT(mp != NULL);
70084d9c625SLionel Sambuc 	KASSERT(dvp != NULL);
70184d9c625SLionel Sambuc 	KASSERT(cnp != NULL);
70284d9c625SLionel Sambuc 	KASSERT(ulr != NULL);
70384d9c625SLionel Sambuc 	KASSERT(vp != NULL);
70484d9c625SLionel Sambuc 	KASSERT(dvp != vp);
70584d9c625SLionel Sambuc 	KASSERT(dvp->v_mount == mp);
70684d9c625SLionel Sambuc 	KASSERT(vp->v_mount == mp);
70784d9c625SLionel Sambuc 	KASSERT(dvp->v_type == VDIR);
70884d9c625SLionel Sambuc 	KASSERT(vp->v_type != VDIR);
70984d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
71084d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
71184d9c625SLionel Sambuc 
71284d9c625SLionel Sambuc 	error = ext2fs_dirremove(dvp, ulr, cnp);
71384d9c625SLionel Sambuc 	if (error)
71484d9c625SLionel Sambuc 		return error;
71584d9c625SLionel Sambuc 
71684d9c625SLionel Sambuc 	KASSERT(0 < VTOI(vp)->i_e2fs_nlink);
71784d9c625SLionel Sambuc 	VTOI(vp)->i_e2fs_nlink--;
71884d9c625SLionel Sambuc 	VTOI(vp)->i_flag |= IN_CHANGE;
71984d9c625SLionel Sambuc 
72084d9c625SLionel Sambuc 	VN_KNOTE(dvp, NOTE_WRITE);
72184d9c625SLionel Sambuc 	VN_KNOTE(vp, (VTOI(vp)->i_e2fs_nlink? NOTE_LINK : NOTE_DELETE));
72284d9c625SLionel Sambuc 
72384d9c625SLionel Sambuc 	return 0;
72484d9c625SLionel Sambuc }
72584d9c625SLionel Sambuc 
72684d9c625SLionel Sambuc /*
72784d9c625SLionel Sambuc  * ext2fs_gro_lookup: Look up and save the lookup results.
72884d9c625SLionel Sambuc  */
72984d9c625SLionel Sambuc static int
ext2fs_gro_lookup(struct mount * mp,struct vnode * dvp,struct componentname * cnp,void * de_ret,struct vnode ** vp_ret)73084d9c625SLionel Sambuc ext2fs_gro_lookup(struct mount *mp, struct vnode *dvp,
73184d9c625SLionel Sambuc     struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
73284d9c625SLionel Sambuc {
73384d9c625SLionel Sambuc 	struct ufs_lookup_results *ulr_ret = de_ret;
73484d9c625SLionel Sambuc 	struct vnode *vp;
73584d9c625SLionel Sambuc 	int error;
73684d9c625SLionel Sambuc 
73784d9c625SLionel Sambuc 	(void)mp;
73884d9c625SLionel Sambuc 	KASSERT(mp != NULL);
73984d9c625SLionel Sambuc 	KASSERT(dvp != NULL);
74084d9c625SLionel Sambuc 	KASSERT(cnp != NULL);
74184d9c625SLionel Sambuc 	KASSERT(ulr_ret != NULL);
74284d9c625SLionel Sambuc 	KASSERT(vp_ret != NULL);
74384d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
74484d9c625SLionel Sambuc 
74584d9c625SLionel Sambuc 	/* Kludge cargo-culted from dholland's ufs_rename.  */
74684d9c625SLionel Sambuc 	cnp->cn_flags &=~ MODMASK;
74784d9c625SLionel Sambuc 	cnp->cn_flags |= (LOCKPARENT | LOCKLEAF);
74884d9c625SLionel Sambuc 
74984d9c625SLionel Sambuc 	error = relookup(dvp, &vp, cnp, 0 /* dummy */);
75084d9c625SLionel Sambuc 	if ((error == 0) && (vp == NULL)) {
75184d9c625SLionel Sambuc 		error = ENOENT;
75284d9c625SLionel Sambuc 		goto out;
75384d9c625SLionel Sambuc 	} else if (error) {
75484d9c625SLionel Sambuc 		return error;
75584d9c625SLionel Sambuc 	}
75684d9c625SLionel Sambuc 
75784d9c625SLionel Sambuc 	/*
75884d9c625SLionel Sambuc 	 * Thanks to VFS insanity, relookup locks vp, which screws us
75984d9c625SLionel Sambuc 	 * in various ways.
76084d9c625SLionel Sambuc 	 */
76184d9c625SLionel Sambuc 	KASSERT(vp != NULL);
76284d9c625SLionel Sambuc 	VOP_UNLOCK(vp);
76384d9c625SLionel Sambuc 
76484d9c625SLionel Sambuc out:	*ulr_ret = VTOI(dvp)->i_crap;
76584d9c625SLionel Sambuc 	*vp_ret = vp;
76684d9c625SLionel Sambuc 	return error;
76784d9c625SLionel Sambuc }
76884d9c625SLionel Sambuc 
76984d9c625SLionel Sambuc /*
77084d9c625SLionel Sambuc  * ext2fs_rmdired_p: Check whether the directory vp has been rmdired.
77184d9c625SLionel Sambuc  *
77284d9c625SLionel Sambuc  * vp must be locked and referenced.
77384d9c625SLionel Sambuc  */
77484d9c625SLionel Sambuc static bool
ext2fs_rmdired_p(struct vnode * vp)77584d9c625SLionel Sambuc ext2fs_rmdired_p(struct vnode *vp)
77684d9c625SLionel Sambuc {
77784d9c625SLionel Sambuc 
77884d9c625SLionel Sambuc 	KASSERT(vp != NULL);
77984d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
78084d9c625SLionel Sambuc 	KASSERT(vp->v_type == VDIR);
78184d9c625SLionel Sambuc 
78284d9c625SLionel Sambuc 	/* XXX Is this correct?  */
78384d9c625SLionel Sambuc 	return (ext2fs_size(VTOI(vp)) == 0);
78484d9c625SLionel Sambuc }
78584d9c625SLionel Sambuc 
78684d9c625SLionel Sambuc /*
78784d9c625SLionel Sambuc  * ext2fs_gro_genealogy: Analyze the genealogy of the source and target
78884d9c625SLionel Sambuc  * directories.
78984d9c625SLionel Sambuc  */
79084d9c625SLionel Sambuc static int
ext2fs_gro_genealogy(struct mount * mp,kauth_cred_t cred,struct vnode * fdvp,struct vnode * tdvp,struct vnode ** intermediate_node_ret)79184d9c625SLionel Sambuc ext2fs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
79284d9c625SLionel Sambuc     struct vnode *fdvp, struct vnode *tdvp,
79384d9c625SLionel Sambuc     struct vnode **intermediate_node_ret)
79484d9c625SLionel Sambuc {
79584d9c625SLionel Sambuc 	struct vnode *vp, *dvp;
796*0a6a1f1dSLionel Sambuc 	ino_t dotdot_ino = -1;	/* XXX gcc 4.8.3: maybe-uninitialized */
79784d9c625SLionel Sambuc 	int error;
79884d9c625SLionel Sambuc 
79984d9c625SLionel Sambuc 	KASSERT(mp != NULL);
80084d9c625SLionel Sambuc 	KASSERT(fdvp != NULL);
80184d9c625SLionel Sambuc 	KASSERT(tdvp != NULL);
80284d9c625SLionel Sambuc 	KASSERT(fdvp != tdvp);
80384d9c625SLionel Sambuc 	KASSERT(intermediate_node_ret != NULL);
80484d9c625SLionel Sambuc 	KASSERT(fdvp->v_mount == mp);
80584d9c625SLionel Sambuc 	KASSERT(tdvp->v_mount == mp);
80684d9c625SLionel Sambuc 	KASSERT(fdvp->v_type == VDIR);
80784d9c625SLionel Sambuc 	KASSERT(tdvp->v_type == VDIR);
80884d9c625SLionel Sambuc 
80984d9c625SLionel Sambuc 	/*
81084d9c625SLionel Sambuc 	 * We need to provisionally lock tdvp to keep rmdir from
81184d9c625SLionel Sambuc 	 * deleting it -- or any ancestor -- at an inopportune moment.
81284d9c625SLionel Sambuc 	 */
81384d9c625SLionel Sambuc 	error = ext2fs_gro_lock_directory(mp, tdvp);
81484d9c625SLionel Sambuc 	if (error)
81584d9c625SLionel Sambuc 		return error;
81684d9c625SLionel Sambuc 
81784d9c625SLionel Sambuc 	vp = tdvp;
81884d9c625SLionel Sambuc 	vref(vp);
81984d9c625SLionel Sambuc 
82084d9c625SLionel Sambuc 	for (;;) {
82184d9c625SLionel Sambuc 		KASSERT(vp != NULL);
82284d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
82384d9c625SLionel Sambuc 		KASSERT(vp->v_mount == mp);
82484d9c625SLionel Sambuc 		KASSERT(vp->v_type == VDIR);
82584d9c625SLionel Sambuc 		KASSERT(!ext2fs_rmdired_p(vp));
82684d9c625SLionel Sambuc 
82784d9c625SLionel Sambuc 		/* Did we hit the root without finding fdvp?  */
82884d9c625SLionel Sambuc 		if (VTOI(vp)->i_number == UFS_ROOTINO) {
82984d9c625SLionel Sambuc 			vput(vp);
83084d9c625SLionel Sambuc 			*intermediate_node_ret = NULL;
83184d9c625SLionel Sambuc 			return 0;
83284d9c625SLionel Sambuc 		}
83384d9c625SLionel Sambuc 
83484d9c625SLionel Sambuc 		error = ext2fs_read_dotdot(vp, cred, &dotdot_ino);
83584d9c625SLionel Sambuc 		if (error) {
83684d9c625SLionel Sambuc 			vput(vp);
83784d9c625SLionel Sambuc 			return error;
83884d9c625SLionel Sambuc 		}
83984d9c625SLionel Sambuc 
84084d9c625SLionel Sambuc 		/* Did we find that fdvp is an ancestor of tdvp?  */
84184d9c625SLionel Sambuc 		if (VTOI(fdvp)->i_number == dotdot_ino) {
84284d9c625SLionel Sambuc 			/* Unlock vp, but keep it referenced.  */
84384d9c625SLionel Sambuc 			VOP_UNLOCK(vp);
84484d9c625SLionel Sambuc 			*intermediate_node_ret = vp;
84584d9c625SLionel Sambuc 			return 0;
84684d9c625SLionel Sambuc 		}
84784d9c625SLionel Sambuc 
84884d9c625SLionel Sambuc 		/* Neither -- keep ascending the family tree.  */
849*0a6a1f1dSLionel Sambuc 		error = vcache_get(mp, &dotdot_ino, sizeof(dotdot_ino), &dvp);
850*0a6a1f1dSLionel Sambuc 		vput(vp);
85184d9c625SLionel Sambuc 		if (error)
85284d9c625SLionel Sambuc 			return error;
853*0a6a1f1dSLionel Sambuc 		error = vn_lock(dvp, LK_EXCLUSIVE);
854*0a6a1f1dSLionel Sambuc 		if (error) {
855*0a6a1f1dSLionel Sambuc 			vrele(dvp);
856*0a6a1f1dSLionel Sambuc 			return error;
857*0a6a1f1dSLionel Sambuc 		}
85884d9c625SLionel Sambuc 
85984d9c625SLionel Sambuc 		KASSERT(dvp != NULL);
86084d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
86184d9c625SLionel Sambuc 		vp = dvp;
86284d9c625SLionel Sambuc 
86384d9c625SLionel Sambuc 		if (vp->v_type != VDIR) {
86484d9c625SLionel Sambuc 			/*
86584d9c625SLionel Sambuc 			 * XXX Panic?  Print a warning?  Can this
86684d9c625SLionel Sambuc 			 * happen if we lose the race I suspect to
86784d9c625SLionel Sambuc 			 * exist above, and the `..' inode number has
86884d9c625SLionel Sambuc 			 * been recycled?
86984d9c625SLionel Sambuc 			 */
87084d9c625SLionel Sambuc 			vput(vp);
87184d9c625SLionel Sambuc 			return ENOTDIR;
87284d9c625SLionel Sambuc 		}
87384d9c625SLionel Sambuc 
87484d9c625SLionel Sambuc 		if (ext2fs_rmdired_p(vp)) {
87584d9c625SLionel Sambuc 			vput(vp);
87684d9c625SLionel Sambuc 			return ENOENT;
87784d9c625SLionel Sambuc 		}
87884d9c625SLionel Sambuc 	}
87984d9c625SLionel Sambuc }
88084d9c625SLionel Sambuc 
88184d9c625SLionel Sambuc /*
88284d9c625SLionel Sambuc  * ext2fs_read_dotdot: Store in *ino_ret the inode number of the parent
88384d9c625SLionel Sambuc  * of the directory vp.
88484d9c625SLionel Sambuc  */
88584d9c625SLionel Sambuc static int
ext2fs_read_dotdot(struct vnode * vp,kauth_cred_t cred,ino_t * ino_ret)88684d9c625SLionel Sambuc ext2fs_read_dotdot(struct vnode *vp, kauth_cred_t cred, ino_t *ino_ret)
88784d9c625SLionel Sambuc {
88884d9c625SLionel Sambuc 	struct ext2fs_dirtemplate dirbuf;
88984d9c625SLionel Sambuc 	int error;
89084d9c625SLionel Sambuc 
89184d9c625SLionel Sambuc 	KASSERT(vp != NULL);
89284d9c625SLionel Sambuc 	KASSERT(ino_ret != NULL);
89384d9c625SLionel Sambuc 	KASSERT(vp->v_type == VDIR);
89484d9c625SLionel Sambuc 
895*0a6a1f1dSLionel Sambuc 	error = ufs_bufio(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
896*0a6a1f1dSLionel Sambuc 	    IO_NODELOCKED, cred, NULL, NULL);
89784d9c625SLionel Sambuc 	if (error)
89884d9c625SLionel Sambuc 		return error;
89984d9c625SLionel Sambuc 
90084d9c625SLionel Sambuc 	if (dirbuf.dotdot_namlen != 2 ||
90184d9c625SLionel Sambuc 	    dirbuf.dotdot_name[0] != '.' ||
90284d9c625SLionel Sambuc 	    dirbuf.dotdot_name[1] != '.')
90384d9c625SLionel Sambuc 		/* XXX Panic?  Print warning?  */
90484d9c625SLionel Sambuc 		return ENOTDIR;
90584d9c625SLionel Sambuc 
90684d9c625SLionel Sambuc 	*ino_ret = fs2h32(dirbuf.dotdot_ino);
90784d9c625SLionel Sambuc 	return 0;
90884d9c625SLionel Sambuc }
90984d9c625SLionel Sambuc 
91084d9c625SLionel Sambuc /*
91184d9c625SLionel Sambuc  * ext2fs_rename_replace_dotdot: Change the target of the `..' entry of
91284d9c625SLionel Sambuc  * the directory vp from fdvp to tdvp.
91384d9c625SLionel Sambuc  */
91484d9c625SLionel Sambuc static int
ext2fs_rename_replace_dotdot(struct vnode * vp,struct vnode * fdvp,struct vnode * tdvp,kauth_cred_t cred)91584d9c625SLionel Sambuc ext2fs_rename_replace_dotdot(struct vnode *vp,
91684d9c625SLionel Sambuc     struct vnode *fdvp, struct vnode *tdvp,
91784d9c625SLionel Sambuc     kauth_cred_t cred)
91884d9c625SLionel Sambuc {
91984d9c625SLionel Sambuc 	struct ext2fs_dirtemplate dirbuf;
92084d9c625SLionel Sambuc 	int error;
92184d9c625SLionel Sambuc 
92284d9c625SLionel Sambuc 	/* XXX Does it make sense to do this before the sanity checks below?  */
92384d9c625SLionel Sambuc 	KASSERT(0 < VTOI(fdvp)->i_e2fs_nlink);
92484d9c625SLionel Sambuc 	VTOI(fdvp)->i_e2fs_nlink--;
92584d9c625SLionel Sambuc 	VTOI(fdvp)->i_flag |= IN_CHANGE;
92684d9c625SLionel Sambuc 
927*0a6a1f1dSLionel Sambuc 	error = ufs_bufio(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
928*0a6a1f1dSLionel Sambuc 	    IO_NODELOCKED, cred, NULL, NULL);
92984d9c625SLionel Sambuc 	if (error)
93084d9c625SLionel Sambuc 		return error;
93184d9c625SLionel Sambuc 
93284d9c625SLionel Sambuc 	if (dirbuf.dotdot_namlen != 2 ||
93384d9c625SLionel Sambuc 	    dirbuf.dotdot_name[0] != '.' ||
93484d9c625SLionel Sambuc 	    dirbuf.dotdot_name[1] != '.') {
93584d9c625SLionel Sambuc 		ufs_dirbad(VTOI(vp), (doff_t)12, "bad `..' entry");
93684d9c625SLionel Sambuc 		return 0;
93784d9c625SLionel Sambuc 	}
93884d9c625SLionel Sambuc 
93984d9c625SLionel Sambuc 	if (fs2h32(dirbuf.dotdot_ino) != VTOI(fdvp)->i_number) {
94084d9c625SLionel Sambuc 		ufs_dirbad(VTOI(vp), (doff_t)12,
94184d9c625SLionel Sambuc 		    "`..' does not point at parent");
94284d9c625SLionel Sambuc 		return 0;
94384d9c625SLionel Sambuc 	}
94484d9c625SLionel Sambuc 
94584d9c625SLionel Sambuc 	dirbuf.dotdot_ino = h2fs32(VTOI(tdvp)->i_number);
94684d9c625SLionel Sambuc 	/* XXX WTF?  Why not check error?  */
947*0a6a1f1dSLionel Sambuc 	(void)ufs_bufio(UIO_WRITE, vp, &dirbuf, sizeof dirbuf, (off_t)0,
948*0a6a1f1dSLionel Sambuc 	    (IO_NODELOCKED | IO_SYNC), cred, NULL, NULL);
94984d9c625SLionel Sambuc 
95084d9c625SLionel Sambuc 	return 0;
95184d9c625SLionel Sambuc }
95284d9c625SLionel Sambuc 
95384d9c625SLionel Sambuc /*
95484d9c625SLionel Sambuc  * ext2fs_gro_lock_directory: Lock the directory vp, but fail if it has
95584d9c625SLionel Sambuc  * been rmdir'd.
95684d9c625SLionel Sambuc  */
95784d9c625SLionel Sambuc static int
ext2fs_gro_lock_directory(struct mount * mp,struct vnode * vp)95884d9c625SLionel Sambuc ext2fs_gro_lock_directory(struct mount *mp, struct vnode *vp)
95984d9c625SLionel Sambuc {
96084d9c625SLionel Sambuc 
96184d9c625SLionel Sambuc 	(void)mp;
96284d9c625SLionel Sambuc 	KASSERT(mp != NULL);
96384d9c625SLionel Sambuc 	KASSERT(vp != NULL);
96484d9c625SLionel Sambuc 	KASSERT(vp->v_mount == mp);
96584d9c625SLionel Sambuc 
96684d9c625SLionel Sambuc 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
96784d9c625SLionel Sambuc 
96884d9c625SLionel Sambuc 	if (ext2fs_rmdired_p(vp)) {
96984d9c625SLionel Sambuc 		VOP_UNLOCK(vp);
97084d9c625SLionel Sambuc 		return ENOENT;
97184d9c625SLionel Sambuc 	}
97284d9c625SLionel Sambuc 
97384d9c625SLionel Sambuc 	return 0;
97484d9c625SLionel Sambuc }
97584d9c625SLionel Sambuc 
97684d9c625SLionel Sambuc static const struct genfs_rename_ops ext2fs_genfs_rename_ops = {
97784d9c625SLionel Sambuc 	.gro_directory_empty_p		= ext2fs_gro_directory_empty_p,
97884d9c625SLionel Sambuc 	.gro_rename_check_possible	= ext2fs_gro_rename_check_possible,
97984d9c625SLionel Sambuc 	.gro_rename_check_permitted	= ext2fs_gro_rename_check_permitted,
98084d9c625SLionel Sambuc 	.gro_remove_check_possible	= ext2fs_gro_remove_check_possible,
98184d9c625SLionel Sambuc 	.gro_remove_check_permitted	= ext2fs_gro_remove_check_permitted,
98284d9c625SLionel Sambuc 	.gro_rename			= ext2fs_gro_rename,
98384d9c625SLionel Sambuc 	.gro_remove			= ext2fs_gro_remove,
98484d9c625SLionel Sambuc 	.gro_lookup			= ext2fs_gro_lookup,
98584d9c625SLionel Sambuc 	.gro_genealogy			= ext2fs_gro_genealogy,
98684d9c625SLionel Sambuc 	.gro_lock_directory		= ext2fs_gro_lock_directory,
98784d9c625SLionel Sambuc };
988