]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ext4/extents_status.c
ext4: track all extent status in extent status tree
[mirror_ubuntu-bionic-kernel.git] / fs / ext4 / extents_status.c
CommitLineData
654598be
ZL
1/*
2 * fs/ext4/extents_status.c
3 *
4 * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
5 * Modified by
6 * Allison Henderson <achender@linux.vnet.ibm.com>
7 * Hugh Dickins <hughd@google.com>
8 * Zheng Liu <wenqing.lz@taobao.com>
9 *
10 * Ext4 extents status tree core functions.
11 */
12#include <linux/rbtree.h>
13#include "ext4.h"
14#include "extents_status.h"
15#include "ext4_extents.h"
16
992e9fdd
ZL
17#include <trace/events/ext4.h>
18
654598be
ZL
19/*
20 * According to previous discussion in Ext4 Developer Workshop, we
21 * will introduce a new structure called io tree to track all extent
22 * status in order to solve some problems that we have met
23 * (e.g. Reservation space warning), and provide extent-level locking.
24 * Delay extent tree is the first step to achieve this goal. It is
25 * original built by Yongqiang Yang. At that time it is called delay
06b0c886 26 * extent tree, whose goal is only track delayed extents in memory to
654598be
ZL
27 * simplify the implementation of fiemap and bigalloc, and introduce
28 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
06b0c886
ZL
29 * delay extent tree at the first commit. But for better understand
30 * what it does, it has been rename to extent status tree.
654598be 31 *
06b0c886
ZL
32 * Step1:
33 * Currently the first step has been done. All delayed extents are
34 * tracked in the tree. It maintains the delayed extent when a delayed
35 * allocation is issued, and the delayed extent is written out or
654598be
ZL
36 * invalidated. Therefore the implementation of fiemap and bigalloc
37 * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
38 *
39 * The following comment describes the implemenmtation of extent
40 * status tree and future works.
06b0c886
ZL
41 *
42 * Step2:
43 * In this step all extent status are tracked by extent status tree.
44 * Thus, we can first try to lookup a block mapping in this tree before
45 * finding it in extent tree. Hence, single extent cache can be removed
46 * because extent status tree can do a better job. Extents in status
47 * tree are loaded on-demand. Therefore, the extent status tree may not
48 * contain all of the extents in a file. Meanwhile we define a shrinker
49 * to reclaim memory from extent status tree because fragmented extent
50 * tree will make status tree cost too much memory. written/unwritten/-
51 * hole extents in the tree will be reclaimed by this shrinker when we
52 * are under high memory pressure. Delayed extents will not be
53 * reclimed because fiemap, bigalloc, and seek_data/hole need it.
654598be
ZL
54 */
55
56/*
06b0c886 57 * Extent status tree implementation for ext4.
654598be
ZL
58 *
59 *
60 * ==========================================================================
06b0c886 61 * Extent status tree tracks all extent status.
654598be 62 *
06b0c886 63 * 1. Why we need to implement extent status tree?
654598be 64 *
06b0c886 65 * Without extent status tree, ext4 identifies a delayed extent by looking
654598be
ZL
66 * up page cache, this has several deficiencies - complicated, buggy,
67 * and inefficient code.
68 *
06b0c886
ZL
69 * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
70 * block or a range of blocks are belonged to a delayed extent.
654598be 71 *
06b0c886 72 * Let us have a look at how they do without extent status tree.
654598be
ZL
73 * -- FIEMAP
74 * FIEMAP looks up page cache to identify delayed allocations from holes.
75 *
76 * -- SEEK_HOLE/DATA
77 * SEEK_HOLE/DATA has the same problem as FIEMAP.
78 *
79 * -- bigalloc
80 * bigalloc looks up page cache to figure out if a block is
81 * already under delayed allocation or not to determine whether
82 * quota reserving is needed for the cluster.
83 *
654598be
ZL
84 * -- writeout
85 * Writeout looks up whole page cache to see if a buffer is
86 * mapped, If there are not very many delayed buffers, then it is
87 * time comsuming.
88 *
06b0c886 89 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
654598be
ZL
90 * bigalloc and writeout can figure out if a block or a range of
91 * blocks is under delayed allocation(belonged to a delayed extent) or
06b0c886 92 * not by searching the extent tree.
654598be
ZL
93 *
94 *
95 * ==========================================================================
06b0c886
ZL
96 * 2. Ext4 extent status tree impelmentation
97 *
98 * -- extent
99 * A extent is a range of blocks which are contiguous logically and
100 * physically. Unlike extent in extent tree, this extent in ext4 is
101 * a in-memory struct, there is no corresponding on-disk data. There
102 * is no limit on length of extent, so an extent can contain as many
103 * blocks as they are contiguous logically and physically.
654598be 104 *
06b0c886
ZL
105 * -- extent status tree
106 * Every inode has an extent status tree and all allocation blocks
107 * are added to the tree with different status. The extent in the
108 * tree are ordered by logical block no.
654598be 109 *
06b0c886
ZL
110 * -- operations on a extent status tree
111 * There are three important operations on a delayed extent tree: find
112 * next extent, adding a extent(a range of blocks) and removing a extent.
654598be 113 *
06b0c886
ZL
114 * -- race on a extent status tree
115 * Extent status tree is protected by inode->i_es_lock.
654598be 116 *
06b0c886
ZL
117 * -- memory consumption
118 * Fragmented extent tree will make extent status tree cost too much
119 * memory. Hence, we will reclaim written/unwritten/hole extents from
120 * the tree under a heavy memory pressure.
654598be
ZL
121 *
122 *
123 * ==========================================================================
06b0c886
ZL
124 * 3. Performance analysis
125 *
654598be
ZL
126 * -- overhead
127 * 1. There is a cache extent for write access, so if writes are
128 * not very random, adding space operaions are in O(1) time.
129 *
130 * -- gain
131 * 2. Code is much simpler, more readable, more maintainable and
132 * more efficient.
133 *
134 *
135 * ==========================================================================
136 * 4. TODO list
654598be 137 *
06b0c886 138 * -- Refactor delayed space reservation
654598be
ZL
139 *
140 * -- Extent-level locking
141 */
142
143static struct kmem_cache *ext4_es_cachep;
144
06b0c886
ZL
145static int __es_insert_extent(struct ext4_es_tree *tree,
146 struct extent_status *newes);
147static int __es_remove_extent(struct ext4_es_tree *tree, ext4_lblk_t lblk,
148 ext4_lblk_t end);
149
654598be
ZL
150int __init ext4_init_es(void)
151{
152 ext4_es_cachep = KMEM_CACHE(extent_status, SLAB_RECLAIM_ACCOUNT);
153 if (ext4_es_cachep == NULL)
154 return -ENOMEM;
155 return 0;
156}
157
158void ext4_exit_es(void)
159{
160 if (ext4_es_cachep)
161 kmem_cache_destroy(ext4_es_cachep);
162}
163
164void ext4_es_init_tree(struct ext4_es_tree *tree)
165{
166 tree->root = RB_ROOT;
167 tree->cache_es = NULL;
168}
169
170#ifdef ES_DEBUG__
171static void ext4_es_print_tree(struct inode *inode)
172{
173 struct ext4_es_tree *tree;
174 struct rb_node *node;
175
176 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
177 tree = &EXT4_I(inode)->i_es_tree;
178 node = rb_first(&tree->root);
179 while (node) {
180 struct extent_status *es;
181 es = rb_entry(node, struct extent_status, rb_node);
fdc0212e
ZL
182 printk(KERN_DEBUG " [%u/%u) %llu %llx",
183 es->es_lblk, es->es_len,
184 ext4_es_pblock(es), ext4_es_status(es));
654598be
ZL
185 node = rb_next(node);
186 }
187 printk(KERN_DEBUG "\n");
188}
189#else
190#define ext4_es_print_tree(inode)
191#endif
192
06b0c886 193static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
654598be 194{
06b0c886
ZL
195 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
196 return es->es_lblk + es->es_len - 1;
654598be
ZL
197}
198
199/*
200 * search through the tree for an delayed extent with a given offset. If
201 * it can't be found, try to find next extent.
202 */
203static struct extent_status *__es_tree_search(struct rb_root *root,
06b0c886 204 ext4_lblk_t lblk)
654598be
ZL
205{
206 struct rb_node *node = root->rb_node;
207 struct extent_status *es = NULL;
208
209 while (node) {
210 es = rb_entry(node, struct extent_status, rb_node);
06b0c886 211 if (lblk < es->es_lblk)
654598be 212 node = node->rb_left;
06b0c886 213 else if (lblk > ext4_es_end(es))
654598be
ZL
214 node = node->rb_right;
215 else
216 return es;
217 }
218
06b0c886 219 if (es && lblk < es->es_lblk)
654598be
ZL
220 return es;
221
06b0c886 222 if (es && lblk > ext4_es_end(es)) {
654598be
ZL
223 node = rb_next(&es->rb_node);
224 return node ? rb_entry(node, struct extent_status, rb_node) :
225 NULL;
226 }
227
228 return NULL;
229}
230
231/*
be401363 232 * ext4_es_find_delayed_extent: find the 1st delayed extent covering @es->lblk
06b0c886 233 * if it exists, otherwise, the next extent after @es->lblk.
654598be
ZL
234 *
235 * @inode: the inode which owns delayed extents
be401363 236 * @lblk: the offset where we start to search
654598be 237 * @es: delayed extent that we found
654598be 238 */
be401363
ZL
239void ext4_es_find_delayed_extent(struct inode *inode, ext4_lblk_t lblk,
240 struct extent_status *es)
654598be
ZL
241{
242 struct ext4_es_tree *tree = NULL;
243 struct extent_status *es1 = NULL;
244 struct rb_node *node;
654598be 245
be401363
ZL
246 BUG_ON(es == NULL);
247 trace_ext4_es_find_delayed_extent_enter(inode, lblk);
992e9fdd 248
654598be
ZL
249 read_lock(&EXT4_I(inode)->i_es_lock);
250 tree = &EXT4_I(inode)->i_es_tree;
251
fdc0212e 252 /* find extent in cache firstly */
be401363 253 es->es_lblk = es->es_len = es->es_pblk = 0;
654598be
ZL
254 if (tree->cache_es) {
255 es1 = tree->cache_es;
be401363 256 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
fdc0212e 257 es_debug("%u cached by [%u/%u) %llu %llx\n",
be401363 258 lblk, es1->es_lblk, es1->es_len,
fdc0212e 259 ext4_es_pblock(es1), ext4_es_status(es1));
654598be
ZL
260 goto out;
261 }
262 }
263
be401363 264 es1 = __es_tree_search(&tree->root, lblk);
654598be
ZL
265
266out:
be401363
ZL
267 if (es1 && !ext4_es_is_delayed(es1)) {
268 while ((node = rb_next(&es1->rb_node)) != NULL) {
269 es1 = rb_entry(node, struct extent_status, rb_node);
270 if (ext4_es_is_delayed(es1))
271 break;
272 }
273 }
274
275 if (es1 && ext4_es_is_delayed(es1)) {
654598be 276 tree->cache_es = es1;
06b0c886
ZL
277 es->es_lblk = es1->es_lblk;
278 es->es_len = es1->es_len;
fdc0212e 279 es->es_pblk = es1->es_pblk;
654598be
ZL
280 }
281
282 read_unlock(&EXT4_I(inode)->i_es_lock);
992e9fdd 283
be401363 284 trace_ext4_es_find_delayed_extent_exit(inode, es);
654598be
ZL
285}
286
287static struct extent_status *
fdc0212e 288ext4_es_alloc_extent(ext4_lblk_t lblk, ext4_lblk_t len, ext4_fsblk_t pblk)
654598be
ZL
289{
290 struct extent_status *es;
291 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
292 if (es == NULL)
293 return NULL;
06b0c886
ZL
294 es->es_lblk = lblk;
295 es->es_len = len;
fdc0212e 296 es->es_pblk = pblk;
654598be
ZL
297 return es;
298}
299
300static void ext4_es_free_extent(struct extent_status *es)
301{
302 kmem_cache_free(ext4_es_cachep, es);
303}
304
06b0c886
ZL
305/*
306 * Check whether or not two extents can be merged
307 * Condition:
308 * - logical block number is contiguous
fdc0212e
ZL
309 * - physical block number is contiguous
310 * - status is equal
06b0c886
ZL
311 */
312static int ext4_es_can_be_merged(struct extent_status *es1,
313 struct extent_status *es2)
314{
315 if (es1->es_lblk + es1->es_len != es2->es_lblk)
316 return 0;
317
fdc0212e
ZL
318 if (ext4_es_status(es1) != ext4_es_status(es2))
319 return 0;
320
321 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
322 (ext4_es_pblock(es1) + es1->es_len != ext4_es_pblock(es2)))
323 return 0;
324
06b0c886
ZL
325 return 1;
326}
327
654598be
ZL
328static struct extent_status *
329ext4_es_try_to_merge_left(struct ext4_es_tree *tree, struct extent_status *es)
330{
331 struct extent_status *es1;
332 struct rb_node *node;
333
334 node = rb_prev(&es->rb_node);
335 if (!node)
336 return es;
337
338 es1 = rb_entry(node, struct extent_status, rb_node);
06b0c886
ZL
339 if (ext4_es_can_be_merged(es1, es)) {
340 es1->es_len += es->es_len;
654598be
ZL
341 rb_erase(&es->rb_node, &tree->root);
342 ext4_es_free_extent(es);
343 es = es1;
344 }
345
346 return es;
347}
348
349static struct extent_status *
350ext4_es_try_to_merge_right(struct ext4_es_tree *tree, struct extent_status *es)
351{
352 struct extent_status *es1;
353 struct rb_node *node;
354
355 node = rb_next(&es->rb_node);
356 if (!node)
357 return es;
358
359 es1 = rb_entry(node, struct extent_status, rb_node);
06b0c886
ZL
360 if (ext4_es_can_be_merged(es, es1)) {
361 es->es_len += es1->es_len;
654598be
ZL
362 rb_erase(node, &tree->root);
363 ext4_es_free_extent(es1);
364 }
365
366 return es;
367}
368
06b0c886
ZL
369static int __es_insert_extent(struct ext4_es_tree *tree,
370 struct extent_status *newes)
654598be
ZL
371{
372 struct rb_node **p = &tree->root.rb_node;
373 struct rb_node *parent = NULL;
374 struct extent_status *es;
654598be
ZL
375
376 while (*p) {
377 parent = *p;
378 es = rb_entry(parent, struct extent_status, rb_node);
379
06b0c886
ZL
380 if (newes->es_lblk < es->es_lblk) {
381 if (ext4_es_can_be_merged(newes, es)) {
382 /*
383 * Here we can modify es_lblk directly
384 * because it isn't overlapped.
385 */
386 es->es_lblk = newes->es_lblk;
387 es->es_len += newes->es_len;
fdc0212e
ZL
388 if (ext4_es_is_written(es) ||
389 ext4_es_is_unwritten(es))
390 ext4_es_store_pblock(es,
391 newes->es_pblk);
654598be
ZL
392 es = ext4_es_try_to_merge_left(tree, es);
393 goto out;
394 }
395 p = &(*p)->rb_left;
06b0c886
ZL
396 } else if (newes->es_lblk > ext4_es_end(es)) {
397 if (ext4_es_can_be_merged(es, newes)) {
398 es->es_len += newes->es_len;
654598be
ZL
399 es = ext4_es_try_to_merge_right(tree, es);
400 goto out;
401 }
402 p = &(*p)->rb_right;
403 } else {
06b0c886
ZL
404 BUG_ON(1);
405 return -EINVAL;
654598be
ZL
406 }
407 }
408
fdc0212e
ZL
409 es = ext4_es_alloc_extent(newes->es_lblk, newes->es_len,
410 newes->es_pblk);
654598be
ZL
411 if (!es)
412 return -ENOMEM;
413 rb_link_node(&es->rb_node, parent, p);
414 rb_insert_color(&es->rb_node, &tree->root);
415
416out:
417 tree->cache_es = es;
418 return 0;
419}
420
421/*
06b0c886 422 * ext4_es_insert_extent() adds a space to a extent status tree.
654598be
ZL
423 *
424 * ext4_es_insert_extent is called by ext4_da_write_begin and
425 * ext4_es_remove_extent.
426 *
427 * Return 0 on success, error code on failure.
428 */
06b0c886 429int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
fdc0212e
ZL
430 ext4_lblk_t len, ext4_fsblk_t pblk,
431 unsigned long long status)
654598be
ZL
432{
433 struct ext4_es_tree *tree;
06b0c886
ZL
434 struct extent_status newes;
435 ext4_lblk_t end = lblk + len - 1;
654598be
ZL
436 int err = 0;
437
fdc0212e
ZL
438 es_debug("add [%u/%u) %llu %llx to extent status tree of inode %lu\n",
439 lblk, len, pblk, status, inode->i_ino);
06b0c886
ZL
440
441 BUG_ON(end < lblk);
442
443 newes.es_lblk = lblk;
444 newes.es_len = len;
fdc0212e
ZL
445 ext4_es_store_pblock(&newes, pblk);
446 ext4_es_store_status(&newes, status);
447 trace_ext4_es_insert_extent(inode, &newes);
654598be
ZL
448
449 write_lock(&EXT4_I(inode)->i_es_lock);
450 tree = &EXT4_I(inode)->i_es_tree;
06b0c886
ZL
451 err = __es_remove_extent(tree, lblk, end);
452 if (err != 0)
453 goto error;
454 err = __es_insert_extent(tree, &newes);
455
456error:
654598be
ZL
457 write_unlock(&EXT4_I(inode)->i_es_lock);
458
459 ext4_es_print_tree(inode);
460
461 return err;
462}
463
06b0c886
ZL
464static int __es_remove_extent(struct ext4_es_tree *tree, ext4_lblk_t lblk,
465 ext4_lblk_t end)
654598be
ZL
466{
467 struct rb_node *node;
654598be
ZL
468 struct extent_status *es;
469 struct extent_status orig_es;
06b0c886 470 ext4_lblk_t len1, len2;
fdc0212e 471 ext4_fsblk_t block;
654598be
ZL
472 int err = 0;
473
06b0c886 474 es = __es_tree_search(&tree->root, lblk);
654598be
ZL
475 if (!es)
476 goto out;
06b0c886 477 if (es->es_lblk > end)
654598be
ZL
478 goto out;
479
480 /* Simply invalidate cache_es. */
481 tree->cache_es = NULL;
482
06b0c886
ZL
483 orig_es.es_lblk = es->es_lblk;
484 orig_es.es_len = es->es_len;
fdc0212e
ZL
485 orig_es.es_pblk = es->es_pblk;
486
06b0c886
ZL
487 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
488 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
654598be 489 if (len1 > 0)
06b0c886 490 es->es_len = len1;
654598be
ZL
491 if (len2 > 0) {
492 if (len1 > 0) {
06b0c886
ZL
493 struct extent_status newes;
494
495 newes.es_lblk = end + 1;
496 newes.es_len = len2;
fdc0212e
ZL
497 if (ext4_es_is_written(&orig_es) ||
498 ext4_es_is_unwritten(&orig_es)) {
499 block = ext4_es_pblock(&orig_es) +
500 orig_es.es_len - len2;
501 ext4_es_store_pblock(&newes, block);
502 }
503 ext4_es_store_status(&newes, ext4_es_status(&orig_es));
06b0c886 504 err = __es_insert_extent(tree, &newes);
654598be 505 if (err) {
06b0c886
ZL
506 es->es_lblk = orig_es.es_lblk;
507 es->es_len = orig_es.es_len;
654598be
ZL
508 goto out;
509 }
510 } else {
06b0c886
ZL
511 es->es_lblk = end + 1;
512 es->es_len = len2;
fdc0212e
ZL
513 if (ext4_es_is_written(es) ||
514 ext4_es_is_unwritten(es)) {
515 block = orig_es.es_pblk + orig_es.es_len - len2;
516 ext4_es_store_pblock(es, block);
517 }
654598be
ZL
518 }
519 goto out;
520 }
521
522 if (len1 > 0) {
523 node = rb_next(&es->rb_node);
524 if (node)
525 es = rb_entry(node, struct extent_status, rb_node);
526 else
527 es = NULL;
528 }
529
06b0c886 530 while (es && ext4_es_end(es) <= end) {
654598be
ZL
531 node = rb_next(&es->rb_node);
532 rb_erase(&es->rb_node, &tree->root);
533 ext4_es_free_extent(es);
534 if (!node) {
535 es = NULL;
536 break;
537 }
538 es = rb_entry(node, struct extent_status, rb_node);
539 }
540
06b0c886 541 if (es && es->es_lblk < end + 1) {
fdc0212e
ZL
542 ext4_lblk_t orig_len = es->es_len;
543
06b0c886
ZL
544 len1 = ext4_es_end(es) - end;
545 es->es_lblk = end + 1;
546 es->es_len = len1;
fdc0212e
ZL
547 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
548 block = es->es_pblk + orig_len - len1;
549 ext4_es_store_pblock(es, block);
550 }
654598be
ZL
551 }
552
553out:
06b0c886
ZL
554 return err;
555}
556
557/*
558 * ext4_es_remove_extent() removes a space from a extent status tree.
559 *
560 * Return 0 on success, error code on failure.
561 */
562int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
563 ext4_lblk_t len)
564{
565 struct ext4_es_tree *tree;
566 ext4_lblk_t end;
567 int err = 0;
568
569 trace_ext4_es_remove_extent(inode, lblk, len);
570 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
571 lblk, len, inode->i_ino);
572
573 end = lblk + len - 1;
574 BUG_ON(end < lblk);
575
576 tree = &EXT4_I(inode)->i_es_tree;
577
578 write_lock(&EXT4_I(inode)->i_es_lock);
579 err = __es_remove_extent(tree, lblk, end);
654598be
ZL
580 write_unlock(&EXT4_I(inode)->i_es_lock);
581 ext4_es_print_tree(inode);
582 return err;
583}