Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_inode.c revision 1.75
      1 /*	$NetBSD: ext2fs_inode.c,v 1.75 2012/01/27 19:22:48 para Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_inode.c	8.8 (Berkeley) 10/19/94
     32  * Modified for ext2fs by Manuel Bouyer.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1997 Manuel Bouyer.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  *
     58  *	@(#)ffs_inode.c	8.8 (Berkeley) 10/19/94
     59  * Modified for ext2fs by Manuel Bouyer.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.75 2012/01/27 19:22:48 para Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/mount.h>
     68 #include <sys/proc.h>
     69 #include <sys/file.h>
     70 #include <sys/buf.h>
     71 #include <sys/vnode.h>
     72 #include <sys/kernel.h>
     73 #include <sys/kmem.h>
     74 #include <sys/trace.h>
     75 #include <sys/resourcevar.h>
     76 #include <sys/kauth.h>
     77 
     78 #include <ufs/ufs/inode.h>
     79 #include <ufs/ufs/ufsmount.h>
     80 #include <ufs/ufs/ufs_extern.h>
     81 
     82 #include <ufs/ext2fs/ext2fs.h>
     83 #include <ufs/ext2fs/ext2fs_extern.h>
     84 
     85 extern int prtactive;
     86 
     87 static int ext2fs_indirtrunc(struct inode *, daddr_t, daddr_t,
     88 				  daddr_t, int, long *);
     89 
     90 /*
     91  * Get the size of an inode.
     92  */
     93 uint64_t
     94 ext2fs_size(struct inode *ip)
     95 {
     96 	uint64_t size = ip->i_e2fs_size;
     97 
     98 	if ((ip->i_e2fs_mode & IFMT) == IFREG)
     99 		size |= (uint64_t)ip->i_e2fs_dacl << 32;
    100 	return size;
    101 }
    102 
    103 int
    104 ext2fs_setsize(struct inode *ip, uint64_t size)
    105 {
    106 	if ((ip->i_e2fs_mode & IFMT) == IFREG ||
    107 	    ip->i_e2fs_mode == 0) {
    108 		ip->i_e2fs_dacl = size >> 32;
    109 		if (size >= 0x80000000U) {
    110 			struct m_ext2fs *fs = ip->i_e2fs;
    111 
    112 			if (fs->e2fs.e2fs_rev <= E2FS_REV0) {
    113 				/* Linux automagically upgrades to REV1 here! */
    114 				return EFBIG;
    115 			}
    116 			if (!(fs->e2fs.e2fs_features_rocompat
    117 			    & EXT2F_ROCOMPAT_LARGEFILE)) {
    118 				fs->e2fs.e2fs_features_rocompat |=
    119 				    EXT2F_ROCOMPAT_LARGEFILE;
    120 				fs->e2fs_fmod = 1;
    121 			}
    122 		}
    123 	} else if (size >= 0x80000000U)
    124 		return EFBIG;
    125 
    126 	ip->i_e2fs_size = size;
    127 
    128 	return 0;
    129 }
    130 
    131 /*
    132  * Last reference to an inode.  If necessary, write or delete it.
    133  */
    134 int
    135 ext2fs_inactive(void *v)
    136 {
    137 	struct vop_inactive_args /* {
    138 		struct vnode *a_vp;
    139 		bool *a_recycle;
    140 	} */ *ap = v;
    141 	struct vnode *vp = ap->a_vp;
    142 	struct inode *ip = VTOI(vp);
    143 	int error = 0;
    144 
    145 	if (prtactive && vp->v_usecount != 0)
    146 		vprint("ext2fs_inactive: pushing active", vp);
    147 	/* Get rid of inodes related to stale file handles. */
    148 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
    149 		goto out;
    150 
    151 	error = 0;
    152 	if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
    153 		/* Defer final inode free and update to reclaim.*/
    154 		if (ext2fs_size(ip) != 0) {
    155 			error = ext2fs_truncate(vp, (off_t)0, 0, NOCRED);
    156 		}
    157 		ip->i_e2fs_dtime = time_second;
    158 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    159 		ip->i_omode = 1;
    160 	}
    161 	if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
    162 		ext2fs_update(vp, NULL, NULL, 0);
    163 	}
    164 out:
    165 	/*
    166 	 * If we are done with the inode, reclaim it
    167 	 * so that it can be reused immediately.
    168 	 */
    169 	*ap->a_recycle = (ip->i_e2fs_dtime != 0);
    170 	VOP_UNLOCK(vp);
    171 	return (error);
    172 }
    173 
    174 
    175 /*
    176  * Update the access, modified, and inode change times as specified by the
    177  * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
    178  * used to specify that the inode needs to be updated but that the times have
    179  * already been set. The access and modified times are taken from the second
    180  * and third parameters; the inode change time is always taken from the current
    181  * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk
    182  * write of the inode to complete.
    183  */
    184 int
    185 ext2fs_update(struct vnode *vp, const struct timespec *acc,
    186     const struct timespec *mod, int updflags)
    187 {
    188 	struct m_ext2fs *fs;
    189 	struct buf *bp;
    190 	struct inode *ip;
    191 	int error;
    192 	void *cp;
    193 	int flags;
    194 
    195 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    196 		return (0);
    197 	ip = VTOI(vp);
    198 	EXT2FS_ITIMES(ip, acc, mod, NULL);
    199 	if (updflags & UPDATE_CLOSE)
    200 		flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
    201 	else
    202 		flags = ip->i_flag & IN_MODIFIED;
    203 	if (flags == 0)
    204 		return (0);
    205 	fs = ip->i_e2fs;
    206 
    207 	error = bread(ip->i_devvp,
    208 			  fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    209 			  (int)fs->e2fs_bsize, NOCRED, B_MODIFY, &bp);
    210 	if (error) {
    211 		brelse(bp, 0);
    212 		return (error);
    213 	}
    214 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    215 	cp = (char *)bp->b_data +
    216 	    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
    217 	e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
    218 	if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
    219 	    (flags & IN_MODIFIED) != 0 &&
    220 	    (vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
    221 		return (bwrite(bp));
    222 	else {
    223 		bdwrite(bp);
    224 		return (0);
    225 	}
    226 }
    227 
    228 #define	SINGLE	0	/* index of single indirect block */
    229 #define	DOUBLE	1	/* index of double indirect block */
    230 #define	TRIPLE	2	/* index of triple indirect block */
    231 /*
    232  * Truncate the inode oip to at most length size, freeing the
    233  * disk blocks.
    234  */
    235 int
    236 ext2fs_truncate(struct vnode *ovp, off_t length, int ioflag,
    237     kauth_cred_t cred)
    238 {
    239 	daddr_t lastblock;
    240 	struct inode *oip = VTOI(ovp);
    241 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    242 	/* XXX ondisk32 */
    243 	int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    244 	struct m_ext2fs *fs;
    245 	int offset, size, level;
    246 	long count, blocksreleased = 0;
    247 	int i, nblocks;
    248 	int error, allerror = 0;
    249 	off_t osize;
    250 	int sync;
    251 	struct ufsmount *ump = oip->i_ump;
    252 
    253 	if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
    254 	    ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
    255 		return 0;
    256 	}
    257 
    258 	if (length < 0)
    259 		return (EINVAL);
    260 
    261 	if (ovp->v_type == VLNK &&
    262 	    (ext2fs_size(oip) < ump->um_maxsymlinklen ||
    263 	     (ump->um_maxsymlinklen == 0 && oip->i_e2fs_nblock == 0))) {
    264 		KDASSERT(length == 0);
    265 		memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
    266 			(u_int)ext2fs_size(oip));
    267 		(void)ext2fs_setsize(oip, 0);
    268 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    269 		return (ext2fs_update(ovp, NULL, NULL, 0));
    270 	}
    271 	if (ext2fs_size(oip) == length) {
    272 		/* still do a uvm_vnp_setsize() as writesize may be larger */
    273 		uvm_vnp_setsize(ovp, length);
    274 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    275 		return (ext2fs_update(ovp, NULL, NULL, 0));
    276 	}
    277 	fs = oip->i_e2fs;
    278 	if (length > ump->um_maxfilesize)
    279 		return (EFBIG);
    280 
    281 	osize = ext2fs_size(oip);
    282 
    283 	/*
    284 	 * Lengthen the size of the file. We must ensure that the
    285 	 * last byte of the file is allocated. Since the smallest
    286 	 * value of osize is 0, length will be at least 1.
    287 	 */
    288 	if (osize < length) {
    289 		uvm_vnp_setwritesize(ovp, length);
    290 		error = ufs_balloc_range(ovp, length - 1, 1, cred,
    291 		    ioflag & IO_SYNC ? B_SYNC : 0);
    292 		if (error) {
    293 			(void) ext2fs_truncate(ovp, osize, ioflag & IO_SYNC,
    294 			    cred);
    295 			return (error);
    296 		}
    297 		uvm_vnp_setsize(ovp, length);
    298 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    299 		KASSERT(error  || ovp->v_size == ext2fs_size(oip));
    300 		return (ext2fs_update(ovp, NULL, NULL, 0));
    301 	}
    302 	/*
    303 	 * Shorten the size of the file. If the file is not being
    304 	 * truncated to a block boundry, the contents of the
    305 	 * partial block following the end of the file must be
    306 	 * zero'ed in case it ever become accessible again because
    307 	 * of subsequent file growth.
    308 	 */
    309 	offset = blkoff(fs, length);
    310 	if (offset != 0) {
    311 		size = fs->e2fs_bsize;
    312 
    313 		/* XXXUBC we should handle more than just VREG */
    314 		ubc_zerorange(&ovp->v_uobj, length, size - offset,
    315 		    UBC_UNMAP_FLAG(ovp));
    316 	}
    317 	(void)ext2fs_setsize(oip, length);
    318 	uvm_vnp_setsize(ovp, length);
    319 	/*
    320 	 * Calculate index into inode's block list of
    321 	 * last direct and indirect blocks (if any)
    322 	 * which we want to keep.  Lastblock is -1 when
    323 	 * the file is truncated to 0.
    324 	 */
    325 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
    326 	lastiblock[SINGLE] = lastblock - NDADDR;
    327 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    328 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    329 	nblocks = btodb(fs->e2fs_bsize);
    330 	/*
    331 	 * Update file and block pointers on disk before we start freeing
    332 	 * blocks.  If we crash before free'ing blocks below, the blocks
    333 	 * will be returned to the free list.  lastiblock values are also
    334 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
    335 	 */
    336 	memcpy((void *)oldblks, (void *)&oip->i_e2fs_blocks[0], sizeof oldblks);
    337 	sync = 0;
    338 	for (level = TRIPLE; level >= SINGLE; level--) {
    339 		if (lastiblock[level] < 0 && oldblks[NDADDR + level] != 0) {
    340 			sync = 1;
    341 			oip->i_e2fs_blocks[NDADDR + level] = 0;
    342 			lastiblock[level] = -1;
    343 		}
    344 	}
    345 	for (i = 0; i < NDADDR; i++) {
    346 		if (i > lastblock && oldblks[i] != 0) {
    347 			sync = 1;
    348 			oip->i_e2fs_blocks[i] = 0;
    349 		}
    350 	}
    351 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    352 	if (sync) {
    353 		error = ext2fs_update(ovp, NULL, NULL, UPDATE_WAIT);
    354 		if (error && !allerror)
    355 			allerror = error;
    356 	}
    357 
    358 	/*
    359 	 * Having written the new inode to disk, save its new configuration
    360 	 * and put back the old block pointers long enough to process them.
    361 	 * Note that we save the new block configuration so we can check it
    362 	 * when we are done.
    363 	 */
    364 	memcpy((void *)newblks, (void *)&oip->i_e2fs_blocks[0], sizeof newblks);
    365 	memcpy((void *)&oip->i_e2fs_blocks[0], (void *)oldblks, sizeof oldblks);
    366 
    367 	(void)ext2fs_setsize(oip, osize);
    368 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    369 	if (error && !allerror)
    370 		allerror = error;
    371 
    372 	/*
    373 	 * Indirect blocks first.
    374 	 */
    375 	indir_lbn[SINGLE] = -NDADDR;
    376 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
    377 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    378 	for (level = TRIPLE; level >= SINGLE; level--) {
    379 		/* XXX ondisk32 */
    380 		bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
    381 		if (bn != 0) {
    382 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
    383 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    384 			if (error)
    385 				allerror = error;
    386 			blocksreleased += count;
    387 			if (lastiblock[level] < 0) {
    388 				oip->i_e2fs_blocks[NDADDR + level] = 0;
    389 				ext2fs_blkfree(oip, bn);
    390 				blocksreleased += nblocks;
    391 			}
    392 		}
    393 		if (lastiblock[level] >= 0)
    394 			goto done;
    395 	}
    396 
    397 	/*
    398 	 * All whole direct blocks or frags.
    399 	 */
    400 	for (i = NDADDR - 1; i > lastblock; i--) {
    401 		/* XXX ondisk32 */
    402 		bn = fs2h32(oip->i_e2fs_blocks[i]);
    403 		if (bn == 0)
    404 			continue;
    405 		oip->i_e2fs_blocks[i] = 0;
    406 		ext2fs_blkfree(oip, bn);
    407 		blocksreleased += btodb(fs->e2fs_bsize);
    408 	}
    409 
    410 done:
    411 #ifdef DIAGNOSTIC
    412 	for (level = SINGLE; level <= TRIPLE; level++)
    413 		if (newblks[NDADDR + level] !=
    414 		    oip->i_e2fs_blocks[NDADDR + level])
    415 			panic("ext2fs_truncate1");
    416 	for (i = 0; i < NDADDR; i++)
    417 		if (newblks[i] != oip->i_e2fs_blocks[i])
    418 			panic("ext2fs_truncate2");
    419 	if (length == 0 &&
    420 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
    421 	     !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    422 		panic("ext2fs_truncate3");
    423 #endif /* DIAGNOSTIC */
    424 	/*
    425 	 * Put back the real size.
    426 	 */
    427 	(void)ext2fs_setsize(oip, length);
    428 	oip->i_e2fs_nblock -= blocksreleased;
    429 	oip->i_flag |= IN_CHANGE;
    430 	KASSERT(ovp->v_type != VREG || ovp->v_size == ext2fs_size(oip));
    431 	return (allerror);
    432 }
    433 
    434 /*
    435  * Release blocks associated with the inode ip and stored in the indirect
    436  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    437  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    438  * and recursive calls to indirtrunc must be used to cleanse other indirect
    439  * blocks.
    440  *
    441  * NB: triple indirect blocks are untested.
    442  */
    443 static int
    444 ext2fs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
    445 		int level, long *countp)
    446 {
    447 	int i;
    448 	struct buf *bp;
    449 	struct m_ext2fs *fs = ip->i_e2fs;
    450 	int32_t *bap;	/* XXX ondisk32 */
    451 	struct vnode *vp;
    452 	daddr_t nb, nlbn, last;
    453 	int32_t *copy = NULL;	/* XXX ondisk32 */
    454 	long blkcount, factor;
    455 	int nblocks, blocksreleased = 0;
    456 	int error = 0, allerror = 0;
    457 
    458 	/*
    459 	 * Calculate index in current block of last
    460 	 * block to be kept.  -1 indicates the entire
    461 	 * block so we need not calculate the index.
    462 	 */
    463 	factor = 1;
    464 	for (i = SINGLE; i < level; i++)
    465 		factor *= NINDIR(fs);
    466 	last = lastbn;
    467 	if (lastbn > 0)
    468 		last /= factor;
    469 	nblocks = btodb(fs->e2fs_bsize);
    470 	/*
    471 	 * Get buffer of block pointers, zero those entries corresponding
    472 	 * to blocks to be free'd, and update on disk copy first.  Since
    473 	 * double(triple) indirect before single(double) indirect, calls
    474 	 * to bmap on these blocks will fail.  However, we already have
    475 	 * the on disk address, so we have to set the b_blkno field
    476 	 * explicitly instead of letting bread do everything for us.
    477 	 */
    478 	vp = ITOV(ip);
    479 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
    480 	if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
    481 		/* Braces must be here in case trace evaluates to nothing. */
    482 		trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
    483 	} else {
    484 		trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
    485 		curlwp->l_ru.ru_inblock++;	/* pay for read */
    486 		bp->b_flags |= B_READ;
    487 		if (bp->b_bcount > bp->b_bufsize)
    488 			panic("ext2fs_indirtrunc: bad buffer size");
    489 		bp->b_blkno = dbn;
    490 		VOP_STRATEGY(vp, bp);
    491 		error = biowait(bp);
    492 	}
    493 	if (error) {
    494 		brelse(bp, 0);
    495 		*countp = 0;
    496 		return (error);
    497 	}
    498 
    499 	bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    500 	if (lastbn >= 0) {
    501 		/* XXX ondisk32 */
    502 		copy = kmem_alloc(fs->e2fs_bsize, KM_SLEEP);
    503 		memcpy((void *)copy, (void *)bap, (u_int)fs->e2fs_bsize);
    504 		memset((void *)&bap[last + 1], 0,
    505 			(u_int)(NINDIR(fs) - (last + 1)) * sizeof (uint32_t));
    506 		error = bwrite(bp);
    507 		if (error)
    508 			allerror = error;
    509 		bap = copy;
    510 	}
    511 
    512 	/*
    513 	 * Recursively free totally unused blocks.
    514 	 */
    515 	for (i = NINDIR(fs) - 1,
    516 		nlbn = lbn + 1 - i * factor; i > last;
    517 		i--, nlbn += factor) {
    518 		/* XXX ondisk32 */
    519 		nb = fs2h32(bap[i]);
    520 		if (nb == 0)
    521 			continue;
    522 		if (level > SINGLE) {
    523 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    524 						   (daddr_t)-1, level - 1,
    525 						   &blkcount);
    526 			if (error)
    527 				allerror = error;
    528 			blocksreleased += blkcount;
    529 		}
    530 		ext2fs_blkfree(ip, nb);
    531 		blocksreleased += nblocks;
    532 	}
    533 
    534 	/*
    535 	 * Recursively free last partial block.
    536 	 */
    537 	if (level > SINGLE && lastbn >= 0) {
    538 		last = lastbn % factor;
    539 		/* XXX ondisk32 */
    540 		nb = fs2h32(bap[i]);
    541 		if (nb != 0) {
    542 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    543 						   last, level - 1, &blkcount);
    544 			if (error)
    545 				allerror = error;
    546 			blocksreleased += blkcount;
    547 		}
    548 	}
    549 
    550 	if (copy != NULL) {
    551 		kmem_free(copy, fs->e2fs_bsize);
    552 	} else {
    553 		brelse(bp, BC_INVAL);
    554 	}
    555 
    556 	*countp = blocksreleased;
    557 	return (allerror);
    558 }
    559