Home | History | Annotate | Line # | Download | only in lfs
lfs_rename.c revision 1.4.2.2
      1  1.4.2.2  rmind /*	$NetBSD: lfs_rename.c,v 1.4.2.2 2013/08/28 23:59:38 rmind Exp $	*/
      2  1.4.2.2  rmind /*  from NetBSD: ufs_rename.c,v 1.6 2013/01/22 09:39:18 dholland Exp  */
      3  1.4.2.2  rmind 
      4  1.4.2.2  rmind /*-
      5  1.4.2.2  rmind  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      6  1.4.2.2  rmind  * All rights reserved.
      7  1.4.2.2  rmind  *
      8  1.4.2.2  rmind  * This code is derived from software contributed to The NetBSD Foundation
      9  1.4.2.2  rmind  * by Taylor R Campbell.
     10  1.4.2.2  rmind  *
     11  1.4.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     12  1.4.2.2  rmind  * modification, are permitted provided that the following conditions
     13  1.4.2.2  rmind  * are met:
     14  1.4.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     15  1.4.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     16  1.4.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.4.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     18  1.4.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     19  1.4.2.2  rmind  *
     20  1.4.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.4.2.2  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.4.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.4.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.4.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.4.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.4.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.4.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.4.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.4.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.4.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     31  1.4.2.2  rmind  */
     32  1.4.2.2  rmind /*-
     33  1.4.2.2  rmind  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
     34  1.4.2.2  rmind  * All rights reserved.
     35  1.4.2.2  rmind  *
     36  1.4.2.2  rmind  * This code is derived from software contributed to The NetBSD Foundation
     37  1.4.2.2  rmind  * by Konrad E. Schroder <perseant (at) hhhh.org>.
     38  1.4.2.2  rmind  *
     39  1.4.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     40  1.4.2.2  rmind  * modification, are permitted provided that the following conditions
     41  1.4.2.2  rmind  * are met:
     42  1.4.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     43  1.4.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     44  1.4.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     45  1.4.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     46  1.4.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     47  1.4.2.2  rmind  *
     48  1.4.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     49  1.4.2.2  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     50  1.4.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     51  1.4.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     52  1.4.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     53  1.4.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     54  1.4.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     55  1.4.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     56  1.4.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     57  1.4.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     58  1.4.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     59  1.4.2.2  rmind  */
     60  1.4.2.2  rmind /*
     61  1.4.2.2  rmind  * Copyright (c) 1986, 1989, 1991, 1993, 1995
     62  1.4.2.2  rmind  *	The Regents of the University of California.  All rights reserved.
     63  1.4.2.2  rmind  *
     64  1.4.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     65  1.4.2.2  rmind  * modification, are permitted provided that the following conditions
     66  1.4.2.2  rmind  * are met:
     67  1.4.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     68  1.4.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     69  1.4.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     70  1.4.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     71  1.4.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     72  1.4.2.2  rmind  * 3. Neither the name of the University nor the names of its contributors
     73  1.4.2.2  rmind  *    may be used to endorse or promote products derived from this software
     74  1.4.2.2  rmind  *    without specific prior written permission.
     75  1.4.2.2  rmind  *
     76  1.4.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     77  1.4.2.2  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     78  1.4.2.2  rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     79  1.4.2.2  rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     80  1.4.2.2  rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     81  1.4.2.2  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     82  1.4.2.2  rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     83  1.4.2.2  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     84  1.4.2.2  rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     85  1.4.2.2  rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     86  1.4.2.2  rmind  * SUCH DAMAGE.
     87  1.4.2.2  rmind  *
     88  1.4.2.2  rmind  *	@(#)lfs_vnops.c	8.13 (Berkeley) 6/10/95
     89  1.4.2.2  rmind  */
     90  1.4.2.2  rmind 
     91  1.4.2.2  rmind #include <sys/cdefs.h>
     92  1.4.2.2  rmind __KERNEL_RCSID(0, "$NetBSD: lfs_rename.c,v 1.4.2.2 2013/08/28 23:59:38 rmind Exp $");
     93  1.4.2.2  rmind 
     94  1.4.2.2  rmind #include <sys/param.h>
     95  1.4.2.2  rmind #include <sys/systm.h>
     96  1.4.2.2  rmind #include <sys/errno.h>
     97  1.4.2.2  rmind #include <sys/namei.h>
     98  1.4.2.2  rmind #include <sys/resourcevar.h>
     99  1.4.2.2  rmind #include <sys/kernel.h>
    100  1.4.2.2  rmind #include <sys/file.h>
    101  1.4.2.2  rmind #include <sys/stat.h>
    102  1.4.2.2  rmind #include <sys/buf.h>
    103  1.4.2.2  rmind #include <sys/proc.h>
    104  1.4.2.2  rmind #include <sys/mount.h>
    105  1.4.2.2  rmind #include <sys/vnode.h>
    106  1.4.2.2  rmind #include <sys/vnode_if.h>
    107  1.4.2.2  rmind #include <sys/pool.h>
    108  1.4.2.2  rmind #include <sys/signalvar.h>
    109  1.4.2.2  rmind #include <sys/kauth.h>
    110  1.4.2.2  rmind #include <sys/syslog.h>
    111  1.4.2.2  rmind #include <sys/fstrans.h>
    112  1.4.2.2  rmind 
    113  1.4.2.2  rmind #include <uvm/uvm.h>
    114  1.4.2.2  rmind #include <uvm/uvm_pmap.h>
    115  1.4.2.2  rmind #include <uvm/uvm_stat.h>
    116  1.4.2.2  rmind #include <uvm/uvm_pager.h>
    117  1.4.2.2  rmind 
    118  1.4.2.2  rmind #include <miscfs/fifofs/fifo.h>
    119  1.4.2.2  rmind #include <miscfs/genfs/genfs.h>
    120  1.4.2.2  rmind #include <miscfs/specfs/specdev.h>
    121  1.4.2.2  rmind 
    122  1.4.2.2  rmind #include <ufs/lfs/ulfs_inode.h>
    123  1.4.2.2  rmind #include <ufs/lfs/ulfsmount.h>
    124  1.4.2.2  rmind #include <ufs/lfs/ulfs_bswap.h>
    125  1.4.2.2  rmind #include <ufs/lfs/ulfs_extern.h>
    126  1.4.2.2  rmind 
    127  1.4.2.2  rmind #include <ufs/lfs/lfs.h>
    128  1.4.2.2  rmind #include <ufs/lfs/lfs_extern.h>
    129  1.4.2.2  rmind 
    130  1.4.2.2  rmind /*
    131  1.4.2.2  rmind  * A virgin directory (no blushing please).
    132  1.4.2.2  rmind  *
    133  1.4.2.2  rmind  * XXX Copypasta from ulfs_vnops.c.  Kill!
    134  1.4.2.2  rmind  */
    135  1.4.2.2  rmind static const struct lfs_dirtemplate mastertemplate = {
    136  1.4.2.2  rmind 	0,	12,			LFS_DT_DIR,	1,	".",
    137  1.4.2.2  rmind 	0,	LFS_DIRBLKSIZ - 12,	LFS_DT_DIR,	2,	".."
    138  1.4.2.2  rmind };
    139  1.4.2.2  rmind 
    140  1.4.2.2  rmind /*
    141  1.4.2.2  rmind  * ulfs_gro_directory_empty_p: Return true if the directory vp is
    142  1.4.2.2  rmind  * empty.  dvp is its parent.
    143  1.4.2.2  rmind  *
    144  1.4.2.2  rmind  * vp and dvp must be locked and referenced.
    145  1.4.2.2  rmind  */
    146  1.4.2.2  rmind static bool
    147  1.4.2.2  rmind ulfs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
    148  1.4.2.2  rmind     struct vnode *vp, struct vnode *dvp)
    149  1.4.2.2  rmind {
    150  1.4.2.2  rmind 
    151  1.4.2.2  rmind 	(void)mp;
    152  1.4.2.2  rmind 	KASSERT(mp != NULL);
    153  1.4.2.2  rmind 	KASSERT(vp != NULL);
    154  1.4.2.2  rmind 	KASSERT(dvp != NULL);
    155  1.4.2.2  rmind 	KASSERT(vp != dvp);
    156  1.4.2.2  rmind 	KASSERT(vp->v_mount == mp);
    157  1.4.2.2  rmind 	KASSERT(dvp->v_mount == mp);
    158  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    159  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    160  1.4.2.2  rmind 
    161  1.4.2.2  rmind 	return ulfs_dirempty(VTOI(vp), VTOI(dvp)->i_number, cred);
    162  1.4.2.2  rmind }
    163  1.4.2.2  rmind 
    164  1.4.2.2  rmind /*
    165  1.4.2.2  rmind  * ulfs_gro_rename_check_possible: Check whether a rename is possible
    166  1.4.2.2  rmind  * independent of credentials.
    167  1.4.2.2  rmind  */
    168  1.4.2.2  rmind static int
    169  1.4.2.2  rmind ulfs_gro_rename_check_possible(struct mount *mp,
    170  1.4.2.2  rmind     struct vnode *fdvp, struct vnode *fvp,
    171  1.4.2.2  rmind     struct vnode *tdvp, struct vnode *tvp)
    172  1.4.2.2  rmind {
    173  1.4.2.2  rmind 
    174  1.4.2.2  rmind 	(void)mp;
    175  1.4.2.2  rmind 	KASSERT(mp != NULL);
    176  1.4.2.2  rmind 	KASSERT(fdvp != NULL);
    177  1.4.2.2  rmind 	KASSERT(fvp != NULL);
    178  1.4.2.2  rmind 	KASSERT(tdvp != NULL);
    179  1.4.2.2  rmind 	KASSERT(fdvp != fvp);
    180  1.4.2.2  rmind 	KASSERT(fdvp != tvp);
    181  1.4.2.2  rmind 	KASSERT(tdvp != fvp);
    182  1.4.2.2  rmind 	KASSERT(tdvp != tvp);
    183  1.4.2.2  rmind 	KASSERT(fvp != tvp);
    184  1.4.2.2  rmind 	KASSERT(fdvp->v_type == VDIR);
    185  1.4.2.2  rmind 	KASSERT(tdvp->v_type == VDIR);
    186  1.4.2.2  rmind 	KASSERT(fdvp->v_mount == mp);
    187  1.4.2.2  rmind 	KASSERT(fvp->v_mount == mp);
    188  1.4.2.2  rmind 	KASSERT(tdvp->v_mount == mp);
    189  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
    190  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
    191  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
    192  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    193  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    194  1.4.2.2  rmind 
    195  1.4.2.2  rmind 	return genfs_ufslike_rename_check_possible(
    196  1.4.2.2  rmind 	    VTOI(fdvp)->i_flags, VTOI(fvp)->i_flags,
    197  1.4.2.2  rmind 	    VTOI(tdvp)->i_flags, (tvp? VTOI(tvp)->i_flags : 0),
    198  1.4.2.2  rmind 	    (tvp != NULL),
    199  1.4.2.2  rmind 	    IMMUTABLE, APPEND);
    200  1.4.2.2  rmind }
    201  1.4.2.2  rmind 
    202  1.4.2.2  rmind /*
    203  1.4.2.2  rmind  * ulfs_gro_rename_check_permitted: Check whether a rename is permitted
    204  1.4.2.2  rmind  * given our credentials.
    205  1.4.2.2  rmind  */
    206  1.4.2.2  rmind static int
    207  1.4.2.2  rmind ulfs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
    208  1.4.2.2  rmind     struct vnode *fdvp, struct vnode *fvp,
    209  1.4.2.2  rmind     struct vnode *tdvp, struct vnode *tvp)
    210  1.4.2.2  rmind {
    211  1.4.2.2  rmind 
    212  1.4.2.2  rmind 	(void)mp;
    213  1.4.2.2  rmind 	KASSERT(mp != NULL);
    214  1.4.2.2  rmind 	KASSERT(fdvp != NULL);
    215  1.4.2.2  rmind 	KASSERT(fvp != NULL);
    216  1.4.2.2  rmind 	KASSERT(tdvp != NULL);
    217  1.4.2.2  rmind 	KASSERT(fdvp != fvp);
    218  1.4.2.2  rmind 	KASSERT(fdvp != tvp);
    219  1.4.2.2  rmind 	KASSERT(tdvp != fvp);
    220  1.4.2.2  rmind 	KASSERT(tdvp != tvp);
    221  1.4.2.2  rmind 	KASSERT(fvp != tvp);
    222  1.4.2.2  rmind 	KASSERT(fdvp->v_type == VDIR);
    223  1.4.2.2  rmind 	KASSERT(tdvp->v_type == VDIR);
    224  1.4.2.2  rmind 	KASSERT(fdvp->v_mount == mp);
    225  1.4.2.2  rmind 	KASSERT(fvp->v_mount == mp);
    226  1.4.2.2  rmind 	KASSERT(tdvp->v_mount == mp);
    227  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
    228  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
    229  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
    230  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    231  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    232  1.4.2.2  rmind 
    233  1.4.2.2  rmind 	return genfs_ufslike_rename_check_permitted(cred,
    234  1.4.2.2  rmind 	    fdvp, VTOI(fdvp)->i_mode, VTOI(fdvp)->i_uid,
    235  1.4.2.2  rmind 	    fvp, VTOI(fvp)->i_uid,
    236  1.4.2.2  rmind 	    tdvp, VTOI(tdvp)->i_mode, VTOI(tdvp)->i_uid,
    237  1.4.2.2  rmind 	    tvp, (tvp? VTOI(tvp)->i_uid : 0));
    238  1.4.2.2  rmind }
    239  1.4.2.2  rmind 
    240  1.4.2.2  rmind /*
    241  1.4.2.2  rmind  * ulfs_gro_remove_check_possible: Check whether a remove is possible
    242  1.4.2.2  rmind  * independent of credentials.
    243  1.4.2.2  rmind  */
    244  1.4.2.2  rmind static int
    245  1.4.2.2  rmind ulfs_gro_remove_check_possible(struct mount *mp,
    246  1.4.2.2  rmind     struct vnode *dvp, struct vnode *vp)
    247  1.4.2.2  rmind {
    248  1.4.2.2  rmind 
    249  1.4.2.2  rmind 	(void)mp;
    250  1.4.2.2  rmind 	KASSERT(mp != NULL);
    251  1.4.2.2  rmind 	KASSERT(dvp != NULL);
    252  1.4.2.2  rmind 	KASSERT(vp != NULL);
    253  1.4.2.2  rmind 	KASSERT(dvp != vp);
    254  1.4.2.2  rmind 	KASSERT(dvp->v_type == VDIR);
    255  1.4.2.2  rmind 	KASSERT(vp->v_type != VDIR);
    256  1.4.2.2  rmind 	KASSERT(dvp->v_mount == mp);
    257  1.4.2.2  rmind 	KASSERT(vp->v_mount == mp);
    258  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    259  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    260  1.4.2.2  rmind 
    261  1.4.2.2  rmind 	return genfs_ufslike_remove_check_possible(
    262  1.4.2.2  rmind 	    VTOI(dvp)->i_flags, VTOI(vp)->i_flags,
    263  1.4.2.2  rmind 	    IMMUTABLE, APPEND);
    264  1.4.2.2  rmind }
    265  1.4.2.2  rmind 
    266  1.4.2.2  rmind /*
    267  1.4.2.2  rmind  * ulfs_gro_remove_check_permitted: Check whether a remove is permitted
    268  1.4.2.2  rmind  * given our credentials.
    269  1.4.2.2  rmind  */
    270  1.4.2.2  rmind static int
    271  1.4.2.2  rmind ulfs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
    272  1.4.2.2  rmind     struct vnode *dvp, struct vnode *vp)
    273  1.4.2.2  rmind {
    274  1.4.2.2  rmind 
    275  1.4.2.2  rmind 	(void)mp;
    276  1.4.2.2  rmind 	KASSERT(mp != NULL);
    277  1.4.2.2  rmind 	KASSERT(dvp != NULL);
    278  1.4.2.2  rmind 	KASSERT(vp != NULL);
    279  1.4.2.2  rmind 	KASSERT(dvp != vp);
    280  1.4.2.2  rmind 	KASSERT(dvp->v_type == VDIR);
    281  1.4.2.2  rmind 	KASSERT(vp->v_type != VDIR);
    282  1.4.2.2  rmind 	KASSERT(dvp->v_mount == mp);
    283  1.4.2.2  rmind 	KASSERT(vp->v_mount == mp);
    284  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    285  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    286  1.4.2.2  rmind 
    287  1.4.2.2  rmind 	return genfs_ufslike_remove_check_permitted(cred,
    288  1.4.2.2  rmind 	    dvp, VTOI(dvp)->i_mode, VTOI(dvp)->i_uid, vp, VTOI(vp)->i_uid);
    289  1.4.2.2  rmind }
    290  1.4.2.2  rmind 
    291  1.4.2.2  rmind /*
    292  1.4.2.2  rmind  * ulfs_rename_ulr_overlap_p: True iff tulr overlaps with fulr so that
    293  1.4.2.2  rmind  * entering a directory entry at tulr may move fulr.
    294  1.4.2.2  rmind  */
    295  1.4.2.2  rmind static bool
    296  1.4.2.2  rmind ulfs_rename_ulr_overlap_p(const struct ulfs_lookup_results *fulr,
    297  1.4.2.2  rmind     const struct ulfs_lookup_results *tulr)
    298  1.4.2.2  rmind {
    299  1.4.2.2  rmind 	doff_t from_prev_start, from_prev_end, to_start, to_end;
    300  1.4.2.2  rmind 
    301  1.4.2.2  rmind 	KASSERT(fulr != NULL);
    302  1.4.2.2  rmind 	KASSERT(tulr != NULL);
    303  1.4.2.2  rmind 	KASSERT(fulr != tulr);
    304  1.4.2.2  rmind 
    305  1.4.2.2  rmind 	/*
    306  1.4.2.2  rmind 	 * fulr is from a DELETE lookup, so fulr->ulr_count is the size
    307  1.4.2.2  rmind 	 * of the preceding entry (d_reclen).
    308  1.4.2.2  rmind 	 */
    309  1.4.2.2  rmind 	from_prev_end = fulr->ulr_offset;
    310  1.4.2.2  rmind 	KASSERT(fulr->ulr_count <= from_prev_end);
    311  1.4.2.2  rmind 	from_prev_start = (from_prev_end - fulr->ulr_count);
    312  1.4.2.2  rmind 
    313  1.4.2.2  rmind 	/*
    314  1.4.2.2  rmind 	 * tulr is from a RENAME lookup, so tulr->ulr_count is the size
    315  1.4.2.2  rmind 	 * of the free space for an entry that we are about to fill.
    316  1.4.2.2  rmind 	 */
    317  1.4.2.2  rmind 	to_start = tulr->ulr_offset;
    318  1.4.2.2  rmind 	KASSERT(tulr->ulr_count < (LFS_MAXDIRSIZE - to_start));
    319  1.4.2.2  rmind 	to_end = (to_start + tulr->ulr_count);
    320  1.4.2.2  rmind 
    321  1.4.2.2  rmind 	return
    322  1.4.2.2  rmind 	    (((to_start <= from_prev_start) && (from_prev_start < to_end)) ||
    323  1.4.2.2  rmind 		((to_start <= from_prev_end) && (from_prev_end < to_end)));
    324  1.4.2.2  rmind }
    325  1.4.2.2  rmind 
    326  1.4.2.2  rmind /*
    327  1.4.2.2  rmind  * ulfs_direct_namlen: Return the namlen of the directory entry ep from
    328  1.4.2.2  rmind  * the directory vp.
    329  1.4.2.2  rmind  */
    330  1.4.2.2  rmind static int			/* XXX int?  uint8_t?  */
    331  1.4.2.2  rmind ulfs_direct_namlen(const struct lfs_direct *ep, const struct vnode *vp)
    332  1.4.2.2  rmind {
    333  1.4.2.2  rmind 	bool swap;
    334  1.4.2.2  rmind 
    335  1.4.2.2  rmind 	KASSERT(ep != NULL);
    336  1.4.2.2  rmind 	KASSERT(vp != NULL);
    337  1.4.2.2  rmind 	KASSERT(VTOI(vp) != NULL);
    338  1.4.2.2  rmind 	KASSERT(VTOI(vp)->i_ump != NULL);
    339  1.4.2.2  rmind 
    340  1.4.2.2  rmind #if (BYTE_ORDER == LITTLE_ENDIAN)
    341  1.4.2.2  rmind 	swap = (ULFS_IPNEEDSWAP(VTOI(vp)) == 0);
    342  1.4.2.2  rmind #else
    343  1.4.2.2  rmind 	swap = (ULFS_IPNEEDSWAP(VTOI(vp)) != 0);
    344  1.4.2.2  rmind #endif
    345  1.4.2.2  rmind 
    346  1.4.2.2  rmind 	return ((FSFMT(vp) && swap)? ep->d_type : ep->d_namlen);
    347  1.4.2.2  rmind }
    348  1.4.2.2  rmind 
    349  1.4.2.2  rmind /*
    350  1.4.2.2  rmind  * ulfs_rename_recalculate_fulr: If we have just entered a directory into
    351  1.4.2.2  rmind  * dvp at tulr, and we were about to remove one at fulr for an entry
    352  1.4.2.2  rmind  * named fcnp, fulr may be invalid.  So, if necessary, recalculate it.
    353  1.4.2.2  rmind  */
    354  1.4.2.2  rmind static int
    355  1.4.2.2  rmind ulfs_rename_recalculate_fulr(struct vnode *dvp,
    356  1.4.2.2  rmind     struct ulfs_lookup_results *fulr, const struct ulfs_lookup_results *tulr,
    357  1.4.2.2  rmind     const struct componentname *fcnp)
    358  1.4.2.2  rmind {
    359  1.4.2.2  rmind 	struct mount *mp;
    360  1.4.2.2  rmind 	struct lfs *fs;
    361  1.4.2.2  rmind 	struct ulfsmount *ump;
    362  1.4.2.2  rmind 	int needswap;
    363  1.4.2.2  rmind 	/* XXX int is a silly type for this; blame ulfsmount::um_dirblksiz.  */
    364  1.4.2.2  rmind 	int dirblksiz;
    365  1.4.2.2  rmind 	doff_t search_start, search_end;
    366  1.4.2.2  rmind 	doff_t offset;		/* Offset of entry we're examining.  */
    367  1.4.2.2  rmind 	struct buf *bp;		/* I/O block we're examining.  */
    368  1.4.2.2  rmind 	char *dirbuf;		/* Pointer into directory at search_start.  */
    369  1.4.2.2  rmind 	struct lfs_direct *ep;	/* Pointer to the entry we're examining.  */
    370  1.4.2.2  rmind 	/* XXX direct::d_reclen is 16-bit;
    371  1.4.2.2  rmind 	 * ulfs_lookup_results::ulr_reclen is 32-bit.  Blah.  */
    372  1.4.2.2  rmind 	uint32_t reclen;	/* Length of the entry we're examining.  */
    373  1.4.2.2  rmind 	uint32_t prev_reclen;	/* Length of the preceding entry.  */
    374  1.4.2.2  rmind 	int error;
    375  1.4.2.2  rmind 
    376  1.4.2.2  rmind 	KASSERT(dvp != NULL);
    377  1.4.2.2  rmind 	KASSERT(dvp->v_mount != NULL);
    378  1.4.2.2  rmind 	KASSERT(VTOI(dvp) != NULL);
    379  1.4.2.2  rmind 	KASSERT(fulr != NULL);
    380  1.4.2.2  rmind 	KASSERT(tulr != NULL);
    381  1.4.2.2  rmind 	KASSERT(fulr != tulr);
    382  1.4.2.2  rmind 	KASSERT(ulfs_rename_ulr_overlap_p(fulr, tulr));
    383  1.4.2.2  rmind 
    384  1.4.2.2  rmind 	mp = dvp->v_mount;
    385  1.4.2.2  rmind 	ump = VFSTOULFS(mp);
    386  1.4.2.2  rmind 	fs = ump->um_lfs;
    387  1.4.2.2  rmind 	KASSERT(ump != NULL);
    388  1.4.2.2  rmind 	KASSERT(ump == VTOI(dvp)->i_ump);
    389  1.4.2.2  rmind 	KASSERT(fs == VTOI(dvp)->i_lfs);
    390  1.4.2.2  rmind 
    391  1.4.2.2  rmind 	needswap = ULFS_MPNEEDSWAP(fs);
    392  1.4.2.2  rmind 
    393  1.4.2.2  rmind 	dirblksiz = fs->um_dirblksiz;
    394  1.4.2.2  rmind 	KASSERT(0 < dirblksiz);
    395  1.4.2.2  rmind 	KASSERT((dirblksiz & (dirblksiz - 1)) == 0);
    396  1.4.2.2  rmind 
    397  1.4.2.2  rmind 	/* A directory block may not span across multiple I/O blocks.  */
    398  1.4.2.2  rmind 	KASSERT(dirblksiz <= mp->mnt_stat.f_iosize);
    399  1.4.2.2  rmind 
    400  1.4.2.2  rmind 	/* Find the bounds of the search.  */
    401  1.4.2.2  rmind 	search_start = tulr->ulr_offset;
    402  1.4.2.2  rmind 	KASSERT(fulr->ulr_reclen < (LFS_MAXDIRSIZE - fulr->ulr_offset));
    403  1.4.2.2  rmind 	search_end = (fulr->ulr_offset + fulr->ulr_reclen);
    404  1.4.2.2  rmind 
    405  1.4.2.2  rmind 	/* Compaction must happen only within a directory block. (*)  */
    406  1.4.2.2  rmind 	KASSERT(search_start <= search_end);
    407  1.4.2.2  rmind 	KASSERT((search_end - (search_start &~ (dirblksiz - 1))) <= dirblksiz);
    408  1.4.2.2  rmind 
    409  1.4.2.2  rmind 	dirbuf = NULL;
    410  1.4.2.2  rmind 	bp = NULL;
    411  1.4.2.2  rmind 	error = ulfs_blkatoff(dvp, (off_t)search_start, &dirbuf, &bp, false);
    412  1.4.2.2  rmind 	if (error)
    413  1.4.2.2  rmind 		return error;
    414  1.4.2.2  rmind 	KASSERT(dirbuf != NULL);
    415  1.4.2.2  rmind 	KASSERT(bp != NULL);
    416  1.4.2.2  rmind 
    417  1.4.2.2  rmind 	/*
    418  1.4.2.2  rmind 	 * Guarantee we sha'n't go past the end of the buffer we got.
    419  1.4.2.2  rmind 	 * dirbuf is bp->b_data + (search_start & (iosize - 1)), and
    420  1.4.2.2  rmind 	 * the valid range is [bp->b_data, bp->b_data + bp->b_bcount).
    421  1.4.2.2  rmind 	 */
    422  1.4.2.2  rmind 	KASSERT((search_end - search_start) <=
    423  1.4.2.2  rmind 	    (bp->b_bcount - (search_start & (mp->mnt_stat.f_iosize - 1))));
    424  1.4.2.2  rmind 
    425  1.4.2.2  rmind 	prev_reclen = fulr->ulr_count;
    426  1.4.2.2  rmind 	offset = search_start;
    427  1.4.2.2  rmind 
    428  1.4.2.2  rmind 	/*
    429  1.4.2.2  rmind 	 * Search from search_start to search_end for the entry matching
    430  1.4.2.2  rmind 	 * fcnp, which must be there because we found it before and it
    431  1.4.2.2  rmind 	 * should only at most have moved earlier.
    432  1.4.2.2  rmind 	 */
    433  1.4.2.2  rmind 	for (;;) {
    434  1.4.2.2  rmind 		KASSERT(search_start <= offset);
    435  1.4.2.2  rmind 		KASSERT(offset < search_end);
    436  1.4.2.2  rmind 
    437  1.4.2.2  rmind 		/*
    438  1.4.2.2  rmind 		 * Examine the directory entry at offset.
    439  1.4.2.2  rmind 		 */
    440  1.4.2.2  rmind 		ep = (struct lfs_direct *)(dirbuf + (offset - search_start));
    441  1.4.2.2  rmind 		reclen = ulfs_rw16(ep->d_reclen, needswap);
    442  1.4.2.2  rmind 
    443  1.4.2.2  rmind 		if (ep->d_ino == 0)
    444  1.4.2.2  rmind 			goto next;	/* Entry is unused.  */
    445  1.4.2.2  rmind 
    446  1.4.2.2  rmind 		if (ulfs_rw32(ep->d_ino, needswap) == ULFS_WINO)
    447  1.4.2.2  rmind 			goto next;	/* Entry is whiteout.  */
    448  1.4.2.2  rmind 
    449  1.4.2.2  rmind 		if (fcnp->cn_namelen != ulfs_direct_namlen(ep, dvp))
    450  1.4.2.2  rmind 			goto next;	/* Wrong name length.  */
    451  1.4.2.2  rmind 
    452  1.4.2.2  rmind 		if (memcmp(ep->d_name, fcnp->cn_nameptr, fcnp->cn_namelen))
    453  1.4.2.2  rmind 			goto next;	/* Wrong name.  */
    454  1.4.2.2  rmind 
    455  1.4.2.2  rmind 		/* Got it!  */
    456  1.4.2.2  rmind 		break;
    457  1.4.2.2  rmind 
    458  1.4.2.2  rmind next:
    459  1.4.2.2  rmind 		if (! ((reclen < search_end) &&
    460  1.4.2.2  rmind 			(offset < (search_end - reclen)))) {
    461  1.4.2.2  rmind 			brelse(bp, 0);
    462  1.4.2.2  rmind 			return EIO;	/* XXX Panic?  What?  */
    463  1.4.2.2  rmind 		}
    464  1.4.2.2  rmind 
    465  1.4.2.2  rmind 		/* We may not move past the search end.  */
    466  1.4.2.2  rmind 		KASSERT(reclen < search_end);
    467  1.4.2.2  rmind 		KASSERT(offset < (search_end - reclen));
    468  1.4.2.2  rmind 
    469  1.4.2.2  rmind 		/*
    470  1.4.2.2  rmind 		 * We may not move across a directory block boundary;
    471  1.4.2.2  rmind 		 * see (*) above.
    472  1.4.2.2  rmind 		 */
    473  1.4.2.2  rmind 		KASSERT((offset &~ (dirblksiz - 1)) ==
    474  1.4.2.2  rmind 		    ((offset + reclen) &~ (dirblksiz - 1)));
    475  1.4.2.2  rmind 
    476  1.4.2.2  rmind 		prev_reclen = reclen;
    477  1.4.2.2  rmind 		offset += reclen;
    478  1.4.2.2  rmind 	}
    479  1.4.2.2  rmind 
    480  1.4.2.2  rmind 	/*
    481  1.4.2.2  rmind 	 * Found the entry.  Record where.
    482  1.4.2.2  rmind 	 */
    483  1.4.2.2  rmind 	fulr->ulr_offset = offset;
    484  1.4.2.2  rmind 	fulr->ulr_reclen = reclen;
    485  1.4.2.2  rmind 
    486  1.4.2.2  rmind 	/*
    487  1.4.2.2  rmind 	 * Record the preceding record length, but not if we're at the
    488  1.4.2.2  rmind 	 * start of a directory block.
    489  1.4.2.2  rmind 	 */
    490  1.4.2.2  rmind 	fulr->ulr_count = ((offset & (dirblksiz - 1))? prev_reclen : 0);
    491  1.4.2.2  rmind 
    492  1.4.2.2  rmind 	brelse(bp, 0);
    493  1.4.2.2  rmind 	return 0;
    494  1.4.2.2  rmind }
    495  1.4.2.2  rmind 
    496  1.4.2.2  rmind /*
    497  1.4.2.2  rmind  * ulfs_gro_remove: Rename an object over another link to itself,
    498  1.4.2.2  rmind  * effectively removing just the original link.
    499  1.4.2.2  rmind  */
    500  1.4.2.2  rmind static int
    501  1.4.2.2  rmind ulfs_gro_remove(struct mount *mp, kauth_cred_t cred,
    502  1.4.2.2  rmind     struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
    503  1.4.2.2  rmind {
    504  1.4.2.2  rmind 	struct ulfs_lookup_results *ulr = de;
    505  1.4.2.2  rmind 	int error;
    506  1.4.2.2  rmind 
    507  1.4.2.2  rmind 	KASSERT(mp != NULL);
    508  1.4.2.2  rmind 	KASSERT(dvp != NULL);
    509  1.4.2.2  rmind 	KASSERT(cnp != NULL);
    510  1.4.2.2  rmind 	KASSERT(ulr != NULL);
    511  1.4.2.2  rmind 	KASSERT(vp != NULL);
    512  1.4.2.2  rmind 	KASSERT(dvp != vp);
    513  1.4.2.2  rmind 	KASSERT(dvp->v_mount == mp);
    514  1.4.2.2  rmind 	KASSERT(vp->v_mount == mp);
    515  1.4.2.2  rmind 	KASSERT(dvp->v_type == VDIR);
    516  1.4.2.2  rmind 	KASSERT(vp->v_type != VDIR);
    517  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    518  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    519  1.4.2.2  rmind 	KASSERT(cnp->cn_nameiop == DELETE);
    520  1.4.2.2  rmind 
    521  1.4.2.2  rmind 	fstrans_start(mp, FSTRANS_SHARED);
    522  1.4.2.2  rmind 
    523  1.4.2.2  rmind 	/* XXX ulfs_dirremove decrements vp's link count for us.  */
    524  1.4.2.2  rmind 	error = ulfs_dirremove(dvp, ulr, VTOI(vp), cnp->cn_flags, 0);
    525  1.4.2.2  rmind 	if (error)
    526  1.4.2.2  rmind 		goto out1;
    527  1.4.2.2  rmind 
    528  1.4.2.2  rmind 	VN_KNOTE(dvp, NOTE_WRITE);
    529  1.4.2.2  rmind 	VN_KNOTE(vp, (VTOI(vp)->i_nlink? NOTE_LINK : NOTE_DELETE));
    530  1.4.2.2  rmind 
    531  1.4.2.2  rmind out1:
    532  1.4.2.2  rmind 	fstrans_done(mp);
    533  1.4.2.2  rmind 	return error;
    534  1.4.2.2  rmind }
    535  1.4.2.2  rmind 
    536  1.4.2.2  rmind /*
    537  1.4.2.2  rmind  * ulfs_gro_lookup: Look up and save the lookup results.
    538  1.4.2.2  rmind  */
    539  1.4.2.2  rmind static int
    540  1.4.2.2  rmind ulfs_gro_lookup(struct mount *mp, struct vnode *dvp,
    541  1.4.2.2  rmind     struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
    542  1.4.2.2  rmind {
    543  1.4.2.2  rmind 	struct ulfs_lookup_results *ulr_ret = de_ret;
    544  1.4.2.2  rmind 	struct vnode *vp = NULL;
    545  1.4.2.2  rmind 	int error;
    546  1.4.2.2  rmind 
    547  1.4.2.2  rmind 	(void)mp;
    548  1.4.2.2  rmind 	KASSERT(mp != NULL);
    549  1.4.2.2  rmind 	KASSERT(dvp != NULL);
    550  1.4.2.2  rmind 	KASSERT(cnp != NULL);
    551  1.4.2.2  rmind 	KASSERT(ulr_ret != NULL);
    552  1.4.2.2  rmind 	KASSERT(vp_ret != NULL);
    553  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    554  1.4.2.2  rmind 
    555  1.4.2.2  rmind 	/* Kludge cargo-culted from dholland's ulfs_rename.  */
    556  1.4.2.2  rmind 	cnp->cn_flags &=~ MODMASK;
    557  1.4.2.2  rmind 	cnp->cn_flags |= (LOCKPARENT | LOCKLEAF);
    558  1.4.2.2  rmind 
    559  1.4.2.2  rmind 	error = relookup(dvp, &vp, cnp, 0 /* dummy */);
    560  1.4.2.2  rmind 	if ((error == 0) && (vp == NULL)) {
    561  1.4.2.2  rmind 		error = ENOENT;
    562  1.4.2.2  rmind 		goto out;
    563  1.4.2.2  rmind 	} else if (error) {
    564  1.4.2.2  rmind 		return error;
    565  1.4.2.2  rmind 	}
    566  1.4.2.2  rmind 
    567  1.4.2.2  rmind 	/*
    568  1.4.2.2  rmind 	 * Thanks to VFS insanity, relookup locks vp, which screws us
    569  1.4.2.2  rmind 	 * in various ways.
    570  1.4.2.2  rmind 	 */
    571  1.4.2.2  rmind 	KASSERT(vp != NULL);
    572  1.4.2.2  rmind 	VOP_UNLOCK(vp);
    573  1.4.2.2  rmind 
    574  1.4.2.2  rmind out:	*ulr_ret = VTOI(dvp)->i_crap;
    575  1.4.2.2  rmind 	*vp_ret = vp;
    576  1.4.2.2  rmind 	return error;
    577  1.4.2.2  rmind }
    578  1.4.2.2  rmind 
    579  1.4.2.2  rmind /*
    580  1.4.2.2  rmind  * ulfs_rmdired_p: Check whether the directory vp has been rmdired.
    581  1.4.2.2  rmind  *
    582  1.4.2.2  rmind  * vp must be locked and referenced.
    583  1.4.2.2  rmind  */
    584  1.4.2.2  rmind static bool
    585  1.4.2.2  rmind ulfs_rmdired_p(struct vnode *vp)
    586  1.4.2.2  rmind {
    587  1.4.2.2  rmind 
    588  1.4.2.2  rmind 	KASSERT(vp != NULL);
    589  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    590  1.4.2.2  rmind 	KASSERT(vp->v_type == VDIR);
    591  1.4.2.2  rmind 
    592  1.4.2.2  rmind 	/* XXX Is this correct?  */
    593  1.4.2.2  rmind 	return (VTOI(vp)->i_size == 0);
    594  1.4.2.2  rmind }
    595  1.4.2.2  rmind 
    596  1.4.2.2  rmind /*
    597  1.4.2.2  rmind  * ulfs_dirbuf_dotdot_namlen: Return the namlen of the directory buffer
    598  1.4.2.2  rmind  * dirbuf that came from the directory vp.  Swap byte order if
    599  1.4.2.2  rmind  * necessary.
    600  1.4.2.2  rmind  */
    601  1.4.2.2  rmind static int			/* XXX int?  uint8_t?  */
    602  1.4.2.2  rmind ulfs_dirbuf_dotdot_namlen(const struct lfs_dirtemplate *dirbuf,
    603  1.4.2.2  rmind     const struct vnode *vp)
    604  1.4.2.2  rmind {
    605  1.4.2.2  rmind 	bool swap;
    606  1.4.2.2  rmind 
    607  1.4.2.2  rmind 	KASSERT(dirbuf != NULL);
    608  1.4.2.2  rmind 	KASSERT(vp != NULL);
    609  1.4.2.2  rmind 	KASSERT(VTOI(vp) != NULL);
    610  1.4.2.2  rmind 	KASSERT(VTOI(vp)->i_ump != NULL);
    611  1.4.2.2  rmind 
    612  1.4.2.2  rmind #if (BYTE_ORDER == LITTLE_ENDIAN)
    613  1.4.2.2  rmind 	swap = (ULFS_IPNEEDSWAP(VTOI(vp)) == 0);
    614  1.4.2.2  rmind #else
    615  1.4.2.2  rmind 	swap = (ULFS_IPNEEDSWAP(VTOI(vp)) != 0);
    616  1.4.2.2  rmind #endif
    617  1.4.2.2  rmind 
    618  1.4.2.2  rmind 	return ((FSFMT(vp) && swap)?
    619  1.4.2.2  rmind 	    dirbuf->dotdot_type : dirbuf->dotdot_namlen);
    620  1.4.2.2  rmind }
    621  1.4.2.2  rmind 
    622  1.4.2.2  rmind /*
    623  1.4.2.2  rmind  * ulfs_read_dotdot: Store in *ino_ret the inode number of the parent
    624  1.4.2.2  rmind  * of the directory vp.
    625  1.4.2.2  rmind  */
    626  1.4.2.2  rmind static int
    627  1.4.2.2  rmind ulfs_read_dotdot(struct vnode *vp, kauth_cred_t cred, ino_t *ino_ret)
    628  1.4.2.2  rmind {
    629  1.4.2.2  rmind 	struct lfs_dirtemplate dirbuf;
    630  1.4.2.2  rmind 	int error;
    631  1.4.2.2  rmind 
    632  1.4.2.2  rmind 	KASSERT(vp != NULL);
    633  1.4.2.2  rmind 	KASSERT(ino_ret != NULL);
    634  1.4.2.2  rmind 	KASSERT(vp->v_type == VDIR);
    635  1.4.2.2  rmind 
    636  1.4.2.2  rmind 	error = vn_rdwr(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
    637  1.4.2.2  rmind 	    UIO_SYSSPACE, IO_NODELOCKED, cred, NULL, NULL);
    638  1.4.2.2  rmind 	if (error)
    639  1.4.2.2  rmind 		return error;
    640  1.4.2.2  rmind 
    641  1.4.2.2  rmind 	if (ulfs_dirbuf_dotdot_namlen(&dirbuf, vp) != 2 ||
    642  1.4.2.2  rmind 	    dirbuf.dotdot_name[0] != '.' ||
    643  1.4.2.2  rmind 	    dirbuf.dotdot_name[1] != '.')
    644  1.4.2.2  rmind 		/* XXX Panic?  Print warning?  */
    645  1.4.2.2  rmind 		return ENOTDIR;
    646  1.4.2.2  rmind 
    647  1.4.2.2  rmind 	*ino_ret = ulfs_rw32(dirbuf.dotdot_ino,
    648  1.4.2.2  rmind 	    ULFS_IPNEEDSWAP(VTOI(vp)));
    649  1.4.2.2  rmind 	return 0;
    650  1.4.2.2  rmind }
    651  1.4.2.2  rmind 
    652  1.4.2.2  rmind /*
    653  1.4.2.2  rmind  * ulfs_gro_lock_directory: Lock the directory vp, but fail if it has
    654  1.4.2.2  rmind  * been rmdir'd.
    655  1.4.2.2  rmind  */
    656  1.4.2.2  rmind static int
    657  1.4.2.2  rmind ulfs_gro_lock_directory(struct mount *mp, struct vnode *vp)
    658  1.4.2.2  rmind {
    659  1.4.2.2  rmind 
    660  1.4.2.2  rmind 	(void)mp;
    661  1.4.2.2  rmind 	KASSERT(mp != NULL);
    662  1.4.2.2  rmind 	KASSERT(vp != NULL);
    663  1.4.2.2  rmind 	KASSERT(vp->v_mount == mp);
    664  1.4.2.2  rmind 
    665  1.4.2.2  rmind 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    666  1.4.2.2  rmind 
    667  1.4.2.2  rmind 	if (ulfs_rmdired_p(vp)) {
    668  1.4.2.2  rmind 		VOP_UNLOCK(vp);
    669  1.4.2.2  rmind 		return ENOENT;
    670  1.4.2.2  rmind 	}
    671  1.4.2.2  rmind 
    672  1.4.2.2  rmind 	return 0;
    673  1.4.2.2  rmind }
    674  1.4.2.2  rmind 
    675  1.4.2.2  rmind /*
    676  1.4.2.2  rmind  * ulfs_gro_genealogy: Analyze the genealogy of the source and target
    677  1.4.2.2  rmind  * directories.
    678  1.4.2.2  rmind  */
    679  1.4.2.2  rmind static int
    680  1.4.2.2  rmind ulfs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
    681  1.4.2.2  rmind     struct vnode *fdvp, struct vnode *tdvp,
    682  1.4.2.2  rmind     struct vnode **intermediate_node_ret)
    683  1.4.2.2  rmind {
    684  1.4.2.2  rmind 	struct vnode *vp, *dvp;
    685  1.4.2.2  rmind 	ino_t dotdot_ino;
    686  1.4.2.2  rmind 	int error;
    687  1.4.2.2  rmind 
    688  1.4.2.2  rmind 	KASSERT(mp != NULL);
    689  1.4.2.2  rmind 	KASSERT(fdvp != NULL);
    690  1.4.2.2  rmind 	KASSERT(tdvp != NULL);
    691  1.4.2.2  rmind 	KASSERT(fdvp != tdvp);
    692  1.4.2.2  rmind 	KASSERT(intermediate_node_ret != NULL);
    693  1.4.2.2  rmind 	KASSERT(fdvp->v_mount == mp);
    694  1.4.2.2  rmind 	KASSERT(tdvp->v_mount == mp);
    695  1.4.2.2  rmind 	KASSERT(fdvp->v_type == VDIR);
    696  1.4.2.2  rmind 	KASSERT(tdvp->v_type == VDIR);
    697  1.4.2.2  rmind 
    698  1.4.2.2  rmind 	/*
    699  1.4.2.2  rmind 	 * We need to provisionally lock tdvp to keep rmdir from
    700  1.4.2.2  rmind 	 * deleting it -- or any ancestor -- at an inopportune moment.
    701  1.4.2.2  rmind 	 */
    702  1.4.2.2  rmind 	error = ulfs_gro_lock_directory(mp, tdvp);
    703  1.4.2.2  rmind 	if (error)
    704  1.4.2.2  rmind 		return error;
    705  1.4.2.2  rmind 
    706  1.4.2.2  rmind 	vp = tdvp;
    707  1.4.2.2  rmind 	vref(vp);
    708  1.4.2.2  rmind 
    709  1.4.2.2  rmind 	for (;;) {
    710  1.4.2.2  rmind 		KASSERT(vp != NULL);
    711  1.4.2.2  rmind 		KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    712  1.4.2.2  rmind 		KASSERT(vp->v_mount == mp);
    713  1.4.2.2  rmind 		KASSERT(vp->v_type == VDIR);
    714  1.4.2.2  rmind 		KASSERT(!ulfs_rmdired_p(vp));
    715  1.4.2.2  rmind 
    716  1.4.2.2  rmind 		/* Did we hit the root without finding fdvp?  */
    717  1.4.2.2  rmind 		if (VTOI(vp)->i_number == ULFS_ROOTINO) {
    718  1.4.2.2  rmind 			vput(vp);
    719  1.4.2.2  rmind 			*intermediate_node_ret = NULL;
    720  1.4.2.2  rmind 			return 0;
    721  1.4.2.2  rmind 		}
    722  1.4.2.2  rmind 
    723  1.4.2.2  rmind 		error = ulfs_read_dotdot(vp, cred, &dotdot_ino);
    724  1.4.2.2  rmind 		if (error) {
    725  1.4.2.2  rmind 			vput(vp);
    726  1.4.2.2  rmind 			return error;
    727  1.4.2.2  rmind 		}
    728  1.4.2.2  rmind 
    729  1.4.2.2  rmind 		/* Did we find that fdvp is an ancestor of tdvp?  */
    730  1.4.2.2  rmind 		if (VTOI(fdvp)->i_number == dotdot_ino) {
    731  1.4.2.2  rmind 			/* Unlock vp, but keep it referenced.  */
    732  1.4.2.2  rmind 			VOP_UNLOCK(vp);
    733  1.4.2.2  rmind 			*intermediate_node_ret = vp;
    734  1.4.2.2  rmind 			return 0;
    735  1.4.2.2  rmind 		}
    736  1.4.2.2  rmind 
    737  1.4.2.2  rmind 		/* Neither -- keep ascending the family tree.  */
    738  1.4.2.2  rmind 
    739  1.4.2.2  rmind 		/*
    740  1.4.2.2  rmind 		 * Unlock vp so that we can lock the parent, but keep
    741  1.4.2.2  rmind 		 * vp referenced until after we have found the parent,
    742  1.4.2.2  rmind 		 * so that dotdot_ino will not be recycled.
    743  1.4.2.2  rmind 		 *
    744  1.4.2.2  rmind 		 * XXX This guarantees that vp's inode number will not
    745  1.4.2.2  rmind 		 * be recycled, but why can't dotdot_ino be recycled?
    746  1.4.2.2  rmind 		 */
    747  1.4.2.2  rmind 		VOP_UNLOCK(vp);
    748  1.4.2.2  rmind 		error = VFS_VGET(mp, dotdot_ino, &dvp);
    749  1.4.2.2  rmind 		vrele(vp);
    750  1.4.2.2  rmind 		if (error)
    751  1.4.2.2  rmind 			return error;
    752  1.4.2.2  rmind 
    753  1.4.2.2  rmind 		KASSERT(dvp != NULL);
    754  1.4.2.2  rmind 		KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
    755  1.4.2.2  rmind 		vp = dvp;
    756  1.4.2.2  rmind 
    757  1.4.2.2  rmind 		if (vp->v_type != VDIR) {
    758  1.4.2.2  rmind 			/*
    759  1.4.2.2  rmind 			 * XXX Panic?  Print a warning?  Can this
    760  1.4.2.2  rmind 			 * happen if we lose the race I suspect to
    761  1.4.2.2  rmind 			 * exist above, and the `..' inode number has
    762  1.4.2.2  rmind 			 * been recycled?
    763  1.4.2.2  rmind 			 */
    764  1.4.2.2  rmind 			vput(vp);
    765  1.4.2.2  rmind 			return ENOTDIR;
    766  1.4.2.2  rmind 		}
    767  1.4.2.2  rmind 
    768  1.4.2.2  rmind 		if (ulfs_rmdired_p(vp)) {
    769  1.4.2.2  rmind 			vput(vp);
    770  1.4.2.2  rmind 			return ENOENT;
    771  1.4.2.2  rmind 		}
    772  1.4.2.2  rmind 	}
    773  1.4.2.2  rmind }
    774  1.4.2.2  rmind 
    775  1.4.2.2  rmind /*
    776  1.4.2.2  rmind  * ulfs_gro_rename: Actually perform the rename operation.
    777  1.4.2.2  rmind  */
    778  1.4.2.2  rmind static int
    779  1.4.2.2  rmind ulfs_gro_rename(struct mount *mp, kauth_cred_t cred,
    780  1.4.2.2  rmind     struct vnode *fdvp, struct componentname *fcnp,
    781  1.4.2.2  rmind     void *fde, struct vnode *fvp,
    782  1.4.2.2  rmind     struct vnode *tdvp, struct componentname *tcnp,
    783  1.4.2.2  rmind     void *tde, struct vnode *tvp)
    784  1.4.2.2  rmind {
    785  1.4.2.2  rmind 	struct ulfs_lookup_results *fulr = fde;
    786  1.4.2.2  rmind 	struct ulfs_lookup_results *tulr = tde;
    787  1.4.2.2  rmind 	bool directory_p, reparent_p;
    788  1.4.2.2  rmind 	struct lfs_direct *newdir;
    789  1.4.2.2  rmind 	int error;
    790  1.4.2.2  rmind 
    791  1.4.2.2  rmind 	KASSERT(mp != NULL);
    792  1.4.2.2  rmind 	KASSERT(fdvp != NULL);
    793  1.4.2.2  rmind 	KASSERT(fcnp != NULL);
    794  1.4.2.2  rmind 	KASSERT(fulr != NULL);
    795  1.4.2.2  rmind 	KASSERT(fvp != NULL);
    796  1.4.2.2  rmind 	KASSERT(tdvp != NULL);
    797  1.4.2.2  rmind 	KASSERT(tcnp != NULL);
    798  1.4.2.2  rmind 	KASSERT(tulr != NULL);
    799  1.4.2.2  rmind 	KASSERT(fulr != tulr);
    800  1.4.2.2  rmind 	KASSERT(fdvp != fvp);
    801  1.4.2.2  rmind 	KASSERT(fdvp != tvp);
    802  1.4.2.2  rmind 	KASSERT(tdvp != fvp);
    803  1.4.2.2  rmind 	KASSERT(tdvp != tvp);
    804  1.4.2.2  rmind 	KASSERT(fvp != tvp);
    805  1.4.2.2  rmind 	KASSERT(fdvp->v_mount == mp);
    806  1.4.2.2  rmind 	KASSERT(fvp->v_mount == mp);
    807  1.4.2.2  rmind 	KASSERT(tdvp->v_mount == mp);
    808  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
    809  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
    810  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
    811  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
    812  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    813  1.4.2.2  rmind 
    814  1.4.2.2  rmind 	/*
    815  1.4.2.2  rmind 	 * We shall need to temporarily bump the link count, so make
    816  1.4.2.2  rmind 	 * sure there is room to do so.
    817  1.4.2.2  rmind 	 */
    818  1.4.2.2  rmind 	if ((nlink_t)VTOI(fvp)->i_nlink >= LINK_MAX)
    819  1.4.2.2  rmind 		return EMLINK;
    820  1.4.2.2  rmind 
    821  1.4.2.2  rmind 	directory_p = (fvp->v_type == VDIR);
    822  1.4.2.2  rmind 	KASSERT(directory_p == ((VTOI(fvp)->i_mode & LFS_IFMT) == LFS_IFDIR));
    823  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (directory_p == (tvp->v_type == VDIR)));
    824  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (directory_p ==
    825  1.4.2.2  rmind 		((VTOI(tvp)->i_mode & LFS_IFMT) == LFS_IFDIR)));
    826  1.4.2.2  rmind 
    827  1.4.2.2  rmind 	reparent_p = (fdvp != tdvp);
    828  1.4.2.2  rmind 	KASSERT(reparent_p == (VTOI(fdvp)->i_number != VTOI(tdvp)->i_number));
    829  1.4.2.2  rmind 
    830  1.4.2.2  rmind 	/*
    831  1.4.2.2  rmind 	 * Commence hacking of the data on disk.
    832  1.4.2.2  rmind 	 */
    833  1.4.2.2  rmind 
    834  1.4.2.2  rmind 	fstrans_start(mp, FSTRANS_SHARED);
    835  1.4.2.2  rmind 	error = 0;
    836  1.4.2.2  rmind 
    837  1.4.2.2  rmind 	/*
    838  1.4.2.2  rmind 	 * 1) Bump link count while we're moving stuff
    839  1.4.2.2  rmind 	 *    around.  If we crash somewhere before
    840  1.4.2.2  rmind 	 *    completing our work, the link count
    841  1.4.2.2  rmind 	 *    may be wrong, but correctable.
    842  1.4.2.2  rmind 	 */
    843  1.4.2.2  rmind 
    844  1.4.2.2  rmind 	KASSERT((nlink_t)VTOI(fvp)->i_nlink < LINK_MAX);
    845  1.4.2.2  rmind 	VTOI(fvp)->i_nlink++;
    846  1.4.2.2  rmind 	DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
    847  1.4.2.2  rmind 	VTOI(fvp)->i_flag |= IN_CHANGE;
    848  1.4.2.2  rmind 	error = lfs_update(fvp, NULL, NULL, UPDATE_DIROP);
    849  1.4.2.2  rmind 	if (error)
    850  1.4.2.2  rmind 		goto whymustithurtsomuch;
    851  1.4.2.2  rmind 
    852  1.4.2.2  rmind 	/*
    853  1.4.2.2  rmind 	 * 2) If target doesn't exist, link the target
    854  1.4.2.2  rmind 	 *    to the source and unlink the source.
    855  1.4.2.2  rmind 	 *    Otherwise, rewrite the target directory
    856  1.4.2.2  rmind 	 *    entry to reference the source inode and
    857  1.4.2.2  rmind 	 *    expunge the original entry's existence.
    858  1.4.2.2  rmind 	 */
    859  1.4.2.2  rmind 
    860  1.4.2.2  rmind 	if (tvp == NULL) {
    861  1.4.2.2  rmind 		/*
    862  1.4.2.2  rmind 		 * Account for ".." in new directory.
    863  1.4.2.2  rmind 		 * When source and destination have the same
    864  1.4.2.2  rmind 		 * parent we don't fool with the link count.
    865  1.4.2.2  rmind 		 */
    866  1.4.2.2  rmind 		if (directory_p && reparent_p) {
    867  1.4.2.2  rmind 			if ((nlink_t)VTOI(tdvp)->i_nlink >= LINK_MAX) {
    868  1.4.2.2  rmind 				error = EMLINK;
    869  1.4.2.2  rmind 				goto whymustithurtsomuch;
    870  1.4.2.2  rmind 			}
    871  1.4.2.2  rmind 			KASSERT((nlink_t)VTOI(tdvp)->i_nlink < LINK_MAX);
    872  1.4.2.2  rmind 			VTOI(tdvp)->i_nlink++;
    873  1.4.2.2  rmind 			DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
    874  1.4.2.2  rmind 			VTOI(tdvp)->i_flag |= IN_CHANGE;
    875  1.4.2.2  rmind 			error = lfs_update(tdvp, NULL, NULL, UPDATE_DIROP);
    876  1.4.2.2  rmind 			if (error) {
    877  1.4.2.2  rmind 				/*
    878  1.4.2.2  rmind 				 * Link count update didn't take --
    879  1.4.2.2  rmind 				 * back out the in-memory link count.
    880  1.4.2.2  rmind 				 */
    881  1.4.2.2  rmind 				KASSERT(0 < VTOI(tdvp)->i_nlink);
    882  1.4.2.2  rmind 				VTOI(tdvp)->i_nlink--;
    883  1.4.2.2  rmind 				DIP_ASSIGN(VTOI(tdvp), nlink,
    884  1.4.2.2  rmind 				    VTOI(tdvp)->i_nlink);
    885  1.4.2.2  rmind 				VTOI(tdvp)->i_flag |= IN_CHANGE;
    886  1.4.2.2  rmind 				goto whymustithurtsomuch;
    887  1.4.2.2  rmind 			}
    888  1.4.2.2  rmind 		}
    889  1.4.2.2  rmind 
    890  1.4.2.2  rmind 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
    891  1.4.2.2  rmind 		ulfs_makedirentry(VTOI(fvp), tcnp, newdir);
    892  1.4.2.2  rmind 		error = ulfs_direnter(tdvp, tulr, NULL, newdir, tcnp, NULL);
    893  1.4.2.2  rmind 		pool_cache_put(ulfs_direct_cache, newdir);
    894  1.4.2.2  rmind 		if (error) {
    895  1.4.2.2  rmind 			if (directory_p && reparent_p) {
    896  1.4.2.2  rmind 				/*
    897  1.4.2.2  rmind 				 * Directory update didn't take, but
    898  1.4.2.2  rmind 				 * the link count update did -- back
    899  1.4.2.2  rmind 				 * out the in-memory link count and the
    900  1.4.2.2  rmind 				 * on-disk link count.
    901  1.4.2.2  rmind 				 */
    902  1.4.2.2  rmind 				KASSERT(0 < VTOI(tdvp)->i_nlink);
    903  1.4.2.2  rmind 				VTOI(tdvp)->i_nlink--;
    904  1.4.2.2  rmind 				DIP_ASSIGN(VTOI(tdvp), nlink,
    905  1.4.2.2  rmind 				    VTOI(tdvp)->i_nlink);
    906  1.4.2.2  rmind 				VTOI(tdvp)->i_flag |= IN_CHANGE;
    907  1.4.2.2  rmind 				(void)lfs_update(tdvp, NULL, NULL,
    908  1.4.2.2  rmind 				    UPDATE_WAIT | UPDATE_DIROP);
    909  1.4.2.2  rmind 			}
    910  1.4.2.2  rmind 			goto whymustithurtsomuch;
    911  1.4.2.2  rmind 		}
    912  1.4.2.2  rmind 	} else {
    913  1.4.2.2  rmind 		if (directory_p)
    914  1.4.2.2  rmind 			/* XXX WTF?  Why purge here?  Why not purge others?  */
    915  1.4.2.2  rmind 			cache_purge(tdvp);
    916  1.4.2.2  rmind 
    917  1.4.2.2  rmind 		/*
    918  1.4.2.2  rmind 		 * Make the target directory's entry for tcnp point at
    919  1.4.2.2  rmind 		 * the source node.
    920  1.4.2.2  rmind 		 *
    921  1.4.2.2  rmind 		 * XXX ulfs_dirrewrite decrements tvp's link count, but
    922  1.4.2.2  rmind 		 * doesn't touch the link count of the new inode.  Go
    923  1.4.2.2  rmind 		 * figure.
    924  1.4.2.2  rmind 		 */
    925  1.4.2.2  rmind 		error = ulfs_dirrewrite(VTOI(tdvp), tulr->ulr_offset,
    926  1.4.2.2  rmind 		    VTOI(tvp), VTOI(fvp)->i_number, LFS_IFTODT(VTOI(fvp)->i_mode),
    927  1.4.2.2  rmind 		    ((directory_p && reparent_p) ? reparent_p : directory_p),
    928  1.4.2.2  rmind 		    IN_CHANGE | IN_UPDATE);
    929  1.4.2.2  rmind 		if (error)
    930  1.4.2.2  rmind 			goto whymustithurtsomuch;
    931  1.4.2.2  rmind 
    932  1.4.2.2  rmind 		/*
    933  1.4.2.2  rmind 		 * If the source and target are directories, and the
    934  1.4.2.2  rmind 		 * target is in the same directory as the source,
    935  1.4.2.2  rmind 		 * decrement the link count of the common parent
    936  1.4.2.2  rmind 		 * directory, since we are removing the target from
    937  1.4.2.2  rmind 		 * that directory.
    938  1.4.2.2  rmind 		 */
    939  1.4.2.2  rmind 		if (directory_p && !reparent_p) {
    940  1.4.2.2  rmind 			KASSERT(fdvp == tdvp);
    941  1.4.2.2  rmind 			/* XXX check, don't kassert */
    942  1.4.2.2  rmind 			KASSERT(0 < VTOI(tdvp)->i_nlink);
    943  1.4.2.2  rmind 			VTOI(tdvp)->i_nlink--;
    944  1.4.2.2  rmind 			DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
    945  1.4.2.2  rmind 			VTOI(tdvp)->i_flag |= IN_CHANGE;
    946  1.4.2.2  rmind 		}
    947  1.4.2.2  rmind 
    948  1.4.2.2  rmind 		if (directory_p) {
    949  1.4.2.2  rmind 			/*
    950  1.4.2.2  rmind 			 * XXX I don't understand the following comment
    951  1.4.2.2  rmind 			 * from ulfs_rename -- in particular, the part
    952  1.4.2.2  rmind 			 * about `there may be other hard links'.
    953  1.4.2.2  rmind 			 *
    954  1.4.2.2  rmind 			 * Truncate inode. The only stuff left in the directory
    955  1.4.2.2  rmind 			 * is "." and "..". The "." reference is inconsequential
    956  1.4.2.2  rmind 			 * since we are quashing it. We have removed the "."
    957  1.4.2.2  rmind 			 * reference and the reference in the parent directory,
    958  1.4.2.2  rmind 			 * but there may be other hard links.
    959  1.4.2.2  rmind 			 *
    960  1.4.2.2  rmind 			 * XXX The ulfs_dirempty call earlier does
    961  1.4.2.2  rmind 			 * not guarantee anything about nlink.
    962  1.4.2.2  rmind 			 */
    963  1.4.2.2  rmind 			if (VTOI(tvp)->i_nlink != 1)
    964  1.4.2.2  rmind 				ulfs_dirbad(VTOI(tvp), (doff_t)0,
    965  1.4.2.2  rmind 				    "hard-linked directory");
    966  1.4.2.2  rmind 			VTOI(tvp)->i_nlink = 0;
    967  1.4.2.2  rmind 			DIP_ASSIGN(VTOI(tvp), nlink, 0);
    968  1.4.2.2  rmind 			error = lfs_truncate(tvp, (off_t)0, IO_SYNC, cred);
    969  1.4.2.2  rmind 			if (error)
    970  1.4.2.2  rmind 				goto whymustithurtsomuch;
    971  1.4.2.2  rmind 		}
    972  1.4.2.2  rmind 	}
    973  1.4.2.2  rmind 
    974  1.4.2.2  rmind 	/*
    975  1.4.2.2  rmind 	 * If the source is a directory with a new parent, the link
    976  1.4.2.2  rmind 	 * count of the old parent directory must be decremented and
    977  1.4.2.2  rmind 	 * ".." set to point to the new parent.
    978  1.4.2.2  rmind 	 *
    979  1.4.2.2  rmind 	 * XXX ulfs_dirrewrite updates the link count of fdvp, but not
    980  1.4.2.2  rmind 	 * the link count of fvp or the link count of tdvp.  Go figure.
    981  1.4.2.2  rmind 	 */
    982  1.4.2.2  rmind 	if (directory_p && reparent_p) {
    983  1.4.2.2  rmind 		error = ulfs_dirrewrite(VTOI(fvp), mastertemplate.dot_reclen,
    984  1.4.2.2  rmind 		    VTOI(fdvp), VTOI(tdvp)->i_number, LFS_DT_DIR, 0, IN_CHANGE);
    985  1.4.2.2  rmind #if 0		/* XXX This branch was not in ulfs_rename! */
    986  1.4.2.2  rmind 		if (error)
    987  1.4.2.2  rmind 			goto whymustithurtsomuch;
    988  1.4.2.2  rmind #endif
    989  1.4.2.2  rmind 
    990  1.4.2.2  rmind 		/* XXX WTF?  Why purge here?  Why not purge others?  */
    991  1.4.2.2  rmind 		cache_purge(fdvp);
    992  1.4.2.2  rmind 	}
    993  1.4.2.2  rmind 
    994  1.4.2.2  rmind 	/*
    995  1.4.2.2  rmind 	 * 3) Unlink the source.
    996  1.4.2.2  rmind 	 */
    997  1.4.2.2  rmind 
    998  1.4.2.2  rmind 	/*
    999  1.4.2.2  rmind 	 * ulfs_direnter may compact the directory in the process of
   1000  1.4.2.2  rmind 	 * inserting a new entry.  That may invalidate fulr, which we
   1001  1.4.2.2  rmind 	 * need in order to remove the old entry.  In that case, we
   1002  1.4.2.2  rmind 	 * need to recalculate what fulr should be.
   1003  1.4.2.2  rmind 	 */
   1004  1.4.2.2  rmind 	if (!reparent_p && (tvp == NULL) &&
   1005  1.4.2.2  rmind 	    ulfs_rename_ulr_overlap_p(fulr, tulr)) {
   1006  1.4.2.2  rmind 		error = ulfs_rename_recalculate_fulr(fdvp, fulr, tulr, fcnp);
   1007  1.4.2.2  rmind #if 0				/* XXX */
   1008  1.4.2.2  rmind 		if (error)	/* XXX Try to back out changes?  */
   1009  1.4.2.2  rmind 			goto whymustithurtsomuch;
   1010  1.4.2.2  rmind #endif
   1011  1.4.2.2  rmind 	}
   1012  1.4.2.2  rmind 
   1013  1.4.2.2  rmind 	/*
   1014  1.4.2.2  rmind 	 * XXX 0 means !isrmdir.  But can't this be an rmdir?
   1015  1.4.2.2  rmind 	 * XXX Well, turns out that argument to ulfs_dirremove is ignored...
   1016  1.4.2.2  rmind 	 * XXX And it turns out ulfs_dirremove updates the link count of fvp.
   1017  1.4.2.2  rmind 	 * XXX But it doesn't update the link count of fdvp.  Go figure.
   1018  1.4.2.2  rmind 	 * XXX fdvp's link count is updated in ulfs_dirrewrite instead.
   1019  1.4.2.2  rmind 	 * XXX Actually, sometimes it doesn't update fvp's link count.
   1020  1.4.2.2  rmind 	 * XXX I hate the world.
   1021  1.4.2.2  rmind 	 */
   1022  1.4.2.2  rmind 	error = ulfs_dirremove(fdvp, fulr, VTOI(fvp), fcnp->cn_flags, 0);
   1023  1.4.2.2  rmind 	if (error)
   1024  1.4.2.2  rmind #if 0				/* XXX */
   1025  1.4.2.2  rmind 		goto whymustithurtsomuch;
   1026  1.4.2.2  rmind #endif
   1027  1.4.2.2  rmind 		goto arghmybrainhurts;
   1028  1.4.2.2  rmind 
   1029  1.4.2.2  rmind 	/*
   1030  1.4.2.2  rmind 	 * XXX Perhaps this should go at the top, in case the file
   1031  1.4.2.2  rmind 	 * system is modified but incompletely so because of an
   1032  1.4.2.2  rmind 	 * intermediate error.
   1033  1.4.2.2  rmind 	 */
   1034  1.4.2.2  rmind 	genfs_rename_knote(fdvp, fvp, tdvp, tvp,
   1035  1.4.2.2  rmind 	    ((tvp != NULL) && (VTOI(tvp)->i_nlink == 0)));
   1036  1.4.2.2  rmind #if 0				/* XXX */
   1037  1.4.2.2  rmind 	genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
   1038  1.4.2.2  rmind #endif
   1039  1.4.2.2  rmind 	goto arghmybrainhurts;
   1040  1.4.2.2  rmind 
   1041  1.4.2.2  rmind whymustithurtsomuch:
   1042  1.4.2.2  rmind 	KASSERT(0 < VTOI(fvp)->i_nlink);
   1043  1.4.2.2  rmind 	VTOI(fvp)->i_nlink--;
   1044  1.4.2.2  rmind 	DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
   1045  1.4.2.2  rmind 	VTOI(fvp)->i_flag |= IN_CHANGE;
   1046  1.4.2.2  rmind 
   1047  1.4.2.2  rmind arghmybrainhurts:
   1048  1.4.2.2  rmind /*ihateyou:*/
   1049  1.4.2.2  rmind 	fstrans_done(mp);
   1050  1.4.2.2  rmind 	return error;
   1051  1.4.2.2  rmind }
   1052  1.4.2.2  rmind 
   1053  1.4.2.2  rmind /*
   1054  1.4.2.2  rmind  * lfs_gro_rename: Actually perform the rename operation.  Do a little
   1055  1.4.2.2  rmind  * LFS bookkeeping and then defer to ulfs_gro_rename.
   1056  1.4.2.2  rmind  */
   1057  1.4.2.2  rmind static int
   1058  1.4.2.2  rmind lfs_gro_rename(struct mount *mp, kauth_cred_t cred,
   1059  1.4.2.2  rmind     struct vnode *fdvp, struct componentname *fcnp,
   1060  1.4.2.2  rmind     void *fde, struct vnode *fvp,
   1061  1.4.2.2  rmind     struct vnode *tdvp, struct componentname *tcnp,
   1062  1.4.2.2  rmind     void *tde, struct vnode *tvp)
   1063  1.4.2.2  rmind {
   1064  1.4.2.2  rmind 	int error;
   1065  1.4.2.2  rmind 
   1066  1.4.2.2  rmind 	KASSERT(mp != NULL);
   1067  1.4.2.2  rmind 	KASSERT(fdvp != NULL);
   1068  1.4.2.2  rmind 	KASSERT(fcnp != NULL);
   1069  1.4.2.2  rmind 	KASSERT(fde != NULL);
   1070  1.4.2.2  rmind 	KASSERT(fvp != NULL);
   1071  1.4.2.2  rmind 	KASSERT(tdvp != NULL);
   1072  1.4.2.2  rmind 	KASSERT(tcnp != NULL);
   1073  1.4.2.2  rmind 	KASSERT(tde != NULL);
   1074  1.4.2.2  rmind 	KASSERT(fdvp != fvp);
   1075  1.4.2.2  rmind 	KASSERT(fdvp != tvp);
   1076  1.4.2.2  rmind 	KASSERT(tdvp != fvp);
   1077  1.4.2.2  rmind 	KASSERT(tdvp != tvp);
   1078  1.4.2.2  rmind 	KASSERT(fvp != tvp);
   1079  1.4.2.2  rmind 	KASSERT(fdvp->v_mount == mp);
   1080  1.4.2.2  rmind 	KASSERT(fvp->v_mount == mp);
   1081  1.4.2.2  rmind 	KASSERT(tdvp->v_mount == mp);
   1082  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (tvp->v_mount == mp));
   1083  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
   1084  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
   1085  1.4.2.2  rmind 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   1086  1.4.2.2  rmind 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   1087  1.4.2.2  rmind 
   1088  1.4.2.2  rmind 	error = SET_DIROP_REMOVE(tdvp, tvp);
   1089  1.4.2.2  rmind 	if (error != 0)
   1090  1.4.2.2  rmind 		return error;
   1091  1.4.2.2  rmind 
   1092  1.4.2.2  rmind 	MARK_VNODE(fdvp);
   1093  1.4.2.2  rmind 	MARK_VNODE(fvp);
   1094  1.4.2.2  rmind 
   1095  1.4.2.2  rmind 	error = ulfs_gro_rename(mp, cred,
   1096  1.4.2.2  rmind 	    fdvp, fcnp, fde, fvp,
   1097  1.4.2.2  rmind 	    tdvp, tcnp, tde, tvp);
   1098  1.4.2.2  rmind 
   1099  1.4.2.2  rmind 	UNMARK_VNODE(fdvp);
   1100  1.4.2.2  rmind 	UNMARK_VNODE(fvp);
   1101  1.4.2.2  rmind 	SET_ENDOP_REMOVE(VFSTOULFS(mp)->um_lfs, tdvp, tvp, "rename");
   1102  1.4.2.2  rmind 
   1103  1.4.2.2  rmind 	return error;
   1104  1.4.2.2  rmind }
   1105  1.4.2.2  rmind 
   1106  1.4.2.2  rmind static const struct genfs_rename_ops lfs_genfs_rename_ops = {
   1107  1.4.2.2  rmind 	.gro_directory_empty_p		= ulfs_gro_directory_empty_p,
   1108  1.4.2.2  rmind 	.gro_rename_check_possible	= ulfs_gro_rename_check_possible,
   1109  1.4.2.2  rmind 	.gro_rename_check_permitted	= ulfs_gro_rename_check_permitted,
   1110  1.4.2.2  rmind 	.gro_remove_check_possible	= ulfs_gro_remove_check_possible,
   1111  1.4.2.2  rmind 	.gro_remove_check_permitted	= ulfs_gro_remove_check_permitted,
   1112  1.4.2.2  rmind 	.gro_rename			= lfs_gro_rename,
   1113  1.4.2.2  rmind 	.gro_remove			= ulfs_gro_remove,
   1114  1.4.2.2  rmind 	.gro_lookup			= ulfs_gro_lookup,
   1115  1.4.2.2  rmind 	.gro_genealogy			= ulfs_gro_genealogy,
   1116  1.4.2.2  rmind 	.gro_lock_directory		= ulfs_gro_lock_directory,
   1117  1.4.2.2  rmind };
   1118  1.4.2.2  rmind 
   1119  1.4.2.2  rmind /*
   1120  1.4.2.2  rmind  * lfs_sane_rename: The hairiest vop, with the saner API.
   1121  1.4.2.2  rmind  *
   1122  1.4.2.2  rmind  * Arguments:
   1123  1.4.2.2  rmind  *
   1124  1.4.2.2  rmind  * . fdvp (from directory vnode),
   1125  1.4.2.2  rmind  * . fcnp (from component name),
   1126  1.4.2.2  rmind  * . tdvp (to directory vnode),
   1127  1.4.2.2  rmind  * . tcnp (to component name),
   1128  1.4.2.2  rmind  * . cred (credentials structure), and
   1129  1.4.2.2  rmind  * . posixly_correct (flag for behaviour if target & source link same file).
   1130  1.4.2.2  rmind  *
   1131  1.4.2.2  rmind  * fdvp and tdvp may be the same, and must be referenced and unlocked.
   1132  1.4.2.2  rmind  */
   1133  1.4.2.2  rmind static int
   1134  1.4.2.2  rmind lfs_sane_rename(
   1135  1.4.2.2  rmind     struct vnode *fdvp, struct componentname *fcnp,
   1136  1.4.2.2  rmind     struct vnode *tdvp, struct componentname *tcnp,
   1137  1.4.2.2  rmind     kauth_cred_t cred, bool posixly_correct)
   1138  1.4.2.2  rmind {
   1139  1.4.2.2  rmind 	struct ulfs_lookup_results fulr, tulr;
   1140  1.4.2.2  rmind 
   1141  1.4.2.2  rmind 	/*
   1142  1.4.2.2  rmind 	 * XXX Provisional kludge -- ulfs_lookup does not reject rename
   1143  1.4.2.2  rmind 	 * of . or .. (from or to), so we hack it here.  This is not
   1144  1.4.2.2  rmind 	 * the right place: it should be caller's responsibility to
   1145  1.4.2.2  rmind 	 * reject this case.
   1146  1.4.2.2  rmind 	 */
   1147  1.4.2.2  rmind 	KASSERT(fcnp != NULL);
   1148  1.4.2.2  rmind 	KASSERT(tcnp != NULL);
   1149  1.4.2.2  rmind 	KASSERT(fcnp != tcnp);
   1150  1.4.2.2  rmind 	KASSERT(fcnp->cn_nameptr != NULL);
   1151  1.4.2.2  rmind 	KASSERT(tcnp->cn_nameptr != NULL);
   1152  1.4.2.2  rmind 
   1153  1.4.2.2  rmind 	if ((fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT)
   1154  1.4.2.2  rmind 		return EINVAL;	/* XXX EISDIR?  */
   1155  1.4.2.2  rmind 	if ((fcnp->cn_namelen == 1) && (fcnp->cn_nameptr[0] == '.'))
   1156  1.4.2.2  rmind 		return EINVAL;
   1157  1.4.2.2  rmind 	if ((tcnp->cn_namelen == 1) && (tcnp->cn_nameptr[0] == '.'))
   1158  1.4.2.2  rmind 		return EINVAL;
   1159  1.4.2.2  rmind 
   1160  1.4.2.2  rmind 	return genfs_sane_rename(&lfs_genfs_rename_ops,
   1161  1.4.2.2  rmind 	    fdvp, fcnp, &fulr, tdvp, tcnp, &tulr,
   1162  1.4.2.2  rmind 	    cred, posixly_correct);
   1163  1.4.2.2  rmind }
   1164  1.4.2.2  rmind 
   1165  1.4.2.2  rmind /*
   1166  1.4.2.2  rmind  * lfs_rename: The hairiest vop, with the insanest API.  Defer to
   1167  1.4.2.2  rmind  * genfs_insane_rename immediately.
   1168  1.4.2.2  rmind  */
   1169  1.4.2.2  rmind int
   1170  1.4.2.2  rmind lfs_rename(void *v)
   1171  1.4.2.2  rmind {
   1172  1.4.2.2  rmind 
   1173  1.4.2.2  rmind 	return genfs_insane_rename(v, &lfs_sane_rename);
   1174  1.4.2.2  rmind }
   1175