extent_cache.c (e98bdb3059cbf2b1cd4261e126b08429f64466c3) extent_cache.c (5e8256ac2e378089a1bf3fae87370811b1d31625)
1/*
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 *

--- 63 unchanged lines hidden (view full) ---

72}
73
74static struct extent_tree *__grab_extent_tree(struct inode *inode)
75{
76 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
77 struct extent_tree *et;
78 nid_t ino = inode->i_ino;
79
1/*
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 *

--- 63 unchanged lines hidden (view full) ---

72}
73
74static struct extent_tree *__grab_extent_tree(struct inode *inode)
75{
76 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
77 struct extent_tree *et;
78 nid_t ino = inode->i_ino;
79
80 down_write(&sbi->extent_tree_lock);
80 mutex_lock(&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);
96 }
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);
96 }
97 up_write(&sbi->extent_tree_lock);
97 mutex_unlock(&sbi->extent_tree_lock);
98
99 /* never died until evict_inode */
100 F2FS_I(inode)->extent_tree = et;
101
102 return et;
103}
104
105static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,

--- 200 unchanged lines hidden (view full) ---

306
307 *insert_p = pnode;
308 *insert_parent = parent;
309
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);
98
99 /* never died until evict_inode */
100 F2FS_I(inode)->extent_tree = et;
101
102 return et;
103}
104
105static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,

--- 200 unchanged lines hidden (view full) ---

306
307 *insert_p = pnode;
308 *insert_parent = parent;
309
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;
314 *next_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
316
317 tmp_node = parent;
318 if (parent && fofs < en->ei.fofs)
319 tmp_node = rb_prev(parent);
315
316 tmp_node = parent;
317 if (parent && fofs < en->ei.fofs)
318 tmp_node = rb_prev(parent);
320 *prev_ex = tmp_node ?
321 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
319 *prev_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
322 return NULL;
323
324lookup_neighbors:
325 if (fofs == en->ei.fofs) {
326 /* lookup prev node for merging backward later */
327 tmp_node = rb_prev(&en->rb_node);
320 return NULL;
321
322lookup_neighbors:
323 if (fofs == en->ei.fofs) {
324 /* lookup prev node for merging backward later */
325 tmp_node = rb_prev(&en->rb_node);
328 *prev_ex = tmp_node ?
329 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
326 *prev_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
330 }
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);
327 }
328 if (fofs == en->ei.fofs + en->ei.len - 1) {
329 /* lookup next node for merging frontward later */
330 tmp_node = rb_next(&en->rb_node);
334 *next_ex = tmp_node ?
335 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
331 *next_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
336 }
337 return en;
338}
339
340static 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)
344{
345 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
346 struct extent_node *en = NULL;
347
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;
352 }
353
354 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
332 }
333 return en;
334}
335
336static struct extent_node *__try_merge_extent_node(struct inode *inode,
337 struct extent_tree *et, struct extent_info *ei,
338 struct extent_node *prev_ex,
339 struct extent_node *next_ex)
340{
341 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
342 struct extent_node *en = NULL;
343
344 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
345 prev_ex->ei.len += ei->len;
346 ei = &prev_ex->ei;
347 en = prev_ex;
348 }
349
350 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
355 if (en)
356 __release_extent_node(sbi, et, prev_ex);
357 next_ex->ei.fofs = ei->fofs;
358 next_ex->ei.blk = ei->blk;
359 next_ex->ei.len += ei->len;
351 next_ex->ei.fofs = ei->fofs;
352 next_ex->ei.blk = ei->blk;
353 next_ex->ei.len += ei->len;
354 if (en)
355 __release_extent_node(sbi, et, prev_ex);
356
360 en = next_ex;
361 }
362
363 if (!en)
364 return NULL;
365
366 __try_update_largest_extent(inode, et, en);
367

--- 119 unchanged lines hidden (view full) ---

487 next_en = en;
488 }
489 parts++;
490 }
491
492 if (!next_en) {
493 struct rb_node *node = rb_next(&en->rb_node);
494
357 en = next_ex;
358 }
359
360 if (!en)
361 return NULL;
362
363 __try_update_largest_extent(inode, et, en);
364

