blob: 69f18fe209236979acca035af00562b0ff7f72bd [file] [log] [blame]
Theodore Ts'of5166762017-12-17 22:00:59 -05001// SPDX-License-Identifier: GPL-2.0+
Dave Kleikamp470decc2006-10-11 01:20:57 -07002/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02003 * linux/fs/jbd2/recovery.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07004 *
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
6 *
7 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
8 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07009 * 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>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070018#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070019#include <linux/errno.h>
Girish Shilamkar818d2762008-01-28 23:58:27 -050020#include <linux/crc32.h>
Jan Kara79feb522012-03-13 22:22:54 -040021#include <linux/blkdev.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070022#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 */
28struct 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
Dave Kleikamp470decc2006-10-11 01:20:57 -070038static int do_one_pass(journal_t *journal,
39 struct recovery_info *info, enum passtype pass);
40static 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 */
46static 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
66static int do_readahead(journal_t *journal, unsigned int start)
67{
68 int err;
69 unsigned int max, nbufs, next;
Mingming Cao18eba7a2006-10-11 01:21:13 -070070 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -070071 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);
Harshad Shirwadkarede7dc72020-11-05 19:58:54 -080077 if (max > journal->j_total_len)
78 max = journal->j_total_len;
Dave Kleikamp470decc2006-10-11 01:20:57 -070079
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++) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -070086 err = jbd2_journal_bmap(journal, next, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -070087
88 if (err) {
Eryu Guanf2a44522011-11-01 19:09:18 -040089 printk(KERN_ERR "JBD2: bad block at offset %u\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -070090 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) {
Mike Christiedfec8a12016-06-05 14:31:44 -0500103 ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700104 journal_brelse_array(bufs, nbufs);
105 nbufs = 0;
106 }
107 } else
108 brelse(bh);
109 }
110
111 if (nbufs)
Mike Christiedfec8a12016-06-05 14:31:44 -0500112 ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700113 err = 0;
114
115failed:
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
128static int jread(struct buffer_head **bhp, journal_t *journal,
129 unsigned int offset)
130{
131 int err;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700132 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700133 struct buffer_head *bh;
134
135 *bhp = NULL;
136
Harshad Shirwadkarede7dc72020-11-05 19:58:54 -0800137 if (offset >= journal->j_total_len) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400138 printk(KERN_ERR "JBD2: corrupted journal superblock\n");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400139 return -EFSCORRUPTED;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700140 }
141
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700142 err = jbd2_journal_bmap(journal, offset, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700143
144 if (err) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400145 printk(KERN_ERR "JBD2: bad block at offset %u\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700146 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)) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400163 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700164 offset);
165 brelse(bh);
166 return -EIO;
167 }
168
169 *bhp = bh;
170 return 0;
171}
172
Jan Kara1101cd42016-02-22 23:19:09 -0500173static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400174{
175 struct jbd2_journal_block_tail *tail;
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400176 __be32 provided;
177 __u32 calculated;
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400178
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400179 if (!jbd2_journal_has_csum_v2or3(j))
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400180 return 1;
181
182 tail = (struct jbd2_journal_block_tail *)(buf + j->j_blocksize -
183 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
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400189 return provided == cpu_to_be32(calculated);
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400190}
Dave Kleikamp470decc2006-10-11 01:20:57 -0700191
192/*
193 * Count the number of in-use tags in a journal descriptor block.
194 */
195
Zach Brownb517bea2006-10-11 01:21:08 -0700196static int count_tags(journal_t *journal, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197{
198 char * tagp;
199 journal_block_tag_t * tag;
Zach Brownb517bea2006-10-11 01:21:08 -0700200 int nr = 0, size = journal->j_blocksize;
201 int tag_bytes = journal_tag_bytes(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700202
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400203 if (jbd2_journal_has_csum_v2or3(journal))
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400204 size -= sizeof(struct jbd2_journal_block_tail);
205
Dave Kleikamp470decc2006-10-11 01:20:57 -0700206 tagp = &bh->b_data[sizeof(journal_header_t)];
207
Zach Brownb517bea2006-10-11 01:21:08 -0700208 while ((tagp - bh->b_data + tag_bytes) <= size) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700209 tag = (journal_block_tag_t *) tagp;
210
211 nr++;
Zach Brownb517bea2006-10-11 01:21:08 -0700212 tagp += tag_bytes;
Darrick J. Wong8f888ef2012-05-22 22:43:41 -0400213 if (!(tag->t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700214 tagp += 16;
215
Darrick J. Wong8f888ef2012-05-22 22:43:41 -0400216 if (tag->t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700217 break;
218 }
219
220 return nr;
221}
222
223
224/* Make sure we wrap around the log correctly! */
225#define wrap(journal, var) \
226do { \
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700227 unsigned long _wrap_last = \
228 jbd2_has_feature_fast_commit(journal) ? \
229 (journal)->j_fc_last : (journal)->j_last; \
230 \
231 if (var >= _wrap_last) \
232 var -= (_wrap_last - (journal)->j_first); \
Dave Kleikamp470decc2006-10-11 01:20:57 -0700233} while (0)
234
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700235static int fc_do_one_pass(journal_t *journal,
236 struct recovery_info *info, enum passtype pass)
237{
238 unsigned int expected_commit_id = info->end_transaction;
239 unsigned long next_fc_block;
240 struct buffer_head *bh;
241 int err = 0;
242
243 next_fc_block = journal->j_fc_first;
244 if (!journal->j_fc_replay_callback)
245 return 0;
246
247 while (next_fc_block <= journal->j_fc_last) {
248 jbd_debug(3, "Fast commit replay: next block %ld",
249 next_fc_block);
250 err = jread(&bh, journal, next_fc_block);
251 if (err) {
252 jbd_debug(3, "Fast commit replay: read error");
253 break;
254 }
255
256 jbd_debug(3, "Processing fast commit blk with seq %d");
257 err = journal->j_fc_replay_callback(journal, bh, pass,
258 next_fc_block - journal->j_fc_first,
259 expected_commit_id);
260 next_fc_block++;
261 if (err < 0 || err == JBD2_FC_REPLAY_STOP)
262 break;
263 err = 0;
264 }
265
266 if (err)
267 jbd_debug(3, "Fast commit replay failed, err = %d\n", err);
268
269 return err;
270}
271
Dave Kleikamp470decc2006-10-11 01:20:57 -0700272/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700273 * jbd2_journal_recover - recovers a on-disk journal
Dave Kleikamp470decc2006-10-11 01:20:57 -0700274 * @journal: the journal to recover
275 *
276 * The primary function for recovering the log contents when mounting a
277 * journaled device.
278 *
279 * Recovery is done in three passes. In the first pass, we look for the
280 * end of the log. In the second, we assemble the list of revoke
281 * blocks. In the third and final pass, we replay any un-revoked blocks
282 * in the log.
283 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700284int jbd2_journal_recover(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700285{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -0400286 int err, err2;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700287 journal_superblock_t * sb;
288
289 struct recovery_info info;
290
291 memset(&info, 0, sizeof(info));
292 sb = journal->j_superblock;
293
294 /*
295 * The journal superblock's s_start field (the current log head)
296 * is always zero if, and only if, the journal was cleanly
297 * unmounted.
298 */
299
300 if (!sb->s_start) {
301 jbd_debug(1, "No recovery required, last transaction %d\n",
302 be32_to_cpu(sb->s_sequence));
303 journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
304 return 0;
305 }
306
307 err = do_one_pass(journal, &info, PASS_SCAN);
308 if (!err)
309 err = do_one_pass(journal, &info, PASS_REVOKE);
310 if (!err)
311 err = do_one_pass(journal, &info, PASS_REPLAY);
312
Eryu Guanf2a44522011-11-01 19:09:18 -0400313 jbd_debug(1, "JBD2: recovery, exit status %d, "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700314 "recovered transactions %u to %u\n",
315 err, info.start_transaction, info.end_transaction);
Eryu Guanf2a44522011-11-01 19:09:18 -0400316 jbd_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700317 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
318
319 /* Restart the log at the next transaction ID, thus invalidating
320 * any existing commit records in the log. */
321 journal->j_transaction_sequence = ++info.end_transaction;
322
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700323 jbd2_journal_clear_revoke(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -0400324 err2 = sync_blockdev(journal->j_fs_dev);
325 if (!err)
326 err = err2;
Jan Kara79feb522012-03-13 22:22:54 -0400327 /* Make sure all replayed data is on permanent storage */
Theodore Ts'o316e4cf2012-08-17 09:56:17 -0400328 if (journal->j_flags & JBD2_BARRIER) {
Christoph Hellwigc6bf3f02021-01-26 15:52:35 +0100329 err2 = blkdev_issue_flush(journal->j_fs_dev);
Theodore Ts'o316e4cf2012-08-17 09:56:17 -0400330 if (!err)
331 err = err2;
332 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700333 return err;
334}
335
336/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700337 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
Dave Kleikamp470decc2006-10-11 01:20:57 -0700338 * @journal: journal to startup
339 *
340 * Locate any valid recovery information from the journal and set up the
341 * journal structures in memory to ignore it (presumably because the
342 * caller has evidence that it is out of date).
Masanari Iidabd7ced92016-02-02 22:31:06 +0900343 * This function doesn't appear to be exported..
Dave Kleikamp470decc2006-10-11 01:20:57 -0700344 *
345 * We perform one pass over the journal to allow us to tell the user how
346 * much recovery information is being erased, and to let us initialise
347 * the journal transaction sequence numbers to the next unused ID.
348 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700349int jbd2_journal_skip_recovery(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700350{
351 int err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700352
353 struct recovery_info info;
354
355 memset (&info, 0, sizeof(info));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700356
357 err = do_one_pass(journal, &info, PASS_SCAN);
358
359 if (err) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400360 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700361 ++journal->j_transaction_sequence;
362 } else {
Jose R. Santose23291b2007-07-18 08:57:06 -0400363#ifdef CONFIG_JBD2_DEBUG
Andi Kleen5a0790c2010-06-14 13:28:03 -0400364 int dropped = info.end_transaction -
365 be32_to_cpu(journal->j_superblock->s_sequence);
Mingming Caob38bd332007-07-19 01:48:35 -0700366 jbd_debug(1,
Eryu Guanf2a44522011-11-01 19:09:18 -0400367 "JBD2: ignoring %d transaction%s from the journal.\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700368 dropped, (dropped == 1) ? "" : "s");
Theodore Ts'o9a4f6272010-12-18 13:36:33 -0500369#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700370 journal->j_transaction_sequence = ++info.end_transaction;
371 }
372
373 journal->j_tail = 0;
374 return err;
375}
376
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400377static inline unsigned long long read_tag_block(journal_t *journal,
378 journal_block_tag_t *tag)
Zach Brownb517bea2006-10-11 01:21:08 -0700379{
Mingming Cao18eba7a2006-10-11 01:21:13 -0700380 unsigned long long block = be32_to_cpu(tag->t_blocknr);
Darrick J. Wong56316a02015-10-17 16:18:45 -0400381 if (jbd2_has_feature_64bit(journal))
Zach Brownb517bea2006-10-11 01:21:08 -0700382 block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
383 return block;
384}
385
Girish Shilamkar818d2762008-01-28 23:58:27 -0500386/*
387 * calc_chksums calculates the checksums for the blocks described in the
388 * descriptor block.
389 */
390static int calc_chksums(journal_t *journal, struct buffer_head *bh,
391 unsigned long *next_log_block, __u32 *crc32_sum)
392{
393 int i, num_blks, err;
394 unsigned long io_block;
395 struct buffer_head *obh;
396
397 num_blks = count_tags(journal, bh);
398 /* Calculate checksum of the descriptor block. */
399 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
400
401 for (i = 0; i < num_blks; i++) {
402 io_block = (*next_log_block)++;
403 wrap(journal, *next_log_block);
404 err = jread(&obh, journal, io_block);
405 if (err) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400406 printk(KERN_ERR "JBD2: IO error %d recovering block "
Girish Shilamkar818d2762008-01-28 23:58:27 -0500407 "%lu in log\n", err, io_block);
408 return 1;
409 } else {
410 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
411 obh->b_size);
412 }
Theodore Ts'o8ea76902008-05-26 10:28:09 -0400413 put_bh(obh);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500414 }
415 return 0;
416}
417
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400418static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
419{
420 struct commit_header *h;
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400421 __be32 provided;
422 __u32 calculated;
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400423
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400424 if (!jbd2_journal_has_csum_v2or3(j))
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400425 return 1;
426
427 h = buf;
428 provided = h->h_chksum[0];
429 h->h_chksum[0] = 0;
430 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
431 h->h_chksum[0] = provided;
432
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400433 return provided == cpu_to_be32(calculated);
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400434}
435
Darrick J. Wongc3900872012-05-27 08:12:12 -0400436static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
437 void *buf, __u32 sequence)
438{
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400439 journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
Darrick J. Wongeee06c52013-05-28 07:31:59 -0400440 __u32 csum32;
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400441 __be32 seq;
Darrick J. Wongc3900872012-05-27 08:12:12 -0400442
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400443 if (!jbd2_journal_has_csum_v2or3(j))
Darrick J. Wongc3900872012-05-27 08:12:12 -0400444 return 1;
445
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400446 seq = cpu_to_be32(sequence);
447 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
Darrick J. Wongeee06c52013-05-28 07:31:59 -0400448 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
Darrick J. Wongc3900872012-05-27 08:12:12 -0400449
Darrick J. Wong56316a02015-10-17 16:18:45 -0400450 if (jbd2_has_feature_csum3(j))
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400451 return tag3->t_checksum == cpu_to_be32(csum32);
452 else
453 return tag->t_checksum == cpu_to_be16(csum32);
Darrick J. Wongc3900872012-05-27 08:12:12 -0400454}
455
Dave Kleikamp470decc2006-10-11 01:20:57 -0700456static int do_one_pass(journal_t *journal,
457 struct recovery_info *info, enum passtype pass)
458{
459 unsigned int first_commit_ID, next_commit_ID;
460 unsigned long next_log_block;
461 int err, success = 0;
462 journal_superblock_t * sb;
463 journal_header_t * tmp;
464 struct buffer_head * bh;
465 unsigned int sequence;
466 int blocktype;
Zach Brownb517bea2006-10-11 01:21:08 -0700467 int tag_bytes = journal_tag_bytes(journal);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500468 __u32 crc32_sum = ~0; /* Transactional Checksums */
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400469 int descr_csum_size = 0;
Darrick J. Wong022eaa72014-08-27 18:40:05 -0400470 int block_error = 0;
changfengnanfc750a3b2020-10-12 18:49:00 +0200471 bool need_check_commit_time = false;
472 __u64 last_trans_commit_time = 0, commit_time;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700473
Dave Kleikamp470decc2006-10-11 01:20:57 -0700474 /*
475 * First thing is to establish what we expect to find in the log
476 * (in terms of transaction IDs), and where (in terms of log
477 * block offsets): query the superblock.
478 */
479
480 sb = journal->j_superblock;
481 next_commit_ID = be32_to_cpu(sb->s_sequence);
482 next_log_block = be32_to_cpu(sb->s_start);
483
484 first_commit_ID = next_commit_ID;
485 if (pass == PASS_SCAN)
486 info->start_transaction = first_commit_ID;
487
488 jbd_debug(1, "Starting recovery pass %d\n", pass);
489
490 /*
491 * Now we walk through the log, transaction by transaction,
492 * making sure that each transaction has a commit block in the
493 * expected place. Each complete transaction gets replayed back
494 * into the main filesystem.
495 */
496
497 while (1) {
498 int flags;
499 char * tagp;
500 journal_block_tag_t * tag;
501 struct buffer_head * obh;
502 struct buffer_head * nbh;
503
Andi Kleene86e1432008-02-06 01:40:11 -0800504 cond_resched();
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505
506 /* If we already know where to stop the log traversal,
507 * check right now that we haven't gone past the end of
508 * the log. */
509
510 if (pass != PASS_SCAN)
511 if (tid_geq(next_commit_ID, info->end_transaction))
512 break;
513
514 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700515 next_commit_ID, next_log_block,
516 jbd2_has_feature_fast_commit(journal) ?
517 journal->j_fc_last : journal->j_last);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700518
519 /* Skip over each chunk of the transaction looking
520 * either the next descriptor block or the final commit
521 * record. */
522
Eryu Guanf2a44522011-11-01 19:09:18 -0400523 jbd_debug(3, "JBD2: checking block %ld\n", next_log_block);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700524 err = jread(&bh, journal, next_log_block);
525 if (err)
526 goto failed;
527
528 next_log_block++;
529 wrap(journal, next_log_block);
530
531 /* What kind of buffer is it?
532 *
533 * If it is a descriptor block, check that it has the
534 * expected sequence number. Otherwise, we're all done
535 * here. */
536
537 tmp = (journal_header_t *)bh->b_data;
538
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700539 if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700540 brelse(bh);
541 break;
542 }
543
544 blocktype = be32_to_cpu(tmp->h_blocktype);
545 sequence = be32_to_cpu(tmp->h_sequence);
546 jbd_debug(3, "Found magic %d, sequence %d\n",
547 blocktype, sequence);
548
549 if (sequence != next_commit_ID) {
550 brelse(bh);
551 break;
552 }
553
554 /* OK, we have a valid descriptor block which matches
555 * all of the sequence number checks. What are we going
556 * to do with it? That depends on the pass... */
557
558 switch(blocktype) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700559 case JBD2_DESCRIPTOR_BLOCK:
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400560 /* Verify checksum first */
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400561 if (jbd2_journal_has_csum_v2or3(journal))
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400562 descr_csum_size =
563 sizeof(struct jbd2_journal_block_tail);
564 if (descr_csum_size > 0 &&
Jan Kara1101cd42016-02-22 23:19:09 -0500565 !jbd2_descriptor_block_csum_verify(journal,
566 bh->b_data)) {
changfengnanfc750a3b2020-10-12 18:49:00 +0200567 /*
568 * PASS_SCAN can see stale blocks due to lazy
569 * journal init. Don't error out on those yet.
570 */
571 if (pass != PASS_SCAN) {
572 pr_err("JBD2: Invalid checksum recovering block %lu in log\n",
573 next_log_block);
574 err = -EFSBADCRC;
575 brelse(bh);
576 goto failed;
577 }
578 need_check_commit_time = true;
579 jbd_debug(1,
580 "invalid descriptor block found in %lu\n",
581 next_log_block);
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400582 }
583
Dave Kleikamp470decc2006-10-11 01:20:57 -0700584 /* If it is a valid descriptor block, replay it
Girish Shilamkar818d2762008-01-28 23:58:27 -0500585 * in pass REPLAY; if journal_checksums enabled, then
586 * calculate checksums in PASS_SCAN, otherwise,
587 * just skip over the blocks it describes. */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700588 if (pass != PASS_REPLAY) {
Girish Shilamkar818d2762008-01-28 23:58:27 -0500589 if (pass == PASS_SCAN &&
Darrick J. Wong56316a02015-10-17 16:18:45 -0400590 jbd2_has_feature_checksum(journal) &&
changfengnanfc750a3b2020-10-12 18:49:00 +0200591 !need_check_commit_time &&
Girish Shilamkar818d2762008-01-28 23:58:27 -0500592 !info->end_transaction) {
593 if (calc_chksums(journal, bh,
594 &next_log_block,
595 &crc32_sum)) {
596 put_bh(bh);
597 break;
598 }
599 put_bh(bh);
600 continue;
601 }
Zach Brownb517bea2006-10-11 01:21:08 -0700602 next_log_block += count_tags(journal, bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700603 wrap(journal, next_log_block);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500604 put_bh(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700605 continue;
606 }
607
608 /* A descriptor block: we can now write all of
609 * the data blocks. Yay, useful work is finally
610 * getting done here! */
611
612 tagp = &bh->b_data[sizeof(journal_header_t)];
Zach Brownb517bea2006-10-11 01:21:08 -0700613 while ((tagp - bh->b_data + tag_bytes)
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400614 <= journal->j_blocksize - descr_csum_size) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700615 unsigned long io_block;
616
617 tag = (journal_block_tag_t *) tagp;
Darrick J. Wong8f888ef2012-05-22 22:43:41 -0400618 flags = be16_to_cpu(tag->t_flags);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700619
620 io_block = next_log_block++;
621 wrap(journal, next_log_block);
622 err = jread(&obh, journal, io_block);
623 if (err) {
624 /* Recover what we can, but
625 * report failure at the end. */
626 success = err;
Eryu Guanf2a44522011-11-01 19:09:18 -0400627 printk(KERN_ERR
628 "JBD2: IO error %d recovering "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700629 "block %ld in log\n",
630 err, io_block);
631 } else {
Mingming Cao18eba7a2006-10-11 01:21:13 -0700632 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700633
634 J_ASSERT(obh != NULL);
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400635 blocknr = read_tag_block(journal,
Zach Brownb517bea2006-10-11 01:21:08 -0700636 tag);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700637
638 /* If the block has been
639 * revoked, then we're all done
640 * here. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700641 if (jbd2_journal_test_revoke
Dave Kleikamp470decc2006-10-11 01:20:57 -0700642 (journal, blocknr,
643 next_commit_ID)) {
644 brelse(obh);
645 ++info->nr_revoke_hits;
646 goto skip_write;
647 }
648
Darrick J. Wongc3900872012-05-27 08:12:12 -0400649 /* Look for block corruption */
650 if (!jbd2_block_tag_csum_verify(
651 journal, tag, obh->b_data,
652 be32_to_cpu(tmp->h_sequence))) {
653 brelse(obh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400654 success = -EFSBADCRC;
Dmitry Monakhova67c8482013-12-08 21:14:59 -0500655 printk(KERN_ERR "JBD2: Invalid "
Darrick J. Wongc3900872012-05-27 08:12:12 -0400656 "checksum recovering "
Theodore Ts'oed65b002018-02-18 21:33:13 -0500657 "data block %llu in "
658 "log\n", blocknr);
Darrick J. Wong022eaa72014-08-27 18:40:05 -0400659 block_error = 1;
660 goto skip_write;
Darrick J. Wongc3900872012-05-27 08:12:12 -0400661 }
662
Dave Kleikamp470decc2006-10-11 01:20:57 -0700663 /* Find a buffer for the new
664 * data being restored */
665 nbh = __getblk(journal->j_fs_dev,
666 blocknr,
667 journal->j_blocksize);
668 if (nbh == NULL) {
669 printk(KERN_ERR
Eryu Guanf2a44522011-11-01 19:09:18 -0400670 "JBD2: Out of memory "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 "during recovery.\n");
672 err = -ENOMEM;
673 brelse(bh);
674 brelse(obh);
675 goto failed;
676 }
677
678 lock_buffer(nbh);
679 memcpy(nbh->b_data, obh->b_data,
680 journal->j_blocksize);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700681 if (flags & JBD2_FLAG_ESCAPE) {
Duane Griffind0025672008-03-19 17:00:54 -0700682 *((__be32 *)nbh->b_data) =
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700683 cpu_to_be32(JBD2_MAGIC_NUMBER);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700684 }
685
686 BUFFER_TRACE(nbh, "marking dirty");
687 set_buffer_uptodate(nbh);
688 mark_buffer_dirty(nbh);
689 BUFFER_TRACE(nbh, "marking uptodate");
690 ++info->nr_replays;
691 /* ll_rw_block(WRITE, 1, &nbh); */
692 unlock_buffer(nbh);
693 brelse(obh);
694 brelse(nbh);
695 }
696
697 skip_write:
Zach Brownb517bea2006-10-11 01:21:08 -0700698 tagp += tag_bytes;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700699 if (!(flags & JBD2_FLAG_SAME_UUID))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700700 tagp += 16;
701
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700702 if (flags & JBD2_FLAG_LAST_TAG)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700703 break;
704 }
705
706 brelse(bh);
707 continue;
708
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700709 case JBD2_COMMIT_BLOCK:
Girish Shilamkar818d2762008-01-28 23:58:27 -0500710 /* How to differentiate between interrupted commit
711 * and journal corruption ?
712 *
713 * {nth transaction}
714 * Checksum Verification Failed
715 * |
716 * ____________________
717 * | |
718 * async_commit sync_commit
719 * | |
720 * | GO TO NEXT "Journal Corruption"
721 * | TRANSACTION
722 * |
723 * {(n+1)th transanction}
724 * |
725 * _______|______________
726 * | |
727 * Commit block found Commit block not found
728 * | |
729 * "Journal Corruption" |
730 * _____________|_________
731 * | |
732 * nth trans corrupt OR nth trans
733 * and (n+1)th interrupted interrupted
734 * before commit block
735 * could reach the disk.
736 * (Cannot find the difference in above
737 * mentioned conditions. Hence assume
738 * "Interrupted Commit".)
739 */
changfengnanfc750a3b2020-10-12 18:49:00 +0200740 commit_time = be64_to_cpu(
741 ((struct commit_header *)bh->b_data)->h_commit_sec);
742 /*
743 * If need_check_commit_time is set, it means we are in
744 * PASS_SCAN and csum verify failed before. If
745 * commit_time is increasing, it's the same journal,
746 * otherwise it is stale journal block, just end this
747 * recovery.
748 */
749 if (need_check_commit_time) {
750 if (commit_time >= last_trans_commit_time) {
751 pr_err("JBD2: Invalid checksum found in transaction %u\n",
752 next_commit_ID);
753 err = -EFSBADCRC;
754 brelse(bh);
755 goto failed;
756 }
757 ignore_crc_mismatch:
758 /*
759 * It likely does not belong to same journal,
760 * just end this recovery with success.
761 */
762 jbd_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n",
763 next_commit_ID);
764 err = 0;
765 brelse(bh);
766 goto done;
767 }
Girish Shilamkar818d2762008-01-28 23:58:27 -0500768
changfengnanfc750a3b2020-10-12 18:49:00 +0200769 /*
770 * Found an expected commit block: if checksums
771 * are present, verify them in PASS_SCAN; else not
Girish Shilamkar818d2762008-01-28 23:58:27 -0500772 * much to do other than move on to the next sequence
changfengnanfc750a3b2020-10-12 18:49:00 +0200773 * number.
774 */
Girish Shilamkar818d2762008-01-28 23:58:27 -0500775 if (pass == PASS_SCAN &&
Darrick J. Wong56316a02015-10-17 16:18:45 -0400776 jbd2_has_feature_checksum(journal)) {
Girish Shilamkar818d2762008-01-28 23:58:27 -0500777 struct commit_header *cbh =
778 (struct commit_header *)bh->b_data;
779 unsigned found_chksum =
780 be32_to_cpu(cbh->h_chksum[0]);
781
Girish Shilamkar818d2762008-01-28 23:58:27 -0500782 if (info->end_transaction) {
Theodore Ts'o624080e2008-06-06 17:50:40 -0400783 journal->j_failed_commit =
784 info->end_transaction;
Girish Shilamkar818d2762008-01-28 23:58:27 -0500785 brelse(bh);
786 break;
787 }
788
Shijie Luo00a3fff2020-08-10 22:21:28 -0400789 /* Neither checksum match nor unused? */
790 if (!((crc32_sum == found_chksum &&
791 cbh->h_chksum_type ==
792 JBD2_CRC32_CHKSUM &&
793 cbh->h_chksum_size ==
794 JBD2_CRC32_CHKSUM_SIZE) ||
795 (cbh->h_chksum_type == 0 &&
796 cbh->h_chksum_size == 0 &&
797 found_chksum == 0)))
798 goto chksum_error;
Girish Shilamkar818d2762008-01-28 23:58:27 -0500799
Girish Shilamkar818d2762008-01-28 23:58:27 -0500800 crc32_sum = ~0;
801 }
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400802 if (pass == PASS_SCAN &&
803 !jbd2_commit_block_csum_verify(journal,
804 bh->b_data)) {
Shijie Luo00a3fff2020-08-10 22:21:28 -0400805 chksum_error:
changfengnanfc750a3b2020-10-12 18:49:00 +0200806 if (commit_time < last_trans_commit_time)
807 goto ignore_crc_mismatch;
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400808 info->end_transaction = next_commit_ID;
809
Darrick J. Wong56316a02015-10-17 16:18:45 -0400810 if (!jbd2_has_feature_async_commit(journal)) {
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400811 journal->j_failed_commit =
812 next_commit_ID;
813 brelse(bh);
814 break;
815 }
816 }
changfengnanfc750a3b2020-10-12 18:49:00 +0200817 if (pass == PASS_SCAN)
818 last_trans_commit_time = commit_time;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700819 brelse(bh);
820 next_commit_ID++;
821 continue;
822
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700823 case JBD2_REVOKE_BLOCK:
changfengnanfc750a3b2020-10-12 18:49:00 +0200824 /*
825 * Check revoke block crc in pass_scan, if csum verify
826 * failed, check commit block time later.
827 */
828 if (pass == PASS_SCAN &&
829 !jbd2_descriptor_block_csum_verify(journal,
830 bh->b_data)) {
831 jbd_debug(1, "JBD2: invalid revoke block found in %lu\n",
832 next_log_block);
833 need_check_commit_time = true;
834 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700835 /* If we aren't in the REVOKE pass, then we can
836 * just skip over this block. */
837 if (pass != PASS_REVOKE) {
838 brelse(bh);
839 continue;
840 }
841
842 err = scan_revoke_records(journal, bh,
843 next_commit_ID, info);
844 brelse(bh);
845 if (err)
846 goto failed;
847 continue;
848
849 default:
850 jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
851 blocktype);
852 brelse(bh);
853 goto done;
854 }
855 }
856
857 done:
858 /*
859 * We broke out of the log scan loop: either we came to the
860 * known end of the log or we found an unexpected block in the
861 * log. If the latter happened, then we know that the "current"
862 * transaction marks the end of the valid log.
863 */
864
Girish Shilamkar818d2762008-01-28 23:58:27 -0500865 if (pass == PASS_SCAN) {
866 if (!info->end_transaction)
867 info->end_transaction = next_commit_ID;
868 } else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700869 /* It's really bad news if different passes end up at
870 * different places (but possible due to IO errors). */
871 if (info->end_transaction != next_commit_ID) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400872 printk(KERN_ERR "JBD2: recovery pass %d ended at "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700873 "transaction %u, expected %u\n",
874 pass, next_commit_ID, info->end_transaction);
875 if (!success)
876 success = -EIO;
877 }
878 }
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700879
880 if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
881 err = fc_do_one_pass(journal, info, pass);
882 if (err)
883 success = err;
884 }
885
Darrick J. Wong022eaa72014-08-27 18:40:05 -0400886 if (block_error && success == 0)
887 success = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700888 return success;
889
890 failed:
891 return err;
892}
893
Dave Kleikamp470decc2006-10-11 01:20:57 -0700894/* Scan a revoke record, marking all blocks mentioned as revoked. */
895
896static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
897 tid_t sequence, struct recovery_info *info)
898{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700899 jbd2_journal_revoke_header_t *header;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700900 int offset, max;
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400901 int csum_size = 0;
902 __u32 rcount;
Zach Brownb517bea2006-10-11 01:21:08 -0700903 int record_len = 4;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700904
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700905 header = (jbd2_journal_revoke_header_t *) bh->b_data;
906 offset = sizeof(jbd2_journal_revoke_header_t);
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400907 rcount = be32_to_cpu(header->r_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700908
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400909 if (jbd2_journal_has_csum_v2or3(journal))
Jan Kara1101cd42016-02-22 23:19:09 -0500910 csum_size = sizeof(struct jbd2_journal_block_tail);
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400911 if (rcount > journal->j_blocksize - csum_size)
912 return -EINVAL;
913 max = rcount;
914
Darrick J. Wong56316a02015-10-17 16:18:45 -0400915 if (jbd2_has_feature_64bit(journal))
Zach Brownb517bea2006-10-11 01:21:08 -0700916 record_len = 8;
917
918 while (offset + record_len <= max) {
Mingming Cao18eba7a2006-10-11 01:21:13 -0700919 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700920 int err;
921
Zach Brownb517bea2006-10-11 01:21:08 -0700922 if (record_len == 4)
923 blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
924 else
925 blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
926 offset += record_len;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700927 err = jbd2_journal_set_revoke(journal, blocknr, sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700928 if (err)
929 return err;
930 ++info->nr_revokes;
931 }
932 return 0;
933}