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