Re: [f2fs-dev] [PATCH] f2fs-tools: add the max chunk size limit in sparse image

2018-10-30 Thread Junling Zheng
Hi, Jaegeuk, Chao, Ming

On 2018/10/15 21:24, Gao Ming wrote:
> Malloc Failure occurs in 32bit Windows, when using fastboot.exe flash the
>  f2fs sparse image filling with  up to 2G chunk size.
> 
> Signed-off-by: Gao Ming 
> ---
>  lib/libf2fs_io.c | 7 +++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/lib/libf2fs_io.c b/lib/libf2fs_io.c
> index 76d283d..47917ab 100644
> --- a/lib/libf2fs_io.c
> +++ b/lib/libf2fs_io.c
> @@ -311,6 +311,7 @@ int f2fs_init_sparse_file(void)
>  #endif
>  }
>  
> +#define MAX_CHUNK_SIZE (1 * 1024 * 1024 * 1024ULL)

Should we introduce an option for fsck.f2fs to get the MAX_CHUNK_SIZE ?

Thanks

>  int f2fs_finalize_device(void)
>  {
>   int i;
> @@ -337,6 +338,12 @@ int f2fs_finalize_device(void)
>   chunk_start = -1;
>   } else if (blocks[j] && chunk_start == -1) {
>   chunk_start = j;
> + } else if (blocks[j] && (chunk_start != -1) &&
> +  (j + 1 - chunk_start >=
> + (MAX_CHUNK_SIZE / F2FS_BLKSIZE))) {
> + ret = sparse_merge_blocks(chunk_start,
> +   j + 1 - chunk_start);
> + chunk_start = -1;
>   }
>   ASSERT(!ret);
>   }
> 




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


[f2fs-dev] [PATCH v2 3/5] fsck.f2fs: unify the updating of superblocks

2018-09-28 Thread Junling Zheng
Rename write_superblock() to update_superblock() and make it support updating
specified one superblock or both two superblocks, then unify all places where
sb needs to be updated.

Signed-off-by: Junling Zheng 
---
Change log from v1:
 - Rename enum SB_ADDR members and define SB_MASK(i).
 fsck/fsck.h   | 12 ++-
 fsck/mount.c  | 94 +++
 fsck/resize.c | 20 ++-
 3 files changed, 47 insertions(+), 79 deletions(-)

diff --git a/fsck/fsck.h b/fsck/fsck.h
index 6042e68..bdd7f8d 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -32,6 +32,15 @@ enum {
EUNKNOWN_ARG,
 };
 
+enum SB_ADDR {
+   SB0_ADDR = 0,
+   SB1_ADDR,
+   SB_MAX_ADDR,
+};
+
+#define SB_MASK(i) (1 << i)
+#define SB_MASK_ALL(SB_MASK(SB0_ADDR) | SB_MASK(SB1_ADDR))
+
 /* fsck.c */
 struct orphan_info {
u32 nr_inodes;
@@ -178,7 +188,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
int);
 extern void write_curseg_info(struct f2fs_sb_info *);
 extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
 extern void write_checkpoint(struct f2fs_sb_info *);
-extern void write_superblock(struct f2fs_super_block *);
+extern void update_superblock(struct f2fs_super_block *, int);
 extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
 extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
 
diff --git a/fsck/mount.c b/fsck/mount.c
index 1daef75..74ff7c6 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -475,8 +475,28 @@ void print_sb_state(struct f2fs_super_block *sb)
MSG(0, "\n");
 }
 
+void update_superblock(struct f2fs_super_block *sb, int sb_mask)
+{
+   int addr, ret;
+   u_int8_t *buf;
+
+   buf = calloc(BLOCK_SZ, 1);
+   ASSERT(buf);
+
+   memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
+   for (addr = SB0_ADDR; addr < SB_MAX_ADDR; addr++) {
+   if (SB_MASK(addr) & sb_mask) {
+   ret = dev_write_block(buf, addr);
+   ASSERT(ret >= 0);
+   }
+   }
+
+   free(buf);
+   DBG(0, "Info: Done to update superblock\n");
+}
+
 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
-   u64 offset)
+   enum SB_ADDR sb_addr)
 {
u32 segment0_blkaddr = get_sb(segment0_blkaddr);
u32 cp_blkaddr = get_sb(cp_blkaddr);
@@ -542,14 +562,11 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
segment_count_main << log_blocks_per_seg);
return -1;
} else if (main_end_blkaddr < seg_end_blkaddr) {
-   int err;
-
set_sb(segment_count, (main_end_blkaddr -
segment0_blkaddr) >> log_blocks_per_seg);
 
-   err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
-   MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
-   err ? "failed": "done",
+   update_superblock(sb, SB_MASK(sb_addr));
+   MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
main_blkaddr,
segment0_blkaddr +
(segment_count << log_blocks_per_seg),
@@ -558,7 +575,7 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
-int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
+int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
@@ -600,30 +617,24 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
return -1;
 
-   if (sanity_check_area_boundary(sb, offset))
+   if (sanity_check_area_boundary(sb, sb_addr))
return -1;
return 0;
 }
 
-int validate_super_block(struct f2fs_sb_info *sbi, int block)
+int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
 {
-   u64 offset;
char buf[F2FS_BLKSIZE];
 
sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
 
-   if (block == 0)
-   offset = F2FS_SUPER_OFFSET;
-   else
-   offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
-
-   if (dev_read_block(buf, block))
+   if (dev_read_block(buf, sb_addr))
return -1;
 
memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
sizeof(struct f2fs_super_block));
 
-   if (!sanity_check_raw_super(sbi->raw_super, offset)) {
+   if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
/* get kernel version */
if (c.kd >= 0) {

[f2fs-dev] [PATCH 2/5] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET

2018-09-28 Thread Junling Zheng
This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fsck/fsck.c|  4 ++--
 fsck/mount.c   |  4 ++--
 fsck/resize.c  |  8 
 include/f2fs_fs.h  |  6 +++---
 mkfs/f2fs_format.c | 14 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..f080d3c 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 2c2473d..1daef75 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2252,8 +2252,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 fe8a61a..e9612b3 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_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 2c086a9..38a0da4 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define PAGE_CACHE_SIZE4096
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
-#define CHECKSUM_OFFSET4092
+#define CP_CHKSUM_OFFSET   4092
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
 } __attribute__((packed));
 
 #define MAX_SIT_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
 #define MAX_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
 
 /*
  * For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..621126c 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ static int f2fs_prepare_super_block(void)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CHECKSUM_OFFSET -
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
-   CHECKSUM_OFFSET - sizeof(struct 

[f2fs-dev] [PATCH 4/5] f2fs-tools: introduce sb checksum

2018-09-28 Thread Junling Zheng
This patch introduced crc for superblock.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c   | 33 +
 fsck/resize.c  | 12 ++--
 include/f2fs_fs.h  |  6 +-
 mkfs/f2fs_format.c |  8 
 4 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 74ff7c6..9019921 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -479,10 +483,20 @@ void update_superblock(struct f2fs_super_block *sb, int 
sb_mask)
 {
int addr, ret;
u_int8_t *buf;
+   u32 old_crc, new_crc;
 
buf = calloc(BLOCK_SZ, 1);
ASSERT(buf);
 
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+   old_crc = get_sb(crc);
+   new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
+   SB_CHKSUM_OFFSET);
+   set_sb(crc, new_crc);
+   MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
+   old_crc, new_crc);
+   }
+
memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
for (addr = SB0_ADDR; addr < SB_MAX_ADDR; addr++) {
if (SB_MASK(addr) & sb_mask) {
@@ -575,10 +589,29 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
+static int verify_sb_chksum(struct f2fs_super_block *sb)
+{
+   if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   return 0;
+}
+
 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
+   verify_sb_chksum(sb))
+   return -1;
+
if (F2FS_SUPER_MAGIC != get_sb(magic))
return -1;
 
diff --git a/fsck/resize.c b/fsck/resize.c
index 5161a1f..3462165 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = new_main_blkaddr - old_main_blkaddr;
@@ -629,6 +626,9 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
migrate_sit(sbi, new_sb, offset_seg);
rebuild_checkpoint(sbi, new_sb, offset_seg);
update_superblock(new_sb, SB_MASK_ALL);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
@@ -658,9 +658,6 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = old_main_blkaddr - new_main_blkaddr;
@@ -690,6 +687,9 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
/* move whole data region */
//if (err)
//  migrate_main(sbi, offset);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 38a0da4..f80632a 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
 #define CP_CHKSUM_OFFSET   4092
+#define SB_CHKSUM_OFFSET   3068
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -579,6 +580,7 @@ enum {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* res

[f2fs-dev] [PATCH 1/5] f2fs: support superblock checksum

2018-09-28 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fs/f2fs/f2fs.h  |  2 ++
 fs/f2fs/super.c | 29 +
 fs/f2fs/sysfs.c |  7 +++
 include/linux/f2fs_fs.h |  3 ++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..3ffc336caea8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
+
+   /* Check checksum_offset and crc in superblock */
+   if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %zu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,13 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover && f2fs_sb_has_sb_chksum(sbi->sb)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+   if (f2fs_sb_has_sb_chksum(sb))
+   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+   len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
 }
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+   FEAT_SB_CHECKSUM,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+   case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, 
FEAT_FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
+F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
 

[f2fs-dev] [PATCH 5/5] fsck.f2fs: try to recover cp_payload from valid cp pack

2018-09-28 Thread Junling Zheng
From: Chao Yu 

If sb checksum is not enabled, and cp pack is valid due to no
crc inconsistence, let's try to recover cp_payload based on
cp_pack_start_sum in cp pack.

Signed-off-by: Chao Yu 
---
 fsck/f2fs.h  |  5 +
 fsck/mount.c | 10 +++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/fsck/f2fs.h b/fsck/f2fs.h
index d216444..0d0d5e2 100644
--- a/fsck/f2fs.h
+++ b/fsck/f2fs.h
@@ -259,6 +259,11 @@ static inline unsigned long __bitmap_size(struct 
f2fs_sb_info *sbi, int flag)
return 0;
 }
 
+static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
+{
+   return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
+}
+
 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
 {
struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
diff --git a/fsck/mount.c b/fsck/mount.c
index 9019921..0e8fa41 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -975,12 +975,16 @@ int sanity_check_ckpt(struct f2fs_sb_info *sbi)
}
 
cp_pack_start_sum = __start_sum_addr(sbi);
-   cp_payload = get_sb(cp_payload);
+   cp_payload = __cp_payload(sbi);
if (cp_pack_start_sum < cp_payload + 1 ||
cp_pack_start_sum > blocks_per_seg - 1 -
NR_CURSEG_TYPE) {
-   MSG(0, "\tWrong cp_pack_start_sum(%u)\n", cp_pack_start_sum);
-   return 1;
+   MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
+   cp_pack_start_sum, cp_payload);
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM))
+   return 1;
+   set_sb(cp_payload, cp_pack_start_sum - 1);
+   update_superblock(sb, SB_MASK_ALL);
}
 
return 0;
-- 
2.19.0



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


Re: [f2fs-dev] [PATCH 4/5] f2fs-tools: introduce sb checksum

2018-09-26 Thread Junling Zheng
On 2018/9/27 4:27, Jaegeuk Kim wrote:
> On 09/26, Junling Zheng wrote:
>> Hi, Jaegeuk
>>
>> On 2018/9/26 9:57, Jaegeuk Kim wrote:
>>> On 09/21, Junling Zheng wrote:
>>>> On 2018/9/21 5:38, Jaegeuk Kim wrote:
>>>>> On 09/20, Junling Zheng wrote:
>>>>>> Hi, Jaegeuk
>>>>>>
>>>>>> On 2018/9/20 7:35, Jaegeuk Kim wrote:
>>>>>>> On 09/19, Junling Zheng wrote:
>>>>>>>> This patch introduced crc for superblock.
>>>>>>>>
>>>>>>>> Signed-off-by: Junling Zheng 
>>>>>>>> ---
>>>>>>>>  fsck/mount.c   | 33 +
>>>>>>>>  fsck/resize.c  | 12 ++--
>>>>>>>>  include/f2fs_fs.h  |  6 +-
>>>>>>>>  mkfs/f2fs_format.c |  8 
>>>>>>>>  4 files changed, 52 insertions(+), 7 deletions(-)
>>>>>>>>
>>>>>>>> diff --git a/fsck/mount.c b/fsck/mount.c
>>>>>>>> index 74ff7c6..9019921 100644
>>>>>>>> --- a/fsck/mount.c
>>>>>>>> +++ b/fsck/mount.c
>>>>>>>> @@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>>>>>>>>DISP_u32(sb, node_ino);
>>>>>>>>DISP_u32(sb, meta_ino);
>>>>>>>>DISP_u32(sb, cp_payload);
>>>>>>>> +  DISP_u32(sb, crc);
>>>>>>>>DISP("%-.256s", sb, version);
>>>>>>>>printf("\n");
>>>>>>>>  }
>>>>>>>> @@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
>>>>>>>>if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
>>>>>>>>MSG(0, "%s", " lost_found");
>>>>>>>>}
>>>>>>>> +  if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
>>>>>>>> +  MSG(0, "%s", " sb_checksum");
>>>>>>>> +  }
>>>>>>>>MSG(0, "\n");
>>>>>>>>MSG(0, "Info: superblock encrypt level = %d, salt = ",
>>>>>>>>sb->encryption_level);
>>>>>>>> @@ -479,10 +483,20 @@ void update_superblock(struct f2fs_super_block 
>>>>>>>> *sb, int sb_mask)
>>>>>>>>  {
>>>>>>>>int index, ret;
>>>>>>>>u_int8_t *buf;
>>>>>>>> +  u32 old_crc, new_crc;
>>>>>>>>  
>>>>>>>>buf = calloc(BLOCK_SZ, 1);
>>>>>>>>ASSERT(buf);
>>>>>>>>  
>>>>>>>> +  if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
>>>>>>>> +  old_crc = get_sb(crc);
>>>>>>>> +  new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
>>>>>>>> +  SB_CHKSUM_OFFSET);
>>>>>>>> +  set_sb(crc, new_crc);
>>>>>>>> +  MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
>>>>>>>> +  old_crc, 
>>>>>>>> new_crc);
>>>>>>>> +  }
>>>>>>>> +
>>>>>>>>memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
>>>>>>>>for (index = 0; index < SB_ADDR_MAX; index++) {
>>>>>>>>if ((1 << index) & sb_mask) {
>>>>>>>> @@ -575,10 +589,29 @@ static inline int 
>>>>>>>> sanity_check_area_boundary(struct f2fs_super_block *sb,
>>>>>>>>return 0;
>>>>>>>>  }
>>>>>>>>  
>>>>>>>> +static int verify_sb_chksum(struct f2fs_super_block *sb)
>>>>>>>> +{
>>>>>>>> +  if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
>>>>>>>> +  MSG(0, "\tInvalid SB CRC offset: %u\n",
>>>>>>>> +  get_sb(checksum_offset));
>

Re: [f2fs-dev] [PATCH 4/5] f2fs-tools: introduce sb checksum

2018-09-25 Thread Junling Zheng
Hi, Jaegeuk

On 2018/9/26 9:57, Jaegeuk Kim wrote:
> On 09/21, Junling Zheng wrote:
>> On 2018/9/21 5:38, Jaegeuk Kim wrote:
>>> On 09/20, Junling Zheng wrote:
>>>> Hi, Jaegeuk
>>>>
>>>> On 2018/9/20 7:35, Jaegeuk Kim wrote:
>>>>> On 09/19, Junling Zheng wrote:
>>>>>> This patch introduced crc for superblock.
>>>>>>
>>>>>> Signed-off-by: Junling Zheng 
>>>>>> ---
>>>>>>  fsck/mount.c   | 33 +
>>>>>>  fsck/resize.c  | 12 ++--
>>>>>>  include/f2fs_fs.h  |  6 +-
>>>>>>  mkfs/f2fs_format.c |  8 
>>>>>>  4 files changed, 52 insertions(+), 7 deletions(-)
>>>>>>
>>>>>> diff --git a/fsck/mount.c b/fsck/mount.c
>>>>>> index 74ff7c6..9019921 100644
>>>>>> --- a/fsck/mount.c
>>>>>> +++ b/fsck/mount.c
>>>>>> @@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>>>>>>  DISP_u32(sb, node_ino);
>>>>>>  DISP_u32(sb, meta_ino);
>>>>>>  DISP_u32(sb, cp_payload);
>>>>>> +DISP_u32(sb, crc);
>>>>>>  DISP("%-.256s", sb, version);
>>>>>>  printf("\n");
>>>>>>  }
>>>>>> @@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
>>>>>>  if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
>>>>>>  MSG(0, "%s", " lost_found");
>>>>>>  }
>>>>>> +if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
>>>>>> +MSG(0, "%s", " sb_checksum");
>>>>>> +}
>>>>>>  MSG(0, "\n");
>>>>>>  MSG(0, "Info: superblock encrypt level = %d, salt = ",
>>>>>>  sb->encryption_level);
>>>>>> @@ -479,10 +483,20 @@ void update_superblock(struct f2fs_super_block 
>>>>>> *sb, int sb_mask)
>>>>>>  {
>>>>>>  int index, ret;
>>>>>>  u_int8_t *buf;
>>>>>> +u32 old_crc, new_crc;
>>>>>>  
>>>>>>  buf = calloc(BLOCK_SZ, 1);
>>>>>>  ASSERT(buf);
>>>>>>  
>>>>>> +if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
>>>>>> +old_crc = get_sb(crc);
>>>>>> +new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
>>>>>> +SB_CHKSUM_OFFSET);
>>>>>> +set_sb(crc, new_crc);
>>>>>> +MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
>>>>>> +old_crc, 
>>>>>> new_crc);
>>>>>> +}
>>>>>> +
>>>>>>  memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
>>>>>>  for (index = 0; index < SB_ADDR_MAX; index++) {
>>>>>>  if ((1 << index) & sb_mask) {
>>>>>> @@ -575,10 +589,29 @@ static inline int 
>>>>>> sanity_check_area_boundary(struct f2fs_super_block *sb,
>>>>>>  return 0;
>>>>>>  }
>>>>>>  
>>>>>> +static int verify_sb_chksum(struct f2fs_super_block *sb)
>>>>>> +{
>>>>>> +if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
>>>>>> +MSG(0, "\tInvalid SB CRC offset: %u\n",
>>>>>> +get_sb(checksum_offset));
>>>>>> +return -1;
>>>>>> +}
>>>>>> +if (f2fs_crc_valid(get_sb(crc), sb,
>>>>>> +get_sb(checksum_offset))) {
>>>>>> +MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
>>>>>> +return -1;
>>>>>> +}
>>>>>> +return 0;
>>>>>> +}
>>>>>> +
>>>>>>  int sanity_check_raw_super(struct f2fs_super_block *sb

Re: [f2fs-dev] [PATCH 4/5] f2fs-tools: introduce sb checksum

2018-09-20 Thread Junling Zheng
On 2018/9/21 5:38, Jaegeuk Kim wrote:
> On 09/20, Junling Zheng wrote:
>> Hi, Jaegeuk
>>
>> On 2018/9/20 7:35, Jaegeuk Kim wrote:
>>> On 09/19, Junling Zheng wrote:
>>>> This patch introduced crc for superblock.
>>>>
>>>> Signed-off-by: Junling Zheng 
>>>> ---
>>>>  fsck/mount.c   | 33 +
>>>>  fsck/resize.c  | 12 ++--
>>>>  include/f2fs_fs.h  |  6 +-
>>>>  mkfs/f2fs_format.c |  8 
>>>>  4 files changed, 52 insertions(+), 7 deletions(-)
>>>>
>>>> diff --git a/fsck/mount.c b/fsck/mount.c
>>>> index 74ff7c6..9019921 100644
>>>> --- a/fsck/mount.c
>>>> +++ b/fsck/mount.c
>>>> @@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>>>>DISP_u32(sb, node_ino);
>>>>DISP_u32(sb, meta_ino);
>>>>DISP_u32(sb, cp_payload);
>>>> +  DISP_u32(sb, crc);
>>>>DISP("%-.256s", sb, version);
>>>>printf("\n");
>>>>  }
>>>> @@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
>>>>if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
>>>>MSG(0, "%s", " lost_found");
>>>>}
>>>> +  if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
>>>> +  MSG(0, "%s", " sb_checksum");
>>>> +  }
>>>>MSG(0, "\n");
>>>>MSG(0, "Info: superblock encrypt level = %d, salt = ",
>>>>sb->encryption_level);
>>>> @@ -479,10 +483,20 @@ void update_superblock(struct f2fs_super_block *sb, 
>>>> int sb_mask)
>>>>  {
>>>>int index, ret;
>>>>u_int8_t *buf;
>>>> +  u32 old_crc, new_crc;
>>>>  
>>>>buf = calloc(BLOCK_SZ, 1);
>>>>ASSERT(buf);
>>>>  
>>>> +  if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
>>>> +  old_crc = get_sb(crc);
>>>> +  new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
>>>> +  SB_CHKSUM_OFFSET);
>>>> +  set_sb(crc, new_crc);
>>>> +  MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
>>>> +  old_crc, new_crc);
>>>> +  }
>>>> +
>>>>memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
>>>>for (index = 0; index < SB_ADDR_MAX; index++) {
>>>>if ((1 << index) & sb_mask) {
>>>> @@ -575,10 +589,29 @@ static inline int sanity_check_area_boundary(struct 
>>>> f2fs_super_block *sb,
>>>>return 0;
>>>>  }
>>>>  
>>>> +static int verify_sb_chksum(struct f2fs_super_block *sb)
>>>> +{
>>>> +  if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
>>>> +  MSG(0, "\tInvalid SB CRC offset: %u\n",
>>>> +  get_sb(checksum_offset));
>>>> +  return -1;
>>>> +  }
>>>> +  if (f2fs_crc_valid(get_sb(crc), sb,
>>>> +  get_sb(checksum_offset))) {
>>>> +  MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
>>>> +  return -1;
>>>> +  }
>>>> +  return 0;
>>>> +}
>>>> +
>>>>  int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR 
>>>> sb_addr)
>>>>  {
>>>>unsigned int blocksize;
>>>>  
>>>> +  if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
>>>> +  verify_sb_chksum(sb))
>>>> +  return -1;
>>>> +
>>>>if (F2FS_SUPER_MAGIC != get_sb(magic))
>>>>return -1;
>>>>  
>>>> diff --git a/fsck/resize.c b/fsck/resize.c
>>>> index 5161a1f..3462165 100644
>>>> --- a/fsck/resize.c
>>>> +++ b/fsck/resize.c
>>>> @@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
>>>>}
>>>>}
>>>>  
>>>> -  print_raw_sb_info(sb);
>>>> -  print_raw_sb_info(new_sb);
>>>
>>> It'd be worth to keep this to show the previous states.
>>>
>>
>> Here, I just want 

Re: [f2fs-dev] [PATCH 5/5] fsck.f2fs: try to recover cp_payload from valid cp pack

2018-09-19 Thread Junling Zheng
On 2018/9/20 7:38, Jaegeuk Kim wrote:
> On 09/19, Junling Zheng wrote:
>> From: Chao Yu 
>>
>> If sb checksum is not enabled, and cp pack is valid due to no
>> crc inconsistence, let's try to recover cp_payload based on
>> cp_pack_start_sum in cp pack.
>>
>> Signed-off-by: Chao Yu 
>> ---
>>  fsck/f2fs.h  |  5 +
>>  fsck/mount.c | 10 +++---
>>  2 files changed, 12 insertions(+), 3 deletions(-)
>>
>> diff --git a/fsck/f2fs.h b/fsck/f2fs.h
>> index d216444..0d0d5e2 100644
>> --- a/fsck/f2fs.h
>> +++ b/fsck/f2fs.h
>> @@ -259,6 +259,11 @@ static inline unsigned long __bitmap_size(struct 
>> f2fs_sb_info *sbi, int flag)
>>  return 0;
>>  }
>>  
>> +static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
>> +{
>> +return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
>> +}
>> +
>>  static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
>>  {
>>  struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 9019921..0e8fa41 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -975,12 +975,16 @@ int sanity_check_ckpt(struct f2fs_sb_info *sbi)
>>  }
>>  
>>  cp_pack_start_sum = __start_sum_addr(sbi);
>> -cp_payload = get_sb(cp_payload);
>> +cp_payload = __cp_payload(sbi);
>>  if (cp_pack_start_sum < cp_payload + 1 ||
>>  cp_pack_start_sum > blocks_per_seg - 1 -
>>  NR_CURSEG_TYPE) {
>> -MSG(0, "\tWrong cp_pack_start_sum(%u)\n", cp_pack_start_sum);
>> -return 1;
>> +MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
>> +cp_pack_start_sum, cp_payload);
>> +if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM))
>> +return 1;
> 
> Is this trying to fix superblock? Why is it related to SB_CHKSUM?
> 

If sb_checksum is enabled, we should not try to fix superblock.
In this situation, both sb and cp are verified with crc, so we
don't know which one, cp_payload or cp_pack_start_sum, is the
exact one.

By contraries, if sb_checksum is disabled, we could try to fix
superblock as below :)

>> +set_sb(cp_payload, cp_pack_start_sum - 1);
>> +update_superblock(sb, SB_ALL);
>>  }
>>  >>  return 0;
>> -- 
>> 2.19.0
> 
> .
> 




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


Re: [f2fs-dev] [PATCH 4/5] f2fs-tools: introduce sb checksum

2018-09-19 Thread Junling Zheng
Hi, Jaegeuk

On 2018/9/20 7:35, Jaegeuk Kim wrote:
> On 09/19, Junling Zheng wrote:
>> This patch introduced crc for superblock.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>>  fsck/mount.c   | 33 +
>>  fsck/resize.c  | 12 ++--
>>  include/f2fs_fs.h  |  6 +-
>>  mkfs/f2fs_format.c |  8 
>>  4 files changed, 52 insertions(+), 7 deletions(-)
>>
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 74ff7c6..9019921 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>>  DISP_u32(sb, node_ino);
>>  DISP_u32(sb, meta_ino);
>>  DISP_u32(sb, cp_payload);
>> +DISP_u32(sb, crc);
>>  DISP("%-.256s", sb, version);
>>  printf("\n");
>>  }
>> @@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
>>  if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
>>  MSG(0, "%s", " lost_found");
>>  }
>> +if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
>> +MSG(0, "%s", " sb_checksum");
>> +}
>>  MSG(0, "\n");
>>  MSG(0, "Info: superblock encrypt level = %d, salt = ",
>>  sb->encryption_level);
>> @@ -479,10 +483,20 @@ void update_superblock(struct f2fs_super_block *sb, 
>> int sb_mask)
>>  {
>>  int index, ret;
>>  u_int8_t *buf;
>> +u32 old_crc, new_crc;
>>  
>>  buf = calloc(BLOCK_SZ, 1);
>>  ASSERT(buf);
>>  
>> +if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
>> +old_crc = get_sb(crc);
>> +new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
>> +SB_CHKSUM_OFFSET);
>> +set_sb(crc, new_crc);
>> +MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
>> +old_crc, new_crc);
>> +}
>> +
>>  memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
>>  for (index = 0; index < SB_ADDR_MAX; index++) {
>>  if ((1 << index) & sb_mask) {
>> @@ -575,10 +589,29 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  return 0;
>>  }
>>  
>> +static int verify_sb_chksum(struct f2fs_super_block *sb)
>> +{
>> +if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
>> +MSG(0, "\tInvalid SB CRC offset: %u\n",
>> +get_sb(checksum_offset));
>> +return -1;
>> +}
>> +if (f2fs_crc_valid(get_sb(crc), sb,
>> +get_sb(checksum_offset))) {
>> +MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
>> +return -1;
>> +}
>> +return 0;
>> +}
>> +
>>  int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR 
>> sb_addr)
>>  {
>>  unsigned int blocksize;
>>  
>> +if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
>> +verify_sb_chksum(sb))
>> +return -1;
>> +
>>  if (F2FS_SUPER_MAGIC != get_sb(magic))
>>  return -1;
>>  
>> diff --git a/fsck/resize.c b/fsck/resize.c
>> index 5161a1f..3462165 100644
>> --- a/fsck/resize.c
>> +++ b/fsck/resize.c
>> @@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
>>  }
>>  }
>>  
>> -print_raw_sb_info(sb);
>> -print_raw_sb_info(new_sb);
> 
> It'd be worth to keep this to show the previous states.
> 

Here, I just want to move the printing of sb and new_sb to the place
behind update_superblock(), where the crc in sb will be updated.

>> -
>>  old_main_blkaddr = get_sb(main_blkaddr);
>>  new_main_blkaddr = get_newsb(main_blkaddr);
>>  offset = new_main_blkaddr - old_main_blkaddr;
>> @@ -629,6 +626,9 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
>>  migrate_sit(sbi, new_sb, offset_seg);
>>  rebuild_checkpoint(sbi, new_sb, offset_seg);
>>  update_superblock(new_sb, SB_ALL);
>> +print_raw_sb_info(sb);
>> +print_raw_sb_info(new_sb);
>> +
>>  return 0;
>>  }
>>  
>> @@ -658,9 +658,6 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
>>  }
>>  }
&

[f2fs-dev] [PATCH 5/5] fsck.f2fs: try to recover cp_payload from valid cp pack

2018-09-19 Thread Junling Zheng
From: Chao Yu 

If sb checksum is not enabled, and cp pack is valid due to no
crc inconsistence, let's try to recover cp_payload based on
cp_pack_start_sum in cp pack.

Signed-off-by: Chao Yu 
---
 fsck/f2fs.h  |  5 +
 fsck/mount.c | 10 +++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/fsck/f2fs.h b/fsck/f2fs.h
index d216444..0d0d5e2 100644
--- a/fsck/f2fs.h
+++ b/fsck/f2fs.h
@@ -259,6 +259,11 @@ static inline unsigned long __bitmap_size(struct 
f2fs_sb_info *sbi, int flag)
return 0;
 }
 
+static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
+{
+   return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
+}
+
 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
 {
struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
diff --git a/fsck/mount.c b/fsck/mount.c
index 9019921..0e8fa41 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -975,12 +975,16 @@ int sanity_check_ckpt(struct f2fs_sb_info *sbi)
}
 
cp_pack_start_sum = __start_sum_addr(sbi);
-   cp_payload = get_sb(cp_payload);
+   cp_payload = __cp_payload(sbi);
if (cp_pack_start_sum < cp_payload + 1 ||
cp_pack_start_sum > blocks_per_seg - 1 -
NR_CURSEG_TYPE) {
-   MSG(0, "\tWrong cp_pack_start_sum(%u)\n", cp_pack_start_sum);
-   return 1;
+   MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
+   cp_pack_start_sum, cp_payload);
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM))
+   return 1;
+   set_sb(cp_payload, cp_pack_start_sum - 1);
+   update_superblock(sb, SB_ALL);
}
 
