Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_inode.c revision 1.44
      1 /*	$NetBSD: ext2fs_inode.c,v 1.44 2005/02/09 23:02:10 ws 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.44 2005/02/09 23:02:10 ws 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 __P((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(v)
    140 	void *v;
    141 {
    142 	struct vop_inactive_args /* {
    143 		struct vnode *a_vp;
    144 		struct proc *a_p;
    145 	} */ *ap = v;
    146 	struct vnode *vp = ap->a_vp;
    147 	struct inode *ip = VTOI(vp);
    148 	struct mount *mp;
    149 	struct proc *p = ap->a_p;
    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 = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
    164 		}
    165 		TIMEVAL_TO_TIMESPEC(&time, &ts);
    166 		ip->i_e2fs_dtime = ts.tv_sec;
    167 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    168 		VOP_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 		VOP_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, p);
    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(v)
    199 	void *v;
    200 {
    201 	struct vop_update_args /* {
    202 		struct vnode *a_vp;
    203 		struct timespec *a_access;
    204 		struct timespec *a_modify;
    205 		int a_flags;
    206 	} */ *ap = v;
    207 	struct m_ext2fs *fs;
    208 	struct buf *bp;
    209 	struct inode *ip;
    210 	int error;
    211 	struct timespec ts;
    212 	caddr_t cp;
    213 	int flags;
    214 
    215 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
    216 		return (0);
    217 	ip = VTOI(ap->a_vp);
    218 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    219 	EXT2FS_ITIMES(ip,
    220 	    ap->a_access ? ap->a_access : &ts,
    221 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    222 	if (ap->a_flags & UPDATE_CLOSE)
    223 		flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
    224 	else
    225 		flags = ip->i_flag & IN_MODIFIED;
    226 	if (flags == 0)
    227 		return (0);
    228 	fs = ip->i_e2fs;
    229 
    230 	error = bread(ip->i_devvp,
    231 			  fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    232 			  (int)fs->e2fs_bsize, NOCRED, &bp);
    233 	if (error) {
    234 		brelse(bp);
    235 		return (error);
    236 	}
    237 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    238 	cp = (caddr_t)bp->b_data +
    239 	    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
    240 	e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
    241 	if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
    242 	    (flags & IN_MODIFIED) != 0 &&
    243 	    (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
    244 		return (bwrite(bp));
    245 	else {
    246 		bdwrite(bp);
    247 		return (0);
    248 	}
    249 }
    250 
    251 #define	SINGLE	0	/* index of single indirect block */
    252 #define	DOUBLE	1	/* index of double indirect block */
    253 #define	TRIPLE	2	/* index of triple indirect block */
    254 /*
    255  * Truncate the inode oip to at most length size, freeing the
    256  * disk blocks.
    257  */
    258 int
    259 ext2fs_truncate(v)
    260 	void *v;
    261 {
    262 	struct vop_truncate_args /* {
    263 		struct vnode *a_vp;
    264 		off_t a_length;
    265 		int a_flags;
    266 		struct ucred *a_cred;
    267 		struct proc *a_p;
    268 	} */ *ap = v;
    269 	struct vnode *ovp = ap->a_vp;
    270 	daddr_t lastblock;
    271 	struct inode *oip = VTOI(ovp);
    272 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    273 	/* XXX ondisk32 */
    274 	int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    275 	off_t length = ap->a_length;
    276 	struct m_ext2fs *fs;
    277 	int offset, size, level;
    278 	long count, blocksreleased = 0;
    279 	int i, ioflag, nblocks;
    280 	int error, allerror = 0;
    281 	off_t osize;
    282 	int sync;
    283 	struct ufsmount *ump = oip->i_ump;
    284 
    285 	if (length < 0)
    286 		return (EINVAL);
    287 
    288 	if (ovp->v_type == VLNK &&
    289 	    (ext2fs_size(oip) < ump->um_maxsymlinklen ||
    290 	     (ump->um_maxsymlinklen == 0 && oip->i_e2fs_nblock == 0))) {
    291 		KDASSERT(length == 0);
    292 		memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
    293 			(u_int)ext2fs_size(oip));
    294 		(void)ext2fs_setsize(oip, 0);
    295 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    296 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    297 	}
    298 	if (ext2fs_size(oip) == length) {
    299 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    300 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    301 	}
    302 	fs = oip->i_e2fs;
    303 	if (length > ump->um_maxfilesize)
    304 		return (EFBIG);
    305 
    306 	osize = ext2fs_size(oip);
    307 	ioflag = ap->a_flags;
    308 
    309 	/*
    310 	 * Lengthen the size of the file. We must ensure that the
    311 	 * last byte of the file is allocated. Since the smallest
    312 	 * value of osize is 0, length will be at least 1.
    313 	 */
    314 	if (osize < length) {
    315 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    316 		    ioflag & IO_SYNC ? B_SYNC : 0);
    317 		if (error) {
    318 			(void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
    319 			    ap->a_cred, ap->a_p);
    320 			return (error);
    321 		}
    322 		uvm_vnp_setsize(ovp, length);
    323 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    324 		KASSERT(error || ovp->v_size == oip->i_size);
    325 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    326 	}
    327 	/*
    328 	 * Shorten the size of the file. If the file is not being
    329 	 * truncated to a block boundry, the contents of the
    330 	 * partial block following the end of the file must be
    331 	 * zero'ed in case it ever become accessible again because
    332 	 * of subsequent file growth.
    333 	 */
    334 	offset = blkoff(fs, length);
    335 	if (offset != 0) {
    336 		size = fs->e2fs_bsize;
    337 
    338 		/* XXXUBC we should handle more than just VREG */
    339 		uvm_vnp_zerorange(ovp, length, size - offset);
    340 	}
    341 	(void)ext2fs_setsize(oip, length);
    342 	uvm_vnp_setsize(ovp, length);
    343 	/*
    344 	 * Calculate index into inode's block list of
    345 	 * last direct and indirect blocks (if any)
    346 	 * which we want to keep.  Lastblock is -1 when
    347 	 * the file is truncated to 0.
    348 	 */
    349 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
    350 	lastiblock[SINGLE] = lastblock - NDADDR;
    351 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    352 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    353 	nblocks = btodb(fs->e2fs_bsize);
    354 	/*
    355 	 * Update file and block pointers on disk before we start freeing
    356 	 * blocks.  If we crash before free'ing blocks below, the blocks
    357 	 * will be returned to the free list.  lastiblock values are also
    358 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
    359 	 */
    360 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
    361 	sync = 0;
    362 	for (level = TRIPLE; level >= SINGLE; level--) {
    363 		if (lastiblock[level] < 0 && oldblks[NDADDR + level] != 0) {
    364 			sync = 1;
    365 			oip->i_e2fs_blocks[NDADDR + level] = 0;
    366 			lastiblock[level] = -1;
    367 		}
    368 	}
    369 	for (i = 0; i < NDADDR; i++) {
    370 		if (i > lastblock && oldblks[i] != 0) {
    371 			sync = 1;
    372 			oip->i_e2fs_blocks[i] = 0;
    373 		}
    374 	}
    375 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    376 	if (sync) {
    377 		error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    378 		if (error && !allerror)
    379 			allerror = error;
    380 	}
    381 
    382 	/*
    383 	 * Having written the new inode to disk, save its new configuration
    384 	 * and put back the old block pointers long enough to process them.
    385 	 * Note that we save the new block configuration so we can check it
    386 	 * when we are done.
    387 	 */
    388 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
    389 	memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
    390 
    391 	(void)ext2fs_setsize(oip, osize);
    392 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    393 	if (error && !allerror)
    394 		allerror = error;
    395 
    396 	/*
    397 	 * Indirect blocks first.
    398 	 */
    399 	indir_lbn[SINGLE] = -NDADDR;
    400 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
    401 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    402 	for (level = TRIPLE; level >= SINGLE; level--) {
    403 		/* XXX ondisk32 */
    404 		bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
    405 		if (bn != 0) {
    406 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
    407 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    408 			if (error)
    409 				allerror = error;
    410 			blocksreleased += count;
    411 			if (lastiblock[level] < 0) {
    412 				oip->i_e2fs_blocks[NDADDR + level] = 0;
    413 				ext2fs_blkfree(oip, bn);
    414 				blocksreleased += nblocks;
    415 			}
    416 		}
    417 		if (lastiblock[level] >= 0)
    418 			goto done;
    419 	}
    420 
    421 	/*
    422 	 * All whole direct blocks or frags.
    423 	 */
    424 	for (i = NDADDR - 1; i > lastblock; i--) {
    425 		/* XXX ondisk32 */
    426 		bn = fs2h32(oip->i_e2fs_blocks[i]);
    427 		if (bn == 0)
    428 			continue;
    429 		oip->i_e2fs_blocks[i] = 0;
    430 		ext2fs_blkfree(oip, bn);
    431 		blocksreleased += btodb(fs->e2fs_bsize);
    432 	}
    433 
    434 done:
    435 #ifdef DIAGNOSTIC
    436 	for (level = SINGLE; level <= TRIPLE; level++)
    437 		if (newblks[NDADDR + level] !=
    438 		    oip->i_e2fs_blocks[NDADDR + level])
    439 			panic("ext2fs_truncate1");
    440 	for (i = 0; i < NDADDR; i++)
    441 		if (newblks[i] != oip->i_e2fs_blocks[i])
    442 			panic("ext2fs_truncate2");
    443 	if (length == 0 &&
    444 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
    445 	     !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    446 		panic("ext2fs_truncate3");
    447 #endif /* DIAGNOSTIC */
    448 	/*
    449 	 * Put back the real size.
    450 	 */
    451 	(void)ext2fs_setsize(oip, length);
    452 	oip->i_e2fs_nblock -= blocksreleased;
    453 	oip->i_flag |= IN_CHANGE;
    454 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
    455 	return (allerror);
    456 }
    457 
    458 /*
    459  * Release blocks associated with the inode ip and stored in the indirect
    460  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    461  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    462  * and recursive calls to indirtrunc must be used to cleanse other indirect
    463  * blocks.
    464  *
    465  * NB: triple indirect blocks are untested.
    466  */
    467 static int
    468 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    469 	struct inode *ip;
    470 	daddr_t lbn, lastbn;
    471 	daddr_t dbn;
    472 	int level;
    473 	long *countp;
    474 {
    475 	int i;
    476 	struct buf *bp;
    477 	struct m_ext2fs *fs = ip->i_e2fs;
    478 	int32_t *bap;	/* XXX ondisk32 */
    479 	struct vnode *vp;
    480 	daddr_t nb, nlbn, last;
    481 	int32_t *copy = NULL;	/* XXX ondisk32 */
    482 	long blkcount, factor;
    483 	int nblocks, blocksreleased = 0;
    484 	int error = 0, allerror = 0;
    485 
    486 	/*
    487 	 * Calculate index in current block of last
    488 	 * block to be kept.  -1 indicates the entire
    489 	 * block so we need not calculate the index.
    490 	 */
    491 	factor = 1;
    492 	for (i = SINGLE; i < level; i++)
    493 		factor *= NINDIR(fs);
    494 	last = lastbn;
    495 	if (lastbn > 0)
    496 		last /= factor;
    497 	nblocks = btodb(fs->e2fs_bsize);
    498 	/*
    499 	 * Get buffer of block pointers, zero those entries corresponding
    500 	 * to blocks to be free'd, and update on disk copy first.  Since
    501 	 * double(triple) indirect before single(double) indirect, calls
    502 	 * to bmap on these blocks will fail.  However, we already have
    503 	 * the on disk address, so we have to set the b_blkno field
    504 	 * explicitly instead of letting bread do everything for us.
    505 	 */
    506 	vp = ITOV(ip);
    507 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
    508 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    509 		/* Braces must be here in case trace evaluates to nothing. */
    510 		trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
    511 	} else {
    512 		trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
    513 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    514 		bp->b_flags |= B_READ;
    515 		if (bp->b_bcount > bp->b_bufsize)
    516 			panic("ext2fs_indirtrunc: bad buffer size");
    517 		bp->b_blkno = dbn;
    518 		VOP_STRATEGY(vp, bp);
    519 		error = biowait(bp);
    520 	}
    521 	if (error) {
    522 		brelse(bp);
    523 		*countp = 0;
    524 		return (error);
    525 	}
    526 
    527 	bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
    528 	if (lastbn >= 0) {
    529 		/* XXX ondisk32 */
    530 		MALLOC(copy, int32_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
    531 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
    532 		memset((caddr_t)&bap[last + 1], 0,
    533 			(u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
    534 		error = bwrite(bp);
    535 		if (error)
    536 			allerror = error;
    537 		bap = copy;
    538 	}
    539 
    540 	/*
    541 	 * Recursively free totally unused blocks.
    542 	 */
    543 	for (i = NINDIR(fs) - 1,
    544 		nlbn = lbn + 1 - i * factor; i > last;
    545 		i--, nlbn += factor) {
    546 		/* XXX ondisk32 */
    547 		nb = fs2h32(bap[i]);
    548 		if (nb == 0)
    549 			continue;
    550 		if (level > SINGLE) {
    551 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    552 						   (daddr_t)-1, level - 1,
    553 						   &blkcount);
    554 			if (error)
    555 				allerror = error;
    556 			blocksreleased += blkcount;
    557 		}
    558 		ext2fs_blkfree(ip, nb);
    559 		blocksreleased += nblocks;
    560 	}
    561 
    562 	/*
    563 	 * Recursively free last partial block.
    564 	 */
    565 	if (level > SINGLE && lastbn >= 0) {
    566 		last = lastbn % factor;
    567 		/* XXX ondisk32 */
    568 		nb = fs2h32(bap[i]);
    569 		if (nb != 0) {
    570 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    571 						   last, level - 1, &blkcount);
    572 			if (error)
    573 				allerror = error;
    574 			blocksreleased += blkcount;
    575 		}
    576 	}
    577 
    578 	if (copy != NULL) {
    579 		FREE(copy, M_TEMP);
    580 	} else {
    581 		bp->b_flags |= B_INVAL;
    582 		brelse(bp);
    583 	}
    584 
    585 	*countp = blocksreleased;
    586 	return (allerror);
    587 }
    588