xref: /freebsd/sys/ufs/ffs/ffs_inode.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)ffs_inode.c	8.13 (Berkeley) 4/21/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_quota.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/proc.h>
41 #include <sys/bio.h>
42 #include <sys/buf.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/resourcevar.h>
46 #include <sys/vmmeter.h>
47 #include <sys/stat.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_extern.h>
51 
52 #include <ufs/ufs/extattr.h>
53 #include <ufs/ufs/quota.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/ufs_extern.h>
57 
58 #include <ufs/ffs/fs.h>
59 #include <ufs/ffs/ffs_extern.h>
60 
61 static int ffs_indirtrunc(struct inode *, ufs2_daddr_t, ufs2_daddr_t,
62 	    ufs2_daddr_t, int, ufs2_daddr_t *);
63 
64 /*
65  * Update the access, modified, and inode change times as specified by the
66  * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.  Write the inode
67  * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
68  * the timestamp update).  The IN_LAZYMOD flag is set to force a write
69  * later if not now.  If we write now, then clear both IN_MODIFIED and
70  * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
71  * set, then wait for the write to complete.
72  */
73 int
74 ffs_update(vp, waitfor)
75 	struct vnode *vp;
76 	int waitfor;
77 {
78 	struct fs *fs;
79 	struct buf *bp;
80 	struct inode *ip;
81 	int error;
82 
83 #ifdef DEBUG_VFS_LOCKS
84 	if ((vp->v_iflag & VI_XLOCK) == 0)
85 		ASSERT_VOP_LOCKED(vp, "ffs_update");
86 #endif
87 	ufs_itimes(vp);
88 	ip = VTOI(vp);
89 	if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
90 		return (0);
91 	ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED);
92 	fs = ip->i_fs;
93 	if (fs->fs_ronly)
94 		return (0);
95 	/*
96 	 * Ensure that uid and gid are correct. This is a temporary
97 	 * fix until fsck has been changed to do the update.
98 	 */
99 	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
100 	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
101 		ip->i_din1->di_ouid = ip->i_uid;	/* XXX */
102 		ip->i_din1->di_ogid = ip->i_gid;	/* XXX */
103 	}						/* XXX */
104 	error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
105 		(int)fs->fs_bsize, NOCRED, &bp);
106 	if (error) {
107 		brelse(bp);
108 		return (error);
109 	}
110 	if (DOINGSOFTDEP(vp))
111 		softdep_update_inodeblock(ip, bp, waitfor);
112 	else if (ip->i_effnlink != ip->i_nlink)
113 		panic("ffs_update: bad link cnt");
114 	if (ip->i_ump->um_fstype == UFS1)
115 		*((struct ufs1_dinode *)bp->b_data +
116 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
117 	else
118 		*((struct ufs2_dinode *)bp->b_data +
119 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
120 	if (waitfor && !DOINGASYNC(vp)) {
121 		return (bwrite(bp));
122 	} else if (vm_page_count_severe() || buf_dirty_count_severe()) {
123 		return (bwrite(bp));
124 	} else {
125 		if (bp->b_bufsize == fs->fs_bsize)
126 			bp->b_flags |= B_CLUSTEROK;
127 		bdwrite(bp);
128 		return (0);
129 	}
130 }
131 
132 #define	SINGLE	0	/* index of single indirect block */
133 #define	DOUBLE	1	/* index of double indirect block */
134 #define	TRIPLE	2	/* index of triple indirect block */
135 /*
136  * Truncate the inode oip to at most length size, freeing the
137  * disk blocks.
138  */
139 int
140 ffs_truncate(vp, length, flags, cred, td)
141 	struct vnode *vp;
142 	off_t length;
143 	int flags;
144 	struct ucred *cred;
145 	struct thread *td;
146 {
147 	struct vnode *ovp = vp;
148 	struct inode *oip;
149 	ufs2_daddr_t bn, lbn, lastblock, lastiblock[NIADDR], indir_lbn[NIADDR];
150 	ufs2_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
151 	ufs2_daddr_t count, blocksreleased = 0, datablocks;
152 	struct fs *fs;
153 	struct buf *bp;
154 	struct ufsmount *ump;
155 	int needextclean, softdepslowdown, extblocks;
156 	int offset, size, level, nblocks;
157 	int i, error, allerror;
158 	off_t osize;
159 
160 	oip = VTOI(ovp);
161 	fs = oip->i_fs;
162 	ump = oip->i_ump;
163 
164 	if (length < 0)
165 		return (EINVAL);
166 	/*
167 	 * Historically clients did not have to specify which data
168 	 * they were truncating. So, if not specified, we assume
169 	 * traditional behavior, e.g., just the normal data.
170 	 */
171 	if ((flags & (IO_EXT | IO_NORMAL)) == 0)
172 		flags |= IO_NORMAL;
173 	/*
174 	 * If we are truncating the extended-attributes, and cannot
175 	 * do it with soft updates, then do it slowly here. If we are
176 	 * truncating both the extended attributes and the file contents
177 	 * (e.g., the file is being unlinked), then pick it off with
178 	 * soft updates below.
179 	 */
180 	needextclean = 0;
181 	softdepslowdown = DOINGSOFTDEP(ovp) && softdep_slowdown(ovp);
182 	extblocks = 0;
183 	datablocks = DIP(oip, i_blocks);
184 	if (fs->fs_magic == FS_UFS2_MAGIC && oip->i_din2->di_extsize > 0) {
185 		extblocks = btodb(fragroundup(fs, oip->i_din2->di_extsize));
186 		datablocks -= extblocks;
187 	}
188 	if ((flags & IO_EXT) && extblocks > 0) {
189 		if (DOINGSOFTDEP(ovp) && softdepslowdown == 0 && length == 0) {
190 			if ((flags & IO_NORMAL) == 0) {
191 				softdep_setup_freeblocks(oip, length, IO_EXT);
192 				return (0);
193 			}
194 			needextclean = 1;
195 		} else {
196 			if (length != 0)
197 				panic("ffs_truncate: partial trunc of extdata");
198 			if ((error = ffs_syncvnode(ovp, MNT_WAIT)) != 0)
199 				return (error);
200 			osize = oip->i_din2->di_extsize;
201 			oip->i_din2->di_blocks -= extblocks;
202 #ifdef QUOTA
203 			(void) chkdq(oip, -extblocks, NOCRED, 0);
204 #endif
205 			vinvalbuf(ovp, V_ALT, td, 0, 0);
206 			oip->i_din2->di_extsize = 0;
207 			for (i = 0; i < NXADDR; i++) {
208 				oldblks[i] = oip->i_din2->di_extb[i];
209 				oip->i_din2->di_extb[i] = 0;
210 			}
211 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
212 			if ((error = ffs_update(ovp, 1)))
213 				return (error);
214 			for (i = 0; i < NXADDR; i++) {
215 				if (oldblks[i] == 0)
216 					continue;
217 				ffs_blkfree(ump, fs, oip->i_devvp, oldblks[i],
218 				    sblksize(fs, osize, i), oip->i_number);
219 			}
220 		}
221 	}
222 	if ((flags & IO_NORMAL) == 0)
223 		return (0);
224 	if (length > fs->fs_maxfilesize)
225 		return (EFBIG);
226 	if (ovp->v_type == VLNK &&
227 	    (oip->i_size < ovp->v_mount->mnt_maxsymlinklen ||
228 	     datablocks == 0)) {
229 #ifdef DIAGNOSTIC
230 		if (length != 0)
231 			panic("ffs_truncate: partial truncate of symlink");
232 #endif
233 		bzero(SHORTLINK(oip), (u_int)oip->i_size);
234 		oip->i_size = 0;
235 		DIP_SET(oip, i_size, 0);
236 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
237 		if (needextclean)
238 			softdep_setup_freeblocks(oip, length, IO_EXT);
239 		return (ffs_update(ovp, 1));
240 	}
241 	if (oip->i_size == length) {
242 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
243 		if (needextclean)
244 			softdep_setup_freeblocks(oip, length, IO_EXT);
245 		return (ffs_update(ovp, 0));
246 	}
247 	if (fs->fs_ronly)
248 		panic("ffs_truncate: read-only filesystem");
249 #ifdef QUOTA
250 	error = getinoquota(oip);
251 	if (error)
252 		return (error);
253 #endif
254 	if ((oip->i_flags & SF_SNAPSHOT) != 0)
255 		ffs_snapremove(ovp);
256 	ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
257 	if (DOINGSOFTDEP(ovp)) {
258 		if (length > 0 || softdepslowdown) {
259 			/*
260 			 * If a file is only partially truncated, then
261 			 * we have to clean up the data structures
262 			 * describing the allocation past the truncation
263 			 * point. Finding and deallocating those structures
264 			 * is a lot of work. Since partial truncation occurs
265 			 * rarely, we solve the problem by syncing the file
266 			 * so that it will have no data structures left.
267 			 */
268 			if ((error = ffs_syncvnode(ovp, MNT_WAIT)) != 0)
269 				return (error);
270 			UFS_LOCK(ump);
271 			if (oip->i_flag & IN_SPACECOUNTED)
272 				fs->fs_pendingblocks -= datablocks;
273 			UFS_UNLOCK(ump);
274 		} else {
275 #ifdef QUOTA
276 			(void) chkdq(oip, -datablocks, NOCRED, 0);
277 #endif
278 			softdep_setup_freeblocks(oip, length, needextclean ?
279 			    IO_EXT | IO_NORMAL : IO_NORMAL);
280 			vinvalbuf(ovp, needextclean ? 0 : V_NORMAL, td, 0, 0);
281 			vnode_pager_setsize(vp, 0);
282 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
283 			return (ffs_update(ovp, 0));
284 		}
285 	}
286 	osize = oip->i_size;
287 	/*
288 	 * Lengthen the size of the file. We must ensure that the
289 	 * last byte of the file is allocated. Since the smallest
290 	 * value of osize is 0, length will be at least 1.
291 	 */
292 	if (osize < length) {
293 		vnode_pager_setsize(ovp, length);
294 		flags |= BA_CLRBUF;
295 		error = UFS_BALLOC(ovp, length - 1, 1, cred, flags, &bp);
296 		if (error)
297 			return (error);
298 		oip->i_size = length;
299 		DIP_SET(oip, i_size, length);
300 		if (bp->b_bufsize == fs->fs_bsize)
301 			bp->b_flags |= B_CLUSTEROK;
302 		if (flags & IO_SYNC)
303 			bwrite(bp);
304 		else
305 			bawrite(bp);
306 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
307 		return (ffs_update(ovp, 1));
308 	}
309 	/*
310 	 * Shorten the size of the file. If the file is not being
311 	 * truncated to a block boundary, the contents of the
312 	 * partial block following the end of the file must be
313 	 * zero'ed in case it ever becomes accessible again because
314 	 * of subsequent file growth. Directories however are not
315 	 * zero'ed as they should grow back initialized to empty.
316 	 */
317 	offset = blkoff(fs, length);
318 	if (offset == 0) {
319 		oip->i_size = length;
320 		DIP_SET(oip, i_size, length);
321 	} else {
322 		lbn = lblkno(fs, length);
323 		flags |= BA_CLRBUF;
324 		error = UFS_BALLOC(ovp, length - 1, 1, cred, flags, &bp);
325 		if (error) {
326 			return (error);
327 		}
328 		/*
329 		 * When we are doing soft updates and the UFS_BALLOC
330 		 * above fills in a direct block hole with a full sized
331 		 * block that will be truncated down to a fragment below,
332 		 * we must flush out the block dependency with an FSYNC
333 		 * so that we do not get a soft updates inconsistency
334 		 * when we create the fragment below.
335 		 */
336 		if (DOINGSOFTDEP(ovp) && lbn < NDADDR &&
337 		    fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
338 		    (error = ffs_syncvnode(ovp, MNT_WAIT)) != 0)
339 			return (error);
340 		oip->i_size = length;
341 		DIP_SET(oip, i_size, length);
342 		size = blksize(fs, oip, lbn);
343 		if (ovp->v_type != VDIR)
344 			bzero((char *)bp->b_data + offset,
345 			    (u_int)(size - offset));
346 		/* Kirk's code has reallocbuf(bp, size, 1) here */
347 		allocbuf(bp, size);
348 		if (bp->b_bufsize == fs->fs_bsize)
349 			bp->b_flags |= B_CLUSTEROK;
350 		if (flags & IO_SYNC)
351 			bwrite(bp);
352 		else
353 			bawrite(bp);
354 	}
355 	/*
356 	 * Calculate index into inode's block list of
357 	 * last direct and indirect blocks (if any)
358 	 * which we want to keep.  Lastblock is -1 when
359 	 * the file is truncated to 0.
360 	 */
361 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
362 	lastiblock[SINGLE] = lastblock - NDADDR;
363 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
364 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
365 	nblocks = btodb(fs->fs_bsize);
366 	/*
367 	 * Update file and block pointers on disk before we start freeing
368 	 * blocks.  If we crash before free'ing blocks below, the blocks
369 	 * will be returned to the free list.  lastiblock values are also
370 	 * normalized to -1 for calls to ffs_indirtrunc below.
371 	 */
372 	for (level = TRIPLE; level >= SINGLE; level--) {
373 		oldblks[NDADDR + level] = DIP(oip, i_ib[level]);
374 		if (lastiblock[level] < 0) {
375 			DIP_SET(oip, i_ib[level], 0);
376 			lastiblock[level] = -1;
377 		}
378 	}
379 	for (i = 0; i < NDADDR; i++) {
380 		oldblks[i] = DIP(oip, i_db[i]);
381 		if (i > lastblock)
382 			DIP_SET(oip, i_db[i], 0);
383 	}
384 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
385 	allerror = ffs_update(ovp, 1);
386 
387 	/*
388 	 * Having written the new inode to disk, save its new configuration
389 	 * and put back the old block pointers long enough to process them.
390 	 * Note that we save the new block configuration so we can check it
391 	 * when we are done.
392 	 */
393 	for (i = 0; i < NDADDR; i++) {
394 		newblks[i] = DIP(oip, i_db[i]);
395 		DIP_SET(oip, i_db[i], oldblks[i]);
396 	}
397 	for (i = 0; i < NIADDR; i++) {
398 		newblks[NDADDR + i] = DIP(oip, i_ib[i]);
399 		DIP_SET(oip, i_ib[i], oldblks[NDADDR + i]);
400 	}
401 	oip->i_size = osize;
402 	DIP_SET(oip, i_size, osize);
403 
404 	error = vtruncbuf(ovp, cred, td, length, fs->fs_bsize);
405 	if (error && (allerror == 0))
406 		allerror = error;
407 
408 	/*
409 	 * Indirect blocks first.
410 	 */
411 	indir_lbn[SINGLE] = -NDADDR;
412 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
413 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
414 	for (level = TRIPLE; level >= SINGLE; level--) {
415 		bn = DIP(oip, i_ib[level]);
416 		if (bn != 0) {
417 			error = ffs_indirtrunc(oip, indir_lbn[level],
418 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
419 			if (error)
420 				allerror = error;
421 			blocksreleased += count;
422 			if (lastiblock[level] < 0) {
423 				DIP_SET(oip, i_ib[level], 0);
424 				ffs_blkfree(ump, fs, oip->i_devvp, bn,
425 				    fs->fs_bsize, oip->i_number);
426 				blocksreleased += nblocks;
427 			}
428 		}
429 		if (lastiblock[level] >= 0)
430 			goto done;
431 	}
432 
433 	/*
434 	 * All whole direct blocks or frags.
435 	 */
436 	for (i = NDADDR - 1; i > lastblock; i--) {
437 		long bsize;
438 
439 		bn = DIP(oip, i_db[i]);
440 		if (bn == 0)
441 			continue;
442 		DIP_SET(oip, i_db[i], 0);
443 		bsize = blksize(fs, oip, i);
444 		ffs_blkfree(ump, fs, oip->i_devvp, bn, bsize, oip->i_number);
445 		blocksreleased += btodb(bsize);
446 	}
447 	if (lastblock < 0)
448 		goto done;
449 
450 	/*
451 	 * Finally, look for a change in size of the
452 	 * last direct block; release any frags.
453 	 */
454 	bn = DIP(oip, i_db[lastblock]);
455 	if (bn != 0) {
456 		long oldspace, newspace;
457 
458 		/*
459 		 * Calculate amount of space we're giving
460 		 * back as old block size minus new block size.
461 		 */
462 		oldspace = blksize(fs, oip, lastblock);
463 		oip->i_size = length;
464 		DIP_SET(oip, i_size, length);
465 		newspace = blksize(fs, oip, lastblock);
466 		if (newspace == 0)
467 			panic("ffs_truncate: newspace");
468 		if (oldspace - newspace > 0) {
469 			/*
470 			 * Block number of space to be free'd is
471 			 * the old block # plus the number of frags
472 			 * required for the storage we're keeping.
473 			 */
474 			bn += numfrags(fs, newspace);
475 			ffs_blkfree(ump, fs, oip->i_devvp, bn,
476 			    oldspace - newspace, oip->i_number);
477 			blocksreleased += btodb(oldspace - newspace);
478 		}
479 	}
480 done:
481 #ifdef DIAGNOSTIC
482 	for (level = SINGLE; level <= TRIPLE; level++)
483 		if (newblks[NDADDR + level] != DIP(oip, i_ib[level]))
484 			panic("ffs_truncate1");
485 	for (i = 0; i < NDADDR; i++)
486 		if (newblks[i] != DIP(oip, i_db[i]))
487 			panic("ffs_truncate2");
488 	VI_LOCK(ovp);
489 	if (length == 0 &&
490 	    (fs->fs_magic != FS_UFS2_MAGIC || oip->i_din2->di_extsize == 0) &&
491 	    (vp->v_bufobj.bo_dirty.bv_cnt > 0 ||
492 	     vp->v_bufobj.bo_clean.bv_cnt > 0))
493 		panic("ffs_truncate3");
494 	VI_UNLOCK(ovp);
495 #endif /* DIAGNOSTIC */
496 	/*
497 	 * Put back the real size.
498 	 */
499 	oip->i_size = length;
500 	DIP_SET(oip, i_size, length);
501 	DIP_SET(oip, i_blocks, DIP(oip, i_blocks) - blocksreleased);
502 
503 	if (DIP(oip, i_blocks) < 0)			/* sanity */
504 		DIP_SET(oip, i_blocks, 0);
505 	oip->i_flag |= IN_CHANGE;
506 #ifdef QUOTA
507 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
508 #endif
509 	return (allerror);
510 }
511 
512 /*
513  * Release blocks associated with the inode ip and stored in the indirect
514  * block bn.  Blocks are free'd in LIFO order up to (but not including)
515  * lastbn.  If level is greater than SINGLE, the block is an indirect block
516  * and recursive calls to indirtrunc must be used to cleanse other indirect
517  * blocks.
518  */
519 static int
520 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
521 	struct inode *ip;
522 	ufs2_daddr_t lbn, lastbn;
523 	ufs2_daddr_t dbn;
524 	int level;
525 	ufs2_daddr_t *countp;
526 {
527 	struct buf *bp;
528 	struct fs *fs = ip->i_fs;
529 	struct vnode *vp;
530 	caddr_t copy = NULL;
531 	int i, nblocks, error = 0, allerror = 0;
532 	ufs2_daddr_t nb, nlbn, last;
533 	ufs2_daddr_t blkcount, factor, blocksreleased = 0;
534 	ufs1_daddr_t *bap1 = NULL;
535 	ufs2_daddr_t *bap2 = NULL;
536 #	define BAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? bap1[i] : bap2[i])
537 
538 	/*
539 	 * Calculate index in current block of last
540 	 * block to be kept.  -1 indicates the entire
541 	 * block so we need not calculate the index.
542 	 */
543 	factor = 1;
544 	for (i = SINGLE; i < level; i++)
545 		factor *= NINDIR(fs);
546 	last = lastbn;
547 	if (lastbn > 0)
548 		last /= factor;
549 	nblocks = btodb(fs->fs_bsize);
550 	/*
551 	 * Get buffer of block pointers, zero those entries corresponding
552 	 * to blocks to be free'd, and update on disk copy first.  Since
553 	 * double(triple) indirect before single(double) indirect, calls
554 	 * to bmap on these blocks will fail.  However, we already have
555 	 * the on disk address, so we have to set the b_blkno field
556 	 * explicitly instead of letting bread do everything for us.
557 	 */
558 	vp = ITOV(ip);
559 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0, 0);
560 	if ((bp->b_flags & B_CACHE) == 0) {
561 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
562 		bp->b_iocmd = BIO_READ;
563 		bp->b_flags &= ~B_INVAL;
564 		bp->b_ioflags &= ~BIO_ERROR;
565 		if (bp->b_bcount > bp->b_bufsize)
566 			panic("ffs_indirtrunc: bad buffer size");
567 		bp->b_blkno = dbn;
568 		vfs_busy_pages(bp, 0);
569 		bp->b_iooffset = dbtob(bp->b_blkno);
570 		bstrategy(bp);
571 		error = bufwait(bp);
572 	}
573 	if (error) {
574 		brelse(bp);
575 		*countp = 0;
576 		return (error);
577 	}
578 
579 	if (ip->i_ump->um_fstype == UFS1)
580 		bap1 = (ufs1_daddr_t *)bp->b_data;
581 	else
582 		bap2 = (ufs2_daddr_t *)bp->b_data;
583 	if (lastbn != -1) {
584 		MALLOC(copy, caddr_t, fs->fs_bsize, M_TEMP, M_WAITOK);
585 		bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
586 		for (i = last + 1; i < NINDIR(fs); i++)
587 			if (ip->i_ump->um_fstype == UFS1)
588 				bap1[i] = 0;
589 			else
590 				bap2[i] = 0;
591 		if (DOINGASYNC(vp)) {
592 			bawrite(bp);
593 		} else {
594 			error = bwrite(bp);
595 			if (error)
596 				allerror = error;
597 		}
598 		if (ip->i_ump->um_fstype == UFS1)
599 			bap1 = (ufs1_daddr_t *)copy;
600 		else
601 			bap2 = (ufs2_daddr_t *)copy;
602 	}
603 
604 	/*
605 	 * Recursively free totally unused blocks.
606 	 */
607 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
608 	    i--, nlbn += factor) {
609 		nb = BAP(ip, i);
610 		if (nb == 0)
611 			continue;
612 		if (level > SINGLE) {
613 			if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
614 			    (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
615 				allerror = error;
616 			blocksreleased += blkcount;
617 		}
618 		ffs_blkfree(ip->i_ump, fs, ip->i_devvp, nb, fs->fs_bsize,
619 		    ip->i_number);
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 = BAP(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 	if (copy != NULL) {
638 		FREE(copy, M_TEMP);
639 	} else {
640 		bp->b_flags |= B_INVAL | B_NOCACHE;
641 		brelse(bp);
642 	}
643 
644 	*countp = blocksreleased;
645 	return (allerror);
646 }
647