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