[f2fs-dev] [PATCH] f2fs: remove redundant check in f2fs_file_write_iter()

2019-04-22 Thread Chengguang Xu
We have already checked flag IOCB_DIRECT in the sanity
check of flag IOCB_NOWAIT, so don't have to check it
again here.

Signed-off-by: Chengguang Xu 
---
 fs/f2fs/file.c | 21 +
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 5742ab8b57dc..43b6a0b08497 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -3056,18 +3056,15 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, 
struct iov_iter *from)
if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
set_inode_flag(inode, FI_NO_PREALLOC);

-   if ((iocb->ki_flags & IOCB_NOWAIT) &&
-   (iocb->ki_flags & IOCB_DIRECT)) {
-   if (!f2fs_overwrite_io(inode, iocb->ki_pos,
-   iov_iter_count(from)) ||
-   f2fs_has_inline_data(inode) ||
-   f2fs_force_buffered_io(inode,
-   iocb, from)) {
-   clear_inode_flag(inode,
-   FI_NO_PREALLOC);
-   inode_unlock(inode);
-   return -EAGAIN;
-   }
+   if (iocb->ki_flags & IOCB_NOWAIT) {
+   if (!f2fs_overwrite_io(inode, iocb->ki_pos,
+   iov_iter_count(from)) ||
+   f2fs_has_inline_data(inode) ||
+   f2fs_force_buffered_io(inode, iocb, from)) {
+   clear_inode_flag(inode, FI_NO_PREALLOC);
+   inode_unlock(inode);
+   return -EAGAIN;
+   }

} else {
preallocated = true;
--
2.20.1



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [RESEND PATCH] f2fs: Fix one GCC warning

2019-04-22 Thread Zhangshaokun
Hi,

On 2019/4/23 10:57, Chao Yu wrote:
> On 2019/4/22 21:27, Shaokun Zhang wrote:
>> fs/f2fs/node.c: In function ‘f2fs_remove_inode_page’:
>> fs/f2fs/node.c:1193:47: warning: format ‘%lu’ expects argument of type ‘long 
>> unsigned int’, but argument 5 has type ‘blkcnt_t {aka long long unsigned 
>> int}’ [-Wformat=]
>> "Inconsistent i_blocks, ino:%lu, iblocks:%lu",
>>  ~~^
>>  %llu
>> inode->i_ino, inode->i_blocks);
>>   ~~~
>> Fixes: cdad635bd068 ("f2fs: fix to avoid panic in f2fs_remove_inode_page()")
> 
> We have already fixed this issue, thanks for noticing this issue. :)
> 

Nice, thanks your reply.

> https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/commit/?h=dev-test&id=5cf2879fa71a865f08348dae982b97b9738a0847
> 
> Thanks,
> 
> .
> 



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [RESEND PATCH] f2fs: Fix one GCC warning

2019-04-22 Thread Chao Yu
On 2019/4/22 21:27, Shaokun Zhang wrote:
> fs/f2fs/node.c: In function ‘f2fs_remove_inode_page’:
> fs/f2fs/node.c:1193:47: warning: format ‘%lu’ expects argument of type ‘long 
> unsigned int’, but argument 5 has type ‘blkcnt_t {aka long long unsigned 
> int}’ [-Wformat=]
> "Inconsistent i_blocks, ino:%lu, iblocks:%lu",
>  ~~^
>  %llu
> inode->i_ino, inode->i_blocks);
>   ~~~
> Fixes: cdad635bd068 ("f2fs: fix to avoid panic in f2fs_remove_inode_page()")

We have already fixed this issue, thanks for noticing this issue. :)

https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/commit/?h=dev-test&id=5cf2879fa71a865f08348dae982b97b9738a0847

Thanks,


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] Possible issues with fsck of f2fs root

2019-04-22 Thread Chao Yu
On 2019/4/22 18:05, Hagbard Celine wrote:
> 2019-04-22 11:26 GMT+02:00, Chao Yu :
>> On 2019/4/22 17:05, Hagbard Celine wrote:
>>> 2019-04-22 9:37 GMT+02:00, Chao Yu :
 On 2019/4/22 15:11, Hagbard Celine wrote:
> With this patch the one problem with opening the device in RO mode is
> fixed.

 Oops, with default preen mode fsck should not open ro mounted image,
 that's
 the
 rule we keep line with ext4...

 How about changing to use -f in your scenario ( on RO mounted root image
 )?
>>>
>>> This was with -f. Without -f it still refuses to open the device.
>>
>> What I mean is we'd better to keep line with ext4, just refusing to open ro
>> mounted device without -f, since triggering fsck and repair on a mounted
>> device
>> is dangerous, it can easily make inconsistency in between in-memory data
>> and
>> on-disk data of filesystem. Refusing fsck without -f is to make user being
>> aware
>> of such danger.
> 
> I am sorry, I've apparently added the -f after my first report. After
> re-testing it seems that fsck.f2fs is opening the RO partition even
> without this patch if I use -f. So the part about fsck.f2fs not being
> able to open RO mounted partition during boot was a user error.

I've sent a patch for your second issue, could you please have a try with it?

[PATCH] fsck.f2fs: fix to repair ro mounted device w/ -f

But one concern is that, with this patch, not like the fsck.ext4, fsck.f2fs
won't show any interaction with below reminding word to remind user to decide
repair or not, it may increase the risk of damaging the device.

Do you want to restore lost files into ./lost_found/?
Do you want to fix this partition? [Y/N]

Jaegeuk, Hagbard,

Any suggestion on this, in current scenario, how about implement:
1. fsck.f2fs -f ro_mounted_device: check; show interaction words if there is
corruption;
2. fsck.f2fs -f -a ro_moutned_device: check and repair automatically;

Thanks,

> 
>>
>> Thanks,
>>
>>>
>>>
 Thanks,