return 0;
-- 
2.19.0



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


[f2fs-dev] [PATCH 2/5] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET

2018-09-19 Thread Junling Zheng
This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fsck/fsck.c|  4 ++--
 fsck/mount.c   |  4 ++--
 fsck/resize.c  |  8 
 include/f2fs_fs.h  |  6 +++---
 mkfs/f2fs_format.c | 14 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..f080d3c 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 2c2473d..1daef75 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2252,8 +2252,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 fe8a61a..e9612b3 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_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 2c086a9..38a0da4 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define PAGE_CACHE_SIZE4096
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
-#define CHECKSUM_OFFSET4092
+#define CP_CHKSUM_OFFSET   4092
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
 } __attribute__((packed));
 
 #define MAX_SIT_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
 #define MAX_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
 
 /*
  * For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..621126c 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ static int f2fs_prepare_super_block(void)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CHECKSUM_OFFSET -
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
-   CHECKSUM_OFFSET - sizeof(struct 

[f2fs-dev] [PATCH 1/5] f2fs: support superblock checksum

2018-09-19 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fs/f2fs/f2fs.h  |  2 ++
 fs/f2fs/super.c | 29 +
 fs/f2fs/sysfs.c |  7 +++
 include/linux/f2fs_fs.h |  3 ++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..3ffc336caea8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
+
+   /* Check checksum_offset and crc in superblock */
+   if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %zu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,13 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover && f2fs_sb_has_sb_chksum(sbi->sb)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+   if (f2fs_sb_has_sb_chksum(sb))
+   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+   len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
 }
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+   FEAT_SB_CHECKSUM,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+   case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, 
FEAT_FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
+F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
 

[f2fs-dev] [PATCH 3/5] fsck.f2fs: unify the updating of superblocks

2018-09-19 Thread Junling Zheng
Rename write_superblock() to update_superblock() and make it support updating
specified one superblock or both two superblocks, then unify all places where
sb needs to be updated.

Signed-off-by: Junling Zheng 
---
 fsck/fsck.h   | 12 ++-
 fsck/mount.c  | 94 +++
 fsck/resize.c | 20 ++-
 3 files changed, 47 insertions(+), 79 deletions(-)

diff --git a/fsck/fsck.h b/fsck/fsck.h
index 6042e68..bdd7f8d 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -32,6 +32,16 @@ enum {
EUNKNOWN_ARG,
 };
 
+enum SB_ADDR {
+   SB_ADDR_0,
+   SB_ADDR_1,
+   SB_ADDR_MAX,
+};
+
+#define SB_FIRST   (1 << SB_ADDR_0)
+#define SB_SECOND  (1 << SB_ADDR_1)
+#define SB_ALL (SB_FIRST | SB_SECOND)
+
 /* fsck.c */
 struct orphan_info {
u32 nr_inodes;
@@ -178,7 +188,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
int);
 extern void write_curseg_info(struct f2fs_sb_info *);
 extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
 extern void write_checkpoint(struct f2fs_sb_info *);
-extern void write_superblock(struct f2fs_super_block *);
+extern void update_superblock(struct f2fs_super_block *, int);
 extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
 extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
 
diff --git a/fsck/mount.c b/fsck/mount.c
index 1daef75..74ff7c6 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -475,8 +475,28 @@ void print_sb_state(struct f2fs_super_block *sb)
MSG(0, "\n");
 }
 
+void update_superblock(struct f2fs_super_block *sb, int sb_mask)
+{
+   int index, ret;
+   u_int8_t *buf;
+
+   buf = calloc(BLOCK_SZ, 1);
+   ASSERT(buf);
+
+   memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
+   for (index = 0; index < SB_ADDR_MAX; index++) {
+   if ((1 << index) & sb_mask) {
+   ret = dev_write_block(buf, index);
+   ASSERT(ret >= 0);
+   }
+   }
+
+   free(buf);
+   DBG(0, "Info: Done to update superblock\n");
+}
+
 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
-   u64 offset)
+   enum SB_ADDR sb_addr)
 {
u32 segment0_blkaddr = get_sb(segment0_blkaddr);
u32 cp_blkaddr = get_sb(cp_blkaddr);
@@ -542,14 +562,11 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
segment_count_main << log_blocks_per_seg);
return -1;
} else if (main_end_blkaddr < seg_end_blkaddr) {
-   int err;
-
set_sb(segment_count, (main_end_blkaddr -
segment0_blkaddr) >> log_blocks_per_seg);
 
-   err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
-   MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
-   err ? "failed": "done",
+   update_superblock(sb, 1 << sb_addr);
+   MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
main_blkaddr,
segment0_blkaddr +
(segment_count << log_blocks_per_seg),
@@ -558,7 +575,7 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
-int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
+int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
@@ -600,30 +617,24 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
return -1;
 
-   if (sanity_check_area_boundary(sb, offset))
+   if (sanity_check_area_boundary(sb, sb_addr))
return -1;
return 0;
 }
 
-int validate_super_block(struct f2fs_sb_info *sbi, int block)
+int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
 {
-   u64 offset;
char buf[F2FS_BLKSIZE];
 
sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
 
-   if (block == 0)
-   offset = F2FS_SUPER_OFFSET;
-   else
-   offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
-
-   if (dev_read_block(buf, block))
+   if (dev_read_block(buf, sb_addr))
return -1;
 
memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
sizeof(struct f2fs_super_block));
 
-   if (!sanity_check_raw_super(sbi->raw_super, offset)) {
+   if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
/* get kernel version */
if (c.kd >= 0) {
  

[f2fs-dev] [PATCH 4/5] f2fs-tools: introduce sb checksum

2018-09-19 Thread Junling Zheng
This patch introduced crc for superblock.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c   | 33 +
 fsck/resize.c  | 12 ++--
 include/f2fs_fs.h  |  6 +-
 mkfs/f2fs_format.c |  8 
 4 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 74ff7c6..9019921 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -479,10 +483,20 @@ void update_superblock(struct f2fs_super_block *sb, int 
sb_mask)
 {
int index, ret;
u_int8_t *buf;
+   u32 old_crc, new_crc;
 
buf = calloc(BLOCK_SZ, 1);
ASSERT(buf);
 
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+   old_crc = get_sb(crc);
+   new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
+   SB_CHKSUM_OFFSET);
+   set_sb(crc, new_crc);
+   MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
+   old_crc, new_crc);
+   }
+
memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
for (index = 0; index < SB_ADDR_MAX; index++) {
if ((1 << index) & sb_mask) {
@@ -575,10 +589,29 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
+static int verify_sb_chksum(struct f2fs_super_block *sb)
+{
+   if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   return 0;
+}
+
 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
+   verify_sb_chksum(sb))
+   return -1;
+
if (F2FS_SUPER_MAGIC != get_sb(magic))
return -1;
 
diff --git a/fsck/resize.c b/fsck/resize.c
index 5161a1f..3462165 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = new_main_blkaddr - old_main_blkaddr;
@@ -629,6 +626,9 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
migrate_sit(sbi, new_sb, offset_seg);
rebuild_checkpoint(sbi, new_sb, offset_seg);
update_superblock(new_sb, SB_ALL);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
@@ -658,9 +658,6 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = old_main_blkaddr - new_main_blkaddr;
@@ -690,6 +687,9 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
/* move whole data region */
//if (err)
//  migrate_main(sbi, offset);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 38a0da4..f80632a 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
 #define CP_CHKSUM_OFFSET   4092
+#define SB_CHKSUM_OFFSET   3068
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -579,6 +580,7 @@ enum {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  

Re: [f2fs-dev] [RFC PATCH v3 3/5] f2fs-tools: unify the writeback of superblock

2018-09-10 Thread Junling Zheng
Hi, Jaegeuk

On 2018/9/11 10:43, Junling Zheng wrote:
> On 2018/9/8 6:23, Jaegeuk Kim wrote:
>> On 08/30, Junling Zheng wrote:
>>> diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
>>> index 38a0da4..df46950 100644
>>> --- a/include/f2fs_fs.h
>>> +++ b/include/f2fs_fs.h
>>> @@ -1411,4 +1411,39 @@ static inline int parse_root_owner(char *ids,
>>> return 0;
>>>  }
>>>  
>>> +enum SB_ADDR {
>>> +   SB_ADDR_0,
>>> +   SB_ADDR_1,
>>> +   SB_ADDR_MAX,
>>> +};
>>> +
>>> +#define SB_FIRST   (1 << SB_ADDR_0)
>>> +#define SB_SECOND  (1 << SB_ADDR_1)
>>> +#define SB_ALL (SB_FIRST | SB_SECOND)
>>> +
>>> +static inline int __write_superblock(struct f2fs_super_block *sb, int 
>>> sb_mask)
>>> +{
>>> +   int index, ret;
>>> +   u_int8_t *buf;
>>> +
>>> +   buf = calloc(F2FS_BLKSIZE, 1);
>>> +   ASSERT(buf);
>>> +
>>> +   memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
>>> +   for (index = 0; index < SB_ADDR_MAX; index++) {
>>> +   if ((1 << index) & sb_mask) {
>>> +   ret = dev_write_block(buf, index);
>>
>> This doesn't look like a proper inline function.
>>
> 
> Ok, I'll remove the "inline" flag :)
> Any other flaws?
> 
> Thanks
> 

Sorry, here may be difficult to handle :(

This function is used in both mkfs and fsck, so I think it's proper to place it 
in
include/f2fs_fs.h.

Then, if the "inline" flag is removed it will generate some compile warnings as 
below:

In file included from libf2fs.c:11:0:
../include/f2fs_fs.h:1428:12: warning: '__write_superblock' defined but not 
used [-Wunused-function]
 static int __write_superblock(struct f2fs_super_block *sb, int sb_mask)
^

Besides, there're some other complex functions like get_best_overprovision(), 
parse_feature()
being defined as inline functions.

So, I think it's better to keep it inline. what do you think?

Thanks

>>> +   if (ret) {
>>> +   MSG(0, "\tError: While writing superblock %d "
>>> +   "to disk!!!\n", index);
>>> +   free(buf);
>>> +   return ret;
>>> +   }
>>> +   }
>>> +   }
>>> +
>>> +   free(buf);
>>> +   return 0;
>>> +}
>>> +
>>>  #endif /*__F2FS_FS_H */
>>> diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
>>> index 621126c..7e3846e 100644
>>> --- a/mkfs/f2fs_format.c
>>> +++ b/mkfs/f2fs_format.c
>>> @@ -979,24 +979,7 @@ free_cp:
>>>  
>>>  static int f2fs_write_super_block(void)
>>>  {
>>> -   int index;
>>> -   u_int8_t *zero_buff;
>>> -
>>> -   zero_buff = calloc(F2FS_BLKSIZE, 1);
>>> -
>>> -   memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
>>> -   DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
>>> -   for (index = 0; index < 2; index++) {
>>> -   if (dev_write_block(zero_buff, index)) {
>>> -   MSG(1, "\tError: While while writing supe_blk "
>>> -   "on disk!!! index : %d\n", index);
>>> -   free(zero_buff);
>>> -   return -1;
>>> -   }
>>> -   }
>>> -
>>> -   free(zero_buff);
>>> -   return 0;
>>> +   return __write_superblock(sb, SB_ALL);
>>>  }
>>>  
>>>  #ifndef WITH_ANDROID
>>> -- 
>>> 2.18.0
>>
>> .
>>
> 
> 
> 
> 
> ___
> 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] [RFC PATCH v3 3/5] f2fs-tools: unify the writeback of superblock

2018-09-10 Thread Junling Zheng
On 2018/9/8 6:23, Jaegeuk Kim wrote:
> On 08/30, Junling Zheng wrote:
>> Introduce __write_superblock() to support updating specified one
>> superblock or both, thus we can wrapper it in update_superblock() and
>> f2fs_write_super_block to unify all places where sb needs to be updated.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>> v2 -> v3:
>>  - fix wrong condition of ASSERT in update_superblock.
>> v1 -> v2:
>>  - if dev_write_block failed, add some notes and free buf to avoid memory 
>> leak.
>>  fsck/fsck.h|  2 +-
>>  fsck/mount.c   | 74 +++---
>>  fsck/resize.c  | 20 ++---
>>  include/f2fs_fs.h  | 35 ++
>>  mkfs/f2fs_format.c | 19 +---
>>  5 files changed, 56 insertions(+), 94 deletions(-)
>>
>> diff --git a/fsck/fsck.h b/fsck/fsck.h
>> index 6042e68..e3490e6 100644
>> --- a/fsck/fsck.h
>> +++ b/fsck/fsck.h
>> @@ -178,7 +178,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
>> int);
>>  extern void write_curseg_info(struct f2fs_sb_info *);
>>  extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
>>  extern void write_checkpoint(struct f2fs_sb_info *);
>> -extern void write_superblock(struct f2fs_super_block *);
>> +extern void update_superblock(struct f2fs_super_block *, int);
>>  extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
>>  extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, 
>> block_t);
>>  
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 1daef75..2bc44ce 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -476,7 +476,7 @@ void print_sb_state(struct f2fs_super_block *sb)
>>  }
>>  
>>  static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
>> -u64 offset)
>> +enum SB_ADDR sb_addr)
>>  {
>>  u32 segment0_blkaddr = get_sb(segment0_blkaddr);
>>  u32 cp_blkaddr = get_sb(cp_blkaddr);
>> @@ -542,14 +542,11 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  segment_count_main << log_blocks_per_seg);
>>  return -1;
>>  } else if (main_end_blkaddr < seg_end_blkaddr) {
>> -int err;
>> -
>>  set_sb(segment_count, (main_end_blkaddr -
>>  segment0_blkaddr) >> log_blocks_per_seg);
>>  
>> -err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
>> -MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
>> -err ? "failed": "done",
>> +update_superblock(sb, 1 << sb_addr);
>> +MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
>>  main_blkaddr,
>>  segment0_blkaddr +
>>  (segment_count << log_blocks_per_seg),
>> @@ -558,7 +555,7 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  return 0;
>>  }
>>  
>> -int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
>> +int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR 
>> sb_addr)
>>  {
>>  unsigned int blocksize;
>>  
>> @@ -600,30 +597,24 @@ int sanity_check_raw_super(struct f2fs_super_block 
>> *sb, u64 offset)
>>  if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
>>  return -1;
>>  
>> -if (sanity_check_area_boundary(sb, offset))
>> +if (sanity_check_area_boundary(sb, sb_addr))
>>  return -1;
>>  return 0;
>>  }
>>  
>> -int validate_super_block(struct f2fs_sb_info *sbi, int block)
>> +int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
>>  {
>> -u64 offset;
>>  char buf[F2FS_BLKSIZE];
>>  
>>  sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
>>  
>> -if (block == 0)
>> -offset = F2FS_SUPER_OFFSET;
>> -else
>> -offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
>> -
>> -if (dev_read_block(buf, block))
>> +if (dev_read_block(buf, sb_addr))
>>  return -1;
>>  
>>  memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
>> 

[f2fs-dev] [RFC PATCH RESEND 4/5] f2fs-tools: introduce sb checksum

2018-08-29 Thread Junling Zheng
This patch introduced crc for superblock.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fsck/mount.c   | 23 +++
 fsck/resize.c  | 12 ++--
 include/f2fs_fs.h  | 16 +++-
 mkfs/f2fs_format.c |  3 +++
 4 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 2bc44ce..71af6b2 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -555,10 +559,29 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
+static int verify_sb_chksum(struct f2fs_super_block *sb)
+{
+   if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   return 0;
+}
+
 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
+   verify_sb_chksum(sb))
+   return -1;
+
if (F2FS_SUPER_MAGIC != get_sb(magic))
return -1;
 
diff --git a/fsck/resize.c b/fsck/resize.c
index 5161a1f..3462165 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = new_main_blkaddr - old_main_blkaddr;
@@ -629,6 +626,9 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
migrate_sit(sbi, new_sb, offset_seg);
rebuild_checkpoint(sbi, new_sb, offset_seg);
update_superblock(new_sb, SB_ALL);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
@@ -658,9 +658,6 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = old_main_blkaddr - new_main_blkaddr;
@@ -690,6 +687,9 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
/* move whole data region */
//if (err)
//  migrate_main(sbi, offset);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index df46950..9396c78 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
 #define CP_CHKSUM_OFFSET   4092
+#define SB_CHKSUM_OFFSET   3068
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -579,6 +580,7 @@ enum {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define MAX_VOLUME_NAME512
 
@@ -632,7 +634,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES];   /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
-   __u8 reserved[314]; /* valid reserved region */
+   __u8 reserved[310]; /* valid reserved region */
+   __le32 crc; /* checksum of superblock */
 } __attribute__((packed));
 
 /*
@@ -1338,6 +1341,7 @@ struct feature feature_table[] = {
\
{ "inode_crtime",   F2FS_FEATURE_INODE_CRTIME },\
{ "lost_found",   

[f2fs-dev] [RFC PATCH RESEND 2/5] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET

2018-08-29 Thread Junling Zheng
This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fsck/fsck.c|  4 ++--
 fsck/mount.c   |  4 ++--
 fsck/resize.c  |  8 
 include/f2fs_fs.h  |  6 +++---
 mkfs/f2fs_format.c | 14 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..f080d3c 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 2c2473d..1daef75 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2252,8 +2252,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 fe8a61a..e9612b3 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_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 2c086a9..38a0da4 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define PAGE_CACHE_SIZE4096
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
-#define CHECKSUM_OFFSET4092
+#define CP_CHKSUM_OFFSET   4092
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
 } __attribute__((packed));
 
 #define MAX_SIT_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
 #define MAX_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
 
 /*
  * For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..621126c 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ static int f2fs_prepare_super_block(void)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CHECKSUM_OFFSET -
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
-   CHECKSUM_OFFSET - sizeof(struct 

[f2fs-dev] [RFC PATCH RESEND 5/5] fsck.f2fs: try to recover cp_payload from valid cp pack

2018-08-29 Thread Junling Zheng
From: Chao Yu 

If sb checksum is not enabled, and cp pack is valid due to no
crc inconsistence, let's try to recover cp_payload based on
cp_pack_start_sum in cp pack.

Signed-off-by: Chao Yu 
---
 fsck/f2fs.h  |  5 +
 fsck/mount.c | 10 +++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/fsck/f2fs.h b/fsck/f2fs.h
index d216444..0d0d5e2 100644
--- a/fsck/f2fs.h
+++ b/fsck/f2fs.h
@@ -259,6 +259,11 @@ static inline unsigned long __bitmap_size(struct 
f2fs_sb_info *sbi, int flag)
return 0;
 }
 
+static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
+{
+   return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
+}
+
 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
 {
struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
diff --git a/fsck/mount.c b/fsck/mount.c
index 71af6b2..8a7d6e4 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -945,12 +945,16 @@ int sanity_check_ckpt(struct f2fs_sb_info *sbi)
}
 
cp_pack_start_sum = __start_sum_addr(sbi);
-   cp_payload = get_sb(cp_payload);
+   cp_payload = __cp_payload(sbi);
if (cp_pack_start_sum < cp_payload + 1 ||
cp_pack_start_sum > blocks_per_seg - 1 -
NR_CURSEG_TYPE) {
-   MSG(0, "\tWrong cp_pack_start_sum(%u)\n", cp_pack_start_sum);
-   return 1;
+   MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
+   cp_pack_start_sum, cp_payload);
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM))
+   return 1;
+   set_sb(cp_payload, cp_pack_start_sum - 1);
+   update_superblock(sb, SB_ALL);
}
 
return 0;
-- 
2.18.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [RFC PATCH v3 3/5] f2fs-tools: unify the writeback of superblock

2018-08-29 Thread Junling Zheng
Introduce __write_superblock() to support updating specified one
superblock or both, thus we can wrapper it in update_superblock() and
f2fs_write_super_block to unify all places where sb needs to be updated.

Signed-off-by: Junling Zheng 
---
v2 -> v3:
 - fix wrong condition of ASSERT in update_superblock.
v1 -> v2:
 - if dev_write_block failed, add some notes and free buf to avoid memory leak.
 fsck/fsck.h|  2 +-
 fsck/mount.c   | 74 +++---
 fsck/resize.c  | 20 ++---
 include/f2fs_fs.h  | 35 ++
 mkfs/f2fs_format.c | 19 +---
 5 files changed, 56 insertions(+), 94 deletions(-)

diff --git a/fsck/fsck.h b/fsck/fsck.h
index 6042e68..e3490e6 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -178,7 +178,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
int);
 extern void write_curseg_info(struct f2fs_sb_info *);
 extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
 extern void write_checkpoint(struct f2fs_sb_info *);
-extern void write_superblock(struct f2fs_super_block *);
+extern void update_superblock(struct f2fs_super_block *, int);
 extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
 extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
 
diff --git a/fsck/mount.c b/fsck/mount.c
index 1daef75..2bc44ce 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -476,7 +476,7 @@ void print_sb_state(struct f2fs_super_block *sb)
 }
 
 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
-   u64 offset)
+   enum SB_ADDR sb_addr)
 {
u32 segment0_blkaddr = get_sb(segment0_blkaddr);
u32 cp_blkaddr = get_sb(cp_blkaddr);
@@ -542,14 +542,11 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
segment_count_main << log_blocks_per_seg);
return -1;
} else if (main_end_blkaddr < seg_end_blkaddr) {
-   int err;
-
set_sb(segment_count, (main_end_blkaddr -
segment0_blkaddr) >> log_blocks_per_seg);
 
-   err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
-   MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
-   err ? "failed": "done",
+   update_superblock(sb, 1 << sb_addr);
+   MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
main_blkaddr,
segment0_blkaddr +
(segment_count << log_blocks_per_seg),
@@ -558,7 +555,7 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
-int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
+int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
@@ -600,30 +597,24 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
return -1;
 
-   if (sanity_check_area_boundary(sb, offset))
+   if (sanity_check_area_boundary(sb, sb_addr))
return -1;
return 0;
 }
 
-int validate_super_block(struct f2fs_sb_info *sbi, int block)
+int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
 {
-   u64 offset;
char buf[F2FS_BLKSIZE];
 
sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
 
-   if (block == 0)
-   offset = F2FS_SUPER_OFFSET;
-   else
-   offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
-
-   if (dev_read_block(buf, block))
+   if (dev_read_block(buf, sb_addr))
return -1;
 
memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
sizeof(struct f2fs_super_block));
 
-   if (!sanity_check_raw_super(sbi->raw_super, offset)) {
+   if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
/* get kernel version */
if (c.kd >= 0) {
dev_read_version(c.version, 0, VERSION_LEN);
@@ -642,13 +633,9 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
MSG(0, "Info: FSCK version\n  from \"%s\"\nto \"%s\"\n",
c.sb_version, c.version);
if (memcmp(c.sb_version, c.version, VERSION_LEN)) {
-   int ret;
-
memcpy(sbi->raw_super->version,
c.version, VERSION_LEN);
-   ret = dev_write(sbi->raw_super, offset,
-   sizeof(struct f2

[f2fs-dev] [RFC PATCH RESEND 1/5] f2fs: support superblock checksum

2018-08-29 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fs/f2fs/f2fs.h  |  2 ++
 fs/f2fs/super.c | 29 +
 fs/f2fs/sysfs.c |  7 +++
 include/linux/f2fs_fs.h |  3 ++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..3ffc336caea8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
+
+   /* Check checksum_offset and crc in superblock */
+   if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %zu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,13 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover && f2fs_sb_has_sb_chksum(sbi->sb)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+   if (f2fs_sb_has_sb_chksum(sb))
+   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+   len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
 }
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+   FEAT_SB_CHECKSUM,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+   case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, 
FEAT_FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
+F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
 

Re: [f2fs-dev] [PATCH] fsck.f2fs: try to recover cp_payload from valid cp pack

2018-08-29 Thread Junling Zheng
Hi, Jaegeuk

On 2018/8/30 10:10, Jaegeuk Kim wrote:
> Hi Chao,
> 
> Could you please add this into Junling's patch series?
> A little bit confusing between patches and reviews on them.
> 

I'll send an new version patch series with this patch as soon as possible :)

> Thanks,
> 
> On 08/28, Chao Yu wrote:
>> If sb checksum is not enabled, and cp pack is valid due to no
>> crc inconsistence, let's try to recover cp_payload based on
>> cp_pack_start_sum in cp pack.
>>
>> Signed-off-by: Chao Yu 
>> ---
>>  fsck/f2fs.h  |  5 +
>>  fsck/mount.c | 10 +++---
>>  2 files changed, 12 insertions(+), 3 deletions(-)
>>
>> diff --git a/fsck/f2fs.h b/fsck/f2fs.h
>> index d2164449db15..0d0d5e2cc399 100644
>> --- a/fsck/f2fs.h
>> +++ b/fsck/f2fs.h
>> @@ -259,6 +259,11 @@ static inline unsigned long __bitmap_size(struct 
>> f2fs_sb_info *sbi, int flag)
>>  return 0;
>>  }
>>  
>> +static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
>> +{
>> +return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
>> +}
>> +
>>  static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
>>  {
>>  struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 4f0c3fc0db50..9421f5953cd8 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -945,12 +945,16 @@ int sanity_check_ckpt(struct f2fs_sb_info *sbi)
>>  }
>>  
>>  cp_pack_start_sum = __start_sum_addr(sbi);
>> -cp_payload = get_sb(cp_payload);
>> +cp_payload = __cp_payload(sbi);
>>  if (cp_pack_start_sum < cp_payload + 1 ||
>>  cp_pack_start_sum > blocks_per_seg - 1 -
>>  NR_CURSEG_TYPE) {
>> -MSG(0, "\tWrong cp_pack_start_sum(%u)\n", cp_pack_start_sum);
>> -return 1;
>> +MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
>> +cp_pack_start_sum, cp_payload);
>> +if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM))
>> +return 1;
>> +set_sb(cp_payload, cp_pack_start_sum - 1);
>> +update_superblock(sb, SB_ALL);
>>  }
>>  
>>  return 0;
>> -- 
>> 2.18.0.rc1
> 
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> ___
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
> 
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [RFC PATCH v2 3/4] f2fs-tools: unify the writeback of superblock

2018-08-28 Thread Junling Zheng
On 2018/8/28 21:47, Chao Yu wrote:
> On 2018/8/14 14:56, Junling Zheng wrote:
>> Introduce __write_superblock() to support updating specified one
>> superblock or both, thus we can wrapper it in update_superblock() and
>> f2fs_write_super_block to unify all places where sb needs to be updated.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>> v1 -> v2:
>>  - if dev_write_block failed, add some notes and free buf to avoid memory 
>> leak.
>>  fsck/fsck.h|  2 +-
>>  fsck/mount.c   | 74 +++---
>>  fsck/resize.c  | 20 ++---
>>  include/f2fs_fs.h  | 35 ++
>>  mkfs/f2fs_format.c | 19 +---
>>  5 files changed, 56 insertions(+), 94 deletions(-)
>>
>> diff --git a/fsck/fsck.h b/fsck/fsck.h
>> index 6042e68..e3490e6 100644
>> --- a/fsck/fsck.h
>> +++ b/fsck/fsck.h
>> @@ -178,7 +178,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
>> int);
>>  extern void write_curseg_info(struct f2fs_sb_info *);
>>  extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
>>  extern void write_checkpoint(struct f2fs_sb_info *);
>> -extern void write_superblock(struct f2fs_super_block *);
>> +extern void update_superblock(struct f2fs_super_block *, int);
>>  extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
>>  extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, 
>> block_t);
>>  
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 58ef3e6..e7ceb8d 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -476,7 +476,7 @@ void print_sb_state(struct f2fs_super_block *sb)
>>  }
>>  
>>  static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
>> -u64 offset)
>> +enum SB_ADDR sb_addr)
>>  {
>>  u32 segment0_blkaddr = get_sb(segment0_blkaddr);
>>  u32 cp_blkaddr = get_sb(cp_blkaddr);
>> @@ -542,14 +542,11 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  segment_count_main << log_blocks_per_seg);
>>  return -1;
>>  } else if (main_end_blkaddr < seg_end_blkaddr) {
>> -int err;
>> -
>>  set_sb(segment_count, (main_end_blkaddr -
>>  segment0_blkaddr) >> log_blocks_per_seg);
>>  
>> -err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
>> -MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
>> -err ? "failed": "done",
>> +update_superblock(sb, 1 << sb_addr);
>> +MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
>>  main_blkaddr,
>>  segment0_blkaddr +
>>  (segment_count << log_blocks_per_seg),
>> @@ -558,7 +555,7 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  return 0;
>>  }
>>  
>> -int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
>> +int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR 
>> sb_addr)
>>  {
>>  unsigned int blocksize;
>>  
>> @@ -600,30 +597,24 @@ int sanity_check_raw_super(struct f2fs_super_block 
>> *sb, u64 offset)
>>  if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
>>  return -1;
>>  
>> -if (sanity_check_area_boundary(sb, offset))
>> +if (sanity_check_area_boundary(sb, sb_addr))
>>  return -1;
>>  return 0;
>>  }
>>  
>> -int validate_super_block(struct f2fs_sb_info *sbi, int block)
>> +int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
>>  {
>> -u64 offset;
>>  char buf[F2FS_BLKSIZE];
>>  
>>  sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
>>  
>> -if (block == 0)
>> -offset = F2FS_SUPER_OFFSET;
>> -else
>> -offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
>> -
>> -if (dev_read_block(buf, block))
>> +if (dev_read_block(buf, sb_addr))
>>  return -1;
>>  
>>  memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
>>  sizeof(struct f2fs_super_block));
>>  
>> -if (!sanity_check_raw_super(sbi->raw_super

Re: [f2fs-dev] [RFC PATCH RESEND 1/4] f2fs: support superblock checksum

2018-08-27 Thread Junling Zheng
Ping...

On 2018/8/14 14:56, Junling Zheng wrote:
> Now we support crc32 checksum for superblock.
> 
> Reviewed-by: Chao Yu 
> Signed-off-by: Junling Zheng 
> ---
>  fs/f2fs/f2fs.h  |  2 ++
>  fs/f2fs/super.c | 29 +
>  fs/f2fs/sysfs.c |  7 +++
>  include/linux/f2fs_fs.h |  3 ++-
>  4 files changed, 40 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 4525f4f82af0..d50d6efda96b 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
>  #define F2FS_FEATURE_INODE_CRTIME0x0100
>  #define F2FS_FEATURE_LOST_FOUND  0x0200
>  #define F2FS_FEATURE_VERITY  0x0400  /* reserved */
> +#define F2FS_FEATURE_SB_CHKSUM   0x0800
>  
>  #define F2FS_HAS_FEATURE(sb, mask)   \
>   ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
> FLEXIBLE_INLINE_XATTR);
>  F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
>  F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
>  F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>  
>  #ifdef CONFIG_BLK_DEV_ZONED
>  static inline int get_blkz_type(struct f2fs_sb_info *sbi,
> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
> index bd57be470e23..3ffc336caea8 100644
> --- a/fs/f2fs/super.c
> +++ b/fs/f2fs/super.c
> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
> *sbi,
>   (bh->b_data + F2FS_SUPER_OFFSET);
>   struct super_block *sb = sbi->sb;
>   unsigned int blocksize;
> + size_t crc_offset = 0;
> + __u32 crc = 0;
> +
> + /* Check checksum_offset and crc in superblock */
> + if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
> + crc_offset = le32_to_cpu(raw_super->checksum_offset);
> + if (crc_offset !=
> + offsetof(struct f2fs_super_block, crc)) {
> + f2fs_msg(sb, KERN_INFO,
> + "Invalid SB checksum offset: %zu",
> + crc_offset);
> + return 1;
> + }
> + crc = le32_to_cpu(raw_super->crc);
> + if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
> + f2fs_msg(sb, KERN_INFO,
> + "Invalid SB checksum value: %u", crc);
> + return 1;
> + }
> + }
>  
>   if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
>   f2fs_msg(sb, KERN_INFO,
> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info 
> *sbi,
>  int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>  {
>   struct buffer_head *bh;
> + __u32 crc = 0;
>   int err;
>  
>   if ((recover && f2fs_readonly(sbi->sb)) ||
> @@ -2576,6 +2597,13 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
> recover)
>   return -EROFS;
>   }
>  
> + /* we should update superblock crc here */
> + if (!recover && f2fs_sb_has_sb_chksum(sbi->sb)) {
> + crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
> + offsetof(struct f2fs_super_block, crc));
> + F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
> + }
> +
>   /* write back-up superblock first */
>   bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
>   if (!bh)
> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
> index cd2e030e47b8..c86d91be6c48 100644
> --- a/fs/f2fs/sysfs.c
> +++ b/fs/f2fs/sysfs.c
> @@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
>   if (f2fs_sb_has_lost_found(sb))
>   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
>   len ? ", " : "", "lost_found");
> + if (f2fs_sb_has_sb_chksum(sb))
> + len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
> + len ? ", " : "", "sb_checksum");
>   len += snprintf(buf + len, PAGE_SIZE - len, "\n");
>   return len;
>  }
> @@ -337,6 +340,7 @@ enum feat_id {
>   FEAT_QUOTA_INO,
>   FEAT_INODE_CRTIME,
>   FEAT_LOST_FOUND,
> + FEAT_SB_CHECKSUM,
>  };
>  
>  static ssize_t f2fs_feature_show(struct f2fs_attr *a,
> @@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
>   case FEAT_QU

[f2fs-dev] [RFC PATCH RESEND 1/4] f2fs: support superblock checksum

2018-08-14 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fs/f2fs/f2fs.h  |  2 ++
 fs/f2fs/super.c | 29 +
 fs/f2fs/sysfs.c |  7 +++
 include/linux/f2fs_fs.h |  3 ++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..3ffc336caea8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
+
+   /* Check checksum_offset and crc in superblock */
+   if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %zu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,13 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover && f2fs_sb_has_sb_chksum(sbi->sb)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+   if (f2fs_sb_has_sb_chksum(sb))
+   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+   len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
 }
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+   FEAT_SB_CHECKSUM,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+   case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, 
FEAT_FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
+F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
 

