[f2fs-dev] [PATCH v2] dump.f2fs: only dump nat inside the specified nid range
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, "nid:%5u\tino:%5u\toffset:%5u" @@ -114,6 +86,26 @@ void nat_dump(struct f2fs_sb_info *sbi) ret =
Re: [f2fs-dev] [PATCH] dump.f2fs: only dump nat inside the specified nid range
On 2018/7/2 11:25, Junling Zheng wrote: > 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", > - 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) { >
Re: [f2fs-dev] [PATCH] dump.f2fs: only dump nat inside the specified nid range
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", - 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,
Re: [f2fs-dev] [PATCH] f2fs: check the right return value of memory alloc function
On 2018/7/2 10:40, Yunlei He wrote: > This patch check the right return value of memory alloc function > > Signed-off-by: Yunlei He Reviewed-by: Chao Yu Thanks, -- 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] f2fs: Replace strncpy with memcpy
On 2018/7/2 10:16, Guenter Roeck wrote: > On 07/01/2018 06:53 PM, Chao Yu wrote: >> On 2018/7/2 4:57, Guenter Roeck wrote: >>> gcc 8.1.0 complains: >>> >>> fs/f2fs/namei.c: In function 'f2fs_update_extension_list': >>> fs/f2fs/namei.c:257:3: warning: >>> 'strncpy' output truncated before terminating nul copying >>> as many bytes from a string as its length >>> fs/f2fs/namei.c:249:3: warning: >>> 'strncpy' output truncated before terminating nul copying >>> as many bytes from a string as its length >>> >>> Using strncpy() is indeed less than perfect since the length of data to >>> be copied has already been determined with strlen(). Replace strncpy() >>> with memcpy() to address the warning and optimize the code a little. >>> >>> Signed-off-by: Guenter Roeck >>> --- >>> fs/f2fs/namei.c | 4 ++-- >>> 1 file changed, 2 insertions(+), 2 deletions(-) >>> >>> diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c >>> index 231b7f3ea7d3..e75607544f7c 100644 >>> --- a/fs/f2fs/namei.c >>> +++ b/fs/f2fs/namei.c >>> @@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info >>> *sbi, const char *name, >>> return -EINVAL; >>> >>> if (hot) { >>> - strncpy(extlist[count], name, strlen(name)); >>> + memcpy(extlist[count], name, strlen(name)); >> >> How about replacing with strcpy(extlist[count], name)? Because name length >> has >> already been checked before f2fs_update_extension_list, it should be valid, >> and >> will not cause overflow during copying. >> > > Your call; feel free to submit an alternative. Since it is different files, > static > analysis might not know and complain, though. You might want to make sure > that this > doesn't happen, and also add a comment explaining the reason for using > strcpy(). Yeah, that could be changed in another patch, but it will be trivial. Anyway, to fix this gcc complaint, this patch looks good to me, thanks for the patch. :) Reviewed-by: Chao Yu Thanks, > > Thanks, > Guenter > > -- 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: check the right return value of memory alloc function
This patch check the right return value of memory alloc function Signed-off-by: Yunlei He --- fs/f2fs/node.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c index 1af61e0..24b5262 100644 --- a/fs/f2fs/node.c +++ b/fs/f2fs/node.c @@ -2794,7 +2794,7 @@ static int init_free_nid_cache(struct f2fs_sb_info *sbi) for (i = 0; i < nm_i->nat_blocks; i++) { nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi, NAT_ENTRY_BITMAP_SIZE_ALIGNED, GFP_KERNEL); - if (!nm_i->free_nid_bitmap) + if (!nm_i->free_nid_bitmap[i]) return -ENOMEM; } -- 1.9.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
Re: [f2fs-dev] [PATCH] dump.f2fs: only dump nat inside the specified nid range
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", >>> - 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
Re: [f2fs-dev] [PATCH] f2fs: Replace strncpy with memcpy
On 07/01/2018 06:53 PM, Chao Yu wrote: On 2018/7/2 4:57, Guenter Roeck wrote: gcc 8.1.0 complains: fs/f2fs/namei.c: In function 'f2fs_update_extension_list': fs/f2fs/namei.c:257:3: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length fs/f2fs/namei.c:249:3: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length Using strncpy() is indeed less than perfect since the length of data to be copied has already been determined with strlen(). Replace strncpy() with memcpy() to address the warning and optimize the code a little. Signed-off-by: Guenter Roeck --- fs/f2fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c index 231b7f3ea7d3..e75607544f7c 100644 --- a/fs/f2fs/namei.c +++ b/fs/f2fs/namei.c @@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, return -EINVAL; if (hot) { - strncpy(extlist[count], name, strlen(name)); + memcpy(extlist[count], name, strlen(name)); How about replacing with strcpy(extlist[count], name)? Because name length has already been checked before f2fs_update_extension_list, it should be valid, and will not cause overflow during copying. Your call; feel free to submit an alternative. Since it is different files, static analysis might not know and complain, though. You might want to make sure that this doesn't happen, and also add a comment explaining the reason for using strcpy(). Thanks, Guenter -- 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] dump.f2fs: only dump nat inside the specified nid range
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; >> - >> -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,
Re: [f2fs-dev] [PATCH] f2fs: Replace strncpy with memcpy
On 2018/7/2 4:57, Guenter Roeck wrote: > gcc 8.1.0 complains: > > fs/f2fs/namei.c: In function 'f2fs_update_extension_list': > fs/f2fs/namei.c:257:3: warning: > 'strncpy' output truncated before terminating nul copying > as many bytes from a string as its length > fs/f2fs/namei.c:249:3: warning: > 'strncpy' output truncated before terminating nul copying > as many bytes from a string as its length > > Using strncpy() is indeed less than perfect since the length of data to > be copied has already been determined with strlen(). Replace strncpy() > with memcpy() to address the warning and optimize the code a little. > > Signed-off-by: Guenter Roeck > --- > fs/f2fs/namei.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c > index 231b7f3ea7d3..e75607544f7c 100644 > --- a/fs/f2fs/namei.c > +++ b/fs/f2fs/namei.c > @@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, > const char *name, > return -EINVAL; > > if (hot) { > - strncpy(extlist[count], name, strlen(name)); > + memcpy(extlist[count], name, strlen(name)); How about replacing with strcpy(extlist[count], name)? Because name length has already been checked before f2fs_update_extension_list, it should be valid, and will not cause overflow during copying. Thanks, > sbi->raw_super->hot_ext_count = hot_count + 1; > } else { > char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN]; > @@ -254,7 +254,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, > const char *name, > memcpy(buf, [cold_count], > F2FS_EXTENSION_LEN * hot_count); > memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN); > - strncpy(extlist[cold_count], name, strlen(name)); > + memcpy(extlist[cold_count], name, strlen(name)); > memcpy([cold_count + 1], buf, > F2FS_EXTENSION_LEN * hot_count); > sbi->raw_super->extension_count = cpu_to_le32(cold_count + 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: Replace strncpy with memcpy
gcc 8.1.0 complains: fs/f2fs/namei.c: In function 'f2fs_update_extension_list': fs/f2fs/namei.c:257:3: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length fs/f2fs/namei.c:249:3: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length Using strncpy() is indeed less than perfect since the length of data to be copied has already been determined with strlen(). Replace strncpy() with memcpy() to address the warning and optimize the code a little. Signed-off-by: Guenter Roeck --- fs/f2fs/namei.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c index 231b7f3ea7d3..e75607544f7c 100644 --- a/fs/f2fs/namei.c +++ b/fs/f2fs/namei.c @@ -246,7 +246,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, return -EINVAL; if (hot) { - strncpy(extlist[count], name, strlen(name)); + memcpy(extlist[count], name, strlen(name)); sbi->raw_super->hot_ext_count = hot_count + 1; } else { char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN]; @@ -254,7 +254,7 @@ int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, memcpy(buf, [cold_count], F2FS_EXTENSION_LEN * hot_count); memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN); - strncpy(extlist[cold_count], name, strlen(name)); + memcpy(extlist[cold_count], name, strlen(name)); memcpy([cold_count + 1], buf, F2FS_EXTENSION_LEN * hot_count); sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1); -- 2.7.4 -- 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