1*0a6a1f1dSLionel Sambuc /* $NetBSD: lfs_rename.c,v 1.16 2015/09/21 01:24:23 dholland Exp $ */
284d9c625SLionel Sambuc /* from NetBSD: ufs_rename.c,v 1.6 2013/01/22 09:39:18 dholland Exp */
384d9c625SLionel Sambuc
484d9c625SLionel Sambuc /*-
584d9c625SLionel Sambuc * Copyright (c) 2012 The NetBSD Foundation, Inc.
684d9c625SLionel Sambuc * All rights reserved.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
984d9c625SLionel Sambuc * by Taylor R Campbell.
1084d9c625SLionel Sambuc *
1184d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1284d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1384d9c625SLionel Sambuc * are met:
1484d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1584d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1684d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1784d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1884d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
1984d9c625SLionel Sambuc *
2084d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2184d9c625SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2284d9c625SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2384d9c625SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2484d9c625SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2584d9c625SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2684d9c625SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2784d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2884d9c625SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2984d9c625SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3084d9c625SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3184d9c625SLionel Sambuc */
3284d9c625SLionel Sambuc /*-
3384d9c625SLionel Sambuc * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
3484d9c625SLionel Sambuc * All rights reserved.
3584d9c625SLionel Sambuc *
3684d9c625SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
3784d9c625SLionel Sambuc * by Konrad E. Schroder <perseant@hhhh.org>.
3884d9c625SLionel Sambuc *
3984d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
4084d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
4184d9c625SLionel Sambuc * are met:
4284d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
4384d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
4484d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
4584d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
4684d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
4784d9c625SLionel Sambuc *
4884d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4984d9c625SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5084d9c625SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5184d9c625SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5284d9c625SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5384d9c625SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5484d9c625SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5584d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5684d9c625SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5784d9c625SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5884d9c625SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
5984d9c625SLionel Sambuc */
6084d9c625SLionel Sambuc /*
6184d9c625SLionel Sambuc * Copyright (c) 1986, 1989, 1991, 1993, 1995
6284d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
6384d9c625SLionel Sambuc *
6484d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
6584d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
6684d9c625SLionel Sambuc * are met:
6784d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
6884d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
6984d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
7084d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
7184d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
7284d9c625SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
7384d9c625SLionel Sambuc * may be used to endorse or promote products derived from this software
7484d9c625SLionel Sambuc * without specific prior written permission.
7584d9c625SLionel Sambuc *
7684d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
7784d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7884d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7984d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
8084d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8184d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
8284d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8384d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8484d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8584d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8684d9c625SLionel Sambuc * SUCH DAMAGE.
8784d9c625SLionel Sambuc *
8884d9c625SLionel Sambuc * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
8984d9c625SLionel Sambuc */
9084d9c625SLionel Sambuc
9184d9c625SLionel Sambuc #include <sys/cdefs.h>
92*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: lfs_rename.c,v 1.16 2015/09/21 01:24:23 dholland Exp $");
9384d9c625SLionel Sambuc
9484d9c625SLionel Sambuc #include <sys/param.h>
9584d9c625SLionel Sambuc #include <sys/systm.h>
9684d9c625SLionel Sambuc #include <sys/errno.h>
9784d9c625SLionel Sambuc #include <sys/namei.h>
9884d9c625SLionel Sambuc #include <sys/resourcevar.h>
9984d9c625SLionel Sambuc #include <sys/kernel.h>
10084d9c625SLionel Sambuc #include <sys/file.h>
10184d9c625SLionel Sambuc #include <sys/stat.h>
10284d9c625SLionel Sambuc #include <sys/buf.h>
10384d9c625SLionel Sambuc #include <sys/proc.h>
10484d9c625SLionel Sambuc #include <sys/mount.h>
10584d9c625SLionel Sambuc #include <sys/vnode.h>
10684d9c625SLionel Sambuc #include <sys/vnode_if.h>
10784d9c625SLionel Sambuc #include <sys/pool.h>
10884d9c625SLionel Sambuc #include <sys/signalvar.h>
10984d9c625SLionel Sambuc #include <sys/kauth.h>
11084d9c625SLionel Sambuc #include <sys/syslog.h>
11184d9c625SLionel Sambuc
11284d9c625SLionel Sambuc #include <uvm/uvm.h>
11384d9c625SLionel Sambuc #include <uvm/uvm_pmap.h>
11484d9c625SLionel Sambuc #include <uvm/uvm_stat.h>
11584d9c625SLionel Sambuc #include <uvm/uvm_pager.h>
11684d9c625SLionel Sambuc
11784d9c625SLionel Sambuc #include <miscfs/fifofs/fifo.h>
11884d9c625SLionel Sambuc #include <miscfs/genfs/genfs.h>
11984d9c625SLionel Sambuc #include <miscfs/specfs/specdev.h>
12084d9c625SLionel Sambuc
12184d9c625SLionel Sambuc #include <ufs/lfs/ulfs_inode.h>
12284d9c625SLionel Sambuc #include <ufs/lfs/ulfsmount.h>
12384d9c625SLionel Sambuc #include <ufs/lfs/ulfs_bswap.h>
12484d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extern.h>
12584d9c625SLionel Sambuc
12684d9c625SLionel Sambuc #include <ufs/lfs/lfs.h>
127*0a6a1f1dSLionel Sambuc #include <ufs/lfs/lfs_accessors.h>
12884d9c625SLionel Sambuc #include <ufs/lfs/lfs_extern.h>
12984d9c625SLionel Sambuc
13084d9c625SLionel Sambuc /*
13184d9c625SLionel Sambuc * ulfs_gro_directory_empty_p: Return true if the directory vp is
13284d9c625SLionel Sambuc * empty. dvp is its parent.
13384d9c625SLionel Sambuc *
13484d9c625SLionel Sambuc * vp and dvp must be locked and referenced.
13584d9c625SLionel Sambuc */
13684d9c625SLionel Sambuc static bool
ulfs_gro_directory_empty_p(struct mount * mp,kauth_cred_t cred,struct vnode * vp,struct vnode * dvp)13784d9c625SLionel Sambuc ulfs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
13884d9c625SLionel Sambuc struct vnode *vp, struct vnode *dvp)
13984d9c625SLionel Sambuc {
14084d9c625SLionel Sambuc
14184d9c625SLionel Sambuc (void)mp;
14284d9c625SLionel Sambuc KASSERT(mp != NULL);
14384d9c625SLionel Sambuc KASSERT(vp != NULL);
14484d9c625SLionel Sambuc KASSERT(dvp != NULL);
14584d9c625SLionel Sambuc KASSERT(vp != dvp);
14684d9c625SLionel Sambuc KASSERT(vp->v_mount == mp);
14784d9c625SLionel Sambuc KASSERT(dvp->v_mount == mp);
14884d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
14984d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
15084d9c625SLionel Sambuc
15184d9c625SLionel Sambuc return ulfs_dirempty(VTOI(vp), VTOI(dvp)->i_number, cred);
15284d9c625SLionel Sambuc }
15384d9c625SLionel Sambuc
15484d9c625SLionel Sambuc /*
15584d9c625SLionel Sambuc * ulfs_gro_rename_check_possible: Check whether a rename is possible
15684d9c625SLionel Sambuc * independent of credentials.
15784d9c625SLionel Sambuc */
15884d9c625SLionel Sambuc static int
ulfs_gro_rename_check_possible(struct mount * mp,struct vnode * fdvp,struct vnode * fvp,struct vnode * tdvp,struct vnode * tvp)15984d9c625SLionel Sambuc ulfs_gro_rename_check_possible(struct mount *mp,
16084d9c625SLionel Sambuc struct vnode *fdvp, struct vnode *fvp,
16184d9c625SLionel Sambuc struct vnode *tdvp, struct vnode *tvp)
16284d9c625SLionel Sambuc {
16384d9c625SLionel Sambuc
16484d9c625SLionel Sambuc (void)mp;
16584d9c625SLionel Sambuc KASSERT(mp != NULL);
16684d9c625SLionel Sambuc KASSERT(fdvp != NULL);
16784d9c625SLionel Sambuc KASSERT(fvp != NULL);
16884d9c625SLionel Sambuc KASSERT(tdvp != NULL);
16984d9c625SLionel Sambuc KASSERT(fdvp != fvp);
17084d9c625SLionel Sambuc KASSERT(fdvp != tvp);
17184d9c625SLionel Sambuc KASSERT(tdvp != fvp);
17284d9c625SLionel Sambuc KASSERT(tdvp != tvp);
17384d9c625SLionel Sambuc KASSERT(fvp != tvp);
17484d9c625SLionel Sambuc KASSERT(fdvp->v_type == VDIR);
17584d9c625SLionel Sambuc KASSERT(tdvp->v_type == VDIR);
17684d9c625SLionel Sambuc KASSERT(fdvp->v_mount == mp);
17784d9c625SLionel Sambuc KASSERT(fvp->v_mount == mp);
17884d9c625SLionel Sambuc KASSERT(tdvp->v_mount == mp);
17984d9c625SLionel Sambuc KASSERT((tvp == NULL) || (tvp->v_mount == mp));
18084d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
18184d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
18284d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
18384d9c625SLionel Sambuc KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
18484d9c625SLionel Sambuc
18584d9c625SLionel Sambuc return genfs_ufslike_rename_check_possible(
18684d9c625SLionel Sambuc VTOI(fdvp)->i_flags, VTOI(fvp)->i_flags,
18784d9c625SLionel Sambuc VTOI(tdvp)->i_flags, (tvp? VTOI(tvp)->i_flags : 0),
18884d9c625SLionel Sambuc (tvp != NULL),
18984d9c625SLionel Sambuc IMMUTABLE, APPEND);
19084d9c625SLionel Sambuc }
19184d9c625SLionel Sambuc
19284d9c625SLionel Sambuc /*
19384d9c625SLionel Sambuc * ulfs_gro_rename_check_permitted: Check whether a rename is permitted
19484d9c625SLionel Sambuc * given our credentials.
19584d9c625SLionel Sambuc */
19684d9c625SLionel Sambuc static int
ulfs_gro_rename_check_permitted(struct mount * mp,kauth_cred_t cred,struct vnode * fdvp,struct vnode * fvp,struct vnode * tdvp,struct vnode * tvp)19784d9c625SLionel Sambuc ulfs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
19884d9c625SLionel Sambuc struct vnode *fdvp, struct vnode *fvp,
19984d9c625SLionel Sambuc struct vnode *tdvp, struct vnode *tvp)
20084d9c625SLionel Sambuc {
20184d9c625SLionel Sambuc
20284d9c625SLionel Sambuc (void)mp;
20384d9c625SLionel Sambuc KASSERT(mp != NULL);
20484d9c625SLionel Sambuc KASSERT(fdvp != NULL);
20584d9c625SLionel Sambuc KASSERT(fvp != NULL);
20684d9c625SLionel Sambuc KASSERT(tdvp != NULL);
20784d9c625SLionel Sambuc KASSERT(fdvp != fvp);
20884d9c625SLionel Sambuc KASSERT(fdvp != tvp);
20984d9c625SLionel Sambuc KASSERT(tdvp != fvp);
21084d9c625SLionel Sambuc KASSERT(tdvp != tvp);
21184d9c625SLionel Sambuc KASSERT(fvp != tvp);
21284d9c625SLionel Sambuc KASSERT(fdvp->v_type == VDIR);
21384d9c625SLionel Sambuc KASSERT(tdvp->v_type == VDIR);
21484d9c625SLionel Sambuc KASSERT(fdvp->v_mount == mp);
21584d9c625SLionel Sambuc KASSERT(fvp->v_mount == mp);
21684d9c625SLionel Sambuc KASSERT(tdvp->v_mount == mp);
21784d9c625SLionel Sambuc KASSERT((tvp == NULL) || (tvp->v_mount == mp));
21884d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
21984d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
22084d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
22184d9c625SLionel Sambuc KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
22284d9c625SLionel Sambuc
22384d9c625SLionel Sambuc return genfs_ufslike_rename_check_permitted(cred,
22484d9c625SLionel Sambuc fdvp, VTOI(fdvp)->i_mode, VTOI(fdvp)->i_uid,
22584d9c625SLionel Sambuc fvp, VTOI(fvp)->i_uid,
22684d9c625SLionel Sambuc tdvp, VTOI(tdvp)->i_mode, VTOI(tdvp)->i_uid,
22784d9c625SLionel Sambuc tvp, (tvp? VTOI(tvp)->i_uid : 0));
22884d9c625SLionel Sambuc }
22984d9c625SLionel Sambuc
23084d9c625SLionel Sambuc /*
23184d9c625SLionel Sambuc * ulfs_gro_remove_check_possible: Check whether a remove is possible
23284d9c625SLionel Sambuc * independent of credentials.
23384d9c625SLionel Sambuc */
23484d9c625SLionel Sambuc static int
ulfs_gro_remove_check_possible(struct mount * mp,struct vnode * dvp,struct vnode * vp)23584d9c625SLionel Sambuc ulfs_gro_remove_check_possible(struct mount *mp,
23684d9c625SLionel Sambuc struct vnode *dvp, struct vnode *vp)
23784d9c625SLionel Sambuc {
23884d9c625SLionel Sambuc
23984d9c625SLionel Sambuc (void)mp;
24084d9c625SLionel Sambuc KASSERT(mp != NULL);
24184d9c625SLionel Sambuc KASSERT(dvp != NULL);
24284d9c625SLionel Sambuc KASSERT(vp != NULL);
24384d9c625SLionel Sambuc KASSERT(dvp != vp);
24484d9c625SLionel Sambuc KASSERT(dvp->v_type == VDIR);
24584d9c625SLionel Sambuc KASSERT(vp->v_type != VDIR);
24684d9c625SLionel Sambuc KASSERT(dvp->v_mount == mp);
24784d9c625SLionel Sambuc KASSERT(vp->v_mount == mp);
24884d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
24984d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
25084d9c625SLionel Sambuc
25184d9c625SLionel Sambuc return genfs_ufslike_remove_check_possible(
25284d9c625SLionel Sambuc VTOI(dvp)->i_flags, VTOI(vp)->i_flags,
25384d9c625SLionel Sambuc IMMUTABLE, APPEND);
25484d9c625SLionel Sambuc }
25584d9c625SLionel Sambuc
25684d9c625SLionel Sambuc /*
25784d9c625SLionel Sambuc * ulfs_gro_remove_check_permitted: Check whether a remove is permitted
25884d9c625SLionel Sambuc * given our credentials.
25984d9c625SLionel Sambuc */
26084d9c625SLionel Sambuc static int
ulfs_gro_remove_check_permitted(struct mount * mp,kauth_cred_t cred,struct vnode * dvp,struct vnode * vp)26184d9c625SLionel Sambuc ulfs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
26284d9c625SLionel Sambuc struct vnode *dvp, struct vnode *vp)
26384d9c625SLionel Sambuc {
26484d9c625SLionel Sambuc
26584d9c625SLionel Sambuc (void)mp;
26684d9c625SLionel Sambuc KASSERT(mp != NULL);
26784d9c625SLionel Sambuc KASSERT(dvp != NULL);
26884d9c625SLionel Sambuc KASSERT(vp != NULL);
26984d9c625SLionel Sambuc KASSERT(dvp != vp);
27084d9c625SLionel Sambuc KASSERT(dvp->v_type == VDIR);
27184d9c625SLionel Sambuc KASSERT(vp->v_type != VDIR);
27284d9c625SLionel Sambuc KASSERT(dvp->v_mount == mp);
27384d9c625SLionel Sambuc KASSERT(vp->v_mount == mp);
27484d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
27584d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
27684d9c625SLionel Sambuc
27784d9c625SLionel Sambuc return genfs_ufslike_remove_check_permitted(cred,
27884d9c625SLionel Sambuc dvp, VTOI(dvp)->i_mode, VTOI(dvp)->i_uid, vp, VTOI(vp)->i_uid);
27984d9c625SLionel Sambuc }
28084d9c625SLionel Sambuc
28184d9c625SLionel Sambuc /*
28284d9c625SLionel Sambuc * ulfs_rename_ulr_overlap_p: True iff tulr overlaps with fulr so that
28384d9c625SLionel Sambuc * entering a directory entry at tulr may move fulr.
28484d9c625SLionel Sambuc */
28584d9c625SLionel Sambuc static bool
ulfs_rename_ulr_overlap_p(const struct ulfs_lookup_results * fulr,const struct ulfs_lookup_results * tulr)28684d9c625SLionel Sambuc ulfs_rename_ulr_overlap_p(const struct ulfs_lookup_results *fulr,
28784d9c625SLionel Sambuc const struct ulfs_lookup_results *tulr)
28884d9c625SLionel Sambuc {
28984d9c625SLionel Sambuc doff_t from_prev_start, from_prev_end, to_start, to_end;
29084d9c625SLionel Sambuc
29184d9c625SLionel Sambuc KASSERT(fulr != NULL);
29284d9c625SLionel Sambuc KASSERT(tulr != NULL);
29384d9c625SLionel Sambuc KASSERT(fulr != tulr);
29484d9c625SLionel Sambuc
29584d9c625SLionel Sambuc /*
29684d9c625SLionel Sambuc * fulr is from a DELETE lookup, so fulr->ulr_count is the size
29784d9c625SLionel Sambuc * of the preceding entry (d_reclen).
29884d9c625SLionel Sambuc */
29984d9c625SLionel Sambuc from_prev_end = fulr->ulr_offset;
30084d9c625SLionel Sambuc KASSERT(fulr->ulr_count <= from_prev_end);
30184d9c625SLionel Sambuc from_prev_start = (from_prev_end - fulr->ulr_count);
30284d9c625SLionel Sambuc
30384d9c625SLionel Sambuc /*
30484d9c625SLionel Sambuc * tulr is from a RENAME lookup, so tulr->ulr_count is the size
30584d9c625SLionel Sambuc * of the free space for an entry that we are about to fill.
30684d9c625SLionel Sambuc */
30784d9c625SLionel Sambuc to_start = tulr->ulr_offset;
30884d9c625SLionel Sambuc KASSERT(tulr->ulr_count < (LFS_MAXDIRSIZE - to_start));
30984d9c625SLionel Sambuc to_end = (to_start + tulr->ulr_count);
31084d9c625SLionel Sambuc
31184d9c625SLionel Sambuc return
31284d9c625SLionel Sambuc (((to_start <= from_prev_start) && (from_prev_start < to_end)) ||
31384d9c625SLionel Sambuc ((to_start <= from_prev_end) && (from_prev_end < to_end)));
31484d9c625SLionel Sambuc }
31584d9c625SLionel Sambuc
31684d9c625SLionel Sambuc /*
31784d9c625SLionel Sambuc * ulfs_direct_namlen: Return the namlen of the directory entry ep from
31884d9c625SLionel Sambuc * the directory vp.
31984d9c625SLionel Sambuc */
32084d9c625SLionel Sambuc static int /* XXX int? uint8_t? */
ulfs_direct_namlen(const LFS_DIRHEADER * ep,const struct vnode * vp)321*0a6a1f1dSLionel Sambuc ulfs_direct_namlen(const LFS_DIRHEADER *ep, const struct vnode *vp)
32284d9c625SLionel Sambuc {
323*0a6a1f1dSLionel Sambuc struct lfs *fs;
32484d9c625SLionel Sambuc
32584d9c625SLionel Sambuc KASSERT(ep != NULL);
32684d9c625SLionel Sambuc KASSERT(vp != NULL);
32784d9c625SLionel Sambuc KASSERT(VTOI(vp) != NULL);
32884d9c625SLionel Sambuc KASSERT(VTOI(vp)->i_ump != NULL);
329*0a6a1f1dSLionel Sambuc KASSERT(VTOI(vp)->i_lfs != NULL);
330*0a6a1f1dSLionel Sambuc fs = VTOI(vp)->i_lfs;
33184d9c625SLionel Sambuc
332*0a6a1f1dSLionel Sambuc return lfs_dir_getnamlen(fs, ep);
33384d9c625SLionel Sambuc }
33484d9c625SLionel Sambuc
33584d9c625SLionel Sambuc /*
33684d9c625SLionel Sambuc * ulfs_rename_recalculate_fulr: If we have just entered a directory into
33784d9c625SLionel Sambuc * dvp at tulr, and we were about to remove one at fulr for an entry
33884d9c625SLionel Sambuc * named fcnp, fulr may be invalid. So, if necessary, recalculate it.
33984d9c625SLionel Sambuc */
34084d9c625SLionel Sambuc static int
ulfs_rename_recalculate_fulr(struct vnode * dvp,struct ulfs_lookup_results * fulr,const struct ulfs_lookup_results * tulr,const struct componentname * fcnp)34184d9c625SLionel Sambuc ulfs_rename_recalculate_fulr(struct vnode *dvp,
34284d9c625SLionel Sambuc struct ulfs_lookup_results *fulr, const struct ulfs_lookup_results *tulr,
34384d9c625SLionel Sambuc const struct componentname *fcnp)
34484d9c625SLionel Sambuc {
34584d9c625SLionel Sambuc struct mount *mp;
34684d9c625SLionel Sambuc struct lfs *fs;
34784d9c625SLionel Sambuc struct ulfsmount *ump;
34884d9c625SLionel Sambuc /* XXX int is a silly type for this; blame ulfsmount::um_dirblksiz. */
34984d9c625SLionel Sambuc int dirblksiz;
35084d9c625SLionel Sambuc doff_t search_start, search_end;
35184d9c625SLionel Sambuc doff_t offset; /* Offset of entry we're examining. */
35284d9c625SLionel Sambuc struct buf *bp; /* I/O block we're examining. */
35384d9c625SLionel Sambuc char *dirbuf; /* Pointer into directory at search_start. */
354*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *ep; /* Pointer to the entry we're examining. */
35584d9c625SLionel Sambuc /* XXX direct::d_reclen is 16-bit;
35684d9c625SLionel Sambuc * ulfs_lookup_results::ulr_reclen is 32-bit. Blah. */
35784d9c625SLionel Sambuc uint32_t reclen; /* Length of the entry we're examining. */
35884d9c625SLionel Sambuc uint32_t prev_reclen; /* Length of the preceding entry. */
35984d9c625SLionel Sambuc int error;
36084d9c625SLionel Sambuc
36184d9c625SLionel Sambuc KASSERT(dvp != NULL);
36284d9c625SLionel Sambuc KASSERT(dvp->v_mount != NULL);
36384d9c625SLionel Sambuc KASSERT(VTOI(dvp) != NULL);
36484d9c625SLionel Sambuc KASSERT(fulr != NULL);
36584d9c625SLionel Sambuc KASSERT(tulr != NULL);
36684d9c625SLionel Sambuc KASSERT(fulr != tulr);
36784d9c625SLionel Sambuc KASSERT(ulfs_rename_ulr_overlap_p(fulr, tulr));
36884d9c625SLionel Sambuc
36984d9c625SLionel Sambuc mp = dvp->v_mount;
37084d9c625SLionel Sambuc ump = VFSTOULFS(mp);
37184d9c625SLionel Sambuc fs = ump->um_lfs;
37284d9c625SLionel Sambuc KASSERT(ump != NULL);
37384d9c625SLionel Sambuc KASSERT(ump == VTOI(dvp)->i_ump);
37484d9c625SLionel Sambuc KASSERT(fs == VTOI(dvp)->i_lfs);
37584d9c625SLionel Sambuc
37684d9c625SLionel Sambuc dirblksiz = fs->um_dirblksiz;
37784d9c625SLionel Sambuc KASSERT(0 < dirblksiz);
37884d9c625SLionel Sambuc KASSERT((dirblksiz & (dirblksiz - 1)) == 0);
37984d9c625SLionel Sambuc
38084d9c625SLionel Sambuc /* A directory block may not span across multiple I/O blocks. */
38184d9c625SLionel Sambuc KASSERT(dirblksiz <= mp->mnt_stat.f_iosize);
38284d9c625SLionel Sambuc
38384d9c625SLionel Sambuc /* Find the bounds of the search. */
38484d9c625SLionel Sambuc search_start = tulr->ulr_offset;
38584d9c625SLionel Sambuc KASSERT(fulr->ulr_reclen < (LFS_MAXDIRSIZE - fulr->ulr_offset));
38684d9c625SLionel Sambuc search_end = (fulr->ulr_offset + fulr->ulr_reclen);
38784d9c625SLionel Sambuc
38884d9c625SLionel Sambuc /* Compaction must happen only within a directory block. (*) */
38984d9c625SLionel Sambuc KASSERT(search_start <= search_end);
39084d9c625SLionel Sambuc KASSERT((search_end - (search_start &~ (dirblksiz - 1))) <= dirblksiz);
39184d9c625SLionel Sambuc
39284d9c625SLionel Sambuc dirbuf = NULL;
39384d9c625SLionel Sambuc bp = NULL;
39484d9c625SLionel Sambuc error = ulfs_blkatoff(dvp, (off_t)search_start, &dirbuf, &bp, false);
39584d9c625SLionel Sambuc if (error)
39684d9c625SLionel Sambuc return error;
39784d9c625SLionel Sambuc KASSERT(dirbuf != NULL);
39884d9c625SLionel Sambuc KASSERT(bp != NULL);
39984d9c625SLionel Sambuc
40084d9c625SLionel Sambuc /*
40184d9c625SLionel Sambuc * Guarantee we sha'n't go past the end of the buffer we got.
40284d9c625SLionel Sambuc * dirbuf is bp->b_data + (search_start & (iosize - 1)), and
40384d9c625SLionel Sambuc * the valid range is [bp->b_data, bp->b_data + bp->b_bcount).
40484d9c625SLionel Sambuc */
40584d9c625SLionel Sambuc KASSERT((search_end - search_start) <=
40684d9c625SLionel Sambuc (bp->b_bcount - (search_start & (mp->mnt_stat.f_iosize - 1))));
40784d9c625SLionel Sambuc
40884d9c625SLionel Sambuc prev_reclen = fulr->ulr_count;
40984d9c625SLionel Sambuc offset = search_start;
41084d9c625SLionel Sambuc
41184d9c625SLionel Sambuc /*
41284d9c625SLionel Sambuc * Search from search_start to search_end for the entry matching
41384d9c625SLionel Sambuc * fcnp, which must be there because we found it before and it
41484d9c625SLionel Sambuc * should only at most have moved earlier.
41584d9c625SLionel Sambuc */
41684d9c625SLionel Sambuc for (;;) {
41784d9c625SLionel Sambuc KASSERT(search_start <= offset);
41884d9c625SLionel Sambuc KASSERT(offset < search_end);
41984d9c625SLionel Sambuc
42084d9c625SLionel Sambuc /*
42184d9c625SLionel Sambuc * Examine the directory entry at offset.
42284d9c625SLionel Sambuc */
423*0a6a1f1dSLionel Sambuc ep = (LFS_DIRHEADER *)(dirbuf + (offset - search_start));
424*0a6a1f1dSLionel Sambuc reclen = lfs_dir_getreclen(fs, ep);
42584d9c625SLionel Sambuc
426*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, ep) == 0)
42784d9c625SLionel Sambuc goto next; /* Entry is unused. */
42884d9c625SLionel Sambuc
429*0a6a1f1dSLionel Sambuc if (lfs_dir_getino(fs, ep) == ULFS_WINO)
43084d9c625SLionel Sambuc goto next; /* Entry is whiteout. */
43184d9c625SLionel Sambuc
43284d9c625SLionel Sambuc if (fcnp->cn_namelen != ulfs_direct_namlen(ep, dvp))
43384d9c625SLionel Sambuc goto next; /* Wrong name length. */
43484d9c625SLionel Sambuc
435*0a6a1f1dSLionel Sambuc if (memcmp(lfs_dir_nameptr(fs, ep), fcnp->cn_nameptr, fcnp->cn_namelen))
43684d9c625SLionel Sambuc goto next; /* Wrong name. */
43784d9c625SLionel Sambuc
43884d9c625SLionel Sambuc /* Got it! */
43984d9c625SLionel Sambuc break;
44084d9c625SLionel Sambuc
44184d9c625SLionel Sambuc next:
44284d9c625SLionel Sambuc if (! ((reclen < search_end) &&
44384d9c625SLionel Sambuc (offset < (search_end - reclen)))) {
44484d9c625SLionel Sambuc brelse(bp, 0);
44584d9c625SLionel Sambuc return EIO; /* XXX Panic? What? */
44684d9c625SLionel Sambuc }
44784d9c625SLionel Sambuc
44884d9c625SLionel Sambuc /* We may not move past the search end. */
44984d9c625SLionel Sambuc KASSERT(reclen < search_end);
45084d9c625SLionel Sambuc KASSERT(offset < (search_end - reclen));
45184d9c625SLionel Sambuc
45284d9c625SLionel Sambuc /*
45384d9c625SLionel Sambuc * We may not move across a directory block boundary;
45484d9c625SLionel Sambuc * see (*) above.
45584d9c625SLionel Sambuc */
45684d9c625SLionel Sambuc KASSERT((offset &~ (dirblksiz - 1)) ==
45784d9c625SLionel Sambuc ((offset + reclen) &~ (dirblksiz - 1)));
45884d9c625SLionel Sambuc
45984d9c625SLionel Sambuc prev_reclen = reclen;
46084d9c625SLionel Sambuc offset += reclen;
46184d9c625SLionel Sambuc }
46284d9c625SLionel Sambuc
46384d9c625SLionel Sambuc /*
46484d9c625SLionel Sambuc * Found the entry. Record where.
46584d9c625SLionel Sambuc */
46684d9c625SLionel Sambuc fulr->ulr_offset = offset;
46784d9c625SLionel Sambuc fulr->ulr_reclen = reclen;
46884d9c625SLionel Sambuc
46984d9c625SLionel Sambuc /*
47084d9c625SLionel Sambuc * Record the preceding record length, but not if we're at the
47184d9c625SLionel Sambuc * start of a directory block.
47284d9c625SLionel Sambuc */
47384d9c625SLionel Sambuc fulr->ulr_count = ((offset & (dirblksiz - 1))? prev_reclen : 0);
47484d9c625SLionel Sambuc
47584d9c625SLionel Sambuc brelse(bp, 0);
47684d9c625SLionel Sambuc return 0;
47784d9c625SLionel Sambuc }
47884d9c625SLionel Sambuc
47984d9c625SLionel Sambuc /*
48084d9c625SLionel Sambuc * ulfs_gro_remove: Rename an object over another link to itself,
48184d9c625SLionel Sambuc * effectively removing just the original link.
48284d9c625SLionel Sambuc */
48384d9c625SLionel Sambuc static int
ulfs_gro_remove(struct mount * mp,kauth_cred_t cred,struct vnode * dvp,struct componentname * cnp,void * de,struct vnode * vp)48484d9c625SLionel Sambuc ulfs_gro_remove(struct mount *mp, kauth_cred_t cred,
48584d9c625SLionel Sambuc struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
48684d9c625SLionel Sambuc {
48784d9c625SLionel Sambuc struct ulfs_lookup_results *ulr = de;
48884d9c625SLionel Sambuc int error;
48984d9c625SLionel Sambuc
49084d9c625SLionel Sambuc KASSERT(mp != NULL);
49184d9c625SLionel Sambuc KASSERT(dvp != NULL);
49284d9c625SLionel Sambuc KASSERT(cnp != NULL);
49384d9c625SLionel Sambuc KASSERT(ulr != NULL);
49484d9c625SLionel Sambuc KASSERT(vp != NULL);
49584d9c625SLionel Sambuc KASSERT(dvp != vp);
49684d9c625SLionel Sambuc KASSERT(dvp->v_mount == mp);
49784d9c625SLionel Sambuc KASSERT(vp->v_mount == mp);
49884d9c625SLionel Sambuc KASSERT(dvp->v_type == VDIR);
49984d9c625SLionel Sambuc KASSERT(vp->v_type != VDIR);
50084d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
50184d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
50284d9c625SLionel Sambuc KASSERT(cnp->cn_nameiop == DELETE);
50384d9c625SLionel Sambuc
50484d9c625SLionel Sambuc /* XXX ulfs_dirremove decrements vp's link count for us. */
50584d9c625SLionel Sambuc error = ulfs_dirremove(dvp, ulr, VTOI(vp), cnp->cn_flags, 0);
50684d9c625SLionel Sambuc if (error)
50784d9c625SLionel Sambuc goto out1;
50884d9c625SLionel Sambuc
50984d9c625SLionel Sambuc VN_KNOTE(dvp, NOTE_WRITE);
51084d9c625SLionel Sambuc VN_KNOTE(vp, (VTOI(vp)->i_nlink? NOTE_LINK : NOTE_DELETE));
51184d9c625SLionel Sambuc
51284d9c625SLionel Sambuc out1:
51384d9c625SLionel Sambuc return error;
51484d9c625SLionel Sambuc }
51584d9c625SLionel Sambuc
51684d9c625SLionel Sambuc /*
51784d9c625SLionel Sambuc * ulfs_gro_lookup: Look up and save the lookup results.
51884d9c625SLionel Sambuc */
51984d9c625SLionel Sambuc static int
ulfs_gro_lookup(struct mount * mp,struct vnode * dvp,struct componentname * cnp,void * de_ret,struct vnode ** vp_ret)52084d9c625SLionel Sambuc ulfs_gro_lookup(struct mount *mp, struct vnode *dvp,
52184d9c625SLionel Sambuc struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
52284d9c625SLionel Sambuc {
52384d9c625SLionel Sambuc struct ulfs_lookup_results *ulr_ret = de_ret;
52484d9c625SLionel Sambuc struct vnode *vp = NULL;
52584d9c625SLionel Sambuc int error;
52684d9c625SLionel Sambuc
52784d9c625SLionel Sambuc (void)mp;
52884d9c625SLionel Sambuc KASSERT(mp != NULL);
52984d9c625SLionel Sambuc KASSERT(dvp != NULL);
53084d9c625SLionel Sambuc KASSERT(cnp != NULL);
53184d9c625SLionel Sambuc KASSERT(ulr_ret != NULL);
53284d9c625SLionel Sambuc KASSERT(vp_ret != NULL);
53384d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
53484d9c625SLionel Sambuc
53584d9c625SLionel Sambuc /* Kludge cargo-culted from dholland's ulfs_rename. */
53684d9c625SLionel Sambuc cnp->cn_flags &=~ MODMASK;
53784d9c625SLionel Sambuc cnp->cn_flags |= (LOCKPARENT | LOCKLEAF);
53884d9c625SLionel Sambuc
53984d9c625SLionel Sambuc error = relookup(dvp, &vp, cnp, 0 /* dummy */);
54084d9c625SLionel Sambuc if ((error == 0) && (vp == NULL)) {
54184d9c625SLionel Sambuc error = ENOENT;
54284d9c625SLionel Sambuc goto out;
54384d9c625SLionel Sambuc } else if (error) {
54484d9c625SLionel Sambuc return error;
54584d9c625SLionel Sambuc }
54684d9c625SLionel Sambuc
54784d9c625SLionel Sambuc /*
54884d9c625SLionel Sambuc * Thanks to VFS insanity, relookup locks vp, which screws us
54984d9c625SLionel Sambuc * in various ways.
55084d9c625SLionel Sambuc */
55184d9c625SLionel Sambuc KASSERT(vp != NULL);
55284d9c625SLionel Sambuc VOP_UNLOCK(vp);
55384d9c625SLionel Sambuc
55484d9c625SLionel Sambuc out: *ulr_ret = VTOI(dvp)->i_crap;
55584d9c625SLionel Sambuc *vp_ret = vp;
55684d9c625SLionel Sambuc return error;
55784d9c625SLionel Sambuc }
55884d9c625SLionel Sambuc
55984d9c625SLionel Sambuc /*
56084d9c625SLionel Sambuc * ulfs_rmdired_p: Check whether the directory vp has been rmdired.
56184d9c625SLionel Sambuc *
56284d9c625SLionel Sambuc * vp must be locked and referenced.
56384d9c625SLionel Sambuc */
56484d9c625SLionel Sambuc static bool
ulfs_rmdired_p(struct vnode * vp)56584d9c625SLionel Sambuc ulfs_rmdired_p(struct vnode *vp)
56684d9c625SLionel Sambuc {
56784d9c625SLionel Sambuc
56884d9c625SLionel Sambuc KASSERT(vp != NULL);
56984d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
57084d9c625SLionel Sambuc KASSERT(vp->v_type == VDIR);
57184d9c625SLionel Sambuc
57284d9c625SLionel Sambuc /* XXX Is this correct? */
57384d9c625SLionel Sambuc return (VTOI(vp)->i_size == 0);
57484d9c625SLionel Sambuc }
57584d9c625SLionel Sambuc
57684d9c625SLionel Sambuc /*
57784d9c625SLionel Sambuc * ulfs_read_dotdot: Store in *ino_ret the inode number of the parent
57884d9c625SLionel Sambuc * of the directory vp.
57984d9c625SLionel Sambuc */
58084d9c625SLionel Sambuc static int
ulfs_read_dotdot(struct vnode * vp,kauth_cred_t cred,ino_t * ino_ret)58184d9c625SLionel Sambuc ulfs_read_dotdot(struct vnode *vp, kauth_cred_t cred, ino_t *ino_ret)
58284d9c625SLionel Sambuc {
583*0a6a1f1dSLionel Sambuc struct lfs *fs;
584*0a6a1f1dSLionel Sambuc union lfs_dirtemplate dirbuf;
585*0a6a1f1dSLionel Sambuc LFS_DIRHEADER *dotdot;
586*0a6a1f1dSLionel Sambuc const char *name;
58784d9c625SLionel Sambuc int error;
58884d9c625SLionel Sambuc
58984d9c625SLionel Sambuc KASSERT(vp != NULL);
59084d9c625SLionel Sambuc KASSERT(ino_ret != NULL);
59184d9c625SLionel Sambuc KASSERT(vp->v_type == VDIR);
59284d9c625SLionel Sambuc
593*0a6a1f1dSLionel Sambuc KASSERT(VTOI(vp) != NULL);
594*0a6a1f1dSLionel Sambuc KASSERT(VTOI(vp)->i_lfs != NULL);
595*0a6a1f1dSLionel Sambuc fs = VTOI(vp)->i_lfs;
596*0a6a1f1dSLionel Sambuc
597*0a6a1f1dSLionel Sambuc error = ulfs_bufio(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
598*0a6a1f1dSLionel Sambuc IO_NODELOCKED, cred, NULL, NULL);
59984d9c625SLionel Sambuc if (error)
60084d9c625SLionel Sambuc return error;
60184d9c625SLionel Sambuc
602*0a6a1f1dSLionel Sambuc dotdot = lfs_dirtemplate_dotdot(fs, &dirbuf);
603*0a6a1f1dSLionel Sambuc name = lfs_dirtemplate_dotdotname(fs, &dirbuf);
604*0a6a1f1dSLionel Sambuc if (lfs_dir_getnamlen(fs, dotdot) != 2 ||
605*0a6a1f1dSLionel Sambuc name[0] != '.' ||
606*0a6a1f1dSLionel Sambuc name[1] != '.')
60784d9c625SLionel Sambuc /* XXX Panic? Print warning? */
60884d9c625SLionel Sambuc return ENOTDIR;
60984d9c625SLionel Sambuc
610*0a6a1f1dSLionel Sambuc *ino_ret = lfs_dir_getino(fs, dotdot);
61184d9c625SLionel Sambuc return 0;
61284d9c625SLionel Sambuc }
61384d9c625SLionel Sambuc
61484d9c625SLionel Sambuc /*
61584d9c625SLionel Sambuc * ulfs_gro_lock_directory: Lock the directory vp, but fail if it has
61684d9c625SLionel Sambuc * been rmdir'd.
61784d9c625SLionel Sambuc */
61884d9c625SLionel Sambuc static int
ulfs_gro_lock_directory(struct mount * mp,struct vnode * vp)61984d9c625SLionel Sambuc ulfs_gro_lock_directory(struct mount *mp, struct vnode *vp)
62084d9c625SLionel Sambuc {
62184d9c625SLionel Sambuc
62284d9c625SLionel Sambuc (void)mp;
62384d9c625SLionel Sambuc KASSERT(mp != NULL);
62484d9c625SLionel Sambuc KASSERT(vp != NULL);
62584d9c625SLionel Sambuc KASSERT(vp->v_mount == mp);
62684d9c625SLionel Sambuc
62784d9c625SLionel Sambuc vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
62884d9c625SLionel Sambuc
62984d9c625SLionel Sambuc if (ulfs_rmdired_p(vp)) {
63084d9c625SLionel Sambuc VOP_UNLOCK(vp);
63184d9c625SLionel Sambuc return ENOENT;
63284d9c625SLionel Sambuc }
63384d9c625SLionel Sambuc
63484d9c625SLionel Sambuc return 0;
63584d9c625SLionel Sambuc }
63684d9c625SLionel Sambuc
63784d9c625SLionel Sambuc /*
63884d9c625SLionel Sambuc * ulfs_gro_genealogy: Analyze the genealogy of the source and target
63984d9c625SLionel Sambuc * directories.
64084d9c625SLionel Sambuc */
64184d9c625SLionel Sambuc static int
ulfs_gro_genealogy(struct mount * mp,kauth_cred_t cred,struct vnode * fdvp,struct vnode * tdvp,struct vnode ** intermediate_node_ret)64284d9c625SLionel Sambuc ulfs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
64384d9c625SLionel Sambuc struct vnode *fdvp, struct vnode *tdvp,
64484d9c625SLionel Sambuc struct vnode **intermediate_node_ret)
64584d9c625SLionel Sambuc {
64684d9c625SLionel Sambuc struct vnode *vp, *dvp;
647*0a6a1f1dSLionel Sambuc ino_t dotdot_ino = -1; /* XXX gcc 4.8: maybe-uninitialized */
64884d9c625SLionel Sambuc int error;
64984d9c625SLionel Sambuc
65084d9c625SLionel Sambuc KASSERT(mp != NULL);
65184d9c625SLionel Sambuc KASSERT(fdvp != NULL);
65284d9c625SLionel Sambuc KASSERT(tdvp != NULL);
65384d9c625SLionel Sambuc KASSERT(fdvp != tdvp);
65484d9c625SLionel Sambuc KASSERT(intermediate_node_ret != NULL);
65584d9c625SLionel Sambuc KASSERT(fdvp->v_mount == mp);
65684d9c625SLionel Sambuc KASSERT(tdvp->v_mount == mp);
65784d9c625SLionel Sambuc KASSERT(fdvp->v_type == VDIR);
65884d9c625SLionel Sambuc KASSERT(tdvp->v_type == VDIR);
65984d9c625SLionel Sambuc
66084d9c625SLionel Sambuc /*
66184d9c625SLionel Sambuc * We need to provisionally lock tdvp to keep rmdir from
66284d9c625SLionel Sambuc * deleting it -- or any ancestor -- at an inopportune moment.
66384d9c625SLionel Sambuc */
66484d9c625SLionel Sambuc error = ulfs_gro_lock_directory(mp, tdvp);
66584d9c625SLionel Sambuc if (error)
66684d9c625SLionel Sambuc return error;
66784d9c625SLionel Sambuc
66884d9c625SLionel Sambuc vp = tdvp;
66984d9c625SLionel Sambuc vref(vp);
67084d9c625SLionel Sambuc
67184d9c625SLionel Sambuc for (;;) {
67284d9c625SLionel Sambuc KASSERT(vp != NULL);
67384d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
67484d9c625SLionel Sambuc KASSERT(vp->v_mount == mp);
67584d9c625SLionel Sambuc KASSERT(vp->v_type == VDIR);
67684d9c625SLionel Sambuc KASSERT(!ulfs_rmdired_p(vp));
67784d9c625SLionel Sambuc
67884d9c625SLionel Sambuc /* Did we hit the root without finding fdvp? */
67984d9c625SLionel Sambuc if (VTOI(vp)->i_number == ULFS_ROOTINO) {
68084d9c625SLionel Sambuc vput(vp);
68184d9c625SLionel Sambuc *intermediate_node_ret = NULL;
68284d9c625SLionel Sambuc return 0;
68384d9c625SLionel Sambuc }
68484d9c625SLionel Sambuc
68584d9c625SLionel Sambuc error = ulfs_read_dotdot(vp, cred, &dotdot_ino);
68684d9c625SLionel Sambuc if (error) {
68784d9c625SLionel Sambuc vput(vp);
68884d9c625SLionel Sambuc return error;
68984d9c625SLionel Sambuc }
69084d9c625SLionel Sambuc
69184d9c625SLionel Sambuc /* Did we find that fdvp is an ancestor of tdvp? */
69284d9c625SLionel Sambuc if (VTOI(fdvp)->i_number == dotdot_ino) {
69384d9c625SLionel Sambuc /* Unlock vp, but keep it referenced. */
69484d9c625SLionel Sambuc VOP_UNLOCK(vp);
69584d9c625SLionel Sambuc *intermediate_node_ret = vp;
69684d9c625SLionel Sambuc return 0;
69784d9c625SLionel Sambuc }
69884d9c625SLionel Sambuc
69984d9c625SLionel Sambuc /* Neither -- keep ascending the family tree. */
70084d9c625SLionel Sambuc
70184d9c625SLionel Sambuc /*
70284d9c625SLionel Sambuc * Unlock vp so that we can lock the parent, but keep
70384d9c625SLionel Sambuc * vp referenced until after we have found the parent,
70484d9c625SLionel Sambuc * so that dotdot_ino will not be recycled.
70584d9c625SLionel Sambuc *
70684d9c625SLionel Sambuc * XXX This guarantees that vp's inode number will not
70784d9c625SLionel Sambuc * be recycled, but why can't dotdot_ino be recycled?
70884d9c625SLionel Sambuc */
70984d9c625SLionel Sambuc VOP_UNLOCK(vp);
71084d9c625SLionel Sambuc error = VFS_VGET(mp, dotdot_ino, &dvp);
71184d9c625SLionel Sambuc vrele(vp);
71284d9c625SLionel Sambuc if (error)
71384d9c625SLionel Sambuc return error;
71484d9c625SLionel Sambuc
71584d9c625SLionel Sambuc KASSERT(dvp != NULL);
71684d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
71784d9c625SLionel Sambuc vp = dvp;
71884d9c625SLionel Sambuc
71984d9c625SLionel Sambuc if (vp->v_type != VDIR) {
72084d9c625SLionel Sambuc /*
72184d9c625SLionel Sambuc * XXX Panic? Print a warning? Can this
72284d9c625SLionel Sambuc * happen if we lose the race I suspect to
72384d9c625SLionel Sambuc * exist above, and the `..' inode number has
72484d9c625SLionel Sambuc * been recycled?
72584d9c625SLionel Sambuc */
72684d9c625SLionel Sambuc vput(vp);
72784d9c625SLionel Sambuc return ENOTDIR;
72884d9c625SLionel Sambuc }
72984d9c625SLionel Sambuc
73084d9c625SLionel Sambuc if (ulfs_rmdired_p(vp)) {
73184d9c625SLionel Sambuc vput(vp);
73284d9c625SLionel Sambuc return ENOENT;
73384d9c625SLionel Sambuc }
73484d9c625SLionel Sambuc }
73584d9c625SLionel Sambuc }
73684d9c625SLionel Sambuc
73784d9c625SLionel Sambuc /*
73884d9c625SLionel Sambuc * ulfs_gro_rename: Actually perform the rename operation.
73984d9c625SLionel Sambuc */
74084d9c625SLionel Sambuc static int
ulfs_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)74184d9c625SLionel Sambuc ulfs_gro_rename(struct mount *mp, kauth_cred_t cred,
74284d9c625SLionel Sambuc struct vnode *fdvp, struct componentname *fcnp,
74384d9c625SLionel Sambuc void *fde, struct vnode *fvp,
74484d9c625SLionel Sambuc struct vnode *tdvp, struct componentname *tcnp,
74584d9c625SLionel Sambuc void *tde, struct vnode *tvp)
74684d9c625SLionel Sambuc {
747*0a6a1f1dSLionel Sambuc struct lfs *fs;
74884d9c625SLionel Sambuc struct ulfs_lookup_results *fulr = fde;
74984d9c625SLionel Sambuc struct ulfs_lookup_results *tulr = tde;
75084d9c625SLionel Sambuc bool directory_p, reparent_p;
75184d9c625SLionel Sambuc int error;
75284d9c625SLionel Sambuc
75384d9c625SLionel Sambuc KASSERT(mp != NULL);
75484d9c625SLionel Sambuc KASSERT(fdvp != NULL);
75584d9c625SLionel Sambuc KASSERT(fcnp != NULL);
75684d9c625SLionel Sambuc KASSERT(fulr != NULL);
75784d9c625SLionel Sambuc KASSERT(fvp != NULL);
75884d9c625SLionel Sambuc KASSERT(tdvp != NULL);
75984d9c625SLionel Sambuc KASSERT(tcnp != NULL);
76084d9c625SLionel Sambuc KASSERT(tulr != NULL);
76184d9c625SLionel Sambuc KASSERT(fulr != tulr);
76284d9c625SLionel Sambuc KASSERT(fdvp != fvp);
76384d9c625SLionel Sambuc KASSERT(fdvp != tvp);
76484d9c625SLionel Sambuc KASSERT(tdvp != fvp);
76584d9c625SLionel Sambuc KASSERT(tdvp != tvp);
76684d9c625SLionel Sambuc KASSERT(fvp != tvp);
76784d9c625SLionel Sambuc KASSERT(fdvp->v_mount == mp);
76884d9c625SLionel Sambuc KASSERT(fvp->v_mount == mp);
76984d9c625SLionel Sambuc KASSERT(tdvp->v_mount == mp);
77084d9c625SLionel Sambuc KASSERT((tvp == NULL) || (tvp->v_mount == mp));
77184d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
77284d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
77384d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
77484d9c625SLionel Sambuc KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
77584d9c625SLionel Sambuc
776*0a6a1f1dSLionel Sambuc fs = VTOI(fdvp)->i_lfs;
777*0a6a1f1dSLionel Sambuc KASSERT(fs == VTOI(tdvp)->i_lfs);
778*0a6a1f1dSLionel Sambuc
77984d9c625SLionel Sambuc /*
78084d9c625SLionel Sambuc * We shall need to temporarily bump the link count, so make
78184d9c625SLionel Sambuc * sure there is room to do so.
78284d9c625SLionel Sambuc */
78384d9c625SLionel Sambuc if ((nlink_t)VTOI(fvp)->i_nlink >= LINK_MAX)
78484d9c625SLionel Sambuc return EMLINK;
78584d9c625SLionel Sambuc
78684d9c625SLionel Sambuc directory_p = (fvp->v_type == VDIR);
78784d9c625SLionel Sambuc KASSERT(directory_p == ((VTOI(fvp)->i_mode & LFS_IFMT) == LFS_IFDIR));
78884d9c625SLionel Sambuc KASSERT((tvp == NULL) || (directory_p == (tvp->v_type == VDIR)));
78984d9c625SLionel Sambuc KASSERT((tvp == NULL) || (directory_p ==
79084d9c625SLionel Sambuc ((VTOI(tvp)->i_mode & LFS_IFMT) == LFS_IFDIR)));
79184d9c625SLionel Sambuc
79284d9c625SLionel Sambuc reparent_p = (fdvp != tdvp);
79384d9c625SLionel Sambuc KASSERT(reparent_p == (VTOI(fdvp)->i_number != VTOI(tdvp)->i_number));
79484d9c625SLionel Sambuc
79584d9c625SLionel Sambuc /*
79684d9c625SLionel Sambuc * Commence hacking of the data on disk.
79784d9c625SLionel Sambuc */
79884d9c625SLionel Sambuc
79984d9c625SLionel Sambuc error = 0;
80084d9c625SLionel Sambuc
80184d9c625SLionel Sambuc /*
80284d9c625SLionel Sambuc * 1) Bump link count while we're moving stuff
80384d9c625SLionel Sambuc * around. If we crash somewhere before
80484d9c625SLionel Sambuc * completing our work, the link count
80584d9c625SLionel Sambuc * may be wrong, but correctable.
80684d9c625SLionel Sambuc */
80784d9c625SLionel Sambuc
80884d9c625SLionel Sambuc KASSERT((nlink_t)VTOI(fvp)->i_nlink < LINK_MAX);
80984d9c625SLionel Sambuc VTOI(fvp)->i_nlink++;
81084d9c625SLionel Sambuc DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
81184d9c625SLionel Sambuc VTOI(fvp)->i_flag |= IN_CHANGE;
81284d9c625SLionel Sambuc error = lfs_update(fvp, NULL, NULL, UPDATE_DIROP);
81384d9c625SLionel Sambuc if (error)
81484d9c625SLionel Sambuc goto whymustithurtsomuch;
81584d9c625SLionel Sambuc
81684d9c625SLionel Sambuc /*
81784d9c625SLionel Sambuc * 2) If target doesn't exist, link the target
81884d9c625SLionel Sambuc * to the source and unlink the source.
81984d9c625SLionel Sambuc * Otherwise, rewrite the target directory
82084d9c625SLionel Sambuc * entry to reference the source inode and
82184d9c625SLionel Sambuc * expunge the original entry's existence.
82284d9c625SLionel Sambuc */
82384d9c625SLionel Sambuc
82484d9c625SLionel Sambuc if (tvp == NULL) {
82584d9c625SLionel Sambuc /*
82684d9c625SLionel Sambuc * Account for ".." in new directory.
82784d9c625SLionel Sambuc * When source and destination have the same
82884d9c625SLionel Sambuc * parent we don't fool with the link count.
82984d9c625SLionel Sambuc */
83084d9c625SLionel Sambuc if (directory_p && reparent_p) {
83184d9c625SLionel Sambuc if ((nlink_t)VTOI(tdvp)->i_nlink >= LINK_MAX) {
83284d9c625SLionel Sambuc error = EMLINK;
83384d9c625SLionel Sambuc goto whymustithurtsomuch;
83484d9c625SLionel Sambuc }
83584d9c625SLionel Sambuc KASSERT((nlink_t)VTOI(tdvp)->i_nlink < LINK_MAX);
83684d9c625SLionel Sambuc VTOI(tdvp)->i_nlink++;
83784d9c625SLionel Sambuc DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
83884d9c625SLionel Sambuc VTOI(tdvp)->i_flag |= IN_CHANGE;
83984d9c625SLionel Sambuc error = lfs_update(tdvp, NULL, NULL, UPDATE_DIROP);
84084d9c625SLionel Sambuc if (error) {
84184d9c625SLionel Sambuc /*
84284d9c625SLionel Sambuc * Link count update didn't take --
84384d9c625SLionel Sambuc * back out the in-memory link count.
84484d9c625SLionel Sambuc */
84584d9c625SLionel Sambuc KASSERT(0 < VTOI(tdvp)->i_nlink);
84684d9c625SLionel Sambuc VTOI(tdvp)->i_nlink--;
84784d9c625SLionel Sambuc DIP_ASSIGN(VTOI(tdvp), nlink,
84884d9c625SLionel Sambuc VTOI(tdvp)->i_nlink);
84984d9c625SLionel Sambuc VTOI(tdvp)->i_flag |= IN_CHANGE;
85084d9c625SLionel Sambuc goto whymustithurtsomuch;
85184d9c625SLionel Sambuc }
85284d9c625SLionel Sambuc }
85384d9c625SLionel Sambuc
854*0a6a1f1dSLionel Sambuc error = ulfs_direnter(tdvp, tulr,
855*0a6a1f1dSLionel Sambuc NULL, tcnp, VTOI(fvp)->i_number, LFS_IFTODT(VTOI(fvp)->i_mode),
856*0a6a1f1dSLionel Sambuc NULL);
85784d9c625SLionel Sambuc if (error) {
85884d9c625SLionel Sambuc if (directory_p && reparent_p) {
85984d9c625SLionel Sambuc /*
86084d9c625SLionel Sambuc * Directory update didn't take, but
86184d9c625SLionel Sambuc * the link count update did -- back
86284d9c625SLionel Sambuc * out the in-memory link count and the
86384d9c625SLionel Sambuc * on-disk link count.
86484d9c625SLionel Sambuc */
86584d9c625SLionel Sambuc KASSERT(0 < VTOI(tdvp)->i_nlink);
86684d9c625SLionel Sambuc VTOI(tdvp)->i_nlink--;
86784d9c625SLionel Sambuc DIP_ASSIGN(VTOI(tdvp), nlink,
86884d9c625SLionel Sambuc VTOI(tdvp)->i_nlink);
86984d9c625SLionel Sambuc VTOI(tdvp)->i_flag |= IN_CHANGE;
87084d9c625SLionel Sambuc (void)lfs_update(tdvp, NULL, NULL,
87184d9c625SLionel Sambuc UPDATE_WAIT | UPDATE_DIROP);
87284d9c625SLionel Sambuc }
87384d9c625SLionel Sambuc goto whymustithurtsomuch;
87484d9c625SLionel Sambuc }
87584d9c625SLionel Sambuc } else {
87684d9c625SLionel Sambuc if (directory_p)
87784d9c625SLionel Sambuc /* XXX WTF? Why purge here? Why not purge others? */
87884d9c625SLionel Sambuc cache_purge(tdvp);
87984d9c625SLionel Sambuc
88084d9c625SLionel Sambuc /*
88184d9c625SLionel Sambuc * Make the target directory's entry for tcnp point at
88284d9c625SLionel Sambuc * the source node.
88384d9c625SLionel Sambuc *
88484d9c625SLionel Sambuc * XXX ulfs_dirrewrite decrements tvp's link count, but
88584d9c625SLionel Sambuc * doesn't touch the link count of the new inode. Go
88684d9c625SLionel Sambuc * figure.
88784d9c625SLionel Sambuc */
88884d9c625SLionel Sambuc error = ulfs_dirrewrite(VTOI(tdvp), tulr->ulr_offset,
88984d9c625SLionel Sambuc VTOI(tvp), VTOI(fvp)->i_number, LFS_IFTODT(VTOI(fvp)->i_mode),
89084d9c625SLionel Sambuc ((directory_p && reparent_p) ? reparent_p : directory_p),
89184d9c625SLionel Sambuc IN_CHANGE | IN_UPDATE);
89284d9c625SLionel Sambuc if (error)
89384d9c625SLionel Sambuc goto whymustithurtsomuch;
89484d9c625SLionel Sambuc
89584d9c625SLionel Sambuc /*
89684d9c625SLionel Sambuc * If the source and target are directories, and the
89784d9c625SLionel Sambuc * target is in the same directory as the source,
89884d9c625SLionel Sambuc * decrement the link count of the common parent
89984d9c625SLionel Sambuc * directory, since we are removing the target from
90084d9c625SLionel Sambuc * that directory.
90184d9c625SLionel Sambuc */
90284d9c625SLionel Sambuc if (directory_p && !reparent_p) {
90384d9c625SLionel Sambuc KASSERT(fdvp == tdvp);
90484d9c625SLionel Sambuc /* XXX check, don't kassert */
90584d9c625SLionel Sambuc KASSERT(0 < VTOI(tdvp)->i_nlink);
90684d9c625SLionel Sambuc VTOI(tdvp)->i_nlink--;
90784d9c625SLionel Sambuc DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
90884d9c625SLionel Sambuc VTOI(tdvp)->i_flag |= IN_CHANGE;
90984d9c625SLionel Sambuc }
91084d9c625SLionel Sambuc
91184d9c625SLionel Sambuc if (directory_p) {
91284d9c625SLionel Sambuc /*
91384d9c625SLionel Sambuc * XXX I don't understand the following comment
91484d9c625SLionel Sambuc * from ulfs_rename -- in particular, the part
91584d9c625SLionel Sambuc * about `there may be other hard links'.
91684d9c625SLionel Sambuc *
91784d9c625SLionel Sambuc * Truncate inode. The only stuff left in the directory
91884d9c625SLionel Sambuc * is "." and "..". The "." reference is inconsequential
91984d9c625SLionel Sambuc * since we are quashing it. We have removed the "."
92084d9c625SLionel Sambuc * reference and the reference in the parent directory,
92184d9c625SLionel Sambuc * but there may be other hard links.
92284d9c625SLionel Sambuc *
92384d9c625SLionel Sambuc * XXX The ulfs_dirempty call earlier does
92484d9c625SLionel Sambuc * not guarantee anything about nlink.
92584d9c625SLionel Sambuc */
92684d9c625SLionel Sambuc if (VTOI(tvp)->i_nlink != 1)
92784d9c625SLionel Sambuc ulfs_dirbad(VTOI(tvp), (doff_t)0,
92884d9c625SLionel Sambuc "hard-linked directory");
92984d9c625SLionel Sambuc VTOI(tvp)->i_nlink = 0;
93084d9c625SLionel Sambuc DIP_ASSIGN(VTOI(tvp), nlink, 0);
93184d9c625SLionel Sambuc error = lfs_truncate(tvp, (off_t)0, IO_SYNC, cred);
93284d9c625SLionel Sambuc if (error)
93384d9c625SLionel Sambuc goto whymustithurtsomuch;
93484d9c625SLionel Sambuc }
93584d9c625SLionel Sambuc }
93684d9c625SLionel Sambuc
93784d9c625SLionel Sambuc /*
93884d9c625SLionel Sambuc * If the source is a directory with a new parent, the link
93984d9c625SLionel Sambuc * count of the old parent directory must be decremented and
94084d9c625SLionel Sambuc * ".." set to point to the new parent.
94184d9c625SLionel Sambuc *
94284d9c625SLionel Sambuc * XXX ulfs_dirrewrite updates the link count of fdvp, but not
94384d9c625SLionel Sambuc * the link count of fvp or the link count of tdvp. Go figure.
94484d9c625SLionel Sambuc */
94584d9c625SLionel Sambuc if (directory_p && reparent_p) {
946*0a6a1f1dSLionel Sambuc off_t position;
947*0a6a1f1dSLionel Sambuc
948*0a6a1f1dSLionel Sambuc /*
949*0a6a1f1dSLionel Sambuc * The .. entry goes immediately after the . entry, so
950*0a6a1f1dSLionel Sambuc * the position is the record length of the . entry,
951*0a6a1f1dSLionel Sambuc * namely LFS_DIRECTSIZ(1).
952*0a6a1f1dSLionel Sambuc */
953*0a6a1f1dSLionel Sambuc position = LFS_DIRECTSIZ(fs, 1);
954*0a6a1f1dSLionel Sambuc error = ulfs_dirrewrite(VTOI(fvp), position,
95584d9c625SLionel Sambuc VTOI(fdvp), VTOI(tdvp)->i_number, LFS_DT_DIR, 0, IN_CHANGE);
95684d9c625SLionel Sambuc #if 0 /* XXX This branch was not in ulfs_rename! */
95784d9c625SLionel Sambuc if (error)
95884d9c625SLionel Sambuc goto whymustithurtsomuch;
95984d9c625SLionel Sambuc #endif
96084d9c625SLionel Sambuc
96184d9c625SLionel Sambuc /* XXX WTF? Why purge here? Why not purge others? */
96284d9c625SLionel Sambuc cache_purge(fdvp);
96384d9c625SLionel Sambuc }
96484d9c625SLionel Sambuc
96584d9c625SLionel Sambuc /*
96684d9c625SLionel Sambuc * 3) Unlink the source.
96784d9c625SLionel Sambuc */
96884d9c625SLionel Sambuc
96984d9c625SLionel Sambuc /*
97084d9c625SLionel Sambuc * ulfs_direnter may compact the directory in the process of
97184d9c625SLionel Sambuc * inserting a new entry. That may invalidate fulr, which we
97284d9c625SLionel Sambuc * need in order to remove the old entry. In that case, we
97384d9c625SLionel Sambuc * need to recalculate what fulr should be.
97484d9c625SLionel Sambuc */
97584d9c625SLionel Sambuc if (!reparent_p && (tvp == NULL) &&
97684d9c625SLionel Sambuc ulfs_rename_ulr_overlap_p(fulr, tulr)) {
97784d9c625SLionel Sambuc error = ulfs_rename_recalculate_fulr(fdvp, fulr, tulr, fcnp);
97884d9c625SLionel Sambuc #if 0 /* XXX */
97984d9c625SLionel Sambuc if (error) /* XXX Try to back out changes? */
98084d9c625SLionel Sambuc goto whymustithurtsomuch;
98184d9c625SLionel Sambuc #endif
98284d9c625SLionel Sambuc }
98384d9c625SLionel Sambuc
98484d9c625SLionel Sambuc /*
98584d9c625SLionel Sambuc * XXX 0 means !isrmdir. But can't this be an rmdir?
98684d9c625SLionel Sambuc * XXX Well, turns out that argument to ulfs_dirremove is ignored...
98784d9c625SLionel Sambuc * XXX And it turns out ulfs_dirremove updates the link count of fvp.
98884d9c625SLionel Sambuc * XXX But it doesn't update the link count of fdvp. Go figure.
98984d9c625SLionel Sambuc * XXX fdvp's link count is updated in ulfs_dirrewrite instead.
99084d9c625SLionel Sambuc * XXX Actually, sometimes it doesn't update fvp's link count.
99184d9c625SLionel Sambuc * XXX I hate the world.
99284d9c625SLionel Sambuc */
99384d9c625SLionel Sambuc error = ulfs_dirremove(fdvp, fulr, VTOI(fvp), fcnp->cn_flags, 0);
99484d9c625SLionel Sambuc if (error)
99584d9c625SLionel Sambuc #if 0 /* XXX */
99684d9c625SLionel Sambuc goto whymustithurtsomuch;
99784d9c625SLionel Sambuc #endif
99884d9c625SLionel Sambuc goto arghmybrainhurts;
99984d9c625SLionel Sambuc
100084d9c625SLionel Sambuc /*
100184d9c625SLionel Sambuc * XXX Perhaps this should go at the top, in case the file
100284d9c625SLionel Sambuc * system is modified but incompletely so because of an
100384d9c625SLionel Sambuc * intermediate error.
100484d9c625SLionel Sambuc */
100584d9c625SLionel Sambuc genfs_rename_knote(fdvp, fvp, tdvp, tvp,
100684d9c625SLionel Sambuc ((tvp != NULL) && (VTOI(tvp)->i_nlink == 0)));
100784d9c625SLionel Sambuc #if 0 /* XXX */
100884d9c625SLionel Sambuc genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
100984d9c625SLionel Sambuc #endif
101084d9c625SLionel Sambuc goto arghmybrainhurts;
101184d9c625SLionel Sambuc
101284d9c625SLionel Sambuc whymustithurtsomuch:
101384d9c625SLionel Sambuc KASSERT(0 < VTOI(fvp)->i_nlink);
101484d9c625SLionel Sambuc VTOI(fvp)->i_nlink--;
101584d9c625SLionel Sambuc DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
101684d9c625SLionel Sambuc VTOI(fvp)->i_flag |= IN_CHANGE;
101784d9c625SLionel Sambuc
101884d9c625SLionel Sambuc arghmybrainhurts:
101984d9c625SLionel Sambuc /*ihateyou:*/
102084d9c625SLionel Sambuc return error;
102184d9c625SLionel Sambuc }
102284d9c625SLionel Sambuc
102384d9c625SLionel Sambuc /*
102484d9c625SLionel Sambuc * lfs_gro_rename: Actually perform the rename operation. Do a little
102584d9c625SLionel Sambuc * LFS bookkeeping and then defer to ulfs_gro_rename.
102684d9c625SLionel Sambuc */
102784d9c625SLionel Sambuc static int
lfs_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)102884d9c625SLionel Sambuc lfs_gro_rename(struct mount *mp, kauth_cred_t cred,
102984d9c625SLionel Sambuc struct vnode *fdvp, struct componentname *fcnp,
103084d9c625SLionel Sambuc void *fde, struct vnode *fvp,
103184d9c625SLionel Sambuc struct vnode *tdvp, struct componentname *tcnp,
103284d9c625SLionel Sambuc void *tde, struct vnode *tvp)
103384d9c625SLionel Sambuc {
103484d9c625SLionel Sambuc int error;
103584d9c625SLionel Sambuc
103684d9c625SLionel Sambuc KASSERT(mp != NULL);
103784d9c625SLionel Sambuc KASSERT(fdvp != NULL);
103884d9c625SLionel Sambuc KASSERT(fcnp != NULL);
103984d9c625SLionel Sambuc KASSERT(fde != NULL);
104084d9c625SLionel Sambuc KASSERT(fvp != NULL);
104184d9c625SLionel Sambuc KASSERT(tdvp != NULL);
104284d9c625SLionel Sambuc KASSERT(tcnp != NULL);
104384d9c625SLionel Sambuc KASSERT(tde != NULL);
104484d9c625SLionel Sambuc KASSERT(fdvp != fvp);
104584d9c625SLionel Sambuc KASSERT(fdvp != tvp);
104684d9c625SLionel Sambuc KASSERT(tdvp != fvp);
104784d9c625SLionel Sambuc KASSERT(tdvp != tvp);
104884d9c625SLionel Sambuc KASSERT(fvp != tvp);
104984d9c625SLionel Sambuc KASSERT(fdvp->v_mount == mp);
105084d9c625SLionel Sambuc KASSERT(fvp->v_mount == mp);
105184d9c625SLionel Sambuc KASSERT(tdvp->v_mount == mp);
105284d9c625SLionel Sambuc KASSERT((tvp == NULL) || (tvp->v_mount == mp));
105384d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
105484d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
105584d9c625SLionel Sambuc KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
105684d9c625SLionel Sambuc KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
105784d9c625SLionel Sambuc
1058*0a6a1f1dSLionel Sambuc error = lfs_set_dirop(tdvp, tvp);
105984d9c625SLionel Sambuc if (error != 0)
106084d9c625SLionel Sambuc return error;
106184d9c625SLionel Sambuc
106284d9c625SLionel Sambuc MARK_VNODE(fdvp);
106384d9c625SLionel Sambuc MARK_VNODE(fvp);
106484d9c625SLionel Sambuc
106584d9c625SLionel Sambuc error = ulfs_gro_rename(mp, cred,
106684d9c625SLionel Sambuc fdvp, fcnp, fde, fvp,
106784d9c625SLionel Sambuc tdvp, tcnp, tde, tvp);
106884d9c625SLionel Sambuc
106984d9c625SLionel Sambuc UNMARK_VNODE(fdvp);
107084d9c625SLionel Sambuc UNMARK_VNODE(fvp);
1071*0a6a1f1dSLionel Sambuc UNMARK_VNODE(tdvp);
1072*0a6a1f1dSLionel Sambuc if (tvp) {
1073*0a6a1f1dSLionel Sambuc UNMARK_VNODE(tvp);
1074*0a6a1f1dSLionel Sambuc }
1075*0a6a1f1dSLionel Sambuc lfs_unset_dirop(VFSTOULFS(mp)->um_lfs, tdvp, "rename");
1076*0a6a1f1dSLionel Sambuc vrele(tdvp);
1077*0a6a1f1dSLionel Sambuc if (tvp) {
1078*0a6a1f1dSLionel Sambuc vrele(tvp);
1079*0a6a1f1dSLionel Sambuc }
108084d9c625SLionel Sambuc
108184d9c625SLionel Sambuc return error;
108284d9c625SLionel Sambuc }
108384d9c625SLionel Sambuc
108484d9c625SLionel Sambuc static const struct genfs_rename_ops lfs_genfs_rename_ops = {
108584d9c625SLionel Sambuc .gro_directory_empty_p = ulfs_gro_directory_empty_p,
108684d9c625SLionel Sambuc .gro_rename_check_possible = ulfs_gro_rename_check_possible,
108784d9c625SLionel Sambuc .gro_rename_check_permitted = ulfs_gro_rename_check_permitted,
108884d9c625SLionel Sambuc .gro_remove_check_possible = ulfs_gro_remove_check_possible,
108984d9c625SLionel Sambuc .gro_remove_check_permitted = ulfs_gro_remove_check_permitted,
109084d9c625SLionel Sambuc .gro_rename = lfs_gro_rename,
109184d9c625SLionel Sambuc .gro_remove = ulfs_gro_remove,
109284d9c625SLionel Sambuc .gro_lookup = ulfs_gro_lookup,
109384d9c625SLionel Sambuc .gro_genealogy = ulfs_gro_genealogy,
109484d9c625SLionel Sambuc .gro_lock_directory = ulfs_gro_lock_directory,
109584d9c625SLionel Sambuc };
109684d9c625SLionel Sambuc
109784d9c625SLionel Sambuc /*
109884d9c625SLionel Sambuc * lfs_sane_rename: The hairiest vop, with the saner API.
109984d9c625SLionel Sambuc *
110084d9c625SLionel Sambuc * Arguments:
110184d9c625SLionel Sambuc *
110284d9c625SLionel Sambuc * . fdvp (from directory vnode),
110384d9c625SLionel Sambuc * . fcnp (from component name),
110484d9c625SLionel Sambuc * . tdvp (to directory vnode),
110584d9c625SLionel Sambuc * . tcnp (to component name),
110684d9c625SLionel Sambuc * . cred (credentials structure), and
110784d9c625SLionel Sambuc * . posixly_correct (flag for behaviour if target & source link same file).
110884d9c625SLionel Sambuc *
110984d9c625SLionel Sambuc * fdvp and tdvp may be the same, and must be referenced and unlocked.
111084d9c625SLionel Sambuc */
111184d9c625SLionel Sambuc static int
lfs_sane_rename(struct vnode * fdvp,struct componentname * fcnp,struct vnode * tdvp,struct componentname * tcnp,kauth_cred_t cred,bool posixly_correct)111284d9c625SLionel Sambuc lfs_sane_rename(
111384d9c625SLionel Sambuc struct vnode *fdvp, struct componentname *fcnp,
111484d9c625SLionel Sambuc struct vnode *tdvp, struct componentname *tcnp,
111584d9c625SLionel Sambuc kauth_cred_t cred, bool posixly_correct)
111684d9c625SLionel Sambuc {
111784d9c625SLionel Sambuc struct ulfs_lookup_results fulr, tulr;
111884d9c625SLionel Sambuc
111984d9c625SLionel Sambuc /*
112084d9c625SLionel Sambuc * XXX Provisional kludge -- ulfs_lookup does not reject rename
112184d9c625SLionel Sambuc * of . or .. (from or to), so we hack it here. This is not
112284d9c625SLionel Sambuc * the right place: it should be caller's responsibility to
112384d9c625SLionel Sambuc * reject this case.
112484d9c625SLionel Sambuc */
112584d9c625SLionel Sambuc KASSERT(fcnp != NULL);
112684d9c625SLionel Sambuc KASSERT(tcnp != NULL);
112784d9c625SLionel Sambuc KASSERT(fcnp != tcnp);
112884d9c625SLionel Sambuc KASSERT(fcnp->cn_nameptr != NULL);
112984d9c625SLionel Sambuc KASSERT(tcnp->cn_nameptr != NULL);
113084d9c625SLionel Sambuc
113184d9c625SLionel Sambuc if ((fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT)
113284d9c625SLionel Sambuc return EINVAL; /* XXX EISDIR? */
113384d9c625SLionel Sambuc if ((fcnp->cn_namelen == 1) && (fcnp->cn_nameptr[0] == '.'))
113484d9c625SLionel Sambuc return EINVAL;
113584d9c625SLionel Sambuc if ((tcnp->cn_namelen == 1) && (tcnp->cn_nameptr[0] == '.'))
113684d9c625SLionel Sambuc return EINVAL;
113784d9c625SLionel Sambuc
113884d9c625SLionel Sambuc return genfs_sane_rename(&lfs_genfs_rename_ops,
113984d9c625SLionel Sambuc fdvp, fcnp, &fulr, tdvp, tcnp, &tulr,
114084d9c625SLionel Sambuc cred, posixly_correct);
114184d9c625SLionel Sambuc }
114284d9c625SLionel Sambuc
114384d9c625SLionel Sambuc /*
114484d9c625SLionel Sambuc * lfs_rename: The hairiest vop, with the insanest API. Defer to
114584d9c625SLionel Sambuc * genfs_insane_rename immediately.
114684d9c625SLionel Sambuc */
114784d9c625SLionel Sambuc int
lfs_rename(void * v)114884d9c625SLionel Sambuc lfs_rename(void *v)
114984d9c625SLionel Sambuc {
115084d9c625SLionel Sambuc
115184d9c625SLionel Sambuc return genfs_insane_rename(v, &lfs_sane_rename);
115284d9c625SLionel Sambuc }
1153