blob: 194785ce890ae027b557c88889b284a7410eb85a [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;
Eric Whitney1dc0aa42018-10-01 14:17:41 -0400145static struct kmem_cache *ext4_pending_cachep;
Zheng Liu654598b2012-11-08 21:57:20 -0500146
Zheng Liubdedbb72013-02-18 00:32:02 -0500147static int __es_insert_extent(struct inode *inode, struct extent_status *newes);
148static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liu06b0c882013-02-18 00:26:51 -0500149 ext4_lblk_t end);
Jan Karadd475922014-11-25 11:51:23 -0500150static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500151static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
152 struct ext4_inode_info *locked_ei);
Zheng Liu06b0c882013-02-18 00:26:51 -0500153
Zheng Liu654598b2012-11-08 21:57:20 -0500154int __init ext4_init_es(void)
155{
Theodore Ts'o24630772013-02-28 23:58:56 -0500156 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
157 sizeof(struct extent_status),
158 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500159 if (ext4_es_cachep == NULL)
160 return -ENOMEM;
161 return 0;
162}
163
164void ext4_exit_es(void)
165{
Sean Fu21c580d2018-05-20 22:44:13 -0400166 kmem_cache_destroy(ext4_es_cachep);
Zheng Liu654598b2012-11-08 21:57:20 -0500167}
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/*
Eric Whitneyad431022018-10-01 14:10:39 -0400237 * ext4_es_find_extent_range - find extent with specified status within block
238 * range or next extent following block range in
239 * extents status tree
Zheng Liu654598b2012-11-08 21:57:20 -0500240 *
Eric Whitneyad431022018-10-01 14:10:39 -0400241 * @inode - file containing the range
242 * @matching_fn - pointer to function that matches extents with desired status
243 * @lblk - logical block defining start of range
244 * @end - logical block defining end of range
245 * @es - extent found, if any
246 *
247 * Find the first extent within the block range specified by @lblk and @end
248 * in the extents status tree that satisfies @matching_fn. If a match
249 * is found, it's returned in @es. If not, and a matching extent is found
250 * beyond the block range, it's returned in @es. If no match is found, an
251 * extent is returned in @es whose es_lblk, es_len, and es_pblk components
252 * are 0.
Zheng Liu654598b2012-11-08 21:57:20 -0500253 */
Eric Whitneyad431022018-10-01 14:10:39 -0400254static void __es_find_extent_range(struct inode *inode,
255 int (*matching_fn)(struct extent_status *es),
256 ext4_lblk_t lblk, ext4_lblk_t end,
257 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500258{
259 struct ext4_es_tree *tree = NULL;
260 struct extent_status *es1 = NULL;
261 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500262
Eric Whitneyad431022018-10-01 14:10:39 -0400263 WARN_ON(es == NULL);
264 WARN_ON(end < lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500265
Zheng Liu654598b2012-11-08 21:57:20 -0500266 tree = &EXT4_I(inode)->i_es_tree;
267
Eric Whitneyad431022018-10-01 14:10:39 -0400268 /* see if the extent has been cached */
Zheng Liube401362013-02-18 00:27:26 -0500269 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500270 if (tree->cache_es) {
271 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500272 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400273 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500274 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500275 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500276 goto out;
277 }
278 }
279
Zheng Liube401362013-02-18 00:27:26 -0500280 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500281
282out:
Eric Whitneyad431022018-10-01 14:10:39 -0400283 if (es1 && !matching_fn(es1)) {
Zheng Liube401362013-02-18 00:27:26 -0500284 while ((node = rb_next(&es1->rb_node)) != NULL) {
285 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400286 if (es1->es_lblk > end) {
287 es1 = NULL;
288 break;
289 }
Eric Whitneyad431022018-10-01 14:10:39 -0400290 if (matching_fn(es1))
Zheng Liube401362013-02-18 00:27:26 -0500291 break;
292 }
293 }
294
Eric Whitneyad431022018-10-01 14:10:39 -0400295 if (es1 && matching_fn(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500296 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500297 es->es_lblk = es1->es_lblk;
298 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500299 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500300 }
301
Eric Whitneyad431022018-10-01 14:10:39 -0400302}
303
304/*
305 * Locking for __es_find_extent_range() for external use
306 */
307void ext4_es_find_extent_range(struct inode *inode,
308 int (*matching_fn)(struct extent_status *es),
309 ext4_lblk_t lblk, ext4_lblk_t end,
310 struct extent_status *es)
311{
312 trace_ext4_es_find_extent_range_enter(inode, lblk);
313
314 read_lock(&EXT4_I(inode)->i_es_lock);
315 __es_find_extent_range(inode, matching_fn, lblk, end, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500316 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500317
Eric Whitneyad431022018-10-01 14:10:39 -0400318 trace_ext4_es_find_extent_range_exit(inode, es);
319}
320
321/*
322 * __es_scan_range - search block range for block with specified status
323 * in extents status tree
324 *
325 * @inode - file containing the range
326 * @matching_fn - pointer to function that matches extents with desired status
327 * @lblk - logical block defining start of range
328 * @end - logical block defining end of range
329 *
330 * Returns true if at least one block in the specified block range satisfies
331 * the criterion specified by @matching_fn, and false if not. If at least
332 * one extent has the specified status, then there is at least one block
333 * in the cluster with that status. Should only be called by code that has
334 * taken i_es_lock.
335 */
336static bool __es_scan_range(struct inode *inode,
337 int (*matching_fn)(struct extent_status *es),
338 ext4_lblk_t start, ext4_lblk_t end)
339{
340 struct extent_status es;
341
342 __es_find_extent_range(inode, matching_fn, start, end, &es);
343 if (es.es_len == 0)
344 return false; /* no matching extent in the tree */
345 else if (es.es_lblk <= start &&
346 start < es.es_lblk + es.es_len)
347 return true;
348 else if (start <= es.es_lblk && es.es_lblk <= end)
349 return true;
350 else
351 return false;
352}
353/*
354 * Locking for __es_scan_range() for external use
355 */
356bool ext4_es_scan_range(struct inode *inode,
357 int (*matching_fn)(struct extent_status *es),
358 ext4_lblk_t lblk, ext4_lblk_t end)
359{
360 bool ret;
361
362 read_lock(&EXT4_I(inode)->i_es_lock);
363 ret = __es_scan_range(inode, matching_fn, lblk, end);
364 read_unlock(&EXT4_I(inode)->i_es_lock);
365
366 return ret;
367}
368
369/*
370 * __es_scan_clu - search cluster for block with specified status in
371 * extents status tree
372 *
373 * @inode - file containing the cluster
374 * @matching_fn - pointer to function that matches extents with desired status
375 * @lblk - logical block in cluster to be searched
376 *
377 * Returns true if at least one extent in the cluster containing @lblk
378 * satisfies the criterion specified by @matching_fn, and false if not. If at
379 * least one extent has the specified status, then there is at least one block
380 * in the cluster with that status. Should only be called by code that has
381 * taken i_es_lock.
382 */
383static bool __es_scan_clu(struct inode *inode,
384 int (*matching_fn)(struct extent_status *es),
385 ext4_lblk_t lblk)
386{
387 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
388 ext4_lblk_t lblk_start, lblk_end;
389
390 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
391 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
392
393 return __es_scan_range(inode, matching_fn, lblk_start, lblk_end);
394}
395
396/*
397 * Locking for __es_scan_clu() for external use
398 */
399bool ext4_es_scan_clu(struct inode *inode,
400 int (*matching_fn)(struct extent_status *es),
401 ext4_lblk_t lblk)
402{
403 bool ret;
404
405 read_lock(&EXT4_I(inode)->i_es_lock);
406 ret = __es_scan_clu(inode, matching_fn, lblk);
407 read_unlock(&EXT4_I(inode)->i_es_lock);
408
409 return ret;
Zheng Liu654598b2012-11-08 21:57:20 -0500410}
411
Jan Karab0dea4c2014-11-25 11:49:25 -0500412static void ext4_es_list_add(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500413{
414 struct ext4_inode_info *ei = EXT4_I(inode);
415 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
416
417 if (!list_empty(&ei->i_es_list))
418 return;
419
420 spin_lock(&sbi->s_es_lock);
421 if (list_empty(&ei->i_es_list)) {
422 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
423 sbi->s_es_nr_inode++;
424 }
425 spin_unlock(&sbi->s_es_lock);
426}
427
Jan Karab0dea4c2014-11-25 11:49:25 -0500428static void ext4_es_list_del(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500429{
430 struct ext4_inode_info *ei = EXT4_I(inode);
431 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
432
433 spin_lock(&sbi->s_es_lock);
434 if (!list_empty(&ei->i_es_list)) {
435 list_del_init(&ei->i_es_list);
436 sbi->s_es_nr_inode--;
437 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
438 }
439 spin_unlock(&sbi->s_es_lock);
440}
441
Zheng Liu654598b2012-11-08 21:57:20 -0500442static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500443ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
444 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500445{
446 struct extent_status *es;
447 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
448 if (es == NULL)
449 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500450 es->es_lblk = lblk;
451 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500452 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500453
454 /*
455 * We don't count delayed extent because we never try to reclaim them
456 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500457 if (!ext4_es_is_delayed(es)) {
Jan Karab0dea4c2014-11-25 11:49:25 -0500458 if (!EXT4_I(inode)->i_es_shk_nr++)
459 ext4_es_list_add(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400460 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500461 s_es_stats.es_stats_shk_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500462 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500463
Zheng Liueb68d0e2014-09-01 22:26:49 -0400464 EXT4_I(inode)->i_es_all_nr++;
465 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
466
Zheng Liu654598b2012-11-08 21:57:20 -0500467 return es;
468}
469
Zheng Liubdedbb72013-02-18 00:32:02 -0500470static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500471{
Zheng Liueb68d0e2014-09-01 22:26:49 -0400472 EXT4_I(inode)->i_es_all_nr--;
473 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
474
Zheng Liuedaa53c2014-11-25 11:45:37 -0500475 /* Decrease the shrink counter when this es is not delayed */
Zheng Liu74cd15c2013-02-18 00:32:55 -0500476 if (!ext4_es_is_delayed(es)) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500477 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
Jan Karab0dea4c2014-11-25 11:49:25 -0500478 if (!--EXT4_I(inode)->i_es_shk_nr)
479 ext4_es_list_del(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400480 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500481 s_es_stats.es_stats_shk_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500482 }
483
Zheng Liu654598b2012-11-08 21:57:20 -0500484 kmem_cache_free(ext4_es_cachep, es);
485}
486
Zheng Liu06b0c882013-02-18 00:26:51 -0500487/*
488 * Check whether or not two extents can be merged
489 * Condition:
490 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500491 * - physical block number is contiguous
492 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500493 */
494static int ext4_es_can_be_merged(struct extent_status *es1,
495 struct extent_status *es2)
496{
Jan Kara2be12de2014-11-25 11:55:24 -0500497 if (ext4_es_type(es1) != ext4_es_type(es2))
Zheng Liufdc02122013-02-18 00:26:51 -0500498 return 0;
499
Lukas Czerner0baaea62014-05-12 22:21:43 -0400500 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
501 pr_warn("ES assertion failed when merging extents. "
502 "The sum of lengths of es1 (%d) and es2 (%d) "
503 "is bigger than allowed file size (%d)\n",
504 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
505 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500506 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400507 }
Zheng Liufdc02122013-02-18 00:26:51 -0500508
Zheng Liubd384362013-03-10 20:48:59 -0400509 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
510 return 0;
511
512 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
513 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
514 return 1;
515
516 if (ext4_es_is_hole(es1))
517 return 1;
518
519 /* we need to check delayed extent is without unwritten status */
520 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
521 return 1;
522
523 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500524}
525
Zheng Liu654598b2012-11-08 21:57:20 -0500526static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500527ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500528{
Zheng Liubdedbb72013-02-18 00:32:02 -0500529 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500530 struct extent_status *es1;
531 struct rb_node *node;
532
533 node = rb_prev(&es->rb_node);
534 if (!node)
535 return es;
536
537 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500538 if (ext4_es_can_be_merged(es1, es)) {
539 es1->es_len += es->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500540 if (ext4_es_is_referenced(es))
541 ext4_es_set_referenced(es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500542 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500543 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500544 es = es1;
545 }
546
547 return es;
548}
549
550static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500551ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500552{
Zheng Liubdedbb72013-02-18 00:32:02 -0500553 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500554 struct extent_status *es1;
555 struct rb_node *node;
556
557 node = rb_next(&es->rb_node);
558 if (!node)
559 return es;
560
561 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500562 if (ext4_es_can_be_merged(es, es1)) {
563 es->es_len += es1->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500564 if (ext4_es_is_referenced(es1))
565 ext4_es_set_referenced(es);
Zheng Liu654598b2012-11-08 21:57:20 -0500566 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500567 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500568 }
569
570 return es;
571}
572
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400573#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400574#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
575
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400576static void ext4_es_insert_extent_ext_check(struct inode *inode,
577 struct extent_status *es)
578{
579 struct ext4_ext_path *path = NULL;
580 struct ext4_extent *ex;
581 ext4_lblk_t ee_block;
582 ext4_fsblk_t ee_start;
583 unsigned short ee_len;
584 int depth, ee_status, es_status;
585
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400586 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400587 if (IS_ERR(path))
588 return;
589
590 depth = ext_depth(inode);
591 ex = path[depth].p_ext;
592
593 if (ex) {
594
595 ee_block = le32_to_cpu(ex->ee_block);
596 ee_start = ext4_ext_pblock(ex);
597 ee_len = ext4_ext_get_actual_len(ex);
598
Lukas Czerner556615d2014-04-20 23:45:47 -0400599 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400600 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
601
602 /*
603 * Make sure ex and es are not overlap when we try to insert
604 * a delayed/hole extent.
605 */
606 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
607 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400608 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400609 "inode: %lu we can find an extent "
610 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500611 "want to add a delayed/hole extent "
612 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400613 inode->i_ino, ee_block, ee_len,
614 ee_start, ee_status ? 'u' : 'w',
615 es->es_lblk, es->es_len,
616 ext4_es_pblock(es), ext4_es_status(es));
617 }
618 goto out;
619 }
620
621 /*
622 * We don't check ee_block == es->es_lblk, etc. because es
623 * might be a part of whole extent, vice versa.
624 */
625 if (es->es_lblk < ee_block ||
626 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400627 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400628 "ex_status [%d/%d/%llu/%c] != "
629 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
630 ee_block, ee_len, ee_start,
631 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
632 ext4_es_pblock(es), es_status ? 'u' : 'w');
633 goto out;
634 }
635
636 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400637 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400638 "ex_status [%d/%d/%llu/%c] != "
639 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
640 ee_block, ee_len, ee_start,
641 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
642 ext4_es_pblock(es), es_status ? 'u' : 'w');
643 }
644 } else {
645 /*
646 * We can't find an extent on disk. So we need to make sure
647 * that we don't want to add an written/unwritten extent.
648 */
649 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400650 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400651 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500652 "to add a written/unwritten extent "
653 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400654 es->es_lblk, es->es_lblk, es->es_len,
655 ext4_es_pblock(es), ext4_es_status(es));
656 }
657 }
658out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400659 ext4_ext_drop_refs(path);
660 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400661}
662
663static void ext4_es_insert_extent_ind_check(struct inode *inode,
664 struct extent_status *es)
665{
666 struct ext4_map_blocks map;
667 int retval;
668
669 /*
670 * Here we call ext4_ind_map_blocks to lookup a block mapping because
671 * 'Indirect' structure is defined in indirect.c. So we couldn't
672 * access direct/indirect tree from outside. It is too dirty to define
673 * this function in indirect.c file.
674 */
675
676 map.m_lblk = es->es_lblk;
677 map.m_len = es->es_len;
678
679 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
680 if (retval > 0) {
681 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
682 /*
683 * We want to add a delayed/hole extent but this
684 * block has been allocated.
685 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400686 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400687 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500688 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400689 inode->i_ino, es->es_lblk, es->es_len,
690 ext4_es_pblock(es), ext4_es_status(es));
691 return;
692 } else if (ext4_es_is_written(es)) {
693 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400694 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400695 "inode: %lu retval %d != es_len %d\n",
696 inode->i_ino, retval, es->es_len);
697 return;
698 }
699 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400700 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400701 "inode: %lu m_pblk %llu != "
702 "es_pblk %llu\n",
703 inode->i_ino, map.m_pblk,
704 ext4_es_pblock(es));
705 return;
706 }
707 } else {
708 /*
709 * We don't need to check unwritten extent because
710 * indirect-based file doesn't have it.
711 */
712 BUG_ON(1);
713 }
714 } else if (retval == 0) {
715 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400716 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400717 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500718 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400719 inode->i_ino, es->es_lblk, es->es_len,
720 ext4_es_pblock(es), ext4_es_status(es));
721 return;
722 }
723 }
724}
725
726static inline void ext4_es_insert_extent_check(struct inode *inode,
727 struct extent_status *es)
728{
729 /*
730 * We don't need to worry about the race condition because
731 * caller takes i_data_sem locking.
732 */
733 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
734 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
735 ext4_es_insert_extent_ext_check(inode, es);
736 else
737 ext4_es_insert_extent_ind_check(inode, es);
738}
739#else
740static inline void ext4_es_insert_extent_check(struct inode *inode,
741 struct extent_status *es)
742{
743}
744#endif
745
Zheng Liubdedbb72013-02-18 00:32:02 -0500746static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500747{
Zheng Liubdedbb72013-02-18 00:32:02 -0500748 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500749 struct rb_node **p = &tree->root.rb_node;
750 struct rb_node *parent = NULL;
751 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500752
753 while (*p) {
754 parent = *p;
755 es = rb_entry(parent, struct extent_status, rb_node);
756
Zheng Liu06b0c882013-02-18 00:26:51 -0500757 if (newes->es_lblk < es->es_lblk) {
758 if (ext4_es_can_be_merged(newes, es)) {
759 /*
760 * Here we can modify es_lblk directly
761 * because it isn't overlapped.
762 */
763 es->es_lblk = newes->es_lblk;
764 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500765 if (ext4_es_is_written(es) ||
766 ext4_es_is_unwritten(es))
767 ext4_es_store_pblock(es,
768 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500769 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500770 goto out;
771 }
772 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500773 } else if (newes->es_lblk > ext4_es_end(es)) {
774 if (ext4_es_can_be_merged(es, newes)) {
775 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500776 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500777 goto out;
778 }
779 p = &(*p)->rb_right;
780 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -0500781 BUG_ON(1);
782 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500783 }
784 }
785
Zheng Liubdedbb72013-02-18 00:32:02 -0500786 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500787 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500788 if (!es)
789 return -ENOMEM;
790 rb_link_node(&es->rb_node, parent, p);
791 rb_insert_color(&es->rb_node, &tree->root);
792
793out:
794 tree->cache_es = es;
795 return 0;
796}
797
798/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400799 * ext4_es_insert_extent() adds information to an inode's extent
800 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500801 *
802 * Return 0 on success, error code on failure.
803 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500804int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500805 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400806 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500807{
Zheng Liu06b0c882013-02-18 00:26:51 -0500808 struct extent_status newes;
809 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500810 int err = 0;
811
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400812 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500813 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500814
Eryu Guand4381472013-02-22 15:27:47 -0500815 if (!len)
816 return 0;
817
Zheng Liu06b0c882013-02-18 00:26:51 -0500818 BUG_ON(end < lblk);
819
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400820 if ((status & EXTENT_STATUS_DELAYED) &&
821 (status & EXTENT_STATUS_WRITTEN)) {
822 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
823 " delayed and written which can potentially "
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -0400824 " cause data loss.", lblk, len);
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400825 WARN_ON(1);
826 }
827
Zheng Liu06b0c882013-02-18 00:26:51 -0500828 newes.es_lblk = lblk;
829 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500830 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500831 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500832
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400833 ext4_es_insert_extent_check(inode, &newes);
834
Zheng Liu654598b2012-11-08 21:57:20 -0500835 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -0500836 err = __es_remove_extent(inode, lblk, end);
Zheng Liu06b0c882013-02-18 00:26:51 -0500837 if (err != 0)
838 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400839retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500840 err = __es_insert_extent(inode, &newes);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500841 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500842 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400843 goto retry;
844 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
845 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500846
847error:
Zheng Liu654598b2012-11-08 21:57:20 -0500848 write_unlock(&EXT4_I(inode)->i_es_lock);
849
850 ext4_es_print_tree(inode);
851
852 return err;
853}
854
Zheng Liud100eef2013-02-18 00:29:59 -0500855/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400856 * ext4_es_cache_extent() inserts information into the extent status
857 * tree if and only if there isn't information about the range in
858 * question already.
859 */
860void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
861 ext4_lblk_t len, ext4_fsblk_t pblk,
862 unsigned int status)
863{
864 struct extent_status *es;
865 struct extent_status newes;
866 ext4_lblk_t end = lblk + len - 1;
867
868 newes.es_lblk = lblk;
869 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500870 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400871 trace_ext4_es_cache_extent(inode, &newes);
872
873 if (!len)
874 return;
875
876 BUG_ON(end < lblk);
877
878 write_lock(&EXT4_I(inode)->i_es_lock);
879
880 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400881 if (!es || es->es_lblk > end)
882 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400883 write_unlock(&EXT4_I(inode)->i_es_lock);
884}
885
886/*
Zheng Liud100eef2013-02-18 00:29:59 -0500887 * ext4_es_lookup_extent() looks up an extent in extent status tree.
888 *
889 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
890 *
891 * Return: 1 on found, 0 on not
892 */
893int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
894 struct extent_status *es)
895{
896 struct ext4_es_tree *tree;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400897 struct ext4_es_stats *stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500898 struct extent_status *es1 = NULL;
899 struct rb_node *node;
900 int found = 0;
901
902 trace_ext4_es_lookup_extent_enter(inode, lblk);
903 es_debug("lookup extent in block %u\n", lblk);
904
905 tree = &EXT4_I(inode)->i_es_tree;
906 read_lock(&EXT4_I(inode)->i_es_lock);
907
908 /* find extent in cache firstly */
909 es->es_lblk = es->es_len = es->es_pblk = 0;
910 if (tree->cache_es) {
911 es1 = tree->cache_es;
912 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
913 es_debug("%u cached by [%u/%u)\n",
914 lblk, es1->es_lblk, es1->es_len);
915 found = 1;
916 goto out;
917 }
918 }
919
920 node = tree->root.rb_node;
921 while (node) {
922 es1 = rb_entry(node, struct extent_status, rb_node);
923 if (lblk < es1->es_lblk)
924 node = node->rb_left;
925 else if (lblk > ext4_es_end(es1))
926 node = node->rb_right;
927 else {
928 found = 1;
929 break;
930 }
931 }
932
933out:
Zheng Liueb68d0e2014-09-01 22:26:49 -0400934 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500935 if (found) {
936 BUG_ON(!es1);
937 es->es_lblk = es1->es_lblk;
938 es->es_len = es1->es_len;
939 es->es_pblk = es1->es_pblk;
Jan Kara87d8a742016-03-09 22:26:55 -0500940 if (!ext4_es_is_referenced(es1))
941 ext4_es_set_referenced(es1);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400942 stats->es_stats_cache_hits++;
943 } else {
944 stats->es_stats_cache_misses++;
Zheng Liud100eef2013-02-18 00:29:59 -0500945 }
946
947 read_unlock(&EXT4_I(inode)->i_es_lock);
948
949 trace_ext4_es_lookup_extent_exit(inode, es, found);
950 return found;
951}
952
Zheng Liubdedbb72013-02-18 00:32:02 -0500953static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
954 ext4_lblk_t end)
Zheng Liu654598b2012-11-08 21:57:20 -0500955{
Zheng Liubdedbb72013-02-18 00:32:02 -0500956 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500957 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500958 struct extent_status *es;
959 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -0500960 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -0500961 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400962 int err;
Zheng Liu654598b2012-11-08 21:57:20 -0500963
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400964retry:
965 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500966 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500967 if (!es)
968 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -0500969 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -0500970 goto out;
971
972 /* Simply invalidate cache_es. */
973 tree->cache_es = NULL;
974
Zheng Liu06b0c882013-02-18 00:26:51 -0500975 orig_es.es_lblk = es->es_lblk;
976 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500977 orig_es.es_pblk = es->es_pblk;
978
Zheng Liu06b0c882013-02-18 00:26:51 -0500979 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
980 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500981 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -0500982 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -0500983 if (len2 > 0) {
984 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500985 struct extent_status newes;
986
987 newes.es_lblk = end + 1;
988 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -0400989 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -0500990 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500991 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -0500992 block = ext4_es_pblock(&orig_es) +
993 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500994 ext4_es_store_pblock_status(&newes, block,
995 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -0500996 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500997 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -0500998 es->es_lblk = orig_es.es_lblk;
999 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001000 if ((err == -ENOMEM) &&
Zheng Liuedaa53c2014-11-25 11:45:37 -05001001 __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -05001002 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001003 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -05001004 goto out;
1005 }
1006 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -05001007 es->es_lblk = end + 1;
1008 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -05001009 if (ext4_es_is_written(es) ||
1010 ext4_es_is_unwritten(es)) {
1011 block = orig_es.es_pblk + orig_es.es_len - len2;
1012 ext4_es_store_pblock(es, block);
1013 }
Zheng Liu654598b2012-11-08 21:57:20 -05001014 }
1015 goto out;
1016 }
1017
1018 if (len1 > 0) {
1019 node = rb_next(&es->rb_node);
1020 if (node)
1021 es = rb_entry(node, struct extent_status, rb_node);
1022 else
1023 es = NULL;
1024 }
1025
Zheng Liu06b0c882013-02-18 00:26:51 -05001026 while (es && ext4_es_end(es) <= end) {
Zheng Liu654598b2012-11-08 21:57:20 -05001027 node = rb_next(&es->rb_node);
1028 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -05001029 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -05001030 if (!node) {
1031 es = NULL;
1032 break;
1033 }
1034 es = rb_entry(node, struct extent_status, rb_node);
1035 }
1036
Zheng Liu06b0c882013-02-18 00:26:51 -05001037 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -05001038 ext4_lblk_t orig_len = es->es_len;
1039
Zheng Liu06b0c882013-02-18 00:26:51 -05001040 len1 = ext4_es_end(es) - end;
1041 es->es_lblk = end + 1;
1042 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -05001043 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
1044 block = es->es_pblk + orig_len - len1;
1045 ext4_es_store_pblock(es, block);
1046 }
Zheng Liu654598b2012-11-08 21:57:20 -05001047 }
1048
1049out:
Zheng Liu06b0c882013-02-18 00:26:51 -05001050 return err;
1051}
1052
1053/*
1054 * ext4_es_remove_extent() removes a space from a extent status tree.
1055 *
1056 * Return 0 on success, error code on failure.
1057 */
1058int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1059 ext4_lblk_t len)
1060{
Zheng Liu06b0c882013-02-18 00:26:51 -05001061 ext4_lblk_t end;
1062 int err = 0;
1063
1064 trace_ext4_es_remove_extent(inode, lblk, len);
1065 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
1066 lblk, len, inode->i_ino);
1067
Eryu Guand4381472013-02-22 15:27:47 -05001068 if (!len)
1069 return err;
1070
Zheng Liu06b0c882013-02-18 00:26:51 -05001071 end = lblk + len - 1;
1072 BUG_ON(end < lblk);
1073
Zheng Liuedaa53c2014-11-25 11:45:37 -05001074 /*
1075 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
1076 * so that we are sure __es_shrink() is done with the inode before it
1077 * is reclaimed.
1078 */
Zheng Liu06b0c882013-02-18 00:26:51 -05001079 write_lock(&EXT4_I(inode)->i_es_lock);
Zheng Liubdedbb72013-02-18 00:32:02 -05001080 err = __es_remove_extent(inode, lblk, end);
Zheng Liu654598b2012-11-08 21:57:20 -05001081 write_unlock(&EXT4_I(inode)->i_es_lock);
1082 ext4_es_print_tree(inode);
1083 return err;
1084}
Zheng Liu74cd15c2013-02-18 00:32:55 -05001085
Zheng Liuedaa53c2014-11-25 11:45:37 -05001086static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
1087 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001088{
Zheng Liu74cd15c2013-02-18 00:32:55 -05001089 struct ext4_inode_info *ei;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001090 struct ext4_es_stats *es_stats;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001091 ktime_t start_time;
1092 u64 scan_time;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001093 int nr_to_walk;
Dave Chinner1ab6c492013-08-28 10:18:09 +10001094 int nr_shrunk = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001095 int retried = 0, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001096
Zheng Liueb68d0e2014-09-01 22:26:49 -04001097 es_stats = &sbi->s_es_stats;
1098 start_time = ktime_get();
Zheng Liud3922a72013-07-01 08:12:37 -04001099
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001100retry:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001101 spin_lock(&sbi->s_es_lock);
1102 nr_to_walk = sbi->s_es_nr_inode;
1103 while (nr_to_walk-- > 0) {
Zheng Liuedaa53c2014-11-25 11:45:37 -05001104 if (list_empty(&sbi->s_es_list)) {
1105 spin_unlock(&sbi->s_es_lock);
1106 goto out;
1107 }
1108 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
1109 i_es_list);
1110 /* Move the inode to the tail */
Jan Karadd475922014-11-25 11:51:23 -05001111 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001112
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001113 /*
Zheng Liuedaa53c2014-11-25 11:45:37 -05001114 * Normally we try hard to avoid shrinking precached inodes,
1115 * but we will as a last resort.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001116 */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001117 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1118 EXT4_STATE_EXT_PRECACHED)) {
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001119 nr_skipped++;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001120 continue;
1121 }
Zheng Liud3922a72013-07-01 08:12:37 -04001122
Zheng Liuedaa53c2014-11-25 11:45:37 -05001123 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1124 nr_skipped++;
Zheng Liud3922a72013-07-01 08:12:37 -04001125 continue;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001126 }
1127 /*
1128 * Now we hold i_es_lock which protects us from inode reclaim
1129 * freeing inode under us
1130 */
1131 spin_unlock(&sbi->s_es_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001132
Jan Karadd475922014-11-25 11:51:23 -05001133 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001134 write_unlock(&ei->i_es_lock);
1135
Jan Karadd475922014-11-25 11:51:23 -05001136 if (nr_to_scan <= 0)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001137 goto out;
1138 spin_lock(&sbi->s_es_lock);
1139 }
1140 spin_unlock(&sbi->s_es_lock);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001141
1142 /*
1143 * If we skipped any inodes, and we weren't able to make any
Zheng Liuedaa53c2014-11-25 11:45:37 -05001144 * forward progress, try again to scan precached inodes.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001145 */
1146 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1147 retried++;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001148 goto retry;
1149 }
1150
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001151 if (locked_ei && nr_shrunk == 0)
Jan Karadd475922014-11-25 11:51:23 -05001152 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001153
Zheng Liuedaa53c2014-11-25 11:45:37 -05001154out:
Zheng Liueb68d0e2014-09-01 22:26:49 -04001155 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1156 if (likely(es_stats->es_stats_scan_time))
1157 es_stats->es_stats_scan_time = (scan_time +
1158 es_stats->es_stats_scan_time*3) / 4;
1159 else
1160 es_stats->es_stats_scan_time = scan_time;
1161 if (scan_time > es_stats->es_stats_max_scan_time)
1162 es_stats->es_stats_max_scan_time = scan_time;
1163 if (likely(es_stats->es_stats_shrunk))
1164 es_stats->es_stats_shrunk = (nr_shrunk +
1165 es_stats->es_stats_shrunk*3) / 4;
1166 else
1167 es_stats->es_stats_shrunk = nr_shrunk;
1168
Zheng Liuedaa53c2014-11-25 11:45:37 -05001169 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001170 nr_skipped, retried);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001171 return nr_shrunk;
1172}
1173
Dave Chinner1ab6c492013-08-28 10:18:09 +10001174static unsigned long ext4_es_count(struct shrinker *shrink,
1175 struct shrink_control *sc)
1176{
1177 unsigned long nr;
1178 struct ext4_sb_info *sbi;
1179
1180 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001181 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001182 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001183 return nr;
1184}
1185
1186static unsigned long ext4_es_scan(struct shrinker *shrink,
1187 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001188{
1189 struct ext4_sb_info *sbi = container_of(shrink,
1190 struct ext4_sb_info, s_es_shrinker);
1191 int nr_to_scan = sc->nr_to_scan;
1192 int ret, nr_shrunk;
1193
Zheng Liuedaa53c2014-11-25 11:45:37 -05001194 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001195 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001196
1197 if (!nr_to_scan)
1198 return ret;
1199
Zheng Liuedaa53c2014-11-25 11:45:37 -05001200 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001201
Zheng Liue963bb12014-09-01 22:22:13 -04001202 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001203 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001204}
1205
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001206int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001207{
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001208 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001209 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1210 struct ext4_inode_info *ei, *max = NULL;
1211 unsigned int inode_cnt = 0;
1212
1213 if (v != SEQ_START_TOKEN)
1214 return 0;
1215
1216 /* here we just find an inode that has the max nr. of objects */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001217 spin_lock(&sbi->s_es_lock);
1218 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
Zheng Liueb68d0e2014-09-01 22:26:49 -04001219 inode_cnt++;
1220 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1221 max = ei;
1222 else if (!max)
1223 max = ei;
1224 }
Zheng Liuedaa53c2014-11-25 11:45:37 -05001225 spin_unlock(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001226
1227 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1228 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
Zheng Liuedaa53c2014-11-25 11:45:37 -05001229 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
Zheng Liueb68d0e2014-09-01 22:26:49 -04001230 seq_printf(seq, " %lu/%lu cache hits/misses\n",
1231 es_stats->es_stats_cache_hits,
1232 es_stats->es_stats_cache_misses);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001233 if (inode_cnt)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001234 seq_printf(seq, " %d inodes on list\n", inode_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001235
1236 seq_printf(seq, "average:\n %llu us scan time\n",
1237 div_u64(es_stats->es_stats_scan_time, 1000));
1238 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1239 if (inode_cnt)
1240 seq_printf(seq,
1241 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1242 " %llu us max scan time\n",
Zheng Liuedaa53c2014-11-25 11:45:37 -05001243 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001244 div_u64(es_stats->es_stats_max_scan_time, 1000));
1245
1246 return 0;
1247}
1248
Zheng Liueb68d0e2014-09-01 22:26:49 -04001249int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1250{
1251 int err;
1252
Jan Kara624d0f12014-11-25 11:53:47 -05001253 /* Make sure we have enough bits for physical block number */
1254 BUILD_BUG_ON(ES_SHIFT < 48);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001255 INIT_LIST_HEAD(&sbi->s_es_list);
1256 sbi->s_es_nr_inode = 0;
1257 spin_lock_init(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001258 sbi->s_es_stats.es_stats_shrunk = 0;
1259 sbi->s_es_stats.es_stats_cache_hits = 0;
1260 sbi->s_es_stats.es_stats_cache_misses = 0;
1261 sbi->s_es_stats.es_stats_scan_time = 0;
1262 sbi->s_es_stats.es_stats_max_scan_time = 0;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001263 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001264 if (err)
1265 return err;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001266 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001267 if (err)
1268 goto err1;
1269
Dave Chinner1ab6c492013-08-28 10:18:09 +10001270 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1271 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001272 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001273 err = register_shrinker(&sbi->s_es_shrinker);
1274 if (err)
1275 goto err2;
1276
Zheng Liueb68d0e2014-09-01 22:26:49 -04001277 return 0;
1278
1279err2:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001280 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001281err1:
1282 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1283 return err;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001284}
1285
Zheng Liud3922a72013-07-01 08:12:37 -04001286void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001287{
Zheng Liueb68d0e2014-09-01 22:26:49 -04001288 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001289 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liud3922a72013-07-01 08:12:37 -04001290 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001291}
1292
Jan Karadd475922014-11-25 11:51:23 -05001293/*
1294 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1295 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1296 *
1297 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1298 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1299 * ei->i_es_shrink_lblk to where we should continue scanning.
1300 */
1301static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1302 int *nr_to_scan, int *nr_shrunk)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001303{
1304 struct inode *inode = &ei->vfs_inode;
1305 struct ext4_es_tree *tree = &ei->i_es_tree;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001306 struct extent_status *es;
Jan Karadd475922014-11-25 11:51:23 -05001307 struct rb_node *node;
1308
1309 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1310 if (!es)
1311 goto out_wrap;
1312 node = &es->rb_node;
1313 while (*nr_to_scan > 0) {
1314 if (es->es_lblk > end) {
1315 ei->i_es_shrink_lblk = end + 1;
1316 return 0;
1317 }
1318
1319 (*nr_to_scan)--;
1320 node = rb_next(&es->rb_node);
1321 /*
1322 * We can't reclaim delayed extent from status tree because
1323 * fiemap, bigallic, and seek_data/hole need to use it.
1324 */
Jan Kara2be12de2014-11-25 11:55:24 -05001325 if (ext4_es_is_delayed(es))
1326 goto next;
1327 if (ext4_es_is_referenced(es)) {
1328 ext4_es_clear_referenced(es);
1329 goto next;
Jan Karadd475922014-11-25 11:51:23 -05001330 }
Jan Kara2be12de2014-11-25 11:55:24 -05001331
1332 rb_erase(&es->rb_node, &tree->root);
1333 ext4_es_free_extent(inode, es);
1334 (*nr_shrunk)++;
1335next:
Jan Karadd475922014-11-25 11:51:23 -05001336 if (!node)
1337 goto out_wrap;
1338 es = rb_entry(node, struct extent_status, rb_node);
1339 }
1340 ei->i_es_shrink_lblk = es->es_lblk;
1341 return 1;
1342out_wrap:
1343 ei->i_es_shrink_lblk = 0;
1344 return 0;
1345}
1346
1347static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1348{
1349 struct inode *inode = &ei->vfs_inode;
1350 int nr_shrunk = 0;
1351 ext4_lblk_t start = ei->i_es_shrink_lblk;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001352 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1353 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001354
Zheng Liuedaa53c2014-11-25 11:45:37 -05001355 if (ei->i_es_shk_nr == 0)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001356 return 0;
1357
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001358 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1359 __ratelimit(&_rs))
1360 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1361
Jan Karadd475922014-11-25 11:51:23 -05001362 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1363 start != 0)
1364 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1365
1366 ei->i_es_tree.cache_es = NULL;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001367 return nr_shrunk;
1368}
Eric Whitney1dc0aa42018-10-01 14:17:41 -04001369
1370#ifdef ES_DEBUG__
1371static void ext4_print_pending_tree(struct inode *inode)
1372{
1373 struct ext4_pending_tree *tree;
1374 struct rb_node *node;
1375 struct pending_reservation *pr;
1376
1377 printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino);
1378 tree = &EXT4_I(inode)->i_pending_tree;
1379 node = rb_first(&tree->root);
1380 while (node) {
1381 pr = rb_entry(node, struct pending_reservation, rb_node);
1382 printk(KERN_DEBUG " %u", pr->lclu);
1383 node = rb_next(node);
1384 }
1385 printk(KERN_DEBUG "\n");
1386}
1387#else
1388#define ext4_print_pending_tree(inode)
1389#endif
1390
1391int __init ext4_init_pending(void)
1392{
1393 ext4_pending_cachep = kmem_cache_create("ext4_pending_reservation",
1394 sizeof(struct pending_reservation),
1395 0, (SLAB_RECLAIM_ACCOUNT), NULL);
1396 if (ext4_pending_cachep == NULL)
1397 return -ENOMEM;
1398 return 0;
1399}
1400
1401void ext4_exit_pending(void)
1402{
1403 kmem_cache_destroy(ext4_pending_cachep);
1404}
1405
1406void ext4_init_pending_tree(struct ext4_pending_tree *tree)
1407{
1408 tree->root = RB_ROOT;
1409}
1410
1411/*
1412 * __get_pending - retrieve a pointer to a pending reservation
1413 *
1414 * @inode - file containing the pending cluster reservation
1415 * @lclu - logical cluster of interest
1416 *
1417 * Returns a pointer to a pending reservation if it's a member of
1418 * the set, and NULL if not. Must be called holding i_es_lock.
1419 */
1420static struct pending_reservation *__get_pending(struct inode *inode,
1421 ext4_lblk_t lclu)
1422{
1423 struct ext4_pending_tree *tree;
1424 struct rb_node *node;
1425 struct pending_reservation *pr = NULL;
1426
1427 tree = &EXT4_I(inode)->i_pending_tree;
1428 node = (&tree->root)->rb_node;
1429
1430 while (node) {
1431 pr = rb_entry(node, struct pending_reservation, rb_node);
1432 if (lclu < pr->lclu)
1433 node = node->rb_left;
1434 else if (lclu > pr->lclu)
1435 node = node->rb_right;
1436 else if (lclu == pr->lclu)
1437 return pr;
1438 }
1439 return NULL;
1440}
1441
1442/*
1443 * __insert_pending - adds a pending cluster reservation to the set of
1444 * pending reservations
1445 *
1446 * @inode - file containing the cluster
1447 * @lblk - logical block in the cluster to be added
1448 *
1449 * Returns 0 on successful insertion and -ENOMEM on failure. If the
1450 * pending reservation is already in the set, returns successfully.
1451 */
1452static int __insert_pending(struct inode *inode, ext4_lblk_t lblk)
1453{
1454 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1455 struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1456 struct rb_node **p = &tree->root.rb_node;
1457 struct rb_node *parent = NULL;
1458 struct pending_reservation *pr;
1459 ext4_lblk_t lclu;
1460 int ret = 0;
1461
1462 lclu = EXT4_B2C(sbi, lblk);
1463 /* search to find parent for insertion */
1464 while (*p) {
1465 parent = *p;
1466 pr = rb_entry(parent, struct pending_reservation, rb_node);
1467
1468 if (lclu < pr->lclu) {
1469 p = &(*p)->rb_left;
1470 } else if (lclu > pr->lclu) {
1471 p = &(*p)->rb_right;
1472 } else {
1473 /* pending reservation already inserted */
1474 goto out;
1475 }
1476 }
1477
1478 pr = kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC);
1479 if (pr == NULL) {
1480 ret = -ENOMEM;
1481 goto out;
1482 }
1483 pr->lclu = lclu;
1484
1485 rb_link_node(&pr->rb_node, parent, p);
1486 rb_insert_color(&pr->rb_node, &tree->root);
1487
1488out:
1489 return ret;
1490}
1491
1492/*
1493 * __remove_pending - removes a pending cluster reservation from the set
1494 * of pending reservations
1495 *
1496 * @inode - file containing the cluster
1497 * @lblk - logical block in the pending cluster reservation to be removed
1498 *
1499 * Returns successfully if pending reservation is not a member of the set.
1500 */
1501static void __remove_pending(struct inode *inode, ext4_lblk_t lblk)
1502{
1503 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1504 struct pending_reservation *pr;
1505 struct ext4_pending_tree *tree;
1506
1507 pr = __get_pending(inode, EXT4_B2C(sbi, lblk));
1508 if (pr != NULL) {
1509 tree = &EXT4_I(inode)->i_pending_tree;
1510 rb_erase(&pr->rb_node, &tree->root);
1511 kmem_cache_free(ext4_pending_cachep, pr);
1512 }
1513}
1514
1515/*
1516 * ext4_remove_pending - removes a pending cluster reservation from the set
1517 * of pending reservations
1518 *
1519 * @inode - file containing the cluster
1520 * @lblk - logical block in the pending cluster reservation to be removed
1521 *
1522 * Locking for external use of __remove_pending.
1523 */
1524void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk)
1525{
1526 struct ext4_inode_info *ei = EXT4_I(inode);
1527
1528 write_lock(&ei->i_es_lock);
1529 __remove_pending(inode, lblk);
1530 write_unlock(&ei->i_es_lock);
1531}
1532
1533/*
1534 * ext4_is_pending - determine whether a cluster has a pending reservation
1535 * on it
1536 *
1537 * @inode - file containing the cluster
1538 * @lblk - logical block in the cluster
1539 *
1540 * Returns true if there's a pending reservation for the cluster in the
1541 * set of pending reservations, and false if not.
1542 */
1543bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk)
1544{
1545 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1546 struct ext4_inode_info *ei = EXT4_I(inode);
1547 bool ret;
1548
1549 read_lock(&ei->i_es_lock);
1550 ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL);
1551 read_unlock(&ei->i_es_lock);
1552
1553 return ret;
1554}