Home | History | Annotate | Line # | Download | only in ffs
ffs_inode.c revision 1.55
      1 /*	$NetBSD: ffs_inode.c,v 1.55 2003/04/02 10:39:37 fvdl 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.55 2003/04/02 10:39:37 fvdl 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 proc *a_p;
    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(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_p);
    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_p)) != 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_p, 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 	if (oip->i_ump->um_fstype != UFS2 &&
    333 	    oip->i_fs->fs_magic == FS_UFS2_MAGIC)
    334 		panic("fstype inconsistency (%ld)", oip->i_ump->um_fstype);
    335 	oip->i_size = length;
    336 	DIP(oip, size) = length;
    337 	uvm_vnp_setsize(ovp, length);
    338 	/*
    339 	 * Calculate index into inode's block list of
    340 	 * last direct and indirect blocks (if any)
    341 	 * which we want to keep.  Lastblock is -1 when
    342 	 * the file is truncated to 0.
    343 	 */
    344 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
    345 	lastiblock[SINGLE] = lastblock - NDADDR;
    346 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
    347 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
    348 	nblocks = btodb(fs->fs_bsize);
    349 	/*
    350 	 * Update file and block pointers on disk before we start freeing
    351 	 * blocks.  If we crash before free'ing blocks below, the blocks
    352 	 * will be returned to the free list.  lastiblock values are also
    353 	 * normalized to -1 for calls to ffs_indirtrunc below.
    354 	 */
    355 	for (level = TRIPLE; level >= SINGLE; level--) {
    356 		oldblks[NDADDR + level] = DIP(oip, ib[level]);
    357 		if (lastiblock[level] < 0) {
    358 			DIP(oip, ib[level]) = 0;
    359 			lastiblock[level] = -1;
    360 		}
    361 	}
    362 	for (i = 0; i < NDADDR; i++) {
    363 		oldblks[i] = DIP(oip, db[i]);
    364 		if (i > lastblock)
    365 			DIP(oip, db[i]) = 0;
    366 	}
    367 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
    368 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
    369 	if (error && !allerror)
    370 		allerror = error;
    371 
    372 	/*
    373 	 * Having written the new inode to disk, save its new configuration
    374 	 * and put back the old block pointers long enough to process them.
    375 	 * Note that we save the new block configuration so we can check it
    376 	 * when we are done.
    377 	 */
    378 	for (i = 0; i < NDADDR; i++) {
    379 		newblks[i] = DIP(oip, db[i]);
    380 		DIP(oip, db[i]) = oldblks[i];
    381 	}
    382 	for (i = 0; i < NIADDR; i++) {
    383 		newblks[NDADDR + i] = DIP(oip, ib[i]);
    384 		DIP(oip, ib[i]) = oldblks[NDADDR + i];
    385 	}
    386 
    387 	if (oip->i_ump->um_fstype != UFS2 &&
    388 	    oip->i_fs->fs_magic == FS_UFS2_MAGIC)
    389 		panic("fstype inconsistency (%ld)", oip->i_ump->um_fstype);
    390 	oip->i_size = osize;
    391 	DIP(oip, size) = osize;
    392 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
    393 	if (error && !allerror)
    394 		allerror = error;
    395 
    396 	/*
    397 	 * Indirect blocks first.
    398 	 */
    399 	indir_lbn[SINGLE] = -NDADDR;
    400 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
    401 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
    402 	for (level = TRIPLE; level >= SINGLE; level--) {
    403 		if (oip->i_ump->um_fstype == UFS1)
    404 			bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
    405 		else
    406 			bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
    407 		if (bn != 0) {
    408 			error = ffs_indirtrunc(oip, indir_lbn[level],
    409 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
    410 			if (error)
    411 				allerror = error;
    412 			blocksreleased += count;
    413 			if (lastiblock[level] < 0) {
    414 				DIP(oip, ib[level]) = 0;
    415 				ffs_blkfree(oip, bn, fs->fs_bsize);
    416 				blocksreleased += nblocks;
    417 			}
    418 		}
    419 		if (lastiblock[level] >= 0)
    420 			goto done;
    421 	}
    422 
    423 	/*
    424 	 * All whole direct blocks or frags.
    425 	 */
    426 	for (i = NDADDR - 1; i > lastblock; i--) {
    427 		long bsize;
    428 
    429 		if (oip->i_ump->um_fstype == UFS1)
    430 			bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
    431 		else
    432 			bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
    433 		if (bn == 0)
    434 			continue;
    435 		DIP(oip, db[i]) = 0;
    436 		bsize = blksize(fs, oip, i);
    437 		ffs_blkfree(oip, bn, bsize);
    438 		blocksreleased += btodb(bsize);
    439 	}
    440 	if (lastblock < 0)
    441 		goto done;
    442 
    443 	/*
    444 	 * Finally, look for a change in size of the
    445 	 * last direct block; release any frags.
    446 	 */
    447 	if (oip->i_ump->um_fstype == UFS1)
    448 		bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
    449 	else
    450 		bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
    451 	if (bn != 0) {
    452 		long oldspace, newspace;
    453 
    454 		/*
    455 		 * Calculate amount of space we're giving
    456 		 * back as old block size minus new block size.
    457 		 */
    458 		oldspace = blksize(fs, oip, lastblock);
    459 	if (oip->i_ump->um_fstype != UFS2 &&
    460 	    oip->i_fs->fs_magic == FS_UFS2_MAGIC)
    461 		panic("fstype inconsistency (%ld)", oip->i_ump->um_fstype);
    462 		oip->i_size = length;
    463 		DIP(oip, size) = length;
    464 		newspace = blksize(fs, oip, lastblock);
    465 		if (newspace == 0)
    466 			panic("itrunc: newspace");
    467 		if (oldspace - newspace > 0) {
    468 			/*
    469 			 * Block number of space to be free'd is
    470 			 * the old block # plus the number of frags
    471 			 * required for the storage we're keeping.
    472 			 */
    473 			bn += numfrags(fs, newspace);
    474 			ffs_blkfree(oip, bn, oldspace - newspace);
    475 			blocksreleased += btodb(oldspace - newspace);
    476 		}
    477 	}
    478 
    479 done:
    480 #ifdef DIAGNOSTIC
    481 	for (level = SINGLE; level <= TRIPLE; level++)
    482 		if (newblks[NDADDR + level] != DIP(oip, ib[level]))
    483 			panic("itrunc1");
    484 	for (i = 0; i < NDADDR; i++)
    485 		if (newblks[i] != DIP(oip, db[i]))
    486 			panic("itrunc2");
    487 	if (length == 0 &&
    488 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
    489 		panic("itrunc3");
    490 #endif /* DIAGNOSTIC */
    491 	/*
    492 	 * Put back the real size.
    493 	 */
    494 	if (oip->i_ump->um_fstype != UFS2 &&
    495 	    oip->i_fs->fs_magic == FS_UFS2_MAGIC)
    496 		panic("fstype inconsistency (%ld)", oip->i_ump->um_fstype);
    497 	oip->i_size = length;
    498 	DIP(oip, size) = length;
    499 	DIP(oip, blocks) -= blocksreleased;
    500 	lockmgr(&gp->g_glock, LK_RELEASE, NULL);
    501 	oip->i_flag |= IN_CHANGE;
    502 #ifdef QUOTA
    503 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
    504 #endif
    505 	KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
    506 	return (allerror);
    507 }
    508 
    509 /*
    510  * Release blocks associated with the inode ip and stored in the indirect
    511  * block bn.  Blocks are free'd in LIFO order up to (but not including)
    512  * lastbn.  If level is greater than SINGLE, the block is an indirect block
    513  * and recursive calls to indirtrunc must be used to cleanse other indirect
    514  * blocks.
    515  *
    516  * NB: triple indirect blocks are untested.
    517  */
    518 static int
    519 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
    520 	struct inode *ip;
    521 	daddr_t lbn, lastbn;
    522 	daddr_t dbn;
    523 	int level;
    524 	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(ip, i) (((ip)->i_ump->um_fstype == UFS1) ?  bap1[i] : bap2[i])
    543 
    544 	/*
    545 	 * Calculate index in current block of last
    546 	 * block to be kept.  -1 indicates the entire
    547 	 * block so we need not calculate the index.
    548 	 */
    549 	factor = 1;
    550 	for (i = SINGLE; i < level; i++)
    551 		factor *= NINDIR(fs);
    552 	last = lastbn;
    553 	if (lastbn > 0)
    554 		last /= factor;
    555 	nblocks = btodb(fs->fs_bsize);
    556 	/*
    557 	 * Get buffer of block pointers, zero those entries corresponding
    558 	 * to blocks to be free'd, and update on disk copy first.  Since
    559 	 * double(triple) indirect before single(double) indirect, calls
    560 	 * to bmap on these blocks will fail.  However, we already have
    561 	 * the on disk address, so we have to set the b_blkno field
    562 	 * explicitly instead of letting bread do everything for us.
    563 	 */
    564 	vp = ITOV(ip);
    565 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
    566 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
    567 		/* Braces must be here in case trace evaluates to nothing. */
    568 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
    569 	} else {
    570 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
    571 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
    572 		bp->b_flags |= B_READ;
    573 		if (bp->b_bcount > bp->b_bufsize)
    574 			panic("ffs_indirtrunc: bad buffer size");
    575 		bp->b_blkno = dbn;
    576 		VOP_STRATEGY(bp);
    577 		error = biowait(bp);
    578 	}
    579 	if (error) {
    580 		brelse(bp);
    581 		*countp = 0;
    582 		return (error);
    583 	}
    584 
    585 	if (ip->i_ump->um_fstype == UFS1)
    586 		bap1 = (int32_t *)bp->b_data;
    587 	else
    588 		bap2 = (int64_t *)bp->b_data;
    589 	if (lastbn >= 0) {
    590 		copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
    591 		memcpy((caddr_t)copy, bp->b_data, (u_int)fs->fs_bsize);
    592 		for (i = last + 1; i < NINDIR(fs); i++)
    593 			BAP(ip, i) = 0;
    594 		error = bwrite(bp);
    595 		if (error)
    596 			allerror = error;
    597 		if (ip->i_ump->um_fstype == UFS1)
    598 			bap1 = (int32_t *)copy;
    599 		else
    600 			bap2 = (int64_t *)copy;
    601 	}
    602 
    603 	/*
    604 	 * Recursively free totally unused blocks.
    605 	 */
    606 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
    607 	    i--, nlbn += factor) {
    608 		nb = RBAP(ip, i);
    609 		if (nb == 0)
    610 			continue;
    611 		if (level > SINGLE) {
    612 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    613 					       (daddr_t)-1, level - 1,
    614 					       &blkcount);
    615 			if (error)
    616 				allerror = error;
    617 			blocksreleased += blkcount;
    618 		}
    619 		ffs_blkfree(ip, nb, fs->fs_bsize);
    620 		blocksreleased += nblocks;
    621 	}
    622 
    623 	/*
    624 	 * Recursively free last partial block.
    625 	 */
    626 	if (level > SINGLE && lastbn >= 0) {
    627 		last = lastbn % factor;
    628 		nb = RBAP(ip, i);
    629 		if (nb != 0) {
    630 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
    631 					       last, level - 1, &blkcount);
    632 			if (error)
    633 				allerror = error;
    634 			blocksreleased += blkcount;
    635 		}
    636 	}
    637 
    638 	if (copy != NULL) {
    639 		FREE(copy, M_TEMP);
    640 	} else {
    641 		bp->b_flags |= B_INVAL;
    642 		brelse(bp);
    643 	}
    644 
    645 	*countp = blocksreleased;
    646 	return (allerror);
    647 }
    648