2 * linux/fs/jbd/recovery.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
6 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * Journal recovery routines for the generic filesystem journaling code;
13 * part of the ext2fs journaling system.
19 #include <linux/module.h>
20 #include <linux/time.h>
22 #include <linux/jbd.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
28 * Maintain information about the progress of the recovery job, so that
29 * the different passes can carry information between them.
33 tid_t start_transaction
;
34 tid_t end_transaction
;
41 enum passtype
{PASS_SCAN
, PASS_REVOKE
, PASS_REPLAY
};
42 static int do_one_pass(journal_t
*journal
,
43 struct recovery_info
*info
, enum passtype pass
);
44 static int scan_revoke_records(journal_t
*, struct buffer_head
*,
45 tid_t
, struct recovery_info
*);
49 /* Release readahead buffers after use */
50 static void journal_brelse_array(struct buffer_head
*b
[], int n
)
58 * When reading from the journal, we are going through the block device
59 * layer directly and so there is no readahead being done for us. We
60 * need to implement any readahead ourselves if we want it to happen at
61 * all. Recovery is basically one long sequential read, so make sure we
62 * do the IO in reasonably large chunks.
64 * This is not so critical that we need to be enormously clever about
65 * the readahead size, though. 128K is a purely arbitrary, good-enough
70 static int do_readahead(journal_t
*journal
, unsigned int start
)
73 unsigned int max
, nbufs
, next
;
74 unsigned long blocknr
;
75 struct buffer_head
*bh
;
77 struct buffer_head
* bufs
[MAXBUF
];
79 /* Do up to 128K of readahead */
80 max
= start
+ (128 * 1024 / journal
->j_blocksize
);
81 if (max
> journal
->j_maxlen
)
82 max
= journal
->j_maxlen
;
84 /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
85 * a time to the block device IO layer. */
89 for (next
= start
; next
< max
; next
++) {
90 err
= journal_bmap(journal
, next
, &blocknr
);
93 printk (KERN_ERR
"JBD: bad block at offset %u\n",
98 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
104 if (!buffer_uptodate(bh
) && !buffer_locked(bh
)) {
106 if (nbufs
== MAXBUF
) {
107 ll_rw_block(READ
, nbufs
, bufs
);
108 journal_brelse_array(bufs
, nbufs
);
116 ll_rw_block(READ
, nbufs
, bufs
);
121 journal_brelse_array(bufs
, nbufs
);
125 #endif /* __KERNEL__ */
129 * Read a block from the journal
132 static int jread(struct buffer_head
**bhp
, journal_t
*journal
,
136 unsigned long blocknr
;
137 struct buffer_head
*bh
;
141 if (offset
>= journal
->j_maxlen
) {
142 printk(KERN_ERR
"JBD: corrupted journal superblock\n");
146 err
= journal_bmap(journal
, offset
, &blocknr
);
149 printk (KERN_ERR
"JBD: bad block at offset %u\n",
154 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
158 if (!buffer_uptodate(bh
)) {
159 /* If this is a brand new buffer, start readahead.
160 Otherwise, we assume we are already reading it. */
162 do_readahead(journal
, offset
);
166 if (!buffer_uptodate(bh
)) {
167 printk (KERN_ERR
"JBD: Failed to read block at offset %u\n",
179 * Count the number of in-use tags in a journal descriptor block.
182 static int count_tags(struct buffer_head
*bh
, int size
)
185 journal_block_tag_t
* tag
;
188 tagp
= &bh
->b_data
[sizeof(journal_header_t
)];
190 while (((int)(tagp
- bh
->b_data
) + (int)sizeof(journal_block_tag_t
)) <= size
) {
191 tag
= (journal_block_tag_t
*) tagp
;
194 tagp
+= sizeof(journal_block_tag_t
);
195 if (!(tag
->t_flags
& cpu_to_be32(JFS_FLAG_SAME_UUID
)))
198 if (tag
->t_flags
& cpu_to_be32(JFS_FLAG_LAST_TAG
))
206 /* Make sure we wrap around the log correctly! */
207 #define wrap(journal, var) \
209 if (var >= (journal)->j_last) \
210 var -= ((journal)->j_last - (journal)->j_first); \
214 * journal_recover - recovers a on-disk journal
215 * @journal: the journal to recover
217 * The primary function for recovering the log contents when mounting a
220 * Recovery is done in three passes. In the first pass, we look for the
221 * end of the log. In the second, we assemble the list of revoke
222 * blocks. In the third and final pass, we replay any un-revoked blocks
225 int journal_recover(journal_t
*journal
)
228 journal_superblock_t
* sb
;
230 struct recovery_info info
;
232 memset(&info
, 0, sizeof(info
));
233 sb
= journal
->j_superblock
;
236 * The journal superblock's s_start field (the current log head)
237 * is always zero if, and only if, the journal was cleanly
242 jbd_debug(1, "No recovery required, last transaction %d\n",
243 be32_to_cpu(sb
->s_sequence
));
244 journal
->j_transaction_sequence
= be32_to_cpu(sb
->s_sequence
) + 1;
248 err
= do_one_pass(journal
, &info
, PASS_SCAN
);
250 err
= do_one_pass(journal
, &info
, PASS_REVOKE
);
252 err
= do_one_pass(journal
, &info
, PASS_REPLAY
);
254 jbd_debug(1, "JBD: recovery, exit status %d, "
255 "recovered transactions %u to %u\n",
256 err
, info
.start_transaction
, info
.end_transaction
);
257 jbd_debug(1, "JBD: Replayed %d and revoked %d/%d blocks\n",
258 info
.nr_replays
, info
.nr_revoke_hits
, info
.nr_revokes
);
260 /* Restart the log at the next transaction ID, thus invalidating
261 * any existing commit records in the log. */
262 journal
->j_transaction_sequence
= ++info
.end_transaction
;
264 journal_clear_revoke(journal
);
265 sync_blockdev(journal
->j_fs_dev
);
270 * journal_skip_recovery - Start journal and wipe exiting records
271 * @journal: journal to startup
273 * Locate any valid recovery information from the journal and set up the
274 * journal structures in memory to ignore it (presumably because the
275 * caller has evidence that it is out of date).
276 * This function does'nt appear to be exorted..
278 * We perform one pass over the journal to allow us to tell the user how
279 * much recovery information is being erased, and to let us initialise
280 * the journal transaction sequence numbers to the next unused ID.
282 int journal_skip_recovery(journal_t
*journal
)
285 journal_superblock_t
* sb
;
287 struct recovery_info info
;
289 memset (&info
, 0, sizeof(info
));
290 sb
= journal
->j_superblock
;
292 err
= do_one_pass(journal
, &info
, PASS_SCAN
);
295 printk(KERN_ERR
"JBD: error %d scanning journal\n", err
);
296 ++journal
->j_transaction_sequence
;
298 #ifdef CONFIG_JBD_DEBUG
299 int dropped
= info
.end_transaction
- be32_to_cpu(sb
->s_sequence
);
301 "JBD: ignoring %d transaction%s from the journal.\n",
302 dropped
, (dropped
== 1) ? "" : "s");
304 journal
->j_transaction_sequence
= ++info
.end_transaction
;
311 static int do_one_pass(journal_t
*journal
,
312 struct recovery_info
*info
, enum passtype pass
)
314 unsigned int first_commit_ID
, next_commit_ID
;
315 unsigned long next_log_block
;
316 int err
, success
= 0;
317 journal_superblock_t
* sb
;
318 journal_header_t
* tmp
;
319 struct buffer_head
* bh
;
320 unsigned int sequence
;
323 /* Precompute the maximum metadata descriptors in a descriptor block */
324 int MAX_BLOCKS_PER_DESC
;
325 MAX_BLOCKS_PER_DESC
= ((journal
->j_blocksize
-sizeof(journal_header_t
))
326 / sizeof(journal_block_tag_t
));
329 * First thing is to establish what we expect to find in the log
330 * (in terms of transaction IDs), and where (in terms of log
331 * block offsets): query the superblock.
334 sb
= journal
->j_superblock
;
335 next_commit_ID
= be32_to_cpu(sb
->s_sequence
);
336 next_log_block
= be32_to_cpu(sb
->s_start
);
338 first_commit_ID
= next_commit_ID
;
339 if (pass
== PASS_SCAN
)
340 info
->start_transaction
= first_commit_ID
;
342 jbd_debug(1, "Starting recovery pass %d\n", pass
);
345 * Now we walk through the log, transaction by transaction,
346 * making sure that each transaction has a commit block in the
347 * expected place. Each complete transaction gets replayed back
348 * into the main filesystem.
354 journal_block_tag_t
* tag
;
355 struct buffer_head
* obh
;
356 struct buffer_head
* nbh
;
360 /* If we already know where to stop the log traversal,
361 * check right now that we haven't gone past the end of
364 if (pass
!= PASS_SCAN
)
365 if (tid_geq(next_commit_ID
, info
->end_transaction
))
368 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
369 next_commit_ID
, next_log_block
, journal
->j_last
);
371 /* Skip over each chunk of the transaction looking
372 * either the next descriptor block or the final commit
375 jbd_debug(3, "JBD: checking block %ld\n", next_log_block
);
376 err
= jread(&bh
, journal
, next_log_block
);
381 wrap(journal
, next_log_block
);
383 /* What kind of buffer is it?
385 * If it is a descriptor block, check that it has the
386 * expected sequence number. Otherwise, we're all done
389 tmp
= (journal_header_t
*)bh
->b_data
;
391 if (tmp
->h_magic
!= cpu_to_be32(JFS_MAGIC_NUMBER
)) {
396 blocktype
= be32_to_cpu(tmp
->h_blocktype
);
397 sequence
= be32_to_cpu(tmp
->h_sequence
);
398 jbd_debug(3, "Found magic %d, sequence %d\n",
399 blocktype
, sequence
);
401 if (sequence
!= next_commit_ID
) {
406 /* OK, we have a valid descriptor block which matches
407 * all of the sequence number checks. What are we going
408 * to do with it? That depends on the pass... */
411 case JFS_DESCRIPTOR_BLOCK
:
412 /* If it is a valid descriptor block, replay it
413 * in pass REPLAY; otherwise, just skip over the
414 * blocks it describes. */
415 if (pass
!= PASS_REPLAY
) {
417 count_tags(bh
, journal
->j_blocksize
);
418 wrap(journal
, next_log_block
);
423 /* A descriptor block: we can now write all of
424 * the data blocks. Yay, useful work is finally
425 * getting done here! */
427 tagp
= &bh
->b_data
[sizeof(journal_header_t
)];
428 while (((int)(tagp
- bh
->b_data
) + (int)sizeof(journal_block_tag_t
))
429 <= journal
->j_blocksize
) {
430 unsigned long io_block
;
432 tag
= (journal_block_tag_t
*) tagp
;
433 flags
= be32_to_cpu(tag
->t_flags
);
435 io_block
= next_log_block
++;
436 wrap(journal
, next_log_block
);
437 err
= jread(&obh
, journal
, io_block
);
439 /* Recover what we can, but
440 * report failure at the end. */
443 "JBD: IO error %d recovering "
444 "block %ld in log\n",
447 unsigned long blocknr
;
449 J_ASSERT(obh
!= NULL
);
450 blocknr
= be32_to_cpu(tag
->t_blocknr
);
452 /* If the block has been
453 * revoked, then we're all done
455 if (journal_test_revoke
459 ++info
->nr_revoke_hits
;
463 /* Find a buffer for the new
464 * data being restored */
465 nbh
= __getblk(journal
->j_fs_dev
,
467 journal
->j_blocksize
);
470 "JBD: Out of memory "
471 "during recovery.\n");
479 memcpy(nbh
->b_data
, obh
->b_data
,
480 journal
->j_blocksize
);
481 if (flags
& JFS_FLAG_ESCAPE
) {
482 *((__be32
*)bh
->b_data
) =
483 cpu_to_be32(JFS_MAGIC_NUMBER
);
486 BUFFER_TRACE(nbh
, "marking dirty");
487 set_buffer_uptodate(nbh
);
488 mark_buffer_dirty(nbh
);
489 BUFFER_TRACE(nbh
, "marking uptodate");
491 /* ll_rw_block(WRITE, 1, &nbh); */
498 tagp
+= sizeof(journal_block_tag_t
);
499 if (!(flags
& JFS_FLAG_SAME_UUID
))
502 if (flags
& JFS_FLAG_LAST_TAG
)
509 case JFS_COMMIT_BLOCK
:
510 /* Found an expected commit block: not much to
511 * do other than move on to the next sequence
517 case JFS_REVOKE_BLOCK
:
518 /* If we aren't in the REVOKE pass, then we can
519 * just skip over this block. */
520 if (pass
!= PASS_REVOKE
) {
525 err
= scan_revoke_records(journal
, bh
,
526 next_commit_ID
, info
);
533 jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
542 * We broke out of the log scan loop: either we came to the
543 * known end of the log or we found an unexpected block in the
544 * log. If the latter happened, then we know that the "current"
545 * transaction marks the end of the valid log.
548 if (pass
== PASS_SCAN
)
549 info
->end_transaction
= next_commit_ID
;
551 /* It's really bad news if different passes end up at
552 * different places (but possible due to IO errors). */
553 if (info
->end_transaction
!= next_commit_ID
) {
554 printk (KERN_ERR
"JBD: recovery pass %d ended at "
555 "transaction %u, expected %u\n",
556 pass
, next_commit_ID
, info
->end_transaction
);
569 /* Scan a revoke record, marking all blocks mentioned as revoked. */
571 static int scan_revoke_records(journal_t
*journal
, struct buffer_head
*bh
,
572 tid_t sequence
, struct recovery_info
*info
)
574 journal_revoke_header_t
*header
;
577 header
= (journal_revoke_header_t
*) bh
->b_data
;
578 offset
= sizeof(journal_revoke_header_t
);
579 max
= be32_to_cpu(header
->r_count
);
581 while (offset
< max
) {
582 unsigned long blocknr
;
585 blocknr
= be32_to_cpu(* ((__be32
*) (bh
->b_data
+offset
)));
587 err
= journal_set_revoke(journal
, blocknr
, sequence
);