blob: bdd400c81533654b00fedc521829e49bcadc868c [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>
Zheng Liud3922a72013-07-01 08:12:37 -040013#include <linux/list_sort.h>
Zheng Liu654598b2012-11-08 21:57:20 -050014#include "ext4.h"
15#include "extents_status.h"
Zheng Liu654598b2012-11-08 21:57:20 -050016
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);
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400150static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
151 struct ext4_inode_info *locked_ei);
Zheng Liu06b0c882013-02-18 00:26:51 -0500152
Zheng Liu654598b2012-11-08 21:57:20 -0500153int __init ext4_init_es(void)
154{
Theodore Ts'o24630772013-02-28 23:58:56 -0500155 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
156 sizeof(struct extent_status),
157 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500158 if (ext4_es_cachep == NULL)
159 return -ENOMEM;
160 return 0;
161}
162
163void ext4_exit_es(void)
164{
165 if (ext4_es_cachep)
166 kmem_cache_destroy(ext4_es_cachep);
167}
168
169void ext4_es_init_tree(struct ext4_es_tree *tree)
170{
171 tree->root = RB_ROOT;
172 tree->cache_es = NULL;
173}
174
175#ifdef ES_DEBUG__
176static void ext4_es_print_tree(struct inode *inode)
177{
178 struct ext4_es_tree *tree;
179 struct rb_node *node;
180
181 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
182 tree = &EXT4_I(inode)->i_es_tree;
183 node = rb_first(&tree->root);
184 while (node) {
185 struct extent_status *es;
186 es = rb_entry(node, struct extent_status, rb_node);
Eric Whitneyce140cd2014-02-20 16:09:12 -0500187 printk(KERN_DEBUG " [%u/%u) %llu %x",
Zheng Liufdc02122013-02-18 00:26:51 -0500188 es->es_lblk, es->es_len,
189 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500190 node = rb_next(node);
191 }
192 printk(KERN_DEBUG "\n");
193}
194#else
195#define ext4_es_print_tree(inode)
196#endif
197
Zheng Liu06b0c882013-02-18 00:26:51 -0500198static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500199{
Zheng Liu06b0c882013-02-18 00:26:51 -0500200 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
201 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500202}
203
204/*
205 * search through the tree for an delayed extent with a given offset. If
206 * it can't be found, try to find next extent.
207 */
208static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500209 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500210{
211 struct rb_node *node = root->rb_node;
212 struct extent_status *es = NULL;
213
214 while (node) {
215 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500216 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500217 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500218 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500219 node = node->rb_right;
220 else
221 return es;
222 }
223
Zheng Liu06b0c882013-02-18 00:26:51 -0500224 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500225 return es;
226
Zheng Liu06b0c882013-02-18 00:26:51 -0500227 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500228 node = rb_next(&es->rb_node);
229 return node ? rb_entry(node, struct extent_status, rb_node) :
230 NULL;
231 }
232
233 return NULL;
234}
235
236/*
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400237 * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
238 * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500239 *
240 * @inode: the inode which owns delayed extents
Zheng Liube401362013-02-18 00:27:26 -0500241 * @lblk: the offset where we start to search
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400242 * @end: the offset where we stop to search
Zheng Liu654598b2012-11-08 21:57:20 -0500243 * @es: delayed extent that we found
Zheng Liu654598b2012-11-08 21:57:20 -0500244 */
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400245void ext4_es_find_delayed_extent_range(struct inode *inode,
246 ext4_lblk_t lblk, ext4_lblk_t end,
Zheng Liube401362013-02-18 00:27:26 -0500247 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500248{
249 struct ext4_es_tree *tree = NULL;
250 struct extent_status *es1 = NULL;
251 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500252
Zheng Liube401362013-02-18 00:27:26 -0500253 BUG_ON(es == NULL);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400254 BUG_ON(end < lblk);
255 trace_ext4_es_find_delayed_extent_range_enter(inode, lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500256
Zheng Liu654598b2012-11-08 21:57:20 -0500257 read_lock(&EXT4_I(inode)->i_es_lock);
258 tree = &EXT4_I(inode)->i_es_tree;
259
Zheng Liufdc02122013-02-18 00:26:51 -0500260 /* find extent in cache firstly */
Zheng Liube401362013-02-18 00:27:26 -0500261 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500262 if (tree->cache_es) {
263 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500264 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400265 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500266 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500267 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500268 goto out;
269 }
270 }
271
Zheng Liube401362013-02-18 00:27:26 -0500272 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500273
274out:
Zheng Liube401362013-02-18 00:27:26 -0500275 if (es1 && !ext4_es_is_delayed(es1)) {
276 while ((node = rb_next(&es1->rb_node)) != NULL) {
277 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400278 if (es1->es_lblk > end) {
279 es1 = NULL;
280 break;
281 }
Zheng Liube401362013-02-18 00:27:26 -0500282 if (ext4_es_is_delayed(es1))
283 break;
284 }
285 }
286
287 if (es1 && ext4_es_is_delayed(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500288 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500289 es->es_lblk = es1->es_lblk;
290 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500291 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500292 }
293
294 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500295
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400296 trace_ext4_es_find_delayed_extent_range_exit(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500297}
298
299static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500300ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
301 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500302{
303 struct extent_status *es;
304 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
305 if (es == NULL)
306 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500307 es->es_lblk = lblk;
308 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500309 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500310
311 /*
312 * We don't count delayed extent because we never try to reclaim them
313 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500314 if (!ext4_es_is_delayed(es)) {
Zheng Liu74cd15c2013-02-18 00:32:55 -0500315 EXT4_I(inode)->i_es_lru_nr++;
Theodore Ts'o1ac64662013-03-02 10:27:46 -0500316 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500317 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500318
Zheng Liu654598b2012-11-08 21:57:20 -0500319 return es;
320}
321
Zheng Liubdedbb72013-02-18 00:32:02 -0500322static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500323{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500324 /* Decrease the lru counter when this es is not delayed */
325 if (!ext4_es_is_delayed(es)) {
326 BUG_ON(EXT4_I(inode)->i_es_lru_nr == 0);
327 EXT4_I(inode)->i_es_lru_nr--;
Theodore Ts'o1ac64662013-03-02 10:27:46 -0500328 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500329 }
330
Zheng Liu654598b2012-11-08 21:57:20 -0500331 kmem_cache_free(ext4_es_cachep, es);
332}
333
Zheng Liu06b0c882013-02-18 00:26:51 -0500334/*
335 * Check whether or not two extents can be merged
336 * Condition:
337 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500338 * - physical block number is contiguous
339 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500340 */
341static int ext4_es_can_be_merged(struct extent_status *es1,
342 struct extent_status *es2)
343{
Zheng Liufdc02122013-02-18 00:26:51 -0500344 if (ext4_es_status(es1) != ext4_es_status(es2))
345 return 0;
346
Lukas Czerner0baaea62014-05-12 22:21:43 -0400347 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
348 pr_warn("ES assertion failed when merging extents. "
349 "The sum of lengths of es1 (%d) and es2 (%d) "
350 "is bigger than allowed file size (%d)\n",
351 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
352 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500353 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400354 }
Zheng Liufdc02122013-02-18 00:26:51 -0500355
Zheng Liubd384362013-03-10 20:48:59 -0400356 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
357 return 0;
358
359 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
360 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
361 return 1;
362
363 if (ext4_es_is_hole(es1))
364 return 1;
365
366 /* we need to check delayed extent is without unwritten status */
367 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
368 return 1;
369
370 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500371}
372
Zheng Liu654598b2012-11-08 21:57:20 -0500373static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500374ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500375{
Zheng Liubdedbb72013-02-18 00:32:02 -0500376 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500377 struct extent_status *es1;
378 struct rb_node *node;
379
380 node = rb_prev(&es->rb_node);
381 if (!node)
382 return es;
383
384 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500385 if (ext4_es_can_be_merged(es1, es)) {
386 es1->es_len += es->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500387 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500388 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500389 es = es1;
390 }
391
392 return es;
393}
394
395static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500396ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500397{
Zheng Liubdedbb72013-02-18 00:32:02 -0500398 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500399 struct extent_status *es1;
400 struct rb_node *node;
401
402 node = rb_next(&es->rb_node);
403 if (!node)
404 return es;
405
406 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500407 if (ext4_es_can_be_merged(es, es1)) {
408 es->es_len += es1->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500409 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500410 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500411 }
412
413 return es;
414}
415
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400416#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400417#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
418
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400419static void ext4_es_insert_extent_ext_check(struct inode *inode,
420 struct extent_status *es)
421{
422 struct ext4_ext_path *path = NULL;
423 struct ext4_extent *ex;
424 ext4_lblk_t ee_block;
425 ext4_fsblk_t ee_start;
426 unsigned short ee_len;
427 int depth, ee_status, es_status;
428
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400429 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400430 if (IS_ERR(path))
431 return;
432
433 depth = ext_depth(inode);
434 ex = path[depth].p_ext;
435
436 if (ex) {
437
438 ee_block = le32_to_cpu(ex->ee_block);
439 ee_start = ext4_ext_pblock(ex);
440 ee_len = ext4_ext_get_actual_len(ex);
441
Lukas Czerner556615d2014-04-20 23:45:47 -0400442 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400443 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
444
445 /*
446 * Make sure ex and es are not overlap when we try to insert
447 * a delayed/hole extent.
448 */
449 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
450 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400451 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400452 "inode: %lu we can find an extent "
453 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500454 "want to add a delayed/hole extent "
455 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400456 inode->i_ino, ee_block, ee_len,
457 ee_start, ee_status ? 'u' : 'w',
458 es->es_lblk, es->es_len,
459 ext4_es_pblock(es), ext4_es_status(es));
460 }
461 goto out;
462 }
463
464 /*
465 * We don't check ee_block == es->es_lblk, etc. because es
466 * might be a part of whole extent, vice versa.
467 */
468 if (es->es_lblk < ee_block ||
469 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400470 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400471 "ex_status [%d/%d/%llu/%c] != "
472 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
473 ee_block, ee_len, ee_start,
474 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
475 ext4_es_pblock(es), es_status ? 'u' : 'w');
476 goto out;
477 }
478
479 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400480 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400481 "ex_status [%d/%d/%llu/%c] != "
482 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
483 ee_block, ee_len, ee_start,
484 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
485 ext4_es_pblock(es), es_status ? 'u' : 'w');
486 }
487 } else {
488 /*
489 * We can't find an extent on disk. So we need to make sure
490 * that we don't want to add an written/unwritten extent.
491 */
492 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400493 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400494 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500495 "to add a written/unwritten extent "
496 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400497 es->es_lblk, es->es_lblk, es->es_len,
498 ext4_es_pblock(es), ext4_es_status(es));
499 }
500 }
501out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400502 ext4_ext_drop_refs(path);
503 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400504}
505
506static void ext4_es_insert_extent_ind_check(struct inode *inode,
507 struct extent_status *es)
508{
509 struct ext4_map_blocks map;
510 int retval;
511
512 /*
513 * Here we call ext4_ind_map_blocks to lookup a block mapping because
514 * 'Indirect' structure is defined in indirect.c. So we couldn't
515 * access direct/indirect tree from outside. It is too dirty to define
516 * this function in indirect.c file.
517 */
518
519 map.m_lblk = es->es_lblk;
520 map.m_len = es->es_len;
521
522 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
523 if (retval > 0) {
524 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
525 /*
526 * We want to add a delayed/hole extent but this
527 * block has been allocated.
528 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400529 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400530 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500531 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400532 inode->i_ino, es->es_lblk, es->es_len,
533 ext4_es_pblock(es), ext4_es_status(es));
534 return;
535 } else if (ext4_es_is_written(es)) {
536 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400537 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400538 "inode: %lu retval %d != es_len %d\n",
539 inode->i_ino, retval, es->es_len);
540 return;
541 }
542 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400543 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400544 "inode: %lu m_pblk %llu != "
545 "es_pblk %llu\n",
546 inode->i_ino, map.m_pblk,
547 ext4_es_pblock(es));
548 return;
549 }
550 } else {
551 /*
552 * We don't need to check unwritten extent because
553 * indirect-based file doesn't have it.
554 */
555 BUG_ON(1);
556 }
557 } else if (retval == 0) {
558 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400559 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400560 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500561 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400562 inode->i_ino, es->es_lblk, es->es_len,
563 ext4_es_pblock(es), ext4_es_status(es));
564 return;
565 }
566 }
567}
568
569static inline void ext4_es_insert_extent_check(struct inode *inode,
570 struct extent_status *es)
571{
572 /*
573 * We don't need to worry about the race condition because
574 * caller takes i_data_sem locking.
575 */
576 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
577 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
578 ext4_es_insert_extent_ext_check(inode, es);
579 else
580 ext4_es_insert_extent_ind_check(inode, es);
581}
582#else
583static inline void ext4_es_insert_extent_check(struct inode *inode,
584 struct extent_status *es)
585{
586}
587#endif
588
Zheng Liubdedbb72013-02-18 00:32:02 -0500589static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500590{
Zheng Liubdedbb72013-02-18 00:32:02 -0500591 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500592 struct rb_node **p = &tree->root.rb_node;
593 struct rb_node *parent = NULL;
594 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500595
596 while (*p) {
597 parent = *p;
598 es = rb_entry(parent, struct extent_status, rb_node);
599
Zheng Liu06b0c882013-02-18 00:26:51 -0500600 if (newes->es_lblk < es->es_lblk) {
601 if (ext4_es_can_be_merged(newes, es)) {
602 /*
603 * Here we can modify es_lblk directly
604 * because it isn't overlapped.
605 */
606 es->es_lblk = newes->es_lblk;
607 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500608 if (ext4_es_is_written(es) ||
609 ext4_es_is_unwritten(es))
610 ext4_es_store_pblock(es,
611 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500612 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500613 goto out;
614 }
615 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500616 } else if (newes->es_lblk > ext4_es_end(es)) {
617 if (ext4_es_can_be_merged(es, newes)) {
618 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500619 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500620 goto out;
621 }
622 p = &(*p)->rb_right;
623 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500624 BUG_ON(1);
625 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500626 }
627 }
628
Zheng Liubdedbb72013-02-18 00:32:02 -0500629 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500630 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500631 if (!es)
632 return -ENOMEM;
633 rb_link_node(&es->rb_node, parent, p);
634 rb_insert_color(&es->rb_node, &tree->root);
635
636out:
637 tree->cache_es = es;
638 return 0;
639}
640
641/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400642 * ext4_es_insert_extent() adds information to an inode's extent
643 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500644 *
645 * Return 0 on success, error code on failure.
646 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500647int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500648 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400649 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500650{
Zheng Liu06b0c882013-02-18 00:26:51 -0500651 struct extent_status newes;
652 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500653 int err = 0;
654
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400655 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500656 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500657
Eryu Guand4381472013-02-22 15:27:47 -0500658 if (!len)
659 return 0;
660
Zheng Liu06b0c882013-02-18 00:26:51 -0500661 BUG_ON(end < lblk);
662
663 newes.es_lblk = lblk;
664 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500665 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500666 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500667
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400668 ext4_es_insert_extent_check(inode, &newes);
669
Zheng Liu654598b2012-11-08 21:57:20 -0500670 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500671 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500672 if (err != 0)
673 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400674retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500675 err = __es_insert_extent(inode, &newes);
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400676 if (err == -ENOMEM && __ext4_es_shrink(EXT4_SB(inode->i_sb), 1,
677 EXT4_I(inode)))
678 goto retry;
679 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
680 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500681
682error:
Zheng Liu654598b2012-11-08 21:57:20 -0500683 write_unlock(&EXT4_I(inode)->i_es_lock);
684
685 ext4_es_print_tree(inode);
686
687 return err;
688}
689
Zheng Liud100eef2013-02-18 00:29:59 -0500690/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400691 * ext4_es_cache_extent() inserts information into the extent status
692 * tree if and only if there isn't information about the range in
693 * question already.
694 */
695void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
696 ext4_lblk_t len, ext4_fsblk_t pblk,
697 unsigned int status)
698{
699 struct extent_status *es;
700 struct extent_status newes;
701 ext4_lblk_t end = lblk + len - 1;
702
703 newes.es_lblk = lblk;
704 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500705 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400706 trace_ext4_es_cache_extent(inode, &newes);
707
708 if (!len)
709 return;
710
711 BUG_ON(end < lblk);
712
713 write_lock(&EXT4_I(inode)->i_es_lock);
714
715 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400716 if (!es || es->es_lblk > end)
717 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400718 write_unlock(&EXT4_I(inode)->i_es_lock);
719}
720
721/*
Zheng Liud100eef2013-02-18 00:29:59 -0500722 * 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 */
728int 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
767out:
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
Zheng Liubdedbb72013-02-18 00:32:02 -0500781static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
782 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500783{
Zheng Liubdedbb72013-02-18 00:32:02 -0500784 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500785 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500786 struct extent_status *es;
787 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500788 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500789 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400790 int err;
Zheng Liu654598b2012-11-08 21:57:20 -0500791
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400792retry:
793 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500794 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500795 if (!es)
796 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500797 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500798 goto out;
799
800 /* Simply invalidate cache_es. */
801 tree->cache_es = NULL;
802
Zheng Liu06b0c882013-02-18 00:26:51 -0500803 orig_es.es_lblk = es->es_lblk;
804 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500805 orig_es.es_pblk = es->es_pblk;
806
Zheng Liu06b0c882013-02-18 00:26:51 -0500807 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
808 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500809 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500810 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500811 if (len2 > 0) {
812 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500813 struct extent_status newes;
814
815 newes.es_lblk = end + 1;
816 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -0400817 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -0500818 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500819 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -0500820 block = ext4_es_pblock(&orig_es) +
821 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500822 ext4_es_store_pblock_status(&newes, block,
823 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500824 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500825 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500826 es->es_lblk = orig_es.es_lblk;
827 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400828 if ((err == -ENOMEM) &&
829 __ext4_es_shrink(EXT4_SB(inode->i_sb), 1,
830 EXT4_I(inode)))
831 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -0500832 goto out;
833 }
834 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500835 es->es_lblk = end + 1;
836 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500837 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 }
Zheng Liu654598b2012-11-08 21:57:20 -0500842 }
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
Zheng Liu06b0c882013-02-18 00:26:51 -0500854 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500855 node = rb_next(&es->rb_node);
856 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500857 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500858 if (!node) {
859 es = NULL;
860 break;
861 }
862 es = rb_entry(node, struct extent_status, rb_node);
863 }
864
Zheng Liu06b0c882013-02-18 00:26:51 -0500865 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -0500866 ext4_lblk_t orig_len = es->es_len;
867
Zheng Liu06b0c882013-02-18 00:26:51 -0500868 len1 = ext4_es_end(es) - end;
869 es->es_lblk = end + 1;
870 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -0500871 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 }
Zheng Liu654598b2012-11-08 21:57:20 -0500875 }
876
877out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500878 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 */
886int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
887 ext4_lblk_t len)
888{
Zheng Liu06b0c882013-02-18 00:26:51 -0500889 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
Eryu Guand4381472013-02-22 15:27:47 -0500896 if (!len)
897 return err;
898
Zheng Liu06b0c882013-02-18 00:26:51 -0500899 end = lblk + len - 1;
900 BUG_ON(end < lblk);
901
Zheng Liu06b0c882013-02-18 00:26:51 -0500902 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500903 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500904 write_unlock(&EXT4_I(inode)->i_es_lock);
905 ext4_es_print_tree(inode);
906 return err;
907}
Zheng Liu74cd15c2013-02-18 00:32:55 -0500908
Zheng Liud3922a72013-07-01 08:12:37 -0400909static int ext4_inode_touch_time_cmp(void *priv, struct list_head *a,
910 struct list_head *b)
911{
912 struct ext4_inode_info *eia, *eib;
913 eia = list_entry(a, struct ext4_inode_info, i_es_lru);
914 eib = list_entry(b, struct ext4_inode_info, i_es_lru);
915
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400916 if (ext4_test_inode_state(&eia->vfs_inode, EXT4_STATE_EXT_PRECACHED) &&
917 !ext4_test_inode_state(&eib->vfs_inode, EXT4_STATE_EXT_PRECACHED))
918 return 1;
919 if (!ext4_test_inode_state(&eia->vfs_inode, EXT4_STATE_EXT_PRECACHED) &&
920 ext4_test_inode_state(&eib->vfs_inode, EXT4_STATE_EXT_PRECACHED))
921 return -1;
Zheng Liud3922a72013-07-01 08:12:37 -0400922 if (eia->i_touch_when == eib->i_touch_when)
923 return 0;
924 if (time_after(eia->i_touch_when, eib->i_touch_when))
925 return 1;
926 else
927 return -1;
928}
929
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400930static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
931 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -0500932{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500933 struct ext4_inode_info *ei;
Zheng Liud3922a72013-07-01 08:12:37 -0400934 struct list_head *cur, *tmp;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400935 LIST_HEAD(skipped);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000936 int nr_shrunk = 0;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400937 int retried = 0, skip_precached = 1, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500938
Zheng Liu74cd15c2013-02-18 00:32:55 -0500939 spin_lock(&sbi->s_es_lru_lock);
Zheng Liud3922a72013-07-01 08:12:37 -0400940
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400941retry:
Zheng Liu74cd15c2013-02-18 00:32:55 -0500942 list_for_each_safe(cur, tmp, &sbi->s_es_lru) {
Dave Chinner1ab6c492013-08-28 10:18:09 +1000943 int shrunk;
944
Zheng Liud3922a72013-07-01 08:12:37 -0400945 /*
946 * If we have already reclaimed all extents from extent
947 * status tree, just stop the loop immediately.
948 */
949 if (percpu_counter_read_positive(&sbi->s_extent_cache_cnt) == 0)
950 break;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500951
952 ei = list_entry(cur, struct ext4_inode_info, i_es_lru);
953
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400954 /*
955 * Skip the inode that is newer than the last_sorted
956 * time. Normally we try hard to avoid shrinking
957 * precached inodes, but we will as a last resort.
958 */
959 if ((sbi->s_es_last_sorted < ei->i_touch_when) ||
960 (skip_precached && ext4_test_inode_state(&ei->vfs_inode,
961 EXT4_STATE_EXT_PRECACHED))) {
962 nr_skipped++;
963 list_move_tail(cur, &skipped);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500964 continue;
965 }
Zheng Liud3922a72013-07-01 08:12:37 -0400966
Theodore Ts'o3f1f9b82014-07-12 15:32:24 -0400967 if (ei->i_es_lru_nr == 0 || ei == locked_ei ||
968 !write_trylock(&ei->i_es_lock))
Zheng Liud3922a72013-07-01 08:12:37 -0400969 continue;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500970
Dave Chinner1ab6c492013-08-28 10:18:09 +1000971 shrunk = __es_try_to_reclaim_extents(ei, nr_to_scan);
Zheng Liud3922a72013-07-01 08:12:37 -0400972 if (ei->i_es_lru_nr == 0)
973 list_del_init(&ei->i_es_lru);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500974 write_unlock(&ei->i_es_lock);
975
Dave Chinner1ab6c492013-08-28 10:18:09 +1000976 nr_shrunk += shrunk;
977 nr_to_scan -= shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500978 if (nr_to_scan == 0)
979 break;
980 }
Zheng Liud3922a72013-07-01 08:12:37 -0400981
982 /* Move the newer inodes into the tail of the LRU list. */
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400983 list_splice_tail(&skipped, &sbi->s_es_lru);
984 INIT_LIST_HEAD(&skipped);
985
986 /*
987 * If we skipped any inodes, and we weren't able to make any
988 * forward progress, sort the list and try again.
989 */
990 if ((nr_shrunk == 0) && nr_skipped && !retried) {
991 retried++;
992 list_sort(NULL, &sbi->s_es_lru, ext4_inode_touch_time_cmp);
993 sbi->s_es_last_sorted = jiffies;
994 ei = list_first_entry(&sbi->s_es_lru, struct ext4_inode_info,
995 i_es_lru);
996 /*
997 * If there are no non-precached inodes left on the
998 * list, start releasing precached extents.
999 */
1000 if (ext4_test_inode_state(&ei->vfs_inode,
1001 EXT4_STATE_EXT_PRECACHED))
1002 skip_precached = 0;
1003 goto retry;
1004 }
1005
Zheng Liu74cd15c2013-02-18 00:32:55 -05001006 spin_unlock(&sbi->s_es_lru_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001007
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001008 if (locked_ei && nr_shrunk == 0)
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001009 nr_shrunk = __es_try_to_reclaim_extents(locked_ei, nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001010
1011 return nr_shrunk;
1012}
1013
Dave Chinner1ab6c492013-08-28 10:18:09 +10001014static unsigned long ext4_es_count(struct shrinker *shrink,
1015 struct shrink_control *sc)
1016{
1017 unsigned long nr;
1018 struct ext4_sb_info *sbi;
1019
1020 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
1021 nr = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
1022 trace_ext4_es_shrink_enter(sbi->s_sb, sc->nr_to_scan, nr);
1023 return nr;
1024}
1025
1026static unsigned long ext4_es_scan(struct shrinker *shrink,
1027 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001028{
1029 struct ext4_sb_info *sbi = container_of(shrink,
1030 struct ext4_sb_info, s_es_shrinker);
1031 int nr_to_scan = sc->nr_to_scan;
1032 int ret, nr_shrunk;
1033
1034 ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
1035 trace_ext4_es_shrink_enter(sbi->s_sb, nr_to_scan, ret);
1036
1037 if (!nr_to_scan)
1038 return ret;
1039
1040 nr_shrunk = __ext4_es_shrink(sbi, nr_to_scan, NULL);
1041
Theodore Ts'o24630772013-02-28 23:58:56 -05001042 trace_ext4_es_shrink_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001043 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001044}
1045
Zheng Liud3922a72013-07-01 08:12:37 -04001046void ext4_es_register_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001047{
Zheng Liu74cd15c2013-02-18 00:32:55 -05001048 INIT_LIST_HEAD(&sbi->s_es_lru);
1049 spin_lock_init(&sbi->s_es_lru_lock);
Zheng Liud3922a72013-07-01 08:12:37 -04001050 sbi->s_es_last_sorted = 0;
Dave Chinner1ab6c492013-08-28 10:18:09 +10001051 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1052 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001053 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
1054 register_shrinker(&sbi->s_es_shrinker);
1055}
1056
Zheng Liud3922a72013-07-01 08:12:37 -04001057void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001058{
Zheng Liud3922a72013-07-01 08:12:37 -04001059 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001060}
1061
1062void ext4_es_lru_add(struct inode *inode)
1063{
1064 struct ext4_inode_info *ei = EXT4_I(inode);
1065 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1066
Zheng Liud3922a72013-07-01 08:12:37 -04001067 ei->i_touch_when = jiffies;
1068
1069 if (!list_empty(&ei->i_es_lru))
1070 return;
1071
Zheng Liu74cd15c2013-02-18 00:32:55 -05001072 spin_lock(&sbi->s_es_lru_lock);
1073 if (list_empty(&ei->i_es_lru))
1074 list_add_tail(&ei->i_es_lru, &sbi->s_es_lru);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001075 spin_unlock(&sbi->s_es_lru_lock);
1076}
1077
1078void ext4_es_lru_del(struct inode *inode)
1079{
1080 struct ext4_inode_info *ei = EXT4_I(inode);
1081 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1082
1083 spin_lock(&sbi->s_es_lru_lock);
1084 if (!list_empty(&ei->i_es_lru))
1085 list_del_init(&ei->i_es_lru);
1086 spin_unlock(&sbi->s_es_lru_lock);
1087}
1088
Zheng Liu74cd15c2013-02-18 00:32:55 -05001089static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
1090 int nr_to_scan)
1091{
1092 struct inode *inode = &ei->vfs_inode;
1093 struct ext4_es_tree *tree = &ei->i_es_tree;
1094 struct rb_node *node;
1095 struct extent_status *es;
Dave Chinner1ab6c492013-08-28 10:18:09 +10001096 unsigned long nr_shrunk = 0;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001097 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1098 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001099
1100 if (ei->i_es_lru_nr == 0)
1101 return 0;
1102
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001103 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1104 __ratelimit(&_rs))
1105 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1106
Zheng Liu74cd15c2013-02-18 00:32:55 -05001107 node = rb_first(&tree->root);
1108 while (node != NULL) {
1109 es = rb_entry(node, struct extent_status, rb_node);
1110 node = rb_next(&es->rb_node);
1111 /*
1112 * We can't reclaim delayed extent from status tree because
1113 * fiemap, bigallic, and seek_data/hole need to use it.
1114 */
1115 if (!ext4_es_is_delayed(es)) {
1116 rb_erase(&es->rb_node, &tree->root);
1117 ext4_es_free_extent(inode, es);
1118 nr_shrunk++;
1119 if (--nr_to_scan == 0)
1120 break;
1121 }
1122 }
1123 tree->cache_es = NULL;
1124 return nr_shrunk;
1125}