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