linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vyacheslav Dubeyko <slava@dubeyko.com>
To: Linux FS devel list <linux-fsdevel@vger.kernel.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>,
	ChristophHellwig <hch@infradead.org>,
	Hin-Tak Leung <htl10@users.sourceforge.net>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH 06/14] hfsplus: implement check consistency of journal log file
Date: Thu, 26 Dec 2013 13:46:28 +0400	[thread overview]
Message-ID: <1388051188.4168.67.camel@slavad-ubuntu> (raw)

From: Vyacheslav Dubeyko <slava@dubeyko.com>
Subject: [PATCH 06/14] hfsplus: implement check consistency of journal log file

This patch implements functionality of checking main HFS+ journal
structures (journal info block and journal header) before replaying
of journal buffer's transactions.

Signed-off-by: Vyacheslav Dubeyko <slava@dubeyko.com>
CC: Al Viro <viro@zeniv.linux.org.uk>
CC: Christoph Hellwig <hch@infradead.org>
CC: Hin-Tak Leung <htl10@users.sourceforge.net>
---
 fs/hfsplus/journal.c |  148 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 148 insertions(+)

diff --git a/fs/hfsplus/journal.c b/fs/hfsplus/journal.c
index 6f61e77..0f1fecc 100644
--- a/fs/hfsplus/journal.c
+++ b/fs/hfsplus/journal.c
@@ -42,6 +42,13 @@
 	((sector_t)(blk) << \
 	 (HFSPLUS_SB(sb)->alloc_blksz_shift - HFSPLUS_SECTOR_SHIFT))
 
