1 // SPDX-License-Identifier: GPL-2.0
3 * fs/ext4/extents_status.c
5 * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
7 * Allison Henderson <achender@linux.vnet.ibm.com>
8 * Hugh Dickins <hughd@google.com>
9 * Zheng Liu <wenqing.lz@taobao.com>
11 * Ext4 extents status tree core functions.
13 #include <linux/list_sort.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
18 #include <trace/events/ext4.h>
21 * According to previous discussion in Ext4 Developer Workshop, we
22 * will introduce a new structure called io tree to track all extent
23 * status in order to solve some problems that we have met
24 * (e.g. Reservation space warning), and provide extent-level locking.
25 * Delay extent tree is the first step to achieve this goal. It is
26 * original built by Yongqiang Yang. At that time it is called delay
27 * extent tree, whose goal is only track delayed extents in memory to
28 * simplify the implementation of fiemap and bigalloc, and introduce
29 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
30 * delay extent tree at the first commit. But for better understand
31 * what it does, it has been rename to extent status tree.
34 * Currently the first step has been done. All delayed extents are
35 * tracked in the tree. It maintains the delayed extent when a delayed
36 * allocation is issued, and the delayed extent is written out or
37 * invalidated. Therefore the implementation of fiemap and bigalloc
38 * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
40 * The following comment describes the implemenmtation of extent
41 * status tree and future works.
44 * In this step all extent status are tracked by extent status tree.
45 * Thus, we can first try to lookup a block mapping in this tree before
46 * finding it in extent tree. Hence, single extent cache can be removed
47 * because extent status tree can do a better job. Extents in status
48 * tree are loaded on-demand. Therefore, the extent status tree may not
49 * contain all of the extents in a file. Meanwhile we define a shrinker
50 * to reclaim memory from extent status tree because fragmented extent
51 * tree will make status tree cost too much memory. written/unwritten/-
52 * hole extents in the tree will be reclaimed by this shrinker when we
53 * are under high memory pressure. Delayed extents will not be
54 * reclimed because fiemap, bigalloc, and seek_data/hole need it.
58 * Extent status tree implementation for ext4.
61 * ==========================================================================
62 * Extent status tree tracks all extent status.
64 * 1. Why we need to implement extent status tree?
66 * Without extent status tree, ext4 identifies a delayed extent by looking
67 * up page cache, this has several deficiencies - complicated, buggy,
68 * and inefficient code.
70 * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
71 * block or a range of blocks are belonged to a delayed extent.
73 * Let us have a look at how they do without extent status tree.
75 * FIEMAP looks up page cache to identify delayed allocations from holes.
78 * SEEK_HOLE/DATA has the same problem as FIEMAP.
81 * bigalloc looks up page cache to figure out if a block is
82 * already under delayed allocation or not to determine whether
83 * quota reserving is needed for the cluster.
86 * Writeout looks up whole page cache to see if a buffer is
87 * mapped, If there are not very many delayed buffers, then it is
90 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
91 * bigalloc and writeout can figure out if a block or a range of
92 * blocks is under delayed allocation(belonged to a delayed extent) or
93 * not by searching the extent tree.
96 * ==========================================================================
97 * 2. Ext4 extent status tree impelmentation
100 * A extent is a range of blocks which are contiguous logically and
101 * physically. Unlike extent in extent tree, this extent in ext4 is
102 * a in-memory struct, there is no corresponding on-disk data. There
103 * is no limit on length of extent, so an extent can contain as many
104 * blocks as they are contiguous logically and physically.
106 * -- extent status tree
107 * Every inode has an extent status tree and all allocation blocks
108 * are added to the tree with different status. The extent in the
109 * tree are ordered by logical block no.
111 * -- operations on a extent status tree
112 * There are three important operations on a delayed extent tree: find
113 * next extent, adding a extent(a range of blocks) and removing a extent.
115 * -- race on a extent status tree
116 * Extent status tree is protected by inode->i_es_lock.
118 * -- memory consumption
119 * Fragmented extent tree will make extent status tree cost too much
120 * memory. Hence, we will reclaim written/unwritten/hole extents from
121 * the tree under a heavy memory pressure.
124 * ==========================================================================
125 * 3. Performance analysis
128 * 1. There is a cache extent for write access, so if writes are
129 * not very random, adding space operaions are in O(1) time.
132 * 2. Code is much simpler, more readable, more maintainable and
136 * ==========================================================================
139 * -- Refactor delayed space reservation
141 * -- Extent-level locking
144 static struct kmem_cache *ext4_es_cachep;
146 static int __es_insert_extent(struct inode *inode, struct extent_status *newes);
147 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
149 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
150 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
151 struct ext4_inode_info *locked_ei);
153 int __init ext4_init_es(void)
155 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
156 sizeof(struct extent_status),
157 0, (SLAB_RECLAIM_ACCOUNT), NULL);
158 if (ext4_es_cachep == NULL)
163 void ext4_exit_es(void)
165 kmem_cache_destroy(ext4_es_cachep);
168 void ext4_es_init_tree(struct ext4_es_tree *tree)
170 tree->root = RB_ROOT;
171 tree->cache_es = NULL;
175 static void ext4_es_print_tree(struct inode *inode)
177 struct ext4_es_tree *tree;
178 struct rb_node *node;
180 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
181 tree = &EXT4_I(inode)->i_es_tree;
182 node = rb_first(&tree->root);
184 struct extent_status *es;
185 es = rb_entry(node, struct extent_status, rb_node);
186 printk(KERN_DEBUG " [%u/%u) %llu %x",
187 es->es_lblk, es->es_len,
188 ext4_es_pblock(es), ext4_es_status(es));
189 node = rb_next(node);
191 printk(KERN_DEBUG "\n");
194 #define ext4_es_print_tree(inode)
197 static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
199 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
200 return es->es_lblk + es->es_len - 1;
204 * search through the tree for an delayed extent with a given offset. If
205 * it can't be found, try to find next extent.
207 static struct extent_status *__es_tree_search(struct rb_root *root,
210 struct rb_node *node = root->rb_node;
211 struct extent_status *es = NULL;
214 es = rb_entry(node, struct extent_status, rb_node);
215 if (lblk < es->es_lblk)
216 node = node->rb_left;
217 else if (lblk > ext4_es_end(es))
218 node = node->rb_right;
223 if (es && lblk < es->es_lblk)
226 if (es && lblk > ext4_es_end(es)) {
227 node = rb_next(&es->rb_node);
228 return node ? rb_entry(node, struct extent_status, rb_node) :
236 * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
237 * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
239 * @inode: the inode which owns delayed extents
240 * @lblk: the offset where we start to search
241 * @end: the offset where we stop to search
242 * @es: delayed extent that we found
244 void ext4_es_find_delayed_extent_range(struct inode *inode,
245 ext4_lblk_t lblk, ext4_lblk_t end,
246 struct extent_status *es)
248 struct ext4_es_tree *tree = NULL;
249 struct extent_status *es1 = NULL;
250 struct rb_node *node;
254 trace_ext4_es_find_delayed_extent_range_enter(inode, lblk);
256 read_lock(&EXT4_I(inode)->i_es_lock);
257 tree = &EXT4_I(inode)->i_es_tree;
259 /* find extent in cache firstly */
260 es->es_lblk = es->es_len = es->es_pblk = 0;
261 if (tree->cache_es) {
262 es1 = tree->cache_es;
263 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
264 es_debug("%u cached by [%u/%u) %llu %x\n",
265 lblk, es1->es_lblk, es1->es_len,
266 ext4_es_pblock(es1), ext4_es_status(es1));
271 es1 = __es_tree_search(&tree->root, lblk);
274 if (es1 && !ext4_es_is_delayed(es1)) {
275 while ((node = rb_next(&es1->rb_node)) != NULL) {
276 es1 = rb_entry(node, struct extent_status, rb_node);
277 if (es1->es_lblk > end) {
281 if (ext4_es_is_delayed(es1))
286 if (es1 && ext4_es_is_delayed(es1)) {
287 tree->cache_es = es1;
288 es->es_lblk = es1->es_lblk;
289 es->es_len = es1->es_len;
290 es->es_pblk = es1->es_pblk;
293 read_unlock(&EXT4_I(inode)->i_es_lock);
295 trace_ext4_es_find_delayed_extent_range_exit(inode, es);
298 static void ext4_es_list_add(struct inode *inode)
300 struct ext4_inode_info *ei = EXT4_I(inode);
301 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
303 if (!list_empty(&ei->i_es_list))
306 spin_lock(&sbi->s_es_lock);
307 if (list_empty(&ei->i_es_list)) {
308 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
309 sbi->s_es_nr_inode++;
311 spin_unlock(&sbi->s_es_lock);
314 static void ext4_es_list_del(struct inode *inode)
316 struct ext4_inode_info *ei = EXT4_I(inode);
317 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
319 spin_lock(&sbi->s_es_lock);
320 if (!list_empty(&ei->i_es_list)) {
321 list_del_init(&ei->i_es_list);
322 sbi->s_es_nr_inode--;
323 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
325 spin_unlock(&sbi->s_es_lock);
328 static struct extent_status *
329 ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
332 struct extent_status *es;
333 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
341 * We don't count delayed extent because we never try to reclaim them
343 if (!ext4_es_is_delayed(es)) {
344 if (!EXT4_I(inode)->i_es_shk_nr++)
345 ext4_es_list_add(inode);
346 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
347 s_es_stats.es_stats_shk_cnt);
350 EXT4_I(inode)->i_es_all_nr++;
351 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
356 static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
358 EXT4_I(inode)->i_es_all_nr--;
359 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
361 /* Decrease the shrink counter when this es is not delayed */
362 if (!ext4_es_is_delayed(es)) {
363 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
364 if (!--EXT4_I(inode)->i_es_shk_nr)
365 ext4_es_list_del(inode);
366 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
367 s_es_stats.es_stats_shk_cnt);
370 kmem_cache_free(ext4_es_cachep, es);
374 * Check whether or not two extents can be merged
376 * - logical block number is contiguous
377 * - physical block number is contiguous
380 static int ext4_es_can_be_merged(struct extent_status *es1,
381 struct extent_status *es2)
383 if (ext4_es_type(es1) != ext4_es_type(es2))
386 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
387 pr_warn("ES assertion failed when merging extents. "
388 "The sum of lengths of es1 (%d) and es2 (%d) "
389 "is bigger than allowed file size (%d)\n",
390 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
395 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
398 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
399 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
402 if (ext4_es_is_hole(es1))
405 /* we need to check delayed extent is without unwritten status */
406 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
412 static struct extent_status *
413 ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
415 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
416 struct extent_status *es1;
417 struct rb_node *node;
419 node = rb_prev(&es->rb_node);
423 es1 = rb_entry(node, struct extent_status, rb_node);
424 if (ext4_es_can_be_merged(es1, es)) {
425 es1->es_len += es->es_len;
426 if (ext4_es_is_referenced(es))
427 ext4_es_set_referenced(es1);
428 rb_erase(&es->rb_node, &tree->root);
429 ext4_es_free_extent(inode, es);
436 static struct extent_status *
437 ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
439 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
440 struct extent_status *es1;
441 struct rb_node *node;
443 node = rb_next(&es->rb_node);
447 es1 = rb_entry(node, struct extent_status, rb_node);
448 if (ext4_es_can_be_merged(es, es1)) {
449 es->es_len += es1->es_len;
450 if (ext4_es_is_referenced(es1))
451 ext4_es_set_referenced(es);
452 rb_erase(node, &tree->root);
453 ext4_es_free_extent(inode, es1);
459 #ifdef ES_AGGRESSIVE_TEST
460 #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
462 static void ext4_es_insert_extent_ext_check(struct inode *inode,
463 struct extent_status *es)
465 struct ext4_ext_path *path = NULL;
466 struct ext4_extent *ex;
467 ext4_lblk_t ee_block;
468 ext4_fsblk_t ee_start;
469 unsigned short ee_len;
470 int depth, ee_status, es_status;
472 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
476 depth = ext_depth(inode);
477 ex = path[depth].p_ext;
481 ee_block = le32_to_cpu(ex->ee_block);
482 ee_start = ext4_ext_pblock(ex);
483 ee_len = ext4_ext_get_actual_len(ex);
485 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
486 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
489 * Make sure ex and es are not overlap when we try to insert
490 * a delayed/hole extent.
492 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
493 if (in_range(es->es_lblk, ee_block, ee_len)) {
494 pr_warn("ES insert assertion failed for "
495 "inode: %lu we can find an extent "
496 "at block [%d/%d/%llu/%c], but we "
497 "want to add a delayed/hole extent "
499 inode->i_ino, ee_block, ee_len,
500 ee_start, ee_status ? 'u' : 'w',
501 es->es_lblk, es->es_len,
502 ext4_es_pblock(es), ext4_es_status(es));
508 * We don't check ee_block == es->es_lblk, etc. because es
509 * might be a part of whole extent, vice versa.
511 if (es->es_lblk < ee_block ||
512 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
513 pr_warn("ES insert assertion failed for inode: %lu "
514 "ex_status [%d/%d/%llu/%c] != "
515 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
516 ee_block, ee_len, ee_start,
517 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
518 ext4_es_pblock(es), es_status ? 'u' : 'w');
522 if (ee_status ^ es_status) {
523 pr_warn("ES insert assertion failed for inode: %lu "
524 "ex_status [%d/%d/%llu/%c] != "
525 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
526 ee_block, ee_len, ee_start,
527 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
528 ext4_es_pblock(es), es_status ? 'u' : 'w');
532 * We can't find an extent on disk. So we need to make sure
533 * that we don't want to add an written/unwritten extent.
535 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
536 pr_warn("ES insert assertion failed for inode: %lu "
537 "can't find an extent at block %d but we want "
538 "to add a written/unwritten extent "
539 "[%d/%d/%llu/%x]\n", inode->i_ino,
540 es->es_lblk, es->es_lblk, es->es_len,
541 ext4_es_pblock(es), ext4_es_status(es));
545 ext4_ext_drop_refs(path);
549 static void ext4_es_insert_extent_ind_check(struct inode *inode,
550 struct extent_status *es)
552 struct ext4_map_blocks map;
556 * Here we call ext4_ind_map_blocks to lookup a block mapping because
557 * 'Indirect' structure is defined in indirect.c. So we couldn't
558 * access direct/indirect tree from outside. It is too dirty to define
559 * this function in indirect.c file.
562 map.m_lblk = es->es_lblk;
563 map.m_len = es->es_len;
565 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
567 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
569 * We want to add a delayed/hole extent but this
570 * block has been allocated.
572 pr_warn("ES insert assertion failed for inode: %lu "
573 "We can find blocks but we want to add a "
574 "delayed/hole extent [%d/%d/%llu/%x]\n",
575 inode->i_ino, es->es_lblk, es->es_len,
576 ext4_es_pblock(es), ext4_es_status(es));
578 } else if (ext4_es_is_written(es)) {
579 if (retval != es->es_len) {
580 pr_warn("ES insert assertion failed for "
581 "inode: %lu retval %d != es_len %d\n",
582 inode->i_ino, retval, es->es_len);
585 if (map.m_pblk != ext4_es_pblock(es)) {
586 pr_warn("ES insert assertion failed for "
587 "inode: %lu m_pblk %llu != "
589 inode->i_ino, map.m_pblk,
595 * We don't need to check unwritten extent because
596 * indirect-based file doesn't have it.
600 } else if (retval == 0) {
601 if (ext4_es_is_written(es)) {
602 pr_warn("ES insert assertion failed for inode: %lu "
603 "We can't find the block but we want to add "
604 "a written extent [%d/%d/%llu/%x]\n",
605 inode->i_ino, es->es_lblk, es->es_len,
606 ext4_es_pblock(es), ext4_es_status(es));
612 static inline void ext4_es_insert_extent_check(struct inode *inode,
613 struct extent_status *es)
616 * We don't need to worry about the race condition because
617 * caller takes i_data_sem locking.
619 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
620 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
621 ext4_es_insert_extent_ext_check(inode, es);
623 ext4_es_insert_extent_ind_check(inode, es);
626 static inline void ext4_es_insert_extent_check(struct inode *inode,
627 struct extent_status *es)
632 static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
634 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
635 struct rb_node **p = &tree->root.rb_node;
636 struct rb_node *parent = NULL;
637 struct extent_status *es;
641 es = rb_entry(parent, struct extent_status, rb_node);
643 if (newes->es_lblk < es->es_lblk) {
644 if (ext4_es_can_be_merged(newes, es)) {
646 * Here we can modify es_lblk directly
647 * because it isn't overlapped.
649 es->es_lblk = newes->es_lblk;
650 es->es_len += newes->es_len;
651 if (ext4_es_is_written(es) ||
652 ext4_es_is_unwritten(es))
653 ext4_es_store_pblock(es,
655 es = ext4_es_try_to_merge_left(inode, es);
659 } else if (newes->es_lblk > ext4_es_end(es)) {
660 if (ext4_es_can_be_merged(es, newes)) {
661 es->es_len += newes->es_len;
662 es = ext4_es_try_to_merge_right(inode, es);
672 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
676 rb_link_node(&es->rb_node, parent, p);
677 rb_insert_color(&es->rb_node, &tree->root);
685 * ext4_es_insert_extent() adds information to an inode's extent
688 * Return 0 on success, error code on failure.
690 int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
691 ext4_lblk_t len, ext4_fsblk_t pblk,
694 struct extent_status newes;
695 ext4_lblk_t end = lblk + len - 1;
698 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
699 lblk, len, pblk, status, inode->i_ino);
706 if ((status & EXTENT_STATUS_DELAYED) &&
707 (status & EXTENT_STATUS_WRITTEN)) {
708 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
709 " delayed and written which can potentially "
710 " cause data loss.", lblk, len);
714 newes.es_lblk = lblk;
716 ext4_es_store_pblock_status(&newes, pblk, status);
717 trace_ext4_es_insert_extent(inode, &newes);
719 ext4_es_insert_extent_check(inode, &newes);
721 write_lock(&EXT4_I(inode)->i_es_lock);
722 err = __es_remove_extent(inode, lblk, end);
726 err = __es_insert_extent(inode, &newes);
727 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
730 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
734 write_unlock(&EXT4_I(inode)->i_es_lock);
736 ext4_es_print_tree(inode);
742 * ext4_es_cache_extent() inserts information into the extent status
743 * tree if and only if there isn't information about the range in
746 void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
747 ext4_lblk_t len, ext4_fsblk_t pblk,
750 struct extent_status *es;
751 struct extent_status newes;
752 ext4_lblk_t end = lblk + len - 1;
754 newes.es_lblk = lblk;
756 ext4_es_store_pblock_status(&newes, pblk, status);
757 trace_ext4_es_cache_extent(inode, &newes);
764 write_lock(&EXT4_I(inode)->i_es_lock);
766 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
767 if (!es || es->es_lblk > end)
768 __es_insert_extent(inode, &newes);
769 write_unlock(&EXT4_I(inode)->i_es_lock);
773 * ext4_es_lookup_extent() looks up an extent in extent status tree.
775 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
777 * Return: 1 on found, 0 on not
779 int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
780 struct extent_status *es)
782 struct ext4_es_tree *tree;
783 struct ext4_es_stats *stats;
784 struct extent_status *es1 = NULL;
785 struct rb_node *node;
788 trace_ext4_es_lookup_extent_enter(inode, lblk);
789 es_debug("lookup extent in block %u\n", lblk);
791 tree = &EXT4_I(inode)->i_es_tree;
792 read_lock(&EXT4_I(inode)->i_es_lock);
794 /* find extent in cache firstly */
795 es->es_lblk = es->es_len = es->es_pblk = 0;
796 if (tree->cache_es) {
797 es1 = tree->cache_es;
798 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
799 es_debug("%u cached by [%u/%u)\n",
800 lblk, es1->es_lblk, es1->es_len);
806 node = tree->root.rb_node;
808 es1 = rb_entry(node, struct extent_status, rb_node);
809 if (lblk < es1->es_lblk)
810 node = node->rb_left;
811 else if (lblk > ext4_es_end(es1))
812 node = node->rb_right;
820 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
823 es->es_lblk = es1->es_lblk;
824 es->es_len = es1->es_len;
825 es->es_pblk = es1->es_pblk;
826 if (!ext4_es_is_referenced(es1))
827 ext4_es_set_referenced(es1);
828 stats->es_stats_cache_hits++;
830 stats->es_stats_cache_misses++;
833 read_unlock(&EXT4_I(inode)->i_es_lock);
835 trace_ext4_es_lookup_extent_exit(inode, es, found);
839 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
842 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
843 struct rb_node *node;
844 struct extent_status *es;
845 struct extent_status orig_es;
846 ext4_lblk_t len1, len2;
852 es = __es_tree_search(&tree->root, lblk);
855 if (es->es_lblk > end)
858 /* Simply invalidate cache_es. */
859 tree->cache_es = NULL;
861 orig_es.es_lblk = es->es_lblk;
862 orig_es.es_len = es->es_len;
863 orig_es.es_pblk = es->es_pblk;
865 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
866 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
871 struct extent_status newes;
873 newes.es_lblk = end + 1;
875 block = 0x7FDEADBEEFULL;
876 if (ext4_es_is_written(&orig_es) ||
877 ext4_es_is_unwritten(&orig_es))
878 block = ext4_es_pblock(&orig_es) +
879 orig_es.es_len - len2;
880 ext4_es_store_pblock_status(&newes, block,
881 ext4_es_status(&orig_es));
882 err = __es_insert_extent(inode, &newes);
884 es->es_lblk = orig_es.es_lblk;
885 es->es_len = orig_es.es_len;
886 if ((err == -ENOMEM) &&
887 __es_shrink(EXT4_SB(inode->i_sb),
893 es->es_lblk = end + 1;
895 if (ext4_es_is_written(es) ||
896 ext4_es_is_unwritten(es)) {
897 block = orig_es.es_pblk + orig_es.es_len - len2;
898 ext4_es_store_pblock(es, block);
905 node = rb_next(&es->rb_node);
907 es = rb_entry(node, struct extent_status, rb_node);
912 while (es && ext4_es_end(es) <= end) {
913 node = rb_next(&es->rb_node);
914 rb_erase(&es->rb_node, &tree->root);
915 ext4_es_free_extent(inode, es);
920 es = rb_entry(node, struct extent_status, rb_node);
923 if (es && es->es_lblk < end + 1) {
924 ext4_lblk_t orig_len = es->es_len;
926 len1 = ext4_es_end(es) - end;
927 es->es_lblk = end + 1;
929 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
930 block = es->es_pblk + orig_len - len1;
931 ext4_es_store_pblock(es, block);
940 * ext4_es_remove_extent() removes a space from a extent status tree.
942 * Return 0 on success, error code on failure.
944 int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
950 trace_ext4_es_remove_extent(inode, lblk, len);
951 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
952 lblk, len, inode->i_ino);
957 end = lblk + len - 1;
961 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
962 * so that we are sure __es_shrink() is done with the inode before it
965 write_lock(&EXT4_I(inode)->i_es_lock);
966 err = __es_remove_extent(inode, lblk, end);
967 write_unlock(&EXT4_I(inode)->i_es_lock);
968 ext4_es_print_tree(inode);
972 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
973 struct ext4_inode_info *locked_ei)
975 struct ext4_inode_info *ei;
976 struct ext4_es_stats *es_stats;
981 int retried = 0, nr_skipped = 0;
983 es_stats = &sbi->s_es_stats;
984 start_time = ktime_get();
987 spin_lock(&sbi->s_es_lock);
988 nr_to_walk = sbi->s_es_nr_inode;
989 while (nr_to_walk-- > 0) {
990 if (list_empty(&sbi->s_es_list)) {
991 spin_unlock(&sbi->s_es_lock);
994 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
996 /* Move the inode to the tail */
997 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
1000 * Normally we try hard to avoid shrinking precached inodes,
1001 * but we will as a last resort.
1003 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1004 EXT4_STATE_EXT_PRECACHED)) {
1009 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1014 * Now we hold i_es_lock which protects us from inode reclaim
1015 * freeing inode under us
1017 spin_unlock(&sbi->s_es_lock);
1019 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
1020 write_unlock(&ei->i_es_lock);
1022 if (nr_to_scan <= 0)
1024 spin_lock(&sbi->s_es_lock);
1026 spin_unlock(&sbi->s_es_lock);
1029 * If we skipped any inodes, and we weren't able to make any
1030 * forward progress, try again to scan precached inodes.
1032 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1037 if (locked_ei && nr_shrunk == 0)
1038 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
1041 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1042 if (likely(es_stats->es_stats_scan_time))
1043 es_stats->es_stats_scan_time = (scan_time +
1044 es_stats->es_stats_scan_time*3) / 4;
1046 es_stats->es_stats_scan_time = scan_time;
1047 if (scan_time > es_stats->es_stats_max_scan_time)
1048 es_stats->es_stats_max_scan_time = scan_time;
1049 if (likely(es_stats->es_stats_shrunk))
1050 es_stats->es_stats_shrunk = (nr_shrunk +
1051 es_stats->es_stats_shrunk*3) / 4;
1053 es_stats->es_stats_shrunk = nr_shrunk;
1055 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
1056 nr_skipped, retried);
1060 static unsigned long ext4_es_count(struct shrinker *shrink,
1061 struct shrink_control *sc)
1064 struct ext4_sb_info *sbi;
1066 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
1067 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1068 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
1072 static unsigned long ext4_es_scan(struct shrinker *shrink,
1073 struct shrink_control *sc)
1075 struct ext4_sb_info *sbi = container_of(shrink,
1076 struct ext4_sb_info, s_es_shrinker);
1077 int nr_to_scan = sc->nr_to_scan;
1080 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1081 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
1083 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
1085 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1086 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
1090 int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
1092 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
1093 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1094 struct ext4_inode_info *ei, *max = NULL;
1095 unsigned int inode_cnt = 0;
1097 if (v != SEQ_START_TOKEN)
1100 /* here we just find an inode that has the max nr. of objects */
1101 spin_lock(&sbi->s_es_lock);
1102 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
1104 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1109 spin_unlock(&sbi->s_es_lock);
1111 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1112 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
1113 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
1114 seq_printf(seq, " %lu/%lu cache hits/misses\n",
1115 es_stats->es_stats_cache_hits,
1116 es_stats->es_stats_cache_misses);
1118 seq_printf(seq, " %d inodes on list\n", inode_cnt);
1120 seq_printf(seq, "average:\n %llu us scan time\n",
1121 div_u64(es_stats->es_stats_scan_time, 1000));
1122 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1125 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1126 " %llu us max scan time\n",
1127 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
1128 div_u64(es_stats->es_stats_max_scan_time, 1000));
1133 int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1137 /* Make sure we have enough bits for physical block number */
1138 BUILD_BUG_ON(ES_SHIFT < 48);
1139 INIT_LIST_HEAD(&sbi->s_es_list);
1140 sbi->s_es_nr_inode = 0;
1141 spin_lock_init(&sbi->s_es_lock);
1142 sbi->s_es_stats.es_stats_shrunk = 0;
1143 sbi->s_es_stats.es_stats_cache_hits = 0;
1144 sbi->s_es_stats.es_stats_cache_misses = 0;
1145 sbi->s_es_stats.es_stats_scan_time = 0;
1146 sbi->s_es_stats.es_stats_max_scan_time = 0;
1147 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
1150 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
1154 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1155 sbi->s_es_shrinker.count_objects = ext4_es_count;
1156 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
1157 err = register_shrinker(&sbi->s_es_shrinker);
1164 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1166 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1170 void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
1172 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1173 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1174 unregister_shrinker(&sbi->s_es_shrinker);
1178 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1179 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1181 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1182 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1183 * ei->i_es_shrink_lblk to where we should continue scanning.
1185 static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1186 int *nr_to_scan, int *nr_shrunk)
1188 struct inode *inode = &ei->vfs_inode;
1189 struct ext4_es_tree *tree = &ei->i_es_tree;
1190 struct extent_status *es;
1191 struct rb_node *node;
1193 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1196 node = &es->rb_node;
1197 while (*nr_to_scan > 0) {
1198 if (es->es_lblk > end) {
1199 ei->i_es_shrink_lblk = end + 1;
1204 node = rb_next(&es->rb_node);
1206 * We can't reclaim delayed extent from status tree because
1207 * fiemap, bigallic, and seek_data/hole need to use it.
1209 if (ext4_es_is_delayed(es))
1211 if (ext4_es_is_referenced(es)) {
1212 ext4_es_clear_referenced(es);
1216 rb_erase(&es->rb_node, &tree->root);
1217 ext4_es_free_extent(inode, es);
1222 es = rb_entry(node, struct extent_status, rb_node);
1224 ei->i_es_shrink_lblk = es->es_lblk;
1227 ei->i_es_shrink_lblk = 0;
1231 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1233 struct inode *inode = &ei->vfs_inode;
1235 ext4_lblk_t start = ei->i_es_shrink_lblk;
1236 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1237 DEFAULT_RATELIMIT_BURST);
1239 if (ei->i_es_shk_nr == 0)
1242 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1244 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1246 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1248 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1250 ei->i_es_tree.cache_es = NULL;