[f2fs-dev] [RFC PATCH RESEND 2/4] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET

2018-08-14 Thread Junling Zheng
This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fsck/fsck.c|  4 ++--
 fsck/mount.c   |  4 ++--
 fsck/resize.c  |  8 
 include/f2fs_fs.h  |  6 +++---
 mkfs/f2fs_format.c | 14 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..f080d3c 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 8fb4d59..58ef3e6 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2187,8 +2187,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 fe8a61a..e9612b3 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_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 53fa002..e279b9f 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define PAGE_CACHE_SIZE4096
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
-#define CHECKSUM_OFFSET4092
+#define CP_CHKSUM_OFFSET   4092
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
 } __attribute__((packed));
 
 #define MAX_SIT_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
 #define MAX_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
 
 /*
  * For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..621126c 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ static int f2fs_prepare_super_block(void)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CHECKSUM_OFFSET -
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
-   CHECKSUM_OFFSET - sizeof(struct 

[f2fs-dev] [RFC PATCH v2 3/4] f2fs-tools: unify the writeback of superblock

2018-08-14 Thread Junling Zheng
Introduce __write_superblock() to support updating specified one
superblock or both, thus we can wrapper it in update_superblock() and
f2fs_write_super_block to unify all places where sb needs to be updated.

Signed-off-by: Junling Zheng 
---
v1 -> v2:
 - if dev_write_block failed, add some notes and free buf to avoid memory leak.
 fsck/fsck.h|  2 +-
 fsck/mount.c   | 74 +++---
 fsck/resize.c  | 20 ++---
 include/f2fs_fs.h  | 35 ++
 mkfs/f2fs_format.c | 19 +---
 5 files changed, 56 insertions(+), 94 deletions(-)

diff --git a/fsck/fsck.h b/fsck/fsck.h
index 6042e68..e3490e6 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -178,7 +178,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
int);
 extern void write_curseg_info(struct f2fs_sb_info *);
 extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
 extern void write_checkpoint(struct f2fs_sb_info *);
-extern void write_superblock(struct f2fs_super_block *);
+extern void update_superblock(struct f2fs_super_block *, int);
 extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
 extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
 
diff --git a/fsck/mount.c b/fsck/mount.c
index 58ef3e6..e7ceb8d 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -476,7 +476,7 @@ void print_sb_state(struct f2fs_super_block *sb)
 }
 
 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
-   u64 offset)
+   enum SB_ADDR sb_addr)
 {
u32 segment0_blkaddr = get_sb(segment0_blkaddr);
u32 cp_blkaddr = get_sb(cp_blkaddr);
@@ -542,14 +542,11 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
segment_count_main << log_blocks_per_seg);
return -1;
} else if (main_end_blkaddr < seg_end_blkaddr) {
-   int err;
-
set_sb(segment_count, (main_end_blkaddr -
segment0_blkaddr) >> log_blocks_per_seg);
 
-   err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
-   MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
-   err ? "failed": "done",
+   update_superblock(sb, 1 << sb_addr);
+   MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
main_blkaddr,
segment0_blkaddr +
(segment_count << log_blocks_per_seg),
@@ -558,7 +555,7 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
-int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
+int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
@@ -600,30 +597,24 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
return -1;
 
-   if (sanity_check_area_boundary(sb, offset))
+   if (sanity_check_area_boundary(sb, sb_addr))
return -1;
return 0;
 }
 
-int validate_super_block(struct f2fs_sb_info *sbi, int block)
+int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
 {
-   u64 offset;
char buf[F2FS_BLKSIZE];
 
sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
 
-   if (block == 0)
-   offset = F2FS_SUPER_OFFSET;
-   else
-   offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
-
-   if (dev_read_block(buf, block))
+   if (dev_read_block(buf, sb_addr))
return -1;
 
memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
sizeof(struct f2fs_super_block));
 
-   if (!sanity_check_raw_super(sbi->raw_super, offset)) {
+   if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
/* get kernel version */
if (c.kd >= 0) {
dev_read_version(c.version, 0, VERSION_LEN);
@@ -642,13 +633,9 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
MSG(0, "Info: FSCK version\n  from \"%s\"\nto \"%s\"\n",
c.sb_version, c.version);
if (memcmp(c.sb_version, c.version, VERSION_LEN)) {
-   int ret;
-
memcpy(sbi->raw_super->version,
c.version, VERSION_LEN);
-   ret = dev_write(sbi->raw_super, offset,
-   sizeof(struct f2fs_super_block));
-   ASSE

[f2fs-dev] [RFC PATCH RESEND 4/4] f2fs-tools: introduce sb checksum

2018-08-14 Thread Junling Zheng
This patch introduced crc for superblock.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fsck/mount.c   | 23 +++
 fsck/resize.c  | 12 ++--
 include/f2fs_fs.h  | 16 +++-
 mkfs/f2fs_format.c |  3 +++
 4 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index e7ceb8d..af9b219 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -555,10 +559,29 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
+static int verify_sb_chksum(struct f2fs_super_block *sb)
+{
+   if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   return 0;
+}
+
 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
+   verify_sb_chksum(sb))
+   return -1;
+
if (F2FS_SUPER_MAGIC != get_sb(magic))
return -1;
 
diff --git a/fsck/resize.c b/fsck/resize.c
index 5161a1f..3462165 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = new_main_blkaddr - old_main_blkaddr;
@@ -629,6 +626,9 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
migrate_sit(sbi, new_sb, offset_seg);
rebuild_checkpoint(sbi, new_sb, offset_seg);
update_superblock(new_sb, SB_ALL);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
@@ -658,9 +658,6 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = old_main_blkaddr - new_main_blkaddr;
@@ -690,6 +687,9 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
/* move whole data region */
//if (err)
//  migrate_main(sbi, offset);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 791e64f..7446baa 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
 #define CP_CHKSUM_OFFSET   4092
+#define SB_CHKSUM_OFFSET   3068
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -579,6 +580,7 @@ enum {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define MAX_VOLUME_NAME512
 
@@ -632,7 +634,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES];   /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
-   __u8 reserved[314]; /* valid reserved region */
+   __u8 reserved[310]; /* valid reserved region */
+   __le32 crc; /* checksum of superblock */
 } __attribute__((packed));
 
 /*
@@ -1337,6 +1340,7 @@ struct feature feature_table[] = {
\
{ "inode_crtime",   F2FS_FEATURE_INODE_CRTIME },\
{ "lost_found",   

Re: [f2fs-dev] [RFC PATCH 3/4] f2fs-tools: unify the writeback of superblock

2018-08-13 Thread Junling Zheng
On 2018/8/13 22:25, Chao Yu wrote:
> On 2018/8/13 21:32, Junling Zheng wrote:
>> Introduce __write_superblock() to support updating specified one
>> superblock or both, thus we can wrapper it in update_superblock() and
>> f2fs_write_super_block to unify all places where sb needs to be updated.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>>  fsck/fsck.h|  2 +-
>>  fsck/mount.c   | 74 +++---
>>  fsck/resize.c  | 20 ++---
>>  include/f2fs_fs.h  | 31 +++
>>  mkfs/f2fs_format.c | 19 +---
>>  5 files changed, 52 insertions(+), 94 deletions(-)
>>
>> diff --git a/fsck/fsck.h b/fsck/fsck.h
>> index 6042e68..e3490e6 100644
>> --- a/fsck/fsck.h
>> +++ b/fsck/fsck.h
>> @@ -178,7 +178,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
>> int);
>>  extern void write_curseg_info(struct f2fs_sb_info *);
>>  extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
>>  extern void write_checkpoint(struct f2fs_sb_info *);
>> -extern void write_superblock(struct f2fs_super_block *);
>> +extern void update_superblock(struct f2fs_super_block *, int);
>>  extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
>>  extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, 
>> block_t);
>>  
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 58ef3e6..e7ceb8d 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -476,7 +476,7 @@ void print_sb_state(struct f2fs_super_block *sb)
>>  }
>>  
>>  static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
>> -u64 offset)
>> +enum SB_ADDR sb_addr)
>>  {
>>  u32 segment0_blkaddr = get_sb(segment0_blkaddr);
>>  u32 cp_blkaddr = get_sb(cp_blkaddr);
>> @@ -542,14 +542,11 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  segment_count_main << log_blocks_per_seg);
>>  return -1;
>>  } else if (main_end_blkaddr < seg_end_blkaddr) {
>> -int err;
>> -
>>  set_sb(segment_count, (main_end_blkaddr -
>>  segment0_blkaddr) >> log_blocks_per_seg);
>>  
>> -err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
>> -MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
>> -err ? "failed": "done",
>> +update_superblock(sb, 1 << sb_addr);
>> +MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
>>  main_blkaddr,
>>  segment0_blkaddr +
>>  (segment_count << log_blocks_per_seg),
>> @@ -558,7 +555,7 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  return 0;
>>  }
>>  
>> -int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
>> +int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR 
>> sb_addr)
>>  {
>>  unsigned int blocksize;
>>  
>> @@ -600,30 +597,24 @@ int sanity_check_raw_super(struct f2fs_super_block 
>> *sb, u64 offset)
>>  if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
>>  return -1;
>>  
>> -if (sanity_check_area_boundary(sb, offset))
>> +if (sanity_check_area_boundary(sb, sb_addr))
>>  return -1;
>>  return 0;
>>  }
>>  
>> -int validate_super_block(struct f2fs_sb_info *sbi, int block)
>> +int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
>>  {
>> -u64 offset;
>>  char buf[F2FS_BLKSIZE];
>>  
>>  sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
>>  
>> -if (block == 0)
>> -offset = F2FS_SUPER_OFFSET;
>> -else
>> -offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
>> -
>> -if (dev_read_block(buf, block))
>> +if (dev_read_block(buf, sb_addr))
>>  return -1;
>>  
>>  memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
>>  sizeof(struct f2fs_super_block));
>>  
>> -if (!sanity_check_raw_super(sbi->raw_super, offset)) {
>> +if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
>>  /* get kernel versio

Re: [f2fs-dev] [RFC PATCH 4/4] f2fs-tools: introduce sb checksum

2018-08-13 Thread Junling Zheng
Hi, Chao

On 2018/8/13 22:33, Chao Yu wrote:
> On 2018/8/13 21:32, Junling Zheng wrote:
>> This patch introduced crc for superblock.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>>  fsck/mount.c   | 23 +++
>>  fsck/resize.c  | 12 ++--
>>  include/f2fs_fs.h  | 16 +++-
>>  mkfs/f2fs_format.c |  3 +++
>>  4 files changed, 47 insertions(+), 7 deletions(-)
>>
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index e7ceb8d..af9b219 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>>  DISP_u32(sb, node_ino);
>>  DISP_u32(sb, meta_ino);
>>  DISP_u32(sb, cp_payload);
>> +DISP_u32(sb, crc);
> 
> if (feature is on) {
>   DISP_u32(sb, checksum_offset);
>   DISP_u32(sb, crc);
> }
> 

sb->checksum_offset had been printed before without any conditions.
So I think it's better to print them always :)

> Thanks,
> 
>>  DISP("%-.256s", sb, version);
>>  printf("\n");
>>  }
>> @@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
>>  if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
>>  MSG(0, "%s", " lost_found");
>>  }
>> +if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
>> +MSG(0, "%s", " sb_checksum");
>> +}
>>  MSG(0, "\n");
>>  MSG(0, "Info: superblock encrypt level = %d, salt = ",
>>  sb->encryption_level);
>> @@ -555,10 +559,29 @@ static inline int sanity_check_area_boundary(struct 
>> f2fs_super_block *sb,
>>  return 0;
>>  }
>>  
>> +static int verify_sb_chksum(struct f2fs_super_block *sb)
>> +{
>> +if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
>> +MSG(0, "\tInvalid SB CRC offset: %u\n",
>> +get_sb(checksum_offset));
>> +return -1;
>> +}
>> +if (f2fs_crc_valid(get_sb(crc), sb,
>> +get_sb(checksum_offset))) {
>> +MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
>> +return -1;
>> +}
>> +return 0;
>> +}
>> +
>>  int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR 
>> sb_addr)
>>  {
>>  unsigned int blocksize;
>>  
>> +if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
>> +verify_sb_chksum(sb))
>> +return -1;
>> +
>>  if (F2FS_SUPER_MAGIC != get_sb(magic))
>>  return -1;
>>  
>> diff --git a/fsck/resize.c b/fsck/resize.c
>> index 5161a1f..3462165 100644
>> --- a/fsck/resize.c
>> +++ b/fsck/resize.c
>> @@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
>>  }
>>  }
>>  
>> -print_raw_sb_info(sb);
>> -print_raw_sb_info(new_sb);
>> -
>>  old_main_blkaddr = get_sb(main_blkaddr);
>>  new_main_blkaddr = get_newsb(main_blkaddr);
>>  offset = new_main_blkaddr - old_main_blkaddr;
>> @@ -629,6 +626,9 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
>>  migrate_sit(sbi, new_sb, offset_seg);
>>  rebuild_checkpoint(sbi, new_sb, offset_seg);
>>  update_superblock(new_sb, SB_ALL);
>> +print_raw_sb_info(sb);
>> +print_raw_sb_info(new_sb);
>> +
>>  return 0;
>>  }
>>  
>> @@ -658,9 +658,6 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
>>  }
>>  }
>>  
>> -print_raw_sb_info(sb);
>> -print_raw_sb_info(new_sb);
>> -
>>  old_main_blkaddr = get_sb(main_blkaddr);
>>  new_main_blkaddr = get_newsb(main_blkaddr);
>>  offset = old_main_blkaddr - new_main_blkaddr;
>> @@ -690,6 +687,9 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
>>  /* move whole data region */
>>  //if (err)
>>  //  migrate_main(sbi, offset);
>> +print_raw_sb_info(sb);
>> +print_raw_sb_info(new_sb);
>> +
>>  return 0;
>>  }
>>  
>> diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
>> index b0d9b9b..556d526 100644
>> --- a/include/f2fs_fs.h
>> +++ b/include/f2fs_fs.h
>> @@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
>>  #define BITS_PER_BYTE   8
>>  #define F2FS_SUPER_MAGIC0

[f2fs-dev] [RFC PATCH 3/4] f2fs-tools: unify the writeback of superblock

2018-08-13 Thread Junling Zheng
Introduce __write_superblock() to support updating specified one
superblock or both, thus we can wrapper it in update_superblock() and
f2fs_write_super_block to unify all places where sb needs to be updated.

Signed-off-by: Junling Zheng 
---
 fsck/fsck.h|  2 +-
 fsck/mount.c   | 74 +++---
 fsck/resize.c  | 20 ++---
 include/f2fs_fs.h  | 31 +++
 mkfs/f2fs_format.c | 19 +---
 5 files changed, 52 insertions(+), 94 deletions(-)

diff --git a/fsck/fsck.h b/fsck/fsck.h
index 6042e68..e3490e6 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -178,7 +178,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64, 
int);
 extern void write_curseg_info(struct f2fs_sb_info *);
 extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
 extern void write_checkpoint(struct f2fs_sb_info *);
-extern void write_superblock(struct f2fs_super_block *);
+extern void update_superblock(struct f2fs_super_block *, int);
 extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
 extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
 
diff --git a/fsck/mount.c b/fsck/mount.c
index 58ef3e6..e7ceb8d 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -476,7 +476,7 @@ void print_sb_state(struct f2fs_super_block *sb)
 }
 
 static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
-   u64 offset)
+   enum SB_ADDR sb_addr)
 {
u32 segment0_blkaddr = get_sb(segment0_blkaddr);
u32 cp_blkaddr = get_sb(cp_blkaddr);
@@ -542,14 +542,11 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
segment_count_main << log_blocks_per_seg);
return -1;
} else if (main_end_blkaddr < seg_end_blkaddr) {
-   int err;
-
set_sb(segment_count, (main_end_blkaddr -
segment0_blkaddr) >> log_blocks_per_seg);
 
-   err = dev_write(sb, offset, sizeof(struct f2fs_super_block));
-   MSG(0, "Info: Fix alignment: %s, start(%u) end(%u) block(%u)\n",
-   err ? "failed": "done",
+   update_superblock(sb, 1 << sb_addr);
+   MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
main_blkaddr,
segment0_blkaddr +
(segment_count << log_blocks_per_seg),
@@ -558,7 +555,7 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
-int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
+int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
@@ -600,30 +597,24 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
if (get_sb(segment_count) > F2FS_MAX_SEGMENT)
return -1;
 
-   if (sanity_check_area_boundary(sb, offset))
+   if (sanity_check_area_boundary(sb, sb_addr))
return -1;
return 0;
 }
 
-int validate_super_block(struct f2fs_sb_info *sbi, int block)
+int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
 {
-   u64 offset;
char buf[F2FS_BLKSIZE];
 
sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
 
-   if (block == 0)
-   offset = F2FS_SUPER_OFFSET;
-   else
-   offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
-
-   if (dev_read_block(buf, block))
+   if (dev_read_block(buf, sb_addr))
return -1;
 
memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
sizeof(struct f2fs_super_block));
 
-   if (!sanity_check_raw_super(sbi->raw_super, offset)) {
+   if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
/* get kernel version */
if (c.kd >= 0) {
dev_read_version(c.version, 0, VERSION_LEN);
@@ -642,13 +633,9 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
MSG(0, "Info: FSCK version\n  from \"%s\"\nto \"%s\"\n",
c.sb_version, c.version);
if (memcmp(c.sb_version, c.version, VERSION_LEN)) {
-   int ret;
-
memcpy(sbi->raw_super->version,
c.version, VERSION_LEN);
-   ret = dev_write(sbi->raw_super, offset,
-   sizeof(struct f2fs_super_block));
-   ASSERT(ret >= 0);
+   update_superblock(sbi->raw_super, 1 << sb_addr);
 

[f2fs-dev] [RFC PATCH RESEND 2/4] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET

2018-08-13 Thread Junling Zheng
This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fsck/fsck.c|  4 ++--
 fsck/mount.c   |  4 ++--
 fsck/resize.c  |  8 
 include/f2fs_fs.h  |  6 +++---
 mkfs/f2fs_format.c | 14 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..f080d3c 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 8fb4d59..58ef3e6 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2187,8 +2187,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 fe8a61a..e9612b3 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_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 53fa002..e279b9f 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define PAGE_CACHE_SIZE4096
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
-#define CHECKSUM_OFFSET4092
+#define CP_CHKSUM_OFFSET   4092
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
 } __attribute__((packed));
 
 #define MAX_SIT_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
 #define MAX_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
 
 /*
  * For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..621126c 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ static int f2fs_prepare_super_block(void)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CHECKSUM_OFFSET -
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
-   CHECKSUM_OFFSET - sizeof(struct 

[f2fs-dev] [RFC PATCH 4/4] f2fs-tools: introduce sb checksum

2018-08-13 Thread Junling Zheng
This patch introduced crc for superblock.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c   | 23 +++
 fsck/resize.c  | 12 ++--
 include/f2fs_fs.h  | 16 +++-
 mkfs/f2fs_format.c |  3 +++
 4 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index e7ceb8d..af9b219 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -555,10 +559,29 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
+static int verify_sb_chksum(struct f2fs_super_block *sb)
+{
+   if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   return 0;
+}
+
 int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 {
unsigned int blocksize;
 
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
+   verify_sb_chksum(sb))
+   return -1;
+
if (F2FS_SUPER_MAGIC != get_sb(magic))
return -1;
 
diff --git a/fsck/resize.c b/fsck/resize.c
index 5161a1f..3462165 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -603,9 +603,6 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = new_main_blkaddr - old_main_blkaddr;
@@ -629,6 +626,9 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
migrate_sit(sbi, new_sb, offset_seg);
rebuild_checkpoint(sbi, new_sb, offset_seg);
update_superblock(new_sb, SB_ALL);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
@@ -658,9 +658,6 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
}
}
 
-   print_raw_sb_info(sb);
-   print_raw_sb_info(new_sb);
-
old_main_blkaddr = get_sb(main_blkaddr);
new_main_blkaddr = get_newsb(main_blkaddr);
offset = old_main_blkaddr - new_main_blkaddr;
@@ -690,6 +687,9 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
/* move whole data region */
//if (err)
//  migrate_main(sbi, offset);
+   print_raw_sb_info(sb);
+   print_raw_sb_info(new_sb);
+
return 0;
 }
 
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index b0d9b9b..556d526 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -279,6 +279,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
 #define CP_CHKSUM_OFFSET   4092
+#define SB_CHKSUM_OFFSET   3068
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -579,6 +580,7 @@ enum {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define MAX_VOLUME_NAME512
 
@@ -632,7 +634,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES];   /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
-   __u8 reserved[314]; /* valid reserved region */
+   __u8 reserved[310]; /* valid reserved region */
+   __le32 crc; /* checksum of superblock */
 } __attribute__((packed));
 
 /*
@@ -1337,6 +1340,7 @@ struct feature feature_table[] = {
\
{ "inode_crtime",   F2FS_FEATURE_INODE_CRTIME },\
{ "lost_found",   

[f2fs-dev] [RFC PATCH RESEND 1/4] f2fs: support superblock checksum

2018-08-13 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Reviewed-by: Chao Yu 
Signed-off-by: Junling Zheng 
---
 fs/f2fs/f2fs.h  |  2 ++
 fs/f2fs/super.c | 29 +
 fs/f2fs/sysfs.c |  7 +++
 include/linux/f2fs_fs.h |  3 ++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..3ffc336caea8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
+
+   /* Check checksum_offset and crc in superblock */
+   if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %zu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,13 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover && f2fs_sb_has_sb_chksum(sbi->sb)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+   if (f2fs_sb_has_sb_chksum(sb))
+   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+   len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
 }
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+   FEAT_SB_CHECKSUM,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+   case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -433,6 +438,7 @@ F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, 
FEAT_FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
+F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
 

[f2fs-dev] [RFC PATCH v4 1/3] f2fs: support superblock checksum

2018-08-08 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Signed-off-by: Junling Zheng 
---
v3 -> v4:
 - use '%zu' for size_t to fix compile warning.
 - use f2fs_sb_has_sb_chksum() instead of F2FS_HAS_FEATURE().
v2 -> v3:
 - add sysfs entry for superblock checksum.
 - move the crc checking to the beginning of sanity_check_raw_super().
 - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
   sanity_check_raw_super().
v1 -> v2:
 - fix to switch endian of crc.
 fs/f2fs/f2fs.h  |  2 ++
 fs/f2fs/super.c | 29 +
 fs/f2fs/sysfs.c |  7 +++
 include/linux/f2fs_fs.h |  3 ++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..3ffc336caea8 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
+
+   /* Check checksum_offset and crc in superblock */
+   if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %zu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover &&
+   f2fs_sb_has_sb_chksum(sbi->sb)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+   if (f2fs_sb_has_sb_chksum(sb))
+   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+   len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
 }
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+   FEAT_SB_CHECKSUM,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+   case FEAT_SB_CHECKSUM

Re: [f2fs-dev] [RFC PATCH v3 1/3] f2fs: support superblock checksum

