Skip to content

Commit 41617e1

Browse files
jankaratytso
authored andcommitted
jbd2: add support for avoiding data writes during transaction commits
Currently when filesystem needs to make sure data is on permanent storage before committing a transaction it adds inode to transaction's inode list. During transaction commit, jbd2 writes back all dirty buffers that have allocated underlying blocks and waits for the IO to finish. However when doing writeback for delayed allocated data, we allocate blocks and immediately submit the data. Thus asking jbd2 to write dirty pages just unnecessarily adds more work to jbd2 possibly writing back other redirtied blocks. Add support to jbd2 to allow filesystem to ask jbd2 to only wait for outstanding data writes before committing a transaction and thus avoid unnecessary writes. Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 3957ef5 commit 41617e1

6 files changed

Lines changed: 38 additions & 9 deletions

File tree

‎fs/ext4/ext4_jbd2.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ static inline int ext4_journal_force_commit(journal_t *journal)
362362
static inline int ext4_jbd2_file_inode(handle_t *handle, struct inode *inode)
363363
{
364364
if (ext4_handle_valid(handle))
365-
return jbd2_journal_file_inode(handle, EXT4_I(inode)->jinode);
365+
return jbd2_journal_inode_add_write(handle,
366+
EXT4_I(inode)->jinode);
366367
return 0;
367368
}
368369

‎fs/jbd2/commit.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ static int journal_submit_data_buffers(journal_t *journal,
219219

220220
spin_lock(&journal->j_list_lock);
221221
list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
222+
if (!(jinode->i_flags & JI_WRITE_DATA))
223+
continue;
222224
mapping = jinode->i_vfs_inode->i_mapping;
223225
jinode->i_flags |= JI_COMMIT_RUNNING;
224226
spin_unlock(&journal->j_list_lock);
@@ -256,6 +258,8 @@ static int journal_finish_inode_data_buffers(journal_t *journal,
256258
/* For locking, see the comment in journal_submit_data_buffers() */
257259
spin_lock(&journal->j_list_lock);
258260
list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
261+
if (!(jinode->i_flags & JI_WAIT_DATA))
262+
continue;
259263
jinode->i_flags |= JI_COMMIT_RUNNING;
260264
spin_unlock(&journal->j_list_lock);
261265
err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping);

‎fs/jbd2/journal.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
9494
EXPORT_SYMBOL(jbd2_journal_invalidatepage);
9595
EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
9696
EXPORT_SYMBOL(jbd2_journal_force_commit);
97-
EXPORT_SYMBOL(jbd2_journal_file_inode);
97+
EXPORT_SYMBOL(jbd2_journal_inode_add_write);
98+
EXPORT_SYMBOL(jbd2_journal_inode_add_wait);
9899
EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
99100
EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
100101
EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);

‎fs/jbd2/transaction.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,8 @@ void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
24622462
/*
24632463
* File inode in the inode list of the handle's transaction
24642464
*/
2465-
int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2465+
static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
2466+
unsigned long flags)
24662467
{
24672468
transaction_t *transaction = handle->h_transaction;
24682469
journal_t *journal;
@@ -2487,12 +2488,14 @@ int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
24872488
* and if jinode->i_next_transaction == transaction, commit code
24882489
* will only file the inode where we want it.
24892490
*/
2490-
if (jinode->i_transaction == transaction ||
2491-
jinode->i_next_transaction == transaction)
2491+
if ((jinode->i_transaction == transaction ||
2492+
jinode->i_next_transaction == transaction) &&
2493+
(jinode->i_flags & flags) == flags)
24922494
return 0;
24932495

24942496
spin_lock(&journal->j_list_lock);
2495-
2497+
jinode->i_flags |= flags;
2498+
/* Is inode already attached where we need it? */
24962499
if (jinode->i_transaction == transaction ||
24972500
jinode->i_next_transaction == transaction)
24982501
goto done;
@@ -2523,6 +2526,17 @@ int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
25232526
return 0;
25242527
}
25252528

2529+
int jbd2_journal_inode_add_write(handle_t *handle, struct jbd2_inode *jinode)
2530+
{
2531+
return jbd2_journal_file_inode(handle, jinode,
2532+
JI_WRITE_DATA | JI_WAIT_DATA);
2533+
}
2534+
2535+
int jbd2_journal_inode_add_wait(handle_t *handle, struct jbd2_inode *jinode)
2536+
{
2537+
return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA);
2538+
}
2539+
25262540
/*
25272541
* File truncate and transaction commit interact with each other in a
25282542
* non-trivial way. If a transaction writing data block A is

‎fs/ocfs2/journal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ static inline int ocfs2_calc_tree_trunc_credits(struct super_block *sb,
619619

620620
static inline int ocfs2_jbd2_file_inode(handle_t *handle, struct inode *inode)
621621
{
622-
return jbd2_journal_file_inode(handle, &OCFS2_I(inode)->ip_jinode);
622+
return jbd2_journal_inode_add_write(handle, &OCFS2_I(inode)->ip_jinode);
623623
}
624624

625625
static inline int ocfs2_begin_ordered_truncate(struct inode *inode,

‎include/linux/jbd2.h‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,19 @@ static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
403403

404404
/* Flags in jbd_inode->i_flags */
405405
#define __JI_COMMIT_RUNNING 0
406-
/* Commit of the inode data in progress. We use this flag to protect us from
406+
#define __JI_WRITE_DATA 1
407+
#define __JI_WAIT_DATA 2
408+
409+
/*
410+
* Commit of the inode data in progress. We use this flag to protect us from
407411
* concurrent deletion of inode. We cannot use reference to inode for this
408412
* since we cannot afford doing last iput() on behalf of kjournald
409413
*/
410414
#define JI_COMMIT_RUNNING (1 << __JI_COMMIT_RUNNING)
415+
/* Write allocated dirty buffers in this inode before commit */
416+
#define JI_WRITE_DATA (1 << __JI_WRITE_DATA)
417+
/* Wait for outstanding data writes for this inode before commit */
418+
#define JI_WAIT_DATA (1 << __JI_WAIT_DATA)
411419

412420
/**
413421
* struct jbd_inode is the structure linking inodes in ordered mode
@@ -1270,7 +1278,8 @@ extern int jbd2_journal_clear_err (journal_t *);
12701278
extern int jbd2_journal_bmap(journal_t *, unsigned long, unsigned long long *);
12711279
extern int jbd2_journal_force_commit(journal_t *);
12721280
extern int jbd2_journal_force_commit_nested(journal_t *);
1273-
extern int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *inode);
1281+
extern int jbd2_journal_inode_add_write(handle_t *handle, struct jbd2_inode *inode);
1282+
extern int jbd2_journal_inode_add_wait(handle_t *handle, struct jbd2_inode *inode);
12741283
extern int jbd2_journal_begin_ordered_truncate(journal_t *journal,
12751284
struct jbd2_inode *inode, loff_t new_size);
12761285
extern void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode);

0 commit comments

Comments
 (0)