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