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