recovery.c 21.6 KB
Newer Older
1
/*
2
 * linux/fs/jbd2/recovery.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
 *
 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
 *
 * This file is part of the Linux kernel and is made available under
 * the terms of the GNU General Public License, version 2, or at your
 * option, any later version, incorporated herein by reference.
 *
 * Journal recovery routines for the generic filesystem journaling code;
 * part of the ext2fs journaling system.
 */

#ifndef __KERNEL__
#include "jfs_user.h"
#else
#include <linux/time.h>
#include <linux/fs.h>
21
#include <linux/jbd2.h>
22
#include <linux/errno.h>
23
#include <linux/crc32.h>
24
#include <linux/blkdev.h>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#endif

/*
 * Maintain information about the progress of the recovery job, so that
 * the different passes can carry information between them.
 */
struct recovery_info
{
	tid_t		start_transaction;
	tid_t		end_transaction;

	int		nr_replays;
	int		nr_revokes;
	int		nr_revoke_hits;
};

enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
static int do_one_pass(journal_t *journal,
				struct recovery_info *info, enum passtype pass);
static int scan_revoke_records(journal_t *, struct buffer_head *,
				tid_t, struct recovery_info *);

#ifdef __KERNEL__

/* Release readahead buffers after use */
static void journal_brelse_array(struct buffer_head *b[], int n)
{
	while (--n >= 0)
		brelse (b[n]);
}


/*
 * When reading from the journal, we are going through the block device
 * layer directly and so there is no readahead being done for us.  We
 * need to implement any readahead ourselves if we want it to happen at
 * all.  Recovery is basically one long sequential read, so make sure we
 * do the IO in reasonably large chunks.
 *
 * This is not so critical that we need to be enormously clever about
 * the readahead size, though.  128K is a purely arbitrary, good-enough
 * fixed value.
 */

#define MAXBUF 8
static int do_readahead(journal_t *journal, unsigned int start)
{
	int err;
	unsigned int max, nbufs, next;
74
	unsigned long long blocknr;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	struct buffer_head *bh;

	struct buffer_head * bufs[MAXBUF];

	/* Do up to 128K of readahead */
	max = start + (128 * 1024 / journal->j_blocksize);
	if (max > journal->j_maxlen)
		max = journal->j_maxlen;

	/* Do the readahead itself.  We'll submit MAXBUF buffer_heads at
	 * a time to the block device IO layer. */

	nbufs = 0;

	for (next = start; next < max; next++) {
90
		err = jbd2_journal_bmap(journal, next, &blocknr);
91 92

		if (err) {
E
Eryu Guan 已提交
93
			printk(KERN_ERR "JBD2: bad block at offset %u\n",
94 95 96 97 98 99 100 101 102 103 104 105 106
				next);
			goto failed;
		}

		bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
		if (!bh) {
			err = -ENOMEM;
			goto failed;
		}

		if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
			bufs[nbufs++] = bh;
			if (nbufs == MAXBUF) {
107
				ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
108 109 110 111 112 113 114 115
				journal_brelse_array(bufs, nbufs);
				nbufs = 0;
			}
		} else
			brelse(bh);
	}

	if (nbufs)
116
		ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	err = 0;

failed:
	if (nbufs)
		journal_brelse_array(bufs, nbufs);
	return err;
}

#endif /* __KERNEL__ */


/*
 * Read a block from the journal
 */

