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; |
Eric Whitney | 1dc0aa4 | 2018-10-01 14:17:41 -0400 | [diff] [blame^] | 145 | static struct kmem_cache *ext4_pending_cachep; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 146 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 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, |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 149 | ext4_lblk_t end); |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 150 | 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] | 151 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
| 152 | struct ext4_inode_info *locked_ei); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 153 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 154 | int __init ext4_init_es(void) |
| 155 | { |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 156 | ext4_es_cachep = kmem_cache_create("ext4_extent_status", |
| 157 | sizeof(struct extent_status), |
| 158 | 0, (SLAB_RECLAIM_ACCOUNT), NULL); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 159 | if (ext4_es_cachep == NULL) |
| 160 | return -ENOMEM; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | void ext4_exit_es(void) |
| 165 | { |
Sean Fu | 21c580d | 2018-05-20 22:44:13 -0400 | [diff] [blame] | 166 | kmem_cache_destroy(ext4_es_cachep); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void ext4_es_init_tree(struct ext4_es_tree *tree) |
| 170 | { |
| 171 | tree->root = RB_ROOT; |
| 172 | tree->cache_es = NULL; |
| 173 | } |
| 174 | |
| 175 | #ifdef ES_DEBUG__ |
| 176 | static void ext4_es_print_tree(struct inode *inode) |
| 177 | { |
| 178 | struct ext4_es_tree *tree; |
| 179 | struct rb_node *node; |
| 180 | |
| 181 | printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino); |
| 182 | tree = &EXT4_I(inode)->i_es_tree; |
| 183 | node = rb_first(&tree->root); |
| 184 | while (node) { |
| 185 | struct extent_status *es; |
| 186 | es = rb_entry(node, struct extent_status, rb_node); |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 187 | printk(KERN_DEBUG " [%u/%u) %llu %x", |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 188 | es->es_lblk, es->es_len, |
| 189 | ext4_es_pblock(es), ext4_es_status(es)); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 190 | node = rb_next(node); |
| 191 | } |
| 192 | printk(KERN_DEBUG "\n"); |
| 193 | } |
| 194 | #else |
| 195 | #define ext4_es_print_tree(inode) |
| 196 | #endif |
| 197 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 198 | static inline ext4_lblk_t ext4_es_end(struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 199 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 200 | BUG_ON(es->es_lblk + es->es_len < es->es_lblk); |
| 201 | return es->es_lblk + es->es_len - 1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * search through the tree for an delayed extent with a given offset. If |
| 206 | * it can't be found, try to find next extent. |
| 207 | */ |
| 208 | static struct extent_status *__es_tree_search(struct rb_root *root, |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 209 | ext4_lblk_t lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 210 | { |
| 211 | struct rb_node *node = root->rb_node; |
| 212 | struct extent_status *es = NULL; |
| 213 | |
| 214 | while (node) { |
| 215 | es = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 216 | if (lblk < es->es_lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 217 | node = node->rb_left; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 218 | else if (lblk > ext4_es_end(es)) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 219 | node = node->rb_right; |
| 220 | else |
| 221 | return es; |
| 222 | } |
| 223 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 224 | if (es && lblk < es->es_lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 225 | return es; |
| 226 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 227 | if (es && lblk > ext4_es_end(es)) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 228 | node = rb_next(&es->rb_node); |
| 229 | return node ? rb_entry(node, struct extent_status, rb_node) : |
| 230 | NULL; |
| 231 | } |
| 232 | |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | /* |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 237 | * ext4_es_find_extent_range - find extent with specified status within block |
| 238 | * range or next extent following block range in |
| 239 | * extents status tree |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 240 | * |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 241 | * @inode - file containing the range |
| 242 | * @matching_fn - pointer to function that matches extents with desired status |
| 243 | * @lblk - logical block defining start of range |
| 244 | * @end - logical block defining end of range |
| 245 | * @es - extent found, if any |
| 246 | * |
| 247 | * Find the first extent within the block range specified by @lblk and @end |
| 248 | * in the extents status tree that satisfies @matching_fn. If a match |
| 249 | * is found, it's returned in @es. If not, and a matching extent is found |
| 250 | * beyond the block range, it's returned in @es. If no match is found, an |
| 251 | * extent is returned in @es whose es_lblk, es_len, and es_pblk components |
| 252 | * are 0. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 253 | */ |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 254 | static void __es_find_extent_range(struct inode *inode, |
| 255 | int (*matching_fn)(struct extent_status *es), |
| 256 | ext4_lblk_t lblk, ext4_lblk_t end, |
| 257 | struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 258 | { |
| 259 | struct ext4_es_tree *tree = NULL; |
| 260 | struct extent_status *es1 = NULL; |
| 261 | struct rb_node *node; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 262 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 263 | WARN_ON(es == NULL); |
| 264 | WARN_ON(end < lblk); |
Zheng Liu | 992e9fd | 2012-11-08 21:57:33 -0500 | [diff] [blame] | 265 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 266 | tree = &EXT4_I(inode)->i_es_tree; |
| 267 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 268 | /* see if the extent has been cached */ |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 269 | es->es_lblk = es->es_len = es->es_pblk = 0; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 270 | if (tree->cache_es) { |
| 271 | es1 = tree->cache_es; |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 272 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame] | 273 | es_debug("%u cached by [%u/%u) %llu %x\n", |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 274 | lblk, es1->es_lblk, es1->es_len, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 275 | ext4_es_pblock(es1), ext4_es_status(es1)); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 276 | goto out; |
| 277 | } |
| 278 | } |
| 279 | |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 280 | es1 = __es_tree_search(&tree->root, lblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 281 | |
| 282 | out: |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 283 | if (es1 && !matching_fn(es1)) { |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 284 | while ((node = rb_next(&es1->rb_node)) != NULL) { |
| 285 | es1 = rb_entry(node, struct extent_status, rb_node); |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 286 | if (es1->es_lblk > end) { |
| 287 | es1 = NULL; |
| 288 | break; |
| 289 | } |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 290 | if (matching_fn(es1)) |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 295 | if (es1 && matching_fn(es1)) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 296 | tree->cache_es = es1; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 297 | es->es_lblk = es1->es_lblk; |
| 298 | es->es_len = es1->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 299 | es->es_pblk = es1->es_pblk; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 300 | } |
| 301 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 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); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 316 | read_unlock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | 992e9fd | 2012-11-08 21:57:33 -0500 | [diff] [blame] | 317 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 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; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 412 | static void ext4_es_list_add(struct inode *inode) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 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 | |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 428 | static void ext4_es_list_del(struct inode *inode) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 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 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 442 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 443 | ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len, |
| 444 | ext4_fsblk_t pblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 445 | { |
| 446 | struct extent_status *es; |
| 447 | es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC); |
| 448 | if (es == NULL) |
| 449 | return NULL; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 450 | es->es_lblk = lblk; |
| 451 | es->es_len = len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 452 | es->es_pblk = pblk; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * We don't count delayed extent because we never try to reclaim them |
| 456 | */ |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 457 | if (!ext4_es_is_delayed(es)) { |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 458 | if (!EXT4_I(inode)->i_es_shk_nr++) |
| 459 | ext4_es_list_add(inode); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 460 | percpu_counter_inc(&EXT4_SB(inode->i_sb)-> |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 461 | s_es_stats.es_stats_shk_cnt); |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 462 | } |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 463 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 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 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 467 | return es; |
| 468 | } |
| 469 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 470 | 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] | 471 | { |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 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 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 475 | /* Decrease the shrink counter when this es is not delayed */ |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 476 | if (!ext4_es_is_delayed(es)) { |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 477 | BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0); |
Jan Kara | b0dea4c | 2014-11-25 11:49:25 -0500 | [diff] [blame] | 478 | if (!--EXT4_I(inode)->i_es_shk_nr) |
| 479 | ext4_es_list_del(inode); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 480 | percpu_counter_dec(&EXT4_SB(inode->i_sb)-> |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 481 | s_es_stats.es_stats_shk_cnt); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 484 | kmem_cache_free(ext4_es_cachep, es); |
| 485 | } |
| 486 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 487 | /* |
| 488 | * Check whether or not two extents can be merged |
| 489 | * Condition: |
| 490 | * - logical block number is contiguous |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 491 | * - physical block number is contiguous |
| 492 | * - status is equal |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 493 | */ |
| 494 | static int ext4_es_can_be_merged(struct extent_status *es1, |
| 495 | struct extent_status *es2) |
| 496 | { |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 497 | if (ext4_es_type(es1) != ext4_es_type(es2)) |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 498 | return 0; |
| 499 | |
Lukas Czerner | 0baaea6 | 2014-05-12 22:21:43 -0400 | [diff] [blame] | 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); |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 506 | return 0; |
Lukas Czerner | 0baaea6 | 2014-05-12 22:21:43 -0400 | [diff] [blame] | 507 | } |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 508 | |
Zheng Liu | bd38436 | 2013-03-10 20:48:59 -0400 | [diff] [blame] | 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; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 526 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 527 | 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] | 528 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 529 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 538 | if (ext4_es_can_be_merged(es1, es)) { |
| 539 | es1->es_len += es->es_len; |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 540 | if (ext4_es_is_referenced(es)) |
| 541 | ext4_es_set_referenced(es1); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 542 | rb_erase(&es->rb_node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 543 | ext4_es_free_extent(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 544 | es = es1; |
| 545 | } |
| 546 | |
| 547 | return es; |
| 548 | } |
| 549 | |
| 550 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 551 | 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] | 552 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 553 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 562 | if (ext4_es_can_be_merged(es, es1)) { |
| 563 | es->es_len += es1->es_len; |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 564 | if (ext4_es_is_referenced(es1)) |
| 565 | ext4_es_set_referenced(es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 566 | rb_erase(node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 567 | ext4_es_free_extent(inode, es1); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | return es; |
| 571 | } |
| 572 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 573 | #ifdef ES_AGGRESSIVE_TEST |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 574 | #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */ |
| 575 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 586 | path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE); |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 599 | ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0; |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 608 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 609 | "inode: %lu we can find an extent " |
| 610 | "at block [%d/%d/%llu/%c], but we " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 611 | "want to add a delayed/hole extent " |
| 612 | "[%d/%d/%llu/%x]\n", |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 627 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 637 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 650 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 651 | "can't find an extent at block %d but we want " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 652 | "to add a written/unwritten extent " |
| 653 | "[%d/%d/%llu/%x]\n", inode->i_ino, |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 654 | es->es_lblk, es->es_lblk, es->es_len, |
| 655 | ext4_es_pblock(es), ext4_es_status(es)); |
| 656 | } |
| 657 | } |
| 658 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 659 | ext4_ext_drop_refs(path); |
| 660 | kfree(path); |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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 | */ |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 686 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 687 | "We can find blocks but we want to add a " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 688 | "delayed/hole extent [%d/%d/%llu/%x]\n", |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 694 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 700 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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_ON(1); |
| 713 | } |
| 714 | } else if (retval == 0) { |
| 715 | if (ext4_es_is_written(es)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 716 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 717 | "We can't find the block but we want to add " |
Eric Whitney | ce140cd | 2014-02-20 16:09:12 -0500 | [diff] [blame] | 718 | "a written extent [%d/%d/%llu/%x]\n", |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 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 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 746 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 747 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 748 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 749 | struct rb_node **p = &tree->root.rb_node; |
| 750 | struct rb_node *parent = NULL; |
| 751 | struct extent_status *es; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 752 | |
| 753 | while (*p) { |
| 754 | parent = *p; |
| 755 | es = rb_entry(parent, struct extent_status, rb_node); |
| 756 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 765 | if (ext4_es_is_written(es) || |
| 766 | ext4_es_is_unwritten(es)) |
| 767 | ext4_es_store_pblock(es, |
| 768 | newes->es_pblk); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 769 | es = ext4_es_try_to_merge_left(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 770 | goto out; |
| 771 | } |
| 772 | p = &(*p)->rb_left; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 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; |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 776 | es = ext4_es_try_to_merge_right(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 777 | goto out; |
| 778 | } |
| 779 | p = &(*p)->rb_right; |
| 780 | } else { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 781 | BUG_ON(1); |
| 782 | return -EINVAL; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 786 | es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 787 | newes->es_pblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 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 | /* |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 799 | * ext4_es_insert_extent() adds information to an inode's extent |
| 800 | * status tree. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 801 | * |
| 802 | * Return 0 on success, error code on failure. |
| 803 | */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 804 | int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 805 | ext4_lblk_t len, ext4_fsblk_t pblk, |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame] | 806 | unsigned int status) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 807 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 808 | struct extent_status newes; |
| 809 | ext4_lblk_t end = lblk + len - 1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 810 | int err = 0; |
| 811 | |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame] | 812 | 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] | 813 | lblk, len, pblk, status, inode->i_ino); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 814 | |
Eryu Guan | d438147 | 2013-02-22 15:27:47 -0500 | [diff] [blame] | 815 | if (!len) |
| 816 | return 0; |
| 817 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 818 | BUG_ON(end < lblk); |
| 819 | |
Lukas Czerner | d2dc317 | 2015-05-02 21:36:55 -0400 | [diff] [blame] | 820 | if ((status & EXTENT_STATUS_DELAYED) && |
| 821 | (status & EXTENT_STATUS_WRITTEN)) { |
| 822 | ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as " |
| 823 | " delayed and written which can potentially " |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 824 | " cause data loss.", lblk, len); |
Lukas Czerner | d2dc317 | 2015-05-02 21:36:55 -0400 | [diff] [blame] | 825 | WARN_ON(1); |
| 826 | } |
| 827 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 828 | newes.es_lblk = lblk; |
| 829 | newes.es_len = len; |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 830 | ext4_es_store_pblock_status(&newes, pblk, status); |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 831 | trace_ext4_es_insert_extent(inode, &newes); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 832 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 833 | ext4_es_insert_extent_check(inode, &newes); |
| 834 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 835 | write_lock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 836 | err = __es_remove_extent(inode, lblk, end); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 837 | if (err != 0) |
| 838 | goto error; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 839 | retry: |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 840 | err = __es_insert_extent(inode, &newes); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 841 | if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb), |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 842 | 128, EXT4_I(inode))) |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 843 | goto retry; |
| 844 | if (err == -ENOMEM && !ext4_es_is_delayed(&newes)) |
| 845 | err = 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 846 | |
| 847 | error: |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 848 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 849 | |
| 850 | ext4_es_print_tree(inode); |
| 851 | |
| 852 | return err; |
| 853 | } |
| 854 | |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 855 | /* |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 856 | * ext4_es_cache_extent() inserts information into the extent status |
| 857 | * tree if and only if there isn't information about the range in |
| 858 | * question already. |
| 859 | */ |
| 860 | void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk, |
| 861 | ext4_lblk_t len, ext4_fsblk_t pblk, |
| 862 | unsigned int status) |
| 863 | { |
| 864 | struct extent_status *es; |
| 865 | struct extent_status newes; |
| 866 | ext4_lblk_t end = lblk + len - 1; |
| 867 | |
| 868 | newes.es_lblk = lblk; |
| 869 | newes.es_len = len; |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 870 | ext4_es_store_pblock_status(&newes, pblk, status); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 871 | trace_ext4_es_cache_extent(inode, &newes); |
| 872 | |
| 873 | if (!len) |
| 874 | return; |
| 875 | |
| 876 | BUG_ON(end < lblk); |
| 877 | |
| 878 | write_lock(&EXT4_I(inode)->i_es_lock); |
| 879 | |
| 880 | es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk); |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 881 | if (!es || es->es_lblk > end) |
| 882 | __es_insert_extent(inode, &newes); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 883 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 884 | } |
| 885 | |
| 886 | /* |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 887 | * ext4_es_lookup_extent() looks up an extent in extent status tree. |
| 888 | * |
| 889 | * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks. |
| 890 | * |
| 891 | * Return: 1 on found, 0 on not |
| 892 | */ |
| 893 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, |
| 894 | struct extent_status *es) |
| 895 | { |
| 896 | struct ext4_es_tree *tree; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 897 | struct ext4_es_stats *stats; |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 898 | struct extent_status *es1 = NULL; |
| 899 | struct rb_node *node; |
| 900 | int found = 0; |
| 901 | |
| 902 | trace_ext4_es_lookup_extent_enter(inode, lblk); |
| 903 | es_debug("lookup extent in block %u\n", lblk); |
| 904 | |
| 905 | tree = &EXT4_I(inode)->i_es_tree; |
| 906 | read_lock(&EXT4_I(inode)->i_es_lock); |
| 907 | |
| 908 | /* find extent in cache firstly */ |
| 909 | es->es_lblk = es->es_len = es->es_pblk = 0; |
| 910 | if (tree->cache_es) { |
| 911 | es1 = tree->cache_es; |
| 912 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
| 913 | es_debug("%u cached by [%u/%u)\n", |
| 914 | lblk, es1->es_lblk, es1->es_len); |
| 915 | found = 1; |
| 916 | goto out; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | node = tree->root.rb_node; |
| 921 | while (node) { |
| 922 | es1 = rb_entry(node, struct extent_status, rb_node); |
| 923 | if (lblk < es1->es_lblk) |
| 924 | node = node->rb_left; |
| 925 | else if (lblk > ext4_es_end(es1)) |
| 926 | node = node->rb_right; |
| 927 | else { |
| 928 | found = 1; |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | out: |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 934 | stats = &EXT4_SB(inode->i_sb)->s_es_stats; |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 935 | if (found) { |
| 936 | BUG_ON(!es1); |
| 937 | es->es_lblk = es1->es_lblk; |
| 938 | es->es_len = es1->es_len; |
| 939 | es->es_pblk = es1->es_pblk; |
Jan Kara | 87d8a74 | 2016-03-09 22:26:55 -0500 | [diff] [blame] | 940 | if (!ext4_es_is_referenced(es1)) |
| 941 | ext4_es_set_referenced(es1); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 942 | stats->es_stats_cache_hits++; |
| 943 | } else { |
| 944 | stats->es_stats_cache_misses++; |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | read_unlock(&EXT4_I(inode)->i_es_lock); |
| 948 | |
| 949 | trace_ext4_es_lookup_extent_exit(inode, es, found); |
| 950 | return found; |
| 951 | } |
| 952 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 953 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
| 954 | ext4_lblk_t end) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 955 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 956 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 957 | struct rb_node *node; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 958 | struct extent_status *es; |
| 959 | struct extent_status orig_es; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 960 | ext4_lblk_t len1, len2; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 961 | ext4_fsblk_t block; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 962 | int err; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 963 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 964 | retry: |
| 965 | err = 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 966 | es = __es_tree_search(&tree->root, lblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 967 | if (!es) |
| 968 | goto out; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 969 | if (es->es_lblk > end) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 970 | goto out; |
| 971 | |
| 972 | /* Simply invalidate cache_es. */ |
| 973 | tree->cache_es = NULL; |
| 974 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 975 | orig_es.es_lblk = es->es_lblk; |
| 976 | orig_es.es_len = es->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 977 | orig_es.es_pblk = es->es_pblk; |
| 978 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 979 | len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0; |
| 980 | len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 981 | if (len1 > 0) |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 982 | es->es_len = len1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 983 | if (len2 > 0) { |
| 984 | if (len1 > 0) { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 985 | struct extent_status newes; |
| 986 | |
| 987 | newes.es_lblk = end + 1; |
| 988 | newes.es_len = len2; |
Chen Gang | 666525d | 2014-04-07 10:18:56 -0400 | [diff] [blame] | 989 | block = 0x7FDEADBEEFULL; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 990 | if (ext4_es_is_written(&orig_es) || |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 991 | ext4_es_is_unwritten(&orig_es)) |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 992 | block = ext4_es_pblock(&orig_es) + |
| 993 | orig_es.es_len - len2; |
Theodore Ts'o | 9a6633b | 2014-02-19 20:15:15 -0500 | [diff] [blame] | 994 | ext4_es_store_pblock_status(&newes, block, |
| 995 | ext4_es_status(&orig_es)); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 996 | err = __es_insert_extent(inode, &newes); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 997 | if (err) { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 998 | es->es_lblk = orig_es.es_lblk; |
| 999 | es->es_len = orig_es.es_len; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1000 | if ((err == -ENOMEM) && |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1001 | __es_shrink(EXT4_SB(inode->i_sb), |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1002 | 128, EXT4_I(inode))) |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1003 | goto retry; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1004 | goto out; |
| 1005 | } |
| 1006 | } else { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1007 | es->es_lblk = end + 1; |
| 1008 | es->es_len = len2; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1009 | if (ext4_es_is_written(es) || |
| 1010 | ext4_es_is_unwritten(es)) { |
| 1011 | block = orig_es.es_pblk + orig_es.es_len - len2; |
| 1012 | ext4_es_store_pblock(es, block); |
| 1013 | } |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1014 | } |
| 1015 | goto out; |
| 1016 | } |
| 1017 | |
| 1018 | if (len1 > 0) { |
| 1019 | node = rb_next(&es->rb_node); |
| 1020 | if (node) |
| 1021 | es = rb_entry(node, struct extent_status, rb_node); |
| 1022 | else |
| 1023 | es = NULL; |
| 1024 | } |
| 1025 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1026 | while (es && ext4_es_end(es) <= end) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1027 | node = rb_next(&es->rb_node); |
| 1028 | rb_erase(&es->rb_node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 1029 | ext4_es_free_extent(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1030 | if (!node) { |
| 1031 | es = NULL; |
| 1032 | break; |
| 1033 | } |
| 1034 | es = rb_entry(node, struct extent_status, rb_node); |
| 1035 | } |
| 1036 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1037 | if (es && es->es_lblk < end + 1) { |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1038 | ext4_lblk_t orig_len = es->es_len; |
| 1039 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1040 | len1 = ext4_es_end(es) - end; |
| 1041 | es->es_lblk = end + 1; |
| 1042 | es->es_len = len1; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1043 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
| 1044 | block = es->es_pblk + orig_len - len1; |
| 1045 | ext4_es_store_pblock(es, block); |
| 1046 | } |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | out: |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1050 | return err; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * ext4_es_remove_extent() removes a space from a extent status tree. |
| 1055 | * |
| 1056 | * Return 0 on success, error code on failure. |
| 1057 | */ |
| 1058 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
| 1059 | ext4_lblk_t len) |
| 1060 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1061 | ext4_lblk_t end; |
| 1062 | int err = 0; |
| 1063 | |
| 1064 | trace_ext4_es_remove_extent(inode, lblk, len); |
| 1065 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", |
| 1066 | lblk, len, inode->i_ino); |
| 1067 | |
Eryu Guan | d438147 | 2013-02-22 15:27:47 -0500 | [diff] [blame] | 1068 | if (!len) |
| 1069 | return err; |
| 1070 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1071 | end = lblk + len - 1; |
| 1072 | BUG_ON(end < lblk); |
| 1073 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1074 | /* |
| 1075 | * ext4_clear_inode() depends on us taking i_es_lock unconditionally |
| 1076 | * so that we are sure __es_shrink() is done with the inode before it |
| 1077 | * is reclaimed. |
| 1078 | */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 1079 | write_lock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 1080 | err = __es_remove_extent(inode, lblk, end); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1081 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 1082 | ext4_es_print_tree(inode); |
| 1083 | return err; |
| 1084 | } |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1085 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1086 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
| 1087 | struct ext4_inode_info *locked_ei) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1088 | { |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1089 | struct ext4_inode_info *ei; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1090 | struct ext4_es_stats *es_stats; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1091 | ktime_t start_time; |
| 1092 | u64 scan_time; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1093 | int nr_to_walk; |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1094 | int nr_shrunk = 0; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1095 | int retried = 0, nr_skipped = 0; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1096 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1097 | es_stats = &sbi->s_es_stats; |
| 1098 | start_time = ktime_get(); |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1099 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1100 | retry: |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1101 | spin_lock(&sbi->s_es_lock); |
| 1102 | nr_to_walk = sbi->s_es_nr_inode; |
| 1103 | while (nr_to_walk-- > 0) { |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1104 | if (list_empty(&sbi->s_es_list)) { |
| 1105 | spin_unlock(&sbi->s_es_lock); |
| 1106 | goto out; |
| 1107 | } |
| 1108 | ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info, |
| 1109 | i_es_list); |
| 1110 | /* Move the inode to the tail */ |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1111 | list_move_tail(&ei->i_es_list, &sbi->s_es_list); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1112 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1113 | /* |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1114 | * Normally we try hard to avoid shrinking precached inodes, |
| 1115 | * but we will as a last resort. |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1116 | */ |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1117 | if (!retried && ext4_test_inode_state(&ei->vfs_inode, |
| 1118 | EXT4_STATE_EXT_PRECACHED)) { |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1119 | nr_skipped++; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1120 | continue; |
| 1121 | } |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1122 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1123 | if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) { |
| 1124 | nr_skipped++; |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1125 | continue; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1126 | } |
| 1127 | /* |
| 1128 | * Now we hold i_es_lock which protects us from inode reclaim |
| 1129 | * freeing inode under us |
| 1130 | */ |
| 1131 | spin_unlock(&sbi->s_es_lock); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1132 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1133 | nr_shrunk += es_reclaim_extents(ei, &nr_to_scan); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1134 | write_unlock(&ei->i_es_lock); |
| 1135 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1136 | if (nr_to_scan <= 0) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1137 | goto out; |
| 1138 | spin_lock(&sbi->s_es_lock); |
| 1139 | } |
| 1140 | spin_unlock(&sbi->s_es_lock); |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1141 | |
| 1142 | /* |
| 1143 | * 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] | 1144 | * forward progress, try again to scan precached inodes. |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1145 | */ |
| 1146 | if ((nr_shrunk == 0) && nr_skipped && !retried) { |
| 1147 | retried++; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1148 | goto retry; |
| 1149 | } |
| 1150 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1151 | if (locked_ei && nr_shrunk == 0) |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1152 | nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1153 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1154 | out: |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1155 | scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time)); |
| 1156 | if (likely(es_stats->es_stats_scan_time)) |
| 1157 | es_stats->es_stats_scan_time = (scan_time + |
| 1158 | es_stats->es_stats_scan_time*3) / 4; |
| 1159 | else |
| 1160 | es_stats->es_stats_scan_time = scan_time; |
| 1161 | if (scan_time > es_stats->es_stats_max_scan_time) |
| 1162 | es_stats->es_stats_max_scan_time = scan_time; |
| 1163 | if (likely(es_stats->es_stats_shrunk)) |
| 1164 | es_stats->es_stats_shrunk = (nr_shrunk + |
| 1165 | es_stats->es_stats_shrunk*3) / 4; |
| 1166 | else |
| 1167 | es_stats->es_stats_shrunk = nr_shrunk; |
| 1168 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1169 | trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time, |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1170 | nr_skipped, retried); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1171 | return nr_shrunk; |
| 1172 | } |
| 1173 | |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1174 | static unsigned long ext4_es_count(struct shrinker *shrink, |
| 1175 | struct shrink_control *sc) |
| 1176 | { |
| 1177 | unsigned long nr; |
| 1178 | struct ext4_sb_info *sbi; |
| 1179 | |
| 1180 | sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1181 | 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] | 1182 | 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] | 1183 | return nr; |
| 1184 | } |
| 1185 | |
| 1186 | static unsigned long ext4_es_scan(struct shrinker *shrink, |
| 1187 | struct shrink_control *sc) |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1188 | { |
| 1189 | struct ext4_sb_info *sbi = container_of(shrink, |
| 1190 | struct ext4_sb_info, s_es_shrinker); |
| 1191 | int nr_to_scan = sc->nr_to_scan; |
| 1192 | int ret, nr_shrunk; |
| 1193 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1194 | 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] | 1195 | 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] | 1196 | |
| 1197 | if (!nr_to_scan) |
| 1198 | return ret; |
| 1199 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1200 | nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 1201 | |
Zheng Liu | e963bb1 | 2014-09-01 22:22:13 -0400 | [diff] [blame] | 1202 | trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret); |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1203 | return nr_shrunk; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1204 | } |
| 1205 | |
Theodore Ts'o | ebd173b | 2015-09-23 12:46:17 -0400 | [diff] [blame] | 1206 | 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] | 1207 | { |
Theodore Ts'o | ebd173b | 2015-09-23 12:46:17 -0400 | [diff] [blame] | 1208 | struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1209 | struct ext4_es_stats *es_stats = &sbi->s_es_stats; |
| 1210 | struct ext4_inode_info *ei, *max = NULL; |
| 1211 | unsigned int inode_cnt = 0; |
| 1212 | |
| 1213 | if (v != SEQ_START_TOKEN) |
| 1214 | return 0; |
| 1215 | |
| 1216 | /* here we just find an inode that has the max nr. of objects */ |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1217 | spin_lock(&sbi->s_es_lock); |
| 1218 | list_for_each_entry(ei, &sbi->s_es_list, i_es_list) { |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1219 | inode_cnt++; |
| 1220 | if (max && max->i_es_all_nr < ei->i_es_all_nr) |
| 1221 | max = ei; |
| 1222 | else if (!max) |
| 1223 | max = ei; |
| 1224 | } |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1225 | spin_unlock(&sbi->s_es_lock); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1226 | |
| 1227 | seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n", |
| 1228 | percpu_counter_sum_positive(&es_stats->es_stats_all_cnt), |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1229 | percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt)); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1230 | seq_printf(seq, " %lu/%lu cache hits/misses\n", |
| 1231 | es_stats->es_stats_cache_hits, |
| 1232 | es_stats->es_stats_cache_misses); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1233 | if (inode_cnt) |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1234 | seq_printf(seq, " %d inodes on list\n", inode_cnt); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1235 | |
| 1236 | seq_printf(seq, "average:\n %llu us scan time\n", |
| 1237 | div_u64(es_stats->es_stats_scan_time, 1000)); |
| 1238 | seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk); |
| 1239 | if (inode_cnt) |
| 1240 | seq_printf(seq, |
| 1241 | "maximum:\n %lu inode (%u objects, %u reclaimable)\n" |
| 1242 | " %llu us max scan time\n", |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1243 | 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] | 1244 | div_u64(es_stats->es_stats_max_scan_time, 1000)); |
| 1245 | |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1249 | int ext4_es_register_shrinker(struct ext4_sb_info *sbi) |
| 1250 | { |
| 1251 | int err; |
| 1252 | |
Jan Kara | 624d0f1 | 2014-11-25 11:53:47 -0500 | [diff] [blame] | 1253 | /* Make sure we have enough bits for physical block number */ |
| 1254 | BUILD_BUG_ON(ES_SHIFT < 48); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1255 | INIT_LIST_HEAD(&sbi->s_es_list); |
| 1256 | sbi->s_es_nr_inode = 0; |
| 1257 | spin_lock_init(&sbi->s_es_lock); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1258 | sbi->s_es_stats.es_stats_shrunk = 0; |
| 1259 | sbi->s_es_stats.es_stats_cache_hits = 0; |
| 1260 | sbi->s_es_stats.es_stats_cache_misses = 0; |
| 1261 | sbi->s_es_stats.es_stats_scan_time = 0; |
| 1262 | sbi->s_es_stats.es_stats_max_scan_time = 0; |
Linus Torvalds | c2661b8 | 2014-10-20 09:50:11 -0700 | [diff] [blame] | 1263 | 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] | 1264 | if (err) |
| 1265 | return err; |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1266 | 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] | 1267 | if (err) |
| 1268 | goto err1; |
| 1269 | |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 1270 | sbi->s_es_shrinker.scan_objects = ext4_es_scan; |
| 1271 | sbi->s_es_shrinker.count_objects = ext4_es_count; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1272 | sbi->s_es_shrinker.seeks = DEFAULT_SEEKS; |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1273 | err = register_shrinker(&sbi->s_es_shrinker); |
| 1274 | if (err) |
| 1275 | goto err2; |
| 1276 | |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1277 | return 0; |
| 1278 | |
| 1279 | err2: |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1280 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1281 | err1: |
| 1282 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); |
| 1283 | return err; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1284 | } |
| 1285 | |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1286 | void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1287 | { |
Zheng Liu | eb68d0e | 2014-09-01 22:26:49 -0400 | [diff] [blame] | 1288 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1289 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1290 | unregister_shrinker(&sbi->s_es_shrinker); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1291 | } |
| 1292 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1293 | /* |
| 1294 | * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at |
| 1295 | * most *nr_to_scan extents, update *nr_to_scan accordingly. |
| 1296 | * |
| 1297 | * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan. |
| 1298 | * Increment *nr_shrunk by the number of reclaimed extents. Also update |
| 1299 | * ei->i_es_shrink_lblk to where we should continue scanning. |
| 1300 | */ |
| 1301 | static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end, |
| 1302 | int *nr_to_scan, int *nr_shrunk) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1303 | { |
| 1304 | struct inode *inode = &ei->vfs_inode; |
| 1305 | struct ext4_es_tree *tree = &ei->i_es_tree; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1306 | struct extent_status *es; |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1307 | struct rb_node *node; |
| 1308 | |
| 1309 | es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk); |
| 1310 | if (!es) |
| 1311 | goto out_wrap; |
| 1312 | node = &es->rb_node; |
| 1313 | while (*nr_to_scan > 0) { |
| 1314 | if (es->es_lblk > end) { |
| 1315 | ei->i_es_shrink_lblk = end + 1; |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | (*nr_to_scan)--; |
| 1320 | node = rb_next(&es->rb_node); |
| 1321 | /* |
| 1322 | * We can't reclaim delayed extent from status tree because |
| 1323 | * fiemap, bigallic, and seek_data/hole need to use it. |
| 1324 | */ |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 1325 | if (ext4_es_is_delayed(es)) |
| 1326 | goto next; |
| 1327 | if (ext4_es_is_referenced(es)) { |
| 1328 | ext4_es_clear_referenced(es); |
| 1329 | goto next; |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1330 | } |
Jan Kara | 2be12de | 2014-11-25 11:55:24 -0500 | [diff] [blame] | 1331 | |
| 1332 | rb_erase(&es->rb_node, &tree->root); |
| 1333 | ext4_es_free_extent(inode, es); |
| 1334 | (*nr_shrunk)++; |
| 1335 | next: |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1336 | if (!node) |
| 1337 | goto out_wrap; |
| 1338 | es = rb_entry(node, struct extent_status, rb_node); |
| 1339 | } |
| 1340 | ei->i_es_shrink_lblk = es->es_lblk; |
| 1341 | return 1; |
| 1342 | out_wrap: |
| 1343 | ei->i_es_shrink_lblk = 0; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan) |
| 1348 | { |
| 1349 | struct inode *inode = &ei->vfs_inode; |
| 1350 | int nr_shrunk = 0; |
| 1351 | ext4_lblk_t start = ei->i_es_shrink_lblk; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1352 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 1353 | DEFAULT_RATELIMIT_BURST); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1354 | |
Zheng Liu | edaa53c | 2014-11-25 11:45:37 -0500 | [diff] [blame] | 1355 | if (ei->i_es_shk_nr == 0) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1356 | return 0; |
| 1357 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1358 | if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) && |
| 1359 | __ratelimit(&_rs)) |
| 1360 | ext4_warning(inode->i_sb, "forced shrink of precached extents"); |
| 1361 | |
Jan Kara | dd47592 | 2014-11-25 11:51:23 -0500 | [diff] [blame] | 1362 | if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) && |
| 1363 | start != 0) |
| 1364 | es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk); |
| 1365 | |
| 1366 | ei->i_es_tree.cache_es = NULL; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1367 | return nr_shrunk; |
| 1368 | } |
Eric Whitney | 1dc0aa4 | 2018-10-01 14:17:41 -0400 | [diff] [blame^] | 1369 | |
| 1370 | #ifdef ES_DEBUG__ |
| 1371 | static void ext4_print_pending_tree(struct inode *inode) |
| 1372 | { |
| 1373 | struct ext4_pending_tree *tree; |
| 1374 | struct rb_node *node; |
| 1375 | struct pending_reservation *pr; |
| 1376 | |
| 1377 | printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino); |
| 1378 | tree = &EXT4_I(inode)->i_pending_tree; |
| 1379 | node = rb_first(&tree->root); |
| 1380 | while (node) { |
| 1381 | pr = rb_entry(node, struct pending_reservation, rb_node); |
| 1382 | printk(KERN_DEBUG " %u", pr->lclu); |
| 1383 | node = rb_next(node); |
| 1384 | } |
| 1385 | printk(KERN_DEBUG "\n"); |
| 1386 | } |
| 1387 | #else |
| 1388 | #define ext4_print_pending_tree(inode) |
| 1389 | #endif |
| 1390 | |
| 1391 | int __init ext4_init_pending(void) |
| 1392 | { |
| 1393 | ext4_pending_cachep = kmem_cache_create("ext4_pending_reservation", |
| 1394 | sizeof(struct pending_reservation), |
| 1395 | 0, (SLAB_RECLAIM_ACCOUNT), NULL); |
| 1396 | if (ext4_pending_cachep == NULL) |
| 1397 | return -ENOMEM; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | void ext4_exit_pending(void) |
| 1402 | { |
| 1403 | kmem_cache_destroy(ext4_pending_cachep); |
| 1404 | } |
| 1405 | |
| 1406 | void ext4_init_pending_tree(struct ext4_pending_tree *tree) |
| 1407 | { |
| 1408 | tree->root = RB_ROOT; |
| 1409 | } |
| 1410 | |
| 1411 | /* |
| 1412 | * __get_pending - retrieve a pointer to a pending reservation |
| 1413 | * |
| 1414 | * @inode - file containing the pending cluster reservation |
| 1415 | * @lclu - logical cluster of interest |
| 1416 | * |
| 1417 | * Returns a pointer to a pending reservation if it's a member of |
| 1418 | * the set, and NULL if not. Must be called holding i_es_lock. |
| 1419 | */ |
| 1420 | static struct pending_reservation *__get_pending(struct inode *inode, |
| 1421 | ext4_lblk_t lclu) |
| 1422 | { |
| 1423 | struct ext4_pending_tree *tree; |
| 1424 | struct rb_node *node; |
| 1425 | struct pending_reservation *pr = NULL; |
| 1426 | |
| 1427 | tree = &EXT4_I(inode)->i_pending_tree; |
| 1428 | node = (&tree->root)->rb_node; |
| 1429 | |
| 1430 | while (node) { |
| 1431 | pr = rb_entry(node, struct pending_reservation, rb_node); |
| 1432 | if (lclu < pr->lclu) |
| 1433 | node = node->rb_left; |
| 1434 | else if (lclu > pr->lclu) |
| 1435 | node = node->rb_right; |
| 1436 | else if (lclu == pr->lclu) |
| 1437 | return pr; |
| 1438 | } |
| 1439 | return NULL; |
| 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * __insert_pending - adds a pending cluster reservation to the set of |
| 1444 | * pending reservations |
| 1445 | * |
| 1446 | * @inode - file containing the cluster |
| 1447 | * @lblk - logical block in the cluster to be added |
| 1448 | * |
| 1449 | * Returns 0 on successful insertion and -ENOMEM on failure. If the |
| 1450 | * pending reservation is already in the set, returns successfully. |
| 1451 | */ |
| 1452 | static int __insert_pending(struct inode *inode, ext4_lblk_t lblk) |
| 1453 | { |
| 1454 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1455 | struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree; |
| 1456 | struct rb_node **p = &tree->root.rb_node; |
| 1457 | struct rb_node *parent = NULL; |
| 1458 | struct pending_reservation *pr; |
| 1459 | ext4_lblk_t lclu; |
| 1460 | int ret = 0; |
| 1461 | |
| 1462 | lclu = EXT4_B2C(sbi, lblk); |
| 1463 | /* search to find parent for insertion */ |
| 1464 | while (*p) { |
| 1465 | parent = *p; |
| 1466 | pr = rb_entry(parent, struct pending_reservation, rb_node); |
| 1467 | |
| 1468 | if (lclu < pr->lclu) { |
| 1469 | p = &(*p)->rb_left; |
| 1470 | } else if (lclu > pr->lclu) { |
| 1471 | p = &(*p)->rb_right; |
| 1472 | } else { |
| 1473 | /* pending reservation already inserted */ |
| 1474 | goto out; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | pr = kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC); |
| 1479 | if (pr == NULL) { |
| 1480 | ret = -ENOMEM; |
| 1481 | goto out; |
| 1482 | } |
| 1483 | pr->lclu = lclu; |
| 1484 | |
| 1485 | rb_link_node(&pr->rb_node, parent, p); |
| 1486 | rb_insert_color(&pr->rb_node, &tree->root); |
| 1487 | |
| 1488 | out: |
| 1489 | return ret; |
| 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * __remove_pending - removes a pending cluster reservation from the set |
| 1494 | * of pending reservations |
| 1495 | * |
| 1496 | * @inode - file containing the cluster |
| 1497 | * @lblk - logical block in the pending cluster reservation to be removed |
| 1498 | * |
| 1499 | * Returns successfully if pending reservation is not a member of the set. |
| 1500 | */ |
| 1501 | static void __remove_pending(struct inode *inode, ext4_lblk_t lblk) |
| 1502 | { |
| 1503 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1504 | struct pending_reservation *pr; |
| 1505 | struct ext4_pending_tree *tree; |
| 1506 | |
| 1507 | pr = __get_pending(inode, EXT4_B2C(sbi, lblk)); |
| 1508 | if (pr != NULL) { |
| 1509 | tree = &EXT4_I(inode)->i_pending_tree; |
| 1510 | rb_erase(&pr->rb_node, &tree->root); |
| 1511 | kmem_cache_free(ext4_pending_cachep, pr); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | * ext4_remove_pending - removes a pending cluster reservation from the set |
| 1517 | * of pending reservations |
| 1518 | * |
| 1519 | * @inode - file containing the cluster |
| 1520 | * @lblk - logical block in the pending cluster reservation to be removed |
| 1521 | * |
| 1522 | * Locking for external use of __remove_pending. |
| 1523 | */ |
| 1524 | void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk) |
| 1525 | { |
| 1526 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 1527 | |
| 1528 | write_lock(&ei->i_es_lock); |
| 1529 | __remove_pending(inode, lblk); |
| 1530 | write_unlock(&ei->i_es_lock); |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * ext4_is_pending - determine whether a cluster has a pending reservation |
| 1535 | * on it |
| 1536 | * |
| 1537 | * @inode - file containing the cluster |
| 1538 | * @lblk - logical block in the cluster |
| 1539 | * |
| 1540 | * Returns true if there's a pending reservation for the cluster in the |
| 1541 | * set of pending reservations, and false if not. |
| 1542 | */ |
| 1543 | bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk) |
| 1544 | { |
| 1545 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1546 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 1547 | bool ret; |
| 1548 | |
| 1549 | read_lock(&ei->i_es_lock); |
| 1550 | ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL); |
| 1551 | read_unlock(&ei->i_es_lock); |
| 1552 | |
| 1553 | return ret; |
| 1554 | } |