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