Skip to content

Commit f5113ef

Browse files
jankaratytso
authored andcommitted
jbd2: don't create journal_head for temporary journal buffers
When writing metadata to the journal, we create temporary buffer heads for that task. We also attach journal heads to these buffer heads but the only purpose of the journal heads is to keep buffers linked in transaction's BJ_IO list. We remove the need for journal heads by reusing buffer_head's b_assoc_buffers list for that purpose. Also since BJ_IO list is just a temporary list for transaction commit, we use a private list in jbd2_journal_commit_transaction() for that thus removing BJ_IO list from transaction completely. Reviewed-by: Zheng Liu <wenqing.lz@taobao.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 97a851e commit f5113ef

5 files changed

Lines changed: 56 additions & 92 deletions

File tree

‎fs/jbd2/checkpoint.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transact
690690
J_ASSERT(transaction->t_state == T_FINISHED);
691691
J_ASSERT(transaction->t_buffers == NULL);
692692
J_ASSERT(transaction->t_forget == NULL);
693-
J_ASSERT(transaction->t_iobuf_list == NULL);
694693
J_ASSERT(transaction->t_shadow_list == NULL);
695694
J_ASSERT(transaction->t_log_list == NULL);
696695
J_ASSERT(transaction->t_checkpoint_list == NULL);

‎fs/jbd2/commit.c‎

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
369369
{
370370
struct transaction_stats_s stats;
371371
transaction_t *commit_transaction;
372-
struct journal_head *jh, *new_jh, *descriptor;
372+
struct journal_head *jh, *descriptor;
373373
struct buffer_head **wbuf = journal->j_wbuf;
374374
int bufs;
375375
int flags;
@@ -393,6 +393,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
393393
tid_t first_tid;
394394
int update_tail;
395395
int csum_size = 0;
396+
LIST_HEAD(io_bufs);
396397

397398
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
398399
csum_size = sizeof(struct jbd2_journal_block_tail);
@@ -659,29 +660,22 @@ void jbd2_journal_commit_transaction(journal_t *journal)
659660

660661
/* Bump b_count to prevent truncate from stumbling over
661662
the shadowed buffer! @@@ This can go if we ever get
662-
rid of the BJ_IO/BJ_Shadow pairing of buffers. */
663+
rid of the shadow pairing of buffers. */
663664
atomic_inc(&jh2bh(jh)->b_count);
664665

665-
/* Make a temporary IO buffer with which to write it out
666-
(this will requeue both the metadata buffer and the
667-
temporary IO buffer). new_bh goes on BJ_IO*/
668-
669-
set_bit(BH_JWrite, &jh2bh(jh)->b_state);
670666
/*
671-
* akpm: jbd2_journal_write_metadata_buffer() sets
672-
* new_bh->b_transaction to commit_transaction.
673-
* We need to clean this up before we release new_bh
674-
* (which is of type BJ_IO)
667+
* Make a temporary IO buffer with which to write it out
668+
* (this will requeue the metadata buffer to BJ_Shadow).
675669
*/
670+
set_bit(BH_JWrite, &jh2bh(jh)->b_state);
676671
JBUFFER_TRACE(jh, "ph3: write metadata");
677672
flags = jbd2_journal_write_metadata_buffer(commit_transaction,
678-
jh, &new_jh, blocknr);
673+
jh, &wbuf[bufs], blocknr);
679674
if (flags < 0) {
680675
jbd2_journal_abort(journal, flags);
681676
continue;
682677
}
683-
set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
684-
wbuf[bufs++] = jh2bh(new_jh);
678+
jbd2_file_log_bh(&io_bufs, wbuf[bufs]);
685679

