linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
From: Jaegeuk Kim <jaegeuk@kernel.org>
To: linux-f2fs-devel@lists.sourceforge.net
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Subject: [PATCH 2/2] fsck.f2fs: support a readonly filesystem
Date: Sun, 22 Nov 2015 11:29:21 +0800	[thread overview]
Message-ID: <1448162962-24175-2-git-send-email-jaegeuk@kernel.org> (raw)
In-Reply-To: <1448162962-24175-1-git-send-email-jaegeuk@kernel.org>

If f2fs is mounted as ro, we can do fsck.f2fs.

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 fsck/fsck.c             | 15 ++++++++-------
 fsck/main.c             | 15 ++++++++++++---
 include/f2fs_fs.h       |  1 +
 lib/libf2fs.c           | 17 +++++++++++------
 mkfs/f2fs_format_main.c |  4 +++-
 5 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d3b5dd4..510c39d 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -174,7 +174,7 @@ static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid,
 			need_fix = 1;
 		}
 	}
-	if (need_fix) {
+	if (need_fix && !config.ro) {
 		u64 ssa_blk;
 		int ret2;
 
@@ -289,7 +289,7 @@ static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
 			need_fix = 1;
 		}
 	}
-	if (need_fix) {
+	if (need_fix && !config.ro) {
 		u64 ssa_blk;
 		int ret2;
 
@@ -719,7 +719,7 @@ skip_blkcnt_fix:
 					nid, i_links);
 		}
 	}
-	if (need_fix) {
+	if (need_fix && !config.ro) {
 		/* drop extent information to avoid potential wrong access */
 		node_blk->i.i_ext.len = 0;
 		ret = dev_write_block(node_blk, ni->blk_addr);
@@ -750,7 +750,7 @@ int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
 			FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
 		}
 	}
-	if (need_fix) {
+	if (need_fix && !config.ro) {
 		ret = dev_write_block(node_blk, ni->blk_addr);
 		ASSERT(ret >= 0);
 	}
@@ -1069,7 +1069,7 @@ int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
 			de_blk->dentry, de_blk->filename,
 			NR_DENTRY_IN_BLOCK, last_blk, encrypted);
 
