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