blob: 763ef185dd17f3ca1c2b97167ae620029de40186 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Zheng Liu654598b2012-11-08 21:57:20 -05002/*
3 * fs/ext4/extents_status.c
4 *
5 * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
6 * Modified by
7 * Allison Henderson <achender@linux.vnet.ibm.com>
8 * Hugh Dickins <hughd@google.com>
9 * Zheng Liu <wenqing.lz@taobao.com>
10 *
11 * Ext4 extents status tree core functions.
12 */
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"
Zheng Liu654598b2012-11-08 21:57:20 -050017
Zheng Liu992e9fd2012-11-08 21:57:33 -050018#include <trace/events/ext4.h>
19
Zheng Liu654598b2012-11-08 21:57:20 -050020/*
21 * According to previous discussion in Ext4 Developer Workshop, we
22 * will introduce a new structure called io tree to track all extent
23 * status in order to solve some problems that we have met
24 * (e.g. Reservation space warning), and provide extent-level locking.
25 * Delay extent tree is the first step to achieve this goal. It is
26 * original built by Yongqiang Yang. At that time it is called delay
Zheng Liu06b0c882013-02-18 00:26:51 -050027 * extent tree, whose goal is only track delayed extents in memory to
Zheng Liu654598b2012-11-08 21:57:20 -050028 * simplify the implementation of fiemap and bigalloc, and introduce
29 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
Zheng Liu06b0c882013-02-18 00:26:51 -050030 * delay extent tree at the first commit. But for better understand
31 * what it does, it has been rename to extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050032 *
Zheng Liu06b0c882013-02-18 00:26:51 -050033 * Step1:
34 * Currently the first step has been done. All delayed extents are
35 * tracked in the tree. It maintains the delayed extent when a delayed
36 * allocation is issued, and the delayed extent is written out or
Zheng Liu654598b2012-11-08 21:57:20 -050037 * invalidated. Therefore the implementation of fiemap and bigalloc
38 * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
39 *
40 * The following comment describes the implemenmtation of extent
41 * status tree and future works.
Zheng Liu06b0c882013-02-18 00:26:51 -050042 *
43 * Step2:
44 * In this step all extent status are tracked by extent status tree.
45 * Thus, we can first try to lookup a block mapping in this tree before
46 * finding it in extent tree. Hence, single extent cache can be removed
47 * because extent status tree can do a better job. Extents in status
48 * tree are loaded on-demand. Therefore, the extent status tree may not
49 * contain all of the extents in a file. Meanwhile we define a shrinker
50 * to reclaim memory from extent status tree because fragmented extent
51 * tree will make status tree cost too much memory. written/unwritten/-
52 * hole extents in the tree will be reclaimed by this shrinker when we
53 * are under high memory pressure. Delayed extents will not be
54 * reclimed because fiemap, bigalloc, and seek_data/hole need it.
Zheng Liu654598b2012-11-08 21:57:20 -050055 */
56
57/*
Zheng Liu06b0c882013-02-18 00:26:51 -050058 * Extent status tree implementation for ext4.
Zheng Liu654598b2012-11-08 21:57:20 -050059 *
60 *
61 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050062 * Extent status tree tracks all extent status.
Zheng Liu654598b2012-11-08 21:57:20 -050063 *
Zheng Liu06b0c882013-02-18 00:26:51 -050064 * 1. Why we need to implement extent status tree?
Zheng Liu654598b2012-11-08 21:57:20 -050065 *
Zheng Liu06b0c882013-02-18 00:26:51 -050066 * Without extent status tree, ext4 identifies a delayed extent by looking
Zheng Liu654598b2012-11-08 21:57:20 -050067 * up page cache, this has several deficiencies - complicated, buggy,
68 * and inefficient code.
69 *
Zheng Liu06b0c882013-02-18 00:26:51 -050070 * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
71 * block or a range of blocks are belonged to a delayed extent.
Zheng Liu654598b2012-11-08 21:57:20 -050072 *
Zheng Liu06b0c882013-02-18 00:26:51 -050073 * Let us have a look at how they do without extent status tree.
Zheng Liu654598b2012-11-08 21:57:20 -050074 * -- FIEMAP
75 * FIEMAP looks up page cache to identify delayed allocations from holes.
76 *
77 * -- SEEK_HOLE/DATA
78 * SEEK_HOLE/DATA has the same problem as FIEMAP.
79 *
80 * -- bigalloc
81 * bigalloc looks up page cache to figure out if a block is
82 * already under delayed allocation or not to determine whether
83 * quota reserving is needed for the cluster.
84 *
Zheng Liu654598b2012-11-08 21:57:20 -050085 * -- writeout
86 * Writeout looks up whole page cache to see if a buffer is
87 * mapped, If there are not very many delayed buffers, then it is
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080088 * time consuming.
Zheng Liu654598b2012-11-08 21:57:20 -050089 *
Zheng Liu06b0c882013-02-18 00:26:51 -050090 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
Zheng Liu654598b2012-11-08 21:57:20 -050091 * bigalloc and writeout can figure out if a block or a range of
92 * blocks is under delayed allocation(belonged to a delayed extent) or
Zheng Liu06b0c882013-02-18 00:26:51 -050093 * not by searching the extent tree.
Zheng Liu654598b2012-11-08 21:57:20 -050094 *
95 *
96 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -050097 * 2. Ext4 extent status tree impelmentation
Zheng Liu654598b2012-11-08 21:57:20 -050098 *
Zheng Liu06b0c882013-02-18 00:26:51 -050099 * -- extent
100 * A extent is a range of blocks which are contiguous logically and
101 * physically. Unlike extent in extent tree, this extent in ext4 is
102 * a in-memory struct, there is no corresponding on-disk data. There
103 * is no limit on length of extent, so an extent can contain as many
104 * blocks as they are contiguous logically and physically.
Zheng Liu654598b2012-11-08 21:57:20 -0500105 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500106 * -- extent status tree
107 * Every inode has an extent status tree and all allocation blocks
108 * are added to the tree with different status. The extent in the
109 * tree are ordered by logical block no.
Zheng Liu654598b2012-11-08 21:57:20 -0500110 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500111 * -- operations on a extent status tree
112 * There are three important operations on a delayed extent tree: find
113 * next extent, adding a extent(a range of blocks) and removing a extent.
Zheng Liu654598b2012-11-08 21:57:20 -0500114 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500115 * -- race on a extent status tree
116 * Extent status tree is protected by inode->i_es_lock.
117 *
118 * -- memory consumption
119 * Fragmented extent tree will make extent status tree cost too much
120 * memory. Hence, we will reclaim written/unwritten/hole extents from
121 * the tree under a heavy memory pressure.
Zheng Liu654598b2012-11-08 21:57:20 -0500122 *
123 *
124 * ==========================================================================
Zheng Liu06b0c882013-02-18 00:26:51 -0500125 * 3. Performance analysis
126 *
Zheng Liu654598b2012-11-08 21:57:20 -0500127 * -- overhead
128 * 1. There is a cache extent for write access, so if writes are
129 * not very random, adding space operaions are in O(1) time.
130 *
131 * -- gain
132 * 2. Code is much simpler, more readable, more maintainable and
133 * more efficient.
134 *
135 *
136 * ==========================================================================
137 * 4. TODO list
Zheng Liu654598b2012-11-08 21:57:20 -0500138 *
Zheng Liu06b0c882013-02-18 00:26:51 -0500139 * -- Refactor delayed space reservation
Zheng Liu654598b2012-11-08 21:57:20 -0500140 *
141 * -- Extent-level locking
142 */
143
144static struct kmem_cache *ext4_es_cachep;
145
Zheng Liubdedbb72013-02-18 00:32:02 -0500146static int __es_insert_extent(struct inode *inode, struct extent_status *newes);
147static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liu06b0c882013-02-18 00:26:51 -0500148 ext4_lblk_t end);
Jan Karadd475922014-11-25 11:51:23 -0500149static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500150static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
151 struct ext4_inode_info *locked_ei);
Zheng Liu06b0c882013-02-18 00:26:51 -0500152
Zheng Liu654598b2012-11-08 21:57:20 -0500153int __init ext4_init_es(void)
154{
Theodore Ts'o24630772013-02-28 23:58:56 -0500155 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
156 sizeof(struct extent_status),
157 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500158 if (ext4_es_cachep == NULL)
159 return -ENOMEM;
160 return 0;
161}
162
163void ext4_exit_es(void)
164{
165 if (ext4_es_cachep)
166 kmem_cache_destroy(ext4_es_cachep);
167}
168
169void ext4_es_init_tree(struct ext4_es_tree *tree)
170{
171 tree->root = RB_ROOT;
172 tree->cache_es = NULL;
173}
174
175#ifdef ES_DEBUG__
176static void ext4_es_print_tree(struct inode *inode)
177{
178 struct ext4_es_tree *tree;
179 struct rb_node *node;
180
181 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
182 tree = &EXT4_I(inode)->i_es_tree;
183 node = rb_first(&tree->root);
184 while (node) {
185 struct extent_status *es;
186 es = rb_entry(node, struct extent_status, rb_node);
Eric Whitneyce140cd2014-02-20 16:09:12 -0500187 printk(KERN_DEBUG " [%u/%u) %llu %x",
Zheng Liufdc02122013-02-18 00:26:51 -0500188 es->es_lblk, es->es_len,
189 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500190 node = rb_next(node);
191 }
192 printk(KERN_DEBUG "\n");
193}
194#else
195#define ext4_es_print_tree(inode)
196#endif
197
Zheng Liu06b0c882013-02-18 00:26:51 -0500198static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500199{
Zheng Liu06b0c882013-02-18 00:26:51 -0500200 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
201 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500202}
203
204/*
205 * search through the tree for an delayed extent with a given offset. If
206 * it can't be found, try to find next extent.
207 */
208static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500209 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500210{
211 struct rb_node *node = root->rb_node;
212 struct extent_status *es = NULL;
213
214 while (node) {
215 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500216 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500217 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500218 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500219 node = node->rb_right;
220 else
221 return es;
222 }
223
Zheng Liu06b0c882013-02-18 00:26:51 -0500224 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500225 return es;
226
Zheng Liu06b0c882013-02-18 00:26:51 -0500227 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500228 node = rb_next(&es->rb_node);
229 return node ? rb_entry(node, struct extent_status, rb_node) :
230 NULL;
231 }
232
233 return NULL;
234}
235
236/*
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400237 * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
238 * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
Zheng Liu654598b2012-11-08 21:57:20 -0500239 *
240 * @inode: the inode which owns delayed extents
Zheng Liube401362013-02-18 00:27:26 -0500241 * @lblk: the offset where we start to search
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400242 * @end: the offset where we stop to search
Zheng Liu654598b2012-11-08 21:57:20 -0500243 * @es: delayed extent that we found
Zheng Liu654598b2012-11-08 21:57:20 -0500244 */
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400245void ext4_es_find_delayed_extent_range(struct inode *inode,
246 ext4_lblk_t lblk, ext4_lblk_t end,
Zheng Liube401362013-02-18 00:27:26 -0500247 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500248{
249 struct ext4_es_tree *tree = NULL;
250 struct extent_status *es1 = NULL;
251 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500252
Zheng Liube401362013-02-18 00:27:26 -0500253 BUG_ON(es == NULL);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400254 BUG_ON(end < lblk);
255 trace_ext4_es_find_delayed_extent_range_enter(inode, lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500256
Zheng Liu654598b2012-11-08 21:57:20 -0500257 read_lock(&EXT4_I(inode)->i_es_lock);
258 tree = &EXT4_I(inode)->i_es_tree;
259
Zheng Liufdc02122013-02-18 00:26:51 -0500260 /* find extent in cache firstly */
Zheng Liube401362013-02-18 00:27:26 -0500261 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500262 if (tree->cache_es) {
263 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500264 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400265 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500266 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500267 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500268 goto out;
269 }
270 }
271
Zheng Liube401362013-02-18 00:27:26 -0500272 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500273
274out:
Zheng Liube401362013-02-18 00:27:26 -0500275 if (es1 && !ext4_es_is_delayed(es1)) {
276 while ((node = rb_next(&es1->rb_node)) != NULL) {
277 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400278 if (es1->es_lblk > end) {
279 es1 = NULL;
280 break;
281 }
Zheng Liube401362013-02-18 00:27:26 -0500282 if (ext4_es_is_delayed(es1))
283 break;
284 }
285 }
286
287 if (es1 && ext4_es_is_delayed(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500288 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500289 es->es_lblk = es1->es_lblk;
290 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500291 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500292 }
293
294 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500295
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400296 trace_ext4_es_find_delayed_extent_range_exit(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500297}
298
Jan Karab0dea4c2014-11-25 11:49:25 -0500299static void ext4_es_list_add(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500300{
301 struct ext4_inode_info *ei = EXT4_I(inode);
302 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
303
304 if (!list_empty(&ei->i_es_list))
305 return;
306
307 spin_lock(&sbi->s_es_lock);
308 if (list_empty(&ei->i_es_list)) {
309 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
310 sbi->s_es_nr_inode++;
311 }
312 spin_unlock(&sbi->s_es_lock);
313}
314
Jan Karab0dea4c2014-11-25 11:49:25 -0500315static void ext4_es_list_del(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500316{
317 struct ext4_inode_info *ei = EXT4_I(inode);
318 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
319
320 spin_lock(&sbi->s_es_lock);
321 if (!list_empty(&ei->i_es_list)) {
322 list_del_init(&ei->i_es_list);
323 sbi->s_es_nr_inode--;
324 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
325 }
326 spin_unlock(&sbi->s_es_lock);
327}
328
Zheng Liu654598b2012-11-08 21:57:20 -0500329static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500330ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
331 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500332{
333 struct extent_status *es;
334 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
335 if (es == NULL)
336 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500337 es->es_lblk = lblk;
338 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500339 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500340
341 /*
342 * We don't count delayed extent because we never try to reclaim them
343 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500344 if (!ext4_es_is_delayed(es)) {
Jan Karab0dea4c2014-11-25 11:49:25 -0500345 if (!EXT4_I(inode)->i_es_shk_nr++)
346 ext4_es_list_add(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400347 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500348 s_es_stats.es_stats_shk_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500349 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500350
Zheng Liueb68d0e2014-09-01 22:26:49 -0400351 EXT4_I(inode)->i_es_all_nr++;
352 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
353
Zheng Liu654598b2012-11-08 21:57:20 -0500354 return es;
355}
356
Zheng Liubdedbb72013-02-18 00:32:02 -0500357static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500358{
Zheng Liueb68d0e2014-09-01 22:26:49 -0400359 EXT4_I(inode)->i_es_all_nr--;
360 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
361
Zheng Liuedaa53c2014-11-25 11:45:37 -0500362 /* Decrease the shrink counter when this es is not delayed */
Zheng Liu74cd15c2013-02-18 00:32:55 -0500363 if (!ext4_es_is_delayed(es)) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500364 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
Jan Karab0dea4c2014-11-25 11:49:25 -0500365 if (!--EXT4_I(inode)->i_es_shk_nr)
366 ext4_es_list_del(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400367 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500368 s_es_stats.es_stats_shk_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500369 }
370
Zheng Liu654598b2012-11-08 21:57:20 -0500371 kmem_cache_free(ext4_es_cachep, es);
372}
373
Zheng Liu06b0c882013-02-18 00:26:51 -0500374/*
375 * Check whether or not two extents can be merged
376 * Condition:
377 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500378 * - physical block number is contiguous
379 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500380 */
381static int ext4_es_can_be_merged(struct extent_status *es1,
382 struct extent_status *es2)
383{
Jan Kara2be12de2014-11-25 11:55:24 -0500384 if (ext4_es_type(es1) != ext4_es_type(es2))
Zheng Liufdc02122013-02-18 00:26:51 -0500385 return 0;
386
Lukas Czerner0baaea62014-05-12 22:21:43 -0400387 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
388 pr_warn("ES assertion failed when merging extents. "
389 "The sum of lengths of es1 (%d) and es2 (%d) "
390 "is bigger than allowed file size (%d)\n",
391 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
392 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500393 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400394 }
Zheng Liufdc02122013-02-18 00:26:51 -0500395
Zheng Liubd384362013-03-10 20:48:59 -0400396 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
397 return 0;
398
399 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
400 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
401 return 1;
402
403 if (ext4_es_is_hole(es1))
404 return 1;
405
406 /* we need to check delayed extent is without unwritten status */
407 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
408 return 1;
409
410 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500411}
412
Zheng Liu654598b2012-11-08 21:57:20 -0500413static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500414ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500415{
Zheng Liubdedbb72013-02-18 00:32:02 -0500416 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500417 struct extent_status *es1;
418 struct rb_node *node;
419
420 node = rb_prev(&es->rb_node);
421 if (!node)
422 return es;
423
424 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500425 if (ext4_es_can_be_merged(es1, es)) {
426 es1->es_len += es->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500427 if (ext4_es_is_referenced(es))
428 ext4_es_set_referenced(es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500429 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500430 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500431 es = es1;
432 }
433
434 return es;
435}
436
437static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500438ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500439{
Zheng Liubdedbb72013-02-18 00:32:02 -0500440 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500441 struct extent_status *es1;
442 struct rb_node *node;
443
444 node = rb_next(&es->rb_node);
445 if (!node)
446 return es;
447
448 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500449 if (ext4_es_can_be_merged(es, es1)) {
450 es->es_len += es1->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500451 if (ext4_es_is_referenced(es1))
452 ext4_es_set_referenced(es);
Zheng Liu654598b2012-11-08 21:57:20 -0500453 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500454 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500455 }
456
457 return es;
458}
459
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400460#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400461#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
462
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400463static void ext4_es_insert_extent_ext_check(struct inode *inode,
464 struct extent_status *es)
465{
466 struct ext4_ext_path *path = NULL;
467 struct ext4_extent *ex;
468 ext4_lblk_t ee_block;
469 ext4_fsblk_t ee_start;
470 unsigned short ee_len;
471 int depth, ee_status, es_status;
472
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400473 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400474 if (IS_ERR(path))
475 return;
476
477 depth = ext_depth(inode);
478 ex = path[depth].p_ext;
479
480 if (ex) {
481
482 ee_block = le32_to_cpu(ex->ee_block);
483 ee_start = ext4_ext_pblock(ex);
484 ee_len = ext4_ext_get_actual_len(ex);
485
Lukas Czerner556615d2014-04-20 23:45:47 -0400486 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400487 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
488
489 /*
490 * Make sure ex and es are not overlap when we try to insert
491 * a delayed/hole extent.
492 */
493 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
494 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400495 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400496 "inode: %lu we can find an extent "
497 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500498 "want to add a delayed/hole extent "
499 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400500 inode->i_ino, ee_block, ee_len,
501 ee_start, ee_status ? 'u' : 'w',
502 es->es_lblk, es->es_len,
503 ext4_es_pblock(es), ext4_es_status(es));
504 }
505 goto out;
506 }
507
508 /*
509 * We don't check ee_block == es->es_lblk, etc. because es
510 * might be a part of whole extent, vice versa.
511 */
512 if (es->es_lblk < ee_block ||
513 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400514 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400515 "ex_status [%d/%d/%llu/%c] != "
516 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
517 ee_block, ee_len, ee_start,
518 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
519 ext4_es_pblock(es), es_status ? 'u' : 'w');
520 goto out;
521 }
522
523 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400524 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400525 "ex_status [%d/%d/%llu/%c] != "
526 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
527 ee_block, ee_len, ee_start,
528 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
529 ext4_es_pblock(es), es_status ? 'u' : 'w');
530 }
531 } else {
532 /*
533 * We can't find an extent on disk. So we need to make sure
534 * that we don't want to add an written/unwritten extent.
535 */
536 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400537 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400538 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500539 "to add a written/unwritten extent "
540 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400541 es->es_lblk, es->es_lblk, es->es_len,
542 ext4_es_pblock(es), ext4_es_status(es));
543 }
544 }
545out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400546 ext4_ext_drop_refs(path);
547 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400548}
549
550static void ext4_es_insert_extent_ind_check(struct inode *inode,
551 struct extent_status *es)
552{
553 struct ext4_map_blocks map;
554 int retval;
555
556 /*
557 * Here we call ext4_ind_map_blocks to lookup a block mapping because
558 * 'Indirect' structure is defined in indirect.c. So we couldn't
559 * access direct/indirect tree from outside. It is too dirty to define
560 * this function in indirect.c file.
561 */
562
563 map.m_lblk = es->es_lblk;
564 map.m_len = es->es_len;
565
566 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
567 if (retval > 0) {
568 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
569 /*
570 * We want to add a delayed/hole extent but this
571 * block has been allocated.
572 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400573 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400574 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500575 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400576 inode->i_ino, es->es_lblk, es->es_len,
577 ext4_es_pblock(es), ext4_es_status(es));
578 return;
579 } else if (ext4_es_is_written(es)) {
580 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400581 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400582 "inode: %lu retval %d != es_len %d\n",
583 inode->i_ino, retval, es->es_len);
584 return;
585 }
586 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400587 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400588 "inode: %lu m_pblk %llu != "
589 "es_pblk %llu\n",
590 inode->i_ino, map.m_pblk,
591 ext4_es_pblock(es));
592 return;
593 }
594 } else {
595 /*
596 * We don't need to check unwritten extent because
597 * indirect-based file doesn't have it.
598 */
599 BUG_ON(1);
600 }
601 } else if (retval == 0) {
602 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400603 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400604 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500605 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400606 inode->i_ino, es->es_lblk, es->es_len,
607 ext4_es_pblock(es), ext4_es_status(es));
608 return;
609 }
610 }
611}
612
613static inline void ext4_es_insert_extent_check(struct inode *inode,
614 struct extent_status *es)
615{
616 /*
617 * We don't need to worry about the race condition because
618 * caller takes i_data_sem locking.
619 */
620 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
621 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
622 ext4_es_insert_extent_ext_check(inode, es);
623 else
624 ext4_es_insert_extent_ind_check(inode, es);
625}
626#else
627static inline void ext4_es_insert_extent_check(struct inode *inode,
628 struct extent_status *es)
629{
630}
631#endif
632
Zheng Liubdedbb72013-02-18 00:32:02 -0500633static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500634{
Zheng Liubdedbb72013-02-18 00:32:02 -0500635 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500636 struct rb_node **p = &tree->root.rb_node;
637 struct rb_node *parent = NULL;
638 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500639
640 while (*p) {
641 parent = *p;
642 es = rb_entry(parent, struct extent_status, rb_node);
643
Zheng Liu06b0c882013-02-18 00:26:51 -0500644 if (newes->es_lblk < es->es_lblk) {
645 if (ext4_es_can_be_merged(newes, es)) {
646 /*
647 * Here we can modify es_lblk directly
648 * because it isn't overlapped.
649 */
650 es->es_lblk = newes->es_lblk;
651 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500652 if (ext4_es_is_written(es) ||
653 ext4_es_is_unwritten(es))
654 ext4_es_store_pblock(es,
655 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500656 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500657 goto out;
658 }
659 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500660 } else if (newes->es_lblk > ext4_es_end(es)) {
661 if (ext4_es_can_be_merged(es, newes)) {
662 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500663 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500664 goto out;
665 }
666 p = &(*p)->rb_right;
667 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500668 BUG_ON(1);
669 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500670 }
671 }
672
Zheng Liubdedbb72013-02-18 00:32:02 -0500673 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500674 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500675 if (!es)
676 return -ENOMEM;
677 rb_link_node(&es->rb_node, parent, p);
678 rb_insert_color(&es->rb_node, &tree->root);
679
680out:
681 tree->cache_es = es;
682 return 0;
683}
684
685/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400686 * ext4_es_insert_extent() adds information to an inode's extent
687 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500688 *
689 * Return 0 on success, error code on failure.
690 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500691int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500692 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400693 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500694{
Zheng Liu06b0c882013-02-18 00:26:51 -0500695 struct extent_status newes;
696 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500697 int err = 0;
698
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400699 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500700 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500701
Eryu Guand4381472013-02-22 15:27:47 -0500702 if (!len)
703 return 0;
704
Zheng Liu06b0c882013-02-18 00:26:51 -0500705 BUG_ON(end < lblk);
706
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400707 if ((status & EXTENT_STATUS_DELAYED) &&
708 (status & EXTENT_STATUS_WRITTEN)) {
709 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
710 " delayed and written which can potentially "
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -0400711 " cause data loss.", lblk, len);
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400712 WARN_ON(1);
713 }
714
Zheng Liu06b0c882013-02-18 00:26:51 -0500715 newes.es_lblk = lblk;
716 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500717 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500718 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500719
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400720 ext4_es_insert_extent_check(inode, &newes);
721
Zheng Liu654598b2012-11-08 21:57:20 -0500722 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500723 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500724 if (err != 0)
725 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400726retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500727 err = __es_insert_extent(inode, &newes);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500728 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500729 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400730 goto retry;
731 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
732 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500733
734error:
Zheng Liu654598b2012-11-08 21:57:20 -0500735 write_unlock(&EXT4_I(inode)->i_es_lock);
736
737 ext4_es_print_tree(inode);
738
739 return err;
740}
741
Zheng Liud100eef2013-02-18 00:29:59 -0500742/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400743 * ext4_es_cache_extent() inserts information into the extent status
744 * tree if and only if there isn't information about the range in
745 * question already.
746 */
747void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
748 ext4_lblk_t len, ext4_fsblk_t pblk,
749 unsigned int status)
750{
751 struct extent_status *es;
752 struct extent_status newes;
753 ext4_lblk_t end = lblk + len - 1;
754
755 newes.es_lblk = lblk;
756 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500757 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400758 trace_ext4_es_cache_extent(inode, &newes);
759
760 if (!len)
761 return;
762
763 BUG_ON(end < lblk);
764
765 write_lock(&EXT4_I(inode)->i_es_lock);
766
767 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400768 if (!es || es->es_lblk > end)
769 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400770 write_unlock(&EXT4_I(inode)->i_es_lock);
771}
772
773/*
Zheng Liud100eef2013-02-18 00:29:59 -0500774 * ext4_es_lookup_extent() looks up an extent in extent status tree.
775 *
776 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
777 *
778 * Return: 1 on found, 0 on not
779 */
780int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
781 struct extent_status *es)
782{
783 struct ext4_es_tree *tree;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400784 struct ext4_es_stats *stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500785 struct extent_status *es1 = NULL;
786 struct rb_node *node;
787 int found = 0;
788
789 trace_ext4_es_lookup_extent_enter(inode, lblk);
790 es_debug("lookup extent in block %u\n", lblk);
791
792 tree = &EXT4_I(inode)->i_es_tree;
793 read_lock(&EXT4_I(inode)->i_es_lock);
794
795 /* find extent in cache firstly */
796 es->es_lblk = es->es_len = es->es_pblk = 0;
797 if (tree->cache_es) {
798 es1 = tree->cache_es;
799 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
800 es_debug("%u cached by [%u/%u)\n",
801 lblk, es1->es_lblk, es1->es_len);
802 found = 1;
803 goto out;
804 }
805 }
806
807 node = tree->root.rb_node;
808 while (node) {
809 es1 = rb_entry(node, struct extent_status, rb_node);
810 if (lblk < es1->es_lblk)
811 node = node->rb_left;
812 else if (lblk > ext4_es_end(es1))
813 node = node->rb_right;
814 else {
815 found = 1;
816 break;
817 }
818 }
819
820out:
Zheng Liueb68d0e2014-09-01 22:26:49 -0400821 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500822 if (found) {
823 BUG_ON(!es1);
824 es->es_lblk = es1->es_lblk;
825 es->es_len = es1->es_len;
826 es->es_pblk = es1->es_pblk;
Jan Kara87d8a742016-03-09 22:26:55 -0500827 if (!ext4_es_is_referenced(es1))
828 ext4_es_set_referenced(es1);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400829 stats->es_stats_cache_hits++;
830 } else {
831 stats->es_stats_cache_misses++;
Zheng Liud100eef2013-02-18 00:29:59 -0500832 }
833
834 read_unlock(&EXT4_I(inode)->i_es_lock);
835
836 trace_ext4_es_lookup_extent_exit(inode, es, found);
837 return found;
838}
839
Zheng Liubdedbb72013-02-18 00:32:02 -0500840static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
841 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500842{
Zheng Liubdedbb72013-02-18 00:32:02 -0500843 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500844 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500845 struct extent_status *es;
846 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500847 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500848 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400849 int err;
Zheng Liu654598b2012-11-08 21:57:20 -0500850
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400851retry:
852 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500853 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500854 if (!es)
855 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500856 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500857 goto out;
858
859 /* Simply invalidate cache_es. */
860 tree->cache_es = NULL;
861
Zheng Liu06b0c882013-02-18 00:26:51 -0500862 orig_es.es_lblk = es->es_lblk;
863 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500864 orig_es.es_pblk = es->es_pblk;
865
Zheng Liu06b0c882013-02-18 00:26:51 -0500866 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
867 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500868 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500869 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500870 if (len2 > 0) {
871 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500872 struct extent_status newes;
873
874 newes.es_lblk = end + 1;
875 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -0400876 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -0500877 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500878 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -0500879 block = ext4_es_pblock(&orig_es) +
880 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500881 ext4_es_store_pblock_status(&newes, block,
882 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500883 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500884 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500885 es->es_lblk = orig_es.es_lblk;
886 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400887 if ((err == -ENOMEM) &&
Zheng Liuedaa53c2014-11-25 11:45:37 -0500888 __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500889 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400890 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -0500891 goto out;
892 }
893 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500894 es->es_lblk = end + 1;
895 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500896 if (ext4_es_is_written(es) ||
897 ext4_es_is_unwritten(es)) {
898 block = orig_es.es_pblk + orig_es.es_len - len2;
899 ext4_es_store_pblock(es, block);
900 }
Zheng Liu654598b2012-11-08 21:57:20 -0500901 }
902 goto out;
903 }
904
905 if (len1 > 0) {
906 node = rb_next(&es->rb_node);
907 if (node)
908 es = rb_entry(node, struct extent_status, rb_node);
909 else
910 es = NULL;
911 }
912
Zheng Liu06b0c882013-02-18 00:26:51 -0500913 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -0500914 node = rb_next(&es->rb_node);
915 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500916 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500917 if (!node) {
918 es = NULL;
919 break;
920 }
921 es = rb_entry(node, struct extent_status, rb_node);
922 }
923
Zheng Liu06b0c882013-02-18 00:26:51 -0500924 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -0500925 ext4_lblk_t orig_len = es->es_len;
926
Zheng Liu06b0c882013-02-18 00:26:51 -0500927 len1 = ext4_es_end(es) - end;
928 es->es_lblk = end + 1;
929 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -0500930 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
931 block = es->es_pblk + orig_len - len1;
932 ext4_es_store_pblock(es, block);
933 }
Zheng Liu654598b2012-11-08 21:57:20 -0500934 }
935
936out:
Zheng Liu06b0c882013-02-18 00:26:51 -0500937 return err;
938}
939
940/*
941 * ext4_es_remove_extent() removes a space from a extent status tree.
942 *
943 * Return 0 on success, error code on failure.
944 */
945int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
946 ext4_lblk_t len)
947{
Zheng Liu06b0c882013-02-18 00:26:51 -0500948 ext4_lblk_t end;
949 int err = 0;
950
951 trace_ext4_es_remove_extent(inode, lblk, len);
952 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
953 lblk, len, inode->i_ino);
954
Eryu Guand4381472013-02-22 15:27:47 -0500955 if (!len)
956 return err;
957
Zheng Liu06b0c882013-02-18 00:26:51 -0500958 end = lblk + len - 1;
959 BUG_ON(end < lblk);
960
Zheng Liuedaa53c2014-11-25 11:45:37 -0500961 /*
962 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
963 * so that we are sure __es_shrink() is done with the inode before it
964 * is reclaimed.
965 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500966 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500967 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -0500968 write_unlock(&EXT4_I(inode)->i_es_lock);
969 ext4_es_print_tree(inode);
970 return err;
971}
Zheng Liu74cd15c2013-02-18 00:32:55 -0500972
Zheng Liuedaa53c2014-11-25 11:45:37 -0500973static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
974 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -0500975{
Zheng Liu74cd15c2013-02-18 00:32:55 -0500976 struct ext4_inode_info *ei;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400977 struct ext4_es_stats *es_stats;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400978 ktime_t start_time;
979 u64 scan_time;
Zheng Liuedaa53c2014-11-25 11:45:37 -0500980 int nr_to_walk;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000981 int nr_shrunk = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -0500982 int retried = 0, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500983
Zheng Liueb68d0e2014-09-01 22:26:49 -0400984 es_stats = &sbi->s_es_stats;
985 start_time = ktime_get();
Zheng Liud3922a72013-07-01 08:12:37 -0400986
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400987retry:
Zheng Liuedaa53c2014-11-25 11:45:37 -0500988 spin_lock(&sbi->s_es_lock);
989 nr_to_walk = sbi->s_es_nr_inode;
990 while (nr_to_walk-- > 0) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500991 if (list_empty(&sbi->s_es_list)) {
992 spin_unlock(&sbi->s_es_lock);
993 goto out;
994 }
995 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
996 i_es_list);
997 /* Move the inode to the tail */
Jan Karadd475922014-11-25 11:51:23 -0500998 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500999
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001000 /*
Zheng Liuedaa53c2014-11-25 11:45:37 -05001001 * Normally we try hard to avoid shrinking precached inodes,
1002 * but we will as a last resort.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001003 */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001004 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1005 EXT4_STATE_EXT_PRECACHED)) {
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001006 nr_skipped++;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001007 continue;
1008 }
Zheng Liud3922a72013-07-01 08:12:37 -04001009
Zheng Liuedaa53c2014-11-25 11:45:37 -05001010 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1011 nr_skipped++;
Zheng Liud3922a72013-07-01 08:12:37 -04001012 continue;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001013 }
1014 /*
1015 * Now we hold i_es_lock which protects us from inode reclaim
1016 * freeing inode under us
1017 */
1018 spin_unlock(&sbi->s_es_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001019
Jan Karadd475922014-11-25 11:51:23 -05001020 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001021 write_unlock(&ei->i_es_lock);
1022
Jan Karadd475922014-11-25 11:51:23 -05001023 if (nr_to_scan <= 0)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001024 goto out;
1025 spin_lock(&sbi->s_es_lock);
1026 }
1027 spin_unlock(&sbi->s_es_lock);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001028
1029 /*
1030 * If we skipped any inodes, and we weren't able to make any
Zheng Liuedaa53c2014-11-25 11:45:37 -05001031 * forward progress, try again to scan precached inodes.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001032 */
1033 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1034 retried++;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001035 goto retry;
1036 }
1037
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001038 if (locked_ei && nr_shrunk == 0)
Jan Karadd475922014-11-25 11:51:23 -05001039 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001040
Zheng Liuedaa53c2014-11-25 11:45:37 -05001041out:
Zheng Liueb68d0e2014-09-01 22:26:49 -04001042 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1043 if (likely(es_stats->es_stats_scan_time))
1044 es_stats->es_stats_scan_time = (scan_time +
1045 es_stats->es_stats_scan_time*3) / 4;
1046 else
1047 es_stats->es_stats_scan_time = scan_time;
1048 if (scan_time > es_stats->es_stats_max_scan_time)
1049 es_stats->es_stats_max_scan_time = scan_time;
1050 if (likely(es_stats->es_stats_shrunk))
1051 es_stats->es_stats_shrunk = (nr_shrunk +
1052 es_stats->es_stats_shrunk*3) / 4;
1053 else
1054 es_stats->es_stats_shrunk = nr_shrunk;
1055
Zheng Liuedaa53c2014-11-25 11:45:37 -05001056 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001057 nr_skipped, retried);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001058 return nr_shrunk;
1059}
1060
Dave Chinner1ab6c492013-08-28 10:18:09 +10001061static unsigned long ext4_es_count(struct shrinker *shrink,
1062 struct shrink_control *sc)
1063{
1064 unsigned long nr;
1065 struct ext4_sb_info *sbi;
1066
1067 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001068 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001069 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001070 return nr;
1071}
1072
1073static unsigned long ext4_es_scan(struct shrinker *shrink,
1074 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001075{
1076 struct ext4_sb_info *sbi = container_of(shrink,
1077 struct ext4_sb_info, s_es_shrinker);
1078 int nr_to_scan = sc->nr_to_scan;
1079 int ret, nr_shrunk;
1080
Zheng Liuedaa53c2014-11-25 11:45:37 -05001081 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001082 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001083
1084 if (!nr_to_scan)
1085 return ret;
1086
Zheng Liuedaa53c2014-11-25 11:45:37 -05001087 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001088
Zheng Liue963bb12014-09-01 22:22:13 -04001089 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001090 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001091}
1092
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001093int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001094{
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001095 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001096 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1097 struct ext4_inode_info *ei, *max = NULL;
1098 unsigned int inode_cnt = 0;
1099
1100 if (v != SEQ_START_TOKEN)
1101 return 0;
1102
1103 /* here we just find an inode that has the max nr. of objects */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001104 spin_lock(&sbi->s_es_lock);
1105 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
Zheng Liueb68d0e2014-09-01 22:26:49 -04001106 inode_cnt++;
1107 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1108 max = ei;
1109 else if (!max)
1110 max = ei;
1111 }
Zheng Liuedaa53c2014-11-25 11:45:37 -05001112 spin_unlock(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001113
1114 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1115 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
Zheng Liuedaa53c2014-11-25 11:45:37 -05001116 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
Zheng Liueb68d0e2014-09-01 22:26:49 -04001117 seq_printf(seq, " %lu/%lu cache hits/misses\n",
1118 es_stats->es_stats_cache_hits,
1119 es_stats->es_stats_cache_misses);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001120 if (inode_cnt)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001121 seq_printf(seq, " %d inodes on list\n", inode_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001122
1123 seq_printf(seq, "average:\n %llu us scan time\n",
1124 div_u64(es_stats->es_stats_scan_time, 1000));
1125 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1126 if (inode_cnt)
1127 seq_printf(seq,
1128 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1129 " %llu us max scan time\n",
Zheng Liuedaa53c2014-11-25 11:45:37 -05001130 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001131 div_u64(es_stats->es_stats_max_scan_time, 1000));
1132
1133 return 0;
1134}
1135
Zheng Liueb68d0e2014-09-01 22:26:49 -04001136int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1137{
1138 int err;
1139
Jan Kara624d0f12014-11-25 11:53:47 -05001140 /* Make sure we have enough bits for physical block number */
1141 BUILD_BUG_ON(ES_SHIFT < 48);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001142 INIT_LIST_HEAD(&sbi->s_es_list);
1143 sbi->s_es_nr_inode = 0;
1144 spin_lock_init(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001145 sbi->s_es_stats.es_stats_shrunk = 0;
1146 sbi->s_es_stats.es_stats_cache_hits = 0;
1147 sbi->s_es_stats.es_stats_cache_misses = 0;
1148 sbi->s_es_stats.es_stats_scan_time = 0;
1149 sbi->s_es_stats.es_stats_max_scan_time = 0;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001150 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001151 if (err)
1152 return err;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001153 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001154 if (err)
1155 goto err1;
1156
Dave Chinner1ab6c492013-08-28 10:18:09 +10001157 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1158 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001159 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001160 err = register_shrinker(&sbi->s_es_shrinker);
1161 if (err)
1162 goto err2;
1163
Zheng Liueb68d0e2014-09-01 22:26:49 -04001164 return 0;
1165
1166err2:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001167 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001168err1:
1169 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1170 return err;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001171}
1172
Zheng Liud3922a72013-07-01 08:12:37 -04001173void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001174{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001175 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001176 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liud3922a72013-07-01 08:12:37 -04001177 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001178}
1179
Jan Karadd475922014-11-25 11:51:23 -05001180/*
1181 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1182 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1183 *
1184 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1185 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1186 * ei->i_es_shrink_lblk to where we should continue scanning.
1187 */
1188static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1189 int *nr_to_scan, int *nr_shrunk)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001190{
1191 struct inode *inode = &ei->vfs_inode;
1192 struct ext4_es_tree *tree = &ei->i_es_tree;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001193 struct extent_status *es;
Jan Karadd475922014-11-25 11:51:23 -05001194 struct rb_node *node;
1195
1196 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1197 if (!es)
1198 goto out_wrap;
1199 node = &es->rb_node;
1200 while (*nr_to_scan > 0) {
1201 if (es->es_lblk > end) {
1202 ei->i_es_shrink_lblk = end + 1;
1203 return 0;
1204 }
1205
1206 (*nr_to_scan)--;
1207 node = rb_next(&es->rb_node);
1208 /*
1209 * We can't reclaim delayed extent from status tree because
1210 * fiemap, bigallic, and seek_data/hole need to use it.
1211 */
Jan Kara2be12de2014-11-25 11:55:24 -05001212 if (ext4_es_is_delayed(es))
1213 goto next;
1214 if (ext4_es_is_referenced(es)) {
1215 ext4_es_clear_referenced(es);
1216 goto next;
Jan Karadd475922014-11-25 11:51:23 -05001217 }
Jan Kara2be12de2014-11-25 11:55:24 -05001218
1219 rb_erase(&es->rb_node, &tree->root);
1220 ext4_es_free_extent(inode, es);
1221 (*nr_shrunk)++;
1222next:
Jan Karadd475922014-11-25 11:51:23 -05001223 if (!node)
1224 goto out_wrap;
1225 es = rb_entry(node, struct extent_status, rb_node);
1226 }
1227 ei->i_es_shrink_lblk = es->es_lblk;
1228 return 1;
1229out_wrap:
1230 ei->i_es_shrink_lblk = 0;
1231 return 0;
1232}
1233
1234static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1235{
1236 struct inode *inode = &ei->vfs_inode;
1237 int nr_shrunk = 0;
1238 ext4_lblk_t start = ei->i_es_shrink_lblk;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001239 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1240 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001241
Zheng Liuedaa53c2014-11-25 11:45:37 -05001242 if (ei->i_es_shk_nr == 0)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001243 return 0;
1244
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001245 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1246 __ratelimit(&_rs))
1247 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1248
Jan Karadd475922014-11-25 11:51:23 -05001249 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1250 start != 0)
1251 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1252
1253 ei->i_es_tree.cache_es = NULL;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001254 return nr_shrunk;
1255}