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 * Update the access, modified, and inode change times as specified by the
72 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
73 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
74 * the timestamp update). The IN_LAZYMOD flag is set to force a write
75 * later if not now. The IN_LAZYACCESS is set instead of IN_MODIFIED if the fs
76 * is currently being suspended (or is suspended) and vnode has been accessed.
77 * If we write now, then clear IN_MODIFIED, IN_LAZYACCESS and IN_LAZYMOD to
78 * reflect the presumably successful write, and if waitfor is set, then wait
79 * for the write to complete.
82 ffs_update(vp, waitfor)
92 ASSERT_VOP_ELOCKED(vp, "ffs_update");
95 if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
97 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
99 * The IN_SIZEMOD and IN_IBLKDATA flags indicate changes to the
100 * file size and block pointer fields in the inode. When these
101 * fields have been changed, the fsync() and fsyncdata() system
102 * calls must write the inode to ensure their semantics that the
103 * file is on stable store.
105 * The IN_SIZEMOD and IN_IBLKDATA flags cannot be cleared until
106 * a synchronous write of the inode is done. If they are cleared
107 * on an asynchronous write, then the inode may not yet have been
108 * written to the disk when an fsync() or fsyncdata() call is done.
109 * Absent these flags, these calls would not know that they needed
110 * to write the inode. Thus, these flags only can be cleared on
111 * synchronous writes of the inode. Since the inode will be locked
112 * for the duration of the I/O that writes it to disk, no fsync()
113 * or fsyncdata() will be able to run before the on-disk inode
117 ip->i_flag &= ~(IN_SIZEMOD | IN_IBLKDATA);
119 if (fs->fs_ronly && ITOUMP(ip)->um_fsckpid == 0)
122 * If we are updating a snapshot and another process is currently
123 * writing the buffer containing the inode for this snapshot then
124 * a deadlock can occur when it tries to check the snapshot to see
125 * if that block needs to be copied. Thus when updating a snapshot
126 * we check to see if the buffer is already locked, and if it is
127 * we drop the snapshot lock until the buffer has been written
128 * and is available to us. We have to grab a reference to the
129 * snapshot vnode to prevent it from being removed while we are
130 * waiting for the buffer.
134 flags = GB_LOCK_NOWAIT;
136 bn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
137 error = ffs_breadz(VFSTOUFS(vp->v_mount), ITODEVVP(ip), bn, bn,
138 (int) fs->fs_bsize, NULL, NULL, 0, NOCRED, flags, NULL, &bp);
142 KASSERT((IS_SNAPSHOT(ip)), ("EBUSY from non-snapshot"));
144 * Wait for our inode block to become available.
146 * Hold a reference to the vnode to protect against
147 * ffs_snapgone(). Since we hold a reference, it can only
148 * get reclaimed (VIRF_DOOMED flag) in a forcible downgrade
149 * or unmount. For an unmount, the entire filesystem will be
150 * gone, so we cannot attempt to touch anything associated
151 * with it while the vnode is unlocked; all we can do is
152 * pause briefly and try again. If when we relock the vnode
153 * we discover that it has been reclaimed, updating it is no
154 * longer necessary and we can just return an error.
159 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
161 if (VN_IS_DOOMED(vp))
165 if (DOINGSOFTDEP(vp))
166 softdep_update_inodeblock(ip, bp, waitfor);
167 else if (ip->i_effnlink != ip->i_nlink)
168 panic("ffs_update: bad link cnt");
170 *((struct ufs1_dinode *)bp->b_data +
171 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
173 * XXX: FIX? The entropy here is desirable,
174 * but the harvesting may be expensive
176 random_harvest_queue(&(ip->i_din1), sizeof(ip->i_din1), RANDOM_FS_ATIME);
178 ffs_update_dinode_ckhash(fs, ip->i_din2);
179 *((struct ufs2_dinode *)bp->b_data +
180 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
182 * XXX: FIX? The entropy here is desirable,
183 * but the harvesting may be expensive
185 random_harvest_queue(&(ip->i_din2), sizeof(ip->i_din2), RANDOM_FS_ATIME);
189 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
191 } else if (vm_page_count_severe() || buf_dirty_count_severe()) {
195 if (bp->b_bufsize == fs->fs_bsize)
196 bp->b_flags |= B_CLUSTEROK;
203 #define SINGLE 0 /* index of single indirect block */
204 #define DOUBLE 1 /* index of double indirect block */
205 #define TRIPLE 2 /* index of triple indirect block */
207 * Truncate the inode ip to at most length size, freeing the
211 ffs_truncate(vp, length, flags, cred)
218 ufs2_daddr_t bn, lbn, lastblock, lastiblock[UFS_NIADDR];
219 ufs2_daddr_t indir_lbn[UFS_NIADDR], oldblks[UFS_NDADDR + UFS_NIADDR];
220 ufs2_daddr_t newblks[UFS_NDADDR + UFS_NIADDR];
221 ufs2_daddr_t count, blocksreleased = 0, datablocks, blkno;
225 struct ufsmount *ump;
226 int softdeptrunc, journaltrunc;
227 int needextclean, extblocks;
228 int offset, size, level, nblocks;
229 int i, error, allerror, indiroff, waitforupdate;
234 ump = VFSTOUFS(vp->v_mount);
238 ASSERT_VOP_LOCKED(vp, "ffs_truncate");
242 if (length > fs->fs_maxfilesize)
245 error = getinoquota(ip);
250 * Historically clients did not have to specify which data
251 * they were truncating. So, if not specified, we assume
252 * traditional behavior, e.g., just the normal data.
254 if ((flags & (IO_EXT | IO_NORMAL)) == 0)
256 if (!DOINGSOFTDEP(vp) && !DOINGASYNC(vp))
258 waitforupdate = (flags & IO_SYNC) != 0 || !DOINGASYNC(vp);
260 * If we are truncating the extended-attributes, and cannot
261 * do it with soft updates, then do it slowly here. If we are
262 * truncating both the extended attributes and the file contents
263 * (e.g., the file is being unlinked), then pick it off with
264 * soft updates below.
269 journaltrunc = DOINGSUJ(vp);
270 journaltrunc = 0; /* XXX temp patch until bug found */
271 if (journaltrunc == 0 && DOINGSOFTDEP(vp) && length == 0)
272 softdeptrunc = !softdep_slowdown(vp);
274 datablocks = DIP(ip, i_blocks);
275 if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
276 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
277 datablocks -= extblocks;
279 if ((flags & IO_EXT) && extblocks > 0) {
281 panic("ffs_truncate: partial trunc of extdata");
282 if (softdeptrunc || journaltrunc) {
283 if ((flags & IO_NORMAL) == 0)
287 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
290 (void) chkdq(ip, -extblocks, NOCRED, FORCE);
292 vinvalbuf(vp, V_ALT, 0, 0);
294 OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
295 osize = ip->i_din2->di_extsize;
296 ip->i_din2->di_blocks -= extblocks;
297 ip->i_din2->di_extsize = 0;
298 for (i = 0; i < UFS_NXADDR; i++) {
299 oldblks[i] = ip->i_din2->di_extb[i];
300 ip->i_din2->di_extb[i] = 0;
302 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
303 if ((error = ffs_update(vp, waitforupdate)))
305 for (i = 0; i < UFS_NXADDR; i++) {
308 ffs_blkfree(ump, fs, ITODEVVP(ip), oldblks[i],
309 sblksize(fs, osize, i), ip->i_number,
310 vp->v_type, NULL, SINGLETON_KEY);
314 if ((flags & IO_NORMAL) == 0)
316 if (vp->v_type == VLNK &&
317 (ip->i_size < vp->v_mount->mnt_maxsymlinklen ||
321 panic("ffs_truncate: partial truncate of symlink");
323 bzero(SHORTLINK(ip), (u_int)ip->i_size);
325 DIP_SET(ip, i_size, 0);
326 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
329 return (ffs_update(vp, waitforupdate));
331 if (ip->i_size == length) {
332 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
335 return (ffs_update(vp, 0));
338 panic("ffs_truncate: read-only filesystem");
341 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
344 * Lengthen the size of the file. We must ensure that the
345 * last byte of the file is allocated. Since the smallest
346 * value of osize is 0, length will be at least 1.
348 if (osize < length) {
349 vnode_pager_setsize(vp, length);
351 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
353 vnode_pager_setsize(vp, osize);
357 DIP_SET(ip, i_size, length);
358 if (bp->b_bufsize == fs->fs_bsize)
359 bp->b_flags |= B_CLUSTEROK;
362 else if (DOINGASYNC(vp))
366 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
367 return (ffs_update(vp, waitforupdate));
370 * Lookup block number for a given offset. Zero length files
371 * have no blocks, so return a blkno of -1.
373 lbn = lblkno(fs, length - 1);
376 } else if (lbn < UFS_NDADDR) {
377 blkno = DIP(ip, i_db[lbn]);
379 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), fs->fs_bsize,
380 cred, BA_METAONLY, &bp);
383 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
385 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
387 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
389 * If the block number is non-zero, then the indirect block
390 * must have been previously allocated and need not be written.
391 * If the block number is zero, then we may have allocated
392 * the indirect block and hence need to write it out.
396 else if (flags & IO_SYNC)
402 * If the block number at the new end of the file is zero,
403 * then we must allocate it to ensure that the last block of
404 * the file is allocated. Soft updates does not handle this
405 * case, so here we have to clean up the soft updates data
406 * structures describing the allocation past the truncation
407 * point. Finding and deallocating those structures is a lot of
408 * work. Since partial truncation with a hole at the end occurs
409 * rarely, we solve the problem by syncing the file so that it
410 * will have no soft updates data structures left.
412 if (blkno == 0 && (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
414 if (blkno != 0 && DOINGSOFTDEP(vp)) {
415 if (softdeptrunc == 0 && journaltrunc == 0) {
417 * If soft updates cannot handle this truncation,
418 * clean up soft dependency data structures and
419 * fall through to the synchronous truncation.
421 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
424 flags = IO_NORMAL | (needextclean ? IO_EXT: 0);
426 softdep_journal_freeblocks(ip, cred, length,
429 softdep_setup_freeblocks(ip, length, flags);
430 ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
431 if (journaltrunc == 0) {
432 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
433 error = ffs_update(vp, 0);
439 * Shorten the size of the file. If the last block of the
440 * shortened file is unallocated, we must allocate it.
441 * Additionally, if the file is not being truncated to a
442 * block boundary, the contents of the partial block
443 * following the end of the file must be zero'ed in
444 * case it ever becomes accessible again because of
445 * subsequent file growth. Directories however are not
446 * zero'ed as they should grow back initialized to empty.
448 offset = blkoff(fs, length);
449 if (blkno != 0 && offset == 0) {
451 DIP_SET(ip, i_size, length);
452 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
454 lbn = lblkno(fs, length);
456 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
460 * When we are doing soft updates and the UFS_BALLOC
461 * above fills in a direct block hole with a full sized
462 * block that will be truncated down to a fragment below,
463 * we must flush out the block dependency with an FSYNC
464 * so that we do not get a soft updates inconsistency
465 * when we create the fragment below.
467 if (DOINGSOFTDEP(vp) && lbn < UFS_NDADDR &&
468 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
469 (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
472 DIP_SET(ip, i_size, length);
473 size = blksize(fs, ip, lbn);
474 if (vp->v_type != VDIR && offset != 0)
475 bzero((char *)bp->b_data + offset,
476 (u_int)(size - offset));
477 /* Kirk's code has reallocbuf(bp, size, 1) here */
479 if (bp->b_bufsize == fs->fs_bsize)
480 bp->b_flags |= B_CLUSTEROK;
483 else if (DOINGASYNC(vp))
487 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
490 * Calculate index into inode's block list of
491 * last direct and indirect blocks (if any)
492 * which we want to keep. Lastblock is -1 when
493 * the file is truncated to 0.
495 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
496 lastiblock[SINGLE] = lastblock - UFS_NDADDR;
497 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
498 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
499 nblocks = btodb(fs->fs_bsize);
501 * Update file and block pointers on disk before we start freeing
502 * blocks. If we crash before free'ing blocks below, the blocks
503 * will be returned to the free list. lastiblock values are also
504 * normalized to -1 for calls to ffs_indirtrunc below.
506 for (level = TRIPLE; level >= SINGLE; level--) {
507 oldblks[UFS_NDADDR + level] = DIP(ip, i_ib[level]);
508 if (lastiblock[level] < 0) {
509 DIP_SET(ip, i_ib[level], 0);
510 lastiblock[level] = -1;
513 for (i = 0; i < UFS_NDADDR; i++) {
514 oldblks[i] = DIP(ip, i_db[i]);
516 DIP_SET(ip, i_db[i], 0);
518 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
519 allerror = ffs_update(vp, waitforupdate);
522 * Having written the new inode to disk, save its new configuration
523 * and put back the old block pointers long enough to process them.
524 * Note that we save the new block configuration so we can check it
527 for (i = 0; i < UFS_NDADDR; i++) {
528 newblks[i] = DIP(ip, i_db[i]);
529 DIP_SET(ip, i_db[i], oldblks[i]);
531 for (i = 0; i < UFS_NIADDR; i++) {
532 newblks[UFS_NDADDR + i] = DIP(ip, i_ib[i]);
533 DIP_SET(ip, i_ib[i], oldblks[UFS_NDADDR + i]);
536 DIP_SET(ip, i_size, osize);
537 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
539 error = vtruncbuf(vp, length, fs->fs_bsize);
540 if (error && (allerror == 0))
544 * Indirect blocks first.
546 indir_lbn[SINGLE] = -UFS_NDADDR;
547 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
548 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
549 for (level = TRIPLE; level >= SINGLE; level--) {
550 bn = DIP(ip, i_ib[level]);
552 error = ffs_indirtrunc(ip, indir_lbn[level],
553 fsbtodb(fs, bn), lastiblock[level], level, &count);
556 blocksreleased += count;
557 if (lastiblock[level] < 0) {
558 DIP_SET(ip, i_ib[level], 0);
559 ffs_blkfree(ump, fs, ump->um_devvp, bn,
560 fs->fs_bsize, ip->i_number,
561 vp->v_type, NULL, SINGLETON_KEY);
562 blocksreleased += nblocks;
565 if (lastiblock[level] >= 0)
570 * All whole direct blocks or frags.
572 key = ffs_blkrelease_start(ump, ump->um_devvp, ip->i_number);
573 for (i = UFS_NDADDR - 1; i > lastblock; i--) {
576 bn = DIP(ip, i_db[i]);
579 DIP_SET(ip, i_db[i], 0);
580 bsize = blksize(fs, ip, i);
581 ffs_blkfree(ump, fs, ump->um_devvp, bn, bsize, ip->i_number,
582 vp->v_type, NULL, key);
583 blocksreleased += btodb(bsize);
585 ffs_blkrelease_finish(ump, key);
590 * Finally, look for a change in size of the
591 * last direct block; release any frags.
593 bn = DIP(ip, i_db[lastblock]);
595 long oldspace, newspace;
598 * Calculate amount of space we're giving
599 * back as old block size minus new block size.
601 oldspace = blksize(fs, ip, lastblock);
603 DIP_SET(ip, i_size, length);
604 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
605 newspace = blksize(fs, ip, lastblock);
607 panic("ffs_truncate: newspace");
608 if (oldspace - newspace > 0) {
610 * Block number of space to be free'd is
611 * the old block # plus the number of frags
612 * required for the storage we're keeping.
614 bn += numfrags(fs, newspace);
615 ffs_blkfree(ump, fs, ump->um_devvp, bn,
616 oldspace - newspace, ip->i_number, vp->v_type,
617 NULL, SINGLETON_KEY);
618 blocksreleased += btodb(oldspace - newspace);
623 for (level = SINGLE; level <= TRIPLE; level++)
624 if (newblks[UFS_NDADDR + level] != DIP(ip, i_ib[level]))
625 panic("ffs_truncate1: level %d newblks %jd != i_ib %jd",
626 level, (intmax_t)newblks[UFS_NDADDR + level],
627 (intmax_t)DIP(ip, i_ib[level]));
628 for (i = 0; i < UFS_NDADDR; i++)
629 if (newblks[i] != DIP(ip, i_db[i]))
630 panic("ffs_truncate2: blkno %d newblks %jd != i_db %jd",
631 i, (intmax_t)newblks[UFS_NDADDR + level],
632 (intmax_t)DIP(ip, i_ib[level]));
635 (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
636 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
637 panic("ffs_truncate3: vp = %p, buffers: dirty = %d, clean = %d",
638 vp, bo->bo_dirty.bv_cnt, bo->bo_clean.bv_cnt);
640 #endif /* INVARIANTS */
642 * Put back the real size.
645 DIP_SET(ip, i_size, length);
646 if (DIP(ip, i_blocks) >= blocksreleased)
647 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
649 DIP_SET(ip, i_blocks, 0);
650 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
652 (void) chkdq(ip, -blocksreleased, NOCRED, FORCE);
658 softdep_journal_freeblocks(ip, cred, length, IO_EXT);
660 softdep_setup_freeblocks(ip, length, IO_EXT);
661 return (ffs_update(vp, waitforupdate));
665 * Release blocks associated with the inode ip and stored in the indirect
666 * block bn. Blocks are free'd in LIFO order up to (but not including)
667 * lastbn. If level is greater than SINGLE, the block is an indirect block
668 * and recursive calls to indirtrunc must be used to cleanse other indirect
672 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
674 ufs2_daddr_t lbn, lastbn;
677 ufs2_daddr_t *countp;
681 struct ufsmount *ump;
685 int i, nblocks, error = 0, allerror = 0;
686 ufs2_daddr_t nb, nlbn, last;
687 ufs2_daddr_t blkcount, factor, blocksreleased = 0;
688 ufs1_daddr_t *bap1 = NULL;
689 ufs2_daddr_t *bap2 = NULL;
690 #define BAP(ip, i) (I_IS_UFS1(ip) ? bap1[i] : bap2[i])
696 * Calculate index in current block of last
697 * block to be kept. -1 indicates the entire
698 * block so we need not calculate the index.
700 factor = lbn_offset(fs, level);
704 nblocks = btodb(fs->fs_bsize);
706 * Get buffer of block pointers, zero those entries corresponding
707 * to blocks to be free'd, and update on disk copy first. Since
708 * double(triple) indirect before single(double) indirect, calls
709 * to VOP_BMAP() on these blocks will fail. However, we already
710 * have the on-disk address, so we just pass it to bread() instead
711 * of having bread() attempt to calculate it using VOP_BMAP().
714 error = ffs_breadz(ump, vp, lbn, dbn, (int)fs->fs_bsize, NULL, NULL, 0,
715 NOCRED, 0, NULL, &bp);
722 bap1 = (ufs1_daddr_t *)bp->b_data;
724 bap2 = (ufs2_daddr_t *)bp->b_data;
726 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
727 bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
728 for (i = last + 1; i < NINDIR(fs); i++)
733 if (DOINGASYNC(vp)) {
741 bap1 = (ufs1_daddr_t *)copy;
743 bap2 = (ufs2_daddr_t *)copy;
747 * Recursively free totally unused blocks.
749 key = ffs_blkrelease_start(ump, ITODEVVP(ip), ip->i_number);
750 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
751 i--, nlbn += factor) {
755 if (level > SINGLE) {
756 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
757 (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
759 blocksreleased += blkcount;
761 ffs_blkfree(ump, fs, ITODEVVP(ip), nb, fs->fs_bsize,
762 ip->i_number, vp->v_type, NULL, key);
763 blocksreleased += nblocks;
765 ffs_blkrelease_finish(ump, key);
768 * Recursively free last partial block.
770 if (level > SINGLE && lastbn >= 0) {
771 last = lastbn % factor;
774 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
775 last, level - 1, &blkcount);
778 blocksreleased += blkcount;
784 bp->b_flags |= B_INVAL | B_NOCACHE;
788 *countp = blocksreleased;
793 ffs_rdonly(struct inode *ip)
796 return (ITOFS(ip)->fs_ronly != 0);