diff mbox series

[f2fs-dev,v2,4/5] f2fs: add parameter @len to f2fs_invalidate_blocks()

Message ID 20241030103136.2874140-5-yi.sun@unisoc.com (mailing list archive)
State New
Headers show
Series Speed up f2fs truncate | expand

Commit Message

Yi Sun Oct. 30, 2024, 10:31 a.m. UTC
New function can process some consecutive blocks at a time.

Function f2fs_invalidate_blocks()->down_write() and up_write()
are very time-consuming, so if f2fs_invalidate_blocks() can
process consecutive blocks at one time, it will save a lot of time.

Signed-off-by: Yi Sun <yi.sun@unisoc.com>
---
 fs/f2fs/compress.c |  4 +--
 fs/f2fs/f2fs.h     |  3 +-
 fs/f2fs/file.c     |  8 +++---
 fs/f2fs/node.c     |  4 +--
 fs/f2fs/segment.c  | 69 ++++++++++++++++++++++++++++++++++++++--------
 5 files changed, 68 insertions(+), 20 deletions(-)

Comments

Daeho Jeong Oct. 30, 2024, 5 p.m. UTC | #1
On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
>
> New function can process some consecutive blocks at a time.
>
> Function f2fs_invalidate_blocks()->down_write() and up_write()
> are very time-consuming, so if f2fs_invalidate_blocks() can
> process consecutive blocks at one time, it will save a lot of time.
>
> Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> ---
>  fs/f2fs/compress.c |  4 +--
>  fs/f2fs/f2fs.h     |  3 +-
>  fs/f2fs/file.c     |  8 +++---
>  fs/f2fs/node.c     |  4 +--
>  fs/f2fs/segment.c  | 69 ++++++++++++++++++++++++++++++++++++++--------
>  5 files changed, 68 insertions(+), 20 deletions(-)
>
> diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> index e607a7885b57..02ad0ff29cf2 100644
> --- a/fs/f2fs/compress.c
> +++ b/fs/f2fs/compress.c
> @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
>                         if (blkaddr == COMPRESS_ADDR)
>                                 fio.compr_blocks++;
>                         if (__is_valid_data_blkaddr(blkaddr))
> -                               f2fs_invalidate_blocks(sbi, blkaddr);
> +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
>                         f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
>                         goto unlock_continue;
>                 }
> @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
>
>                 if (i > cc->valid_nr_cpages) {
>                         if (__is_valid_data_blkaddr(blkaddr)) {
> -                               f2fs_invalidate_blocks(sbi, blkaddr);
> +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
>                                 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
>                         }
>                         goto unlock_continue;
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index addd49af57ec..4bb459157adf 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
>  int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
>  int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
>  void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> +                                               unsigned int len);
>  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
>  int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
>  void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index 75a8b22da664..13594bb502d1 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
>                                 valid_blocks++;
>                 }
>
> -               f2fs_invalidate_blocks(sbi, blkaddr);
> +               f2fs_invalidate_blocks(sbi, blkaddr, 1);
>
>                 if (!released || blkaddr != COMPRESS_ADDR)
>                         nr_free++;
> @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
>                 unsigned int i;
>
>                 for (i = 0; i < ei.len; i++)
> -                       f2fs_invalidate_blocks(sbi, ei.blk + i);
> +                       f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
>
>                 dec_valid_block_count(sbi, inode, ei.len);
>                 f2fs_update_time(sbi, REQ_TIME);
> @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
>                 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
>                 if (ret) {
>                         dec_valid_block_count(sbi, inode, 1);
> -                       f2fs_invalidate_blocks(sbi, *blkaddr);
> +                       f2fs_invalidate_blocks(sbi, *blkaddr, 1);
>                 } else {
>                         f2fs_update_data_blkaddr(&dn, *blkaddr);
>                 }
> @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
>                         break;
>                 }
>
> -               f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> +               f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
>                 f2fs_set_data_blkaddr(dn, NEW_ADDR);
>         }
>
> diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> index af36c6d6542b..db15d6a90f67 100644
> --- a/fs/f2fs/node.c
> +++ b/fs/f2fs/node.c
> @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
>         }
>
>         /* Deallocate node address */
> -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
>         dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
>         set_node_addr(sbi, &ni, NULL_ADDR, false);
>
> @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
>         if (err)
>                 return err;
>
> -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
>         dec_valid_node_count(sbi, inode, false);
>         set_node_addr(sbi, &ni, NULL_ADDR, false);
>
> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> index 92ddff285a65..67f2bfdeb6ec 100644
> --- a/fs/f2fs/segment.c
> +++ b/fs/f2fs/segment.c
> @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
>                 if (!__is_valid_data_blkaddr(new_addr)) {
>                         if (new_addr == NULL_ADDR)
>                                 dec_valid_block_count(sbi, inode, 1);
> -                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> +                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
>                         f2fs_update_data_blkaddr(&dn, new_addr);
>                 } else {
>                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
>                 get_sec_entry(sbi, segno)->valid_blocks += del;
>  }
>
> -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> +                                       block_t addr, block_t end)
>  {
>         unsigned int segno = GET_SEGNO(sbi, addr);
>         struct sit_info *sit_i = SIT_I(sbi);
> +       unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> +       unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> +       block_t addr_start = addr;
>
> -       f2fs_bug_on(sbi, addr == NULL_ADDR);
> -       if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> -               return;
> -
> -       f2fs_invalidate_internal_cache(sbi, addr, 1);
> +       f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
>
>         /* add it into sit main buffer */
>         down_write(&sit_i->sentry_lock);
>
> -       update_segment_mtime(sbi, addr, 0);
> -       update_sit_entry(sbi, addr, -1);
> +       if (seg_num == 1)
> +               len = end - addr + 1;
> +       else
> +               len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
>
> -       /* add it into dirty seglist */
> -       locate_dirty_segment(sbi, segno);
> +       do {
> +               update_segment_mtime(sbi, addr_start, 0);
> +               update_sit_entry(sbi, addr_start, -len);
> +
> +               /* add it into dirty seglist */
> +               locate_dirty_segment(sbi, segno);
> +
> +               /* update @addr_start and @len and @segno */
> +               addr_start = START_BLOCK(sbi, ++segno);
> +               if (++i == seg_num)
> +                       len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> +               else
> +                       len = max_blocks;
> +       } while (i <= seg_num);
>
>         up_write(&sit_i->sentry_lock);
>  }
>
> +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> +                               block_t addr, unsigned int len)
> +{
> +       unsigned int i;
> +       /* Temporary record location */
> +       block_t addr_start = addr, addr_end;
> +
> +       if (len == 0)
> +               return;
> +
> +       for (i = 0; i < len; i++) {
> +               addr_end = addr + i;
> +
> +               f2fs_bug_on(sbi, addr_end == NULL_ADDR);

Looks like this line should be out of this loop, right?

> +
> +               if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {

ditto?
Could you help with enhancing the readability here? a little bit
confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
here.

> +                       if (addr_start == addr_end) {
> +                               addr_end = addr_start = addr_end + 1;
> +                               continue;
> +                       }
> +
> +                       __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> +                       addr_end = addr_start = addr_end + 1;
> +               }
> +       }
> +
> +       if (addr_end >= (addr + len))
> +               return;
> +
> +       __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> +
> +}
> +
>  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
>  {
>         struct sit_info *sit_i = SIT_I(sbi);
> --
> 2.25.1
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
yi sun Oct. 31, 2024, 3 a.m. UTC | #2
On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
>
> On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> >
> > New function can process some consecutive blocks at a time.
> >
> > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > are very time-consuming, so if f2fs_invalidate_blocks() can
> > process consecutive blocks at one time, it will save a lot of time.
> >
> > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > ---
> >  fs/f2fs/compress.c |  4 +--
> >  fs/f2fs/f2fs.h     |  3 +-
> >  fs/f2fs/file.c     |  8 +++---
> >  fs/f2fs/node.c     |  4 +--
> >  fs/f2fs/segment.c  | 69 ++++++++++++++++++++++++++++++++++++++--------
> >  5 files changed, 68 insertions(+), 20 deletions(-)
> >
> > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > index e607a7885b57..02ad0ff29cf2 100644
> > --- a/fs/f2fs/compress.c
> > +++ b/fs/f2fs/compress.c
> > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> >                         if (blkaddr == COMPRESS_ADDR)
> >                                 fio.compr_blocks++;
> >                         if (__is_valid_data_blkaddr(blkaddr))
> > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >                         f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> >                         goto unlock_continue;
> >                 }
> > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> >
> >                 if (i > cc->valid_nr_cpages) {
> >                         if (__is_valid_data_blkaddr(blkaddr)) {
> > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >                                 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> >                         }
> >                         goto unlock_continue;
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index addd49af57ec..4bb459157adf 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> >  int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> >  int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> >  void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > +                                               unsigned int len);
> >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> >  int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> >  void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index 75a8b22da664..13594bb502d1 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> >                                 valid_blocks++;
> >                 }
> >
> > -               f2fs_invalidate_blocks(sbi, blkaddr);
> > +               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >
> >                 if (!released || blkaddr != COMPRESS_ADDR)
> >                         nr_free++;
> > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> >                 unsigned int i;
> >
> >                 for (i = 0; i < ei.len; i++)
> > -                       f2fs_invalidate_blocks(sbi, ei.blk + i);
> > +                       f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> >
> >                 dec_valid_block_count(sbi, inode, ei.len);
> >                 f2fs_update_time(sbi, REQ_TIME);
> > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> >                 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> >                 if (ret) {
> >                         dec_valid_block_count(sbi, inode, 1);
> > -                       f2fs_invalidate_blocks(sbi, *blkaddr);
> > +                       f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> >                 } else {
> >                         f2fs_update_data_blkaddr(&dn, *blkaddr);
> >                 }
> > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> >                         break;
> >                 }
> >
> > -               f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > +               f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> >                 f2fs_set_data_blkaddr(dn, NEW_ADDR);
> >         }
> >
> > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > index af36c6d6542b..db15d6a90f67 100644
> > --- a/fs/f2fs/node.c
> > +++ b/fs/f2fs/node.c
> > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> >         }
> >
> >         /* Deallocate node address */
> > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> >         dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> >         if (err)
> >                 return err;
> >
> > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> >         dec_valid_node_count(sbi, inode, false);
> >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > index 92ddff285a65..67f2bfdeb6ec 100644
> > --- a/fs/f2fs/segment.c
> > +++ b/fs/f2fs/segment.c
> > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> >                 if (!__is_valid_data_blkaddr(new_addr)) {
> >                         if (new_addr == NULL_ADDR)
> >                                 dec_valid_block_count(sbi, inode, 1);
> > -                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > +                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> >                         f2fs_update_data_blkaddr(&dn, new_addr);
> >                 } else {
> >                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> >                 get_sec_entry(sbi, segno)->valid_blocks += del;
> >  }
> >
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > +                                       block_t addr, block_t end)
> >  {
> >         unsigned int segno = GET_SEGNO(sbi, addr);
> >         struct sit_info *sit_i = SIT_I(sbi);
> > +       unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > +       unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > +       block_t addr_start = addr;
> >
> > -       f2fs_bug_on(sbi, addr == NULL_ADDR);
> > -       if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > -               return;
> > -
> > -       f2fs_invalidate_internal_cache(sbi, addr, 1);
> > +       f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> >
> >         /* add it into sit main buffer */
> >         down_write(&sit_i->sentry_lock);
> >
> > -       update_segment_mtime(sbi, addr, 0);
> > -       update_sit_entry(sbi, addr, -1);
> > +       if (seg_num == 1)
> > +               len = end - addr + 1;
> > +       else
> > +               len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> >
> > -       /* add it into dirty seglist */
> > -       locate_dirty_segment(sbi, segno);
> > +       do {
> > +               update_segment_mtime(sbi, addr_start, 0);
> > +               update_sit_entry(sbi, addr_start, -len);
> > +
> > +               /* add it into dirty seglist */
> > +               locate_dirty_segment(sbi, segno);
> > +
> > +               /* update @addr_start and @len and @segno */
> > +               addr_start = START_BLOCK(sbi, ++segno);
> > +               if (++i == seg_num)
> > +                       len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > +               else
> > +                       len = max_blocks;
> > +       } while (i <= seg_num);
> >
> >         up_write(&sit_i->sentry_lock);
> >  }
> >
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > +                               block_t addr, unsigned int len)
> > +{
> > +       unsigned int i;
> > +       /* Temporary record location */
> > +       block_t addr_start = addr, addr_end;
> > +
> > +       if (len == 0)
> > +               return;
> > +
> > +       for (i = 0; i < len; i++) {
> > +               addr_end = addr + i;
> > +
> > +               f2fs_bug_on(sbi, addr_end == NULL_ADDR);
>
> Looks like this line should be out of this loop, right?
>
> > +
> > +               if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
>
> ditto?

The original f2fs_invalidate_blocks() can only process one block at a time,
and it will check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR for
each input block.

The new f2fs_invalidate_blocks() can process multiple blocks at a time.
In order to keep it consistent with the original f2fs_invalidate_blocks(),
the new function will also check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR
for each block, so these two lines are placed in the loop.

> Could you help with enhancing the readability here? a little bit
> confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> here.
>

The addr_start and addr_end will continue to move to filter out the range of
all valid data blkaddr and pass it to __f2fs_invalidate_blocks() for
further processing.

Assume that the input  parameters of f2fs_invalidate_blocks() are addr=N, len=4,
and there is a blkaddr = NEW_ADDR.

Like this:
          |      N       |    N+1    |NEW_ADDR|N+3|
loop1  addr_start
           addr_end

loop2                  addr_end

loop3                                  addr_end
                                           At this point we need to
pass range [addr_start, addr_end - 1] into __f2fs_invalidate_blocks()
for processing.

loop4
                                                           addr_start
                                                           addr_end

> > +                       if (addr_start == addr_end) {
> > +                               addr_end = addr_start = addr_end + 1;
> > +                               continue;
> > +                       }
> > +
> > +                       __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > +                       addr_end = addr_start = addr_end + 1;
> > +               }
> > +       }
> > +
> > +       if (addr_end >= (addr + len))
> > +               return;
> > +
> > +       __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > +
> > +}
> > +
> >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> >  {
> >         struct sit_info *sit_i = SIT_I(sbi);
> > --
> > 2.25.1
> >
> >
> >
> > _______________________________________________
> > Linux-f2fs-devel mailing list
> > Linux-f2fs-devel@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Zhiguo Niu Oct. 31, 2024, 9:10 a.m. UTC | #3
yi sun <sunyibuaa@gmail.com> 于2024年10月31日周四 11:00写道:
>
> On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
> >
> > On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> > >
> > > New function can process some consecutive blocks at a time.
> > >
> > > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > > are very time-consuming, so if f2fs_invalidate_blocks() can
> > > process consecutive blocks at one time, it will save a lot of time.
> > >
> > > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > > ---
> > >  fs/f2fs/compress.c |  4 +--
> > >  fs/f2fs/f2fs.h     |  3 +-
> > >  fs/f2fs/file.c     |  8 +++---
> > >  fs/f2fs/node.c     |  4 +--
> > >  fs/f2fs/segment.c  | 69 ++++++++++++++++++++++++++++++++++++++--------
> > >  5 files changed, 68 insertions(+), 20 deletions(-)
> > >
> > > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > > index e607a7885b57..02ad0ff29cf2 100644
> > > --- a/fs/f2fs/compress.c
> > > +++ b/fs/f2fs/compress.c
> > > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > >                         if (blkaddr == COMPRESS_ADDR)
> > >                                 fio.compr_blocks++;
> > >                         if (__is_valid_data_blkaddr(blkaddr))
> > > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > >                         f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> > >                         goto unlock_continue;
> > >                 }
> > > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > >
> > >                 if (i > cc->valid_nr_cpages) {
> > >                         if (__is_valid_data_blkaddr(blkaddr)) {
> > > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > >                                 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> > >                         }
> > >                         goto unlock_continue;
> > > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > > index addd49af57ec..4bb459157adf 100644
> > > --- a/fs/f2fs/f2fs.h
> > > +++ b/fs/f2fs/f2fs.h
> > > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> > >  int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> > >  int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> > >  void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > > +                                               unsigned int len);
> > >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> > >  int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> > >  void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > > index 75a8b22da664..13594bb502d1 100644
> > > --- a/fs/f2fs/file.c
> > > +++ b/fs/f2fs/file.c
> > > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> > >                                 valid_blocks++;
> > >                 }
> > >
> > > -               f2fs_invalidate_blocks(sbi, blkaddr);
> > > +               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > >
> > >                 if (!released || blkaddr != COMPRESS_ADDR)
> > >                         nr_free++;
> > > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> > >                 unsigned int i;
> > >
> > >                 for (i = 0; i < ei.len; i++)
> > > -                       f2fs_invalidate_blocks(sbi, ei.blk + i);
> > > +                       f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> > >
> > >                 dec_valid_block_count(sbi, inode, ei.len);
> > >                 f2fs_update_time(sbi, REQ_TIME);
> > > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> > >                 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> > >                 if (ret) {
> > >                         dec_valid_block_count(sbi, inode, 1);
> > > -                       f2fs_invalidate_blocks(sbi, *blkaddr);
> > > +                       f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> > >                 } else {
> > >                         f2fs_update_data_blkaddr(&dn, *blkaddr);
> > >                 }
> > > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> > >                         break;
> > >                 }
> > >
> > > -               f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > > +               f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> > >                 f2fs_set_data_blkaddr(dn, NEW_ADDR);
> > >         }
> > >
> > > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > > index af36c6d6542b..db15d6a90f67 100644
> > > --- a/fs/f2fs/node.c
> > > +++ b/fs/f2fs/node.c
> > > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> > >         }
> > >
> > >         /* Deallocate node address */
> > > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > >         dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> > >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> > >
> > > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> > >         if (err)
> > >                 return err;
> > >
> > > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > >         dec_valid_node_count(sbi, inode, false);
> > >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> > >
> > > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > > index 92ddff285a65..67f2bfdeb6ec 100644
> > > --- a/fs/f2fs/segment.c
> > > +++ b/fs/f2fs/segment.c
> > > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> > >                 if (!__is_valid_data_blkaddr(new_addr)) {
> > >                         if (new_addr == NULL_ADDR)
> > >                                 dec_valid_block_count(sbi, inode, 1);
> > > -                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > > +                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> > >                         f2fs_update_data_blkaddr(&dn, new_addr);
> > >                 } else {
> > >                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> > >                 get_sec_entry(sbi, segno)->valid_blocks += del;
> > >  }
> > >
> > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > +                                       block_t addr, block_t end)
> > >  {
> > >         unsigned int segno = GET_SEGNO(sbi, addr);
> > >         struct sit_info *sit_i = SIT_I(sbi);
> > > +       unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > > +       unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > > +       block_t addr_start = addr;
> > >
> > > -       f2fs_bug_on(sbi, addr == NULL_ADDR);
> > > -       if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > > -               return;
> > > -
> > > -       f2fs_invalidate_internal_cache(sbi, addr, 1);
> > > +       f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> > >
> > >         /* add it into sit main buffer */
> > >         down_write(&sit_i->sentry_lock);
> > >
> > > -       update_segment_mtime(sbi, addr, 0);
> > > -       update_sit_entry(sbi, addr, -1);
> > > +       if (seg_num == 1)
> > > +               len = end - addr + 1;
> > > +       else
> > > +               len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> > >
> > > -       /* add it into dirty seglist */
> > > -       locate_dirty_segment(sbi, segno);
> > > +       do {
> > > +               update_segment_mtime(sbi, addr_start, 0);
> > > +               update_sit_entry(sbi, addr_start, -len);
> > > +
> > > +               /* add it into dirty seglist */
> > > +               locate_dirty_segment(sbi, segno);
> > > +
> > > +               /* update @addr_start and @len and @segno */
> > > +               addr_start = START_BLOCK(sbi, ++segno);
> > > +               if (++i == seg_num)
> > > +                       len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > > +               else
> > > +                       len = max_blocks;
> > > +       } while (i <= seg_num);
> > >
> > >         up_write(&sit_i->sentry_lock);
> > >  }
> > >
> > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > +                               block_t addr, unsigned int len)
> > > +{
> > > +       unsigned int i;
> > > +       /* Temporary record location */
> > > +       block_t addr_start = addr, addr_end;
> > > +
> > > +       if (len == 0)
> > > +               return;
> > > +
> > > +       for (i = 0; i < len; i++) {
> > > +               addr_end = addr + i;
> > > +
> > > +               f2fs_bug_on(sbi, addr_end == NULL_ADDR);
> >
> > Looks like this line should be out of this loop, right?
> >
> > > +
> > > +               if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
> >
> > ditto?
>
> The original f2fs_invalidate_blocks() can only process one block at a time,
> and it will check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR for
> each input block.
>
> The new f2fs_invalidate_blocks() can process multiple blocks at a time.
> In order to keep it consistent with the original f2fs_invalidate_blocks(),
> the new function will also check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR
> for each block, so these two lines are placed in the loop.
>
> > Could you help with enhancing the readability here? a little bit
> > confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> > here.
> >
>
> The addr_start and addr_end will continue to move to filter out the range of
> all valid data blkaddr and pass it to __f2fs_invalidate_blocks() for
> further processing.
>
> Assume that the input  parameters of f2fs_invalidate_blocks() are addr=N, len=4,
> and there is a blkaddr = NEW_ADDR.
>
> Like this:
>           |      N       |    N+1    |NEW_ADDR|N+3|

Is this actually not a continuous address case?

> loop1  addr_start
>            addr_end
>
> loop2                  addr_end
>
> loop3                                  addr_end
>                                            At this point we need to
> pass range [addr_start, addr_end - 1] into __f2fs_invalidate_blocks()
> for processing.
>
> loop4
>                                                            addr_start
>                                                            addr_end
>
> > > +                       if (addr_start == addr_end) {
> > > +                               addr_end = addr_start = addr_end + 1;
> > > +                               continue;
> > > +                       }
> > > +
> > > +                       __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > > +                       addr_end = addr_start = addr_end + 1;
> > > +               }
> > > +       }
> > > +
> > > +       if (addr_end >= (addr + len))
> > > +               return;
> > > +
> > > +       __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > > +
> > > +}
> > > +
> > >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> > >  {
> > >         struct sit_info *sit_i = SIT_I(sbi);
> > > --
> > > 2.25.1
> > >
> > >
> > >
> > > _______________________________________________
> > > Linux-f2fs-devel mailing list
> > > Linux-f2fs-devel@lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
yi sun Oct. 31, 2024, 10:51 a.m. UTC | #4
On Thu, Oct 31, 2024 at 5:10 PM Zhiguo Niu <niuzhiguo84@gmail.com> wrote:
>
> yi sun <sunyibuaa@gmail.com> 于2024年10月31日周四 11:00写道:
> >
> > On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
> > >
> > > On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> > > >
> > > > New function can process some consecutive blocks at a time.
> > > >
> > > > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > > > are very time-consuming, so if f2fs_invalidate_blocks() can
> > > > process consecutive blocks at one time, it will save a lot of time.
> > > >
> > > > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > > > ---
> > > >  fs/f2fs/compress.c |  4 +--
> > > >  fs/f2fs/f2fs.h     |  3 +-
> > > >  fs/f2fs/file.c     |  8 +++---
> > > >  fs/f2fs/node.c     |  4 +--
> > > >  fs/f2fs/segment.c  | 69 ++++++++++++++++++++++++++++++++++++++--------
> > > >  5 files changed, 68 insertions(+), 20 deletions(-)
> > > >
> > > > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > > > index e607a7885b57..02ad0ff29cf2 100644
> > > > --- a/fs/f2fs/compress.c
> > > > +++ b/fs/f2fs/compress.c
> > > > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > > >                         if (blkaddr == COMPRESS_ADDR)
> > > >                                 fio.compr_blocks++;
> > > >                         if (__is_valid_data_blkaddr(blkaddr))
> > > > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > > > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > >                         f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> > > >                         goto unlock_continue;
> > > >                 }
> > > > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> > > >
> > > >                 if (i > cc->valid_nr_cpages) {
> > > >                         if (__is_valid_data_blkaddr(blkaddr)) {
> > > > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > > > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > >                                 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> > > >                         }
> > > >                         goto unlock_continue;
> > > > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > > > index addd49af57ec..4bb459157adf 100644
> > > > --- a/fs/f2fs/f2fs.h
> > > > +++ b/fs/f2fs/f2fs.h
> > > > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> > > >  int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> > > >  int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> > > >  void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > > > +                                               unsigned int len);
> > > >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> > > >  int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> > > >  void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > > > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > > > index 75a8b22da664..13594bb502d1 100644
> > > > --- a/fs/f2fs/file.c
> > > > +++ b/fs/f2fs/file.c
> > > > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> > > >                                 valid_blocks++;
> > > >                 }
> > > >
> > > > -               f2fs_invalidate_blocks(sbi, blkaddr);
> > > > +               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> > > >
> > > >                 if (!released || blkaddr != COMPRESS_ADDR)
> > > >                         nr_free++;
> > > > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> > > >                 unsigned int i;
> > > >
> > > >                 for (i = 0; i < ei.len; i++)
> > > > -                       f2fs_invalidate_blocks(sbi, ei.blk + i);
> > > > +                       f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> > > >
> > > >                 dec_valid_block_count(sbi, inode, ei.len);
> > > >                 f2fs_update_time(sbi, REQ_TIME);
> > > > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> > > >                 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> > > >                 if (ret) {
> > > >                         dec_valid_block_count(sbi, inode, 1);
> > > > -                       f2fs_invalidate_blocks(sbi, *blkaddr);
> > > > +                       f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> > > >                 } else {
> > > >                         f2fs_update_data_blkaddr(&dn, *blkaddr);
> > > >                 }
> > > > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> > > >                         break;
> > > >                 }
> > > >
> > > > -               f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > > > +               f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> > > >                 f2fs_set_data_blkaddr(dn, NEW_ADDR);
> > > >         }
> > > >
> > > > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > > > index af36c6d6542b..db15d6a90f67 100644
> > > > --- a/fs/f2fs/node.c
> > > > +++ b/fs/f2fs/node.c
> > > > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> > > >         }
> > > >
> > > >         /* Deallocate node address */
> > > > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > > >         dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> > > >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> > > >
> > > > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> > > >         if (err)
> > > >                 return err;
> > > >
> > > > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > > > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> > > >         dec_valid_node_count(sbi, inode, false);
> > > >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> > > >
> > > > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > > > index 92ddff285a65..67f2bfdeb6ec 100644
> > > > --- a/fs/f2fs/segment.c
> > > > +++ b/fs/f2fs/segment.c
> > > > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> > > >                 if (!__is_valid_data_blkaddr(new_addr)) {
> > > >                         if (new_addr == NULL_ADDR)
> > > >                                 dec_valid_block_count(sbi, inode, 1);
> > > > -                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > > > +                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> > > >                         f2fs_update_data_blkaddr(&dn, new_addr);
> > > >                 } else {
> > > >                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > > > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> > > >                 get_sec_entry(sbi, segno)->valid_blocks += del;
> > > >  }
> > > >
> > > > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > > > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > > +                                       block_t addr, block_t end)
> > > >  {
> > > >         unsigned int segno = GET_SEGNO(sbi, addr);
> > > >         struct sit_info *sit_i = SIT_I(sbi);
> > > > +       unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > > > +       unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > > > +       block_t addr_start = addr;
> > > >
> > > > -       f2fs_bug_on(sbi, addr == NULL_ADDR);
> > > > -       if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > > > -               return;
> > > > -
> > > > -       f2fs_invalidate_internal_cache(sbi, addr, 1);
> > > > +       f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> > > >
> > > >         /* add it into sit main buffer */
> > > >         down_write(&sit_i->sentry_lock);
> > > >
> > > > -       update_segment_mtime(sbi, addr, 0);
> > > > -       update_sit_entry(sbi, addr, -1);
> > > > +       if (seg_num == 1)
> > > > +               len = end - addr + 1;
> > > > +       else
> > > > +               len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> > > >
> > > > -       /* add it into dirty seglist */
> > > > -       locate_dirty_segment(sbi, segno);
> > > > +       do {
> > > > +               update_segment_mtime(sbi, addr_start, 0);
> > > > +               update_sit_entry(sbi, addr_start, -len);
> > > > +
> > > > +               /* add it into dirty seglist */
> > > > +               locate_dirty_segment(sbi, segno);
> > > > +
> > > > +               /* update @addr_start and @len and @segno */
> > > > +               addr_start = START_BLOCK(sbi, ++segno);
> > > > +               if (++i == seg_num)
> > > > +                       len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > > > +               else
> > > > +                       len = max_blocks;
> > > > +       } while (i <= seg_num);
> > > >
> > > >         up_write(&sit_i->sentry_lock);
> > > >  }
> > > >
> > > > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > > > +                               block_t addr, unsigned int len)
> > > > +{
> > > > +       unsigned int i;
> > > > +       /* Temporary record location */
> > > > +       block_t addr_start = addr, addr_end;
> > > > +
> > > > +       if (len == 0)
> > > > +               return;
> > > > +
> > > > +       for (i = 0; i < len; i++) {
> > > > +               addr_end = addr + i;
> > > > +
> > > > +               f2fs_bug_on(sbi, addr_end == NULL_ADDR);
> > >
> > > Looks like this line should be out of this loop, right?
> > >
> > > > +
> > > > +               if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
> > >
> > > ditto?
> >
> > The original f2fs_invalidate_blocks() can only process one block at a time,
> > and it will check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR for
> > each input block.
> >
> > The new f2fs_invalidate_blocks() can process multiple blocks at a time.
> > In order to keep it consistent with the original f2fs_invalidate_blocks(),
> > the new function will also check NULL_ADDR, NEW_ADDR and COMPRESS_ADDR
> > for each block, so these two lines are placed in the loop.
> >
> > > Could you help with enhancing the readability here? a little bit
> > > confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> > > here.
> > >
> >
> > The addr_start and addr_end will continue to move to filter out the range of
> > all valid data blkaddr and pass it to __f2fs_invalidate_blocks() for
> > further processing.
> >
> > Assume that the input  parameters of f2fs_invalidate_blocks() are addr=N, len=4,
> > and there is a blkaddr = NEW_ADDR.
> >
> > Like this:
> >           |      N       |    N+1    |NEW_ADDR|N+3|
>
> Is this actually not a continuous address case?
>

I'm not sure if this case actually exists, I just took all the
possibilities into consideration.

> > loop1  addr_start
> >            addr_end
> >
> > loop2                  addr_end
> >
> > loop3                                  addr_end
> >                                            At this point we need to
> > pass range [addr_start, addr_end - 1] into __f2fs_invalidate_blocks()
> > for processing.
> >
> > loop4
> >                                                            addr_start
> >                                                            addr_end
> >
> > > > +                       if (addr_start == addr_end) {
> > > > +                               addr_end = addr_start = addr_end + 1;
> > > > +                               continue;
> > > > +                       }
> > > > +
> > > > +                       __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > > > +                       addr_end = addr_start = addr_end + 1;
> > > > +               }
> > > > +       }
> > > > +
> > > > +       if (addr_end >= (addr + len))
> > > > +               return;
> > > > +
> > > > +       __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > > > +
> > > > +}
> > > > +
> > > >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> > > >  {
> > > >         struct sit_info *sit_i = SIT_I(sbi);
> > > > --
> > > > 2.25.1
> > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > Linux-f2fs-devel mailing list
> > > > Linux-f2fs-devel@lists.sourceforge.net
> > > > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
yi sun Nov. 1, 2024, 3:39 a.m. UTC | #5
On Thu, Oct 31, 2024 at 1:00 AM Daeho Jeong <daeho43@gmail.com> wrote:
>
> On Wed, Oct 30, 2024 at 3:35 AM Yi Sun <yi.sun@unisoc.com> wrote:
> >
> > New function can process some consecutive blocks at a time.
> >
> > Function f2fs_invalidate_blocks()->down_write() and up_write()
> > are very time-consuming, so if f2fs_invalidate_blocks() can
> > process consecutive blocks at one time, it will save a lot of time.
> >
> > Signed-off-by: Yi Sun <yi.sun@unisoc.com>
> > ---
> >  fs/f2fs/compress.c |  4 +--
> >  fs/f2fs/f2fs.h     |  3 +-
> >  fs/f2fs/file.c     |  8 +++---
> >  fs/f2fs/node.c     |  4 +--
> >  fs/f2fs/segment.c  | 69 ++++++++++++++++++++++++++++++++++++++--------
> >  5 files changed, 68 insertions(+), 20 deletions(-)
> >
> > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > index e607a7885b57..02ad0ff29cf2 100644
> > --- a/fs/f2fs/compress.c
> > +++ b/fs/f2fs/compress.c
> > @@ -1374,7 +1374,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> >                         if (blkaddr == COMPRESS_ADDR)
> >                                 fio.compr_blocks++;
> >                         if (__is_valid_data_blkaddr(blkaddr))
> > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >                         f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
> >                         goto unlock_continue;
> >                 }
> > @@ -1384,7 +1384,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
> >
> >                 if (i > cc->valid_nr_cpages) {
> >                         if (__is_valid_data_blkaddr(blkaddr)) {
> > -                               f2fs_invalidate_blocks(sbi, blkaddr);
> > +                               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >                                 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
> >                         }
> >                         goto unlock_continue;
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index addd49af57ec..4bb459157adf 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -3716,7 +3716,8 @@ int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
> >  int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
> >  int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
> >  void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
> > +                                               unsigned int len);
> >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
> >  int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
> >  void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index 75a8b22da664..13594bb502d1 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -652,7 +652,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
> >                                 valid_blocks++;
> >                 }
> >
> > -               f2fs_invalidate_blocks(sbi, blkaddr);
> > +               f2fs_invalidate_blocks(sbi, blkaddr, 1);
> >
> >                 if (!released || blkaddr != COMPRESS_ADDR)
> >                         nr_free++;
> > @@ -750,7 +750,7 @@ int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
> >                 unsigned int i;
> >
> >                 for (i = 0; i < ei.len; i++)
> > -                       f2fs_invalidate_blocks(sbi, ei.blk + i);
> > +                       f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
> >
> >                 dec_valid_block_count(sbi, inode, ei.len);
> >                 f2fs_update_time(sbi, REQ_TIME);
> > @@ -1319,7 +1319,7 @@ static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
> >                 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
> >                 if (ret) {
> >                         dec_valid_block_count(sbi, inode, 1);
> > -                       f2fs_invalidate_blocks(sbi, *blkaddr);
> > +                       f2fs_invalidate_blocks(sbi, *blkaddr, 1);
> >                 } else {
> >                         f2fs_update_data_blkaddr(&dn, *blkaddr);
> >                 }
> > @@ -1571,7 +1571,7 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
> >                         break;
> >                 }
> >
> > -               f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
> > +               f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
> >                 f2fs_set_data_blkaddr(dn, NEW_ADDR);
> >         }
> >
> > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
> > index af36c6d6542b..db15d6a90f67 100644
> > --- a/fs/f2fs/node.c
> > +++ b/fs/f2fs/node.c
> > @@ -916,7 +916,7 @@ static int truncate_node(struct dnode_of_data *dn)
> >         }
> >
> >         /* Deallocate node address */
> > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> >         dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
> >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > @@ -2761,7 +2761,7 @@ int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
> >         if (err)
> >                 return err;
> >
> > -       f2fs_invalidate_blocks(sbi, ni.blk_addr);
> > +       f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
> >         dec_valid_node_count(sbi, inode, false);
> >         set_node_addr(sbi, &ni, NULL_ADDR, false);
> >
> > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > index 92ddff285a65..67f2bfdeb6ec 100644
> > --- a/fs/f2fs/segment.c
> > +++ b/fs/f2fs/segment.c
> > @@ -245,7 +245,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
> >                 if (!__is_valid_data_blkaddr(new_addr)) {
> >                         if (new_addr == NULL_ADDR)
> >                                 dec_valid_block_count(sbi, inode, 1);
> > -                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
> > +                       f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
> >                         f2fs_update_data_blkaddr(&dn, new_addr);
> >                 } else {
> >                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
> > @@ -2558,29 +2558,76 @@ static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
> >                 get_sec_entry(sbi, segno)->valid_blocks += del;
> >  }
> >
> > -void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
> > +static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > +                                       block_t addr, block_t end)
> >  {
> >         unsigned int segno = GET_SEGNO(sbi, addr);
> >         struct sit_info *sit_i = SIT_I(sbi);
> > +       unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
> > +       unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
> > +       block_t addr_start = addr;
> >
> > -       f2fs_bug_on(sbi, addr == NULL_ADDR);
> > -       if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
> > -               return;
> > -
> > -       f2fs_invalidate_internal_cache(sbi, addr, 1);
> > +       f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
> >
> >         /* add it into sit main buffer */
> >         down_write(&sit_i->sentry_lock);
> >
> > -       update_segment_mtime(sbi, addr, 0);
> > -       update_sit_entry(sbi, addr, -1);
> > +       if (seg_num == 1)
> > +               len = end - addr + 1;
> > +       else
> > +               len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
> >
> > -       /* add it into dirty seglist */
> > -       locate_dirty_segment(sbi, segno);
> > +       do {
> > +               update_segment_mtime(sbi, addr_start, 0);
> > +               update_sit_entry(sbi, addr_start, -len);
> > +
> > +               /* add it into dirty seglist */
> > +               locate_dirty_segment(sbi, segno);
> > +
> > +               /* update @addr_start and @len and @segno */
> > +               addr_start = START_BLOCK(sbi, ++segno);
> > +               if (++i == seg_num)
> > +                       len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
> > +               else
> > +                       len = max_blocks;
> > +       } while (i <= seg_num);
> >
> >         up_write(&sit_i->sentry_lock);
> >  }
> >
> > +void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
> > +                               block_t addr, unsigned int len)
> > +{
> > +       unsigned int i;
> > +       /* Temporary record location */
> > +       block_t addr_start = addr, addr_end;
> > +
> > +       if (len == 0)
> > +               return;
> > +
> > +       for (i = 0; i < len; i++) {
> > +               addr_end = addr + i;
> > +
> > +               f2fs_bug_on(sbi, addr_end == NULL_ADDR);
>
> Looks like this line should be out of this loop, right?
>
> > +
> > +               if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
>
> ditto?
> Could you help with enhancing the readability here? a little bit
> confused with using addr_start, addr_end and NEW_ADDR, COMPRESS_ADDR,
> here.
>

Hi Daeho,
Thanks for your feedback, I think you are right. I will correct this problem.


> > +                       if (addr_start == addr_end) {
> > +                               addr_end = addr_start = addr_end + 1;
> > +                               continue;
> > +                       }
> > +
> > +                       __f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
> > +                       addr_end = addr_start = addr_end + 1;
> > +               }
> > +       }
> > +
> > +       if (addr_end >= (addr + len))
> > +               return;
> > +
> > +       __f2fs_invalidate_blocks(sbi, addr_start, addr_end);
> > +
> > +}
> > +
> >  bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
> >  {
> >         struct sit_info *sit_i = SIT_I(sbi);
> > --
> > 2.25.1
> >
> >
> >
> > _______________________________________________
> > Linux-f2fs-devel mailing list
> > Linux-f2fs-devel@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
diff mbox series

Patch

diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index e607a7885b57..02ad0ff29cf2 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -1374,7 +1374,7 @@  static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 			if (blkaddr == COMPRESS_ADDR)
 				fio.compr_blocks++;
 			if (__is_valid_data_blkaddr(blkaddr))
-				f2fs_invalidate_blocks(sbi, blkaddr);
+				f2fs_invalidate_blocks(sbi, blkaddr, 1);
 			f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
 			goto unlock_continue;
 		}
