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