static int jread(struct buffer_head **bhp, journal_t *journal,
		 unsigned int offset)
{
	int err;
136
	unsigned long long blocknr;
137 138 139 140 141
	struct buffer_head *bh;

	*bhp = NULL;

	if (offset >= journal->j_maxlen) {
E
Eryu Guan 已提交
142
		printk(KERN_ERR "JBD2: corrupted journal superblock\n");
143
		return -EFSCORRUPTED;
144 145
	}

146
	err = jbd2_journal_bmap(journal, offset, &blocknr);
147 148

	if (err) {
E
Eryu Guan 已提交
149
		printk(KERN_ERR "JBD2: bad block at offset %u\n",
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
			offset);
		return err;
	}

	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
	if (!bh)
		return -ENOMEM;

	if (!buffer_uptodate(bh)) {
		/* If this is a brand new buffer, start readahead.
                   Otherwise, we assume we are already reading it.  */
		if (!buffer_req(bh))
			do_readahead(journal, offset);
		wait_on_buffer(bh);
	}

	if (!buffer_uptodate(bh)) {
E
Eryu Guan 已提交
167
		printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
168 169 170 171 172 173 174 175 176
			offset);
		brelse(bh);
		return -EIO;
	}

	*bhp = bh;
	return 0;
}

177
static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
178 179
{
	struct jbd2_journal_block_tail *tail;
180 181
	__be32 provided;
	__u32 calculated;
182

183
	if (!jbd2_journal_has_csum_v2or3(j))
184 185 186 187 188 189 190 191 192
		return 1;

	tail = (struct jbd2_journal_block_tail *)(buf + j->j_blocksize -
			sizeof(struct jbd2_journal_block_tail));
	provided = tail->t_checksum;
	tail->t_checksum = 0;
	calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
	tail->t_checksum = provided;

193
	return provided == cpu_to_be32(calculated);
194
}
195 196 197 198 199

/*
 * Count the number of in-use tags in a journal descriptor block.
 */

