]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/recovery.c
e2scrub: make e2scrub_fail's e-mail addresses be configurable
[thirdparty/e2fsprogs.git] / e2fsck / recovery.c
CommitLineData
3b5386dc 1/*
13af4b93 2 * linux/fs/jbd2/recovery.c
e5ea6b14 3 *
3b5386dc
TT
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
5 *
0e8a9560 6 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
3b5386dc
TT
7 *
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.
11 *
12 * Journal recovery routines for the generic filesystem journaling code;
e5ea6b14 13 * part of the ext2fs journaling system.
3b5386dc
TT
14 */
15
16#ifndef __KERNEL__
0e8a9560 17#include "jfs_user.h"
3b5386dc 18#else
3de085dd 19#include <linux/time.h>
3b5386dc 20#include <linux/fs.h>
13af4b93 21#include <linux/jbd2.h>
3b5386dc 22#include <linux/errno.h>
13af4b93
DW
23#include <linux/crc32.h>
24#include <linux/blkdev.h>
0e8a9560
TT
25#endif
26
27/*
28 * Maintain information about the progress of the recovery job, so that
e5ea6b14 29 * the different passes can carry information between them.
0e8a9560 30 */
e5ea6b14 31struct recovery_info
0e8a9560 32{
e5ea6b14 33 tid_t start_transaction;
0e8a9560 34 tid_t end_transaction;
e5ea6b14 35
0e8a9560
TT
36 int nr_replays;
37 int nr_revokes;
38 int nr_revoke_hits;
39};
3b5386dc 40
0e8a9560 41enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
8cf93332
TT
42static int do_one_pass(journal_t *journal,
43 struct recovery_info *info, enum passtype pass);
44static int scan_revoke_records(journal_t *, struct buffer_head *,
45 tid_t, struct recovery_info *);
0e8a9560
TT
46
47#ifdef __KERNEL__
3b5386dc
TT
48
49/* Release readahead buffers after use */
e5ea6b14 50static void journal_brelse_array(struct buffer_head *b[], int n)
3b5386dc
TT
51{
52 while (--n >= 0)
53 brelse (b[n]);
54}
55
56
57/*
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.
63 *
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
66 * fixed value.
67 */
68
1f735038 69#define MAXBUF 8
3b5386dc
TT
70static int do_readahead(journal_t *journal, unsigned int start)
71{
72 int err;
8cf93332 73 unsigned int max, nbufs, next;
3b693d0b 74 unsigned long long blocknr;
3b5386dc 75 struct buffer_head *bh;
e5ea6b14 76
3b5386dc 77 struct buffer_head * bufs[MAXBUF];
e5ea6b14 78
3b5386dc
TT
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;
83
84 /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
85 * a time to the block device IO layer. */
e5ea6b14 86
3b5386dc 87 nbufs = 0;
e5ea6b14 88
3b5386dc 89 for (next = start; next < max; next++) {
8cf93332
TT
90 err = journal_bmap(journal, next, &blocknr);
91
92 if (err) {
13af4b93 93 printk(KERN_ERR "JBD2: bad block at offset %u\n",
3b5386dc 94 next);
3b5386dc
TT
95 goto failed;
96 }
8cf93332 97
3de085dd 98 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
3b5386dc 99 if (!bh) {
3b5386dc
TT
100 err = -ENOMEM;
101 goto failed;
102 }
103
104 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
105 bufs[nbufs++] = bh;
106 if (nbufs == MAXBUF) {
107 ll_rw_block(READ, nbufs, bufs);
8cf93332 108 journal_brelse_array(bufs, nbufs);
3b5386dc
TT
109 nbufs = 0;
110 }
111 } else
112 brelse(bh);
113 }
114
115 if (nbufs)
116 ll_rw_block(READ, nbufs, bufs);
117 err = 0;
8cf93332 118
e5ea6b14
TT
119failed:
120 if (nbufs)
8cf93332 121 journal_brelse_array(bufs, nbufs);
3b5386dc
TT
122 return err;
123}
0e8a9560
TT
124
125#endif /* __KERNEL__ */
126
c8ca2397
TT
127static inline __u32 get_be32(__be32 *p)
128{
129 unsigned char *cp = (unsigned char *) p;
130 __u32 ret;
131
132 ret = *cp++;
133 ret = (ret << 8) + *cp++;
134 ret = (ret << 8) + *cp++;
135 ret = (ret << 8) + *cp++;
136 return ret;
137}
138
139static inline __u16 get_be16(__be16 *p)
140{
141 unsigned char *cp = (unsigned char *) p;
142 __u16 ret;
143
144 ret = *cp++;
145 ret = (ret << 8) + *cp++;
146 return ret;
147}
3b5386dc
TT
148
149/*
150 * Read a block from the journal
151 */
152
e5ea6b14 153static int jread(struct buffer_head **bhp, journal_t *journal,
3b5386dc
TT
154 unsigned int offset)
155{
8cf93332 156 int err;
3b693d0b 157 unsigned long long blocknr;
3b5386dc
TT
158 struct buffer_head *bh;
159
160 *bhp = NULL;
161
e5ea6b14 162 if (offset >= journal->j_maxlen) {
13af4b93 163 printk(KERN_ERR "JBD2: corrupted journal superblock\n");
b4f02c9f 164 return -EFSCORRUPTED;
e5ea6b14
TT
165 }
166
8cf93332
TT
167 err = journal_bmap(journal, offset, &blocknr);
168
169 if (err) {
13af4b93 170 printk(KERN_ERR "JBD2: bad block at offset %u\n",
3b5386dc 171 offset);
8cf93332 172 return err;
3b5386dc 173 }
8cf93332 174
3de085dd 175 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
3b5386dc
TT
176 if (!bh)
177 return -ENOMEM;
8cf93332 178
3b5386dc
TT
179 if (!buffer_uptodate(bh)) {
180 /* If this is a brand new buffer, start readahead.
181 Otherwise, we assume we are already reading it. */
182 if (!buffer_req(bh))
183 do_readahead(journal, offset);
184 wait_on_buffer(bh);
185 }
8cf93332 186
3b5386dc 187 if (!buffer_uptodate(bh)) {
13af4b93 188 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
3b5386dc
TT
189 offset);
190 brelse(bh);
191 return -EIO;
192 }
8cf93332 193
3b5386dc
TT
194 *bhp = bh;
195 return 0;
196}
197
5c842851
DW
198static int jbd2_descr_block_csum_verify(journal_t *j,
199 void *buf)
200{
201 struct journal_block_tail *tail;
13af4b93
DW
202 __u32 provided;
203 __u32 calculated;
5c842851 204
38d5adf3 205 if (!journal_has_csum_v2or3(j))
5c842851
DW
206 return 1;
207
478360f5 208 tail = (struct journal_block_tail *)((char *)buf + j->j_blocksize -
5c842851
DW
209 sizeof(struct journal_block_tail));
210 provided = tail->t_checksum;
211 tail->t_checksum = 0;
13af4b93 212 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
5c842851
DW
213 tail->t_checksum = provided;
214
13af4b93 215 return provided == ext2fs_cpu_to_be32(calculated);
5c842851 216}
3b5386dc
TT
217
218/*
219 * Count the number of in-use tags in a journal descriptor block.
220 */
221
185c4aea 222static int count_tags(journal_t *journal, struct buffer_head *bh)
3b5386dc
TT
223{
224 char * tagp;
225 journal_block_tag_t * tag;
185c4aea
TT
226 int nr = 0, size = journal->j_blocksize;
227 int tag_bytes = journal_tag_bytes(journal);
8cf93332 228
38d5adf3 229 if (journal_has_csum_v2or3(journal))
5c842851
DW
230 size -= sizeof(struct journal_block_tail);
231
3b5386dc 232 tagp = &bh->b_data[sizeof(journal_header_t)];
8cf93332 233
185c4aea 234 while ((tagp - bh->b_data + tag_bytes) <= size) {
3b5386dc 235 tag = (journal_block_tag_t *) tagp;
8cf93332 236
3b5386dc 237 nr++;
185c4aea 238 tagp += tag_bytes;
c8ca2397 239 if (!(get_be16(&tag->t_flags) & JFS_FLAG_SAME_UUID))
3b5386dc
TT
240 tagp += 16;
241
c8ca2397 242 if (get_be16(&tag->t_flags) & JFS_FLAG_LAST_TAG)
3b5386dc
TT
243 break;
244 }
8cf93332 245
3b5386dc
TT
246 return nr;
247}
248
249
250/* Make sure we wrap around the log correctly! */
251#define wrap(journal, var) \
252do { \
253 if (var >= (journal)->j_last) \
254 var -= ((journal)->j_last - (journal)->j_first); \
255} while (0)
256
3de085dd 257/**
e5ea6b14 258 * journal_recover - recovers a on-disk journal
3de085dd 259 * @journal: the journal to recover
3b5386dc
TT
260 *
261 * The primary function for recovering the log contents when mounting a
e5ea6b14
TT
262 * journaled device.
263 *
0e8a9560
TT
264 * Recovery is done in three passes. In the first pass, we look for the
265 * end of the log. In the second, we assemble the list of revoke
266 * blocks. In the third and final pass, we replay any un-revoked blocks
e5ea6b14 267 * in the log.
3b5386dc 268 */
3b5386dc
TT
269int journal_recover(journal_t *journal)
270{
13af4b93 271 int err, err2;
0e8a9560
TT
272 journal_superblock_t * sb;
273
53ef44c4 274 struct recovery_info info;
e5ea6b14 275
1f735038 276 memset(&info, 0, sizeof(info));
0e8a9560 277 sb = journal->j_superblock;
e5ea6b14
TT
278
279 /*
0e8a9560
TT
280 * The journal superblock's s_start field (the current log head)
281 * is always zero if, and only if, the journal was cleanly
e5ea6b14 282 * unmounted.
0e8a9560
TT
283 */
284
285 if (!sb->s_start) {
8cf93332 286 jbd_debug(1, "No recovery required, last transaction %d\n",
13af4b93
DW
287 ext2fs_be32_to_cpu(sb->s_sequence));
288 journal->j_transaction_sequence = ext2fs_be32_to_cpu(sb->s_sequence) + 1;
0e8a9560
TT
289 return 0;
290 }
e5ea6b14 291
0e8a9560
TT
292 err = do_one_pass(journal, &info, PASS_SCAN);
293 if (!err)
294 err = do_one_pass(journal, &info, PASS_REVOKE);
295 if (!err)
296 err = do_one_pass(journal, &info, PASS_REPLAY);
297
13af4b93 298 jbd_debug(1, "JBD2: recovery, exit status %d, "
0e8a9560
TT
299 "recovered transactions %u to %u\n",
300 err, info.start_transaction, info.end_transaction);
13af4b93 301 jbd_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
0e8a9560
TT
302 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
303
304 /* Restart the log at the next transaction ID, thus invalidating
305 * any existing commit records in the log. */
306 journal->j_transaction_sequence = ++info.end_transaction;
e5ea6b14 307
0e8a9560 308 journal_clear_revoke(journal);
13af4b93
DW
309 err2 = sync_blockdev(journal->j_fs_dev);
310 if (!err)
311 err = err2;
312 /* Make sure all replayed data is on permanent storage */
313 if (journal->j_flags & JFS_BARRIER) {
314 err2 = blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL);
315 if (!err)
316 err = err2;
317 }
0e8a9560
TT
318 return err;
319}
320
3de085dd 321/**
e5ea6b14 322 * journal_skip_recovery - Start journal and wipe exiting records
3de085dd 323 * @journal: journal to startup
1f735038
TT
324 *
325 * Locate any valid recovery information from the journal and set up the
326 * journal structures in memory to ignore it (presumably because the
e5ea6b14 327 * caller has evidence that it is out of date).
055866d8 328 * This function doesn't appear to be exported..
1f735038
TT
329 *
330 * We perform one pass over the journal to allow us to tell the user how
331 * much recovery information is being erased, and to let us initialise
e5ea6b14 332 * the journal transaction sequence numbers to the next unused ID.
1f735038 333 */
1f735038
TT
334int journal_skip_recovery(journal_t *journal)
335{
336 int err;
13af4b93 337
1f735038 338 struct recovery_info info;
e5ea6b14 339
8cf93332 340 memset (&info, 0, sizeof(info));
e5ea6b14 341
1f735038
TT
342 err = do_one_pass(journal, &info, PASS_SCAN);
343
344 if (err) {
13af4b93 345 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
1f735038
TT
346 ++journal->j_transaction_sequence;
347 } else {
13af4b93 348#ifdef CONFIG_JFS_DEBUG
97f168b6 349 int dropped = info.end_transaction -
13af4b93 350 ext2fs_be32_to_cpu(journal->j_superblock->s_sequence);
e5ea6b14 351 jbd_debug(1,
13af4b93 352 "JBD2: ignoring %d transaction%s from the journal.\n",
1f735038 353 dropped, (dropped == 1) ? "" : "s");
13af4b93 354#endif
1f735038
TT
355 journal->j_transaction_sequence = ++info.end_transaction;
356 }
357
358 journal->j_tail = 0;
1f735038
TT
359 return err;
360}
361
38d5adf3
DW
362static inline unsigned long long read_tag_block(journal_t *journal,
363 journal_block_tag_t *tag)
185c4aea 364{
8d7a6392 365 unsigned long long block = get_be32(&tag->t_blocknr);
86f3b6cf 366 if (jfs_has_feature_64bit(journal))
8d7a6392 367 block |= (u64)get_be32(&tag->t_blocknr_high) << 32;
185c4aea
TT
368 return block;
369}
370
371/*
372 * calc_chksums calculates the checksums for the blocks described in the
373 * descriptor block.
374 */
375static int calc_chksums(journal_t *journal, struct buffer_head *bh,
13af4b93 376 unsigned long *next_log_block, __u32 *crc32_sum)
185c4aea
TT
377{
378 int i, num_blks, err;
13af4b93 379 unsigned long io_block;
185c4aea
TT
380 struct buffer_head *obh;
381
382 num_blks = count_tags(journal, bh);
383 /* Calculate checksum of the descriptor block. */
13af4b93 384 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
185c4aea
TT
385
386 for (i = 0; i < num_blks; i++) {
387 io_block = (*next_log_block)++;
388 wrap(journal, *next_log_block);
389 err = jread(&obh, journal, io_block);
390 if (err) {
13af4b93
DW
391 printk(KERN_ERR "JBD2: IO error %d recovering block "
392 "%lu in log\n", err, io_block);
185c4aea
TT
393 return 1;
394 } else {
13af4b93
DW
395 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
396 obh->b_size);
185c4aea 397 }
13af4b93 398 put_bh(obh);
185c4aea
TT
399 }
400 return 0;
401}
402
721e065c
DW
403static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
404{
405 struct commit_header *h;
13af4b93
DW
406 __u32 provided;
407 __u32 calculated;
721e065c 408
38d5adf3 409 if (!journal_has_csum_v2or3(j))
721e065c
DW
410 return 1;
411
412 h = buf;
413 provided = h->h_chksum[0];
414 h->h_chksum[0] = 0;
13af4b93 415 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
721e065c
DW
416 h->h_chksum[0] = provided;
417
13af4b93 418 return provided == ext2fs_cpu_to_be32(calculated);
721e065c
DW
419}
420
d9200380
DW
421static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
422 void *buf, __u32 sequence)
423{
38d5adf3 424 journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
13af4b93
DW
425 __u32 csum32;
426 __u32 seq;
d9200380 427
38d5adf3 428 if (!journal_has_csum_v2or3(j))
d9200380
DW
429 return 1;
430
13af4b93
DW
431 seq = ext2fs_cpu_to_be32(sequence);
432 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
433 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
d9200380 434
86f3b6cf 435 if (jfs_has_feature_csum3(j))
c8ca2397 436 return get_be32(&tag3->t_checksum) == csum32;
38d5adf3 437
c8ca2397 438 return get_be16(&tag->t_checksum) == (csum32 & 0xFFFF);
d9200380
DW
439}
440
8cf93332
TT
441static int do_one_pass(journal_t *journal,
442 struct recovery_info *info, enum passtype pass)
0e8a9560 443{
3b5386dc 444 unsigned int first_commit_ID, next_commit_ID;
13af4b93 445 unsigned long next_log_block;
3b5386dc 446 int err, success = 0;
0e8a9560 447 journal_superblock_t * sb;
e5ea6b14 448 journal_header_t * tmp;
3b5386dc 449 struct buffer_head * bh;
0e8a9560
TT
450 unsigned int sequence;
451 int blocktype;
185c4aea
TT
452 int tag_bytes = journal_tag_bytes(journal);
453 __u32 crc32_sum = ~0; /* Transactional Checksums */
5c842851 454 int descr_csum_size = 0;
bf93a968 455 int block_error = 0;
e5ea6b14 456
e5ea6b14 457 /*
3b5386dc
TT
458 * First thing is to establish what we expect to find in the log
459 * (in terms of transaction IDs), and where (in terms of log
e5ea6b14 460 * block offsets): query the superblock.
3b5386dc
TT
461 */
462
0e8a9560 463 sb = journal->j_superblock;
13af4b93
DW
464 next_commit_ID = ext2fs_be32_to_cpu(sb->s_sequence);
465 next_log_block = ext2fs_be32_to_cpu(sb->s_start);
3b5386dc
TT
466
467 first_commit_ID = next_commit_ID;
0e8a9560
TT
468 if (pass == PASS_SCAN)
469 info->start_transaction = first_commit_ID;
8cf93332
TT
470
471 jbd_debug(1, "Starting recovery pass %d\n", pass);
472
3b5386dc
TT
473 /*
474 * Now we walk through the log, transaction by transaction,
475 * making sure that each transaction has a commit block in the
476 * expected place. Each complete transaction gets replayed back
e5ea6b14 477 * into the main filesystem.
3b5386dc
TT
478 */
479
0e8a9560
TT
480 while (1) {
481 int flags;
482 char * tagp;
483 journal_block_tag_t * tag;
484 struct buffer_head * obh;
485 struct buffer_head * nbh;
e5ea6b14
TT
486
487 cond_resched();
488
0e8a9560
TT
489 /* If we already know where to stop the log traversal,
490 * check right now that we haven't gone past the end of
491 * the log. */
e5ea6b14 492
0e8a9560
TT
493 if (pass != PASS_SCAN)
494 if (tid_geq(next_commit_ID, info->end_transaction))
495 break;
8cf93332 496
13af4b93 497 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
3b5386dc 498 next_commit_ID, next_log_block, journal->j_last);
3b5386dc 499
0e8a9560
TT
500 /* Skip over each chunk of the transaction looking
501 * either the next descriptor block or the final commit
502 * record. */
e5ea6b14 503
13af4b93 504 jbd_debug(3, "JBD2: checking block %ld\n", next_log_block);
0e8a9560
TT
505 err = jread(&bh, journal, next_log_block);
506 if (err)
507 goto failed;
8cf93332 508
0e8a9560
TT
509 next_log_block++;
510 wrap(journal, next_log_block);
e5ea6b14
TT
511
512 /* What kind of buffer is it?
513 *
0e8a9560
TT
514 * If it is a descriptor block, check that it has the
515 * expected sequence number. Otherwise, we're all done
516 * here. */
3b5386dc 517
8cf93332 518 tmp = (journal_header_t *)bh->b_data;
e5ea6b14 519
13af4b93 520 if (tmp->h_magic != ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER)) {
3b5386dc
TT
521 brelse(bh);
522 break;
523 }
8cf93332 524
13af4b93
DW
525 blocktype = ext2fs_be32_to_cpu(tmp->h_blocktype);
526 sequence = ext2fs_be32_to_cpu(tmp->h_sequence);
e5ea6b14 527 jbd_debug(3, "Found magic %d, sequence %d\n",
0e8a9560 528 blocktype, sequence);
e5ea6b14 529
0e8a9560
TT
530 if (sequence != next_commit_ID) {
531 brelse(bh);
532 break;
533 }
e5ea6b14 534
0e8a9560
TT
535 /* OK, we have a valid descriptor block which matches
536 * all of the sequence number checks. What are we going
537 * to do with it? That depends on the pass... */
8cf93332 538
0e8a9560
TT
539 switch(blocktype) {
540 case JFS_DESCRIPTOR_BLOCK:
5c842851 541 /* Verify checksum first */
38d5adf3 542 if (journal_has_csum_v2or3(journal))
5c842851
DW
543 descr_csum_size =
544 sizeof(struct journal_block_tail);
545 if (descr_csum_size > 0 &&
546 !jbd2_descr_block_csum_verify(journal,
547 bh->b_data)) {
b4f02c9f 548 err = -EFSBADCRC;
c10700f9 549 brelse(bh);
5c842851
DW
550 goto failed;
551 }
552
0e8a9560 553 /* If it is a valid descriptor block, replay it
185c4aea
TT
554 * in pass REPLAY; if journal_checksums enabled, then
555 * calculate checksums in PASS_SCAN, otherwise,
556 * just skip over the blocks it describes. */
0e8a9560 557 if (pass != PASS_REPLAY) {
185c4aea 558 if (pass == PASS_SCAN &&
86f3b6cf 559 jfs_has_feature_checksum(journal) &&
185c4aea
TT
560 !info->end_transaction) {
561 if (calc_chksums(journal, bh,
562 &next_log_block,
563 &crc32_sum)) {
13af4b93 564 put_bh(bh);
185c4aea
TT
565 break;
566 }
13af4b93 567 put_bh(bh);
185c4aea
TT
568 continue;
569 }
570 next_log_block += count_tags(journal, bh);
0e8a9560 571 wrap(journal, next_log_block);
13af4b93 572 put_bh(bh);
0e8a9560 573 continue;
3b5386dc 574 }
0e8a9560 575
3b5386dc
TT
576 /* A descriptor block: we can now write all of
577 * the data blocks. Yay, useful work is finally
578 * getting done here! */
579
580 tagp = &bh->b_data[sizeof(journal_header_t)];
185c4aea 581 while ((tagp - bh->b_data + tag_bytes)
5c842851 582 <= journal->j_blocksize - descr_csum_size) {
13af4b93 583 unsigned long io_block;
8cf93332 584
3b5386dc 585 tag = (journal_block_tag_t *) tagp;
c8ca2397 586 flags = get_be16(&tag->t_flags);
e5ea6b14 587
0e8a9560 588 io_block = next_log_block++;
3b5386dc 589 wrap(journal, next_log_block);
0e8a9560 590 err = jread(&obh, journal, io_block);
3b5386dc
TT
591 if (err) {
592 /* Recover what we can, but
593 * report failure at the end. */
594 success = err;
13af4b93
DW
595 printk(KERN_ERR
596 "JBD2: IO error %d recovering "
597 "block %ld in log\n",
0e8a9560 598 err, io_block);
3b5386dc 599 } else {
3b693d0b 600 unsigned long long blocknr;
e5ea6b14 601
3b5386dc 602 J_ASSERT(obh != NULL);
38d5adf3 603 blocknr = read_tag_block(journal,
3b693d0b 604 tag);
0e8a9560
TT
605
606 /* If the block has been
607 * revoked, then we're all done
608 * here. */
609 if (journal_test_revoke
e5ea6b14 610 (journal, blocknr,
0e8a9560
TT
611 next_commit_ID)) {
612 brelse(obh);
613 ++info->nr_revoke_hits;
614 goto skip_write;
615 }
e5ea6b14 616
d9200380
DW
617 /* Look for block corruption */
618 if (!jbd2_block_tag_csum_verify(
619 journal, tag, obh->b_data,
13af4b93 620 ext2fs_be32_to_cpu(tmp->h_sequence))) {
d9200380 621 brelse(obh);
b4f02c9f 622 success = -EFSBADCRC;
13af4b93 623 printk(KERN_ERR "JBD2: Invalid "
d9200380 624 "checksum recovering "
13af4b93 625 "block %llu in log\n",
d9200380 626 blocknr);
bf93a968
DW
627 block_error = 1;
628 goto skip_write;
d9200380
DW
629 }
630
0e8a9560
TT
631 /* Find a buffer for the new
632 * data being restored */
e5ea6b14
TT
633 nbh = __getblk(journal->j_fs_dev,
634 blocknr,
635 journal->j_blocksize);
3b5386dc 636 if (nbh == NULL) {
e5ea6b14 637 printk(KERN_ERR
13af4b93 638 "JBD2: Out of memory "
3b5386dc
TT
639 "during recovery.\n");
640 err = -ENOMEM;
641 brelse(bh);
642 brelse(obh);
643 goto failed;
644 }
645
3de085dd 646 lock_buffer(nbh);
8cf93332
TT
647 memcpy(nbh->b_data, obh->b_data,
648 journal->j_blocksize);
3b5386dc 649 if (flags & JFS_FLAG_ESCAPE) {
478360f5
TT
650 __u32 magic = ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER);
651 memcpy(nbh->b_data, &magic,
652 sizeof(magic));
3b5386dc 653 }
8cf93332
TT
654
655 BUFFER_TRACE(nbh, "marking dirty");
3de085dd 656 set_buffer_uptodate(nbh);
8cf93332
TT
657 mark_buffer_dirty(nbh);
658 BUFFER_TRACE(nbh, "marking uptodate");
0e8a9560 659 ++info->nr_replays;
53ef44c4 660 /* ll_rw_block(WRITE, 1, &nbh); */
3de085dd 661 unlock_buffer(nbh);
3b5386dc
TT
662 brelse(obh);
663 brelse(nbh);
664 }
e5ea6b14 665
0e8a9560 666 skip_write:
185c4aea 667 tagp += tag_bytes;
3b5386dc
TT
668 if (!(flags & JFS_FLAG_SAME_UUID))
669 tagp += 16;
670
671 if (flags & JFS_FLAG_LAST_TAG)
672 break;
0e8a9560 673 }
e5ea6b14 674
0e8a9560
TT
675 brelse(bh);
676 continue;
8cf93332 677
0e8a9560 678 case JFS_COMMIT_BLOCK:
185c4aea
TT
679 /* How to differentiate between interrupted commit
680 * and journal corruption ?
681 *
682 * {nth transaction}
683 * Checksum Verification Failed
684 * |
685 * ____________________
686 * | |
687 * async_commit sync_commit
688 * | |
689 * | GO TO NEXT "Journal Corruption"
690 * | TRANSACTION
691 * |
055866d8 692 * {(n+1)th transaction}
185c4aea
TT
693 * |
694 * _______|______________
695 * | |
696 * Commit block found Commit block not found
697 * | |
698 * "Journal Corruption" |
699 * _____________|_________
700 * | |
701 * nth trans corrupt OR nth trans
702 * and (n+1)th interrupted interrupted
703 * before commit block
704 * could reach the disk.
705 * (Cannot find the difference in above
706 * mentioned conditions. Hence assume
707 * "Interrupted Commit".)
708 */
709
710 /* Found an expected commit block: if checksums
711 * are present verify them in PASS_SCAN; else not
712 * much to do other than move on to the next sequence
0e8a9560 713 * number. */
185c4aea 714 if (pass == PASS_SCAN &&
86f3b6cf 715 jfs_has_feature_checksum(journal)) {
185c4aea
TT
716 int chksum_err, chksum_seen;
717 struct commit_header *cbh =
718 (struct commit_header *)bh->b_data;
719 unsigned found_chksum =
13af4b93 720 ext2fs_be32_to_cpu(cbh->h_chksum[0]);
185c4aea
TT
721
722 chksum_err = chksum_seen = 0;
723
185c4aea
TT
724 if (info->end_transaction) {
725 journal->j_failed_commit =
726 info->end_transaction;
727 brelse(bh);
728 break;
729 }
730
731 if (crc32_sum == found_chksum &&
13af4b93 732 cbh->h_chksum_type == JFS_CRC32_CHKSUM &&
185c4aea 733 cbh->h_chksum_size ==
13af4b93 734 JFS_CRC32_CHKSUM_SIZE)
185c4aea
TT
735 chksum_seen = 1;
736 else if (!(cbh->h_chksum_type == 0 &&
737 cbh->h_chksum_size == 0 &&
738 found_chksum == 0 &&
739 !chksum_seen))
740 /*
741 * If fs is mounted using an old kernel and then
742 * kernel with journal_chksum is used then we
743 * get a situation where the journal flag has
744 * checksum flag set but checksums are not
745 * present i.e chksum = 0, in the individual
746 * commit blocks.
747 * Hence to avoid checksum failures, in this
748 * situation, this extra check is added.
749 */
750 chksum_err = 1;
751
752 if (chksum_err) {
753 info->end_transaction = next_commit_ID;
13af4b93 754
86f3b6cf 755 if (!jfs_has_feature_async_commit(journal)){
185c4aea
TT
756 journal->j_failed_commit =
757 next_commit_ID;
758 brelse(bh);
759 break;
760 }
761 }
762 crc32_sum = ~0;
763 }
721e065c
DW
764 if (pass == PASS_SCAN &&
765 !jbd2_commit_block_csum_verify(journal,
766 bh->b_data)) {
767 info->end_transaction = next_commit_ID;
768
86f3b6cf 769 if (!jfs_has_feature_async_commit(journal)) {
721e065c
DW
770 journal->j_failed_commit =
771 next_commit_ID;
772 brelse(bh);
773 break;
774 }
775 }
0e8a9560
TT
776 brelse(bh);
777 next_commit_ID++;
778 continue;
3b5386dc 779
0e8a9560
TT
780 case JFS_REVOKE_BLOCK:
781 /* If we aren't in the REVOKE pass, then we can
782 * just skip over this block. */
783 if (pass != PASS_REVOKE) {
784 brelse(bh);
785 continue;
786 }
3b5386dc 787
8cf93332 788 err = scan_revoke_records(journal, bh,
0e8a9560 789 next_commit_ID, info);
3b5386dc 790 brelse(bh);
0e8a9560
TT
791 if (err)
792 goto failed;
793 continue;
794
795 default:
8cf93332 796 jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
0e8a9560 797 blocktype);
2aa362f5 798 brelse(bh);
0e8a9560
TT
799 goto done;
800 }
3b5386dc 801 }
3b5386dc 802
0e8a9560 803 done:
e5ea6b14 804 /*
0e8a9560
TT
805 * We broke out of the log scan loop: either we came to the
806 * known end of the log or we found an unexpected block in the
807 * log. If the latter happened, then we know that the "current"
808 * transaction marks the end of the valid log.
809 */
e5ea6b14 810
185c4aea
TT
811 if (pass == PASS_SCAN) {
812 if (!info->end_transaction)
813 info->end_transaction = next_commit_ID;
814 } else {
0e8a9560
TT
815 /* It's really bad news if different passes end up at
816 * different places (but possible due to IO errors). */
817 if (info->end_transaction != next_commit_ID) {
13af4b93 818 printk(KERN_ERR "JBD2: recovery pass %d ended at "
0e8a9560
TT
819 "transaction %u, expected %u\n",
820 pass, next_commit_ID, info->end_transaction);
821 if (!success)
822 success = -EIO;
823 }
824 }
bf93a968
DW
825 if (block_error && success == 0)
826 success = -EIO;
0e8a9560 827 return success;
3b5386dc 828
0e8a9560 829 failed:
3b5386dc
TT
830 return err;
831}
0e8a9560 832
d35ed158
DW
833static int jbd2_revoke_block_csum_verify(journal_t *j,
834 void *buf)
835{
836 struct journal_revoke_tail *tail;
13af4b93
DW
837 __u32 provided;
838 __u32 calculated;
d35ed158 839
38d5adf3 840 if (!journal_has_csum_v2or3(j))
d35ed158
DW
841 return 1;
842
478360f5 843 tail = (struct journal_revoke_tail *)((char *)buf + j->j_blocksize -
d35ed158
DW
844 sizeof(struct journal_revoke_tail));
845 provided = tail->r_checksum;
846 tail->r_checksum = 0;
13af4b93 847 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
d35ed158
DW
848 tail->r_checksum = provided;
849
13af4b93 850 return provided == ext2fs_cpu_to_be32(calculated);
d35ed158 851}
0e8a9560
TT
852
853/* Scan a revoke record, marking all blocks mentioned as revoked. */
854
e5ea6b14 855static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
0e8a9560
TT
856 tid_t sequence, struct recovery_info *info)
857{
858 journal_revoke_header_t *header;
859 int offset, max;
478360f5 860 unsigned csum_size = 0;
04c66cb2 861 __u32 rcount;
79a4dddb 862 int record_len = 4;
8cf93332 863
0e8a9560
TT
864 header = (journal_revoke_header_t *) bh->b_data;
865 offset = sizeof(journal_revoke_header_t);
04c66cb2 866 rcount = ext2fs_be32_to_cpu(header->r_count);
e5ea6b14 867
d35ed158 868 if (!jbd2_revoke_block_csum_verify(journal, header))
b4f02c9f 869 return -EFSBADCRC;
d35ed158 870
04c66cb2
DW
871 if (journal_has_csum_v2or3(journal))
872 csum_size = sizeof(struct journal_revoke_tail);
873 if (rcount > journal->j_blocksize - csum_size)
874 return -EINVAL;
875 max = rcount;
876
86f3b6cf 877 if (jfs_has_feature_64bit(journal))
79a4dddb
DW
878 record_len = 8;
879
13af4b93 880 while (offset + record_len <= max) {
3b693d0b 881 unsigned long long blocknr;
0e8a9560 882 int err;
e5ea6b14 883
13af4b93
DW
884 if (record_len == 4)
885 blocknr = ext2fs_be32_to_cpu(* ((__u32 *) (bh->b_data+offset)));
886 else
887 blocknr = ext2fs_be64_to_cpu(* ((__u64 *) (bh->b_data+offset)));
79a4dddb 888 offset += record_len;
0e8a9560
TT
889 err = journal_set_revoke(journal, blocknr, sequence);
890 if (err)
891 return err;
892 ++info->nr_revokes;
893 }
894 return 0;
895}