blob: 9f1380e054740b6635c692f2de1a04848a4f43e7 [file] [log] [blame]
Zheng Liu654598b2012-11-08 21:57:20 -05001/*
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
Zheng Liu992e9fd2012-11-08 21:57:33 -050017#include <trace/events/ext4.h>
18
Zheng Liu654598b2012-11-08 21:57:20 -050019/*
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
Zheng Liu06b0c882013-02-18 00:26:51 -050026 * extent tree, whose goal is only track delayed extents in memory to
Zheng Liu654598b2012-11-08 21:57:20 -050027 * simplify the implementation of fiemap and bigalloc, and introduce
28 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
Zheng Liu06b0c882013-02-18 00:26:51 -050029 * delay extent tree at the first commit. But for better understand
30 * what it does, it has been rename to extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050031 *
Zheng Liu06b0c882013-02-18 00:26:51 -050032 * 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
Zheng Liu654598b2012-11-08 21:57:20 -050036 * 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.
Zheng Liu06b0c882013-02-18 00:26:51 -050041 *
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.
Zheng Liu654598b2012-11-08 21:57:20 -050054 */
55
56/*
Zheng Liu06b0c882013-02-18 00:26:51 -050057 * Extent status tree implementation for ext4.
Zheng Liu654598b2012-11-08 21:57:20 -050058 *
59 *
60 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050061 * Extent status tree tracks all extent status.
Zheng Liu654598b2012-11-08 21:57:20 -050062 *
Zheng Liu06b0c882013-02-18 00:26:51 -050063 * 1. Why we need to implement extent status tree?
Zheng Liu654598b2012-11-08 21:57:20 -050064 *
Zheng Liu06b0c882013-02-18 00:26:51 -050065 * Without extent status tree, ext4 identifies a delayed extent by looking
Zheng Liu654598b2012-11-08 21:57:20 -050066 * up page cache, this has several deficiencies - complicated, buggy,
67 * and inefficient code.
68 *
Zheng Liu06b0c882013-02-18 00:26:51 -050069 * 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.
Zheng Liu654598b2012-11-08 21:57:20 -050071 *
Zheng Liu06b0c882013-02-18 00:26:51 -050072 * Let us have a look at how they do without extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050073 * -- 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 *
Zheng Liu654598b2012-11-08 21:57:20 -050084 * -- 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 *
Zheng Liu06b0c882013-02-18 00:26:51 -050089 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
Zheng Liu654598b2012-11-08 21:57:20 -050090 * 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
Zheng Liu06b0c882013-02-18 00:26:51 -050092 * not by searching the extent tree.
Zheng Liu654598b2012-11-08 21:57:20 -050093 *
94 *
95 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050096 * 2. Ext4 extent status tree impelmentation
Zheng Liu654598b2012-11-08 21:57:20 -050097 *
Zheng Liu06b0c882013-02-18 00:26:51 -050098 * -- 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.
Zheng Liu654598b2012-11-08 21:57:20 -0500104 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500105 * -- 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.
Zheng Liu654598b2012-11-08 21:57:20 -0500109 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500110 * -- 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.
Zheng Liu654598b2012-11-08 21:57:20 -0500113 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500114 * -- race on a extent status tree
115 * Extent status tree is protected by inode->i_es_lock.
116 *
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.
Zheng Liu654598b2012-11-08 21:57:20 -0500121 *
122 *
123 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -0500124 * 3. Performance analysis
125 *
Zheng Liu654598b2012-11-08 21:57:20 -0500126 * -- 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
Zheng Liu654598b2012-11-08 21:57:20 -0500137 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500138 * -- Refactor delayed space reservation
Zheng Liu654598b2012-11-08 21:57:20 -0500139 *
140 * -- Extent-level locking
141 */
142
143static struct kmem_cache *ext4_es_cachep;
144
Zheng Liubdedbb72013-02-18 00:32:02 -0500145static int __es_insert_extent(struct inode *inode, struct extent_status *newes);
146static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liu06b0c882013-02-18 00:26:51 -0500147 ext4_lblk_t end);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500148static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
149 int nr_to_scan);
150static int ext4_es_reclaim_extents_count(struct super_block *sb);
Zheng Liu06b0c882013-02-18 00:26:51 -0500151
Zheng Liu654598b2012-11-08 21:57:20 -0500152int __init ext4_init_es(void)
153{
154 ext4_es_cachep = KMEM_CACHE(extent_status, SLAB_RECLAIM_ACCOUNT);
155 if (ext4_es_cachep == NULL)
156 return -ENOMEM;
157 return 0;
158}
159
160void ext4_exit_es(void)
161{
162 if (ext4_es_cachep)
163 kmem_cache_destroy(ext4_es_cachep);
164}
165
166void ext4_es_init_tree(struct ext4_es_tree *tree)
167{
168 tree->root = RB_ROOT;
169 tree->cache_es = NULL;
170}
171
172#ifdef ES_DEBUG__
173static void ext4_es_print_tree(struct inode *inode)
174{
175 struct ext4_es_tree *tree;
176 struct rb_node *node;
177
178 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
179 tree = &EXT4_I(inode)->i_es_tree;
180 node = rb_first(&tree->root);
181 while (node) {
182 struct extent_status *es;
183 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liufdc02122013-02-18 00:26:51 -0500184 printk(KERN_DEBUG " [%u/%u) %llu %llx",
185 es->es_lblk, es->es_len,
186 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500187 node = rb_next(node);
188 }
189 printk(KERN_DEBUG "\n");
190}
191#else
192#define ext4_es_print_tree(inode)
193#endif
194
Zheng Liu06b0c882013-02-18 00:26:51 -0500195static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500196{
Zheng Liu06b0c882013-02-18 00:26:51 -0500197 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
198 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500199}
200
201/*
202 * search through the tree for an delayed extent with a given offset. If
203 * it can't be found, try to find next extent.
204 */
205static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500206 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500207{
208 struct rb_node *node = root->rb_node;
209 struct extent_status *es = NULL;
210
211 while (node) {
212 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500213 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500214 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500215 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500216 node = node->rb_right;
217 else
218 return es;
219 }
220
Zheng Liu06b0c882013-02-18 00:26:51 -0500221 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500222 return es;
223
Zheng Liu06b0c882013-02-18 00:26:51 -0500224 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500225 node = rb_next(&es->rb_node);
226 return node ? rb_entry(node, struct extent_status, rb_node) :
227 NULL;
228 }
229
230 return NULL;
231}
232
233/*
Zheng Liube401362013-02-18 00:27:26 -0500234 * ext4_es_find_delayed_extent: find the 1st delayed extent covering @es->lblk
Zheng Liu06b0c882013-02-18 00:26:51 -0500235 * if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500236 *
237 * @inode: the inode which owns delayed extents
Zheng Liube401362013-02-18 00:27:26 -0500238 * @lblk: the offset where we start to search
Zheng Liu654598b2012-11-08 21:57:20 -0500239 * @es: delayed extent that we found
Zheng Liu654598b2012-11-08 21:57:20 -0500240 */
Zheng Liube401362013-02-18 00:27:26 -0500241void ext4_es_find_delayed_extent(struct inode *inode, ext4_lblk_t lblk,
242 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500243{
244 struct ext4_es_tree *tree = NULL;
245 struct extent_status *es1 = NULL;
246 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500247
Zheng Liube401362013-02-18 00:27:26 -0500248 BUG_ON(es == NULL);
249 trace_ext4_es_find_delayed_extent_enter(inode, lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500250
Zheng Liu654598b2012-11-08 21:57:20 -0500251 read_lock(&EXT4_I(inode)->i_es_lock);
252 tree = &EXT4_I(inode)->i_es_tree;
253
Zheng Liufdc02122013-02-18 00:26:51 -0500254 /* find extent in cache firstly */
Zheng Liube401362013-02-18 00:27:26 -0500255 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500256 if (tree->cache_es) {
257 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500258 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Zheng Liufdc02122013-02-18 00:26:51 -0500259 es_debug("%u cached by [%u/%u) %llu %llx\n",
Zheng Liube401362013-02-18 00:27:26 -0500260 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500261 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500262 goto out;
263 }
264 }
265
Zheng Liube401362013-02-18 00:27:26 -0500266 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500267
268out:
Zheng Liube401362013-02-18 00:27:26 -0500269 if (es1 && !ext4_es_is_delayed(es1)) {
270 while ((node = rb_next(&es1->rb_node)) != NULL) {
271 es1 = rb_entry(node, struct extent_status, rb_node);
272 if (ext4_es_is_delayed(es1))
273 break;
274 }
275 }
276
277 if (es1 && ext4_es_is_delayed(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500278 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500279 es->es_lblk = es1->es_lblk;
280 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500281 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500282 }
283
284 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500285
Zheng Liu74cd15c2013-02-18 00:32:55 -0500286 ext4_es_lru_add(inode);
Zheng Liube401362013-02-18 00:27:26 -0500287 trace_ext4_es_find_delayed_extent_exit(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500288}
289
290static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500291ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
292 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500293{
294 struct extent_status *es;
295 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
296 if (es == NULL)
297 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500298 es->es_lblk = lblk;
299 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500300 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500301
302 /*
303 * We don't count delayed extent because we never try to reclaim them
304 */
305 if (!ext4_es_is_delayed(es))
306 EXT4_I(inode)->i_es_lru_nr++;
307
Zheng Liu654598b2012-11-08 21:57:20 -0500308 return es;
309}
310
Zheng Liubdedbb72013-02-18 00:32:02 -0500311static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500312{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500313 /* Decrease the lru counter when this es is not delayed */
314 if (!ext4_es_is_delayed(es)) {
315 BUG_ON(EXT4_I(inode)->i_es_lru_nr == 0);
316 EXT4_I(inode)->i_es_lru_nr--;
317 }
318
Zheng Liu654598b2012-11-08 21:57:20 -0500319 kmem_cache_free(ext4_es_cachep, es);
320}
321
Zheng Liu06b0c882013-02-18 00:26:51 -0500322/*
323 * Check whether or not two extents can be merged
324 * Condition:
325 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500326 * - physical block number is contiguous
327 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500328 */
329static int ext4_es_can_be_merged(struct extent_status *es1,
330 struct extent_status *es2)
331{
332 if (es1->es_lblk + es1->es_len != es2->es_lblk)
333 return 0;
334
Zheng Liufdc02122013-02-18 00:26:51 -0500335 if (ext4_es_status(es1) != ext4_es_status(es2))
336 return 0;
337
338 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
339 (ext4_es_pblock(es1) + es1->es_len != ext4_es_pblock(es2)))
340 return 0;
341
Zheng Liu06b0c882013-02-18 00:26:51 -0500342 return 1;
343}
344
Zheng Liu654598b2012-11-08 21:57:20 -0500345static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500346ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500347{
Zheng Liubdedbb72013-02-18 00:32:02 -0500348 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500349 struct extent_status *es1;
350 struct rb_node *node;
351
352 node = rb_prev(&es->rb_node);
353 if (!node)
354 return es;
355
356 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500357 if (ext4_es_can_be_merged(es1, es)) {
358 es1->es_len += es->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500359 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500360 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500361 es = es1;
362 }
363
364 return es;
365}
366
367static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500368ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500369{
Zheng Liubdedbb72013-02-18 00:32:02 -0500370 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500371 struct extent_status *es1;
372 struct rb_node *node;
373
374 node = rb_next(&es->rb_node);
375 if (!node)
376 return es;
377
378 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500379 if (ext4_es_can_be_merged(es, es1)) {
380 es->es_len += es1->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500381 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500382 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500383 }
384
385 return es;
386}
387
Zheng Liubdedbb72013-02-18 00:32:02 -0500388static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500389{
Zheng Liubdedbb72013-02-18 00:32:02 -0500390 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500391 struct rb_node **p = &tree->root.rb_node;
392 struct rb_node *parent = NULL;
393 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500394
395 while (*p) {
396 parent = *p;
397 es = rb_entry(parent, struct extent_status, rb_node);
398
Zheng Liu06b0c882013-02-18 00:26:51 -0500399 if (newes->es_lblk < es->es_lblk) {
400 if (ext4_es_can_be_merged(newes, es)) {
401 /*
402 * Here we can modify es_lblk directly
403 * because it isn't overlapped.
404 */
405 es->es_lblk = newes->es_lblk;
406 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500407 if (ext4_es_is_written(es) ||
408 ext4_es_is_unwritten(es))
409 ext4_es_store_pblock(es,
410 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500411 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500412 goto out;
413 }
414 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500415 } else if (newes->es_lblk > ext4_es_end(es)) {
416 if (ext4_es_can_be_merged(es, newes)) {
417 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500418 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500419 goto out;
420 }
421 p = &(*p)->rb_right;
422 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500423 BUG_ON(1);
424 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500425 }
426 }
427
Zheng Liubdedbb72013-02-18 00:32:02 -0500428 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500429 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500430 if (!es)
431 return -ENOMEM;
432 rb_link_node(&es->rb_node, parent, p);
433 rb_insert_color(&es->rb_node, &tree->root);
434
435out:
436 tree->cache_es = es;
437 return 0;
438}
439
440/*
Zheng Liu06b0c882013-02-18 00:26:51 -0500441 * ext4_es_insert_extent() adds a space to a extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500442 *
443 * ext4_es_insert_extent is called by ext4_da_write_begin and
444 * ext4_es_remove_extent.
445 *
446 * Return 0 on success, error code on failure.
447 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500448int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500449 ext4_lblk_t len, ext4_fsblk_t pblk,
450 unsigned long long status)
Zheng Liu654598b2012-11-08 21:57:20 -0500451{
Zheng Liu06b0c882013-02-18 00:26:51 -0500452 struct extent_status newes;
453 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500454 int err = 0;
455
Zheng Liufdc02122013-02-18 00:26:51 -0500456 es_debug("add [%u/%u) %llu %llx to extent status tree of inode %lu\n",
457 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500458
459 BUG_ON(end < lblk);
460
461 newes.es_lblk = lblk;
462 newes.es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500463 ext4_es_store_pblock(&newes, pblk);
464 ext4_es_store_status(&newes, status);
465 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500466
467 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500468 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500469 if (err != 0)
470 goto error;
Zheng Liubdedbb72013-02-18 00:32:02 -0500471 err = __es_insert_extent(inode, &newes);
Zheng Liu06b0c882013-02-18 00:26:51 -0500472
473error:
Zheng Liu654598b2012-11-08 21:57:20 -0500474 write_unlock(&EXT4_I(inode)->i_es_lock);
475
Zheng Liu74cd15c2013-02-18 00:32:55 -0500476 ext4_es_lru_add(inode);
Zheng Liu654598b2012-11-08 21:57:20 -0500477 ext4_es_print_tree(inode);
478
479 return err;
480}
481
Zheng Liud100eef2013-02-18 00:29:59 -0500482/*
483 * ext4_es_lookup_extent() looks up an extent in extent status tree.
484 *
485 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
486 *
487 * Return: 1 on found, 0 on not
488 */
489int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
490 struct extent_status *es)
491{
492 struct ext4_es_tree *tree;
493 struct extent_status *es1 = NULL;
494 struct rb_node *node;
495 int found = 0;
496
497 trace_ext4_es_lookup_extent_enter(inode, lblk);
498 es_debug("lookup extent in block %u\n", lblk);
499
500 tree = &EXT4_I(inode)->i_es_tree;
501 read_lock(&EXT4_I(inode)->i_es_lock);
502
503 /* find extent in cache firstly */
504 es->es_lblk = es->es_len = es->es_pblk = 0;
505 if (tree->cache_es) {
506 es1 = tree->cache_es;
507 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
508 es_debug("%u cached by [%u/%u)\n",
509 lblk, es1->es_lblk, es1->es_len);
510 found = 1;
511 goto out;
512 }
513 }
514
515 node = tree->root.rb_node;
516 while (node) {
517 es1 = rb_entry(node, struct extent_status, rb_node);
518 if (lblk < es1->es_lblk)
519 node = node->rb_left;
520 else if (lblk > ext4_es_end(es1))
521 node = node->rb_right;
522 else {
523 found = 1;
524 break;
525 }
526 }
527
528out:
529 if (found) {
530 BUG_ON(!es1);
531 es->es_lblk = es1->es_lblk;
532 es->es_len = es1->es_len;
533 es->es_pblk = es1->es_pblk;
534 }
535
536 read_unlock(&EXT4_I(inode)->i_es_lock);
537
Zheng Liu74cd15c2013-02-18 00:32:55 -0500538 ext4_es_lru_add(inode);
Zheng Liud100eef2013-02-18 00:29:59 -0500539 trace_ext4_es_lookup_extent_exit(inode, es, found);
540 return found;
541}
542
Zheng Liubdedbb72013-02-18 00:32:02 -0500543static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
544 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500545{
Zheng Liubdedbb72013-02-18 00:32:02 -0500546 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500547 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500548 struct extent_status *es;
549 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500550 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500551 ext4_fsblk_t block;
Zheng Liu654598b2012-11-08 21:57:20 -0500552 int err = 0;
553
Zheng Liu06b0c882013-02-18 00:26:51 -0500554 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500555 if (!es)
556 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500557 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500558 goto out;
559
560 /* Simply invalidate cache_es. */
561 tree->cache_es = NULL;
562
Zheng Liu06b0c882013-02-18 00:26:51 -0500563 orig_es.es_lblk = es->es_lblk;
564 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500565 orig_es.es_pblk = es->es_pblk;
566
Zheng Liu06b0c882013-02-18 00:26:51 -0500567 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
568 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500569 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500570 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500571 if (len2 > 0) {
572 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500573 struct extent_status newes;
574
575 newes.es_lblk = end + 1;
576 newes.es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500577 if (ext4_es_is_written(&orig_es) ||
578 ext4_es_is_unwritten(&orig_es)) {
579 block = ext4_es_pblock(&orig_es) +
580 orig_es.es_len - len2;
581 ext4_es_store_pblock(&newes, block);
582 }
583 ext4_es_store_status(&newes, ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500584 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500585 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500586 es->es_lblk = orig_es.es_lblk;
587 es->es_len = orig_es.es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500588 goto out;
589 }
590 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500591 es->es_lblk = end + 1;
592 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500593 if (ext4_es_is_written(es) ||
594 ext4_es_is_unwritten(es)) {
595 block = orig_es.es_pblk + orig_es.es_len - len2;
596 ext4_es_store_pblock(es, block);
597 }
Zheng Liu654598b2012-11-08 21:57:20 -0500598 }
599 goto out;
600 }
601
602 if (len1 > 0) {
603 node = rb_next(&es->rb_node);
604 if (node)
605 es = rb_entry(node, struct extent_status, rb_node);
606 else
607 es = NULL;
608 }
609
Zheng Liu06b0c882013-02-18 00:26:51 -0500610 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500611 node = rb_next(&es->rb_node);
612 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500613 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500614 if (!node) {
615 es = NULL;
616 break;
617 }
618 es = rb_entry(node, struct extent_status, rb_node);
619 }
620
Zheng Liu06b0c882013-02-18 00:26:51 -0500621 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -0500622 ext4_lblk_t orig_len = es->es_len;
623
Zheng Liu06b0c882013-02-18 00:26:51 -0500624 len1 = ext4_es_end(es) - end;
625 es->es_lblk = end + 1;
626 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -0500627 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
628 block = es->es_pblk + orig_len - len1;
629 ext4_es_store_pblock(es, block);
630 }
Zheng Liu654598b2012-11-08 21:57:20 -0500631 }
632
633out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500634 return err;
635}
636
637/*
638 * ext4_es_remove_extent() removes a space from a extent status tree.
639 *
640 * Return 0 on success, error code on failure.
641 */
642int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
643 ext4_lblk_t len)
644{
Zheng Liu06b0c882013-02-18 00:26:51 -0500645 ext4_lblk_t end;
646 int err = 0;
647
648 trace_ext4_es_remove_extent(inode, lblk, len);
649 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
650 lblk, len, inode->i_ino);
651
652 end = lblk + len - 1;
653 BUG_ON(end < lblk);
654
Zheng Liu06b0c882013-02-18 00:26:51 -0500655 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500656 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500657 write_unlock(&EXT4_I(inode)->i_es_lock);
658 ext4_es_print_tree(inode);
659 return err;
660}
Zheng Liu74cd15c2013-02-18 00:32:55 -0500661
662static int ext4_es_shrink(struct shrinker *shrink, struct shrink_control *sc)
663{
664 struct ext4_sb_info *sbi = container_of(shrink,
665 struct ext4_sb_info, s_es_shrinker);
666 struct ext4_inode_info *ei;
667 struct list_head *cur, *tmp, scanned;
668 int nr_to_scan = sc->nr_to_scan;
669 int ret, nr_shrunk = 0;
670
671 trace_ext4_es_shrink_enter(sbi->s_sb, nr_to_scan);
672
673 if (!nr_to_scan)
674 return ext4_es_reclaim_extents_count(sbi->s_sb);
675
676 INIT_LIST_HEAD(&scanned);
677
678 spin_lock(&sbi->s_es_lru_lock);
679 list_for_each_safe(cur, tmp, &sbi->s_es_lru) {
680 list_move_tail(cur, &scanned);
681
682 ei = list_entry(cur, struct ext4_inode_info, i_es_lru);
683
684 read_lock(&ei->i_es_lock);
685 if (ei->i_es_lru_nr == 0) {
686 read_unlock(&ei->i_es_lock);
687 continue;
688 }
689 read_unlock(&ei->i_es_lock);
690
691 write_lock(&ei->i_es_lock);
692 ret = __es_try_to_reclaim_extents(ei, nr_to_scan);
693 write_unlock(&ei->i_es_lock);
694
695 nr_shrunk += ret;
696 nr_to_scan -= ret;
697 if (nr_to_scan == 0)
698 break;
699 }
700 list_splice_tail(&scanned, &sbi->s_es_lru);
701 spin_unlock(&sbi->s_es_lru_lock);
702 trace_ext4_es_shrink_exit(sbi->s_sb, nr_shrunk);
703
704 return ext4_es_reclaim_extents_count(sbi->s_sb);
705}
706
707void ext4_es_register_shrinker(struct super_block *sb)
708{
709 struct ext4_sb_info *sbi;
710
711 sbi = EXT4_SB(sb);
712 INIT_LIST_HEAD(&sbi->s_es_lru);
713 spin_lock_init(&sbi->s_es_lru_lock);
714 sbi->s_es_shrinker.shrink = ext4_es_shrink;
715 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
716 register_shrinker(&sbi->s_es_shrinker);
717}
718
719void ext4_es_unregister_shrinker(struct super_block *sb)
720{
721 unregister_shrinker(&EXT4_SB(sb)->s_es_shrinker);
722}
723
724void ext4_es_lru_add(struct inode *inode)
725{
726 struct ext4_inode_info *ei = EXT4_I(inode);
727 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
728
729 spin_lock(&sbi->s_es_lru_lock);
730 if (list_empty(&ei->i_es_lru))
731 list_add_tail(&ei->i_es_lru, &sbi->s_es_lru);
732 else
733 list_move_tail(&ei->i_es_lru, &sbi->s_es_lru);
734 spin_unlock(&sbi->s_es_lru_lock);
735}
736
737void ext4_es_lru_del(struct inode *inode)
738{
739 struct ext4_inode_info *ei = EXT4_I(inode);
740 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
741
742 spin_lock(&sbi->s_es_lru_lock);
743 if (!list_empty(&ei->i_es_lru))
744 list_del_init(&ei->i_es_lru);
745 spin_unlock(&sbi->s_es_lru_lock);
746}
747
748static int ext4_es_reclaim_extents_count(struct super_block *sb)
749{
750 struct ext4_sb_info *sbi = EXT4_SB(sb);
751 struct ext4_inode_info *ei;
752 struct list_head *cur;
753 int nr_cached = 0;
754
755 spin_lock(&sbi->s_es_lru_lock);
756 list_for_each(cur, &sbi->s_es_lru) {
757 ei = list_entry(cur, struct ext4_inode_info, i_es_lru);
758 read_lock(&ei->i_es_lock);
759 nr_cached += ei->i_es_lru_nr;
760 read_unlock(&ei->i_es_lock);
761 }
762 spin_unlock(&sbi->s_es_lru_lock);
763 trace_ext4_es_reclaim_extents_count(sb, nr_cached);
764 return nr_cached;
765}
766
767static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
768 int nr_to_scan)
769{
770 struct inode *inode = &ei->vfs_inode;
771 struct ext4_es_tree *tree = &ei->i_es_tree;
772 struct rb_node *node;
773 struct extent_status *es;
774 int nr_shrunk = 0;
775
776 if (ei->i_es_lru_nr == 0)
777 return 0;
778
779 node = rb_first(&tree->root);
780 while (node != NULL) {
781 es = rb_entry(node, struct extent_status, rb_node);
782 node = rb_next(&es->rb_node);
783 /*
784 * We can't reclaim delayed extent from status tree because
785 * fiemap, bigallic, and seek_data/hole need to use it.
786 */
787 if (!ext4_es_is_delayed(es)) {
788 rb_erase(&es->rb_node, &tree->root);
789 ext4_es_free_extent(inode, es);
790 nr_shrunk++;
791 if (--nr_to_scan == 0)
792 break;
793 }
794 }
795 tree->cache_es = NULL;
796 return nr_shrunk;
797}