0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/fs.h>
0010 #include <linux/f2fs_fs.h>
0011
0012 #include "f2fs.h"
0013 #include "node.h"
0014
0015 static LIST_HEAD(f2fs_list);
0016 static DEFINE_SPINLOCK(f2fs_list_lock);
0017 static unsigned int shrinker_run_no;
0018
0019 static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi)
0020 {
0021 return NM_I(sbi)->nat_cnt[RECLAIMABLE_NAT];
0022 }
0023
0024 static unsigned long __count_free_nids(struct f2fs_sb_info *sbi)
0025 {
0026 long count = NM_I(sbi)->nid_cnt[FREE_NID] - MAX_FREE_NIDS;
0027
0028 return count > 0 ? count : 0;
0029 }
0030
0031 static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi)
0032 {
0033 return atomic_read(&sbi->total_zombie_tree) +
0034 atomic_read(&sbi->total_ext_node);
0035 }
0036
0037 unsigned long f2fs_shrink_count(struct shrinker *shrink,
0038 struct shrink_control *sc)
0039 {
0040 struct f2fs_sb_info *sbi;
0041 struct list_head *p;
0042 unsigned long count = 0;
0043
0044 spin_lock(&f2fs_list_lock);
0045 p = f2fs_list.next;
0046 while (p != &f2fs_list) {
0047 sbi = list_entry(p, struct f2fs_sb_info, s_list);
0048
0049
0050 if (!mutex_trylock(&sbi->umount_mutex)) {
0051 p = p->next;
0052 continue;
0053 }
0054 spin_unlock(&f2fs_list_lock);
0055
0056
0057 count += __count_extent_cache(sbi);
0058
0059
0060 count += __count_nat_entries(sbi);
0061
0062
0063 count += __count_free_nids(sbi);
0064
0065 spin_lock(&f2fs_list_lock);
0066 p = p->next;
0067 mutex_unlock(&sbi->umount_mutex);
0068 }
0069 spin_unlock(&f2fs_list_lock);
0070 return count;
0071 }
0072
0073 unsigned long f2fs_shrink_scan(struct shrinker *shrink,
0074 struct shrink_control *sc)
0075 {
0076 unsigned long nr = sc->nr_to_scan;
0077 struct f2fs_sb_info *sbi;
0078 struct list_head *p;
0079 unsigned int run_no;
0080 unsigned long freed = 0;
0081
0082 spin_lock(&f2fs_list_lock);
0083 do {
0084 run_no = ++shrinker_run_no;
0085 } while (run_no == 0);
0086 p = f2fs_list.next;
0087 while (p != &f2fs_list) {
0088 sbi = list_entry(p, struct f2fs_sb_info, s_list);
0089
0090 if (sbi->shrinker_run_no == run_no)
0091 break;
0092
0093
0094 if (!mutex_trylock(&sbi->umount_mutex)) {
0095 p = p->next;
0096 continue;
0097 }
0098 spin_unlock(&f2fs_list_lock);
0099
0100 sbi->shrinker_run_no = run_no;
0101
0102
0103 freed += f2fs_shrink_extent_tree(sbi, nr >> 1);
0104
0105
0106 if (freed < nr)
0107 freed += f2fs_try_to_free_nats(sbi, nr - freed);
0108
0109
0110 if (freed < nr)
0111 freed += f2fs_try_to_free_nids(sbi, nr - freed);
0112
0113 spin_lock(&f2fs_list_lock);
0114 p = p->next;
0115 list_move_tail(&sbi->s_list, &f2fs_list);
0116 mutex_unlock(&sbi->umount_mutex);
0117 if (freed >= nr)
0118 break;
0119 }
0120 spin_unlock(&f2fs_list_lock);
0121 return freed;
0122 }
0123
0124 void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
0125 {
0126 spin_lock(&f2fs_list_lock);
0127 list_add_tail(&sbi->s_list, &f2fs_list);
0128 spin_unlock(&f2fs_list_lock);
0129 }
0130
0131 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi)
0132 {
0133 f2fs_shrink_extent_tree(sbi, __count_extent_cache(sbi));
0134
0135 spin_lock(&f2fs_list_lock);
0136 list_del_init(&sbi->s_list);
0137 spin_unlock(&f2fs_list_lock);
0138 }