187153602Smckusick /*
22b768bd7Sbostic * Copyright (c) 1982, 1986, 1989, 1993
32b768bd7Sbostic * The Regents of the University of California. All rights reserved.
487153602Smckusick *
5dd22d480Sbostic * %sccs.include.redist.c%
63c0618ecSmckusick *
7*dd75f133Smckusick * @(#)ffs_inode.c 8.13 (Berkeley) 04/21/95
887153602Smckusick */
9d4b8d8d2Sbill
10f56b3bb2Sbostic #include <sys/param.h>
11f56b3bb2Sbostic #include <sys/systm.h>
12f56b3bb2Sbostic #include <sys/mount.h>
13f56b3bb2Sbostic #include <sys/proc.h>
14f56b3bb2Sbostic #include <sys/file.h>
15f56b3bb2Sbostic #include <sys/buf.h>
16f56b3bb2Sbostic #include <sys/vnode.h>
17f56b3bb2Sbostic #include <sys/kernel.h>
18f56b3bb2Sbostic #include <sys/malloc.h>
19ad8b6216Smargo #include <sys/trace.h>
20ad8b6216Smargo #include <sys/resourcevar.h>
21d4b8d8d2Sbill
22d6ef23c7Smckusick #include <vm/vm.h>
23d6ef23c7Smckusick
24f56b3bb2Sbostic #include <ufs/ufs/quota.h>
25f56b3bb2Sbostic #include <ufs/ufs/inode.h>
26f56b3bb2Sbostic #include <ufs/ufs/ufsmount.h>
27f56b3bb2Sbostic #include <ufs/ufs/ufs_extern.h>
2826a59bf8Skarels
29f56b3bb2Sbostic #include <ufs/ffs/fs.h>
30f56b3bb2Sbostic #include <ufs/ffs/ffs_extern.h>
3121e88249Skre
322c9a935dSmckusick static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
332c9a935dSmckusick ufs_daddr_t, int, long *));
3421e88249Skre
3521e88249Skre /*
3662b96425Sbostic * Update the access, modified, and inode change times as specified by the
3762b96425Sbostic * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
3862b96425Sbostic * used to specify that the inode needs to be updated but that the times have
3962b96425Sbostic * already been set. The access and modified times are taken from the second
4062b96425Sbostic * and third parameters; the inode change time is always taken from the current
4162b96425Sbostic * time. If waitfor is set, then wait for the disk write of the inode to
4262b96425Sbostic * complete.
43d4b8d8d2Sbill */
44f56b3bb2Sbostic int
ffs_update(ap)4542026a80Sheideman ffs_update(ap)
463a43d60fSmckusick struct vop_update_args /* {
473a43d60fSmckusick struct vnode *a_vp;
4826ea9362Sbostic struct timeval *a_access;
4926ea9362Sbostic struct timeval *a_modify;
503a43d60fSmckusick int a_waitfor;
513a43d60fSmckusick } */ *ap;
52d4b8d8d2Sbill {
5326ea9362Sbostic register struct fs *fs;
543c0618ecSmckusick struct buf *bp;
552faef195Smckusick struct inode *ip;
563c0618ecSmckusick int error;
57d4b8d8d2Sbill
5898a5cedeStorek ip = VTOI(ap->a_vp);
59d719e8a1Smckusick if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) {
60cb55a29dSbostic ip->i_flag &=
61cb55a29dSbostic ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
623c0618ecSmckusick return (0);
63d719e8a1Smckusick }
64cb55a29dSbostic if ((ip->i_flag &
65cb55a29dSbostic (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
662faef195Smckusick return (0);
67cb55a29dSbostic if (ip->i_flag & IN_ACCESS)
682c9a935dSmckusick ip->i_atime = ap->a_access->tv_sec;
69cb55a29dSbostic if (ip->i_flag & IN_UPDATE) {
702c9a935dSmckusick ip->i_mtime = ap->a_modify->tv_sec;
719d7d661cSmckusick ip->i_modrev++;
7234d99fc1Smckusick }
73cb55a29dSbostic if (ip->i_flag & IN_CHANGE)
742c9a935dSmckusick ip->i_ctime = time.tv_sec;
75cb55a29dSbostic ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
763faa2d20Smckusick fs = ip->i_fs;
77ee6a67e5Smckusick /*
78ee6a67e5Smckusick * Ensure that uid and gid are correct. This is a temporary
79ee6a67e5Smckusick * fix until fsck has been changed to do the update.
80ee6a67e5Smckusick */
813faa2d20Smckusick if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
823faa2d20Smckusick ip->i_din.di_ouid = ip->i_uid; /* XXX */
833faa2d20Smckusick ip->i_din.di_ogid = ip->i_gid; /* XXX */
843faa2d20Smckusick } /* XXX */
85cb55a29dSbostic if (error = bread(ip->i_devvp,
86cb55a29dSbostic fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
872faef195Smckusick (int)fs->fs_bsize, NOCRED, &bp)) {
882faef195Smckusick brelse(bp);
892faef195Smckusick return (error);
902faef195Smckusick }
91cb55a29dSbostic *((struct dinode *)bp->b_data +
92cb55a29dSbostic ino_to_fsbo(fs, ip->i_number)) = ip->i_din;
93fab02d96Smckusick if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
943c0618ecSmckusick return (bwrite(bp));
952faef195Smckusick else {
96d4b8d8d2Sbill bdwrite(bp);
973c0618ecSmckusick return (0);
98d4b8d8d2Sbill }
99d4b8d8d2Sbill }
100d4b8d8d2Sbill
1013f227b83Ssam #define SINGLE 0 /* index of single indirect block */
1023f227b83Ssam #define DOUBLE 1 /* index of double indirect block */
1033f227b83Ssam #define TRIPLE 2 /* index of triple indirect block */
104d4b8d8d2Sbill /*
105cb55a29dSbostic * Truncate the inode oip to at most length size, freeing the
106cb55a29dSbostic * disk blocks.
107d4b8d8d2Sbill */
10842026a80Sheideman ffs_truncate(ap)
1093a43d60fSmckusick struct vop_truncate_args /* {
1103a43d60fSmckusick struct vnode *a_vp;
1113a43d60fSmckusick off_t a_length;
1123a43d60fSmckusick int a_flags;
1133a43d60fSmckusick struct ucred *a_cred;
1143a43d60fSmckusick struct proc *a_p;
1153a43d60fSmckusick } */ *ap;
116d4b8d8d2Sbill {
1172dd35649Sheideman register struct vnode *ovp = ap->a_vp;
1182c9a935dSmckusick ufs_daddr_t lastblock;
1192faef195Smckusick register struct inode *oip;
1202c9a935dSmckusick ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
1212c9a935dSmckusick ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
1223a43d60fSmckusick off_t length = ap->a_length;
123f33622a8Smckusic register struct fs *fs;
12491a16382Smckusick struct buf *bp;
1255aa03ac6Smckusick int offset, size, level;
1263bcf2b3eSmargo long count, nblocks, vflags, blocksreleased = 0;
127100a94d5Smckusick struct timeval tv;
12891a16382Smckusick register int i;
12929b6fa5cSmckusick int aflags, error, allerror;
1305aa03ac6Smckusick off_t osize;
131e0df3eb1Ssam
132acfd7a5aSmckusick if (length < 0)
133f2840fbaSmckusick return (EINVAL);
134acfd7a5aSmckusick oip = VTOI(ovp);
135100a94d5Smckusick tv = time;
136bbf65f7cSmckusick if (ovp->v_type == VLNK &&
137bbf65f7cSmckusick oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
138db3951f6Smckusick #ifdef DIAGNOSTIC
1393a43d60fSmckusick if (length != 0)
140db3951f6Smckusick panic("ffs_truncate: partial truncate of symlink");
141db3951f6Smckusick #endif
142db3951f6Smckusick bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
143db3951f6Smckusick oip->i_size = 0;
144cb55a29dSbostic oip->i_flag |= IN_CHANGE | IN_UPDATE;
145100a94d5Smckusick return (VOP_UPDATE(ovp, &tv, &tv, 1));
146db3951f6Smckusick }
14778940e30Smckusick if (oip->i_size == length) {
148cb55a29dSbostic oip->i_flag |= IN_CHANGE | IN_UPDATE;
1494e87ccd9Smckusick return (VOP_UPDATE(ovp, &tv, &tv, 0));
15088bdc9f6Ssam }
15160b79ed9Smckusick #ifdef QUOTA
15260b79ed9Smckusick if (error = getinoquota(oip))
15360b79ed9Smckusick return (error);
15460b79ed9Smckusick #endif
155acfd7a5aSmckusick fs = oip->i_fs;
15660b79ed9Smckusick osize = oip->i_size;
1579c4e8a6fSbill /*
15860b79ed9Smckusick * Lengthen the size of the file. We must ensure that the
15960b79ed9Smckusick * last byte of the file is allocated. Since the smallest
160acfd7a5aSmckusick * value of osize is 0, length will be at least 1.
16160b79ed9Smckusick */
16260b79ed9Smckusick if (osize < length) {
163acfd7a5aSmckusick if (length > fs->fs_maxfilesize)
164acfd7a5aSmckusick return (EFBIG);
16560b79ed9Smckusick offset = blkoff(fs, length - 1);
16660b79ed9Smckusick lbn = lblkno(fs, length - 1);
16760b79ed9Smckusick aflags = B_CLRBUF;
16860b79ed9Smckusick if (ap->a_flags & IO_SYNC)
16960b79ed9Smckusick aflags |= B_SYNC;
17060b79ed9Smckusick if (error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
17160b79ed9Smckusick aflags))
17260b79ed9Smckusick return (error);
17360b79ed9Smckusick oip->i_size = length;
174*dd75f133Smckusick vnode_pager_setsize(ovp, (u_long)length);
17560b79ed9Smckusick (void) vnode_pager_uncache(ovp);
176f83d5080Smckusick if (aflags & B_SYNC)
17760b79ed9Smckusick bwrite(bp);
17860b79ed9Smckusick else
17960b79ed9Smckusick bawrite(bp);
180cb55a29dSbostic oip->i_flag |= IN_CHANGE | IN_UPDATE;
18160b79ed9Smckusick return (VOP_UPDATE(ovp, &tv, &tv, 1));
18260b79ed9Smckusick }
18360b79ed9Smckusick /*
18460b79ed9Smckusick * Shorten the size of the file. If the file is not being
18591a16382Smckusick * truncated to a block boundry, the contents of the
18691a16382Smckusick * partial block following the end of the file must be
18791a16382Smckusick * zero'ed in case it ever become accessable again because
18891a16382Smckusick * of subsequent file growth.
18991a16382Smckusick */
1903a43d60fSmckusick offset = blkoff(fs, length);
19160b79ed9Smckusick if (offset == 0) {
1923a43d60fSmckusick oip->i_size = length;
19391a16382Smckusick } else {
1943a43d60fSmckusick lbn = lblkno(fs, length);
19529b6fa5cSmckusick aflags = B_CLRBUF;
19618e09e81Sheideman if (ap->a_flags & IO_SYNC)
19729b6fa5cSmckusick aflags |= B_SYNC;
19860b79ed9Smckusick if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp,
19960b79ed9Smckusick aflags))
2003c0618ecSmckusick return (error);
2013a43d60fSmckusick oip->i_size = length;
20291a16382Smckusick size = blksize(fs, oip, lbn);
2032dd35649Sheideman (void) vnode_pager_uncache(ovp);
20462b96425Sbostic bzero((char *)bp->b_data + offset, (u_int)(size - offset));
2051fcae812Smckusick allocbuf(bp, size);
206f83d5080Smckusick if (aflags & B_SYNC)
20729b6fa5cSmckusick bwrite(bp);
20829b6fa5cSmckusick else
209214e4f3dSmckusick bawrite(bp);
21078940e30Smckusick }
211*dd75f133Smckusick vnode_pager_setsize(ovp, (u_long)length);
21278940e30Smckusick /*
21378940e30Smckusick * Calculate index into inode's block list of
21478940e30Smckusick * last direct and indirect blocks (if any)
21578940e30Smckusick * which we want to keep. Lastblock is -1 when
21678940e30Smckusick * the file is truncated to 0.
21778940e30Smckusick */
21878940e30Smckusick lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
21978940e30Smckusick lastiblock[SINGLE] = lastblock - NDADDR;
22078940e30Smckusick lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
22178940e30Smckusick lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
22278940e30Smckusick nblocks = btodb(fs->fs_bsize);
22391a16382Smckusick /*
224f56b3bb2Sbostic * Update file and block pointers on disk before we start freeing
225f56b3bb2Sbostic * blocks. If we crash before free'ing blocks below, the blocks
226f56b3bb2Sbostic * will be returned to the free list. lastiblock values are also
227f56b3bb2Sbostic * normalized to -1 for calls to ffs_indirtrunc below.
228f33622a8Smckusic */
229214e4f3dSmckusick bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
2303f227b83Ssam for (level = TRIPLE; level >= SINGLE; level--)
2313f227b83Ssam if (lastiblock[level] < 0) {
2323f227b83Ssam oip->i_ib[level] = 0;
2333f227b83Ssam lastiblock[level] = -1;
2343f227b83Ssam }
2353f227b83Ssam for (i = NDADDR - 1; i > lastblock; i--)
2363f227b83Ssam oip->i_db[i] = 0;
237cb55a29dSbostic oip->i_flag |= IN_CHANGE | IN_UPDATE;
238100a94d5Smckusick if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
239ab038e32Smckusick allerror = error;
240214e4f3dSmckusick /*
241214e4f3dSmckusick * Having written the new inode to disk, save its new configuration
242214e4f3dSmckusick * and put back the old block pointers long enough to process them.
243214e4f3dSmckusick * Note that we save the new block configuration so we can check it
244214e4f3dSmckusick * when we are done.
245214e4f3dSmckusick */
246214e4f3dSmckusick bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
247214e4f3dSmckusick bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
248214e4f3dSmckusick oip->i_size = osize;
249214e4f3dSmckusick vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
250214e4f3dSmckusick allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
2513f227b83Ssam
252f33622a8Smckusic /*
2533f227b83Ssam * Indirect blocks first.
254f33622a8Smckusic */
2553bcf2b3eSmargo indir_lbn[SINGLE] = -NDADDR;
2563bcf2b3eSmargo indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
2573bcf2b3eSmargo indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
2583f227b83Ssam for (level = TRIPLE; level >= SINGLE; level--) {
259214e4f3dSmckusick bn = oip->i_ib[level];
2603f227b83Ssam if (bn != 0) {
261214e4f3dSmckusick error = ffs_indirtrunc(oip, indir_lbn[level],
262ad8b6216Smargo fsbtodb(fs, bn), lastiblock[level], level, &count);
2633c0618ecSmckusick if (error)
2643c0618ecSmckusick allerror = error;
2653c0618ecSmckusick blocksreleased += count;
2663f227b83Ssam if (lastiblock[level] < 0) {
267214e4f3dSmckusick oip->i_ib[level] = 0;
268214e4f3dSmckusick ffs_blkfree(oip, bn, fs->fs_bsize);
269e0df3eb1Ssam blocksreleased += nblocks;
270e0df3eb1Ssam }
271e0df3eb1Ssam }
2723f227b83Ssam if (lastiblock[level] >= 0)
273e0df3eb1Ssam goto done;
274e0df3eb1Ssam }
2753f227b83Ssam
276f33622a8Smckusic /*
2773f227b83Ssam * All whole direct blocks or frags.
278f33622a8Smckusic */
279e0df3eb1Ssam for (i = NDADDR - 1; i > lastblock; i--) {
2805aa03ac6Smckusick register long bsize;
281e0df3eb1Ssam
282214e4f3dSmckusick bn = oip->i_db[i];
283e0df3eb1Ssam if (bn == 0)
284d4b8d8d2Sbill continue;
285214e4f3dSmckusick oip->i_db[i] = 0;
286214e4f3dSmckusick bsize = blksize(fs, oip, i);
287214e4f3dSmckusick ffs_blkfree(oip, bn, bsize);
288c35478d1Sbloom blocksreleased += btodb(bsize);
289d4b8d8d2Sbill }
2903f227b83Ssam if (lastblock < 0)
2913f227b83Ssam goto done;
2923f227b83Ssam
2939c4e8a6fSbill /*
294e0df3eb1Ssam * Finally, look for a change in size of the
295e0df3eb1Ssam * last direct block; release any frags.
296e0df3eb1Ssam */
297214e4f3dSmckusick bn = oip->i_db[lastblock];
2983f227b83Ssam if (bn != 0) {
2995aa03ac6Smckusick long oldspace, newspace;
3003f227b83Ssam
301e0df3eb1Ssam /*
302e0df3eb1Ssam * Calculate amount of space we're giving
303e0df3eb1Ssam * back as old block size minus new block size.
304e0df3eb1Ssam */
305214e4f3dSmckusick oldspace = blksize(fs, oip, lastblock);
306214e4f3dSmckusick oip->i_size = length;
307214e4f3dSmckusick newspace = blksize(fs, oip, lastblock);
3083f227b83Ssam if (newspace == 0)
3093f227b83Ssam panic("itrunc: newspace");
3103f227b83Ssam if (oldspace - newspace > 0) {
311e0df3eb1Ssam /*
312e0df3eb1Ssam * Block number of space to be free'd is
313e0df3eb1Ssam * the old block # plus the number of frags
314e0df3eb1Ssam * required for the storage we're keeping.
315e0df3eb1Ssam */
3163f227b83Ssam bn += numfrags(fs, newspace);
317214e4f3dSmckusick ffs_blkfree(oip, bn, oldspace - newspace);
318104e039dSsam blocksreleased += btodb(oldspace - newspace);
319e0df3eb1Ssam }
320e0df3eb1Ssam }
321e0df3eb1Ssam done:
3227f2e25eeSmckusick #ifdef DIAGNOSTIC
3233f227b83Ssam for (level = SINGLE; level <= TRIPLE; level++)
324214e4f3dSmckusick if (newblks[NDADDR + level] != oip->i_ib[level])
3253f227b83Ssam panic("itrunc1");
3263f227b83Ssam for (i = 0; i < NDADDR; i++)
327214e4f3dSmckusick if (newblks[i] != oip->i_db[i])
3283f227b83Ssam panic("itrunc2");
3297f2e25eeSmckusick if (length == 0 &&
330abad4ad2Smckusick (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
3317f2e25eeSmckusick panic("itrunc3");
3327f2e25eeSmckusick #endif /* DIAGNOSTIC */
333214e4f3dSmckusick /*
334214e4f3dSmckusick * Put back the real size.
335214e4f3dSmckusick */
336214e4f3dSmckusick oip->i_size = length;
337104e039dSsam oip->i_blocks -= blocksreleased;
338104e039dSsam if (oip->i_blocks < 0) /* sanity */
339104e039dSsam oip->i_blocks = 0;
340cb55a29dSbostic oip->i_flag |= IN_CHANGE;
34159f822f3Skre #ifdef QUOTA
342721b5a6aSmckusick (void) chkdq(oip, -blocksreleased, NOCRED, 0);
34359f822f3Skre #endif
3443c0618ecSmckusick return (allerror);
345d4b8d8d2Sbill }
346d4b8d8d2Sbill
347e0df3eb1Ssam /*
348f56b3bb2Sbostic * Release blocks associated with the inode ip and stored in the indirect
349f56b3bb2Sbostic * block bn. Blocks are free'd in LIFO order up to (but not including)
350f56b3bb2Sbostic * lastbn. If level is greater than SINGLE, the block is an indirect block
351f56b3bb2Sbostic * and recursive calls to indirtrunc must be used to cleanse other indirect
352f56b3bb2Sbostic * blocks.
3533f227b83Ssam *
3543f227b83Ssam * NB: triple indirect blocks are untested.
355e0df3eb1Ssam */
356f56b3bb2Sbostic static int
ffs_indirtrunc(ip,lbn,dbn,lastbn,level,countp)357ad8b6216Smargo ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
358f33622a8Smckusic register struct inode *ip;
3592c9a935dSmckusick ufs_daddr_t lbn, lastbn;
3602c9a935dSmckusick ufs_daddr_t dbn;
3613f227b83Ssam int level;
3623c0618ecSmckusick long *countp;
363d4b8d8d2Sbill {
364e0df3eb1Ssam register int i;
365a755bbd5Smckusick struct buf *bp;
3663f227b83Ssam register struct fs *fs = ip->i_fs;
3672c9a935dSmckusick register ufs_daddr_t *bap;
368ad8b6216Smargo struct vnode *vp;
3692c9a935dSmckusick ufs_daddr_t *copy, nb, nlbn, last;
3703c0618ecSmckusick long blkcount, factor;
3713c0618ecSmckusick int nblocks, blocksreleased = 0;
372ad8b6216Smargo int error = 0, allerror = 0;
373d4b8d8d2Sbill
3743f227b83Ssam /*
3753f227b83Ssam * Calculate index in current block of last
3763f227b83Ssam * block to be kept. -1 indicates the entire
3773f227b83Ssam * block so we need not calculate the index.
3783f227b83Ssam */
3793f227b83Ssam factor = 1;
3803f227b83Ssam for (i = SINGLE; i < level; i++)
3813f227b83Ssam factor *= NINDIR(fs);
382e0df3eb1Ssam last = lastbn;
3833f227b83Ssam if (lastbn > 0)
3843f227b83Ssam last /= factor;
385104e039dSsam nblocks = btodb(fs->fs_bsize);
3863f227b83Ssam /*
387ad8b6216Smargo * Get buffer of block pointers, zero those entries corresponding
388ad8b6216Smargo * to blocks to be free'd, and update on disk copy first. Since
389ad8b6216Smargo * double(triple) indirect before single(double) indirect, calls
390ad8b6216Smargo * to bmap on these blocks will fail. However, we already have
391ad8b6216Smargo * the on disk address, so we have to set the b_blkno field
392ad8b6216Smargo * explicitly instead of letting bread do everything for us.
3933f227b83Ssam */
394ad8b6216Smargo vp = ITOV(ip);
395ab62d2ccSmckusick bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
396ad8b6216Smargo if (bp->b_flags & (B_DONE | B_DELWRI)) {
397ad8b6216Smargo /* Braces must be here in case trace evaluates to nothing. */
398ad8b6216Smargo trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
399ad8b6216Smargo } else {
400ad8b6216Smargo trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
401ad8b6216Smargo curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
402ad8b6216Smargo bp->b_flags |= B_READ;
403ad8b6216Smargo if (bp->b_bcount > bp->b_bufsize)
404ad8b6216Smargo panic("ffs_indirtrunc: bad buffer size");
405ad8b6216Smargo bp->b_blkno = dbn;
406ad8b6216Smargo VOP_STRATEGY(bp);
407ad8b6216Smargo error = biowait(bp);
408ad8b6216Smargo }
4093c0618ecSmckusick if (error) {
410d4b8d8d2Sbill brelse(bp);
4113c0618ecSmckusick *countp = 0;
4123c0618ecSmckusick return (error);
413d4b8d8d2Sbill }
414ad8b6216Smargo
4152c9a935dSmckusick bap = (ufs_daddr_t *)bp->b_data;
4162c9a935dSmckusick MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
417a755bbd5Smckusick bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
418e0df3eb1Ssam bzero((caddr_t)&bap[last + 1],
4192c9a935dSmckusick (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
42029b6fa5cSmckusick if (last == -1)
42129b6fa5cSmckusick bp->b_flags |= B_INVAL;
4223c0618ecSmckusick error = bwrite(bp);
4233c0618ecSmckusick if (error)
4243c0618ecSmckusick allerror = error;
425a755bbd5Smckusick bap = copy;
4263f227b83Ssam
4273f227b83Ssam /*
4283f227b83Ssam * Recursively free totally unused blocks.
4293f227b83Ssam */
4303bcf2b3eSmargo for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
431ad8b6216Smargo i--, nlbn += factor) {
432d4b8d8d2Sbill nb = bap[i];
433e0df3eb1Ssam if (nb == 0)
434d4b8d8d2Sbill continue;
4353c0618ecSmckusick if (level > SINGLE) {
4362c9a935dSmckusick if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
4372c9a935dSmckusick (ufs_daddr_t)-1, level - 1, &blkcount))
4383c0618ecSmckusick allerror = error;
4393c0618ecSmckusick blocksreleased += blkcount;
4403c0618ecSmckusick }
4415aa03ac6Smckusick ffs_blkfree(ip, nb, fs->fs_bsize);
442e0df3eb1Ssam blocksreleased += nblocks;
44359f822f3Skre }
4443f227b83Ssam
4453f227b83Ssam /*
4463f227b83Ssam * Recursively free last partial block.
4473f227b83Ssam */
4483f227b83Ssam if (level > SINGLE && lastbn >= 0) {
4493f227b83Ssam last = lastbn % factor;
450e0df3eb1Ssam nb = bap[i];
4513c0618ecSmckusick if (nb != 0) {
452ad8b6216Smargo if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
453ad8b6216Smargo last, level - 1, &blkcount))
4543c0618ecSmckusick allerror = error;
4553c0618ecSmckusick blocksreleased += blkcount;
4563c0618ecSmckusick }
457d4b8d8d2Sbill }
458a755bbd5Smckusick FREE(copy, M_TEMP);
4593c0618ecSmckusick *countp = blocksreleased;
4603c0618ecSmckusick return (allerror);
461d4b8d8d2Sbill }
462