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