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