Whamcloud - gitweb
misc: zero s_jnl_blocks when removing internal journal
[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 (!journal_has_csum_v2or3(j))
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 (!journal_has_csum_v2or3(j))
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 (!journal_has_csum_v2or3(j))
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 (int) 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 int 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         return io_channel_flush(io) ? EIO : 0;
157 }
158
159 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
160 {
161         errcode_t 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 = (int) 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 = (int) 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
295         clear_problem_context(&pctx);
296
297         journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
298         if (!journal) {
299                 return EXT2_ET_NO_MEMORY;
300         }
301
302         dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
303         if (!dev_fs) {
304                 retval = EXT2_ET_NO_MEMORY;
305                 goto errout;
306         }
307         dev_journal = dev_fs+1;
308
309         dev_fs->k_ctx = dev_journal->k_ctx = ctx;
310         dev_fs->k_dev = K_DEV_FS;
311         dev_journal->k_dev = K_DEV_JOURNAL;
312
313         journal->j_dev = dev_journal;
314         journal->j_fs_dev = dev_fs;
315         journal->j_inode = NULL;
316         journal->j_blocksize = ctx->fs->blocksize;
317
318         if (uuid_is_null(sb->s_journal_uuid)) {
319                 if (!sb->s_journal_inum) {
320                         retval = EXT2_ET_BAD_INODE_NUM;
321                         goto errout;
322                 }
323                 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
324                                                  "journal inode");
325                 if (!j_inode) {
326                         retval = EXT2_ET_NO_MEMORY;
327                         goto errout;
328                 }
329
330                 j_inode->i_ctx = ctx;
331                 j_inode->i_ino = sb->s_journal_inum;
332
333                 if ((retval = ext2fs_read_inode(ctx->fs,
334                                                 sb->s_journal_inum,
335                                                 &j_inode->i_ext2))) {
336                 try_backup_journal:
337                         if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
338                             tried_backup_jnl)
339                                 goto errout;
340                         memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
341                         memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
342                                EXT2_N_BLOCKS*4);
343                         j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
344                         j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
345                         j_inode->i_ext2.i_links_count = 1;
346                         j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
347                         e2fsck_use_inode_shortcuts(ctx, 1);
348                         ctx->stashed_ino = j_inode->i_ino;
349                         ctx->stashed_inode = &j_inode->i_ext2;
350                         tried_backup_jnl++;
351                 }
352                 if (!j_inode->i_ext2.i_links_count ||
353                     !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
354                         retval = EXT2_ET_NO_JOURNAL;
355                         goto try_backup_journal;
356                 }
357                 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
358                     JFS_MIN_JOURNAL_BLOCKS) {
359                         retval = EXT2_ET_JOURNAL_TOO_SMALL;
360                         goto try_backup_journal;
361                 }
362                 pb.last_block = -1;
363                 retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
364                                                BLOCK_FLAG_HOLE, 0,
365                                                process_journal_block, &pb);
366                 if ((pb.last_block + 1) * ctx->fs->blocksize <
367                     (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
368                         retval = EXT2_ET_JOURNAL_TOO_SMALL;
369                         goto try_backup_journal;
370                 }
371                 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
372                         retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
373                                                     &j_inode->i_ext2);
374                         if (retval)
375                                 goto errout;
376                 }
377
378                 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
379                         journal->j_blocksize;
380
381 #ifdef USE_INODE_IO
382                 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
383                                                  &j_inode->i_ext2,
384                                                  &journal_name);
385                 if (retval)
386                         goto errout;
387
388                 io_ptr = inode_io_manager;
389 #else
390                 journal->j_inode = j_inode;
391                 ctx->journal_io = ctx->fs->io;
392                 if ((retval = (errcode_t) journal_bmap(journal, 0, &start)) != 0)
393                         goto errout;
394 #endif
395         } else {
396                 ext_journal = 1;
397                 if (!ctx->journal_name) {
398                         char uuid[37];
399
400                         uuid_unparse(sb->s_journal_uuid, uuid);
401                         ctx->journal_name = blkid_get_devname(ctx->blkid,
402                                                               "UUID", uuid);
403                         if (!ctx->journal_name)
404                                 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
405                 }
406                 journal_name = ctx->journal_name;
407
408                 if (!journal_name) {
409                         fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
410                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
411                         goto errout;
412                 }
413
414                 jfs_debug(1, "Using journal file %s\n", journal_name);
415                 io_ptr = unix_io_manager;
416         }
417
418 #if 0
419         test_io_backing_manager = io_ptr;
420         io_ptr = test_io_manager;
421 #endif
422 #ifndef USE_INODE_IO
423         if (ext_journal)
424 #endif
425         {
426                 int flags = IO_FLAG_RW;
427                 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
428                       ctx->mount_flags & EXT2_MF_READONLY))
429                         flags |= IO_FLAG_EXCLUSIVE;
430                 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
431                     (ctx->options & E2F_OPT_FORCE))
432                         flags &= ~IO_FLAG_EXCLUSIVE;
433
434
435                 retval = io_ptr->open(journal_name, flags,
436                                       &ctx->journal_io);
437         }
438         if (retval)
439                 goto errout;
440
441         io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
442
443         if (ext_journal) {
444                 blk64_t maxlen;
445
446                 start = ext2fs_journal_sb_start(ctx->fs->blocksize) - 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 + SUPERBLOCK_OFFSET,
458                        sizeof(jsuper));
459 #ifdef WORDS_BIGENDIAN
460                 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
461                         ext2fs_swap_super(&jsuper);
462 #endif
463                 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
464                     !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
465                         fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
466                         retval = EXT2_ET_LOAD_EXT_JOURNAL;
467                         brelse(bh);
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                         brelse(bh);
476                         goto errout;
477                 }
478
479                 /* Check the superblock checksum */
480                 if (jsuper.s_feature_ro_compat &
481                     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
482                         struct struct_ext2_filsys fsx;
483                         struct ext2_super_block superx;
484                         void *p;
485
486                         p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
487                         memcpy(&fsx, ctx->fs, sizeof(fsx));
488                         memcpy(&superx, ctx->fs->super, sizeof(superx));
489                         fsx.super = &superx;
490                         fsx.super->s_feature_ro_compat |=
491                                         EXT4_FEATURE_RO_COMPAT_METADATA_CSUM;
492                         if (!ext2fs_superblock_csum_verify(&fsx, p) &&
493                             fix_problem(ctx, PR_0_EXT_JOURNAL_SUPER_CSUM_INVALID,
494                                         &pctx)) {
495                                 ext2fs_superblock_csum_set(&fsx, p);
496                                 mark_buffer_dirty(bh);
497                         }
498                 }
499                 brelse(bh);
500
501                 maxlen = ext2fs_blocks_count(&jsuper);
502                 journal->j_maxlen = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1;
503                 start++;
504         }
505
506         if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
507                 retval = EXT2_ET_NO_MEMORY;
508                 goto errout;
509         }
510
511         journal->j_sb_buffer = bh;
512         journal->j_superblock = (journal_superblock_t *)bh->b_data;
513
514 #ifdef USE_INODE_IO
515         if (j_inode)
516                 ext2fs_free_mem(&j_inode);
517 #endif
518
519         *ret_journal = journal;
520         e2fsck_use_inode_shortcuts(ctx, 0);
521         return 0;
522
523 errout:
524         e2fsck_use_inode_shortcuts(ctx, 0);
525         if (dev_fs)
526                 ext2fs_free_mem(&dev_fs);
527         if (j_inode)
528                 ext2fs_free_mem(&j_inode);
529         if (journal)
530                 ext2fs_free_mem(&journal);
531         return retval;
532 }
533
534 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
535                                               struct problem_context *pctx)
536 {
537         struct ext2_super_block *sb = ctx->fs->super;
538         int recover = ctx->fs->super->s_feature_incompat &
539                 EXT3_FEATURE_INCOMPAT_RECOVER;
540         int has_journal = ctx->fs->super->s_feature_compat &
541                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
542
543         if (has_journal || sb->s_journal_inum) {
544                 /* The journal inode is bogus, remove and force full fsck */
545                 pctx->ino = sb->s_journal_inum;
546                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
547                         if (has_journal && sb->s_journal_inum)
548                                 printf("*** ext3 journal has been deleted - "
549                                        "filesystem is now ext2 only ***\n\n");
550                         sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
551                         sb->s_journal_inum = 0;
552                         memset(sb->s_jnl_blocks, 0, sizeof(sb->s_jnl_blocks));
553                         ctx->flags |= E2F_FLAG_JOURNAL_INODE;
554                         ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
555                         e2fsck_clear_recover(ctx, 1);
556                         return 0;
557                 }
558                 return EXT2_ET_BAD_INODE_NUM;
559         } else if (recover) {
560                 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
561                         e2fsck_clear_recover(ctx, 1);
562                         return 0;
563                 }
564                 return EXT2_ET_UNSUPP_FEATURE;
565         }
566         return 0;
567 }
568
569 #define V1_SB_SIZE      0x0024
570 static void clear_v2_journal_fields(journal_t *journal)
571 {
572         e2fsck_t ctx = journal->j_dev->k_ctx;
573         struct problem_context pctx;
574
575         clear_problem_context(&pctx);
576
577         if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
578                 return;
579
580         memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
581                ctx->fs->blocksize-V1_SB_SIZE);
582         mark_buffer_dirty(journal->j_sb_buffer);
583 }
584
585
586 static errcode_t e2fsck_journal_load(journal_t *journal)
587 {
588         e2fsck_t ctx = journal->j_dev->k_ctx;
589         journal_superblock_t *jsb;
590         struct buffer_head *jbh = journal->j_sb_buffer;
591         struct problem_context pctx;
592
593         clear_problem_context(&pctx);
594
595         ll_rw_block(READ, 1, &jbh);
596         if (jbh->b_err) {
597                 com_err(ctx->device_name, jbh->b_err, "%s",
598                         _("reading journal superblock\n"));
599                 return jbh->b_err;
600         }
601
602         jsb = journal->j_superblock;
603         /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
604         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
605                 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
606
607         switch (ntohl(jsb->s_header.h_blocktype)) {
608         case JFS_SUPERBLOCK_V1:
609                 journal->j_format_version = 1;
610                 if (jsb->s_feature_compat ||
611                     jsb->s_feature_incompat ||
612                     jsb->s_feature_ro_compat ||
613                     jsb->s_nr_users)
614                         clear_v2_journal_fields(journal);
615                 break;
616
617         case JFS_SUPERBLOCK_V2:
618                 journal->j_format_version = 2;
619                 if (ntohl(jsb->s_nr_users) > 1 &&
620                     uuid_is_null(ctx->fs->super->s_journal_uuid))
621                         clear_v2_journal_fields(journal);
622                 if (ntohl(jsb->s_nr_users) > 1) {
623                         fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
624                         return EXT2_ET_JOURNAL_UNSUPP_VERSION;
625                 }
626                 break;
627
628         /*
629          * These should never appear in a journal super block, so if
630          * they do, the journal is badly corrupted.
631          */
632         case JFS_DESCRIPTOR_BLOCK:
633         case JFS_COMMIT_BLOCK:
634         case JFS_REVOKE_BLOCK:
635                 return EXT2_ET_CORRUPT_SUPERBLOCK;
636
637         /* If we don't understand the superblock major type, but there
638          * is a magic number, then it is likely to be a new format we
639          * just don't understand, so leave it alone. */
640         default:
641                 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
642         }
643
644         if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
645                 return EXT2_ET_UNSUPP_FEATURE;
646
647         if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
648                 return EXT2_ET_RO_UNSUPP_FEATURE;
649
650         /* Checksum v1-3 are mutually exclusive features. */
651         if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V2) &&
652             JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V3))
653                 return EXT2_ET_CORRUPT_SUPERBLOCK;
654
655         if (journal_has_csum_v2or3(journal) &&
656             JFS_HAS_COMPAT_FEATURE(journal, JFS_FEATURE_COMPAT_CHECKSUM))
657                 return EXT2_ET_CORRUPT_SUPERBLOCK;
658
659         if (!e2fsck_journal_verify_csum_type(journal, jsb) ||
660             !e2fsck_journal_sb_csum_verify(journal, jsb))
661                 return EXT2_ET_CORRUPT_SUPERBLOCK;
662
663         if (journal_has_csum_v2or3(journal))
664                 journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid,
665                                                    sizeof(jsb->s_uuid));
666
667         /* We have now checked whether we know enough about the journal
668          * format to be able to proceed safely, so any other checks that
669          * fail we should attempt to recover from. */
670         if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
671                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
672                         _("%s: no valid journal superblock found\n"),
673                         ctx->device_name);
674                 return EXT2_ET_CORRUPT_SUPERBLOCK;
675         }
676
677         if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
678                 journal->j_maxlen = ntohl(jsb->s_maxlen);
679         else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
680                 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
681                         _("%s: journal too short\n"),
682                         ctx->device_name);
683                 return EXT2_ET_CORRUPT_SUPERBLOCK;
684         }
685
686         journal->j_tail_sequence = ntohl(jsb->s_sequence);
687         journal->j_transaction_sequence = journal->j_tail_sequence;
688         journal->j_tail = ntohl(jsb->s_start);
689         journal->j_first = ntohl(jsb->s_first);
690         journal->j_last = ntohl(jsb->s_maxlen);
691
692         return 0;
693 }
694
695 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
696                                        journal_t *journal)
697 {
698         char *p;
699         union {
700                 uuid_t uuid;
701                 __u32 val[4];
702         } u;
703         __u32 new_seq = 0;
704         int i;
705
706         /* Leave a valid existing V1 superblock signature alone.
707          * Anything unrecognisable we overwrite with a new V2
708          * signature. */
709
710         if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
711             jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
712                 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
713                 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
714         }
715
716         /* Zero out everything else beyond the superblock header */
717
718         p = ((char *) jsb) + sizeof(journal_header_t);
719         memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
720
721         jsb->s_blocksize = htonl(ctx->fs->blocksize);
722         jsb->s_maxlen = htonl(journal->j_maxlen);
723         jsb->s_first = htonl(1);
724
725         /* Initialize the journal sequence number so that there is "no"
726          * chance we will find old "valid" transactions in the journal.
727          * This avoids the need to zero the whole journal (slow to do,
728          * and risky when we are just recovering the filesystem).
729          */
730         uuid_generate(u.uuid);
731         for (i = 0; i < 4; i ++)
732                 new_seq ^= u.val[i];
733         jsb->s_sequence = htonl(new_seq);
734         e2fsck_journal_sb_csum_set(journal, jsb);
735
736         mark_buffer_dirty(journal->j_sb_buffer);
737         ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
738 }
739
740 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
741                                                   journal_t *journal,
742                                                   struct problem_context *pctx)
743 {
744         struct ext2_super_block *sb = ctx->fs->super;
745         int recover = ctx->fs->super->s_feature_incompat &
746                 EXT3_FEATURE_INCOMPAT_RECOVER;
747
748         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
749                 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
750                         e2fsck_journal_reset_super(ctx, journal->j_superblock,
751                                                    journal);
752                         journal->j_transaction_sequence = 1;
753                         e2fsck_clear_recover(ctx, recover);
754                         return 0;
755                 }
756                 return EXT2_ET_CORRUPT_SUPERBLOCK;
757         } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
758                 return EXT2_ET_CORRUPT_SUPERBLOCK;
759
760         return 0;
761 }
762
763 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
764                                    int reset, int drop)
765 {
766         journal_superblock_t *jsb;
767
768         if (drop)
769                 mark_buffer_clean(journal->j_sb_buffer);
770         else if (!(ctx->options & E2F_OPT_READONLY)) {
771                 jsb = journal->j_superblock;
772                 jsb->s_sequence = htonl(journal->j_transaction_sequence);
773                 if (reset)
774                         jsb->s_start = 0; /* this marks the journal as empty */
775                 e2fsck_journal_sb_csum_set(journal, jsb);
776                 mark_buffer_dirty(journal->j_sb_buffer);
777         }
778         brelse(journal->j_sb_buffer);
779
780         if (ctx->journal_io) {
781                 if (ctx->fs && ctx->fs->io != ctx->journal_io)
782                         io_channel_close(ctx->journal_io);
783                 ctx->journal_io = 0;
784         }
785
786 #ifndef USE_INODE_IO
787         if (journal->j_inode)
788                 ext2fs_free_mem(&journal->j_inode);
789 #endif
790         if (journal->j_fs_dev)
791                 ext2fs_free_mem(&journal->j_fs_dev);
792         ext2fs_free_mem(&journal);
793 }
794
795 /*
796  * This function makes sure that the superblock fields regarding the
797  * journal are consistent.
798  */
799 errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
800 {
801         struct ext2_super_block *sb = ctx->fs->super;
802         journal_t *journal;
803         int recover = ctx->fs->super->s_feature_incompat &
804                 EXT3_FEATURE_INCOMPAT_RECOVER;
805         struct problem_context pctx;
806         problem_t problem;
807         int reset = 0, force_fsck = 0;
808         errcode_t retval;
809
810         /* If we don't have any journal features, don't do anything more */
811         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
812             !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
813             uuid_is_null(sb->s_journal_uuid))
814                 return 0;
815
816         clear_problem_context(&pctx);
817         pctx.num = sb->s_journal_inum;
818
819         retval = e2fsck_get_journal(ctx, &journal);
820         if (retval) {
821                 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
822                     (retval == EXT2_ET_BAD_BLOCK_NUM) ||
823                     (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
824                     (retval == EXT2_ET_NO_JOURNAL))
825                         return e2fsck_journal_fix_bad_inode(ctx, &pctx);
826                 return retval;
827         }
828
829         retval = e2fsck_journal_load(journal);
830         if (retval) {
831                 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
832                     ((retval == EXT2_ET_UNSUPP_FEATURE) &&
833                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
834                                   &pctx))) ||
835                     ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
836                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
837                                   &pctx))) ||
838                     ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
839                     (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
840                         retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
841                                                                   &pctx);
842                 e2fsck_journal_release(ctx, journal, 0, 1);
843                 return retval;
844         }
845
846         /*
847          * We want to make the flags consistent here.  We will not leave with
848          * needs_recovery set but has_journal clear.  We can't get in a loop
849          * with -y, -n, or -p, only if a user isn't making up their mind.
850          */
851 no_has_journal:
852         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
853                 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
854                 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
855                         if (recover &&
856                             !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
857                                 goto no_has_journal;
858                         /*
859                          * Need a full fsck if we are releasing a
860                          * journal stored on a reserved inode.
861                          */
862                         force_fsck = recover ||
863                                 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
864                         /* Clear all of the journal fields */
865                         sb->s_journal_inum = 0;
866                         sb->s_journal_dev = 0;
867                         memset(sb->s_journal_uuid, 0,
868                                sizeof(sb->s_journal_uuid));
869                         e2fsck_clear_recover(ctx, force_fsck);
870                 } else if (!(ctx->options & E2F_OPT_READONLY)) {
871                         sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
872                         ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
873                         ext2fs_mark_super_dirty(ctx->fs);
874                 }
875         }
876
877         if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
878             !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
879             journal->j_superblock->s_start != 0) {
880                 /* Print status information */
881                 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
882                 if (ctx->superblock)
883                         problem = PR_0_JOURNAL_RUN_DEFAULT;
884                 else
885                         problem = PR_0_JOURNAL_RUN;
886                 if (fix_problem(ctx, problem, &pctx)) {
887                         ctx->options |= E2F_OPT_FORCE;
888                         sb->s_feature_incompat |=
889                                 EXT3_FEATURE_INCOMPAT_RECOVER;
890                         ext2fs_mark_super_dirty(ctx->fs);
891                 } else if (fix_problem(ctx,
892                                        PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
893                         reset = 1;
894                         sb->s_state &= ~EXT2_VALID_FS;
895                         ext2fs_mark_super_dirty(ctx->fs);
896                 }
897                 /*
898                  * If the user answers no to the above question, we
899                  * ignore the fact that journal apparently has data;
900                  * accidentally replaying over valid data would be far
901                  * worse than skipping a questionable recovery.
902                  *
903                  * XXX should we abort with a fatal error here?  What
904                  * will the ext3 kernel code do if a filesystem with
905                  * !NEEDS_RECOVERY but with a non-zero
906                  * journal->j_superblock->s_start is mounted?
907                  */
908         }
909
910         /*
911          * If we don't need to do replay the journal, check to see if
912          * the journal's errno is set; if so, we need to mark the file
913          * system as being corrupt and clear the journal's s_errno.
914          */
915         if (!(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
916             journal->j_superblock->s_errno) {
917                 ctx->fs->super->s_state |= EXT2_ERROR_FS;
918                 ext2fs_mark_super_dirty(ctx->fs);
919                 journal->j_superblock->s_errno = 0;
920                 e2fsck_journal_sb_csum_set(journal, journal->j_superblock);
921                 mark_buffer_dirty(journal->j_sb_buffer);
922         }
923
924         e2fsck_journal_release(ctx, journal, reset, 0);
925         return retval;
926 }
927
928 static errcode_t recover_ext3_journal(e2fsck_t ctx)
929 {
930         struct problem_context  pctx;
931         journal_t *journal;
932         errcode_t retval;
933
934         clear_problem_context(&pctx);
935
936         journal_init_revoke_caches();
937         retval = e2fsck_get_journal(ctx, &journal);
938         if (retval)
939                 return retval;
940
941         retval = e2fsck_journal_load(journal);
942         if (retval)
943                 goto errout;
944
945         retval = journal_init_revoke(journal, 1024);
946         if (retval)
947                 goto errout;
948
949         retval = -journal_recover(journal);
950         if (retval)
951                 goto errout;
952
953         if (journal->j_failed_commit) {
954                 pctx.ino = journal->j_failed_commit;
955                 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
956                 journal->j_superblock->s_errno = -EINVAL;
957                 mark_buffer_dirty(journal->j_sb_buffer);
958         }
959
960 errout:
961         journal_destroy_revoke(journal);
962         journal_destroy_revoke_caches();
963         e2fsck_journal_release(ctx, journal, 1, 0);
964         return retval;
965 }
966
967 errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
968 {
969         io_manager io_ptr = ctx->fs->io->manager;
970         int blocksize = ctx->fs->blocksize;
971         errcode_t       retval, recover_retval;
972         io_stats        stats = 0;
973         unsigned long long kbytes_written = 0;
974
975         printf(_("%s: recovering journal\n"), ctx->device_name);
976         if (ctx->options & E2F_OPT_READONLY) {
977                 printf(_("%s: won't do journal recovery while read-only\n"),
978                        ctx->device_name);
979                 return EXT2_ET_FILE_RO;
980         }
981
982         if (ctx->fs->flags & EXT2_FLAG_DIRTY)
983                 ext2fs_flush(ctx->fs);  /* Force out any modifications */
984
985         recover_retval = recover_ext3_journal(ctx);
986
987         /*
988          * Reload the filesystem context to get up-to-date data from disk
989          * because journal recovery will change the filesystem under us.
990          */
991         if (ctx->fs->super->s_kbytes_written &&
992             ctx->fs->io->manager->get_stats)
993                 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
994         if (stats && stats->bytes_written)
995                 kbytes_written = stats->bytes_written >> 10;
996
997         ext2fs_mmp_stop(ctx->fs);
998         ext2fs_free(ctx->fs);
999         retval = ext2fs_open(ctx->filesystem_name, ctx->openfs_flags,
1000                              ctx->superblock, blocksize, io_ptr,
1001                              &ctx->fs);
1002         if (retval) {
1003                 com_err(ctx->program_name, retval,
1004                         _("while trying to re-open %s"),
1005                         ctx->device_name);
1006                 fatal_error(ctx, 0);
1007         }
1008         ctx->fs->priv_data = ctx;
1009         ctx->fs->now = ctx->now;
1010         ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1011         ctx->fs->super->s_kbytes_written += kbytes_written;
1012
1013         /* Set the superblock flags */
1014         e2fsck_clear_recover(ctx, recover_retval != 0);
1015
1016         /*
1017          * Do one last sanity check, and propagate journal->s_errno to
1018          * the EXT2_ERROR_FS flag in the fs superblock if needed.
1019          */
1020         retval = e2fsck_check_ext3_journal(ctx);
1021         return retval ? retval : recover_retval;
1022 }
1023
1024 /*
1025  * This function will move the journal inode from a visible file in
1026  * the filesystem directory hierarchy to the reserved inode if necessary.
1027  */
1028 static const char * const journal_names[] = {
1029         ".journal", "journal", ".journal.dat", "journal.dat", 0 };
1030
1031 void e2fsck_move_ext3_journal(e2fsck_t ctx)
1032 {
1033         struct ext2_super_block *sb = ctx->fs->super;
1034         struct problem_context  pctx;
1035         struct ext2_inode       inode;
1036         ext2_filsys             fs = ctx->fs;
1037         ext2_ino_t              ino;
1038         errcode_t               retval;
1039         const char * const *    cpp;
1040         dgrp_t                  group;
1041         int                     mount_flags;
1042
1043         clear_problem_context(&pctx);
1044
1045         /*
1046          * If the filesystem is opened read-only, or there is no
1047          * journal, then do nothing.
1048          */
1049         if ((ctx->options & E2F_OPT_READONLY) ||
1050             (sb->s_journal_inum == 0) ||
1051             !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1052                 return;
1053
1054         /*
1055          * Read in the journal inode
1056          */
1057         if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
1058                 return;
1059
1060         /*
1061          * If it's necessary to backup the journal inode, do so.
1062          */
1063         if ((sb->s_jnl_backup_type == 0) ||
1064             ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
1065              memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
1066                 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
1067                         memcpy(sb->s_jnl_blocks, inode.i_block,
1068                                EXT2_N_BLOCKS*4);
1069                         sb->s_jnl_blocks[15] = inode.i_size_high;
1070                         sb->s_jnl_blocks[16] = inode.i_size;
1071                         sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1072                         ext2fs_mark_super_dirty(fs);
1073                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1074                 }
1075         }
1076
1077         /*
1078          * If the journal is already the hidden inode, then do nothing
1079          */
1080         if (sb->s_journal_inum == EXT2_JOURNAL_INO)
1081                 return;
1082
1083         /*
1084          * The journal inode had better have only one link and not be readable.
1085          */
1086         if (inode.i_links_count != 1)
1087                 return;
1088
1089         /*
1090          * If the filesystem is mounted, or we can't tell whether
1091          * or not it's mounted, do nothing.
1092          */
1093         retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
1094         if (retval || (mount_flags & EXT2_MF_MOUNTED))
1095                 return;
1096
1097         /*
1098          * If we can't find the name of the journal inode, then do
1099          * nothing.
1100          */
1101         for (cpp = journal_names; *cpp; cpp++) {
1102                 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
1103                                        strlen(*cpp), 0, &ino);
1104                 if ((retval == 0) && (ino == sb->s_journal_inum))
1105                         break;
1106         }
1107         if (*cpp == 0)
1108                 return;
1109
1110         /* We need the inode bitmap to be loaded */
1111         retval = ext2fs_read_bitmaps(fs);
1112         if (retval)
1113                 return;
1114
1115         pctx.str = *cpp;
1116         if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
1117                 return;
1118
1119         /*
1120          * OK, we've done all the checks, let's actually move the
1121          * journal inode.  Errors at this point mean we need to force
1122          * an ext2 filesystem check.
1123          */
1124         if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1125                 goto err_out;
1126         if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1127                 goto err_out;
1128         sb->s_journal_inum = EXT2_JOURNAL_INO;
1129         ext2fs_mark_super_dirty(fs);
1130         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1131         inode.i_links_count = 0;
1132         inode.i_dtime = ctx->now;
1133         if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1134                 goto err_out;
1135
1136         group = ext2fs_group_of_ino(fs, ino);
1137         ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
1138         ext2fs_mark_ib_dirty(fs);
1139         ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
1140         ext2fs_group_desc_csum_set(fs, group);
1141         fs->super->s_free_inodes_count++;
1142         return;
1143
1144 err_out:
1145         pctx.errcode = retval;
1146         fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1147         fs->super->s_state &= ~EXT2_VALID_FS;
1148         ext2fs_mark_super_dirty(fs);
1149         return;
1150 }
1151
1152 /*
1153  * This function makes sure the superblock hint for the external
1154  * journal is correct.
1155  */
1156 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1157 {
1158         struct ext2_super_block *sb = ctx->fs->super;
1159         struct problem_context pctx;
1160         char uuid[37], *journal_name;
1161         struct stat st;
1162
1163         if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1164             uuid_is_null(sb->s_journal_uuid))
1165                 return 0;
1166
1167         uuid_unparse(sb->s_journal_uuid, uuid);
1168         journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1169         if (!journal_name)
1170                 return 0;
1171
1172         if (stat(journal_name, &st) < 0) {
1173                 free(journal_name);
1174                 return 0;
1175         }
1176
1177         if (st.st_rdev != sb->s_journal_dev) {
1178                 clear_problem_context(&pctx);
1179                 pctx.num = st.st_rdev;
1180                 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1181                         sb->s_journal_dev = st.st_rdev;
1182                         ext2fs_mark_super_dirty(ctx->fs);
1183                 }
1184         }
1185
1186         free(journal_name);
1187         return 0;
1188 }