Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.35.2.2
      1 /*	$NetBSD: ffs_inode.c,v 1.35.2.2 2001/09/30 13:17:24 he 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ffs_inode.c	8.13 (Berkeley) 4/21/95
     36  */
     37 
     38 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_ffs.h"
     40 #include "opt_quota.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/mount.h>
     46 #include <sys/proc.h>
     47 #include <sys/file.h>
     48 #include <sys/buf.h>
     49 #include <sys/vnode.h>
     50 #include <sys/kernel.h>
     51 #include <sys/malloc.h>
     52 #include <sys/trace.h>
     53 #include <sys/resourcevar.h>
     54 
     55 #include <vm/vm.h>
     56 
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <ufs/ufs/quota.h>
     60 #include <ufs/ufs/inode.h>
     61 #include <ufs/ufs/ufsmount.h>
     62 #include <ufs/ufs/ufs_extern.h>
     63 #include <ufs/ufs/ufs_bswap.h>
     64 
     65 #include <ufs/ffs/fs.h>
     66 #include <ufs/ffs/ffs_extern.h>
     67 
     68 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
     69 			       ufs_daddr_t, int, long *));
     70 
     71 /*
     72  * Update the access, modified, and inode change times as specified
     73  * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
     74  * The IN_MODIFIED flag is used to specify that the inode needs to be
     75  * updated but that the times have already been set. The access
     76  * and modified times are taken from the second and third parameters;
     77  * the inode change time is always taken from the current time. If
     78  * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
     79  * softupdates, then wait for the disk write of the inode to complete.
     80  */
     81 
     82 int
     83 ffs_update(v)
     84 	void *v;
     85 {
     86 	struct vop_update_args /* {
     87 		struct vnode *a_vp;
     88 		struct timespec *a_access;
     89 		struct timespec *a_modify;
     90 		int a_flags;
     91 	} */ *ap = v;
     92 	struct fs *fs;
     93 	struct buf *bp;
     94 	struct inode *ip;
     95 	int error;
     96 	struct timespec ts;
     97 	caddr_t cp;
     98 	int waitfor, flags;
     99 
    100 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
    101 		return (0);
    102 	ip = VTOI(ap->a_vp);
    103 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    104 	FFS_ITIMES(ip,
    105 	    ap->a_access ? ap->a_access : &ts,
    106 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    107 	flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
    108 	if (flags == 0)
    109 		return (0);
    110 	fs = ip->i_fs;
    111 
    112 	if ((flags & IN_MODIFIED) != 0 &&
    113 	    (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
    114 		waitfor = ap->a_flags & UPDATE_WAIT;
    115 		if ((ap->a_flags & UPDATE_DIROP) && !DOINGSOFTDEP(ap->a_vp))
    116 			waitfor |= UPDATE_WAIT;
    117 	} else
    118 		waitfor = 0;
    119 
    120 	/*
    121 	 * Ensure that uid and gid are correct. This is a temporary
    122 	 * fix until fsck has been changed to do the update.
    123 	 */
    124 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    125 		ip->i_din.ffs_din.di_ouid = ip->i_ffs_uid;	/* XXX */
    126 		ip->i_din.ffs_din.di_ogid = ip->i_ffs_gid;	/* XXX */
    127 	}							/* XXX */
    128 	error = bread(ip->i_devvp,
    129 		      fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    130 		      (int)fs->fs_bsize, NOCRED, &bp);
    131 	if (error) {
    132 		brelse(bp);
    133 		return (error);
    134 	}
    135 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    136 	if (DOINGSOFTDEP(ap->a_vp))
    137 		softdep_update_inodeblock(ip, bp, waitfor);
    138 	else if (ip->i_ffs_effnlink != ip->i_ffs_nlink)
    139 		panic("ffs_update: bad link cnt");
    140 	cp = (caddr_t)bp->b_data +
    141 	    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
    142 #ifdef FFS_EI
    143 	if (UFS_FSNEEDSWAP(fs))
    144 		ffs_dinode_swap(&ip->i_din.ffs_din, (struct dinode *)cp);
    145 	else
    146 #endif
    147 		memcpy(cp, &ip->i_din.ffs_din, DINODE_SIZE);
    148 	if (waitfor) {
    149 		return (bwrite(bp));
    150 	} else {
    151 		bdwrite(bp);
    152 		return (0);
    153 	}
    154 }
    155 
    156 #define	SINGLE	0	/* index of single indirect block */
    157 #define	DOUBLE	1	/* index of double indirect block */
    158 #define	TRIPLE	2	/* index of triple indirect block */
    159 /*
    160  * Truncate the inode oip to at most length size, freeing the
    161  * disk blocks.
    162  */
    163 int
    164 ffs_truncate(v)
    165 	void *v;
    166 {
    167 	struct vop_truncate_args /* {
    168 		struct vnode *a_vp;
    169 		off_t a_length;
    170 		int a_flags;
    171 		struct ucred *a_cred;
    172 		struct proc *a_p;
    173 	} */ *ap = v;
    174 	struct vnode *ovp = ap->a_vp;
    175 	ufs_daddr_t lastblock;
    176 	struct inode *oip;
    177 	ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
    178 	ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    179 	off_t length = ap->a_length;
    180 	struct fs *fs;
    181 	struct buf *bp;
    182 	int offset, size, level;
    183 	long count, nblocks, blocksreleased = 0;
    184 	int i;
    185 	int aflags, error, allerror = 0;
    186 	off_t osize;
    187 
    188 	if (length < 0)
    189 		return (EINVAL);
    190 	oip = VTOI(ovp);
    191 #if 1
    192 	/*
    193 	 * XXX. Was in Kirk's patches. Is it good behavior to just
    194 	 * return and not update modification times?
    195 	 */
    196 	if (oip->i_ffs_size == length)
    197 		return (0);
    198 #endif
    199 	if (ovp->v_type == VLNK &&
    200 	    (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
    201 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    202 	      oip->i_din.ffs_din.di_blocks == 0))) {
    203 #ifdef DIAGNOSTIC
    204 		if (length != 0)
    205 			panic("ffs_truncate: partial truncate of symlink");
    206 #endif
    207 		memset((char *)&oip->i_ffs_shortlink, 0, (u_int)oip->i_ffs_size);
    208 		oip->i_ffs_size = 0;
    209 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    210 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
    211 	}
    212 	if (oip->i_ffs_size == length) {
    213 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    214 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    215 	}
    216 #ifdef QUOTA
    217 	if ((error = getinoquota(oip)) != 0)
    218 		return (error);
    219 #endif
    220 	fs = oip->i_fs;
    221 	osize = oip->i_ffs_size;
    222 	ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
    223 
    224 	if (DOINGSOFTDEP(ovp)) {
    225 		uvm_vnp_setsize(ovp, length);
    226 		if (ovp->v_usecount)	/* can't be cached if usecount=0 */
    227 			(void) uvm_vnp_uncache(ovp);
    228 		if (length > 0) {
    229 			/*
    230 			 * If a file is only partially truncated, then
    231 			 * we have to clean up the data structures
    232 			 * describing the allocation past the truncation
    233 			 * point. Finding and deallocating those structures
    234 			 * is a lot of work. Since partial truncation occurs
    235 			 * rarely, we solve the problem by syncing the file
    236 			 * so that it will have no data structures left.
    237 			 */
    238 			if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
    239 			    0, 0, ap->a_p)) != 0)
    240 				return (error);
    241 		} else {
    242 #ifdef QUOTA
    243  			(void) chkdq(oip, -oip->i_ffs_blocks, NOCRED, 0);
    244 #endif
    245 			softdep_setup_freeblocks(oip, length);
    246 			(void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
    247 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    248 			return (VOP_UPDATE(ovp, NULL, NULL, 0));
    249 		}
    250 	}
    251 	/*
    252 	 * Lengthen the size of the file. We must ensure that the
    253 	 * last byte of the file is allocated. Since the smallest
    254 	 * value of osize is 0, length will be at least 1.
    255 	 */
    256 	if (osize < length) {
    257 		if (length > fs->fs_maxfilesize)
    258 			return (EFBIG);
    259 		aflags = B_CLRBUF;
    260 		if (ap->a_flags & IO_SYNC)
    261 			aflags |= B_SYNC;
    262 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
    263 		if (error)
    264 			return (error);
    265 		oip->i_ffs_size = length;
    266 		uvm_vnp_setsize(ovp, length);
    267 		(void) uvm_vnp_uncache(ovp);
    268 		if (aflags & B_SYNC)
    269 			bwrite(bp);
    270 		else
    271 			bawrite(bp);
    272 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    273 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
    274 	}
    275 	/*
    276 	 * Shorten the size of the file. If the file is not being
    277 	 * truncated to a block boundary, the contents of the
    278 	 * partial block following the end of the file must be
    279 	 * zero'ed in case it ever becomes accessible again because
    280 	 * of subsequent file growth. Directories however are not
    281 	 * zero'ed as they should grow back initialized to empty.
    282 	 */
    283 	offset = blkoff(fs, length);
    284 	if (offset == 0) {
    285 		oip->i_ffs_size = length;
    286 	} else {
    287 		lbn = lblkno(fs, length);
    288 		aflags = B_CLRBUF;
    289 		if (ap->a_flags & IO_SYNC)
    290 			aflags |= B_SYNC;
    291 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
    292 		if (error)
    293 			return (error);
    294 		oip->i_ffs_size = length;
    295 		size = blksize(fs, oip, lbn);
    296 		(void) uvm_vnp_uncache(ovp);
    297 		if (ovp->v_type != VDIR)
    298 			memset((char *)bp->b_data + offset, 0,
    299 			       (u_int)(size - offset));
    300 		allocbuf(bp, size);
    301 		if (aflags & B_SYNC)
    302 			bwrite(bp);
    303 		else
    304 			bawrite(bp);
    305 	}
    306 	uvm_vnp_setsize(ovp, length);
    307 	/*
    308 	 * Calculate index into inode's block list of
    309 	 * last direct and indirect blocks (if any)
    310 	 * which we want to keep.  Lastblock is -1 when
    311 	 * the file is truncated to 0.
    312 	 */
    313 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    314 	lastiblock[SINGLE] = lastblock - NDADDR;
    315 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    316 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    317 	nblocks = btodb(fs->fs_bsize);
    318 	/*
    319 	 * Update file and block pointers on disk before we start freeing
    320 	 * blocks.  If we crash before free'ing blocks below, the blocks
    321 	 * will be returned to the free list.  lastiblock values are also
    322 	 * normalized to -1 for calls to ffs_indirtrunc below.
    323 	 */
    324 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
    325 	for (level = TRIPLE; level >= SINGLE; level--)
    326 		if (lastiblock[level] < 0) {
    327 			oip->i_ffs_ib[level] = 0;
    328 			lastiblock[level] = -1;
    329 		}
    330 	for (i = NDADDR - 1; i > lastblock; i--)
    331 		oip->i_ffs_db[i] = 0;
    332 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    333 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    334 	if (error && !allerror)
    335 		allerror = error;
    336 
    337 	/*
    338 	 * Having written the new inode to disk, save its new configuration
    339 	 * and put back the old block pointers long enough to process them.
    340 	 * Note that we save the new block configuration so we can check it
    341 	 * when we are done.
    342 	 */
    343 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
    344 	memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
    345 	oip->i_ffs_size = osize;
    346 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    347 	if (error && !allerror)
    348 		allerror = error;
    349 
    350 	/*
    351 	 * Indirect blocks first.
    352 	 */
    353 	indir_lbn[SINGLE] = -NDADDR;
    354 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    355 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    356 	for (level = TRIPLE; level >= SINGLE; level--) {
    357 		bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
    358 		if (bn != 0) {
    359 			error = ffs_indirtrunc(oip, indir_lbn[level],
    360 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    361 			if (error)
    362 				allerror = error;
    363 			blocksreleased += count;
    364 			if (lastiblock[level] < 0) {
    365 				oip->i_ffs_ib[level] = 0;
    366 				ffs_blkfree(oip, bn, fs->fs_bsize);
    367 				blocksreleased += nblocks;
    368 			}
    369 		}
    370 		if (lastiblock[level] >= 0)
    371 			goto done;
    372 	}
    373 
    374 	/*
    375 	 * All whole direct blocks or frags.
    376 	 */
    377 	for (i = NDADDR - 1; i > lastblock; i--) {
    378 		long bsize;
    379 
    380 		bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
    381 		if (bn == 0)
    382 			continue;
    383 		oip->i_ffs_db[i] = 0;
    384 		bsize = blksize(fs, oip, i);
    385 		ffs_blkfree(oip, bn, bsize);
    386 		blocksreleased += btodb(bsize);
    387 	}
    388 	if (lastblock < 0)
    389 		goto done;
    390 
    391 	/*
    392 	 * Finally, look for a change in size of the
    393 	 * last direct block; release any frags.
    394 	 */
    395 	bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
    396 	if (bn != 0) {
    397 		long oldspace, newspace;
    398 
    399 		/*
    400 		 * Calculate amount of space we're giving
    401 		 * back as old block size minus new block size.
    402 		 */
    403 		oldspace = blksize(fs, oip, lastblock);
    404 		oip->i_ffs_size = length;
    405 		newspace = blksize(fs, oip, lastblock);
    406 		if (newspace == 0)
    407 			panic("itrunc: newspace");
    408 		if (oldspace - newspace > 0) {
    409 			/*
    410 			 * Block number of space to be free'd is
    411 			 * the old block # plus the number of frags
    412 			 * required for the storage we're keeping.
    413 			 */
    414 			bn += numfrags(fs, newspace);
    415 			ffs_blkfree(oip, bn, oldspace - newspace);
    416 			blocksreleased += btodb(oldspace - newspace);
    417 		}
    418 	}
    419 
    420 done:
    421 #ifdef DIAGNOSTIC
    422 	for (level = SINGLE; level <= TRIPLE; level++)
    423 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
    424 			panic("itrunc1");
    425 	for (i = 0; i < NDADDR; i++)
    426 		if (newblks[i] != oip->i_ffs_db[i])
    427 			panic("itrunc2");
    428 	if (length == 0 &&
    429 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    430 		panic("itrunc3");
    431 #endif /* DIAGNOSTIC */
    432 	/*
    433 	 * Put back the real size.
    434 	 */
    435 	oip->i_ffs_size = length;
    436 	oip->i_ffs_blocks -= blocksreleased;
    437 	if (oip->i_ffs_blocks < 0)			/* sanity */
    438 		oip->i_ffs_blocks = 0;
    439 	oip->i_flag |= IN_CHANGE;
    440 #ifdef QUOTA
    441 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    442 #endif
    443 	return (allerror);
    444 }
    445 
    446 /*
    447  * Release blocks associated with the inode ip and stored in the indirect
    448  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    449  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    450  * and recursive calls to indirtrunc must be used to cleanse other indirect
    451  * blocks.
    452  *
    453  * NB: triple indirect blocks are untested.
    454  */
    455 static int
    456 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    457 	struct inode *ip;
    458 	ufs_daddr_t lbn, lastbn;
    459 	ufs_daddr_t dbn;
    460 	int level;
    461 	long *countp;
    462 {
    463 	int i;
    464 	struct buf *bp;
    465 	struct fs *fs = ip->i_fs;
    466 	ufs_daddr_t *bap;
    467 	struct vnode *vp;
    468 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
    469 	long blkcount, factor;
    470 	int nblocks, blocksreleased = 0;
    471 	int error = 0, allerror = 0;
    472 
    473 	/*
    474 	 * Calculate index in current block of last
    475 	 * block to be kept.  -1 indicates the entire
    476 	 * block so we need not calculate the index.
    477 	 */
    478 	factor = 1;
    479 	for (i = SINGLE; i < level; i++)
    480 		factor *= NINDIR(fs);
    481 	last = lastbn;
    482 	if (lastbn > 0)
    483 		last /= factor;
    484 	nblocks = btodb(fs->fs_bsize);
    485 	/*
    486 	 * Get buffer of block pointers, zero those entries corresponding
    487 	 * to blocks to be free'd, and update on disk copy first.  Since
    488 	 * double(triple) indirect before single(double) indirect, calls
    489 	 * to bmap on these blocks will fail.  However, we already have
    490 	 * the on disk address, so we have to set the b_blkno field
    491 	 * explicitly instead of letting bread do everything for us.
    492 	 */
    493 	vp = ITOV(ip);
    494 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    495 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    496 		/* Braces must be here in case trace evaluates to nothing. */
    497 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    498 	} else {
    499 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    500 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    501 		bp->b_flags |= B_READ;
    502 		if (bp->b_bcount > bp->b_bufsize)
    503 			panic("ffs_indirtrunc: bad buffer size");
    504 		bp->b_blkno = dbn;
    505 		VOP_STRATEGY(bp);
    506 		error = biowait(bp);
    507 	}
    508 	if (error) {
    509 		brelse(bp);
    510 		*countp = 0;
    511 		return (error);
    512 	}
    513 
    514 	bap = (ufs_daddr_t *)bp->b_data;
    515 	if (lastbn >= 0) {
    516 		MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
    517 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
    518 		memset((caddr_t)&bap[last + 1], 0,
    519 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
    520 		error = bwrite(bp);
    521 		if (error)
    522 			allerror = error;
    523 		bap = copy;
    524 	}
    525 
    526 	/*
    527 	 * Recursively free totally unused blocks.
    528 	 */
    529 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    530 	    i--, nlbn += factor) {
    531 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    532 		if (nb == 0)
    533 			continue;
    534 		if (level > SINGLE) {
    535 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    536 					       (ufs_daddr_t)-1, level - 1,
    537 					       &blkcount);
    538 			if (error)
    539 				allerror = error;
    540 			blocksreleased += blkcount;
    541 		}
    542 		ffs_blkfree(ip, nb, fs->fs_bsize);
    543 		blocksreleased += nblocks;
    544 	}
    545 
    546 	/*
    547 	 * Recursively free last partial block.
    548 	 */
    549 	if (level > SINGLE && lastbn >= 0) {
    550 		last = lastbn % factor;
    551 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    552 		if (nb != 0) {
    553 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    554 					       last, level - 1, &blkcount);
    555 			if (error)
    556 				allerror = error;
    557 			blocksreleased += blkcount;
    558 		}
    559 	}
    560 
    561 	if (copy != NULL) {
    562 		FREE(copy, M_TEMP);
    563 	} else {
    564 		bp->b_flags |= B_INVAL;
    565 		brelse(bp);
    566 	}
    567 
    568 	*countp = blocksreleased;
    569 	return (allerror);
    570 }
    571