blob: ac748b3af1c1ca2f242ce99b3a260850ae150fc9 [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 */
Zheng Liud3922a72013-07-01 08:12:37 -040012#include <linux/list_sort.h>
Zheng Liueb68d0e2014-09-01 22:26:49 -040013#include <linux/proc_fs.h>
14#include <linux/seq_file.h>
Zheng Liu654598b2012-11-08 21:57:20 -050015#include "ext4.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);
Jan Karadd475922014-11-25 11:51:23 -0500148static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500149static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
150 struct ext4_inode_info *locked_ei);
Zheng Liu06b0c882013-02-18 00:26:51 -0500151
Zheng Liu654598b2012-11-08 21:57:20 -0500152int __init ext4_init_es(void)
153{
Theodore Ts'o24630772013-02-28 23:58:56 -0500154 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
155 sizeof(struct extent_status),
156 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500157 if (ext4_es_cachep == NULL)
158 return -ENOMEM;
159 return 0;
160}
161
162void ext4_exit_es(void)
163{
164 if (ext4_es_cachep)
165 kmem_cache_destroy(ext4_es_cachep);
166}
167
168void ext4_es_init_tree(struct ext4_es_tree *tree)
169{
170 tree->root = RB_ROOT;
171 tree->cache_es = NULL;
172}
173
174#ifdef ES_DEBUG__
175static void ext4_es_print_tree(struct inode *inode)
176{
177 struct ext4_es_tree *tree;
178 struct rb_node *node;
179
180 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
181 tree = &EXT4_I(inode)->i_es_tree;
182 node = rb_first(&tree->root);
183 while (node) {
184 struct extent_status *es;
185 es = rb_entry(node, struct extent_status, rb_node);
Eric Whitneyce140cd2014-02-20 16:09:12 -0500186 printk(KERN_DEBUG " [%u/%u) %llu %x",
Zheng Liufdc02122013-02-18 00:26:51 -0500187 es->es_lblk, es->es_len,
188 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500189 node = rb_next(node);
190 }
191 printk(KERN_DEBUG "\n");
192}
193#else
194#define ext4_es_print_tree(inode)
195#endif
196
Zheng Liu06b0c882013-02-18 00:26:51 -0500197static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500198{
Zheng Liu06b0c882013-02-18 00:26:51 -0500199 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
200 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500201}
202
203/*
204 * search through the tree for an delayed extent with a given offset. If
205 * it can't be found, try to find next extent.
206 */
207static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500208 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500209{
210 struct rb_node *node = root->rb_node;
211 struct extent_status *es = NULL;
212
213 while (node) {
214 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500215 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500216 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500217 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500218 node = node->rb_right;
219 else
220 return es;
221 }
222
Zheng Liu06b0c882013-02-18 00:26:51 -0500223 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500224 return es;
225
Zheng Liu06b0c882013-02-18 00:26:51 -0500226 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500227 node = rb_next(&es->rb_node);
228 return node ? rb_entry(node, struct extent_status, rb_node) :
229 NULL;
230 }
231
232 return NULL;
233}
234
235/*
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400236 * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
237 * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500238 *
239 * @inode: the inode which owns delayed extents
Zheng Liube401362013-02-18 00:27:26 -0500240 * @lblk: the offset where we start to search
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400241 * @end: the offset where we stop to search
Zheng Liu654598b2012-11-08 21:57:20 -0500242 * @es: delayed extent that we found
Zheng Liu654598b2012-11-08 21:57:20 -0500243 */
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400244void ext4_es_find_delayed_extent_range(struct inode *inode,
245 ext4_lblk_t lblk, ext4_lblk_t end,
Zheng Liube401362013-02-18 00:27:26 -0500246 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500247{
248 struct ext4_es_tree *tree = NULL;
249 struct extent_status *es1 = NULL;
250 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500251
Zheng Liube401362013-02-18 00:27:26 -0500252 BUG_ON(es == NULL);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400253 BUG_ON(end < lblk);
254 trace_ext4_es_find_delayed_extent_range_enter(inode, lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500255
Zheng Liu654598b2012-11-08 21:57:20 -0500256 read_lock(&EXT4_I(inode)->i_es_lock);
257 tree = &EXT4_I(inode)->i_es_tree;
258
Zheng Liufdc02122013-02-18 00:26:51 -0500259 /* find extent in cache firstly */
Zheng Liube401362013-02-18 00:27:26 -0500260 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500261 if (tree->cache_es) {
262 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500263 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400264 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500265 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500266 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500267 goto out;
268 }
269 }
270
Zheng Liube401362013-02-18 00:27:26 -0500271 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500272
273out:
Zheng Liube401362013-02-18 00:27:26 -0500274 if (es1 && !ext4_es_is_delayed(es1)) {
275 while ((node = rb_next(&es1->rb_node)) != NULL) {
276 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400277 if (es1->es_lblk > end) {
278 es1 = NULL;
279 break;
280 }
Zheng Liube401362013-02-18 00:27:26 -0500281 if (ext4_es_is_delayed(es1))
282 break;
283 }
284 }
285
286 if (es1 && ext4_es_is_delayed(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500287 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500288 es->es_lblk = es1->es_lblk;
289 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500290 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500291 }
292
293 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500294
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400295 trace_ext4_es_find_delayed_extent_range_exit(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500296}
297
Jan Karab0dea4c2014-11-25 11:49:25 -0500298static void ext4_es_list_add(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500299{
300 struct ext4_inode_info *ei = EXT4_I(inode);
301 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
302
303 if (!list_empty(&ei->i_es_list))
304 return;
305
306 spin_lock(&sbi->s_es_lock);
307 if (list_empty(&ei->i_es_list)) {
308 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
309 sbi->s_es_nr_inode++;
310 }
311 spin_unlock(&sbi->s_es_lock);
312}
313
Jan Karab0dea4c2014-11-25 11:49:25 -0500314static void ext4_es_list_del(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500315{
316 struct ext4_inode_info *ei = EXT4_I(inode);
317 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
318
319 spin_lock(&sbi->s_es_lock);
320 if (!list_empty(&ei->i_es_list)) {
321 list_del_init(&ei->i_es_list);
322 sbi->s_es_nr_inode--;
323 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
324 }
325 spin_unlock(&sbi->s_es_lock);
326}
327
Zheng Liu654598b2012-11-08 21:57:20 -0500328static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500329ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
330 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500331{
332 struct extent_status *es;
333 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
334 if (es == NULL)
335 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500336 es->es_lblk = lblk;
337 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500338 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500339
340 /*
341 * We don't count delayed extent because we never try to reclaim them
342 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500343 if (!ext4_es_is_delayed(es)) {
Jan Karab0dea4c2014-11-25 11:49:25 -0500344 if (!EXT4_I(inode)->i_es_shk_nr++)
345 ext4_es_list_add(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400346 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500347 s_es_stats.es_stats_shk_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500348 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500349
Zheng Liueb68d0e2014-09-01 22:26:49 -0400350 EXT4_I(inode)->i_es_all_nr++;
351 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
352
Zheng Liu654598b2012-11-08 21:57:20 -0500353 return es;
354}
355
Zheng Liubdedbb72013-02-18 00:32:02 -0500356static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500357{
Zheng Liueb68d0e2014-09-01 22:26:49 -0400358 EXT4_I(inode)->i_es_all_nr--;
359 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
360
Zheng Liuedaa53c2014-11-25 11:45:37 -0500361 /* Decrease the shrink counter when this es is not delayed */
Zheng Liu74cd15c2013-02-18 00:32:55 -0500362 if (!ext4_es_is_delayed(es)) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500363 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
Jan Karab0dea4c2014-11-25 11:49:25 -0500364 if (!--EXT4_I(inode)->i_es_shk_nr)
365 ext4_es_list_del(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400366 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500367 s_es_stats.es_stats_shk_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500368 }
369
Zheng Liu654598b2012-11-08 21:57:20 -0500370 kmem_cache_free(ext4_es_cachep, es);
371}
372
Zheng Liu06b0c882013-02-18 00:26:51 -0500373/*
374 * Check whether or not two extents can be merged
375 * Condition:
376 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500377 * - physical block number is contiguous
378 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500379 */
380static int ext4_es_can_be_merged(struct extent_status *es1,
381 struct extent_status *es2)
382{
Jan Kara2be12de2014-11-25 11:55:24 -0500383 if (ext4_es_type(es1) != ext4_es_type(es2))
Zheng Liufdc02122013-02-18 00:26:51 -0500384 return 0;
385
Lukas Czerner0baaea62014-05-12 22:21:43 -0400386 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
387 pr_warn("ES assertion failed when merging extents. "
388 "The sum of lengths of es1 (%d) and es2 (%d) "
389 "is bigger than allowed file size (%d)\n",
390 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
391 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500392 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400393 }
Zheng Liufdc02122013-02-18 00:26:51 -0500394
Zheng Liubd384362013-03-10 20:48:59 -0400395 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
396 return 0;
397
398 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
399 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
400 return 1;
401
402 if (ext4_es_is_hole(es1))
403 return 1;
404
405 /* we need to check delayed extent is without unwritten status */
406 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
407 return 1;
408
409 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500410}
411
Zheng Liu654598b2012-11-08 21:57:20 -0500412static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500413ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500414{
Zheng Liubdedbb72013-02-18 00:32:02 -0500415 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500416 struct extent_status *es1;
417 struct rb_node *node;
418
419 node = rb_prev(&es->rb_node);
420 if (!node)
421 return es;
422
423 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500424 if (ext4_es_can_be_merged(es1, es)) {
425 es1->es_len += es->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500426 if (ext4_es_is_referenced(es))
427 ext4_es_set_referenced(es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500428 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500429 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500430 es = es1;
431 }
432
433 return es;
434}
435
436static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500437ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500438{
Zheng Liubdedbb72013-02-18 00:32:02 -0500439 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500440 struct extent_status *es1;
441 struct rb_node *node;
442
443 node = rb_next(&es->rb_node);
444 if (!node)
445 return es;
446
447 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500448 if (ext4_es_can_be_merged(es, es1)) {
449 es->es_len += es1->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500450 if (ext4_es_is_referenced(es1))
451 ext4_es_set_referenced(es);
Zheng Liu654598b2012-11-08 21:57:20 -0500452 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500453 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500454 }
455
456 return es;
457}
458
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400459#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400460#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
461
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400462static void ext4_es_insert_extent_ext_check(struct inode *inode,
463 struct extent_status *es)
464{
465 struct ext4_ext_path *path = NULL;
466 struct ext4_extent *ex;
467 ext4_lblk_t ee_block;
468 ext4_fsblk_t ee_start;
469 unsigned short ee_len;
470 int depth, ee_status, es_status;
471
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400472 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400473 if (IS_ERR(path))
474 return;
475
476 depth = ext_depth(inode);
477 ex = path[depth].p_ext;
478
479 if (ex) {
480
481 ee_block = le32_to_cpu(ex->ee_block);
482 ee_start = ext4_ext_pblock(ex);
483 ee_len = ext4_ext_get_actual_len(ex);
484
Lukas Czerner556615d2014-04-20 23:45:47 -0400485 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400486 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
487
488 /*
489 * Make sure ex and es are not overlap when we try to insert
490 * a delayed/hole extent.
491 */
492 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
493 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400494 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400495 "inode: %lu we can find an extent "
496 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500497 "want to add a delayed/hole extent "
498 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400499 inode->i_ino, ee_block, ee_len,
500 ee_start, ee_status ? 'u' : 'w',
501 es->es_lblk, es->es_len,
502 ext4_es_pblock(es), ext4_es_status(es));
503 }
504 goto out;
505 }
506
507 /*
508 * We don't check ee_block == es->es_lblk, etc. because es
509 * might be a part of whole extent, vice versa.
510 */
511 if (es->es_lblk < ee_block ||
512 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400513 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400514 "ex_status [%d/%d/%llu/%c] != "
515 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
516 ee_block, ee_len, ee_start,
517 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
518 ext4_es_pblock(es), es_status ? 'u' : 'w');
519 goto out;
520 }
521
522 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400523 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400524 "ex_status [%d/%d/%llu/%c] != "
525 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
526 ee_block, ee_len, ee_start,
527 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
528 ext4_es_pblock(es), es_status ? 'u' : 'w');
529 }
530 } else {
531 /*
532 * We can't find an extent on disk. So we need to make sure
533 * that we don't want to add an written/unwritten extent.
534 */
535 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400536 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400537 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500538 "to add a written/unwritten extent "
539 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400540 es->es_lblk, es->es_lblk, es->es_len,
541 ext4_es_pblock(es), ext4_es_status(es));
542 }
543 }
544out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400545 ext4_ext_drop_refs(path);
546 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400547}
548
549static void ext4_es_insert_extent_ind_check(struct inode *inode,
550 struct extent_status *es)
551{
552 struct ext4_map_blocks map;
553 int retval;
554
555 /*
556 * Here we call ext4_ind_map_blocks to lookup a block mapping because
557 * 'Indirect' structure is defined in indirect.c. So we couldn't
558 * access direct/indirect tree from outside. It is too dirty to define
559 * this function in indirect.c file.
560 */
561
562 map.m_lblk = es->es_lblk;
563 map.m_len = es->es_len;
564
565 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
566 if (retval > 0) {
567 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
568 /*
569 * We want to add a delayed/hole extent but this
570 * block has been allocated.
571 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400572 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400573 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500574 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400575 inode->i_ino, es->es_lblk, es->es_len,
576 ext4_es_pblock(es), ext4_es_status(es));
577 return;
578 } else if (ext4_es_is_written(es)) {
579 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400580 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400581 "inode: %lu retval %d != es_len %d\n",
582 inode->i_ino, retval, es->es_len);
583 return;
584 }
585 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400586 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400587 "inode: %lu m_pblk %llu != "
588 "es_pblk %llu\n",
589 inode->i_ino, map.m_pblk,
590 ext4_es_pblock(es));
591 return;
592 }
593 } else {
594 /*
595 * We don't need to check unwritten extent because
596 * indirect-based file doesn't have it.
597 */
598 BUG_ON(1);
599 }
600 } else if (retval == 0) {
601 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400602 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400603 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500604 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400605 inode->i_ino, es->es_lblk, es->es_len,
606 ext4_es_pblock(es), ext4_es_status(es));
607 return;
608 }
609 }
610}
611
612static inline void ext4_es_insert_extent_check(struct inode *inode,
613 struct extent_status *es)
614{
615 /*
616 * We don't need to worry about the race condition because
617 * caller takes i_data_sem locking.
618 */
619 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
620 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
621 ext4_es_insert_extent_ext_check(inode, es);
622 else
623 ext4_es_insert_extent_ind_check(inode, es);
624}
625#else
626static inline void ext4_es_insert_extent_check(struct inode *inode,
627 struct extent_status *es)
628{
629}
630#endif
631
Zheng Liubdedbb72013-02-18 00:32:02 -0500632static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500633{
Zheng Liubdedbb72013-02-18 00:32:02 -0500634 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500635 struct rb_node **p = &tree->root.rb_node;
636 struct rb_node *parent = NULL;
637 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500638
639 while (*p) {
640 parent = *p;
641 es = rb_entry(parent, struct extent_status, rb_node);
642
Zheng Liu06b0c882013-02-18 00:26:51 -0500643 if (newes->es_lblk < es->es_lblk) {
644 if (ext4_es_can_be_merged(newes, es)) {
645 /*
646 * Here we can modify es_lblk directly
647 * because it isn't overlapped.
648 */
649 es->es_lblk = newes->es_lblk;
650 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500651 if (ext4_es_is_written(es) ||
652 ext4_es_is_unwritten(es))
653 ext4_es_store_pblock(es,
654 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500655 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500656 goto out;
657 }
658 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500659 } else if (newes->es_lblk > ext4_es_end(es)) {
660 if (ext4_es_can_be_merged(es, newes)) {
661 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500662 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500663 goto out;
664 }
665 p = &(*p)->rb_right;
666 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500667 BUG_ON(1);
668 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500669 }
670 }
671
Zheng Liubdedbb72013-02-18 00:32:02 -0500672 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500673 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500674 if (!es)
675 return -ENOMEM;
676 rb_link_node(&es->rb_node, parent, p);
677 rb_insert_color(&es->rb_node, &tree->root);
678
679out:
680 tree->cache_es = es;
681 return 0;
682}
683
684/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400685 * ext4_es_insert_extent() adds information to an inode's extent
686 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500687 *
688 * Return 0 on success, error code on failure.
689 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500690int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500691 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400692 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500693{
Zheng Liu06b0c882013-02-18 00:26:51 -0500694 struct extent_status newes;
695 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500696 int err = 0;
697
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400698 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500699 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500700
Eryu Guand4381472013-02-22 15:27:47 -0500701 if (!len)
702 return 0;
703
Zheng Liu06b0c882013-02-18 00:26:51 -0500704 BUG_ON(end < lblk);
705
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400706 if ((status & EXTENT_STATUS_DELAYED) &&
707 (status & EXTENT_STATUS_WRITTEN)) {
708 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
709 " delayed and written which can potentially "
710 " cause data loss.\n", lblk, len);
711 WARN_ON(1);
712 }
713
Zheng Liu06b0c882013-02-18 00:26:51 -0500714 newes.es_lblk = lblk;
715 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500716 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500717 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500718
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400719 ext4_es_insert_extent_check(inode, &newes);
720
Zheng Liu654598b2012-11-08 21:57:20 -0500721 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500722 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500723 if (err != 0)
724 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400725retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500726 err = __es_insert_extent(inode, &newes);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500727 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500728 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400729 goto retry;
730 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
731 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500732
733error:
Zheng Liu654598b2012-11-08 21:57:20 -0500734 write_unlock(&EXT4_I(inode)->i_es_lock);
735
736 ext4_es_print_tree(inode);
737
738 return err;
739}
740
Zheng Liud100eef2013-02-18 00:29:59 -0500741/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400742 * ext4_es_cache_extent() inserts information into the extent status
743 * tree if and only if there isn't information about the range in
744 * question already.
745 */
746void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
747 ext4_lblk_t len, ext4_fsblk_t pblk,
748 unsigned int status)
749{
750 struct extent_status *es;
751 struct extent_status newes;
752 ext4_lblk_t end = lblk + len - 1;
753
754 newes.es_lblk = lblk;
755 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500756 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400757 trace_ext4_es_cache_extent(inode, &newes);
758
759 if (!len)
760 return;
761
762 BUG_ON(end < lblk);
763
764 write_lock(&EXT4_I(inode)->i_es_lock);
765
766 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400767 if (!es || es->es_lblk > end)
768 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400769 write_unlock(&EXT4_I(inode)->i_es_lock);
770}
771
772/*
Zheng Liud100eef2013-02-18 00:29:59 -0500773 * ext4_es_lookup_extent() looks up an extent in extent status tree.
774 *
775 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
776 *
777 * Return: 1 on found, 0 on not
778 */
779int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
780 struct extent_status *es)
781{
782 struct ext4_es_tree *tree;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400783 struct ext4_es_stats *stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500784 struct extent_status *es1 = NULL;
785 struct rb_node *node;
786 int found = 0;
787
788 trace_ext4_es_lookup_extent_enter(inode, lblk);
789 es_debug("lookup extent in block %u\n", lblk);
790
791 tree = &EXT4_I(inode)->i_es_tree;
792 read_lock(&EXT4_I(inode)->i_es_lock);
793
794 /* find extent in cache firstly */
795 es->es_lblk = es->es_len = es->es_pblk = 0;
796 if (tree->cache_es) {
797 es1 = tree->cache_es;
798 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
799 es_debug("%u cached by [%u/%u)\n",
800 lblk, es1->es_lblk, es1->es_len);
801 found = 1;
802 goto out;
803 }
804 }
805
806 node = tree->root.rb_node;
807 while (node) {
808 es1 = rb_entry(node, struct extent_status, rb_node);
809 if (lblk < es1->es_lblk)
810 node = node->rb_left;
811 else if (lblk > ext4_es_end(es1))
812 node = node->rb_right;
813 else {
814 found = 1;
815 break;
816 }
817 }
818
819out:
Zheng Liueb68d0e2014-09-01 22:26:49 -0400820 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500821 if (found) {
822 BUG_ON(!es1);
823 es->es_lblk = es1->es_lblk;
824 es->es_len = es1->es_len;
825 es->es_pblk = es1->es_pblk;
Jan Kara2be12de2014-11-25 11:55:24 -0500826 if (!ext4_es_is_referenced(es))
827 ext4_es_set_referenced(es);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400828 stats->es_stats_cache_hits++;
829 } else {
830 stats->es_stats_cache_misses++;
Zheng Liud100eef2013-02-18 00:29:59 -0500831 }
832
833 read_unlock(&EXT4_I(inode)->i_es_lock);
834
835 trace_ext4_es_lookup_extent_exit(inode, es, found);
836 return found;
837}
838
Zheng Liubdedbb72013-02-18 00:32:02 -0500839static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
840 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500841{
Zheng Liubdedbb72013-02-18 00:32:02 -0500842 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500843 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500844 struct extent_status *es;
845 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500846 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500847 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400848 int err;
Zheng Liu654598b2012-11-08 21:57:20 -0500849
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400850retry:
851 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500852 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500853 if (!es)
854 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500855 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500856 goto out;
857
858 /* Simply invalidate cache_es. */
859 tree->cache_es = NULL;
860
Zheng Liu06b0c882013-02-18 00:26:51 -0500861 orig_es.es_lblk = es->es_lblk;
862 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500863 orig_es.es_pblk = es->es_pblk;
864
Zheng Liu06b0c882013-02-18 00:26:51 -0500865 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
866 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500867 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500868 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500869 if (len2 > 0) {
870 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500871 struct extent_status newes;
872
873 newes.es_lblk = end + 1;
874 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -0400875 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -0500876 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500877 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -0500878 block = ext4_es_pblock(&orig_es) +
879 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500880 ext4_es_store_pblock_status(&newes, block,
881 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500882 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500883 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500884 es->es_lblk = orig_es.es_lblk;
885 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400886 if ((err == -ENOMEM) &&
Zheng Liuedaa53c2014-11-25 11:45:37 -0500887 __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500888 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400889 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -0500890 goto out;
891 }
892 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500893 es->es_lblk = end + 1;
894 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500895 if (ext4_es_is_written(es) ||
896 ext4_es_is_unwritten(es)) {
897 block = orig_es.es_pblk + orig_es.es_len - len2;
898 ext4_es_store_pblock(es, block);
899 }
Zheng Liu654598b2012-11-08 21:57:20 -0500900 }
901 goto out;
902 }
903
904 if (len1 > 0) {
905 node = rb_next(&es->rb_node);
906 if (node)
907 es = rb_entry(node, struct extent_status, rb_node);
908 else
909 es = NULL;
910 }
911
Zheng Liu06b0c882013-02-18 00:26:51 -0500912 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500913 node = rb_next(&es->rb_node);
914 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500915 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500916 if (!node) {
917 es = NULL;
918 break;
919 }
920 es = rb_entry(node, struct extent_status, rb_node);
921 }
922
Zheng Liu06b0c882013-02-18 00:26:51 -0500923 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -0500924 ext4_lblk_t orig_len = es->es_len;
925
Zheng Liu06b0c882013-02-18 00:26:51 -0500926 len1 = ext4_es_end(es) - end;
927 es->es_lblk = end + 1;
928 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -0500929 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
930 block = es->es_pblk + orig_len - len1;
931 ext4_es_store_pblock(es, block);
932 }
Zheng Liu654598b2012-11-08 21:57:20 -0500933 }
934
935out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500936 return err;
937}
938
939/*
940 * ext4_es_remove_extent() removes a space from a extent status tree.
941 *
942 * Return 0 on success, error code on failure.
943 */
944int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
945 ext4_lblk_t len)
946{
Zheng Liu06b0c882013-02-18 00:26:51 -0500947 ext4_lblk_t end;
948 int err = 0;
949
950 trace_ext4_es_remove_extent(inode, lblk, len);
951 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
952 lblk, len, inode->i_ino);
953
Eryu Guand4381472013-02-22 15:27:47 -0500954 if (!len)
955 return err;
956
Zheng Liu06b0c882013-02-18 00:26:51 -0500957 end = lblk + len - 1;
958 BUG_ON(end < lblk);
959
Zheng Liuedaa53c2014-11-25 11:45:37 -0500960 /*
961 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
962 * so that we are sure __es_shrink() is done with the inode before it
963 * is reclaimed.
964 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500965 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500966 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500967 write_unlock(&EXT4_I(inode)->i_es_lock);
968 ext4_es_print_tree(inode);
969 return err;
970}
Zheng Liu74cd15c2013-02-18 00:32:55 -0500971
Zheng Liuedaa53c2014-11-25 11:45:37 -0500972static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
973 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -0500974{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500975 struct ext4_inode_info *ei;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400976 struct ext4_es_stats *es_stats;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400977 ktime_t start_time;
978 u64 scan_time;
Zheng Liuedaa53c2014-11-25 11:45:37 -0500979 int nr_to_walk;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000980 int nr_shrunk = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -0500981 int retried = 0, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500982
Zheng Liueb68d0e2014-09-01 22:26:49 -0400983 es_stats = &sbi->s_es_stats;
984 start_time = ktime_get();
Zheng Liud3922a72013-07-01 08:12:37 -0400985
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400986retry:
Zheng Liuedaa53c2014-11-25 11:45:37 -0500987 spin_lock(&sbi->s_es_lock);
988 nr_to_walk = sbi->s_es_nr_inode;
989 while (nr_to_walk-- > 0) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500990 if (list_empty(&sbi->s_es_list)) {
991 spin_unlock(&sbi->s_es_lock);
992 goto out;
993 }
994 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
995 i_es_list);
996 /* Move the inode to the tail */
Jan Karadd475922014-11-25 11:51:23 -0500997 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500998
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400999 /*
Zheng Liuedaa53c2014-11-25 11:45:37 -05001000 * Normally we try hard to avoid shrinking precached inodes,
1001 * but we will as a last resort.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001002 */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001003 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1004 EXT4_STATE_EXT_PRECACHED)) {
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001005 nr_skipped++;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001006 continue;
1007 }
Zheng Liud3922a72013-07-01 08:12:37 -04001008
Zheng Liuedaa53c2014-11-25 11:45:37 -05001009 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1010 nr_skipped++;
Zheng Liud3922a72013-07-01 08:12:37 -04001011 continue;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001012 }
1013 /*
1014 * Now we hold i_es_lock which protects us from inode reclaim
1015 * freeing inode under us
1016 */
1017 spin_unlock(&sbi->s_es_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001018
Jan Karadd475922014-11-25 11:51:23 -05001019 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001020 write_unlock(&ei->i_es_lock);
1021
Jan Karadd475922014-11-25 11:51:23 -05001022 if (nr_to_scan <= 0)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001023 goto out;
1024 spin_lock(&sbi->s_es_lock);
1025 }
1026 spin_unlock(&sbi->s_es_lock);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001027
1028 /*
1029 * If we skipped any inodes, and we weren't able to make any
Zheng Liuedaa53c2014-11-25 11:45:37 -05001030 * forward progress, try again to scan precached inodes.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001031 */
1032 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1033 retried++;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001034 goto retry;
1035 }
1036
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001037 if (locked_ei && nr_shrunk == 0)
Jan Karadd475922014-11-25 11:51:23 -05001038 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001039
Zheng Liuedaa53c2014-11-25 11:45:37 -05001040out:
Zheng Liueb68d0e2014-09-01 22:26:49 -04001041 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1042 if (likely(es_stats->es_stats_scan_time))
1043 es_stats->es_stats_scan_time = (scan_time +
1044 es_stats->es_stats_scan_time*3) / 4;
1045 else
1046 es_stats->es_stats_scan_time = scan_time;
1047 if (scan_time > es_stats->es_stats_max_scan_time)
1048 es_stats->es_stats_max_scan_time = scan_time;
1049 if (likely(es_stats->es_stats_shrunk))
1050 es_stats->es_stats_shrunk = (nr_shrunk +
1051 es_stats->es_stats_shrunk*3) / 4;
1052 else
1053 es_stats->es_stats_shrunk = nr_shrunk;
1054
Zheng Liuedaa53c2014-11-25 11:45:37 -05001055 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001056 nr_skipped, retried);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001057 return nr_shrunk;
1058}
1059
Dave Chinner1ab6c492013-08-28 10:18:09 +10001060static unsigned long ext4_es_count(struct shrinker *shrink,
1061 struct shrink_control *sc)
1062{
1063 unsigned long nr;
1064 struct ext4_sb_info *sbi;
1065
1066 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001067 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001068 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001069 return nr;
1070}
1071
1072static unsigned long ext4_es_scan(struct shrinker *shrink,
1073 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001074{
1075 struct ext4_sb_info *sbi = container_of(shrink,
1076 struct ext4_sb_info, s_es_shrinker);
1077 int nr_to_scan = sc->nr_to_scan;
1078 int ret, nr_shrunk;
1079
Zheng Liuedaa53c2014-11-25 11:45:37 -05001080 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001081 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001082
1083 if (!nr_to_scan)
1084 return ret;
1085
Zheng Liuedaa53c2014-11-25 11:45:37 -05001086 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001087
Zheng Liue963bb12014-09-01 22:22:13 -04001088 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001089 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001090}
1091
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001092int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001093{
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001094 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001095 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1096 struct ext4_inode_info *ei, *max = NULL;
1097 unsigned int inode_cnt = 0;
1098
1099 if (v != SEQ_START_TOKEN)
1100 return 0;
1101
1102 /* here we just find an inode that has the max nr. of objects */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001103 spin_lock(&sbi->s_es_lock);
1104 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
Zheng Liueb68d0e2014-09-01 22:26:49 -04001105 inode_cnt++;
1106 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1107 max = ei;
1108 else if (!max)
1109 max = ei;
1110 }
Zheng Liuedaa53c2014-11-25 11:45:37 -05001111 spin_unlock(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001112
1113 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1114 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
Zheng Liuedaa53c2014-11-25 11:45:37 -05001115 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
Zheng Liueb68d0e2014-09-01 22:26:49 -04001116 seq_printf(seq, " %lu/%lu cache hits/misses\n",
1117 es_stats->es_stats_cache_hits,
1118 es_stats->es_stats_cache_misses);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001119 if (inode_cnt)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001120 seq_printf(seq, " %d inodes on list\n", inode_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001121
1122 seq_printf(seq, "average:\n %llu us scan time\n",
1123 div_u64(es_stats->es_stats_scan_time, 1000));
1124 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1125 if (inode_cnt)
1126 seq_printf(seq,
1127 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1128 " %llu us max scan time\n",
Zheng Liuedaa53c2014-11-25 11:45:37 -05001129 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001130 div_u64(es_stats->es_stats_max_scan_time, 1000));
1131
1132 return 0;
1133}
1134
Zheng Liueb68d0e2014-09-01 22:26:49 -04001135int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1136{
1137 int err;
1138
Jan Kara624d0f12014-11-25 11:53:47 -05001139 /* Make sure we have enough bits for physical block number */
1140 BUILD_BUG_ON(ES_SHIFT < 48);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001141 INIT_LIST_HEAD(&sbi->s_es_list);
1142 sbi->s_es_nr_inode = 0;
1143 spin_lock_init(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001144 sbi->s_es_stats.es_stats_shrunk = 0;
1145 sbi->s_es_stats.es_stats_cache_hits = 0;
1146 sbi->s_es_stats.es_stats_cache_misses = 0;
1147 sbi->s_es_stats.es_stats_scan_time = 0;
1148 sbi->s_es_stats.es_stats_max_scan_time = 0;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001149 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001150 if (err)
1151 return err;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001152 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001153 if (err)
1154 goto err1;
1155
Dave Chinner1ab6c492013-08-28 10:18:09 +10001156 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1157 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001158 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001159 err = register_shrinker(&sbi->s_es_shrinker);
1160 if (err)
1161 goto err2;
1162
Zheng Liueb68d0e2014-09-01 22:26:49 -04001163 return 0;
1164
1165err2:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001166 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001167err1:
1168 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1169 return err;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001170}
1171
Zheng Liud3922a72013-07-01 08:12:37 -04001172void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001173{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001174 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001175 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liud3922a72013-07-01 08:12:37 -04001176 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001177}
1178
Jan Karadd475922014-11-25 11:51:23 -05001179/*
1180 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1181 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1182 *
1183 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1184 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1185 * ei->i_es_shrink_lblk to where we should continue scanning.
1186 */
1187static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1188 int *nr_to_scan, int *nr_shrunk)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001189{
1190 struct inode *inode = &ei->vfs_inode;
1191 struct ext4_es_tree *tree = &ei->i_es_tree;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001192 struct extent_status *es;
Jan Karadd475922014-11-25 11:51:23 -05001193 struct rb_node *node;
1194
1195 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1196 if (!es)
1197 goto out_wrap;
1198 node = &es->rb_node;
1199 while (*nr_to_scan > 0) {
1200 if (es->es_lblk > end) {
1201 ei->i_es_shrink_lblk = end + 1;
1202 return 0;
1203 }
1204
1205 (*nr_to_scan)--;
1206 node = rb_next(&es->rb_node);
1207 /*
1208 * We can't reclaim delayed extent from status tree because
1209 * fiemap, bigallic, and seek_data/hole need to use it.
1210 */
Jan Kara2be12de2014-11-25 11:55:24 -05001211 if (ext4_es_is_delayed(es))
1212 goto next;
1213 if (ext4_es_is_referenced(es)) {
1214 ext4_es_clear_referenced(es);
1215 goto next;
Jan Karadd475922014-11-25 11:51:23 -05001216 }
Jan Kara2be12de2014-11-25 11:55:24 -05001217
1218 rb_erase(&es->rb_node, &tree->root);
1219 ext4_es_free_extent(inode, es);
1220 (*nr_shrunk)++;
1221next:
Jan Karadd475922014-11-25 11:51:23 -05001222 if (!node)
1223 goto out_wrap;
1224 es = rb_entry(node, struct extent_status, rb_node);
1225 }
1226 ei->i_es_shrink_lblk = es->es_lblk;
1227 return 1;
1228out_wrap:
1229 ei->i_es_shrink_lblk = 0;
1230 return 0;
1231}
1232
1233static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1234{
1235 struct inode *inode = &ei->vfs_inode;
1236 int nr_shrunk = 0;
1237 ext4_lblk_t start = ei->i_es_shrink_lblk;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001238 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1239 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001240
Zheng Liuedaa53c2014-11-25 11:45:37 -05001241 if (ei->i_es_shk_nr == 0)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001242 return 0;
1243
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001244 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1245 __ratelimit(&_rs))
1246 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1247
Jan Karadd475922014-11-25 11:51:23 -05001248 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1249 start != 0)
1250 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1251
1252 ei->i_es_tree.cache_es = NULL;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001253 return nr_shrunk;
1254}