Z
Zach Brown 已提交
200
static int count_tags(journal_t *journal, struct buffer_head *bh)
201 202 203
{
	char *			tagp;
	journal_block_tag_t *	tag;
Z
Zach Brown 已提交
204 205
	int			nr = 0, size = journal->j_blocksize;
	int			tag_bytes = journal_tag_bytes(journal);
206

207
	if (jbd2_journal_has_csum_v2or3(journal))
208 209
		size -= sizeof(struct jbd2_journal_block_tail);

210 211
	tagp = &bh->b_data[sizeof(journal_header_t)];

Z
Zach Brown 已提交
212
	while ((tagp - bh->b_data + tag_bytes) <= size) {
213 214 215
		tag = (journal_block_tag_t *) tagp;

		nr++;
Z
Zach Brown 已提交
216
		tagp += tag_bytes;
217
		if (!(tag->t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
218 219
			tagp += 16;

220
		if (tag->t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
			break;
	}

	return nr;
}


/* Make sure we wrap around the log correctly! */
#define wrap(journal, var)						\
do {									\
	if (var >= (journal)->j_last)					\
		var -= ((journal)->j_last - (journal)->j_first);	\
} while (0)

/**
236
 * jbd2_journal_recover - recovers a on-disk journal
237 238 239 240 241 242 243 244 245 246
 * @journal: the journal to recover
 *
 * The primary function for recovering the log contents when mounting a
 * journaled device.
 *
 * Recovery is done in three passes.  In the first pass, we look for the
 * end of the log.  In the second, we assemble the list of revoke
 * blocks.  In the third and final pass, we replay any un-revoked blocks
 * in the log.
 */
247
int jbd2_journal_recover(journal_t *journal)
248
{
249
	int			err, err2;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	journal_superblock_t *	sb;

	struct recovery_info	info;

	memset(&info, 0, sizeof(info));
	sb = journal->j_superblock;

	/*
	 * The journal superblock's s_start field (the current log head)
	 * is always zero if, and only if, the journal was cleanly
	 * unmounted.
	 */

	if (!sb->s_start) {
		jbd_debug(1, "No recovery required, last transaction %d\n",
			  be32_to_cpu(sb->s_sequence));
		journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
		return 0;
	}

	err = do_one_pass(journal, &info, PASS_SCAN);
	if (!err)
		err = do_one_pass(journal, &info, PASS_REVOKE);
	if (!err)
		err = do_one_pass(journal, &info, PASS_REPLAY);

E
Eryu Guan 已提交
276
	jbd_debug(1, "JBD2: recovery, exit status %d, "
277 278
		  "recovered transactions %u to %u\n",
		  err, info.start_transaction, info.end_transaction);
E
Eryu Guan 已提交
279
	jbd_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
280 281 282 283 284 285
		  info.nr_replays, info.nr_revoke_hits, info.nr_revokes);

	/* Restart the log at the next transaction ID, thus invalidating
	 * any existing commit records in the log. */
	journal->j_transaction_sequence = ++info.end_transaction;

286
	jbd2_journal_clear_revoke(journal);
287 288 289
	err2 = sync_blockdev(journal->j_fs_dev);
	if (!err)
		err = err2;
290
	/* Make sure all replayed data is on permanent storage */
291 292 293 294 295
	if (journal->j_flags & JBD2_BARRIER) {
		err2 = blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL);
		if (!err)
			err = err2;
	}
296 297 298 299
	return err;
}

/**
300
 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
301 302 303 304 305
 * @journal: journal to startup
 *
 * Locate any valid recovery information from the journal and set up the
 * journal structures in memory to ignore it (presumably because the
 * caller has evidence that it is out of date).
306
 * This function doesn't appear to be exported..
307 308 309 310 311
 *
 * We perform one pass over the journal to allow us to tell the user how
 * much recovery information is being erased, and to let us initialise
 * the journal transaction sequence numbers to the next unused ID.
 */
312
int jbd2_journal_skip_recovery(journal_t *journal)
313 314 315 316 317 318 319 320 321 322
{
	int			err;

	struct recovery_info	info;

	memset (&info, 0, sizeof(info));

	err = do_one_pass(journal, &info, PASS_SCAN);

	if (err) {
E
Eryu Guan 已提交
323
		printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
324 325
		++journal->j_transaction_sequence;
	} else {
326
#ifdef CONFIG_JBD2_DEBUG
327 328
		int dropped = info.end_transaction - 
			be32_to_cpu(journal->j_superblock->s_sequence);
M
Mingming Cao 已提交
329
		jbd_debug(1,
E
Eryu Guan 已提交
330
			  "JBD2: ignoring %d transaction%s from the journal.\n",
331
			  dropped, (dropped == 1) ? "" : "s");
332
#endif
333 334 335 336 337 338 339
		journal->j_transaction_sequence = ++info.end_transaction;
	}

	journal->j_tail = 0;
	return err;
}

340 341
static inline unsigned long long read_tag_block(journal_t *journal,
						journal_block_tag_t *tag)
Z
Zach Brown 已提交
342
{
343
	unsigned long long block = be32_to_cpu(tag->t_blocknr);
344
	if (jbd2_has_feature_64bit(journal))
Z
Zach Brown 已提交
345 346 347 348
		block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
	return block;
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/*
 * calc_chksums calculates the checksums for the blocks described in the
 * descriptor block.
 */
static int calc_chksums(journal_t *journal, struct buffer_head *bh,
			unsigned long *next_log_block, __u32 *crc32_sum)
{
	int i, num_blks, err;
	unsigned long io_block;
	struct buffer_head *obh;

	num_blks = count_tags(journal, bh);
	/* Calculate checksum of the descriptor block. */
	*crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);

	for (i = 0; i < num_blks; i++) {
		io_block = (*next_log_block)++;
		wrap(journal, *next_log_block);
		err = jread(&obh, journal, io_block);
		if (err) {
E
Eryu Guan 已提交
369
			printk(KERN_ERR "JBD2: IO error %d recovering block "
370 371 372 373 374 375
				"%lu in log\n", err, io_block);
			return 1;
		} else {
			*crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
				     obh->b_size);
		}
376
		put_bh(obh);
377 378 379 380
	}
	return 0;
}

