blob: 8ca3527189f8711ff24663ce380350264194b227 [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
Theodore Ts'o4009cc72021-08-10 14:02:33 -0400182 tail = (struct jbd2_journal_block_tail *)((char *)buf +
183 j->j_blocksize - sizeof(struct jbd2_journal_block_tail));
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400184 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;
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400199 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) {
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400209 memcpy(&tag, tagp, sizeof(tag));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700210
211 nr++;
Zach Brownb517bea2006-10-11 01:21:08 -0700212 tagp += tag_bytes;
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400213 if (!(tag.t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700214 tagp += 16;
215
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -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) {
Arnd Bergmannfcdf3c32021-04-09 22:12:05 +0200248 jbd_debug(3, "Fast commit replay: next block %ld\n",
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700249 next_fc_block);
250 err = jread(&bh, journal, next_fc_block);
251 if (err) {
Arnd Bergmannfcdf3c32021-04-09 22:12:05 +0200252 jbd_debug(3, "Fast commit replay: read error\n");
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700253 break;
254 }
255
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700256 err = journal->j_fc_replay_callback(journal, bh, pass,
257 next_fc_block - journal->j_fc_first,
258 expected_commit_id);
259 next_fc_block++;
260 if (err < 0 || err == JBD2_FC_REPLAY_STOP)
261 break;
262 err = 0;
263 }
264
265 if (err)
266 jbd_debug(3, "Fast commit replay failed, err = %d\n", err);
267
268 return err;
269}
270
Dave Kleikamp470decc2006-10-11 01:20:57 -0700271/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700272 * jbd2_journal_recover - recovers a on-disk journal
Dave Kleikamp470decc2006-10-11 01:20:57 -0700273 * @journal: the journal to recover
274 *
275 * The primary function for recovering the log contents when mounting a
276 * journaled device.
277 *
278 * Recovery is done in three passes. In the first pass, we look for the
279 * end of the log. In the second, we assemble the list of revoke
280 * blocks. In the third and final pass, we replay any un-revoked blocks
281 * in the log.
282 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700283int jbd2_journal_recover(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284{
Hidehiro Kawai44519fa2008-10-10 20:29:13 -0400285 int err, err2;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700286 journal_superblock_t * sb;
287
288 struct recovery_info info;
289
290 memset(&info, 0, sizeof(info));
291 sb = journal->j_superblock;
292
293 /*
294 * The journal superblock's s_start field (the current log head)
295 * is always zero if, and only if, the journal was cleanly
296 * unmounted.
297 */
298
299 if (!sb->s_start) {
300 jbd_debug(1, "No recovery required, last transaction %d\n",
301 be32_to_cpu(sb->s_sequence));
302 journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
303 return 0;
304 }
305
306 err = do_one_pass(journal, &info, PASS_SCAN);
307 if (!err)
308 err = do_one_pass(journal, &info, PASS_REVOKE);
309 if (!err)
310 err = do_one_pass(journal, &info, PASS_REPLAY);
311
Eryu Guanf2a44522011-11-01 19:09:18 -0400312 jbd_debug(1, "JBD2: recovery, exit status %d, "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700313 "recovered transactions %u to %u\n",
314 err, info.start_transaction, info.end_transaction);
Eryu Guanf2a44522011-11-01 19:09:18 -0400315 jbd_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700316 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
317
318 /* Restart the log at the next transaction ID, thus invalidating
319 * any existing commit records in the log. */
320 journal->j_transaction_sequence = ++info.end_transaction;
321
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700322 jbd2_journal_clear_revoke(journal);
Hidehiro Kawai44519fa2008-10-10 20:29:13 -0400323 err2 = sync_blockdev(journal->j_fs_dev);
324 if (!err)
325 err = err2;
Jan Kara79feb522012-03-13 22:22:54 -0400326 /* Make sure all replayed data is on permanent storage */
Theodore Ts'o316e4cf2012-08-17 09:56:17 -0400327 if (journal->j_flags & JBD2_BARRIER) {
Christoph Hellwigc6bf3f02021-01-26 15:52:35 +0100328 err2 = blkdev_issue_flush(journal->j_fs_dev);
Theodore Ts'o316e4cf2012-08-17 09:56:17 -0400329 if (!err)
330 err = err2;
331 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700332 return err;
333}
334
335/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700336 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
Dave Kleikamp470decc2006-10-11 01:20:57 -0700337 * @journal: journal to startup
338 *
339 * Locate any valid recovery information from the journal and set up the
340 * journal structures in memory to ignore it (presumably because the
341 * caller has evidence that it is out of date).
Masanari Iidabd7ced92016-02-02 22:31:06 +0900342 * This function doesn't appear to be exported..
Dave Kleikamp470decc2006-10-11 01:20:57 -0700343 *
344 * We perform one pass over the journal to allow us to tell the user how
345 * much recovery information is being erased, and to let us initialise
346 * the journal transaction sequence numbers to the next unused ID.
347 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700348int jbd2_journal_skip_recovery(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700349{
350 int err;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700351
352 struct recovery_info info;
353
354 memset (&info, 0, sizeof(info));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700355
356 err = do_one_pass(journal, &info, PASS_SCAN);
357
358 if (err) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400359 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700360 ++journal->j_transaction_sequence;
361 } else {
Jose R. Santose23291b2007-07-18 08:57:06 -0400362#ifdef CONFIG_JBD2_DEBUG
Andi Kleen5a0790c2010-06-14 13:28:03 -0400363 int dropped = info.end_transaction -
364 be32_to_cpu(journal->j_superblock->s_sequence);
Mingming Caob38bd332007-07-19 01:48:35 -0700365 jbd_debug(1,
Eryu Guanf2a44522011-11-01 19:09:18 -0400366 "JBD2: ignoring %d transaction%s from the journal.\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700367 dropped, (dropped == 1) ? "" : "s");
Theodore Ts'o9a4f6272010-12-18 13:36:33 -0500368#endif
Dave Kleikamp470decc2006-10-11 01:20:57 -0700369 journal->j_transaction_sequence = ++info.end_transaction;
370 }
371
372 journal->j_tail = 0;
373 return err;
374}
375
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400376static inline unsigned long long read_tag_block(journal_t *journal,
377 journal_block_tag_t *tag)
Zach Brownb517bea2006-10-11 01:21:08 -0700378{
Mingming Cao18eba7a2006-10-11 01:21:13 -0700379 unsigned long long block = be32_to_cpu(tag->t_blocknr);
Darrick J. Wong56316a02015-10-17 16:18:45 -0400380 if (jbd2_has_feature_64bit(journal))
Zach Brownb517bea2006-10-11 01:21:08 -0700381 block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
382 return block;
383}
384
Girish Shilamkar818d2762008-01-28 23:58:27 -0500385/*
386 * calc_chksums calculates the checksums for the blocks described in the
387 * descriptor block.
388 */
389static int calc_chksums(journal_t *journal, struct buffer_head *bh,
390 unsigned long *next_log_block, __u32 *crc32_sum)
391{
392 int i, num_blks, err;
393 unsigned long io_block;
394 struct buffer_head *obh;
395
396 num_blks = count_tags(journal, bh);
397 /* Calculate checksum of the descriptor block. */
398 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
399
400 for (i = 0; i < num_blks; i++) {
401 io_block = (*next_log_block)++;
402 wrap(journal, *next_log_block);
403 err = jread(&obh, journal, io_block);
404 if (err) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400405 printk(KERN_ERR "JBD2: IO error %d recovering block "
Girish Shilamkar818d2762008-01-28 23:58:27 -0500406 "%lu in log\n", err, io_block);
407 return 1;
408 } else {
409 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
410 obh->b_size);
411 }
Theodore Ts'o8ea76902008-05-26 10:28:09 -0400412 put_bh(obh);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500413 }
414 return 0;
415}
416
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400417static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
418{
419 struct commit_header *h;
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400420 __be32 provided;
421 __u32 calculated;
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400422
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400423 if (!jbd2_journal_has_csum_v2or3(j))
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400424 return 1;
425
426 h = buf;
427 provided = h->h_chksum[0];
428 h->h_chksum[0] = 0;
429 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
430 h->h_chksum[0] = provided;
431
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400432 return provided == cpu_to_be32(calculated);
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400433}
434
Darrick J. Wongc3900872012-05-27 08:12:12 -0400435static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400436 journal_block_tag3_t *tag3,
Darrick J. Wongc3900872012-05-27 08:12:12 -0400437 void *buf, __u32 sequence)
438{
Darrick J. Wongeee06c52013-05-28 07:31:59 -0400439 __u32 csum32;
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400440 __be32 seq;
Darrick J. Wongc3900872012-05-27 08:12:12 -0400441
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400442 if (!jbd2_journal_has_csum_v2or3(j))
Darrick J. Wongc3900872012-05-27 08:12:12 -0400443 return 1;
444
Darrick J. Wong18a6ea12013-08-28 14:59:58 -0400445 seq = cpu_to_be32(sequence);
446 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
Darrick J. Wongeee06c52013-05-28 07:31:59 -0400447 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
Darrick J. Wongc3900872012-05-27 08:12:12 -0400448
Darrick J. Wong56316a02015-10-17 16:18:45 -0400449 if (jbd2_has_feature_csum3(j))
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400450 return tag3->t_checksum == cpu_to_be32(csum32);
451 else
452 return tag->t_checksum == cpu_to_be16(csum32);
Darrick J. Wongc3900872012-05-27 08:12:12 -0400453}
454
Dave Kleikamp470decc2006-10-11 01:20:57 -0700455static int do_one_pass(journal_t *journal,
456 struct recovery_info *info, enum passtype pass)
457{
458 unsigned int first_commit_ID, next_commit_ID;
459 unsigned long next_log_block;
460 int err, success = 0;
461 journal_superblock_t * sb;
462 journal_header_t * tmp;
463 struct buffer_head * bh;
464 unsigned int sequence;
465 int blocktype;
Zach Brownb517bea2006-10-11 01:21:08 -0700466 int tag_bytes = journal_tag_bytes(journal);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500467 __u32 crc32_sum = ~0; /* Transactional Checksums */
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400468 int descr_csum_size = 0;
Darrick J. Wong022eaa72014-08-27 18:40:05 -0400469 int block_error = 0;
changfengnanfc750a3b2020-10-12 18:49:00 +0200470 bool need_check_commit_time = false;
471 __u64 last_trans_commit_time = 0, commit_time;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700472
Dave Kleikamp470decc2006-10-11 01:20:57 -0700473 /*
474 * First thing is to establish what we expect to find in the log
475 * (in terms of transaction IDs), and where (in terms of log
476 * block offsets): query the superblock.
477 */
478
479 sb = journal->j_superblock;
480 next_commit_ID = be32_to_cpu(sb->s_sequence);
481 next_log_block = be32_to_cpu(sb->s_start);
482
483 first_commit_ID = next_commit_ID;
484 if (pass == PASS_SCAN)
485 info->start_transaction = first_commit_ID;
486
487 jbd_debug(1, "Starting recovery pass %d\n", pass);
488
489 /*
490 * Now we walk through the log, transaction by transaction,
491 * making sure that each transaction has a commit block in the
492 * expected place. Each complete transaction gets replayed back
493 * into the main filesystem.
494 */
495
496 while (1) {
497 int flags;
498 char * tagp;
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400499 journal_block_tag_t tag;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700500 struct buffer_head * obh;
501 struct buffer_head * nbh;
502
Andi Kleene86e1432008-02-06 01:40:11 -0800503 cond_resched();
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504
505 /* If we already know where to stop the log traversal,
506 * check right now that we haven't gone past the end of
507 * the log. */
508
509 if (pass != PASS_SCAN)
510 if (tid_geq(next_commit_ID, info->end_transaction))
511 break;
512
513 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700514 next_commit_ID, next_log_block,
515 jbd2_has_feature_fast_commit(journal) ?
516 journal->j_fc_last : journal->j_last);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700517
518 /* Skip over each chunk of the transaction looking
519 * either the next descriptor block or the final commit
520 * record. */
521
Eryu Guanf2a44522011-11-01 19:09:18 -0400522 jbd_debug(3, "JBD2: checking block %ld\n", next_log_block);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523 err = jread(&bh, journal, next_log_block);
524 if (err)
525 goto failed;
526
527 next_log_block++;
528 wrap(journal, next_log_block);
529
530 /* What kind of buffer is it?
531 *
532 * If it is a descriptor block, check that it has the
533 * expected sequence number. Otherwise, we're all done
534 * here. */
535
536 tmp = (journal_header_t *)bh->b_data;
537
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700538 if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700539 brelse(bh);
540 break;
541 }
542
543 blocktype = be32_to_cpu(tmp->h_blocktype);
544 sequence = be32_to_cpu(tmp->h_sequence);
545 jbd_debug(3, "Found magic %d, sequence %d\n",
546 blocktype, sequence);
547
548 if (sequence != next_commit_ID) {
549 brelse(bh);
550 break;
551 }
552
553 /* OK, we have a valid descriptor block which matches
554 * all of the sequence number checks. What are we going
555 * to do with it? That depends on the pass... */
556
557 switch(blocktype) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700558 case JBD2_DESCRIPTOR_BLOCK:
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400559 /* Verify checksum first */
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400560 if (jbd2_journal_has_csum_v2or3(journal))
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400561 descr_csum_size =
562 sizeof(struct jbd2_journal_block_tail);
563 if (descr_csum_size > 0 &&
Jan Kara1101cd42016-02-22 23:19:09 -0500564 !jbd2_descriptor_block_csum_verify(journal,
565 bh->b_data)) {
changfengnanfc750a3b2020-10-12 18:49:00 +0200566 /*
567 * PASS_SCAN can see stale blocks due to lazy
568 * journal init. Don't error out on those yet.
569 */
570 if (pass != PASS_SCAN) {
571 pr_err("JBD2: Invalid checksum recovering block %lu in log\n",
572 next_log_block);
573 err = -EFSBADCRC;
574 brelse(bh);
575 goto failed;
576 }
577 need_check_commit_time = true;
578 jbd_debug(1,
579 "invalid descriptor block found in %lu\n",
580 next_log_block);
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400581 }
582
Dave Kleikamp470decc2006-10-11 01:20:57 -0700583 /* If it is a valid descriptor block, replay it
Girish Shilamkar818d2762008-01-28 23:58:27 -0500584 * in pass REPLAY; if journal_checksums enabled, then
585 * calculate checksums in PASS_SCAN, otherwise,
586 * just skip over the blocks it describes. */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587 if (pass != PASS_REPLAY) {
Girish Shilamkar818d2762008-01-28 23:58:27 -0500588 if (pass == PASS_SCAN &&
Darrick J. Wong56316a02015-10-17 16:18:45 -0400589 jbd2_has_feature_checksum(journal) &&
changfengnanfc750a3b2020-10-12 18:49:00 +0200590 !need_check_commit_time &&
Girish Shilamkar818d2762008-01-28 23:58:27 -0500591 !info->end_transaction) {
592 if (calc_chksums(journal, bh,
593 &next_log_block,
594 &crc32_sum)) {
595 put_bh(bh);
596 break;
597 }
598 put_bh(bh);
599 continue;
600 }
Zach Brownb517bea2006-10-11 01:21:08 -0700601 next_log_block += count_tags(journal, bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700602 wrap(journal, next_log_block);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500603 put_bh(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604 continue;
605 }
606
607 /* A descriptor block: we can now write all of
608 * the data blocks. Yay, useful work is finally
609 * getting done here! */
610
611 tagp = &bh->b_data[sizeof(journal_header_t)];
Zach Brownb517bea2006-10-11 01:21:08 -0700612 while ((tagp - bh->b_data + tag_bytes)
Darrick J. Wong3caa4872012-05-27 08:10:22 -0400613 <= journal->j_blocksize - descr_csum_size) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 unsigned long io_block;
615
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400616 memcpy(&tag, tagp, sizeof(tag));
617 flags = be16_to_cpu(tag.t_flags);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700618
619 io_block = next_log_block++;
620 wrap(journal, next_log_block);
621 err = jread(&obh, journal, io_block);
622 if (err) {
623 /* Recover what we can, but
624 * report failure at the end. */
625 success = err;
Eryu Guanf2a44522011-11-01 19:09:18 -0400626 printk(KERN_ERR
627 "JBD2: IO error %d recovering "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 "block %ld in log\n",
629 err, io_block);
630 } else {
Mingming Cao18eba7a2006-10-11 01:21:13 -0700631 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700632
633 J_ASSERT(obh != NULL);
Darrick J. Wongdb9ee222014-08-27 18:40:07 -0400634 blocknr = read_tag_block(journal,
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400635 &tag);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700636
637 /* If the block has been
638 * revoked, then we're all done
639 * here. */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700640 if (jbd2_journal_test_revoke
Dave Kleikamp470decc2006-10-11 01:20:57 -0700641 (journal, blocknr,
642 next_commit_ID)) {
643 brelse(obh);
644 ++info->nr_revoke_hits;
645 goto skip_write;
646 }
647
Darrick J. Wongc3900872012-05-27 08:12:12 -0400648 /* Look for block corruption */
649 if (!jbd2_block_tag_csum_verify(
Theodore Ts'oa20d1ce2021-05-03 15:37:33 -0400650 journal, &tag, (journal_block_tag3_t *)tagp,
651 obh->b_data, be32_to_cpu(tmp->h_sequence))) {
Darrick J. Wongc3900872012-05-27 08:12:12 -0400652 brelse(obh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400653 success = -EFSBADCRC;
Dmitry Monakhova67c8482013-12-08 21:14:59 -0500654 printk(KERN_ERR "JBD2: Invalid "
Darrick J. Wongc3900872012-05-27 08:12:12 -0400655 "checksum recovering "
Theodore Ts'oed65b002018-02-18 21:33:13 -0500656 "data block %llu in "
657 "log\n", blocknr);
Darrick J. Wong022eaa72014-08-27 18:40:05 -0400658 block_error = 1;
659 goto skip_write;
Darrick J. Wongc3900872012-05-27 08:12:12 -0400660 }
661
Dave Kleikamp470decc2006-10-11 01:20:57 -0700662 /* Find a buffer for the new
663 * data being restored */
664 nbh = __getblk(journal->j_fs_dev,
665 blocknr,
666 journal->j_blocksize);
667 if (nbh == NULL) {
668 printk(KERN_ERR
Eryu Guanf2a44522011-11-01 19:09:18 -0400669 "JBD2: Out of memory "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700670 "during recovery.\n");
671 err = -ENOMEM;
672 brelse(bh);
673 brelse(obh);
674 goto failed;
675 }
676
677 lock_buffer(nbh);
678 memcpy(nbh->b_data, obh->b_data,
679 journal->j_blocksize);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700680 if (flags & JBD2_FLAG_ESCAPE) {
Duane Griffind0025672008-03-19 17:00:54 -0700681 *((__be32 *)nbh->b_data) =
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700682 cpu_to_be32(JBD2_MAGIC_NUMBER);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700683 }
684
685 BUFFER_TRACE(nbh, "marking dirty");
686 set_buffer_uptodate(nbh);
687 mark_buffer_dirty(nbh);
688 BUFFER_TRACE(nbh, "marking uptodate");
689 ++info->nr_replays;
690 /* ll_rw_block(WRITE, 1, &nbh); */
691 unlock_buffer(nbh);
692 brelse(obh);
693 brelse(nbh);
694 }
695
696 skip_write:
Zach Brownb517bea2006-10-11 01:21:08 -0700697 tagp += tag_bytes;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700698 if (!(flags & JBD2_FLAG_SAME_UUID))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700699 tagp += 16;
700
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700701 if (flags & JBD2_FLAG_LAST_TAG)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700702 break;
703 }
704
705 brelse(bh);
706 continue;
707
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700708 case JBD2_COMMIT_BLOCK:
Girish Shilamkar818d2762008-01-28 23:58:27 -0500709 /* How to differentiate between interrupted commit
710 * and journal corruption ?
711 *
712 * {nth transaction}
713 * Checksum Verification Failed
714 * |
715 * ____________________
716 * | |
717 * async_commit sync_commit
718 * | |
719 * | GO TO NEXT "Journal Corruption"
720 * | TRANSACTION
721 * |
722 * {(n+1)th transanction}
723 * |
724 * _______|______________
725 * | |
726 * Commit block found Commit block not found
727 * | |
728 * "Journal Corruption" |
729 * _____________|_________
730 * | |
731 * nth trans corrupt OR nth trans
732 * and (n+1)th interrupted interrupted
733 * before commit block
734 * could reach the disk.
735 * (Cannot find the difference in above
736 * mentioned conditions. Hence assume
737 * "Interrupted Commit".)
738 */
changfengnanfc750a3b2020-10-12 18:49:00 +0200739 commit_time = be64_to_cpu(
740 ((struct commit_header *)bh->b_data)->h_commit_sec);
741 /*
742 * If need_check_commit_time is set, it means we are in
743 * PASS_SCAN and csum verify failed before. If
744 * commit_time is increasing, it's the same journal,
745 * otherwise it is stale journal block, just end this
746 * recovery.
747 */
748 if (need_check_commit_time) {
749 if (commit_time >= last_trans_commit_time) {
750 pr_err("JBD2: Invalid checksum found in transaction %u\n",
751 next_commit_ID);
752 err = -EFSBADCRC;
753 brelse(bh);
754 goto failed;
755 }
756 ignore_crc_mismatch:
757 /*
758 * It likely does not belong to same journal,
759 * just end this recovery with success.
760 */
761 jbd_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n",
762 next_commit_ID);
changfengnanfc750a3b2020-10-12 18:49:00 +0200763 brelse(bh);
764 goto done;
765 }
Girish Shilamkar818d2762008-01-28 23:58:27 -0500766
changfengnanfc750a3b2020-10-12 18:49:00 +0200767 /*
768 * Found an expected commit block: if checksums
769 * are present, verify them in PASS_SCAN; else not
Girish Shilamkar818d2762008-01-28 23:58:27 -0500770 * much to do other than move on to the next sequence
changfengnanfc750a3b2020-10-12 18:49:00 +0200771 * number.
772 */
Girish Shilamkar818d2762008-01-28 23:58:27 -0500773 if (pass == PASS_SCAN &&
Darrick J. Wong56316a02015-10-17 16:18:45 -0400774 jbd2_has_feature_checksum(journal)) {
Girish Shilamkar818d2762008-01-28 23:58:27 -0500775 struct commit_header *cbh =
776 (struct commit_header *)bh->b_data;
777 unsigned found_chksum =
778 be32_to_cpu(cbh->h_chksum[0]);
779
Girish Shilamkar818d2762008-01-28 23:58:27 -0500780 if (info->end_transaction) {
Theodore Ts'o624080e2008-06-06 17:50:40 -0400781 journal->j_failed_commit =
782 info->end_transaction;
Girish Shilamkar818d2762008-01-28 23:58:27 -0500783 brelse(bh);
784 break;
785 }
786
Shijie Luo00a3fff2020-08-10 22:21:28 -0400787 /* Neither checksum match nor unused? */
788 if (!((crc32_sum == found_chksum &&
789 cbh->h_chksum_type ==
790 JBD2_CRC32_CHKSUM &&
791 cbh->h_chksum_size ==
792 JBD2_CRC32_CHKSUM_SIZE) ||
793 (cbh->h_chksum_type == 0 &&
794 cbh->h_chksum_size == 0 &&
795 found_chksum == 0)))
796 goto chksum_error;
Girish Shilamkar818d2762008-01-28 23:58:27 -0500797
Girish Shilamkar818d2762008-01-28 23:58:27 -0500798 crc32_sum = ~0;
799 }
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400800 if (pass == PASS_SCAN &&
801 !jbd2_commit_block_csum_verify(journal,
802 bh->b_data)) {
Shijie Luo00a3fff2020-08-10 22:21:28 -0400803 chksum_error:
changfengnanfc750a3b2020-10-12 18:49:00 +0200804 if (commit_time < last_trans_commit_time)
805 goto ignore_crc_mismatch;
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400806 info->end_transaction = next_commit_ID;
807
Darrick J. Wong56316a02015-10-17 16:18:45 -0400808 if (!jbd2_has_feature_async_commit(journal)) {
Darrick J. Wong1f56c582012-05-27 08:10:25 -0400809 journal->j_failed_commit =
810 next_commit_ID;
811 brelse(bh);
812 break;
813 }
814 }
changfengnanfc750a3b2020-10-12 18:49:00 +0200815 if (pass == PASS_SCAN)
816 last_trans_commit_time = commit_time;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700817 brelse(bh);
818 next_commit_ID++;
819 continue;
820
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700821 case JBD2_REVOKE_BLOCK:
changfengnanfc750a3b2020-10-12 18:49:00 +0200822 /*
823 * Check revoke block crc in pass_scan, if csum verify
824 * failed, check commit block time later.
825 */
826 if (pass == PASS_SCAN &&
827 !jbd2_descriptor_block_csum_verify(journal,
828 bh->b_data)) {
829 jbd_debug(1, "JBD2: invalid revoke block found in %lu\n",
830 next_log_block);
831 need_check_commit_time = true;
832 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700833 /* If we aren't in the REVOKE pass, then we can
834 * just skip over this block. */
835 if (pass != PASS_REVOKE) {
836 brelse(bh);
837 continue;
838 }
839
840 err = scan_revoke_records(journal, bh,
841 next_commit_ID, info);
842 brelse(bh);
843 if (err)
844 goto failed;
845 continue;
846
847 default:
848 jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
849 blocktype);
850 brelse(bh);
851 goto done;
852 }
853 }
854
855 done:
856 /*
857 * We broke out of the log scan loop: either we came to the
858 * known end of the log or we found an unexpected block in the
859 * log. If the latter happened, then we know that the "current"
860 * transaction marks the end of the valid log.
861 */
862
Girish Shilamkar818d2762008-01-28 23:58:27 -0500863 if (pass == PASS_SCAN) {
864 if (!info->end_transaction)
865 info->end_transaction = next_commit_ID;
866 } else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700867 /* It's really bad news if different passes end up at
868 * different places (but possible due to IO errors). */
869 if (info->end_transaction != next_commit_ID) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400870 printk(KERN_ERR "JBD2: recovery pass %d ended at "
Dave Kleikamp470decc2006-10-11 01:20:57 -0700871 "transaction %u, expected %u\n",
872 pass, next_commit_ID, info->end_transaction);
873 if (!success)
874 success = -EIO;
875 }
876 }
Harshad Shirwadkar5b849b52020-10-15 13:37:58 -0700877
878 if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
879 err = fc_do_one_pass(journal, info, pass);
880 if (err)
881 success = err;
882 }
883
Darrick J. Wong022eaa72014-08-27 18:40:05 -0400884 if (block_error && success == 0)
885 success = -EIO;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700886 return success;
887
888 failed:
889 return err;
890}
891
Dave Kleikamp470decc2006-10-11 01:20:57 -0700892/* Scan a revoke record, marking all blocks mentioned as revoked. */
893
894static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
895 tid_t sequence, struct recovery_info *info)
896{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700897 jbd2_journal_revoke_header_t *header;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700898 int offset, max;
Theodore Ts'o4009cc72021-08-10 14:02:33 -0400899 unsigned csum_size = 0;
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400900 __u32 rcount;
Zach Brownb517bea2006-10-11 01:21:08 -0700901 int record_len = 4;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700902
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700903 header = (jbd2_journal_revoke_header_t *) bh->b_data;
904 offset = sizeof(jbd2_journal_revoke_header_t);
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400905 rcount = be32_to_cpu(header->r_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700906
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400907 if (jbd2_journal_has_csum_v2or3(journal))
Jan Kara1101cd42016-02-22 23:19:09 -0500908 csum_size = sizeof(struct jbd2_journal_block_tail);
Darrick J. Wonge531d0b2015-05-14 19:11:50 -0400909 if (rcount > journal->j_blocksize - csum_size)
910 return -EINVAL;
911 max = rcount;
912
Darrick J. Wong56316a02015-10-17 16:18:45 -0400913 if (jbd2_has_feature_64bit(journal))
Zach Brownb517bea2006-10-11 01:21:08 -0700914 record_len = 8;
915
916 while (offset + record_len <= max) {
Mingming Cao18eba7a2006-10-11 01:21:13 -0700917 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700918 int err;
919
Zach Brownb517bea2006-10-11 01:21:08 -0700920 if (record_len == 4)
921 blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
922 else
923 blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
924 offset += record_len;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700925 err = jbd2_journal_set_revoke(journal, blocknr, sequence);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700926 if (err)
927 return err;
928 ++info->nr_revokes;
929 }
930 return 0;
931}