blob: 0a729027322ddc116c35ab387528f2eba8b99133 [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,
Eric Whitney8fcc3a52019-08-22 23:22:14 -0400149 ext4_lblk_t end, int *reserved);
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);
Eric Whitneyb6bf9172018-10-01 14:24:08 -0400153static void __revise_pending(struct inode *inode, ext4_lblk_t lblk,
154 ext4_lblk_t len);
Zheng Liu06b0c882013-02-18 00:26:51 -0500155
Zheng Liu654598b2012-11-08 21:57:20 -0500156int __init ext4_init_es(void)
157{
Theodore Ts'o24630772013-02-28 23:58:56 -0500158 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
159 sizeof(struct extent_status),
160 0, (SLAB_RECLAIM_ACCOUNT), NULL);
Zheng Liu654598b2012-11-08 21:57:20 -0500161 if (ext4_es_cachep == NULL)
162 return -ENOMEM;
163 return 0;
164}
165
166void ext4_exit_es(void)
167{
Sean Fu21c580d2018-05-20 22:44:13 -0400168 kmem_cache_destroy(ext4_es_cachep);
Zheng Liu654598b2012-11-08 21:57:20 -0500169}
170
171void ext4_es_init_tree(struct ext4_es_tree *tree)
172{
173 tree->root = RB_ROOT;
174 tree->cache_es = NULL;
175}
176
177#ifdef ES_DEBUG__
178static void ext4_es_print_tree(struct inode *inode)
179{
180 struct ext4_es_tree *tree;
181 struct rb_node *node;
182
183 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
184 tree = &EXT4_I(inode)->i_es_tree;
185 node = rb_first(&tree->root);
186 while (node) {
187 struct extent_status *es;
188 es = rb_entry(node, struct extent_status, rb_node);
Eric Whitneyce140cd2014-02-20 16:09:12 -0500189 printk(KERN_DEBUG " [%u/%u) %llu %x",
Zheng Liufdc02122013-02-18 00:26:51 -0500190 es->es_lblk, es->es_len,
191 ext4_es_pblock(es), ext4_es_status(es));
Zheng Liu654598b2012-11-08 21:57:20 -0500192 node = rb_next(node);
193 }
194 printk(KERN_DEBUG "\n");
195}
196#else
197#define ext4_es_print_tree(inode)
198#endif
199
Zheng Liu06b0c882013-02-18 00:26:51 -0500200static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500201{
Zheng Liu06b0c882013-02-18 00:26:51 -0500202 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
203 return es->es_lblk + es->es_len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500204}
205
206/*
207 * search through the tree for an delayed extent with a given offset. If
208 * it can't be found, try to find next extent.
209 */
210static struct extent_status *__es_tree_search(struct rb_root *root,
Zheng Liu06b0c882013-02-18 00:26:51 -0500211 ext4_lblk_t lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500212{
213 struct rb_node *node = root->rb_node;
214 struct extent_status *es = NULL;
215
216 while (node) {
217 es = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500218 if (lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500219 node = node->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500220 else if (lblk > ext4_es_end(es))
Zheng Liu654598b2012-11-08 21:57:20 -0500221 node = node->rb_right;
222 else
223 return es;
224 }
225
Zheng Liu06b0c882013-02-18 00:26:51 -0500226 if (es && lblk < es->es_lblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500227 return es;
228
Zheng Liu06b0c882013-02-18 00:26:51 -0500229 if (es && lblk > ext4_es_end(es)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500230 node = rb_next(&es->rb_node);
231 return node ? rb_entry(node, struct extent_status, rb_node) :
232 NULL;
233 }
234
235 return NULL;
236}
237
238/*
Eric Whitneyad431022018-10-01 14:10:39 -0400239 * ext4_es_find_extent_range - find extent with specified status within block
240 * range or next extent following block range in
241 * extents status tree
Zheng Liu654598b2012-11-08 21:57:20 -0500242 *
Eric Whitneyad431022018-10-01 14:10:39 -0400243 * @inode - file containing the range
244 * @matching_fn - pointer to function that matches extents with desired status
245 * @lblk - logical block defining start of range
246 * @end - logical block defining end of range
247 * @es - extent found, if any
248 *
249 * Find the first extent within the block range specified by @lblk and @end
250 * in the extents status tree that satisfies @matching_fn. If a match
251 * is found, it's returned in @es. If not, and a matching extent is found
252 * beyond the block range, it's returned in @es. If no match is found, an
253 * extent is returned in @es whose es_lblk, es_len, and es_pblk components
254 * are 0.
Zheng Liu654598b2012-11-08 21:57:20 -0500255 */
Eric Whitneyad431022018-10-01 14:10:39 -0400256static void __es_find_extent_range(struct inode *inode,
257 int (*matching_fn)(struct extent_status *es),
258 ext4_lblk_t lblk, ext4_lblk_t end,
259 struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500260{
261 struct ext4_es_tree *tree = NULL;
262 struct extent_status *es1 = NULL;
263 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -0500264
Eric Whitneyad431022018-10-01 14:10:39 -0400265 WARN_ON(es == NULL);
266 WARN_ON(end < lblk);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500267
Zheng Liu654598b2012-11-08 21:57:20 -0500268 tree = &EXT4_I(inode)->i_es_tree;
269
Eric Whitneyad431022018-10-01 14:10:39 -0400270 /* see if the extent has been cached */
Zheng Liube401362013-02-18 00:27:26 -0500271 es->es_lblk = es->es_len = es->es_pblk = 0;
Zheng Liu654598b2012-11-08 21:57:20 -0500272 if (tree->cache_es) {
273 es1 = tree->cache_es;
Zheng Liube401362013-02-18 00:27:26 -0500274 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400275 es_debug("%u cached by [%u/%u) %llu %x\n",
Zheng Liube401362013-02-18 00:27:26 -0500276 lblk, es1->es_lblk, es1->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500277 ext4_es_pblock(es1), ext4_es_status(es1));
Zheng Liu654598b2012-11-08 21:57:20 -0500278 goto out;
279 }
280 }
281
Zheng Liube401362013-02-18 00:27:26 -0500282 es1 = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500283
284out:
Eric Whitneyad431022018-10-01 14:10:39 -0400285 if (es1 && !matching_fn(es1)) {
Zheng Liube401362013-02-18 00:27:26 -0500286 while ((node = rb_next(&es1->rb_node)) != NULL) {
287 es1 = rb_entry(node, struct extent_status, rb_node);
Yan, Zhenge30b5dc2013-05-03 02:15:52 -0400288 if (es1->es_lblk > end) {
289 es1 = NULL;
290 break;
291 }
Eric Whitneyad431022018-10-01 14:10:39 -0400292 if (matching_fn(es1))
Zheng Liube401362013-02-18 00:27:26 -0500293 break;
294 }
295 }
296
Eric Whitneyad431022018-10-01 14:10:39 -0400297 if (es1 && matching_fn(es1)) {
Zheng Liu654598b2012-11-08 21:57:20 -0500298 tree->cache_es = es1;
Zheng Liu06b0c882013-02-18 00:26:51 -0500299 es->es_lblk = es1->es_lblk;
300 es->es_len = es1->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500301 es->es_pblk = es1->es_pblk;
Zheng Liu654598b2012-11-08 21:57:20 -0500302 }
303
Eric Whitneyad431022018-10-01 14:10:39 -0400304}
305
306/*
307 * Locking for __es_find_extent_range() for external use
308 */
309void ext4_es_find_extent_range(struct inode *inode,
310 int (*matching_fn)(struct extent_status *es),
311 ext4_lblk_t lblk, ext4_lblk_t end,
312 struct extent_status *es)
313{
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700314 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
315 return;
316
Eric Whitneyad431022018-10-01 14:10:39 -0400317 trace_ext4_es_find_extent_range_enter(inode, lblk);
318
319 read_lock(&EXT4_I(inode)->i_es_lock);
320 __es_find_extent_range(inode, matching_fn, lblk, end, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500321 read_unlock(&EXT4_I(inode)->i_es_lock);
Zheng Liu992e9fd2012-11-08 21:57:33 -0500322
Eric Whitneyad431022018-10-01 14:10:39 -0400323 trace_ext4_es_find_extent_range_exit(inode, es);
324}
325
326/*
327 * __es_scan_range - search block range for block with specified status
328 * in extents status tree
329 *
330 * @inode - file containing the range
331 * @matching_fn - pointer to function that matches extents with desired status
332 * @lblk - logical block defining start of range
333 * @end - logical block defining end of range
334 *
335 * Returns true if at least one block in the specified block range satisfies
336 * the criterion specified by @matching_fn, and false if not. If at least
337 * one extent has the specified status, then there is at least one block
338 * in the cluster with that status. Should only be called by code that has
339 * taken i_es_lock.
340 */
341static bool __es_scan_range(struct inode *inode,
342 int (*matching_fn)(struct extent_status *es),
343 ext4_lblk_t start, ext4_lblk_t end)
344{
345 struct extent_status es;
346
347 __es_find_extent_range(inode, matching_fn, start, end, &es);
348 if (es.es_len == 0)
349 return false; /* no matching extent in the tree */
350 else if (es.es_lblk <= start &&
351 start < es.es_lblk + es.es_len)
352 return true;
353 else if (start <= es.es_lblk && es.es_lblk <= end)
354 return true;
355 else
356 return false;
357}
358/*
359 * Locking for __es_scan_range() for external use
360 */
361bool ext4_es_scan_range(struct inode *inode,
362 int (*matching_fn)(struct extent_status *es),
363 ext4_lblk_t lblk, ext4_lblk_t end)
364{
365 bool ret;
366
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700367 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
368 return false;
369
Eric Whitneyad431022018-10-01 14:10:39 -0400370 read_lock(&EXT4_I(inode)->i_es_lock);
371 ret = __es_scan_range(inode, matching_fn, lblk, end);
372 read_unlock(&EXT4_I(inode)->i_es_lock);
373
374 return ret;
375}
376
377/*
378 * __es_scan_clu - search cluster for block with specified status in
379 * extents status tree
380 *
381 * @inode - file containing the cluster
382 * @matching_fn - pointer to function that matches extents with desired status
383 * @lblk - logical block in cluster to be searched
384 *
385 * Returns true if at least one extent in the cluster containing @lblk
386 * satisfies the criterion specified by @matching_fn, and false if not. If at
387 * least one extent has the specified status, then there is at least one block
388 * in the cluster with that status. Should only be called by code that has
389 * taken i_es_lock.
390 */
391static bool __es_scan_clu(struct inode *inode,
392 int (*matching_fn)(struct extent_status *es),
393 ext4_lblk_t lblk)
394{
395 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
396 ext4_lblk_t lblk_start, lblk_end;
397
398 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
399 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
400
401 return __es_scan_range(inode, matching_fn, lblk_start, lblk_end);
402}
403
404/*
405 * Locking for __es_scan_clu() for external use
406 */
407bool ext4_es_scan_clu(struct inode *inode,
408 int (*matching_fn)(struct extent_status *es),
409 ext4_lblk_t lblk)
410{
411 bool ret;
412
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700413 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
414 return false;
415
Eric Whitneyad431022018-10-01 14:10:39 -0400416 read_lock(&EXT4_I(inode)->i_es_lock);
417 ret = __es_scan_clu(inode, matching_fn, lblk);
418 read_unlock(&EXT4_I(inode)->i_es_lock);
419
420 return ret;
Zheng Liu654598b2012-11-08 21:57:20 -0500421}
422
Jan Karab0dea4c2014-11-25 11:49:25 -0500423static void ext4_es_list_add(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500424{
425 struct ext4_inode_info *ei = EXT4_I(inode);
426 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
427
428 if (!list_empty(&ei->i_es_list))
429 return;
430
431 spin_lock(&sbi->s_es_lock);
432 if (list_empty(&ei->i_es_list)) {
433 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
434 sbi->s_es_nr_inode++;
435 }
436 spin_unlock(&sbi->s_es_lock);
437}
438
Jan Karab0dea4c2014-11-25 11:49:25 -0500439static void ext4_es_list_del(struct inode *inode)
Zheng Liuedaa53c2014-11-25 11:45:37 -0500440{
441 struct ext4_inode_info *ei = EXT4_I(inode);
442 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
443
444 spin_lock(&sbi->s_es_lock);
445 if (!list_empty(&ei->i_es_list)) {
446 list_del_init(&ei->i_es_list);
447 sbi->s_es_nr_inode--;
448 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
449 }
450 spin_unlock(&sbi->s_es_lock);
451}
452
Zheng Liu654598b2012-11-08 21:57:20 -0500453static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500454ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
455 ext4_fsblk_t pblk)
Zheng Liu654598b2012-11-08 21:57:20 -0500456{
457 struct extent_status *es;
458 es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
459 if (es == NULL)
460 return NULL;
Zheng Liu06b0c882013-02-18 00:26:51 -0500461 es->es_lblk = lblk;
462 es->es_len = len;
Zheng Liufdc02122013-02-18 00:26:51 -0500463 es->es_pblk = pblk;
Zheng Liu74cd15c2013-02-18 00:32:55 -0500464
465 /*
466 * We don't count delayed extent because we never try to reclaim them
467 */
Theodore Ts'o24630772013-02-28 23:58:56 -0500468 if (!ext4_es_is_delayed(es)) {
Jan Karab0dea4c2014-11-25 11:49:25 -0500469 if (!EXT4_I(inode)->i_es_shk_nr++)
470 ext4_es_list_add(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400471 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500472 s_es_stats.es_stats_shk_cnt);
Theodore Ts'o24630772013-02-28 23:58:56 -0500473 }
Zheng Liu74cd15c2013-02-18 00:32:55 -0500474
Zheng Liueb68d0e2014-09-01 22:26:49 -0400475 EXT4_I(inode)->i_es_all_nr++;
476 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
477
Zheng Liu654598b2012-11-08 21:57:20 -0500478 return es;
479}
480
Zheng Liubdedbb72013-02-18 00:32:02 -0500481static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500482{
Zheng Liueb68d0e2014-09-01 22:26:49 -0400483 EXT4_I(inode)->i_es_all_nr--;
484 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
485
Zheng Liuedaa53c2014-11-25 11:45:37 -0500486 /* Decrease the shrink counter when this es is not delayed */
Zheng Liu74cd15c2013-02-18 00:32:55 -0500487 if (!ext4_es_is_delayed(es)) {
Zheng Liuedaa53c2014-11-25 11:45:37 -0500488 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
Jan Karab0dea4c2014-11-25 11:49:25 -0500489 if (!--EXT4_I(inode)->i_es_shk_nr)
490 ext4_es_list_del(inode);
Zheng Liueb68d0e2014-09-01 22:26:49 -0400491 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
Zheng Liuedaa53c2014-11-25 11:45:37 -0500492 s_es_stats.es_stats_shk_cnt);
Zheng Liu74cd15c2013-02-18 00:32:55 -0500493 }
494
Zheng Liu654598b2012-11-08 21:57:20 -0500495 kmem_cache_free(ext4_es_cachep, es);
496}
497
Zheng Liu06b0c882013-02-18 00:26:51 -0500498/*
499 * Check whether or not two extents can be merged
500 * Condition:
501 * - logical block number is contiguous
Zheng Liufdc02122013-02-18 00:26:51 -0500502 * - physical block number is contiguous
503 * - status is equal
Zheng Liu06b0c882013-02-18 00:26:51 -0500504 */
505static int ext4_es_can_be_merged(struct extent_status *es1,
506 struct extent_status *es2)
507{
Jan Kara2be12de2014-11-25 11:55:24 -0500508 if (ext4_es_type(es1) != ext4_es_type(es2))
Zheng Liufdc02122013-02-18 00:26:51 -0500509 return 0;
510
Lukas Czerner0baaea62014-05-12 22:21:43 -0400511 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
512 pr_warn("ES assertion failed when merging extents. "
513 "The sum of lengths of es1 (%d) and es2 (%d) "
514 "is bigger than allowed file size (%d)\n",
515 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
516 WARN_ON(1);
Zheng Liufdc02122013-02-18 00:26:51 -0500517 return 0;
Lukas Czerner0baaea62014-05-12 22:21:43 -0400518 }
Zheng Liufdc02122013-02-18 00:26:51 -0500519
Zheng Liubd384362013-03-10 20:48:59 -0400520 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
521 return 0;
522
523 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
524 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
525 return 1;
526
527 if (ext4_es_is_hole(es1))
528 return 1;
529
530 /* we need to check delayed extent is without unwritten status */
531 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
532 return 1;
533
534 return 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500535}
536
Zheng Liu654598b2012-11-08 21:57:20 -0500537static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500538ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500539{
Zheng Liubdedbb72013-02-18 00:32:02 -0500540 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500541 struct extent_status *es1;
542 struct rb_node *node;
543
544 node = rb_prev(&es->rb_node);
545 if (!node)
546 return es;
547
548 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500549 if (ext4_es_can_be_merged(es1, es)) {
550 es1->es_len += es->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500551 if (ext4_es_is_referenced(es))
552 ext4_es_set_referenced(es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500553 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500554 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500555 es = es1;
556 }
557
558 return es;
559}
560
561static struct extent_status *
Zheng Liubdedbb72013-02-18 00:32:02 -0500562ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
Zheng Liu654598b2012-11-08 21:57:20 -0500563{
Zheng Liubdedbb72013-02-18 00:32:02 -0500564 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500565 struct extent_status *es1;
566 struct rb_node *node;
567
568 node = rb_next(&es->rb_node);
569 if (!node)
570 return es;
571
572 es1 = rb_entry(node, struct extent_status, rb_node);
Zheng Liu06b0c882013-02-18 00:26:51 -0500573 if (ext4_es_can_be_merged(es, es1)) {
574 es->es_len += es1->es_len;
Jan Kara2be12de2014-11-25 11:55:24 -0500575 if (ext4_es_is_referenced(es1))
576 ext4_es_set_referenced(es);
Zheng Liu654598b2012-11-08 21:57:20 -0500577 rb_erase(node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -0500578 ext4_es_free_extent(inode, es1);
Zheng Liu654598b2012-11-08 21:57:20 -0500579 }
580
581 return es;
582}
583
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400584#ifdef ES_AGGRESSIVE_TEST
Zheng Liud7b2a002013-08-28 14:47:06 -0400585#include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
586
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400587static void ext4_es_insert_extent_ext_check(struct inode *inode,
588 struct extent_status *es)
589{
590 struct ext4_ext_path *path = NULL;
591 struct ext4_extent *ex;
592 ext4_lblk_t ee_block;
593 ext4_fsblk_t ee_start;
594 unsigned short ee_len;
595 int depth, ee_status, es_status;
596
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400597 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400598 if (IS_ERR(path))
599 return;
600
601 depth = ext_depth(inode);
602 ex = path[depth].p_ext;
603
604 if (ex) {
605
606 ee_block = le32_to_cpu(ex->ee_block);
607 ee_start = ext4_ext_pblock(ex);
608 ee_len = ext4_ext_get_actual_len(ex);
609
Lukas Czerner556615d2014-04-20 23:45:47 -0400610 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400611 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
612
613 /*
614 * Make sure ex and es are not overlap when we try to insert
615 * a delayed/hole extent.
616 */
617 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
618 if (in_range(es->es_lblk, ee_block, ee_len)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400619 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400620 "inode: %lu we can find an extent "
621 "at block [%d/%d/%llu/%c], but we "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500622 "want to add a delayed/hole extent "
623 "[%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400624 inode->i_ino, ee_block, ee_len,
625 ee_start, ee_status ? 'u' : 'w',
626 es->es_lblk, es->es_len,
627 ext4_es_pblock(es), ext4_es_status(es));
628 }
629 goto out;
630 }
631
632 /*
633 * We don't check ee_block == es->es_lblk, etc. because es
634 * might be a part of whole extent, vice versa.
635 */
636 if (es->es_lblk < ee_block ||
637 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400638 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400639 "ex_status [%d/%d/%llu/%c] != "
640 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
641 ee_block, ee_len, ee_start,
642 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
643 ext4_es_pblock(es), es_status ? 'u' : 'w');
644 goto out;
645 }
646
647 if (ee_status ^ es_status) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400648 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400649 "ex_status [%d/%d/%llu/%c] != "
650 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
651 ee_block, ee_len, ee_start,
652 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
653 ext4_es_pblock(es), es_status ? 'u' : 'w');
654 }
655 } else {
656 /*
657 * We can't find an extent on disk. So we need to make sure
658 * that we don't want to add an written/unwritten extent.
659 */
660 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400661 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400662 "can't find an extent at block %d but we want "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500663 "to add a written/unwritten extent "
664 "[%d/%d/%llu/%x]\n", inode->i_ino,
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400665 es->es_lblk, es->es_lblk, es->es_len,
666 ext4_es_pblock(es), ext4_es_status(es));
667 }
668 }
669out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400670 ext4_ext_drop_refs(path);
671 kfree(path);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400672}
673
674static void ext4_es_insert_extent_ind_check(struct inode *inode,
675 struct extent_status *es)
676{
677 struct ext4_map_blocks map;
678 int retval;
679
680 /*
681 * Here we call ext4_ind_map_blocks to lookup a block mapping because
682 * 'Indirect' structure is defined in indirect.c. So we couldn't
683 * access direct/indirect tree from outside. It is too dirty to define
684 * this function in indirect.c file.
685 */
686
687 map.m_lblk = es->es_lblk;
688 map.m_len = es->es_len;
689
690 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
691 if (retval > 0) {
692 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
693 /*
694 * We want to add a delayed/hole extent but this
695 * block has been allocated.
696 */
Theodore Ts'obdafe422013-07-13 00:40:31 -0400697 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400698 "We can find blocks but we want to add a "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500699 "delayed/hole extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400700 inode->i_ino, es->es_lblk, es->es_len,
701 ext4_es_pblock(es), ext4_es_status(es));
702 return;
703 } else if (ext4_es_is_written(es)) {
704 if (retval != es->es_len) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400705 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400706 "inode: %lu retval %d != es_len %d\n",
707 inode->i_ino, retval, es->es_len);
708 return;
709 }
710 if (map.m_pblk != ext4_es_pblock(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400711 pr_warn("ES insert assertion failed for "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400712 "inode: %lu m_pblk %llu != "
713 "es_pblk %llu\n",
714 inode->i_ino, map.m_pblk,
715 ext4_es_pblock(es));
716 return;
717 }
718 } else {
719 /*
720 * We don't need to check unwritten extent because
721 * indirect-based file doesn't have it.
722 */
Arnd Bergmann1e83bc82019-04-07 12:24:43 -0400723 BUG();
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400724 }
725 } else if (retval == 0) {
726 if (ext4_es_is_written(es)) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400727 pr_warn("ES insert assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400728 "We can't find the block but we want to add "
Eric Whitneyce140cd2014-02-20 16:09:12 -0500729 "a written extent [%d/%d/%llu/%x]\n",
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400730 inode->i_ino, es->es_lblk, es->es_len,
731 ext4_es_pblock(es), ext4_es_status(es));
732 return;
733 }
734 }
735}
736
737static inline void ext4_es_insert_extent_check(struct inode *inode,
738 struct extent_status *es)
739{
740 /*
741 * We don't need to worry about the race condition because
742 * caller takes i_data_sem locking.
743 */
744 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
745 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
746 ext4_es_insert_extent_ext_check(inode, es);
747 else
748 ext4_es_insert_extent_ind_check(inode, es);
749}
750#else
751static inline void ext4_es_insert_extent_check(struct inode *inode,
752 struct extent_status *es)
753{
754}
755#endif
756
Zheng Liubdedbb72013-02-18 00:32:02 -0500757static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
Zheng Liu654598b2012-11-08 21:57:20 -0500758{
Zheng Liubdedbb72013-02-18 00:32:02 -0500759 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -0500760 struct rb_node **p = &tree->root.rb_node;
761 struct rb_node *parent = NULL;
762 struct extent_status *es;
Zheng Liu654598b2012-11-08 21:57:20 -0500763
764 while (*p) {
765 parent = *p;
766 es = rb_entry(parent, struct extent_status, rb_node);
767
Zheng Liu06b0c882013-02-18 00:26:51 -0500768 if (newes->es_lblk < es->es_lblk) {
769 if (ext4_es_can_be_merged(newes, es)) {
770 /*
771 * Here we can modify es_lblk directly
772 * because it isn't overlapped.
773 */
774 es->es_lblk = newes->es_lblk;
775 es->es_len += newes->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -0500776 if (ext4_es_is_written(es) ||
777 ext4_es_is_unwritten(es))
778 ext4_es_store_pblock(es,
779 newes->es_pblk);
Zheng Liubdedbb72013-02-18 00:32:02 -0500780 es = ext4_es_try_to_merge_left(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500781 goto out;
782 }
783 p = &(*p)->rb_left;
Zheng Liu06b0c882013-02-18 00:26:51 -0500784 } else if (newes->es_lblk > ext4_es_end(es)) {
785 if (ext4_es_can_be_merged(es, newes)) {
786 es->es_len += newes->es_len;
Zheng Liubdedbb72013-02-18 00:32:02 -0500787 es = ext4_es_try_to_merge_right(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -0500788 goto out;
789 }
790 p = &(*p)->rb_right;
791 } else {
Arnd Bergmann1e83bc82019-04-07 12:24:43 -0400792 BUG();
Zheng Liu06b0c882013-02-18 00:26:51 -0500793 return -EINVAL;
Zheng Liu654598b2012-11-08 21:57:20 -0500794 }
795 }
796
Zheng Liubdedbb72013-02-18 00:32:02 -0500797 es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
Zheng Liufdc02122013-02-18 00:26:51 -0500798 newes->es_pblk);
Zheng Liu654598b2012-11-08 21:57:20 -0500799 if (!es)
800 return -ENOMEM;
801 rb_link_node(&es->rb_node, parent, p);
802 rb_insert_color(&es->rb_node, &tree->root);
803
804out:
805 tree->cache_es = es;
806 return 0;
807}
808
809/*
Theodore Ts'obdafe422013-07-13 00:40:31 -0400810 * ext4_es_insert_extent() adds information to an inode's extent
811 * status tree.
Zheng Liu654598b2012-11-08 21:57:20 -0500812 *
813 * Return 0 on success, error code on failure.
814 */
Zheng Liu06b0c882013-02-18 00:26:51 -0500815int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
Zheng Liufdc02122013-02-18 00:26:51 -0500816 ext4_lblk_t len, ext4_fsblk_t pblk,
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400817 unsigned int status)
Zheng Liu654598b2012-11-08 21:57:20 -0500818{
Zheng Liu06b0c882013-02-18 00:26:51 -0500819 struct extent_status newes;
820 ext4_lblk_t end = lblk + len - 1;
Zheng Liu654598b2012-11-08 21:57:20 -0500821 int err = 0;
Eric Whitneyb6bf9172018-10-01 14:24:08 -0400822 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Zheng Liu654598b2012-11-08 21:57:20 -0500823
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700824 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
825 return 0;
826
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400827 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
Zheng Liufdc02122013-02-18 00:26:51 -0500828 lblk, len, pblk, status, inode->i_ino);
Zheng Liu06b0c882013-02-18 00:26:51 -0500829
Eryu Guand4381472013-02-22 15:27:47 -0500830 if (!len)
831 return 0;
832
Zheng Liu06b0c882013-02-18 00:26:51 -0500833 BUG_ON(end < lblk);
834
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400835 if ((status & EXTENT_STATUS_DELAYED) &&
836 (status & EXTENT_STATUS_WRITTEN)) {
837 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
838 " delayed and written which can potentially "
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -0400839 " cause data loss.", lblk, len);
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400840 WARN_ON(1);
841 }
842
Zheng Liu06b0c882013-02-18 00:26:51 -0500843 newes.es_lblk = lblk;
844 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500845 ext4_es_store_pblock_status(&newes, pblk, status);
Zheng Liufdc02122013-02-18 00:26:51 -0500846 trace_ext4_es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -0500847
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400848 ext4_es_insert_extent_check(inode, &newes);
849
Zheng Liu654598b2012-11-08 21:57:20 -0500850 write_lock(&EXT4_I(inode)->i_es_lock);
Eric Whitney8fcc3a52019-08-22 23:22:14 -0400851 err = __es_remove_extent(inode, lblk, end, NULL);
Zheng Liu06b0c882013-02-18 00:26:51 -0500852 if (err != 0)
853 goto error;
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400854retry:
Zheng Liubdedbb72013-02-18 00:32:02 -0500855 err = __es_insert_extent(inode, &newes);
Zheng Liuedaa53c2014-11-25 11:45:37 -0500856 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -0500857 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -0400858 goto retry;
859 if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
860 err = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -0500861
Eric Whitneyb6bf9172018-10-01 14:24:08 -0400862 if (sbi->s_cluster_ratio > 1 && test_opt(inode->i_sb, DELALLOC) &&
863 (status & EXTENT_STATUS_WRITTEN ||
864 status & EXTENT_STATUS_UNWRITTEN))
865 __revise_pending(inode, lblk, len);
866
Zheng Liu06b0c882013-02-18 00:26:51 -0500867error:
Zheng Liu654598b2012-11-08 21:57:20 -0500868 write_unlock(&EXT4_I(inode)->i_es_lock);
869
870 ext4_es_print_tree(inode);
871
872 return err;
873}
874
Zheng Liud100eef2013-02-18 00:29:59 -0500875/*
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400876 * ext4_es_cache_extent() inserts information into the extent status
877 * tree if and only if there isn't information about the range in
878 * question already.
879 */
880void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
881 ext4_lblk_t len, ext4_fsblk_t pblk,
882 unsigned int status)
883{
884 struct extent_status *es;
885 struct extent_status newes;
886 ext4_lblk_t end = lblk + len - 1;
887
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700888 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
889 return;
890
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400891 newes.es_lblk = lblk;
892 newes.es_len = len;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -0500893 ext4_es_store_pblock_status(&newes, pblk, status);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400894 trace_ext4_es_cache_extent(inode, &newes);
895
896 if (!len)
897 return;
898
899 BUG_ON(end < lblk);
900
901 write_lock(&EXT4_I(inode)->i_es_lock);
902
903 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400904 if (!es || es->es_lblk > end)
905 __es_insert_extent(inode, &newes);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400906 write_unlock(&EXT4_I(inode)->i_es_lock);
907}
908
909/*
Zheng Liud100eef2013-02-18 00:29:59 -0500910 * ext4_es_lookup_extent() looks up an extent in extent status tree.
911 *
912 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
913 *
914 * Return: 1 on found, 0 on not
915 */
916int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400917 ext4_lblk_t *next_lblk,
Zheng Liud100eef2013-02-18 00:29:59 -0500918 struct extent_status *es)
919{
920 struct ext4_es_tree *tree;
Zheng Liueb68d0e2014-09-01 22:26:49 -0400921 struct ext4_es_stats *stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500922 struct extent_status *es1 = NULL;
923 struct rb_node *node;
924 int found = 0;
925
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700926 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
927 return 0;
928
Zheng Liud100eef2013-02-18 00:29:59 -0500929 trace_ext4_es_lookup_extent_enter(inode, lblk);
930 es_debug("lookup extent in block %u\n", lblk);
931
932 tree = &EXT4_I(inode)->i_es_tree;
933 read_lock(&EXT4_I(inode)->i_es_lock);
934
935 /* find extent in cache firstly */
936 es->es_lblk = es->es_len = es->es_pblk = 0;
937 if (tree->cache_es) {
938 es1 = tree->cache_es;
939 if (in_range(lblk, es1->es_lblk, es1->es_len)) {
940 es_debug("%u cached by [%u/%u)\n",
941 lblk, es1->es_lblk, es1->es_len);
942 found = 1;
943 goto out;
944 }
945 }
946
947 node = tree->root.rb_node;
948 while (node) {
949 es1 = rb_entry(node, struct extent_status, rb_node);
950 if (lblk < es1->es_lblk)
951 node = node->rb_left;
952 else if (lblk > ext4_es_end(es1))
953 node = node->rb_right;
954 else {
955 found = 1;
956 break;
957 }
958 }
959
960out:
Zheng Liueb68d0e2014-09-01 22:26:49 -0400961 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
Zheng Liud100eef2013-02-18 00:29:59 -0500962 if (found) {
963 BUG_ON(!es1);
964 es->es_lblk = es1->es_lblk;
965 es->es_len = es1->es_len;
966 es->es_pblk = es1->es_pblk;
Jan Kara87d8a742016-03-09 22:26:55 -0500967 if (!ext4_es_is_referenced(es1))
968 ext4_es_set_referenced(es1);
Yang Guo520f8972019-08-28 11:19:23 -0400969 percpu_counter_inc(&stats->es_stats_cache_hits);
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400970 if (next_lblk) {
971 node = rb_next(&es1->rb_node);
972 if (node) {
973 es1 = rb_entry(node, struct extent_status,
974 rb_node);
975 *next_lblk = es1->es_lblk;
976 } else
977 *next_lblk = 0;
978 }
Zheng Liueb68d0e2014-09-01 22:26:49 -0400979 } else {
Yang Guo520f8972019-08-28 11:19:23 -0400980 percpu_counter_inc(&stats->es_stats_cache_misses);
Zheng Liud100eef2013-02-18 00:29:59 -0500981 }
982
983 read_unlock(&EXT4_I(inode)->i_es_lock);
984
985 trace_ext4_es_lookup_extent_exit(inode, es, found);
986 return found;
987}
988
Eric Whitney8fcc3a52019-08-22 23:22:14 -0400989struct rsvd_count {
990 int ndelonly;
991 bool first_do_lblk_found;
992 ext4_lblk_t first_do_lblk;
993 ext4_lblk_t last_do_lblk;
994 struct extent_status *left_es;
995 bool partial;
996 ext4_lblk_t lclu;
997};
998
999/*
1000 * init_rsvd - initialize reserved count data before removing block range
1001 * in file from extent status tree
1002 *
1003 * @inode - file containing range
1004 * @lblk - first block in range
1005 * @es - pointer to first extent in range
1006 * @rc - pointer to reserved count data
1007 *
1008 * Assumes es is not NULL
1009 */
1010static void init_rsvd(struct inode *inode, ext4_lblk_t lblk,
1011 struct extent_status *es, struct rsvd_count *rc)
1012{
1013 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1014 struct rb_node *node;
1015
1016 rc->ndelonly = 0;
1017
1018 /*
1019 * for bigalloc, note the first delonly block in the range has not
1020 * been found, record the extent containing the block to the left of
1021 * the region to be removed, if any, and note that there's no partial
1022 * cluster to track
1023 */
1024 if (sbi->s_cluster_ratio > 1) {
1025 rc->first_do_lblk_found = false;
1026 if (lblk > es->es_lblk) {
1027 rc->left_es = es;
1028 } else {
1029 node = rb_prev(&es->rb_node);
1030 rc->left_es = node ? rb_entry(node,
1031 struct extent_status,
1032 rb_node) : NULL;
1033 }
1034 rc->partial = false;
1035 }
1036}
1037
1038/*
1039 * count_rsvd - count the clusters containing delayed and not unwritten
1040 * (delonly) blocks in a range within an extent and add to
1041 * the running tally in rsvd_count
1042 *
1043 * @inode - file containing extent
1044 * @lblk - first block in range
1045 * @len - length of range in blocks
1046 * @es - pointer to extent containing clusters to be counted
1047 * @rc - pointer to reserved count data
1048 *
1049 * Tracks partial clusters found at the beginning and end of extents so
1050 * they aren't overcounted when they span adjacent extents
1051 */
1052static void count_rsvd(struct inode *inode, ext4_lblk_t lblk, long len,
1053 struct extent_status *es, struct rsvd_count *rc)
1054{
1055 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1056 ext4_lblk_t i, end, nclu;
1057
1058 if (!ext4_es_is_delonly(es))
1059 return;
1060
1061 WARN_ON(len <= 0);
1062
1063 if (sbi->s_cluster_ratio == 1) {
1064 rc->ndelonly += (int) len;
1065 return;
1066 }
1067
1068 /* bigalloc */
1069
1070 i = (lblk < es->es_lblk) ? es->es_lblk : lblk;
1071 end = lblk + (ext4_lblk_t) len - 1;
1072 end = (end > ext4_es_end(es)) ? ext4_es_end(es) : end;
1073
1074 /* record the first block of the first delonly extent seen */
Jason Yan39c0ae12020-04-20 12:29:18 +08001075 if (!rc->first_do_lblk_found) {
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001076 rc->first_do_lblk = i;
1077 rc->first_do_lblk_found = true;
1078 }
1079
1080 /* update the last lblk in the region seen so far */
1081 rc->last_do_lblk = end;
1082
1083 /*
1084 * if we're tracking a partial cluster and the current extent
1085 * doesn't start with it, count it and stop tracking
1086 */
1087 if (rc->partial && (rc->lclu != EXT4_B2C(sbi, i))) {
1088 rc->ndelonly++;
1089 rc->partial = false;
1090 }
1091
1092 /*
1093 * if the first cluster doesn't start on a cluster boundary but
1094 * ends on one, count it
1095 */
1096 if (EXT4_LBLK_COFF(sbi, i) != 0) {
1097 if (end >= EXT4_LBLK_CFILL(sbi, i)) {
1098 rc->ndelonly++;
1099 rc->partial = false;
1100 i = EXT4_LBLK_CFILL(sbi, i) + 1;
1101 }
1102 }
1103
1104 /*
1105 * if the current cluster starts on a cluster boundary, count the
1106 * number of whole delonly clusters in the extent
1107 */
1108 if ((i + sbi->s_cluster_ratio - 1) <= end) {
1109 nclu = (end - i + 1) >> sbi->s_cluster_bits;
1110 rc->ndelonly += nclu;
1111 i += nclu << sbi->s_cluster_bits;
1112 }
1113
1114 /*
1115 * start tracking a partial cluster if there's a partial at the end
1116 * of the current extent and we're not already tracking one
1117 */
1118 if (!rc->partial && i <= end) {
1119 rc->partial = true;
1120 rc->lclu = EXT4_B2C(sbi, i);
1121 }
1122}
1123
1124/*
1125 * __pr_tree_search - search for a pending cluster reservation
1126 *
1127 * @root - root of pending reservation tree
1128 * @lclu - logical cluster to search for
1129 *
1130 * Returns the pending reservation for the cluster identified by @lclu
1131 * if found. If not, returns a reservation for the next cluster if any,
1132 * and if not, returns NULL.
1133 */
1134static struct pending_reservation *__pr_tree_search(struct rb_root *root,
1135 ext4_lblk_t lclu)
1136{
1137 struct rb_node *node = root->rb_node;
1138 struct pending_reservation *pr = NULL;
1139
1140 while (node) {
1141 pr = rb_entry(node, struct pending_reservation, rb_node);
1142 if (lclu < pr->lclu)
1143 node = node->rb_left;
1144 else if (lclu > pr->lclu)
1145 node = node->rb_right;
1146 else
1147 return pr;
1148 }
1149 if (pr && lclu < pr->lclu)
1150 return pr;
1151 if (pr && lclu > pr->lclu) {
1152 node = rb_next(&pr->rb_node);
1153 return node ? rb_entry(node, struct pending_reservation,
1154 rb_node) : NULL;
1155 }
1156 return NULL;
1157}
1158
1159/*
1160 * get_rsvd - calculates and returns the number of cluster reservations to be
1161 * released when removing a block range from the extent status tree
1162 * and releases any pending reservations within the range
1163 *
1164 * @inode - file containing block range
1165 * @end - last block in range
1166 * @right_es - pointer to extent containing next block beyond end or NULL
1167 * @rc - pointer to reserved count data
1168 *
1169 * The number of reservations to be released is equal to the number of
1170 * clusters containing delayed and not unwritten (delonly) blocks within
1171 * the range, minus the number of clusters still containing delonly blocks
1172 * at the ends of the range, and minus the number of pending reservations
1173 * within the range.
1174 */
1175static unsigned int get_rsvd(struct inode *inode, ext4_lblk_t end,
1176 struct extent_status *right_es,
1177 struct rsvd_count *rc)
1178{
1179 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1180 struct pending_reservation *pr;
1181 struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1182 struct rb_node *node;
1183 ext4_lblk_t first_lclu, last_lclu;
1184 bool left_delonly, right_delonly, count_pending;
1185 struct extent_status *es;
1186
1187 if (sbi->s_cluster_ratio > 1) {
1188 /* count any remaining partial cluster */
1189 if (rc->partial)
1190 rc->ndelonly++;
1191
1192 if (rc->ndelonly == 0)
1193 return 0;
1194
1195 first_lclu = EXT4_B2C(sbi, rc->first_do_lblk);
1196 last_lclu = EXT4_B2C(sbi, rc->last_do_lblk);
1197
1198 /*
1199 * decrease the delonly count by the number of clusters at the
1200 * ends of the range that still contain delonly blocks -
1201 * these clusters still need to be reserved
1202 */
1203 left_delonly = right_delonly = false;
1204
1205 es = rc->left_es;
1206 while (es && ext4_es_end(es) >=
1207 EXT4_LBLK_CMASK(sbi, rc->first_do_lblk)) {
1208 if (ext4_es_is_delonly(es)) {
1209 rc->ndelonly--;
1210 left_delonly = true;
1211 break;
1212 }
1213 node = rb_prev(&es->rb_node);
1214 if (!node)
1215 break;
1216 es = rb_entry(node, struct extent_status, rb_node);
1217 }
1218 if (right_es && (!left_delonly || first_lclu != last_lclu)) {
1219 if (end < ext4_es_end(right_es)) {
1220 es = right_es;
1221 } else {
1222 node = rb_next(&right_es->rb_node);
1223 es = node ? rb_entry(node, struct extent_status,
1224 rb_node) : NULL;
1225 }
1226 while (es && es->es_lblk <=
1227 EXT4_LBLK_CFILL(sbi, rc->last_do_lblk)) {
1228 if (ext4_es_is_delonly(es)) {
1229 rc->ndelonly--;
1230 right_delonly = true;
1231 break;
1232 }
1233 node = rb_next(&es->rb_node);
1234 if (!node)
1235 break;
1236 es = rb_entry(node, struct extent_status,
1237 rb_node);
1238 }
1239 }
1240
1241 /*
1242 * Determine the block range that should be searched for
1243 * pending reservations, if any. Clusters on the ends of the
1244 * original removed range containing delonly blocks are
1245 * excluded. They've already been accounted for and it's not
1246 * possible to determine if an associated pending reservation
1247 * should be released with the information available in the
1248 * extents status tree.
1249 */
1250 if (first_lclu == last_lclu) {
1251 if (left_delonly | right_delonly)
1252 count_pending = false;
1253 else
1254 count_pending = true;
1255 } else {
1256 if (left_delonly)
1257 first_lclu++;
1258 if (right_delonly)
1259 last_lclu--;
1260 if (first_lclu <= last_lclu)
1261 count_pending = true;
1262 else
1263 count_pending = false;
1264 }
1265
1266 /*
1267 * a pending reservation found between first_lclu and last_lclu
1268 * represents an allocated cluster that contained at least one
1269 * delonly block, so the delonly total must be reduced by one
1270 * for each pending reservation found and released
1271 */
1272 if (count_pending) {
1273 pr = __pr_tree_search(&tree->root, first_lclu);
1274 while (pr && pr->lclu <= last_lclu) {
1275 rc->ndelonly--;
1276 node = rb_next(&pr->rb_node);
1277 rb_erase(&pr->rb_node, &tree->root);
1278 kmem_cache_free(ext4_pending_cachep, pr);
1279 if (!node)
1280 break;
1281 pr = rb_entry(node, struct pending_reservation,
1282 rb_node);
1283 }
1284 }
1285 }
1286 return rc->ndelonly;
1287}
1288
1289
1290/*
1291 * __es_remove_extent - removes block range from extent status tree
1292 *
1293 * @inode - file containing range
1294 * @lblk - first block in range
1295 * @end - last block in range
1296 * @reserved - number of cluster reservations released
1297 *
1298 * If @reserved is not NULL and delayed allocation is enabled, counts
1299 * block/cluster reservations freed by removing range and if bigalloc
1300 * enabled cancels pending reservations as needed. Returns 0 on success,
1301 * error code on failure.
1302 */
Zheng Liubdedbb72013-02-18 00:32:02 -05001303static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001304 ext4_lblk_t end, int *reserved)
Zheng Liu654598b2012-11-08 21:57:20 -05001305{
Zheng Liubdedbb72013-02-18 00:32:02 -05001306 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
Zheng Liu654598b2012-11-08 21:57:20 -05001307 struct rb_node *node;
Zheng Liu654598b2012-11-08 21:57:20 -05001308 struct extent_status *es;
1309 struct extent_status orig_es;
Zheng Liu06b0c882013-02-18 00:26:51 -05001310 ext4_lblk_t len1, len2;
Zheng Liufdc02122013-02-18 00:26:51 -05001311 ext4_fsblk_t block;
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001312 int err;
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001313 bool count_reserved = true;
1314 struct rsvd_count rc;
Zheng Liu654598b2012-11-08 21:57:20 -05001315
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001316 if (reserved == NULL || !test_opt(inode->i_sb, DELALLOC))
1317 count_reserved = false;
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001318retry:
1319 err = 0;
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001320
Zheng Liu06b0c882013-02-18 00:26:51 -05001321 es = __es_tree_search(&tree->root, lblk);
Zheng Liu654598b2012-11-08 21:57:20 -05001322 if (!es)
1323 goto out;
Zheng Liu06b0c882013-02-18 00:26:51 -05001324 if (es->es_lblk > end)
Zheng Liu654598b2012-11-08 21:57:20 -05001325 goto out;
1326
1327 /* Simply invalidate cache_es. */
1328 tree->cache_es = NULL;
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001329 if (count_reserved)
1330 init_rsvd(inode, lblk, es, &rc);
Zheng Liu654598b2012-11-08 21:57:20 -05001331
Zheng Liu06b0c882013-02-18 00:26:51 -05001332 orig_es.es_lblk = es->es_lblk;
1333 orig_es.es_len = es->es_len;
Zheng Liufdc02122013-02-18 00:26:51 -05001334 orig_es.es_pblk = es->es_pblk;
1335
Zheng Liu06b0c882013-02-18 00:26:51 -05001336 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
1337 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
Zheng Liu654598b2012-11-08 21:57:20 -05001338 if (len1 > 0)
Zheng Liu06b0c882013-02-18 00:26:51 -05001339 es->es_len = len1;
Zheng Liu654598b2012-11-08 21:57:20 -05001340 if (len2 > 0) {
1341 if (len1 > 0) {
Zheng Liu06b0c882013-02-18 00:26:51 -05001342 struct extent_status newes;
1343
1344 newes.es_lblk = end + 1;
1345 newes.es_len = len2;
Chen Gang666525d2014-04-07 10:18:56 -04001346 block = 0x7FDEADBEEFULL;
Zheng Liufdc02122013-02-18 00:26:51 -05001347 if (ext4_es_is_written(&orig_es) ||
Theodore Ts'o9a6633b2014-02-19 20:15:15 -05001348 ext4_es_is_unwritten(&orig_es))
Zheng Liufdc02122013-02-18 00:26:51 -05001349 block = ext4_es_pblock(&orig_es) +
1350 orig_es.es_len - len2;
Theodore Ts'o9a6633b2014-02-19 20:15:15 -05001351 ext4_es_store_pblock_status(&newes, block,
1352 ext4_es_status(&orig_es));
Zheng Liubdedbb72013-02-18 00:32:02 -05001353 err = __es_insert_extent(inode, &newes);
Zheng Liu654598b2012-11-08 21:57:20 -05001354 if (err) {
Zheng Liu06b0c882013-02-18 00:26:51 -05001355 es->es_lblk = orig_es.es_lblk;
1356 es->es_len = orig_es.es_len;
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001357 if ((err == -ENOMEM) &&
Zheng Liuedaa53c2014-11-25 11:45:37 -05001358 __es_shrink(EXT4_SB(inode->i_sb),
Jan Karadd475922014-11-25 11:51:23 -05001359 128, EXT4_I(inode)))
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001360 goto retry;
Zheng Liu654598b2012-11-08 21:57:20 -05001361 goto out;
1362 }
1363 } else {
Zheng Liu06b0c882013-02-18 00:26:51 -05001364 es->es_lblk = end + 1;
1365 es->es_len = len2;
Zheng Liufdc02122013-02-18 00:26:51 -05001366 if (ext4_es_is_written(es) ||
1367 ext4_es_is_unwritten(es)) {
1368 block = orig_es.es_pblk + orig_es.es_len - len2;
1369 ext4_es_store_pblock(es, block);
1370 }
Zheng Liu654598b2012-11-08 21:57:20 -05001371 }
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001372 if (count_reserved)
1373 count_rsvd(inode, lblk, orig_es.es_len - len1 - len2,
1374 &orig_es, &rc);
Zheng Liu654598b2012-11-08 21:57:20 -05001375 goto out;
1376 }
1377
1378 if (len1 > 0) {
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001379 if (count_reserved)
1380 count_rsvd(inode, lblk, orig_es.es_len - len1,
1381 &orig_es, &rc);
Zheng Liu654598b2012-11-08 21:57:20 -05001382 node = rb_next(&es->rb_node);
1383 if (node)
1384 es = rb_entry(node, struct extent_status, rb_node);
1385 else
1386 es = NULL;
1387 }
1388
Zheng Liu06b0c882013-02-18 00:26:51 -05001389 while (es && ext4_es_end(es) <= end) {
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001390 if (count_reserved)
1391 count_rsvd(inode, es->es_lblk, es->es_len, es, &rc);
Zheng Liu654598b2012-11-08 21:57:20 -05001392 node = rb_next(&es->rb_node);
1393 rb_erase(&es->rb_node, &tree->root);
Zheng Liubdedbb72013-02-18 00:32:02 -05001394 ext4_es_free_extent(inode, es);
Zheng Liu654598b2012-11-08 21:57:20 -05001395 if (!node) {
1396 es = NULL;
1397 break;
1398 }
1399 es = rb_entry(node, struct extent_status, rb_node);
1400 }
1401
Zheng Liu06b0c882013-02-18 00:26:51 -05001402 if (es && es->es_lblk < end + 1) {
Zheng Liufdc02122013-02-18 00:26:51 -05001403 ext4_lblk_t orig_len = es->es_len;
1404
Zheng Liu06b0c882013-02-18 00:26:51 -05001405 len1 = ext4_es_end(es) - end;
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001406 if (count_reserved)
1407 count_rsvd(inode, es->es_lblk, orig_len - len1,
1408 es, &rc);
Zheng Liu06b0c882013-02-18 00:26:51 -05001409 es->es_lblk = end + 1;
1410 es->es_len = len1;
Zheng Liufdc02122013-02-18 00:26:51 -05001411 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
1412 block = es->es_pblk + orig_len - len1;
1413 ext4_es_store_pblock(es, block);
1414 }
Zheng Liu654598b2012-11-08 21:57:20 -05001415 }
1416
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001417 if (count_reserved)
1418 *reserved = get_rsvd(inode, end, es, &rc);
Zheng Liu654598b2012-11-08 21:57:20 -05001419out:
Zheng Liu06b0c882013-02-18 00:26:51 -05001420 return err;
1421}
1422
1423/*
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001424 * ext4_es_remove_extent - removes block range from extent status tree
Zheng Liu06b0c882013-02-18 00:26:51 -05001425 *
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001426 * @inode - file containing range
1427 * @lblk - first block in range
1428 * @len - number of blocks to remove
1429 *
1430 * Reduces block/cluster reservation count and for bigalloc cancels pending
1431 * reservations as needed. Returns 0 on success, error code on failure.
Zheng Liu06b0c882013-02-18 00:26:51 -05001432 */
1433int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1434 ext4_lblk_t len)
1435{
Zheng Liu06b0c882013-02-18 00:26:51 -05001436 ext4_lblk_t end;
1437 int err = 0;
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001438 int reserved = 0;
Zheng Liu06b0c882013-02-18 00:26:51 -05001439
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001440 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
1441 return 0;
1442
Zheng Liu06b0c882013-02-18 00:26:51 -05001443 trace_ext4_es_remove_extent(inode, lblk, len);
1444 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
1445 lblk, len, inode->i_ino);
1446
Eryu Guand4381472013-02-22 15:27:47 -05001447 if (!len)
1448 return err;
1449
Zheng Liu06b0c882013-02-18 00:26:51 -05001450 end = lblk + len - 1;
1451 BUG_ON(end < lblk);
1452
Zheng Liuedaa53c2014-11-25 11:45:37 -05001453 /*
1454 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
1455 * so that we are sure __es_shrink() is done with the inode before it
1456 * is reclaimed.
1457 */
Zheng Liu06b0c882013-02-18 00:26:51 -05001458 write_lock(&EXT4_I(inode)->i_es_lock);
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001459 err = __es_remove_extent(inode, lblk, end, &reserved);
Zheng Liu654598b2012-11-08 21:57:20 -05001460 write_unlock(&EXT4_I(inode)->i_es_lock);
1461 ext4_es_print_tree(inode);
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001462 ext4_da_release_space(inode, reserved);
Zheng Liu654598b2012-11-08 21:57:20 -05001463 return err;
1464}
Zheng Liu74cd15c2013-02-18 00:32:55 -05001465
Zheng Liuedaa53c2014-11-25 11:45:37 -05001466static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
1467 struct ext4_inode_info *locked_ei)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001468{
Zheng Liu74cd15c2013-02-18 00:32:55 -05001469 struct ext4_inode_info *ei;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001470 struct ext4_es_stats *es_stats;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001471 ktime_t start_time;
1472 u64 scan_time;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001473 int nr_to_walk;
Dave Chinner1ab6c492013-08-28 10:18:09 +10001474 int nr_shrunk = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001475 int retried = 0, nr_skipped = 0;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001476
Zheng Liueb68d0e2014-09-01 22:26:49 -04001477 es_stats = &sbi->s_es_stats;
1478 start_time = ktime_get();
Zheng Liud3922a72013-07-01 08:12:37 -04001479
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001480retry:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001481 spin_lock(&sbi->s_es_lock);
1482 nr_to_walk = sbi->s_es_nr_inode;
1483 while (nr_to_walk-- > 0) {
Zheng Liuedaa53c2014-11-25 11:45:37 -05001484 if (list_empty(&sbi->s_es_list)) {
1485 spin_unlock(&sbi->s_es_lock);
1486 goto out;
1487 }
1488 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
1489 i_es_list);
1490 /* Move the inode to the tail */
Jan Karadd475922014-11-25 11:51:23 -05001491 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001492
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001493 /*
Zheng Liuedaa53c2014-11-25 11:45:37 -05001494 * Normally we try hard to avoid shrinking precached inodes,
1495 * but we will as a last resort.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001496 */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001497 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1498 EXT4_STATE_EXT_PRECACHED)) {
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001499 nr_skipped++;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001500 continue;
1501 }
Zheng Liud3922a72013-07-01 08:12:37 -04001502
Zheng Liuedaa53c2014-11-25 11:45:37 -05001503 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1504 nr_skipped++;
Zheng Liud3922a72013-07-01 08:12:37 -04001505 continue;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001506 }
1507 /*
1508 * Now we hold i_es_lock which protects us from inode reclaim
1509 * freeing inode under us
1510 */
1511 spin_unlock(&sbi->s_es_lock);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001512
Jan Karadd475922014-11-25 11:51:23 -05001513 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001514 write_unlock(&ei->i_es_lock);
1515
Jan Karadd475922014-11-25 11:51:23 -05001516 if (nr_to_scan <= 0)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001517 goto out;
1518 spin_lock(&sbi->s_es_lock);
1519 }
1520 spin_unlock(&sbi->s_es_lock);
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001521
1522 /*
1523 * If we skipped any inodes, and we weren't able to make any
Zheng Liuedaa53c2014-11-25 11:45:37 -05001524 * forward progress, try again to scan precached inodes.
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001525 */
1526 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1527 retried++;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001528 goto retry;
1529 }
1530
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001531 if (locked_ei && nr_shrunk == 0)
Jan Karadd475922014-11-25 11:51:23 -05001532 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001533
Zheng Liuedaa53c2014-11-25 11:45:37 -05001534out:
Zheng Liueb68d0e2014-09-01 22:26:49 -04001535 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1536 if (likely(es_stats->es_stats_scan_time))
1537 es_stats->es_stats_scan_time = (scan_time +
1538 es_stats->es_stats_scan_time*3) / 4;
1539 else
1540 es_stats->es_stats_scan_time = scan_time;
1541 if (scan_time > es_stats->es_stats_max_scan_time)
1542 es_stats->es_stats_max_scan_time = scan_time;
1543 if (likely(es_stats->es_stats_shrunk))
1544 es_stats->es_stats_shrunk = (nr_shrunk +
1545 es_stats->es_stats_shrunk*3) / 4;
1546 else
1547 es_stats->es_stats_shrunk = nr_shrunk;
1548
Zheng Liuedaa53c2014-11-25 11:45:37 -05001549 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001550 nr_skipped, retried);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001551 return nr_shrunk;
1552}
1553
Dave Chinner1ab6c492013-08-28 10:18:09 +10001554static unsigned long ext4_es_count(struct shrinker *shrink,
1555 struct shrink_control *sc)
1556{
1557 unsigned long nr;
1558 struct ext4_sb_info *sbi;
1559
1560 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001561 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001562 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001563 return nr;
1564}
1565
1566static unsigned long ext4_es_scan(struct shrinker *shrink,
1567 struct shrink_control *sc)
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001568{
1569 struct ext4_sb_info *sbi = container_of(shrink,
1570 struct ext4_sb_info, s_es_shrinker);
1571 int nr_to_scan = sc->nr_to_scan;
1572 int ret, nr_shrunk;
1573
Zheng Liuedaa53c2014-11-25 11:45:37 -05001574 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liue963bb12014-09-01 22:22:13 -04001575 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001576
1577 if (!nr_to_scan)
1578 return ret;
1579
Zheng Liuedaa53c2014-11-25 11:45:37 -05001580 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
Theodore Ts'oe15f7422013-07-15 00:12:14 -04001581
Zheng Liue963bb12014-09-01 22:22:13 -04001582 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001583 return nr_shrunk;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001584}
1585
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001586int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001587{
Theodore Ts'oebd173b2015-09-23 12:46:17 -04001588 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001589 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1590 struct ext4_inode_info *ei, *max = NULL;
1591 unsigned int inode_cnt = 0;
1592
1593 if (v != SEQ_START_TOKEN)
1594 return 0;
1595
1596 /* here we just find an inode that has the max nr. of objects */
Zheng Liuedaa53c2014-11-25 11:45:37 -05001597 spin_lock(&sbi->s_es_lock);
1598 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
Zheng Liueb68d0e2014-09-01 22:26:49 -04001599 inode_cnt++;
1600 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1601 max = ei;
1602 else if (!max)
1603 max = ei;
1604 }
Zheng Liuedaa53c2014-11-25 11:45:37 -05001605 spin_unlock(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001606
1607 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1608 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
Zheng Liuedaa53c2014-11-25 11:45:37 -05001609 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
Yang Guo520f8972019-08-28 11:19:23 -04001610 seq_printf(seq, " %lld/%lld cache hits/misses\n",
1611 percpu_counter_sum_positive(&es_stats->es_stats_cache_hits),
1612 percpu_counter_sum_positive(&es_stats->es_stats_cache_misses));
Zheng Liueb68d0e2014-09-01 22:26:49 -04001613 if (inode_cnt)
Zheng Liuedaa53c2014-11-25 11:45:37 -05001614 seq_printf(seq, " %d inodes on list\n", inode_cnt);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001615
1616 seq_printf(seq, "average:\n %llu us scan time\n",
1617 div_u64(es_stats->es_stats_scan_time, 1000));
1618 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1619 if (inode_cnt)
1620 seq_printf(seq,
1621 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1622 " %llu us max scan time\n",
Zheng Liuedaa53c2014-11-25 11:45:37 -05001623 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
Zheng Liueb68d0e2014-09-01 22:26:49 -04001624 div_u64(es_stats->es_stats_max_scan_time, 1000));
1625
1626 return 0;
1627}
1628
Zheng Liueb68d0e2014-09-01 22:26:49 -04001629int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1630{
1631 int err;
1632
Jan Kara624d0f12014-11-25 11:53:47 -05001633 /* Make sure we have enough bits for physical block number */
1634 BUILD_BUG_ON(ES_SHIFT < 48);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001635 INIT_LIST_HEAD(&sbi->s_es_list);
1636 sbi->s_es_nr_inode = 0;
1637 spin_lock_init(&sbi->s_es_lock);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001638 sbi->s_es_stats.es_stats_shrunk = 0;
Yang Guo520f8972019-08-28 11:19:23 -04001639 err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_hits, 0,
1640 GFP_KERNEL);
1641 if (err)
1642 return err;
1643 err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_misses, 0,
1644 GFP_KERNEL);
1645 if (err)
1646 goto err1;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001647 sbi->s_es_stats.es_stats_scan_time = 0;
1648 sbi->s_es_stats.es_stats_max_scan_time = 0;
Linus Torvaldsc2661b82014-10-20 09:50:11 -07001649 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001650 if (err)
Yang Guo520f8972019-08-28 11:19:23 -04001651 goto err2;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001652 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001653 if (err)
Yang Guo520f8972019-08-28 11:19:23 -04001654 goto err3;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001655
Dave Chinner1ab6c492013-08-28 10:18:09 +10001656 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1657 sbi->s_es_shrinker.count_objects = ext4_es_count;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001658 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001659 err = register_shrinker(&sbi->s_es_shrinker);
1660 if (err)
Yang Guo520f8972019-08-28 11:19:23 -04001661 goto err4;
Zheng Liueb68d0e2014-09-01 22:26:49 -04001662
Zheng Liueb68d0e2014-09-01 22:26:49 -04001663 return 0;
Yang Guo520f8972019-08-28 11:19:23 -04001664err4:
Zheng Liuedaa53c2014-11-25 11:45:37 -05001665 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Yang Guo520f8972019-08-28 11:19:23 -04001666err3:
Zheng Liueb68d0e2014-09-01 22:26:49 -04001667 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
Yang Guo520f8972019-08-28 11:19:23 -04001668err2:
1669 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1670err1:
1671 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001672 return err;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001673}
1674
Zheng Liud3922a72013-07-01 08:12:37 -04001675void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001676{
Yang Guo520f8972019-08-28 11:19:23 -04001677 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1678 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001679 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001680 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
Zheng Liud3922a72013-07-01 08:12:37 -04001681 unregister_shrinker(&sbi->s_es_shrinker);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001682}
1683
Jan Karadd475922014-11-25 11:51:23 -05001684/*
1685 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1686 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1687 *
1688 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1689 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1690 * ei->i_es_shrink_lblk to where we should continue scanning.
1691 */
1692static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1693 int *nr_to_scan, int *nr_shrunk)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001694{
1695 struct inode *inode = &ei->vfs_inode;
1696 struct ext4_es_tree *tree = &ei->i_es_tree;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001697 struct extent_status *es;
Jan Karadd475922014-11-25 11:51:23 -05001698 struct rb_node *node;
1699
1700 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1701 if (!es)
1702 goto out_wrap;
Eric Whitney8fcc3a52019-08-22 23:22:14 -04001703
Jan Karadd475922014-11-25 11:51:23 -05001704 while (*nr_to_scan > 0) {
1705 if (es->es_lblk > end) {
1706 ei->i_es_shrink_lblk = end + 1;
1707 return 0;
1708 }
1709
1710 (*nr_to_scan)--;
1711 node = rb_next(&es->rb_node);
1712 /*
1713 * We can't reclaim delayed extent from status tree because
1714 * fiemap, bigallic, and seek_data/hole need to use it.
1715 */
Jan Kara2be12de2014-11-25 11:55:24 -05001716 if (ext4_es_is_delayed(es))
1717 goto next;
1718 if (ext4_es_is_referenced(es)) {
1719 ext4_es_clear_referenced(es);
1720 goto next;
Jan Karadd475922014-11-25 11:51:23 -05001721 }
Jan Kara2be12de2014-11-25 11:55:24 -05001722
1723 rb_erase(&es->rb_node, &tree->root);
1724 ext4_es_free_extent(inode, es);
1725 (*nr_shrunk)++;
1726next:
Jan Karadd475922014-11-25 11:51:23 -05001727 if (!node)
1728 goto out_wrap;
1729 es = rb_entry(node, struct extent_status, rb_node);
1730 }
1731 ei->i_es_shrink_lblk = es->es_lblk;
1732 return 1;
1733out_wrap:
1734 ei->i_es_shrink_lblk = 0;
1735 return 0;
1736}
1737
1738static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1739{
1740 struct inode *inode = &ei->vfs_inode;
1741 int nr_shrunk = 0;
1742 ext4_lblk_t start = ei->i_es_shrink_lblk;
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001743 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1744 DEFAULT_RATELIMIT_BURST);
Zheng Liu74cd15c2013-02-18 00:32:55 -05001745
Zheng Liuedaa53c2014-11-25 11:45:37 -05001746 if (ei->i_es_shk_nr == 0)
Zheng Liu74cd15c2013-02-18 00:32:55 -05001747 return 0;
1748
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001749 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1750 __ratelimit(&_rs))
1751 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1752
Jan Karadd475922014-11-25 11:51:23 -05001753 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1754 start != 0)
1755 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1756
1757 ei->i_es_tree.cache_es = NULL;
Zheng Liu74cd15c2013-02-18 00:32:55 -05001758 return nr_shrunk;
1759}
Eric Whitney1dc0aa42018-10-01 14:17:41 -04001760
Theodore Ts'ob0c013e2019-08-11 16:30:41 -04001761/*
1762 * Called to support EXT4_IOC_CLEAR_ES_CACHE. We can only remove
1763 * discretionary entries from the extent status cache. (Some entries
1764 * must be present for proper operations.)
1765 */
1766void ext4_clear_inode_es(struct inode *inode)
1767{
1768 struct ext4_inode_info *ei = EXT4_I(inode);
1769 struct extent_status *es;
1770 struct ext4_es_tree *tree;
1771 struct rb_node *node;
1772
1773 write_lock(&ei->i_es_lock);
1774 tree = &EXT4_I(inode)->i_es_tree;
1775 tree->cache_es = NULL;
1776 node = rb_first(&tree->root);
1777 while (node) {
1778 es = rb_entry(node, struct extent_status, rb_node);
1779 node = rb_next(node);
1780 if (!ext4_es_is_delayed(es)) {
1781 rb_erase(&es->rb_node, &tree->root);
1782 ext4_es_free_extent(inode, es);
1783 }
1784 }
1785 ext4_clear_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
1786 write_unlock(&ei->i_es_lock);
1787}
1788
Eric Whitney1dc0aa42018-10-01 14:17:41 -04001789#ifdef ES_DEBUG__
1790static void ext4_print_pending_tree(struct inode *inode)
1791{
1792 struct ext4_pending_tree *tree;
1793 struct rb_node *node;
1794 struct pending_reservation *pr;
1795
1796 printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino);
1797 tree = &EXT4_I(inode)->i_pending_tree;
1798 node = rb_first(&tree->root);
1799 while (node) {
1800 pr = rb_entry(node, struct pending_reservation, rb_node);
1801 printk(KERN_DEBUG " %u", pr->lclu);
1802 node = rb_next(node);
1803 }
1804 printk(KERN_DEBUG "\n");
1805}
1806#else
1807#define ext4_print_pending_tree(inode)
1808#endif
1809
1810int __init ext4_init_pending(void)
1811{
1812 ext4_pending_cachep = kmem_cache_create("ext4_pending_reservation",
1813 sizeof(struct pending_reservation),
1814 0, (SLAB_RECLAIM_ACCOUNT), NULL);
1815 if (ext4_pending_cachep == NULL)
1816 return -ENOMEM;
1817 return 0;
1818}
1819
1820void ext4_exit_pending(void)
1821{
1822 kmem_cache_destroy(ext4_pending_cachep);
1823}
1824
1825void ext4_init_pending_tree(struct ext4_pending_tree *tree)
1826{
1827 tree->root = RB_ROOT;
1828}
1829
1830/*
1831 * __get_pending - retrieve a pointer to a pending reservation
1832 *
1833 * @inode - file containing the pending cluster reservation
1834 * @lclu - logical cluster of interest
1835 *
1836 * Returns a pointer to a pending reservation if it's a member of
1837 * the set, and NULL if not. Must be called holding i_es_lock.
1838 */
1839static struct pending_reservation *__get_pending(struct inode *inode,
1840 ext4_lblk_t lclu)
1841{
1842 struct ext4_pending_tree *tree;
1843 struct rb_node *node;
1844 struct pending_reservation *pr = NULL;
1845
1846 tree = &EXT4_I(inode)->i_pending_tree;
1847 node = (&tree->root)->rb_node;
1848
1849 while (node) {
1850 pr = rb_entry(node, struct pending_reservation, rb_node);
1851 if (lclu < pr->lclu)
1852 node = node->rb_left;
1853 else if (lclu > pr->lclu)
1854 node = node->rb_right;
1855 else if (lclu == pr->lclu)
1856 return pr;
1857 }
1858 return NULL;
1859}
1860
1861/*
1862 * __insert_pending - adds a pending cluster reservation to the set of
1863 * pending reservations
1864 *
1865 * @inode - file containing the cluster
1866 * @lblk - logical block in the cluster to be added
1867 *
1868 * Returns 0 on successful insertion and -ENOMEM on failure. If the
1869 * pending reservation is already in the set, returns successfully.
1870 */
1871static int __insert_pending(struct inode *inode, ext4_lblk_t lblk)
1872{
1873 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1874 struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1875 struct rb_node **p = &tree->root.rb_node;
1876 struct rb_node *parent = NULL;
1877 struct pending_reservation *pr;
1878 ext4_lblk_t lclu;
1879 int ret = 0;
1880
1881 lclu = EXT4_B2C(sbi, lblk);
1882 /* search to find parent for insertion */
1883 while (*p) {
1884 parent = *p;
1885 pr = rb_entry(parent, struct pending_reservation, rb_node);
1886
1887 if (lclu < pr->lclu) {
1888 p = &(*p)->rb_left;
1889 } else if (lclu > pr->lclu) {
1890 p = &(*p)->rb_right;
1891 } else {
1892 /* pending reservation already inserted */
1893 goto out;
1894 }
1895 }
1896
1897 pr = kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC);
1898 if (pr == NULL) {
1899 ret = -ENOMEM;
1900 goto out;
1901 }
1902 pr->lclu = lclu;
1903
1904 rb_link_node(&pr->rb_node, parent, p);
1905 rb_insert_color(&pr->rb_node, &tree->root);
1906
1907out:
1908 return ret;
1909}
1910
1911/*
1912 * __remove_pending - removes a pending cluster reservation from the set
1913 * of pending reservations
1914 *
1915 * @inode - file containing the cluster
1916 * @lblk - logical block in the pending cluster reservation to be removed
1917 *
1918 * Returns successfully if pending reservation is not a member of the set.
1919 */
1920static void __remove_pending(struct inode *inode, ext4_lblk_t lblk)
1921{
1922 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1923 struct pending_reservation *pr;
1924 struct ext4_pending_tree *tree;
1925
1926 pr = __get_pending(inode, EXT4_B2C(sbi, lblk));
1927 if (pr != NULL) {
1928 tree = &EXT4_I(inode)->i_pending_tree;
1929 rb_erase(&pr->rb_node, &tree->root);
1930 kmem_cache_free(ext4_pending_cachep, pr);
1931 }
1932}
1933
1934/*
1935 * ext4_remove_pending - removes a pending cluster reservation from the set
1936 * of pending reservations
1937 *
1938 * @inode - file containing the cluster
1939 * @lblk - logical block in the pending cluster reservation to be removed
1940 *
1941 * Locking for external use of __remove_pending.
1942 */
1943void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk)
1944{
1945 struct ext4_inode_info *ei = EXT4_I(inode);
1946
1947 write_lock(&ei->i_es_lock);
1948 __remove_pending(inode, lblk);
1949 write_unlock(&ei->i_es_lock);
1950}
1951
1952/*
1953 * ext4_is_pending - determine whether a cluster has a pending reservation
1954 * on it
1955 *
1956 * @inode - file containing the cluster
1957 * @lblk - logical block in the cluster
1958 *
1959 * Returns true if there's a pending reservation for the cluster in the
1960 * set of pending reservations, and false if not.
1961 */
1962bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk)
1963{
1964 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1965 struct ext4_inode_info *ei = EXT4_I(inode);
1966 bool ret;
1967
1968 read_lock(&ei->i_es_lock);
1969 ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL);
1970 read_unlock(&ei->i_es_lock);
1971
1972 return ret;
1973}
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001974
1975/*
1976 * ext4_es_insert_delayed_block - adds a delayed block to the extents status
1977 * tree, adding a pending reservation where
1978 * needed
1979 *
1980 * @inode - file containing the newly added block
1981 * @lblk - logical block to be added
1982 * @allocated - indicates whether a physical cluster has been allocated for
1983 * the logical cluster that contains the block
1984 *
1985 * Returns 0 on success, negative error code on failure.
1986 */
1987int ext4_es_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk,
1988 bool allocated)
1989{
1990 struct extent_status newes;
1991 int err = 0;
1992
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001993 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
1994 return 0;
1995
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001996 es_debug("add [%u/1) delayed to extent status tree of inode %lu\n",
1997 lblk, inode->i_ino);
1998
1999 newes.es_lblk = lblk;
2000 newes.es_len = 1;
2001 ext4_es_store_pblock_status(&newes, ~0, EXTENT_STATUS_DELAYED);
2002 trace_ext4_es_insert_delayed_block(inode, &newes, allocated);
2003
2004 ext4_es_insert_extent_check(inode, &newes);
2005
2006 write_lock(&EXT4_I(inode)->i_es_lock);
2007
Eric Whitney8fcc3a52019-08-22 23:22:14 -04002008 err = __es_remove_extent(inode, lblk, lblk, NULL);
Eric Whitney0b02f4c2018-10-01 14:19:37 -04002009 if (err != 0)
2010 goto error;
2011retry:
2012 err = __es_insert_extent(inode, &newes);
2013 if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
2014 128, EXT4_I(inode)))
2015 goto retry;
2016 if (err != 0)
2017 goto error;
2018
2019 if (allocated)
2020 __insert_pending(inode, lblk);
2021
2022error:
2023 write_unlock(&EXT4_I(inode)->i_es_lock);
2024
2025 ext4_es_print_tree(inode);
2026 ext4_print_pending_tree(inode);
2027
2028 return err;
2029}
Eric Whitneyb6bf9172018-10-01 14:24:08 -04002030
2031/*
2032 * __es_delayed_clu - count number of clusters containing blocks that
2033 * are delayed only
2034 *
2035 * @inode - file containing block range
2036 * @start - logical block defining start of range
2037 * @end - logical block defining end of range
2038 *
2039 * Returns the number of clusters containing only delayed (not delayed
2040 * and unwritten) blocks in the range specified by @start and @end. Any
2041 * cluster or part of a cluster within the range and containing a delayed
2042 * and not unwritten block within the range is counted as a whole cluster.
2043 */
2044static unsigned int __es_delayed_clu(struct inode *inode, ext4_lblk_t start,
2045 ext4_lblk_t end)
2046{
2047 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
2048 struct extent_status *es;
2049 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2050 struct rb_node *node;
2051 ext4_lblk_t first_lclu, last_lclu;
2052 unsigned long long last_counted_lclu;
2053 unsigned int n = 0;
2054
2055 /* guaranteed to be unequal to any ext4_lblk_t value */
2056 last_counted_lclu = ~0ULL;
2057
2058 es = __es_tree_search(&tree->root, start);
2059
2060 while (es && (es->es_lblk <= end)) {
2061 if (ext4_es_is_delonly(es)) {
2062 if (es->es_lblk <= start)
2063 first_lclu = EXT4_B2C(sbi, start);
2064 else
2065 first_lclu = EXT4_B2C(sbi, es->es_lblk);
2066
2067 if (ext4_es_end(es) >= end)
2068 last_lclu = EXT4_B2C(sbi, end);
2069 else
2070 last_lclu = EXT4_B2C(sbi, ext4_es_end(es));
2071
2072 if (first_lclu == last_counted_lclu)
2073 n += last_lclu - first_lclu;
2074 else
2075 n += last_lclu - first_lclu + 1;
2076 last_counted_lclu = last_lclu;
2077 }
2078 node = rb_next(&es->rb_node);
2079 if (!node)
2080 break;
2081 es = rb_entry(node, struct extent_status, rb_node);
2082 }
2083
2084 return n;
2085}
2086
2087/*
2088 * ext4_es_delayed_clu - count number of clusters containing blocks that
2089 * are both delayed and unwritten
2090 *
2091 * @inode - file containing block range
2092 * @lblk - logical block defining start of range
2093 * @len - number of blocks in range
2094 *
2095 * Locking for external use of __es_delayed_clu().
2096 */
2097unsigned int ext4_es_delayed_clu(struct inode *inode, ext4_lblk_t lblk,
2098 ext4_lblk_t len)
2099{
2100 struct ext4_inode_info *ei = EXT4_I(inode);
2101 ext4_lblk_t end;
2102 unsigned int n;
2103
2104 if (len == 0)
2105 return 0;
2106
2107 end = lblk + len - 1;
2108 WARN_ON(end < lblk);
2109
2110 read_lock(&ei->i_es_lock);
2111
2112 n = __es_delayed_clu(inode, lblk, end);
2113
2114 read_unlock(&ei->i_es_lock);
2115
2116 return n;
2117}
2118
2119/*
2120 * __revise_pending - makes, cancels, or leaves unchanged pending cluster
2121 * reservations for a specified block range depending
2122 * upon the presence or absence of delayed blocks
2123 * outside the range within clusters at the ends of the
2124 * range
2125 *
2126 * @inode - file containing the range
2127 * @lblk - logical block defining the start of range
2128 * @len - length of range in blocks
2129 *
2130 * Used after a newly allocated extent is added to the extents status tree.
2131 * Requires that the extents in the range have either written or unwritten
2132 * status. Must be called while holding i_es_lock.
2133 */
2134static void __revise_pending(struct inode *inode, ext4_lblk_t lblk,
2135 ext4_lblk_t len)
2136{
2137 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2138 ext4_lblk_t end = lblk + len - 1;
2139 ext4_lblk_t first, last;
2140 bool f_del = false, l_del = false;
2141
2142 if (len == 0)
2143 return;
2144
2145 /*
2146 * Two cases - block range within single cluster and block range
2147 * spanning two or more clusters. Note that a cluster belonging
2148 * to a range starting and/or ending on a cluster boundary is treated
2149 * as if it does not contain a delayed extent. The new range may
2150 * have allocated space for previously delayed blocks out to the
2151 * cluster boundary, requiring that any pre-existing pending
2152 * reservation be canceled. Because this code only looks at blocks
2153 * outside the range, it should revise pending reservations
2154 * correctly even if the extent represented by the range can't be
2155 * inserted in the extents status tree due to ENOSPC.
2156 */
2157
2158 if (EXT4_B2C(sbi, lblk) == EXT4_B2C(sbi, end)) {
2159 first = EXT4_LBLK_CMASK(sbi, lblk);
2160 if (first != lblk)
2161 f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2162 first, lblk - 1);
2163 if (f_del) {
2164 __insert_pending(inode, first);
2165 } else {
2166 last = EXT4_LBLK_CMASK(sbi, end) +
2167 sbi->s_cluster_ratio - 1;
2168 if (last != end)
2169 l_del = __es_scan_range(inode,
2170 &ext4_es_is_delonly,
2171 end + 1, last);
2172 if (l_del)
2173 __insert_pending(inode, last);
2174 else
2175 __remove_pending(inode, last);
2176 }
2177 } else {
2178 first = EXT4_LBLK_CMASK(sbi, lblk);
2179 if (first != lblk)
2180 f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2181 first, lblk - 1);
2182 if (f_del)
2183 __insert_pending(inode, first);
2184 else
2185 __remove_pending(inode, first);
2186
2187 last = EXT4_LBLK_CMASK(sbi, end) + sbi->s_cluster_ratio - 1;
2188 if (last != end)
2189 l_del = __es_scan_range(inode, &ext4_es_is_delonly,
2190 end + 1, last);
2191 if (l_del)
2192 __insert_pending(inode, last);
2193 else
2194 __remove_pending(inode, last);
2195 }
2196}