D
Darrick J. Wong 已提交
381 382 383
static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
{
	struct commit_header *h;
384 385
	__be32 provided;
	__u32 calculated;
D
Darrick J. Wong 已提交
386

387
	if (!jbd2_journal_has_csum_v2or3(j))
D
Darrick J. Wong 已提交
388 389 390 391 392 393 394 395
		return 1;

	h = buf;
	provided = h->h_chksum[0];
	h->h_chksum[0] = 0;
	calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
	h->h_chksum[0] = provided;

396
	return provided == cpu_to_be32(calculated);
D
Darrick J. Wong 已提交
397 398
}

399 400 401
static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
				      void *buf, __u32 sequence)
{
402
	journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
403
	__u32 csum32;
404
	__be32 seq;
405

406
	if (!jbd2_journal_has_csum_v2or3(j))
407 408
		return 1;

409 410
	seq = cpu_to_be32(sequence);
	csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
411
	csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
412

413
	if (jbd2_has_feature_csum3(j))
414 415 416
		return tag3->t_checksum == cpu_to_be32(csum32);
	else
		return tag->t_checksum == cpu_to_be16(csum32);
417 418
}

419 420 421 422 423 424 425 426 427 428 429
static int do_one_pass(journal_t *journal,
			struct recovery_info *info, enum passtype pass)
{
	unsigned int		first_commit_ID, next_commit_ID;
	unsigned long		next_log_block;
	int			err, success = 0;
	journal_superblock_t *	sb;
	journal_header_t *	tmp;
	struct buffer_head *	bh;
	unsigned int		sequence;
	int			blocktype;
Z
Zach Brown 已提交
430
	int			tag_bytes = journal_tag_bytes(journal);
431
	__u32			crc32_sum = ~0; /* Transactional Checksums */
432
	int			descr_csum_size = 0;
433
	int			block_error = 0;
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

	/*
	 * First thing is to establish what we expect to find in the log
	 * (in terms of transaction IDs), and where (in terms of log
	 * block offsets): query the superblock.
	 */

	sb = journal->j_superblock;
	next_commit_ID = be32_to_cpu(sb->s_sequence);
	next_log_block = be32_to_cpu(sb->s_start);

	first_commit_ID = next_commit_ID;
	if (pass == PASS_SCAN)
		info->start_transaction = first_commit_ID;

	jbd_debug(1, "Starting recovery pass %d\n", pass);

	/*
	 * Now we walk through the log, transaction by transaction,
	 * making sure that each transaction has a commit block in the
	 * expected place.  Each complete transaction gets replayed back
	 * into the main filesystem.
	 */

	while (1) {
		int			flags;
		char *			tagp;
		journal_block_tag_t *	tag;
		struct buffer_head *	obh;
		struct buffer_head *	nbh;

465
		cond_resched();
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

		/* If we already know where to stop the log traversal,
		 * check right now that we haven't gone past the end of
		 * the log. */

		if (pass != PASS_SCAN)
			if (tid_geq(next_commit_ID, info->end_transaction))
				break;

		jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
			  next_commit_ID, next_log_block, journal->j_last);

		/* Skip over each chunk of the transaction looking
		 * either the next descriptor block or the final commit
		 * record. */

E
Eryu Guan 已提交
482
		jbd_debug(3, "JBD2: checking block %ld\n", next_log_block);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
		err = jread(&bh, journal, next_log_block);
		if (err)
			goto failed;

		next_log_block++;
		wrap(journal, next_log_block);

		/* What kind of buffer is it?
		 *
		 * If it is a descriptor block, check that it has the
		 * expected sequence number.  Otherwise, we're all done
		 * here. */

