1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 */
13 #include <linux/list_sort.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include "ext4.h"
17
18 #include <trace/events/ext4.h>
19
20 /*
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
27 * extent tree, whose goal is only track delayed extents in memory to
28 * simplify the implementation of fiemap and bigalloc, and introduce
29 * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
30 * delay extent tree at the first commit. But for better understand
31 * what it does, it has been rename to extent status tree.
32 *
33 * 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
37 * 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.
42 *
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.
55 */
56
57 /*
58 * Extent status tree implementation for ext4.
59 *
60 *
61 * ==========================================================================
62 * Extent status tree tracks all extent status.
63 *
64 * 1. Why we need to implement extent status tree?
65 *
66 * Without extent status tree, ext4 identifies a delayed extent by looking
67 * up page cache, this has several deficiencies - complicated, buggy,
68 * and inefficient code.
69 *
70 * 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.
72 *
73 * Let us have a look at how they do without extent status tree.
74 * -- 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 *
85 * -- 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
88 * time consuming.
89 *
90 * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
91 * 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
93 * not by searching the extent tree.
94 *
95 *
96 * ==========================================================================
97 * 2. Ext4 extent status tree impelmentation
98 *
99 * -- 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.
105 *
106 * -- 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.
110 *
111 * -- 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.
114 *
115 * -- 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.
122 *
123 *
124 * ==========================================================================
125 * 3. Performance analysis
126 *
127 * -- 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
138 *
139 * -- Refactor delayed space reservation
140 *
141 * -- Extent-level locking
142 */
143
144 static struct kmem_cache *ext4_es_cachep;
145 static struct kmem_cache *ext4_pending_cachep;
146
147 static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
148 struct extent_status *prealloc);
149 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
150 ext4_lblk_t end, int *reserved,
151 struct extent_status *prealloc);
152 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
153 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
154 struct ext4_inode_info *locked_ei);
155 static int __revise_pending(struct inode *inode, ext4_lblk_t lblk,
156 ext4_lblk_t len,
157 struct pending_reservation **prealloc);
158
ext4_init_es(void)159 int __init ext4_init_es(void)
160 {
161 ext4_es_cachep = kmem_cache_create("ext4_extent_status",
162 sizeof(struct extent_status),
163 0, (SLAB_RECLAIM_ACCOUNT), NULL);
164 if (ext4_es_cachep == NULL)
165 return -ENOMEM;
166 return 0;
167 }
168
ext4_exit_es(void)169 void ext4_exit_es(void)
170 {
171 kmem_cache_destroy(ext4_es_cachep);
172 }
173
ext4_es_init_tree(struct ext4_es_tree * tree)174 void ext4_es_init_tree(struct ext4_es_tree *tree)
175 {
176 tree->root = RB_ROOT;
177 tree->cache_es = NULL;
178 }
179
180 #ifdef ES_DEBUG__
ext4_es_print_tree(struct inode * inode)181 static void ext4_es_print_tree(struct inode *inode)
182 {
183 struct ext4_es_tree *tree;
184 struct rb_node *node;
185
186 printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
187 tree = &EXT4_I(inode)->i_es_tree;
188 node = rb_first(&tree->root);
189 while (node) {
190 struct extent_status *es;
191 es = rb_entry(node, struct extent_status, rb_node);
192 printk(KERN_DEBUG " [%u/%u) %llu %x",
193 es->es_lblk, es->es_len,
194 ext4_es_pblock(es), ext4_es_status(es));
195 node = rb_next(node);
196 }
197 printk(KERN_DEBUG "\n");
198 }
199 #else
200 #define ext4_es_print_tree(inode)
201 #endif
202
ext4_es_end(struct extent_status * es)203 static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
204 {
205 BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
206 return es->es_lblk + es->es_len - 1;
207 }
208
209 /*
210 * search through the tree for an delayed extent with a given offset. If
211 * it can't be found, try to find next extent.
212 */
__es_tree_search(struct rb_root * root,ext4_lblk_t lblk)213 static struct extent_status *__es_tree_search(struct rb_root *root,
214 ext4_lblk_t lblk)
215 {
216 struct rb_node *node = root->rb_node;
217 struct extent_status *es = NULL;
218
219 while (node) {
220 es = rb_entry(node, struct extent_status, rb_node);
221 if (lblk < es->es_lblk)
222 node = node->rb_left;
223 else if (lblk > ext4_es_end(es))
224 node = node->rb_right;
225 else
226 return es;
227 }
228
229 if (es && lblk < es->es_lblk)
230 return es;
231
232 if (es && lblk > ext4_es_end(es)) {
233 node = rb_next(&es->rb_node);
234 return node ? rb_entry(node, struct extent_status, rb_node) :
235 NULL;
236 }
237
238 return NULL;
239 }
240
241 /*
242 * ext4_es_find_extent_range - find extent with specified status within block
243 * range or next extent following block range in
244 * extents status tree
245 *
246 * @inode - file containing the range
247 * @matching_fn - pointer to function that matches extents with desired status
248 * @lblk - logical block defining start of range
249 * @end - logical block defining end of range
250 * @es - extent found, if any
251 *
252 * Find the first extent within the block range specified by @lblk and @end
253 * in the extents status tree that satisfies @matching_fn. If a match
254 * is found, it's returned in @es. If not, and a matching extent is found
255 * beyond the block range, it's returned in @es. If no match is found, an
256 * extent is returned in @es whose es_lblk, es_len, and es_pblk components
257 * are 0.
258 */
__es_find_extent_range(struct inode * inode,int (* matching_fn)(struct extent_status * es),ext4_lblk_t lblk,ext4_lblk_t end,struct extent_status * es)259 static void __es_find_extent_range(struct inode *inode,
260 int (*matching_fn)(struct extent_status *es),
261 ext4_lblk_t lblk, ext4_lblk_t end,
262 struct extent_status *es)
263 {
264 struct ext4_es_tree *tree = NULL;
265 struct extent_status *es1 = NULL;
266 struct rb_node *node;
267
268 WARN_ON(es == NULL);
269 WARN_ON(end < lblk);
270
271 tree = &EXT4_I(inode)->i_es_tree;
272
273 /* see if the extent has been cached */
274 es->es_lblk = es->es_len = es->es_pblk = 0;
275 es1 = READ_ONCE(tree->cache_es);
276 if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
277 es_debug("%u cached by [%u/%u) %llu %x\n",
278 lblk, es1->es_lblk, es1->es_len,
279 ext4_es_pblock(es1), ext4_es_status(es1));
280 goto out;
281 }
282
283 es1 = __es_tree_search(&tree->root, lblk);
284
285 out:
286 if (es1 && !matching_fn(es1)) {
287 while ((node = rb_next(&es1->rb_node)) != NULL) {
288 es1 = rb_entry(node, struct extent_status, rb_node);
289 if (es1->es_lblk > end) {
290 es1 = NULL;
291 break;
292 }
293 if (matching_fn(es1))
294 break;
295 }
296 }
297
298 if (es1 && matching_fn(es1)) {
299 WRITE_ONCE(tree->cache_es, es1);
300 es->es_lblk = es1->es_lblk;
301 es->es_len = es1->es_len;
302 es->es_pblk = es1->es_pblk;
303 }
304
305 }
306
307 /*
308 * Locking for __es_find_extent_range() for external use
309 */
ext4_es_find_extent_range(struct inode * inode,int (* matching_fn)(struct extent_status * es),ext4_lblk_t lblk,ext4_lblk_t end,struct extent_status * es)310 void ext4_es_find_extent_range(struct inode *inode,
311 int (*matching_fn)(struct extent_status *es),
312 ext4_lblk_t lblk, ext4_lblk_t end,
313 struct extent_status *es)
314 {
315 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
316 return;
317
318 trace_ext4_es_find_extent_range_enter(inode, lblk);
319
320 read_lock(&EXT4_I(inode)->i_es_lock);
321 __es_find_extent_range(inode, matching_fn, lblk, end, es);
322 read_unlock(&EXT4_I(inode)->i_es_lock);
323
324 trace_ext4_es_find_extent_range_exit(inode, es);
325 }
326
327 /*
328 * __es_scan_range - search block range for block with specified status
329 * in extents status tree
330 *
331 * @inode - file containing the range
332 * @matching_fn - pointer to function that matches extents with desired status
333 * @lblk - logical block defining start of range
334 * @end - logical block defining end of range
335 *
336 * Returns true if at least one block in the specified block range satisfies
337 * the criterion specified by @matching_fn, and false if not. If at least
338 * one extent has the specified status, then there is at least one block
339 * in the cluster with that status. Should only be called by code that has
340 * taken i_es_lock.
341 */
__es_scan_range(struct inode * inode,int (* matching_fn)(struct extent_status * es),ext4_lblk_t start,ext4_lblk_t end)342 static bool __es_scan_range(struct inode *inode,
343 int (*matching_fn)(struct extent_status *es),
344 ext4_lblk_t start, ext4_lblk_t end)
345 {
346 struct extent_status es;
347
348 __es_find_extent_range(inode, matching_fn, start, end, &es);
349 if (es.es_len == 0)
350 return false; /* no matching extent in the tree */
351 else if (es.es_lblk <= start &&
352 start < es.es_lblk + es.es_len)
353 return true;
354 else if (start <= es.es_lblk && es.es_lblk <= end)
355 return true;
356 else
357 return false;
358 }
359 /*
360 * Locking for __es_scan_range() for external use
361 */
ext4_es_scan_range(struct inode * inode,int (* matching_fn)(struct extent_status * es),ext4_lblk_t lblk,ext4_lblk_t end)362 bool ext4_es_scan_range(struct inode *inode,
363 int (*matching_fn)(struct extent_status *es),
364 ext4_lblk_t lblk, ext4_lblk_t end)
365 {
366 bool ret;
367
368 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
369 return false;
370
371 read_lock(&EXT4_I(inode)->i_es_lock);
372 ret = __es_scan_range(inode, matching_fn, lblk, end);
373 read_unlock(&EXT4_I(inode)->i_es_lock);
374
375 return ret;
376 }
377
378 /*
379 * __es_scan_clu - search cluster for block with specified status in
380 * extents status tree
381 *
382 * @inode - file containing the cluster
383 * @matching_fn - pointer to function that matches extents with desired status
384 * @lblk - logical block in cluster to be searched
385 *
386 * Returns true if at least one extent in the cluster containing @lblk
387 * satisfies the criterion specified by @matching_fn, and false if not. If at
388 * least one extent has the specified status, then there is at least one block
389 * in the cluster with that status. Should only be called by code that has
390 * taken i_es_lock.
391 */
__es_scan_clu(struct inode * inode,int (* matching_fn)(struct extent_status * es),ext4_lblk_t lblk)392 static bool __es_scan_clu(struct inode *inode,
393 int (*matching_fn)(struct extent_status *es),
394 ext4_lblk_t lblk)
395 {
396 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
397 ext4_lblk_t lblk_start, lblk_end;
398
399 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
400 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
401
402 return __es_scan_range(inode, matching_fn, lblk_start, lblk_end);
403 }
404
405 /*
406 * Locking for __es_scan_clu() for external use
407 */
ext4_es_scan_clu(struct inode * inode,int (* matching_fn)(struct extent_status * es),ext4_lblk_t lblk)408 bool ext4_es_scan_clu(struct inode *inode,
409 int (*matching_fn)(struct extent_status *es),
410 ext4_lblk_t lblk)
411 {
412 bool ret;
413
414 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
415 return false;
416
417 read_lock(&EXT4_I(inode)->i_es_lock);
418 ret = __es_scan_clu(inode, matching_fn, lblk);
419 read_unlock(&EXT4_I(inode)->i_es_lock);
420
421 return ret;
422 }
423
ext4_es_list_add(struct inode * inode)424 static void ext4_es_list_add(struct inode *inode)
425 {
426 struct ext4_inode_info *ei = EXT4_I(inode);
427 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
428
429 if (!list_empty(&ei->i_es_list))
430 return;
431
432 spin_lock(&sbi->s_es_lock);
433 if (list_empty(&ei->i_es_list)) {
434 list_add_tail(&ei->i_es_list, &sbi->s_es_list);
435 sbi->s_es_nr_inode++;
436 }
437 spin_unlock(&sbi->s_es_lock);
438 }
439
ext4_es_list_del(struct inode * inode)440 static void ext4_es_list_del(struct inode *inode)
441 {
442 struct ext4_inode_info *ei = EXT4_I(inode);
443 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
444
445 spin_lock(&sbi->s_es_lock);
446 if (!list_empty(&ei->i_es_list)) {
447 list_del_init(&ei->i_es_list);
448 sbi->s_es_nr_inode--;
449 WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
450 }
451 spin_unlock(&sbi->s_es_lock);
452 }
453
__alloc_pending(bool nofail)454 static inline struct pending_reservation *__alloc_pending(bool nofail)
455 {
456 if (!nofail)
457 return kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC);
458
459 return kmem_cache_zalloc(ext4_pending_cachep, GFP_KERNEL | __GFP_NOFAIL);
460 }
461
__free_pending(struct pending_reservation * pr)462 static inline void __free_pending(struct pending_reservation *pr)
463 {
464 kmem_cache_free(ext4_pending_cachep, pr);
465 }
466
467 /*
468 * Returns true if we cannot fail to allocate memory for this extent_status
469 * entry and cannot reclaim it until its status changes.
470 */
ext4_es_must_keep(struct extent_status * es)471 static inline bool ext4_es_must_keep(struct extent_status *es)
472 {
473 /* fiemap, bigalloc, and seek_data/hole need to use it. */
474 if (ext4_es_is_delayed(es))
475 return true;
476
477 return false;
478 }
479
__es_alloc_extent(bool nofail)480 static inline struct extent_status *__es_alloc_extent(bool nofail)
481 {
482 if (!nofail)
483 return kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
484
485 return kmem_cache_zalloc(ext4_es_cachep, GFP_KERNEL | __GFP_NOFAIL);
486 }
487
ext4_es_init_extent(struct inode * inode,struct extent_status * es,ext4_lblk_t lblk,ext4_lblk_t len,ext4_fsblk_t pblk)488 static void ext4_es_init_extent(struct inode *inode, struct extent_status *es,
489 ext4_lblk_t lblk, ext4_lblk_t len, ext4_fsblk_t pblk)
490 {
491 es->es_lblk = lblk;
492 es->es_len = len;
493 es->es_pblk = pblk;
494
495 /* We never try to reclaim a must kept extent, so we don't count it. */
496 if (!ext4_es_must_keep(es)) {
497 if (!EXT4_I(inode)->i_es_shk_nr++)
498 ext4_es_list_add(inode);
499 percpu_counter_inc(&EXT4_SB(inode->i_sb)->
500 s_es_stats.es_stats_shk_cnt);
501 }
502
503 EXT4_I(inode)->i_es_all_nr++;
504 percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
505 }
506
__es_free_extent(struct extent_status * es)507 static inline void __es_free_extent(struct extent_status *es)
508 {
509 kmem_cache_free(ext4_es_cachep, es);
510 }
511
ext4_es_free_extent(struct inode * inode,struct extent_status * es)512 static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
513 {
514 EXT4_I(inode)->i_es_all_nr--;
515 percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
516
517 /* Decrease the shrink counter when we can reclaim the extent. */
518 if (!ext4_es_must_keep(es)) {
519 BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
520 if (!--EXT4_I(inode)->i_es_shk_nr)
521 ext4_es_list_del(inode);
522 percpu_counter_dec(&EXT4_SB(inode->i_sb)->
523 s_es_stats.es_stats_shk_cnt);
524 }
525
526 __es_free_extent(es);
527 }
528
529 /*
530 * Check whether or not two extents can be merged
531 * Condition:
532 * - logical block number is contiguous
533 * - physical block number is contiguous
534 * - status is equal
535 */
ext4_es_can_be_merged(struct extent_status * es1,struct extent_status * es2)536 static int ext4_es_can_be_merged(struct extent_status *es1,
537 struct extent_status *es2)
538 {
539 if (ext4_es_type(es1) != ext4_es_type(es2))
540 return 0;
541
542 if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
543 pr_warn("ES assertion failed when merging extents. "
544 "The sum of lengths of es1 (%d) and es2 (%d) "
545 "is bigger than allowed file size (%d)\n",
546 es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
547 WARN_ON(1);
548 return 0;
549 }
550
551 if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
552 return 0;
553
554 if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
555 (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
556 return 1;
557
558 if (ext4_es_is_hole(es1))
559 return 1;
560
561 /* we need to check delayed extent is without unwritten status */
562 if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
563 return 1;
564
565 return 0;
566 }
567
568 static struct extent_status *
ext4_es_try_to_merge_left(struct inode * inode,struct extent_status * es)569 ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
570 {
571 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
572 struct extent_status *es1;
573 struct rb_node *node;
574
575 node = rb_prev(&es->rb_node);
576 if (!node)
577 return es;
578
579 es1 = rb_entry(node, struct extent_status, rb_node);
580 if (ext4_es_can_be_merged(es1, es)) {
581 es1->es_len += es->es_len;
582 if (ext4_es_is_referenced(es))
583 ext4_es_set_referenced(es1);
584 rb_erase(&es->rb_node, &tree->root);
585 ext4_es_free_extent(inode, es);
586 es = es1;
587 }
588
589 return es;
590 }
591
592 static struct extent_status *
ext4_es_try_to_merge_right(struct inode * inode,struct extent_status * es)593 ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
594 {
595 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
596 struct extent_status *es1;
597 struct rb_node *node;
598
599 node = rb_next(&es->rb_node);
600 if (!node)
601 return es;
602
603 es1 = rb_entry(node, struct extent_status, rb_node);
604 if (ext4_es_can_be_merged(es, es1)) {
605 es->es_len += es1->es_len;
606 if (ext4_es_is_referenced(es1))
607 ext4_es_set_referenced(es);
608 rb_erase(node, &tree->root);
609 ext4_es_free_extent(inode, es1);
610 }
611
612 return es;
613 }
614
615 #ifdef ES_AGGRESSIVE_TEST
616 #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
617
ext4_es_insert_extent_ext_check(struct inode * inode,struct extent_status * es)618 static void ext4_es_insert_extent_ext_check(struct inode *inode,
619 struct extent_status *es)
620 {
621 struct ext4_ext_path *path = NULL;
622 struct ext4_extent *ex;
623 ext4_lblk_t ee_block;
624 ext4_fsblk_t ee_start;
625 unsigned short ee_len;
626 int depth, ee_status, es_status;
627
628 path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
629 if (IS_ERR(path))
630 return;
631
632 depth = ext_depth(inode);
633 ex = path[depth].p_ext;
634
635 if (ex) {
636
637 ee_block = le32_to_cpu(ex->ee_block);
638 ee_start = ext4_ext_pblock(ex);
639 ee_len = ext4_ext_get_actual_len(ex);
640
641 ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
642 es_status = ext4_es_is_unwritten(es) ? 1 : 0;
643
644 /*
645 * Make sure ex and es are not overlap when we try to insert
646 * a delayed/hole extent.
647 */
648 if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
649 if (in_range(es->es_lblk, ee_block, ee_len)) {
650 pr_warn("ES insert assertion failed for "
651 "inode: %lu we can find an extent "
652 "at block [%d/%d/%llu/%c], but we "
653 "want to add a delayed/hole extent "
654 "[%d/%d/%llu/%x]\n",
655 inode->i_ino, ee_block, ee_len,
656 ee_start, ee_status ? 'u' : 'w',
657 es->es_lblk, es->es_len,
658 ext4_es_pblock(es), ext4_es_status(es));
659 }
660 goto out;
661 }
662
663 /*
664 * We don't check ee_block == es->es_lblk, etc. because es
665 * might be a part of whole extent, vice versa.
666 */
667 if (es->es_lblk < ee_block ||
668 ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
669 pr_warn("ES insert assertion failed for inode: %lu "
670 "ex_status [%d/%d/%llu/%c] != "
671 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
672 ee_block, ee_len, ee_start,
673 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
674 ext4_es_pblock(es), es_status ? 'u' : 'w');
675 goto out;
676 }
677
678 if (ee_status ^ es_status) {
679 pr_warn("ES insert assertion failed for inode: %lu "
680 "ex_status [%d/%d/%llu/%c] != "
681 "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
682 ee_block, ee_len, ee_start,
683 ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
684 ext4_es_pblock(es), es_status ? 'u' : 'w');
685 }
686 } else {
687 /*
688 * We can't find an extent on disk. So we need to make sure
689 * that we don't want to add an written/unwritten extent.
690 */
691 if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
692 pr_warn("ES insert assertion failed for inode: %lu "
693 "can't find an extent at block %d but we want "
694 "to add a written/unwritten extent "
695 "[%d/%d/%llu/%x]\n", inode->i_ino,
696 es->es_lblk, es->es_lblk, es->es_len,
697 ext4_es_pblock(es), ext4_es_status(es));
698 }
699 }
700 out:
701 ext4_ext_drop_refs(path);
702 kfree(path);
703 }
704
ext4_es_insert_extent_ind_check(struct inode * inode,struct extent_status * es)705 static void ext4_es_insert_extent_ind_check(struct inode *inode,
706 struct extent_status *es)
707 {
708 struct ext4_map_blocks map;
709 int retval;
710
711 /*
712 * Here we call ext4_ind_map_blocks to lookup a block mapping because
713 * 'Indirect' structure is defined in indirect.c. So we couldn't
714 * access direct/indirect tree from outside. It is too dirty to define
715 * this function in indirect.c file.
716 */
717
718 map.m_lblk = es->es_lblk;
719 map.m_len = es->es_len;
720
721 retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
722 if (retval > 0) {
723 if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
724 /*
725 * We want to add a delayed/hole extent but this
726 * block has been allocated.
727 */
728 pr_warn("ES insert assertion failed for inode: %lu "
729 "We can find blocks but we want to add a "
730 "delayed/hole extent [%d/%d/%llu/%x]\n",
731 inode->i_ino, es->es_lblk, es->es_len,
732 ext4_es_pblock(es), ext4_es_status(es));
733 return;
734 } else if (ext4_es_is_written(es)) {
735 if (retval != es->es_len) {
736 pr_warn("ES insert assertion failed for "
737 "inode: %lu retval %d != es_len %d\n",
738 inode->i_ino, retval, es->es_len);
739 return;
740 }
741 if (map.m_pblk != ext4_es_pblock(es)) {
742 pr_warn("ES insert assertion failed for "
743 "inode: %lu m_pblk %llu != "
744 "es_pblk %llu\n",
745 inode->i_ino, map.m_pblk,
746 ext4_es_pblock(es));
747 return;
748 }
749 } else {
750 /*
751 * We don't need to check unwritten extent because
752 * indirect-based file doesn't have it.
753 */
754 BUG();
755 }
756 } else if (retval == 0) {
757 if (ext4_es_is_written(es)) {
758 pr_warn("ES insert assertion failed for inode: %lu "
759 "We can't find the block but we want to add "
760 "a written extent [%d/%d/%llu/%x]\n",
761 inode->i_ino, es->es_lblk, es->es_len,
762 ext4_es_pblock(es), ext4_es_status(es));
763 return;
764 }
765 }
766 }
767
ext4_es_insert_extent_check(struct inode * inode,struct extent_status * es)768 static inline void ext4_es_insert_extent_check(struct inode *inode,
769 struct extent_status *es)
770 {
771 /*
772 * We don't need to worry about the race condition because
773 * caller takes i_data_sem locking.
774 */
775 BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
776 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
777 ext4_es_insert_extent_ext_check(inode, es);
778 else
779 ext4_es_insert_extent_ind_check(inode, es);
780 }
781 #else
ext4_es_insert_extent_check(struct inode * inode,struct extent_status * es)782 static inline void ext4_es_insert_extent_check(struct inode *inode,
783 struct extent_status *es)
784 {
785 }
786 #endif
787
__es_insert_extent(struct inode * inode,struct extent_status * newes,struct extent_status * prealloc)788 static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
789 struct extent_status *prealloc)
790 {
791 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
792 struct rb_node **p = &tree->root.rb_node;
793 struct rb_node *parent = NULL;
794 struct extent_status *es;
795
796 while (*p) {
797 parent = *p;
798 es = rb_entry(parent, struct extent_status, rb_node);
799
800 if (newes->es_lblk < es->es_lblk) {
801 if (ext4_es_can_be_merged(newes, es)) {
802 /*
803 * Here we can modify es_lblk directly
804 * because it isn't overlapped.
805 */
806 es->es_lblk = newes->es_lblk;
807 es->es_len += newes->es_len;
808 if (ext4_es_is_written(es) ||
809 ext4_es_is_unwritten(es))
810 ext4_es_store_pblock(es,
811 newes->es_pblk);
812 es = ext4_es_try_to_merge_left(inode, es);
813 goto out;
814 }
815 p = &(*p)->rb_left;
816 } else if (newes->es_lblk > ext4_es_end(es)) {
817 if (ext4_es_can_be_merged(es, newes)) {
818 es->es_len += newes->es_len;
819 es = ext4_es_try_to_merge_right(inode, es);
820 goto out;
821 }
822 p = &(*p)->rb_right;
823 } else {
824 BUG();
825 return -EINVAL;
826 }
827 }
828
829 if (prealloc)
830 es = prealloc;
831 else
832 es = __es_alloc_extent(false);
833 if (!es)
834 return -ENOMEM;
835 ext4_es_init_extent(inode, es, newes->es_lblk, newes->es_len,
836 newes->es_pblk);
837
838 rb_link_node(&es->rb_node, parent, p);
839 rb_insert_color(&es->rb_node, &tree->root);
840
841 out:
842 tree->cache_es = es;
843 return 0;
844 }
845
846 /*
847 * ext4_es_insert_extent() adds information to an inode's extent
848 * status tree.
849 */
ext4_es_insert_extent(struct inode * inode,ext4_lblk_t lblk,ext4_lblk_t len,ext4_fsblk_t pblk,unsigned int status)850 void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
851 ext4_lblk_t len, ext4_fsblk_t pblk,
852 unsigned int status)
853 {
854 struct extent_status newes;
855 ext4_lblk_t end = lblk + len - 1;
856 int err1 = 0, err2 = 0, err3 = 0;
857 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
858 struct extent_status *es1 = NULL;
859 struct extent_status *es2 = NULL;
860 struct pending_reservation *pr = NULL;
861 bool revise_pending = false;
862
863 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
864 return;
865
866 es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
867 lblk, len, pblk, status, inode->i_ino);
868
869 if (!len)
870 return;
871
872 BUG_ON(end < lblk);
873
874 if ((status & EXTENT_STATUS_DELAYED) &&
875 (status & EXTENT_STATUS_WRITTEN)) {
876 ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
877 " delayed and written which can potentially "
878 " cause data loss.", lblk, len);
879 WARN_ON(1);
880 }
881
882 newes.es_lblk = lblk;
883 newes.es_len = len;
884 ext4_es_store_pblock_status(&newes, pblk, status);
885 trace_ext4_es_insert_extent(inode, &newes);
886
887 ext4_es_insert_extent_check(inode, &newes);
888
889 revise_pending = sbi->s_cluster_ratio > 1 &&
890 test_opt(inode->i_sb, DELALLOC) &&
891 (status & (EXTENT_STATUS_WRITTEN |
892 EXTENT_STATUS_UNWRITTEN));
893 retry:
894 if (err1 && !es1)
895 es1 = __es_alloc_extent(true);
896 if ((err1 || err2) && !es2)
897 es2 = __es_alloc_extent(true);
898 if ((err1 || err2 || err3) && revise_pending && !pr)
899 pr = __alloc_pending(true);
900 write_lock(&EXT4_I(inode)->i_es_lock);
901
902 err1 = __es_remove_extent(inode, lblk, end, NULL, es1);
903 if (err1 != 0)
904 goto error;
905 /* Free preallocated extent if it didn't get used. */
906 if (es1) {
907 if (!es1->es_len)
908 __es_free_extent(es1);
909 es1 = NULL;
910 }
911
912 err2 = __es_insert_extent(inode, &newes, es2);
913 if (err2 == -ENOMEM && !ext4_es_must_keep(&newes))
914 err2 = 0;
915 if (err2 != 0)
916 goto error;
917 /* Free preallocated extent if it didn't get used. */
918 if (es2) {
919 if (!es2->es_len)
920 __es_free_extent(es2);
921 es2 = NULL;
922 }
923
924 if (revise_pending) {
925 err3 = __revise_pending(inode, lblk, len, &pr);
926 if (err3 != 0)
927 goto error;
928 if (pr) {
929 __free_pending(pr);
930 pr = NULL;
931 }
932 }
933 error:
934 write_unlock(&EXT4_I(inode)->i_es_lock);
935 if (err1 || err2 || err3)
936 goto retry;
937
938 ext4_es_print_tree(inode);
939 return;
940 }
941
942 /*
943 * ext4_es_cache_extent() inserts information into the extent status
944 * tree if and only if there isn't information about the range in
945 * question already.
946 */
ext4_es_cache_extent(struct inode * inode,ext4_lblk_t lblk,ext4_lblk_t len,ext4_fsblk_t pblk,unsigned int status)947 void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
948 ext4_lblk_t len, ext4_fsblk_t pblk,
949 unsigned int status)
950 {
951 struct extent_status *es;
952 struct extent_status newes;
953 ext4_lblk_t end = lblk + len - 1;
954
955 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
956 return;
957
958 newes.es_lblk = lblk;
959 newes.es_len = len;
960 ext4_es_store_pblock_status(&newes, pblk, status);
961 trace_ext4_es_cache_extent(inode, &newes);
962
963 if (!len)
964 return;
965
966 BUG_ON(end < lblk);
967
968 write_lock(&EXT4_I(inode)->i_es_lock);
969
970 es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
971 if (!es || es->es_lblk > end)
972 __es_insert_extent(inode, &newes, NULL);
973 write_unlock(&EXT4_I(inode)->i_es_lock);
974 }
975
976 /*
977 * ext4_es_lookup_extent() looks up an extent in extent status tree.
978 *
979 * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
980 *
981 * Return: 1 on found, 0 on not
982 */
ext4_es_lookup_extent(struct inode * inode,ext4_lblk_t lblk,ext4_lblk_t * next_lblk,struct extent_status * es)983 int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
984 ext4_lblk_t *next_lblk,
985 struct extent_status *es)
986 {
987 struct ext4_es_tree *tree;
988 struct ext4_es_stats *stats;
989 struct extent_status *es1 = NULL;
990 struct rb_node *node;
991 int found = 0;
992
993 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
994 return 0;
995
996 trace_ext4_es_lookup_extent_enter(inode, lblk);
997 es_debug("lookup extent in block %u\n", lblk);
998
999 tree = &EXT4_I(inode)->i_es_tree;
1000 read_lock(&EXT4_I(inode)->i_es_lock);
1001
1002 /* find extent in cache firstly */
1003 es->es_lblk = es->es_len = es->es_pblk = 0;
1004 es1 = READ_ONCE(tree->cache_es);
1005 if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
1006 es_debug("%u cached by [%u/%u)\n",
1007 lblk, es1->es_lblk, es1->es_len);
1008 found = 1;
1009 goto out;
1010 }
1011
1012 node = tree->root.rb_node;
1013 while (node) {
1014 es1 = rb_entry(node, struct extent_status, rb_node);
1015 if (lblk < es1->es_lblk)
1016 node = node->rb_left;
1017 else if (lblk > ext4_es_end(es1))
1018 node = node->rb_right;
1019 else {
1020 found = 1;
1021 break;
1022 }
1023 }
1024
1025 out:
1026 stats = &EXT4_SB(inode->i_sb)->s_es_stats;
1027 if (found) {
1028 BUG_ON(!es1);
1029 es->es_lblk = es1->es_lblk;
1030 es->es_len = es1->es_len;
1031 es->es_pblk = es1->es_pblk;
1032 if (!ext4_es_is_referenced(es1))
1033 ext4_es_set_referenced(es1);
1034 percpu_counter_inc(&stats->es_stats_cache_hits);
1035 if (next_lblk) {
1036 node = rb_next(&es1->rb_node);
1037 if (node) {
1038 es1 = rb_entry(node, struct extent_status,
1039 rb_node);
1040 *next_lblk = es1->es_lblk;
1041 } else
1042 *next_lblk = 0;
1043 }
1044 } else {
1045 percpu_counter_inc(&stats->es_stats_cache_misses);
1046 }
1047
1048 read_unlock(&EXT4_I(inode)->i_es_lock);
1049
1050 trace_ext4_es_lookup_extent_exit(inode, es, found);
1051 return found;
1052 }
1053
1054 struct rsvd_count {
1055 int ndelonly;
1056 bool first_do_lblk_found;
1057 ext4_lblk_t first_do_lblk;
1058 ext4_lblk_t last_do_lblk;
1059 struct extent_status *left_es;
1060 bool partial;
1061 ext4_lblk_t lclu;
1062 };
1063
1064 /*
1065 * init_rsvd - initialize reserved count data before removing block range
1066 * in file from extent status tree
1067 *
1068 * @inode - file containing range
1069 * @lblk - first block in range
1070 * @es - pointer to first extent in range
1071 * @rc - pointer to reserved count data
1072 *
1073 * Assumes es is not NULL
1074 */
init_rsvd(struct inode * inode,ext4_lblk_t lblk,struct extent_status * es,struct rsvd_count * rc)1075 static void init_rsvd(struct inode *inode, ext4_lblk_t lblk,
1076 struct extent_status *es, struct rsvd_count *rc)
1077 {
1078 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1079 struct rb_node *node;
1080
1081 rc->ndelonly = 0;
1082
1083 /*
1084 * for bigalloc, note the first delonly block in the range has not
1085 * been found, record the extent containing the block to the left of
1086 * the region to be removed, if any, and note that there's no partial
1087 * cluster to track
1088 */
1089 if (sbi->s_cluster_ratio > 1) {
1090 rc->first_do_lblk_found = false;
1091 if (lblk > es->es_lblk) {
1092 rc->left_es = es;
1093 } else {
1094 node = rb_prev(&es->rb_node);
1095 rc->left_es = node ? rb_entry(node,
1096 struct extent_status,
1097 rb_node) : NULL;
1098 }
1099 rc->partial = false;
1100 }
1101 }
1102
1103 /*
1104 * count_rsvd - count the clusters containing delayed and not unwritten
1105 * (delonly) blocks in a range within an extent and add to
1106 * the running tally in rsvd_count
1107 *
1108 * @inode - file containing extent
1109 * @lblk - first block in range
1110 * @len - length of range in blocks
1111 * @es - pointer to extent containing clusters to be counted
1112 * @rc - pointer to reserved count data
1113 *
1114 * Tracks partial clusters found at the beginning and end of extents so
1115 * they aren't overcounted when they span adjacent extents
1116 */
count_rsvd(struct inode * inode,ext4_lblk_t lblk,long len,struct extent_status * es,struct rsvd_count * rc)1117 static void count_rsvd(struct inode *inode, ext4_lblk_t lblk, long len,
1118 struct extent_status *es, struct rsvd_count *rc)
1119 {
1120 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1121 ext4_lblk_t i, end, nclu;
1122
1123 if (!ext4_es_is_delonly(es))
1124 return;
1125
1126 WARN_ON(len <= 0);
1127
1128 if (sbi->s_cluster_ratio == 1) {
1129 rc->ndelonly += (int) len;
1130 return;
1131 }
1132
1133 /* bigalloc */
1134
1135 i = (lblk < es->es_lblk) ? es->es_lblk : lblk;
1136 end = lblk + (ext4_lblk_t) len - 1;
1137 end = (end > ext4_es_end(es)) ? ext4_es_end(es) : end;
1138
1139 /* record the first block of the first delonly extent seen */
1140 if (!rc->first_do_lblk_found) {
1141 rc->first_do_lblk = i;
1142 rc->first_do_lblk_found = true;
1143 }
1144
1145 /* update the last lblk in the region seen so far */
1146 rc->last_do_lblk = end;
1147
1148 /*
1149 * if we're tracking a partial cluster and the current extent
1150 * doesn't start with it, count it and stop tracking
1151 */
1152 if (rc->partial && (rc->lclu != EXT4_B2C(sbi, i))) {
1153 rc->ndelonly++;
1154 rc->partial = false;
1155 }
1156
1157 /*
1158 * if the first cluster doesn't start on a cluster boundary but
1159 * ends on one, count it
1160 */
1161 if (EXT4_LBLK_COFF(sbi, i) != 0) {
1162 if (end >= EXT4_LBLK_CFILL(sbi, i)) {
1163 rc->ndelonly++;
1164 rc->partial = false;
1165 i = EXT4_LBLK_CFILL(sbi, i) + 1;
1166 }
1167 }
1168
1169 /*
1170 * if the current cluster starts on a cluster boundary, count the
1171 * number of whole delonly clusters in the extent
1172 */
1173 if ((i + sbi->s_cluster_ratio - 1) <= end) {
1174 nclu = (end - i + 1) >> sbi->s_cluster_bits;
1175 rc->ndelonly += nclu;
1176 i += nclu << sbi->s_cluster_bits;
1177 }
1178
1179 /*
1180 * start tracking a partial cluster if there's a partial at the end
1181 * of the current extent and we're not already tracking one
1182 */
1183 if (!rc->partial && i <= end) {
1184 rc->partial = true;
1185 rc->lclu = EXT4_B2C(sbi, i);
1186 }
1187 }
1188
1189 /*
1190 * __pr_tree_search - search for a pending cluster reservation
1191 *
1192 * @root - root of pending reservation tree
1193 * @lclu - logical cluster to search for
1194 *
1195 * Returns the pending reservation for the cluster identified by @lclu
1196 * if found. If not, returns a reservation for the next cluster if any,
1197 * and if not, returns NULL.
1198 */
__pr_tree_search(struct rb_root * root,ext4_lblk_t lclu)1199 static struct pending_reservation *__pr_tree_search(struct rb_root *root,
1200 ext4_lblk_t lclu)
1201 {
1202 struct rb_node *node = root->rb_node;
1203 struct pending_reservation *pr = NULL;
1204
1205 while (node) {
1206 pr = rb_entry(node, struct pending_reservation, rb_node);
1207 if (lclu < pr->lclu)
1208 node = node->rb_left;
1209 else if (lclu > pr->lclu)
1210 node = node->rb_right;
1211 else
1212 return pr;
1213 }
1214 if (pr && lclu < pr->lclu)
1215 return pr;
1216 if (pr && lclu > pr->lclu) {
1217 node = rb_next(&pr->rb_node);
1218 return node ? rb_entry(node, struct pending_reservation,
1219 rb_node) : NULL;
1220 }
1221 return NULL;
1222 }
1223
1224 /*
1225 * get_rsvd - calculates and returns the number of cluster reservations to be
1226 * released when removing a block range from the extent status tree
1227 * and releases any pending reservations within the range
1228 *
1229 * @inode - file containing block range
1230 * @end - last block in range
1231 * @right_es - pointer to extent containing next block beyond end or NULL
1232 * @rc - pointer to reserved count data
1233 *
1234 * The number of reservations to be released is equal to the number of
1235 * clusters containing delayed and not unwritten (delonly) blocks within
1236 * the range, minus the number of clusters still containing delonly blocks
1237 * at the ends of the range, and minus the number of pending reservations
1238 * within the range.
1239 */
get_rsvd(struct inode * inode,ext4_lblk_t end,struct extent_status * right_es,struct rsvd_count * rc)1240 static unsigned int get_rsvd(struct inode *inode, ext4_lblk_t end,
1241 struct extent_status *right_es,
1242 struct rsvd_count *rc)
1243 {
1244 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1245 struct pending_reservation *pr;
1246 struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1247 struct rb_node *node;
1248 ext4_lblk_t first_lclu, last_lclu;
1249 bool left_delonly, right_delonly, count_pending;
1250 struct extent_status *es;
1251
1252 if (sbi->s_cluster_ratio > 1) {
1253 /* count any remaining partial cluster */
1254 if (rc->partial)
1255 rc->ndelonly++;
1256
1257 if (rc->ndelonly == 0)
1258 return 0;
1259
1260 first_lclu = EXT4_B2C(sbi, rc->first_do_lblk);
1261 last_lclu = EXT4_B2C(sbi, rc->last_do_lblk);
1262
1263 /*
1264 * decrease the delonly count by the number of clusters at the
1265 * ends of the range that still contain delonly blocks -
1266 * these clusters still need to be reserved
1267 */
1268 left_delonly = right_delonly = false;
1269
1270 es = rc->left_es;
1271 while (es && ext4_es_end(es) >=
1272 EXT4_LBLK_CMASK(sbi, rc->first_do_lblk)) {
1273 if (ext4_es_is_delonly(es)) {
1274 rc->ndelonly--;
1275 left_delonly = true;
1276 break;
1277 }
1278 node = rb_prev(&es->rb_node);
1279 if (!node)
1280 break;
1281 es = rb_entry(node, struct extent_status, rb_node);
1282 }
1283 if (right_es && (!left_delonly || first_lclu != last_lclu)) {
1284 if (end < ext4_es_end(right_es)) {
1285 es = right_es;
1286 } else {
1287 node = rb_next(&right_es->rb_node);
1288 es = node ? rb_entry(node, struct extent_status,
1289 rb_node) : NULL;
1290 }
1291 while (es && es->es_lblk <=
1292 EXT4_LBLK_CFILL(sbi, rc->last_do_lblk)) {
1293 if (ext4_es_is_delonly(es)) {
1294 rc->ndelonly--;
1295 right_delonly = true;
1296 break;
1297 }
1298 node = rb_next(&es->rb_node);
1299 if (!node)
1300 break;
1301 es = rb_entry(node, struct extent_status,
1302 rb_node);
1303 }
1304 }
1305
1306 /*
1307 * Determine the block range that should be searched for
1308 * pending reservations, if any. Clusters on the ends of the
1309 * original removed range containing delonly blocks are
1310 * excluded. They've already been accounted for and it's not
1311 * possible to determine if an associated pending reservation
1312 * should be released with the information available in the
1313 * extents status tree.
1314 */
1315 if (first_lclu == last_lclu) {
1316 if (left_delonly | right_delonly)
1317 count_pending = false;
1318 else
1319 count_pending = true;
1320 } else {
1321 if (left_delonly)
1322 first_lclu++;
1323 if (right_delonly)
1324 last_lclu--;
1325 if (first_lclu <= last_lclu)
1326 count_pending = true;
1327 else
1328 count_pending = false;
1329 }
1330
1331 /*
1332 * a pending reservation found between first_lclu and last_lclu
1333 * represents an allocated cluster that contained at least one
1334 * delonly block, so the delonly total must be reduced by one
1335 * for each pending reservation found and released
1336 */
1337 if (count_pending) {
1338 pr = __pr_tree_search(&tree->root, first_lclu);
1339 while (pr && pr->lclu <= last_lclu) {
1340 rc->ndelonly--;
1341 node = rb_next(&pr->rb_node);
1342 rb_erase(&pr->rb_node, &tree->root);
1343 __free_pending(pr);
1344 if (!node)
1345 break;
1346 pr = rb_entry(node, struct pending_reservation,
1347 rb_node);
1348 }
1349 }
1350 }
1351 return rc->ndelonly;
1352 }
1353
1354
1355 /*
1356 * __es_remove_extent - removes block range from extent status tree
1357 *
1358 * @inode - file containing range
1359 * @lblk - first block in range
1360 * @end - last block in range
1361 * @reserved - number of cluster reservations released
1362 * @prealloc - pre-allocated es to avoid memory allocation failures
1363 *
1364 * If @reserved is not NULL and delayed allocation is enabled, counts
1365 * block/cluster reservations freed by removing range and if bigalloc
1366 * enabled cancels pending reservations as needed. Returns 0 on success,
1367 * error code on failure.
1368 */
__es_remove_extent(struct inode * inode,ext4_lblk_t lblk,ext4_lblk_t end,int * reserved,struct extent_status * prealloc)1369 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1370 ext4_lblk_t end, int *reserved,
1371 struct extent_status *prealloc)
1372 {
1373 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
1374 struct rb_node *node;
1375 struct extent_status *es;
1376 struct extent_status orig_es;
1377 ext4_lblk_t len1, len2;
1378 ext4_fsblk_t block;
1379 int err = 0;
1380 bool count_reserved = true;
1381 struct rsvd_count rc;
1382
1383 if (reserved == NULL || !test_opt(inode->i_sb, DELALLOC))
1384 count_reserved = false;
1385
1386 es = __es_tree_search(&tree->root, lblk);
1387 if (!es)
1388 goto out;
1389 if (es->es_lblk > end)
1390 goto out;
1391
1392 /* Simply invalidate cache_es. */
1393 tree->cache_es = NULL;
1394 if (count_reserved)
1395 init_rsvd(inode, lblk, es, &rc);
1396
1397 orig_es.es_lblk = es->es_lblk;
1398 orig_es.es_len = es->es_len;
1399 orig_es.es_pblk = es->es_pblk;
1400
1401 len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
1402 len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
1403 if (len1 > 0)
1404 es->es_len = len1;
1405 if (len2 > 0) {
1406 if (len1 > 0) {
1407 struct extent_status newes;
1408
1409 newes.es_lblk = end + 1;
1410 newes.es_len = len2;
1411 block = 0x7FDEADBEEFULL;
1412 if (ext4_es_is_written(&orig_es) ||
1413 ext4_es_is_unwritten(&orig_es))
1414 block = ext4_es_pblock(&orig_es) +
1415 orig_es.es_len - len2;
1416 ext4_es_store_pblock_status(&newes, block,
1417 ext4_es_status(&orig_es));
1418 err = __es_insert_extent(inode, &newes, prealloc);
1419 if (err) {
1420 if (!ext4_es_must_keep(&newes))
1421 return 0;
1422
1423 es->es_lblk = orig_es.es_lblk;
1424 es->es_len = orig_es.es_len;
1425 goto out;
1426 }
1427 } else {
1428 es->es_lblk = end + 1;
1429 es->es_len = len2;
1430 if (ext4_es_is_written(es) ||
1431 ext4_es_is_unwritten(es)) {
1432 block = orig_es.es_pblk + orig_es.es_len - len2;
1433 ext4_es_store_pblock(es, block);
1434 }
1435 }
1436 if (count_reserved)
1437 count_rsvd(inode, orig_es.es_lblk + len1,
1438 orig_es.es_len - len1 - len2, &orig_es, &rc);
1439 goto out_get_reserved;
1440 }
1441
1442 if (len1 > 0) {
1443 if (count_reserved)
1444 count_rsvd(inode, lblk, orig_es.es_len - len1,
1445 &orig_es, &rc);
1446 node = rb_next(&es->rb_node);
1447 if (node)
1448 es = rb_entry(node, struct extent_status, rb_node);
1449 else
1450 es = NULL;
1451 }
1452
1453 while (es && ext4_es_end(es) <= end) {
1454 if (count_reserved)
1455 count_rsvd(inode, es->es_lblk, es->es_len, es, &rc);
1456 node = rb_next(&es->rb_node);
1457 rb_erase(&es->rb_node, &tree->root);
1458 ext4_es_free_extent(inode, es);
1459 if (!node) {
1460 es = NULL;
1461 break;
1462 }
1463 es = rb_entry(node, struct extent_status, rb_node);
1464 }
1465
1466 if (es && es->es_lblk < end + 1) {
1467 ext4_lblk_t orig_len = es->es_len;
1468
1469 len1 = ext4_es_end(es) - end;
1470 if (count_reserved)
1471 count_rsvd(inode, es->es_lblk, orig_len - len1,
1472 es, &rc);
1473 es->es_lblk = end + 1;
1474 es->es_len = len1;
1475 if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
1476 block = es->es_pblk + orig_len - len1;
1477 ext4_es_store_pblock(es, block);
1478 }
1479 }
1480
1481 out_get_reserved:
1482 if (count_reserved)
1483 *reserved = get_rsvd(inode, end, es, &rc);
1484 out:
1485 return err;
1486 }
1487
1488 /*
1489 * ext4_es_remove_extent - removes block range from extent status tree
1490 *
1491 * @inode - file containing range
1492 * @lblk - first block in range
1493 * @len - number of blocks to remove
1494 *
1495 * Reduces block/cluster reservation count and for bigalloc cancels pending
1496 * reservations as needed. Returns 0 on success, error code on failure.
1497 */
ext4_es_remove_extent(struct inode * inode,ext4_lblk_t lblk,ext4_lblk_t len)1498 int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
1499 ext4_lblk_t len)
1500 {
1501 ext4_lblk_t end;
1502 int err = 0;
1503 int reserved = 0;
1504 struct extent_status *es = NULL;
1505
1506 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
1507 return 0;
1508
1509 trace_ext4_es_remove_extent(inode, lblk, len);
1510 es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
1511 lblk, len, inode->i_ino);
1512
1513 if (!len)
1514 return err;
1515
1516 end = lblk + len - 1;
1517 BUG_ON(end < lblk);
1518
1519 retry:
1520 if (err && !es)
1521 es = __es_alloc_extent(true);
1522 /*
1523 * ext4_clear_inode() depends on us taking i_es_lock unconditionally
1524 * so that we are sure __es_shrink() is done with the inode before it
1525 * is reclaimed.
1526 */
1527 write_lock(&EXT4_I(inode)->i_es_lock);
1528 err = __es_remove_extent(inode, lblk, end, &reserved, es);
1529 /* Free preallocated extent if it didn't get used. */
1530 if (es) {
1531 if (!es->es_len)
1532 __es_free_extent(es);
1533 es = NULL;
1534 }
1535 write_unlock(&EXT4_I(inode)->i_es_lock);
1536 if (err)
1537 goto retry;
1538
1539 ext4_es_print_tree(inode);
1540 ext4_da_release_space(inode, reserved);
1541 return 0;
1542 }
1543
__es_shrink(struct ext4_sb_info * sbi,int nr_to_scan,struct ext4_inode_info * locked_ei)1544 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
1545 struct ext4_inode_info *locked_ei)
1546 {
1547 struct ext4_inode_info *ei;
1548 struct ext4_es_stats *es_stats;
1549 ktime_t start_time;
1550 u64 scan_time;
1551 int nr_to_walk;
1552 int nr_shrunk = 0;
1553 int retried = 0, nr_skipped = 0;
1554
1555 es_stats = &sbi->s_es_stats;
1556 start_time = ktime_get();
1557
1558 retry:
1559 spin_lock(&sbi->s_es_lock);
1560 nr_to_walk = sbi->s_es_nr_inode;
1561 while (nr_to_walk-- > 0) {
1562 if (list_empty(&sbi->s_es_list)) {
1563 spin_unlock(&sbi->s_es_lock);
1564 goto out;
1565 }
1566 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
1567 i_es_list);
1568 /* Move the inode to the tail */
1569 list_move_tail(&ei->i_es_list, &sbi->s_es_list);
1570
1571 /*
1572 * Normally we try hard to avoid shrinking precached inodes,
1573 * but we will as a last resort.
1574 */
1575 if (!retried && ext4_test_inode_state(&ei->vfs_inode,
1576 EXT4_STATE_EXT_PRECACHED)) {
1577 nr_skipped++;
1578 continue;
1579 }
1580
1581 if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
1582 nr_skipped++;
1583 continue;
1584 }
1585 /*
1586 * Now we hold i_es_lock which protects us from inode reclaim
1587 * freeing inode under us
1588 */
1589 spin_unlock(&sbi->s_es_lock);
1590
1591 nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
1592 write_unlock(&ei->i_es_lock);
1593
1594 if (nr_to_scan <= 0)
1595 goto out;
1596 spin_lock(&sbi->s_es_lock);
1597 }
1598 spin_unlock(&sbi->s_es_lock);
1599
1600 /*
1601 * If we skipped any inodes, and we weren't able to make any
1602 * forward progress, try again to scan precached inodes.
1603 */
1604 if ((nr_shrunk == 0) && nr_skipped && !retried) {
1605 retried++;
1606 goto retry;
1607 }
1608
1609 if (locked_ei && nr_shrunk == 0)
1610 nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
1611
1612 out:
1613 scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1614 if (likely(es_stats->es_stats_scan_time))
1615 es_stats->es_stats_scan_time = (scan_time +
1616 es_stats->es_stats_scan_time*3) / 4;
1617 else
1618 es_stats->es_stats_scan_time = scan_time;
1619 if (scan_time > es_stats->es_stats_max_scan_time)
1620 es_stats->es_stats_max_scan_time = scan_time;
1621 if (likely(es_stats->es_stats_shrunk))
1622 es_stats->es_stats_shrunk = (nr_shrunk +
1623 es_stats->es_stats_shrunk*3) / 4;
1624 else
1625 es_stats->es_stats_shrunk = nr_shrunk;
1626
1627 trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
1628 nr_skipped, retried);
1629 return nr_shrunk;
1630 }
1631
ext4_es_count(struct shrinker * shrink,struct shrink_control * sc)1632 static unsigned long ext4_es_count(struct shrinker *shrink,
1633 struct shrink_control *sc)
1634 {
1635 unsigned long nr;
1636 struct ext4_sb_info *sbi;
1637
1638 sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
1639 nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1640 trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
1641 return nr;
1642 }
1643
ext4_es_scan(struct shrinker * shrink,struct shrink_control * sc)1644 static unsigned long ext4_es_scan(struct shrinker *shrink,
1645 struct shrink_control *sc)
1646 {
1647 struct ext4_sb_info *sbi = container_of(shrink,
1648 struct ext4_sb_info, s_es_shrinker);
1649 int nr_to_scan = sc->nr_to_scan;
1650 int ret, nr_shrunk;
1651
1652 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1653 trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
1654
1655 nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
1656
1657 ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
1658 trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
1659 return nr_shrunk;
1660 }
1661
ext4_seq_es_shrinker_info_show(struct seq_file * seq,void * v)1662 int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
1663 {
1664 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
1665 struct ext4_es_stats *es_stats = &sbi->s_es_stats;
1666 struct ext4_inode_info *ei, *max = NULL;
1667 unsigned int inode_cnt = 0;
1668
1669 if (v != SEQ_START_TOKEN)
1670 return 0;
1671
1672 /* here we just find an inode that has the max nr. of objects */
1673 spin_lock(&sbi->s_es_lock);
1674 list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
1675 inode_cnt++;
1676 if (max && max->i_es_all_nr < ei->i_es_all_nr)
1677 max = ei;
1678 else if (!max)
1679 max = ei;
1680 }
1681 spin_unlock(&sbi->s_es_lock);
1682
1683 seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
1684 percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
1685 percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
1686 seq_printf(seq, " %lld/%lld cache hits/misses\n",
1687 percpu_counter_sum_positive(&es_stats->es_stats_cache_hits),
1688 percpu_counter_sum_positive(&es_stats->es_stats_cache_misses));
1689 if (inode_cnt)
1690 seq_printf(seq, " %d inodes on list\n", inode_cnt);
1691
1692 seq_printf(seq, "average:\n %llu us scan time\n",
1693 div_u64(es_stats->es_stats_scan_time, 1000));
1694 seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
1695 if (inode_cnt)
1696 seq_printf(seq,
1697 "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
1698 " %llu us max scan time\n",
1699 max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
1700 div_u64(es_stats->es_stats_max_scan_time, 1000));
1701
1702 return 0;
1703 }
1704
ext4_es_register_shrinker(struct ext4_sb_info * sbi)1705 int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
1706 {
1707 int err;
1708
1709 /* Make sure we have enough bits for physical block number */
1710 BUILD_BUG_ON(ES_SHIFT < 48);
1711 INIT_LIST_HEAD(&sbi->s_es_list);
1712 sbi->s_es_nr_inode = 0;
1713 spin_lock_init(&sbi->s_es_lock);
1714 sbi->s_es_stats.es_stats_shrunk = 0;
1715 err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_hits, 0,
1716 GFP_KERNEL);
1717 if (err)
1718 return err;
1719 err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_misses, 0,
1720 GFP_KERNEL);
1721 if (err)
1722 goto err1;
1723 sbi->s_es_stats.es_stats_scan_time = 0;
1724 sbi->s_es_stats.es_stats_max_scan_time = 0;
1725 err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
1726 if (err)
1727 goto err2;
1728 err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
1729 if (err)
1730 goto err3;
1731
1732 sbi->s_es_shrinker.scan_objects = ext4_es_scan;
1733 sbi->s_es_shrinker.count_objects = ext4_es_count;
1734 sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
1735 err = register_shrinker(&sbi->s_es_shrinker);
1736 if (err)
1737 goto err4;
1738
1739 return 0;
1740 err4:
1741 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1742 err3:
1743 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1744 err2:
1745 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1746 err1:
1747 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1748 return err;
1749 }
1750
ext4_es_unregister_shrinker(struct ext4_sb_info * sbi)1751 void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
1752 {
1753 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1754 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1755 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1756 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1757 unregister_shrinker(&sbi->s_es_shrinker);
1758 }
1759
1760 /*
1761 * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
1762 * most *nr_to_scan extents, update *nr_to_scan accordingly.
1763 *
1764 * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
1765 * Increment *nr_shrunk by the number of reclaimed extents. Also update
1766 * ei->i_es_shrink_lblk to where we should continue scanning.
1767 */
es_do_reclaim_extents(struct ext4_inode_info * ei,ext4_lblk_t end,int * nr_to_scan,int * nr_shrunk)1768 static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
1769 int *nr_to_scan, int *nr_shrunk)
1770 {
1771 struct inode *inode = &ei->vfs_inode;
1772 struct ext4_es_tree *tree = &ei->i_es_tree;
1773 struct extent_status *es;
1774 struct rb_node *node;
1775
1776 es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
1777 if (!es)
1778 goto out_wrap;
1779
1780 while (*nr_to_scan > 0) {
1781 if (es->es_lblk > end) {
1782 ei->i_es_shrink_lblk = end + 1;
1783 return 0;
1784 }
1785
1786 (*nr_to_scan)--;
1787 node = rb_next(&es->rb_node);
1788
1789 if (ext4_es_must_keep(es))
1790 goto next;
1791 if (ext4_es_is_referenced(es)) {
1792 ext4_es_clear_referenced(es);
1793 goto next;
1794 }
1795
1796 rb_erase(&es->rb_node, &tree->root);
1797 ext4_es_free_extent(inode, es);
1798 (*nr_shrunk)++;
1799 next:
1800 if (!node)
1801 goto out_wrap;
1802 es = rb_entry(node, struct extent_status, rb_node);
1803 }
1804 ei->i_es_shrink_lblk = es->es_lblk;
1805 return 1;
1806 out_wrap:
1807 ei->i_es_shrink_lblk = 0;
1808 return 0;
1809 }
1810
es_reclaim_extents(struct ext4_inode_info * ei,int * nr_to_scan)1811 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
1812 {
1813 struct inode *inode = &ei->vfs_inode;
1814 int nr_shrunk = 0;
1815 ext4_lblk_t start = ei->i_es_shrink_lblk;
1816 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1817 DEFAULT_RATELIMIT_BURST);
1818
1819 if (ei->i_es_shk_nr == 0)
1820 return 0;
1821
1822 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
1823 __ratelimit(&_rs))
1824 ext4_warning(inode->i_sb, "forced shrink of precached extents");
1825
1826 if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
1827 start != 0)
1828 es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
1829
1830 ei->i_es_tree.cache_es = NULL;
1831 return nr_shrunk;
1832 }
1833
1834 /*
1835 * Called to support EXT4_IOC_CLEAR_ES_CACHE. We can only remove
1836 * discretionary entries from the extent status cache. (Some entries
1837 * must be present for proper operations.)
1838 */
ext4_clear_inode_es(struct inode * inode)1839 void ext4_clear_inode_es(struct inode *inode)
1840 {
1841 struct ext4_inode_info *ei = EXT4_I(inode);
1842 struct extent_status *es;
1843 struct ext4_es_tree *tree;
1844 struct rb_node *node;
1845
1846 write_lock(&ei->i_es_lock);
1847 tree = &EXT4_I(inode)->i_es_tree;
1848 tree->cache_es = NULL;
1849 node = rb_first(&tree->root);
1850 while (node) {
1851 es = rb_entry(node, struct extent_status, rb_node);
1852 node = rb_next(node);
1853 if (!ext4_es_must_keep(es)) {
1854 rb_erase(&es->rb_node, &tree->root);
1855 ext4_es_free_extent(inode, es);
1856 }
1857 }
1858 ext4_clear_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
1859 write_unlock(&ei->i_es_lock);
1860 }
1861
1862 #ifdef ES_DEBUG__
ext4_print_pending_tree(struct inode * inode)1863 static void ext4_print_pending_tree(struct inode *inode)
1864 {
1865 struct ext4_pending_tree *tree;
1866 struct rb_node *node;
1867 struct pending_reservation *pr;
1868
1869 printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino);
1870 tree = &EXT4_I(inode)->i_pending_tree;
1871 node = rb_first(&tree->root);
1872 while (node) {
1873 pr = rb_entry(node, struct pending_reservation, rb_node);
1874 printk(KERN_DEBUG " %u", pr->lclu);
1875 node = rb_next(node);
1876 }
1877 printk(KERN_DEBUG "\n");
1878 }
1879 #else
1880 #define ext4_print_pending_tree(inode)
1881 #endif
1882
ext4_init_pending(void)1883 int __init ext4_init_pending(void)
1884 {
1885 ext4_pending_cachep = kmem_cache_create("ext4_pending_reservation",
1886 sizeof(struct pending_reservation),
1887 0, (SLAB_RECLAIM_ACCOUNT), NULL);
1888 if (ext4_pending_cachep == NULL)
1889 return -ENOMEM;
1890 return 0;
1891 }
1892
ext4_exit_pending(void)1893 void ext4_exit_pending(void)
1894 {
1895 kmem_cache_destroy(ext4_pending_cachep);
1896 }
1897
ext4_init_pending_tree(struct ext4_pending_tree * tree)1898 void ext4_init_pending_tree(struct ext4_pending_tree *tree)
1899 {
1900 tree->root = RB_ROOT;
1901 }
1902
1903 /*
1904 * __get_pending - retrieve a pointer to a pending reservation
1905 *
1906 * @inode - file containing the pending cluster reservation
1907 * @lclu - logical cluster of interest
1908 *
1909 * Returns a pointer to a pending reservation if it's a member of
1910 * the set, and NULL if not. Must be called holding i_es_lock.
1911 */
__get_pending(struct inode * inode,ext4_lblk_t lclu)1912 static struct pending_reservation *__get_pending(struct inode *inode,
1913 ext4_lblk_t lclu)
1914 {
1915 struct ext4_pending_tree *tree;
1916 struct rb_node *node;
1917 struct pending_reservation *pr = NULL;
1918
1919 tree = &EXT4_I(inode)->i_pending_tree;
1920 node = (&tree->root)->rb_node;
1921
1922 while (node) {
1923 pr = rb_entry(node, struct pending_reservation, rb_node);
1924 if (lclu < pr->lclu)
1925 node = node->rb_left;
1926 else if (lclu > pr->lclu)
1927 node = node->rb_right;
1928 else if (lclu == pr->lclu)
1929 return pr;
1930 }
1931 return NULL;
1932 }
1933
1934 /*
1935 * __insert_pending - adds a pending cluster reservation to the set of
1936 * pending reservations
1937 *
1938 * @inode - file containing the cluster
1939 * @lblk - logical block in the cluster to be added
1940 * @prealloc - preallocated pending entry
1941 *
1942 * Returns 0 on successful insertion and -ENOMEM on failure. If the
1943 * pending reservation is already in the set, returns successfully.
1944 */
__insert_pending(struct inode * inode,ext4_lblk_t lblk,struct pending_reservation ** prealloc)1945 static int __insert_pending(struct inode *inode, ext4_lblk_t lblk,
1946 struct pending_reservation **prealloc)
1947 {
1948 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1949 struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
1950 struct rb_node **p = &tree->root.rb_node;
1951 struct rb_node *parent = NULL;
1952 struct pending_reservation *pr;
1953 ext4_lblk_t lclu;
1954 int ret = 0;
1955
1956 lclu = EXT4_B2C(sbi, lblk);
1957 /* search to find parent for insertion */
1958 while (*p) {
1959 parent = *p;
1960 pr = rb_entry(parent, struct pending_reservation, rb_node);
1961
1962 if (lclu < pr->lclu) {
1963 p = &(*p)->rb_left;
1964 } else if (lclu > pr->lclu) {
1965 p = &(*p)->rb_right;
1966 } else {
1967 /* pending reservation already inserted */
1968 goto out;
1969 }
1970 }
1971
1972 if (likely(*prealloc == NULL)) {
1973 pr = __alloc_pending(false);
1974 if (!pr) {
1975 ret = -ENOMEM;
1976 goto out;
1977 }
1978 } else {
1979 pr = *prealloc;
1980 *prealloc = NULL;
1981 }
1982 pr->lclu = lclu;
1983
1984 rb_link_node(&pr->rb_node, parent, p);
1985 rb_insert_color(&pr->rb_node, &tree->root);
1986
1987 out:
1988 return ret;
1989 }
1990
1991 /*
1992 * __remove_pending - removes a pending cluster reservation from the set
1993 * of pending reservations
1994 *
1995 * @inode - file containing the cluster
1996 * @lblk - logical block in the pending cluster reservation to be removed
1997 *
1998 * Returns successfully if pending reservation is not a member of the set.
1999 */
__remove_pending(struct inode * inode,ext4_lblk_t lblk)2000 static void __remove_pending(struct inode *inode, ext4_lblk_t lblk)
2001 {
2002 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2003 struct pending_reservation *pr;
2004 struct ext4_pending_tree *tree;
2005
2006 pr = __get_pending(inode, EXT4_B2C(sbi, lblk));
2007 if (pr != NULL) {
2008 tree = &EXT4_I(inode)->i_pending_tree;
2009 rb_erase(&pr->rb_node, &tree->root);
2010 __free_pending(pr);
2011 }
2012 }
2013
2014 /*
2015 * ext4_remove_pending - removes a pending cluster reservation from the set
2016 * of pending reservations
2017 *
2018 * @inode - file containing the cluster
2019 * @lblk - logical block in the pending cluster reservation to be removed
2020 *
2021 * Locking for external use of __remove_pending.
2022 */
ext4_remove_pending(struct inode * inode,ext4_lblk_t lblk)2023 void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk)
2024 {
2025 struct ext4_inode_info *ei = EXT4_I(inode);
2026
2027 write_lock(&ei->i_es_lock);
2028 __remove_pending(inode, lblk);
2029 write_unlock(&ei->i_es_lock);
2030 }
2031
2032 /*
2033 * ext4_is_pending - determine whether a cluster has a pending reservation
2034 * on it
2035 *
2036 * @inode - file containing the cluster
2037 * @lblk - logical block in the cluster
2038 *
2039 * Returns true if there's a pending reservation for the cluster in the
2040 * set of pending reservations, and false if not.
2041 */
ext4_is_pending(struct inode * inode,ext4_lblk_t lblk)2042 bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk)
2043 {
2044 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2045 struct ext4_inode_info *ei = EXT4_I(inode);
2046 bool ret;
2047
2048 read_lock(&ei->i_es_lock);
2049 ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL);
2050 read_unlock(&ei->i_es_lock);
2051
2052 return ret;
2053 }
2054
2055 /*
2056 * ext4_es_insert_delayed_block - adds a delayed block to the extents status
2057 * tree, adding a pending reservation where
2058 * needed
2059 *
2060 * @inode - file containing the newly added block
2061 * @lblk - logical block to be added
2062 * @allocated - indicates whether a physical cluster has been allocated for
2063 * the logical cluster that contains the block
2064 *
2065 * Returns 0 on success, negative error code on failure.
2066 */
ext4_es_insert_delayed_block(struct inode * inode,ext4_lblk_t lblk,bool allocated)2067 int ext4_es_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk,
2068 bool allocated)
2069 {
2070 struct extent_status newes;
2071 int err1 = 0, err2 = 0, err3 = 0;
2072 struct extent_status *es1 = NULL;
2073 struct extent_status *es2 = NULL;
2074 struct pending_reservation *pr = NULL;
2075
2076 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
2077 return 0;
2078
2079 es_debug("add [%u/1) delayed to extent status tree of inode %lu\n",
2080 lblk, inode->i_ino);
2081
2082 newes.es_lblk = lblk;
2083 newes.es_len = 1;
2084 ext4_es_store_pblock_status(&newes, ~0, EXTENT_STATUS_DELAYED);
2085 trace_ext4_es_insert_delayed_block(inode, &newes, allocated);
2086
2087 ext4_es_insert_extent_check(inode, &newes);
2088
2089 retry:
2090 if (err1 && !es1)
2091 es1 = __es_alloc_extent(true);
2092 if ((err1 || err2) && !es2)
2093 es2 = __es_alloc_extent(true);
2094 if ((err1 || err2 || err3) && allocated && !pr)
2095 pr = __alloc_pending(true);
2096 write_lock(&EXT4_I(inode)->i_es_lock);
2097
2098 err1 = __es_remove_extent(inode, lblk, lblk, NULL, es1);
2099 if (err1 != 0)
2100 goto error;
2101 /* Free preallocated extent if it didn't get used. */
2102 if (es1) {
2103 if (!es1->es_len)
2104 __es_free_extent(es1);
2105 es1 = NULL;
2106 }
2107
2108 err2 = __es_insert_extent(inode, &newes, es2);
2109 if (err2 != 0)
2110 goto error;
2111 /* Free preallocated extent if it didn't get used. */
2112 if (es2) {
2113 if (!es2->es_len)
2114 __es_free_extent(es2);
2115 es2 = NULL;
2116 }
2117
2118 if (allocated) {
2119 err3 = __insert_pending(inode, lblk, &pr);
2120 if (err3 != 0)
2121 goto error;
2122 if (pr) {
2123 __free_pending(pr);
2124 pr = NULL;
2125 }
2126 }
2127 error:
2128 write_unlock(&EXT4_I(inode)->i_es_lock);
2129 if (err1 || err2 || err3)
2130 goto retry;
2131
2132 ext4_es_print_tree(inode);
2133 ext4_print_pending_tree(inode);
2134 return 0;
2135 }
2136
2137 /*
2138 * __es_delayed_clu - count number of clusters containing blocks that
2139 * are delayed only
2140 *
2141 * @inode - file containing block range
2142 * @start - logical block defining start of range
2143 * @end - logical block defining end of range
2144 *
2145 * Returns the number of clusters containing only delayed (not delayed
2146 * and unwritten) blocks in the range specified by @start and @end. Any
2147 * cluster or part of a cluster within the range and containing a delayed
2148 * and not unwritten block within the range is counted as a whole cluster.
2149 */
__es_delayed_clu(struct inode * inode,ext4_lblk_t start,ext4_lblk_t end)2150 static unsigned int __es_delayed_clu(struct inode *inode, ext4_lblk_t start,
2151 ext4_lblk_t end)
2152 {
2153 struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
2154 struct extent_status *es;
2155 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2156 struct rb_node *node;
2157 ext4_lblk_t first_lclu, last_lclu;
2158 unsigned long long last_counted_lclu;
2159 unsigned int n = 0;
2160
2161 /* guaranteed to be unequal to any ext4_lblk_t value */
2162 last_counted_lclu = ~0ULL;
2163
2164 es = __es_tree_search(&tree->root, start);
2165
2166 while (es && (es->es_lblk <= end)) {
2167 if (ext4_es_is_delonly(es)) {
2168 if (es->es_lblk <= start)
2169 first_lclu = EXT4_B2C(sbi, start);
2170 else
2171 first_lclu = EXT4_B2C(sbi, es->es_lblk);
2172
2173 if (ext4_es_end(es) >= end)
2174 last_lclu = EXT4_B2C(sbi, end);
2175 else
2176 last_lclu = EXT4_B2C(sbi, ext4_es_end(es));
2177
2178 if (first_lclu == last_counted_lclu)
2179 n += last_lclu - first_lclu;
2180 else
2181 n += last_lclu - first_lclu + 1;
2182 last_counted_lclu = last_lclu;
2183 }
2184 node = rb_next(&es->rb_node);
2185 if (!node)
2186 break;
2187 es = rb_entry(node, struct extent_status, rb_node);
2188 }
2189
2190 return n;
2191 }
2192
2193 /*
2194 * ext4_es_delayed_clu - count number of clusters containing blocks that
2195 * are both delayed and unwritten
2196 *
2197 * @inode - file containing block range
2198 * @lblk - logical block defining start of range
2199 * @len - number of blocks in range
2200 *
2201 * Locking for external use of __es_delayed_clu().
2202 */
ext4_es_delayed_clu(struct inode * inode,ext4_lblk_t lblk,ext4_lblk_t len)2203 unsigned int ext4_es_delayed_clu(struct inode *inode, ext4_lblk_t lblk,
2204 ext4_lblk_t len)
2205 {
2206 struct ext4_inode_info *ei = EXT4_I(inode);
2207 ext4_lblk_t end;
2208 unsigned int n;
2209
2210 if (len == 0)
2211 return 0;
2212
2213 end = lblk + len - 1;
2214 WARN_ON(end < lblk);
2215
2216 read_lock(&ei->i_es_lock);
2217
2218 n = __es_delayed_clu(inode, lblk, end);
2219
2220 read_unlock(&ei->i_es_lock);
2221
2222 return n;
2223 }
2224
2225 /*
2226 * __revise_pending - makes, cancels, or leaves unchanged pending cluster
2227 * reservations for a specified block range depending
2228 * upon the presence or absence of delayed blocks
2229 * outside the range within clusters at the ends of the
2230 * range
2231 *
2232 * @inode - file containing the range
2233 * @lblk - logical block defining the start of range
2234 * @len - length of range in blocks
2235 * @prealloc - preallocated pending entry
2236 *
2237 * Used after a newly allocated extent is added to the extents status tree.
2238 * Requires that the extents in the range have either written or unwritten
2239 * status. Must be called while holding i_es_lock.
2240 */
__revise_pending(struct inode * inode,ext4_lblk_t lblk,ext4_lblk_t len,struct pending_reservation ** prealloc)2241 static int __revise_pending(struct inode *inode, ext4_lblk_t lblk,
2242 ext4_lblk_t len,
2243 struct pending_reservation **prealloc)
2244 {
2245 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2246 ext4_lblk_t end = lblk + len - 1;
2247 ext4_lblk_t first, last;
2248 bool f_del = false, l_del = false;
2249 int ret = 0;
2250
2251 if (len == 0)
2252 return 0;
2253
2254 /*
2255 * Two cases - block range within single cluster and block range
2256 * spanning two or more clusters. Note that a cluster belonging
2257 * to a range starting and/or ending on a cluster boundary is treated
2258 * as if it does not contain a delayed extent. The new range may
2259 * have allocated space for previously delayed blocks out to the
2260 * cluster boundary, requiring that any pre-existing pending
2261 * reservation be canceled. Because this code only looks at blocks
2262 * outside the range, it should revise pending reservations
2263 * correctly even if the extent represented by the range can't be
2264 * inserted in the extents status tree due to ENOSPC.
2265 */
2266
2267 if (EXT4_B2C(sbi, lblk) == EXT4_B2C(sbi, end)) {
2268 first = EXT4_LBLK_CMASK(sbi, lblk);
2269 if (first != lblk)
2270 f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2271 first, lblk - 1);
2272 if (f_del) {
2273 ret = __insert_pending(inode, first, prealloc);
2274 if (ret < 0)
2275 goto out;
2276 } else {
2277 last = EXT4_LBLK_CMASK(sbi, end) +
2278 sbi->s_cluster_ratio - 1;
2279 if (last != end)
2280 l_del = __es_scan_range(inode,
2281 &ext4_es_is_delonly,
2282 end + 1, last);
2283 if (l_del) {
2284 ret = __insert_pending(inode, last, prealloc);
2285 if (ret < 0)
2286 goto out;
2287 } else
2288 __remove_pending(inode, last);
2289 }
2290 } else {
2291 first = EXT4_LBLK_CMASK(sbi, lblk);
2292 if (first != lblk)
2293 f_del = __es_scan_range(inode, &ext4_es_is_delonly,
2294 first, lblk - 1);
2295 if (f_del) {
2296 ret = __insert_pending(inode, first, prealloc);
2297 if (ret < 0)
2298 goto out;
2299 } else
2300 __remove_pending(inode, first);
2301
2302 last = EXT4_LBLK_CMASK(sbi, end) + sbi->s_cluster_ratio - 1;
2303 if (last != end)
2304 l_del = __es_scan_range(inode, &ext4_es_is_delonly,
2305 end + 1, last);
2306 if (l_del) {
2307 ret = __insert_pending(inode, last, prealloc);
2308 if (ret < 0)
2309 goto out;
2310 } else
2311 __remove_pending(inode, last);
2312 }
2313 out:
2314 return ret;
2315 }
2316