]>
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> |
654598be ZL |
14 | #include "ext4.h" |
15 | #include "extents_status.h" | |
16 | #include "ext4_extents.h" | |
17 | ||
992e9fdd ZL |
18 | #include <trace/events/ext4.h> |
19 | ||
654598be ZL |
20 | /* |
21 | * According to previous discussion in Ext4 Developer Workshop, we | |
22 | * will introduce a new structure called io tree to track all extent | |
23 | * status in order to solve some problems that we have met | |
24 | * (e.g. Reservation space warning), and provide extent-level locking. | |
25 | * Delay extent tree is the first step to achieve this goal. It is | |
26 | * original built by Yongqiang Yang. At that time it is called delay | |
06b0c886 | 27 | * extent tree, whose goal is only track delayed extents in memory to |
654598be ZL |
28 | * simplify the implementation of fiemap and bigalloc, and introduce |
29 | * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called | |
06b0c886 ZL |
30 | * delay extent tree at the first commit. But for better understand |
31 | * what it does, it has been rename to extent status tree. | |
654598be | 32 | * |
06b0c886 ZL |
33 | * Step1: |
34 | * Currently the first step has been done. All delayed extents are | |
35 | * tracked in the tree. It maintains the delayed extent when a delayed | |
36 | * allocation is issued, and the delayed extent is written out or | |
654598be ZL |
37 | * invalidated. Therefore the implementation of fiemap and bigalloc |
38 | * are simplified, and SEEK_DATA/SEEK_HOLE are introduced. | |
39 | * | |
40 | * The following comment describes the implemenmtation of extent | |
41 | * status tree and future works. | |
06b0c886 ZL |
42 | * |
43 | * Step2: | |
44 | * In this step all extent status are tracked by extent status tree. | |
45 | * Thus, we can first try to lookup a block mapping in this tree before | |
46 | * finding it in extent tree. Hence, single extent cache can be removed | |
47 | * because extent status tree can do a better job. Extents in status | |
48 | * tree are loaded on-demand. Therefore, the extent status tree may not | |
49 | * contain all of the extents in a file. Meanwhile we define a shrinker | |
50 | * to reclaim memory from extent status tree because fragmented extent | |
51 | * tree will make status tree cost too much memory. written/unwritten/- | |
52 | * hole extents in the tree will be reclaimed by this shrinker when we | |
53 | * are under high memory pressure. Delayed extents will not be | |
54 | * reclimed because fiemap, bigalloc, and seek_data/hole need it. | |
654598be ZL |
55 | */ |
56 | ||
57 | /* | |
06b0c886 | 58 | * Extent status tree implementation for ext4. |
654598be ZL |
59 | * |
60 | * | |
61 | * ========================================================================== | |
06b0c886 | 62 | * Extent status tree tracks all extent status. |
654598be | 63 | * |
06b0c886 | 64 | * 1. Why we need to implement extent status tree? |
654598be | 65 | * |
06b0c886 | 66 | * Without extent status tree, ext4 identifies a delayed extent by looking |
654598be ZL |
67 | * up page cache, this has several deficiencies - complicated, buggy, |
68 | * and inefficient code. | |
69 | * | |
06b0c886 ZL |
70 | * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a |
71 | * block or a range of blocks are belonged to a delayed extent. | |
654598be | 72 | * |
06b0c886 | 73 | * Let us have a look at how they do without extent status tree. |
654598be ZL |
74 | * -- FIEMAP |
75 | * FIEMAP looks up page cache to identify delayed allocations from holes. | |
76 | * | |
77 | * -- SEEK_HOLE/DATA | |
78 | * SEEK_HOLE/DATA has the same problem as FIEMAP. | |
79 | * | |
80 | * -- bigalloc | |
81 | * bigalloc looks up page cache to figure out if a block is | |
82 | * already under delayed allocation or not to determine whether | |
83 | * quota reserving is needed for the cluster. | |
84 | * | |
654598be ZL |
85 | * -- writeout |
86 | * Writeout looks up whole page cache to see if a buffer is | |
87 | * mapped, If there are not very many delayed buffers, then it is | |
88 | * time comsuming. | |
89 | * | |
06b0c886 | 90 | * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA, |
654598be ZL |
91 | * bigalloc and writeout can figure out if a block or a range of |
92 | * blocks is under delayed allocation(belonged to a delayed extent) or | |
06b0c886 | 93 | * not by searching the extent tree. |
654598be ZL |
94 | * |
95 | * | |
96 | * ========================================================================== | |
06b0c886 ZL |
97 | * 2. Ext4 extent status tree impelmentation |
98 | * | |
99 | * -- extent | |
100 | * A extent is a range of blocks which are contiguous logically and | |
101 | * physically. Unlike extent in extent tree, this extent in ext4 is | |
102 | * a in-memory struct, there is no corresponding on-disk data. There | |
103 | * is no limit on length of extent, so an extent can contain as many | |
104 | * blocks as they are contiguous logically and physically. | |
654598be | 105 | * |
06b0c886 ZL |
106 | * -- extent status tree |
107 | * Every inode has an extent status tree and all allocation blocks | |
108 | * are added to the tree with different status. The extent in the | |
109 | * tree are ordered by logical block no. | |
654598be | 110 | * |
06b0c886 ZL |
111 | * -- operations on a extent status tree |
112 | * There are three important operations on a delayed extent tree: find | |
113 | * next extent, adding a extent(a range of blocks) and removing a extent. | |
654598be | 114 | * |
06b0c886 ZL |
115 | * -- race on a extent status tree |
116 | * Extent status tree is protected by inode->i_es_lock. | |
654598be | 117 | * |
06b0c886 ZL |
118 | * -- memory consumption |
119 | * Fragmented extent tree will make extent status tree cost too much | |
120 | * memory. Hence, we will reclaim written/unwritten/hole extents from | |
121 | * the tree under a heavy memory pressure. | |
654598be ZL |
122 | * |
123 | * | |
124 | * ========================================================================== | |
06b0c886 ZL |
125 | * 3. Performance analysis |
126 | * | |
654598be ZL |
127 | * -- overhead |
128 | * 1. There is a cache extent for write access, so if writes are | |
129 | * not very random, adding space operaions are in O(1) time. | |
130 | * | |
131 | * -- gain | |
132 | * 2. Code is much simpler, more readable, more maintainable and | |
133 | * more efficient. | |
134 | * | |
135 | * | |
136 | * ========================================================================== | |
137 | * 4. TODO list | |
654598be | 138 | * |
06b0c886 | 139 | * -- Refactor delayed space reservation |
654598be ZL |
140 | * |
141 | * -- Extent-level locking | |
142 | */ | |
143 | ||
144 | static struct kmem_cache *ext4_es_cachep; | |
145 | ||
bdedbb7b ZL |
146 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes); |
147 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, | |
06b0c886 | 148 | ext4_lblk_t end); |
74cd15cd ZL |
149 | static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei, |
150 | int nr_to_scan); | |
e15f742c TT |
151 | static int __ext4_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); | |
fdc0212e ZL |
188 | printk(KERN_DEBUG " [%u/%u) %llu %llx", |
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 | ||
300 | static struct extent_status * | |
bdedbb7b ZL |
301 | ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len, |
302 | ext4_fsblk_t pblk) | |
654598be ZL |
303 | { |
304 | struct extent_status *es; | |
305 | es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC); | |
306 | if (es == NULL) | |
307 | return NULL; | |
06b0c886 ZL |
308 | es->es_lblk = lblk; |
309 | es->es_len = len; | |
fdc0212e | 310 | es->es_pblk = pblk; |
74cd15cd ZL |
311 | |
312 | /* | |
313 | * We don't count delayed extent because we never try to reclaim them | |
314 | */ | |
24630774 | 315 | if (!ext4_es_is_delayed(es)) { |
74cd15cd | 316 | EXT4_I(inode)->i_es_lru_nr++; |
1ac6466f | 317 | percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt); |
24630774 | 318 | } |
74cd15cd | 319 | |
654598be ZL |
320 | return es; |
321 | } | |
322 | ||
bdedbb7b | 323 | static void ext4_es_free_extent(struct inode *inode, struct extent_status *es) |
654598be | 324 | { |
74cd15cd ZL |
325 | /* Decrease the lru counter when this es is not delayed */ |
326 | if (!ext4_es_is_delayed(es)) { | |
327 | BUG_ON(EXT4_I(inode)->i_es_lru_nr == 0); | |
328 | EXT4_I(inode)->i_es_lru_nr--; | |
1ac6466f | 329 | percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt); |
74cd15cd ZL |
330 | } |
331 | ||
654598be ZL |
332 | kmem_cache_free(ext4_es_cachep, es); |
333 | } | |
334 | ||
06b0c886 ZL |
335 | /* |
336 | * Check whether or not two extents can be merged | |
337 | * Condition: | |
338 | * - logical block number is contiguous | |
fdc0212e ZL |
339 | * - physical block number is contiguous |
340 | * - status is equal | |
06b0c886 ZL |
341 | */ |
342 | static int ext4_es_can_be_merged(struct extent_status *es1, | |
343 | struct extent_status *es2) | |
344 | { | |
bd384364 | 345 | if (ext4_es_status(es1) != ext4_es_status(es2)) |
06b0c886 ZL |
346 | return 0; |
347 | ||
bd384364 | 348 | if (((__u64) es1->es_len) + es2->es_len > 0xFFFFFFFFULL) |
fdc0212e ZL |
349 | return 0; |
350 | ||
bd384364 | 351 | if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk) |
fdc0212e ZL |
352 | return 0; |
353 | ||
bd384364 ZL |
354 | if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) && |
355 | (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2))) | |
356 | return 1; | |
357 | ||
358 | if (ext4_es_is_hole(es1)) | |
359 | return 1; | |
360 | ||
361 | /* we need to check delayed extent is without unwritten status */ | |
362 | if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1)) | |
363 | return 1; | |
364 | ||
365 | return 0; | |
06b0c886 ZL |
366 | } |
367 | ||
654598be | 368 | static struct extent_status * |
bdedbb7b | 369 | ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es) |
654598be | 370 | { |
bdedbb7b | 371 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
372 | struct extent_status *es1; |
373 | struct rb_node *node; | |
374 | ||
375 | node = rb_prev(&es->rb_node); | |
376 | if (!node) | |
377 | return es; | |
378 | ||
379 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
380 | if (ext4_es_can_be_merged(es1, es)) { |
381 | es1->es_len += es->es_len; | |
654598be | 382 | rb_erase(&es->rb_node, &tree->root); |
bdedbb7b | 383 | ext4_es_free_extent(inode, es); |
654598be ZL |
384 | es = es1; |
385 | } | |
386 | ||
387 | return es; | |
388 | } | |
389 | ||
390 | static struct extent_status * | |
bdedbb7b | 391 | ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es) |
654598be | 392 | { |
bdedbb7b | 393 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
394 | struct extent_status *es1; |
395 | struct rb_node *node; | |
396 | ||
397 | node = rb_next(&es->rb_node); | |
398 | if (!node) | |
399 | return es; | |
400 | ||
401 | es1 = rb_entry(node, struct extent_status, rb_node); | |
06b0c886 ZL |
402 | if (ext4_es_can_be_merged(es, es1)) { |
403 | es->es_len += es1->es_len; | |
654598be | 404 | rb_erase(node, &tree->root); |
bdedbb7b | 405 | ext4_es_free_extent(inode, es1); |
654598be ZL |
406 | } |
407 | ||
408 | return es; | |
409 | } | |
410 | ||
921f266b DM |
411 | #ifdef ES_AGGRESSIVE_TEST |
412 | static void ext4_es_insert_extent_ext_check(struct inode *inode, | |
413 | struct extent_status *es) | |
414 | { | |
415 | struct ext4_ext_path *path = NULL; | |
416 | struct ext4_extent *ex; | |
417 | ext4_lblk_t ee_block; | |
418 | ext4_fsblk_t ee_start; | |
419 | unsigned short ee_len; | |
420 | int depth, ee_status, es_status; | |
421 | ||
107a7bd3 | 422 | path = ext4_ext_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE); |
921f266b DM |
423 | if (IS_ERR(path)) |
424 | return; | |
425 | ||
426 | depth = ext_depth(inode); | |
427 | ex = path[depth].p_ext; | |
428 | ||
429 | if (ex) { | |
430 | ||
431 | ee_block = le32_to_cpu(ex->ee_block); | |
432 | ee_start = ext4_ext_pblock(ex); | |
433 | ee_len = ext4_ext_get_actual_len(ex); | |
434 | ||
435 | ee_status = ext4_ext_is_uninitialized(ex) ? 1 : 0; | |
436 | es_status = ext4_es_is_unwritten(es) ? 1 : 0; | |
437 | ||
438 | /* | |
439 | * Make sure ex and es are not overlap when we try to insert | |
440 | * a delayed/hole extent. | |
441 | */ | |
442 | if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) { | |
443 | if (in_range(es->es_lblk, ee_block, ee_len)) { | |
bdafe42a | 444 | pr_warn("ES insert assertion failed for " |
921f266b DM |
445 | "inode: %lu we can find an extent " |
446 | "at block [%d/%d/%llu/%c], but we " | |
447 | "want to add an delayed/hole extent " | |
448 | "[%d/%d/%llu/%llx]\n", | |
449 | inode->i_ino, ee_block, ee_len, | |
450 | ee_start, ee_status ? 'u' : 'w', | |
451 | es->es_lblk, es->es_len, | |
452 | ext4_es_pblock(es), ext4_es_status(es)); | |
453 | } | |
454 | goto out; | |
455 | } | |
456 | ||
457 | /* | |
458 | * We don't check ee_block == es->es_lblk, etc. because es | |
459 | * might be a part of whole extent, vice versa. | |
460 | */ | |
461 | if (es->es_lblk < ee_block || | |
462 | ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) { | |
bdafe42a | 463 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
464 | "ex_status [%d/%d/%llu/%c] != " |
465 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, | |
466 | ee_block, ee_len, ee_start, | |
467 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, | |
468 | ext4_es_pblock(es), es_status ? 'u' : 'w'); | |
469 | goto out; | |
470 | } | |
471 | ||
472 | if (ee_status ^ es_status) { | |
bdafe42a | 473 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
474 | "ex_status [%d/%d/%llu/%c] != " |
475 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, | |
476 | ee_block, ee_len, ee_start, | |
477 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, | |
478 | ext4_es_pblock(es), es_status ? 'u' : 'w'); | |
479 | } | |
480 | } else { | |
481 | /* | |
482 | * We can't find an extent on disk. So we need to make sure | |
483 | * that we don't want to add an written/unwritten extent. | |
484 | */ | |
485 | if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) { | |
bdafe42a | 486 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
487 | "can't find an extent at block %d but we want " |
488 | "to add an written/unwritten extent " | |
489 | "[%d/%d/%llu/%llx]\n", inode->i_ino, | |
490 | es->es_lblk, es->es_lblk, es->es_len, | |
491 | ext4_es_pblock(es), ext4_es_status(es)); | |
492 | } | |
493 | } | |
494 | out: | |
495 | if (path) { | |
496 | ext4_ext_drop_refs(path); | |
497 | kfree(path); | |
498 | } | |
499 | } | |
500 | ||
501 | static void ext4_es_insert_extent_ind_check(struct inode *inode, | |
502 | struct extent_status *es) | |
503 | { | |
504 | struct ext4_map_blocks map; | |
505 | int retval; | |
506 | ||
507 | /* | |
508 | * Here we call ext4_ind_map_blocks to lookup a block mapping because | |
509 | * 'Indirect' structure is defined in indirect.c. So we couldn't | |
510 | * access direct/indirect tree from outside. It is too dirty to define | |
511 | * this function in indirect.c file. | |
512 | */ | |
513 | ||
514 | map.m_lblk = es->es_lblk; | |
515 | map.m_len = es->es_len; | |
516 | ||
517 | retval = ext4_ind_map_blocks(NULL, inode, &map, 0); | |
518 | if (retval > 0) { | |
519 | if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) { | |
520 | /* | |
521 | * We want to add a delayed/hole extent but this | |
522 | * block has been allocated. | |
523 | */ | |
bdafe42a | 524 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
525 | "We can find blocks but we want to add a " |
526 | "delayed/hole extent [%d/%d/%llu/%llx]\n", | |
527 | inode->i_ino, es->es_lblk, es->es_len, | |
528 | ext4_es_pblock(es), ext4_es_status(es)); | |
529 | return; | |
530 | } else if (ext4_es_is_written(es)) { | |
531 | if (retval != es->es_len) { | |
bdafe42a | 532 | pr_warn("ES insert assertion failed for " |
921f266b DM |
533 | "inode: %lu retval %d != es_len %d\n", |
534 | inode->i_ino, retval, es->es_len); | |
535 | return; | |
536 | } | |
537 | if (map.m_pblk != ext4_es_pblock(es)) { | |
bdafe42a | 538 | pr_warn("ES insert assertion failed for " |
921f266b DM |
539 | "inode: %lu m_pblk %llu != " |
540 | "es_pblk %llu\n", | |
541 | inode->i_ino, map.m_pblk, | |
542 | ext4_es_pblock(es)); | |
543 | return; | |
544 | } | |
545 | } else { | |
546 | /* | |
547 | * We don't need to check unwritten extent because | |
548 | * indirect-based file doesn't have it. | |
549 | */ | |
550 | BUG_ON(1); | |
551 | } | |
552 | } else if (retval == 0) { | |
553 | if (ext4_es_is_written(es)) { | |
bdafe42a | 554 | pr_warn("ES insert assertion failed for inode: %lu " |
921f266b DM |
555 | "We can't find the block but we want to add " |
556 | "an written extent [%d/%d/%llu/%llx]\n", | |
557 | inode->i_ino, es->es_lblk, es->es_len, | |
558 | ext4_es_pblock(es), ext4_es_status(es)); | |
559 | return; | |
560 | } | |
561 | } | |
562 | } | |
563 | ||
564 | static inline void ext4_es_insert_extent_check(struct inode *inode, | |
565 | struct extent_status *es) | |
566 | { | |
567 | /* | |
568 | * We don't need to worry about the race condition because | |
569 | * caller takes i_data_sem locking. | |
570 | */ | |
571 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); | |
572 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) | |
573 | ext4_es_insert_extent_ext_check(inode, es); | |
574 | else | |
575 | ext4_es_insert_extent_ind_check(inode, es); | |
576 | } | |
577 | #else | |
578 | static inline void ext4_es_insert_extent_check(struct inode *inode, | |
579 | struct extent_status *es) | |
580 | { | |
581 | } | |
582 | #endif | |
583 | ||
bdedbb7b | 584 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes) |
654598be | 585 | { |
bdedbb7b | 586 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be ZL |
587 | struct rb_node **p = &tree->root.rb_node; |
588 | struct rb_node *parent = NULL; | |
589 | struct extent_status *es; | |
654598be ZL |
590 | |
591 | while (*p) { | |
592 | parent = *p; | |
593 | es = rb_entry(parent, struct extent_status, rb_node); | |
594 | ||
06b0c886 ZL |
595 | if (newes->es_lblk < es->es_lblk) { |
596 | if (ext4_es_can_be_merged(newes, es)) { | |
597 | /* | |
598 | * Here we can modify es_lblk directly | |
599 | * because it isn't overlapped. | |
600 | */ | |
601 | es->es_lblk = newes->es_lblk; | |
602 | es->es_len += newes->es_len; | |
fdc0212e ZL |
603 | if (ext4_es_is_written(es) || |
604 | ext4_es_is_unwritten(es)) | |
605 | ext4_es_store_pblock(es, | |
606 | newes->es_pblk); | |
bdedbb7b | 607 | es = ext4_es_try_to_merge_left(inode, es); |
654598be ZL |
608 | goto out; |
609 | } | |
610 | p = &(*p)->rb_left; | |
06b0c886 ZL |
611 | } else if (newes->es_lblk > ext4_es_end(es)) { |
612 | if (ext4_es_can_be_merged(es, newes)) { | |
613 | es->es_len += newes->es_len; | |
bdedbb7b | 614 | es = ext4_es_try_to_merge_right(inode, es); |
654598be ZL |
615 | goto out; |
616 | } | |
617 | p = &(*p)->rb_right; | |
618 | } else { | |
06b0c886 ZL |
619 | BUG_ON(1); |
620 | return -EINVAL; | |
654598be ZL |
621 | } |
622 | } | |
623 | ||
bdedbb7b | 624 | es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len, |
fdc0212e | 625 | newes->es_pblk); |
654598be ZL |
626 | if (!es) |
627 | return -ENOMEM; | |
628 | rb_link_node(&es->rb_node, parent, p); | |
629 | rb_insert_color(&es->rb_node, &tree->root); | |
630 | ||
631 | out: | |
632 | tree->cache_es = es; | |
633 | return 0; | |
634 | } | |
635 | ||
636 | /* | |
bdafe42a TT |
637 | * ext4_es_insert_extent() adds information to an inode's extent |
638 | * status tree. | |
654598be ZL |
639 | * |
640 | * Return 0 on success, error code on failure. | |
641 | */ | |
06b0c886 | 642 | int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk, |
fdc0212e | 643 | ext4_lblk_t len, ext4_fsblk_t pblk, |
3be78c73 | 644 | unsigned int status) |
654598be | 645 | { |
06b0c886 ZL |
646 | struct extent_status newes; |
647 | ext4_lblk_t end = lblk + len - 1; | |
654598be ZL |
648 | int err = 0; |
649 | ||
3be78c73 | 650 | es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n", |
fdc0212e | 651 | lblk, len, pblk, status, inode->i_ino); |
06b0c886 | 652 | |
d4381472 EG |
653 | if (!len) |
654 | return 0; | |
655 | ||
06b0c886 ZL |
656 | BUG_ON(end < lblk); |
657 | ||
658 | newes.es_lblk = lblk; | |
659 | newes.es_len = len; | |
fdc0212e ZL |
660 | ext4_es_store_pblock(&newes, pblk); |
661 | ext4_es_store_status(&newes, status); | |
662 | trace_ext4_es_insert_extent(inode, &newes); | |
654598be | 663 | |
921f266b DM |
664 | ext4_es_insert_extent_check(inode, &newes); |
665 | ||
654598be | 666 | write_lock(&EXT4_I(inode)->i_es_lock); |
bdedbb7b | 667 | err = __es_remove_extent(inode, lblk, end); |
06b0c886 ZL |
668 | if (err != 0) |
669 | goto error; | |
e15f742c | 670 | retry: |
bdedbb7b | 671 | err = __es_insert_extent(inode, &newes); |
e15f742c TT |
672 | if (err == -ENOMEM && __ext4_es_shrink(EXT4_SB(inode->i_sb), 1, |
673 | EXT4_I(inode))) | |
674 | goto retry; | |
675 | if (err == -ENOMEM && !ext4_es_is_delayed(&newes)) | |
676 | err = 0; | |
06b0c886 ZL |
677 | |
678 | error: | |
654598be ZL |
679 | write_unlock(&EXT4_I(inode)->i_es_lock); |
680 | ||
681 | ext4_es_print_tree(inode); | |
682 | ||
683 | return err; | |
684 | } | |
685 | ||
107a7bd3 TT |
686 | /* |
687 | * ext4_es_cache_extent() inserts information into the extent status | |
688 | * tree if and only if there isn't information about the range in | |
689 | * question already. | |
690 | */ | |
691 | void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk, | |
692 | ext4_lblk_t len, ext4_fsblk_t pblk, | |
693 | unsigned int status) | |
694 | { | |
695 | struct extent_status *es; | |
696 | struct extent_status newes; | |
697 | ext4_lblk_t end = lblk + len - 1; | |
698 | ||
699 | newes.es_lblk = lblk; | |
700 | newes.es_len = len; | |
701 | ext4_es_store_pblock(&newes, pblk); | |
702 | ext4_es_store_status(&newes, status); | |
703 | trace_ext4_es_cache_extent(inode, &newes); | |
704 | ||
705 | if (!len) | |
706 | return; | |
707 | ||
708 | BUG_ON(end < lblk); | |
709 | ||
710 | write_lock(&EXT4_I(inode)->i_es_lock); | |
711 | ||
712 | es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk); | |
713 | if (es && ((es->es_lblk <= lblk) || (es->es_lblk <= end))) | |
714 | goto out; | |
715 | ||
716 | __es_insert_extent(inode, &newes); | |
717 | out: | |
718 | write_unlock(&EXT4_I(inode)->i_es_lock); | |
719 | } | |
720 | ||
d100eef2 ZL |
721 | /* |
722 | * ext4_es_lookup_extent() looks up an extent in extent status tree. | |
723 | * | |
724 | * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks. | |
725 | * | |
726 | * Return: 1 on found, 0 on not | |
727 | */ | |
728 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, | |
729 | struct extent_status *es) | |
730 | { | |
731 | struct ext4_es_tree *tree; | |
732 | struct extent_status *es1 = NULL; | |
733 | struct rb_node *node; | |
734 | int found = 0; | |
735 | ||
736 | trace_ext4_es_lookup_extent_enter(inode, lblk); | |
737 | es_debug("lookup extent in block %u\n", lblk); | |
738 | ||
739 | tree = &EXT4_I(inode)->i_es_tree; | |
740 | read_lock(&EXT4_I(inode)->i_es_lock); | |
741 | ||
742 | /* find extent in cache firstly */ | |
743 | es->es_lblk = es->es_len = es->es_pblk = 0; | |
744 | if (tree->cache_es) { | |
745 | es1 = tree->cache_es; | |
746 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { | |
747 | es_debug("%u cached by [%u/%u)\n", | |
748 | lblk, es1->es_lblk, es1->es_len); | |
749 | found = 1; | |
750 | goto out; | |
751 | } | |
752 | } | |
753 | ||
754 | node = tree->root.rb_node; | |
755 | while (node) { | |
756 | es1 = rb_entry(node, struct extent_status, rb_node); | |
757 | if (lblk < es1->es_lblk) | |
758 | node = node->rb_left; | |
759 | else if (lblk > ext4_es_end(es1)) | |
760 | node = node->rb_right; | |
761 | else { | |
762 | found = 1; | |
763 | break; | |
764 | } | |
765 | } | |
766 | ||
767 | out: | |
768 | if (found) { | |
769 | BUG_ON(!es1); | |
770 | es->es_lblk = es1->es_lblk; | |
771 | es->es_len = es1->es_len; | |
772 | es->es_pblk = es1->es_pblk; | |
773 | } | |
774 | ||
775 | read_unlock(&EXT4_I(inode)->i_es_lock); | |
776 | ||
777 | trace_ext4_es_lookup_extent_exit(inode, es, found); | |
778 | return found; | |
779 | } | |
780 | ||
bdedbb7b ZL |
781 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
782 | ext4_lblk_t end) | |
654598be | 783 | { |
bdedbb7b | 784 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
654598be | 785 | struct rb_node *node; |
654598be ZL |
786 | struct extent_status *es; |
787 | struct extent_status orig_es; | |
06b0c886 | 788 | ext4_lblk_t len1, len2; |
fdc0212e | 789 | ext4_fsblk_t block; |
e15f742c | 790 | int err; |
654598be | 791 | |
e15f742c TT |
792 | retry: |
793 | err = 0; | |
06b0c886 | 794 | es = __es_tree_search(&tree->root, lblk); |
654598be ZL |
795 | if (!es) |
796 | goto out; | |
06b0c886 | 797 | if (es->es_lblk > end) |
654598be ZL |
798 | goto out; |
799 | ||
800 | /* Simply invalidate cache_es. */ | |
801 | tree->cache_es = NULL; | |
802 | ||
06b0c886 ZL |
803 | orig_es.es_lblk = es->es_lblk; |
804 | orig_es.es_len = es->es_len; | |
fdc0212e ZL |
805 | orig_es.es_pblk = es->es_pblk; |
806 | ||
06b0c886 ZL |
807 | len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0; |
808 | len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0; | |
654598be | 809 | if (len1 > 0) |
06b0c886 | 810 | es->es_len = len1; |
654598be ZL |
811 | if (len2 > 0) { |
812 | if (len1 > 0) { | |
06b0c886 ZL |
813 | struct extent_status newes; |
814 | ||
815 | newes.es_lblk = end + 1; | |
816 | newes.es_len = len2; | |
fdc0212e ZL |
817 | if (ext4_es_is_written(&orig_es) || |
818 | ext4_es_is_unwritten(&orig_es)) { | |
819 | block = ext4_es_pblock(&orig_es) + | |
820 | orig_es.es_len - len2; | |
821 | ext4_es_store_pblock(&newes, block); | |
822 | } | |
823 | ext4_es_store_status(&newes, ext4_es_status(&orig_es)); | |
bdedbb7b | 824 | err = __es_insert_extent(inode, &newes); |
654598be | 825 | if (err) { |
06b0c886 ZL |
826 | es->es_lblk = orig_es.es_lblk; |
827 | es->es_len = orig_es.es_len; | |
e15f742c TT |
828 | if ((err == -ENOMEM) && |
829 | __ext4_es_shrink(EXT4_SB(inode->i_sb), 1, | |
830 | EXT4_I(inode))) | |
831 | goto retry; | |
654598be ZL |
832 | goto out; |
833 | } | |
834 | } else { | |
06b0c886 ZL |
835 | es->es_lblk = end + 1; |
836 | es->es_len = len2; | |
fdc0212e ZL |
837 | if (ext4_es_is_written(es) || |
838 | ext4_es_is_unwritten(es)) { | |
839 | block = orig_es.es_pblk + orig_es.es_len - len2; | |
840 | ext4_es_store_pblock(es, block); | |
841 | } | |
654598be ZL |
842 | } |
843 | goto out; | |
844 | } | |
845 | ||
846 | if (len1 > 0) { | |
847 | node = rb_next(&es->rb_node); | |
848 | if (node) | |
849 | es = rb_entry(node, struct extent_status, rb_node); | |
850 | else | |
851 | es = NULL; | |
852 | } | |
853 | ||
06b0c886 | 854 | while (es && ext4_es_end(es) <= end) { |
654598be ZL |
855 | node = rb_next(&es->rb_node); |
856 | rb_erase(&es->rb_node, &tree->root); | |
bdedbb7b | 857 | ext4_es_free_extent(inode, es); |
654598be ZL |
858 | if (!node) { |
859 | es = NULL; | |
860 | break; | |
861 | } | |
862 | es = rb_entry(node, struct extent_status, rb_node); | |
863 | } | |
864 | ||
06b0c886 | 865 | if (es && es->es_lblk < end + 1) { |
fdc0212e ZL |
866 | ext4_lblk_t orig_len = es->es_len; |
867 | ||
06b0c886 ZL |
868 | len1 = ext4_es_end(es) - end; |
869 | es->es_lblk = end + 1; | |
870 | es->es_len = len1; | |
fdc0212e ZL |
871 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
872 | block = es->es_pblk + orig_len - len1; | |
873 | ext4_es_store_pblock(es, block); | |
874 | } | |
654598be ZL |
875 | } |
876 | ||
877 | out: | |
06b0c886 ZL |
878 | return err; |
879 | } | |
880 | ||
881 | /* | |
882 | * ext4_es_remove_extent() removes a space from a extent status tree. | |
883 | * | |
884 | * Return 0 on success, error code on failure. | |
885 | */ | |
886 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, | |
887 | ext4_lblk_t len) | |
888 | { | |
06b0c886 ZL |
889 | ext4_lblk_t end; |
890 | int err = 0; | |
891 | ||
892 | trace_ext4_es_remove_extent(inode, lblk, len); | |
893 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", | |
894 | lblk, len, inode->i_ino); | |
895 | ||
d4381472 EG |
896 | if (!len) |
897 | return err; | |
898 | ||
06b0c886 ZL |
899 | end = lblk + len - 1; |
900 | BUG_ON(end < lblk); | |
901 | ||
06b0c886 | 902 | write_lock(&EXT4_I(inode)->i_es_lock); |
bdedbb7b | 903 | err = __es_remove_extent(inode, lblk, end); |
654598be ZL |
904 | write_unlock(&EXT4_I(inode)->i_es_lock); |
905 | ext4_es_print_tree(inode); | |
906 | return err; | |
907 | } | |
74cd15cd | 908 | |
adb23551 ZL |
909 | int ext4_es_zeroout(struct inode *inode, struct ext4_extent *ex) |
910 | { | |
911 | ext4_lblk_t ee_block; | |
912 | ext4_fsblk_t ee_pblock; | |
913 | unsigned int ee_len; | |
914 | ||
915 | ee_block = le32_to_cpu(ex->ee_block); | |
916 | ee_len = ext4_ext_get_actual_len(ex); | |
917 | ee_pblock = ext4_ext_pblock(ex); | |
918 | ||
919 | if (ee_len == 0) | |
920 | return 0; | |
921 | ||
922 | return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, | |
923 | EXTENT_STATUS_WRITTEN); | |
924 | } | |
925 | ||
d3922a77 ZL |
926 | static int ext4_inode_touch_time_cmp(void *priv, struct list_head *a, |
927 | struct list_head *b) | |
928 | { | |
929 | struct ext4_inode_info *eia, *eib; | |
930 | eia = list_entry(a, struct ext4_inode_info, i_es_lru); | |
931 | eib = list_entry(b, struct ext4_inode_info, i_es_lru); | |
932 | ||
933 | if (eia->i_touch_when == eib->i_touch_when) | |
934 | return 0; | |
935 | if (time_after(eia->i_touch_when, eib->i_touch_when)) | |
936 | return 1; | |
937 | else | |
938 | return -1; | |
939 | } | |
940 | ||
e15f742c TT |
941 | static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
942 | struct ext4_inode_info *locked_ei) | |
74cd15cd | 943 | { |
74cd15cd | 944 | struct ext4_inode_info *ei; |
d3922a77 ZL |
945 | struct list_head *cur, *tmp; |
946 | LIST_HEAD(skiped); | |
74cd15cd ZL |
947 | int ret, nr_shrunk = 0; |
948 | ||
74cd15cd | 949 | spin_lock(&sbi->s_es_lru_lock); |
d3922a77 ZL |
950 | |
951 | /* | |
952 | * If the inode that is at the head of LRU list is newer than | |
953 | * last_sorted time, that means that we need to sort this list. | |
954 | */ | |
955 | ei = list_first_entry(&sbi->s_es_lru, struct ext4_inode_info, i_es_lru); | |
956 | if (sbi->s_es_last_sorted < ei->i_touch_when) { | |
957 | list_sort(NULL, &sbi->s_es_lru, ext4_inode_touch_time_cmp); | |
958 | sbi->s_es_last_sorted = jiffies; | |
959 | } | |
960 | ||
74cd15cd | 961 | list_for_each_safe(cur, tmp, &sbi->s_es_lru) { |
d3922a77 ZL |
962 | /* |
963 | * If we have already reclaimed all extents from extent | |
964 | * status tree, just stop the loop immediately. | |
965 | */ | |
966 | if (percpu_counter_read_positive(&sbi->s_extent_cache_cnt) == 0) | |
967 | break; | |
74cd15cd ZL |
968 | |
969 | ei = list_entry(cur, struct ext4_inode_info, i_es_lru); | |
970 | ||
d3922a77 ZL |
971 | /* Skip the inode that is newer than the last_sorted time */ |
972 | if (sbi->s_es_last_sorted < ei->i_touch_when) { | |
973 | list_move_tail(cur, &skiped); | |
74cd15cd ZL |
974 | continue; |
975 | } | |
d3922a77 | 976 | |
e15f742c | 977 | if (ei->i_es_lru_nr == 0 || ei == locked_ei) |
d3922a77 | 978 | continue; |
74cd15cd ZL |
979 | |
980 | write_lock(&ei->i_es_lock); | |
981 | ret = __es_try_to_reclaim_extents(ei, nr_to_scan); | |
d3922a77 ZL |
982 | if (ei->i_es_lru_nr == 0) |
983 | list_del_init(&ei->i_es_lru); | |
74cd15cd ZL |
984 | write_unlock(&ei->i_es_lock); |
985 | ||
986 | nr_shrunk += ret; | |
987 | nr_to_scan -= ret; | |
988 | if (nr_to_scan == 0) | |
989 | break; | |
990 | } | |
d3922a77 ZL |
991 | |
992 | /* Move the newer inodes into the tail of the LRU list. */ | |
993 | list_splice_tail(&skiped, &sbi->s_es_lru); | |
74cd15cd | 994 | spin_unlock(&sbi->s_es_lru_lock); |
74cd15cd | 995 | |
e15f742c TT |
996 | if (locked_ei && nr_shrunk == 0) |
997 | nr_shrunk = __es_try_to_reclaim_extents(ei, nr_to_scan); | |
998 | ||
999 | return nr_shrunk; | |
1000 | } | |
1001 | ||
1002 | static int ext4_es_shrink(struct shrinker *shrink, struct shrink_control *sc) | |
1003 | { | |
1004 | struct ext4_sb_info *sbi = container_of(shrink, | |
1005 | struct ext4_sb_info, s_es_shrinker); | |
1006 | int nr_to_scan = sc->nr_to_scan; | |
1007 | int ret, nr_shrunk; | |
1008 | ||
1009 | ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt); | |
1010 | trace_ext4_es_shrink_enter(sbi->s_sb, nr_to_scan, ret); | |
1011 | ||
1012 | if (!nr_to_scan) | |
1013 | return ret; | |
1014 | ||
1015 | nr_shrunk = __ext4_es_shrink(sbi, nr_to_scan, NULL); | |
1016 | ||
1ac6466f | 1017 | ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt); |
24630774 TT |
1018 | trace_ext4_es_shrink_exit(sbi->s_sb, nr_shrunk, ret); |
1019 | return ret; | |
74cd15cd ZL |
1020 | } |
1021 | ||
d3922a77 | 1022 | void ext4_es_register_shrinker(struct ext4_sb_info *sbi) |
74cd15cd | 1023 | { |
74cd15cd ZL |
1024 | INIT_LIST_HEAD(&sbi->s_es_lru); |
1025 | spin_lock_init(&sbi->s_es_lru_lock); | |
d3922a77 | 1026 | sbi->s_es_last_sorted = 0; |
74cd15cd ZL |
1027 | sbi->s_es_shrinker.shrink = ext4_es_shrink; |
1028 | sbi->s_es_shrinker.seeks = DEFAULT_SEEKS; | |
1029 | register_shrinker(&sbi->s_es_shrinker); | |
1030 | } | |
1031 | ||
d3922a77 | 1032 | void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi) |
74cd15cd | 1033 | { |
d3922a77 | 1034 | unregister_shrinker(&sbi->s_es_shrinker); |
74cd15cd ZL |
1035 | } |
1036 | ||
1037 | void ext4_es_lru_add(struct inode *inode) | |
1038 | { | |
1039 | struct ext4_inode_info *ei = EXT4_I(inode); | |
1040 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1041 | ||
d3922a77 ZL |
1042 | ei->i_touch_when = jiffies; |
1043 | ||
1044 | if (!list_empty(&ei->i_es_lru)) | |
1045 | return; | |
1046 | ||
74cd15cd ZL |
1047 | spin_lock(&sbi->s_es_lru_lock); |
1048 | if (list_empty(&ei->i_es_lru)) | |
1049 | list_add_tail(&ei->i_es_lru, &sbi->s_es_lru); | |
74cd15cd ZL |
1050 | spin_unlock(&sbi->s_es_lru_lock); |
1051 | } | |
1052 | ||
1053 | void ext4_es_lru_del(struct inode *inode) | |
1054 | { | |
1055 | struct ext4_inode_info *ei = EXT4_I(inode); | |
1056 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1057 | ||
1058 | spin_lock(&sbi->s_es_lru_lock); | |
1059 | if (!list_empty(&ei->i_es_lru)) | |
1060 | list_del_init(&ei->i_es_lru); | |
1061 | spin_unlock(&sbi->s_es_lru_lock); | |
1062 | } | |
1063 | ||
74cd15cd ZL |
1064 | static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei, |
1065 | int nr_to_scan) | |
1066 | { | |
1067 | struct inode *inode = &ei->vfs_inode; | |
1068 | struct ext4_es_tree *tree = &ei->i_es_tree; | |
1069 | struct rb_node *node; | |
1070 | struct extent_status *es; | |
1071 | int nr_shrunk = 0; | |
1072 | ||
1073 | if (ei->i_es_lru_nr == 0) | |
1074 | return 0; | |
1075 | ||
1076 | node = rb_first(&tree->root); | |
1077 | while (node != NULL) { | |
1078 | es = rb_entry(node, struct extent_status, rb_node); | |
1079 | node = rb_next(&es->rb_node); | |
1080 | /* | |
1081 | * We can't reclaim delayed extent from status tree because | |
1082 | * fiemap, bigallic, and seek_data/hole need to use it. | |
1083 | */ | |
1084 | if (!ext4_es_is_delayed(es)) { | |
1085 | rb_erase(&es->rb_node, &tree->root); | |
1086 | ext4_es_free_extent(inode, es); | |
1087 | nr_shrunk++; | |
1088 | if (--nr_to_scan == 0) | |
1089 | break; | |
1090 | } | |
1091 | } | |
1092 | tree->cache_es = NULL; | |
1093 | return nr_shrunk; | |
1094 | } |