--- 119 unchanged lines hidden (view full) ---

484 next_en = en;
485 }
486 parts++;
487 }
488
489 if (!next_en) {
490 struct rb_node *node = rb_next(&en->rb_node);
491
495 next_en = node ?
496 rb_entry(node, struct extent_node, rb_node)
497 : NULL;
492 next_en = rb_entry_safe(node, struct extent_node,
493 rb_node);
498 }
499
500 if (parts)
501 __try_update_largest_extent(inode, et, en);
502 else
503 __release_extent_node(sbi, et, en);
504
505 /*

--- 41 unchanged lines hidden (view full) ---

547 int remained;
548
549 if (!test_opt(sbi, EXTENT_CACHE))
550 return 0;
551
552 if (!atomic_read(&sbi->total_zombie_tree))
553 goto free_node;
554
494 }
495
496 if (parts)
497 __try_update_largest_extent(inode, et, en);
498 else
499 __release_extent_node(sbi, et, en);
500
501 /*

--- 41 unchanged lines hidden (view full) ---

543 int remained;
544
545 if (!test_opt(sbi, EXTENT_CACHE))
546 return 0;
547
548 if (!atomic_read(&sbi->total_zombie_tree))
549 goto free_node;
550
555 if (!down_write_trylock(&sbi->extent_tree_lock))
551 if (!mutex_trylock(&sbi->extent_tree_lock))
556 goto out;
557
558 /* 1. remove unreferenced extent tree */
559 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
560 if (atomic_read(&et->node_cnt)) {
561 write_lock(&et->lock);
562 node_cnt += __free_extent_tree(sbi, et);
563 write_unlock(&et->lock);

--- 5 unchanged lines hidden (view full) ---

569 atomic_dec(&sbi->total_ext_tree);
570 atomic_dec(&sbi->total_zombie_tree);
571 tree_cnt++;
572
573 if (node_cnt + tree_cnt >= nr_shrink)
574 goto unlock_out;
575 cond_resched();
576 }
552 goto out;
553
554 /* 1. remove unreferenced extent tree */
555 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
556 if (atomic_read(&et->node_cnt)) {
557 write_lock(&et->lock);
558 node_cnt += __free_extent_tree(sbi, et);
559 write_unlock(&et->lock);

--- 5 unchanged lines hidden (view full) ---

565 atomic_dec(&sbi->total_ext_tree);
566 atomic_dec(&sbi->total_zombie_tree);
567 tree_cnt++;
568
569 if (node_cnt + tree_cnt >= nr_shrink)
570 goto unlock_out;
571 cond_resched();
572 }
577 up_write(&sbi->extent_tree_lock);
573 mutex_unlock(&sbi->extent_tree_lock);
578
579free_node:
580 /* 2. remove LRU extent entries */
574
575free_node:
576 /* 2. remove LRU extent entries */
581 if (!down_write_trylock(&sbi->extent_tree_lock))
577 if (!mutex_trylock(&sbi->extent_tree_lock))
582 goto out;
583
584 remained = nr_shrink - (node_cnt + tree_cnt);
585
586 spin_lock(&sbi->extent_lock);
587 for (; remained > 0; remained--) {
588 if (list_empty(&sbi->extent_list))
589 break;

--- 13 unchanged lines hidden (view full) ---

603
604 write_unlock(&et->lock);
605 node_cnt++;
606 spin_lock(&sbi->extent_lock);
607 }
608 spin_unlock(&sbi->extent_lock);
609
610unlock_out:
578 goto out;
579
580 remained = nr_shrink - (node_cnt + tree_cnt);
581
582 spin_lock(&sbi->extent_lock);
583 for (; remained > 0; remained--) {
584 if (list_empty(&sbi->extent_list))
585 break;

--- 13 unchanged lines hidden (view full) ---

599
600 write_unlock(&et->lock);
601 node_cnt++;
602 spin_lock(&sbi->extent_lock);
603 }
604 spin_unlock(&sbi->extent_lock);
605
606unlock_out:
611 up_write(&sbi->extent_tree_lock);
607 mutex_unlock(&sbi->extent_tree_lock);
612out:
613 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
614
615 return node_cnt + tree_cnt;
616}
617
618unsigned int f2fs_destroy_extent_node(struct inode *inode)
619{

--- 30 unchanged lines hidden (view full) ---

650 struct extent_tree *et = F2FS_I(inode)->extent_tree;
651 unsigned int node_cnt = 0;
652
653 if (!et)
654 return;
655
656 if (inode->i_nlink && !is_bad_inode(inode) &&
657 atomic_read(&et->node_cnt)) {
608out:
609 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
610
611 return node_cnt + tree_cnt;
612}
613
614unsigned int f2fs_destroy_extent_node(struct inode *inode)
615{

--- 30 unchanged lines hidden (view full) ---

646 struct extent_tree *et = F2FS_I(inode)->extent_tree;
647 unsigned int node_cnt = 0;
648
649 if (!et)
650 return;
651
652 if (inode->i_nlink && !is_bad_inode(inode) &&
653 atomic_read(&et->node_cnt)) {
658 down_write(&sbi->extent_tree_lock);
654 mutex_lock(&sbi->extent_tree_lock);
659 list_add_tail(&et->list, &sbi->zombie_list);
660 atomic_inc(&sbi->total_zombie_tree);
655 list_add_tail(&et->list, &sbi->zombie_list);
656 atomic_inc(&sbi->total_zombie_tree);
661 up_write(&sbi->extent_tree_lock);
657 mutex_unlock(&sbi->extent_tree_lock);
662 return;
663 }
664
665 /* free all extent info belong to this extent tree */
666 node_cnt = f2fs_destroy_extent_node(inode);
667
668 /* delete extent tree entry in radix tree */
658 return;
659 }
660
661 /* free all extent info belong to this extent tree */
662 node_cnt = f2fs_destroy_extent_node(inode);
663
664 /* delete extent tree entry in radix tree */
669 down_write(&sbi->extent_tree_lock);
665 mutex_lock(&sbi->extent_tree_lock);
670 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
671 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
672 kmem_cache_free(extent_tree_slab, et);
673 atomic_dec(&sbi->total_ext_tree);
666 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
667 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
668 kmem_cache_free(extent_tree_slab, et);
669 atomic_dec(&sbi->total_ext_tree);
674 up_write(&sbi->extent_tree_lock);
670 mutex_unlock(&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)

--- 30 unchanged lines hidden (view full) ---

713 return;
714
715 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
716}
717
718void init_extent_cache_info(struct f2fs_sb_info *sbi)
719{
720 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
671
672 F2FS_I(inode)->extent_tree = NULL;
673
674 trace_f2fs_destroy_extent_tree(inode, node_cnt);
675}
676
677bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
678 struct extent_info *ei)

--- 30 unchanged lines hidden (view full) ---

709 return;
710
711 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
712}
713
714void init_extent_cache_info(struct f2fs_sb_info *sbi)
715{
716 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
721 init_rwsem(&sbi->extent_tree_lock);
717 mutex_init(&sbi->extent_tree_lock);
722 INIT_LIST_HEAD(&sbi->extent_list);
723 spin_lock_init(&sbi->extent_lock);
724 atomic_set(&sbi->total_ext_tree, 0);
725 INIT_LIST_HEAD(&sbi->zombie_list);
726 atomic_set(&sbi->total_zombie_tree, 0);
727 atomic_set(&sbi->total_ext_node, 0);
728}
729

--- 20 unchanged lines hidden ---
718 INIT_LIST_HEAD(&sbi->extent_list);
719 spin_lock_init(&sbi->extent_lock);
720 atomic_set(&sbi->total_ext_tree, 0);
721 INIT_LIST_HEAD(&sbi->zombie_list);
722 atomic_set(&sbi->total_zombie_tree, 0);
723 atomic_set(&sbi->total_ext_node, 0);
724}
725

--- 20 unchanged lines hidden ---