> But as far as I can understand it will still only check the fs, not fix
> it.
>
>
> 2019-04-21 12:27 GMT+02:00, Jaegeuk Kim :
>
>>
>> New version of the patch is:
>>
>> From 3221692b060649378f1f69b898ed85a814af3dbf Mon Sep 17 00:00:00 2001
>> From: Jaegeuk Kim 
>> Date: Tue, 16 Apr 2019 11:46:31 -0700
>> Subject: [PATCH] fsck.f2fs: open ro disk if we want to check fs only
>>
>> This patch fixes the "open failure" issue on ro disk, reported by
>> Hagbard.
>>
>> "
>>  If I boot with kernel option "ro rootfstype=f2fs
>>  I get the following halfway trough boot:
>>
>>   * Checking local filesystems  ...
>>  Info: Use default preen mode
>>  Info: Mounted device!
>>  Info: Check FS only due to RO
>>  Error: Failed to open the device!
>>   * Filesystems couldn't be fixed
>> "
>>
>> Reported-by: Hagbard Celine 
>> Signed-off-by: Jaegeuk Kim 
>> ---
>>  lib/libf2fs.c | 25 +
>>  1 file changed, 21 insertions(+), 4 deletions(-)
>>
>> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
>> index d30047f..853e713 100644
>> --- a/lib/libf2fs.c
>> +++ b/lib/libf2fs.c
>> @@ -789,6 +789,15 @@ void get_kernel_uname_version(__u8 *version)
>>  #endif /* APPLE_DARWIN */
>>
>>  #ifndef ANDROID_WINDOWS_HOST
>> +static int open_check_fs(char *path, int flag)
>> +{
>> +if (c.func != FSCK || c.fix_on || c.auto_fix)
>> +return -1;
>> +
>> +/* allow to open ro */
>> +return open(path, O_RDONLY | flag);
>> +}
>> +
>>  int get_device_info(int i)
>>  {
>>  int32_t fd = 0;
>> @@ -810,8 +819,11 @@ int get_device_info(int i)
>>  if (c.sparse_mode) {
>>  fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
>>  if (fd < 0) {
>> -MSG(0, "\tError: Failed to open a sparse 
>> file!\n");
>> -return -1;
>> +fd = open_check_fs(dev->path, O_BINARY);
>> +if (fd < 0) {
>> +MSG(0, "\tError: Failed to open a 
>> sparse file!\n");
>> +return -1;
>> +}
>>  }
>>  }
>>
>> @@ -825,10 +837,15 @@ int get_device_info(int i)
>>  return -1;
>>  }
>>
>> -if (S_ISBLK(stat_buf->st_mode) && !c.force)
>> +if (S_ISBLK(stat_buf->st_mode) && !c.force) {
>>  fd = open(dev->path, O_RDWR | O_EXCL);
>> -else
>> +if (fd < 0)
>> +fd = open_check_fs(dev->path, O_EXCL);
>> +} else {
>>  

[f2fs-dev] [PATCH] fsck.f2fs: fix to repair ro mounted device w/ -f

2019-04-22 Thread Chao Yu
As Hagbard Celine reported:

"
Referring to the output from the fsck running against a "ro"
filesystem, especially this line:
Info: Check FS only due to RO

As far as i can tell this says that opposed to other filesystems
running fsck against a "ro" mounted f2fs partition will never fix any
errors.
So I tried running fsck against the same partition mounted "rw":
- mount -o remount,rw /mnt/f2fstest/
- fsck.f2fs  -f /dev/nvme0n1p7
Info: Force to fix corruption
Info: Mounted device!
Error: Not available on mounted device!

I might be misunderstanding something, but all this tells me that
unless one make a custom initramfs that runs fsck before root is
mounted (something no distributions has, as far as I know), fsck will
never fix an f2fs formatted root partition during boot.
If this is by design and not a bug/unintended behavior, it should be
documented somewhere least more people will experience system crashes
like mine.

All tests above done with kernel 5.0.5 and f2fs-tools 1.12.0 with
"fsck.f2fs: allow to fsck readonly image w/ -f option"-patch by Chao
Yu.
"

We try to make our fsck behavior keeping line with e2fsprogs, but w/
-f option, we can just check a RO mounted device rather repair it, so
let's fix this.

Signed-off-by: Chao Yu 
---
 fsck/fsck.c   | 20 ++--
 fsck/main.c   | 10 +++---
 include/f2fs_fs.h |  1 +
 lib/libf2fs.c |  5 +
 4 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index 8d7deb5..a1791d4 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -199,7 +199,7 @@ static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, 
u32 nid,
need_fix = 1;
}
}
-   if (need_fix && !c.ro) {
+   if (need_fix && f2fs_dev_is_writable()) {
u64 ssa_blk;
int ret2;
 
@@ -325,7 +325,7 @@ static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, 
u32 blk_addr,
need_fix = 1;
}
}
-   if (need_fix && !c.ro) {
+   if (need_fix && f2fs_dev_is_writable()) {
u64 ssa_blk;
int ret2;
 
@@ -1015,7 +1015,7 @@ skip_blkcnt_fix:
}
 
/* drop extent information to avoid potential wrong access */
-   if (need_fix && !c.ro)
+   if (need_fix && f2fs_dev_is_writable())
node_blk->i.i_ext.len = 0;
 
if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
@@ -1038,7 +1038,7 @@ skip_blkcnt_fix:
}
}
 
-   if (need_fix && !c.ro) {
+   if (need_fix && f2fs_dev_is_writable()) {
ret = dev_write_block(node_blk, ni->blk_addr);
ASSERT(ret >= 0);
}
@@ -1073,7 +1073,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 && !c.ro) {
+   if (need_fix && f2fs_dev_is_writable()) {
ret = dev_write_block(node_blk, ni->blk_addr);
ASSERT(ret >= 0);
}
@@ -1110,7 +1110,7 @@ skip:
}
}
 
-   if (need_fix && !c.ro) {
+   if (need_fix && f2fs_dev_is_writable()) {
struct node_info ni;
nid_t nid = le32_to_cpu(node_blk->footer.nid);
 
@@ -1152,7 +1152,7 @@ skip:
}
}
 
-   if (need_fix && !c.ro) {
+   if (need_fix && f2fs_dev_is_writable()) {
struct node_info ni;
nid_t nid = le32_to_cpu(node_blk->footer.nid);
 
@@ -1595,7 +1595,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, enc_name);
 
-   if (dentries < 0 && !c.ro) {
+   if (dentries < 0 && f2fs_dev_is_writable()) {
ret = dev_write_block(de_blk, blk_addr);
ASSERT(ret >= 0);
DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
@@ -1708,7 +1708,7 @@ int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
else if (ret)
ASSERT_MSG("[0x%x] wrong orphan inode", ino);
}
-   if (!c.ro && c.fix_on &&
+   if (f2fs_dev_is_writable() && c.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);
@@ -2719,7 +2719,7 @@ int fsck_verify(struct f2fs_sb_info *sbi)
}
 #endif
/* fix global metadata */
-   if (force || (c.fix_on && !c.ro)) {
+   if (force || (c.fix_on && f2fs_dev_is_writable())) {
struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
 
if (force || c.bug_on || c.bug_nat_bits) {
diff --git a/fsck/main.c b/fsck/main.c
index e61bb91..afdfec9 100644

Re: [f2fs-dev] [Shaokun Zhang] f2fs: Fix one GCC warning

2019-04-22 Thread Zhangshaokun
please ignore this.

Thanks,
Shaokun

On 2019/4/22 21:25, Shaokun Zhang wrote:
> fs/f2fs/node.c: In function ‘f2fs_remove_inode_page’:
> fs/f2fs/node.c:1193:47: warning: format ‘%lu’ expects argument of type ‘long 
> unsigned int’, but argument 5 has type ‘blkcnt_t {aka long long unsigned 
> int}’ [-Wformat=]
> "Inconsistent i_blocks, ino:%lu, iblocks:%lu",
>  ~~^
>  %llu
> inode->i_ino, inode->i_blocks);
>   ~~~
> GCC version is gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-6).
> 
> Fixes: cdad635bd068 ("f2fs: fix to avoid panic in f2fs_remove_inode_page()")
> Cc: Jaegeuk Kim 
> Cc: Chao Yu 
> Signed-off-by: Shaokun Zhang 
> ---
>  fs/f2fs/node.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> index 22d21c546bf6..e15d0be729b4 100644
> --- a/fs/f2fs/node.c
> +++ b/fs/f2fs/node.c
> @@ -1190,7 +1190,7 @@ int f2fs_remove_inode_page(struct inode *inode)
>  
>   if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
>   f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
> - "Inconsistent i_blocks, ino:%lu, iblocks:%lu",
> + "Inconsistent i_blocks, ino:%lu, iblocks:%llu",
>   inode->i_ino, inode->i_blocks);
>   set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
>   }
> 



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [RESEND PATCH] f2fs: Fix one GCC warning

2019-04-22 Thread Shaokun Zhang
fs/f2fs/node.c: In function ‘f2fs_remove_inode_page’:
fs/f2fs/node.c:1193:47: warning: format ‘%lu’ expects argument of type ‘long 
unsigned int’, but argument 5 has type ‘blkcnt_t {aka long long unsigned int}’ 
[-Wformat=]
"Inconsistent i_blocks, ino:%lu, iblocks:%lu",
 ~~^
 %llu
inode->i_ino, inode->i_blocks);
  ~~~
Fixes: cdad635bd068 ("f2fs: fix to avoid panic in f2fs_remove_inode_page()")
Cc: Jaegeuk Kim 
Cc: Chao Yu 
Signed-off-by: Shaokun Zhang 
---
 fs/f2fs/node.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index 22d21c546bf6..e15d0be729b4 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -1190,7 +1190,7 @@ int f2fs_remove_inode_page(struct inode *inode)
 
if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
-   "Inconsistent i_blocks, ino:%lu, iblocks:%lu",
+   "Inconsistent i_blocks, ino:%lu, iblocks:%llu",
inode->i_ino, inode->i_blocks);
set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
}
-- 
2.7.4



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [Shaokun Zhang] f2fs: Fix one GCC warning

