Whamcloud - gitweb
Merge branch 'maint' into next
[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 #include "config.h"
16 #ifdef HAVE_SYS_MOUNT_H
17 #include <sys/param.h>
18 #include <sys/mount.h>
19 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
20 #endif
21 #ifdef HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24
25 #define E2FSCK_INCLUDE_INLINE_FUNCS
26 #include "jfs_user.h"
27 #include "problem.h"
28 #include "uuid/uuid.h"
29
30 #ifdef CONFIG_JBD_DEBUG         /* Enabled by configure --enable-jfs-debug */
31 static int bh_count = 0;
32 #endif
33
34 /*
35  * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
36  * This creates a larger static binary, and a smaller binary using
37  * shared libraries.  It's also probably slightly less CPU-efficient,
38  * which is why it's not on by default.  But, it's a good way of
39  * testing the functions in inode_io.c and fileio.c.
40  */
41 #undef USE_INODE_IO
42
43 /* Checksumming functions */
44 static int e2fsck_journal_verify_csum_type(journal_t *j,
45                                            journal_superblock_t *jsb)
46 {
47         if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
48                 return 1;
49
50         return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM;
51 }
52
53 static __u32 e2fsck_journal_sb_csum(journal_superblock_t *jsb)
54 {
55         __u32 crc, old_crc;
56
57         old_crc = jsb->s_checksum;
58         jsb->s_checksum = 0;
59         crc = ext2fs_crc32c_le(~0, (unsigned char *)jsb,
60                                sizeof(journal_superblock_t));
61         jsb->s_checksum = old_crc;
62
63         return crc;
64 }
65
66 static int e2fsck_journal_sb_csum_verify(journal_t *j,
67                                          journal_superblock_t *jsb)
68 {
69         __u32 provided, calculated;
70
71         if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
72                 return 1;
73
74         provided = ext2fs_be32_to_cpu(jsb->s_checksum);
75         calculated = e2fsck_journal_sb_csum(jsb);
76
77         return provided == calculated;
78 }
79
80 static errcode_t e2fsck_journal_sb_csum_set(journal_t *j,
81                                             journal_superblock_t *jsb)
82 {
83         __u32 crc;
84
85         if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
86                 return 0;
87
88         crc = e2fsck_journal_sb_csum(jsb);
89         jsb->s_checksum = ext2fs_cpu_to_be32(crc);
90         return 0;
91 }
92
93 /* Kernel compatibility functions for handling the journal.  These allow us
94  * to use the recovery.c file virtually unchanged from the kernel, so we
95  * don't have to do much to keep kernel and user recovery in sync.
96  */
97 int journal_bmap(journal_t *journal, blk64_t block, unsigned long long *phys)
98 {
99 #ifdef USE_INODE_IO
100         *phys = block;
101         return 0;
102 #else
103         struct inode    *inode = journal->j_inode;
104         errcode_t       retval;
105         blk64_t         pblk;
106
107         if (!inode) {
108                 *phys = block;
109                 return 0;
110         }
111
112         retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino,
113                              &inode->i_ext2, NULL, 0, block, 0, &pblk);
114         *phys = pblk;
115         return (retval);
116 #endif
117 }
118
119 struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize)
120 {
121         struct buffer_head *bh;
122         int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
123                 sizeof(bh->b_data);
124
125         bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
126         if (!bh)
127                 return NULL;
128
129 #ifdef CONFIG_JBD_DEBUG
130         if (journal_enable_debug >= 3)
131                 bh_count++;
132 #endif
133         jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
134                   (unsigned long long) blocknr, blocksize, bh_count);
135
136         bh->b_ctx = kdev->k_ctx;
137         if (kdev->k_dev == K_DEV_FS)
138                 bh->b_io = kdev->k_ctx->fs->io;
139         else
140                 bh->b_io = kdev->k_ctx->journal_io;
141         bh->b_size = blocksize;
142         bh->b_blocknr = blocknr;
143
144         return bh;
145 }
146
147 void sync_blockdev(kdev_t kdev)
148 {
149         io_channel      io;
150
151         if (kdev->k_dev == K_DEV_FS)
152                 io = kdev->k_ctx->fs->io;
153         else
154                 io = kdev->k_ctx->journal_io;
155
156         io_channel_flush(io);
157 }
158
159 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
160 {
161         int retval;
162         struct buffer_head *bh;
163
164         for (; nr > 0; --nr) {
165                 bh = *bhp++;
166                 if (rw == READ && !bh->b_uptodate) {
167                         jfs_debug(3, "reading block %llu/%p\n",
168                                   bh->b_blocknr, (void *) bh);
169                         retval = io_channel_read_blk64(bh->b_io,
170                                                      bh->b_blocknr,
171                                                      1, bh->b_data);
172                         if (retval) {
173                                 com_err(bh->b_ctx->device_name, retval,
174                                         "while reading block %llu\n",
175                                         bh->b_blocknr);
176                                 bh->b_err = retval;
177                                 continue;
178                         }
179                         bh->b_uptodate = 1;
180                 } else if (rw == WRITE && bh->b_dirty) {
181                         jfs_debug(3, "writing block %llu/%p\n",
182                                   bh->b_blocknr,
183                                   (void *) bh);
184                         retval = io_channel_write_blk64(bh->b_io,
185                                                       bh->b_blocknr,
186                                                       1, bh->b_data);
187                         if (retval) {
188                                 com_err(bh->b_ctx->device_name, retval,
189                                         "while writing block %llu\n",
190                                         bh->b_blocknr);
191                                 bh->b_err = retval;
192                                 continue;
193                         }
194                         bh->b_dirty = 0;
195                         bh->b_uptodate = 1;
196                 } else {
197                         jfs_debug(3, "no-op %s for block %llu\n",
198                                   rw == READ ? "read" : "write",
199                                   bh->b_blocknr);
200                 }
201         }
202 }
203
204 void mark_buffer_dirty(struct buffer_head *bh)
205 {
206         bh->b_dirty = 1;
207 }
208
209 static void mark_buffer_clean(struct buffer_head * bh)
210 {
211         bh->b_dirty = 0;
212 }
213
214 void brelse(struct buffer_head *bh)
215 {
216         if (bh->b_dirty)
217                 ll_rw_block(WRITE, 1, &bh);
218         jfs_debug(3, "freeing block %llu/%p (total %d)\n",
219                   bh->b_blocknr, (void *) bh, --bh_count);
220         ext2fs_free_mem(&bh);
221 }
222
223 int buffer_uptodate(struct buffer_head *bh)
224 {
225         return bh->b_uptodate;
226 }
227
228 void mark_buffer_uptodate(struct buffer_head *bh, int val)
229 {
230         bh->b_uptodate = val;
231 }
232
233 void wait_on_buffer(struct buffer_head *bh)
234 {
235         if (!bh->b_uptodate)
236                 ll_rw_block(READ, 1, &bh);
237 }
238
239
240 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
241 {
242         ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
243
244         /* if we had an error doing journal recovery, we need a full fsck */
245         if (error)
246                 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
247         ext2fs_mark_super_dirty(ctx->fs);
248 }
249
250 /*
251  * This is a helper function to check the validity of the journal.
252  */
253 struct process_block_struct {
254         e2_blkcnt_t     last_block;
255 };
256
257 static int process_journal_block(ext2_filsys fs,
258                                  blk64_t        *block_nr,
259                                  e2_blkcnt_t blockcnt,
260                                  blk64_t ref_block EXT2FS_ATTR((unused)),
261                                  int ref_offset EXT2FS_ATTR((unused)),
262                                  void *priv_data)
263 {
264         struct process_block_struct *p;
265         blk64_t blk = *block_nr;
266
267         p = (struct process_block_struct *) priv_data;
268
269         if (!blk || blk < fs->super->s_first_data_block ||
270             blk >= ext2fs_blocks_count(fs->super))
271                 return BLOCK_ABORT;
272
273         if (blockcnt >= 0)
274                 p->last_block = blockcnt;
275         return 0;
276 }
277
278 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
279 {
280         struct process_block_struct pb;
281         struct ext2_super_block *sb = ctx->fs->super;
282         struct ext2_super_block jsuper;
283         struct problem_context  pctx;
284         struct buffer_head      *bh;
285         struct inode            *j_inode = NULL;
286         struct kdev_s           *dev_fs = NULL, *dev_journal;
287         const char              *journal_name = 0;
288         journal_t               *journal = NULL;
289         errcode_t               retval = 0;
290         io_manager              io_ptr = 0;
291         unsigned long long      start = 0;
292         int                     ext_journal = 0;
293         int                     tried_backup_jnl = 0;
294         blk64_t                 maxlen;
295
296         clear_problem_context(&pctx);
297
298         journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
299         if (!journal) {
300                 return EXT2_ET_NO_MEMORY;
301         }
302
303         dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
304         if (!dev_fs) {
305                 retval = EXT2_ET_NO_MEMORY;
306                 goto errout;
307         }
308         dev_journal = dev_fs+1;
309
310         dev_fs->k_ctx = dev_journal->k_ctx = ctx;
311         dev_fs->k_dev = K_DEV_FS;
312         dev_journal->k_dev = K_DEV_JOURNAL;
313
314         journal->j_dev = dev_journal;
315         journal->j_fs_dev = dev_fs;
316         journal->j_inode = NULL;
317         journal->j_blocksize = ctx->fs->blocksize;
318
319         if (uuid_is_null(sb->s_journal_uuid)) {
320                 if (!sb->s_journal_inum) {
321                         retval = EXT2_ET_BAD_INODE_NUM;
322                         goto errout;
323                 }
324                 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
325                                                  "journal inode");
326                 if (!j_inode) {
327                         retval = EXT2_ET_NO_MEMORY;
328                         goto errout;
329                 }
330
331                 j_inode->i_ctx = ctx;
332                 j_inode->i_ino = sb->s_journal_inum;
333
334                 if ((retval = ext2fs_read_inode(ctx->fs,
335                                                 sb->s_journal_inum,
336                                                 &j_inode->i_ext2))) {
337                 try_backup_journal:
338                         if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
339                             tried_backup_jnl)
340                                 goto errout;
341                         memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
342                         memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
343                                EXT2_N_BLOCKS*4);
344                         j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
345                         j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
346                         j_inode->i_ext2.i_links_count = 1;
347                         j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
348                         e2fsck_use_inode_shortcuts(ctx, 1);
349                         ctx->stashed_ino = j_inode->i_ino;
350                         ctx->stashed_inode = &j_inode->i_ext2;
351                         tried_backup_jnl++;
352                 }
353                 if (!j_inode->i_ext2.i_links_count ||
354                     !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
355                         retval = EXT2_ET_NO_JOURNAL;
356                         goto try_backup_journal;
357                 }
358                 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
359                     JFS_MIN_JOURNAL_BLOCKS) {
360                         retval = EXT2_ET_JOURNAL_TOO_SMALL;
361                         goto try_backup_journal;
362                 }
363                 pb.last_block = -1;
364                 retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
365                                                BLOCK_FLAG_HOLE, 0,
366                                                process_journal_block, &pb);
367                 if ((pb.last_block + 1) * ctx->fs->blocksize <
368                     (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
369                         retval = EXT2_ET_JOURNAL_TOO_SMALL;
370                         goto try_backup_journal;
371                 }
372                 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
373                         retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
374                                                     &j_inode->i_ext2);
375                         if (retval)
376                                 goto errout;
377                 }
378
379                 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
380                         journal->j_blocksize;
381
382 #ifdef USE_INODE_IO
383                 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
384                                                  &j_inode->i_ext2,
385                                                  &journal_name);
386                 if (retval)
387                         goto errout;
388
389                 io_ptr = inode_io_manager;
390 #else
391                 journal->j_inode = j_inode;
392                 ctx->journal_io = ctx->fs->io;
393                 if ((retval = journal_bmap(journal, 0, &start)) != 0)
394                         goto errout;
395 #endif
396         } else {
397                 ext_journal = 1;
398                 if (!ctx->journal_name) {
399                         char uuid[37];
400
401                         uuid_unparse(sb->s_journal_uuid, uuid);
402                         ctx->journal_name = blkid_get_devname(ctx->blkid,
403                                                               "UUID", uuid);
404                         if (!ctx->journal_name)
405                                 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
406                 }
407                 journal_name = ctx->journal_name;
408
409                 if (!journal_name) {
410                         fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
411                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
412                         goto errout;
413                 }
414
415                 jfs_debug(1, "Using journal file %s\n", journal_name);
416                 io_ptr = unix_io_manager;
417         }
418
419 #if 0
420         test_io_backing_manager = io_ptr;
421         io_ptr = test_io_manager;
422 #endif
423 #ifndef USE_INODE_IO
424         if (ext_journal)
425 #endif
426         {
427                 int flags = IO_FLAG_RW;
428                 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
429                       ctx->mount_flags & EXT2_MF_READONLY))
430                         flags |= IO_FLAG_EXCLUSIVE;
431                 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
432                     (ctx->options & E2F_OPT_FORCE))
433                         flags &= ~IO_FLAG_EXCLUSIVE;
434
435
436                 retval = io_ptr->open(journal_name, flags,
437                                       &ctx->journal_io);
438         }
439         if (retval)
440                 goto errout;
441
442         io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
443
444         if (ext_journal) {
445                 if (ctx->fs->blocksize == 1024)
446                         start = 1;
447                 bh = getblk(dev_journal, start, ctx->fs->blocksize);
448                 if (!bh) {
449                         retval = EXT2_ET_NO_MEMORY;
450                         goto errout;
451                 }
452                 ll_rw_block(READ, 1, &bh);
453                 if ((retval = bh->b_err) != 0) {
454                         brelse(bh);
455                         goto errout;
456                 }
457                 memcpy(&jsuper, start ? bh->b_data :  bh->b_data + 1024,
458                        sizeof(jsuper));
459                 brelse(bh);
460 #ifdef WORDS_BIGENDIAN
461                 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
462                         ext2fs_swap_super(&jsuper);
463 #endif
464                 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
465                     !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
466                         fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
467                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
468                         goto errout;
469                 }
470                 /* Make sure the journal UUID is correct */
471                 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
472                            sizeof(jsuper.s_uuid))) {
473                         fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
474                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
475                         goto errout;
476                 }
477
478                 maxlen = ext2fs_blocks_count(&jsuper);
479                 if (maxlen > 1ULL << 32)
480                         maxlen = (1ULL << 32) - 1;
481                 journal->j_maxlen = maxlen;
482                 start++;
483         }
484
485         if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
486                 retval = EXT2_ET_NO_MEMORY;
487                 goto errout;
488         }
489
490         journal->j_sb_buffer = bh;
491         journal->j_superblock = (journal_superblock_t *)bh->b_data;
492
493 #ifdef USE_INODE_IO
494         if (j_inode)
495                 ext2fs_free_mem(&j_inode);
496 #endif
497
498         *ret_journal = journal;
499         e2fsck_use_inode_shortcuts(ctx, 0);
500         return 0;
501
502 errout:
503         e2fsck_use_inode_shortcuts(ctx, 0);
504         if (dev_fs)
505                 ext2fs_free_mem(&dev_fs);
506         if (j_inode)
507                 ext2fs_free_mem(&j_inode);
508         if (journal)
509                 ext2fs_free_mem(&journal);
510         return retval;
511 }
512
513 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
514                                               struct problem_context *pctx)
515 {
516         struct ext2_super_block *sb = ctx->fs->super;
517         int recover = ctx->fs->super->s_feature_incompat &
518                 EXT3_FEATURE_INCOMPAT_RECOVER;
519         int has_journal = ctx->fs->super->s_feature_compat &
520                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
521
522         if (has_journal || sb->s_journal_inum) {
523                 /* The journal inode is bogus, remove and force full fsck */
524                 pctx->ino = sb->s_journal_inum;
525                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
526                         if (has_journal && sb->s_journal_inum)
527                                 printf("*** ext3 journal has been deleted - "
528                                        "filesystem is now ext2 only ***\n\n");
529                         sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
530                         sb->s_journal_inum = 0;
531                         ctx->flags |= E2F_FLAG_JOURNAL_INODE;
532                         ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
533                         e2fsck_clear_recover(ctx, 1);
534                         return 0;
535                 }
536                 return EXT2_ET_BAD_INODE_NUM;
537         } else if (recover) {
538                 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
539                         e2fsck_clear_recover(ctx, 1);
540                         return 0;
541                 }
542                 return EXT2_ET_UNSUPP_FEATURE;
543         }
544         return 0;
545 }
546
547 #define V1_SB_SIZE      0x0024
548 static void clear_v2_journal_fields(journal_t *journal)
549 {
550         e2fsck_t ctx = journal->j_dev->k_ctx;
551         struct problem_context pctx;
552
553         clear_problem_context(&pctx);
554
555         if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
556                 return;
557
558         memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
559                ctx->fs->blocksize-V1_SB_SIZE);
560         mark_buffer_dirty(journal->j_sb_buffer);
561 }
562
563
564 static errcode_t e2fsck_journal_load(journal_t *journal)
565 {
566         e2fsck_t ctx = journal->j_dev->k_ctx;
567         journal_superblock_t *jsb;
568         struct buffer_head *jbh = journal->j_sb_buffer;
569         struct problem_context pctx;
570
571         clear_problem_context(&pctx);
572
573         ll_rw_block(READ, 1, &jbh);
574         if (jbh->b_err) {
575                 com_err(ctx->device_name, jbh->b_err,
576                         _("reading journal superblock\n"));
577                 return jbh->b_err;
578         }
579
580         jsb = journal->j_superblock;
581         /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
582         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
583                 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
584
585         switch (ntohl(jsb->s_header.h_blocktype)) {
586         case JFS_SUPERBLOCK_V1:
587                 journal->j_format_version = 1;
588                 if (jsb->s_feature_compat ||
589                     jsb->s_feature_incompat ||
590                     jsb->s_feature_ro_compat ||
591                     jsb->s_nr_users)
592                         clear_v2_journal_fields(journal);
593                 break;
594
595         case JFS_SUPERBLOCK_V2:
596                 journal->j_format_version = 2;
597                 if (ntohl(jsb->s_nr_users) > 1 &&
598                     uuid_is_null(ctx->fs->super->s_journal_uuid))
599                         clear_v2_journal_fields(journal);
600                 if (ntohl(jsb->s_nr_users) > 1) {
601                         fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
602                         return EXT2_ET_JOURNAL_UNSUPP_VERSION;
603                 }
604                 break;
605
606         /*
607          * These should never appear in a journal super block, so if
608          * they do, the journal is badly corrupted.
609          */
610         case JFS_DESCRIPTOR_BLOCK:
611         case JFS_COMMIT_BLOCK:
612         case JFS_REVOKE_BLOCK:
613                 return EXT2_ET_CORRUPT_SUPERBLOCK;
614
615         /* If we don't understand the superblock major type, but there
616          * is a magic number, then it is likely to be a new format we
617          * just don't understand, so leave it alone. */
618         default:
619                 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
620         }
621
622         if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
623                 return EXT2_ET_UNSUPP_FEATURE;
624
625         if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
626                 return EXT2_ET_RO_UNSUPP_FEATURE;
627
628         /* Checksum v1 and v2 are mutually exclusive features. */
629         if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V2) &&
630             JFS_HAS_COMPAT_FEATURE(journal, JFS_FEATURE_COMPAT_CHECKSUM))
631                 return EXT2_ET_CORRUPT_SUPERBLOCK;
632
633         if (!e2fsck_journal_verify_csum_type(journal, jsb) ||
634             !e2fsck_journal_sb_csum_verify(journal, jsb))
635                 return EXT2_ET_CORRUPT_SUPERBLOCK;
636
637         /* We have now checked whether we know enough about the journal
638          * format to be able to proceed safely, so any other checks that
639          * fail we should attempt to recover from. */
640         if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
641                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
642                         _("%s: no valid journal superblock found\n"),
643                         ctx->device_name);
644                 return EXT2_ET_CORRUPT_SUPERBLOCK;
645         }
646
647         if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
648                 journal->j_maxlen = ntohl(jsb->s_maxlen);
649         else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
650                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
651                         _("%s: journal too short\n"),
652                         ctx->device_name);
653                 return EXT2_ET_CORRUPT_SUPERBLOCK;
654         }
655
656         journal->j_tail_sequence = ntohl(jsb->s_sequence);
657         journal->j_transaction_sequence = journal->j_tail_sequence;
658         journal->j_tail = ntohl(jsb->s_start);
659         journal->j_first = ntohl(jsb->s_first);
660         journal->j_last = ntohl(jsb->s_maxlen);
661
662         return 0;
663 }
664
665 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
666                                        journal_t *journal)
667 {
668         char *p;
669         union {
670                 uuid_t uuid;
671                 __u32 val[4];
672         } u;
673         __u32 new_seq = 0;
674         int i;
675
676         /* Leave a valid existing V1 superblock signature alone.
677          * Anything unrecognisable we overwrite with a new V2
678          * signature. */
679
680         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
681             jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
682                 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
683                 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
684         }
685
686         /* Zero out everything else beyond the superblock header */
687
688         p = ((char *) jsb) + sizeof(journal_header_t);
689         memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
690
691         jsb->s_blocksize = htonl(ctx->fs->blocksize);
692         jsb->s_maxlen = htonl(journal->j_maxlen);
693         jsb->s_first = htonl(1);
694
695         /* Initialize the journal sequence number so that there is "no"
696          * chance we will find old "valid" transactions in the journal.
697          * This avoids the need to zero the whole journal (slow to do,
698          * and risky when we are just recovering the filesystem).
699          */
700         uuid_generate(u.uuid);
701         for (i = 0; i < 4; i ++)
702                 new_seq ^= u.val[i];
703         jsb->s_sequence = htonl(new_seq);
704         e2fsck_journal_sb_csum_set(journal, jsb);
705
706         mark_buffer_dirty(journal->j_sb_buffer);
707         ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
708 }
709
710 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
711                                                   journal_t *journal,
712                                                   struct problem_context *pctx)
713 {
714         struct ext2_super_block *sb = ctx->fs->super;
715         int recover = ctx->fs->super->s_feature_incompat &
716                 EXT3_FEATURE_INCOMPAT_RECOVER;
717
718         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
719                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
720                         e2fsck_journal_reset_super(ctx, journal->j_superblock,
721                                                    journal);
722                         journal->j_transaction_sequence = 1;
723                         e2fsck_clear_recover(ctx, recover);
724                         return 0;
725                 }
726                 return EXT2_ET_CORRUPT_SUPERBLOCK;
727         } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
728                 return EXT2_ET_CORRUPT_SUPERBLOCK;
729
730         return 0;
731 }
732
733 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
734                                    int reset, int drop)
735 {
736         journal_superblock_t *jsb;
737
738         if (drop)
739                 mark_buffer_clean(journal->j_sb_buffer);
740         else if (!(ctx->options & E2F_OPT_READONLY)) {
741                 jsb = journal->j_superblock;
742                 jsb->s_sequence = htonl(journal->j_transaction_sequence);
743                 if (reset)
744                         jsb->s_start = 0; /* this marks the journal as empty */
745                 e2fsck_journal_sb_csum_set(journal, jsb);
746                 mark_buffer_dirty(journal->j_sb_buffer);
747         }
748         brelse(journal->j_sb_buffer);
749
750         if (ctx->journal_io) {
751                 if (ctx->fs && ctx->fs->io != ctx->journal_io)
752                         io_channel_close(ctx->journal_io);
753                 ctx->journal_io = 0;
754         }
755
756 #ifndef USE_INODE_IO
757         if (journal->j_inode)
758                 ext2fs_free_mem(&journal->j_inode);
759 #endif
760         if (journal->j_fs_dev)
761                 ext2fs_free_mem(&journal->j_fs_dev);
762         ext2fs_free_mem(&journal);
763 }
764
765 /*
766  * This function makes sure that the superblock fields regarding the
767  * journal are consistent.
768  */
769 int e2fsck_check_ext3_journal(e2fsck_t ctx)
770 {
771         struct ext2_super_block *sb = ctx->fs->super;
772         journal_t *journal;
773         int recover = ctx->fs->super->s_feature_incompat &
774                 EXT3_FEATURE_INCOMPAT_RECOVER;
775         struct problem_context pctx;
776         problem_t problem;
777         int reset = 0, force_fsck = 0;
778         int retval;
779
780         /* If we don't have any journal features, don't do anything more */
781         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
782             !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
783             uuid_is_null(sb->s_journal_uuid))
784                 return 0;
785
786         clear_problem_context(&pctx);
787         pctx.num = sb->s_journal_inum;
788
789         retval = e2fsck_get_journal(ctx, &journal);
790         if (retval) {
791                 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
792                     (retval == EXT2_ET_BAD_BLOCK_NUM) ||
793                     (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
794                     (retval == EXT2_ET_NO_JOURNAL))
795                         return e2fsck_journal_fix_bad_inode(ctx, &pctx);
796                 return retval;
797         }
798
799         retval = e2fsck_journal_load(journal);
800         if (retval) {
801                 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
802                     ((retval == EXT2_ET_UNSUPP_FEATURE) &&
803                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
804                                   &pctx))) ||
805                     ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
806                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
807                                   &pctx))) ||
808                     ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
809                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
810                         retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
811                                                                   &pctx);
812                 e2fsck_journal_release(ctx, journal, 0, 1);
813                 return retval;
814         }
815
816         /*
817          * We want to make the flags consistent here.  We will not leave with
818          * needs_recovery set but has_journal clear.  We can't get in a loop
819          * with -y, -n, or -p, only if a user isn't making up their mind.
820          */
821 no_has_journal:
822         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
823                 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
824                 pctx.str = "inode";
825                 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
826                         if (recover &&
827                             !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
828                                 goto no_has_journal;
829                         /*
830                          * Need a full fsck if we are releasing a
831                          * journal stored on a reserved inode.
832                          */
833                         force_fsck = recover ||
834                                 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
835                         /* Clear all of the journal fields */
836                         sb->s_journal_inum = 0;
837                         sb->s_journal_dev = 0;
838                         memset(sb->s_journal_uuid, 0,
839                                sizeof(sb->s_journal_uuid));
840                         e2fsck_clear_recover(ctx, force_fsck);
841                 } else if (!(ctx->options & E2F_OPT_READONLY)) {
842                         sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
843                         ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
844                         ext2fs_mark_super_dirty(ctx->fs);
845                 }
846         }
847
848         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
849             !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
850             journal->j_superblock->s_start != 0) {
851                 /* Print status information */
852                 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
853                 if (ctx->superblock)
854                         problem = PR_0_JOURNAL_RUN_DEFAULT;
855                 else
856                         problem = PR_0_JOURNAL_RUN;
857                 if (fix_problem(ctx, problem, &pctx)) {
858                         ctx->options |= E2F_OPT_FORCE;
859                         sb->s_feature_incompat |=
860                                 EXT3_FEATURE_INCOMPAT_RECOVER;
861                         ext2fs_mark_super_dirty(ctx->fs);
862                 } else if (fix_problem(ctx,
863                                        PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
864                         reset = 1;
865                         sb->s_state &= ~EXT2_VALID_FS;
866                         ext2fs_mark_super_dirty(ctx->fs);
867                 }
868                 /*
869                  * If the user answers no to the above question, we
870                  * ignore the fact that journal apparently has data;
871                  * accidentally replaying over valid data would be far
872                  * worse than skipping a questionable recovery.
873                  *
874                  * XXX should we abort with a fatal error here?  What
875                  * will the ext3 kernel code do if a filesystem with
876                  * !NEEDS_RECOVERY but with a non-zero
877                  * journal->j_superblock->s_start is mounted?
878                  */
879         }
880
881         /*
882          * If we don't need to do replay the journal, check to see if
883          * the journal's errno is set; if so, we need to mark the file
884          * system as being corrupt and clear the journal's s_errno.
885          */
886         if (!(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
887             journal->j_superblock->s_errno) {
888                 ctx->fs->super->s_state |= EXT2_ERROR_FS;
889                 ext2fs_mark_super_dirty(ctx->fs);
890                 journal->j_superblock->s_errno = 0;
891                 e2fsck_journal_sb_csum_set(journal, journal->j_superblock);
892                 mark_buffer_dirty(journal->j_sb_buffer);
893         }
894
895         e2fsck_journal_release(ctx, journal, reset, 0);
896         return retval;
897 }
898
899 static errcode_t recover_ext3_journal(e2fsck_t ctx)
900 {
901         struct problem_context  pctx;
902         journal_t *journal;
903         int retval;
904
905         clear_problem_context(&pctx);
906
907         journal_init_revoke_caches();
908         retval = e2fsck_get_journal(ctx, &journal);
909         if (retval)
910                 return retval;
911
912         retval = e2fsck_journal_load(journal);
913         if (retval)
914                 goto errout;
915
916         retval = journal_init_revoke(journal, 1024);
917         if (retval)
918                 goto errout;
919
920         retval = -journal_recover(journal);
921         if (retval)
922                 goto errout;
923
924         if (journal->j_failed_commit) {
925                 pctx.ino = journal->j_failed_commit;
926                 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
927                 journal->j_superblock->s_errno = -EINVAL;
928                 mark_buffer_dirty(journal->j_sb_buffer);
929         }
930
931 errout:
932         journal_destroy_revoke(journal);
933         journal_destroy_revoke_caches();
934         e2fsck_journal_release(ctx, journal, 1, 0);
935         return retval;
936 }
937
938 int e2fsck_run_ext3_journal(e2fsck_t ctx)
939 {
940         io_manager io_ptr = ctx->fs->io->manager;
941         int blocksize = ctx->fs->blocksize;
942         errcode_t       retval, recover_retval;
943         io_stats        stats = 0;
944         unsigned long long kbytes_written = 0;
945
946         printf(_("%s: recovering journal\n"), ctx->device_name);
947         if (ctx->options & E2F_OPT_READONLY) {
948                 printf(_("%s: won't do journal recovery while read-only\n"),
949                        ctx->device_name);
950                 return EXT2_ET_FILE_RO;
951         }
952
953         if (ctx->fs->flags & EXT2_FLAG_DIRTY)
954                 ext2fs_flush(ctx->fs);  /* Force out any modifications */
955
956         recover_retval = recover_ext3_journal(ctx);
957
958         /*
959          * Reload the filesystem context to get up-to-date data from disk
960          * because journal recovery will change the filesystem under us.
961          */
962         if (ctx->fs->super->s_kbytes_written &&
963             ctx->fs->io->manager->get_stats)
964                 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
965         if (stats && stats->bytes_written)
966                 kbytes_written = stats->bytes_written >> 10;
967
968         ext2fs_mmp_stop(ctx->fs);
969         ext2fs_free(ctx->fs);
970         retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
971                              ctx->superblock, blocksize, io_ptr,
972                              &ctx->fs);
973         if (retval) {
974                 com_err(ctx->program_name, retval,
975                         _("while trying to re-open %s"),
976                         ctx->device_name);
977                 fatal_error(ctx, 0);
978         }
979         ctx->fs->priv_data = ctx;
980         ctx->fs->now = ctx->now;
981         ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
982         ctx->fs->super->s_kbytes_written += kbytes_written;
983
984         /* Set the superblock flags */
985         e2fsck_clear_recover(ctx, recover_retval);
986
987         /*
988          * Do one last sanity check, and propagate journal->s_errno to
989          * the EXT2_ERROR_FS flag in the fs superblock if needed.
990          */
991         retval = e2fsck_check_ext3_journal(ctx);
992         return retval ? retval : recover_retval;
993 }
994
995 /*
996  * This function will move the journal inode from a visible file in
997  * the filesystem directory hierarchy to the reserved inode if necessary.
998  */
999 static const char * const journal_names[] = {
1000         ".journal", "journal", ".journal.dat", "journal.dat", 0 };
1001
1002 void e2fsck_move_ext3_journal(e2fsck_t ctx)
1003 {
1004         struct ext2_super_block *sb = ctx->fs->super;
1005         struct problem_context  pctx;
1006         struct ext2_inode       inode;
1007         ext2_filsys             fs = ctx->fs;
1008         ext2_ino_t              ino;
1009         errcode_t               retval;
1010         const char * const *    cpp;
1011         int                     group, mount_flags;
1012
1013         clear_problem_context(&pctx);
1014
1015         /*
1016          * If the filesystem is opened read-only, or there is no
1017          * journal, then do nothing.
1018          */
1019         if ((ctx->options & E2F_OPT_READONLY) ||
1020             (sb->s_journal_inum == 0) ||
1021             !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1022                 return;
1023
1024         /*
1025          * Read in the journal inode
1026          */
1027         if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
1028                 return;
1029
1030         /*
1031          * If it's necessary to backup the journal inode, do so.
1032          */
1033         if ((sb->s_jnl_backup_type == 0) ||
1034             ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
1035              memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
1036                 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
1037                         memcpy(sb->s_jnl_blocks, inode.i_block,
1038                                EXT2_N_BLOCKS*4);
1039                         sb->s_jnl_blocks[15] = inode.i_size_high;
1040                         sb->s_jnl_blocks[16] = inode.i_size;
1041                         sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1042                         ext2fs_mark_super_dirty(fs);
1043                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1044                 }
1045         }
1046
1047         /*
1048          * If the journal is already the hidden inode, then do nothing
1049          */
1050         if (sb->s_journal_inum == EXT2_JOURNAL_INO)
1051                 return;
1052
1053         /*
1054          * The journal inode had better have only one link and not be readable.
1055          */
1056         if (inode.i_links_count != 1)
1057                 return;
1058
1059         /*
1060          * If the filesystem is mounted, or we can't tell whether
1061          * or not it's mounted, do nothing.
1062          */
1063         retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
1064         if (retval || (mount_flags & EXT2_MF_MOUNTED))
1065                 return;
1066
1067         /*
1068          * If we can't find the name of the journal inode, then do
1069          * nothing.
1070          */
1071         for (cpp = journal_names; *cpp; cpp++) {
1072                 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
1073                                        strlen(*cpp), 0, &ino);
1074                 if ((retval == 0) && (ino == sb->s_journal_inum))
1075                         break;
1076         }
1077         if (*cpp == 0)
1078                 return;
1079
1080         /* We need the inode bitmap to be loaded */
1081         retval = ext2fs_read_bitmaps(fs);
1082         if (retval)
1083                 return;
1084
1085         pctx.str = *cpp;
1086         if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
1087                 return;
1088
1089         /*
1090          * OK, we've done all the checks, let's actually move the
1091          * journal inode.  Errors at this point mean we need to force
1092          * an ext2 filesystem check.
1093          */
1094         if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1095                 goto err_out;
1096         if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1097                 goto err_out;
1098         sb->s_journal_inum = EXT2_JOURNAL_INO;
1099         ext2fs_mark_super_dirty(fs);
1100         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1101         inode.i_links_count = 0;
1102         inode.i_dtime = ctx->now;
1103         if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1104                 goto err_out;
1105
1106         group = ext2fs_group_of_ino(fs, ino);
1107         ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
1108         ext2fs_mark_ib_dirty(fs);
1109         ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
1110         ext2fs_group_desc_csum_set(fs, group);
1111         fs->super->s_free_inodes_count++;
1112         return;
1113
1114 err_out:
1115         pctx.errcode = retval;
1116         fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1117         fs->super->s_state &= ~EXT2_VALID_FS;
1118         ext2fs_mark_super_dirty(fs);
1119         return;
1120 }
1121
1122 /*
1123  * This function makes sure the superblock hint for the external
1124  * journal is correct.
1125  */
1126 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1127 {
1128         struct ext2_super_block *sb = ctx->fs->super;
1129         struct problem_context pctx;
1130         char uuid[37], *journal_name;
1131         struct stat st;
1132
1133         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1134             uuid_is_null(sb->s_journal_uuid))
1135                 return 0;
1136
1137         uuid_unparse(sb->s_journal_uuid, uuid);
1138         journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1139         if (!journal_name)
1140                 return 0;
1141
1142         if (stat(journal_name, &st) < 0)
1143                 return 0;
1144
1145         if (st.st_rdev != sb->s_journal_dev) {
1146                 clear_problem_context(&pctx);
1147                 pctx.num = st.st_rdev;
1148                 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1149                         sb->s_journal_dev = st.st_rdev;
1150                         ext2fs_mark_super_dirty(ctx->fs);
1151                 }
1152         }
1153
1154         free(journal_name);
1155         return 0;
1156 }