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