]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - e2fsck/journal.c
Fix more compiler warnings.
[thirdparty/e2fsprogs.git] / e2fsck / journal.c
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 #ifdef HAVE_SYS_MOUNT_H
16 #include <sys/mount.h>
17 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
18 #endif
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22
23 #define E2FSCK_INCLUDE_INLINE_FUNCS
24 #include "jfs_user.h"
25 #include "problem.h"
26 #include "uuid/uuid.h"
27
28 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
29 static int bh_count = 0;
30 #endif
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 /* Kernel compatibility functions for handling the journal. These allow us
42 * to use the recovery.c file virtually unchanged from the kernel, so we
43 * don't have to do much to keep kernel and user recovery in sync.
44 */
45 int journal_bmap(journal_t *journal, blk_t block, unsigned long *phys)
46 {
47 #ifdef USE_INODE_IO
48 *phys = block;
49 return 0;
50 #else
51 struct inode *inode = journal->j_inode;
52 errcode_t retval;
53 blk_t pblk;
54
55 if (!inode) {
56 *phys = block;
57 return 0;
58 }
59
60 retval= ext2fs_bmap(inode->i_ctx->fs, inode->i_ino,
61 &inode->i_ext2, NULL, 0, block, &pblk);
62 *phys = pblk;
63 return (retval);
64 #endif
65 }
66
67 struct buffer_head *getblk(kdev_t kdev, blk_t blocknr, int blocksize)
68 {
69 struct buffer_head *bh;
70
71 bh = e2fsck_allocate_memory(kdev->k_ctx, sizeof(*bh), "block buffer");
72 if (!bh)
73 return NULL;
74
75 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
76 (unsigned long) blocknr, blocksize, ++bh_count);
77
78 bh->b_ctx = kdev->k_ctx;
79 if (kdev->k_dev == K_DEV_FS)
80 bh->b_io = kdev->k_ctx->fs->io;
81 else
82 bh->b_io = kdev->k_ctx->journal_io;
83 bh->b_size = blocksize;
84 bh->b_blocknr = blocknr;
85
86 return bh;
87 }
88
89 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
90 {
91 int retval;
92 struct buffer_head *bh;
93
94 for (; nr > 0; --nr) {
95 bh = *bhp++;
96 if (rw == READ && !bh->b_uptodate) {
97 jfs_debug(3, "reading block %lu/%p\n",
98 (unsigned long) bh->b_blocknr, (void *) bh);
99 retval = io_channel_read_blk(bh->b_io,
100 bh->b_blocknr,
101 1, bh->b_data);
102 if (retval) {
103 com_err(bh->b_ctx->device_name, retval,
104 "while reading block %lu\n",
105 (unsigned long) bh->b_blocknr);
106 bh->b_err = retval;
107 continue;
108 }
109 bh->b_uptodate = 1;
110 } else if (rw == WRITE && bh->b_dirty) {
111 jfs_debug(3, "writing block %lu/%p\n",
112 (unsigned long) bh->b_blocknr, (void *) bh);
113 retval = io_channel_write_blk(bh->b_io,
114 bh->b_blocknr,
115 1, bh->b_data);
116 if (retval) {
117 com_err(bh->b_ctx->device_name, retval,
118 "while writing block %lu\n",
119 (unsigned long) bh->b_blocknr);
120 bh->b_err = retval;
121 continue;
122 }
123 bh->b_dirty = 0;
124 bh->b_uptodate = 1;
125 } else {
126 jfs_debug(3, "no-op %s for block %lu\n",
127 rw == READ ? "read" : "write",
128 (unsigned long) bh->b_blocknr);
129 }
130 }
131 }
132
133 void mark_buffer_dirty(struct buffer_head *bh)
134 {
135 bh->b_dirty = 1;
136 }
137
138 static void mark_buffer_clean(struct buffer_head * bh)
139 {
140 bh->b_dirty = 0;
141 }
142
143 void brelse(struct buffer_head *bh)
144 {
145 if (bh->b_dirty)
146 ll_rw_block(WRITE, 1, &bh);
147 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
148 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
149 ext2fs_free_mem(&bh);
150 }
151
152 int buffer_uptodate(struct buffer_head *bh)
153 {
154 return bh->b_uptodate;
155 }
156
157 void mark_buffer_uptodate(struct buffer_head *bh, int val)
158 {
159 bh->b_uptodate = val;
160 }
161
162 void wait_on_buffer(struct buffer_head *bh)
163 {
164 if (!bh->b_uptodate)
165 ll_rw_block(READ, 1, &bh);
166 }
167
168
169 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
170 {
171 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
172
173 /* if we had an error doing journal recovery, we need a full fsck */
174 if (error)
175 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
176 ext2fs_mark_super_dirty(ctx->fs);
177 }
178
179 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
180 {
181 struct ext2_super_block *sb = ctx->fs->super;
182 struct ext2_super_block jsuper;
183 struct problem_context pctx;
184 struct buffer_head *bh;
185 struct inode *j_inode = NULL;
186 struct kdev_s *dev_fs = NULL, *dev_journal;
187 const char *journal_name = 0;
188 journal_t *journal = NULL;
189 errcode_t retval = 0;
190 io_manager io_ptr = 0;
191 unsigned long start = 0;
192 blk_t blk;
193 int ext_journal = 0;
194 int tried_backup_jnl = 0;
195 int i;
196
197 clear_problem_context(&pctx);
198
199 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
200 if (!journal) {
201 return EXT2_ET_NO_MEMORY;
202 }
203
204 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
205 if (!dev_fs) {
206 retval = EXT2_ET_NO_MEMORY;
207 goto errout;
208 }
209 dev_journal = dev_fs+1;
210
211 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
212 dev_fs->k_dev = K_DEV_FS;
213 dev_journal->k_dev = K_DEV_JOURNAL;
214
215 journal->j_dev = dev_journal;
216 journal->j_fs_dev = dev_fs;
217 journal->j_inode = NULL;
218 journal->j_blocksize = ctx->fs->blocksize;
219
220 if (uuid_is_null(sb->s_journal_uuid)) {
221 if (!sb->s_journal_inum)
222 return EXT2_ET_BAD_INODE_NUM;
223 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
224 "journal inode");
225 if (!j_inode) {
226 retval = EXT2_ET_NO_MEMORY;
227 goto errout;
228 }
229
230 j_inode->i_ctx = ctx;
231 j_inode->i_ino = sb->s_journal_inum;
232
233 if ((retval = ext2fs_read_inode(ctx->fs,
234 sb->s_journal_inum,
235 &j_inode->i_ext2))) {
236 try_backup_journal:
237 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
238 tried_backup_jnl)
239 goto errout;
240 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
241 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
242 EXT2_N_BLOCKS*4);
243 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
244 j_inode->i_ext2.i_links_count = 1;
245 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
246 tried_backup_jnl++;
247 }
248 if (!j_inode->i_ext2.i_links_count ||
249 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
250 retval = EXT2_ET_NO_JOURNAL;
251 goto try_backup_journal;
252 }
253 if (j_inode->i_ext2.i_size / journal->j_blocksize <
254 JFS_MIN_JOURNAL_BLOCKS) {
255 retval = EXT2_ET_JOURNAL_TOO_SMALL;
256 goto try_backup_journal;
257 }
258 for (i=0; i < EXT2_N_BLOCKS; i++) {
259 blk = j_inode->i_ext2.i_block[i];
260 if (!blk) {
261 if (i < EXT2_NDIR_BLOCKS) {
262 retval = EXT2_ET_JOURNAL_TOO_SMALL;
263 goto try_backup_journal;
264 }
265 continue;
266 }
267 if (blk < sb->s_first_data_block ||
268 blk >= sb->s_blocks_count) {
269 retval = EXT2_ET_BAD_BLOCK_NUM;
270 goto try_backup_journal;
271 }
272 }
273 journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize;
274
275 #ifdef USE_INODE_IO
276 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
277 &j_inode->i_ext2,
278 &journal_name);
279 if (retval)
280 goto errout;
281
282 io_ptr = inode_io_manager;
283 #else
284 journal->j_inode = j_inode;
285 ctx->journal_io = ctx->fs->io;
286 if ((retval = journal_bmap(journal, 0, &start)) != 0)
287 goto errout;
288 #endif
289 } else {
290 ext_journal = 1;
291 if (!ctx->journal_name) {
292 char uuid[37];
293
294 uuid_unparse(sb->s_journal_uuid, uuid);
295 ctx->journal_name = blkid_get_devname(ctx->blkid,
296 "UUID", uuid);
297 if (!ctx->journal_name)
298 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
299 }
300 journal_name = ctx->journal_name;
301
302 if (!journal_name) {
303 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
304 return EXT2_ET_LOAD_EXT_JOURNAL;
305 }
306
307 jfs_debug(1, "Using journal file %s\n", journal_name);
308 io_ptr = unix_io_manager;
309 }
310
311 #if 0
312 test_io_backing_manager = io_ptr;
313 io_ptr = test_io_manager;
314 #endif
315 #ifndef USE_INODE_IO
316 if (ext_journal)
317 #endif
318 retval = io_ptr->open(journal_name, IO_FLAG_RW,
319 &ctx->journal_io);
320 if (retval)
321 goto errout;
322
323 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
324
325 if (ext_journal) {
326 if (ctx->fs->blocksize == 1024)
327 start = 1;
328 bh = getblk(dev_journal, start, ctx->fs->blocksize);
329 if (!bh) {
330 retval = EXT2_ET_NO_MEMORY;
331 goto errout;
332 }
333 ll_rw_block(READ, 1, &bh);
334 if ((retval = bh->b_err) != 0)
335 goto errout;
336 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
337 sizeof(jsuper));
338 brelse(bh);
339 #ifdef EXT2FS_ENABLE_SWAPFS
340 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
341 ext2fs_swap_super(&jsuper);
342 #endif
343 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
344 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
345 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
346 retval = EXT2_ET_LOAD_EXT_JOURNAL;
347 goto errout;
348 }
349 /* Make sure the journal UUID is correct */
350 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
351 sizeof(jsuper.s_uuid))) {
352 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
353 retval = EXT2_ET_LOAD_EXT_JOURNAL;
354 goto errout;
355 }
356
357 journal->j_maxlen = jsuper.s_blocks_count;
358 start++;
359 }
360
361 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
362 retval = EXT2_ET_NO_MEMORY;
363 goto errout;
364 }
365
366 journal->j_sb_buffer = bh;
367 journal->j_superblock = (journal_superblock_t *)bh->b_data;
368
369 #ifdef USE_INODE_IO
370 if (j_inode)
371 ext2fs_free_mem(&j_inode);
372 #endif
373
374 *ret_journal = journal;
375 return 0;
376
377 errout:
378 if (dev_fs)
379 ext2fs_free_mem(&dev_fs);
380 if (j_inode)
381 ext2fs_free_mem(&j_inode);
382 if (journal)
383 ext2fs_free_mem(&journal);
384 return retval;
385
386 }
387
388 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
389 struct problem_context *pctx)
390 {
391 struct ext2_super_block *sb = ctx->fs->super;
392 int recover = ctx->fs->super->s_feature_incompat &
393 EXT3_FEATURE_INCOMPAT_RECOVER;
394 int has_journal = ctx->fs->super->s_feature_compat &
395 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
396
397 if (has_journal || sb->s_journal_inum) {
398 /* The journal inode is bogus, remove and force full fsck */
399 pctx->ino = sb->s_journal_inum;
400 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
401 if (has_journal && sb->s_journal_inum)
402 printf("*** ext3 journal has been deleted - "
403 "filesystem is now ext2 only ***\n\n");
404 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
405 sb->s_journal_inum = 0;
406 ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
407 e2fsck_clear_recover(ctx, 1);
408 return 0;
409 }
410 return EXT2_ET_BAD_INODE_NUM;
411 } else if (recover) {
412 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
413 e2fsck_clear_recover(ctx, 1);
414 return 0;
415 }
416 return EXT2_ET_UNSUPP_FEATURE;
417 }
418 return 0;
419 }
420
421 #define V1_SB_SIZE 0x0024
422 static void clear_v2_journal_fields(journal_t *journal)
423 {
424 e2fsck_t ctx = journal->j_dev->k_ctx;
425 struct problem_context pctx;
426
427 clear_problem_context(&pctx);
428
429 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
430 return;
431
432 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
433 ctx->fs->blocksize-V1_SB_SIZE);
434 mark_buffer_dirty(journal->j_sb_buffer);
435 }
436
437
438 static errcode_t e2fsck_journal_load(journal_t *journal)
439 {
440 e2fsck_t ctx = journal->j_dev->k_ctx;
441 journal_superblock_t *jsb;
442 struct buffer_head *jbh = journal->j_sb_buffer;
443 struct problem_context pctx;
444
445 clear_problem_context(&pctx);
446
447 ll_rw_block(READ, 1, &jbh);
448 if (jbh->b_err) {
449 com_err(ctx->device_name, jbh->b_err,
450 _("reading journal superblock\n"));
451 return jbh->b_err;
452 }
453
454 jsb = journal->j_superblock;
455 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
456 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
457 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
458
459 switch (ntohl(jsb->s_header.h_blocktype)) {
460 case JFS_SUPERBLOCK_V1:
461 journal->j_format_version = 1;
462 if (jsb->s_feature_compat ||
463 jsb->s_feature_incompat ||
464 jsb->s_feature_ro_compat ||
465 jsb->s_nr_users)
466 clear_v2_journal_fields(journal);
467 break;
468
469 case JFS_SUPERBLOCK_V2:
470 journal->j_format_version = 2;
471 if (ntohl(jsb->s_nr_users) > 1 &&
472 uuid_is_null(ctx->fs->super->s_journal_uuid))
473 clear_v2_journal_fields(journal);
474 if (ntohl(jsb->s_nr_users) > 1) {
475 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
476 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
477 }
478 break;
479
480 /*
481 * These should never appear in a journal super block, so if
482 * they do, the journal is badly corrupted.
483 */
484 case JFS_DESCRIPTOR_BLOCK:
485 case JFS_COMMIT_BLOCK:
486 case JFS_REVOKE_BLOCK:
487 return EXT2_ET_CORRUPT_SUPERBLOCK;
488
489 /* If we don't understand the superblock major type, but there
490 * is a magic number, then it is likely to be a new format we
491 * just don't understand, so leave it alone. */
492 default:
493 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
494 }
495
496 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
497 return EXT2_ET_UNSUPP_FEATURE;
498
499 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
500 return EXT2_ET_RO_UNSUPP_FEATURE;
501
502 /* We have now checked whether we know enough about the journal
503 * format to be able to proceed safely, so any other checks that
504 * fail we should attempt to recover from. */
505 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
506 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
507 _("%s: no valid journal superblock found\n"),
508 ctx->device_name);
509 return EXT2_ET_CORRUPT_SUPERBLOCK;
510 }
511
512 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
513 journal->j_maxlen = ntohl(jsb->s_maxlen);
514 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
515 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
516 _("%s: journal too short\n"),
517 ctx->device_name);
518 return EXT2_ET_CORRUPT_SUPERBLOCK;
519 }
520
521 journal->j_tail_sequence = ntohl(jsb->s_sequence);
522 journal->j_transaction_sequence = journal->j_tail_sequence;
523 journal->j_tail = ntohl(jsb->s_start);
524 journal->j_first = ntohl(jsb->s_first);
525 journal->j_last = ntohl(jsb->s_maxlen);
526
527 return 0;
528 }
529
530 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
531 journal_t *journal)
532 {
533 char *p;
534 union {
535 uuid_t uuid;
536 __u32 val[4];
537 } u;
538 __u32 new_seq = 0;
539 int i;
540
541 /* Leave a valid existing V1 superblock signature alone.
542 * Anything unrecognisable we overwrite with a new V2
543 * signature. */
544
545 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
546 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
547 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
548 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
549 }
550
551 /* Zero out everything else beyond the superblock header */
552
553 p = ((char *) jsb) + sizeof(journal_header_t);
554 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
555
556 jsb->s_blocksize = htonl(ctx->fs->blocksize);
557 jsb->s_maxlen = htonl(journal->j_maxlen);
558 jsb->s_first = htonl(1);
559
560 /* Initialize the journal sequence number so that there is "no"
561 * chance we will find old "valid" transactions in the journal.
562 * This avoids the need to zero the whole journal (slow to do,
563 * and risky when we are just recovering the filesystem).
564 */
565 uuid_generate(u.uuid);
566 for (i = 0; i < 4; i ++)
567 new_seq ^= u.val[i];
568 jsb->s_sequence = htonl(new_seq);
569
570 mark_buffer_dirty(journal->j_sb_buffer);
571 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
572 }
573
574 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
575 journal_t *journal,
576 struct problem_context *pctx)
577 {
578 struct ext2_super_block *sb = ctx->fs->super;
579 int recover = ctx->fs->super->s_feature_incompat &
580 EXT3_FEATURE_INCOMPAT_RECOVER;
581
582 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
583 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
584 e2fsck_journal_reset_super(ctx, journal->j_superblock,
585 journal);
586 journal->j_transaction_sequence = 1;
587 e2fsck_clear_recover(ctx, recover);
588 return 0;
589 }
590 return EXT2_ET_CORRUPT_SUPERBLOCK;
591 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
592 return EXT2_ET_CORRUPT_SUPERBLOCK;
593
594 return 0;
595 }
596
597 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
598 int reset, int drop)
599 {
600 journal_superblock_t *jsb;
601
602 if (drop)
603 mark_buffer_clean(journal->j_sb_buffer);
604 else if (!(ctx->options & E2F_OPT_READONLY)) {
605 jsb = journal->j_superblock;
606 jsb->s_sequence = htonl(journal->j_transaction_sequence);
607 if (reset)
608 jsb->s_start = 0; /* this marks the journal as empty */
609 mark_buffer_dirty(journal->j_sb_buffer);
610 }
611 brelse(journal->j_sb_buffer);
612
613 if (ctx->journal_io) {
614 if (ctx->fs && ctx->fs->io != ctx->journal_io)
615 io_channel_close(ctx->journal_io);
616 ctx->journal_io = 0;
617 }
618
619 #ifndef USE_INODE_IO
620 if (journal->j_inode)
621 ext2fs_free_mem(&journal->j_inode);
622 #endif
623 if (journal->j_fs_dev)
624 ext2fs_free_mem(&journal->j_fs_dev);
625 ext2fs_free_mem(&journal);
626 }
627
628 /*
629 * This function makes sure that the superblock fields regarding the
630 * journal are consistent.
631 */
632 int e2fsck_check_ext3_journal(e2fsck_t ctx)
633 {
634 struct ext2_super_block *sb = ctx->fs->super;
635 journal_t *journal;
636 int recover = ctx->fs->super->s_feature_incompat &
637 EXT3_FEATURE_INCOMPAT_RECOVER;
638 struct problem_context pctx;
639 problem_t problem;
640 int reset = 0, force_fsck = 0;
641 int retval;
642
643 /* If we don't have any journal features, don't do anything more */
644 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
645 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
646 uuid_is_null(sb->s_journal_uuid))
647 return 0;
648
649 clear_problem_context(&pctx);
650 pctx.num = sb->s_journal_inum;
651
652 retval = e2fsck_get_journal(ctx, &journal);
653 if (retval) {
654 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
655 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
656 (retval == EXT2_ET_NO_JOURNAL))
657 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
658 return retval;
659 }
660
661 retval = e2fsck_journal_load(journal);
662 if (retval) {
663 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
664 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
665 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
666 &pctx))) ||
667 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
668 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
669 &pctx))) ||
670 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
671 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
672 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
673 &pctx);
674 e2fsck_journal_release(ctx, journal, 0, 1);
675 return retval;
676 }
677
678 /*
679 * We want to make the flags consistent here. We will not leave with
680 * needs_recovery set but has_journal clear. We can't get in a loop
681 * with -y, -n, or -p, only if a user isn't making up their mind.
682 */
683 no_has_journal:
684 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
685 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
686 pctx.str = "inode";
687 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
688 if (recover &&
689 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
690 goto no_has_journal;
691 /*
692 * Need a full fsck if we are releasing a
693 * journal stored on a reserved inode.
694 */
695 force_fsck = recover ||
696 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
697 /* Clear all of the journal fields */
698 sb->s_journal_inum = 0;
699 sb->s_journal_dev = 0;
700 memset(sb->s_journal_uuid, 0,
701 sizeof(sb->s_journal_uuid));
702 e2fsck_clear_recover(ctx, force_fsck);
703 } else if (!(ctx->options & E2F_OPT_READONLY)) {
704 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
705 ext2fs_mark_super_dirty(ctx->fs);
706 }
707 }
708
709 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
710 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
711 journal->j_superblock->s_start != 0) {
712 /* Print status information */
713 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
714 if (ctx->superblock)
715 problem = PR_0_JOURNAL_RUN_DEFAULT;
716 else
717 problem = PR_0_JOURNAL_RUN;
718 if (fix_problem(ctx, problem, &pctx)) {
719 ctx->options |= E2F_OPT_FORCE;
720 sb->s_feature_incompat |=
721 EXT3_FEATURE_INCOMPAT_RECOVER;
722 ext2fs_mark_super_dirty(ctx->fs);
723 } else if (fix_problem(ctx,
724 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
725 reset = 1;
726 sb->s_state &= ~EXT2_VALID_FS;
727 ext2fs_mark_super_dirty(ctx->fs);
728 }
729 /*
730 * If the user answers no to the above question, we
731 * ignore the fact that journal apparently has data;
732 * accidentally replaying over valid data would be far
733 * worse than skipping a questionable recovery.
734 *
735 * XXX should we abort with a fatal error here? What
736 * will the ext3 kernel code do if a filesystem with
737 * !NEEDS_RECOVERY but with a non-zero
738 * journal->j_superblock->s_start is mounted?
739 */
740 }
741
742 e2fsck_journal_release(ctx, journal, reset, 0);
743 return retval;
744 }
745
746 static errcode_t recover_ext3_journal(e2fsck_t ctx)
747 {
748 journal_t *journal;
749 int retval;
750
751 journal_init_revoke_caches();
752 retval = e2fsck_get_journal(ctx, &journal);
753 if (retval)
754 return retval;
755
756 retval = e2fsck_journal_load(journal);
757 if (retval)
758 goto errout;
759
760 retval = journal_init_revoke(journal, 1024);
761 if (retval)
762 goto errout;
763
764 retval = -journal_recover(journal);
765 if (retval)
766 goto errout;
767
768 if (journal->j_superblock->s_errno) {
769 ctx->fs->super->s_state |= EXT2_ERROR_FS;
770 ext2fs_mark_super_dirty(ctx->fs);
771 journal->j_superblock->s_errno = 0;
772 mark_buffer_dirty(journal->j_sb_buffer);
773 }
774
775 errout:
776 journal_destroy_revoke(journal);
777 journal_destroy_revoke_caches();
778 e2fsck_journal_release(ctx, journal, 1, 0);
779 return retval;
780 }
781
782 int e2fsck_run_ext3_journal(e2fsck_t ctx)
783 {
784 io_manager io_ptr = ctx->fs->io->manager;
785 int blocksize = ctx->fs->blocksize;
786 errcode_t retval, recover_retval;
787
788 printf(_("%s: recovering journal\n"), ctx->device_name);
789 if (ctx->options & E2F_OPT_READONLY) {
790 printf(_("%s: won't do journal recovery while read-only\n"),
791 ctx->device_name);
792 return EXT2_ET_FILE_RO;
793 }
794
795 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
796 ext2fs_flush(ctx->fs); /* Force out any modifications */
797
798 recover_retval = recover_ext3_journal(ctx);
799
800 /*
801 * Reload the filesystem context to get up-to-date data from disk
802 * because journal recovery will change the filesystem under us.
803 */
804 ext2fs_close(ctx->fs);
805 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
806 ctx->superblock, blocksize, io_ptr,
807 &ctx->fs);
808
809 if (retval) {
810 com_err(ctx->program_name, retval,
811 _("while trying to re-open %s"),
812 ctx->device_name);
813 fatal_error(ctx, 0);
814 }
815 ctx->fs->priv_data = ctx;
816
817 /* Set the superblock flags */
818 e2fsck_clear_recover(ctx, recover_retval);
819 return recover_retval;
820 }
821
822 /*
823 * This function will move the journal inode from a visible file in
824 * the filesystem directory hierarchy to the reserved inode if necessary.
825 */
826 static const char * const journal_names[] = {
827 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
828
829 void e2fsck_move_ext3_journal(e2fsck_t ctx)
830 {
831 struct ext2_super_block *sb = ctx->fs->super;
832 struct problem_context pctx;
833 struct ext2_inode inode;
834 ext2_filsys fs = ctx->fs;
835 ext2_ino_t ino;
836 errcode_t retval;
837 const char * const * cpp;
838 int group, mount_flags;
839
840 clear_problem_context(&pctx);
841
842 /*
843 * If the filesystem is opened read-only, or there is no
844 * journal, then do nothing.
845 */
846 if ((ctx->options & E2F_OPT_READONLY) ||
847 (sb->s_journal_inum == 0) ||
848 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
849 return;
850
851 /*
852 * Read in the journal inode
853 */
854 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
855 return;
856
857 /*
858 * If it's necessary to backup the journal inode, do so.
859 */
860 if ((sb->s_jnl_backup_type == 0) ||
861 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
862 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
863 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
864 memcpy(sb->s_jnl_blocks, inode.i_block,
865 EXT2_N_BLOCKS*4);
866 sb->s_jnl_blocks[16] = inode.i_size;
867 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
868 ext2fs_mark_super_dirty(fs);
869 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
870 }
871 }
872
873 /*
874 * If the journal is already the hidden inode, then do nothing
875 */
876 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
877 return;
878
879 /*
880 * The journal inode had better have only one link and not be readable.
881 */
882 if (inode.i_links_count != 1)
883 return;
884
885 /*
886 * If the filesystem is mounted, or we can't tell whether
887 * or not it's mounted, do nothing.
888 */
889 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
890 if (retval || (mount_flags & EXT2_MF_MOUNTED))
891 return;
892
893 /*
894 * If we can't find the name of the journal inode, then do
895 * nothing.
896 */
897 for (cpp = journal_names; *cpp; cpp++) {
898 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
899 strlen(*cpp), 0, &ino);
900 if ((retval == 0) && (ino == sb->s_journal_inum))
901 break;
902 }
903 if (*cpp == 0)
904 return;
905
906 /* We need the inode bitmap to be loaded */
907 retval = ext2fs_read_bitmaps(fs);
908 if (retval)
909 return;
910
911 pctx.str = *cpp;
912 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
913 return;
914
915 /*
916 * OK, we've done all the checks, let's actually move the
917 * journal inode. Errors at this point mean we need to force
918 * an ext2 filesystem check.
919 */
920 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
921 goto err_out;
922 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
923 goto err_out;
924 sb->s_journal_inum = EXT2_JOURNAL_INO;
925 ext2fs_mark_super_dirty(fs);
926 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
927 inode.i_links_count = 0;
928 inode.i_dtime = time(0);
929 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
930 goto err_out;
931
932 group = ext2fs_group_of_ino(fs, ino);
933 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
934 ext2fs_mark_ib_dirty(fs);
935 fs->group_desc[group].bg_free_inodes_count++;
936 fs->super->s_free_inodes_count++;
937 return;
938
939 err_out:
940 pctx.errcode = retval;
941 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
942 fs->super->s_state &= ~EXT2_VALID_FS;
943 ext2fs_mark_super_dirty(fs);
944 return;
945 }
946