Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * fs/f2fs/gc.h
0004  *
0005  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
0006  *             http://www.samsung.com/
0007  */
0008 #define GC_THREAD_MIN_WB_PAGES      1   /*
0009                          * a threshold to determine
0010                          * whether IO subsystem is idle
0011                          * or not
0012                          */
0013 #define DEF_GC_THREAD_URGENT_SLEEP_TIME 500 /* 500 ms */
0014 #define DEF_GC_THREAD_MIN_SLEEP_TIME    30000   /* milliseconds */
0015 #define DEF_GC_THREAD_MAX_SLEEP_TIME    60000
0016 #define DEF_GC_THREAD_NOGC_SLEEP_TIME   300000  /* wait 5 min */
0017 
0018 /* choose candidates from sections which has age of more than 7 days */
0019 #define DEF_GC_THREAD_AGE_THRESHOLD     (60 * 60 * 24 * 7)
0020 #define DEF_GC_THREAD_CANDIDATE_RATIO       20  /* select 20% oldest sections as candidates */
0021 #define DEF_GC_THREAD_MAX_CANDIDATE_COUNT   10  /* select at most 10 sections as candidates */
0022 #define DEF_GC_THREAD_AGE_WEIGHT        60  /* age weight */
0023 #define DEFAULT_ACCURACY_CLASS          10000   /* accuracy class */
0024 
0025 #define LIMIT_INVALID_BLOCK 40 /* percentage over total user space */
0026 #define LIMIT_FREE_BLOCK    40 /* percentage over invalid + free space */
0027 
0028 #define DEF_GC_FAILED_PINNED_FILES  2048
0029 
0030 /* Search max. number of dirty segments to select a victim segment */
0031 #define DEF_MAX_VICTIM_SEARCH 4096 /* covers 8GB */
0032 
0033 struct f2fs_gc_kthread {
0034     struct task_struct *f2fs_gc_task;
0035     wait_queue_head_t gc_wait_queue_head;
0036 
0037     /* for gc sleep time */
0038     unsigned int urgent_sleep_time;
0039     unsigned int min_sleep_time;
0040     unsigned int max_sleep_time;
0041     unsigned int no_gc_sleep_time;
0042 
0043     /* for changing gc mode */
0044     unsigned int gc_wake;
0045 
0046     /* for GC_MERGE mount option */
0047     wait_queue_head_t fggc_wq;      /*
0048                          * caller of f2fs_balance_fs()
0049                          * will wait on this wait queue.
0050                          */
0051 };
0052 
0053 struct gc_inode_list {
0054     struct list_head ilist;
0055     struct radix_tree_root iroot;
0056 };
0057 
0058 struct victim_info {
0059     unsigned long long mtime;   /* mtime of section */
0060     unsigned int segno;     /* section No. */
0061 };
0062 
0063 struct victim_entry {
0064     struct rb_node rb_node;     /* rb node located in rb-tree */
0065     union {
0066         struct {
0067             unsigned long long mtime;   /* mtime of section */
0068             unsigned int segno;     /* segment No. */
0069         };
0070         struct victim_info vi;  /* victim info */
0071     };
0072     struct list_head list;
0073 };
0074 
0075 /*
0076  * inline functions
0077  */
0078 
0079 /*
0080  * On a Zoned device zone-capacity can be less than zone-size and if
0081  * zone-capacity is not aligned to f2fs segment size(2MB), then the segment
0082  * starting just before zone-capacity has some blocks spanning across the
0083  * zone-capacity, these blocks are not usable.
0084  * Such spanning segments can be in free list so calculate the sum of usable
0085  * blocks in currently free segments including normal and spanning segments.
0086  */
0087 static inline block_t free_segs_blk_count_zoned(struct f2fs_sb_info *sbi)
0088 {
0089     block_t free_seg_blks = 0;
0090     struct free_segmap_info *free_i = FREE_I(sbi);
0091     int j;
0092 
0093     spin_lock(&free_i->segmap_lock);
0094     for (j = 0; j < MAIN_SEGS(sbi); j++)
0095         if (!test_bit(j, free_i->free_segmap))
0096             free_seg_blks += f2fs_usable_blks_in_seg(sbi, j);
0097     spin_unlock(&free_i->segmap_lock);
0098 
0099     return free_seg_blks;
0100 }
0101 
0102 static inline block_t free_segs_blk_count(struct f2fs_sb_info *sbi)
0103 {
0104     if (f2fs_sb_has_blkzoned(sbi))
0105         return free_segs_blk_count_zoned(sbi);
0106 
0107     return free_segments(sbi) << sbi->log_blocks_per_seg;
0108 }
0109 
0110 static inline block_t free_user_blocks(struct f2fs_sb_info *sbi)
0111 {
0112     block_t free_blks, ovp_blks;
0113 
0114     free_blks = free_segs_blk_count(sbi);
0115     ovp_blks = overprovision_segments(sbi) << sbi->log_blocks_per_seg;
0116 
0117     if (free_blks < ovp_blks)
0118         return 0;
0119 
0120     return free_blks - ovp_blks;
0121 }
0122 
0123 static inline block_t limit_invalid_user_blocks(block_t user_block_count)
0124 {
0125     return (long)(user_block_count * LIMIT_INVALID_BLOCK) / 100;
0126 }
0127 
0128 static inline block_t limit_free_user_blocks(block_t reclaimable_user_blocks)
0129 {
0130     return (long)(reclaimable_user_blocks * LIMIT_FREE_BLOCK) / 100;
0131 }
0132 
0133 static inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th,
0134                             unsigned int *wait)
0135 {
0136     unsigned int min_time = gc_th->min_sleep_time;
0137     unsigned int max_time = gc_th->max_sleep_time;
0138 
0139     if (*wait == gc_th->no_gc_sleep_time)
0140         return;
0141 
0142     if ((long long)*wait + (long long)min_time > (long long)max_time)
0143         *wait = max_time;
0144     else
0145         *wait += min_time;
0146 }
0147 
0148 static inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th,
0149                             unsigned int *wait)
0150 {
0151     unsigned int min_time = gc_th->min_sleep_time;
0152 
0153     if (*wait == gc_th->no_gc_sleep_time)
0154         *wait = gc_th->max_sleep_time;
0155 
0156     if ((long long)*wait - (long long)min_time < (long long)min_time)
0157         *wait = min_time;
0158     else
0159         *wait -= min_time;
0160 }
0161 
0162 static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi)
0163 {
0164     block_t user_block_count = sbi->user_block_count;
0165     block_t invalid_user_blocks = user_block_count -
0166         written_block_count(sbi);
0167     /*
0168      * Background GC is triggered with the following conditions.
0169      * 1. There are a number of invalid blocks.
0170      * 2. There is not enough free space.
0171      */
0172     return (invalid_user_blocks >
0173             limit_invalid_user_blocks(user_block_count) &&
0174         free_user_blocks(sbi) <
0175             limit_free_user_blocks(invalid_user_blocks));
0176 }