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