f2fs: use bitmap in discard_entry
authorChao Yu <yuchao0@huawei.com>
Tue, 28 Mar 2017 10:18:50 +0000 (18:18 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Wed, 5 Apr 2017 18:05:04 +0000 (11:05 -0700)
This patch changes to use bitmap instead of extent in struct discard_entry
to indicate discard range in one segment, for fragmented space, this
implementation can save memory footprint.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/f2fs.h
fs/f2fs/segment.c

index 8741d5102f14e90360697383a61d7842ec192e28..43b6e1eeceb6bd89a7236e6a46512f6b79da9eb2 100644 (file)
@@ -182,11 +182,11 @@ struct inode_entry {
        struct inode *inode;    /* vfs inode pointer */
 };
 
-/* for the list of blockaddresses to be discarded */
+/* for the bitmap indicate blocks to be discarded */
 struct discard_entry {
        struct list_head list;  /* list head */
-       block_t blkaddr;        /* block address to be discarded */
-       int len;                /* # of consecutive blocks of the discard */
+       block_t start_blkaddr;  /* start blockaddr of current segment */
+       unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
 };
 
 enum {
index d5413afdd3dab38fa298a98d0fbea760b9e2a347..12d2aa7032bba292fa339645bddbaec3bd1b9f08 100644 (file)
@@ -962,32 +962,6 @@ static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
        return err;
 }
 
-static void __add_discard_entry(struct f2fs_sb_info *sbi,
-               struct cp_control *cpc, struct seg_entry *se,
-               unsigned int start, unsigned int end)
-{
-       struct list_head *head = &SM_I(sbi)->dcc_info->discard_entry_list;
-       struct discard_entry *new, *last;
-
-       if (!list_empty(head)) {
-               last = list_last_entry(head, struct discard_entry, list);
-               if (START_BLOCK(sbi, cpc->trim_start) + start ==
-                               last->blkaddr + last->len &&
-                               last->len < MAX_DISCARD_BLOCKS(sbi)) {
-                       last->len += end - start;
-                       goto done;
-               }
-       }
-
-       new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
-       INIT_LIST_HEAD(&new->list);
-       new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
-       new->len = end - start;
-       list_add_tail(&new->list, head);
-done:
-       SM_I(sbi)->dcc_info->nr_discards += end - start;
-}
-
 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
                                                        bool check_only)
 {
@@ -1000,6 +974,8 @@ static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
        unsigned long *dmap = SIT_I(sbi)->tmp_map;
        unsigned int start = 0, end = -1;
        bool force = (cpc->reason == CP_DISCARD);
+       struct discard_entry *de = NULL;
+       struct list_head *head = &SM_I(sbi)->dcc_info->discard_entry_list;
        int i;
 
        if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
@@ -1031,7 +1007,17 @@ static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
                if (check_only)
                        return true;
 
-               __add_discard_entry(sbi, cpc, se, start, end);
+               if (!de) {
+                       de = f2fs_kmem_cache_alloc(discard_entry_slab,
+                                                               GFP_F2FS_ZERO);
+                       de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
+                       list_add_tail(&de->list, head);
+               }
+
+               for (i = start; i < end; i++)
+                       __set_bit_le(i, (void *)de->discard_map);
+
+               SM_I(sbi)->dcc_info->nr_discards += end - start;
        }
        return false;
 }
@@ -1117,13 +1103,35 @@ next:
 
        /* send small discards */
        list_for_each_entry_safe(entry, this, head, list) {
-               if (force && entry->len < cpc->trim_minlen)
-                       goto skip;
-               f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
-               cpc->trimmed += entry->len;
+               unsigned int cur_pos = 0, next_pos, len, total_len = 0;
+               bool is_valid = test_bit_le(0, entry->discard_map);
+
+find_next:
+               if (is_valid) {
+                       next_pos = find_next_zero_bit_le(entry->discard_map,
+                                       sbi->blocks_per_seg, cur_pos);
+                       len = next_pos - cur_pos;
+
+                       if (force && len < cpc->trim_minlen)
+                               goto skip;
+
+                       f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
+                                                                       len);
+                       cpc->trimmed += len;
+                       total_len += len;
+               } else {
+                       next_pos = find_next_bit_le(entry->discard_map,
+                                       sbi->blocks_per_seg, cur_pos);
+               }
 skip:
+               cur_pos = next_pos;
+               is_valid = !is_valid;
+
+               if (cur_pos < sbi->blocks_per_seg)
+                       goto find_next;
+
                list_del(&entry->list);
-               SM_I(sbi)->dcc_info->nr_discards -= entry->len;
+               SM_I(sbi)->dcc_info->nr_discards -= total_len;
                kmem_cache_free(discard_entry_slab, entry);
        }
 }