Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.35.2.3
      1 /*	$NetBSD: ffs_inode.c,v 1.35.2.3 2002/02/26 21:13:18 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 		/*
    295 		 * When we are doing soft updates and the VOP_BALLOC
    296 		 * above fills in a direct block hole with a full sized
    297 		 * block that will be truncated down to a fragment below,
    298 		 * we must flush out the block dependency with an FSYNC
    299 		 * so that we do not get a soft updates inconsistency
    300 		 * when we create the fragment below.
    301 		 */
    302 		if (DOINGSOFTDEP(ovp) && lbn < NDADDR &&
    303 		    fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize) {
    304 			error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT, 0, 0,
    305 			    ap->a_p);
    306 			if (error != 0)
    307 				return error;
    308 		}
    309 
    310 		oip->i_ffs_size = length;
    311 		size = blksize(fs, oip, lbn);
    312 		(void) uvm_vnp_uncache(ovp);
    313 		if (ovp->v_type != VDIR)
    314 			memset((char *)bp->b_data + offset, 0,
    315 			       (u_int)(size - offset));
    316 		allocbuf(bp, size);
    317 		if (aflags & B_SYNC)
    318 			bwrite(bp);
    319 		else
    320 			bawrite(bp);
    321 	}
    322 	uvm_vnp_setsize(ovp, length);
    323 	/*
    324 	 * Calculate index into inode's block list of
    325 	 * last direct and indirect blocks (if any)
    326 	 * which we want to keep.  Lastblock is -1 when
    327 	 * the file is truncated to 0.
    328 	 */
    329 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    330 	lastiblock[SINGLE] = lastblock - NDADDR;
    331 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    332 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    333 	nblocks = btodb(fs->fs_bsize);
    334 	/*
    335 	 * Update file and block pointers on disk before we start freeing
    336 	 * blocks.  If we crash before free'ing blocks below, the blocks
    337 	 * will be returned to the free list.  lastiblock values are also
    338 	 * normalized to -1 for calls to ffs_indirtrunc below.
    339 	 */
    340 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
    341 	for (level = TRIPLE; level >= SINGLE; level--)
    342 		if (lastiblock[level] < 0) {
    343 			oip->i_ffs_ib[level] = 0;
    344 			lastiblock[level] = -1;
    345 		}
    346 	for (i = NDADDR - 1; i > lastblock; i--)
    347 		oip->i_ffs_db[i] = 0;
    348 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    349 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    350 	if (error && !allerror)
    351 		allerror = error;
    352 
    353 	/*
    354 	 * Having written the new inode to disk, save its new configuration
    355 	 * and put back the old block pointers long enough to process them.
    356 	 * Note that we save the new block configuration so we can check it
    357 	 * when we are done.
    358 	 */
    359 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
    360 	memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
    361 	oip->i_ffs_size = osize;
    362 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    363 	if (error && !allerror)
    364 		allerror = error;
    365 
    366 	/*
    367 	 * Indirect blocks first.
    368 	 */
    369 	indir_lbn[SINGLE] = -NDADDR;
    370 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    371 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    372 	for (level = TRIPLE; level >= SINGLE; level--) {
    373 		bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
    374 		if (bn != 0) {
    375 			error = ffs_indirtrunc(oip, indir_lbn[level],
    376 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    377 			if (error)
    378 				allerror = error;
    379 			blocksreleased += count;
    380 			if (lastiblock[level] < 0) {
    381 				oip->i_ffs_ib[level] = 0;
    382 				ffs_blkfree(oip, bn, fs->fs_bsize);
    383 				blocksreleased += nblocks;
    384 			}
    385 		}
    386 		if (lastiblock[level] >= 0)
    387 			goto done;
    388 	}
    389 
    390 	/*
    391 	 * All whole direct blocks or frags.
    392 	 */
    393 	for (i = NDADDR - 1; i > lastblock; i--) {
    394 		long bsize;
    395 
    396 		bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
    397 		if (bn == 0)
    398 			continue;
    399 		oip->i_ffs_db[i] = 0;
    400 		bsize = blksize(fs, oip, i);
    401 		ffs_blkfree(oip, bn, bsize);
    402 		blocksreleased += btodb(bsize);
    403 	}
    404 	if (lastblock < 0)
    405 		goto done;
    406 
    407 	/*
    408 	 * Finally, look for a change in size of the
    409 	 * last direct block; release any frags.
    410 	 */
    411 	bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
    412 	if (bn != 0) {
    413 		long oldspace, newspace;
    414 
    415 		/*
    416 		 * Calculate amount of space we're giving
    417 		 * back as old block size minus new block size.
    418 		 */
    419 		oldspace = blksize(fs, oip, lastblock);
    420 		oip->i_ffs_size = length;
    421 		newspace = blksize(fs, oip, lastblock);
    422 		if (newspace == 0)
    423 			panic("itrunc: newspace");
    424 		if (oldspace - newspace > 0) {
    425 			/*
    426 			 * Block number of space to be free'd is
    427 			 * the old block # plus the number of frags
    428 			 * required for the storage we're keeping.
    429 			 */
    430 			bn += numfrags(fs, newspace);
    431 			ffs_blkfree(oip, bn, oldspace - newspace);
    432 			blocksreleased += btodb(oldspace - newspace);
    433 		}
    434 	}
    435 
    436 done:
    437 #ifdef DIAGNOSTIC
    438 	for (level = SINGLE; level <= TRIPLE; level++)
    439 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
    440 			panic("itrunc1");
    441 	for (i = 0; i < NDADDR; i++)
    442 		if (newblks[i] != oip->i_ffs_db[i])
    443 			panic("itrunc2");
    444 	if (length == 0 &&
    445 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    446 		panic("itrunc3");
    447 #endif /* DIAGNOSTIC */
    448 	/*
    449 	 * Put back the real size.
    450 	 */
    451 	oip->i_ffs_size = length;
    452 	oip->i_ffs_blocks -= blocksreleased;
    453 	if (oip->i_ffs_blocks < 0)			/* sanity */
    454 		oip->i_ffs_blocks = 0;
    455 	oip->i_flag |= IN_CHANGE;
    456 #ifdef QUOTA
    457 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    458 #endif
    459 	return (allerror);
    460 }
    461 
    462 /*
    463  * Release blocks associated with the inode ip and stored in the indirect
    464  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    465  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    466  * and recursive calls to indirtrunc must be used to cleanse other indirect
    467  * blocks.
    468  *
    469  * NB: triple indirect blocks are untested.
    470  */
    471 static int
    472 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    473 	struct inode *ip;
    474 	ufs_daddr_t lbn, lastbn;
    475 	ufs_daddr_t dbn;
    476 	int level;
    477 	long *countp;
    478 {
    479 	int i;
    480 	struct buf *bp;
    481 	struct fs *fs = ip->i_fs;
    482 	ufs_daddr_t *bap;
    483 	struct vnode *vp;
    484 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
    485 	long blkcount, factor;
    486 	int nblocks, blocksreleased = 0;
    487 	int error = 0, allerror = 0;
    488 
    489 	/*
    490 	 * Calculate index in current block of last
    491 	 * block to be kept.  -1 indicates the entire
    492 	 * block so we need not calculate the index.
    493 	 */
    494 	factor = 1;
    495 	for (i = SINGLE; i < level; i++)
    496 		factor *= NINDIR(fs);
    497 	last = lastbn;
    498 	if (lastbn > 0)
    499 		last /= factor;
    500 	nblocks = btodb(fs->fs_bsize);
    501 	/*
    502 	 * Get buffer of block pointers, zero those entries corresponding
    503 	 * to blocks to be free'd, and update on disk copy first.  Since
    504 	 * double(triple) indirect before single(double) indirect, calls
    505 	 * to bmap on these blocks will fail.  However, we already have
    506 	 * the on disk address, so we have to set the b_blkno field
    507 	 * explicitly instead of letting bread do everything for us.
    508 	 */
    509 	vp = ITOV(ip);
    510 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    511 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    512 		/* Braces must be here in case trace evaluates to nothing. */
    513 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    514 	} else {
    515 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    516 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    517 		bp->b_flags |= B_READ;
    518 		if (bp->b_bcount > bp->b_bufsize)
    519 			panic("ffs_indirtrunc: bad buffer size");
    520 		bp->b_blkno = dbn;
    521 		VOP_STRATEGY(bp);
    522 		error = biowait(bp);
    523 	}
    524 	if (error) {
    525 		brelse(bp);
    526 		*countp = 0;
    527 		return (error);
    528 	}
    529 
    530 	bap = (ufs_daddr_t *)bp->b_data;
    531 	if (lastbn >= 0) {
    532 		MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
    533 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
    534 		memset((caddr_t)&bap[last + 1], 0,
    535 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
    536 		error = bwrite(bp);
    537 		if (error)
    538 			allerror = error;
    539 		bap = copy;
    540 	}
    541 
    542 	/*
    543 	 * Recursively free totally unused blocks.
    544 	 */
    545 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    546 	    i--, nlbn += factor) {
    547 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    548 		if (nb == 0)
    549 			continue;
    550 		if (level > SINGLE) {
    551 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    552 					       (ufs_daddr_t)-1, level - 1,
    553 					       &blkcount);
    554 			if (error)
    555 				allerror = error;
    556 			blocksreleased += blkcount;
    557 		}
    558 		ffs_blkfree(ip, nb, fs->fs_bsize);
    559 		blocksreleased += nblocks;
    560 	}
    561 
    562 	/*
    563 	 * Recursively free last partial block.
    564 	 */
    565 	if (level > SINGLE && lastbn >= 0) {
    566 		last = lastbn % factor;
    567 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
    568 		if (nb != 0) {
    569 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    570 					       last, level - 1, &blkcount);
    571 			if (error)
    572 				allerror = error;
    573 			blocksreleased += blkcount;
    574 		}
    575 	}
    576 
    577 	if (copy != NULL) {
    578 		FREE(copy, M_TEMP);
    579 	} else {
    580 		bp->b_flags |= B_INVAL;
    581 		brelse(bp);
    582 	}
    583 
    584 	*countp = blocksreleased;
    585 	return (allerror);
    586 }
    587