		tmp = (journal_header_t *)bh->b_data;

498
		if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
			brelse(bh);
			break;
		}

		blocktype = be32_to_cpu(tmp->h_blocktype);
		sequence = be32_to_cpu(tmp->h_sequence);
		jbd_debug(3, "Found magic %d, sequence %d\n",
			  blocktype, sequence);

		if (sequence != next_commit_ID) {
			brelse(bh);
			break;
		}

		/* OK, we have a valid descriptor block which matches
		 * all of the sequence number checks.  What are we going
		 * to do with it?  That depends on the pass... */

		switch(blocktype) {
518
		case JBD2_DESCRIPTOR_BLOCK:
519
			/* Verify checksum first */
520
			if (jbd2_journal_has_csum_v2or3(journal))
521 522 523
				descr_csum_size =
					sizeof(struct jbd2_journal_block_tail);
			if (descr_csum_size > 0 &&
524 525
			    !jbd2_descriptor_block_csum_verify(journal,
							       bh->b_data)) {
526 527 528
				printk(KERN_ERR "JBD2: Invalid checksum "
				       "recovering block %lu in log\n",
				       next_log_block);
529
				err = -EFSBADCRC;
530
				brelse(bh);
531 532 533
				goto failed;
			}

534
			/* If it is a valid descriptor block, replay it
535 536 537
			 * in pass REPLAY; if journal_checksums enabled, then
			 * calculate checksums in PASS_SCAN, otherwise,
			 * just skip over the blocks it describes. */
538
			if (pass != PASS_REPLAY) {
539
				if (pass == PASS_SCAN &&
540
				    jbd2_has_feature_checksum(journal) &&
541 542 543 544 545 546 547 548 549 550
				    !info->end_transaction) {
					if (calc_chksums(journal, bh,
							&next_log_block,
							&crc32_sum)) {
						put_bh(bh);
						break;
					}
					put_bh(bh);
					continue;
				}
Z
Zach Brown 已提交
551
				next_log_block += count_tags(journal, bh);
552
				wrap(journal, next_log_block);
553
				put_bh(bh);
554 555 556 557 558 559 560 561
				continue;
			}

			/* A descriptor block: we can now write all of
			 * the data blocks.  Yay, useful work is finally
			 * getting done here! */

			tagp = &bh->b_data[sizeof(journal_header_t)];
Z
Zach Brown 已提交
562
			while ((tagp - bh->b_data + tag_bytes)
563
			       <= journal->j_blocksize - descr_csum_size) {
564 565 566
				unsigned long io_block;

				tag = (journal_block_tag_t *) tagp;
567
				flags = be16_to_cpu(tag->t_flags);
568 569 570 571 572 573 574 575

				io_block = next_log_block++;
				wrap(journal, next_log_block);
				err = jread(&obh, journal, io_block);
				if (err) {
					/* Recover what we can, but
					 * report failure at the end. */
					success = err;
E
Eryu Guan 已提交
576 577
					printk(KERN_ERR
						"JBD2: IO error %d recovering "
578 579 580
						"block %ld in log\n",
						err, io_block);
				} else {
581
					unsigned long long blocknr;
582 583

					J_ASSERT(obh != NULL);
584
					blocknr = read_tag_block(journal,
Z
Zach Brown 已提交
585
								 tag);
586 587 588 589

					/* If the block has been
					 * revoked, then we're all done
					 * here. */
590
					if (jbd2_journal_test_revoke
591 592 593 594 595 596 597
					    (journal, blocknr,
					     next_commit_ID)) {
						brelse(obh);
						++info->nr_revoke_hits;
						goto skip_write;
					}

598 599 600 601 602
					/* Look for block corruption */
					if (!jbd2_block_tag_csum_verify(
						journal, tag, obh->b_data,
						be32_to_cpu(tmp->h_sequence))) {
						brelse(obh);
603
						success = -EFSBADCRC;
604
						printk(KERN_ERR "JBD2: Invalid "
605 606 607
						       "checksum recovering "
						       "block %llu in log\n",
						       blocknr);
608 609
						block_error = 1;
						goto skip_write;
610 611
					}

612 613 614 615 616 617 618
					/* Find a buffer for the new
					 * data being restored */
					nbh = __getblk(journal->j_fs_dev,
							blocknr,
							journal->j_blocksize);
					if (nbh == NULL) {
						printk(KERN_ERR
E
Eryu Guan 已提交
619
						       "JBD2: Out of memory "
620 621 622 623 624 625 626 627 628 629
						       "during recovery.\n");
						err = -ENOMEM;
						brelse(bh);
						brelse(obh);
						goto failed;
					}

					lock_buffer(nbh);
					memcpy(nbh->b_data, obh->b_data,
							journal->j_blocksize);
630
					if (flags & JBD2_FLAG_ESCAPE) {
631
						*((__be32 *)nbh->b_data) =
632
						cpu_to_be32(JBD2_MAGIC_NUMBER);
633 634 635 636 637 638 639 640 641 642 643 644 645 646
					}

					BUFFER_TRACE(nbh, "marking dirty");
					set_buffer_uptodate(nbh);
					mark_buffer_dirty(nbh);
					BUFFER_TRACE(nbh, "marking uptodate");
					++info->nr_replays;
					/* ll_rw_block(WRITE, 1, &nbh); */
					unlock_buffer(nbh);
					brelse(obh);
					brelse(nbh);
				}

			skip_write:
Z
Zach Brown 已提交
647
				tagp += tag_bytes;
648
				if (!(flags & JBD2_FLAG_SAME_UUID))
649 650
					tagp += 16;

651
				if (flags & JBD2_FLAG_LAST_TAG)
652 653 654 655 656 657
					break;
			}

			brelse(bh);
			continue;

658
		case JBD2_COMMIT_BLOCK:
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
			/*     How to differentiate between interrupted commit
			 *               and journal corruption ?
			 *
			 * {nth transaction}
			 *        Checksum Verification Failed
			 *			 |
			 *		 ____________________
			 *		|		     |
			 * 	async_commit             sync_commit
			 *     		|                    |
			 *		| GO TO NEXT    "Journal Corruption"
			 *		| TRANSACTION
			 *		|
			 * {(n+1)th transanction}
			 *		|
			 * 	 _______|______________
			 * 	|	 	      |
			 * Commit block found	Commit block not found
			 *      |		      |
			 * "Journal Corruption"       |
			 *		 _____________|_________
			 *     		|	           	|
			 *	nth trans corrupt	OR   nth trans
			 *	and (n+1)th interrupted     interrupted
			 *	before commit block
			 *      could reach the disk.
			 *	(Cannot find the difference in above
			 *	 mentioned conditions. Hence assume
			 *	 "Interrupted Commit".)
			 */

			/* Found an expected commit block: if checksums
			 * are present verify them in PASS_SCAN; else not
			 * much to do other than move on to the next sequence
693
			 * number. */
694
			if (pass == PASS_SCAN &&
695
			    jbd2_has_feature_checksum(journal)) {
696 697 698 699 700 701 702 703 704
				int chksum_err, chksum_seen;
				struct commit_header *cbh =
					(struct commit_header *)bh->b_data;
				unsigned found_chksum =
					be32_to_cpu(cbh->h_chksum[0]);

				chksum_err = chksum_seen = 0;

				if (info->end_transaction) {
705 706
					journal->j_failed_commit =
						info->end_transaction;
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
					brelse(bh);
					break;
				}

				if (crc32_sum == found_chksum &&
				    cbh->h_chksum_type == JBD2_CRC32_CHKSUM &&
				    cbh->h_chksum_size ==
						JBD2_CRC32_CHKSUM_SIZE)
				       chksum_seen = 1;
				else if (!(cbh->h_chksum_type == 0 &&
					     cbh->h_chksum_size == 0 &&
					     found_chksum == 0 &&
					     !chksum_seen))
				/*
				 * If fs is mounted using an old kernel and then
				 * kernel with journal_chksum is used then we
				 * get a situation where the journal flag has
				 * checksum flag set but checksums are not
				 * present i.e chksum = 0, in the individual
				 * commit blocks.
				 * Hence to avoid checksum failures, in this
				 * situation, this extra check is added.
				 */
						chksum_err = 1;

				if (chksum_err) {
					info->end_transaction = next_commit_ID;

735
					if (!jbd2_has_feature_async_commit(journal)) {
736 737
						journal->j_failed_commit =
							next_commit_ID;
738 739 740 741 742 743
						brelse(bh);
						break;
					}
				}
				crc32_sum = ~0;
			}
D
Darrick J. Wong 已提交
744 745 746 747 748
			if (pass == PASS_SCAN &&
			    !jbd2_commit_block_csum_verify(journal,
							   bh->b_data)) {
				info->end_transaction = next_commit_ID;

749
				if (!jbd2_has_feature_async_commit(journal)) {
D
Darrick J. Wong 已提交
750 751 752 753 754 755
					journal->j_failed_commit =
						next_commit_ID;
					brelse(bh);
					break;
				}
			}
756 757 758 759
			brelse(bh);
			next_commit_ID++;
			continue;

760
		case JBD2_REVOKE_BLOCK:
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
			/* If we aren't in the REVOKE pass, then we can
			 * just skip over this block. */
			if (pass != PASS_REVOKE) {
				brelse(bh);
				continue;
			}

			err = scan_revoke_records(journal, bh,
						  next_commit_ID, info);
			brelse(bh);
			if (err)
				goto failed;
			continue;

		default:
			jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
				  blocktype);
			brelse(bh);
			goto done;
		}
	}

 done:
	/*
	 * We broke out of the log scan loop: either we came to the
	 * known end of the log or we found an unexpected block in the
	 * log.  If the latter happened, then we know that the "current"
	 * transaction marks the end of the valid log.
	 */

