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