hc
2024-02-20 e636c8d336489bf3eed5878299e6cc045bbad077
kernel/fs/ext4/indirect.c
....@@ -148,6 +148,7 @@
148148 struct super_block *sb = inode->i_sb;
149149 Indirect *p = chain;
150150 struct buffer_head *bh;
151
+ unsigned int key;
151152 int ret = -EIO;
152153
153154 *err = 0;
....@@ -156,14 +157,20 @@
156157 if (!p->key)
157158 goto no_block;
158159 while (--depth) {
159
- bh = sb_getblk(sb, le32_to_cpu(p->key));
160
+ key = le32_to_cpu(p->key);
161
+ if (key > ext4_blocks_count(EXT4_SB(sb)->s_es)) {
162
+ /* the block was out of range */
163
+ ret = -EFSCORRUPTED;
164
+ goto failure;
165
+ }
166
+ bh = sb_getblk(sb, key);
160167 if (unlikely(!bh)) {
161168 ret = -ENOMEM;
162169 goto failure;
163170 }
164171
165172 if (!bh_uptodate_or_lock(bh)) {
166
- if (bh_submit_read(bh) < 0) {
173
+ if (ext4_read_bh(bh, 0, NULL) < 0) {
167174 put_bh(bh);
168175 goto failure;
169176 }
....@@ -294,14 +301,12 @@
294301 }
295302
296303 /**
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.
304
+ * ext4_alloc_branch() - allocate and set up a chain of blocks
305
+ * @handle: handle for this transaction
306
+ * @ar: structure describing the allocation request
307
+ * @indirect_blks: number of allocated indirect blocks
308
+ * @offsets: offsets (in the blocks) to store the pointers to next.
309
+ * @branch: place to store the chain in.
305310 *
306311 * This function allocates blocks, zeroes out all but the last one,
307312 * links them into chain and (if we are synchronous) writes them to disk.
....@@ -333,11 +338,14 @@
333338 for (i = 0; i <= indirect_blks; i++) {
334339 if (i == indirect_blks) {
335340 new_blocks[i] = ext4_mb_new_blocks(handle, ar, &err);
336
- } else
341
+ } else {
337342 ar->goal = new_blocks[i] = ext4_new_meta_blocks(handle,
338343 ar->inode, ar->goal,
339344 ar->flags & EXT4_MB_DELALLOC_RESERVED,
340345 NULL, &err);
346
+ /* Simplify error cleanup... */
347
+ branch[i+1].bh = NULL;
348
+ }
341349 if (err) {
342350 i--;
343351 goto failed;
....@@ -379,32 +387,35 @@
379387 }
380388 return 0;
381389 failed:
390
+ if (i == indirect_blks) {
391
+ /* Free data blocks */
392
+ ext4_free_blocks(handle, ar->inode, NULL, new_blocks[i],
393
+ ar->len, 0);
394
+ i--;
395
+ }
382396 for (; i >= 0; i--) {
383397 /*
384398 * 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.
399
+ * blocks. Buffer for new_blocks[i] is at branch[i+1].bh
400
+ * (buffer at branch[0].bh is indirect block / inode already
401
+ * existing before ext4_alloc_branch() was called). Also
402
+ * because blocks are freshly allocated, we don't need to
403
+ * revoke them which is why we don't set
404
+ * EXT4_FREE_BLOCKS_METADATA.
388405 */
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);
406
+ ext4_free_blocks(handle, ar->inode, branch[i+1].bh,
407
+ new_blocks[i], 1,
408
+ branch[i+1].bh ? EXT4_FREE_BLOCKS_FORGET : 0);
394409 }
395410 return err;
396411 }
397412
398413 /**
399
- * ext4_splice_branch - splice the allocated branch onto inode.
414
+ * ext4_splice_branch() - splice the allocated branch onto inode.
400415 * @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)
416
+ * @ar: structure describing the allocation request
405417 * @where: location of missing link
406418 * @num: number of indirect blocks we are adding
407
- * @blks: number of direct blocks we are adding
408419 *
409420 * This function fills the missing link and does all housekeeping needed in
410421 * inode (->i_blocks, etc.). In case of success we end up with the full
....@@ -463,7 +474,9 @@
463474 /*
464475 * OK, we spliced it into the inode itself on a direct block.
465476 */
466
- ext4_mark_inode_dirty(handle, ar->inode);
477
+ err = ext4_mark_inode_dirty(handle, ar->inode);
478
+ if (unlikely(err))
479
+ goto err_out;
467480 jbd_debug(5, "splicing direct\n");
468481 }
469482 return err;
....@@ -587,7 +600,8 @@
587600 if (ext4_has_feature_bigalloc(inode->i_sb)) {
588601 EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
589602 "non-extent mapped inodes with bigalloc");
590
- return -EFSCORRUPTED;
603
+ err = -EFSCORRUPTED;
604
+ goto out;
591605 }
592606
593607 /* Set up for the direct block allocation */
....@@ -635,6 +649,14 @@
635649
636650 ext4_update_inode_fsync_trans(handle, inode, 1);
637651 count = ar.len;
652
+
653
+ /*
654
+ * Update reserved blocks/metadata blocks after successful block
655
+ * allocation which had been deferred till now.
656
+ */
657
+ if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
658
+ ext4_da_update_reserve_space(inode, count, 1);
659
+
638660 got_it:
639661 map->m_flags |= EXT4_MAP_MAPPED;
640662 map->m_pblk = le32_to_cpu(chain[depth-1].key);
....@@ -656,32 +678,6 @@
656678 }
657679
658680 /*
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
-/*
685681 * Calculate number of indirect blocks touched by mapping @nrblocks logically
686682 * contiguous blocks
687683 */
....@@ -695,27 +691,63 @@
695691 return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
696692 }
697693
694
+static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode,
695
+ struct buffer_head *bh, int *dropped)
696
+{
697
+ int err;
698
+
699
+ if (bh) {
700
+ BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
701
+ err = ext4_handle_dirty_metadata(handle, inode, bh);
702
+ if (unlikely(err))
703
+ return err;
704
+ }
705
+ err = ext4_mark_inode_dirty(handle, inode);
706
+ if (unlikely(err))
707
+ return err;
708
+ /*
709
+ * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
710
+ * moment, get_block can be called only for blocks inside i_size since
711
+ * page cache has been already dropped and writes are blocked by
712
+ * i_mutex. So we can safely drop the i_data_sem here.
713
+ */
714
+ BUG_ON(EXT4_JOURNAL(inode) == NULL);
715
+ ext4_discard_preallocations(inode, 0);
716
+ up_write(&EXT4_I(inode)->i_data_sem);
717
+ *dropped = 1;
718
+ return 0;
719
+}
720
+
698721 /*
699722 * Truncate transactions can be complex and absolutely huge. So we need to
700
- * be able to restart the transaction at a conventient checkpoint to make
723
+ * be able to restart the transaction at a convenient checkpoint to make
701724 * sure we don't overflow the journal.
702725 *
703726 * 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.
727
+ * extend fails, we restart transaction.
709728 */
710
-static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
729
+static int ext4_ind_truncate_ensure_credits(handle_t *handle,
730
+ struct inode *inode,
731
+ struct buffer_head *bh,
732
+ int revoke_creds)
711733 {
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;
734
+ int ret;
735
+ int dropped = 0;
736
+
737
+ ret = ext4_journal_ensure_credits_fn(handle, EXT4_RESERVE_TRANS_BLOCKS,
738
+ ext4_blocks_for_truncate(inode), revoke_creds,
739
+ ext4_ind_trunc_restart_fn(handle, inode, bh, &dropped));
740
+ if (dropped)
741
+ down_write(&EXT4_I(inode)->i_data_sem);
742
+ if (ret <= 0)
743
+ return ret;
744
+ if (bh) {
745
+ BUFFER_TRACE(bh, "retaking write access");
746
+ ret = ext4_journal_get_write_access(handle, bh);
747
+ if (unlikely(ret))
748
+ return ret;
749
+ }
750
+ return 0;
719751 }
720752
721753 /*
....@@ -849,27 +881,10 @@
849881 return 1;
850882 }
851883
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
- }
884
+ err = ext4_ind_truncate_ensure_credits(handle, inode, bh,
885
+ ext4_free_data_revoke_credits(inode, count));
886
+ if (err < 0)
887
+ goto out_err;
873888
874889 for (p = first; p < last; p++)
875890 *p = 0;
....@@ -1013,14 +1028,14 @@
10131028 }
10141029
10151030 /* Go read the buffer for the next level down */
1016
- bh = sb_bread(inode->i_sb, nr);
1031
+ bh = ext4_sb_bread(inode->i_sb, nr, 0);
10171032
10181033 /*
10191034 * A read failure? Report error and clear slot
10201035 * (should be rare).
10211036 */
1022
- if (!bh) {
1023
- EXT4_ERROR_INODE_BLOCK(inode, nr,
1037
+ if (IS_ERR(bh)) {
1038
+ ext4_error_inode_block(inode, nr, -PTR_ERR(bh),
10241039 "Read failure");
10251040 continue;
10261041 }
....@@ -1034,7 +1049,7 @@
10341049 brelse(bh);
10351050
10361051 /*
1037
- * Everything below this this pointer has been
1052
+ * Everything below this pointer has been
10381053 * released. Now let this top-of-subtree go.
10391054 *
10401055 * We want the freeing of this indirect block to be
....@@ -1051,11 +1066,11 @@
10511066 */
10521067 if (ext4_handle_is_aborted(handle))
10531068 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
- }
1069
+ if (ext4_ind_truncate_ensure_credits(handle, inode,
1070
+ NULL,
1071
+ ext4_free_metadata_revoke_credits(
1072
+ inode->i_sb, 1)) < 0)
1073
+ return;
10591074
10601075 /*
10611076 * The forget flag here is critical because if
....@@ -1181,18 +1196,21 @@
11811196 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
11821197 i_data[EXT4_IND_BLOCK] = 0;
11831198 }
1199
+ fallthrough;
11841200 case EXT4_IND_BLOCK:
11851201 nr = i_data[EXT4_DIND_BLOCK];
11861202 if (nr) {
11871203 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
11881204 i_data[EXT4_DIND_BLOCK] = 0;
11891205 }
1206
+ fallthrough;
11901207 case EXT4_DIND_BLOCK:
11911208 nr = i_data[EXT4_TIND_BLOCK];
11921209 if (nr) {
11931210 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
11941211 i_data[EXT4_TIND_BLOCK] = 0;
11951212 }
1213
+ fallthrough;
11961214 case EXT4_TIND_BLOCK:
11971215 ;
11981216 }
....@@ -1432,6 +1450,7 @@
14321450 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
14331451 i_data[EXT4_IND_BLOCK] = 0;
14341452 }
1453
+ fallthrough;
14351454 case EXT4_IND_BLOCK:
14361455 if (++n >= n2)
14371456 break;
....@@ -1440,6 +1459,7 @@
14401459 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
14411460 i_data[EXT4_DIND_BLOCK] = 0;
14421461 }
1462
+ fallthrough;
14431463 case EXT4_DIND_BLOCK:
14441464 if (++n >= n2)
14451465 break;
....@@ -1448,6 +1468,7 @@
14481468 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
14491469 i_data[EXT4_TIND_BLOCK] = 0;
14501470 }
1471
+ fallthrough;
14511472 case EXT4_TIND_BLOCK:
14521473 ;
14531474 }