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