@@ -1384,7 +1384,7 @@  static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 
 		if (i > cc->valid_nr_cpages) {
 			if (__is_valid_data_blkaddr(blkaddr)) {
-				f2fs_invalidate_blocks(sbi, blkaddr);
+				f2fs_invalidate_blocks(sbi, blkaddr, 1);
 				f2fs_update_data_blkaddr(&dn, NEW_ADDR);
 			}
 			goto unlock_continue;
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index addd49af57ec..4bb459157adf 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -3716,7 +3716,8 @@  int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
-void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
+void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
+						unsigned int len);
 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 75a8b22da664..13594bb502d1 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -652,7 +652,7 @@  void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
 				valid_blocks++;
 		}
 
-		f2fs_invalidate_blocks(sbi, blkaddr);
+		f2fs_invalidate_blocks(sbi, blkaddr, 1);
 
 		if (!released || blkaddr != COMPRESS_ADDR)
 			nr_free++;
@@ -750,7 +750,7 @@  int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
 		unsigned int i;
 
 		for (i = 0; i < ei.len; i++)
-			f2fs_invalidate_blocks(sbi, ei.blk + i);
+			f2fs_invalidate_blocks(sbi, ei.blk + i, 1);
 
 		dec_valid_block_count(sbi, inode, ei.len);
 		f2fs_update_time(sbi, REQ_TIME);