+#define TR_START(jh) \
+	(le64_to_cpu(((struct hfsplus_journal_header *)(jh))->start))
+#define LAST_TR_END(jh) \
+	(le64_to_cpu(((struct hfsplus_journal_header *)(jh))->end))
+#define JHDR_SIZE(jh) \
+	(le32_to_cpu(((struct hfsplus_journal_header *)(jh))->jhdr_size))
+
 /*
  * We want at least 8 megs of journal for each 100 gigs of
  * disk space.  We cap the size at 512 megs (64x default), unless
@@ -120,6 +127,32 @@ static u32 calc_internal_checksum(u32 seed, unsigned char *ptr, int len)
 #define HFSPLUS_CALC_CHECKSUM(ptr, len) \
 	HFSPLUS_CHECKSUM(calc_internal_checksum(0, ptr, len))
 
+static int hfsplus_replay_journal_header(struct super_block *sb)
+{
+	struct hfsplus_journal *jnl = HFSPLUS_SB(sb)->jnl;
+	struct hfsplus_journal_header *jh = jnl->jh;
+	sector_t jh_blk = JH_BLOCK(sb, jnl);
+	int err;
+
+	hfs_dbg(JOURNAL, "replay journal header\n");
+
+	jh->start = cpu_to_le64((u64)JHDR_SIZE(jh));
+	jh->end = cpu_to_le64((u64)JHDR_SIZE(jh));
+
+	jh->checksum = 0;
+	jh->checksum = HFSPLUS_CALC_CHECKSUM((unsigned char *)jh, sizeof(*jh));
+
+	err = hfsplus_submit_bio(sb, BLOCK_TO_SEC(sb, jh_blk),
+				 jnl->jh_buf, NULL, WRITE_SYNC, NULL);
+	if (err) {
+		pr_err("can't write journal header block %lu\n",
+			jh_blk);
+		return err;
+	}
+
+	return 0;
+}
+
 /*
  * hfsplus_create_journal - create journal
  *
@@ -194,6 +227,121 @@ static int hfsplus_create_journal(struct super_block *sb,
 	return 0;
 }
 
+static inline
+bool hfsplus_journal_empty(struct hfsplus_journal_header *jh)
+{
+	return TR_START(jh) == LAST_TR_END(jh);
+}
+
+static int hfsplus_replay_journal(struct super_block *sb);
+
+static
+int hfsplus_check_journal_info_block(struct hfsplus_journal_info_block *jib)
+{
+	u32 jib_flags;
+
+	jib_flags = be32_to_cpu(jib->flags);
+	hfs_dbg(JOURNAL, "jib->flags: %#x\n", jib_flags);
+
+	/* Check that journal is on another volume */
+	if (jib_flags & HFSPLUS_JOURNAL_ON_OTHER_DEVICE &&
+	    !(jib_flags & HFSPLUS_JOURNAL_IN_FS)) {
+		pr_err("unable to access the HFS+ journal\n");
+		return -EOPNOTSUPP;
+	}
+
+	/* Check that journal is created yet */
+	if (jib_flags & HFSPLUS_JOURNAL_NEED_INIT) {
+		pr_err("HFS+ journal is not created\n");
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int hfsplus_verify_journal_header(struct hfsplus_journal_header *jh)
+{
+	__le32 jh_checksum;
+	__le32 checksum;
+
+	if (jh->magic != cpu_to_le32(HFSPLUS_JOURNAL_HEADER_MAGIC) ||
+	    jh->endian != cpu_to_le32(HFSPLUS_JOURNAL_HEADER_ENDIAN)) {
+		pr_err("invalid journal header\n");
+		return -EIO;
+	}
+
+	jh_checksum = jh->checksum;
+	jh->checksum = 0;
+
+	checksum = HFSPLUS_CALC_CHECKSUM((unsigned char *)jh, sizeof(*jh));
+
+	if (le32_to_cpu(checksum) != le32_to_cpu(jh_checksum)) {
+		jh->checksum = jh_checksum;
+		pr_err("invalid journal header\n");
+		hfs_dbg(JOURNAL, "calculated CRC %#x, jh_checksum %#x\n",
+			checksum, le32_to_cpu(jh_checksum));
+		return -EIO;
+	}
+
+	jh->checksum = jh_checksum;
+
+	return 0;
+}
+
+/*
+ * hfsplus_check_journal - check consistency of journal log file.
+ *
+ * @sb: superblock
+ */
+int hfsplus_check_journal(struct super_block *sb)
+{
+	struct hfsplus_journal_header *jh;
+	int err;
+
+	if (!HFSPLUS_SB(sb)->jnl) {
+		hfs_dbg(JOURNAL, "HFS+ journal absent");
+		return 0;
+	}
+
+	hfs_dbg(JOURNAL, "check HFS+ journal\n");
+
+	err = hfsplus_check_journal_info_block(HFSPLUS_SB(sb)->jnl->jib);
+	if (err)
+		return err;
+
+	jh = HFSPLUS_SB(sb)->jnl->jh;
+	err = hfsplus_verify_journal_header(jh);
+	if (err)
+		return err;
+
+	if (hfsplus_journal_empty(jh)) {
+		err = hfsplus_replay_journal_header(sb);
+		if (unlikely(err)) {
+			pr_err("journal replay is failed, please run fsck\n");
+			return err;
+		}
+		/* If they're the same, we can mount, it's clean */
+		hfs_dbg(JOURNAL, "journal is empty\n");
+		return 0;
+	} else {
+		/* Try to replay journal */
+		if (test_bit(HFSPLUS_SB_NORECOVERY, &HFSPLUS_SB(sb)->flags)) {
+			pr_info("journal replay is skiped, mounting read-only.\n");
+			sb->s_flags |= MS_RDONLY;
+			return 0;
+		}
+
+		err = hfsplus_replay_journal(sb);
+		if (unlikely(err)) {
+			pr_err("journal replay is failed, please run fsck\n");
+			return err;
+		}
+		hfs_dbg(JOURNAL, "journal replay is done\n");
+	}
+
+	return 0;
+}
+
 /*
  * hfsplus_init_journal - initialize journal object
  *
-- 
1.7.9.5




                 reply	other threads:[~2013-12-26  9:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1388051188.4168.67.camel@slavad-ubuntu \
    --to=slava@dubeyko.com \
    --cc=akpm@linux-foundation.org \
    --cc=hch@infradead.org \
    --cc=htl10@users.sourceforge.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).