Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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 | */ |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 13 | #include <linux/list_sort.h> |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 14 | #include <linux/proc_fs.h> |
| 15 | #include <linux/seq_file.h> |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 16 | #include "ext4.h" |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 17 | |
Zheng Liu | 992e9fd | 2012-11-08 21:57:33 -0500 | [diff] [blame] | 18 | #include <trace/events/ext4.h> |
| 19 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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 |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 27 | * extent tree, whose goal is only track delayed extents in memory to |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 28 | * simplify the implementation of fiemap and bigalloc, and introduce |
| 29 | * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 30 | * delay extent tree at the first commit. But for better understand |
| 31 | * what it does, it has been rename to extent status tree. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 32 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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 |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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. |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 55 | */ |
| 56 | |
| 57 | /* |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 58 | * Extent status tree implementation for ext4. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 59 | * |
| 60 | * |
| 61 | * ========================================================================== |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 62 | * Extent status tree tracks all extent status. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 63 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 64 | * 1. Why we need to implement extent status tree? |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 65 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 66 | * Without extent status tree, ext4 identifies a delayed extent by looking |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 67 | * up page cache, this has several deficiencies - complicated, buggy, |
| 68 | * and inefficient code. |
| 69 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 72 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 73 | * Let us have a look at how they do without extent status tree. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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 | * |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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 |
Masahiro Yamada | 3f8b6fb | 2017-02-27 14:29:25 -0800 | [diff] [blame] | 88 | * time consuming. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 89 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 90 | * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA, |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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 |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 93 | * not by searching the extent tree. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 94 | * |
| 95 | * |
| 96 | * ========================================================================== |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 97 | * 2. Ext4 extent status tree impelmentation |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 98 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 105 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 110 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 114 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 122 | * |
| 123 | * |
| 124 | * ========================================================================== |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 125 | * 3. Performance analysis |
| 126 | * |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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 |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 138 | * |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 139 | * -- Refactor delayed space reservation |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 140 | * |
| 141 | * -- Extent-level locking |
| 142 | */ |
| 143 | |
| 144 | static struct kmem_cache *ext4_es_cachep; |
| 145 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 146 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes); |
| 147 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 148 | ext4_lblk_t end); |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 149 | static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 150 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
| 151 | struct ext4_inode_info *locked_ei); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 152 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 153 | int __init ext4_init_es(void) |
| 154 | { |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 155 | ext4_es_cachep = kmem_cache_create("ext4_extent_status", |
| 156 | sizeof(struct extent_status), |
| 157 | 0, (SLAB_RECLAIM_ACCOUNT), NULL); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 158 | if (ext4_es_cachep == NULL) |
| 159 | return -ENOMEM; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | void ext4_exit_es(void) |
| 164 | { |
Sean Fu | 21c580d | 2018-05-20 22:44:13 -0400 | [diff] [blame] | 165 | kmem_cache_destroy(ext4_es_cachep); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void ext4_es_init_tree(struct ext4_es_tree *tree) |
| 169 | { |
| 170 | tree->root = RB_ROOT; |
| 171 | tree->cache_es = NULL; |
| 172 | } |
| 173 | |
| 174 | #ifdef ES_DEBUG__ |
| 175 | static void ext4_es_print_tree(struct inode *inode) |
| 176 | { |
| 177 | struct ext4_es_tree *tree; |
| 178 | struct rb_node *node; |
| 179 | |
| 180 | printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino); |
| 181 | tree = &EXT4_I(inode)->i_es_tree; |
| 182 | node = rb_first(&tree->root); |
| 183 | while (node) { |
| 184 | struct extent_status *es; |
| 185 | es = rb_entry(node, struct extent_status, rb_node); |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 186 | printk(KERN_DEBUG " [%u/%u) %llu %x", |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 187 | es->es_lblk, es->es_len, |
| 188 | ext4_es_pblock(es), ext4_es_status(es)); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 189 | node = rb_next(node); |
| 190 | } |
| 191 | printk(KERN_DEBUG "\n"); |
| 192 | } |
| 193 | #else |
| 194 | #define ext4_es_print_tree(inode) |
| 195 | #endif |
| 196 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 197 | static inline ext4_lblk_t ext4_es_end(struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 198 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 199 | BUG_ON(es->es_lblk + es->es_len < es->es_lblk); |
| 200 | return es->es_lblk + es->es_len - 1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * search through the tree for an delayed extent with a given offset. If |
| 205 | * it can't be found, try to find next extent. |
| 206 | */ |
| 207 | static struct extent_status *__es_tree_search(struct rb_root *root, |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 208 | ext4_lblk_t lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 209 | { |
| 210 | struct rb_node *node = root->rb_node; |
| 211 | struct extent_status *es = NULL; |
| 212 | |
| 213 | while (node) { |
| 214 | es = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 215 | if (lblk < es->es_lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 216 | node = node->rb_left; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 217 | else if (lblk > ext4_es_end(es)) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 218 | node = node->rb_right; |
| 219 | else |
| 220 | return es; |
| 221 | } |
| 222 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 223 | if (es && lblk < es->es_lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 224 | return es; |
| 225 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 226 | if (es && lblk > ext4_es_end(es)) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 227 | node = rb_next(&es->rb_node); |
| 228 | return node ? rb_entry(node, struct extent_status, rb_node) : |
| 229 | NULL; |
| 230 | } |
| 231 | |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | /* |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 236 | * ext4_es_find_extent_range - find extent with specified status within block |
| 237 | * range or next extent following block range in |
| 238 | * extents status tree |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 239 | * |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 240 | * @inode - file containing the range |
| 241 | * @matching_fn - pointer to function that matches extents with desired status |
| 242 | * @lblk - logical block defining start of range |
| 243 | * @end - logical block defining end of range |
| 244 | * @es - extent found, if any |
| 245 | * |
| 246 | * Find the first extent within the block range specified by @lblk and @end |
| 247 | * in the extents status tree that satisfies @matching_fn. If a match |
| 248 | * is found, it's returned in @es. If not, and a matching extent is found |
| 249 | * beyond the block range, it's returned in @es. If no match is found, an |
| 250 | * extent is returned in @es whose es_lblk, es_len, and es_pblk components |
| 251 | * are 0. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 252 | */ |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 253 | static void __es_find_extent_range(struct inode *inode, |
| 254 | int (*matching_fn)(struct extent_status *es), |
| 255 | ext4_lblk_t lblk, ext4_lblk_t end, |
| 256 | struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 257 | { |
| 258 | struct ext4_es_tree *tree = NULL; |
| 259 | struct extent_status *es1 = NULL; |
| 260 | struct rb_node *node; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 261 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 262 | WARN_ON(es == NULL); |
| 263 | WARN_ON(end < lblk); |
Zheng Liu | 992e9fd | 2012-11-08 21:57:33 -0500 | [diff] [blame] | 264 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 265 | tree = &EXT4_I(inode)->i_es_tree; |
| 266 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 267 | /* see if the extent has been cached */ |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 268 | es->es_lblk = es->es_len = es->es_pblk = 0; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 269 | if (tree->cache_es) { |
| 270 | es1 = tree->cache_es; |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 271 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame] | 272 | es_debug("%u cached by [%u/%u) %llu %x\n", |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 273 | lblk, es1->es_lblk, es1->es_len, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 274 | ext4_es_pblock(es1), ext4_es_status(es1)); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 275 | goto out; |
| 276 | } |
| 277 | } |
| 278 | |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 279 | es1 = __es_tree_search(&tree->root, lblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 280 | |
| 281 | out: |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 282 | if (es1 && !matching_fn(es1)) { |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 283 | while ((node = rb_next(&es1->rb_node)) != NULL) { |
| 284 | es1 = rb_entry(node, struct extent_status, rb_node); |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 285 | if (es1->es_lblk > end) { |
| 286 | es1 = NULL; |
| 287 | break; |
| 288 | } |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 289 | if (matching_fn(es1)) |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 290 | break; |
| 291 | } |
| 292 | } |
| 293 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 294 | if (es1 && matching_fn(es1)) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 295 | tree->cache_es = es1; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 296 | es->es_lblk = es1->es_lblk; |
| 297 | es->es_len = es1->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 298 | es->es_pblk = es1->es_pblk; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 299 | } |
| 300 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Locking for __es_find_extent_range() for external use |
| 305 | */ |
| 306 | void ext4_es_find_extent_range(struct inode *inode, |
| 307 | int (*matching_fn)(struct extent_status *es), |
| 308 | ext4_lblk_t lblk, ext4_lblk_t end, |
| 309 | struct extent_status *es) |
| 310 | { |
| 311 | trace_ext4_es_find_extent_range_enter(inode, lblk); |
| 312 | |
| 313 | read_lock(&EXT4_I(inode)->i_es_lock); |
| 314 | __es_find_extent_range(inode, matching_fn, lblk, end, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 315 | read_unlock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | 992e9fd | 2012-11-08 21:57:33 -0500 | [diff] [blame] | 316 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame^] | 317 | trace_ext4_es_find_extent_range_exit(inode, es); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * __es_scan_range - search block range for block with specified status |
| 322 | * in extents status tree |
| 323 | * |
| 324 | * @inode - file containing the range |
| 325 | * @matching_fn - pointer to function that matches extents with desired status |
| 326 | * @lblk - logical block defining start of range |
| 327 | * @end - logical block defining end of range |
| 328 | * |
| 329 | * Returns true if at least one block in the specified block range satisfies |
| 330 | * the criterion specified by @matching_fn, and false if not. If at least |
| 331 | * one extent has the specified status, then there is at least one block |
| 332 | * in the cluster with that status. Should only be called by code that has |
| 333 | * taken i_es_lock. |
| 334 | */ |
| 335 | static bool __es_scan_range(struct inode *inode, |
| 336 | int (*matching_fn)(struct extent_status *es), |
| 337 | ext4_lblk_t start, ext4_lblk_t end) |
| 338 | { |
| 339 | struct extent_status es; |
| 340 | |
| 341 | __es_find_extent_range(inode, matching_fn, start, end, &es); |
| 342 | if (es.es_len == 0) |
| 343 | return false; /* no matching extent in the tree */ |
| 344 | else if (es.es_lblk <= start && |
| 345 | start < es.es_lblk + es.es_len) |
| 346 | return true; |
| 347 | else if (start <= es.es_lblk && es.es_lblk <= end) |
| 348 | return true; |
| 349 | else |
| 350 | return false; |
| 351 | } |
| 352 | /* |
| 353 | * Locking for __es_scan_range() for external use |
| 354 | */ |
| 355 | bool ext4_es_scan_range(struct inode *inode, |
| 356 | int (*matching_fn)(struct extent_status *es), |
| 357 | ext4_lblk_t lblk, ext4_lblk_t end) |
| 358 | { |
| 359 | bool ret; |
| 360 | |
| 361 | read_lock(&EXT4_I(inode)->i_es_lock); |
| 362 | ret = __es_scan_range(inode, matching_fn, lblk, end); |
| 363 | read_unlock(&EXT4_I(inode)->i_es_lock); |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * __es_scan_clu - search cluster for block with specified status in |
| 370 | * extents status tree |
| 371 | * |
| 372 | * @inode - file containing the cluster |
| 373 | * @matching_fn - pointer to function that matches extents with desired status |
| 374 | * @lblk - logical block in cluster to be searched |
| 375 | * |
| 376 | * Returns true if at least one extent in the cluster containing @lblk |
| 377 | * satisfies the criterion specified by @matching_fn, and false if not. If at |
| 378 | * least one extent has the specified status, then there is at least one block |
| 379 | * in the cluster with that status. Should only be called by code that has |
| 380 | * taken i_es_lock. |
| 381 | */ |
| 382 | static bool __es_scan_clu(struct inode *inode, |
| 383 | int (*matching_fn)(struct extent_status *es), |
| 384 | ext4_lblk_t lblk) |
| 385 | { |
| 386 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 387 | ext4_lblk_t lblk_start, lblk_end; |
| 388 | |
| 389 | lblk_start = EXT4_LBLK_CMASK(sbi, lblk); |
| 390 | lblk_end = lblk_start + sbi->s_cluster_ratio - 1; |
| 391 | |
| 392 | return __es_scan_range(inode, matching_fn, lblk_start, lblk_end); |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Locking for __es_scan_clu() for external use |
| 397 | */ |
| 398 | bool ext4_es_scan_clu(struct inode *inode, |
| 399 | int (*matching_fn)(struct extent_status *es), |
| 400 | ext4_lblk_t lblk) |
| 401 | { |
| 402 | bool ret; |
| 403 | |
| 404 | read_lock(&EXT4_I(inode)->i_es_lock); |
| 405 | ret = __es_scan_clu(inode, matching_fn, lblk); |
| 406 | read_unlock(&EXT4_I(inode)->i_es_lock); |
| 407 | |
| 408 | return ret; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 409 | } |
| 410 | |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 411 | static void ext4_es_list_add(struct inode *inode) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 412 | { |
| 413 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 414 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 415 | |
| 416 | if (!list_empty(&ei->i_es_list)) |
| 417 | return; |
| 418 | |
| 419 | spin_lock(&sbi->s_es_lock); |
| 420 | if (list_empty(&ei->i_es_list)) { |
| 421 | list_add_tail(&ei->i_es_list, &sbi->s_es_list); |
| 422 | sbi->s_es_nr_inode++; |
| 423 | } |
| 424 | spin_unlock(&sbi->s_es_lock); |
| 425 | } |
| 426 | |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 427 | static void ext4_es_list_del(struct inode *inode) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 428 | { |
| 429 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 430 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 431 | |
| 432 | spin_lock(&sbi->s_es_lock); |
| 433 | if (!list_empty(&ei->i_es_list)) { |
| 434 | list_del_init(&ei->i_es_list); |
| 435 | sbi->s_es_nr_inode--; |
| 436 | WARN_ON_ONCE(sbi->s_es_nr_inode < 0); |
| 437 | } |
| 438 | spin_unlock(&sbi->s_es_lock); |
| 439 | } |
| 440 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 441 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 442 | ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len, |
| 443 | ext4_fsblk_t pblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 444 | { |
| 445 | struct extent_status *es; |
| 446 | es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC); |
| 447 | if (es == NULL) |
| 448 | return NULL; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 449 | es->es_lblk = lblk; |
| 450 | es->es_len = len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 451 | es->es_pblk = pblk; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * We don't count delayed extent because we never try to reclaim them |
| 455 | */ |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 456 | if (!ext4_es_is_delayed(es)) { |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 457 | if (!EXT4_I(inode)->i_es_shk_nr++) |
| 458 | ext4_es_list_add(inode); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 459 | percpu_counter_inc(&EXT4_SB(inode->i_sb)-> |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 460 | s_es_stats.es_stats_shk_cnt); |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 461 | } |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 462 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 463 | EXT4_I(inode)->i_es_all_nr++; |
| 464 | percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt); |
| 465 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 466 | return es; |
| 467 | } |
| 468 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 469 | static void ext4_es_free_extent(struct inode *inode, struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 470 | { |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 471 | EXT4_I(inode)->i_es_all_nr--; |
| 472 | percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt); |
| 473 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 474 | /* Decrease the shrink counter when this es is not delayed */ |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 475 | if (!ext4_es_is_delayed(es)) { |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 476 | BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0); |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 477 | if (!--EXT4_I(inode)->i_es_shk_nr) |
| 478 | ext4_es_list_del(inode); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 479 | percpu_counter_dec(&EXT4_SB(inode->i_sb)-> |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 480 | s_es_stats.es_stats_shk_cnt); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 483 | kmem_cache_free(ext4_es_cachep, es); |
| 484 | } |
| 485 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 486 | /* |
| 487 | * Check whether or not two extents can be merged |
| 488 | * Condition: |
| 489 | * - logical block number is contiguous |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 490 | * - physical block number is contiguous |
| 491 | * - status is equal |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 492 | */ |
| 493 | static int ext4_es_can_be_merged(struct extent_status *es1, |
| 494 | struct extent_status *es2) |
| 495 | { |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 496 | if (ext4_es_type(es1) != ext4_es_type(es2)) |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 497 | return 0; |
| 498 | |
Lukas Czerner | 0baaea6 | 2014-05-12 22:21:43 -0400 | [diff] [blame] | 499 | if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) { |
| 500 | pr_warn("ES assertion failed when merging extents. " |
| 501 | "The sum of lengths of es1 (%d) and es2 (%d) " |
| 502 | "is bigger than allowed file size (%d)\n", |
| 503 | es1->es_len, es2->es_len, EXT_MAX_BLOCKS); |
| 504 | WARN_ON(1); |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 505 | return 0; |
Lukas Czerner | 0baaea6 | 2014-05-12 22:21:43 -0400 | [diff] [blame] | 506 | } |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 507 | |
Zheng Liu | bd38436 | 2013-03-10 20:48:59 -0400 | [diff] [blame] | 508 | if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk) |
| 509 | return 0; |
| 510 | |
| 511 | if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) && |
| 512 | (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2))) |
| 513 | return 1; |
| 514 | |
| 515 | if (ext4_es_is_hole(es1)) |
| 516 | return 1; |
| 517 | |
| 518 | /* we need to check delayed extent is without unwritten status */ |
| 519 | if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1)) |
| 520 | return 1; |
| 521 | |
| 522 | return 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 525 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 526 | ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 527 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 528 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 529 | struct extent_status *es1; |
| 530 | struct rb_node *node; |
| 531 | |
| 532 | node = rb_prev(&es->rb_node); |
| 533 | if (!node) |
| 534 | return es; |
| 535 | |
| 536 | es1 = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 537 | if (ext4_es_can_be_merged(es1, es)) { |
| 538 | es1->es_len += es->es_len; |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 539 | if (ext4_es_is_referenced(es)) |
| 540 | ext4_es_set_referenced(es1); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 541 | rb_erase(&es->rb_node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 542 | ext4_es_free_extent(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 543 | es = es1; |
| 544 | } |
| 545 | |
| 546 | return es; |
| 547 | } |
| 548 | |
| 549 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 550 | ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 551 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 552 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 553 | struct extent_status *es1; |
| 554 | struct rb_node *node; |
| 555 | |
| 556 | node = rb_next(&es->rb_node); |
| 557 | if (!node) |
| 558 | return es; |
| 559 | |
| 560 | es1 = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 561 | if (ext4_es_can_be_merged(es, es1)) { |
| 562 | es->es_len += es1->es_len; |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 563 | if (ext4_es_is_referenced(es1)) |
| 564 | ext4_es_set_referenced(es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 565 | rb_erase(node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 566 | ext4_es_free_extent(inode, es1); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return es; |
| 570 | } |
| 571 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 572 | #ifdef ES_AGGRESSIVE_TEST |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 573 | #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */ |
| 574 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 575 | static void ext4_es_insert_extent_ext_check(struct inode *inode, |
| 576 | struct extent_status *es) |
| 577 | { |
| 578 | struct ext4_ext_path *path = NULL; |
| 579 | struct ext4_extent *ex; |
| 580 | ext4_lblk_t ee_block; |
| 581 | ext4_fsblk_t ee_start; |
| 582 | unsigned short ee_len; |
| 583 | int depth, ee_status, es_status; |
| 584 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 585 | path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE); |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 586 | if (IS_ERR(path)) |
| 587 | return; |
| 588 | |
| 589 | depth = ext_depth(inode); |
| 590 | ex = path[depth].p_ext; |
| 591 | |
| 592 | if (ex) { |
| 593 | |
| 594 | ee_block = le32_to_cpu(ex->ee_block); |
| 595 | ee_start = ext4_ext_pblock(ex); |
| 596 | ee_len = ext4_ext_get_actual_len(ex); |
| 597 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 598 | ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0; |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 599 | es_status = ext4_es_is_unwritten(es) ? 1 : 0; |
| 600 | |
| 601 | /* |
| 602 | * Make sure ex and es are not overlap when we try to insert |
| 603 | * a delayed/hole extent. |
| 604 | */ |
| 605 | if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) { |
| 606 | if (in_range(es->es_lblk, ee_block, ee_len)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 607 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 608 | "inode: %lu we can find an extent " |
| 609 | "at block [%d/%d/%llu/%c], but we " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 610 | "want to add a delayed/hole extent " |
| 611 | "[%d/%d/%llu/%x]\n", |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 612 | inode->i_ino, ee_block, ee_len, |
| 613 | ee_start, ee_status ? 'u' : 'w', |
| 614 | es->es_lblk, es->es_len, |
| 615 | ext4_es_pblock(es), ext4_es_status(es)); |
| 616 | } |
| 617 | goto out; |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * We don't check ee_block == es->es_lblk, etc. because es |
| 622 | * might be a part of whole extent, vice versa. |
| 623 | */ |
| 624 | if (es->es_lblk < ee_block || |
| 625 | ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 626 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 627 | "ex_status [%d/%d/%llu/%c] != " |
| 628 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, |
| 629 | ee_block, ee_len, ee_start, |
| 630 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, |
| 631 | ext4_es_pblock(es), es_status ? 'u' : 'w'); |
| 632 | goto out; |
| 633 | } |
| 634 | |
| 635 | if (ee_status ^ es_status) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 636 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 637 | "ex_status [%d/%d/%llu/%c] != " |
| 638 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, |
| 639 | ee_block, ee_len, ee_start, |
| 640 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, |
| 641 | ext4_es_pblock(es), es_status ? 'u' : 'w'); |
| 642 | } |
| 643 | } else { |
| 644 | /* |
| 645 | * We can't find an extent on disk. So we need to make sure |
| 646 | * that we don't want to add an written/unwritten extent. |
| 647 | */ |
| 648 | if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 649 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 650 | "can't find an extent at block %d but we want " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 651 | "to add a written/unwritten extent " |
| 652 | "[%d/%d/%llu/%x]\n", inode->i_ino, |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 653 | es->es_lblk, es->es_lblk, es->es_len, |
| 654 | ext4_es_pblock(es), ext4_es_status(es)); |
| 655 | } |
| 656 | } |
| 657 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 658 | ext4_ext_drop_refs(path); |
| 659 | kfree(path); |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void ext4_es_insert_extent_ind_check(struct inode *inode, |
| 663 | struct extent_status *es) |
| 664 | { |
| 665 | struct ext4_map_blocks map; |
| 666 | int retval; |
| 667 | |
| 668 | /* |
| 669 | * Here we call ext4_ind_map_blocks to lookup a block mapping because |
| 670 | * 'Indirect' structure is defined in indirect.c. So we couldn't |
| 671 | * access direct/indirect tree from outside. It is too dirty to define |
| 672 | * this function in indirect.c file. |
| 673 | */ |
| 674 | |
| 675 | map.m_lblk = es->es_lblk; |
| 676 | map.m_len = es->es_len; |
| 677 | |
| 678 | retval = ext4_ind_map_blocks(NULL, inode, &map, 0); |
| 679 | if (retval > 0) { |
| 680 | if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) { |
| 681 | /* |
| 682 | * We want to add a delayed/hole extent but this |
| 683 | * block has been allocated. |
| 684 | */ |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 685 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 686 | "We can find blocks but we want to add a " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 687 | "delayed/hole extent [%d/%d/%llu/%x]\n", |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 688 | inode->i_ino, es->es_lblk, es->es_len, |
| 689 | ext4_es_pblock(es), ext4_es_status(es)); |
| 690 | return; |
| 691 | } else if (ext4_es_is_written(es)) { |
| 692 | if (retval != es->es_len) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 693 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 694 | "inode: %lu retval %d != es_len %d\n", |
| 695 | inode->i_ino, retval, es->es_len); |
| 696 | return; |
| 697 | } |
| 698 | if (map.m_pblk != ext4_es_pblock(es)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 699 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 700 | "inode: %lu m_pblk %llu != " |
| 701 | "es_pblk %llu\n", |
| 702 | inode->i_ino, map.m_pblk, |
| 703 | ext4_es_pblock(es)); |
| 704 | return; |
| 705 | } |
| 706 | } else { |
| 707 | /* |
| 708 | * We don't need to check unwritten extent because |
| 709 | * indirect-based file doesn't have it. |
| 710 | */ |
| 711 | BUG_ON(1); |
| 712 | } |
| 713 | } else if (retval == 0) { |
| 714 | if (ext4_es_is_written(es)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 715 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 716 | "We can't find the block but we want to add " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 717 | "a written extent [%d/%d/%llu/%x]\n", |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 718 | inode->i_ino, es->es_lblk, es->es_len, |
| 719 | ext4_es_pblock(es), ext4_es_status(es)); |
| 720 | return; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | static inline void ext4_es_insert_extent_check(struct inode *inode, |
| 726 | struct extent_status *es) |
| 727 | { |
| 728 | /* |
| 729 | * We don't need to worry about the race condition because |
| 730 | * caller takes i_data_sem locking. |
| 731 | */ |
| 732 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); |
| 733 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 734 | ext4_es_insert_extent_ext_check(inode, es); |
| 735 | else |
| 736 | ext4_es_insert_extent_ind_check(inode, es); |
| 737 | } |
| 738 | #else |
| 739 | static inline void ext4_es_insert_extent_check(struct inode *inode, |
| 740 | struct extent_status *es) |
| 741 | { |
| 742 | } |
| 743 | #endif |
| 744 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 745 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 746 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 747 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 748 | struct rb_node **p = &tree->root.rb_node; |
| 749 | struct rb_node *parent = NULL; |
| 750 | struct extent_status *es; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 751 | |
| 752 | while (*p) { |
| 753 | parent = *p; |
| 754 | es = rb_entry(parent, struct extent_status, rb_node); |
| 755 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 756 | if (newes->es_lblk < es->es_lblk) { |
| 757 | if (ext4_es_can_be_merged(newes, es)) { |
| 758 | /* |
| 759 | * Here we can modify es_lblk directly |
| 760 | * because it isn't overlapped. |
| 761 | */ |
| 762 | es->es_lblk = newes->es_lblk; |
| 763 | es->es_len += newes->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 764 | if (ext4_es_is_written(es) || |
| 765 | ext4_es_is_unwritten(es)) |
| 766 | ext4_es_store_pblock(es, |
| 767 | newes->es_pblk); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 768 | es = ext4_es_try_to_merge_left(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 769 | goto out; |
| 770 | } |
| 771 | p = &(*p)->rb_left; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 772 | } else if (newes->es_lblk > ext4_es_end(es)) { |
| 773 | if (ext4_es_can_be_merged(es, newes)) { |
| 774 | es->es_len += newes->es_len; |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 775 | es = ext4_es_try_to_merge_right(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 776 | goto out; |
| 777 | } |
| 778 | p = &(*p)->rb_right; |
| 779 | } else { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 780 | BUG_ON(1); |
| 781 | return -EINVAL; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 785 | es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 786 | newes->es_pblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 787 | if (!es) |
| 788 | return -ENOMEM; |
| 789 | rb_link_node(&es->rb_node, parent, p); |
| 790 | rb_insert_color(&es->rb_node, &tree->root); |
| 791 | |
| 792 | out: |
| 793 | tree->cache_es = es; |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | /* |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 798 | * ext4_es_insert_extent() adds information to an inode's extent |
| 799 | * status tree. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 800 | * |
| 801 | * Return 0 on success, error code on failure. |
| 802 | */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 803 | int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 804 | ext4_lblk_t len, ext4_fsblk_t pblk, |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame] | 805 | unsigned int status) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 806 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 807 | struct extent_status newes; |
| 808 | ext4_lblk_t end = lblk + len - 1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 809 | int err = 0; |
| 810 | |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame] | 811 | es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n", |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 812 | lblk, len, pblk, status, inode->i_ino); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 813 | |
Eryu Guan | d438147 | 2013-02-22 15:27:47 -0500 | [diff] [blame] | 814 | if (!len) |
| 815 | return 0; |
| 816 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 817 | BUG_ON(end < lblk); |
| 818 | |
Lukas Czerner | d2dc317 | 2015-05-02 21:36:55 -0400 | [diff] [blame] | 819 | if ((status & EXTENT_STATUS_DELAYED) && |
| 820 | (status & EXTENT_STATUS_WRITTEN)) { |
| 821 | ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as " |
| 822 | " delayed and written which can potentially " |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 823 | " cause data loss.", lblk, len); |
Lukas Czerner | d2dc317 | 2015-05-02 21:36:55 -0400 | [diff] [blame] | 824 | WARN_ON(1); |
| 825 | } |
| 826 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 827 | newes.es_lblk = lblk; |
| 828 | newes.es_len = len; |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 829 | ext4_es_store_pblock_status(&newes, pblk, status); |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 830 | trace_ext4_es_insert_extent(inode, &newes); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 831 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 832 | ext4_es_insert_extent_check(inode, &newes); |
| 833 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 834 | write_lock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 835 | err = __es_remove_extent(inode, lblk, end); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 836 | if (err != 0) |
| 837 | goto error; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 838 | retry: |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 839 | err = __es_insert_extent(inode, &newes); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 840 | if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb), |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 841 | 128, EXT4_I(inode))) |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 842 | goto retry; |
| 843 | if (err == -ENOMEM && !ext4_es_is_delayed(&newes)) |
| 844 | err = 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 845 | |
| 846 | error: |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 847 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 848 | |
| 849 | ext4_es_print_tree(inode); |
| 850 | |
| 851 | return err; |
| 852 | } |
| 853 | |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 854 | /* |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 855 | * ext4_es_cache_extent() inserts information into the extent status |
| 856 | * tree if and only if there isn't information about the range in |
| 857 | * question already. |
| 858 | */ |
| 859 | void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk, |
| 860 | ext4_lblk_t len, ext4_fsblk_t pblk, |
| 861 | unsigned int status) |
| 862 | { |
| 863 | struct extent_status *es; |
| 864 | struct extent_status newes; |
| 865 | ext4_lblk_t end = lblk + len - 1; |
| 866 | |
| 867 | newes.es_lblk = lblk; |
| 868 | newes.es_len = len; |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 869 | ext4_es_store_pblock_status(&newes, pblk, status); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 870 | trace_ext4_es_cache_extent(inode, &newes); |
| 871 | |
| 872 | if (!len) |
| 873 | return; |
| 874 | |
| 875 | BUG_ON(end < lblk); |
| 876 | |
| 877 | write_lock(&EXT4_I(inode)->i_es_lock); |
| 878 | |
| 879 | es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk); |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 880 | if (!es || es->es_lblk > end) |
| 881 | __es_insert_extent(inode, &newes); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 882 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 883 | } |
| 884 | |
| 885 | /* |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 886 | * ext4_es_lookup_extent() looks up an extent in extent status tree. |
| 887 | * |
| 888 | * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks. |
| 889 | * |
| 890 | * Return: 1 on found, 0 on not |
| 891 | */ |
| 892 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, |
| 893 | struct extent_status *es) |
| 894 | { |
| 895 | struct ext4_es_tree *tree; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 896 | struct ext4_es_stats *stats; |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 897 | struct extent_status *es1 = NULL; |
| 898 | struct rb_node *node; |
| 899 | int found = 0; |
| 900 | |
| 901 | trace_ext4_es_lookup_extent_enter(inode, lblk); |
| 902 | es_debug("lookup extent in block %u\n", lblk); |
| 903 | |
| 904 | tree = &EXT4_I(inode)->i_es_tree; |
| 905 | read_lock(&EXT4_I(inode)->i_es_lock); |
| 906 | |
| 907 | /* find extent in cache firstly */ |
| 908 | es->es_lblk = es->es_len = es->es_pblk = 0; |
| 909 | if (tree->cache_es) { |
| 910 | es1 = tree->cache_es; |
| 911 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
| 912 | es_debug("%u cached by [%u/%u)\n", |
| 913 | lblk, es1->es_lblk, es1->es_len); |
| 914 | found = 1; |
| 915 | goto out; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | node = tree->root.rb_node; |
| 920 | while (node) { |
| 921 | es1 = rb_entry(node, struct extent_status, rb_node); |
| 922 | if (lblk < es1->es_lblk) |
| 923 | node = node->rb_left; |
| 924 | else if (lblk > ext4_es_end(es1)) |
| 925 | node = node->rb_right; |
| 926 | else { |
| 927 | found = 1; |
| 928 | break; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | out: |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 933 | stats = &EXT4_SB(inode->i_sb)->s_es_stats; |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 934 | if (found) { |
| 935 | BUG_ON(!es1); |
| 936 | es->es_lblk = es1->es_lblk; |
| 937 | es->es_len = es1->es_len; |
| 938 | es->es_pblk = es1->es_pblk; |
Jan Kara | 87d8a74 | 2016-03-09 22:26:55 -0500 | [diff] [blame] | 939 | if (!ext4_es_is_referenced(es1)) |
| 940 | ext4_es_set_referenced(es1); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 941 | stats->es_stats_cache_hits++; |
| 942 | } else { |
| 943 | stats->es_stats_cache_misses++; |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | read_unlock(&EXT4_I(inode)->i_es_lock); |
| 947 | |
| 948 | trace_ext4_es_lookup_extent_exit(inode, es, found); |
| 949 | return found; |
| 950 | } |
| 951 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 952 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
| 953 | ext4_lblk_t end) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 954 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 955 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 956 | struct rb_node *node; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 957 | struct extent_status *es; |
| 958 | struct extent_status orig_es; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 959 | ext4_lblk_t len1, len2; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 960 | ext4_fsblk_t block; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 961 | int err; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 962 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 963 | retry: |
| 964 | err = 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 965 | es = __es_tree_search(&tree->root, lblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 966 | if (!es) |
| 967 | goto out; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 968 | if (es->es_lblk > end) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 969 | goto out; |
| 970 | |
| 971 | /* Simply invalidate cache_es. */ |
| 972 | tree->cache_es = NULL; |
| 973 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 974 | orig_es.es_lblk = es->es_lblk; |
| 975 | orig_es.es_len = es->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 976 | orig_es.es_pblk = es->es_pblk; |
| 977 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 978 | len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0; |
| 979 | len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 980 | if (len1 > 0) |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 981 | es->es_len = len1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 982 | if (len2 > 0) { |
| 983 | if (len1 > 0) { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 984 | struct extent_status newes; |
| 985 | |
| 986 | newes.es_lblk = end + 1; |
| 987 | newes.es_len = len2; |
Chen Gang | 666525d | 2014-04-07 10:18:56 -0400 | [diff] [blame] | 988 | block = 0x7FDEADBEEFULL; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 989 | if (ext4_es_is_written(&orig_es) || |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 990 | ext4_es_is_unwritten(&orig_es)) |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 991 | block = ext4_es_pblock(&orig_es) + |
| 992 | orig_es.es_len - len2; |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 993 | ext4_es_store_pblock_status(&newes, block, |
| 994 | ext4_es_status(&orig_es)); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 995 | err = __es_insert_extent(inode, &newes); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 996 | if (err) { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 997 | es->es_lblk = orig_es.es_lblk; |
| 998 | es->es_len = orig_es.es_len; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 999 | if ((err == -ENOMEM) && |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1000 | __es_shrink(EXT4_SB(inode->i_sb), |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1001 | 128, EXT4_I(inode))) |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1002 | goto retry; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1003 | goto out; |
| 1004 | } |
| 1005 | } else { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1006 | es->es_lblk = end + 1; |
| 1007 | es->es_len = len2; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1008 | if (ext4_es_is_written(es) || |
| 1009 | ext4_es_is_unwritten(es)) { |
| 1010 | block = orig_es.es_pblk + orig_es.es_len - len2; |
| 1011 | ext4_es_store_pblock(es, block); |
| 1012 | } |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1013 | } |
| 1014 | goto out; |
| 1015 | } |
| 1016 | |
| 1017 | if (len1 > 0) { |
| 1018 | node = rb_next(&es->rb_node); |
| 1019 | if (node) |
| 1020 | es = rb_entry(node, struct extent_status, rb_node); |
| 1021 | else |
| 1022 | es = NULL; |
| 1023 | } |
| 1024 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1025 | while (es && ext4_es_end(es) <= end) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1026 | node = rb_next(&es->rb_node); |
| 1027 | rb_erase(&es->rb_node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 1028 | ext4_es_free_extent(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1029 | if (!node) { |
| 1030 | es = NULL; |
| 1031 | break; |
| 1032 | } |
| 1033 | es = rb_entry(node, struct extent_status, rb_node); |
| 1034 | } |
| 1035 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1036 | if (es && es->es_lblk < end + 1) { |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1037 | ext4_lblk_t orig_len = es->es_len; |
| 1038 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1039 | len1 = ext4_es_end(es) - end; |
| 1040 | es->es_lblk = end + 1; |
| 1041 | es->es_len = len1; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1042 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
| 1043 | block = es->es_pblk + orig_len - len1; |
| 1044 | ext4_es_store_pblock(es, block); |
| 1045 | } |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | out: |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1049 | return err; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * ext4_es_remove_extent() removes a space from a extent status tree. |
| 1054 | * |
| 1055 | * Return 0 on success, error code on failure. |
| 1056 | */ |
| 1057 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
| 1058 | ext4_lblk_t len) |
| 1059 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1060 | ext4_lblk_t end; |
| 1061 | int err = 0; |
| 1062 | |
| 1063 | trace_ext4_es_remove_extent(inode, lblk, len); |
| 1064 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", |
| 1065 | lblk, len, inode->i_ino); |
| 1066 | |
Eryu Guan | d438147 | 2013-02-22 15:27:47 -0500 | [diff] [blame] | 1067 | if (!len) |
| 1068 | return err; |
| 1069 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1070 | end = lblk + len - 1; |
| 1071 | BUG_ON(end < lblk); |
| 1072 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1073 | /* |
| 1074 | * ext4_clear_inode() depends on us taking i_es_lock unconditionally |
| 1075 | * so that we are sure __es_shrink() is done with the inode before it |
| 1076 | * is reclaimed. |
| 1077 | */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1078 | write_lock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 1079 | err = __es_remove_extent(inode, lblk, end); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1080 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 1081 | ext4_es_print_tree(inode); |
| 1082 | return err; |
| 1083 | } |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1084 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1085 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
| 1086 | struct ext4_inode_info *locked_ei) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1087 | { |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1088 | struct ext4_inode_info *ei; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1089 | struct ext4_es_stats *es_stats; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1090 | ktime_t start_time; |
| 1091 | u64 scan_time; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1092 | int nr_to_walk; |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1093 | int nr_shrunk = 0; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1094 | int retried = 0, nr_skipped = 0; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1095 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1096 | es_stats = &sbi->s_es_stats; |
| 1097 | start_time = ktime_get(); |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1098 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1099 | retry: |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1100 | spin_lock(&sbi->s_es_lock); |
| 1101 | nr_to_walk = sbi->s_es_nr_inode; |
| 1102 | while (nr_to_walk-- > 0) { |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1103 | if (list_empty(&sbi->s_es_list)) { |
| 1104 | spin_unlock(&sbi->s_es_lock); |
| 1105 | goto out; |
| 1106 | } |
| 1107 | ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info, |
| 1108 | i_es_list); |
| 1109 | /* Move the inode to the tail */ |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1110 | list_move_tail(&ei->i_es_list, &sbi->s_es_list); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1111 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1112 | /* |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1113 | * Normally we try hard to avoid shrinking precached inodes, |
| 1114 | * but we will as a last resort. |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1115 | */ |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1116 | if (!retried && ext4_test_inode_state(&ei->vfs_inode, |
| 1117 | EXT4_STATE_EXT_PRECACHED)) { |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1118 | nr_skipped++; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1119 | continue; |
| 1120 | } |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1121 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1122 | if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) { |
| 1123 | nr_skipped++; |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1124 | continue; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1125 | } |
| 1126 | /* |
| 1127 | * Now we hold i_es_lock which protects us from inode reclaim |
| 1128 | * freeing inode under us |
| 1129 | */ |
| 1130 | spin_unlock(&sbi->s_es_lock); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1131 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1132 | nr_shrunk += es_reclaim_extents(ei, &nr_to_scan); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1133 | write_unlock(&ei->i_es_lock); |
| 1134 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1135 | if (nr_to_scan <= 0) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1136 | goto out; |
| 1137 | spin_lock(&sbi->s_es_lock); |
| 1138 | } |
| 1139 | spin_unlock(&sbi->s_es_lock); |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1140 | |
| 1141 | /* |
| 1142 | * If we skipped any inodes, and we weren't able to make any |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1143 | * forward progress, try again to scan precached inodes. |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1144 | */ |
| 1145 | if ((nr_shrunk == 0) && nr_skipped && !retried) { |
| 1146 | retried++; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1147 | goto retry; |
| 1148 | } |
| 1149 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1150 | if (locked_ei && nr_shrunk == 0) |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1151 | nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1152 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1153 | out: |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1154 | scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time)); |
| 1155 | if (likely(es_stats->es_stats_scan_time)) |
| 1156 | es_stats->es_stats_scan_time = (scan_time + |
| 1157 | es_stats->es_stats_scan_time*3) / 4; |
| 1158 | else |
| 1159 | es_stats->es_stats_scan_time = scan_time; |
| 1160 | if (scan_time > es_stats->es_stats_max_scan_time) |
| 1161 | es_stats->es_stats_max_scan_time = scan_time; |
| 1162 | if (likely(es_stats->es_stats_shrunk)) |
| 1163 | es_stats->es_stats_shrunk = (nr_shrunk + |
| 1164 | es_stats->es_stats_shrunk*3) / 4; |
| 1165 | else |
| 1166 | es_stats->es_stats_shrunk = nr_shrunk; |
| 1167 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1168 | trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time, |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1169 | nr_skipped, retried); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1170 | return nr_shrunk; |
| 1171 | } |
| 1172 | |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1173 | static unsigned long ext4_es_count(struct shrinker *shrink, |
| 1174 | struct shrink_control *sc) |
| 1175 | { |
| 1176 | unsigned long nr; |
| 1177 | struct ext4_sb_info *sbi; |
| 1178 | |
| 1179 | sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1180 | nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt); |
Zheng Liu | e963bb1 | 2014-09-01 22:22:13 -0400 | [diff] [blame] | 1181 | trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr); |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1182 | return nr; |
| 1183 | } |
| 1184 | |
| 1185 | static unsigned long ext4_es_scan(struct shrinker *shrink, |
| 1186 | struct shrink_control *sc) |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1187 | { |
| 1188 | struct ext4_sb_info *sbi = container_of(shrink, |
| 1189 | struct ext4_sb_info, s_es_shrinker); |
| 1190 | int nr_to_scan = sc->nr_to_scan; |
| 1191 | int ret, nr_shrunk; |
| 1192 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1193 | ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt); |
Zheng Liu | e963bb1 | 2014-09-01 22:22:13 -0400 | [diff] [blame] | 1194 | trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1195 | |
| 1196 | if (!nr_to_scan) |
| 1197 | return ret; |
| 1198 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1199 | nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1200 | |
Zheng Liu | e963bb1 | 2014-09-01 22:22:13 -0400 | [diff] [blame] | 1201 | trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret); |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1202 | return nr_shrunk; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1203 | } |
| 1204 | |
Theodore Ts'o | ebd173b | 2015-09-23 12:46:17 -0400 | [diff] [blame] | 1205 | int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1206 | { |
Theodore Ts'o | ebd173b | 2015-09-23 12:46:17 -0400 | [diff] [blame] | 1207 | struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1208 | struct ext4_es_stats *es_stats = &sbi->s_es_stats; |
| 1209 | struct ext4_inode_info *ei, *max = NULL; |
| 1210 | unsigned int inode_cnt = 0; |
| 1211 | |
| 1212 | if (v != SEQ_START_TOKEN) |
| 1213 | return 0; |
| 1214 | |
| 1215 | /* here we just find an inode that has the max nr. of objects */ |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1216 | spin_lock(&sbi->s_es_lock); |
| 1217 | list_for_each_entry(ei, &sbi->s_es_list, i_es_list) { |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1218 | inode_cnt++; |
| 1219 | if (max && max->i_es_all_nr < ei->i_es_all_nr) |
| 1220 | max = ei; |
| 1221 | else if (!max) |
| 1222 | max = ei; |
| 1223 | } |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1224 | spin_unlock(&sbi->s_es_lock); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1225 | |
| 1226 | seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n", |
| 1227 | percpu_counter_sum_positive(&es_stats->es_stats_all_cnt), |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1228 | percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt)); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1229 | seq_printf(seq, " %lu/%lu cache hits/misses\n", |
| 1230 | es_stats->es_stats_cache_hits, |
| 1231 | es_stats->es_stats_cache_misses); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1232 | if (inode_cnt) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1233 | seq_printf(seq, " %d inodes on list\n", inode_cnt); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1234 | |
| 1235 | seq_printf(seq, "average:\n %llu us scan time\n", |
| 1236 | div_u64(es_stats->es_stats_scan_time, 1000)); |
| 1237 | seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk); |
| 1238 | if (inode_cnt) |
| 1239 | seq_printf(seq, |
| 1240 | "maximum:\n %lu inode (%u objects, %u reclaimable)\n" |
| 1241 | " %llu us max scan time\n", |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1242 | max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr, |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1243 | div_u64(es_stats->es_stats_max_scan_time, 1000)); |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1248 | int ext4_es_register_shrinker(struct ext4_sb_info *sbi) |
| 1249 | { |
| 1250 | int err; |
| 1251 | |
Jan Kara | 624d0f1 | 2014-11-25 11:53:47 -0500 | [diff] [blame] | 1252 | /* Make sure we have enough bits for physical block number */ |
| 1253 | BUILD_BUG_ON(ES_SHIFT < 48); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1254 | INIT_LIST_HEAD(&sbi->s_es_list); |
| 1255 | sbi->s_es_nr_inode = 0; |
| 1256 | spin_lock_init(&sbi->s_es_lock); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1257 | sbi->s_es_stats.es_stats_shrunk = 0; |
| 1258 | sbi->s_es_stats.es_stats_cache_hits = 0; |
| 1259 | sbi->s_es_stats.es_stats_cache_misses = 0; |
| 1260 | sbi->s_es_stats.es_stats_scan_time = 0; |
| 1261 | sbi->s_es_stats.es_stats_max_scan_time = 0; |
Linus Torvalds | c2661b8 | 2014-10-20 09:50:11 -0700 | [diff] [blame] | 1262 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1263 | if (err) |
| 1264 | return err; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1265 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1266 | if (err) |
| 1267 | goto err1; |
| 1268 | |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1269 | sbi->s_es_shrinker.scan_objects = ext4_es_scan; |
| 1270 | sbi->s_es_shrinker.count_objects = ext4_es_count; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1271 | sbi->s_es_shrinker.seeks = DEFAULT_SEEKS; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1272 | err = register_shrinker(&sbi->s_es_shrinker); |
| 1273 | if (err) |
| 1274 | goto err2; |
| 1275 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1276 | return 0; |
| 1277 | |
| 1278 | err2: |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1279 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1280 | err1: |
| 1281 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); |
| 1282 | return err; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1283 | } |
| 1284 | |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1285 | void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1286 | { |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1287 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1288 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1289 | unregister_shrinker(&sbi->s_es_shrinker); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1290 | } |
| 1291 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1292 | /* |
| 1293 | * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at |
| 1294 | * most *nr_to_scan extents, update *nr_to_scan accordingly. |
| 1295 | * |
| 1296 | * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan. |
| 1297 | * Increment *nr_shrunk by the number of reclaimed extents. Also update |
| 1298 | * ei->i_es_shrink_lblk to where we should continue scanning. |
| 1299 | */ |
| 1300 | static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end, |
| 1301 | int *nr_to_scan, int *nr_shrunk) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1302 | { |
| 1303 | struct inode *inode = &ei->vfs_inode; |
| 1304 | struct ext4_es_tree *tree = &ei->i_es_tree; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1305 | struct extent_status *es; |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1306 | struct rb_node *node; |
| 1307 | |
| 1308 | es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk); |
| 1309 | if (!es) |
| 1310 | goto out_wrap; |
| 1311 | node = &es->rb_node; |
| 1312 | while (*nr_to_scan > 0) { |
| 1313 | if (es->es_lblk > end) { |
| 1314 | ei->i_es_shrink_lblk = end + 1; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | (*nr_to_scan)--; |
| 1319 | node = rb_next(&es->rb_node); |
| 1320 | /* |
| 1321 | * We can't reclaim delayed extent from status tree because |
| 1322 | * fiemap, bigallic, and seek_data/hole need to use it. |
| 1323 | */ |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 1324 | if (ext4_es_is_delayed(es)) |
| 1325 | goto next; |
| 1326 | if (ext4_es_is_referenced(es)) { |
| 1327 | ext4_es_clear_referenced(es); |
| 1328 | goto next; |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1329 | } |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 1330 | |
| 1331 | rb_erase(&es->rb_node, &tree->root); |
| 1332 | ext4_es_free_extent(inode, es); |
| 1333 | (*nr_shrunk)++; |
| 1334 | next: |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1335 | if (!node) |
| 1336 | goto out_wrap; |
| 1337 | es = rb_entry(node, struct extent_status, rb_node); |
| 1338 | } |
| 1339 | ei->i_es_shrink_lblk = es->es_lblk; |
| 1340 | return 1; |
| 1341 | out_wrap: |
| 1342 | ei->i_es_shrink_lblk = 0; |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan) |
| 1347 | { |
| 1348 | struct inode *inode = &ei->vfs_inode; |
| 1349 | int nr_shrunk = 0; |
| 1350 | ext4_lblk_t start = ei->i_es_shrink_lblk; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1351 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 1352 | DEFAULT_RATELIMIT_BURST); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1353 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1354 | if (ei->i_es_shk_nr == 0) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1355 | return 0; |
| 1356 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1357 | if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) && |
| 1358 | __ratelimit(&_rs)) |
| 1359 | ext4_warning(inode->i_sb, "forced shrink of precached extents"); |
| 1360 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1361 | if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) && |
| 1362 | start != 0) |
| 1363 | es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk); |
| 1364 | |
| 1365 | ei->i_es_tree.cache_es = NULL; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1366 | return nr_shrunk; |
| 1367 | } |