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