Make journal_commit_transaction() more understandable
PATCH 1/1 Make journal_commit_transaction() more understandable
journal_commit_transaction() is still 650+ lines long and contains 16 local variables. By moving phase 3 into its own function, we reduce its length by 150+ lines and reduce it to 5 local variables.
commit.c | 367 +++++++++++++++++++++++++++++++++------------------------------ 1 files changed, 194 insertions(+), 173 deletions(-)
Index: linux-2.6/fs/jbd/commit.c
===================================================================
RCS file: /var/lib/cvs/linux-2.6/fs/jbd/commit.c,v retrieving revision 1.14
fs/jbd/commit.c +194 -173
@@ -93,6 +93,189 @@ static int inverted_lock(journal_t *jourreturn 1 ; } +/* +* Way to go: we have now written out all of the data for a transaction! +* Now comes the tricky part: we need to write out metadata. Loop over the +* transaction's entire buffer list: +*/ +static int journal_write_metadata (journal_t *journal, +transaction_t *commit_transaction) +{ +char *tagp =NULL ; +struct journal_head *descriptor =NULL ; +int flags; +unsigned long blocknr; +journal_header_t *header; +journal_block_tag_t *tag =NULL ; +int space_left =0 ; +int first_tag =0 ; +int tag_flag; +int i; +int err =0 ; +int bufs =0 ; + +struct buffer_head **wbuf = journal->j_wbuf; + + commit_transaction->t_state =T_COMMIT ; + +while (commit_transaction->t_buffers) { + +/* Find the next buffer to be journaled... */ + +struct journal_head *jh, *new_jh; + jh = commit_transaction->t_buffers; + +/* If we're in abort mode, we just un-journal the buffer and +release it for background writing. */ + +if (is_journal_aborted (journal)) { +JBUFFER_TRACE (jh,"journal is aborting: refile" ); +journal_refile_buffer (journal, jh); +/* If that was the last one, we need to clean up +* any descriptor buffers which may have been +* already allocated, even if we are now +* aborting. */ +if (!commit_transaction->t_buffers) + goto start_journal_io; +continue ; + } + +/* Make sure we have a descriptor block in which to +record the metadata buffer. */ + +if (!descriptor) { +struct buffer_head *bh; + +J_ASSERT (bufs ==0 ); + +jbd_debug (4 ,"JBD: get descriptor\n" ); + + descriptor =journal_get_descriptor_buffer (journal); +if (!descriptor) { +__journal_abort_hard (journal); +continue ; + } + + bh =jh2bh (descriptor); +jbd_debug (4 ,"JBD: got buffer %llu (%p)\n" , + (unsigned long long )bh->b_blocknr, bh->b_data); + header = (journal_header_t *)&bh->b_data[0 ]; + header->h_magic =cpu_to_be32 (JFS_MAGIC_NUMBER ); + header->h_blocktype =cpu_to_be32 (JFS_DESCRIPTOR_BLOCK ); + header->h_sequence =cpu_to_be32 (commit_transaction->t_tid); + + tagp = &bh->b_data[sizeof (journal_header_t)]; + space_left = bh->b_size -sizeof (journal_header_t); + first_tag =1 ; +set_buffer_jwrite (bh); +set_buffer_dirty (bh); + wbuf[bufs++] = bh; + +/* Record it so that we can wait for IO +completion later */ +BUFFER_TRACE (bh,"ph3: file as descriptor" ); +journal_file_buffer (descriptor, commit_transaction, + BJ_LogCtl); + } + +/* Where is the buffer to be written? */ + + err =journal_next_log_block (journal, &blocknr); +/* If the block mapping failed, just abandon the buffer +and repeat this loop: we'll fall into the +refile-on-abort condition above. */ +if (err) { +__journal_abort_hard (journal); +continue ; + } + +/* +* start_this_handle() uses t_outstanding_credits to determine +* the free space in the log, but this counter is changed +* by journal_next_log_block() also. +*/ + commit_transaction->t_outstanding_credits--; + +/* Bump b_count to prevent truncate from stumbling over +the shadowed buffer! @@@ This can go if we ever get +rid of the BJ_IO/BJ_Shadow pairing of buffers. */ +atomic_inc (&jh2bh (jh)->b_count); + +/* Make a temporary IO buffer with which to write it out +(this will requeue both the metadata buffer and the +temporary IO buffer). new_bh goes on BJ_IO*/ + +set_bit (BH_JWrite, &jh2bh (jh)->b_state); +/* +* akpm: journal_write_metadata_buffer() sets +* new_bh->b_transaction to commit_transaction. +* We need to clean this up before we release new_bh +* (which is of type BJ_IO) +*/ +JBUFFER_TRACE (jh,"ph3: write metadata" ); + flags =journal_write_metadata_buffer (commit_transaction, + jh, &new_jh, blocknr); +set_bit (BH_JWrite, &jh2bh (new_jh)->b_state); + wbuf[bufs++] =jh2bh (new_jh); + +/* Record the new block's tag in the current descriptor +buffer */ + + tag_flag =0 ; +if (flags &1 ) + tag_flag |=JFS_FLAG_ESCAPE ; +if (!first_tag) + tag_flag |=JFS_FLAG_SAME_UUID ; + + tag = (journal_block_tag_t *) tagp; + tag->t_blocknr =cpu_to_be32 (jh2bh (jh)->b_blocknr); + tag->t_flags =cpu_to_be32 (tag_flag); + tagp +=sizeof (journal_block_tag_t); + space_left -=sizeof (journal_block_tag_t); + +if (first_tag) { +memcpy (tagp, journal->j_uuid,16 ); + tagp +=16 ; + space_left -=16 ; + first_tag =0 ; + } + +/* If there's no more to do, or if the descriptor is full, +let the IO rip! */ + +if (bufs == journal->j_wbufsize || + commit_transaction->t_buffers ==NULL || + space_left <sizeof (journal_block_tag_t) +16 ) { + +jbd_debug (4 ,"JBD: Submit %d IOs\n" , bufs); + +/* Write an end-of-descriptor marker before +submitting the IOs. "tag" still points to +the last tag we set up. */ + + tag->t_flags |=cpu_to_be32 (JFS_FLAG_LAST_TAG ); + +start_journal_io: +for (i =0 ; i < bufs; i++) { +struct buffer_head *bh = wbuf[i]; +lock_buffer (bh); +clear_buffer_dirty (bh); +set_buffer_uptodate (bh); + bh->b_end_io = journal_end_buffer_io_sync; +submit_bh (WRITE , bh); + } +cond_resched (); + +/* Force a new descriptor to be generated next +time round the loop. */ + descriptor =NULL ; + bufs =0 ; + } + } + +return err; +} +/* Done it all: now write the commit record. We should have * cleaned up our previous buffers by now, so if we are in abort * mode we can now just skip the rest of the journal write
@@ -169,19 +352,9 @@ static int journal_write_commit_record(jvoid journal_commit_transaction (journal_t *journal) {transaction_t *commit_transaction; -struct journal_head *jh, *new_jh, *descriptor;struct buffer_head **wbuf = journal->j_wbuf;int bufs; -int flags; -int err; -unsigned long blocknr; -char *tagp =NULL ; -journal_header_t *header; -journal_block_tag_t *tag =NULL ; -int space_left =0 ; -int first_tag =0 ; -int tag_flag; -int i; +int err, err2;/* * First job: lock down the current transaction and wait for
@@ -251,6 +424,7 @@ void journal_commit_transaction(journal_* buffer are perfectly permissable. */ while (commit_transaction->t_reserved_list) { +struct journal_head *jh; jh = commit_transaction->t_reserved_list;JBUFFER_TRACE (jh,"reserved, unused: refile" );/*
@@ -301,6 +475,7 @@ void journal_commit_transaction(journal_*/ spin_lock (&journal->j_list_lock); if (commit_transaction->t_buffers) { + struct journal_head *jh, *new_jh; new_jh = jh = commit_transaction->t_buffers->b_tnext; do { J_ASSERT_JH(new_jh, new_jh->b_modified == 1 ||
@@ -333,6 +508,7 @@ write_out_data:while (commit_transaction->t_sync_datalist) {struct buffer_head *bh; +struct journal_head *jh; jh = commit_transaction->t_sync_datalist; commit_transaction->t_sync_datalist = jh->b_tnext;
@@ -391,6 +567,7 @@ write_out_data:*/ while (commit_transaction->t_locked_list) {struct buffer_head *bh; +struct journal_head *jh; jh = commit_transaction->t_locked_list->b_tprev; bh =jh2bh (jh);
@@ -437,168 +614,9 @@ write_out_data:jbd_debug (3 ,"JBD: commit phase 3\n" ); -/* -* Way to go: we have now written out all of the data for a -* transaction! Now comes the tricky part: we need to write out -* metadata. Loop over the transaction's entire buffer list: -*/ - commit_transaction->t_state =T_COMMIT ; - - descriptor =NULL ; - bufs =0 ; -while (commit_transaction->t_buffers) { - -/* Find the next buffer to be journaled... */ - - jh = commit_transaction->t_buffers; - -/* If we're in abort mode, we just un-journal the buffer and -release it for background writing. */ - -if (is_journal_aborted (journal)) { -JBUFFER_TRACE (jh,"journal is aborting: refile" ); -journal_refile_buffer (journal, jh); -/* If that was the last one, we need to clean up -* any descriptor buffers which may have been -* already allocated, even if we are now -* aborting. */ -if (!commit_transaction->t_buffers) - goto start_journal_io; -continue ; - } - -/* Make sure we have a descriptor block in which to -record the metadata buffer. */ - -if (!descriptor) { -struct buffer_head *bh; - -J_ASSERT (bufs ==0 ); - -jbd_debug (4 ,"JBD: get descriptor\n" ); - - descriptor =journal_get_descriptor_buffer (journal); -if (!descriptor) { -__journal_abort_hard (journal); -continue ; - } - - bh =jh2bh (descriptor); -jbd_debug (4 ,"JBD: got buffer %llu (%p)\n" , - (unsigned long long )bh->b_blocknr, bh->b_data); - header = (journal_header_t *)&bh->b_data[0 ]; - header->h_magic =cpu_to_be32 (JFS_MAGIC_NUMBER ); - header->h_blocktype =cpu_to_be32 (JFS_DESCRIPTOR_BLOCK ); - header->h_sequence =cpu_to_be32 (commit_transaction->t_tid); - - tagp = &bh->b_data[sizeof (journal_header_t)]; - space_left = bh->b_size -sizeof (journal_header_t); - first_tag =1 ; -set_buffer_jwrite (bh); -set_buffer_dirty (bh); - wbuf[bufs++] = bh; - -/* Record it so that we can wait for IO -completion later */ -BUFFER_TRACE (bh,"ph3: file as descriptor" ); -journal_file_buffer (descriptor, commit_transaction, - BJ_LogCtl); - } - -/* Where is the buffer to be written? */ - - err =journal_next_log_block (journal, &blocknr); -/* If the block mapping failed, just abandon the buffer -and repeat this loop: we'll fall into the -refile-on-abort condition above. */ -if (err) { -__journal_abort_hard (journal); -continue ; - } - -/* -* start_this_handle() uses t_outstanding_credits to determine -* the free space in the log, but this counter is changed -* by journal_next_log_block() also. -*/ - commit_transaction->t_outstanding_credits--; - -/* Bump b_count to prevent truncate from stumbling over -the shadowed buffer! @@@ This can go if we ever get -rid of the BJ_IO/BJ_Shadow pairing of buffers. */ -atomic_inc (&jh2bh (jh)->b_count); - -/* Make a temporary IO buffer with which to write it out -(this will requeue both the metadata buffer and the -temporary IO buffer). new_bh goes on BJ_IO*/ - -set_bit (BH_JWrite, &jh2bh (jh)->b_state); -/* -* akpm: journal_write_metadata_buffer() sets -* new_bh->b_transaction to commit_transaction. -* We need to clean this up before we release new_bh -* (which is of type BJ_IO) -*/ -JBUFFER_TRACE (jh,"ph3: write metadata" ); - flags =journal_write_metadata_buffer (commit_transaction, - jh, &new_jh, blocknr); -set_bit (BH_JWrite, &jh2bh (new_jh)->b_state); - wbuf[bufs++] =jh2bh (new_jh); - -/* Record the new block's tag in the current descriptor -buffer */ - - tag_flag =0 ; -if (flags &1 ) - tag_flag |=JFS_FLAG_ESCAPE ; -if (!first_tag) - tag_flag |=JFS_FLAG_SAME_UUID ; - - tag = (journal_block_tag_t *) tagp; - tag->t_blocknr =cpu_to_be32 (jh2bh (jh)->b_blocknr); - tag->t_flags =cpu_to_be32 (tag_flag); - tagp +=sizeof (journal_block_tag_t); - space_left -=sizeof (journal_block_tag_t); - -if (first_tag) { -memcpy (tagp, journal->j_uuid,16 ); - tagp +=16 ; - space_left -=16 ; - first_tag =0 ; - } - -/* If there's no more to do, or if the descriptor is full, -let the IO rip! */ - -if (bufs == journal->j_wbufsize || - commit_transaction->t_buffers ==NULL || - space_left <sizeof (journal_block_tag_t) +16 ) { - -jbd_debug (4 ,"JBD: Submit %d IOs\n" , bufs); - -/* Write an end-of-descriptor marker before -submitting the IOs. "tag" still points to -the last tag we set up. */ - - tag->t_flags |=cpu_to_be32 (JFS_FLAG_LAST_TAG ); - -start_journal_io: -for (i =0 ; i < bufs; i++) { -struct buffer_head *bh = wbuf[i]; -lock_buffer (bh); -clear_buffer_dirty (bh); -set_buffer_uptodate (bh); - bh->b_end_io = journal_end_buffer_io_sync; -submit_bh (WRITE , bh); - } -cond_resched (); - -/* Force a new descriptor to be generated next -time round the loop. */ - descriptor =NULL ; - bufs =0 ; - } - } + err2 =journal_write_metadata (journal, commit_transaction); +if (err2) + err = err2;/* Lo and behold: we have just managed to send a transaction to the log. Before we can commit it, wait for the IO so far to
@@ -620,6 +638,7 @@ start_journal_io: wait_for_iobuf:while (commit_transaction->t_iobuf_list !=NULL ) {struct buffer_head *bh; +struct journal_head *jh; jh = commit_transaction->t_iobuf_list->b_tprev; bh =jh2bh (jh);
@@ -676,6 +695,7 @@ wait_for_iobuf: wait_for_ctlbuf:while (commit_transaction->t_log_list !=NULL ) {struct buffer_head *bh; +struct journal_head *jh; jh = commit_transaction->t_log_list->b_tprev; bh =jh2bh (jh);
@@ -723,6 +743,7 @@ restart_loop:while (commit_transaction->t_forget) {transaction_t *cp_transaction;struct buffer_head *bh; +struct journal_head *jh; jh = commit_transaction->t_forget; bh =jh2bh (jh);
Signature · 6 lines
"Next the statesmen will invent cheap lies, putting the blame upon the nation that is attacked, and every man will be glad of those conscience-soothing falsities, and will diligently study them, and refuse to examine any refutations of them; and thus he will by and by convince himself that the war is just, and will thank God for the better sleep he enjoys after this process of grotesque self-deception." -- Mark Twain
Quoted 21 lines · Matthew Wilcox
journal_commit_transaction()is still 650+ lines long and contains 16 local variables. By moving phase 3 into its own function, we reduce its length by 150+ lines and reduce it to 5 local variables.fs/jbd/commit.c +2
-0@@ -251,6 +424,7 @@ void journal_commit_transaction(journal_* buffer are perfectly permissable. */ while (commit_transaction->t_reserved_list) { +struct journal_head *jh; jh = commit_transaction->t_reserved_list;JBUFFER_TRACE (jh,"reserved, unused: refile" );/* @@ -301,6 +475,7 @@ void journal_commit_transaction(journal_*/ spin_lock (&journal->j_list_lock); if (commit_transaction->t_buffers) { + struct journal_head *jh, *new_jh; new_jh = jh = commit_transaction->t_buffers->b_tnext; do { J_ASSERT_JH(new_jh, new_jh->b_modified == 1 ||
fs/jbd/commit.c write_out_data: · line 514
Quoted 7 lines
fs/jbd/commit.c +1
-0@@ -333,6 +508,7 @@ write_out_data:while (commit_transaction->t_sync_datalist) {struct buffer_head *bh; +struct journal_head *jh; jh = commit_transaction->t_sync_datalist; commit_transaction->t_sync_datalist = jh->b_tnext;
The one comment I have here is that in the past GCC has been bad at allocating local variables on the stack, and would reserve the sum of all local variables instead of the union. This would unnecessarily bloat code that had a lot of local variables defined in only a subset of the function. Could you verify with checkstack that this doesn't hit here? Otherwise we need to move the common journal_head and buffer_head allocations up to the main function declarations.
Cheers, Andreas
--
Andreas Dilger
Principal Software Engineer
Cluster File Systems, Inc.
Hi,
Quoted 4 lines · Andreas Dilger
Quoted 3 lines · Matthew Wilcox
journal_commit_transaction()is still 650+ lines long and contains 16 local variables. By moving phase 3 into its own function, we reduce its length by 150+ lines and reduce it to 5 local variables.
Thanks.
Quoted 6 lines
The one comment I have here is that in the past GCC has been bad at allocating local variables on the stack, and would reserve the sum of all local variables instead of the union. This would unnecessarily bloat code that had a lot of local variables defined in only a subset of the function. Could you verify with checkstack that this doesn't hit here?
Well, I remember one gcc problem where a gcc errata --- not even a major version change --- suddenly started reserving massively more space not just for the function itself, but for everything inlined into it too. It seems to be highly gcc version-specific, and we still support some rather old gccs.
--Stephen
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf