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> | |
d3922a77 | 13 | #include <linux/list_sort.h> |
eb68d0e2 ZL |
14 | #include <linux/proc_fs.h> |
15 | #include <linux/seq_file.h> | |
654598be ZL |
16 | #include "ext4.h" |
17 | #include "extents_status.h" | |
654598be | 18 | |
992e9fdd ZL |
19 | #include <trace/events/ext4.h> |
20 | ||
654598be ZL |
21 | /* |
22 | * According to previous discussion in Ext4 Developer Workshop, we | |
23 | * will introduce a new structure called io tree to track all extent | |
24 | * status in order to solve some problems that we have met | |
25 | * (e.g. Reservation space warning), and provide extent-level locking. | |
26 | * Delay extent tree is the first step to achieve this goal. It is | |
27 | * original built by Yongqiang Yang. At that time it is called delay | |
06b0c886 | 28 | * extent tree, whose goal is only track delayed extents in memory to |
654598be ZL |
29 | * simplify the implementation of fiemap and bigalloc, and introduce |
30 | * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called | |
06b0c886 ZL |
31 | * delay extent tree at the first commit. But for better understand |
32 | * what it does, it has been rename to extent status tree. | |
654598be | 33 | * |
06b0c886 ZL |
34 | * Step1: |
35 | * Currently the first step has been done. All delayed extents are | |
36 | * tracked in the tree. It maintains the delayed extent when a delayed | |
37 | * allocation is issued, and the delayed extent is written out or | |
654598be ZL |
38 | * invalidated. Therefore the implementation of fiemap and bigalloc |
39 | * are simplified, and SEEK_DATA/SEEK_HOLE are introduced. | |
40 | * | |
41 | * The following comment describes the implemenmtation of extent | |
42 | * status tree and future works. | |
06b0c886 ZL |
43 | * |
44 | * Step2: | |
45 | * In this step all extent status are tracked by extent status tree. | |
46 | * Thus, we can first try to lookup a block mapping in this tree before | |
47 | * finding it in extent tree. Hence, single extent cache can be removed | |
48 | * because extent status tree can do a better job. Extents in status | |
49 | * tree are loaded on-demand. Therefore, the extent status tree may not | |
50 | * contain all of the extents in a file. Meanwhile we define a shrinker | |
51 | * to reclaim memory from extent status tree because fragmented extent | |
52 | * tree will make status tree cost too much memory. written/unwritten/- | |
53 | * hole extents in the tree will be reclaimed by this shrinker when we | |
54 | * are under high memory pressure. Delayed extents will not be | |
55 | * reclimed because fiemap, bigalloc, and seek_data/hole need it. | |
654598be ZL |
56 | */ |
57 | ||
58 | /* | |
06b0c886 | 59 | * Extent status tree implementation for ext4. |
654598be ZL |
60 | * |
61 | * | |
62 | * ========================================================================== | |
06b0c886 | 63 | * Extent status tree tracks all extent status. |
654598be | 64 | * |
06b0c886 | 65 | * 1. Why we need to implement extent status tree? |
654598be | 66 | * |
06b0c886 | 67 | * Without extent status tree, ext4 identifies a delayed extent by looking |
654598be ZL |
68 | * up page cache, this has several deficiencies - complicated, buggy, |
69 | * and inefficient code. | |
70 | * | |
06b0c886 ZL |
71 | * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a |
72 | * block or a range of blocks are belonged to a delayed extent. | |
654598be | 73 | * |
06b0c886 | 74 | * Let us have a look at how they do without extent status tree. |
654598be ZL |
75 | * -- FIEMAP |
76 | * FIEMAP looks up page cache to identify delayed allocations from holes. | |
77 | * | |
78 | * -- SEEK_HOLE/DATA | |
79 | * SEEK_HOLE/DATA has the same problem as FIEMAP. | |
80 | * | |
81 | * -- bigalloc | |
82 | * bigalloc looks up page cache to figure out if a block is | |
83 | * already under delayed allocation or not to determine whether | |
84 | * quota reserving is needed for the cluster. | |
85 | * | |
654598be ZL |
86 | * -- writeout |
87 | * Writeout looks up whole page cache to see if a buffer is | |
88 | * mapped, If there are not very many delayed buffers, then it is | |
89 | * time comsuming. | |
90 | * | |
06b0c886 | 91 | * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA, |
654598be ZL |
92 | * bigalloc and writeout can figure out if a block or a range of |
93 | * blocks is under delayed allocation(belonged to a delayed extent) or | |
06b0c886 | 94 | * not by searching the extent tree. |
654598be ZL |
95 | * |
96 | * | |
97 | * ========================================================================== | |
06b0c886 ZL |
98 | * 2. Ext4 extent status tree impelmentation |
99 | * | |
100 | * -- extent | |
101 | * A extent is a range of blocks which are contiguous logically and | |
102 | * physically. Unlike extent in extent tree, this extent in ext4 is | |
103 | * a in-memory struct, there is no corresponding on-disk data. There | |
104 | * is no limit on length of extent, so an extent can contain as many | |
105 | * blocks as they are contiguous logically and physically. | |
654598be | 106 | * |
06b0c886 ZL |
107 | * -- extent status tree |
108 | * Every inode has an extent status tree and all allocation blocks | |
109 | * are added to the tree with different status. The extent in the | |
110 | * tree are ordered by logical block no. | |
654598be | 111 | * |
06b0c886 ZL |
112 | * -- operations on a extent status tree |
113 | * There are three important operations on a delayed extent tree: find | |
114 | * next extent, adding a extent(a range of blocks) and removing a extent. | |
654598be | 115 | * |
06b0c886 ZL |
116 | * -- race on a extent status tree |
117 | * Extent status tree is protected by inode->i_es_lock. | |
654598be | 118 | * |
06b0c886 ZL |
119 | * -- memory consumption |
120 | * Fragmented extent tree will make extent status tree cost too much | |
121 | * memory. Hence, we will reclaim written/unwritten/hole extents from | |
122 | * the tree under a heavy memory pressure. | |
654598be ZL |
123 | * |
124 | * | |
125 | * ========================================================================== | |
06b0c886 ZL |
126 | * 3. Performance analysis |
127 | * | |
654598be ZL |
128 | * -- overhead |
129 | * 1. There is a cache extent for write access, so if writes are | |
130 | * not very random, adding space operaions are in O(1) time. | |
131 | * | |
132 | * -- gain | |
133 | * 2. Code is much simpler, more readable, more maintainable and | |
134 | * more efficient. | |
135 | * | |
136 | * | |
137 | * ========================================================================== | |
138 | * 4. TODO list | |
654598be | 139 | * |
06b0c886 | 140 | * -- Refactor delayed space reservation |
654598be ZL |
141 | * |
142 | * -- Extent-level locking | |
143 | */ | |
144 | ||
145 | static struct kmem_cache *ext4_es_cachep; | |
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 | { | |
166 | if (ext4_es_cachep) | |
167 | kmem_cache_destroy(ext4_es_cachep); | |
168 | } | |
169 | ||
170 | void ext4_es_init_tree(struct ext4_es_tree *tree) | |
171 | { | |
172 | tree->root = RB_ROOT; | |
173 | tree->cache_es = NULL; | |
174 | } | |
175 | ||
176 | #ifdef ES_DEBUG__ | |
177 | static void ext4_es_print_tree(struct inode *inode) | |
178 | { | |
179 | struct ext4_es_tree *tree; | |
180 | struct rb_node *node; | |
181 | ||
182 | printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino); | |
183 | tree = &EXT4_I(inode)->i_es_tree; | |
184 | node = rb_first(&tree->root); | |
185 | while (node) { | |
186 | struct extent_status *es; | |
187 | es = rb_entry(node, struct extent_status, rb_node); | |
ce140cdd | 188 | printk(KERN_DEBUG " [%u/%u) %llu %x", |
fdc0212e ZL |
189 | es->es_lblk, es->es_len, |
190 | ext4_es_pblock(es), ext4_es_status(es)); | |
654598be ZL |
191 | node = rb_next(node); |
192 | } | |
193 | printk(KERN_DEBUG "\n"); | |
194 | } | |
195 | #else | |
196 | #define ext4_es_print_tree(inode) | |
197 | #endif | |
198 | ||
06b0c886 | 199 | static inline ext4_lblk_t ext4_es_end(struct extent_status *es) |
654598be | 200 | { |
06b0c886 ZL |
201 | BUG_ON(es->es_lblk + es->es_len < es->es_lblk); |
202 | return es->es_lblk + es->es_len - 1; | |
654598be ZL |
203 | } |
204 | ||
205 | /* | |
206 | * search through the tree for an delayed extent with a given offset. If | |
207 | * it can't be found, try to find next extent. | |
208 | */ | |
209 | static struct extent_status *__es_tree_search(struct rb_root *root, | |
06b0c886 | 210 | ext4_lblk_t lblk) |
654598be ZL |
211 | { |
212 | struct rb_node *node = root->rb_node; | |
213 | struct extent_status *es = NULL; | |
214 | ||
215 | while (node) { | |
216 | es = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 | 217 | if (lblk < es->es_lblk) |
654598be | 218 | node = node->rb_left; |
06b0c886 | 219 | else if (lblk > ext4_es_end(es)) |
654598be ZL |
220 | node = node->rb_right; |
221 | else | |
222 | return es; | |
223 | } | |
224 | ||
06b0c886 | 225 | if (es && lblk < es->es_lblk) |
654598be ZL |
226 | return es; |
227 | ||
06b0c886 | 228 | if (es && lblk > ext4_es_end(es)) { |
654598be ZL |
229 | node = rb_next(&es->rb_node); |
230 | return node ? rb_entry(node, struct extent_status, rb_node) : | |
231 | NULL; | |
232 | } | |
233 | ||
234 | return NULL; | |
235 | } | |
236 | ||
237 | /* | |
e30b5dca YZ |
238 | * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering |
239 | * @es->lblk if it exists, otherwise, the next extent after @es->lblk. | |
654598be ZL |
240 | * |
241 | * @inode: the inode which owns delayed extents | |
be401363 | 242 | * @lblk: the offset where we start to search |
e30b5dca | 243 | * @end: the offset where we stop to search |
654598be | 244 | * @es: delayed extent that we found |
654598be | 245 | */ |
e30b5dca YZ |
246 | void ext4_es_find_delayed_extent_range(struct inode *inode, |
247 | ext4_lblk_t lblk, ext4_lblk_t end, | |
be401363 | 248 | struct extent_status *es) |
654598be ZL |
249 | { |
250 | struct ext4_es_tree *tree = NULL; | |
251 | struct extent_status *es1 = NULL; | |
252 | struct rb_node *node; | |
654598be | 253 | |
be401363 | 254 | BUG_ON(es == NULL); |
e30b5dca YZ |
255 | BUG_ON(end < lblk); |
256 | trace_ext4_es_find_delayed_extent_range_enter(inode, lblk); | |
992e9fdd | 257 | |
654598be ZL |
258 | read_lock(&EXT4_I(inode)->i_es_lock); |
259 | tree = &EXT4_I(inode)->i_es_tree; | |
260 | ||
fdc0212e | 261 | /* find extent in cache firstly */ |
be401363 | 262 | es->es_lblk = es->es_len = es->es_pblk = 0; |
654598be ZL |
263 | if (tree->cache_es) { |
264 | es1 = tree->cache_es; | |
be401363 | 265 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
3be78c73 | 266 | es_debug("%u cached by [%u/%u) %llu %x\n", |
be401363 | 267 | lblk, es1->es_lblk, es1->es_len, |
fdc0212e | 268 | ext4_es_pblock(es1), ext4_es_status(es1)); |
654598be ZL |
269 | goto out; |
270 | } | |
271 | } | |
272 | ||
be401363 | 273 | es1 = __es_tree_search(&tree->root, lblk); |
654598be ZL |
274 | |
275 | out: | |
be401363 ZL |
276 | if (es1 && !ext4_es_is_delayed(es1)) { |
277 | while ((node = rb_next(&es1->rb_node)) != NULL) { | |
278 | es1 = rb_entry(node, struct extent_status, rb_node); | |
e30b5dca YZ |
279 | if (es1->es_lblk > end) { |
280 | es1 = NULL; | |
281 | break; | |
282 | } | |
be401363 ZL |
283 | if (ext4_es_is_delayed(es1)) |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
288 | if (es1 && ext4_es_is_delayed(es1)) { | |
654598be | 289 | tree->cache_es = es1; |
06b0c886 ZL |
290 | es->es_lblk = es1->es_lblk; |
291 | es->es_len = es1->es_len; | |
fdc0212e | 292 | es->es_pblk = es1->es_pblk; |
654598be ZL |
293 | } |
294 | ||
295 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
992e9fdd | 296 | |
e30b5dca | 297 | trace_ext4_es_find_delayed_extent_range_exit(inode, es); |
654598be ZL |
298 | } |
299 | ||
b0dea4c1 | 300 | static void ext4_es_list_add(struct inode *inode) |
edaa53ca ZL |
301 | { |
302 | struct ext4_inode_info *ei = EXT4_I(inode); | |
303 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
304 | ||
305 | if (!list_empty(&ei->i_es_list)) | |
306 | return; | |
307 | ||
308 | spin_lock(&sbi->s_es_lock); | |
309 | if (list_empty(&ei->i_es_list)) { | |
310 | list_add_tail(&ei->i_es_list, &sbi->s_es_list); | |
311 | sbi->s_es_nr_inode++; | |
312 | } | |
313 | spin_unlock(&sbi->s_es_lock); | |
314 | } | |
315 | ||
b0dea4c1 | 316 | static void ext4_es_list_del(struct inode *inode) |
edaa53ca ZL |
317 | { |
318 | struct ext4_inode_info *ei = EXT4_I(inode); | |
319 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
320 | ||
321 | spin_lock(&sbi->s_es_lock); | |
322 | if (!list_empty(&ei->i_es_list)) { | |
323 | list_del_init(&ei->i_es_list); | |
324 | sbi->s_es_nr_inode--; | |
325 | WARN_ON_ONCE(sbi->s_es_nr_inode < 0); | |
326 | } | |
327 | spin_unlock(&sbi->s_es_lock); | |
328 | } | |
329 | ||
654598be | 330 | static struct extent_status * |
bdedbb7b ZL |
331 | ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len, |
332 | ext4_fsblk_t pblk) | |
654598be ZL |
333 | { |
334 | struct extent_status *es; | |
335 | es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC); | |
336 | if (es == NULL) | |
337 | return NULL; | |
06b0c886 ZL |
338 | es->es_lblk = lblk; |
339 | es->es_len = len; | |
fdc0212e | 340 | es->es_pblk = pblk; |
74cd15cd ZL |
341 | |
342 | /* | |
343 | * We don't count delayed extent because we never try to reclaim them | |
344 | */ | |
24630774 | 345 | if (!ext4_es_is_delayed(es)) { |
b0dea4c1 JK |
346 | if (!EXT4_I(inode)->i_es_shk_nr++) |
347 | ext4_es_list_add(inode); | |
eb68d0e2 | 348 | percpu_counter_inc(&EXT4_SB(inode->i_sb)-> |
edaa53ca | 349 | s_es_stats.es_stats_shk_cnt); |
24630774 | 350 | } |
74cd15cd | 351 | |
eb68d0e2 ZL |
352 | EXT4_I(inode)->i_es_all_nr++; |
353 | percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt); | |
354 | ||
654598be ZL |
355 | return es; |
356 | } | |
357 | ||
bdedbb7b | 358 | static void ext4_es_free_extent(struct inode *inode, struct extent_status *es) |
654598be | 359 | { |
eb68d0e2 ZL |
360 | EXT4_I(inode)->i_es_all_nr--; |
361 | percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt); | |
362 | ||
edaa53ca | 363 | /* Decrease the shrink counter when this es is not delayed */ |
74cd15cd | 364 | if (!ext4_es_is_delayed(es)) { |
edaa53ca | 365 | BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0); |
b0dea4c1 JK |
366 | if (!--EXT4_I(inode)->i_es_shk_nr) |
367 | ext4_es_list_del(inode); | |
eb68d0e2 | 368 | percpu_counter_dec(&EXT4_SB(inode->i_sb)-> |
edaa53ca | 369 | s_es_stats.es_stats_shk_cnt); |
74cd15cd ZL |
370 | } |
371 | ||
654598be ZL |
372 | kmem_cache_free(ext4_es_cachep, es); |
373 | } | |
374 | ||
06b0c886 ZL |
375 | /* |
376 | * Check whether or not two extents can be merged | |
377 | * Condition: | |
378 | * - logical block number is contiguous | |
fdc0212e ZL |
379 | * - physical block number is contiguous |
380 | * - status is equal | |
06b0c886 ZL |
381 | */ |
382 | static int ext4_es_can_be_merged(struct extent_status *es1, | |
383 | struct extent_status *es2) | |
384 | { | |
bd384364 | 385 | if (ext4_es_status(es1) != ext4_es_status(es2)) |
06b0c886 ZL |
386 | return 0; |
387 | ||
0baaea64 LC |
388 | if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) { |
389 | pr_warn("ES assertion failed when merging extents. " | |
390 | "The sum of lengths of es1 (%d) and es2 (%d) " | |
391 | "is bigger than allowed file size (%d)\n", | |
392 | es1->es_len, es2->es_len, EXT_MAX_BLOCKS); | |
393 | WARN_ON(1); | |
fdc0212e | 394 | return 0; |
0baaea64 | 395 | } |
fdc0212e | 396 | |
bd384364 | 397 | if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk) |
fdc0212e ZL |
398 | return 0; |
399 | ||
bd384364 ZL |
400 | if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) && |
401 | (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2))) | |
402 | return 1; | |
403 | ||
404 | if (ext4_es_is_hole(es1)) | |
405 | return 1; | |
406 | ||
407 | /* we need to check delayed extent is without unwritten status */ | |
408 | if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1)) | |
409 | return 1; | |
410 | ||
411 | return 0; | |
06b0c886 ZL |
412 | } |
413 | ||
654598be | 414 | static struct extent_status * |
bdedbb7b | 415 | ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es) |
654598be | 416 | { |
bdedbb7b | 417 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
418 | struct extent_status *es1; |
419 | struct rb_node *node; | |
420 | ||
421 | node = rb_prev(&es->rb_node); | |
422 | if (!node) | |
423 | return es; | |
424 | ||
425 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
426 | if (ext4_es_can_be_merged(es1, es)) { |
427 | es1->es_len += es->es_len; | |
654598be | 428 | rb_erase(&es->rb_node, &tree->root); |
bdedbb7b | 429 | ext4_es_free_extent(inode, es); |
654598be ZL |
430 | es = es1; |
431 | } | |
432 | ||
433 | return es; | |
434 | } | |
435 | ||
436 | static struct extent_status * | |
bdedbb7b | 437 | ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es) |
654598be | 438 | { |
bdedbb7b | 439 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
440 | struct extent_status *es1; |
441 | struct rb_node *node; | |
442 | ||
443 | node = rb_next(&es->rb_node); | |
444 | if (!node) | |
445 | return es; | |
446 | ||
447 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
448 | if (ext4_es_can_be_merged(es, es1)) { |
449 | es->es_len += es1->es_len; | |
654598be | 450 | rb_erase(node, &tree->root); |
bdedbb7b | 451 | ext4_es_free_extent(inode, es1); |
654598be ZL |
452 | } |
453 | ||
454 | return es; | |
455 | } | |
456 | ||
921f266b | 457 | #ifdef ES_AGGRESSIVE_TEST |
d7b2a00c ZL |
458 | #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */ |
459 | ||
921f266b DM |
460 | static void ext4_es_insert_extent_ext_check(struct inode *inode, |
461 | struct extent_status *es) | |
462 | { | |
463 | struct ext4_ext_path *path = NULL; | |
464 | struct ext4_extent *ex; | |
465 | ext4_lblk_t ee_block; | |
466 | ext4_fsblk_t ee_start; | |
467 | unsigned short ee_len; | |
468 | int depth, ee_status, es_status; | |
469 | ||
ed8a1a76 | 470 | path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE); |
921f266b DM |
471 | if (IS_ERR(path)) |
472 | return; | |
473 | ||
474 | depth = ext_depth(inode); | |
475 | ex = path[depth].p_ext; | |
476 | ||
477 | if (ex) { | |
478 | ||
479 | ee_block = le32_to_cpu(ex->ee_block); | |
480 | ee_start = ext4_ext_pblock(ex); | |
481 | ee_len = ext4_ext_get_actual_len(ex); | |
482 | ||
556615dc | 483 | ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0; |
921f266b DM |
484 | es_status = ext4_es_is_unwritten(es) ? 1 : 0; |
485 | ||
486 | /* | |
487 | * Make sure ex and es are not overlap when we try to insert | |
488 | * a delayed/hole extent. | |
489 | */ | |
490 | if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) { | |
491 | if (in_range(es->es_lblk, ee_block, ee_len)) { | |
bdafe42a | 492 | pr_warn("ES insert assertion failed for " |
921f266b DM |
493 | "inode: %lu we can find an extent " |
494 | "at block [%d/%d/%llu/%c], but we " | |
ce140cdd EW |
495 | "want to add a delayed/hole extent " |
496 | "[%d/%d/%llu/%x]\n", | |
921f266b DM |
497 | inode->i_ino, ee_block, ee_len, |
498 | ee_start, ee_status ? 'u' : 'w', | |
499 | es->es_lblk, es->es_len, | |
500 | ext4_es_pblock(es), ext4_es_status(es)); | |
501 | } | |
502 | goto out; | |
503 | } | |
504 | ||
505 | /* | |
506 | * We don't check ee_block == es->es_lblk, etc. because es | |
507 | * might be a part of whole extent, vice versa. | |
508 | */ | |
509 | if (es->es_lblk < ee_block || | |
510 | ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) { | |
bdafe42a | 511 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
512 | "ex_status [%d/%d/%llu/%c] != " |
513 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, | |
514 | ee_block, ee_len, ee_start, | |
515 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, | |
516 | ext4_es_pblock(es), es_status ? 'u' : 'w'); | |
517 | goto out; | |
518 | } | |
519 | ||
520 | if (ee_status ^ es_status) { | |
bdafe42a | 521 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
522 | "ex_status [%d/%d/%llu/%c] != " |
523 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, | |
524 | ee_block, ee_len, ee_start, | |
525 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, | |
526 | ext4_es_pblock(es), es_status ? 'u' : 'w'); | |
527 | } | |
528 | } else { | |
529 | /* | |
530 | * We can't find an extent on disk. So we need to make sure | |
531 | * that we don't want to add an written/unwritten extent. | |
532 | */ | |
533 | if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) { | |
bdafe42a | 534 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b | 535 | "can't find an extent at block %d but we want " |
ce140cdd EW |
536 | "to add a written/unwritten extent " |
537 | "[%d/%d/%llu/%x]\n", inode->i_ino, | |
921f266b DM |
538 | es->es_lblk, es->es_lblk, es->es_len, |
539 | ext4_es_pblock(es), ext4_es_status(es)); | |
540 | } | |
541 | } | |
542 | out: | |
b7ea89ad TT |
543 | ext4_ext_drop_refs(path); |
544 | kfree(path); | |
921f266b DM |
545 | } |
546 | ||
547 | static void ext4_es_insert_extent_ind_check(struct inode *inode, | |
548 | struct extent_status *es) | |
549 | { | |
550 | struct ext4_map_blocks map; | |
551 | int retval; | |
552 | ||
553 | /* | |
554 | * Here we call ext4_ind_map_blocks to lookup a block mapping because | |
555 | * 'Indirect' structure is defined in indirect.c. So we couldn't | |
556 | * access direct/indirect tree from outside. It is too dirty to define | |
557 | * this function in indirect.c file. | |
558 | */ | |
559 | ||
560 | map.m_lblk = es->es_lblk; | |
561 | map.m_len = es->es_len; | |
562 | ||
563 | retval = ext4_ind_map_blocks(NULL, inode, &map, 0); | |
564 | if (retval > 0) { | |
565 | if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) { | |
566 | /* | |
567 | * We want to add a delayed/hole extent but this | |
568 | * block has been allocated. | |
569 | */ | |
bdafe42a | 570 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b | 571 | "We can find blocks but we want to add a " |
ce140cdd | 572 | "delayed/hole extent [%d/%d/%llu/%x]\n", |
921f266b DM |
573 | inode->i_ino, es->es_lblk, es->es_len, |
574 | ext4_es_pblock(es), ext4_es_status(es)); | |
575 | return; | |
576 | } else if (ext4_es_is_written(es)) { | |
577 | if (retval != es->es_len) { | |
bdafe42a | 578 | pr_warn("ES insert assertion failed for " |
921f266b DM |
579 | "inode: %lu retval %d != es_len %d\n", |
580 | inode->i_ino, retval, es->es_len); | |
581 | return; | |
582 | } | |
583 | if (map.m_pblk != ext4_es_pblock(es)) { | |
bdafe42a | 584 | pr_warn("ES insert assertion failed for " |
921f266b DM |
585 | "inode: %lu m_pblk %llu != " |
586 | "es_pblk %llu\n", | |
587 | inode->i_ino, map.m_pblk, | |
588 | ext4_es_pblock(es)); | |
589 | return; | |
590 | } | |
591 | } else { | |
592 | /* | |
593 | * We don't need to check unwritten extent because | |
594 | * indirect-based file doesn't have it. | |
595 | */ | |
596 | BUG_ON(1); | |
597 | } | |
598 | } else if (retval == 0) { | |
599 | if (ext4_es_is_written(es)) { | |
bdafe42a | 600 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b | 601 | "We can't find the block but we want to add " |
ce140cdd | 602 | "a written extent [%d/%d/%llu/%x]\n", |
921f266b DM |
603 | inode->i_ino, es->es_lblk, es->es_len, |
604 | ext4_es_pblock(es), ext4_es_status(es)); | |
605 | return; | |
606 | } | |
607 | } | |
608 | } | |
609 | ||
610 | static inline void ext4_es_insert_extent_check(struct inode *inode, | |
611 | struct extent_status *es) | |
612 | { | |
613 | /* | |
614 | * We don't need to worry about the race condition because | |
615 | * caller takes i_data_sem locking. | |
616 | */ | |
617 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); | |
618 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) | |
619 | ext4_es_insert_extent_ext_check(inode, es); | |
620 | else | |
621 | ext4_es_insert_extent_ind_check(inode, es); | |
622 | } | |
623 | #else | |
624 | static inline void ext4_es_insert_extent_check(struct inode *inode, | |
625 | struct extent_status *es) | |
626 | { | |
627 | } | |
628 | #endif | |
629 | ||
bdedbb7b | 630 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes) |
654598be | 631 | { |
bdedbb7b | 632 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
633 | struct rb_node **p = &tree->root.rb_node; |
634 | struct rb_node *parent = NULL; | |
635 | struct extent_status *es; | |
654598be ZL |
636 | |
637 | while (*p) { | |
638 | parent = *p; | |
639 | es = rb_entry(parent, struct extent_status, rb_node); | |
640 | ||
06b0c886 ZL |
641 | if (newes->es_lblk < es->es_lblk) { |
642 | if (ext4_es_can_be_merged(newes, es)) { | |
643 | /* | |
644 | * Here we can modify es_lblk directly | |
645 | * because it isn't overlapped. | |
646 | */ | |
647 | es->es_lblk = newes->es_lblk; | |
648 | es->es_len += newes->es_len; | |
fdc0212e ZL |
649 | if (ext4_es_is_written(es) || |
650 | ext4_es_is_unwritten(es)) | |
651 | ext4_es_store_pblock(es, | |
652 | newes->es_pblk); | |
bdedbb7b | 653 | es = ext4_es_try_to_merge_left(inode, es); |
654598be ZL |
654 | goto out; |
655 | } | |
656 | p = &(*p)->rb_left; | |
06b0c886 ZL |
657 | } else if (newes->es_lblk > ext4_es_end(es)) { |
658 | if (ext4_es_can_be_merged(es, newes)) { | |
659 | es->es_len += newes->es_len; | |
bdedbb7b | 660 | es = ext4_es_try_to_merge_right(inode, es); |
654598be ZL |
661 | goto out; |
662 | } | |
663 | p = &(*p)->rb_right; | |
664 | } else { | |
06b0c886 ZL |
665 | BUG_ON(1); |
666 | return -EINVAL; | |
654598be ZL |
667 | } |
668 | } | |
669 | ||
bdedbb7b | 670 | es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len, |
fdc0212e | 671 | newes->es_pblk); |
654598be ZL |
672 | if (!es) |
673 | return -ENOMEM; | |
674 | rb_link_node(&es->rb_node, parent, p); | |
675 | rb_insert_color(&es->rb_node, &tree->root); | |
676 | ||
677 | out: | |
678 | tree->cache_es = es; | |
679 | return 0; | |
680 | } | |
681 | ||
682 | /* | |
bdafe42a TT |
683 | * ext4_es_insert_extent() adds information to an inode's extent |
684 | * status tree. | |
654598be ZL |
685 | * |
686 | * Return 0 on success, error code on failure. | |
687 | */ | |
06b0c886 | 688 | int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk, |
fdc0212e | 689 | ext4_lblk_t len, ext4_fsblk_t pblk, |
3be78c73 | 690 | unsigned int status) |
654598be | 691 | { |
06b0c886 ZL |
692 | struct extent_status newes; |
693 | ext4_lblk_t end = lblk + len - 1; | |
654598be ZL |
694 | int err = 0; |
695 | ||
3be78c73 | 696 | es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n", |
fdc0212e | 697 | lblk, len, pblk, status, inode->i_ino); |
06b0c886 | 698 | |
d4381472 EG |
699 | if (!len) |
700 | return 0; | |
701 | ||
06b0c886 ZL |
702 | BUG_ON(end < lblk); |
703 | ||
704 | newes.es_lblk = lblk; | |
705 | newes.es_len = len; | |
9a6633b1 | 706 | ext4_es_store_pblock_status(&newes, pblk, status); |
fdc0212e | 707 | trace_ext4_es_insert_extent(inode, &newes); |
654598be | 708 | |
921f266b DM |
709 | ext4_es_insert_extent_check(inode, &newes); |
710 | ||
654598be | 711 | write_lock(&EXT4_I(inode)->i_es_lock); |
bdedbb7b | 712 | err = __es_remove_extent(inode, lblk, end); |
06b0c886 ZL |
713 | if (err != 0) |
714 | goto error; | |
e15f742c | 715 | retry: |
bdedbb7b | 716 | err = __es_insert_extent(inode, &newes); |
edaa53ca | 717 | if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb), |
dd475925 | 718 | 128, EXT4_I(inode))) |
e15f742c TT |
719 | goto retry; |
720 | if (err == -ENOMEM && !ext4_es_is_delayed(&newes)) | |
721 | err = 0; | |
06b0c886 ZL |
722 | |
723 | error: | |
654598be ZL |
724 | write_unlock(&EXT4_I(inode)->i_es_lock); |
725 | ||
726 | ext4_es_print_tree(inode); | |
727 | ||
728 | return err; | |
729 | } | |
730 | ||
107a7bd3 TT |
731 | /* |
732 | * ext4_es_cache_extent() inserts information into the extent status | |
733 | * tree if and only if there isn't information about the range in | |
734 | * question already. | |
735 | */ | |
736 | void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk, | |
737 | ext4_lblk_t len, ext4_fsblk_t pblk, | |
738 | unsigned int status) | |
739 | { | |
740 | struct extent_status *es; | |
741 | struct extent_status newes; | |
742 | ext4_lblk_t end = lblk + len - 1; | |
743 | ||
744 | newes.es_lblk = lblk; | |
745 | newes.es_len = len; | |
9a6633b1 | 746 | ext4_es_store_pblock_status(&newes, pblk, status); |
107a7bd3 TT |
747 | trace_ext4_es_cache_extent(inode, &newes); |
748 | ||
749 | if (!len) | |
750 | return; | |
751 | ||
752 | BUG_ON(end < lblk); | |
753 | ||
754 | write_lock(&EXT4_I(inode)->i_es_lock); | |
755 | ||
756 | es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk); | |
7869a4a6 TT |
757 | if (!es || es->es_lblk > end) |
758 | __es_insert_extent(inode, &newes); | |
107a7bd3 TT |
759 | write_unlock(&EXT4_I(inode)->i_es_lock); |
760 | } | |
761 | ||
d100eef2 ZL |
762 | /* |
763 | * ext4_es_lookup_extent() looks up an extent in extent status tree. | |
764 | * | |
765 | * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks. | |
766 | * | |
767 | * Return: 1 on found, 0 on not | |
768 | */ | |
769 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, | |
770 | struct extent_status *es) | |
771 | { | |
772 | struct ext4_es_tree *tree; | |
eb68d0e2 | 773 | struct ext4_es_stats *stats; |
d100eef2 ZL |
774 | struct extent_status *es1 = NULL; |
775 | struct rb_node *node; | |
776 | int found = 0; | |
777 | ||
778 | trace_ext4_es_lookup_extent_enter(inode, lblk); | |
779 | es_debug("lookup extent in block %u\n", lblk); | |
780 | ||
781 | tree = &EXT4_I(inode)->i_es_tree; | |
782 | read_lock(&EXT4_I(inode)->i_es_lock); | |
783 | ||
784 | /* find extent in cache firstly */ | |
785 | es->es_lblk = es->es_len = es->es_pblk = 0; | |
786 | if (tree->cache_es) { | |
787 | es1 = tree->cache_es; | |
788 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { | |
789 | es_debug("%u cached by [%u/%u)\n", | |
790 | lblk, es1->es_lblk, es1->es_len); | |
791 | found = 1; | |
792 | goto out; | |
793 | } | |
794 | } | |
795 | ||
796 | node = tree->root.rb_node; | |
797 | while (node) { | |
798 | es1 = rb_entry(node, struct extent_status, rb_node); | |
799 | if (lblk < es1->es_lblk) | |
800 | node = node->rb_left; | |
801 | else if (lblk > ext4_es_end(es1)) | |
802 | node = node->rb_right; | |
803 | else { | |
804 | found = 1; | |
805 | break; | |
806 | } | |
807 | } | |
808 | ||
809 | out: | |
eb68d0e2 | 810 | stats = &EXT4_SB(inode->i_sb)->s_es_stats; |
d100eef2 ZL |
811 | if (found) { |
812 | BUG_ON(!es1); | |
813 | es->es_lblk = es1->es_lblk; | |
814 | es->es_len = es1->es_len; | |
815 | es->es_pblk = es1->es_pblk; | |
eb68d0e2 ZL |
816 | stats->es_stats_cache_hits++; |
817 | } else { | |
818 | stats->es_stats_cache_misses++; | |
d100eef2 ZL |
819 | } |
820 | ||
821 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
822 | ||
823 | trace_ext4_es_lookup_extent_exit(inode, es, found); | |
824 | return found; | |
825 | } | |
826 | ||
bdedbb7b ZL |
827 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
828 | ext4_lblk_t end) | |
654598be | 829 | { |
bdedbb7b | 830 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be | 831 | struct rb_node *node; |
654598be ZL |
832 | struct extent_status *es; |
833 | struct extent_status orig_es; | |
06b0c886 | 834 | ext4_lblk_t len1, len2; |
fdc0212e | 835 | ext4_fsblk_t block; |
e15f742c | 836 | int err; |
654598be | 837 | |
e15f742c TT |
838 | retry: |
839 | err = 0; | |
06b0c886 | 840 | es = __es_tree_search(&tree->root, lblk); |
654598be ZL |
841 | if (!es) |
842 | goto out; | |
06b0c886 | 843 | if (es->es_lblk > end) |
654598be ZL |
844 | goto out; |
845 | ||
846 | /* Simply invalidate cache_es. */ | |
847 | tree->cache_es = NULL; | |
848 | ||
06b0c886 ZL |
849 | orig_es.es_lblk = es->es_lblk; |
850 | orig_es.es_len = es->es_len; | |
fdc0212e ZL |
851 | orig_es.es_pblk = es->es_pblk; |
852 | ||
06b0c886 ZL |
853 | len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0; |
854 | len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0; | |
654598be | 855 | if (len1 > 0) |
06b0c886 | 856 | es->es_len = len1; |
654598be ZL |
857 | if (len2 > 0) { |
858 | if (len1 > 0) { | |
06b0c886 ZL |
859 | struct extent_status newes; |
860 | ||
861 | newes.es_lblk = end + 1; | |
862 | newes.es_len = len2; | |
666525df | 863 | block = 0x7FDEADBEEFULL; |
fdc0212e | 864 | if (ext4_es_is_written(&orig_es) || |
9a6633b1 | 865 | ext4_es_is_unwritten(&orig_es)) |
fdc0212e ZL |
866 | block = ext4_es_pblock(&orig_es) + |
867 | orig_es.es_len - len2; | |
9a6633b1 TT |
868 | ext4_es_store_pblock_status(&newes, block, |
869 | ext4_es_status(&orig_es)); | |
bdedbb7b | 870 | err = __es_insert_extent(inode, &newes); |
654598be | 871 | if (err) { |
06b0c886 ZL |
872 | es->es_lblk = orig_es.es_lblk; |
873 | es->es_len = orig_es.es_len; | |
e15f742c | 874 | if ((err == -ENOMEM) && |
edaa53ca | 875 | __es_shrink(EXT4_SB(inode->i_sb), |
dd475925 | 876 | 128, EXT4_I(inode))) |
e15f742c | 877 | goto retry; |
654598be ZL |
878 | goto out; |
879 | } | |
880 | } else { | |
06b0c886 ZL |
881 | es->es_lblk = end + 1; |
882 | es->es_len = len2; | |
fdc0212e ZL |
883 | if (ext4_es_is_written(es) || |
884 | ext4_es_is_unwritten(es)) { | |
885 | block = orig_es.es_pblk + orig_es.es_len - len2; | |
886 | ext4_es_store_pblock(es, block); | |
887 | } | |
654598be ZL |
888 | } |
889 | goto out; | |
890 | } | |
891 | ||
892 | if (len1 > 0) { | |
893 | node = rb_next(&es->rb_node); | |
894 | if (node) | |
895 | es = rb_entry(node, struct extent_status, rb_node); | |
896 | else | |
897 | es = NULL; | |
898 | } | |
899 | ||
06b0c886 | 900 | while (es && ext4_es_end(es) <= end) { |
654598be ZL |
901 | node = rb_next(&es->rb_node); |
902 | rb_erase(&es->rb_node, &tree->root); | |
bdedbb7b | 903 | ext4_es_free_extent(inode, es); |
654598be ZL |
904 | if (!node) { |
905 | es = NULL; | |
906 | break; | |
907 | } | |
908 | es = rb_entry(node, struct extent_status, rb_node); | |
909 | } | |
910 | ||
06b0c886 | 911 | if (es && es->es_lblk < end + 1) { |
fdc0212e ZL |
912 | ext4_lblk_t orig_len = es->es_len; |
913 | ||
06b0c886 ZL |
914 | len1 = ext4_es_end(es) - end; |
915 | es->es_lblk = end + 1; | |
916 | es->es_len = len1; | |
fdc0212e ZL |
917 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
918 | block = es->es_pblk + orig_len - len1; | |
919 | ext4_es_store_pblock(es, block); | |
920 | } | |
654598be ZL |
921 | } |
922 | ||
923 | out: | |
06b0c886 ZL |
924 | return err; |
925 | } | |
926 | ||
927 | /* | |
928 | * ext4_es_remove_extent() removes a space from a extent status tree. | |
929 | * | |
930 | * Return 0 on success, error code on failure. | |
931 | */ | |
932 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, | |
933 | ext4_lblk_t len) | |
934 | { | |
06b0c886 ZL |
935 | ext4_lblk_t end; |
936 | int err = 0; | |
937 | ||
938 | trace_ext4_es_remove_extent(inode, lblk, len); | |
939 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", | |
940 | lblk, len, inode->i_ino); | |
941 | ||
d4381472 EG |
942 | if (!len) |
943 | return err; | |
944 | ||
06b0c886 ZL |
945 | end = lblk + len - 1; |
946 | BUG_ON(end < lblk); | |
947 | ||
edaa53ca ZL |
948 | /* |
949 | * ext4_clear_inode() depends on us taking i_es_lock unconditionally | |
950 | * so that we are sure __es_shrink() is done with the inode before it | |
951 | * is reclaimed. | |
952 | */ | |
06b0c886 | 953 | write_lock(&EXT4_I(inode)->i_es_lock); |
bdedbb7b | 954 | err = __es_remove_extent(inode, lblk, end); |
654598be ZL |
955 | write_unlock(&EXT4_I(inode)->i_es_lock); |
956 | ext4_es_print_tree(inode); | |
957 | return err; | |
958 | } | |
74cd15cd | 959 | |
edaa53ca ZL |
960 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
961 | struct ext4_inode_info *locked_ei) | |
74cd15cd | 962 | { |
74cd15cd | 963 | struct ext4_inode_info *ei; |
eb68d0e2 | 964 | struct ext4_es_stats *es_stats; |
eb68d0e2 ZL |
965 | ktime_t start_time; |
966 | u64 scan_time; | |
edaa53ca | 967 | int nr_to_walk; |
1ab6c499 | 968 | int nr_shrunk = 0; |
edaa53ca | 969 | int retried = 0, nr_skipped = 0; |
74cd15cd | 970 | |
eb68d0e2 ZL |
971 | es_stats = &sbi->s_es_stats; |
972 | start_time = ktime_get(); | |
d3922a77 | 973 | |
7869a4a6 | 974 | retry: |
edaa53ca ZL |
975 | spin_lock(&sbi->s_es_lock); |
976 | nr_to_walk = sbi->s_es_nr_inode; | |
977 | while (nr_to_walk-- > 0) { | |
edaa53ca ZL |
978 | if (list_empty(&sbi->s_es_list)) { |
979 | spin_unlock(&sbi->s_es_lock); | |
980 | goto out; | |
981 | } | |
982 | ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info, | |
983 | i_es_list); | |
984 | /* Move the inode to the tail */ | |
dd475925 | 985 | list_move_tail(&ei->i_es_list, &sbi->s_es_list); |
74cd15cd | 986 | |
7869a4a6 | 987 | /* |
edaa53ca ZL |
988 | * Normally we try hard to avoid shrinking precached inodes, |
989 | * but we will as a last resort. | |
7869a4a6 | 990 | */ |
edaa53ca ZL |
991 | if (!retried && ext4_test_inode_state(&ei->vfs_inode, |
992 | EXT4_STATE_EXT_PRECACHED)) { | |
7869a4a6 | 993 | nr_skipped++; |
74cd15cd ZL |
994 | continue; |
995 | } | |
d3922a77 | 996 | |
edaa53ca ZL |
997 | if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) { |
998 | nr_skipped++; | |
d3922a77 | 999 | continue; |
edaa53ca ZL |
1000 | } |
1001 | /* | |
1002 | * Now we hold i_es_lock which protects us from inode reclaim | |
1003 | * freeing inode under us | |
1004 | */ | |
1005 | spin_unlock(&sbi->s_es_lock); | |
74cd15cd | 1006 | |
dd475925 | 1007 | nr_shrunk += es_reclaim_extents(ei, &nr_to_scan); |
74cd15cd ZL |
1008 | write_unlock(&ei->i_es_lock); |
1009 | ||
dd475925 | 1010 | if (nr_to_scan <= 0) |
edaa53ca ZL |
1011 | goto out; |
1012 | spin_lock(&sbi->s_es_lock); | |
74cd15cd | 1013 | } |
edaa53ca | 1014 | spin_unlock(&sbi->s_es_lock); |
7869a4a6 TT |
1015 | |
1016 | /* | |
1017 | * If we skipped any inodes, and we weren't able to make any | |
edaa53ca | 1018 | * forward progress, try again to scan precached inodes. |
7869a4a6 TT |
1019 | */ |
1020 | if ((nr_shrunk == 0) && nr_skipped && !retried) { | |
1021 | retried++; | |
7869a4a6 TT |
1022 | goto retry; |
1023 | } | |
1024 | ||
e15f742c | 1025 | if (locked_ei && nr_shrunk == 0) |
dd475925 | 1026 | nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan); |
e15f742c | 1027 | |
edaa53ca | 1028 | out: |
eb68d0e2 ZL |
1029 | scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time)); |
1030 | if (likely(es_stats->es_stats_scan_time)) | |
1031 | es_stats->es_stats_scan_time = (scan_time + | |
1032 | es_stats->es_stats_scan_time*3) / 4; | |
1033 | else | |
1034 | es_stats->es_stats_scan_time = scan_time; | |
1035 | if (scan_time > es_stats->es_stats_max_scan_time) | |
1036 | es_stats->es_stats_max_scan_time = scan_time; | |
1037 | if (likely(es_stats->es_stats_shrunk)) | |
1038 | es_stats->es_stats_shrunk = (nr_shrunk + | |
1039 | es_stats->es_stats_shrunk*3) / 4; | |
1040 | else | |
1041 | es_stats->es_stats_shrunk = nr_shrunk; | |
1042 | ||
edaa53ca | 1043 | trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time, |
eb68d0e2 | 1044 | nr_skipped, retried); |
e15f742c TT |
1045 | return nr_shrunk; |
1046 | } | |
1047 | ||
1ab6c499 DC |
1048 | static unsigned long ext4_es_count(struct shrinker *shrink, |
1049 | struct shrink_control *sc) | |
1050 | { | |
1051 | unsigned long nr; | |
1052 | struct ext4_sb_info *sbi; | |
1053 | ||
1054 | sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker); | |
edaa53ca | 1055 | nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt); |
e963bb1d | 1056 | trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr); |
1ab6c499 DC |
1057 | return nr; |
1058 | } | |
1059 | ||
1060 | static unsigned long ext4_es_scan(struct shrinker *shrink, | |
1061 | struct shrink_control *sc) | |
e15f742c TT |
1062 | { |
1063 | struct ext4_sb_info *sbi = container_of(shrink, | |
1064 | struct ext4_sb_info, s_es_shrinker); | |
1065 | int nr_to_scan = sc->nr_to_scan; | |
1066 | int ret, nr_shrunk; | |
1067 | ||
edaa53ca | 1068 | ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt); |
e963bb1d | 1069 | trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret); |
e15f742c TT |
1070 | |
1071 | if (!nr_to_scan) | |
1072 | return ret; | |
1073 | ||
edaa53ca | 1074 | nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL); |
e15f742c | 1075 | |
e963bb1d | 1076 | trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret); |
1ab6c499 | 1077 | return nr_shrunk; |
74cd15cd ZL |
1078 | } |
1079 | ||
eb68d0e2 | 1080 | static void *ext4_es_seq_shrinker_info_start(struct seq_file *seq, loff_t *pos) |
74cd15cd | 1081 | { |
eb68d0e2 ZL |
1082 | return *pos ? NULL : SEQ_START_TOKEN; |
1083 | } | |
1084 | ||
1085 | static void * | |
1086 | ext4_es_seq_shrinker_info_next(struct seq_file *seq, void *v, loff_t *pos) | |
1087 | { | |
1088 | return NULL; | |
1089 | } | |
1090 | ||
1091 | static int ext4_es_seq_shrinker_info_show(struct seq_file *seq, void *v) | |
1092 | { | |
1093 | struct ext4_sb_info *sbi = seq->private; | |
1094 | struct ext4_es_stats *es_stats = &sbi->s_es_stats; | |
1095 | struct ext4_inode_info *ei, *max = NULL; | |
1096 | unsigned int inode_cnt = 0; | |
1097 | ||
1098 | if (v != SEQ_START_TOKEN) | |
1099 | return 0; | |
1100 | ||
1101 | /* here we just find an inode that has the max nr. of objects */ | |
edaa53ca ZL |
1102 | spin_lock(&sbi->s_es_lock); |
1103 | list_for_each_entry(ei, &sbi->s_es_list, i_es_list) { | |
eb68d0e2 ZL |
1104 | inode_cnt++; |
1105 | if (max && max->i_es_all_nr < ei->i_es_all_nr) | |
1106 | max = ei; | |
1107 | else if (!max) | |
1108 | max = ei; | |
1109 | } | |
edaa53ca | 1110 | spin_unlock(&sbi->s_es_lock); |
eb68d0e2 ZL |
1111 | |
1112 | seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n", | |
1113 | percpu_counter_sum_positive(&es_stats->es_stats_all_cnt), | |
edaa53ca | 1114 | percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt)); |
eb68d0e2 ZL |
1115 | seq_printf(seq, " %lu/%lu cache hits/misses\n", |
1116 | es_stats->es_stats_cache_hits, | |
1117 | es_stats->es_stats_cache_misses); | |
eb68d0e2 | 1118 | if (inode_cnt) |
edaa53ca | 1119 | seq_printf(seq, " %d inodes on list\n", inode_cnt); |
eb68d0e2 ZL |
1120 | |
1121 | seq_printf(seq, "average:\n %llu us scan time\n", | |
1122 | div_u64(es_stats->es_stats_scan_time, 1000)); | |
1123 | seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk); | |
1124 | if (inode_cnt) | |
1125 | seq_printf(seq, | |
1126 | "maximum:\n %lu inode (%u objects, %u reclaimable)\n" | |
1127 | " %llu us max scan time\n", | |
edaa53ca | 1128 | max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr, |
eb68d0e2 ZL |
1129 | div_u64(es_stats->es_stats_max_scan_time, 1000)); |
1130 | ||
1131 | return 0; | |
1132 | } | |
1133 | ||
1134 | static void ext4_es_seq_shrinker_info_stop(struct seq_file *seq, void *v) | |
1135 | { | |
1136 | } | |
1137 | ||
1138 | static const struct seq_operations ext4_es_seq_shrinker_info_ops = { | |
1139 | .start = ext4_es_seq_shrinker_info_start, | |
1140 | .next = ext4_es_seq_shrinker_info_next, | |
1141 | .stop = ext4_es_seq_shrinker_info_stop, | |
1142 | .show = ext4_es_seq_shrinker_info_show, | |
1143 | }; | |
1144 | ||
1145 | static int | |
1146 | ext4_es_seq_shrinker_info_open(struct inode *inode, struct file *file) | |
1147 | { | |
1148 | int ret; | |
1149 | ||
1150 | ret = seq_open(file, &ext4_es_seq_shrinker_info_ops); | |
1151 | if (!ret) { | |
1152 | struct seq_file *m = file->private_data; | |
1153 | m->private = PDE_DATA(inode); | |
1154 | } | |
1155 | ||
1156 | return ret; | |
1157 | } | |
1158 | ||
1159 | static int | |
1160 | ext4_es_seq_shrinker_info_release(struct inode *inode, struct file *file) | |
1161 | { | |
1162 | return seq_release(inode, file); | |
1163 | } | |
1164 | ||
1165 | static const struct file_operations ext4_es_seq_shrinker_info_fops = { | |
1166 | .owner = THIS_MODULE, | |
1167 | .open = ext4_es_seq_shrinker_info_open, | |
1168 | .read = seq_read, | |
1169 | .llseek = seq_lseek, | |
1170 | .release = ext4_es_seq_shrinker_info_release, | |
1171 | }; | |
1172 | ||
1173 | int ext4_es_register_shrinker(struct ext4_sb_info *sbi) | |
74cd15cd | 1174 | { |
eb68d0e2 ZL |
1175 | int err; |
1176 | ||
edaa53ca ZL |
1177 | INIT_LIST_HEAD(&sbi->s_es_list); |
1178 | sbi->s_es_nr_inode = 0; | |
1179 | spin_lock_init(&sbi->s_es_lock); | |
eb68d0e2 ZL |
1180 | sbi->s_es_stats.es_stats_shrunk = 0; |
1181 | sbi->s_es_stats.es_stats_cache_hits = 0; | |
1182 | sbi->s_es_stats.es_stats_cache_misses = 0; | |
1183 | sbi->s_es_stats.es_stats_scan_time = 0; | |
1184 | sbi->s_es_stats.es_stats_max_scan_time = 0; | |
c2661b80 | 1185 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL); |
eb68d0e2 ZL |
1186 | if (err) |
1187 | return err; | |
edaa53ca | 1188 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL); |
eb68d0e2 ZL |
1189 | if (err) |
1190 | goto err1; | |
1191 | ||
1ab6c499 DC |
1192 | sbi->s_es_shrinker.scan_objects = ext4_es_scan; |
1193 | sbi->s_es_shrinker.count_objects = ext4_es_count; | |
74cd15cd | 1194 | sbi->s_es_shrinker.seeks = DEFAULT_SEEKS; |
eb68d0e2 ZL |
1195 | err = register_shrinker(&sbi->s_es_shrinker); |
1196 | if (err) | |
1197 | goto err2; | |
1198 | ||
1199 | if (sbi->s_proc) | |
1200 | proc_create_data("es_shrinker_info", S_IRUGO, sbi->s_proc, | |
1201 | &ext4_es_seq_shrinker_info_fops, sbi); | |
1202 | ||
1203 | return 0; | |
1204 | ||
1205 | err2: | |
edaa53ca | 1206 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
eb68d0e2 ZL |
1207 | err1: |
1208 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); | |
1209 | return err; | |
74cd15cd ZL |
1210 | } |
1211 | ||
d3922a77 | 1212 | void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi) |
74cd15cd | 1213 | { |
eb68d0e2 ZL |
1214 | if (sbi->s_proc) |
1215 | remove_proc_entry("es_shrinker_info", sbi->s_proc); | |
1216 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); | |
edaa53ca | 1217 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
d3922a77 | 1218 | unregister_shrinker(&sbi->s_es_shrinker); |
74cd15cd ZL |
1219 | } |
1220 | ||
dd475925 JK |
1221 | /* |
1222 | * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at | |
1223 | * most *nr_to_scan extents, update *nr_to_scan accordingly. | |
1224 | * | |
1225 | * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan. | |
1226 | * Increment *nr_shrunk by the number of reclaimed extents. Also update | |
1227 | * ei->i_es_shrink_lblk to where we should continue scanning. | |
1228 | */ | |
1229 | static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end, | |
1230 | int *nr_to_scan, int *nr_shrunk) | |
74cd15cd ZL |
1231 | { |
1232 | struct inode *inode = &ei->vfs_inode; | |
1233 | struct ext4_es_tree *tree = &ei->i_es_tree; | |
74cd15cd | 1234 | struct extent_status *es; |
dd475925 | 1235 | struct rb_node *node; |
74cd15cd | 1236 | |
dd475925 JK |
1237 | es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk); |
1238 | if (!es) | |
1239 | goto out_wrap; | |
1240 | node = &es->rb_node; | |
1241 | while (*nr_to_scan > 0) { | |
1242 | if (es->es_lblk > end) { | |
1243 | ei->i_es_shrink_lblk = end + 1; | |
1244 | return 0; | |
1245 | } | |
7869a4a6 | 1246 | |
dd475925 | 1247 | (*nr_to_scan)--; |
74cd15cd ZL |
1248 | node = rb_next(&es->rb_node); |
1249 | /* | |
1250 | * We can't reclaim delayed extent from status tree because | |
1251 | * fiemap, bigallic, and seek_data/hole need to use it. | |
1252 | */ | |
1253 | if (!ext4_es_is_delayed(es)) { | |
1254 | rb_erase(&es->rb_node, &tree->root); | |
1255 | ext4_es_free_extent(inode, es); | |
dd475925 | 1256 | (*nr_shrunk)++; |
74cd15cd | 1257 | } |
dd475925 JK |
1258 | if (!node) |
1259 | goto out_wrap; | |
1260 | es = rb_entry(node, struct extent_status, rb_node); | |
74cd15cd | 1261 | } |
dd475925 JK |
1262 | ei->i_es_shrink_lblk = es->es_lblk; |
1263 | return 1; | |
1264 | out_wrap: | |
1265 | ei->i_es_shrink_lblk = 0; | |
1266 | return 0; | |
1267 | } | |
1268 | ||
1269 | static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan) | |
1270 | { | |
1271 | struct inode *inode = &ei->vfs_inode; | |
1272 | int nr_shrunk = 0; | |
1273 | ext4_lblk_t start = ei->i_es_shrink_lblk; | |
1274 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, | |
1275 | DEFAULT_RATELIMIT_BURST); | |
1276 | ||
1277 | if (ei->i_es_shk_nr == 0) | |
1278 | return 0; | |
1279 | ||
1280 | if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) && | |
1281 | __ratelimit(&_rs)) | |
1282 | ext4_warning(inode->i_sb, "forced shrink of precached extents"); | |
1283 | ||
1284 | if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) && | |
1285 | start != 0) | |
1286 | es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk); | |
1287 | ||
1288 | ei->i_es_tree.cache_es = NULL; | |
74cd15cd ZL |
1289 | return nr_shrunk; |
1290 | } |