2018-08-08 Thread Junling Zheng
On 2018/8/8 20:24, Chao Yu wrote:
> On 2018/8/8 20:21, Junling Zheng wrote:
>> On 2018/8/8 20:03, Chao Yu wrote:
>>> On 2018/8/8 18:27, Junling Zheng wrote:
>>>> Now we support crc32 checksum for superblock.
>>>>
>>>> Signed-off-by: Junling Zheng 
>>>> ---
>>>> v2 -> v3:
>>>>  - add sysfs entry for superblock checksum.
>>>>  - move the crc checking to the beginning of sanity_check_raw_super().
>>>>  - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
>>>>sanity_check_raw_super().
>>>> v1 -> v2:
>>>>  - fix to switch endian of crc.
>>>>  fs/f2fs/f2fs.h  |  2 ++
>>>>  fs/f2fs/super.c | 29 +
>>>>  fs/f2fs/sysfs.c |  7 +++
>>>>  include/linux/f2fs_fs.h |  3 ++-
>>>>  4 files changed, 40 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
>>>> index 4525f4f82af0..d50d6efda96b 100644
>>>> --- a/fs/f2fs/f2fs.h
>>>> +++ b/fs/f2fs/f2fs.h
>>>> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
>>>>  #define F2FS_FEATURE_INODE_CRTIME 0x0100
>>>>  #define F2FS_FEATURE_LOST_FOUND   0x0200
>>>>  #define F2FS_FEATURE_VERITY   0x0400  /* reserved */
>>>> +#define F2FS_FEATURE_SB_CHKSUM0x0800
>>>>  
>>>>  #define F2FS_HAS_FEATURE(sb, mask)
>>>> \
>>>>((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
>>>> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
>>>> FLEXIBLE_INLINE_XATTR);
>>>>  F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
>>>>  F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
>>>>  F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
>>>> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>>>>  
>>>>  #ifdef CONFIG_BLK_DEV_ZONED
>>>>  static inline int get_blkz_type(struct f2fs_sb_info *sbi,
>>>> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
>>>> index bd57be470e23..c3dbafa31613 100644
>>>> --- a/fs/f2fs/super.c
>>>> +++ b/fs/f2fs/super.c
>>>> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct 
>>>> f2fs_sb_info *sbi,
>>>>(bh->b_data + F2FS_SUPER_OFFSET);
>>>>struct super_block *sb = sbi->sb;
>>>>unsigned int blocksize;
>>>> +  size_t crc_offset = 0;
>>>> +  __u32 crc = 0;
>>>> +
>>>> +  /* Check checksum_offset and crc in superblock */
>>>> +  if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
>>>> +  crc_offset = le32_to_cpu(raw_super->checksum_offset);
>>>> +  if (crc_offset !=
>>>> +  offsetof(struct f2fs_super_block, crc)) {
>>>> +  f2fs_msg(sb, KERN_INFO,
>>>> +  "Invalid SB checksum offset: %lu",
>>>
>>> warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but 
>>> argument
>>> 4 has type ‘size_t’ [-Wformat=]
>>>  crc_offset);
>>>
>>
>> :( sorry, I'll send a v4 patch.
>>
>>> Otherwise, it looks good to me, you can add in next version:
>>>
>>> Reviewed-by: Chao Yu 
>>>
>>> Thanks,
>>>
>>>> +  crc_offset);
>>>> +  return 1;
>>>> +  }
>>>> +  crc = le32_to_cpu(raw_super->crc);
>>>> +  if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
>>>> +  f2fs_msg(sb, KERN_INFO,
>>>> +  "Invalid SB checksum value: %u", crc);
>>>> +  return 1;
>>>> +  }
>>>> +  }
>>>>  
>>>>if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
>>>>f2fs_msg(sb, KERN_INFO,
>>>> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info 
>>>> *sbi,
>>>>  int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>>>>  {
>>>>struct buffer_head *bh;
>>>> +  __u32 crc = 0;
>>>>int err;
>>>>  
>>>>if ((recover && f2fs_readonly(sbi->sb)) ||
>>>> @@ -257

Re: [f2fs-dev] [RFC PATCH v3 1/3] f2fs: support superblock checksum

2018-08-08 Thread Junling Zheng
On 2018/8/8 20:03, Chao Yu wrote:
> On 2018/8/8 18:27, Junling Zheng wrote:
>> Now we support crc32 checksum for superblock.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>> v2 -> v3:
>>  - add sysfs entry for superblock checksum.
>>  - move the crc checking to the beginning of sanity_check_raw_super().
>>  - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
>>sanity_check_raw_super().
>> v1 -> v2:
>>  - fix to switch endian of crc.
>>  fs/f2fs/f2fs.h  |  2 ++
>>  fs/f2fs/super.c | 29 +
>>  fs/f2fs/sysfs.c |  7 +++
>>  include/linux/f2fs_fs.h |  3 ++-
>>  4 files changed, 40 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
>> index 4525f4f82af0..d50d6efda96b 100644
>> --- a/fs/f2fs/f2fs.h
>> +++ b/fs/f2fs/f2fs.h
>> @@ -147,6 +147,7 @@ struct f2fs_mount_info {
>>  #define F2FS_FEATURE_INODE_CRTIME   0x0100
>>  #define F2FS_FEATURE_LOST_FOUND 0x0200
>>  #define F2FS_FEATURE_VERITY 0x0400  /* reserved */
>> +#define F2FS_FEATURE_SB_CHKSUM  0x0800
>>  
>>  #define F2FS_HAS_FEATURE(sb, mask)  \
>>  ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
>> @@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
>> FLEXIBLE_INLINE_XATTR);
>>  F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
>>  F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
>>  F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
>> +F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
>>  
>>  #ifdef CONFIG_BLK_DEV_ZONED
>>  static inline int get_blkz_type(struct f2fs_sb_info *sbi,
>> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
>> index bd57be470e23..c3dbafa31613 100644
>> --- a/fs/f2fs/super.c
>> +++ b/fs/f2fs/super.c
>> @@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
>> *sbi,
>>  (bh->b_data + F2FS_SUPER_OFFSET);
>>  struct super_block *sb = sbi->sb;
>>  unsigned int blocksize;
>> +size_t crc_offset = 0;
>> +__u32 crc = 0;
>> +
>> +/* Check checksum_offset and crc in superblock */
>> +if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
>> +crc_offset = le32_to_cpu(raw_super->checksum_offset);
>> +if (crc_offset !=
>> +offsetof(struct f2fs_super_block, crc)) {
>> +f2fs_msg(sb, KERN_INFO,
>> +"Invalid SB checksum offset: %lu",
> 
> warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but 
> argument
> 4 has type ‘size_t’ [-Wformat=]
>  crc_offset);
> 

:( sorry, I'll send a v4 patch.

> Otherwise, it looks good to me, you can add in next version:
> 
> Reviewed-by: Chao Yu 
> 
> Thanks,
> 
>> +crc_offset);
>> +return 1;
>> +}
>> +crc = le32_to_cpu(raw_super->crc);
>> +if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
>> +f2fs_msg(sb, KERN_INFO,
>> +"Invalid SB checksum value: %u", crc);
>> +return 1;
>> +}
>> +}
>>  
>>  if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
>>  f2fs_msg(sb, KERN_INFO,
>> @@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info 
>> *sbi,
>>  int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
>>  {
>>  struct buffer_head *bh;
>> +__u32 crc = 0;
>>  int err;
>>  
>>  if ((recover && f2fs_readonly(sbi->sb)) ||
>> @@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
>> recover)
>>  return -EROFS;
>>  }
>>  
>> +/* we should update superblock crc here */
>> +if (!recover &&
>> +F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
>> +crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
>> +offsetof(struct f2fs_super_block, crc));
>> +F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
>> +}
>> +
>>  /* write back-up superblock first */
>>  bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
>>  if (!bh)
>> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
>> index cd2e030e47b8..c86d91be6c48 100644
&

[f2fs-dev] [RFC PATCH v2 3/3] f2fs-tools: introduce sb checksum

2018-08-08 Thread Junling Zheng
This patch introduced superblock checksum.

Signed-off-by: Junling Zheng 
---
v1 -> v2:
 - Rename CHKSUM_OFFSET_SB to SB_CHKSUM_OFFSET.
 - Introduce update_sb_chksum() to update superblock checksum.
 fsck/fsck.h|  1 +
 fsck/mount.c   | 44 
 fsck/resize.c  |  3 +++
 include/f2fs_fs.h  |  6 +-
 mkfs/f2fs_format.c |  9 +
 5 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/fsck/fsck.h b/fsck/fsck.h
index 6042e68..2172cac 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -170,6 +170,7 @@ extern int fsck_verify(struct f2fs_sb_info *);
 extern void fsck_free(struct f2fs_sb_info *);
 extern int f2fs_do_mount(struct f2fs_sb_info *);
 extern void f2fs_do_umount(struct f2fs_sb_info *);
+extern void update_sb_chksum(struct f2fs_super_block *);
 
 extern void flush_journal_entries(struct f2fs_sb_info *);
 extern void zero_journal_entries(struct f2fs_sb_info *);
diff --git a/fsck/mount.c b/fsck/mount.c
index 58ef3e6..1dfabba 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -558,10 +562,29 @@ static inline int sanity_check_area_boundary(struct 
f2fs_super_block *sb,
return 0;
 }
 
+static int verify_sb_chksum(struct f2fs_super_block *sb)
+{
+   if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   return 0;
+}
+
 int sanity_check_raw_super(struct f2fs_super_block *sb, u64 offset)
 {
unsigned int blocksize;
 
+   if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
+   verify_sb_chksum(sb))
+   return -1;
+
if (F2FS_SUPER_MAGIC != get_sb(magic))
return -1;
 
@@ -605,6 +628,18 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
return 0;
 }
 
+void update_sb_chksum(struct f2fs_super_block *sb)
+{
+   u32 old_crc, new_crc;
+
+   old_crc = get_sb(crc);
+   new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
+   SB_CHKSUM_OFFSET);
+   set_sb(crc, new_crc);
+   MSG(0, "Info: update SB CRC successfully "
+   "(0x%x --> 0x%x)\n", old_crc, new_crc);
+}
+
 int validate_super_block(struct f2fs_sb_info *sbi, int block)
 {
u64 offset;
@@ -646,6 +681,10 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
 
memcpy(sbi->raw_super->version,
c.version, VERSION_LEN);
+   if (le32_to_cpu(sbi->raw_super->feature) &
+   F2FS_FEATURE_SB_CHKSUM)
+   update_sb_chksum(sbi->raw_super);
+
ret = dev_write(sbi->raw_super, offset,
sizeof(struct f2fs_super_block));
ASSERT(ret >= 0);
@@ -2402,6 +2441,9 @@ static int check_sector_size(struct f2fs_super_block *sb)
set_sb(log_sectorsize, log_sectorsize);
set_sb(log_sectors_per_block, log_sectors_per_block);
 
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
+   update_sb_chksum(sb);
+
memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
for (index = 0; index < 2; index++) {
@@ -2434,6 +2476,8 @@ static void tune_sb_features(struct f2fs_sb_info *sbi)
if (!sb_changed)
return;
 
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
+   update_sb_chksum(sb);
write_superblock(sb);
 }
 
diff --git a/fsck/resize.c b/fsck/resize.c
index e9612b3..ca8c6f9 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -160,6 +160,9 @@ safe_resize:
(get_sb(segment_count_main) - 2));

[f2fs-dev] [RFC PATCH v3 1/3] f2fs: support superblock checksum

2018-08-08 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Signed-off-by: Junling Zheng 
---
v2 -> v3:
 - add sysfs entry for superblock checksum.
 - move the crc checking to the beginning of sanity_check_raw_super().
 - fix the NULL pointer dereference as sb->s_fs_info hasn't be set yet in
   sanity_check_raw_super().
v1 -> v2:
 - fix to switch endian of crc.
 fs/f2fs/f2fs.h  |  2 ++
 fs/f2fs/super.c | 29 +
 fs/f2fs/sysfs.c |  7 +++
 include/linux/f2fs_fs.h |  3 ++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d50d6efda96b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
@@ -3376,6 +3377,7 @@ F2FS_FEATURE_FUNCS(flexible_inline_xattr, 
FLEXIBLE_INLINE_XATTR);
 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
+F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
 
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..c3dbafa31613 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,26 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
+
+   /* Check checksum_offset and crc in superblock */
+   if (le32_to_cpu(raw_super->feature) & F2FS_FEATURE_SB_CHKSUM) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %lu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2568,6 +2588,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2597,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover &&
+   F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index cd2e030e47b8..c86d91be6c48 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -120,6 +120,9 @@ static ssize_t features_show(struct f2fs_attr *a,
if (f2fs_sb_has_lost_found(sb))
len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
len ? ", " : "", "lost_found");
+   if (f2fs_sb_has_sb_chksum(sb))
+   len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
+   len ? ", " : "", "sb_checksum");
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
return len;
 }
@@ -337,6 +340,7 @@ enum feat_id {
FEAT_QUOTA_INO,
FEAT_INODE_CRTIME,
FEAT_LOST_FOUND,
+   FEAT_SB_CHECKSUM,
 };
 
 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
@@ -353,6 +357,7 @@ static ssize_t f2fs_feature_show(struct f2fs_attr *a,
case FEAT_QUOTA_INO:
case FEAT_INODE_CRTIME:
case FEAT_LOST_FOUND:
+   case FEAT_SB_CHECKSUM:
return snprintf(buf, PAGE_SIZE, "supported\n");
}
return 0;
@@ -4

[f2fs-dev] [RFC PATCH v2 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CP_CHKSUM_OFFSET

2018-08-08 Thread Junling Zheng
This patch renamed CHECKSUM_OFFSET to CP_CHKSUM_OFFSET.

Signed-off-by: Junling Zheng 
---
v1 -> v2:
 - Rename CHKSUM_OFFSET_CP to CP_CHKSUM_OFFSET.
 fsck/fsck.c|  4 ++--
 fsck/mount.c   |  4 ++--
 fsck/resize.c  |  8 
 include/f2fs_fs.h  |  6 +++---
 mkfs/f2fs_format.c | 14 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..f080d3c 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 8fb4d59..58ef3e6 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2187,8 +2187,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)cp + CP_CHKSUM_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 fe8a61a..e9612b3 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CP_CHKSUM_OFFSET);
+   *((__le32 *)((unsigned char *)new_cp + CP_CHKSUM_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 53fa002..e279b9f 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define PAGE_CACHE_SIZE4096
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
-#define CHECKSUM_OFFSET4092
+#define CP_CHKSUM_OFFSET   4092
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
 } __attribute__((packed));
 
 #define MAX_SIT_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
 #define MAX_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+   (CP_CHKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
 
 /*
  * For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..621126c 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ static int f2fs_prepare_super_block(void)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CHECKSUM_OFFSET -
+   max_nat_bitmap_size = CP_CHKSUM_OFFSET -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
-   CHECKSU

[f2fs-dev] [RFC PATCH v2 1/3] f2fs: support superblock checksum

2018-08-07 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Signed-off-by: Junling Zheng 
---
v1 -> v1:
 - fix to switch endian of crc.
 fs/f2fs/f2fs.h  |  1 +
 fs/f2fs/super.c | 28 
 include/linux/f2fs_fs.h |  3 ++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d404ede01fcd 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..c782fab52a12 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,8 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2283,6 +2285,23 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
if (sanity_check_area_boundary(sbi, bh))
return 1;
 
+   if (F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %lu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
+
return 0;
 }
 
@@ -2568,6 +2587,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2596,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover &&
+   F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index f70f8ac9c4f4..aa4b586569c1 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -112,7 +112,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES];   /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
-   __u8 reserved[314]; /* valid reserved region */
+   __u8 reserved[310]; /* valid reserved region */
+   __le32 crc; /* checksum of superblock */
 } __packed;
 
 /*
-- 
2.18.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [RFC PATCH] f2fs: support superblock checksum

2018-08-07 Thread Junling Zheng
Now we support crc32 checksum for superblock.

Signed-off-by: Junling Zheng 
---
 fs/f2fs/f2fs.h  |  1 +
 fs/f2fs/super.c | 28 
 include/linux/f2fs_fs.h |  3 ++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 4525f4f82af0..d404ede01fcd 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -147,6 +147,7 @@ struct f2fs_mount_info {
 #define F2FS_FEATURE_INODE_CRTIME  0x0100
 #define F2FS_FEATURE_LOST_FOUND0x0200
 #define F2FS_FEATURE_VERITY0x0400  /* reserved */
+#define F2FS_FEATURE_SB_CHKSUM 0x0800
 
 #define F2FS_HAS_FEATURE(sb, mask) \
((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index bd57be470e23..dbb1a2bb9e8e 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2149,6 +2149,8 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
(bh->b_data + F2FS_SUPER_OFFSET);
struct super_block *sb = sbi->sb;
unsigned int blocksize;
+   size_t crc_offset = 0;
+   __u32 crc = 0;
 
if (F2FS_SUPER_MAGIC != le32_to_cpu(raw_super->magic)) {
f2fs_msg(sb, KERN_INFO,
@@ -2283,6 +2285,23 @@ static int sanity_check_raw_super(struct f2fs_sb_info 
*sbi,
if (sanity_check_area_boundary(sbi, bh))
return 1;
 
+   if (F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
+   crc_offset = le32_to_cpu(raw_super->checksum_offset);
+   if (crc_offset !=
+   offsetof(struct f2fs_super_block, crc)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum offset: %lu",
+   crc_offset);
+   return 1;
+   }
+   crc = le32_to_cpu(raw_super->crc);
+   if (!f2fs_crc_valid(sbi, crc, raw_super, crc_offset)) {
+   f2fs_msg(sb, KERN_INFO,
+   "Invalid SB checksum value: %u", crc);
+   return 1;
+   }
+   }
+
return 0;
 }
 
@@ -2568,6 +2587,7 @@ static int read_raw_super_block(struct f2fs_sb_info *sbi,
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
 {
struct buffer_head *bh;
+   __u32 crc = 0;
int err;
 
if ((recover && f2fs_readonly(sbi->sb)) ||
@@ -2576,6 +2596,14 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, bool 
recover)
return -EROFS;
}
 
+   /* we should update superblock crc here */
+   if (!recover &&
+   F2FS_HAS_FEATURE(sbi->sb, F2FS_FEATURE_SB_CHKSUM)) {
+   crc = f2fs_crc32(sbi, F2FS_RAW_SUPER(sbi),
+   offsetof(struct f2fs_super_block, crc));
+   F2FS_RAW_SUPER(sbi)->crc = crc;
+   }
+
/* write back-up superblock first */
bh = sb_bread(sbi->sb, sbi->valid_super_block ? 0 : 1);
if (!bh)
diff --git a/include/linux/f2fs_fs.h b/include/linux/f2fs_fs.h
index f70f8ac9c4f4..aa4b586569c1 100644
--- a/include/linux/f2fs_fs.h
+++ b/include/linux/f2fs_fs.h
@@ -112,7 +112,8 @@ struct f2fs_super_block {
struct f2fs_device devs[MAX_DEVICES];   /* device list */
__le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
__u8 hot_ext_count; /* # of hot file extension */
-   __u8 reserved[314]; /* valid reserved region */
+   __u8 reserved[310]; /* valid reserved region */
+   __le32 crc; /* checksum of superblock */
 } __packed;
 
 /*
-- 
2.18.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [RFC PATCH 3/3] f2fs-tools: introduce sb checksum

2018-08-07 Thread Junling Zheng
This patch introduced superblock checksum.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c   | 52 ++
 fsck/resize.c  | 11 ++
 include/f2fs_fs.h  |  6 +-
 mkfs/f2fs_format.c |  9 
 4 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 957f531..b29af3c 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -340,6 +340,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
+   DISP_u32(sb, crc);
DISP("%-.256s", sb, version);
printf("\n");
 }
@@ -467,6 +468,9 @@ void print_sb_state(struct f2fs_super_block *sb)
if (f & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
MSG(0, "%s", " lost_found");
}
+   if (f & cpu_to_le32(F2FS_FEATURE_SB_CHKSUM)) {
+   MSG(0, "%s", " sb_checksum");
+   }
MSG(0, "\n");
MSG(0, "Info: superblock encrypt level = %d, salt = ",
sb->encryption_level);
@@ -602,6 +606,20 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
 
if (sanity_check_area_boundary(sb, offset))
return -1;
+
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+   if (CHKSUM_OFFSET_SB != get_sb(checksum_offset)) {
+   MSG(0, "\tInvalid SB CRC offset: %u\n",
+   get_sb(checksum_offset));
+   return -1;
+   }
+   if (f2fs_crc_valid(get_sb(crc), sb,
+   get_sb(checksum_offset))) {
+   MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
+   return -1;
+   }
+   }
+
return 0;
 }
 
@@ -609,6 +627,7 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
 {
u64 offset;
char buf[F2FS_BLKSIZE];
+   u32 old_crc, new_crc;
 
sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
 
@@ -646,6 +665,18 @@ int validate_super_block(struct f2fs_sb_info *sbi, int 
block)
 
memcpy(sbi->raw_super->version,
c.version, VERSION_LEN);
+   if (le32_to_cpu(sbi->raw_super->feature) &
+   F2FS_FEATURE_SB_CHKSUM) {
+   /* Recalculate CRC */
+   old_crc = le32_to_cpu(sbi->raw_super->crc);
+   new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC,
+   sbi->raw_super,
+   
CHKSUM_OFFSET_SB);
+   sbi->raw_super->crc = cpu_to_le32(new_crc);
+   MSG(0, "Info: update SB CRC successfully"
+   "(0x%x --> 0x%x)\n", old_crc, new_crc);
+   }
+
ret = dev_write(sbi->raw_super, offset,
sizeof(struct f2fs_super_block));
ASSERT(ret >= 0);
@@ -2453,6 +2484,7 @@ static int check_sector_size(struct f2fs_super_block *sb)
int index;
u_int32_t log_sectorsize, log_sectors_per_block;
u_int8_t *zero_buff;
+   u32 old_crc, new_crc;
 
log_sectorsize = log_base_2(c.sector_size);
log_sectors_per_block = log_base_2(c.sectors_per_blk);
@@ -2467,6 +2499,16 @@ static int check_sector_size(struct f2fs_super_block *sb)
set_sb(log_sectorsize, log_sectorsize);
set_sb(log_sectors_per_block, log_sectors_per_block);
 
+   if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
+   /* Recalculate CRC */
+   old_crc = get_sb(crc);
+   new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
+   CHKSUM_OFFSET_SB);
+   set_sb(crc, new_crc);
+   MSG(0, "Info: update SB CRC successfully"
+   "(0x%x --> 0x%x)\n", old_crc, new_crc);
+   }
+
memcpy(zero_buff + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
for (index = 0; index < 2; index++) {
@@ -2485,6 +2527,7 @@ static int check_sector_size(struct f2fs_super_block *sb)
 static void tune_sb_features(struct f2fs_sb_info *sbi)
 {
int sb_changed = 0;
+   u32 old_crc, new_crc;
struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
 
if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) &&
@@ -2499,6 +2542,15 @@ sta

[f2fs-dev] [RFC PATCH 2/3] f2fs-tools: rename CHECKSUM_OFFSET to CHKSUM_OFFSET_CP

2018-08-07 Thread Junling Zheng
This patch renamed CHECKSUM_OFFSET to CHKSUM_OFFSET_CP.

Signed-off-by: Junling Zheng 
---
 fsck/fsck.c|  4 ++--
 fsck/mount.c   |  4 ++--
 fsck/resize.c  |  8 
 include/f2fs_fs.h  |  6 +++---
 mkfs/f2fs_format.c | 14 +++---
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index d550403..da2fab4 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2010,8 +2010,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHKSUM_OFFSET_CP);
+   *((__le32 *)((unsigned char *)cp + CHKSUM_OFFSET_CP)) = 
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 2c2473d..957f531 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2252,8 +2252,8 @@ 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)cp + CHECKSUM_OFFSET)) = cpu_to_le32(crc);
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CHKSUM_OFFSET_CP);
+   *((__le32 *)((unsigned char *)cp + CHKSUM_OFFSET_CP)) = 
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 fe8a61a..94580db 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -90,10 +90,10 @@ 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 = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1;
+   max_nat_bitmap_size = CHKSUM_OFFSET_CP - sizeof(struct 
f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
-   max_nat_bitmap_size = CHECKSUM_OFFSET - sizeof(struct 
f2fs_checkpoint) + 1
+   max_nat_bitmap_size = CHKSUM_OFFSET_CP - sizeof(struct 
f2fs_checkpoint) + 1
- 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, CHECKSUM_OFFSET);
-   *((__le32 *)((unsigned char *)new_cp + CHECKSUM_OFFSET)) =
+   crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, new_cp, CHKSUM_OFFSET_CP);
+   *((__le32 *)((unsigned char *)new_cp + CHKSUM_OFFSET_CP)) =
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 2c086a9..dbeeb6a 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -278,7 +278,7 @@ static inline uint64_t bswap_64(uint64_t val)
 #define PAGE_CACHE_SIZE4096
 #define BITS_PER_BYTE  8
 #define F2FS_SUPER_MAGIC   0xF2F52010  /* F2FS Magic Number */
-#define CHECKSUM_OFFSET4092
+#define CHKSUM_OFFSET_CP   4092
 #define MAX_PATH_LEN   64
 #define MAX_DEVICES8
 
@@ -682,9 +682,9 @@ struct f2fs_checkpoint {
 } __attribute__((packed));
 
 #define MAX_SIT_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
+   (CHKSUM_OFFSET_CP - sizeof(struct f2fs_checkpoint) + 1 - 64)
 #define MAX_BITMAP_SIZE_IN_CKPT\
-   (CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1)
+   (CHKSUM_OFFSET_CP - sizeof(struct f2fs_checkpoint) + 1)
 
 /*
  * For orphan inode management
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4b88d93..038a589 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -342,12 +342,12 @@ static int f2fs_prepare_super_block(void)
 * It requires more pages for cp.
 */
if (max_sit_bitmap_size > MAX_SIT_BITMAP_SIZE_IN_CKPT) {
-   max_nat_bitmap_size = CHECKSUM_OFFSET -
+   max_nat_bitmap_size = CHKSUM_OFFSET_CP -
sizeof(struct f2fs_checkpoint) + 1;
set_sb(cp_payload, F2FS_BLK_ALIGN(max_sit_bitmap_size));
} else {
max_nat_bitmap_size =
-   CHECKSUM_OFFSET - sizeof(struct 

[f2fs-dev] [PATCH] fsck.f2fs: supply more check entries for checkpoint

2018-08-02 Thread Junling Zheng
Supply some more check entries for checkpoint in sanity_check_ckpt()
and validate_checkpoint() to sync them with kernel.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c  | 69 +--
 include/f2fs_fs.h |  1 +
 2 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 8fb4d59..2c2473d 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -747,6 +747,9 @@ void *validate_checkpoint(struct f2fs_sb_info *sbi, block_t 
cp_addr,
if (f2fs_crc_valid(crc, cp, crc_offset))
goto invalid_cp1;
 
+   if (get_cp(cp_pack_total_block_count) > sbi->blocks_per_seg)
+   goto invalid_cp1;
+
pre_version = get_cp(checkpoint_ver);
 
/* Read the 2nd cp block in this CP pack */
@@ -867,17 +870,79 @@ int sanity_check_ckpt(struct f2fs_sb_info *sbi)
unsigned int total, fsmeta;
struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
+   unsigned int ovp_segments, reserved_segments;
+   unsigned int main_segs, blocks_per_seg;
+   unsigned int sit_segs, nat_segs;
+   unsigned int sit_bitmap_size, nat_bitmap_size;
+   unsigned int log_blocks_per_seg;
+   unsigned int segment_count_main;
+   unsigned int cp_pack_start_sum, cp_payload;
+   block_t user_block_count;
+   int i;
 
total = get_sb(segment_count);
fsmeta = get_sb(segment_count_ckpt);
-   fsmeta += get_sb(segment_count_sit);
-   fsmeta += get_sb(segment_count_nat);
+   sit_segs = get_sb(segment_count_sit);
+   fsmeta += sit_segs;
+   nat_segs = get_sb(segment_count_nat);
+   fsmeta += nat_segs;
fsmeta += get_cp(rsvd_segment_count);
fsmeta += get_sb(segment_count_ssa);
 
if (fsmeta >= total)
return 1;
 
+   ovp_segments = get_cp(overprov_segment_count);
+   reserved_segments = get_cp(rsvd_segment_count);
+
+   if (fsmeta < F2FS_MIN_SEGMENT || ovp_segments == 0 ||
+   reserved_segments == 0) {
+   MSG(0, "\tWrong layout: check mkfs.f2fs version\n");
+   return 1;
+   }
+
+   user_block_count = get_cp(user_block_count);
+   segment_count_main = get_sb(segment_count_main);
+   log_blocks_per_seg = get_sb(log_blocks_per_seg);
+   if (!user_block_count || user_block_count >=
+   segment_count_main << log_blocks_per_seg) {
+   MSG(0, "\tWrong user_block_count(%u)\n", user_block_count);
+   return 1;
+   }
+
+   main_segs = get_sb(segment_count_main);
+   blocks_per_seg = sbi->blocks_per_seg;
+
+   for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
+   if (get_cp(cur_node_segno[i]) >= main_segs ||
+   get_cp(cur_node_blkoff[i]) >= blocks_per_seg)
+   return 1;
+   }
+   for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
+   if (get_cp(cur_data_segno[i]) >= main_segs ||
+   get_cp(cur_data_blkoff[i]) >= blocks_per_seg)
+   return 1;
+   }
+
+   sit_bitmap_size = get_cp(sit_ver_bitmap_bytesize);
+   nat_bitmap_size = get_cp(nat_ver_bitmap_bytesize);
+
+   if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
+   nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
+   MSG(0, "\tWrong bitmap size: sit(%u), nat(%u)\n",
+   sit_bitmap_size, nat_bitmap_size);
+   return 1;
+   }
+
+   cp_pack_start_sum = __start_sum_addr(sbi);
+   cp_payload = get_sb(cp_payload);
+   if (cp_pack_start_sum < cp_payload + 1 ||
+   cp_pack_start_sum > blocks_per_seg - 1 -
+   NR_CURSEG_TYPE) {
+   MSG(0, "\tWrong cp_pack_start_sum(%u)\n", cp_pack_start_sum);
+   return 1;
+   }
+
return 0;
 }
 
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 53fa002..2c086a9 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -888,6 +888,7 @@ struct f2fs_nat_block {
  * F2FS uses 4 bytes to represent block address. As a result, supported size of
  * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
  */
+#define F2FS_MIN_SEGMENT  9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
 #define F2FS_MAX_SEGMENT   ((16 * 1024 * 1024) / 2)
 #define MAX_SIT_BITMAP_SIZE(SEG_ALIGN(SIZE_ALIGN(F2FS_MAX_SEGMENT, \
SIT_ENTRY_PER_BLOCK)) * \
-- 
2.18.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing

Re: [f2fs-dev] [PATCH] f2fs: remove unused cp_blkaddr in f2fs_sanity_check_ckpt

2018-08-01 Thread Junling Zheng
On 2018/8/1 14:54, Chao Yu wrote:
> Hi Junling,
> 
> On 2018/7/31 13:21, Junling Zheng wrote:
>> Remove unused cp_blkaddr in f2fs_sanity_check_ckpt().
> 
> Since this issue is introduced by recent change, and the related patch has not
> been upstreamed yet, can we merge this into that patch, if you don't mind?
> 

OK, I don't mind :)

> Thanks,
> 
>>
>> Signed-off-by: Junling Zheng 
>> ---
>>  fs/f2fs/super.c | 3 +--
>>  1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
>> index d56bc6eb8760..6ee003e87f63 100644
>> --- a/fs/f2fs/super.c
>> +++ b/fs/f2fs/super.c
>> @@ -2297,7 +2297,7 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
>>  unsigned int sit_bitmap_size, nat_bitmap_size;
>>  unsigned int log_blocks_per_seg;
>>  unsigned int segment_count_main;
>> -unsigned int cp_pack_start_sum, cp_blkaddr, cp_payload;
>> +unsigned int cp_pack_start_sum, cp_payload;
>>  block_t user_block_count;
>>  int i;
>>  
>> @@ -2359,7 +2359,6 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
>>  }
>>  
>>  cp_pack_start_sum = __start_sum_addr(sbi);
>> -cp_blkaddr = __start_cp_addr(sbi);
>>  cp_payload = __cp_payload(sbi);
>>  if (cp_pack_start_sum < cp_payload + 1 ||
>>  cp_pack_start_sum > blocks_per_seg - 1 -
>>
> 
> 
> .
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] f2fs: remove unused cp_blkaddr in f2fs_sanity_check_ckpt

2018-07-30 Thread Junling Zheng
Remove unused cp_blkaddr in f2fs_sanity_check_ckpt().

Signed-off-by: Junling Zheng 
---
 fs/f2fs/super.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index d56bc6eb8760..6ee003e87f63 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2297,7 +2297,7 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
unsigned int sit_bitmap_size, nat_bitmap_size;
unsigned int log_blocks_per_seg;
unsigned int segment_count_main;
-   unsigned int cp_pack_start_sum, cp_blkaddr, cp_payload;
+   unsigned int cp_pack_start_sum, cp_payload;
block_t user_block_count;
int i;
 
@@ -2359,7 +2359,6 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
}
 
cp_pack_start_sum = __start_sum_addr(sbi);
-   cp_blkaddr = __start_cp_addr(sbi);
cp_payload = __cp_payload(sbi);
if (cp_pack_start_sum < cp_payload + 1 ||
cp_pack_start_sum > blocks_per_seg - 1 -
-- 
2.18.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH v3] dump.f2fs: only dump nat inside the specified nid range

2018-07-02 Thread Junling Zheng
Only dump nat info of nids inside the specified range.

Signed-off-by: Junling Zheng 
---
v2 -> v3:
 1) add a parameter to current_nat_addr() to get the correct nat pack
 2) use current_nat_addr() to get block_addr and nat pack in nat_dump
v1 -> v2:
 1) change {start,end}_nat type into nid_t
 2) put ASSERT() sentence close up to malloc
 3) use F2FS_NODE_INO and F2FS_META_INO instead of ino 1 and 2
 4) update man page of dump.f2fs

 fsck/dump.c | 94 +++--
 fsck/fsck.h |  7 ++--
 fsck/main.c |  4 +--
 fsck/mount.c| 19 ++
 man/dump.f2fs.8 | 13 +--
 5 files changed, 65 insertions(+), 72 deletions(-)

diff --git a/fsck/dump.c b/fsck/dump.c
index 9236a43..8cf431c 100644
--- a/fsck/dump.c
+++ b/fsck/dump.c
@@ -31,78 +31,38 @@ const char *seg_type_name[SEG_TYPE_MAX + 1] = {
"SEG_TYPE_NONE",
 };
 