-	if (dentries < 0) {
+	if (dentries < 0 && !config.ro) {
 		ret = dev_write_block(de_blk, blk_addr);
 		ASSERT(ret >= 0);
 		DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
@@ -1172,7 +1172,8 @@ void fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
 			else if (ret)
 				ASSERT_MSG("[0x%x] wrong orphan inode", ino);
 		}
-		if (config.fix_on && entry_count != new_entry_count) {
+		if (!config.ro && config.fix_on &&
+				entry_count != new_entry_count) {
 			new_blk->entry_count = cpu_to_le32(new_entry_count);
 			ret = dev_write_block(new_blk, start_blk + i);
 			ASSERT(ret >= 0);
@@ -1475,7 +1476,7 @@ int fsck_verify(struct f2fs_sb_info *sbi)
 	}
 
 	/* fix global metadata */
-	if (force || (config.bug_on && config.fix_on)) {
+	if (force || (config.bug_on && config.fix_on && !config.ro)) {
 		fix_hard_links(sbi);
 		fix_nat_entries(sbi);
 		rewrite_sit_area_bitmap(sbi);
diff --git a/fsck/main.c b/fsck/main.c
index 844ee8a..3db4a44 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -197,8 +197,17 @@ int main(int argc, char **argv)
 
 	f2fs_parse_options(argc, argv);
 
-	if (f2fs_dev_is_umounted(&config) < 0)
-		return -1;
+	if (f2fs_dev_is_umounted(&config) < 0) {
+		if (!config.ro) {
+			MSG(0, "\tError: Not available on mounted device!\n");
+			return -1;
+		}
+
+		/* allow ro-mounted partition */
+		MSG(0, "Info: Check FS only due to RO\n");
+		config.fix_on = 0;
+		config.auto_fix = 0;
+	}
 
 	/* Get device */
 	if (f2fs_get_device_info(&config) < 0)
@@ -228,7 +237,7 @@ fsck_again:
 	f2fs_do_umount(sbi);
 out:
 	if (config.func == FSCK && config.bug_on) {
-		if (config.fix_on == 0 && config.auto_fix == 0) {
+		if (!config.ro && config.fix_on == 0 && config.auto_fix == 0) {
 			char ans[255] = {0};
 retry:
 			printf("Do you want to fix this partition? [Y/N] ");
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 359deec..c0bbfa4 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -250,6 +250,7 @@ struct f2fs_configuration {
 	int fix_on;
 	int bug_on;
 	int auto_fix;
+	int ro;
 	__le32 feature;			/* defined features */
 } __attribute__((packed));
 
diff --git a/lib/libf2fs.c b/lib/libf2fs.c
index 83d1296..32e0651 100644
--- a/lib/libf2fs.c
+++ b/lib/libf2fs.c
@@ -362,9 +362,11 @@ void f2fs_init_configuration(struct f2fs_configuration *c)
 	c->vol_label = "";
 	c->device_name = NULL;
 	c->trim = 1;
+	c->ro = 0;
 }
 
-static int is_mounted(const char *mpt, const char *device)
+static int is_mounted(struct f2fs_configuration *c,
+				const char *mpt, const char *device)
 {
 	FILE *file = NULL;
 	struct mntent *mnt = NULL;
@@ -374,8 +376,11 @@ static int is_mounted(const char *mpt, const char *device)
 		return 0;
 
 	while ((mnt = getmntent(file)) != NULL) {
-		if (!strcmp(device, mnt->mnt_fsname))
+		if (!strcmp(device, mnt->mnt_fsname)) {
+			if (hasmntopt(mnt, MNTOPT_RO))
+				config.ro = 1;
 			break;
+		}
 	}
 	endmntent(file);
 	return mnt ? 1 : 0;
@@ -386,9 +391,9 @@ int f2fs_dev_is_umounted(struct f2fs_configuration *c)
 	struct stat st_buf;
 	int ret = 0;
 
-	ret = is_mounted(MOUNTED, c->device_name);
+	ret = is_mounted(c, MOUNTED, c->device_name);
 	if (ret) {
-		MSG(0, "\tError: Not available on mounted device!\n");
+		MSG(0, "Info: Mounted device!\n");
 		return -1;
 	}
 
@@ -396,9 +401,9 @@ int f2fs_dev_is_umounted(struct f2fs_configuration *c)
 	 * if failed due to /etc/mtab file not present
 	 * try with /proc/mounts.
 	 */
-	ret = is_mounted("/proc/mounts", c->device_name);
+	ret = is_mounted(c, "/proc/mounts", c->device_name);
 	if (ret) {
-		MSG(0, "\tError: Not available on mounted device!\n");
+		MSG(0, "Info: Mounted device!\n");
 		return -1;
 	}
 
diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index 2ea809c..0eee81a 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -144,8 +144,10 @@ int main(int argc, char *argv[])
 
 	f2fs_show_info();
 
-	if (f2fs_dev_is_umounted(&config) < 0)
+	if (f2fs_dev_is_umounted(&config) < 0) {
+		MSG(0, "\tError: Not available on mounted device!\n");
 		return -1;
+	}
 
 	if (f2fs_get_device_info(&config) < 0)
 		return -1;
-- 
2.4.9 (Apple Git-60)


------------------------------------------------------------------------------

      reply	other threads:[~2015-11-22  3:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-22  3:29 [PATCH 1/2] fsck.f2fs: declare static function Jaegeuk Kim
2015-11-22  3:29 ` Jaegeuk Kim [this message]

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=1448162962-24175-2-git-send-email-jaegeuk@kernel.org \
    --to=jaegeuk@kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    /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).