@@ -1319,7 +1319,7 @@  static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
 		ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
 		if (ret) {
 			dec_valid_block_count(sbi, inode, 1);
-			f2fs_invalidate_blocks(sbi, *blkaddr);
+			f2fs_invalidate_blocks(sbi, *blkaddr, 1);
 		} else {
 			f2fs_update_data_blkaddr(&dn, *blkaddr);
 		}
@@ -1571,7 +1571,7 @@  static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
 			break;
 		}
 
-		f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
+		f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
 		f2fs_set_data_blkaddr(dn, NEW_ADDR);
 	}
 
diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index af36c6d6542b..db15d6a90f67 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -916,7 +916,7 @@  static int truncate_node(struct dnode_of_data *dn)
 	}
 
 	/* Deallocate node address */
-	f2fs_invalidate_blocks(sbi, ni.blk_addr);
+	f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
 	dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
 	set_node_addr(sbi, &ni, NULL_ADDR, false);
 
@@ -2761,7 +2761,7 @@  int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
 	if (err)
 		return err;
 
-	f2fs_invalidate_blocks(sbi, ni.blk_addr);
+	f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
 	dec_valid_node_count(sbi, inode, false);
 	set_node_addr(sbi, &ni, NULL_ADDR, false);
 
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 92ddff285a65..67f2bfdeb6ec 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -245,7 +245,7 @@  static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
 		if (!__is_valid_data_blkaddr(new_addr)) {
 			if (new_addr == NULL_ADDR)
 				dec_valid_block_count(sbi, inode, 1);
-			f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
+			f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1);
 			f2fs_update_data_blkaddr(&dn, new_addr);
 		} else {
 			f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
@@ -2558,29 +2558,76 @@  static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
 		get_sec_entry(sbi, segno)->valid_blocks += del;
 }
 