686680
/* Record the new block's tag in the current descriptor
687681
buffer */
@@ -695,10 +689,11 @@ void jbd2_journal_commit_transaction(journal_t *journal)
695689
tag = (journal_block_tag_t *) tagp;
696690
write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr);
697691
tag->t_flags = cpu_to_be16(tag_flag);
698-
jbd2_block_tag_csum_set(journal, tag, jh2bh(new_jh),
692+
jbd2_block_tag_csum_set(journal, tag, wbuf[bufs],
699693
commit_transaction->t_tid);
700694
tagp += tag_bytes;
701695
space_left -= tag_bytes;
696+
bufs++;
702697

703698
if (first_tag) {
704699
memcpy (tagp, journal->j_uuid, 16);
@@ -810,7 +805,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
810805
the log. Before we can commit it, wait for the IO so far to
811806
complete. Control buffers being written are on the
812807
transaction's t_log_list queue, and metadata buffers are on
813-
the t_iobuf_list queue.
808+
the io_bufs list.
814809
815810
Wait for the buffers in reverse order. That way we are
816811
less likely to be woken up until all IOs have completed, and
@@ -819,46 +814,31 @@ void jbd2_journal_commit_transaction(journal_t *journal)
819814

820815
jbd_debug(3, "JBD2: commit phase 3\n");
821816

822-
/*
823-
* akpm: these are BJ_IO, and j_list_lock is not needed.
824-
* See __journal_try_to_free_buffer.
825-
*/
826-
wait_for_iobuf:
827-
while (commit_transaction->t_iobuf_list != NULL) {
828-
struct buffer_head *bh;
817+
while (!list_empty(&io_bufs)) {
818+
struct buffer_head *bh = list_entry(io_bufs.prev,
819+
struct buffer_head,
820+
b_assoc_buffers);
829821

830-
jh = commit_transaction->t_iobuf_list->b_tprev;
831-
bh = jh2bh(jh);
832-
if (buffer_locked(bh)) {
833-
wait_on_buffer(bh);
834-
goto wait_for_iobuf;
835-
}
836-
if (cond_resched())
837-
goto wait_for_iobuf;
822+
wait_on_buffer(bh);
823+
cond_resched();
838824

839825
if (unlikely(!buffer_uptodate(bh)))
840826
err = -EIO;
841-
842-
clear_buffer_jwrite(bh);
843-
844-
JBUFFER_TRACE(jh, "ph4: unfile after journal write");
845-
jbd2_journal_unfile_buffer(journal, jh);
827+
jbd2_unfile_log_bh(bh);
846828

847829
/*
848-
* ->t_iobuf_list should contain only dummy buffer_heads
849-
* which were created by jbd2_journal_write_metadata_buffer().
830+
* The list contains temporary buffer heads created by
831+
* jbd2_journal_write_metadata_buffer().
850832
*/
851833
BUFFER_TRACE(bh, "dumping temporary bh");
852-
jbd2_journal_put_journal_head(jh);
853834
__brelse(bh);
854835
J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
855836
free_buffer_head(bh);
856837

857-
/* We also have to unlock and free the corresponding
858-
shadowed buffer */
838+
/* We also have to refile the corresponding shadowed buffer */
859839
jh = commit_transaction->t_shadow_list->b_tprev;
860840
bh = jh2bh(jh);
861-
clear_bit(BH_JWrite, &bh->b_state);
841+
clear_buffer_jwrite(bh);
862842
J_ASSERT_BH(bh, buffer_jbddirty(bh));
863843

864844
/* The metadata is now released for reuse, but we need
@@ -953,7 +933,6 @@ void jbd2_journal_commit_transaction(journal_t *journal)
953933
J_ASSERT(list_empty(&commit_transaction->t_inode_list));
954934
J_ASSERT(commit_transaction->t_buffers == NULL);
955935
J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
956-
J_ASSERT(commit_transaction->t_iobuf_list == NULL);
957936
J_ASSERT(commit_transaction->t_shadow_list == NULL);
958937
J_ASSERT(commit_transaction->t_log_list == NULL);
959938

‎fs/jbd2/journal.c‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,12 @@ static void journal_kill_thread(journal_t *journal)
310310
*
311311
* If the source buffer has already been modified by a new transaction
312312
* since we took the last commit snapshot, we use the frozen copy of
313-
* that data for IO. If we end up using the existing buffer_head's data
314-
* for the write, then we *have* to lock the buffer to prevent anyone
315-
* else from using and possibly modifying it while the IO is in
316-
* progress.
313+
* that data for IO. If we end up using the existing buffer_head's data
314+
* for the write, then we have to make sure nobody modifies it while the
315+
* IO is in progress. do_get_write_access() handles this.
317316
*
318-
* The function returns a pointer to the buffer_heads to be used for IO.
319-
*
320-
* We assume that the journal has already been locked in this function.
317+
* The function returns a pointer to the buffer_head to be used for IO.
318+
*
321319
*
322320
* Return value:
323321
* <0: Error
@@ -330,15 +328,14 @@ static void journal_kill_thread(journal_t *journal)
330328

331329
int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
332330
struct journal_head *jh_in,
333-
struct journal_head **jh_out,
334-
unsigned long long blocknr)
331+
struct buffer_head **bh_out,
332+
sector_t blocknr)
335333
{
336334
int need_copy_out = 0;
337335
int done_copy_out = 0;
338336
int do_escape = 0;
339337
char *mapped_data;
340338
struct buffer_head *new_bh;
341-
struct journal_head *new_jh;
342339
struct page *new_page;
343340
unsigned int new_offset;
344341
struct buffer_head *bh_in = jh2bh(jh_in);
@@ -368,14 +365,13 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
368365

369366
/* keep subsequent assertions sane */
370367
atomic_set(&new_bh->b_count, 1);
371-
new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
372368

369+
jbd_lock_bh_state(bh_in);
370+
repeat:
373371
/*
374372
* If a new transaction has already done a buffer copy-out, then
375373
* we use that version of the data for the commit.
376374
*/
377-
jbd_lock_bh_state(bh_in);
378-
repeat:
379375
if (jh_in->b_frozen_data) {
380376
done_copy_out = 1;
381377
new_page = virt_to_page(jh_in->b_frozen_data);
@@ -415,7 +411,7 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
415411
jbd_unlock_bh_state(bh_in);
416412
tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
417413
if (!tmp) {
418-
jbd2_journal_put_journal_head(new_jh);
414+
brelse(new_bh);
419415
return -ENOMEM;
420416
}
421417
jbd_lock_bh_state(bh_in);
@@ -426,7 +422,7 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
426422

427423
jh_in->b_frozen_data = tmp;
428424
mapped_data = kmap_atomic(new_page);
429-
memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
425+
memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
430426
kunmap_atomic(mapped_data);
431427

432428
new_page = virt_to_page(tmp);
@@ -452,14 +448,13 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
452448
}
453449

454450
set_bh_page(new_bh, new_page, new_offset);
455-
new_jh->b_transaction = NULL;
456-
new_bh->b_size = jh2bh(jh_in)->b_size;
457-
new_bh->b_bdev = transaction->t_journal->j_dev;
451+
new_bh->b_size = bh_in->b_size;
452+
new_bh->b_bdev = journal->j_dev;
458453
new_bh->b_blocknr = blocknr;
459454
set_buffer_mapped(new_bh);
460455
set_buffer_dirty(new_bh);
461456

462-
*jh_out = new_jh;
457+
*bh_out = new_bh;
463458

464459
/*
465460
* The to-be-written buffer needs to get moved to the io queue,
@@ -472,9 +467,6 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
472467
spin_unlock(&journal->j_list_lock);
473468
jbd_unlock_bh_state(bh_in);
474469

475-
JBUFFER_TRACE(new_jh, "file as BJ_IO");
476-
jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
477-
478470
return do_escape | (done_copy_out << 1);
479471
}
480472

‎fs/jbd2/transaction.c‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,10 +1601,10 @@ __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
16011601
* Remove a buffer from the appropriate transaction list.
16021602
*
16031603
* Note that this function can *change* the value of
1604-
* bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1605-
* t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1606-
* of these pointers, it could go bad. Generally the caller needs to re-read
1607-
* the pointer from the transaction_t.
1604+
* bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
1605+
* t_reserved_list. If the caller is holding onto a copy of one of these
1606+
* pointers, it could go bad. Generally the caller needs to re-read the
1607+
* pointer from the transaction_t.
16081608
*
16091609
* Called under j_list_lock.
16101610
*/
@@ -1634,9 +1634,6 @@ static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
16341634
case BJ_Forget:
16351635
list = &transaction->t_forget;
16361636
break;
1637-
case BJ_IO:
1638-
list = &transaction->t_iobuf_list;
1639-
break;
16401637
case BJ_Shadow:
16411638
list = &transaction->t_shadow_list;
16421639
break;
@@ -2148,9 +2145,6 @@ void __jbd2_journal_file_buffer(struct journal_head *jh,
21482145
case BJ_Forget:
21492146
list = &transaction->t_forget;
21502147
break;
2151-
case BJ_IO:
2152-
list = &transaction->t_iobuf_list;
2153-
break;
21542148
case BJ_Shadow:
21552149
list = &transaction->t_shadow_list;
21562150
break;

‎include/linux/jbd2.h‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,6 @@ struct transaction_s
523523
*/
524524
struct journal_head *t_checkpoint_io_list;
525525

526-
/*
527-
* Doubly-linked circular list of temporary buffers currently undergoing
528-
* IO in the log [j_list_lock]
529-
*/
530-
struct journal_head *t_iobuf_list;
531-
532526
/*
533527
* Doubly-linked circular list of metadata buffers being shadowed by log
534528
* IO. The IO buffers on the iobuf list and the shadow buffers on this
@@ -991,6 +985,14 @@ extern void __jbd2_journal_file_buffer(struct journal_head *, transaction_t *, i
991985
extern void __journal_free_buffer(struct journal_head *bh);
992986
extern void jbd2_journal_file_buffer(struct journal_head *, transaction_t *, int);
993987
extern void __journal_clean_data_list(transaction_t *transaction);
988+
static inline void jbd2_file_log_bh(struct list_head *head, struct buffer_head *bh)
989+
{
990+
list_add_tail(&bh->b_assoc_buffers, head);
991+
}
992+
static inline void jbd2_unfile_log_bh(struct buffer_head *bh)
993+
{
994+
list_del_init(&bh->b_assoc_buffers);
995+
}
994996

995997
/* Log buffer allocation */
996998
extern struct journal_head * jbd2_journal_get_descriptor_buffer(journal_t *);
@@ -1039,11 +1041,10 @@ extern void jbd2_buffer_abort_trigger(struct journal_head *jh,
10391041
struct jbd2_buffer_trigger_type *triggers);
10401042

10411043
/* Buffer IO */
1042-
extern int
1043-
jbd2_journal_write_metadata_buffer(transaction_t *transaction,
1044-
struct journal_head *jh_in,
1045-
struct journal_head **jh_out,
1046-
unsigned long long blocknr);
1044+
extern int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
1045+
struct journal_head *jh_in,
1046+
struct buffer_head **bh_out,
1047+
sector_t blocknr);
10471048

10481049
/* Transaction locking */
10491050
extern void __wait_on_journal (journal_t *);
@@ -1286,11 +1287,10 @@ static inline int jbd_space_needed(journal_t *journal)
12861287
#define BJ_None 0 /* Not journaled */
12871288
#define BJ_Metadata 1 /* Normal journaled metadata */
12881289
#define BJ_Forget 2 /* Buffer superseded by this transaction */
1289-
#define BJ_IO 3 /* Buffer is for temporary IO use */
1290-
#define BJ_Shadow 4 /* Buffer contents being shadowed to the log */
1291-
#define BJ_LogCtl 5 /* Buffer contains log descriptors */
1292-
#define BJ_Reserved 6 /* Buffer is reserved for access by journal */
1293-
#define BJ_Types 7
1290+
#define BJ_Shadow 3 /* Buffer contents being shadowed to the log */
1291+
#define BJ_LogCtl 4 /* Buffer contains log descriptors */
1292+
#define BJ_Reserved 5 /* Buffer is reserved for access by journal */
1293+
#define BJ_Types 6
12941294

12951295
extern int jbd_blocks_per_page(struct inode *inode);
12961296

0 commit comments

Comments
 (0)