2019-04-22 Thread Shaokun Zhang
fs/f2fs/node.c: In function ‘f2fs_remove_inode_page’:
fs/f2fs/node.c:1193:47: warning: format ‘%lu’ expects argument of type ‘long 
unsigned int’, but argument 5 has type ‘blkcnt_t {aka long long unsigned int}’ 
[-Wformat=]
"Inconsistent i_blocks, ino:%lu, iblocks:%lu",
 ~~^
 %llu
inode->i_ino, inode->i_blocks);
  ~~~
GCC version is gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-6).

Fixes: cdad635bd068 ("f2fs: fix to avoid panic in f2fs_remove_inode_page()")
Cc: Jaegeuk Kim 
Cc: Chao Yu 
Signed-off-by: Shaokun Zhang 
---
 fs/f2fs/node.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index 22d21c546bf6..e15d0be729b4 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -1190,7 +1190,7 @@ int f2fs_remove_inode_page(struct inode *inode)
 
if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
-   "Inconsistent i_blocks, ino:%lu, iblocks:%lu",
+   "Inconsistent i_blocks, ino:%lu, iblocks:%llu",
inode->i_ino, inode->i_blocks);
set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
}
-- 
2.7.4



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH 1/3] f2fs: fix to consider multiple device for readonly check

2019-04-22 Thread Chao Yu
This patch introduce f2fs_hw_is_readonly() to check whether lower
device is readonly or not, it adapts multiple device scenario.

Signed-off-by: Chao Yu 
---
 fs/f2fs/f2fs.h  | 14 ++
 fs/f2fs/super.c |  2 +-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index e066f48b3f67..a73e7340a62f 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -3590,6 +3590,20 @@ static inline bool f2fs_realtime_discard_enable(struct 
f2fs_sb_info *sbi)
f2fs_hw_should_discard(sbi);
 }
 
+static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
+{
+   int i;
+
+   if (!f2fs_is_multi_device(sbi))
+   return bdev_read_only(sbi->sb->s_bdev);
+
+   for (i = 0; i < sbi->s_ndevs; i++)
+   if (bdev_read_only(FDEV(i).bdev))
+   return true;
+   return false;
+}
+
+
 static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
 {
clear_opt(sbi, ADAPTIVE);
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index cbbb1e35070d..0c3abcc0dd27 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -3377,7 +3377,7 @@ static int f2fs_fill_super(struct super_block *sb, void 
*data, int silent)
 * mount should be failed, when device has readonly mode, and
 * previous checkpoint was not done by clean system shutdown.
 */
-   if (bdev_read_only(sb->s_bdev) &&
+   if (f2fs_hw_is_readonly(sbi) &&
!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
err = -EROFS;
goto free_meta;
-- 
2.18.0.rc1



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH 2/3] f2fs: fix to skip recovery on readonly device

2019-04-22 Thread Chao Yu
As Park Ju Hyung reported in mailing list:

https://sourceforge.net/p/linux-f2fs/mailman/message/36639787/

generic_make_request: Trying to write to read-only block-device loop0 (partno 0)
WARNING: CPU: 0 PID: 23437 at block/blk-core.c:2174 
generic_make_request_checks+0x594/0x630

 generic_make_request+0x46/0x3d0
 submit_bio+0x30/0x110
 __submit_merged_bio+0x68/0x390
 f2fs_submit_page_write+0x1bb/0x7f0
 f2fs_do_write_meta_page+0x7f/0x160
 __f2fs_write_meta_page+0x70/0x140
 f2fs_sync_meta_pages+0x140/0x250
 f2fs_write_checkpoint+0x5c5/0x17b0
 f2fs_sync_fs+0x9c/0x110
 sync_filesystem+0x66/0x80
 f2fs_recover_fsync_data+0x790/0xa30
 f2fs_fill_super+0xe4e/0x1980
 mount_bdev+0x518/0x610
 mount_fs+0x34/0x13f
 vfs_kern_mount.part.11+0x4f/0x120
 do_mount+0x2d1/0xe40
 __x64_sys_mount+0xbf/0xe0
 do_syscall_64+0x4a/0xf0
 entry_SYSCALL_64_after_hwframe+0x44/0xa9

print_req_error: I/O error, dev loop0, sector 4096

If block device is readonly, we should never trigger write IO from
filesystem layer, but previously, orphan and journal recovery didn't
consider such condition, result in triggering above warning, fix it.

Reported-by: Park Ju Hyung 
Tested-by: Park Ju Hyung 
Signed-off-by: Chao Yu 
---
 fs/f2fs/checkpoint.c |  6 ++
 fs/f2fs/super.c  | 15 +++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index a7ad1b1e5750..90e1bab86269 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -674,6 +674,12 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
if (!is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
return 0;
 
+   if (bdev_read_only(sbi->sb->s_bdev)) {
+   f2fs_msg(sbi->sb, KERN_INFO, "write access "
+   "unavailable, skipping orphan cleanup");
+   return 0;
+   }
+
if (s_flags & SB_RDONLY) {
f2fs_msg(sbi->sb, KERN_INFO, "orphan cleanup on readonly fs");
sbi->sb->s_flags &= ~SB_RDONLY;
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 0c3abcc0dd27..6a89ee55efe3 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -3377,10 +3377,17 @@ static int f2fs_fill_super(struct super_block *sb, void 
*data, int silent)
 * mount should be failed, when device has readonly mode, and
 * previous checkpoint was not done by clean system shutdown.
 */
-   if (f2fs_hw_is_readonly(sbi) &&
-   !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
-   err = -EROFS;
-   goto free_meta;
+   if (f2fs_hw_is_readonly(sbi)) {
+   if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
+   err = -EROFS;
+   f2fs_msg(sb, KERN_ERR,
+   "Need to recover fsync data, but "
+   "write access unavailable");
+   goto free_meta;
+   }
+   f2fs_msg(sbi->sb, KERN_INFO, "write access "
+   "unavailable, skipping recovery");
+   goto reset_checkpoint;
}
 
if (need_fsck)
-- 
2.18.0.rc1



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH 3/3] f2fs: fix to be aware of readonly device in write_checkpoint()

2019-04-22 Thread Chao Yu
As Park Ju Hyung reported:

Probably unrelated but a similar issue:
Warning appears upon unmounting a corrupted R/O f2fs loop image.

Should be a trivial issue to fix as well :)

[ 2373.758424] [ cut here ]
[ 2373.758428] generic_make_request: Trying to write to read-only
block-device loop1 (partno 0)
[ 2373.758455] WARNING: CPU: 1 PID: 13950 at block/blk-core.c:2174
generic_make_request_checks+0x590/0x630
[ 2373.758556] CPU: 1 PID: 13950 Comm: umount Tainted: G   O
   4.19.35-zen+ #1
[ 2373.758558] Hardware name: System manufacturer System Product
Name/ROG MAXIMUS X HERO (WI-FI AC), BIOS 1704 09/14/2018
[ 2373.758564] RIP: 0010:generic_make_request_checks+0x590/0x630
[ 2373.758567] Code: 5c 03 00 00 48 8d 74 24 08 48 89 df c6 05 b5 cd
36 01 01 e8 c2 90 01 00 48 89 c6 44 89 ea 48 c7 c7 98 64 59 82 e8 d5
9b a7 ff <0f> 0b 48 8b 7b 08 e9 f2 fa ff ff 41 8b 86 98 02 00 00 49 8b
16 89
[ 2373.758570] RSP: 0018:8882bdb43950 EFLAGS: 00010282
[ 2373.758573] RAX: 0050 RBX: 8887244c6700 RCX: 0006
[ 2373.758575] RDX: 0007 RSI: 0086 RDI: 4ec56340
[ 2373.758577] RBP: 49c426c0 R08: 0004 R09: 03ba
[ 2373.758579] R10: 0001 R11: 0029 R12: 1000
[ 2373.758581] R13:  R14: 44a2e800 R15: 8882bdb43ac0
[ 2373.758584] FS:  7fc0d114f8c0() GS:4ec4()
knlGS:
[ 2373.758586] CS:  0010 DS:  ES:  CR0: 80050033
[ 2373.758588] CR2: 7fc0d1ad12c0 CR3: 0002bdb82003 CR4: 003606e0
[ 2373.758590] DR0:  DR1:  DR2: 
[ 2373.758592] DR3:  DR6: fffe0ff0 DR7: 0400
[ 2373.758593] Call Trace:
[ 2373.758602]  ? generic_make_request+0x46/0x3d0
[ 2373.758608]  ? wait_woken+0x80/0x80
[ 2373.758612]  ? mempool_alloc+0xb7/0x1a0
[ 2373.758618]  ? submit_bio+0x30/0x110
[ 2373.758622]  ? bvec_alloc+0x7c/0xd0
[ 2373.758628]  ? __submit_merged_bio+0x68/0x390
[ 2373.758633]  ? f2fs_submit_page_write+0x1bb/0x7f0
[ 2373.758638]  ? f2fs_do_write_meta_page+0x7f/0x160
[ 2373.758642]  ? __f2fs_write_meta_page+0x70/0x140
[ 2373.758647]  ? f2fs_sync_meta_pages+0x140/0x250
[ 2373.758653]  ? f2fs_write_checkpoint+0x5c5/0x17b0
[ 2373.758657]  ? f2fs_sync_fs+0x9c/0x110
[ 2373.758664]  ? sync_filesystem+0x66/0x80
[ 2373.758667]  ? generic_shutdown_super+0x1d/0x100
[ 2373.758670]  ? kill_block_super+0x1c/0x40
[ 2373.758674]  ? kill_f2fs_super+0x64/0xb0
[ 2373.758678]  ? deactivate_locked_super+0x2d/0xb0
[ 2373.758682]  ? cleanup_mnt+0x65/0xa0
[ 2373.758688]  ? task_work_run+0x7f/0xa0
[ 2373.758693]  ? exit_to_usermode_loop+0x9c/0xa0
[ 2373.758698]  ? do_syscall_64+0xc7/0xf0
[ 2373.758703]  ? entry_SYSCALL_64_after_hwframe+0x44/0xa9
[ 2373.758706] ---[ end trace 5d3639907c56271b ]---
[ 2373.758780] print_req_error: I/O error, dev loop1, sector 143048
[ 2373.758800] print_req_error: I/O error, dev loop1, sector 152200
[ 2373.758808] print_req_error: I/O error, dev loop1, sector 8192
[ 2373.758819] print_req_error: I/O error, dev loop1, sector 12272

This patch adds to detect readonly device in write_checkpoint() to avoid
trigger write IOs on it.

Reported-by: Park Ju Hyung 
Signed-off-by: Chao Yu 
---
 fs/f2fs/checkpoint.c | 7 +++
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index 90e1bab86269..ca7da012de68 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -1534,6 +1534,9 @@ int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, 
struct cp_control *cpc)
unsigned long long ckpt_ver;
int err = 0;
 
+   if (f2fs_readonly(sbi->sb) || f2fs_hw_is_readonly(sbi))
+   return -EROFS;
+
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
if (cpc->reason != CP_PAUSE)
return 0;
@@ -1550,10 +1553,6 @@ int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, 
struct cp_control *cpc)
err = -EIO;
goto out;
}
-   if (f2fs_readonly(sbi->sb)) {
-   err = -EROFS;
-   goto out;
-   }
 
trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
 
-- 
2.18.0.rc1



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH 2/2] fs: f2fs: insert space before the open parenthesis '('

2019-04-22 Thread Chao Yu
Original patch was Ccing to wrong f2fs mailing list address, correct it.

On 2019/4/20 21:51, Youngjun Yoo wrote:
> Modify coding style
> ERROR: space required before the open parenthesis '('
> 
> Signed-off-by: Youngjun Yoo 

Reviewed-by: Chao Yu 

Thanks,


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH 1/2] fs: f2fs: Replace spaces with tab.

2019-04-22 Thread Chao Yu
On 2019/4/20 21:50, Youngjun Yoo wrote:
> Modify coding style
> ERROR: code indent should use tabs where possible
> 
> Signed-off-by: Youngjun Yoo 

Reviewed-by: Chao Yu 

Thanks,


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] Possible issues with fsck of f2fs root

2019-04-22 Thread Hagbard Celine
2019-04-22 11:26 GMT+02:00, Chao Yu :
> On 2019/4/22 17:05, Hagbard Celine wrote:
>> 2019-04-22 9:37 GMT+02:00, Chao Yu :
>>> On 2019/4/22 15:11, Hagbard Celine wrote:
 With this patch the one problem with opening the device in RO mode is
 fixed.
>>>
>>> Oops, with default preen mode fsck should not open ro mounted image,
>>> that's
>>> the
>>> rule we keep line with ext4...
>>>
>>> How about changing to use -f in your scenario ( on RO mounted root image
>>> )?
>>
>> This was with -f. Without -f it still refuses to open the device.
>
> What I mean is we'd better to keep line with ext4, just refusing to open ro
> mounted device without -f, since triggering fsck and repair on a mounted
> device
> is dangerous, it can easily make inconsistency in between in-memory data
> and
> on-disk data of filesystem. Refusing fsck without -f is to make user being
> aware
> of such danger.

I am sorry, I've apparently added the -f after my first report. After
re-testing it seems that fsck.f2fs is opening the RO partition even
without this patch if I use -f. So the part about fsck.f2fs not being
able to open RO mounted partition during boot was a user error.

>
> Thanks,
>
>>
>>
>>> Thanks,
>>>
 But as far as I can understand it will still only check the fs, not fix
 it.


 2019-04-21 12:27 GMT+02:00, Jaegeuk Kim :

>
> New version of the patch is:
>
> From 3221692b060649378f1f69b898ed85a814af3dbf Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim 
> Date: Tue, 16 Apr 2019 11:46:31 -0700
> Subject: [PATCH] fsck.f2fs: open ro disk if we want to check fs only
>
> This patch fixes the "open failure" issue on ro disk, reported by
> Hagbard.
>
> "
>  If I boot with kernel option "ro rootfstype=f2fs
>  I get the following halfway trough boot:
>
>   * Checking local filesystems  ...
>  Info: Use default preen mode
>  Info: Mounted device!
>  Info: Check FS only due to RO
>  Error: Failed to open the device!
>   * Filesystems couldn't be fixed
> "
>
> Reported-by: Hagbard Celine 
> Signed-off-by: Jaegeuk Kim 
> ---
>  lib/libf2fs.c | 25 +
>  1 file changed, 21 insertions(+), 4 deletions(-)
>
> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
> index d30047f..853e713 100644
> --- a/lib/libf2fs.c
> +++ b/lib/libf2fs.c
> @@ -789,6 +789,15 @@ void get_kernel_uname_version(__u8 *version)
>  #endif /* APPLE_DARWIN */
>
>  #ifndef ANDROID_WINDOWS_HOST
> +static int open_check_fs(char *path, int flag)
> +{
> + if (c.func != FSCK || c.fix_on || c.auto_fix)
> + return -1;
> +
> + /* allow to open ro */
> + return open(path, O_RDONLY | flag);
> +}
> +
>  int get_device_info(int i)
>  {
>   int32_t fd = 0;
> @@ -810,8 +819,11 @@ int get_device_info(int i)
>   if (c.sparse_mode) {
>   fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
>   if (fd < 0) {
> - MSG(0, "\tError: Failed to open a sparse file!\n");
> - return -1;
> + fd = open_check_fs(dev->path, O_BINARY);
> + if (fd < 0) {
> + MSG(0, "\tError: Failed to open a sparse 
> file!\n");
> + return -1;
> + }
>   }
>   }
>
> @@ -825,10 +837,15 @@ int get_device_info(int i)
>   return -1;
>   }
>
> - if (S_ISBLK(stat_buf->st_mode) && !c.force)
> + if (S_ISBLK(stat_buf->st_mode) && !c.force) {
>   fd = open(dev->path, O_RDWR | O_EXCL);
> - else
> + if (fd < 0)
> + fd = open_check_fs(dev->path, O_EXCL);
> + } else {
>   fd = open(dev->path, O_RDWR);
> + if (fd < 0)
> + fd = open_check_fs(dev->path, 0);
> + }
>   }
>   if (fd < 0) {
>   MSG(0, "\tError: Failed to open the device!\n");
> --
> 2.19.0.605.g01d371f741-goog
>
>


 ___
 Linux-f2fs-devel mailing list
 Linux-f2fs-devel@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
 .

>>>
>> .
>>
>


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH 1/2] f2fs-tools: allow unfixed f2fs_checkpoint.checksum_offset

