| .. | .. |
|---|
| 163 | 163 | } |
|---|
| 164 | 164 | |
|---|
| 165 | 165 | if (!bh_uptodate_or_lock(bh)) { |
|---|
| 166 | | - if (bh_submit_read(bh) < 0) { |
|---|
| 166 | + if (ext4_read_bh(bh, 0, NULL) < 0) { |
|---|
| 167 | 167 | put_bh(bh); |
|---|
| 168 | 168 | goto failure; |
|---|
| 169 | 169 | } |
|---|
| .. | .. |
|---|
| 294 | 294 | } |
|---|
| 295 | 295 | |
|---|
| 296 | 296 | /** |
|---|
| 297 | | - * ext4_alloc_branch - allocate and set up a chain of blocks. |
|---|
| 298 | | - * @handle: handle for this transaction |
|---|
| 299 | | - * @inode: owner |
|---|
| 300 | | - * @indirect_blks: number of allocated indirect blocks |
|---|
| 301 | | - * @blks: number of allocated direct blocks |
|---|
| 302 | | - * @goal: preferred place for allocation |
|---|
| 303 | | - * @offsets: offsets (in the blocks) to store the pointers to next. |
|---|
| 304 | | - * @branch: place to store the chain in. |
|---|
| 297 | + * ext4_alloc_branch() - allocate and set up a chain of blocks |
|---|
| 298 | + * @handle: handle for this transaction |
|---|
| 299 | + * @ar: structure describing the allocation request |
|---|
| 300 | + * @indirect_blks: number of allocated indirect blocks |
|---|
| 301 | + * @offsets: offsets (in the blocks) to store the pointers to next. |
|---|
| 302 | + * @branch: place to store the chain in. |
|---|
| 305 | 303 | * |
|---|
| 306 | 304 | * This function allocates blocks, zeroes out all but the last one, |
|---|
| 307 | 305 | * links them into chain and (if we are synchronous) writes them to disk. |
|---|
| .. | .. |
|---|
| 333 | 331 | for (i = 0; i <= indirect_blks; i++) { |
|---|
| 334 | 332 | if (i == indirect_blks) { |
|---|
| 335 | 333 | new_blocks[i] = ext4_mb_new_blocks(handle, ar, &err); |
|---|
| 336 | | - } else |
|---|
| 334 | + } else { |
|---|
| 337 | 335 | ar->goal = new_blocks[i] = ext4_new_meta_blocks(handle, |
|---|
| 338 | 336 | ar->inode, ar->goal, |
|---|
| 339 | 337 | ar->flags & EXT4_MB_DELALLOC_RESERVED, |
|---|
| 340 | 338 | NULL, &err); |
|---|
| 339 | + /* Simplify error cleanup... */ |
|---|
| 340 | + branch[i+1].bh = NULL; |
|---|
| 341 | + } |
|---|
| 341 | 342 | if (err) { |
|---|
| 342 | 343 | i--; |
|---|
| 343 | 344 | goto failed; |
|---|
| .. | .. |
|---|
| 379 | 380 | } |
|---|
| 380 | 381 | return 0; |
|---|
| 381 | 382 | failed: |
|---|
| 383 | + if (i == indirect_blks) { |
|---|
| 384 | + /* Free data blocks */ |
|---|
| 385 | + ext4_free_blocks(handle, ar->inode, NULL, new_blocks[i], |
|---|
| 386 | + ar->len, 0); |
|---|
| 387 | + i--; |
|---|
| 388 | + } |
|---|
| 382 | 389 | for (; i >= 0; i--) { |
|---|
| 383 | 390 | /* |
|---|
| 384 | 391 | * We want to ext4_forget() only freshly allocated indirect |
|---|
| 385 | | - * blocks. Buffer for new_blocks[i-1] is at branch[i].bh and |
|---|
| 386 | | - * buffer at branch[0].bh is indirect block / inode already |
|---|
| 387 | | - * existing before ext4_alloc_branch() was called. |
|---|
| 392 | + * blocks. Buffer for new_blocks[i] is at branch[i+1].bh |
|---|
| 393 | + * (buffer at branch[0].bh is indirect block / inode already |
|---|
| 394 | + * existing before ext4_alloc_branch() was called). Also |
|---|
| 395 | + * because blocks are freshly allocated, we don't need to |
|---|
| 396 | + * revoke them which is why we don't set |
|---|
| 397 | + * EXT4_FREE_BLOCKS_METADATA. |
|---|
| 388 | 398 | */ |
|---|
| 389 | | - if (i > 0 && i != indirect_blks && branch[i].bh) |
|---|
| 390 | | - ext4_forget(handle, 1, ar->inode, branch[i].bh, |
|---|
| 391 | | - branch[i].bh->b_blocknr); |
|---|
| 392 | | - ext4_free_blocks(handle, ar->inode, NULL, new_blocks[i], |
|---|
| 393 | | - (i == indirect_blks) ? ar->len : 1, 0); |
|---|
| 399 | + ext4_free_blocks(handle, ar->inode, branch[i+1].bh, |
|---|
| 400 | + new_blocks[i], 1, |
|---|
| 401 | + branch[i+1].bh ? EXT4_FREE_BLOCKS_FORGET : 0); |
|---|
| 394 | 402 | } |
|---|
| 395 | 403 | return err; |
|---|
| 396 | 404 | } |
|---|
| 397 | 405 | |
|---|
| 398 | 406 | /** |
|---|
| 399 | | - * ext4_splice_branch - splice the allocated branch onto inode. |
|---|
| 407 | + * ext4_splice_branch() - splice the allocated branch onto inode. |
|---|
| 400 | 408 | * @handle: handle for this transaction |
|---|
| 401 | | - * @inode: owner |
|---|
| 402 | | - * @block: (logical) number of block we are adding |
|---|
| 403 | | - * @chain: chain of indirect blocks (with a missing link - see |
|---|
| 404 | | - * ext4_alloc_branch) |
|---|
| 409 | + * @ar: structure describing the allocation request |
|---|
| 405 | 410 | * @where: location of missing link |
|---|
| 406 | 411 | * @num: number of indirect blocks we are adding |
|---|
| 407 | | - * @blks: number of direct blocks we are adding |
|---|
| 408 | 412 | * |
|---|
| 409 | 413 | * This function fills the missing link and does all housekeeping needed in |
|---|
| 410 | 414 | * inode (->i_blocks, etc.). In case of success we end up with the full |
|---|
| .. | .. |
|---|
| 463 | 467 | /* |
|---|
| 464 | 468 | * OK, we spliced it into the inode itself on a direct block. |
|---|
| 465 | 469 | */ |
|---|
| 466 | | - ext4_mark_inode_dirty(handle, ar->inode); |
|---|
| 470 | + err = ext4_mark_inode_dirty(handle, ar->inode); |
|---|
| 471 | + if (unlikely(err)) |
|---|
| 472 | + goto err_out; |
|---|
| 467 | 473 | jbd_debug(5, "splicing direct\n"); |
|---|
| 468 | 474 | } |
|---|
| 469 | 475 | return err; |
|---|
| .. | .. |
|---|
| 587 | 593 | if (ext4_has_feature_bigalloc(inode->i_sb)) { |
|---|
| 588 | 594 | EXT4_ERROR_INODE(inode, "Can't allocate blocks for " |
|---|
| 589 | 595 | "non-extent mapped inodes with bigalloc"); |
|---|
| 590 | | - return -EFSCORRUPTED; |
|---|
| 596 | + err = -EFSCORRUPTED; |
|---|
| 597 | + goto out; |
|---|
| 591 | 598 | } |
|---|
| 592 | 599 | |
|---|
| 593 | 600 | /* Set up for the direct block allocation */ |
|---|
| .. | .. |
|---|
| 656 | 663 | } |
|---|
| 657 | 664 | |
|---|
| 658 | 665 | /* |
|---|
| 659 | | - * Calculate the number of metadata blocks need to reserve |
|---|
| 660 | | - * to allocate a new block at @lblocks for non extent file based file |
|---|
| 661 | | - */ |
|---|
| 662 | | -int ext4_ind_calc_metadata_amount(struct inode *inode, sector_t lblock) |
|---|
| 663 | | -{ |
|---|
| 664 | | - struct ext4_inode_info *ei = EXT4_I(inode); |
|---|
| 665 | | - sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1); |
|---|
| 666 | | - int blk_bits; |
|---|
| 667 | | - |
|---|
| 668 | | - if (lblock < EXT4_NDIR_BLOCKS) |
|---|
| 669 | | - return 0; |
|---|
| 670 | | - |
|---|
| 671 | | - lblock -= EXT4_NDIR_BLOCKS; |
|---|
| 672 | | - |
|---|
| 673 | | - if (ei->i_da_metadata_calc_len && |
|---|
| 674 | | - (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) { |
|---|
| 675 | | - ei->i_da_metadata_calc_len++; |
|---|
| 676 | | - return 0; |
|---|
| 677 | | - } |
|---|
| 678 | | - ei->i_da_metadata_calc_last_lblock = lblock & dind_mask; |
|---|
| 679 | | - ei->i_da_metadata_calc_len = 1; |
|---|
| 680 | | - blk_bits = order_base_2(lblock); |
|---|
| 681 | | - return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1; |
|---|
| 682 | | -} |
|---|
| 683 | | - |
|---|
| 684 | | -/* |
|---|
| 685 | 666 | * Calculate number of indirect blocks touched by mapping @nrblocks logically |
|---|
| 686 | 667 | * contiguous blocks |
|---|
| 687 | 668 | */ |
|---|
| .. | .. |
|---|
| 695 | 676 | return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4; |
|---|
| 696 | 677 | } |
|---|
| 697 | 678 | |
|---|
| 679 | +static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode, |
|---|
| 680 | + struct buffer_head *bh, int *dropped) |
|---|
| 681 | +{ |
|---|
| 682 | + int err; |
|---|
| 683 | + |
|---|
| 684 | + if (bh) { |
|---|
| 685 | + BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
|---|
| 686 | + err = ext4_handle_dirty_metadata(handle, inode, bh); |
|---|
| 687 | + if (unlikely(err)) |
|---|
| 688 | + return err; |
|---|
| 689 | + } |
|---|
| 690 | + err = ext4_mark_inode_dirty(handle, inode); |
|---|
| 691 | + if (unlikely(err)) |
|---|
| 692 | + return err; |
|---|
| 693 | + /* |
|---|
| 694 | + * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this |
|---|
| 695 | + * moment, get_block can be called only for blocks inside i_size since |
|---|
| 696 | + * page cache has been already dropped and writes are blocked by |
|---|
| 697 | + * i_mutex. So we can safely drop the i_data_sem here. |
|---|
| 698 | + */ |
|---|
| 699 | + BUG_ON(EXT4_JOURNAL(inode) == NULL); |
|---|
| 700 | + ext4_discard_preallocations(inode, 0); |
|---|
| 701 | + up_write(&EXT4_I(inode)->i_data_sem); |
|---|
| 702 | + *dropped = 1; |
|---|
| 703 | + return 0; |
|---|
| 704 | +} |
|---|
| 705 | + |
|---|
| 698 | 706 | /* |
|---|
| 699 | 707 | * Truncate transactions can be complex and absolutely huge. So we need to |
|---|
| 700 | 708 | * be able to restart the transaction at a conventient checkpoint to make |
|---|
| 701 | 709 | * sure we don't overflow the journal. |
|---|
| 702 | 710 | * |
|---|
| 703 | 711 | * Try to extend this transaction for the purposes of truncation. If |
|---|
| 704 | | - * extend fails, we need to propagate the failure up and restart the |
|---|
| 705 | | - * transaction in the top-level truncate loop. --sct |
|---|
| 706 | | - * |
|---|
| 707 | | - * Returns 0 if we managed to create more room. If we can't create more |
|---|
| 708 | | - * room, and the transaction must be restarted we return 1. |
|---|
| 712 | + * extend fails, we restart transaction. |
|---|
| 709 | 713 | */ |
|---|
| 710 | | -static int try_to_extend_transaction(handle_t *handle, struct inode *inode) |
|---|
| 714 | +static int ext4_ind_truncate_ensure_credits(handle_t *handle, |
|---|
| 715 | + struct inode *inode, |
|---|
| 716 | + struct buffer_head *bh, |
|---|
| 717 | + int revoke_creds) |
|---|
| 711 | 718 | { |
|---|
| 712 | | - if (!ext4_handle_valid(handle)) |
|---|
| 713 | | - return 0; |
|---|
| 714 | | - if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) |
|---|
| 715 | | - return 0; |
|---|
| 716 | | - if (!ext4_journal_extend(handle, ext4_blocks_for_truncate(inode))) |
|---|
| 717 | | - return 0; |
|---|
| 718 | | - return 1; |
|---|
| 719 | + int ret; |
|---|
| 720 | + int dropped = 0; |
|---|
| 721 | + |
|---|
| 722 | + ret = ext4_journal_ensure_credits_fn(handle, EXT4_RESERVE_TRANS_BLOCKS, |
|---|
| 723 | + ext4_blocks_for_truncate(inode), revoke_creds, |
|---|
| 724 | + ext4_ind_trunc_restart_fn(handle, inode, bh, &dropped)); |
|---|
| 725 | + if (dropped) |
|---|
| 726 | + down_write(&EXT4_I(inode)->i_data_sem); |
|---|
| 727 | + if (ret <= 0) |
|---|
| 728 | + return ret; |
|---|
| 729 | + if (bh) { |
|---|
| 730 | + BUFFER_TRACE(bh, "retaking write access"); |
|---|
| 731 | + ret = ext4_journal_get_write_access(handle, bh); |
|---|
| 732 | + if (unlikely(ret)) |
|---|
| 733 | + return ret; |
|---|
| 734 | + } |
|---|
| 735 | + return 0; |
|---|
| 719 | 736 | } |
|---|
| 720 | 737 | |
|---|
| 721 | 738 | /* |
|---|
| .. | .. |
|---|
| 849 | 866 | return 1; |
|---|
| 850 | 867 | } |
|---|
| 851 | 868 | |
|---|
| 852 | | - if (try_to_extend_transaction(handle, inode)) { |
|---|
| 853 | | - if (bh) { |
|---|
| 854 | | - BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
|---|
| 855 | | - err = ext4_handle_dirty_metadata(handle, inode, bh); |
|---|
| 856 | | - if (unlikely(err)) |
|---|
| 857 | | - goto out_err; |
|---|
| 858 | | - } |
|---|
| 859 | | - err = ext4_mark_inode_dirty(handle, inode); |
|---|
| 860 | | - if (unlikely(err)) |
|---|
| 861 | | - goto out_err; |
|---|
| 862 | | - err = ext4_truncate_restart_trans(handle, inode, |
|---|
| 863 | | - ext4_blocks_for_truncate(inode)); |
|---|
| 864 | | - if (unlikely(err)) |
|---|
| 865 | | - goto out_err; |
|---|
| 866 | | - if (bh) { |
|---|
| 867 | | - BUFFER_TRACE(bh, "retaking write access"); |
|---|
| 868 | | - err = ext4_journal_get_write_access(handle, bh); |
|---|
| 869 | | - if (unlikely(err)) |
|---|
| 870 | | - goto out_err; |
|---|
| 871 | | - } |
|---|
| 872 | | - } |
|---|
| 869 | + err = ext4_ind_truncate_ensure_credits(handle, inode, bh, |
|---|
| 870 | + ext4_free_data_revoke_credits(inode, count)); |
|---|
| 871 | + if (err < 0) |
|---|
| 872 | + goto out_err; |
|---|
| 873 | 873 | |
|---|
| 874 | 874 | for (p = first; p < last; p++) |
|---|
| 875 | 875 | *p = 0; |
|---|
| .. | .. |
|---|
| 1013 | 1013 | } |
|---|
| 1014 | 1014 | |
|---|
| 1015 | 1015 | /* Go read the buffer for the next level down */ |
|---|
| 1016 | | - bh = sb_bread(inode->i_sb, nr); |
|---|
| 1016 | + bh = ext4_sb_bread(inode->i_sb, nr, 0); |
|---|
| 1017 | 1017 | |
|---|
| 1018 | 1018 | /* |
|---|
| 1019 | 1019 | * A read failure? Report error and clear slot |
|---|
| 1020 | 1020 | * (should be rare). |
|---|
| 1021 | 1021 | */ |
|---|
| 1022 | | - if (!bh) { |
|---|
| 1023 | | - EXT4_ERROR_INODE_BLOCK(inode, nr, |
|---|
| 1022 | + if (IS_ERR(bh)) { |
|---|
| 1023 | + ext4_error_inode_block(inode, nr, -PTR_ERR(bh), |
|---|
| 1024 | 1024 | "Read failure"); |
|---|
| 1025 | 1025 | continue; |
|---|
| 1026 | 1026 | } |
|---|
| .. | .. |
|---|
| 1034 | 1034 | brelse(bh); |
|---|
| 1035 | 1035 | |
|---|
| 1036 | 1036 | /* |
|---|
| 1037 | | - * Everything below this this pointer has been |
|---|
| 1037 | + * Everything below this pointer has been |
|---|
| 1038 | 1038 | * released. Now let this top-of-subtree go. |
|---|
| 1039 | 1039 | * |
|---|
| 1040 | 1040 | * We want the freeing of this indirect block to be |
|---|
| .. | .. |
|---|
| 1051 | 1051 | */ |
|---|
| 1052 | 1052 | if (ext4_handle_is_aborted(handle)) |
|---|
| 1053 | 1053 | return; |
|---|
| 1054 | | - if (try_to_extend_transaction(handle, inode)) { |
|---|
| 1055 | | - ext4_mark_inode_dirty(handle, inode); |
|---|
| 1056 | | - ext4_truncate_restart_trans(handle, inode, |
|---|
| 1057 | | - ext4_blocks_for_truncate(inode)); |
|---|
| 1058 | | - } |
|---|
| 1054 | + if (ext4_ind_truncate_ensure_credits(handle, inode, |
|---|
| 1055 | + NULL, |
|---|
| 1056 | + ext4_free_metadata_revoke_credits( |
|---|
| 1057 | + inode->i_sb, 1)) < 0) |
|---|
| 1058 | + return; |
|---|
| 1059 | 1059 | |
|---|
| 1060 | 1060 | /* |
|---|
| 1061 | 1061 | * The forget flag here is critical because if |
|---|
| .. | .. |
|---|
| 1181 | 1181 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); |
|---|
| 1182 | 1182 | i_data[EXT4_IND_BLOCK] = 0; |
|---|
| 1183 | 1183 | } |
|---|
| 1184 | + fallthrough; |
|---|
| 1184 | 1185 | case EXT4_IND_BLOCK: |
|---|
| 1185 | 1186 | nr = i_data[EXT4_DIND_BLOCK]; |
|---|
| 1186 | 1187 | if (nr) { |
|---|
| 1187 | 1188 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); |
|---|
| 1188 | 1189 | i_data[EXT4_DIND_BLOCK] = 0; |
|---|
| 1189 | 1190 | } |
|---|
| 1191 | + fallthrough; |
|---|
| 1190 | 1192 | case EXT4_DIND_BLOCK: |
|---|
| 1191 | 1193 | nr = i_data[EXT4_TIND_BLOCK]; |
|---|
| 1192 | 1194 | if (nr) { |
|---|
| 1193 | 1195 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); |
|---|
| 1194 | 1196 | i_data[EXT4_TIND_BLOCK] = 0; |
|---|
| 1195 | 1197 | } |
|---|
| 1198 | + fallthrough; |
|---|
| 1196 | 1199 | case EXT4_TIND_BLOCK: |
|---|
| 1197 | 1200 | ; |
|---|
| 1198 | 1201 | } |
|---|
| .. | .. |
|---|
| 1432 | 1435 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); |
|---|
| 1433 | 1436 | i_data[EXT4_IND_BLOCK] = 0; |
|---|
| 1434 | 1437 | } |
|---|
| 1438 | + fallthrough; |
|---|
| 1435 | 1439 | case EXT4_IND_BLOCK: |
|---|
| 1436 | 1440 | if (++n >= n2) |
|---|
| 1437 | 1441 | break; |
|---|
| .. | .. |
|---|
| 1440 | 1444 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); |
|---|
| 1441 | 1445 | i_data[EXT4_DIND_BLOCK] = 0; |
|---|
| 1442 | 1446 | } |
|---|
| 1447 | + fallthrough; |
|---|
| 1443 | 1448 | case EXT4_DIND_BLOCK: |
|---|
| 1444 | 1449 | if (++n >= n2) |
|---|
| 1445 | 1450 | break; |
|---|
| .. | .. |
|---|
| 1448 | 1453 | ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); |
|---|
| 1449 | 1454 | i_data[EXT4_TIND_BLOCK] = 0; |
|---|
| 1450 | 1455 | } |
|---|
| 1456 | + fallthrough; |
|---|
| 1451 | 1457 | case EXT4_TIND_BLOCK: |
|---|
| 1452 | 1458 | ; |
|---|
| 1453 | 1459 | } |
|---|