forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/fs/ext4/balloc.c
....@@ -368,7 +368,12 @@
368368 struct buffer_head *bh)
369369 {
370370 ext4_fsblk_t blk;
371
- struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
371
+ struct ext4_group_info *grp;
372
+
373
+ if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
374
+ return 0;
375
+
376
+ grp = ext4_get_group_info(sb, block_group);
372377
373378 if (buffer_verified(bh))
374379 return 0;
....@@ -379,7 +384,8 @@
379384 if (buffer_verified(bh))
380385 goto verified;
381386 if (unlikely(!ext4_block_bitmap_csum_verify(sb, block_group,
382
- desc, bh))) {
387
+ desc, bh) ||
388
+ ext4_simulate_fail(sb, EXT4_SIM_BBITMAP_CRC))) {
383389 ext4_unlock_group(sb, block_group);
384390 ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
385391 ext4_mark_group_bitmap_corrupted(sb, block_group,
....@@ -409,10 +415,11 @@
409415 * Read the bitmap for a given block_group,and validate the
410416 * bits for block/inode/inode tables are set in the bitmaps
411417 *
412
- * Return buffer_head on success or NULL in case of failure.
418
+ * Return buffer_head on success or an ERR_PTR in case of failure.
413419 */
414420 struct buffer_head *
415
-ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
421
+ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group,
422
+ bool ignore_locked)
416423 {
417424 struct ext4_group_desc *desc;
418425 struct ext4_sb_info *sbi = EXT4_SB(sb);
....@@ -438,6 +445,12 @@
438445 "block_group = %u, block_bitmap = %llu",
439446 block_group, bitmap_blk);
440447 return ERR_PTR(-ENOMEM);
448
+ }
449
+
450
+ if (ignore_locked && buffer_locked(bh)) {
451
+ /* buffer under IO already, return if called for prefetching */
452
+ put_bh(bh);
453
+ return NULL;
441454 }
442455
443456 if (bitmap_uptodate(bh))
....@@ -486,10 +499,10 @@
486499 * submit the buffer_head for reading
487500 */
488501 set_buffer_new(bh);
489
- trace_ext4_read_block_bitmap_load(sb, block_group);
490
- bh->b_end_io = ext4_end_bitmap_read;
491
- get_bh(bh);
492
- submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
502
+ trace_ext4_read_block_bitmap_load(sb, block_group, ignore_locked);
503
+ ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO |
504
+ (ignore_locked ? REQ_RAHEAD : 0),
505
+ ext4_end_bitmap_read);
493506 return bh;
494507 verify:
495508 err = ext4_validate_block_bitmap(sb, desc, block_group, bh);
....@@ -501,7 +514,7 @@
501514 return ERR_PTR(err);
502515 }
503516
504
-/* Returns 0 on success, 1 on error */
517
+/* Returns 0 on success, -errno on error */
505518 int ext4_wait_block_bitmap(struct super_block *sb, ext4_group_t block_group,
506519 struct buffer_head *bh)
507520 {
....@@ -513,10 +526,11 @@
513526 if (!desc)
514527 return -EFSCORRUPTED;
515528 wait_on_buffer(bh);
529
+ ext4_simulate_fail_bh(sb, bh, EXT4_SIM_BBITMAP_EIO);
516530 if (!buffer_uptodate(bh)) {
517
- ext4_error(sb, "Cannot read block bitmap - "
518
- "block_group = %u, block_bitmap = %llu",
519
- block_group, (unsigned long long) bh->b_blocknr);
531
+ ext4_error_err(sb, EIO, "Cannot read block bitmap - "
532
+ "block_group = %u, block_bitmap = %llu",
533
+ block_group, (unsigned long long) bh->b_blocknr);
520534 ext4_mark_group_bitmap_corrupted(sb, block_group,
521535 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
522536 return -EIO;
....@@ -532,7 +546,7 @@
532546 struct buffer_head *bh;
533547 int err;
534548
535
- bh = ext4_read_block_bitmap_nowait(sb, block_group);
549
+ bh = ext4_read_block_bitmap_nowait(sb, block_group, false);
536550 if (IS_ERR(bh))
537551 return bh;
538552 err = ext4_wait_block_bitmap(sb, block_group, bh);
....@@ -611,28 +625,42 @@
611625 }
612626
613627 /**
614
- * ext4_should_retry_alloc()
615
- * @sb: super block
616
- * @retries number of attemps has been made
628
+ * ext4_should_retry_alloc() - check if a block allocation should be retried
629
+ * @sb: superblock
630
+ * @retries: number of retry attempts made so far
617631 *
618
- * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
619
- * it is profitable to retry the operation, this function will wait
620
- * for the current or committing transaction to complete, and then
621
- * return TRUE. We will only retry once.
632
+ * ext4_should_retry_alloc() is called when ENOSPC is returned while
633
+ * attempting to allocate blocks. If there's an indication that a pending
634
+ * journal transaction might free some space and allow another attempt to
635
+ * succeed, this function will wait for the current or committing transaction
636
+ * to complete and then return TRUE.
622637 */
623638 int ext4_should_retry_alloc(struct super_block *sb, int *retries)
624639 {
625
- if (!ext4_has_free_clusters(EXT4_SB(sb), 1, 0) ||
626
- (*retries)++ > 1 ||
627
- !EXT4_SB(sb)->s_journal)
640
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
641
+
642
+ if (!sbi->s_journal)
628643 return 0;
629644
645
+ if (++(*retries) > 3) {
646
+ percpu_counter_inc(&sbi->s_sra_exceeded_retry_limit);
647
+ return 0;
648
+ }
649
+
650
+ /*
651
+ * if there's no indication that blocks are about to be freed it's
652
+ * possible we just missed a transaction commit that did so
653
+ */
630654 smp_mb();
631
- if (EXT4_SB(sb)->s_mb_free_pending == 0)
632
- return 0;
655
+ if (sbi->s_mb_free_pending == 0)
656
+ return ext4_has_free_clusters(sbi, 1, 0);
633657
658
+ /*
659
+ * it's possible we've just missed a transaction commit here,
660
+ * so ignore the returned status
661
+ */
634662 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
635
- jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
663
+ (void) jbd2_journal_force_commit_nested(sbi->s_journal);
636664 return 1;
637665 }
638666
....@@ -901,10 +929,11 @@
901929 return bg_start;
902930
903931 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
904
- colour = (current->pid % 16) *
932
+ colour = (task_pid_nr(current) % 16) *
905933 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
906934 else
907
- colour = (current->pid % 16) * ((last_block - bg_start) / 16);
935
+ colour = (task_pid_nr(current) % 16) *
936
+ ((last_block - bg_start) / 16);
908937 return bg_start + colour;
909938 }
910939