2019-04-22 Thread Chao Yu
Previously, f2fs_checkpoint.checksum_offset points fixed position of
f2fs_checkpoint structure:

"#define CP_CHKSUM_OFFSET   4092"

It is unnecessary, and it breaks the consecutiveness of nat and sit
bitmap stored across checkpoint park block and payload blocks.

This patch allows f2fs-tools to handle unfixed .checksum_offset.

In addition, for the case checksum value is stored in the middle of
checkpoint park, calculating checksum value with superposition method
like we did for inode_checksum.

In addition, using MAX_BITMAP_SIZE_IN_CKPT to clean up codes.

Signed-off-by: Chao Yu 
---
 fsck/fsck.c|  5 +++--
 fsck/mount.c   |  5 +++--
 fsck/resize.c  | 10 +-
 include/f2fs_fs.h  |  1 +
 lib/libf2fs.c  | 14 ++
 mkfs/f2fs_format.c | 16 +++-
 6 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index a17555c..7ecdee1 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2056,8 +2056,9 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi)
set_cp(valid_node_count, fsck->chk.valid_node_cnt);
set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
 
-   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_OFFSET)) = 
cpu_to_le32(crc);
+   crc = f2fs_checkpoint_chksum(cp);
+   *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
+   cpu_to_le32(crc);
 
cp_blk_no = get_sb(cp_blkaddr);
if (sbi->cur_cp == 2)
diff --git a/fsck/mount.c b/fsck/mount.c
index aa64e93..da1d4c9 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2373,8 +2373,9 @@ void write_checkpoint(struct f2fs_sb_info *sbi)
flags = update_nat_bits_flags(sb, cp, flags);
set_cp(ckpt_flags, flags);
 
-   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_OFFSET)) = 
cpu_to_le32(crc);
+   crc = f2fs_checkpoint_chksum(cp);
+   *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
+   cpu_to_le32(crc);
 
cp_blk_no = get_sb(cp_blkaddr);
if (sbi->cur_cp == 2)
diff --git a/fsck/resize.c b/fsck/resize.c
index 56c8103..2c623c5 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,11 +90,11 @@ static int get_new_sb(struct f2fs_super_block *sb)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
-   - max_sit_bitmap_size;
+   max_nat_bitmap_size = MAX_BITMAP_SIZE_IN_CKPT -
+   max_sit_bitmap_size;
set_sb(cp_payload, 0);
}
 
@@ -520,8 +520,8 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi,
(unsigned char *)cp);
new_cp->checkpoint_ver = cpu_to_le64(cp_ver + 1);
 
-   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_OFFSET)) =
+   crc = f2fs_checkpoint_chksum(cp);
+   *((__le32 *)((unsigned char *)new_cp + get_cp(checksum_offset))) =
cpu_to_le32(crc);
 
/* Write a new checkpoint in the other set */
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index c6669c9..1bd935c 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -1133,6 +1133,7 @@ extern int utf16_to_utf8(char *, const u_int16_t *, 
size_t, size_t);
 extern int log_base_2(u_int32_t);
 extern unsigned int addrs_per_inode(struct f2fs_inode *);
 extern __u32 f2fs_inode_chksum(struct f2fs_node *);
+extern __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *);
 
 extern int get_bits_in_byte(unsigned char n);
 extern int test_and_set_bit_le(u32, u8 *);
diff --git a/lib/libf2fs.c b/lib/libf2fs.c
index 853e713..4202cd1 100644
--- a/lib/libf2fs.c
+++ b/lib/libf2fs.c
@@ -532,6 +532,20 @@ __u32 f2fs_inode_chksum(struct f2fs_node *node)
return chksum;
 }
 
+__u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *cp)
+{
+   unsigned int chksum_ofs = le32_to_cpu(cp->checksum_offset);
+   __u32 chksum;
+
+   chksum = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, chksum_ofs);
+   if (chksum_ofs < CP_CHKSUM_OFFSET) {
+   chksum_ofs += sizeof(chksum);
+   chksum = f2fs_cal_crc32(chksum, (__u8 *)cp + chksum_ofs,
+   F2FS

[f2fs-dev] [PATCH 2/2] f2fs-tools: relocate chksum_offset for large_nat_bitmap feature

2019-04-22 Thread Chao Yu
For large_nat_bitmap feature, there is a design flaw:

Previous:

struct f2fs_checkpoint layout:
+--+  0x
| checkpoint_ver   |
| ..   |
| checksum_offset  |--+
| ..   |  |
| sit_nat_version_bitmap[] |<-|---+
| ..   |  |   |
| checksum_value   |<-+   |
+--+  0x1000  |
|  |  nat_bitmap + sit_bitmap
| payload blocks   |  |
|  |  |
+--|<-+

Obviously, if nat_bitmap size + sit_bitmap size is larger than
MAX_BITMAP_SIZE_IN_CKPT, nat_bitmap or sit_bitmap may overlap
checkpoint checksum's position, once checkpoint() is triggered
from kernel, nat or sit bitmap will be damaged by checksum field.

In order to fix this, let's relocate checksum_value's position
to the head of sit_nat_version_bitmap as below, then nat/sit
bitmap and chksum value update will become safe.

After:

struct f2fs_checkpoint layout:
+--+  0x
| checkpoint_ver   |
| ..   |
| checksum_offset  |--+
| ..   |  |
| sit_nat_version_bitmap[] |<-+
| ..   |<-+
|  |  |
+--+  0x1000  |
|  |  nat_bitmap + sit_bitmap
| payload blocks   |  |
|  |  |
+--|<-+

Related report and discussion:

https://sourceforge.net/p/linux-f2fs/mailman/message/36642346/

In addition, during writing checkpoint, if large_nat_bitmap feature is
enabled, we need to set CP_LARGE_NAT_BITMAP_FLAG flag in checkpoint.

Reported-by: Park Ju Hyung 
Signed-off-by: Chao Yu 
---
 fsck/f2fs.h|  6 +-
 fsck/fsck.c| 16 
 fsck/fsck.h|  1 +
 fsck/main.c|  2 ++
 fsck/mount.c   | 42 +++---
 include/f2fs_fs.h  |  9 +++--
 mkfs/f2fs_format.c |  5 -
 7 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/fsck/f2fs.h b/fsck/f2fs.h
index 93f01e5..6a6c4a5 100644
--- a/fsck/f2fs.h
+++ b/fsck/f2fs.h
@@ -272,7 +272,11 @@ static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, 
int flag)
if (is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG)) {
offset = (flag == SIT_BITMAP) ?
le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
-   return &ckpt->sit_nat_version_bitmap + offset;
+   /*
+* if large_nat_bitmap feature is enabled, leave checksum
+* protection for all nat/sit bitmaps.
+*/
+   return &ckpt->sit_nat_version_bitmap + offset + sizeof(__le32);
}
 
if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
diff --git a/fsck/fsck.c b/fsck/fsck.c
index 7ecdee1..8d7deb5 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -1917,6 +1917,18 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)
return 0;
 }
 
