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