-void nat_dump(struct f2fs_sb_info *sbi)
+void nat_dump(struct f2fs_sb_info *sbi, nid_t start_nat, nid_t end_nat)
 {
-   struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
-   struct f2fs_nm_info *nm_i = NM_I(sbi);
struct f2fs_nat_block *nat_block;
struct f2fs_node *node_block;
-   u32 nr_nat_blks, nid;
-   pgoff_t block_off;
+   nid_t nid;
pgoff_t block_addr;
char buf[BUF_SZ];
-   int seg_off;
int fd, ret, pack;
-   unsigned int i;
 
nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
-   node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
ASSERT(nat_block);
-
-   nr_nat_blks = get_sb(segment_count_nat) <<
-   (sbi->log_blocks_per_seg - 1);
+   node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
+   ASSERT(node_block);
 
fd = open("dump_nat", O_CREAT|O_WRONLY|O_TRUNC, 0666);
ASSERT(fd >= 0);
 
-   for (block_off = 0; block_off < nr_nat_blks; pack = 1, block_off++) {
-
-   seg_off = block_off >> sbi->log_blocks_per_seg;
-   block_addr = (pgoff_t)(nm_i->nat_blkaddr +
-   (seg_off << sbi->log_blocks_per_seg << 1) +
-   (block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
-
-   if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) {
-   block_addr += sbi->blocks_per_seg;
-   pack = 2;
-   }
-
-   ret = dev_read_block(nat_block, block_addr);
-   ASSERT(ret >= 0);
+   for (nid = start_nat; nid < end_nat; nid++) {
+   struct f2fs_nat_entry raw_nat;
+   struct node_info ni;
+   if(nid == 0 || nid == F2FS_NODE_INO(sbi) ||
+   nid == F2FS_META_INO(sbi))
+   continue;
 
-   nid = block_off * NAT_ENTRY_PER_BLOCK;
-   for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
-   struct f2fs_nat_entry raw_nat;
-   struct node_info ni;
-   ni.nid = nid + i;
+   ni.nid = nid;
+   block_addr = current_nat_addr(sbi, nid, );
 
-   if(nid + i  == 0 || nid + i  == 1 || nid + i == 2 )
-   continue;
-   if (lookup_nat_in_journal(sbi, nid + i,
-   _nat) >= 0) {
-   node_info_from_raw_nat(, _nat);
-   ret = dev_read_block(node_block, ni.blk_addr);
-   ASSERT(ret >= 0);
-   if (ni.blk_addr != 0x0) {
-   memset(buf, 0, BUF_SZ);
-   snprintf(buf, BUF_SZ,
-   "nid:%5u\tino:%5u\toffset:%5u"
-   "\tblkaddr:%10u\tpack:%d\n",
-   ni.nid, ni.ino,
-   
le32_to_cpu(node_block->footer.flag) >>
-   OFFSET_BIT_SHIFT,
-   ni.blk_addr, pack);
-   ret = write(fd, buf, strlen(buf));
-   ASSERT(ret >= 0);
-   }
-   } else {
-   node_info_from_raw_nat(,
-   _block->entries[i]);
-   if (ni.blk_addr == 0)
-   continue;
-
-   ret = dev_read_block(node_block, ni.blk_addr);
-   ASSERT(ret >= 0);
+   if (lookup_nat_in_journal(sbi, nid, _nat) >= 0) {
+  

[f2fs-dev] [PATCH v2] dump.f2fs: only dump nat inside the specified nid range

2018-07-01 Thread Junling Zheng
Only dump nat info of nids inside the specified range.

Signed-off-by: Junling Zheng 
---
v1 -> v2:
 1) change {start,end}_nat type into nid_t
 2) put ASSERT() sentence close up to malloc
 3) use F2FS_NODE_INO and F2FS_META_INO instead of ino 1 and 2
 4) update man page of dump.f2fs

 fsck/dump.c | 82 ++---
 fsck/fsck.h |  6 ++--
 fsck/main.c |  4 +--
 man/dump.f2fs.8 | 13 ++--
 4 files changed, 52 insertions(+), 53 deletions(-)

diff --git a/fsck/dump.c b/fsck/dump.c
index 9236a43..942e874 100644
--- a/fsck/dump.c
+++ b/fsck/dump.c
@@ -31,32 +31,35 @@ const char *seg_type_name[SEG_TYPE_MAX + 1] = {
"SEG_TYPE_NONE",
 };
 
-void nat_dump(struct f2fs_sb_info *sbi)
+void nat_dump(struct f2fs_sb_info *sbi, nid_t start_nat, nid_t end_nat)
 {
-   struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
struct f2fs_nm_info *nm_i = NM_I(sbi);
struct f2fs_nat_block *nat_block;
struct f2fs_node *node_block;
-   u32 nr_nat_blks, nid;
+   nid_t nid;
pgoff_t block_off;
pgoff_t block_addr;
char buf[BUF_SZ];
int seg_off;
int fd, ret, pack;
-   unsigned int i;
 
nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
-   node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
ASSERT(nat_block);
-
-   nr_nat_blks = get_sb(segment_count_nat) <<
-   (sbi->log_blocks_per_seg - 1);
+   node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
+   ASSERT(node_block);
 
fd = open("dump_nat", O_CREAT|O_WRONLY|O_TRUNC, 0666);
ASSERT(fd >= 0);
 
-   for (block_off = 0; block_off < nr_nat_blks; pack = 1, block_off++) {
+   for (nid = start_nat; nid < end_nat; pack = 1, nid++) {
+   struct f2fs_nat_entry raw_nat;
+   struct node_info ni;
+   if(nid == 0 || nid == F2FS_NODE_INO(sbi) ||
+   nid == F2FS_META_INO(sbi))
+   continue;
 
+   ni.nid = nid;
+   block_off = nid / NAT_ENTRY_PER_BLOCK;
seg_off = block_off >> sbi->log_blocks_per_seg;
block_addr = (pgoff_t)(nm_i->nat_blkaddr +
(seg_off << sbi->log_blocks_per_seg << 1) +
@@ -67,42 +70,11 @@ void nat_dump(struct f2fs_sb_info *sbi)
pack = 2;
}
 
-   ret = dev_read_block(nat_block, block_addr);
-   ASSERT(ret >= 0);
-
-   nid = block_off * NAT_ENTRY_PER_BLOCK;
-   for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
-   struct f2fs_nat_entry raw_nat;
-   struct node_info ni;
-   ni.nid = nid + i;
-
-   if(nid + i  == 0 || nid + i  == 1 || nid + i == 2 )
-   continue;
-   if (lookup_nat_in_journal(sbi, nid + i,
-   _nat) >= 0) {
-   node_info_from_raw_nat(, _nat);
-   ret = dev_read_block(node_block, ni.blk_addr);
-   ASSERT(ret >= 0);
-   if (ni.blk_addr != 0x0) {
-   memset(buf, 0, BUF_SZ);
-   snprintf(buf, BUF_SZ,
-   "nid:%5u\tino:%5u\toffset:%5u"
-   "\tblkaddr:%10u\tpack:%d\n",
-   ni.nid, ni.ino,
-   
le32_to_cpu(node_block->footer.flag) >>
-   OFFSET_BIT_SHIFT,
-   ni.blk_addr, pack);
-   ret = write(fd, buf, strlen(buf));
-   ASSERT(ret >= 0);
-   }
-   } else {
-   node_info_from_raw_nat(,
-   _block->entries[i]);
-   if (ni.blk_addr == 0)
-   continue;
-
-   ret = dev_read_block(node_block, ni.blk_addr);
-   ASSERT(ret >= 0);
+   if (lookup_nat_in_journal(sbi, nid, _nat) >= 0) {
+   node_info_from_raw_nat(, _nat);
+   ret = dev_read_block(node_block, ni.blk_addr);
+   ASSERT(ret >= 0);
+   if (ni.blk_addr != 0x0) {
memset(buf, 0, BUF_SZ);
snprintf(buf, BUF_SZ,
&q

Re: [f2fs-dev] [PATCH] dump.f2fs: only dump nat inside the specified nid range

2018-07-01 Thread Junling Zheng
On 2018/7/2 10:43, Chao Yu wrote:
> Hi Junling,
> 
> On 2018/7/2 10:09, Junling Zheng wrote:
>> Hi, Chao
>>
>> On 2018/7/1 10:22, Chao Yu wrote:
>>> Hi Junling,
>>>
>>> On 2018/6/29 18:11, Junling Zheng wrote:
>>>> Only dump nat info of nids inside the specified range.
>>>>
>>>> Signed-off-by: Junling Zheng 
>>>> ---
>>>>  fsck/dump.c | 79 -
>>>>  fsck/fsck.h |  2 +-
>>>>  fsck/main.c |  4 +--
>>>>  3 files changed, 38 insertions(+), 47 deletions(-)
>>>>
>>>> diff --git a/fsck/dump.c b/fsck/dump.c
>>>> index 9236a43..89cff83 100644
>>>> --- a/fsck/dump.c
>>>> +++ b/fsck/dump.c
>>>> @@ -31,32 +31,34 @@ const char *seg_type_name[SEG_TYPE_MAX + 1] = {
>>>>"SEG_TYPE_NONE",
>>>>  };
>>>>  
>>>> -void nat_dump(struct f2fs_sb_info *sbi)
>>>> +void nat_dump(struct f2fs_sb_info *sbi, int start_nat, int end_nat)
>>>>  {
>>>> -  struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
>>>>struct f2fs_nm_info *nm_i = NM_I(sbi);
>>>>struct f2fs_nat_block *nat_block;
>>>>struct f2fs_node *node_block;
>>>> -  u32 nr_nat_blks, nid;
>>>> +  u32 nid;
>>>>pgoff_t block_off;
>>>>pgoff_t block_addr;
>>>>char buf[BUF_SZ];
>>>>int seg_off;
>>>>int fd, ret, pack;
>>>> -  unsigned int i;
>>>>  
>>>>nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
>>>
>>> move ASSERT(nat_block) here.
>>>
>>
>> Yeah, right.
>>
>>>>node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
>>>>ASSERT(nat_block);
>>>> -
>>>> -  nr_nat_blks = get_sb(segment_count_nat) <<
>>>> -  (sbi->log_blocks_per_seg - 1);
>>>> +  ASSERT(node_block);
>>>>  
>>>>fd = open("dump_nat", O_CREAT|O_WRONLY|O_TRUNC, 0666);
>>>>ASSERT(fd >= 0);
>>>>  
>>>> -  for (block_off = 0; block_off < nr_nat_blks; pack = 1, block_off++) {
>>>> +  for (nid = start_nat; nid < end_nat; pack = 1, nid++) {
>>>> +  struct f2fs_nat_entry raw_nat;
>>>> +  struct node_info ni;
>>>> +  if(nid == 0 || nid == 1 || nid == 2 )
>>>
>>> minor cleanup
>>>
>>> if (nid == 0 || nid == F2FS_NODE_INO(sbi) || nid == F2FS_META_INO(sbi))
>>>
>>
>> OK.
>>
>>>> +  continue;
>>>>  
>>>> +  ni.nid = nid;
>>>> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
>>>>seg_off = block_off >> sbi->log_blocks_per_seg;
>>>>block_addr = (pgoff_t)(nm_i->nat_blkaddr +
>>>>(seg_off << sbi->log_blocks_per_seg << 1) +
>>>> @@ -67,42 +69,11 @@ void nat_dump(struct f2fs_sb_info *sbi)
>>>>pack = 2;
>>>>}
>>>>  
>>>> -  ret = dev_read_block(nat_block, block_addr);
>>>> -  ASSERT(ret >= 0);
>>>> -
>>>> -  nid = block_off * NAT_ENTRY_PER_BLOCK;
>>>> -  for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
>>>> -  struct f2fs_nat_entry raw_nat;
>>>> -  struct node_info ni;
>>>> -  ni.nid = nid + i;
>>>> -
>>>> -  if(nid + i  == 0 || nid + i  == 1 || nid + i == 2 )
>>>> -  continue;
>>>> -  if (lookup_nat_in_journal(sbi, nid + i,
>>>> -  _nat) >= 0) {
>>>> -  node_info_from_raw_nat(, _nat);
>>>> -  ret = dev_read_block(node_block, ni.blk_addr);
>>>> -  ASSERT(ret >= 0);
>>>> -  if (ni.blk_addr != 0x0) {
>>>> -  memset(buf, 0, BUF_SZ);
>>>> -  snprintf(buf, BUF_SZ,
>>>> -  "nid:%5u\tino:%5u\toffset:%5u"
>>>> -  "\tblkaddr:%10u\tpack:%d\n",
>>>> -  

Re: [f2fs-dev] [PATCH] dump.f2fs: only dump nat inside the specified nid range

2018-07-01 Thread Junling Zheng
Hi, Chao

On 2018/7/1 10:22, Chao Yu wrote:
> Hi Junling,
> 
> On 2018/6/29 18:11, Junling Zheng wrote:
>> Only dump nat info of nids inside the specified range.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>>  fsck/dump.c | 79 -
>>  fsck/fsck.h |  2 +-
>>  fsck/main.c |  4 +--
>>  3 files changed, 38 insertions(+), 47 deletions(-)
>>
>> diff --git a/fsck/dump.c b/fsck/dump.c
>> index 9236a43..89cff83 100644
>> --- a/fsck/dump.c
>> +++ b/fsck/dump.c
>> @@ -31,32 +31,34 @@ const char *seg_type_name[SEG_TYPE_MAX + 1] = {
>>  "SEG_TYPE_NONE",
>>  };
>>  
>> -void nat_dump(struct f2fs_sb_info *sbi)
>> +void nat_dump(struct f2fs_sb_info *sbi, int start_nat, int end_nat)
>>  {
>> -struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
>>  struct f2fs_nm_info *nm_i = NM_I(sbi);
>>  struct f2fs_nat_block *nat_block;
>>  struct f2fs_node *node_block;
>> -u32 nr_nat_blks, nid;
>> +u32 nid;
>>  pgoff_t block_off;
>>  pgoff_t block_addr;
>>  char buf[BUF_SZ];
>>  int seg_off;
>>  int fd, ret, pack;
>> -unsigned int i;
>>  
>>  nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
> 
> move ASSERT(nat_block) here.
> 

Yeah, right.

>>  node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
>>  ASSERT(nat_block);
>> -
>> -nr_nat_blks = get_sb(segment_count_nat) <<
>> -(sbi->log_blocks_per_seg - 1);
>> +ASSERT(node_block);
>>  
>>  fd = open("dump_nat", O_CREAT|O_WRONLY|O_TRUNC, 0666);
>>  ASSERT(fd >= 0);
>>  
>> -for (block_off = 0; block_off < nr_nat_blks; pack = 1, block_off++) {
>> +for (nid = start_nat; nid < end_nat; pack = 1, nid++) {
>> +struct f2fs_nat_entry raw_nat;
>> +struct node_info ni;
>> +if(nid == 0 || nid == 1 || nid == 2 )
> 
> minor cleanup
> 
> if (nid == 0 || nid == F2FS_NODE_INO(sbi) || nid == F2FS_META_INO(sbi))
> 

OK.

>> +continue;
>>  
>> +ni.nid = nid;
>> +block_off = nid / NAT_ENTRY_PER_BLOCK;
>>  seg_off = block_off >> sbi->log_blocks_per_seg;
>>  block_addr = (pgoff_t)(nm_i->nat_blkaddr +
>>  (seg_off << sbi->log_blocks_per_seg << 1) +
>> @@ -67,42 +69,11 @@ void nat_dump(struct f2fs_sb_info *sbi)
>>  pack = 2;
>>  }
>>  
>> -ret = dev_read_block(nat_block, block_addr);
>> -ASSERT(ret >= 0);
>> -
>> -nid = block_off * NAT_ENTRY_PER_BLOCK;
>> -for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
>> -struct f2fs_nat_entry raw_nat;
>> -struct node_info ni;
>> -ni.nid = nid + i;
>> -
>> -if(nid + i  == 0 || nid + i  == 1 || nid + i == 2 )
>> -continue;
>> -if (lookup_nat_in_journal(sbi, nid + i,
>> -_nat) >= 0) {
>> -node_info_from_raw_nat(, _nat);
>> -ret = dev_read_block(node_block, ni.blk_addr);
>> -ASSERT(ret >= 0);
>> -if (ni.blk_addr != 0x0) {
>> -memset(buf, 0, BUF_SZ);
>> -snprintf(buf, BUF_SZ,
>> -"nid:%5u\tino:%5u\toffset:%5u"
>> -"\tblkaddr:%10u\tpack:%d\n",
>> -ni.nid, ni.ino,
>> -
>> le32_to_cpu(node_block->footer.flag) >>
>> -OFFSET_BIT_SHIFT,
>> -ni.blk_addr, pack);
>> -ret = write(fd, buf, strlen(buf));
>> -ASSERT(ret >= 0);
>> -}
>> -} else {
>> -node_info_from_raw_nat(,
>> -_block->entries[i]);
>> -if (ni.blk_addr == 0)
>> -continue;
>> -
>&g

[f2fs-dev] [PATCH] dump.f2fs: only dump nat inside the specified nid range

2018-06-29 Thread Junling Zheng
Only dump nat info of nids inside the specified range.

Signed-off-by: Junling Zheng 
---
 fsck/dump.c | 79 -
 fsck/fsck.h |  2 +-
 fsck/main.c |  4 +--
 3 files changed, 38 insertions(+), 47 deletions(-)

diff --git a/fsck/dump.c b/fsck/dump.c
index 9236a43..89cff83 100644
--- a/fsck/dump.c
+++ b/fsck/dump.c
@@ -31,32 +31,34 @@ const char *seg_type_name[SEG_TYPE_MAX + 1] = {
"SEG_TYPE_NONE",
 };
 
-void nat_dump(struct f2fs_sb_info *sbi)
+void nat_dump(struct f2fs_sb_info *sbi, int start_nat, int end_nat)
 {
-   struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
struct f2fs_nm_info *nm_i = NM_I(sbi);
struct f2fs_nat_block *nat_block;
struct f2fs_node *node_block;
-   u32 nr_nat_blks, nid;
+   u32 nid;
pgoff_t block_off;
pgoff_t block_addr;
char buf[BUF_SZ];
int seg_off;
int fd, ret, pack;
-   unsigned int i;
 
nat_block = (struct f2fs_nat_block *)calloc(BLOCK_SZ, 1);
node_block = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
ASSERT(nat_block);
-
-   nr_nat_blks = get_sb(segment_count_nat) <<
-   (sbi->log_blocks_per_seg - 1);
+   ASSERT(node_block);
 
fd = open("dump_nat", O_CREAT|O_WRONLY|O_TRUNC, 0666);
ASSERT(fd >= 0);
 
-   for (block_off = 0; block_off < nr_nat_blks; pack = 1, block_off++) {
+   for (nid = start_nat; nid < end_nat; pack = 1, nid++) {
+   struct f2fs_nat_entry raw_nat;
+   struct node_info ni;
+   if(nid == 0 || nid == 1 || nid == 2 )
+   continue;
 
+   ni.nid = nid;
+   block_off = nid / NAT_ENTRY_PER_BLOCK;
seg_off = block_off >> sbi->log_blocks_per_seg;
block_addr = (pgoff_t)(nm_i->nat_blkaddr +
(seg_off << sbi->log_blocks_per_seg << 1) +
@@ -67,42 +69,11 @@ void nat_dump(struct f2fs_sb_info *sbi)
pack = 2;
}
 
-   ret = dev_read_block(nat_block, block_addr);
-   ASSERT(ret >= 0);
-
-   nid = block_off * NAT_ENTRY_PER_BLOCK;
-   for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
-   struct f2fs_nat_entry raw_nat;
-   struct node_info ni;
-   ni.nid = nid + i;
-
-   if(nid + i  == 0 || nid + i  == 1 || nid + i == 2 )
-   continue;
-   if (lookup_nat_in_journal(sbi, nid + i,
-   _nat) >= 0) {
-   node_info_from_raw_nat(, _nat);
-   ret = dev_read_block(node_block, ni.blk_addr);
-   ASSERT(ret >= 0);
-   if (ni.blk_addr != 0x0) {
-   memset(buf, 0, BUF_SZ);
-   snprintf(buf, BUF_SZ,
-   "nid:%5u\tino:%5u\toffset:%5u"
-   "\tblkaddr:%10u\tpack:%d\n",
-   ni.nid, ni.ino,
-   
le32_to_cpu(node_block->footer.flag) >>
-   OFFSET_BIT_SHIFT,
-   ni.blk_addr, pack);
-   ret = write(fd, buf, strlen(buf));
-   ASSERT(ret >= 0);
-   }
-   } else {
-   node_info_from_raw_nat(,
-   _block->entries[i]);
-   if (ni.blk_addr == 0)
-   continue;
-
-   ret = dev_read_block(node_block, ni.blk_addr);
-   ASSERT(ret >= 0);
+   if (lookup_nat_in_journal(sbi, nid, _nat) >= 0) {
+   node_info_from_raw_nat(, _nat);
+   ret = dev_read_block(node_block, ni.blk_addr);
+   ASSERT(ret >= 0);
+   if (ni.blk_addr != 0x0) {
memset(buf, 0, BUF_SZ);
snprintf(buf, BUF_SZ,
"nid:%5u\tino:%5u\toffset:%5u"
@@ -114,6 +85,26 @@ void nat_dump(struct f2fs_sb_info *sbi)
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
}
+   } else {
+   ret = dev_read_block(nat_block, block_addr);
+   ASSERT(ret >

[f2fs-dev] [PATCH] f2fs-tools: fix compile errors on AOSP

2018-06-20 Thread Junling Zheng
Include needed header files directly to fix compile errors on AOSP.

Signed-off-by: Junling Zheng 
---
 include/f2fs_fs.h | 10 ++
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 49ecee9..0a1ed84 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -13,6 +13,8 @@
 #define __F2FS_FS_H__
 
 #include 
+#include 
+#include 
 #ifdef HAVE_CONFIG_H
 #include 
 #endif
@@ -40,14 +42,6 @@
 #include 
 #endif
 
-#ifdef HAVE_STDLIB_H
-#include 
-#endif
-
-#ifdef HAVE_STRING_H
-#include 
-#endif
-
 #ifdef HAVE_LIBSELINUX
 #include 
 #include 
-- 
2.17.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] fsck.f2fs: do not print content beyond sb->version

2018-06-07 Thread Junling Zheng
On 2018/6/7 18:53, Chao Yu wrote:
> On 2018/6/6 22:39, Junling Zheng wrote:
>> Currently, versions in f2fs_configuration have one more byte than
>> those in sb, so versions in sb may not end with '\0', and then
>> print_raw_sb_info() will print something beyond sb->version.
>>
>> Signed-off-by: Junling Zheng 
>> ---
>>  fsck/mount.c | 5 -
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 0a30adb..9def919 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -341,7 +341,10 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>>  DISP_u32(sb, node_ino);
>>  DISP_u32(sb, meta_ino);
>>  DISP_u32(sb, cp_payload);
>> -DISP("%s", sb, version);
>> +if (sb->version[255] != '\0')
>> +DISP("%-.256s", sb, version);
>> +else
>> +DISP("%s", sb, version);
> 
> IMO, DISP("%-.256s", sb, version) is enough.
> 

Yeah, it works :)

Thanks,

> Thanks,
> 
>>  printf("\n");
>>  }
>>  
>>
> 
> 
> .
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH v2] fsck.f2fs: do not print content beyond sb->version

2018-06-07 Thread Junling Zheng
Currently, versions in f2fs_configuration have one more byte than
those in sb, so versions in sb may not end with '\0', and then
print_raw_sb_info() will print something beyond sb->version.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 0a30adb..66b4a5d 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -341,7 +341,7 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
-   DISP("%s", sb, version);
+   DISP("%-.256s", sb, version);
printf("\n");
 }
 
-- 
2.17.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] fsck.f2fs: do not print content beyond sb->version

2018-06-06 Thread Junling Zheng
Actually, I prefer to fix this bug by unifying the length of versions in
f2fs_configuration and f2fs_super_block :)

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

Thanks

On 2018/6/6 22:39, Junling Zheng wrote:
> Currently, versions in f2fs_configuration have one more byte than
> those in sb, so versions in sb may not end with '\0', and then
> print_raw_sb_info() will print something beyond sb->version.
> 
> Signed-off-by: Junling Zheng 
> ---
>  fsck/mount.c | 5 -
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/fsck/mount.c b/fsck/mount.c
> index 0a30adb..9def919 100644
> --- a/fsck/mount.c
> +++ b/fsck/mount.c
> @@ -341,7 +341,10 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
>   DISP_u32(sb, node_ino);
>   DISP_u32(sb, meta_ino);
>   DISP_u32(sb, cp_payload);
> - DISP("%s", sb, version);
> + if (sb->version[255] != '\0')
> + DISP("%-.256s", sb, version);
> + else
> + DISP("%s", sb, version);
>   printf("\n");
>  }
>  
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] fsck.f2fs: do not print content beyond sb->version

2018-06-06 Thread Junling Zheng
Currently, versions in f2fs_configuration have one more byte than
those in sb, so versions in sb may not end with '\0', and then
print_raw_sb_info() will print something beyond sb->version.

Signed-off-by: Junling Zheng 
---
 fsck/mount.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 0a30adb..9def919 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -341,7 +341,10 @@ void print_raw_sb_info(struct f2fs_super_block *sb)
DISP_u32(sb, node_ino);
DISP_u32(sb, meta_ino);
DISP_u32(sb, cp_payload);
-   DISP("%s", sb, version);
+   if (sb->version[255] != '\0')
+   DISP("%-.256s", sb, version);
+   else
+   DISP("%s", sb, version);
printf("\n");
 }
 
-- 
2.17.0


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] resize.f2fs: skip cursegs when finding next free block

2018-06-04 Thread Junling Zheng
On 2018/6/5 4:55, Jaegeuk Kim wrote:
> On 06/04, Sheng Yong wrote:
>> resize.f2fs (f2fs_defragment) tries to migrate blocks to new positions.
>> However, if a curseg is selected, and f2fs_defragment is broken by any
>> error, curseg->next_blkoff is left not updated.
>>
>> To avoid this, we skip cursegs when finding next free block.
> 
> Don't we update the curseg at the end of resize/defrag?
> 

Yeah, currently, at the beginning of resize, we finds the next free block
from the beginning of main instead of from cursegs. So, we needn't to update
the curseg if we skip cursegs while finding next free block.

Besides, the logic of IS_CUR_SEGNO is puzzling, does it mean to check the
type of curseg? If the type of curseg with specified segno is not that we
need, then returns nonzero?

>>
>> Signed-off-by: Sheng Yong 
>> ---
>>  fsck/f2fs.h  | 5 +
>>  fsck/fsck.c  | 2 +-
>>  fsck/mount.c | 8 +++-
>>  3 files changed, 5 insertions(+), 10 deletions(-)
>>
>> diff --git a/fsck/f2fs.h b/fsck/f2fs.h
>> index d0e08aa..d216444 100644
>> --- a/fsck/f2fs.h
>> +++ b/fsck/f2fs.h
>> @@ -380,16 +380,13 @@ static inline bool IS_VALID_BLK_ADDR(struct 
>> f2fs_sb_info *sbi, u32 addr)
>>  return 1;
>>  }
>>  
>> -static inline int IS_CUR_SEGNO(struct f2fs_sb_info *sbi, u32 segno, int 
>> type)
>> +static inline int IS_CUR_SEGNO(struct f2fs_sb_info *sbi, u32 segno)
>>  {
>>  int i;
>>  
>>  for (i = 0; i < NO_CHECK_TYPE; i++) {
>>  struct curseg_info *curseg = CURSEG_I(sbi, i);
>>  
>> -if (type == i)
>> -continue;
>> -
>>  if (segno == curseg->segno)
>>  return 1;
>>  }
>> diff --git a/fsck/fsck.c b/fsck/fsck.c
>> index 5b6dbc8..8145199 100644
>> --- a/fsck/fsck.c
>> +++ b/fsck/fsck.c
>> @@ -1741,7 +1741,7 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)
>>  se = get_seg_entry(sbi, i);
>>  if (se->valid_blocks != 0)
>>  sit_valid_segs++;
>> -else if (IS_CUR_SEGNO(sbi, i, NO_CHECK_TYPE)) {
>> +else if (IS_CUR_SEGNO(sbi, i)) {
>>  /* curseg has not been written back to device */
>>  MSG(1, "\tInfo: curseg %u is counted in valid segs\n", 
>> i);
>>  sit_valid_segs++;
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 0a30adb..8d4704e 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -35,8 +35,7 @@ u32 get_free_segments(struct f2fs_sb_info *sbi)
>>  for (i = 0; i < TOTAL_SEGS(sbi); i++) {
>>  struct seg_entry *se = get_seg_entry(sbi, i);
>>  
>> -if (se->valid_blocks == 0x0 &&
>> -!IS_CUR_SEGNO(sbi, i, NO_CHECK_TYPE))
>> +if (se->valid_blocks == 0x0 && !IS_CUR_SEGNO(sbi, i))
>>  free_segs++;
>>  }
>>  return free_segs;
>> @@ -1891,8 +1890,7 @@ void flush_sit_entries(struct f2fs_sb_info *sbi)
>>  se->valid_blocks);
>>  rewrite_current_sit_page(sbi, segno, sit_blk);
>>  
>> -if (se->valid_blocks == 0x0 &&
>> -!IS_CUR_SEGNO(sbi, segno, NO_CHECK_TYPE))
>> +if (se->valid_blocks == 0x0 && !IS_CUR_SEGNO(sbi, segno))
>>  free_segs++;
>>  }
>>  
>> @@ -1922,7 +1920,7 @@ int find_next_free_block(struct f2fs_sb_info *sbi, u64 
>> *to, int left, int type)
>>  se = get_seg_entry(sbi, segno);
>>  
>>  if (se->valid_blocks == sbi->blocks_per_seg ||
>> -IS_CUR_SEGNO(sbi, segno, type)) {
>> +IS_CUR_SEGNO(sbi, segno)) {
>>  *to = left ? START_BLOCK(sbi, segno) - 1:
>>  START_BLOCK(sbi, segno + 1);
>>  continue;
>> -- 
>> 2.17.0
> 
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> ___
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
> 
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] fsck.f2fs: do umount to free memory in out_err

2018-05-17 Thread Junling Zheng
Sorry, this patch has a bug:

If f2fs_do_mount fails in validate_super_block or get_valid_checkpoint,
then we should only free sbi->ckpt and sbi->raw_super :(

On 2018/5/17 15:57, Junling Zheng wrote:
> Call f2fs_do_umount to free memory in out_err branch.
> 
> Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
> ---
>  fsck/main.c | 5 +
>  1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/fsck/main.c b/fsck/main.c
> index c4dd8b1..870f2d8 100644
> --- a/fsck/main.c
> +++ b/fsck/main.c
> @@ -844,9 +844,6 @@ retry:
>   return 0;
>  
>  out_err:
> - if (sbi->ckpt)
> - free(sbi->ckpt);
> - if (sbi->raw_super)
> - free(sbi->raw_super);
> + f2fs_do_umount(sbi);
>   return ret;
>  }
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] fsck.f2fs: do umount to free memory in out_err

2018-05-17 Thread Junling Zheng
Call f2fs_do_umount to free memory in out_err branch.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/main.c | 5 +
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/fsck/main.c b/fsck/main.c
index c4dd8b1..870f2d8 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -844,9 +844,6 @@ retry:
return 0;
 
 out_err:
-   if (sbi->ckpt)
-   free(sbi->ckpt);
-   if (sbi->raw_super)
-   free(sbi->raw_super);
+   f2fs_do_umount(sbi);
return ret;
 }
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [RFC PATCH] f2fs-tools: fix blocks allocation direction inside segment

2018-05-08 Thread Junling Zheng
This fixes the block allocation direction from left to right inside
one segment despite of the direction of segment allocation.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/mount.c | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 1396a2e..38d2aa2 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -1921,13 +1921,13 @@ int find_next_free_block(struct f2fs_sb_info *sbi, u64 
*to, int left, int type)
 
if (se->valid_blocks == sbi->blocks_per_seg ||
IS_CUR_SEGNO(sbi, segno, type)) {
-   *to = left ? START_BLOCK(sbi, segno) - 1:
+   *to = left ? START_BLOCK(sbi, segno - 1) :
START_BLOCK(sbi, segno + 1);
continue;
}
 
if (se->valid_blocks == 0 && not_enough) {
-   *to = left ? START_BLOCK(sbi, segno) - 1:
+   *to = left ? START_BLOCK(sbi, segno - 1) :
START_BLOCK(sbi, segno + 1);
continue;
}
@@ -1949,7 +1949,10 @@ int find_next_free_block(struct f2fs_sb_info *sbi, u64 
*to, int left, int type)
!f2fs_test_bit(offset, (const char *)se->cur_valid_map))
return 0;
 
-   *to = left ? *to - 1: *to + 1;
+   if (!(OFFSET_IN_SEG(sbi, *to + 1)) && left)
+   *to = START_BLOCK(sbi, segno - 1);
+   else
+   *to = *to + 1;
}
return -1;
 }
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] sload.f2fs: give correct file type

2018-05-07 Thread Junling Zheng
Hi, Jaegeuk

My previous patch (sload.f2fs: fix the missing of bit mask for file type) had
already fix this bug :)

Thanks,
Junling

On 2018/5/8 2:58, Jaegeuk Kim wrote:
> From: Lianjun Huang 
> 
> This fixes permission error due to wrong file type.
> 
> Signed-off-by: Lianjun Huang 
> Signed-off-by: Jaegeuk Kim 
> ---
>  fsck/sload.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fsck/sload.c b/fsck/sload.c
> index 2842f2c..2fb13f2 100644
> --- a/fsck/sload.c
> +++ b/fsck/sload.c
> @@ -106,7 +106,7 @@ static int set_perms_and_caps(struct dentry *de)
>  
>   /* Permissions */
>   if (fs_config_func != NULL) {
> - fs_config_func(mnt_path, S_ISDIR(de->mode),
> + fs_config_func(mnt_path, de->file_type == F2FS_FT_DIR,
>   c.target_out_dir, , , ,
>   );
>   de->uid = uid & 0x;
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] sload.f2fs: fix the missing of bit mask for file type

2018-05-07 Thread Junling Zheng
Hi, Jaegeuk

Could you please merge this fix into external/f2fs-tools repo for AOSP?
Since we found this bug while using sload feature in Android P :)

Thanks,
Junling

On 2018/5/3 19:25, Junling Zheng wrote:
> Fix the missing of bit mask for the file type bit fields.
> 
> Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
> Signed-off-by: Sheng Yong <shengyo...@huawei.com>
> ---
>  fsck/sload.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fsck/sload.c b/fsck/sload.c
> index 2842f2c..1b7a2d1 100644
> --- a/fsck/sload.c
> +++ b/fsck/sload.c
> @@ -157,7 +157,7 @@ static void set_inode_metadata(struct dentry *de)
>  
>   de->size = stat.st_size;
>   de->mode = stat.st_mode &
> - (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
> + 
> (S_IFMT|S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
>   if (c.fixed_time == -1 && c.from_dir)
>   de->mtime = stat.st_mtime;
>   else
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] sload.f2fs: fix the missing of bit mask for file type

2018-05-03 Thread Junling Zheng
Fix the missing of bit mask for the file type bit fields.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
Signed-off-by: Sheng Yong <shengyo...@huawei.com>
---
 fsck/sload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fsck/sload.c b/fsck/sload.c
index 2842f2c..1b7a2d1 100644
--- a/fsck/sload.c
+++ b/fsck/sload.c
@@ -157,7 +157,7 @@ static void set_inode_metadata(struct dentry *de)
 
de->size = stat.st_size;
de->mode = stat.st_mode &
-   (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
+   
(S_IFMT|S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
if (c.fixed_time == -1 && c.from_dir)
de->mtime = stat.st_mtime;
else
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [RFC PATCH] f2fs-tools: introduce tune.f2fs

2018-04-25 Thread Junling Zheng
Ping...

On 2018/4/23 15:32, Junling Zheng wrote:
> Introduce tune.f2fs tool to change the f2fs parameters.
> Currently this tool only supports adding or removing encrypt
> feature bit in superblock.
> 
> Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
> ---
>  fsck/Makefile.am  |  3 ++-
>  fsck/fsck.h   |  5 +
>  fsck/main.c   | 60 ++
>  fsck/tune.c   | 46 +
>  include/f2fs_fs.h | 61 
> +++
>  man/Makefile.am   |  2 +-
>  man/tune.f2fs.8   | 50 +
>  7 files changed, 207 insertions(+), 20 deletions(-)
>  create mode 100644 fsck/tune.c
>  create mode 100644 man/tune.f2fs.8
> 
> diff --git a/fsck/Makefile.am b/fsck/Makefile.am
> index 1fc7310..3efacfd 100644
> --- a/fsck/Makefile.am
> +++ b/fsck/Makefile.am
> @@ -5,7 +5,7 @@ AM_CFLAGS = -Wall
>  sbin_PROGRAMS = fsck.f2fs
>  noinst_HEADERS = common.h dict.h dqblk_v2.h f2fs.h fsck.h node.h quotaio.h 
> quotaio_tree.h quotaio_v2.h xattr.h
>  include_HEADERS = $(top_srcdir)/include/quota.h
> -fsck_f2fs_SOURCES = main.c fsck.c dump.c mount.c defrag.c resize.c \
> +fsck_f2fs_SOURCES = main.c fsck.c dump.c mount.c defrag.c resize.c tune.c \
>   node.c segment.c dir.c sload.c xattr.c \
>   dict.c mkquota.c quotaio.c quotaio_tree.c quotaio_v2.c
>  fsck_f2fs_LDADD = ${libselinux_LIBS} ${libuuid_LIBS} 
> $(top_builddir)/lib/libf2fs.la
> @@ -15,3 +15,4 @@ install-data-hook:
>   ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/defrag.f2fs
>   ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/resize.f2fs
>   ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/sload.f2fs
> + ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/tune.f2fs
> diff --git a/fsck/fsck.h b/fsck/fsck.h
> index 3e13fc6..39d6ed4 100644
> --- a/fsck/fsck.h
> +++ b/fsck/fsck.h
> @@ -180,6 +180,8 @@ extern void write_superblock(struct f2fs_super_block *);
>  extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
>  extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
>  
> +extern int validate_super_block(struct f2fs_sb_info *, int);
> +extern void print_sb_state(struct f2fs_super_block *);
>  extern void print_raw_sb_info(struct f2fs_super_block *);
>  
>  extern u32 get_free_segments(struct f2fs_sb_info *);
> @@ -220,6 +222,9 @@ int f2fs_resize(struct f2fs_sb_info *);
>  /* sload.c */
>  int f2fs_sload(struct f2fs_sb_info *);
>  
> +/* tune.c */
> +int f2fs_tune(struct f2fs_sb_info *);
> +
>  /* segment.c */
>  void reserve_new_block(struct f2fs_sb_info *, block_t *,
>   struct f2fs_summary *, int);
> diff --git a/fsck/main.c b/fsck/main.c
> index c4dd8b1..b8e1aee 100644
> --- a/fsck/main.c
> +++ b/fsck/main.c
> @@ -29,6 +29,7 @@ extern struct sparse_file *f2fs_sparse_file;
>  #endif
>  
>  INIT_FEATURE_TABLE;
> +INIT_TUNE_FEATURE_TABLE;
>  
>  static char *absolute_path(const char *file)
>  {
> @@ -123,6 +124,15 @@ void sload_usage()
>   exit(1);
>  }
>  
> +void tune_usage()
> +{
> + MSG(0, "\nUsage: tune.f2fs [options] device\n");
> + MSG(0, "[options]:\n");
> + MSG(0, "  -d debug level [default:0]\n");
> + MSG(0, "  -O [^]feature[,...]\n");
> + exit(1);
> +}
> +
>  static int is_digits(char *optarg)
>  {
>   unsigned int i;
> @@ -145,6 +155,8 @@ static void error_out(char *prog)
>   resize_usage();
>   else if (!strcmp("sload.f2fs", prog))
>   sload_usage();
> + else if (!strcmp("tune.f2fs", prog))
> + tune_usage();
>   else
>   MSG(0, "\nWrong program.\n");
>  }
> @@ -528,6 +540,32 @@ void f2fs_parse_options(int argc, char *argv[])
>   if (err != NOERROR)
>   break;
>   }
> + } else if (!strcmp("tune.f2fs", prog)) {
> + const char *option_string = "d:O:";
> +
> + c.func = TUNE;
> + while ((option = getopt(argc, argv, option_string)) != EOF) {
> + switch (option) {
> + case 'd':
> + if (!is_digits(optarg)) {
> + err = EWRONG_OPT;
> + break;
> + }
> + c.dbg_lv = atoi(optarg);
> + MSG(0, "Info: Debug level = %d\n",
> +   

[f2fs-dev] [RFC PATCH] f2fs-tools: introduce tune.f2fs

2018-04-23 Thread Junling Zheng
Introduce tune.f2fs tool to change the f2fs parameters.
Currently this tool only supports adding or removing encrypt
feature bit in superblock.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/Makefile.am  |  3 ++-
 fsck/fsck.h   |  5 +
 fsck/main.c   | 60 ++
 fsck/tune.c   | 46 +
 include/f2fs_fs.h | 61 +++
 man/Makefile.am   |  2 +-
 man/tune.f2fs.8   | 50 +
 7 files changed, 207 insertions(+), 20 deletions(-)
 create mode 100644 fsck/tune.c
 create mode 100644 man/tune.f2fs.8

diff --git a/fsck/Makefile.am b/fsck/Makefile.am
index 1fc7310..3efacfd 100644
--- a/fsck/Makefile.am
+++ b/fsck/Makefile.am
@@ -5,7 +5,7 @@ AM_CFLAGS = -Wall
 sbin_PROGRAMS = fsck.f2fs
 noinst_HEADERS = common.h dict.h dqblk_v2.h f2fs.h fsck.h node.h quotaio.h 
quotaio_tree.h quotaio_v2.h xattr.h
 include_HEADERS = $(top_srcdir)/include/quota.h
-fsck_f2fs_SOURCES = main.c fsck.c dump.c mount.c defrag.c resize.c \
+fsck_f2fs_SOURCES = main.c fsck.c dump.c mount.c defrag.c resize.c tune.c \
node.c segment.c dir.c sload.c xattr.c \
dict.c mkquota.c quotaio.c quotaio_tree.c quotaio_v2.c
 fsck_f2fs_LDADD = ${libselinux_LIBS} ${libuuid_LIBS} 
$(top_builddir)/lib/libf2fs.la
@@ -15,3 +15,4 @@ install-data-hook:
ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/defrag.f2fs
ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/resize.f2fs
ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/sload.f2fs
+   ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/tune.f2fs
diff --git a/fsck/fsck.h b/fsck/fsck.h
index 3e13fc6..39d6ed4 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -180,6 +180,8 @@ extern void write_superblock(struct f2fs_super_block *);
 extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
 extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
 
+extern int validate_super_block(struct f2fs_sb_info *, int);
+extern void print_sb_state(struct f2fs_super_block *);
 extern void print_raw_sb_info(struct f2fs_super_block *);
 
 extern u32 get_free_segments(struct f2fs_sb_info *);
@@ -220,6 +222,9 @@ int f2fs_resize(struct f2fs_sb_info *);
 /* sload.c */
 int f2fs_sload(struct f2fs_sb_info *);
 
+/* tune.c */
+int f2fs_tune(struct f2fs_sb_info *);
+
 /* segment.c */
 void reserve_new_block(struct f2fs_sb_info *, block_t *,
struct f2fs_summary *, int);
diff --git a/fsck/main.c b/fsck/main.c
index c4dd8b1..b8e1aee 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -29,6 +29,7 @@ extern struct sparse_file *f2fs_sparse_file;
 #endif
 
 INIT_FEATURE_TABLE;
+INIT_TUNE_FEATURE_TABLE;
 
 static char *absolute_path(const char *file)
 {
@@ -123,6 +124,15 @@ void sload_usage()
exit(1);
 }
 
+void tune_usage()
+{
+   MSG(0, "\nUsage: tune.f2fs [options] device\n");
+   MSG(0, "[options]:\n");
+   MSG(0, "  -d debug level [default:0]\n");
+   MSG(0, "  -O [^]feature[,...]\n");
+   exit(1);
+}
+
 static int is_digits(char *optarg)
 {
unsigned int i;
@@ -145,6 +155,8 @@ static void error_out(char *prog)
resize_usage();
else if (!strcmp("sload.f2fs", prog))
sload_usage();
+   else if (!strcmp("tune.f2fs", prog))
+   tune_usage();
else
MSG(0, "\nWrong program.\n");
 }
@@ -528,6 +540,32 @@ void f2fs_parse_options(int argc, char *argv[])
if (err != NOERROR)
break;
}
+   } else if (!strcmp("tune.f2fs", prog)) {
+   const char *option_string = "d:O:";
+
+   c.func = TUNE;
+   while ((option = getopt(argc, argv, option_string)) != EOF) {
+   switch (option) {
+   case 'd':
+   if (!is_digits(optarg)) {
+   err = EWRONG_OPT;
+   break;
+   }
+   c.dbg_lv = atoi(optarg);
+   MSG(0, "Info: Debug level = %d\n",
+   c.dbg_lv);
+   break;
+   case 'O':
+   if (parse_feature(tune_feature_table, optarg))
+   tune_usage();
+   break;
+   default:
+   err = EUNKNOWN_OPT;
+   break;
+   }
+   if (err != NOERROR)
+   break;
+   }
}
 
add_default_options();
@@ -7

Re: [f2fs-dev] [PATCH] fsck.f2fs: add -O features to tune the bits

2018-04-19 Thread Junling Zheng
Hi, Jaegeuk

On 2018/4/20 4:54, Jaegeuk Kim wrote:
> This patch add -O features for fsck.f2fs in order to tune the feature bits.
> Currently, it supports -O encrypt only.
> 

Shall we introduce a new tool like tune.f2fs to tune the parameters of f2fs?
Maybe we will tune others parameters in the future, not only features bits in 
sb :)

> Signed-off-by: Jaegeuk Kim 
> ---
>  fsck/fsck.h   |  1 +
>  fsck/main.c   |  9 -
>  fsck/mount.c  | 39 ++-
>  fsck/resize.c | 18 +-
>  4 files changed, 48 insertions(+), 19 deletions(-)
> 
> diff --git a/fsck/fsck.h b/fsck/fsck.h
> index 8e133fa..3e13fc6 100644
> --- a/fsck/fsck.h
> +++ b/fsck/fsck.h
> @@ -176,6 +176,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64);
>  extern void write_curseg_info(struct f2fs_sb_info *);
>  extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
>  extern void write_checkpoint(struct f2fs_sb_info *);
> +extern void write_superblock(struct f2fs_super_block *);
>  extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
>  extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
>  
> diff --git a/fsck/main.c b/fsck/main.c
> index 9256d21..c4dd8b1 100644
> --- a/fsck/main.c
> +++ b/fsck/main.c
> @@ -28,6 +28,8 @@ struct f2fs_fsck gfsck;
>  extern struct sparse_file *f2fs_sparse_file;
>  #endif
>  
> +INIT_FEATURE_TABLE;
> +
>  static char *absolute_path(const char *file)
>  {
>   char *ret;
> @@ -54,6 +56,7 @@ void fsck_usage()
>   MSG(0, "  -d debug level [default:0]\n");
>   MSG(0, "  -f check/fix entire partition\n");
>   MSG(0, "  -g add default options\n");
> + MSG(0, "  -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
>   MSG(0, "  -p preen mode [default:0 the same as -a [0|1]]\n");
>   MSG(0, "  -S sparse_mode\n");
>   MSG(0, "  -t show directory tree\n");
> @@ -180,7 +183,7 @@ void f2fs_parse_options(int argc, char *argv[])
>   }
>  
>   if (!strcmp("fsck.f2fs", prog)) {
> - const char *option_string = ":ad:fg:p:q:StyV";
> + const char *option_string = ":ad:fg:O:p:q:StyV";
>   int opt = 0;
>   struct option long_opt[] = {
>   {"dry-run", no_argument, 0, 1},
> @@ -203,6 +206,10 @@ void f2fs_parse_options(int argc, char *argv[])
>   if (!strcmp(optarg, "android"))
>   c.defset = CONF_ANDROID;
>   break;
> + case 'O':
> + if (parse_feature(feature_table, optarg))
> + fsck_usage();
> + break;
>   case 'p':
>   /* preen mode has different levels:
>*  0: default level, the same as -a
> diff --git a/fsck/mount.c b/fsck/mount.c
> index e5574c5..b374b46 100644
> --- a/fsck/mount.c
> +++ b/fsck/mount.c
> @@ -2144,6 +2144,22 @@ void write_checkpoint(struct f2fs_sb_info *sbi)
>   ASSERT(ret >= 0);
>  }
>  
> +void write_superblock(struct f2fs_super_block *new_sb)
> +{
> + int index, ret;
> + u_int8_t *buf;
> +
> + buf = calloc(BLOCK_SZ, 1);
> +
> + memcpy(buf + F2FS_SUPER_OFFSET, new_sb, sizeof(*new_sb));
> + for (index = 0; index < 2; index++) {
> + ret = dev_write_block(buf, index);
> + ASSERT(ret >= 0);
> + }
> + free(buf);
> + DBG(0, "Info: Done to rebuild superblock\n");
> +}
> +
>  void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
>  {
>   struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
> @@ -2314,6 +2330,26 @@ static int check_sector_size(struct f2fs_super_block 
> *sb)
>   return 0;
>  }
>  
> +static void tune_sb_features(struct f2fs_sb_info *sbi)
> +{
> + int sb_changed = 0;
> + struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
> +
> + if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) &&
> + c.feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) {
> + sb->feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
> + MSG(0, "Info: Set Encryption feature\n");
> + sb_changed = 1;
> + }
> + /* TODO: quota needs to allocate inode numbers */
> +
> + c.feature = sb->feature;
> + if (!sb_changed)
> + return;
> +
> + write_superblock(sb);
> +}
> +
>  int f2fs_do_mount(struct f2fs_sb_info *sbi)
>  {
>   struct f2fs_checkpoint *cp = NULL;
> @@ -2365,7 +2401,8 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi)
>   }
>  
>   c.bug_on = 0;
> - c.feature = sb->feature;
> +
> + tune_sb_features(sbi);
>  
>   /* precompute checksum seed for metadata */
>   if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
> diff --git a/fsck/resize.c b/fsck/resize.c
> index 019da71..d285dd7 100644
> --- a/fsck/resize.c
> +++ 

[f2fs-dev] [PATCH v3] mkfs.f2fs: support multiple features with one "-O"

2018-04-07 Thread Junling Zheng
Now one "-O" option can support multiple features separated
by a comma or blank, such as:
feature1,feature2,... or "feature1 feature2 ..."

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
Reviewed-by: Chao Yu <yuch...@huawei.com>
---
Changes from v1:
 - free buf to fix memory leak.
Changes from v2:
 - modify usage and man page to show this feature.
 man/mkfs.f2fs.8 |  3 ++-
 mkfs/f2fs_format_main.c | 33 +
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/man/mkfs.f2fs.8 b/man/mkfs.f2fs.8
index 442c0ea..29dd68f 100644
--- a/man/mkfs.f2fs.8
+++ b/man/mkfs.f2fs.8
@@ -112,7 +112,8 @@ is hidden to users, and utilized by F2FS cleaner. If not 
specified, the best
 number will be assigned automatically accoring to the partition size.
 .TP
 .BI \-O " feature-list"
-Specify a feature list in order f2fs filesystem will supports.
+Specify a feature list like feature1[feature2,feature3,...] in order f2fs
+filesystem will supports.
 e.g "encrypt" and so on.
 .TP
 .BI \-q
diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index 449a0ed..a6e4474 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -51,7 +51,7 @@ static void mkfs_usage()
MSG(0, "  -l label\n");
MSG(0, "  -m support zoned block device [default:0]\n");
MSG(0, "  -o overprovision ratio [default:5]\n");
-   MSG(0, "  -O [feature list] e.g. \"encrypt\"\n");
+   MSG(0, "  -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
MSG(0, "  -q quiet mode\n");
MSG(0, "  -s # of segments per section [default:1]\n");
MSG(0, "  -S sparse mode\n");
@@ -81,10 +81,8 @@ static void f2fs_show_info()
MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
 }
 
-static void parse_feature(const char *features)
+static void set_feature_bits(char *features)
 {
-   while (*features == ' ')
-   features++;
if (!strcmp(features, "encrypt")) {
c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
} else if (!strcmp(features, "verity")) {
@@ -109,6 +107,33 @@ static void parse_feature(const char *features)
}
 }
 
+static void parse_feature(const char *features)
+{
+   char *buf, *sub, *next;
+
+   buf = calloc(strlen(features) + 1, sizeof(char));
+   ASSERT(buf);
+   strncpy(buf, features, strlen(features) + 1);
+
+   for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
+   /* Skip the beginning blanks */
+   while (*sub && *sub == ' ')
+   sub++;
+   next = sub;
+   /* Skip a feature word */
+   while (*next && *next != ' ' && *next != ',')
+   next++;
+
+   if (*next == 0)
+   next = NULL;
+   else
+   *next = 0;
+
+   set_feature_bits(sub);
+   }
+   free(buf);
+}
+
 static void f2fs_parse_options(int argc, char *argv[])
 {
static const char *option_string = "qa:c:d:e:E:il:mo:O:s:S:z:t:fw:";
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH v2] mkfs.f2fs: support multiple features with one "-O"

2018-04-03 Thread Junling Zheng
Now one "-O" option can support multiple features separated
by a comma or blank, such as:
feature1,feature2,... or "feature1 feature2 ..."

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
Changes from v1:
 - free buf to fix memory leak.
 mkfs/f2fs_format_main.c | 31 ---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index 741600e..278efff 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -80,10 +80,8 @@ static void f2fs_show_info()
MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
 }
 
-static void parse_feature(const char *features)
+static void set_feature_bits(char *features)
 {
-   while (*features == ' ')
-   features++;
if (!strcmp(features, "encrypt")) {
c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
} else if (!strcmp(features, "verity")) {
@@ -108,6 +106,33 @@ static void parse_feature(const char *features)
}
 }
 
+static void parse_feature(const char *features)
+{
+   char *buf, *sub, *next;
+
+   buf = calloc(strlen(features) + 1, sizeof(char));
+   ASSERT(buf);
+   strncpy(buf, features, strlen(features) + 1);
+
+   for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
+   /* Skip the beginning blanks */
+   while (*sub && *sub == ' ')
+   sub++;
+   next = sub;
+   /* Skip a feature word */
+   while (*next && *next != ' ' && *next != ',')
+   next++;
+
+   if (*next == 0)
+   next = NULL;
+   else
+   *next = 0;
+
+   set_feature_bits(sub);
+   }
+   free(buf);
+}
+
 static void f2fs_parse_options(int argc, char *argv[])
 {
static const char *option_string = "qa:c:d:e:E:il:mo:O:s:S:z:t:f";
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] mkfs.f2fs: support multiple features with one "-O"

2018-04-03 Thread Junling Zheng
On 2018/4/3 17:24, Chao Yu wrote:
> Hi Junling,
> 
> On 2018/4/3 10:15, Junling Zheng wrote:
>> Hi, Chao
>>
>> On 2018/4/2 21:35, Chao Yu wrote:
>>> On 2018/4/2 12:19, Junling Zheng wrote:
>>>> Now one "-O" option can support multiple features separated
>>>> by a comma or blank, such as:
>>>> feature1,feature2,... or "feature1 feature2 ..."
>>>
>>> At a glance, can last sector number be parsed as feature name?
>>>
>>
>> No, we should use quotation marks to wrap whole features while separating 
>> them with blanks.
> 
> Sorry, that's right.
> 
>>
>> Thanks,
>> Junling
>>
>>> Thanks,
>>>
>>>>
>>>> Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
>>>> ---
>>>>  mkfs/f2fs_format_main.c | 30 +++---
>>>>  1 file changed, 27 insertions(+), 3 deletions(-)
>>>>
>>>> diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
>>>> index 741600e..76ff296 100644
>>>> --- a/mkfs/f2fs_format_main.c
>>>> +++ b/mkfs/f2fs_format_main.c
>>>> @@ -80,10 +80,8 @@ static void f2fs_show_info()
>>>>MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
>>>>  }
>>>>  
>>>> -static void parse_feature(const char *features)
>>>> +static void set_feature_bits(char *features)
>>>>  {
>>>> -  while (*features == ' ')
>>>> -  features++;
>>>>if (!strcmp(features, "encrypt")) {
>>>>c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
>>>>} else if (!strcmp(features, "verity")) {
>>>> @@ -108,6 +106,32 @@ static void parse_feature(const char *features)
>>>>}
>>>>  }
>>>>  
>>>> +static void parse_feature(const char *features)
>>>> +{
>>>> +  char *buf, *sub, *next;
>>>> +
>>>> +  buf = calloc(strlen(features) + 1, sizeof(char));
>>>> +  ASSERT(buf);
>>>> +  strncpy(buf, features, strlen(features) + 1);
>>>> +
>>>> +  for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
>>>> +  /* Skip the beginning blanks */
>>>> +  while (*sub && *sub == ' ')
>>>> +  sub++;
>>>> +  next = sub;
>>>> +  /* Skip a feature word */
>>>> +  while (*next && *next != ' ' && *next != ',')
>>>> +  next++;
>>>> +
>>>> +  if (*next == 0)
>>>> +  next = NULL;
>>>> +  else
>>>> +  *next = 0;
>>>> +
>>>> +  set_feature_bits(sub);
>>>> +  }
> 
> free(buf);?
> 

Sorry :(
I'll send patch v2...

> Thanks,
> 
>>>> +}
>>>> +
>>>>  static void f2fs_parse_options(int argc, char *argv[])
>>>>  {
>>>>static const char *option_string = "qa:c:d:e:E:il:mo:O:s:S:z:t:f";
>>>>
>>>
>>> .
>>>
>>
>>
>>
>> .
>>
> 
> 
> .
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] mkfs.f2fs: support multiple features with one "-O"

2018-04-02 Thread Junling Zheng
Hi, Chao

On 2018/4/2 21:35, Chao Yu wrote:
> On 2018/4/2 12:19, Junling Zheng wrote:
>> Now one "-O" option can support multiple features separated
>> by a comma or blank, such as:
>> feature1,feature2,... or "feature1 feature2 ..."
> 
> At a glance, can last sector number be parsed as feature name?
> 

No, we should use quotation marks to wrap whole features while separating them 
with blanks.

Thanks,
Junling

> Thanks,
> 
>>
>> Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
>> ---
>>  mkfs/f2fs_format_main.c | 30 +++---
>>  1 file changed, 27 insertions(+), 3 deletions(-)
>>
>> diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
>> index 741600e..76ff296 100644
>> --- a/mkfs/f2fs_format_main.c
>> +++ b/mkfs/f2fs_format_main.c
>> @@ -80,10 +80,8 @@ static void f2fs_show_info()
>>  MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
>>  }
>>  
>> -static void parse_feature(const char *features)
>> +static void set_feature_bits(char *features)
>>  {
>> -while (*features == ' ')
>> -features++;
>>  if (!strcmp(features, "encrypt")) {
>>  c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
>>  } else if (!strcmp(features, "verity")) {
>> @@ -108,6 +106,32 @@ static void parse_feature(const char *features)
>>  }
>>  }
>>  
>> +static void parse_feature(const char *features)
>> +{
>> +char *buf, *sub, *next;
>> +
>> +buf = calloc(strlen(features) + 1, sizeof(char));
>> +ASSERT(buf);
>> +strncpy(buf, features, strlen(features) + 1);
>> +
>> +for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
>> +/* Skip the beginning blanks */
>> +while (*sub && *sub == ' ')
>> +sub++;
>> +next = sub;
>> +/* Skip a feature word */
>> +while (*next && *next != ' ' && *next != ',')
>> +next++;
>> +
>> +if (*next == 0)
>> +next = NULL;
>> +else
>> +*next = 0;
>> +
>> +set_feature_bits(sub);
>> +}
>> +}
>> +
>>  static void f2fs_parse_options(int argc, char *argv[])
>>  {
>>  static const char *option_string = "qa:c:d:e:E:il:mo:O:s:S:z:t:f";
>>
> 
> .
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] mkfs.f2fs: support multiple features with one "-O"

2018-04-01 Thread Junling Zheng
Now one "-O" option can support multiple features separated
by a comma or blank, such as:
feature1,feature2,... or "feature1 feature2 ..."

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 mkfs/f2fs_format_main.c | 30 +++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index 741600e..76ff296 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -80,10 +80,8 @@ static void f2fs_show_info()
MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
 }
 
-static void parse_feature(const char *features)
+static void set_feature_bits(char *features)
 {
-   while (*features == ' ')
-   features++;
if (!strcmp(features, "encrypt")) {
c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
} else if (!strcmp(features, "verity")) {
@@ -108,6 +106,32 @@ static void parse_feature(const char *features)
}
 }
 
+static void parse_feature(const char *features)
+{
+   char *buf, *sub, *next;
+
+   buf = calloc(strlen(features) + 1, sizeof(char));
+   ASSERT(buf);
+   strncpy(buf, features, strlen(features) + 1);
+
+   for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
+   /* Skip the beginning blanks */
+   while (*sub && *sub == ' ')
+   sub++;
+   next = sub;
+   /* Skip a feature word */
+   while (*next && *next != ' ' && *next != ',')
+   next++;
+
+   if (*next == 0)
+   next = NULL;
+   else
+   *next = 0;
+
+   set_feature_bits(sub);
+   }
+}
+
 static void f2fs_parse_options(int argc, char *argv[])
 {
static const char *option_string = "qa:c:d:e:E:il:mo:O:s:S:z:t:f";
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] libf2fs: reset wanted_total_sectors by new sector_size

2018-03-30 Thread Junling Zheng
On 2018/3/30 19:26, Chao Yu wrote:
> On 2018/3/30 18:51, Junling Zheng wrote:
>> Hi,
>>
>> On 2018/3/30 17:28, Chao Yu wrote:
>>> Hi All,
>>>
>>> On 2018/3/28 1:19, Jaegeuk Kim wrote:
>>>> From: katao <ka...@xiaomi.com>
>>>>
>>>> The args of wanted_total_sectors is calculated based
>>>> on the DEFAULT_SECTOR_SIZE(512Bytes).get_device_info(i)
>>>> may be reset dev_sector_size, we should reset the number
>>>> of wanted_total_sectors.
>>>>
>>>> This bug was reported to Google Issue Tracker.
>>>> Link: https://issuetracker.google.com/issues/76407663
>>>
>>> I don't think this is the right way, since now we have changed previous
>>> sector_counter's meaning, some applications, for example, like xfstests 
>>> will get
>>> device's real sector size via blockdev --getsize64, then calculate total 
>>> wanted
>>> sector count by total_wanted_size / real_sector_size, if we changed default
>>> sector size to 512bytes, xfstests will pass a wrong sector number, result in
>>> getting wrong partition size.
>>>
>>> For something worse, in order to get the correct sector number, we have to
>>> change the way of calculation method of xfstests for new mkfs, but how can
>>> xfstests know the current version of mkfs is new or old...
>>>
>>> I think the change didn't consider backward compatibility of mkfs, so, in 
>>> order
>>> to keep that, we'd better to let user pass the right sector number based on
>>> their device, or we can introduce a new parameter to indicate user wanted 
>>> total
>>> size.
>>>
>>> How do you think?
>>>
>>
>> Agree. It's not backward-compatible. Most users can pass the correct sector 
>> number
>> calculated by the real sector size. For those very few users using 512B 
>> despite of
>> the actual sector size, all we need to do is informing them the real sector 
>> size.
> 
> The problem is via passed sector number, we can't know user has already knew 
> the
> real sector size or not, so we don't have any chance to info them.
> 

Yeah, we can't guess users' behaviors. And only when wanted size is over device 
size,
we can inform users the real sector size.

> Thanks,
> 
>>
>> Thanks,
>> Junling
>>
>>> Thanks,
>>>
>>>>
>>>> Signed-off-by: katao <ka...@xiaomi.com>
>>>> Signed-off-by: Jaegeuk Kim <jaeg...@google.com>
>>>> ---
>>>>  lib/libf2fs.c | 9 -
>>>>  1 file changed, 8 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
>>>> index 0c684d5..5f11796 100644
>>>> --- a/lib/libf2fs.c
>>>> +++ b/lib/libf2fs.c
>>>> @@ -799,8 +799,15 @@ int get_device_info(int i)
>>>>  #ifdef BLKSSZGET
>>>>if (ioctl(fd, BLKSSZGET, _size) < 0)
>>>>MSG(0, "\tError: Using the default sector size\n");
>>>> -  else if (dev->sector_size < sector_size)
>>>> +  else if (dev->sector_size < sector_size){
>>>> +  /*
>>>> +   * wanted_total_sectors need to be reset by new
>>>> +   * sector_size.
>>>> +   */
>>>> +  c.wanted_total_sectors = (c.wanted_total_sectors *
>>>> +  dev->sector_size) / sector_size;
>>>>dev->sector_size = sector_size;
>>>> +  }
>>>>  #endif
>>>>  #ifdef BLKGETSIZE64
>>>>if (ioctl(fd, BLKGETSIZE64, >total_sectors) < 0) {
>>>>
>>>
>>>
>>> .
>>>
>>
>>
>>
>> .
>>
> 
> 
> .
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] libf2fs: reset wanted_total_sectors by new sector_size

2018-03-30 Thread Junling Zheng
Hi,

On 2018/3/30 17:28, Chao Yu wrote:
> Hi All,
> 
> On 2018/3/28 1:19, Jaegeuk Kim wrote:
>> From: katao 
>>
>> The args of wanted_total_sectors is calculated based
>> on the DEFAULT_SECTOR_SIZE(512Bytes).get_device_info(i)
>> may be reset dev_sector_size, we should reset the number
>> of wanted_total_sectors.
>>
>> This bug was reported to Google Issue Tracker.
>> Link: https://issuetracker.google.com/issues/76407663
> 
> I don't think this is the right way, since now we have changed previous
> sector_counter's meaning, some applications, for example, like xfstests will 
> get
> device's real sector size via blockdev --getsize64, then calculate total 
> wanted
> sector count by total_wanted_size / real_sector_size, if we changed default
> sector size to 512bytes, xfstests will pass a wrong sector number, result in
> getting wrong partition size.
> 
> For something worse, in order to get the correct sector number, we have to
> change the way of calculation method of xfstests for new mkfs, but how can
> xfstests know the current version of mkfs is new or old...
> 
> I think the change didn't consider backward compatibility of mkfs, so, in 
> order
> to keep that, we'd better to let user pass the right sector number based on
> their device, or we can introduce a new parameter to indicate user wanted 
> total
> size.
> 
> How do you think?
> 

Agree. It's not backward-compatible. Most users can pass the correct sector 
number
calculated by the real sector size. For those very few users using 512B despite 
of
the actual sector size, all we need to do is informing them the real sector 
size.

Thanks,
Junling

> Thanks,
> 
>>
>> Signed-off-by: katao 
>> Signed-off-by: Jaegeuk Kim 
>> ---
>>  lib/libf2fs.c | 9 -
>>  1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
>> index 0c684d5..5f11796 100644
>> --- a/lib/libf2fs.c
>> +++ b/lib/libf2fs.c
>> @@ -799,8 +799,15 @@ int get_device_info(int i)
>>  #ifdef BLKSSZGET
>>  if (ioctl(fd, BLKSSZGET, _size) < 0)
>>  MSG(0, "\tError: Using the default sector size\n");
>> -else if (dev->sector_size < sector_size)
>> +else if (dev->sector_size < sector_size){
>> +/*
>> + * wanted_total_sectors need to be reset by new
>> + * sector_size.
>> + */
>> +c.wanted_total_sectors = (c.wanted_total_sectors *
>> +dev->sector_size) / sector_size;
>>  dev->sector_size = sector_size;
>> +}
>>  #endif
>>  #ifdef BLKGETSIZE64
>>  if (ioctl(fd, BLKGETSIZE64, >total_sectors) < 0) {
>>
> 
> 
> .
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] f2fs: fix a wrong condition in f2fs_skip_inode_update

2018-03-29 Thread Junling Zheng
Fix commit 97dd26ad8347 (f2fs: fix wrong AUTO_RECOVER condition).
We should use ~PAGE_MASK to determine whether i_size is aligned to
the f2fs's block size or not.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fs/f2fs/f2fs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index ae69dc358401..8c159785f6b5 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -2469,7 +2469,7 @@ static inline bool f2fs_skip_inode_update(struct inode 
*inode, int dsync)
}
if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
file_keep_isize(inode) ||
-   i_size_read(inode) & PAGE_MASK)
+   i_size_read(inode) & ~PAGE_MASK)
return false;
 
