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