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