2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
47 #include <sys/racct.h>
48 #include <sys/random.h>
49 #include <sys/resourcevar.h>
50 #include <sys/rwlock.h>
52 #include <sys/vmmeter.h>
53 #include <sys/vnode.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_object.h>
59 #include <ufs/ufs/extattr.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/ufsmount.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ufs/dir.h>
65 #include <ufs/ufs/dirhash.h>
67 #include <ufs/ufs/ufs_extern.h>
69 #include <ufs/ffs/fs.h>
70 #include <ufs/ffs/ffs_extern.h>
72 static int ffs_indirtrunc(struct inode *, ufs2_daddr_t, ufs2_daddr_t,
73 ufs2_daddr_t, int, ufs2_daddr_t *);
76 ffs_inode_bwrite(struct vnode *vp, struct buf *bp, int flags)
78 if ((flags & IO_SYNC) != 0)
80 else if (DOINGASYNC(vp))
87 * Update the access, modified, and inode change times as specified by the
88 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
89 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
90 * the timestamp update). The IN_LAZYMOD flag is set to force a write
91 * later if not now. The IN_LAZYACCESS is set instead of IN_MODIFIED if the fs
92 * is currently being suspended (or is suspended) and vnode has been accessed.
93 * If we write now, then clear IN_MODIFIED, IN_LAZYACCESS and IN_LAZYMOD to
94 * reflect the presumably successful write, and if waitfor is set, then wait
95 * for the write to complete.
98 ffs_update(struct vnode *vp, int waitfor)
106 ASSERT_VOP_ELOCKED(vp, "ffs_update");
109 if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
111 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
113 * The IN_SIZEMOD and IN_IBLKDATA flags indicate changes to the
114 * file size and block pointer fields in the inode. When these
115 * fields have been changed, the fsync() and fsyncdata() system
116 * calls must write the inode to ensure their semantics that the
117 * file is on stable store.
119 * The IN_SIZEMOD and IN_IBLKDATA flags cannot be cleared until
120 * a synchronous write of the inode is done. If they are cleared
121 * on an asynchronous write, then the inode may not yet have been
122 * written to the disk when an fsync() or fsyncdata() call is done.
123 * Absent these flags, these calls would not know that they needed
124 * to write the inode. Thus, these flags only can be cleared on
125 * synchronous writes of the inode. Since the inode will be locked
126 * for the duration of the I/O that writes it to disk, no fsync()
127 * or fsyncdata() will be able to run before the on-disk inode
131 ip->i_flag &= ~(IN_SIZEMOD | IN_IBLKDATA);
136 * If we are updating a snapshot and another process is currently
137 * writing the buffer containing the inode for this snapshot then
138 * a deadlock can occur when it tries to check the snapshot to see
139 * if that block needs to be copied. Thus when updating a snapshot
140 * we check to see if the buffer is already locked, and if it is
141 * we drop the snapshot lock until the buffer has been written
142 * and is available to us. We have to grab a reference to the
143 * snapshot vnode to prevent it from being removed while we are
144 * waiting for the buffer.
149 flags = GB_LOCK_NOWAIT;
150 bn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
151 error = ffs_breadz(VFSTOUFS(vp->v_mount), ITODEVVP(ip), bn, bn,
152 (int) fs->fs_bsize, NULL, NULL, 0, NOCRED, flags, NULL, &bp);
155 * If EBUSY was returned without GB_LOCK_NOWAIT (which
156 * requests trylock for buffer lock), it is for some
157 * other reason and we should not handle it specially.
159 if (error != EBUSY || (flags & GB_LOCK_NOWAIT) == 0)
163 * Wait for our inode block to become available.
165 * Hold a reference to the vnode to protect against
166 * ffs_snapgone(). Since we hold a reference, it can only
167 * get reclaimed (VIRF_DOOMED flag) in a forcible downgrade
168 * or unmount. For an unmount, the entire filesystem will be
169 * gone, so we cannot attempt to touch anything associated
170 * with it while the vnode is unlocked; all we can do is
171 * pause briefly and try again. If when we relock the vnode
172 * we discover that it has been reclaimed, updating it is no
173 * longer necessary and we can just return an error.
178 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
184 * Recalculate flags, because the vnode was relocked and
185 * could no longer be a snapshot.
189 if (DOINGSOFTDEP(vp))
190 softdep_update_inodeblock(ip, bp, waitfor);
191 else if (ip->i_effnlink != ip->i_nlink)
192 panic("ffs_update: bad link cnt");
194 *((struct ufs1_dinode *)bp->b_data +
195 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
197 * XXX: FIX? The entropy here is desirable,
198 * but the harvesting may be expensive
200 random_harvest_queue(&(ip->i_din1), sizeof(ip->i_din1), RANDOM_FS_ATIME);
202 ffs_update_dinode_ckhash(fs, ip->i_din2);
203 *((struct ufs2_dinode *)bp->b_data +
204 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
206 * XXX: FIX? The entropy here is desirable,
207 * but the harvesting may be expensive
209 random_harvest_queue(&(ip->i_din2), sizeof(ip->i_din2), RANDOM_FS_ATIME);
213 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
215 } else if (vm_page_count_severe() || buf_dirty_count_severe()) {
219 if (bp->b_bufsize == fs->fs_bsize)
220 bp->b_flags |= B_CLUSTEROK;
227 #define SINGLE 0 /* index of single indirect block */
228 #define DOUBLE 1 /* index of double indirect block */
229 #define TRIPLE 2 /* index of triple indirect block */
231 * Truncate the inode ip to at most length size, freeing the
235 ffs_truncate(struct vnode *vp,
241 ufs2_daddr_t bn, lbn, lastblock, lastiblock[UFS_NIADDR];
242 ufs2_daddr_t indir_lbn[UFS_NIADDR], oldblks[UFS_NDADDR + UFS_NIADDR];
243 ufs2_daddr_t newblks[UFS_NDADDR + UFS_NIADDR];
244 ufs2_daddr_t count, blocksreleased = 0, blkno;
245 struct bufobj *bo __diagused;
248 struct ufsmount *ump;
249 int softdeptrunc, journaltrunc;
250 int needextclean, extblocks;
251 int offset, size, level, nblocks;
252 int i, error, allerror, indiroff, waitforupdate;
257 ump = VFSTOUFS(vp->v_mount);
261 ASSERT_VOP_LOCKED(vp, "ffs_truncate");
265 if (length > fs->fs_maxfilesize)
268 error = getinoquota(ip);
273 * Historically clients did not have to specify which data
274 * they were truncating. So, if not specified, we assume
275 * traditional behavior, e.g., just the normal data.
277 if ((flags & (IO_EXT | IO_NORMAL)) == 0)
279 if (!DOINGSOFTDEP(vp) && !DOINGASYNC(vp))
281 waitforupdate = (flags & IO_SYNC) != 0 || !DOINGASYNC(vp);
283 * If we are truncating the extended-attributes, and cannot
284 * do it with soft updates, then do it slowly here. If we are
285 * truncating both the extended attributes and the file contents
286 * (e.g., the file is being unlinked), then pick it off with
287 * soft updates below.
292 journaltrunc = DOINGSUJ(vp);
293 journaltrunc = 0; /* XXX temp patch until bug found */
294 if (journaltrunc == 0 && DOINGSOFTDEP(vp) && length == 0)
295 softdeptrunc = !softdep_slowdown(vp);
297 if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
298 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
300 if ((flags & IO_EXT) && extblocks > 0) {
302 panic("ffs_truncate: partial trunc of extdata");
303 if (softdeptrunc || journaltrunc) {
304 if ((flags & IO_NORMAL) == 0)
308 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
311 (void) chkdq(ip, -extblocks, NOCRED, FORCE);
313 vinvalbuf(vp, V_ALT, 0, 0);
315 OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
316 osize = ip->i_din2->di_extsize;
317 ip->i_din2->di_blocks -= extblocks;
318 ip->i_din2->di_extsize = 0;
319 for (i = 0; i < UFS_NXADDR; i++) {
320 oldblks[i] = ip->i_din2->di_extb[i];
321 ip->i_din2->di_extb[i] = 0;
323 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
324 if ((error = ffs_update(vp, waitforupdate)))
326 for (i = 0; i < UFS_NXADDR; i++) {
329 ffs_blkfree(ump, fs, ITODEVVP(ip), oldblks[i],
330 sblksize(fs, osize, i), ip->i_number,
331 vp->v_type, NULL, SINGLETON_KEY);
335 if ((flags & IO_NORMAL) == 0)
337 if (vp->v_type == VLNK && ip->i_size < ump->um_maxsymlinklen) {
340 panic("ffs_truncate: partial truncate of symlink");
342 bzero(DIP(ip, i_shortlink), (uint64_t)ip->i_size);
344 DIP_SET(ip, i_size, 0);
345 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
348 return (ffs_update(vp, waitforupdate));
350 if (ip->i_size == length) {
351 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
354 return (ffs_update(vp, 0));
357 panic("ffs_truncate: read-only filesystem");
360 cluster_init_vn(&ip->i_clusterw);
363 * Lengthen the size of the file. We must ensure that the
364 * last byte of the file is allocated. Since the smallest
365 * value of osize is 0, length will be at least 1.
367 if (osize < length) {
368 vnode_pager_setsize(vp, length);
370 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
372 vnode_pager_setsize(vp, osize);
376 DIP_SET(ip, i_size, length);
377 if (bp->b_bufsize == fs->fs_bsize)
378 bp->b_flags |= B_CLUSTEROK;
379 ffs_inode_bwrite(vp, bp, flags);
380 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
381 return (ffs_update(vp, waitforupdate));
384 * Lookup block number for a given offset. Zero length files
385 * have no blocks, so return a blkno of -1.
387 lbn = lblkno(fs, length - 1);
390 } else if (lbn < UFS_NDADDR) {
391 blkno = DIP(ip, i_db[lbn]);
393 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), fs->fs_bsize,
394 cred, BA_METAONLY, &bp);
397 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
399 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
401 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
403 * If the block number is non-zero, then the indirect block
404 * must have been previously allocated and need not be written.
405 * If the block number is zero, then we may have allocated
406 * the indirect block and hence need to write it out.
410 else if (flags & IO_SYNC)
416 * If the block number at the new end of the file is zero,
417 * then we must allocate it to ensure that the last block of
418 * the file is allocated. Soft updates does not handle this
419 * case, so here we have to clean up the soft updates data
420 * structures describing the allocation past the truncation
421 * point. Finding and deallocating those structures is a lot of
422 * work. Since partial truncation with a hole at the end occurs
423 * rarely, we solve the problem by syncing the file so that it
424 * will have no soft updates data structures left.
426 if (blkno == 0 && (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
428 if (blkno != 0 && DOINGSOFTDEP(vp)) {
429 if (softdeptrunc == 0 && journaltrunc == 0) {
431 * If soft updates cannot handle this truncation,
432 * clean up soft dependency data structures and
433 * fall through to the synchronous truncation.
435 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
438 flags = IO_NORMAL | (needextclean ? IO_EXT: 0);
440 softdep_journal_freeblocks(ip, cred, length,
443 softdep_setup_freeblocks(ip, length, flags);
444 ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
445 if (journaltrunc == 0) {
446 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
447 error = ffs_update(vp, 0);
453 * Shorten the size of the file. If the last block of the
454 * shortened file is unallocated, we must allocate it.
455 * Additionally, if the file is not being truncated to a
456 * block boundary, the contents of the partial block
457 * following the end of the file must be zero'ed in
458 * case it ever becomes accessible again because of
459 * subsequent file growth. Directories however are not
460 * zero'ed as they should grow back initialized to empty.
462 offset = blkoff(fs, length);
463 if (blkno != 0 && offset == 0) {
465 DIP_SET(ip, i_size, length);
466 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
468 if (vp->v_type == VDIR && ip->i_dirhash != NULL)
469 ufsdirhash_dirtrunc(ip, length);
472 lbn = lblkno(fs, length);
474 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
477 ffs_inode_bwrite(vp, bp, flags);
480 * When we are doing soft updates and the UFS_BALLOC
481 * above fills in a direct block hole with a full sized
482 * block that will be truncated down to a fragment below,
483 * we must flush out the block dependency with an FSYNC
484 * so that we do not get a soft updates inconsistency
485 * when we create the fragment below.
487 if (DOINGSOFTDEP(vp) && lbn < UFS_NDADDR &&
488 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
489 (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
492 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
496 DIP_SET(ip, i_size, length);
498 if (vp->v_type == VDIR && ip->i_dirhash != NULL)
499 ufsdirhash_dirtrunc(ip, length);
501 size = blksize(fs, ip, lbn);
502 if (vp->v_type != VDIR && offset != 0)
503 bzero((char *)bp->b_data + offset,
504 (uint64_t)(size - offset));
505 /* Kirk's code has reallocbuf(bp, size, 1) here */
507 if (bp->b_bufsize == fs->fs_bsize)
508 bp->b_flags |= B_CLUSTEROK;
509 ffs_inode_bwrite(vp, bp, flags);
510 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
513 * Calculate index into inode's block list of
514 * last direct and indirect blocks (if any)
515 * which we want to keep. Lastblock is -1 when
516 * the file is truncated to 0.
518 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
519 lastiblock[SINGLE] = lastblock - UFS_NDADDR;
520 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
521 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
522 nblocks = btodb(fs->fs_bsize);
524 * Update file and block pointers on disk before we start freeing
525 * blocks. If we crash before free'ing blocks below, the blocks
526 * will be returned to the free list. lastiblock values are also
527 * normalized to -1 for calls to ffs_indirtrunc below.
529 for (level = TRIPLE; level >= SINGLE; level--) {
530 oldblks[UFS_NDADDR + level] = DIP(ip, i_ib[level]);
531 if (lastiblock[level] < 0) {
532 DIP_SET(ip, i_ib[level], 0);
533 lastiblock[level] = -1;
536 for (i = 0; i < UFS_NDADDR; i++) {
537 oldblks[i] = DIP(ip, i_db[i]);
539 DIP_SET(ip, i_db[i], 0);
541 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
542 allerror = ffs_update(vp, waitforupdate);
545 * Having written the new inode to disk, save its new configuration
546 * and put back the old block pointers long enough to process them.
547 * Note that we save the new block configuration so we can check it
550 for (i = 0; i < UFS_NDADDR; i++) {
551 newblks[i] = DIP(ip, i_db[i]);
552 DIP_SET(ip, i_db[i], oldblks[i]);
554 for (i = 0; i < UFS_NIADDR; i++) {
555 newblks[UFS_NDADDR + i] = DIP(ip, i_ib[i]);
556 DIP_SET(ip, i_ib[i], oldblks[UFS_NDADDR + i]);
559 DIP_SET(ip, i_size, osize);
560 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
562 error = vtruncbuf(vp, length, fs->fs_bsize);
563 if (error && (allerror == 0))
567 * Indirect blocks first.
569 indir_lbn[SINGLE] = -UFS_NDADDR;
570 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
571 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
572 for (level = TRIPLE; level >= SINGLE; level--) {
573 bn = DIP(ip, i_ib[level]);
575 error = ffs_indirtrunc(ip, indir_lbn[level],
576 fsbtodb(fs, bn), lastiblock[level], level, &count);
579 blocksreleased += count;
580 if (lastiblock[level] < 0) {
581 DIP_SET(ip, i_ib[level], 0);
582 ffs_blkfree(ump, fs, ump->um_devvp, bn,
583 fs->fs_bsize, ip->i_number,
584 vp->v_type, NULL, SINGLETON_KEY);
585 blocksreleased += nblocks;
588 if (lastiblock[level] >= 0)
593 * All whole direct blocks or frags.
595 key = ffs_blkrelease_start(ump, ump->um_devvp, ip->i_number);
596 for (i = UFS_NDADDR - 1; i > lastblock; i--) {
599 bn = DIP(ip, i_db[i]);
602 DIP_SET(ip, i_db[i], 0);
603 bsize = blksize(fs, ip, i);
604 ffs_blkfree(ump, fs, ump->um_devvp, bn, bsize, ip->i_number,
605 vp->v_type, NULL, key);
606 blocksreleased += btodb(bsize);
608 ffs_blkrelease_finish(ump, key);
613 * Finally, look for a change in size of the
614 * last direct block; release any frags.
616 bn = DIP(ip, i_db[lastblock]);
618 long oldspace, newspace;
621 * Calculate amount of space we're giving
622 * back as old block size minus new block size.
624 oldspace = blksize(fs, ip, lastblock);
626 DIP_SET(ip, i_size, length);
627 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
628 newspace = blksize(fs, ip, lastblock);
630 panic("ffs_truncate: newspace");
631 if (oldspace - newspace > 0) {
633 * Block number of space to be free'd is
634 * the old block # plus the number of frags
635 * required for the storage we're keeping.
637 bn += numfrags(fs, newspace);
638 ffs_blkfree(ump, fs, ump->um_devvp, bn,
639 oldspace - newspace, ip->i_number, vp->v_type,
640 NULL, SINGLETON_KEY);
641 blocksreleased += btodb(oldspace - newspace);
646 for (level = SINGLE; level <= TRIPLE; level++)
647 if (newblks[UFS_NDADDR + level] != DIP(ip, i_ib[level]))
648 panic("ffs_truncate1: level %d newblks %jd != i_ib %jd",
649 level, (intmax_t)newblks[UFS_NDADDR + level],
650 (intmax_t)DIP(ip, i_ib[level]));
651 for (i = 0; i < UFS_NDADDR; i++)
652 if (newblks[i] != DIP(ip, i_db[i]))
653 panic("ffs_truncate2: blkno %d newblks %jd != i_db %jd",
654 i, (intmax_t)newblks[UFS_NDADDR + level],
655 (intmax_t)DIP(ip, i_ib[level]));
658 (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
659 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
660 panic("ffs_truncate3: vp = %p, buffers: dirty = %d, clean = %d",
661 vp, bo->bo_dirty.bv_cnt, bo->bo_clean.bv_cnt);
663 #endif /* INVARIANTS */
665 * Put back the real size.
668 DIP_SET(ip, i_size, length);
669 if (DIP(ip, i_blocks) >= blocksreleased)
670 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
672 DIP_SET(ip, i_blocks, 0);
673 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
675 (void) chkdq(ip, -blocksreleased, NOCRED, FORCE);
681 softdep_journal_freeblocks(ip, cred, length, IO_EXT);
683 softdep_setup_freeblocks(ip, length, IO_EXT);
684 return (ffs_update(vp, waitforupdate));
688 * Release blocks associated with the inode ip and stored in the indirect
689 * block bn. Blocks are free'd in LIFO order up to (but not including)
690 * lastbn. If level is greater than SINGLE, the block is an indirect block
691 * and recursive calls to indirtrunc must be used to cleanse other indirect
695 ffs_indirtrunc(struct inode *ip,
700 ufs2_daddr_t *countp)
704 struct ufsmount *ump;
708 int i, nblocks, error = 0, allerror = 0;
709 ufs2_daddr_t nb, nlbn, last;
710 ufs2_daddr_t blkcount, factor, blocksreleased = 0;
711 ufs1_daddr_t *bap1 = NULL;
712 ufs2_daddr_t *bap2 = NULL;
713 #define BAP(ip, i) (I_IS_UFS1(ip) ? bap1[i] : bap2[i])
719 * Calculate index in current block of last
720 * block to be kept. -1 indicates the entire
721 * block so we need not calculate the index.
723 factor = lbn_offset(fs, level);
727 nblocks = btodb(fs->fs_bsize);
729 * Get buffer of block pointers, zero those entries corresponding
730 * to blocks to be free'd, and update on disk copy first. Since
731 * double(triple) indirect before single(double) indirect, calls
732 * to VOP_BMAP() on these blocks will fail. However, we already
733 * have the on-disk address, so we just pass it to bread() instead
734 * of having bread() attempt to calculate it using VOP_BMAP().
737 error = ffs_breadz(ump, vp, lbn, dbn, (int)fs->fs_bsize, NULL, NULL, 0,
738 NOCRED, 0, NULL, &bp);
745 bap1 = (ufs1_daddr_t *)bp->b_data;
747 bap2 = (ufs2_daddr_t *)bp->b_data;
749 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
750 bcopy((caddr_t)bp->b_data, copy, (uint64_t)fs->fs_bsize);
751 for (i = last + 1; i < NINDIR(fs); i++)
756 if (DOINGASYNC(vp)) {
764 bap1 = (ufs1_daddr_t *)copy;
766 bap2 = (ufs2_daddr_t *)copy;
770 * Recursively free totally unused blocks.
772 key = ffs_blkrelease_start(ump, ITODEVVP(ip), ip->i_number);
773 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
774 i--, nlbn += factor) {
778 if (level > SINGLE) {
779 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
780 (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
782 blocksreleased += blkcount;
784 ffs_blkfree(ump, fs, ITODEVVP(ip), nb, fs->fs_bsize,
785 ip->i_number, vp->v_type, NULL, key);
786 blocksreleased += nblocks;
788 ffs_blkrelease_finish(ump, key);
791 * Recursively free last partial block.
793 if (level > SINGLE && lastbn >= 0) {
794 last = lastbn % factor;
797 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
798 last, level - 1, &blkcount);
801 blocksreleased += blkcount;
807 bp->b_flags |= B_INVAL | B_NOCACHE;
811 *countp = blocksreleased;
816 ffs_rdonly(struct inode *ip)
819 return (ITOFS(ip)->fs_ronly != 0);