1 ext4: add journal_lazy mount option
3 This option turns out the lazy journalling option, as described in the
4 FAST 2017 paper, "Evolving Ext4 for Shingled Disks"[1].
6 [1] https://www.usenix.org/conference/fast17/technical-sessions/presentation/aghayev
8 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
11 fs/ext4/inode.c | 2 +-
12 fs/ext4/ioctl.c | 48 +++++++++++++++++++++++++++++++++---------------
13 fs/ext4/super.c | 32 +++++++++++++++++++++-----------
14 4 files changed, 56 insertions(+), 27 deletions(-)
16 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
17 index 63a4be5d5b17..07942ade7596 100644
20 @@ -1145,6 +1145,7 @@ struct ext4_inode_info {
21 #define EXT4_MOUNT_JOURNAL_CHECKSUM 0x800000 /* Journal checksums */
22 #define EXT4_MOUNT_JOURNAL_ASYNC_COMMIT 0x1000000 /* Journal Async Commit */
23 #define EXT4_MOUNT_JOURNAL_NO_CLEANUP 0x2000000 /* Preserve the journal on unmount */
24 +#define EXT4_MOUNT_JOURNAL_LAZY 0x4000000 /* Do lazy writeback of journalled metadata */
25 #define EXT4_MOUNT_DELALLOC 0x8000000 /* Delalloc support */
26 #define EXT4_MOUNT_DATA_ERR_ABORT 0x10000000 /* Abort on file data write */
27 #define EXT4_MOUNT_BLOCK_VALIDITY 0x20000000 /* Block validity checking */
28 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
29 index 831d025e59ad..00bed25d05b2 100644
32 @@ -3188,7 +3188,7 @@ static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
33 filemap_write_and_wait(mapping);
36 - if (EXT4_JOURNAL(inode) &&
37 + if (EXT4_JOURNAL(inode) && !test_opt(inode->i_sb, JOURNAL_LAZY) &&
38 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
40 * This is a REALLY heavyweight approach, but the use of
41 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
42 index b383ebf4020c..ba234eb5a1bd 100644
45 @@ -242,6 +242,20 @@ static int ext4_ioctl_setflags(struct inode *inode,
46 if (!capable(CAP_SYS_RESOURCE))
51 + * Clearing the JOURNAL_DATA flag is *hard* with lazy
52 + * journalling. We can't use jbd2_journal_flush(); instead,
53 + * we would have to make sure all blocks belonging to the file
54 + * are evacuated from the journal and saved to their final
55 + * location on disk. Punt for now.
57 + if ((oldflags & EXT4_JOURNAL_DATA_FL) && !jflag &&
58 + test_opt(inode->i_sb, JOURNAL_LAZY)) {
63 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
66 @@ -489,6 +503,22 @@ int ext4_goingdown(struct super_block *sb, unsigned long arg)
71 + * If we are using journalling (excepting JBD2 lazy mode), make sure
72 + * the block group descriptors are written out immediately
74 +static int flush_fs_group_descriptors(struct super_block *sb)
78 + if (EXT4_SB(sb)->s_journal && !test_opt(sb, JOURNAL_LAZY)) {
79 + jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
80 + err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
81 + jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
86 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
88 struct inode *inode = file_inode(filp);
89 @@ -606,11 +636,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
90 goto group_extend_out;
92 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
93 - if (EXT4_SB(sb)->s_journal) {
94 - jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
95 - err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
96 - jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
98 + err2 = flush_fs_group_descriptors(sb);
101 mnt_drop_write_file(filp);
102 @@ -696,11 +722,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
105 err = ext4_group_add(sb, &input);
106 - if (EXT4_SB(sb)->s_journal) {
107 - jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
108 - err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
109 - jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
111 + err2 = flush_fs_group_descriptors(sb);
114 mnt_drop_write_file(filp);
115 @@ -786,11 +808,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
118 err = ext4_resize_fs(sb, n_blocks_count);
119 - if (EXT4_SB(sb)->s_journal) {
120 - jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
121 - err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
122 - jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
124 + err2 = flush_fs_group_descriptors(sb);
127 mnt_drop_write_file(filp);
128 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
129 index 233fccf28025..b2bd2a31a931 100644
130 --- a/fs/ext4/super.c
131 +++ b/fs/ext4/super.c
132 @@ -1309,6 +1309,7 @@ enum {
133 Opt_inode_readahead_blks, Opt_journal_ioprio,
134 Opt_dioread_nolock, Opt_dioread_lock,
135 Opt_journal_nocleanup, Opt_journal_cleanup,
136 + Opt_journal_nolazy, Opt_journal_lazy,
137 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
138 Opt_max_dir_size_kb, Opt_nojournal_checksum,
140 @@ -1395,6 +1396,8 @@ static const match_table_t tokens = {
141 {Opt_test_dummy_encryption, "test_dummy_encryption"},
142 {Opt_journal_nocleanup, "journal_nocleanup"},
143 {Opt_journal_cleanup, "journal_cleanup"},
144 + {Opt_journal_lazy, "journal_lazy"},
145 + {Opt_journal_nolazy, "journal_nolazy"},
146 {Opt_removed, "check=none"}, /* mount option from ext2/3 */
147 {Opt_removed, "nocheck"}, /* mount option from ext2/3 */
148 {Opt_removed, "reservation"}, /* mount option from ext2/3 */
149 @@ -1603,6 +1606,8 @@ static const struct mount_opts {
150 {Opt_test_dummy_encryption, 0, MOPT_GTE0},
151 {Opt_journal_nocleanup, EXT4_MOUNT_JOURNAL_NO_CLEANUP, MOPT_SET},
152 {Opt_journal_cleanup, EXT4_MOUNT_JOURNAL_NO_CLEANUP, MOPT_CLEAR},
153 + {Opt_journal_lazy, EXT4_MOUNT_JOURNAL_LAZY, MOPT_SET},
154 + {Opt_journal_nolazy, EXT4_MOUNT_JOURNAL_LAZY, MOPT_CLEAR},
158 @@ -4337,6 +4342,10 @@ static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
159 journal->j_flags |= JBD2_NO_CLEANUP;
161 journal->j_flags &= ~JBD2_NO_CLEANUP;
162 + if (test_opt(sb, JOURNAL_LAZY))
163 + journal->j_flags |= JBD2_LAZY;
165 + journal->j_flags &= ~JBD2_LAZY;
166 write_unlock(&journal->j_state_lock);
169 @@ -4801,21 +4810,20 @@ static int ext4_freeze(struct super_block *sb)
170 journal = EXT4_SB(sb)->s_journal;
173 - /* Now we set up the journal barrier. */
174 - jbd2_journal_lock_updates(journal);
177 - * Don't clear the needs_recovery flag if we failed to
178 - * flush the journal.
179 + * Set the journal barrier, then flush the journal and
180 + * clear the needs_recovery flag if we are not in
183 - error = jbd2_journal_flush(journal);
186 + jbd2_journal_lock_updates(journal);
188 - /* Journal blocked and flushed, clear needs_recovery flag. */
189 + if (!test_opt(sb, JOURNAL_LAZY)) {
190 + error = jbd2_journal_flush(journal);
194 ext4_clear_feature_journal_needs_recovery(sb);
197 error = ext4_commit_super(sb, 1);
200 @@ -4833,7 +4841,7 @@ static int ext4_unfreeze(struct super_block *sb)
201 if ((sb->s_flags & MS_RDONLY) || ext4_forced_shutdown(EXT4_SB(sb)))
204 - if (EXT4_SB(sb)->s_journal) {
205 + if (EXT4_SB(sb)->s_journal && !test_opt(sb, JOURNAL_LAZY)) {
206 /* Reset the needs_recovery flag before the fs is unlocked. */
207 ext4_set_feature_journal_needs_recovery(sb);
209 @@ -5349,6 +5357,8 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,
210 * We don't need to lock updates but journal_flush() could
211 * otherwise be livelocked...
213 + if (test_opt(sb, JOURNAL_LAZY))
214 + return -EOPNOTSUPP;
215 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
216 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
217 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);