Whamcloud - gitweb
Fix really stupid typo.
[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->fs->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->fs->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                                            ext2_ino_t journal_inum,
163                                            journal_t **journal)
164 {
165         struct inode *inode;
166         struct buffer_head *bh;
167         blk_t start;
168         int retval;
169
170         jfs_debug(1, "Using journal inode %u\n", journal_inum);
171         *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
172         if (!*journal) {
173                 return EXT2_ET_NO_MEMORY;
174         }
175
176         inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode");
177         if (!inode) {
178                 retval = EXT2_ET_NO_MEMORY;
179                 goto exit_journal;
180         }
181
182         inode->i_ctx = ctx;
183         inode->i_ino = journal_inum;
184         retval = ext2fs_read_inode(ctx->fs, journal_inum, &inode->i_ext2);
185         if (retval)
186                 goto exit_inode;
187
188         (*journal)->j_dev = ctx;
189         (*journal)->j_inode = inode;
190         (*journal)->j_blocksize = ctx->fs->blocksize;
191         (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize;
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_get_journal(e2fsck_t ctx, journal_t **journal)
220 {
221         char uuid_str[40];
222         struct problem_context pctx;
223         struct ext2_super_block *sb = ctx->fs->super;
224
225         clear_problem_context(&pctx);
226
227         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
228                 /* FIXME: check if dev is valid block dev, has a journal */
229                 if (sb->s_journal_dev) {
230                         pctx.num = sb->s_journal_dev;
231                         /* this problem aborts on -y, -p, unsupported on -n */
232                         if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_DEV, &pctx))
233                                 return EXT2_ET_UNSUPP_FEATURE;
234                         sb->s_journal_dev = 0;
235                         sb->s_state &= ~EXT2_VALID_FS;
236                         ext2fs_mark_super_dirty(ctx->fs);
237                 }
238                 /* FIXME: check if UUID is valid block dev, has a journal */
239                 if (!uuid_is_null(sb->s_journal_uuid)) {
240                         uuid_unparse(sb->s_journal_uuid, uuid_str);
241                         pctx.str = uuid_str;
242                         /* this problem aborts on -y, -p, unsupported on -n */
243                         if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_UUID, &pctx))
244                                 return EXT2_ET_UNSUPP_FEATURE;
245                         uuid_clear(sb->s_journal_uuid);
246                         sb->s_state &= ~EXT2_VALID_FS;
247                         ext2fs_mark_super_dirty(ctx->fs);
248                 }
249                 if (!sb->s_journal_inum)
250                         return EXT2_ET_BAD_INODE_NUM;
251         }
252
253         if (sb->s_journal_dev) {
254                 pctx.num = sb->s_journal_dev;
255                 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_DEV, &pctx))
256                         return EXT2_ET_UNSUPP_FEATURE;
257                 sb->s_journal_dev = 0;
258                 sb->s_state &= ~EXT2_VALID_FS;
259                 ext2fs_mark_super_dirty(ctx->fs);
260         }
261         if (!uuid_is_null(sb->s_journal_uuid)) {
262                 uuid_unparse(sb->s_journal_uuid, uuid_str);
263                 pctx.str = uuid_str;
264                 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx))
265                         return EXT2_ET_UNSUPP_FEATURE;
266                 uuid_clear(sb->s_journal_uuid);
267                 sb->s_state &= ~EXT2_VALID_FS;
268                 ext2fs_mark_super_dirty(ctx->fs);
269         }
270
271         return e2fsck_journal_init_inode(ctx, sb, sb->s_journal_inum, journal);
272 }
273
274 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
275                                               struct problem_context *pctx)
276 {
277         struct ext2_super_block *sb = ctx->fs->super;
278         int recover = ctx->fs->super->s_feature_incompat &
279                 EXT3_FEATURE_INCOMPAT_RECOVER;
280         int has_journal = ctx->fs->super->s_feature_compat &
281                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
282
283         if (has_journal || sb->s_journal_inum) {
284                 /* The journal inode is bogus, remove and force full fsck */
285                 pctx->ino = sb->s_journal_inum;
286                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
287                         if (has_journal && sb->s_journal_inum)
288                                 printf("*** ext3 journal has been deleted - "
289                                        "filesystem is now ext2 only ***\n\n");
290                         sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
291                         sb->s_journal_inum = 0;
292                         ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
293                         e2fsck_clear_recover(ctx, 1);
294                         return 0;
295                 }
296                 return EXT2_ET_BAD_INODE_NUM;
297         } else if (recover) {
298                 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
299                         e2fsck_clear_recover(ctx, 1);
300                         return 0;
301                 }
302                 return EXT2_ET_UNSUPP_FEATURE;
303         }
304         return 0;
305 }
306
307 static errcode_t e2fsck_journal_load(journal_t *journal)
308 {
309         e2fsck_t ctx = journal->j_dev;
310         journal_superblock_t *jsb;
311         struct buffer_head *jbh = journal->j_sb_buffer;
312         struct problem_context pctx;
313
314         clear_problem_context(&pctx);
315
316         ll_rw_block(READ, 1, &jbh);
317         if (jbh->b_err) {
318                 com_err(ctx->device_name, jbh->b_err,
319                         _("reading journal superblock\n"));
320                 return jbh->b_err;
321         }
322
323         jsb = journal->j_superblock;
324         /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
325         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
326                 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
327
328         switch (ntohl(jsb->s_header.h_blocktype)) {
329         case JFS_SUPERBLOCK_V1:
330                 journal->j_format_version = 1;
331                 break;
332                 
333         case JFS_SUPERBLOCK_V2:
334                 journal->j_format_version = 2;
335                 break;
336
337         /*
338          * These should never appear in a journal super block, so if
339          * they do, the journal is badly corrupted.
340          */
341         case JFS_DESCRIPTOR_BLOCK:
342         case JFS_COMMIT_BLOCK:
343         case JFS_REVOKE_BLOCK:
344                 return EXT2_ET_CORRUPT_SUPERBLOCK;
345                 
346         /* If we don't understand the superblock major type, but there
347          * is a magic number, then it is likely to be a new format we
348          * just don't understand, so leave it alone. */
349         default:
350                 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
351         }
352
353         if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
354                 return EXT2_ET_UNSUPP_FEATURE;
355         
356         if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
357                 return EXT2_ET_RO_UNSUPP_FEATURE;
358
359         /* We have now checked whether we know enough about the journal
360          * format to be able to proceed safely, so any other checks that
361          * fail we should attempt to recover from. */
362         if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
363                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
364                         _("%s: no valid journal superblock found\n"),
365                         ctx->device_name);
366                 return EXT2_ET_CORRUPT_SUPERBLOCK;
367         }
368
369         if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
370                 journal->j_maxlen = ntohl(jsb->s_maxlen);
371         else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
372                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
373                         _("%s: journal too short\n"),
374                         ctx->device_name);
375                 return EXT2_ET_CORRUPT_SUPERBLOCK;
376         }
377
378         journal->j_tail_sequence = ntohl(jsb->s_sequence);
379         journal->j_transaction_sequence = journal->j_tail_sequence;
380         journal->j_tail = ntohl(jsb->s_start);
381         journal->j_first = ntohl(jsb->s_first);
382         journal->j_last = ntohl(jsb->s_maxlen);
383
384         return 0;
385 }
386
387 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
388                                        journal_t *journal)
389 {
390         char *p;
391         union {
392                 uuid_t uuid;
393                 __u32 val[4];
394         } u;
395         __u32 new_seq = 0;
396         int i;
397
398         /* Leave a valid existing V1 superblock signature alone.
399          * Anything unrecognisable we overwrite with a new V2
400          * signature. */
401         
402         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
403             jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
404                 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
405                 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
406         }
407
408         /* Zero out everything else beyond the superblock header */
409         
410         p = ((char *) jsb) + sizeof(journal_header_t);
411         memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
412
413         jsb->s_blocksize = htonl(ctx->fs->blocksize);
414         jsb->s_maxlen = htonl(journal->j_maxlen);
415         jsb->s_first = htonl(1);
416
417         /* Initialize the journal sequence number so that there is "no"
418          * chance we will find old "valid" transactions in the journal.
419          * This avoids the need to zero the whole journal (slow to do,
420          * and risky when we are just recovering the filesystem).
421          */
422         uuid_generate(u.uuid);
423         for (i = 0; i < 4; i ++)
424                 new_seq ^= u.val[i];
425         jsb->s_sequence = htonl(new_seq);
426
427         mark_buffer_dirty(journal->j_sb_buffer, 1);
428         ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
429 }
430
431 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
432                                                   journal_t *journal,
433                                                   struct problem_context *pctx)
434 {
435         struct ext2_super_block *sb = ctx->fs->super;
436         int recover = ctx->fs->super->s_feature_incompat &
437                 EXT3_FEATURE_INCOMPAT_RECOVER;
438
439         pctx->num = journal->j_inode->i_ino;
440
441         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
442                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
443                         e2fsck_journal_reset_super(ctx, journal->j_superblock,
444                                                    journal);
445                         journal->j_transaction_sequence = 1;
446                         e2fsck_clear_recover(ctx, recover);
447                         return 0;
448                 }
449                 return EXT2_ET_CORRUPT_SUPERBLOCK;
450         } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
451                 return EXT2_ET_CORRUPT_SUPERBLOCK;
452
453         return 0;
454 }
455
456 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
457                                    int reset, int drop)
458 {
459         journal_superblock_t *jsb;
460
461         if (drop)
462                 mark_buffer_clean(journal->j_sb_buffer);
463         else if (!(ctx->options & E2F_OPT_READONLY)) {
464                 jsb = journal->j_superblock;
465                 jsb->s_sequence = htonl(journal->j_transaction_sequence);
466                 if (reset)
467                         jsb->s_start = 0; /* this marks the journal as empty */
468                 mark_buffer_dirty(journal->j_sb_buffer, 1);
469         }
470         brelse(journal->j_sb_buffer);
471
472         if (journal->j_inode)
473                 ext2fs_free_mem((void **)&journal->j_inode);
474         ext2fs_free_mem((void **)&journal);
475 }
476
477 /*
478  * This function makes sure that the superblock fields regarding the
479  * journal are consistent.
480  */
481 int e2fsck_check_ext3_journal(e2fsck_t ctx)
482 {
483         struct ext2_super_block *sb = ctx->fs->super;
484         journal_t *journal;
485         int recover = ctx->fs->super->s_feature_incompat &
486                 EXT3_FEATURE_INCOMPAT_RECOVER;
487         struct problem_context pctx;
488         int reset = 0, force_fsck = 0;
489         int retval;
490
491         /* If we don't have any journal features, don't do anything more */
492         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
493             !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
494             uuid_is_null(sb->s_journal_uuid))
495                 return 0;
496
497         clear_problem_context(&pctx);
498         pctx.num = sb->s_journal_inum;
499
500         retval = e2fsck_get_journal(ctx, &journal);
501         if (retval) {
502                 if (retval == EXT2_ET_BAD_INODE_NUM)
503                         return e2fsck_journal_fix_bad_inode(ctx, &pctx);
504                 return retval;
505         }
506
507         retval = e2fsck_journal_load(journal);
508         if (retval) {
509                 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
510                     ((retval == EXT2_ET_UNSUPP_FEATURE) &&
511                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
512                                   &pctx))) ||
513                     ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
514                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
515                                   &pctx))) ||
516                     ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
517                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
518                         retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
519                                                                   &pctx);
520                 e2fsck_journal_release(ctx, journal, 0, 1);
521                 return retval;
522         }
523
524         /*
525          * We want to make the flags consistent here.  We will not leave with
526          * needs_recovery set but has_journal clear.  We can't get in a loop
527          * with -y, -n, or -p, only if a user isn't making up their mind.
528          */
529 no_has_journal:
530         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
531                 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
532                 pctx.str = "inode";
533                 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
534                         if (recover &&
535                             !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
536                                 goto no_has_journal;
537                         /*
538                          * Need a full fsck if we are releasing a
539                          * journal stored on a reserved inode.
540                          */
541                         force_fsck = recover ||
542                                 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
543                         /* Clear all of the journal fields */
544                         sb->s_journal_inum = 0;
545                         sb->s_journal_dev = 0;
546                         memset(sb->s_journal_uuid, 0,
547                                sizeof(sb->s_journal_uuid));
548                         e2fsck_clear_recover(ctx, force_fsck);
549                 } else if (!(ctx->options & E2F_OPT_READONLY)) {
550                         sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
551                         ext2fs_mark_super_dirty(ctx->fs);
552                 }
553         }
554
555         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
556             !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
557             journal->j_superblock->s_start != 0) {
558                 if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
559                         reset = 1;
560                         sb->s_state &= ~EXT2_VALID_FS;
561                         ext2fs_mark_super_dirty(ctx->fs);
562                 }
563                 /*
564                  * If the user answers no to the above question, we
565                  * ignore the fact that journal apparently has data;
566                  * accidentally replaying over valid data would be far
567                  * worse than skipping a questionable recovery.
568                  * 
569                  * XXX should we abort with a fatal error here?  What
570                  * will the ext3 kernel code do if a filesystem with
571                  * !NEEDS_RECOVERY but with a non-zero
572                  * journal->j_superblock->s_start is mounted?
573                  */
574         }
575
576         e2fsck_journal_release(ctx, journal, reset, 0);
577         return retval;
578 }
579
580 static errcode_t recover_ext3_journal(e2fsck_t ctx)
581 {
582         journal_t *journal;
583         int retval;
584
585         retval = e2fsck_get_journal(ctx, &journal);
586         if (retval)
587                 return retval;
588
589         retval = e2fsck_journal_load(journal);
590         if (retval)
591                 goto errout;
592
593         retval = journal_init_revoke(journal, 1024);
594         if (retval)
595                 goto errout;
596         
597         retval = -journal_recover(journal);
598 errout:
599         e2fsck_journal_release(ctx, journal, 1, 0);
600         return retval;
601 }
602
603
604 #if 0
605 #define TEMPLATE "/tmp/ext3.XXXXXX"
606
607 /*
608  * This function attempts to mount and unmount an ext3 filesystem,
609  * which is a cheap way to force the kernel to run the journal and
610  * handle the recovery for us.
611  */
612 static errcode_t recover_ext3_journal_via_mount(e2fsck_t ctx)
613 {
614         ext2_filsys fs = ctx->fs;
615         char    *dirlist[] = {"/mnt","/lost+found","/tmp","/root","/boot",0};
616         errcode_t        retval, retval2;
617         int      count = 0;
618         char     template[] = TEMPLATE;
619         struct stat buf;
620         char    *tmpdir;
621
622         if (ctx->options & E2F_OPT_READONLY) {
623                 printf("%s: won't do journal recovery while read-only\n",
624                        ctx->device_name);
625                 return EXT2_ET_FILE_RO;
626         }
627
628         printf(_("%s: trying for ext3 kernel journal recovery\n"),
629                ctx->device_name);
630         /*
631          * First try to make a temporary directory.  This may fail if
632          * the root partition is still mounted read-only.
633          */
634 newtemp:
635         tmpdir = mktemp(template);
636         if (tmpdir) {
637                 jfs_debug(2, "trying %s as ext3 temp mount point\n", tmpdir);
638                 if (mkdir(template, 0700)) {
639                         if (errno == EROFS) {
640                                 tmpdir = NULL;
641                                 template[0] = '\0';
642                         } else if (errno == EEXIST && count++ < 10) {
643                                 strcpy(template, TEMPLATE);
644                                 goto newtemp;
645                         }
646                         return errno;
647                 }
648         }
649
650         /*
651          * OK, creating a temporary directory didn't work.
652          * Let's try a list of possible temporary mountpoints.
653          */
654         if (!tmpdir) {
655                 dev_t   rootdev;
656                 char    **cpp, *dir;
657
658                 if (stat("/", &buf))
659                         return errno;
660
661                 rootdev = buf.st_dev;
662
663                 /*
664                  * Check that dir is on the same device as root (no other
665                  * filesystem is mounted there), and it's a directory.
666                  */
667                 for (cpp = dirlist; (dir = *cpp); cpp++)
668                         if (stat(dir, &buf) == 0 && buf.st_dev == rootdev &&
669                             S_ISDIR(buf.st_mode)) {
670                                 tmpdir = dir;
671                                 break;
672                         }
673         }
674
675         if (tmpdir) {
676                 io_manager      io_ptr = fs->io->manager;
677                 int             blocksize = fs->blocksize;
678
679                 jfs_debug(2, "using %s for ext3 mount\n", tmpdir);
680                 /* FIXME - need to handle loop devices here */
681                 if (mount(ctx->device_name, tmpdir, "ext3", MNT_FL, NULL)) {
682                         retval = errno;
683                         com_err(ctx->program_name, errno,
684                                 "when mounting %s", ctx->device_name);
685                         if (template[0])
686                                 rmdir(tmpdir);
687                         return retval;
688                 }
689                 /*
690                  * Now that it mounted cleanly, the filesystem will have been
691                  * recovered, so we can now unmount it.
692                  */
693                 if (umount(tmpdir))
694                         return errno;
695
696                 /*
697                  * Remove the temporary directory, if it was created.
698                  */
699                 if (template[0])
700                         rmdir(tmpdir);
701                 return 0;
702         }
703 }
704 #endif
705
706 int e2fsck_run_ext3_journal(e2fsck_t ctx)
707 {
708         io_manager io_ptr = ctx->fs->io->manager;
709         int blocksize = ctx->fs->blocksize;
710         errcode_t       retval, recover_retval;
711
712         printf(_("%s: recovering journal\n"), ctx->device_name);
713         if (ctx->options & E2F_OPT_READONLY) {
714                 printf(_("%s: won't do journal recovery while read-only\n"),
715                        ctx->device_name);
716                 return EXT2_ET_FILE_RO;
717         }
718
719         if (ctx->fs->flags & EXT2_FLAG_DIRTY)
720                 ext2fs_flush(ctx->fs);  /* Force out any modifications */
721
722         recover_retval = recover_ext3_journal(ctx);
723         
724         /*
725          * Reload the filesystem context to get up-to-date data from disk
726          * because journal recovery will change the filesystem under us.
727          */
728         ext2fs_close(ctx->fs);
729         retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
730                              ctx->superblock, blocksize, io_ptr,
731                              &ctx->fs);
732
733         if (retval) {
734                 com_err(ctx->program_name, retval,
735                         _("while trying to re-open %s"),
736                         ctx->device_name);
737                 fatal_error(ctx, 0);
738         }
739         ctx->fs->priv_data = ctx;
740
741         /* Set the superblock flags */
742         e2fsck_clear_recover(ctx, recover_retval);
743         return recover_retval;
744 }