blob: c6934f014e0f1d9a458a6489422612a6484fe198 [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
21static struct kmem_cache *extent_tree_slab;
22static struct kmem_cache *extent_node_slab;
23
24static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
25 struct extent_tree *et, struct extent_info *ei,
26 struct rb_node *parent, struct rb_node **p)
27{
28 struct extent_node *en;
29
30 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
31 if (!en)
32 return NULL;
33
34 en->ei = *ei;
35 INIT_LIST_HEAD(&en->list);
Hou Pengyang201ef5e2016-01-26 12:56:26 +000036 en->et = et;
Chao Yua28ef1f2015-07-08 17:59:36 +080037
38 rb_link_node(&en->rb_node, parent, p);
39 rb_insert_color(&en->rb_node, &et->root);
Chao Yu68e35382016-01-08 20:22:52 +080040 atomic_inc(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +080041 atomic_inc(&sbi->total_ext_node);
42 return en;
43}
44
45static void __detach_extent_node(struct f2fs_sb_info *sbi,
46 struct extent_tree *et, struct extent_node *en)
47{
48 rb_erase(&en->rb_node, &et->root);
Chao Yu68e35382016-01-08 20:22:52 +080049 atomic_dec(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +080050 atomic_dec(&sbi->total_ext_node);
51
52 if (et->cached_en == en)
53 et->cached_en = NULL;
Hou Pengyanga03f01f2016-01-26 12:56:25 +000054 kmem_cache_free(extent_node_slab, en);
55}
56
57/*
58 * Flow to release an extent_node:
59 * 1. list_del_init
60 * 2. __detach_extent_node
61 * 3. kmem_cache_free.
62 */
63static void __release_extent_node(struct f2fs_sb_info *sbi,
64 struct extent_tree *et, struct extent_node *en)
65{
66 spin_lock(&sbi->extent_lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +000067 f2fs_bug_on(sbi, list_empty(&en->list));
68 list_del_init(&en->list);
Hou Pengyanga03f01f2016-01-26 12:56:25 +000069 spin_unlock(&sbi->extent_lock);
70
71 __detach_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +080072}
73
74static struct extent_tree *__grab_extent_tree(struct inode *inode)
75{
76 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
77 struct extent_tree *et;
78 nid_t ino = inode->i_ino;
79
Yunlei He5e8256a2017-02-23 19:39:59 +080080 mutex_lock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +080081 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
82 if (!et) {
83 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
84 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
85 memset(et, 0, sizeof(struct extent_tree));
86 et->ino = ino;
87 et->root = RB_ROOT;
88 et->cached_en = NULL;
89 rwlock_init(&et->lock);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -080090 INIT_LIST_HEAD(&et->list);
Chao Yu68e35382016-01-08 20:22:52 +080091 atomic_set(&et->node_cnt, 0);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080092 atomic_inc(&sbi->total_ext_tree);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -080093 } else {
94 atomic_dec(&sbi->total_zombie_tree);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -080095 list_del_init(&et->list);
Chao Yua28ef1f2015-07-08 17:59:36 +080096 }
Yunlei He5e8256a2017-02-23 19:39:59 +080097 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +080098
99 /* never died until evict_inode */
100 F2FS_I(inode)->extent_tree = et;
101
102 return et;
103}
104
Chao Yu91c481f2015-08-19 19:12:20 +0800105static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,
106 struct extent_tree *et, unsigned int fofs)
Chao Yua28ef1f2015-07-08 17:59:36 +0800107{
108 struct rb_node *node = et->root.rb_node;
Fan Lif8b703d2015-08-18 17:13:13 +0800109 struct extent_node *en = et->cached_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800110
Fan Lif8b703d2015-08-18 17:13:13 +0800111 if (en) {
112 struct extent_info *cei = &en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +0800113
Chao Yu91c481f2015-08-19 19:12:20 +0800114 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) {
115 stat_inc_cached_node_hit(sbi);
Fan Lif8b703d2015-08-18 17:13:13 +0800116 return en;
Chao Yu91c481f2015-08-19 19:12:20 +0800117 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800118 }
119
120 while (node) {
121 en = rb_entry(node, struct extent_node, rb_node);
122
Chao Yu029e13c2015-08-19 19:13:25 +0800123 if (fofs < en->ei.fofs) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800124 node = node->rb_left;
Chao Yu029e13c2015-08-19 19:13:25 +0800125 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800126 node = node->rb_right;
Chao Yu029e13c2015-08-19 19:13:25 +0800127 } else {
128 stat_inc_rbtree_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800129 return en;
Chao Yu029e13c2015-08-19 19:13:25 +0800130 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800131 }
132 return NULL;
133}
134
Chao Yua6f78342015-08-19 19:14:15 +0800135static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
136 struct extent_tree *et, struct extent_info *ei)
Chao Yua28ef1f2015-07-08 17:59:36 +0800137{
138 struct rb_node **p = &et->root.rb_node;
Chao Yua28ef1f2015-07-08 17:59:36 +0800139 struct extent_node *en;
140
Chao Yua6f78342015-08-19 19:14:15 +0800141 en = __attach_extent_node(sbi, et, ei, NULL, p);
Chao Yua28ef1f2015-07-08 17:59:36 +0800142 if (!en)
143 return NULL;
Chao Yua6f78342015-08-19 19:14:15 +0800144
145 et->largest = en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +0800146 et->cached_en = en;
147 return en;
148}
149
150static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000151 struct extent_tree *et)
Chao Yua28ef1f2015-07-08 17:59:36 +0800152{
153 struct rb_node *node, *next;
154 struct extent_node *en;
Chao Yu68e35382016-01-08 20:22:52 +0800155 unsigned int count = atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800156
157 node = rb_first(&et->root);
158 while (node) {
159 next = rb_next(node);
160 en = rb_entry(node, struct extent_node, rb_node);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000161 __release_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +0800162 node = next;
163 }
164
Chao Yu68e35382016-01-08 20:22:52 +0800165 return count - atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800166}
167
Fan Li41a099d2015-09-17 18:24:17 +0800168static void __drop_largest_extent(struct inode *inode,
169 pgoff_t fofs, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800170{
171 struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
172
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700173 if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800174 largest->len = 0;
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700175 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700176 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800177}
178
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800179/* return true, if inode page is changed */
180bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
Chao Yua28ef1f2015-07-08 17:59:36 +0800181{
182 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
183 struct extent_tree *et;
184 struct extent_node *en;
185 struct extent_info ei;
186
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800187 if (!f2fs_may_extent_tree(inode)) {
188 /* drop largest extent */
189 if (i_ext && i_ext->len) {
190 i_ext->len = 0;
191 return true;
192 }
193 return false;
194 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800195
196 et = __grab_extent_tree(inode);
197
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800198 if (!i_ext || !i_ext->len)
199 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800200
Chao Yubd933d42016-05-04 23:19:47 +0800201 get_extent_info(&ei, i_ext);
Chao Yua28ef1f2015-07-08 17:59:36 +0800202
203 write_lock(&et->lock);
Chao Yu68e35382016-01-08 20:22:52 +0800204 if (atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800205 goto out;
206
Chao Yua6f78342015-08-19 19:14:15 +0800207 en = __init_extent_tree(sbi, et, &ei);
Chao Yua28ef1f2015-07-08 17:59:36 +0800208 if (en) {
209 spin_lock(&sbi->extent_lock);
210 list_add_tail(&en->list, &sbi->extent_list);
211 spin_unlock(&sbi->extent_lock);
212 }
213out:
214 write_unlock(&et->lock);
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800215 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800216}
217
218static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
219 struct extent_info *ei)
220{
221 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
222 struct extent_tree *et = F2FS_I(inode)->extent_tree;
223 struct extent_node *en;
224 bool ret = false;
225
226 f2fs_bug_on(sbi, !et);
227
228 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
229
230 read_lock(&et->lock);
231
232 if (et->largest.fofs <= pgofs &&
233 et->largest.fofs + et->largest.len > pgofs) {
234 *ei = et->largest;
235 ret = true;
Chao Yu91c481f2015-08-19 19:12:20 +0800236 stat_inc_largest_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800237 goto out;
238 }
239
Chao Yu91c481f2015-08-19 19:12:20 +0800240 en = __lookup_extent_tree(sbi, et, pgofs);
Chao Yua28ef1f2015-07-08 17:59:36 +0800241 if (en) {
242 *ei = en->ei;
243 spin_lock(&sbi->extent_lock);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800244 if (!list_empty(&en->list)) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800245 list_move_tail(&en->list, &sbi->extent_list);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800246 et->cached_en = en;
247 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800248 spin_unlock(&sbi->extent_lock);
249 ret = true;
Chao Yua28ef1f2015-07-08 17:59:36 +0800250 }
251out:
Chao Yu727edac2015-07-15 17:29:49 +0800252 stat_inc_total_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800253 read_unlock(&et->lock);
254
255 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
256 return ret;
257}
258
Fan Li0f825ee2015-07-15 18:05:17 +0800259
260/*
261 * lookup extent at @fofs, if hit, return the extent
262 * if not, return NULL and
263 * @prev_ex: extent before fofs
264 * @next_ex: extent after fofs
265 * @insert_p: insert point for new extent at fofs
266 * in order to simpfy the insertion after.
267 * tree must stay unchanged between lookup and insertion.
268 */
269static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
Chao Yu91c481f2015-08-19 19:12:20 +0800270 unsigned int fofs,
271 struct extent_node **prev_ex,
Fan Li0f825ee2015-07-15 18:05:17 +0800272 struct extent_node **next_ex,
273 struct rb_node ***insert_p,
274 struct rb_node **insert_parent)
275{
276 struct rb_node **pnode = &et->root.rb_node;
277 struct rb_node *parent = NULL, *tmp_node;
Chao Yudac2dde2015-08-19 19:16:09 +0800278 struct extent_node *en = et->cached_en;
Fan Li0f825ee2015-07-15 18:05:17 +0800279
Chao Yudac2dde2015-08-19 19:16:09 +0800280 *insert_p = NULL;
281 *insert_parent = NULL;
282 *prev_ex = NULL;
283 *next_ex = NULL;
284
285 if (RB_EMPTY_ROOT(&et->root))
286 return NULL;
287
288 if (en) {
289 struct extent_info *cei = &en->ei;
Fan Li0f825ee2015-07-15 18:05:17 +0800290
291 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
Chao Yudac2dde2015-08-19 19:16:09 +0800292 goto lookup_neighbors;
Fan Li0f825ee2015-07-15 18:05:17 +0800293 }
294
295 while (*pnode) {
296 parent = *pnode;
297 en = rb_entry(*pnode, struct extent_node, rb_node);
298
299 if (fofs < en->ei.fofs)
300 pnode = &(*pnode)->rb_left;
301 else if (fofs >= en->ei.fofs + en->ei.len)
302 pnode = &(*pnode)->rb_right;
303 else
Chao Yudac2dde2015-08-19 19:16:09 +0800304 goto lookup_neighbors;
Fan Li0f825ee2015-07-15 18:05:17 +0800305 }
306
307 *insert_p = pnode;
308 *insert_parent = parent;
309
310 en = rb_entry(parent, struct extent_node, rb_node);
311 tmp_node = parent;
312 if (parent && fofs > en->ei.fofs)
313 tmp_node = rb_next(parent);
Geliang Tanged0b5622016-12-20 21:57:42 +0800314 *next_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
Fan Li0f825ee2015-07-15 18:05:17 +0800315
316 tmp_node = parent;
317 if (parent && fofs < en->ei.fofs)
318 tmp_node = rb_prev(parent);
Geliang Tanged0b5622016-12-20 21:57:42 +0800319 *prev_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
Fan Li0f825ee2015-07-15 18:05:17 +0800320 return NULL;
Chao Yudac2dde2015-08-19 19:16:09 +0800321
322lookup_neighbors:
323 if (fofs == en->ei.fofs) {
324 /* lookup prev node for merging backward later */
325 tmp_node = rb_prev(&en->rb_node);
Geliang Tanged0b5622016-12-20 21:57:42 +0800326 *prev_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
Chao Yudac2dde2015-08-19 19:16:09 +0800327 }
328 if (fofs == en->ei.fofs + en->ei.len - 1) {
329 /* lookup next node for merging frontward later */
330 tmp_node = rb_next(&en->rb_node);
Geliang Tanged0b5622016-12-20 21:57:42 +0800331 *next_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
Chao Yudac2dde2015-08-19 19:16:09 +0800332 }
333 return en;
Fan Li0f825ee2015-07-15 18:05:17 +0800334}
335
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700336static struct extent_node *__try_merge_extent_node(struct inode *inode,
Fan Li0f825ee2015-07-15 18:05:17 +0800337 struct extent_tree *et, struct extent_info *ei,
Fan Li0f825ee2015-07-15 18:05:17 +0800338 struct extent_node *prev_ex,
Chao Yuef05e222015-08-19 19:15:09 +0800339 struct extent_node *next_ex)
Fan Li0f825ee2015-07-15 18:05:17 +0800340{
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700341 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Fan Li0f825ee2015-07-15 18:05:17 +0800342 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800343
344 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800345 prev_ex->ei.len += ei->len;
346 ei = &prev_ex->ei;
347 en = prev_ex;
348 }
Chao Yuef05e222015-08-19 19:15:09 +0800349
Fan Li0f825ee2015-07-15 18:05:17 +0800350 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800351 next_ex->ei.fofs = ei->fofs;
352 next_ex->ei.blk = ei->blk;
353 next_ex->ei.len += ei->len;
Yunlei He7855eba2016-12-19 20:10:48 +0800354 if (en)
355 __release_extent_node(sbi, et, prev_ex);
356
Fan Li0f825ee2015-07-15 18:05:17 +0800357 en = next_ex;
358 }
Chao Yuef05e222015-08-19 19:15:09 +0800359
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800360 if (!en)
361 return NULL;
362
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700363 __try_update_largest_extent(inode, et, en);
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800364
365 spin_lock(&sbi->extent_lock);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800366 if (!list_empty(&en->list)) {
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800367 list_move_tail(&en->list, &sbi->extent_list);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800368 et->cached_en = en;
369 }
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800370 spin_unlock(&sbi->extent_lock);
Chao Yuef05e222015-08-19 19:15:09 +0800371 return en;
372}
373
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700374static struct extent_node *__insert_extent_tree(struct inode *inode,
Chao Yuef05e222015-08-19 19:15:09 +0800375 struct extent_tree *et, struct extent_info *ei,
376 struct rb_node **insert_p,
377 struct rb_node *insert_parent)
378{
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700379 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuef05e222015-08-19 19:15:09 +0800380 struct rb_node **p = &et->root.rb_node;
381 struct rb_node *parent = NULL;
382 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800383
384 if (insert_p && insert_parent) {
385 parent = insert_parent;
386 p = insert_p;
387 goto do_insert;
388 }
389
390 while (*p) {
391 parent = *p;
392 en = rb_entry(parent, struct extent_node, rb_node);
393
394 if (ei->fofs < en->ei.fofs)
395 p = &(*p)->rb_left;
396 else if (ei->fofs >= en->ei.fofs + en->ei.len)
397 p = &(*p)->rb_right;
398 else
399 f2fs_bug_on(sbi, 1);
400 }
401do_insert:
402 en = __attach_extent_node(sbi, et, ei, parent, p);
403 if (!en)
404 return NULL;
Chao Yuef05e222015-08-19 19:15:09 +0800405
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700406 __try_update_largest_extent(inode, et, en);
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800407
408 /* update in global extent list */
409 spin_lock(&sbi->extent_lock);
410 list_add_tail(&en->list, &sbi->extent_list);
Jaegeuk Kim42926742016-01-26 09:20:05 -0800411 et->cached_en = en;
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800412 spin_unlock(&sbi->extent_lock);
Fan Li0f825ee2015-07-15 18:05:17 +0800413 return en;
414}
415
Chao Yu317e1302017-02-25 17:29:54 +0800416static void f2fs_update_extent_tree_range(struct inode *inode,
Chao Yu19b2c302015-08-26 20:34:48 +0800417 pgoff_t fofs, block_t blkaddr, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800418{
419 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
420 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Fan Li4d1fa812015-09-17 18:42:06 +0800421 struct extent_node *en = NULL, *en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800422 struct extent_node *prev_en = NULL, *next_en = NULL;
Chao Yua28ef1f2015-07-08 17:59:36 +0800423 struct extent_info ei, dei, prev;
Fan Li0f825ee2015-07-15 18:05:17 +0800424 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800425 unsigned int end = fofs + len;
426 unsigned int pos = (unsigned int)fofs;
Chao Yua28ef1f2015-07-08 17:59:36 +0800427
428 if (!et)
Chao Yu317e1302017-02-25 17:29:54 +0800429 return;
Chao Yua28ef1f2015-07-08 17:59:36 +0800430
Chao Yu744288c2015-09-06 17:50:13 +0800431 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
432
Chao Yua28ef1f2015-07-08 17:59:36 +0800433 write_lock(&et->lock);
434
Jaegeuk Kim91942322016-05-20 10:13:22 -0700435 if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800436 write_unlock(&et->lock);
Chao Yu317e1302017-02-25 17:29:54 +0800437 return;
Chao Yua28ef1f2015-07-08 17:59:36 +0800438 }
439
440 prev = et->largest;
441 dei.len = 0;
442
Fan Li4d1fa812015-09-17 18:42:06 +0800443 /*
444 * drop largest extent before lookup, in case it's already
445 * been shrunk from extent tree
446 */
Fan Li41a099d2015-09-17 18:24:17 +0800447 __drop_largest_extent(inode, fofs, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800448
Chao Yu19b2c302015-08-26 20:34:48 +0800449 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
450 en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
Fan Li0f825ee2015-07-15 18:05:17 +0800451 &insert_p, &insert_parent);
Fan Li4d1fa812015-09-17 18:42:06 +0800452 if (!en)
453 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800454
Chao Yu19b2c302015-08-26 20:34:48 +0800455 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
Fan Li4d1fa812015-09-17 18:42:06 +0800456 while (en && en->ei.fofs < end) {
457 unsigned int org_end;
458 int parts = 0; /* # of parts current extent split into */
Chao Yua28ef1f2015-07-08 17:59:36 +0800459
Fan Li4d1fa812015-09-17 18:42:06 +0800460 next_en = en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800461
462 dei = en->ei;
Fan Li4d1fa812015-09-17 18:42:06 +0800463 org_end = dei.fofs + dei.len;
464 f2fs_bug_on(sbi, pos >= org_end);
Chao Yu19b2c302015-08-26 20:34:48 +0800465
Fan Li4d1fa812015-09-17 18:42:06 +0800466 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
467 en->ei.len = pos - en->ei.fofs;
468 prev_en = en;
469 parts = 1;
Chao Yua28ef1f2015-07-08 17:59:36 +0800470 }
471
Fan Li4d1fa812015-09-17 18:42:06 +0800472 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
473 if (parts) {
Chao Yu19b2c302015-08-26 20:34:48 +0800474 set_extent_info(&ei, end,
475 end - dei.fofs + dei.blk,
Fan Li4d1fa812015-09-17 18:42:06 +0800476 org_end - end);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700477 en1 = __insert_extent_tree(inode, et, &ei,
Fan Li4d1fa812015-09-17 18:42:06 +0800478 NULL, NULL);
479 next_en = en1;
480 } else {
481 en->ei.fofs = end;
482 en->ei.blk += end - dei.fofs;
483 en->ei.len -= end - dei.fofs;
484 next_en = en;
Chao Yu19b2c302015-08-26 20:34:48 +0800485 }
Fan Li4d1fa812015-09-17 18:42:06 +0800486 parts++;
Chao Yu19b2c302015-08-26 20:34:48 +0800487 }
Fan Li4d1fa812015-09-17 18:42:06 +0800488
489 if (!next_en) {
490 struct rb_node *node = rb_next(&en->rb_node);
491
Geliang Tanged0b5622016-12-20 21:57:42 +0800492 next_en = rb_entry_safe(node, struct extent_node,
493 rb_node);
Fan Li4d1fa812015-09-17 18:42:06 +0800494 }
495
Chao Yu4abd3f52015-09-22 21:07:47 +0800496 if (parts)
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700497 __try_update_largest_extent(inode, et, en);
Chao Yu4abd3f52015-09-22 21:07:47 +0800498 else
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000499 __release_extent_node(sbi, et, en);
Fan Li4d1fa812015-09-17 18:42:06 +0800500
501 /*
502 * if original extent is split into zero or two parts, extent
503 * tree has been altered by deletion or insertion, therefore
504 * invalidate pointers regard to tree.
505 */
506 if (parts != 1) {
507 insert_p = NULL;
508 insert_parent = NULL;
509 }
Fan Li4d1fa812015-09-17 18:42:06 +0800510 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800511 }
512
Chao Yua28ef1f2015-07-08 17:59:36 +0800513 /* 3. update extent in extent cache */
514 if (blkaddr) {
Chao Yu19b2c302015-08-26 20:34:48 +0800515
516 set_extent_info(&ei, fofs, blkaddr, len);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700517 if (!__try_merge_extent_node(inode, et, &ei, prev_en, next_en))
518 __insert_extent_tree(inode, et, &ei,
Chao Yuef05e222015-08-19 19:15:09 +0800519 insert_p, insert_parent);
Chao Yua28ef1f2015-07-08 17:59:36 +0800520
521 /* give up extent_cache, if split and small updates happen */
522 if (dei.len >= 1 &&
523 prev.len < F2FS_MIN_EXTENT_LEN &&
524 et->largest.len < F2FS_MIN_EXTENT_LEN) {
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700525 __drop_largest_extent(inode, 0, UINT_MAX);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700526 set_inode_flag(inode, FI_NO_EXTENT);
Chao Yua28ef1f2015-07-08 17:59:36 +0800527 }
Chao Yu19b2c302015-08-26 20:34:48 +0800528 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800529
Jaegeuk Kim91942322016-05-20 10:13:22 -0700530 if (is_inode_flag_set(inode, FI_NO_EXTENT))
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000531 __free_extent_tree(sbi, et);
Chao Yua28ef1f2015-07-08 17:59:36 +0800532
533 write_unlock(&et->lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800534}
535
536unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
537{
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800538 struct extent_tree *et, *next;
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000539 struct extent_node *en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800540 unsigned int node_cnt = 0, tree_cnt = 0;
541 int remained;
542
543 if (!test_opt(sbi, EXTENT_CACHE))
544 return 0;
545
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800546 if (!atomic_read(&sbi->total_zombie_tree))
547 goto free_node;
548
Yunlei He5e8256a2017-02-23 19:39:59 +0800549 if (!mutex_trylock(&sbi->extent_tree_lock))
Chao Yua28ef1f2015-07-08 17:59:36 +0800550 goto out;
551
552 /* 1. remove unreferenced extent tree */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800553 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
Chao Yu9b72a382016-01-08 20:24:00 +0800554 if (atomic_read(&et->node_cnt)) {
555 write_lock(&et->lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000556 node_cnt += __free_extent_tree(sbi, et);
Chao Yu9b72a382016-01-08 20:24:00 +0800557 write_unlock(&et->lock);
558 }
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000559 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800560 list_del_init(&et->list);
561 radix_tree_delete(&sbi->extent_tree_root, et->ino);
562 kmem_cache_free(extent_tree_slab, et);
563 atomic_dec(&sbi->total_ext_tree);
564 atomic_dec(&sbi->total_zombie_tree);
565 tree_cnt++;
Chao Yua28ef1f2015-07-08 17:59:36 +0800566
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800567 if (node_cnt + tree_cnt >= nr_shrink)
568 goto unlock_out;
Jaegeuk Kim6fe2bc92016-01-20 07:31:48 +0800569 cond_resched();
Chao Yua28ef1f2015-07-08 17:59:36 +0800570 }
Yunlei He5e8256a2017-02-23 19:39:59 +0800571 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800572
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800573free_node:
Chao Yua28ef1f2015-07-08 17:59:36 +0800574 /* 2. remove LRU extent entries */
Yunlei He5e8256a2017-02-23 19:39:59 +0800575 if (!mutex_trylock(&sbi->extent_tree_lock))
Chao Yua28ef1f2015-07-08 17:59:36 +0800576 goto out;
577
578 remained = nr_shrink - (node_cnt + tree_cnt);
579
580 spin_lock(&sbi->extent_lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000581 for (; remained > 0; remained--) {
582 if (list_empty(&sbi->extent_list))
Chao Yua28ef1f2015-07-08 17:59:36 +0800583 break;
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000584 en = list_first_entry(&sbi->extent_list,
585 struct extent_node, list);
586 et = en->et;
587 if (!write_trylock(&et->lock)) {
588 /* refresh this extent node's position in extent list */
589 list_move_tail(&en->list, &sbi->extent_list);
590 continue;
591 }
592
Chao Yua28ef1f2015-07-08 17:59:36 +0800593 list_del_init(&en->list);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000594 spin_unlock(&sbi->extent_lock);
595
596 __detach_extent_node(sbi, et, en);
597
598 write_unlock(&et->lock);
599 node_cnt++;
600 spin_lock(&sbi->extent_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800601 }
602 spin_unlock(&sbi->extent_lock);
603
Chao Yua28ef1f2015-07-08 17:59:36 +0800604unlock_out:
Yunlei He5e8256a2017-02-23 19:39:59 +0800605 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800606out:
607 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
608
609 return node_cnt + tree_cnt;
610}
611
612unsigned int f2fs_destroy_extent_node(struct inode *inode)
613{
614 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
615 struct extent_tree *et = F2FS_I(inode)->extent_tree;
616 unsigned int node_cnt = 0;
617
Chao Yu9b72a382016-01-08 20:24:00 +0800618 if (!et || !atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800619 return 0;
620
621 write_lock(&et->lock);
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000622 node_cnt = __free_extent_tree(sbi, et);
Chao Yua28ef1f2015-07-08 17:59:36 +0800623 write_unlock(&et->lock);
624
625 return node_cnt;
626}
627
Jaegeuk Kim5f281fa2016-07-12 11:07:52 -0700628void f2fs_drop_extent_tree(struct inode *inode)
629{
630 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
631 struct extent_tree *et = F2FS_I(inode)->extent_tree;
632
633 set_inode_flag(inode, FI_NO_EXTENT);
634
635 write_lock(&et->lock);
636 __free_extent_tree(sbi, et);
637 __drop_largest_extent(inode, 0, UINT_MAX);
638 write_unlock(&et->lock);
639}
640
Chao Yua28ef1f2015-07-08 17:59:36 +0800641void f2fs_destroy_extent_tree(struct inode *inode)
642{
643 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
644 struct extent_tree *et = F2FS_I(inode)->extent_tree;
645 unsigned int node_cnt = 0;
646
647 if (!et)
648 return;
649
Chao Yu68e35382016-01-08 20:22:52 +0800650 if (inode->i_nlink && !is_bad_inode(inode) &&
651 atomic_read(&et->node_cnt)) {
Yunlei He5e8256a2017-02-23 19:39:59 +0800652 mutex_lock(&sbi->extent_tree_lock);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800653 list_add_tail(&et->list, &sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800654 atomic_inc(&sbi->total_zombie_tree);
Yunlei He5e8256a2017-02-23 19:39:59 +0800655 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800656 return;
657 }
658
659 /* free all extent info belong to this extent tree */
660 node_cnt = f2fs_destroy_extent_node(inode);
661
662 /* delete extent tree entry in radix tree */
Yunlei He5e8256a2017-02-23 19:39:59 +0800663 mutex_lock(&sbi->extent_tree_lock);
Chao Yu68e35382016-01-08 20:22:52 +0800664 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
Chao Yua28ef1f2015-07-08 17:59:36 +0800665 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
666 kmem_cache_free(extent_tree_slab, et);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800667 atomic_dec(&sbi->total_ext_tree);
Yunlei He5e8256a2017-02-23 19:39:59 +0800668 mutex_unlock(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800669
670 F2FS_I(inode)->extent_tree = NULL;
671
672 trace_f2fs_destroy_extent_tree(inode, node_cnt);
673}
674
675bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
676 struct extent_info *ei)
677{
678 if (!f2fs_may_extent_tree(inode))
679 return false;
680
681 return f2fs_lookup_extent_tree(inode, pgofs, ei);
682}
683
684void f2fs_update_extent_cache(struct dnode_of_data *dn)
685{
Chao Yua28ef1f2015-07-08 17:59:36 +0800686 pgoff_t fofs;
Chao Yuf28b3432016-02-24 17:16:47 +0800687 block_t blkaddr;
Chao Yua28ef1f2015-07-08 17:59:36 +0800688
689 if (!f2fs_may_extent_tree(dn->inode))
690 return;
691
Chao Yuf28b3432016-02-24 17:16:47 +0800692 if (dn->data_blkaddr == NEW_ADDR)
693 blkaddr = NULL_ADDR;
694 else
695 blkaddr = dn->data_blkaddr;
Chao Yu19b2c302015-08-26 20:34:48 +0800696
Chao Yu81ca7352016-01-26 15:39:35 +0800697 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
698 dn->ofs_in_node;
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700699 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
Chao Yu19b2c302015-08-26 20:34:48 +0800700}
701
702void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
703 pgoff_t fofs, block_t blkaddr, unsigned int len)
704
705{
706 if (!f2fs_may_extent_tree(dn->inode))
707 return;
708
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700709 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800710}
711
712void init_extent_cache_info(struct f2fs_sb_info *sbi)
713{
714 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
Yunlei He5e8256a2017-02-23 19:39:59 +0800715 mutex_init(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800716 INIT_LIST_HEAD(&sbi->extent_list);
717 spin_lock_init(&sbi->extent_lock);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800718 atomic_set(&sbi->total_ext_tree, 0);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800719 INIT_LIST_HEAD(&sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800720 atomic_set(&sbi->total_zombie_tree, 0);
Chao Yua28ef1f2015-07-08 17:59:36 +0800721 atomic_set(&sbi->total_ext_node, 0);
722}
723
724int __init create_extent_cache(void)
725{
726 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
727 sizeof(struct extent_tree));
728 if (!extent_tree_slab)
729 return -ENOMEM;
730 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
731 sizeof(struct extent_node));
732 if (!extent_node_slab) {
733 kmem_cache_destroy(extent_tree_slab);
734 return -ENOMEM;
735 }
736 return 0;
737}
738
739void destroy_extent_cache(void)
740{
741 kmem_cache_destroy(extent_node_slab);
742 kmem_cache_destroy(extent_tree_slab);
743}