recovery.c revision e3507739e4185bdb2394928eb6479e48f4e690a8
1/*
2 * linux/fs/jbd/recovery.c
3 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
5 *
6 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
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;
13 * part of the ext2fs journaling system.
14 */
15
16#ifndef __KERNEL__
17#include "config.h"
18#include "jfs_user.h"
19#else
20#include <linux/time.h>
21#include <linux/fs.h>
22#include <linux/jbd.h>
23#include <linux/errno.h>
24#include <linux/slab.h>
25#endif
26
27/*
28 * Maintain information about the progress of the recovery job, so that
29 * the different passes can carry information between them.
30 */
31struct recovery_info
32{
33	tid_t		start_transaction;
34	tid_t		end_transaction;
35
36	int		nr_replays;
37	int		nr_revokes;
38	int		nr_revoke_hits;
39};
40
41enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
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 *);
46
47#ifdef __KERNEL__
48
49/* Release readahead buffers after use */
50static void journal_brelse_array(struct buffer_head *b[], int n)
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
69#define MAXBUF 8
70static int do_readahead(journal_t *journal, unsigned int start)
71{
72	int err;
73	unsigned int max, nbufs, next;
74	unsigned long long blocknr;
75	struct buffer_head *bh;
76
77	struct buffer_head * bufs[MAXBUF];
78
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. */
86
87	nbufs = 0;
88
89	for (next = start; next < max; next++) {
90		err = journal_bmap(journal, next, &blocknr);
91
92		if (err) {
93			printk (KERN_ERR "JBD: bad block at offset %u\n",
94				next);
95			goto failed;
96		}
97
98		bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
99		if (!bh) {
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);
108				journal_brelse_array(bufs, nbufs);
109				nbufs = 0;
110			}
111		} else
112			brelse(bh);
113	}
114
115	if (nbufs)
116		ll_rw_block(READ, nbufs, bufs);
117	err = 0;
118
119failed:
120	if (nbufs)
121		journal_brelse_array(bufs, nbufs);
122	return err;
123}
124
125#endif /* __KERNEL__ */
126
127
128/*
129 * Read a block from the journal
130 */
131
132static int jread(struct buffer_head **bhp, journal_t *journal,
133		 unsigned int offset)
134{
135	int err;
136	unsigned long long blocknr;
137	struct buffer_head *bh;
138
139	*bhp = NULL;
140
141	if (offset >= journal->j_maxlen) {
142		printk(KERN_ERR "JBD: corrupted journal superblock\n");
143		return -EIO;
144	}
145
146	err = journal_bmap(journal, offset, &blocknr);
147
148	if (err) {
149		printk (KERN_ERR "JBD: bad block at offset %u\n",
150			offset);
151		return err;
152	}
153
154	bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
155	if (!bh)
156		return -ENOMEM;
157
158	if (!buffer_uptodate(bh)) {
159		/* If this is a brand new buffer, start readahead.
160                   Otherwise, we assume we are already reading it.  */
161		if (!buffer_req(bh))
162			do_readahead(journal, offset);
163		wait_on_buffer(bh);
164	}
165
166	if (!buffer_uptodate(bh)) {
167		printk (KERN_ERR "JBD: Failed to read block at offset %u\n",
168			offset);
169		brelse(bh);
170		return -EIO;
171	}
172
173	*bhp = bh;
174	return 0;
175}
176
177
178/*
179 * Count the number of in-use tags in a journal descriptor block.
180 */
181
182static int count_tags(journal_t *journal, struct buffer_head *bh)
183{
184	char *			tagp;
185	journal_block_tag_t *	tag;
186	int			nr = 0, size = journal->j_blocksize;
187	int			tag_bytes = journal_tag_bytes(journal);
188
189	tagp = &bh->b_data[sizeof(journal_header_t)];
190
191	while ((tagp - bh->b_data + tag_bytes) <= size) {
192		tag = (journal_block_tag_t *) tagp;
193
194		nr++;
195		tagp += tag_bytes;
196		if (!(tag->t_flags & cpu_to_be32(JFS_FLAG_SAME_UUID)))
197			tagp += 16;
198
199		if (tag->t_flags & cpu_to_be32(JFS_FLAG_LAST_TAG))
200			break;
201	}
202
203	return nr;
204}
205
206
207/* Make sure we wrap around the log correctly! */
208#define wrap(journal, var)						\
209do {									\
210	if (var >= (journal)->j_last)					\
211		var -= ((journal)->j_last - (journal)->j_first);	\
212} while (0)
213
214/**
215 * journal_recover - recovers a on-disk journal
216 * @journal: the journal to recover
217 *
218 * The primary function for recovering the log contents when mounting a
219 * journaled device.
220 *
221 * Recovery is done in three passes.  In the first pass, we look for the
222 * end of the log.  In the second, we assemble the list of revoke
223 * blocks.  In the third and final pass, we replay any un-revoked blocks
224 * in the log.
225 */
226int journal_recover(journal_t *journal)
227{
228	int			err;
229	journal_superblock_t *	sb;
230
231	struct recovery_info	info;
232
233	memset(&info, 0, sizeof(info));
234	sb = journal->j_superblock;
235
236	/*
237	 * The journal superblock's s_start field (the current log head)
238	 * is always zero if, and only if, the journal was cleanly
239	 * unmounted.
240	 */
241
242	if (!sb->s_start) {
243		jbd_debug(1, "No recovery required, last transaction %d\n",
244			  be32_to_cpu(sb->s_sequence));
245		journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
246		return 0;
247	}
248
249	err = do_one_pass(journal, &info, PASS_SCAN);
250	if (!err)
251		err = do_one_pass(journal, &info, PASS_REVOKE);
252	if (!err)
253		err = do_one_pass(journal, &info, PASS_REPLAY);
254
255	jbd_debug(1, "JBD: recovery, exit status %d, "
256		  "recovered transactions %u to %u\n",
257		  err, info.start_transaction, info.end_transaction);
258	jbd_debug(1, "JBD: Replayed %d and revoked %d/%d blocks\n",
259		  info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
260
261	/* Restart the log at the next transaction ID, thus invalidating
262	 * any existing commit records in the log. */
263	journal->j_transaction_sequence = ++info.end_transaction;
264
265	journal_clear_revoke(journal);
266	sync_blockdev(journal->j_fs_dev);
267	return err;
268}
269
270/**
271 * journal_skip_recovery - Start journal and wipe exiting records
272 * @journal: journal to startup
273 *
274 * Locate any valid recovery information from the journal and set up the
275 * journal structures in memory to ignore it (presumably because the
276 * caller has evidence that it is out of date).
277 * This function does'nt appear to be exorted..
278 *
279 * We perform one pass over the journal to allow us to tell the user how
280 * much recovery information is being erased, and to let us initialise
281 * the journal transaction sequence numbers to the next unused ID.
282 */
283int journal_skip_recovery(journal_t *journal)
284{
285	int			err;
286	struct recovery_info	info;
287
288	memset (&info, 0, sizeof(info));
289
290	err = do_one_pass(journal, &info, PASS_SCAN);
291
292	if (err) {
293		printk(KERN_ERR "JBD: error %d scanning journal\n", err);
294		++journal->j_transaction_sequence;
295	} else {
296#ifdef CONFIG_JBD_DEBUG
297		int dropped = info.end_transaction - be32_to_cpu(sb->s_sequence);
298#endif
299		jbd_debug(1,
300			  "JBD: ignoring %d transaction%s from the journal.\n",
301			  dropped, (dropped == 1) ? "" : "s");
302		journal->j_transaction_sequence = ++info.end_transaction;
303	}
304
305	journal->j_tail = 0;
306	return err;
307}
308
309static inline unsigned long long read_tag_block(int tag_bytes, journal_block_tag_t *tag)
310{
311	unsigned long long block = be32_to_cpu(tag->t_blocknr);
312	if (tag_bytes > JBD_TAG_SIZE32)
313		block |= (__u64)be32_to_cpu(tag->t_blocknr_high) << 32;
314	return block;
315}
316
317/*
318 * calc_chksums calculates the checksums for the blocks described in the
319 * descriptor block.
320 */
321static int calc_chksums(journal_t *journal, struct buffer_head *bh,
322			unsigned long long *next_log_block, __u32 *crc32_sum)
323{
324	int i, num_blks, err;
325	unsigned long long io_block;
326	struct buffer_head *obh;
327
328	num_blks = count_tags(journal, bh);
329	/* Calculate checksum of the descriptor block. */
330	*crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
331
332	for (i = 0; i < num_blks; i++) {
333		io_block = (*next_log_block)++;
334		wrap(journal, *next_log_block);
335		err = jread(&obh, journal, io_block);
336		if (err) {
337			printk(KERN_ERR "JBD: IO error %d recovering block "
338				"%llu in log\n", err, io_block);
339			return 1;
340		} else {
341			*crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
342				     obh->b_size);
343		}
344		brelse(obh);
345	}
346	return 0;
347}
348
349static int do_one_pass(journal_t *journal,
350			struct recovery_info *info, enum passtype pass)
351{
352	unsigned int		first_commit_ID, next_commit_ID;
353	unsigned long long	next_log_block;
354	int			err, success = 0;
355	journal_superblock_t *	sb;
356	journal_header_t *	tmp;
357	struct buffer_head *	bh;
358	unsigned int		sequence;
359	int			blocktype;
360	int			tag_bytes = journal_tag_bytes(journal);
361	__u32			crc32_sum = ~0; /* Transactional Checksums */
362
363	/*
364	 * First thing is to establish what we expect to find in the log
365	 * (in terms of transaction IDs), and where (in terms of log
366	 * block offsets): query the superblock.
367	 */
368
369	sb = journal->j_superblock;
370	next_commit_ID = be32_to_cpu(sb->s_sequence);
371	next_log_block = be32_to_cpu(sb->s_start);
372
373	first_commit_ID = next_commit_ID;
374	if (pass == PASS_SCAN)
375		info->start_transaction = first_commit_ID;
376
377	jbd_debug(1, "Starting recovery pass %d\n", pass);
378
379	/*
380	 * Now we walk through the log, transaction by transaction,
381	 * making sure that each transaction has a commit block in the
382	 * expected place.  Each complete transaction gets replayed back
383	 * into the main filesystem.
384	 */
385
386	while (1) {
387		int			flags;
388		char *			tagp;
389		journal_block_tag_t *	tag;
390		struct buffer_head *	obh;
391		struct buffer_head *	nbh;
392
393		cond_resched();
394
395		/* If we already know where to stop the log traversal,
396		 * check right now that we haven't gone past the end of
397		 * the log. */
398
399		if (pass != PASS_SCAN)
400			if (tid_geq(next_commit_ID, info->end_transaction))
401				break;
402
403		jbd_debug(2, "Scanning for sequence ID %u at %llu/%lu\n",
404			  next_commit_ID, next_log_block, journal->j_last);
405
406		/* Skip over each chunk of the transaction looking
407		 * either the next descriptor block or the final commit
408		 * record. */
409
410		jbd_debug(3, "JBD: checking block %llu\n", next_log_block);
411		err = jread(&bh, journal, next_log_block);
412		if (err)
413			goto failed;
414
415		next_log_block++;
416		wrap(journal, next_log_block);
417
418		/* What kind of buffer is it?
419		 *
420		 * If it is a descriptor block, check that it has the
421		 * expected sequence number.  Otherwise, we're all done
422		 * here. */
423
424		tmp = (journal_header_t *)bh->b_data;
425
426		if (tmp->h_magic != cpu_to_be32(JFS_MAGIC_NUMBER)) {
427			brelse(bh);
428			break;
429		}
430
431		blocktype = be32_to_cpu(tmp->h_blocktype);
432		sequence = be32_to_cpu(tmp->h_sequence);
433		jbd_debug(3, "Found magic %d, sequence %d\n",
434			  blocktype, sequence);
435
436		if (sequence != next_commit_ID) {
437			brelse(bh);
438			break;
439		}
440
441		/* OK, we have a valid descriptor block which matches
442		 * all of the sequence number checks.  What are we going
443		 * to do with it?  That depends on the pass... */
444
445		switch(blocktype) {
446		case JFS_DESCRIPTOR_BLOCK:
447			/* If it is a valid descriptor block, replay it
448			 * in pass REPLAY; if journal_checksums enabled, then
449			 * calculate checksums in PASS_SCAN, otherwise,
450			 * just skip over the blocks it describes. */
451			if (pass != PASS_REPLAY) {
452				if (pass == PASS_SCAN &&
453				    JFS_HAS_COMPAT_FEATURE(journal,
454					    JFS_FEATURE_COMPAT_CHECKSUM) &&
455				    !info->end_transaction) {
456					if (calc_chksums(journal, bh,
457							&next_log_block,
458							&crc32_sum)) {
459						brelse(bh);
460						break;
461					}
462					brelse(bh);
463					continue;
464				}
465				next_log_block += count_tags(journal, bh);
466				wrap(journal, next_log_block);
467				brelse(bh);
468				continue;
469			}
470
471			/* A descriptor block: we can now write all of
472			 * the data blocks.  Yay, useful work is finally
473			 * getting done here! */
474
475			tagp = &bh->b_data[sizeof(journal_header_t)];
476			while ((tagp - bh->b_data + tag_bytes)
477			       <= journal->j_blocksize) {
478				unsigned long long io_block;
479
480				tag = (journal_block_tag_t *) tagp;
481				flags = be32_to_cpu(tag->t_flags);
482
483				io_block = next_log_block++;
484				wrap(journal, next_log_block);
485				err = jread(&obh, journal, io_block);
486				if (err) {
487					/* Recover what we can, but
488					 * report failure at the end. */
489					success = err;
490					printk (KERN_ERR
491						"JBD: IO error %d recovering "
492						"block %llu in log\n",
493						err, io_block);
494				} else {
495					unsigned long long blocknr;
496
497					J_ASSERT(obh != NULL);
498					blocknr = read_tag_block(tag_bytes,
499								 tag);
500
501					/* If the block has been
502					 * revoked, then we're all done
503					 * here. */
504					if (journal_test_revoke
505					    (journal, blocknr,
506					     next_commit_ID)) {
507						brelse(obh);
508						++info->nr_revoke_hits;
509						goto skip_write;
510					}
511
512					/* Find a buffer for the new
513					 * data being restored */
514					nbh = __getblk(journal->j_fs_dev,
515							blocknr,
516							journal->j_blocksize);
517					if (nbh == NULL) {
518						printk(KERN_ERR
519						       "JBD: Out of memory "
520						       "during recovery.\n");
521						err = -ENOMEM;
522						brelse(bh);
523						brelse(obh);
524						goto failed;
525					}
526
527					lock_buffer(nbh);
528					memcpy(nbh->b_data, obh->b_data,
529							journal->j_blocksize);
530					if (flags & JFS_FLAG_ESCAPE) {
531						journal_header_t *header;
532
533						header = (journal_header_t *) &nbh->b_data[0];
534						header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
535					}
536
537					BUFFER_TRACE(nbh, "marking dirty");
538					set_buffer_uptodate(nbh);
539					mark_buffer_dirty(nbh);
540					BUFFER_TRACE(nbh, "marking uptodate");
541					++info->nr_replays;
542					/* ll_rw_block(WRITE, 1, &nbh); */
543					unlock_buffer(nbh);
544					brelse(obh);
545					brelse(nbh);
546				}
547
548			skip_write:
549				tagp += tag_bytes;
550				if (!(flags & JFS_FLAG_SAME_UUID))
551					tagp += 16;
552
553				if (flags & JFS_FLAG_LAST_TAG)
554					break;
555			}
556
557			brelse(bh);
558			continue;
559
560		case JFS_COMMIT_BLOCK:
561			jbd_debug(3, "Commit block for #%u found\n",
562				  next_commit_ID);
563			/*     How to differentiate between interrupted commit
564			 *               and journal corruption ?
565			 *
566			 * {nth transaction}
567			 *        Checksum Verification Failed
568			 *			 |
569			 *		 ____________________
570			 *		|		     |
571			 * 	async_commit             sync_commit
572			 *     		|                    |
573			 *		| GO TO NEXT    "Journal Corruption"
574			 *		| TRANSACTION
575			 *		|
576			 * {(n+1)th transanction}
577			 *		|
578			 * 	 _______|______________
579			 * 	|	 	      |
580			 * Commit block found	Commit block not found
581			 *      |		      |
582			 * "Journal Corruption"       |
583			 *		 _____________|_________
584			 *     		|	           	|
585			 *	nth trans corrupt	OR   nth trans
586			 *	and (n+1)th interrupted     interrupted
587			 *	before commit block
588			 *      could reach the disk.
589			 *	(Cannot find the difference in above
590			 *	 mentioned conditions. Hence assume
591			 *	 "Interrupted Commit".)
592			 */
593
594			/* Found an expected commit block: if checksums
595			 * are present verify them in PASS_SCAN; else not
596			 * much to do other than move on to the next sequence
597			 * number. */
598			if (pass == PASS_SCAN &&
599			    JFS_HAS_COMPAT_FEATURE(journal,
600				    JFS_FEATURE_COMPAT_CHECKSUM)) {
601				int chksum_err, chksum_seen;
602				struct commit_header *cbh =
603					(struct commit_header *)bh->b_data;
604				unsigned found_chksum =
605					be32_to_cpu(cbh->h_chksum[0]);
606
607				chksum_err = chksum_seen = 0;
608
609				jbd_debug(3, "Checksums %x %x\n",
610					  crc32_sum, found_chksum);
611				if (info->end_transaction) {
612					journal->j_failed_commit =
613						info->end_transaction;
614					brelse(bh);
615					break;
616				}
617
618				if (crc32_sum == found_chksum &&
619				    cbh->h_chksum_type == JBD2_CRC32_CHKSUM &&
620				    cbh->h_chksum_size ==
621						JBD2_CRC32_CHKSUM_SIZE)
622				       chksum_seen = 1;
623				else if (!(cbh->h_chksum_type == 0 &&
624					     cbh->h_chksum_size == 0 &&
625					     found_chksum == 0 &&
626					     !chksum_seen))
627				/*
628				 * If fs is mounted using an old kernel and then
629				 * kernel with journal_chksum is used then we
630				 * get a situation where the journal flag has
631				 * checksum flag set but checksums are not
632				 * present i.e chksum = 0, in the individual
633				 * commit blocks.
634				 * Hence to avoid checksum failures, in this
635				 * situation, this extra check is added.
636				 */
637						chksum_err = 1;
638
639				if (chksum_err) {
640					info->end_transaction = next_commit_ID;
641					jbd_debug(1, "Checksum_err %x %x\n",
642						  crc32_sum, found_chksum);
643					if (!JFS_HAS_INCOMPAT_FEATURE(journal,
644					   JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)){
645						journal->j_failed_commit =
646							next_commit_ID;
647						brelse(bh);
648						break;
649					}
650				}
651				crc32_sum = ~0;
652			}
653			brelse(bh);
654			next_commit_ID++;
655			continue;
656
657		case JFS_REVOKE_BLOCK:
658			/* If we aren't in the REVOKE pass, then we can
659			 * just skip over this block. */
660			if (pass != PASS_REVOKE) {
661				brelse(bh);
662				continue;
663			}
664
665			err = scan_revoke_records(journal, bh,
666						  next_commit_ID, info);
667			brelse(bh);
668			if (err)
669				goto failed;
670			continue;
671
672		default:
673			jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
674				  blocktype);
675			brelse(bh);
676			goto done;
677		}
678	}
679
680 done:
681	/*
682	 * We broke out of the log scan loop: either we came to the
683	 * known end of the log or we found an unexpected block in the
684	 * log.  If the latter happened, then we know that the "current"
685	 * transaction marks the end of the valid log.
686	 */
687
688	if (pass == PASS_SCAN) {
689		if (!info->end_transaction)
690			info->end_transaction = next_commit_ID;
691	} else {
692		/* It's really bad news if different passes end up at
693		 * different places (but possible due to IO errors). */
694		if (info->end_transaction != next_commit_ID) {
695			printk (KERN_ERR "JBD: recovery pass %d ended at "
696				"transaction %u, expected %u\n",
697				pass, next_commit_ID, info->end_transaction);
698			if (!success)
699				success = -EIO;
700		}
701	}
702
703	return success;
704
705 failed:
706	return err;
707}
708
709
710/* Scan a revoke record, marking all blocks mentioned as revoked. */
711
712static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
713			       tid_t sequence, struct recovery_info *info)
714{
715	journal_revoke_header_t *header;
716	int offset, max;
717	int record_len = 4;
718
719	header = (journal_revoke_header_t *) bh->b_data;
720	offset = sizeof(journal_revoke_header_t);
721	max = be32_to_cpu(header->r_count);
722
723	if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_64BIT))
724		record_len = 8;
725
726	while (offset < max) {
727		unsigned long long blocknr;
728		int err;
729
730		if (record_len == 4)
731			blocknr = ext2fs_be32_to_cpu(*((__be32 *)(bh->b_data +
732								  offset)));
733		else
734			blocknr = ext2fs_be64_to_cpu(*((__be64 *)(bh->b_data +
735								  offset)));
736		offset += record_len;
737		err = journal_set_revoke(journal, blocknr, sequence);
738		if (err)
739			return err;
740		++info->nr_revokes;
741	}
742	return 0;
743}
744