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