-void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
+static void __f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
+					block_t addr, block_t end)
 {
 	unsigned int segno = GET_SEGNO(sbi, addr);
 	struct sit_info *sit_i = SIT_I(sbi);
+	unsigned int seg_num = GET_SEGNO(sbi, end) - segno + 1;
+	unsigned int i = 1, max_blocks = sbi->blocks_per_seg, len;
+	block_t addr_start = addr;
 
-	f2fs_bug_on(sbi, addr == NULL_ADDR);
-	if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
-		return;
-
-	f2fs_invalidate_internal_cache(sbi, addr, 1);
+	f2fs_invalidate_internal_cache(sbi, addr, end - addr + 1);
 
 	/* add it into sit main buffer */
 	down_write(&sit_i->sentry_lock);
 
-	update_segment_mtime(sbi, addr, 0);
-	update_sit_entry(sbi, addr, -1);
+	if (seg_num == 1)
+		len = end - addr + 1;
+	else
+		len = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr);
 
-	/* add it into dirty seglist */
-	locate_dirty_segment(sbi, segno);
+	do {
+		update_segment_mtime(sbi, addr_start, 0);
+		update_sit_entry(sbi, addr_start, -len);
+
+		/* add it into dirty seglist */
+		locate_dirty_segment(sbi, segno);
+
+		/* update @addr_start and @len and @segno */
+		addr_start = START_BLOCK(sbi, ++segno);
+		if (++i == seg_num)
+			len = GET_BLKOFF_FROM_SEG0(sbi, end) + 1;
+		else
+			len = max_blocks;
+	} while (i <= seg_num);
 
 	up_write(&sit_i->sentry_lock);
 }
 
+void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi,
+				block_t addr, unsigned int len)
+{
+	unsigned int i;
+	/* Temporary record location */
+	block_t addr_start = addr, addr_end;
+
+	if (len == 0)
+		return;
+
+	for (i = 0; i < len; i++) {
+		addr_end = addr + i;
+
+		f2fs_bug_on(sbi, addr_end == NULL_ADDR);
+
+		if (addr_end == NEW_ADDR || addr_end == COMPRESS_ADDR) {
+			if (addr_start == addr_end) {
+				addr_end = addr_start = addr_end + 1;
+				continue;
+			}
+
+			__f2fs_invalidate_blocks(sbi, addr_start, addr_end - 1);
+			addr_end = addr_start = addr_end + 1;
+		}
+	}
+
+	if (addr_end >= (addr + len))
+		return;
+
+	__f2fs_invalidate_blocks(sbi, addr_start, addr_end);
+
+}
+
 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
 {
 	struct sit_info *sit_i = SIT_I(sbi);