Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.50
      1 /*	$NetBSD: ffs_inode.c,v 1.50 2001/12/18 06:50:28 chs 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 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.50 2001/12/18 06:50:28 chs Exp $");
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_ffs.h"
     43 #include "opt_quota.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mount.h>
     49 #include <sys/proc.h>
     50 #include <sys/file.h>
     51 #include <sys/buf.h>
     52 #include <sys/vnode.h>
     53 #include <sys/kernel.h>
     54 #include <sys/malloc.h>
     55 #include <sys/trace.h>
     56 #include <sys/resourcevar.h>
     57 
     58 #include <ufs/ufs/quota.h>
     59 #include <ufs/ufs/inode.h>
     60 #include <ufs/ufs/ufsmount.h>
     61 #include <ufs/ufs/ufs_extern.h>
     62 #include <ufs/ufs/ufs_bswap.h>
     63 
     64 #include <ufs/ffs/fs.h>
     65 #include <ufs/ffs/ffs_extern.h>
     66 
     67 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
     68 			       ufs_daddr_t, int, long *));
     69 
     70 /*
     71  * Update the access, modified, and inode change times as specified
     72  * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
     73  * The IN_MODIFIED flag is used to specify that the inode needs to be
     74  * updated but that the times have already been set. The access
     75  * and modified times are taken from the second and third parameters;
     76  * the inode change time is always taken from the current time. If
     77  * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
     78  * softupdates, then wait for the disk write of the inode to complete.
     79  */
     80 
     81 int
     82 ffs_update(v)
     83 	void *v;
     84 {
     85 	struct vop_update_args /* {
     86 		struct vnode *a_vp;
     87 		struct timespec *a_access;
     88 		struct timespec *a_modify;
     89 		int a_flags;
     90 	} */ *ap = v;
     91 	struct fs *fs;
     92 	struct buf *bp;
     93 	struct inode *ip;
     94 	int error;
     95 	struct timespec ts;
     96 	caddr_t cp;
     97 	int waitfor, flags;
     98 
     99 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
    100 		return (0);
    101 	ip = VTOI(ap->a_vp);
    102 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    103 	FFS_ITIMES(ip,
    104 	    ap->a_access ? ap->a_access : &ts,
    105 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    106 	flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
    107 	if (flags == 0)
    108 		return (0);
    109 	fs = ip->i_fs;
    110 
    111 	if ((flags & IN_MODIFIED) != 0 &&
    112 	    (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
    113 		waitfor = ap->a_flags & UPDATE_WAIT;
    114 		if ((ap->a_flags & UPDATE_DIROP) && !DOINGSOFTDEP(ap->a_vp))
    115 			waitfor |= UPDATE_WAIT;
    116 	} else
    117 		waitfor = 0;
    118 
    119 	/*
    120 	 * Ensure that uid and gid are correct. This is a temporary
    121 	 * fix until fsck has been changed to do the update.
    122 	 */
    123 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    124 		ip->i_din.ffs_din.di_ouid = ip->i_ffs_uid;	/* XXX */
    125 		ip->i_din.ffs_din.di_ogid = ip->i_ffs_gid;	/* XXX */
    126 	}							/* XXX */
    127 	error = bread(ip->i_devvp,
    128 		      fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    129 		      (int)fs->fs_bsize, NOCRED, &bp);
    130 	if (error) {
    131 		brelse(bp);
    132 		return (error);
    133 	}
    134 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    135 	if (DOINGSOFTDEP(ap->a_vp))
    136 		softdep_update_inodeblock(ip, bp, waitfor);
    137 	else if (ip->i_ffs_effnlink != ip->i_ffs_nlink)
    138 		panic("ffs_update: bad link cnt");
    139 	cp = (caddr_t)bp->b_data +
    140 	    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
    141 #ifdef FFS_EI
    142 	if (UFS_FSNEEDSWAP(fs))
    143 		ffs_dinode_swap(&ip->i_din.ffs_din, (struct dinode *)cp);
    144 	else
    145 #endif
    146 		memcpy(cp, &ip->i_din.ffs_din, DINODE_SIZE);
    147 	if (waitfor) {
    148 		return (bwrite(bp));
    149 	} else {
    150 		bdwrite(bp);
    151 		return (0);
    152 	}
    153 }
    154 
    155 #define	SINGLE	0	/* index of single indirect block */
    156 #define	DOUBLE	1	/* index of double indirect block */
    157 #define	TRIPLE	2	/* index of triple indirect block */
    158 /*
    159  * Truncate the inode oip to at most length size, freeing the
    160  * disk blocks.
    161  */
    162 int
    163 ffs_truncate(v)
    164 	void *v;
    165 {
    166 	struct vop_truncate_args /* {
    167 		struct vnode *a_vp;
    168 		off_t a_length;
    169 		int a_flags;
    170 		struct ucred *a_cred;
    171 		struct proc *a_p;
    172 	} */ *ap = v;
    173 	struct vnode *ovp = ap->a_vp;
    174 	struct genfs_node *gp = VTOG(ovp);
    175 	ufs_daddr_t lastblock;
    176 	struct inode *oip;
    177 	ufs_daddr_t bn, 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 	int offset, size, level;
    182 	long count, nblocks, blocksreleased = 0;
    183 	int i, ioflag, aflag;
    184 	int error, allerror = 0;
    185 	off_t osize;
    186 
    187 	if (length < 0)
    188 		return (EINVAL);
    189 	oip = VTOI(ovp);
    190 	if (ovp->v_type == VLNK &&
    191 	    (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
    192 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    193 	      oip->i_din.ffs_din.di_blocks == 0))) {
    194 		KDASSERT(length == 0);
    195 		memset(&oip->i_ffs_shortlink, 0, (size_t)oip->i_ffs_size);
    196 		oip->i_ffs_size = 0;
    197 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    198 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
    199 	}
    200 	if (oip->i_ffs_size == length) {
    201 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    202 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    203 	}
    204 #ifdef QUOTA
    205 	if ((error = getinoquota(oip)) != 0)
    206 		return (error);
    207 #endif
    208 	fs = oip->i_fs;
    209 	if (length > fs->fs_maxfilesize)
    210 		return (EFBIG);
    211 
    212 	osize = oip->i_ffs_size;
    213 	ioflag = ap->a_flags;
    214 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    215 
    216 	/*
    217 	 * Lengthen the size of the file. We must ensure that the
    218 	 * last byte of the file is allocated. Since the smallest
    219 	 * value of osize is 0, length will be at least 1.
    220 	 */
    221 
    222 	if (osize < length) {
    223 		if (lblkno(fs, osize) < NDADDR &&
    224 		    lblkno(fs, osize) != lblkno(fs, length) &&
    225 		    blkroundup(fs, osize) != osize) {
    226 			error = ufs_balloc_range(ovp, osize,
    227 			    blkroundup(fs, osize) - osize, ap->a_cred, aflag);
    228 			if (error) {
    229 				return error;
    230 			}
    231 			if (ioflag & IO_SYNC) {
    232 				ovp->v_size = blkroundup(fs, osize);
    233 				simple_lock(&ovp->v_interlock);
    234 				VOP_PUTPAGES(ovp,
    235 				    trunc_page(osize & ~(fs->fs_bsize - 1)),
    236 				    round_page(ovp->v_size),
    237 				    PGO_CLEANIT | PGO_SYNCIO);
    238 			}
    239 		}
    240 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    241 		    aflag);
    242 		if (error) {
    243 			(void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
    244 			    ap->a_cred, ap->a_p);
    245 			return error;
    246 		}
    247 		uvm_vnp_setsize(ovp, length);
    248 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    249 		KASSERT(ovp->v_size == oip->i_ffs_size);
    250 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    251 	}
    252 
    253 	/*
    254 	 * When truncating a regular file down to a non-block-aligned size,
    255 	 * we must zero the part of last block which is past the new EOF.
    256 	 * We must synchronously flush the zeroed pages to disk
    257 	 * since the new pages will be invalidated as soon as we
    258 	 * inform the VM system of the new, smaller size.
    259 	 * We must do this before acquiring the GLOCK, since fetching
    260 	 * the pages will acquire the GLOCK internally.
    261 	 * So there is a window where another thread could see a whole
    262 	 * zeroed page past EOF, but that's life.
    263 	 */
    264 
    265 	offset = blkoff(fs, length);
    266 	if (ovp->v_type == VREG && length < osize && offset != 0) {
    267 		voff_t eoz;
    268 
    269 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    270 		    aflag);
    271 		if (error) {
    272 			return error;
    273 		}
    274 		size = blksize(fs, oip, lblkno(fs, length));
    275 		eoz = MIN(lblktosize(fs, lblkno(fs, length)) + size, osize);
    276 		uvm_vnp_zerorange(ovp, length, eoz - length);
    277 		simple_lock(&ovp->v_interlock);
    278 		error = VOP_PUTPAGES(ovp, trunc_page(length), round_page(eoz),
    279 		    PGO_CLEANIT | PGO_DEACTIVATE | PGO_SYNCIO);
    280 		if (error) {
    281 			return error;
    282 		}
    283 	}
    284 
    285 	lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    286 
    287 	if (DOINGSOFTDEP(ovp)) {
    288 		if (length > 0) {
    289 			/*
    290 			 * If a file is only partially truncated, then
    291 			 * we have to clean up the data structures
    292 			 * describing the allocation past the truncation
    293 			 * point. Finding and deallocating those structures
    294 			 * is a lot of work. Since partial truncation occurs
    295 			 * rarely, we solve the problem by syncing the file
    296 			 * so that it will have no data structures left.
    297 			 */
    298 			if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
    299 			    0, 0, ap->a_p)) != 0) {
    300 				lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    301 				return (error);
    302 			}
    303 		} else {
    304 			uvm_vnp_setsize(ovp, length);
    305 #ifdef QUOTA
    306  			(void) chkdq(oip, -oip->i_ffs_blocks, NOCRED, 0);
    307 #endif
    308 			softdep_setup_freeblocks(oip, length);
    309 			(void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
    310 			lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    311 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    312 			return (VOP_UPDATE(ovp, NULL, NULL, 0));
    313 		}
    314 	}
    315 
    316 	/*
    317 	 * Reduce the size of the file.
    318 	 */
    319 	oip->i_ffs_size = length;
    320 	uvm_vnp_setsize(ovp, length);
    321 	/*
    322 	 * Calculate index into inode's block list of
    323 	 * last direct and indirect blocks (if any)
    324 	 * which we want to keep.  Lastblock is -1 when
    325 	 * the file is truncated to 0.
    326 	 */
    327 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    328 	lastiblock[SINGLE] = lastblock - NDADDR;
    329 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    330 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    331 	nblocks = btodb(fs->fs_bsize);
    332 	/*
    333 	 * Update file and block pointers on disk before we start freeing
    334 	 * blocks.  If we crash before free'ing blocks below, the blocks
    335 	 * will be returned to the free list.  lastiblock values are also
    336 	 * normalized to -1 for calls to ffs_indirtrunc below.
    337 	 */
    338 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
    339 	for (level = TRIPLE; level >= SINGLE; level--)
    340 		if (lastiblock[level] < 0) {
    341 			oip->i_ffs_ib[level] = 0;
    342 			lastiblock[level] = -1;
    343 		}
    344 	for (i = NDADDR - 1; i > lastblock; i--)
    345 		oip->i_ffs_db[i] = 0;
    346 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    347 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    348 	if (error && !allerror)
    349 		allerror = error;
    350 
    351 	/*
    352 	 * Having written the new inode to disk, save its new configuration
    353 	 * and put back the old block pointers long enough to process them.
    354 	 * Note that we save the new block configuration so we can check it
    355 	 * when we are done.
    356 	 */
    357 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
    358 	memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
    359 	oip->i_ffs_size = osize;
    360 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    361 	if (error && !allerror)
    362 		allerror = error;
    363 
    364 	/*
    365 	 * Indirect blocks first.
    366 	 */
    367 	indir_lbn[SINGLE] = -NDADDR;
    368 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    369 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    370 	for (level = TRIPLE; level >= SINGLE; level--) {
    371 		bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
    372 		if (bn != 0) {
    373 			error = ffs_indirtrunc(oip, indir_lbn[level],
    374 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    375 			if (error)
    376 				allerror = error;
    377 			blocksreleased += count;
    378 			if (lastiblock[level] < 0) {
    379 				oip->i_ffs_ib[level] = 0;
    380 				ffs_blkfree(oip, bn, fs->fs_bsize);
    381 				blocksreleased += nblocks;
    382 			}
    383 		}
    384 		if (lastiblock[level] >= 0)
    385 			goto done;
    386 	}
    387 
    388 	/*
    389 	 * All whole direct blocks or frags.
    390 	 */
    391 	for (i = NDADDR - 1; i > lastblock; i--) {
    392 		long bsize;
    393 
    394 		bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
    395 		if (bn == 0)
    396 			continue;
    397 		oip->i_ffs_db[i] = 0;
    398 		bsize = blksize(fs, oip, i);
    399 		ffs_blkfree(oip, bn, bsize);
    400 		blocksreleased += btodb(bsize);
    401 	}
    402 	if (lastblock < 0)
    403 		goto done;
    404 
    405 	/*
    406 	 * Finally, look for a change in size of the
    407 	 * last direct block; release any frags.
    408 	 */
    409 	bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
    410 	if (bn != 0) {
    411 		long oldspace, newspace;
    412 
    413 		/*
    414 		 * Calculate amount of space we're giving
    415 		 * back as old block size minus new block size.
    416 		 */
    417 		oldspace = blksize(fs, oip, lastblock);
    418 		oip->i_ffs_size = length;
    419 		newspace = blksize(fs, oip, lastblock);
    420 		if (newspace == 0)
    421 			panic("itrunc: newspace");
    422 		if (oldspace - newspace > 0) {
    423 			/*
    424 			 * Block number of space to be free'd is
    425 			 * the old block # plus the number of frags
    426 			 * required for the storage we're keeping.
    427 			 */
    428 			bn += numfrags(fs, newspace);
    429 			ffs_blkfree(oip, bn, oldspace - newspace);
    430 			blocksreleased += btodb(oldspace - newspace);
    431 		}
    432 	}
    433 
    434 done:
    435 #ifdef DIAGNOSTIC
    436 	for (level = SINGLE; level <= TRIPLE; level++)
    437 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
    438 			panic("itrunc1");
    439 	for (i = 0; i < NDADDR; i++)
    440 		if (newblks[i] != oip->i_ffs_db[i])
    441 			panic("itrunc2");
    442 	if (length == 0 &&
    443 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    444 		panic("itrunc3");
    445 #endif /* DIAGNOSTIC */
    446 	/*
    447 	 * Put back the real size.
    448 	 */
    449 	oip->i_ffs_size = length;
    450 	oip->i_ffs_blocks -= blocksreleased;
    451 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    452 	oip->i_flag |= IN_CHANGE;
    453 #ifdef QUOTA
    454 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    455 #endif
    456 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_ffs_size);
    457 	return (allerror);
    458 }
    459 
    460 /*
    461  * Release blocks associated with the inode ip and stored in the indirect
    462  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    463  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    464  * and recursive calls to indirtrunc must be used to cleanse other indirect
    465  * blocks.
    466  *
    467  * NB: triple indirect blocks are untested.
    468  */
    469 static int
    470 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    471 	struct inode *ip;
    472 	ufs_daddr_t lbn, lastbn;
    473 	ufs_daddr_t dbn;
    474 	int level;
    475 	long *countp;
    476 {
    477 	int i;
    478 	struct buf *bp;
    479 	struct fs *fs = ip->i_fs;
    480 	ufs_daddr_t *bap;
    481 	struct vnode *vp;
    482 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
    483 	long blkcount, factor;
    484 	int nblocks, blocksreleased = 0;
    485 	int error = 0, allerror = 0;
    486 
    487 	/*
    488 	 * Calculate index in current block of last
    489 	 * block to be kept.  -1 indicates the entire
    490 	 * block so we need not calculate the index.
    491 	 */
    492 	factor = 1;
    493 	for (i = SINGLE; i < level; i++)
    494 		factor *= NINDIR(fs);
    495 	last = lastbn;
    496 	if (lastbn > 0)
    497 		last /= factor;
    498 	nblocks = btodb(fs->fs_bsize);
    499 	/*
    500 	 * Get buffer of block pointers, zero those entries corresponding
    501 	 * to blocks to be free'd, and update on disk copy first.  Since
    502 	 * double(triple) indirect before single(double) indirect, calls
    503 	 * to bmap on these blocks will fail.  However, we already have
    504 	 * the on disk address, so we have to set the b_blkno field
    505 	 * explicitly instead of letting bread do everything for us.
    506 	 */
    507 	vp = ITOV(ip);
    508 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    509 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    510 		/* Braces must be here in case trace evaluates to nothing. */
    511 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    512 	} else {
    513 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    514 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    515 		bp->b_flags |= B_READ;
    516 		if (bp->b_bcount > bp->b_bufsize)
    517 			panic("ffs_indirtrunc: bad buffer size");
    518 		bp->b_blkno = dbn;
    519 		VOP_STRATEGY(bp);
    520 		error = biowait(bp);
    521 	}
    522 	if (error) {
    523 		brelse(bp);
    524 		*countp = 0;
    525 		return (error);
    526 	}
    527 
    528 	bap = (ufs_daddr_t *)bp->b_data;
    529 	if (lastbn >= 0) {
    530 		copy = (ufs_daddr_t *) malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
    531 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
    532 		memset((caddr_t)&bap[last + 1], 0,
    533 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_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, nlbn = lbn + 1 - i * factor; i > last;
    544 	    i--, nlbn += factor) {
    545 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    546 		if (nb == 0)
    547 			continue;
    548 		if (level > SINGLE) {
    549 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    550 					       (ufs_daddr_t)-1, level - 1,
    551 					       &blkcount);
    552 			if (error)
    553 				allerror = error;
    554 			blocksreleased += blkcount;
    555 		}
    556 		ffs_blkfree(ip, nb, fs->fs_bsize);
    557 		blocksreleased += nblocks;
    558 	}
    559 
    560 	/*
    561 	 * Recursively free last partial block.
    562 	 */
    563 	if (level > SINGLE && lastbn >= 0) {
    564 		last = lastbn % factor;
    565 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    566 		if (nb != 0) {
    567 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    568 					       last, level - 1, &blkcount);
    569 			if (error)
    570 				allerror = error;
    571 			blocksreleased += blkcount;
    572 		}
    573 	}
    574 
    575 	if (copy != NULL) {
    576 		FREE(copy, M_TEMP);
    577 	} else {
    578 		bp->b_flags |= B_INVAL;
    579 		brelse(bp);
    580 	}
    581 
    582 	*countp = blocksreleased;
    583 	return (allerror);
    584 }
    585