add patch revert-remove-block_device_ejected
[ext4-patch-queue.git] / limit-number-of-reserved-credits
blobb26d6cbe3921d5556cf09b34be120f7dcd35a6a5
1 jbd2: limit number of reserved credits
3 From: Lukas Czerner <lczerner@redhat.com>
5 Currently there is no limitation on number of reserved credits we can
6 ask for. If we ask for more reserved credits than 1/2 of maximum
7 transaction size, or if total number of credits exceeds the maximum
8 transaction size per operation (which is currently only possible with
9 the former) we will spin forever in start_this_handle().
11 Fix this by adding this limitation at the start of start_this_handle().
13 This patch also removes the credit limitation 1/2 of maximum transaction
14 size, since we really only want to limit the number of reserved credits.
15 There is not much point to limit the credits if there is still space in
16 the journal.
18 This accidentally also fixes the online resize, where due to the
19 limitation of the journal credits we're unable to grow file systems with
20 1k block size and size between 16M and 32M. It has been partially fixed
21 by 2c869b262a10ca99cb866d04087d75311587a30c, but not entirely.
23 Thanks Jan Kara for helping me getting the correct fix.
25 Signed-off-by: Lukas Czerner <lczerner@redhat.com>
26 Reviewed-by: Jan Kara <jack@suse.cz>
27 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
28 ---
29 v2: added wait event Jan suggested
31  fs/jbd2/transaction.c | 36 +++++++++++++++++++++++++++---------
32  1 file changed, 27 insertions(+), 9 deletions(-)
34 diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
35 index f3d0617..f8183d4 100644
36 --- a/fs/jbd2/transaction.c
37 +++ b/fs/jbd2/transaction.c
38 @@ -204,6 +204,20 @@ static int add_transaction_credits(journal_t *journal, int blocks,
39                  * attach this handle to a new transaction.
40                  */
41                 atomic_sub(total, &t->t_outstanding_credits);
43 +               /*
44 +                * Is the number of reserved credits in the current transaction too
45 +                * big to fit this handle? Wait until reserved credits are freed.
46 +                */
47 +               if (atomic_read(&journal->j_reserved_credits) + total >
48 +                   journal->j_max_transaction_buffers) {
49 +                       read_unlock(&journal->j_state_lock);
50 +                       wait_event(journal->j_wait_reserved,
51 +                                  atomic_read(&journal->j_reserved_credits) + total <=
52 +                                  journal->j_max_transaction_buffers);
53 +                       return 1;
54 +               }
56                 wait_transaction_locked(journal);
57                 return 1;
58         }
59 @@ -262,20 +276,24 @@ static int start_this_handle(journal_t *journal, handle_t *handle,
60         int             rsv_blocks = 0;
61         unsigned long ts = jiffies;
63 +       if (handle->h_rsv_handle)
64 +               rsv_blocks = handle->h_rsv_handle->h_buffer_credits;
66         /*
67 -        * 1/2 of transaction can be reserved so we can practically handle
68 -        * only 1/2 of maximum transaction size per operation
69 +        * Limit the number of reserved credits to 1/2 of maximum transaction
70 +        * size and limit the number of total credits to not exceed maximum
71 +        * transaction size per operation.
72          */
73 -       if (WARN_ON(blocks > journal->j_max_transaction_buffers / 2)) {
74 -               printk(KERN_ERR "JBD2: %s wants too many credits (%d > %d)\n",
75 -                      current->comm, blocks,
76 -                      journal->j_max_transaction_buffers / 2);
77 +       if ((rsv_blocks > journal->j_max_transaction_buffers / 2) ||
78 +           (rsv_blocks + blocks > journal->j_max_transaction_buffers)) {
79 +               printk(KERN_ERR "JBD2: %s wants too many credits "
80 +                      "credits:%d rsv_credits:%d max:%d\n",
81 +                      current->comm, blocks, rsv_blocks,
82 +                      journal->j_max_transaction_buffers);
83 +               WARN_ON(1);
84                 return -ENOSPC;
85         }
87 -       if (handle->h_rsv_handle)
88 -               rsv_blocks = handle->h_rsv_handle->h_buffer_credits;
90  alloc_transaction:
91         if (!journal->j_running_transaction) {
92                 /*