]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/3.4.83/ext4-jbd2-don-t-wait-forever-for-stale-tid-caused-by-wraparound.patch
Linux 3.4.83
[thirdparty/kernel/stable-queue.git] / releases / 3.4.83 / ext4-jbd2-don-t-wait-forever-for-stale-tid-caused-by-wraparound.patch
1 From 164ed4383ca615f9a4f19673ac82372ccf96a33f Mon Sep 17 00:00:00 2001
2 From: Theodore Ts'o <tytso@mit.edu>
3 Date: Wed, 3 Apr 2013 22:02:52 -0400
4 Subject: ext4/jbd2: don't wait (forever) for stale tid caused by wraparound
5
6 From: Theodore Ts'o <tytso@mit.edu>
7
8 commit d76a3a77113db020d9bb1e894822869410450bd9 upstream.
9
10 In the case where an inode has a very stale transaction id (tid) in
11 i_datasync_tid or i_sync_tid, it's possible that after a very large
12 (2**31) number of transactions, that the tid number space might wrap,
13 causing tid_geq()'s calculations to fail.
14
15 Commit deeeaf13 "jbd2: fix fsync() tid wraparound bug", later modified
16 by commit e7b04ac0 "jbd2: don't wake kjournald unnecessarily",
17 attempted to fix this problem, but it only avoided kjournald spinning
18 forever by fixing the logic in jbd2_log_start_commit().
19
20 Unfortunately, in the codepaths in fs/ext4/fsync.c and fs/ext4/inode.c
21 that might call jbd2_log_start_commit() with a stale tid, those
22 functions will subsequently call jbd2_log_wait_commit() with the same
23 stale tid, and then wait for a very long time. To fix this, we
24 replace the calls to jbd2_log_start_commit() and
25 jbd2_log_wait_commit() with a call to a new function,
26 jbd2_complete_transaction(), which will correctly handle stale tid's.
27
28 As a bonus, jbd2_complete_transaction() will avoid locking
29 j_state_lock for writing unless a commit needs to be started. This
30 should have a small (but probably not measurable) improvement for
31 ext4's scalability.
32
33 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
34 Reported-by: Ben Hutchings <ben@decadent.org.uk>
35 Reported-by: George Barnett <gbarnett@atlassian.com>
36 [bwh: Backported to 3.2: adjust context]
37 Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
38 Cc: Rui Xiang <rui.xiang@huawei.com>
39 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
40
41 ---
42 fs/ext4/fsync.c | 3 +--
43 fs/ext4/inode.c | 3 +--
44 fs/jbd2/journal.c | 31 +++++++++++++++++++++++++++++++
45 include/linux/jbd2.h | 1 +
46 4 files changed, 34 insertions(+), 4 deletions(-)
47
48 --- a/fs/ext4/fsync.c
49 +++ b/fs/ext4/fsync.c
50 @@ -260,8 +260,7 @@ int ext4_sync_file(struct file *file, lo
51 if (journal->j_flags & JBD2_BARRIER &&
52 !jbd2_trans_will_send_data_barrier(journal, commit_tid))
53 needs_barrier = true;
54 - jbd2_log_start_commit(journal, commit_tid);
55 - ret = jbd2_log_wait_commit(journal, commit_tid);
56 + ret = jbd2_complete_transaction(journal, commit_tid);
57 if (needs_barrier)
58 blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
59 out:
60 --- a/fs/ext4/inode.c
61 +++ b/fs/ext4/inode.c
62 @@ -149,8 +149,7 @@ void ext4_evict_inode(struct inode *inod
63 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
64 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
65
66 - jbd2_log_start_commit(journal, commit_tid);
67 - jbd2_log_wait_commit(journal, commit_tid);
68 + jbd2_complete_transaction(journal, commit_tid);
69 filemap_write_and_wait(&inode->i_data);
70 }
71 truncate_inode_pages(&inode->i_data, 0);
72 --- a/fs/jbd2/journal.c
73 +++ b/fs/jbd2/journal.c
74 @@ -662,6 +662,37 @@ int jbd2_log_wait_commit(journal_t *jour
75 }
76
77 /*
78 + * When this function returns the transaction corresponding to tid
79 + * will be completed. If the transaction has currently running, start
80 + * committing that transaction before waiting for it to complete. If
81 + * the transaction id is stale, it is by definition already completed,
82 + * so just return SUCCESS.
83 + */
84 +int jbd2_complete_transaction(journal_t *journal, tid_t tid)
85 +{
86 + int need_to_wait = 1;
87 +
88 + read_lock(&journal->j_state_lock);
89 + if (journal->j_running_transaction &&
90 + journal->j_running_transaction->t_tid == tid) {
91 + if (journal->j_commit_request != tid) {
92 + /* transaction not yet started, so request it */
93 + read_unlock(&journal->j_state_lock);
94 + jbd2_log_start_commit(journal, tid);
95 + goto wait_commit;
96 + }
97 + } else if (!(journal->j_committing_transaction &&
98 + journal->j_committing_transaction->t_tid == tid))
99 + need_to_wait = 0;
100 + read_unlock(&journal->j_state_lock);
101 + if (!need_to_wait)
102 + return 0;
103 +wait_commit:
104 + return jbd2_log_wait_commit(journal, tid);
105 +}
106 +EXPORT_SYMBOL(jbd2_complete_transaction);
107 +
108 +/*
109 * Log buffer allocation routines:
110 */
111
112 --- a/include/linux/jbd2.h
113 +++ b/include/linux/jbd2.h
114 @@ -1178,6 +1178,7 @@ int __jbd2_log_start_commit(journal_t *j
115 int jbd2_journal_start_commit(journal_t *journal, tid_t *tid);
116 int jbd2_journal_force_commit_nested(journal_t *journal);
117 int jbd2_log_wait_commit(journal_t *journal, tid_t tid);
118 +int jbd2_complete_transaction(journal_t *journal, tid_t tid);
119 int jbd2_log_do_checkpoint(journal_t *journal);
120 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid);
121