Whamcloud - gitweb
Many files:
[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 #include "jfs_user.h"
24 #include "problem.h"
25 #include "uuid/uuid.h"
26
27 #ifdef JFS_DEBUG
28 static int bh_count = 0;
29 int journal_enable_debug = 0;
30 #endif
31
32 int bmap(struct inode *inode, int block)
33 {
34         int retval;
35         blk_t phys;
36
37         retval = ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, &inode->i_ext2,
38                              NULL, 0, block, &phys);
39
40         if (retval)
41                 com_err(inode->i_ctx->device_name, retval,
42                         _("bmap journal inode %ld, block %d\n"),
43                         inode->i_ino, block);
44
45         return phys;
46 }
47
48 struct buffer_head *getblk(e2fsck_t ctx, blk_t blocknr, int blocksize)
49 {
50         struct buffer_head *bh;
51
52         bh = e2fsck_allocate_memory(ctx, sizeof(*bh), "block buffer");
53         if (!bh)
54                 return NULL;
55
56         jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
57                   blocknr, blocksize, ++bh_count);
58
59         bh->b_ctx = ctx;
60         bh->b_size = blocksize;
61         bh->b_blocknr = blocknr;
62
63         return bh;
64 }
65
66 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
67 {
68         int retval;
69         struct buffer_head *bh;
70
71         for (; nr > 0; --nr) {
72                 bh = *bhp++;
73                 if (rw == READ && !bh->b_uptodate) {
74                         jfs_debug(3, "reading block %lu/%p\n", 
75                                   bh->b_blocknr, bh);
76                         retval = io_channel_read_blk(bh->b_ctx->fs->io, 
77                                                      bh->b_blocknr,
78                                                      1, bh->b_data);
79                         if (retval) {
80                                 com_err(bh->b_ctx->device_name, retval,
81                                         "while reading block %ld\n", 
82                                         bh->b_blocknr);
83                                 bh->b_err = retval;
84                                 continue;
85                         }
86                         bh->b_uptodate = 1;
87                 } else if (rw == WRITE && bh->b_dirty) {
88                         jfs_debug(3, "writing block %lu/%p\n", 
89                                   bh->b_blocknr, bh);
90                         retval = io_channel_write_blk(bh->b_ctx->fs->io, 
91                                                       bh->b_blocknr,
92                                                       1, bh->b_data);
93                         if (retval) {
94                                 com_err(bh->b_ctx->device_name, retval,
95                                         "while writing block %ld\n", 
96                                         bh->b_blocknr);
97                                 bh->b_err = retval;
98                                 continue;
99                         }
100                         bh->b_dirty = 0;
101                         bh->b_uptodate = 1;
102                 } else
103                         jfs_debug(3, "no-op %s for block %lu\n",
104                                   rw == READ ? "read" : "write", 
105                                   bh->b_blocknr);
106         }
107 }
108
109 void mark_buffer_dirty(struct buffer_head *bh, int dummy)
110 {
111         bh->b_dirty = dummy | 1; /* use dummy to avoid unused variable */
112 }
113
114 void brelse(struct buffer_head *bh)
115 {
116         if (bh->b_dirty)
117                 ll_rw_block(WRITE, 1, &bh);
118         jfs_debug(3, "freeing block %lu/%p (total %d)\n",
119                   bh->b_blocknr, bh, --bh_count);
120         ext2fs_free_mem((void **) &bh);
121 }
122
123 int buffer_uptodate(struct buffer_head *bh)
124 {
125         return bh->b_uptodate;
126 }
127
128 void wait_on_buffer(struct buffer_head *bh)
129 {
130         if (!bh->b_uptodate)
131                 ll_rw_block(READ, 1, &bh);
132 }
133
134
135 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
136 {
137         struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
138
139         s->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
140
141         /* if we had an error doing journal recovery, we need a full fsck */
142         if (error)
143                 s->s_state &= ~EXT2_VALID_FS;
144         ext2fs_mark_super_dirty(ctx->fs);
145 }
146
147 static int e2fsck_journal_init_inode(e2fsck_t ctx, struct ext2fs_sb *s,
148                                      ino_t journal_inum, journal_t **journal)
149 {
150         struct inode *inode;
151         const char *cmdname = ctx->program_name;
152         struct buffer_head *bh;
153         blk_t start;
154         int retval;
155
156         jfs_debug(1, "Using journal inode %lu\n", journal_inum);
157         *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
158         if (!*journal) {
159                 return EXT2_ET_NO_MEMORY;
160         }
161
162         inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode");
163         if (!inode) {
164                 retval = EXT2_ET_NO_MEMORY;
165                 goto exit_journal;
166         }
167
168         inode->i_ctx = ctx;
169         inode->i_ino = journal_inum;
170         retval = ext2fs_read_inode(ctx->fs, journal_inum, &inode->i_ext2);
171         if (retval)
172                 goto exit_inode;
173
174         (*journal)->j_dev = ctx;
175         (*journal)->j_inode = inode;
176         (*journal)->j_blocksize = ctx->fs->blocksize;
177         (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize;
178
179         if (!inode->i_ext2.i_links_count ||
180             !LINUX_S_ISREG(inode->i_ext2.i_mode) ||
181             (*journal)->j_maxlen < JFS_MIN_JOURNAL_BLOCKS ||
182             (start = bmap(inode, 0)) == 0) {
183                 retval = EXT2_ET_BAD_INODE_NUM;
184                 goto exit_inode;
185         }
186
187         bh = getblk(ctx, start, (*journal)->j_blocksize);
188         if (!bh) {
189                 retval = EXT2_ET_NO_MEMORY;
190                 goto exit_inode;
191         }
192         (*journal)->j_sb_buffer = bh;
193         (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
194
195         return 0;
196
197 exit_inode:
198         ext2fs_free_mem((void **)&inode);
199 exit_journal:
200         ext2fs_free_mem((void **)journal);
201
202         return retval;
203 }
204
205 static int e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
206 {
207         char uuid_str[40];
208         struct problem_context pctx;
209         struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
210         int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
211
212         clear_problem_context(&pctx);
213
214         if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
215                 if (s->s_journal_dev) {
216                         pctx.num = s->s_journal_dev;
217                         /* this problem aborts on -y, -p, unsupported on -n */
218                         if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_DEV, &pctx))
219                                 return EXT2_ET_UNSUPP_FEATURE;
220                         s->s_journal_dev = 0;
221                         s->s_state &= ~EXT2_VALID_FS;
222                         ext2fs_mark_super_dirty(ctx->fs);
223                 }
224                 if (!uuid_is_null(s->s_journal_uuid)) {
225                         uuid_unparse(s->s_journal_uuid, uuid_str);
226                         pctx.str = uuid_str;
227                         /* this problem aborts on -y, -p, unsupported on -n */
228                         if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_UUID, &pctx))
229                                 return EXT2_ET_UNSUPP_FEATURE;
230                         uuid_clear(s->s_journal_uuid);
231                         s->s_state &= ~EXT2_VALID_FS;
232                         ext2fs_mark_super_dirty(ctx->fs);
233                 }
234                 if (!s->s_journal_inum)
235                         return EXT2_ET_BAD_INODE_NUM;
236         }
237
238         if (s->s_journal_dev) {
239                 pctx.num = s->s_journal_dev;
240                 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_DEV, &pctx))
241                         return EXT2_ET_UNSUPP_FEATURE;
242                 s->s_journal_dev = 0;
243                 s->s_state &= ~EXT2_VALID_FS;
244                 ext2fs_mark_super_dirty(ctx->fs);
245         }
246         if (!uuid_is_null(s->s_journal_uuid)) {
247                 uuid_unparse(s->s_journal_uuid, uuid_str);
248                 pctx.str = uuid_str;
249                 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx))
250                         return EXT2_ET_UNSUPP_FEATURE;
251                 uuid_clear(s->s_journal_uuid);
252                 s->s_state &= ~EXT2_VALID_FS;
253                 ext2fs_mark_super_dirty(ctx->fs);
254         }
255
256         return e2fsck_journal_init_inode(ctx, s, s->s_journal_inum, journal);
257 }
258
259 static int e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
260                                         struct problem_context *pctx)
261 {
262         struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
263         int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
264         int has_journal = s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL;
265
266         if (has_journal || s->s_journal_inum) {
267                 /* The journal inode is bogus, remove and force full fsck */
268                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
269                         struct ext2fs_sb *s =(struct ext2fs_sb *)ctx->fs->super;
270
271                         if (has_journal && s->s_journal_inum)
272                                 printf("*** ext3 journal has been deleted - "
273                                        "filesystem is now ext2 only ***\n\n");
274                         s->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
275                         s->s_journal_inum = 0;
276                         e2fsck_clear_recover(ctx, 1);
277                         return 0;
278                 }
279                 return EXT2_ET_BAD_INODE_NUM;
280         } else if (recover) {
281                 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
282                         e2fsck_clear_recover(ctx, 1);
283                         return 0;
284                 }
285                 return EXT2_ET_UNSUPP_FEATURE;
286         }
287         return 0;
288 }
289
290 static int e2fsck_journal_fix_unsupported_super(e2fsck_t ctx,
291                                                 struct problem_context *pctx)
292 {
293         struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
294
295         /* Unsupported journal superblock - first choice is abort.
296          * Declining that gives the option to reset the superblock.
297          *
298          * Otherwise we get the chance to delete the journal, and
299          * failing that we abort because we can't handle this.
300          */
301         if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
302             fix_problem(ctx, PR_0_JOURNAL_UNSUPP_SUPER, pctx))
303                 return EXT2_ET_CORRUPT_SUPERBLOCK;
304
305         if (e2fsck_journal_fix_bad_inode(ctx, pctx))
306                 return EXT2_ET_UNSUPP_FEATURE;
307
308         return 0;
309 }
310
311 static int e2fsck_journal_load(journal_t *journal)
312 {
313         e2fsck_t ctx = journal->j_dev;
314         journal_superblock_t *jsb;
315         struct buffer_head *jbh = journal->j_sb_buffer;
316         struct problem_context pctx;
317
318         clear_problem_context(&pctx);
319
320         ll_rw_block(READ, 1, &jbh);
321         if (jbh->b_err) {
322                 com_err(ctx->device_name, jbh->b_err,
323                         _("reading journal superblock\n"));
324                 return jbh->b_err;
325         }
326
327         jsb = journal->j_superblock;
328         /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
329         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
330                 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
331
332         switch (ntohl(jsb->s_header.h_blocktype)) {
333         case JFS_SUPERBLOCK_V1:
334                 journal->j_format_version = 1;
335                 break;
336                 
337         case JFS_SUPERBLOCK_V2:
338                 journal->j_format_version = 2;
339                 break;
340                 
341         /* If we don't understand the superblock major type, but there
342          * is a magic number, then it is likely to be a new format we
343          * just don't understand, so leave it alone. */
344         default:
345                 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
346                         _("%s: journal has unrecognised format\n"),
347                         ctx->device_name);
348                 return EXT2_ET_UNSUPP_FEATURE;
349         }
350
351         if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) {
352                 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
353                         _("%s: journal has incompatible features\n"),
354                         ctx->device_name);
355                 return EXT2_ET_UNSUPP_FEATURE;
356         }
357                 
358         if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) {
359                 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
360                         _("%s: journal has readonly-incompatible features\n"),
361                         ctx->device_name);
362                 return EXT2_ET_RO_UNSUPP_FEATURE;
363         }
364
365         /* We have now checked whether we know enough about the journal
366          * format to be able to proceed safely, so any other checks that
367          * fail we should attempt to recover from. */
368         if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
369                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
370                         _("%s: no valid journal superblock found\n"),
371                         ctx->device_name);
372                 return EXT2_ET_CORRUPT_SUPERBLOCK;
373         }
374
375         if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
376                 journal->j_maxlen = ntohl(jsb->s_maxlen);
377         else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
378                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
379                         _("%s: journal too short\n"),
380                         ctx->device_name);
381                 return EXT2_ET_CORRUPT_SUPERBLOCK;
382         }
383
384         journal->j_tail_sequence = ntohl(jsb->s_sequence);
385         journal->j_transaction_sequence = journal->j_tail_sequence;
386         journal->j_tail = ntohl(jsb->s_start);
387         journal->j_first = ntohl(jsb->s_first);
388         journal->j_last = ntohl(jsb->s_maxlen);
389
390         return 0;
391 }
392
393 void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
394                                 journal_t *journal)
395 {
396         char *p;
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         jsb->s_sequence = htonl(1);
417
418         /* In theory we should also re-zero the entire journal here.
419          * Initialising s_sequence to a random value would be a
420          * reasonable compromise. */
421
422         ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
423 }
424
425 static int e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, journal_t *journal,
426                                             struct problem_context *pctx)
427 {
428         struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
429         int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
430
431         pctx->num = journal->j_inode->i_ino;
432
433         if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
434                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
435                         journal_superblock_t *jsb = journal->j_superblock;
436
437                         e2fsck_journal_reset_super(ctx, jsb, journal);
438
439                         journal->j_transaction_sequence = 1;
440                         e2fsck_clear_recover(ctx, recover);
441                         return 0;
442                 }
443                 return EXT2_ET_CORRUPT_SUPERBLOCK;
444         } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
445                 return EXT2_ET_CORRUPT_SUPERBLOCK;
446
447         return 0;
448 }
449
450 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, int reset)
451 {
452         journal_superblock_t *jsb;
453
454         if (!(ctx->options & E2F_OPT_READONLY)) {
455                 jsb = journal->j_superblock;
456                 jsb->s_sequence = htonl(journal->j_transaction_sequence);
457                 if (reset)
458                         jsb->s_start = 0; /* this marks the journal as empty */
459                 mark_buffer_dirty(journal->j_sb_buffer, 1);
460         }
461         brelse(journal->j_sb_buffer);
462
463         if (journal->j_inode)
464                 free(journal->j_inode);
465         ext2fs_free_mem((void **)&journal);
466 }
467
468 int e2fsck_check_ext3_journal(e2fsck_t ctx)
469 {
470         struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
471         journal_t *journal;
472         int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
473         struct problem_context pctx;
474         int reset = 0;
475         int retval;
476
477         /* If we don't have any journal features, don't do anything more */
478         if (!(s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
479                 return 0;
480
481         clear_problem_context(&pctx);
482         pctx.num = s->s_journal_inum;
483
484         retval = e2fsck_get_journal(ctx, &journal);
485         if (retval) {
486                 if (retval == EXT2_ET_BAD_INODE_NUM)
487                         return e2fsck_journal_fix_bad_inode(ctx, &pctx);
488                 return retval;
489         }
490
491         retval = e2fsck_journal_load(journal);
492         if (retval) {
493                 if (retval == EXT2_ET_CORRUPT_SUPERBLOCK)
494                         return e2fsck_journal_fix_corrupt_super(ctx, journal,
495                                                                 &pctx);
496                 return retval;
497         }
498
499         /*
500          * We want to make the flags consistent here.  We will not leave with
501          * needs_recovery set but has_journal clear.  We can't get in a loop
502          * with -y, -n, or -p, only if a user isn't making up their mind.
503          */
504 no_has_journal:
505         if (!(s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
506                 recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
507                 pctx.str = "inode";
508                 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
509                         if (recover &&
510                             !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
511                                 goto no_has_journal;
512                         s->s_journal_inum = 0;
513                         e2fsck_clear_recover(ctx, recover);
514                 } else if (!(ctx->options & E2F_OPT_READONLY)) {
515                         s->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
516                         ext2fs_mark_super_dirty(ctx->fs);
517                 }
518         }
519
520         if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
521             !(s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
522             journal->j_superblock->s_start != 0) {
523                 if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx))
524                         reset = 1;
525                 /* I refuse to enable recovery for journal */
526         }
527
528         e2fsck_journal_release(ctx, journal, reset);
529         return retval;
530 }
531
532 static int recover_ext3_journal(e2fsck_t ctx)
533 {
534         ext2_filsys fs = ctx->fs;
535         journal_t *journal;
536         int retval;
537
538         retval = e2fsck_get_journal(ctx, &journal);
539         if (retval)
540                 return retval;
541
542         retval = e2fsck_journal_load(journal);
543         if (retval)
544                 return retval;
545
546         retval = journal_init_revoke(journal, 1024);
547         if (retval)
548                 return retval;
549         
550         retval = -journal_recover(journal);
551         e2fsck_journal_release(ctx, journal, 1);
552         return retval;
553 }
554
555
556 #if 0
557 #define TEMPLATE "/tmp/ext3.XXXXXX"
558
559 /*
560  * This function attempts to mount and unmount an ext3 filesystem,
561  * which is a cheap way to force the kernel to run the journal and
562  * handle the recovery for us.
563  */
564 static int recover_ext3_journal_via_mount(e2fsck_t ctx)
565 {
566         ext2_filsys fs = ctx->fs;
567         char    *dirlist[] = {"/mnt","/lost+found","/tmp","/root","/boot",0};
568         errcode_t        retval, retval2;
569         int      count = 0;
570         char     template[] = TEMPLATE;
571         struct stat buf;
572         char    *tmpdir;
573
574         if (ctx->options & E2F_OPT_READONLY) {
575                 printf("%s: won't do journal recovery while read-only\n",
576                        ctx->device_name);
577                 return EXT2_ET_FILE_RO;
578         }
579
580         printf(_("%s: trying for ext3 kernel journal recovery\n"),
581                ctx->device_name);
582         /*
583          * First try to make a temporary directory.  This may fail if
584          * the root partition is still mounted read-only.
585          */
586 newtemp:
587         tmpdir = mktemp(template);
588         if (tmpdir) {
589                 jfs_debug(2, "trying %s as ext3 temp mount point\n", tmpdir);
590                 if (mkdir(template, 0700)) {
591                         if (errno == EROFS) {
592                                 tmpdir = NULL;
593                                 template[0] = '\0';
594                         } else if (errno == EEXIST && count++ < 10) {
595                                 strcpy(template, TEMPLATE);
596                                 goto newtemp;
597                         }
598                         return errno;
599                 }
600         }
601
602         /*
603          * OK, creating a temporary directory didn't work.
604          * Let's try a list of possible temporary mountpoints.
605          */
606         if (!tmpdir) {
607                 dev_t   rootdev;
608                 char    **cpp, *dir;
609
610                 if (stat("/", &buf))
611                         return errno;
612
613                 rootdev = buf.st_dev;
614
615                 /*
616                  * Check that dir is on the same device as root (no other
617                  * filesystem is mounted there), and it's a directory.
618                  */
619                 for (cpp = dirlist; (dir = *cpp); cpp++)
620                         if (stat(dir, &buf) == 0 && buf.st_dev == rootdev &&
621                             S_ISDIR(buf.st_mode)) {
622                                 tmpdir = dir;
623                                 break;
624                         }
625         }
626
627         if (tmpdir) {
628                 io_manager      io_ptr = fs->io->manager;
629                 int             blocksize = fs->blocksize;
630
631                 jfs_debug(2, "using %s for ext3 mount\n", tmpdir);
632                 /* FIXME - need to handle loop devices here */
633                 if (mount(ctx->device_name, tmpdir, "ext3", MNT_FL, NULL)) {
634                         retval = errno;
635                         com_err(ctx->program_name, errno,
636                                 "when mounting %s", ctx->device_name);
637                         if (template[0])
638                                 rmdir(tmpdir);
639                         return retval;
640                 }
641                 /*
642                  * Now that it mounted cleanly, the filesystem will have been
643                  * recovered, so we can now unmount it.
644                  */
645                 if (umount(tmpdir))
646                         return errno;
647
648                 /*
649                  * Remove the temporary directory, if it was created.
650                  */
651                 if (template[0])
652                         rmdir(tmpdir);
653                 return 0;
654         }
655 }
656 #endif
657
658 int e2fsck_run_ext3_journal(e2fsck_t ctx)
659 {
660         io_manager io_ptr = ctx->fs->io->manager;
661         int blocksize = ctx->fs->blocksize;
662         errcode_t       retval, recover_retval;
663
664         printf(_("%s: recovering journal\n"), ctx->device_name);
665         if (ctx->options & E2F_OPT_READONLY) {
666                 printf(_("%s: won't do journal recovery while read-only\n"),
667                        ctx->device_name);
668                 return EXT2_ET_FILE_RO;
669         }
670
671         recover_retval = recover_ext3_journal(ctx);
672         
673         /*
674          * Reload the filesystem context to get up-to-date data from disk
675          * because journal recovery will change the filesystem under us.
676          */
677         ext2fs_close(ctx->fs);
678         retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
679                              ctx->superblock, blocksize, io_ptr,
680                              &ctx->fs);
681
682         if (retval) {
683                 com_err(ctx->program_name, retval,
684                         _("while trying to re-open %s"),
685                         ctx->device_name);
686                 fatal_error(ctx, 0);
687         }
688         ctx->fs->priv_data = ctx;
689
690         /* Set the superblock flags */
691         e2fsck_clear_recover(ctx, recover_retval);
692         return recover_retval;
693 }