down_read(_I(inode)->i_sem);
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [RFC PATCH v2] mkfs.f2fs: use 512B as the sector size criterion

2018-03-28 Thread Junling Zheng
Use 512 bytes as the sector size criterion while specifying the
amount of sectors passed to mkfs.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
Changes from v1:
 - specify the sector size criterion in man page
 - replace "Warning" with "Info" while wanted size is over device size

 lib/libf2fs.c   | 5 +++--
 man/mkfs.f2fs.8 | 2 +-
 mkfs/f2fs_format_main.c | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/lib/libf2fs.c b/lib/libf2fs.c
index 5f11796..906f789 100644
--- a/lib/libf2fs.c
+++ b/lib/libf2fs.c
@@ -1002,9 +1002,10 @@ int f2fs_get_device_info(void)
if (get_device_info(i))
return -1;
 
+   MSG(0, "Info: total sectors = %"PRIu64", "
+   "wanted sectors = %"PRIu64", in %u bytes\n",
+   c.total_sectors, c.wanted_total_sectors, c.sector_size);
if (c.wanted_total_sectors < c.total_sectors) {
-   MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
-   c.total_sectors, c.sector_size);
c.total_sectors = c.wanted_total_sectors;
c.devices[0].total_sectors = c.total_sectors;
}
diff --git a/man/mkfs.f2fs.8 b/man/mkfs.f2fs.8
index c2f9c86..e48ce39 100644
--- a/man/mkfs.f2fs.8
+++ b/man/mkfs.f2fs.8
@@ -63,7 +63,7 @@ mkfs.f2fs \- create an F2FS file system
 is used to create a f2fs file system (usually in a disk partition).
 \fIdevice\fP is the special file corresponding to the device (e.g.
 \fI/dev/sdXX\fP).
