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