blob: ff2352a0ed157c21b14a3b55d880e927827cb2a2 [file] [log] [blame]
Chao Yua28ef1f2015-07-08 17:59:36 +08001/*
2 * f2fs extent cache support
3 *
4 * Copyright (c) 2015 Motorola Mobility
5 * Copyright (c) 2015 Samsung Electronics
6 * Authors: Jaegeuk Kim <jaegeuk@kernel.org>
7 * Chao Yu <chao2.yu@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/fs.h>
15#include <linux/f2fs_fs.h>
16
17#include "f2fs.h"
18#include "node.h"
19#include <trace/events/f2fs.h>
20
Chao Yu54c22582017-04-11 09:25:22 +080021static struct rb_entry *__lookup_rb_tree_fast(struct rb_entry *cached_re,
22 unsigned int ofs)
23{
24 if (cached_re) {
25 if (cached_re->ofs <= ofs &&
26 cached_re->ofs + cached_re->len > ofs) {
27 return cached_re;
28 }
29 }
30 return NULL;
31}
32
33static struct rb_entry *__lookup_rb_tree_slow(struct rb_root *root,
34 unsigned int ofs)
35{
36 struct rb_node *node = root->rb_node;
37 struct rb_entry *re;
38
39 while (node) {
40 re = rb_entry(node, struct rb_entry, rb_node);
41
42 if (ofs < re->ofs)
43 node = node->rb_left;
44 else if (ofs >= re->ofs + re->len)
45 node = node->rb_right;
46 else
47 return re;
48 }
49 return NULL;
50}
51
Chao Yu004b6862017-04-14 23:24:55 +080052struct rb_entry *__lookup_rb_tree(struct rb_root *root,
Chao Yu54c22582017-04-11 09:25:22 +080053 struct rb_entry *cached_re, unsigned int ofs)
54{
55 struct rb_entry *re;
56
57 re = __lookup_rb_tree_fast(cached_re, ofs);
58 if (!re)
59 return __lookup_rb_tree_slow(root, ofs);
60
61 return re;
62}
63
Chao Yu004b6862017-04-14 23:24:55 +080064struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
Chao Yu54c22582017-04-11 09:25:22 +080065 struct rb_root *root, struct rb_node **parent,
66 unsigned int ofs)
67{
68 struct rb_node **p = &root->rb_node;
69 struct rb_entry *re;
70
71 while (*p) {
72 *parent = *p;
73 re = rb_entry(*parent, struct rb_entry, rb_node);
74
75 if (ofs < re->ofs)
76 p = &(*p)->rb_left;
77 else if (ofs >= re->ofs + re->len)
78 p = &(*p)->rb_right;
79 else
80 f2fs_bug_on(sbi, 1);
81 }
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 Yu004b6862017-04-14 23:24:55 +080095struct rb_entry *__lookup_rb_tree_ret(struct rb_root *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,
102 bool force)
Chao Yu54c22582017-04-11 09:25:22 +0800103{
104 struct rb_node **pnode = &root->rb_node;
105 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
113 if (RB_EMPTY_ROOT(root))
114 return NULL;
115
116 if (re) {
117 if (re->ofs <= ofs && re->ofs + re->len > ofs)
118 goto lookup_neighbors;
119 }
120
121 while (*pnode) {
122 parent = *pnode;
123 re = rb_entry(*pnode, struct rb_entry, rb_node);
124
125 if (ofs < re->ofs)
126 pnode = &(*pnode)->rb_left;
127 else if (ofs >= re->ofs + re->len)
128 pnode = &(*pnode)->rb_right;
129 else
130 goto lookup_neighbors;
131 }
132
133 *insert_p = pnode;
134 *insert_parent = parent;
135
136 re = rb_entry(parent, struct rb_entry, rb_node);
137 tmp_node = parent;
138 if (parent && ofs > re->ofs)
139 tmp_node = rb_next(parent);
140 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
141
142 tmp_node = parent;
143 if (parent && ofs < re->ofs)
144 tmp_node = rb_prev(parent);
145 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
146 return NULL;
147
148lookup_neighbors:
Chao Yu004b6862017-04-14 23:24:55 +0800149 if (ofs == re->ofs || force) {
Chao Yu54c22582017-04-11 09:25:22 +0800150 /* lookup prev node for merging backward later */
151 tmp_node = rb_prev(&re->rb_node);
152 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
153 }
Chao Yu004b6862017-04-14 23:24:55 +0800154 if (ofs == re->ofs + re->len - 1 || force) {
Chao Yu54c22582017-04-11 09:25:22 +0800155 /* lookup next node for merging frontward later */
156 tmp_node = rb_next(&re->rb_node);
157 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
158 }
159 return re;
160}
161
Chao Yudf0f6b42017-04-17 18:21:43 +0800162bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
163 struct rb_root *root)
164{
165#ifdef CONFIG_F2FS_CHECK_FS
166 struct rb_node *cur = rb_first(root), *next;
167 struct rb_entry *cur_re, *next_re;
168
169 if (!cur)
170 return true;
171
172 while (cur) {
173 next = rb_next(cur);
174 if (!next)
175 return true;
176
177 cur_re = rb_entry(cur, struct rb_entry, rb_node);
178 next_re = rb_entry(next, struct rb_entry, rb_node);
179
180 if (cur_re->ofs + cur_re->len > next_re->ofs) {
181 f2fs_msg(sbi->sb, KERN_INFO, "inconsistent rbtree, "
182 "cur(%u, %u) next(%u, %u)",
183 cur_re->ofs, cur_re->len,
184 next_re->ofs, next_re->len);
185 return false;
186 }
187
188 cur = next;
189 }
190#endif
191 return true;
192}
193
Chao Yua28ef1f2015-07-08 17:59:36 +0800194static struct kmem_cache *extent_tree_slab;
195static struct kmem_cache *extent_node_slab;
196
197static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
198 struct extent_tree *et, struct extent_info *ei,
199 struct rb_node *parent, struct rb_node **p)
200{
201 struct extent_node *en;
202
203 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
204 if (!en)
205 return NULL;
206
207 en->ei = *ei;
208 INIT_LIST_HEAD(&en->list);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000209 en->et = et;
Chao Yua28ef1f2015-07-08 17:59:36 +0800210
211 rb_link_node(&en->rb_node, parent, p);
212 rb_insert_color(&en->rb_node, &et->root);
Chao Yu68e35382016-01-08 20:22:52 +0800213 atomic_inc(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800214 atomic_inc(&sbi->total_ext_node);
215 return en;
216}
217
218static void __detach_extent_node(struct f2fs_sb_info *sbi,
219 struct extent_tree *et, struct extent_node *en)
220{
221 rb_erase(&en->rb_node, &et->root);
Chao Yu68e35382016-01-08 20:22:52 +0800222 atomic_dec(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800223 atomic_dec(&sbi->total_ext_node);
224
225 if (et->cached_en == en)
226 et->cached_en = NULL;
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000227 kmem_cache_free(extent_node_slab, en);
228}
229
230/*
231 * Flow to release an extent_node:
232 * 1. list_del_init
233 * 2. __detach_extent_node
234 * 3. kmem_cache_free.
235 */
236static void __release_extent_node(struct f2fs_sb_info *sbi,
237 struct extent_tree *et, struct extent_node *en)
238{
239 spin_lock(&sbi->extent_lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000240 f2fs_bug_on(sbi, list_empty(&en->list));
241 list_del_init(&en->list);
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000242 spin_unlock(&sbi->extent_lock);
243
244 __detach_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +0800245}
246
247static struct extent_tree *__grab_extent_tree(struct inode *inode)
248{
249 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
250 struct extent_tree *et;
251 nid_t ino = inode->i_ino;
252
Yunlei He5e8256a2017-02-23 19:39:59 +0800253 mutex_lock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800254 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
255 if (!et) {
256 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
257 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
258 memset(et, 0, sizeof(struct extent_tree));
259 et->ino = ino;
260 et->root = RB_ROOT;
261 et->cached_en = NULL;
262 rwlock_init(&et->lock);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800263 INIT_LIST_HEAD(&et->list);
Chao Yu68e35382016-01-08 20:22:52 +0800264 atomic_set(&et->node_cnt, 0);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800265 atomic_inc(&sbi->total_ext_tree);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800266 } else {
267 atomic_dec(&sbi->total_zombie_tree);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800268 list_del_init(&et->list);
Chao Yua28ef1f2015-07-08 17:59:36 +0800269 }
Yunlei He5e8256a2017-02-23 19:39:59 +0800270 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800271
272 /* never died until evict_inode */
273 F2FS_I(inode)->extent_tree = et;
274
275 return et;
276}
277
Chao Yua6f78342015-08-19 19:14:15 +0800278static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
279 struct extent_tree *et, struct extent_info *ei)
Chao Yua28ef1f2015-07-08 17:59:36 +0800280{
281 struct rb_node **p = &et->root.rb_node;
Chao Yua28ef1f2015-07-08 17:59:36 +0800282 struct extent_node *en;
283
Chao Yua6f78342015-08-19 19:14:15 +0800284 en = __attach_extent_node(sbi, et, ei, NULL, p);
Chao Yua28ef1f2015-07-08 17:59:36 +0800285 if (!en)
286 return NULL;
Chao Yua6f78342015-08-19 19:14:15 +0800287
288 et->largest = en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +0800289 et->cached_en = en;
290 return en;
291}
292
293static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000294 struct extent_tree *et)
Chao Yua28ef1f2015-07-08 17:59:36 +0800295{
296 struct rb_node *node, *next;
297 struct extent_node *en;
Chao Yu68e35382016-01-08 20:22:52 +0800298 unsigned int count = atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800299
300 node = rb_first(&et->root);
301 while (node) {
302 next = rb_next(node);
303 en = rb_entry(node, struct extent_node, rb_node);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000304 __release_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +0800305 node = next;
306 }
307
Chao Yu68e35382016-01-08 20:22:52 +0800308 return count - atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800309}
310
Fan Li41a099d2015-09-17 18:24:17 +0800311static void __drop_largest_extent(struct inode *inode,
312 pgoff_t fofs, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800313{
314 struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
315
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700316 if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800317 largest->len = 0;
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700318 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700319 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800320}
321
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800322/* return true, if inode page is changed */
Yunlei Hedad48e72017-05-19 15:06:12 +0800323static bool __f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
Chao Yua28ef1f2015-07-08 17:59:36 +0800324{
325 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
326 struct extent_tree *et;
327 struct extent_node *en;
328 struct extent_info ei;
329
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800330 if (!f2fs_may_extent_tree(inode)) {
331 /* drop largest extent */
332 if (i_ext && i_ext->len) {
333 i_ext->len = 0;
334 return true;
335 }
336 return false;
337 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800338
339 et = __grab_extent_tree(inode);
340
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800341 if (!i_ext || !i_ext->len)
342 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800343
Chao Yubd933d42016-05-04 23:19:47 +0800344 get_extent_info(&ei, i_ext);
Chao Yua28ef1f2015-07-08 17:59:36 +0800345
346 write_lock(&et->lock);
Chao Yu68e35382016-01-08 20:22:52 +0800347 if (atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800348 goto out;
349
Chao Yua6f78342015-08-19 19:14:15 +0800350 en = __init_extent_tree(sbi, et, &ei);
Chao Yua28ef1f2015-07-08 17:59:36 +0800351 if (en) {
352 spin_lock(&sbi->extent_lock);
353 list_add_tail(&en->list, &sbi->extent_list);
354 spin_unlock(&sbi->extent_lock);
355 }
356out:
357 write_unlock(&et->lock);
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800358 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800359}
360
Yunlei Hedad48e72017-05-19 15:06:12 +0800361bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
362{
363 bool ret = __f2fs_init_extent_tree(inode, i_ext);
364
365 if (!F2FS_I(inode)->extent_tree)
366 set_inode_flag(inode, FI_NO_EXTENT);
367
368 return ret;
369}
370
Chao Yua28ef1f2015-07-08 17:59:36 +0800371static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
372 struct extent_info *ei)
373{
374 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
375 struct extent_tree *et = F2FS_I(inode)->extent_tree;
376 struct extent_node *en;
377 bool ret = false;
378
379 f2fs_bug_on(sbi, !et);
380
381 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
382
383 read_lock(&et->lock);
384
385 if (et->largest.fofs <= pgofs &&
386 et->largest.fofs + et->largest.len > pgofs) {
387 *ei = et->largest;
388 ret = true;
Chao Yu91c481f2015-08-19 19:12:20 +0800389 stat_inc_largest_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800390 goto out;
391 }
392
Chao Yu54c22582017-04-11 09:25:22 +0800393 en = (struct extent_node *)__lookup_rb_tree(&et->root,
394 (struct rb_entry *)et->cached_en, pgofs);
395 if (!en)
396 goto out;
397
398 if (en == et->cached_en)
399 stat_inc_cached_node_hit(sbi);
400 else
401 stat_inc_rbtree_node_hit(sbi);
402
403 *ei = en->ei;
404 spin_lock(&sbi->extent_lock);
405 if (!list_empty(&en->list)) {
406 list_move_tail(&en->list, &sbi->extent_list);
407 et->cached_en = en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800408 }
Chao Yu54c22582017-04-11 09:25:22 +0800409 spin_unlock(&sbi->extent_lock);
410 ret = true;
Chao Yua28ef1f2015-07-08 17:59:36 +0800411out:
Chao Yu727edac2015-07-15 17:29:49 +0800412 stat_inc_total_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800413 read_unlock(&et->lock);
414
415 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
416 return ret;
417}
418
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700419static struct extent_node *__try_merge_extent_node(struct inode *inode,
Fan Li0f825ee2015-07-15 18:05:17 +0800420 struct extent_tree *et, struct extent_info *ei,
Fan Li0f825ee2015-07-15 18:05:17 +0800421 struct extent_node *prev_ex,
Chao Yuef05e222015-08-19 19:15:09 +0800422 struct extent_node *next_ex)
Fan Li0f825ee2015-07-15 18:05:17 +0800423{
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700424 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Fan Li0f825ee2015-07-15 18:05:17 +0800425 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800426
427 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800428 prev_ex->ei.len += ei->len;
429 ei = &prev_ex->ei;
430 en = prev_ex;
431 }
Chao Yuef05e222015-08-19 19:15:09 +0800432
Fan Li0f825ee2015-07-15 18:05:17 +0800433 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800434 next_ex->ei.fofs = ei->fofs;
435 next_ex->ei.blk = ei->blk;
436 next_ex->ei.len += ei->len;
Yunlei He7855eba2016-12-19 20:10:48 +0800437 if (en)
438 __release_extent_node(sbi, et, prev_ex);
439
Fan Li0f825ee2015-07-15 18:05:17 +0800440 en = next_ex;
441 }
Chao Yuef05e222015-08-19 19:15:09 +0800442
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800443 if (!en)
444 return NULL;
445
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700446 __try_update_largest_extent(inode, et, en);
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800447
448 spin_lock(&sbi->extent_lock);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800449 if (!list_empty(&en->list)) {
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800450 list_move_tail(&en->list, &sbi->extent_list);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800451 et->cached_en = en;
452 }
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800453 spin_unlock(&sbi->extent_lock);
Chao Yuef05e222015-08-19 19:15:09 +0800454 return en;
455}
456
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700457static struct extent_node *__insert_extent_tree(struct inode *inode,
Chao Yuef05e222015-08-19 19:15:09 +0800458 struct extent_tree *et, struct extent_info *ei,
459 struct rb_node **insert_p,
460 struct rb_node *insert_parent)
461{
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700462 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuef05e222015-08-19 19:15:09 +0800463 struct rb_node **p = &et->root.rb_node;
464 struct rb_node *parent = NULL;
465 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800466
467 if (insert_p && insert_parent) {
468 parent = insert_parent;
469 p = insert_p;
470 goto do_insert;
471 }
472
Chao Yu54c22582017-04-11 09:25:22 +0800473 p = __lookup_rb_tree_for_insert(sbi, &et->root, &parent, ei->fofs);
Fan Li0f825ee2015-07-15 18:05:17 +0800474do_insert:
475 en = __attach_extent_node(sbi, et, ei, parent, p);
476 if (!en)
477 return NULL;
Chao Yuef05e222015-08-19 19:15:09 +0800478
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700479 __try_update_largest_extent(inode, et, en);
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800480
481 /* update in global extent list */
482 spin_lock(&sbi->extent_lock);
483 list_add_tail(&en->list, &sbi->extent_list);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800484 et->cached_en = en;
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800485 spin_unlock(&sbi->extent_lock);
Fan Li0f825ee2015-07-15 18:05:17 +0800486 return en;
487}
488
Chao Yu317e1302017-02-25 17:29:54 +0800489static void f2fs_update_extent_tree_range(struct inode *inode,
Chao Yu19b2c302015-08-26 20:34:48 +0800490 pgoff_t fofs, block_t blkaddr, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800491{
492 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
493 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Fan Li4d1fa812015-09-17 18:42:06 +0800494 struct extent_node *en = NULL, *en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800495 struct extent_node *prev_en = NULL, *next_en = NULL;
Chao Yua28ef1f2015-07-08 17:59:36 +0800496 struct extent_info ei, dei, prev;
Fan Li0f825ee2015-07-15 18:05:17 +0800497 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800498 unsigned int end = fofs + len;
499 unsigned int pos = (unsigned int)fofs;
Chao Yua28ef1f2015-07-08 17:59:36 +0800500
501 if (!et)
Chao Yu317e1302017-02-25 17:29:54 +0800502 return;
Chao Yua28ef1f2015-07-08 17:59:36 +0800503
Chao Yu744288c2015-09-06 17:50:13 +0800504 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
505
Chao Yua28ef1f2015-07-08 17:59:36 +0800506 write_lock(&et->lock);
507
Jaegeuk Kim91942322016-05-20 10:13:22 -0700508 if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800509 write_unlock(&et->lock);
Chao Yu317e1302017-02-25 17:29:54 +0800510 return;
Chao Yua28ef1f2015-07-08 17:59:36 +0800511 }
512
513 prev = et->largest;
514 dei.len = 0;
515
Fan Li4d1fa812015-09-17 18:42:06 +0800516 /*
517 * drop largest extent before lookup, in case it's already
518 * been shrunk from extent tree
519 */
Fan Li41a099d2015-09-17 18:24:17 +0800520 __drop_largest_extent(inode, fofs, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800521
Chao Yu19b2c302015-08-26 20:34:48 +0800522 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
Chao Yu54c22582017-04-11 09:25:22 +0800523 en = (struct extent_node *)__lookup_rb_tree_ret(&et->root,
524 (struct rb_entry *)et->cached_en, fofs,
525 (struct rb_entry **)&prev_en,
526 (struct rb_entry **)&next_en,
Chao Yu004b6862017-04-14 23:24:55 +0800527 &insert_p, &insert_parent, false);
Fan Li4d1fa812015-09-17 18:42:06 +0800528 if (!en)
529 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800530
Chao Yu19b2c302015-08-26 20:34:48 +0800531 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
Fan Li4d1fa812015-09-17 18:42:06 +0800532 while (en && en->ei.fofs < end) {
533 unsigned int org_end;
534 int parts = 0; /* # of parts current extent split into */
Chao Yua28ef1f2015-07-08 17:59:36 +0800535
Fan Li4d1fa812015-09-17 18:42:06 +0800536 next_en = en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800537
538 dei = en->ei;
Fan Li4d1fa812015-09-17 18:42:06 +0800539 org_end = dei.fofs + dei.len;
540 f2fs_bug_on(sbi, pos >= org_end);
Chao Yu19b2c302015-08-26 20:34:48 +0800541
Fan Li4d1fa812015-09-17 18:42:06 +0800542 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
543 en->ei.len = pos - en->ei.fofs;
544 prev_en = en;
545 parts = 1;
Chao Yua28ef1f2015-07-08 17:59:36 +0800546 }
547
Fan Li4d1fa812015-09-17 18:42:06 +0800548 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
549 if (parts) {
Chao Yu19b2c302015-08-26 20:34:48 +0800550 set_extent_info(&ei, end,
551 end - dei.fofs + dei.blk,
Fan Li4d1fa812015-09-17 18:42:06 +0800552 org_end - end);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700553 en1 = __insert_extent_tree(inode, et, &ei,
Fan Li4d1fa812015-09-17 18:42:06 +0800554 NULL, NULL);
555 next_en = en1;
556 } else {
557 en->ei.fofs = end;
558 en->ei.blk += end - dei.fofs;
559 en->ei.len -= end - dei.fofs;
560 next_en = en;
Chao Yu19b2c302015-08-26 20:34:48 +0800561 }
Fan Li4d1fa812015-09-17 18:42:06 +0800562 parts++;
Chao Yu19b2c302015-08-26 20:34:48 +0800563 }
Fan Li4d1fa812015-09-17 18:42:06 +0800564
565 if (!next_en) {
566 struct rb_node *node = rb_next(&en->rb_node);
567
Geliang Tanged0b5622016-12-20 21:57:42 +0800568 next_en = rb_entry_safe(node, struct extent_node,
569 rb_node);
Fan Li4d1fa812015-09-17 18:42:06 +0800570 }
571
Chao Yu4abd3f52015-09-22 21:07:47 +0800572 if (parts)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700573 __try_update_largest_extent(inode, et, en);
Chao Yu4abd3f52015-09-22 21:07:47 +0800574 else
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000575 __release_extent_node(sbi, et, en);
Fan Li4d1fa812015-09-17 18:42:06 +0800576
577 /*
578 * if original extent is split into zero or two parts, extent
579 * tree has been altered by deletion or insertion, therefore
580 * invalidate pointers regard to tree.
581 */
582 if (parts != 1) {
583 insert_p = NULL;
584 insert_parent = NULL;
585 }
Fan Li4d1fa812015-09-17 18:42:06 +0800586 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800587 }
588
Chao Yua28ef1f2015-07-08 17:59:36 +0800589 /* 3. update extent in extent cache */
590 if (blkaddr) {
Chao Yu19b2c302015-08-26 20:34:48 +0800591
592 set_extent_info(&ei, fofs, blkaddr, len);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700593 if (!__try_merge_extent_node(inode, et, &ei, prev_en, next_en))
594 __insert_extent_tree(inode, et, &ei,
Chao Yuef05e222015-08-19 19:15:09 +0800595 insert_p, insert_parent);
Chao Yua28ef1f2015-07-08 17:59:36 +0800596
597 /* give up extent_cache, if split and small updates happen */
598 if (dei.len >= 1 &&
599 prev.len < F2FS_MIN_EXTENT_LEN &&
600 et->largest.len < F2FS_MIN_EXTENT_LEN) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700601 __drop_largest_extent(inode, 0, UINT_MAX);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700602 set_inode_flag(inode, FI_NO_EXTENT);
Chao Yua28ef1f2015-07-08 17:59:36 +0800603 }
Chao Yu19b2c302015-08-26 20:34:48 +0800604 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800605
Jaegeuk Kim91942322016-05-20 10:13:22 -0700606 if (is_inode_flag_set(inode, FI_NO_EXTENT))
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000607 __free_extent_tree(sbi, et);
Chao Yua28ef1f2015-07-08 17:59:36 +0800608
609 write_unlock(&et->lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800610}
611
612unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
613{
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800614 struct extent_tree *et, *next;
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000615 struct extent_node *en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800616 unsigned int node_cnt = 0, tree_cnt = 0;
617 int remained;
618
619 if (!test_opt(sbi, EXTENT_CACHE))
620 return 0;
621
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800622 if (!atomic_read(&sbi->total_zombie_tree))
623 goto free_node;
624
Yunlei He5e8256a2017-02-23 19:39:59 +0800625 if (!mutex_trylock(&sbi->extent_tree_lock))
Chao Yua28ef1f2015-07-08 17:59:36 +0800626 goto out;
627
628 /* 1. remove unreferenced extent tree */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800629 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
Chao Yu9b72a382016-01-08 20:24:00 +0800630 if (atomic_read(&et->node_cnt)) {
631 write_lock(&et->lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000632 node_cnt += __free_extent_tree(sbi, et);
Chao Yu9b72a382016-01-08 20:24:00 +0800633 write_unlock(&et->lock);
634 }
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000635 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800636 list_del_init(&et->list);
637 radix_tree_delete(&sbi->extent_tree_root, et->ino);
638 kmem_cache_free(extent_tree_slab, et);
639 atomic_dec(&sbi->total_ext_tree);
640 atomic_dec(&sbi->total_zombie_tree);
641 tree_cnt++;
Chao Yua28ef1f2015-07-08 17:59:36 +0800642
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800643 if (node_cnt + tree_cnt >= nr_shrink)
644 goto unlock_out;
Jaegeuk Kim6fe2bc92016-01-20 07:31:48 +0800645 cond_resched();
Chao Yua28ef1f2015-07-08 17:59:36 +0800646 }
Yunlei He5e8256a2017-02-23 19:39:59 +0800647 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800648
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800649free_node:
Chao Yua28ef1f2015-07-08 17:59:36 +0800650 /* 2. remove LRU extent entries */
Yunlei He5e8256a2017-02-23 19:39:59 +0800651 if (!mutex_trylock(&sbi->extent_tree_lock))
Chao Yua28ef1f2015-07-08 17:59:36 +0800652 goto out;
653
654 remained = nr_shrink - (node_cnt + tree_cnt);
655
656 spin_lock(&sbi->extent_lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000657 for (; remained > 0; remained--) {
658 if (list_empty(&sbi->extent_list))
Chao Yua28ef1f2015-07-08 17:59:36 +0800659 break;
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000660 en = list_first_entry(&sbi->extent_list,
661 struct extent_node, list);
662 et = en->et;
663 if (!write_trylock(&et->lock)) {
664 /* refresh this extent node's position in extent list */
665 list_move_tail(&en->list, &sbi->extent_list);
666 continue;
667 }
668
Chao Yua28ef1f2015-07-08 17:59:36 +0800669 list_del_init(&en->list);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000670 spin_unlock(&sbi->extent_lock);
671
672 __detach_extent_node(sbi, et, en);
673
674 write_unlock(&et->lock);
675 node_cnt++;
676 spin_lock(&sbi->extent_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800677 }
678 spin_unlock(&sbi->extent_lock);
679
Chao Yua28ef1f2015-07-08 17:59:36 +0800680unlock_out:
Yunlei He5e8256a2017-02-23 19:39:59 +0800681 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800682out:
683 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
684
685 return node_cnt + tree_cnt;
686}
687
688unsigned int f2fs_destroy_extent_node(struct inode *inode)
689{
690 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
691 struct extent_tree *et = F2FS_I(inode)->extent_tree;
692 unsigned int node_cnt = 0;
693
Chao Yu9b72a382016-01-08 20:24:00 +0800694 if (!et || !atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800695 return 0;
696
697 write_lock(&et->lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000698 node_cnt = __free_extent_tree(sbi, et);
Chao Yua28ef1f2015-07-08 17:59:36 +0800699 write_unlock(&et->lock);
700
701 return node_cnt;
702}
703
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -0700704void f2fs_drop_extent_tree(struct inode *inode)
705{
706 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
707 struct extent_tree *et = F2FS_I(inode)->extent_tree;
708
709 set_inode_flag(inode, FI_NO_EXTENT);
710
711 write_lock(&et->lock);
712 __free_extent_tree(sbi, et);
713 __drop_largest_extent(inode, 0, UINT_MAX);
714 write_unlock(&et->lock);
715}
716
Chao Yua28ef1f2015-07-08 17:59:36 +0800717void f2fs_destroy_extent_tree(struct inode *inode)
718{
719 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
720 struct extent_tree *et = F2FS_I(inode)->extent_tree;
721 unsigned int node_cnt = 0;
722
723 if (!et)
724 return;
725
Chao Yu68e35382016-01-08 20:22:52 +0800726 if (inode->i_nlink && !is_bad_inode(inode) &&
727 atomic_read(&et->node_cnt)) {
Yunlei He5e8256a2017-02-23 19:39:59 +0800728 mutex_lock(&sbi->extent_tree_lock);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800729 list_add_tail(&et->list, &sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800730 atomic_inc(&sbi->total_zombie_tree);
Yunlei He5e8256a2017-02-23 19:39:59 +0800731 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800732 return;
733 }
734
735 /* free all extent info belong to this extent tree */
736 node_cnt = f2fs_destroy_extent_node(inode);
737
738 /* delete extent tree entry in radix tree */
Yunlei He5e8256a2017-02-23 19:39:59 +0800739 mutex_lock(&sbi->extent_tree_lock);
Chao Yu68e35382016-01-08 20:22:52 +0800740 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
Chao Yua28ef1f2015-07-08 17:59:36 +0800741 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
742 kmem_cache_free(extent_tree_slab, et);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800743 atomic_dec(&sbi->total_ext_tree);
Yunlei He5e8256a2017-02-23 19:39:59 +0800744 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800745
746 F2FS_I(inode)->extent_tree = NULL;
747
748 trace_f2fs_destroy_extent_tree(inode, node_cnt);
749}
750
751bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
752 struct extent_info *ei)
753{
754 if (!f2fs_may_extent_tree(inode))
755 return false;
756
757 return f2fs_lookup_extent_tree(inode, pgofs, ei);
758}
759
760void f2fs_update_extent_cache(struct dnode_of_data *dn)
761{
Chao Yua28ef1f2015-07-08 17:59:36 +0800762 pgoff_t fofs;
Chao Yuf28b3432016-02-24 17:16:47 +0800763 block_t blkaddr;
Chao Yua28ef1f2015-07-08 17:59:36 +0800764
765 if (!f2fs_may_extent_tree(dn->inode))
766 return;
767
Chao Yuf28b3432016-02-24 17:16:47 +0800768 if (dn->data_blkaddr == NEW_ADDR)
769 blkaddr = NULL_ADDR;
770 else
771 blkaddr = dn->data_blkaddr;
Chao Yu19b2c302015-08-26 20:34:48 +0800772
Chao Yu81ca7352016-01-26 15:39:35 +0800773 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
774 dn->ofs_in_node;
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700775 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
Chao Yu19b2c302015-08-26 20:34:48 +0800776}
777
778void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
779 pgoff_t fofs, block_t blkaddr, unsigned int len)
780
781{
782 if (!f2fs_may_extent_tree(dn->inode))
783 return;
784
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700785 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800786}
787
788void init_extent_cache_info(struct f2fs_sb_info *sbi)
789{
790 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
Yunlei He5e8256a2017-02-23 19:39:59 +0800791 mutex_init(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800792 INIT_LIST_HEAD(&sbi->extent_list);
793 spin_lock_init(&sbi->extent_lock);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800794 atomic_set(&sbi->total_ext_tree, 0);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800795 INIT_LIST_HEAD(&sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800796 atomic_set(&sbi->total_zombie_tree, 0);
Chao Yua28ef1f2015-07-08 17:59:36 +0800797 atomic_set(&sbi->total_ext_node, 0);
798}
799
800int __init create_extent_cache(void)
801{
802 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
803 sizeof(struct extent_tree));
804 if (!extent_tree_slab)
805 return -ENOMEM;
806 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
807 sizeof(struct extent_node));
808 if (!extent_node_slab) {
809 kmem_cache_destroy(extent_tree_slab);
810 return -ENOMEM;
811 }
812 return 0;
813}
814
815void destroy_extent_cache(void)
816{
817 kmem_cache_destroy(extent_node_slab);
818 kmem_cache_destroy(extent_tree_slab);
819}