+void fsck_chk_checkpoint(struct f2fs_sb_info *sbi)
+{
+   struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
+
+   if (get_cp(ckpt_flags) & CP_LARGE_NAT_BITMAP_FLAG) {
+   if (get_cp(checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
+   ASSERT_MSG("Deprecated layout of large_nat_bitmap, "
+   "chksum_offset:%u", get_cp(checksum_offset));
+   }
+   }
+}
+
 void fsck_init(struct f2fs_sb_info *sbi)
 {
struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
@@ -2038,6 +2050,10 @@ static void fix_checkpoint(struct f2fs_sb_info *sbi)
flags |= CP_TRIMMED_FLAG;
if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
flags |= CP_DISABLED_FLAG;
+   if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
+   flags |= CP_LARGE_NAT_BITMAP_FLAG;
+   set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
+   }
 
if (flags & CP_UMOUNT_FLAG)
cp_blocks = 8;
diff --git a/fsck/fsck.h b/fsck/fsck.h
index 376ced7..dd831de 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -154,6 +154,7 @@ extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, u32, 
struct child_info *,
int, int);
 int fsck_chk_inline_dentries(struct f2fs_sb_info *, struct f2fs_node *,
struct child_info *);
+void fsck_chk_checkpoint(struct f2fs_sb_info *sbi);
 int fsck_chk_meta(struct f2fs_sb_info *sbi);
 int fsck_chk_curseg_info(struct f2fs_sb_info *);
 int convert_encrypted_name(unsigned char *, u32, unsigned char *, int);
diff --git a/fsck/main.c b/fsck/main.c
index d3f0c0d..e61bb91 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -616,6 +616,8 @@ static void do_fsck(struct f2fs_sb_info *sbi)
c.fix_on = 1;
}
 
+   fsck_chk_checkpoint(s

[f2fs-dev] [PATCH 1/2] f2fs: allow unfixed f2fs_checkpoint.checksum_offset

2019-04-22 Thread Chao Yu
Previously, f2fs_checkpoint.checksum_offset points fixed position of
f2fs_checkpoint structure:

"#define CP_CHKSUM_OFFSET   4092"

It is unnecessary, and it breaks the consecutiveness of nat and sit
bitmap stored across checkpoint park block and payload blocks.

This patch allows f2fs to handle unfixed .checksum_offset.

In addition, for the case checksum value is stored in the middle of
checkpoint park, calculating checksum value with superposition method
like we did for inode_checksum.

Signed-off-by: Chao Yu 
---
 fs/f2fs/checkpoint.c| 27 +--
 include/linux/f2fs_fs.h |  4 
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index 441814607b13..a25556aef8cc 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -794,13 +794,27 @@ static void write_orphan_inodes(struct f2fs_sb_info *sbi, 
block_t start_blk)
}
 }
 
+static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
+   struct f2fs_checkpoint *ckpt)
+{
+   unsigned int chksum_ofs = le32_to_cpu(ckpt->checksum_offset);
+   __u32 chksum;
+
+   chksum = f2fs_crc32(sbi, ckpt, chksum_ofs);
+   if (chksum_ofs < CP_CHKSUM_OFFSET) {
+   chksum_ofs += sizeof(chksum);
+   chksum = f2fs_chksum(sbi, chksum, (__u8 *)ckpt + chksum_ofs,
+   F2FS_BLKSIZE - chksum_ofs);
+   }
+   return chksum;
+}
+
 static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
