blob: c9921cf4f817a37cc0e93a634dd96070a9df4ee4 [file] [log] [blame]
Zheng Liu654598b2012-11-08 21:57:20 -05001/*
2 * fs/ext4/extents_status.c
3 *
4 * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
5 * Modified by
6 * Allison Henderson <achender@linux.vnet.ibm.com>
7 * Hugh Dickins <hughd@google.com>
8 * Zheng Liu <wenqing.lz@taobao.com>
9 *
10 * Ext4 extents status tree core functions.
11 */
12#include <linux/rbtree.h>
13#include "ext4.h"
14#include "extents_status.h"
15#include "ext4_extents.h"
16
Zheng Liu992e9fd2012-11-08 21:57:33 -050017#include <trace/events/ext4.h>
18
Zheng Liu654598b2012-11-08 21:57:20 -050019/*
20 * According to previous discussion in Ext4 Developer Workshop, we
21 * will introduce a new structure called io tree to track all extent
22 * status in order to solve some problems that we have met
23 * (e.g. Reservation space warning), and provide extent-level locking.
24 * Delay extent tree is the first step to achieve this goal. It is
25 * original built by Yongqiang Yang. At that time it is called delay
Zheng Liu06b0c882013-02-18 00:26:51 -050026 * extent tree, whose goal is only track delayed extents in memory to
Zheng Liu654598b2012-11-08 21:57:20 -050027 * simplify the implementation of fiemap and bigalloc, and introduce
28 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
Zheng Liu06b0c882013-02-18 00:26:51 -050029 * delay extent tree at the first commit. But for better understand
30 * what it does, it has been rename to extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050031 *
Zheng Liu06b0c882013-02-18 00:26:51 -050032 * Step1:
33 * Currently the first step has been done. All delayed extents are
34 * tracked in the tree. It maintains the delayed extent when a delayed
35 * allocation is issued, and the delayed extent is written out or
Zheng Liu654598b2012-11-08 21:57:20 -050036 * invalidated. Therefore the implementation of fiemap and bigalloc
37 * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
38 *
39 * The following comment describes the implemenmtation of extent
40 * status tree and future works.
Zheng Liu06b0c882013-02-18 00:26:51 -050041 *
42 * Step2:
43 * In this step all extent status are tracked by extent status tree.
44 * Thus, we can first try to lookup a block mapping in this tree before
45 * finding it in extent tree. Hence, single extent cache can be removed
46 * because extent status tree can do a better job. Extents in status
47 * tree are loaded on-demand. Therefore, the extent status tree may not
48 * contain all of the extents in a file. Meanwhile we define a shrinker
49 * to reclaim memory from extent status tree because fragmented extent
50 * tree will make status tree cost too much memory. written/unwritten/-
51 * hole extents in the tree will be reclaimed by this shrinker when we
52 * are under high memory pressure. Delayed extents will not be
53 * reclimed because fiemap, bigalloc, and seek_data/hole need it.
Zheng Liu654598b2012-11-08 21:57:20 -050054 */
55
56/*
Zheng Liu06b0c882013-02-18 00:26:51 -050057 * Extent status tree implementation for ext4.
Zheng Liu654598b2012-11-08 21:57:20 -050058 *
59 *
60 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050061 * Extent status tree tracks all extent status.
Zheng Liu654598b2012-11-08 21:57:20 -050062 *
Zheng Liu06b0c882013-02-18 00:26:51 -050063 * 1. Why we need to implement extent status tree?
Zheng Liu654598b2012-11-08 21:57:20 -050064 *
Zheng Liu06b0c882013-02-18 00:26:51 -050065 * Without extent status tree, ext4 identifies a delayed extent by looking
Zheng Liu654598b2012-11-08 21:57:20 -050066 * up page cache, this has several deficiencies - complicated, buggy,
67 * and inefficient code.
68 *
Zheng Liu06b0c882013-02-18 00:26:51 -050069 * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
70 * block or a range of blocks are belonged to a delayed extent.
Zheng Liu654598b2012-11-08 21:57:20 -050071 *
Zheng Liu06b0c882013-02-18 00:26:51 -050072 * Let us have a look at how they do without extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050073 * -- FIEMAP
74 * FIEMAP looks up page cache to identify delayed allocations from holes.
75 *
76 * -- SEEK_HOLE/DATA
77 * SEEK_HOLE/DATA has the same problem as FIEMAP.
78 *
79 * -- bigalloc
80 * bigalloc looks up page cache to figure out if a block is
81 * already under delayed allocation or not to determine whether
82 * quota reserving is needed for the cluster.
83 *
Zheng Liu654598b2012-11-08 21:57:20 -050084 * -- writeout
85 * Writeout looks up whole page cache to see if a buffer is
86 * mapped, If there are not very many delayed buffers, then it is
87 * time comsuming.
88 *
Zheng Liu06b0c882013-02-18 00:26:51 -050089 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
Zheng Liu654598b2012-11-08 21:57:20 -050090 * bigalloc and writeout can figure out if a block or a range of
91 * blocks is under delayed allocation(belonged to a delayed extent) or
Zheng Liu06b0c882013-02-18 00:26:51 -050092 * not by searching the extent tree.
Zheng Liu654598b2012-11-08 21:57:20 -050093 *
94 *
95 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050096 * 2. Ext4 extent status tree impelmentation
Zheng Liu654598b2012-11-08 21:57:20 -050097 *
Zheng Liu06b0c882013-02-18 00:26:51 -050098 * -- extent
99 * A extent is a range of blocks which are contiguous logically and
100 * physically. Unlike extent in extent tree, this extent in ext4 is
101 * a in-memory struct, there is no corresponding on-disk data. There
102 * is no limit on length of extent, so an extent can contain as many
103 * blocks as they are contiguous logically and physically.
Zheng Liu654598b2012-11-08 21:57:20 -0500104 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500105 * -- extent status tree
106 * Every inode has an extent status tree and all allocation blocks
107 * are added to the tree with different status. The extent in the
108 * tree are ordered by logical block no.
Zheng Liu654598b2012-11-08 21:57:20 -0500109 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500110 * -- operations on a extent status tree
111 * There are three important operations on a delayed extent tree: find
112 * next extent, adding a extent(a range of blocks) and removing a extent.
Zheng Liu654598b2012-11-08 21:57:20 -0500113 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500114 * -- race on a extent status tree
115 * Extent status tree is protected by inode->i_es_lock.
116 *
117 * -- memory consumption
118 * Fragmented extent tree will make extent status tree cost too much
119 * memory. Hence, we will reclaim written/unwritten/hole extents from
120 * the tree under a heavy memory pressure.
Zheng Liu654598b2012-11-08 21:57:20 -0500121 *
122 *
123 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -0500124 * 3. Performance analysis
125 *
Zheng Liu654598b2012-11-08 21:57:20 -0500126 * -- overhead
127 * 1. There is a cache extent for write access, so if writes are
128 * not very random, adding space operaions are in O(1) time.
129 *
130 * -- gain
131 * 2. Code is much simpler, more readable, more maintainable and
132 * more efficient.
133 *
134 *
135 * ==========================================================================
136 * 4. TODO list
Zheng Liu654598b2012-11-08 21:57:20 -0500137 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500138 * -- Refactor delayed space reservation
Zheng Liu654598b2012-11-08 21:57:20 -0500139 *
140 * -- Extent-level locking
141 */
142
143static struct kmem_cache *ext4_es_cachep;
144
Zheng Liu06b0c882013-02-18 00:26:51 -0500145static int __es_insert_extent(struct ext4_es_tree *tree,
146 struct extent_status *newes);
147static int __es_remove_extent(struct ext4_es_tree *tree, ext4_lblk_t lblk,
148 ext4_lblk_t end);
149
Zheng Liu654598b2012-11-08 21:57:20 -0500150int __init ext4_init_es(void)
151{
152 ext4_es_cachep = KMEM_CACHE(extent_status, SLAB_RECLAIM_ACCOUNT);
153 if (ext4_es_cachep == NULL)
154 return -ENOMEM;
155 return 0;
156}
157
158void ext4_exit_es(void)
159{
160 if (ext4_es_cachep)
161 kmem_cache_destroy(ext4_es_cachep);
162}
163
164void ext4_es_init_tree(struct ext4_es_tree *tree)
165{
166 tree->root = RB_ROOT;
167 tree->cache_es = NULL;
168}
169
170#ifdef ES_DEBUG__
171static void ext4_es_print_tree(struct inode *inode)
172{
173 struct ext4_es_tree *tree;
174 struct rb_node *node;
175
176 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
177 tree = &EXT4_I(inode)->i_es_tree;
178 node = rb_first(&tree->root);
179 while (node) {
180 struct extent_status *es;
181 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500182 printk(KERN_DEBUG " [%u/%u)", es->es_lblk, es->es_len);
Zheng Liu654598b2012-11-08 21:57:20 -0500183 node = rb_next(node);
184 }
185 printk(KERN_DEBUG "\n");
186}
187#else
188#define ext4_es_print_tree(inode)
189#endif
190
Zheng Liu06b0c882013-02-18 00:26:51 -0500191static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500192{
Zheng Liu06b0c882013-02-18 00:26:51 -0500193 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
194 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500195}
196
197/*
198 * search through the tree for an delayed extent with a given offset. If
199 * it can't be found, try to find next extent.
200 */
201static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500202 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500203{
204 struct rb_node *node = root->rb_node;
205 struct extent_status *es = NULL;
206
207 while (node) {
208 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500209 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500210 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500211 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500212 node = node->rb_right;
213 else
214 return es;
215 }
216
Zheng Liu06b0c882013-02-18 00:26:51 -0500217 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500218 return es;
219
Zheng Liu06b0c882013-02-18 00:26:51 -0500220 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500221 node = rb_next(&es->rb_node);
222 return node ? rb_entry(node, struct extent_status, rb_node) :
223 NULL;
224 }
225
226 return NULL;
227}
228
229/*
Zheng Liu06b0c882013-02-18 00:26:51 -0500230 * ext4_es_find_extent: find the 1st delayed extent covering @es->lblk
231 * if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500232 *
233 * @inode: the inode which owns delayed extents
234 * @es: delayed extent that we found
235 *
236 * Returns the first block of the next extent after es, otherwise
237 * EXT_MAX_BLOCKS if no delay extent is found.
238 * Delayed extent is returned via @es.
239 */
240ext4_lblk_t ext4_es_find_extent(struct inode *inode, struct extent_status *es)
241{
242 struct ext4_es_tree *tree = NULL;
243 struct extent_status *es1 = NULL;
244 struct rb_node *node;
245 ext4_lblk_t ret = EXT_MAX_BLOCKS;
246
Zheng Liu06b0c882013-02-18 00:26:51 -0500247 trace_ext4_es_find_extent_enter(inode, es->es_lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500248
Zheng Liu654598b2012-11-08 21:57:20 -0500249 read_lock(&EXT4_I(inode)->i_es_lock);
250 tree = &EXT4_I(inode)->i_es_tree;
251
252 /* find delay extent in cache firstly */
253 if (tree->cache_es) {
254 es1 = tree->cache_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500255 if (in_range(es->es_lblk, es1->es_lblk, es1->es_len)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500256 es_debug("%u cached by [%u/%u)\n",
Zheng Liu06b0c882013-02-18 00:26:51 -0500257 es->es_lblk, es1->es_lblk, es1->es_len);
Zheng Liu654598b2012-11-08 21:57:20 -0500258 goto out;
259 }
260 }
261
Zheng Liu06b0c882013-02-18 00:26:51 -0500262 es->es_len = 0;
263 es1 = __es_tree_search(&tree->root, es->es_lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500264
265out:
266 if (es1) {
267 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500268 es->es_lblk = es1->es_lblk;
269 es->es_len = es1->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500270 node = rb_next(&es1->rb_node);
271 if (node) {
272 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500273 ret = es1->es_lblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500274 }
275 }
276
277 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500278
279 trace_ext4_es_find_extent_exit(inode, es, ret);
Zheng Liu654598b2012-11-08 21:57:20 -0500280 return ret;
281}
282
283static struct extent_status *
Zheng Liu06b0c882013-02-18 00:26:51 -0500284ext4_es_alloc_extent(ext4_lblk_t lblk, ext4_lblk_t len)
Zheng Liu654598b2012-11-08 21:57:20 -0500285{
286 struct extent_status *es;
287 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
288 if (es == NULL)
289 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500290 es->es_lblk = lblk;
291 es->es_len = len;
Zheng Liu654598b2012-11-08 21:57:20 -0500292 return es;
293}
294
295static void ext4_es_free_extent(struct extent_status *es)
296{
297 kmem_cache_free(ext4_es_cachep, es);
298}
299
Zheng Liu06b0c882013-02-18 00:26:51 -0500300/*
301 * Check whether or not two extents can be merged
302 * Condition:
303 * - logical block number is contiguous
304 */
305static int ext4_es_can_be_merged(struct extent_status *es1,
306 struct extent_status *es2)
307{
308 if (es1->es_lblk + es1->es_len != es2->es_lblk)
309 return 0;
310
311 return 1;
312}
313
Zheng Liu654598b2012-11-08 21:57:20 -0500314static struct extent_status *
315ext4_es_try_to_merge_left(struct ext4_es_tree *tree, struct extent_status *es)
316{
317 struct extent_status *es1;
318 struct rb_node *node;
319
320 node = rb_prev(&es->rb_node);
321 if (!node)
322 return es;
323
324 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500325 if (ext4_es_can_be_merged(es1, es)) {
326 es1->es_len += es->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500327 rb_erase(&es->rb_node, &tree->root);
328 ext4_es_free_extent(es);
329 es = es1;
330 }
331
332 return es;
333}
334
335static struct extent_status *
336ext4_es_try_to_merge_right(struct ext4_es_tree *tree, struct extent_status *es)
337{
338 struct extent_status *es1;
339 struct rb_node *node;
340
341 node = rb_next(&es->rb_node);
342 if (!node)
343 return es;
344
345 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500346 if (ext4_es_can_be_merged(es, es1)) {
347 es->es_len += es1->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500348 rb_erase(node, &tree->root);
349 ext4_es_free_extent(es1);
350 }
351
352 return es;
353}
354
Zheng Liu06b0c882013-02-18 00:26:51 -0500355static int __es_insert_extent(struct ext4_es_tree *tree,
356 struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500357{
358 struct rb_node **p = &tree->root.rb_node;
359 struct rb_node *parent = NULL;
360 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500361
362 while (*p) {
363 parent = *p;
364 es = rb_entry(parent, struct extent_status, rb_node);
365
Zheng Liu06b0c882013-02-18 00:26:51 -0500366 if (newes->es_lblk < es->es_lblk) {
367 if (ext4_es_can_be_merged(newes, es)) {
368 /*
369 * Here we can modify es_lblk directly
370 * because it isn't overlapped.
371 */
372 es->es_lblk = newes->es_lblk;
373 es->es_len += newes->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500374 es = ext4_es_try_to_merge_left(tree, es);
375 goto out;
376 }
377 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500378 } else if (newes->es_lblk > ext4_es_end(es)) {
379 if (ext4_es_can_be_merged(es, newes)) {
380 es->es_len += newes->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500381 es = ext4_es_try_to_merge_right(tree, es);
382 goto out;
383 }
384 p = &(*p)->rb_right;
385 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500386 BUG_ON(1);
387 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500388 }
389 }
390
Zheng Liu06b0c882013-02-18 00:26:51 -0500391 es = ext4_es_alloc_extent(newes->es_lblk, newes->es_len);
Zheng Liu654598b2012-11-08 21:57:20 -0500392 if (!es)
393 return -ENOMEM;
394 rb_link_node(&es->rb_node, parent, p);
395 rb_insert_color(&es->rb_node, &tree->root);
396
397out:
398 tree->cache_es = es;
399 return 0;
400}
401
402/*
Zheng Liu06b0c882013-02-18 00:26:51 -0500403 * ext4_es_insert_extent() adds a space to a extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500404 *
405 * ext4_es_insert_extent is called by ext4_da_write_begin and
406 * ext4_es_remove_extent.
407 *
408 * Return 0 on success, error code on failure.
409 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500410int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liu654598b2012-11-08 21:57:20 -0500411 ext4_lblk_t len)
412{
413 struct ext4_es_tree *tree;
Zheng Liu06b0c882013-02-18 00:26:51 -0500414 struct extent_status newes;
415 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500416 int err = 0;
417
Zheng Liu06b0c882013-02-18 00:26:51 -0500418 trace_ext4_es_insert_extent(inode, lblk, len);
Zheng Liu654598b2012-11-08 21:57:20 -0500419 es_debug("add [%u/%u) to extent status tree of inode %lu\n",
Zheng Liu06b0c882013-02-18 00:26:51 -0500420 lblk, len, inode->i_ino);
421
422 BUG_ON(end < lblk);
423
424 newes.es_lblk = lblk;
425 newes.es_len = len;
Zheng Liu654598b2012-11-08 21:57:20 -0500426
427 write_lock(&EXT4_I(inode)->i_es_lock);
428 tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu06b0c882013-02-18 00:26:51 -0500429 err = __es_remove_extent(tree, lblk, end);
430 if (err != 0)
431 goto error;
432 err = __es_insert_extent(tree, &newes);
433
434error:
Zheng Liu654598b2012-11-08 21:57:20 -0500435 write_unlock(&EXT4_I(inode)->i_es_lock);
436
437 ext4_es_print_tree(inode);
438
439 return err;
440}
441
Zheng Liu06b0c882013-02-18 00:26:51 -0500442static int __es_remove_extent(struct ext4_es_tree *tree, ext4_lblk_t lblk,
443 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500444{
445 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500446 struct extent_status *es;
447 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500448 ext4_lblk_t len1, len2;
Zheng Liu654598b2012-11-08 21:57:20 -0500449 int err = 0;
450
Zheng Liu06b0c882013-02-18 00:26:51 -0500451 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500452 if (!es)
453 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500454 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500455 goto out;
456
457 /* Simply invalidate cache_es. */
458 tree->cache_es = NULL;
459
Zheng Liu06b0c882013-02-18 00:26:51 -0500460 orig_es.es_lblk = es->es_lblk;
461 orig_es.es_len = es->es_len;
462 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
463 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500464 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500465 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500466 if (len2 > 0) {
467 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500468 struct extent_status newes;
469
470 newes.es_lblk = end + 1;
471 newes.es_len = len2;
472 err = __es_insert_extent(tree, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500473 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500474 es->es_lblk = orig_es.es_lblk;
475 es->es_len = orig_es.es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500476 goto out;
477 }
478 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500479 es->es_lblk = end + 1;
480 es->es_len = len2;
Zheng Liu654598b2012-11-08 21:57:20 -0500481 }
482 goto out;
483 }
484
485 if (len1 > 0) {
486 node = rb_next(&es->rb_node);
487 if (node)
488 es = rb_entry(node, struct extent_status, rb_node);
489 else
490 es = NULL;
491 }
492
Zheng Liu06b0c882013-02-18 00:26:51 -0500493 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500494 node = rb_next(&es->rb_node);
495 rb_erase(&es->rb_node, &tree->root);
496 ext4_es_free_extent(es);
497 if (!node) {
498 es = NULL;
499 break;
500 }
501 es = rb_entry(node, struct extent_status, rb_node);
502 }
503
Zheng Liu06b0c882013-02-18 00:26:51 -0500504 if (es && es->es_lblk < end + 1) {
505 len1 = ext4_es_end(es) - end;
506 es->es_lblk = end + 1;
507 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500508 }
509
510out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500511 return err;
512}
513
514/*
515 * ext4_es_remove_extent() removes a space from a extent status tree.
516 *
517 * Return 0 on success, error code on failure.
518 */
519int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
520 ext4_lblk_t len)
521{
522 struct ext4_es_tree *tree;
523 ext4_lblk_t end;
524 int err = 0;
525
526 trace_ext4_es_remove_extent(inode, lblk, len);
527 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
528 lblk, len, inode->i_ino);
529
530 end = lblk + len - 1;
531 BUG_ON(end < lblk);
532
533 tree = &EXT4_I(inode)->i_es_tree;
534
535 write_lock(&EXT4_I(inode)->i_es_lock);
536 err = __es_remove_extent(tree, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500537 write_unlock(&EXT4_I(inode)->i_es_lock);
538 ext4_es_print_tree(inode);
539 return err;
540}