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