blob: 995a7b1baa70801cab9ac624ba10af7cfa159361 [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);
36
37 rb_link_node(&en->rb_node, parent, p);
38 rb_insert_color(&en->rb_node, &et->root);
Chao Yu68e35382016-01-08 20:22:52 +080039 atomic_inc(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +080040 atomic_inc(&sbi->total_ext_node);
41 return en;
42}
43
44static void __detach_extent_node(struct f2fs_sb_info *sbi,
45 struct extent_tree *et, struct extent_node *en)
46{
47 rb_erase(&en->rb_node, &et->root);
Chao Yu68e35382016-01-08 20:22:52 +080048 atomic_dec(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +080049 atomic_dec(&sbi->total_ext_node);
50
51 if (et->cached_en == en)
52 et->cached_en = NULL;
Hou Pengyanga03f01f2016-01-26 12:56:25 +000053 kmem_cache_free(extent_node_slab, en);
54}
55
56/*
57 * Flow to release an extent_node:
58 * 1. list_del_init
59 * 2. __detach_extent_node
60 * 3. kmem_cache_free.
61 */
62static void __release_extent_node(struct f2fs_sb_info *sbi,
63 struct extent_tree *et, struct extent_node *en)
64{
65 spin_lock(&sbi->extent_lock);
66 if (!list_empty(&en->list))
67 list_del_init(&en->list);
68 spin_unlock(&sbi->extent_lock);
69
70 __detach_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +080071}
72
73static struct extent_tree *__grab_extent_tree(struct inode *inode)
74{
75 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
76 struct extent_tree *et;
77 nid_t ino = inode->i_ino;
78
79 down_write(&sbi->extent_tree_lock);
80 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
81 if (!et) {
82 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
83 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
84 memset(et, 0, sizeof(struct extent_tree));
85 et->ino = ino;
86 et->root = RB_ROOT;
87 et->cached_en = NULL;
88 rwlock_init(&et->lock);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -080089 INIT_LIST_HEAD(&et->list);
Chao Yu68e35382016-01-08 20:22:52 +080090 atomic_set(&et->node_cnt, 0);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080091 atomic_inc(&sbi->total_ext_tree);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -080092 } else {
93 atomic_dec(&sbi->total_zombie_tree);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -080094 list_del_init(&et->list);
Chao Yua28ef1f2015-07-08 17:59:36 +080095 }
Chao Yua28ef1f2015-07-08 17:59:36 +080096 up_write(&sbi->extent_tree_lock);
97
98 /* never died until evict_inode */
99 F2FS_I(inode)->extent_tree = et;
100
101 return et;
102}
103
Chao Yu91c481f2015-08-19 19:12:20 +0800104static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,
105 struct extent_tree *et, unsigned int fofs)
Chao Yua28ef1f2015-07-08 17:59:36 +0800106{
107 struct rb_node *node = et->root.rb_node;
Fan Lif8b703d2015-08-18 17:13:13 +0800108 struct extent_node *en = et->cached_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800109
Fan Lif8b703d2015-08-18 17:13:13 +0800110 if (en) {
111 struct extent_info *cei = &en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +0800112
Chao Yu91c481f2015-08-19 19:12:20 +0800113 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) {
114 stat_inc_cached_node_hit(sbi);
Fan Lif8b703d2015-08-18 17:13:13 +0800115 return en;
Chao Yu91c481f2015-08-19 19:12:20 +0800116 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800117 }
118
119 while (node) {
120 en = rb_entry(node, struct extent_node, rb_node);
121
Chao Yu029e13c2015-08-19 19:13:25 +0800122 if (fofs < en->ei.fofs) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800123 node = node->rb_left;
Chao Yu029e13c2015-08-19 19:13:25 +0800124 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800125 node = node->rb_right;
Chao Yu029e13c2015-08-19 19:13:25 +0800126 } else {
127 stat_inc_rbtree_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800128 return en;
Chao Yu029e13c2015-08-19 19:13:25 +0800129 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800130 }
131 return NULL;
132}
133
Chao Yua6f78342015-08-19 19:14:15 +0800134static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
135 struct extent_tree *et, struct extent_info *ei)
Chao Yua28ef1f2015-07-08 17:59:36 +0800136{
137 struct rb_node **p = &et->root.rb_node;
Chao Yua28ef1f2015-07-08 17:59:36 +0800138 struct extent_node *en;
139
Chao Yua6f78342015-08-19 19:14:15 +0800140 en = __attach_extent_node(sbi, et, ei, NULL, p);
Chao Yua28ef1f2015-07-08 17:59:36 +0800141 if (!en)
142 return NULL;
Chao Yua6f78342015-08-19 19:14:15 +0800143
144 et->largest = en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +0800145 et->cached_en = en;
146 return en;
147}
148
149static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
150 struct extent_tree *et, bool free_all)
151{
152 struct rb_node *node, *next;
153 struct extent_node *en;
Chao Yu68e35382016-01-08 20:22:52 +0800154 unsigned int count = atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800155
156 node = rb_first(&et->root);
157 while (node) {
158 next = rb_next(node);
159 en = rb_entry(node, struct extent_node, rb_node);
160
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000161 if (free_all)
162 __release_extent_node(sbi, et, en);
163 else if (list_empty(&en->list))
Chao Yua28ef1f2015-07-08 17:59:36 +0800164 __detach_extent_node(sbi, et, en);
Chao Yua28ef1f2015-07-08 17:59:36 +0800165 node = next;
166 }
167
Chao Yu68e35382016-01-08 20:22:52 +0800168 return count - atomic_read(&et->node_cnt);
Chao Yua28ef1f2015-07-08 17:59:36 +0800169}
170
Fan Li41a099d2015-09-17 18:24:17 +0800171static void __drop_largest_extent(struct inode *inode,
172 pgoff_t fofs, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800173{
174 struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
175
Fan Li41a099d2015-09-17 18:24:17 +0800176 if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs)
Chao Yua28ef1f2015-07-08 17:59:36 +0800177 largest->len = 0;
178}
179
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800180/* return true, if inode page is changed */
181bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
Chao Yua28ef1f2015-07-08 17:59:36 +0800182{
183 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
184 struct extent_tree *et;
185 struct extent_node *en;
186 struct extent_info ei;
187
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800188 if (!f2fs_may_extent_tree(inode)) {
189 /* drop largest extent */
190 if (i_ext && i_ext->len) {
191 i_ext->len = 0;
192 return true;
193 }
194 return false;
195 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800196
197 et = __grab_extent_tree(inode);
198
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800199 if (!i_ext || !i_ext->len)
200 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800201
202 set_extent_info(&ei, le32_to_cpu(i_ext->fofs),
203 le32_to_cpu(i_ext->blk), le32_to_cpu(i_ext->len));
204
205 write_lock(&et->lock);
Chao Yu68e35382016-01-08 20:22:52 +0800206 if (atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800207 goto out;
208
Chao Yua6f78342015-08-19 19:14:15 +0800209 en = __init_extent_tree(sbi, et, &ei);
Chao Yua28ef1f2015-07-08 17:59:36 +0800210 if (en) {
211 spin_lock(&sbi->extent_lock);
212 list_add_tail(&en->list, &sbi->extent_list);
213 spin_unlock(&sbi->extent_lock);
214 }
215out:
216 write_unlock(&et->lock);
Jaegeuk Kimed3d1252015-12-28 11:39:06 -0800217 return false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800218}
219
220static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
221 struct extent_info *ei)
222{
223 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
224 struct extent_tree *et = F2FS_I(inode)->extent_tree;
225 struct extent_node *en;
226 bool ret = false;
227
228 f2fs_bug_on(sbi, !et);
229
230 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
231
232 read_lock(&et->lock);
233
234 if (et->largest.fofs <= pgofs &&
235 et->largest.fofs + et->largest.len > pgofs) {
236 *ei = et->largest;
237 ret = true;
Chao Yu91c481f2015-08-19 19:12:20 +0800238 stat_inc_largest_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800239 goto out;
240 }
241
Chao Yu91c481f2015-08-19 19:12:20 +0800242 en = __lookup_extent_tree(sbi, et, pgofs);
Chao Yua28ef1f2015-07-08 17:59:36 +0800243 if (en) {
244 *ei = en->ei;
245 spin_lock(&sbi->extent_lock);
246 if (!list_empty(&en->list))
247 list_move_tail(&en->list, &sbi->extent_list);
248 et->cached_en = en;
249 spin_unlock(&sbi->extent_lock);
250 ret = true;
Chao Yua28ef1f2015-07-08 17:59:36 +0800251 }
252out:
Chao Yu727edac2015-07-15 17:29:49 +0800253 stat_inc_total_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800254 read_unlock(&et->lock);
255
256 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
257 return ret;
258}
259
Fan Li0f825ee2015-07-15 18:05:17 +0800260
261/*
262 * lookup extent at @fofs, if hit, return the extent
263 * if not, return NULL and
264 * @prev_ex: extent before fofs
265 * @next_ex: extent after fofs
266 * @insert_p: insert point for new extent at fofs
267 * in order to simpfy the insertion after.
268 * tree must stay unchanged between lookup and insertion.
269 */
270static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
Chao Yu91c481f2015-08-19 19:12:20 +0800271 unsigned int fofs,
272 struct extent_node **prev_ex,
Fan Li0f825ee2015-07-15 18:05:17 +0800273 struct extent_node **next_ex,
274 struct rb_node ***insert_p,
275 struct rb_node **insert_parent)
276{
277 struct rb_node **pnode = &et->root.rb_node;
278 struct rb_node *parent = NULL, *tmp_node;
Chao Yudac2ddef2015-08-19 19:16:09 +0800279 struct extent_node *en = et->cached_en;
Fan Li0f825ee2015-07-15 18:05:17 +0800280
Chao Yudac2ddef2015-08-19 19:16:09 +0800281 *insert_p = NULL;
282 *insert_parent = NULL;
283 *prev_ex = NULL;
284 *next_ex = NULL;
285
286 if (RB_EMPTY_ROOT(&et->root))
287 return NULL;
288
289 if (en) {
290 struct extent_info *cei = &en->ei;
Fan Li0f825ee2015-07-15 18:05:17 +0800291
292 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
Chao Yudac2ddef2015-08-19 19:16:09 +0800293 goto lookup_neighbors;
Fan Li0f825ee2015-07-15 18:05:17 +0800294 }
295
296 while (*pnode) {
297 parent = *pnode;
298 en = rb_entry(*pnode, struct extent_node, rb_node);
299
300 if (fofs < en->ei.fofs)
301 pnode = &(*pnode)->rb_left;
302 else if (fofs >= en->ei.fofs + en->ei.len)
303 pnode = &(*pnode)->rb_right;
304 else
Chao Yudac2ddef2015-08-19 19:16:09 +0800305 goto lookup_neighbors;
Fan Li0f825ee2015-07-15 18:05:17 +0800306 }
307
308 *insert_p = pnode;
309 *insert_parent = parent;
310
311 en = rb_entry(parent, struct extent_node, rb_node);
312 tmp_node = parent;
313 if (parent && fofs > en->ei.fofs)
314 tmp_node = rb_next(parent);
315 *next_ex = tmp_node ?
316 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
317
318 tmp_node = parent;
319 if (parent && fofs < en->ei.fofs)
320 tmp_node = rb_prev(parent);
321 *prev_ex = tmp_node ?
322 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800323 return NULL;
Chao Yudac2ddef2015-08-19 19:16:09 +0800324
325lookup_neighbors:
326 if (fofs == en->ei.fofs) {
327 /* lookup prev node for merging backward later */
328 tmp_node = rb_prev(&en->rb_node);
329 *prev_ex = tmp_node ?
330 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
331 }
332 if (fofs == en->ei.fofs + en->ei.len - 1) {
333 /* lookup next node for merging frontward later */
334 tmp_node = rb_next(&en->rb_node);
335 *next_ex = tmp_node ?
336 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
337 }
338 return en;
Fan Li0f825ee2015-07-15 18:05:17 +0800339}
340
Chao Yuef05e222015-08-19 19:15:09 +0800341static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
Fan Li0f825ee2015-07-15 18:05:17 +0800342 struct extent_tree *et, struct extent_info *ei,
Fan Li0f825ee2015-07-15 18:05:17 +0800343 struct extent_node *prev_ex,
Chao Yuef05e222015-08-19 19:15:09 +0800344 struct extent_node *next_ex)
Fan Li0f825ee2015-07-15 18:05:17 +0800345{
Fan Li0f825ee2015-07-15 18:05:17 +0800346 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800347
348 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800349 prev_ex->ei.len += ei->len;
350 ei = &prev_ex->ei;
351 en = prev_ex;
352 }
Chao Yuef05e222015-08-19 19:15:09 +0800353
Fan Li0f825ee2015-07-15 18:05:17 +0800354 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000355 if (en)
356 __release_extent_node(sbi, et, prev_ex);
Fan Li0f825ee2015-07-15 18:05:17 +0800357 next_ex->ei.fofs = ei->fofs;
358 next_ex->ei.blk = ei->blk;
359 next_ex->ei.len += ei->len;
360 en = next_ex;
361 }
Chao Yuef05e222015-08-19 19:15:09 +0800362
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800363 if (!en)
364 return NULL;
365
366 __try_update_largest_extent(et, en);
367 et->cached_en = en;
368
369 spin_lock(&sbi->extent_lock);
370 if (!list_empty(&en->list))
371 list_move_tail(&en->list, &sbi->extent_list);
372 spin_unlock(&sbi->extent_lock);
Chao Yuef05e222015-08-19 19:15:09 +0800373 return en;
374}
375
376static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
377 struct extent_tree *et, struct extent_info *ei,
378 struct rb_node **insert_p,
379 struct rb_node *insert_parent)
380{
381 struct rb_node **p = &et->root.rb_node;
382 struct rb_node *parent = NULL;
383 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800384
385 if (insert_p && insert_parent) {
386 parent = insert_parent;
387 p = insert_p;
388 goto do_insert;
389 }
390
391 while (*p) {
392 parent = *p;
393 en = rb_entry(parent, struct extent_node, rb_node);
394
395 if (ei->fofs < en->ei.fofs)
396 p = &(*p)->rb_left;
397 else if (ei->fofs >= en->ei.fofs + en->ei.len)
398 p = &(*p)->rb_right;
399 else
400 f2fs_bug_on(sbi, 1);
401 }
402do_insert:
403 en = __attach_extent_node(sbi, et, ei, parent, p);
404 if (!en)
405 return NULL;
Chao Yuef05e222015-08-19 19:15:09 +0800406
Chao Yu4abd3f52015-09-22 21:07:47 +0800407 __try_update_largest_extent(et, en);
Fan Li0f825ee2015-07-15 18:05:17 +0800408 et->cached_en = en;
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800409
410 /* update in global extent list */
411 spin_lock(&sbi->extent_lock);
412 list_add_tail(&en->list, &sbi->extent_list);
413 spin_unlock(&sbi->extent_lock);
Fan Li0f825ee2015-07-15 18:05:17 +0800414 return en;
415}
416
Jaegeuk Kim514053e2015-09-15 14:46:43 -0700417static unsigned int f2fs_update_extent_tree_range(struct inode *inode,
Chao Yu19b2c302015-08-26 20:34:48 +0800418 pgoff_t fofs, block_t blkaddr, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800419{
420 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
421 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Fan Li4d1fa812015-09-17 18:42:06 +0800422 struct extent_node *en = NULL, *en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800423 struct extent_node *prev_en = NULL, *next_en = NULL;
Chao Yua28ef1f2015-07-08 17:59:36 +0800424 struct extent_info ei, dei, prev;
Fan Li0f825ee2015-07-15 18:05:17 +0800425 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800426 unsigned int end = fofs + len;
427 unsigned int pos = (unsigned int)fofs;
Chao Yua28ef1f2015-07-08 17:59:36 +0800428
429 if (!et)
430 return false;
431
Chao Yu744288c2015-09-06 17:50:13 +0800432 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
433
Chao Yua28ef1f2015-07-08 17:59:36 +0800434 write_lock(&et->lock);
435
436 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT)) {
437 write_unlock(&et->lock);
438 return false;
439 }
440
441 prev = et->largest;
442 dei.len = 0;
443
Fan Li4d1fa812015-09-17 18:42:06 +0800444 /*
445 * drop largest extent before lookup, in case it's already
446 * been shrunk from extent tree
447 */
Fan Li41a099d2015-09-17 18:24:17 +0800448 __drop_largest_extent(inode, fofs, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800449
Chao Yu19b2c302015-08-26 20:34:48 +0800450 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
451 en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
Fan Li0f825ee2015-07-15 18:05:17 +0800452 &insert_p, &insert_parent);
Fan Li4d1fa812015-09-17 18:42:06 +0800453 if (!en)
454 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800455
Chao Yu19b2c302015-08-26 20:34:48 +0800456 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
Fan Li4d1fa812015-09-17 18:42:06 +0800457 while (en && en->ei.fofs < end) {
458 unsigned int org_end;
459 int parts = 0; /* # of parts current extent split into */
Chao Yua28ef1f2015-07-08 17:59:36 +0800460
Fan Li4d1fa812015-09-17 18:42:06 +0800461 next_en = en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800462
463 dei = en->ei;
Fan Li4d1fa812015-09-17 18:42:06 +0800464 org_end = dei.fofs + dei.len;
465 f2fs_bug_on(sbi, pos >= org_end);
Chao Yu19b2c302015-08-26 20:34:48 +0800466
Fan Li4d1fa812015-09-17 18:42:06 +0800467 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
468 en->ei.len = pos - en->ei.fofs;
469 prev_en = en;
470 parts = 1;
Chao Yua28ef1f2015-07-08 17:59:36 +0800471 }
472
Fan Li4d1fa812015-09-17 18:42:06 +0800473 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
474 if (parts) {
Chao Yu19b2c302015-08-26 20:34:48 +0800475 set_extent_info(&ei, end,
476 end - dei.fofs + dei.blk,
Fan Li4d1fa812015-09-17 18:42:06 +0800477 org_end - end);
478 en1 = __insert_extent_tree(sbi, et, &ei,
479 NULL, NULL);
480 next_en = en1;
481 } else {
482 en->ei.fofs = end;
483 en->ei.blk += end - dei.fofs;
484 en->ei.len -= end - dei.fofs;
485 next_en = en;
Chao Yu19b2c302015-08-26 20:34:48 +0800486 }
Fan Li4d1fa812015-09-17 18:42:06 +0800487 parts++;
Chao Yu19b2c302015-08-26 20:34:48 +0800488 }
Fan Li4d1fa812015-09-17 18:42:06 +0800489
490 if (!next_en) {
491 struct rb_node *node = rb_next(&en->rb_node);
492
493 next_en = node ?
494 rb_entry(node, struct extent_node, rb_node)
495 : NULL;
496 }
497
Chao Yu4abd3f52015-09-22 21:07:47 +0800498 if (parts)
499 __try_update_largest_extent(et, en);
500 else
Hou Pengyanga03f01f2016-01-26 12:56:25 +0000501 __release_extent_node(sbi, et, en);
Fan Li4d1fa812015-09-17 18:42:06 +0800502
503 /*
504 * if original extent is split into zero or two parts, extent
505 * tree has been altered by deletion or insertion, therefore
506 * invalidate pointers regard to tree.
507 */
508 if (parts != 1) {
509 insert_p = NULL;
510 insert_parent = NULL;
511 }
Fan Li4d1fa812015-09-17 18:42:06 +0800512 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800513 }
514
Chao Yua28ef1f2015-07-08 17:59:36 +0800515 /* 3. update extent in extent cache */
516 if (blkaddr) {
Chao Yu19b2c302015-08-26 20:34:48 +0800517
518 set_extent_info(&ei, fofs, blkaddr, len);
Jaegeuk Kim43a2fa12016-01-26 09:12:50 -0800519 if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
520 __insert_extent_tree(sbi, et, &ei,
Chao Yuef05e222015-08-19 19:15:09 +0800521 insert_p, insert_parent);
Chao Yua28ef1f2015-07-08 17:59:36 +0800522
523 /* give up extent_cache, if split and small updates happen */
524 if (dei.len >= 1 &&
525 prev.len < F2FS_MIN_EXTENT_LEN &&
526 et->largest.len < F2FS_MIN_EXTENT_LEN) {
527 et->largest.len = 0;
528 set_inode_flag(F2FS_I(inode), FI_NO_EXTENT);
529 }
Chao Yu19b2c302015-08-26 20:34:48 +0800530 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800531
532 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
533 __free_extent_tree(sbi, et, true);
534
535 write_unlock(&et->lock);
536
537 return !__is_extent_same(&prev, &et->largest);
538}
539
540unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
541{
542 struct extent_tree *treevec[EXT_TREE_VEC_SIZE];
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800543 struct extent_tree *et, *next;
Chao Yua28ef1f2015-07-08 17:59:36 +0800544 struct extent_node *en, *tmp;
545 unsigned long ino = F2FS_ROOT_INO(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800546 unsigned int found;
547 unsigned int node_cnt = 0, tree_cnt = 0;
548 int remained;
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800549 bool do_free = false;
Chao Yua28ef1f2015-07-08 17:59:36 +0800550
551 if (!test_opt(sbi, EXTENT_CACHE))
552 return 0;
553
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800554 if (!atomic_read(&sbi->total_zombie_tree))
555 goto free_node;
556
Chao Yua28ef1f2015-07-08 17:59:36 +0800557 if (!down_write_trylock(&sbi->extent_tree_lock))
558 goto out;
559
560 /* 1. remove unreferenced extent tree */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800561 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
Chao Yu9b72a382016-01-08 20:24:00 +0800562 if (atomic_read(&et->node_cnt)) {
563 write_lock(&et->lock);
564 node_cnt += __free_extent_tree(sbi, et, true);
565 write_unlock(&et->lock);
566 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800567
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800568 list_del_init(&et->list);
569 radix_tree_delete(&sbi->extent_tree_root, et->ino);
570 kmem_cache_free(extent_tree_slab, et);
571 atomic_dec(&sbi->total_ext_tree);
572 atomic_dec(&sbi->total_zombie_tree);
573 tree_cnt++;
Chao Yua28ef1f2015-07-08 17:59:36 +0800574
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800575 if (node_cnt + tree_cnt >= nr_shrink)
576 goto unlock_out;
Chao Yua28ef1f2015-07-08 17:59:36 +0800577 }
578 up_write(&sbi->extent_tree_lock);
579
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800580free_node:
Chao Yua28ef1f2015-07-08 17:59:36 +0800581 /* 2. remove LRU extent entries */
582 if (!down_write_trylock(&sbi->extent_tree_lock))
583 goto out;
584
585 remained = nr_shrink - (node_cnt + tree_cnt);
586
587 spin_lock(&sbi->extent_lock);
588 list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) {
589 if (!remained--)
590 break;
591 list_del_init(&en->list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800592 do_free = true;
Chao Yua28ef1f2015-07-08 17:59:36 +0800593 }
594 spin_unlock(&sbi->extent_lock);
595
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800596 if (do_free == false)
597 goto unlock_out;
598
Chao Yu100136a2015-09-11 14:43:02 +0800599 /*
600 * reset ino for searching victims from beginning of global extent tree.
601 */
602 ino = F2FS_ROOT_INO(sbi);
603
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800604 while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root,
Chao Yua28ef1f2015-07-08 17:59:36 +0800605 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
606 unsigned i;
607
608 ino = treevec[found - 1]->ino + 1;
609 for (i = 0; i < found; i++) {
610 struct extent_tree *et = treevec[i];
611
Chao Yu9b72a382016-01-08 20:24:00 +0800612 if (!atomic_read(&et->node_cnt))
613 continue;
614
Jaegeuk Kim760de792015-11-30 16:26:44 -0800615 if (write_trylock(&et->lock)) {
616 node_cnt += __free_extent_tree(sbi, et, false);
617 write_unlock(&et->lock);
618 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800619
620 if (node_cnt + tree_cnt >= nr_shrink)
Chao Yubeaa57d2015-10-22 18:24:12 +0800621 goto unlock_out;
Chao Yua28ef1f2015-07-08 17:59:36 +0800622 }
623 }
624unlock_out:
625 up_write(&sbi->extent_tree_lock);
626out:
627 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
628
629 return node_cnt + tree_cnt;
630}
631
632unsigned int f2fs_destroy_extent_node(struct inode *inode)
633{
634 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
635 struct extent_tree *et = F2FS_I(inode)->extent_tree;
636 unsigned int node_cnt = 0;
637
Chao Yu9b72a382016-01-08 20:24:00 +0800638 if (!et || !atomic_read(&et->node_cnt))
Chao Yua28ef1f2015-07-08 17:59:36 +0800639 return 0;
640
641 write_lock(&et->lock);
642 node_cnt = __free_extent_tree(sbi, et, true);
643 write_unlock(&et->lock);
644
645 return node_cnt;
646}
647
648void f2fs_destroy_extent_tree(struct inode *inode)
649{
650 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
651 struct extent_tree *et = F2FS_I(inode)->extent_tree;
652 unsigned int node_cnt = 0;
653
654 if (!et)
655 return;
656
Chao Yu68e35382016-01-08 20:22:52 +0800657 if (inode->i_nlink && !is_bad_inode(inode) &&
658 atomic_read(&et->node_cnt)) {
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800659 down_write(&sbi->extent_tree_lock);
660 list_add_tail(&et->list, &sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800661 atomic_inc(&sbi->total_zombie_tree);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800662 up_write(&sbi->extent_tree_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800663 return;
664 }
665
666 /* free all extent info belong to this extent tree */
667 node_cnt = f2fs_destroy_extent_node(inode);
668
669 /* delete extent tree entry in radix tree */
670 down_write(&sbi->extent_tree_lock);
Chao Yu68e35382016-01-08 20:22:52 +0800671 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
Chao Yua28ef1f2015-07-08 17:59:36 +0800672 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
673 kmem_cache_free(extent_tree_slab, et);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800674 atomic_dec(&sbi->total_ext_tree);
Chao Yua28ef1f2015-07-08 17:59:36 +0800675 up_write(&sbi->extent_tree_lock);
676
677 F2FS_I(inode)->extent_tree = NULL;
678
679 trace_f2fs_destroy_extent_tree(inode, node_cnt);
680}
681
682bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
683 struct extent_info *ei)
684{
685 if (!f2fs_may_extent_tree(inode))
686 return false;
687
688 return f2fs_lookup_extent_tree(inode, pgofs, ei);
689}
690
691void f2fs_update_extent_cache(struct dnode_of_data *dn)
692{
693 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
694 pgoff_t fofs;
695
696 if (!f2fs_may_extent_tree(dn->inode))
697 return;
698
699 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
700
Chao Yu19b2c302015-08-26 20:34:48 +0800701
Chao Yua28ef1f2015-07-08 17:59:36 +0800702 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
703 dn->ofs_in_node;
704
Chao Yu19b2c302015-08-26 20:34:48 +0800705 if (f2fs_update_extent_tree_range(dn->inode, fofs, dn->data_blkaddr, 1))
706 sync_inode_page(dn);
707}
708
709void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
710 pgoff_t fofs, block_t blkaddr, unsigned int len)
711
712{
713 if (!f2fs_may_extent_tree(dn->inode))
714 return;
715
716 if (f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len))
Chao Yua28ef1f2015-07-08 17:59:36 +0800717 sync_inode_page(dn);
718}
719
720void init_extent_cache_info(struct f2fs_sb_info *sbi)
721{
722 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
723 init_rwsem(&sbi->extent_tree_lock);
724 INIT_LIST_HEAD(&sbi->extent_list);
725 spin_lock_init(&sbi->extent_lock);
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800726 atomic_set(&sbi->total_ext_tree, 0);
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800727 INIT_LIST_HEAD(&sbi->zombie_list);
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800728 atomic_set(&sbi->total_zombie_tree, 0);
Chao Yua28ef1f2015-07-08 17:59:36 +0800729 atomic_set(&sbi->total_ext_node, 0);
730}
731
732int __init create_extent_cache(void)
733{
734 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
735 sizeof(struct extent_tree));
736 if (!extent_tree_slab)
737 return -ENOMEM;
738 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
739 sizeof(struct extent_node));
740 if (!extent_node_slab) {
741 kmem_cache_destroy(extent_tree_slab);
742 return -ENOMEM;
743 }
744 return 0;
745}
746
747void destroy_extent_cache(void)
748{
749 kmem_cache_destroy(extent_node_slab);
750 kmem_cache_destroy(extent_tree_slab);
751}