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