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;
145 static struct kmem_cache *ext4_pending_cachep;
147 static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
148 struct extent_status *prealloc);
149 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
150 ext4_lblk_t end, int *reserved,
151 struct extent_status *prealloc);
152 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
153 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
154 struct ext4_inode_info *locked_ei);
155 static int __revise_pending(struct inode *inode, ext4_lblk_t lblk,
157 struct pending_reservation **prealloc);
159 int __init ext4_init_es(void)
161 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
162 sizeof(struct extent_status),
163 0, (SLAB_RECLAIM_ACCOUNT), NULL);
164 if (ext4_es_cachep == NULL)
169 void ext4_exit_es(void)
171 kmem_cache_destroy(ext4_es_cachep);
174 void ext4_es_init_tree(struct ext4_es_tree *tree)
176 tree->root = RB_ROOT;
177 tree->cache_es = NULL;
181 static void ext4_es_print_tree(struct inode *inode)
183 struct ext4_es_tree *tree;
184 struct rb_node *node;
186 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
187 tree = &EXT4_I(inode)->i_es_tree;
188 node = rb_first(&tree->root);
190 struct extent_status *es;
191 es = rb_entry(node, struct extent_status, rb_node);
192 printk(KERN_DEBUG " [%u/%u) %llu %x",
193 es->es_lblk, es->es_len,
194 ext4_es_pblock(es), ext4_es_status(es));
195 node = rb_next(node);
197 printk(KERN_DEBUG "\n");
200 #define ext4_es_print_tree(inode)
203 static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
205 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
206 return es->es_lblk + es->es_len - 1;
210 * search through the tree for an delayed extent with a given offset. If
211 * it can't be found, try to find next extent.
213 static struct extent_status *__es_tree_search(struct rb_root *root,
216 struct rb_node *node = root->rb_node;
217 struct extent_status *es = NULL;
220 es = rb_entry(node, struct extent_status, rb_node);
221 if (lblk < es->es_lblk)
222 node = node->rb_left;
223 else if (lblk > ext4_es_end(es))
224 node = node->rb_right;
229 if (es && lblk < es->es_lblk)
232 if (es && lblk > ext4_es_end(es)) {
233 node = rb_next(&es->rb_node);
234 return node ? rb_entry(node, struct extent_status, rb_node) :
242 * ext4_es_find_extent_range - find extent with specified status within block
243 * range or next extent following block range in
244 * extents status tree
246 * @inode - file containing the range
247 * @matching_fn - pointer to function that matches extents with desired status
248 * @lblk - logical block defining start of range
249 * @end - logical block defining end of range
250 * @es - extent found, if any
252 * Find the first extent within the block range specified by @lblk and @end
253 * in the extents status tree that satisfies @matching_fn. If a match
254 * is found, it's returned in @es. If not, and a matching extent is found
255 * beyond the block range, it's returned in @es. If no match is found, an
256 * extent is returned in @es whose es_lblk, es_len, and es_pblk components
259 static void __es_find_extent_range(struct inode *inode,
260 int (*matching_fn)(struct extent_status *es),
261 ext4_lblk_t lblk, ext4_lblk_t end,
262 struct extent_status *es)
264 struct ext4_es_tree *tree = NULL;
265 struct extent_status *es1 = NULL;
266 struct rb_node *node;
271 tree = &EXT4_I(inode)->i_es_tree;
273 /* see if the extent has been cached */
274 es->es_lblk = es->es_len = es->es_pblk = 0;
275 es1 = READ_ONCE(tree->cache_es);
276 if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
277 es_debug("%u cached by [%u/%u) %llu %x\n",
278 lblk, es1->es_lblk, es1->es_len,
279 ext4_es_pblock(es1), ext4_es_status(es1));
283 es1 = __es_tree_search(&tree->root, lblk);
286 if (es1 && !matching_fn(es1)) {
287 while ((node = rb_next(&es1->rb_node)) != NULL) {
288 es1 = rb_entry(node, struct extent_status, rb_node);
289 if (es1->es_lblk > end) {
293 if (matching_fn(es1))
298 if (es1 && matching_fn(es1)) {
299 WRITE_ONCE(tree->cache_es, es1);
300 es->es_lblk = es1->es_lblk;
301 es->es_len = es1->es_len;
302 es->es_pblk = es1->es_pblk;
308 * Locking for __es_find_extent_range() for external use
310 void ext4_es_find_extent_range(struct inode *inode,
311 int (*matching_fn)(struct extent_status *es),
312 ext4_lblk_t lblk, ext4_lblk_t end,
313 struct extent_status *es)
315 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
318 trace_ext4_es_find_extent_range_enter(inode, lblk);
320 read_lock(&EXT4_I(inode)->i_es_lock);
321 __es_find_extent_range(inode, matching_fn, lblk, end, es);
322 read_unlock(&EXT4_I(inode)->i_es_lock);
324 trace_ext4_es_find_extent_range_exit(inode, es);
328 * __es_scan_range - search block range for block with specified status
329 * in extents status tree
331 * @inode - file containing the range
332 * @matching_fn - pointer to function that matches extents with desired status
333 * @lblk - logical block defining start of range
334 * @end - logical block defining end of range
336 * Returns true if at least one block in the specified block range satisfies
337 * the criterion specified by @matching_fn, and false if not. If at least
338 * one extent has the specified status, then there is at least one block
339 * in the cluster with that status. Should only be called by code that has
342 static bool __es_scan_range(struct inode *inode,
343 int (*matching_fn)(struct extent_status *es),
344 ext4_lblk_t start, ext4_lblk_t end)
346 struct extent_status es;
348 __es_find_extent_range(inode, matching_fn, start, end, &es);
350 return false; /* no matching extent in the tree */
351 else if (es.es_lblk <= start &&
352 start < es.es_lblk + es.es_len)
354 else if (start <= es.es_lblk && es.es_lblk <= end)
360 * Locking for __es_scan_range() for external use
362 bool ext4_es_scan_range(struct inode *inode,
363 int (*matching_fn)(struct extent_status *es),
364 ext4_lblk_t lblk, ext4_lblk_t end)
368 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
371 read_lock(&EXT4_I(inode)->i_es_lock);
372 ret = __es_scan_range(inode, matching_fn, lblk, end);
373 read_unlock(&EXT4_I(inode)->i_es_lock);
379 * __es_scan_clu - search cluster for block with specified status in
380 * extents status tree
382 * @inode - file containing the cluster
383 * @matching_fn - pointer to function that matches extents with desired status
384 * @lblk - logical block in cluster to be searched
386 * Returns true if at least one extent in the cluster containing @lblk
387 * satisfies the criterion specified by @matching_fn, and false if not. If at
388 * least one extent has the specified status, then there is at least one block
389 * in the cluster with that status. Should only be called by code that has
392 static bool __es_scan_clu(struct inode *inode,
393 int (*matching_fn)(struct extent_status *es),
396 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
397 ext4_lblk_t lblk_start, lblk_end;
399 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
400 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
402 return __es_scan_range(inode, matching_fn, lblk_start, lblk_end);
406 * Locking for __es_scan_clu() for external use
408 bool ext4_es_scan_clu(struct inode *inode,
409 int (*matching_fn)(struct extent_status *es),
414 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
417 read_lock(&EXT4_I(inode)->i_es_lock);
418 ret = __es_scan_clu(inode, matching_fn, lblk);
419 read_unlock(&EXT4_I(inode)->i_es_lock);
424 static void ext4_es_list_add(struct inode *inode)
426 struct ext4_inode_info *ei = EXT4_I(inode);
427 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
429 if (!list_empty(&ei->i_es_list))
432 spin_lock(&sbi->s_es_lock);
433 if (list_empty(&ei->i_es_list)) {
434 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
435 sbi->s_es_nr_inode++;
437 spin_unlock(&sbi->s_es_lock);
440 static void ext4_es_list_del(struct inode *inode)
442 struct ext4_inode_info *ei = EXT4_I(inode);
443 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
445 spin_lock(&sbi->s_es_lock);
446 if (!list_empty(&ei->i_es_list)) {
447 list_del_init(&ei->i_es_list);
448 sbi->s_es_nr_inode--;
449 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
451 spin_unlock(&sbi->s_es_lock);
454 static inline struct pending_reservation *__alloc_pending(bool nofail)
457 return kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC);
459 return kmem_cache_zalloc(ext4_pending_cachep, GFP_KERNEL | __GFP_NOFAIL);
462 static inline void __free_pending(struct pending_reservation *pr)
464 kmem_cache_free(ext4_pending_cachep, pr);
468 * Returns true if we cannot fail to allocate memory for this extent_status
469 * entry and cannot reclaim it until its status changes.
471 static inline bool ext4_es_must_keep(struct extent_status *es)
473 /* fiemap, bigalloc, and seek_data/hole need to use it. */
474 if (ext4_es_is_delayed(es))
480 static inline struct extent_status *__es_alloc_extent(bool nofail)
483 return kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
485 return kmem_cache_zalloc(ext4_es_cachep, GFP_KERNEL | __GFP_NOFAIL);
488 static void ext4_es_init_extent(struct inode *inode, struct extent_status *es,
489 ext4_lblk_t lblk, ext4_lblk_t len, ext4_fsblk_t pblk)
495 /* We never try to reclaim a must kept extent, so we don't count it. */
496 if (!ext4_es_must_keep(es)) {
497 if (!EXT4_I(inode)->i_es_shk_nr++)
498 ext4_es_list_add(inode);
499 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
500 s_es_stats.es_stats_shk_cnt);
503 EXT4_I(inode)->i_es_all_nr++;
504 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
507 static inline void __es_free_extent(struct extent_status *es)
509 kmem_cache_free(ext4_es_cachep, es);
512 static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
514 EXT4_I(inode)->i_es_all_nr--;
515 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
517 /* Decrease the shrink counter when we can reclaim the extent. */
518 if (!ext4_es_must_keep(es)) {
519 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
520 if (!--EXT4_I(inode)->i_es_shk_nr)
521 ext4_es_list_del(inode);
522 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
523 s_es_stats.es_stats_shk_cnt);
526 __es_free_extent(es);
530 * Check whether or not two extents can be merged
532 * - logical block number is contiguous
533 * - physical block number is contiguous
536 static int ext4_es_can_be_merged(struct extent_status *es1,
537 struct extent_status *es2)
539 if (ext4_es_type(es1) != ext4_es_type(es2))
542 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
543 pr_warn("ES assertion failed when merging extents. "
544 "The sum of lengths of es1 (%d) and es2 (%d) "
545 "is bigger than allowed file size (%d)\n",
546 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
551 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
554 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
555 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
558 if (ext4_es_is_hole(es1))
561 /* we need to check delayed extent is without unwritten status */
562 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
568 static struct extent_status *
569 ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
571 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
572 struct extent_status *es1;
573 struct rb_node *node;
575 node = rb_prev(&es->rb_node);
579 es1 = rb_entry(node, struct extent_status, rb_node);
580 if (ext4_es_can_be_merged(es1, es)) {
581 es1->es_len += es->es_len;
582 if (ext4_es_is_referenced(es))
583 ext4_es_set_referenced(es1);
584 rb_erase(&es->rb_node, &tree->root);
585 ext4_es_free_extent(inode, es);
592 static struct extent_status *
593 ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
595 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
596 struct extent_status *es1;
597 struct rb_node *node;
599 node = rb_next(&es->rb_node);
603 es1 = rb_entry(node, struct extent_status, rb_node);
604 if (ext4_es_can_be_merged(es, es1)) {
605 es->es_len += es1->es_len;
606 if (ext4_es_is_referenced(es1))
607 ext4_es_set_referenced(es);
608 rb_erase(node, &tree->root);
609 ext4_es_free_extent(inode, es1);
615 #ifdef ES_AGGRESSIVE_TEST
616 #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
618 static void ext4_es_insert_extent_ext_check(struct inode *inode,
619 struct extent_status *es)
621 struct ext4_ext_path *path = NULL;
622 struct ext4_extent *ex;
623 ext4_lblk_t ee_block;
624 ext4_fsblk_t ee_start;
625 unsigned short ee_len;
626 int depth, ee_status, es_status;
628 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
632 depth = ext_depth(inode);
633 ex = path[depth].p_ext;
637 ee_block = le32_to_cpu(ex->ee_block);
638 ee_start = ext4_ext_pblock(ex);
639 ee_len = ext4_ext_get_actual_len(ex);
641 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
642 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
645 * Make sure ex and es are not overlap when we try to insert
646 * a delayed/hole extent.
648 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
649 if (in_range(es->es_lblk, ee_block, ee_len)) {
650 pr_warn("ES insert assertion failed for "
651 "inode: %lu we can find an extent "
652 "at block [%d/%d/%llu/%c], but we "
653 "want to add a delayed/hole extent "
655 inode->i_ino, ee_block, ee_len,
656 ee_start, ee_status ? 'u' : 'w',
657 es->es_lblk, es->es_len,
658 ext4_es_pblock(es), ext4_es_status(es));
664 * We don't check ee_block == es->es_lblk, etc. because es
665 * might be a part of whole extent, vice versa.
667 if (es->es_lblk < ee_block ||
668 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
669 pr_warn("ES insert assertion failed for inode: %lu "
670 "ex_status [%d/%d/%llu/%c] != "
671 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
672 ee_block, ee_len, ee_start,
673 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
674 ext4_es_pblock(es), es_status ? 'u' : 'w');
678 if (ee_status ^ es_status) {
679 pr_warn("ES insert assertion failed for inode: %lu "
680 "ex_status [%d/%d/%llu/%c] != "
681 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
682 ee_block, ee_len, ee_start,
683 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
684 ext4_es_pblock(es), es_status ? 'u' : 'w');
688 * We can't find an extent on disk. So we need to make sure
689 * that we don't want to add an written/unwritten extent.
691 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
692 pr_warn("ES insert assertion failed for inode: %lu "
693 "can't find an extent at block %d but we want "
694 "to add a written/unwritten extent "
695 "[%d/%d/%llu/%x]\n", inode->i_ino,
696 es->es_lblk, es->es_lblk, es->es_len,
697 ext4_es_pblock(es), ext4_es_status(es));
701 ext4_free_ext_path(path);
704 static void ext4_es_insert_extent_ind_check(struct inode *inode,
705 struct extent_status *es)
707 struct ext4_map_blocks map;
711 * Here we call ext4_ind_map_blocks to lookup a block mapping because
712 * 'Indirect' structure is defined in indirect.c. So we couldn't
713 * access direct/indirect tree from outside. It is too dirty to define
714 * this function in indirect.c file.
717 map.m_lblk = es->es_lblk;
718 map.m_len = es->es_len;
720 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
722 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
724 * We want to add a delayed/hole extent but this
725 * block has been allocated.
727 pr_warn("ES insert assertion failed for inode: %lu "
728 "We can find blocks but we want to add a "
729 "delayed/hole extent [%d/%d/%llu/%x]\n",
730 inode->i_ino, es->es_lblk, es->es_len,
731 ext4_es_pblock(es), ext4_es_status(es));
733 } else if (ext4_es_is_written(es)) {
734 if (retval != es->es_len) {
735 pr_warn("ES insert assertion failed for "
736 "inode: %lu retval %d != es_len %d\n",
737 inode->i_ino, retval, es->es_len);
740 if (map.m_pblk != ext4_es_pblock(es)) {
741 pr_warn("ES insert assertion failed for "
742 "inode: %lu m_pblk %llu != "
744 inode->i_ino, map.m_pblk,
750 * We don't need to check unwritten extent because
751 * indirect-based file doesn't have it.
755 } else if (retval == 0) {
756 if (ext4_es_is_written(es)) {
757 pr_warn("ES insert assertion failed for inode: %lu "
758 "We can't find the block but we want to add "
759 "a written extent [%d/%d/%llu/%x]\n",
760 inode->i_ino, es->es_lblk, es->es_len,
761 ext4_es_pblock(es), ext4_es_status(es));
767 static inline void ext4_es_insert_extent_check(struct inode *inode,
768 struct extent_status *es)
771 * We don't need to worry about the race condition because
772 * caller takes i_data_sem locking.
774 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
775 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
776 ext4_es_insert_extent_ext_check(inode, es);
778 ext4_es_insert_extent_ind_check(inode, es);
781 static inline void ext4_es_insert_extent_check(struct inode *inode,
782 struct extent_status *es)
787 static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
788 struct extent_status *prealloc)
790 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
791 struct rb_node **p = &tree->root.rb_node;
792 struct rb_node *parent = NULL;
793 struct extent_status *es;
797 es = rb_entry(parent, struct extent_status, rb_node);
799 if (newes->es_lblk < es->es_lblk) {
800 if (ext4_es_can_be_merged(newes, es)) {
802 * Here we can modify es_lblk directly
803 * because it isn't overlapped.
805 es->es_lblk = newes->es_lblk;
806 es->es_len += newes->es_len;
807 if (ext4_es_is_written(es) ||
808 ext4_es_is_unwritten(es))
809 ext4_es_store_pblock(es,
811 es = ext4_es_try_to_merge_left(inode, es);
815 } else if (newes->es_lblk > ext4_es_end(es)) {
816 if (ext4_es_can_be_merged(es, newes)) {
817 es->es_len += newes->es_len;
818 es = ext4_es_try_to_merge_right(inode, es);
831 es = __es_alloc_extent(false);
834 ext4_es_init_extent(inode, es, newes->es_lblk, newes->es_len,
837 rb_link_node(&es->rb_node, parent, p);
838 rb_insert_color(&es->rb_node, &tree->root);
846 * ext4_es_insert_extent() adds information to an inode's extent
849 * Return 0 on success, error code on failure.
851 int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
852 ext4_lblk_t len, ext4_fsblk_t pblk,
855 struct extent_status newes;
856 ext4_lblk_t end = lblk + len - 1;
857 int err1 = 0, err2 = 0, err3 = 0;
858 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
859 struct extent_status *es1 = NULL;
860 struct extent_status *es2 = NULL;
861 struct pending_reservation *pr = NULL;
862 bool revise_pending = false;
864 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
867 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
868 lblk, len, pblk, status, inode->i_ino);
875 if ((status & EXTENT_STATUS_DELAYED) &&
876 (status & EXTENT_STATUS_WRITTEN)) {
877 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
878 " delayed and written which can potentially "
879 " cause data loss.", lblk, len);
883 newes.es_lblk = lblk;
885 ext4_es_store_pblock_status(&newes, pblk, status);
886 trace_ext4_es_insert_extent(inode, &newes);
888 ext4_es_insert_extent_check(inode, &newes);
890 revise_pending = sbi->s_cluster_ratio > 1 &&
891 test_opt(inode->i_sb, DELALLOC) &&
892 (status & (EXTENT_STATUS_WRITTEN |
893 EXTENT_STATUS_UNWRITTEN));
896 es1 = __es_alloc_extent(true);
897 if ((err1 || err2) && !es2)
898 es2 = __es_alloc_extent(true);
899 if ((err1 || err2 || err3) && revise_pending && !pr)
900 pr = __alloc_pending(true);
901 write_lock(&EXT4_I(inode)->i_es_lock);
903 err1 = __es_remove_extent(inode, lblk, end, NULL, es1);
906 /* Free preallocated extent if it didn't get used. */
909 __es_free_extent(es1);
913 err2 = __es_insert_extent(inode, &newes, es2);
914 if (err2 == -ENOMEM && !ext4_es_must_keep(&newes))
918 /* Free preallocated extent if it didn't get used. */
921 __es_free_extent(es2);
925 if (revise_pending) {
926 err3 = __revise_pending(inode, lblk, len, &pr);
935 write_unlock(&EXT4_I(inode)->i_es_lock);
936 if (err1 || err2 || err3)
939 ext4_es_print_tree(inode);
944 * ext4_es_cache_extent() inserts information into the extent status
945 * tree if and only if there isn't information about the range in
948 void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
949 ext4_lblk_t len, ext4_fsblk_t pblk,
952 struct extent_status *es;
953 struct extent_status newes;
954 ext4_lblk_t end = lblk + len - 1;
956 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
959 newes.es_lblk = lblk;
961 ext4_es_store_pblock_status(&newes, pblk, status);
962 trace_ext4_es_cache_extent(inode, &newes);
969 write_lock(&EXT4_I(inode)->i_es_lock);
971 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
972 if (!es || es->es_lblk > end)
973 __es_insert_extent(inode, &newes, NULL);
974 write_unlock(&EXT4_I(inode)->i_es_lock);
978 * ext4_es_lookup_extent() looks up an extent in extent status tree.
980 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
982 * Return: 1 on found, 0 on not
984 int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
985 ext4_lblk_t *next_lblk,
986 struct extent_status *es)
988 struct ext4_es_tree *tree;
989 struct ext4_es_stats *stats;
990 struct extent_status *es1 = NULL;
991 struct rb_node *node;
994 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
997 trace_ext4_es_lookup_extent_enter(inode, lblk);
998 es_debug("lookup extent in block %u\n", lblk);
1000 tree = &EXT4_I(inode)->i_es_tree;
1001 read_lock(&EXT4_I(inode)->i_es_lock);
1003 /* find extent in cache firstly */
1004 es->es_lblk = es->es_len = es->es_pblk = 0;
1005 es1 = READ_ONCE(tree->cache_es);
1006 if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
1007 es_debug("%u cached by [%u/%u)\n",
1008 lblk, es1->es_lblk, es1->es_len);
1013 node = tree->root.rb_node;
1015 es1 = rb_entry(node, struct extent_status, rb_node);
1016 if (lblk < es1->es_lblk)
1017 node = node->rb_left;
1018 else if (lblk > ext4_es_end(es1))
1019 node = node->rb_right;
1027 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
1030 es->es_lblk = es1->es_lblk;
1031 es->es_len = es1->es_len;
1032 es->es_pblk = es1->es_pblk;
1033 if (!ext4_es_is_referenced(es1))
1034 ext4_es_set_referenced(es1);
1035 percpu_counter_inc(&stats->es_stats_cache_hits);
1037 node = rb_next(&es1->rb_node);
1039 es1 = rb_entry(node, struct extent_status,
1041 *next_lblk = es1->es_lblk;
1046 percpu_counter_inc(&stats->es_stats_cache_misses);
1049 read_unlock(&EXT4_I(inode)->i_es_lock);
1051 trace_ext4_es_lookup_extent_exit(inode, es, found);
1057 bool first_do_lblk_found;
1058 ext4_lblk_t first_do_lblk;
1059 ext4_lblk_t last_do_lblk;
1060 struct extent_status *left_es;
1066 * init_rsvd - initialize reserved count data before removing block range
1067 * in file from extent status tree
1069 * @inode - file containing range
1070 * @lblk - first block in range
1071 * @es - pointer to first extent in range
1072 * @rc - pointer to reserved count data
1074 * Assumes es is not NULL
1076 static void init_rsvd(struct inode *inode, ext4_lblk_t lblk,
1077 struct extent_status *es, struct rsvd_count *rc)
1079 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1080 struct rb_node *node;
1085 * for bigalloc, note the first delonly block in the range has not
1086 * been found, record the extent containing the block to the left of
1087 * the region to be removed, if any, and note that there's no partial
1090 if (sbi->s_cluster_ratio > 1) {
1091 rc->first_do_lblk_found = false;
1092 if (lblk > es->es_lblk) {
1095 node = rb_prev(&es->rb_node);
1096 rc->left_es = node ? rb_entry(node,
1097 struct extent_status,
1100 rc->partial = false;
1105 * count_rsvd - count the clusters containing delayed and not unwritten
1106 * (delonly) blocks in a range within an extent and add to
1107 * the running tally in rsvd_count
1109 * @inode - file containing extent
1110 * @lblk - first block in range
1111 * @len - length of range in blocks
1112 * @es - pointer to extent containing clusters to be counted
1113 * @rc - pointer to reserved count data
1115 * Tracks partial clusters found at the beginning and end of extents so
1116 * they aren't overcounted when they span adjacent extents
1118 static void count_rsvd(struct inode *inode, ext4_lblk_t lblk, long len,
1119 struct extent_status *es, struct rsvd_count *rc)
1121 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1122 ext4_lblk_t i, end, nclu;
1124 if (!ext4_es_is_delonly(es))
1129 if (sbi->s_cluster_ratio == 1) {
1130 rc->ndelonly += (int) len;
1136 i = (lblk < es->es_lblk) ? es->es_lblk : lblk;
1137 end = lblk + (ext4_lblk_t) len - 1;
1138 end = (end > ext4_es_end(es)) ? ext4_es_end(es) : end;
1140 /* record the first block of the first delonly extent seen */
1141 if (!rc->first_do_lblk_found) {
1142 rc->first_do_lblk = i;
1143 rc->first_do_lblk_found = true;
1146 /* update the last lblk in the region seen so far */
1147 rc->last_do_lblk = end;
1150 * if we're tracking a partial cluster and the current extent
1151 * doesn't start with it, count it and stop tracking
1153 if (rc->partial && (rc->lclu != EXT4_B2C(sbi, i))) {
1155 rc->partial = false;
1159 * if the first cluster doesn't start on a cluster boundary but
1160 * ends on one, count it
1162 if (EXT4_LBLK_COFF(sbi, i) != 0) {
1163 if (end >= EXT4_LBLK_CFILL(sbi, i)) {
1165 rc->partial = false;
1166 i = EXT4_LBLK_CFILL(sbi, i) + 1;
1171 * if the current cluster starts on a cluster boundary, count the
1172 * number of whole delonly clusters in the extent
1174 if ((i + sbi->s_cluster_ratio - 1) <= end) {
1175 nclu = (end - i + 1) >> sbi->s_cluster_bits;
1176 rc->ndelonly += nclu;
1177 i += nclu << sbi->s_cluster_bits;
1181 * start tracking a partial cluster if there's a partial at the end
1182 * of the current extent and we're not already tracking one
1184 if (!rc->partial && i <= end) {
1186 rc->lclu = EXT4_B2C(sbi, i);
1191 * __pr_tree_search - search for a pending cluster reservation
1193 * @root - root of pending reservation tree
1194 * @lclu - logical cluster to search for
1196 * Returns the pending reservation for the cluster identified by @lclu
1197 * if found. If not, returns a reservation for the next cluster if any,
1198 * and if not, returns NULL.
1200 static struct pending_reservation *__pr_tree_search(struct rb_root *root,
1203 struct rb_node *node = root->rb_node;
1204 struct pending_reservation *pr = NULL;
1207 pr = rb_entry(node, struct pending_reservation, rb_node);
1208 if (lclu < pr->lclu)
1209 node = node->rb_left;
1210 else if (lclu > pr->lclu)
1211 node = node->rb_right;
1215 if (pr && lclu < pr->lclu)
1217 if (pr && lclu > pr->lclu) {
1218 node = rb_next(&pr->rb_node);
1219 return node ? rb_entry(node, struct pending_reservation,
1226 * get_rsvd - calculates and returns the number of cluster reservations to be
1227 * released when removing a block range from the extent status tree
1228 * and releases any pending reservations within the range
1230 * @inode - file containing block range
1231 * @end - last block in range
1232 * @right_es - pointer to extent containing next block beyond end or NULL
1233 * @rc - pointer to reserved count data
1235 * The number of reservations to be released is equal to the number of
1236 * clusters containing delayed and not unwritten (delonly) blocks within
1237 * the range, minus the number of clusters still containing delonly blocks
1238 * at the ends of the range, and minus the number of pending reservations
1241 static unsigned int get_rsvd(struct inode *inode, ext4_lblk_t end,
1242 struct extent_status *right_es,
1243 struct rsvd_count *rc)
1245 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1246 struct pending_reservation *pr;
1247 struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1248 struct rb_node *node;
1249 ext4_lblk_t first_lclu, last_lclu;
1250 bool left_delonly, right_delonly, count_pending;
1251 struct extent_status *es;
1253 if (sbi->s_cluster_ratio > 1) {
1254 /* count any remaining partial cluster */
1258 if (rc->ndelonly == 0)
1261 first_lclu = EXT4_B2C(sbi, rc->first_do_lblk);
1262 last_lclu = EXT4_B2C(sbi, rc->last_do_lblk);
1265 * decrease the delonly count by the number of clusters at the
1266 * ends of the range that still contain delonly blocks -
1267 * these clusters still need to be reserved
1269 left_delonly = right_delonly = false;
1272 while (es && ext4_es_end(es) >=
1273 EXT4_LBLK_CMASK(sbi, rc->first_do_lblk)) {
1274 if (ext4_es_is_delonly(es)) {
1276 left_delonly = true;
1279 node = rb_prev(&es->rb_node);
1282 es = rb_entry(node, struct extent_status, rb_node);
1284 if (right_es && (!left_delonly || first_lclu != last_lclu)) {
1285 if (end < ext4_es_end(right_es)) {
1288 node = rb_next(&right_es->rb_node);
1289 es = node ? rb_entry(node, struct extent_status,
1292 while (es && es->es_lblk <=
1293 EXT4_LBLK_CFILL(sbi, rc->last_do_lblk)) {
1294 if (ext4_es_is_delonly(es)) {
1296 right_delonly = true;
1299 node = rb_next(&es->rb_node);
1302 es = rb_entry(node, struct extent_status,
1308 * Determine the block range that should be searched for
1309 * pending reservations, if any. Clusters on the ends of the
1310 * original removed range containing delonly blocks are
1311 * excluded. They've already been accounted for and it's not
1312 * possible to determine if an associated pending reservation
1313 * should be released with the information available in the
1314 * extents status tree.
1316 if (first_lclu == last_lclu) {
1317 if (left_delonly | right_delonly)
1318 count_pending = false;
1320 count_pending = true;
1326 if (first_lclu <= last_lclu)
1327 count_pending = true;
1329 count_pending = false;
1333 * a pending reservation found between first_lclu and last_lclu
1334 * represents an allocated cluster that contained at least one
1335 * delonly block, so the delonly total must be reduced by one
1336 * for each pending reservation found and released
1338 if (count_pending) {
1339 pr = __pr_tree_search(&tree->root, first_lclu);
1340 while (pr && pr->lclu <= last_lclu) {
1342 node = rb_next(&pr->rb_node);
1343 rb_erase(&pr->rb_node, &tree->root);
1347 pr = rb_entry(node, struct pending_reservation,
1352 return rc->ndelonly;
1357 * __es_remove_extent - removes block range from extent status tree
1359 * @inode - file containing range
1360 * @lblk - first block in range
1361 * @end - last block in range
1362 * @reserved - number of cluster reservations released
1363 * @prealloc - pre-allocated es to avoid memory allocation failures
1365 * If @reserved is not NULL and delayed allocation is enabled, counts
1366 * block/cluster reservations freed by removing range and if bigalloc
1367 * enabled cancels pending reservations as needed. Returns 0 on success,
1368 * error code on failure.
1370 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1371 ext4_lblk_t end, int *reserved,
1372 struct extent_status *prealloc)
1374 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
1375 struct rb_node *node;
1376 struct extent_status *es;
1377 struct extent_status orig_es;
1378 ext4_lblk_t len1, len2;
1381 bool count_reserved = true;
1382 struct rsvd_count rc;
1384 if (reserved == NULL || !test_opt(inode->i_sb, DELALLOC))
1385 count_reserved = false;
1387 es = __es_tree_search(&tree->root, lblk);
1390 if (es->es_lblk > end)
1393 /* Simply invalidate cache_es. */
1394 tree->cache_es = NULL;
1396 init_rsvd(inode, lblk, es, &rc);
1398 orig_es.es_lblk = es->es_lblk;
1399 orig_es.es_len = es->es_len;
1400 orig_es.es_pblk = es->es_pblk;
1402 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
1403 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
1408 struct extent_status newes;
1410 newes.es_lblk = end + 1;
1411 newes.es_len = len2;
1412 block = 0x7FDEADBEEFULL;
1413 if (ext4_es_is_written(&orig_es) ||
1414 ext4_es_is_unwritten(&orig_es))
1415 block = ext4_es_pblock(&orig_es) +
1416 orig_es.es_len - len2;
1417 ext4_es_store_pblock_status(&newes, block,
1418 ext4_es_status(&orig_es));
1419 err = __es_insert_extent(inode, &newes, prealloc);
1421 if (!ext4_es_must_keep(&newes))
1424 es->es_lblk = orig_es.es_lblk;
1425 es->es_len = orig_es.es_len;
1429 es->es_lblk = end + 1;
1431 if (ext4_es_is_written(es) ||
1432 ext4_es_is_unwritten(es)) {
1433 block = orig_es.es_pblk + orig_es.es_len - len2;
1434 ext4_es_store_pblock(es, block);
1438 count_rsvd(inode, orig_es.es_lblk + len1,
1439 orig_es.es_len - len1 - len2, &orig_es, &rc);
1440 goto out_get_reserved;
1445 count_rsvd(inode, lblk, orig_es.es_len - len1,
1447 node = rb_next(&es->rb_node);
1449 es = rb_entry(node, struct extent_status, rb_node);
1454 while (es && ext4_es_end(es) <= end) {
1456 count_rsvd(inode, es->es_lblk, es->es_len, es, &rc);
1457 node = rb_next(&es->rb_node);
1458 rb_erase(&es->rb_node, &tree->root);
1459 ext4_es_free_extent(inode, es);
1464 es = rb_entry(node, struct extent_status, rb_node);
1467 if (es && es->es_lblk < end + 1) {
1468 ext4_lblk_t orig_len = es->es_len;
1470 len1 = ext4_es_end(es) - end;
1472 count_rsvd(inode, es->es_lblk, orig_len - len1,
1474 es->es_lblk = end + 1;
1476 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
1477 block = es->es_pblk + orig_len - len1;
1478 ext4_es_store_pblock(es, block);
1484 *reserved = get_rsvd(inode, end, es, &rc);
1490 * ext4_es_remove_extent - removes block range from extent status tree
1492 * @inode - file containing range
1493 * @lblk - first block in range
1494 * @len - number of blocks to remove
1496 * Reduces block/cluster reservation count and for bigalloc cancels pending
1497 * reservations as needed. Returns 0 on success, error code on failure.
1499 int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1505 struct extent_status *es = NULL;
1507 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
1510 trace_ext4_es_remove_extent(inode, lblk, len);
1511 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
1512 lblk, len, inode->i_ino);
1517 end = lblk + len - 1;
1522 es = __es_alloc_extent(true);
1524 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
1525 * so that we are sure __es_shrink() is done with the inode before it
1528 write_lock(&EXT4_I(inode)->i_es_lock);
1529 err = __es_remove_extent(inode, lblk, end, &reserved, es);
1530 /* Free preallocated extent if it didn't get used. */
1533 __es_free_extent(es);
1536 write_unlock(&EXT4_I(inode)->i_es_lock);
1540 ext4_es_print_tree(inode);
1541 ext4_da_release_space(inode, reserved);
1545 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
1546 struct ext4_inode_info *locked_ei)
1548 struct ext4_inode_info *ei;
1549 struct ext4_es_stats *es_stats;
1554 int retried = 0, nr_skipped = 0;
1556 es_stats = &sbi->s_es_stats;
1557 start_time = ktime_get();
1560 spin_lock(&sbi->s_es_lock);
1561 nr_to_walk = sbi->s_es_nr_inode;
1562 while (nr_to_walk-- > 0) {
1563 if (list_empty(&sbi->s_es_list)) {
1564 spin_unlock(&sbi->s_es_lock);
1567 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
1569 /* Move the inode to the tail */
1570 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
1573 * Normally we try hard to avoid shrinking precached inodes,
1574 * but we will as a last resort.
1576 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1577 EXT4_STATE_EXT_PRECACHED)) {
1582 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1587 * Now we hold i_es_lock which protects us from inode reclaim
1588 * freeing inode under us
1590 spin_unlock(&sbi->s_es_lock);
1592 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
1593 write_unlock(&ei->i_es_lock);
1595 if (nr_to_scan <= 0)
1597 spin_lock(&sbi->s_es_lock);
1599 spin_unlock(&sbi->s_es_lock);
1602 * If we skipped any inodes, and we weren't able to make any
1603 * forward progress, try again to scan precached inodes.
1605 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1610 if (locked_ei && nr_shrunk == 0)
1611 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
1614 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1615 if (likely(es_stats->es_stats_scan_time))
1616 es_stats->es_stats_scan_time = (scan_time +
1617 es_stats->es_stats_scan_time*3) / 4;
1619 es_stats->es_stats_scan_time = scan_time;
1620 if (scan_time > es_stats->es_stats_max_scan_time)
1621 es_stats->es_stats_max_scan_time = scan_time;
1622 if (likely(es_stats->es_stats_shrunk))
1623 es_stats->es_stats_shrunk = (nr_shrunk +
1624 es_stats->es_stats_shrunk*3) / 4;
1626 es_stats->es_stats_shrunk = nr_shrunk;
1628 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
1629 nr_skipped, retried);
1633 static unsigned long ext4_es_count(struct shrinker *shrink,
1634 struct shrink_control *sc)
1637 struct ext4_sb_info *sbi;
1639 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
1640 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1641 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
1645 static unsigned long ext4_es_scan(struct shrinker *shrink,
1646 struct shrink_control *sc)
1648 struct ext4_sb_info *sbi = container_of(shrink,
1649 struct ext4_sb_info, s_es_shrinker);
1650 int nr_to_scan = sc->nr_to_scan;
1653 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1654 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
1656 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
1658 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1659 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
1663 int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
1665 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
1666 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1667 struct ext4_inode_info *ei, *max = NULL;
1668 unsigned int inode_cnt = 0;
1670 if (v != SEQ_START_TOKEN)
1673 /* here we just find an inode that has the max nr. of objects */
1674 spin_lock(&sbi->s_es_lock);
1675 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
1677 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1682 spin_unlock(&sbi->s_es_lock);
1684 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1685 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
1686 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
1687 seq_printf(seq, " %lld/%lld cache hits/misses\n",
1688 percpu_counter_sum_positive(&es_stats->es_stats_cache_hits),
1689 percpu_counter_sum_positive(&es_stats->es_stats_cache_misses));
1691 seq_printf(seq, " %d inodes on list\n", inode_cnt);
1693 seq_printf(seq, "average:\n %llu us scan time\n",
1694 div_u64(es_stats->es_stats_scan_time, 1000));
1695 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1698 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1699 " %llu us max scan time\n",
1700 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
1701 div_u64(es_stats->es_stats_max_scan_time, 1000));
1706 int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1710 /* Make sure we have enough bits for physical block number */
1711 BUILD_BUG_ON(ES_SHIFT < 48);
1712 INIT_LIST_HEAD(&sbi->s_es_list);
1713 sbi->s_es_nr_inode = 0;
1714 spin_lock_init(&sbi->s_es_lock);
1715 sbi->s_es_stats.es_stats_shrunk = 0;
1716 err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_hits, 0,
1720 err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_misses, 0,
1724 sbi->s_es_stats.es_stats_scan_time = 0;
1725 sbi->s_es_stats.es_stats_max_scan_time = 0;
1726 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
1729 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
1733 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1734 sbi->s_es_shrinker.count_objects = ext4_es_count;
1735 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
1736 err = register_shrinker(&sbi->s_es_shrinker, "ext4-es:%s",
1743 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1745 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1747 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1749 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1753 void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
1755 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1756 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1757 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1758 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1759 unregister_shrinker(&sbi->s_es_shrinker);
1763 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1764 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1766 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1767 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1768 * ei->i_es_shrink_lblk to where we should continue scanning.
1770 static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1771 int *nr_to_scan, int *nr_shrunk)
1773 struct inode *inode = &ei->vfs_inode;
1774 struct ext4_es_tree *tree = &ei->i_es_tree;
1775 struct extent_status *es;
1776 struct rb_node *node;
1778 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1782 while (*nr_to_scan > 0) {
1783 if (es->es_lblk > end) {
1784 ei->i_es_shrink_lblk = end + 1;
1789 node = rb_next(&es->rb_node);
1791 if (ext4_es_must_keep(es))
1793 if (ext4_es_is_referenced(es)) {
1794 ext4_es_clear_referenced(es);
1798 rb_erase(&es->rb_node, &tree->root);
1799 ext4_es_free_extent(inode, es);
1804 es = rb_entry(node, struct extent_status, rb_node);
1806 ei->i_es_shrink_lblk = es->es_lblk;
1809 ei->i_es_shrink_lblk = 0;
1813 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1815 struct inode *inode = &ei->vfs_inode;
1817 ext4_lblk_t start = ei->i_es_shrink_lblk;
1818 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1819 DEFAULT_RATELIMIT_BURST);
1821 if (ei->i_es_shk_nr == 0)
1824 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1826 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1828 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1830 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1832 ei->i_es_tree.cache_es = NULL;
1837 * Called to support EXT4_IOC_CLEAR_ES_CACHE. We can only remove
1838 * discretionary entries from the extent status cache. (Some entries
1839 * must be present for proper operations.)
1841 void ext4_clear_inode_es(struct inode *inode)
1843 struct ext4_inode_info *ei = EXT4_I(inode);
1844 struct extent_status *es;
1845 struct ext4_es_tree *tree;
1846 struct rb_node *node;
1848 write_lock(&ei->i_es_lock);
1849 tree = &EXT4_I(inode)->i_es_tree;
1850 tree->cache_es = NULL;
1851 node = rb_first(&tree->root);
1853 es = rb_entry(node, struct extent_status, rb_node);
1854 node = rb_next(node);
1855 if (!ext4_es_must_keep(es)) {
1856 rb_erase(&es->rb_node, &tree->root);
1857 ext4_es_free_extent(inode, es);
1860 ext4_clear_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
1861 write_unlock(&ei->i_es_lock);
1865 static void ext4_print_pending_tree(struct inode *inode)
1867 struct ext4_pending_tree *tree;
1868 struct rb_node *node;
1869 struct pending_reservation *pr;
1871 printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino);
1872 tree = &EXT4_I(inode)->i_pending_tree;
1873 node = rb_first(&tree->root);
1875 pr = rb_entry(node, struct pending_reservation, rb_node);
1876 printk(KERN_DEBUG " %u", pr->lclu);
1877 node = rb_next(node);
1879 printk(KERN_DEBUG "\n");
1882 #define ext4_print_pending_tree(inode)
1885 int __init ext4_init_pending(void)
1887 ext4_pending_cachep = kmem_cache_create("ext4_pending_reservation",
1888 sizeof(struct pending_reservation),
1889 0, (SLAB_RECLAIM_ACCOUNT), NULL);
1890 if (ext4_pending_cachep == NULL)
1895 void ext4_exit_pending(void)
1897 kmem_cache_destroy(ext4_pending_cachep);
1900 void ext4_init_pending_tree(struct ext4_pending_tree *tree)
1902 tree->root = RB_ROOT;
1906 * __get_pending - retrieve a pointer to a pending reservation
1908 * @inode - file containing the pending cluster reservation
1909 * @lclu - logical cluster of interest
1911 * Returns a pointer to a pending reservation if it's a member of
1912 * the set, and NULL if not. Must be called holding i_es_lock.
1914 static struct pending_reservation *__get_pending(struct inode *inode,
1917 struct ext4_pending_tree *tree;
1918 struct rb_node *node;
1919 struct pending_reservation *pr = NULL;
1921 tree = &EXT4_I(inode)->i_pending_tree;
1922 node = (&tree->root)->rb_node;
1925 pr = rb_entry(node, struct pending_reservation, rb_node);
1926 if (lclu < pr->lclu)
1927 node = node->rb_left;
1928 else if (lclu > pr->lclu)
1929 node = node->rb_right;
1930 else if (lclu == pr->lclu)
1937 * __insert_pending - adds a pending cluster reservation to the set of
1938 * pending reservations
1940 * @inode - file containing the cluster
1941 * @lblk - logical block in the cluster to be added
1942 * @prealloc - preallocated pending entry
1944 * Returns 0 on successful insertion and -ENOMEM on failure. If the
1945 * pending reservation is already in the set, returns successfully.
1947 static int __insert_pending(struct inode *inode, ext4_lblk_t lblk,
1948 struct pending_reservation **prealloc)
1950 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1951 struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1952 struct rb_node **p = &tree->root.rb_node;
1953 struct rb_node *parent = NULL;
1954 struct pending_reservation *pr;
1958 lclu = EXT4_B2C(sbi, lblk);
1959 /* search to find parent for insertion */
1962 pr = rb_entry(parent, struct pending_reservation, rb_node);
1964 if (lclu < pr->lclu) {
1966 } else if (lclu > pr->lclu) {
1967 p = &(*p)->rb_right;
1969 /* pending reservation already inserted */
1974 if (likely(*prealloc == NULL)) {
1975 pr = __alloc_pending(false);
1986 rb_link_node(&pr->rb_node, parent, p);
1987 rb_insert_color(&pr->rb_node, &tree->root);
1994 * __remove_pending - removes a pending cluster reservation from the set
1995 * of pending reservations
1997 * @inode - file containing the cluster
1998 * @lblk - logical block in the pending cluster reservation to be removed
2000 * Returns successfully if pending reservation is not a member of the set.
2002 static void __remove_pending(struct inode *inode, ext4_lblk_t lblk)
2004 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2005 struct pending_reservation *pr;
2006 struct ext4_pending_tree *tree;
2008 pr = __get_pending(inode, EXT4_B2C(sbi, lblk));
2010 tree = &EXT4_I(inode)->i_pending_tree;
2011 rb_erase(&pr->rb_node, &tree->root);
2017 * ext4_remove_pending - removes a pending cluster reservation from the set
2018 * of pending reservations
2020 * @inode - file containing the cluster
2021 * @lblk - logical block in the pending cluster reservation to be removed
2023 * Locking for external use of __remove_pending.
2025 void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk)
2027 struct ext4_inode_info *ei = EXT4_I(inode);
2029 write_lock(&ei->i_es_lock);
2030 __remove_pending(inode, lblk);
2031 write_unlock(&ei->i_es_lock);
2035 * ext4_is_pending - determine whether a cluster has a pending reservation
2038 * @inode - file containing the cluster
2039 * @lblk - logical block in the cluster
2041 * Returns true if there's a pending reservation for the cluster in the
2042 * set of pending reservations, and false if not.
2044 bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk)
2046 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2047 struct ext4_inode_info *ei = EXT4_I(inode);
2050 read_lock(&ei->i_es_lock);
2051 ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL);
2052 read_unlock(&ei->i_es_lock);
2058 * ext4_es_insert_delayed_block - adds a delayed block to the extents status
2059 * tree, adding a pending reservation where
2062 * @inode - file containing the newly added block
2063 * @lblk - logical block to be added
2064 * @allocated - indicates whether a physical cluster has been allocated for
2065 * the logical cluster that contains the block
2067 * Returns 0 on success, negative error code on failure.
2069 int ext4_es_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk,
2072 struct extent_status newes;
2073 int err1 = 0, err2 = 0, err3 = 0;
2074 struct extent_status *es1 = NULL;
2075 struct extent_status *es2 = NULL;
2076 struct pending_reservation *pr = NULL;
2078 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
2081 es_debug("add [%u/1) delayed to extent status tree of inode %lu\n",
2082 lblk, inode->i_ino);
2084 newes.es_lblk = lblk;
2086 ext4_es_store_pblock_status(&newes, ~0, EXTENT_STATUS_DELAYED);
2087 trace_ext4_es_insert_delayed_block(inode, &newes, allocated);
2089 ext4_es_insert_extent_check(inode, &newes);
2093 es1 = __es_alloc_extent(true);
2094 if ((err1 || err2) && !es2)
2095 es2 = __es_alloc_extent(true);
2096 if ((err1 || err2 || err3) && allocated && !pr)
2097 pr = __alloc_pending(true);
2098 write_lock(&EXT4_I(inode)->i_es_lock);
2100 err1 = __es_remove_extent(inode, lblk, lblk, NULL, es1);
2103 /* Free preallocated extent if it didn't get used. */
2106 __es_free_extent(es1);
2110 err2 = __es_insert_extent(inode, &newes, es2);
2113 /* Free preallocated extent if it didn't get used. */
2116 __es_free_extent(es2);
2121 err3 = __insert_pending(inode, lblk, &pr);
2130 write_unlock(&EXT4_I(inode)->i_es_lock);
2131 if (err1 || err2 || err3)
2134 ext4_es_print_tree(inode);
2135 ext4_print_pending_tree(inode);
2140 * __es_delayed_clu - count number of clusters containing blocks that
2143 * @inode - file containing block range
2144 * @start - logical block defining start of range
2145 * @end - logical block defining end of range
2147 * Returns the number of clusters containing only delayed (not delayed
2148 * and unwritten) blocks in the range specified by @start and @end. Any
2149 * cluster or part of a cluster within the range and containing a delayed
2150 * and not unwritten block within the range is counted as a whole cluster.
2152 static unsigned int __es_delayed_clu(struct inode *inode, ext4_lblk_t start,
2155 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
2156 struct extent_status *es;
2157 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2158 struct rb_node *node;
2159 ext4_lblk_t first_lclu, last_lclu;
2160 unsigned long long last_counted_lclu;
2163 /* guaranteed to be unequal to any ext4_lblk_t value */
2164 last_counted_lclu = ~0ULL;
2166 es = __es_tree_search(&tree->root, start);
2168 while (es && (es->es_lblk <= end)) {
2169 if (ext4_es_is_delonly(es)) {
2170 if (es->es_lblk <= start)
2171 first_lclu = EXT4_B2C(sbi, start);
2173 first_lclu = EXT4_B2C(sbi, es->es_lblk);
2175 if (ext4_es_end(es) >= end)
2176 last_lclu = EXT4_B2C(sbi, end);
2178 last_lclu = EXT4_B2C(sbi, ext4_es_end(es));
2180 if (first_lclu == last_counted_lclu)
2181 n += last_lclu - first_lclu;
2183 n += last_lclu - first_lclu + 1;
2184 last_counted_lclu = last_lclu;
2186 node = rb_next(&es->rb_node);
2189 es = rb_entry(node, struct extent_status, rb_node);
2196 * ext4_es_delayed_clu - count number of clusters containing blocks that
2197 * are both delayed and unwritten
2199 * @inode - file containing block range
2200 * @lblk - logical block defining start of range
2201 * @len - number of blocks in range
2203 * Locking for external use of __es_delayed_clu().
2205 unsigned int ext4_es_delayed_clu(struct inode *inode, ext4_lblk_t lblk,
2208 struct ext4_inode_info *ei = EXT4_I(inode);
2215 end = lblk + len - 1;
2216 WARN_ON(end < lblk);
2218 read_lock(&ei->i_es_lock);
2220 n = __es_delayed_clu(inode, lblk, end);
2222 read_unlock(&ei->i_es_lock);
2228 * __revise_pending - makes, cancels, or leaves unchanged pending cluster
2229 * reservations for a specified block range depending
2230 * upon the presence or absence of delayed blocks
2231 * outside the range within clusters at the ends of the
2234 * @inode - file containing the range
2235 * @lblk - logical block defining the start of range
2236 * @len - length of range in blocks
2237 * @prealloc - preallocated pending entry
2239 * Used after a newly allocated extent is added to the extents status tree.
2240 * Requires that the extents in the range have either written or unwritten
2241 * status. Must be called while holding i_es_lock.
2243 static int __revise_pending(struct inode *inode, ext4_lblk_t lblk,
2245 struct pending_reservation **prealloc)
2247 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2248 ext4_lblk_t end = lblk + len - 1;
2249 ext4_lblk_t first, last;
2250 bool f_del = false, l_del = false;
2257 * Two cases - block range within single cluster and block range
2258 * spanning two or more clusters. Note that a cluster belonging
2259 * to a range starting and/or ending on a cluster boundary is treated
2260 * as if it does not contain a delayed extent. The new range may
2261 * have allocated space for previously delayed blocks out to the
2262 * cluster boundary, requiring that any pre-existing pending
2263 * reservation be canceled. Because this code only looks at blocks
2264 * outside the range, it should revise pending reservations
2265 * correctly even if the extent represented by the range can't be
2266 * inserted in the extents status tree due to ENOSPC.
2269 if (EXT4_B2C(sbi, lblk) == EXT4_B2C(sbi, end)) {
2270 first = EXT4_LBLK_CMASK(sbi, lblk);
2272 f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2275 ret = __insert_pending(inode, first, prealloc);
2279 last = EXT4_LBLK_CMASK(sbi, end) +
2280 sbi->s_cluster_ratio - 1;
2282 l_del = __es_scan_range(inode,
2283 &ext4_es_is_delonly,
2286 ret = __insert_pending(inode, last, prealloc);
2290 __remove_pending(inode, last);
2293 first = EXT4_LBLK_CMASK(sbi, lblk);
2295 f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2298 ret = __insert_pending(inode, first, prealloc);
2302 __remove_pending(inode, first);
2304 last = EXT4_LBLK_CMASK(sbi, end) + sbi->s_cluster_ratio - 1;
2306 l_del = __es_scan_range(inode, &ext4_es_is_delonly,
2309 ret = __insert_pending(inode, last, prealloc);
2313 __remove_pending(inode, last);