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