brcmfmac: fix uninitialized warning in brcmf_usb_probe_phase2()
[linux/fpc-iii.git] / fs / f2fs / extent_cache.c
blob7b32ce979fe1daffde70da73351c92893ec02d3f
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;
175 f2fs_mark_inode_dirty_sync(inode);
179 /* return true, if inode page is changed */
180 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
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;
187 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;
193 return false;
196 et = __grab_extent_tree(inode);
198 if (!i_ext || !i_ext->len)
199 return false;
201 get_extent_info(&ei, i_ext);
203 write_lock(&et->lock);
204 if (atomic_read(&et->node_cnt))
205 goto out;
207 en = __init_extent_tree(sbi, et, &ei);
208 if (en) {
209 spin_lock(&sbi->extent_lock);
210 list_add_tail(&en->list, &sbi->extent_list);
211 spin_unlock(&sbi->extent_lock);
213 out:
214 write_unlock(&et->lock);
215 return false;
218 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
219 struct extent_info *ei)
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;
226 f2fs_bug_on(sbi, !et);
228 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
230 read_lock(&et->lock);
232 if (et->largest.fofs <= pgofs &&
233 et->largest.fofs + et->largest.len > pgofs) {
234 *ei = et->largest;
235 ret = true;
236 stat_inc_largest_node_hit(sbi);
237 goto out;
240 en = __lookup_extent_tree(sbi, et, pgofs);
241 if (en) {
242 *ei = en->ei;
243 spin_lock(&sbi->extent_lock);
244 if (!list_empty(&en->list)) {
245 list_move_tail(&en->list, &sbi->extent_list);
246 et->cached_en = en;
248 spin_unlock(&sbi->extent_lock);
249 ret = true;
251 out:
252 stat_inc_total_hit(sbi);
253 read_unlock(&et->lock);
255 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
256 return ret;
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.
269 static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
270 unsigned int fofs,
271 struct extent_node **prev_ex,
272 struct extent_node **next_ex,
273 struct rb_node ***insert_p,
274 struct rb_node **insert_parent)
276 struct rb_node **pnode = &et->root.rb_node;
277 struct rb_node *parent = NULL, *tmp_node;
278 struct extent_node *en = et->cached_en;
280 *insert_p = NULL;
281 *insert_parent = NULL;
282 *prev_ex = NULL;
283 *next_ex = NULL;
285 if (RB_EMPTY_ROOT(&et->root))
286 return NULL;
288 if (en) {
289 struct extent_info *cei = &en->ei;
291 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
292 goto lookup_neighbors;
295 while (*pnode) {
296 parent = *pnode;
297 en = rb_entry(*pnode, struct extent_node, rb_node);
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
304 goto lookup_neighbors;
307 *insert_p = pnode;
308 *insert_parent = parent;
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);
314 *next_ex = tmp_node ?
315 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
317 tmp_node = parent;
318 if (parent && fofs < en->ei.fofs)
319 tmp_node = rb_prev(parent);
320 *prev_ex = tmp_node ?
321 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
322 return NULL;
324 lookup_neighbors:
325 if (fofs == en->ei.fofs) {
326 /* lookup prev node for merging backward later */
327 tmp_node = rb_prev(&en->rb_node);
328 *prev_ex = tmp_node ?
329 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
331 if (fofs == en->ei.fofs + en->ei.len - 1) {
332 /* lookup next node for merging frontward later */
333 tmp_node = rb_next(&en->rb_node);
334 *next_ex = tmp_node ?
335 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
337 return en;
340 static struct extent_node *__try_merge_extent_node(struct inode *inode,
341 struct extent_tree *et, struct extent_info *ei,
342 struct extent_node *prev_ex,
343 struct extent_node *next_ex)
345 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
346 struct extent_node *en = NULL;
348 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
349 prev_ex->ei.len += ei->len;
350 ei = &prev_ex->ei;
351 en = prev_ex;
354 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
355 next_ex->ei.fofs = ei->fofs;
356 next_ex->ei.blk = ei->blk;
357 next_ex->ei.len += ei->len;
358 if (en)
359 __release_extent_node(sbi, et, prev_ex);
361 en = next_ex;
364 if (!en)
365 return NULL;
367 __try_update_largest_extent(inode, et, en);
369 spin_lock(&sbi->extent_lock);
370 if (!list_empty(&en->list)) {
371 list_move_tail(&en->list, &sbi->extent_list);
372 et->cached_en = en;
374 spin_unlock(&sbi->extent_lock);
375 return en;
378 static struct extent_node *__insert_extent_tree(struct inode *inode,
379 struct extent_tree *et, struct extent_info *ei,
380 struct rb_node **insert_p,
381 struct rb_node *insert_parent)
383 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
384 struct rb_node **p = &et->root.rb_node;
385 struct rb_node *parent = NULL;
386 struct extent_node *en = NULL;
388 if (insert_p && insert_parent) {
389 parent = insert_parent;
390 p = insert_p;
391 goto do_insert;
394 while (*p) {
395 parent = *p;
396 en = rb_entry(parent, struct extent_node, rb_node);
398 if (ei->fofs < en->ei.fofs)
399 p = &(*p)->rb_left;
400 else if (ei->fofs >= en->ei.fofs + en->ei.len)
401 p = &(*p)->rb_right;
402 else
403 f2fs_bug_on(sbi, 1);
405 do_insert:
406 en = __attach_extent_node(sbi, et, ei, parent, p);
407 if (!en)
408 return NULL;
410 __try_update_largest_extent(inode, et, en);
412 /* update in global extent list */
413 spin_lock(&sbi->extent_lock);
414 list_add_tail(&en->list, &sbi->extent_list);
415 et->cached_en = en;
416 spin_unlock(&sbi->extent_lock);
417 return en;
420 static unsigned int f2fs_update_extent_tree_range(struct inode *inode,
421 pgoff_t fofs, block_t blkaddr, unsigned int len)
423 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
424 struct extent_tree *et = F2FS_I(inode)->extent_tree;
425 struct extent_node *en = NULL, *en1 = NULL;
426 struct extent_node *prev_en = NULL, *next_en = NULL;
427 struct extent_info ei, dei, prev;
428 struct rb_node **insert_p = NULL, *insert_parent = NULL;
429 unsigned int end = fofs + len;
430 unsigned int pos = (unsigned int)fofs;
432 if (!et)
433 return false;
435 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
437 write_lock(&et->lock);
439 if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
440 write_unlock(&et->lock);
441 return false;
444 prev = et->largest;
445 dei.len = 0;
448 * drop largest extent before lookup, in case it's already
449 * been shrunk from extent tree
451 __drop_largest_extent(inode, fofs, len);
453 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
454 en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
455 &insert_p, &insert_parent);
456 if (!en)
457 en = next_en;
459 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
460 while (en && en->ei.fofs < end) {
461 unsigned int org_end;
462 int parts = 0; /* # of parts current extent split into */
464 next_en = en1 = NULL;
466 dei = en->ei;
467 org_end = dei.fofs + dei.len;
468 f2fs_bug_on(sbi, pos >= org_end);
470 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
471 en->ei.len = pos - en->ei.fofs;
472 prev_en = en;
473 parts = 1;
476 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
477 if (parts) {
478 set_extent_info(&ei, end,
479 end - dei.fofs + dei.blk,
480 org_end - end);
481 en1 = __insert_extent_tree(inode, et, &ei,
482 NULL, NULL);
483 next_en = en1;
484 } else {
485 en->ei.fofs = end;
486 en->ei.blk += end - dei.fofs;
487 en->ei.len -= end - dei.fofs;
488 next_en = en;
490 parts++;
493 if (!next_en) {
494 struct rb_node *node = rb_next(&en->rb_node);
496 next_en = node ?
497 rb_entry(node, struct extent_node, rb_node)
498 : NULL;
501 if (parts)
502 __try_update_largest_extent(inode, et, en);
503 else
504 __release_extent_node(sbi, et, en);
507 * if original extent is split into zero or two parts, extent
508 * tree has been altered by deletion or insertion, therefore
509 * invalidate pointers regard to tree.
511 if (parts != 1) {
512 insert_p = NULL;
513 insert_parent = NULL;
515 en = next_en;
518 /* 3. update extent in extent cache */
519 if (blkaddr) {
521 set_extent_info(&ei, fofs, blkaddr, len);
522 if (!__try_merge_extent_node(inode, et, &ei, prev_en, next_en))
523 __insert_extent_tree(inode, et, &ei,
524 insert_p, insert_parent);
526 /* give up extent_cache, if split and small updates happen */
527 if (dei.len >= 1 &&
528 prev.len < F2FS_MIN_EXTENT_LEN &&
529 et->largest.len < F2FS_MIN_EXTENT_LEN) {
530 __drop_largest_extent(inode, 0, UINT_MAX);
531 set_inode_flag(inode, FI_NO_EXTENT);
535 if (is_inode_flag_set(inode, FI_NO_EXTENT))
536 __free_extent_tree(sbi, et);
538 write_unlock(&et->lock);
540 return !__is_extent_same(&prev, &et->largest);
543 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
545 struct extent_tree *et, *next;
546 struct extent_node *en;
547 unsigned int node_cnt = 0, tree_cnt = 0;
548 int remained;
550 if (!test_opt(sbi, EXTENT_CACHE))
551 return 0;
553 if (!atomic_read(&sbi->total_zombie_tree))
554 goto free_node;
556 if (!down_write_trylock(&sbi->extent_tree_lock))
557 goto out;
559 /* 1. remove unreferenced extent tree */
560 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
561 if (atomic_read(&et->node_cnt)) {
562 write_lock(&et->lock);
563 node_cnt += __free_extent_tree(sbi, et);
564 write_unlock(&et->lock);
566 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
567 list_del_init(&et->list);
568 radix_tree_delete(&sbi->extent_tree_root, et->ino);
569 kmem_cache_free(extent_tree_slab, et);
570 atomic_dec(&sbi->total_ext_tree);
571 atomic_dec(&sbi->total_zombie_tree);
572 tree_cnt++;
574 if (node_cnt + tree_cnt >= nr_shrink)
575 goto unlock_out;
576 cond_resched();
578 up_write(&sbi->extent_tree_lock);
580 free_node:
581 /* 2. remove LRU extent entries */
582 if (!down_write_trylock(&sbi->extent_tree_lock))
583 goto out;
585 remained = nr_shrink - (node_cnt + tree_cnt);
587 spin_lock(&sbi->extent_lock);
588 for (; remained > 0; remained--) {
589 if (list_empty(&sbi->extent_list))
590 break;
591 en = list_first_entry(&sbi->extent_list,
592 struct extent_node, list);
593 et = en->et;
594 if (!write_trylock(&et->lock)) {
595 /* refresh this extent node's position in extent list */
596 list_move_tail(&en->list, &sbi->extent_list);
597 continue;
600 list_del_init(&en->list);
601 spin_unlock(&sbi->extent_lock);
603 __detach_extent_node(sbi, et, en);
605 write_unlock(&et->lock);
606 node_cnt++;
607 spin_lock(&sbi->extent_lock);
609 spin_unlock(&sbi->extent_lock);
611 unlock_out:
612 up_write(&sbi->extent_tree_lock);
613 out:
614 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
616 return node_cnt + tree_cnt;
619 unsigned int f2fs_destroy_extent_node(struct inode *inode)
621 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
622 struct extent_tree *et = F2FS_I(inode)->extent_tree;
623 unsigned int node_cnt = 0;
625 if (!et || !atomic_read(&et->node_cnt))
626 return 0;
628 write_lock(&et->lock);
629 node_cnt = __free_extent_tree(sbi, et);
630 write_unlock(&et->lock);
632 return node_cnt;
635 void f2fs_drop_extent_tree(struct inode *inode)
637 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
638 struct extent_tree *et = F2FS_I(inode)->extent_tree;
640 set_inode_flag(inode, FI_NO_EXTENT);
642 write_lock(&et->lock);
643 __free_extent_tree(sbi, et);
644 __drop_largest_extent(inode, 0, UINT_MAX);
645 write_unlock(&et->lock);
648 void f2fs_destroy_extent_tree(struct inode *inode)
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;
654 if (!et)
655 return;
657 if (inode->i_nlink && !is_bad_inode(inode) &&
658 atomic_read(&et->node_cnt)) {
659 down_write(&sbi->extent_tree_lock);
660 list_add_tail(&et->list, &sbi->zombie_list);
661 atomic_inc(&sbi->total_zombie_tree);
662 up_write(&sbi->extent_tree_lock);
663 return;
666 /* free all extent info belong to this extent tree */
667 node_cnt = f2fs_destroy_extent_node(inode);
669 /* delete extent tree entry in radix tree */
670 down_write(&sbi->extent_tree_lock);
671 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
672 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
673 kmem_cache_free(extent_tree_slab, et);
674 atomic_dec(&sbi->total_ext_tree);
675 up_write(&sbi->extent_tree_lock);
677 F2FS_I(inode)->extent_tree = NULL;
679 trace_f2fs_destroy_extent_tree(inode, node_cnt);
682 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
683 struct extent_info *ei)
685 if (!f2fs_may_extent_tree(inode))
686 return false;
688 return f2fs_lookup_extent_tree(inode, pgofs, ei);
691 void f2fs_update_extent_cache(struct dnode_of_data *dn)
693 pgoff_t fofs;
694 block_t blkaddr;
696 if (!f2fs_may_extent_tree(dn->inode))
697 return;
699 if (dn->data_blkaddr == NEW_ADDR)
700 blkaddr = NULL_ADDR;
701 else
702 blkaddr = dn->data_blkaddr;
704 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
705 dn->ofs_in_node;
706 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
709 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
710 pgoff_t fofs, block_t blkaddr, unsigned int len)
713 if (!f2fs_may_extent_tree(dn->inode))
714 return;
716 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
719 void init_extent_cache_info(struct f2fs_sb_info *sbi)
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 atomic_set(&sbi->total_ext_tree, 0);
726 INIT_LIST_HEAD(&sbi->zombie_list);
727 atomic_set(&sbi->total_zombie_tree, 0);
728 atomic_set(&sbi->total_ext_node, 0);
731 int __init create_extent_cache(void)
733 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
734 sizeof(struct extent_tree));
735 if (!extent_tree_slab)
736 return -ENOMEM;
737 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
738 sizeof(struct extent_node));
739 if (!extent_node_slab) {
740 kmem_cache_destroy(extent_tree_slab);
741 return -ENOMEM;
743 return 0;
746 void destroy_extent_cache(void)
748 kmem_cache_destroy(extent_node_slab);
749 kmem_cache_destroy(extent_tree_slab);