blob: a467aca29cfefd8a25a16b125356debc54850e33 [file] [log] [blame]
Chao Yu6d1a8322018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002/*
3 * f2fs shrinker support
4 * the basic infra was copied from fs/ubifs/shrinker.c
5 *
6 * Copyright (c) 2015 Motorola Mobility
7 * Copyright (c) 2015 Jaegeuk Kim <jaegeuk@kernel.org>
Jaegeuk Kim2658e502015-06-19 12:01:21 -07008 */
9#include <linux/fs.h>
10#include <linux/f2fs_fs.h>
11
12#include "f2fs.h"
Jaegeuk Kimad4edb82016-06-16 16:41:49 -070013#include "node.h"
Jaegeuk Kim2658e502015-06-19 12:01:21 -070014
15static LIST_HEAD(f2fs_list);
16static DEFINE_SPINLOCK(f2fs_list_lock);
17static unsigned int shrinker_run_no;
18
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -070019static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi)
20{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070021 long count = NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt;
22
23 return count > 0 ? count : 0;
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -070024}
25
Chao Yu31696582015-07-28 18:33:46 +080026static unsigned long __count_free_nids(struct f2fs_sb_info *sbi)
27{
Chao Yu84af6ae2017-09-29 13:59:35 +080028 long count = NM_I(sbi)->nid_cnt[FREE_NID] - MAX_FREE_NIDS;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070029
30 return count > 0 ? count : 0;
Chao Yu31696582015-07-28 18:33:46 +080031}
32
Jaegeuk Kim554df792015-06-19 13:41:23 -070033static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi)
34{
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -080035 return atomic_read(&sbi->total_zombie_tree) +
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080036 atomic_read(&sbi->total_ext_node);
Jaegeuk Kim554df792015-06-19 13:41:23 -070037}
38
Jaegeuk Kim2658e502015-06-19 12:01:21 -070039unsigned long f2fs_shrink_count(struct shrinker *shrink,
40 struct shrink_control *sc)
41{
42 struct f2fs_sb_info *sbi;
43 struct list_head *p;
44 unsigned long count = 0;
45
46 spin_lock(&f2fs_list_lock);
47 p = f2fs_list.next;
48 while (p != &f2fs_list) {
49 sbi = list_entry(p, struct f2fs_sb_info, s_list);
50
51 /* stop f2fs_put_super */
52 if (!mutex_trylock(&sbi->umount_mutex)) {
53 p = p->next;
54 continue;
55 }
56 spin_unlock(&f2fs_list_lock);
57
Jaegeuk Kim554df792015-06-19 13:41:23 -070058 /* count extent cache entries */
59 count += __count_extent_cache(sbi);
60
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -070061 /* shrink clean nat cache entries */
62 count += __count_nat_entries(sbi);
Jaegeuk Kim2658e502015-06-19 12:01:21 -070063
Chao Yu31696582015-07-28 18:33:46 +080064 /* count free nids cache entries */
65 count += __count_free_nids(sbi);
66
Jaegeuk Kim2658e502015-06-19 12:01:21 -070067 spin_lock(&f2fs_list_lock);
68 p = p->next;
69 mutex_unlock(&sbi->umount_mutex);
70 }
71 spin_unlock(&f2fs_list_lock);
72 return count;
73}
74
75unsigned long f2fs_shrink_scan(struct shrinker *shrink,
76 struct shrink_control *sc)
77{
78 unsigned long nr = sc->nr_to_scan;
79 struct f2fs_sb_info *sbi;
80 struct list_head *p;
81 unsigned int run_no;
82 unsigned long freed = 0;
83
84 spin_lock(&f2fs_list_lock);
85 do {
86 run_no = ++shrinker_run_no;
87 } while (run_no == 0);
88 p = f2fs_list.next;
89 while (p != &f2fs_list) {
90 sbi = list_entry(p, struct f2fs_sb_info, s_list);
91
92 if (sbi->shrinker_run_no == run_no)
93 break;
94
95 /* stop f2fs_put_super */
96 if (!mutex_trylock(&sbi->umount_mutex)) {
97 p = p->next;
98 continue;
99 }
100 spin_unlock(&f2fs_list_lock);
101
102 sbi->shrinker_run_no = run_no;
103
Jaegeuk Kim554df792015-06-19 13:41:23 -0700104 /* shrink extent cache entries */
105 freed += f2fs_shrink_extent_tree(sbi, nr >> 1);
106
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700107 /* shrink clean nat cache entries */
Jaegeuk Kim554df792015-06-19 13:41:23 -0700108 if (freed < nr)
Chao Yu6b4d6a82018-05-30 00:20:41 +0800109 freed += f2fs_try_to_free_nats(sbi, nr - freed);
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700110
Chao Yu31696582015-07-28 18:33:46 +0800111 /* shrink free nids cache entries */
112 if (freed < nr)
Chao Yu6b4d6a82018-05-30 00:20:41 +0800113 freed += f2fs_try_to_free_nids(sbi, nr - freed);
Chao Yu31696582015-07-28 18:33:46 +0800114
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700115 spin_lock(&f2fs_list_lock);
116 p = p->next;
117 list_move_tail(&sbi->s_list, &f2fs_list);
118 mutex_unlock(&sbi->umount_mutex);
119 if (freed >= nr)
120 break;
121 }
122 spin_unlock(&f2fs_list_lock);
123 return freed;
124}
125
126void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
127{
128 spin_lock(&f2fs_list_lock);
129 list_add_tail(&sbi->s_list, &f2fs_list);
130 spin_unlock(&f2fs_list_lock);
131}
132
133void f2fs_leave_shrinker(struct f2fs_sb_info *sbi)
134{
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700135 f2fs_shrink_extent_tree(sbi, __count_extent_cache(sbi));
136
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700137 spin_lock(&f2fs_list_lock);
Sahitya Tummalabe83ade2018-12-18 16:39:24 +0530138 list_del_init(&sbi->s_list);
Jaegeuk Kim2658e502015-06-19 12:01:21 -0700139 spin_unlock(&f2fs_list_lock);
140}