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