Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / fs / f2fs / extent_cache.c
blobc859bb04472807e20ef3c0742080023b54bdc196
1 /*
2 * f2fs extent cache support
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>
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.
14 #include <linux/fs.h>
15 #include <linux/f2fs_fs.h>
17 #include "f2fs.h"
18 #include "node.h"
19 #include <trace/events/f2fs.h>
21 static struct kmem_cache *extent_tree_slab;
22 static struct kmem_cache *extent_node_slab;
24 static 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)
28 struct extent_node *en;
30 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
31 if (!en)
32 return NULL;
34 en->ei = *ei;
35 INIT_LIST_HEAD(&en->list);
36 en->et = et;
38 rb_link_node(&en->rb_node, parent, p);
39 rb_insert_color(&en->rb_node, &et->root);
40 atomic_inc(&et->node_cnt);
41 atomic_inc(&sbi->total_ext_node);
42 return en;
45 static void __detach_extent_node(struct f2fs_sb_info *sbi,
46 struct extent_tree *et, struct extent_node *en)
48 rb_erase(&en->rb_node, &et->root);
49 atomic_dec(&et->node_cnt);
50 atomic_dec(&sbi->total_ext_node);
52 if (et->cached_en == en)
53 et->cached_en = NULL;
54 kmem_cache_free(extent_node_slab, en);
58 * Flow to release an extent_node:
59 * 1. list_del_init
60 * 2. __detach_extent_node
61 * 3. kmem_cache_free.
63 static void __release_extent_node(struct f2fs_sb_info *sbi,
64 struct extent_tree *et, struct extent_node *en)
66 spin_lock(&sbi->extent_lock);
67 f2fs_bug_on(sbi, list_empty(&en->list));
68 list_del_init(&en->list);
69 spin_unlock(&sbi->extent_lock);
71 __detach_extent_node(sbi, et, en);
74 static struct extent_tree *__grab_extent_tree(struct inode *inode)
76 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
77 struct extent_tree *et;
78 nid_t ino = inode->i_ino;
80 down_write(&sbi->extent_tree_lock);
81 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);
90 INIT_LIST_HEAD(&et->list);
91 atomic_set(&et->node_cnt, 0);
92 atomic_inc(&sbi->total_ext_tree);
93 } else {
94 atomic_dec(&sbi->total_zombie_tree);
95 list_del_init(&et->list);
97 up_write(&sbi->extent_tree_lock);
99 /* never died until evict_inode */
100 F2FS_I(inode)->extent_tree = et;
102 return et;
105 static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,
106 struct extent_tree *et, unsigned int fofs)
108 struct rb_node *node = et->root.rb_node;
109 struct extent_node *en = et->cached_en;
111 if (en) {
112 struct extent_info *cei = &en->ei;
114 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) {
115 stat_inc_cached_node_hit(sbi);
116 return en;
120 while (node) {
121 en = rb_entry(node, struct extent_node, rb_node);
123 if (fofs < en->ei.fofs) {
124 node = node->rb_left;
125 } else if (fofs >= en->ei.fofs + en->ei.len) {
126 node = node->rb_right;
127 } else {
128 stat_inc_rbtree_node_hit(sbi);
129 return en;
132 return NULL;
135 static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
136 struct extent_tree *et, struct extent_info *ei)
138 struct rb_node **p = &et->root.rb_node;
139 struct extent_node *en;
141 en = __attach_extent_node(sbi, et, ei, NULL, p);
142 if (!en)
143 return NULL;
145 et->largest = en->ei;
146 et->cached_en = en;
147 return en;
150 static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
151 struct extent_tree *et)
153 struct rb_node *node, *next;
154 struct extent_node *en;
155 unsigned int count = atomic_read(&et->node_cnt);
157 node = rb_first(&et->root);
158 while (node) {
159 next = rb_next(node);
160 en = rb_entry(node, struct extent_node, rb_node);
161 __release_extent_node(sbi, et, en);
162 node = next;
165 return count - atomic_read(&et->node_cnt);
168 static void __drop_largest_extent(struct inode *inode,
169 pgoff_t fofs, unsigned int len)
171 struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
173 if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs)
174 largest->len = 0;
177 /* return true, if inode page is changed */
178 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
180 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
181 struct extent_tree *et;
182 struct extent_node *en;
183 struct extent_info ei;
185 if (!f2fs_may_extent_tree(inode)) {
186 /* drop largest extent */
187 if (i_ext && i_ext->len) {
188 i_ext->len = 0;
189 return true;
191 return false;
194 et = __grab_extent_tree(inode);
196 if (!i_ext || !i_ext->len)
197 return false;
199 set_extent_info(&ei, le32_to_cpu(i_ext->fofs),
200 le32_to_cpu(i_ext->blk), le32_to_cpu(i_ext->len));
202 write_lock(&et->lock);
203 if (atomic_read(&et->node_cnt))
204 goto out;
206 en = __init_extent_tree(sbi, et, &ei);
207 if (en) {
208 spin_lock(&sbi->extent_lock);
209 list_add_tail(&en->list, &sbi->extent_list);
210 spin_unlock(&sbi->extent_lock);
212 out:
213 write_unlock(&et->lock);
214 return false;
217 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
218 struct extent_info *ei)
220 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
221 struct extent_tree *et = F2FS_I(inode)->extent_tree;
222 struct extent_node *en;
223 bool ret = false;
225 f2fs_bug_on(sbi, !et);
227 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
229 read_lock(&et->lock);
231 if (et->largest.fofs <= pgofs &&
232 et->largest.fofs + et->largest.len > pgofs) {
233 *ei = et->largest;
234 ret = true;
235 stat_inc_largest_node_hit(sbi);
236 goto out;
239 en = __lookup_extent_tree(sbi, et, pgofs);
240 if (en) {
241 *ei = en->ei;
242 spin_lock(&sbi->extent_lock);
243 if (!list_empty(&en->list)) {
244 list_move_tail(&en->list, &sbi->extent_list);
245 et->cached_en = en;
247 spin_unlock(&sbi->extent_lock);
248 ret = true;
250 out:
251 stat_inc_total_hit(sbi);
252 read_unlock(&et->lock);
254 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
255 return ret;
260 * lookup extent at @fofs, if hit, return the extent
261 * if not, return NULL and
262 * @prev_ex: extent before fofs
263 * @next_ex: extent after fofs
264 * @insert_p: insert point for new extent at fofs
265 * in order to simpfy the insertion after.
266 * tree must stay unchanged between lookup and insertion.
268 static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
269 unsigned int fofs,
270 struct extent_node **prev_ex,
271 struct extent_node **next_ex,
272 struct rb_node ***insert_p,
273 struct rb_node **insert_parent)
275 struct rb_node **pnode = &et->root.rb_node;
276 struct rb_node *parent = NULL, *tmp_node;
277 struct extent_node *en = et->cached_en;
279 *insert_p = NULL;
280 *insert_parent = NULL;
281 *prev_ex = NULL;
282 *next_ex = NULL;
284 if (RB_EMPTY_ROOT(&et->root))
285 return NULL;
287 if (en) {
288 struct extent_info *cei = &en->ei;
290 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
291 goto lookup_neighbors;
294 while (*pnode) {
295 parent = *pnode;
296 en = rb_entry(*pnode, struct extent_node, rb_node);
298 if (fofs < en->ei.fofs)
299 pnode = &(*pnode)->rb_left;
300 else if (fofs >= en->ei.fofs + en->ei.len)
301 pnode = &(*pnode)->rb_right;
302 else
303 goto lookup_neighbors;
306 *insert_p = pnode;
307 *insert_parent = parent;
309 en = rb_entry(parent, struct extent_node, rb_node);
310 tmp_node = parent;
311 if (parent && fofs > en->ei.fofs)
312 tmp_node = rb_next(parent);
313 *next_ex = tmp_node ?
314 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
316 tmp_node = parent;
317 if (parent && fofs < en->ei.fofs)
318 tmp_node = rb_prev(parent);
319 *prev_ex = tmp_node ?
320 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
321 return NULL;
323 lookup_neighbors:
324 if (fofs == en->ei.fofs) {
325 /* lookup prev node for merging backward later */
326 tmp_node = rb_prev(&en->rb_node);
327 *prev_ex = tmp_node ?
328 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
330 if (fofs == en->ei.fofs + en->ei.len - 1) {
331 /* lookup next node for merging frontward later */
332 tmp_node = rb_next(&en->rb_node);
333 *next_ex = tmp_node ?
334 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
336 return en;
339 static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
340 struct extent_tree *et, struct extent_info *ei,
341 struct extent_node *prev_ex,
342 struct extent_node *next_ex)
344 struct extent_node *en = NULL;
346 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
347 prev_ex->ei.len += ei->len;
348 ei = &prev_ex->ei;
349 en = prev_ex;
352 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
353 if (en)
354 __release_extent_node(sbi, et, prev_ex);
355 next_ex->ei.fofs = ei->fofs;
356 next_ex->ei.blk = ei->blk;
357 next_ex->ei.len += ei->len;
358 en = next_ex;
361 if (!en)
362 return NULL;
364 __try_update_largest_extent(et, en);
366 spin_lock(&sbi->extent_lock);
367 if (!list_empty(&en->list)) {
368 list_move_tail(&en->list, &sbi->extent_list);
369 et->cached_en = en;
371 spin_unlock(&sbi->extent_lock);
372 return en;
375 static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
376 struct extent_tree *et, struct extent_info *ei,
377 struct rb_node **insert_p,
378 struct rb_node *insert_parent)
380 struct rb_node **p = &et->root.rb_node;
381 struct rb_node *parent = NULL;
382 struct extent_node *en = NULL;
384 if (insert_p && insert_parent) {
385 parent = insert_parent;
386 p = insert_p;
387 goto do_insert;
390 while (*p) {
391 parent = *p;
392 en = rb_entry(parent, struct extent_node, rb_node);
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);
401 do_insert:
402 en = __attach_extent_node(sbi, et, ei, parent, p);
403 if (!en)
404 return NULL;
406 __try_update_largest_extent(et, en);
408 /* update in global extent list */
409 spin_lock(&sbi->extent_lock);
410 list_add_tail(&en->list, &sbi->extent_list);
411 et->cached_en = en;
412 spin_unlock(&sbi->extent_lock);
413 return en;
416 static unsigned int f2fs_update_extent_tree_range(struct inode *inode,
417 pgoff_t fofs, block_t blkaddr, unsigned int len)
419 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
420 struct extent_tree *et = F2FS_I(inode)->extent_tree;
421 struct extent_node *en = NULL, *en1 = NULL;
422 struct extent_node *prev_en = NULL, *next_en = NULL;
423 struct extent_info ei, dei, prev;
424 struct rb_node **insert_p = NULL, *insert_parent = NULL;
425 unsigned int end = fofs + len;
426 unsigned int pos = (unsigned int)fofs;
428 if (!et)
429 return false;
431 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
433 write_lock(&et->lock);
435 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT)) {
436 write_unlock(&et->lock);
437 return false;
440 prev = et->largest;
441 dei.len = 0;
444 * drop largest extent before lookup, in case it's already
445 * been shrunk from extent tree
447 __drop_largest_extent(inode, fofs, len);
449 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
450 en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
451 &insert_p, &insert_parent);
452 if (!en)
453 en = next_en;
455 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
456 while (en && en->ei.fofs < end) {
457 unsigned int org_end;
458 int parts = 0; /* # of parts current extent split into */
460 next_en = en1 = NULL;
462 dei = en->ei;
463 org_end = dei.fofs + dei.len;
464 f2fs_bug_on(sbi, pos >= org_end);
466 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;
472 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
473 if (parts) {
474 set_extent_info(&ei, end,
475 end - dei.fofs + dei.blk,
476 org_end - end);
477 en1 = __insert_extent_tree(sbi, et, &ei,
478 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;
486 parts++;
489 if (!next_en) {
490 struct rb_node *node = rb_next(&en->rb_node);
492 next_en = node ?
493 rb_entry(node, struct extent_node, rb_node)
494 : NULL;
497 if (parts)
498 __try_update_largest_extent(et, en);
499 else
500 __release_extent_node(sbi, et, en);
503 * if original extent is split into zero or two parts, extent
504 * tree has been altered by deletion or insertion, therefore
505 * invalidate pointers regard to tree.
507 if (parts != 1) {
508 insert_p = NULL;
509 insert_parent = NULL;
511 en = next_en;
514 /* 3. update extent in extent cache */
515 if (blkaddr) {
517 set_extent_info(&ei, fofs, blkaddr, len);
518 if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
519 __insert_extent_tree(sbi, et, &ei,
520 insert_p, insert_parent);
522 /* give up extent_cache, if split and small updates happen */
523 if (dei.len >= 1 &&
524 prev.len < F2FS_MIN_EXTENT_LEN &&
525 et->largest.len < F2FS_MIN_EXTENT_LEN) {
526 et->largest.len = 0;
527 set_inode_flag(F2FS_I(inode), FI_NO_EXTENT);
531 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
532 __free_extent_tree(sbi, et);
534 write_unlock(&et->lock);
536 return !__is_extent_same(&prev, &et->largest);
539 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
541 struct extent_tree *et, *next;
542 struct extent_node *en;
543 unsigned int node_cnt = 0, tree_cnt = 0;
544 int remained;
546 if (!test_opt(sbi, EXTENT_CACHE))
547 return 0;
549 if (!atomic_read(&sbi->total_zombie_tree))
550 goto free_node;
552 if (!down_write_trylock(&sbi->extent_tree_lock))
553 goto out;
555 /* 1. remove unreferenced extent tree */
556 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
557 if (atomic_read(&et->node_cnt)) {
558 write_lock(&et->lock);
559 node_cnt += __free_extent_tree(sbi, et);
560 write_unlock(&et->lock);
562 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
563 list_del_init(&et->list);
564 radix_tree_delete(&sbi->extent_tree_root, et->ino);
565 kmem_cache_free(extent_tree_slab, et);
566 atomic_dec(&sbi->total_ext_tree);
567 atomic_dec(&sbi->total_zombie_tree);
568 tree_cnt++;
570 if (node_cnt + tree_cnt >= nr_shrink)
571 goto unlock_out;
572 cond_resched();
574 up_write(&sbi->extent_tree_lock);
576 free_node:
577 /* 2. remove LRU extent entries */
578 if (!down_write_trylock(&sbi->extent_tree_lock))
579 goto out;
581 remained = nr_shrink - (node_cnt + tree_cnt);
583 spin_lock(&sbi->extent_lock);
584 for (; remained > 0; remained--) {
585 if (list_empty(&sbi->extent_list))
586 break;
587 en = list_first_entry(&sbi->extent_list,
588 struct extent_node, list);
589 et = en->et;
590 if (!write_trylock(&et->lock)) {
591 /* refresh this extent node's position in extent list */
592 list_move_tail(&en->list, &sbi->extent_list);
593 continue;
596 list_del_init(&en->list);
597 spin_unlock(&sbi->extent_lock);
599 __detach_extent_node(sbi, et, en);
601 write_unlock(&et->lock);
602 node_cnt++;
603 spin_lock(&sbi->extent_lock);
605 spin_unlock(&sbi->extent_lock);
607 unlock_out:
608 up_write(&sbi->extent_tree_lock);
609 out:
610 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
612 return node_cnt + tree_cnt;
615 unsigned int f2fs_destroy_extent_node(struct inode *inode)
617 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
618 struct extent_tree *et = F2FS_I(inode)->extent_tree;
619 unsigned int node_cnt = 0;
621 if (!et || !atomic_read(&et->node_cnt))
622 return 0;
624 write_lock(&et->lock);
625 node_cnt = __free_extent_tree(sbi, et);
626 write_unlock(&et->lock);
628 return node_cnt;
631 void f2fs_destroy_extent_tree(struct inode *inode)
633 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
634 struct extent_tree *et = F2FS_I(inode)->extent_tree;
635 unsigned int node_cnt = 0;
637 if (!et)
638 return;
640 if (inode->i_nlink && !is_bad_inode(inode) &&
641 atomic_read(&et->node_cnt)) {
642 down_write(&sbi->extent_tree_lock);
643 list_add_tail(&et->list, &sbi->zombie_list);
644 atomic_inc(&sbi->total_zombie_tree);
645 up_write(&sbi->extent_tree_lock);
646 return;
649 /* free all extent info belong to this extent tree */
650 node_cnt = f2fs_destroy_extent_node(inode);
652 /* delete extent tree entry in radix tree */
653 down_write(&sbi->extent_tree_lock);
654 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
655 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
656 kmem_cache_free(extent_tree_slab, et);
657 atomic_dec(&sbi->total_ext_tree);
658 up_write(&sbi->extent_tree_lock);
660 F2FS_I(inode)->extent_tree = NULL;
662 trace_f2fs_destroy_extent_tree(inode, node_cnt);
665 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
666 struct extent_info *ei)
668 if (!f2fs_may_extent_tree(inode))
669 return false;
671 return f2fs_lookup_extent_tree(inode, pgofs, ei);
674 void f2fs_update_extent_cache(struct dnode_of_data *dn)
676 pgoff_t fofs;
677 block_t blkaddr;
679 if (!f2fs_may_extent_tree(dn->inode))
680 return;
682 if (dn->data_blkaddr == NEW_ADDR)
683 blkaddr = NULL_ADDR;
684 else
685 blkaddr = dn->data_blkaddr;
687 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
688 dn->ofs_in_node;
690 if (f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1))
691 sync_inode_page(dn);
694 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
695 pgoff_t fofs, block_t blkaddr, unsigned int len)
698 if (!f2fs_may_extent_tree(dn->inode))
699 return;
701 if (f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len))
702 sync_inode_page(dn);
705 void init_extent_cache_info(struct f2fs_sb_info *sbi)
707 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
708 init_rwsem(&sbi->extent_tree_lock);
709 INIT_LIST_HEAD(&sbi->extent_list);
710 spin_lock_init(&sbi->extent_lock);
711 atomic_set(&sbi->total_ext_tree, 0);
712 INIT_LIST_HEAD(&sbi->zombie_list);
713 atomic_set(&sbi->total_zombie_tree, 0);
714 atomic_set(&sbi->total_ext_node, 0);
717 int __init create_extent_cache(void)
719 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
720 sizeof(struct extent_tree));
721 if (!extent_tree_slab)
722 return -ENOMEM;
723 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
724 sizeof(struct extent_node));
725 if (!extent_node_slab) {
726 kmem_cache_destroy(extent_tree_slab);
727 return -ENOMEM;
729 return 0;
732 void destroy_extent_cache(void)
734 kmem_cache_destroy(extent_node_slab);
735 kmem_cache_destroy(extent_tree_slab);