41 */ 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/mount.h> 46#include <sys/bio.h> 47#include <sys/buf.h> 48#include <sys/vnode.h> 49#include <sys/malloc.h> 50 51#include <vm/vm.h> 52#include <vm/vm_extern.h> 53 54#include <gnu/ext2fs/inode.h> 55#include <gnu/ext2fs/ext2_mount.h> 56#include <gnu/ext2fs/ext2_fs.h> 57#include <gnu/ext2fs/ext2_fs_sb.h> 58#include <gnu/ext2fs/fs.h> 59#include <gnu/ext2fs/ext2_extern.h> 60 61static int ext2_indirtrunc(struct inode *, int32_t, int32_t, int32_t, int, 62 long *); 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. If we write now, then clear both IN_MODIFIED and 70 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is 71 * set, then wait for the write to complete. 72 */ 73int 74ext2_update(vp, waitfor) 75 struct vnode *vp; 76 int waitfor; 77{ 78 struct ext2_sb_info *fs; 79 struct buf *bp; 80 struct inode *ip; 81 int error; 82 83 ext2_itimes(vp); 84 ip = VTOI(vp); 85 if ((ip->i_flag & IN_MODIFIED) == 0) 86 return (0); 87 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED); 88 if (vp->v_mount->mnt_flag & MNT_RDONLY) 89 return (0); 90 fs = ip->i_e2fs; 91 if ((error = bread(ip->i_devvp, 92 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 93 (int)fs->s_blocksize, NOCRED, &bp)) != 0) { 94 brelse(bp); 95 return (error); 96 } 97 ext2_i2ei(ip, (struct ext2_inode *)((char *)bp->b_data + 98 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number))); 99/* 100 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 101 return (bwrite(bp)); 102 else { 103*/ 104 bdwrite(bp); 105 return (0); 106/* 107 } 108*/ 109} 110 111#define SINGLE 0 /* index of single indirect block */ 112#define DOUBLE 1 /* index of double indirect block */ 113#define TRIPLE 2 /* index of triple indirect block */ 114/* 115 * Truncate the inode oip to at most length size, freeing the 116 * disk blocks. 117 */ 118int 119ext2_truncate(vp, length, flags, cred, td) 120 struct vnode *vp; 121 off_t length; 122 int flags; 123 struct ucred *cred; 124 struct thread *td; 125{ 126 struct vnode *ovp = vp; 127 int32_t lastblock; 128 struct inode *oip; 129 int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 130 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; 131 struct ext2_sb_info *fs; 132 struct buf *bp; 133 int offset, size, level; 134 long count, nblocks, blocksreleased = 0; 135 int aflags, error, i, allerror; 136 off_t osize; 137/* 138printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length); 139*/ /* 140 * negative file sizes will totally break the code below and 141 * are not meaningful anyways. 142 */ 143 if (length < 0) 144 return EFBIG; 145 146 oip = VTOI(ovp); 147 if (ovp->v_type == VLNK && 148 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) { 149#if DIAGNOSTIC 150 if (length != 0) 151 panic("ext2_truncate: partial truncate of symlink"); 152#endif 153 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); 154 oip->i_size = 0; 155 oip->i_flag |= IN_CHANGE | IN_UPDATE; 156 return (ext2_update(ovp, 1)); 157 } 158 if (oip->i_size == length) { 159 oip->i_flag |= IN_CHANGE | IN_UPDATE; 160 return (ext2_update(ovp, 0)); 161 } 162 fs = oip->i_e2fs; 163 osize = oip->i_size; 164 ext2_discard_prealloc(oip); 165 /* 166 * Lengthen the size of the file. We must ensure that the 167 * last byte of the file is allocated. Since the smallest 168 * value of oszie is 0, length will be at least 1. 169 */ 170 if (osize < length) { 171 offset = blkoff(fs, length - 1); 172 lbn = lblkno(fs, length - 1); 173 aflags = B_CLRBUF; 174 if (flags & IO_SYNC) 175 aflags |= B_SYNC; 176 vnode_pager_setsize(ovp, length); 177 if ((error = ext2_balloc(oip, lbn, offset + 1, cred, &bp, 178 aflags)) != 0) 179 return (error); 180 oip->i_size = length; 181 if (aflags & IO_SYNC) 182 bwrite(bp); 183 else 184 bawrite(bp); 185 oip->i_flag |= IN_CHANGE | IN_UPDATE; 186 return (ext2_update(ovp, 1)); 187 } 188 /* 189 * Shorten the size of the file. If the file is not being 190 * truncated to a block boundry, the contents of the 191 * partial block following the end of the file must be 192 * zero'ed in case it ever become accessible again because 193 * of subsequent file growth. 194 */ 195 /* I don't understand the comment above */ 196 offset = blkoff(fs, length); 197 if (offset == 0) { 198 oip->i_size = length; 199 } else { 200 lbn = lblkno(fs, length); 201 aflags = B_CLRBUF; 202 if (flags & IO_SYNC) 203 aflags |= B_SYNC; 204 if ((error = ext2_balloc(oip, lbn, offset, cred, &bp, 205 aflags)) != 0) 206 return (error); 207 oip->i_size = length; 208 size = blksize(fs, oip, lbn); 209 bzero((char *)bp->b_data + offset, (u_int)(size - offset)); 210 allocbuf(bp, size); 211 if (aflags & IO_SYNC) 212 bwrite(bp); 213 else 214 bawrite(bp); 215 } 216 /* 217 * Calculate index into inode's block list of 218 * last direct and indirect blocks (if any) 219 * which we want to keep. Lastblock is -1 when 220 * the file is truncated to 0. 221 */ 222 lastblock = lblkno(fs, length + fs->s_blocksize - 1) - 1; 223 lastiblock[SINGLE] = lastblock - NDADDR; 224 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 225 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 226 nblocks = btodb(fs->s_blocksize); 227 /* 228 * Update file and block pointers on disk before we start freeing 229 * blocks. If we crash before free'ing blocks below, the blocks 230 * will be returned to the free list. lastiblock values are also 231 * normalized to -1 for calls to ext2_indirtrunc below. 232 */ 233 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); 234 for (level = TRIPLE; level >= SINGLE; level--) 235 if (lastiblock[level] < 0) { 236 oip->i_ib[level] = 0; 237 lastiblock[level] = -1; 238 } 239 for (i = NDADDR - 1; i > lastblock; i--) 240 oip->i_db[i] = 0; 241 oip->i_flag |= IN_CHANGE | IN_UPDATE; 242 allerror = ext2_update(ovp, 1); 243 244 /* 245 * Having written the new inode to disk, save its new configuration 246 * and put back the old block pointers long enough to process them. 247 * Note that we save the new block configuration so we can check it 248 * when we are done. 249 */ 250 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); 251 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); 252 oip->i_size = osize; 253 error = vtruncbuf(ovp, cred, td, length, (int)fs->s_blocksize); 254 if (error && (allerror == 0)) 255 allerror = error; 256 257 /* 258 * Indirect blocks first. 259 */ 260 indir_lbn[SINGLE] = -NDADDR; 261 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; 262 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 263 for (level = TRIPLE; level >= SINGLE; level--) { 264 bn = oip->i_ib[level]; 265 if (bn != 0) { 266 error = ext2_indirtrunc(oip, indir_lbn[level], 267 fsbtodb(fs, bn), lastiblock[level], level, &count); 268 if (error) 269 allerror = error; 270 blocksreleased += count; 271 if (lastiblock[level] < 0) { 272 oip->i_ib[level] = 0; 273 ext2_blkfree(oip, bn, fs->s_frag_size); 274 blocksreleased += nblocks; 275 } 276 } 277 if (lastiblock[level] >= 0) 278 goto done; 279 } 280 281 /* 282 * All whole direct blocks or frags. 283 */ 284 for (i = NDADDR - 1; i > lastblock; i--) { 285 long bsize; 286 287 bn = oip->i_db[i]; 288 if (bn == 0) 289 continue; 290 oip->i_db[i] = 0; 291 bsize = blksize(fs, oip, i); 292 ext2_blkfree(oip, bn, bsize); 293 blocksreleased += btodb(bsize); 294 } 295 if (lastblock < 0) 296 goto done; 297 298 /* 299 * Finally, look for a change in size of the 300 * last direct block; release any frags. 301 */ 302 bn = oip->i_db[lastblock]; 303 if (bn != 0) { 304 long oldspace, newspace; 305 306 /* 307 * Calculate amount of space we're giving 308 * back as old block size minus new block size. 309 */ 310 oldspace = blksize(fs, oip, lastblock); 311 oip->i_size = length; 312 newspace = blksize(fs, oip, lastblock); 313 if (newspace == 0) 314 panic("itrunc: newspace"); 315 if (oldspace - newspace > 0) { 316 /* 317 * Block number of space to be free'd is 318 * the old block # plus the number of frags 319 * required for the storage we're keeping. 320 */ 321 bn += numfrags(fs, newspace); 322 ext2_blkfree(oip, bn, oldspace - newspace); 323 blocksreleased += btodb(oldspace - newspace); 324 } 325 } 326done: 327#if DIAGNOSTIC 328 for (level = SINGLE; level <= TRIPLE; level++) 329 if (newblks[NDADDR + level] != oip->i_ib[level]) 330 panic("itrunc1"); 331 for (i = 0; i < NDADDR; i++) 332 if (newblks[i] != oip->i_db[i]) 333 panic("itrunc2"); 334 VI_LOCK(ovp); 335 if (length == 0 && (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) || 336 !TAILQ_EMPTY(&ovp->v_cleanblkhd))) 337 panic("itrunc3"); 338 VI_UNLOCK(ovp); 339#endif /* DIAGNOSTIC */ 340 /* 341 * Put back the real size. 342 */ 343 oip->i_size = length; 344 oip->i_blocks -= blocksreleased; 345 if (oip->i_blocks < 0) /* sanity */ 346 oip->i_blocks = 0; 347 oip->i_flag |= IN_CHANGE; 348 vnode_pager_setsize(ovp, length); 349 return (allerror); 350} 351 352/* 353 * Release blocks associated with the inode ip and stored in the indirect 354 * block bn. Blocks are free'd in LIFO order up to (but not including) 355 * lastbn. If level is greater than SINGLE, the block is an indirect block 356 * and recursive calls to indirtrunc must be used to cleanse other indirect 357 * blocks. 358 * 359 * NB: triple indirect blocks are untested. 360 */ 361 362static int 363ext2_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 364 struct inode *ip; 365 int32_t lbn, lastbn; 366 int32_t dbn; 367 int level; 368 long *countp; 369{ 370 struct buf *bp; 371 struct ext2_sb_info *fs = ip->i_e2fs; 372 struct vnode *vp; 373 int32_t *bap, *copy, nb, nlbn, last; 374 long blkcount, factor; 375 int i, nblocks, blocksreleased = 0; 376 int error = 0, allerror = 0; 377 378 /* 379 * Calculate index in current block of last 380 * block to be kept. -1 indicates the entire 381 * block so we need not calculate the index. 382 */ 383 factor = 1; 384 for (i = SINGLE; i < level; i++) 385 factor *= NINDIR(fs); 386 last = lastbn; 387 if (lastbn > 0) 388 last /= factor; 389 nblocks = btodb(fs->s_blocksize); 390 /* 391 * Get buffer of block pointers, zero those entries corresponding 392 * to blocks to be free'd, and update on disk copy first. Since 393 * double(triple) indirect before single(double) indirect, calls 394 * to bmap on these blocks will fail. However, we already have 395 * the on disk address, so we have to set the b_blkno field 396 * explicitly instead of letting bread do everything for us. 397 */ 398 vp = ITOV(ip); 399 bp = getblk(vp, lbn, (int)fs->s_blocksize, 0, 0); 400 if (bp->b_flags & (B_DONE | B_DELWRI)) { 401 } else { 402 bp->b_iocmd = BIO_READ; 403 if (bp->b_bcount > bp->b_bufsize) 404 panic("ext2_indirtrunc: bad buffer size"); 405 bp->b_blkno = dbn; 406 vfs_busy_pages(bp, 0); 407 VOP_STRATEGY(vp, bp); 408 error = bufwait(bp); 409 } 410 if (error) { 411 brelse(bp); 412 *countp = 0; 413 return (error); 414 } 415 416 bap = (int32_t *)bp->b_data; 417 MALLOC(copy, int32_t *, fs->s_blocksize, M_TEMP, M_WAITOK); 418 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->s_blocksize); 419 bzero((caddr_t)&bap[last + 1], 420 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t)); 421 if (last == -1) 422 bp->b_flags |= B_INVAL; 423 error = bwrite(bp); 424 if (error) 425 allerror = error; 426 bap = copy; 427 428 /* 429 * Recursively free totally unused blocks. 430 */ 431 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 432 i--, nlbn += factor) { 433 nb = bap[i]; 434 if (nb == 0) 435 continue; 436 if (level > SINGLE) { 437 if ((error = ext2_indirtrunc(ip, nlbn, 438 fsbtodb(fs, nb), (int32_t)-1, level - 1, &blkcount)) != 0) 439 allerror = error; 440 blocksreleased += blkcount; 441 } 442 ext2_blkfree(ip, nb, fs->s_blocksize); 443 blocksreleased += nblocks; 444 } 445 446 /* 447 * Recursively free last partial block. 448 */ 449 if (level > SINGLE && lastbn >= 0) { 450 last = lastbn % factor; 451 nb = bap[i]; 452 if (nb != 0) { 453 if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 454 last, level - 1, &blkcount)) != 0) 455 allerror = error; 456 blocksreleased += blkcount; 457 } 458 } 459 FREE(copy, M_TEMP); 460 *countp = blocksreleased; 461 return (allerror); 462} 463 464/* 465 * discard preallocated blocks 466 */ 467int 468ext2_inactive(ap) 469 struct vop_inactive_args /* { 470 struct vnode *a_vp; 471 struct thread *a_td; 472 } */ *ap; 473{ 474 struct vnode *vp = ap->a_vp; 475 struct inode *ip = VTOI(vp); 476 struct thread *td = ap->a_td; 477 int mode, error = 0; 478 479 ext2_discard_prealloc(ip); 480 if (prtactive && vrefcnt(vp) != 0) 481 vprint("ext2_inactive: pushing active", vp); 482 483 /* 484 * Ignore inodes related to stale file handles. 485 */ 486 if (ip->i_mode == 0) 487 goto out; 488 if (ip->i_nlink <= 0) { 489 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 490 error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td); 491 ip->i_rdev = 0; 492 mode = ip->i_mode; 493 ip->i_mode = 0; 494 ip->i_flag |= IN_CHANGE | IN_UPDATE; 495 ext2_vfree(vp, ip->i_number, mode); 496 } 497 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 498 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 && 499 vn_write_suspend_wait(vp, NULL, V_NOWAIT)) { 500 ip->i_flag &= ~IN_ACCESS; 501 } else { 502 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 503 ext2_update(vp, 0); 504 } 505 } 506out: 507 VOP_UNLOCK(vp, 0, td); 508 /* 509 * If we are done with the inode, reclaim it 510 * so that it can be reused immediately. 511 */ 512 if (ip->i_mode == 0) 513 vrecycle(vp, NULL, td); 514 return (error); 515} 516 517/* 518 * Reclaim an inode so that it can be used for other purposes. 519 */ 520int 521ext2_reclaim(ap) 522 struct vop_reclaim_args /* { 523 struct vnode *a_vp; 524 struct thread *a_td; 525 } */ *ap; 526{ 527 struct inode *ip; 528 struct vnode *vp = ap->a_vp; 529 530 if (prtactive && vrefcnt(vp) != 0) 531 vprint("ufs_reclaim: pushing active", vp); 532 ip = VTOI(vp); 533 if (ip->i_flag & IN_LAZYMOD) { 534 ip->i_flag |= IN_MODIFIED; 535 ext2_update(vp, 0); 536 } 537 /* 538 * Remove the inode from its hash chain. 539 */ 540 ext2_ihashrem(ip); 541 /* 542 * Purge old data structures associated with the inode. 543 */ 544 cache_purge(vp); 545 if (ip->i_devvp) { 546 vrele(ip->i_devvp); 547 ip->i_devvp = 0; 548 }
| 41 */ 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/mount.h> 46#include <sys/bio.h> 47#include <sys/buf.h> 48#include <sys/vnode.h> 49#include <sys/malloc.h> 50 51#include <vm/vm.h> 52#include <vm/vm_extern.h> 53 54#include <gnu/ext2fs/inode.h> 55#include <gnu/ext2fs/ext2_mount.h> 56#include <gnu/ext2fs/ext2_fs.h> 57#include <gnu/ext2fs/ext2_fs_sb.h> 58#include <gnu/ext2fs/fs.h> 59#include <gnu/ext2fs/ext2_extern.h> 60 61static int ext2_indirtrunc(struct inode *, int32_t, int32_t, int32_t, int, 62 long *); 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. If we write now, then clear both IN_MODIFIED and 70 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is 71 * set, then wait for the write to complete. 72 */ 73int 74ext2_update(vp, waitfor) 75 struct vnode *vp; 76 int waitfor; 77{ 78 struct ext2_sb_info *fs; 79 struct buf *bp; 80 struct inode *ip; 81 int error; 82 83 ext2_itimes(vp); 84 ip = VTOI(vp); 85 if ((ip->i_flag & IN_MODIFIED) == 0) 86 return (0); 87 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED); 88 if (vp->v_mount->mnt_flag & MNT_RDONLY) 89 return (0); 90 fs = ip->i_e2fs; 91 if ((error = bread(ip->i_devvp, 92 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 93 (int)fs->s_blocksize, NOCRED, &bp)) != 0) { 94 brelse(bp); 95 return (error); 96 } 97 ext2_i2ei(ip, (struct ext2_inode *)((char *)bp->b_data + 98 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number))); 99/* 100 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 101 return (bwrite(bp)); 102 else { 103*/ 104 bdwrite(bp); 105 return (0); 106/* 107 } 108*/ 109} 110 111#define SINGLE 0 /* index of single indirect block */ 112#define DOUBLE 1 /* index of double indirect block */ 113#define TRIPLE 2 /* index of triple indirect block */ 114/* 115 * Truncate the inode oip to at most length size, freeing the 116 * disk blocks. 117 */ 118int 119ext2_truncate(vp, length, flags, cred, td) 120 struct vnode *vp; 121 off_t length; 122 int flags; 123 struct ucred *cred; 124 struct thread *td; 125{ 126 struct vnode *ovp = vp; 127 int32_t lastblock; 128 struct inode *oip; 129 int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 130 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; 131 struct ext2_sb_info *fs; 132 struct buf *bp; 133 int offset, size, level; 134 long count, nblocks, blocksreleased = 0; 135 int aflags, error, i, allerror; 136 off_t osize; 137/* 138printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length); 139*/ /* 140 * negative file sizes will totally break the code below and 141 * are not meaningful anyways. 142 */ 143 if (length < 0) 144 return EFBIG; 145 146 oip = VTOI(ovp); 147 if (ovp->v_type == VLNK && 148 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) { 149#if DIAGNOSTIC 150 if (length != 0) 151 panic("ext2_truncate: partial truncate of symlink"); 152#endif 153 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); 154 oip->i_size = 0; 155 oip->i_flag |= IN_CHANGE | IN_UPDATE; 156 return (ext2_update(ovp, 1)); 157 } 158 if (oip->i_size == length) { 159 oip->i_flag |= IN_CHANGE | IN_UPDATE; 160 return (ext2_update(ovp, 0)); 161 } 162 fs = oip->i_e2fs; 163 osize = oip->i_size; 164 ext2_discard_prealloc(oip); 165 /* 166 * Lengthen the size of the file. We must ensure that the 167 * last byte of the file is allocated. Since the smallest 168 * value of oszie is 0, length will be at least 1. 169 */ 170 if (osize < length) { 171 offset = blkoff(fs, length - 1); 172 lbn = lblkno(fs, length - 1); 173 aflags = B_CLRBUF; 174 if (flags & IO_SYNC) 175 aflags |= B_SYNC; 176 vnode_pager_setsize(ovp, length); 177 if ((error = ext2_balloc(oip, lbn, offset + 1, cred, &bp, 178 aflags)) != 0) 179 return (error); 180 oip->i_size = length; 181 if (aflags & IO_SYNC) 182 bwrite(bp); 183 else 184 bawrite(bp); 185 oip->i_flag |= IN_CHANGE | IN_UPDATE; 186 return (ext2_update(ovp, 1)); 187 } 188 /* 189 * Shorten the size of the file. If the file is not being 190 * truncated to a block boundry, the contents of the 191 * partial block following the end of the file must be 192 * zero'ed in case it ever become accessible again because 193 * of subsequent file growth. 194 */ 195 /* I don't understand the comment above */ 196 offset = blkoff(fs, length); 197 if (offset == 0) { 198 oip->i_size = length; 199 } else { 200 lbn = lblkno(fs, length); 201 aflags = B_CLRBUF; 202 if (flags & IO_SYNC) 203 aflags |= B_SYNC; 204 if ((error = ext2_balloc(oip, lbn, offset, cred, &bp, 205 aflags)) != 0) 206 return (error); 207 oip->i_size = length; 208 size = blksize(fs, oip, lbn); 209 bzero((char *)bp->b_data + offset, (u_int)(size - offset)); 210 allocbuf(bp, size); 211 if (aflags & IO_SYNC) 212 bwrite(bp); 213 else 214 bawrite(bp); 215 } 216 /* 217 * Calculate index into inode's block list of 218 * last direct and indirect blocks (if any) 219 * which we want to keep. Lastblock is -1 when 220 * the file is truncated to 0. 221 */ 222 lastblock = lblkno(fs, length + fs->s_blocksize - 1) - 1; 223 lastiblock[SINGLE] = lastblock - NDADDR; 224 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 225 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 226 nblocks = btodb(fs->s_blocksize); 227 /* 228 * Update file and block pointers on disk before we start freeing 229 * blocks. If we crash before free'ing blocks below, the blocks 230 * will be returned to the free list. lastiblock values are also 231 * normalized to -1 for calls to ext2_indirtrunc below. 232 */ 233 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); 234 for (level = TRIPLE; level >= SINGLE; level--) 235 if (lastiblock[level] < 0) { 236 oip->i_ib[level] = 0; 237 lastiblock[level] = -1; 238 } 239 for (i = NDADDR - 1; i > lastblock; i--) 240 oip->i_db[i] = 0; 241 oip->i_flag |= IN_CHANGE | IN_UPDATE; 242 allerror = ext2_update(ovp, 1); 243 244 /* 245 * Having written the new inode to disk, save its new configuration 246 * and put back the old block pointers long enough to process them. 247 * Note that we save the new block configuration so we can check it 248 * when we are done. 249 */ 250 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); 251 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); 252 oip->i_size = osize; 253 error = vtruncbuf(ovp, cred, td, length, (int)fs->s_blocksize); 254 if (error && (allerror == 0)) 255 allerror = error; 256 257 /* 258 * Indirect blocks first. 259 */ 260 indir_lbn[SINGLE] = -NDADDR; 261 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; 262 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 263 for (level = TRIPLE; level >= SINGLE; level--) { 264 bn = oip->i_ib[level]; 265 if (bn != 0) { 266 error = ext2_indirtrunc(oip, indir_lbn[level], 267 fsbtodb(fs, bn), lastiblock[level], level, &count); 268 if (error) 269 allerror = error; 270 blocksreleased += count; 271 if (lastiblock[level] < 0) { 272 oip->i_ib[level] = 0; 273 ext2_blkfree(oip, bn, fs->s_frag_size); 274 blocksreleased += nblocks; 275 } 276 } 277 if (lastiblock[level] >= 0) 278 goto done; 279 } 280 281 /* 282 * All whole direct blocks or frags. 283 */ 284 for (i = NDADDR - 1; i > lastblock; i--) { 285 long bsize; 286 287 bn = oip->i_db[i]; 288 if (bn == 0) 289 continue; 290 oip->i_db[i] = 0; 291 bsize = blksize(fs, oip, i); 292 ext2_blkfree(oip, bn, bsize); 293 blocksreleased += btodb(bsize); 294 } 295 if (lastblock < 0) 296 goto done; 297 298 /* 299 * Finally, look for a change in size of the 300 * last direct block; release any frags. 301 */ 302 bn = oip->i_db[lastblock]; 303 if (bn != 0) { 304 long oldspace, newspace; 305 306 /* 307 * Calculate amount of space we're giving 308 * back as old block size minus new block size. 309 */ 310 oldspace = blksize(fs, oip, lastblock); 311 oip->i_size = length; 312 newspace = blksize(fs, oip, lastblock); 313 if (newspace == 0) 314 panic("itrunc: newspace"); 315 if (oldspace - newspace > 0) { 316 /* 317 * Block number of space to be free'd is 318 * the old block # plus the number of frags 319 * required for the storage we're keeping. 320 */ 321 bn += numfrags(fs, newspace); 322 ext2_blkfree(oip, bn, oldspace - newspace); 323 blocksreleased += btodb(oldspace - newspace); 324 } 325 } 326done: 327#if DIAGNOSTIC 328 for (level = SINGLE; level <= TRIPLE; level++) 329 if (newblks[NDADDR + level] != oip->i_ib[level]) 330 panic("itrunc1"); 331 for (i = 0; i < NDADDR; i++) 332 if (newblks[i] != oip->i_db[i]) 333 panic("itrunc2"); 334 VI_LOCK(ovp); 335 if (length == 0 && (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) || 336 !TAILQ_EMPTY(&ovp->v_cleanblkhd))) 337 panic("itrunc3"); 338 VI_UNLOCK(ovp); 339#endif /* DIAGNOSTIC */ 340 /* 341 * Put back the real size. 342 */ 343 oip->i_size = length; 344 oip->i_blocks -= blocksreleased; 345 if (oip->i_blocks < 0) /* sanity */ 346 oip->i_blocks = 0; 347 oip->i_flag |= IN_CHANGE; 348 vnode_pager_setsize(ovp, length); 349 return (allerror); 350} 351 352/* 353 * Release blocks associated with the inode ip and stored in the indirect 354 * block bn. Blocks are free'd in LIFO order up to (but not including) 355 * lastbn. If level is greater than SINGLE, the block is an indirect block 356 * and recursive calls to indirtrunc must be used to cleanse other indirect 357 * blocks. 358 * 359 * NB: triple indirect blocks are untested. 360 */ 361 362static int 363ext2_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 364 struct inode *ip; 365 int32_t lbn, lastbn; 366 int32_t dbn; 367 int level; 368 long *countp; 369{ 370 struct buf *bp; 371 struct ext2_sb_info *fs = ip->i_e2fs; 372 struct vnode *vp; 373 int32_t *bap, *copy, nb, nlbn, last; 374 long blkcount, factor; 375 int i, nblocks, blocksreleased = 0; 376 int error = 0, allerror = 0; 377 378 /* 379 * Calculate index in current block of last 380 * block to be kept. -1 indicates the entire 381 * block so we need not calculate the index. 382 */ 383 factor = 1; 384 for (i = SINGLE; i < level; i++) 385 factor *= NINDIR(fs); 386 last = lastbn; 387 if (lastbn > 0) 388 last /= factor; 389 nblocks = btodb(fs->s_blocksize); 390 /* 391 * Get buffer of block pointers, zero those entries corresponding 392 * to blocks to be free'd, and update on disk copy first. Since 393 * double(triple) indirect before single(double) indirect, calls 394 * to bmap on these blocks will fail. However, we already have 395 * the on disk address, so we have to set the b_blkno field 396 * explicitly instead of letting bread do everything for us. 397 */ 398 vp = ITOV(ip); 399 bp = getblk(vp, lbn, (int)fs->s_blocksize, 0, 0); 400 if (bp->b_flags & (B_DONE | B_DELWRI)) { 401 } else { 402 bp->b_iocmd = BIO_READ; 403 if (bp->b_bcount > bp->b_bufsize) 404 panic("ext2_indirtrunc: bad buffer size"); 405 bp->b_blkno = dbn; 406 vfs_busy_pages(bp, 0); 407 VOP_STRATEGY(vp, bp); 408 error = bufwait(bp); 409 } 410 if (error) { 411 brelse(bp); 412 *countp = 0; 413 return (error); 414 } 415 416 bap = (int32_t *)bp->b_data; 417 MALLOC(copy, int32_t *, fs->s_blocksize, M_TEMP, M_WAITOK); 418 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->s_blocksize); 419 bzero((caddr_t)&bap[last + 1], 420 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t)); 421 if (last == -1) 422 bp->b_flags |= B_INVAL; 423 error = bwrite(bp); 424 if (error) 425 allerror = error; 426 bap = copy; 427 428 /* 429 * Recursively free totally unused blocks. 430 */ 431 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 432 i--, nlbn += factor) { 433 nb = bap[i]; 434 if (nb == 0) 435 continue; 436 if (level > SINGLE) { 437 if ((error = ext2_indirtrunc(ip, nlbn, 438 fsbtodb(fs, nb), (int32_t)-1, level - 1, &blkcount)) != 0) 439 allerror = error; 440 blocksreleased += blkcount; 441 } 442 ext2_blkfree(ip, nb, fs->s_blocksize); 443 blocksreleased += nblocks; 444 } 445 446 /* 447 * Recursively free last partial block. 448 */ 449 if (level > SINGLE && lastbn >= 0) { 450 last = lastbn % factor; 451 nb = bap[i]; 452 if (nb != 0) { 453 if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 454 last, level - 1, &blkcount)) != 0) 455 allerror = error; 456 blocksreleased += blkcount; 457 } 458 } 459 FREE(copy, M_TEMP); 460 *countp = blocksreleased; 461 return (allerror); 462} 463 464/* 465 * discard preallocated blocks 466 */ 467int 468ext2_inactive(ap) 469 struct vop_inactive_args /* { 470 struct vnode *a_vp; 471 struct thread *a_td; 472 } */ *ap; 473{ 474 struct vnode *vp = ap->a_vp; 475 struct inode *ip = VTOI(vp); 476 struct thread *td = ap->a_td; 477 int mode, error = 0; 478 479 ext2_discard_prealloc(ip); 480 if (prtactive && vrefcnt(vp) != 0) 481 vprint("ext2_inactive: pushing active", vp); 482 483 /* 484 * Ignore inodes related to stale file handles. 485 */ 486 if (ip->i_mode == 0) 487 goto out; 488 if (ip->i_nlink <= 0) { 489 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 490 error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td); 491 ip->i_rdev = 0; 492 mode = ip->i_mode; 493 ip->i_mode = 0; 494 ip->i_flag |= IN_CHANGE | IN_UPDATE; 495 ext2_vfree(vp, ip->i_number, mode); 496 } 497 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 498 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 && 499 vn_write_suspend_wait(vp, NULL, V_NOWAIT)) { 500 ip->i_flag &= ~IN_ACCESS; 501 } else { 502 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 503 ext2_update(vp, 0); 504 } 505 } 506out: 507 VOP_UNLOCK(vp, 0, td); 508 /* 509 * If we are done with the inode, reclaim it 510 * so that it can be reused immediately. 511 */ 512 if (ip->i_mode == 0) 513 vrecycle(vp, NULL, td); 514 return (error); 515} 516 517/* 518 * Reclaim an inode so that it can be used for other purposes. 519 */ 520int 521ext2_reclaim(ap) 522 struct vop_reclaim_args /* { 523 struct vnode *a_vp; 524 struct thread *a_td; 525 } */ *ap; 526{ 527 struct inode *ip; 528 struct vnode *vp = ap->a_vp; 529 530 if (prtactive && vrefcnt(vp) != 0) 531 vprint("ufs_reclaim: pushing active", vp); 532 ip = VTOI(vp); 533 if (ip->i_flag & IN_LAZYMOD) { 534 ip->i_flag |= IN_MODIFIED; 535 ext2_update(vp, 0); 536 } 537 /* 538 * Remove the inode from its hash chain. 539 */ 540 ext2_ihashrem(ip); 541 /* 542 * Purge old data structures associated with the inode. 543 */ 544 cache_purge(vp); 545 if (ip->i_devvp) { 546 vrele(ip->i_devvp); 547 ip->i_devvp = 0; 548 }
|