Whamcloud - gitweb
78f2f1a947349bfa67870664532820ba3bab2a83
[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         const char      *journal_name;
231
232         clear_problem_context(&pctx);
233         journal_name = ctx->journal_name;
234         if (!journal_name)
235                 journal_name = ext2fs_find_block_device(s->s_journal_dev);
236
237         if (!journal_name) {
238                 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
239                 return EXT2_ET_LOAD_EXT_JOURNAL;
240         }
241
242         jfs_debug(1, "Using journal file %s\n", journal_name);
243
244 #if 1
245         io_ptr = unix_io_manager;
246 #else
247         io_ptr = test_io_manager;
248         test_io_backing_manager = unix_io_manager;
249 #endif
250         retval = io_ptr->open(journal_name, IO_FLAG_RW, &ctx->journal_io);
251         if (!ctx->journal_name)
252                 free((void *) journal_name);
253         if (retval)
254                 return retval;
255
256         io_channel_set_blksize(ctx->journal_io, blocksize);
257         start = (blocksize == 1024) ? 1 : 0;
258         bh = getblk(ctx, start, blocksize);
259         if (!bh)
260                 return EXT2_ET_NO_MEMORY;
261         ll_rw_block(READ, 1, &bh);
262         if (bh->b_err)
263                 return bh->b_err;
264         memcpy(&jsuper, start ? bh->b_data :  bh->b_data + 1024,
265                sizeof(jsuper));
266         brelse(bh);
267 #ifdef EXT2FS_ENABLE_SWAPFS
268         if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) 
269                 ext2fs_swap_super(&jsuper);
270 #endif
271         if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
272             !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
273                 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
274                 return EXT2_ET_LOAD_EXT_JOURNAL;
275         }
276         /* Make sure the journal UUID is correct */
277         if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
278                    sizeof(jsuper.s_uuid))) {
279                 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
280                 return EXT2_ET_LOAD_EXT_JOURNAL;
281         }
282                 
283         *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
284         if (!*journal) {
285                 return EXT2_ET_NO_MEMORY;
286         }
287
288         (*journal)->j_dev = ctx;
289         (*journal)->j_inode = NULL;
290         (*journal)->j_blocksize = ctx->fs->blocksize;
291         (*journal)->j_maxlen = jsuper.s_blocks_count;
292
293         bh = getblk(ctx, start+1, (*journal)->j_blocksize);
294         if (!bh) {
295                 retval = EXT2_ET_NO_MEMORY;
296                 goto errout;
297         }
298         (*journal)->j_sb_buffer = bh;
299         (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
300         
301         return 0;
302
303 errout:
304         ext2fs_free_mem((void **)journal);
305         return retval;
306 }
307
308 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
309 {
310         struct ext2_super_block *sb = ctx->fs->super;
311
312         if (uuid_is_null(sb->s_journal_uuid)) {
313                 if (!sb->s_journal_inum)
314                         return EXT2_ET_BAD_INODE_NUM;
315                 return e2fsck_journal_init_inode(ctx, sb, journal);
316         } else {
317                 return e2fsck_journal_init_dev(ctx, sb, journal);
318         }
319 }
320
321 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
322                                               struct problem_context *pctx)
323 {
324         struct ext2_super_block *sb = ctx->fs->super;
325         int recover = ctx->fs->super->s_feature_incompat &
326                 EXT3_FEATURE_INCOMPAT_RECOVER;
327         int has_journal = ctx->fs->super->s_feature_compat &
328                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
329
330         if (has_journal || sb->s_journal_inum) {
331                 /* The journal inode is bogus, remove and force full fsck */
332                 pctx->ino = sb->s_journal_inum;
333                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
334                         if (has_journal && sb->s_journal_inum)
335                                 printf("*** ext3 journal has been deleted - "
336                                        "filesystem is now ext2 only ***\n\n");
337                         sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
338                         sb->s_journal_inum = 0;
339                         ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
340                         e2fsck_clear_recover(ctx, 1);
341                         return 0;
342                 }
343                 return EXT2_ET_BAD_INODE_NUM;
344         } else if (recover) {
345                 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
346                         e2fsck_clear_recover(ctx, 1);
347                         return 0;
348                 }
349                 return EXT2_ET_UNSUPP_FEATURE;
350         }
351         return 0;
352 }
353
354 #define V1_SB_SIZE      0x0024
355 static void clear_v2_journal_fields(journal_t *journal)
356 {
357         e2fsck_t ctx = journal->j_dev;
358         struct buffer_head *jbh = journal->j_sb_buffer;
359         struct problem_context pctx;
360
361         clear_problem_context(&pctx);
362
363         if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
364                 return;
365
366         memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
367                ctx->fs->blocksize-V1_SB_SIZE);
368         mark_buffer_dirty(journal->j_sb_buffer, 1);
369 }
370
371
372 static errcode_t e2fsck_journal_load(journal_t *journal)
373 {
374         e2fsck_t ctx = journal->j_dev;
375         journal_superblock_t *jsb;
376         struct buffer_head *jbh = journal->j_sb_buffer;
377         struct problem_context pctx;
378
379         clear_problem_context(&pctx);
380
381         ll_rw_block(READ, 1, &jbh);
382         if (jbh->b_err) {
383                 com_err(ctx->device_name, jbh->b_err,
384                         _("reading journal superblock\n"));
385                 return jbh->b_err;
386         }
387
388         jsb = journal->j_superblock;
389         /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
390         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
391                 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
392
393         switch (ntohl(jsb->s_header.h_blocktype)) {
394         case JFS_SUPERBLOCK_V1:
395                 journal->j_format_version = 1;
396                 if (jsb->s_feature_compat ||
397                     jsb->s_feature_incompat ||
398                     jsb->s_feature_ro_compat ||
399                     jsb->s_nr_users)
400                         clear_v2_journal_fields(journal);
401                 break;
402                 
403         case JFS_SUPERBLOCK_V2:
404                 journal->j_format_version = 2;
405                 if (jsb->s_nr_users > 1 &&
406                     (ctx->fs->io == ctx->journal_io))
407                         clear_v2_journal_fields(journal);
408                 if (ntohl(jsb->s_nr_users) > 1) {
409                         fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
410                         return EXT2_ET_JOURNAL_UNSUPP_VERSION;
411                 }
412                 break;
413
414         /*
415          * These should never appear in a journal super block, so if
416          * they do, the journal is badly corrupted.
417          */
418         case JFS_DESCRIPTOR_BLOCK:
419         case JFS_COMMIT_BLOCK:
420         case JFS_REVOKE_BLOCK:
421                 return EXT2_ET_CORRUPT_SUPERBLOCK;
422                 
423         /* If we don't understand the superblock major type, but there
424          * is a magic number, then it is likely to be a new format we
425          * just don't understand, so leave it alone. */
426         default:
427                 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
428         }
429
430         if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
431                 return EXT2_ET_UNSUPP_FEATURE;
432         
433         if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
434                 return EXT2_ET_RO_UNSUPP_FEATURE;
435
436         /* We have now checked whether we know enough about the journal
437          * format to be able to proceed safely, so any other checks that
438          * fail we should attempt to recover from. */
439         if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
440                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
441                         _("%s: no valid journal superblock found\n"),
442                         ctx->device_name);
443                 return EXT2_ET_CORRUPT_SUPERBLOCK;
444         }
445
446         if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
447                 journal->j_maxlen = ntohl(jsb->s_maxlen);
448         else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
449                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
450                         _("%s: journal too short\n"),
451                         ctx->device_name);
452                 return EXT2_ET_CORRUPT_SUPERBLOCK;
453         }
454
455         journal->j_tail_sequence = ntohl(jsb->s_sequence);
456         journal->j_transaction_sequence = journal->j_tail_sequence;
457         journal->j_tail = ntohl(jsb->s_start);
458         journal->j_first = ntohl(jsb->s_first);
459         journal->j_last = ntohl(jsb->s_maxlen);
460
461         return 0;
462 }
463
464 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
465                                        journal_t *journal)
466 {
467         char *p;
468         union {
469                 uuid_t uuid;
470                 __u32 val[4];
471         } u;
472         __u32 new_seq = 0;
473         int i;
474
475         /* Leave a valid existing V1 superblock signature alone.
476          * Anything unrecognisable we overwrite with a new V2
477          * signature. */
478         
479         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
480             jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
481                 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
482                 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
483         }
484
485         /* Zero out everything else beyond the superblock header */
486         
487         p = ((char *) jsb) + sizeof(journal_header_t);
488         memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
489
490         jsb->s_blocksize = htonl(ctx->fs->blocksize);
491         jsb->s_maxlen = htonl(journal->j_maxlen);
492         jsb->s_first = htonl(1);
493
494         /* Initialize the journal sequence number so that there is "no"
495          * chance we will find old "valid" transactions in the journal.
496          * This avoids the need to zero the whole journal (slow to do,
497          * and risky when we are just recovering the filesystem).
498          */
499         uuid_generate(u.uuid);
500         for (i = 0; i < 4; i ++)
501                 new_seq ^= u.val[i];
502         jsb->s_sequence = htonl(new_seq);
503
504         mark_buffer_dirty(journal->j_sb_buffer, 1);
505         ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
506 }
507
508 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
509                                                   journal_t *journal,
510                                                   struct problem_context *pctx)
511 {
512         struct ext2_super_block *sb = ctx->fs->super;
513         int recover = ctx->fs->super->s_feature_incompat &
514                 EXT3_FEATURE_INCOMPAT_RECOVER;
515
516         pctx->num = journal->j_inode->i_ino;
517
518         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
519                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
520                         e2fsck_journal_reset_super(ctx, journal->j_superblock,
521                                                    journal);
522                         journal->j_transaction_sequence = 1;
523                         e2fsck_clear_recover(ctx, recover);
524                         return 0;
525                 }
526                 return EXT2_ET_CORRUPT_SUPERBLOCK;
527         } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
528                 return EXT2_ET_CORRUPT_SUPERBLOCK;
529
530         return 0;
531 }
532
533 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
534                                    int reset, int drop)
535 {
536         journal_superblock_t *jsb;
537
538         if (drop)
539                 mark_buffer_clean(journal->j_sb_buffer);
540         else if (!(ctx->options & E2F_OPT_READONLY)) {
541                 jsb = journal->j_superblock;
542                 jsb->s_sequence = htonl(journal->j_transaction_sequence);
543                 if (reset)
544                         jsb->s_start = 0; /* this marks the journal as empty */
545                 mark_buffer_dirty(journal->j_sb_buffer, 1);
546         }
547         brelse(journal->j_sb_buffer);
548
549         if (ctx->journal_io) {
550                 if (ctx->fs && ctx->fs->io != ctx->journal_io)
551                         io_channel_close(ctx->journal_io);
552                 ctx->journal_io = 0;
553         }
554         
555         if (journal->j_inode)
556                 ext2fs_free_mem((void **)&journal->j_inode);
557         ext2fs_free_mem((void **)&journal);
558 }
559
560 /*
561  * This function makes sure that the superblock fields regarding the
562  * journal are consistent.
563  */
564 int e2fsck_check_ext3_journal(e2fsck_t ctx)
565 {
566         struct ext2_super_block *sb = ctx->fs->super;
567         journal_t *journal;
568         int recover = ctx->fs->super->s_feature_incompat &
569                 EXT3_FEATURE_INCOMPAT_RECOVER;
570         struct problem_context pctx;
571         int reset = 0, force_fsck = 0;
572         int retval;
573
574         /* If we don't have any journal features, don't do anything more */
575         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
576             !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
577             uuid_is_null(sb->s_journal_uuid))
578                 return 0;
579
580         clear_problem_context(&pctx);
581         pctx.num = sb->s_journal_inum;
582
583         retval = e2fsck_get_journal(ctx, &journal);
584         if (retval) {
585                 if (retval == EXT2_ET_BAD_INODE_NUM)
586                         return e2fsck_journal_fix_bad_inode(ctx, &pctx);
587                 return retval;
588         }
589
590         retval = e2fsck_journal_load(journal);
591         if (retval) {
592                 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
593                     ((retval == EXT2_ET_UNSUPP_FEATURE) &&
594                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
595                                   &pctx))) ||
596                     ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
597                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
598                                   &pctx))) ||
599                     ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
600                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
601                         retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
602                                                                   &pctx);
603                 e2fsck_journal_release(ctx, journal, 0, 1);
604                 return retval;
605         }
606
607         /*
608          * We want to make the flags consistent here.  We will not leave with
609          * needs_recovery set but has_journal clear.  We can't get in a loop
610          * with -y, -n, or -p, only if a user isn't making up their mind.
611          */
612 no_has_journal:
613         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
614                 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
615                 pctx.str = "inode";
616                 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
617                         if (recover &&
618                             !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
619                                 goto no_has_journal;
620                         /*
621                          * Need a full fsck if we are releasing a
622                          * journal stored on a reserved inode.
623                          */
624                         force_fsck = recover ||
625                                 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
626                         /* Clear all of the journal fields */
627                         sb->s_journal_inum = 0;
628                         sb->s_journal_dev = 0;
629                         memset(sb->s_journal_uuid, 0,
630                                sizeof(sb->s_journal_uuid));
631                         e2fsck_clear_recover(ctx, force_fsck);
632                 } else if (!(ctx->options & E2F_OPT_READONLY)) {
633                         sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
634                         ext2fs_mark_super_dirty(ctx->fs);
635                 }
636         }
637
638         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
639             !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
640             journal->j_superblock->s_start != 0) {
641                 if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
642                         reset = 1;
643                         sb->s_state &= ~EXT2_VALID_FS;
644                         ext2fs_mark_super_dirty(ctx->fs);
645                 }
646                 /*
647                  * If the user answers no to the above question, we
648                  * ignore the fact that journal apparently has data;
649                  * accidentally replaying over valid data would be far
650                  * worse than skipping a questionable recovery.
651                  * 
652                  * XXX should we abort with a fatal error here?  What
653                  * will the ext3 kernel code do if a filesystem with
654                  * !NEEDS_RECOVERY but with a non-zero
655                  * journal->j_superblock->s_start is mounted?
656                  */
657         }
658
659         e2fsck_journal_release(ctx, journal, reset, 0);
660         return retval;
661 }
662
663 static errcode_t recover_ext3_journal(e2fsck_t ctx)
664 {
665         journal_t *journal;
666         int retval;
667
668         retval = e2fsck_get_journal(ctx, &journal);
669         if (retval)
670                 return retval;
671
672         retval = e2fsck_journal_load(journal);
673         if (retval)
674                 goto errout;
675
676         retval = journal_init_revoke(journal, 1024);
677         if (retval)
678                 goto errout;
679         
680         retval = -journal_recover(journal);
681         if (retval)
682                 goto errout;
683         
684         if (journal->j_superblock->s_errno) {
685                 ctx->fs->super->s_state |= EXT2_ERROR_FS;
686                 ext2fs_mark_super_dirty(ctx->fs);
687                 journal->j_superblock->s_errno = 0;
688                 mark_buffer_dirty(journal->j_sb_buffer, 1);
689         }
690                 
691 errout:
692         e2fsck_journal_release(ctx, journal, 1, 0);
693         return retval;
694 }
695
696 int e2fsck_run_ext3_journal(e2fsck_t ctx)
697 {
698         io_manager io_ptr = ctx->fs->io->manager;
699         int blocksize = ctx->fs->blocksize;
700         errcode_t       retval, recover_retval;
701
702         printf(_("%s: recovering journal\n"), ctx->device_name);
703         if (ctx->options & E2F_OPT_READONLY) {
704                 printf(_("%s: won't do journal recovery while read-only\n"),
705                        ctx->device_name);
706                 return EXT2_ET_FILE_RO;
707         }
708
709         if (ctx->fs->flags & EXT2_FLAG_DIRTY)
710                 ext2fs_flush(ctx->fs);  /* Force out any modifications */
711
712         recover_retval = recover_ext3_journal(ctx);
713         
714         /*
715          * Reload the filesystem context to get up-to-date data from disk
716          * because journal recovery will change the filesystem under us.
717          */
718         ext2fs_close(ctx->fs);
719         retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
720                              ctx->superblock, blocksize, io_ptr,
721                              &ctx->fs);
722
723         if (retval) {
724                 com_err(ctx->program_name, retval,
725                         _("while trying to re-open %s"),
726                         ctx->device_name);
727                 fatal_error(ctx, 0);
728         }
729         ctx->fs->priv_data = ctx;
730
731         /* Set the superblock flags */
732         e2fsck_clear_recover(ctx, recover_retval);
733         return recover_retval;
734 }
735
736 /*
737  * This function will move the journal inode from a visible file in
738  * the filesystem directory hierarchy to the reserved inode if necessary.
739  */
740 const static char * const journal_names[] = {
741         ".journal", "journal", ".journal.dat", "journal.dat", 0 };
742
743 void e2fsck_move_ext3_journal(e2fsck_t ctx)
744 {
745         struct ext2_super_block *sb = ctx->fs->super;
746         struct problem_context  pctx;
747         struct ext2_inode       inode;
748         ext2_filsys             fs = ctx->fs;
749         ext2_ino_t              ino;
750         errcode_t               retval;
751         const char * const *    cpp;
752         int                     group, mount_flags;
753         
754         /*
755          * If the filesystem is opened read-only, or there is no
756          * journal, or the journal is already in the hidden inode,
757          * then do nothing.
758          */
759         if ((ctx->options & E2F_OPT_READONLY) ||
760             (sb->s_journal_inum == 0) ||
761             (sb->s_journal_inum == EXT2_JOURNAL_INO) ||
762             !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
763                 return;
764
765         /*
766          * If the filesystem is mounted, or we can't tell whether
767          * or not it's mounted, do nothing.
768          */
769         retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
770         if (retval || (mount_flags & EXT2_MF_MOUNTED))
771                 return;
772
773         /*
774          * If we can't find the name of the journal inode, then do
775          * nothing.
776          */
777         for (cpp = journal_names; *cpp; cpp++) {
778                 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
779                                        strlen(*cpp), 0, &ino);
780                 if ((retval == 0) && (ino == sb->s_journal_inum))
781                         break;
782         }
783         if (*cpp == 0)
784                 return;
785
786         /*
787          * The inode had better have only one link and not be readable.
788          */
789         if (ext2fs_read_inode(fs, ino, &inode) != 0)
790                 return;
791         if (inode.i_links_count != 1)
792                 return;
793
794         /* We need the inode bitmap to be loaded */
795         retval = ext2fs_read_bitmaps(fs);
796         if (retval)
797                 return;
798
799         clear_problem_context(&pctx);
800         pctx.str = *cpp;
801         if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
802                 return;
803                 
804         /*
805          * OK, we've done all the checks, let's actually move the
806          * journal inode.  Errors at this point mean we need to force
807          * an ext2 filesystem check.
808          */
809         if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
810                 goto err_out;
811         if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
812                 goto err_out;
813         sb->s_journal_inum = EXT2_JOURNAL_INO;
814         ext2fs_mark_super_dirty(fs);
815         inode.i_links_count = 0;
816         inode.i_dtime = time(0);
817         if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
818                 goto err_out;
819
820         group = ext2fs_group_of_ino(fs, ino);
821         ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
822         ext2fs_mark_ib_dirty(fs);
823         fs->group_desc[group].bg_free_inodes_count++;
824         fs->super->s_free_inodes_count++;
825         return;
826
827 err_out:
828         pctx.errcode = retval;
829         fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
830         fs->super->s_state &= ~EXT2_VALID_FS;
831         ext2fs_mark_super_dirty(fs);
832         return;
833 }
834