2 * fs/ext4/extents_status.c
4 * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
6 * Allison Henderson <achender@linux.vnet.ibm.com>
7 * Hugh Dickins <hughd@google.com>
8 * Zheng Liu <wenqing.lz@taobao.com>
10 * Ext4 extents status tree core functions.
12 #include <linux/rbtree.h>
13 #include <linux/list_sort.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
17 #include "extents_status.h"
19 #include <trace/events/ext4.h>
22 * According to previous discussion in Ext4 Developer Workshop, we
23 * will introduce a new structure called io tree to track all extent
24 * status in order to solve some problems that we have met
25 * (e.g. Reservation space warning), and provide extent-level locking.
26 * Delay extent tree is the first step to achieve this goal. It is
27 * original built by Yongqiang Yang. At that time it is called delay
28 * extent tree, whose goal is only track delayed extents in memory to
29 * simplify the implementation of fiemap and bigalloc, and introduce
30 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
31 * delay extent tree at the first commit. But for better understand
32 * what it does, it has been rename to extent status tree.
35 * Currently the first step has been done. All delayed extents are
36 * tracked in the tree. It maintains the delayed extent when a delayed
37 * allocation is issued, and the delayed extent is written out or
38 * invalidated. Therefore the implementation of fiemap and bigalloc
39 * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
41 * The following comment describes the implemenmtation of extent
42 * status tree and future works.
45 * In this step all extent status are tracked by extent status tree.
46 * Thus, we can first try to lookup a block mapping in this tree before
47 * finding it in extent tree. Hence, single extent cache can be removed
48 * because extent status tree can do a better job. Extents in status
49 * tree are loaded on-demand. Therefore, the extent status tree may not
50 * contain all of the extents in a file. Meanwhile we define a shrinker
51 * to reclaim memory from extent status tree because fragmented extent
52 * tree will make status tree cost too much memory. written/unwritten/-
53 * hole extents in the tree will be reclaimed by this shrinker when we
54 * are under high memory pressure. Delayed extents will not be
55 * reclimed because fiemap, bigalloc, and seek_data/hole need it.
59 * Extent status tree implementation for ext4.
62 * ==========================================================================
63 * Extent status tree tracks all extent status.
65 * 1. Why we need to implement extent status tree?
67 * Without extent status tree, ext4 identifies a delayed extent by looking
68 * up page cache, this has several deficiencies - complicated, buggy,
69 * and inefficient code.
71 * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
72 * block or a range of blocks are belonged to a delayed extent.
74 * Let us have a look at how they do without extent status tree.
76 * FIEMAP looks up page cache to identify delayed allocations from holes.
79 * SEEK_HOLE/DATA has the same problem as FIEMAP.
82 * bigalloc looks up page cache to figure out if a block is
83 * already under delayed allocation or not to determine whether
84 * quota reserving is needed for the cluster.
87 * Writeout looks up whole page cache to see if a buffer is
88 * mapped, If there are not very many delayed buffers, then it is
91 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
92 * bigalloc and writeout can figure out if a block or a range of
93 * blocks is under delayed allocation(belonged to a delayed extent) or
94 * not by searching the extent tree.
97 * ==========================================================================
98 * 2. Ext4 extent status tree impelmentation
101 * A extent is a range of blocks which are contiguous logically and
102 * physically. Unlike extent in extent tree, this extent in ext4 is
103 * a in-memory struct, there is no corresponding on-disk data. There
104 * is no limit on length of extent, so an extent can contain as many
105 * blocks as they are contiguous logically and physically.
107 * -- extent status tree
108 * Every inode has an extent status tree and all allocation blocks
109 * are added to the tree with different status. The extent in the
110 * tree are ordered by logical block no.
112 * -- operations on a extent status tree
113 * There are three important operations on a delayed extent tree: find
114 * next extent, adding a extent(a range of blocks) and removing a extent.
116 * -- race on a extent status tree
117 * Extent status tree is protected by inode->i_es_lock.
119 * -- memory consumption
120 * Fragmented extent tree will make extent status tree cost too much
121 * memory. Hence, we will reclaim written/unwritten/hole extents from
122 * the tree under a heavy memory pressure.
125 * ==========================================================================
126 * 3. Performance analysis
129 * 1. There is a cache extent for write access, so if writes are
130 * not very random, adding space operaions are in O(1) time.
133 * 2. Code is much simpler, more readable, more maintainable and
137 * ==========================================================================
140 * -- Refactor delayed space reservation
142 * -- Extent-level locking
145 static struct kmem_cache
*ext4_es_cachep
;
147 static int __es_insert_extent(struct inode
*inode
, struct extent_status
*newes
);
148 static int __es_remove_extent(struct inode
*inode
, ext4_lblk_t lblk
,
150 static int __es_try_to_reclaim_extents(struct ext4_inode_info
*ei
,
152 static int __ext4_es_shrink(struct ext4_sb_info
*sbi
, int nr_to_scan
,
153 struct ext4_inode_info
*locked_ei
);
155 int __init
ext4_init_es(void)
157 ext4_es_cachep
= kmem_cache_create("ext4_extent_status",
158 sizeof(struct extent_status
),
159 0, (SLAB_RECLAIM_ACCOUNT
), NULL
);
160 if (ext4_es_cachep
== NULL
)
165 void ext4_exit_es(void)
168 kmem_cache_destroy(ext4_es_cachep
);
171 void ext4_es_init_tree(struct ext4_es_tree
*tree
)
173 tree
->root
= RB_ROOT
;
174 tree
->cache_es
= NULL
;
178 static void ext4_es_print_tree(struct inode
*inode
)
180 struct ext4_es_tree
*tree
;
181 struct rb_node
*node
;
183 printk(KERN_DEBUG
"status extents for inode %lu:", inode
->i_ino
);
184 tree
= &EXT4_I(inode
)->i_es_tree
;
185 node
= rb_first(&tree
->root
);
187 struct extent_status
*es
;
188 es
= rb_entry(node
, struct extent_status
, rb_node
);
189 printk(KERN_DEBUG
" [%u/%u) %llu %x",
190 es
->es_lblk
, es
->es_len
,
191 ext4_es_pblock(es
), ext4_es_status(es
));
192 node
= rb_next(node
);
194 printk(KERN_DEBUG
"\n");
197 #define ext4_es_print_tree(inode)
200 static inline ext4_lblk_t
ext4_es_end(struct extent_status
*es
)
202 BUG_ON(es
->es_lblk
+ es
->es_len
< es
->es_lblk
);
203 return es
->es_lblk
+ es
->es_len
- 1;
207 * search through the tree for an delayed extent with a given offset. If
208 * it can't be found, try to find next extent.
210 static struct extent_status
*__es_tree_search(struct rb_root
*root
,
213 struct rb_node
*node
= root
->rb_node
;
214 struct extent_status
*es
= NULL
;
217 es
= rb_entry(node
, struct extent_status
, rb_node
);
218 if (lblk
< es
->es_lblk
)
219 node
= node
->rb_left
;
220 else if (lblk
> ext4_es_end(es
))
221 node
= node
->rb_right
;
226 if (es
&& lblk
< es
->es_lblk
)
229 if (es
&& lblk
> ext4_es_end(es
)) {
230 node
= rb_next(&es
->rb_node
);
231 return node
? rb_entry(node
, struct extent_status
, rb_node
) :
239 * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
240 * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
242 * @inode: the inode which owns delayed extents
243 * @lblk: the offset where we start to search
244 * @end: the offset where we stop to search
245 * @es: delayed extent that we found
247 void ext4_es_find_delayed_extent_range(struct inode
*inode
,
248 ext4_lblk_t lblk
, ext4_lblk_t end
,
249 struct extent_status
*es
)
251 struct ext4_es_tree
*tree
= NULL
;
252 struct extent_status
*es1
= NULL
;
253 struct rb_node
*node
;
257 trace_ext4_es_find_delayed_extent_range_enter(inode
, lblk
);
259 read_lock(&EXT4_I(inode
)->i_es_lock
);
260 tree
= &EXT4_I(inode
)->i_es_tree
;
262 /* find extent in cache firstly */
263 es
->es_lblk
= es
->es_len
= es
->es_pblk
= 0;
264 if (tree
->cache_es
) {
265 es1
= tree
->cache_es
;
266 if (in_range(lblk
, es1
->es_lblk
, es1
->es_len
)) {
267 es_debug("%u cached by [%u/%u) %llu %x\n",
268 lblk
, es1
->es_lblk
, es1
->es_len
,
269 ext4_es_pblock(es1
), ext4_es_status(es1
));
274 es1
= __es_tree_search(&tree
->root
, lblk
);
277 if (es1
&& !ext4_es_is_delayed(es1
)) {
278 while ((node
= rb_next(&es1
->rb_node
)) != NULL
) {
279 es1
= rb_entry(node
, struct extent_status
, rb_node
);
280 if (es1
->es_lblk
> end
) {
284 if (ext4_es_is_delayed(es1
))
289 if (es1
&& ext4_es_is_delayed(es1
)) {
290 tree
->cache_es
= es1
;
291 es
->es_lblk
= es1
->es_lblk
;
292 es
->es_len
= es1
->es_len
;
293 es
->es_pblk
= es1
->es_pblk
;
296 read_unlock(&EXT4_I(inode
)->i_es_lock
);
298 trace_ext4_es_find_delayed_extent_range_exit(inode
, es
);
301 static struct extent_status
*
302 ext4_es_alloc_extent(struct inode
*inode
, ext4_lblk_t lblk
, ext4_lblk_t len
,
305 struct extent_status
*es
;
306 es
= kmem_cache_alloc(ext4_es_cachep
, GFP_ATOMIC
);
314 * We don't count delayed extent because we never try to reclaim them
316 if (!ext4_es_is_delayed(es
)) {
317 EXT4_I(inode
)->i_es_lru_nr
++;
318 percpu_counter_inc(&EXT4_SB(inode
->i_sb
)->
319 s_es_stats
.es_stats_lru_cnt
);
322 EXT4_I(inode
)->i_es_all_nr
++;
323 percpu_counter_inc(&EXT4_SB(inode
->i_sb
)->s_es_stats
.es_stats_all_cnt
);
328 static void ext4_es_free_extent(struct inode
*inode
, struct extent_status
*es
)
330 EXT4_I(inode
)->i_es_all_nr
--;
331 percpu_counter_dec(&EXT4_SB(inode
->i_sb
)->s_es_stats
.es_stats_all_cnt
);
333 /* Decrease the lru counter when this es is not delayed */
334 if (!ext4_es_is_delayed(es
)) {
335 BUG_ON(EXT4_I(inode
)->i_es_lru_nr
== 0);
336 EXT4_I(inode
)->i_es_lru_nr
--;
337 percpu_counter_dec(&EXT4_SB(inode
->i_sb
)->
338 s_es_stats
.es_stats_lru_cnt
);
341 kmem_cache_free(ext4_es_cachep
, es
);
345 * Check whether or not two extents can be merged
347 * - logical block number is contiguous
348 * - physical block number is contiguous
351 static int ext4_es_can_be_merged(struct extent_status
*es1
,
352 struct extent_status
*es2
)
354 if (ext4_es_status(es1
) != ext4_es_status(es2
))
357 if (((__u64
) es1
->es_len
) + es2
->es_len
> EXT_MAX_BLOCKS
) {
358 pr_warn("ES assertion failed when merging extents. "
359 "The sum of lengths of es1 (%d) and es2 (%d) "
360 "is bigger than allowed file size (%d)\n",
361 es1
->es_len
, es2
->es_len
, EXT_MAX_BLOCKS
);
366 if (((__u64
) es1
->es_lblk
) + es1
->es_len
!= es2
->es_lblk
)
369 if ((ext4_es_is_written(es1
) || ext4_es_is_unwritten(es1
)) &&
370 (ext4_es_pblock(es1
) + es1
->es_len
== ext4_es_pblock(es2
)))
373 if (ext4_es_is_hole(es1
))
376 /* we need to check delayed extent is without unwritten status */
377 if (ext4_es_is_delayed(es1
) && !ext4_es_is_unwritten(es1
))
383 static struct extent_status
*
384 ext4_es_try_to_merge_left(struct inode
*inode
, struct extent_status
*es
)
386 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
387 struct extent_status
*es1
;
388 struct rb_node
*node
;
390 node
= rb_prev(&es
->rb_node
);
394 es1
= rb_entry(node
, struct extent_status
, rb_node
);
395 if (ext4_es_can_be_merged(es1
, es
)) {
396 es1
->es_len
+= es
->es_len
;
397 rb_erase(&es
->rb_node
, &tree
->root
);
398 ext4_es_free_extent(inode
, es
);
405 static struct extent_status
*
406 ext4_es_try_to_merge_right(struct inode
*inode
, struct extent_status
*es
)
408 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
409 struct extent_status
*es1
;
410 struct rb_node
*node
;
412 node
= rb_next(&es
->rb_node
);
416 es1
= rb_entry(node
, struct extent_status
, rb_node
);
417 if (ext4_es_can_be_merged(es
, es1
)) {
418 es
->es_len
+= es1
->es_len
;
419 rb_erase(node
, &tree
->root
);
420 ext4_es_free_extent(inode
, es1
);
426 #ifdef ES_AGGRESSIVE_TEST
427 #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
429 static void ext4_es_insert_extent_ext_check(struct inode
*inode
,
430 struct extent_status
*es
)
432 struct ext4_ext_path
*path
= NULL
;
433 struct ext4_extent
*ex
;
434 ext4_lblk_t ee_block
;
435 ext4_fsblk_t ee_start
;
436 unsigned short ee_len
;
437 int depth
, ee_status
, es_status
;
439 path
= ext4_find_extent(inode
, es
->es_lblk
, NULL
, EXT4_EX_NOCACHE
);
443 depth
= ext_depth(inode
);
444 ex
= path
[depth
].p_ext
;
448 ee_block
= le32_to_cpu(ex
->ee_block
);
449 ee_start
= ext4_ext_pblock(ex
);
450 ee_len
= ext4_ext_get_actual_len(ex
);
452 ee_status
= ext4_ext_is_unwritten(ex
) ? 1 : 0;
453 es_status
= ext4_es_is_unwritten(es
) ? 1 : 0;
456 * Make sure ex and es are not overlap when we try to insert
457 * a delayed/hole extent.
459 if (!ext4_es_is_written(es
) && !ext4_es_is_unwritten(es
)) {
460 if (in_range(es
->es_lblk
, ee_block
, ee_len
)) {
461 pr_warn("ES insert assertion failed for "
462 "inode: %lu we can find an extent "
463 "at block [%d/%d/%llu/%c], but we "
464 "want to add a delayed/hole extent "
466 inode
->i_ino
, ee_block
, ee_len
,
467 ee_start
, ee_status
? 'u' : 'w',
468 es
->es_lblk
, es
->es_len
,
469 ext4_es_pblock(es
), ext4_es_status(es
));
475 * We don't check ee_block == es->es_lblk, etc. because es
476 * might be a part of whole extent, vice versa.
478 if (es
->es_lblk
< ee_block
||
479 ext4_es_pblock(es
) != ee_start
+ es
->es_lblk
- ee_block
) {
480 pr_warn("ES insert assertion failed for inode: %lu "
481 "ex_status [%d/%d/%llu/%c] != "
482 "es_status [%d/%d/%llu/%c]\n", inode
->i_ino
,
483 ee_block
, ee_len
, ee_start
,
484 ee_status
? 'u' : 'w', es
->es_lblk
, es
->es_len
,
485 ext4_es_pblock(es
), es_status
? 'u' : 'w');
489 if (ee_status
^ es_status
) {
490 pr_warn("ES insert assertion failed for inode: %lu "
491 "ex_status [%d/%d/%llu/%c] != "
492 "es_status [%d/%d/%llu/%c]\n", inode
->i_ino
,
493 ee_block
, ee_len
, ee_start
,
494 ee_status
? 'u' : 'w', es
->es_lblk
, es
->es_len
,
495 ext4_es_pblock(es
), es_status
? 'u' : 'w');
499 * We can't find an extent on disk. So we need to make sure
500 * that we don't want to add an written/unwritten extent.
502 if (!ext4_es_is_delayed(es
) && !ext4_es_is_hole(es
)) {
503 pr_warn("ES insert assertion failed for inode: %lu "
504 "can't find an extent at block %d but we want "
505 "to add a written/unwritten extent "
506 "[%d/%d/%llu/%x]\n", inode
->i_ino
,
507 es
->es_lblk
, es
->es_lblk
, es
->es_len
,
508 ext4_es_pblock(es
), ext4_es_status(es
));
512 ext4_ext_drop_refs(path
);
516 static void ext4_es_insert_extent_ind_check(struct inode
*inode
,
517 struct extent_status
*es
)
519 struct ext4_map_blocks map
;
523 * Here we call ext4_ind_map_blocks to lookup a block mapping because
524 * 'Indirect' structure is defined in indirect.c. So we couldn't
525 * access direct/indirect tree from outside. It is too dirty to define
526 * this function in indirect.c file.
529 map
.m_lblk
= es
->es_lblk
;
530 map
.m_len
= es
->es_len
;
532 retval
= ext4_ind_map_blocks(NULL
, inode
, &map
, 0);
534 if (ext4_es_is_delayed(es
) || ext4_es_is_hole(es
)) {
536 * We want to add a delayed/hole extent but this
537 * block has been allocated.
539 pr_warn("ES insert assertion failed for inode: %lu "
540 "We can find blocks but we want to add a "
541 "delayed/hole extent [%d/%d/%llu/%x]\n",
542 inode
->i_ino
, es
->es_lblk
, es
->es_len
,
543 ext4_es_pblock(es
), ext4_es_status(es
));
545 } else if (ext4_es_is_written(es
)) {
546 if (retval
!= es
->es_len
) {
547 pr_warn("ES insert assertion failed for "
548 "inode: %lu retval %d != es_len %d\n",
549 inode
->i_ino
, retval
, es
->es_len
);
552 if (map
.m_pblk
!= ext4_es_pblock(es
)) {
553 pr_warn("ES insert assertion failed for "
554 "inode: %lu m_pblk %llu != "
556 inode
->i_ino
, map
.m_pblk
,
562 * We don't need to check unwritten extent because
563 * indirect-based file doesn't have it.
567 } else if (retval
== 0) {
568 if (ext4_es_is_written(es
)) {
569 pr_warn("ES insert assertion failed for inode: %lu "
570 "We can't find the block but we want to add "
571 "a written extent [%d/%d/%llu/%x]\n",
572 inode
->i_ino
, es
->es_lblk
, es
->es_len
,
573 ext4_es_pblock(es
), ext4_es_status(es
));
579 static inline void ext4_es_insert_extent_check(struct inode
*inode
,
580 struct extent_status
*es
)
583 * We don't need to worry about the race condition because
584 * caller takes i_data_sem locking.
586 BUG_ON(!rwsem_is_locked(&EXT4_I(inode
)->i_data_sem
));
587 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
588 ext4_es_insert_extent_ext_check(inode
, es
);
590 ext4_es_insert_extent_ind_check(inode
, es
);
593 static inline void ext4_es_insert_extent_check(struct inode
*inode
,
594 struct extent_status
*es
)
599 static int __es_insert_extent(struct inode
*inode
, struct extent_status
*newes
)
601 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
602 struct rb_node
**p
= &tree
->root
.rb_node
;
603 struct rb_node
*parent
= NULL
;
604 struct extent_status
*es
;
608 es
= rb_entry(parent
, struct extent_status
, rb_node
);
610 if (newes
->es_lblk
< es
->es_lblk
) {
611 if (ext4_es_can_be_merged(newes
, es
)) {
613 * Here we can modify es_lblk directly
614 * because it isn't overlapped.
616 es
->es_lblk
= newes
->es_lblk
;
617 es
->es_len
+= newes
->es_len
;
618 if (ext4_es_is_written(es
) ||
619 ext4_es_is_unwritten(es
))
620 ext4_es_store_pblock(es
,
622 es
= ext4_es_try_to_merge_left(inode
, es
);
626 } else if (newes
->es_lblk
> ext4_es_end(es
)) {
627 if (ext4_es_can_be_merged(es
, newes
)) {
628 es
->es_len
+= newes
->es_len
;
629 es
= ext4_es_try_to_merge_right(inode
, es
);
639 es
= ext4_es_alloc_extent(inode
, newes
->es_lblk
, newes
->es_len
,
643 rb_link_node(&es
->rb_node
, parent
, p
);
644 rb_insert_color(&es
->rb_node
, &tree
->root
);
652 * ext4_es_insert_extent() adds information to an inode's extent
655 * Return 0 on success, error code on failure.
657 int ext4_es_insert_extent(struct inode
*inode
, ext4_lblk_t lblk
,
658 ext4_lblk_t len
, ext4_fsblk_t pblk
,
661 struct extent_status newes
;
662 ext4_lblk_t end
= lblk
+ len
- 1;
665 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
666 lblk
, len
, pblk
, status
, inode
->i_ino
);
673 newes
.es_lblk
= lblk
;
675 ext4_es_store_pblock_status(&newes
, pblk
, status
);
676 trace_ext4_es_insert_extent(inode
, &newes
);
678 ext4_es_insert_extent_check(inode
, &newes
);
680 write_lock(&EXT4_I(inode
)->i_es_lock
);
681 err
= __es_remove_extent(inode
, lblk
, end
);
685 err
= __es_insert_extent(inode
, &newes
);
686 if (err
== -ENOMEM
&& __ext4_es_shrink(EXT4_SB(inode
->i_sb
), 1,
689 if (err
== -ENOMEM
&& !ext4_es_is_delayed(&newes
))
693 write_unlock(&EXT4_I(inode
)->i_es_lock
);
695 ext4_es_print_tree(inode
);
701 * ext4_es_cache_extent() inserts information into the extent status
702 * tree if and only if there isn't information about the range in
705 void ext4_es_cache_extent(struct inode
*inode
, ext4_lblk_t lblk
,
706 ext4_lblk_t len
, ext4_fsblk_t pblk
,
709 struct extent_status
*es
;
710 struct extent_status newes
;
711 ext4_lblk_t end
= lblk
+ len
- 1;
713 newes
.es_lblk
= lblk
;
715 ext4_es_store_pblock_status(&newes
, pblk
, status
);
716 trace_ext4_es_cache_extent(inode
, &newes
);
723 write_lock(&EXT4_I(inode
)->i_es_lock
);
725 es
= __es_tree_search(&EXT4_I(inode
)->i_es_tree
.root
, lblk
);
726 if (!es
|| es
->es_lblk
> end
)
727 __es_insert_extent(inode
, &newes
);
728 write_unlock(&EXT4_I(inode
)->i_es_lock
);
732 * ext4_es_lookup_extent() looks up an extent in extent status tree.
734 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
736 * Return: 1 on found, 0 on not
738 int ext4_es_lookup_extent(struct inode
*inode
, ext4_lblk_t lblk
,
739 struct extent_status
*es
)
741 struct ext4_es_tree
*tree
;
742 struct ext4_es_stats
*stats
;
743 struct extent_status
*es1
= NULL
;
744 struct rb_node
*node
;
747 trace_ext4_es_lookup_extent_enter(inode
, lblk
);
748 es_debug("lookup extent in block %u\n", lblk
);
750 tree
= &EXT4_I(inode
)->i_es_tree
;
751 read_lock(&EXT4_I(inode
)->i_es_lock
);
753 /* find extent in cache firstly */
754 es
->es_lblk
= es
->es_len
= es
->es_pblk
= 0;
755 if (tree
->cache_es
) {
756 es1
= tree
->cache_es
;
757 if (in_range(lblk
, es1
->es_lblk
, es1
->es_len
)) {
758 es_debug("%u cached by [%u/%u)\n",
759 lblk
, es1
->es_lblk
, es1
->es_len
);
765 node
= tree
->root
.rb_node
;
767 es1
= rb_entry(node
, struct extent_status
, rb_node
);
768 if (lblk
< es1
->es_lblk
)
769 node
= node
->rb_left
;
770 else if (lblk
> ext4_es_end(es1
))
771 node
= node
->rb_right
;
779 stats
= &EXT4_SB(inode
->i_sb
)->s_es_stats
;
782 es
->es_lblk
= es1
->es_lblk
;
783 es
->es_len
= es1
->es_len
;
784 es
->es_pblk
= es1
->es_pblk
;
785 stats
->es_stats_cache_hits
++;
787 stats
->es_stats_cache_misses
++;
790 read_unlock(&EXT4_I(inode
)->i_es_lock
);
792 trace_ext4_es_lookup_extent_exit(inode
, es
, found
);
796 static int __es_remove_extent(struct inode
*inode
, ext4_lblk_t lblk
,
799 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
800 struct rb_node
*node
;
801 struct extent_status
*es
;
802 struct extent_status orig_es
;
803 ext4_lblk_t len1
, len2
;
809 es
= __es_tree_search(&tree
->root
, lblk
);
812 if (es
->es_lblk
> end
)
815 /* Simply invalidate cache_es. */
816 tree
->cache_es
= NULL
;
818 orig_es
.es_lblk
= es
->es_lblk
;
819 orig_es
.es_len
= es
->es_len
;
820 orig_es
.es_pblk
= es
->es_pblk
;
822 len1
= lblk
> es
->es_lblk
? lblk
- es
->es_lblk
: 0;
823 len2
= ext4_es_end(es
) > end
? ext4_es_end(es
) - end
: 0;
828 struct extent_status newes
;
830 newes
.es_lblk
= end
+ 1;
832 block
= 0x7FDEADBEEFULL
;
833 if (ext4_es_is_written(&orig_es
) ||
834 ext4_es_is_unwritten(&orig_es
))
835 block
= ext4_es_pblock(&orig_es
) +
836 orig_es
.es_len
- len2
;
837 ext4_es_store_pblock_status(&newes
, block
,
838 ext4_es_status(&orig_es
));
839 err
= __es_insert_extent(inode
, &newes
);
841 es
->es_lblk
= orig_es
.es_lblk
;
842 es
->es_len
= orig_es
.es_len
;
843 if ((err
== -ENOMEM
) &&
844 __ext4_es_shrink(EXT4_SB(inode
->i_sb
), 1,
850 es
->es_lblk
= end
+ 1;
852 if (ext4_es_is_written(es
) ||
853 ext4_es_is_unwritten(es
)) {
854 block
= orig_es
.es_pblk
+ orig_es
.es_len
- len2
;
855 ext4_es_store_pblock(es
, block
);
862 node
= rb_next(&es
->rb_node
);
864 es
= rb_entry(node
, struct extent_status
, rb_node
);
869 while (es
&& ext4_es_end(es
) <= end
) {
870 node
= rb_next(&es
->rb_node
);
871 rb_erase(&es
->rb_node
, &tree
->root
);
872 ext4_es_free_extent(inode
, es
);
877 es
= rb_entry(node
, struct extent_status
, rb_node
);
880 if (es
&& es
->es_lblk
< end
+ 1) {
881 ext4_lblk_t orig_len
= es
->es_len
;
883 len1
= ext4_es_end(es
) - end
;
884 es
->es_lblk
= end
+ 1;
886 if (ext4_es_is_written(es
) || ext4_es_is_unwritten(es
)) {
887 block
= es
->es_pblk
+ orig_len
- len1
;
888 ext4_es_store_pblock(es
, block
);
897 * ext4_es_remove_extent() removes a space from a extent status tree.
899 * Return 0 on success, error code on failure.
901 int ext4_es_remove_extent(struct inode
*inode
, ext4_lblk_t lblk
,
907 trace_ext4_es_remove_extent(inode
, lblk
, len
);
908 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
909 lblk
, len
, inode
->i_ino
);
914 end
= lblk
+ len
- 1;
917 write_lock(&EXT4_I(inode
)->i_es_lock
);
918 err
= __es_remove_extent(inode
, lblk
, end
);
919 write_unlock(&EXT4_I(inode
)->i_es_lock
);
920 ext4_es_print_tree(inode
);
924 static int ext4_inode_touch_time_cmp(void *priv
, struct list_head
*a
,
927 struct ext4_inode_info
*eia
, *eib
;
928 eia
= list_entry(a
, struct ext4_inode_info
, i_es_lru
);
929 eib
= list_entry(b
, struct ext4_inode_info
, i_es_lru
);
931 if (ext4_test_inode_state(&eia
->vfs_inode
, EXT4_STATE_EXT_PRECACHED
) &&
932 !ext4_test_inode_state(&eib
->vfs_inode
, EXT4_STATE_EXT_PRECACHED
))
934 if (!ext4_test_inode_state(&eia
->vfs_inode
, EXT4_STATE_EXT_PRECACHED
) &&
935 ext4_test_inode_state(&eib
->vfs_inode
, EXT4_STATE_EXT_PRECACHED
))
937 if (eia
->i_touch_when
== eib
->i_touch_when
)
939 if (time_after(eia
->i_touch_when
, eib
->i_touch_when
))
945 static int __ext4_es_shrink(struct ext4_sb_info
*sbi
, int nr_to_scan
,
946 struct ext4_inode_info
*locked_ei
)
948 struct ext4_inode_info
*ei
;
949 struct ext4_es_stats
*es_stats
;
950 struct list_head
*cur
, *tmp
;
955 int retried
= 0, skip_precached
= 1, nr_skipped
= 0;
957 es_stats
= &sbi
->s_es_stats
;
958 start_time
= ktime_get();
959 spin_lock(&sbi
->s_es_lru_lock
);
962 list_for_each_safe(cur
, tmp
, &sbi
->s_es_lru
) {
966 * If we have already reclaimed all extents from extent
967 * status tree, just stop the loop immediately.
969 if (percpu_counter_read_positive(
970 &es_stats
->es_stats_lru_cnt
) == 0)
973 ei
= list_entry(cur
, struct ext4_inode_info
, i_es_lru
);
976 * Skip the inode that is newer than the last_sorted
977 * time. Normally we try hard to avoid shrinking
978 * precached inodes, but we will as a last resort.
980 if ((es_stats
->es_stats_last_sorted
< ei
->i_touch_when
) ||
981 (skip_precached
&& ext4_test_inode_state(&ei
->vfs_inode
,
982 EXT4_STATE_EXT_PRECACHED
))) {
984 list_move_tail(cur
, &skipped
);
988 if (ei
->i_es_lru_nr
== 0 || ei
== locked_ei
||
989 !write_trylock(&ei
->i_es_lock
))
992 shrunk
= __es_try_to_reclaim_extents(ei
, nr_to_scan
);
993 if (ei
->i_es_lru_nr
== 0)
994 list_del_init(&ei
->i_es_lru
);
995 write_unlock(&ei
->i_es_lock
);
998 nr_to_scan
-= shrunk
;
1003 /* Move the newer inodes into the tail of the LRU list. */
1004 list_splice_tail(&skipped
, &sbi
->s_es_lru
);
1005 INIT_LIST_HEAD(&skipped
);
1008 * If we skipped any inodes, and we weren't able to make any
1009 * forward progress, sort the list and try again.
1011 if ((nr_shrunk
== 0) && nr_skipped
&& !retried
) {
1013 list_sort(NULL
, &sbi
->s_es_lru
, ext4_inode_touch_time_cmp
);
1014 es_stats
->es_stats_last_sorted
= jiffies
;
1015 ei
= list_first_entry(&sbi
->s_es_lru
, struct ext4_inode_info
,
1018 * If there are no non-precached inodes left on the
1019 * list, start releasing precached extents.
1021 if (ext4_test_inode_state(&ei
->vfs_inode
,
1022 EXT4_STATE_EXT_PRECACHED
))
1027 spin_unlock(&sbi
->s_es_lru_lock
);
1029 if (locked_ei
&& nr_shrunk
== 0)
1030 nr_shrunk
= __es_try_to_reclaim_extents(locked_ei
, nr_to_scan
);
1032 scan_time
= ktime_to_ns(ktime_sub(ktime_get(), start_time
));
1033 if (likely(es_stats
->es_stats_scan_time
))
1034 es_stats
->es_stats_scan_time
= (scan_time
+
1035 es_stats
->es_stats_scan_time
*3) / 4;
1037 es_stats
->es_stats_scan_time
= scan_time
;
1038 if (scan_time
> es_stats
->es_stats_max_scan_time
)
1039 es_stats
->es_stats_max_scan_time
= scan_time
;
1040 if (likely(es_stats
->es_stats_shrunk
))
1041 es_stats
->es_stats_shrunk
= (nr_shrunk
+
1042 es_stats
->es_stats_shrunk
*3) / 4;
1044 es_stats
->es_stats_shrunk
= nr_shrunk
;
1046 trace_ext4_es_shrink(sbi
->s_sb
, nr_shrunk
, scan_time
, skip_precached
,
1047 nr_skipped
, retried
);
1051 static unsigned long ext4_es_count(struct shrinker
*shrink
,
1052 struct shrink_control
*sc
)
1055 struct ext4_sb_info
*sbi
;
1057 sbi
= container_of(shrink
, struct ext4_sb_info
, s_es_shrinker
);
1058 nr
= percpu_counter_read_positive(&sbi
->s_es_stats
.es_stats_lru_cnt
);
1059 trace_ext4_es_shrink_count(sbi
->s_sb
, sc
->nr_to_scan
, nr
);
1063 static unsigned long ext4_es_scan(struct shrinker
*shrink
,
1064 struct shrink_control
*sc
)
1066 struct ext4_sb_info
*sbi
= container_of(shrink
,
1067 struct ext4_sb_info
, s_es_shrinker
);
1068 int nr_to_scan
= sc
->nr_to_scan
;
1071 ret
= percpu_counter_read_positive(&sbi
->s_es_stats
.es_stats_lru_cnt
);
1072 trace_ext4_es_shrink_scan_enter(sbi
->s_sb
, nr_to_scan
, ret
);
1077 nr_shrunk
= __ext4_es_shrink(sbi
, nr_to_scan
, NULL
);
1079 trace_ext4_es_shrink_scan_exit(sbi
->s_sb
, nr_shrunk
, ret
);
1083 static void *ext4_es_seq_shrinker_info_start(struct seq_file
*seq
, loff_t
*pos
)
1085 return *pos
? NULL
: SEQ_START_TOKEN
;
1089 ext4_es_seq_shrinker_info_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1094 static int ext4_es_seq_shrinker_info_show(struct seq_file
*seq
, void *v
)
1096 struct ext4_sb_info
*sbi
= seq
->private;
1097 struct ext4_es_stats
*es_stats
= &sbi
->s_es_stats
;
1098 struct ext4_inode_info
*ei
, *max
= NULL
;
1099 unsigned int inode_cnt
= 0;
1101 if (v
!= SEQ_START_TOKEN
)
1104 /* here we just find an inode that has the max nr. of objects */
1105 spin_lock(&sbi
->s_es_lru_lock
);
1106 list_for_each_entry(ei
, &sbi
->s_es_lru
, i_es_lru
) {
1108 if (max
&& max
->i_es_all_nr
< ei
->i_es_all_nr
)
1113 spin_unlock(&sbi
->s_es_lru_lock
);
1115 seq_printf(seq
, "stats:\n %lld objects\n %lld reclaimable objects\n",
1116 percpu_counter_sum_positive(&es_stats
->es_stats_all_cnt
),
1117 percpu_counter_sum_positive(&es_stats
->es_stats_lru_cnt
));
1118 seq_printf(seq
, " %lu/%lu cache hits/misses\n",
1119 es_stats
->es_stats_cache_hits
,
1120 es_stats
->es_stats_cache_misses
);
1121 if (es_stats
->es_stats_last_sorted
!= 0)
1122 seq_printf(seq
, " %u ms last sorted interval\n",
1123 jiffies_to_msecs(jiffies
-
1124 es_stats
->es_stats_last_sorted
));
1126 seq_printf(seq
, " %d inodes on lru list\n", inode_cnt
);
1128 seq_printf(seq
, "average:\n %llu us scan time\n",
1129 div_u64(es_stats
->es_stats_scan_time
, 1000));
1130 seq_printf(seq
, " %lu shrunk objects\n", es_stats
->es_stats_shrunk
);
1133 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1134 " %llu us max scan time\n",
1135 max
->vfs_inode
.i_ino
, max
->i_es_all_nr
, max
->i_es_lru_nr
,
1136 div_u64(es_stats
->es_stats_max_scan_time
, 1000));
1141 static void ext4_es_seq_shrinker_info_stop(struct seq_file
*seq
, void *v
)
1145 static const struct seq_operations ext4_es_seq_shrinker_info_ops
= {
1146 .start
= ext4_es_seq_shrinker_info_start
,
1147 .next
= ext4_es_seq_shrinker_info_next
,
1148 .stop
= ext4_es_seq_shrinker_info_stop
,
1149 .show
= ext4_es_seq_shrinker_info_show
,
1153 ext4_es_seq_shrinker_info_open(struct inode
*inode
, struct file
*file
)
1157 ret
= seq_open(file
, &ext4_es_seq_shrinker_info_ops
);
1159 struct seq_file
*m
= file
->private_data
;
1160 m
->private = PDE_DATA(inode
);
1167 ext4_es_seq_shrinker_info_release(struct inode
*inode
, struct file
*file
)
1169 return seq_release(inode
, file
);
1172 static const struct file_operations ext4_es_seq_shrinker_info_fops
= {
1173 .owner
= THIS_MODULE
,
1174 .open
= ext4_es_seq_shrinker_info_open
,
1176 .llseek
= seq_lseek
,
1177 .release
= ext4_es_seq_shrinker_info_release
,
1180 int ext4_es_register_shrinker(struct ext4_sb_info
*sbi
)
1184 INIT_LIST_HEAD(&sbi
->s_es_lru
);
1185 spin_lock_init(&sbi
->s_es_lru_lock
);
1186 sbi
->s_es_stats
.es_stats_last_sorted
= 0;
1187 sbi
->s_es_stats
.es_stats_shrunk
= 0;
1188 sbi
->s_es_stats
.es_stats_cache_hits
= 0;
1189 sbi
->s_es_stats
.es_stats_cache_misses
= 0;
1190 sbi
->s_es_stats
.es_stats_scan_time
= 0;
1191 sbi
->s_es_stats
.es_stats_max_scan_time
= 0;
1192 err
= percpu_counter_init(&sbi
->s_es_stats
.es_stats_all_cnt
, 0, GFP_KERNEL
);
1195 err
= percpu_counter_init(&sbi
->s_es_stats
.es_stats_lru_cnt
, 0, GFP_KERNEL
);
1199 sbi
->s_es_shrinker
.scan_objects
= ext4_es_scan
;
1200 sbi
->s_es_shrinker
.count_objects
= ext4_es_count
;
1201 sbi
->s_es_shrinker
.seeks
= DEFAULT_SEEKS
;
1202 err
= register_shrinker(&sbi
->s_es_shrinker
);
1207 proc_create_data("es_shrinker_info", S_IRUGO
, sbi
->s_proc
,
1208 &ext4_es_seq_shrinker_info_fops
, sbi
);
1213 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_lru_cnt
);
1215 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_all_cnt
);
1219 void ext4_es_unregister_shrinker(struct ext4_sb_info
*sbi
)
1222 remove_proc_entry("es_shrinker_info", sbi
->s_proc
);
1223 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_all_cnt
);
1224 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_lru_cnt
);
1225 unregister_shrinker(&sbi
->s_es_shrinker
);
1228 void ext4_es_lru_add(struct inode
*inode
)
1230 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1231 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1233 ei
->i_touch_when
= jiffies
;
1235 if (!list_empty(&ei
->i_es_lru
))
1238 spin_lock(&sbi
->s_es_lru_lock
);
1239 if (list_empty(&ei
->i_es_lru
))
1240 list_add_tail(&ei
->i_es_lru
, &sbi
->s_es_lru
);
1241 spin_unlock(&sbi
->s_es_lru_lock
);
1244 void ext4_es_lru_del(struct inode
*inode
)
1246 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1247 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1249 spin_lock(&sbi
->s_es_lru_lock
);
1250 if (!list_empty(&ei
->i_es_lru
))
1251 list_del_init(&ei
->i_es_lru
);
1252 spin_unlock(&sbi
->s_es_lru_lock
);
1255 static int __es_try_to_reclaim_extents(struct ext4_inode_info
*ei
,
1258 struct inode
*inode
= &ei
->vfs_inode
;
1259 struct ext4_es_tree
*tree
= &ei
->i_es_tree
;
1260 struct rb_node
*node
;
1261 struct extent_status
*es
;
1262 unsigned long nr_shrunk
= 0;
1263 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
1264 DEFAULT_RATELIMIT_BURST
);
1266 if (ei
->i_es_lru_nr
== 0)
1269 if (ext4_test_inode_state(inode
, EXT4_STATE_EXT_PRECACHED
) &&
1271 ext4_warning(inode
->i_sb
, "forced shrink of precached extents");
1273 node
= rb_first(&tree
->root
);
1274 while (node
!= NULL
) {
1275 es
= rb_entry(node
, struct extent_status
, rb_node
);
1276 node
= rb_next(&es
->rb_node
);
1278 * We can't reclaim delayed extent from status tree because
1279 * fiemap, bigallic, and seek_data/hole need to use it.
1281 if (!ext4_es_is_delayed(es
)) {
1282 rb_erase(&es
->rb_node
, &tree
->root
);
1283 ext4_es_free_extent(inode
, es
);
1285 if (--nr_to_scan
== 0)
1289 tree
->cache_es
= NULL
;