2020-04-23 03:00:47

by Zhe Li

[permalink] [raw]
Subject: [PATCH v2] jffs2: fix jffs2 mounting failure

Mounting jffs2 on nand flash will get message "failed: I/O error"
with the steps listed below.
1.erase nand flash
2.mount jffs2 on it (this mounting operation will be successful)
3.do chown or chmod to the mount point directory
4.umount jffs2
5.mount jffs2 on nand flash
After step 5, we will get message "mount ... failed: I/O error".

Typical image of this problem is like:
Empty space found from 0x00000000 to 0x008a0000
Inode node at xx, totlen 0x00000044, #ino 1, version 1, isize 0...

The reason for this mounting failure is that at the end of function
jffs2_scan_medium(), jffs2 will check the used_size and some info
of nr_blocks.If conditions are met, it will return -EIO.

The detail is that, in the steps listed above, step 3 will write
jffs2_raw_inode into flash without jffs2_raw_dirent, which will
cause that there are some jffs2_raw_inode but no jffs2_raw_dirent
on flash. This will meet the condition at the end of function
jffs2_scan_medium() and return -EIO if we umount jffs2 and mount it
again.

This patch add a flag in struct jffs2_sb_info to identify the info of
jffs2_raw_inode on flash. The info of jffs2_raw_inode can be devided
into two type--jffs2_raw_inode with ino != 1 and ino == 1. When we
do chown or chmod to the mount point directory, jffs2_raw_inode with
ino == 1 will be written into flash. So this flag use this flag to
identify this case and return success.

Signed-off-by: Zhe Li <[email protected]>
---
fs/jffs2/build.c | 1 +
fs/jffs2/jffs2_fs_sb.h | 26 ++++++++++++++++++++++++++
fs/jffs2/scan.c | 8 +++++++-
3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/fs/jffs2/build.c b/fs/jffs2/build.c
index b288c8a..d9fda40 100644
--- a/fs/jffs2/build.c
+++ b/fs/jffs2/build.c
@@ -405,6 +405,7 @@ int jffs2_do_mount_fs(struct jffs2_sb_info *c)
INIT_LIST_HEAD(&c->bad_used_list);
c->highest_ino = 1;
c->summary = NULL;
+ c->inode_flag = INODE_FLAG_INIT;

ret = jffs2_sum_init(c);
if (ret)
diff --git a/fs/jffs2/jffs2_fs_sb.h b/fs/jffs2/jffs2_fs_sb.h
index 778275f..a01b5f4 100644
--- a/fs/jffs2/jffs2_fs_sb.h
+++ b/fs/jffs2/jffs2_fs_sb.h
@@ -27,6 +27,10 @@
#define JFFS2_SB_FLAG_SCANNING 2 /* Flash scanning is in progress */
#define JFFS2_SB_FLAG_BUILDING 4 /* File system building is in progress */

+#define INODE_FLAG_INIT 0
+#define INODE_FLAG_HAS_ROOT_INODE (0x01 << 0)
+#define INODE_FLAG_HAS_OTHER_INODE (0x01 << 1)
+
struct jffs2_inodirty;

struct jffs2_mount_opts {
@@ -157,6 +161,28 @@ struct jffs2_sb_info {
#endif
/* OS-private pointer for getting back to master superblock info */
void *os_priv;
+ /* this flag show the contained inodes info */
+ int inode_flag;
};

+static inline void set_root_inode_flag(struct jffs2_sb_info *c)
+{
+ c->inode_flag |= INODE_FLAG_HAS_ROOT_INODE;
+}
+
+static inline void set_other_inode_flag(struct jffs2_sb_info *c)
+{
+ c->inode_flag |= INODE_FLAG_HAS_OTHER_INODE;
+}
+
+static inline int has_root_inode(struct jffs2_sb_info *c)
+{
+ return (c->inode_flag & INODE_FLAG_HAS_ROOT_INODE);
+}
+
+static inline int has_other_inode(struct jffs2_sb_info *c)
+{
+ return (c->inode_flag & INODE_FLAG_HAS_OTHER_INODE);
+}
+
#endif /* _JFFS2_FS_SB */
diff --git a/fs/jffs2/scan.c b/fs/jffs2/scan.c
index 5f7e284..ec13e08 100644
--- a/fs/jffs2/scan.c
+++ b/fs/jffs2/scan.c
@@ -261,7 +261,8 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
}
#endif
if (c->nr_erasing_blocks) {
- if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
+ if (!c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks) != c->nr_blocks || bad_blocks == c->nr_blocks) &&
+ !(has_root_inode(c) && !has_other_inode(c))) {
pr_notice("Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
pr_notice("empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",
empty_blocks, bad_blocks, c->nr_blocks);
@@ -1036,6 +1037,11 @@ static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_erasebloc
jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
}

+ if (likely(ino != 1))
+ set_other_inode_flag(c);
+ else
+ set_root_inode_flag(c);
+
return 0;
}

