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.
15 #include <linux/f2fs_fs.h>
19 #include <trace/events/f2fs.h>
21 static struct rb_entry *__lookup_rb_tree_fast(struct rb_entry *cached_re,
25 if (cached_re->ofs <= ofs &&
26 cached_re->ofs + cached_re->len > ofs) {
33 static struct rb_entry *__lookup_rb_tree_slow(struct rb_root *root,
36 struct rb_node *node = root->rb_node;
40 re = rb_entry(node, struct rb_entry, rb_node);
44 else if (ofs >= re->ofs + re->len)
45 node = node->rb_right;
52 struct rb_entry *f2fs_lookup_rb_tree(struct rb_root *root,
53 struct rb_entry *cached_re, unsigned int ofs)
57 re = __lookup_rb_tree_fast(cached_re, ofs);
59 return __lookup_rb_tree_slow(root, ofs);
64 struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
65 struct rb_root *root, struct rb_node **parent,
68 struct rb_node **p = &root->rb_node;
73 re = rb_entry(*parent, struct rb_entry, rb_node);
77 else if (ofs >= re->ofs + re->len)
87 * lookup rb entry in position of @ofs in rb-tree,
88 * if hit, return the entry, otherwise, return NULL
89 * @prev_ex: extent before ofs
90 * @next_ex: extent after ofs
91 * @insert_p: insert point for new extent at ofs
92 * in order to simpfy the insertion after.
93 * tree must stay unchanged between lookup and insertion.
95 struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root *root,
96 struct rb_entry *cached_re,
98 struct rb_entry **prev_entry,
99 struct rb_entry **next_entry,
100 struct rb_node ***insert_p,
101 struct rb_node **insert_parent,
104 struct rb_node **pnode = &root->rb_node;
105 struct rb_node *parent = NULL, *tmp_node;
106 struct rb_entry *re = cached_re;
109 *insert_parent = NULL;
113 if (RB_EMPTY_ROOT(root))
117 if (re->ofs <= ofs && re->ofs + re->len > ofs)
118 goto lookup_neighbors;
123 re = rb_entry(*pnode, struct rb_entry, rb_node);
126 pnode = &(*pnode)->rb_left;
127 else if (ofs >= re->ofs + re->len)
128 pnode = &(*pnode)->rb_right;
130 goto lookup_neighbors;
134 *insert_parent = parent;
136 re = rb_entry(parent, struct rb_entry, rb_node);
138 if (parent && ofs > re->ofs)
139 tmp_node = rb_next(parent);
140 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
143 if (parent && ofs < re->ofs)
144 tmp_node = rb_prev(parent);
145 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
149 if (ofs == re->ofs || force) {
150 /* lookup prev node for merging backward later */
151 tmp_node = rb_prev(&re->rb_node);
152 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
154 if (ofs == re->ofs + re->len - 1 || force) {
155 /* lookup next node for merging frontward later */
156 tmp_node = rb_next(&re->rb_node);
157 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
162 bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
163 struct rb_root *root)
165 #ifdef CONFIG_F2FS_CHECK_FS
166 struct rb_node *cur = rb_first(root), *next;
167 struct rb_entry *cur_re, *next_re;
177 cur_re = rb_entry(cur, struct rb_entry, rb_node);
178 next_re = rb_entry(next, struct rb_entry, rb_node);
180 if (cur_re->ofs + cur_re->len > next_re->ofs) {
181 f2fs_msg(sbi->sb, KERN_INFO, "inconsistent rbtree, "
182 "cur(%u, %u) next(%u, %u)",
183 cur_re->ofs, cur_re->len,
184 next_re->ofs, next_re->len);
194 static struct kmem_cache *extent_tree_slab;
195 static struct kmem_cache *extent_node_slab;
197 static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
198 struct extent_tree *et, struct extent_info *ei,
199 struct rb_node *parent, struct rb_node **p)
201 struct extent_node *en;
203 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
208 INIT_LIST_HEAD(&en->list);
211 rb_link_node(&en->rb_node, parent, p);
212 rb_insert_color(&en->rb_node, &et->root);
213 atomic_inc(&et->node_cnt);
214 atomic_inc(&sbi->total_ext_node);
218 static void __detach_extent_node(struct f2fs_sb_info *sbi,
219 struct extent_tree *et, struct extent_node *en)
221 rb_erase(&en->rb_node, &et->root);
222 atomic_dec(&et->node_cnt);
223 atomic_dec(&sbi->total_ext_node);
225 if (et->cached_en == en)
226 et->cached_en = NULL;
227 kmem_cache_free(extent_node_slab, en);
231 * Flow to release an extent_node:
233 * 2. __detach_extent_node
234 * 3. kmem_cache_free.
236 static void __release_extent_node(struct f2fs_sb_info *sbi,
237 struct extent_tree *et, struct extent_node *en)
239 spin_lock(&sbi->extent_lock);
240 f2fs_bug_on(sbi, list_empty(&en->list));
241 list_del_init(&en->list);
242 spin_unlock(&sbi->extent_lock);
244 __detach_extent_node(sbi, et, en);
247 static struct extent_tree *__grab_extent_tree(struct inode *inode)
249 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
250 struct extent_tree *et;
251 nid_t ino = inode->i_ino;
253 mutex_lock(&sbi->extent_tree_lock);
254 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
256 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
257 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
258 memset(et, 0, sizeof(struct extent_tree));
261 et->cached_en = NULL;
262 rwlock_init(&et->lock);
263 INIT_LIST_HEAD(&et->list);
264 atomic_set(&et->node_cnt, 0);
265 atomic_inc(&sbi->total_ext_tree);
267 atomic_dec(&sbi->total_zombie_tree);
268 list_del_init(&et->list);
270 mutex_unlock(&sbi->extent_tree_lock);
272 /* never died until evict_inode */
273 F2FS_I(inode)->extent_tree = et;
278 static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
279 struct extent_tree *et, struct extent_info *ei)
281 struct rb_node **p = &et->root.rb_node;
282 struct extent_node *en;
284 en = __attach_extent_node(sbi, et, ei, NULL, p);
288 et->largest = en->ei;
293 static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
294 struct extent_tree *et)
296 struct rb_node *node, *next;
297 struct extent_node *en;
298 unsigned int count = atomic_read(&et->node_cnt);
300 node = rb_first(&et->root);
302 next = rb_next(node);
303 en = rb_entry(node, struct extent_node, rb_node);
304 __release_extent_node(sbi, et, en);
308 return count - atomic_read(&et->node_cnt);
311 static void __drop_largest_extent(struct extent_tree *et,
312 pgoff_t fofs, unsigned int len)
314 if (fofs < et->largest.fofs + et->largest.len &&
315 fofs + len > et->largest.fofs) {
317 et->largest_updated = true;
321 /* return true, if inode page is changed */
322 static bool __f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
324 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
325 struct extent_tree *et;
326 struct extent_node *en;
327 struct extent_info ei;
329 if (!f2fs_may_extent_tree(inode)) {
330 /* drop largest extent */
331 if (i_ext && i_ext->len) {
338 et = __grab_extent_tree(inode);
340 if (!i_ext || !i_ext->len)
343 get_extent_info(&ei, i_ext);
345 write_lock(&et->lock);
346 if (atomic_read(&et->node_cnt))
349 en = __init_extent_tree(sbi, et, &ei);
351 spin_lock(&sbi->extent_lock);
352 list_add_tail(&en->list, &sbi->extent_list);
353 spin_unlock(&sbi->extent_lock);
356 write_unlock(&et->lock);
360 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
362 bool ret = __f2fs_init_extent_tree(inode, i_ext);
364 if (!F2FS_I(inode)->extent_tree)
365 set_inode_flag(inode, FI_NO_EXTENT);
370 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
371 struct extent_info *ei)
373 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
374 struct extent_tree *et = F2FS_I(inode)->extent_tree;
375 struct extent_node *en;
378 f2fs_bug_on(sbi, !et);
380 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
382 read_lock(&et->lock);
384 if (et->largest.fofs <= pgofs &&
385 et->largest.fofs + et->largest.len > pgofs) {
388 stat_inc_largest_node_hit(sbi);
392 en = (struct extent_node *)f2fs_lookup_rb_tree(&et->root,
393 (struct rb_entry *)et->cached_en, pgofs);
397 if (en == et->cached_en)
398 stat_inc_cached_node_hit(sbi);
400 stat_inc_rbtree_node_hit(sbi);
403 spin_lock(&sbi->extent_lock);
404 if (!list_empty(&en->list)) {
405 list_move_tail(&en->list, &sbi->extent_list);
408 spin_unlock(&sbi->extent_lock);
411 stat_inc_total_hit(sbi);
412 read_unlock(&et->lock);
414 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
418 static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
419 struct extent_tree *et, struct extent_info *ei,
420 struct extent_node *prev_ex,
421 struct extent_node *next_ex)
423 struct extent_node *en = NULL;
425 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
426 prev_ex->ei.len += ei->len;
431 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
432 next_ex->ei.fofs = ei->fofs;
433 next_ex->ei.blk = ei->blk;
434 next_ex->ei.len += ei->len;
436 __release_extent_node(sbi, et, prev_ex);
444 __try_update_largest_extent(et, en);
446 spin_lock(&sbi->extent_lock);
447 if (!list_empty(&en->list)) {
448 list_move_tail(&en->list, &sbi->extent_list);
451 spin_unlock(&sbi->extent_lock);
455 static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
456 struct extent_tree *et, struct extent_info *ei,
457 struct rb_node **insert_p,
458 struct rb_node *insert_parent)
461 struct rb_node *parent = NULL;
462 struct extent_node *en = NULL;
464 if (insert_p && insert_parent) {
465 parent = insert_parent;
470 p = f2fs_lookup_rb_tree_for_insert(sbi, &et->root, &parent, ei->fofs);
472 en = __attach_extent_node(sbi, et, ei, parent, p);
476 __try_update_largest_extent(et, en);
478 /* update in global extent list */
479 spin_lock(&sbi->extent_lock);
480 list_add_tail(&en->list, &sbi->extent_list);
482 spin_unlock(&sbi->extent_lock);
486 static void f2fs_update_extent_tree_range(struct inode *inode,
487 pgoff_t fofs, block_t blkaddr, unsigned int len)
489 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
490 struct extent_tree *et = F2FS_I(inode)->extent_tree;
491 struct extent_node *en = NULL, *en1 = NULL;
492 struct extent_node *prev_en = NULL, *next_en = NULL;
493 struct extent_info ei, dei, prev;
494 struct rb_node **insert_p = NULL, *insert_parent = NULL;
495 unsigned int end = fofs + len;
496 unsigned int pos = (unsigned int)fofs;
497 bool updated = false;
502 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
504 write_lock(&et->lock);
506 if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
507 write_unlock(&et->lock);
515 * drop largest extent before lookup, in case it's already
516 * been shrunk from extent tree
518 __drop_largest_extent(et, fofs, len);
520 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
521 en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root,
522 (struct rb_entry *)et->cached_en, fofs,
523 (struct rb_entry **)&prev_en,
524 (struct rb_entry **)&next_en,
525 &insert_p, &insert_parent, false);
529 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
530 while (en && en->ei.fofs < end) {
531 unsigned int org_end;
532 int parts = 0; /* # of parts current extent split into */
534 next_en = en1 = NULL;
537 org_end = dei.fofs + dei.len;
538 f2fs_bug_on(sbi, pos >= org_end);
540 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
541 en->ei.len = pos - en->ei.fofs;
546 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
548 set_extent_info(&ei, end,
549 end - dei.fofs + dei.blk,
551 en1 = __insert_extent_tree(sbi, et, &ei,
556 en->ei.blk += end - dei.fofs;
557 en->ei.len -= end - dei.fofs;
564 struct rb_node *node = rb_next(&en->rb_node);
566 next_en = rb_entry_safe(node, struct extent_node,
571 __try_update_largest_extent(et, en);
573 __release_extent_node(sbi, et, en);
576 * if original extent is split into zero or two parts, extent
577 * tree has been altered by deletion or insertion, therefore
578 * invalidate pointers regard to tree.
582 insert_parent = NULL;
587 /* 3. update extent in extent cache */
590 set_extent_info(&ei, fofs, blkaddr, len);
591 if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
592 __insert_extent_tree(sbi, et, &ei,
593 insert_p, insert_parent);
595 /* give up extent_cache, if split and small updates happen */
597 prev.len < F2FS_MIN_EXTENT_LEN &&
598 et->largest.len < F2FS_MIN_EXTENT_LEN) {
600 et->largest_updated = true;
601 set_inode_flag(inode, FI_NO_EXTENT);
605 if (is_inode_flag_set(inode, FI_NO_EXTENT))
606 __free_extent_tree(sbi, et);
608 if (et->largest_updated) {
609 et->largest_updated = false;
613 write_unlock(&et->lock);
616 f2fs_mark_inode_dirty_sync(inode, true);
619 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
621 struct extent_tree *et, *next;
622 struct extent_node *en;
623 unsigned int node_cnt = 0, tree_cnt = 0;
626 if (!test_opt(sbi, EXTENT_CACHE))
629 if (!atomic_read(&sbi->total_zombie_tree))
632 if (!mutex_trylock(&sbi->extent_tree_lock))
635 /* 1. remove unreferenced extent tree */
636 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
637 if (atomic_read(&et->node_cnt)) {
638 write_lock(&et->lock);
639 node_cnt += __free_extent_tree(sbi, et);
640 write_unlock(&et->lock);
642 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
643 list_del_init(&et->list);
644 radix_tree_delete(&sbi->extent_tree_root, et->ino);
645 kmem_cache_free(extent_tree_slab, et);
646 atomic_dec(&sbi->total_ext_tree);
647 atomic_dec(&sbi->total_zombie_tree);
650 if (node_cnt + tree_cnt >= nr_shrink)
654 mutex_unlock(&sbi->extent_tree_lock);
657 /* 2. remove LRU extent entries */
658 if (!mutex_trylock(&sbi->extent_tree_lock))
661 remained = nr_shrink - (node_cnt + tree_cnt);
663 spin_lock(&sbi->extent_lock);
664 for (; remained > 0; remained--) {
665 if (list_empty(&sbi->extent_list))
667 en = list_first_entry(&sbi->extent_list,
668 struct extent_node, list);
670 if (!write_trylock(&et->lock)) {
671 /* refresh this extent node's position in extent list */
672 list_move_tail(&en->list, &sbi->extent_list);
676 list_del_init(&en->list);
677 spin_unlock(&sbi->extent_lock);
679 __detach_extent_node(sbi, et, en);
681 write_unlock(&et->lock);
683 spin_lock(&sbi->extent_lock);
685 spin_unlock(&sbi->extent_lock);
688 mutex_unlock(&sbi->extent_tree_lock);
690 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
692 return node_cnt + tree_cnt;
695 unsigned int f2fs_destroy_extent_node(struct inode *inode)
697 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
698 struct extent_tree *et = F2FS_I(inode)->extent_tree;
699 unsigned int node_cnt = 0;
701 if (!et || !atomic_read(&et->node_cnt))
704 write_lock(&et->lock);
705 node_cnt = __free_extent_tree(sbi, et);
706 write_unlock(&et->lock);
711 void f2fs_drop_extent_tree(struct inode *inode)
713 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
714 struct extent_tree *et = F2FS_I(inode)->extent_tree;
715 bool updated = false;
717 if (!f2fs_may_extent_tree(inode))
720 set_inode_flag(inode, FI_NO_EXTENT);
722 write_lock(&et->lock);
723 __free_extent_tree(sbi, et);
724 if (et->largest.len) {
728 write_unlock(&et->lock);
730 f2fs_mark_inode_dirty_sync(inode, true);
733 void f2fs_destroy_extent_tree(struct inode *inode)
735 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
736 struct extent_tree *et = F2FS_I(inode)->extent_tree;
737 unsigned int node_cnt = 0;
742 if (inode->i_nlink && !is_bad_inode(inode) &&
743 atomic_read(&et->node_cnt)) {
744 mutex_lock(&sbi->extent_tree_lock);
745 list_add_tail(&et->list, &sbi->zombie_list);
746 atomic_inc(&sbi->total_zombie_tree);
747 mutex_unlock(&sbi->extent_tree_lock);
751 /* free all extent info belong to this extent tree */
752 node_cnt = f2fs_destroy_extent_node(inode);
754 /* delete extent tree entry in radix tree */
755 mutex_lock(&sbi->extent_tree_lock);
756 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
757 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
758 kmem_cache_free(extent_tree_slab, et);
759 atomic_dec(&sbi->total_ext_tree);
760 mutex_unlock(&sbi->extent_tree_lock);
762 F2FS_I(inode)->extent_tree = NULL;
764 trace_f2fs_destroy_extent_tree(inode, node_cnt);
767 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
768 struct extent_info *ei)
770 if (!f2fs_may_extent_tree(inode))
773 return f2fs_lookup_extent_tree(inode, pgofs, ei);
776 void f2fs_update_extent_cache(struct dnode_of_data *dn)
781 if (!f2fs_may_extent_tree(dn->inode))
784 if (dn->data_blkaddr == NEW_ADDR)
787 blkaddr = dn->data_blkaddr;
789 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
791 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
794 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
795 pgoff_t fofs, block_t blkaddr, unsigned int len)
798 if (!f2fs_may_extent_tree(dn->inode))
801 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
804 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
806 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
807 mutex_init(&sbi->extent_tree_lock);
808 INIT_LIST_HEAD(&sbi->extent_list);
809 spin_lock_init(&sbi->extent_lock);
810 atomic_set(&sbi->total_ext_tree, 0);
811 INIT_LIST_HEAD(&sbi->zombie_list);
812 atomic_set(&sbi->total_zombie_tree, 0);
813 atomic_set(&sbi->total_ext_node, 0);
816 int __init f2fs_create_extent_cache(void)
818 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
819 sizeof(struct extent_tree));
820 if (!extent_tree_slab)
822 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
823 sizeof(struct extent_node));
824 if (!extent_node_slab) {
825 kmem_cache_destroy(extent_tree_slab);
831 void f2fs_destroy_extent_cache(void)
833 kmem_cache_destroy(extent_node_slab);
834 kmem_cache_destroy(extent_tree_slab);