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