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