blob: 37f9a2d8fd0487c7e868c9e9ba4d009a004f689f [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);
Zheng Liu06b0c882013-02-18 00:26:51 -0500150
Zheng Liu654598b2012-11-08 21:57:20 -0500151int __init ext4_init_es(void)
152{
Theodore Ts'o24630772013-02-28 23:58:56 -0500153 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
154 sizeof(struct extent_status),
155 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500156 if (ext4_es_cachep == NULL)
157 return -ENOMEM;
158 return 0;
159}
160
161void ext4_exit_es(void)
162{
163 if (ext4_es_cachep)
164 kmem_cache_destroy(ext4_es_cachep);
165}
166
167void ext4_es_init_tree(struct ext4_es_tree *tree)
168{
169 tree->root = RB_ROOT;
170 tree->cache_es = NULL;
171}
172
173#ifdef ES_DEBUG__
174static void ext4_es_print_tree(struct inode *inode)
175{
176 struct ext4_es_tree *tree;
177 struct rb_node *node;
178
179 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
180 tree = &EXT4_I(inode)->i_es_tree;
181 node = rb_first(&tree->root);
182 while (node) {
183 struct extent_status *es;
184 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liufdc02122013-02-18 00:26:51 -0500185 printk(KERN_DEBUG " [%u/%u) %llu %llx",
186 es->es_lblk, es->es_len,
187 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500188 node = rb_next(node);
189 }
190 printk(KERN_DEBUG "\n");
191}
192#else
193#define ext4_es_print_tree(inode)
194#endif
195
Zheng Liu06b0c882013-02-18 00:26:51 -0500196static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500197{
Zheng Liu06b0c882013-02-18 00:26:51 -0500198 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
199 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500200}
201
202/*
203 * search through the tree for an delayed extent with a given offset. If
204 * it can't be found, try to find next extent.
205 */
206static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500207 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500208{
209 struct rb_node *node = root->rb_node;
210 struct extent_status *es = NULL;
211
212 while (node) {
213 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500214 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500215 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500216 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500217 node = node->rb_right;
218 else
219 return es;
220 }
221
Zheng Liu06b0c882013-02-18 00:26:51 -0500222 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500223 return es;
224
Zheng Liu06b0c882013-02-18 00:26:51 -0500225 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500226 node = rb_next(&es->rb_node);
227 return node ? rb_entry(node, struct extent_status, rb_node) :
228 NULL;
229 }
230
231 return NULL;
232}
233
234/*
Zheng Liube401362013-02-18 00:27:26 -0500235 * ext4_es_find_delayed_extent: find the 1st delayed extent covering @es->lblk
Zheng Liu06b0c882013-02-18 00:26:51 -0500236 * if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500237 *
238 * @inode: the inode which owns delayed extents
Zheng Liube401362013-02-18 00:27:26 -0500239 * @lblk: the offset where we start to search
Zheng Liu654598b2012-11-08 21:57:20 -0500240 * @es: delayed extent that we found
Zheng Liu654598b2012-11-08 21:57:20 -0500241 */
Zheng Liube401362013-02-18 00:27:26 -0500242void ext4_es_find_delayed_extent(struct inode *inode, ext4_lblk_t lblk,
243 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500244{
245 struct ext4_es_tree *tree = NULL;
246 struct extent_status *es1 = NULL;
247 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500248
Zheng Liube401362013-02-18 00:27:26 -0500249 BUG_ON(es == NULL);
250 trace_ext4_es_find_delayed_extent_enter(inode, lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500251
Zheng Liu654598b2012-11-08 21:57:20 -0500252 read_lock(&EXT4_I(inode)->i_es_lock);
253 tree = &EXT4_I(inode)->i_es_tree;
254
Zheng Liufdc02122013-02-18 00:26:51 -0500255 /* find extent in cache firstly */
Zheng Liube401362013-02-18 00:27:26 -0500256 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500257 if (tree->cache_es) {
258 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500259 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Zheng Liufdc02122013-02-18 00:26:51 -0500260 es_debug("%u cached by [%u/%u) %llu %llx\n",
Zheng Liube401362013-02-18 00:27:26 -0500261 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500262 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500263 goto out;
264 }
265 }
266
Zheng Liube401362013-02-18 00:27:26 -0500267 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500268
269out:
Zheng Liube401362013-02-18 00:27:26 -0500270 if (es1 && !ext4_es_is_delayed(es1)) {
271 while ((node = rb_next(&es1->rb_node)) != NULL) {
272 es1 = rb_entry(node, struct extent_status, rb_node);
273 if (ext4_es_is_delayed(es1))
274 break;
275 }
276 }
277
278 if (es1 && ext4_es_is_delayed(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500279 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500280 es->es_lblk = es1->es_lblk;
281 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500282 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500283 }
284
285 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500286
Zheng Liu74cd15c2013-02-18 00:32:55 -0500287 ext4_es_lru_add(inode);
Zheng Liube401362013-02-18 00:27:26 -0500288 trace_ext4_es_find_delayed_extent_exit(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500289}
290
291static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500292ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
293 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500294{
295 struct extent_status *es;
296 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
297 if (es == NULL)
298 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500299 es->es_lblk = lblk;
300 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500301 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500302
303 /*
304 * We don't count delayed extent because we never try to reclaim them
305 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500306 if (!ext4_es_is_delayed(es)) {
Zheng Liu74cd15c2013-02-18 00:32:55 -0500307 EXT4_I(inode)->i_es_lru_nr++;
Theodore Ts'o1ac64662013-03-02 10:27:46 -0500308 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500309 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500310
Zheng Liu654598b2012-11-08 21:57:20 -0500311 return es;
312}
313
Zheng Liubdedbb72013-02-18 00:32:02 -0500314static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500315{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500316 /* Decrease the lru counter when this es is not delayed */
317 if (!ext4_es_is_delayed(es)) {
318 BUG_ON(EXT4_I(inode)->i_es_lru_nr == 0);
319 EXT4_I(inode)->i_es_lru_nr--;
Theodore Ts'o1ac64662013-03-02 10:27:46 -0500320 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500321 }
322
Zheng Liu654598b2012-11-08 21:57:20 -0500323 kmem_cache_free(ext4_es_cachep, es);
324}
325
Zheng Liu06b0c882013-02-18 00:26:51 -0500326/*
327 * Check whether or not two extents can be merged
328 * Condition:
329 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500330 * - physical block number is contiguous
331 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500332 */
333static int ext4_es_can_be_merged(struct extent_status *es1,
334 struct extent_status *es2)
335{
Zheng Liufdc02122013-02-18 00:26:51 -0500336 if (ext4_es_status(es1) != ext4_es_status(es2))
337 return 0;
338
Zheng Liubd384362013-03-10 20:48:59 -0400339 if (((__u64) es1->es_len) + es2->es_len > 0xFFFFFFFFULL)
Zheng Liufdc02122013-02-18 00:26:51 -0500340 return 0;
341
Zheng Liubd384362013-03-10 20:48:59 -0400342 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
343 return 0;
344
345 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
346 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
347 return 1;
348
349 if (ext4_es_is_hole(es1))
350 return 1;
351
352 /* we need to check delayed extent is without unwritten status */
353 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
354 return 1;
355
356 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500357}
358
Zheng Liu654598b2012-11-08 21:57:20 -0500359static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500360ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500361{
Zheng Liubdedbb72013-02-18 00:32:02 -0500362 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500363 struct extent_status *es1;
364 struct rb_node *node;
365
366 node = rb_prev(&es->rb_node);
367 if (!node)
368 return es;
369
370 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500371 if (ext4_es_can_be_merged(es1, es)) {
372 es1->es_len += es->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500373 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500374 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500375 es = es1;
376 }
377
378 return es;
379}
380
381static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500382ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500383{
Zheng Liubdedbb72013-02-18 00:32:02 -0500384 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500385 struct extent_status *es1;
386 struct rb_node *node;
387
388 node = rb_next(&es->rb_node);
389 if (!node)
390 return es;
391
392 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500393 if (ext4_es_can_be_merged(es, es1)) {
394 es->es_len += es1->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500395 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500396 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500397 }
398
399 return es;
400}
401
Zheng Liubdedbb72013-02-18 00:32:02 -0500402static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500403{
Zheng Liubdedbb72013-02-18 00:32:02 -0500404 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500405 struct rb_node **p = &tree->root.rb_node;
406 struct rb_node *parent = NULL;
407 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500408
409 while (*p) {
410 parent = *p;
411 es = rb_entry(parent, struct extent_status, rb_node);
412
Zheng Liu06b0c882013-02-18 00:26:51 -0500413 if (newes->es_lblk < es->es_lblk) {
414 if (ext4_es_can_be_merged(newes, es)) {
415 /*
416 * Here we can modify es_lblk directly
417 * because it isn't overlapped.
418 */
419 es->es_lblk = newes->es_lblk;
420 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500421 if (ext4_es_is_written(es) ||
422 ext4_es_is_unwritten(es))
423 ext4_es_store_pblock(es,
424 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500425 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500426 goto out;
427 }
428 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500429 } else if (newes->es_lblk > ext4_es_end(es)) {
430 if (ext4_es_can_be_merged(es, newes)) {
431 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500432 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500433 goto out;
434 }
435 p = &(*p)->rb_right;
436 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500437 BUG_ON(1);
438 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500439 }
440 }
441
Zheng Liubdedbb72013-02-18 00:32:02 -0500442 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500443 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500444 if (!es)
445 return -ENOMEM;
446 rb_link_node(&es->rb_node, parent, p);
447 rb_insert_color(&es->rb_node, &tree->root);
448
449out:
450 tree->cache_es = es;
451 return 0;
452}
453
454/*
Zheng Liu06b0c882013-02-18 00:26:51 -0500455 * ext4_es_insert_extent() adds a space to a extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500456 *
457 * ext4_es_insert_extent is called by ext4_da_write_begin and
458 * ext4_es_remove_extent.
459 *
460 * Return 0 on success, error code on failure.
461 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500462int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500463 ext4_lblk_t len, ext4_fsblk_t pblk,
464 unsigned long long status)
Zheng Liu654598b2012-11-08 21:57:20 -0500465{
Zheng Liu06b0c882013-02-18 00:26:51 -0500466 struct extent_status newes;
467 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500468 int err = 0;
469
Zheng Liufdc02122013-02-18 00:26:51 -0500470 es_debug("add [%u/%u) %llu %llx to extent status tree of inode %lu\n",
471 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500472
Eryu Guand4381472013-02-22 15:27:47 -0500473 if (!len)
474 return 0;
475
Zheng Liu06b0c882013-02-18 00:26:51 -0500476 BUG_ON(end < lblk);
477
478 newes.es_lblk = lblk;
479 newes.es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500480 ext4_es_store_pblock(&newes, pblk);
481 ext4_es_store_status(&newes, status);
482 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500483
484 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500485 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500486 if (err != 0)
487 goto error;
Zheng Liubdedbb72013-02-18 00:32:02 -0500488 err = __es_insert_extent(inode, &newes);
Zheng Liu06b0c882013-02-18 00:26:51 -0500489
490error:
Zheng Liu654598b2012-11-08 21:57:20 -0500491 write_unlock(&EXT4_I(inode)->i_es_lock);
492
Zheng Liu74cd15c2013-02-18 00:32:55 -0500493 ext4_es_lru_add(inode);
Zheng Liu654598b2012-11-08 21:57:20 -0500494 ext4_es_print_tree(inode);
495
496 return err;
497}
498
Zheng Liud100eef2013-02-18 00:29:59 -0500499/*
500 * ext4_es_lookup_extent() looks up an extent in extent status tree.
501 *
502 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
503 *
504 * Return: 1 on found, 0 on not
505 */
506int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
507 struct extent_status *es)
508{
509 struct ext4_es_tree *tree;
510 struct extent_status *es1 = NULL;
511 struct rb_node *node;
512 int found = 0;
513
514 trace_ext4_es_lookup_extent_enter(inode, lblk);
515 es_debug("lookup extent in block %u\n", lblk);
516
517 tree = &EXT4_I(inode)->i_es_tree;
518 read_lock(&EXT4_I(inode)->i_es_lock);
519
520 /* find extent in cache firstly */
521 es->es_lblk = es->es_len = es->es_pblk = 0;
522 if (tree->cache_es) {
523 es1 = tree->cache_es;
524 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
525 es_debug("%u cached by [%u/%u)\n",
526 lblk, es1->es_lblk, es1->es_len);
527 found = 1;
528 goto out;
529 }
530 }
531
532 node = tree->root.rb_node;
533 while (node) {
534 es1 = rb_entry(node, struct extent_status, rb_node);
535 if (lblk < es1->es_lblk)
536 node = node->rb_left;
537 else if (lblk > ext4_es_end(es1))
538 node = node->rb_right;
539 else {
540 found = 1;
541 break;
542 }
543 }
544
545out:
546 if (found) {
547 BUG_ON(!es1);
548 es->es_lblk = es1->es_lblk;
549 es->es_len = es1->es_len;
550 es->es_pblk = es1->es_pblk;
551 }
552
553 read_unlock(&EXT4_I(inode)->i_es_lock);
554
Zheng Liu74cd15c2013-02-18 00:32:55 -0500555 ext4_es_lru_add(inode);
Zheng Liud100eef2013-02-18 00:29:59 -0500556 trace_ext4_es_lookup_extent_exit(inode, es, found);
557 return found;
558}
559
Zheng Liubdedbb72013-02-18 00:32:02 -0500560static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
561 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500562{
Zheng Liubdedbb72013-02-18 00:32:02 -0500563 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500564 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500565 struct extent_status *es;
566 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500567 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500568 ext4_fsblk_t block;
Zheng Liu654598b2012-11-08 21:57:20 -0500569 int err = 0;
570
Zheng Liu06b0c882013-02-18 00:26:51 -0500571 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500572 if (!es)
573 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500574 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500575 goto out;
576
577 /* Simply invalidate cache_es. */
578 tree->cache_es = NULL;
579
Zheng Liu06b0c882013-02-18 00:26:51 -0500580 orig_es.es_lblk = es->es_lblk;
581 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500582 orig_es.es_pblk = es->es_pblk;
583
Zheng Liu06b0c882013-02-18 00:26:51 -0500584 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
585 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500586 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500587 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500588 if (len2 > 0) {
589 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500590 struct extent_status newes;
591
592 newes.es_lblk = end + 1;
593 newes.es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500594 if (ext4_es_is_written(&orig_es) ||
595 ext4_es_is_unwritten(&orig_es)) {
596 block = ext4_es_pblock(&orig_es) +
597 orig_es.es_len - len2;
598 ext4_es_store_pblock(&newes, block);
599 }
600 ext4_es_store_status(&newes, ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500601 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500602 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500603 es->es_lblk = orig_es.es_lblk;
604 es->es_len = orig_es.es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500605 goto out;
606 }
607 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500608 es->es_lblk = end + 1;
609 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500610 if (ext4_es_is_written(es) ||
611 ext4_es_is_unwritten(es)) {
612 block = orig_es.es_pblk + orig_es.es_len - len2;
613 ext4_es_store_pblock(es, block);
614 }
Zheng Liu654598b2012-11-08 21:57:20 -0500615 }
616 goto out;
617 }
618
619 if (len1 > 0) {
620 node = rb_next(&es->rb_node);
621 if (node)
622 es = rb_entry(node, struct extent_status, rb_node);
623 else
624 es = NULL;
625 }
626
Zheng Liu06b0c882013-02-18 00:26:51 -0500627 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500628 node = rb_next(&es->rb_node);
629 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500630 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500631 if (!node) {
632 es = NULL;
633 break;
634 }
635 es = rb_entry(node, struct extent_status, rb_node);
636 }
637
Zheng Liu06b0c882013-02-18 00:26:51 -0500638 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -0500639 ext4_lblk_t orig_len = es->es_len;
640
Zheng Liu06b0c882013-02-18 00:26:51 -0500641 len1 = ext4_es_end(es) - end;
642 es->es_lblk = end + 1;
643 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -0500644 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
645 block = es->es_pblk + orig_len - len1;
646 ext4_es_store_pblock(es, block);
647 }
Zheng Liu654598b2012-11-08 21:57:20 -0500648 }
649
650out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500651 return err;
652}
653
654/*
655 * ext4_es_remove_extent() removes a space from a extent status tree.
656 *
657 * Return 0 on success, error code on failure.
658 */
659int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
660 ext4_lblk_t len)
661{
Zheng Liu06b0c882013-02-18 00:26:51 -0500662 ext4_lblk_t end;
663 int err = 0;
664
665 trace_ext4_es_remove_extent(inode, lblk, len);
666 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
667 lblk, len, inode->i_ino);
668
Eryu Guand4381472013-02-22 15:27:47 -0500669 if (!len)
670 return err;
671
Zheng Liu06b0c882013-02-18 00:26:51 -0500672 end = lblk + len - 1;
673 BUG_ON(end < lblk);
674
Zheng Liu06b0c882013-02-18 00:26:51 -0500675 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500676 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500677 write_unlock(&EXT4_I(inode)->i_es_lock);
678 ext4_es_print_tree(inode);
679 return err;
680}
Zheng Liu74cd15c2013-02-18 00:32:55 -0500681
682static int ext4_es_shrink(struct shrinker *shrink, struct shrink_control *sc)
683{
684 struct ext4_sb_info *sbi = container_of(shrink,
685 struct ext4_sb_info, s_es_shrinker);
686 struct ext4_inode_info *ei;
687 struct list_head *cur, *tmp, scanned;
688 int nr_to_scan = sc->nr_to_scan;
689 int ret, nr_shrunk = 0;
690
Theodore Ts'o1ac64662013-03-02 10:27:46 -0500691 ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500692 trace_ext4_es_shrink_enter(sbi->s_sb, nr_to_scan, ret);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500693
694 if (!nr_to_scan)
Theodore Ts'o24630772013-02-28 23:58:56 -0500695 return ret;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500696
697 INIT_LIST_HEAD(&scanned);
698
699 spin_lock(&sbi->s_es_lru_lock);
700 list_for_each_safe(cur, tmp, &sbi->s_es_lru) {
701 list_move_tail(cur, &scanned);
702
703 ei = list_entry(cur, struct ext4_inode_info, i_es_lru);
704
705 read_lock(&ei->i_es_lock);
706 if (ei->i_es_lru_nr == 0) {
707 read_unlock(&ei->i_es_lock);
708 continue;
709 }
710 read_unlock(&ei->i_es_lock);
711
712 write_lock(&ei->i_es_lock);
713 ret = __es_try_to_reclaim_extents(ei, nr_to_scan);
714 write_unlock(&ei->i_es_lock);
715
716 nr_shrunk += ret;
717 nr_to_scan -= ret;
718 if (nr_to_scan == 0)
719 break;
720 }
721 list_splice_tail(&scanned, &sbi->s_es_lru);
722 spin_unlock(&sbi->s_es_lru_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500723
Theodore Ts'o1ac64662013-03-02 10:27:46 -0500724 ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500725 trace_ext4_es_shrink_exit(sbi->s_sb, nr_shrunk, ret);
726 return ret;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500727}
728
729void ext4_es_register_shrinker(struct super_block *sb)
730{
731 struct ext4_sb_info *sbi;
732
733 sbi = EXT4_SB(sb);
734 INIT_LIST_HEAD(&sbi->s_es_lru);
735 spin_lock_init(&sbi->s_es_lru_lock);
736 sbi->s_es_shrinker.shrink = ext4_es_shrink;
737 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
738 register_shrinker(&sbi->s_es_shrinker);
739}
740
741void ext4_es_unregister_shrinker(struct super_block *sb)
742{
743 unregister_shrinker(&EXT4_SB(sb)->s_es_shrinker);
744}
745
746void ext4_es_lru_add(struct inode *inode)
747{
748 struct ext4_inode_info *ei = EXT4_I(inode);
749 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
750
751 spin_lock(&sbi->s_es_lru_lock);
752 if (list_empty(&ei->i_es_lru))
753 list_add_tail(&ei->i_es_lru, &sbi->s_es_lru);
754 else
755 list_move_tail(&ei->i_es_lru, &sbi->s_es_lru);
756 spin_unlock(&sbi->s_es_lru_lock);
757}
758
759void ext4_es_lru_del(struct inode *inode)
760{
761 struct ext4_inode_info *ei = EXT4_I(inode);
762 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
763
764 spin_lock(&sbi->s_es_lru_lock);
765 if (!list_empty(&ei->i_es_lru))
766 list_del_init(&ei->i_es_lru);
767 spin_unlock(&sbi->s_es_lru_lock);
768}
769
Zheng Liu74cd15c2013-02-18 00:32:55 -0500770static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
771 int nr_to_scan)
772{
773 struct inode *inode = &ei->vfs_inode;
774 struct ext4_es_tree *tree = &ei->i_es_tree;
775 struct rb_node *node;
776 struct extent_status *es;
777 int nr_shrunk = 0;
778
779 if (ei->i_es_lru_nr == 0)
780 return 0;
781
782 node = rb_first(&tree->root);
783 while (node != NULL) {
784 es = rb_entry(node, struct extent_status, rb_node);
785 node = rb_next(&es->rb_node);
786 /*
787 * We can't reclaim delayed extent from status tree because
788 * fiemap, bigallic, and seek_data/hole need to use it.
789 */
790 if (!ext4_es_is_delayed(es)) {
791 rb_erase(&es->rb_node, &tree->root);
792 ext4_es_free_extent(inode, es);
793 nr_shrunk++;
794 if (--nr_to_scan == 0)
795 break;
796 }
797 }
798 tree->cache_es = NULL;
799 return nr_shrunk;
800}