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