-\fIsectors\fP is optionally given for specifing the filesystem size.
+\fIsectors\fP (in 512 bytes) is optionally given for specifing the filesystem 
size.
 .PP
 The exit code returned by
 .B mkfs.f2fs
diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index f23fd84..71fd7c2 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -57,7 +57,7 @@ static void mkfs_usage()
MSG(0, "  -S sparse mode\n");
MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
MSG(0, "  -z # of sections per zone [default:1]\n");
-   MSG(0, "sectors: number of sectors. [default: determined by device 
size]\n");
+   MSG(0, "sectors: number of sectors (in 512 bytes). [default: determined 
by device size]\n");
exit(1);
 }
 
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [RFC PATCH] mkfs.f2fs: use 512B as the sector size criterion

2018-03-28 Thread Junling Zheng
Hi Jaegeuk,

On 2018/3/29 4:30, Jaegeuk Kim wrote:
> Hi Junling,
> 
> On Wed, Mar 28, 2018 at 2:04 AM, Junling Zheng <zhengjunl...@huawei.com 
> <mailto:zhengjunl...@huawei.com>> wrote:
> 
> Use 512 bytes as the sector size criterion while specifying the
> amount of sectors passed to mkfs.
> 
> Signed-off-by: Junling Zheng <zhengjunl...@huawei.com 
> <mailto:zhengjunl...@huawei.com>>
> ---
>  lib/libf2fs.c   | 11 ---
>  mkfs/f2fs_format_main.c |  2 +-
>  2 files changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
> index 5f11796..5fdcdde 100644
> --- a/lib/libf2fs.c
> +++ b/lib/libf2fs.c
> @@ -1002,9 +1002,14 @@ int f2fs_get_device_info(void)
> if (get_device_info(i))
> return -1;
> 
> -   if (c.wanted_total_sectors < c.total_sectors) {
> -   MSG(0, "Info: total device sectors = %"PRIu64" (in %u 
> bytes)\n",
> -   c.total_sectors, c.sector_size);
> +   if (c.wanted_total_sectors > c.total_sectors)
> +   MSG(0, "Warning: total sectors = %"PRIu64", "
> +   "wanted sectors = %"PRIu64", in %u bytes\n",
> +   c.total_sectors, c.wanted_total_sectors, 
> c.sector_size);
> 
> 
> This seems not warning message. IMO, it'd be enough to inform just
> wanted size over device size?

This warning will not break mkfs, just a warning to inform user, and mkfs will 
use c.total_sectors
to format device :)

>  
> 
> +   else {
> +   MSG(0, "Info: total sectors = %"PRIu64", "
> +   "wanted sectors = %"PRIu64", in %u bytes\n",
> +   c.total_sectors, c.wanted_total_sectors, 
> c.sector_size);
> c.total_sectors = c.wanted_total_sectors;
> c.devices[0].total_sectors = c.total_sectors;
> }
> diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
> index f23fd84..71fd7c2 100644
> --- a/mkfs/f2fs_format_main.c
> +++ b/mkfs/f2fs_format_main.c
> @@ -57,7 +57,7 @@ static void mkfs_usage()
> MSG(0, "  -S sparse mode\n");
> MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
> MSG(0, "  -z # of sections per zone [default:1]\n");
> -   MSG(0, "sectors: number of sectors. [default: determined by 
> device size]\n");
> +   MSG(0, "sectors: number of sectors (in 512 bytes). [default: 
> determined by device size]\n");
> 
> 
> We also need to specify this in man page.

OK, I'll add it.

>  
> 
> exit(1);
>  }
> 
> --
> 2.16.2
> 
> 
> 
> 
> -- 
> Jaegeuk Kim



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [RFC PATCH] mkfs.f2fs: use 512B as the sector size criterion

2018-03-28 Thread Junling Zheng
Use 512 bytes as the sector size criterion while specifying the
amount of sectors passed to mkfs.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 lib/libf2fs.c   | 11 ---
 mkfs/f2fs_format_main.c |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/lib/libf2fs.c b/lib/libf2fs.c
index 5f11796..5fdcdde 100644
--- a/lib/libf2fs.c
+++ b/lib/libf2fs.c
@@ -1002,9 +1002,14 @@ int f2fs_get_device_info(void)
if (get_device_info(i))
return -1;
 
-   if (c.wanted_total_sectors < c.total_sectors) {
-   MSG(0, "Info: total device sectors = %"PRIu64" (in %u bytes)\n",
-   c.total_sectors, c.sector_size);
+   if (c.wanted_total_sectors > c.total_sectors)
+   MSG(0, "Warning: total sectors = %"PRIu64", "
+   "wanted sectors = %"PRIu64", in %u bytes\n",
+   c.total_sectors, c.wanted_total_sectors, c.sector_size);
+   else {
+   MSG(0, "Info: total sectors = %"PRIu64", "
+   "wanted sectors = %"PRIu64", in %u bytes\n",
+   c.total_sectors, c.wanted_total_sectors, c.sector_size);
c.total_sectors = c.wanted_total_sectors;
c.devices[0].total_sectors = c.total_sectors;
}
diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index f23fd84..71fd7c2 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -57,7 +57,7 @@ static void mkfs_usage()
MSG(0, "  -S sparse mode\n");
MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
MSG(0, "  -z # of sections per zone [default:1]\n");
-   MSG(0, "sectors: number of sectors. [default: determined by device 
size]\n");
+   MSG(0, "sectors: number of sectors (in 512 bytes). [default: determined 
by device size]\n");
exit(1);
 }
 
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


Re: [f2fs-dev] [PATCH] libf2fs: reset wanted_total_sectors by new sector_size

2018-03-27 Thread Junling Zheng
Hi, Jaegeuk:

On 2018/3/28 1:19, Jaegeuk Kim wrote:
> From: katao 
> 
> The args of wanted_total_sectors is calculated based
> on the DEFAULT_SECTOR_SIZE(512Bytes).get_device_info(i)
> may be reset dev_sector_size, we should reset the number
> of wanted_total_sectors.
> 
> This bug was reported to Google Issue Tracker.
> Link: https://issuetracker.google.com/issues/76407663
> 

This fix is puzzling...

IMO, we should not recalculate wanted_total_sectors here.
c.wanted_total_sectors is got from user in f2fs_parse_options.
Users must know the sector size of device they use, and they need to
pass a correct wanted_total_sectors to mkfs.f2fs.

Thanks,
Junling

> Signed-off-by: katao 
> Signed-off-by: Jaegeuk Kim 
> ---
>  lib/libf2fs.c | 9 -
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/libf2fs.c b/lib/libf2fs.c
> index 0c684d5..5f11796 100644
> --- a/lib/libf2fs.c
> +++ b/lib/libf2fs.c
> @@ -799,8 +799,15 @@ int get_device_info(int i)
>  #ifdef BLKSSZGET
>   if (ioctl(fd, BLKSSZGET, _size) < 0)
>   MSG(0, "\tError: Using the default sector size\n");
> - else if (dev->sector_size < sector_size)
> + else if (dev->sector_size < sector_size){
> + /*
> +  * wanted_total_sectors need to be reset by new
> +  * sector_size.
> +  */
> + c.wanted_total_sectors = (c.wanted_total_sectors *
> + dev->sector_size) / sector_size;
>   dev->sector_size = sector_size;
> + }
>  #endif
>  #ifdef BLKGETSIZE64
>   if (ioctl(fd, BLKGETSIZE64, >total_sectors) < 0) {
> 



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] fsck.f2fs: simplify fsck_chk_quota_node in PREEN_MODE_1

2018-03-26 Thread Junling Zheng
Do not do fsck_chk_node_blk to simplify fsck_chk_quota_node in
PREEN_MODE_1, as well as fsck_chk_orphan_node.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/fsck.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index bc91839..91c8529 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -1671,6 +1671,7 @@ int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
if (!IS_VALID_NID(sbi, ino) ||
!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
return -EINVAL;
+   continue;
}
ret = fsck_chk_node_blk(sbi, NULL, ino,
F2FS_FT_REG_FILE, TYPE_INODE, _cnt, NULL);
-- 
2.16.2


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH v4] f2fs: introduce mount option for fsync mode

2018-02-28 Thread Junling Zheng
Commit "0a007b97aad6"(f2fs: recover directory operations by fsync)
fixed xfstest generic/342 case, but it also increased the written
data and caused the performance degradation. In most cases, there's
no need to do so heavy fsync actually.

So we introduce new mount option "fsync_mode={posix,strict}" to
control the policy of fsync. "fsync_mode=posix" is set by default,
and means that f2fs uses a light fsync, which follows POSIX semantics.
And "fsync_mode=strict" means that it's a heavy fsync, which behaves
in line with xfs, ext4 and btrfs, where generic/342 will pass, but
the performance will regress.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
Changes from v3:
 - add fsync_mode in sbi to record fsync mode
 - keep the extensibility for fsync_mode
Changes from v2:
 - Change to "fsync={posix,strict}" format
 - Set "fsync=posix" default
Changes from v1:
 - Add document modify
 - Add reviewer
 Documentation/filesystems/f2fs.txt |  7 +++
 fs/f2fs/dir.c  |  3 ++-
 fs/f2fs/f2fs.h |  8 
 fs/f2fs/file.c |  3 ++-
 fs/f2fs/namei.c|  9 ++---
 fs/f2fs/super.c| 24 
 6 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/Documentation/filesystems/f2fs.txt 
b/Documentation/filesystems/f2fs.txt
index 0409c47584ea..514e44983a8e 100644
--- a/Documentation/filesystems/f2fs.txt
+++ b/Documentation/filesystems/f2fs.txt
@@ -182,6 +182,13 @@ whint_mode=%s  Control which write hints are 
passed down to block
passes down hints with its policy.
 alloc_mode=%s  Adjust block allocation policy, which supports "reuse"
and "default".
+fsync_mode=%s  Control the policy of fsync. Currently supports "posix"
+   and "strict". In "posix" mode, which is default, fsync
+   will follow POSIX semantics and does a light operation
+   to improve the filesystem performance. In "strict" mode,
+   fsync will be heavy and behaves in line with xfs, ext4
+   and btrfs, where xfstest generic/342 will pass, but the
+   performance will regress.
 
 

 DEBUGFS ENTRIES
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index f00b5ed8c011..cce8ce11c2cc 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -713,7 +713,8 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, 
struct page *page,
 
f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
 
-   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
+   if (F2FS_I_SB(dir)->fsync_mode == FSYNC_MODE_STRICT)
+   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
 
if (f2fs_has_inline_dentry(dir))
return f2fs_delete_inline_entry(dentry, page, dir, inode);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index e4a782b23e66..b85dcfb30bac 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -1052,6 +1052,11 @@ enum {
ALLOC_MODE_REUSE,   /* reuse segments as much as possible */
 };
 
+enum fsync_mode {
+   FSYNC_MODE_POSIX,   /* fsync follows posix semantics */
+   FSYNC_MODE_STRICT,  /* fsync behaves in line with ext4 */
+};
+
 struct f2fs_sb_info {
struct super_block *sb; /* pointer to VFS super block */
struct proc_dir_entry *s_proc;  /* proc entry */
@@ -1241,6 +1246,9 @@ struct f2fs_sb_info {
 
/* segment allocation policy */
int alloc_mode;
+
+   /* fsync policy */
+   int fsync_mode;
 };
 
 #ifdef CONFIG_F2FS_FAULT_INJECTION
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 6a202e5a6386..99fa207fc310 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -165,7 +165,8 @@ static inline enum cp_reason_type need_do_checkpoint(struct 
inode *inode)
cp_reason = CP_FASTBOOT_MODE;
else if (sbi->active_logs == 2)
cp_reason = CP_SPEC_LOG_NUM;
-   else if (need_dentry_mark(sbi, inode->i_ino) &&
+   else if (sbi->fsync_mode == FSYNC_MODE_STRICT &&
+   need_dentry_mark(sbi, inode->i_ino) &&
exist_written_data(sbi, F2FS_I(inode)->i_pino, TRANS_DIR_INO))
cp_reason = CP_RECOVER_DIR;
 
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 75135191a73d..010fc9a4a589 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -977,7 +977,8 @@ static int f2fs_rename(struct inode *old_dir, struct dentry 
*old_dentry,
}
f2fs_i_links_write(old_dir, false);
}
-   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   if (sbi->fsync_mode == FSYNC_MODE_STRICT)
+ 

[f2fs-dev] [PATCH v3] f2fs: introduce "fsync={posix, strict}" mount options

2018-02-13 Thread Junling Zheng
Commit "0a007b97aad6"(f2fs: recover directory operations by fsync)
fixed xfstest generic/342 case, but it also increased the written
data and caused the performance degradation. In most cases, there's
no need to do so heavy fsync actually.

So we introduce two new mount options "fsync={posix,strict}" to
control the policy of fsync. "fsync=posix" is set by default, and
means that f2fs uses a light fsync, which follows POSIX semantics.
And "fsync=strict" means that it's a heavy fsync, which behaves in
line with xfs, ext4 and btrfs, where generic/342 will pass, but the
performance will regress.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
Changes from v2:
 - Change to "fsync={posix,strict}" format
 - Set "fsync=posix" default
Changes from v1:
 - Add document modify
 - Add reviewer
 Documentation/filesystems/f2fs.txt |  7 +++
 fs/f2fs/dir.c  |  3 ++-
 fs/f2fs/f2fs.h |  1 +
 fs/f2fs/file.c |  3 ++-
 fs/f2fs/namei.c|  9 ++---
 fs/f2fs/super.c| 10 ++
 6 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/Documentation/filesystems/f2fs.txt 
b/Documentation/filesystems/f2fs.txt
index 0caf7da0a532..c00d40655bbc 100644
--- a/Documentation/filesystems/f2fs.txt
+++ b/Documentation/filesystems/f2fs.txt
@@ -180,6 +180,13 @@ whint_mode=%s  Control which write hints are 
passed down to block
down hints. In "user-based" mode, f2fs tries to pass
down hints given by users. And in "fs-based" mode, f2fs
passes down hints with its policy.
+fsync=%s   Control the policy of fsync. This supports "posix" and
+   "strict" modes. In "posix" mode, which is default, fsync
+   will follow POSIX semantics and does a light operation 
to
+   improve the filesystem performance. In "strict" mode, 
fsync
+   will be heavy and behaves in line with xfs, ext4 and 
btrfs,
+   where xfstest generic/342 will pass, but the performance
+   will regress.
 
 

 DEBUGFS ENTRIES
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index f00b5ed8c011..37d1259f1b92 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -713,7 +713,8 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, 
struct page *page,
 
f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
 
-   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
+   if (test_opt(F2FS_I_SB(dir), STRICT_FSYNC))
+   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
 
if (f2fs_has_inline_dentry(dir))
return f2fs_delete_inline_entry(dentry, page, dir, inode);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index dbe87c7a266e..8cf914d12f17 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -97,6 +97,7 @@ extern char *fault_name[FAULT_MAX];
 #define F2FS_MOUNT_QUOTA   0x0040
 #define F2FS_MOUNT_INLINE_XATTR_SIZE   0x0080
 #define F2FS_MOUNT_RESERVE_ROOT0x0100
+#define F2FS_MOUNT_STRICT_FSYNC0x0200
 
 #define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
 #define set_opt(sbi, option)   ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 672a542e5464..509b3e045247 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -165,7 +165,8 @@ static inline enum cp_reason_type need_do_checkpoint(struct 
inode *inode)
cp_reason = CP_FASTBOOT_MODE;
else if (sbi->active_logs == 2)
cp_reason = CP_SPEC_LOG_NUM;
-   else if (need_dentry_mark(sbi, inode->i_ino) &&
+   else if (test_opt(sbi, STRICT_FSYNC) &&
+   need_dentry_mark(sbi, inode->i_ino) &&
exist_written_data(sbi, F2FS_I(inode)->i_pino, TRANS_DIR_INO))
cp_reason = CP_RECOVER_DIR;
 
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index c4c94c7e9f4f..5a385b8ab172 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -936,7 +936,8 @@ static int f2fs_rename(struct inode *old_dir, struct dentry 
*old_dentry,
}
f2fs_i_links_write(old_dir, false);
}
-   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   if (test_opt(sbi, STRICT_FSYNC))
+   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
 
f2fs_unlock_op(sbi);
 
@@ -1091,8 +1092,10 @@ static int f2fs_cross_rename(struct inode *old_dir, 
struct dentry *old_dentry,
}
f2fs_mark_inode_dirty_sync(new_dir, false);
 
-   add_ino_entry(sbi, old_dir->i_

Re: [f2fs-dev] [PATCH v2] f2fs: introduce "strict_fsync" for posix standard fsync

2018-02-10 Thread Junling Zheng
Hi, Jaegeuk

On 2018/2/10 8:44, Jaegeuk Kim wrote:
> On 02/02, Junling Zheng wrote:
>> Commit "0a007b97aad6"(f2fs: recover directory operations by fsync)
>> fixed xfstest generic/342 case, but it also increased the written
>> data and caused the performance degradation. In most cases, there's
>> no need to do so heavily fsync actually.
>>
>> So we introduce a new mount option "strict_fsync" to control the
>> policy of fsync. It's set by default, and means that fsync follows
>> POSIX semantics. And "nostrict_fsync" means that the behaviour is
>> in line with xfs, ext4 and btrfs, where generic/342 will pass.
> 
> How about adding "fsync=%s" to give another chance for fsync policies?
> 

OK, I'll give patch v3 to change to "fsync=%s" format.
BTW, which policy do u think should be the default behavior for f2fs? Posix
or ext4?

Thanks
Junling

> Thanks,
> 
>>
>> Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
>> Reviewed-by: Chao Yu <yuch...@huawei.com>
>> ---
>>  Documentation/filesystems/f2fs.txt |  4 
>>  fs/f2fs/dir.c  |  3 ++-
>>  fs/f2fs/f2fs.h |  1 +
>>  fs/f2fs/file.c |  3 ++-
>>  fs/f2fs/namei.c|  9 ++---
>>  fs/f2fs/super.c| 13 +
>>  6 files changed, 28 insertions(+), 5 deletions(-)
>>


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] dump.f2fs: correct the seg type in ssa_dump

2018-02-06 Thread Junling Zheng
Fix the mixed using of "ret" in ssa_dump.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/dump.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fsck/dump.c b/fsck/dump.c
index 8772a62..23e4f47 100644
--- a/fsck/dump.c
+++ b/fsck/dump.c
@@ -195,7 +195,7 @@ void ssa_dump(struct f2fs_sb_info *sbi, int start_ssa, int 
end_ssa)
 {
struct f2fs_summary_block *sum_blk;
char buf[BUF_SZ];
-   int segno, i, ret;
+   int segno, type, i, ret;
int fd;
 
fd = open("dump_ssa", O_CREAT|O_WRONLY|O_TRUNC, 0666);
@@ -208,10 +208,10 @@ void ssa_dump(struct f2fs_sb_info *sbi, int start_ssa, 
int end_ssa)
ASSERT(ret >= 0);
 
for (segno = start_ssa; segno < end_ssa; segno++) {
-   sum_blk = get_sum_block(sbi, segno, );
+   sum_blk = get_sum_block(sbi, segno, );
 
memset(buf, 0, BUF_SZ);
-   switch (ret) {
+   switch (type) {
case SEG_TYPE_CUR_NODE:
snprintf(buf, BUF_SZ, "\n\nsegno: %x, Current Node\n", 
segno);
break;
@@ -240,8 +240,8 @@ void ssa_dump(struct f2fs_sb_info *sbi, int start_ssa, int 
end_ssa)
ret = write(fd, buf, strlen(buf));
ASSERT(ret >= 0);
}
-   if (ret == SEG_TYPE_NODE || ret == SEG_TYPE_DATA ||
-   ret == SEG_TYPE_MAX)
+   if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
+   type == SEG_TYPE_MAX)
free(sum_blk);
}
close(fd);
-- 
2.15.1


--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH v2] f2fs: introduce "strict_fsync" for posix standard fsync

2018-02-02 Thread Junling Zheng
Commit "0a007b97aad6"(f2fs: recover directory operations by fsync)
fixed xfstest generic/342 case, but it also increased the written
data and caused the performance degradation. In most cases, there's
no need to do so heavily fsync actually.

So we introduce a new mount option "strict_fsync" to control the
policy of fsync. It's set by default, and means that fsync follows
POSIX semantics. And "nostrict_fsync" means that the behaviour is
in line with xfs, ext4 and btrfs, where generic/342 will pass.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
Reviewed-by: Chao Yu <yuch...@huawei.com>
---
 Documentation/filesystems/f2fs.txt |  4 
 fs/f2fs/dir.c  |  3 ++-
 fs/f2fs/f2fs.h |  1 +
 fs/f2fs/file.c |  3 ++-
 fs/f2fs/namei.c|  9 ++---
 fs/f2fs/super.c| 13 +
 6 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/Documentation/filesystems/f2fs.txt 
b/Documentation/filesystems/f2fs.txt
index 0caf7da0a532..c484ce8d1f4c 100644
--- a/Documentation/filesystems/f2fs.txt
+++ b/Documentation/filesystems/f2fs.txt
@@ -180,6 +180,10 @@ whint_mode=%s  Control which write hints are 
passed down to block
down hints. In "user-based" mode, f2fs tries to pass
down hints given by users. And in "fs-based" mode, f2fs
passes down hints with its policy.
+{,no}strict_fsync  Control the policy of fsync. Set "strict_fsync" by 
default,
+   which means that fsync will follow POSIX semantics. Use
+   "nostrict_fsync" if you expect fsync to behave in line 
with
+   xfs, ext4 and btrfs, where xfstest generic/342 will 
pass.
 
 

 DEBUGFS ENTRIES
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index f00b5ed8c011..7487b7e77a36 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -713,7 +713,8 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, 
struct page *page,
 
f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
 
-   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
+   if (!test_opt(F2FS_I_SB(dir), STRICT_FSYNC))
+   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
 
