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