blob: a38ee9bec4ba0c37f632eae08378649bb33c50a8 [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);
39 et->count++;
40 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);
48 et->count--;
49 atomic_dec(&sbi->total_ext_node);
50
51 if (et->cached_en == en)
52 et->cached_en = NULL;
53}
54
55static struct extent_tree *__grab_extent_tree(struct inode *inode)
56{
57 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
58 struct extent_tree *et;
59 nid_t ino = inode->i_ino;
60
61 down_write(&sbi->extent_tree_lock);
62 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
63 if (!et) {
64 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
65 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
66 memset(et, 0, sizeof(struct extent_tree));
67 et->ino = ino;
68 et->root = RB_ROOT;
69 et->cached_en = NULL;
70 rwlock_init(&et->lock);
71 atomic_set(&et->refcount, 0);
72 et->count = 0;
73 sbi->total_ext_tree++;
74 }
75 atomic_inc(&et->refcount);
76 up_write(&sbi->extent_tree_lock);
77
78 /* never died until evict_inode */
79 F2FS_I(inode)->extent_tree = et;
80
81 return et;
82}
83
Chao Yu91c481f2015-08-19 19:12:20 +080084static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,
85 struct extent_tree *et, unsigned int fofs)
Chao Yua28ef1f2015-07-08 17:59:36 +080086{
87 struct rb_node *node = et->root.rb_node;
Fan Lif8b703d2015-08-18 17:13:13 +080088 struct extent_node *en = et->cached_en;
Chao Yua28ef1f2015-07-08 17:59:36 +080089
Fan Lif8b703d2015-08-18 17:13:13 +080090 if (en) {
91 struct extent_info *cei = &en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +080092
Chao Yu91c481f2015-08-19 19:12:20 +080093 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) {
94 stat_inc_cached_node_hit(sbi);
Fan Lif8b703d2015-08-18 17:13:13 +080095 return en;
Chao Yu91c481f2015-08-19 19:12:20 +080096 }
Chao Yua28ef1f2015-07-08 17:59:36 +080097 }
98
99 while (node) {
100 en = rb_entry(node, struct extent_node, rb_node);
101
Chao Yu029e13c2015-08-19 19:13:25 +0800102 if (fofs < en->ei.fofs) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800103 node = node->rb_left;
Chao Yu029e13c2015-08-19 19:13:25 +0800104 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yua28ef1f2015-07-08 17:59:36 +0800105 node = node->rb_right;
Chao Yu029e13c2015-08-19 19:13:25 +0800106 } else {
107 stat_inc_rbtree_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800108 return en;
Chao Yu029e13c2015-08-19 19:13:25 +0800109 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800110 }
111 return NULL;
112}
113
Chao Yua6f78342015-08-19 19:14:15 +0800114static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
115 struct extent_tree *et, struct extent_info *ei)
Chao Yua28ef1f2015-07-08 17:59:36 +0800116{
117 struct rb_node **p = &et->root.rb_node;
Chao Yua28ef1f2015-07-08 17:59:36 +0800118 struct extent_node *en;
119
Chao Yua6f78342015-08-19 19:14:15 +0800120 en = __attach_extent_node(sbi, et, ei, NULL, p);
Chao Yua28ef1f2015-07-08 17:59:36 +0800121 if (!en)
122 return NULL;
Chao Yua6f78342015-08-19 19:14:15 +0800123
124 et->largest = en->ei;
Chao Yua28ef1f2015-07-08 17:59:36 +0800125 et->cached_en = en;
126 return en;
127}
128
129static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
130 struct extent_tree *et, bool free_all)
131{
132 struct rb_node *node, *next;
133 struct extent_node *en;
134 unsigned int count = et->count;
135
136 node = rb_first(&et->root);
137 while (node) {
138 next = rb_next(node);
139 en = rb_entry(node, struct extent_node, rb_node);
140
141 if (free_all) {
142 spin_lock(&sbi->extent_lock);
143 if (!list_empty(&en->list))
144 list_del_init(&en->list);
145 spin_unlock(&sbi->extent_lock);
146 }
147
148 if (free_all || list_empty(&en->list)) {
149 __detach_extent_node(sbi, et, en);
150 kmem_cache_free(extent_node_slab, en);
151 }
152 node = next;
153 }
154
155 return count - et->count;
156}
157
Fan Li41a099d2015-09-17 18:24:17 +0800158static void __drop_largest_extent(struct inode *inode,
159 pgoff_t fofs, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800160{
161 struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
162
Fan Li41a099d2015-09-17 18:24:17 +0800163 if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs)
Chao Yua28ef1f2015-07-08 17:59:36 +0800164 largest->len = 0;
165}
166
Chao Yu54d71852015-08-28 18:18:57 +0800167void f2fs_drop_largest_extent(struct inode *inode, pgoff_t fofs)
168{
169 if (!f2fs_may_extent_tree(inode))
170 return;
171
Fan Li41a099d2015-09-17 18:24:17 +0800172 __drop_largest_extent(inode, fofs, 1);
Chao Yu54d71852015-08-28 18:18:57 +0800173}
174
Chao Yua28ef1f2015-07-08 17:59:36 +0800175void f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
176{
177 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
178 struct extent_tree *et;
179 struct extent_node *en;
180 struct extent_info ei;
181
182 if (!f2fs_may_extent_tree(inode))
183 return;
184
185 et = __grab_extent_tree(inode);
186
187 if (!i_ext || le32_to_cpu(i_ext->len) < F2FS_MIN_EXTENT_LEN)
188 return;
189
190 set_extent_info(&ei, le32_to_cpu(i_ext->fofs),
191 le32_to_cpu(i_ext->blk), le32_to_cpu(i_ext->len));
192
193 write_lock(&et->lock);
194 if (et->count)
195 goto out;
196
Chao Yua6f78342015-08-19 19:14:15 +0800197 en = __init_extent_tree(sbi, et, &ei);
Chao Yua28ef1f2015-07-08 17:59:36 +0800198 if (en) {
199 spin_lock(&sbi->extent_lock);
200 list_add_tail(&en->list, &sbi->extent_list);
201 spin_unlock(&sbi->extent_lock);
202 }
203out:
204 write_unlock(&et->lock);
205}
206
207static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
208 struct extent_info *ei)
209{
210 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
211 struct extent_tree *et = F2FS_I(inode)->extent_tree;
212 struct extent_node *en;
213 bool ret = false;
214
215 f2fs_bug_on(sbi, !et);
216
217 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
218
219 read_lock(&et->lock);
220
221 if (et->largest.fofs <= pgofs &&
222 et->largest.fofs + et->largest.len > pgofs) {
223 *ei = et->largest;
224 ret = true;
Chao Yu91c481f2015-08-19 19:12:20 +0800225 stat_inc_largest_node_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800226 goto out;
227 }
228
Chao Yu91c481f2015-08-19 19:12:20 +0800229 en = __lookup_extent_tree(sbi, et, pgofs);
Chao Yua28ef1f2015-07-08 17:59:36 +0800230 if (en) {
231 *ei = en->ei;
232 spin_lock(&sbi->extent_lock);
233 if (!list_empty(&en->list))
234 list_move_tail(&en->list, &sbi->extent_list);
235 et->cached_en = en;
236 spin_unlock(&sbi->extent_lock);
237 ret = true;
Chao Yua28ef1f2015-07-08 17:59:36 +0800238 }
239out:
Chao Yu727edac2015-07-15 17:29:49 +0800240 stat_inc_total_hit(sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +0800241 read_unlock(&et->lock);
242
243 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
244 return ret;
245}
246
Fan Li0f825ee2015-07-15 18:05:17 +0800247
248/*
249 * lookup extent at @fofs, if hit, return the extent
250 * if not, return NULL and
251 * @prev_ex: extent before fofs
252 * @next_ex: extent after fofs
253 * @insert_p: insert point for new extent at fofs
254 * in order to simpfy the insertion after.
255 * tree must stay unchanged between lookup and insertion.
256 */
257static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
Chao Yu91c481f2015-08-19 19:12:20 +0800258 unsigned int fofs,
259 struct extent_node **prev_ex,
Fan Li0f825ee2015-07-15 18:05:17 +0800260 struct extent_node **next_ex,
261 struct rb_node ***insert_p,
262 struct rb_node **insert_parent)
263{
264 struct rb_node **pnode = &et->root.rb_node;
265 struct rb_node *parent = NULL, *tmp_node;
Chao Yudac2dde2015-08-19 19:16:09 +0800266 struct extent_node *en = et->cached_en;
Fan Li0f825ee2015-07-15 18:05:17 +0800267
Chao Yudac2dde2015-08-19 19:16:09 +0800268 *insert_p = NULL;
269 *insert_parent = NULL;
270 *prev_ex = NULL;
271 *next_ex = NULL;
272
273 if (RB_EMPTY_ROOT(&et->root))
274 return NULL;
275
276 if (en) {
277 struct extent_info *cei = &en->ei;
Fan Li0f825ee2015-07-15 18:05:17 +0800278
279 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
Chao Yudac2dde2015-08-19 19:16:09 +0800280 goto lookup_neighbors;
Fan Li0f825ee2015-07-15 18:05:17 +0800281 }
282
283 while (*pnode) {
284 parent = *pnode;
285 en = rb_entry(*pnode, struct extent_node, rb_node);
286
287 if (fofs < en->ei.fofs)
288 pnode = &(*pnode)->rb_left;
289 else if (fofs >= en->ei.fofs + en->ei.len)
290 pnode = &(*pnode)->rb_right;
291 else
Chao Yudac2dde2015-08-19 19:16:09 +0800292 goto lookup_neighbors;
Fan Li0f825ee2015-07-15 18:05:17 +0800293 }
294
295 *insert_p = pnode;
296 *insert_parent = parent;
297
298 en = rb_entry(parent, struct extent_node, rb_node);
299 tmp_node = parent;
300 if (parent && fofs > en->ei.fofs)
301 tmp_node = rb_next(parent);
302 *next_ex = tmp_node ?
303 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
304
305 tmp_node = parent;
306 if (parent && fofs < en->ei.fofs)
307 tmp_node = rb_prev(parent);
308 *prev_ex = tmp_node ?
309 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800310 return NULL;
Chao Yudac2dde2015-08-19 19:16:09 +0800311
312lookup_neighbors:
313 if (fofs == en->ei.fofs) {
314 /* lookup prev node for merging backward later */
315 tmp_node = rb_prev(&en->rb_node);
316 *prev_ex = tmp_node ?
317 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
318 }
319 if (fofs == en->ei.fofs + en->ei.len - 1) {
320 /* lookup next node for merging frontward later */
321 tmp_node = rb_next(&en->rb_node);
322 *next_ex = tmp_node ?
323 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
324 }
325 return en;
Fan Li0f825ee2015-07-15 18:05:17 +0800326}
327
Chao Yuef05e222015-08-19 19:15:09 +0800328static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
Fan Li0f825ee2015-07-15 18:05:17 +0800329 struct extent_tree *et, struct extent_info *ei,
330 struct extent_node **den,
331 struct extent_node *prev_ex,
Chao Yuef05e222015-08-19 19:15:09 +0800332 struct extent_node *next_ex)
Fan Li0f825ee2015-07-15 18:05:17 +0800333{
Fan Li0f825ee2015-07-15 18:05:17 +0800334 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800335
336 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
Fan Li0f825ee2015-07-15 18:05:17 +0800337 prev_ex->ei.len += ei->len;
338 ei = &prev_ex->ei;
339 en = prev_ex;
340 }
Chao Yuef05e222015-08-19 19:15:09 +0800341
Fan Li0f825ee2015-07-15 18:05:17 +0800342 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
Chao Yuef05e222015-08-19 19:15:09 +0800343 if (en) {
Fan Li0f825ee2015-07-15 18:05:17 +0800344 __detach_extent_node(sbi, et, prev_ex);
345 *den = prev_ex;
346 }
347 next_ex->ei.fofs = ei->fofs;
348 next_ex->ei.blk = ei->blk;
349 next_ex->ei.len += ei->len;
350 en = next_ex;
351 }
Chao Yuef05e222015-08-19 19:15:09 +0800352
353 if (en) {
Chao Yu4abd3f52015-09-22 21:07:47 +0800354 __try_update_largest_extent(et, en);
Chao Yuef05e222015-08-19 19:15:09 +0800355 et->cached_en = en;
356 }
357 return en;
358}
359
360static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
361 struct extent_tree *et, struct extent_info *ei,
362 struct rb_node **insert_p,
363 struct rb_node *insert_parent)
364{
365 struct rb_node **p = &et->root.rb_node;
366 struct rb_node *parent = NULL;
367 struct extent_node *en = NULL;
Fan Li0f825ee2015-07-15 18:05:17 +0800368
369 if (insert_p && insert_parent) {
370 parent = insert_parent;
371 p = insert_p;
372 goto do_insert;
373 }
374
375 while (*p) {
376 parent = *p;
377 en = rb_entry(parent, struct extent_node, rb_node);
378
379 if (ei->fofs < en->ei.fofs)
380 p = &(*p)->rb_left;
381 else if (ei->fofs >= en->ei.fofs + en->ei.len)
382 p = &(*p)->rb_right;
383 else
384 f2fs_bug_on(sbi, 1);
385 }
386do_insert:
387 en = __attach_extent_node(sbi, et, ei, parent, p);
388 if (!en)
389 return NULL;
Chao Yuef05e222015-08-19 19:15:09 +0800390
Chao Yu4abd3f52015-09-22 21:07:47 +0800391 __try_update_largest_extent(et, en);
Fan Li0f825ee2015-07-15 18:05:17 +0800392 et->cached_en = en;
393 return en;
394}
395
Jaegeuk Kim514053e2015-09-15 14:46:43 -0700396static unsigned int f2fs_update_extent_tree_range(struct inode *inode,
Chao Yu19b2c302015-08-26 20:34:48 +0800397 pgoff_t fofs, block_t blkaddr, unsigned int len)
Chao Yua28ef1f2015-07-08 17:59:36 +0800398{
399 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
400 struct extent_tree *et = F2FS_I(inode)->extent_tree;
Fan Li4d1fa812015-09-17 18:42:06 +0800401 struct extent_node *en = NULL, *en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800402 struct extent_node *prev_en = NULL, *next_en = NULL;
Chao Yua28ef1f2015-07-08 17:59:36 +0800403 struct extent_info ei, dei, prev;
Fan Li0f825ee2015-07-15 18:05:17 +0800404 struct rb_node **insert_p = NULL, *insert_parent = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800405 unsigned int end = fofs + len;
406 unsigned int pos = (unsigned int)fofs;
Chao Yua28ef1f2015-07-08 17:59:36 +0800407
408 if (!et)
409 return false;
410
Chao Yu744288c2015-09-06 17:50:13 +0800411 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
412
Chao Yua28ef1f2015-07-08 17:59:36 +0800413 write_lock(&et->lock);
414
415 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT)) {
416 write_unlock(&et->lock);
417 return false;
418 }
419
420 prev = et->largest;
421 dei.len = 0;
422
Fan Li4d1fa812015-09-17 18:42:06 +0800423 /*
424 * drop largest extent before lookup, in case it's already
425 * been shrunk from extent tree
426 */
Fan Li41a099d2015-09-17 18:24:17 +0800427 __drop_largest_extent(inode, fofs, len);
Chao Yua28ef1f2015-07-08 17:59:36 +0800428
Chao Yu19b2c302015-08-26 20:34:48 +0800429 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
430 en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
Fan Li0f825ee2015-07-15 18:05:17 +0800431 &insert_p, &insert_parent);
Fan Li4d1fa812015-09-17 18:42:06 +0800432 if (!en)
433 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800434
Chao Yu19b2c302015-08-26 20:34:48 +0800435 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
Fan Li4d1fa812015-09-17 18:42:06 +0800436 while (en && en->ei.fofs < end) {
437 unsigned int org_end;
438 int parts = 0; /* # of parts current extent split into */
Chao Yua28ef1f2015-07-08 17:59:36 +0800439
Fan Li4d1fa812015-09-17 18:42:06 +0800440 next_en = en1 = NULL;
Chao Yu19b2c302015-08-26 20:34:48 +0800441
442 dei = en->ei;
Fan Li4d1fa812015-09-17 18:42:06 +0800443 org_end = dei.fofs + dei.len;
444 f2fs_bug_on(sbi, pos >= org_end);
Chao Yu19b2c302015-08-26 20:34:48 +0800445
Fan Li4d1fa812015-09-17 18:42:06 +0800446 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
447 en->ei.len = pos - en->ei.fofs;
448 prev_en = en;
449 parts = 1;
Chao Yua28ef1f2015-07-08 17:59:36 +0800450 }
451
Fan Li4d1fa812015-09-17 18:42:06 +0800452 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
453 if (parts) {
Chao Yu19b2c302015-08-26 20:34:48 +0800454 set_extent_info(&ei, end,
455 end - dei.fofs + dei.blk,
Fan Li4d1fa812015-09-17 18:42:06 +0800456 org_end - end);
457 en1 = __insert_extent_tree(sbi, et, &ei,
458 NULL, NULL);
459 next_en = en1;
460 } else {
461 en->ei.fofs = end;
462 en->ei.blk += end - dei.fofs;
463 en->ei.len -= end - dei.fofs;
464 next_en = en;
Chao Yu19b2c302015-08-26 20:34:48 +0800465 }
Fan Li4d1fa812015-09-17 18:42:06 +0800466 parts++;
Chao Yu19b2c302015-08-26 20:34:48 +0800467 }
Fan Li4d1fa812015-09-17 18:42:06 +0800468
469 if (!next_en) {
470 struct rb_node *node = rb_next(&en->rb_node);
471
472 next_en = node ?
473 rb_entry(node, struct extent_node, rb_node)
474 : NULL;
475 }
476
Chao Yu4abd3f52015-09-22 21:07:47 +0800477 if (parts)
478 __try_update_largest_extent(et, en);
479 else
Fan Li4d1fa812015-09-17 18:42:06 +0800480 __detach_extent_node(sbi, et, en);
Fan Li4d1fa812015-09-17 18:42:06 +0800481
482 /*
483 * if original extent is split into zero or two parts, extent
484 * tree has been altered by deletion or insertion, therefore
485 * invalidate pointers regard to tree.
486 */
487 if (parts != 1) {
488 insert_p = NULL;
489 insert_parent = NULL;
490 }
491
492 /* update in global extent list */
Chao Yu19b2c302015-08-26 20:34:48 +0800493 spin_lock(&sbi->extent_lock);
Fan Li4d1fa812015-09-17 18:42:06 +0800494 if (!parts && !list_empty(&en->list))
Chao Yu19b2c302015-08-26 20:34:48 +0800495 list_del(&en->list);
496 if (en1)
497 list_add_tail(&en1->list, &sbi->extent_list);
Chao Yu19b2c302015-08-26 20:34:48 +0800498 spin_unlock(&sbi->extent_lock);
499
Fan Li4d1fa812015-09-17 18:42:06 +0800500 /* release extent node */
501 if (!parts)
Chao Yu19b2c302015-08-26 20:34:48 +0800502 kmem_cache_free(extent_node_slab, en);
Fan Li4d1fa812015-09-17 18:42:06 +0800503
504 en = next_en;
Chao Yua28ef1f2015-07-08 17:59:36 +0800505 }
506
Chao Yua28ef1f2015-07-08 17:59:36 +0800507 /* 3. update extent in extent cache */
508 if (blkaddr) {
Chao Yu19b2c302015-08-26 20:34:48 +0800509 struct extent_node *den = NULL;
510
511 set_extent_info(&ei, fofs, blkaddr, len);
Fan Li4d1fa812015-09-17 18:42:06 +0800512 en1 = __try_merge_extent_node(sbi, et, &ei, &den,
Chao Yu19b2c302015-08-26 20:34:48 +0800513 prev_en, next_en);
Fan Li4d1fa812015-09-17 18:42:06 +0800514 if (!en1)
515 en1 = __insert_extent_tree(sbi, et, &ei,
Chao Yuef05e222015-08-19 19:15:09 +0800516 insert_p, insert_parent);
Chao Yua28ef1f2015-07-08 17:59:36 +0800517
518 /* give up extent_cache, if split and small updates happen */
519 if (dei.len >= 1 &&
520 prev.len < F2FS_MIN_EXTENT_LEN &&
521 et->largest.len < F2FS_MIN_EXTENT_LEN) {
522 et->largest.len = 0;
523 set_inode_flag(F2FS_I(inode), FI_NO_EXTENT);
524 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800525
Chao Yu19b2c302015-08-26 20:34:48 +0800526 spin_lock(&sbi->extent_lock);
Fan Li4d1fa812015-09-17 18:42:06 +0800527 if (en1) {
528 if (list_empty(&en1->list))
529 list_add_tail(&en1->list, &sbi->extent_list);
Chao Yu19b2c302015-08-26 20:34:48 +0800530 else
Fan Li4d1fa812015-09-17 18:42:06 +0800531 list_move_tail(&en1->list, &sbi->extent_list);
Chao Yu19b2c302015-08-26 20:34:48 +0800532 }
533 if (den && !list_empty(&den->list))
534 list_del(&den->list);
535 spin_unlock(&sbi->extent_lock);
Chao Yua28ef1f2015-07-08 17:59:36 +0800536
Chao Yu19b2c302015-08-26 20:34:48 +0800537 if (den)
538 kmem_cache_free(extent_node_slab, den);
539 }
Chao Yua28ef1f2015-07-08 17:59:36 +0800540
541 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
542 __free_extent_tree(sbi, et, true);
543
544 write_unlock(&et->lock);
545
546 return !__is_extent_same(&prev, &et->largest);
547}
548
549unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
550{
551 struct extent_tree *treevec[EXT_TREE_VEC_SIZE];
552 struct extent_node *en, *tmp;
553 unsigned long ino = F2FS_ROOT_INO(sbi);
554 struct radix_tree_root *root = &sbi->extent_tree_root;
555 unsigned int found;
556 unsigned int node_cnt = 0, tree_cnt = 0;
557 int remained;
558
559 if (!test_opt(sbi, EXTENT_CACHE))
560 return 0;
561
562 if (!down_write_trylock(&sbi->extent_tree_lock))
563 goto out;
564
565 /* 1. remove unreferenced extent tree */
566 while ((found = radix_tree_gang_lookup(root,
567 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
568 unsigned i;
569
570 ino = treevec[found - 1]->ino + 1;
571 for (i = 0; i < found; i++) {
572 struct extent_tree *et = treevec[i];
573
574 if (!atomic_read(&et->refcount)) {
575 write_lock(&et->lock);
576 node_cnt += __free_extent_tree(sbi, et, true);
577 write_unlock(&et->lock);
578
579 radix_tree_delete(root, et->ino);
580 kmem_cache_free(extent_tree_slab, et);
581 sbi->total_ext_tree--;
582 tree_cnt++;
583
584 if (node_cnt + tree_cnt >= nr_shrink)
585 goto unlock_out;
586 }
587 }
588 }
589 up_write(&sbi->extent_tree_lock);
590
591 /* 2. remove LRU extent entries */
592 if (!down_write_trylock(&sbi->extent_tree_lock))
593 goto out;
594
595 remained = nr_shrink - (node_cnt + tree_cnt);
596
597 spin_lock(&sbi->extent_lock);
598 list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) {
599 if (!remained--)
600 break;
601 list_del_init(&en->list);
602 }
603 spin_unlock(&sbi->extent_lock);
604
Chao Yu100136a2015-09-11 14:43:02 +0800605 /*
606 * reset ino for searching victims from beginning of global extent tree.
607 */
608 ino = F2FS_ROOT_INO(sbi);
609
Chao Yua28ef1f2015-07-08 17:59:36 +0800610 while ((found = radix_tree_gang_lookup(root,
611 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
612 unsigned i;
613
614 ino = treevec[found - 1]->ino + 1;
615 for (i = 0; i < found; i++) {
616 struct extent_tree *et = treevec[i];
617
618 write_lock(&et->lock);
619 node_cnt += __free_extent_tree(sbi, et, false);
620 write_unlock(&et->lock);
621
622 if (node_cnt + tree_cnt >= nr_shrink)
623 break;
624 }
625 }
626unlock_out:
627 up_write(&sbi->extent_tree_lock);
628out:
629 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
630
631 return node_cnt + tree_cnt;
632}
633
634unsigned int f2fs_destroy_extent_node(struct inode *inode)
635{
636 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
637 struct extent_tree *et = F2FS_I(inode)->extent_tree;
638 unsigned int node_cnt = 0;
639
640 if (!et)
641 return 0;
642
643 write_lock(&et->lock);
644 node_cnt = __free_extent_tree(sbi, et, true);
645 write_unlock(&et->lock);
646
647 return node_cnt;
648}
649
650void f2fs_destroy_extent_tree(struct inode *inode)
651{
652 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
653 struct extent_tree *et = F2FS_I(inode)->extent_tree;
654 unsigned int node_cnt = 0;
655
656 if (!et)
657 return;
658
659 if (inode->i_nlink && !is_bad_inode(inode) && et->count) {
660 atomic_dec(&et->refcount);
661 return;
662 }
663
664 /* free all extent info belong to this extent tree */
665 node_cnt = f2fs_destroy_extent_node(inode);
666
667 /* delete extent tree entry in radix tree */
668 down_write(&sbi->extent_tree_lock);
669 atomic_dec(&et->refcount);
670 f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count);
671 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
672 kmem_cache_free(extent_tree_slab, et);
673 sbi->total_ext_tree--;
674 up_write(&sbi->extent_tree_lock);
675
676 F2FS_I(inode)->extent_tree = NULL;
677
678 trace_f2fs_destroy_extent_tree(inode, node_cnt);
679}
680
681bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
682 struct extent_info *ei)
683{
684 if (!f2fs_may_extent_tree(inode))
685 return false;
686
687 return f2fs_lookup_extent_tree(inode, pgofs, ei);
688}
689
690void f2fs_update_extent_cache(struct dnode_of_data *dn)
691{
692 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
693 pgoff_t fofs;
694
695 if (!f2fs_may_extent_tree(dn->inode))
696 return;
697
698 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
699
Chao Yu19b2c302015-08-26 20:34:48 +0800700
Chao Yua28ef1f2015-07-08 17:59:36 +0800701 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
702 dn->ofs_in_node;
703
Chao Yu19b2c302015-08-26 20:34:48 +0800704 if (f2fs_update_extent_tree_range(dn->inode, fofs, dn->data_blkaddr, 1))
705 sync_inode_page(dn);
706}
707
708void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
709 pgoff_t fofs, block_t blkaddr, unsigned int len)
710
711{
712 if (!f2fs_may_extent_tree(dn->inode))
713 return;
714
715 if (f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len))
Chao Yua28ef1f2015-07-08 17:59:36 +0800716 sync_inode_page(dn);
717}
718
719void init_extent_cache_info(struct f2fs_sb_info *sbi)
720{
721 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
722 init_rwsem(&sbi->extent_tree_lock);
723 INIT_LIST_HEAD(&sbi->extent_list);
724 spin_lock_init(&sbi->extent_lock);
725 sbi->total_ext_tree = 0;
726 atomic_set(&sbi->total_ext_node, 0);
727}
728
729int __init create_extent_cache(void)
730{
731 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
732 sizeof(struct extent_tree));
733 if (!extent_tree_slab)
734 return -ENOMEM;
735 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
736 sizeof(struct extent_node));
737 if (!extent_node_slab) {
738 kmem_cache_destroy(extent_tree_slab);
739 return -ENOMEM;
740 }
741 return 0;
742}
743
744void destroy_extent_cache(void)
745{
746 kmem_cache_destroy(extent_node_slab);
747 kmem_cache_destroy(extent_tree_slab);
748}