blob: 30596498ed0b86b9758375b6757ba093fae20dff [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 Liueb68d0e2014-09-01 22:26:49 -040014#include <linux/proc_fs.h>
15#include <linux/seq_file.h>
Zheng Liu654598b2012-11-08 21:57:20 -050016#include "ext4.h"
17#include "extents_status.h"
Zheng Liu654598b2012-11-08 21:57:20 -050018
Zheng Liu992e9fd2012-11-08 21:57:33 -050019#include <trace/events/ext4.h>
20
Zheng Liu654598b2012-11-08 21:57:20 -050021/*
22 * According to previous discussion in Ext4 Developer Workshop, we
23 * will introduce a new structure called io tree to track all extent
24 * status in order to solve some problems that we have met
25 * (e.g. Reservation space warning), and provide extent-level locking.
26 * Delay extent tree is the first step to achieve this goal. It is
27 * original built by Yongqiang Yang. At that time it is called delay
Zheng Liu06b0c882013-02-18 00:26:51 -050028 * extent tree, whose goal is only track delayed extents in memory to
Zheng Liu654598b2012-11-08 21:57:20 -050029 * simplify the implementation of fiemap and bigalloc, and introduce
30 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
Zheng Liu06b0c882013-02-18 00:26:51 -050031 * delay extent tree at the first commit. But for better understand
32 * what it does, it has been rename to extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050033 *
Zheng Liu06b0c882013-02-18 00:26:51 -050034 * Step1:
35 * Currently the first step has been done. All delayed extents are
36 * tracked in the tree. It maintains the delayed extent when a delayed
37 * allocation is issued, and the delayed extent is written out or
Zheng Liu654598b2012-11-08 21:57:20 -050038 * invalidated. Therefore the implementation of fiemap and bigalloc
39 * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
40 *
41 * The following comment describes the implemenmtation of extent
42 * status tree and future works.
Zheng Liu06b0c882013-02-18 00:26:51 -050043 *
44 * Step2:
45 * In this step all extent status are tracked by extent status tree.
46 * Thus, we can first try to lookup a block mapping in this tree before
47 * finding it in extent tree. Hence, single extent cache can be removed
48 * because extent status tree can do a better job. Extents in status
49 * tree are loaded on-demand. Therefore, the extent status tree may not
50 * contain all of the extents in a file. Meanwhile we define a shrinker
51 * to reclaim memory from extent status tree because fragmented extent
52 * tree will make status tree cost too much memory. written/unwritten/-
53 * hole extents in the tree will be reclaimed by this shrinker when we
54 * are under high memory pressure. Delayed extents will not be
55 * reclimed because fiemap, bigalloc, and seek_data/hole need it.
Zheng Liu654598b2012-11-08 21:57:20 -050056 */
57
58/*
Zheng Liu06b0c882013-02-18 00:26:51 -050059 * Extent status tree implementation for ext4.
Zheng Liu654598b2012-11-08 21:57:20 -050060 *
61 *
62 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050063 * Extent status tree tracks all extent status.
Zheng Liu654598b2012-11-08 21:57:20 -050064 *
Zheng Liu06b0c882013-02-18 00:26:51 -050065 * 1. Why we need to implement extent status tree?
Zheng Liu654598b2012-11-08 21:57:20 -050066 *
Zheng Liu06b0c882013-02-18 00:26:51 -050067 * Without extent status tree, ext4 identifies a delayed extent by looking
Zheng Liu654598b2012-11-08 21:57:20 -050068 * up page cache, this has several deficiencies - complicated, buggy,
69 * and inefficient code.
70 *
Zheng Liu06b0c882013-02-18 00:26:51 -050071 * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
72 * block or a range of blocks are belonged to a delayed extent.
Zheng Liu654598b2012-11-08 21:57:20 -050073 *
Zheng Liu06b0c882013-02-18 00:26:51 -050074 * Let us have a look at how they do without extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050075 * -- FIEMAP
76 * FIEMAP looks up page cache to identify delayed allocations from holes.
77 *
78 * -- SEEK_HOLE/DATA
79 * SEEK_HOLE/DATA has the same problem as FIEMAP.
80 *
81 * -- bigalloc
82 * bigalloc looks up page cache to figure out if a block is
83 * already under delayed allocation or not to determine whether
84 * quota reserving is needed for the cluster.
85 *
Zheng Liu654598b2012-11-08 21:57:20 -050086 * -- writeout
87 * Writeout looks up whole page cache to see if a buffer is
88 * mapped, If there are not very many delayed buffers, then it is
89 * time comsuming.
90 *
Zheng Liu06b0c882013-02-18 00:26:51 -050091 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
Zheng Liu654598b2012-11-08 21:57:20 -050092 * bigalloc and writeout can figure out if a block or a range of
93 * blocks is under delayed allocation(belonged to a delayed extent) or
Zheng Liu06b0c882013-02-18 00:26:51 -050094 * not by searching the extent tree.
Zheng Liu654598b2012-11-08 21:57:20 -050095 *
96 *
97 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050098 * 2. Ext4 extent status tree impelmentation
Zheng Liu654598b2012-11-08 21:57:20 -050099 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500100 * -- extent
101 * A extent is a range of blocks which are contiguous logically and
102 * physically. Unlike extent in extent tree, this extent in ext4 is
103 * a in-memory struct, there is no corresponding on-disk data. There
104 * is no limit on length of extent, so an extent can contain as many
105 * blocks as they are contiguous logically and physically.
Zheng Liu654598b2012-11-08 21:57:20 -0500106 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500107 * -- extent status tree
108 * Every inode has an extent status tree and all allocation blocks
109 * are added to the tree with different status. The extent in the
110 * tree are ordered by logical block no.
Zheng Liu654598b2012-11-08 21:57:20 -0500111 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500112 * -- operations on a extent status tree
113 * There are three important operations on a delayed extent tree: find
114 * next extent, adding a extent(a range of blocks) and removing a extent.
Zheng Liu654598b2012-11-08 21:57:20 -0500115 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500116 * -- race on a extent status tree
117 * Extent status tree is protected by inode->i_es_lock.
118 *
119 * -- memory consumption
120 * Fragmented extent tree will make extent status tree cost too much
121 * memory. Hence, we will reclaim written/unwritten/hole extents from
122 * the tree under a heavy memory pressure.
Zheng Liu654598b2012-11-08 21:57:20 -0500123 *
124 *
125 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -0500126 * 3. Performance analysis
127 *
Zheng Liu654598b2012-11-08 21:57:20 -0500128 * -- overhead
129 * 1. There is a cache extent for write access, so if writes are
130 * not very random, adding space operaions are in O(1) time.
131 *
132 * -- gain
133 * 2. Code is much simpler, more readable, more maintainable and
134 * more efficient.
135 *
136 *
137 * ==========================================================================
138 * 4. TODO list
Zheng Liu654598b2012-11-08 21:57:20 -0500139 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500140 * -- Refactor delayed space reservation
Zheng Liu654598b2012-11-08 21:57:20 -0500141 *
142 * -- Extent-level locking
143 */
144
145static struct kmem_cache *ext4_es_cachep;
146
Zheng Liubdedbb72013-02-18 00:32:02 -0500147static int __es_insert_extent(struct inode *inode, struct extent_status *newes);
148static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liu06b0c882013-02-18 00:26:51 -0500149 ext4_lblk_t end);
Jan Karadd475922014-11-25 11:51:23 -0500150static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500151static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
152 struct ext4_inode_info *locked_ei);
Zheng Liu06b0c882013-02-18 00:26:51 -0500153
Zheng Liu654598b2012-11-08 21:57:20 -0500154int __init ext4_init_es(void)
155{
Theodore Ts'o24630772013-02-28 23:58:56 -0500156 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
157 sizeof(struct extent_status),
158 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500159 if (ext4_es_cachep == NULL)
160 return -ENOMEM;
161 return 0;
162}
163
164void ext4_exit_es(void)
165{
166 if (ext4_es_cachep)
167 kmem_cache_destroy(ext4_es_cachep);
168}
169
170void ext4_es_init_tree(struct ext4_es_tree *tree)
171{
172 tree->root = RB_ROOT;
173 tree->cache_es = NULL;
174}
175
176#ifdef ES_DEBUG__
177static void ext4_es_print_tree(struct inode *inode)
178{
179 struct ext4_es_tree *tree;
180 struct rb_node *node;
181
182 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
183 tree = &EXT4_I(inode)->i_es_tree;
184 node = rb_first(&tree->root);
185 while (node) {
186 struct extent_status *es;
187 es = rb_entry(node, struct extent_status, rb_node);
Eric Whitneyce140cd2014-02-20 16:09:12 -0500188 printk(KERN_DEBUG " [%u/%u) %llu %x",
Zheng Liufdc02122013-02-18 00:26:51 -0500189 es->es_lblk, es->es_len,
190 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500191 node = rb_next(node);
192 }
193 printk(KERN_DEBUG "\n");
194}
195#else
196#define ext4_es_print_tree(inode)
197#endif
198
Zheng Liu06b0c882013-02-18 00:26:51 -0500199static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500200{
Zheng Liu06b0c882013-02-18 00:26:51 -0500201 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
202 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500203}
204
205/*
206 * search through the tree for an delayed extent with a given offset. If
207 * it can't be found, try to find next extent.
208 */
209static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500210 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500211{
212 struct rb_node *node = root->rb_node;
213 struct extent_status *es = NULL;
214
215 while (node) {
216 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500217 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500218 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500219 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500220 node = node->rb_right;
221 else
222 return es;
223 }
224
Zheng Liu06b0c882013-02-18 00:26:51 -0500225 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500226 return es;
227
Zheng Liu06b0c882013-02-18 00:26:51 -0500228 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500229 node = rb_next(&es->rb_node);
230 return node ? rb_entry(node, struct extent_status, rb_node) :
231 NULL;
232 }
233
234 return NULL;
235}
236
237/*
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400238 * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
239 * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500240 *
241 * @inode: the inode which owns delayed extents
Zheng Liube401362013-02-18 00:27:26 -0500242 * @lblk: the offset where we start to search
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400243 * @end: the offset where we stop to search
Zheng Liu654598b2012-11-08 21:57:20 -0500244 * @es: delayed extent that we found
Zheng Liu654598b2012-11-08 21:57:20 -0500245 */
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400246void ext4_es_find_delayed_extent_range(struct inode *inode,
247 ext4_lblk_t lblk, ext4_lblk_t end,
Zheng Liube401362013-02-18 00:27:26 -0500248 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500249{
250 struct ext4_es_tree *tree = NULL;
251 struct extent_status *es1 = NULL;
252 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500253
Zheng Liube401362013-02-18 00:27:26 -0500254 BUG_ON(es == NULL);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400255 BUG_ON(end < lblk);
256 trace_ext4_es_find_delayed_extent_range_enter(inode, lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500257
Zheng Liu654598b2012-11-08 21:57:20 -0500258 read_lock(&EXT4_I(inode)->i_es_lock);
259 tree = &EXT4_I(inode)->i_es_tree;
260
Zheng Liufdc02122013-02-18 00:26:51 -0500261 /* find extent in cache firstly */
Zheng Liube401362013-02-18 00:27:26 -0500262 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500263 if (tree->cache_es) {
264 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500265 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400266 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500267 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500268 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500269 goto out;
270 }
271 }
272
Zheng Liube401362013-02-18 00:27:26 -0500273 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500274
275out:
Zheng Liube401362013-02-18 00:27:26 -0500276 if (es1 && !ext4_es_is_delayed(es1)) {
277 while ((node = rb_next(&es1->rb_node)) != NULL) {
278 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400279 if (es1->es_lblk > end) {
280 es1 = NULL;
281 break;
282 }
Zheng Liube401362013-02-18 00:27:26 -0500283 if (ext4_es_is_delayed(es1))
284 break;
285 }
286 }
287
288 if (es1 && ext4_es_is_delayed(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500289 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500290 es->es_lblk = es1->es_lblk;
291 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500292 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500293 }
294
295 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500296
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400297 trace_ext4_es_find_delayed_extent_range_exit(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500298}
299
Jan Karab0dea4c2014-11-25 11:49:25 -0500300static void ext4_es_list_add(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500301{
302 struct ext4_inode_info *ei = EXT4_I(inode);
303 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
304
305 if (!list_empty(&ei->i_es_list))
306 return;
307
308 spin_lock(&sbi->s_es_lock);
309 if (list_empty(&ei->i_es_list)) {
310 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
311 sbi->s_es_nr_inode++;
312 }
313 spin_unlock(&sbi->s_es_lock);
314}
315
Jan Karab0dea4c2014-11-25 11:49:25 -0500316static void ext4_es_list_del(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500317{
318 struct ext4_inode_info *ei = EXT4_I(inode);
319 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
320
321 spin_lock(&sbi->s_es_lock);
322 if (!list_empty(&ei->i_es_list)) {
323 list_del_init(&ei->i_es_list);
324 sbi->s_es_nr_inode--;
325 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
326 }
327 spin_unlock(&sbi->s_es_lock);
328}
329
Zheng Liu654598b2012-11-08 21:57:20 -0500330static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500331ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
332 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500333{
334 struct extent_status *es;
335 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
336 if (es == NULL)
337 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500338 es->es_lblk = lblk;
339 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500340 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500341
342 /*
343 * We don't count delayed extent because we never try to reclaim them
344 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500345 if (!ext4_es_is_delayed(es)) {
Jan Karab0dea4c2014-11-25 11:49:25 -0500346 if (!EXT4_I(inode)->i_es_shk_nr++)
347 ext4_es_list_add(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400348 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500349 s_es_stats.es_stats_shk_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500350 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500351
Zheng Liueb68d0e2014-09-01 22:26:49 -0400352 EXT4_I(inode)->i_es_all_nr++;
353 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
354
Zheng Liu654598b2012-11-08 21:57:20 -0500355 return es;
356}
357
Zheng Liubdedbb72013-02-18 00:32:02 -0500358static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500359{
Zheng Liueb68d0e2014-09-01 22:26:49 -0400360 EXT4_I(inode)->i_es_all_nr--;
361 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
362
Zheng Liuedaa53c2014-11-25 11:45:37 -0500363 /* Decrease the shrink counter when this es is not delayed */
Zheng Liu74cd15c2013-02-18 00:32:55 -0500364 if (!ext4_es_is_delayed(es)) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500365 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
Jan Karab0dea4c2014-11-25 11:49:25 -0500366 if (!--EXT4_I(inode)->i_es_shk_nr)
367 ext4_es_list_del(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400368 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500369 s_es_stats.es_stats_shk_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500370 }
371
Zheng Liu654598b2012-11-08 21:57:20 -0500372 kmem_cache_free(ext4_es_cachep, es);
373}
374
Zheng Liu06b0c882013-02-18 00:26:51 -0500375/*
376 * Check whether or not two extents can be merged
377 * Condition:
378 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500379 * - physical block number is contiguous
380 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500381 */
382static int ext4_es_can_be_merged(struct extent_status *es1,
383 struct extent_status *es2)
384{
Zheng Liufdc02122013-02-18 00:26:51 -0500385 if (ext4_es_status(es1) != ext4_es_status(es2))
386 return 0;
387
Lukas Czerner0baaea62014-05-12 22:21:43 -0400388 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
389 pr_warn("ES assertion failed when merging extents. "
390 "The sum of lengths of es1 (%d) and es2 (%d) "
391 "is bigger than allowed file size (%d)\n",
392 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
393 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500394 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400395 }
Zheng Liufdc02122013-02-18 00:26:51 -0500396
Zheng Liubd384362013-03-10 20:48:59 -0400397 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
398 return 0;
399
400 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
401 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
402 return 1;
403
404 if (ext4_es_is_hole(es1))
405 return 1;
406
407 /* we need to check delayed extent is without unwritten status */
408 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
409 return 1;
410
411 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500412}
413
Zheng Liu654598b2012-11-08 21:57:20 -0500414static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500415ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500416{
Zheng Liubdedbb72013-02-18 00:32:02 -0500417 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500418 struct extent_status *es1;
419 struct rb_node *node;
420
421 node = rb_prev(&es->rb_node);
422 if (!node)
423 return es;
424
425 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500426 if (ext4_es_can_be_merged(es1, es)) {
427 es1->es_len += es->es_len;
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;
Zheng Liu654598b2012-11-08 21:57:20 -0500450 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500451 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500452 }
453
454 return es;
455}
456
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400457#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400458#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
459
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400460static void ext4_es_insert_extent_ext_check(struct inode *inode,
461 struct extent_status *es)
462{
463 struct ext4_ext_path *path = NULL;
464 struct ext4_extent *ex;
465 ext4_lblk_t ee_block;
466 ext4_fsblk_t ee_start;
467 unsigned short ee_len;
468 int depth, ee_status, es_status;
469
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400470 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400471 if (IS_ERR(path))
472 return;
473
474 depth = ext_depth(inode);
475 ex = path[depth].p_ext;
476
477 if (ex) {
478
479 ee_block = le32_to_cpu(ex->ee_block);
480 ee_start = ext4_ext_pblock(ex);
481 ee_len = ext4_ext_get_actual_len(ex);
482
Lukas Czerner556615d2014-04-20 23:45:47 -0400483 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400484 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
485
486 /*
487 * Make sure ex and es are not overlap when we try to insert
488 * a delayed/hole extent.
489 */
490 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
491 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400492 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400493 "inode: %lu we can find an extent "
494 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500495 "want to add a delayed/hole extent "
496 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400497 inode->i_ino, ee_block, ee_len,
498 ee_start, ee_status ? 'u' : 'w',
499 es->es_lblk, es->es_len,
500 ext4_es_pblock(es), ext4_es_status(es));
501 }
502 goto out;
503 }
504
505 /*
506 * We don't check ee_block == es->es_lblk, etc. because es
507 * might be a part of whole extent, vice versa.
508 */
509 if (es->es_lblk < ee_block ||
510 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400511 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400512 "ex_status [%d/%d/%llu/%c] != "
513 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
514 ee_block, ee_len, ee_start,
515 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
516 ext4_es_pblock(es), es_status ? 'u' : 'w');
517 goto out;
518 }
519
520 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400521 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400522 "ex_status [%d/%d/%llu/%c] != "
523 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
524 ee_block, ee_len, ee_start,
525 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
526 ext4_es_pblock(es), es_status ? 'u' : 'w');
527 }
528 } else {
529 /*
530 * We can't find an extent on disk. So we need to make sure
531 * that we don't want to add an written/unwritten extent.
532 */
533 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400534 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400535 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500536 "to add a written/unwritten extent "
537 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400538 es->es_lblk, es->es_lblk, es->es_len,
539 ext4_es_pblock(es), ext4_es_status(es));
540 }
541 }
542out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400543 ext4_ext_drop_refs(path);
544 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400545}
546
547static void ext4_es_insert_extent_ind_check(struct inode *inode,
548 struct extent_status *es)
549{
550 struct ext4_map_blocks map;
551 int retval;
552
553 /*
554 * Here we call ext4_ind_map_blocks to lookup a block mapping because
555 * 'Indirect' structure is defined in indirect.c. So we couldn't
556 * access direct/indirect tree from outside. It is too dirty to define
557 * this function in indirect.c file.
558 */
559
560 map.m_lblk = es->es_lblk;
561 map.m_len = es->es_len;
562
563 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
564 if (retval > 0) {
565 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
566 /*
567 * We want to add a delayed/hole extent but this
568 * block has been allocated.
569 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400570 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400571 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500572 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400573 inode->i_ino, es->es_lblk, es->es_len,
574 ext4_es_pblock(es), ext4_es_status(es));
575 return;
576 } else if (ext4_es_is_written(es)) {
577 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400578 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400579 "inode: %lu retval %d != es_len %d\n",
580 inode->i_ino, retval, es->es_len);
581 return;
582 }
583 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400584 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400585 "inode: %lu m_pblk %llu != "
586 "es_pblk %llu\n",
587 inode->i_ino, map.m_pblk,
588 ext4_es_pblock(es));
589 return;
590 }
591 } else {
592 /*
593 * We don't need to check unwritten extent because
594 * indirect-based file doesn't have it.
595 */
596 BUG_ON(1);
597 }
598 } else if (retval == 0) {
599 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400600 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400601 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500602 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400603 inode->i_ino, es->es_lblk, es->es_len,
604 ext4_es_pblock(es), ext4_es_status(es));
605 return;
606 }
607 }
608}
609
610static inline void ext4_es_insert_extent_check(struct inode *inode,
611 struct extent_status *es)
612{
613 /*
614 * We don't need to worry about the race condition because
615 * caller takes i_data_sem locking.
616 */
617 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
618 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
619 ext4_es_insert_extent_ext_check(inode, es);
620 else
621 ext4_es_insert_extent_ind_check(inode, es);
622}
623#else
624static inline void ext4_es_insert_extent_check(struct inode *inode,
625 struct extent_status *es)
626{
627}
628#endif
629
Zheng Liubdedbb72013-02-18 00:32:02 -0500630static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500631{
Zheng Liubdedbb72013-02-18 00:32:02 -0500632 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500633 struct rb_node **p = &tree->root.rb_node;
634 struct rb_node *parent = NULL;
635 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500636
637 while (*p) {
638 parent = *p;
639 es = rb_entry(parent, struct extent_status, rb_node);
640
Zheng Liu06b0c882013-02-18 00:26:51 -0500641 if (newes->es_lblk < es->es_lblk) {
642 if (ext4_es_can_be_merged(newes, es)) {
643 /*
644 * Here we can modify es_lblk directly
645 * because it isn't overlapped.
646 */
647 es->es_lblk = newes->es_lblk;
648 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500649 if (ext4_es_is_written(es) ||
650 ext4_es_is_unwritten(es))
651 ext4_es_store_pblock(es,
652 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500653 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500654 goto out;
655 }
656 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500657 } else if (newes->es_lblk > ext4_es_end(es)) {
658 if (ext4_es_can_be_merged(es, newes)) {
659 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500660 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500661 goto out;
662 }
663 p = &(*p)->rb_right;
664 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500665 BUG_ON(1);
666 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500667 }
668 }
669
Zheng Liubdedbb72013-02-18 00:32:02 -0500670 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500671 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500672 if (!es)
673 return -ENOMEM;
674 rb_link_node(&es->rb_node, parent, p);
675 rb_insert_color(&es->rb_node, &tree->root);
676
677out:
678 tree->cache_es = es;
679 return 0;
680}
681
682/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400683 * ext4_es_insert_extent() adds information to an inode's extent
684 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500685 *
686 * Return 0 on success, error code on failure.
687 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500688int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500689 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400690 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500691{
Zheng Liu06b0c882013-02-18 00:26:51 -0500692 struct extent_status newes;
693 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500694 int err = 0;
695
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400696 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500697 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500698
Eryu Guand4381472013-02-22 15:27:47 -0500699 if (!len)
700 return 0;
701
Zheng Liu06b0c882013-02-18 00:26:51 -0500702 BUG_ON(end < lblk);
703
704 newes.es_lblk = lblk;
705 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500706 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500707 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500708
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400709 ext4_es_insert_extent_check(inode, &newes);
710
Zheng Liu654598b2012-11-08 21:57:20 -0500711 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500712 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500713 if (err != 0)
714 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400715retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500716 err = __es_insert_extent(inode, &newes);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500717 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500718 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400719 goto retry;
720 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
721 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500722
723error:
Zheng Liu654598b2012-11-08 21:57:20 -0500724 write_unlock(&EXT4_I(inode)->i_es_lock);
725
726 ext4_es_print_tree(inode);
727
728 return err;
729}
730
Zheng Liud100eef2013-02-18 00:29:59 -0500731/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400732 * ext4_es_cache_extent() inserts information into the extent status
733 * tree if and only if there isn't information about the range in
734 * question already.
735 */
736void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
737 ext4_lblk_t len, ext4_fsblk_t pblk,
738 unsigned int status)
739{
740 struct extent_status *es;
741 struct extent_status newes;
742 ext4_lblk_t end = lblk + len - 1;
743
744 newes.es_lblk = lblk;
745 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500746 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400747 trace_ext4_es_cache_extent(inode, &newes);
748
749 if (!len)
750 return;
751
752 BUG_ON(end < lblk);
753
754 write_lock(&EXT4_I(inode)->i_es_lock);
755
756 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400757 if (!es || es->es_lblk > end)
758 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400759 write_unlock(&EXT4_I(inode)->i_es_lock);
760}
761
762/*
Zheng Liud100eef2013-02-18 00:29:59 -0500763 * ext4_es_lookup_extent() looks up an extent in extent status tree.
764 *
765 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
766 *
767 * Return: 1 on found, 0 on not
768 */
769int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
770 struct extent_status *es)
771{
772 struct ext4_es_tree *tree;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400773 struct ext4_es_stats *stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500774 struct extent_status *es1 = NULL;
775 struct rb_node *node;
776 int found = 0;
777
778 trace_ext4_es_lookup_extent_enter(inode, lblk);
779 es_debug("lookup extent in block %u\n", lblk);
780
781 tree = &EXT4_I(inode)->i_es_tree;
782 read_lock(&EXT4_I(inode)->i_es_lock);
783
784 /* find extent in cache firstly */
785 es->es_lblk = es->es_len = es->es_pblk = 0;
786 if (tree->cache_es) {
787 es1 = tree->cache_es;
788 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
789 es_debug("%u cached by [%u/%u)\n",
790 lblk, es1->es_lblk, es1->es_len);
791 found = 1;
792 goto out;
793 }
794 }
795
796 node = tree->root.rb_node;
797 while (node) {
798 es1 = rb_entry(node, struct extent_status, rb_node);
799 if (lblk < es1->es_lblk)
800 node = node->rb_left;
801 else if (lblk > ext4_es_end(es1))
802 node = node->rb_right;
803 else {
804 found = 1;
805 break;
806 }
807 }
808
809out:
Zheng Liueb68d0e2014-09-01 22:26:49 -0400810 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500811 if (found) {
812 BUG_ON(!es1);
813 es->es_lblk = es1->es_lblk;
814 es->es_len = es1->es_len;
815 es->es_pblk = es1->es_pblk;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400816 stats->es_stats_cache_hits++;
817 } else {
818 stats->es_stats_cache_misses++;
Zheng Liud100eef2013-02-18 00:29:59 -0500819 }
820
821 read_unlock(&EXT4_I(inode)->i_es_lock);
822
823 trace_ext4_es_lookup_extent_exit(inode, es, found);
824 return found;
825}
826
Zheng Liubdedbb72013-02-18 00:32:02 -0500827static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
828 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500829{
Zheng Liubdedbb72013-02-18 00:32:02 -0500830 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500831 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500832 struct extent_status *es;
833 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500834 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500835 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400836 int err;
Zheng Liu654598b2012-11-08 21:57:20 -0500837
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400838retry:
839 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500840 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500841 if (!es)
842 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500843 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500844 goto out;
845
846 /* Simply invalidate cache_es. */
847 tree->cache_es = NULL;
848
Zheng Liu06b0c882013-02-18 00:26:51 -0500849 orig_es.es_lblk = es->es_lblk;
850 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500851 orig_es.es_pblk = es->es_pblk;
852
Zheng Liu06b0c882013-02-18 00:26:51 -0500853 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
854 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500855 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500856 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500857 if (len2 > 0) {
858 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500859 struct extent_status newes;
860
861 newes.es_lblk = end + 1;
862 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -0400863 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -0500864 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500865 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -0500866 block = ext4_es_pblock(&orig_es) +
867 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500868 ext4_es_store_pblock_status(&newes, block,
869 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500870 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500871 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500872 es->es_lblk = orig_es.es_lblk;
873 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400874 if ((err == -ENOMEM) &&
Zheng Liuedaa53c2014-11-25 11:45:37 -0500875 __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500876 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400877 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -0500878 goto out;
879 }
880 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500881 es->es_lblk = end + 1;
882 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500883 if (ext4_es_is_written(es) ||
884 ext4_es_is_unwritten(es)) {
885 block = orig_es.es_pblk + orig_es.es_len - len2;
886 ext4_es_store_pblock(es, block);
887 }
Zheng Liu654598b2012-11-08 21:57:20 -0500888 }
889 goto out;
890 }
891
892 if (len1 > 0) {
893 node = rb_next(&es->rb_node);
894 if (node)
895 es = rb_entry(node, struct extent_status, rb_node);
896 else
897 es = NULL;
898 }
899
Zheng Liu06b0c882013-02-18 00:26:51 -0500900 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500901 node = rb_next(&es->rb_node);
902 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500903 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500904 if (!node) {
905 es = NULL;
906 break;
907 }
908 es = rb_entry(node, struct extent_status, rb_node);
909 }
910
Zheng Liu06b0c882013-02-18 00:26:51 -0500911 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -0500912 ext4_lblk_t orig_len = es->es_len;
913
Zheng Liu06b0c882013-02-18 00:26:51 -0500914 len1 = ext4_es_end(es) - end;
915 es->es_lblk = end + 1;
916 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -0500917 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
918 block = es->es_pblk + orig_len - len1;
919 ext4_es_store_pblock(es, block);
920 }
Zheng Liu654598b2012-11-08 21:57:20 -0500921 }
922
923out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500924 return err;
925}
926
927/*
928 * ext4_es_remove_extent() removes a space from a extent status tree.
929 *
930 * Return 0 on success, error code on failure.
931 */
932int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
933 ext4_lblk_t len)
934{
Zheng Liu06b0c882013-02-18 00:26:51 -0500935 ext4_lblk_t end;
936 int err = 0;
937
938 trace_ext4_es_remove_extent(inode, lblk, len);
939 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
940 lblk, len, inode->i_ino);
941
Eryu Guand4381472013-02-22 15:27:47 -0500942 if (!len)
943 return err;
944
Zheng Liu06b0c882013-02-18 00:26:51 -0500945 end = lblk + len - 1;
946 BUG_ON(end < lblk);
947
Zheng Liuedaa53c2014-11-25 11:45:37 -0500948 /*
949 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
950 * so that we are sure __es_shrink() is done with the inode before it
951 * is reclaimed.
952 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500953 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500954 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500955 write_unlock(&EXT4_I(inode)->i_es_lock);
956 ext4_es_print_tree(inode);
957 return err;
958}
Zheng Liu74cd15c2013-02-18 00:32:55 -0500959
Zheng Liuedaa53c2014-11-25 11:45:37 -0500960static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
961 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -0500962{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500963 struct ext4_inode_info *ei;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400964 struct ext4_es_stats *es_stats;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400965 ktime_t start_time;
966 u64 scan_time;
Zheng Liuedaa53c2014-11-25 11:45:37 -0500967 int nr_to_walk;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000968 int nr_shrunk = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -0500969 int retried = 0, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500970
Zheng Liueb68d0e2014-09-01 22:26:49 -0400971 es_stats = &sbi->s_es_stats;
972 start_time = ktime_get();
Zheng Liud3922a72013-07-01 08:12:37 -0400973
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400974retry:
Zheng Liuedaa53c2014-11-25 11:45:37 -0500975 spin_lock(&sbi->s_es_lock);
976 nr_to_walk = sbi->s_es_nr_inode;
977 while (nr_to_walk-- > 0) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500978 if (list_empty(&sbi->s_es_list)) {
979 spin_unlock(&sbi->s_es_lock);
980 goto out;
981 }
982 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
983 i_es_list);
984 /* Move the inode to the tail */
Jan Karadd475922014-11-25 11:51:23 -0500985 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500986
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400987 /*
Zheng Liuedaa53c2014-11-25 11:45:37 -0500988 * Normally we try hard to avoid shrinking precached inodes,
989 * but we will as a last resort.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400990 */
Zheng Liuedaa53c2014-11-25 11:45:37 -0500991 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
992 EXT4_STATE_EXT_PRECACHED)) {
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400993 nr_skipped++;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500994 continue;
995 }
Zheng Liud3922a72013-07-01 08:12:37 -0400996
Zheng Liuedaa53c2014-11-25 11:45:37 -0500997 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
998 nr_skipped++;
Zheng Liud3922a72013-07-01 08:12:37 -0400999 continue;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001000 }
1001 /*
1002 * Now we hold i_es_lock which protects us from inode reclaim
1003 * freeing inode under us
1004 */
1005 spin_unlock(&sbi->s_es_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001006
Jan Karadd475922014-11-25 11:51:23 -05001007 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001008 write_unlock(&ei->i_es_lock);
1009
Jan Karadd475922014-11-25 11:51:23 -05001010 if (nr_to_scan <= 0)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001011 goto out;
1012 spin_lock(&sbi->s_es_lock);
1013 }
1014 spin_unlock(&sbi->s_es_lock);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001015
1016 /*
1017 * If we skipped any inodes, and we weren't able to make any
Zheng Liuedaa53c2014-11-25 11:45:37 -05001018 * forward progress, try again to scan precached inodes.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001019 */
1020 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1021 retried++;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001022 goto retry;
1023 }
1024
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001025 if (locked_ei && nr_shrunk == 0)
Jan Karadd475922014-11-25 11:51:23 -05001026 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001027
Zheng Liuedaa53c2014-11-25 11:45:37 -05001028out:
Zheng Liueb68d0e2014-09-01 22:26:49 -04001029 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1030 if (likely(es_stats->es_stats_scan_time))
1031 es_stats->es_stats_scan_time = (scan_time +
1032 es_stats->es_stats_scan_time*3) / 4;
1033 else
1034 es_stats->es_stats_scan_time = scan_time;
1035 if (scan_time > es_stats->es_stats_max_scan_time)
1036 es_stats->es_stats_max_scan_time = scan_time;
1037 if (likely(es_stats->es_stats_shrunk))
1038 es_stats->es_stats_shrunk = (nr_shrunk +
1039 es_stats->es_stats_shrunk*3) / 4;
1040 else
1041 es_stats->es_stats_shrunk = nr_shrunk;
1042
Zheng Liuedaa53c2014-11-25 11:45:37 -05001043 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001044 nr_skipped, retried);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001045 return nr_shrunk;
1046}
1047
Dave Chinner1ab6c492013-08-28 10:18:09 +10001048static unsigned long ext4_es_count(struct shrinker *shrink,
1049 struct shrink_control *sc)
1050{
1051 unsigned long nr;
1052 struct ext4_sb_info *sbi;
1053
1054 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001055 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001056 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001057 return nr;
1058}
1059
1060static unsigned long ext4_es_scan(struct shrinker *shrink,
1061 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001062{
1063 struct ext4_sb_info *sbi = container_of(shrink,
1064 struct ext4_sb_info, s_es_shrinker);
1065 int nr_to_scan = sc->nr_to_scan;
1066 int ret, nr_shrunk;
1067
Zheng Liuedaa53c2014-11-25 11:45:37 -05001068 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001069 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001070
1071 if (!nr_to_scan)
1072 return ret;
1073
Zheng Liuedaa53c2014-11-25 11:45:37 -05001074 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001075
Zheng Liue963bb12014-09-01 22:22:13 -04001076 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001077 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001078}
1079
Zheng Liueb68d0e2014-09-01 22:26:49 -04001080static void *ext4_es_seq_shrinker_info_start(struct seq_file *seq, loff_t *pos)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001081{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001082 return *pos ? NULL : SEQ_START_TOKEN;
1083}
1084
1085static void *
1086ext4_es_seq_shrinker_info_next(struct seq_file *seq, void *v, loff_t *pos)
1087{
1088 return NULL;
1089}
1090
1091static int ext4_es_seq_shrinker_info_show(struct seq_file *seq, void *v)
1092{
1093 struct ext4_sb_info *sbi = seq->private;
1094 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1095 struct ext4_inode_info *ei, *max = NULL;
1096 unsigned int inode_cnt = 0;
1097
1098 if (v != SEQ_START_TOKEN)
1099 return 0;
1100
1101 /* here we just find an inode that has the max nr. of objects */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001102 spin_lock(&sbi->s_es_lock);
1103 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
Zheng Liueb68d0e2014-09-01 22:26:49 -04001104 inode_cnt++;
1105 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1106 max = ei;
1107 else if (!max)
1108 max = ei;
1109 }
Zheng Liuedaa53c2014-11-25 11:45:37 -05001110 spin_unlock(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001111
1112 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1113 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
Zheng Liuedaa53c2014-11-25 11:45:37 -05001114 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
Zheng Liueb68d0e2014-09-01 22:26:49 -04001115 seq_printf(seq, " %lu/%lu cache hits/misses\n",
1116 es_stats->es_stats_cache_hits,
1117 es_stats->es_stats_cache_misses);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001118 if (inode_cnt)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001119 seq_printf(seq, " %d inodes on list\n", inode_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001120
1121 seq_printf(seq, "average:\n %llu us scan time\n",
1122 div_u64(es_stats->es_stats_scan_time, 1000));
1123 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1124 if (inode_cnt)
1125 seq_printf(seq,
1126 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1127 " %llu us max scan time\n",
Zheng Liuedaa53c2014-11-25 11:45:37 -05001128 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001129 div_u64(es_stats->es_stats_max_scan_time, 1000));
1130
1131 return 0;
1132}
1133
1134static void ext4_es_seq_shrinker_info_stop(struct seq_file *seq, void *v)
1135{
1136}
1137
1138static const struct seq_operations ext4_es_seq_shrinker_info_ops = {
1139 .start = ext4_es_seq_shrinker_info_start,
1140 .next = ext4_es_seq_shrinker_info_next,
1141 .stop = ext4_es_seq_shrinker_info_stop,
1142 .show = ext4_es_seq_shrinker_info_show,
1143};
1144
1145static int
1146ext4_es_seq_shrinker_info_open(struct inode *inode, struct file *file)
1147{
1148 int ret;
1149
1150 ret = seq_open(file, &ext4_es_seq_shrinker_info_ops);
1151 if (!ret) {
1152 struct seq_file *m = file->private_data;
1153 m->private = PDE_DATA(inode);
1154 }
1155
1156 return ret;
1157}
1158
1159static int
1160ext4_es_seq_shrinker_info_release(struct inode *inode, struct file *file)
1161{
1162 return seq_release(inode, file);
1163}
1164
1165static const struct file_operations ext4_es_seq_shrinker_info_fops = {
1166 .owner = THIS_MODULE,
1167 .open = ext4_es_seq_shrinker_info_open,
1168 .read = seq_read,
1169 .llseek = seq_lseek,
1170 .release = ext4_es_seq_shrinker_info_release,
1171};
1172
1173int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1174{
1175 int err;
1176
Jan Kara624d0f12014-11-25 11:53:47 -05001177 /* Make sure we have enough bits for physical block number */
1178 BUILD_BUG_ON(ES_SHIFT < 48);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001179 INIT_LIST_HEAD(&sbi->s_es_list);
1180 sbi->s_es_nr_inode = 0;
1181 spin_lock_init(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001182 sbi->s_es_stats.es_stats_shrunk = 0;
1183 sbi->s_es_stats.es_stats_cache_hits = 0;
1184 sbi->s_es_stats.es_stats_cache_misses = 0;
1185 sbi->s_es_stats.es_stats_scan_time = 0;
1186 sbi->s_es_stats.es_stats_max_scan_time = 0;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001187 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001188 if (err)
1189 return err;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001190 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001191 if (err)
1192 goto err1;
1193
Dave Chinner1ab6c492013-08-28 10:18:09 +10001194 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1195 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001196 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001197 err = register_shrinker(&sbi->s_es_shrinker);
1198 if (err)
1199 goto err2;
1200
1201 if (sbi->s_proc)
1202 proc_create_data("es_shrinker_info", S_IRUGO, sbi->s_proc,
1203 &ext4_es_seq_shrinker_info_fops, sbi);
1204
1205 return 0;
1206
1207err2:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001208 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001209err1:
1210 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1211 return err;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001212}
1213
Zheng Liud3922a72013-07-01 08:12:37 -04001214void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001215{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001216 if (sbi->s_proc)
1217 remove_proc_entry("es_shrinker_info", sbi->s_proc);
1218 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001219 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liud3922a72013-07-01 08:12:37 -04001220 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001221}
1222
Jan Karadd475922014-11-25 11:51:23 -05001223/*
1224 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1225 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1226 *
1227 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1228 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1229 * ei->i_es_shrink_lblk to where we should continue scanning.
1230 */
1231static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1232 int *nr_to_scan, int *nr_shrunk)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001233{
1234 struct inode *inode = &ei->vfs_inode;
1235 struct ext4_es_tree *tree = &ei->i_es_tree;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001236 struct extent_status *es;
Jan Karadd475922014-11-25 11:51:23 -05001237 struct rb_node *node;
1238
1239 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1240 if (!es)
1241 goto out_wrap;
1242 node = &es->rb_node;
1243 while (*nr_to_scan > 0) {
1244 if (es->es_lblk > end) {
1245 ei->i_es_shrink_lblk = end + 1;
1246 return 0;
1247 }
1248
1249 (*nr_to_scan)--;
1250 node = rb_next(&es->rb_node);
1251 /*
1252 * We can't reclaim delayed extent from status tree because
1253 * fiemap, bigallic, and seek_data/hole need to use it.
1254 */
1255 if (!ext4_es_is_delayed(es)) {
1256 rb_erase(&es->rb_node, &tree->root);
1257 ext4_es_free_extent(inode, es);
1258 (*nr_shrunk)++;
1259 }
1260 if (!node)
1261 goto out_wrap;
1262 es = rb_entry(node, struct extent_status, rb_node);
1263 }
1264 ei->i_es_shrink_lblk = es->es_lblk;
1265 return 1;
1266out_wrap:
1267 ei->i_es_shrink_lblk = 0;
1268 return 0;
1269}
1270
1271static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1272{
1273 struct inode *inode = &ei->vfs_inode;
1274 int nr_shrunk = 0;
1275 ext4_lblk_t start = ei->i_es_shrink_lblk;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001276 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1277 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001278
Zheng Liuedaa53c2014-11-25 11:45:37 -05001279 if (ei->i_es_shk_nr == 0)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001280 return 0;
1281
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001282 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1283 __ratelimit(&_rs))
1284 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1285
Jan Karadd475922014-11-25 11:51:23 -05001286 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1287 start != 0)
1288 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1289
1290 ei->i_es_tree.cache_es = NULL;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001291 return nr_shrunk;
1292}