blob: 1cb0fcc67d2df66d891c0d33f66d1c49cb16a3ca [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Chao Yua28ef1f2015-07-08 17:59:36 +08002/*
3 * f2fs extent cache support
4 *
5 * Copyright (c) 2015 Motorola Mobility
6 * Copyright (c) 2015 Samsung Electronics
7 * Authors: Jaegeuk Kim <jaegeuk@kernel.org>
8 * Chao Yu <chao2.yu@samsung.com>
Chao Yua28ef1f2015-07-08 17:59:36 +08009 */
10
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13
14#include "f2fs.h"
15#include "node.h"
16#include <trace/events/f2fs.h>
17
Chao Yu54c22582017-04-11 09:25:22 +080018static struct rb_entry *__lookup_rb_tree_fast(struct rb_entry *cached_re,
19 unsigned int ofs)
20{
21 if (cached_re) {
22 if (cached_re->ofs <= ofs &&
23 cached_re->ofs + cached_re->len > ofs) {
24 return cached_re;
25 }
26 }
27 return NULL;
28}
29
Chao Yu4dada3f2018-10-04 11:18:30 +080030static struct rb_entry *__lookup_rb_tree_slow(struct rb_root_cached *root,
Chao Yu54c22582017-04-11 09:25:22 +080031 unsigned int ofs)
32{
Chao Yu4dada3f2018-10-04 11:18:30 +080033 struct rb_node *node = root->rb_root.rb_node;
Chao Yu54c22582017-04-11 09:25:22 +080034 struct rb_entry *re;
35
36 while (node) {
37 re = rb_entry(node, struct rb_entry, rb_node);
38
39 if (ofs < re->ofs)
40 node = node->rb_left;
41 else if (ofs >= re->ofs + re->len)
42 node = node->rb_right;
43 else
44 return re;
45 }
46 return NULL;
47}
48
Chao Yu4dada3f2018-10-04 11:18:30 +080049struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
Chao Yu54c22582017-04-11 09:25:22 +080050 struct rb_entry *cached_re, unsigned int ofs)
51{
52 struct rb_entry *re;
53
54 re = __lookup_rb_tree_fast(cached_re, ofs);
55 if (!re)
56 return __lookup_rb_tree_slow(root, ofs);
57
58 return re;
59}
60
Chao Yu4d57b862018-05-30 00:20:41 +080061struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
Chao Yu4dada3f2018-10-04 11:18:30 +080062 struct rb_root_cached *root,
63 struct rb_node **parent,
64 unsigned int ofs, bool *leftmost)
Chao Yu54c22582017-04-11 09:25:22 +080065{
Chao Yu4dada3f2018-10-04 11:18:30 +080066 struct rb_node **p = &root->rb_root.rb_node;
Chao Yu54c22582017-04-11 09:25:22 +080067 struct rb_entry *re;
68
69 while (*p) {
70 *parent = *p;
71 re = rb_entry(*parent, struct rb_entry, rb_node);
72
Chao Yu4dada3f2018-10-04 11:18:30 +080073 if (ofs < re->ofs) {
Chao Yu54c22582017-04-11 09:25:22 +080074 p = &(*p)->rb_left;
Chao Yu4dada3f2018-10-04 11:18:30 +080075 } else if (ofs >= re->ofs + re->len) {
Chao Yu54c22582017-04-11 09:25:22 +080076 p = &(*p)->rb_right;
Chao Yu4dada3f2018-10-04 11:18:30 +080077 *leftmost = false;
78 } else {
Chao Yu54c22582017-04-11 09:25:22 +080079 f2fs_bug_on(sbi, 1);
Chao Yu4dada3f2018-10-04 11:18:30 +080080 }
Chao Yu54c22582017-04-11 09:25:22 +080081 }
82
83 return p;
84}
85
86/*
87 * lookup rb entry in position of @ofs in rb-tree,
88 * if hit, return the entry, otherwise, return NULL
89 * @prev_ex: extent before ofs
90 * @next_ex: extent after ofs
91 * @insert_p: insert point for new extent at ofs
92 * in order to simpfy the insertion after.
93 * tree must stay unchanged between lookup and insertion.
94 */
Chao Yu4dada3f2018-10-04 11:18:30 +080095struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
Chao Yu54c22582017-04-11 09:25:22 +080096 struct rb_entry *cached_re,
97 unsigned int ofs,
98 struct rb_entry **prev_entry,
99 struct rb_entry **next_entry,
100 struct rb_node ***insert_p,
Chao Yu004b6862017-04-14 23:24:55 +0800101 struct rb_node **insert_parent,
Chao Yu4dada3f2018-10-04 11:18:30 +0800102 bool force, bool *leftmost)
Chao Yu54c22582017-04-11 09:25:22 +0800103{
Chao Yu4dada3f2018-10-04 11:18:30 +0800104 struct rb_node **pnode = &root->rb_root.rb_node;
Chao Yu54c22582017-04-11 09:25:22 +0800105 struct rb_node *parent = NULL, *tmp_node;
106 struct rb_entry *re = cached_re;
107
108 *insert_p = NULL;
109 *insert_parent = NULL;
110 *prev_entry = NULL;
111 *next_entry = NULL;
112
Chao Yu4dada3f2018-10-04 11:18:30 +0800113 if (RB_EMPTY_ROOT(&root->rb_root))
Chao Yu54c22582017-04-11 09:25:22 +0800114 return NULL;
115
116 if (re) {
117 if (re->ofs <= ofs && re->ofs + re->len > ofs)
118 goto lookup_neighbors;
119 }
120
Chao Yu4dada3f2018-10-04 11:18:30 +0800121 if (leftmost)
122 *leftmost = true;
123
Chao Yu54c22582017-04-11 09:25:22 +0800124 while (*pnode) {
125 parent = *pnode;
126 re = rb_entry(*pnode, struct rb_entry, rb_node);
127
Chao Yu4dada3f2018-10-04 11:18:30 +0800128 if (ofs < re->ofs) {
Chao Yu54c22582017-04-11 09:25:22 +0800129 pnode = &(*pnode)->rb_left;
Chao Yu4dada3f2018-10-04 11:18:30 +0800130 } else if (ofs >= re->ofs + re->len) {
Chao Yu54c22582017-04-11 09:25:22 +0800131 pnode = &(*pnode)->rb_right;
Chao Yu4dada3f2018-10-04 11:18:30 +0800132 if (leftmost)
133 *leftmost = false;
134 } else {
Chao Yu54c22582017-04-11 09:25:22 +0800135 goto lookup_neighbors;
Chao Yu4dada3f2018-10-04 11:18:30 +0800136 }
Chao Yu54c22582017-04-11 09:25:22 +0800137 }
138
139 *insert_p = pnode;
140 *insert_parent = parent;
141
142 re = rb_entry(parent, struct rb_entry, rb_node);
143 tmp_node = parent;
144 if (parent && ofs > re->ofs)
145 tmp_node = rb_next(parent);
146 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
147
148 tmp_node = parent;
149 if (parent && ofs < re->ofs)
150 tmp_node = rb_prev(parent);
151 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
152 return NULL;
153
154lookup_neighbors:
Chao Yu004b6862017-04-14 23:24:55 +0800155 if (ofs == re->ofs || force) {
Chao Yu54c22582017-04-11 09:25:22 +0800156 /* lookup prev node for merging backward later */
157 tmp_node = rb_prev(&re->rb_node);
158 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
159 }
Chao Yu004b6862017-04-14 23:24:55 +0800160 if (ofs == re->ofs + re->len - 1 || force) {
Chao Yu54c22582017-04-11 09:25:22 +0800161 /* lookup next node for merging frontward later */
162 tmp_node = rb_next(&re->rb_node);
163 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
164 }
165 return re;
166}
167
Chao Yu4d57b862018-05-30 00:20:41 +0800168bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
Chao Yu4dada3f2018-10-04 11:18:30 +0800169 struct rb_root_cached *root)
Chao Yudf0f6b42017-04-17 18:21:43 +0800170{
171#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu4dada3f2018-10-04 11:18:30 +0800172 struct rb_node *cur = rb_first_cached(root), *next;
Chao Yudf0f6b42017-04-17 18:21:43 +0800173 struct rb_entry *cur_re, *next_re;
174
175 if (!cur)
176 return true;
177
178 while (cur) {
179 next = rb_next(cur);
180 if (!next)
181 return true;
182
183 cur_re = rb_entry(cur, struct rb_entry, rb_node);
184 next_re = rb_entry(next, struct rb_entry, rb_node);
185
186 if (cur_re->ofs + cur_re->len > next_re->ofs) {
187 f2fs_msg(sbi->sb, KERN_INFO, "inconsistent rbtree, "
188 "cur(%u, %u) next(%u, %u)",
189 cur_re->ofs, cur_re->len,
190 next_re->ofs, next_re->len);
191 return false;
192 }
193
194 cur = next;
195 }
196#endif
197 return true;
198}
199
Chao Yua28ef1f2015-07-08 17:59:36 +0800200static struct kmem_cache *extent_tree_slab;
201static struct kmem_cache *extent_node_slab;
202
203static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
204 struct extent_tree *et, struct extent_info *ei,
Chao Yu4dada3f2018-10-04 11:18:30 +0800205 struct rb_node *parent, struct rb_node **p,
206 bool leftmost)
Chao Yua28ef1f2015-07-08 17:59:36 +0800207{
208 struct extent_node *en;
209
210 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
211 if (!en)
212 return NULL;
213
214 en->ei = *ei;
215 INIT_LIST_HEAD(&en->list);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000216 en->et = et;
Chao Yua28ef1f2015-07-08 17:59:36 +0800217
218 rb_link_node(&en->rb_node, parent, p);
Chao Yu4dada3f2018-10-04 11:18:30 +0800219 rb_insert_color_cached(&en->rb_node, &et->root, leftmost);
Chao Yu68e35382016-01-08 20:22:52 +0800220 atomic_inc(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800221 atomic_inc(&sbi->total_ext_node);
222 return en;
223}
224
225static void __detach_extent_node(struct f2fs_sb_info *sbi,
226 struct extent_tree *et, struct extent_node *en)
227{
Chao Yu4dada3f2018-10-04 11:18:30 +0800228 rb_erase_cached(&en->rb_node, &et->root);
Chao Yu68e35382016-01-08 20:22:52 +0800229 atomic_dec(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800230 atomic_dec(&sbi->total_ext_node);
231
232 if (et->cached_en == en)
233 et->cached_en = NULL;
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000234 kmem_cache_free(extent_node_slab, en);
235}
236
237/*
238 * Flow to release an extent_node:
239 * 1. list_del_init
240 * 2. __detach_extent_node
241 * 3. kmem_cache_free.
242 */
243static void __release_extent_node(struct f2fs_sb_info *sbi,
244 struct extent_tree *et, struct extent_node *en)
245{
246 spin_lock(&sbi->extent_lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000247 f2fs_bug_on(sbi, list_empty(&en->list));
248 list_del_init(&en->list);
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000249 spin_unlock(&sbi->extent_lock);
250
251 __detach_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +0800252}
253
254static struct extent_tree *__grab_extent_tree(struct inode *inode)
255{
256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
257 struct extent_tree *et;
258 nid_t ino = inode->i_ino;
259
Yunlei He5e8256a2017-02-23 19:39:59 +0800260 mutex_lock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800261 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
262 if (!et) {
263 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
264 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
265 memset(et, 0, sizeof(struct extent_tree));
266 et->ino = ino;
Chao Yu4dada3f2018-10-04 11:18:30 +0800267 et->root = RB_ROOT_CACHED;
Chao Yua28ef1f2015-07-08 17:59:36 +0800268 et->cached_en = NULL;
269 rwlock_init(&et->lock);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800270 INIT_LIST_HEAD(&et->list);
Chao Yu68e35382016-01-08 20:22:52 +0800271 atomic_set(&et->node_cnt, 0);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800272 atomic_inc(&sbi->total_ext_tree);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800273 } else {
274 atomic_dec(&sbi->total_zombie_tree);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800275 list_del_init(&et->list);
Chao Yua28ef1f2015-07-08 17:59:36 +0800276 }
Yunlei He5e8256a2017-02-23 19:39:59 +0800277 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800278
279 /* never died until evict_inode */
280 F2FS_I(inode)->extent_tree = et;
281
282 return et;
283}
284
Chao Yua6f78342015-08-19 19:14:15 +0800285static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
286 struct extent_tree *et, struct extent_info *ei)
Chao Yua28ef1f2015-07-08 17:59:36 +0800287{
Chao Yu4dada3f2018-10-04 11:18:30 +0800288 struct rb_node **p = &et->root.rb_root.rb_node;
Chao Yua28ef1f2015-07-08 17:59:36 +0800289 struct extent_node *en;
290
Chao Yu4dada3f2018-10-04 11:18:30 +0800291 en = __attach_extent_node(sbi, et, ei, NULL, p, true);
Chao Yua28ef1f2015-07-08 17:59:36 +0800292 if (!en)
293 return NULL;
Chao Yua6f78342015-08-19 19:14:15 +0800294
295 et->largest = en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +0800296 et->cached_en = en;
297 return en;
298}
299
300static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000301 struct extent_tree *et)
Chao Yua28ef1f2015-07-08 17:59:36 +0800302{
303 struct rb_node *node, *next;
304 struct extent_node *en;
Chao Yu68e35382016-01-08 20:22:52 +0800305 unsigned int count = atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800306
Chao Yu4dada3f2018-10-04 11:18:30 +0800307 node = rb_first_cached(&et->root);
Chao Yua28ef1f2015-07-08 17:59:36 +0800308 while (node) {
309 next = rb_next(node);
310 en = rb_entry(node, struct extent_node, rb_node);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000311 __release_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +0800312 node = next;
313 }
314
Chao Yu68e35382016-01-08 20:22:52 +0800315 return count - atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800316}
317
Zhikang Zhangb430f722018-09-10 16:18:25 +0800318static void __drop_largest_extent(struct extent_tree *et,
Fan Li41a099d2015-09-17 18:24:17 +0800319 pgoff_t fofs, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800320{
Zhikang Zhangb430f722018-09-10 16:18:25 +0800321 if (fofs < et->largest.fofs + et->largest.len &&
322 fofs + len > et->largest.fofs) {
323 et->largest.len = 0;
324 et->largest_updated = true;
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700325 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800326}
327
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800328/* return true, if inode page is changed */
Yunlei Hedad48e72017-05-19 15:06:12 +0800329static bool __f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
Chao Yua28ef1f2015-07-08 17:59:36 +0800330{
331 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
332 struct extent_tree *et;
333 struct extent_node *en;
334 struct extent_info ei;
335
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800336 if (!f2fs_may_extent_tree(inode)) {
337 /* drop largest extent */
338 if (i_ext && i_ext->len) {
339 i_ext->len = 0;
340 return true;
341 }
342 return false;
343 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800344
345 et = __grab_extent_tree(inode);
346
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800347 if (!i_ext || !i_ext->len)
348 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800349
Chao Yubd933d42016-05-04 23:19:47 +0800350 get_extent_info(&ei, i_ext);
Chao Yua28ef1f2015-07-08 17:59:36 +0800351
352 write_lock(&et->lock);
Chao Yu68e35382016-01-08 20:22:52 +0800353 if (atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800354 goto out;
355
Chao Yua6f78342015-08-19 19:14:15 +0800356 en = __init_extent_tree(sbi, et, &ei);
Chao Yua28ef1f2015-07-08 17:59:36 +0800357 if (en) {
358 spin_lock(&sbi->extent_lock);
359 list_add_tail(&en->list, &sbi->extent_list);
360 spin_unlock(&sbi->extent_lock);
361 }
362out:
363 write_unlock(&et->lock);
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800364 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800365}
366
Yunlei Hedad48e72017-05-19 15:06:12 +0800367bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
368{
369 bool ret = __f2fs_init_extent_tree(inode, i_ext);
370
371 if (!F2FS_I(inode)->extent_tree)
372 set_inode_flag(inode, FI_NO_EXTENT);
373
374 return ret;
375}
376
Chao Yua28ef1f2015-07-08 17:59:36 +0800377static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
378 struct extent_info *ei)
379{
380 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
381 struct extent_tree *et = F2FS_I(inode)->extent_tree;
382 struct extent_node *en;
383 bool ret = false;
384
385 f2fs_bug_on(sbi, !et);
386
387 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
388
389 read_lock(&et->lock);
390
391 if (et->largest.fofs <= pgofs &&
392 et->largest.fofs + et->largest.len > pgofs) {
393 *ei = et->largest;
394 ret = true;
Chao Yu91c481f2015-08-19 19:12:20 +0800395 stat_inc_largest_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800396 goto out;
397 }
398
Chao Yu4d57b862018-05-30 00:20:41 +0800399 en = (struct extent_node *)f2fs_lookup_rb_tree(&et->root,
Chao Yu54c22582017-04-11 09:25:22 +0800400 (struct rb_entry *)et->cached_en, pgofs);
401 if (!en)
402 goto out;
403
404 if (en == et->cached_en)
405 stat_inc_cached_node_hit(sbi);
406 else
407 stat_inc_rbtree_node_hit(sbi);
408
409 *ei = en->ei;
410 spin_lock(&sbi->extent_lock);
411 if (!list_empty(&en->list)) {
412 list_move_tail(&en->list, &sbi->extent_list);
413 et->cached_en = en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800414 }
Chao Yu54c22582017-04-11 09:25:22 +0800415 spin_unlock(&sbi->extent_lock);
416 ret = true;
Chao Yua28ef1f2015-07-08 17:59:36 +0800417out:
Chao Yu727edac2015-07-15 17:29:49 +0800418 stat_inc_total_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800419 read_unlock(&et->lock);
420
421 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
422 return ret;
423}
424
Zhikang Zhangb430f722018-09-10 16:18:25 +0800425static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
Fan Li0f825ee2015-07-15 18:05:17 +0800426 struct extent_tree *et, struct extent_info *ei,
Fan Li0f825ee2015-07-15 18:05:17 +0800427 struct extent_node *prev_ex,
Chao Yuef05e222015-08-19 19:15:09 +0800428 struct extent_node *next_ex)
Fan Li0f825ee2015-07-15 18:05:17 +0800429{
Fan Li0f825ee2015-07-15 18:05:17 +0800430 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800431
432 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800433 prev_ex->ei.len += ei->len;
434 ei = &prev_ex->ei;
435 en = prev_ex;
436 }
Chao Yuef05e222015-08-19 19:15:09 +0800437
Fan Li0f825ee2015-07-15 18:05:17 +0800438 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800439 next_ex->ei.fofs = ei->fofs;
440 next_ex->ei.blk = ei->blk;
441 next_ex->ei.len += ei->len;
Yunlei He7855eba2016-12-19 20:10:48 +0800442 if (en)
443 __release_extent_node(sbi, et, prev_ex);
444
Fan Li0f825ee2015-07-15 18:05:17 +0800445 en = next_ex;
446 }
Chao Yuef05e222015-08-19 19:15:09 +0800447
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800448 if (!en)
449 return NULL;
450
Zhikang Zhangb430f722018-09-10 16:18:25 +0800451 __try_update_largest_extent(et, en);
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800452
453 spin_lock(&sbi->extent_lock);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800454 if (!list_empty(&en->list)) {
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800455 list_move_tail(&en->list, &sbi->extent_list);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800456 et->cached_en = en;
457 }
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800458 spin_unlock(&sbi->extent_lock);
Chao Yuef05e222015-08-19 19:15:09 +0800459 return en;
460}
461
Zhikang Zhangb430f722018-09-10 16:18:25 +0800462static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
Chao Yuef05e222015-08-19 19:15:09 +0800463 struct extent_tree *et, struct extent_info *ei,
464 struct rb_node **insert_p,
Chao Yu4dada3f2018-10-04 11:18:30 +0800465 struct rb_node *insert_parent,
466 bool leftmost)
Chao Yuef05e222015-08-19 19:15:09 +0800467{
Colin Ian King8fe326c2018-02-21 18:13:40 +0000468 struct rb_node **p;
Chao Yuef05e222015-08-19 19:15:09 +0800469 struct rb_node *parent = NULL;
470 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800471
472 if (insert_p && insert_parent) {
473 parent = insert_parent;
474 p = insert_p;
475 goto do_insert;
476 }
477
Chao Yu4dada3f2018-10-04 11:18:30 +0800478 leftmost = true;
479
480 p = f2fs_lookup_rb_tree_for_insert(sbi, &et->root, &parent,
481 ei->fofs, &leftmost);
Fan Li0f825ee2015-07-15 18:05:17 +0800482do_insert:
Chao Yu4dada3f2018-10-04 11:18:30 +0800483 en = __attach_extent_node(sbi, et, ei, parent, p, leftmost);
Fan Li0f825ee2015-07-15 18:05:17 +0800484 if (!en)
485 return NULL;
Chao Yuef05e222015-08-19 19:15:09 +0800486
Zhikang Zhangb430f722018-09-10 16:18:25 +0800487 __try_update_largest_extent(et, en);
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800488
489 /* update in global extent list */
490 spin_lock(&sbi->extent_lock);
491 list_add_tail(&en->list, &sbi->extent_list);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800492 et->cached_en = en;
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800493 spin_unlock(&sbi->extent_lock);
Fan Li0f825ee2015-07-15 18:05:17 +0800494 return en;
495}
496
Chao Yu317e1302017-02-25 17:29:54 +0800497static void f2fs_update_extent_tree_range(struct inode *inode,
Chao Yu19b2c302015-08-26 20:34:48 +0800498 pgoff_t fofs, block_t blkaddr, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800499{
500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
501 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Fan Li4d1fa812015-09-17 18:42:06 +0800502 struct extent_node *en = NULL, *en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800503 struct extent_node *prev_en = NULL, *next_en = NULL;
Chao Yua28ef1f2015-07-08 17:59:36 +0800504 struct extent_info ei, dei, prev;
Fan Li0f825ee2015-07-15 18:05:17 +0800505 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800506 unsigned int end = fofs + len;
507 unsigned int pos = (unsigned int)fofs;
Zhikang Zhangb430f722018-09-10 16:18:25 +0800508 bool updated = false;
Chao Yu4dada3f2018-10-04 11:18:30 +0800509 bool leftmost;
Chao Yua28ef1f2015-07-08 17:59:36 +0800510
511 if (!et)
Chao Yu317e1302017-02-25 17:29:54 +0800512 return;
Chao Yua28ef1f2015-07-08 17:59:36 +0800513
Chao Yu744288c2015-09-06 17:50:13 +0800514 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
515
Chao Yua28ef1f2015-07-08 17:59:36 +0800516 write_lock(&et->lock);
517
Jaegeuk Kim91942322016-05-20 10:13:22 -0700518 if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800519 write_unlock(&et->lock);
Chao Yu317e1302017-02-25 17:29:54 +0800520 return;
Chao Yua28ef1f2015-07-08 17:59:36 +0800521 }
522
523 prev = et->largest;
524 dei.len = 0;
525
Fan Li4d1fa812015-09-17 18:42:06 +0800526 /*
527 * drop largest extent before lookup, in case it's already
528 * been shrunk from extent tree
529 */
Zhikang Zhangb430f722018-09-10 16:18:25 +0800530 __drop_largest_extent(et, fofs, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800531
Chao Yu19b2c302015-08-26 20:34:48 +0800532 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
Chao Yu4d57b862018-05-30 00:20:41 +0800533 en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root,
Chao Yu54c22582017-04-11 09:25:22 +0800534 (struct rb_entry *)et->cached_en, fofs,
535 (struct rb_entry **)&prev_en,
536 (struct rb_entry **)&next_en,
Chao Yu4dada3f2018-10-04 11:18:30 +0800537 &insert_p, &insert_parent, false,
538 &leftmost);
Fan Li4d1fa812015-09-17 18:42:06 +0800539 if (!en)
540 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800541
Chao Yu19b2c302015-08-26 20:34:48 +0800542 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
Fan Li4d1fa812015-09-17 18:42:06 +0800543 while (en && en->ei.fofs < end) {
544 unsigned int org_end;
545 int parts = 0; /* # of parts current extent split into */
Chao Yua28ef1f2015-07-08 17:59:36 +0800546
Fan Li4d1fa812015-09-17 18:42:06 +0800547 next_en = en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800548
549 dei = en->ei;
Fan Li4d1fa812015-09-17 18:42:06 +0800550 org_end = dei.fofs + dei.len;
551 f2fs_bug_on(sbi, pos >= org_end);
Chao Yu19b2c302015-08-26 20:34:48 +0800552
Fan Li4d1fa812015-09-17 18:42:06 +0800553 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
554 en->ei.len = pos - en->ei.fofs;
555 prev_en = en;
556 parts = 1;
Chao Yua28ef1f2015-07-08 17:59:36 +0800557 }
558
Fan Li4d1fa812015-09-17 18:42:06 +0800559 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
560 if (parts) {
Chao Yu19b2c302015-08-26 20:34:48 +0800561 set_extent_info(&ei, end,
562 end - dei.fofs + dei.blk,
Fan Li4d1fa812015-09-17 18:42:06 +0800563 org_end - end);
Zhikang Zhangb430f722018-09-10 16:18:25 +0800564 en1 = __insert_extent_tree(sbi, et, &ei,
Chao Yu4dada3f2018-10-04 11:18:30 +0800565 NULL, NULL, true);
Fan Li4d1fa812015-09-17 18:42:06 +0800566 next_en = en1;
567 } else {
568 en->ei.fofs = end;
569 en->ei.blk += end - dei.fofs;
570 en->ei.len -= end - dei.fofs;
571 next_en = en;
Chao Yu19b2c302015-08-26 20:34:48 +0800572 }
Fan Li4d1fa812015-09-17 18:42:06 +0800573 parts++;
Chao Yu19b2c302015-08-26 20:34:48 +0800574 }
Fan Li4d1fa812015-09-17 18:42:06 +0800575
576 if (!next_en) {
577 struct rb_node *node = rb_next(&en->rb_node);
578
Geliang Tanged0b5622016-12-20 21:57:42 +0800579 next_en = rb_entry_safe(node, struct extent_node,
580 rb_node);
Fan Li4d1fa812015-09-17 18:42:06 +0800581 }
582
Chao Yu4abd3f52015-09-22 21:07:47 +0800583 if (parts)
Zhikang Zhangb430f722018-09-10 16:18:25 +0800584 __try_update_largest_extent(et, en);
Chao Yu4abd3f52015-09-22 21:07:47 +0800585 else
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000586 __release_extent_node(sbi, et, en);
Fan Li4d1fa812015-09-17 18:42:06 +0800587
588 /*
589 * if original extent is split into zero or two parts, extent
590 * tree has been altered by deletion or insertion, therefore
591 * invalidate pointers regard to tree.
592 */
593 if (parts != 1) {
594 insert_p = NULL;
595 insert_parent = NULL;
596 }
Fan Li4d1fa812015-09-17 18:42:06 +0800597 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800598 }
599
Chao Yua28ef1f2015-07-08 17:59:36 +0800600 /* 3. update extent in extent cache */
601 if (blkaddr) {
Chao Yu19b2c302015-08-26 20:34:48 +0800602
603 set_extent_info(&ei, fofs, blkaddr, len);
Zhikang Zhangb430f722018-09-10 16:18:25 +0800604 if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
605 __insert_extent_tree(sbi, et, &ei,
Chao Yu4dada3f2018-10-04 11:18:30 +0800606 insert_p, insert_parent, leftmost);
Chao Yua28ef1f2015-07-08 17:59:36 +0800607
608 /* give up extent_cache, if split and small updates happen */
609 if (dei.len >= 1 &&
610 prev.len < F2FS_MIN_EXTENT_LEN &&
611 et->largest.len < F2FS_MIN_EXTENT_LEN) {
Zhikang Zhangb430f722018-09-10 16:18:25 +0800612 et->largest.len = 0;
613 et->largest_updated = true;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700614 set_inode_flag(inode, FI_NO_EXTENT);
Chao Yua28ef1f2015-07-08 17:59:36 +0800615 }
Chao Yu19b2c302015-08-26 20:34:48 +0800616 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800617
Jaegeuk Kim91942322016-05-20 10:13:22 -0700618 if (is_inode_flag_set(inode, FI_NO_EXTENT))
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000619 __free_extent_tree(sbi, et);
Chao Yua28ef1f2015-07-08 17:59:36 +0800620
Zhikang Zhangb430f722018-09-10 16:18:25 +0800621 if (et->largest_updated) {
622 et->largest_updated = false;
623 updated = true;
624 }
625
Chao Yua28ef1f2015-07-08 17:59:36 +0800626 write_unlock(&et->lock);
Zhikang Zhangb430f722018-09-10 16:18:25 +0800627
628 if (updated)
629 f2fs_mark_inode_dirty_sync(inode, true);
Chao Yua28ef1f2015-07-08 17:59:36 +0800630}
631
632unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
633{
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800634 struct extent_tree *et, *next;
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000635 struct extent_node *en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800636 unsigned int node_cnt = 0, tree_cnt = 0;
637 int remained;
638
639 if (!test_opt(sbi, EXTENT_CACHE))
640 return 0;
641
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800642 if (!atomic_read(&sbi->total_zombie_tree))
643 goto free_node;
644
Yunlei He5e8256a2017-02-23 19:39:59 +0800645 if (!mutex_trylock(&sbi->extent_tree_lock))
Chao Yua28ef1f2015-07-08 17:59:36 +0800646 goto out;
647
648 /* 1. remove unreferenced extent tree */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800649 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
Chao Yu9b72a382016-01-08 20:24:00 +0800650 if (atomic_read(&et->node_cnt)) {
651 write_lock(&et->lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000652 node_cnt += __free_extent_tree(sbi, et);
Chao Yu9b72a382016-01-08 20:24:00 +0800653 write_unlock(&et->lock);
654 }
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000655 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800656 list_del_init(&et->list);
657 radix_tree_delete(&sbi->extent_tree_root, et->ino);
658 kmem_cache_free(extent_tree_slab, et);
659 atomic_dec(&sbi->total_ext_tree);
660 atomic_dec(&sbi->total_zombie_tree);
661 tree_cnt++;
Chao Yua28ef1f2015-07-08 17:59:36 +0800662
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800663 if (node_cnt + tree_cnt >= nr_shrink)
664 goto unlock_out;
Jaegeuk Kim6fe2bc92016-01-20 07:31:48 +0800665 cond_resched();
Chao Yua28ef1f2015-07-08 17:59:36 +0800666 }
Yunlei He5e8256a2017-02-23 19:39:59 +0800667 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800668
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800669free_node:
Chao Yua28ef1f2015-07-08 17:59:36 +0800670 /* 2. remove LRU extent entries */
Yunlei He5e8256a2017-02-23 19:39:59 +0800671 if (!mutex_trylock(&sbi->extent_tree_lock))
Chao Yua28ef1f2015-07-08 17:59:36 +0800672 goto out;
673
674 remained = nr_shrink - (node_cnt + tree_cnt);
675
676 spin_lock(&sbi->extent_lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000677 for (; remained > 0; remained--) {
678 if (list_empty(&sbi->extent_list))
Chao Yua28ef1f2015-07-08 17:59:36 +0800679 break;
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000680 en = list_first_entry(&sbi->extent_list,
681 struct extent_node, list);
682 et = en->et;
683 if (!write_trylock(&et->lock)) {
684 /* refresh this extent node's position in extent list */
685 list_move_tail(&en->list, &sbi->extent_list);
686 continue;
687 }
688
Chao Yua28ef1f2015-07-08 17:59:36 +0800689 list_del_init(&en->list);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000690 spin_unlock(&sbi->extent_lock);
691
692 __detach_extent_node(sbi, et, en);
693
694 write_unlock(&et->lock);
695 node_cnt++;
696 spin_lock(&sbi->extent_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800697 }
698 spin_unlock(&sbi->extent_lock);
699
Chao Yua28ef1f2015-07-08 17:59:36 +0800700unlock_out:
Yunlei He5e8256a2017-02-23 19:39:59 +0800701 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800702out:
703 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
704
705 return node_cnt + tree_cnt;
706}
707
708unsigned int f2fs_destroy_extent_node(struct inode *inode)
709{
710 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
711 struct extent_tree *et = F2FS_I(inode)->extent_tree;
712 unsigned int node_cnt = 0;
713
Chao Yu9b72a382016-01-08 20:24:00 +0800714 if (!et || !atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800715 return 0;
716
717 write_lock(&et->lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000718 node_cnt = __free_extent_tree(sbi, et);
Chao Yua28ef1f2015-07-08 17:59:36 +0800719 write_unlock(&et->lock);
720
721 return node_cnt;
722}
723
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -0700724void f2fs_drop_extent_tree(struct inode *inode)
725{
726 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
727 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Zhikang Zhangb430f722018-09-10 16:18:25 +0800728 bool updated = false;
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -0700729
Chao Yubf617f72018-01-27 17:29:49 +0800730 if (!f2fs_may_extent_tree(inode))
731 return;
732
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -0700733 set_inode_flag(inode, FI_NO_EXTENT);
734
735 write_lock(&et->lock);
736 __free_extent_tree(sbi, et);
Zhikang Zhangb430f722018-09-10 16:18:25 +0800737 if (et->largest.len) {
738 et->largest.len = 0;
739 updated = true;
740 }
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -0700741 write_unlock(&et->lock);
Zhikang Zhangb430f722018-09-10 16:18:25 +0800742 if (updated)
743 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -0700744}
745
Chao Yua28ef1f2015-07-08 17:59:36 +0800746void f2fs_destroy_extent_tree(struct inode *inode)
747{
748 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
749 struct extent_tree *et = F2FS_I(inode)->extent_tree;
750 unsigned int node_cnt = 0;
751
752 if (!et)
753 return;
754
Chao Yu68e35382016-01-08 20:22:52 +0800755 if (inode->i_nlink && !is_bad_inode(inode) &&
756 atomic_read(&et->node_cnt)) {
Yunlei He5e8256a2017-02-23 19:39:59 +0800757 mutex_lock(&sbi->extent_tree_lock);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800758 list_add_tail(&et->list, &sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800759 atomic_inc(&sbi->total_zombie_tree);
Yunlei He5e8256a2017-02-23 19:39:59 +0800760 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800761 return;
762 }
763
764 /* free all extent info belong to this extent tree */
765 node_cnt = f2fs_destroy_extent_node(inode);
766
767 /* delete extent tree entry in radix tree */
Yunlei He5e8256a2017-02-23 19:39:59 +0800768 mutex_lock(&sbi->extent_tree_lock);
Chao Yu68e35382016-01-08 20:22:52 +0800769 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
Chao Yua28ef1f2015-07-08 17:59:36 +0800770 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
771 kmem_cache_free(extent_tree_slab, et);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800772 atomic_dec(&sbi->total_ext_tree);
Yunlei He5e8256a2017-02-23 19:39:59 +0800773 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800774
775 F2FS_I(inode)->extent_tree = NULL;
776
777 trace_f2fs_destroy_extent_tree(inode, node_cnt);
778}
779
780bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
781 struct extent_info *ei)
782{
783 if (!f2fs_may_extent_tree(inode))
784 return false;
785
786 return f2fs_lookup_extent_tree(inode, pgofs, ei);
787}
788
789void f2fs_update_extent_cache(struct dnode_of_data *dn)
790{
Chao Yua28ef1f2015-07-08 17:59:36 +0800791 pgoff_t fofs;
Chao Yuf28b3432016-02-24 17:16:47 +0800792 block_t blkaddr;
Chao Yua28ef1f2015-07-08 17:59:36 +0800793
794 if (!f2fs_may_extent_tree(dn->inode))
795 return;
796
Chao Yuf28b3432016-02-24 17:16:47 +0800797 if (dn->data_blkaddr == NEW_ADDR)
798 blkaddr = NULL_ADDR;
799 else
800 blkaddr = dn->data_blkaddr;
Chao Yu19b2c302015-08-26 20:34:48 +0800801
Chao Yu4d57b862018-05-30 00:20:41 +0800802 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Chao Yu81ca7352016-01-26 15:39:35 +0800803 dn->ofs_in_node;
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700804 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
Chao Yu19b2c302015-08-26 20:34:48 +0800805}
806
807void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
808 pgoff_t fofs, block_t blkaddr, unsigned int len)
809
810{
811 if (!f2fs_may_extent_tree(dn->inode))
812 return;
813
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700814 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800815}
816
Chao Yu4d57b862018-05-30 00:20:41 +0800817void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
Chao Yua28ef1f2015-07-08 17:59:36 +0800818{
819 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
Yunlei He5e8256a2017-02-23 19:39:59 +0800820 mutex_init(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800821 INIT_LIST_HEAD(&sbi->extent_list);
822 spin_lock_init(&sbi->extent_lock);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800823 atomic_set(&sbi->total_ext_tree, 0);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800824 INIT_LIST_HEAD(&sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800825 atomic_set(&sbi->total_zombie_tree, 0);
Chao Yua28ef1f2015-07-08 17:59:36 +0800826 atomic_set(&sbi->total_ext_node, 0);
827}
828
Chao Yu4d57b862018-05-30 00:20:41 +0800829int __init f2fs_create_extent_cache(void)
Chao Yua28ef1f2015-07-08 17:59:36 +0800830{
831 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
832 sizeof(struct extent_tree));
833 if (!extent_tree_slab)
834 return -ENOMEM;
835 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
836 sizeof(struct extent_node));
837 if (!extent_node_slab) {
838 kmem_cache_destroy(extent_tree_slab);
839 return -ENOMEM;
840 }
841 return 0;
842}
843
Chao Yu4d57b862018-05-30 00:20:41 +0800844void f2fs_destroy_extent_cache(void)
Chao Yua28ef1f2015-07-08 17:59:36 +0800845{
846 kmem_cache_destroy(extent_node_slab);
847 kmem_cache_destroy(extent_tree_slab);
848}