| 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 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | * |
| 33 | * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95 |
| 34 | * $FreeBSD: src/sys/ufs/ffs/ffs_inode.c,v 1.56.2.5 2002/02/05 18:35:03 dillon Exp $ |
| 35 | * $DragonFly: src/sys/vfs/ufs/ffs_inode.c,v 1.10 2004/05/18 00:16:46 cpressey Exp $ |
| 36 | */ |
| 37 | |
| 38 | #include "opt_quota.h" |
| 39 | |
| 40 | #include <sys/param.h> |
| 41 | #include <sys/systm.h> |
| 42 | #include <sys/mount.h> |
| 43 | #include <sys/proc.h> |
| 44 | #include <sys/buf.h> |
| 45 | #include <sys/vnode.h> |
| 46 | #include <sys/kernel.h> |
| 47 | #include <sys/malloc.h> |
| 48 | #include <sys/resourcevar.h> |
| 49 | #include <sys/vmmeter.h> |
| 50 | |
| 51 | #include <vm/vm.h> |
| 52 | #include <vm/vm_extern.h> |
| 53 | |
| 54 | #include "quota.h" |
| 55 | #include "ufsmount.h" |
| 56 | #include "inode.h" |
| 57 | #include "ufs_extern.h" |
| 58 | |
| 59 | #include "fs.h" |
| 60 | #include "ffs_extern.h" |
| 61 | |
| 62 | #include <vm/vm_page2.h> |
| 63 | |
| 64 | static int ffs_indirtrunc (struct inode *, ufs_daddr_t, ufs_daddr_t, |
| 65 | ufs_daddr_t, int, long *); |
| 66 | |
| 67 | /* |
| 68 | * Update the access, modified, and inode change times as specified by the |
| 69 | * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode |
| 70 | * to disk if the IN_MODIFIED flag is set (it may be set initially, or by |
| 71 | * the timestamp update). The IN_LAZYMOD flag is set to force a write |
| 72 | * later if not now. If we write now, then clear both IN_MODIFIED and |
| 73 | * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is |
| 74 | * set, then wait for the write to complete. |
| 75 | */ |
| 76 | int |
| 77 | ffs_update(struct vnode *vp, int waitfor) |
| 78 | { |
| 79 | struct fs *fs; |
| 80 | struct buf *bp; |
| 81 | struct inode *ip; |
| 82 | int error; |
| 83 | |
| 84 | ufs_itimes(vp); |
| 85 | ip = VTOI(vp); |
| 86 | if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0) |
| 87 | return (0); |
| 88 | ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED); |
| 89 | fs = ip->i_fs; |
| 90 | if (fs->fs_ronly) |
| 91 | return (0); |
| 92 | /* |
| 93 | * Ensure that uid and gid are correct. This is a temporary |
| 94 | * fix until fsck has been changed to do the update. |
| 95 | */ |
| 96 | if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ |
| 97 | ip->i_din.di_ouid = ip->i_uid; /* XXX */ |
| 98 | ip->i_din.di_ogid = ip->i_gid; /* XXX */ |
| 99 | } /* XXX */ |
| 100 | error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), |
| 101 | (int)fs->fs_bsize, &bp); |
| 102 | if (error) { |
| 103 | brelse(bp); |
| 104 | return (error); |
| 105 | } |
| 106 | if (DOINGSOFTDEP(vp)) |
| 107 | softdep_update_inodeblock(ip, bp, waitfor); |
| 108 | else if (ip->i_effnlink != ip->i_nlink) |
| 109 | panic("ffs_update: bad link cnt"); |
| 110 | *((struct dinode *)bp->b_data + |
| 111 | ino_to_fsbo(fs, ip->i_number)) = ip->i_din; |
| 112 | if (waitfor && !DOINGASYNC(vp)) { |
| 113 | return (bwrite(bp)); |
| 114 | } else if (vm_page_count_severe() || buf_dirty_count_severe()) { |
| 115 | return (bwrite(bp)); |
| 116 | } else { |
| 117 | if (bp->b_bufsize == fs->fs_bsize) |
| 118 | bp->b_flags |= B_CLUSTEROK; |
| 119 | bdwrite(bp); |
| 120 | return (0); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #define SINGLE 0 /* index of single indirect block */ |
| 125 | #define DOUBLE 1 /* index of double indirect block */ |
| 126 | #define TRIPLE 2 /* index of triple indirect block */ |
| 127 | /* |
| 128 | * Truncate the inode oip to at most length size, freeing the |
| 129 | * disk blocks. |
| 130 | */ |
| 131 | int |
| 132 | ffs_truncate(struct vnode *vp, off_t length, int flags, struct ucred *cred, |
| 133 | struct thread *td) |
| 134 | { |
| 135 | struct vnode *ovp = vp; |
| 136 | ufs_daddr_t lastblock; |
| 137 | struct inode *oip; |
| 138 | ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; |
| 139 | ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; |
| 140 | struct fs *fs; |
| 141 | struct buf *bp; |
| 142 | int offset, size, level; |
| 143 | long count, nblocks, blocksreleased = 0; |
| 144 | int i; |
| 145 | int aflags, error, allerror; |
| 146 | off_t osize; |
| 147 | |
| 148 | oip = VTOI(ovp); |
| 149 | fs = oip->i_fs; |
| 150 | if (length < 0) |
| 151 | return (EINVAL); |
| 152 | if (length > fs->fs_maxfilesize) |
| 153 | return (EFBIG); |
| 154 | if (ovp->v_type == VLNK && |
| 155 | (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) { |
| 156 | #ifdef DIAGNOSTIC |
| 157 | if (length != 0) |
| 158 | panic("ffs_truncate: partial truncate of symlink"); |
| 159 | #endif |
| 160 | bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); |
| 161 | oip->i_size = 0; |
| 162 | oip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 163 | return (UFS_UPDATE(ovp, 1)); |
| 164 | } |
| 165 | if (oip->i_size == length) { |
| 166 | oip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 167 | return (UFS_UPDATE(ovp, 0)); |
| 168 | } |
| 169 | if (fs->fs_ronly) |
| 170 | panic("ffs_truncate: read-only filesystem"); |
| 171 | #ifdef QUOTA |
| 172 | error = getinoquota(oip); |
| 173 | if (error) |
| 174 | return (error); |
| 175 | #endif |
| 176 | ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0; |
| 177 | if (DOINGSOFTDEP(ovp)) { |
| 178 | if (length > 0 || softdep_slowdown(ovp)) { |
| 179 | /* |
| 180 | * If a file is only partially truncated, then |
| 181 | * we have to clean up the data structures |
| 182 | * describing the allocation past the truncation |
| 183 | * point. Finding and deallocating those structures |
| 184 | * is a lot of work. Since partial truncation occurs |
| 185 | * rarely, we solve the problem by syncing the file |
| 186 | * so that it will have no data structures left. |
| 187 | */ |
| 188 | if ((error = VOP_FSYNC(ovp, MNT_WAIT, td)) != 0) |
| 189 | return (error); |
| 190 | } else { |
| 191 | #ifdef QUOTA |
| 192 | (void) chkdq(oip, -oip->i_blocks, NOCRED, 0); |
| 193 | #endif |
| 194 | softdep_setup_freeblocks(oip, length); |
| 195 | vinvalbuf(ovp, 0, td, 0, 0); |
| 196 | oip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 197 | return (ffs_update(ovp, 0)); |
| 198 | } |
| 199 | } |
| 200 | osize = oip->i_size; |
| 201 | /* |
| 202 | * Lengthen the size of the file. We must ensure that the |
| 203 | * last byte of the file is allocated. Since the smallest |
| 204 | * value of osize is 0, length will be at least 1. |
| 205 | */ |
| 206 | if (osize < length) { |
| 207 | vnode_pager_setsize(ovp, length); |
| 208 | aflags = B_CLRBUF; |
| 209 | if (flags & IO_SYNC) |
| 210 | aflags |= B_SYNC; |
| 211 | error = VOP_BALLOC(ovp, length - 1, 1, |
| 212 | cred, aflags, &bp); |
| 213 | if (error) |
| 214 | return (error); |
| 215 | oip->i_size = length; |
| 216 | if (bp->b_bufsize == fs->fs_bsize) |
| 217 | bp->b_flags |= B_CLUSTEROK; |
| 218 | if (aflags & B_SYNC) |
| 219 | bwrite(bp); |
| 220 | else |
| 221 | bawrite(bp); |
| 222 | oip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 223 | return (UFS_UPDATE(ovp, 1)); |
| 224 | } |
| 225 | /* |
| 226 | * Shorten the size of the file. If the file is not being |
| 227 | * truncated to a block boundary, the contents of the |
| 228 | * partial block following the end of the file must be |
| 229 | * zero'ed in case it ever becomes accessible again because |
| 230 | * of subsequent file growth. Directories however are not |
| 231 | * zero'ed as they should grow back initialized to empty. |
| 232 | */ |
| 233 | offset = blkoff(fs, length); |
| 234 | if (offset == 0) { |
| 235 | oip->i_size = length; |
| 236 | } else { |
| 237 | lbn = lblkno(fs, length); |
| 238 | aflags = B_CLRBUF; |
| 239 | if (flags & IO_SYNC) |
| 240 | aflags |= B_SYNC; |
| 241 | error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp); |
| 242 | if (error) { |
| 243 | return (error); |
| 244 | } |
| 245 | /* |
| 246 | * When we are doing soft updates and the UFS_BALLOC |
| 247 | * above fills in a direct block hole with a full sized |
| 248 | * block that will be truncated down to a fragment below, |
| 249 | * we must flush out the block dependency with an FSYNC |
| 250 | * so that we do not get a soft updates inconsistency |
| 251 | * when we create the fragment below. |
| 252 | */ |
| 253 | if (DOINGSOFTDEP(ovp) && lbn < NDADDR && |
| 254 | fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize && |
| 255 | (error = VOP_FSYNC(ovp, MNT_WAIT, td)) != 0) { |
| 256 | return (error); |
| 257 | } |
| 258 | oip->i_size = length; |
| 259 | size = blksize(fs, oip, lbn); |
| 260 | if (ovp->v_type != VDIR) |
| 261 | bzero((char *)bp->b_data + offset, |
| 262 | (u_int)(size - offset)); |
| 263 | /* Kirk's code has reallocbuf(bp, size, 1) here */ |
| 264 | allocbuf(bp, size); |
| 265 | if (bp->b_bufsize == fs->fs_bsize) |
| 266 | bp->b_flags |= B_CLUSTEROK; |
| 267 | if (aflags & B_SYNC) |
| 268 | bwrite(bp); |
| 269 | else |
| 270 | bawrite(bp); |
| 271 | } |
| 272 | /* |
| 273 | * Calculate index into inode's block list of |
| 274 | * last direct and indirect blocks (if any) |
| 275 | * which we want to keep. Lastblock is -1 when |
| 276 | * the file is truncated to 0. |
| 277 | */ |
| 278 | lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; |
| 279 | lastiblock[SINGLE] = lastblock - NDADDR; |
| 280 | lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); |
| 281 | lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); |
| 282 | nblocks = btodb(fs->fs_bsize); |
| 283 | /* |
| 284 | * Update file and block pointers on disk before we start freeing |
| 285 | * blocks. If we crash before free'ing blocks below, the blocks |
| 286 | * will be returned to the free list. lastiblock values are also |
| 287 | * normalized to -1 for calls to ffs_indirtrunc below. |
| 288 | */ |
| 289 | bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); |
| 290 | for (level = TRIPLE; level >= SINGLE; level--) |
| 291 | if (lastiblock[level] < 0) { |
| 292 | oip->i_ib[level] = 0; |
| 293 | lastiblock[level] = -1; |
| 294 | } |
| 295 | for (i = NDADDR - 1; i > lastblock; i--) |
| 296 | oip->i_db[i] = 0; |
| 297 | oip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 298 | allerror = UFS_UPDATE(ovp, 1); |
| 299 | |
| 300 | /* |
| 301 | * Having written the new inode to disk, save its new configuration |
| 302 | * and put back the old block pointers long enough to process them. |
| 303 | * Note that we save the new block configuration so we can check it |
| 304 | * when we are done. |
| 305 | */ |
| 306 | bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); |
| 307 | bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); |
| 308 | oip->i_size = osize; |
| 309 | |
| 310 | error = vtruncbuf(ovp, td, length, fs->fs_bsize); |
| 311 | if (error && (allerror == 0)) |
| 312 | allerror = error; |
| 313 | |
| 314 | /* |
| 315 | * Indirect blocks first. |
| 316 | */ |
| 317 | indir_lbn[SINGLE] = -NDADDR; |
| 318 | indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; |
| 319 | indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; |
| 320 | for (level = TRIPLE; level >= SINGLE; level--) { |
| 321 | bn = oip->i_ib[level]; |
| 322 | if (bn != 0) { |
| 323 | error = ffs_indirtrunc(oip, indir_lbn[level], |
| 324 | fsbtodb(fs, bn), lastiblock[level], level, &count); |
| 325 | if (error) |
| 326 | allerror = error; |
| 327 | blocksreleased += count; |
| 328 | if (lastiblock[level] < 0) { |
| 329 | oip->i_ib[level] = 0; |
| 330 | ffs_blkfree(oip, bn, fs->fs_bsize); |
| 331 | blocksreleased += nblocks; |
| 332 | } |
| 333 | } |
| 334 | if (lastiblock[level] >= 0) |
| 335 | goto done; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * All whole direct blocks or frags. |
| 340 | */ |
| 341 | for (i = NDADDR - 1; i > lastblock; i--) { |
| 342 | long bsize; |
| 343 | |
| 344 | bn = oip->i_db[i]; |
| 345 | if (bn == 0) |
| 346 | continue; |
| 347 | oip->i_db[i] = 0; |
| 348 | bsize = blksize(fs, oip, i); |
| 349 | ffs_blkfree(oip, bn, bsize); |
| 350 | blocksreleased += btodb(bsize); |
| 351 | } |
| 352 | if (lastblock < 0) |
| 353 | goto done; |
| 354 | |
| 355 | /* |
| 356 | * Finally, look for a change in size of the |
| 357 | * last direct block; release any frags. |
| 358 | */ |
| 359 | bn = oip->i_db[lastblock]; |
| 360 | if (bn != 0) { |
| 361 | long oldspace, newspace; |
| 362 | |
| 363 | /* |
| 364 | * Calculate amount of space we're giving |
| 365 | * back as old block size minus new block size. |
| 366 | */ |
| 367 | oldspace = blksize(fs, oip, lastblock); |
| 368 | oip->i_size = length; |
| 369 | newspace = blksize(fs, oip, lastblock); |
| 370 | if (newspace == 0) |
| 371 | panic("ffs_truncate: newspace"); |
| 372 | if (oldspace - newspace > 0) { |
| 373 | /* |
| 374 | * Block number of space to be free'd is |
| 375 | * the old block # plus the number of frags |
| 376 | * required for the storage we're keeping. |
| 377 | */ |
| 378 | bn += numfrags(fs, newspace); |
| 379 | ffs_blkfree(oip, bn, oldspace - newspace); |
| 380 | blocksreleased += btodb(oldspace - newspace); |
| 381 | } |
| 382 | } |
| 383 | done: |
| 384 | #ifdef DIAGNOSTIC |
| 385 | for (level = SINGLE; level <= TRIPLE; level++) |
| 386 | if (newblks[NDADDR + level] != oip->i_ib[level]) |
| 387 | panic("ffs_truncate1"); |
| 388 | for (i = 0; i < NDADDR; i++) |
| 389 | if (newblks[i] != oip->i_db[i]) |
| 390 | panic("ffs_truncate2"); |
| 391 | if (length == 0 && |
| 392 | (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) || |
| 393 | !TAILQ_EMPTY(&ovp->v_cleanblkhd))) |
| 394 | panic("ffs_truncate3"); |
| 395 | #endif /* DIAGNOSTIC */ |
| 396 | /* |
| 397 | * Put back the real size. |
| 398 | */ |
| 399 | oip->i_size = length; |
| 400 | oip->i_blocks -= blocksreleased; |
| 401 | |
| 402 | if (oip->i_blocks < 0) /* sanity */ |
| 403 | oip->i_blocks = 0; |
| 404 | oip->i_flag |= IN_CHANGE; |
| 405 | #ifdef QUOTA |
| 406 | (void) chkdq(oip, -blocksreleased, NOCRED, 0); |
| 407 | #endif |
| 408 | return (allerror); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Release blocks associated with the inode ip and stored in the indirect |
| 413 | * block bn. Blocks are free'd in LIFO order up to (but not including) |
| 414 | * lastbn. If level is greater than SINGLE, the block is an indirect block |
| 415 | * and recursive calls to indirtrunc must be used to cleanse other indirect |
| 416 | * blocks. |
| 417 | * |
| 418 | * NB: triple indirect blocks are untested. |
| 419 | */ |
| 420 | static int |
| 421 | ffs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, ufs_daddr_t dbn, |
| 422 | ufs_daddr_t lastbn, int level, long *countp) |
| 423 | { |
| 424 | int i; |
| 425 | struct buf *bp; |
| 426 | struct fs *fs = ip->i_fs; |
| 427 | ufs_daddr_t *bap; |
| 428 | struct vnode *vp; |
| 429 | ufs_daddr_t *copy = NULL, nb, nlbn, last; |
| 430 | long blkcount, factor; |
| 431 | int nblocks, blocksreleased = 0; |
| 432 | int error = 0, allerror = 0; |
| 433 | |
| 434 | /* |
| 435 | * Calculate index in current block of last |
| 436 | * block to be kept. -1 indicates the entire |
| 437 | * block so we need not calculate the index. |
| 438 | */ |
| 439 | factor = 1; |
| 440 | for (i = SINGLE; i < level; i++) |
| 441 | factor *= NINDIR(fs); |
| 442 | last = lastbn; |
| 443 | if (lastbn > 0) |
| 444 | last /= factor; |
| 445 | nblocks = btodb(fs->fs_bsize); |
| 446 | /* |
| 447 | * Get buffer of block pointers, zero those entries corresponding |
| 448 | * to blocks to be free'd, and update on disk copy first. Since |
| 449 | * double(triple) indirect before single(double) indirect, calls |
| 450 | * to bmap on these blocks will fail. However, we already have |
| 451 | * the on disk address, so we have to set the b_blkno field |
| 452 | * explicitly instead of letting bread do everything for us. |
| 453 | */ |
| 454 | vp = ITOV(ip); |
| 455 | bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0); |
| 456 | if ((bp->b_flags & B_CACHE) == 0) { |
| 457 | bp->b_flags |= B_READ; |
| 458 | bp->b_flags &= ~(B_ERROR|B_INVAL); |
| 459 | if (bp->b_bcount > bp->b_bufsize) |
| 460 | panic("ffs_indirtrunc: bad buffer size"); |
| 461 | bp->b_blkno = dbn; |
| 462 | vfs_busy_pages(bp, 0); |
| 463 | VOP_STRATEGY(bp->b_vp, bp); |
| 464 | error = biowait(bp); |
| 465 | } |
| 466 | if (error) { |
| 467 | brelse(bp); |
| 468 | *countp = 0; |
| 469 | return (error); |
| 470 | } |
| 471 | |
| 472 | bap = (ufs_daddr_t *)bp->b_data; |
| 473 | if (lastbn != -1) { |
| 474 | MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); |
| 475 | bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); |
| 476 | bzero((caddr_t)&bap[last + 1], |
| 477 | (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t)); |
| 478 | if (DOINGASYNC(vp)) { |
| 479 | bawrite(bp); |
| 480 | } else { |
| 481 | error = bwrite(bp); |
| 482 | if (error) |
| 483 | allerror = error; |
| 484 | } |
| 485 | bap = copy; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Recursively free totally unused blocks. |
| 490 | */ |
| 491 | for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; |
| 492 | i--, nlbn += factor) { |
| 493 | nb = bap[i]; |
| 494 | if (nb == 0) |
| 495 | continue; |
| 496 | if (level > SINGLE) { |
| 497 | if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), |
| 498 | (ufs_daddr_t)-1, level - 1, &blkcount)) != 0) |
| 499 | allerror = error; |
| 500 | blocksreleased += blkcount; |
| 501 | } |
| 502 | ffs_blkfree(ip, nb, fs->fs_bsize); |
| 503 | blocksreleased += nblocks; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Recursively free last partial block. |
| 508 | */ |
| 509 | if (level > SINGLE && lastbn >= 0) { |
| 510 | last = lastbn % factor; |
| 511 | nb = bap[i]; |
| 512 | if (nb != 0) { |
| 513 | error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), |
| 514 | last, level - 1, &blkcount); |
| 515 | if (error) |
| 516 | allerror = error; |
| 517 | blocksreleased += blkcount; |
| 518 | } |
| 519 | } |
| 520 | if (copy != NULL) { |
| 521 | FREE(copy, M_TEMP); |
| 522 | } else { |
| 523 | bp->b_flags |= B_INVAL | B_NOCACHE; |
| 524 | brelse(bp); |
| 525 | } |
| 526 | |
| 527 | *countp = blocksreleased; |
| 528 | return (allerror); |
| 529 | } |