1 /*
2 * journal.c --- code for handling the "ext3" journal
3 *
4 * Copyright (C) 2000 Andreas Dilger
5 * Copyright (C) 2000 Theodore Ts'o
6 *
7 * Parts of the code are based on fs/jfs/journal.c by Stephen C. Tweedie
8 * Copyright (C) 1999 Red Hat Software
9 *
10 * This file may be redistributed under the terms of the
11 * GNU General Public License version 2 or at your discretion
12 * any later version.
13 */
14
15 #include "config.h"
16 #ifdef HAVE_SYS_MOUNT_H
17 #include <sys/param.h>
18 #include <sys/mount.h>
19 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
20 #endif
21 #ifdef HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24
25 #define E2FSCK_INCLUDE_INLINE_FUNCS
26 #include "jfs_user.h"
27 #include "problem.h"
28 #include "uuid/uuid.h"
29
30 static int bh_count = 0;
31
32 /*
33 * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
34 * This creates a larger static binary, and a smaller binary using
35 * shared libraries. It's also probably slightly less CPU-efficient,
36 * which is why it's not on by default. But, it's a good way of
37 * testing the functions in inode_io.c and fileio.c.
38 */
39 #undef USE_INODE_IO
40
41 /* Checksumming functions */
e2fsck_journal_verify_csum_type(journal_t * j,journal_superblock_t * jsb)42 static int e2fsck_journal_verify_csum_type(journal_t *j,
43 journal_superblock_t *jsb)
44 {
45 if (!jbd2_journal_has_csum_v2or3(j))
46 return 1;
47
48 return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM;
49 }
50
e2fsck_journal_sb_csum(journal_superblock_t * jsb)51 static __u32 e2fsck_journal_sb_csum(journal_superblock_t *jsb)
52 {
53 __u32 crc, old_crc;
54
55 old_crc = jsb->s_checksum;
56 jsb->s_checksum = 0;
57 crc = ext2fs_crc32c_le(~0, (unsigned char *)jsb,
58 sizeof(journal_superblock_t));
59 jsb->s_checksum = old_crc;
60
61 return crc;
62 }
63
e2fsck_journal_sb_csum_verify(journal_t * j,journal_superblock_t * jsb)64 static int e2fsck_journal_sb_csum_verify(journal_t *j,
65 journal_superblock_t *jsb)
66 {
67 __u32 provided, calculated;
68
69 if (!jbd2_journal_has_csum_v2or3(j))
70 return 1;
71
72 provided = ext2fs_be32_to_cpu(jsb->s_checksum);
73 calculated = e2fsck_journal_sb_csum(jsb);
74
75 return provided == calculated;
76 }
77
e2fsck_journal_sb_csum_set(journal_t * j,journal_superblock_t * jsb)78 static errcode_t e2fsck_journal_sb_csum_set(journal_t *j,
79 journal_superblock_t *jsb)
80 {
81 __u32 crc;
82
83 if (!jbd2_journal_has_csum_v2or3(j))
84 return 0;
85
86 crc = e2fsck_journal_sb_csum(jsb);
87 jsb->s_checksum = ext2fs_cpu_to_be32(crc);
88 return 0;
89 }
90
91 /* Kernel compatibility functions for handling the journal. These allow us
92 * to use the recovery.c file virtually unchanged from the kernel, so we
93 * don't have to do much to keep kernel and user recovery in sync.
94 */
jbd2_journal_bmap(journal_t * journal,unsigned long block,unsigned long long * phys)95 int jbd2_journal_bmap(journal_t *journal, unsigned long block,
96 unsigned long long *phys)
97 {
98 #ifdef USE_INODE_IO
99 *phys = block;
100 return 0;
101 #else
102 struct inode *inode = journal->j_inode;
103 errcode_t retval;
104 blk64_t pblk;
105
106 if (!inode) {
107 *phys = block;
108 return 0;
109 }
110
111 retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino,
112 &inode->i_ext2, NULL, 0, (blk64_t) block,
113 0, &pblk);
114 *phys = pblk;
115 return -1 * ((int) retval);
116 #endif
117 }
118
getblk(kdev_t kdev,unsigned long long blocknr,int blocksize)119 struct buffer_head *getblk(kdev_t kdev, unsigned long long blocknr,
120 int blocksize)
121 {
122 struct buffer_head *bh;
123 int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
124 sizeof(bh->b_data);
125
126 bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
127 if (!bh)
128 return NULL;
129
130 if (journal_enable_debug >= 3)
131 bh_count++;
132 jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
133 blocknr, blocksize, bh_count);
134
135 bh->b_ctx = kdev->k_ctx;
136 if (kdev->k_dev == K_DEV_FS)
137 bh->b_io = kdev->k_ctx->fs->io;
138 else
139 bh->b_io = kdev->k_ctx->journal_io;
140 bh->b_size = blocksize;
141 bh->b_blocknr = blocknr;
142
143 return bh;
144 }
145
sync_blockdev(kdev_t kdev)146 int sync_blockdev(kdev_t kdev)
147 {
148 io_channel io;
149
150 if (kdev->k_dev == K_DEV_FS)
151 io = kdev->k_ctx->fs->io;
152 else
153 io = kdev->k_ctx->journal_io;
154
155 return io_channel_flush(io) ? -EIO : 0;
156 }
157
ll_rw_block(int rw,int op_flags EXT2FS_ATTR ((unused)),int nr,struct buffer_head * bhp[])158 void ll_rw_block(int rw, int op_flags EXT2FS_ATTR((unused)), int nr,
159 struct buffer_head *bhp[])
160 {
161 errcode_t retval;
162 struct buffer_head *bh;
163
164 for (; nr > 0; --nr) {
165 bh = *bhp++;
166 if (rw == REQ_OP_READ && !bh->b_uptodate) {
167 jfs_debug(3, "reading block %llu/%p\n",
168 bh->b_blocknr, (void *) bh);
169 retval = io_channel_read_blk64(bh->b_io,
170 bh->b_blocknr,
171 1, bh->b_data);
172 if (retval) {
173 com_err(bh->b_ctx->device_name, retval,
174 "while reading block %llu\n",
175 bh->b_blocknr);
176 bh->b_err = (int) retval;
177 continue;
178 }
179 bh->b_uptodate = 1;
180 } else if (rw == REQ_OP_WRITE && bh->b_dirty) {
181 jfs_debug(3, "writing block %llu/%p\n",
182 bh->b_blocknr,
183 (void *) bh);
184 retval = io_channel_write_blk64(bh->b_io,
185 bh->b_blocknr,
186 1, bh->b_data);
187 if (retval) {
188 com_err(bh->b_ctx->device_name, retval,
189 "while writing block %llu\n",
190 bh->b_blocknr);
191 bh->b_err = (int) retval;
192 continue;
193 }
194 bh->b_dirty = 0;
195 bh->b_uptodate = 1;
196 } else {
197 jfs_debug(3, "no-op %s for block %llu\n",
198 rw == REQ_OP_READ ? "read" : "write",
199 bh->b_blocknr);
200 }
201 }
202 }
203
mark_buffer_dirty(struct buffer_head * bh)204 void mark_buffer_dirty(struct buffer_head *bh)
205 {
206 bh->b_dirty = 1;
207 }
208
mark_buffer_clean(struct buffer_head * bh)209 static void mark_buffer_clean(struct buffer_head * bh)
210 {
211 bh->b_dirty = 0;
212 }
213
brelse(struct buffer_head * bh)214 void brelse(struct buffer_head *bh)
215 {
216 if (bh->b_dirty)
217 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
218 jfs_debug(3, "freeing block %llu/%p (total %d)\n",
219 bh->b_blocknr, (void *) bh, --bh_count);
220 ext2fs_free_mem(&bh);
221 }
222
buffer_uptodate(struct buffer_head * bh)223 int buffer_uptodate(struct buffer_head *bh)
224 {
225 return bh->b_uptodate;
226 }
227
mark_buffer_uptodate(struct buffer_head * bh,int val)228 void mark_buffer_uptodate(struct buffer_head *bh, int val)
229 {
230 bh->b_uptodate = val;
231 }
232
wait_on_buffer(struct buffer_head * bh)233 void wait_on_buffer(struct buffer_head *bh)
234 {
235 if (!bh->b_uptodate)
236 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
237 }
238
239
e2fsck_clear_recover(e2fsck_t ctx,int error)240 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
241 {
242 ext2fs_clear_feature_journal_needs_recovery(ctx->fs->super);
243
244 /* if we had an error doing journal recovery, we need a full fsck */
245 if (error)
246 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
247 ext2fs_mark_super_dirty(ctx->fs);
248 }
249
250 /*
251 * This is a helper function to check the validity of the journal.
252 */
253 struct process_block_struct {
254 e2_blkcnt_t last_block;
255 };
256
process_journal_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)257 static int process_journal_block(ext2_filsys fs,
258 blk64_t *block_nr,
259 e2_blkcnt_t blockcnt,
260 blk64_t ref_block EXT2FS_ATTR((unused)),
261 int ref_offset EXT2FS_ATTR((unused)),
262 void *priv_data)
263 {
264 struct process_block_struct *p;
265 blk64_t blk = *block_nr;
266
267 p = (struct process_block_struct *) priv_data;
268
269 if (!blk || blk < fs->super->s_first_data_block ||
270 blk >= ext2fs_blocks_count(fs->super))
271 return BLOCK_ABORT;
272
273 if (blockcnt >= 0)
274 p->last_block = blockcnt;
275 return 0;
276 }
277
ext4_fc_replay_scan(journal_t * j,struct buffer_head * bh,int off,tid_t expected_tid)278 static int ext4_fc_replay_scan(journal_t *j, struct buffer_head *bh,
279 int off, tid_t expected_tid)
280 {
281 e2fsck_t ctx = j->j_fs_dev->k_ctx;
282 struct e2fsck_fc_replay_state *state;
283 int ret = JBD2_FC_REPLAY_CONTINUE;
284 struct ext4_fc_add_range ext;
285 struct ext4_fc_tl tl;
286 struct ext4_fc_tail tail;
287 __u8 *start, *cur, *end, *val;
288 struct ext4_fc_head head;
289 struct ext2fs_extent ext2fs_ex = {0};
290
291 state = &ctx->fc_replay_state;
292
293 start = (__u8 *)bh->b_data;
294 end = (__u8 *)bh->b_data + j->j_blocksize - 1;
295
296 jbd_debug(1, "Scan phase starting, expected %d", expected_tid);
297 if (state->fc_replay_expected_off == 0) {
298 memset(state, 0, sizeof(*state));
299 /* Check if we can stop early */
300 if (le16_to_cpu(((struct ext4_fc_tl *)start)->fc_tag)
301 != EXT4_FC_TAG_HEAD) {
302 jbd_debug(1, "Ending early!, not a head tag");
303 return 0;
304 }
305 }
306
307 if (off != state->fc_replay_expected_off) {
308 ret = -EFSCORRUPTED;
309 goto out_err;
310 }
311
312 state->fc_replay_expected_off++;
313 for (cur = start; cur < end; cur = cur + le16_to_cpu(tl.fc_len) + sizeof(tl)) {
314 memcpy(&tl, cur, sizeof(tl));
315 val = cur + sizeof(tl);
316
317 jbd_debug(3, "Scan phase, tag:%s, blk %lld\n",
318 tag2str(le16_to_cpu(tl.fc_tag)), bh->b_blocknr);
319 switch (le16_to_cpu(tl.fc_tag)) {
320 case EXT4_FC_TAG_ADD_RANGE:
321 memcpy(&ext, val, sizeof(ext));
322 ret = ext2fs_decode_extent(&ext2fs_ex,
323 (void *)&ext.fc_ex,
324 sizeof(ext.fc_ex));
325 if (ret)
326 ret = JBD2_FC_REPLAY_STOP;
327 else
328 ret = JBD2_FC_REPLAY_CONTINUE;
329 /* fallthrough */
330 case EXT4_FC_TAG_DEL_RANGE:
331 case EXT4_FC_TAG_LINK:
332 case EXT4_FC_TAG_UNLINK:
333 case EXT4_FC_TAG_CREAT:
334 case EXT4_FC_TAG_INODE:
335 case EXT4_FC_TAG_PAD:
336 state->fc_cur_tag++;
337 state->fc_crc = jbd2_chksum(j, state->fc_crc, cur,
338 sizeof(tl) + ext4_fc_tag_len(&tl));
339 break;
340 case EXT4_FC_TAG_TAIL:
341 state->fc_cur_tag++;
342 memcpy(&tail, val, sizeof(tail));
343 state->fc_crc = jbd2_chksum(j, state->fc_crc, cur,
344 sizeof(tl) +
345 offsetof(struct ext4_fc_tail,
346 fc_crc));
347 jbd_debug(1, "tail tid %d, expected %d\n",
348 le32_to_cpu(tail.fc_tid), expected_tid);
349 if (le32_to_cpu(tail.fc_tid) == expected_tid &&
350 le32_to_cpu(tail.fc_crc) == state->fc_crc) {
351 state->fc_replay_num_tags = state->fc_cur_tag;
352 } else {
353 ret = state->fc_replay_num_tags ?
354 JBD2_FC_REPLAY_STOP : -EFSBADCRC;
355 }
356 state->fc_crc = 0;
357 break;
358 case EXT4_FC_TAG_HEAD:
359 memcpy(&head, val, sizeof(head));
360 if (le32_to_cpu(head.fc_features) &
361 ~EXT4_FC_SUPPORTED_FEATURES) {
362 ret = -EOPNOTSUPP;
363 break;
364 }
365 if (le32_to_cpu(head.fc_tid) != expected_tid) {
366 ret = -EINVAL;
367 break;
368 }
369 state->fc_cur_tag++;
370 state->fc_crc = jbd2_chksum(j, state->fc_crc, cur,
371 sizeof(tl) + ext4_fc_tag_len(&tl));
372 break;
373 default:
374 ret = state->fc_replay_num_tags ?
375 JBD2_FC_REPLAY_STOP : -ECANCELED;
376 }
377 if (ret < 0 || ret == JBD2_FC_REPLAY_STOP)
378 break;
379 }
380
381 out_err:
382 return ret;
383 }
384
__errcode_to_errno(errcode_t err,const char * func,int line)385 static int __errcode_to_errno(errcode_t err, const char *func, int line)
386 {
387 if (err == 0)
388 return 0;
389 fprintf(stderr, "Error \"%s\" encountered in function %s at line %d\n",
390 error_message(err), func, line);
391 if (err <= 256)
392 return -err;
393 return -EFAULT;
394 }
395
396 #define errcode_to_errno(err) __errcode_to_errno(err, __func__, __LINE__)
397
398 #define ex_end(__ex) ((__ex)->e_lblk + (__ex)->e_len - 1)
399 #define ex_pend(__ex) ((__ex)->e_pblk + (__ex)->e_len - 1)
400
make_room(struct extent_list * list,int i)401 static int make_room(struct extent_list *list, int i)
402 {
403 int ret;
404
405 if (list->count == list->size) {
406 unsigned int new_size = (list->size + 341) *
407 sizeof(struct ext2fs_extent);
408 ret = errcode_to_errno(ext2fs_resize_mem(0, new_size, &list->extents));
409 if (ret)
410 return ret;
411 list->size += 341;
412 }
413
414 memmove(&list->extents[i + 1], &list->extents[i],
415 sizeof(list->extents[0]) * (list->count - i));
416 list->count++;
417 return 0;
418 }
419
ex_compar(const void * arg1,const void * arg2)420 static int ex_compar(const void *arg1, const void *arg2)
421 {
422 const struct ext2fs_extent *ex1 = (const struct ext2fs_extent *)arg1;
423 const struct ext2fs_extent *ex2 = (const struct ext2fs_extent *)arg2;
424
425 if (ex1->e_lblk < ex2->e_lblk)
426 return -1;
427 if (ex1->e_lblk > ex2->e_lblk)
428 return 1;
429 return ex1->e_len - ex2->e_len;
430 }
431
ex_len_compar(const void * arg1,const void * arg2)432 static int ex_len_compar(const void *arg1, const void *arg2)
433 {
434 const struct ext2fs_extent *ex1 = (const struct ext2fs_extent *)arg1;
435 const struct ext2fs_extent *ex2 = (const struct ext2fs_extent *)arg2;
436
437 if (ex1->e_len < ex2->e_len)
438 return 1;
439
440 if (ex1->e_lblk > ex2->e_lblk)
441 return -1;
442
443 return 0;
444 }
445
ex_sort_and_merge(struct extent_list * list)446 static void ex_sort_and_merge(struct extent_list *list)
447 {
448 unsigned int i, j;
449
450 if (list->count < 2)
451 return;
452
453 /*
454 * Reverse sort by length, that way we strip off all the 0 length
455 * extents
456 */
457 qsort(list->extents, list->count, sizeof(struct ext2fs_extent),
458 ex_len_compar);
459
460 for (i = 0; i < list->count; i++) {
461 if (list->extents[i].e_len == 0) {
462 list->count = i;
463 break;
464 }
465 }
466
467 if (list->count == 0)
468 return;
469
470 /* Now sort by logical offset */
471 qsort(list->extents, list->count, sizeof(list->extents[0]),
472 ex_compar);
473
474 /* Merge adjacent extents if they are logically and physically contiguous */
475 i = 0;
476 while (i < list->count - 1) {
477 if (ex_end(&list->extents[i]) + 1 != list->extents[i + 1].e_lblk ||
478 ex_pend(&list->extents[i]) + 1 != list->extents[i + 1].e_pblk ||
479 (list->extents[i].e_flags & EXT2_EXTENT_FLAGS_UNINIT) !=
480 (list->extents[i + 1].e_flags & EXT2_EXTENT_FLAGS_UNINIT)) {
481 i++;
482 continue;
483 }
484
485 list->extents[i].e_len += list->extents[i + 1].e_len;
486 for (j = i + 1; j < list->count - 1; j++)
487 list->extents[j] = list->extents[j + 1];
488 list->count--;
489 }
490 }
491
492 /* must free blocks that are released */
ext4_modify_extent_list(e2fsck_t ctx,struct extent_list * list,struct ext2fs_extent * ex,int del)493 static int ext4_modify_extent_list(e2fsck_t ctx, struct extent_list *list,
494 struct ext2fs_extent *ex, int del)
495 {
496 int ret, offset;
497 unsigned int i;
498 struct ext2fs_extent add_ex = *ex;
499
500 /* First let's create a hole from ex->e_lblk of length ex->e_len */
501 for (i = 0; i < list->count; i++) {
502 if (ex_end(&list->extents[i]) < add_ex.e_lblk)
503 continue;
504
505 /* Case 1: No overlap */
506 if (list->extents[i].e_lblk > ex_end(&add_ex))
507 break;
508 /*
509 * Unmark all the blocks in bb now. All the blocks get marked
510 * before we exit this function.
511 */
512 ext2fs_unmark_block_bitmap_range2(ctx->fs->block_map,
513 list->extents[i].e_pblk, list->extents[i].e_len);
514 /* Case 2: Split */
515 if (list->extents[i].e_lblk < add_ex.e_lblk &&
516 ex_end(&list->extents[i]) > ex_end(&add_ex)) {
517 ret = make_room(list, i + 1);
518 if (ret)
519 return ret;
520 list->extents[i + 1] = list->extents[i];
521 offset = ex_end(&add_ex) + 1 - list->extents[i].e_lblk;
522 list->extents[i + 1].e_lblk += offset;
523 list->extents[i + 1].e_pblk += offset;
524 list->extents[i + 1].e_len -= offset;
525 list->extents[i].e_len =
526 add_ex.e_lblk - list->extents[i].e_lblk;
527 break;
528 }
529
530 /* Case 3: Exact overlap */
531 if (add_ex.e_lblk <= list->extents[i].e_lblk &&
532 ex_end(&list->extents[i]) <= ex_end(&add_ex)) {
533
534 list->extents[i].e_len = 0;
535 continue;
536 }
537
538 /* Case 4: Partial overlap */
539 if (ex_end(&list->extents[i]) > ex_end(&add_ex)) {
540 offset = ex_end(&add_ex) + 1 - list->extents[i].e_lblk;
541 list->extents[i].e_lblk += offset;
542 list->extents[i].e_pblk += offset;
543 list->extents[i].e_len -= offset;
544 break;
545 }
546
547 if (ex_end(&add_ex) >= ex_end(&list->extents[i]))
548 list->extents[i].e_len =
549 add_ex.e_lblk > list->extents[i].e_lblk ?
550 add_ex.e_lblk - list->extents[i].e_lblk : 0;
551 }
552
553 if (add_ex.e_len && !del) {
554 make_room(list, list->count);
555 list->extents[list->count - 1] = add_ex;
556 }
557
558 ex_sort_and_merge(list);
559
560 /* Mark all occupied blocks allocated */
561 for (i = 0; i < list->count; i++)
562 ext2fs_mark_block_bitmap_range2(ctx->fs->block_map,
563 list->extents[i].e_pblk, list->extents[i].e_len);
564 ext2fs_mark_bb_dirty(ctx->fs);
565
566 return 0;
567 }
568
ext4_add_extent_to_list(e2fsck_t ctx,struct extent_list * list,struct ext2fs_extent * ex)569 static int ext4_add_extent_to_list(e2fsck_t ctx, struct extent_list *list,
570 struct ext2fs_extent *ex)
571 {
572 return ext4_modify_extent_list(ctx, list, ex, 0 /* add */);
573 }
574
ext4_del_extent_from_list(e2fsck_t ctx,struct extent_list * list,struct ext2fs_extent * ex)575 static int ext4_del_extent_from_list(e2fsck_t ctx, struct extent_list *list,
576 struct ext2fs_extent *ex)
577 {
578 return ext4_modify_extent_list(ctx, list, ex, 1 /* delete */);
579 }
580
ext4_fc_read_extents(e2fsck_t ctx,ino_t ino)581 static int ext4_fc_read_extents(e2fsck_t ctx, ino_t ino)
582 {
583 struct extent_list *extent_list = &ctx->fc_replay_state.fc_extent_list;
584
585 if (extent_list->ino == ino)
586 return 0;
587
588 extent_list->ino = ino;
589 return errcode_to_errno(e2fsck_read_extents(ctx, extent_list));
590 }
591
592 /*
593 * Flush extents in replay state on disk. @ino is the inode that is going
594 * to be processed next. So, we hold back flushing of the extent list
595 * if the next inode that's going to be processed is same as the one with
596 * cached extents in our replay state. That allows us to gather multiple extents
597 * for the inode so that we can flush all of them at once and it also saves us
598 * from continuously growing and shrinking the extent tree.
599 */
ext4_fc_flush_extents(e2fsck_t ctx,ino_t ino)600 static void ext4_fc_flush_extents(e2fsck_t ctx, ino_t ino)
601 {
602 struct extent_list *extent_list = &ctx->fc_replay_state.fc_extent_list;
603
604 if (extent_list->ino == ino || extent_list->ino == 0)
605 return;
606 e2fsck_rewrite_extent_tree(ctx, extent_list);
607 ext2fs_free_mem(&extent_list->extents);
608 memset(extent_list, 0, sizeof(*extent_list));
609 }
610
611 /* Helper struct for dentry replay routines */
612 struct dentry_info_args {
613 ino_t parent_ino;
614 int dname_len;
615 ino_t ino;
616 char *dname;
617 };
618
tl_to_darg(struct dentry_info_args * darg,struct ext4_fc_tl * tl,__u8 * val)619 static inline int tl_to_darg(struct dentry_info_args *darg,
620 struct ext4_fc_tl *tl, __u8 *val)
621 {
622 struct ext4_fc_dentry_info fcd;
623 int tag = le16_to_cpu(tl->fc_tag);
624
625 memcpy(&fcd, val, sizeof(fcd));
626
627 darg->parent_ino = le32_to_cpu(fcd.fc_parent_ino);
628 darg->ino = le32_to_cpu(fcd.fc_ino);
629 darg->dname_len = ext4_fc_tag_len(tl) -
630 sizeof(struct ext4_fc_dentry_info);
631 darg->dname = malloc(darg->dname_len + 1);
632 if (!darg->dname)
633 return -ENOMEM;
634 memcpy(darg->dname,
635 val + sizeof(struct ext4_fc_dentry_info),
636 darg->dname_len);
637 darg->dname[darg->dname_len] = 0;
638 jbd_debug(1, "%s: %s, ino %lu, parent %lu\n",
639 tag == EXT4_FC_TAG_CREAT ? "create" :
640 (tag == EXT4_FC_TAG_LINK ? "link" :
641 (tag == EXT4_FC_TAG_UNLINK ? "unlink" : "error")),
642 darg->dname, darg->ino, darg->parent_ino);
643 return 0;
644 }
645
ext4_fc_handle_unlink(e2fsck_t ctx,struct ext4_fc_tl * tl,__u8 * val)646 static int ext4_fc_handle_unlink(e2fsck_t ctx, struct ext4_fc_tl *tl, __u8 *val)
647 {
648 struct dentry_info_args darg;
649 int ret;
650
651 ret = tl_to_darg(&darg, tl, val);
652 if (ret)
653 return ret;
654 ext4_fc_flush_extents(ctx, darg.ino);
655 ret = errcode_to_errno(
656 ext2fs_unlink(ctx->fs, darg.parent_ino,
657 darg.dname, darg.ino, 0));
658 /* It's okay if the above call fails */
659 free(darg.dname);
660 return ret;
661 }
662
ext4_fc_handle_link_and_create(e2fsck_t ctx,struct ext4_fc_tl * tl,__u8 * val)663 static int ext4_fc_handle_link_and_create(e2fsck_t ctx, struct ext4_fc_tl *tl, __u8 *val)
664 {
665 struct dentry_info_args darg;
666 ext2_filsys fs = ctx->fs;
667 struct ext2_inode_large inode_large;
668 int ret, filetype, mode;
669
670 ret = tl_to_darg(&darg, tl, val);
671 if (ret)
672 return ret;
673 ext4_fc_flush_extents(ctx, 0);
674 ret = errcode_to_errno(ext2fs_read_inode(fs, darg.ino,
675 (struct ext2_inode *)&inode_large));
676 if (ret)
677 goto out;
678
679 mode = inode_large.i_mode;
680
681 if (LINUX_S_ISREG(mode))
682 filetype = EXT2_FT_REG_FILE;
683 else if (LINUX_S_ISDIR(mode))
684 filetype = EXT2_FT_DIR;
685 else if (LINUX_S_ISCHR(mode))
686 filetype = EXT2_FT_CHRDEV;
687 else if (LINUX_S_ISBLK(mode))
688 filetype = EXT2_FT_BLKDEV;
689 else if (LINUX_S_ISLNK(mode))
690 return EXT2_FT_SYMLINK;
691 else if (LINUX_S_ISFIFO(mode))
692 filetype = EXT2_FT_FIFO;
693 else if (LINUX_S_ISSOCK(mode))
694 filetype = EXT2_FT_SOCK;
695 else {
696 ret = -EINVAL;
697 goto out;
698 }
699
700 /*
701 * Forcefully unlink if the same name is present and ignore the error
702 * if any, since this dirent might not exist
703 */
704 ext2fs_unlink(fs, darg.parent_ino, darg.dname, darg.ino,
705 EXT2FS_UNLINK_FORCE);
706
707 ret = errcode_to_errno(
708 ext2fs_link(fs, darg.parent_ino, darg.dname, darg.ino,
709 filetype));
710 out:
711 free(darg.dname);
712 return ret;
713
714 }
715
716 /* This function fixes the i_blocks field in the replayed indoe */
ext4_fc_replay_fixup_iblocks(struct ext2_inode_large * ondisk_inode,struct ext2_inode_large * fc_inode)717 static void ext4_fc_replay_fixup_iblocks(struct ext2_inode_large *ondisk_inode,
718 struct ext2_inode_large *fc_inode)
719 {
720 if (ondisk_inode->i_flags & EXT4_EXTENTS_FL) {
721 struct ext3_extent_header *eh;
722
723 eh = (struct ext3_extent_header *)(&ondisk_inode->i_block[0]);
724 if (le16_to_cpu(eh->eh_magic) != EXT3_EXT_MAGIC) {
725 memset(eh, 0, sizeof(*eh));
726 eh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
727 eh->eh_max = cpu_to_le16(
728 (sizeof(ondisk_inode->i_block) -
729 sizeof(struct ext3_extent_header)) /
730 sizeof(struct ext3_extent));
731 }
732 } else if (ondisk_inode->i_flags & EXT4_INLINE_DATA_FL) {
733 memcpy(ondisk_inode->i_block, fc_inode->i_block,
734 sizeof(fc_inode->i_block));
735 }
736 }
737
ext4_fc_handle_inode(e2fsck_t ctx,__u8 * val)738 static int ext4_fc_handle_inode(e2fsck_t ctx, __u8 *val)
739 {
740 int ino, inode_len = EXT2_GOOD_OLD_INODE_SIZE;
741 struct ext2_inode_large *inode = NULL, *fc_inode = NULL;
742 __le32 fc_ino;
743 __u8 *fc_raw_inode;
744 errcode_t err;
745 blk64_t blks;
746
747 memcpy(&fc_ino, val, sizeof(fc_ino));
748 fc_raw_inode = val + sizeof(fc_ino);
749 ino = le32_to_cpu(fc_ino);
750
751 if (EXT2_INODE_SIZE(ctx->fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
752 inode_len += ext2fs_le16_to_cpu(
753 ((struct ext2_inode_large *)fc_raw_inode)->i_extra_isize);
754 err = ext2fs_get_mem(inode_len, &inode);
755 if (err)
756 goto out;
757 err = ext2fs_get_mem(inode_len, &fc_inode);
758 if (err)
759 goto out;
760 ext4_fc_flush_extents(ctx, ino);
761
762 err = ext2fs_read_inode_full(ctx->fs, ino, (struct ext2_inode *)inode,
763 inode_len);
764 if (err)
765 goto out;
766 memcpy(fc_inode, fc_raw_inode, inode_len);
767 #ifdef WORDS_BIGENDIAN
768 ext2fs_swap_inode_full(ctx->fs, fc_inode, fc_inode, 0, inode_len);
769 #endif
770 memcpy(inode, fc_inode, offsetof(struct ext2_inode_large, i_block));
771 memcpy(&inode->i_generation, &fc_inode->i_generation,
772 inode_len - offsetof(struct ext2_inode_large, i_generation));
773 ext4_fc_replay_fixup_iblocks(inode, fc_inode);
774 err = ext2fs_count_blocks(ctx->fs, ino, EXT2_INODE(inode), &blks);
775 if (err)
776 goto out;
777 ext2fs_iblk_set(ctx->fs, EXT2_INODE(inode), blks);
778 ext2fs_inode_csum_set(ctx->fs, ino, inode);
779
780 err = ext2fs_write_inode_full(ctx->fs, ino, (struct ext2_inode *)inode,
781 inode_len);
782 if (err)
783 goto out;
784 if (inode->i_links_count)
785 ext2fs_mark_inode_bitmap2(ctx->fs->inode_map, ino);
786 else
787 ext2fs_unmark_inode_bitmap2(ctx->fs->inode_map, ino);
788 ext2fs_mark_ib_dirty(ctx->fs);
789
790 out:
791 ext2fs_free_mem(&inode);
792 ext2fs_free_mem(&fc_inode);
793 return errcode_to_errno(err);
794 }
795
796 /*
797 * Handle add extent replay tag.
798 */
ext4_fc_handle_add_extent(e2fsck_t ctx,__u8 * val)799 static int ext4_fc_handle_add_extent(e2fsck_t ctx, __u8 *val)
800 {
801 struct ext2fs_extent extent;
802 struct ext4_fc_add_range add_range;
803 ino_t ino;
804 int ret = 0;
805
806 memcpy(&add_range, val, sizeof(add_range));
807 ino = le32_to_cpu(add_range.fc_ino);
808 ext4_fc_flush_extents(ctx, ino);
809
810 ret = ext4_fc_read_extents(ctx, ino);
811 if (ret)
812 return ret;
813 memset(&extent, 0, sizeof(extent));
814 ret = errcode_to_errno(ext2fs_decode_extent(
815 &extent, (void *)add_range.fc_ex,
816 sizeof(add_range.fc_ex)));
817 if (ret)
818 return ret;
819 return ext4_add_extent_to_list(ctx,
820 &ctx->fc_replay_state.fc_extent_list, &extent);
821 }
822
823 /*
824 * Handle delete logical range replay tag.
825 */
ext4_fc_handle_del_range(e2fsck_t ctx,__u8 * val)826 static int ext4_fc_handle_del_range(e2fsck_t ctx, __u8 *val)
827 {
828 struct ext2fs_extent extent;
829 struct ext4_fc_del_range del_range;
830 int ret, ino;
831
832 memcpy(&del_range, val, sizeof(del_range));
833 ino = le32_to_cpu(del_range.fc_ino);
834 ext4_fc_flush_extents(ctx, ino);
835
836 memset(&extent, 0, sizeof(extent));
837 extent.e_lblk = le32_to_cpu(del_range.fc_lblk);
838 extent.e_len = le32_to_cpu(del_range.fc_len);
839 ret = ext4_fc_read_extents(ctx, ino);
840 if (ret)
841 return ret;
842 return ext4_del_extent_from_list(ctx,
843 &ctx->fc_replay_state.fc_extent_list, &extent);
844 }
845
846 /*
847 * Main recovery path entry point. This function returns JBD2_FC_REPLAY_CONTINUE
848 * to indicate that it is expecting more fast commit blocks. It returns
849 * JBD2_FC_REPLAY_STOP to indicate that replay is done.
850 */
ext4_fc_replay(journal_t * journal,struct buffer_head * bh,enum passtype pass,int off,tid_t expected_tid)851 static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
852 enum passtype pass, int off, tid_t expected_tid)
853 {
854 e2fsck_t ctx = journal->j_fs_dev->k_ctx;
855 struct e2fsck_fc_replay_state *state = &ctx->fc_replay_state;
856 int ret = JBD2_FC_REPLAY_CONTINUE;
857 struct ext4_fc_tl tl;
858 __u8 *start, *end, *cur, *val;
859
860 if (pass == PASS_SCAN) {
861 state->fc_current_pass = PASS_SCAN;
862 return ext4_fc_replay_scan(journal, bh, off, expected_tid);
863 }
864
865 if (state->fc_replay_num_tags == 0)
866 goto replay_done;
867
868 if (state->fc_current_pass != pass) {
869 /* Starting replay phase */
870 state->fc_current_pass = pass;
871 /* We will reset checksums */
872 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
873 ret = errcode_to_errno(ext2fs_read_bitmaps(ctx->fs));
874 if (ret) {
875 jbd_debug(1, "Error %d while reading bitmaps\n", ret);
876 return ret;
877 }
878 state->fc_super_state = ctx->fs->super->s_state;
879 /*
880 * Mark the file system to indicate it contains errors. That's
881 * because the updates performed by fast commit replay code are
882 * not atomic and may result in incosistent file system if it
883 * crashes before the replay is complete.
884 */
885 ctx->fs->super->s_state |= EXT2_ERROR_FS;
886 ctx->fs->super->s_state |= EXT4_FC_REPLAY;
887 ext2fs_mark_super_dirty(ctx->fs);
888 ext2fs_flush(ctx->fs);
889 }
890
891 start = (__u8 *)bh->b_data;
892 end = (__u8 *)bh->b_data + journal->j_blocksize - 1;
893
894 for (cur = start; cur < end; cur = cur + le16_to_cpu(tl.fc_len) + sizeof(tl)) {
895 memcpy(&tl, cur, sizeof(tl));
896 val = cur + sizeof(tl);
897
898 if (state->fc_replay_num_tags == 0)
899 goto replay_done;
900 jbd_debug(3, "Replay phase processing %s tag\n",
901 tag2str(le16_to_cpu(tl.fc_tag)));
902 state->fc_replay_num_tags--;
903 switch (le16_to_cpu(tl.fc_tag)) {
904 case EXT4_FC_TAG_CREAT:
905 case EXT4_FC_TAG_LINK:
906 ret = ext4_fc_handle_link_and_create(ctx, &tl, val);
907 break;
908 case EXT4_FC_TAG_UNLINK:
909 ret = ext4_fc_handle_unlink(ctx, &tl, val);
910 break;
911 case EXT4_FC_TAG_ADD_RANGE:
912 ret = ext4_fc_handle_add_extent(ctx, val);
913 break;
914 case EXT4_FC_TAG_DEL_RANGE:
915 ret = ext4_fc_handle_del_range(ctx, val);
916 break;
917 case EXT4_FC_TAG_INODE:
918 ret = ext4_fc_handle_inode(ctx, val);
919 break;
920 case EXT4_FC_TAG_TAIL:
921 ext4_fc_flush_extents(ctx, 0);
922 case EXT4_FC_TAG_PAD:
923 case EXT4_FC_TAG_HEAD:
924 break;
925 default:
926 ret = -ECANCELED;
927 break;
928 }
929 if (ret < 0)
930 break;
931 ret = JBD2_FC_REPLAY_CONTINUE;
932 }
933 return ret;
934 replay_done:
935 jbd_debug(1, "End of fast commit replay\n");
936 if (state->fc_current_pass != pass)
937 return JBD2_FC_REPLAY_STOP;
938
939 ext2fs_calculate_summary_stats(ctx->fs, 0 /* update bg also */);
940 ext2fs_write_block_bitmap(ctx->fs);
941 ext2fs_write_inode_bitmap(ctx->fs);
942 ext2fs_mark_super_dirty(ctx->fs);
943 ext2fs_set_gdt_csum(ctx->fs);
944 ctx->fs->super->s_state = state->fc_super_state;
945 ext2fs_flush(ctx->fs);
946
947 return JBD2_FC_REPLAY_STOP;
948 }
949
e2fsck_get_journal(e2fsck_t ctx,journal_t ** ret_journal)950 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
951 {
952 struct process_block_struct pb;
953 struct ext2_super_block *sb = ctx->fs->super;
954 struct ext2_super_block jsuper;
955 struct problem_context pctx;
956 struct buffer_head *bh;
957 struct inode *j_inode = NULL;
958 struct kdev_s *dev_fs = NULL, *dev_journal;
959 const char *journal_name = 0;
960 journal_t *journal = NULL;
961 errcode_t retval = 0;
962 io_manager io_ptr = 0;
963 unsigned long long start = 0;
964 int ret;
965 int ext_journal = 0;
966 int tried_backup_jnl = 0;
967
968 clear_problem_context(&pctx);
969
970 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
971 if (!journal) {
972 return EXT2_ET_NO_MEMORY;
973 }
974
975 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
976 if (!dev_fs) {
977 retval = EXT2_ET_NO_MEMORY;
978 goto errout;
979 }
980 dev_journal = dev_fs+1;
981
982 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
983 dev_fs->k_dev = K_DEV_FS;
984 dev_journal->k_dev = K_DEV_JOURNAL;
985
986 journal->j_dev = dev_journal;
987 journal->j_fs_dev = dev_fs;
988 journal->j_inode = NULL;
989 journal->j_blocksize = ctx->fs->blocksize;
990
991 if (uuid_is_null(sb->s_journal_uuid)) {
992 if (!sb->s_journal_inum) {
993 retval = EXT2_ET_BAD_INODE_NUM;
994 goto errout;
995 }
996 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
997 "journal inode");
998 if (!j_inode) {
999 retval = EXT2_ET_NO_MEMORY;
1000 goto errout;
1001 }
1002
1003 j_inode->i_ctx = ctx;
1004 j_inode->i_ino = sb->s_journal_inum;
1005
1006 if ((retval = ext2fs_read_inode(ctx->fs,
1007 sb->s_journal_inum,
1008 &j_inode->i_ext2))) {
1009 try_backup_journal:
1010 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
1011 tried_backup_jnl)
1012 goto errout;
1013 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
1014 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
1015 EXT2_N_BLOCKS*4);
1016 j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
1017 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
1018 j_inode->i_ext2.i_links_count = 1;
1019 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
1020 e2fsck_use_inode_shortcuts(ctx, 1);
1021 ctx->stashed_ino = j_inode->i_ino;
1022 ctx->stashed_inode = &j_inode->i_ext2;
1023 tried_backup_jnl++;
1024 }
1025 if (!j_inode->i_ext2.i_links_count ||
1026 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
1027 retval = EXT2_ET_NO_JOURNAL;
1028 goto try_backup_journal;
1029 }
1030 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
1031 JBD2_MIN_JOURNAL_BLOCKS) {
1032 retval = EXT2_ET_JOURNAL_TOO_SMALL;
1033 goto try_backup_journal;
1034 }
1035 pb.last_block = -1;
1036 retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
1037 BLOCK_FLAG_HOLE, 0,
1038 process_journal_block, &pb);
1039 if ((pb.last_block + 1) * ctx->fs->blocksize <
1040 (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
1041 retval = EXT2_ET_JOURNAL_TOO_SMALL;
1042 goto try_backup_journal;
1043 }
1044 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
1045 retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
1046 &j_inode->i_ext2);
1047 if (retval)
1048 goto errout;
1049 }
1050
1051 journal->j_total_len = EXT2_I_SIZE(&j_inode->i_ext2) /
1052 journal->j_blocksize;
1053
1054 #ifdef USE_INODE_IO
1055 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
1056 &j_inode->i_ext2,
1057 &journal_name);
1058 if (retval)
1059 goto errout;
1060
1061 io_ptr = inode_io_manager;
1062 #else
1063 journal->j_inode = j_inode;
1064 ctx->journal_io = ctx->fs->io;
1065 if ((ret = jbd2_journal_bmap(journal, 0, &start)) != 0) {
1066 retval = (errcode_t) (-1 * ret);
1067 goto errout;
1068 }
1069 #endif
1070 } else {
1071 ext_journal = 1;
1072 if (!ctx->journal_name) {
1073 char uuid[37];
1074
1075 uuid_unparse(sb->s_journal_uuid, uuid);
1076 ctx->journal_name = blkid_get_devname(ctx->blkid,
1077 "UUID", uuid);
1078 if (!ctx->journal_name)
1079 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
1080 }
1081 journal_name = ctx->journal_name;
1082
1083 if (!journal_name) {
1084 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
1085 retval = EXT2_ET_LOAD_EXT_JOURNAL;
1086 goto errout;
1087 }
1088
1089 jfs_debug(1, "Using journal file %s\n", journal_name);
1090 io_ptr = unix_io_manager;
1091 }
1092
1093 #if 0
1094 test_io_backing_manager = io_ptr;
1095 io_ptr = test_io_manager;
1096 #endif
1097 #ifndef USE_INODE_IO
1098 if (ext_journal)
1099 #endif
1100 {
1101 int flags = IO_FLAG_RW;
1102 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
1103 ctx->mount_flags & EXT2_MF_READONLY))
1104 flags |= IO_FLAG_EXCLUSIVE;
1105 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
1106 (ctx->options & E2F_OPT_FORCE))
1107 flags &= ~IO_FLAG_EXCLUSIVE;
1108
1109
1110 retval = io_ptr->open(journal_name, flags,
1111 &ctx->journal_io);
1112 }
1113 if (retval)
1114 goto errout;
1115
1116 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
1117
1118 if (ext_journal) {
1119 blk64_t maxlen;
1120
1121 start = ext2fs_journal_sb_start(ctx->fs->blocksize) - 1;
1122 bh = getblk(dev_journal, start, ctx->fs->blocksize);
1123 if (!bh) {
1124 retval = EXT2_ET_NO_MEMORY;
1125 goto errout;
1126 }
1127 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1128 if ((retval = bh->b_err) != 0) {
1129 brelse(bh);
1130 goto errout;
1131 }
1132 memcpy(&jsuper, start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET,
1133 sizeof(jsuper));
1134 #ifdef WORDS_BIGENDIAN
1135 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
1136 ext2fs_swap_super(&jsuper);
1137 #endif
1138 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
1139 !ext2fs_has_feature_journal_dev(&jsuper)) {
1140 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
1141 retval = EXT2_ET_LOAD_EXT_JOURNAL;
1142 brelse(bh);
1143 goto errout;
1144 }
1145 /* Make sure the journal UUID is correct */
1146 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
1147 sizeof(jsuper.s_uuid))) {
1148 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
1149 retval = EXT2_ET_LOAD_EXT_JOURNAL;
1150 brelse(bh);
1151 goto errout;
1152 }
1153
1154 /* Check the superblock checksum */
1155 if (ext2fs_has_feature_metadata_csum(&jsuper)) {
1156 struct struct_ext2_filsys fsx;
1157 struct ext2_super_block superx;
1158 void *p;
1159
1160 p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
1161 memcpy(&fsx, ctx->fs, sizeof(fsx));
1162 memcpy(&superx, ctx->fs->super, sizeof(superx));
1163 fsx.super = &superx;
1164 ext2fs_set_feature_metadata_csum(fsx.super);
1165 if (!ext2fs_superblock_csum_verify(&fsx, p) &&
1166 fix_problem(ctx, PR_0_EXT_JOURNAL_SUPER_CSUM_INVALID,
1167 &pctx)) {
1168 ext2fs_superblock_csum_set(&fsx, p);
1169 mark_buffer_dirty(bh);
1170 }
1171 }
1172 brelse(bh);
1173
1174 maxlen = ext2fs_blocks_count(&jsuper);
1175 journal->j_total_len = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1;
1176 start++;
1177 }
1178
1179 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
1180 retval = EXT2_ET_NO_MEMORY;
1181 goto errout;
1182 }
1183
1184 journal->j_sb_buffer = bh;
1185 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1186 if (ext2fs_has_feature_fast_commit(ctx->fs->super))
1187 journal->j_fc_replay_callback = ext4_fc_replay;
1188 else
1189 journal->j_fc_replay_callback = NULL;
1190
1191 #ifdef USE_INODE_IO
1192 if (j_inode)
1193 ext2fs_free_mem(&j_inode);
1194 #endif
1195
1196 *ret_journal = journal;
1197 e2fsck_use_inode_shortcuts(ctx, 0);
1198 return 0;
1199
1200 errout:
1201 e2fsck_use_inode_shortcuts(ctx, 0);
1202 if (dev_fs)
1203 ext2fs_free_mem(&dev_fs);
1204 if (j_inode)
1205 ext2fs_free_mem(&j_inode);
1206 if (journal)
1207 ext2fs_free_mem(&journal);
1208 return retval;
1209 }
1210
e2fsck_journal_fix_bad_inode(e2fsck_t ctx,struct problem_context * pctx)1211 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
1212 struct problem_context *pctx)
1213 {
1214 struct ext2_super_block *sb = ctx->fs->super;
1215 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
1216 int has_journal = ext2fs_has_feature_journal(ctx->fs->super);
1217
1218 if (has_journal || sb->s_journal_inum) {
1219 /* The journal inode is bogus, remove and force full fsck */
1220 pctx->ino = sb->s_journal_inum;
1221 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
1222 if (has_journal && sb->s_journal_inum)
1223 printf("*** journal has been deleted ***\n\n");
1224 ext2fs_clear_feature_journal(sb);
1225 sb->s_journal_inum = 0;
1226 memset(sb->s_jnl_blocks, 0, sizeof(sb->s_jnl_blocks));
1227 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
1228 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1229 e2fsck_clear_recover(ctx, 1);
1230 return 0;
1231 }
1232 return EXT2_ET_CORRUPT_JOURNAL_SB;
1233 } else if (recover) {
1234 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
1235 e2fsck_clear_recover(ctx, 1);
1236 return 0;
1237 }
1238 return EXT2_ET_UNSUPP_FEATURE;
1239 }
1240 return 0;
1241 }
1242
1243 #define V1_SB_SIZE 0x0024
clear_v2_journal_fields(journal_t * journal)1244 static void clear_v2_journal_fields(journal_t *journal)
1245 {
1246 e2fsck_t ctx = journal->j_dev->k_ctx;
1247 struct problem_context pctx;
1248
1249 clear_problem_context(&pctx);
1250
1251 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
1252 return;
1253
1254 ctx->flags |= E2F_FLAG_PROBLEMS_FIXED;
1255 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
1256 ctx->fs->blocksize-V1_SB_SIZE);
1257 mark_buffer_dirty(journal->j_sb_buffer);
1258 }
1259
1260
e2fsck_journal_load(journal_t * journal)1261 static errcode_t e2fsck_journal_load(journal_t *journal)
1262 {
1263 e2fsck_t ctx = journal->j_dev->k_ctx;
1264 journal_superblock_t *jsb;
1265 struct buffer_head *jbh = journal->j_sb_buffer;
1266 struct problem_context pctx;
1267
1268 clear_problem_context(&pctx);
1269
1270 ll_rw_block(REQ_OP_READ, 0, 1, &jbh);
1271 if (jbh->b_err) {
1272 com_err(ctx->device_name, jbh->b_err, "%s",
1273 _("reading journal superblock\n"));
1274 return jbh->b_err;
1275 }
1276
1277 jsb = journal->j_superblock;
1278 /* If we don't even have JBD2_MAGIC, we probably have a wrong inode */
1279 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER))
1280 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
1281
1282 switch (ntohl(jsb->s_header.h_blocktype)) {
1283 case JBD2_SUPERBLOCK_V1:
1284 journal->j_format_version = 1;
1285 if (jsb->s_feature_compat ||
1286 jsb->s_feature_incompat ||
1287 jsb->s_feature_ro_compat ||
1288 jsb->s_nr_users)
1289 clear_v2_journal_fields(journal);
1290 break;
1291
1292 case JBD2_SUPERBLOCK_V2:
1293 journal->j_format_version = 2;
1294 if (ntohl(jsb->s_nr_users) > 1 &&
1295 uuid_is_null(ctx->fs->super->s_journal_uuid))
1296 clear_v2_journal_fields(journal);
1297 if (ntohl(jsb->s_nr_users) > 1) {
1298 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
1299 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
1300 }
1301 break;
1302
1303 /*
1304 * These should never appear in a journal super block, so if
1305 * they do, the journal is badly corrupted.
1306 */
1307 case JBD2_DESCRIPTOR_BLOCK:
1308 case JBD2_COMMIT_BLOCK:
1309 case JBD2_REVOKE_BLOCK:
1310 return EXT2_ET_CORRUPT_JOURNAL_SB;
1311
1312 /* If we don't understand the superblock major type, but there
1313 * is a magic number, then it is likely to be a new format we
1314 * just don't understand, so leave it alone. */
1315 default:
1316 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
1317 }
1318
1319 if (JBD2_HAS_INCOMPAT_FEATURE(journal, ~JBD2_KNOWN_INCOMPAT_FEATURES))
1320 return EXT2_ET_UNSUPP_FEATURE;
1321
1322 if (JBD2_HAS_RO_COMPAT_FEATURE(journal, ~JBD2_KNOWN_ROCOMPAT_FEATURES))
1323 return EXT2_ET_RO_UNSUPP_FEATURE;
1324
1325 /* Checksum v1-3 are mutually exclusive features. */
1326 if (jbd2_has_feature_csum2(journal) && jbd2_has_feature_csum3(journal))
1327 return EXT2_ET_CORRUPT_JOURNAL_SB;
1328
1329 if (jbd2_journal_has_csum_v2or3(journal) &&
1330 jbd2_has_feature_checksum(journal))
1331 return EXT2_ET_CORRUPT_JOURNAL_SB;
1332
1333 if (!e2fsck_journal_verify_csum_type(journal, jsb) ||
1334 !e2fsck_journal_sb_csum_verify(journal, jsb))
1335 return EXT2_ET_CORRUPT_JOURNAL_SB;
1336
1337 if (jbd2_journal_has_csum_v2or3(journal))
1338 journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid,
1339 sizeof(jsb->s_uuid));
1340
1341 /* We have now checked whether we know enough about the journal
1342 * format to be able to proceed safely, so any other checks that
1343 * fail we should attempt to recover from. */
1344 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
1345 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
1346 _("%s: no valid journal superblock found\n"),
1347 ctx->device_name);
1348 return EXT2_ET_CORRUPT_JOURNAL_SB;
1349 }
1350
1351 if (ntohl(jsb->s_maxlen) < journal->j_total_len)
1352 journal->j_total_len = ntohl(jsb->s_maxlen);
1353 else if (ntohl(jsb->s_maxlen) > journal->j_total_len) {
1354 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
1355 _("%s: journal too short\n"),
1356 ctx->device_name);
1357 return EXT2_ET_CORRUPT_JOURNAL_SB;
1358 }
1359
1360 journal->j_tail_sequence = ntohl(jsb->s_sequence);
1361 journal->j_transaction_sequence = journal->j_tail_sequence;
1362 journal->j_tail = ntohl(jsb->s_start);
1363 journal->j_first = ntohl(jsb->s_first);
1364 if (jbd2_has_feature_fast_commit(journal)) {
1365 if (ntohl(jsb->s_maxlen) - jbd2_journal_get_num_fc_blks(jsb)
1366 < JBD2_MIN_JOURNAL_BLOCKS) {
1367 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
1368 _("%s: incorrect fast commit blocks\n"),
1369 ctx->device_name);
1370 return EXT2_ET_CORRUPT_JOURNAL_SB;
1371 }
1372 journal->j_fc_last = ntohl(jsb->s_maxlen);
1373 journal->j_last = journal->j_fc_last -
1374 jbd2_journal_get_num_fc_blks(jsb);
1375 journal->j_fc_first = journal->j_last + 1;
1376 } else {
1377 journal->j_last = ntohl(jsb->s_maxlen);
1378 }
1379
1380 return 0;
1381 }
1382
e2fsck_journal_reset_super(e2fsck_t ctx,journal_superblock_t * jsb,journal_t * journal)1383 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
1384 journal_t *journal)
1385 {
1386 char *p;
1387 union {
1388 uuid_t uuid;
1389 __u32 val[4];
1390 } u;
1391 __u32 new_seq = 0;
1392 int i;
1393
1394 /* Leave a valid existing V1 superblock signature alone.
1395 * Anything unrecognisable we overwrite with a new V2
1396 * signature. */
1397
1398 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER) ||
1399 jsb->s_header.h_blocktype != htonl(JBD2_SUPERBLOCK_V1)) {
1400 jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
1401 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
1402 }
1403
1404 /* Zero out everything else beyond the superblock header */
1405
1406 p = ((char *) jsb) + sizeof(journal_header_t);
1407 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
1408
1409 jsb->s_blocksize = htonl(ctx->fs->blocksize);
1410 jsb->s_maxlen = htonl(journal->j_total_len);
1411 jsb->s_first = htonl(1);
1412
1413 /* Initialize the journal sequence number so that there is "no"
1414 * chance we will find old "valid" transactions in the journal.
1415 * This avoids the need to zero the whole journal (slow to do,
1416 * and risky when we are just recovering the filesystem).
1417 */
1418 uuid_generate(u.uuid);
1419 for (i = 0; i < 4; i ++)
1420 new_seq ^= u.val[i];
1421 jsb->s_sequence = htonl(new_seq);
1422 e2fsck_journal_sb_csum_set(journal, jsb);
1423
1424 mark_buffer_dirty(journal->j_sb_buffer);
1425 ll_rw_block(REQ_OP_WRITE, 0, 1, &journal->j_sb_buffer);
1426 }
1427
e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,journal_t * journal,struct problem_context * pctx)1428 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
1429 journal_t *journal,
1430 struct problem_context *pctx)
1431 {
1432 struct ext2_super_block *sb = ctx->fs->super;
1433 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
1434
1435 if (ext2fs_has_feature_journal(sb)) {
1436 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
1437 e2fsck_journal_reset_super(ctx, journal->j_superblock,
1438 journal);
1439 journal->j_transaction_sequence = 1;
1440 e2fsck_clear_recover(ctx, recover);
1441 return 0;
1442 }
1443 return EXT2_ET_CORRUPT_JOURNAL_SB;
1444 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
1445 return EXT2_ET_CORRUPT_JOURNAL_SB;
1446
1447 return 0;
1448 }
1449
e2fsck_journal_release(e2fsck_t ctx,journal_t * journal,int reset,int drop)1450 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
1451 int reset, int drop)
1452 {
1453 journal_superblock_t *jsb;
1454
1455 if (drop)
1456 mark_buffer_clean(journal->j_sb_buffer);
1457 else if (!(ctx->options & E2F_OPT_READONLY)) {
1458 jsb = journal->j_superblock;
1459 jsb->s_sequence = htonl(journal->j_tail_sequence);
1460 if (reset)
1461 jsb->s_start = 0; /* this marks the journal as empty */
1462 e2fsck_journal_sb_csum_set(journal, jsb);
1463 mark_buffer_dirty(journal->j_sb_buffer);
1464 }
1465 brelse(journal->j_sb_buffer);
1466
1467 if (ctx->journal_io) {
1468 if (ctx->fs && ctx->fs->io != ctx->journal_io)
1469 io_channel_close(ctx->journal_io);
1470 ctx->journal_io = 0;
1471 }
1472
1473 #ifndef USE_INODE_IO
1474 if (journal->j_inode)
1475 ext2fs_free_mem(&journal->j_inode);
1476 #endif
1477 if (journal->j_fs_dev)
1478 ext2fs_free_mem(&journal->j_fs_dev);
1479 ext2fs_free_mem(&journal);
1480 }
1481
1482 /*
1483 * This function makes sure that the superblock fields regarding the
1484 * journal are consistent.
1485 */
e2fsck_check_ext3_journal(e2fsck_t ctx)1486 errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
1487 {
1488 struct ext2_super_block *sb = ctx->fs->super;
1489 journal_t *journal;
1490 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
1491 struct problem_context pctx;
1492 problem_t problem;
1493 int reset = 0, force_fsck = 0;
1494 errcode_t retval;
1495
1496 /* If we don't have any journal features, don't do anything more */
1497 if (!ext2fs_has_feature_journal(sb) &&
1498 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
1499 uuid_is_null(sb->s_journal_uuid))
1500 return 0;
1501
1502 clear_problem_context(&pctx);
1503 pctx.num = sb->s_journal_inum;
1504
1505 retval = e2fsck_get_journal(ctx, &journal);
1506 if (retval) {
1507 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
1508 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
1509 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
1510 (retval == EXT2_ET_NO_JOURNAL))
1511 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
1512 return retval;
1513 }
1514
1515 retval = e2fsck_journal_load(journal);
1516 if (retval) {
1517 if ((retval == EXT2_ET_CORRUPT_JOURNAL_SB) ||
1518 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
1519 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
1520 &pctx))) ||
1521 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
1522 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
1523 &pctx))) ||
1524 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
1525 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
1526 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
1527 &pctx);
1528 e2fsck_journal_release(ctx, journal, 0, 1);
1529 return retval;
1530 }
1531
1532 /*
1533 * We want to make the flags consistent here. We will not leave with
1534 * needs_recovery set but has_journal clear. We can't get in a loop
1535 * with -y, -n, or -p, only if a user isn't making up their mind.
1536 */
1537 no_has_journal:
1538 if (!ext2fs_has_feature_journal(sb)) {
1539 recover = ext2fs_has_feature_journal_needs_recovery(sb);
1540 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
1541 if (recover &&
1542 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
1543 goto no_has_journal;
1544 /*
1545 * Need a full fsck if we are releasing a
1546 * journal stored on a reserved inode.
1547 */
1548 force_fsck = recover ||
1549 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
1550 /* Clear all of the journal fields */
1551 sb->s_journal_inum = 0;
1552 sb->s_journal_dev = 0;
1553 memset(sb->s_journal_uuid, 0,
1554 sizeof(sb->s_journal_uuid));
1555 e2fsck_clear_recover(ctx, force_fsck);
1556 } else if (!(ctx->options & E2F_OPT_READONLY)) {
1557 ext2fs_set_feature_journal(sb);
1558 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1559 ext2fs_mark_super_dirty(ctx->fs);
1560 }
1561 }
1562
1563 if (ext2fs_has_feature_journal(sb) &&
1564 !ext2fs_has_feature_journal_needs_recovery(sb) &&
1565 journal->j_superblock->s_start != 0) {
1566 /* Print status information */
1567 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
1568 if (ctx->superblock)
1569 problem = PR_0_JOURNAL_RUN_DEFAULT;
1570 else
1571 problem = PR_0_JOURNAL_RUN;
1572 if (fix_problem(ctx, problem, &pctx)) {
1573 ctx->options |= E2F_OPT_FORCE;
1574 ext2fs_set_feature_journal_needs_recovery(sb);
1575 ext2fs_mark_super_dirty(ctx->fs);
1576 } else if (fix_problem(ctx,
1577 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
1578 reset = 1;
1579 sb->s_state &= ~EXT2_VALID_FS;
1580 ext2fs_mark_super_dirty(ctx->fs);
1581 }
1582 /*
1583 * If the user answers no to the above question, we
1584 * ignore the fact that journal apparently has data;
1585 * accidentally replaying over valid data would be far
1586 * worse than skipping a questionable recovery.
1587 *
1588 * XXX should we abort with a fatal error here? What
1589 * will the ext3 kernel code do if a filesystem with
1590 * !NEEDS_RECOVERY but with a non-zero
1591 * journal->j_superblock->s_start is mounted?
1592 */
1593 }
1594
1595 /*
1596 * If we don't need to do replay the journal, check to see if
1597 * the journal's errno is set; if so, we need to mark the file
1598 * system as being corrupt and clear the journal's s_errno.
1599 */
1600 if (!ext2fs_has_feature_journal_needs_recovery(sb) &&
1601 journal->j_superblock->s_errno) {
1602 ctx->fs->super->s_state |= EXT2_ERROR_FS;
1603 ext2fs_mark_super_dirty(ctx->fs);
1604 journal->j_superblock->s_errno = 0;
1605 e2fsck_journal_sb_csum_set(journal, journal->j_superblock);
1606 mark_buffer_dirty(journal->j_sb_buffer);
1607 }
1608
1609 e2fsck_journal_release(ctx, journal, reset, 0);
1610 return retval;
1611 }
1612
recover_ext3_journal(e2fsck_t ctx)1613 static errcode_t recover_ext3_journal(e2fsck_t ctx)
1614 {
1615 struct problem_context pctx;
1616 journal_t *journal;
1617 errcode_t retval;
1618
1619 clear_problem_context(&pctx);
1620
1621 retval = jbd2_journal_init_revoke_record_cache();
1622 if (retval)
1623 return retval;
1624
1625 retval = jbd2_journal_init_revoke_table_cache();
1626 if (retval)
1627 return retval;
1628
1629 retval = e2fsck_get_journal(ctx, &journal);
1630 if (retval)
1631 return retval;
1632
1633 retval = e2fsck_journal_load(journal);
1634 if (retval)
1635 goto errout;
1636
1637 retval = jbd2_journal_init_revoke(journal, 1024);
1638 if (retval)
1639 goto errout;
1640
1641 retval = -jbd2_journal_recover(journal);
1642 if (retval)
1643 goto errout;
1644
1645 if (journal->j_failed_commit) {
1646 pctx.ino = journal->j_failed_commit;
1647 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
1648 journal->j_superblock->s_errno = -EINVAL;
1649 mark_buffer_dirty(journal->j_sb_buffer);
1650 }
1651
1652 journal->j_tail_sequence = journal->j_transaction_sequence;
1653
1654 errout:
1655 jbd2_journal_destroy_revoke(journal);
1656 jbd2_journal_destroy_revoke_record_cache();
1657 jbd2_journal_destroy_revoke_table_cache();
1658 e2fsck_journal_release(ctx, journal, 1, 0);
1659 return retval;
1660 }
1661
e2fsck_run_ext3_journal(e2fsck_t ctx)1662 errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
1663 {
1664 io_manager io_ptr = ctx->fs->io->manager;
1665 int blocksize = ctx->fs->blocksize;
1666 errcode_t retval, recover_retval;
1667 io_stats stats = 0;
1668 unsigned long long kbytes_written = 0;
1669
1670 printf(_("%s: recovering journal\n"), ctx->device_name);
1671 if (ctx->options & E2F_OPT_READONLY) {
1672 printf(_("%s: won't do journal recovery while read-only\n"),
1673 ctx->device_name);
1674 return EXT2_ET_FILE_RO;
1675 }
1676
1677 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
1678 ext2fs_flush(ctx->fs); /* Force out any modifications */
1679
1680 recover_retval = recover_ext3_journal(ctx);
1681
1682 /*
1683 * Reload the filesystem context to get up-to-date data from disk
1684 * because journal recovery will change the filesystem under us.
1685 */
1686 if (ctx->fs->super->s_kbytes_written &&
1687 ctx->fs->io->manager->get_stats)
1688 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
1689 if (stats && stats->bytes_written)
1690 kbytes_written = stats->bytes_written >> 10;
1691
1692 ext2fs_mmp_stop(ctx->fs);
1693 ext2fs_free(ctx->fs);
1694 retval = ext2fs_open(ctx->filesystem_name, ctx->openfs_flags,
1695 ctx->superblock, blocksize, io_ptr,
1696 &ctx->fs);
1697 if (retval) {
1698 com_err(ctx->program_name, retval,
1699 _("while trying to re-open %s"),
1700 ctx->device_name);
1701 fatal_error(ctx, 0);
1702 }
1703 ctx->fs->priv_data = ctx;
1704 ctx->fs->now = ctx->now;
1705 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1706 ctx->fs->super->s_kbytes_written += kbytes_written;
1707
1708 /* Set the superblock flags */
1709 e2fsck_clear_recover(ctx, recover_retval != 0);
1710
1711 /*
1712 * Do one last sanity check, and propagate journal->s_errno to
1713 * the EXT2_ERROR_FS flag in the fs superblock if needed.
1714 */
1715 retval = e2fsck_check_ext3_journal(ctx);
1716 return retval ? retval : recover_retval;
1717 }
1718
1719 /*
1720 * This function will move the journal inode from a visible file in
1721 * the filesystem directory hierarchy to the reserved inode if necessary.
1722 */
1723 static const char * const journal_names[] = {
1724 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
1725
e2fsck_move_ext3_journal(e2fsck_t ctx)1726 void e2fsck_move_ext3_journal(e2fsck_t ctx)
1727 {
1728 struct ext2_super_block *sb = ctx->fs->super;
1729 struct problem_context pctx;
1730 struct ext2_inode inode;
1731 ext2_filsys fs = ctx->fs;
1732 ext2_ino_t ino;
1733 errcode_t retval;
1734 const char * const * cpp;
1735 dgrp_t group;
1736 int mount_flags;
1737
1738 clear_problem_context(&pctx);
1739
1740 /*
1741 * If the filesystem is opened read-only, or there is no
1742 * journal, then do nothing.
1743 */
1744 if ((ctx->options & E2F_OPT_READONLY) ||
1745 (sb->s_journal_inum == 0) ||
1746 !ext2fs_has_feature_journal(sb))
1747 return;
1748
1749 /*
1750 * Read in the journal inode
1751 */
1752 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
1753 return;
1754
1755 /*
1756 * If it's necessary to backup the journal inode, do so.
1757 */
1758 if ((sb->s_jnl_backup_type == 0) ||
1759 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
1760 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
1761 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
1762 memcpy(sb->s_jnl_blocks, inode.i_block,
1763 EXT2_N_BLOCKS*4);
1764 sb->s_jnl_blocks[15] = inode.i_size_high;
1765 sb->s_jnl_blocks[16] = inode.i_size;
1766 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1767 ext2fs_mark_super_dirty(fs);
1768 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1769 }
1770 }
1771
1772 /*
1773 * If the journal is already the hidden inode, then do nothing
1774 */
1775 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
1776 return;
1777
1778 /*
1779 * The journal inode had better have only one link and not be readable.
1780 */
1781 if (inode.i_links_count != 1)
1782 return;
1783
1784 /*
1785 * If the filesystem is mounted, or we can't tell whether
1786 * or not it's mounted, do nothing.
1787 */
1788 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
1789 if (retval || (mount_flags & EXT2_MF_MOUNTED))
1790 return;
1791
1792 /*
1793 * If we can't find the name of the journal inode, then do
1794 * nothing.
1795 */
1796 for (cpp = journal_names; *cpp; cpp++) {
1797 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
1798 strlen(*cpp), 0, &ino);
1799 if ((retval == 0) && (ino == sb->s_journal_inum))
1800 break;
1801 }
1802 if (*cpp == 0)
1803 return;
1804
1805 /* We need the inode bitmap to be loaded */
1806 retval = ext2fs_read_bitmaps(fs);
1807 if (retval)
1808 return;
1809
1810 pctx.str = *cpp;
1811 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
1812 return;
1813
1814 /*
1815 * OK, we've done all the checks, let's actually move the
1816 * journal inode. Errors at this point mean we need to force
1817 * an ext2 filesystem check.
1818 */
1819 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1820 goto err_out;
1821 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1822 goto err_out;
1823 sb->s_journal_inum = EXT2_JOURNAL_INO;
1824 ext2fs_mark_super_dirty(fs);
1825 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1826 inode.i_links_count = 0;
1827 inode.i_dtime = ctx->now;
1828 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1829 goto err_out;
1830
1831 group = ext2fs_group_of_ino(fs, ino);
1832 ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
1833 ext2fs_mark_ib_dirty(fs);
1834 ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
1835 ext2fs_group_desc_csum_set(fs, group);
1836 fs->super->s_free_inodes_count++;
1837 return;
1838
1839 err_out:
1840 pctx.errcode = retval;
1841 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1842 fs->super->s_state &= ~EXT2_VALID_FS;
1843 ext2fs_mark_super_dirty(fs);
1844 return;
1845 }
1846
1847 /*
1848 * This function makes sure the superblock hint for the external
1849 * journal is correct.
1850 */
e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)1851 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1852 {
1853 struct ext2_super_block *sb = ctx->fs->super;
1854 struct problem_context pctx;
1855 char uuid[37], *journal_name;
1856 struct stat st;
1857
1858 if (!ext2fs_has_feature_journal(sb) ||
1859 uuid_is_null(sb->s_journal_uuid))
1860 return 0;
1861
1862 uuid_unparse(sb->s_journal_uuid, uuid);
1863 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1864 if (!journal_name)
1865 return 0;
1866
1867 if (stat(journal_name, &st) < 0) {
1868 free(journal_name);
1869 return 0;
1870 }
1871
1872 if (st.st_rdev != sb->s_journal_dev) {
1873 clear_problem_context(&pctx);
1874 pctx.num = st.st_rdev;
1875 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1876 sb->s_journal_dev = st.st_rdev;
1877 ext2fs_mark_super_dirty(ctx->fs);
1878 }
1879 }
1880
1881 free(journal_name);
1882 return 0;
1883 }
1884