--
2.7.4


2020-06-03 07:06:15

by Richard Weinberger

[permalink] [raw]
Subject: Re: [PATCH v2] jffs2: fix jffs2 mounting failure

On Thu, Apr 23, 2020 at 4:58 AM Zhe Li <[email protected]> wrote:
>
> Mounting jffs2 on nand flash will get message "failed: I/O error"
> with the steps listed below.
> 1.erase nand flash
> 2.mount jffs2 on it (this mounting operation will be successful)
> 3.do chown or chmod to the mount point directory
> 4.umount jffs2
> 5.mount jffs2 on nand flash
> After step 5, we will get message "mount ... failed: I/O error".

The important step is missing. You need to unmount before jffs2 is done
with erasing.

> if (c->nr_erasing_blocks) {
> - if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
> + if (!c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks) != c->nr_blocks || bad_blocks == c->nr_blocks) &&
> + !(has_root_inode(c) && !has_other_inode(c))) {

I see the problem but I don't think it is wise to implement such a workaround.
IIUC the real issue is that c->used_size being zero but
c->nr_free_blocks+empty_blocks+bad_blocks does not sum
up correctly in this situation.

--
Thanks,
//richard

2020-06-09 02:13:58

by Zhe Li

[permalink] [raw]
Subject: [PATCH v3] jffs2: fix jffs2 mounting failure

Thanks for the advice mentioned in the email.
This is my v3 patch for this problem.

Mounting jffs2 on nand flash will get message "failed: I/O error"
with the steps listed below.
1.umount jffs2
2.erase nand flash
3.mount jffs2 on it (this mounting operation will be successful)
4.do chown or chmod to the mount point directory
5.umount jffs2
6.mount jffs2 on nand flash
After step 6, we will get message "mount ... failed: I/O error".

Typical image of this problem is like:
Empty space found from 0x00000000 to 0x008a0000
Inode node at xx, totlen 0x00000044, #ino 1, version 1, isize 0...

The reason for this mounting failure is that at the end of function
jffs2_scan_medium(), jffs2 will check the used_size and some info
of nr_blocks.If conditions are met, it will return -EIO.

The detail is that, in the steps listed above, step 4 will write
jffs2_raw_inode into flash without jffs2_raw_dirent, which will
cause that there are some jffs2_raw_inode but no jffs2_raw_dirent
on flash. This will meet the condition at the end of function
jffs2_scan_medium() and return -EIO if we umount jffs2 and mount it
again.

We notice that jffs2 add the value of c->unchecked_size if we find
an inode node while mounting. And jffs2 will never add the value of
c->unchecked_size in other situations. So this patch add one more
condition about c->unchecked_size of the judgement to fix this problem.

Signed-off-by: Zhe Li <[email protected]>
---
fs/jffs2/scan.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/jffs2/scan.c b/fs/jffs2/scan.c
index 5f7e284..db72a9d 100644
--- a/fs/jffs2/scan.c
+++ b/fs/jffs2/scan.c
@@ -261,7 +261,8 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
}
#endif
if (c->nr_erasing_blocks) {
- if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
+ if (!c->used_size && !c->unchecked_size &&
+ ((c->nr_free_blocks+empty_blocks+bad_blocks) != c->nr_blocks || bad_blocks == c->nr_blocks)) {
pr_notice("Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
pr_notice("empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",
empty_blocks, bad_blocks, c->nr_blocks);
--
2.7.4