@@ -923,8 +923,22 @@ static int journal_reset(journal_t *journal)
923923
924924 journal -> j_max_transaction_buffers = journal -> j_maxlen / 4 ;
925925
926- /* Add the dynamic fields and write it to disk. */
927- journal_update_superblock (journal , 1 );
926+ /*
927+ * As a special case, if the on-disk copy is already marked as needing
928+ * no recovery (s_start == 0), then we can safely defer the superblock
929+ * update until the next commit by setting JFS_FLUSHED. This avoids
930+ * attempting a write to a potential-readonly device.
931+ */
932+ if (sb -> s_start == 0 ) {
933+ jbd_debug (1 ,"JBD: Skipping superblock update on recovered sb "
934+ "(start %u, seq %d, errno %d)\n" ,
935+ journal -> j_tail , journal -> j_tail_sequence ,
936+ journal -> j_errno );
937+ journal -> j_flags |= JFS_FLUSHED ;
938+ } else {
939+ /* Add the dynamic fields and write it to disk. */
940+ journal_update_sb_log_tail (journal );
941+ }
928942 return journal_start_thread (journal );
929943}
930944
@@ -1001,35 +1015,11 @@ int journal_create(journal_t *journal)
10011015 return journal_reset (journal );
10021016}
10031017
1004- /**
1005- * void journal_update_superblock() - Update journal sb on disk.
1006- * @journal: The journal to update.
1007- * @wait: Set to '0' if you don't want to wait for IO completion.
1008- *
1009- * Update a journal's dynamic superblock fields and write it to disk,
1010- * optionally waiting for the IO to complete.
1011- */
1012- void journal_update_superblock (journal_t * journal , int wait )
1018+ static void journal_write_superblock (journal_t * journal )
10131019{
1014- journal_superblock_t * sb = journal -> j_superblock ;
10151020 struct buffer_head * bh = journal -> j_sb_buffer ;
10161021
1017- /*
1018- * As a special case, if the on-disk copy is already marked as needing
1019- * no recovery (s_start == 0) and there are no outstanding transactions
1020- * in the filesystem, then we can safely defer the superblock update
1021- * until the next commit by setting JFS_FLUSHED. This avoids
1022- * attempting a write to a potential-readonly device.
1023- */
1024- if (sb -> s_start == 0 && journal -> j_tail_sequence ==
1025- journal -> j_transaction_sequence ) {
1026- jbd_debug (1 ,"JBD: Skipping superblock update on recovered sb "
1027- "(start %u, seq %d, errno %d)\n" ,
1028- journal -> j_tail , journal -> j_tail_sequence ,
1029- journal -> j_errno );
1030- goto out ;
1031- }
1032-
1022+ trace_journal_write_superblock (journal );
10331023 if (buffer_write_io_error (bh )) {
10341024 char b [BDEVNAME_SIZE ];
10351025 /*
@@ -1047,44 +1037,94 @@ void journal_update_superblock(journal_t *journal, int wait)
10471037 set_buffer_uptodate (bh );
10481038 }
10491039
1040+ BUFFER_TRACE (bh , "marking dirty" );
1041+ mark_buffer_dirty (bh );
1042+ sync_dirty_buffer (bh );
1043+ if (buffer_write_io_error (bh )) {
1044+ char b [BDEVNAME_SIZE ];
1045+ printk (KERN_ERR "JBD: I/O error detected "
1046+ "when updating journal superblock for %s.\n" ,
1047+ journal_dev_name (journal , b ));
1048+ clear_buffer_write_io_error (bh );
1049+ set_buffer_uptodate (bh );
1050+ }
1051+ }
1052+
1053+ /**
1054+ * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1055+ * @journal: The journal to update.
1056+ *
1057+ * Update a journal's superblock information about log tail and write it to
1058+ * disk, waiting for the IO to complete.
1059+ */
1060+ void journal_update_sb_log_tail (journal_t * journal )
1061+ {
1062+ journal_superblock_t * sb = journal -> j_superblock ;
1063+
10501064 spin_lock (& journal -> j_state_lock );
10511065 jbd_debug (1 ,"JBD: updating superblock (start %u, seq %d, errno %d)\n" ,
10521066 journal -> j_tail , journal -> j_tail_sequence , journal -> j_errno );
10531067
10541068 sb -> s_sequence = cpu_to_be32 (journal -> j_tail_sequence );
10551069 sb -> s_start = cpu_to_be32 (journal -> j_tail );
1056- sb -> s_errno = cpu_to_be32 (journal -> j_errno );
10571070 spin_unlock (& journal -> j_state_lock );
10581071
1059- BUFFER_TRACE (bh , "marking dirty" );
1060- mark_buffer_dirty (bh );
1061- if (wait ) {
1062- sync_dirty_buffer (bh );
1063- if (buffer_write_io_error (bh )) {
1064- char b [BDEVNAME_SIZE ];
1065- printk (KERN_ERR "JBD: I/O error detected "
1066- "when updating journal superblock for %s.\n" ,
1067- journal_dev_name (journal , b ));
1068- clear_buffer_write_io_error (bh );
1069- set_buffer_uptodate (bh );
1070- }
1071- } else
1072- write_dirty_buffer (bh , WRITE );
1072+ journal_write_superblock (journal );
10731073
1074- trace_jbd_update_superblock_end (journal , wait );
1075- out :
1076- /* If we have just flushed the log (by marking s_start==0), then
1077- * any future commit will have to be careful to update the
1078- * superblock again to re-record the true start of the log. */
1074+ /* Log is no longer empty */
1075+ spin_lock (& journal -> j_state_lock );
1076+ WARN_ON (!sb -> s_sequence );
1077+ journal -> j_flags &= ~JFS_FLUSHED ;
1078+ spin_unlock (& journal -> j_state_lock );
1079+ }
1080+
1081+ /**
1082+ * mark_journal_empty() - Mark on disk journal as empty.
1083+ * @journal: The journal to update.
1084+ *
1085+ * Update a journal's dynamic superblock fields to show that journal is empty.
1086+ * Write updated superblock to disk waiting for IO to complete.
1087+ */
1088+ static void mark_journal_empty (journal_t * journal )
1089+ {
1090+ journal_superblock_t * sb = journal -> j_superblock ;
10791091
10801092 spin_lock (& journal -> j_state_lock );
1081- if (sb -> s_start )
1082- journal -> j_flags &= ~JFS_FLUSHED ;
1083- else
1084- journal -> j_flags |= JFS_FLUSHED ;
1093+ jbd_debug (1 , "JBD: Marking journal as empty (seq %d)\n" ,
1094+ journal -> j_tail_sequence );
1095+
1096+ sb -> s_sequence = cpu_to_be32 (journal -> j_tail_sequence );
1097+ sb -> s_start = cpu_to_be32 (0 );
1098+ spin_unlock (& journal -> j_state_lock );
1099+
1100+ journal_write_superblock (journal );
1101+
1102+ spin_lock (& journal -> j_state_lock );
1103+ /* Log is empty */
1104+ journal -> j_flags |= JFS_FLUSHED ;
10851105 spin_unlock (& journal -> j_state_lock );
10861106}
10871107
1108+ /**
1109+ * journal_update_sb_errno() - Update error in the journal.
1110+ * @journal: The journal to update.
1111+ *
1112+ * Update a journal's errno. Write updated superblock to disk waiting for IO
1113+ * to complete.
1114+ */
1115+ static void journal_update_sb_errno (journal_t * journal )
1116+ {
1117+ journal_superblock_t * sb = journal -> j_superblock ;
1118+
1119+ spin_lock (& journal -> j_state_lock );
1120+ jbd_debug (1 , "JBD: updating superblock error (errno %d)\n" ,
1121+ journal -> j_errno );
1122+ sb -> s_errno = cpu_to_be32 (journal -> j_errno );
1123+ spin_unlock (& journal -> j_state_lock );
1124+
1125+ journal_write_superblock (journal );
1126+ }
1127+
10881128/*
10891129 * Read the superblock for a given journal, performing initial
10901130 * validation of the format.
@@ -1268,14 +1308,11 @@ int journal_destroy(journal_t *journal)
12681308
12691309 if (journal -> j_sb_buffer ) {
12701310 if (!is_journal_aborted (journal )) {
1271- /* We can now mark the journal as empty. */
1272- journal -> j_tail = 0 ;
12731311 journal -> j_tail_sequence =
12741312 ++ journal -> j_transaction_sequence ;
1275- journal_update_superblock (journal , 1 );
1276- } else {
1313+ mark_journal_empty (journal );
1314+ } else
12771315 err = - EIO ;
1278- }
12791316 brelse (journal -> j_sb_buffer );
12801317 }
12811318
@@ -1457,7 +1494,6 @@ int journal_flush(journal_t *journal)
14571494{
14581495 int err = 0 ;
14591496 transaction_t * transaction = NULL ;
1460- unsigned int old_tail ;
14611497
14621498 spin_lock (& journal -> j_state_lock );
14631499
@@ -1499,14 +1535,8 @@ int journal_flush(journal_t *journal)
14991535 * the magic code for a fully-recovered superblock. Any future
15001536 * commits of data to the journal will restore the current
15011537 * s_start value. */
1538+ mark_journal_empty (journal );
15021539 spin_lock (& journal -> j_state_lock );
1503- old_tail = journal -> j_tail ;
1504- journal -> j_tail = 0 ;
1505- spin_unlock (& journal -> j_state_lock );
1506- journal_update_superblock (journal , 1 );
1507- spin_lock (& journal -> j_state_lock );
1508- journal -> j_tail = old_tail ;
1509-
15101540 J_ASSERT (!journal -> j_running_transaction );
15111541 J_ASSERT (!journal -> j_committing_transaction );
15121542 J_ASSERT (!journal -> j_checkpoint_transactions );
@@ -1547,7 +1577,7 @@ int journal_wipe(journal_t *journal, int write)
15471577
15481578 err = journal_skip_recovery (journal );
15491579 if (write )
1550- journal_update_superblock (journal , 1 );
1580+ mark_journal_empty (journal );
15511581
15521582 no_recovery :
15531583 return err ;
@@ -1615,7 +1645,7 @@ static void __journal_abort_soft (journal_t *journal, int errno)
16151645 __journal_abort_hard (journal );
16161646
16171647 if (errno )
1618- journal_update_superblock (journal , 1 );
1648+ journal_update_sb_errno (journal );
16191649}
16201650
16211651/**
0 commit comments