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