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