Chao Yu | 7c1a000 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 2 | /* |
| 3 | * f2fs extent cache support |
| 4 | * |
| 5 | * Copyright (c) 2015 Motorola Mobility |
| 6 | * Copyright (c) 2015 Samsung Electronics |
| 7 | * Authors: Jaegeuk Kim <jaegeuk@kernel.org> |
| 8 | * Chao Yu <chao2.yu@samsung.com> |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | |
| 14 | #include "f2fs.h" |
| 15 | #include "node.h" |
| 16 | #include <trace/events/f2fs.h> |
| 17 | |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 18 | static struct rb_entry *__lookup_rb_tree_fast(struct rb_entry *cached_re, |
| 19 | unsigned int ofs) |
| 20 | { |
| 21 | if (cached_re) { |
| 22 | if (cached_re->ofs <= ofs && |
| 23 | cached_re->ofs + cached_re->len > ofs) { |
| 24 | return cached_re; |
| 25 | } |
| 26 | } |
| 27 | return NULL; |
| 28 | } |
| 29 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 30 | static struct rb_entry *__lookup_rb_tree_slow(struct rb_root_cached *root, |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 31 | unsigned int ofs) |
| 32 | { |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 33 | struct rb_node *node = root->rb_root.rb_node; |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 34 | struct rb_entry *re; |
| 35 | |
| 36 | while (node) { |
| 37 | re = rb_entry(node, struct rb_entry, rb_node); |
| 38 | |
| 39 | if (ofs < re->ofs) |
| 40 | node = node->rb_left; |
| 41 | else if (ofs >= re->ofs + re->len) |
| 42 | node = node->rb_right; |
| 43 | else |
| 44 | return re; |
| 45 | } |
| 46 | return NULL; |
| 47 | } |
| 48 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 49 | struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root, |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 50 | struct rb_entry *cached_re, unsigned int ofs) |
| 51 | { |
| 52 | struct rb_entry *re; |
| 53 | |
| 54 | re = __lookup_rb_tree_fast(cached_re, ofs); |
| 55 | if (!re) |
| 56 | return __lookup_rb_tree_slow(root, ofs); |
| 57 | |
| 58 | return re; |
| 59 | } |
| 60 | |
Chao Yu | 2e9b2bb | 2020-08-04 21:14:48 +0800 | [diff] [blame] | 61 | struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi, |
| 62 | struct rb_root_cached *root, |
| 63 | struct rb_node **parent, |
| 64 | unsigned long long key, bool *leftmost) |
| 65 | { |
| 66 | struct rb_node **p = &root->rb_root.rb_node; |
| 67 | struct rb_entry *re; |
| 68 | |
| 69 | while (*p) { |
| 70 | *parent = *p; |
| 71 | re = rb_entry(*parent, struct rb_entry, rb_node); |
| 72 | |
| 73 | if (key < re->key) { |
| 74 | p = &(*p)->rb_left; |
| 75 | } else { |
| 76 | p = &(*p)->rb_right; |
| 77 | *leftmost = false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return p; |
| 82 | } |
| 83 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 84 | struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 85 | struct rb_root_cached *root, |
| 86 | struct rb_node **parent, |
| 87 | unsigned int ofs, bool *leftmost) |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 88 | { |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 89 | struct rb_node **p = &root->rb_root.rb_node; |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 90 | struct rb_entry *re; |
| 91 | |
| 92 | while (*p) { |
| 93 | *parent = *p; |
| 94 | re = rb_entry(*parent, struct rb_entry, rb_node); |
| 95 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 96 | if (ofs < re->ofs) { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 97 | p = &(*p)->rb_left; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 98 | } else if (ofs >= re->ofs + re->len) { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 99 | p = &(*p)->rb_right; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 100 | *leftmost = false; |
| 101 | } else { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 102 | f2fs_bug_on(sbi, 1); |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 103 | } |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return p; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * lookup rb entry in position of @ofs in rb-tree, |
| 111 | * if hit, return the entry, otherwise, return NULL |
| 112 | * @prev_ex: extent before ofs |
| 113 | * @next_ex: extent after ofs |
| 114 | * @insert_p: insert point for new extent at ofs |
| 115 | * in order to simpfy the insertion after. |
| 116 | * tree must stay unchanged between lookup and insertion. |
| 117 | */ |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 118 | struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root, |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 119 | struct rb_entry *cached_re, |
| 120 | unsigned int ofs, |
| 121 | struct rb_entry **prev_entry, |
| 122 | struct rb_entry **next_entry, |
| 123 | struct rb_node ***insert_p, |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 124 | struct rb_node **insert_parent, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 125 | bool force, bool *leftmost) |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 126 | { |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 127 | struct rb_node **pnode = &root->rb_root.rb_node; |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 128 | struct rb_node *parent = NULL, *tmp_node; |
| 129 | struct rb_entry *re = cached_re; |
| 130 | |
| 131 | *insert_p = NULL; |
| 132 | *insert_parent = NULL; |
| 133 | *prev_entry = NULL; |
| 134 | *next_entry = NULL; |
| 135 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 136 | if (RB_EMPTY_ROOT(&root->rb_root)) |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 137 | return NULL; |
| 138 | |
| 139 | if (re) { |
| 140 | if (re->ofs <= ofs && re->ofs + re->len > ofs) |
| 141 | goto lookup_neighbors; |
| 142 | } |
| 143 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 144 | if (leftmost) |
| 145 | *leftmost = true; |
| 146 | |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 147 | while (*pnode) { |
| 148 | parent = *pnode; |
| 149 | re = rb_entry(*pnode, struct rb_entry, rb_node); |
| 150 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 151 | if (ofs < re->ofs) { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 152 | pnode = &(*pnode)->rb_left; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 153 | } else if (ofs >= re->ofs + re->len) { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 154 | pnode = &(*pnode)->rb_right; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 155 | if (leftmost) |
| 156 | *leftmost = false; |
| 157 | } else { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 158 | goto lookup_neighbors; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 159 | } |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | *insert_p = pnode; |
| 163 | *insert_parent = parent; |
| 164 | |
| 165 | re = rb_entry(parent, struct rb_entry, rb_node); |
| 166 | tmp_node = parent; |
| 167 | if (parent && ofs > re->ofs) |
| 168 | tmp_node = rb_next(parent); |
| 169 | *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); |
| 170 | |
| 171 | tmp_node = parent; |
| 172 | if (parent && ofs < re->ofs) |
| 173 | tmp_node = rb_prev(parent); |
| 174 | *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); |
| 175 | return NULL; |
| 176 | |
| 177 | lookup_neighbors: |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 178 | if (ofs == re->ofs || force) { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 179 | /* lookup prev node for merging backward later */ |
| 180 | tmp_node = rb_prev(&re->rb_node); |
| 181 | *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); |
| 182 | } |
Chao Yu | 004b686 | 2017-04-14 23:24:55 +0800 | [diff] [blame] | 183 | if (ofs == re->ofs + re->len - 1 || force) { |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 184 | /* lookup next node for merging frontward later */ |
| 185 | tmp_node = rb_next(&re->rb_node); |
| 186 | *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); |
| 187 | } |
| 188 | return re; |
| 189 | } |
| 190 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 191 | bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi, |
Chao Yu | 2e9b2bb | 2020-08-04 21:14:48 +0800 | [diff] [blame] | 192 | struct rb_root_cached *root, bool check_key) |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 193 | { |
| 194 | #ifdef CONFIG_F2FS_CHECK_FS |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 195 | struct rb_node *cur = rb_first_cached(root), *next; |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 196 | struct rb_entry *cur_re, *next_re; |
| 197 | |
| 198 | if (!cur) |
| 199 | return true; |
| 200 | |
| 201 | while (cur) { |
| 202 | next = rb_next(cur); |
| 203 | if (!next) |
| 204 | return true; |
| 205 | |
| 206 | cur_re = rb_entry(cur, struct rb_entry, rb_node); |
| 207 | next_re = rb_entry(next, struct rb_entry, rb_node); |
| 208 | |
Chao Yu | 2e9b2bb | 2020-08-04 21:14:48 +0800 | [diff] [blame] | 209 | if (check_key) { |
| 210 | if (cur_re->key > next_re->key) { |
| 211 | f2fs_info(sbi, "inconsistent rbtree, " |
| 212 | "cur(%llu) next(%llu)", |
| 213 | cur_re->key, next_re->key); |
| 214 | return false; |
| 215 | } |
| 216 | goto next; |
| 217 | } |
| 218 | |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 219 | if (cur_re->ofs + cur_re->len > next_re->ofs) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 220 | f2fs_info(sbi, "inconsistent rbtree, cur(%u, %u) next(%u, %u)", |
| 221 | cur_re->ofs, cur_re->len, |
| 222 | next_re->ofs, next_re->len); |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 223 | return false; |
| 224 | } |
Chao Yu | 2e9b2bb | 2020-08-04 21:14:48 +0800 | [diff] [blame] | 225 | next: |
Chao Yu | df0f6b4 | 2017-04-17 18:21:43 +0800 | [diff] [blame] | 226 | cur = next; |
| 227 | } |
| 228 | #endif |
| 229 | return true; |
| 230 | } |
| 231 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 232 | static struct kmem_cache *extent_tree_slab; |
| 233 | static struct kmem_cache *extent_node_slab; |
| 234 | |
| 235 | static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi, |
| 236 | struct extent_tree *et, struct extent_info *ei, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 237 | struct rb_node *parent, struct rb_node **p, |
| 238 | bool leftmost) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 239 | { |
| 240 | struct extent_node *en; |
| 241 | |
Chao Yu | 3241057 | 2021-08-09 08:24:48 +0800 | [diff] [blame] | 242 | en = f2fs_kmem_cache_alloc(extent_node_slab, GFP_ATOMIC, false, sbi); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 243 | if (!en) |
| 244 | return NULL; |
| 245 | |
| 246 | en->ei = *ei; |
| 247 | INIT_LIST_HEAD(&en->list); |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 248 | en->et = et; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 249 | |
| 250 | rb_link_node(&en->rb_node, parent, p); |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 251 | rb_insert_color_cached(&en->rb_node, &et->root, leftmost); |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 252 | atomic_inc(&et->node_cnt); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 253 | atomic_inc(&sbi->total_ext_node); |
| 254 | return en; |
| 255 | } |
| 256 | |
| 257 | static void __detach_extent_node(struct f2fs_sb_info *sbi, |
| 258 | struct extent_tree *et, struct extent_node *en) |
| 259 | { |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 260 | rb_erase_cached(&en->rb_node, &et->root); |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 261 | atomic_dec(&et->node_cnt); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 262 | atomic_dec(&sbi->total_ext_node); |
| 263 | |
| 264 | if (et->cached_en == en) |
| 265 | et->cached_en = NULL; |
Hou Pengyang | a03f01f | 2016-01-26 12:56:25 +0000 | [diff] [blame] | 266 | kmem_cache_free(extent_node_slab, en); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Flow to release an extent_node: |
| 271 | * 1. list_del_init |
| 272 | * 2. __detach_extent_node |
| 273 | * 3. kmem_cache_free. |
| 274 | */ |
| 275 | static void __release_extent_node(struct f2fs_sb_info *sbi, |
| 276 | struct extent_tree *et, struct extent_node *en) |
| 277 | { |
| 278 | spin_lock(&sbi->extent_lock); |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 279 | f2fs_bug_on(sbi, list_empty(&en->list)); |
| 280 | list_del_init(&en->list); |
Hou Pengyang | a03f01f | 2016-01-26 12:56:25 +0000 | [diff] [blame] | 281 | spin_unlock(&sbi->extent_lock); |
| 282 | |
| 283 | __detach_extent_node(sbi, et, en); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static struct extent_tree *__grab_extent_tree(struct inode *inode) |
| 287 | { |
| 288 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 289 | struct extent_tree *et; |
| 290 | nid_t ino = inode->i_ino; |
| 291 | |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 292 | mutex_lock(&sbi->extent_tree_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 293 | et = radix_tree_lookup(&sbi->extent_tree_root, ino); |
| 294 | if (!et) { |
Chao Yu | 3241057 | 2021-08-09 08:24:48 +0800 | [diff] [blame] | 295 | et = f2fs_kmem_cache_alloc(extent_tree_slab, |
| 296 | GFP_NOFS, true, NULL); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 297 | f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et); |
| 298 | memset(et, 0, sizeof(struct extent_tree)); |
| 299 | et->ino = ino; |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 300 | et->root = RB_ROOT_CACHED; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 301 | et->cached_en = NULL; |
| 302 | rwlock_init(&et->lock); |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 303 | INIT_LIST_HEAD(&et->list); |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 304 | atomic_set(&et->node_cnt, 0); |
Jaegeuk Kim | 7441cce | 2015-12-21 19:20:15 -0800 | [diff] [blame] | 305 | atomic_inc(&sbi->total_ext_tree); |
Jaegeuk Kim | 74fd8d9 | 2015-12-21 19:25:50 -0800 | [diff] [blame] | 306 | } else { |
| 307 | atomic_dec(&sbi->total_zombie_tree); |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 308 | list_del_init(&et->list); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 309 | } |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 310 | mutex_unlock(&sbi->extent_tree_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 311 | |
| 312 | /* never died until evict_inode */ |
| 313 | F2FS_I(inode)->extent_tree = et; |
| 314 | |
| 315 | return et; |
| 316 | } |
| 317 | |
Chao Yu | a6f7834 | 2015-08-19 19:14:15 +0800 | [diff] [blame] | 318 | static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi, |
| 319 | struct extent_tree *et, struct extent_info *ei) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 320 | { |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 321 | struct rb_node **p = &et->root.rb_root.rb_node; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 322 | struct extent_node *en; |
| 323 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 324 | en = __attach_extent_node(sbi, et, ei, NULL, p, true); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 325 | if (!en) |
| 326 | return NULL; |
Chao Yu | a6f7834 | 2015-08-19 19:14:15 +0800 | [diff] [blame] | 327 | |
| 328 | et->largest = en->ei; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 329 | et->cached_en = en; |
| 330 | return en; |
| 331 | } |
| 332 | |
| 333 | static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi, |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 334 | struct extent_tree *et) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 335 | { |
| 336 | struct rb_node *node, *next; |
| 337 | struct extent_node *en; |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 338 | unsigned int count = atomic_read(&et->node_cnt); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 339 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 340 | node = rb_first_cached(&et->root); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 341 | while (node) { |
| 342 | next = rb_next(node); |
| 343 | en = rb_entry(node, struct extent_node, rb_node); |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 344 | __release_extent_node(sbi, et, en); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 345 | node = next; |
| 346 | } |
| 347 | |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 348 | return count - atomic_read(&et->node_cnt); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 349 | } |
| 350 | |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 351 | static void __drop_largest_extent(struct extent_tree *et, |
Fan Li | 41a099d | 2015-09-17 18:24:17 +0800 | [diff] [blame] | 352 | pgoff_t fofs, unsigned int len) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 353 | { |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 354 | if (fofs < et->largest.fofs + et->largest.len && |
| 355 | fofs + len > et->largest.fofs) { |
| 356 | et->largest.len = 0; |
| 357 | et->largest_updated = true; |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 358 | } |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 359 | } |
| 360 | |
Jaegeuk Kim | ed3d125 | 2015-12-28 11:39:06 -0800 | [diff] [blame] | 361 | /* return true, if inode page is changed */ |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 362 | static void __f2fs_init_extent_tree(struct inode *inode, struct page *ipage) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 363 | { |
| 364 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 365 | struct f2fs_extent *i_ext = ipage ? &F2FS_INODE(ipage)->i_ext : NULL; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 366 | struct extent_tree *et; |
| 367 | struct extent_node *en; |
| 368 | struct extent_info ei; |
| 369 | |
Jaegeuk Kim | ed3d125 | 2015-12-28 11:39:06 -0800 | [diff] [blame] | 370 | if (!f2fs_may_extent_tree(inode)) { |
| 371 | /* drop largest extent */ |
| 372 | if (i_ext && i_ext->len) { |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 373 | f2fs_wait_on_page_writeback(ipage, NODE, true, true); |
Jaegeuk Kim | ed3d125 | 2015-12-28 11:39:06 -0800 | [diff] [blame] | 374 | i_ext->len = 0; |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 375 | set_page_dirty(ipage); |
| 376 | return; |
Jaegeuk Kim | ed3d125 | 2015-12-28 11:39:06 -0800 | [diff] [blame] | 377 | } |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 378 | return; |
Jaegeuk Kim | ed3d125 | 2015-12-28 11:39:06 -0800 | [diff] [blame] | 379 | } |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 380 | |
| 381 | et = __grab_extent_tree(inode); |
| 382 | |
Jaegeuk Kim | ed3d125 | 2015-12-28 11:39:06 -0800 | [diff] [blame] | 383 | if (!i_ext || !i_ext->len) |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 384 | return; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 385 | |
Chao Yu | bd933d4 | 2016-05-04 23:19:47 +0800 | [diff] [blame] | 386 | get_extent_info(&ei, i_ext); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 387 | |
| 388 | write_lock(&et->lock); |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 389 | if (atomic_read(&et->node_cnt)) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 390 | goto out; |
| 391 | |
Chao Yu | a6f7834 | 2015-08-19 19:14:15 +0800 | [diff] [blame] | 392 | en = __init_extent_tree(sbi, et, &ei); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 393 | if (en) { |
| 394 | spin_lock(&sbi->extent_lock); |
| 395 | list_add_tail(&en->list, &sbi->extent_list); |
| 396 | spin_unlock(&sbi->extent_lock); |
| 397 | } |
| 398 | out: |
| 399 | write_unlock(&et->lock); |
| 400 | } |
| 401 | |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 402 | void f2fs_init_extent_tree(struct inode *inode, struct page *ipage) |
Yunlei He | dad48e7 | 2017-05-19 15:06:12 +0800 | [diff] [blame] | 403 | { |
Chao Yu | a6d601f | 2020-06-28 10:58:17 +0800 | [diff] [blame] | 404 | __f2fs_init_extent_tree(inode, ipage); |
Yunlei He | dad48e7 | 2017-05-19 15:06:12 +0800 | [diff] [blame] | 405 | |
| 406 | if (!F2FS_I(inode)->extent_tree) |
| 407 | set_inode_flag(inode, FI_NO_EXTENT); |
Yunlei He | dad48e7 | 2017-05-19 15:06:12 +0800 | [diff] [blame] | 408 | } |
| 409 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 410 | static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs, |
| 411 | struct extent_info *ei) |
| 412 | { |
| 413 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 414 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
| 415 | struct extent_node *en; |
| 416 | bool ret = false; |
| 417 | |
| 418 | f2fs_bug_on(sbi, !et); |
| 419 | |
| 420 | trace_f2fs_lookup_extent_tree_start(inode, pgofs); |
| 421 | |
| 422 | read_lock(&et->lock); |
| 423 | |
| 424 | if (et->largest.fofs <= pgofs && |
| 425 | et->largest.fofs + et->largest.len > pgofs) { |
| 426 | *ei = et->largest; |
| 427 | ret = true; |
Chao Yu | 91c481f | 2015-08-19 19:12:20 +0800 | [diff] [blame] | 428 | stat_inc_largest_node_hit(sbi); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 429 | goto out; |
| 430 | } |
| 431 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 432 | en = (struct extent_node *)f2fs_lookup_rb_tree(&et->root, |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 433 | (struct rb_entry *)et->cached_en, pgofs); |
| 434 | if (!en) |
| 435 | goto out; |
| 436 | |
| 437 | if (en == et->cached_en) |
| 438 | stat_inc_cached_node_hit(sbi); |
| 439 | else |
| 440 | stat_inc_rbtree_node_hit(sbi); |
| 441 | |
| 442 | *ei = en->ei; |
| 443 | spin_lock(&sbi->extent_lock); |
| 444 | if (!list_empty(&en->list)) { |
| 445 | list_move_tail(&en->list, &sbi->extent_list); |
| 446 | et->cached_en = en; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 447 | } |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 448 | spin_unlock(&sbi->extent_lock); |
| 449 | ret = true; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 450 | out: |
Chao Yu | 727edac | 2015-07-15 17:29:49 +0800 | [diff] [blame] | 451 | stat_inc_total_hit(sbi); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 452 | read_unlock(&et->lock); |
| 453 | |
| 454 | trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei); |
| 455 | return ret; |
| 456 | } |
| 457 | |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 458 | static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi, |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 459 | struct extent_tree *et, struct extent_info *ei, |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 460 | struct extent_node *prev_ex, |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 461 | struct extent_node *next_ex) |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 462 | { |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 463 | struct extent_node *en = NULL; |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 464 | |
| 465 | if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) { |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 466 | prev_ex->ei.len += ei->len; |
| 467 | ei = &prev_ex->ei; |
| 468 | en = prev_ex; |
| 469 | } |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 470 | |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 471 | if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) { |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 472 | next_ex->ei.fofs = ei->fofs; |
| 473 | next_ex->ei.blk = ei->blk; |
| 474 | next_ex->ei.len += ei->len; |
Yunlei He | 7855eba | 2016-12-19 20:10:48 +0800 | [diff] [blame] | 475 | if (en) |
| 476 | __release_extent_node(sbi, et, prev_ex); |
| 477 | |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 478 | en = next_ex; |
| 479 | } |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 480 | |
Jaegeuk Kim | 43a2fa1 | 2016-01-26 09:12:50 -0800 | [diff] [blame] | 481 | if (!en) |
| 482 | return NULL; |
| 483 | |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 484 | __try_update_largest_extent(et, en); |
Jaegeuk Kim | 43a2fa1 | 2016-01-26 09:12:50 -0800 | [diff] [blame] | 485 | |
| 486 | spin_lock(&sbi->extent_lock); |
Jaegeuk Kim | 4292674 | 2016-01-26 09:20:05 -0800 | [diff] [blame] | 487 | if (!list_empty(&en->list)) { |
Jaegeuk Kim | 43a2fa1 | 2016-01-26 09:12:50 -0800 | [diff] [blame] | 488 | list_move_tail(&en->list, &sbi->extent_list); |
Jaegeuk Kim | 4292674 | 2016-01-26 09:20:05 -0800 | [diff] [blame] | 489 | et->cached_en = en; |
| 490 | } |
Jaegeuk Kim | 43a2fa1 | 2016-01-26 09:12:50 -0800 | [diff] [blame] | 491 | spin_unlock(&sbi->extent_lock); |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 492 | return en; |
| 493 | } |
| 494 | |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 495 | static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi, |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 496 | struct extent_tree *et, struct extent_info *ei, |
| 497 | struct rb_node **insert_p, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 498 | struct rb_node *insert_parent, |
| 499 | bool leftmost) |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 500 | { |
Colin Ian King | 8fe326c | 2018-02-21 18:13:40 +0000 | [diff] [blame] | 501 | struct rb_node **p; |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 502 | struct rb_node *parent = NULL; |
| 503 | struct extent_node *en = NULL; |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 504 | |
| 505 | if (insert_p && insert_parent) { |
| 506 | parent = insert_parent; |
| 507 | p = insert_p; |
| 508 | goto do_insert; |
| 509 | } |
| 510 | |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 511 | leftmost = true; |
| 512 | |
| 513 | p = f2fs_lookup_rb_tree_for_insert(sbi, &et->root, &parent, |
| 514 | ei->fofs, &leftmost); |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 515 | do_insert: |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 516 | en = __attach_extent_node(sbi, et, ei, parent, p, leftmost); |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 517 | if (!en) |
| 518 | return NULL; |
Chao Yu | ef05e22 | 2015-08-19 19:15:09 +0800 | [diff] [blame] | 519 | |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 520 | __try_update_largest_extent(et, en); |
Jaegeuk Kim | 43a2fa1 | 2016-01-26 09:12:50 -0800 | [diff] [blame] | 521 | |
| 522 | /* update in global extent list */ |
| 523 | spin_lock(&sbi->extent_lock); |
| 524 | list_add_tail(&en->list, &sbi->extent_list); |
Jaegeuk Kim | 4292674 | 2016-01-26 09:20:05 -0800 | [diff] [blame] | 525 | et->cached_en = en; |
Jaegeuk Kim | 43a2fa1 | 2016-01-26 09:12:50 -0800 | [diff] [blame] | 526 | spin_unlock(&sbi->extent_lock); |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 527 | return en; |
| 528 | } |
| 529 | |
Chao Yu | 317e130 | 2017-02-25 17:29:54 +0800 | [diff] [blame] | 530 | static void f2fs_update_extent_tree_range(struct inode *inode, |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 531 | pgoff_t fofs, block_t blkaddr, unsigned int len) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 532 | { |
| 533 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 534 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 535 | struct extent_node *en = NULL, *en1 = NULL; |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 536 | struct extent_node *prev_en = NULL, *next_en = NULL; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 537 | struct extent_info ei, dei, prev; |
Fan Li | 0f825ee | 2015-07-15 18:05:17 +0800 | [diff] [blame] | 538 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 539 | unsigned int end = fofs + len; |
| 540 | unsigned int pos = (unsigned int)fofs; |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 541 | bool updated = false; |
Chao Yu | f9aa52a | 2019-01-16 09:51:28 +0800 | [diff] [blame] | 542 | bool leftmost = false; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 543 | |
| 544 | if (!et) |
Chao Yu | 317e130 | 2017-02-25 17:29:54 +0800 | [diff] [blame] | 545 | return; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 546 | |
Chao Yu | 744288c | 2015-09-06 17:50:13 +0800 | [diff] [blame] | 547 | trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len); |
| 548 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 549 | write_lock(&et->lock); |
| 550 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 551 | if (is_inode_flag_set(inode, FI_NO_EXTENT)) { |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 552 | write_unlock(&et->lock); |
Chao Yu | 317e130 | 2017-02-25 17:29:54 +0800 | [diff] [blame] | 553 | return; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | prev = et->largest; |
| 557 | dei.len = 0; |
| 558 | |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 559 | /* |
| 560 | * drop largest extent before lookup, in case it's already |
| 561 | * been shrunk from extent tree |
| 562 | */ |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 563 | __drop_largest_extent(et, fofs, len); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 564 | |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 565 | /* 1. lookup first extent node in range [fofs, fofs + len - 1] */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 566 | en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root, |
Chao Yu | 54c2258 | 2017-04-11 09:25:22 +0800 | [diff] [blame] | 567 | (struct rb_entry *)et->cached_en, fofs, |
| 568 | (struct rb_entry **)&prev_en, |
| 569 | (struct rb_entry **)&next_en, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 570 | &insert_p, &insert_parent, false, |
| 571 | &leftmost); |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 572 | if (!en) |
| 573 | en = next_en; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 574 | |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 575 | /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */ |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 576 | while (en && en->ei.fofs < end) { |
| 577 | unsigned int org_end; |
| 578 | int parts = 0; /* # of parts current extent split into */ |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 579 | |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 580 | next_en = en1 = NULL; |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 581 | |
| 582 | dei = en->ei; |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 583 | org_end = dei.fofs + dei.len; |
| 584 | f2fs_bug_on(sbi, pos >= org_end); |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 585 | |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 586 | if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) { |
| 587 | en->ei.len = pos - en->ei.fofs; |
| 588 | prev_en = en; |
| 589 | parts = 1; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 590 | } |
| 591 | |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 592 | if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) { |
| 593 | if (parts) { |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 594 | set_extent_info(&ei, end, |
| 595 | end - dei.fofs + dei.blk, |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 596 | org_end - end); |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 597 | en1 = __insert_extent_tree(sbi, et, &ei, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 598 | NULL, NULL, true); |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 599 | next_en = en1; |
| 600 | } else { |
| 601 | en->ei.fofs = end; |
| 602 | en->ei.blk += end - dei.fofs; |
| 603 | en->ei.len -= end - dei.fofs; |
| 604 | next_en = en; |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 605 | } |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 606 | parts++; |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 607 | } |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 608 | |
| 609 | if (!next_en) { |
| 610 | struct rb_node *node = rb_next(&en->rb_node); |
| 611 | |
Geliang Tang | ed0b562 | 2016-12-20 21:57:42 +0800 | [diff] [blame] | 612 | next_en = rb_entry_safe(node, struct extent_node, |
| 613 | rb_node); |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 614 | } |
| 615 | |
Chao Yu | 4abd3f5 | 2015-09-22 21:07:47 +0800 | [diff] [blame] | 616 | if (parts) |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 617 | __try_update_largest_extent(et, en); |
Chao Yu | 4abd3f5 | 2015-09-22 21:07:47 +0800 | [diff] [blame] | 618 | else |
Hou Pengyang | a03f01f | 2016-01-26 12:56:25 +0000 | [diff] [blame] | 619 | __release_extent_node(sbi, et, en); |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 620 | |
| 621 | /* |
| 622 | * if original extent is split into zero or two parts, extent |
| 623 | * tree has been altered by deletion or insertion, therefore |
| 624 | * invalidate pointers regard to tree. |
| 625 | */ |
| 626 | if (parts != 1) { |
| 627 | insert_p = NULL; |
| 628 | insert_parent = NULL; |
| 629 | } |
Fan Li | 4d1fa81 | 2015-09-17 18:42:06 +0800 | [diff] [blame] | 630 | en = next_en; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 631 | } |
| 632 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 633 | /* 3. update extent in extent cache */ |
| 634 | if (blkaddr) { |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 635 | |
| 636 | set_extent_info(&ei, fofs, blkaddr, len); |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 637 | if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en)) |
| 638 | __insert_extent_tree(sbi, et, &ei, |
Chao Yu | 4dada3f | 2018-10-04 11:18:30 +0800 | [diff] [blame] | 639 | insert_p, insert_parent, leftmost); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 640 | |
| 641 | /* give up extent_cache, if split and small updates happen */ |
| 642 | if (dei.len >= 1 && |
| 643 | prev.len < F2FS_MIN_EXTENT_LEN && |
| 644 | et->largest.len < F2FS_MIN_EXTENT_LEN) { |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 645 | et->largest.len = 0; |
| 646 | et->largest_updated = true; |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 647 | set_inode_flag(inode, FI_NO_EXTENT); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 648 | } |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 649 | } |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 650 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 651 | if (is_inode_flag_set(inode, FI_NO_EXTENT)) |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 652 | __free_extent_tree(sbi, et); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 653 | |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 654 | if (et->largest_updated) { |
| 655 | et->largest_updated = false; |
| 656 | updated = true; |
| 657 | } |
| 658 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 659 | write_unlock(&et->lock); |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 660 | |
| 661 | if (updated) |
| 662 | f2fs_mark_inode_dirty_sync(inode, true); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 663 | } |
| 664 | |
Chao Yu | 94afd6d | 2021-08-04 10:23:48 +0800 | [diff] [blame] | 665 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
| 666 | void f2fs_update_extent_tree_range_compressed(struct inode *inode, |
| 667 | pgoff_t fofs, block_t blkaddr, unsigned int llen, |
| 668 | unsigned int c_len) |
| 669 | { |
| 670 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 671 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
| 672 | struct extent_node *en = NULL; |
| 673 | struct extent_node *prev_en = NULL, *next_en = NULL; |
| 674 | struct extent_info ei; |
| 675 | struct rb_node **insert_p = NULL, *insert_parent = NULL; |
| 676 | bool leftmost = false; |
| 677 | |
| 678 | trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, llen); |
| 679 | |
| 680 | /* it is safe here to check FI_NO_EXTENT w/o et->lock in ro image */ |
| 681 | if (is_inode_flag_set(inode, FI_NO_EXTENT)) |
| 682 | return; |
| 683 | |
| 684 | write_lock(&et->lock); |
| 685 | |
| 686 | en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root, |
| 687 | (struct rb_entry *)et->cached_en, fofs, |
| 688 | (struct rb_entry **)&prev_en, |
| 689 | (struct rb_entry **)&next_en, |
| 690 | &insert_p, &insert_parent, false, |
| 691 | &leftmost); |
| 692 | if (en) |
| 693 | goto unlock_out; |
| 694 | |
| 695 | set_extent_info(&ei, fofs, blkaddr, llen); |
| 696 | ei.c_len = c_len; |
| 697 | |
| 698 | if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en)) |
| 699 | __insert_extent_tree(sbi, et, &ei, |
| 700 | insert_p, insert_parent, leftmost); |
| 701 | unlock_out: |
| 702 | write_unlock(&et->lock); |
| 703 | } |
| 704 | #endif |
| 705 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 706 | unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink) |
| 707 | { |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 708 | struct extent_tree *et, *next; |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 709 | struct extent_node *en; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 710 | unsigned int node_cnt = 0, tree_cnt = 0; |
| 711 | int remained; |
| 712 | |
| 713 | if (!test_opt(sbi, EXTENT_CACHE)) |
| 714 | return 0; |
| 715 | |
Jaegeuk Kim | 74fd8d9 | 2015-12-21 19:25:50 -0800 | [diff] [blame] | 716 | if (!atomic_read(&sbi->total_zombie_tree)) |
| 717 | goto free_node; |
| 718 | |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 719 | if (!mutex_trylock(&sbi->extent_tree_lock)) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 720 | goto out; |
| 721 | |
| 722 | /* 1. remove unreferenced extent tree */ |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 723 | list_for_each_entry_safe(et, next, &sbi->zombie_list, list) { |
Chao Yu | 9b72a38 | 2016-01-08 20:24:00 +0800 | [diff] [blame] | 724 | if (atomic_read(&et->node_cnt)) { |
| 725 | write_lock(&et->lock); |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 726 | node_cnt += __free_extent_tree(sbi, et); |
Chao Yu | 9b72a38 | 2016-01-08 20:24:00 +0800 | [diff] [blame] | 727 | write_unlock(&et->lock); |
| 728 | } |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 729 | f2fs_bug_on(sbi, atomic_read(&et->node_cnt)); |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 730 | list_del_init(&et->list); |
| 731 | radix_tree_delete(&sbi->extent_tree_root, et->ino); |
| 732 | kmem_cache_free(extent_tree_slab, et); |
| 733 | atomic_dec(&sbi->total_ext_tree); |
| 734 | atomic_dec(&sbi->total_zombie_tree); |
| 735 | tree_cnt++; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 736 | |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 737 | if (node_cnt + tree_cnt >= nr_shrink) |
| 738 | goto unlock_out; |
Jaegeuk Kim | 6fe2bc9 | 2016-01-20 07:31:48 +0800 | [diff] [blame] | 739 | cond_resched(); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 740 | } |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 741 | mutex_unlock(&sbi->extent_tree_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 742 | |
Jaegeuk Kim | 74fd8d9 | 2015-12-21 19:25:50 -0800 | [diff] [blame] | 743 | free_node: |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 744 | /* 2. remove LRU extent entries */ |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 745 | if (!mutex_trylock(&sbi->extent_tree_lock)) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 746 | goto out; |
| 747 | |
| 748 | remained = nr_shrink - (node_cnt + tree_cnt); |
| 749 | |
| 750 | spin_lock(&sbi->extent_lock); |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 751 | for (; remained > 0; remained--) { |
| 752 | if (list_empty(&sbi->extent_list)) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 753 | break; |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 754 | en = list_first_entry(&sbi->extent_list, |
| 755 | struct extent_node, list); |
| 756 | et = en->et; |
| 757 | if (!write_trylock(&et->lock)) { |
| 758 | /* refresh this extent node's position in extent list */ |
| 759 | list_move_tail(&en->list, &sbi->extent_list); |
| 760 | continue; |
| 761 | } |
| 762 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 763 | list_del_init(&en->list); |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 764 | spin_unlock(&sbi->extent_lock); |
| 765 | |
| 766 | __detach_extent_node(sbi, et, en); |
| 767 | |
| 768 | write_unlock(&et->lock); |
| 769 | node_cnt++; |
| 770 | spin_lock(&sbi->extent_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 771 | } |
| 772 | spin_unlock(&sbi->extent_lock); |
| 773 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 774 | unlock_out: |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 775 | mutex_unlock(&sbi->extent_tree_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 776 | out: |
| 777 | trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt); |
| 778 | |
| 779 | return node_cnt + tree_cnt; |
| 780 | } |
| 781 | |
| 782 | unsigned int f2fs_destroy_extent_node(struct inode *inode) |
| 783 | { |
| 784 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 785 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
| 786 | unsigned int node_cnt = 0; |
| 787 | |
Chao Yu | 9b72a38 | 2016-01-08 20:24:00 +0800 | [diff] [blame] | 788 | if (!et || !atomic_read(&et->node_cnt)) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 789 | return 0; |
| 790 | |
| 791 | write_lock(&et->lock); |
Hou Pengyang | 201ef5e | 2016-01-26 12:56:26 +0000 | [diff] [blame] | 792 | node_cnt = __free_extent_tree(sbi, et); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 793 | write_unlock(&et->lock); |
| 794 | |
| 795 | return node_cnt; |
| 796 | } |
| 797 | |
Jaegeuk Kim | 5f281fa | 2016-07-12 11:07:52 -0700 | [diff] [blame] | 798 | void f2fs_drop_extent_tree(struct inode *inode) |
| 799 | { |
| 800 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 801 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 802 | bool updated = false; |
Jaegeuk Kim | 5f281fa | 2016-07-12 11:07:52 -0700 | [diff] [blame] | 803 | |
Chao Yu | bf617f7 | 2018-01-27 17:29:49 +0800 | [diff] [blame] | 804 | if (!f2fs_may_extent_tree(inode)) |
| 805 | return; |
| 806 | |
Jaegeuk Kim | 5f281fa | 2016-07-12 11:07:52 -0700 | [diff] [blame] | 807 | set_inode_flag(inode, FI_NO_EXTENT); |
| 808 | |
| 809 | write_lock(&et->lock); |
| 810 | __free_extent_tree(sbi, et); |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 811 | if (et->largest.len) { |
| 812 | et->largest.len = 0; |
| 813 | updated = true; |
| 814 | } |
Jaegeuk Kim | 5f281fa | 2016-07-12 11:07:52 -0700 | [diff] [blame] | 815 | write_unlock(&et->lock); |
Zhikang Zhang | b430f72 | 2018-09-10 16:18:25 +0800 | [diff] [blame] | 816 | if (updated) |
| 817 | f2fs_mark_inode_dirty_sync(inode, true); |
Jaegeuk Kim | 5f281fa | 2016-07-12 11:07:52 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 820 | void f2fs_destroy_extent_tree(struct inode *inode) |
| 821 | { |
| 822 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 823 | struct extent_tree *et = F2FS_I(inode)->extent_tree; |
| 824 | unsigned int node_cnt = 0; |
| 825 | |
| 826 | if (!et) |
| 827 | return; |
| 828 | |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 829 | if (inode->i_nlink && !is_bad_inode(inode) && |
| 830 | atomic_read(&et->node_cnt)) { |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 831 | mutex_lock(&sbi->extent_tree_lock); |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 832 | list_add_tail(&et->list, &sbi->zombie_list); |
Jaegeuk Kim | 74fd8d9 | 2015-12-21 19:25:50 -0800 | [diff] [blame] | 833 | atomic_inc(&sbi->total_zombie_tree); |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 834 | mutex_unlock(&sbi->extent_tree_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 835 | return; |
| 836 | } |
| 837 | |
| 838 | /* free all extent info belong to this extent tree */ |
| 839 | node_cnt = f2fs_destroy_extent_node(inode); |
| 840 | |
| 841 | /* delete extent tree entry in radix tree */ |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 842 | mutex_lock(&sbi->extent_tree_lock); |
Chao Yu | 68e3538 | 2016-01-08 20:22:52 +0800 | [diff] [blame] | 843 | f2fs_bug_on(sbi, atomic_read(&et->node_cnt)); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 844 | radix_tree_delete(&sbi->extent_tree_root, inode->i_ino); |
| 845 | kmem_cache_free(extent_tree_slab, et); |
Jaegeuk Kim | 7441cce | 2015-12-21 19:20:15 -0800 | [diff] [blame] | 846 | atomic_dec(&sbi->total_ext_tree); |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 847 | mutex_unlock(&sbi->extent_tree_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 848 | |
| 849 | F2FS_I(inode)->extent_tree = NULL; |
| 850 | |
| 851 | trace_f2fs_destroy_extent_tree(inode, node_cnt); |
| 852 | } |
| 853 | |
| 854 | bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs, |
| 855 | struct extent_info *ei) |
| 856 | { |
| 857 | if (!f2fs_may_extent_tree(inode)) |
| 858 | return false; |
| 859 | |
| 860 | return f2fs_lookup_extent_tree(inode, pgofs, ei); |
| 861 | } |
| 862 | |
| 863 | void f2fs_update_extent_cache(struct dnode_of_data *dn) |
| 864 | { |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 865 | pgoff_t fofs; |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 866 | block_t blkaddr; |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 867 | |
| 868 | if (!f2fs_may_extent_tree(dn->inode)) |
| 869 | return; |
| 870 | |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 871 | if (dn->data_blkaddr == NEW_ADDR) |
| 872 | blkaddr = NULL_ADDR; |
| 873 | else |
| 874 | blkaddr = dn->data_blkaddr; |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 875 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 876 | fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) + |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 877 | dn->ofs_in_node; |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 878 | f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1); |
Chao Yu | 19b2c30 | 2015-08-26 20:34:48 +0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | void f2fs_update_extent_cache_range(struct dnode_of_data *dn, |
| 882 | pgoff_t fofs, block_t blkaddr, unsigned int len) |
| 883 | |
| 884 | { |
| 885 | if (!f2fs_may_extent_tree(dn->inode)) |
| 886 | return; |
| 887 | |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 888 | f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 889 | } |
| 890 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 891 | void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 892 | { |
| 893 | INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO); |
Yunlei He | 5e8256a | 2017-02-23 19:39:59 +0800 | [diff] [blame] | 894 | mutex_init(&sbi->extent_tree_lock); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 895 | INIT_LIST_HEAD(&sbi->extent_list); |
| 896 | spin_lock_init(&sbi->extent_lock); |
Jaegeuk Kim | 7441cce | 2015-12-21 19:20:15 -0800 | [diff] [blame] | 897 | atomic_set(&sbi->total_ext_tree, 0); |
Jaegeuk Kim | 137d09f | 2015-12-31 15:02:16 -0800 | [diff] [blame] | 898 | INIT_LIST_HEAD(&sbi->zombie_list); |
Jaegeuk Kim | 74fd8d9 | 2015-12-21 19:25:50 -0800 | [diff] [blame] | 899 | atomic_set(&sbi->total_zombie_tree, 0); |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 900 | atomic_set(&sbi->total_ext_node, 0); |
| 901 | } |
| 902 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 903 | int __init f2fs_create_extent_cache(void) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 904 | { |
| 905 | extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree", |
| 906 | sizeof(struct extent_tree)); |
| 907 | if (!extent_tree_slab) |
| 908 | return -ENOMEM; |
| 909 | extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node", |
| 910 | sizeof(struct extent_node)); |
| 911 | if (!extent_node_slab) { |
| 912 | kmem_cache_destroy(extent_tree_slab); |
| 913 | return -ENOMEM; |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 918 | void f2fs_destroy_extent_cache(void) |
Chao Yu | a28ef1f | 2015-07-08 17:59:36 +0800 | [diff] [blame] | 919 | { |
| 920 | kmem_cache_destroy(extent_node_slab); |
| 921 | kmem_cache_destroy(extent_tree_slab); |
| 922 | } |