xref: /freebsd/sys/ufs/ffs/ffs_inode.c (revision 59c7ad52aaa5b26e503871334672af0f58f9c2e8)
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 #include <vm/vm_object.h>
52 
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/inode.h>
57 #include <ufs/ufs/ufs_extern.h>
58 
59 #include <ufs/ffs/fs.h>
60 #include <ufs/ffs/ffs_extern.h>
61 
62 static int ffs_indirtrunc(struct inode *, ufs2_daddr_t, ufs2_daddr_t,
63 	    ufs2_daddr_t, int, ufs2_daddr_t *);
64 
65 /*
66  * Update the access, modified, and inode change times as specified by the
67  * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.  Write the inode
68  * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
69  * the timestamp update).  The IN_LAZYMOD flag is set to force a write
70  * later if not now.  The IN_LAZYACCESS is set instead of IN_MODIFIED if the fs
71  * is currently being suspended (or is suspended) and vnode has been accessed.
72  * If we write now, then clear IN_MODIFIED, IN_LAZYACCESS and IN_LAZYMOD to
73  * reflect the presumably successful write, and if waitfor is set, then wait
74  * for the write to complete.
75  */
76 int
77 ffs_update(vp, waitfor)
78 	struct vnode *vp;
79 	int waitfor;
80 {
81 	struct fs *fs;
82 	struct buf *bp;
83 	struct inode *ip;
84 	int error;
85 
86 	ASSERT_VOP_ELOCKED(vp, "ffs_update");
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_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
92 	fs = ip->i_fs;
93 	if (fs->fs_ronly)
94 		return (0);
95 	error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
96 		(int)fs->fs_bsize, NOCRED, &bp);
97 	if (error) {
98 		brelse(bp);
99 		return (error);
100 	}
101 	if (DOINGSOFTDEP(vp))
102 		softdep_update_inodeblock(ip, bp, waitfor);
103 	else if (ip->i_effnlink != ip->i_nlink)
104 		panic("ffs_update: bad link cnt");
105 	if (ip->i_ump->um_fstype == UFS1)
106 		*((struct ufs1_dinode *)bp->b_data +
107 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
108 	else
109 		*((struct ufs2_dinode *)bp->b_data +
110 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
111 	if (waitfor && !DOINGASYNC(vp)) {
112 		return (bwrite(bp));
113 	} else if (vm_page_count_severe() || buf_dirty_count_severe()) {
114 		return (bwrite(bp));
115 	} else {
116 		if (bp->b_bufsize == fs->fs_bsize)
117 			bp->b_flags |= B_CLUSTEROK;
118 		bdwrite(bp);
119 		return (0);
120 	}
121 }
122 
123 static void
124 ffs_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
125 {
126 	vm_object_t object;
127 
128 	if ((object = vp->v_object) == NULL)
129 		return;
130 	VM_OBJECT_LOCK(object);
131 	vm_object_page_remove(object, start, end, FALSE);
132 	VM_OBJECT_UNLOCK(object);
133 }
134 
135 #define	SINGLE	0	/* index of single indirect block */
136 #define	DOUBLE	1	/* index of double indirect block */
137 #define	TRIPLE	2	/* index of triple indirect block */
138 /*
139  * Truncate the inode ip to at most length size, freeing the
140  * disk blocks.
141  */
142 int
143 ffs_truncate(vp, length, flags, cred, td)
144 	struct vnode *vp;
145 	off_t length;
146 	int flags;
147 	struct ucred *cred;
148 	struct thread *td;
149 {
150 	struct inode *ip;
151 	ufs2_daddr_t bn, lbn, lastblock, lastiblock[NIADDR], indir_lbn[NIADDR];
152 	ufs2_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
153 	ufs2_daddr_t count, blocksreleased = 0, datablocks;
154 	void *cookie;
155 	struct bufobj *bo;
156 	struct fs *fs;
157 	struct buf *bp;
158 	struct ufsmount *ump;
159 	int needextclean, softdepslowdown, extblocks;
160 	int offset, size, level, nblocks;
161 	int i, error, allerror;
162 	off_t osize;
163 
164 	ip = VTOI(vp);
165 	fs = ip->i_fs;
166 	ump = ip->i_ump;
167 	bo = &vp->v_bufobj;
168 	cookie = NULL;
169 
170 	ASSERT_VOP_LOCKED(vp, "ffs_truncate");
171 
172 	if (length < 0)
173 		return (EINVAL);
174 	if (length > fs->fs_maxfilesize)
175 		return (EFBIG);
176 	/*
177 	 * Historically clients did not have to specify which data
178 	 * they were truncating. So, if not specified, we assume
179 	 * traditional behavior, e.g., just the normal data.
180 	 */
181 	if ((flags & (IO_EXT | IO_NORMAL)) == 0)
182 		flags |= IO_NORMAL;
183 	if (!DOINGSOFTDEP(vp) && !DOINGASYNC(vp))
184 		flags |= IO_SYNC;
185 	/*
186 	 * If we are truncating the extended-attributes, and cannot
187 	 * do it with soft updates, then do it slowly here. If we are
188 	 * truncating both the extended attributes and the file contents
189 	 * (e.g., the file is being unlinked), then pick it off with
190 	 * soft updates below.
191 	 */
192 	allerror = 0;
193 	needextclean = 0;
194 	softdepslowdown = DOINGSOFTDEP(vp) && softdep_slowdown(vp);
195 	extblocks = 0;
196 	datablocks = DIP(ip, i_blocks);
197 	if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
198 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
199 		datablocks -= extblocks;
200 	}
201 	if ((flags & IO_EXT) && extblocks > 0) {
202 		if (DOINGSOFTDEP(vp) && softdepslowdown == 0 && length == 0) {
203 			if ((flags & IO_NORMAL) == 0) {
204 				softdep_setup_freeblocks(ip, length, IO_EXT);
205 				return (0);
206 			}
207 			needextclean = 1;
208 		} else {
209 			if (length != 0)
210 				panic("ffs_truncate: partial trunc of extdata");
211 			if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
212 				return (error);
213 			if (DOINGSUJ(vp))
214 				cookie = softdep_setup_trunc(vp, length, flags);
215 			osize = ip->i_din2->di_extsize;
216 			ip->i_din2->di_blocks -= extblocks;
217 #ifdef QUOTA
218 			(void) chkdq(ip, -extblocks, NOCRED, 0);
219 #endif
220 			vinvalbuf(vp, V_ALT, 0, 0);
221 			ffs_pages_remove(vp,
222 			    OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
223 			ip->i_din2->di_extsize = 0;
224 			for (i = 0; i < NXADDR; i++) {
225 				oldblks[i] = ip->i_din2->di_extb[i];
226 				ip->i_din2->di_extb[i] = 0;
227 			}
228 			ip->i_flag |= IN_CHANGE;
229 			if ((error = ffs_update(vp, 1)))
230 				goto out;
231 			for (i = 0; i < NXADDR; i++) {
232 				if (oldblks[i] == 0)
233 					continue;
234 				ffs_blkfree(ump, fs, ip->i_devvp, oldblks[i],
235 				    sblksize(fs, osize, i), ip->i_number, NULL);
236 			}
237 		}
238 	}
239 	if ((flags & IO_NORMAL) == 0) {
240 		error = 0;
241 		goto out;
242 	}
243 	if (vp->v_type == VLNK &&
244 	    (ip->i_size < vp->v_mount->mnt_maxsymlinklen ||
245 	     datablocks == 0)) {
246 #ifdef INVARIANTS
247 		if (length != 0)
248 			panic("ffs_truncate: partial truncate of symlink");
249 #endif
250 		bzero(SHORTLINK(ip), (u_int)ip->i_size);
251 		ip->i_size = 0;
252 		DIP_SET(ip, i_size, 0);
253 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
254 		if (needextclean)
255 			softdep_setup_freeblocks(ip, length, IO_EXT);
256 		error = ffs_update(vp, 1);
257 		goto out;
258 	}
259 	if (ip->i_size == length) {
260 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
261 		if (needextclean)
262 			softdep_setup_freeblocks(ip, length, IO_EXT);
263 		error = ffs_update(vp, 0);
264 		goto out;
265 	}
266 	if (fs->fs_ronly)
267 		panic("ffs_truncate: read-only filesystem");
268 #ifdef QUOTA
269 	error = getinoquota(ip);
270 	if (error)
271 		goto out;
272 #endif
273 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
274 		ffs_snapremove(vp);
275 	vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
276 	osize = ip->i_size;
277 	/*
278 	 * Lengthen the size of the file. We must ensure that the
279 	 * last byte of the file is allocated. Since the smallest
280 	 * value of osize is 0, length will be at least 1.
281 	 */
282 	if (osize < length) {
283 		vnode_pager_setsize(vp, length);
284 		flags |= BA_CLRBUF;
285 		error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
286 		if (error) {
287 			vnode_pager_setsize(vp, osize);
288 			goto out;
289 		}
290 		ip->i_size = length;
291 		DIP_SET(ip, i_size, length);
292 		if (bp->b_bufsize == fs->fs_bsize)
293 			bp->b_flags |= B_CLUSTEROK;
294 		if (flags & IO_SYNC)
295 			bwrite(bp);
296 		else
297 			bawrite(bp);
298 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
299 		error = ffs_update(vp, 1);
300 		goto out;
301 	}
302 	if (DOINGSOFTDEP(vp)) {
303 		if (length > 0 || softdepslowdown) {
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 = ffs_syncvnode(vp, MNT_WAIT)) != 0)
314 				goto out;
315 			/*
316 			 * We have to journal the truncation before we change
317 			 * any blocks so we don't leave the file partially
318 			 * truncated.
319 			 */
320 			if (DOINGSUJ(vp) && cookie == NULL)
321 				cookie = softdep_setup_trunc(vp, length, flags);
322 		} else {
323 #ifdef QUOTA
324 			(void) chkdq(ip, -datablocks, NOCRED, 0);
325 #endif
326 			softdep_setup_freeblocks(ip, length, needextclean ?
327 			    IO_EXT | IO_NORMAL : IO_NORMAL);
328 			ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
329 			vinvalbuf(vp, needextclean ? 0 : V_NORMAL, 0, 0);
330 			if (!needextclean)
331 				ffs_pages_remove(vp, 0,
332 				    OFF_TO_IDX(lblktosize(fs, -extblocks)));
333 			vnode_pager_setsize(vp, 0);
334 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
335 			error = ffs_update(vp, 0);
336 			goto out;
337 		}
338 	}
339 	/*
340 	 * Shorten the size of the file. If the file is not being
341 	 * truncated to a block boundary, the contents of the
342 	 * partial block following the end of the file must be
343 	 * zero'ed in case it ever becomes accessible again because
344 	 * of subsequent file growth. Directories however are not
345 	 * zero'ed as they should grow back initialized to empty.
346 	 */
347 	offset = blkoff(fs, length);
348 	if (offset == 0) {
349 		ip->i_size = length;
350 		DIP_SET(ip, i_size, length);
351 	} else {
352 		lbn = lblkno(fs, length);
353 		flags |= BA_CLRBUF;
354 		error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
355 		if (error)
356 			goto out;
357 		/*
358 		 * When we are doing soft updates and the UFS_BALLOC
359 		 * above fills in a direct block hole with a full sized
360 		 * block that will be truncated down to a fragment below,
361 		 * we must flush out the block dependency with an FSYNC
362 		 * so that we do not get a soft updates inconsistency
363 		 * when we create the fragment below.
364 		 */
365 		if (DOINGSOFTDEP(vp) && lbn < NDADDR &&
366 		    fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
367 		    (error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
368 			goto out;
369 		ip->i_size = length;
370 		DIP_SET(ip, i_size, length);
371 		size = blksize(fs, ip, lbn);
372 		if (vp->v_type != VDIR)
373 			bzero((char *)bp->b_data + offset,
374 			    (u_int)(size - offset));
375 		/* Kirk's code has reallocbuf(bp, size, 1) here */
376 		allocbuf(bp, size);
377 		if (bp->b_bufsize == fs->fs_bsize)
378 			bp->b_flags |= B_CLUSTEROK;
379 		if (flags & IO_SYNC)
380 			bwrite(bp);
381 		else
382 			bawrite(bp);
383 	}
384 	/*
385 	 * Calculate index into inode's block list of
386 	 * last direct and indirect blocks (if any)
387 	 * which we want to keep.  Lastblock is -1 when
388 	 * the file is truncated to 0.
389 	 */
390 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
391 	lastiblock[SINGLE] = lastblock - NDADDR;
392 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
393 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
394 	nblocks = btodb(fs->fs_bsize);
395 	/*
396 	 * Update file and block pointers on disk before we start freeing
397 	 * blocks.  If we crash before free'ing blocks below, the blocks
398 	 * will be returned to the free list.  lastiblock values are also
399 	 * normalized to -1 for calls to ffs_indirtrunc below.
400 	 */
401 	for (level = TRIPLE; level >= SINGLE; level--) {
402 		oldblks[NDADDR + level] = DIP(ip, i_ib[level]);
403 		if (lastiblock[level] < 0) {
404 			DIP_SET(ip, i_ib[level], 0);
405 			lastiblock[level] = -1;
406 		}
407 	}
408 	for (i = 0; i < NDADDR; i++) {
409 		oldblks[i] = DIP(ip, i_db[i]);
410 		if (i > lastblock)
411 			DIP_SET(ip, i_db[i], 0);
412 	}
413 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
414 	/*
415 	 * When doing softupdate journaling we must preserve the size along
416 	 * with the old pointers until they are freed or we might not
417 	 * know how many fragments remain.
418 	 */
419 	if (!DOINGSUJ(vp))
420 		allerror = ffs_update(vp, 1);
421 
422 	/*
423 	 * Having written the new inode to disk, save its new configuration
424 	 * and put back the old block pointers long enough to process them.
425 	 * Note that we save the new block configuration so we can check it
426 	 * when we are done.
427 	 */
428 	for (i = 0; i < NDADDR; i++) {
429 		newblks[i] = DIP(ip, i_db[i]);
430 		DIP_SET(ip, i_db[i], oldblks[i]);
431 	}
432 	for (i = 0; i < NIADDR; i++) {
433 		newblks[NDADDR + i] = DIP(ip, i_ib[i]);
434 		DIP_SET(ip, i_ib[i], oldblks[NDADDR + i]);
435 	}
436 	ip->i_size = osize;
437 	DIP_SET(ip, i_size, osize);
438 
439 	error = vtruncbuf(vp, cred, td, length, fs->fs_bsize);
440 	if (error && (allerror == 0))
441 		allerror = error;
442 
443 	/*
444 	 * Indirect blocks first.
445 	 */
446 	indir_lbn[SINGLE] = -NDADDR;
447 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
448 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
449 	for (level = TRIPLE; level >= SINGLE; level--) {
450 		bn = DIP(ip, i_ib[level]);
451 		if (bn != 0) {
452 			error = ffs_indirtrunc(ip, indir_lbn[level],
453 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
454 			if (error)
455 				allerror = error;
456 			blocksreleased += count;
457 			if (lastiblock[level] < 0) {
458 				DIP_SET(ip, i_ib[level], 0);
459 				ffs_blkfree(ump, fs, ip->i_devvp, bn,
460 				    fs->fs_bsize, ip->i_number, NULL);
461 				blocksreleased += nblocks;
462 			}
463 		}
464 		if (lastiblock[level] >= 0)
465 			goto done;
466 	}
467 
468 	/*
469 	 * All whole direct blocks or frags.
470 	 */
471 	for (i = NDADDR - 1; i > lastblock; i--) {
472 		long bsize;
473 
474 		bn = DIP(ip, i_db[i]);
475 		if (bn == 0)
476 			continue;
477 		DIP_SET(ip, i_db[i], 0);
478 		bsize = blksize(fs, ip, i);
479 		ffs_blkfree(ump, fs, ip->i_devvp, bn, bsize, ip->i_number,
480 		    NULL);
481 		blocksreleased += btodb(bsize);
482 	}
483 	if (lastblock < 0)
484 		goto done;
485 
486 	/*
487 	 * Finally, look for a change in size of the
488 	 * last direct block; release any frags.
489 	 */
490 	bn = DIP(ip, i_db[lastblock]);
491 	if (bn != 0) {
492 		long oldspace, newspace;
493 
494 		/*
495 		 * Calculate amount of space we're giving
496 		 * back as old block size minus new block size.
497 		 */
498 		oldspace = blksize(fs, ip, lastblock);
499 		ip->i_size = length;
500 		DIP_SET(ip, i_size, length);
501 		newspace = blksize(fs, ip, lastblock);
502 		if (newspace == 0)
503 			panic("ffs_truncate: newspace");
504 		if (oldspace - newspace > 0) {
505 			/*
506 			 * Block number of space to be free'd is
507 			 * the old block # plus the number of frags
508 			 * required for the storage we're keeping.
509 			 */
510 			bn += numfrags(fs, newspace);
511 			ffs_blkfree(ump, fs, ip->i_devvp, bn,
512 			    oldspace - newspace, ip->i_number, NULL);
513 			blocksreleased += btodb(oldspace - newspace);
514 		}
515 	}
516 done:
517 #ifdef INVARIANTS
518 	for (level = SINGLE; level <= TRIPLE; level++)
519 		if (newblks[NDADDR + level] != DIP(ip, i_ib[level]))
520 			panic("ffs_truncate1");
521 	for (i = 0; i < NDADDR; i++)
522 		if (newblks[i] != DIP(ip, i_db[i]))
523 			panic("ffs_truncate2");
524 	BO_LOCK(bo);
525 	if (length == 0 &&
526 	    (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
527 	    (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
528 		panic("ffs_truncate3");
529 	BO_UNLOCK(bo);
530 #endif /* INVARIANTS */
531 	/*
532 	 * Put back the real size.
533 	 */
534 	ip->i_size = length;
535 	DIP_SET(ip, i_size, length);
536 	DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
537 
538 	if (DIP(ip, i_blocks) < 0)			/* sanity */
539 		DIP_SET(ip, i_blocks, 0);
540 	ip->i_flag |= IN_CHANGE;
541 #ifdef QUOTA
542 	(void) chkdq(ip, -blocksreleased, NOCRED, 0);
543 #endif
544 	error = allerror;
545 out:
546 	if (cookie) {
547 		allerror = softdep_complete_trunc(vp, cookie);
548 		if (allerror != 0 && error == 0)
549 			error = allerror;
550 	}
551 	return (error);
552 }
553 
554 /*
555  * Release blocks associated with the inode ip and stored in the indirect
556  * block bn.  Blocks are free'd in LIFO order up to (but not including)
557  * lastbn.  If level is greater than SINGLE, the block is an indirect block
558  * and recursive calls to indirtrunc must be used to cleanse other indirect
559  * blocks.
560  */
561 static int
562 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
563 	struct inode *ip;
564 	ufs2_daddr_t lbn, lastbn;
565 	ufs2_daddr_t dbn;
566 	int level;
567 	ufs2_daddr_t *countp;
568 {
569 	struct buf *bp;
570 	struct fs *fs = ip->i_fs;
571 	struct vnode *vp;
572 	caddr_t copy = NULL;
573 	int i, nblocks, error = 0, allerror = 0;
574 	ufs2_daddr_t nb, nlbn, last;
575 	ufs2_daddr_t blkcount, factor, blocksreleased = 0;
576 	ufs1_daddr_t *bap1 = NULL;
577 	ufs2_daddr_t *bap2 = NULL;
578 #	define BAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? bap1[i] : bap2[i])
579 
580 	/*
581 	 * Calculate index in current block of last
582 	 * block to be kept.  -1 indicates the entire
583 	 * block so we need not calculate the index.
584 	 */
585 	factor = lbn_offset(fs, level);
586 	last = lastbn;
587 	if (lastbn > 0)
588 		last /= factor;
589 	nblocks = btodb(fs->fs_bsize);
590 	/*
591 	 * Get buffer of block pointers, zero those entries corresponding
592 	 * to blocks to be free'd, and update on disk copy first.  Since
593 	 * double(triple) indirect before single(double) indirect, calls
594 	 * to bmap on these blocks will fail.  However, we already have
595 	 * the on disk address, so we have to set the b_blkno field
596 	 * explicitly instead of letting bread do everything for us.
597 	 */
598 	vp = ITOV(ip);
599 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0, 0);
600 	if ((bp->b_flags & B_CACHE) == 0) {
601 		curthread->td_ru.ru_inblock++;	/* pay for read */
602 		bp->b_iocmd = BIO_READ;
603 		bp->b_flags &= ~B_INVAL;
604 		bp->b_ioflags &= ~BIO_ERROR;
605 		if (bp->b_bcount > bp->b_bufsize)
606 			panic("ffs_indirtrunc: bad buffer size");
607 		bp->b_blkno = dbn;
608 		vfs_busy_pages(bp, 0);
609 		bp->b_iooffset = dbtob(bp->b_blkno);
610 		bstrategy(bp);
611 		error = bufwait(bp);
612 	}
613 	if (error) {
614 		brelse(bp);
615 		*countp = 0;
616 		return (error);
617 	}
618 
619 	if (ip->i_ump->um_fstype == UFS1)
620 		bap1 = (ufs1_daddr_t *)bp->b_data;
621 	else
622 		bap2 = (ufs2_daddr_t *)bp->b_data;
623 	if (lastbn != -1) {
624 		copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
625 		bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
626 		for (i = last + 1; i < NINDIR(fs); i++)
627 			if (ip->i_ump->um_fstype == UFS1)
628 				bap1[i] = 0;
629 			else
630 				bap2[i] = 0;
631 		if (DOINGASYNC(vp)) {
632 			bawrite(bp);
633 		} else {
634 			error = bwrite(bp);
635 			if (error)
636 				allerror = error;
637 		}
638 		if (ip->i_ump->um_fstype == UFS1)
639 			bap1 = (ufs1_daddr_t *)copy;
640 		else
641 			bap2 = (ufs2_daddr_t *)copy;
642 	}
643 
644 	/*
645 	 * Recursively free totally unused blocks.
646 	 */
647 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
648 	    i--, nlbn += factor) {
649 		nb = BAP(ip, i);
650 		if (nb == 0)
651 			continue;
652 		if (level > SINGLE) {
653 			if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
654 			    (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
655 				allerror = error;
656 			blocksreleased += blkcount;
657 		}
658 		ffs_blkfree(ip->i_ump, fs, ip->i_devvp, nb, fs->fs_bsize,
659 		    ip->i_number, NULL);
660 		blocksreleased += nblocks;
661 	}
662 
663 	/*
664 	 * Recursively free last partial block.
665 	 */
666 	if (level > SINGLE && lastbn >= 0) {
667 		last = lastbn % factor;
668 		nb = BAP(ip, i);
669 		if (nb != 0) {
670 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
671 			    last, level - 1, &blkcount);
672 			if (error)
673 				allerror = error;
674 			blocksreleased += blkcount;
675 		}
676 	}
677 	if (copy != NULL) {
678 		free(copy, M_TEMP);
679 	} else {
680 		bp->b_flags |= B_INVAL | B_NOCACHE;
681 		brelse(bp);
682 	}
683 
684 	*countp = blocksreleased;
685 	return (allerror);
686 }
687 
688 int
689 ffs_rdonly(struct inode *ip)
690 {
691 
692 	return (ip->i_ump->um_fs->fs_ronly != 0);
693 }
694 
695