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