- return (allerror);
-}
-
-/*
- * Release blocks associated with the inode ip and
- * stored in the indirect block bn. Blocks are free'd
- * in LIFO order up to (but not including) lastbn. If
- * level is greater than SINGLE, the block is an indirect
- * block and recursive calls to indirtrunc must be used to
- * cleanse other indirect blocks.
- *
- * NB: triple indirect blocks are untested.
- */
-indirtrunc(ip, bn, lastbn, level, countp)
- register struct inode *ip;
- daddr_t bn, lastbn;
- int level;
- long *countp;
-{
- register int i;
- struct buf *bp;
- register struct fs *fs = ip->i_fs;
- register daddr_t *bap;
- daddr_t *copy, nb, last;
- long blkcount, factor;
- int nblocks, blocksreleased = 0;
- int error, allerror = 0;
-
- /*
- * Calculate index in current block of last
- * block to be kept. -1 indicates the entire
- * block so we need not calculate the index.
- */
- factor = 1;
- for (i = SINGLE; i < level; i++)
- factor *= NINDIR(fs);
- last = lastbn;
- if (lastbn > 0)
- last /= factor;
- nblocks = btodb(fs->fs_bsize);
- /*
- * Get buffer of block pointers, zero those
- * entries corresponding to blocks to be free'd,
- * and update on disk copy first.
- */
-#ifdef SECSIZE
- bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize,
- fs->fs_dbsize);
-#else SECSIZE
- error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
- NOCRED, &bp);
- if (error) {
- brelse(bp);
- *countp = 0;
- return (error);
- }
- bap = bp->b_un.b_daddr;
- MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
- bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
- bzero((caddr_t)&bap[last + 1],
- (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
- if (last == -1)
- bp->b_flags |= B_INVAL;
- error = bwrite(bp);
- if (error)
- allerror = error;
- bap = copy;
-
- /*
- * Recursively free totally unused blocks.
- */
- for (i = NINDIR(fs) - 1; i > last; i--) {
- nb = bap[i];
- if (nb == 0)
- continue;
- if (level > SINGLE) {
- error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
- &blkcount);
- if (error)
- allerror = error;
- blocksreleased += blkcount;
+ default: /* Chain of indirect blocks. */
+ ap = a + --depth;
+ if (ap->in_off > 0 && lbn != lastblock) {
+ lbn -= ap->in_off < lbn - lastblock ?
+ ap->in_off : lbn - lastblock;
+ break;
+ }
+ for (; depth && (ap->in_off == 0 || lbn == lastblock);
+ --ap, --depth) {
+ /*
+ * XXX
+ * The indirect block may not yet exist, so
+ * bread will create one just so we can free
+ * it.
+ */
+ if (bread(vp,
+ ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
+ panic("lfs_truncate: bread bno %d",
+ ap->in_lbn);
+ daddrp = bp->b_un.b_daddr + ap->in_off;
+ for (i = ap->in_off;
+ i++ <= a_end[depth].in_off;) {
+ daddr = *daddrp++;
+ SEGDEC;
+ }
+ a_end[depth].in_off=NINDIR(fs)-1;
+ if (ap->in_off > 0 && lbn == lastblock) {
+ bzero(bp->b_un.b_daddr + ap->in_off,
+ fs->lfs_bsize -
+ ap->in_off * sizeof(daddr_t));
+ LFS_UBWRITE(bp);
+ } else
+ brelse (bp);
+ }
+ if (a[1].in_off == 0) {
+ off = a[0].in_off;
+ daddr = ip->i_ib[off];
+ SEGDEC;
+ ip->i_ib[off] = 0;
+ }
+ if (lbn == lastblock)
+ --lbn;
+ else {
+ lbn -= NINDIR(fs);
+ if (lbn < lastblock)
+ lbn = lastblock;
+ }