]>
Commit | Line | Data |
---|---|---|
654598be ZL |
1 | /* |
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 | ||
992e9fdd ZL |
17 | #include <trace/events/ext4.h> |
18 | ||
654598be ZL |
19 | /* |
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 | |
06b0c886 | 26 | * extent tree, whose goal is only track delayed extents in memory to |
654598be ZL |
27 | * simplify the implementation of fiemap and bigalloc, and introduce |
28 | * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called | |
06b0c886 ZL |
29 | * delay extent tree at the first commit. But for better understand |
30 | * what it does, it has been rename to extent status tree. | |
654598be | 31 | * |
06b0c886 ZL |
32 | * 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 | |
654598be ZL |
36 | * 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. | |
06b0c886 ZL |
41 | * |
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. | |
654598be ZL |
54 | */ |
55 | ||
56 | /* | |
06b0c886 | 57 | * Extent status tree implementation for ext4. |
654598be ZL |
58 | * |
59 | * | |
60 | * ========================================================================== | |
06b0c886 | 61 | * Extent status tree tracks all extent status. |
654598be | 62 | * |
06b0c886 | 63 | * 1. Why we need to implement extent status tree? |
654598be | 64 | * |
06b0c886 | 65 | * Without extent status tree, ext4 identifies a delayed extent by looking |
654598be ZL |
66 | * up page cache, this has several deficiencies - complicated, buggy, |
67 | * and inefficient code. | |
68 | * | |
06b0c886 ZL |
69 | * 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. | |
654598be | 71 | * |
06b0c886 | 72 | * Let us have a look at how they do without extent status tree. |
654598be ZL |
73 | * -- 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 | * | |
654598be ZL |
84 | * -- 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 | * | |
06b0c886 | 89 | * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA, |
654598be ZL |
90 | * 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 | |
06b0c886 | 92 | * not by searching the extent tree. |
654598be ZL |
93 | * |
94 | * | |
95 | * ========================================================================== | |
06b0c886 ZL |
96 | * 2. Ext4 extent status tree impelmentation |
97 | * | |
98 | * -- 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. | |
654598be | 104 | * |
06b0c886 ZL |
105 | * -- 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. | |
654598be | 109 | * |
06b0c886 ZL |
110 | * -- 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. | |
654598be | 113 | * |
06b0c886 ZL |
114 | * -- race on a extent status tree |
115 | * Extent status tree is protected by inode->i_es_lock. | |
654598be | 116 | * |
06b0c886 ZL |
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. | |
654598be ZL |
121 | * |
122 | * | |
123 | * ========================================================================== | |
06b0c886 ZL |
124 | * 3. Performance analysis |
125 | * | |
654598be ZL |
126 | * -- 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 | |
654598be | 137 | * |
06b0c886 | 138 | * -- Refactor delayed space reservation |
654598be ZL |
139 | * |
140 | * -- Extent-level locking | |
141 | */ | |
142 | ||
143 | static struct kmem_cache *ext4_es_cachep; | |
144 | ||
bdedbb7b ZL |
145 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes); |
146 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, | |
06b0c886 ZL |
147 | ext4_lblk_t end); |
148 | ||
654598be ZL |
149 | int __init ext4_init_es(void) |
150 | { | |
151 | ext4_es_cachep = KMEM_CACHE(extent_status, SLAB_RECLAIM_ACCOUNT); | |
152 | if (ext4_es_cachep == NULL) | |
153 | return -ENOMEM; | |
154 | return 0; | |
155 | } | |
156 | ||
157 | void ext4_exit_es(void) | |
158 | { | |
159 | if (ext4_es_cachep) | |
160 | kmem_cache_destroy(ext4_es_cachep); | |
161 | } | |
162 | ||
163 | void ext4_es_init_tree(struct ext4_es_tree *tree) | |
164 | { | |
165 | tree->root = RB_ROOT; | |
166 | tree->cache_es = NULL; | |
167 | } | |
168 | ||
169 | #ifdef ES_DEBUG__ | |
170 | static void ext4_es_print_tree(struct inode *inode) | |
171 | { | |
172 | struct ext4_es_tree *tree; | |
173 | struct rb_node *node; | |
174 | ||
175 | printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino); | |
176 | tree = &EXT4_I(inode)->i_es_tree; | |
177 | node = rb_first(&tree->root); | |
178 | while (node) { | |
179 | struct extent_status *es; | |
180 | es = rb_entry(node, struct extent_status, rb_node); | |
fdc0212e ZL |
181 | printk(KERN_DEBUG " [%u/%u) %llu %llx", |
182 | es->es_lblk, es->es_len, | |
183 | ext4_es_pblock(es), ext4_es_status(es)); | |
654598be ZL |
184 | node = rb_next(node); |
185 | } | |
186 | printk(KERN_DEBUG "\n"); | |
187 | } | |
188 | #else | |
189 | #define ext4_es_print_tree(inode) | |
190 | #endif | |
191 | ||
06b0c886 | 192 | static inline ext4_lblk_t ext4_es_end(struct extent_status *es) |
654598be | 193 | { |
06b0c886 ZL |
194 | BUG_ON(es->es_lblk + es->es_len < es->es_lblk); |
195 | return es->es_lblk + es->es_len - 1; | |
654598be ZL |
196 | } |
197 | ||
198 | /* | |
199 | * search through the tree for an delayed extent with a given offset. If | |
200 | * it can't be found, try to find next extent. | |
201 | */ | |
202 | static struct extent_status *__es_tree_search(struct rb_root *root, | |
06b0c886 | 203 | ext4_lblk_t lblk) |
654598be ZL |
204 | { |
205 | struct rb_node *node = root->rb_node; | |
206 | struct extent_status *es = NULL; | |
207 | ||
208 | while (node) { | |
209 | es = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 | 210 | if (lblk < es->es_lblk) |
654598be | 211 | node = node->rb_left; |
06b0c886 | 212 | else if (lblk > ext4_es_end(es)) |
654598be ZL |
213 | node = node->rb_right; |
214 | else | |
215 | return es; | |
216 | } | |
217 | ||
06b0c886 | 218 | if (es && lblk < es->es_lblk) |
654598be ZL |
219 | return es; |
220 | ||
06b0c886 | 221 | if (es && lblk > ext4_es_end(es)) { |
654598be ZL |
222 | node = rb_next(&es->rb_node); |
223 | return node ? rb_entry(node, struct extent_status, rb_node) : | |
224 | NULL; | |
225 | } | |
226 | ||
227 | return NULL; | |
228 | } | |
229 | ||
230 | /* | |
be401363 | 231 | * ext4_es_find_delayed_extent: find the 1st delayed extent covering @es->lblk |
06b0c886 | 232 | * if it exists, otherwise, the next extent after @es->lblk. |
654598be ZL |
233 | * |
234 | * @inode: the inode which owns delayed extents | |
be401363 | 235 | * @lblk: the offset where we start to search |
654598be | 236 | * @es: delayed extent that we found |
654598be | 237 | */ |
be401363 ZL |
238 | void ext4_es_find_delayed_extent(struct inode *inode, ext4_lblk_t lblk, |
239 | struct extent_status *es) | |
654598be ZL |
240 | { |
241 | struct ext4_es_tree *tree = NULL; | |
242 | struct extent_status *es1 = NULL; | |
243 | struct rb_node *node; | |
654598be | 244 | |
be401363 ZL |
245 | BUG_ON(es == NULL); |
246 | trace_ext4_es_find_delayed_extent_enter(inode, lblk); | |
992e9fdd | 247 | |
654598be ZL |
248 | read_lock(&EXT4_I(inode)->i_es_lock); |
249 | tree = &EXT4_I(inode)->i_es_tree; | |
250 | ||
fdc0212e | 251 | /* find extent in cache firstly */ |
be401363 | 252 | es->es_lblk = es->es_len = es->es_pblk = 0; |
654598be ZL |
253 | if (tree->cache_es) { |
254 | es1 = tree->cache_es; | |
be401363 | 255 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
fdc0212e | 256 | es_debug("%u cached by [%u/%u) %llu %llx\n", |
be401363 | 257 | lblk, es1->es_lblk, es1->es_len, |
fdc0212e | 258 | ext4_es_pblock(es1), ext4_es_status(es1)); |
654598be ZL |
259 | goto out; |
260 | } | |
261 | } | |
262 | ||
be401363 | 263 | es1 = __es_tree_search(&tree->root, lblk); |
654598be ZL |
264 | |
265 | out: | |
be401363 ZL |
266 | if (es1 && !ext4_es_is_delayed(es1)) { |
267 | while ((node = rb_next(&es1->rb_node)) != NULL) { | |
268 | es1 = rb_entry(node, struct extent_status, rb_node); | |
269 | if (ext4_es_is_delayed(es1)) | |
270 | break; | |
271 | } | |
272 | } | |
273 | ||
274 | if (es1 && ext4_es_is_delayed(es1)) { | |
654598be | 275 | tree->cache_es = es1; |
06b0c886 ZL |
276 | es->es_lblk = es1->es_lblk; |
277 | es->es_len = es1->es_len; | |
fdc0212e | 278 | es->es_pblk = es1->es_pblk; |
654598be ZL |
279 | } |
280 | ||
281 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
992e9fdd | 282 | |
be401363 | 283 | trace_ext4_es_find_delayed_extent_exit(inode, es); |
654598be ZL |
284 | } |
285 | ||
286 | static struct extent_status * | |
bdedbb7b ZL |
287 | ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len, |
288 | ext4_fsblk_t pblk) | |
654598be ZL |
289 | { |
290 | struct extent_status *es; | |
291 | es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC); | |
292 | if (es == NULL) | |
293 | return NULL; | |
06b0c886 ZL |
294 | es->es_lblk = lblk; |
295 | es->es_len = len; | |
fdc0212e | 296 | es->es_pblk = pblk; |
654598be ZL |
297 | return es; |
298 | } | |
299 | ||
bdedbb7b | 300 | static void ext4_es_free_extent(struct inode *inode, struct extent_status *es) |
654598be ZL |
301 | { |
302 | kmem_cache_free(ext4_es_cachep, es); | |
303 | } | |
304 | ||
06b0c886 ZL |
305 | /* |
306 | * Check whether or not two extents can be merged | |
307 | * Condition: | |
308 | * - logical block number is contiguous | |
fdc0212e ZL |
309 | * - physical block number is contiguous |
310 | * - status is equal | |
06b0c886 ZL |
311 | */ |
312 | static int ext4_es_can_be_merged(struct extent_status *es1, | |
313 | struct extent_status *es2) | |
314 | { | |
315 | if (es1->es_lblk + es1->es_len != es2->es_lblk) | |
316 | return 0; | |
317 | ||
fdc0212e ZL |
318 | if (ext4_es_status(es1) != ext4_es_status(es2)) |
319 | return 0; | |
320 | ||
321 | if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) && | |
322 | (ext4_es_pblock(es1) + es1->es_len != ext4_es_pblock(es2))) | |
323 | return 0; | |
324 | ||
06b0c886 ZL |
325 | return 1; |
326 | } | |
327 | ||
654598be | 328 | static struct extent_status * |
bdedbb7b | 329 | ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es) |
654598be | 330 | { |
bdedbb7b | 331 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
332 | struct extent_status *es1; |
333 | struct rb_node *node; | |
334 | ||
335 | node = rb_prev(&es->rb_node); | |
336 | if (!node) | |
337 | return es; | |
338 | ||
339 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
340 | if (ext4_es_can_be_merged(es1, es)) { |
341 | es1->es_len += es->es_len; | |
654598be | 342 | rb_erase(&es->rb_node, &tree->root); |
bdedbb7b | 343 | ext4_es_free_extent(inode, es); |
654598be ZL |
344 | es = es1; |
345 | } | |
346 | ||
347 | return es; | |
348 | } | |
349 | ||
350 | static struct extent_status * | |
bdedbb7b | 351 | ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es) |
654598be | 352 | { |
bdedbb7b | 353 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
354 | struct extent_status *es1; |
355 | struct rb_node *node; | |
356 | ||
357 | node = rb_next(&es->rb_node); | |
358 | if (!node) | |
359 | return es; | |
360 | ||
361 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
362 | if (ext4_es_can_be_merged(es, es1)) { |
363 | es->es_len += es1->es_len; | |
654598be | 364 | rb_erase(node, &tree->root); |
bdedbb7b | 365 | ext4_es_free_extent(inode, es1); |
654598be ZL |
366 | } |
367 | ||
368 | return es; | |
369 | } | |
370 | ||
bdedbb7b | 371 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes) |
654598be | 372 | { |
bdedbb7b | 373 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
374 | struct rb_node **p = &tree->root.rb_node; |
375 | struct rb_node *parent = NULL; | |
376 | struct extent_status *es; | |
654598be ZL |
377 | |
378 | while (*p) { | |
379 | parent = *p; | |
380 | es = rb_entry(parent, struct extent_status, rb_node); | |
381 | ||
06b0c886 ZL |
382 | if (newes->es_lblk < es->es_lblk) { |
383 | if (ext4_es_can_be_merged(newes, es)) { | |
384 | /* | |
385 | * Here we can modify es_lblk directly | |
386 | * because it isn't overlapped. | |
387 | */ | |
388 | es->es_lblk = newes->es_lblk; | |
389 | es->es_len += newes->es_len; | |
fdc0212e ZL |
390 | if (ext4_es_is_written(es) || |
391 | ext4_es_is_unwritten(es)) | |
392 | ext4_es_store_pblock(es, | |
393 | newes->es_pblk); | |
bdedbb7b | 394 | es = ext4_es_try_to_merge_left(inode, es); |
654598be ZL |
395 | goto out; |
396 | } | |
397 | p = &(*p)->rb_left; | |
06b0c886 ZL |
398 | } else if (newes->es_lblk > ext4_es_end(es)) { |
399 | if (ext4_es_can_be_merged(es, newes)) { | |
400 | es->es_len += newes->es_len; | |
bdedbb7b | 401 | es = ext4_es_try_to_merge_right(inode, es); |
654598be ZL |
402 | goto out; |
403 | } | |
404 | p = &(*p)->rb_right; | |
405 | } else { | |
06b0c886 ZL |
406 | BUG_ON(1); |
407 | return -EINVAL; | |
654598be ZL |
408 | } |
409 | } | |
410 | ||
bdedbb7b | 411 | es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len, |
fdc0212e | 412 | newes->es_pblk); |
654598be ZL |
413 | if (!es) |
414 | return -ENOMEM; | |
415 | rb_link_node(&es->rb_node, parent, p); | |
416 | rb_insert_color(&es->rb_node, &tree->root); | |
417 | ||
418 | out: | |
419 | tree->cache_es = es; | |
420 | return 0; | |
421 | } | |
422 | ||
423 | /* | |
06b0c886 | 424 | * ext4_es_insert_extent() adds a space to a extent status tree. |
654598be ZL |
425 | * |
426 | * ext4_es_insert_extent is called by ext4_da_write_begin and | |
427 | * ext4_es_remove_extent. | |
428 | * | |
429 | * Return 0 on success, error code on failure. | |
430 | */ | |
06b0c886 | 431 | int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk, |
fdc0212e ZL |
432 | ext4_lblk_t len, ext4_fsblk_t pblk, |
433 | unsigned long long status) | |
654598be | 434 | { |
06b0c886 ZL |
435 | struct extent_status newes; |
436 | ext4_lblk_t end = lblk + len - 1; | |
654598be ZL |
437 | int err = 0; |
438 | ||
fdc0212e ZL |
439 | es_debug("add [%u/%u) %llu %llx to extent status tree of inode %lu\n", |
440 | lblk, len, pblk, status, inode->i_ino); | |
06b0c886 ZL |
441 | |
442 | BUG_ON(end < lblk); | |
443 | ||
444 | newes.es_lblk = lblk; | |
445 | newes.es_len = len; | |
fdc0212e ZL |
446 | ext4_es_store_pblock(&newes, pblk); |
447 | ext4_es_store_status(&newes, status); | |
448 | trace_ext4_es_insert_extent(inode, &newes); | |
654598be ZL |
449 | |
450 | write_lock(&EXT4_I(inode)->i_es_lock); | |
bdedbb7b | 451 | err = __es_remove_extent(inode, lblk, end); |
06b0c886 ZL |
452 | if (err != 0) |
453 | goto error; | |
bdedbb7b | 454 | err = __es_insert_extent(inode, &newes); |
06b0c886 ZL |
455 | |
456 | error: | |
654598be ZL |
457 | write_unlock(&EXT4_I(inode)->i_es_lock); |
458 | ||
459 | ext4_es_print_tree(inode); | |
460 | ||
461 | return err; | |
462 | } | |
463 | ||
d100eef2 ZL |
464 | /* |
465 | * ext4_es_lookup_extent() looks up an extent in extent status tree. | |
466 | * | |
467 | * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks. | |
468 | * | |
469 | * Return: 1 on found, 0 on not | |
470 | */ | |
471 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, | |
472 | struct extent_status *es) | |
473 | { | |
474 | struct ext4_es_tree *tree; | |
475 | struct extent_status *es1 = NULL; | |
476 | struct rb_node *node; | |
477 | int found = 0; | |
478 | ||
479 | trace_ext4_es_lookup_extent_enter(inode, lblk); | |
480 | es_debug("lookup extent in block %u\n", lblk); | |
481 | ||
482 | tree = &EXT4_I(inode)->i_es_tree; | |
483 | read_lock(&EXT4_I(inode)->i_es_lock); | |
484 | ||
485 | /* find extent in cache firstly */ | |
486 | es->es_lblk = es->es_len = es->es_pblk = 0; | |
487 | if (tree->cache_es) { | |
488 | es1 = tree->cache_es; | |
489 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { | |
490 | es_debug("%u cached by [%u/%u)\n", | |
491 | lblk, es1->es_lblk, es1->es_len); | |
492 | found = 1; | |
493 | goto out; | |
494 | } | |
495 | } | |
496 | ||
497 | node = tree->root.rb_node; | |
498 | while (node) { | |
499 | es1 = rb_entry(node, struct extent_status, rb_node); | |
500 | if (lblk < es1->es_lblk) | |
501 | node = node->rb_left; | |
502 | else if (lblk > ext4_es_end(es1)) | |
503 | node = node->rb_right; | |
504 | else { | |
505 | found = 1; | |
506 | break; | |
507 | } | |
508 | } | |
509 | ||
510 | out: | |
511 | if (found) { | |
512 | BUG_ON(!es1); | |
513 | es->es_lblk = es1->es_lblk; | |
514 | es->es_len = es1->es_len; | |
515 | es->es_pblk = es1->es_pblk; | |
516 | } | |
517 | ||
518 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
519 | ||
520 | trace_ext4_es_lookup_extent_exit(inode, es, found); | |
521 | return found; | |
522 | } | |
523 | ||
bdedbb7b ZL |
524 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
525 | ext4_lblk_t end) | |
654598be | 526 | { |
bdedbb7b | 527 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be | 528 | struct rb_node *node; |
654598be ZL |
529 | struct extent_status *es; |
530 | struct extent_status orig_es; | |
06b0c886 | 531 | ext4_lblk_t len1, len2; |
fdc0212e | 532 | ext4_fsblk_t block; |
654598be ZL |
533 | int err = 0; |
534 | ||
06b0c886 | 535 | es = __es_tree_search(&tree->root, lblk); |
654598be ZL |
536 | if (!es) |
537 | goto out; | |
06b0c886 | 538 | if (es->es_lblk > end) |
654598be ZL |
539 | goto out; |
540 | ||
541 | /* Simply invalidate cache_es. */ | |
542 | tree->cache_es = NULL; | |
543 | ||
06b0c886 ZL |
544 | orig_es.es_lblk = es->es_lblk; |
545 | orig_es.es_len = es->es_len; | |
fdc0212e ZL |
546 | orig_es.es_pblk = es->es_pblk; |
547 | ||
06b0c886 ZL |
548 | len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0; |
549 | len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0; | |
654598be | 550 | if (len1 > 0) |
06b0c886 | 551 | es->es_len = len1; |
654598be ZL |
552 | if (len2 > 0) { |
553 | if (len1 > 0) { | |
06b0c886 ZL |
554 | struct extent_status newes; |
555 | ||
556 | newes.es_lblk = end + 1; | |
557 | newes.es_len = len2; | |
fdc0212e ZL |
558 | if (ext4_es_is_written(&orig_es) || |
559 | ext4_es_is_unwritten(&orig_es)) { | |
560 | block = ext4_es_pblock(&orig_es) + | |
561 | orig_es.es_len - len2; | |
562 | ext4_es_store_pblock(&newes, block); | |
563 | } | |
564 | ext4_es_store_status(&newes, ext4_es_status(&orig_es)); | |
bdedbb7b | 565 | err = __es_insert_extent(inode, &newes); |
654598be | 566 | if (err) { |
06b0c886 ZL |
567 | es->es_lblk = orig_es.es_lblk; |
568 | es->es_len = orig_es.es_len; | |
654598be ZL |
569 | goto out; |
570 | } | |
571 | } else { | |
06b0c886 ZL |
572 | es->es_lblk = end + 1; |
573 | es->es_len = len2; | |
fdc0212e ZL |
574 | if (ext4_es_is_written(es) || |
575 | ext4_es_is_unwritten(es)) { | |
576 | block = orig_es.es_pblk + orig_es.es_len - len2; | |
577 | ext4_es_store_pblock(es, block); | |
578 | } | |
654598be ZL |
579 | } |
580 | goto out; | |
581 | } | |
582 | ||
583 | if (len1 > 0) { | |
584 | node = rb_next(&es->rb_node); | |
585 | if (node) | |
586 | es = rb_entry(node, struct extent_status, rb_node); | |
587 | else | |
588 | es = NULL; | |
589 | } | |
590 | ||
06b0c886 | 591 | while (es && ext4_es_end(es) <= end) { |
654598be ZL |
592 | node = rb_next(&es->rb_node); |
593 | rb_erase(&es->rb_node, &tree->root); | |
bdedbb7b | 594 | ext4_es_free_extent(inode, es); |
654598be ZL |
595 | if (!node) { |
596 | es = NULL; | |
597 | break; | |
598 | } | |
599 | es = rb_entry(node, struct extent_status, rb_node); | |
600 | } | |
601 | ||
06b0c886 | 602 | if (es && es->es_lblk < end + 1) { |
fdc0212e ZL |
603 | ext4_lblk_t orig_len = es->es_len; |
604 | ||
06b0c886 ZL |
605 | len1 = ext4_es_end(es) - end; |
606 | es->es_lblk = end + 1; | |
607 | es->es_len = len1; | |
fdc0212e ZL |
608 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
609 | block = es->es_pblk + orig_len - len1; | |
610 | ext4_es_store_pblock(es, block); | |
611 | } | |
654598be ZL |
612 | } |
613 | ||
614 | out: | |
06b0c886 ZL |
615 | return err; |
616 | } | |
617 | ||
618 | /* | |
619 | * ext4_es_remove_extent() removes a space from a extent status tree. | |
620 | * | |
621 | * Return 0 on success, error code on failure. | |
622 | */ | |
623 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, | |
624 | ext4_lblk_t len) | |
625 | { | |
06b0c886 ZL |
626 | ext4_lblk_t end; |
627 | int err = 0; | |
628 | ||
629 | trace_ext4_es_remove_extent(inode, lblk, len); | |
630 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", | |
631 | lblk, len, inode->i_ino); | |
632 | ||
633 | end = lblk + len - 1; | |
634 | BUG_ON(end < lblk); | |
635 | ||
06b0c886 | 636 | write_lock(&EXT4_I(inode)->i_es_lock); |
bdedbb7b | 637 | err = __es_remove_extent(inode, lblk, end); |
654598be ZL |
638 | write_unlock(&EXT4_I(inode)->i_es_lock); |
639 | ext4_es_print_tree(inode); | |
640 | return err; | |
641 | } |