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 static int __es_remove_extent(struct inode
*inode
, ext4_lblk_t lblk
,
150 static int es_reclaim_extents(struct ext4_inode_info
*ei
, int *nr_to_scan
);
151 static int __es_shrink(struct ext4_sb_info
*sbi
, int nr_to_scan
,
152 struct ext4_inode_info
*locked_ei
);
153 static void __revise_pending(struct inode
*inode
, ext4_lblk_t lblk
,
156 int __init
ext4_init_es(void)
158 ext4_es_cachep
= kmem_cache_create("ext4_extent_status",
159 sizeof(struct extent_status
),
160 0, (SLAB_RECLAIM_ACCOUNT
), NULL
);
161 if (ext4_es_cachep
== NULL
)
166 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_extent_range - find extent with specified status within block
240 * range or next extent following block range in
241 * extents status tree
243 * @inode - file containing the range
244 * @matching_fn - pointer to function that matches extents with desired status
245 * @lblk - logical block defining start of range
246 * @end - logical block defining end of range
247 * @es - extent found, if any
249 * Find the first extent within the block range specified by @lblk and @end
250 * in the extents status tree that satisfies @matching_fn. If a match
251 * is found, it's returned in @es. If not, and a matching extent is found
252 * beyond the block range, it's returned in @es. If no match is found, an
253 * extent is returned in @es whose es_lblk, es_len, and es_pblk components
256 static void __es_find_extent_range(struct inode
*inode
,
257 int (*matching_fn
)(struct extent_status
*es
),
258 ext4_lblk_t lblk
, ext4_lblk_t end
,
259 struct extent_status
*es
)
261 struct ext4_es_tree
*tree
= NULL
;
262 struct extent_status
*es1
= NULL
;
263 struct rb_node
*node
;
268 tree
= &EXT4_I(inode
)->i_es_tree
;
270 /* see if the extent has been cached */
271 es
->es_lblk
= es
->es_len
= es
->es_pblk
= 0;
272 if (tree
->cache_es
) {
273 es1
= tree
->cache_es
;
274 if (in_range(lblk
, es1
->es_lblk
, es1
->es_len
)) {
275 es_debug("%u cached by [%u/%u) %llu %x\n",
276 lblk
, es1
->es_lblk
, es1
->es_len
,
277 ext4_es_pblock(es1
), ext4_es_status(es1
));
282 es1
= __es_tree_search(&tree
->root
, lblk
);
285 if (es1
&& !matching_fn(es1
)) {
286 while ((node
= rb_next(&es1
->rb_node
)) != NULL
) {
287 es1
= rb_entry(node
, struct extent_status
, rb_node
);
288 if (es1
->es_lblk
> end
) {
292 if (matching_fn(es1
))
297 if (es1
&& matching_fn(es1
)) {
298 tree
->cache_es
= es1
;
299 es
->es_lblk
= es1
->es_lblk
;
300 es
->es_len
= es1
->es_len
;
301 es
->es_pblk
= es1
->es_pblk
;
307 * Locking for __es_find_extent_range() for external use
309 void ext4_es_find_extent_range(struct inode
*inode
,
310 int (*matching_fn
)(struct extent_status
*es
),
311 ext4_lblk_t lblk
, ext4_lblk_t end
,
312 struct extent_status
*es
)
314 trace_ext4_es_find_extent_range_enter(inode
, lblk
);
316 read_lock(&EXT4_I(inode
)->i_es_lock
);
317 __es_find_extent_range(inode
, matching_fn
, lblk
, end
, es
);
318 read_unlock(&EXT4_I(inode
)->i_es_lock
);
320 trace_ext4_es_find_extent_range_exit(inode
, es
);
324 * __es_scan_range - search block range for block with specified status
325 * in extents status tree
327 * @inode - file containing the range
328 * @matching_fn - pointer to function that matches extents with desired status
329 * @lblk - logical block defining start of range
330 * @end - logical block defining end of range
332 * Returns true if at least one block in the specified block range satisfies
333 * the criterion specified by @matching_fn, and false if not. If at least
334 * one extent has the specified status, then there is at least one block
335 * in the cluster with that status. Should only be called by code that has
338 static bool __es_scan_range(struct inode
*inode
,
339 int (*matching_fn
)(struct extent_status
*es
),
340 ext4_lblk_t start
, ext4_lblk_t end
)
342 struct extent_status es
;
344 __es_find_extent_range(inode
, matching_fn
, start
, end
, &es
);
346 return false; /* no matching extent in the tree */
347 else if (es
.es_lblk
<= start
&&
348 start
< es
.es_lblk
+ es
.es_len
)
350 else if (start
<= es
.es_lblk
&& es
.es_lblk
<= end
)
356 * Locking for __es_scan_range() for external use
358 bool ext4_es_scan_range(struct inode
*inode
,
359 int (*matching_fn
)(struct extent_status
*es
),
360 ext4_lblk_t lblk
, ext4_lblk_t end
)
364 read_lock(&EXT4_I(inode
)->i_es_lock
);
365 ret
= __es_scan_range(inode
, matching_fn
, lblk
, end
);
366 read_unlock(&EXT4_I(inode
)->i_es_lock
);
372 * __es_scan_clu - search cluster for block with specified status in
373 * extents status tree
375 * @inode - file containing the cluster
376 * @matching_fn - pointer to function that matches extents with desired status
377 * @lblk - logical block in cluster to be searched
379 * Returns true if at least one extent in the cluster containing @lblk
380 * satisfies the criterion specified by @matching_fn, and false if not. If at
381 * least one extent has the specified status, then there is at least one block
382 * in the cluster with that status. Should only be called by code that has
385 static bool __es_scan_clu(struct inode
*inode
,
386 int (*matching_fn
)(struct extent_status
*es
),
389 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
390 ext4_lblk_t lblk_start
, lblk_end
;
392 lblk_start
= EXT4_LBLK_CMASK(sbi
, lblk
);
393 lblk_end
= lblk_start
+ sbi
->s_cluster_ratio
- 1;
395 return __es_scan_range(inode
, matching_fn
, lblk_start
, lblk_end
);
399 * Locking for __es_scan_clu() for external use
401 bool ext4_es_scan_clu(struct inode
*inode
,
402 int (*matching_fn
)(struct extent_status
*es
),
407 read_lock(&EXT4_I(inode
)->i_es_lock
);
408 ret
= __es_scan_clu(inode
, matching_fn
, lblk
);
409 read_unlock(&EXT4_I(inode
)->i_es_lock
);
414 static void ext4_es_list_add(struct inode
*inode
)
416 struct ext4_inode_info
*ei
= EXT4_I(inode
);
417 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
419 if (!list_empty(&ei
->i_es_list
))
422 spin_lock(&sbi
->s_es_lock
);
423 if (list_empty(&ei
->i_es_list
)) {
424 list_add_tail(&ei
->i_es_list
, &sbi
->s_es_list
);
425 sbi
->s_es_nr_inode
++;
427 spin_unlock(&sbi
->s_es_lock
);
430 static void ext4_es_list_del(struct inode
*inode
)
432 struct ext4_inode_info
*ei
= EXT4_I(inode
);
433 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
435 spin_lock(&sbi
->s_es_lock
);
436 if (!list_empty(&ei
->i_es_list
)) {
437 list_del_init(&ei
->i_es_list
);
438 sbi
->s_es_nr_inode
--;
439 WARN_ON_ONCE(sbi
->s_es_nr_inode
< 0);
441 spin_unlock(&sbi
->s_es_lock
);
444 static struct extent_status
*
445 ext4_es_alloc_extent(struct inode
*inode
, ext4_lblk_t lblk
, ext4_lblk_t len
,
448 struct extent_status
*es
;
449 es
= kmem_cache_alloc(ext4_es_cachep
, GFP_ATOMIC
);
457 * We don't count delayed extent because we never try to reclaim them
459 if (!ext4_es_is_delayed(es
)) {
460 if (!EXT4_I(inode
)->i_es_shk_nr
++)
461 ext4_es_list_add(inode
);
462 percpu_counter_inc(&EXT4_SB(inode
->i_sb
)->
463 s_es_stats
.es_stats_shk_cnt
);
466 EXT4_I(inode
)->i_es_all_nr
++;
467 percpu_counter_inc(&EXT4_SB(inode
->i_sb
)->s_es_stats
.es_stats_all_cnt
);
472 static void ext4_es_free_extent(struct inode
*inode
, struct extent_status
*es
)
474 EXT4_I(inode
)->i_es_all_nr
--;
475 percpu_counter_dec(&EXT4_SB(inode
->i_sb
)->s_es_stats
.es_stats_all_cnt
);
477 /* Decrease the shrink counter when this es is not delayed */
478 if (!ext4_es_is_delayed(es
)) {
479 BUG_ON(EXT4_I(inode
)->i_es_shk_nr
== 0);
480 if (!--EXT4_I(inode
)->i_es_shk_nr
)
481 ext4_es_list_del(inode
);
482 percpu_counter_dec(&EXT4_SB(inode
->i_sb
)->
483 s_es_stats
.es_stats_shk_cnt
);
486 kmem_cache_free(ext4_es_cachep
, es
);
490 * Check whether or not two extents can be merged
492 * - logical block number is contiguous
493 * - physical block number is contiguous
496 static int ext4_es_can_be_merged(struct extent_status
*es1
,
497 struct extent_status
*es2
)
499 if (ext4_es_type(es1
) != ext4_es_type(es2
))
502 if (((__u64
) es1
->es_len
) + es2
->es_len
> EXT_MAX_BLOCKS
) {
503 pr_warn("ES assertion failed when merging extents. "
504 "The sum of lengths of es1 (%d) and es2 (%d) "
505 "is bigger than allowed file size (%d)\n",
506 es1
->es_len
, es2
->es_len
, EXT_MAX_BLOCKS
);
511 if (((__u64
) es1
->es_lblk
) + es1
->es_len
!= es2
->es_lblk
)
514 if ((ext4_es_is_written(es1
) || ext4_es_is_unwritten(es1
)) &&
515 (ext4_es_pblock(es1
) + es1
->es_len
== ext4_es_pblock(es2
)))
518 if (ext4_es_is_hole(es1
))
521 /* we need to check delayed extent is without unwritten status */
522 if (ext4_es_is_delayed(es1
) && !ext4_es_is_unwritten(es1
))
528 static struct extent_status
*
529 ext4_es_try_to_merge_left(struct inode
*inode
, struct extent_status
*es
)
531 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
532 struct extent_status
*es1
;
533 struct rb_node
*node
;
535 node
= rb_prev(&es
->rb_node
);
539 es1
= rb_entry(node
, struct extent_status
, rb_node
);
540 if (ext4_es_can_be_merged(es1
, es
)) {
541 es1
->es_len
+= es
->es_len
;
542 if (ext4_es_is_referenced(es
))
543 ext4_es_set_referenced(es1
);
544 rb_erase(&es
->rb_node
, &tree
->root
);
545 ext4_es_free_extent(inode
, es
);
552 static struct extent_status
*
553 ext4_es_try_to_merge_right(struct inode
*inode
, struct extent_status
*es
)
555 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
556 struct extent_status
*es1
;
557 struct rb_node
*node
;
559 node
= rb_next(&es
->rb_node
);
563 es1
= rb_entry(node
, struct extent_status
, rb_node
);
564 if (ext4_es_can_be_merged(es
, es1
)) {
565 es
->es_len
+= es1
->es_len
;
566 if (ext4_es_is_referenced(es1
))
567 ext4_es_set_referenced(es
);
568 rb_erase(node
, &tree
->root
);
569 ext4_es_free_extent(inode
, es1
);
575 #ifdef ES_AGGRESSIVE_TEST
576 #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
578 static void ext4_es_insert_extent_ext_check(struct inode
*inode
,
579 struct extent_status
*es
)
581 struct ext4_ext_path
*path
= NULL
;
582 struct ext4_extent
*ex
;
583 ext4_lblk_t ee_block
;
584 ext4_fsblk_t ee_start
;
585 unsigned short ee_len
;
586 int depth
, ee_status
, es_status
;
588 path
= ext4_find_extent(inode
, es
->es_lblk
, NULL
, EXT4_EX_NOCACHE
);
592 depth
= ext_depth(inode
);
593 ex
= path
[depth
].p_ext
;
597 ee_block
= le32_to_cpu(ex
->ee_block
);
598 ee_start
= ext4_ext_pblock(ex
);
599 ee_len
= ext4_ext_get_actual_len(ex
);
601 ee_status
= ext4_ext_is_unwritten(ex
) ? 1 : 0;
602 es_status
= ext4_es_is_unwritten(es
) ? 1 : 0;
605 * Make sure ex and es are not overlap when we try to insert
606 * a delayed/hole extent.
608 if (!ext4_es_is_written(es
) && !ext4_es_is_unwritten(es
)) {
609 if (in_range(es
->es_lblk
, ee_block
, ee_len
)) {
610 pr_warn("ES insert assertion failed for "
611 "inode: %lu we can find an extent "
612 "at block [%d/%d/%llu/%c], but we "
613 "want to add a delayed/hole extent "
615 inode
->i_ino
, ee_block
, ee_len
,
616 ee_start
, ee_status
? 'u' : 'w',
617 es
->es_lblk
, es
->es_len
,
618 ext4_es_pblock(es
), ext4_es_status(es
));
624 * We don't check ee_block == es->es_lblk, etc. because es
625 * might be a part of whole extent, vice versa.
627 if (es
->es_lblk
< ee_block
||
628 ext4_es_pblock(es
) != ee_start
+ es
->es_lblk
- ee_block
) {
629 pr_warn("ES insert assertion failed for inode: %lu "
630 "ex_status [%d/%d/%llu/%c] != "
631 "es_status [%d/%d/%llu/%c]\n", inode
->i_ino
,
632 ee_block
, ee_len
, ee_start
,
633 ee_status
? 'u' : 'w', es
->es_lblk
, es
->es_len
,
634 ext4_es_pblock(es
), es_status
? 'u' : 'w');
638 if (ee_status
^ es_status
) {
639 pr_warn("ES insert assertion failed for inode: %lu "
640 "ex_status [%d/%d/%llu/%c] != "
641 "es_status [%d/%d/%llu/%c]\n", inode
->i_ino
,
642 ee_block
, ee_len
, ee_start
,
643 ee_status
? 'u' : 'w', es
->es_lblk
, es
->es_len
,
644 ext4_es_pblock(es
), es_status
? 'u' : 'w');
648 * We can't find an extent on disk. So we need to make sure
649 * that we don't want to add an written/unwritten extent.
651 if (!ext4_es_is_delayed(es
) && !ext4_es_is_hole(es
)) {
652 pr_warn("ES insert assertion failed for inode: %lu "
653 "can't find an extent at block %d but we want "
654 "to add a written/unwritten extent "
655 "[%d/%d/%llu/%x]\n", inode
->i_ino
,
656 es
->es_lblk
, es
->es_lblk
, es
->es_len
,
657 ext4_es_pblock(es
), ext4_es_status(es
));
661 ext4_ext_drop_refs(path
);
665 static void ext4_es_insert_extent_ind_check(struct inode
*inode
,
666 struct extent_status
*es
)
668 struct ext4_map_blocks map
;
672 * Here we call ext4_ind_map_blocks to lookup a block mapping because
673 * 'Indirect' structure is defined in indirect.c. So we couldn't
674 * access direct/indirect tree from outside. It is too dirty to define
675 * this function in indirect.c file.
678 map
.m_lblk
= es
->es_lblk
;
679 map
.m_len
= es
->es_len
;
681 retval
= ext4_ind_map_blocks(NULL
, inode
, &map
, 0);
683 if (ext4_es_is_delayed(es
) || ext4_es_is_hole(es
)) {
685 * We want to add a delayed/hole extent but this
686 * block has been allocated.
688 pr_warn("ES insert assertion failed for inode: %lu "
689 "We can find blocks but we want to add a "
690 "delayed/hole extent [%d/%d/%llu/%x]\n",
691 inode
->i_ino
, es
->es_lblk
, es
->es_len
,
692 ext4_es_pblock(es
), ext4_es_status(es
));
694 } else if (ext4_es_is_written(es
)) {
695 if (retval
!= es
->es_len
) {
696 pr_warn("ES insert assertion failed for "
697 "inode: %lu retval %d != es_len %d\n",
698 inode
->i_ino
, retval
, es
->es_len
);
701 if (map
.m_pblk
!= ext4_es_pblock(es
)) {
702 pr_warn("ES insert assertion failed for "
703 "inode: %lu m_pblk %llu != "
705 inode
->i_ino
, map
.m_pblk
,
711 * We don't need to check unwritten extent because
712 * indirect-based file doesn't have it.
716 } else if (retval
== 0) {
717 if (ext4_es_is_written(es
)) {
718 pr_warn("ES insert assertion failed for inode: %lu "
719 "We can't find the block but we want to add "
720 "a written extent [%d/%d/%llu/%x]\n",
721 inode
->i_ino
, es
->es_lblk
, es
->es_len
,
722 ext4_es_pblock(es
), ext4_es_status(es
));
728 static inline void ext4_es_insert_extent_check(struct inode
*inode
,
729 struct extent_status
*es
)
732 * We don't need to worry about the race condition because
733 * caller takes i_data_sem locking.
735 BUG_ON(!rwsem_is_locked(&EXT4_I(inode
)->i_data_sem
));
736 if (ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
737 ext4_es_insert_extent_ext_check(inode
, es
);
739 ext4_es_insert_extent_ind_check(inode
, es
);
742 static inline void ext4_es_insert_extent_check(struct inode
*inode
,
743 struct extent_status
*es
)
748 static int __es_insert_extent(struct inode
*inode
, struct extent_status
*newes
)
750 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
751 struct rb_node
**p
= &tree
->root
.rb_node
;
752 struct rb_node
*parent
= NULL
;
753 struct extent_status
*es
;
757 es
= rb_entry(parent
, struct extent_status
, rb_node
);
759 if (newes
->es_lblk
< es
->es_lblk
) {
760 if (ext4_es_can_be_merged(newes
, es
)) {
762 * Here we can modify es_lblk directly
763 * because it isn't overlapped.
765 es
->es_lblk
= newes
->es_lblk
;
766 es
->es_len
+= newes
->es_len
;
767 if (ext4_es_is_written(es
) ||
768 ext4_es_is_unwritten(es
))
769 ext4_es_store_pblock(es
,
771 es
= ext4_es_try_to_merge_left(inode
, es
);
775 } else if (newes
->es_lblk
> ext4_es_end(es
)) {
776 if (ext4_es_can_be_merged(es
, newes
)) {
777 es
->es_len
+= newes
->es_len
;
778 es
= ext4_es_try_to_merge_right(inode
, es
);
788 es
= ext4_es_alloc_extent(inode
, newes
->es_lblk
, newes
->es_len
,
792 rb_link_node(&es
->rb_node
, parent
, p
);
793 rb_insert_color(&es
->rb_node
, &tree
->root
);
801 * ext4_es_insert_extent() adds information to an inode's extent
804 * Return 0 on success, error code on failure.
806 int ext4_es_insert_extent(struct inode
*inode
, ext4_lblk_t lblk
,
807 ext4_lblk_t len
, ext4_fsblk_t pblk
,
810 struct extent_status newes
;
811 ext4_lblk_t end
= lblk
+ len
- 1;
813 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
815 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
816 lblk
, len
, pblk
, status
, inode
->i_ino
);
823 if ((status
& EXTENT_STATUS_DELAYED
) &&
824 (status
& EXTENT_STATUS_WRITTEN
)) {
825 ext4_warning(inode
->i_sb
, "Inserting extent [%u/%u] as "
826 " delayed and written which can potentially "
827 " cause data loss.", lblk
, len
);
831 newes
.es_lblk
= lblk
;
833 ext4_es_store_pblock_status(&newes
, pblk
, status
);
834 trace_ext4_es_insert_extent(inode
, &newes
);
836 ext4_es_insert_extent_check(inode
, &newes
);
838 write_lock(&EXT4_I(inode
)->i_es_lock
);
839 err
= __es_remove_extent(inode
, lblk
, end
);
843 err
= __es_insert_extent(inode
, &newes
);
844 if (err
== -ENOMEM
&& __es_shrink(EXT4_SB(inode
->i_sb
),
847 if (err
== -ENOMEM
&& !ext4_es_is_delayed(&newes
))
850 if (sbi
->s_cluster_ratio
> 1 && test_opt(inode
->i_sb
, DELALLOC
) &&
851 (status
& EXTENT_STATUS_WRITTEN
||
852 status
& EXTENT_STATUS_UNWRITTEN
))
853 __revise_pending(inode
, lblk
, len
);
856 write_unlock(&EXT4_I(inode
)->i_es_lock
);
858 ext4_es_print_tree(inode
);
864 * ext4_es_cache_extent() inserts information into the extent status
865 * tree if and only if there isn't information about the range in
868 void ext4_es_cache_extent(struct inode
*inode
, ext4_lblk_t lblk
,
869 ext4_lblk_t len
, ext4_fsblk_t pblk
,
872 struct extent_status
*es
;
873 struct extent_status newes
;
874 ext4_lblk_t end
= lblk
+ len
- 1;
876 newes
.es_lblk
= lblk
;
878 ext4_es_store_pblock_status(&newes
, pblk
, status
);
879 trace_ext4_es_cache_extent(inode
, &newes
);
886 write_lock(&EXT4_I(inode
)->i_es_lock
);
888 es
= __es_tree_search(&EXT4_I(inode
)->i_es_tree
.root
, lblk
);
889 if (!es
|| es
->es_lblk
> end
)
890 __es_insert_extent(inode
, &newes
);
891 write_unlock(&EXT4_I(inode
)->i_es_lock
);
895 * ext4_es_lookup_extent() looks up an extent in extent status tree.
897 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
899 * Return: 1 on found, 0 on not
901 int ext4_es_lookup_extent(struct inode
*inode
, ext4_lblk_t lblk
,
902 struct extent_status
*es
)
904 struct ext4_es_tree
*tree
;
905 struct ext4_es_stats
*stats
;
906 struct extent_status
*es1
= NULL
;
907 struct rb_node
*node
;
910 trace_ext4_es_lookup_extent_enter(inode
, lblk
);
911 es_debug("lookup extent in block %u\n", lblk
);
913 tree
= &EXT4_I(inode
)->i_es_tree
;
914 read_lock(&EXT4_I(inode
)->i_es_lock
);
916 /* find extent in cache firstly */
917 es
->es_lblk
= es
->es_len
= es
->es_pblk
= 0;
918 if (tree
->cache_es
) {
919 es1
= tree
->cache_es
;
920 if (in_range(lblk
, es1
->es_lblk
, es1
->es_len
)) {
921 es_debug("%u cached by [%u/%u)\n",
922 lblk
, es1
->es_lblk
, es1
->es_len
);
928 node
= tree
->root
.rb_node
;
930 es1
= rb_entry(node
, struct extent_status
, rb_node
);
931 if (lblk
< es1
->es_lblk
)
932 node
= node
->rb_left
;
933 else if (lblk
> ext4_es_end(es1
))
934 node
= node
->rb_right
;
942 stats
= &EXT4_SB(inode
->i_sb
)->s_es_stats
;
945 es
->es_lblk
= es1
->es_lblk
;
946 es
->es_len
= es1
->es_len
;
947 es
->es_pblk
= es1
->es_pblk
;
948 if (!ext4_es_is_referenced(es1
))
949 ext4_es_set_referenced(es1
);
950 stats
->es_stats_cache_hits
++;
952 stats
->es_stats_cache_misses
++;
955 read_unlock(&EXT4_I(inode
)->i_es_lock
);
957 trace_ext4_es_lookup_extent_exit(inode
, es
, found
);
961 static int __es_remove_extent(struct inode
*inode
, ext4_lblk_t lblk
,
964 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
965 struct rb_node
*node
;
966 struct extent_status
*es
;
967 struct extent_status orig_es
;
968 ext4_lblk_t len1
, len2
;
974 es
= __es_tree_search(&tree
->root
, lblk
);
977 if (es
->es_lblk
> end
)
980 /* Simply invalidate cache_es. */
981 tree
->cache_es
= NULL
;
983 orig_es
.es_lblk
= es
->es_lblk
;
984 orig_es
.es_len
= es
->es_len
;
985 orig_es
.es_pblk
= es
->es_pblk
;
987 len1
= lblk
> es
->es_lblk
? lblk
- es
->es_lblk
: 0;
988 len2
= ext4_es_end(es
) > end
? ext4_es_end(es
) - end
: 0;
993 struct extent_status newes
;
995 newes
.es_lblk
= end
+ 1;
997 block
= 0x7FDEADBEEFULL
;
998 if (ext4_es_is_written(&orig_es
) ||
999 ext4_es_is_unwritten(&orig_es
))
1000 block
= ext4_es_pblock(&orig_es
) +
1001 orig_es
.es_len
- len2
;
1002 ext4_es_store_pblock_status(&newes
, block
,
1003 ext4_es_status(&orig_es
));
1004 err
= __es_insert_extent(inode
, &newes
);
1006 es
->es_lblk
= orig_es
.es_lblk
;
1007 es
->es_len
= orig_es
.es_len
;
1008 if ((err
== -ENOMEM
) &&
1009 __es_shrink(EXT4_SB(inode
->i_sb
),
1010 128, EXT4_I(inode
)))
1015 es
->es_lblk
= end
+ 1;
1017 if (ext4_es_is_written(es
) ||
1018 ext4_es_is_unwritten(es
)) {
1019 block
= orig_es
.es_pblk
+ orig_es
.es_len
- len2
;
1020 ext4_es_store_pblock(es
, block
);
1027 node
= rb_next(&es
->rb_node
);
1029 es
= rb_entry(node
, struct extent_status
, rb_node
);
1034 while (es
&& ext4_es_end(es
) <= end
) {
1035 node
= rb_next(&es
->rb_node
);
1036 rb_erase(&es
->rb_node
, &tree
->root
);
1037 ext4_es_free_extent(inode
, es
);
1042 es
= rb_entry(node
, struct extent_status
, rb_node
);
1045 if (es
&& es
->es_lblk
< end
+ 1) {
1046 ext4_lblk_t orig_len
= es
->es_len
;
1048 len1
= ext4_es_end(es
) - end
;
1049 es
->es_lblk
= end
+ 1;
1051 if (ext4_es_is_written(es
) || ext4_es_is_unwritten(es
)) {
1052 block
= es
->es_pblk
+ orig_len
- len1
;
1053 ext4_es_store_pblock(es
, block
);
1062 * ext4_es_remove_extent() removes a space from a extent status tree.
1064 * Return 0 on success, error code on failure.
1066 int ext4_es_remove_extent(struct inode
*inode
, ext4_lblk_t lblk
,
1072 trace_ext4_es_remove_extent(inode
, lblk
, len
);
1073 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
1074 lblk
, len
, inode
->i_ino
);
1079 end
= lblk
+ len
- 1;
1083 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
1084 * so that we are sure __es_shrink() is done with the inode before it
1087 write_lock(&EXT4_I(inode
)->i_es_lock
);
1088 err
= __es_remove_extent(inode
, lblk
, end
);
1089 write_unlock(&EXT4_I(inode
)->i_es_lock
);
1090 ext4_es_print_tree(inode
);
1094 static int __es_shrink(struct ext4_sb_info
*sbi
, int nr_to_scan
,
1095 struct ext4_inode_info
*locked_ei
)
1097 struct ext4_inode_info
*ei
;
1098 struct ext4_es_stats
*es_stats
;
1103 int retried
= 0, nr_skipped
= 0;
1105 es_stats
= &sbi
->s_es_stats
;
1106 start_time
= ktime_get();
1109 spin_lock(&sbi
->s_es_lock
);
1110 nr_to_walk
= sbi
->s_es_nr_inode
;
1111 while (nr_to_walk
-- > 0) {
1112 if (list_empty(&sbi
->s_es_list
)) {
1113 spin_unlock(&sbi
->s_es_lock
);
1116 ei
= list_first_entry(&sbi
->s_es_list
, struct ext4_inode_info
,
1118 /* Move the inode to the tail */
1119 list_move_tail(&ei
->i_es_list
, &sbi
->s_es_list
);
1122 * Normally we try hard to avoid shrinking precached inodes,
1123 * but we will as a last resort.
1125 if (!retried
&& ext4_test_inode_state(&ei
->vfs_inode
,
1126 EXT4_STATE_EXT_PRECACHED
)) {
1131 if (ei
== locked_ei
|| !write_trylock(&ei
->i_es_lock
)) {
1136 * Now we hold i_es_lock which protects us from inode reclaim
1137 * freeing inode under us
1139 spin_unlock(&sbi
->s_es_lock
);
1141 nr_shrunk
+= es_reclaim_extents(ei
, &nr_to_scan
);
1142 write_unlock(&ei
->i_es_lock
);
1144 if (nr_to_scan
<= 0)
1146 spin_lock(&sbi
->s_es_lock
);
1148 spin_unlock(&sbi
->s_es_lock
);
1151 * If we skipped any inodes, and we weren't able to make any
1152 * forward progress, try again to scan precached inodes.
1154 if ((nr_shrunk
== 0) && nr_skipped
&& !retried
) {
1159 if (locked_ei
&& nr_shrunk
== 0)
1160 nr_shrunk
= es_reclaim_extents(locked_ei
, &nr_to_scan
);
1163 scan_time
= ktime_to_ns(ktime_sub(ktime_get(), start_time
));
1164 if (likely(es_stats
->es_stats_scan_time
))
1165 es_stats
->es_stats_scan_time
= (scan_time
+
1166 es_stats
->es_stats_scan_time
*3) / 4;
1168 es_stats
->es_stats_scan_time
= scan_time
;
1169 if (scan_time
> es_stats
->es_stats_max_scan_time
)
1170 es_stats
->es_stats_max_scan_time
= scan_time
;
1171 if (likely(es_stats
->es_stats_shrunk
))
1172 es_stats
->es_stats_shrunk
= (nr_shrunk
+
1173 es_stats
->es_stats_shrunk
*3) / 4;
1175 es_stats
->es_stats_shrunk
= nr_shrunk
;
1177 trace_ext4_es_shrink(sbi
->s_sb
, nr_shrunk
, scan_time
,
1178 nr_skipped
, retried
);
1182 static unsigned long ext4_es_count(struct shrinker
*shrink
,
1183 struct shrink_control
*sc
)
1186 struct ext4_sb_info
*sbi
;
1188 sbi
= container_of(shrink
, struct ext4_sb_info
, s_es_shrinker
);
1189 nr
= percpu_counter_read_positive(&sbi
->s_es_stats
.es_stats_shk_cnt
);
1190 trace_ext4_es_shrink_count(sbi
->s_sb
, sc
->nr_to_scan
, nr
);
1194 static unsigned long ext4_es_scan(struct shrinker
*shrink
,
1195 struct shrink_control
*sc
)
1197 struct ext4_sb_info
*sbi
= container_of(shrink
,
1198 struct ext4_sb_info
, s_es_shrinker
);
1199 int nr_to_scan
= sc
->nr_to_scan
;
1202 ret
= percpu_counter_read_positive(&sbi
->s_es_stats
.es_stats_shk_cnt
);
1203 trace_ext4_es_shrink_scan_enter(sbi
->s_sb
, nr_to_scan
, ret
);
1208 nr_shrunk
= __es_shrink(sbi
, nr_to_scan
, NULL
);
1210 trace_ext4_es_shrink_scan_exit(sbi
->s_sb
, nr_shrunk
, ret
);
1214 int ext4_seq_es_shrinker_info_show(struct seq_file
*seq
, void *v
)
1216 struct ext4_sb_info
*sbi
= EXT4_SB((struct super_block
*) seq
->private);
1217 struct ext4_es_stats
*es_stats
= &sbi
->s_es_stats
;
1218 struct ext4_inode_info
*ei
, *max
= NULL
;
1219 unsigned int inode_cnt
= 0;
1221 if (v
!= SEQ_START_TOKEN
)
1224 /* here we just find an inode that has the max nr. of objects */
1225 spin_lock(&sbi
->s_es_lock
);
1226 list_for_each_entry(ei
, &sbi
->s_es_list
, i_es_list
) {
1228 if (max
&& max
->i_es_all_nr
< ei
->i_es_all_nr
)
1233 spin_unlock(&sbi
->s_es_lock
);
1235 seq_printf(seq
, "stats:\n %lld objects\n %lld reclaimable objects\n",
1236 percpu_counter_sum_positive(&es_stats
->es_stats_all_cnt
),
1237 percpu_counter_sum_positive(&es_stats
->es_stats_shk_cnt
));
1238 seq_printf(seq
, " %lu/%lu cache hits/misses\n",
1239 es_stats
->es_stats_cache_hits
,
1240 es_stats
->es_stats_cache_misses
);
1242 seq_printf(seq
, " %d inodes on list\n", inode_cnt
);
1244 seq_printf(seq
, "average:\n %llu us scan time\n",
1245 div_u64(es_stats
->es_stats_scan_time
, 1000));
1246 seq_printf(seq
, " %lu shrunk objects\n", es_stats
->es_stats_shrunk
);
1249 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1250 " %llu us max scan time\n",
1251 max
->vfs_inode
.i_ino
, max
->i_es_all_nr
, max
->i_es_shk_nr
,
1252 div_u64(es_stats
->es_stats_max_scan_time
, 1000));
1257 int ext4_es_register_shrinker(struct ext4_sb_info
*sbi
)
1261 /* Make sure we have enough bits for physical block number */
1262 BUILD_BUG_ON(ES_SHIFT
< 48);
1263 INIT_LIST_HEAD(&sbi
->s_es_list
);
1264 sbi
->s_es_nr_inode
= 0;
1265 spin_lock_init(&sbi
->s_es_lock
);
1266 sbi
->s_es_stats
.es_stats_shrunk
= 0;
1267 sbi
->s_es_stats
.es_stats_cache_hits
= 0;
1268 sbi
->s_es_stats
.es_stats_cache_misses
= 0;
1269 sbi
->s_es_stats
.es_stats_scan_time
= 0;
1270 sbi
->s_es_stats
.es_stats_max_scan_time
= 0;
1271 err
= percpu_counter_init(&sbi
->s_es_stats
.es_stats_all_cnt
, 0, GFP_KERNEL
);
1274 err
= percpu_counter_init(&sbi
->s_es_stats
.es_stats_shk_cnt
, 0, GFP_KERNEL
);
1278 sbi
->s_es_shrinker
.scan_objects
= ext4_es_scan
;
1279 sbi
->s_es_shrinker
.count_objects
= ext4_es_count
;
1280 sbi
->s_es_shrinker
.seeks
= DEFAULT_SEEKS
;
1281 err
= register_shrinker(&sbi
->s_es_shrinker
);
1288 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_shk_cnt
);
1290 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_all_cnt
);
1294 void ext4_es_unregister_shrinker(struct ext4_sb_info
*sbi
)
1296 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_all_cnt
);
1297 percpu_counter_destroy(&sbi
->s_es_stats
.es_stats_shk_cnt
);
1298 unregister_shrinker(&sbi
->s_es_shrinker
);
1302 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1303 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1305 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1306 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1307 * ei->i_es_shrink_lblk to where we should continue scanning.
1309 static int es_do_reclaim_extents(struct ext4_inode_info
*ei
, ext4_lblk_t end
,
1310 int *nr_to_scan
, int *nr_shrunk
)
1312 struct inode
*inode
= &ei
->vfs_inode
;
1313 struct ext4_es_tree
*tree
= &ei
->i_es_tree
;
1314 struct extent_status
*es
;
1315 struct rb_node
*node
;
1317 es
= __es_tree_search(&tree
->root
, ei
->i_es_shrink_lblk
);
1320 node
= &es
->rb_node
;
1321 while (*nr_to_scan
> 0) {
1322 if (es
->es_lblk
> end
) {
1323 ei
->i_es_shrink_lblk
= end
+ 1;
1328 node
= rb_next(&es
->rb_node
);
1330 * We can't reclaim delayed extent from status tree because
1331 * fiemap, bigallic, and seek_data/hole need to use it.
1333 if (ext4_es_is_delayed(es
))
1335 if (ext4_es_is_referenced(es
)) {
1336 ext4_es_clear_referenced(es
);
1340 rb_erase(&es
->rb_node
, &tree
->root
);
1341 ext4_es_free_extent(inode
, es
);
1346 es
= rb_entry(node
, struct extent_status
, rb_node
);
1348 ei
->i_es_shrink_lblk
= es
->es_lblk
;
1351 ei
->i_es_shrink_lblk
= 0;
1355 static int es_reclaim_extents(struct ext4_inode_info
*ei
, int *nr_to_scan
)
1357 struct inode
*inode
= &ei
->vfs_inode
;
1359 ext4_lblk_t start
= ei
->i_es_shrink_lblk
;
1360 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
1361 DEFAULT_RATELIMIT_BURST
);
1363 if (ei
->i_es_shk_nr
== 0)
1366 if (ext4_test_inode_state(inode
, EXT4_STATE_EXT_PRECACHED
) &&
1368 ext4_warning(inode
->i_sb
, "forced shrink of precached extents");
1370 if (!es_do_reclaim_extents(ei
, EXT_MAX_BLOCKS
, nr_to_scan
, &nr_shrunk
) &&
1372 es_do_reclaim_extents(ei
, start
- 1, nr_to_scan
, &nr_shrunk
);
1374 ei
->i_es_tree
.cache_es
= NULL
;
1379 static void ext4_print_pending_tree(struct inode
*inode
)
1381 struct ext4_pending_tree
*tree
;
1382 struct rb_node
*node
;
1383 struct pending_reservation
*pr
;
1385 printk(KERN_DEBUG
"pending reservations for inode %lu:", inode
->i_ino
);
1386 tree
= &EXT4_I(inode
)->i_pending_tree
;
1387 node
= rb_first(&tree
->root
);
1389 pr
= rb_entry(node
, struct pending_reservation
, rb_node
);
1390 printk(KERN_DEBUG
" %u", pr
->lclu
);
1391 node
= rb_next(node
);
1393 printk(KERN_DEBUG
"\n");
1396 #define ext4_print_pending_tree(inode)
1399 int __init
ext4_init_pending(void)
1401 ext4_pending_cachep
= kmem_cache_create("ext4_pending_reservation",
1402 sizeof(struct pending_reservation
),
1403 0, (SLAB_RECLAIM_ACCOUNT
), NULL
);
1404 if (ext4_pending_cachep
== NULL
)
1409 void ext4_exit_pending(void)
1411 kmem_cache_destroy(ext4_pending_cachep
);
1414 void ext4_init_pending_tree(struct ext4_pending_tree
*tree
)
1416 tree
->root
= RB_ROOT
;
1420 * __get_pending - retrieve a pointer to a pending reservation
1422 * @inode - file containing the pending cluster reservation
1423 * @lclu - logical cluster of interest
1425 * Returns a pointer to a pending reservation if it's a member of
1426 * the set, and NULL if not. Must be called holding i_es_lock.
1428 static struct pending_reservation
*__get_pending(struct inode
*inode
,
1431 struct ext4_pending_tree
*tree
;
1432 struct rb_node
*node
;
1433 struct pending_reservation
*pr
= NULL
;
1435 tree
= &EXT4_I(inode
)->i_pending_tree
;
1436 node
= (&tree
->root
)->rb_node
;
1439 pr
= rb_entry(node
, struct pending_reservation
, rb_node
);
1440 if (lclu
< pr
->lclu
)
1441 node
= node
->rb_left
;
1442 else if (lclu
> pr
->lclu
)
1443 node
= node
->rb_right
;
1444 else if (lclu
== pr
->lclu
)
1451 * __insert_pending - adds a pending cluster reservation to the set of
1452 * pending reservations
1454 * @inode - file containing the cluster
1455 * @lblk - logical block in the cluster to be added
1457 * Returns 0 on successful insertion and -ENOMEM on failure. If the
1458 * pending reservation is already in the set, returns successfully.
1460 static int __insert_pending(struct inode
*inode
, ext4_lblk_t lblk
)
1462 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1463 struct ext4_pending_tree
*tree
= &EXT4_I(inode
)->i_pending_tree
;
1464 struct rb_node
**p
= &tree
->root
.rb_node
;
1465 struct rb_node
*parent
= NULL
;
1466 struct pending_reservation
*pr
;
1470 lclu
= EXT4_B2C(sbi
, lblk
);
1471 /* search to find parent for insertion */
1474 pr
= rb_entry(parent
, struct pending_reservation
, rb_node
);
1476 if (lclu
< pr
->lclu
) {
1478 } else if (lclu
> pr
->lclu
) {
1479 p
= &(*p
)->rb_right
;
1481 /* pending reservation already inserted */
1486 pr
= kmem_cache_alloc(ext4_pending_cachep
, GFP_ATOMIC
);
1493 rb_link_node(&pr
->rb_node
, parent
, p
);
1494 rb_insert_color(&pr
->rb_node
, &tree
->root
);
1501 * __remove_pending - removes a pending cluster reservation from the set
1502 * of pending reservations
1504 * @inode - file containing the cluster
1505 * @lblk - logical block in the pending cluster reservation to be removed
1507 * Returns successfully if pending reservation is not a member of the set.
1509 static void __remove_pending(struct inode
*inode
, ext4_lblk_t lblk
)
1511 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1512 struct pending_reservation
*pr
;
1513 struct ext4_pending_tree
*tree
;
1515 pr
= __get_pending(inode
, EXT4_B2C(sbi
, lblk
));
1517 tree
= &EXT4_I(inode
)->i_pending_tree
;
1518 rb_erase(&pr
->rb_node
, &tree
->root
);
1519 kmem_cache_free(ext4_pending_cachep
, pr
);
1524 * ext4_remove_pending - removes a pending cluster reservation from the set
1525 * of pending reservations
1527 * @inode - file containing the cluster
1528 * @lblk - logical block in the pending cluster reservation to be removed
1530 * Locking for external use of __remove_pending.
1532 void ext4_remove_pending(struct inode
*inode
, ext4_lblk_t lblk
)
1534 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1536 write_lock(&ei
->i_es_lock
);
1537 __remove_pending(inode
, lblk
);
1538 write_unlock(&ei
->i_es_lock
);
1542 * ext4_is_pending - determine whether a cluster has a pending reservation
1545 * @inode - file containing the cluster
1546 * @lblk - logical block in the cluster
1548 * Returns true if there's a pending reservation for the cluster in the
1549 * set of pending reservations, and false if not.
1551 bool ext4_is_pending(struct inode
*inode
, ext4_lblk_t lblk
)
1553 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1554 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1557 read_lock(&ei
->i_es_lock
);
1558 ret
= (bool)(__get_pending(inode
, EXT4_B2C(sbi
, lblk
)) != NULL
);
1559 read_unlock(&ei
->i_es_lock
);
1565 * ext4_es_insert_delayed_block - adds a delayed block to the extents status
1566 * tree, adding a pending reservation where
1569 * @inode - file containing the newly added block
1570 * @lblk - logical block to be added
1571 * @allocated - indicates whether a physical cluster has been allocated for
1572 * the logical cluster that contains the block
1574 * Returns 0 on success, negative error code on failure.
1576 int ext4_es_insert_delayed_block(struct inode
*inode
, ext4_lblk_t lblk
,
1579 struct extent_status newes
;
1582 es_debug("add [%u/1) delayed to extent status tree of inode %lu\n",
1583 lblk
, inode
->i_ino
);
1585 newes
.es_lblk
= lblk
;
1587 ext4_es_store_pblock_status(&newes
, ~0, EXTENT_STATUS_DELAYED
);
1588 trace_ext4_es_insert_delayed_block(inode
, &newes
, allocated
);
1590 ext4_es_insert_extent_check(inode
, &newes
);
1592 write_lock(&EXT4_I(inode
)->i_es_lock
);
1594 err
= __es_remove_extent(inode
, lblk
, lblk
);
1598 err
= __es_insert_extent(inode
, &newes
);
1599 if (err
== -ENOMEM
&& __es_shrink(EXT4_SB(inode
->i_sb
),
1600 128, EXT4_I(inode
)))
1606 __insert_pending(inode
, lblk
);
1609 write_unlock(&EXT4_I(inode
)->i_es_lock
);
1611 ext4_es_print_tree(inode
);
1612 ext4_print_pending_tree(inode
);
1618 * __es_delayed_clu - count number of clusters containing blocks that
1621 * @inode - file containing block range
1622 * @start - logical block defining start of range
1623 * @end - logical block defining end of range
1625 * Returns the number of clusters containing only delayed (not delayed
1626 * and unwritten) blocks in the range specified by @start and @end. Any
1627 * cluster or part of a cluster within the range and containing a delayed
1628 * and not unwritten block within the range is counted as a whole cluster.
1630 static unsigned int __es_delayed_clu(struct inode
*inode
, ext4_lblk_t start
,
1633 struct ext4_es_tree
*tree
= &EXT4_I(inode
)->i_es_tree
;
1634 struct extent_status
*es
;
1635 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1636 struct rb_node
*node
;
1637 ext4_lblk_t first_lclu
, last_lclu
;
1638 unsigned long long last_counted_lclu
;
1641 /* guaranteed to be unequal to any ext4_lblk_t value */
1642 last_counted_lclu
= ~0ULL;
1644 es
= __es_tree_search(&tree
->root
, start
);
1646 while (es
&& (es
->es_lblk
<= end
)) {
1647 if (ext4_es_is_delonly(es
)) {
1648 if (es
->es_lblk
<= start
)
1649 first_lclu
= EXT4_B2C(sbi
, start
);
1651 first_lclu
= EXT4_B2C(sbi
, es
->es_lblk
);
1653 if (ext4_es_end(es
) >= end
)
1654 last_lclu
= EXT4_B2C(sbi
, end
);
1656 last_lclu
= EXT4_B2C(sbi
, ext4_es_end(es
));
1658 if (first_lclu
== last_counted_lclu
)
1659 n
+= last_lclu
- first_lclu
;
1661 n
+= last_lclu
- first_lclu
+ 1;
1662 last_counted_lclu
= last_lclu
;
1664 node
= rb_next(&es
->rb_node
);
1667 es
= rb_entry(node
, struct extent_status
, rb_node
);
1674 * ext4_es_delayed_clu - count number of clusters containing blocks that
1675 * are both delayed and unwritten
1677 * @inode - file containing block range
1678 * @lblk - logical block defining start of range
1679 * @len - number of blocks in range
1681 * Locking for external use of __es_delayed_clu().
1683 unsigned int ext4_es_delayed_clu(struct inode
*inode
, ext4_lblk_t lblk
,
1686 struct ext4_inode_info
*ei
= EXT4_I(inode
);
1693 end
= lblk
+ len
- 1;
1694 WARN_ON(end
< lblk
);
1696 read_lock(&ei
->i_es_lock
);
1698 n
= __es_delayed_clu(inode
, lblk
, end
);
1700 read_unlock(&ei
->i_es_lock
);
1706 * __revise_pending - makes, cancels, or leaves unchanged pending cluster
1707 * reservations for a specified block range depending
1708 * upon the presence or absence of delayed blocks
1709 * outside the range within clusters at the ends of the
1712 * @inode - file containing the range
1713 * @lblk - logical block defining the start of range
1714 * @len - length of range in blocks
1716 * Used after a newly allocated extent is added to the extents status tree.
1717 * Requires that the extents in the range have either written or unwritten
1718 * status. Must be called while holding i_es_lock.
1720 static void __revise_pending(struct inode
*inode
, ext4_lblk_t lblk
,
1723 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1724 ext4_lblk_t end
= lblk
+ len
- 1;
1725 ext4_lblk_t first
, last
;
1726 bool f_del
= false, l_del
= false;
1732 * Two cases - block range within single cluster and block range
1733 * spanning two or more clusters. Note that a cluster belonging
1734 * to a range starting and/or ending on a cluster boundary is treated
1735 * as if it does not contain a delayed extent. The new range may
1736 * have allocated space for previously delayed blocks out to the
1737 * cluster boundary, requiring that any pre-existing pending
1738 * reservation be canceled. Because this code only looks at blocks
1739 * outside the range, it should revise pending reservations
1740 * correctly even if the extent represented by the range can't be
1741 * inserted in the extents status tree due to ENOSPC.
1744 if (EXT4_B2C(sbi
, lblk
) == EXT4_B2C(sbi
, end
)) {
1745 first
= EXT4_LBLK_CMASK(sbi
, lblk
);
1747 f_del
= __es_scan_range(inode
, &ext4_es_is_delonly
,
1750 __insert_pending(inode
, first
);
1752 last
= EXT4_LBLK_CMASK(sbi
, end
) +
1753 sbi
->s_cluster_ratio
- 1;
1755 l_del
= __es_scan_range(inode
,
1756 &ext4_es_is_delonly
,
1759 __insert_pending(inode
, last
);
1761 __remove_pending(inode
, last
);
1764 first
= EXT4_LBLK_CMASK(sbi
, lblk
);
1766 f_del
= __es_scan_range(inode
, &ext4_es_is_delonly
,
1769 __insert_pending(inode
, first
);
1771 __remove_pending(inode
, first
);
1773 last
= EXT4_LBLK_CMASK(sbi
, end
) + sbi
->s_cluster_ratio
- 1;
1775 l_del
= __es_scan_range(inode
, &ext4_es_is_delonly
,
1778 __insert_pending(inode
, last
);
1780 __remove_pending(inode
, last
);
1785 * ext4_es_remove_blks - remove block range from extents status tree and
1786 * reduce reservation count or cancel pending
1787 * reservation as needed
1789 * @inode - file containing range
1790 * @lblk - first block in range
1791 * @len - number of blocks to remove
1794 void ext4_es_remove_blks(struct inode
*inode
, ext4_lblk_t lblk
,
1797 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1798 unsigned int clu_size
, reserved
= 0;
1799 ext4_lblk_t last_lclu
, first
, length
, remainder
, last
;
1802 struct pending_reservation
*pr
;
1803 struct ext4_pending_tree
*tree
;
1806 * Process cluster by cluster for bigalloc - there may be up to
1807 * two clusters in a 4k page with a 1k block size and two blocks
1808 * per cluster. Also necessary for systems with larger page sizes
1809 * and potentially larger block sizes.
1811 clu_size
= sbi
->s_cluster_ratio
;
1812 last_lclu
= EXT4_B2C(sbi
, lblk
+ len
- 1);
1814 write_lock(&EXT4_I(inode
)->i_es_lock
);
1816 for (first
= lblk
, remainder
= len
;
1818 first
+= length
, remainder
-= length
) {
1820 if (EXT4_B2C(sbi
, first
) == last_lclu
)
1823 length
= clu_size
- EXT4_LBLK_COFF(sbi
, first
);
1826 * The BH_Delay flag, which triggers calls to this function,
1827 * and the contents of the extents status tree can be
1828 * inconsistent due to writepages activity. So, note whether
1829 * the blocks to be removed actually belong to an extent with
1830 * delayed only status.
1832 delonly
= __es_scan_clu(inode
, &ext4_es_is_delonly
, first
);
1835 * because of the writepages effect, written and unwritten
1836 * blocks could be removed here
1838 last
= first
+ length
- 1;
1839 err
= __es_remove_extent(inode
, first
, last
);
1841 ext4_warning(inode
->i_sb
,
1842 "%s: couldn't remove page (err = %d)",
1845 /* non-bigalloc case: simply count the cluster for release */
1846 if (sbi
->s_cluster_ratio
== 1 && delonly
) {
1852 * bigalloc case: if all delayed allocated only blocks have
1853 * just been removed from a cluster, either cancel a pending
1854 * reservation if it exists or count a cluster for release
1857 !__es_scan_clu(inode
, &ext4_es_is_delonly
, first
)) {
1858 pr
= __get_pending(inode
, EXT4_B2C(sbi
, first
));
1860 tree
= &EXT4_I(inode
)->i_pending_tree
;
1861 rb_erase(&pr
->rb_node
, &tree
->root
);
1862 kmem_cache_free(ext4_pending_cachep
, pr
);
1869 write_unlock(&EXT4_I(inode
)->i_es_lock
);
1871 ext4_da_release_space(inode
, reserved
);