xref: /freebsd/sys/ufs/ffs/ffs_inode.c (revision 43764a7ffa9ad6eba3275410bc2397d3d398f75f)
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 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 	struct bufobj *bo;
155 	struct fs *fs;
156 	struct buf *bp;
157 	struct ufsmount *ump;
158 	int softdeptrunc, journaltrunc;
159 	int needextclean, 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 
169 	ASSERT_VOP_LOCKED(vp, "ffs_truncate");
170 
171 	if (length < 0)
172 		return (EINVAL);
173 	if (length > fs->fs_maxfilesize)
174 		return (EFBIG);
175 #ifdef QUOTA
176 	error = getinoquota(ip);
177 	if (error)
178 		return (error);
179 #endif
180 	/*
181 	 * Historically clients did not have to specify which data
182 	 * they were truncating. So, if not specified, we assume
183 	 * traditional behavior, e.g., just the normal data.
184 	 */
185 	if ((flags & (IO_EXT | IO_NORMAL)) == 0)
186 		flags |= IO_NORMAL;
187 	if (!DOINGSOFTDEP(vp) && !DOINGASYNC(vp))
188 		flags |= IO_SYNC;
189 	/*
190 	 * If we are truncating the extended-attributes, and cannot
191 	 * do it with soft updates, then do it slowly here. If we are
192 	 * truncating both the extended attributes and the file contents
193 	 * (e.g., the file is being unlinked), then pick it off with
194 	 * soft updates below.
195 	 */
196 	allerror = 0;
197 	needextclean = 0;
198 	softdeptrunc = 0;
199 	journaltrunc = DOINGSUJ(vp);
200 	if (journaltrunc == 0 && DOINGSOFTDEP(vp) && length == 0)
201 		softdeptrunc = !softdep_slowdown(vp);
202 	extblocks = 0;
203 	datablocks = DIP(ip, i_blocks);
204 	if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
205 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
206 		datablocks -= extblocks;
207 	}
208 	if ((flags & IO_EXT) && extblocks > 0) {
209 		if (length != 0)
210 			panic("ffs_truncate: partial trunc of extdata");
211 		if (softdeptrunc || journaltrunc) {
212 			if ((flags & IO_NORMAL) == 0)
213 				goto extclean;
214 			needextclean = 1;
215 		} else {
216 			if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
217 				return (error);
218 #ifdef QUOTA
219 			(void) chkdq(ip, -extblocks, NOCRED, 0);
220 #endif
221 			vinvalbuf(vp, V_ALT, 0, 0);
222 			ffs_pages_remove(vp,
223 			    OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
224 			osize = ip->i_din2->di_extsize;
225 			ip->i_din2->di_blocks -= extblocks;
226 			ip->i_din2->di_extsize = 0;
227 			for (i = 0; i < NXADDR; i++) {
228 				oldblks[i] = ip->i_din2->di_extb[i];
229 				ip->i_din2->di_extb[i] = 0;
230 			}
231 			ip->i_flag |= IN_CHANGE;
232 			if ((error = ffs_update(vp, 1)))
233 				return (error);
234 			for (i = 0; i < NXADDR; i++) {
235 				if (oldblks[i] == 0)
236 					continue;
237 				ffs_blkfree(ump, fs, ip->i_devvp, oldblks[i],
238 				    sblksize(fs, osize, i), ip->i_number,
239 				    vp->v_type, NULL);
240 			}
241 		}
242 	}
243 	if ((flags & IO_NORMAL) == 0)
244 		return (0);
245 	if (vp->v_type == VLNK &&
246 	    (ip->i_size < vp->v_mount->mnt_maxsymlinklen ||
247 	     datablocks == 0)) {
248 #ifdef INVARIANTS
249 		if (length != 0)
250 			panic("ffs_truncate: partial truncate of symlink");
251 #endif
252 		bzero(SHORTLINK(ip), (u_int)ip->i_size);
253 		ip->i_size = 0;
254 		DIP_SET(ip, i_size, 0);
255 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
256 		if (needextclean)
257 			goto extclean;
258 		return ffs_update(vp, 1);
259 	}
260 	if (ip->i_size == length) {
261 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
262 		if (needextclean)
263 			goto extclean;
264 		return ffs_update(vp, 0);
265 	}
266 	if (fs->fs_ronly)
267 		panic("ffs_truncate: read-only filesystem");
268 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
269 		ffs_snapremove(vp);
270 	vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
271 	osize = ip->i_size;
272 	/*
273 	 * Lengthen the size of the file. We must ensure that the
274 	 * last byte of the file is allocated. Since the smallest
275 	 * value of osize is 0, length will be at least 1.
276 	 */
277 	if (osize < length) {
278 		vnode_pager_setsize(vp, length);
279 		flags |= BA_CLRBUF;
280 		error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
281 		if (error) {
282 			vnode_pager_setsize(vp, osize);
283 			return (error);
284 		}
285 		ip->i_size = length;
286 		DIP_SET(ip, i_size, length);
287 		if (bp->b_bufsize == fs->fs_bsize)
288 			bp->b_flags |= B_CLUSTEROK;
289 		if (flags & IO_SYNC)
290 			bwrite(bp);
291 		else
292 			bawrite(bp);
293 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
294 		return ffs_update(vp, 1);
295 	}
296 	if (DOINGSOFTDEP(vp)) {
297 		if (softdeptrunc == 0 && journaltrunc == 0) {
298 			/*
299 			 * If a file is only partially truncated, then
300 			 * we have to clean up the data structures
301 			 * describing the allocation past the truncation
302 			 * point. Finding and deallocating those structures
303 			 * is a lot of work. Since partial truncation occurs
304 			 * rarely, we solve the problem by syncing the file
305 			 * so that it will have no data structures left.
306 			 */
307 			if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
308 				return (error);
309 		} else {
310 			flags = IO_NORMAL | (needextclean ? IO_EXT: 0);
311 			if (journaltrunc)
312 				softdep_journal_freeblocks(ip, cred, length,
313 				    flags);
314 			else
315 				softdep_setup_freeblocks(ip, length, flags);
316 			ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
317 			if (journaltrunc == 0) {
318 				ip->i_flag |= IN_CHANGE | IN_UPDATE;
319 				error = ffs_update(vp, 0);
320 			}
321 			return (error);
322 		}
323 	}
324 	/*
325 	 * Shorten the size of the file. If the file is not being
326 	 * truncated to a block boundary, the contents of the
327 	 * partial block following the end of the file must be
328 	 * zero'ed in case it ever becomes accessible again because
329 	 * of subsequent file growth. Directories however are not
330 	 * zero'ed as they should grow back initialized to empty.
331 	 */
332 	offset = blkoff(fs, length);
333 	if (offset == 0) {
334 		ip->i_size = length;
335 		DIP_SET(ip, i_size, length);
336 	} else {
337 		lbn = lblkno(fs, length);
338 		flags |= BA_CLRBUF;
339 		error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
340 		if (error)
341 			return (error);
342 		/*
343 		 * When we are doing soft updates and the UFS_BALLOC
344 		 * above fills in a direct block hole with a full sized
345 		 * block that will be truncated down to a fragment below,
346 		 * we must flush out the block dependency with an FSYNC
347 		 * so that we do not get a soft updates inconsistency
348 		 * when we create the fragment below.
349 		 */
350 		if (DOINGSOFTDEP(vp) && lbn < NDADDR &&
351 		    fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
352 		    (error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
353 			return (error);
354 		ip->i_size = length;
355 		DIP_SET(ip, i_size, length);
356 		size = blksize(fs, ip, lbn);
357 		if (vp->v_type != VDIR)
358 			bzero((char *)bp->b_data + offset,
359 			    (u_int)(size - offset));
360 		/* Kirk's code has reallocbuf(bp, size, 1) here */
361 		allocbuf(bp, size);
362 		if (bp->b_bufsize == fs->fs_bsize)
363 			bp->b_flags |= B_CLUSTEROK;
364 		if (flags & IO_SYNC)
365 			bwrite(bp);
366 		else
367 			bawrite(bp);
368 	}
369 	/*
370 	 * Calculate index into inode's block list of
371 	 * last direct and indirect blocks (if any)
372 	 * which we want to keep.  Lastblock is -1 when
373 	 * the file is truncated to 0.
374 	 */
375 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
376 	lastiblock[SINGLE] = lastblock - NDADDR;
377 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
378 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
379 	nblocks = btodb(fs->fs_bsize);
380 	/*
381 	 * Update file and block pointers on disk before we start freeing
382 	 * blocks.  If we crash before free'ing blocks below, the blocks
383 	 * will be returned to the free list.  lastiblock values are also
384 	 * normalized to -1 for calls to ffs_indirtrunc below.
385 	 */
386 	for (level = TRIPLE; level >= SINGLE; level--) {
387 		oldblks[NDADDR + level] = DIP(ip, i_ib[level]);
388 		if (lastiblock[level] < 0) {
389 			DIP_SET(ip, i_ib[level], 0);
390 			lastiblock[level] = -1;
391 		}
392 	}
393 	for (i = 0; i < NDADDR; i++) {
394 		oldblks[i] = DIP(ip, i_db[i]);
395 		if (i > lastblock)
396 			DIP_SET(ip, i_db[i], 0);
397 	}
398 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
399 	allerror = ffs_update(vp, 1);
400 
401 	/*
402 	 * Having written the new inode to disk, save its new configuration
403 	 * and put back the old block pointers long enough to process them.
404 	 * Note that we save the new block configuration so we can check it
405 	 * when we are done.
406 	 */
407 	for (i = 0; i < NDADDR; i++) {
408 		newblks[i] = DIP(ip, i_db[i]);
409 		DIP_SET(ip, i_db[i], oldblks[i]);
410 	}
411 	for (i = 0; i < NIADDR; i++) {
412 		newblks[NDADDR + i] = DIP(ip, i_ib[i]);
413 		DIP_SET(ip, i_ib[i], oldblks[NDADDR + i]);
414 	}
415 	ip->i_size = osize;
416 	DIP_SET(ip, i_size, osize);
417 
418 	error = vtruncbuf(vp, cred, td, length, fs->fs_bsize);
419 	if (error && (allerror == 0))
420 		allerror = error;
421 
422 	/*
423 	 * Indirect blocks first.
424 	 */
425 	indir_lbn[SINGLE] = -NDADDR;
426 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
427 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
428 	for (level = TRIPLE; level >= SINGLE; level--) {
429 		bn = DIP(ip, i_ib[level]);
430 		if (bn != 0) {
431 			error = ffs_indirtrunc(ip, indir_lbn[level],
432 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
433 			if (error)
434 				allerror = error;
435 			blocksreleased += count;
436 			if (lastiblock[level] < 0) {
437 				DIP_SET(ip, i_ib[level], 0);
438 				ffs_blkfree(ump, fs, ip->i_devvp, bn,
439 				    fs->fs_bsize, ip->i_number,
440 				    vp->v_type, NULL);
441 				blocksreleased += nblocks;
442 			}
443 		}
444 		if (lastiblock[level] >= 0)
445 			goto done;
446 	}
447 
448 	/*
449 	 * All whole direct blocks or frags.
450 	 */
451 	for (i = NDADDR - 1; i > lastblock; i--) {
452 		long bsize;
453 
454 		bn = DIP(ip, i_db[i]);
455 		if (bn == 0)
456 			continue;
457 		DIP_SET(ip, i_db[i], 0);
458 		bsize = blksize(fs, ip, i);
459 		ffs_blkfree(ump, fs, ip->i_devvp, bn, bsize, ip->i_number,
460 		    vp->v_type, NULL);
461 		blocksreleased += btodb(bsize);
462 	}
463 	if (lastblock < 0)
464 		goto done;
465 
466 	/*
467 	 * Finally, look for a change in size of the
468 	 * last direct block; release any frags.
469 	 */
470 	bn = DIP(ip, i_db[lastblock]);
471 	if (bn != 0) {
472 		long oldspace, newspace;
473 
474 		/*
475 		 * Calculate amount of space we're giving
476 		 * back as old block size minus new block size.
477 		 */
478 		oldspace = blksize(fs, ip, lastblock);
479 		ip->i_size = length;
480 		DIP_SET(ip, i_size, length);
481 		newspace = blksize(fs, ip, lastblock);
482 		if (newspace == 0)
483 			panic("ffs_truncate: newspace");
484 		if (oldspace - newspace > 0) {
485 			/*
486 			 * Block number of space to be free'd is
487 			 * the old block # plus the number of frags
488 			 * required for the storage we're keeping.
489 			 */
490 			bn += numfrags(fs, newspace);
491 			ffs_blkfree(ump, fs, ip->i_devvp, bn,
492 			   oldspace - newspace, ip->i_number, vp->v_type, NULL);
493 			blocksreleased += btodb(oldspace - newspace);
494 		}
495 	}
496 done:
497 #ifdef INVARIANTS
498 	for (level = SINGLE; level <= TRIPLE; level++)
499 		if (newblks[NDADDR + level] != DIP(ip, i_ib[level]))
500 			panic("ffs_truncate1");
501 	for (i = 0; i < NDADDR; i++)
502 		if (newblks[i] != DIP(ip, i_db[i]))
503 			panic("ffs_truncate2");
504 	BO_LOCK(bo);
505 	if (length == 0 &&
506 	    (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
507 	    (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
508 		panic("ffs_truncate3");
509 	BO_UNLOCK(bo);
510 #endif /* INVARIANTS */
511 	/*
512 	 * Put back the real size.
513 	 */
514 	ip->i_size = length;
515 	DIP_SET(ip, i_size, length);
516 	DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
517 
518 	if (DIP(ip, i_blocks) < 0)			/* sanity */
519 		DIP_SET(ip, i_blocks, 0);
520 	ip->i_flag |= IN_CHANGE;
521 #ifdef QUOTA
522 	(void) chkdq(ip, -blocksreleased, NOCRED, 0);
523 #endif
524 	return (allerror);
525 
526 extclean:
527 	if (journaltrunc)
528 		softdep_journal_freeblocks(ip, cred, length, IO_EXT);
529 	else
530 		softdep_setup_freeblocks(ip, length, IO_EXT);
531 	return ffs_update(vp, MNT_WAIT);
532 }
533 
534 /*
535  * Release blocks associated with the inode ip and stored in the indirect
536  * block bn.  Blocks are free'd in LIFO order up to (but not including)
537  * lastbn.  If level is greater than SINGLE, the block is an indirect block
538  * and recursive calls to indirtrunc must be used to cleanse other indirect
539  * blocks.
540  */
541 static int
542 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
543 	struct inode *ip;
544 	ufs2_daddr_t lbn, lastbn;
545 	ufs2_daddr_t dbn;
546 	int level;
547 	ufs2_daddr_t *countp;
548 {
549 	struct buf *bp;
550 	struct fs *fs = ip->i_fs;
551 	struct vnode *vp;
552 	caddr_t copy = NULL;
553 	int i, nblocks, error = 0, allerror = 0;
554 	ufs2_daddr_t nb, nlbn, last;
555 	ufs2_daddr_t blkcount, factor, blocksreleased = 0;
556 	ufs1_daddr_t *bap1 = NULL;
557 	ufs2_daddr_t *bap2 = NULL;
558 #	define BAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? bap1[i] : bap2[i])
559 
560 	/*
561 	 * Calculate index in current block of last
562 	 * block to be kept.  -1 indicates the entire
563 	 * block so we need not calculate the index.
564 	 */
565 	factor = lbn_offset(fs, level);
566 	last = lastbn;
567 	if (lastbn > 0)
568 		last /= factor;
569 	nblocks = btodb(fs->fs_bsize);
570 	/*
571 	 * Get buffer of block pointers, zero those entries corresponding
572 	 * to blocks to be free'd, and update on disk copy first.  Since
573 	 * double(triple) indirect before single(double) indirect, calls
574 	 * to bmap on these blocks will fail.  However, we already have
575 	 * the on disk address, so we have to set the b_blkno field
576 	 * explicitly instead of letting bread do everything for us.
577 	 */
578 	vp = ITOV(ip);
579 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0, 0);
580 	if ((bp->b_flags & B_CACHE) == 0) {
581 		curthread->td_ru.ru_inblock++;	/* pay for read */
582 		bp->b_iocmd = BIO_READ;
583 		bp->b_flags &= ~B_INVAL;
584 		bp->b_ioflags &= ~BIO_ERROR;
585 		if (bp->b_bcount > bp->b_bufsize)
586 			panic("ffs_indirtrunc: bad buffer size");
587 		bp->b_blkno = dbn;
588 		vfs_busy_pages(bp, 0);
589 		bp->b_iooffset = dbtob(bp->b_blkno);
590 		bstrategy(bp);
591 		error = bufwait(bp);
592 	}
593 	if (error) {
594 		brelse(bp);
595 		*countp = 0;
596 		return (error);
597 	}
598 
599 	if (ip->i_ump->um_fstype == UFS1)
600 		bap1 = (ufs1_daddr_t *)bp->b_data;
601 	else
602 		bap2 = (ufs2_daddr_t *)bp->b_data;
603 	if (lastbn != -1) {
604 		copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
605 		bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
606 		for (i = last + 1; i < NINDIR(fs); i++)
607 			if (ip->i_ump->um_fstype == UFS1)
608 				bap1[i] = 0;
609 			else
610 				bap2[i] = 0;
611 		if (DOINGASYNC(vp)) {
612 			bawrite(bp);
613 		} else {
614 			error = bwrite(bp);
615 			if (error)
616 				allerror = error;
617 		}
618 		if (ip->i_ump->um_fstype == UFS1)
619 			bap1 = (ufs1_daddr_t *)copy;
620 		else
621 			bap2 = (ufs2_daddr_t *)copy;
622 	}
623 
624 	/*
625 	 * Recursively free totally unused blocks.
626 	 */
627 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
628 	    i--, nlbn += factor) {
629 		nb = BAP(ip, i);
630 		if (nb == 0)
631 			continue;
632 		if (level > SINGLE) {
633 			if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
634 			    (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
635 				allerror = error;
636 			blocksreleased += blkcount;
637 		}
638 		ffs_blkfree(ip->i_ump, fs, ip->i_devvp, nb, fs->fs_bsize,
639 		    ip->i_number, vp->v_type, NULL);
640 		blocksreleased += nblocks;
641 	}
642 
643 	/*
644 	 * Recursively free last partial block.
645 	 */
646 	if (level > SINGLE && lastbn >= 0) {
647 		last = lastbn % factor;
648 		nb = BAP(ip, i);
649 		if (nb != 0) {
650 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
651 			    last, level - 1, &blkcount);
652 			if (error)
653 				allerror = error;
654 			blocksreleased += blkcount;
655 		}
656 	}
657 	if (copy != NULL) {
658 		free(copy, M_TEMP);
659 	} else {
660 		bp->b_flags |= B_INVAL | B_NOCACHE;
661 		brelse(bp);
662 	}
663 
664 	*countp = blocksreleased;
665 	return (allerror);
666 }
667 
668 int
669 ffs_rdonly(struct inode *ip)
670 {
671 
672 	return (ip->i_ump->um_fs->fs_ronly != 0);
673 }
674 
675