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$");
37 #include "opt_quota.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
46 #include <sys/racct.h>
47 #include <sys/random.h>
48 #include <sys/resourcevar.h>
49 #include <sys/rwlock.h>
51 #include <sys/vmmeter.h>
52 #include <sys/vnode.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_object.h>
58 #include <ufs/ufs/extattr.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_extern.h>
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
67 static int ffs_indirtrunc(struct inode *, ufs2_daddr_t, ufs2_daddr_t,
68 ufs2_daddr_t, int, ufs2_daddr_t *);
71 ffs_inode_bwrite(struct vnode *vp, struct buf *bp, int flags)
73 if ((flags & IO_SYNC) != 0)
75 else if (DOINGASYNC(vp))
82 * Update the access, modified, and inode change times as specified by the
83 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
84 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
85 * the timestamp update). The IN_LAZYMOD flag is set to force a write
86 * later if not now. The IN_LAZYACCESS is set instead of IN_MODIFIED if the fs
87 * is currently being suspended (or is suspended) and vnode has been accessed.
88 * If we write now, then clear IN_MODIFIED, IN_LAZYACCESS and IN_LAZYMOD to
89 * reflect the presumably successful write, and if waitfor is set, then wait
90 * for the write to complete.
93 ffs_update(vp, waitfor)
103 ASSERT_VOP_ELOCKED(vp, "ffs_update");
106 if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
108 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
110 * The IN_SIZEMOD and IN_IBLKDATA flags indicate changes to the
111 * file size and block pointer fields in the inode. When these
112 * fields have been changed, the fsync() and fsyncdata() system
113 * calls must write the inode to ensure their semantics that the
114 * file is on stable store.
116 * The IN_SIZEMOD and IN_IBLKDATA flags cannot be cleared until
117 * a synchronous write of the inode is done. If they are cleared
118 * on an asynchronous write, then the inode may not yet have been
119 * written to the disk when an fsync() or fsyncdata() call is done.
120 * Absent these flags, these calls would not know that they needed
121 * to write the inode. Thus, these flags only can be cleared on
122 * synchronous writes of the inode. Since the inode will be locked
123 * for the duration of the I/O that writes it to disk, no fsync()
124 * or fsyncdata() will be able to run before the on-disk inode
128 ip->i_flag &= ~(IN_SIZEMOD | IN_IBLKDATA);
130 if (fs->fs_ronly && ITOUMP(ip)->um_fsckpid == 0)
133 * If we are updating a snapshot and another process is currently
134 * writing the buffer containing the inode for this snapshot then
135 * a deadlock can occur when it tries to check the snapshot to see
136 * if that block needs to be copied. Thus when updating a snapshot
137 * we check to see if the buffer is already locked, and if it is
138 * we drop the snapshot lock until the buffer has been written
139 * and is available to us. We have to grab a reference to the
140 * snapshot vnode to prevent it from being removed while we are
141 * waiting for the buffer.
145 flags = GB_LOCK_NOWAIT;
147 bn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
148 error = ffs_breadz(VFSTOUFS(vp->v_mount), ITODEVVP(ip), bn, bn,
149 (int) fs->fs_bsize, NULL, NULL, 0, NOCRED, flags, NULL, &bp);
153 KASSERT((IS_SNAPSHOT(ip)), ("EBUSY from non-snapshot"));
155 * Wait for our inode block to become available.
157 * Hold a reference to the vnode to protect against
158 * ffs_snapgone(). Since we hold a reference, it can only
159 * get reclaimed (VIRF_DOOMED flag) in a forcible downgrade
160 * or unmount. For an unmount, the entire filesystem will be
161 * gone, so we cannot attempt to touch anything associated
162 * with it while the vnode is unlocked; all we can do is
163 * pause briefly and try again. If when we relock the vnode
164 * we discover that it has been reclaimed, updating it is no
165 * longer necessary and we can just return an error.
170 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
172 if (VN_IS_DOOMED(vp))
176 if (DOINGSOFTDEP(vp))
177 softdep_update_inodeblock(ip, bp, waitfor);
178 else if (ip->i_effnlink != ip->i_nlink)
179 panic("ffs_update: bad link cnt");
181 *((struct ufs1_dinode *)bp->b_data +
182 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
184 * XXX: FIX? The entropy here is desirable,
185 * but the harvesting may be expensive
187 random_harvest_queue(&(ip->i_din1), sizeof(ip->i_din1), RANDOM_FS_ATIME);
189 ffs_update_dinode_ckhash(fs, ip->i_din2);
190 *((struct ufs2_dinode *)bp->b_data +
191 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
193 * XXX: FIX? The entropy here is desirable,
194 * but the harvesting may be expensive
196 random_harvest_queue(&(ip->i_din2), sizeof(ip->i_din2), RANDOM_FS_ATIME);
200 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
202 } else if (vm_page_count_severe() || buf_dirty_count_severe()) {
206 if (bp->b_bufsize == fs->fs_bsize)
207 bp->b_flags |= B_CLUSTEROK;
214 #define SINGLE 0 /* index of single indirect block */
215 #define DOUBLE 1 /* index of double indirect block */
216 #define TRIPLE 2 /* index of triple indirect block */
218 * Truncate the inode ip to at most length size, freeing the
222 ffs_truncate(vp, length, flags, cred)
229 ufs2_daddr_t bn, lbn, lastblock, lastiblock[UFS_NIADDR];
230 ufs2_daddr_t indir_lbn[UFS_NIADDR], oldblks[UFS_NDADDR + UFS_NIADDR];
231 ufs2_daddr_t newblks[UFS_NDADDR + UFS_NIADDR];
232 ufs2_daddr_t count, blocksreleased = 0, datablocks, blkno;
236 struct ufsmount *ump;
237 int softdeptrunc, journaltrunc;
238 int needextclean, extblocks;
239 int offset, size, level, nblocks;
240 int i, error, allerror, indiroff, waitforupdate;
245 ump = VFSTOUFS(vp->v_mount);
249 ASSERT_VOP_LOCKED(vp, "ffs_truncate");
253 if (length > fs->fs_maxfilesize)
256 error = getinoquota(ip);
261 * Historically clients did not have to specify which data
262 * they were truncating. So, if not specified, we assume
263 * traditional behavior, e.g., just the normal data.
265 if ((flags & (IO_EXT | IO_NORMAL)) == 0)
267 if (!DOINGSOFTDEP(vp) && !DOINGASYNC(vp))
269 waitforupdate = (flags & IO_SYNC) != 0 || !DOINGASYNC(vp);
271 * If we are truncating the extended-attributes, and cannot
272 * do it with soft updates, then do it slowly here. If we are
273 * truncating both the extended attributes and the file contents
274 * (e.g., the file is being unlinked), then pick it off with
275 * soft updates below.
280 journaltrunc = DOINGSUJ(vp);
281 journaltrunc = 0; /* XXX temp patch until bug found */
282 if (journaltrunc == 0 && DOINGSOFTDEP(vp) && length == 0)
283 softdeptrunc = !softdep_slowdown(vp);
285 datablocks = DIP(ip, i_blocks);
286 if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
287 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
288 datablocks -= extblocks;
290 if ((flags & IO_EXT) && extblocks > 0) {
292 panic("ffs_truncate: partial trunc of extdata");
293 if (softdeptrunc || journaltrunc) {
294 if ((flags & IO_NORMAL) == 0)
298 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
301 (void) chkdq(ip, -extblocks, NOCRED, FORCE);
303 vinvalbuf(vp, V_ALT, 0, 0);
305 OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
306 osize = ip->i_din2->di_extsize;
307 ip->i_din2->di_blocks -= extblocks;
308 ip->i_din2->di_extsize = 0;
309 for (i = 0; i < UFS_NXADDR; i++) {
310 oldblks[i] = ip->i_din2->di_extb[i];
311 ip->i_din2->di_extb[i] = 0;
313 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
314 if ((error = ffs_update(vp, waitforupdate)))
316 for (i = 0; i < UFS_NXADDR; i++) {
319 ffs_blkfree(ump, fs, ITODEVVP(ip), oldblks[i],
320 sblksize(fs, osize, i), ip->i_number,
321 vp->v_type, NULL, SINGLETON_KEY);
325 if ((flags & IO_NORMAL) == 0)
327 if (vp->v_type == VLNK &&
328 (ip->i_size < vp->v_mount->mnt_maxsymlinklen ||
332 panic("ffs_truncate: partial truncate of symlink");
334 bzero(SHORTLINK(ip), (u_int)ip->i_size);
336 DIP_SET(ip, i_size, 0);
337 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
340 return (ffs_update(vp, waitforupdate));
342 if (ip->i_size == length) {
343 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
346 return (ffs_update(vp, 0));
349 panic("ffs_truncate: read-only filesystem");
352 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
355 * Lengthen the size of the file. We must ensure that the
356 * last byte of the file is allocated. Since the smallest
357 * value of osize is 0, length will be at least 1.
359 if (osize < length) {
360 vnode_pager_setsize(vp, length);
362 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
364 vnode_pager_setsize(vp, osize);
368 DIP_SET(ip, i_size, length);
369 if (bp->b_bufsize == fs->fs_bsize)
370 bp->b_flags |= B_CLUSTEROK;
371 ffs_inode_bwrite(vp, bp, flags);
372 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
373 return (ffs_update(vp, waitforupdate));
376 * Lookup block number for a given offset. Zero length files
377 * have no blocks, so return a blkno of -1.
379 lbn = lblkno(fs, length - 1);
382 } else if (lbn < UFS_NDADDR) {
383 blkno = DIP(ip, i_db[lbn]);
385 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), fs->fs_bsize,
386 cred, BA_METAONLY, &bp);
389 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
391 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
393 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
395 * If the block number is non-zero, then the indirect block
396 * must have been previously allocated and need not be written.
397 * If the block number is zero, then we may have allocated
398 * the indirect block and hence need to write it out.
402 else if (flags & IO_SYNC)
408 * If the block number at the new end of the file is zero,
409 * then we must allocate it to ensure that the last block of
410 * the file is allocated. Soft updates does not handle this
411 * case, so here we have to clean up the soft updates data
412 * structures describing the allocation past the truncation
413 * point. Finding and deallocating those structures is a lot of
414 * work. Since partial truncation with a hole at the end occurs
415 * rarely, we solve the problem by syncing the file so that it
416 * will have no soft updates data structures left.
418 if (blkno == 0 && (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
420 if (blkno != 0 && DOINGSOFTDEP(vp)) {
421 if (softdeptrunc == 0 && journaltrunc == 0) {
423 * If soft updates cannot handle this truncation,
424 * clean up soft dependency data structures and
425 * fall through to the synchronous truncation.
427 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
430 flags = IO_NORMAL | (needextclean ? IO_EXT: 0);
432 softdep_journal_freeblocks(ip, cred, length,
435 softdep_setup_freeblocks(ip, length, flags);
436 ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
437 if (journaltrunc == 0) {
438 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
439 error = ffs_update(vp, 0);
445 * Shorten the size of the file. If the last block of the
446 * shortened file is unallocated, we must allocate it.
447 * Additionally, if the file is not being truncated to a
448 * block boundary, the contents of the partial block
449 * following the end of the file must be zero'ed in
450 * case it ever becomes accessible again because of
451 * subsequent file growth. Directories however are not
452 * zero'ed as they should grow back initialized to empty.
454 offset = blkoff(fs, length);
455 if (blkno != 0 && offset == 0) {
457 DIP_SET(ip, i_size, length);
458 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
460 lbn = lblkno(fs, length);
462 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
465 ffs_inode_bwrite(vp, bp, flags);
468 * When we are doing soft updates and the UFS_BALLOC
469 * above fills in a direct block hole with a full sized
470 * block that will be truncated down to a fragment below,
471 * we must flush out the block dependency with an FSYNC
472 * so that we do not get a soft updates inconsistency
473 * when we create the fragment below.
475 if (DOINGSOFTDEP(vp) && lbn < UFS_NDADDR &&
476 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
477 (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
480 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
484 DIP_SET(ip, i_size, length);
485 size = blksize(fs, ip, lbn);
486 if (vp->v_type != VDIR && offset != 0)
487 bzero((char *)bp->b_data + offset,
488 (u_int)(size - offset));
489 /* Kirk's code has reallocbuf(bp, size, 1) here */
491 if (bp->b_bufsize == fs->fs_bsize)
492 bp->b_flags |= B_CLUSTEROK;
493 ffs_inode_bwrite(vp, bp, flags);
494 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
497 * Calculate index into inode's block list of
498 * last direct and indirect blocks (if any)
499 * which we want to keep. Lastblock is -1 when
500 * the file is truncated to 0.
502 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
503 lastiblock[SINGLE] = lastblock - UFS_NDADDR;
504 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
505 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
506 nblocks = btodb(fs->fs_bsize);
508 * Update file and block pointers on disk before we start freeing
509 * blocks. If we crash before free'ing blocks below, the blocks
510 * will be returned to the free list. lastiblock values are also
511 * normalized to -1 for calls to ffs_indirtrunc below.
513 for (level = TRIPLE; level >= SINGLE; level--) {
514 oldblks[UFS_NDADDR + level] = DIP(ip, i_ib[level]);
515 if (lastiblock[level] < 0) {
516 DIP_SET(ip, i_ib[level], 0);
517 lastiblock[level] = -1;
520 for (i = 0; i < UFS_NDADDR; i++) {
521 oldblks[i] = DIP(ip, i_db[i]);
523 DIP_SET(ip, i_db[i], 0);
525 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
526 allerror = ffs_update(vp, waitforupdate);
529 * Having written the new inode to disk, save its new configuration
530 * and put back the old block pointers long enough to process them.
531 * Note that we save the new block configuration so we can check it
534 for (i = 0; i < UFS_NDADDR; i++) {
535 newblks[i] = DIP(ip, i_db[i]);
536 DIP_SET(ip, i_db[i], oldblks[i]);
538 for (i = 0; i < UFS_NIADDR; i++) {
539 newblks[UFS_NDADDR + i] = DIP(ip, i_ib[i]);
540 DIP_SET(ip, i_ib[i], oldblks[UFS_NDADDR + i]);
543 DIP_SET(ip, i_size, osize);
544 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
546 error = vtruncbuf(vp, length, fs->fs_bsize);
547 if (error && (allerror == 0))
551 * Indirect blocks first.
553 indir_lbn[SINGLE] = -UFS_NDADDR;
554 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
555 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
556 for (level = TRIPLE; level >= SINGLE; level--) {
557 bn = DIP(ip, i_ib[level]);
559 error = ffs_indirtrunc(ip, indir_lbn[level],
560 fsbtodb(fs, bn), lastiblock[level], level, &count);
563 blocksreleased += count;
564 if (lastiblock[level] < 0) {
565 DIP_SET(ip, i_ib[level], 0);
566 ffs_blkfree(ump, fs, ump->um_devvp, bn,
567 fs->fs_bsize, ip->i_number,
568 vp->v_type, NULL, SINGLETON_KEY);
569 blocksreleased += nblocks;
572 if (lastiblock[level] >= 0)
577 * All whole direct blocks or frags.
579 key = ffs_blkrelease_start(ump, ump->um_devvp, ip->i_number);
580 for (i = UFS_NDADDR - 1; i > lastblock; i--) {
583 bn = DIP(ip, i_db[i]);
586 DIP_SET(ip, i_db[i], 0);
587 bsize = blksize(fs, ip, i);
588 ffs_blkfree(ump, fs, ump->um_devvp, bn, bsize, ip->i_number,
589 vp->v_type, NULL, key);
590 blocksreleased += btodb(bsize);
592 ffs_blkrelease_finish(ump, key);
597 * Finally, look for a change in size of the
598 * last direct block; release any frags.
600 bn = DIP(ip, i_db[lastblock]);
602 long oldspace, newspace;
605 * Calculate amount of space we're giving
606 * back as old block size minus new block size.
608 oldspace = blksize(fs, ip, lastblock);
610 DIP_SET(ip, i_size, length);
611 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
612 newspace = blksize(fs, ip, lastblock);
614 panic("ffs_truncate: newspace");
615 if (oldspace - newspace > 0) {
617 * Block number of space to be free'd is
618 * the old block # plus the number of frags
619 * required for the storage we're keeping.
621 bn += numfrags(fs, newspace);
622 ffs_blkfree(ump, fs, ump->um_devvp, bn,
623 oldspace - newspace, ip->i_number, vp->v_type,
624 NULL, SINGLETON_KEY);
625 blocksreleased += btodb(oldspace - newspace);
630 for (level = SINGLE; level <= TRIPLE; level++)
631 if (newblks[UFS_NDADDR + level] != DIP(ip, i_ib[level]))
632 panic("ffs_truncate1: level %d newblks %jd != i_ib %jd",
633 level, (intmax_t)newblks[UFS_NDADDR + level],
634 (intmax_t)DIP(ip, i_ib[level]));
635 for (i = 0; i < UFS_NDADDR; i++)
636 if (newblks[i] != DIP(ip, i_db[i]))
637 panic("ffs_truncate2: blkno %d newblks %jd != i_db %jd",
638 i, (intmax_t)newblks[UFS_NDADDR + level],
639 (intmax_t)DIP(ip, i_ib[level]));
642 (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
643 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
644 panic("ffs_truncate3: vp = %p, buffers: dirty = %d, clean = %d",
645 vp, bo->bo_dirty.bv_cnt, bo->bo_clean.bv_cnt);
647 #endif /* INVARIANTS */
649 * Put back the real size.
652 DIP_SET(ip, i_size, length);
653 if (DIP(ip, i_blocks) >= blocksreleased)
654 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
656 DIP_SET(ip, i_blocks, 0);
657 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
659 (void) chkdq(ip, -blocksreleased, NOCRED, FORCE);
665 softdep_journal_freeblocks(ip, cred, length, IO_EXT);
667 softdep_setup_freeblocks(ip, length, IO_EXT);
668 return (ffs_update(vp, waitforupdate));
672 * Release blocks associated with the inode ip and stored in the indirect
673 * block bn. Blocks are free'd in LIFO order up to (but not including)
674 * lastbn. If level is greater than SINGLE, the block is an indirect block
675 * and recursive calls to indirtrunc must be used to cleanse other indirect
679 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
681 ufs2_daddr_t lbn, lastbn;
684 ufs2_daddr_t *countp;
688 struct ufsmount *ump;
692 int i, nblocks, error = 0, allerror = 0;
693 ufs2_daddr_t nb, nlbn, last;
694 ufs2_daddr_t blkcount, factor, blocksreleased = 0;
695 ufs1_daddr_t *bap1 = NULL;
696 ufs2_daddr_t *bap2 = NULL;
697 #define BAP(ip, i) (I_IS_UFS1(ip) ? bap1[i] : bap2[i])
703 * Calculate index in current block of last
704 * block to be kept. -1 indicates the entire
705 * block so we need not calculate the index.
707 factor = lbn_offset(fs, level);
711 nblocks = btodb(fs->fs_bsize);
713 * Get buffer of block pointers, zero those entries corresponding
714 * to blocks to be free'd, and update on disk copy first. Since
715 * double(triple) indirect before single(double) indirect, calls
716 * to VOP_BMAP() on these blocks will fail. However, we already
717 * have the on-disk address, so we just pass it to bread() instead
718 * of having bread() attempt to calculate it using VOP_BMAP().
721 error = ffs_breadz(ump, vp, lbn, dbn, (int)fs->fs_bsize, NULL, NULL, 0,
722 NOCRED, 0, NULL, &bp);
729 bap1 = (ufs1_daddr_t *)bp->b_data;
731 bap2 = (ufs2_daddr_t *)bp->b_data;
733 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
734 bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
735 for (i = last + 1; i < NINDIR(fs); i++)
740 if (DOINGASYNC(vp)) {
748 bap1 = (ufs1_daddr_t *)copy;
750 bap2 = (ufs2_daddr_t *)copy;
754 * Recursively free totally unused blocks.
756 key = ffs_blkrelease_start(ump, ITODEVVP(ip), ip->i_number);
757 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
758 i--, nlbn += factor) {
762 if (level > SINGLE) {
763 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
764 (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
766 blocksreleased += blkcount;
768 ffs_blkfree(ump, fs, ITODEVVP(ip), nb, fs->fs_bsize,
769 ip->i_number, vp->v_type, NULL, key);
770 blocksreleased += nblocks;
772 ffs_blkrelease_finish(ump, key);
775 * Recursively free last partial block.
777 if (level > SINGLE && lastbn >= 0) {
778 last = lastbn % factor;
781 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
782 last, level - 1, &blkcount);
785 blocksreleased += blkcount;
791 bp->b_flags |= B_INVAL | B_NOCACHE;
795 *countp = blocksreleased;
800 ffs_rdonly(struct inode *ip)
803 return (ITOFS(ip)->fs_ronly != 0);