struct f2fs_checkpoint **cp_block, struct page **cp_page,
unsigned long long *version)
 {
-   unsigned long blk_size = sbi->blocksize;
size_t crc_offset = 0;
-   __u32 crc = 0;
+   __u32 crc;
 
*cp_page = f2fs_get_meta_page(sbi, cp_addr);
if (IS_ERR(*cp_page))
@@ -809,15 +823,16 @@ static int get_checkpoint_version(struct f2fs_sb_info 
*sbi, block_t cp_addr,
*cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
 
crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
-   if (crc_offset > (blk_size - sizeof(__le32))) {
+   if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
+   crc_offset > CP_CHKSUM_OFFSET) {
f2fs_put_page(*cp_page, 1);
f2fs_msg(sbi->sb, KERN_WARNING,
"invalid crc_offset: %zu", crc_offset);
return -EINVAL;
}
 
-   crc = cur_cp_crc(*cp_block);
-   if (!f2fs_crc_valid(sbi, crc, *cp_block, crc_offset)) {
+   crc = f2fs_checkpoint_chksum(sbi, *cp_block);
+   if (crc != cur_cp_crc(*cp_block)) {
f2fs_put_page(*cp_page, 1);
f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
return -EINVAL;
@@ -1425,7 +1440,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct 
cp_control *cpc)
get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
 
-   crc32 = f2fs_crc32(sbi, ckpt, le32_to_cpu(ckpt->checksum_offset));
+   crc32 = f2fs_checkpoint_chksum(sbi, ckpt);
*((__le32 *)((unsigned char *)ckpt +
le32_to_cpu(ckpt->checksum_offset)))
= cpu_to_le32(crc32);
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index 55da9abed023..65559900d4d7 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -164,6 +164,10 @@ struct f2fs_checkpoint {
unsigned char sit_nat_version_bitmap[1];
 } __packed;
 
+#define CP_CHKSUM_OFFSET   4092/* default chksum offset in checkpoint 
*/
+#define CP_MIN_CHKSUM_OFFSET   \
+   (offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap))
+
 /*
  * For orphan inode management
  */
-- 
2.18.0.rc1



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH 2/2] f2fs: relocate chksum_offset for large_nat_bitmap feature

2019-04-22 Thread Chao Yu
For large_nat_bitmap feature, there is a design flaw:

Previous:

struct f2fs_checkpoint layout:
+--+  0x
| checkpoint_ver   |
| ..   |
| checksum_offset  |--+
| ..   |  |
| sit_nat_version_bitmap[] |<-|---+
| ..   |  |   |
| checksum_value   |<-+   |
+--+  0x1000  |
|  |  nat_bitmap + sit_bitmap
| payload blocks   |  |
|  |  |
+--|<-+

Obviously, if nat_bitmap size + sit_bitmap size is larger than
MAX_BITMAP_SIZE_IN_CKPT, nat_bitmap or sit_bitmap may overlap
checkpoint checksum's position, once checkpoint() is triggered
from kernel, nat or sit bitmap will be damaged by checksum field.

In order to fix this, let's relocate checksum_value's position
to the head of sit_nat_version_bitmap as below, then nat/sit
bitmap and chksum value update will become safe.

After:

struct f2fs_checkpoint layout:
+--+  0x
| checkpoint_ver   |
| ..   |
| checksum_offset  |--+
| ..   |  |
| sit_nat_version_bitmap[] |<-+
| ..   |<-+
|  |  |
+--+  0x1000  |
|  |  nat_bitmap + sit_bitmap
| payload blocks   |  |
|  |  |
+--|<-+

Related report and discussion:

https://sourceforge.net/p/linux-f2fs/mailman/message/36642346/

Reported-by: Park Ju Hyung 
Signed-off-by: Chao Yu 
---
 fs/f2fs/checkpoint.c | 11 +++
 fs/f2fs/f2fs.h   |  6 +-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index a25556aef8cc..081eee9e3d92 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -831,6 +831,17 @@ static int get_checkpoint_version(struct f2fs_sb_info 
*sbi, block_t cp_addr,
return -EINVAL;
}
 
+   if (__is_set_ckpt_flags(*cp_block, CP_LARGE_NAT_BITMAP_FLAG)) {
+   if (crc_offset != CP_MIN_CHKSUM_OFFSET) {
+   f2fs_put_page(*cp_page, 1);
+   f2fs_msg(sbi->sb, KERN_WARNING,
+   "layout of large_nat_bitmap is deprecated, "
+   "run fsck to repair, chksum_offset: %zu",
+   crc_offset);
+   return -EINVAL;
+   }
+   }
+
crc = f2fs_checkpoint_chksum(sbi, *cp_block);
if (crc != cur_cp_crc(*cp_block)) {
f2fs_put_page(*cp_page, 1);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 31531711e148..f5ffc09705eb 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -1911,7 +1911,11 @@ static inline void *__bitmap_ptr(struct f2fs_sb_info 
*sbi, int flag)
if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
offset = (flag == SIT_BITMAP) ?
le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
-   return &ckpt->sit_nat_version_bitmap + offset;
+   /*
+* if large_nat_bitmap feature is enabled, leave checksum
+* protection for all nat/sit bitmaps.
+*/
+   return &ckpt->sit_nat_version_bitmap + offset + sizeof(__le32);
}
 
if (__cp_payload(sbi) > 0) {
-- 
2.18.0.rc1



___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] Possible issues with fsck of f2fs root

2019-04-22 Thread Chao Yu
On 2019/4/22 17:05, Hagbard Celine wrote:
> 2019-04-22 9:37 GMT+02:00, Chao Yu :
>> On 2019/4/22 15:11, Hagbard Celine wrote:
>>> With this patch the one problem with opening the device in RO mode is
>>> fixed.
>>
>> Oops, with default preen mode fsck should not open ro mounted image, that's
>> the
>> rule we keep line with ext4...
>>
>> How about changing to use -f in your scenario ( on RO mounted root image )?
> 
> This was with -f. Without -f it still refuses to open the device.

What I mean is we'd better to keep line with ext4, just refusing to open ro
mounted device without -f, since triggering fsck and repair on a mounted device
is dangerous, it can easily make inconsistency in between in-memory data and
on-disk data of filesystem. Refusing fsck without -f is to make user being aware
of such danger.

Thanks,

> 
> 
>> Thanks,
>>
>>> But as far as I can understand it will still only check the fs, not fix
>>> it.
>>>
>>>
>>> 2019-04-21 12:27 GMT+02:00, Jaegeuk Kim :
>>>

 New version of the patch is:

 From 3221692b060649378f1f69b898ed85a814af3dbf Mon Sep 17 00:00:00 2001
 From: Jaegeuk Kim 
 Date: Tue, 16 Apr 2019 11:46:31 -0700
 Subject: [PATCH] fsck.f2fs: open ro disk if we want to check fs only

 This patch fixes the "open failure" issue on ro disk, reported by
 Hagbard.

 "
  If I boot with kernel option "ro rootfstype=f2fs
  I get the following halfway trough boot:

   * Checking local filesystems  ...
  Info: Use default preen mode
  Info: Mounted device!
  Info: Check FS only due to RO
  Error: Failed to open the device!
   * Filesystems couldn't be fixed
 "

 Reported-by: Hagbard Celine 
 Signed-off-by: Jaegeuk Kim 
 ---
  lib/libf2fs.c | 25 +
  1 file changed, 21 insertions(+), 4 deletions(-)

 diff --git a/lib/libf2fs.c b/lib/libf2fs.c
 index d30047f..853e713 100644
 --- a/lib/libf2fs.c
 +++ b/lib/libf2fs.c
 @@ -789,6 +789,15 @@ void get_kernel_uname_version(__u8 *version)
  #endif /* APPLE_DARWIN */

  #ifndef ANDROID_WINDOWS_HOST
 +static int open_check_fs(char *path, int flag)
 +{
 +  if (c.func != FSCK || c.fix_on || c.auto_fix)
 +  return -1;
 +
 +  /* allow to open ro */
 +  return open(path, O_RDONLY | flag);
 +}
 +
  int get_device_info(int i)
  {
int32_t fd = 0;
 @@ -810,8 +819,11 @@ int get_device_info(int i)
if (c.sparse_mode) {
fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
if (fd < 0) {
 -  MSG(0, "\tError: Failed to open a sparse file!\n");
 -  return -1;
 +  fd = open_check_fs(dev->path, O_BINARY);
 +  if (fd < 0) {
 +  MSG(0, "\tError: Failed to open a sparse 
 file!\n");
 +  return -1;
 +  }
}
}

 @@ -825,10 +837,15 @@ int get_device_info(int i)
return -1;
}

 -  if (S_ISBLK(stat_buf->st_mode) && !c.force)
 +  if (S_ISBLK(stat_buf->st_mode) && !c.force) {
fd = open(dev->path, O_RDWR | O_EXCL);
 -  else
 +  if (fd < 0)
 +  fd = open_check_fs(dev->path, O_EXCL);
 +  } else {
fd = open(dev->path, O_RDWR);
 +  if (fd < 0)
 +  fd = open_check_fs(dev->path, 0);
 +  }
}
if (fd < 0) {
MSG(0, "\tError: Failed to open the device!\n");
 --
 2.19.0.605.g01d371f741-goog


>>>
>>>
>>> ___
>>> Linux-f2fs-devel mailing list
>>> Linux-f2fs-devel@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
>>> .
>>>
>>
> .
> 


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] Possible issues with fsck of f2fs root

2019-04-22 Thread Hagbard Celine
2019-04-22 9:37 GMT+02:00, Chao Yu :
> On 2019/4/22 15:11, Hagbard Celine wrote:
>> With this patch the one problem with opening the device in RO mode is
>> fixed.
>
> Oops, with default preen mode fsck should not open ro mounted image, that's
> the
> rule we keep line with ext4...
>
> How about changing to use -f in your scenario ( on RO mounted root image )?

This was with -f. Without -f it still refuses to open the device.


> Thanks,
>
>> But as far as I can understand it will still only check the fs, not fix
>> it.
>>
>>
>> 2019-04-21 12:27 GMT+02:00, Jaegeuk Kim :
>>
>>>
>>> New version of the patch is:
>>>
>>> From 3221692b060649378f1f69b898ed85a814af3dbf Mon Sep 17 00:00:00 2001
>>> From: Jaegeuk Kim 
>>> Date: Tue, 16 Apr 2019 11:46:31 -0700
>>> Subject: [PATCH] fsck.f2fs: open ro disk if we want to check fs only
>>>
>>> This patch fixes the "open failure" issue on ro disk, reported by
>>> Hagbard.
>>>
>>> "
>>>  If I boot with kernel option "ro rootfstype=f2fs
>>>  I get the following halfway trough boot:
>>>
>>>   * Checking local filesystems  ...
>>>  Info: Use default preen mode
>>>  Info: Mounted device!
>>>  Info: Check FS only due to RO
>>>  Error: Failed to open the device!
>>>   * Filesystems couldn't be fixed
>>> "
>>>
>>> Reported-by: Hagbard Celine 
>>> Signed-off-by: Jaegeuk Kim 
>>> ---
>>>  lib/libf2fs.c | 25 +
>>>  1 file changed, 21 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
>>> index d30047f..853e713 100644
>>> --- a/lib/libf2fs.c
>>> +++ b/lib/libf2fs.c
>>> @@ -789,6 +789,15 @@ void get_kernel_uname_version(__u8 *version)
>>>  #endif /* APPLE_DARWIN */
>>>
>>>  #ifndef ANDROID_WINDOWS_HOST
>>> +static int open_check_fs(char *path, int flag)
>>> +{
>>> +   if (c.func != FSCK || c.fix_on || c.auto_fix)
>>> +   return -1;
>>> +
>>> +   /* allow to open ro */
>>> +   return open(path, O_RDONLY | flag);
>>> +}
>>> +
>>>  int get_device_info(int i)
>>>  {
>>> int32_t fd = 0;
>>> @@ -810,8 +819,11 @@ int get_device_info(int i)
>>> if (c.sparse_mode) {
>>> fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
>>> if (fd < 0) {
>>> -   MSG(0, "\tError: Failed to open a sparse file!\n");
>>> -   return -1;
>>> +   fd = open_check_fs(dev->path, O_BINARY);
>>> +   if (fd < 0) {
>>> +   MSG(0, "\tError: Failed to open a sparse 
>>> file!\n");
>>> +   return -1;
>>> +   }
>>> }
>>> }
>>>
>>> @@ -825,10 +837,15 @@ int get_device_info(int i)
>>> return -1;
>>> }
>>>
>>> -   if (S_ISBLK(stat_buf->st_mode) && !c.force)
>>> +   if (S_ISBLK(stat_buf->st_mode) && !c.force) {
>>> fd = open(dev->path, O_RDWR | O_EXCL);
>>> -   else
>>> +   if (fd < 0)
>>> +   fd = open_check_fs(dev->path, O_EXCL);
>>> +   } else {
>>> fd = open(dev->path, O_RDWR);
>>> +   if (fd < 0)
>>> +   fd = open_check_fs(dev->path, 0);
>>> +   }
>>> }
>>> if (fd < 0) {
>>> MSG(0, "\tError: Failed to open the device!\n");
>>> --
>>> 2.19.0.605.g01d371f741-goog
>>>
>>>
>>
>>
>> ___
>> Linux-f2fs-devel mailing list
>> Linux-f2fs-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
>> .
>>
>


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] Possible issues with fsck of f2fs root

2019-04-22 Thread Chao Yu
On 2019/4/22 15:11, Hagbard Celine wrote:
> With this patch the one problem with opening the device in RO mode is fixed.

Oops, with default preen mode fsck should not open ro mounted image, that's the
rule we keep line with ext4...

How about changing to use -f in your scenario ( on RO mounted root image )?

Thanks,

> But as far as I can understand it will still only check the fs, not fix it.
> 
> 
> 2019-04-21 12:27 GMT+02:00, Jaegeuk Kim :
> 
>>
>> New version of the patch is:
>>
>> From 3221692b060649378f1f69b898ed85a814af3dbf Mon Sep 17 00:00:00 2001
>> From: Jaegeuk Kim 
>> Date: Tue, 16 Apr 2019 11:46:31 -0700
>> Subject: [PATCH] fsck.f2fs: open ro disk if we want to check fs only
>>
>> This patch fixes the "open failure" issue on ro disk, reported by Hagbard.
>>
>> "
>>  If I boot with kernel option "ro rootfstype=f2fs
>>  I get the following halfway trough boot:
>>
>>   * Checking local filesystems  ...
>>  Info: Use default preen mode
>>  Info: Mounted device!
>>  Info: Check FS only due to RO
>>  Error: Failed to open the device!
>>   * Filesystems couldn't be fixed
>> "
>>
>> Reported-by: Hagbard Celine 
>> Signed-off-by: Jaegeuk Kim 
>> ---
>>  lib/libf2fs.c | 25 +
>>  1 file changed, 21 insertions(+), 4 deletions(-)
>>
>> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
>> index d30047f..853e713 100644
>> --- a/lib/libf2fs.c
>> +++ b/lib/libf2fs.c
>> @@ -789,6 +789,15 @@ void get_kernel_uname_version(__u8 *version)
>>  #endif /* APPLE_DARWIN */
>>
>>  #ifndef ANDROID_WINDOWS_HOST
>> +static int open_check_fs(char *path, int flag)
>> +{
>> +if (c.func != FSCK || c.fix_on || c.auto_fix)
>> +return -1;
>> +
>> +/* allow to open ro */
>> +return open(path, O_RDONLY | flag);
>> +}
>> +
>>  int get_device_info(int i)
>>  {
>>  int32_t fd = 0;
>> @@ -810,8 +819,11 @@ int get_device_info(int i)
>>  if (c.sparse_mode) {
>>  fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
>>  if (fd < 0) {
>> -MSG(0, "\tError: Failed to open a sparse file!\n");
>> -return -1;
>> +fd = open_check_fs(dev->path, O_BINARY);
>> +if (fd < 0) {
>> +MSG(0, "\tError: Failed to open a sparse 
>> file!\n");
>> +return -1;
>> +}
>>  }
>>  }
>>
>> @@ -825,10 +837,15 @@ int get_device_info(int i)
>>  return -1;
>>  }
>>
>> -if (S_ISBLK(stat_buf->st_mode) && !c.force)
>> +if (S_ISBLK(stat_buf->st_mode) && !c.force) {
>>  fd = open(dev->path, O_RDWR | O_EXCL);
>> -else
>> +if (fd < 0)
>> +fd = open_check_fs(dev->path, O_EXCL);
>> +} else {
>>  fd = open(dev->path, O_RDWR);
>> +if (fd < 0)
>> +fd = open_check_fs(dev->path, 0);
>> +}
>>  }
>>  if (fd < 0) {
>>  MSG(0, "\tError: Failed to open the device!\n");
>> --
>> 2.19.0.605.g01d371f741-goog
>>
>>
> 
> 
> ___
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
> .
> 


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] Possible issues with fsck of f2fs root

2019-04-22 Thread Hagbard Celine
With this patch the one problem with opening the device in RO mode is fixed.
But as far as I can understand it will still only check the fs, not fix it.


2019-04-21 12:27 GMT+02:00, Jaegeuk Kim :

>
> New version of the patch is:
>
> From 3221692b060649378f1f69b898ed85a814af3dbf Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim 
> Date: Tue, 16 Apr 2019 11:46:31 -0700
> Subject: [PATCH] fsck.f2fs: open ro disk if we want to check fs only
>
> This patch fixes the "open failure" issue on ro disk, reported by Hagbard.
>
> "
>  If I boot with kernel option "ro rootfstype=f2fs
>  I get the following halfway trough boot:
>
>   * Checking local filesystems  ...
>  Info: Use default preen mode
>  Info: Mounted device!
>  Info: Check FS only due to RO
>  Error: Failed to open the device!
>   * Filesystems couldn't be fixed
> "
>
> Reported-by: Hagbard Celine 
> Signed-off-by: Jaegeuk Kim 
> ---
>  lib/libf2fs.c | 25 +
>  1 file changed, 21 insertions(+), 4 deletions(-)
>
> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
> index d30047f..853e713 100644
> --- a/lib/libf2fs.c
> +++ b/lib/libf2fs.c
> @@ -789,6 +789,15 @@ void get_kernel_uname_version(__u8 *version)
>  #endif /* APPLE_DARWIN */
>
>  #ifndef ANDROID_WINDOWS_HOST
> +static int open_check_fs(char *path, int flag)
> +{
> + if (c.func != FSCK || c.fix_on || c.auto_fix)
> + return -1;
> +
> + /* allow to open ro */
> + return open(path, O_RDONLY | flag);
> +}
> +
>  int get_device_info(int i)
>  {
>   int32_t fd = 0;
> @@ -810,8 +819,11 @@ int get_device_info(int i)
>   if (c.sparse_mode) {
>   fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
>   if (fd < 0) {
> - MSG(0, "\tError: Failed to open a sparse file!\n");
> - return -1;
> + fd = open_check_fs(dev->path, O_BINARY);
> + if (fd < 0) {
> + MSG(0, "\tError: Failed to open a sparse 
> file!\n");
> + return -1;
> + }
>   }
>   }
>
> @@ -825,10 +837,15 @@ int get_device_info(int i)
>   return -1;
>   }
>
> - if (S_ISBLK(stat_buf->st_mode) && !c.force)
> + if (S_ISBLK(stat_buf->st_mode) && !c.force) {
>   fd = open(dev->path, O_RDWR | O_EXCL);
> - else
> + if (fd < 0)
> + fd = open_check_fs(dev->path, O_EXCL);
> + } else {
>   fd = open(dev->path, O_RDWR);
> + if (fd < 0)
> + fd = open_check_fs(dev->path, 0);
> + }
>   }
>   if (fd < 0) {
>   MSG(0, "\tError: Failed to open the device!\n");
> --
> 2.19.0.605.g01d371f741-goog
>
>


___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel