blob: 94e7855ae71b03e26559e02cbe6491e9d8be2767 [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);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500150static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
151 int nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400152static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
153 struct ext4_inode_info *locked_ei);
Zheng Liu06b0c882013-02-18 00:26:51 -0500154
Zheng Liu654598b2012-11-08 21:57:20 -0500155int __init ext4_init_es(void)
156{
Theodore Ts'o24630772013-02-28 23:58:56 -0500157 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
158 sizeof(struct extent_status),
159 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500160 if (ext4_es_cachep == NULL)
161 return -ENOMEM;
162 return 0;
163}
164
165void ext4_exit_es(void)
166{
167 if (ext4_es_cachep)
168 kmem_cache_destroy(ext4_es_cachep);
169}
170
171void ext4_es_init_tree(struct ext4_es_tree *tree)
172{
173 tree->root = RB_ROOT;
174 tree->cache_es = NULL;
175}
176
177#ifdef ES_DEBUG__
178static void ext4_es_print_tree(struct inode *inode)
179{
180 struct ext4_es_tree *tree;
181 struct rb_node *node;
182
183 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
184 tree = &EXT4_I(inode)->i_es_tree;
185 node = rb_first(&tree->root);
186 while (node) {
187 struct extent_status *es;
188 es = rb_entry(node, struct extent_status, rb_node);
Eric Whitneyce140cd2014-02-20 16:09:12 -0500189 printk(KERN_DEBUG " [%u/%u) %llu %x",
Zheng Liufdc02122013-02-18 00:26:51 -0500190 es->es_lblk, es->es_len,
191 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500192 node = rb_next(node);
193 }
194 printk(KERN_DEBUG "\n");
195}
196#else
197#define ext4_es_print_tree(inode)
198#endif
199
Zheng Liu06b0c882013-02-18 00:26:51 -0500200static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500201{
Zheng Liu06b0c882013-02-18 00:26:51 -0500202 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
203 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500204}
205
206/*
207 * search through the tree for an delayed extent with a given offset. If
208 * it can't be found, try to find next extent.
209 */
210static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500211 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500212{
213 struct rb_node *node = root->rb_node;
214 struct extent_status *es = NULL;
215
216 while (node) {
217 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500218 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500219 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500220 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500221 node = node->rb_right;
222 else
223 return es;
224 }
225
Zheng Liu06b0c882013-02-18 00:26:51 -0500226 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500227 return es;
228
Zheng Liu06b0c882013-02-18 00:26:51 -0500229 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500230 node = rb_next(&es->rb_node);
231 return node ? rb_entry(node, struct extent_status, rb_node) :
232 NULL;
233 }
234
235 return NULL;
236}
237
238/*
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400239 * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
240 * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500241 *
242 * @inode: the inode which owns delayed extents
Zheng Liube401362013-02-18 00:27:26 -0500243 * @lblk: the offset where we start to search
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400244 * @end: the offset where we stop to search
Zheng Liu654598b2012-11-08 21:57:20 -0500245 * @es: delayed extent that we found
Zheng Liu654598b2012-11-08 21:57:20 -0500246 */
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400247void ext4_es_find_delayed_extent_range(struct inode *inode,
248 ext4_lblk_t lblk, ext4_lblk_t end,
Zheng Liube401362013-02-18 00:27:26 -0500249 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500250{
251 struct ext4_es_tree *tree = NULL;
252 struct extent_status *es1 = NULL;
253 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500254
Zheng Liube401362013-02-18 00:27:26 -0500255 BUG_ON(es == NULL);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400256 BUG_ON(end < lblk);
257 trace_ext4_es_find_delayed_extent_range_enter(inode, lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500258
Zheng Liu654598b2012-11-08 21:57:20 -0500259 read_lock(&EXT4_I(inode)->i_es_lock);
260 tree = &EXT4_I(inode)->i_es_tree;
261
Zheng Liufdc02122013-02-18 00:26:51 -0500262 /* find extent in cache firstly */
Zheng Liube401362013-02-18 00:27:26 -0500263 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500264 if (tree->cache_es) {
265 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500266 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400267 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500268 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500269 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500270 goto out;
271 }
272 }
273
Zheng Liube401362013-02-18 00:27:26 -0500274 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500275
276out:
Zheng Liube401362013-02-18 00:27:26 -0500277 if (es1 && !ext4_es_is_delayed(es1)) {
278 while ((node = rb_next(&es1->rb_node)) != NULL) {
279 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400280 if (es1->es_lblk > end) {
281 es1 = NULL;
282 break;
283 }
Zheng Liube401362013-02-18 00:27:26 -0500284 if (ext4_es_is_delayed(es1))
285 break;
286 }
287 }
288
289 if (es1 && ext4_es_is_delayed(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500290 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500291 es->es_lblk = es1->es_lblk;
292 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500293 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500294 }
295
296 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500297
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400298 trace_ext4_es_find_delayed_extent_range_exit(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500299}
300
301static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500302ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
303 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500304{
305 struct extent_status *es;
306 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
307 if (es == NULL)
308 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500309 es->es_lblk = lblk;
310 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500311 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500312
313 /*
314 * We don't count delayed extent because we never try to reclaim them
315 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500316 if (!ext4_es_is_delayed(es)) {
Zheng Liu74cd15c2013-02-18 00:32:55 -0500317 EXT4_I(inode)->i_es_lru_nr++;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400318 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
319 s_es_stats.es_stats_lru_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500320 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500321
Zheng Liueb68d0e2014-09-01 22:26:49 -0400322 EXT4_I(inode)->i_es_all_nr++;
323 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
324
Zheng Liu654598b2012-11-08 21:57:20 -0500325 return es;
326}
327
Zheng Liubdedbb72013-02-18 00:32:02 -0500328static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500329{
Zheng Liueb68d0e2014-09-01 22:26:49 -0400330 EXT4_I(inode)->i_es_all_nr--;
331 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
332
Zheng Liu74cd15c2013-02-18 00:32:55 -0500333 /* Decrease the lru counter when this es is not delayed */
334 if (!ext4_es_is_delayed(es)) {
335 BUG_ON(EXT4_I(inode)->i_es_lru_nr == 0);
336 EXT4_I(inode)->i_es_lru_nr--;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400337 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
338 s_es_stats.es_stats_lru_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500339 }
340
Zheng Liu654598b2012-11-08 21:57:20 -0500341 kmem_cache_free(ext4_es_cachep, es);
342}
343
Zheng Liu06b0c882013-02-18 00:26:51 -0500344/*
345 * Check whether or not two extents can be merged
346 * Condition:
347 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500348 * - physical block number is contiguous
349 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500350 */
351static int ext4_es_can_be_merged(struct extent_status *es1,
352 struct extent_status *es2)
353{
Zheng Liufdc02122013-02-18 00:26:51 -0500354 if (ext4_es_status(es1) != ext4_es_status(es2))
355 return 0;
356
Lukas Czerner0baaea62014-05-12 22:21:43 -0400357 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
358 pr_warn("ES assertion failed when merging extents. "
359 "The sum of lengths of es1 (%d) and es2 (%d) "
360 "is bigger than allowed file size (%d)\n",
361 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
362 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500363 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400364 }
Zheng Liufdc02122013-02-18 00:26:51 -0500365
Zheng Liubd384362013-03-10 20:48:59 -0400366 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
367 return 0;
368
369 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
370 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
371 return 1;
372
373 if (ext4_es_is_hole(es1))
374 return 1;
375
376 /* we need to check delayed extent is without unwritten status */
377 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
378 return 1;
379
380 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500381}
382
Zheng Liu654598b2012-11-08 21:57:20 -0500383static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500384ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500385{
Zheng Liubdedbb72013-02-18 00:32:02 -0500386 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500387 struct extent_status *es1;
388 struct rb_node *node;
389
390 node = rb_prev(&es->rb_node);
391 if (!node)
392 return es;
393
394 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500395 if (ext4_es_can_be_merged(es1, es)) {
396 es1->es_len += es->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500397 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500398 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500399 es = es1;
400 }
401
402 return es;
403}
404
405static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500406ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500407{
Zheng Liubdedbb72013-02-18 00:32:02 -0500408 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500409 struct extent_status *es1;
410 struct rb_node *node;
411
412 node = rb_next(&es->rb_node);
413 if (!node)
414 return es;
415
416 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500417 if (ext4_es_can_be_merged(es, es1)) {
418 es->es_len += es1->es_len;
Zheng Liu654598b2012-11-08 21:57:20 -0500419 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500420 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500421 }
422
423 return es;
424}
425
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400426#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400427#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
428
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400429static void ext4_es_insert_extent_ext_check(struct inode *inode,
430 struct extent_status *es)
431{
432 struct ext4_ext_path *path = NULL;
433 struct ext4_extent *ex;
434 ext4_lblk_t ee_block;
435 ext4_fsblk_t ee_start;
436 unsigned short ee_len;
437 int depth, ee_status, es_status;
438
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400439 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400440 if (IS_ERR(path))
441 return;
442
443 depth = ext_depth(inode);
444 ex = path[depth].p_ext;
445
446 if (ex) {
447
448 ee_block = le32_to_cpu(ex->ee_block);
449 ee_start = ext4_ext_pblock(ex);
450 ee_len = ext4_ext_get_actual_len(ex);
451
Lukas Czerner556615d2014-04-20 23:45:47 -0400452 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400453 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
454
455 /*
456 * Make sure ex and es are not overlap when we try to insert
457 * a delayed/hole extent.
458 */
459 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
460 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400461 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400462 "inode: %lu we can find an extent "
463 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500464 "want to add a delayed/hole extent "
465 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400466 inode->i_ino, ee_block, ee_len,
467 ee_start, ee_status ? 'u' : 'w',
468 es->es_lblk, es->es_len,
469 ext4_es_pblock(es), ext4_es_status(es));
470 }
471 goto out;
472 }
473
474 /*
475 * We don't check ee_block == es->es_lblk, etc. because es
476 * might be a part of whole extent, vice versa.
477 */
478 if (es->es_lblk < ee_block ||
479 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400480 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400481 "ex_status [%d/%d/%llu/%c] != "
482 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
483 ee_block, ee_len, ee_start,
484 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
485 ext4_es_pblock(es), es_status ? 'u' : 'w');
486 goto out;
487 }
488
489 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400490 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400491 "ex_status [%d/%d/%llu/%c] != "
492 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
493 ee_block, ee_len, ee_start,
494 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
495 ext4_es_pblock(es), es_status ? 'u' : 'w');
496 }
497 } else {
498 /*
499 * We can't find an extent on disk. So we need to make sure
500 * that we don't want to add an written/unwritten extent.
501 */
502 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400503 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400504 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500505 "to add a written/unwritten extent "
506 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400507 es->es_lblk, es->es_lblk, es->es_len,
508 ext4_es_pblock(es), ext4_es_status(es));
509 }
510 }
511out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400512 ext4_ext_drop_refs(path);
513 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400514}
515
516static void ext4_es_insert_extent_ind_check(struct inode *inode,
517 struct extent_status *es)
518{
519 struct ext4_map_blocks map;
520 int retval;
521
522 /*
523 * Here we call ext4_ind_map_blocks to lookup a block mapping because
524 * 'Indirect' structure is defined in indirect.c. So we couldn't
525 * access direct/indirect tree from outside. It is too dirty to define
526 * this function in indirect.c file.
527 */
528
529 map.m_lblk = es->es_lblk;
530 map.m_len = es->es_len;
531
532 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
533 if (retval > 0) {
534 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
535 /*
536 * We want to add a delayed/hole extent but this
537 * block has been allocated.
538 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400539 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400540 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500541 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400542 inode->i_ino, es->es_lblk, es->es_len,
543 ext4_es_pblock(es), ext4_es_status(es));
544 return;
545 } else if (ext4_es_is_written(es)) {
546 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400547 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400548 "inode: %lu retval %d != es_len %d\n",
549 inode->i_ino, retval, es->es_len);
550 return;
551 }
552 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400553 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400554 "inode: %lu m_pblk %llu != "
555 "es_pblk %llu\n",
556 inode->i_ino, map.m_pblk,
557 ext4_es_pblock(es));
558 return;
559 }
560 } else {
561 /*
562 * We don't need to check unwritten extent because
563 * indirect-based file doesn't have it.
564 */
565 BUG_ON(1);
566 }
567 } else if (retval == 0) {
568 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400569 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400570 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500571 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400572 inode->i_ino, es->es_lblk, es->es_len,
573 ext4_es_pblock(es), ext4_es_status(es));
574 return;
575 }
576 }
577}
578
579static inline void ext4_es_insert_extent_check(struct inode *inode,
580 struct extent_status *es)
581{
582 /*
583 * We don't need to worry about the race condition because
584 * caller takes i_data_sem locking.
585 */
586 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
587 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
588 ext4_es_insert_extent_ext_check(inode, es);
589 else
590 ext4_es_insert_extent_ind_check(inode, es);
591}
592#else
593static inline void ext4_es_insert_extent_check(struct inode *inode,
594 struct extent_status *es)
595{
596}
597#endif
598
Zheng Liubdedbb72013-02-18 00:32:02 -0500599static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500600{
Zheng Liubdedbb72013-02-18 00:32:02 -0500601 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500602 struct rb_node **p = &tree->root.rb_node;
603 struct rb_node *parent = NULL;
604 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500605
606 while (*p) {
607 parent = *p;
608 es = rb_entry(parent, struct extent_status, rb_node);
609
Zheng Liu06b0c882013-02-18 00:26:51 -0500610 if (newes->es_lblk < es->es_lblk) {
611 if (ext4_es_can_be_merged(newes, es)) {
612 /*
613 * Here we can modify es_lblk directly
614 * because it isn't overlapped.
615 */
616 es->es_lblk = newes->es_lblk;
617 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500618 if (ext4_es_is_written(es) ||
619 ext4_es_is_unwritten(es))
620 ext4_es_store_pblock(es,
621 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500622 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500623 goto out;
624 }
625 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500626 } else if (newes->es_lblk > ext4_es_end(es)) {
627 if (ext4_es_can_be_merged(es, newes)) {
628 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500629 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500630 goto out;
631 }
632 p = &(*p)->rb_right;
633 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500634 BUG_ON(1);
635 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500636 }
637 }
638
Zheng Liubdedbb72013-02-18 00:32:02 -0500639 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500640 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500641 if (!es)
642 return -ENOMEM;
643 rb_link_node(&es->rb_node, parent, p);
644 rb_insert_color(&es->rb_node, &tree->root);
645
646out:
647 tree->cache_es = es;
648 return 0;
649}
650
651/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400652 * ext4_es_insert_extent() adds information to an inode's extent
653 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500654 *
655 * Return 0 on success, error code on failure.
656 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500657int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500658 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400659 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500660{
Zheng Liu06b0c882013-02-18 00:26:51 -0500661 struct extent_status newes;
662 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500663 int err = 0;
664
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400665 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500666 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500667
Eryu Guand4381472013-02-22 15:27:47 -0500668 if (!len)
669 return 0;
670
Zheng Liu06b0c882013-02-18 00:26:51 -0500671 BUG_ON(end < lblk);
672
673 newes.es_lblk = lblk;
674 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500675 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500676 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500677
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400678 ext4_es_insert_extent_check(inode, &newes);
679
Zheng Liu654598b2012-11-08 21:57:20 -0500680 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500681 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500682 if (err != 0)
683 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400684retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500685 err = __es_insert_extent(inode, &newes);
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400686 if (err == -ENOMEM && __ext4_es_shrink(EXT4_SB(inode->i_sb), 1,
687 EXT4_I(inode)))
688 goto retry;
689 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
690 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500691
692error:
Zheng Liu654598b2012-11-08 21:57:20 -0500693 write_unlock(&EXT4_I(inode)->i_es_lock);
694
695 ext4_es_print_tree(inode);
696
697 return err;
698}
699
Zheng Liud100eef2013-02-18 00:29:59 -0500700/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400701 * ext4_es_cache_extent() inserts information into the extent status
702 * tree if and only if there isn't information about the range in
703 * question already.
704 */
705void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
706 ext4_lblk_t len, ext4_fsblk_t pblk,
707 unsigned int status)
708{
709 struct extent_status *es;
710 struct extent_status newes;
711 ext4_lblk_t end = lblk + len - 1;
712
713 newes.es_lblk = lblk;
714 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500715 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400716 trace_ext4_es_cache_extent(inode, &newes);
717
718 if (!len)
719 return;
720
721 BUG_ON(end < lblk);
722
723 write_lock(&EXT4_I(inode)->i_es_lock);
724
725 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400726 if (!es || es->es_lblk > end)
727 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400728 write_unlock(&EXT4_I(inode)->i_es_lock);
729}
730
731/*
Zheng Liud100eef2013-02-18 00:29:59 -0500732 * ext4_es_lookup_extent() looks up an extent in extent status tree.
733 *
734 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
735 *
736 * Return: 1 on found, 0 on not
737 */
738int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
739 struct extent_status *es)
740{
741 struct ext4_es_tree *tree;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400742 struct ext4_es_stats *stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500743 struct extent_status *es1 = NULL;
744 struct rb_node *node;
745 int found = 0;
746
747 trace_ext4_es_lookup_extent_enter(inode, lblk);
748 es_debug("lookup extent in block %u\n", lblk);
749
750 tree = &EXT4_I(inode)->i_es_tree;
751 read_lock(&EXT4_I(inode)->i_es_lock);
752
753 /* find extent in cache firstly */
754 es->es_lblk = es->es_len = es->es_pblk = 0;
755 if (tree->cache_es) {
756 es1 = tree->cache_es;
757 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
758 es_debug("%u cached by [%u/%u)\n",
759 lblk, es1->es_lblk, es1->es_len);
760 found = 1;
761 goto out;
762 }
763 }
764
765 node = tree->root.rb_node;
766 while (node) {
767 es1 = rb_entry(node, struct extent_status, rb_node);
768 if (lblk < es1->es_lblk)
769 node = node->rb_left;
770 else if (lblk > ext4_es_end(es1))
771 node = node->rb_right;
772 else {
773 found = 1;
774 break;
775 }
776 }
777
778out:
Zheng Liueb68d0e2014-09-01 22:26:49 -0400779 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500780 if (found) {
781 BUG_ON(!es1);
782 es->es_lblk = es1->es_lblk;
783 es->es_len = es1->es_len;
784 es->es_pblk = es1->es_pblk;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400785 stats->es_stats_cache_hits++;
786 } else {
787 stats->es_stats_cache_misses++;
Zheng Liud100eef2013-02-18 00:29:59 -0500788 }
789
790 read_unlock(&EXT4_I(inode)->i_es_lock);
791
792 trace_ext4_es_lookup_extent_exit(inode, es, found);
793 return found;
794}
795
Zheng Liubdedbb72013-02-18 00:32:02 -0500796static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
797 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500798{
Zheng Liubdedbb72013-02-18 00:32:02 -0500799 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500800 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500801 struct extent_status *es;
802 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500803 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500804 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400805 int err;
Zheng Liu654598b2012-11-08 21:57:20 -0500806
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400807retry:
808 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500809 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500810 if (!es)
811 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500812 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500813 goto out;
814
815 /* Simply invalidate cache_es. */
816 tree->cache_es = NULL;
817
Zheng Liu06b0c882013-02-18 00:26:51 -0500818 orig_es.es_lblk = es->es_lblk;
819 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500820 orig_es.es_pblk = es->es_pblk;
821
Zheng Liu06b0c882013-02-18 00:26:51 -0500822 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
823 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500824 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500825 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500826 if (len2 > 0) {
827 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500828 struct extent_status newes;
829
830 newes.es_lblk = end + 1;
831 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -0400832 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -0500833 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500834 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -0500835 block = ext4_es_pblock(&orig_es) +
836 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500837 ext4_es_store_pblock_status(&newes, block,
838 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500839 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500840 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500841 es->es_lblk = orig_es.es_lblk;
842 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400843 if ((err == -ENOMEM) &&
844 __ext4_es_shrink(EXT4_SB(inode->i_sb), 1,
845 EXT4_I(inode)))
846 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -0500847 goto out;
848 }
849 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500850 es->es_lblk = end + 1;
851 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500852 if (ext4_es_is_written(es) ||
853 ext4_es_is_unwritten(es)) {
854 block = orig_es.es_pblk + orig_es.es_len - len2;
855 ext4_es_store_pblock(es, block);
856 }
Zheng Liu654598b2012-11-08 21:57:20 -0500857 }
858 goto out;
859 }
860
861 if (len1 > 0) {
862 node = rb_next(&es->rb_node);
863 if (node)
864 es = rb_entry(node, struct extent_status, rb_node);
865 else
866 es = NULL;
867 }
868
Zheng Liu06b0c882013-02-18 00:26:51 -0500869 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500870 node = rb_next(&es->rb_node);
871 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500872 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500873 if (!node) {
874 es = NULL;
875 break;
876 }
877 es = rb_entry(node, struct extent_status, rb_node);
878 }
879
Zheng Liu06b0c882013-02-18 00:26:51 -0500880 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -0500881 ext4_lblk_t orig_len = es->es_len;
882
Zheng Liu06b0c882013-02-18 00:26:51 -0500883 len1 = ext4_es_end(es) - end;
884 es->es_lblk = end + 1;
885 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -0500886 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
887 block = es->es_pblk + orig_len - len1;
888 ext4_es_store_pblock(es, block);
889 }
Zheng Liu654598b2012-11-08 21:57:20 -0500890 }
891
892out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500893 return err;
894}
895
896/*
897 * ext4_es_remove_extent() removes a space from a extent status tree.
898 *
899 * Return 0 on success, error code on failure.
900 */
901int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
902 ext4_lblk_t len)
903{
Zheng Liu06b0c882013-02-18 00:26:51 -0500904 ext4_lblk_t end;
905 int err = 0;
906
907 trace_ext4_es_remove_extent(inode, lblk, len);
908 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
909 lblk, len, inode->i_ino);
910
Eryu Guand4381472013-02-22 15:27:47 -0500911 if (!len)
912 return err;
913
Zheng Liu06b0c882013-02-18 00:26:51 -0500914 end = lblk + len - 1;
915 BUG_ON(end < lblk);
916
Zheng Liu06b0c882013-02-18 00:26:51 -0500917 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500918 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500919 write_unlock(&EXT4_I(inode)->i_es_lock);
920 ext4_es_print_tree(inode);
921 return err;
922}
Zheng Liu74cd15c2013-02-18 00:32:55 -0500923
Zheng Liud3922a72013-07-01 08:12:37 -0400924static int ext4_inode_touch_time_cmp(void *priv, struct list_head *a,
925 struct list_head *b)
926{
927 struct ext4_inode_info *eia, *eib;
928 eia = list_entry(a, struct ext4_inode_info, i_es_lru);
929 eib = list_entry(b, struct ext4_inode_info, i_es_lru);
930
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400931 if (ext4_test_inode_state(&eia->vfs_inode, EXT4_STATE_EXT_PRECACHED) &&
932 !ext4_test_inode_state(&eib->vfs_inode, EXT4_STATE_EXT_PRECACHED))
933 return 1;
934 if (!ext4_test_inode_state(&eia->vfs_inode, EXT4_STATE_EXT_PRECACHED) &&
935 ext4_test_inode_state(&eib->vfs_inode, EXT4_STATE_EXT_PRECACHED))
936 return -1;
Zheng Liud3922a72013-07-01 08:12:37 -0400937 if (eia->i_touch_when == eib->i_touch_when)
938 return 0;
939 if (time_after(eia->i_touch_when, eib->i_touch_when))
940 return 1;
941 else
942 return -1;
943}
944
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400945static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
946 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -0500947{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500948 struct ext4_inode_info *ei;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400949 struct ext4_es_stats *es_stats;
Zheng Liud3922a72013-07-01 08:12:37 -0400950 struct list_head *cur, *tmp;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400951 LIST_HEAD(skipped);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400952 ktime_t start_time;
953 u64 scan_time;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000954 int nr_shrunk = 0;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400955 int retried = 0, skip_precached = 1, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500956
Zheng Liueb68d0e2014-09-01 22:26:49 -0400957 es_stats = &sbi->s_es_stats;
958 start_time = ktime_get();
Zheng Liu74cd15c2013-02-18 00:32:55 -0500959 spin_lock(&sbi->s_es_lru_lock);
Zheng Liud3922a72013-07-01 08:12:37 -0400960
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400961retry:
Zheng Liu74cd15c2013-02-18 00:32:55 -0500962 list_for_each_safe(cur, tmp, &sbi->s_es_lru) {
Dave Chinner1ab6c492013-08-28 10:18:09 +1000963 int shrunk;
964
Zheng Liud3922a72013-07-01 08:12:37 -0400965 /*
966 * If we have already reclaimed all extents from extent
967 * status tree, just stop the loop immediately.
968 */
Zheng Liueb68d0e2014-09-01 22:26:49 -0400969 if (percpu_counter_read_positive(
970 &es_stats->es_stats_lru_cnt) == 0)
Zheng Liud3922a72013-07-01 08:12:37 -0400971 break;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500972
973 ei = list_entry(cur, struct ext4_inode_info, i_es_lru);
974
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400975 /*
976 * Skip the inode that is newer than the last_sorted
977 * time. Normally we try hard to avoid shrinking
978 * precached inodes, but we will as a last resort.
979 */
Zheng Liueb68d0e2014-09-01 22:26:49 -0400980 if ((es_stats->es_stats_last_sorted < ei->i_touch_when) ||
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400981 (skip_precached && ext4_test_inode_state(&ei->vfs_inode,
982 EXT4_STATE_EXT_PRECACHED))) {
983 nr_skipped++;
984 list_move_tail(cur, &skipped);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500985 continue;
986 }
Zheng Liud3922a72013-07-01 08:12:37 -0400987
Theodore Ts'o3f1f9b82014-07-12 15:32:24 -0400988 if (ei->i_es_lru_nr == 0 || ei == locked_ei ||
989 !write_trylock(&ei->i_es_lock))
Zheng Liud3922a72013-07-01 08:12:37 -0400990 continue;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500991
Dave Chinner1ab6c492013-08-28 10:18:09 +1000992 shrunk = __es_try_to_reclaim_extents(ei, nr_to_scan);
Zheng Liud3922a72013-07-01 08:12:37 -0400993 if (ei->i_es_lru_nr == 0)
994 list_del_init(&ei->i_es_lru);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500995 write_unlock(&ei->i_es_lock);
996
Dave Chinner1ab6c492013-08-28 10:18:09 +1000997 nr_shrunk += shrunk;
998 nr_to_scan -= shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500999 if (nr_to_scan == 0)
1000 break;
1001 }
Zheng Liud3922a72013-07-01 08:12:37 -04001002
1003 /* Move the newer inodes into the tail of the LRU list. */
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001004 list_splice_tail(&skipped, &sbi->s_es_lru);
1005 INIT_LIST_HEAD(&skipped);
1006
1007 /*
1008 * If we skipped any inodes, and we weren't able to make any
1009 * forward progress, sort the list and try again.
1010 */
1011 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1012 retried++;
1013 list_sort(NULL, &sbi->s_es_lru, ext4_inode_touch_time_cmp);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001014 es_stats->es_stats_last_sorted = jiffies;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001015 ei = list_first_entry(&sbi->s_es_lru, struct ext4_inode_info,
1016 i_es_lru);
1017 /*
1018 * If there are no non-precached inodes left on the
1019 * list, start releasing precached extents.
1020 */
1021 if (ext4_test_inode_state(&ei->vfs_inode,
1022 EXT4_STATE_EXT_PRECACHED))
1023 skip_precached = 0;
1024 goto retry;
1025 }
1026
Zheng Liu74cd15c2013-02-18 00:32:55 -05001027 spin_unlock(&sbi->s_es_lru_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001028
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001029 if (locked_ei && nr_shrunk == 0)
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001030 nr_shrunk = __es_try_to_reclaim_extents(locked_ei, nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001031
Zheng Liueb68d0e2014-09-01 22:26:49 -04001032 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1033 if (likely(es_stats->es_stats_scan_time))
1034 es_stats->es_stats_scan_time = (scan_time +
1035 es_stats->es_stats_scan_time*3) / 4;
1036 else
1037 es_stats->es_stats_scan_time = scan_time;
1038 if (scan_time > es_stats->es_stats_max_scan_time)
1039 es_stats->es_stats_max_scan_time = scan_time;
1040 if (likely(es_stats->es_stats_shrunk))
1041 es_stats->es_stats_shrunk = (nr_shrunk +
1042 es_stats->es_stats_shrunk*3) / 4;
1043 else
1044 es_stats->es_stats_shrunk = nr_shrunk;
1045
1046 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time, skip_precached,
1047 nr_skipped, retried);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001048 return nr_shrunk;
1049}
1050
Dave Chinner1ab6c492013-08-28 10:18:09 +10001051static unsigned long ext4_es_count(struct shrinker *shrink,
1052 struct shrink_control *sc)
1053{
1054 unsigned long nr;
1055 struct ext4_sb_info *sbi;
1056
1057 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001058 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_lru_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001059 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001060 return nr;
1061}
1062
1063static unsigned long ext4_es_scan(struct shrinker *shrink,
1064 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001065{
1066 struct ext4_sb_info *sbi = container_of(shrink,
1067 struct ext4_sb_info, s_es_shrinker);
1068 int nr_to_scan = sc->nr_to_scan;
1069 int ret, nr_shrunk;
1070
Zheng Liueb68d0e2014-09-01 22:26:49 -04001071 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_lru_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001072 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001073
1074 if (!nr_to_scan)
1075 return ret;
1076
1077 nr_shrunk = __ext4_es_shrink(sbi, nr_to_scan, NULL);
1078
Zheng Liue963bb12014-09-01 22:22:13 -04001079 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001080 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001081}
1082
Zheng Liueb68d0e2014-09-01 22:26:49 -04001083static void *ext4_es_seq_shrinker_info_start(struct seq_file *seq, loff_t *pos)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001084{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001085 return *pos ? NULL : SEQ_START_TOKEN;
1086}
1087
1088static void *
1089ext4_es_seq_shrinker_info_next(struct seq_file *seq, void *v, loff_t *pos)
1090{
1091 return NULL;
1092}
1093
1094static int ext4_es_seq_shrinker_info_show(struct seq_file *seq, void *v)
1095{
1096 struct ext4_sb_info *sbi = seq->private;
1097 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1098 struct ext4_inode_info *ei, *max = NULL;
1099 unsigned int inode_cnt = 0;
1100
1101 if (v != SEQ_START_TOKEN)
1102 return 0;
1103
1104 /* here we just find an inode that has the max nr. of objects */
1105 spin_lock(&sbi->s_es_lru_lock);
1106 list_for_each_entry(ei, &sbi->s_es_lru, i_es_lru) {
1107 inode_cnt++;
1108 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1109 max = ei;
1110 else if (!max)
1111 max = ei;
1112 }
1113 spin_unlock(&sbi->s_es_lru_lock);
1114
1115 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1116 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
1117 percpu_counter_sum_positive(&es_stats->es_stats_lru_cnt));
1118 seq_printf(seq, " %lu/%lu cache hits/misses\n",
1119 es_stats->es_stats_cache_hits,
1120 es_stats->es_stats_cache_misses);
1121 if (es_stats->es_stats_last_sorted != 0)
1122 seq_printf(seq, " %u ms last sorted interval\n",
1123 jiffies_to_msecs(jiffies -
1124 es_stats->es_stats_last_sorted));
1125 if (inode_cnt)
1126 seq_printf(seq, " %d inodes on lru list\n", inode_cnt);
1127
1128 seq_printf(seq, "average:\n %llu us scan time\n",
1129 div_u64(es_stats->es_stats_scan_time, 1000));
1130 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1131 if (inode_cnt)
1132 seq_printf(seq,
1133 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1134 " %llu us max scan time\n",
1135 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_lru_nr,
1136 div_u64(es_stats->es_stats_max_scan_time, 1000));
1137
1138 return 0;
1139}
1140
1141static void ext4_es_seq_shrinker_info_stop(struct seq_file *seq, void *v)
1142{
1143}
1144
1145static const struct seq_operations ext4_es_seq_shrinker_info_ops = {
1146 .start = ext4_es_seq_shrinker_info_start,
1147 .next = ext4_es_seq_shrinker_info_next,
1148 .stop = ext4_es_seq_shrinker_info_stop,
1149 .show = ext4_es_seq_shrinker_info_show,
1150};
1151
1152static int
1153ext4_es_seq_shrinker_info_open(struct inode *inode, struct file *file)
1154{
1155 int ret;
1156
1157 ret = seq_open(file, &ext4_es_seq_shrinker_info_ops);
1158 if (!ret) {
1159 struct seq_file *m = file->private_data;
1160 m->private = PDE_DATA(inode);
1161 }
1162
1163 return ret;
1164}
1165
1166static int
1167ext4_es_seq_shrinker_info_release(struct inode *inode, struct file *file)
1168{
1169 return seq_release(inode, file);
1170}
1171
1172static const struct file_operations ext4_es_seq_shrinker_info_fops = {
1173 .owner = THIS_MODULE,
1174 .open = ext4_es_seq_shrinker_info_open,
1175 .read = seq_read,
1176 .llseek = seq_lseek,
1177 .release = ext4_es_seq_shrinker_info_release,
1178};
1179
1180int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1181{
1182 int err;
1183
Zheng Liu74cd15c2013-02-18 00:32:55 -05001184 INIT_LIST_HEAD(&sbi->s_es_lru);
1185 spin_lock_init(&sbi->s_es_lru_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001186 sbi->s_es_stats.es_stats_last_sorted = 0;
1187 sbi->s_es_stats.es_stats_shrunk = 0;
1188 sbi->s_es_stats.es_stats_cache_hits = 0;
1189 sbi->s_es_stats.es_stats_cache_misses = 0;
1190 sbi->s_es_stats.es_stats_scan_time = 0;
1191 sbi->s_es_stats.es_stats_max_scan_time = 0;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001192 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001193 if (err)
1194 return err;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001195 err = percpu_counter_init(&sbi->s_es_stats.es_stats_lru_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001196 if (err)
1197 goto err1;
1198
Dave Chinner1ab6c492013-08-28 10:18:09 +10001199 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1200 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001201 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001202 err = register_shrinker(&sbi->s_es_shrinker);
1203 if (err)
1204 goto err2;
1205
1206 if (sbi->s_proc)
1207 proc_create_data("es_shrinker_info", S_IRUGO, sbi->s_proc,
1208 &ext4_es_seq_shrinker_info_fops, sbi);
1209
1210 return 0;
1211
1212err2:
1213 percpu_counter_destroy(&sbi->s_es_stats.es_stats_lru_cnt);
1214err1:
1215 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1216 return err;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001217}
1218
Zheng Liud3922a72013-07-01 08:12:37 -04001219void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001220{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001221 if (sbi->s_proc)
1222 remove_proc_entry("es_shrinker_info", sbi->s_proc);
1223 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1224 percpu_counter_destroy(&sbi->s_es_stats.es_stats_lru_cnt);
Zheng Liud3922a72013-07-01 08:12:37 -04001225 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001226}
1227
1228void ext4_es_lru_add(struct inode *inode)
1229{
1230 struct ext4_inode_info *ei = EXT4_I(inode);
1231 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1232
Zheng Liud3922a72013-07-01 08:12:37 -04001233 ei->i_touch_when = jiffies;
1234
1235 if (!list_empty(&ei->i_es_lru))
1236 return;
1237
Zheng Liu74cd15c2013-02-18 00:32:55 -05001238 spin_lock(&sbi->s_es_lru_lock);
1239 if (list_empty(&ei->i_es_lru))
1240 list_add_tail(&ei->i_es_lru, &sbi->s_es_lru);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001241 spin_unlock(&sbi->s_es_lru_lock);
1242}
1243
1244void ext4_es_lru_del(struct inode *inode)
1245{
1246 struct ext4_inode_info *ei = EXT4_I(inode);
1247 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1248
1249 spin_lock(&sbi->s_es_lru_lock);
1250 if (!list_empty(&ei->i_es_lru))
1251 list_del_init(&ei->i_es_lru);
1252 spin_unlock(&sbi->s_es_lru_lock);
1253}
1254
Zheng Liu74cd15c2013-02-18 00:32:55 -05001255static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
1256 int nr_to_scan)
1257{
1258 struct inode *inode = &ei->vfs_inode;
1259 struct ext4_es_tree *tree = &ei->i_es_tree;
1260 struct rb_node *node;
1261 struct extent_status *es;
Dave Chinner1ab6c492013-08-28 10:18:09 +10001262 unsigned long nr_shrunk = 0;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001263 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1264 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001265
1266 if (ei->i_es_lru_nr == 0)
1267 return 0;
1268
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001269 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1270 __ratelimit(&_rs))
1271 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1272
Zheng Liu74cd15c2013-02-18 00:32:55 -05001273 node = rb_first(&tree->root);
1274 while (node != NULL) {
1275 es = rb_entry(node, struct extent_status, rb_node);
1276 node = rb_next(&es->rb_node);
1277 /*
1278 * We can't reclaim delayed extent from status tree because
1279 * fiemap, bigallic, and seek_data/hole need to use it.
1280 */
1281 if (!ext4_es_is_delayed(es)) {
1282 rb_erase(&es->rb_node, &tree->root);
1283 ext4_es_free_extent(inode, es);
1284 nr_shrunk++;
1285 if (--nr_to_scan == 0)
1286 break;
1287 }
1288 }
1289 tree->cache_es = NULL;
1290 return nr_shrunk;
1291}