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