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