forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/fs/ext4/indirect.c
....@@ -163,7 +163,7 @@
163163 }
164164
165165 if (!bh_uptodate_or_lock(bh)) {
166
- if (bh_submit_read(bh) < 0) {
166
+ if (ext4_read_bh(bh, 0, NULL) < 0) {
167167 put_bh(bh);
168168 goto failure;
169169 }
....@@ -294,14 +294,12 @@
294294 }
295295
296296 /**
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.
305303 *
306304 * This function allocates blocks, zeroes out all but the last one,
307305 * links them into chain and (if we are synchronous) writes them to disk.
....@@ -333,11 +331,14 @@
333331 for (i = 0; i <= indirect_blks; i++) {
334332 if (i == indirect_blks) {
335333 new_blocks[i] = ext4_mb_new_blocks(handle, ar, &err);
336
- } else
334
+ } else {
337335 ar->goal = new_blocks[i] = ext4_new_meta_blocks(handle,
338336 ar->inode, ar->goal,
339337 ar->flags & EXT4_MB_DELALLOC_RESERVED,
340338 NULL, &err);
339
+ /* Simplify error cleanup... */
340
+ branch[i+1].bh = NULL;
341
+ }
341342 if (err) {
342343 i--;
343344 goto failed;
....@@ -379,32 +380,35 @@
379380 }
380381 return 0;
381382 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
+ }
382389 for (; i >= 0; i--) {
383390 /*
384391 * 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.
388398 */
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);
394402 }
395403 return err;
396404 }
397405
398406 /**
399
- * ext4_splice_branch - splice the allocated branch onto inode.
407
+ * ext4_splice_branch() - splice the allocated branch onto inode.
400408 * @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
405410 * @where: location of missing link
406411 * @num: number of indirect blocks we are adding
407
- * @blks: number of direct blocks we are adding
408412 *
409413 * This function fills the missing link and does all housekeeping needed in
410414 * inode (->i_blocks, etc.). In case of success we end up with the full
....@@ -463,7 +467,9 @@
463467 /*
464468 * OK, we spliced it into the inode itself on a direct block.
465469 */
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;
467473 jbd_debug(5, "splicing direct\n");
468474 }
469475 return err;
....@@ -587,7 +593,8 @@
587593 if (ext4_has_feature_bigalloc(inode->i_sb)) {
588594 EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
589595 "non-extent mapped inodes with bigalloc");
590
- return -EFSCORRUPTED;
596
+ err = -EFSCORRUPTED;
597
+ goto out;
591598 }
592599
593600 /* Set up for the direct block allocation */
....@@ -656,32 +663,6 @@
656663 }
657664
658665 /*
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
-/*
685666 * Calculate number of indirect blocks touched by mapping @nrblocks logically
686667 * contiguous blocks
687668 */
....@@ -695,27 +676,63 @@
695676 return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
696677 }
697678
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
+
698706 /*
699707 * Truncate transactions can be complex and absolutely huge. So we need to
700708 * be able to restart the transaction at a conventient checkpoint to make
701709 * sure we don't overflow the journal.
702710 *
703711 * 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.
709713 */
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)
711718 {
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;
719736 }
720737
721738 /*
....@@ -849,27 +866,10 @@
849866 return 1;
850867 }
851868
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;
873873
874874 for (p = first; p < last; p++)
875875 *p = 0;
....@@ -1013,14 +1013,14 @@
10131013 }
10141014
10151015 /* 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);
10171017
10181018 /*
10191019 * A read failure? Report error and clear slot
10201020 * (should be rare).
10211021 */
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),
10241024 "Read failure");
10251025 continue;
10261026 }
....@@ -1034,7 +1034,7 @@
10341034 brelse(bh);
10351035
10361036 /*
1037
- * Everything below this this pointer has been
1037
+ * Everything below this pointer has been
10381038 * released. Now let this top-of-subtree go.
10391039 *
10401040 * We want the freeing of this indirect block to be
....@@ -1051,11 +1051,11 @@
10511051 */
10521052 if (ext4_handle_is_aborted(handle))
10531053 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;
10591059
10601060 /*
10611061 * The forget flag here is critical because if
....@@ -1181,18 +1181,21 @@
11811181 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
11821182 i_data[EXT4_IND_BLOCK] = 0;
11831183 }
1184
+ fallthrough;
11841185 case EXT4_IND_BLOCK:
11851186 nr = i_data[EXT4_DIND_BLOCK];
11861187 if (nr) {
11871188 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
11881189 i_data[EXT4_DIND_BLOCK] = 0;
11891190 }
1191
+ fallthrough;
11901192 case EXT4_DIND_BLOCK:
11911193 nr = i_data[EXT4_TIND_BLOCK];
11921194 if (nr) {
11931195 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
11941196 i_data[EXT4_TIND_BLOCK] = 0;
11951197 }
1198
+ fallthrough;
11961199 case EXT4_TIND_BLOCK:
11971200 ;
11981201 }
....@@ -1432,6 +1435,7 @@
14321435 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
14331436 i_data[EXT4_IND_BLOCK] = 0;
14341437 }
1438
+ fallthrough;
14351439 case EXT4_IND_BLOCK:
14361440 if (++n >= n2)
14371441 break;
....@@ -1440,6 +1444,7 @@
14401444 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
14411445 i_data[EXT4_DIND_BLOCK] = 0;
14421446 }
1447
+ fallthrough;
14431448 case EXT4_DIND_BLOCK:
14441449 if (++n >= n2)
14451450 break;
....@@ -1448,6 +1453,7 @@
14481453 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
14491454 i_data[EXT4_TIND_BLOCK] = 0;
14501455 }
1456
+ fallthrough;
14511457 case EXT4_TIND_BLOCK:
14521458 ;
14531459 }