]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
654598be ZL |
2 | /* |
3 | * fs/ext4/extents_status.c | |
4 | * | |
5 | * Written by Yongqiang Yang <xiaoqiangnk@gmail.com> | |
6 | * Modified by | |
7 | * Allison Henderson <achender@linux.vnet.ibm.com> | |
8 | * Hugh Dickins <hughd@google.com> | |
9 | * Zheng Liu <wenqing.lz@taobao.com> | |
10 | * | |
11 | * Ext4 extents status tree core functions. | |
12 | */ | |
d3922a77 | 13 | #include <linux/list_sort.h> |
eb68d0e2 ZL |
14 | #include <linux/proc_fs.h> |
15 | #include <linux/seq_file.h> | |
654598be | 16 | #include "ext4.h" |
654598be | 17 | |
992e9fdd ZL |
18 | #include <trace/events/ext4.h> |
19 | ||
654598be ZL |
20 | /* |
21 | * According to previous discussion in Ext4 Developer Workshop, we | |
22 | * will introduce a new structure called io tree to track all extent | |
23 | * status in order to solve some problems that we have met | |
24 | * (e.g. Reservation space warning), and provide extent-level locking. | |
25 | * Delay extent tree is the first step to achieve this goal. It is | |
26 | * original built by Yongqiang Yang. At that time it is called delay | |
06b0c886 | 27 | * extent tree, whose goal is only track delayed extents in memory to |
654598be ZL |
28 | * simplify the implementation of fiemap and bigalloc, and introduce |
29 | * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called | |
06b0c886 ZL |
30 | * delay extent tree at the first commit. But for better understand |
31 | * what it does, it has been rename to extent status tree. | |
654598be | 32 | * |
06b0c886 ZL |
33 | * Step1: |
34 | * Currently the first step has been done. All delayed extents are | |
35 | * tracked in the tree. It maintains the delayed extent when a delayed | |
36 | * allocation is issued, and the delayed extent is written out or | |
654598be ZL |
37 | * invalidated. Therefore the implementation of fiemap and bigalloc |
38 | * are simplified, and SEEK_DATA/SEEK_HOLE are introduced. | |
39 | * | |
40 | * The following comment describes the implemenmtation of extent | |
41 | * status tree and future works. | |
06b0c886 ZL |
42 | * |
43 | * Step2: | |
44 | * In this step all extent status are tracked by extent status tree. | |
45 | * Thus, we can first try to lookup a block mapping in this tree before | |
46 | * finding it in extent tree. Hence, single extent cache can be removed | |
47 | * because extent status tree can do a better job. Extents in status | |
48 | * tree are loaded on-demand. Therefore, the extent status tree may not | |
49 | * contain all of the extents in a file. Meanwhile we define a shrinker | |
50 | * to reclaim memory from extent status tree because fragmented extent | |
51 | * tree will make status tree cost too much memory. written/unwritten/- | |
52 | * hole extents in the tree will be reclaimed by this shrinker when we | |
53 | * are under high memory pressure. Delayed extents will not be | |
54 | * reclimed because fiemap, bigalloc, and seek_data/hole need it. | |
654598be ZL |
55 | */ |
56 | ||
57 | /* | |
06b0c886 | 58 | * Extent status tree implementation for ext4. |
654598be ZL |
59 | * |
60 | * | |
61 | * ========================================================================== | |
06b0c886 | 62 | * Extent status tree tracks all extent status. |
654598be | 63 | * |
06b0c886 | 64 | * 1. Why we need to implement extent status tree? |
654598be | 65 | * |
06b0c886 | 66 | * Without extent status tree, ext4 identifies a delayed extent by looking |
654598be ZL |
67 | * up page cache, this has several deficiencies - complicated, buggy, |
68 | * and inefficient code. | |
69 | * | |
06b0c886 ZL |
70 | * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a |
71 | * block or a range of blocks are belonged to a delayed extent. | |
654598be | 72 | * |
06b0c886 | 73 | * Let us have a look at how they do without extent status tree. |
654598be ZL |
74 | * -- FIEMAP |
75 | * FIEMAP looks up page cache to identify delayed allocations from holes. | |
76 | * | |
77 | * -- SEEK_HOLE/DATA | |
78 | * SEEK_HOLE/DATA has the same problem as FIEMAP. | |
79 | * | |
80 | * -- bigalloc | |
81 | * bigalloc looks up page cache to figure out if a block is | |
82 | * already under delayed allocation or not to determine whether | |
83 | * quota reserving is needed for the cluster. | |
84 | * | |
654598be ZL |
85 | * -- writeout |
86 | * Writeout looks up whole page cache to see if a buffer is | |
87 | * mapped, If there are not very many delayed buffers, then it is | |
3f8b6fb7 | 88 | * time consuming. |
654598be | 89 | * |
06b0c886 | 90 | * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA, |
654598be ZL |
91 | * bigalloc and writeout can figure out if a block or a range of |
92 | * blocks is under delayed allocation(belonged to a delayed extent) or | |
06b0c886 | 93 | * not by searching the extent tree. |
654598be ZL |
94 | * |
95 | * | |
96 | * ========================================================================== | |
06b0c886 ZL |
97 | * 2. Ext4 extent status tree impelmentation |
98 | * | |
99 | * -- extent | |
100 | * A extent is a range of blocks which are contiguous logically and | |
101 | * physically. Unlike extent in extent tree, this extent in ext4 is | |
102 | * a in-memory struct, there is no corresponding on-disk data. There | |
103 | * is no limit on length of extent, so an extent can contain as many | |
104 | * blocks as they are contiguous logically and physically. | |
654598be | 105 | * |
06b0c886 ZL |
106 | * -- extent status tree |
107 | * Every inode has an extent status tree and all allocation blocks | |
108 | * are added to the tree with different status. The extent in the | |
109 | * tree are ordered by logical block no. | |
654598be | 110 | * |
06b0c886 ZL |
111 | * -- operations on a extent status tree |
112 | * There are three important operations on a delayed extent tree: find | |
113 | * next extent, adding a extent(a range of blocks) and removing a extent. | |
654598be | 114 | * |
06b0c886 ZL |
115 | * -- race on a extent status tree |
116 | * Extent status tree is protected by inode->i_es_lock. | |
654598be | 117 | * |
06b0c886 ZL |
118 | * -- memory consumption |
119 | * Fragmented extent tree will make extent status tree cost too much | |
120 | * memory. Hence, we will reclaim written/unwritten/hole extents from | |
121 | * the tree under a heavy memory pressure. | |
654598be ZL |
122 | * |
123 | * | |
124 | * ========================================================================== | |
06b0c886 ZL |
125 | * 3. Performance analysis |
126 | * | |
654598be ZL |
127 | * -- overhead |
128 | * 1. There is a cache extent for write access, so if writes are | |
129 | * not very random, adding space operaions are in O(1) time. | |
130 | * | |
131 | * -- gain | |
132 | * 2. Code is much simpler, more readable, more maintainable and | |
133 | * more efficient. | |
134 | * | |
135 | * | |
136 | * ========================================================================== | |
137 | * 4. TODO list | |
654598be | 138 | * |
06b0c886 | 139 | * -- Refactor delayed space reservation |
654598be ZL |
140 | * |
141 | * -- Extent-level locking | |
142 | */ | |
143 | ||
144 | static struct kmem_cache *ext4_es_cachep; | |
1dc0aa46 | 145 | static struct kmem_cache *ext4_pending_cachep; |
654598be | 146 | |
bdedbb7b ZL |
147 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes); |
148 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, | |
06b0c886 | 149 | ext4_lblk_t end); |
dd475925 | 150 | static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan); |
edaa53ca ZL |
151 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
152 | struct ext4_inode_info *locked_ei); | |
06b0c886 | 153 | |
654598be ZL |
154 | int __init ext4_init_es(void) |
155 | { | |
24630774 TT |
156 | ext4_es_cachep = kmem_cache_create("ext4_extent_status", |
157 | sizeof(struct extent_status), | |
158 | 0, (SLAB_RECLAIM_ACCOUNT), NULL); | |
654598be ZL |
159 | if (ext4_es_cachep == NULL) |
160 | return -ENOMEM; | |
161 | return 0; | |
162 | } | |
163 | ||
164 | void ext4_exit_es(void) | |
165 | { | |
21c580d8 | 166 | kmem_cache_destroy(ext4_es_cachep); |
654598be ZL |
167 | } |
168 | ||
169 | void ext4_es_init_tree(struct ext4_es_tree *tree) | |
170 | { | |
171 | tree->root = RB_ROOT; | |
172 | tree->cache_es = NULL; | |
173 | } | |
174 | ||
175 | #ifdef ES_DEBUG__ | |
176 | static void ext4_es_print_tree(struct inode *inode) | |
177 | { | |
178 | struct ext4_es_tree *tree; | |
179 | struct rb_node *node; | |
180 | ||
181 | printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino); | |
182 | tree = &EXT4_I(inode)->i_es_tree; | |
183 | node = rb_first(&tree->root); | |
184 | while (node) { | |
185 | struct extent_status *es; | |
186 | es = rb_entry(node, struct extent_status, rb_node); | |
ce140cdd | 187 | printk(KERN_DEBUG " [%u/%u) %llu %x", |
fdc0212e ZL |
188 | es->es_lblk, es->es_len, |
189 | ext4_es_pblock(es), ext4_es_status(es)); | |
654598be ZL |
190 | node = rb_next(node); |
191 | } | |
192 | printk(KERN_DEBUG "\n"); | |
193 | } | |
194 | #else | |
195 | #define ext4_es_print_tree(inode) | |
196 | #endif | |
197 | ||
06b0c886 | 198 | static inline ext4_lblk_t ext4_es_end(struct extent_status *es) |
654598be | 199 | { |
06b0c886 ZL |
200 | BUG_ON(es->es_lblk + es->es_len < es->es_lblk); |
201 | return es->es_lblk + es->es_len - 1; | |
654598be ZL |
202 | } |
203 | ||
204 | /* | |
205 | * search through the tree for an delayed extent with a given offset. If | |
206 | * it can't be found, try to find next extent. | |
207 | */ | |
208 | static struct extent_status *__es_tree_search(struct rb_root *root, | |
06b0c886 | 209 | ext4_lblk_t lblk) |
654598be ZL |
210 | { |
211 | struct rb_node *node = root->rb_node; | |
212 | struct extent_status *es = NULL; | |
213 | ||
214 | while (node) { | |
215 | es = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 | 216 | if (lblk < es->es_lblk) |
654598be | 217 | node = node->rb_left; |
06b0c886 | 218 | else if (lblk > ext4_es_end(es)) |
654598be ZL |
219 | node = node->rb_right; |
220 | else | |
221 | return es; | |
222 | } | |
223 | ||
06b0c886 | 224 | if (es && lblk < es->es_lblk) |
654598be ZL |
225 | return es; |
226 | ||
06b0c886 | 227 | if (es && lblk > ext4_es_end(es)) { |
654598be ZL |
228 | node = rb_next(&es->rb_node); |
229 | return node ? rb_entry(node, struct extent_status, rb_node) : | |
230 | NULL; | |
231 | } | |
232 | ||
233 | return NULL; | |
234 | } | |
235 | ||
236 | /* | |
ad431025 EW |
237 | * ext4_es_find_extent_range - find extent with specified status within block |
238 | * range or next extent following block range in | |
239 | * extents status tree | |
654598be | 240 | * |
ad431025 EW |
241 | * @inode - file containing the range |
242 | * @matching_fn - pointer to function that matches extents with desired status | |
243 | * @lblk - logical block defining start of range | |
244 | * @end - logical block defining end of range | |
245 | * @es - extent found, if any | |
246 | * | |
247 | * Find the first extent within the block range specified by @lblk and @end | |
248 | * in the extents status tree that satisfies @matching_fn. If a match | |
249 | * is found, it's returned in @es. If not, and a matching extent is found | |
250 | * beyond the block range, it's returned in @es. If no match is found, an | |
251 | * extent is returned in @es whose es_lblk, es_len, and es_pblk components | |
252 | * are 0. | |
654598be | 253 | */ |
ad431025 EW |
254 | static void __es_find_extent_range(struct inode *inode, |
255 | int (*matching_fn)(struct extent_status *es), | |
256 | ext4_lblk_t lblk, ext4_lblk_t end, | |
257 | struct extent_status *es) | |
654598be ZL |
258 | { |
259 | struct ext4_es_tree *tree = NULL; | |
260 | struct extent_status *es1 = NULL; | |
261 | struct rb_node *node; | |
654598be | 262 | |
ad431025 EW |
263 | WARN_ON(es == NULL); |
264 | WARN_ON(end < lblk); | |
992e9fdd | 265 | |
654598be ZL |
266 | tree = &EXT4_I(inode)->i_es_tree; |
267 | ||
ad431025 | 268 | /* see if the extent has been cached */ |
be401363 | 269 | es->es_lblk = es->es_len = es->es_pblk = 0; |
654598be ZL |
270 | if (tree->cache_es) { |
271 | es1 = tree->cache_es; | |
be401363 | 272 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
3be78c73 | 273 | es_debug("%u cached by [%u/%u) %llu %x\n", |
be401363 | 274 | lblk, es1->es_lblk, es1->es_len, |
fdc0212e | 275 | ext4_es_pblock(es1), ext4_es_status(es1)); |
654598be ZL |
276 | goto out; |
277 | } | |
278 | } | |
279 | ||
be401363 | 280 | es1 = __es_tree_search(&tree->root, lblk); |
654598be ZL |
281 | |
282 | out: | |
ad431025 | 283 | if (es1 && !matching_fn(es1)) { |
be401363 ZL |
284 | while ((node = rb_next(&es1->rb_node)) != NULL) { |
285 | es1 = rb_entry(node, struct extent_status, rb_node); | |
e30b5dca YZ |
286 | if (es1->es_lblk > end) { |
287 | es1 = NULL; | |
288 | break; | |
289 | } | |
ad431025 | 290 | if (matching_fn(es1)) |
be401363 ZL |
291 | break; |
292 | } | |
293 | } | |
294 | ||
ad431025 | 295 | if (es1 && matching_fn(es1)) { |
654598be | 296 | tree->cache_es = es1; |
06b0c886 ZL |
297 | es->es_lblk = es1->es_lblk; |
298 | es->es_len = es1->es_len; | |
fdc0212e | 299 | es->es_pblk = es1->es_pblk; |
654598be ZL |
300 | } |
301 | ||
ad431025 EW |
302 | } |
303 | ||
304 | /* | |
305 | * Locking for __es_find_extent_range() for external use | |
306 | */ | |
307 | void ext4_es_find_extent_range(struct inode *inode, | |
308 | int (*matching_fn)(struct extent_status *es), | |
309 | ext4_lblk_t lblk, ext4_lblk_t end, | |
310 | struct extent_status *es) | |
311 | { | |
312 | trace_ext4_es_find_extent_range_enter(inode, lblk); | |
313 | ||
314 | read_lock(&EXT4_I(inode)->i_es_lock); | |
315 | __es_find_extent_range(inode, matching_fn, lblk, end, es); | |
316 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
317 | ||
318 | trace_ext4_es_find_extent_range_exit(inode, es); | |
319 | } | |
320 | ||
321 | /* | |
322 | * __es_scan_range - search block range for block with specified status | |
323 | * in extents status tree | |
324 | * | |
325 | * @inode - file containing the range | |
326 | * @matching_fn - pointer to function that matches extents with desired status | |
327 | * @lblk - logical block defining start of range | |
328 | * @end - logical block defining end of range | |
329 | * | |
330 | * Returns true if at least one block in the specified block range satisfies | |
331 | * the criterion specified by @matching_fn, and false if not. If at least | |
332 | * one extent has the specified status, then there is at least one block | |
333 | * in the cluster with that status. Should only be called by code that has | |
334 | * taken i_es_lock. | |
335 | */ | |
336 | static bool __es_scan_range(struct inode *inode, | |
337 | int (*matching_fn)(struct extent_status *es), | |
338 | ext4_lblk_t start, ext4_lblk_t end) | |
339 | { | |
340 | struct extent_status es; | |
341 | ||
342 | __es_find_extent_range(inode, matching_fn, start, end, &es); | |
343 | if (es.es_len == 0) | |
344 | return false; /* no matching extent in the tree */ | |
345 | else if (es.es_lblk <= start && | |
346 | start < es.es_lblk + es.es_len) | |
347 | return true; | |
348 | else if (start <= es.es_lblk && es.es_lblk <= end) | |
349 | return true; | |
350 | else | |
351 | return false; | |
352 | } | |
353 | /* | |
354 | * Locking for __es_scan_range() for external use | |
355 | */ | |
356 | bool ext4_es_scan_range(struct inode *inode, | |
357 | int (*matching_fn)(struct extent_status *es), | |
358 | ext4_lblk_t lblk, ext4_lblk_t end) | |
359 | { | |
360 | bool ret; | |
361 | ||
362 | read_lock(&EXT4_I(inode)->i_es_lock); | |
363 | ret = __es_scan_range(inode, matching_fn, lblk, end); | |
364 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
365 | ||
366 | return ret; | |
367 | } | |
368 | ||
369 | /* | |
370 | * __es_scan_clu - search cluster for block with specified status in | |
371 | * extents status tree | |
372 | * | |
373 | * @inode - file containing the cluster | |
374 | * @matching_fn - pointer to function that matches extents with desired status | |
375 | * @lblk - logical block in cluster to be searched | |
376 | * | |
377 | * Returns true if at least one extent in the cluster containing @lblk | |
378 | * satisfies the criterion specified by @matching_fn, and false if not. If at | |
379 | * least one extent has the specified status, then there is at least one block | |
380 | * in the cluster with that status. Should only be called by code that has | |
381 | * taken i_es_lock. | |
382 | */ | |
383 | static bool __es_scan_clu(struct inode *inode, | |
384 | int (*matching_fn)(struct extent_status *es), | |
385 | ext4_lblk_t lblk) | |
386 | { | |
387 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
388 | ext4_lblk_t lblk_start, lblk_end; | |
389 | ||
390 | lblk_start = EXT4_LBLK_CMASK(sbi, lblk); | |
391 | lblk_end = lblk_start + sbi->s_cluster_ratio - 1; | |
392 | ||
393 | return __es_scan_range(inode, matching_fn, lblk_start, lblk_end); | |
394 | } | |
395 | ||
396 | /* | |
397 | * Locking for __es_scan_clu() for external use | |
398 | */ | |
399 | bool ext4_es_scan_clu(struct inode *inode, | |
400 | int (*matching_fn)(struct extent_status *es), | |
401 | ext4_lblk_t lblk) | |
402 | { | |
403 | bool ret; | |
404 | ||
405 | read_lock(&EXT4_I(inode)->i_es_lock); | |
406 | ret = __es_scan_clu(inode, matching_fn, lblk); | |
654598be | 407 | read_unlock(&EXT4_I(inode)->i_es_lock); |
992e9fdd | 408 | |
ad431025 | 409 | return ret; |
654598be ZL |
410 | } |
411 | ||
b0dea4c1 | 412 | static void ext4_es_list_add(struct inode *inode) |
edaa53ca ZL |
413 | { |
414 | struct ext4_inode_info *ei = EXT4_I(inode); | |
415 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
416 | ||
417 | if (!list_empty(&ei->i_es_list)) | |
418 | return; | |
419 | ||
420 | spin_lock(&sbi->s_es_lock); | |
421 | if (list_empty(&ei->i_es_list)) { | |
422 | list_add_tail(&ei->i_es_list, &sbi->s_es_list); | |
423 | sbi->s_es_nr_inode++; | |
424 | } | |
425 | spin_unlock(&sbi->s_es_lock); | |
426 | } | |
427 | ||
b0dea4c1 | 428 | static void ext4_es_list_del(struct inode *inode) |
edaa53ca ZL |
429 | { |
430 | struct ext4_inode_info *ei = EXT4_I(inode); | |
431 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
432 | ||
433 | spin_lock(&sbi->s_es_lock); | |
434 | if (!list_empty(&ei->i_es_list)) { | |
435 | list_del_init(&ei->i_es_list); | |
436 | sbi->s_es_nr_inode--; | |
437 | WARN_ON_ONCE(sbi->s_es_nr_inode < 0); | |
438 | } | |
439 | spin_unlock(&sbi->s_es_lock); | |
440 | } | |
441 | ||
654598be | 442 | static struct extent_status * |
bdedbb7b ZL |
443 | ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len, |
444 | ext4_fsblk_t pblk) | |
654598be ZL |
445 | { |
446 | struct extent_status *es; | |
447 | es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC); | |
448 | if (es == NULL) | |
449 | return NULL; | |
06b0c886 ZL |
450 | es->es_lblk = lblk; |
451 | es->es_len = len; | |
fdc0212e | 452 | es->es_pblk = pblk; |
74cd15cd ZL |
453 | |
454 | /* | |
455 | * We don't count delayed extent because we never try to reclaim them | |
456 | */ | |
24630774 | 457 | if (!ext4_es_is_delayed(es)) { |
b0dea4c1 JK |
458 | if (!EXT4_I(inode)->i_es_shk_nr++) |
459 | ext4_es_list_add(inode); | |
eb68d0e2 | 460 | percpu_counter_inc(&EXT4_SB(inode->i_sb)-> |
edaa53ca | 461 | s_es_stats.es_stats_shk_cnt); |
24630774 | 462 | } |
74cd15cd | 463 | |
eb68d0e2 ZL |
464 | EXT4_I(inode)->i_es_all_nr++; |
465 | percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt); | |
466 | ||
654598be ZL |
467 | return es; |
468 | } | |
469 | ||
bdedbb7b | 470 | static void ext4_es_free_extent(struct inode *inode, struct extent_status *es) |
654598be | 471 | { |
eb68d0e2 ZL |
472 | EXT4_I(inode)->i_es_all_nr--; |
473 | percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt); | |
474 | ||
edaa53ca | 475 | /* Decrease the shrink counter when this es is not delayed */ |
74cd15cd | 476 | if (!ext4_es_is_delayed(es)) { |
edaa53ca | 477 | BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0); |
b0dea4c1 JK |
478 | if (!--EXT4_I(inode)->i_es_shk_nr) |
479 | ext4_es_list_del(inode); | |
eb68d0e2 | 480 | percpu_counter_dec(&EXT4_SB(inode->i_sb)-> |
edaa53ca | 481 | s_es_stats.es_stats_shk_cnt); |
74cd15cd ZL |
482 | } |
483 | ||
654598be ZL |
484 | kmem_cache_free(ext4_es_cachep, es); |
485 | } | |
486 | ||
06b0c886 ZL |
487 | /* |
488 | * Check whether or not two extents can be merged | |
489 | * Condition: | |
490 | * - logical block number is contiguous | |
fdc0212e ZL |
491 | * - physical block number is contiguous |
492 | * - status is equal | |
06b0c886 ZL |
493 | */ |
494 | static int ext4_es_can_be_merged(struct extent_status *es1, | |
495 | struct extent_status *es2) | |
496 | { | |
2be12de9 | 497 | if (ext4_es_type(es1) != ext4_es_type(es2)) |
06b0c886 ZL |
498 | return 0; |
499 | ||
0baaea64 LC |
500 | if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) { |
501 | pr_warn("ES assertion failed when merging extents. " | |
502 | "The sum of lengths of es1 (%d) and es2 (%d) " | |
503 | "is bigger than allowed file size (%d)\n", | |
504 | es1->es_len, es2->es_len, EXT_MAX_BLOCKS); | |
505 | WARN_ON(1); | |
fdc0212e | 506 | return 0; |
0baaea64 | 507 | } |
fdc0212e | 508 | |
bd384364 | 509 | if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk) |
fdc0212e ZL |
510 | return 0; |
511 | ||
bd384364 ZL |
512 | if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) && |
513 | (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2))) | |
514 | return 1; | |
515 | ||
516 | if (ext4_es_is_hole(es1)) | |
517 | return 1; | |
518 | ||
519 | /* we need to check delayed extent is without unwritten status */ | |
520 | if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1)) | |
521 | return 1; | |
522 | ||
523 | return 0; | |
06b0c886 ZL |
524 | } |
525 | ||
654598be | 526 | static struct extent_status * |
bdedbb7b | 527 | ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es) |
654598be | 528 | { |
bdedbb7b | 529 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
530 | struct extent_status *es1; |
531 | struct rb_node *node; | |
532 | ||
533 | node = rb_prev(&es->rb_node); | |
534 | if (!node) | |
535 | return es; | |
536 | ||
537 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
538 | if (ext4_es_can_be_merged(es1, es)) { |
539 | es1->es_len += es->es_len; | |
2be12de9 JK |
540 | if (ext4_es_is_referenced(es)) |
541 | ext4_es_set_referenced(es1); | |
654598be | 542 | rb_erase(&es->rb_node, &tree->root); |
bdedbb7b | 543 | ext4_es_free_extent(inode, es); |
654598be ZL |
544 | es = es1; |
545 | } | |
546 | ||
547 | return es; | |
548 | } | |
549 | ||
550 | static struct extent_status * | |
bdedbb7b | 551 | ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es) |
654598be | 552 | { |
bdedbb7b | 553 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
554 | struct extent_status *es1; |
555 | struct rb_node *node; | |
556 | ||
557 | node = rb_next(&es->rb_node); | |
558 | if (!node) | |
559 | return es; | |
560 | ||
561 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
562 | if (ext4_es_can_be_merged(es, es1)) { |
563 | es->es_len += es1->es_len; | |
2be12de9 JK |
564 | if (ext4_es_is_referenced(es1)) |
565 | ext4_es_set_referenced(es); | |
654598be | 566 | rb_erase(node, &tree->root); |
bdedbb7b | 567 | ext4_es_free_extent(inode, es1); |
654598be ZL |
568 | } |
569 | ||
570 | return es; | |
571 | } | |
572 | ||
921f266b | 573 | #ifdef ES_AGGRESSIVE_TEST |
d7b2a00c ZL |
574 | #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */ |
575 | ||
921f266b DM |
576 | static void ext4_es_insert_extent_ext_check(struct inode *inode, |
577 | struct extent_status *es) | |
578 | { | |
579 | struct ext4_ext_path *path = NULL; | |
580 | struct ext4_extent *ex; | |
581 | ext4_lblk_t ee_block; | |
582 | ext4_fsblk_t ee_start; | |
583 | unsigned short ee_len; | |
584 | int depth, ee_status, es_status; | |
585 | ||
ed8a1a76 | 586 | path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE); |
921f266b DM |
587 | if (IS_ERR(path)) |
588 | return; | |
589 | ||
590 | depth = ext_depth(inode); | |
591 | ex = path[depth].p_ext; | |
592 | ||
593 | if (ex) { | |
594 | ||
595 | ee_block = le32_to_cpu(ex->ee_block); | |
596 | ee_start = ext4_ext_pblock(ex); | |
597 | ee_len = ext4_ext_get_actual_len(ex); | |
598 | ||
556615dc | 599 | ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0; |
921f266b DM |
600 | es_status = ext4_es_is_unwritten(es) ? 1 : 0; |
601 | ||
602 | /* | |
603 | * Make sure ex and es are not overlap when we try to insert | |
604 | * a delayed/hole extent. | |
605 | */ | |
606 | if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) { | |
607 | if (in_range(es->es_lblk, ee_block, ee_len)) { | |
bdafe42a | 608 | pr_warn("ES insert assertion failed for " |
921f266b DM |
609 | "inode: %lu we can find an extent " |
610 | "at block [%d/%d/%llu/%c], but we " | |
ce140cdd EW |
611 | "want to add a delayed/hole extent " |
612 | "[%d/%d/%llu/%x]\n", | |
921f266b DM |
613 | inode->i_ino, ee_block, ee_len, |
614 | ee_start, ee_status ? 'u' : 'w', | |
615 | es->es_lblk, es->es_len, | |
616 | ext4_es_pblock(es), ext4_es_status(es)); | |
617 | } | |
618 | goto out; | |
619 | } | |
620 | ||
621 | /* | |
622 | * We don't check ee_block == es->es_lblk, etc. because es | |
623 | * might be a part of whole extent, vice versa. | |
624 | */ | |
625 | if (es->es_lblk < ee_block || | |
626 | ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) { | |
bdafe42a | 627 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
628 | "ex_status [%d/%d/%llu/%c] != " |
629 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, | |
630 | ee_block, ee_len, ee_start, | |
631 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, | |
632 | ext4_es_pblock(es), es_status ? 'u' : 'w'); | |
633 | goto out; | |
634 | } | |
635 | ||
636 | if (ee_status ^ es_status) { | |
bdafe42a | 637 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
638 | "ex_status [%d/%d/%llu/%c] != " |
639 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, | |
640 | ee_block, ee_len, ee_start, | |
641 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, | |
642 | ext4_es_pblock(es), es_status ? 'u' : 'w'); | |
643 | } | |
644 | } else { | |
645 | /* | |
646 | * We can't find an extent on disk. So we need to make sure | |
647 | * that we don't want to add an written/unwritten extent. | |
648 | */ | |
649 | if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) { | |
bdafe42a | 650 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b | 651 | "can't find an extent at block %d but we want " |
ce140cdd EW |
652 | "to add a written/unwritten extent " |
653 | "[%d/%d/%llu/%x]\n", inode->i_ino, | |
921f266b DM |
654 | es->es_lblk, es->es_lblk, es->es_len, |
655 | ext4_es_pblock(es), ext4_es_status(es)); | |
656 | } | |
657 | } | |
658 | out: | |
b7ea89ad TT |
659 | ext4_ext_drop_refs(path); |
660 | kfree(path); | |
921f266b DM |
661 | } |
662 | ||
663 | static void ext4_es_insert_extent_ind_check(struct inode *inode, | |
664 | struct extent_status *es) | |
665 | { | |
666 | struct ext4_map_blocks map; | |
667 | int retval; | |
668 | ||
669 | /* | |
670 | * Here we call ext4_ind_map_blocks to lookup a block mapping because | |
671 | * 'Indirect' structure is defined in indirect.c. So we couldn't | |
672 | * access direct/indirect tree from outside. It is too dirty to define | |
673 | * this function in indirect.c file. | |
674 | */ | |
675 | ||
676 | map.m_lblk = es->es_lblk; | |
677 | map.m_len = es->es_len; | |
678 | ||
679 | retval = ext4_ind_map_blocks(NULL, inode, &map, 0); | |
680 | if (retval > 0) { | |
681 | if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) { | |
682 | /* | |
683 | * We want to add a delayed/hole extent but this | |
684 | * block has been allocated. | |
685 | */ | |
bdafe42a | 686 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b | 687 | "We can find blocks but we want to add a " |
ce140cdd | 688 | "delayed/hole extent [%d/%d/%llu/%x]\n", |
921f266b DM |
689 | inode->i_ino, es->es_lblk, es->es_len, |
690 | ext4_es_pblock(es), ext4_es_status(es)); | |
691 | return; | |
692 | } else if (ext4_es_is_written(es)) { | |
693 | if (retval != es->es_len) { | |
bdafe42a | 694 | pr_warn("ES insert assertion failed for " |
921f266b DM |
695 | "inode: %lu retval %d != es_len %d\n", |
696 | inode->i_ino, retval, es->es_len); | |
697 | return; | |
698 | } | |
699 | if (map.m_pblk != ext4_es_pblock(es)) { | |
bdafe42a | 700 | pr_warn("ES insert assertion failed for " |
921f266b DM |
701 | "inode: %lu m_pblk %llu != " |
702 | "es_pblk %llu\n", | |
703 | inode->i_ino, map.m_pblk, | |
704 | ext4_es_pblock(es)); | |
705 | return; | |
706 | } | |
707 | } else { | |
708 | /* | |
709 | * We don't need to check unwritten extent because | |
710 | * indirect-based file doesn't have it. | |
711 | */ | |
712 | BUG_ON(1); | |
713 | } | |
714 | } else if (retval == 0) { | |
715 | if (ext4_es_is_written(es)) { | |
bdafe42a | 716 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b | 717 | "We can't find the block but we want to add " |
ce140cdd | 718 | "a written extent [%d/%d/%llu/%x]\n", |
921f266b DM |
719 | inode->i_ino, es->es_lblk, es->es_len, |
720 | ext4_es_pblock(es), ext4_es_status(es)); | |
721 | return; | |
722 | } | |
723 | } | |
724 | } | |
725 | ||
726 | static inline void ext4_es_insert_extent_check(struct inode *inode, | |
727 | struct extent_status *es) | |
728 | { | |
729 | /* | |
730 | * We don't need to worry about the race condition because | |
731 | * caller takes i_data_sem locking. | |
732 | */ | |
733 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); | |
734 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) | |
735 | ext4_es_insert_extent_ext_check(inode, es); | |
736 | else | |
737 | ext4_es_insert_extent_ind_check(inode, es); | |
738 | } | |
739 | #else | |
740 | static inline void ext4_es_insert_extent_check(struct inode *inode, | |
741 | struct extent_status *es) | |
742 | { | |
743 | } | |
744 | #endif | |
745 | ||
bdedbb7b | 746 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes) |
654598be | 747 | { |
bdedbb7b | 748 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
749 | struct rb_node **p = &tree->root.rb_node; |
750 | struct rb_node *parent = NULL; | |
751 | struct extent_status *es; | |
654598be ZL |
752 | |
753 | while (*p) { | |
754 | parent = *p; | |
755 | es = rb_entry(parent, struct extent_status, rb_node); | |
756 | ||
06b0c886 ZL |
757 | if (newes->es_lblk < es->es_lblk) { |
758 | if (ext4_es_can_be_merged(newes, es)) { | |
759 | /* | |
760 | * Here we can modify es_lblk directly | |
761 | * because it isn't overlapped. | |
762 | */ | |
763 | es->es_lblk = newes->es_lblk; | |
764 | es->es_len += newes->es_len; | |
fdc0212e ZL |
765 | if (ext4_es_is_written(es) || |
766 | ext4_es_is_unwritten(es)) | |
767 | ext4_es_store_pblock(es, | |
768 | newes->es_pblk); | |
bdedbb7b | 769 | es = ext4_es_try_to_merge_left(inode, es); |
654598be ZL |
770 | goto out; |
771 | } | |
772 | p = &(*p)->rb_left; | |
06b0c886 ZL |
773 | } else if (newes->es_lblk > ext4_es_end(es)) { |
774 | if (ext4_es_can_be_merged(es, newes)) { | |
775 | es->es_len += newes->es_len; | |
bdedbb7b | 776 | es = ext4_es_try_to_merge_right(inode, es); |
654598be ZL |
777 | goto out; |
778 | } | |
779 | p = &(*p)->rb_right; | |
780 | } else { | |
06b0c886 ZL |
781 | BUG_ON(1); |
782 | return -EINVAL; | |
654598be ZL |
783 | } |
784 | } | |
785 | ||
bdedbb7b | 786 | es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len, |
fdc0212e | 787 | newes->es_pblk); |
654598be ZL |
788 | if (!es) |
789 | return -ENOMEM; | |
790 | rb_link_node(&es->rb_node, parent, p); | |
791 | rb_insert_color(&es->rb_node, &tree->root); | |
792 | ||
793 | out: | |
794 | tree->cache_es = es; | |
795 | return 0; | |
796 | } | |
797 | ||
798 | /* | |
bdafe42a TT |
799 | * ext4_es_insert_extent() adds information to an inode's extent |
800 | * status tree. | |
654598be ZL |
801 | * |
802 | * Return 0 on success, error code on failure. | |
803 | */ | |
06b0c886 | 804 | int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk, |
fdc0212e | 805 | ext4_lblk_t len, ext4_fsblk_t pblk, |
3be78c73 | 806 | unsigned int status) |
654598be | 807 | { |
06b0c886 ZL |
808 | struct extent_status newes; |
809 | ext4_lblk_t end = lblk + len - 1; | |
654598be ZL |
810 | int err = 0; |
811 | ||
3be78c73 | 812 | es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n", |
fdc0212e | 813 | lblk, len, pblk, status, inode->i_ino); |
06b0c886 | 814 | |
d4381472 EG |
815 | if (!len) |
816 | return 0; | |
817 | ||
06b0c886 ZL |
818 | BUG_ON(end < lblk); |
819 | ||
d2dc317d LC |
820 | if ((status & EXTENT_STATUS_DELAYED) && |
821 | (status & EXTENT_STATUS_WRITTEN)) { | |
822 | ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as " | |
823 | " delayed and written which can potentially " | |
8d2ae1cb | 824 | " cause data loss.", lblk, len); |
d2dc317d LC |
825 | WARN_ON(1); |
826 | } | |
827 | ||
06b0c886 ZL |
828 | newes.es_lblk = lblk; |
829 | newes.es_len = len; | |
9a6633b1 | 830 | ext4_es_store_pblock_status(&newes, pblk, status); |
fdc0212e | 831 | trace_ext4_es_insert_extent(inode, &newes); |
654598be | 832 | |
921f266b DM |
833 | ext4_es_insert_extent_check(inode, &newes); |
834 | ||
654598be | 835 | write_lock(&EXT4_I(inode)->i_es_lock); |
bdedbb7b | 836 | err = __es_remove_extent(inode, lblk, end); |
06b0c886 ZL |
837 | if (err != 0) |
838 | goto error; | |
e15f742c | 839 | retry: |
bdedbb7b | 840 | err = __es_insert_extent(inode, &newes); |
edaa53ca | 841 | if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb), |
dd475925 | 842 | 128, EXT4_I(inode))) |
e15f742c TT |
843 | goto retry; |
844 | if (err == -ENOMEM && !ext4_es_is_delayed(&newes)) | |
845 | err = 0; | |
06b0c886 ZL |
846 | |
847 | error: | |
654598be ZL |
848 | write_unlock(&EXT4_I(inode)->i_es_lock); |
849 | ||
850 | ext4_es_print_tree(inode); | |
851 | ||
852 | return err; | |
853 | } | |
854 | ||
107a7bd3 TT |
855 | /* |
856 | * ext4_es_cache_extent() inserts information into the extent status | |
857 | * tree if and only if there isn't information about the range in | |
858 | * question already. | |
859 | */ | |
860 | void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk, | |
861 | ext4_lblk_t len, ext4_fsblk_t pblk, | |
862 | unsigned int status) | |
863 | { | |
864 | struct extent_status *es; | |
865 | struct extent_status newes; | |
866 | ext4_lblk_t end = lblk + len - 1; | |
867 | ||
868 | newes.es_lblk = lblk; | |
869 | newes.es_len = len; | |
9a6633b1 | 870 | ext4_es_store_pblock_status(&newes, pblk, status); |
107a7bd3 TT |
871 | trace_ext4_es_cache_extent(inode, &newes); |
872 | ||
873 | if (!len) | |
874 | return; | |
875 | ||
876 | BUG_ON(end < lblk); | |
877 | ||
878 | write_lock(&EXT4_I(inode)->i_es_lock); | |
879 | ||
880 | es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk); | |
7869a4a6 TT |
881 | if (!es || es->es_lblk > end) |
882 | __es_insert_extent(inode, &newes); | |
107a7bd3 TT |
883 | write_unlock(&EXT4_I(inode)->i_es_lock); |
884 | } | |
885 | ||
d100eef2 ZL |
886 | /* |
887 | * ext4_es_lookup_extent() looks up an extent in extent status tree. | |
888 | * | |
889 | * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks. | |
890 | * | |
891 | * Return: 1 on found, 0 on not | |
892 | */ | |
893 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, | |
894 | struct extent_status *es) | |
895 | { | |
896 | struct ext4_es_tree *tree; | |
eb68d0e2 | 897 | struct ext4_es_stats *stats; |
d100eef2 ZL |
898 | struct extent_status *es1 = NULL; |
899 | struct rb_node *node; | |
900 | int found = 0; | |
901 | ||
902 | trace_ext4_es_lookup_extent_enter(inode, lblk); | |
903 | es_debug("lookup extent in block %u\n", lblk); | |
904 | ||
905 | tree = &EXT4_I(inode)->i_es_tree; | |
906 | read_lock(&EXT4_I(inode)->i_es_lock); | |
907 | ||
908 | /* find extent in cache firstly */ | |
909 | es->es_lblk = es->es_len = es->es_pblk = 0; | |
910 | if (tree->cache_es) { | |
911 | es1 = tree->cache_es; | |
912 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { | |
913 | es_debug("%u cached by [%u/%u)\n", | |
914 | lblk, es1->es_lblk, es1->es_len); | |
915 | found = 1; | |
916 | goto out; | |
917 | } | |
918 | } | |
919 | ||
920 | node = tree->root.rb_node; | |
921 | while (node) { | |
922 | es1 = rb_entry(node, struct extent_status, rb_node); | |
923 | if (lblk < es1->es_lblk) | |
924 | node = node->rb_left; | |
925 | else if (lblk > ext4_es_end(es1)) | |
926 | node = node->rb_right; | |
927 | else { | |
928 | found = 1; | |
929 | break; | |
930 | } | |
931 | } | |
932 | ||
933 | out: | |
eb68d0e2 | 934 | stats = &EXT4_SB(inode->i_sb)->s_es_stats; |
d100eef2 ZL |
935 | if (found) { |
936 | BUG_ON(!es1); | |
937 | es->es_lblk = es1->es_lblk; | |
938 | es->es_len = es1->es_len; | |
939 | es->es_pblk = es1->es_pblk; | |
87d8a74b JK |
940 | if (!ext4_es_is_referenced(es1)) |
941 | ext4_es_set_referenced(es1); | |
eb68d0e2 ZL |
942 | stats->es_stats_cache_hits++; |
943 | } else { | |
944 | stats->es_stats_cache_misses++; | |
d100eef2 ZL |
945 | } |
946 | ||
947 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
948 | ||
949 | trace_ext4_es_lookup_extent_exit(inode, es, found); | |
950 | return found; | |
951 | } | |
952 | ||
bdedbb7b ZL |
953 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
954 | ext4_lblk_t end) | |
654598be | 955 | { |
bdedbb7b | 956 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be | 957 | struct rb_node *node; |
654598be ZL |
958 | struct extent_status *es; |
959 | struct extent_status orig_es; | |
06b0c886 | 960 | ext4_lblk_t len1, len2; |
fdc0212e | 961 | ext4_fsblk_t block; |
e15f742c | 962 | int err; |
654598be | 963 | |
e15f742c TT |
964 | retry: |
965 | err = 0; | |
06b0c886 | 966 | es = __es_tree_search(&tree->root, lblk); |
654598be ZL |
967 | if (!es) |
968 | goto out; | |
06b0c886 | 969 | if (es->es_lblk > end) |
654598be ZL |
970 | goto out; |
971 | ||
972 | /* Simply invalidate cache_es. */ | |
973 | tree->cache_es = NULL; | |
974 | ||
06b0c886 ZL |
975 | orig_es.es_lblk = es->es_lblk; |
976 | orig_es.es_len = es->es_len; | |
fdc0212e ZL |
977 | orig_es.es_pblk = es->es_pblk; |
978 | ||
06b0c886 ZL |
979 | len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0; |
980 | len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0; | |
654598be | 981 | if (len1 > 0) |
06b0c886 | 982 | es->es_len = len1; |
654598be ZL |
983 | if (len2 > 0) { |
984 | if (len1 > 0) { | |
06b0c886 ZL |
985 | struct extent_status newes; |
986 | ||
987 | newes.es_lblk = end + 1; | |
988 | newes.es_len = len2; | |
666525df | 989 | block = 0x7FDEADBEEFULL; |
fdc0212e | 990 | if (ext4_es_is_written(&orig_es) || |
9a6633b1 | 991 | ext4_es_is_unwritten(&orig_es)) |
fdc0212e ZL |
992 | block = ext4_es_pblock(&orig_es) + |
993 | orig_es.es_len - len2; | |
9a6633b1 TT |
994 | ext4_es_store_pblock_status(&newes, block, |
995 | ext4_es_status(&orig_es)); | |
bdedbb7b | 996 | err = __es_insert_extent(inode, &newes); |
654598be | 997 | if (err) { |
06b0c886 ZL |
998 | es->es_lblk = orig_es.es_lblk; |
999 | es->es_len = orig_es.es_len; | |
e15f742c | 1000 | if ((err == -ENOMEM) && |
edaa53ca | 1001 | __es_shrink(EXT4_SB(inode->i_sb), |
dd475925 | 1002 | 128, EXT4_I(inode))) |
e15f742c | 1003 | goto retry; |
654598be ZL |
1004 | goto out; |
1005 | } | |
1006 | } else { | |
06b0c886 ZL |
1007 | es->es_lblk = end + 1; |
1008 | es->es_len = len2; | |
fdc0212e ZL |
1009 | if (ext4_es_is_written(es) || |
1010 | ext4_es_is_unwritten(es)) { | |
1011 | block = orig_es.es_pblk + orig_es.es_len - len2; | |
1012 | ext4_es_store_pblock(es, block); | |
1013 | } | |
654598be ZL |
1014 | } |
1015 | goto out; | |
1016 | } | |
1017 | ||
1018 | if (len1 > 0) { | |
1019 | node = rb_next(&es->rb_node); | |
1020 | if (node) | |
1021 | es = rb_entry(node, struct extent_status, rb_node); | |
1022 | else | |
1023 | es = NULL; | |
1024 | } | |
1025 | ||
06b0c886 | 1026 | while (es && ext4_es_end(es) <= end) { |
654598be ZL |
1027 | node = rb_next(&es->rb_node); |
1028 | rb_erase(&es->rb_node, &tree->root); | |
bdedbb7b | 1029 | ext4_es_free_extent(inode, es); |
654598be ZL |
1030 | if (!node) { |
1031 | es = NULL; | |
1032 | break; | |
1033 | } | |
1034 | es = rb_entry(node, struct extent_status, rb_node); | |
1035 | } | |
1036 | ||
06b0c886 | 1037 | if (es && es->es_lblk < end + 1) { |
fdc0212e ZL |
1038 | ext4_lblk_t orig_len = es->es_len; |
1039 | ||
06b0c886 ZL |
1040 | len1 = ext4_es_end(es) - end; |
1041 | es->es_lblk = end + 1; | |
1042 | es->es_len = len1; | |
fdc0212e ZL |
1043 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
1044 | block = es->es_pblk + orig_len - len1; | |
1045 | ext4_es_store_pblock(es, block); | |
1046 | } | |
654598be ZL |
1047 | } |
1048 | ||
1049 | out: | |
06b0c886 ZL |
1050 | return err; |
1051 | } | |
1052 | ||
1053 | /* | |
1054 | * ext4_es_remove_extent() removes a space from a extent status tree. | |
1055 | * | |
1056 | * Return 0 on success, error code on failure. | |
1057 | */ | |
1058 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, | |
1059 | ext4_lblk_t len) | |
1060 | { | |
06b0c886 ZL |
1061 | ext4_lblk_t end; |
1062 | int err = 0; | |
1063 | ||
1064 | trace_ext4_es_remove_extent(inode, lblk, len); | |
1065 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", | |
1066 | lblk, len, inode->i_ino); | |
1067 | ||
d4381472 EG |
1068 | if (!len) |
1069 | return err; | |
1070 | ||
06b0c886 ZL |
1071 | end = lblk + len - 1; |
1072 | BUG_ON(end < lblk); | |
1073 | ||
edaa53ca ZL |
1074 | /* |
1075 | * ext4_clear_inode() depends on us taking i_es_lock unconditionally | |
1076 | * so that we are sure __es_shrink() is done with the inode before it | |
1077 | * is reclaimed. | |
1078 | */ | |
06b0c886 | 1079 | write_lock(&EXT4_I(inode)->i_es_lock); |
bdedbb7b | 1080 | err = __es_remove_extent(inode, lblk, end); |
654598be ZL |
1081 | write_unlock(&EXT4_I(inode)->i_es_lock); |
1082 | ext4_es_print_tree(inode); | |
1083 | return err; | |
1084 | } | |
74cd15cd | 1085 | |
edaa53ca ZL |
1086 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
1087 | struct ext4_inode_info *locked_ei) | |
74cd15cd | 1088 | { |
74cd15cd | 1089 | struct ext4_inode_info *ei; |
eb68d0e2 | 1090 | struct ext4_es_stats *es_stats; |
eb68d0e2 ZL |
1091 | ktime_t start_time; |
1092 | u64 scan_time; | |
edaa53ca | 1093 | int nr_to_walk; |
1ab6c499 | 1094 | int nr_shrunk = 0; |
edaa53ca | 1095 | int retried = 0, nr_skipped = 0; |
74cd15cd | 1096 | |
eb68d0e2 ZL |
1097 | es_stats = &sbi->s_es_stats; |
1098 | start_time = ktime_get(); | |
d3922a77 | 1099 | |
7869a4a6 | 1100 | retry: |
edaa53ca ZL |
1101 | spin_lock(&sbi->s_es_lock); |
1102 | nr_to_walk = sbi->s_es_nr_inode; | |
1103 | while (nr_to_walk-- > 0) { | |
edaa53ca ZL |
1104 | if (list_empty(&sbi->s_es_list)) { |
1105 | spin_unlock(&sbi->s_es_lock); | |
1106 | goto out; | |
1107 | } | |
1108 | ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info, | |
1109 | i_es_list); | |
1110 | /* Move the inode to the tail */ | |
dd475925 | 1111 | list_move_tail(&ei->i_es_list, &sbi->s_es_list); |
74cd15cd | 1112 | |
7869a4a6 | 1113 | /* |
edaa53ca ZL |
1114 | * Normally we try hard to avoid shrinking precached inodes, |
1115 | * but we will as a last resort. | |
7869a4a6 | 1116 | */ |
edaa53ca ZL |
1117 | if (!retried && ext4_test_inode_state(&ei->vfs_inode, |
1118 | EXT4_STATE_EXT_PRECACHED)) { | |
7869a4a6 | 1119 | nr_skipped++; |
74cd15cd ZL |
1120 | continue; |
1121 | } | |
d3922a77 | 1122 | |
edaa53ca ZL |
1123 | if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) { |
1124 | nr_skipped++; | |
d3922a77 | 1125 | continue; |
edaa53ca ZL |
1126 | } |
1127 | /* | |
1128 | * Now we hold i_es_lock which protects us from inode reclaim | |
1129 | * freeing inode under us | |
1130 | */ | |
1131 | spin_unlock(&sbi->s_es_lock); | |
74cd15cd | 1132 | |
dd475925 | 1133 | nr_shrunk += es_reclaim_extents(ei, &nr_to_scan); |
74cd15cd ZL |
1134 | write_unlock(&ei->i_es_lock); |
1135 | ||
dd475925 | 1136 | if (nr_to_scan <= 0) |
edaa53ca ZL |
1137 | goto out; |
1138 | spin_lock(&sbi->s_es_lock); | |
74cd15cd | 1139 | } |
edaa53ca | 1140 | spin_unlock(&sbi->s_es_lock); |
7869a4a6 TT |
1141 | |
1142 | /* | |
1143 | * If we skipped any inodes, and we weren't able to make any | |
edaa53ca | 1144 | * forward progress, try again to scan precached inodes. |
7869a4a6 TT |
1145 | */ |
1146 | if ((nr_shrunk == 0) && nr_skipped && !retried) { | |
1147 | retried++; | |
7869a4a6 TT |
1148 | goto retry; |
1149 | } | |
1150 | ||
e15f742c | 1151 | if (locked_ei && nr_shrunk == 0) |
dd475925 | 1152 | nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan); |
e15f742c | 1153 | |
edaa53ca | 1154 | out: |
eb68d0e2 ZL |
1155 | scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time)); |
1156 | if (likely(es_stats->es_stats_scan_time)) | |
1157 | es_stats->es_stats_scan_time = (scan_time + | |
1158 | es_stats->es_stats_scan_time*3) / 4; | |
1159 | else | |
1160 | es_stats->es_stats_scan_time = scan_time; | |
1161 | if (scan_time > es_stats->es_stats_max_scan_time) | |
1162 | es_stats->es_stats_max_scan_time = scan_time; | |
1163 | if (likely(es_stats->es_stats_shrunk)) | |
1164 | es_stats->es_stats_shrunk = (nr_shrunk + | |
1165 | es_stats->es_stats_shrunk*3) / 4; | |
1166 | else | |
1167 | es_stats->es_stats_shrunk = nr_shrunk; | |
1168 | ||
edaa53ca | 1169 | trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time, |
eb68d0e2 | 1170 | nr_skipped, retried); |
e15f742c TT |
1171 | return nr_shrunk; |
1172 | } | |
1173 | ||
1ab6c499 DC |
1174 | static unsigned long ext4_es_count(struct shrinker *shrink, |
1175 | struct shrink_control *sc) | |
1176 | { | |
1177 | unsigned long nr; | |
1178 | struct ext4_sb_info *sbi; | |
1179 | ||
1180 | sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker); | |
edaa53ca | 1181 | nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt); |
e963bb1d | 1182 | trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr); |
1ab6c499 DC |
1183 | return nr; |
1184 | } | |
1185 | ||
1186 | static unsigned long ext4_es_scan(struct shrinker *shrink, | |
1187 | struct shrink_control *sc) | |
e15f742c TT |
1188 | { |
1189 | struct ext4_sb_info *sbi = container_of(shrink, | |
1190 | struct ext4_sb_info, s_es_shrinker); | |
1191 | int nr_to_scan = sc->nr_to_scan; | |
1192 | int ret, nr_shrunk; | |
1193 | ||
edaa53ca | 1194 | ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt); |
e963bb1d | 1195 | trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret); |
e15f742c TT |
1196 | |
1197 | if (!nr_to_scan) | |
1198 | return ret; | |
1199 | ||
edaa53ca | 1200 | nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL); |
e15f742c | 1201 | |
e963bb1d | 1202 | trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret); |
1ab6c499 | 1203 | return nr_shrunk; |
74cd15cd ZL |
1204 | } |
1205 | ||
ebd173be | 1206 | int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v) |
74cd15cd | 1207 | { |
ebd173be | 1208 | struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private); |
eb68d0e2 ZL |
1209 | struct ext4_es_stats *es_stats = &sbi->s_es_stats; |
1210 | struct ext4_inode_info *ei, *max = NULL; | |
1211 | unsigned int inode_cnt = 0; | |
1212 | ||
1213 | if (v != SEQ_START_TOKEN) | |
1214 | return 0; | |
1215 | ||
1216 | /* here we just find an inode that has the max nr. of objects */ | |
edaa53ca ZL |
1217 | spin_lock(&sbi->s_es_lock); |
1218 | list_for_each_entry(ei, &sbi->s_es_list, i_es_list) { | |
eb68d0e2 ZL |
1219 | inode_cnt++; |
1220 | if (max && max->i_es_all_nr < ei->i_es_all_nr) | |
1221 | max = ei; | |
1222 | else if (!max) | |
1223 | max = ei; | |
1224 | } | |
edaa53ca | 1225 | spin_unlock(&sbi->s_es_lock); |
eb68d0e2 ZL |
1226 | |
1227 | seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n", | |
1228 | percpu_counter_sum_positive(&es_stats->es_stats_all_cnt), | |
edaa53ca | 1229 | percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt)); |
eb68d0e2 ZL |
1230 | seq_printf(seq, " %lu/%lu cache hits/misses\n", |
1231 | es_stats->es_stats_cache_hits, | |
1232 | es_stats->es_stats_cache_misses); | |
eb68d0e2 | 1233 | if (inode_cnt) |
edaa53ca | 1234 | seq_printf(seq, " %d inodes on list\n", inode_cnt); |
eb68d0e2 ZL |
1235 | |
1236 | seq_printf(seq, "average:\n %llu us scan time\n", | |
1237 | div_u64(es_stats->es_stats_scan_time, 1000)); | |
1238 | seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk); | |
1239 | if (inode_cnt) | |
1240 | seq_printf(seq, | |
1241 | "maximum:\n %lu inode (%u objects, %u reclaimable)\n" | |
1242 | " %llu us max scan time\n", | |
edaa53ca | 1243 | max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr, |
eb68d0e2 ZL |
1244 | div_u64(es_stats->es_stats_max_scan_time, 1000)); |
1245 | ||
1246 | return 0; | |
1247 | } | |
1248 | ||
eb68d0e2 | 1249 | int ext4_es_register_shrinker(struct ext4_sb_info *sbi) |
74cd15cd | 1250 | { |
eb68d0e2 ZL |
1251 | int err; |
1252 | ||
624d0f1d JK |
1253 | /* Make sure we have enough bits for physical block number */ |
1254 | BUILD_BUG_ON(ES_SHIFT < 48); | |
edaa53ca ZL |
1255 | INIT_LIST_HEAD(&sbi->s_es_list); |
1256 | sbi->s_es_nr_inode = 0; | |
1257 | spin_lock_init(&sbi->s_es_lock); | |
eb68d0e2 ZL |
1258 | sbi->s_es_stats.es_stats_shrunk = 0; |
1259 | sbi->s_es_stats.es_stats_cache_hits = 0; | |
1260 | sbi->s_es_stats.es_stats_cache_misses = 0; | |
1261 | sbi->s_es_stats.es_stats_scan_time = 0; | |
1262 | sbi->s_es_stats.es_stats_max_scan_time = 0; | |
c2661b80 | 1263 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL); |
eb68d0e2 ZL |
1264 | if (err) |
1265 | return err; | |
edaa53ca | 1266 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL); |
eb68d0e2 ZL |
1267 | if (err) |
1268 | goto err1; | |
1269 | ||
1ab6c499 DC |
1270 | sbi->s_es_shrinker.scan_objects = ext4_es_scan; |
1271 | sbi->s_es_shrinker.count_objects = ext4_es_count; | |
74cd15cd | 1272 | sbi->s_es_shrinker.seeks = DEFAULT_SEEKS; |
eb68d0e2 ZL |
1273 | err = register_shrinker(&sbi->s_es_shrinker); |
1274 | if (err) | |
1275 | goto err2; | |
1276 | ||
eb68d0e2 ZL |
1277 | return 0; |
1278 | ||
1279 | err2: | |
edaa53ca | 1280 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
eb68d0e2 ZL |
1281 | err1: |
1282 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); | |
1283 | return err; | |
74cd15cd ZL |
1284 | } |
1285 | ||
d3922a77 | 1286 | void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi) |
74cd15cd | 1287 | { |
eb68d0e2 | 1288 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); |
edaa53ca | 1289 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
d3922a77 | 1290 | unregister_shrinker(&sbi->s_es_shrinker); |
74cd15cd ZL |
1291 | } |
1292 | ||
dd475925 JK |
1293 | /* |
1294 | * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at | |
1295 | * most *nr_to_scan extents, update *nr_to_scan accordingly. | |
1296 | * | |
1297 | * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan. | |
1298 | * Increment *nr_shrunk by the number of reclaimed extents. Also update | |
1299 | * ei->i_es_shrink_lblk to where we should continue scanning. | |
1300 | */ | |
1301 | static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end, | |
1302 | int *nr_to_scan, int *nr_shrunk) | |
74cd15cd ZL |
1303 | { |
1304 | struct inode *inode = &ei->vfs_inode; | |
1305 | struct ext4_es_tree *tree = &ei->i_es_tree; | |
74cd15cd | 1306 | struct extent_status *es; |
dd475925 | 1307 | struct rb_node *node; |
74cd15cd | 1308 | |
dd475925 JK |
1309 | es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk); |
1310 | if (!es) | |
1311 | goto out_wrap; | |
1312 | node = &es->rb_node; | |
1313 | while (*nr_to_scan > 0) { | |
1314 | if (es->es_lblk > end) { | |
1315 | ei->i_es_shrink_lblk = end + 1; | |
1316 | return 0; | |
1317 | } | |
7869a4a6 | 1318 | |
dd475925 | 1319 | (*nr_to_scan)--; |
74cd15cd ZL |
1320 | node = rb_next(&es->rb_node); |
1321 | /* | |
1322 | * We can't reclaim delayed extent from status tree because | |
1323 | * fiemap, bigallic, and seek_data/hole need to use it. | |
1324 | */ | |
2be12de9 JK |
1325 | if (ext4_es_is_delayed(es)) |
1326 | goto next; | |
1327 | if (ext4_es_is_referenced(es)) { | |
1328 | ext4_es_clear_referenced(es); | |
1329 | goto next; | |
74cd15cd | 1330 | } |
2be12de9 JK |
1331 | |
1332 | rb_erase(&es->rb_node, &tree->root); | |
1333 | ext4_es_free_extent(inode, es); | |
1334 | (*nr_shrunk)++; | |
1335 | next: | |
dd475925 JK |
1336 | if (!node) |
1337 | goto out_wrap; | |
1338 | es = rb_entry(node, struct extent_status, rb_node); | |
74cd15cd | 1339 | } |
dd475925 JK |
1340 | ei->i_es_shrink_lblk = es->es_lblk; |
1341 | return 1; | |
1342 | out_wrap: | |
1343 | ei->i_es_shrink_lblk = 0; | |
1344 | return 0; | |
1345 | } | |
1346 | ||
1347 | static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan) | |
1348 | { | |
1349 | struct inode *inode = &ei->vfs_inode; | |
1350 | int nr_shrunk = 0; | |
1351 | ext4_lblk_t start = ei->i_es_shrink_lblk; | |
1352 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, | |
1353 | DEFAULT_RATELIMIT_BURST); | |
1354 | ||
1355 | if (ei->i_es_shk_nr == 0) | |
1356 | return 0; | |
1357 | ||
1358 | if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) && | |
1359 | __ratelimit(&_rs)) | |
1360 | ext4_warning(inode->i_sb, "forced shrink of precached extents"); | |
1361 | ||
1362 | if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) && | |
1363 | start != 0) | |
1364 | es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk); | |
1365 | ||
1366 | ei->i_es_tree.cache_es = NULL; | |
74cd15cd ZL |
1367 | return nr_shrunk; |
1368 | } | |
1dc0aa46 EW |
1369 | |
1370 | #ifdef ES_DEBUG__ | |
1371 | static void ext4_print_pending_tree(struct inode *inode) | |
1372 | { | |
1373 | struct ext4_pending_tree *tree; | |
1374 | struct rb_node *node; | |
1375 | struct pending_reservation *pr; | |
1376 | ||
1377 | printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino); | |
1378 | tree = &EXT4_I(inode)->i_pending_tree; | |
1379 | node = rb_first(&tree->root); | |
1380 | while (node) { | |
1381 | pr = rb_entry(node, struct pending_reservation, rb_node); | |
1382 | printk(KERN_DEBUG " %u", pr->lclu); | |
1383 | node = rb_next(node); | |
1384 | } | |
1385 | printk(KERN_DEBUG "\n"); | |
1386 | } | |
1387 | #else | |
1388 | #define ext4_print_pending_tree(inode) | |
1389 | #endif | |
1390 | ||
1391 | int __init ext4_init_pending(void) | |
1392 | { | |
1393 | ext4_pending_cachep = kmem_cache_create("ext4_pending_reservation", | |
1394 | sizeof(struct pending_reservation), | |
1395 | 0, (SLAB_RECLAIM_ACCOUNT), NULL); | |
1396 | if (ext4_pending_cachep == NULL) | |
1397 | return -ENOMEM; | |
1398 | return 0; | |
1399 | } | |
1400 | ||
1401 | void ext4_exit_pending(void) | |
1402 | { | |
1403 | kmem_cache_destroy(ext4_pending_cachep); | |
1404 | } | |
1405 | ||
1406 | void ext4_init_pending_tree(struct ext4_pending_tree *tree) | |
1407 | { | |
1408 | tree->root = RB_ROOT; | |
1409 | } | |
1410 | ||
1411 | /* | |
1412 | * __get_pending - retrieve a pointer to a pending reservation | |
1413 | * | |
1414 | * @inode - file containing the pending cluster reservation | |
1415 | * @lclu - logical cluster of interest | |
1416 | * | |
1417 | * Returns a pointer to a pending reservation if it's a member of | |
1418 | * the set, and NULL if not. Must be called holding i_es_lock. | |
1419 | */ | |
1420 | static struct pending_reservation *__get_pending(struct inode *inode, | |
1421 | ext4_lblk_t lclu) | |
1422 | { | |
1423 | struct ext4_pending_tree *tree; | |
1424 | struct rb_node *node; | |
1425 | struct pending_reservation *pr = NULL; | |
1426 | ||
1427 | tree = &EXT4_I(inode)->i_pending_tree; | |
1428 | node = (&tree->root)->rb_node; | |
1429 | ||
1430 | while (node) { | |
1431 | pr = rb_entry(node, struct pending_reservation, rb_node); | |
1432 | if (lclu < pr->lclu) | |
1433 | node = node->rb_left; | |
1434 | else if (lclu > pr->lclu) | |
1435 | node = node->rb_right; | |
1436 | else if (lclu == pr->lclu) | |
1437 | return pr; | |
1438 | } | |
1439 | return NULL; | |
1440 | } | |
1441 | ||
1442 | /* | |
1443 | * __insert_pending - adds a pending cluster reservation to the set of | |
1444 | * pending reservations | |
1445 | * | |
1446 | * @inode - file containing the cluster | |
1447 | * @lblk - logical block in the cluster to be added | |
1448 | * | |
1449 | * Returns 0 on successful insertion and -ENOMEM on failure. If the | |
1450 | * pending reservation is already in the set, returns successfully. | |
1451 | */ | |
1452 | static int __insert_pending(struct inode *inode, ext4_lblk_t lblk) | |
1453 | { | |
1454 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1455 | struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree; | |
1456 | struct rb_node **p = &tree->root.rb_node; | |
1457 | struct rb_node *parent = NULL; | |
1458 | struct pending_reservation *pr; | |
1459 | ext4_lblk_t lclu; | |
1460 | int ret = 0; | |
1461 | ||
1462 | lclu = EXT4_B2C(sbi, lblk); | |
1463 | /* search to find parent for insertion */ | |
1464 | while (*p) { | |
1465 | parent = *p; | |
1466 | pr = rb_entry(parent, struct pending_reservation, rb_node); | |
1467 | ||
1468 | if (lclu < pr->lclu) { | |
1469 | p = &(*p)->rb_left; | |
1470 | } else if (lclu > pr->lclu) { | |
1471 | p = &(*p)->rb_right; | |
1472 | } else { | |
1473 | /* pending reservation already inserted */ | |
1474 | goto out; | |
1475 | } | |
1476 | } | |
1477 | ||
1478 | pr = kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC); | |
1479 | if (pr == NULL) { | |
1480 | ret = -ENOMEM; | |
1481 | goto out; | |
1482 | } | |
1483 | pr->lclu = lclu; | |
1484 | ||
1485 | rb_link_node(&pr->rb_node, parent, p); | |
1486 | rb_insert_color(&pr->rb_node, &tree->root); | |
1487 | ||
1488 | out: | |
1489 | return ret; | |
1490 | } | |
1491 | ||
1492 | /* | |
1493 | * __remove_pending - removes a pending cluster reservation from the set | |
1494 | * of pending reservations | |
1495 | * | |
1496 | * @inode - file containing the cluster | |
1497 | * @lblk - logical block in the pending cluster reservation to be removed | |
1498 | * | |
1499 | * Returns successfully if pending reservation is not a member of the set. | |
1500 | */ | |
1501 | static void __remove_pending(struct inode *inode, ext4_lblk_t lblk) | |
1502 | { | |
1503 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1504 | struct pending_reservation *pr; | |
1505 | struct ext4_pending_tree *tree; | |
1506 | ||
1507 | pr = __get_pending(inode, EXT4_B2C(sbi, lblk)); | |
1508 | if (pr != NULL) { | |
1509 | tree = &EXT4_I(inode)->i_pending_tree; | |
1510 | rb_erase(&pr->rb_node, &tree->root); | |
1511 | kmem_cache_free(ext4_pending_cachep, pr); | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | /* | |
1516 | * ext4_remove_pending - removes a pending cluster reservation from the set | |
1517 | * of pending reservations | |
1518 | * | |
1519 | * @inode - file containing the cluster | |
1520 | * @lblk - logical block in the pending cluster reservation to be removed | |
1521 | * | |
1522 | * Locking for external use of __remove_pending. | |
1523 | */ | |
1524 | void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk) | |
1525 | { | |
1526 | struct ext4_inode_info *ei = EXT4_I(inode); | |
1527 | ||
1528 | write_lock(&ei->i_es_lock); | |
1529 | __remove_pending(inode, lblk); | |
1530 | write_unlock(&ei->i_es_lock); | |
1531 | } | |
1532 | ||
1533 | /* | |
1534 | * ext4_is_pending - determine whether a cluster has a pending reservation | |
1535 | * on it | |
1536 | * | |
1537 | * @inode - file containing the cluster | |
1538 | * @lblk - logical block in the cluster | |
1539 | * | |
1540 | * Returns true if there's a pending reservation for the cluster in the | |
1541 | * set of pending reservations, and false if not. | |
1542 | */ | |
1543 | bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk) | |
1544 | { | |
1545 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1546 | struct ext4_inode_info *ei = EXT4_I(inode); | |
1547 | bool ret; | |
1548 | ||
1549 | read_lock(&ei->i_es_lock); | |
1550 | ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL); | |
1551 | read_unlock(&ei->i_es_lock); | |
1552 | ||
1553 | return ret; | |
1554 | } | |
0b02f4c0 EW |
1555 | |
1556 | /* | |
1557 | * ext4_es_insert_delayed_block - adds a delayed block to the extents status | |
1558 | * tree, adding a pending reservation where | |
1559 | * needed | |
1560 | * | |
1561 | * @inode - file containing the newly added block | |
1562 | * @lblk - logical block to be added | |
1563 | * @allocated - indicates whether a physical cluster has been allocated for | |
1564 | * the logical cluster that contains the block | |
1565 | * | |
1566 | * Returns 0 on success, negative error code on failure. | |
1567 | */ | |
1568 | int ext4_es_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk, | |
1569 | bool allocated) | |
1570 | { | |
1571 | struct extent_status newes; | |
1572 | int err = 0; | |
1573 | ||
1574 | es_debug("add [%u/1) delayed to extent status tree of inode %lu\n", | |
1575 | lblk, inode->i_ino); | |
1576 | ||
1577 | newes.es_lblk = lblk; | |
1578 | newes.es_len = 1; | |
1579 | ext4_es_store_pblock_status(&newes, ~0, EXTENT_STATUS_DELAYED); | |
1580 | trace_ext4_es_insert_delayed_block(inode, &newes, allocated); | |
1581 | ||
1582 | ext4_es_insert_extent_check(inode, &newes); | |
1583 | ||
1584 | write_lock(&EXT4_I(inode)->i_es_lock); | |
1585 | ||
1586 | err = __es_remove_extent(inode, lblk, lblk); | |
1587 | if (err != 0) | |
1588 | goto error; | |
1589 | retry: | |
1590 | err = __es_insert_extent(inode, &newes); | |
1591 | if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb), | |
1592 | 128, EXT4_I(inode))) | |
1593 | goto retry; | |
1594 | if (err != 0) | |
1595 | goto error; | |
1596 | ||
1597 | if (allocated) | |
1598 | __insert_pending(inode, lblk); | |
1599 | ||
1600 | error: | |
1601 | write_unlock(&EXT4_I(inode)->i_es_lock); | |
1602 | ||
1603 | ext4_es_print_tree(inode); | |
1604 | ext4_print_pending_tree(inode); | |
1605 | ||
1606 | return err; | |
1607 | } |