Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.58
      1 /*	$NetBSD: ffs_inode.c,v 1.58 2003/06/28 14:22:25 darrenr 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.58 2003/06/28 14:22:25 darrenr 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 *, daddr_t, daddr_t,
     68 			       daddr_t, int, int64_t *));
     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_magic == FS_UFS1_MAGIC &&			/* XXX */
    124 	    fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
    125 		ip->i_ffs1_ouid = ip->i_uid;	/* XXX */
    126 		ip->i_ffs1_ogid = ip->i_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_nlink)
    139 		panic("ffs_update: bad link cnt");
    140 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    141 		cp = (caddr_t)bp->b_data +
    142 		    (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
    143 #ifdef FFS_EI
    144 		if (UFS_FSNEEDSWAP(fs))
    145 			ffs_dinode1_swap(ip->i_din.ffs1_din,
    146 			    (struct ufs1_dinode *)cp);
    147 		else
    148 #endif
    149 			memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
    150 	} else {
    151 		cp = (caddr_t)bp->b_data +
    152 		    (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
    153 #ifdef FFS_EI
    154 		if (UFS_FSNEEDSWAP(fs))
    155 			ffs_dinode2_swap(ip->i_din.ffs2_din,
    156 			    (struct ufs2_dinode *)cp);
    157 		else
    158 #endif
    159 			memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
    160 	}
    161 	if (waitfor) {
    162 		return (bwrite(bp));
    163 	} else {
    164 		bdwrite(bp);
    165 		return (0);
    166 	}
    167 }
    168 
    169 #define	SINGLE	0	/* index of single indirect block */
    170 #define	DOUBLE	1	/* index of double indirect block */
    171 #define	TRIPLE	2	/* index of triple indirect block */
    172 /*
    173  * Truncate the inode oip to at most length size, freeing the
    174  * disk blocks.
    175  */
    176 int
    177 ffs_truncate(v)
    178 	void *v;
    179 {
    180 	struct vop_truncate_args /* {
    181 		struct vnode *a_vp;
    182 		off_t a_length;
    183 		int a_flags;
    184 		struct ucred *a_cred;
    185 		struct lwp *a_l;
    186 	} */ *ap = v;
    187 	struct vnode *ovp = ap->a_vp;
    188 	struct genfs_node *gp = VTOG(ovp);
    189 	daddr_t lastblock;
    190 	struct inode *oip;
    191 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    192 	daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
    193 	off_t length = ap->a_length;
    194 	struct fs *fs;
    195 	int offset, size, level;
    196 	int64_t count, blocksreleased = 0;
    197 	int i, ioflag, aflag, nblocks;
    198 	int error, allerror = 0;
    199 	off_t osize;
    200 
    201 	if (length < 0)
    202 		return (EINVAL);
    203 	oip = VTOI(ovp);
    204 	if (ovp->v_type == VLNK &&
    205 	    (oip->i_size < ovp->v_mount->mnt_maxsymlinklen ||
    206 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    207 	      DIP(oip, blocks) == 0))) {
    208 		KDASSERT(length == 0);
    209 		memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
    210 		oip->i_size = 0;
    211 		DIP_ASSIGN(oip, size, 0);
    212 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    213 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
    214 	}
    215 	if (oip->i_size == length) {
    216 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    217 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    218 	}
    219 #ifdef QUOTA
    220 	if ((error = getinoquota(oip)) != 0)
    221 		return (error);
    222 #endif
    223 	fs = oip->i_fs;
    224 	if (length > fs->fs_maxfilesize)
    225 		return (EFBIG);
    226 
    227 	osize = oip->i_size;
    228 	ioflag = ap->a_flags;
    229 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    230 
    231 	/*
    232 	 * Lengthen the size of the file. We must ensure that the
    233 	 * last byte of the file is allocated. Since the smallest
    234 	 * value of osize is 0, length will be at least 1.
    235 	 */
    236 
    237 	if (osize < length) {
    238 		if (lblkno(fs, osize) < NDADDR &&
    239 		    lblkno(fs, osize) != lblkno(fs, length) &&
    240 		    blkroundup(fs, osize) != osize) {
    241 			error = ufs_balloc_range(ovp, osize,
    242 			    blkroundup(fs, osize) - osize, ap->a_cred, aflag);
    243 			if (error) {
    244 				return error;
    245 			}
    246 			if (ioflag & IO_SYNC) {
    247 				ovp->v_size = blkroundup(fs, osize);
    248 				simple_lock(&ovp->v_interlock);
    249 				VOP_PUTPAGES(ovp,
    250 				    trunc_page(osize & ~(fs->fs_bsize - 1)),
    251 				    round_page(ovp->v_size),
    252 				    PGO_CLEANIT | PGO_SYNCIO);
    253 			}
    254 		}
    255 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    256 		    aflag);
    257 		if (error) {
    258 			(void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
    259 			    ap->a_cred, ap->a_l);
    260 			return error;
    261 		}
    262 		uvm_vnp_setsize(ovp, length);
    263 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    264 		KASSERT(ovp->v_size == oip->i_size);
    265 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    266 	}
    267 
    268 	/*
    269 	 * When truncating a regular file down to a non-block-aligned size,
    270 	 * we must zero the part of last block which is past the new EOF.
    271 	 * We must synchronously flush the zeroed pages to disk
    272 	 * since the new pages will be invalidated as soon as we
    273 	 * inform the VM system of the new, smaller size.
    274 	 * We must do this before acquiring the GLOCK, since fetching
    275 	 * the pages will acquire the GLOCK internally.
    276 	 * So there is a window where another thread could see a whole
    277 	 * zeroed page past EOF, but that's life.
    278 	 */
    279 
    280 	offset = blkoff(fs, length);
    281 	if (ovp->v_type == VREG && length < osize && offset != 0) {
    282 		voff_t eoz;
    283 
    284 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    285 		    aflag);
    286 		if (error) {
    287 			return error;
    288 		}
    289 		size = blksize(fs, oip, lblkno(fs, length));
    290 		eoz = MIN(lblktosize(fs, lblkno(fs, length)) + size, osize);
    291 		uvm_vnp_zerorange(ovp, length, eoz - length);
    292 		simple_lock(&ovp->v_interlock);
    293 		error = VOP_PUTPAGES(ovp, trunc_page(length), round_page(eoz),
    294 		    PGO_CLEANIT | PGO_DEACTIVATE | PGO_SYNCIO);
    295 		if (error) {
    296 			return error;
    297 		}
    298 	}
    299 
    300 	lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    301 
    302 	if (DOINGSOFTDEP(ovp)) {
    303 		if (length > 0) {
    304 			/*
    305 			 * If a file is only partially truncated, then
    306 			 * we have to clean up the data structures
    307 			 * describing the allocation past the truncation
    308 			 * point. Finding and deallocating those structures
    309 			 * is a lot of work. Since partial truncation occurs
    310 			 * rarely, we solve the problem by syncing the file
    311 			 * so that it will have no data structures left.
    312 			 */
    313 			if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
    314 			    0, 0, ap->a_l)) != 0) {
    315 				lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    316 				return (error);
    317 			}
    318 			if (oip->i_flag & IN_SPACECOUNTED)
    319 				fs->fs_pendingblocks -= DIP(oip, blocks);
    320 		} else {
    321 			uvm_vnp_setsize(ovp, length);
    322 #ifdef QUOTA
    323  			(void) chkdq(oip, -DIP(oip, blocks), NOCRED, 0);
    324 #endif
    325 			softdep_setup_freeblocks(oip, length, 0);
    326 			(void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_l, 0, 0);
    327 			lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    328 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    329 			return (VOP_UPDATE(ovp, NULL, NULL, 0));
    330 		}
    331 	}
    332 	oip->i_size = length;
    333 	DIP_ASSIGN(oip, size, length);
    334 	uvm_vnp_setsize(ovp, length);
    335 	/*
    336 	 * Calculate index into inode's block list of
    337 	 * last direct and indirect blocks (if any)
    338 	 * which we want to keep.  Lastblock is -1 when
    339 	 * the file is truncated to 0.
    340 	 */
    341 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    342 	lastiblock[SINGLE] = lastblock - NDADDR;
    343 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    344 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    345 	nblocks = btodb(fs->fs_bsize);
    346 	/*
    347 	 * Update file and block pointers on disk before we start freeing
    348 	 * blocks.  If we crash before free'ing blocks below, the blocks
    349 	 * will be returned to the free list.  lastiblock values are also
    350 	 * normalized to -1 for calls to ffs_indirtrunc below.
    351 	 */
    352 	for (level = TRIPLE; level >= SINGLE; level--) {
    353 		oldblks[NDADDR + level] = DIP(oip, ib[level]);
    354 		if (lastiblock[level] < 0) {
    355 			DIP_ASSIGN(oip, ib[level], 0);
    356 			lastiblock[level] = -1;
    357 		}
    358 	}
    359 	for (i = 0; i < NDADDR; i++) {
    360 		oldblks[i] = DIP(oip, db[i]);
    361 		if (i > lastblock)
    362 			DIP_ASSIGN(oip, db[i], 0);
    363 	}
    364 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    365 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    366 	if (error && !allerror)
    367 		allerror = error;
    368 
    369 	/*
    370 	 * Having written the new inode to disk, save its new configuration
    371 	 * and put back the old block pointers long enough to process them.
    372 	 * Note that we save the new block configuration so we can check it
    373 	 * when we are done.
    374 	 */
    375 	for (i = 0; i < NDADDR; i++) {
    376 		newblks[i] = DIP(oip, db[i]);
    377 		DIP_ASSIGN(oip, db[i], oldblks[i]);
    378 	}
    379 	for (i = 0; i < NIADDR; i++) {
    380 		newblks[NDADDR + i] = DIP(oip, ib[i]);
    381 		DIP_ASSIGN(oip, ib[i], oldblks[NDADDR + i]);
    382 	}
    383 
    384 	oip->i_size = osize;
    385 	DIP_ASSIGN(oip, size, osize);
    386 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    387 	if (error && !allerror)
    388 		allerror = error;
    389 
    390 	/*
    391 	 * Indirect blocks first.
    392 	 */
    393 	indir_lbn[SINGLE] = -NDADDR;
    394 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    395 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    396 	for (level = TRIPLE; level >= SINGLE; level--) {
    397 		if (oip->i_ump->um_fstype == UFS1)
    398 			bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
    399 		else
    400 			bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
    401 		if (bn != 0) {
    402 			error = ffs_indirtrunc(oip, indir_lbn[level],
    403 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    404 			if (error)
    405 				allerror = error;
    406 			blocksreleased += count;
    407 			if (lastiblock[level] < 0) {
    408 				DIP_ASSIGN(oip, ib[level], 0);
    409 				ffs_blkfree(oip, bn, fs->fs_bsize);
    410 				blocksreleased += nblocks;
    411 			}
    412 		}
    413 		if (lastiblock[level] >= 0)
    414 			goto done;
    415 	}
    416 
    417 	/*
    418 	 * All whole direct blocks or frags.
    419 	 */
    420 	for (i = NDADDR - 1; i > lastblock; i--) {
    421 		long bsize;
    422 
    423 		if (oip->i_ump->um_fstype == UFS1)
    424 			bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
    425 		else
    426 			bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
    427 		if (bn == 0)
    428 			continue;
    429 		DIP_ASSIGN(oip, db[i], 0);
    430 		bsize = blksize(fs, oip, i);
    431 		ffs_blkfree(oip, bn, bsize);
    432 		blocksreleased += btodb(bsize);
    433 	}
    434 	if (lastblock < 0)
    435 		goto done;
    436 
    437 	/*
    438 	 * Finally, look for a change in size of the
    439 	 * last direct block; release any frags.
    440 	 */
    441 	if (oip->i_ump->um_fstype == UFS1)
    442 		bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
    443 	else
    444 		bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
    445 	if (bn != 0) {
    446 		long oldspace, newspace;
    447 
    448 		/*
    449 		 * Calculate amount of space we're giving
    450 		 * back as old block size minus new block size.
    451 		 */
    452 		oldspace = blksize(fs, oip, lastblock);
    453 		oip->i_size = length;
    454 		DIP_ASSIGN(oip, size, length);
    455 		newspace = blksize(fs, oip, lastblock);
    456 		if (newspace == 0)
    457 			panic("itrunc: newspace");
    458 		if (oldspace - newspace > 0) {
    459 			/*
    460 			 * Block number of space to be free'd is
    461 			 * the old block # plus the number of frags
    462 			 * required for the storage we're keeping.
    463 			 */
    464 			bn += numfrags(fs, newspace);
    465 			ffs_blkfree(oip, bn, oldspace - newspace);
    466 			blocksreleased += btodb(oldspace - newspace);
    467 		}
    468 	}
    469 
    470 done:
    471 #ifdef DIAGNOSTIC
    472 	for (level = SINGLE; level <= TRIPLE; level++)
    473 		if (newblks[NDADDR + level] != DIP(oip, ib[level]))
    474 			panic("itrunc1");
    475 	for (i = 0; i < NDADDR; i++)
    476 		if (newblks[i] != DIP(oip, db[i]))
    477 			panic("itrunc2");
    478 	if (length == 0 &&
    479 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    480 		panic("itrunc3");
    481 #endif /* DIAGNOSTIC */
    482 	/*
    483 	 * Put back the real size.
    484 	 */
    485 	oip->i_size = length;
    486 	DIP_ASSIGN(oip, size, length);
    487 	DIP_ADD(oip, blocks, -blocksreleased);
    488 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    489 	oip->i_flag |= IN_CHANGE;
    490 #ifdef QUOTA
    491 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    492 #endif
    493 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
    494 	return (allerror);
    495 }
    496 
    497 /*
    498  * Release blocks associated with the inode ip and stored in the indirect
    499  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    500  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    501  * and recursive calls to indirtrunc must be used to cleanse other indirect
    502  * blocks.
    503  *
    504  * NB: triple indirect blocks are untested.
    505  */
    506 static int
    507 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    508 	struct inode *ip;
    509 	daddr_t lbn, lastbn;
    510 	daddr_t dbn;
    511 	int level;
    512 	int64_t *countp;
    513 {
    514 	int i;
    515 	struct buf *bp;
    516 	struct fs *fs = ip->i_fs;
    517 	int32_t *bap1 = NULL;
    518 	int64_t *bap2 = NULL;
    519 	struct vnode *vp;
    520 	daddr_t nb, nlbn, last;
    521 	char *copy = NULL;
    522 	int64_t blkcount, factor, blocksreleased = 0;
    523 	int nblocks;
    524 	int error = 0, allerror = 0;
    525 #ifdef FFS_EI
    526 	const int needswap = UFS_FSNEEDSWAP(fs);
    527 #endif
    528 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
    529 	    ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
    530 #define BAP_ASSIGN(ip, i, value)					\
    531 	do {								\
    532 		if ((ip)->i_ump->um_fstype == UFS1)			\
    533 			bap1[i] = (value);				\
    534 		else							\
    535 			bap2[i] = (value);				\
    536 	} while(0)
    537 
    538 	/*
    539 	 * Calculate index in current block of last
    540 	 * block to be kept.  -1 indicates the entire
    541 	 * block so we need not calculate the index.
    542 	 */
    543 	factor = 1;
    544 	for (i = SINGLE; i < level; i++)
    545 		factor *= NINDIR(fs);
    546 	last = lastbn;
    547 	if (lastbn > 0)
    548 		last /= factor;
    549 	nblocks = btodb(fs->fs_bsize);
    550 	/*
    551 	 * Get buffer of block pointers, zero those entries corresponding
    552 	 * to blocks to be free'd, and update on disk copy first.  Since
    553 	 * double(triple) indirect before single(double) indirect, calls
    554 	 * to bmap on these blocks will fail.  However, we already have
    555 	 * the on disk address, so we have to set the b_blkno field
    556 	 * explicitly instead of letting bread do everything for us.
    557 	 */
    558 	vp = ITOV(ip);
    559 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    560 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    561 		/* Braces must be here in case trace evaluates to nothing. */
    562 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    563 	} else {
    564 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    565 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    566 		bp->b_flags |= B_READ;
    567 		if (bp->b_bcount > bp->b_bufsize)
    568 			panic("ffs_indirtrunc: bad buffer size");
    569 		bp->b_blkno = dbn;
    570 		VOP_STRATEGY(bp);
    571 		error = biowait(bp);
    572 	}
    573 	if (error) {
    574 		brelse(bp);
    575 		*countp = 0;
    576 		return (error);
    577 	}
    578 
    579 	if (ip->i_ump->um_fstype == UFS1)
    580 		bap1 = (int32_t *)bp->b_data;
    581 	else
    582 		bap2 = (int64_t *)bp->b_data;
    583 	if (lastbn >= 0) {
    584 		copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
    585 		memcpy((caddr_t)copy, bp->b_data, (u_int)fs->fs_bsize);
    586 		for (i = last + 1; i < NINDIR(fs); i++)
    587 			BAP_ASSIGN(ip, i, 0);
    588 		error = bwrite(bp);
    589 		if (error)
    590 			allerror = error;
    591 		if (ip->i_ump->um_fstype == UFS1)
    592 			bap1 = (int32_t *)copy;
    593 		else
    594 			bap2 = (int64_t *)copy;
    595 	}
    596 
    597 	/*
    598 	 * Recursively free totally unused blocks.
    599 	 */
    600 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    601 	    i--, nlbn += factor) {
    602 		nb = RBAP(ip, i);
    603 		if (nb == 0)
    604 			continue;
    605 		if (level > SINGLE) {
    606 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    607 					       (daddr_t)-1, level - 1,
    608 					       &blkcount);
    609 			if (error)
    610 				allerror = error;
    611 			blocksreleased += blkcount;
    612 		}
    613 		ffs_blkfree(ip, nb, fs->fs_bsize);
    614 		blocksreleased += nblocks;
    615 	}
    616 
    617 	/*
    618 	 * Recursively free last partial block.
    619 	 */
    620 	if (level > SINGLE && lastbn >= 0) {
    621 		last = lastbn % factor;
    622 		nb = RBAP(ip, i);
    623 		if (nb != 0) {
    624 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    625 					       last, level - 1, &blkcount);
    626 			if (error)
    627 				allerror = error;
    628 			blocksreleased += blkcount;
    629 		}
    630 	}
    631 
    632 	if (copy != NULL) {
    633 		FREE(copy, M_TEMP);
    634 	} else {
    635 		bp->b_flags |= B_INVAL;
    636 		brelse(bp);
    637 	}
    638 
    639 	*countp = blocksreleased;
    640 	return (allerror);
    641 }
    642