791 792 793 794
	if (pass == PASS_SCAN) {
		if (!info->end_transaction)
			info->end_transaction = next_commit_ID;
	} else {
795 796 797
		/* It's really bad news if different passes end up at
		 * different places (but possible due to IO errors). */
		if (info->end_transaction != next_commit_ID) {
E
Eryu Guan 已提交
798
			printk(KERN_ERR "JBD2: recovery pass %d ended at "
799 800 801 802 803 804
				"transaction %u, expected %u\n",
				pass, next_commit_ID, info->end_transaction);
			if (!success)
				success = -EIO;
		}
	}
805 806
	if (block_error && success == 0)
		success = -EIO;
807 808 809 810 811 812 813 814 815 816 817
	return success;

 failed:
	return err;
}

/* Scan a revoke record, marking all blocks mentioned as revoked. */

static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
			       tid_t sequence, struct recovery_info *info)
{
818
	jbd2_journal_revoke_header_t *header;
819
	int offset, max;
820 821
	int csum_size = 0;
	__u32 rcount;
Z
Zach Brown 已提交
822
	int record_len = 4;
823

824 825
	header = (jbd2_journal_revoke_header_t *) bh->b_data;
	offset = sizeof(jbd2_journal_revoke_header_t);
826
	rcount = be32_to_cpu(header->r_count);
827

828
	if (!jbd2_descriptor_block_csum_verify(journal, header))
829
		return -EFSBADCRC;
830

831
	if (jbd2_journal_has_csum_v2or3(journal))
832
		csum_size = sizeof(struct jbd2_journal_block_tail);
833 834 835 836
	if (rcount > journal->j_blocksize - csum_size)
		return -EINVAL;
	max = rcount;

837
	if (jbd2_has_feature_64bit(journal))
Z
Zach Brown 已提交
838 839 840
		record_len = 8;

	while (offset + record_len <= max) {
841
		unsigned long long blocknr;
842 843
		int err;

Z
Zach Brown 已提交
844 845 846 847 848
		if (record_len == 4)
			blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
		else
			blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
		offset += record_len;
849
		err = jbd2_journal_set_revoke(journal, blocknr, sequence);
850 851 852 853 854 855
		if (err)
			return err;
		++info->nr_revokes;
	}
	return 0;
}