Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.64
      1 /*	$NetBSD: ffs_inode.c,v 1.64 2004/06/20 18:23:30 hannken 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_inode.c	8.13 (Berkeley) 4/21/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.64 2004/06/20 18:23:30 hannken Exp $");
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_ffs.h"
     39 #include "opt_quota.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mount.h>
     45 #include <sys/proc.h>
     46 #include <sys/file.h>
     47 #include <sys/buf.h>
     48 #include <sys/vnode.h>
     49 #include <sys/kernel.h>
     50 #include <sys/malloc.h>
     51 #include <sys/trace.h>
     52 #include <sys/resourcevar.h>
     53 
     54 #include <ufs/ufs/quota.h>
     55 #include <ufs/ufs/inode.h>
     56 #include <ufs/ufs/ufsmount.h>
     57 #include <ufs/ufs/ufs_extern.h>
     58 #include <ufs/ufs/ufs_bswap.h>
     59 
     60 #include <ufs/ffs/fs.h>
     61 #include <ufs/ffs/ffs_extern.h>
     62 
     63 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t,
     64 			       daddr_t, int, int64_t *));
     65 
     66 /*
     67  * Update the access, modified, and inode change times as specified
     68  * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
     69  * The IN_MODIFIED flag is used to specify that the inode needs to be
     70  * updated but that the times have already been set. The access
     71  * and modified times are taken from the second and third parameters;
     72  * the inode change time is always taken from the current time. If
     73  * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
     74  * softupdates, then wait for the disk write of the inode to complete.
     75  */
     76 
     77 int
     78 ffs_update(v)
     79 	void *v;
     80 {
     81 	struct vop_update_args /* {
     82 		struct vnode *a_vp;
     83 		struct timespec *a_access;
     84 		struct timespec *a_modify;
     85 		int a_flags;
     86 	} */ *ap = v;
     87 	struct fs *fs;
     88 	struct buf *bp;
     89 	struct inode *ip;
     90 	int error;
     91 	struct timespec ts;
     92 	caddr_t cp;
     93 	int waitfor, flags;
     94 
     95 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
     96 		return (0);
     97 	ip = VTOI(ap->a_vp);
     98 	TIMEVAL_TO_TIMESPEC(&time, &ts);
     99 	FFS_ITIMES(ip,
    100 	    ap->a_access ? ap->a_access : &ts,
    101 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    102 	flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
    103 	if (flags == 0)
    104 		return (0);
    105 	fs = ip->i_fs;
    106 
    107 	if ((flags & IN_MODIFIED) != 0 &&
    108 	    (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
    109 		waitfor = ap->a_flags & UPDATE_WAIT;
    110 		if ((ap->a_flags & UPDATE_DIROP) && !DOINGSOFTDEP(ap->a_vp))
    111 			waitfor |= UPDATE_WAIT;
    112 	} else
    113 		waitfor = 0;
    114 
    115 	/*
    116 	 * Ensure that uid and gid are correct. This is a temporary
    117 	 * fix until fsck has been changed to do the update.
    118 	 */
    119 	if (fs->fs_magic == FS_UFS1_MAGIC &&			/* XXX */
    120 	    fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
    121 		ip->i_ffs1_ouid = ip->i_uid;	/* XXX */
    122 		ip->i_ffs1_ogid = ip->i_gid;	/* XXX */
    123 	}							/* XXX */
    124 	error = bread(ip->i_devvp,
    125 		      fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    126 		      (int)fs->fs_bsize, NOCRED, &bp);
    127 	if (error) {
    128 		brelse(bp);
    129 		return (error);
    130 	}
    131 	ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
    132 	if (DOINGSOFTDEP(ap->a_vp))
    133 		softdep_update_inodeblock(ip, bp, waitfor);
    134 	else if (ip->i_ffs_effnlink != ip->i_nlink)
    135 		panic("ffs_update: bad link cnt");
    136 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    137 		cp = (caddr_t)bp->b_data +
    138 		    (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
    139 #ifdef FFS_EI
    140 		if (UFS_FSNEEDSWAP(fs))
    141 			ffs_dinode1_swap(ip->i_din.ffs1_din,
    142 			    (struct ufs1_dinode *)cp);
    143 		else
    144 #endif
    145 			memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
    146 	} else {
    147 		cp = (caddr_t)bp->b_data +
    148 		    (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
    149 #ifdef FFS_EI
    150 		if (UFS_FSNEEDSWAP(fs))
    151 			ffs_dinode2_swap(ip->i_din.ffs2_din,
    152 			    (struct ufs2_dinode *)cp);
    153 		else
    154 #endif
    155 			memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
    156 	}
    157 	if (waitfor) {
    158 		return (bwrite(bp));
    159 	} else {
    160 		bdwrite(bp);
    161 		return (0);
    162 	}
    163 }
    164 
    165 #define	SINGLE	0	/* index of single indirect block */
    166 #define	DOUBLE	1	/* index of double indirect block */
    167 #define	TRIPLE	2	/* index of triple indirect block */
    168 /*
    169  * Truncate the inode oip to at most length size, freeing the
    170  * disk blocks.
    171  */
    172 int
    173 ffs_truncate(v)
    174 	void *v;
    175 {
    176 	struct vop_truncate_args /* {
    177 		struct vnode *a_vp;
    178 		off_t a_length;
    179 		int a_flags;
    180 		struct ucred *a_cred;
    181 		struct proc *a_p;
    182 	} */ *ap = v;
    183 	struct vnode *ovp = ap->a_vp;
    184 	struct genfs_node *gp = VTOG(ovp);
    185 	daddr_t lastblock;
    186 	struct inode *oip;
    187 	daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
    188 	daddr_t blks[NDADDR + NIADDR];
    189 	off_t length = ap->a_length;
    190 	struct fs *fs;
    191 	int offset, size, level;
    192 	int64_t count, blocksreleased = 0;
    193 	int i, ioflag, aflag, nblocks;
    194 	int error, allerror = 0;
    195 	off_t osize;
    196 
    197 	if (length < 0)
    198 		return (EINVAL);
    199 	oip = VTOI(ovp);
    200 	if (ovp->v_type == VLNK &&
    201 	    (oip->i_size < ovp->v_mount->mnt_maxsymlinklen ||
    202 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
    203 	      DIP(oip, blocks) == 0))) {
    204 		KDASSERT(length == 0);
    205 		memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
    206 		oip->i_size = 0;
    207 		DIP_ASSIGN(oip, size, 0);
    208 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    209 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
    210 	}
    211 	if (oip->i_size == length) {
    212 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    213 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
    214 	}
    215 #ifdef QUOTA
    216 	if ((error = getinoquota(oip)) != 0)
    217 		return (error);
    218 #endif
    219 	fs = oip->i_fs;
    220 	if (length > fs->fs_maxfilesize)
    221 		return (EFBIG);
    222 
    223 	if ((oip->i_flags & SF_SNAPSHOT) != 0)
    224 		ffs_snapremove(ovp);
    225 
    226 	osize = oip->i_size;
    227 	ioflag = ap->a_flags;
    228 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    229 
    230 	/*
    231 	 * Lengthen the size of the file. We must ensure that the
    232 	 * last byte of the file is allocated. Since the smallest
    233 	 * value of osize is 0, length will be at least 1.
    234 	 */
    235 
    236 	if (osize < length) {
    237 		if (lblkno(fs, osize) < NDADDR &&
    238 		    lblkno(fs, osize) != lblkno(fs, length) &&
    239 		    blkroundup(fs, osize) != osize) {
    240 			error = ufs_balloc_range(ovp, osize,
    241 			    blkroundup(fs, osize) - osize, ap->a_cred, aflag);
    242 			if (error) {
    243 				return error;
    244 			}
    245 			if (ioflag & IO_SYNC) {
    246 				ovp->v_size = blkroundup(fs, osize);
    247 				simple_lock(&ovp->v_interlock);
    248 				VOP_PUTPAGES(ovp,
    249 				    trunc_page(osize & ~(fs->fs_bsize - 1)),
    250 				    round_page(ovp->v_size),
    251 				    PGO_CLEANIT | PGO_SYNCIO);
    252 			}
    253 		}
    254 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    255 		    aflag);
    256 		if (error) {
    257 			(void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
    258 			    ap->a_cred, ap->a_p);
    259 			return error;
    260 		}
    261 		uvm_vnp_setsize(ovp, length);
    262 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
    263 		KASSERT(ovp->v_size == oip->i_size);
    264 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
    265 	}
    266 
    267 	/*
    268 	 * When truncating a regular file down to a non-block-aligned size,
    269 	 * we must zero the part of last block which is past the new EOF.
    270 	 * We must synchronously flush the zeroed pages to disk
    271 	 * since the new pages will be invalidated as soon as we
    272 	 * inform the VM system of the new, smaller size.
    273 	 * We must do this before acquiring the GLOCK, since fetching
    274 	 * the pages will acquire the GLOCK internally.
    275 	 * So there is a window where another thread could see a whole
    276 	 * zeroed page past EOF, but that's life.
    277 	 */
    278 
    279 	offset = blkoff(fs, length);
    280 	if (ovp->v_type == VREG && length < osize && offset != 0) {
    281 		voff_t eoz;
    282 
    283 		error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
    284 		    aflag);
    285 		if (error) {
    286 			return error;
    287 		}
    288 		size = blksize(fs, oip, lblkno(fs, length));
    289 		eoz = MIN(lblktosize(fs, lblkno(fs, length)) + size, osize);
    290 		uvm_vnp_zerorange(ovp, length, eoz - length);
    291 		simple_lock(&ovp->v_interlock);
    292 		error = VOP_PUTPAGES(ovp, trunc_page(length), round_page(eoz),
    293 		    PGO_CLEANIT | PGO_DEACTIVATE | PGO_SYNCIO);
    294 		if (error) {
    295 			return error;
    296 		}
    297 	}
    298 
    299 	lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
    300 
    301 	if (DOINGSOFTDEP(ovp)) {
    302 		if (length > 0) {
    303 			/*
    304 			 * If a file is only partially truncated, then
    305 			 * we have to clean up the data structures
    306 			 * describing the allocation past the truncation
    307 			 * point. Finding and deallocating those structures
    308 			 * is a lot of work. Since partial truncation occurs
    309 			 * rarely, we solve the problem by syncing the file
    310 			 * so that it will have no data structures left.
    311 			 */
    312 			if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
    313 			    0, 0, ap->a_p)) != 0) {
    314 				lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    315 				return (error);
    316 			}
    317 			if (oip->i_flag & IN_SPACECOUNTED)
    318 				fs->fs_pendingblocks -= DIP(oip, blocks);
    319 		} else {
    320 			uvm_vnp_setsize(ovp, length);
    321 #ifdef QUOTA
    322  			(void) chkdq(oip, -DIP(oip, blocks), NOCRED, 0);
    323 #endif
    324 			softdep_setup_freeblocks(oip, length, 0);
    325 			(void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
    326 			lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    327 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
    328 			return (VOP_UPDATE(ovp, NULL, NULL, 0));
    329 		}
    330 	}
    331 	oip->i_size = length;
    332 	DIP_ASSIGN(oip, size, length);
    333 	uvm_vnp_setsize(ovp, length);
    334 	/*
    335 	 * Calculate index into inode's block list of
    336 	 * last direct and indirect blocks (if any)
    337 	 * which we want to keep.  Lastblock is -1 when
    338 	 * the file is truncated to 0.
    339 	 */
    340 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    341 	lastiblock[SINGLE] = lastblock - NDADDR;
    342 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    343 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    344 	nblocks = btodb(fs->fs_bsize);
    345 	/*
    346 	 * Update file and block pointers on disk before we start freeing
    347 	 * blocks.  If we crash before free'ing blocks below, the blocks
    348 	 * will be returned to the free list.  lastiblock values are also
    349 	 * normalized to -1 for calls to ffs_indirtrunc below.
    350 	 */
    351 	for (level = TRIPLE; level >= SINGLE; level--) {
    352 		blks[NDADDR + level] = DIP(oip, ib[level]);
    353 		if (lastiblock[level] < 0) {
    354 			DIP_ASSIGN(oip, ib[level], 0);
    355 			lastiblock[level] = -1;
    356 		}
    357 	}
    358 	for (i = 0; i < NDADDR; i++) {
    359 		blks[i] = DIP(oip, db[i]);
    360 		if (i > lastblock)
    361 			DIP_ASSIGN(oip, db[i], 0);
    362 	}
    363 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    364 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    365 	if (error && !allerror)
    366 		allerror = error;
    367 
    368 	/*
    369 	 * Having written the new inode to disk, save its new configuration
    370 	 * and put back the old block pointers long enough to process them.
    371 	 * Note that we save the new block configuration so we can check it
    372 	 * when we are done.
    373 	 */
    374 	for (i = 0; i < NDADDR; i++) {
    375 		bn = DIP(oip, db[i]);
    376 		DIP_ASSIGN(oip, db[i], blks[i]);
    377 		blks[i] = bn;
    378 	}
    379 	for (i = 0; i < NIADDR; i++) {
    380 		bn = DIP(oip, ib[i]);
    381 		DIP_ASSIGN(oip, ib[i], blks[NDADDR + i]);
    382 		blks[NDADDR + i] = bn;
    383 	}
    384 
    385 	oip->i_size = osize;
    386 	DIP_ASSIGN(oip, size, osize);
    387 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    388 	if (error && !allerror)
    389 		allerror = error;
    390 
    391 	/*
    392 	 * Indirect blocks first.
    393 	 */
    394 	indir_lbn[SINGLE] = -NDADDR;
    395 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    396 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    397 	for (level = TRIPLE; level >= SINGLE; level--) {
    398 		if (oip->i_ump->um_fstype == UFS1)
    399 			bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
    400 		else
    401 			bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
    402 		if (bn != 0) {
    403 			error = ffs_indirtrunc(oip, indir_lbn[level],
    404 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    405 			if (error)
    406 				allerror = error;
    407 			blocksreleased += count;
    408 			if (lastiblock[level] < 0) {
    409 				DIP_ASSIGN(oip, ib[level], 0);
    410 				ffs_blkfree(fs, oip->i_devvp, bn, fs->fs_bsize,
    411 				    oip->i_number);
    412 				blocksreleased += nblocks;
    413 			}
    414 		}
    415 		if (lastiblock[level] >= 0)
    416 			goto done;
    417 	}
    418 
    419 	/*
    420 	 * All whole direct blocks or frags.
    421 	 */
    422 	for (i = NDADDR - 1; i > lastblock; i--) {
    423 		long bsize;
    424 
    425 		if (oip->i_ump->um_fstype == UFS1)
    426 			bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
    427 		else
    428 			bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
    429 		if (bn == 0)
    430 			continue;
    431 		DIP_ASSIGN(oip, db[i], 0);
    432 		bsize = blksize(fs, oip, i);
    433 		ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
    434 		blocksreleased += btodb(bsize);
    435 	}
    436 	if (lastblock < 0)
    437 		goto done;
    438 
    439 	/*
    440 	 * Finally, look for a change in size of the
    441 	 * last direct block; release any frags.
    442 	 */
    443 	if (oip->i_ump->um_fstype == UFS1)
    444 		bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
    445 	else
    446 		bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
    447 	if (bn != 0) {
    448 		long oldspace, newspace;
    449 
    450 		/*
    451 		 * Calculate amount of space we're giving
    452 		 * back as old block size minus new block size.
    453 		 */
    454 		oldspace = blksize(fs, oip, lastblock);
    455 		oip->i_size = length;
    456 		DIP_ASSIGN(oip, size, length);
    457 		newspace = blksize(fs, oip, lastblock);
    458 		if (newspace == 0)
    459 			panic("itrunc: newspace");
    460 		if (oldspace - newspace > 0) {
    461 			/*
    462 			 * Block number of space to be free'd is
    463 			 * the old block # plus the number of frags
    464 			 * required for the storage we're keeping.
    465 			 */
    466 			bn += numfrags(fs, newspace);
    467 			ffs_blkfree(fs, oip->i_devvp, bn, oldspace - newspace,
    468 			    oip->i_number);
    469 			blocksreleased += btodb(oldspace - newspace);
    470 		}
    471 	}
    472 
    473 done:
    474 #ifdef DIAGNOSTIC
    475 	for (level = SINGLE; level <= TRIPLE; level++)
    476 		if (blks[NDADDR + level] != DIP(oip, ib[level]))
    477 			panic("itrunc1");
    478 	for (i = 0; i < NDADDR; i++)
    479 		if (blks[i] != DIP(oip, db[i]))
    480 			panic("itrunc2");
    481 	if (length == 0 &&
    482 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    483 		panic("itrunc3");
    484 #endif /* DIAGNOSTIC */
    485 	/*
    486 	 * Put back the real size.
    487 	 */
    488 	oip->i_size = length;
    489 	DIP_ASSIGN(oip, size, length);
    490 	DIP_ADD(oip, blocks, -blocksreleased);
    491 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    492 	oip->i_flag |= IN_CHANGE;
    493 #ifdef QUOTA
    494 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    495 #endif
    496 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
    497 	return (allerror);
    498 }
    499 
    500 /*
    501  * Release blocks associated with the inode ip and stored in the indirect
    502  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    503  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    504  * and recursive calls to indirtrunc must be used to cleanse other indirect
    505  * blocks.
    506  *
    507  * NB: triple indirect blocks are untested.
    508  */
    509 static int
    510 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    511 	struct inode *ip;
    512 	daddr_t lbn, lastbn;
    513 	daddr_t dbn;
    514 	int level;
    515 	int64_t *countp;
    516 {
    517 	int i;
    518 	struct buf *bp;
    519 	struct fs *fs = ip->i_fs;
    520 	int32_t *bap1 = NULL;
    521 	int64_t *bap2 = NULL;
    522 	struct vnode *vp;
    523 	daddr_t nb, nlbn, last;
    524 	char *copy = NULL;
    525 	int64_t blkcount, factor, blocksreleased = 0;
    526 	int nblocks;
    527 	int error = 0, allerror = 0;
    528 #ifdef FFS_EI
    529 	const int needswap = UFS_FSNEEDSWAP(fs);
    530 #endif
    531 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
    532 	    ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
    533 #define BAP_ASSIGN(ip, i, value)					\
    534 	do {								\
    535 		if ((ip)->i_ump->um_fstype == UFS1)			\
    536 			bap1[i] = (value);				\
    537 		else							\
    538 			bap2[i] = (value);				\
    539 	} while(0)
    540 
    541 	/*
    542 	 * Calculate index in current block of last
    543 	 * block to be kept.  -1 indicates the entire
    544 	 * block so we need not calculate the index.
    545 	 */
    546 	factor = 1;
    547 	for (i = SINGLE; i < level; i++)
    548 		factor *= NINDIR(fs);
    549 	last = lastbn;
    550 	if (lastbn > 0)
    551 		last /= factor;
    552 	nblocks = btodb(fs->fs_bsize);
    553 	/*
    554 	 * Get buffer of block pointers, zero those entries corresponding
    555 	 * to blocks to be free'd, and update on disk copy first.  Since
    556 	 * double(triple) indirect before single(double) indirect, calls
    557 	 * to bmap on these blocks will fail.  However, we already have
    558 	 * the on disk address, so we have to set the b_blkno field
    559 	 * explicitly instead of letting bread do everything for us.
    560 	 */
    561 	vp = ITOV(ip);
    562 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    563 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    564 		/* Braces must be here in case trace evaluates to nothing. */
    565 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    566 	} else {
    567 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    568 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    569 		bp->b_flags |= B_READ;
    570 		if (bp->b_bcount > bp->b_bufsize)
    571 			panic("ffs_indirtrunc: bad buffer size");
    572 		bp->b_blkno = dbn;
    573 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    574 		VOP_STRATEGY(vp, bp);
    575 		error = biowait(bp);
    576 	}
    577 	if (error) {
    578 		brelse(bp);
    579 		*countp = 0;
    580 		return (error);
    581 	}
    582 
    583 	if (ip->i_ump->um_fstype == UFS1)
    584 		bap1 = (int32_t *)bp->b_data;
    585 	else
    586 		bap2 = (int64_t *)bp->b_data;
    587 	if (lastbn >= 0) {
    588 		copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
    589 		memcpy((caddr_t)copy, bp->b_data, (u_int)fs->fs_bsize);
    590 		for (i = last + 1; i < NINDIR(fs); i++)
    591 			BAP_ASSIGN(ip, i, 0);
    592 		error = bwrite(bp);
    593 		if (error)
    594 			allerror = error;
    595 		if (ip->i_ump->um_fstype == UFS1)
    596 			bap1 = (int32_t *)copy;
    597 		else
    598 			bap2 = (int64_t *)copy;
    599 	}
    600 
    601 	/*
    602 	 * Recursively free totally unused blocks.
    603 	 */
    604 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    605 	    i--, nlbn += factor) {
    606 		nb = RBAP(ip, i);
    607 		if (nb == 0)
    608 			continue;
    609 		if (level > SINGLE) {
    610 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    611 					       (daddr_t)-1, level - 1,
    612 					       &blkcount);
    613 			if (error)
    614 				allerror = error;
    615 			blocksreleased += blkcount;
    616 		}
    617 		ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, ip->i_number);
    618 		blocksreleased += nblocks;
    619 	}
    620 
    621 	/*
    622 	 * Recursively free last partial block.
    623 	 */
    624 	if (level > SINGLE && lastbn >= 0) {
    625 		last = lastbn % factor;
    626 		nb = RBAP(ip, i);
    627 		if (nb != 0) {
    628 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    629 					       last, level - 1, &blkcount);
    630 			if (error)
    631 				allerror = error;
    632 			blocksreleased += blkcount;
    633 		}
    634 	}
    635 
    636 	if (copy != NULL) {
    637 		FREE(copy, M_TEMP);
    638 	} else {
    639 		bp->b_flags |= B_INVAL;
    640 		brelse(bp);
    641 	}
    642 
    643 	*countp = blocksreleased;
    644 	return (allerror);
    645 }
    646