if (f2fs_has_inline_dentry(dir))
return f2fs_delete_inline_entry(dentry, page, dir, inode);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index dbe87c7a266e..8cf914d12f17 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -97,6 +97,7 @@ extern char *fault_name[FAULT_MAX];
 #define F2FS_MOUNT_QUOTA   0x0040
 #define F2FS_MOUNT_INLINE_XATTR_SIZE   0x0080
 #define F2FS_MOUNT_RESERVE_ROOT0x0100
+#define F2FS_MOUNT_STRICT_FSYNC0x0200
 
 #define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
 #define set_opt(sbi, option)   ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 672a542e5464..9b39254f5b48 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -165,7 +165,8 @@ static inline enum cp_reason_type need_do_checkpoint(struct 
inode *inode)
cp_reason = CP_FASTBOOT_MODE;
else if (sbi->active_logs == 2)
cp_reason = CP_SPEC_LOG_NUM;
-   else if (need_dentry_mark(sbi, inode->i_ino) &&
+   else if (!test_opt(sbi, STRICT_FSYNC) &&
+   need_dentry_mark(sbi, inode->i_ino) &&
exist_written_data(sbi, F2FS_I(inode)->i_pino, TRANS_DIR_INO))
cp_reason = CP_RECOVER_DIR;
 
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index c4c94c7e9f4f..ef86ae327f91 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -936,7 +936,8 @@ static int f2fs_rename(struct inode *old_dir, struct dentry 
*old_dentry,
}
f2fs_i_links_write(old_dir, false);
}
-   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   if (!test_opt(sbi, STRICT_FSYNC))
+   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
 
f2fs_unlock_op(sbi);
 
@@ -1091,8 +1092,10 @@ static int f2fs_cross_rename(struct inode *old_dir, 
struct dentry *old_dentry,
}
f2fs_mark_inode_dirty_sync(new_dir, false);
 
-   add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
-   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   if (!test_opt(sbi, STRICT_FSYNC)) {
+   add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
+   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   }
 
f2fs_unlock_op(sbi);
 
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 7966cf7bfb8e..3066fc9d8985 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -130,6 +13

[f2fs-dev] [PATCH] f2fs: introduce "strict_fsync" for posix standard fsync

2018-02-01 Thread Junling Zheng
Commit "0a007b97aad6"(f2fs: recover directory operations by fsync)
fixed xfstest generic/342 case, but it also increased the written
data and caused the performance degradation. In most cases, there's
no need to do so heavily fsync actually.

So we introduce a new mount option "strict_fsync" to control the
policy of fsync. It's set by default, and means that fsync follows
POSIX semantics. And "nostrict_fsync" means that the behaviour is
in line with xfs, ext4 and btrfs, on which generic/342 will be passed.

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fs/f2fs/dir.c   |  3 ++-
 fs/f2fs/f2fs.h  |  1 +
 fs/f2fs/file.c  |  3 ++-
 fs/f2fs/namei.c |  9 ++---
 fs/f2fs/super.c | 13 +
 5 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index f00b5ed8c011..7487b7e77a36 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -713,7 +713,8 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, 
struct page *page,
 
f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
 
-   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
+   if (!test_opt(F2FS_I_SB(dir), STRICT_FSYNC))
+   add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
 
if (f2fs_has_inline_dentry(dir))
return f2fs_delete_inline_entry(dentry, page, dir, inode);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index dbe87c7a266e..8cf914d12f17 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -97,6 +97,7 @@ extern char *fault_name[FAULT_MAX];
 #define F2FS_MOUNT_QUOTA   0x0040
 #define F2FS_MOUNT_INLINE_XATTR_SIZE   0x0080
 #define F2FS_MOUNT_RESERVE_ROOT0x0100
+#define F2FS_MOUNT_STRICT_FSYNC0x0200
 
 #define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
 #define set_opt(sbi, option)   ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 672a542e5464..9b39254f5b48 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -165,7 +165,8 @@ static inline enum cp_reason_type need_do_checkpoint(struct 
inode *inode)
cp_reason = CP_FASTBOOT_MODE;
else if (sbi->active_logs == 2)
cp_reason = CP_SPEC_LOG_NUM;
-   else if (need_dentry_mark(sbi, inode->i_ino) &&
+   else if (!test_opt(sbi, STRICT_FSYNC) &&
+   need_dentry_mark(sbi, inode->i_ino) &&
exist_written_data(sbi, F2FS_I(inode)->i_pino, TRANS_DIR_INO))
cp_reason = CP_RECOVER_DIR;
 
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index c4c94c7e9f4f..ef86ae327f91 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -936,7 +936,8 @@ static int f2fs_rename(struct inode *old_dir, struct dentry 
*old_dentry,
}
f2fs_i_links_write(old_dir, false);
}
-   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   if (!test_opt(sbi, STRICT_FSYNC))
+   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
 
f2fs_unlock_op(sbi);
 
@@ -1091,8 +1092,10 @@ static int f2fs_cross_rename(struct inode *old_dir, 
struct dentry *old_dentry,
}
f2fs_mark_inode_dirty_sync(new_dir, false);
 
-   add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
-   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   if (!test_opt(sbi, STRICT_FSYNC)) {
+   add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
+   add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+   }
 
f2fs_unlock_op(sbi);
 
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 7966cf7bfb8e..3066fc9d8985 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -130,6 +130,8 @@ enum {
Opt_jqfmt_vfsv0,
Opt_jqfmt_vfsv1,
Opt_whint,
+   Opt_strict_fsync,
+   Opt_nostrict_fsync,
Opt_err,
 };
 
@@ -184,6 +186,8 @@ static match_table_t f2fs_tokens = {
{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
{Opt_whint, "whint_mode=%s"},
+   {Opt_strict_fsync, "strict_fsync"},
+   {Opt_nostrict_fsync, "nostrict_fsync"},
{Opt_err, NULL},
 };
 
@@ -700,6 +704,12 @@ static int parse_options(struct super_block *sb, char 
*options)
}
kfree(name);
break;
+   case Opt_strict_fsync:
+   set_opt(sbi, STRICT_FSYNC);
+   break;
+   case Opt_nostrict_fsync:
+   clear_opt(sbi, STRICT_FSYNC);
+   break;
default:
f2fs_msg(sb, KERN_ERR,
"Unrecognized mount option \"%s\" or missing 
value",
@@ -1296,6 +1306,9 @@ static void default_options(s

[f2fs-dev] [PATCH v2] resize.f2fs: fix the number of moved ssa blocks in migrate_ssa

2016-11-29 Thread Junling Zheng
If the offset passed in migrate_ssa is not zero, it means that there're
offset segments of old main will disappear after migrating, then there're
offset blocks of old ssa should be invalidated and removed accordingly.
So, the number of moved ssa blocks should be: TOTAL_SEGS(sbi) - offset,
and the expanded summary, which is filled with zero_blocks, should start
from: new_sum_blkaddr + TOTAL_SEGS(sbi) - offset.

Signed-off-by: Yunlei He <heyun...@huawei.com>
Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/resize.c | 25 ++---
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/fsck/resize.c b/fsck/resize.c
index ba7bb88..3e8fdd8 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -207,30 +207,33 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
block_t end_sum_blkaddr = get_newsb(main_blkaddr);
+   block_t expand_sum_blkaddr = new_sum_blkaddr +
+   TOTAL_SEGS(sbi) - offset;
block_t blkaddr;
+   int ret;
void *zero_block = calloc(BLOCK_SZ, 1);
-
ASSERT(zero_block);
 
if (offset && new_sum_blkaddr < old_sum_blkaddr + offset) {
blkaddr = new_sum_blkaddr;
while (blkaddr < end_sum_blkaddr) {
-   if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi))
-   move_ssa(sbi, offset, blkaddr);
-   else
-   dev_write_block(zero_block, blkaddr);
-   offset++;
-   blkaddr++;
+   if (blkaddr < expand_sum_blkaddr)
+   move_ssa(sbi, offset++, blkaddr++);
+   else {
+   ret = dev_write_block(zero_block, blkaddr++);
+   ASSERT(ret >=0);
+   }
}
} else {
blkaddr = end_sum_blkaddr - 1;
offset = TOTAL_SEGS(sbi) - 1;
while (blkaddr >= new_sum_blkaddr) {
-   if (blkaddr >= TOTAL_SEGS(sbi) + new_sum_blkaddr)
-   dev_write_block(zero_block, blkaddr);
+   if (blkaddr >= expand_sum_blkaddr) {
+   ret = dev_write_block(zero_block, blkaddr--);
+   ASSERT(ret >=0);
+   }
else
-   move_ssa(sbi, offset--, blkaddr);
-   blkaddr--;
+   move_ssa(sbi, offset--, blkaddr--);
}
}
 
-- 
2.7.4


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


Re: [f2fs-dev] [PATCH] resize.f2fs: fix an error in migrate_ssa

2016-11-29 Thread Junling Zheng
Ping ...

On 2016/11/25 11:53, Junling Zheng wrote:
> Sorry, I forget to get the return value of dev_write_block :(
> Please review the following patch :)
> 
> ---
>  fsck/resize.c | 25 ++---
>  1 file changed, 14 insertions(+), 11 deletions(-)
> 
> diff --git a/fsck/resize.c b/fsck/resize.c
> index 46aa30e..9f9c7a6 100644
> --- a/fsck/resize.c
> +++ b/fsck/resize.c
> @@ -207,30 +207,33 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
>   block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
>   block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
>   block_t end_sum_blkaddr = get_newsb(main_blkaddr);
> + block_t expand_sum_blkaddr = new_sum_blkaddr +
> + TOTAL_SEGS(sbi) - offset;
>   block_t blkaddr;
> + int ret;
>   void *zero_block = calloc(BLOCK_SZ, 1);
> -
>   ASSERT(zero_block);
> 
>   if (offset && new_sum_blkaddr < old_sum_blkaddr + offset) {
>   blkaddr = new_sum_blkaddr;
>   while (blkaddr < end_sum_blkaddr) {
> - if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi))
> - move_ssa(sbi, offset, blkaddr);
> - else
> - dev_write_block(zero_block, blkaddr);
> - offset++;
> - blkaddr++;
> + if (blkaddr < expand_sum_blkaddr)
> + move_ssa(sbi, offset++, blkaddr++);
> + else {
> + ret = dev_write_block(zero_block, blkaddr++);
> + ASSERT(ret >=0);
> + }
>   }
>   } else {
>   blkaddr = end_sum_blkaddr - 1;
>   offset = TOTAL_SEGS(sbi) - 1;
>   while (blkaddr >= new_sum_blkaddr) {
> - if (blkaddr >= TOTAL_SEGS(sbi) + new_sum_blkaddr)
> - dev_write_block(zero_block, blkaddr);
> + if (blkaddr >= expand_sum_blkaddr) {
> + ret = dev_write_block(zero_block, blkaddr--);
> + ASSERT(ret >=0);
> + }
>   else
> - move_ssa(sbi, offset--, blkaddr);
> - blkaddr--;
> + move_ssa(sbi, offset--, blkaddr--);
>   }
>   }
> 
> 
> On 2016/11/25 11:32, Junling Zheng wrote:
>> How about the following patch, which I think would be a little better :)
>>
>> diff --git a/fsck/resize.c b/fsck/resize.c
>> index 46aa30e..c295a06 100644
>> --- a/fsck/resize.c
>> +++ b/fsck/resize.c
>> @@ -207,30 +207,33 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
>>  block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
>>  block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
>>  block_t end_sum_blkaddr = get_newsb(main_blkaddr);
>> +block_t expand_sum_blkaddr = new_sum_blkaddr +
>> +TOTAL_SEGS(sbi) - offset;
>>  block_t blkaddr;
>> +int ret;
>>  void *zero_block = calloc(BLOCK_SZ, 1);
>> -
>>  ASSERT(zero_block);
>>
>>  if (offset && new_sum_blkaddr < old_sum_blkaddr + offset) {
>>  blkaddr = new_sum_blkaddr;
>>  while (blkaddr < end_sum_blkaddr) {
>> -if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi))
>> -move_ssa(sbi, offset, blkaddr);
>> -else
>> -dev_write_block(zero_block, blkaddr);
>> -offset++;
>> -blkaddr++;
>> +if (blkaddr < expand_sum_blkaddr)
>> +move_ssa(sbi, offset++, blkaddr++);
>> +else {
>> +dev_write_block(zero_block, blkaddr++);
>> +ASSERT(ret >=0);
> 
> forget to get the return value of dev_write_block :(
> 
>> +}
>>  }
>>  } else {
>>  blkaddr = end_sum_blkaddr - 1;
>>  offset = TOTAL_SEGS(sbi) - 1;
>>  while (blkaddr >= new_sum_blkaddr) {
>> -if (blkaddr >= TOTAL_SEGS(sbi) + new_sum_blkaddr)
>> -dev_write_block(zero_block, blkaddr);
>> +if (blkaddr >= expand_sum_blkaddr) {
>> +dev_write_block(zero_block, blkaddr--);
>> +   

[f2fs-dev] [PATCH] resize.f2fs: correct the max segno in migrate_main

2016-11-27 Thread Junling Zheng
Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/resize.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fsck/resize.c b/fsck/resize.c
index 46aa30e..ba7bb88 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -143,7 +143,7 @@ static void migrate_main(struct f2fs_sb_info *sbi,
 
ASSERT(raw != NULL);
 
-   for (i = TOTAL_SEGS(sbi); i >= 0; i--) {
+   for (i = TOTAL_SEGS(sbi) - 1; i >= 0; i--) {
se = get_seg_entry(sbi, i);
if (!se->valid_blocks)
continue;
-- 
2.7.4


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


Re: [f2fs-dev] [PATCH] resize.f2fs: fix an error in migrate_ssa

2016-11-24 Thread Junling Zheng
Sorry, I forget to get the return value of dev_write_block :(
Please review the following patch :)

---
 fsck/resize.c | 25 ++---
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/fsck/resize.c b/fsck/resize.c
index 46aa30e..9f9c7a6 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -207,30 +207,33 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
block_t end_sum_blkaddr = get_newsb(main_blkaddr);
+   block_t expand_sum_blkaddr = new_sum_blkaddr +
+   TOTAL_SEGS(sbi) - offset;
block_t blkaddr;
+   int ret;
void *zero_block = calloc(BLOCK_SZ, 1);
-
ASSERT(zero_block);

if (offset && new_sum_blkaddr < old_sum_blkaddr + offset) {
blkaddr = new_sum_blkaddr;
while (blkaddr < end_sum_blkaddr) {
-   if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi))
-   move_ssa(sbi, offset, blkaddr);
-   else
-   dev_write_block(zero_block, blkaddr);
-   offset++;
-   blkaddr++;
+   if (blkaddr < expand_sum_blkaddr)
+   move_ssa(sbi, offset++, blkaddr++);
+   else {
+   ret = dev_write_block(zero_block, blkaddr++);
+   ASSERT(ret >=0);
+   }
}
} else {
blkaddr = end_sum_blkaddr - 1;
offset = TOTAL_SEGS(sbi) - 1;
while (blkaddr >= new_sum_blkaddr) {
-   if (blkaddr >= TOTAL_SEGS(sbi) + new_sum_blkaddr)
-   dev_write_block(zero_block, blkaddr);
+   if (blkaddr >= expand_sum_blkaddr) {
+   ret = dev_write_block(zero_block, blkaddr--);
+   ASSERT(ret >=0);
+   }
else
-   move_ssa(sbi, offset--, blkaddr);
-   blkaddr--;
+   move_ssa(sbi, offset--, blkaddr--);
}
    }


On 2016/11/25 11:32, Junling Zheng wrote:
> How about the following patch, which I think would be a little better :)
> 
> diff --git a/fsck/resize.c b/fsck/resize.c
> index 46aa30e..c295a06 100644
> --- a/fsck/resize.c
> +++ b/fsck/resize.c
> @@ -207,30 +207,33 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
>   block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
>   block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
>   block_t end_sum_blkaddr = get_newsb(main_blkaddr);
> + block_t expand_sum_blkaddr = new_sum_blkaddr +
> + TOTAL_SEGS(sbi) - offset;
>   block_t blkaddr;
> + int ret;
>   void *zero_block = calloc(BLOCK_SZ, 1);
> -
>   ASSERT(zero_block);
> 
>   if (offset && new_sum_blkaddr < old_sum_blkaddr + offset) {
>   blkaddr = new_sum_blkaddr;
>   while (blkaddr < end_sum_blkaddr) {
> - if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi))
> - move_ssa(sbi, offset, blkaddr);
> - else
> - dev_write_block(zero_block, blkaddr);
> - offset++;
> - blkaddr++;
> + if (blkaddr < expand_sum_blkaddr)
> + move_ssa(sbi, offset++, blkaddr++);
> + else {
> + dev_write_block(zero_block, blkaddr++);
> + ASSERT(ret >=0);

forget to get the return value of dev_write_block :(

> + }
>   }
>   } else {
>   blkaddr = end_sum_blkaddr - 1;
>   offset = TOTAL_SEGS(sbi) - 1;
>   while (blkaddr >= new_sum_blkaddr) {
> - if (blkaddr >= TOTAL_SEGS(sbi) + new_sum_blkaddr)
> - dev_write_block(zero_block, blkaddr);
> + if (blkaddr >= expand_sum_blkaddr) {
> + dev_write_block(zero_block, blkaddr--);
> + ASSERT(ret >=0);
> + }
>   else
> - move_ssa(sbi, offset--, blkaddr);
> - blkaddr--;
> + move_ssa(sbi, offset--, blkaddr--);
>   }
>   }
> 
> 
> On 2016/11/24 15:33, Yunlei He wrote:
>> This patch fix an error in migrate_ssa when res

Re: [f2fs-dev] [PATCH] resize.f2fs: fix an error in migrate_ssa

2016-11-24 Thread Junling Zheng
How about the following patch, which I think would be a little better :)

diff --git a/fsck/resize.c b/fsck/resize.c
index 46aa30e..c295a06 100644
--- a/fsck/resize.c
+++ b/fsck/resize.c
@@ -207,30 +207,33 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
block_t end_sum_blkaddr = get_newsb(main_blkaddr);
+   block_t expand_sum_blkaddr = new_sum_blkaddr +
+   TOTAL_SEGS(sbi) - offset;
block_t blkaddr;
+   int ret;
void *zero_block = calloc(BLOCK_SZ, 1);
-
ASSERT(zero_block);

if (offset && new_sum_blkaddr < old_sum_blkaddr + offset) {
blkaddr = new_sum_blkaddr;
while (blkaddr < end_sum_blkaddr) {
-   if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi))
-   move_ssa(sbi, offset, blkaddr);
-   else
-   dev_write_block(zero_block, blkaddr);
-   offset++;
-   blkaddr++;
+   if (blkaddr < expand_sum_blkaddr)
+   move_ssa(sbi, offset++, blkaddr++);
+   else {
+   dev_write_block(zero_block, blkaddr++);
+   ASSERT(ret >=0);
+   }
}
} else {
blkaddr = end_sum_blkaddr - 1;
offset = TOTAL_SEGS(sbi) - 1;
while (blkaddr >= new_sum_blkaddr) {
-   if (blkaddr >= TOTAL_SEGS(sbi) + new_sum_blkaddr)
-   dev_write_block(zero_block, blkaddr);
+   if (blkaddr >= expand_sum_blkaddr) {
+   dev_write_block(zero_block, blkaddr--);
+   ASSERT(ret >=0);
+   }
else
-   move_ssa(sbi, offset--, blkaddr);
-   blkaddr--;
+   move_ssa(sbi, offset--, blkaddr--);
}
}


On 2016/11/24 15:33, Yunlei He wrote:
> This patch fix an error in migrate_ssa when resize with condition
> that offset is not zero && new_sum_blkaddr > old_sum_blkaddr + offset
> 
> Signed-off-by: Yunlei He 
> ---
>  fsck/resize.c | 37 +++--
>  1 file changed, 27 insertions(+), 10 deletions(-)
> 
> diff --git a/fsck/resize.c b/fsck/resize.c
> index 46aa30e..70dbef5 100644
> --- a/fsck/resize.c
> +++ b/fsck/resize.c
> @@ -208,28 +208,45 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
>   block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
>   block_t end_sum_blkaddr = get_newsb(main_blkaddr);
>   block_t blkaddr;
> + unsigned int offset1 = offset;
> + int ret = 1;
>   void *zero_block = calloc(BLOCK_SZ, 1);
>  
>   ASSERT(zero_block);
>  
> - if (offset && new_sum_blkaddr < old_sum_blkaddr + offset) {
> - blkaddr = new_sum_blkaddr;
> - while (blkaddr < end_sum_blkaddr) {
> - if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi))
> - move_ssa(sbi, offset, blkaddr);
> - else
> - dev_write_block(zero_block, blkaddr);
> - offset++;
> - blkaddr++;
> + if (offset) {
> + if (new_sum_blkaddr < old_sum_blkaddr + offset) {
> + blkaddr = new_sum_blkaddr;
> + while (blkaddr < end_sum_blkaddr) {
> + if (blkaddr - new_sum_blkaddr < TOTAL_SEGS(sbi) 
> - offset1)
> + move_ssa(sbi, offset, blkaddr);
> + else
> + ret = dev_write_block(zero_block, 
> blkaddr);
> + ASSERT(ret >= 0);
> + offset++;
> + blkaddr++;
> + }
> + } else {
> + blkaddr = end_sum_blkaddr - 1;
> + offset = TOTAL_SEGS(sbi)-1;
> + while (blkaddr >= new_sum_blkaddr) {
> + if (blkaddr >= TOTAL_SEGS(sbi) - offset1 + 
> new_sum_blkaddr)
> + ret = dev_write_block(zero_block, 
> blkaddr);
> + else
> + move_ssa(sbi, offset--, blkaddr);
> + ASSERT(ret >= 0);
> + blkaddr--;
> + }
>   }
>   } else {
>   blkaddr = end_sum_blkaddr - 1;
>   offset = TOTAL_SEGS(sbi) - 1;
>   while (blkaddr >= new_sum_blkaddr) {
>   if (blkaddr >= TOTAL_SEGS(sbi) 

[f2fs-dev] [PATCH] fsck: refactor build_nat_area_bitmap to speed up fsck

2016-11-07 Thread Junling Zheng
Refactor build_nat_area_bitmap, move out lookup_nat_in_journal from the
dual loops. Instead of looking up all nids in journals, we traverse nat
journals, whose entries are used to replace the NAT entries with the
corresponding nid to reduce the time build_nat_area_bitmap costs.

For "fsck -p 1", it reduces over half of the time.

Empty   Fragmented Entirely
32G(ms):
  original  625.8   727.3
  improved  226.3   298.2
64G(ms):
  original  11591637.7
  improved  515.8   676.7

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/mount.c | 118 ---
 1 file changed, 64 insertions(+), 54 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 21a96a7..9fcb008 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -1792,10 +1792,13 @@ void write_checkpoint(struct f2fs_sb_info *sbi)
 
 void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
 {
+   struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
+   struct f2fs_journal *journal = >sum_blk->journal;
struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
struct f2fs_nm_info *nm_i = NM_I(sbi);
struct f2fs_nat_block *nat_block;
+   struct node_info ni;
u32 nid, nr_nat_blks;
pgoff_t block_off;
pgoff_t block_addr;
@@ -1834,8 +1837,6 @@ void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
 
nid = block_off * NAT_ENTRY_PER_BLOCK;
for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
-   struct f2fs_nat_entry raw_nat;
-   struct node_info ni;
ni.nid = nid + i;
 
if ((nid + i) == F2FS_NODE_INO(sbi) ||
@@ -1851,63 +1852,72 @@ void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
continue;
}
 
-   if (lookup_nat_in_journal(sbi, nid + i,
-   _nat) >= 0) {
-   node_info_from_raw_nat(, _nat);
-   if ((ni.ino == 0x0 && ni.blk_addr != 0x0))
-   ASSERT_MSG("\tError: ino[0x%8x] or 
blk_addr[0x%16x]"
-   " is invalid\n",
-   ni.ino, ni.blk_addr);
-   if (ni.ino == (nid + i) && ni.blk_addr != 0) {
-   fsck->nat_valid_inode_cnt++;
-   DBG(3, "ino[0x%8x] maybe is inode\n",
-   ni.ino);
-   }
-   if (ni.blk_addr != 0x0) {
-   f2fs_set_bit(nid + i,
-   fsck->nat_area_bitmap);
-   fsck->chk.valid_nat_entry_cnt++;
-   DBG(3, "nid[0x%x] in nat cache\n",
-   nid + i);
-   }
+   node_info_from_raw_nat(, _block->entries[i]);
+   if (ni.blk_addr == 0x0)
+   continue;
+   if (ni.ino == 0x0) {
+   ASSERT_MSG("\tError: ino[0x%8x] or 
blk_addr[0x%16x]"
+   " is invalid\n", ni.ino, ni.blk_addr);
+   }
+   if (ni.ino == (nid + i)) {
+   fsck->nat_valid_inode_cnt++;
+   DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino);
+   }
+   if (nid + i == 0) {
+   /*
+* nat entry [0] must be null.  If
+* it is corrupted, set its bit in
+* nat_area_bitmap, fsck_verify will
+* nullify it
+*/
+   ASSERT_MSG("Invalid nat entry[0]: "
+   "blk_addr[0x%x]\n", ni.blk_addr);
+   c.fix_on = 1;
+   fsck->chk.valid_nat_entry_cnt--;
+   }
 
-   fsck->entries[nid + i] = raw_nat;
-   } else {
-   node_info_from_raw_nat(,
-   _block->entries[i]);
-   if ((ni.ino == 0x0 && ni.blk_addr

[f2fs-dev] [PATCH 3/4] fsck.f2fs: fix a typo in check_sector_size

2016-09-29 Thread Junling Zheng
Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/mount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index a247dec..5f51009 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -1908,7 +1908,7 @@ static int check_sector_size(struct f2fs_super_block *sb)
DBG(1, "\tWriting super block, at offset 0x%08x\n", 0);
for (index = 0; index < 2; index++) {
if (dev_write(zero_buff, index * F2FS_BLKSIZE, F2FS_BLKSIZE)) {
-   MSG(1, "\tError: While while writing supe_blk "
+   MSG(1, "\tError: Failed while writing supe_blk "
"on disk!!! index : %d\n", index);
free(zero_buff);
return -1;
-- 
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 2/4] fsck.f2fs: fix incorrect ERR_MSG in f2fs_do_mount

2016-09-29 Thread Junling Zheng
Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/mount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index e390b26..a247dec 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -1978,7 +1978,7 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi)
}
 
if (build_node_manager(sbi)) {
-   ERR_MSG("build_segment_manager failed\n");
+   ERR_MSG("build_node_manager failed\n");
return -1;
}
 
-- 
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] fsck.f2fs: remove duplicated codes in sanity_check_raw_super

2016-04-25 Thread Junling Zheng
Remove duplicated codes in sanity_check_raw_super().

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/mount.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 4bde179..f866c4f 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -376,10 +376,6 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, 
u64 offset)
if (get_sb(log_blocks_per_seg) != 9)
return -1;
 
-   if (get_sb(log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE ||
-   get_sb(log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE)
-   return -1;
-
/* Currently, support 512/1024/2048/4096 bytes sector size */
if (get_sb(log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE ||
get_sb(log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE)
-- 
1.9.1


--
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] fsck.f2fs: calculate orphan_blkaddr correctly

2016-03-16 Thread Junling Zheng
Commit 4ea4f1db ("fsck.f2fs: large volume support") added the support
for large volume over about 3TB. The "cp_payload" is added to start_blk,
but not removed from orphan_blkaddr.

Fixes: 4ea4f1db ("fsck.f2fs: large volume support")
Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 fsck/fsck.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index 6f47c5d..624adae 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -1426,7 +1426,7 @@ void fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
return;
 
start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
-   orphan_blkaddr = __start_sum_addr(sbi) - 1;
+   orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
 
orphan_blk = calloc(BLOCK_SZ, 1);
ASSERT(orphan_blk);
-- 
1.9.1


--
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231=/4140
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH v3] mkfs.f2fs: set segment_count in super block correctly

2016-03-15 Thread Junling Zheng
From: Fan Li <fanofcode...@samsung.com>

Now f2fs will check statistics recorded in super block in
sanity_check_area_boundary() during mount. If the number of segments
per zone is greater than 1, and the disk space isn't aligned with zone,
mount will fail due to following condition:

main_blkaddr + (segment_count_main << log_blocks_per_seg) !=
segment0_blkaddr + (segment_count << log_blocks_per_seg)

This is because when the length of main area isn't aligned with zone,
mkfs doesn't add those excess segments to segment_count_main, but adds
them to segment_count.

Here align segment_count with zone size as well as segment_count_main
to prevent such problem.

Signed-off-by: Fan Li <fanofcode...@samsung.com>
Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
---
 mkfs/f2fs_format.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index 4f38f03..960031e 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -175,7 +175,8 @@ static int f2fs_prepare_super_block(void)
}
 
set_sb(segment_count, (config.total_sectors * config.sector_size -
-   zone_align_start_offset) / segment_size_bytes);
+   zone_align_start_offset) / segment_size_bytes /
+   config.segs_per_zone * config.segs_per_zone);
 
set_sb(segment0_blkaddr, zone_align_start_offset / blk_size_bytes);
sb->cp_blkaddr = sb->segment0_blkaddr;
-- 
1.9.1


--
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231=/4140
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH] fsck.f2fs: fix incorrect block_addr of node/meta

2016-03-02 Thread Junling Zheng
Fix incorrect block_addr of node or meta inode into 0x1 during
build_nat_area_bitmap().

Signed-off-by: Junling Zheng <zhengjunl...@huawei.com>
Signed-off-by: Liu Xue <liuxueliu@huawei.com>
Signed-off-by: Sheng Yong <shengyo...@huawei.com>
---
 fsck/mount.c | 9 -
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 4c807f9..9360656 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -1664,7 +1664,14 @@ void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
 
if ((nid + i) == F2FS_NODE_INO(sbi) ||
(nid + i) == F2FS_META_INO(sbi)) {
-   ASSERT(nat_block->entries[i].block_addr != 0x0);
+   /* block_addr of node/meta inode should be 0x1 
*/
+   if 
(le32_to_cpu(nat_block->entries[i].block_addr) != 0x1) {
+   FIX_MSG("ino: 0x%x node/meta inode, 
block_addr= 0x%x -> 0x1",
+   nid + i, 
le32_to_cpu(nat_block->entries[i].block_addr));
+   nat_block->entries[i].block_addr = 
cpu_to_le32(0x1);
+   ret = dev_write_block(nat_block, 
block_addr);
+   ASSERT(ret >= 0);
+   }
continue;
}
 
-- 
1.9.1


--
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151=/4140
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


  1   2   >