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