Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.40.2.6
      1 /*	$NetBSD: ffs_inode.c,v 1.40.2.6 2002/01/08 00:34:46 nathanw 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.40.2.6 2002/01/08 00:34:46 nathanw 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/lwp.h>
     50 #include <sys/proc.h>
     51 #include <sys/file.h>
     52 #include <sys/buf.h>
     53 #include <sys/vnode.h>
     54 #include <sys/kernel.h>
     55 #include <sys/malloc.h>
     56 #include <sys/trace.h>
     57 #include <sys/resourcevar.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 	struct genfs_node *gp = VTOG(ovp);
    176 	ufs_daddr_t lastblock;
    177 	struct inode *oip;
    178 	ufs_daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    179 	ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    180 	off_t length = ap->a_length;
    181 	struct fs *fs;
    182 	int offset, size, level;
    183 	long count, nblocks, blocksreleased = 0;
    184 	int i, ioflag, aflag;
    185 	int error, allerror = 0;
    186 	off_t osize;
    187 
    188 	if (length < 0)
    189 		return (EINVAL);
    190 	oip = VTOI(ovp);
    191 	if (ovp->v_type == VLNK &&
    192 	    (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
    193 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    194 	      oip->i_din.ffs_din.di_blocks == 0))) {
    195 		KDASSERT(length == 0);
    196 		memset(&oip->i_ffs_shortlink, 0, (size_t)oip->i_ffs_size);
    197 		oip->i_ffs_size = 0;
    198 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    199 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
    200 	}
    201 	if (oip->i_ffs_size == length) {
    202 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    203 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    204 	}
    205 #ifdef QUOTA
    206 	if ((error = getinoquota(oip)) != 0)
    207 		return (error);
    208 #endif
    209 	fs = oip->i_fs;
    210 	if (length > fs->fs_maxfilesize)
    211 		return (EFBIG);
    212 
    213 	osize = oip->i_ffs_size;
    214 	ioflag = ap->a_flags;
    215 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    216 
    217 	/*
    218 	 * Lengthen the size of the file. We must ensure that the
    219 	 * last byte of the file is allocated. Since the smallest
    220 	 * value of osize is 0, length will be at least 1.
    221 	 */
    222 
    223 	if (osize < length) {
    224 		if (lblkno(fs, osize) < NDADDR &&
    225 		    lblkno(fs, osize) != lblkno(fs, length) &&
    226 		    blkroundup(fs, osize) != osize) {
    227 			error = ufs_balloc_range(ovp, osize,
    228 			    blkroundup(fs, osize) - osize, ap->a_cred, aflag);
    229 			if (error) {
    230 				return error;
    231 			}
    232 			if (ioflag & IO_SYNC) {
    233 				ovp->v_size = blkroundup(fs, osize);
    234 				simple_lock(&ovp->v_interlock);
    235 				VOP_PUTPAGES(ovp,
    236 				    trunc_page(osize & ~(fs->fs_bsize - 1)),
    237 				    round_page(ovp->v_size),
    238 				    PGO_CLEANIT | PGO_SYNCIO);
    239 			}
    240 		}
    241 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    242 		    aflag);
    243 		if (error) {
    244 			(void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
    245 			    ap->a_cred, ap->a_p);
    246 			return error;
    247 		}
    248 		uvm_vnp_setsize(ovp, length);
    249 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    250 		KASSERT(ovp->v_size == oip->i_ffs_size);
    251 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    252 	}
    253 
    254 	/*
    255 	 * When truncating a regular file down to a non-block-aligned size,
    256 	 * we must zero the part of last block which is past the new EOF.
    257 	 * We must synchronously flush the zeroed pages to disk
    258 	 * since the new pages will be invalidated as soon as we
    259 	 * inform the VM system of the new, smaller size.
    260 	 * We must do this before acquiring the GLOCK, since fetching
    261 	 * the pages will acquire the GLOCK internally.
    262 	 * So there is a window where another thread could see a whole
    263 	 * zeroed page past EOF, but that's life.
    264 	 */
    265 
    266 	offset = blkoff(fs, length);
    267 	if (ovp->v_type == VREG && length < osize && offset != 0) {
    268 		voff_t eoz;
    269 
    270 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    271 		    aflag);
    272 		if (error) {
    273 			return error;
    274 		}
    275 		size = blksize(fs, oip, lblkno(fs, length));
    276 		eoz = MIN(lblktosize(fs, lblkno(fs, length)) + size, osize);
    277 		uvm_vnp_zerorange(ovp, length, eoz - length);
    278 		simple_lock(&ovp->v_interlock);
    279 		error = VOP_PUTPAGES(ovp, trunc_page(length), round_page(eoz),
    280 		    PGO_CLEANIT | PGO_DEACTIVATE | PGO_SYNCIO);
    281 		if (error) {
    282 			return error;
    283 		}
    284 	}
    285 
    286 	lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    287 
    288 	if (DOINGSOFTDEP(ovp)) {
    289 		if (length > 0) {
    290 			/*
    291 			 * If a file is only partially truncated, then
    292 			 * we have to clean up the data structures
    293 			 * describing the allocation past the truncation
    294 			 * point. Finding and deallocating those structures
    295 			 * is a lot of work. Since partial truncation occurs
    296 			 * rarely, we solve the problem by syncing the file
    297 			 * so that it will have no data structures left.
    298 			 */
    299 			if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
    300 			    0, 0, ap->a_p)) != 0) {
    301 				lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    302 				return (error);
    303 			if (oip->i_flag & IN_SPACECOUNTED)
    304 				fs->fs_pendingblocks -= oip->i_ffs_blocks;
    305 			}
    306 		} else {
    307 			uvm_vnp_setsize(ovp, length);
    308 #ifdef QUOTA
    309  			(void) chkdq(oip, -oip->i_ffs_blocks, NOCRED, 0);
    310 #endif
    311 			softdep_setup_freeblocks(oip, length);
    312 			(void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
    313 			lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    314 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    315 			return (VOP_UPDATE(ovp, NULL, NULL, 0));
    316 		}
    317 	}
    318 	oip->i_ffs_size = length;
    319 	uvm_vnp_setsize(ovp, length);
    320 	/*
    321 	 * Calculate index into inode's block list of
    322 	 * last direct and indirect blocks (if any)
    323 	 * which we want to keep.  Lastblock is -1 when
    324 	 * the file is truncated to 0.
    325 	 */
    326 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    327 	lastiblock[SINGLE] = lastblock - NDADDR;
    328 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    329 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    330 	nblocks = btodb(fs->fs_bsize);
    331 	/*
    332 	 * Update file and block pointers on disk before we start freeing
    333 	 * blocks.  If we crash before free'ing blocks below, the blocks
    334 	 * will be returned to the free list.  lastiblock values are also
    335 	 * normalized to -1 for calls to ffs_indirtrunc below.
    336 	 */
    337 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
    338 	for (level = TRIPLE; level >= SINGLE; level--)
    339 		if (lastiblock[level] < 0) {
    340 			oip->i_ffs_ib[level] = 0;
    341 			lastiblock[level] = -1;
    342 		}
    343 	for (i = NDADDR - 1; i > lastblock; i--)
    344 		oip->i_ffs_db[i] = 0;
    345 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    346 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    347 	if (error && !allerror)
    348 		allerror = error;
    349 
    350 	/*
    351 	 * Having written the new inode to disk, save its new configuration
    352 	 * and put back the old block pointers long enough to process them.
    353 	 * Note that we save the new block configuration so we can check it
    354 	 * when we are done.
    355 	 */
    356 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
    357 	memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
    358 	oip->i_ffs_size = osize;
    359 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    360 	if (error && !allerror)
    361 		allerror = error;
    362 
    363 	/*
    364 	 * Indirect blocks first.
    365 	 */
    366 	indir_lbn[SINGLE] = -NDADDR;
    367 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    368 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    369 	for (level = TRIPLE; level >= SINGLE; level--) {
    370 		bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
    371 		if (bn != 0) {
    372 			error = ffs_indirtrunc(oip, indir_lbn[level],
    373 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    374 			if (error)
    375 				allerror = error;
    376 			blocksreleased += count;
    377 			if (lastiblock[level] < 0) {
    378 				oip->i_ffs_ib[level] = 0;
    379 				ffs_blkfree(oip, bn, fs->fs_bsize);
    380 				blocksreleased += nblocks;
    381 			}
    382 		}
    383 		if (lastiblock[level] >= 0)
    384 			goto done;
    385 	}
    386 
    387 	/*
    388 	 * All whole direct blocks or frags.
    389 	 */
    390 	for (i = NDADDR - 1; i > lastblock; i--) {
    391 		long bsize;
    392 
    393 		bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
    394 		if (bn == 0)
    395 			continue;
    396 		oip->i_ffs_db[i] = 0;
    397 		bsize = blksize(fs, oip, i);
    398 		ffs_blkfree(oip, bn, bsize);
    399 		blocksreleased += btodb(bsize);
    400 	}
    401 	if (lastblock < 0)
    402 		goto done;
    403 
    404 	/*
    405 	 * Finally, look for a change in size of the
    406 	 * last direct block; release any frags.
    407 	 */
    408 	bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
    409 	if (bn != 0) {
    410 		long oldspace, newspace;
    411 
    412 		/*
    413 		 * Calculate amount of space we're giving
    414 		 * back as old block size minus new block size.
    415 		 */
    416 		oldspace = blksize(fs, oip, lastblock);
    417 		oip->i_ffs_size = length;
    418 		newspace = blksize(fs, oip, lastblock);
    419 		if (newspace == 0)
    420 			panic("itrunc: newspace");
    421 		if (oldspace - newspace > 0) {
    422 			/*
    423 			 * Block number of space to be free'd is
    424 			 * the old block # plus the number of frags
    425 			 * required for the storage we're keeping.
    426 			 */
    427 			bn += numfrags(fs, newspace);
    428 			ffs_blkfree(oip, bn, oldspace - newspace);
    429 			blocksreleased += btodb(oldspace - newspace);
    430 		}
    431 	}
    432 
    433 done:
    434 #ifdef DIAGNOSTIC
    435 	for (level = SINGLE; level <= TRIPLE; level++)
    436 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
    437 			panic("itrunc1");
    438 	for (i = 0; i < NDADDR; i++)
    439 		if (newblks[i] != oip->i_ffs_db[i])
    440 			panic("itrunc2");
    441 	if (length == 0 &&
    442 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    443 		panic("itrunc3");
    444 #endif /* DIAGNOSTIC */
    445 	/*
    446 	 * Put back the real size.
    447 	 */
    448 	oip->i_ffs_size = length;
    449 	oip->i_ffs_blocks -= blocksreleased;
    450 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    451 	oip->i_flag |= IN_CHANGE;
    452 #ifdef QUOTA
    453 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    454 #endif
    455 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_ffs_size);
    456 	return (allerror);
    457 }
    458 
    459 /*
    460  * Release blocks associated with the inode ip and stored in the indirect
    461  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    462  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    463  * and recursive calls to indirtrunc must be used to cleanse other indirect
    464  * blocks.
    465  *
    466  * NB: triple indirect blocks are untested.
    467  */
    468 static int
    469 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    470 	struct inode *ip;
    471 	ufs_daddr_t lbn, lastbn;
    472 	ufs_daddr_t dbn;
    473 	int level;
    474 	long *countp;
    475 {
    476 	int i;
    477 	struct buf *bp;
    478 	struct fs *fs = ip->i_fs;
    479 	ufs_daddr_t *bap;
    480 	struct vnode *vp;
    481 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
    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->fs_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->fs_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->fs_bsize), lbn);
    511 	} else {
    512 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    513 		curproc->l_proc->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("ffs_indirtrunc: bad buffer size");
    517 		bp->b_blkno = dbn;
    518 		VOP_STRATEGY(bp);
    519 		error = biowait(bp);
    520 	}
    521 	if (error) {
    522 		brelse(bp);
    523 		*countp = 0;
    524 		return (error);
    525 	}
    526 
    527 	bap = (ufs_daddr_t *)bp->b_data;
    528 	if (lastbn >= 0) {
    529 		copy = (ufs_daddr_t *) malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
    530 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
    531 		memset((caddr_t)&bap[last + 1], 0,
    532 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
    533 		error = bwrite(bp);
    534 		if (error)
    535 			allerror = error;
    536 		bap = copy;
    537 	}
    538 
    539 	/*
    540 	 * Recursively free totally unused blocks.
    541 	 */
    542 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    543 	    i--, nlbn += factor) {
    544 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    545 		if (nb == 0)
    546 			continue;
    547 		if (level > SINGLE) {
    548 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    549 					       (ufs_daddr_t)-1, level - 1,
    550 					       &blkcount);
    551 			if (error)
    552 				allerror = error;
    553 			blocksreleased += blkcount;
    554 		}
    555 		ffs_blkfree(ip, nb, fs->fs_bsize);
    556 		blocksreleased += nblocks;
    557 	}
    558 
    559 	/*
    560 	 * Recursively free last partial block.
    561 	 */
    562 	if (level > SINGLE && lastbn >= 0) {
    563 		last = lastbn % factor;
    564 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    565 		if (nb != 0) {
    566 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    567 					       last, level - 1, &blkcount);
    568 			if (error)
    569 				allerror = error;
    570 			blocksreleased += blkcount;
    571 		}
    572 	}
    573 
    574 	if (copy != NULL) {
    575 		FREE(copy, M_TEMP);
    576 	} else {
    577 		bp->b_flags |= B_INVAL;
    578 		brelse(bp);
    579 	}
    580 
    581 	*countp = blocksreleased;
    582 	return (allerror);
    583 }
    584