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