add patch only-call-ext4_truncate-if-there-is-data-to-truncate
[ext4-patch-queue.git] / optimize-ext4_should_retry_alloc-to-improve-ENOSPC-performance
blob141efb46e1f620b1cc658be85ad23d8e71c5627e
1 ext4: optimize ext4_should_retry_alloc() to improve ENOSPC performance
3 If there are no pending blocks to be released after a commit, forcing
4 a journal commit has no hope of helping.  It's possible that a commit
5 had just completed, so if there are now free blocks available for
6 allocation, it's worth retrying the commit.
8 Reported-by: Chao Yu <yuchao0@huawei.com>
9 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
10 ---
11  fs/ext4/balloc.c    |  4 +++-
12  fs/ext4/ext4.h      |  1 +
13  fs/ext4/ext4_jbd2.h | 10 +++++++++-
14  fs/ext4/mballoc.c   | 12 ++++++++++--
15  4 files changed, 23 insertions(+), 4 deletions(-)
17 diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
18 index 3020fd7..0b8105b 100644
19 --- a/fs/ext4/balloc.c
20 +++ b/fs/ext4/balloc.c
21 @@ -610,7 +610,9 @@ int ext4_should_retry_alloc(struct super_block *sb, int *retries)
23         jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
25 -       jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
26 +       smp_mb();
27 +       if (EXT4_SB(sb)->s_mb_free_pending)
28 +               jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
29         return 1;
30  }
32 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
33 index b84aa1c..96c73e6 100644
34 --- a/fs/ext4/ext4.h
35 +++ b/fs/ext4/ext4.h
36 @@ -1430,6 +1430,7 @@ struct ext4_sb_info {
37         unsigned short *s_mb_offsets;
38         unsigned int *s_mb_maxs;
39         unsigned int s_group_info_size;
40 +       unsigned int s_mb_free_pending;
42         /* tunables */
43         unsigned long s_stripe;
44 diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
45 index 09c1ef3..b1d52c1 100644
46 --- a/fs/ext4/ext4_jbd2.h
47 +++ b/fs/ext4/ext4_jbd2.h
48 @@ -175,6 +175,13 @@ struct ext4_journal_cb_entry {
49   * There is no guaranteed calling order of multiple registered callbacks on
50   * the same transaction.
51   */
52 +static inline void _ext4_journal_callback_add(handle_t *handle,
53 +                       struct ext4_journal_cb_entry *jce)
55 +       /* Add the jce to transaction's private list */
56 +       list_add_tail(&jce->jce_list, &handle->h_transaction->t_private_list);
59  static inline void ext4_journal_callback_add(handle_t *handle,
60                         void (*func)(struct super_block *sb,
61                                      struct ext4_journal_cb_entry *jce,
62 @@ -187,10 +194,11 @@ static inline void ext4_journal_callback_add(handle_t *handle,
63         /* Add the jce to transaction's private list */
64         jce->jce_func = func;
65         spin_lock(&sbi->s_md_lock);
66 -       list_add_tail(&jce->jce_list, &handle->h_transaction->t_private_list);
67 +       _ext4_journal_callback_add(handle, jce);
68         spin_unlock(&sbi->s_md_lock);
69  }
72  /**
73   * ext4_journal_callback_del: delete a registered callback
74   * @handle: active journal transaction handle on which callback was registered
75 diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
76 index c1ab3ec..77249e1 100644
77 --- a/fs/ext4/mballoc.c
78 +++ b/fs/ext4/mballoc.c
79 @@ -2627,6 +2627,7 @@ int ext4_mb_init(struct super_block *sb)
81         spin_lock_init(&sbi->s_md_lock);
82         spin_lock_init(&sbi->s_bal_lock);
83 +       sbi->s_mb_free_pending = 0;
85         sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
86         sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
87 @@ -2814,6 +2815,9 @@ static void ext4_free_data_callback(struct super_block *sb,
88         /* we expect to find existing buddy because it's pinned */
89         BUG_ON(err != 0);
91 +       spin_lock(&EXT4_SB(sb)->s_md_lock);
92 +       EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
93 +       spin_unlock(&EXT4_SB(sb)->s_md_lock);
95         db = e4b.bd_info;
96         /* there are blocks to put in buddy to make them really free */
97 @@ -4583,6 +4587,7 @@ ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
98  {
99         ext4_group_t group = e4b->bd_group;
100         ext4_grpblk_t cluster;
101 +       ext4_grpblk_t clusters = new_entry->efd_count;
102         struct ext4_free_data *entry;
103         struct ext4_group_info *db = e4b->bd_info;
104         struct super_block *sb = e4b->bd_sb;
105 @@ -4649,8 +4654,11 @@ ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
106                 }
107         }
108         /* Add the extent to transaction's private list */
109 -       ext4_journal_callback_add(handle, ext4_free_data_callback,
110 -                                 &new_entry->efd_jce);
111 +       new_entry->efd_jce.jce_func = ext4_free_data_callback;
112 +       spin_lock(&sbi->s_md_lock);
113 +       _ext4_journal_callback_add(handle, &new_entry->efd_jce);
114 +       sbi->s_mb_free_pending += clusters;
115 +       spin_unlock(&sbi->s_md_lock);
116         return 0;