xref: /openbmc/linux/fs/ext4/extents_status.c (revision 66c98360)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  fs/ext4/extents_status.c
4  *
5  * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
6  * Modified by
7  *	Allison Henderson <achender@linux.vnet.ibm.com>
8  *	Hugh Dickins <hughd@google.com>
9  *	Zheng Liu <wenqing.lz@taobao.com>
10  *
11  * Ext4 extents status tree core functions.
12  */
13 #include <linux/list_sort.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include "ext4.h"
17 
18 #include <trace/events/ext4.h>
19 
20 /*
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.
32  *
33  * Step1:
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.
39  *
40  * The following comment describes the implemenmtation of extent
41  * status tree and future works.
42  *
43  * Step2:
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.
55  */
56 
57 /*
58  * Extent status tree implementation for ext4.
59  *
60  *
61  * ==========================================================================
62  * Extent status tree tracks all extent status.
63  *
64  * 1. Why we need to implement extent status tree?
65  *
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.
69  *
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.
72  *
73  * Let us have a look at how they do without extent status tree.
74  *   --	FIEMAP
75  *	FIEMAP looks up page cache to identify delayed allocations from holes.
76  *
77  *   --	SEEK_HOLE/DATA
78  *	SEEK_HOLE/DATA has the same problem as FIEMAP.
79  *
80  *   --	bigalloc
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.
84  *
85  *   --	writeout
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
88  *	time consuming.
89  *
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.
94  *
95  *
96  * ==========================================================================
97  * 2. Ext4 extent status tree impelmentation
98  *
99  *   --	extent
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.
105  *
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.
110  *
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.
114  *
115  *   --	race on a extent status tree
116  *	Extent status tree is protected by inode->i_es_lock.
117  *
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.
122  *
123  *
124  * ==========================================================================
125  * 3. Performance analysis
126  *
127  *   --	overhead
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.
130  *
131  *   --	gain
132  *	2. Code is much simpler, more readable, more maintainable and
133  *	more efficient.
134  *
135  *
136  * ==========================================================================
137  * 4. TODO list
138  *
139  *   -- Refactor delayed space reservation
140  *
141  *   -- Extent-level locking
142  */
143 
144 static struct kmem_cache *ext4_es_cachep;
145 static struct kmem_cache *ext4_pending_cachep;
146 
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,
149 			      ext4_lblk_t end, int *reserved);
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,
154 			     ext4_lblk_t len);
155 
156 int __init ext4_init_es(void)
157 {
158 	ext4_es_cachep = KMEM_CACHE(extent_status, SLAB_RECLAIM_ACCOUNT);
159 	if (ext4_es_cachep == NULL)
160 		return -ENOMEM;
161 	return 0;
162 }
163 
164 void ext4_exit_es(void)
165 {
166 	kmem_cache_destroy(ext4_es_cachep);
167 }
168 
169 void ext4_es_init_tree(struct ext4_es_tree *tree)
170 {
171 	tree->root = RB_ROOT;
172 	tree->cache_es = NULL;
173 }
174 
175 #ifdef ES_DEBUG__
176 static void ext4_es_print_tree(struct inode *inode)
177 {
178 	struct ext4_es_tree *tree;
179 	struct rb_node *node;
180 
181 	printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
182 	tree = &EXT4_I(inode)->i_es_tree;
183 	node = rb_first(&tree->root);
184 	while (node) {
185 		struct extent_status *es;
186 		es = rb_entry(node, struct extent_status, rb_node);
187 		printk(KERN_DEBUG " [%u/%u) %llu %x",
188 		       es->es_lblk, es->es_len,
189 		       ext4_es_pblock(es), ext4_es_status(es));
190 		node = rb_next(node);
191 	}
192 	printk(KERN_DEBUG "\n");
193 }
194 #else
195 #define ext4_es_print_tree(inode)
196 #endif
197 
198 static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
199 {
200 	BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
201 	return es->es_lblk + es->es_len - 1;
202 }
203 
204 /*
205  * search through the tree for an delayed extent with a given offset.  If
206  * it can't be found, try to find next extent.
207  */
208 static struct extent_status *__es_tree_search(struct rb_root *root,
209 					      ext4_lblk_t lblk)
210 {
211 	struct rb_node *node = root->rb_node;
212 	struct extent_status *es = NULL;
213 
214 	while (node) {
215 		es = rb_entry(node, struct extent_status, rb_node);
216 		if (lblk < es->es_lblk)
217 			node = node->rb_left;
218 		else if (lblk > ext4_es_end(es))
219 			node = node->rb_right;
220 		else
221 			return es;
222 	}
223 
224 	if (es && lblk < es->es_lblk)
225 		return es;
226 
227 	if (es && lblk > ext4_es_end(es)) {
228 		node = rb_next(&es->rb_node);
229 		return node ? rb_entry(node, struct extent_status, rb_node) :
230 			      NULL;
231 	}
232 
233 	return NULL;
234 }
235 
236 /*
237  * ext4_es_find_extent_range - find extent with specified status within block
238  *                             range or next extent following block range in
239  *                             extents status tree
240  *
241  * @inode - file containing the range
242  * @matching_fn - pointer to function that matches extents with desired status
243  * @lblk - logical block defining start of range
244  * @end - logical block defining end of range
245  * @es - extent found, if any
246  *
247  * Find the first extent within the block range specified by @lblk and @end
248  * in the extents status tree that satisfies @matching_fn.  If a match
249  * is found, it's returned in @es.  If not, and a matching extent is found
250  * beyond the block range, it's returned in @es.  If no match is found, an
251  * extent is returned in @es whose es_lblk, es_len, and es_pblk components
252  * are 0.
253  */
254 static void __es_find_extent_range(struct inode *inode,
255 				   int (*matching_fn)(struct extent_status *es),
256 				   ext4_lblk_t lblk, ext4_lblk_t end,
257 				   struct extent_status *es)
258 {
259 	struct ext4_es_tree *tree = NULL;
260 	struct extent_status *es1 = NULL;
261 	struct rb_node *node;
262 
263 	WARN_ON(es == NULL);
264 	WARN_ON(end < lblk);
265 
266 	tree = &EXT4_I(inode)->i_es_tree;
267 
268 	/* see if the extent has been cached */
269 	es->es_lblk = es->es_len = es->es_pblk = 0;
270 	es1 = READ_ONCE(tree->cache_es);
271 	if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
272 		es_debug("%u cached by [%u/%u) %llu %x\n",
273 			 lblk, es1->es_lblk, es1->es_len,
274 			 ext4_es_pblock(es1), ext4_es_status(es1));
275 		goto out;
276 	}
277 
278 	es1 = __es_tree_search(&tree->root, lblk);
279 
280 out:
281 	if (es1 && !matching_fn(es1)) {
282 		while ((node = rb_next(&es1->rb_node)) != NULL) {
283 			es1 = rb_entry(node, struct extent_status, rb_node);
284 			if (es1->es_lblk > end) {
285 				es1 = NULL;
286 				break;
287 			}
288 			if (matching_fn(es1))
289 				break;
290 		}
291 	}
292 
293 	if (es1 && matching_fn(es1)) {
294 		WRITE_ONCE(tree->cache_es, es1);
295 		es->es_lblk = es1->es_lblk;
296 		es->es_len = es1->es_len;
297 		es->es_pblk = es1->es_pblk;
298 	}
299 
300 }
301 
302 /*
303  * Locking for __es_find_extent_range() for external use
304  */
305 void ext4_es_find_extent_range(struct inode *inode,
306 			       int (*matching_fn)(struct extent_status *es),
307 			       ext4_lblk_t lblk, ext4_lblk_t end,
308 			       struct extent_status *es)
309 {
310 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
311 		return;
312 
313 	trace_ext4_es_find_extent_range_enter(inode, lblk);
314 
315 	read_lock(&EXT4_I(inode)->i_es_lock);
316 	__es_find_extent_range(inode, matching_fn, lblk, end, es);
317 	read_unlock(&EXT4_I(inode)->i_es_lock);
318 
319 	trace_ext4_es_find_extent_range_exit(inode, es);
320 }
321 
322 /*
323  * __es_scan_range - search block range for block with specified status
324  *                   in extents status tree
325  *
326  * @inode - file containing the range
327  * @matching_fn - pointer to function that matches extents with desired status
328  * @lblk - logical block defining start of range
329  * @end - logical block defining end of range
330  *
331  * Returns true if at least one block in the specified block range satisfies
332  * the criterion specified by @matching_fn, and false if not.  If at least
333  * one extent has the specified status, then there is at least one block
334  * in the cluster with that status.  Should only be called by code that has
335  * taken i_es_lock.
336  */
337 static bool __es_scan_range(struct inode *inode,
338 			    int (*matching_fn)(struct extent_status *es),
339 			    ext4_lblk_t start, ext4_lblk_t end)
340 {
341 	struct extent_status es;
342 
343 	__es_find_extent_range(inode, matching_fn, start, end, &es);
344 	if (es.es_len == 0)
345 		return false;   /* no matching extent in the tree */
346 	else if (es.es_lblk <= start &&
347 		 start < es.es_lblk + es.es_len)
348 		return true;
349 	else if (start <= es.es_lblk && es.es_lblk <= end)
350 		return true;
351 	else
352 		return false;
353 }
354 /*
355  * Locking for __es_scan_range() for external use
356  */
357 bool ext4_es_scan_range(struct inode *inode,
358 			int (*matching_fn)(struct extent_status *es),
359 			ext4_lblk_t lblk, ext4_lblk_t end)
360 {
361 	bool ret;
362 
363 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
364 		return false;
365 
366 	read_lock(&EXT4_I(inode)->i_es_lock);
367 	ret = __es_scan_range(inode, matching_fn, lblk, end);
368 	read_unlock(&EXT4_I(inode)->i_es_lock);
369 
370 	return ret;
371 }
372 
373 /*
374  * __es_scan_clu - search cluster for block with specified status in
375  *                 extents status tree
376  *
377  * @inode - file containing the cluster
378  * @matching_fn - pointer to function that matches extents with desired status
379  * @lblk - logical block in cluster to be searched
380  *
381  * Returns true if at least one extent in the cluster containing @lblk
382  * satisfies the criterion specified by @matching_fn, and false if not.  If at
383  * least one extent has the specified status, then there is at least one block
384  * in the cluster with that status.  Should only be called by code that has
385  * taken i_es_lock.
386  */
387 static bool __es_scan_clu(struct inode *inode,
388 			  int (*matching_fn)(struct extent_status *es),
389 			  ext4_lblk_t lblk)
390 {
391 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
392 	ext4_lblk_t lblk_start, lblk_end;
393 
394 	lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
395 	lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
396 
397 	return __es_scan_range(inode, matching_fn, lblk_start, lblk_end);
398 }
399 
400 /*
401  * Locking for __es_scan_clu() for external use
402  */
403 bool ext4_es_scan_clu(struct inode *inode,
404 		      int (*matching_fn)(struct extent_status *es),
405 		      ext4_lblk_t lblk)
406 {
407 	bool ret;
408 
409 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
410 		return false;
411 
412 	read_lock(&EXT4_I(inode)->i_es_lock);
413 	ret = __es_scan_clu(inode, matching_fn, lblk);
414 	read_unlock(&EXT4_I(inode)->i_es_lock);
415 
416 	return ret;
417 }
418 
419 static void ext4_es_list_add(struct inode *inode)
420 {
421 	struct ext4_inode_info *ei = EXT4_I(inode);
422 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
423 
424 	if (!list_empty(&ei->i_es_list))
425 		return;
426 
427 	spin_lock(&sbi->s_es_lock);
428 	if (list_empty(&ei->i_es_list)) {
429 		list_add_tail(&ei->i_es_list, &sbi->s_es_list);
430 		sbi->s_es_nr_inode++;
431 	}
432 	spin_unlock(&sbi->s_es_lock);
433 }
434 
435 static void ext4_es_list_del(struct inode *inode)
436 {
437 	struct ext4_inode_info *ei = EXT4_I(inode);
438 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
439 
440 	spin_lock(&sbi->s_es_lock);
441 	if (!list_empty(&ei->i_es_list)) {
442 		list_del_init(&ei->i_es_list);
443 		sbi->s_es_nr_inode--;
444 		WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
445 	}
446 	spin_unlock(&sbi->s_es_lock);
447 }
448 
449 static struct extent_status *
450 ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
451 		     ext4_fsblk_t pblk)
452 {
453 	struct extent_status *es;
454 	es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
455 	if (es == NULL)
456 		return NULL;
457 	es->es_lblk = lblk;
458 	es->es_len = len;
459 	es->es_pblk = pblk;
460 
461 	/*
462 	 * We don't count delayed extent because we never try to reclaim them
463 	 */
464 	if (!ext4_es_is_delayed(es)) {
465 		if (!EXT4_I(inode)->i_es_shk_nr++)
466 			ext4_es_list_add(inode);
467 		percpu_counter_inc(&EXT4_SB(inode->i_sb)->
468 					s_es_stats.es_stats_shk_cnt);
469 	}
470 
471 	EXT4_I(inode)->i_es_all_nr++;
472 	percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
473 
474 	return es;
475 }
476 
477 static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
478 {
479 	EXT4_I(inode)->i_es_all_nr--;
480 	percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
481 
482 	/* Decrease the shrink counter when this es is not delayed */
483 	if (!ext4_es_is_delayed(es)) {
484 		BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
485 		if (!--EXT4_I(inode)->i_es_shk_nr)
486 			ext4_es_list_del(inode);
487 		percpu_counter_dec(&EXT4_SB(inode->i_sb)->
488 					s_es_stats.es_stats_shk_cnt);
489 	}
490 
491 	kmem_cache_free(ext4_es_cachep, es);
492 }
493 
494 /*
495  * Check whether or not two extents can be merged
496  * Condition:
497  *  - logical block number is contiguous
498  *  - physical block number is contiguous
499  *  - status is equal
500  */
501 static int ext4_es_can_be_merged(struct extent_status *es1,
502 				 struct extent_status *es2)
503 {
504 	if (ext4_es_type(es1) != ext4_es_type(es2))
505 		return 0;
506 
507 	if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
508 		pr_warn("ES assertion failed when merging extents. "
509 			"The sum of lengths of es1 (%d) and es2 (%d) "
510 			"is bigger than allowed file size (%d)\n",
511 			es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
512 		WARN_ON(1);
513 		return 0;
514 	}
515 
516 	if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
517 		return 0;
518 
519 	if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
520 	    (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
521 		return 1;
522 
523 	if (ext4_es_is_hole(es1))
524 		return 1;
525 
526 	/* we need to check delayed extent is without unwritten status */
527 	if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
528 		return 1;
529 
530 	return 0;
531 }
532 
533 static struct extent_status *
534 ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
535 {
536 	struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
537 	struct extent_status *es1;
538 	struct rb_node *node;
539 
540 	node = rb_prev(&es->rb_node);
541 	if (!node)
542 		return es;
543 
544 	es1 = rb_entry(node, struct extent_status, rb_node);
545 	if (ext4_es_can_be_merged(es1, es)) {
546 		es1->es_len += es->es_len;
547 		if (ext4_es_is_referenced(es))
548 			ext4_es_set_referenced(es1);
549 		rb_erase(&es->rb_node, &tree->root);
550 		ext4_es_free_extent(inode, es);
551 		es = es1;
552 	}
553 
554 	return es;
555 }
556 
557 static struct extent_status *
558 ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
559 {
560 	struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
561 	struct extent_status *es1;
562 	struct rb_node *node;
563 
564 	node = rb_next(&es->rb_node);
565 	if (!node)
566 		return es;
567 
568 	es1 = rb_entry(node, struct extent_status, rb_node);
569 	if (ext4_es_can_be_merged(es, es1)) {
570 		es->es_len += es1->es_len;
571 		if (ext4_es_is_referenced(es1))
572 			ext4_es_set_referenced(es);
573 		rb_erase(node, &tree->root);
574 		ext4_es_free_extent(inode, es1);
575 	}
576 
577 	return es;
578 }
579 
580 #ifdef ES_AGGRESSIVE_TEST
581 #include "ext4_extents.h"	/* Needed when ES_AGGRESSIVE_TEST is defined */
582 
583 static void ext4_es_insert_extent_ext_check(struct inode *inode,
584 					    struct extent_status *es)
585 {
586 	struct ext4_ext_path *path = NULL;
587 	struct ext4_extent *ex;
588 	ext4_lblk_t ee_block;
589 	ext4_fsblk_t ee_start;
590 	unsigned short ee_len;
591 	int depth, ee_status, es_status;
592 
593 	path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
594 	if (IS_ERR(path))
595 		return;
596 
597 	depth = ext_depth(inode);
598 	ex = path[depth].p_ext;
599 
600 	if (ex) {
601 
602 		ee_block = le32_to_cpu(ex->ee_block);
603 		ee_start = ext4_ext_pblock(ex);
604 		ee_len = ext4_ext_get_actual_len(ex);
605 
606 		ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
607 		es_status = ext4_es_is_unwritten(es) ? 1 : 0;
608 
609 		/*
610 		 * Make sure ex and es are not overlap when we try to insert
611 		 * a delayed/hole extent.
612 		 */
613 		if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
614 			if (in_range(es->es_lblk, ee_block, ee_len)) {
615 				pr_warn("ES insert assertion failed for "
616 					"inode: %lu we can find an extent "
617 					"at block [%d/%d/%llu/%c], but we "
618 					"want to add a delayed/hole extent "
619 					"[%d/%d/%llu/%x]\n",
620 					inode->i_ino, ee_block, ee_len,
621 					ee_start, ee_status ? 'u' : 'w',
622 					es->es_lblk, es->es_len,
623 					ext4_es_pblock(es), ext4_es_status(es));
624 			}
625 			goto out;
626 		}
627 
628 		/*
629 		 * We don't check ee_block == es->es_lblk, etc. because es
630 		 * might be a part of whole extent, vice versa.
631 		 */
632 		if (es->es_lblk < ee_block ||
633 		    ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
634 			pr_warn("ES insert assertion failed for inode: %lu "
635 				"ex_status [%d/%d/%llu/%c] != "
636 				"es_status [%d/%d/%llu/%c]\n", inode->i_ino,
637 				ee_block, ee_len, ee_start,
638 				ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
639 				ext4_es_pblock(es), es_status ? 'u' : 'w');
640 			goto out;
641 		}
642 
643 		if (ee_status ^ es_status) {
644 			pr_warn("ES insert assertion failed for inode: %lu "
645 				"ex_status [%d/%d/%llu/%c] != "
646 				"es_status [%d/%d/%llu/%c]\n", inode->i_ino,
647 				ee_block, ee_len, ee_start,
648 				ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
649 				ext4_es_pblock(es), es_status ? 'u' : 'w');
650 		}
651 	} else {
652 		/*
653 		 * We can't find an extent on disk.  So we need to make sure
654 		 * that we don't want to add an written/unwritten extent.
655 		 */
656 		if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
657 			pr_warn("ES insert assertion failed for inode: %lu "
658 				"can't find an extent at block %d but we want "
659 				"to add a written/unwritten extent "
660 				"[%d/%d/%llu/%x]\n", inode->i_ino,
661 				es->es_lblk, es->es_lblk, es->es_len,
662 				ext4_es_pblock(es), ext4_es_status(es));
663 		}
664 	}
665 out:
666 	ext4_free_ext_path(path);
667 }
668 
669 static void ext4_es_insert_extent_ind_check(struct inode *inode,
670 					    struct extent_status *es)
671 {
672 	struct ext4_map_blocks map;
673 	int retval;
674 
675 	/*
676 	 * Here we call ext4_ind_map_blocks to lookup a block mapping because
677 	 * 'Indirect' structure is defined in indirect.c.  So we couldn't
678 	 * access direct/indirect tree from outside.  It is too dirty to define
679 	 * this function in indirect.c file.
680 	 */
681 
682 	map.m_lblk = es->es_lblk;
683 	map.m_len = es->es_len;
684 
685 	retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
686 	if (retval > 0) {
687 		if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
688 			/*
689 			 * We want to add a delayed/hole extent but this
690 			 * block has been allocated.
691 			 */
692 			pr_warn("ES insert assertion failed for inode: %lu "
693 				"We can find blocks but we want to add a "
694 				"delayed/hole extent [%d/%d/%llu/%x]\n",
695 				inode->i_ino, es->es_lblk, es->es_len,
696 				ext4_es_pblock(es), ext4_es_status(es));
697 			return;
698 		} else if (ext4_es_is_written(es)) {
699 			if (retval != es->es_len) {
700 				pr_warn("ES insert assertion failed for "
701 					"inode: %lu retval %d != es_len %d\n",
702 					inode->i_ino, retval, es->es_len);
703 				return;
704 			}
705 			if (map.m_pblk != ext4_es_pblock(es)) {
706 				pr_warn("ES insert assertion failed for "
707 					"inode: %lu m_pblk %llu != "
708 					"es_pblk %llu\n",
709 					inode->i_ino, map.m_pblk,
710 					ext4_es_pblock(es));
711 				return;
712 			}
713 		} else {
714 			/*
715 			 * We don't need to check unwritten extent because
716 			 * indirect-based file doesn't have it.
717 			 */
718 			BUG();
719 		}
720 	} else if (retval == 0) {
721 		if (ext4_es_is_written(es)) {
722 			pr_warn("ES insert assertion failed for inode: %lu "
723 				"We can't find the block but we want to add "
724 				"a written extent [%d/%d/%llu/%x]\n",
725 				inode->i_ino, es->es_lblk, es->es_len,
726 				ext4_es_pblock(es), ext4_es_status(es));
727 			return;
728 		}
729 	}
730 }
731 
732 static inline void ext4_es_insert_extent_check(struct inode *inode,
733 					       struct extent_status *es)
734 {
735 	/*
736 	 * We don't need to worry about the race condition because
737 	 * caller takes i_data_sem locking.
738 	 */
739 	BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
740 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
741 		ext4_es_insert_extent_ext_check(inode, es);
742 	else
743 		ext4_es_insert_extent_ind_check(inode, es);
744 }
745 #else
746 static inline void ext4_es_insert_extent_check(struct inode *inode,
747 					       struct extent_status *es)
748 {
749 }
750 #endif
751 
752 static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
753 {
754 	struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
755 	struct rb_node **p = &tree->root.rb_node;
756 	struct rb_node *parent = NULL;
757 	struct extent_status *es;
758 
759 	while (*p) {
760 		parent = *p;
761 		es = rb_entry(parent, struct extent_status, rb_node);
762 
763 		if (newes->es_lblk < es->es_lblk) {
764 			if (ext4_es_can_be_merged(newes, es)) {
765 				/*
766 				 * Here we can modify es_lblk directly
767 				 * because it isn't overlapped.
768 				 */
769 				es->es_lblk = newes->es_lblk;
770 				es->es_len += newes->es_len;
771 				if (ext4_es_is_written(es) ||
772 				    ext4_es_is_unwritten(es))
773 					ext4_es_store_pblock(es,
774 							     newes->es_pblk);
775 				es = ext4_es_try_to_merge_left(inode, es);
776 				goto out;
777 			}
778 			p = &(*p)->rb_left;
779 		} else if (newes->es_lblk > ext4_es_end(es)) {
780 			if (ext4_es_can_be_merged(es, newes)) {
781 				es->es_len += newes->es_len;
782 				es = ext4_es_try_to_merge_right(inode, es);
783 				goto out;
784 			}
785 			p = &(*p)->rb_right;
786 		} else {
787 			BUG();
788 			return -EINVAL;
789 		}
790 	}
791 
792 	es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
793 				  newes->es_pblk);
794 	if (!es)
795 		return -ENOMEM;
796 	rb_link_node(&es->rb_node, parent, p);
797 	rb_insert_color(&es->rb_node, &tree->root);
798 
799 out:
800 	tree->cache_es = es;
801 	return 0;
802 }
803 
804 /*
805  * ext4_es_insert_extent() adds information to an inode's extent
806  * status tree.
807  *
808  * Return 0 on success, error code on failure.
809  */
810 int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
811 			  ext4_lblk_t len, ext4_fsblk_t pblk,
812 			  unsigned int status)
813 {
814 	struct extent_status newes;
815 	ext4_lblk_t end = lblk + len - 1;
816 	int err = 0;
817 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
818 
819 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
820 		return 0;
821 
822 	es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
823 		 lblk, len, pblk, status, inode->i_ino);
824 
825 	if (!len)
826 		return 0;
827 
828 	BUG_ON(end < lblk);
829 
830 	if ((status & EXTENT_STATUS_DELAYED) &&
831 	    (status & EXTENT_STATUS_WRITTEN)) {
832 		ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
833 				" delayed and written which can potentially "
834 				" cause data loss.", lblk, len);
835 		WARN_ON(1);
836 	}
837 
838 	newes.es_lblk = lblk;
839 	newes.es_len = len;
840 	ext4_es_store_pblock_status(&newes, pblk, status);
841 	trace_ext4_es_insert_extent(inode, &newes);
842 
843 	ext4_es_insert_extent_check(inode, &newes);
844 
845 	write_lock(&EXT4_I(inode)->i_es_lock);
846 	err = __es_remove_extent(inode, lblk, end, NULL);
847 	if (err != 0)
848 		goto error;
849 retry:
850 	err = __es_insert_extent(inode, &newes);
851 	if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
852 					  128, EXT4_I(inode)))
853 		goto retry;
854 	if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
855 		err = 0;
856 
857 	if (sbi->s_cluster_ratio > 1 && test_opt(inode->i_sb, DELALLOC) &&
858 	    (status & EXTENT_STATUS_WRITTEN ||
859 	     status & EXTENT_STATUS_UNWRITTEN))
860 		__revise_pending(inode, lblk, len);
861 
862 error:
863 	write_unlock(&EXT4_I(inode)->i_es_lock);
864 
865 	ext4_es_print_tree(inode);
866 
867 	return err;
868 }
869 
870 /*
871  * ext4_es_cache_extent() inserts information into the extent status
872  * tree if and only if there isn't information about the range in
873  * question already.
874  */
875 void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
876 			  ext4_lblk_t len, ext4_fsblk_t pblk,
877 			  unsigned int status)
878 {
879 	struct extent_status *es;
880 	struct extent_status newes;
881 	ext4_lblk_t end = lblk + len - 1;
882 
883 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
884 		return;
885 
886 	newes.es_lblk = lblk;
887 	newes.es_len = len;
888 	ext4_es_store_pblock_status(&newes, pblk, status);
889 	trace_ext4_es_cache_extent(inode, &newes);
890 
891 	if (!len)
892 		return;
893 
894 	BUG_ON(end < lblk);
895 
896 	write_lock(&EXT4_I(inode)->i_es_lock);
897 
898 	es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
899 	if (!es || es->es_lblk > end)
900 		__es_insert_extent(inode, &newes);
901 	write_unlock(&EXT4_I(inode)->i_es_lock);
902 }
903 
904 /*
905  * ext4_es_lookup_extent() looks up an extent in extent status tree.
906  *
907  * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
908  *
909  * Return: 1 on found, 0 on not
910  */
911 int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
912 			  ext4_lblk_t *next_lblk,
913 			  struct extent_status *es)
914 {
915 	struct ext4_es_tree *tree;
916 	struct ext4_es_stats *stats;
917 	struct extent_status *es1 = NULL;
918 	struct rb_node *node;
919 	int found = 0;
920 
921 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
922 		return 0;
923 
924 	trace_ext4_es_lookup_extent_enter(inode, lblk);
925 	es_debug("lookup extent in block %u\n", lblk);
926 
927 	tree = &EXT4_I(inode)->i_es_tree;
928 	read_lock(&EXT4_I(inode)->i_es_lock);
929 
930 	/* find extent in cache firstly */
931 	es->es_lblk = es->es_len = es->es_pblk = 0;
932 	es1 = READ_ONCE(tree->cache_es);
933 	if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
934 		es_debug("%u cached by [%u/%u)\n",
935 			 lblk, es1->es_lblk, es1->es_len);
936 		found = 1;
937 		goto out;
938 	}
939 
940 	node = tree->root.rb_node;
941 	while (node) {
942 		es1 = rb_entry(node, struct extent_status, rb_node);
943 		if (lblk < es1->es_lblk)
944 			node = node->rb_left;
945 		else if (lblk > ext4_es_end(es1))
946 			node = node->rb_right;
947 		else {
948 			found = 1;
949 			break;
950 		}
951 	}
952 
953 out:
954 	stats = &EXT4_SB(inode->i_sb)->s_es_stats;
955 	if (found) {
956 		BUG_ON(!es1);
957 		es->es_lblk = es1->es_lblk;
958 		es->es_len = es1->es_len;
959 		es->es_pblk = es1->es_pblk;
960 		if (!ext4_es_is_referenced(es1))
961 			ext4_es_set_referenced(es1);
962 		percpu_counter_inc(&stats->es_stats_cache_hits);
963 		if (next_lblk) {
964 			node = rb_next(&es1->rb_node);
965 			if (node) {
966 				es1 = rb_entry(node, struct extent_status,
967 					       rb_node);
968 				*next_lblk = es1->es_lblk;
969 			} else
970 				*next_lblk = 0;
971 		}
972 	} else {
973 		percpu_counter_inc(&stats->es_stats_cache_misses);
974 	}
975 
976 	read_unlock(&EXT4_I(inode)->i_es_lock);
977 
978 	trace_ext4_es_lookup_extent_exit(inode, es, found);
979 	return found;
980 }
981 
982 struct rsvd_count {
983 	int ndelonly;
984 	bool first_do_lblk_found;
985 	ext4_lblk_t first_do_lblk;
986 	ext4_lblk_t last_do_lblk;
987 	struct extent_status *left_es;
988 	bool partial;
989 	ext4_lblk_t lclu;
990 };
991 
992 /*
993  * init_rsvd - initialize reserved count data before removing block range
994  *	       in file from extent status tree
995  *
996  * @inode - file containing range
997  * @lblk - first block in range
998  * @es - pointer to first extent in range
999  * @rc - pointer to reserved count data
1000  *
1001  * Assumes es is not NULL
1002  */
1003 static void init_rsvd(struct inode *inode, ext4_lblk_t lblk,
1004 		      struct extent_status *es, struct rsvd_count *rc)
1005 {
1006 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1007 	struct rb_node *node;
1008 
1009 	rc->ndelonly = 0;
1010 
1011 	/*
1012 	 * for bigalloc, note the first delonly block in the range has not
1013 	 * been found, record the extent containing the block to the left of
1014 	 * the region to be removed, if any, and note that there's no partial
1015 	 * cluster to track
1016 	 */
1017 	if (sbi->s_cluster_ratio > 1) {
1018 		rc->first_do_lblk_found = false;
1019 		if (lblk > es->es_lblk) {
1020 			rc->left_es = es;
1021 		} else {
1022 			node = rb_prev(&es->rb_node);
1023 			rc->left_es = node ? rb_entry(node,
1024 						      struct extent_status,
1025 						      rb_node) : NULL;
1026 		}
1027 		rc->partial = false;
1028 	}
1029 }
1030 
1031 /*
1032  * count_rsvd - count the clusters containing delayed and not unwritten
1033  *		(delonly) blocks in a range within an extent and add to
1034  *	        the running tally in rsvd_count
1035  *
1036  * @inode - file containing extent
1037  * @lblk - first block in range
1038  * @len - length of range in blocks
1039  * @es - pointer to extent containing clusters to be counted
1040  * @rc - pointer to reserved count data
1041  *
1042  * Tracks partial clusters found at the beginning and end of extents so
1043  * they aren't overcounted when they span adjacent extents
1044  */
1045 static void count_rsvd(struct inode *inode, ext4_lblk_t lblk, long len,
1046 		       struct extent_status *es, struct rsvd_count *rc)
1047 {
1048 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1049 	ext4_lblk_t i, end, nclu;
1050 
1051 	if (!ext4_es_is_delonly(es))
1052 		return;
1053 
1054 	WARN_ON(len <= 0);
1055 
1056 	if (sbi->s_cluster_ratio == 1) {
1057 		rc->ndelonly += (int) len;
1058 		return;
1059 	}
1060 
1061 	/* bigalloc */
1062 
1063 	i = (lblk < es->es_lblk) ? es->es_lblk : lblk;
1064 	end = lblk + (ext4_lblk_t) len - 1;
1065 	end = (end > ext4_es_end(es)) ? ext4_es_end(es) : end;
1066 
1067 	/* record the first block of the first delonly extent seen */
1068 	if (!rc->first_do_lblk_found) {
1069 		rc->first_do_lblk = i;
1070 		rc->first_do_lblk_found = true;
1071 	}
1072 
1073 	/* update the last lblk in the region seen so far */
1074 	rc->last_do_lblk = end;
1075 
1076 	/*
1077 	 * if we're tracking a partial cluster and the current extent
1078 	 * doesn't start with it, count it and stop tracking
1079 	 */
1080 	if (rc->partial && (rc->lclu != EXT4_B2C(sbi, i))) {
1081 		rc->ndelonly++;
1082 		rc->partial = false;
1083 	}
1084 
1085 	/*
1086 	 * if the first cluster doesn't start on a cluster boundary but
1087 	 * ends on one, count it
1088 	 */
1089 	if (EXT4_LBLK_COFF(sbi, i) != 0) {
1090 		if (end >= EXT4_LBLK_CFILL(sbi, i)) {
1091 			rc->ndelonly++;
1092 			rc->partial = false;
1093 			i = EXT4_LBLK_CFILL(sbi, i) + 1;
1094 		}
1095 	}
1096 
1097 	/*
1098 	 * if the current cluster starts on a cluster boundary, count the
1099 	 * number of whole delonly clusters in the extent
1100 	 */
1101 	if ((i + sbi->s_cluster_ratio - 1) <= end) {
1102 		nclu = (end - i + 1) >> sbi->s_cluster_bits;
1103 		rc->ndelonly += nclu;
1104 		i += nclu << sbi->s_cluster_bits;
1105 	}
1106 
1107 	/*
1108 	 * start tracking a partial cluster if there's a partial at the end
1109 	 * of the current extent and we're not already tracking one
1110 	 */
1111 	if (!rc->partial && i <= end) {
1112 		rc->partial = true;
1113 		rc->lclu = EXT4_B2C(sbi, i);
1114 	}
1115 }
1116 
1117 /*
1118  * __pr_tree_search - search for a pending cluster reservation
1119  *
1120  * @root - root of pending reservation tree
1121  * @lclu - logical cluster to search for
1122  *
1123  * Returns the pending reservation for the cluster identified by @lclu
1124  * if found.  If not, returns a reservation for the next cluster if any,
1125  * and if not, returns NULL.
1126  */
1127 static struct pending_reservation *__pr_tree_search(struct rb_root *root,
1128 						    ext4_lblk_t lclu)
1129 {
1130 	struct rb_node *node = root->rb_node;
1131 	struct pending_reservation *pr = NULL;
1132 
1133 	while (node) {
1134 		pr = rb_entry(node, struct pending_reservation, rb_node);
1135 		if (lclu < pr->lclu)
1136 			node = node->rb_left;
1137 		else if (lclu > pr->lclu)
1138 			node = node->rb_right;
1139 		else
1140 			return pr;
1141 	}
1142 	if (pr && lclu < pr->lclu)
1143 		return pr;
1144 	if (pr && lclu > pr->lclu) {
1145 		node = rb_next(&pr->rb_node);
1146 		return node ? rb_entry(node, struct pending_reservation,
1147 				       rb_node) : NULL;
1148 	}
1149 	return NULL;
1150 }
1151 
1152 /*
1153  * get_rsvd - calculates and returns the number of cluster reservations to be
1154  *	      released when removing a block range from the extent status tree
1155  *	      and releases any pending reservations within the range
1156  *
1157  * @inode - file containing block range
1158  * @end - last block in range
1159  * @right_es - pointer to extent containing next block beyond end or NULL
1160  * @rc - pointer to reserved count data
1161  *
1162  * The number of reservations to be released is equal to the number of
1163  * clusters containing delayed and not unwritten (delonly) blocks within
1164  * the range, minus the number of clusters still containing delonly blocks
1165  * at the ends of the range, and minus the number of pending reservations
1166  * within the range.
1167  */
1168 static unsigned int get_rsvd(struct inode *inode, ext4_lblk_t end,
1169 			     struct extent_status *right_es,
1170 			     struct rsvd_count *rc)
1171 {
1172 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1173 	struct pending_reservation *pr;
1174 	struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1175 	struct rb_node *node;
1176 	ext4_lblk_t first_lclu, last_lclu;
1177 	bool left_delonly, right_delonly, count_pending;
1178 	struct extent_status *es;
1179 
1180 	if (sbi->s_cluster_ratio > 1) {
1181 		/* count any remaining partial cluster */
1182 		if (rc->partial)
1183 			rc->ndelonly++;
1184 
1185 		if (rc->ndelonly == 0)
1186 			return 0;
1187 
1188 		first_lclu = EXT4_B2C(sbi, rc->first_do_lblk);
1189 		last_lclu = EXT4_B2C(sbi, rc->last_do_lblk);
1190 
1191 		/*
1192 		 * decrease the delonly count by the number of clusters at the
1193 		 * ends of the range that still contain delonly blocks -
1194 		 * these clusters still need to be reserved
1195 		 */
1196 		left_delonly = right_delonly = false;
1197 
1198 		es = rc->left_es;
1199 		while (es && ext4_es_end(es) >=
1200 		       EXT4_LBLK_CMASK(sbi, rc->first_do_lblk)) {
1201 			if (ext4_es_is_delonly(es)) {
1202 				rc->ndelonly--;
1203 				left_delonly = true;
1204 				break;
1205 			}
1206 			node = rb_prev(&es->rb_node);
1207 			if (!node)
1208 				break;
1209 			es = rb_entry(node, struct extent_status, rb_node);
1210 		}
1211 		if (right_es && (!left_delonly || first_lclu != last_lclu)) {
1212 			if (end < ext4_es_end(right_es)) {
1213 				es = right_es;
1214 			} else {
1215 				node = rb_next(&right_es->rb_node);
1216 				es = node ? rb_entry(node, struct extent_status,
1217 						     rb_node) : NULL;
1218 			}
1219 			while (es && es->es_lblk <=
1220 			       EXT4_LBLK_CFILL(sbi, rc->last_do_lblk)) {
1221 				if (ext4_es_is_delonly(es)) {
1222 					rc->ndelonly--;
1223 					right_delonly = true;
1224 					break;
1225 				}
1226 				node = rb_next(&es->rb_node);
1227 				if (!node)
1228 					break;
1229 				es = rb_entry(node, struct extent_status,
1230 					      rb_node);
1231 			}
1232 		}
1233 
1234 		/*
1235 		 * Determine the block range that should be searched for
1236 		 * pending reservations, if any.  Clusters on the ends of the
1237 		 * original removed range containing delonly blocks are
1238 		 * excluded.  They've already been accounted for and it's not
1239 		 * possible to determine if an associated pending reservation
1240 		 * should be released with the information available in the
1241 		 * extents status tree.
1242 		 */
1243 		if (first_lclu == last_lclu) {
1244 			if (left_delonly | right_delonly)
1245 				count_pending = false;
1246 			else
1247 				count_pending = true;
1248 		} else {
1249 			if (left_delonly)
1250 				first_lclu++;
1251 			if (right_delonly)
1252 				last_lclu--;
1253 			if (first_lclu <= last_lclu)
1254 				count_pending = true;
1255 			else
1256 				count_pending = false;
1257 		}
1258 
1259 		/*
1260 		 * a pending reservation found between first_lclu and last_lclu
1261 		 * represents an allocated cluster that contained at least one
1262 		 * delonly block, so the delonly total must be reduced by one
1263 		 * for each pending reservation found and released
1264 		 */
1265 		if (count_pending) {
1266 			pr = __pr_tree_search(&tree->root, first_lclu);
1267 			while (pr && pr->lclu <= last_lclu) {
1268 				rc->ndelonly--;
1269 				node = rb_next(&pr->rb_node);
1270 				rb_erase(&pr->rb_node, &tree->root);
1271 				kmem_cache_free(ext4_pending_cachep, pr);
1272 				if (!node)
1273 					break;
1274 				pr = rb_entry(node, struct pending_reservation,
1275 					      rb_node);
1276 			}
1277 		}
1278 	}
1279 	return rc->ndelonly;
1280 }
1281 
1282 
1283 /*
1284  * __es_remove_extent - removes block range from extent status tree
1285  *
1286  * @inode - file containing range
1287  * @lblk - first block in range
1288  * @end - last block in range
1289  * @reserved - number of cluster reservations released
1290  *
1291  * If @reserved is not NULL and delayed allocation is enabled, counts
1292  * block/cluster reservations freed by removing range and if bigalloc
1293  * enabled cancels pending reservations as needed. Returns 0 on success,
1294  * error code on failure.
1295  */
1296 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1297 			      ext4_lblk_t end, int *reserved)
1298 {
1299 	struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
1300 	struct rb_node *node;
1301 	struct extent_status *es;
1302 	struct extent_status orig_es;
1303 	ext4_lblk_t len1, len2;
1304 	ext4_fsblk_t block;
1305 	int err;
1306 	bool count_reserved = true;
1307 	struct rsvd_count rc;
1308 
1309 	if (reserved == NULL || !test_opt(inode->i_sb, DELALLOC))
1310 		count_reserved = false;
1311 retry:
1312 	err = 0;
1313 
1314 	es = __es_tree_search(&tree->root, lblk);
1315 	if (!es)
1316 		goto out;
1317 	if (es->es_lblk > end)
1318 		goto out;
1319 
1320 	/* Simply invalidate cache_es. */
1321 	tree->cache_es = NULL;
1322 	if (count_reserved)
1323 		init_rsvd(inode, lblk, es, &rc);
1324 
1325 	orig_es.es_lblk = es->es_lblk;
1326 	orig_es.es_len = es->es_len;
1327 	orig_es.es_pblk = es->es_pblk;
1328 
1329 	len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
1330 	len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
1331 	if (len1 > 0)
1332 		es->es_len = len1;
1333 	if (len2 > 0) {
1334 		if (len1 > 0) {
1335 			struct extent_status newes;
1336 
1337 			newes.es_lblk = end + 1;
1338 			newes.es_len = len2;
1339 			block = 0x7FDEADBEEFULL;
1340 			if (ext4_es_is_written(&orig_es) ||
1341 			    ext4_es_is_unwritten(&orig_es))
1342 				block = ext4_es_pblock(&orig_es) +
1343 					orig_es.es_len - len2;
1344 			ext4_es_store_pblock_status(&newes, block,
1345 						    ext4_es_status(&orig_es));
1346 			err = __es_insert_extent(inode, &newes);
1347 			if (err) {
1348 				es->es_lblk = orig_es.es_lblk;
1349 				es->es_len = orig_es.es_len;
1350 				if ((err == -ENOMEM) &&
1351 				    __es_shrink(EXT4_SB(inode->i_sb),
1352 							128, EXT4_I(inode)))
1353 					goto retry;
1354 				goto out;
1355 			}
1356 		} else {
1357 			es->es_lblk = end + 1;
1358 			es->es_len = len2;
1359 			if (ext4_es_is_written(es) ||
1360 			    ext4_es_is_unwritten(es)) {
1361 				block = orig_es.es_pblk + orig_es.es_len - len2;
1362 				ext4_es_store_pblock(es, block);
1363 			}
1364 		}
1365 		if (count_reserved)
1366 			count_rsvd(inode, lblk, orig_es.es_len - len1 - len2,
1367 				   &orig_es, &rc);
1368 		goto out_get_reserved;
1369 	}
1370 
1371 	if (len1 > 0) {
1372 		if (count_reserved)
1373 			count_rsvd(inode, lblk, orig_es.es_len - len1,
1374 				   &orig_es, &rc);
1375 		node = rb_next(&es->rb_node);
1376 		if (node)
1377 			es = rb_entry(node, struct extent_status, rb_node);
1378 		else
1379 			es = NULL;
1380 	}
1381 
1382 	while (es && ext4_es_end(es) <= end) {
1383 		if (count_reserved)
1384 			count_rsvd(inode, es->es_lblk, es->es_len, es, &rc);
1385 		node = rb_next(&es->rb_node);
1386 		rb_erase(&es->rb_node, &tree->root);
1387 		ext4_es_free_extent(inode, es);
1388 		if (!node) {
1389 			es = NULL;
1390 			break;
1391 		}
1392 		es = rb_entry(node, struct extent_status, rb_node);
1393 	}
1394 
1395 	if (es && es->es_lblk < end + 1) {
1396 		ext4_lblk_t orig_len = es->es_len;
1397 
1398 		len1 = ext4_es_end(es) - end;
1399 		if (count_reserved)
1400 			count_rsvd(inode, es->es_lblk, orig_len - len1,
1401 				   es, &rc);
1402 		es->es_lblk = end + 1;
1403 		es->es_len = len1;
1404 		if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
1405 			block = es->es_pblk + orig_len - len1;
1406 			ext4_es_store_pblock(es, block);
1407 		}
1408 	}
1409 
1410 out_get_reserved:
1411 	if (count_reserved)
1412 		*reserved = get_rsvd(inode, end, es, &rc);
1413 out:
1414 	return err;
1415 }
1416 
1417 /*
1418  * ext4_es_remove_extent - removes block range from extent status tree
1419  *
1420  * @inode - file containing range
1421  * @lblk - first block in range
1422  * @len - number of blocks to remove
1423  *
1424  * Reduces block/cluster reservation count and for bigalloc cancels pending
1425  * reservations as needed. Returns 0 on success, error code on failure.
1426  */
1427 int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1428 			  ext4_lblk_t len)
1429 {
1430 	ext4_lblk_t end;
1431 	int err = 0;
1432 	int reserved = 0;
1433 
1434 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
1435 		return 0;
1436 
1437 	trace_ext4_es_remove_extent(inode, lblk, len);
1438 	es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
1439 		 lblk, len, inode->i_ino);
1440 
1441 	if (!len)
1442 		return err;
1443 
1444 	end = lblk + len - 1;
1445 	BUG_ON(end < lblk);
1446 
1447 	/*
1448 	 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
1449 	 * so that we are sure __es_shrink() is done with the inode before it
1450 	 * is reclaimed.
1451 	 */
1452 	write_lock(&EXT4_I(inode)->i_es_lock);
1453 	err = __es_remove_extent(inode, lblk, end, &reserved);
1454 	write_unlock(&EXT4_I(inode)->i_es_lock);
1455 	ext4_es_print_tree(inode);
1456 	ext4_da_release_space(inode, reserved);
1457 	return err;
1458 }
1459 
1460 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
1461 		       struct ext4_inode_info *locked_ei)
1462 {
1463 	struct ext4_inode_info *ei;
1464 	struct ext4_es_stats *es_stats;
1465 	ktime_t start_time;
1466 	u64 scan_time;
1467 	int nr_to_walk;
1468 	int nr_shrunk = 0;
1469 	int retried = 0, nr_skipped = 0;
1470 
1471 	es_stats = &sbi->s_es_stats;
1472 	start_time = ktime_get();
1473 
1474 retry:
1475 	spin_lock(&sbi->s_es_lock);
1476 	nr_to_walk = sbi->s_es_nr_inode;
1477 	while (nr_to_walk-- > 0) {
1478 		if (list_empty(&sbi->s_es_list)) {
1479 			spin_unlock(&sbi->s_es_lock);
1480 			goto out;
1481 		}
1482 		ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
1483 				      i_es_list);
1484 		/* Move the inode to the tail */
1485 		list_move_tail(&ei->i_es_list, &sbi->s_es_list);
1486 
1487 		/*
1488 		 * Normally we try hard to avoid shrinking precached inodes,
1489 		 * but we will as a last resort.
1490 		 */
1491 		if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1492 						EXT4_STATE_EXT_PRECACHED)) {
1493 			nr_skipped++;
1494 			continue;
1495 		}
1496 
1497 		if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1498 			nr_skipped++;
1499 			continue;
1500 		}
1501 		/*
1502 		 * Now we hold i_es_lock which protects us from inode reclaim
1503 		 * freeing inode under us
1504 		 */
1505 		spin_unlock(&sbi->s_es_lock);
1506 
1507 		nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
1508 		write_unlock(&ei->i_es_lock);
1509 
1510 		if (nr_to_scan <= 0)
1511 			goto out;
1512 		spin_lock(&sbi->s_es_lock);
1513 	}
1514 	spin_unlock(&sbi->s_es_lock);
1515 
1516 	/*
1517 	 * If we skipped any inodes, and we weren't able to make any
1518 	 * forward progress, try again to scan precached inodes.
1519 	 */
1520 	if ((nr_shrunk == 0) && nr_skipped && !retried) {
1521 		retried++;
1522 		goto retry;
1523 	}
1524 
1525 	if (locked_ei && nr_shrunk == 0)
1526 		nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
1527 
1528 out:
1529 	scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1530 	if (likely(es_stats->es_stats_scan_time))
1531 		es_stats->es_stats_scan_time = (scan_time +
1532 				es_stats->es_stats_scan_time*3) / 4;
1533 	else
1534 		es_stats->es_stats_scan_time = scan_time;
1535 	if (scan_time > es_stats->es_stats_max_scan_time)
1536 		es_stats->es_stats_max_scan_time = scan_time;
1537 	if (likely(es_stats->es_stats_shrunk))
1538 		es_stats->es_stats_shrunk = (nr_shrunk +
1539 				es_stats->es_stats_shrunk*3) / 4;
1540 	else
1541 		es_stats->es_stats_shrunk = nr_shrunk;
1542 
1543 	trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
1544 			     nr_skipped, retried);
1545 	return nr_shrunk;
1546 }
1547 
1548 static unsigned long ext4_es_count(struct shrinker *shrink,
1549 				   struct shrink_control *sc)
1550 {
1551 	unsigned long nr;
1552 	struct ext4_sb_info *sbi;
1553 
1554 	sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
1555 	nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1556 	trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
1557 	return nr;
1558 }
1559 
1560 static unsigned long ext4_es_scan(struct shrinker *shrink,
1561 				  struct shrink_control *sc)
1562 {
1563 	struct ext4_sb_info *sbi = container_of(shrink,
1564 					struct ext4_sb_info, s_es_shrinker);
1565 	int nr_to_scan = sc->nr_to_scan;
1566 	int ret, nr_shrunk;
1567 
1568 	ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1569 	trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
1570 
1571 	nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
1572 
1573 	ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1574 	trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
1575 	return nr_shrunk;
1576 }
1577 
1578 int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
1579 {
1580 	struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
1581 	struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1582 	struct ext4_inode_info *ei, *max = NULL;
1583 	unsigned int inode_cnt = 0;
1584 
1585 	if (v != SEQ_START_TOKEN)
1586 		return 0;
1587 
1588 	/* here we just find an inode that has the max nr. of objects */
1589 	spin_lock(&sbi->s_es_lock);
1590 	list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
1591 		inode_cnt++;
1592 		if (max && max->i_es_all_nr < ei->i_es_all_nr)
1593 			max = ei;
1594 		else if (!max)
1595 			max = ei;
1596 	}
1597 	spin_unlock(&sbi->s_es_lock);
1598 
1599 	seq_printf(seq, "stats:\n  %lld objects\n  %lld reclaimable objects\n",
1600 		   percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
1601 		   percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
1602 	seq_printf(seq, "  %lld/%lld cache hits/misses\n",
1603 		   percpu_counter_sum_positive(&es_stats->es_stats_cache_hits),
1604 		   percpu_counter_sum_positive(&es_stats->es_stats_cache_misses));
1605 	if (inode_cnt)
1606 		seq_printf(seq, "  %d inodes on list\n", inode_cnt);
1607 
1608 	seq_printf(seq, "average:\n  %llu us scan time\n",
1609 	    div_u64(es_stats->es_stats_scan_time, 1000));
1610 	seq_printf(seq, "  %lu shrunk objects\n", es_stats->es_stats_shrunk);
1611 	if (inode_cnt)
1612 		seq_printf(seq,
1613 		    "maximum:\n  %lu inode (%u objects, %u reclaimable)\n"
1614 		    "  %llu us max scan time\n",
1615 		    max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
1616 		    div_u64(es_stats->es_stats_max_scan_time, 1000));
1617 
1618 	return 0;
1619 }
1620 
1621 int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1622 {
1623 	int err;
1624 
1625 	/* Make sure we have enough bits for physical block number */
1626 	BUILD_BUG_ON(ES_SHIFT < 48);
1627 	INIT_LIST_HEAD(&sbi->s_es_list);
1628 	sbi->s_es_nr_inode = 0;
1629 	spin_lock_init(&sbi->s_es_lock);
1630 	sbi->s_es_stats.es_stats_shrunk = 0;
1631 	err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_hits, 0,
1632 				  GFP_KERNEL);
1633 	if (err)
1634 		return err;
1635 	err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_misses, 0,
1636 				  GFP_KERNEL);
1637 	if (err)
1638 		goto err1;
1639 	sbi->s_es_stats.es_stats_scan_time = 0;
1640 	sbi->s_es_stats.es_stats_max_scan_time = 0;
1641 	err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
1642 	if (err)
1643 		goto err2;
1644 	err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
1645 	if (err)
1646 		goto err3;
1647 
1648 	sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1649 	sbi->s_es_shrinker.count_objects = ext4_es_count;
1650 	sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
1651 	err = register_shrinker(&sbi->s_es_shrinker, "ext4-es:%s",
1652 				sbi->s_sb->s_id);
1653 	if (err)
1654 		goto err4;
1655 
1656 	return 0;
1657 err4:
1658 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1659 err3:
1660 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1661 err2:
1662 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1663 err1:
1664 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1665 	return err;
1666 }
1667 
1668 void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
1669 {
1670 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1671 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1672 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1673 	percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1674 	unregister_shrinker(&sbi->s_es_shrinker);
1675 }
1676 
1677 /*
1678  * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1679  * most *nr_to_scan extents, update *nr_to_scan accordingly.
1680  *
1681  * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1682  * Increment *nr_shrunk by the number of reclaimed extents. Also update
1683  * ei->i_es_shrink_lblk to where we should continue scanning.
1684  */
1685 static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1686 				 int *nr_to_scan, int *nr_shrunk)
1687 {
1688 	struct inode *inode = &ei->vfs_inode;
1689 	struct ext4_es_tree *tree = &ei->i_es_tree;
1690 	struct extent_status *es;
1691 	struct rb_node *node;
1692 
1693 	es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1694 	if (!es)
1695 		goto out_wrap;
1696 
1697 	while (*nr_to_scan > 0) {
1698 		if (es->es_lblk > end) {
1699 			ei->i_es_shrink_lblk = end + 1;
1700 			return 0;
1701 		}
1702 
1703 		(*nr_to_scan)--;
1704 		node = rb_next(&es->rb_node);
1705 		/*
1706 		 * We can't reclaim delayed extent from status tree because
1707 		 * fiemap, bigallic, and seek_data/hole need to use it.
1708 		 */
1709 		if (ext4_es_is_delayed(es))
1710 			goto next;
1711 		if (ext4_es_is_referenced(es)) {
1712 			ext4_es_clear_referenced(es);
1713 			goto next;
1714 		}
1715 
1716 		rb_erase(&es->rb_node, &tree->root);
1717 		ext4_es_free_extent(inode, es);
1718 		(*nr_shrunk)++;
1719 next:
1720 		if (!node)
1721 			goto out_wrap;
1722 		es = rb_entry(node, struct extent_status, rb_node);
1723 	}
1724 	ei->i_es_shrink_lblk = es->es_lblk;
1725 	return 1;
1726 out_wrap:
1727 	ei->i_es_shrink_lblk = 0;
1728 	return 0;
1729 }
1730 
1731 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1732 {
1733 	struct inode *inode = &ei->vfs_inode;
1734 	int nr_shrunk = 0;
1735 	ext4_lblk_t start = ei->i_es_shrink_lblk;
1736 	static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1737 				      DEFAULT_RATELIMIT_BURST);
1738 
1739 	if (ei->i_es_shk_nr == 0)
1740 		return 0;
1741 
1742 	if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1743 	    __ratelimit(&_rs))
1744 		ext4_warning(inode->i_sb, "forced shrink of precached extents");
1745 
1746 	if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1747 	    start != 0)
1748 		es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1749 
1750 	ei->i_es_tree.cache_es = NULL;
1751 	return nr_shrunk;
1752 }
1753 
1754 /*
1755  * Called to support EXT4_IOC_CLEAR_ES_CACHE.  We can only remove
1756  * discretionary entries from the extent status cache.  (Some entries
1757  * must be present for proper operations.)
1758  */
1759 void ext4_clear_inode_es(struct inode *inode)
1760 {
1761 	struct ext4_inode_info *ei = EXT4_I(inode);
1762 	struct extent_status *es;
1763 	struct ext4_es_tree *tree;
1764 	struct rb_node *node;
1765 
1766 	write_lock(&ei->i_es_lock);
1767 	tree = &EXT4_I(inode)->i_es_tree;
1768 	tree->cache_es = NULL;
1769 	node = rb_first(&tree->root);
1770 	while (node) {
1771 		es = rb_entry(node, struct extent_status, rb_node);
1772 		node = rb_next(node);
1773 		if (!ext4_es_is_delayed(es)) {
1774 			rb_erase(&es->rb_node, &tree->root);
1775 			ext4_es_free_extent(inode, es);
1776 		}
1777 	}
1778 	ext4_clear_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
1779 	write_unlock(&ei->i_es_lock);
1780 }
1781 
1782 #ifdef ES_DEBUG__
1783 static void ext4_print_pending_tree(struct inode *inode)
1784 {
1785 	struct ext4_pending_tree *tree;
1786 	struct rb_node *node;
1787 	struct pending_reservation *pr;
1788 
1789 	printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino);
1790 	tree = &EXT4_I(inode)->i_pending_tree;
1791 	node = rb_first(&tree->root);
1792 	while (node) {
1793 		pr = rb_entry(node, struct pending_reservation, rb_node);
1794 		printk(KERN_DEBUG " %u", pr->lclu);
1795 		node = rb_next(node);
1796 	}
1797 	printk(KERN_DEBUG "\n");
1798 }
1799 #else
1800 #define ext4_print_pending_tree(inode)
1801 #endif
1802 
1803 int __init ext4_init_pending(void)
1804 {
1805 	ext4_pending_cachep = KMEM_CACHE(pending_reservation, SLAB_RECLAIM_ACCOUNT);
1806 	if (ext4_pending_cachep == NULL)
1807 		return -ENOMEM;
1808 	return 0;
1809 }
1810 
1811 void ext4_exit_pending(void)
1812 {
1813 	kmem_cache_destroy(ext4_pending_cachep);
1814 }
1815 
1816 void ext4_init_pending_tree(struct ext4_pending_tree *tree)
1817 {
1818 	tree->root = RB_ROOT;
1819 }
1820 
1821 /*
1822  * __get_pending - retrieve a pointer to a pending reservation
1823  *
1824  * @inode - file containing the pending cluster reservation
1825  * @lclu - logical cluster of interest
1826  *
1827  * Returns a pointer to a pending reservation if it's a member of
1828  * the set, and NULL if not.  Must be called holding i_es_lock.
1829  */
1830 static struct pending_reservation *__get_pending(struct inode *inode,
1831 						 ext4_lblk_t lclu)
1832 {
1833 	struct ext4_pending_tree *tree;
1834 	struct rb_node *node;
1835 	struct pending_reservation *pr = NULL;
1836 
1837 	tree = &EXT4_I(inode)->i_pending_tree;
1838 	node = (&tree->root)->rb_node;
1839 
1840 	while (node) {
1841 		pr = rb_entry(node, struct pending_reservation, rb_node);
1842 		if (lclu < pr->lclu)
1843 			node = node->rb_left;
1844 		else if (lclu > pr->lclu)
1845 			node = node->rb_right;
1846 		else if (lclu == pr->lclu)
1847 			return pr;
1848 	}
1849 	return NULL;
1850 }
1851 
1852 /*
1853  * __insert_pending - adds a pending cluster reservation to the set of
1854  *                    pending reservations
1855  *
1856  * @inode - file containing the cluster
1857  * @lblk - logical block in the cluster to be added
1858  *
1859  * Returns 0 on successful insertion and -ENOMEM on failure.  If the
1860  * pending reservation is already in the set, returns successfully.
1861  */
1862 static int __insert_pending(struct inode *inode, ext4_lblk_t lblk)
1863 {
1864 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1865 	struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1866 	struct rb_node **p = &tree->root.rb_node;
1867 	struct rb_node *parent = NULL;
1868 	struct pending_reservation *pr;
1869 	ext4_lblk_t lclu;
1870 	int ret = 0;
1871 
1872 	lclu = EXT4_B2C(sbi, lblk);
1873 	/* search to find parent for insertion */
1874 	while (*p) {
1875 		parent = *p;
1876 		pr = rb_entry(parent, struct pending_reservation, rb_node);
1877 
1878 		if (lclu < pr->lclu) {
1879 			p = &(*p)->rb_left;
1880 		} else if (lclu > pr->lclu) {
1881 			p = &(*p)->rb_right;
1882 		} else {
1883 			/* pending reservation already inserted */
1884 			goto out;
1885 		}
1886 	}
1887 
1888 	pr = kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC);
1889 	if (pr == NULL) {
1890 		ret = -ENOMEM;
1891 		goto out;
1892 	}
1893 	pr->lclu = lclu;
1894 
1895 	rb_link_node(&pr->rb_node, parent, p);
1896 	rb_insert_color(&pr->rb_node, &tree->root);
1897 
1898 out:
1899 	return ret;
1900 }
1901 
1902 /*
1903  * __remove_pending - removes a pending cluster reservation from the set
1904  *                    of pending reservations
1905  *
1906  * @inode - file containing the cluster
1907  * @lblk - logical block in the pending cluster reservation to be removed
1908  *
1909  * Returns successfully if pending reservation is not a member of the set.
1910  */
1911 static void __remove_pending(struct inode *inode, ext4_lblk_t lblk)
1912 {
1913 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1914 	struct pending_reservation *pr;
1915 	struct ext4_pending_tree *tree;
1916 
1917 	pr = __get_pending(inode, EXT4_B2C(sbi, lblk));
1918 	if (pr != NULL) {
1919 		tree = &EXT4_I(inode)->i_pending_tree;
1920 		rb_erase(&pr->rb_node, &tree->root);
1921 		kmem_cache_free(ext4_pending_cachep, pr);
1922 	}
1923 }
1924 
1925 /*
1926  * ext4_remove_pending - removes a pending cluster reservation from the set
1927  *                       of pending reservations
1928  *
1929  * @inode - file containing the cluster
1930  * @lblk - logical block in the pending cluster reservation to be removed
1931  *
1932  * Locking for external use of __remove_pending.
1933  */
1934 void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk)
1935 {
1936 	struct ext4_inode_info *ei = EXT4_I(inode);
1937 
1938 	write_lock(&ei->i_es_lock);
1939 	__remove_pending(inode, lblk);
1940 	write_unlock(&ei->i_es_lock);
1941 }
1942 
1943 /*
1944  * ext4_is_pending - determine whether a cluster has a pending reservation
1945  *                   on it
1946  *
1947  * @inode - file containing the cluster
1948  * @lblk - logical block in the cluster
1949  *
1950  * Returns true if there's a pending reservation for the cluster in the
1951  * set of pending reservations, and false if not.
1952  */
1953 bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk)
1954 {
1955 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1956 	struct ext4_inode_info *ei = EXT4_I(inode);
1957 	bool ret;
1958 
1959 	read_lock(&ei->i_es_lock);
1960 	ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL);
1961 	read_unlock(&ei->i_es_lock);
1962 
1963 	return ret;
1964 }
1965 
1966 /*
1967  * ext4_es_insert_delayed_block - adds a delayed block to the extents status
1968  *                                tree, adding a pending reservation where
1969  *                                needed
1970  *
1971  * @inode - file containing the newly added block
1972  * @lblk - logical block to be added
1973  * @allocated - indicates whether a physical cluster has been allocated for
1974  *              the logical cluster that contains the block
1975  *
1976  * Returns 0 on success, negative error code on failure.
1977  */
1978 int ext4_es_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk,
1979 				 bool allocated)
1980 {
1981 	struct extent_status newes;
1982 	int err = 0;
1983 
1984 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
1985 		return 0;
1986 
1987 	es_debug("add [%u/1) delayed to extent status tree of inode %lu\n",
1988 		 lblk, inode->i_ino);
1989 
1990 	newes.es_lblk = lblk;
1991 	newes.es_len = 1;
1992 	ext4_es_store_pblock_status(&newes, ~0, EXTENT_STATUS_DELAYED);
1993 	trace_ext4_es_insert_delayed_block(inode, &newes, allocated);
1994 
1995 	ext4_es_insert_extent_check(inode, &newes);
1996 
1997 	write_lock(&EXT4_I(inode)->i_es_lock);
1998 
1999 	err = __es_remove_extent(inode, lblk, lblk, NULL);
2000 	if (err != 0)
2001 		goto error;
2002 retry:
2003 	err = __es_insert_extent(inode, &newes);
2004 	if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
2005 					  128, EXT4_I(inode)))
2006 		goto retry;
2007 	if (err != 0)
2008 		goto error;
2009 
2010 	if (allocated)
2011 		__insert_pending(inode, lblk);
2012 
2013 error:
2014 	write_unlock(&EXT4_I(inode)->i_es_lock);
2015 
2016 	ext4_es_print_tree(inode);
2017 	ext4_print_pending_tree(inode);
2018 
2019 	return err;
2020 }
2021 
2022 /*
2023  * __es_delayed_clu - count number of clusters containing blocks that
2024  *                    are delayed only
2025  *
2026  * @inode - file containing block range
2027  * @start - logical block defining start of range
2028  * @end - logical block defining end of range
2029  *
2030  * Returns the number of clusters containing only delayed (not delayed
2031  * and unwritten) blocks in the range specified by @start and @end.  Any
2032  * cluster or part of a cluster within the range and containing a delayed
2033  * and not unwritten block within the range is counted as a whole cluster.
2034  */
2035 static unsigned int __es_delayed_clu(struct inode *inode, ext4_lblk_t start,
2036 				     ext4_lblk_t end)
2037 {
2038 	struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
2039 	struct extent_status *es;
2040 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2041 	struct rb_node *node;
2042 	ext4_lblk_t first_lclu, last_lclu;
2043 	unsigned long long last_counted_lclu;
2044 	unsigned int n = 0;
2045 
2046 	/* guaranteed to be unequal to any ext4_lblk_t value */
2047 	last_counted_lclu = ~0ULL;
2048 
2049 	es = __es_tree_search(&tree->root, start);
2050 
2051 	while (es && (es->es_lblk <= end)) {
2052 		if (ext4_es_is_delonly(es)) {
2053 			if (es->es_lblk <= start)
2054 				first_lclu = EXT4_B2C(sbi, start);
2055 			else
2056 				first_lclu = EXT4_B2C(sbi, es->es_lblk);
2057 
2058 			if (ext4_es_end(es) >= end)
2059 				last_lclu = EXT4_B2C(sbi, end);
2060 			else
2061 				last_lclu = EXT4_B2C(sbi, ext4_es_end(es));
2062 
2063 			if (first_lclu == last_counted_lclu)
2064 				n += last_lclu - first_lclu;
2065 			else
2066 				n += last_lclu - first_lclu + 1;
2067 			last_counted_lclu = last_lclu;
2068 		}
2069 		node = rb_next(&es->rb_node);
2070 		if (!node)
2071 			break;
2072 		es = rb_entry(node, struct extent_status, rb_node);
2073 	}
2074 
2075 	return n;
2076 }
2077 
2078 /*
2079  * ext4_es_delayed_clu - count number of clusters containing blocks that
2080  *                       are both delayed and unwritten
2081  *
2082  * @inode - file containing block range
2083  * @lblk - logical block defining start of range
2084  * @len - number of blocks in range
2085  *
2086  * Locking for external use of __es_delayed_clu().
2087  */
2088 unsigned int ext4_es_delayed_clu(struct inode *inode, ext4_lblk_t lblk,
2089 				 ext4_lblk_t len)
2090 {
2091 	struct ext4_inode_info *ei = EXT4_I(inode);
2092 	ext4_lblk_t end;
2093 	unsigned int n;
2094 
2095 	if (len == 0)
2096 		return 0;
2097 
2098 	end = lblk + len - 1;
2099 	WARN_ON(end < lblk);
2100 
2101 	read_lock(&ei->i_es_lock);
2102 
2103 	n = __es_delayed_clu(inode, lblk, end);
2104 
2105 	read_unlock(&ei->i_es_lock);
2106 
2107 	return n;
2108 }
2109 
2110 /*
2111  * __revise_pending - makes, cancels, or leaves unchanged pending cluster
2112  *                    reservations for a specified block range depending
2113  *                    upon the presence or absence of delayed blocks
2114  *                    outside the range within clusters at the ends of the
2115  *                    range
2116  *
2117  * @inode - file containing the range
2118  * @lblk - logical block defining the start of range
2119  * @len  - length of range in blocks
2120  *
2121  * Used after a newly allocated extent is added to the extents status tree.
2122  * Requires that the extents in the range have either written or unwritten
2123  * status.  Must be called while holding i_es_lock.
2124  */
2125 static void __revise_pending(struct inode *inode, ext4_lblk_t lblk,
2126 			     ext4_lblk_t len)
2127 {
2128 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2129 	ext4_lblk_t end = lblk + len - 1;
2130 	ext4_lblk_t first, last;
2131 	bool f_del = false, l_del = false;
2132 
2133 	if (len == 0)
2134 		return;
2135 
2136 	/*
2137 	 * Two cases - block range within single cluster and block range
2138 	 * spanning two or more clusters.  Note that a cluster belonging
2139 	 * to a range starting and/or ending on a cluster boundary is treated
2140 	 * as if it does not contain a delayed extent.  The new range may
2141 	 * have allocated space for previously delayed blocks out to the
2142 	 * cluster boundary, requiring that any pre-existing pending
2143 	 * reservation be canceled.  Because this code only looks at blocks
2144 	 * outside the range, it should revise pending reservations
2145 	 * correctly even if the extent represented by the range can't be
2146 	 * inserted in the extents status tree due to ENOSPC.
2147 	 */
2148 
2149 	if (EXT4_B2C(sbi, lblk) == EXT4_B2C(sbi, end)) {
2150 		first = EXT4_LBLK_CMASK(sbi, lblk);
2151 		if (first != lblk)
2152 			f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2153 						first, lblk - 1);
2154 		if (f_del) {
2155 			__insert_pending(inode, first);
2156 		} else {
2157 			last = EXT4_LBLK_CMASK(sbi, end) +
2158 			       sbi->s_cluster_ratio - 1;
2159 			if (last != end)
2160 				l_del = __es_scan_range(inode,
2161 							&ext4_es_is_delonly,
2162 							end + 1, last);
2163 			if (l_del)
2164 				__insert_pending(inode, last);
2165 			else
2166 				__remove_pending(inode, last);
2167 		}
2168 	} else {
2169 		first = EXT4_LBLK_CMASK(sbi, lblk);
2170 		if (first != lblk)
2171 			f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2172 						first, lblk - 1);
2173 		if (f_del)
2174 			__insert_pending(inode, first);
2175 		else
2176 			__remove_pending(inode, first);
2177 
2178 		last = EXT4_LBLK_CMASK(sbi, end) + sbi->s_cluster_ratio - 1;
2179 		if (last != end)
2180 			l_del = __es_scan_range(inode, &ext4_es_is_delonly,
2181 						end + 1, last);
2182 		if (l_del)
2183 			__insert_pending(inode, last);
2184 		else
2185 			__remove_pending(inode, last);
2186 	}
2187 }
2188