blob: 8530fbd3012d43cf04dd5427938f4c1a104ccab1 [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{
Sean Fu21c580d2018-05-20 22:44:13 -0400165 kmem_cache_destroy(ext4_es_cachep);
Zheng Liu654598b2012-11-08 21:57:20 -0500166}
167
168void ext4_es_init_tree(struct ext4_es_tree *tree)
169{
170 tree->root = RB_ROOT;
171 tree->cache_es = NULL;
172}
173
174#ifdef ES_DEBUG__
175static void ext4_es_print_tree(struct inode *inode)
176{
177 struct ext4_es_tree *tree;
178 struct rb_node *node;
179
180 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
181 tree = &EXT4_I(inode)->i_es_tree;
182 node = rb_first(&tree->root);
183 while (node) {
184 struct extent_status *es;
185 es = rb_entry(node, struct extent_status, rb_node);
Eric Whitneyce140cd2014-02-20 16:09:12 -0500186 printk(KERN_DEBUG " [%u/%u) %llu %x",
Zheng Liufdc02122013-02-18 00:26:51 -0500187 es->es_lblk, es->es_len,
188 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500189 node = rb_next(node);
190 }
191 printk(KERN_DEBUG "\n");
192}
193#else
194#define ext4_es_print_tree(inode)
195#endif
196
Zheng Liu06b0c882013-02-18 00:26:51 -0500197static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500198{
Zheng Liu06b0c882013-02-18 00:26:51 -0500199 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
200 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500201}
202
203/*
204 * search through the tree for an delayed extent with a given offset. If
205 * it can't be found, try to find next extent.
206 */
207static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500208 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500209{
210 struct rb_node *node = root->rb_node;
211 struct extent_status *es = NULL;
212
213 while (node) {
214 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500215 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500216 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500217 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500218 node = node->rb_right;
219 else
220 return es;
221 }
222
Zheng Liu06b0c882013-02-18 00:26:51 -0500223 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500224 return es;
225
Zheng Liu06b0c882013-02-18 00:26:51 -0500226 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500227 node = rb_next(&es->rb_node);
228 return node ? rb_entry(node, struct extent_status, rb_node) :
229 NULL;
230 }
231
232 return NULL;
233}
234
235/*
Eric Whitneyad431022018-10-01 14:10:39 -0400236 * ext4_es_find_extent_range - find extent with specified status within block
237 * range or next extent following block range in
238 * extents status tree
Zheng Liu654598b2012-11-08 21:57:20 -0500239 *
Eric Whitneyad431022018-10-01 14:10:39 -0400240 * @inode - file containing the range
241 * @matching_fn - pointer to function that matches extents with desired status
242 * @lblk - logical block defining start of range
243 * @end - logical block defining end of range
244 * @es - extent found, if any
245 *
246 * Find the first extent within the block range specified by @lblk and @end
247 * in the extents status tree that satisfies @matching_fn. If a match
248 * is found, it's returned in @es. If not, and a matching extent is found
249 * beyond the block range, it's returned in @es. If no match is found, an
250 * extent is returned in @es whose es_lblk, es_len, and es_pblk components
251 * are 0.
Zheng Liu654598b2012-11-08 21:57:20 -0500252 */
Eric Whitneyad431022018-10-01 14:10:39 -0400253static void __es_find_extent_range(struct inode *inode,
254 int (*matching_fn)(struct extent_status *es),
255 ext4_lblk_t lblk, ext4_lblk_t end,
256 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500257{
258 struct ext4_es_tree *tree = NULL;
259 struct extent_status *es1 = NULL;
260 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500261
Eric Whitneyad431022018-10-01 14:10:39 -0400262 WARN_ON(es == NULL);
263 WARN_ON(end < lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500264
Zheng Liu654598b2012-11-08 21:57:20 -0500265 tree = &EXT4_I(inode)->i_es_tree;
266
Eric Whitneyad431022018-10-01 14:10:39 -0400267 /* see if the extent has been cached */
Zheng Liube401362013-02-18 00:27:26 -0500268 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500269 if (tree->cache_es) {
270 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500271 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400272 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500273 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500274 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500275 goto out;
276 }
277 }
278
Zheng Liube401362013-02-18 00:27:26 -0500279 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500280
281out:
Eric Whitneyad431022018-10-01 14:10:39 -0400282 if (es1 && !matching_fn(es1)) {
Zheng Liube401362013-02-18 00:27:26 -0500283 while ((node = rb_next(&es1->rb_node)) != NULL) {
284 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400285 if (es1->es_lblk > end) {
286 es1 = NULL;
287 break;
288 }
Eric Whitneyad431022018-10-01 14:10:39 -0400289 if (matching_fn(es1))
Zheng Liube401362013-02-18 00:27:26 -0500290 break;
291 }
292 }
293
Eric Whitneyad431022018-10-01 14:10:39 -0400294 if (es1 && matching_fn(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500295 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500296 es->es_lblk = es1->es_lblk;
297 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500298 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500299 }
300
Eric Whitneyad431022018-10-01 14:10:39 -0400301}
302
303/*
304 * Locking for __es_find_extent_range() for external use
305 */
306void ext4_es_find_extent_range(struct inode *inode,
307 int (*matching_fn)(struct extent_status *es),
308 ext4_lblk_t lblk, ext4_lblk_t end,
309 struct extent_status *es)
310{
311 trace_ext4_es_find_extent_range_enter(inode, lblk);
312
313 read_lock(&EXT4_I(inode)->i_es_lock);
314 __es_find_extent_range(inode, matching_fn, lblk, end, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500315 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500316
Eric Whitneyad431022018-10-01 14:10:39 -0400317 trace_ext4_es_find_extent_range_exit(inode, es);
318}
319
320/*
321 * __es_scan_range - search block range for block with specified status
322 * in extents status tree
323 *
324 * @inode - file containing the range
325 * @matching_fn - pointer to function that matches extents with desired status
326 * @lblk - logical block defining start of range
327 * @end - logical block defining end of range
328 *
329 * Returns true if at least one block in the specified block range satisfies
330 * the criterion specified by @matching_fn, and false if not. If at least
331 * one extent has the specified status, then there is at least one block
332 * in the cluster with that status. Should only be called by code that has
333 * taken i_es_lock.
334 */
335static bool __es_scan_range(struct inode *inode,
336 int (*matching_fn)(struct extent_status *es),
337 ext4_lblk_t start, ext4_lblk_t end)
338{
339 struct extent_status es;
340
341 __es_find_extent_range(inode, matching_fn, start, end, &es);
342 if (es.es_len == 0)
343 return false; /* no matching extent in the tree */
344 else if (es.es_lblk <= start &&
345 start < es.es_lblk + es.es_len)
346 return true;
347 else if (start <= es.es_lblk && es.es_lblk <= end)
348 return true;
349 else
350 return false;
351}
352/*
353 * Locking for __es_scan_range() for external use
354 */
355bool ext4_es_scan_range(struct inode *inode,
356 int (*matching_fn)(struct extent_status *es),
357 ext4_lblk_t lblk, ext4_lblk_t end)
358{
359 bool ret;
360
361 read_lock(&EXT4_I(inode)->i_es_lock);
362 ret = __es_scan_range(inode, matching_fn, lblk, end);
363 read_unlock(&EXT4_I(inode)->i_es_lock);
364
365 return ret;
366}
367
368/*
369 * __es_scan_clu - search cluster for block with specified status in
370 * extents status tree
371 *
372 * @inode - file containing the cluster
373 * @matching_fn - pointer to function that matches extents with desired status
374 * @lblk - logical block in cluster to be searched
375 *
376 * Returns true if at least one extent in the cluster containing @lblk
377 * satisfies the criterion specified by @matching_fn, and false if not. If at
378 * least one extent has the specified status, then there is at least one block
379 * in the cluster with that status. Should only be called by code that has
380 * taken i_es_lock.
381 */
382static bool __es_scan_clu(struct inode *inode,
383 int (*matching_fn)(struct extent_status *es),
384 ext4_lblk_t lblk)
385{
386 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
387 ext4_lblk_t lblk_start, lblk_end;
388
389 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
390 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
391
392 return __es_scan_range(inode, matching_fn, lblk_start, lblk_end);
393}
394
395/*
396 * Locking for __es_scan_clu() for external use
397 */
398bool ext4_es_scan_clu(struct inode *inode,
399 int (*matching_fn)(struct extent_status *es),
400 ext4_lblk_t lblk)
401{
402 bool ret;
403
404 read_lock(&EXT4_I(inode)->i_es_lock);
405 ret = __es_scan_clu(inode, matching_fn, lblk);
406 read_unlock(&EXT4_I(inode)->i_es_lock);
407
408 return ret;
Zheng Liu654598b2012-11-08 21:57:20 -0500409}
410
Jan Karab0dea4c2014-11-25 11:49:25 -0500411static void ext4_es_list_add(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500412{
413 struct ext4_inode_info *ei = EXT4_I(inode);
414 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
415
416 if (!list_empty(&ei->i_es_list))
417 return;
418
419 spin_lock(&sbi->s_es_lock);
420 if (list_empty(&ei->i_es_list)) {
421 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
422 sbi->s_es_nr_inode++;
423 }
424 spin_unlock(&sbi->s_es_lock);
425}
426
Jan Karab0dea4c2014-11-25 11:49:25 -0500427static void ext4_es_list_del(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500428{
429 struct ext4_inode_info *ei = EXT4_I(inode);
430 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
431
432 spin_lock(&sbi->s_es_lock);
433 if (!list_empty(&ei->i_es_list)) {
434 list_del_init(&ei->i_es_list);
435 sbi->s_es_nr_inode--;
436 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
437 }
438 spin_unlock(&sbi->s_es_lock);
439}
440
Zheng Liu654598b2012-11-08 21:57:20 -0500441static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500442ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
443 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500444{
445 struct extent_status *es;
446 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
447 if (es == NULL)
448 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500449 es->es_lblk = lblk;
450 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500451 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500452
453 /*
454 * We don't count delayed extent because we never try to reclaim them
455 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500456 if (!ext4_es_is_delayed(es)) {
Jan Karab0dea4c2014-11-25 11:49:25 -0500457 if (!EXT4_I(inode)->i_es_shk_nr++)
458 ext4_es_list_add(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400459 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500460 s_es_stats.es_stats_shk_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500461 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500462
Zheng Liueb68d0e2014-09-01 22:26:49 -0400463 EXT4_I(inode)->i_es_all_nr++;
464 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
465
Zheng Liu654598b2012-11-08 21:57:20 -0500466 return es;
467}
468
Zheng Liubdedbb72013-02-18 00:32:02 -0500469static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500470{
Zheng Liueb68d0e2014-09-01 22:26:49 -0400471 EXT4_I(inode)->i_es_all_nr--;
472 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
473
Zheng Liuedaa53c2014-11-25 11:45:37 -0500474 /* Decrease the shrink counter when this es is not delayed */
Zheng Liu74cd15c2013-02-18 00:32:55 -0500475 if (!ext4_es_is_delayed(es)) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500476 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
Jan Karab0dea4c2014-11-25 11:49:25 -0500477 if (!--EXT4_I(inode)->i_es_shk_nr)
478 ext4_es_list_del(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400479 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500480 s_es_stats.es_stats_shk_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500481 }
482
Zheng Liu654598b2012-11-08 21:57:20 -0500483 kmem_cache_free(ext4_es_cachep, es);
484}
485
Zheng Liu06b0c882013-02-18 00:26:51 -0500486/*
487 * Check whether or not two extents can be merged
488 * Condition:
489 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500490 * - physical block number is contiguous
491 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500492 */
493static int ext4_es_can_be_merged(struct extent_status *es1,
494 struct extent_status *es2)
495{
Jan Kara2be12de2014-11-25 11:55:24 -0500496 if (ext4_es_type(es1) != ext4_es_type(es2))
Zheng Liufdc02122013-02-18 00:26:51 -0500497 return 0;
498
Lukas Czerner0baaea62014-05-12 22:21:43 -0400499 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
500 pr_warn("ES assertion failed when merging extents. "
501 "The sum of lengths of es1 (%d) and es2 (%d) "
502 "is bigger than allowed file size (%d)\n",
503 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
504 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500505 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400506 }
Zheng Liufdc02122013-02-18 00:26:51 -0500507
Zheng Liubd384362013-03-10 20:48:59 -0400508 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
509 return 0;
510
511 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
512 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
513 return 1;
514
515 if (ext4_es_is_hole(es1))
516 return 1;
517
518 /* we need to check delayed extent is without unwritten status */
519 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
520 return 1;
521
522 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500523}
524
Zheng Liu654598b2012-11-08 21:57:20 -0500525static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500526ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500527{
Zheng Liubdedbb72013-02-18 00:32:02 -0500528 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500529 struct extent_status *es1;
530 struct rb_node *node;
531
532 node = rb_prev(&es->rb_node);
533 if (!node)
534 return es;
535
536 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500537 if (ext4_es_can_be_merged(es1, es)) {
538 es1->es_len += es->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500539 if (ext4_es_is_referenced(es))
540 ext4_es_set_referenced(es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500541 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500542 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500543 es = es1;
544 }
545
546 return es;
547}
548
549static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500550ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500551{
Zheng Liubdedbb72013-02-18 00:32:02 -0500552 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500553 struct extent_status *es1;
554 struct rb_node *node;
555
556 node = rb_next(&es->rb_node);
557 if (!node)
558 return es;
559
560 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500561 if (ext4_es_can_be_merged(es, es1)) {
562 es->es_len += es1->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500563 if (ext4_es_is_referenced(es1))
564 ext4_es_set_referenced(es);
Zheng Liu654598b2012-11-08 21:57:20 -0500565 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500566 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500567 }
568
569 return es;
570}
571
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400572#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400573#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
574
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400575static void ext4_es_insert_extent_ext_check(struct inode *inode,
576 struct extent_status *es)
577{
578 struct ext4_ext_path *path = NULL;
579 struct ext4_extent *ex;
580 ext4_lblk_t ee_block;
581 ext4_fsblk_t ee_start;
582 unsigned short ee_len;
583 int depth, ee_status, es_status;
584
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400585 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400586 if (IS_ERR(path))
587 return;
588
589 depth = ext_depth(inode);
590 ex = path[depth].p_ext;
591
592 if (ex) {
593
594 ee_block = le32_to_cpu(ex->ee_block);
595 ee_start = ext4_ext_pblock(ex);
596 ee_len = ext4_ext_get_actual_len(ex);
597
Lukas Czerner556615d2014-04-20 23:45:47 -0400598 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400599 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
600
601 /*
602 * Make sure ex and es are not overlap when we try to insert
603 * a delayed/hole extent.
604 */
605 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
606 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400607 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400608 "inode: %lu we can find an extent "
609 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500610 "want to add a delayed/hole extent "
611 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400612 inode->i_ino, ee_block, ee_len,
613 ee_start, ee_status ? 'u' : 'w',
614 es->es_lblk, es->es_len,
615 ext4_es_pblock(es), ext4_es_status(es));
616 }
617 goto out;
618 }
619
620 /*
621 * We don't check ee_block == es->es_lblk, etc. because es
622 * might be a part of whole extent, vice versa.
623 */
624 if (es->es_lblk < ee_block ||
625 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400626 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400627 "ex_status [%d/%d/%llu/%c] != "
628 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
629 ee_block, ee_len, ee_start,
630 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
631 ext4_es_pblock(es), es_status ? 'u' : 'w');
632 goto out;
633 }
634
635 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400636 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400637 "ex_status [%d/%d/%llu/%c] != "
638 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
639 ee_block, ee_len, ee_start,
640 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
641 ext4_es_pblock(es), es_status ? 'u' : 'w');
642 }
643 } else {
644 /*
645 * We can't find an extent on disk. So we need to make sure
646 * that we don't want to add an written/unwritten extent.
647 */
648 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400649 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400650 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500651 "to add a written/unwritten extent "
652 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400653 es->es_lblk, es->es_lblk, es->es_len,
654 ext4_es_pblock(es), ext4_es_status(es));
655 }
656 }
657out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400658 ext4_ext_drop_refs(path);
659 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400660}
661
662static void ext4_es_insert_extent_ind_check(struct inode *inode,
663 struct extent_status *es)
664{
665 struct ext4_map_blocks map;
666 int retval;
667
668 /*
669 * Here we call ext4_ind_map_blocks to lookup a block mapping because
670 * 'Indirect' structure is defined in indirect.c. So we couldn't
671 * access direct/indirect tree from outside. It is too dirty to define
672 * this function in indirect.c file.
673 */
674
675 map.m_lblk = es->es_lblk;
676 map.m_len = es->es_len;
677
678 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
679 if (retval > 0) {
680 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
681 /*
682 * We want to add a delayed/hole extent but this
683 * block has been allocated.
684 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400685 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400686 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500687 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400688 inode->i_ino, es->es_lblk, es->es_len,
689 ext4_es_pblock(es), ext4_es_status(es));
690 return;
691 } else if (ext4_es_is_written(es)) {
692 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400693 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400694 "inode: %lu retval %d != es_len %d\n",
695 inode->i_ino, retval, es->es_len);
696 return;
697 }
698 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400699 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400700 "inode: %lu m_pblk %llu != "
701 "es_pblk %llu\n",
702 inode->i_ino, map.m_pblk,
703 ext4_es_pblock(es));
704 return;
705 }
706 } else {
707 /*
708 * We don't need to check unwritten extent because
709 * indirect-based file doesn't have it.
710 */
711 BUG_ON(1);
712 }
713 } else if (retval == 0) {
714 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400715 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400716 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500717 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400718 inode->i_ino, es->es_lblk, es->es_len,
719 ext4_es_pblock(es), ext4_es_status(es));
720 return;
721 }
722 }
723}
724
725static inline void ext4_es_insert_extent_check(struct inode *inode,
726 struct extent_status *es)
727{
728 /*
729 * We don't need to worry about the race condition because
730 * caller takes i_data_sem locking.
731 */
732 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
733 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
734 ext4_es_insert_extent_ext_check(inode, es);
735 else
736 ext4_es_insert_extent_ind_check(inode, es);
737}
738#else
739static inline void ext4_es_insert_extent_check(struct inode *inode,
740 struct extent_status *es)
741{
742}
743#endif
744
Zheng Liubdedbb72013-02-18 00:32:02 -0500745static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500746{
Zheng Liubdedbb72013-02-18 00:32:02 -0500747 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500748 struct rb_node **p = &tree->root.rb_node;
749 struct rb_node *parent = NULL;
750 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500751
752 while (*p) {
753 parent = *p;
754 es = rb_entry(parent, struct extent_status, rb_node);
755
Zheng Liu06b0c882013-02-18 00:26:51 -0500756 if (newes->es_lblk < es->es_lblk) {
757 if (ext4_es_can_be_merged(newes, es)) {
758 /*
759 * Here we can modify es_lblk directly
760 * because it isn't overlapped.
761 */
762 es->es_lblk = newes->es_lblk;
763 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500764 if (ext4_es_is_written(es) ||
765 ext4_es_is_unwritten(es))
766 ext4_es_store_pblock(es,
767 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500768 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500769 goto out;
770 }
771 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500772 } else if (newes->es_lblk > ext4_es_end(es)) {
773 if (ext4_es_can_be_merged(es, newes)) {
774 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500775 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500776 goto out;
777 }
778 p = &(*p)->rb_right;
779 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500780 BUG_ON(1);
781 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500782 }
783 }
784
Zheng Liubdedbb72013-02-18 00:32:02 -0500785 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500786 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500787 if (!es)
788 return -ENOMEM;
789 rb_link_node(&es->rb_node, parent, p);
790 rb_insert_color(&es->rb_node, &tree->root);
791
792out:
793 tree->cache_es = es;
794 return 0;
795}
796
797/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400798 * ext4_es_insert_extent() adds information to an inode's extent
799 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500800 *
801 * Return 0 on success, error code on failure.
802 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500803int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500804 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400805 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500806{
Zheng Liu06b0c882013-02-18 00:26:51 -0500807 struct extent_status newes;
808 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500809 int err = 0;
810
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400811 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500812 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500813
Eryu Guand4381472013-02-22 15:27:47 -0500814 if (!len)
815 return 0;
816
Zheng Liu06b0c882013-02-18 00:26:51 -0500817 BUG_ON(end < lblk);
818
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400819 if ((status & EXTENT_STATUS_DELAYED) &&
820 (status & EXTENT_STATUS_WRITTEN)) {
821 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
822 " delayed and written which can potentially "
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -0400823 " cause data loss.", lblk, len);
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400824 WARN_ON(1);
825 }
826
Zheng Liu06b0c882013-02-18 00:26:51 -0500827 newes.es_lblk = lblk;
828 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500829 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500830 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500831
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400832 ext4_es_insert_extent_check(inode, &newes);
833
Zheng Liu654598b2012-11-08 21:57:20 -0500834 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500835 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500836 if (err != 0)
837 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400838retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500839 err = __es_insert_extent(inode, &newes);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500840 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500841 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400842 goto retry;
843 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
844 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500845
846error:
Zheng Liu654598b2012-11-08 21:57:20 -0500847 write_unlock(&EXT4_I(inode)->i_es_lock);
848
849 ext4_es_print_tree(inode);
850
851 return err;
852}
853
Zheng Liud100eef2013-02-18 00:29:59 -0500854/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400855 * ext4_es_cache_extent() inserts information into the extent status
856 * tree if and only if there isn't information about the range in
857 * question already.
858 */
859void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
860 ext4_lblk_t len, ext4_fsblk_t pblk,
861 unsigned int status)
862{
863 struct extent_status *es;
864 struct extent_status newes;
865 ext4_lblk_t end = lblk + len - 1;
866
867 newes.es_lblk = lblk;
868 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500869 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400870 trace_ext4_es_cache_extent(inode, &newes);
871
872 if (!len)
873 return;
874
875 BUG_ON(end < lblk);
876
877 write_lock(&EXT4_I(inode)->i_es_lock);
878
879 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400880 if (!es || es->es_lblk > end)
881 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400882 write_unlock(&EXT4_I(inode)->i_es_lock);
883}
884
885/*
Zheng Liud100eef2013-02-18 00:29:59 -0500886 * ext4_es_lookup_extent() looks up an extent in extent status tree.
887 *
888 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
889 *
890 * Return: 1 on found, 0 on not
891 */
892int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
893 struct extent_status *es)
894{
895 struct ext4_es_tree *tree;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400896 struct ext4_es_stats *stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500897 struct extent_status *es1 = NULL;
898 struct rb_node *node;
899 int found = 0;
900
901 trace_ext4_es_lookup_extent_enter(inode, lblk);
902 es_debug("lookup extent in block %u\n", lblk);
903
904 tree = &EXT4_I(inode)->i_es_tree;
905 read_lock(&EXT4_I(inode)->i_es_lock);
906
907 /* find extent in cache firstly */
908 es->es_lblk = es->es_len = es->es_pblk = 0;
909 if (tree->cache_es) {
910 es1 = tree->cache_es;
911 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
912 es_debug("%u cached by [%u/%u)\n",
913 lblk, es1->es_lblk, es1->es_len);
914 found = 1;
915 goto out;
916 }
917 }
918
919 node = tree->root.rb_node;
920 while (node) {
921 es1 = rb_entry(node, struct extent_status, rb_node);
922 if (lblk < es1->es_lblk)
923 node = node->rb_left;
924 else if (lblk > ext4_es_end(es1))
925 node = node->rb_right;
926 else {
927 found = 1;
928 break;
929 }
930 }
931
932out:
Zheng Liueb68d0e2014-09-01 22:26:49 -0400933 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500934 if (found) {
935 BUG_ON(!es1);
936 es->es_lblk = es1->es_lblk;
937 es->es_len = es1->es_len;
938 es->es_pblk = es1->es_pblk;
Jan Kara87d8a742016-03-09 22:26:55 -0500939 if (!ext4_es_is_referenced(es1))
940 ext4_es_set_referenced(es1);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400941 stats->es_stats_cache_hits++;
942 } else {
943 stats->es_stats_cache_misses++;
Zheng Liud100eef2013-02-18 00:29:59 -0500944 }
945
946 read_unlock(&EXT4_I(inode)->i_es_lock);
947
948 trace_ext4_es_lookup_extent_exit(inode, es, found);
949 return found;
950}
951
Zheng Liubdedbb72013-02-18 00:32:02 -0500952static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
953 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500954{
Zheng Liubdedbb72013-02-18 00:32:02 -0500955 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500956 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500957 struct extent_status *es;
958 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500959 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500960 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400961 int err;
Zheng Liu654598b2012-11-08 21:57:20 -0500962
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400963retry:
964 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500965 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500966 if (!es)
967 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500968 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500969 goto out;
970
971 /* Simply invalidate cache_es. */
972 tree->cache_es = NULL;
973
Zheng Liu06b0c882013-02-18 00:26:51 -0500974 orig_es.es_lblk = es->es_lblk;
975 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500976 orig_es.es_pblk = es->es_pblk;
977
Zheng Liu06b0c882013-02-18 00:26:51 -0500978 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
979 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500980 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500981 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500982 if (len2 > 0) {
983 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500984 struct extent_status newes;
985
986 newes.es_lblk = end + 1;
987 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -0400988 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -0500989 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500990 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -0500991 block = ext4_es_pblock(&orig_es) +
992 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500993 ext4_es_store_pblock_status(&newes, block,
994 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500995 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500996 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500997 es->es_lblk = orig_es.es_lblk;
998 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400999 if ((err == -ENOMEM) &&
Zheng Liuedaa53c2014-11-25 11:45:37 -05001000 __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -05001001 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001002 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -05001003 goto out;
1004 }
1005 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -05001006 es->es_lblk = end + 1;
1007 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -05001008 if (ext4_es_is_written(es) ||
1009 ext4_es_is_unwritten(es)) {
1010 block = orig_es.es_pblk + orig_es.es_len - len2;
1011 ext4_es_store_pblock(es, block);
1012 }
Zheng Liu654598b2012-11-08 21:57:20 -05001013 }
1014 goto out;
1015 }
1016
1017 if (len1 > 0) {
1018 node = rb_next(&es->rb_node);
1019 if (node)
1020 es = rb_entry(node, struct extent_status, rb_node);
1021 else
1022 es = NULL;
1023 }
1024
Zheng Liu06b0c882013-02-18 00:26:51 -05001025 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -05001026 node = rb_next(&es->rb_node);
1027 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -05001028 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -05001029 if (!node) {
1030 es = NULL;
1031 break;
1032 }
1033 es = rb_entry(node, struct extent_status, rb_node);
1034 }
1035
Zheng Liu06b0c882013-02-18 00:26:51 -05001036 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -05001037 ext4_lblk_t orig_len = es->es_len;
1038
Zheng Liu06b0c882013-02-18 00:26:51 -05001039 len1 = ext4_es_end(es) - end;
1040 es->es_lblk = end + 1;
1041 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -05001042 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
1043 block = es->es_pblk + orig_len - len1;
1044 ext4_es_store_pblock(es, block);
1045 }
Zheng Liu654598b2012-11-08 21:57:20 -05001046 }
1047
1048out:
Zheng Liu06b0c882013-02-18 00:26:51 -05001049 return err;
1050}
1051
1052/*
1053 * ext4_es_remove_extent() removes a space from a extent status tree.
1054 *
1055 * Return 0 on success, error code on failure.
1056 */
1057int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1058 ext4_lblk_t len)
1059{
Zheng Liu06b0c882013-02-18 00:26:51 -05001060 ext4_lblk_t end;
1061 int err = 0;
1062
1063 trace_ext4_es_remove_extent(inode, lblk, len);
1064 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
1065 lblk, len, inode->i_ino);
1066
Eryu Guand4381472013-02-22 15:27:47 -05001067 if (!len)
1068 return err;
1069
Zheng Liu06b0c882013-02-18 00:26:51 -05001070 end = lblk + len - 1;
1071 BUG_ON(end < lblk);
1072
Zheng Liuedaa53c2014-11-25 11:45:37 -05001073 /*
1074 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
1075 * so that we are sure __es_shrink() is done with the inode before it
1076 * is reclaimed.
1077 */
Zheng Liu06b0c882013-02-18 00:26:51 -05001078 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -05001079 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -05001080 write_unlock(&EXT4_I(inode)->i_es_lock);
1081 ext4_es_print_tree(inode);
1082 return err;
1083}
Zheng Liu74cd15c2013-02-18 00:32:55 -05001084
Zheng Liuedaa53c2014-11-25 11:45:37 -05001085static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
1086 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001087{
Zheng Liu74cd15c2013-02-18 00:32:55 -05001088 struct ext4_inode_info *ei;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001089 struct ext4_es_stats *es_stats;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001090 ktime_t start_time;
1091 u64 scan_time;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001092 int nr_to_walk;
Dave Chinner1ab6c492013-08-28 10:18:09 +10001093 int nr_shrunk = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001094 int retried = 0, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001095
Zheng Liueb68d0e2014-09-01 22:26:49 -04001096 es_stats = &sbi->s_es_stats;
1097 start_time = ktime_get();
Zheng Liud3922a72013-07-01 08:12:37 -04001098
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001099retry:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001100 spin_lock(&sbi->s_es_lock);
1101 nr_to_walk = sbi->s_es_nr_inode;
1102 while (nr_to_walk-- > 0) {
Zheng Liuedaa53c2014-11-25 11:45:37 -05001103 if (list_empty(&sbi->s_es_list)) {
1104 spin_unlock(&sbi->s_es_lock);
1105 goto out;
1106 }
1107 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
1108 i_es_list);
1109 /* Move the inode to the tail */
Jan Karadd475922014-11-25 11:51:23 -05001110 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001111
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001112 /*
Zheng Liuedaa53c2014-11-25 11:45:37 -05001113 * Normally we try hard to avoid shrinking precached inodes,
1114 * but we will as a last resort.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001115 */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001116 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1117 EXT4_STATE_EXT_PRECACHED)) {
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001118 nr_skipped++;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001119 continue;
1120 }
Zheng Liud3922a72013-07-01 08:12:37 -04001121
Zheng Liuedaa53c2014-11-25 11:45:37 -05001122 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1123 nr_skipped++;
Zheng Liud3922a72013-07-01 08:12:37 -04001124 continue;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001125 }
1126 /*
1127 * Now we hold i_es_lock which protects us from inode reclaim
1128 * freeing inode under us
1129 */
1130 spin_unlock(&sbi->s_es_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001131
Jan Karadd475922014-11-25 11:51:23 -05001132 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001133 write_unlock(&ei->i_es_lock);
1134
Jan Karadd475922014-11-25 11:51:23 -05001135 if (nr_to_scan <= 0)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001136 goto out;
1137 spin_lock(&sbi->s_es_lock);
1138 }
1139 spin_unlock(&sbi->s_es_lock);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001140
1141 /*
1142 * If we skipped any inodes, and we weren't able to make any
Zheng Liuedaa53c2014-11-25 11:45:37 -05001143 * forward progress, try again to scan precached inodes.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001144 */
1145 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1146 retried++;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001147 goto retry;
1148 }
1149
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001150 if (locked_ei && nr_shrunk == 0)
Jan Karadd475922014-11-25 11:51:23 -05001151 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001152
Zheng Liuedaa53c2014-11-25 11:45:37 -05001153out:
Zheng Liueb68d0e2014-09-01 22:26:49 -04001154 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1155 if (likely(es_stats->es_stats_scan_time))
1156 es_stats->es_stats_scan_time = (scan_time +
1157 es_stats->es_stats_scan_time*3) / 4;
1158 else
1159 es_stats->es_stats_scan_time = scan_time;
1160 if (scan_time > es_stats->es_stats_max_scan_time)
1161 es_stats->es_stats_max_scan_time = scan_time;
1162 if (likely(es_stats->es_stats_shrunk))
1163 es_stats->es_stats_shrunk = (nr_shrunk +
1164 es_stats->es_stats_shrunk*3) / 4;
1165 else
1166 es_stats->es_stats_shrunk = nr_shrunk;
1167
Zheng Liuedaa53c2014-11-25 11:45:37 -05001168 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001169 nr_skipped, retried);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001170 return nr_shrunk;
1171}
1172
Dave Chinner1ab6c492013-08-28 10:18:09 +10001173static unsigned long ext4_es_count(struct shrinker *shrink,
1174 struct shrink_control *sc)
1175{
1176 unsigned long nr;
1177 struct ext4_sb_info *sbi;
1178
1179 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001180 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001181 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001182 return nr;
1183}
1184
1185static unsigned long ext4_es_scan(struct shrinker *shrink,
1186 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001187{
1188 struct ext4_sb_info *sbi = container_of(shrink,
1189 struct ext4_sb_info, s_es_shrinker);
1190 int nr_to_scan = sc->nr_to_scan;
1191 int ret, nr_shrunk;
1192
Zheng Liuedaa53c2014-11-25 11:45:37 -05001193 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001194 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001195
1196 if (!nr_to_scan)
1197 return ret;
1198
Zheng Liuedaa53c2014-11-25 11:45:37 -05001199 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001200
Zheng Liue963bb12014-09-01 22:22:13 -04001201 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001202 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001203}
1204
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001205int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001206{
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001207 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001208 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1209 struct ext4_inode_info *ei, *max = NULL;
1210 unsigned int inode_cnt = 0;
1211
1212 if (v != SEQ_START_TOKEN)
1213 return 0;
1214
1215 /* here we just find an inode that has the max nr. of objects */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001216 spin_lock(&sbi->s_es_lock);
1217 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
Zheng Liueb68d0e2014-09-01 22:26:49 -04001218 inode_cnt++;
1219 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1220 max = ei;
1221 else if (!max)
1222 max = ei;
1223 }
Zheng Liuedaa53c2014-11-25 11:45:37 -05001224 spin_unlock(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001225
1226 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1227 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
Zheng Liuedaa53c2014-11-25 11:45:37 -05001228 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
Zheng Liueb68d0e2014-09-01 22:26:49 -04001229 seq_printf(seq, " %lu/%lu cache hits/misses\n",
1230 es_stats->es_stats_cache_hits,
1231 es_stats->es_stats_cache_misses);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001232 if (inode_cnt)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001233 seq_printf(seq, " %d inodes on list\n", inode_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001234
1235 seq_printf(seq, "average:\n %llu us scan time\n",
1236 div_u64(es_stats->es_stats_scan_time, 1000));
1237 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1238 if (inode_cnt)
1239 seq_printf(seq,
1240 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1241 " %llu us max scan time\n",
Zheng Liuedaa53c2014-11-25 11:45:37 -05001242 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001243 div_u64(es_stats->es_stats_max_scan_time, 1000));
1244
1245 return 0;
1246}
1247
Zheng Liueb68d0e2014-09-01 22:26:49 -04001248int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1249{
1250 int err;
1251
Jan Kara624d0f12014-11-25 11:53:47 -05001252 /* Make sure we have enough bits for physical block number */
1253 BUILD_BUG_ON(ES_SHIFT < 48);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001254 INIT_LIST_HEAD(&sbi->s_es_list);
1255 sbi->s_es_nr_inode = 0;
1256 spin_lock_init(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001257 sbi->s_es_stats.es_stats_shrunk = 0;
1258 sbi->s_es_stats.es_stats_cache_hits = 0;
1259 sbi->s_es_stats.es_stats_cache_misses = 0;
1260 sbi->s_es_stats.es_stats_scan_time = 0;
1261 sbi->s_es_stats.es_stats_max_scan_time = 0;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001262 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001263 if (err)
1264 return err;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001265 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001266 if (err)
1267 goto err1;
1268
Dave Chinner1ab6c492013-08-28 10:18:09 +10001269 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1270 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001271 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001272 err = register_shrinker(&sbi->s_es_shrinker);
1273 if (err)
1274 goto err2;
1275
Zheng Liueb68d0e2014-09-01 22:26:49 -04001276 return 0;
1277
1278err2:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001279 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001280err1:
1281 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1282 return err;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001283}
1284
Zheng Liud3922a72013-07-01 08:12:37 -04001285void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001286{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001287 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001288 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liud3922a72013-07-01 08:12:37 -04001289 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001290}
1291
Jan Karadd475922014-11-25 11:51:23 -05001292/*
1293 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1294 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1295 *
1296 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1297 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1298 * ei->i_es_shrink_lblk to where we should continue scanning.
1299 */
1300static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1301 int *nr_to_scan, int *nr_shrunk)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001302{
1303 struct inode *inode = &ei->vfs_inode;
1304 struct ext4_es_tree *tree = &ei->i_es_tree;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001305 struct extent_status *es;
Jan Karadd475922014-11-25 11:51:23 -05001306 struct rb_node *node;
1307
1308 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1309 if (!es)
1310 goto out_wrap;
1311 node = &es->rb_node;
1312 while (*nr_to_scan > 0) {
1313 if (es->es_lblk > end) {
1314 ei->i_es_shrink_lblk = end + 1;
1315 return 0;
1316 }
1317
1318 (*nr_to_scan)--;
1319 node = rb_next(&es->rb_node);
1320 /*
1321 * We can't reclaim delayed extent from status tree because
1322 * fiemap, bigallic, and seek_data/hole need to use it.
1323 */
Jan Kara2be12de2014-11-25 11:55:24 -05001324 if (ext4_es_is_delayed(es))
1325 goto next;
1326 if (ext4_es_is_referenced(es)) {
1327 ext4_es_clear_referenced(es);
1328 goto next;
Jan Karadd475922014-11-25 11:51:23 -05001329 }
Jan Kara2be12de2014-11-25 11:55:24 -05001330
1331 rb_erase(&es->rb_node, &tree->root);
1332 ext4_es_free_extent(inode, es);
1333 (*nr_shrunk)++;
1334next:
Jan Karadd475922014-11-25 11:51:23 -05001335 if (!node)
1336 goto out_wrap;
1337 es = rb_entry(node, struct extent_status, rb_node);
1338 }
1339 ei->i_es_shrink_lblk = es->es_lblk;
1340 return 1;
1341out_wrap:
1342 ei->i_es_shrink_lblk = 0;
1343 return 0;
1344}
1345
1346static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1347{
1348 struct inode *inode = &ei->vfs_inode;
1349 int nr_shrunk = 0;
1350 ext4_lblk_t start = ei->i_es_shrink_lblk;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001351 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1352 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001353
Zheng Liuedaa53c2014-11-25 11:45:37 -05001354 if (ei->i_es_shk_nr == 0)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001355 return 0;
1356
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001357 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1358 __ratelimit(&_rs))
1359 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1360
Jan Karadd475922014-11-25 11:51:23 -05001361 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1362 start != 0)
1363 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1364
1365 ei->i_es_tree.cache_es = NULL;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001366 return nr_shrunk;
1367}