Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * fs/ext4/extents_status.c |
| 3 | * |
| 4 | * Written by Yongqiang Yang <xiaoqiangnk@gmail.com> |
| 5 | * Modified by |
| 6 | * Allison Henderson <achender@linux.vnet.ibm.com> |
| 7 | * Hugh Dickins <hughd@google.com> |
| 8 | * Zheng Liu <wenqing.lz@taobao.com> |
| 9 | * |
| 10 | * Ext4 extents status tree core functions. |
| 11 | */ |
| 12 | #include <linux/rbtree.h> |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 13 | #include <linux/list_sort.h> |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 14 | #include "ext4.h" |
| 15 | #include "extents_status.h" |
| 16 | #include "ext4_extents.h" |
| 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 |
| 88 | * time comsuming. |
| 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); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 149 | static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei, |
| 150 | int nr_to_scan); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 151 | static int __ext4_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 | { |
| 166 | if (ext4_es_cachep) |
| 167 | kmem_cache_destroy(ext4_es_cachep); |
| 168 | } |
| 169 | |
| 170 | void ext4_es_init_tree(struct ext4_es_tree *tree) |
| 171 | { |
| 172 | tree->root = RB_ROOT; |
| 173 | tree->cache_es = NULL; |
| 174 | } |
| 175 | |
| 176 | #ifdef ES_DEBUG__ |
| 177 | static void ext4_es_print_tree(struct inode *inode) |
| 178 | { |
| 179 | struct ext4_es_tree *tree; |
| 180 | struct rb_node *node; |
| 181 | |
| 182 | printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino); |
| 183 | tree = &EXT4_I(inode)->i_es_tree; |
| 184 | node = rb_first(&tree->root); |
| 185 | while (node) { |
| 186 | struct extent_status *es; |
| 187 | es = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 188 | printk(KERN_DEBUG " [%u/%u) %llu %llx", |
| 189 | es->es_lblk, es->es_len, |
| 190 | ext4_es_pblock(es), ext4_es_status(es)); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 191 | node = rb_next(node); |
| 192 | } |
| 193 | printk(KERN_DEBUG "\n"); |
| 194 | } |
| 195 | #else |
| 196 | #define ext4_es_print_tree(inode) |
| 197 | #endif |
| 198 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 199 | static inline ext4_lblk_t ext4_es_end(struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 200 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 201 | BUG_ON(es->es_lblk + es->es_len < es->es_lblk); |
| 202 | return es->es_lblk + es->es_len - 1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * search through the tree for an delayed extent with a given offset. If |
| 207 | * it can't be found, try to find next extent. |
| 208 | */ |
| 209 | static struct extent_status *__es_tree_search(struct rb_root *root, |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 210 | ext4_lblk_t lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 211 | { |
| 212 | struct rb_node *node = root->rb_node; |
| 213 | struct extent_status *es = NULL; |
| 214 | |
| 215 | while (node) { |
| 216 | es = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 217 | if (lblk < es->es_lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 218 | node = node->rb_left; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 219 | else if (lblk > ext4_es_end(es)) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 220 | node = node->rb_right; |
| 221 | else |
| 222 | return es; |
| 223 | } |
| 224 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 225 | if (es && lblk < es->es_lblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 226 | return es; |
| 227 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 228 | if (es && lblk > ext4_es_end(es)) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 229 | node = rb_next(&es->rb_node); |
| 230 | return node ? rb_entry(node, struct extent_status, rb_node) : |
| 231 | NULL; |
| 232 | } |
| 233 | |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | /* |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 238 | * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering |
| 239 | * @es->lblk if it exists, otherwise, the next extent after @es->lblk. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 240 | * |
| 241 | * @inode: the inode which owns delayed extents |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 242 | * @lblk: the offset where we start to search |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 243 | * @end: the offset where we stop to search |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 244 | * @es: delayed extent that we found |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 245 | */ |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 246 | void ext4_es_find_delayed_extent_range(struct inode *inode, |
| 247 | ext4_lblk_t lblk, ext4_lblk_t end, |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 248 | struct extent_status *es) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 249 | { |
| 250 | struct ext4_es_tree *tree = NULL; |
| 251 | struct extent_status *es1 = NULL; |
| 252 | struct rb_node *node; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 253 | |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 254 | BUG_ON(es == NULL); |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 255 | BUG_ON(end < lblk); |
| 256 | trace_ext4_es_find_delayed_extent_range_enter(inode, lblk); |
Zheng Liu | 992e9fd | 2012-11-08 21:57:33 -0500 | [diff] [blame] | 257 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 258 | read_lock(&EXT4_I(inode)->i_es_lock); |
| 259 | tree = &EXT4_I(inode)->i_es_tree; |
| 260 | |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 261 | /* find extent in cache firstly */ |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 262 | es->es_lblk = es->es_len = es->es_pblk = 0; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 263 | if (tree->cache_es) { |
| 264 | es1 = tree->cache_es; |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 265 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame^] | 266 | es_debug("%u cached by [%u/%u) %llu %x\n", |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 267 | lblk, es1->es_lblk, es1->es_len, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 268 | ext4_es_pblock(es1), ext4_es_status(es1)); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 269 | goto out; |
| 270 | } |
| 271 | } |
| 272 | |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 273 | es1 = __es_tree_search(&tree->root, lblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 274 | |
| 275 | out: |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 276 | if (es1 && !ext4_es_is_delayed(es1)) { |
| 277 | while ((node = rb_next(&es1->rb_node)) != NULL) { |
| 278 | es1 = rb_entry(node, struct extent_status, rb_node); |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 279 | if (es1->es_lblk > end) { |
| 280 | es1 = NULL; |
| 281 | break; |
| 282 | } |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 283 | if (ext4_es_is_delayed(es1)) |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (es1 && ext4_es_is_delayed(es1)) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 289 | tree->cache_es = es1; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 290 | es->es_lblk = es1->es_lblk; |
| 291 | es->es_len = es1->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 292 | es->es_pblk = es1->es_pblk; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | read_unlock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | 992e9fd | 2012-11-08 21:57:33 -0500 | [diff] [blame] | 296 | |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 297 | trace_ext4_es_find_delayed_extent_range_exit(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 301 | ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len, |
| 302 | ext4_fsblk_t pblk) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 303 | { |
| 304 | struct extent_status *es; |
| 305 | es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC); |
| 306 | if (es == NULL) |
| 307 | return NULL; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 308 | es->es_lblk = lblk; |
| 309 | es->es_len = len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 310 | es->es_pblk = pblk; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * We don't count delayed extent because we never try to reclaim them |
| 314 | */ |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 315 | if (!ext4_es_is_delayed(es)) { |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 316 | EXT4_I(inode)->i_es_lru_nr++; |
Theodore Ts'o | 1ac6466 | 2013-03-02 10:27:46 -0500 | [diff] [blame] | 317 | percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt); |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 318 | } |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 319 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 320 | return es; |
| 321 | } |
| 322 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 323 | 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] | 324 | { |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 325 | /* Decrease the lru counter when this es is not delayed */ |
| 326 | if (!ext4_es_is_delayed(es)) { |
| 327 | BUG_ON(EXT4_I(inode)->i_es_lru_nr == 0); |
| 328 | EXT4_I(inode)->i_es_lru_nr--; |
Theodore Ts'o | 1ac6466 | 2013-03-02 10:27:46 -0500 | [diff] [blame] | 329 | percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_extent_cache_cnt); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 332 | kmem_cache_free(ext4_es_cachep, es); |
| 333 | } |
| 334 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 335 | /* |
| 336 | * Check whether or not two extents can be merged |
| 337 | * Condition: |
| 338 | * - logical block number is contiguous |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 339 | * - physical block number is contiguous |
| 340 | * - status is equal |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 341 | */ |
| 342 | static int ext4_es_can_be_merged(struct extent_status *es1, |
| 343 | struct extent_status *es2) |
| 344 | { |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 345 | if (ext4_es_status(es1) != ext4_es_status(es2)) |
| 346 | return 0; |
| 347 | |
Zheng Liu | bd38436 | 2013-03-10 20:48:59 -0400 | [diff] [blame] | 348 | if (((__u64) es1->es_len) + es2->es_len > 0xFFFFFFFFULL) |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 349 | return 0; |
| 350 | |
Zheng Liu | bd38436 | 2013-03-10 20:48:59 -0400 | [diff] [blame] | 351 | if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk) |
| 352 | return 0; |
| 353 | |
| 354 | if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) && |
| 355 | (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2))) |
| 356 | return 1; |
| 357 | |
| 358 | if (ext4_es_is_hole(es1)) |
| 359 | return 1; |
| 360 | |
| 361 | /* we need to check delayed extent is without unwritten status */ |
| 362 | if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1)) |
| 363 | return 1; |
| 364 | |
| 365 | return 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 368 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 369 | 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] | 370 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 371 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 372 | struct extent_status *es1; |
| 373 | struct rb_node *node; |
| 374 | |
| 375 | node = rb_prev(&es->rb_node); |
| 376 | if (!node) |
| 377 | return es; |
| 378 | |
| 379 | es1 = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 380 | if (ext4_es_can_be_merged(es1, es)) { |
| 381 | es1->es_len += es->es_len; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 382 | rb_erase(&es->rb_node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 383 | ext4_es_free_extent(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 384 | es = es1; |
| 385 | } |
| 386 | |
| 387 | return es; |
| 388 | } |
| 389 | |
| 390 | static struct extent_status * |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 391 | 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] | 392 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 393 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 394 | struct extent_status *es1; |
| 395 | struct rb_node *node; |
| 396 | |
| 397 | node = rb_next(&es->rb_node); |
| 398 | if (!node) |
| 399 | return es; |
| 400 | |
| 401 | es1 = rb_entry(node, struct extent_status, rb_node); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 402 | if (ext4_es_can_be_merged(es, es1)) { |
| 403 | es->es_len += es1->es_len; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 404 | rb_erase(node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 405 | ext4_es_free_extent(inode, es1); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | return es; |
| 409 | } |
| 410 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 411 | #ifdef ES_AGGRESSIVE_TEST |
| 412 | static void ext4_es_insert_extent_ext_check(struct inode *inode, |
| 413 | struct extent_status *es) |
| 414 | { |
| 415 | struct ext4_ext_path *path = NULL; |
| 416 | struct ext4_extent *ex; |
| 417 | ext4_lblk_t ee_block; |
| 418 | ext4_fsblk_t ee_start; |
| 419 | unsigned short ee_len; |
| 420 | int depth, ee_status, es_status; |
| 421 | |
| 422 | path = ext4_ext_find_extent(inode, es->es_lblk, NULL); |
| 423 | if (IS_ERR(path)) |
| 424 | return; |
| 425 | |
| 426 | depth = ext_depth(inode); |
| 427 | ex = path[depth].p_ext; |
| 428 | |
| 429 | if (ex) { |
| 430 | |
| 431 | ee_block = le32_to_cpu(ex->ee_block); |
| 432 | ee_start = ext4_ext_pblock(ex); |
| 433 | ee_len = ext4_ext_get_actual_len(ex); |
| 434 | |
| 435 | ee_status = ext4_ext_is_uninitialized(ex) ? 1 : 0; |
| 436 | es_status = ext4_es_is_unwritten(es) ? 1 : 0; |
| 437 | |
| 438 | /* |
| 439 | * Make sure ex and es are not overlap when we try to insert |
| 440 | * a delayed/hole extent. |
| 441 | */ |
| 442 | if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) { |
| 443 | if (in_range(es->es_lblk, ee_block, ee_len)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 444 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 445 | "inode: %lu we can find an extent " |
| 446 | "at block [%d/%d/%llu/%c], but we " |
| 447 | "want to add an delayed/hole extent " |
| 448 | "[%d/%d/%llu/%llx]\n", |
| 449 | inode->i_ino, ee_block, ee_len, |
| 450 | ee_start, ee_status ? 'u' : 'w', |
| 451 | es->es_lblk, es->es_len, |
| 452 | ext4_es_pblock(es), ext4_es_status(es)); |
| 453 | } |
| 454 | goto out; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * We don't check ee_block == es->es_lblk, etc. because es |
| 459 | * might be a part of whole extent, vice versa. |
| 460 | */ |
| 461 | if (es->es_lblk < ee_block || |
| 462 | ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 463 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 464 | "ex_status [%d/%d/%llu/%c] != " |
| 465 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, |
| 466 | ee_block, ee_len, ee_start, |
| 467 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, |
| 468 | ext4_es_pblock(es), es_status ? 'u' : 'w'); |
| 469 | goto out; |
| 470 | } |
| 471 | |
| 472 | if (ee_status ^ es_status) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 473 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 474 | "ex_status [%d/%d/%llu/%c] != " |
| 475 | "es_status [%d/%d/%llu/%c]\n", inode->i_ino, |
| 476 | ee_block, ee_len, ee_start, |
| 477 | ee_status ? 'u' : 'w', es->es_lblk, es->es_len, |
| 478 | ext4_es_pblock(es), es_status ? 'u' : 'w'); |
| 479 | } |
| 480 | } else { |
| 481 | /* |
| 482 | * We can't find an extent on disk. So we need to make sure |
| 483 | * that we don't want to add an written/unwritten extent. |
| 484 | */ |
| 485 | if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 486 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 487 | "can't find an extent at block %d but we want " |
| 488 | "to add an written/unwritten extent " |
| 489 | "[%d/%d/%llu/%llx]\n", inode->i_ino, |
| 490 | es->es_lblk, es->es_lblk, es->es_len, |
| 491 | ext4_es_pblock(es), ext4_es_status(es)); |
| 492 | } |
| 493 | } |
| 494 | out: |
| 495 | if (path) { |
| 496 | ext4_ext_drop_refs(path); |
| 497 | kfree(path); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | static void ext4_es_insert_extent_ind_check(struct inode *inode, |
| 502 | struct extent_status *es) |
| 503 | { |
| 504 | struct ext4_map_blocks map; |
| 505 | int retval; |
| 506 | |
| 507 | /* |
| 508 | * Here we call ext4_ind_map_blocks to lookup a block mapping because |
| 509 | * 'Indirect' structure is defined in indirect.c. So we couldn't |
| 510 | * access direct/indirect tree from outside. It is too dirty to define |
| 511 | * this function in indirect.c file. |
| 512 | */ |
| 513 | |
| 514 | map.m_lblk = es->es_lblk; |
| 515 | map.m_len = es->es_len; |
| 516 | |
| 517 | retval = ext4_ind_map_blocks(NULL, inode, &map, 0); |
| 518 | if (retval > 0) { |
| 519 | if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) { |
| 520 | /* |
| 521 | * We want to add a delayed/hole extent but this |
| 522 | * block has been allocated. |
| 523 | */ |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 524 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 525 | "We can find blocks but we want to add a " |
| 526 | "delayed/hole extent [%d/%d/%llu/%llx]\n", |
| 527 | inode->i_ino, es->es_lblk, es->es_len, |
| 528 | ext4_es_pblock(es), ext4_es_status(es)); |
| 529 | return; |
| 530 | } else if (ext4_es_is_written(es)) { |
| 531 | if (retval != es->es_len) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 532 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 533 | "inode: %lu retval %d != es_len %d\n", |
| 534 | inode->i_ino, retval, es->es_len); |
| 535 | return; |
| 536 | } |
| 537 | if (map.m_pblk != ext4_es_pblock(es)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 538 | pr_warn("ES insert assertion failed for " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 539 | "inode: %lu m_pblk %llu != " |
| 540 | "es_pblk %llu\n", |
| 541 | inode->i_ino, map.m_pblk, |
| 542 | ext4_es_pblock(es)); |
| 543 | return; |
| 544 | } |
| 545 | } else { |
| 546 | /* |
| 547 | * We don't need to check unwritten extent because |
| 548 | * indirect-based file doesn't have it. |
| 549 | */ |
| 550 | BUG_ON(1); |
| 551 | } |
| 552 | } else if (retval == 0) { |
| 553 | if (ext4_es_is_written(es)) { |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 554 | pr_warn("ES insert assertion failed for inode: %lu " |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 555 | "We can't find the block but we want to add " |
| 556 | "an written extent [%d/%d/%llu/%llx]\n", |
| 557 | inode->i_ino, es->es_lblk, es->es_len, |
| 558 | ext4_es_pblock(es), ext4_es_status(es)); |
| 559 | return; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | static inline void ext4_es_insert_extent_check(struct inode *inode, |
| 565 | struct extent_status *es) |
| 566 | { |
| 567 | /* |
| 568 | * We don't need to worry about the race condition because |
| 569 | * caller takes i_data_sem locking. |
| 570 | */ |
| 571 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); |
| 572 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 573 | ext4_es_insert_extent_ext_check(inode, es); |
| 574 | else |
| 575 | ext4_es_insert_extent_ind_check(inode, es); |
| 576 | } |
| 577 | #else |
| 578 | static inline void ext4_es_insert_extent_check(struct inode *inode, |
| 579 | struct extent_status *es) |
| 580 | { |
| 581 | } |
| 582 | #endif |
| 583 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 584 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 585 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 586 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 587 | struct rb_node **p = &tree->root.rb_node; |
| 588 | struct rb_node *parent = NULL; |
| 589 | struct extent_status *es; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 590 | |
| 591 | while (*p) { |
| 592 | parent = *p; |
| 593 | es = rb_entry(parent, struct extent_status, rb_node); |
| 594 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 595 | if (newes->es_lblk < es->es_lblk) { |
| 596 | if (ext4_es_can_be_merged(newes, es)) { |
| 597 | /* |
| 598 | * Here we can modify es_lblk directly |
| 599 | * because it isn't overlapped. |
| 600 | */ |
| 601 | es->es_lblk = newes->es_lblk; |
| 602 | es->es_len += newes->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 603 | if (ext4_es_is_written(es) || |
| 604 | ext4_es_is_unwritten(es)) |
| 605 | ext4_es_store_pblock(es, |
| 606 | newes->es_pblk); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 607 | es = ext4_es_try_to_merge_left(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 608 | goto out; |
| 609 | } |
| 610 | p = &(*p)->rb_left; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 611 | } else if (newes->es_lblk > ext4_es_end(es)) { |
| 612 | if (ext4_es_can_be_merged(es, newes)) { |
| 613 | es->es_len += newes->es_len; |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 614 | es = ext4_es_try_to_merge_right(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 615 | goto out; |
| 616 | } |
| 617 | p = &(*p)->rb_right; |
| 618 | } else { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 619 | BUG_ON(1); |
| 620 | return -EINVAL; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 624 | es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 625 | newes->es_pblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 626 | if (!es) |
| 627 | return -ENOMEM; |
| 628 | rb_link_node(&es->rb_node, parent, p); |
| 629 | rb_insert_color(&es->rb_node, &tree->root); |
| 630 | |
| 631 | out: |
| 632 | tree->cache_es = es; |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | /* |
Theodore Ts'o | bdafe42 | 2013-07-13 00:40:31 -0400 | [diff] [blame] | 637 | * ext4_es_insert_extent() adds information to an inode's extent |
| 638 | * status tree. |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 639 | * |
| 640 | * Return 0 on success, error code on failure. |
| 641 | */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 642 | int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk, |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 643 | ext4_lblk_t len, ext4_fsblk_t pblk, |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame^] | 644 | unsigned int status) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 645 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 646 | struct extent_status newes; |
| 647 | ext4_lblk_t end = lblk + len - 1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 648 | int err = 0; |
| 649 | |
Theodore Ts'o | 3be78c7 | 2013-08-16 21:22:41 -0400 | [diff] [blame^] | 650 | 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] | 651 | lblk, len, pblk, status, inode->i_ino); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 652 | |
Eryu Guan | d438147 | 2013-02-22 15:27:47 -0500 | [diff] [blame] | 653 | if (!len) |
| 654 | return 0; |
| 655 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 656 | BUG_ON(end < lblk); |
| 657 | |
| 658 | newes.es_lblk = lblk; |
| 659 | newes.es_len = len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 660 | ext4_es_store_pblock(&newes, pblk); |
| 661 | ext4_es_store_status(&newes, status); |
| 662 | trace_ext4_es_insert_extent(inode, &newes); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 663 | |
Dmitry Monakhov | 921f266 | 2013-03-10 21:01:03 -0400 | [diff] [blame] | 664 | ext4_es_insert_extent_check(inode, &newes); |
| 665 | |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 666 | write_lock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 667 | err = __es_remove_extent(inode, lblk, end); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 668 | if (err != 0) |
| 669 | goto error; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 670 | retry: |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 671 | err = __es_insert_extent(inode, &newes); |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 672 | if (err == -ENOMEM && __ext4_es_shrink(EXT4_SB(inode->i_sb), 1, |
| 673 | EXT4_I(inode))) |
| 674 | goto retry; |
| 675 | if (err == -ENOMEM && !ext4_es_is_delayed(&newes)) |
| 676 | err = 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 677 | |
| 678 | error: |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 679 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 680 | |
| 681 | ext4_es_print_tree(inode); |
| 682 | |
| 683 | return err; |
| 684 | } |
| 685 | |
Zheng Liu | d100eef | 2013-02-18 00:29:59 -0500 | [diff] [blame] | 686 | /* |
| 687 | * ext4_es_lookup_extent() looks up an extent in extent status tree. |
| 688 | * |
| 689 | * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks. |
| 690 | * |
| 691 | * Return: 1 on found, 0 on not |
| 692 | */ |
| 693 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, |
| 694 | struct extent_status *es) |
| 695 | { |
| 696 | struct ext4_es_tree *tree; |
| 697 | struct extent_status *es1 = NULL; |
| 698 | struct rb_node *node; |
| 699 | int found = 0; |
| 700 | |
| 701 | trace_ext4_es_lookup_extent_enter(inode, lblk); |
| 702 | es_debug("lookup extent in block %u\n", lblk); |
| 703 | |
| 704 | tree = &EXT4_I(inode)->i_es_tree; |
| 705 | read_lock(&EXT4_I(inode)->i_es_lock); |
| 706 | |
| 707 | /* find extent in cache firstly */ |
| 708 | es->es_lblk = es->es_len = es->es_pblk = 0; |
| 709 | if (tree->cache_es) { |
| 710 | es1 = tree->cache_es; |
| 711 | if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
| 712 | es_debug("%u cached by [%u/%u)\n", |
| 713 | lblk, es1->es_lblk, es1->es_len); |
| 714 | found = 1; |
| 715 | goto out; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | node = tree->root.rb_node; |
| 720 | while (node) { |
| 721 | es1 = rb_entry(node, struct extent_status, rb_node); |
| 722 | if (lblk < es1->es_lblk) |
| 723 | node = node->rb_left; |
| 724 | else if (lblk > ext4_es_end(es1)) |
| 725 | node = node->rb_right; |
| 726 | else { |
| 727 | found = 1; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | out: |
| 733 | if (found) { |
| 734 | BUG_ON(!es1); |
| 735 | es->es_lblk = es1->es_lblk; |
| 736 | es->es_len = es1->es_len; |
| 737 | es->es_pblk = es1->es_pblk; |
| 738 | } |
| 739 | |
| 740 | read_unlock(&EXT4_I(inode)->i_es_lock); |
| 741 | |
| 742 | trace_ext4_es_lookup_extent_exit(inode, es, found); |
| 743 | return found; |
| 744 | } |
| 745 | |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 746 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
| 747 | ext4_lblk_t end) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 748 | { |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 749 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 750 | struct rb_node *node; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 751 | struct extent_status *es; |
| 752 | struct extent_status orig_es; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 753 | ext4_lblk_t len1, len2; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 754 | ext4_fsblk_t block; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 755 | int err; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 756 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 757 | retry: |
| 758 | err = 0; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 759 | es = __es_tree_search(&tree->root, lblk); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 760 | if (!es) |
| 761 | goto out; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 762 | if (es->es_lblk > end) |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 763 | goto out; |
| 764 | |
| 765 | /* Simply invalidate cache_es. */ |
| 766 | tree->cache_es = NULL; |
| 767 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 768 | orig_es.es_lblk = es->es_lblk; |
| 769 | orig_es.es_len = es->es_len; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 770 | orig_es.es_pblk = es->es_pblk; |
| 771 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 772 | len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0; |
| 773 | len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 774 | if (len1 > 0) |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 775 | es->es_len = len1; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 776 | if (len2 > 0) { |
| 777 | if (len1 > 0) { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 778 | struct extent_status newes; |
| 779 | |
| 780 | newes.es_lblk = end + 1; |
| 781 | newes.es_len = len2; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 782 | if (ext4_es_is_written(&orig_es) || |
| 783 | ext4_es_is_unwritten(&orig_es)) { |
| 784 | block = ext4_es_pblock(&orig_es) + |
| 785 | orig_es.es_len - len2; |
| 786 | ext4_es_store_pblock(&newes, block); |
| 787 | } |
| 788 | ext4_es_store_status(&newes, ext4_es_status(&orig_es)); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 789 | err = __es_insert_extent(inode, &newes); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 790 | if (err) { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 791 | es->es_lblk = orig_es.es_lblk; |
| 792 | es->es_len = orig_es.es_len; |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 793 | if ((err == -ENOMEM) && |
| 794 | __ext4_es_shrink(EXT4_SB(inode->i_sb), 1, |
| 795 | EXT4_I(inode))) |
| 796 | goto retry; |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 797 | goto out; |
| 798 | } |
| 799 | } else { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 800 | es->es_lblk = end + 1; |
| 801 | es->es_len = len2; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 802 | if (ext4_es_is_written(es) || |
| 803 | ext4_es_is_unwritten(es)) { |
| 804 | block = orig_es.es_pblk + orig_es.es_len - len2; |
| 805 | ext4_es_store_pblock(es, block); |
| 806 | } |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 807 | } |
| 808 | goto out; |
| 809 | } |
| 810 | |
| 811 | if (len1 > 0) { |
| 812 | node = rb_next(&es->rb_node); |
| 813 | if (node) |
| 814 | es = rb_entry(node, struct extent_status, rb_node); |
| 815 | else |
| 816 | es = NULL; |
| 817 | } |
| 818 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 819 | while (es && ext4_es_end(es) <= end) { |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 820 | node = rb_next(&es->rb_node); |
| 821 | rb_erase(&es->rb_node, &tree->root); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 822 | ext4_es_free_extent(inode, es); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 823 | if (!node) { |
| 824 | es = NULL; |
| 825 | break; |
| 826 | } |
| 827 | es = rb_entry(node, struct extent_status, rb_node); |
| 828 | } |
| 829 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 830 | if (es && es->es_lblk < end + 1) { |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 831 | ext4_lblk_t orig_len = es->es_len; |
| 832 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 833 | len1 = ext4_es_end(es) - end; |
| 834 | es->es_lblk = end + 1; |
| 835 | es->es_len = len1; |
Zheng Liu | fdc0212 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 836 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
| 837 | block = es->es_pblk + orig_len - len1; |
| 838 | ext4_es_store_pblock(es, block); |
| 839 | } |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | out: |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 843 | return err; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * ext4_es_remove_extent() removes a space from a extent status tree. |
| 848 | * |
| 849 | * Return 0 on success, error code on failure. |
| 850 | */ |
| 851 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
| 852 | ext4_lblk_t len) |
| 853 | { |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 854 | ext4_lblk_t end; |
| 855 | int err = 0; |
| 856 | |
| 857 | trace_ext4_es_remove_extent(inode, lblk, len); |
| 858 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", |
| 859 | lblk, len, inode->i_ino); |
| 860 | |
Eryu Guan | d438147 | 2013-02-22 15:27:47 -0500 | [diff] [blame] | 861 | if (!len) |
| 862 | return err; |
| 863 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 864 | end = lblk + len - 1; |
| 865 | BUG_ON(end < lblk); |
| 866 | |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 867 | write_lock(&EXT4_I(inode)->i_es_lock); |
Zheng Liu | bdedbb7 | 2013-02-18 00:32:02 -0500 | [diff] [blame] | 868 | err = __es_remove_extent(inode, lblk, end); |
Zheng Liu | 654598b | 2012-11-08 21:57:20 -0500 | [diff] [blame] | 869 | write_unlock(&EXT4_I(inode)->i_es_lock); |
| 870 | ext4_es_print_tree(inode); |
| 871 | return err; |
| 872 | } |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 873 | |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 874 | int ext4_es_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 875 | { |
| 876 | ext4_lblk_t ee_block; |
| 877 | ext4_fsblk_t ee_pblock; |
| 878 | unsigned int ee_len; |
| 879 | |
| 880 | ee_block = le32_to_cpu(ex->ee_block); |
| 881 | ee_len = ext4_ext_get_actual_len(ex); |
| 882 | ee_pblock = ext4_ext_pblock(ex); |
| 883 | |
| 884 | if (ee_len == 0) |
| 885 | return 0; |
| 886 | |
| 887 | return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, |
| 888 | EXTENT_STATUS_WRITTEN); |
| 889 | } |
| 890 | |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 891 | static int ext4_inode_touch_time_cmp(void *priv, struct list_head *a, |
| 892 | struct list_head *b) |
| 893 | { |
| 894 | struct ext4_inode_info *eia, *eib; |
| 895 | eia = list_entry(a, struct ext4_inode_info, i_es_lru); |
| 896 | eib = list_entry(b, struct ext4_inode_info, i_es_lru); |
| 897 | |
| 898 | if (eia->i_touch_when == eib->i_touch_when) |
| 899 | return 0; |
| 900 | if (time_after(eia->i_touch_when, eib->i_touch_when)) |
| 901 | return 1; |
| 902 | else |
| 903 | return -1; |
| 904 | } |
| 905 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 906 | static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
| 907 | struct ext4_inode_info *locked_ei) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 908 | { |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 909 | struct ext4_inode_info *ei; |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 910 | struct list_head *cur, *tmp; |
| 911 | LIST_HEAD(skiped); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 912 | int ret, nr_shrunk = 0; |
| 913 | |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 914 | spin_lock(&sbi->s_es_lru_lock); |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 915 | |
| 916 | /* |
| 917 | * If the inode that is at the head of LRU list is newer than |
| 918 | * last_sorted time, that means that we need to sort this list. |
| 919 | */ |
| 920 | ei = list_first_entry(&sbi->s_es_lru, struct ext4_inode_info, i_es_lru); |
| 921 | if (sbi->s_es_last_sorted < ei->i_touch_when) { |
| 922 | list_sort(NULL, &sbi->s_es_lru, ext4_inode_touch_time_cmp); |
| 923 | sbi->s_es_last_sorted = jiffies; |
| 924 | } |
| 925 | |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 926 | list_for_each_safe(cur, tmp, &sbi->s_es_lru) { |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 927 | /* |
| 928 | * If we have already reclaimed all extents from extent |
| 929 | * status tree, just stop the loop immediately. |
| 930 | */ |
| 931 | if (percpu_counter_read_positive(&sbi->s_extent_cache_cnt) == 0) |
| 932 | break; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 933 | |
| 934 | ei = list_entry(cur, struct ext4_inode_info, i_es_lru); |
| 935 | |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 936 | /* Skip the inode that is newer than the last_sorted time */ |
| 937 | if (sbi->s_es_last_sorted < ei->i_touch_when) { |
| 938 | list_move_tail(cur, &skiped); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 939 | continue; |
| 940 | } |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 941 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 942 | if (ei->i_es_lru_nr == 0 || ei == locked_ei) |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 943 | continue; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 944 | |
| 945 | write_lock(&ei->i_es_lock); |
| 946 | ret = __es_try_to_reclaim_extents(ei, nr_to_scan); |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 947 | if (ei->i_es_lru_nr == 0) |
| 948 | list_del_init(&ei->i_es_lru); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 949 | write_unlock(&ei->i_es_lock); |
| 950 | |
| 951 | nr_shrunk += ret; |
| 952 | nr_to_scan -= ret; |
| 953 | if (nr_to_scan == 0) |
| 954 | break; |
| 955 | } |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 956 | |
| 957 | /* Move the newer inodes into the tail of the LRU list. */ |
| 958 | list_splice_tail(&skiped, &sbi->s_es_lru); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 959 | spin_unlock(&sbi->s_es_lru_lock); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 960 | |
Theodore Ts'o | e15f742 | 2013-07-15 00:12:14 -0400 | [diff] [blame] | 961 | if (locked_ei && nr_shrunk == 0) |
| 962 | nr_shrunk = __es_try_to_reclaim_extents(ei, nr_to_scan); |
| 963 | |
| 964 | return nr_shrunk; |
| 965 | } |
| 966 | |
| 967 | static int ext4_es_shrink(struct shrinker *shrink, struct shrink_control *sc) |
| 968 | { |
| 969 | struct ext4_sb_info *sbi = container_of(shrink, |
| 970 | struct ext4_sb_info, s_es_shrinker); |
| 971 | int nr_to_scan = sc->nr_to_scan; |
| 972 | int ret, nr_shrunk; |
| 973 | |
| 974 | ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt); |
| 975 | trace_ext4_es_shrink_enter(sbi->s_sb, nr_to_scan, ret); |
| 976 | |
| 977 | if (!nr_to_scan) |
| 978 | return ret; |
| 979 | |
| 980 | nr_shrunk = __ext4_es_shrink(sbi, nr_to_scan, NULL); |
| 981 | |
Theodore Ts'o | 1ac6466 | 2013-03-02 10:27:46 -0500 | [diff] [blame] | 982 | ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt); |
Theodore Ts'o | 2463077 | 2013-02-28 23:58:56 -0500 | [diff] [blame] | 983 | trace_ext4_es_shrink_exit(sbi->s_sb, nr_shrunk, ret); |
| 984 | return ret; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 985 | } |
| 986 | |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 987 | void ext4_es_register_shrinker(struct ext4_sb_info *sbi) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 988 | { |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 989 | INIT_LIST_HEAD(&sbi->s_es_lru); |
| 990 | spin_lock_init(&sbi->s_es_lru_lock); |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 991 | sbi->s_es_last_sorted = 0; |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 992 | sbi->s_es_shrinker.shrink = ext4_es_shrink; |
| 993 | sbi->s_es_shrinker.seeks = DEFAULT_SEEKS; |
| 994 | register_shrinker(&sbi->s_es_shrinker); |
| 995 | } |
| 996 | |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 997 | void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi) |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 998 | { |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 999 | unregister_shrinker(&sbi->s_es_shrinker); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | void ext4_es_lru_add(struct inode *inode) |
| 1003 | { |
| 1004 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 1005 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1006 | |
Zheng Liu | d3922a7 | 2013-07-01 08:12:37 -0400 | [diff] [blame] | 1007 | ei->i_touch_when = jiffies; |
| 1008 | |
| 1009 | if (!list_empty(&ei->i_es_lru)) |
| 1010 | return; |
| 1011 | |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1012 | spin_lock(&sbi->s_es_lru_lock); |
| 1013 | if (list_empty(&ei->i_es_lru)) |
| 1014 | list_add_tail(&ei->i_es_lru, &sbi->s_es_lru); |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1015 | spin_unlock(&sbi->s_es_lru_lock); |
| 1016 | } |
| 1017 | |
| 1018 | void ext4_es_lru_del(struct inode *inode) |
| 1019 | { |
| 1020 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 1021 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 1022 | |
| 1023 | spin_lock(&sbi->s_es_lru_lock); |
| 1024 | if (!list_empty(&ei->i_es_lru)) |
| 1025 | list_del_init(&ei->i_es_lru); |
| 1026 | spin_unlock(&sbi->s_es_lru_lock); |
| 1027 | } |
| 1028 | |
Zheng Liu | 74cd15c | 2013-02-18 00:32:55 -0500 | [diff] [blame] | 1029 | static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei, |
| 1030 | int nr_to_scan) |
| 1031 | { |
| 1032 | struct inode *inode = &ei->vfs_inode; |
| 1033 | struct ext4_es_tree *tree = &ei->i_es_tree; |
| 1034 | struct rb_node *node; |
| 1035 | struct extent_status *es; |
| 1036 | int nr_shrunk = 0; |
| 1037 | |
| 1038 | if (ei->i_es_lru_nr == 0) |
| 1039 | return 0; |
| 1040 | |
| 1041 | node = rb_first(&tree->root); |
| 1042 | while (node != NULL) { |
| 1043 | es = rb_entry(node, struct extent_status, rb_node); |
| 1044 | node = rb_next(&es->rb_node); |
| 1045 | /* |
| 1046 | * We can't reclaim delayed extent from status tree because |
| 1047 | * fiemap, bigallic, and seek_data/hole need to use it. |
| 1048 | */ |
| 1049 | if (!ext4_es_is_delayed(es)) { |
| 1050 | rb_erase(&es->rb_node, &tree->root); |
| 1051 | ext4_es_free_extent(inode, es); |
| 1052 | nr_shrunk++; |
| 1053 | if (--nr_to_scan == 0) |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | tree->cache_es = NULL; |
| 1058 | return nr_shrunk; |
| 1059 | } |