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