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>
15 #include "extents_status.h"
16 #include "ext4_extents.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_try_to_reclaim_extents(struct ext4_inode_info *ei,
152 int __init ext4_init_es(void)
154 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
155 sizeof(struct extent_status),
156 0, (SLAB_RECLAIM_ACCOUNT), NULL);
157 if (ext4_es_cachep == NULL)
162 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 %llx",
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 %llx\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 struct extent_status *
299 ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
302 struct extent_status *es;
303 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
311 * We don't count delayed extent because we never try to reclaim them
313 if (!ext4_es_is_delayed(es)) {
314 EXT4_I(inode)->i_es_lru_nr++;
315 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt);
321 static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
323 /* Decrease the lru counter when this es is not delayed */
324 if (!ext4_es_is_delayed(es)) {
325 BUG_ON(EXT4_I(inode)->i_es_lru_nr == 0);
326 EXT4_I(inode)->i_es_lru_nr--;
327 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt);
330 kmem_cache_free(ext4_es_cachep, es);
334 * Check whether or not two extents can be merged
336 * - logical block number is contiguous
337 * - physical block number is contiguous
340 static int ext4_es_can_be_merged(struct extent_status *es1,
341 struct extent_status *es2)
343 if (ext4_es_status(es1) != ext4_es_status(es2))
346 if (((__u64) es1->es_len) + es2->es_len > 0xFFFFFFFFULL)
349 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
352 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
353 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
356 if (ext4_es_is_hole(es1))
359 /* we need to check delayed extent is without unwritten status */
360 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
366 static struct extent_status *
367 ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
369 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
370 struct extent_status *es1;
371 struct rb_node *node;
373 node = rb_prev(&es->rb_node);
377 es1 = rb_entry(node, struct extent_status, rb_node);
378 if (ext4_es_can_be_merged(es1, es)) {
379 es1->es_len += es->es_len;
380 rb_erase(&es->rb_node, &tree->root);
381 ext4_es_free_extent(inode, es);
388 static struct extent_status *
389 ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
391 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
392 struct extent_status *es1;
393 struct rb_node *node;
395 node = rb_next(&es->rb_node);
399 es1 = rb_entry(node, struct extent_status, rb_node);
400 if (ext4_es_can_be_merged(es, es1)) {
401 es->es_len += es1->es_len;
402 rb_erase(node, &tree->root);
403 ext4_es_free_extent(inode, es1);
409 #ifdef ES_AGGRESSIVE_TEST
410 static void ext4_es_insert_extent_ext_check(struct inode *inode,
411 struct extent_status *es)
413 struct ext4_ext_path *path = NULL;
414 struct ext4_extent *ex;
415 ext4_lblk_t ee_block;
416 ext4_fsblk_t ee_start;
417 unsigned short ee_len;
418 int depth, ee_status, es_status;
420 path = ext4_ext_find_extent(inode, es->es_lblk, NULL);
424 depth = ext_depth(inode);
425 ex = path[depth].p_ext;
429 ee_block = le32_to_cpu(ex->ee_block);
430 ee_start = ext4_ext_pblock(ex);
431 ee_len = ext4_ext_get_actual_len(ex);
433 ee_status = ext4_ext_is_uninitialized(ex) ? 1 : 0;
434 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
437 * Make sure ex and es are not overlap when we try to insert
438 * a delayed/hole extent.
440 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
441 if (in_range(es->es_lblk, ee_block, ee_len)) {
442 pr_warn("ES insert assertation failed for "
443 "inode: %lu we can find an extent "
444 "at block [%d/%d/%llu/%c], but we "
445 "want to add an delayed/hole extent "
446 "[%d/%d/%llu/%llx]\n",
447 inode->i_ino, ee_block, ee_len,
448 ee_start, ee_status ? 'u' : 'w',
449 es->es_lblk, es->es_len,
450 ext4_es_pblock(es), ext4_es_status(es));
456 * We don't check ee_block == es->es_lblk, etc. because es
457 * might be a part of whole extent, vice versa.
459 if (es->es_lblk < ee_block ||
460 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
461 pr_warn("ES insert assertation failed for inode: %lu "
462 "ex_status [%d/%d/%llu/%c] != "
463 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
464 ee_block, ee_len, ee_start,
465 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
466 ext4_es_pblock(es), es_status ? 'u' : 'w');
470 if (ee_status ^ es_status) {
471 pr_warn("ES insert assertation failed for inode: %lu "
472 "ex_status [%d/%d/%llu/%c] != "
473 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
474 ee_block, ee_len, ee_start,
475 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
476 ext4_es_pblock(es), es_status ? 'u' : 'w');
480 * We can't find an extent on disk. So we need to make sure
481 * that we don't want to add an written/unwritten extent.
483 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
484 pr_warn("ES insert assertation failed for inode: %lu "
485 "can't find an extent at block %d but we want "
486 "to add an written/unwritten extent "
487 "[%d/%d/%llu/%llx]\n", inode->i_ino,
488 es->es_lblk, es->es_lblk, es->es_len,
489 ext4_es_pblock(es), ext4_es_status(es));
494 ext4_ext_drop_refs(path);
499 static void ext4_es_insert_extent_ind_check(struct inode *inode,
500 struct extent_status *es)
502 struct ext4_map_blocks map;
506 * Here we call ext4_ind_map_blocks to lookup a block mapping because
507 * 'Indirect' structure is defined in indirect.c. So we couldn't
508 * access direct/indirect tree from outside. It is too dirty to define
509 * this function in indirect.c file.
512 map.m_lblk = es->es_lblk;
513 map.m_len = es->es_len;
515 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
517 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
519 * We want to add a delayed/hole extent but this
520 * block has been allocated.
522 pr_warn("ES insert assertation failed for inode: %lu "
523 "We can find blocks but we want to add a "
524 "delayed/hole extent [%d/%d/%llu/%llx]\n",
525 inode->i_ino, es->es_lblk, es->es_len,
526 ext4_es_pblock(es), ext4_es_status(es));
528 } else if (ext4_es_is_written(es)) {
529 if (retval != es->es_len) {
530 pr_warn("ES insert assertation failed for "
531 "inode: %lu retval %d != es_len %d\n",
532 inode->i_ino, retval, es->es_len);
535 if (map.m_pblk != ext4_es_pblock(es)) {
536 pr_warn("ES insert assertation failed for "
537 "inode: %lu m_pblk %llu != "
539 inode->i_ino, map.m_pblk,
545 * We don't need to check unwritten extent because
546 * indirect-based file doesn't have it.
550 } else if (retval == 0) {
551 if (ext4_es_is_written(es)) {
552 pr_warn("ES insert assertation failed for inode: %lu "
553 "We can't find the block but we want to add "
554 "an written extent [%d/%d/%llu/%llx]\n",
555 inode->i_ino, es->es_lblk, es->es_len,
556 ext4_es_pblock(es), ext4_es_status(es));
562 static inline void ext4_es_insert_extent_check(struct inode *inode,
563 struct extent_status *es)
566 * We don't need to worry about the race condition because
567 * caller takes i_data_sem locking.
569 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
570 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
571 ext4_es_insert_extent_ext_check(inode, es);
573 ext4_es_insert_extent_ind_check(inode, es);
576 static inline void ext4_es_insert_extent_check(struct inode *inode,
577 struct extent_status *es)
582 static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
584 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
585 struct rb_node **p = &tree->root.rb_node;
586 struct rb_node *parent = NULL;
587 struct extent_status *es;
591 es = rb_entry(parent, struct extent_status, rb_node);
593 if (newes->es_lblk < es->es_lblk) {
594 if (ext4_es_can_be_merged(newes, es)) {
596 * Here we can modify es_lblk directly
597 * because it isn't overlapped.
599 es->es_lblk = newes->es_lblk;
600 es->es_len += newes->es_len;
601 if (ext4_es_is_written(es) ||
602 ext4_es_is_unwritten(es))
603 ext4_es_store_pblock(es,
605 es = ext4_es_try_to_merge_left(inode, es);
609 } else if (newes->es_lblk > ext4_es_end(es)) {
610 if (ext4_es_can_be_merged(es, newes)) {
611 es->es_len += newes->es_len;
612 es = ext4_es_try_to_merge_right(inode, es);
622 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
626 rb_link_node(&es->rb_node, parent, p);
627 rb_insert_color(&es->rb_node, &tree->root);
635 * ext4_es_insert_extent() adds a space to a extent status tree.
637 * ext4_es_insert_extent is called by ext4_da_write_begin and
638 * ext4_es_remove_extent.
640 * Return 0 on success, error code on failure.
642 int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
643 ext4_lblk_t len, ext4_fsblk_t pblk,
644 unsigned long long status)
646 struct extent_status newes;
647 ext4_lblk_t end = lblk + len - 1;
650 es_debug("add [%u/%u) %llu %llx to extent status tree of inode %lu\n",
651 lblk, len, pblk, status, inode->i_ino);
658 newes.es_lblk = lblk;
660 ext4_es_store_pblock(&newes, pblk);
661 ext4_es_store_status(&newes, status);
662 trace_ext4_es_insert_extent(inode, &newes);
664 ext4_es_insert_extent_check(inode, &newes);
666 write_lock(&EXT4_I(inode)->i_es_lock);
667 err = __es_remove_extent(inode, lblk, end);
670 err = __es_insert_extent(inode, &newes);
673 write_unlock(&EXT4_I(inode)->i_es_lock);
675 ext4_es_print_tree(inode);
681 * ext4_es_lookup_extent() looks up an extent in extent status tree.
683 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
685 * Return: 1 on found, 0 on not
687 int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
688 struct extent_status *es)
690 struct ext4_es_tree *tree;
691 struct extent_status *es1 = NULL;
692 struct rb_node *node;
695 trace_ext4_es_lookup_extent_enter(inode, lblk);
696 es_debug("lookup extent in block %u\n", lblk);
698 tree = &EXT4_I(inode)->i_es_tree;
699 read_lock(&EXT4_I(inode)->i_es_lock);
701 /* find extent in cache firstly */
702 es->es_lblk = es->es_len = es->es_pblk = 0;
703 if (tree->cache_es) {
704 es1 = tree->cache_es;
705 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
706 es_debug("%u cached by [%u/%u)\n",
707 lblk, es1->es_lblk, es1->es_len);
713 node = tree->root.rb_node;
715 es1 = rb_entry(node, struct extent_status, rb_node);
716 if (lblk < es1->es_lblk)
717 node = node->rb_left;
718 else if (lblk > ext4_es_end(es1))
719 node = node->rb_right;
729 es->es_lblk = es1->es_lblk;
730 es->es_len = es1->es_len;
731 es->es_pblk = es1->es_pblk;
734 read_unlock(&EXT4_I(inode)->i_es_lock);
736 trace_ext4_es_lookup_extent_exit(inode, es, found);
740 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
743 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
744 struct rb_node *node;
745 struct extent_status *es;
746 struct extent_status orig_es;
747 ext4_lblk_t len1, len2;
751 es = __es_tree_search(&tree->root, lblk);
754 if (es->es_lblk > end)
757 /* Simply invalidate cache_es. */
758 tree->cache_es = NULL;
760 orig_es.es_lblk = es->es_lblk;
761 orig_es.es_len = es->es_len;
762 orig_es.es_pblk = es->es_pblk;
764 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
765 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
770 struct extent_status newes;
772 newes.es_lblk = end + 1;
774 if (ext4_es_is_written(&orig_es) ||
775 ext4_es_is_unwritten(&orig_es)) {
776 block = ext4_es_pblock(&orig_es) +
777 orig_es.es_len - len2;
778 ext4_es_store_pblock(&newes, block);
780 ext4_es_store_status(&newes, ext4_es_status(&orig_es));
781 err = __es_insert_extent(inode, &newes);
783 es->es_lblk = orig_es.es_lblk;
784 es->es_len = orig_es.es_len;
788 es->es_lblk = end + 1;
790 if (ext4_es_is_written(es) ||
791 ext4_es_is_unwritten(es)) {
792 block = orig_es.es_pblk + orig_es.es_len - len2;
793 ext4_es_store_pblock(es, block);
800 node = rb_next(&es->rb_node);
802 es = rb_entry(node, struct extent_status, rb_node);
807 while (es && ext4_es_end(es) <= end) {
808 node = rb_next(&es->rb_node);
809 rb_erase(&es->rb_node, &tree->root);
810 ext4_es_free_extent(inode, es);
815 es = rb_entry(node, struct extent_status, rb_node);
818 if (es && es->es_lblk < end + 1) {
819 ext4_lblk_t orig_len = es->es_len;
821 len1 = ext4_es_end(es) - end;
822 es->es_lblk = end + 1;
824 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
825 block = es->es_pblk + orig_len - len1;
826 ext4_es_store_pblock(es, block);
835 * ext4_es_remove_extent() removes a space from a extent status tree.
837 * Return 0 on success, error code on failure.
839 int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
845 trace_ext4_es_remove_extent(inode, lblk, len);
846 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
847 lblk, len, inode->i_ino);
852 end = lblk + len - 1;
855 write_lock(&EXT4_I(inode)->i_es_lock);
856 err = __es_remove_extent(inode, lblk, end);
857 write_unlock(&EXT4_I(inode)->i_es_lock);
858 ext4_es_print_tree(inode);
862 int ext4_es_zeroout(struct inode *inode, struct ext4_extent *ex)
864 ext4_lblk_t ee_block;
865 ext4_fsblk_t ee_pblock;
868 ee_block = le32_to_cpu(ex->ee_block);
869 ee_len = ext4_ext_get_actual_len(ex);
870 ee_pblock = ext4_ext_pblock(ex);
875 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
876 EXTENT_STATUS_WRITTEN);
879 static int ext4_inode_touch_time_cmp(void *priv, struct list_head *a,
882 struct ext4_inode_info *eia, *eib;
883 eia = list_entry(a, struct ext4_inode_info, i_es_lru);
884 eib = list_entry(b, struct ext4_inode_info, i_es_lru);
886 if (eia->i_touch_when == eib->i_touch_when)
888 if (time_after(eia->i_touch_when, eib->i_touch_when))
894 static int ext4_es_shrink(struct shrinker *shrink, struct shrink_control *sc)
896 struct ext4_sb_info *sbi = container_of(shrink,
897 struct ext4_sb_info, s_es_shrinker);
898 struct ext4_inode_info *ei;
899 struct list_head *cur, *tmp;
901 int nr_to_scan = sc->nr_to_scan;
902 int ret, nr_shrunk = 0;
904 ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
905 trace_ext4_es_shrink_enter(sbi->s_sb, nr_to_scan, ret);
910 spin_lock(&sbi->s_es_lru_lock);
913 * If the inode that is at the head of LRU list is newer than
914 * last_sorted time, that means that we need to sort this list.
916 ei = list_first_entry(&sbi->s_es_lru, struct ext4_inode_info, i_es_lru);
917 if (sbi->s_es_last_sorted < ei->i_touch_when) {
918 list_sort(NULL, &sbi->s_es_lru, ext4_inode_touch_time_cmp);
919 sbi->s_es_last_sorted = jiffies;
922 list_for_each_safe(cur, tmp, &sbi->s_es_lru) {
924 * If we have already reclaimed all extents from extent
925 * status tree, just stop the loop immediately.
927 if (percpu_counter_read_positive(&sbi->s_extent_cache_cnt) == 0)
930 ei = list_entry(cur, struct ext4_inode_info, i_es_lru);
932 /* Skip the inode that is newer than the last_sorted time */
933 if (sbi->s_es_last_sorted < ei->i_touch_when) {
934 list_move_tail(cur, &skiped);
938 if (ei->i_es_lru_nr == 0)
941 write_lock(&ei->i_es_lock);
942 ret = __es_try_to_reclaim_extents(ei, nr_to_scan);
943 if (ei->i_es_lru_nr == 0)
944 list_del_init(&ei->i_es_lru);
945 write_unlock(&ei->i_es_lock);
953 /* Move the newer inodes into the tail of the LRU list. */
954 list_splice_tail(&skiped, &sbi->s_es_lru);
955 spin_unlock(&sbi->s_es_lru_lock);
957 ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
958 trace_ext4_es_shrink_exit(sbi->s_sb, nr_shrunk, ret);
962 void ext4_es_register_shrinker(struct ext4_sb_info *sbi)
964 INIT_LIST_HEAD(&sbi->s_es_lru);
965 spin_lock_init(&sbi->s_es_lru_lock);
966 sbi->s_es_last_sorted = 0;
967 sbi->s_es_shrinker.shrink = ext4_es_shrink;
968 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
969 register_shrinker(&sbi->s_es_shrinker);
972 void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
974 unregister_shrinker(&sbi->s_es_shrinker);
977 void ext4_es_lru_add(struct inode *inode)
979 struct ext4_inode_info *ei = EXT4_I(inode);
980 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
982 ei->i_touch_when = jiffies;
984 if (!list_empty(&ei->i_es_lru))
987 spin_lock(&sbi->s_es_lru_lock);
988 if (list_empty(&ei->i_es_lru))
989 list_add_tail(&ei->i_es_lru, &sbi->s_es_lru);
990 spin_unlock(&sbi->s_es_lru_lock);
993 void ext4_es_lru_del(struct inode *inode)
995 struct ext4_inode_info *ei = EXT4_I(inode);
996 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
998 spin_lock(&sbi->s_es_lru_lock);
999 if (!list_empty(&ei->i_es_lru))
1000 list_del_init(&ei->i_es_lru);
1001 spin_unlock(&sbi->s_es_lru_lock);
1004 static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
1007 struct inode *inode = &ei->vfs_inode;
1008 struct ext4_es_tree *tree = &ei->i_es_tree;
1009 struct rb_node *node;
1010 struct extent_status *es;
1013 if (ei->i_es_lru_nr == 0)
1016 node = rb_first(&tree->root);
1017 while (node != NULL) {
1018 es = rb_entry(node, struct extent_status, rb_node);
1019 node = rb_next(&es->rb_node);
1021 * We can't reclaim delayed extent from status tree because
1022 * fiemap, bigallic, and seek_data/hole need to use it.
1024 if (!ext4_es_is_delayed(es)) {
1025 rb_erase(&es->rb_node, &tree->root);
1026 ext4_es_free_extent(inode, es);
1028 if (--nr_to_scan == 0)
1032 tree->cache_es = NULL;