Whamcloud - gitweb
Add support for use of an external journal (so long as the external
[tools/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 JFS_DEBUG                /* Enabled by configure --enable-jfs-debug */
29 static int bh_count = 0;
30 int journal_enable_debug = 2;
31 #endif
32
33 /* Kernel compatibility functions for handling the journal.  These allow us
34  * to use the recovery.c file virtually unchanged from the kernel, so we
35  * don't have to do much to keep kernel and user recovery in sync.
36  */
37 int bmap(struct inode *inode, int block)
38 {
39         int retval;
40         blk_t phys;
41
42         retval = ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, &inode->i_ext2,
43                              NULL, 0, block, &phys);
44
45         if (retval)
46                 com_err(inode->i_ctx->device_name, retval,
47                         _("bmap journal inode %ld, block %d\n"),
48                         inode->i_ino, block);
49
50         return phys;
51 }
52
53 struct buffer_head *getblk(e2fsck_t ctx, blk_t blocknr, int blocksize)
54 {
55         struct buffer_head *bh;
56
57         bh = e2fsck_allocate_memory(ctx, sizeof(*bh), "block buffer");
58         if (!bh)
59                 return NULL;
60
61         jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
62                   (unsigned long) blocknr, blocksize, ++bh_count);
63
64         bh->b_ctx = ctx;
65         bh->b_size = blocksize;
66         bh->b_blocknr = blocknr;
67
68         return bh;
69 }
70
71 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
72 {
73         int retval;
74         struct buffer_head *bh;
75
76         for (; nr > 0; --nr) {
77                 bh = *bhp++;
78                 if (rw == READ && !bh->b_uptodate) {
79                         jfs_debug(3, "reading block %lu/%p\n", 
80                                   (unsigned long) bh->b_blocknr, (void *) bh);
81                         retval = io_channel_read_blk(bh->b_ctx->journal_io, 
82                                                      bh->b_blocknr,
83                                                      1, bh->b_data);
84                         if (retval) {
85                                 com_err(bh->b_ctx->device_name, retval,
86                                         "while reading block %ld\n", 
87                                         bh->b_blocknr);
88                                 bh->b_err = retval;
89                                 continue;
90                         }
91                         bh->b_uptodate = 1;
92                 } else if (rw == WRITE && bh->b_dirty) {
93                         jfs_debug(3, "writing block %lu/%p\n", 
94                                   (unsigned long) bh->b_blocknr, (void *) bh);
95                         retval = io_channel_write_blk(bh->b_ctx->journal_io, 
96                                                       bh->b_blocknr,
97                                                       1, bh->b_data);
98                         if (retval) {
99                                 com_err(bh->b_ctx->device_name, retval,
100                                         "while writing block %ld\n", 
101                                         bh->b_blocknr);
102                                 bh->b_err = retval;
103                                 continue;
104                         }
105                         bh->b_dirty = 0;
106                         bh->b_uptodate = 1;
107                 } else
108                         jfs_debug(3, "no-op %s for block %lu\n",
109                                   rw == READ ? "read" : "write", 
110                                   (unsigned long) bh->b_blocknr);
111         }
112 }
113
114 void mark_buffer_dirty(struct buffer_head *bh, int dummy)
115 {
116         bh->b_dirty = dummy | 1; /* use dummy to avoid unused variable */
117 }
118
119 static void mark_buffer_clean(struct buffer_head * bh)
120 {
121         bh->b_dirty = 0;
122 }
123
124 void brelse(struct buffer_head *bh)
125 {
126         if (bh->b_dirty)
127                 ll_rw_block(WRITE, 1, &bh);
128         jfs_debug(3, "freeing block %lu/%p (total %d)\n",
129                   (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
130         ext2fs_free_mem((void **) &bh);
131 }
132
133 int buffer_uptodate(struct buffer_head *bh)
134 {
135         return bh->b_uptodate;
136 }
137
138 void mark_buffer_uptodate(struct buffer_head *bh, int val)
139 {
140         bh->b_uptodate = val;
141 }
142
143 void wait_on_buffer(struct buffer_head *bh)
144 {
145         if (!bh->b_uptodate)
146                 ll_rw_block(READ, 1, &bh);
147 }
148
149
150 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
151 {
152         ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
153
154         /* if we had an error doing journal recovery, we need a full fsck */
155         if (error)
156                 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
157         ext2fs_mark_super_dirty(ctx->fs);
158 }
159
160 static errcode_t e2fsck_journal_init_inode(e2fsck_t ctx,
161                                            struct ext2_super_block *s,
162                                            journal_t **journal)
163 {
164         struct inode *inode;
165         struct buffer_head *bh;
166         blk_t start;
167         int retval;
168
169         jfs_debug(1, "Using journal inode %u\n", s->s_journal_inum);
170         *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
171         if (!*journal) {
172                 return EXT2_ET_NO_MEMORY;
173         }
174
175         inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode");
176         if (!inode) {
177                 retval = EXT2_ET_NO_MEMORY;
178                 goto exit_journal;
179         }
180
181         inode->i_ctx = ctx;
182         inode->i_ino = s->s_journal_inum;
183         retval = ext2fs_read_inode(ctx->fs, s->s_journal_inum, &inode->i_ext2);
184         if (retval)
185                 goto exit_inode;
186
187         (*journal)->j_dev = ctx;
188         (*journal)->j_inode = inode;
189         (*journal)->j_blocksize = ctx->fs->blocksize;
190         (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize;
191         ctx->journal_io = ctx->fs->io;
192
193         if (!inode->i_ext2.i_links_count ||
194             !LINUX_S_ISREG(inode->i_ext2.i_mode) ||
195             (*journal)->j_maxlen < JFS_MIN_JOURNAL_BLOCKS ||
196             (start = bmap(inode, 0)) == 0) {
197                 retval = EXT2_ET_BAD_INODE_NUM;
198                 goto exit_inode;
199         }
200
201         bh = getblk(ctx, start, (*journal)->j_blocksize);
202         if (!bh) {
203                 retval = EXT2_ET_NO_MEMORY;
204                 goto exit_inode;
205         }
206         (*journal)->j_sb_buffer = bh;
207         (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
208         
209         return 0;
210
211 exit_inode:
212         ext2fs_free_mem((void **)&inode);
213 exit_journal:
214         ext2fs_free_mem((void **)journal);
215
216         return retval;
217 }
218
219 static errcode_t e2fsck_journal_init_dev(e2fsck_t ctx,
220                                          struct ext2_super_block *s,
221                                          journal_t **journal)
222 {
223         struct buffer_head *bh;
224         io_manager      io_ptr;
225         blk_t           start;
226         int             retval;
227         int             blocksize = ctx->fs->blocksize;
228         struct ext2_super_block jsuper;
229         struct problem_context pctx;
230
231         clear_problem_context(&pctx);
232         if (!ctx->journal_name)
233                 ctx->journal_name = ext2fs_find_block_device(s->s_journal_dev);
234
235         if (!ctx->journal_name) {
236                 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
237                 return EXT2_ET_LOAD_EXT_JOURNAL;
238         }
239
240         jfs_debug(1, "Using journal file %s\n", ctx->journal_name);
241
242 #if 1
243         io_ptr = unix_io_manager;
244 #else
245         io_ptr = test_io_manager;
246         test_io_backing_manager = unix_io_manager;
247 #endif
248         if ((retval = io_ptr->open(ctx->journal_name, IO_FLAG_RW,
249                                             &ctx->journal_io)))
250                 return retval;
251
252         io_channel_set_blksize(ctx->journal_io, blocksize);
253         start = (blocksize == 1024) ? 1 : 0;
254         bh = getblk(ctx, start, blocksize);
255         if (!bh)
256                 return EXT2_ET_NO_MEMORY;
257         ll_rw_block(READ, 1, &bh);
258         if (bh->b_err)
259                 return bh->b_err;
260         memcpy(&jsuper, start ? bh->b_data :  bh->b_data + 1024,
261                sizeof(jsuper));
262         brelse(bh);
263 #ifdef EXT2FS_ENABLE_SWAPFS
264         if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) 
265                 ext2fs_swap_super(&jsuper);
266 #endif
267         if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
268             !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
269                 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
270                 return EXT2_ET_LOAD_EXT_JOURNAL;
271         }
272         /* Make sure the journal UUID is correct */
273         if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
274                    sizeof(jsuper.s_uuid))) {
275                 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
276                 return EXT2_ET_LOAD_EXT_JOURNAL;
277         }
278                 
279         *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
280         if (!*journal) {
281                 return EXT2_ET_NO_MEMORY;
282         }
283
284         (*journal)->j_dev = ctx;
285         (*journal)->j_inode = NULL;
286         (*journal)->j_blocksize = ctx->fs->blocksize;
287         (*journal)->j_maxlen = jsuper.s_blocks_count;
288
289         bh = getblk(ctx, start+1, (*journal)->j_blocksize);
290         if (!bh) {
291                 retval = EXT2_ET_NO_MEMORY;
292                 goto errout;
293         }
294         (*journal)->j_sb_buffer = bh;
295         (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
296         
297         return 0;
298
299 errout:
300         ext2fs_free_mem((void **)journal);
301         return retval;
302 }
303
304 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
305 {
306         struct ext2_super_block *sb = ctx->fs->super;
307
308         if (uuid_is_null(sb->s_journal_uuid)) {
309                 if (!sb->s_journal_inum)
310                         return EXT2_ET_BAD_INODE_NUM;
311                 return e2fsck_journal_init_inode(ctx, sb, journal);
312         } else {
313                 return e2fsck_journal_init_dev(ctx, sb, journal);
314         }
315 }
316
317 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
318                                               struct problem_context *pctx)
319 {
320         struct ext2_super_block *sb = ctx->fs->super;
321         int recover = ctx->fs->super->s_feature_incompat &
322                 EXT3_FEATURE_INCOMPAT_RECOVER;
323         int has_journal = ctx->fs->super->s_feature_compat &
324                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
325
326         if (has_journal || sb->s_journal_inum) {
327                 /* The journal inode is bogus, remove and force full fsck */
328                 pctx->ino = sb->s_journal_inum;
329                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
330                         if (has_journal && sb->s_journal_inum)
331                                 printf("*** ext3 journal has been deleted - "
332                                        "filesystem is now ext2 only ***\n\n");
333                         sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
334                         sb->s_journal_inum = 0;
335                         ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
336                         e2fsck_clear_recover(ctx, 1);
337                         return 0;
338                 }
339                 return EXT2_ET_BAD_INODE_NUM;
340         } else if (recover) {
341                 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
342                         e2fsck_clear_recover(ctx, 1);
343                         return 0;
344                 }
345                 return EXT2_ET_UNSUPP_FEATURE;
346         }
347         return 0;
348 }
349
350 static errcode_t e2fsck_journal_load(journal_t *journal)
351 {
352         e2fsck_t ctx = journal->j_dev;
353         journal_superblock_t *jsb;
354         struct buffer_head *jbh = journal->j_sb_buffer;
355         struct problem_context pctx;
356
357         clear_problem_context(&pctx);
358
359         ll_rw_block(READ, 1, &jbh);
360         if (jbh->b_err) {
361                 com_err(ctx->device_name, jbh->b_err,
362                         _("reading journal superblock\n"));
363                 return jbh->b_err;
364         }
365
366         jsb = journal->j_superblock;
367         /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
368         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
369                 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
370
371         switch (ntohl(jsb->s_header.h_blocktype)) {
372         case JFS_SUPERBLOCK_V1:
373                 journal->j_format_version = 1;
374                 break;
375                 
376         case JFS_SUPERBLOCK_V2:
377                 journal->j_format_version = 2;
378                 if (ntohl(jsb->s_nr_users) > 1) {
379                         fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
380                         return EXT2_ET_JOURNAL_UNSUPP_VERSION;
381                 }
382                 break;
383
384         /*
385          * These should never appear in a journal super block, so if
386          * they do, the journal is badly corrupted.
387          */
388         case JFS_DESCRIPTOR_BLOCK:
389         case JFS_COMMIT_BLOCK:
390         case JFS_REVOKE_BLOCK:
391                 return EXT2_ET_CORRUPT_SUPERBLOCK;
392                 
393         /* If we don't understand the superblock major type, but there
394          * is a magic number, then it is likely to be a new format we
395          * just don't understand, so leave it alone. */
396         default:
397                 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
398         }
399
400         if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
401                 return EXT2_ET_UNSUPP_FEATURE;
402         
403         if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
404                 return EXT2_ET_RO_UNSUPP_FEATURE;
405
406         /* We have now checked whether we know enough about the journal
407          * format to be able to proceed safely, so any other checks that
408          * fail we should attempt to recover from. */
409         if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
410                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
411                         _("%s: no valid journal superblock found\n"),
412                         ctx->device_name);
413                 return EXT2_ET_CORRUPT_SUPERBLOCK;
414         }
415
416         if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
417                 journal->j_maxlen = ntohl(jsb->s_maxlen);
418         else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
419                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
420                         _("%s: journal too short\n"),
421                         ctx->device_name);
422                 return EXT2_ET_CORRUPT_SUPERBLOCK;
423         }
424
425         journal->j_tail_sequence = ntohl(jsb->s_sequence);
426         journal->j_transaction_sequence = journal->j_tail_sequence;
427         journal->j_tail = ntohl(jsb->s_start);
428         journal->j_first = ntohl(jsb->s_first);
429         journal->j_last = ntohl(jsb->s_maxlen);
430
431         return 0;
432 }
433
434 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
435                                        journal_t *journal)
436 {
437         char *p;
438         union {
439                 uuid_t uuid;
440                 __u32 val[4];
441         } u;
442         __u32 new_seq = 0;
443         int i;
444
445         /* Leave a valid existing V1 superblock signature alone.
446          * Anything unrecognisable we overwrite with a new V2
447          * signature. */
448         
449         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
450             jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
451                 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
452                 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
453         }
454
455         /* Zero out everything else beyond the superblock header */
456         
457         p = ((char *) jsb) + sizeof(journal_header_t);
458         memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
459
460         jsb->s_blocksize = htonl(ctx->fs->blocksize);
461         jsb->s_maxlen = htonl(journal->j_maxlen);
462         jsb->s_first = htonl(1);
463
464         /* Initialize the journal sequence number so that there is "no"
465          * chance we will find old "valid" transactions in the journal.
466          * This avoids the need to zero the whole journal (slow to do,
467          * and risky when we are just recovering the filesystem).
468          */
469         uuid_generate(u.uuid);
470         for (i = 0; i < 4; i ++)
471                 new_seq ^= u.val[i];
472         jsb->s_sequence = htonl(new_seq);
473
474         mark_buffer_dirty(journal->j_sb_buffer, 1);
475         ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
476 }
477
478 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
479                                                   journal_t *journal,
480                                                   struct problem_context *pctx)
481 {
482         struct ext2_super_block *sb = ctx->fs->super;
483         int recover = ctx->fs->super->s_feature_incompat &
484                 EXT3_FEATURE_INCOMPAT_RECOVER;
485
486         pctx->num = journal->j_inode->i_ino;
487
488         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
489                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
490                         e2fsck_journal_reset_super(ctx, journal->j_superblock,
491                                                    journal);
492                         journal->j_transaction_sequence = 1;
493                         e2fsck_clear_recover(ctx, recover);
494                         return 0;
495                 }
496                 return EXT2_ET_CORRUPT_SUPERBLOCK;
497         } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
498                 return EXT2_ET_CORRUPT_SUPERBLOCK;
499
500         return 0;
501 }
502
503 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
504                                    int reset, int drop)
505 {
506         journal_superblock_t *jsb;
507
508         if (drop)
509                 mark_buffer_clean(journal->j_sb_buffer);
510         else if (!(ctx->options & E2F_OPT_READONLY)) {
511                 jsb = journal->j_superblock;
512                 jsb->s_sequence = htonl(journal->j_transaction_sequence);
513                 if (reset)
514                         jsb->s_start = 0; /* this marks the journal as empty */
515                 mark_buffer_dirty(journal->j_sb_buffer, 1);
516         }
517         brelse(journal->j_sb_buffer);
518
519         if (journal->j_inode)
520                 ext2fs_free_mem((void **)&journal->j_inode);
521         ext2fs_free_mem((void **)&journal);
522 }
523
524 /*
525  * This function makes sure that the superblock fields regarding the
526  * journal are consistent.
527  */
528 int e2fsck_check_ext3_journal(e2fsck_t ctx)
529 {
530         struct ext2_super_block *sb = ctx->fs->super;
531         journal_t *journal;
532         int recover = ctx->fs->super->s_feature_incompat &
533                 EXT3_FEATURE_INCOMPAT_RECOVER;
534         struct problem_context pctx;
535         int reset = 0, force_fsck = 0;
536         int retval;
537
538         /* If we don't have any journal features, don't do anything more */
539         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
540             !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
541             uuid_is_null(sb->s_journal_uuid))
542                 return 0;
543
544         clear_problem_context(&pctx);
545         pctx.num = sb->s_journal_inum;
546
547         retval = e2fsck_get_journal(ctx, &journal);
548         if (retval) {
549                 if (retval == EXT2_ET_BAD_INODE_NUM)
550                         return e2fsck_journal_fix_bad_inode(ctx, &pctx);
551                 return retval;
552         }
553
554         retval = e2fsck_journal_load(journal);
555         if (retval) {
556                 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
557                     ((retval == EXT2_ET_UNSUPP_FEATURE) &&
558                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
559                                   &pctx))) ||
560                     ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
561                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
562                                   &pctx))) ||
563                     ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
564                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
565                         retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
566                                                                   &pctx);
567                 e2fsck_journal_release(ctx, journal, 0, 1);
568                 return retval;
569         }
570
571         /*
572          * We want to make the flags consistent here.  We will not leave with
573          * needs_recovery set but has_journal clear.  We can't get in a loop
574          * with -y, -n, or -p, only if a user isn't making up their mind.
575          */
576 no_has_journal:
577         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
578                 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
579                 pctx.str = "inode";
580                 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
581                         if (recover &&
582                             !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
583                                 goto no_has_journal;
584                         /*
585                          * Need a full fsck if we are releasing a
586                          * journal stored on a reserved inode.
587                          */
588                         force_fsck = recover ||
589                                 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
590                         /* Clear all of the journal fields */
591                         sb->s_journal_inum = 0;
592                         sb->s_journal_dev = 0;
593                         memset(sb->s_journal_uuid, 0,
594                                sizeof(sb->s_journal_uuid));
595                         e2fsck_clear_recover(ctx, force_fsck);
596                 } else if (!(ctx->options & E2F_OPT_READONLY)) {
597                         sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
598                         ext2fs_mark_super_dirty(ctx->fs);
599                 }
600         }
601
602         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
603             !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
604             journal->j_superblock->s_start != 0) {
605                 if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
606                         reset = 1;
607                         sb->s_state &= ~EXT2_VALID_FS;
608                         ext2fs_mark_super_dirty(ctx->fs);
609                 }
610                 /*
611                  * If the user answers no to the above question, we
612                  * ignore the fact that journal apparently has data;
613                  * accidentally replaying over valid data would be far
614                  * worse than skipping a questionable recovery.
615                  * 
616                  * XXX should we abort with a fatal error here?  What
617                  * will the ext3 kernel code do if a filesystem with
618                  * !NEEDS_RECOVERY but with a non-zero
619                  * journal->j_superblock->s_start is mounted?
620                  */
621         }
622
623         e2fsck_journal_release(ctx, journal, reset, 0);
624         return retval;
625 }
626
627 static errcode_t recover_ext3_journal(e2fsck_t ctx)
628 {
629         journal_t *journal;
630         int retval;
631
632         retval = e2fsck_get_journal(ctx, &journal);
633         if (retval)
634                 return retval;
635
636         retval = e2fsck_journal_load(journal);
637         if (retval)
638                 goto errout;
639
640         retval = journal_init_revoke(journal, 1024);
641         if (retval)
642                 goto errout;
643         
644         retval = -journal_recover(journal);
645 errout:
646         e2fsck_journal_release(ctx, journal, 1, 0);
647         return retval;
648 }
649
650 int e2fsck_run_ext3_journal(e2fsck_t ctx)
651 {
652         io_manager io_ptr = ctx->fs->io->manager;
653         int blocksize = ctx->fs->blocksize;
654         errcode_t       retval, recover_retval;
655
656         printf(_("%s: recovering journal\n"), ctx->device_name);
657         if (ctx->options & E2F_OPT_READONLY) {
658                 printf(_("%s: won't do journal recovery while read-only\n"),
659                        ctx->device_name);
660                 return EXT2_ET_FILE_RO;
661         }
662
663         if (ctx->fs->flags & EXT2_FLAG_DIRTY)
664                 ext2fs_flush(ctx->fs);  /* Force out any modifications */
665
666         recover_retval = recover_ext3_journal(ctx);
667         
668         /*
669          * Reload the filesystem context to get up-to-date data from disk
670          * because journal recovery will change the filesystem under us.
671          */
672         ext2fs_close(ctx->fs);
673         retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
674                              ctx->superblock, blocksize, io_ptr,
675                              &ctx->fs);
676
677         if (retval) {
678                 com_err(ctx->program_name, retval,
679                         _("while trying to re-open %s"),
680                         ctx->device_name);
681                 fatal_error(ctx, 0);
682         }
683         ctx->fs->priv_data = ctx;
684
685         /* Set the superblock flags */
686         e2fsck_clear_recover(ctx, recover_retval);
687         return recover_retval;
688 }