diff options
Diffstat (limited to 'fs/btrfs/relocation.c')
| -rw-r--r-- | fs/btrfs/relocation.c | 1975 | 
1 files changed, 1217 insertions, 758 deletions
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 0b23942cbc0d..b37d723b9d4a 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c  | |||
| @@ -21,6 +21,7 @@ | |||
| 21 | #include <linux/writeback.h> | 21 | #include <linux/writeback.h> | 
| 22 | #include <linux/blkdev.h> | 22 | #include <linux/blkdev.h> | 
| 23 | #include <linux/rbtree.h> | 23 | #include <linux/rbtree.h> | 
| 24 | #include <linux/slab.h> | ||
| 24 | #include "ctree.h" | 25 | #include "ctree.h" | 
| 25 | #include "disk-io.h" | 26 | #include "disk-io.h" | 
| 26 | #include "transaction.h" | 27 | #include "transaction.h" | 
| @@ -43,8 +44,12 @@ struct tree_entry { | |||
| 43 | struct backref_node { | 44 | struct backref_node { | 
| 44 | struct rb_node rb_node; | 45 | struct rb_node rb_node; | 
| 45 | u64 bytenr; | 46 | u64 bytenr; | 
| 46 | /* objectid tree block owner */ | 47 | |
| 48 | u64 new_bytenr; | ||
| 49 | /* objectid of tree block owner, can be not uptodate */ | ||
| 47 | u64 owner; | 50 | u64 owner; | 
| 51 | /* link to pending, changed or detached list */ | ||
| 52 | struct list_head list; | ||
| 48 | /* list of upper level blocks reference this block */ | 53 | /* list of upper level blocks reference this block */ | 
| 49 | struct list_head upper; | 54 | struct list_head upper; | 
| 50 | /* list of child blocks in the cache */ | 55 | /* list of child blocks in the cache */ | 
| @@ -55,9 +60,9 @@ struct backref_node { | |||
| 55 | struct extent_buffer *eb; | 60 | struct extent_buffer *eb; | 
| 56 | /* level of tree block */ | 61 | /* level of tree block */ | 
| 57 | unsigned int level:8; | 62 | unsigned int level:8; | 
| 58 | /* 1 if the block is root of old snapshot */ | 63 | /* is the block in non-reference counted tree */ | 
| 59 | unsigned int old_root:1; | 64 | unsigned int cowonly:1; | 
| 60 | /* 1 if no child blocks in the cache */ | 65 | /* 1 if no child node in the cache */ | 
| 61 | unsigned int lowest:1; | 66 | unsigned int lowest:1; | 
| 62 | /* is the extent buffer locked */ | 67 | /* is the extent buffer locked */ | 
| 63 | unsigned int locked:1; | 68 | unsigned int locked:1; | 
| @@ -65,6 +70,16 @@ struct backref_node { | |||
| 65 | unsigned int processed:1; | 70 | unsigned int processed:1; | 
| 66 | /* have backrefs of this block been checked */ | 71 | /* have backrefs of this block been checked */ | 
| 67 | unsigned int checked:1; | 72 | unsigned int checked:1; | 
| 73 | /* | ||
| 74 | * 1 if corresponding block has been cowed but some upper | ||
| 75 | * level block pointers may not point to the new location | ||
| 76 | */ | ||
| 77 | unsigned int pending:1; | ||
| 78 | /* | ||
| 79 | * 1 if the backref node isn't connected to any other | ||
| 80 | * backref node. | ||
| 81 | */ | ||
| 82 | unsigned int detached:1; | ||
| 68 | }; | 83 | }; | 
| 69 | 84 | ||
| 70 | /* | 85 | /* | 
| @@ -73,7 +88,6 @@ struct backref_node { | |||
| 73 | struct backref_edge { | 88 | struct backref_edge { | 
| 74 | struct list_head list[2]; | 89 | struct list_head list[2]; | 
| 75 | struct backref_node *node[2]; | 90 | struct backref_node *node[2]; | 
| 76 | u64 blockptr; | ||
| 77 | }; | 91 | }; | 
| 78 | 92 | ||
| 79 | #define LOWER 0 | 93 | #define LOWER 0 | 
| @@ -82,9 +96,25 @@ struct backref_edge { | |||
| 82 | struct backref_cache { | 96 | struct backref_cache { | 
| 83 | /* red black tree of all backref nodes in the cache */ | 97 | /* red black tree of all backref nodes in the cache */ | 
| 84 | struct rb_root rb_root; | 98 | struct rb_root rb_root; | 
| 85 | /* list of backref nodes with no child block in the cache */ | 99 | /* for passing backref nodes to btrfs_reloc_cow_block */ | 
| 100 | struct backref_node *path[BTRFS_MAX_LEVEL]; | ||
| 101 | /* | ||
| 102 | * list of blocks that have been cowed but some block | ||
| 103 | * pointers in upper level blocks may not reflect the | ||
| 104 | * new location | ||
| 105 | */ | ||
| 86 | struct list_head pending[BTRFS_MAX_LEVEL]; | 106 | struct list_head pending[BTRFS_MAX_LEVEL]; | 
| 87 | spinlock_t lock; | 107 | /* list of backref nodes with no child node */ | 
| 108 | struct list_head leaves; | ||
| 109 | /* list of blocks that have been cowed in current transaction */ | ||
| 110 | struct list_head changed; | ||
| 111 | /* list of detached backref node. */ | ||
| 112 | struct list_head detached; | ||
| 113 | |||
| 114 | u64 last_trans; | ||
| 115 | |||
| 116 | int nr_nodes; | ||
| 117 | int nr_edges; | ||
| 88 | }; | 118 | }; | 
| 89 | 119 | ||
| 90 | /* | 120 | /* | 
| @@ -112,15 +142,6 @@ struct tree_block { | |||
| 112 | unsigned int key_ready:1; | 142 | unsigned int key_ready:1; | 
| 113 | }; | 143 | }; | 
| 114 | 144 | ||
| 115 | /* inode vector */ | ||
| 116 | #define INODEVEC_SIZE 16 | ||
| 117 | |||
| 118 | struct inodevec { | ||
| 119 | struct list_head list; | ||
| 120 | struct inode *inode[INODEVEC_SIZE]; | ||
| 121 | int nr; | ||
| 122 | }; | ||
| 123 | |||
| 124 | #define MAX_EXTENTS 128 | 145 | #define MAX_EXTENTS 128 | 
| 125 | 146 | ||
| 126 | struct file_extent_cluster { | 147 | struct file_extent_cluster { | 
| @@ -137,36 +158,43 @@ struct reloc_control { | |||
| 137 | struct btrfs_root *extent_root; | 158 | struct btrfs_root *extent_root; | 
| 138 | /* inode for moving data */ | 159 | /* inode for moving data */ | 
| 139 | struct inode *data_inode; | 160 | struct inode *data_inode; | 
| 140 | struct btrfs_workers workers; | 161 | |
| 162 | struct btrfs_block_rsv *block_rsv; | ||
| 163 | |||
| 164 | struct backref_cache backref_cache; | ||
| 165 | |||
| 166 | struct file_extent_cluster cluster; | ||
| 141 | /* tree blocks have been processed */ | 167 | /* tree blocks have been processed */ | 
| 142 | struct extent_io_tree processed_blocks; | 168 | struct extent_io_tree processed_blocks; | 
| 143 | /* map start of tree root to corresponding reloc tree */ | 169 | /* map start of tree root to corresponding reloc tree */ | 
| 144 | struct mapping_tree reloc_root_tree; | 170 | struct mapping_tree reloc_root_tree; | 
| 145 | /* list of reloc trees */ | 171 | /* list of reloc trees */ | 
| 146 | struct list_head reloc_roots; | 172 | struct list_head reloc_roots; | 
| 173 | /* size of metadata reservation for merging reloc trees */ | ||
| 174 | u64 merging_rsv_size; | ||
| 175 | /* size of relocated tree nodes */ | ||
| 176 | u64 nodes_relocated; | ||
| 177 | |||
| 147 | u64 search_start; | 178 | u64 search_start; | 
| 148 | u64 extents_found; | 179 | u64 extents_found; | 
| 149 | u64 extents_skipped; | 180 | |
| 150 | int stage; | 181 | int block_rsv_retries; | 
| 151 | int create_reloc_root; | 182 | |
| 183 | unsigned int stage:8; | ||
| 184 | unsigned int create_reloc_tree:1; | ||
| 185 | unsigned int merge_reloc_tree:1; | ||
| 152 | unsigned int found_file_extent:1; | 186 | unsigned int found_file_extent:1; | 
| 153 | unsigned int found_old_snapshot:1; | 187 | unsigned int commit_transaction:1; | 
| 154 | }; | 188 | }; | 
| 155 | 189 | ||
| 156 | /* stages of data relocation */ | 190 | /* stages of data relocation */ | 
| 157 | #define MOVE_DATA_EXTENTS 0 | 191 | #define MOVE_DATA_EXTENTS 0 | 
| 158 | #define UPDATE_DATA_PTRS 1 | 192 | #define UPDATE_DATA_PTRS 1 | 
| 159 | 193 | ||
| 160 | /* | 194 | static void remove_backref_node(struct backref_cache *cache, | 
| 161 | * merge reloc tree to corresponding fs tree in worker threads | 195 | struct backref_node *node); | 
| 162 | */ | 196 | static void __mark_block_processed(struct reloc_control *rc, | 
| 163 | struct async_merge { | 197 | struct backref_node *node); | 
| 164 | struct btrfs_work work; | ||
| 165 | struct reloc_control *rc; | ||
| 166 | struct btrfs_root *root; | ||
| 167 | struct completion *done; | ||
| 168 | atomic_t *num_pending; | ||
| 169 | }; | ||
| 170 | 198 | ||
| 171 | static void mapping_tree_init(struct mapping_tree *tree) | 199 | static void mapping_tree_init(struct mapping_tree *tree) | 
| 172 | { | 200 | { | 
| @@ -180,15 +208,80 @@ static void backref_cache_init(struct backref_cache *cache) | |||
| 180 | cache->rb_root = RB_ROOT; | 208 | cache->rb_root = RB_ROOT; | 
| 181 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) | 209 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) | 
| 182 | INIT_LIST_HEAD(&cache->pending[i]); | 210 | INIT_LIST_HEAD(&cache->pending[i]); | 
| 183 | spin_lock_init(&cache->lock); | 211 | INIT_LIST_HEAD(&cache->changed); | 
| 212 | INIT_LIST_HEAD(&cache->detached); | ||
| 213 | INIT_LIST_HEAD(&cache->leaves); | ||
| 214 | } | ||
| 215 | |||
| 216 | static void backref_cache_cleanup(struct backref_cache *cache) | ||
| 217 | { | ||
| 218 | struct backref_node *node; | ||
| 219 | int i; | ||
| 220 | |||
| 221 | while (!list_empty(&cache->detached)) { | ||
| 222 | node = list_entry(cache->detached.next, | ||
| 223 | struct backref_node, list); | ||
| 224 | remove_backref_node(cache, node); | ||
| 225 | } | ||
| 226 | |||
| 227 | while (!list_empty(&cache->leaves)) { | ||
| 228 | node = list_entry(cache->leaves.next, | ||
| 229 | struct backref_node, lower); | ||
| 230 | remove_backref_node(cache, node); | ||
| 231 | } | ||
| 232 | |||
| 233 | cache->last_trans = 0; | ||
| 234 | |||
| 235 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) | ||
| 236 | BUG_ON(!list_empty(&cache->pending[i])); | ||
| 237 | BUG_ON(!list_empty(&cache->changed)); | ||
| 238 | BUG_ON(!list_empty(&cache->detached)); | ||
| 239 | BUG_ON(!RB_EMPTY_ROOT(&cache->rb_root)); | ||
| 240 | BUG_ON(cache->nr_nodes); | ||
| 241 | BUG_ON(cache->nr_edges); | ||
| 242 | } | ||
| 243 | |||
| 244 | static struct backref_node *alloc_backref_node(struct backref_cache *cache) | ||
| 245 | { | ||
| 246 | struct backref_node *node; | ||
| 247 | |||
| 248 | node = kzalloc(sizeof(*node), GFP_NOFS); | ||
| 249 | if (node) { | ||
| 250 | INIT_LIST_HEAD(&node->list); | ||
| 251 | INIT_LIST_HEAD(&node->upper); | ||
| 252 | INIT_LIST_HEAD(&node->lower); | ||
| 253 | RB_CLEAR_NODE(&node->rb_node); | ||
| 254 | cache->nr_nodes++; | ||
| 255 | } | ||
| 256 | return node; | ||
| 257 | } | ||
| 258 | |||
| 259 | static void free_backref_node(struct backref_cache *cache, | ||
| 260 | struct backref_node *node) | ||
| 261 | { | ||
| 262 | if (node) { | ||
| 263 | cache->nr_nodes--; | ||
| 264 | kfree(node); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | static struct backref_edge *alloc_backref_edge(struct backref_cache *cache) | ||
| 269 | { | ||
| 270 | struct backref_edge *edge; | ||
| 271 | |||
| 272 | edge = kzalloc(sizeof(*edge), GFP_NOFS); | ||
| 273 | if (edge) | ||
| 274 | cache->nr_edges++; | ||
| 275 | return edge; | ||
| 184 | } | 276 | } | 
| 185 | 277 | ||
| 186 | static void backref_node_init(struct backref_node *node) | 278 | static void free_backref_edge(struct backref_cache *cache, | 
| 279 | struct backref_edge *edge) | ||
| 187 | { | 280 | { | 
| 188 | memset(node, 0, sizeof(*node)); | 281 | if (edge) { | 
| 189 | INIT_LIST_HEAD(&node->upper); | 282 | cache->nr_edges--; | 
| 190 | INIT_LIST_HEAD(&node->lower); | 283 | kfree(edge); | 
| 191 | RB_CLEAR_NODE(&node->rb_node); | 284 | } | 
| 192 | } | 285 | } | 
| 193 | 286 | ||
| 194 | static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr, | 287 | static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr, | 
| @@ -249,6 +342,7 @@ static struct backref_node *walk_up_backref(struct backref_node *node, | |||
| 249 | edges[idx++] = edge; | 342 | edges[idx++] = edge; | 
| 250 | node = edge->node[UPPER]; | 343 | node = edge->node[UPPER]; | 
| 251 | } | 344 | } | 
| 345 | BUG_ON(node->detached); | ||
| 252 | *index = idx; | 346 | *index = idx; | 
| 253 | return node; | 347 | return node; | 
| 254 | } | 348 | } | 
| @@ -280,13 +374,18 @@ static struct backref_node *walk_down_backref(struct backref_edge *edges[], | |||
| 280 | return NULL; | 374 | return NULL; | 
| 281 | } | 375 | } | 
| 282 | 376 | ||
| 377 | static void unlock_node_buffer(struct backref_node *node) | ||
| 378 | { | ||
| 379 | if (node->locked) { | ||
| 380 | btrfs_tree_unlock(node->eb); | ||
| 381 | node->locked = 0; | ||
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 283 | static void drop_node_buffer(struct backref_node *node) | 385 | static void drop_node_buffer(struct backref_node *node) | 
| 284 | { | 386 | { | 
| 285 | if (node->eb) { | 387 | if (node->eb) { | 
| 286 | if (node->locked) { | 388 | unlock_node_buffer(node); | 
| 287 | btrfs_tree_unlock(node->eb); | ||
| 288 | node->locked = 0; | ||
| 289 | } | ||
| 290 | free_extent_buffer(node->eb); | 389 | free_extent_buffer(node->eb); | 
| 291 | node->eb = NULL; | 390 | node->eb = NULL; | 
| 292 | } | 391 | } | 
| @@ -295,14 +394,14 @@ static void drop_node_buffer(struct backref_node *node) | |||
| 295 | static void drop_backref_node(struct backref_cache *tree, | 394 | static void drop_backref_node(struct backref_cache *tree, | 
| 296 | struct backref_node *node) | 395 | struct backref_node *node) | 
| 297 | { | 396 | { | 
| 298 | BUG_ON(!node->lowest); | ||
| 299 | BUG_ON(!list_empty(&node->upper)); | 397 | BUG_ON(!list_empty(&node->upper)); | 
| 300 | 398 | ||
| 301 | drop_node_buffer(node); | 399 | drop_node_buffer(node); | 
| 400 | list_del(&node->list); | ||
| 302 | list_del(&node->lower); | 401 | list_del(&node->lower); | 
| 303 | 402 | if (!RB_EMPTY_NODE(&node->rb_node)) | |
| 304 | rb_erase(&node->rb_node, &tree->rb_root); | 403 | rb_erase(&node->rb_node, &tree->rb_root); | 
| 305 | kfree(node); | 404 | free_backref_node(tree, node); | 
| 306 | } | 405 | } | 
| 307 | 406 | ||
| 308 | /* | 407 | /* | 
| @@ -317,27 +416,121 @@ static void remove_backref_node(struct backref_cache *cache, | |||
| 317 | if (!node) | 416 | if (!node) | 
| 318 | return; | 417 | return; | 
| 319 | 418 | ||
| 320 | BUG_ON(!node->lowest); | 419 | BUG_ON(!node->lowest && !node->detached); | 
| 321 | while (!list_empty(&node->upper)) { | 420 | while (!list_empty(&node->upper)) { | 
| 322 | edge = list_entry(node->upper.next, struct backref_edge, | 421 | edge = list_entry(node->upper.next, struct backref_edge, | 
| 323 | list[LOWER]); | 422 | list[LOWER]); | 
| 324 | upper = edge->node[UPPER]; | 423 | upper = edge->node[UPPER]; | 
| 325 | list_del(&edge->list[LOWER]); | 424 | list_del(&edge->list[LOWER]); | 
| 326 | list_del(&edge->list[UPPER]); | 425 | list_del(&edge->list[UPPER]); | 
| 327 | kfree(edge); | 426 | free_backref_edge(cache, edge); | 
| 427 | |||
| 428 | if (RB_EMPTY_NODE(&upper->rb_node)) { | ||
| 429 | BUG_ON(!list_empty(&node->upper)); | ||
| 430 | drop_backref_node(cache, node); | ||
| 431 | node = upper; | ||
| 432 | node->lowest = 1; | ||
| 433 | continue; | ||
| 434 | } | ||
| 328 | /* | 435 | /* | 
| 329 | * add the node to pending list if no other | 436 | * add the node to leaf node list if no other | 
| 330 | * child block cached. | 437 | * child block cached. | 
| 331 | */ | 438 | */ | 
| 332 | if (list_empty(&upper->lower)) { | 439 | if (list_empty(&upper->lower)) { | 
| 333 | list_add_tail(&upper->lower, | 440 | list_add_tail(&upper->lower, &cache->leaves); | 
| 334 | &cache->pending[upper->level]); | ||
| 335 | upper->lowest = 1; | 441 | upper->lowest = 1; | 
| 336 | } | 442 | } | 
| 337 | } | 443 | } | 
| 444 | |||
| 338 | drop_backref_node(cache, node); | 445 | drop_backref_node(cache, node); | 
| 339 | } | 446 | } | 
| 340 | 447 | ||
| 448 | static void update_backref_node(struct backref_cache *cache, | ||
| 449 | struct backref_node *node, u64 bytenr) | ||
| 450 | { | ||
| 451 | struct rb_node *rb_node; | ||
| 452 | rb_erase(&node->rb_node, &cache->rb_root); | ||
| 453 | node->bytenr = bytenr; | ||
| 454 | rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node); | ||
| 455 | BUG_ON(rb_node); | ||
| 456 | } | ||
| 457 | |||
| 458 | /* | ||
| 459 | * update backref cache after a transaction commit | ||
| 460 | */ | ||
| 461 | static int update_backref_cache(struct btrfs_trans_handle *trans, | ||
| 462 | struct backref_cache *cache) | ||
| 463 | { | ||
| 464 | struct backref_node *node; | ||
| 465 | int level = 0; | ||
| 466 | |||
| 467 | if (cache->last_trans == 0) { | ||
| 468 | cache->last_trans = trans->transid; | ||
| 469 | return 0; | ||
| 470 | } | ||
| 471 | |||
| 472 | if (cache->last_trans == trans->transid) | ||
| 473 | return 0; | ||
| 474 | |||
| 475 | /* | ||
| 476 | * detached nodes are used to avoid unnecessary backref | ||
| 477 | * lookup. transaction commit changes the extent tree. | ||
| 478 | * so the detached nodes are no longer useful. | ||
| 479 | */ | ||
| 480 | while (!list_empty(&cache->detached)) { | ||
| 481 | node = list_entry(cache->detached.next, | ||
| 482 | struct backref_node, list); | ||
| 483 | remove_backref_node(cache, node); | ||
| 484 | } | ||
| 485 | |||
| 486 | while (!list_empty(&cache->changed)) { | ||
| 487 | node = list_entry(cache->changed.next, | ||
| 488 | struct backref_node, list); | ||
| 489 | list_del_init(&node->list); | ||
| 490 | BUG_ON(node->pending); | ||
| 491 | update_backref_node(cache, node, node->new_bytenr); | ||
| 492 | } | ||
| 493 | |||
| 494 | /* | ||
| 495 | * some nodes can be left in the pending list if there were | ||
| 496 | * errors during processing the pending nodes. | ||
| 497 | */ | ||
| 498 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { | ||
| 499 | list_for_each_entry(node, &cache->pending[level], list) { | ||
| 500 | BUG_ON(!node->pending); | ||
| 501 | if (node->bytenr == node->new_bytenr) | ||
| 502 | continue; | ||
| 503 | update_backref_node(cache, node, node->new_bytenr); | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | cache->last_trans = 0; | ||
| 508 | return 1; | ||
| 509 | } | ||
| 510 | |||
| 511 | static int should_ignore_root(struct btrfs_root *root) | ||
| 512 | { | ||
| 513 | struct btrfs_root *reloc_root; | ||
| 514 | |||
| 515 | if (!root->ref_cows) | ||
| 516 | return 0; | ||
| 517 | |||
| 518 | reloc_root = root->reloc_root; | ||
| 519 | if (!reloc_root) | ||
| 520 | return 0; | ||
| 521 | |||
| 522 | if (btrfs_root_last_snapshot(&reloc_root->root_item) == | ||
| 523 | root->fs_info->running_transaction->transid - 1) | ||
| 524 | return 0; | ||
| 525 | /* | ||
| 526 | * if there is reloc tree and it was created in previous | ||
| 527 | * transaction backref lookup can find the reloc tree, | ||
| 528 | * so backref node for the fs tree root is useless for | ||
| 529 | * relocation. | ||
| 530 | */ | ||
| 531 | return 1; | ||
| 532 | } | ||
| 533 | |||
| 341 | /* | 534 | /* | 
| 342 | * find reloc tree by address of tree root | 535 | * find reloc tree by address of tree root | 
| 343 | */ | 536 | */ | 
| @@ -452,11 +645,12 @@ int find_inline_backref(struct extent_buffer *leaf, int slot, | |||
| 452 | * for all upper level blocks that directly/indirectly reference the | 645 | * for all upper level blocks that directly/indirectly reference the | 
| 453 | * block are also cached. | 646 | * block are also cached. | 
| 454 | */ | 647 | */ | 
| 455 | static struct backref_node *build_backref_tree(struct reloc_control *rc, | 648 | static noinline_for_stack | 
| 456 | struct backref_cache *cache, | 649 | struct backref_node *build_backref_tree(struct reloc_control *rc, | 
| 457 | struct btrfs_key *node_key, | 650 | struct btrfs_key *node_key, | 
| 458 | int level, u64 bytenr) | 651 | int level, u64 bytenr) | 
| 459 | { | 652 | { | 
| 653 | struct backref_cache *cache = &rc->backref_cache; | ||
| 460 | struct btrfs_path *path1; | 654 | struct btrfs_path *path1; | 
| 461 | struct btrfs_path *path2; | 655 | struct btrfs_path *path2; | 
| 462 | struct extent_buffer *eb; | 656 | struct extent_buffer *eb; | 
| @@ -472,6 +666,8 @@ static struct backref_node *build_backref_tree(struct reloc_control *rc, | |||
| 472 | unsigned long end; | 666 | unsigned long end; | 
| 473 | unsigned long ptr; | 667 | unsigned long ptr; | 
| 474 | LIST_HEAD(list); | 668 | LIST_HEAD(list); | 
| 669 | LIST_HEAD(useless); | ||
| 670 | int cowonly; | ||
| 475 | int ret; | 671 | int ret; | 
| 476 | int err = 0; | 672 | int err = 0; | 
| 477 | 673 | ||
| @@ -482,15 +678,13 @@ static struct backref_node *build_backref_tree(struct reloc_control *rc, | |||
| 482 | goto out; | 678 | goto out; | 
| 483 | } | 679 | } | 
| 484 | 680 | ||
| 485 | node = kmalloc(sizeof(*node), GFP_NOFS); | 681 | node = alloc_backref_node(cache); | 
| 486 | if (!node) { | 682 | if (!node) { | 
| 487 | err = -ENOMEM; | 683 | err = -ENOMEM; | 
| 488 | goto out; | 684 | goto out; | 
| 489 | } | 685 | } | 
| 490 | 686 | ||
| 491 | backref_node_init(node); | ||
| 492 | node->bytenr = bytenr; | 687 | node->bytenr = bytenr; | 
| 493 | node->owner = 0; | ||
| 494 | node->level = level; | 688 | node->level = level; | 
| 495 | node->lowest = 1; | 689 | node->lowest = 1; | 
| 496 | cur = node; | 690 | cur = node; | 
| @@ -586,17 +780,21 @@ again: | |||
| 586 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | 780 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | 
| 587 | if (key.type == BTRFS_SHARED_BLOCK_REF_KEY || | 781 | if (key.type == BTRFS_SHARED_BLOCK_REF_KEY || | 
| 588 | key.type == BTRFS_EXTENT_REF_V0_KEY) { | 782 | key.type == BTRFS_EXTENT_REF_V0_KEY) { | 
| 589 | if (key.objectid == key.offset && | 783 | if (key.type == BTRFS_EXTENT_REF_V0_KEY) { | 
| 590 | key.type == BTRFS_EXTENT_REF_V0_KEY) { | ||
| 591 | struct btrfs_extent_ref_v0 *ref0; | 784 | struct btrfs_extent_ref_v0 *ref0; | 
| 592 | ref0 = btrfs_item_ptr(eb, path1->slots[0], | 785 | ref0 = btrfs_item_ptr(eb, path1->slots[0], | 
| 593 | struct btrfs_extent_ref_v0); | 786 | struct btrfs_extent_ref_v0); | 
| 594 | root = find_tree_root(rc, eb, ref0); | 787 | if (key.objectid == key.offset) { | 
| 595 | if (root) | 788 | root = find_tree_root(rc, eb, ref0); | 
| 596 | cur->root = root; | 789 | if (root && !should_ignore_root(root)) | 
| 597 | else | 790 | cur->root = root; | 
| 598 | cur->old_root = 1; | 791 | else | 
| 599 | break; | 792 | list_add(&cur->list, &useless); | 
| 793 | break; | ||
| 794 | } | ||
| 795 | if (is_cowonly_root(btrfs_ref_root_v0(eb, | ||
| 796 | ref0))) | ||
| 797 | cur->cowonly = 1; | ||
| 600 | } | 798 | } | 
| 601 | #else | 799 | #else | 
| 602 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); | 800 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); | 
| @@ -613,22 +811,20 @@ again: | |||
| 613 | break; | 811 | break; | 
| 614 | } | 812 | } | 
| 615 | 813 | ||
| 616 | edge = kzalloc(sizeof(*edge), GFP_NOFS); | 814 | edge = alloc_backref_edge(cache); | 
| 617 | if (!edge) { | 815 | if (!edge) { | 
| 618 | err = -ENOMEM; | 816 | err = -ENOMEM; | 
| 619 | goto out; | 817 | goto out; | 
| 620 | } | 818 | } | 
| 621 | rb_node = tree_search(&cache->rb_root, key.offset); | 819 | rb_node = tree_search(&cache->rb_root, key.offset); | 
| 622 | if (!rb_node) { | 820 | if (!rb_node) { | 
| 623 | upper = kmalloc(sizeof(*upper), GFP_NOFS); | 821 | upper = alloc_backref_node(cache); | 
| 624 | if (!upper) { | 822 | if (!upper) { | 
| 625 | kfree(edge); | 823 | free_backref_edge(cache, edge); | 
| 626 | err = -ENOMEM; | 824 | err = -ENOMEM; | 
| 627 | goto out; | 825 | goto out; | 
| 628 | } | 826 | } | 
| 629 | backref_node_init(upper); | ||
| 630 | upper->bytenr = key.offset; | 827 | upper->bytenr = key.offset; | 
| 631 | upper->owner = 0; | ||
| 632 | upper->level = cur->level + 1; | 828 | upper->level = cur->level + 1; | 
| 633 | /* | 829 | /* | 
| 634 | * backrefs for the upper level block isn't | 830 | * backrefs for the upper level block isn't | 
| @@ -638,11 +834,12 @@ again: | |||
| 638 | } else { | 834 | } else { | 
| 639 | upper = rb_entry(rb_node, struct backref_node, | 835 | upper = rb_entry(rb_node, struct backref_node, | 
| 640 | rb_node); | 836 | rb_node); | 
| 837 | BUG_ON(!upper->checked); | ||
| 641 | INIT_LIST_HEAD(&edge->list[UPPER]); | 838 | INIT_LIST_HEAD(&edge->list[UPPER]); | 
| 642 | } | 839 | } | 
| 643 | list_add(&edge->list[LOWER], &cur->upper); | 840 | list_add_tail(&edge->list[LOWER], &cur->upper); | 
| 644 | edge->node[UPPER] = upper; | ||
| 645 | edge->node[LOWER] = cur; | 841 | edge->node[LOWER] = cur; | 
| 842 | edge->node[UPPER] = upper; | ||
| 646 | 843 | ||
| 647 | goto next; | 844 | goto next; | 
| 648 | } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { | 845 | } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { | 
| @@ -656,11 +853,17 @@ again: | |||
| 656 | goto out; | 853 | goto out; | 
| 657 | } | 854 | } | 
| 658 | 855 | ||
| 856 | if (!root->ref_cows) | ||
| 857 | cur->cowonly = 1; | ||
| 858 | |||
| 659 | if (btrfs_root_level(&root->root_item) == cur->level) { | 859 | if (btrfs_root_level(&root->root_item) == cur->level) { | 
| 660 | /* tree root */ | 860 | /* tree root */ | 
| 661 | BUG_ON(btrfs_root_bytenr(&root->root_item) != | 861 | BUG_ON(btrfs_root_bytenr(&root->root_item) != | 
| 662 | cur->bytenr); | 862 | cur->bytenr); | 
| 663 | cur->root = root; | 863 | if (should_ignore_root(root)) | 
| 864 | list_add(&cur->list, &useless); | ||
| 865 | else | ||
| 866 | cur->root = root; | ||
| 664 | break; | 867 | break; | 
| 665 | } | 868 | } | 
| 666 | 869 | ||
| @@ -691,11 +894,14 @@ again: | |||
| 691 | if (!path2->nodes[level]) { | 894 | if (!path2->nodes[level]) { | 
| 692 | BUG_ON(btrfs_root_bytenr(&root->root_item) != | 895 | BUG_ON(btrfs_root_bytenr(&root->root_item) != | 
| 693 | lower->bytenr); | 896 | lower->bytenr); | 
| 694 | lower->root = root; | 897 | if (should_ignore_root(root)) | 
| 898 | list_add(&lower->list, &useless); | ||
| 899 | else | ||
| 900 | lower->root = root; | ||
| 695 | break; | 901 | break; | 
| 696 | } | 902 | } | 
| 697 | 903 | ||
| 698 | edge = kzalloc(sizeof(*edge), GFP_NOFS); | 904 | edge = alloc_backref_edge(cache); | 
| 699 | if (!edge) { | 905 | if (!edge) { | 
| 700 | err = -ENOMEM; | 906 | err = -ENOMEM; | 
| 701 | goto out; | 907 | goto out; | 
| @@ -704,16 +910,17 @@ again: | |||
| 704 | eb = path2->nodes[level]; | 910 | eb = path2->nodes[level]; | 
| 705 | rb_node = tree_search(&cache->rb_root, eb->start); | 911 | rb_node = tree_search(&cache->rb_root, eb->start); | 
| 706 | if (!rb_node) { | 912 | if (!rb_node) { | 
| 707 | upper = kmalloc(sizeof(*upper), GFP_NOFS); | 913 | upper = alloc_backref_node(cache); | 
| 708 | if (!upper) { | 914 | if (!upper) { | 
| 709 | kfree(edge); | 915 | free_backref_edge(cache, edge); | 
| 710 | err = -ENOMEM; | 916 | err = -ENOMEM; | 
| 711 | goto out; | 917 | goto out; | 
| 712 | } | 918 | } | 
| 713 | backref_node_init(upper); | ||
| 714 | upper->bytenr = eb->start; | 919 | upper->bytenr = eb->start; | 
| 715 | upper->owner = btrfs_header_owner(eb); | 920 | upper->owner = btrfs_header_owner(eb); | 
| 716 | upper->level = lower->level + 1; | 921 | upper->level = lower->level + 1; | 
| 922 | if (!root->ref_cows) | ||
| 923 | upper->cowonly = 1; | ||
| 717 | 924 | ||
| 718 | /* | 925 | /* | 
| 719 | * if we know the block isn't shared | 926 | * if we know the block isn't shared | 
| @@ -743,10 +950,12 @@ again: | |||
| 743 | rb_node); | 950 | rb_node); | 
| 744 | BUG_ON(!upper->checked); | 951 | BUG_ON(!upper->checked); | 
| 745 | INIT_LIST_HEAD(&edge->list[UPPER]); | 952 | INIT_LIST_HEAD(&edge->list[UPPER]); | 
| 953 | if (!upper->owner) | ||
| 954 | upper->owner = btrfs_header_owner(eb); | ||
| 746 | } | 955 | } | 
| 747 | list_add_tail(&edge->list[LOWER], &lower->upper); | 956 | list_add_tail(&edge->list[LOWER], &lower->upper); | 
| 748 | edge->node[UPPER] = upper; | ||
| 749 | edge->node[LOWER] = lower; | 957 | edge->node[LOWER] = lower; | 
| 958 | edge->node[UPPER] = upper; | ||
| 750 | 959 | ||
| 751 | if (rb_node) | 960 | if (rb_node) | 
| 752 | break; | 961 | break; | 
| @@ -784,8 +993,13 @@ next: | |||
| 784 | * into the cache. | 993 | * into the cache. | 
| 785 | */ | 994 | */ | 
| 786 | BUG_ON(!node->checked); | 995 | BUG_ON(!node->checked); | 
| 787 | rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node); | 996 | cowonly = node->cowonly; | 
| 788 | BUG_ON(rb_node); | 997 | if (!cowonly) { | 
| 998 | rb_node = tree_insert(&cache->rb_root, node->bytenr, | ||
| 999 | &node->rb_node); | ||
| 1000 | BUG_ON(rb_node); | ||
| 1001 | list_add_tail(&node->lower, &cache->leaves); | ||
| 1002 | } | ||
| 789 | 1003 | ||
| 790 | list_for_each_entry(edge, &node->upper, list[LOWER]) | 1004 | list_for_each_entry(edge, &node->upper, list[LOWER]) | 
| 791 | list_add_tail(&edge->list[UPPER], &list); | 1005 | list_add_tail(&edge->list[UPPER], &list); | 
| @@ -794,6 +1008,14 @@ next: | |||
| 794 | edge = list_entry(list.next, struct backref_edge, list[UPPER]); | 1008 | edge = list_entry(list.next, struct backref_edge, list[UPPER]); | 
| 795 | list_del_init(&edge->list[UPPER]); | 1009 | list_del_init(&edge->list[UPPER]); | 
| 796 | upper = edge->node[UPPER]; | 1010 | upper = edge->node[UPPER]; | 
| 1011 | if (upper->detached) { | ||
| 1012 | list_del(&edge->list[LOWER]); | ||
| 1013 | lower = edge->node[LOWER]; | ||
| 1014 | free_backref_edge(cache, edge); | ||
| 1015 | if (list_empty(&lower->upper)) | ||
| 1016 | list_add(&lower->list, &useless); | ||
| 1017 | continue; | ||
| 1018 | } | ||
| 797 | 1019 | ||
| 798 | if (!RB_EMPTY_NODE(&upper->rb_node)) { | 1020 | if (!RB_EMPTY_NODE(&upper->rb_node)) { | 
| 799 | if (upper->lowest) { | 1021 | if (upper->lowest) { | 
| @@ -806,25 +1028,69 @@ next: | |||
| 806 | } | 1028 | } | 
| 807 | 1029 | ||
| 808 | BUG_ON(!upper->checked); | 1030 | BUG_ON(!upper->checked); | 
| 809 | rb_node = tree_insert(&cache->rb_root, upper->bytenr, | 1031 | BUG_ON(cowonly != upper->cowonly); | 
| 810 | &upper->rb_node); | 1032 | if (!cowonly) { | 
| 811 | BUG_ON(rb_node); | 1033 | rb_node = tree_insert(&cache->rb_root, upper->bytenr, | 
| 1034 | &upper->rb_node); | ||
| 1035 | BUG_ON(rb_node); | ||
| 1036 | } | ||
| 812 | 1037 | ||
| 813 | list_add_tail(&edge->list[UPPER], &upper->lower); | 1038 | list_add_tail(&edge->list[UPPER], &upper->lower); | 
| 814 | 1039 | ||
| 815 | list_for_each_entry(edge, &upper->upper, list[LOWER]) | 1040 | list_for_each_entry(edge, &upper->upper, list[LOWER]) | 
| 816 | list_add_tail(&edge->list[UPPER], &list); | 1041 | list_add_tail(&edge->list[UPPER], &list); | 
| 817 | } | 1042 | } | 
| 1043 | /* | ||
| 1044 | * process useless backref nodes. backref nodes for tree leaves | ||
| 1045 | * are deleted from the cache. backref nodes for upper level | ||
| 1046 | * tree blocks are left in the cache to avoid unnecessary backref | ||
| 1047 | * lookup. | ||
| 1048 | */ | ||
| 1049 | while (!list_empty(&useless)) { | ||
| 1050 | upper = list_entry(useless.next, struct backref_node, list); | ||
| 1051 | list_del_init(&upper->list); | ||
| 1052 | BUG_ON(!list_empty(&upper->upper)); | ||
| 1053 | if (upper == node) | ||
| 1054 | node = NULL; | ||
| 1055 | if (upper->lowest) { | ||
| 1056 | list_del_init(&upper->lower); | ||
| 1057 | upper->lowest = 0; | ||
| 1058 | } | ||
| 1059 | while (!list_empty(&upper->lower)) { | ||
| 1060 | edge = list_entry(upper->lower.next, | ||
| 1061 | struct backref_edge, list[UPPER]); | ||
| 1062 | list_del(&edge->list[UPPER]); | ||
| 1063 | list_del(&edge->list[LOWER]); | ||
| 1064 | lower = edge->node[LOWER]; | ||
| 1065 | free_backref_edge(cache, edge); | ||
| 1066 | |||
| 1067 | if (list_empty(&lower->upper)) | ||
| 1068 | list_add(&lower->list, &useless); | ||
| 1069 | } | ||
| 1070 | __mark_block_processed(rc, upper); | ||
| 1071 | if (upper->level > 0) { | ||
| 1072 | list_add(&upper->list, &cache->detached); | ||
| 1073 | upper->detached = 1; | ||
| 1074 | } else { | ||
| 1075 | rb_erase(&upper->rb_node, &cache->rb_root); | ||
| 1076 | free_backref_node(cache, upper); | ||
| 1077 | } | ||
| 1078 | } | ||
| 818 | out: | 1079 | out: | 
| 819 | btrfs_free_path(path1); | 1080 | btrfs_free_path(path1); | 
| 820 | btrfs_free_path(path2); | 1081 | btrfs_free_path(path2); | 
| 821 | if (err) { | 1082 | if (err) { | 
| 822 | INIT_LIST_HEAD(&list); | 1083 | while (!list_empty(&useless)) { | 
| 1084 | lower = list_entry(useless.next, | ||
| 1085 | struct backref_node, upper); | ||
| 1086 | list_del_init(&lower->upper); | ||
| 1087 | } | ||
| 823 | upper = node; | 1088 | upper = node; | 
| 1089 | INIT_LIST_HEAD(&list); | ||
| 824 | while (upper) { | 1090 | while (upper) { | 
| 825 | if (RB_EMPTY_NODE(&upper->rb_node)) { | 1091 | if (RB_EMPTY_NODE(&upper->rb_node)) { | 
| 826 | list_splice_tail(&upper->upper, &list); | 1092 | list_splice_tail(&upper->upper, &list); | 
| 827 | kfree(upper); | 1093 | free_backref_node(cache, upper); | 
| 828 | } | 1094 | } | 
| 829 | 1095 | ||
| 830 | if (list_empty(&list)) | 1096 | if (list_empty(&list)) | 
| @@ -832,15 +1098,104 @@ out: | |||
| 832 | 1098 | ||
| 833 | edge = list_entry(list.next, struct backref_edge, | 1099 | edge = list_entry(list.next, struct backref_edge, | 
| 834 | list[LOWER]); | 1100 | list[LOWER]); | 
| 1101 | list_del(&edge->list[LOWER]); | ||
| 835 | upper = edge->node[UPPER]; | 1102 | upper = edge->node[UPPER]; | 
| 836 | kfree(edge); | 1103 | free_backref_edge(cache, edge); | 
| 837 | } | 1104 | } | 
| 838 | return ERR_PTR(err); | 1105 | return ERR_PTR(err); | 
| 839 | } | 1106 | } | 
| 1107 | BUG_ON(node && node->detached); | ||
| 840 | return node; | 1108 | return node; | 
| 841 | } | 1109 | } | 
| 842 | 1110 | ||
| 843 | /* | 1111 | /* | 
| 1112 | * helper to add backref node for the newly created snapshot. | ||
| 1113 | * the backref node is created by cloning backref node that | ||
| 1114 | * corresponds to root of source tree | ||
| 1115 | */ | ||
| 1116 | static int clone_backref_node(struct btrfs_trans_handle *trans, | ||
| 1117 | struct reloc_control *rc, | ||
| 1118 | struct btrfs_root *src, | ||
| 1119 | struct btrfs_root *dest) | ||
| 1120 | { | ||
| 1121 | struct btrfs_root *reloc_root = src->reloc_root; | ||
| 1122 | struct backref_cache *cache = &rc->backref_cache; | ||
| 1123 | struct backref_node *node = NULL; | ||
| 1124 | struct backref_node *new_node; | ||
| 1125 | struct backref_edge *edge; | ||
| 1126 | struct backref_edge *new_edge; | ||
| 1127 | struct rb_node *rb_node; | ||
| 1128 | |||
| 1129 | if (cache->last_trans > 0) | ||
| 1130 | update_backref_cache(trans, cache); | ||
| 1131 | |||
| 1132 | rb_node = tree_search(&cache->rb_root, src->commit_root->start); | ||
| 1133 | if (rb_node) { | ||
| 1134 | node = rb_entry(rb_node, struct backref_node, rb_node); | ||
| 1135 | if (node->detached) | ||
| 1136 | node = NULL; | ||
| 1137 | else | ||
| 1138 | BUG_ON(node->new_bytenr != reloc_root->node->start); | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | if (!node) { | ||
| 1142 | rb_node = tree_search(&cache->rb_root, | ||
| 1143 | reloc_root->commit_root->start); | ||
| 1144 | if (rb_node) { | ||
| 1145 | node = rb_entry(rb_node, struct backref_node, | ||
| 1146 | rb_node); | ||
| 1147 | BUG_ON(node->detached); | ||
| 1148 | } | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | if (!node) | ||
| 1152 | return 0; | ||
| 1153 | |||
| 1154 | new_node = alloc_backref_node(cache); | ||
| 1155 | if (!new_node) | ||
| 1156 | return -ENOMEM; | ||
| 1157 | |||
| 1158 | new_node->bytenr = dest->node->start; | ||
| 1159 | new_node->level = node->level; | ||
| 1160 | new_node->lowest = node->lowest; | ||
| 1161 | new_node->root = dest; | ||
| 1162 | |||
| 1163 | if (!node->lowest) { | ||
| 1164 | list_for_each_entry(edge, &node->lower, list[UPPER]) { | ||
| 1165 | new_edge = alloc_backref_edge(cache); | ||
| 1166 | if (!new_edge) | ||
| 1167 | goto fail; | ||
| 1168 | |||
| 1169 | new_edge->node[UPPER] = new_node; | ||
| 1170 | new_edge->node[LOWER] = edge->node[LOWER]; | ||
| 1171 | list_add_tail(&new_edge->list[UPPER], | ||
| 1172 | &new_node->lower); | ||
| 1173 | } | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | rb_node = tree_insert(&cache->rb_root, new_node->bytenr, | ||
| 1177 | &new_node->rb_node); | ||
| 1178 | BUG_ON(rb_node); | ||
| 1179 | |||
| 1180 | if (!new_node->lowest) { | ||
| 1181 | list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) { | ||
| 1182 | list_add_tail(&new_edge->list[LOWER], | ||
| 1183 | &new_edge->node[LOWER]->upper); | ||
| 1184 | } | ||
| 1185 | } | ||
| 1186 | return 0; | ||
| 1187 | fail: | ||
| 1188 | while (!list_empty(&new_node->lower)) { | ||
| 1189 | new_edge = list_entry(new_node->lower.next, | ||
| 1190 | struct backref_edge, list[UPPER]); | ||
| 1191 | list_del(&new_edge->list[UPPER]); | ||
| 1192 | free_backref_edge(cache, new_edge); | ||
| 1193 | } | ||
| 1194 | free_backref_node(cache, new_node); | ||
| 1195 | return -ENOMEM; | ||
| 1196 | } | ||
| 1197 | |||
| 1198 | /* | ||
| 844 | * helper to add 'address of tree root -> reloc tree' mapping | 1199 | * helper to add 'address of tree root -> reloc tree' mapping | 
| 845 | */ | 1200 | */ | 
| 846 | static int __add_reloc_root(struct btrfs_root *root) | 1201 | static int __add_reloc_root(struct btrfs_root *root) | 
| @@ -900,12 +1255,8 @@ static int __update_reloc_root(struct btrfs_root *root, int del) | |||
| 900 | return 0; | 1255 | return 0; | 
| 901 | } | 1256 | } | 
| 902 | 1257 | ||
| 903 | /* | 1258 | static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, | 
| 904 | * create reloc tree for a given fs tree. reloc tree is just a | 1259 | struct btrfs_root *root, u64 objectid) | 
| 905 | * snapshot of the fs tree with special root objectid. | ||
| 906 | */ | ||
| 907 | int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, | ||
| 908 | struct btrfs_root *root) | ||
| 909 | { | 1260 | { | 
| 910 | struct btrfs_root *reloc_root; | 1261 | struct btrfs_root *reloc_root; | 
| 911 | struct extent_buffer *eb; | 1262 | struct extent_buffer *eb; | 
| @@ -913,36 +1264,45 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, | |||
| 913 | struct btrfs_key root_key; | 1264 | struct btrfs_key root_key; | 
| 914 | int ret; | 1265 | int ret; | 
| 915 | 1266 | ||
| 916 | if (root->reloc_root) { | ||
| 917 | reloc_root = root->reloc_root; | ||
| 918 | reloc_root->last_trans = trans->transid; | ||
| 919 | return 0; | ||
| 920 | } | ||
| 921 | |||
| 922 | if (!root->fs_info->reloc_ctl || | ||
| 923 | !root->fs_info->reloc_ctl->create_reloc_root || | ||
| 924 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) | ||
| 925 | return 0; | ||
| 926 | |||
| 927 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); | 1267 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); | 
| 928 | BUG_ON(!root_item); | 1268 | BUG_ON(!root_item); | 
| 929 | 1269 | ||
| 930 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; | 1270 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; | 
| 931 | root_key.type = BTRFS_ROOT_ITEM_KEY; | 1271 | root_key.type = BTRFS_ROOT_ITEM_KEY; | 
| 932 | root_key.offset = root->root_key.objectid; | 1272 | root_key.offset = objectid; | 
| 933 | 1273 | ||
| 934 | ret = btrfs_copy_root(trans, root, root->commit_root, &eb, | 1274 | if (root->root_key.objectid == objectid) { | 
| 935 | BTRFS_TREE_RELOC_OBJECTID); | 1275 | /* called by btrfs_init_reloc_root */ | 
| 936 | BUG_ON(ret); | 1276 | ret = btrfs_copy_root(trans, root, root->commit_root, &eb, | 
| 1277 | BTRFS_TREE_RELOC_OBJECTID); | ||
| 1278 | BUG_ON(ret); | ||
| 1279 | |||
| 1280 | btrfs_set_root_last_snapshot(&root->root_item, | ||
| 1281 | trans->transid - 1); | ||
| 1282 | } else { | ||
| 1283 | /* | ||
| 1284 | * called by btrfs_reloc_post_snapshot_hook. | ||
| 1285 | * the source tree is a reloc tree, all tree blocks | ||
| 1286 | * modified after it was created have RELOC flag | ||
| 1287 | * set in their headers. so it's OK to not update | ||
| 1288 | * the 'last_snapshot'. | ||
| 1289 | */ | ||
| 1290 | ret = btrfs_copy_root(trans, root, root->node, &eb, | ||
| 1291 | BTRFS_TREE_RELOC_OBJECTID); | ||
| 1292 | BUG_ON(ret); | ||
| 1293 | } | ||
| 937 | 1294 | ||
| 938 | btrfs_set_root_last_snapshot(&root->root_item, trans->transid - 1); | ||
| 939 | memcpy(root_item, &root->root_item, sizeof(*root_item)); | 1295 | memcpy(root_item, &root->root_item, sizeof(*root_item)); | 
| 940 | btrfs_set_root_refs(root_item, 1); | ||
| 941 | btrfs_set_root_bytenr(root_item, eb->start); | 1296 | btrfs_set_root_bytenr(root_item, eb->start); | 
| 942 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); | 1297 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); | 
| 943 | btrfs_set_root_generation(root_item, trans->transid); | 1298 | btrfs_set_root_generation(root_item, trans->transid); | 
| 944 | memset(&root_item->drop_progress, 0, sizeof(struct btrfs_disk_key)); | 1299 | |
| 945 | root_item->drop_level = 0; | 1300 | if (root->root_key.objectid == objectid) { | 
| 1301 | btrfs_set_root_refs(root_item, 0); | ||
| 1302 | memset(&root_item->drop_progress, 0, | ||
| 1303 | sizeof(struct btrfs_disk_key)); | ||
| 1304 | root_item->drop_level = 0; | ||
| 1305 | } | ||
| 946 | 1306 | ||
| 947 | btrfs_tree_unlock(eb); | 1307 | btrfs_tree_unlock(eb); | 
| 948 | free_extent_buffer(eb); | 1308 | free_extent_buffer(eb); | 
| @@ -956,6 +1316,37 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, | |||
| 956 | &root_key); | 1316 | &root_key); | 
| 957 | BUG_ON(IS_ERR(reloc_root)); | 1317 | BUG_ON(IS_ERR(reloc_root)); | 
| 958 | reloc_root->last_trans = trans->transid; | 1318 | reloc_root->last_trans = trans->transid; | 
| 1319 | return reloc_root; | ||
| 1320 | } | ||
| 1321 | |||
| 1322 | /* | ||
| 1323 | * create reloc tree for a given fs tree. reloc tree is just a | ||
| 1324 | * snapshot of the fs tree with special root objectid. | ||
| 1325 | */ | ||
| 1326 | int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, | ||
| 1327 | struct btrfs_root *root) | ||
| 1328 | { | ||
| 1329 | struct btrfs_root *reloc_root; | ||
| 1330 | struct reloc_control *rc = root->fs_info->reloc_ctl; | ||
| 1331 | int clear_rsv = 0; | ||
| 1332 | |||
| 1333 | if (root->reloc_root) { | ||
| 1334 | reloc_root = root->reloc_root; | ||
| 1335 | reloc_root->last_trans = trans->transid; | ||
| 1336 | return 0; | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | if (!rc || !rc->create_reloc_tree || | ||
| 1340 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) | ||
| 1341 | return 0; | ||
| 1342 | |||
| 1343 | if (!trans->block_rsv) { | ||
| 1344 | trans->block_rsv = rc->block_rsv; | ||
| 1345 | clear_rsv = 1; | ||
| 1346 | } | ||
| 1347 | reloc_root = create_reloc_root(trans, root, root->root_key.objectid); | ||
| 1348 | if (clear_rsv) | ||
| 1349 | trans->block_rsv = NULL; | ||
| 959 | 1350 | ||
| 960 | __add_reloc_root(reloc_root); | 1351 | __add_reloc_root(reloc_root); | 
| 961 | root->reloc_root = reloc_root; | 1352 | root->reloc_root = reloc_root; | 
| @@ -979,7 +1370,8 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, | |||
| 979 | reloc_root = root->reloc_root; | 1370 | reloc_root = root->reloc_root; | 
| 980 | root_item = &reloc_root->root_item; | 1371 | root_item = &reloc_root->root_item; | 
| 981 | 1372 | ||
| 982 | if (btrfs_root_refs(root_item) == 0) { | 1373 | if (root->fs_info->reloc_ctl->merge_reloc_tree && | 
| 1374 | btrfs_root_refs(root_item) == 0) { | ||
| 983 | root->reloc_root = NULL; | 1375 | root->reloc_root = NULL; | 
| 984 | del = 1; | 1376 | del = 1; | 
| 985 | } | 1377 | } | 
| @@ -1101,8 +1493,7 @@ static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr, | |||
| 1101 | goto out; | 1493 | goto out; | 
| 1102 | } | 1494 | } | 
| 1103 | 1495 | ||
| 1104 | if (new_bytenr) | 1496 | *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | 
| 1105 | *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | ||
| 1106 | ret = 0; | 1497 | ret = 0; | 
| 1107 | out: | 1498 | out: | 
| 1108 | btrfs_free_path(path); | 1499 | btrfs_free_path(path); | 
| @@ -1113,19 +1504,18 @@ out: | |||
| 1113 | * update file extent items in the tree leaf to point to | 1504 | * update file extent items in the tree leaf to point to | 
| 1114 | * the new locations. | 1505 | * the new locations. | 
| 1115 | */ | 1506 | */ | 
| 1116 | static int replace_file_extents(struct btrfs_trans_handle *trans, | 1507 | static noinline_for_stack | 
| 1117 | struct reloc_control *rc, | 1508 | int replace_file_extents(struct btrfs_trans_handle *trans, | 
| 1118 | struct btrfs_root *root, | 1509 | struct reloc_control *rc, | 
| 1119 | struct extent_buffer *leaf, | 1510 | struct btrfs_root *root, | 
| 1120 | struct list_head *inode_list) | 1511 | struct extent_buffer *leaf) | 
| 1121 | { | 1512 | { | 
| 1122 | struct btrfs_key key; | 1513 | struct btrfs_key key; | 
| 1123 | struct btrfs_file_extent_item *fi; | 1514 | struct btrfs_file_extent_item *fi; | 
| 1124 | struct inode *inode = NULL; | 1515 | struct inode *inode = NULL; | 
| 1125 | struct inodevec *ivec = NULL; | ||
| 1126 | u64 parent; | 1516 | u64 parent; | 
| 1127 | u64 bytenr; | 1517 | u64 bytenr; | 
| 1128 | u64 new_bytenr; | 1518 | u64 new_bytenr = 0; | 
| 1129 | u64 num_bytes; | 1519 | u64 num_bytes; | 
| 1130 | u64 end; | 1520 | u64 end; | 
| 1131 | u32 nritems; | 1521 | u32 nritems; | 
| @@ -1165,21 +1555,12 @@ static int replace_file_extents(struct btrfs_trans_handle *trans, | |||
| 1165 | * to complete and drop the extent cache | 1555 | * to complete and drop the extent cache | 
| 1166 | */ | 1556 | */ | 
| 1167 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { | 1557 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { | 
| 1168 | if (!ivec || ivec->nr == INODEVEC_SIZE) { | ||
| 1169 | ivec = kmalloc(sizeof(*ivec), GFP_NOFS); | ||
| 1170 | BUG_ON(!ivec); | ||
| 1171 | ivec->nr = 0; | ||
| 1172 | list_add_tail(&ivec->list, inode_list); | ||
| 1173 | } | ||
| 1174 | if (first) { | 1558 | if (first) { | 
| 1175 | inode = find_next_inode(root, key.objectid); | 1559 | inode = find_next_inode(root, key.objectid); | 
| 1176 | if (inode) | ||
| 1177 | ivec->inode[ivec->nr++] = inode; | ||
| 1178 | first = 0; | 1560 | first = 0; | 
| 1179 | } else if (inode && inode->i_ino < key.objectid) { | 1561 | } else if (inode && inode->i_ino < key.objectid) { | 
| 1562 | btrfs_add_delayed_iput(inode); | ||
| 1180 | inode = find_next_inode(root, key.objectid); | 1563 | inode = find_next_inode(root, key.objectid); | 
| 1181 | if (inode) | ||
| 1182 | ivec->inode[ivec->nr++] = inode; | ||
| 1183 | } | 1564 | } | 
| 1184 | if (inode && inode->i_ino == key.objectid) { | 1565 | if (inode && inode->i_ino == key.objectid) { | 
| 1185 | end = key.offset + | 1566 | end = key.offset + | 
| @@ -1203,8 +1584,10 @@ static int replace_file_extents(struct btrfs_trans_handle *trans, | |||
| 1203 | 1584 | ||
| 1204 | ret = get_new_location(rc->data_inode, &new_bytenr, | 1585 | ret = get_new_location(rc->data_inode, &new_bytenr, | 
| 1205 | bytenr, num_bytes); | 1586 | bytenr, num_bytes); | 
| 1206 | if (ret > 0) | 1587 | if (ret > 0) { | 
| 1588 | WARN_ON(1); | ||
| 1207 | continue; | 1589 | continue; | 
| 1590 | } | ||
| 1208 | BUG_ON(ret < 0); | 1591 | BUG_ON(ret < 0); | 
| 1209 | 1592 | ||
| 1210 | btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr); | 1593 | btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr); | 
| @@ -1224,6 +1607,8 @@ static int replace_file_extents(struct btrfs_trans_handle *trans, | |||
| 1224 | } | 1607 | } | 
| 1225 | if (dirty) | 1608 | if (dirty) | 
| 1226 | btrfs_mark_buffer_dirty(leaf); | 1609 | btrfs_mark_buffer_dirty(leaf); | 
| 1610 | if (inode) | ||
| 1611 | btrfs_add_delayed_iput(inode); | ||
| 1227 | return 0; | 1612 | return 0; | 
| 1228 | } | 1613 | } | 
| 1229 | 1614 | ||
| @@ -1247,11 +1632,11 @@ int memcmp_node_keys(struct extent_buffer *eb, int slot, | |||
| 1247 | * if no block got replaced, 0 is returned. if there are other | 1632 | * if no block got replaced, 0 is returned. if there are other | 
| 1248 | * errors, a negative error number is returned. | 1633 | * errors, a negative error number is returned. | 
| 1249 | */ | 1634 | */ | 
| 1250 | static int replace_path(struct btrfs_trans_handle *trans, | 1635 | static noinline_for_stack | 
| 1251 | struct btrfs_root *dest, struct btrfs_root *src, | 1636 | int replace_path(struct btrfs_trans_handle *trans, | 
| 1252 | struct btrfs_path *path, struct btrfs_key *next_key, | 1637 | struct btrfs_root *dest, struct btrfs_root *src, | 
| 1253 | struct extent_buffer **leaf, | 1638 | struct btrfs_path *path, struct btrfs_key *next_key, | 
| 1254 | int lowest_level, int max_level) | 1639 | int lowest_level, int max_level) | 
| 1255 | { | 1640 | { | 
| 1256 | struct extent_buffer *eb; | 1641 | struct extent_buffer *eb; | 
| 1257 | struct extent_buffer *parent; | 1642 | struct extent_buffer *parent; | 
| @@ -1262,16 +1647,16 @@ static int replace_path(struct btrfs_trans_handle *trans, | |||
| 1262 | u64 new_ptr_gen; | 1647 | u64 new_ptr_gen; | 
| 1263 | u64 last_snapshot; | 1648 | u64 last_snapshot; | 
| 1264 | u32 blocksize; | 1649 | u32 blocksize; | 
| 1650 | int cow = 0; | ||
| 1265 | int level; | 1651 | int level; | 
| 1266 | int ret; | 1652 | int ret; | 
| 1267 | int slot; | 1653 | int slot; | 
| 1268 | 1654 | ||
| 1269 | BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); | 1655 | BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); | 
| 1270 | BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID); | 1656 | BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID); | 
| 1271 | BUG_ON(lowest_level > 1 && leaf); | ||
| 1272 | 1657 | ||
| 1273 | last_snapshot = btrfs_root_last_snapshot(&src->root_item); | 1658 | last_snapshot = btrfs_root_last_snapshot(&src->root_item); | 
| 1274 | 1659 | again: | |
| 1275 | slot = path->slots[lowest_level]; | 1660 | slot = path->slots[lowest_level]; | 
| 1276 | btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot); | 1661 | btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot); | 
| 1277 | 1662 | ||
| @@ -1285,8 +1670,10 @@ static int replace_path(struct btrfs_trans_handle *trans, | |||
| 1285 | return 0; | 1670 | return 0; | 
| 1286 | } | 1671 | } | 
| 1287 | 1672 | ||
| 1288 | ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb); | 1673 | if (cow) { | 
| 1289 | BUG_ON(ret); | 1674 | ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb); | 
| 1675 | BUG_ON(ret); | ||
| 1676 | } | ||
| 1290 | btrfs_set_lock_blocking(eb); | 1677 | btrfs_set_lock_blocking(eb); | 
| 1291 | 1678 | ||
| 1292 | if (next_key) { | 1679 | if (next_key) { | 
| @@ -1330,7 +1717,7 @@ static int replace_path(struct btrfs_trans_handle *trans, | |||
| 1330 | 1717 | ||
| 1331 | if (new_bytenr == 0 || old_ptr_gen > last_snapshot || | 1718 | if (new_bytenr == 0 || old_ptr_gen > last_snapshot || | 
| 1332 | memcmp_node_keys(parent, slot, path, level)) { | 1719 | memcmp_node_keys(parent, slot, path, level)) { | 
| 1333 | if (level <= lowest_level && !leaf) { | 1720 | if (level <= lowest_level) { | 
| 1334 | ret = 0; | 1721 | ret = 0; | 
| 1335 | break; | 1722 | break; | 
| 1336 | } | 1723 | } | 
| @@ -1338,16 +1725,12 @@ static int replace_path(struct btrfs_trans_handle *trans, | |||
| 1338 | eb = read_tree_block(dest, old_bytenr, blocksize, | 1725 | eb = read_tree_block(dest, old_bytenr, blocksize, | 
| 1339 | old_ptr_gen); | 1726 | old_ptr_gen); | 
| 1340 | btrfs_tree_lock(eb); | 1727 | btrfs_tree_lock(eb); | 
| 1341 | ret = btrfs_cow_block(trans, dest, eb, parent, | 1728 | if (cow) { | 
| 1342 | slot, &eb); | 1729 | ret = btrfs_cow_block(trans, dest, eb, parent, | 
| 1343 | BUG_ON(ret); | 1730 | slot, &eb); | 
| 1344 | btrfs_set_lock_blocking(eb); | 1731 | BUG_ON(ret); | 
| 1345 | |||
| 1346 | if (level <= lowest_level) { | ||
| 1347 | *leaf = eb; | ||
| 1348 | ret = 0; | ||
| 1349 | break; | ||
| 1350 | } | 1732 | } | 
| 1733 | btrfs_set_lock_blocking(eb); | ||
| 1351 | 1734 | ||
| 1352 | btrfs_tree_unlock(parent); | 1735 | btrfs_tree_unlock(parent); | 
| 1353 | free_extent_buffer(parent); | 1736 | free_extent_buffer(parent); | 
| @@ -1356,6 +1739,13 @@ static int replace_path(struct btrfs_trans_handle *trans, | |||
| 1356 | continue; | 1739 | continue; | 
| 1357 | } | 1740 | } | 
| 1358 | 1741 | ||
| 1742 | if (!cow) { | ||
| 1743 | btrfs_tree_unlock(parent); | ||
| 1744 | free_extent_buffer(parent); | ||
| 1745 | cow = 1; | ||
| 1746 | goto again; | ||
| 1747 | } | ||
| 1748 | |||
| 1359 | btrfs_node_key_to_cpu(path->nodes[level], &key, | 1749 | btrfs_node_key_to_cpu(path->nodes[level], &key, | 
| 1360 | path->slots[level]); | 1750 | path->slots[level]); | 
| 1361 | btrfs_release_path(src, path); | 1751 | btrfs_release_path(src, path); | 
| @@ -1561,20 +1951,6 @@ static int invalidate_extent_cache(struct btrfs_root *root, | |||
| 1561 | return 0; | 1951 | return 0; | 
| 1562 | } | 1952 | } | 
| 1563 | 1953 | ||
| 1564 | static void put_inodes(struct list_head *list) | ||
| 1565 | { | ||
| 1566 | struct inodevec *ivec; | ||
| 1567 | while (!list_empty(list)) { | ||
| 1568 | ivec = list_entry(list->next, struct inodevec, list); | ||
| 1569 | list_del(&ivec->list); | ||
| 1570 | while (ivec->nr > 0) { | ||
| 1571 | ivec->nr--; | ||
| 1572 | iput(ivec->inode[ivec->nr]); | ||
| 1573 | } | ||
| 1574 | kfree(ivec); | ||
| 1575 | } | ||
| 1576 | } | ||
| 1577 | |||
| 1578 | static int find_next_key(struct btrfs_path *path, int level, | 1954 | static int find_next_key(struct btrfs_path *path, int level, | 
| 1579 | struct btrfs_key *key) | 1955 | struct btrfs_key *key) | 
| 1580 | 1956 | ||
| @@ -1607,13 +1983,14 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, | |||
| 1607 | struct btrfs_root *reloc_root; | 1983 | struct btrfs_root *reloc_root; | 
| 1608 | struct btrfs_root_item *root_item; | 1984 | struct btrfs_root_item *root_item; | 
| 1609 | struct btrfs_path *path; | 1985 | struct btrfs_path *path; | 
| 1610 | struct extent_buffer *leaf = NULL; | 1986 | struct extent_buffer *leaf; | 
| 1611 | unsigned long nr; | 1987 | unsigned long nr; | 
| 1612 | int level; | 1988 | int level; | 
| 1613 | int max_level; | 1989 | int max_level; | 
| 1614 | int replaced = 0; | 1990 | int replaced = 0; | 
| 1615 | int ret; | 1991 | int ret; | 
| 1616 | int err = 0; | 1992 | int err = 0; | 
| 1993 | u32 min_reserved; | ||
| 1617 | 1994 | ||
| 1618 | path = btrfs_alloc_path(); | 1995 | path = btrfs_alloc_path(); | 
| 1619 | if (!path) | 1996 | if (!path) | 
| @@ -1647,34 +2024,23 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, | |||
| 1647 | btrfs_unlock_up_safe(path, 0); | 2024 | btrfs_unlock_up_safe(path, 0); | 
| 1648 | } | 2025 | } | 
| 1649 | 2026 | ||
| 1650 | if (level == 0 && rc->stage == UPDATE_DATA_PTRS) { | 2027 | min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; | 
| 1651 | trans = btrfs_start_transaction(root, 1); | 2028 | memset(&next_key, 0, sizeof(next_key)); | 
| 1652 | 2029 | ||
| 1653 | leaf = path->nodes[0]; | 2030 | while (1) { | 
| 1654 | btrfs_item_key_to_cpu(leaf, &key, 0); | 2031 | trans = btrfs_start_transaction(root, 0); | 
| 1655 | btrfs_release_path(reloc_root, path); | 2032 | trans->block_rsv = rc->block_rsv; | 
| 1656 | 2033 | ||
| 1657 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | 2034 | ret = btrfs_block_rsv_check(trans, root, rc->block_rsv, | 
| 1658 | if (ret < 0) { | 2035 | min_reserved, 0); | 
| 1659 | err = ret; | 2036 | if (ret) { | 
| 1660 | goto out; | 2037 | BUG_ON(ret != -EAGAIN); | 
| 2038 | ret = btrfs_commit_transaction(trans, root); | ||
| 2039 | BUG_ON(ret); | ||
| 2040 | continue; | ||
| 1661 | } | 2041 | } | 
| 1662 | 2042 | ||
| 1663 | leaf = path->nodes[0]; | ||
| 1664 | btrfs_unlock_up_safe(path, 1); | ||
| 1665 | ret = replace_file_extents(trans, rc, root, leaf, | ||
| 1666 | &inode_list); | ||
| 1667 | if (ret < 0) | ||
| 1668 | err = ret; | ||
| 1669 | goto out; | ||
| 1670 | } | ||
| 1671 | |||
| 1672 | memset(&next_key, 0, sizeof(next_key)); | ||
| 1673 | |||
| 1674 | while (1) { | ||
| 1675 | leaf = NULL; | ||
| 1676 | replaced = 0; | 2043 | replaced = 0; | 
| 1677 | trans = btrfs_start_transaction(root, 1); | ||
| 1678 | max_level = level; | 2044 | max_level = level; | 
| 1679 | 2045 | ||
| 1680 | ret = walk_down_reloc_tree(reloc_root, path, &level); | 2046 | ret = walk_down_reloc_tree(reloc_root, path, &level); | 
| @@ -1688,14 +2054,9 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, | |||
| 1688 | if (!find_next_key(path, level, &key) && | 2054 | if (!find_next_key(path, level, &key) && | 
| 1689 | btrfs_comp_cpu_keys(&next_key, &key) >= 0) { | 2055 | btrfs_comp_cpu_keys(&next_key, &key) >= 0) { | 
| 1690 | ret = 0; | 2056 | ret = 0; | 
| 1691 | } else if (level == 1 && rc->stage == UPDATE_DATA_PTRS) { | ||
| 1692 | ret = replace_path(trans, root, reloc_root, | ||
| 1693 | path, &next_key, &leaf, | ||
| 1694 | level, max_level); | ||
| 1695 | } else { | 2057 | } else { | 
| 1696 | ret = replace_path(trans, root, reloc_root, | 2058 | ret = replace_path(trans, root, reloc_root, path, | 
| 1697 | path, &next_key, NULL, | 2059 | &next_key, level, max_level); | 
| 1698 | level, max_level); | ||
| 1699 | } | 2060 | } | 
| 1700 | if (ret < 0) { | 2061 | if (ret < 0) { | 
| 1701 | err = ret; | 2062 | err = ret; | 
| @@ -1707,16 +2068,6 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, | |||
| 1707 | btrfs_node_key_to_cpu(path->nodes[level], &key, | 2068 | btrfs_node_key_to_cpu(path->nodes[level], &key, | 
| 1708 | path->slots[level]); | 2069 | path->slots[level]); | 
| 1709 | replaced = 1; | 2070 | replaced = 1; | 
| 1710 | } else if (leaf) { | ||
| 1711 | /* | ||
| 1712 | * no block got replaced, try replacing file extents | ||
| 1713 | */ | ||
| 1714 | btrfs_item_key_to_cpu(leaf, &key, 0); | ||
| 1715 | ret = replace_file_extents(trans, rc, root, leaf, | ||
| 1716 | &inode_list); | ||
| 1717 | btrfs_tree_unlock(leaf); | ||
| 1718 | free_extent_buffer(leaf); | ||
| 1719 | BUG_ON(ret < 0); | ||
| 1720 | } | 2071 | } | 
| 1721 | 2072 | ||
| 1722 | ret = walk_up_reloc_tree(reloc_root, path, &level); | 2073 | ret = walk_up_reloc_tree(reloc_root, path, &level); | 
| @@ -1733,15 +2084,10 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, | |||
| 1733 | root_item->drop_level = level; | 2084 | root_item->drop_level = level; | 
| 1734 | 2085 | ||
| 1735 | nr = trans->blocks_used; | 2086 | nr = trans->blocks_used; | 
| 1736 | btrfs_end_transaction(trans, root); | 2087 | btrfs_end_transaction_throttle(trans, root); | 
| 1737 | 2088 | ||
| 1738 | btrfs_btree_balance_dirty(root, nr); | 2089 | btrfs_btree_balance_dirty(root, nr); | 
| 1739 | 2090 | ||
| 1740 | /* | ||
| 1741 | * put inodes outside transaction, otherwise we may deadlock. | ||
| 1742 | */ | ||
| 1743 | put_inodes(&inode_list); | ||
| 1744 | |||
| 1745 | if (replaced && rc->stage == UPDATE_DATA_PTRS) | 2091 | if (replaced && rc->stage == UPDATE_DATA_PTRS) | 
| 1746 | invalidate_extent_cache(root, &key, &next_key); | 2092 | invalidate_extent_cache(root, &key, &next_key); | 
| 1747 | } | 2093 | } | 
| @@ -1764,87 +2110,125 @@ out: | |||
| 1764 | sizeof(root_item->drop_progress)); | 2110 | sizeof(root_item->drop_progress)); | 
| 1765 | root_item->drop_level = 0; | 2111 | root_item->drop_level = 0; | 
| 1766 | btrfs_set_root_refs(root_item, 0); | 2112 | btrfs_set_root_refs(root_item, 0); | 
| 2113 | btrfs_update_reloc_root(trans, root); | ||
| 1767 | } | 2114 | } | 
| 1768 | 2115 | ||
| 1769 | nr = trans->blocks_used; | 2116 | nr = trans->blocks_used; | 
| 1770 | btrfs_end_transaction(trans, root); | 2117 | btrfs_end_transaction_throttle(trans, root); | 
| 1771 | 2118 | ||
| 1772 | btrfs_btree_balance_dirty(root, nr); | 2119 | btrfs_btree_balance_dirty(root, nr); | 
| 1773 | 2120 | ||
| 1774 | put_inodes(&inode_list); | ||
| 1775 | |||
| 1776 | if (replaced && rc->stage == UPDATE_DATA_PTRS) | 2121 | if (replaced && rc->stage == UPDATE_DATA_PTRS) | 
| 1777 | invalidate_extent_cache(root, &key, &next_key); | 2122 | invalidate_extent_cache(root, &key, &next_key); | 
| 1778 | 2123 | ||
| 1779 | return err; | 2124 | return err; | 
| 1780 | } | 2125 | } | 
| 1781 | 2126 | ||
| 1782 | /* | 2127 | static noinline_for_stack | 
| 1783 | * callback for the work threads. | 2128 | int prepare_to_merge(struct reloc_control *rc, int err) | 
| 1784 | * this function merges reloc tree with corresponding fs tree, | ||
| 1785 | * and then drops the reloc tree. | ||
| 1786 | */ | ||
| 1787 | static void merge_func(struct btrfs_work *work) | ||
| 1788 | { | 2129 | { | 
| 1789 | struct btrfs_trans_handle *trans; | 2130 | struct btrfs_root *root = rc->extent_root; | 
| 1790 | struct btrfs_root *root; | ||
| 1791 | struct btrfs_root *reloc_root; | 2131 | struct btrfs_root *reloc_root; | 
| 1792 | struct async_merge *async; | 2132 | struct btrfs_trans_handle *trans; | 
| 2133 | LIST_HEAD(reloc_roots); | ||
| 2134 | u64 num_bytes = 0; | ||
| 2135 | int ret; | ||
| 2136 | int retries = 0; | ||
| 2137 | |||
| 2138 | mutex_lock(&root->fs_info->trans_mutex); | ||
| 2139 | rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; | ||
| 2140 | rc->merging_rsv_size += rc->nodes_relocated * 2; | ||
| 2141 | mutex_unlock(&root->fs_info->trans_mutex); | ||
| 2142 | again: | ||
| 2143 | if (!err) { | ||
| 2144 | num_bytes = rc->merging_rsv_size; | ||
| 2145 | ret = btrfs_block_rsv_add(NULL, root, rc->block_rsv, | ||
| 2146 | num_bytes, &retries); | ||
| 2147 | if (ret) | ||
| 2148 | err = ret; | ||
| 2149 | } | ||
| 2150 | |||
| 2151 | trans = btrfs_join_transaction(rc->extent_root, 1); | ||
| 2152 | |||
| 2153 | if (!err) { | ||
| 2154 | if (num_bytes != rc->merging_rsv_size) { | ||
| 2155 | btrfs_end_transaction(trans, rc->extent_root); | ||
| 2156 | btrfs_block_rsv_release(rc->extent_root, | ||
| 2157 | rc->block_rsv, num_bytes); | ||
| 2158 | retries = 0; | ||
| 2159 | goto again; | ||
| 2160 | } | ||
| 2161 | } | ||
| 1793 | 2162 | ||
| 1794 | async = container_of(work, struct async_merge, work); | 2163 | rc->merge_reloc_tree = 1; | 
| 1795 | reloc_root = async->root; | 2164 | |
| 2165 | while (!list_empty(&rc->reloc_roots)) { | ||
| 2166 | reloc_root = list_entry(rc->reloc_roots.next, | ||
| 2167 | struct btrfs_root, root_list); | ||
| 2168 | list_del_init(&reloc_root->root_list); | ||
| 1796 | 2169 | ||
| 1797 | if (btrfs_root_refs(&reloc_root->root_item) > 0) { | ||
| 1798 | root = read_fs_root(reloc_root->fs_info, | 2170 | root = read_fs_root(reloc_root->fs_info, | 
| 1799 | reloc_root->root_key.offset); | 2171 | reloc_root->root_key.offset); | 
| 1800 | BUG_ON(IS_ERR(root)); | 2172 | BUG_ON(IS_ERR(root)); | 
| 1801 | BUG_ON(root->reloc_root != reloc_root); | 2173 | BUG_ON(root->reloc_root != reloc_root); | 
| 1802 | 2174 | ||
| 1803 | merge_reloc_root(async->rc, root); | 2175 | /* | 
| 1804 | 2176 | * set reference count to 1, so btrfs_recover_relocation | |
| 1805 | trans = btrfs_start_transaction(root, 1); | 2177 | * knows it should resumes merging | 
| 2178 | */ | ||
| 2179 | if (!err) | ||
| 2180 | btrfs_set_root_refs(&reloc_root->root_item, 1); | ||
| 1806 | btrfs_update_reloc_root(trans, root); | 2181 | btrfs_update_reloc_root(trans, root); | 
| 1807 | btrfs_end_transaction(trans, root); | ||
| 1808 | } | ||
| 1809 | 2182 | ||
| 1810 | btrfs_drop_snapshot(reloc_root, 0); | 2183 | list_add(&reloc_root->root_list, &reloc_roots); | 
| 2184 | } | ||
| 1811 | 2185 | ||
| 1812 | if (atomic_dec_and_test(async->num_pending)) | 2186 | list_splice(&reloc_roots, &rc->reloc_roots); | 
| 1813 | complete(async->done); | ||
| 1814 | 2187 | ||
| 1815 | kfree(async); | 2188 | if (!err) | 
| 2189 | btrfs_commit_transaction(trans, rc->extent_root); | ||
| 2190 | else | ||
| 2191 | btrfs_end_transaction(trans, rc->extent_root); | ||
| 2192 | return err; | ||
| 1816 | } | 2193 | } | 
| 1817 | 2194 | ||
| 1818 | static int merge_reloc_roots(struct reloc_control *rc) | 2195 | static noinline_for_stack | 
| 2196 | int merge_reloc_roots(struct reloc_control *rc) | ||
| 1819 | { | 2197 | { | 
| 1820 | struct async_merge *async; | ||
| 1821 | struct btrfs_root *root; | 2198 | struct btrfs_root *root; | 
| 1822 | struct completion done; | 2199 | struct btrfs_root *reloc_root; | 
| 1823 | atomic_t num_pending; | 2200 | LIST_HEAD(reloc_roots); | 
| 2201 | int found = 0; | ||
| 2202 | int ret; | ||
| 2203 | again: | ||
| 2204 | root = rc->extent_root; | ||
| 2205 | mutex_lock(&root->fs_info->trans_mutex); | ||
| 2206 | list_splice_init(&rc->reloc_roots, &reloc_roots); | ||
| 2207 | mutex_unlock(&root->fs_info->trans_mutex); | ||
| 1824 | 2208 | ||
| 1825 | init_completion(&done); | 2209 | while (!list_empty(&reloc_roots)) { | 
| 1826 | atomic_set(&num_pending, 1); | 2210 | found = 1; | 
| 2211 | reloc_root = list_entry(reloc_roots.next, | ||
| 2212 | struct btrfs_root, root_list); | ||
| 1827 | 2213 | ||
| 1828 | while (!list_empty(&rc->reloc_roots)) { | 2214 | if (btrfs_root_refs(&reloc_root->root_item) > 0) { | 
| 1829 | root = list_entry(rc->reloc_roots.next, | 2215 | root = read_fs_root(reloc_root->fs_info, | 
| 1830 | struct btrfs_root, root_list); | 2216 | reloc_root->root_key.offset); | 
| 1831 | list_del_init(&root->root_list); | 2217 | BUG_ON(IS_ERR(root)); | 
| 2218 | BUG_ON(root->reloc_root != reloc_root); | ||
| 1832 | 2219 | ||
| 1833 | async = kmalloc(sizeof(*async), GFP_NOFS); | 2220 | ret = merge_reloc_root(rc, root); | 
| 1834 | BUG_ON(!async); | 2221 | BUG_ON(ret); | 
| 1835 | async->work.func = merge_func; | 2222 | } else { | 
| 1836 | async->work.flags = 0; | 2223 | list_del_init(&reloc_root->root_list); | 
| 1837 | async->rc = rc; | 2224 | } | 
| 1838 | async->root = root; | 2225 | btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0); | 
| 1839 | async->done = &done; | ||
| 1840 | async->num_pending = &num_pending; | ||
| 1841 | atomic_inc(&num_pending); | ||
| 1842 | btrfs_queue_worker(&rc->workers, &async->work); | ||
| 1843 | } | 2226 | } | 
| 1844 | 2227 | ||
| 1845 | if (!atomic_dec_and_test(&num_pending)) | 2228 | if (found) { | 
| 1846 | wait_for_completion(&done); | 2229 | found = 0; | 
| 1847 | 2230 | goto again; | |
| 2231 | } | ||
| 1848 | BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); | 2232 | BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); | 
| 1849 | return 0; | 2233 | return 0; | 
| 1850 | } | 2234 | } | 
| @@ -1875,119 +2259,169 @@ static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, | |||
| 1875 | return btrfs_record_root_in_trans(trans, root); | 2259 | return btrfs_record_root_in_trans(trans, root); | 
| 1876 | } | 2260 | } | 
| 1877 | 2261 | ||
| 1878 | /* | 2262 | static noinline_for_stack | 
| 1879 | * select one tree from trees that references the block. | 2263 | struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, | 
| 1880 | * for blocks in refernce counted trees, we preper reloc tree. | 2264 | struct reloc_control *rc, | 
| 1881 | * if no reloc tree found and reloc_only is true, NULL is returned. | 2265 | struct backref_node *node, | 
| 1882 | */ | 2266 | struct backref_edge *edges[], int *nr) | 
| 1883 | static struct btrfs_root *__select_one_root(struct btrfs_trans_handle *trans, | ||
| 1884 | struct backref_node *node, | ||
| 1885 | struct backref_edge *edges[], | ||
| 1886 | int *nr, int reloc_only) | ||
| 1887 | { | 2267 | { | 
| 1888 | struct backref_node *next; | 2268 | struct backref_node *next; | 
| 1889 | struct btrfs_root *root; | 2269 | struct btrfs_root *root; | 
| 1890 | int index; | 2270 | int index = 0; | 
| 1891 | int loop = 0; | 2271 | |
| 1892 | again: | ||
| 1893 | index = 0; | ||
| 1894 | next = node; | 2272 | next = node; | 
| 1895 | while (1) { | 2273 | while (1) { | 
| 1896 | cond_resched(); | 2274 | cond_resched(); | 
| 1897 | next = walk_up_backref(next, edges, &index); | 2275 | next = walk_up_backref(next, edges, &index); | 
| 1898 | root = next->root; | 2276 | root = next->root; | 
| 1899 | if (!root) { | 2277 | BUG_ON(!root); | 
| 1900 | BUG_ON(!node->old_root); | 2278 | BUG_ON(!root->ref_cows); | 
| 1901 | goto skip; | ||
| 1902 | } | ||
| 1903 | |||
| 1904 | /* no other choice for non-refernce counted tree */ | ||
| 1905 | if (!root->ref_cows) { | ||
| 1906 | BUG_ON(reloc_only); | ||
| 1907 | break; | ||
| 1908 | } | ||
| 1909 | 2279 | ||
| 1910 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { | 2280 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { | 
| 1911 | record_reloc_root_in_trans(trans, root); | 2281 | record_reloc_root_in_trans(trans, root); | 
| 1912 | break; | 2282 | break; | 
| 1913 | } | 2283 | } | 
| 1914 | 2284 | ||
| 1915 | if (loop) { | 2285 | btrfs_record_root_in_trans(trans, root); | 
| 1916 | btrfs_record_root_in_trans(trans, root); | 2286 | root = root->reloc_root; | 
| 2287 | |||
| 2288 | if (next->new_bytenr != root->node->start) { | ||
| 2289 | BUG_ON(next->new_bytenr); | ||
| 2290 | BUG_ON(!list_empty(&next->list)); | ||
| 2291 | next->new_bytenr = root->node->start; | ||
| 2292 | next->root = root; | ||
| 2293 | list_add_tail(&next->list, | ||
| 2294 | &rc->backref_cache.changed); | ||
| 2295 | __mark_block_processed(rc, next); | ||
| 1917 | break; | 2296 | break; | 
| 1918 | } | 2297 | } | 
| 1919 | 2298 | ||
| 1920 | if (reloc_only || next != node) { | 2299 | WARN_ON(1); | 
| 1921 | if (!root->reloc_root) | ||
| 1922 | btrfs_record_root_in_trans(trans, root); | ||
| 1923 | root = root->reloc_root; | ||
| 1924 | /* | ||
| 1925 | * if the reloc tree was created in current | ||
| 1926 | * transation, there is no node in backref tree | ||
| 1927 | * corresponds to the root of the reloc tree. | ||
| 1928 | */ | ||
| 1929 | if (btrfs_root_last_snapshot(&root->root_item) == | ||
| 1930 | trans->transid - 1) | ||
| 1931 | break; | ||
| 1932 | } | ||
| 1933 | skip: | ||
| 1934 | root = NULL; | 2300 | root = NULL; | 
| 1935 | next = walk_down_backref(edges, &index); | 2301 | next = walk_down_backref(edges, &index); | 
| 1936 | if (!next || next->level <= node->level) | 2302 | if (!next || next->level <= node->level) | 
| 1937 | break; | 2303 | break; | 
| 1938 | } | 2304 | } | 
| 2305 | if (!root) | ||
| 2306 | return NULL; | ||
| 1939 | 2307 | ||
| 1940 | if (!root && !loop && !reloc_only) { | 2308 | *nr = index; | 
| 1941 | loop = 1; | 2309 | next = node; | 
| 1942 | goto again; | 2310 | /* setup backref node path for btrfs_reloc_cow_block */ | 
| 2311 | while (1) { | ||
| 2312 | rc->backref_cache.path[next->level] = next; | ||
| 2313 | if (--index < 0) | ||
| 2314 | break; | ||
| 2315 | next = edges[index]->node[UPPER]; | ||
| 1943 | } | 2316 | } | 
| 1944 | |||
| 1945 | if (root) | ||
| 1946 | *nr = index; | ||
| 1947 | else | ||
| 1948 | *nr = 0; | ||
| 1949 | |||
| 1950 | return root; | 2317 | return root; | 
| 1951 | } | 2318 | } | 
| 1952 | 2319 | ||
| 2320 | /* | ||
| 2321 | * select a tree root for relocation. return NULL if the block | ||
| 2322 | * is reference counted. we should use do_relocation() in this | ||
| 2323 | * case. return a tree root pointer if the block isn't reference | ||
| 2324 | * counted. return -ENOENT if the block is root of reloc tree. | ||
| 2325 | */ | ||
| 1953 | static noinline_for_stack | 2326 | static noinline_for_stack | 
| 1954 | struct btrfs_root *select_one_root(struct btrfs_trans_handle *trans, | 2327 | struct btrfs_root *select_one_root(struct btrfs_trans_handle *trans, | 
| 1955 | struct backref_node *node) | 2328 | struct backref_node *node) | 
| 1956 | { | 2329 | { | 
| 2330 | struct backref_node *next; | ||
| 2331 | struct btrfs_root *root; | ||
| 2332 | struct btrfs_root *fs_root = NULL; | ||
| 1957 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; | 2333 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; | 
| 1958 | int nr; | 2334 | int index = 0; | 
| 1959 | return __select_one_root(trans, node, edges, &nr, 0); | 2335 | |
| 2336 | next = node; | ||
| 2337 | while (1) { | ||
| 2338 | cond_resched(); | ||
| 2339 | next = walk_up_backref(next, edges, &index); | ||
| 2340 | root = next->root; | ||
| 2341 | BUG_ON(!root); | ||
| 2342 | |||
| 2343 | /* no other choice for non-refernce counted tree */ | ||
| 2344 | if (!root->ref_cows) | ||
| 2345 | return root; | ||
| 2346 | |||
| 2347 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) | ||
| 2348 | fs_root = root; | ||
| 2349 | |||
| 2350 | if (next != node) | ||
| 2351 | return NULL; | ||
| 2352 | |||
| 2353 | next = walk_down_backref(edges, &index); | ||
| 2354 | if (!next || next->level <= node->level) | ||
| 2355 | break; | ||
| 2356 | } | ||
| 2357 | |||
| 2358 | if (!fs_root) | ||
| 2359 | return ERR_PTR(-ENOENT); | ||
| 2360 | return fs_root; | ||
| 1960 | } | 2361 | } | 
| 1961 | 2362 | ||
| 1962 | static noinline_for_stack | 2363 | static noinline_for_stack | 
| 1963 | struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, | 2364 | u64 calcu_metadata_size(struct reloc_control *rc, | 
| 1964 | struct backref_node *node, | 2365 | struct backref_node *node, int reserve) | 
| 1965 | struct backref_edge *edges[], int *nr) | ||
| 1966 | { | 2366 | { | 
| 1967 | return __select_one_root(trans, node, edges, nr, 1); | 2367 | struct backref_node *next = node; | 
| 2368 | struct backref_edge *edge; | ||
| 2369 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; | ||
| 2370 | u64 num_bytes = 0; | ||
| 2371 | int index = 0; | ||
| 2372 | |||
| 2373 | BUG_ON(reserve && node->processed); | ||
| 2374 | |||
| 2375 | while (next) { | ||
| 2376 | cond_resched(); | ||
| 2377 | while (1) { | ||
| 2378 | if (next->processed && (reserve || next != node)) | ||
| 2379 | break; | ||
| 2380 | |||
| 2381 | num_bytes += btrfs_level_size(rc->extent_root, | ||
| 2382 | next->level); | ||
| 2383 | |||
| 2384 | if (list_empty(&next->upper)) | ||
| 2385 | break; | ||
| 2386 | |||
| 2387 | edge = list_entry(next->upper.next, | ||
| 2388 | struct backref_edge, list[LOWER]); | ||
| 2389 | edges[index++] = edge; | ||
| 2390 | next = edge->node[UPPER]; | ||
| 2391 | } | ||
| 2392 | next = walk_down_backref(edges, &index); | ||
| 2393 | } | ||
| 2394 | return num_bytes; | ||
| 1968 | } | 2395 | } | 
| 1969 | 2396 | ||
| 1970 | static void grab_path_buffers(struct btrfs_path *path, | 2397 | static int reserve_metadata_space(struct btrfs_trans_handle *trans, | 
| 1971 | struct backref_node *node, | 2398 | struct reloc_control *rc, | 
| 1972 | struct backref_edge *edges[], int nr) | 2399 | struct backref_node *node) | 
| 1973 | { | 2400 | { | 
| 1974 | int i = 0; | 2401 | struct btrfs_root *root = rc->extent_root; | 
| 1975 | while (1) { | 2402 | u64 num_bytes; | 
| 1976 | drop_node_buffer(node); | 2403 | int ret; | 
| 1977 | node->eb = path->nodes[node->level]; | 2404 | |
| 1978 | BUG_ON(!node->eb); | 2405 | num_bytes = calcu_metadata_size(rc, node, 1) * 2; | 
| 1979 | if (path->locks[node->level]) | ||
| 1980 | node->locked = 1; | ||
| 1981 | path->nodes[node->level] = NULL; | ||
| 1982 | path->locks[node->level] = 0; | ||
| 1983 | |||
| 1984 | if (i >= nr) | ||
| 1985 | break; | ||
| 1986 | 2406 | ||
| 1987 | edges[i]->blockptr = node->eb->start; | 2407 | trans->block_rsv = rc->block_rsv; | 
| 1988 | node = edges[i]->node[UPPER]; | 2408 | ret = btrfs_block_rsv_add(trans, root, rc->block_rsv, num_bytes, | 
| 1989 | i++; | 2409 | &rc->block_rsv_retries); | 
| 2410 | if (ret) { | ||
| 2411 | if (ret == -EAGAIN) | ||
| 2412 | rc->commit_transaction = 1; | ||
| 2413 | return ret; | ||
| 1990 | } | 2414 | } | 
| 2415 | |||
| 2416 | rc->block_rsv_retries = 0; | ||
| 2417 | return 0; | ||
| 2418 | } | ||
| 2419 | |||
| 2420 | static void release_metadata_space(struct reloc_control *rc, | ||
| 2421 | struct backref_node *node) | ||
| 2422 | { | ||
| 2423 | u64 num_bytes = calcu_metadata_size(rc, node, 0) * 2; | ||
| 2424 | btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, num_bytes); | ||
| 1991 | } | 2425 | } | 
| 1992 | 2426 | ||
| 1993 | /* | 2427 | /* | 
| @@ -1998,6 +2432,7 @@ static void grab_path_buffers(struct btrfs_path *path, | |||
| 1998 | * in that case this function just updates pointers. | 2432 | * in that case this function just updates pointers. | 
| 1999 | */ | 2433 | */ | 
| 2000 | static int do_relocation(struct btrfs_trans_handle *trans, | 2434 | static int do_relocation(struct btrfs_trans_handle *trans, | 
| 2435 | struct reloc_control *rc, | ||
| 2001 | struct backref_node *node, | 2436 | struct backref_node *node, | 
| 2002 | struct btrfs_key *key, | 2437 | struct btrfs_key *key, | 
| 2003 | struct btrfs_path *path, int lowest) | 2438 | struct btrfs_path *path, int lowest) | 
| @@ -2018,18 +2453,25 @@ static int do_relocation(struct btrfs_trans_handle *trans, | |||
| 2018 | BUG_ON(lowest && node->eb); | 2453 | BUG_ON(lowest && node->eb); | 
| 2019 | 2454 | ||
| 2020 | path->lowest_level = node->level + 1; | 2455 | path->lowest_level = node->level + 1; | 
| 2456 | rc->backref_cache.path[node->level] = node; | ||
| 2021 | list_for_each_entry(edge, &node->upper, list[LOWER]) { | 2457 | list_for_each_entry(edge, &node->upper, list[LOWER]) { | 
| 2022 | cond_resched(); | 2458 | cond_resched(); | 
| 2023 | if (node->eb && node->eb->start == edge->blockptr) | ||
| 2024 | continue; | ||
| 2025 | 2459 | ||
| 2026 | upper = edge->node[UPPER]; | 2460 | upper = edge->node[UPPER]; | 
| 2027 | root = select_reloc_root(trans, upper, edges, &nr); | 2461 | root = select_reloc_root(trans, rc, upper, edges, &nr); | 
| 2028 | if (!root) | 2462 | BUG_ON(!root); | 
| 2029 | continue; | 2463 | |
| 2030 | 2464 | if (upper->eb && !upper->locked) { | |
| 2031 | if (upper->eb && !upper->locked) | 2465 | if (!lowest) { | 
| 2466 | ret = btrfs_bin_search(upper->eb, key, | ||
| 2467 | upper->level, &slot); | ||
| 2468 | BUG_ON(ret); | ||
| 2469 | bytenr = btrfs_node_blockptr(upper->eb, slot); | ||
| 2470 | if (node->eb->start == bytenr) | ||
| 2471 | goto next; | ||
| 2472 | } | ||
| 2032 | drop_node_buffer(upper); | 2473 | drop_node_buffer(upper); | 
| 2474 | } | ||
| 2033 | 2475 | ||
| 2034 | if (!upper->eb) { | 2476 | if (!upper->eb) { | 
| 2035 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | 2477 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | 
| @@ -2039,11 +2481,17 @@ static int do_relocation(struct btrfs_trans_handle *trans, | |||
| 2039 | } | 2481 | } | 
| 2040 | BUG_ON(ret > 0); | 2482 | BUG_ON(ret > 0); | 
| 2041 | 2483 | ||
| 2042 | slot = path->slots[upper->level]; | 2484 | if (!upper->eb) { | 
| 2485 | upper->eb = path->nodes[upper->level]; | ||
| 2486 | path->nodes[upper->level] = NULL; | ||
| 2487 | } else { | ||
| 2488 | BUG_ON(upper->eb != path->nodes[upper->level]); | ||
| 2489 | } | ||
| 2043 | 2490 | ||
| 2044 | btrfs_unlock_up_safe(path, upper->level + 1); | 2491 | upper->locked = 1; | 
| 2045 | grab_path_buffers(path, upper, edges, nr); | 2492 | path->locks[upper->level] = 0; | 
| 2046 | 2493 | ||
| 2494 | slot = path->slots[upper->level]; | ||
| 2047 | btrfs_release_path(NULL, path); | 2495 | btrfs_release_path(NULL, path); | 
| 2048 | } else { | 2496 | } else { | 
| 2049 | ret = btrfs_bin_search(upper->eb, key, upper->level, | 2497 | ret = btrfs_bin_search(upper->eb, key, upper->level, | 
| @@ -2052,14 +2500,11 @@ static int do_relocation(struct btrfs_trans_handle *trans, | |||
| 2052 | } | 2500 | } | 
| 2053 | 2501 | ||
| 2054 | bytenr = btrfs_node_blockptr(upper->eb, slot); | 2502 | bytenr = btrfs_node_blockptr(upper->eb, slot); | 
| 2055 | if (!lowest) { | 2503 | if (lowest) { | 
| 2056 | if (node->eb->start == bytenr) { | 2504 | BUG_ON(bytenr != node->bytenr); | 
| 2057 | btrfs_tree_unlock(upper->eb); | ||
| 2058 | upper->locked = 0; | ||
| 2059 | continue; | ||
| 2060 | } | ||
| 2061 | } else { | 2505 | } else { | 
| 2062 | BUG_ON(node->bytenr != bytenr); | 2506 | if (node->eb->start == bytenr) | 
| 2507 | goto next; | ||
| 2063 | } | 2508 | } | 
| 2064 | 2509 | ||
| 2065 | blocksize = btrfs_level_size(root, node->level); | 2510 | blocksize = btrfs_level_size(root, node->level); | 
| @@ -2071,13 +2516,13 @@ static int do_relocation(struct btrfs_trans_handle *trans, | |||
| 2071 | if (!node->eb) { | 2516 | if (!node->eb) { | 
| 2072 | ret = btrfs_cow_block(trans, root, eb, upper->eb, | 2517 | ret = btrfs_cow_block(trans, root, eb, upper->eb, | 
| 2073 | slot, &eb); | 2518 | slot, &eb); | 
| 2519 | btrfs_tree_unlock(eb); | ||
| 2520 | free_extent_buffer(eb); | ||
| 2074 | if (ret < 0) { | 2521 | if (ret < 0) { | 
| 2075 | err = ret; | 2522 | err = ret; | 
| 2076 | break; | 2523 | goto next; | 
| 2077 | } | 2524 | } | 
| 2078 | btrfs_set_lock_blocking(eb); | 2525 | BUG_ON(node->eb != eb); | 
| 2079 | node->eb = eb; | ||
| 2080 | node->locked = 1; | ||
| 2081 | } else { | 2526 | } else { | 
| 2082 | btrfs_set_node_blockptr(upper->eb, slot, | 2527 | btrfs_set_node_blockptr(upper->eb, slot, | 
| 2083 | node->eb->start); | 2528 | node->eb->start); | 
| @@ -2095,67 +2540,80 @@ static int do_relocation(struct btrfs_trans_handle *trans, | |||
| 2095 | ret = btrfs_drop_subtree(trans, root, eb, upper->eb); | 2540 | ret = btrfs_drop_subtree(trans, root, eb, upper->eb); | 
| 2096 | BUG_ON(ret); | 2541 | BUG_ON(ret); | 
| 2097 | } | 2542 | } | 
| 2098 | if (!lowest) { | 2543 | next: | 
| 2099 | btrfs_tree_unlock(upper->eb); | 2544 | if (!upper->pending) | 
| 2100 | upper->locked = 0; | 2545 | drop_node_buffer(upper); | 
| 2101 | } | 2546 | else | 
| 2547 | unlock_node_buffer(upper); | ||
| 2548 | if (err) | ||
| 2549 | break; | ||
| 2102 | } | 2550 | } | 
| 2551 | |||
| 2552 | if (!err && node->pending) { | ||
| 2553 | drop_node_buffer(node); | ||
| 2554 | list_move_tail(&node->list, &rc->backref_cache.changed); | ||
| 2555 | node->pending = 0; | ||
| 2556 | } | ||
| 2557 | |||
| 2103 | path->lowest_level = 0; | 2558 | path->lowest_level = 0; | 
| 2559 | BUG_ON(err == -ENOSPC); | ||
| 2104 | return err; | 2560 | return err; | 
| 2105 | } | 2561 | } | 
| 2106 | 2562 | ||
| 2107 | static int link_to_upper(struct btrfs_trans_handle *trans, | 2563 | static int link_to_upper(struct btrfs_trans_handle *trans, | 
| 2564 | struct reloc_control *rc, | ||
| 2108 | struct backref_node *node, | 2565 | struct backref_node *node, | 
| 2109 | struct btrfs_path *path) | 2566 | struct btrfs_path *path) | 
| 2110 | { | 2567 | { | 
| 2111 | struct btrfs_key key; | 2568 | struct btrfs_key key; | 
| 2112 | if (!node->eb || list_empty(&node->upper)) | ||
| 2113 | return 0; | ||
| 2114 | 2569 | ||
| 2115 | btrfs_node_key_to_cpu(node->eb, &key, 0); | 2570 | btrfs_node_key_to_cpu(node->eb, &key, 0); | 
| 2116 | return do_relocation(trans, node, &key, path, 0); | 2571 | return do_relocation(trans, rc, node, &key, path, 0); | 
| 2117 | } | 2572 | } | 
| 2118 | 2573 | ||
| 2119 | static int finish_pending_nodes(struct btrfs_trans_handle *trans, | 2574 | static int finish_pending_nodes(struct btrfs_trans_handle *trans, | 
| 2120 | struct backref_cache *cache, | 2575 | struct reloc_control *rc, | 
| 2121 | struct btrfs_path *path) | 2576 | struct btrfs_path *path, int err) | 
| 2122 | { | 2577 | { | 
| 2578 | LIST_HEAD(list); | ||
| 2579 | struct backref_cache *cache = &rc->backref_cache; | ||
| 2123 | struct backref_node *node; | 2580 | struct backref_node *node; | 
| 2124 | int level; | 2581 | int level; | 
| 2125 | int ret; | 2582 | int ret; | 
| 2126 | int err = 0; | ||
| 2127 | 2583 | ||
| 2128 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { | 2584 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { | 
| 2129 | while (!list_empty(&cache->pending[level])) { | 2585 | while (!list_empty(&cache->pending[level])) { | 
| 2130 | node = list_entry(cache->pending[level].next, | 2586 | node = list_entry(cache->pending[level].next, | 
| 2131 | struct backref_node, lower); | 2587 | struct backref_node, list); | 
| 2132 | BUG_ON(node->level != level); | 2588 | list_move_tail(&node->list, &list); | 
| 2589 | BUG_ON(!node->pending); | ||
| 2133 | 2590 | ||
| 2134 | ret = link_to_upper(trans, node, path); | 2591 | if (!err) { | 
| 2135 | if (ret < 0) | 2592 | ret = link_to_upper(trans, rc, node, path); | 
| 2136 | err = ret; | 2593 | if (ret < 0) | 
| 2137 | /* | 2594 | err = ret; | 
| 2138 | * this remove the node from the pending list and | 2595 | } | 
| 2139 | * may add some other nodes to the level + 1 | ||
| 2140 | * pending list | ||
| 2141 | */ | ||
| 2142 | remove_backref_node(cache, node); | ||
| 2143 | } | 2596 | } | 
| 2597 | list_splice_init(&list, &cache->pending[level]); | ||
| 2144 | } | 2598 | } | 
| 2145 | BUG_ON(!RB_EMPTY_ROOT(&cache->rb_root)); | ||
| 2146 | return err; | 2599 | return err; | 
| 2147 | } | 2600 | } | 
| 2148 | 2601 | ||
| 2149 | static void mark_block_processed(struct reloc_control *rc, | 2602 | static void mark_block_processed(struct reloc_control *rc, | 
| 2150 | struct backref_node *node) | 2603 | u64 bytenr, u32 blocksize) | 
| 2604 | { | ||
| 2605 | set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1, | ||
| 2606 | EXTENT_DIRTY, GFP_NOFS); | ||
| 2607 | } | ||
| 2608 | |||
| 2609 | static void __mark_block_processed(struct reloc_control *rc, | ||
| 2610 | struct backref_node *node) | ||
| 2151 | { | 2611 | { | 
| 2152 | u32 blocksize; | 2612 | u32 blocksize; | 
| 2153 | if (node->level == 0 || | 2613 | if (node->level == 0 || | 
| 2154 | in_block_group(node->bytenr, rc->block_group)) { | 2614 | in_block_group(node->bytenr, rc->block_group)) { | 
| 2155 | blocksize = btrfs_level_size(rc->extent_root, node->level); | 2615 | blocksize = btrfs_level_size(rc->extent_root, node->level); | 
| 2156 | set_extent_bits(&rc->processed_blocks, node->bytenr, | 2616 | mark_block_processed(rc, node->bytenr, blocksize); | 
| 2157 | node->bytenr + blocksize - 1, EXTENT_DIRTY, | ||
| 2158 | GFP_NOFS); | ||
| 2159 | } | 2617 | } | 
| 2160 | node->processed = 1; | 2618 | node->processed = 1; | 
| 2161 | } | 2619 | } | 
| @@ -2178,7 +2636,7 @@ static void update_processed_blocks(struct reloc_control *rc, | |||
| 2178 | if (next->processed) | 2636 | if (next->processed) | 
| 2179 | break; | 2637 | break; | 
| 2180 | 2638 | ||
| 2181 | mark_block_processed(rc, next); | 2639 | __mark_block_processed(rc, next); | 
| 2182 | 2640 | ||
| 2183 | if (list_empty(&next->upper)) | 2641 | if (list_empty(&next->upper)) | 
| 2184 | break; | 2642 | break; | 
| @@ -2201,138 +2659,6 @@ static int tree_block_processed(u64 bytenr, u32 blocksize, | |||
| 2201 | return 0; | 2659 | return 0; | 
| 2202 | } | 2660 | } | 
| 2203 | 2661 | ||
| 2204 | /* | ||
| 2205 | * check if there are any file extent pointers in the leaf point to | ||
| 2206 | * data require processing | ||
| 2207 | */ | ||
| 2208 | static int check_file_extents(struct reloc_control *rc, | ||
| 2209 | u64 bytenr, u32 blocksize, u64 ptr_gen) | ||
| 2210 | { | ||
| 2211 | struct btrfs_key found_key; | ||
| 2212 | struct btrfs_file_extent_item *fi; | ||
| 2213 | struct extent_buffer *leaf; | ||
| 2214 | u32 nritems; | ||
| 2215 | int i; | ||
| 2216 | int ret = 0; | ||
| 2217 | |||
| 2218 | leaf = read_tree_block(rc->extent_root, bytenr, blocksize, ptr_gen); | ||
| 2219 | |||
| 2220 | nritems = btrfs_header_nritems(leaf); | ||
| 2221 | for (i = 0; i < nritems; i++) { | ||
| 2222 | cond_resched(); | ||
| 2223 | btrfs_item_key_to_cpu(leaf, &found_key, i); | ||
| 2224 | if (found_key.type != BTRFS_EXTENT_DATA_KEY) | ||
| 2225 | continue; | ||
| 2226 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | ||
| 2227 | if (btrfs_file_extent_type(leaf, fi) == | ||
| 2228 | BTRFS_FILE_EXTENT_INLINE) | ||
| 2229 | continue; | ||
| 2230 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | ||
| 2231 | if (bytenr == 0) | ||
| 2232 | continue; | ||
| 2233 | if (in_block_group(bytenr, rc->block_group)) { | ||
| 2234 | ret = 1; | ||
| 2235 | break; | ||
| 2236 | } | ||
| 2237 | } | ||
| 2238 | free_extent_buffer(leaf); | ||
| 2239 | return ret; | ||
| 2240 | } | ||
| 2241 | |||
| 2242 | /* | ||
| 2243 | * scan child blocks of a given block to find blocks require processing | ||
| 2244 | */ | ||
| 2245 | static int add_child_blocks(struct btrfs_trans_handle *trans, | ||
| 2246 | struct reloc_control *rc, | ||
| 2247 | struct backref_node *node, | ||
| 2248 | struct rb_root *blocks) | ||
| 2249 | { | ||
| 2250 | struct tree_block *block; | ||
| 2251 | struct rb_node *rb_node; | ||
| 2252 | u64 bytenr; | ||
| 2253 | u64 ptr_gen; | ||
| 2254 | u32 blocksize; | ||
| 2255 | u32 nritems; | ||
| 2256 | int i; | ||
| 2257 | int err = 0; | ||
| 2258 | |||
| 2259 | nritems = btrfs_header_nritems(node->eb); | ||
| 2260 | blocksize = btrfs_level_size(rc->extent_root, node->level - 1); | ||
| 2261 | for (i = 0; i < nritems; i++) { | ||
| 2262 | cond_resched(); | ||
| 2263 | bytenr = btrfs_node_blockptr(node->eb, i); | ||
| 2264 | ptr_gen = btrfs_node_ptr_generation(node->eb, i); | ||
| 2265 | if (ptr_gen == trans->transid) | ||
| 2266 | continue; | ||
| 2267 | if (!in_block_group(bytenr, rc->block_group) && | ||
| 2268 | (node->level > 1 || rc->stage == MOVE_DATA_EXTENTS)) | ||
| 2269 | continue; | ||
| 2270 | if (tree_block_processed(bytenr, blocksize, rc)) | ||
| 2271 | continue; | ||
| 2272 | |||
| 2273 | readahead_tree_block(rc->extent_root, | ||
| 2274 | bytenr, blocksize, ptr_gen); | ||
| 2275 | } | ||
| 2276 | |||
| 2277 | for (i = 0; i < nritems; i++) { | ||
| 2278 | cond_resched(); | ||
| 2279 | bytenr = btrfs_node_blockptr(node->eb, i); | ||
| 2280 | ptr_gen = btrfs_node_ptr_generation(node->eb, i); | ||
| 2281 | if (ptr_gen == trans->transid) | ||
| 2282 | continue; | ||
| 2283 | if (!in_block_group(bytenr, rc->block_group) && | ||
| 2284 | (node->level > 1 || rc->stage == MOVE_DATA_EXTENTS)) | ||
| 2285 | continue; | ||
| 2286 | if (tree_block_processed(bytenr, blocksize, rc)) | ||
| 2287 | continue; | ||
| 2288 | if (!in_block_group(bytenr, rc->block_group) && | ||
| 2289 | !check_file_extents(rc, bytenr, blocksize, ptr_gen)) | ||
| 2290 | continue; | ||
| 2291 | |||
| 2292 | block = kmalloc(sizeof(*block), GFP_NOFS); | ||
| 2293 | if (!block) { | ||
| 2294 | err = -ENOMEM; | ||
| 2295 | break; | ||
| 2296 | } | ||
| 2297 | block->bytenr = bytenr; | ||
| 2298 | btrfs_node_key_to_cpu(node->eb, &block->key, i); | ||
| 2299 | block->level = node->level - 1; | ||
| 2300 | block->key_ready = 1; | ||
| 2301 | rb_node = tree_insert(blocks, block->bytenr, &block->rb_node); | ||
| 2302 | BUG_ON(rb_node); | ||
| 2303 | } | ||
| 2304 | if (err) | ||
| 2305 | free_block_list(blocks); | ||
| 2306 | return err; | ||
| 2307 | } | ||
| 2308 | |||
| 2309 | /* | ||
| 2310 | * find adjacent blocks require processing | ||
| 2311 | */ | ||
| 2312 | static noinline_for_stack | ||
| 2313 | int add_adjacent_blocks(struct btrfs_trans_handle *trans, | ||
| 2314 | struct reloc_control *rc, | ||
| 2315 | struct backref_cache *cache, | ||
| 2316 | struct rb_root *blocks, int level, | ||
| 2317 | struct backref_node **upper) | ||
| 2318 | { | ||
| 2319 | struct backref_node *node; | ||
| 2320 | int ret = 0; | ||
| 2321 | |||
| 2322 | WARN_ON(!list_empty(&cache->pending[level])); | ||
| 2323 | |||
| 2324 | if (list_empty(&cache->pending[level + 1])) | ||
| 2325 | return 1; | ||
| 2326 | |||
| 2327 | node = list_entry(cache->pending[level + 1].next, | ||
| 2328 | struct backref_node, lower); | ||
| 2329 | if (node->eb) | ||
| 2330 | ret = add_child_blocks(trans, rc, node, blocks); | ||
| 2331 | |||
| 2332 | *upper = node; | ||
| 2333 | return ret; | ||
| 2334 | } | ||
| 2335 | |||
| 2336 | static int get_tree_block_key(struct reloc_control *rc, | 2662 | static int get_tree_block_key(struct reloc_control *rc, | 
| 2337 | struct tree_block *block) | 2663 | struct tree_block *block) | 
| 2338 | { | 2664 | { | 
| @@ -2370,40 +2696,53 @@ static int relocate_tree_block(struct btrfs_trans_handle *trans, | |||
| 2370 | struct btrfs_path *path) | 2696 | struct btrfs_path *path) | 
| 2371 | { | 2697 | { | 
| 2372 | struct btrfs_root *root; | 2698 | struct btrfs_root *root; | 
| 2373 | int ret; | 2699 | int release = 0; | 
| 2700 | int ret = 0; | ||
| 2374 | 2701 | ||
| 2702 | if (!node) | ||
| 2703 | return 0; | ||
| 2704 | |||
| 2705 | BUG_ON(node->processed); | ||
| 2375 | root = select_one_root(trans, node); | 2706 | root = select_one_root(trans, node); | 
| 2376 | if (unlikely(!root)) { | 2707 | if (root == ERR_PTR(-ENOENT)) { | 
| 2377 | rc->found_old_snapshot = 1; | ||
| 2378 | update_processed_blocks(rc, node); | 2708 | update_processed_blocks(rc, node); | 
| 2379 | return 0; | 2709 | goto out; | 
| 2380 | } | 2710 | } | 
| 2381 | 2711 | ||
| 2382 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { | 2712 | if (!root || root->ref_cows) { | 
| 2383 | ret = do_relocation(trans, node, key, path, 1); | 2713 | ret = reserve_metadata_space(trans, rc, node); | 
| 2384 | if (ret < 0) | 2714 | if (ret) | 
| 2385 | goto out; | ||
| 2386 | if (node->level == 0 && rc->stage == UPDATE_DATA_PTRS) { | ||
| 2387 | ret = replace_file_extents(trans, rc, root, | ||
| 2388 | node->eb, NULL); | ||
| 2389 | if (ret < 0) | ||
| 2390 | goto out; | ||
| 2391 | } | ||
| 2392 | drop_node_buffer(node); | ||
| 2393 | } else if (!root->ref_cows) { | ||
| 2394 | path->lowest_level = node->level; | ||
| 2395 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | ||
| 2396 | btrfs_release_path(root, path); | ||
| 2397 | if (ret < 0) | ||
| 2398 | goto out; | 2715 | goto out; | 
| 2399 | } else if (root != node->root) { | 2716 | release = 1; | 
| 2400 | WARN_ON(node->level > 0 || rc->stage != UPDATE_DATA_PTRS); | ||
| 2401 | } | 2717 | } | 
| 2402 | 2718 | ||
| 2403 | update_processed_blocks(rc, node); | 2719 | if (root) { | 
| 2404 | ret = 0; | 2720 | if (root->ref_cows) { | 
| 2721 | BUG_ON(node->new_bytenr); | ||
| 2722 | BUG_ON(!list_empty(&node->list)); | ||
| 2723 | btrfs_record_root_in_trans(trans, root); | ||
| 2724 | root = root->reloc_root; | ||
| 2725 | node->new_bytenr = root->node->start; | ||
| 2726 | node->root = root; | ||
| 2727 | list_add_tail(&node->list, &rc->backref_cache.changed); | ||
| 2728 | } else { | ||
| 2729 | path->lowest_level = node->level; | ||
| 2730 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | ||
| 2731 | btrfs_release_path(root, path); | ||
| 2732 | if (ret > 0) | ||
| 2733 | ret = 0; | ||
| 2734 | } | ||
| 2735 | if (!ret) | ||
| 2736 | update_processed_blocks(rc, node); | ||
| 2737 | } else { | ||
| 2738 | ret = do_relocation(trans, rc, node, key, path, 1); | ||
| 2739 | } | ||
| 2405 | out: | 2740 | out: | 
| 2406 | drop_node_buffer(node); | 2741 | if (ret || node->level == 0 || node->cowonly) { | 
| 2742 | if (release) | ||
| 2743 | release_metadata_space(rc, node); | ||
| 2744 | remove_backref_node(&rc->backref_cache, node); | ||
| 2745 | } | ||
| 2407 | return ret; | 2746 | return ret; | 
| 2408 | } | 2747 | } | 
| 2409 | 2748 | ||
| @@ -2414,12 +2753,10 @@ static noinline_for_stack | |||
| 2414 | int relocate_tree_blocks(struct btrfs_trans_handle *trans, | 2753 | int relocate_tree_blocks(struct btrfs_trans_handle *trans, | 
| 2415 | struct reloc_control *rc, struct rb_root *blocks) | 2754 | struct reloc_control *rc, struct rb_root *blocks) | 
| 2416 | { | 2755 | { | 
| 2417 | struct backref_cache *cache; | ||
| 2418 | struct backref_node *node; | 2756 | struct backref_node *node; | 
| 2419 | struct btrfs_path *path; | 2757 | struct btrfs_path *path; | 
| 2420 | struct tree_block *block; | 2758 | struct tree_block *block; | 
| 2421 | struct rb_node *rb_node; | 2759 | struct rb_node *rb_node; | 
| 2422 | int level = -1; | ||
| 2423 | int ret; | 2760 | int ret; | 
| 2424 | int err = 0; | 2761 | int err = 0; | 
| 2425 | 2762 | ||
| @@ -2427,21 +2764,9 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans, | |||
| 2427 | if (!path) | 2764 | if (!path) | 
| 2428 | return -ENOMEM; | 2765 | return -ENOMEM; | 
| 2429 | 2766 | ||
| 2430 | cache = kmalloc(sizeof(*cache), GFP_NOFS); | ||
| 2431 | if (!cache) { | ||
| 2432 | btrfs_free_path(path); | ||
| 2433 | return -ENOMEM; | ||
| 2434 | } | ||
| 2435 | |||
| 2436 | backref_cache_init(cache); | ||
| 2437 | |||
| 2438 | rb_node = rb_first(blocks); | 2767 | rb_node = rb_first(blocks); | 
| 2439 | while (rb_node) { | 2768 | while (rb_node) { | 
| 2440 | block = rb_entry(rb_node, struct tree_block, rb_node); | 2769 | block = rb_entry(rb_node, struct tree_block, rb_node); | 
| 2441 | if (level == -1) | ||
| 2442 | level = block->level; | ||
| 2443 | else | ||
| 2444 | BUG_ON(level != block->level); | ||
| 2445 | if (!block->key_ready) | 2770 | if (!block->key_ready) | 
| 2446 | reada_tree_block(rc, block); | 2771 | reada_tree_block(rc, block); | 
| 2447 | rb_node = rb_next(rb_node); | 2772 | rb_node = rb_next(rb_node); | 
| @@ -2459,7 +2784,7 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans, | |||
| 2459 | while (rb_node) { | 2784 | while (rb_node) { | 
| 2460 | block = rb_entry(rb_node, struct tree_block, rb_node); | 2785 | block = rb_entry(rb_node, struct tree_block, rb_node); | 
| 2461 | 2786 | ||
| 2462 | node = build_backref_tree(rc, cache, &block->key, | 2787 | node = build_backref_tree(rc, &block->key, | 
| 2463 | block->level, block->bytenr); | 2788 | block->level, block->bytenr); | 
| 2464 | if (IS_ERR(node)) { | 2789 | if (IS_ERR(node)) { | 
| 2465 | err = PTR_ERR(node); | 2790 | err = PTR_ERR(node); | 
| @@ -2469,79 +2794,62 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans, | |||
| 2469 | ret = relocate_tree_block(trans, rc, node, &block->key, | 2794 | ret = relocate_tree_block(trans, rc, node, &block->key, | 
| 2470 | path); | 2795 | path); | 
| 2471 | if (ret < 0) { | 2796 | if (ret < 0) { | 
| 2472 | err = ret; | 2797 | if (ret != -EAGAIN || rb_node == rb_first(blocks)) | 
| 2798 | err = ret; | ||
| 2473 | goto out; | 2799 | goto out; | 
| 2474 | } | 2800 | } | 
| 2475 | remove_backref_node(cache, node); | ||
| 2476 | rb_node = rb_next(rb_node); | 2801 | rb_node = rb_next(rb_node); | 
| 2477 | } | 2802 | } | 
| 2478 | 2803 | out: | |
| 2479 | if (level > 0) | ||
| 2480 | goto out; | ||
| 2481 | |||
| 2482 | free_block_list(blocks); | 2804 | free_block_list(blocks); | 
| 2805 | err = finish_pending_nodes(trans, rc, path, err); | ||
| 2483 | 2806 | ||
| 2484 | /* | 2807 | btrfs_free_path(path); | 
| 2485 | * now backrefs of some upper level tree blocks have been cached, | 2808 | return err; | 
| 2486 | * try relocating blocks referenced by these upper level blocks. | 2809 | } | 
| 2487 | */ | ||
| 2488 | while (1) { | ||
| 2489 | struct backref_node *upper = NULL; | ||
| 2490 | if (trans->transaction->in_commit || | ||
| 2491 | trans->transaction->delayed_refs.flushing) | ||
| 2492 | break; | ||
| 2493 | 2810 | ||
| 2494 | ret = add_adjacent_blocks(trans, rc, cache, blocks, level, | 2811 | static noinline_for_stack | 
| 2495 | &upper); | 2812 | int prealloc_file_extent_cluster(struct inode *inode, | 
| 2496 | if (ret < 0) | 2813 | struct file_extent_cluster *cluster) | 
| 2497 | err = ret; | 2814 | { | 
| 2498 | if (ret != 0) | 2815 | u64 alloc_hint = 0; | 
| 2499 | break; | 2816 | u64 start; | 
| 2817 | u64 end; | ||
| 2818 | u64 offset = BTRFS_I(inode)->index_cnt; | ||
| 2819 | u64 num_bytes; | ||
| 2820 | int nr = 0; | ||
| 2821 | int ret = 0; | ||
| 2500 | 2822 | ||
| 2501 | rb_node = rb_first(blocks); | 2823 | BUG_ON(cluster->start != cluster->boundary[0]); | 
| 2502 | while (rb_node) { | 2824 | mutex_lock(&inode->i_mutex); | 
| 2503 | block = rb_entry(rb_node, struct tree_block, rb_node); | ||
| 2504 | if (trans->transaction->in_commit || | ||
| 2505 | trans->transaction->delayed_refs.flushing) | ||
| 2506 | goto out; | ||
| 2507 | BUG_ON(!block->key_ready); | ||
| 2508 | node = build_backref_tree(rc, cache, &block->key, | ||
| 2509 | level, block->bytenr); | ||
| 2510 | if (IS_ERR(node)) { | ||
| 2511 | err = PTR_ERR(node); | ||
| 2512 | goto out; | ||
| 2513 | } | ||
| 2514 | 2825 | ||
| 2515 | ret = relocate_tree_block(trans, rc, node, | 2826 | ret = btrfs_check_data_free_space(inode, cluster->end + | 
| 2516 | &block->key, path); | 2827 | 1 - cluster->start); | 
| 2517 | if (ret < 0) { | 2828 | if (ret) | 
| 2518 | err = ret; | 2829 | goto out; | 
| 2519 | goto out; | ||
| 2520 | } | ||
| 2521 | remove_backref_node(cache, node); | ||
| 2522 | rb_node = rb_next(rb_node); | ||
| 2523 | } | ||
| 2524 | free_block_list(blocks); | ||
| 2525 | 2830 | ||
| 2526 | if (upper) { | 2831 | while (nr < cluster->nr) { | 
| 2527 | ret = link_to_upper(trans, upper, path); | 2832 | start = cluster->boundary[nr] - offset; | 
| 2528 | if (ret < 0) { | 2833 | if (nr + 1 < cluster->nr) | 
| 2529 | err = ret; | 2834 | end = cluster->boundary[nr + 1] - 1 - offset; | 
| 2530 | break; | 2835 | else | 
| 2531 | } | 2836 | end = cluster->end - offset; | 
| 2532 | remove_backref_node(cache, upper); | 2837 | |
| 2533 | } | 2838 | lock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS); | 
| 2839 | num_bytes = end + 1 - start; | ||
| 2840 | ret = btrfs_prealloc_file_range(inode, 0, start, | ||
| 2841 | num_bytes, num_bytes, | ||
| 2842 | end + 1, &alloc_hint); | ||
| 2843 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS); | ||
| 2844 | if (ret) | ||
| 2845 | break; | ||
| 2846 | nr++; | ||
| 2534 | } | 2847 | } | 
| 2848 | btrfs_free_reserved_data_space(inode, cluster->end + | ||
| 2849 | 1 - cluster->start); | ||
| 2535 | out: | 2850 | out: | 
| 2536 | free_block_list(blocks); | 2851 | mutex_unlock(&inode->i_mutex); | 
| 2537 | 2852 | return ret; | |
| 2538 | ret = finish_pending_nodes(trans, cache, path); | ||
| 2539 | if (ret < 0) | ||
| 2540 | err = ret; | ||
| 2541 | |||
| 2542 | kfree(cache); | ||
| 2543 | btrfs_free_path(path); | ||
| 2544 | return err; | ||
| 2545 | } | 2853 | } | 
| 2546 | 2854 | ||
| 2547 | static noinline_for_stack | 2855 | static noinline_for_stack | 
| @@ -2587,7 +2895,6 @@ static int relocate_file_extent_cluster(struct inode *inode, | |||
| 2587 | u64 offset = BTRFS_I(inode)->index_cnt; | 2895 | u64 offset = BTRFS_I(inode)->index_cnt; | 
| 2588 | unsigned long index; | 2896 | unsigned long index; | 
| 2589 | unsigned long last_index; | 2897 | unsigned long last_index; | 
| 2590 | unsigned int dirty_page = 0; | ||
| 2591 | struct page *page; | 2898 | struct page *page; | 
| 2592 | struct file_ra_state *ra; | 2899 | struct file_ra_state *ra; | 
| 2593 | int nr = 0; | 2900 | int nr = 0; | 
| @@ -2600,21 +2907,24 @@ static int relocate_file_extent_cluster(struct inode *inode, | |||
| 2600 | if (!ra) | 2907 | if (!ra) | 
| 2601 | return -ENOMEM; | 2908 | return -ENOMEM; | 
| 2602 | 2909 | ||
| 2603 | index = (cluster->start - offset) >> PAGE_CACHE_SHIFT; | 2910 | ret = prealloc_file_extent_cluster(inode, cluster); | 
| 2604 | last_index = (cluster->end - offset) >> PAGE_CACHE_SHIFT; | 2911 | if (ret) | 
| 2912 | goto out; | ||
| 2605 | 2913 | ||
| 2606 | mutex_lock(&inode->i_mutex); | 2914 | file_ra_state_init(ra, inode->i_mapping); | 
| 2607 | 2915 | ||
| 2608 | i_size_write(inode, cluster->end + 1 - offset); | ||
| 2609 | ret = setup_extent_mapping(inode, cluster->start - offset, | 2916 | ret = setup_extent_mapping(inode, cluster->start - offset, | 
| 2610 | cluster->end - offset, cluster->start); | 2917 | cluster->end - offset, cluster->start); | 
| 2611 | if (ret) | 2918 | if (ret) | 
| 2612 | goto out_unlock; | 2919 | goto out; | 
| 2613 | |||
| 2614 | file_ra_state_init(ra, inode->i_mapping); | ||
| 2615 | 2920 | ||
| 2616 | WARN_ON(cluster->start != cluster->boundary[0]); | 2921 | index = (cluster->start - offset) >> PAGE_CACHE_SHIFT; | 
| 2922 | last_index = (cluster->end - offset) >> PAGE_CACHE_SHIFT; | ||
| 2617 | while (index <= last_index) { | 2923 | while (index <= last_index) { | 
| 2924 | ret = btrfs_delalloc_reserve_metadata(inode, PAGE_CACHE_SIZE); | ||
| 2925 | if (ret) | ||
| 2926 | goto out; | ||
| 2927 | |||
| 2618 | page = find_lock_page(inode->i_mapping, index); | 2928 | page = find_lock_page(inode->i_mapping, index); | 
| 2619 | if (!page) { | 2929 | if (!page) { | 
| 2620 | page_cache_sync_readahead(inode->i_mapping, | 2930 | page_cache_sync_readahead(inode->i_mapping, | 
| @@ -2622,8 +2932,10 @@ static int relocate_file_extent_cluster(struct inode *inode, | |||
| 2622 | last_index + 1 - index); | 2932 | last_index + 1 - index); | 
| 2623 | page = grab_cache_page(inode->i_mapping, index); | 2933 | page = grab_cache_page(inode->i_mapping, index); | 
| 2624 | if (!page) { | 2934 | if (!page) { | 
| 2935 | btrfs_delalloc_release_metadata(inode, | ||
| 2936 | PAGE_CACHE_SIZE); | ||
| 2625 | ret = -ENOMEM; | 2937 | ret = -ENOMEM; | 
| 2626 | goto out_unlock; | 2938 | goto out; | 
| 2627 | } | 2939 | } | 
| 2628 | } | 2940 | } | 
| 2629 | 2941 | ||
| @@ -2639,8 +2951,10 @@ static int relocate_file_extent_cluster(struct inode *inode, | |||
| 2639 | if (!PageUptodate(page)) { | 2951 | if (!PageUptodate(page)) { | 
| 2640 | unlock_page(page); | 2952 | unlock_page(page); | 
| 2641 | page_cache_release(page); | 2953 | page_cache_release(page); | 
| 2954 | btrfs_delalloc_release_metadata(inode, | ||
| 2955 | PAGE_CACHE_SIZE); | ||
| 2642 | ret = -EIO; | 2956 | ret = -EIO; | 
| 2643 | goto out_unlock; | 2957 | goto out; | 
| 2644 | } | 2958 | } | 
| 2645 | } | 2959 | } | 
| 2646 | 2960 | ||
| @@ -2659,10 +2973,9 @@ static int relocate_file_extent_cluster(struct inode *inode, | |||
| 2659 | EXTENT_BOUNDARY, GFP_NOFS); | 2973 | EXTENT_BOUNDARY, GFP_NOFS); | 
| 2660 | nr++; | 2974 | nr++; | 
| 2661 | } | 2975 | } | 
| 2662 | btrfs_set_extent_delalloc(inode, page_start, page_end, NULL); | ||
| 2663 | 2976 | ||
| 2977 | btrfs_set_extent_delalloc(inode, page_start, page_end, NULL); | ||
| 2664 | set_page_dirty(page); | 2978 | set_page_dirty(page); | 
| 2665 | dirty_page++; | ||
| 2666 | 2979 | ||
| 2667 | unlock_extent(&BTRFS_I(inode)->io_tree, | 2980 | unlock_extent(&BTRFS_I(inode)->io_tree, | 
| 2668 | page_start, page_end, GFP_NOFS); | 2981 | page_start, page_end, GFP_NOFS); | 
| @@ -2670,20 +2983,11 @@ static int relocate_file_extent_cluster(struct inode *inode, | |||
| 2670 | page_cache_release(page); | 2983 | page_cache_release(page); | 
| 2671 | 2984 | ||
| 2672 | index++; | 2985 | index++; | 
| 2673 | if (nr < cluster->nr && | 2986 | balance_dirty_pages_ratelimited(inode->i_mapping); | 
| 2674 | page_end + 1 + offset == cluster->boundary[nr]) { | 2987 | btrfs_throttle(BTRFS_I(inode)->root); | 
| 2675 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, | ||
| 2676 | dirty_page); | ||
| 2677 | dirty_page = 0; | ||
| 2678 | } | ||
| 2679 | } | ||
| 2680 | if (dirty_page) { | ||
| 2681 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, | ||
| 2682 | dirty_page); | ||
| 2683 | } | 2988 | } | 
| 2684 | WARN_ON(nr != cluster->nr); | 2989 | WARN_ON(nr != cluster->nr); | 
| 2685 | out_unlock: | 2990 | out: | 
| 2686 | mutex_unlock(&inode->i_mutex); | ||
| 2687 | kfree(ra); | 2991 | kfree(ra); | 
| 2688 | return ret; | 2992 | return ret; | 
| 2689 | } | 2993 | } | 
| @@ -2869,9 +3173,6 @@ out: | |||
| 2869 | static int block_use_full_backref(struct reloc_control *rc, | 3173 | static int block_use_full_backref(struct reloc_control *rc, | 
| 2870 | struct extent_buffer *eb) | 3174 | struct extent_buffer *eb) | 
| 2871 | { | 3175 | { | 
| 2872 | struct btrfs_path *path; | ||
| 2873 | struct btrfs_extent_item *ei; | ||
| 2874 | struct btrfs_key key; | ||
| 2875 | u64 flags; | 3176 | u64 flags; | 
| 2876 | int ret; | 3177 | int ret; | 
| 2877 | 3178 | ||
| @@ -2879,28 +3180,14 @@ static int block_use_full_backref(struct reloc_control *rc, | |||
| 2879 | btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV) | 3180 | btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV) | 
| 2880 | return 1; | 3181 | return 1; | 
| 2881 | 3182 | ||
| 2882 | path = btrfs_alloc_path(); | 3183 | ret = btrfs_lookup_extent_info(NULL, rc->extent_root, | 
| 2883 | BUG_ON(!path); | 3184 | eb->start, eb->len, NULL, &flags); | 
| 2884 | |||
| 2885 | key.objectid = eb->start; | ||
| 2886 | key.type = BTRFS_EXTENT_ITEM_KEY; | ||
| 2887 | key.offset = eb->len; | ||
| 2888 | |||
| 2889 | path->search_commit_root = 1; | ||
| 2890 | path->skip_locking = 1; | ||
| 2891 | ret = btrfs_search_slot(NULL, rc->extent_root, | ||
| 2892 | &key, path, 0, 0); | ||
| 2893 | BUG_ON(ret); | 3185 | BUG_ON(ret); | 
| 2894 | 3186 | ||
| 2895 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | ||
| 2896 | struct btrfs_extent_item); | ||
| 2897 | flags = btrfs_extent_flags(path->nodes[0], ei); | ||
| 2898 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); | ||
| 2899 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) | 3187 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) | 
| 2900 | ret = 1; | 3188 | ret = 1; | 
| 2901 | else | 3189 | else | 
| 2902 | ret = 0; | 3190 | ret = 0; | 
| 2903 | btrfs_free_path(path); | ||
| 2904 | return ret; | 3191 | return ret; | 
| 2905 | } | 3192 | } | 
| 2906 | 3193 | ||
| @@ -3073,22 +3360,10 @@ int add_data_references(struct reloc_control *rc, | |||
| 3073 | struct btrfs_extent_inline_ref *iref; | 3360 | struct btrfs_extent_inline_ref *iref; | 
| 3074 | unsigned long ptr; | 3361 | unsigned long ptr; | 
| 3075 | unsigned long end; | 3362 | unsigned long end; | 
| 3076 | u32 blocksize; | 3363 | u32 blocksize = btrfs_level_size(rc->extent_root, 0); | 
| 3077 | int ret; | 3364 | int ret; | 
| 3078 | int err = 0; | 3365 | int err = 0; | 
| 3079 | 3366 | ||
| 3080 | ret = get_new_location(rc->data_inode, NULL, extent_key->objectid, | ||
| 3081 | extent_key->offset); | ||
| 3082 | BUG_ON(ret < 0); | ||
| 3083 | if (ret > 0) { | ||
| 3084 | /* the relocated data is fragmented */ | ||
| 3085 | rc->extents_skipped++; | ||
| 3086 | btrfs_release_path(rc->extent_root, path); | ||
| 3087 | return 0; | ||
| 3088 | } | ||
| 3089 | |||
| 3090 | blocksize = btrfs_level_size(rc->extent_root, 0); | ||
| 3091 | |||
| 3092 | eb = path->nodes[0]; | 3367 | eb = path->nodes[0]; | 
| 3093 | ptr = btrfs_item_ptr_offset(eb, path->slots[0]); | 3368 | ptr = btrfs_item_ptr_offset(eb, path->slots[0]); | 
| 3094 | end = ptr + btrfs_item_size_nr(eb, path->slots[0]); | 3369 | end = ptr + btrfs_item_size_nr(eb, path->slots[0]); | 
| @@ -3169,7 +3444,8 @@ int add_data_references(struct reloc_control *rc, | |||
| 3169 | */ | 3444 | */ | 
| 3170 | static noinline_for_stack | 3445 | static noinline_for_stack | 
| 3171 | int find_next_extent(struct btrfs_trans_handle *trans, | 3446 | int find_next_extent(struct btrfs_trans_handle *trans, | 
| 3172 | struct reloc_control *rc, struct btrfs_path *path) | 3447 | struct reloc_control *rc, struct btrfs_path *path, | 
| 3448 | struct btrfs_key *extent_key) | ||
| 3173 | { | 3449 | { | 
| 3174 | struct btrfs_key key; | 3450 | struct btrfs_key key; | 
| 3175 | struct extent_buffer *leaf; | 3451 | struct extent_buffer *leaf; | 
| @@ -3224,6 +3500,7 @@ next: | |||
| 3224 | rc->search_start = end + 1; | 3500 | rc->search_start = end + 1; | 
| 3225 | } else { | 3501 | } else { | 
| 3226 | rc->search_start = key.objectid + key.offset; | 3502 | rc->search_start = key.objectid + key.offset; | 
| 3503 | memcpy(extent_key, &key, sizeof(key)); | ||
| 3227 | return 0; | 3504 | return 0; | 
| 3228 | } | 3505 | } | 
| 3229 | } | 3506 | } | 
| @@ -3261,12 +3538,49 @@ static int check_extent_flags(u64 flags) | |||
| 3261 | return 0; | 3538 | return 0; | 
| 3262 | } | 3539 | } | 
| 3263 | 3540 | ||
| 3541 | static noinline_for_stack | ||
| 3542 | int prepare_to_relocate(struct reloc_control *rc) | ||
| 3543 | { | ||
| 3544 | struct btrfs_trans_handle *trans; | ||
| 3545 | int ret; | ||
| 3546 | |||
| 3547 | rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root); | ||
| 3548 | if (!rc->block_rsv) | ||
| 3549 | return -ENOMEM; | ||
| 3550 | |||
| 3551 | /* | ||
| 3552 | * reserve some space for creating reloc trees. | ||
| 3553 | * btrfs_init_reloc_root will use them when there | ||
| 3554 | * is no reservation in transaction handle. | ||
| 3555 | */ | ||
| 3556 | ret = btrfs_block_rsv_add(NULL, rc->extent_root, rc->block_rsv, | ||
| 3557 | rc->extent_root->nodesize * 256, | ||
| 3558 | &rc->block_rsv_retries); | ||
| 3559 | if (ret) | ||
| 3560 | return ret; | ||
| 3561 | |||
| 3562 | rc->block_rsv->refill_used = 1; | ||
| 3563 | btrfs_add_durable_block_rsv(rc->extent_root->fs_info, rc->block_rsv); | ||
| 3564 | |||
| 3565 | memset(&rc->cluster, 0, sizeof(rc->cluster)); | ||
| 3566 | rc->search_start = rc->block_group->key.objectid; | ||
| 3567 | rc->extents_found = 0; | ||
| 3568 | rc->nodes_relocated = 0; | ||
| 3569 | rc->merging_rsv_size = 0; | ||
| 3570 | rc->block_rsv_retries = 0; | ||
| 3571 | |||
| 3572 | rc->create_reloc_tree = 1; | ||
| 3573 | set_reloc_control(rc); | ||
| 3574 | |||
| 3575 | trans = btrfs_join_transaction(rc->extent_root, 1); | ||
| 3576 | btrfs_commit_transaction(trans, rc->extent_root); | ||
| 3577 | return 0; | ||
| 3578 | } | ||
| 3264 | 3579 | ||
| 3265 | static noinline_for_stack int relocate_block_group(struct reloc_control *rc) | 3580 | static noinline_for_stack int relocate_block_group(struct reloc_control *rc) | 
| 3266 | { | 3581 | { | 
| 3267 | struct rb_root blocks = RB_ROOT; | 3582 | struct rb_root blocks = RB_ROOT; | 
| 3268 | struct btrfs_key key; | 3583 | struct btrfs_key key; | 
| 3269 | struct file_extent_cluster *cluster; | ||
| 3270 | struct btrfs_trans_handle *trans = NULL; | 3584 | struct btrfs_trans_handle *trans = NULL; | 
| 3271 | struct btrfs_path *path; | 3585 | struct btrfs_path *path; | 
| 3272 | struct btrfs_extent_item *ei; | 3586 | struct btrfs_extent_item *ei; | 
| @@ -3276,33 +3590,25 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) | |||
| 3276 | int ret; | 3590 | int ret; | 
| 3277 | int err = 0; | 3591 | int err = 0; | 
| 3278 | 3592 | ||
| 3279 | cluster = kzalloc(sizeof(*cluster), GFP_NOFS); | ||
| 3280 | if (!cluster) | ||
| 3281 | return -ENOMEM; | ||
| 3282 | |||
| 3283 | path = btrfs_alloc_path(); | 3593 | path = btrfs_alloc_path(); | 
| 3284 | if (!path) { | 3594 | if (!path) | 
| 3285 | kfree(cluster); | ||
| 3286 | return -ENOMEM; | 3595 | return -ENOMEM; | 
| 3287 | } | ||
| 3288 | |||
| 3289 | rc->extents_found = 0; | ||
| 3290 | rc->extents_skipped = 0; | ||
| 3291 | |||
| 3292 | rc->search_start = rc->block_group->key.objectid; | ||
| 3293 | clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY, | ||
| 3294 | GFP_NOFS); | ||
| 3295 | |||
| 3296 | rc->create_reloc_root = 1; | ||
| 3297 | set_reloc_control(rc); | ||
| 3298 | 3596 | ||
| 3299 | trans = btrfs_start_transaction(rc->extent_root, 1); | 3597 | ret = prepare_to_relocate(rc); | 
| 3300 | btrfs_commit_transaction(trans, rc->extent_root); | 3598 | if (ret) { | 
| 3599 | err = ret; | ||
| 3600 | goto out_free; | ||
| 3601 | } | ||
| 3301 | 3602 | ||
| 3302 | while (1) { | 3603 | while (1) { | 
| 3303 | trans = btrfs_start_transaction(rc->extent_root, 1); | 3604 | trans = btrfs_start_transaction(rc->extent_root, 0); | 
| 3605 | |||
| 3606 | if (update_backref_cache(trans, &rc->backref_cache)) { | ||
| 3607 | btrfs_end_transaction(trans, rc->extent_root); | ||
| 3608 | continue; | ||
| 3609 | } | ||
| 3304 | 3610 | ||
| 3305 | ret = find_next_extent(trans, rc, path); | 3611 | ret = find_next_extent(trans, rc, path, &key); | 
| 3306 | if (ret < 0) | 3612 | if (ret < 0) | 
| 3307 | err = ret; | 3613 | err = ret; | 
| 3308 | if (ret != 0) | 3614 | if (ret != 0) | 
| @@ -3312,9 +3618,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) | |||
| 3312 | 3618 | ||
| 3313 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | 3619 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | 
| 3314 | struct btrfs_extent_item); | 3620 | struct btrfs_extent_item); | 
| 3315 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | 3621 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); | 
| 3316 | item_size = btrfs_item_size_nr(path->nodes[0], | ||
| 3317 | path->slots[0]); | ||
| 3318 | if (item_size >= sizeof(*ei)) { | 3622 | if (item_size >= sizeof(*ei)) { | 
| 3319 | flags = btrfs_extent_flags(path->nodes[0], ei); | 3623 | flags = btrfs_extent_flags(path->nodes[0], ei); | 
| 3320 | ret = check_extent_flags(flags); | 3624 | ret = check_extent_flags(flags); | 
| @@ -3355,73 +3659,100 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) | |||
| 3355 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | 3659 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | 
| 3356 | ret = add_tree_block(rc, &key, path, &blocks); | 3660 | ret = add_tree_block(rc, &key, path, &blocks); | 
| 3357 | } else if (rc->stage == UPDATE_DATA_PTRS && | 3661 | } else if (rc->stage == UPDATE_DATA_PTRS && | 
| 3358 | (flags & BTRFS_EXTENT_FLAG_DATA)) { | 3662 | (flags & BTRFS_EXTENT_FLAG_DATA)) { | 
| 3359 | ret = add_data_references(rc, &key, path, &blocks); | 3663 | ret = add_data_references(rc, &key, path, &blocks); | 
| 3360 | } else { | 3664 | } else { | 
| 3361 | btrfs_release_path(rc->extent_root, path); | 3665 | btrfs_release_path(rc->extent_root, path); | 
| 3362 | ret = 0; | 3666 | ret = 0; | 
| 3363 | } | 3667 | } | 
| 3364 | if (ret < 0) { | 3668 | if (ret < 0) { | 
| 3365 | err = 0; | 3669 | err = ret; | 
| 3366 | break; | 3670 | break; | 
| 3367 | } | 3671 | } | 
| 3368 | 3672 | ||
| 3369 | if (!RB_EMPTY_ROOT(&blocks)) { | 3673 | if (!RB_EMPTY_ROOT(&blocks)) { | 
| 3370 | ret = relocate_tree_blocks(trans, rc, &blocks); | 3674 | ret = relocate_tree_blocks(trans, rc, &blocks); | 
| 3371 | if (ret < 0) { | 3675 | if (ret < 0) { | 
| 3676 | if (ret != -EAGAIN) { | ||
| 3677 | err = ret; | ||
| 3678 | break; | ||
| 3679 | } | ||
| 3680 | rc->extents_found--; | ||
| 3681 | rc->search_start = key.objectid; | ||
| 3682 | } | ||
| 3683 | } | ||
| 3684 | |||
| 3685 | ret = btrfs_block_rsv_check(trans, rc->extent_root, | ||
| 3686 | rc->block_rsv, 0, 5); | ||
| 3687 | if (ret < 0) { | ||
| 3688 | if (ret != -EAGAIN) { | ||
| 3372 | err = ret; | 3689 | err = ret; | 
| 3690 | WARN_ON(1); | ||
| 3373 | break; | 3691 | break; | 
| 3374 | } | 3692 | } | 
| 3693 | rc->commit_transaction = 1; | ||
| 3375 | } | 3694 | } | 
| 3376 | 3695 | ||
| 3377 | nr = trans->blocks_used; | 3696 | if (rc->commit_transaction) { | 
| 3378 | btrfs_end_transaction(trans, rc->extent_root); | 3697 | rc->commit_transaction = 0; | 
| 3698 | ret = btrfs_commit_transaction(trans, rc->extent_root); | ||
| 3699 | BUG_ON(ret); | ||
| 3700 | } else { | ||
| 3701 | nr = trans->blocks_used; | ||
| 3702 | btrfs_end_transaction_throttle(trans, rc->extent_root); | ||
| 3703 | btrfs_btree_balance_dirty(rc->extent_root, nr); | ||
| 3704 | } | ||
| 3379 | trans = NULL; | 3705 | trans = NULL; | 
| 3380 | btrfs_btree_balance_dirty(rc->extent_root, nr); | ||
| 3381 | 3706 | ||
| 3382 | if (rc->stage == MOVE_DATA_EXTENTS && | 3707 | if (rc->stage == MOVE_DATA_EXTENTS && | 
| 3383 | (flags & BTRFS_EXTENT_FLAG_DATA)) { | 3708 | (flags & BTRFS_EXTENT_FLAG_DATA)) { | 
| 3384 | rc->found_file_extent = 1; | 3709 | rc->found_file_extent = 1; | 
| 3385 | ret = relocate_data_extent(rc->data_inode, | 3710 | ret = relocate_data_extent(rc->data_inode, | 
| 3386 | &key, cluster); | 3711 | &key, &rc->cluster); | 
| 3387 | if (ret < 0) { | 3712 | if (ret < 0) { | 
| 3388 | err = ret; | 3713 | err = ret; | 
| 3389 | break; | 3714 | break; | 
| 3390 | } | 3715 | } | 
| 3391 | } | 3716 | } | 
| 3392 | } | 3717 | } | 
| 3393 | btrfs_free_path(path); | 3718 | |
| 3719 | btrfs_release_path(rc->extent_root, path); | ||
| 3720 | clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY, | ||
| 3721 | GFP_NOFS); | ||
| 3394 | 3722 | ||
| 3395 | if (trans) { | 3723 | if (trans) { | 
| 3396 | nr = trans->blocks_used; | 3724 | nr = trans->blocks_used; | 
| 3397 | btrfs_end_transaction(trans, rc->extent_root); | 3725 | btrfs_end_transaction_throttle(trans, rc->extent_root); | 
| 3398 | btrfs_btree_balance_dirty(rc->extent_root, nr); | 3726 | btrfs_btree_balance_dirty(rc->extent_root, nr); | 
| 3399 | } | 3727 | } | 
| 3400 | 3728 | ||
| 3401 | if (!err) { | 3729 | if (!err) { | 
| 3402 | ret = relocate_file_extent_cluster(rc->data_inode, cluster); | 3730 | ret = relocate_file_extent_cluster(rc->data_inode, | 
| 3731 | &rc->cluster); | ||
| 3403 | if (ret < 0) | 3732 | if (ret < 0) | 
| 3404 | err = ret; | 3733 | err = ret; | 
| 3405 | } | 3734 | } | 
| 3406 | 3735 | ||
| 3407 | kfree(cluster); | 3736 | rc->create_reloc_tree = 0; | 
| 3737 | set_reloc_control(rc); | ||
| 3408 | 3738 | ||
| 3409 | rc->create_reloc_root = 0; | 3739 | backref_cache_cleanup(&rc->backref_cache); | 
| 3410 | smp_mb(); | 3740 | btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1); | 
| 3411 | 3741 | ||
| 3412 | if (rc->extents_found > 0) { | 3742 | err = prepare_to_merge(rc, err); | 
| 3413 | trans = btrfs_start_transaction(rc->extent_root, 1); | ||
| 3414 | btrfs_commit_transaction(trans, rc->extent_root); | ||
| 3415 | } | ||
| 3416 | 3743 | ||
| 3417 | merge_reloc_roots(rc); | 3744 | merge_reloc_roots(rc); | 
| 3418 | 3745 | ||
| 3746 | rc->merge_reloc_tree = 0; | ||
| 3419 | unset_reloc_control(rc); | 3747 | unset_reloc_control(rc); | 
| 3748 | btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1); | ||
| 3420 | 3749 | ||
| 3421 | /* get rid of pinned extents */ | 3750 | /* get rid of pinned extents */ | 
| 3422 | trans = btrfs_start_transaction(rc->extent_root, 1); | 3751 | trans = btrfs_join_transaction(rc->extent_root, 1); | 
| 3423 | btrfs_commit_transaction(trans, rc->extent_root); | 3752 | btrfs_commit_transaction(trans, rc->extent_root); | 
| 3424 | 3753 | out_free: | |
| 3754 | btrfs_free_block_rsv(rc->extent_root, rc->block_rsv); | ||
| 3755 | btrfs_free_path(path); | ||
| 3425 | return err; | 3756 | return err; | 
| 3426 | } | 3757 | } | 
| 3427 | 3758 | ||
| @@ -3447,7 +3778,8 @@ static int __insert_orphan_inode(struct btrfs_trans_handle *trans, | |||
| 3447 | btrfs_set_inode_generation(leaf, item, 1); | 3778 | btrfs_set_inode_generation(leaf, item, 1); | 
| 3448 | btrfs_set_inode_size(leaf, item, 0); | 3779 | btrfs_set_inode_size(leaf, item, 0); | 
| 3449 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); | 3780 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); | 
| 3450 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS); | 3781 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS | | 
| 3782 | BTRFS_INODE_PREALLOC); | ||
| 3451 | btrfs_mark_buffer_dirty(leaf); | 3783 | btrfs_mark_buffer_dirty(leaf); | 
| 3452 | btrfs_release_path(root, path); | 3784 | btrfs_release_path(root, path); | 
| 3453 | out: | 3785 | out: | 
| @@ -3459,8 +3791,9 @@ out: | |||
| 3459 | * helper to create inode for data relocation. | 3791 | * helper to create inode for data relocation. | 
| 3460 | * the inode is in data relocation tree and its link count is 0 | 3792 | * the inode is in data relocation tree and its link count is 0 | 
| 3461 | */ | 3793 | */ | 
| 3462 | static struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, | 3794 | static noinline_for_stack | 
| 3463 | struct btrfs_block_group_cache *group) | 3795 | struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, | 
| 3796 | struct btrfs_block_group_cache *group) | ||
| 3464 | { | 3797 | { | 
| 3465 | struct inode *inode = NULL; | 3798 | struct inode *inode = NULL; | 
| 3466 | struct btrfs_trans_handle *trans; | 3799 | struct btrfs_trans_handle *trans; | 
| @@ -3474,8 +3807,9 @@ static struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, | |||
| 3474 | if (IS_ERR(root)) | 3807 | if (IS_ERR(root)) | 
| 3475 | return ERR_CAST(root); | 3808 | return ERR_CAST(root); | 
| 3476 | 3809 | ||
| 3477 | trans = btrfs_start_transaction(root, 1); | 3810 | trans = btrfs_start_transaction(root, 6); | 
| 3478 | BUG_ON(!trans); | 3811 | if (IS_ERR(trans)) | 
| 3812 | return ERR_CAST(trans); | ||
| 3479 | 3813 | ||
| 3480 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); | 3814 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); | 
| 3481 | if (err) | 3815 | if (err) | 
| @@ -3495,7 +3829,6 @@ static struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, | |||
| 3495 | out: | 3829 | out: | 
| 3496 | nr = trans->blocks_used; | 3830 | nr = trans->blocks_used; | 
| 3497 | btrfs_end_transaction(trans, root); | 3831 | btrfs_end_transaction(trans, root); | 
| 3498 | |||
| 3499 | btrfs_btree_balance_dirty(root, nr); | 3832 | btrfs_btree_balance_dirty(root, nr); | 
| 3500 | if (err) { | 3833 | if (err) { | 
| 3501 | if (inode) | 3834 | if (inode) | 
| @@ -3505,6 +3838,21 @@ out: | |||
| 3505 | return inode; | 3838 | return inode; | 
| 3506 | } | 3839 | } | 
| 3507 | 3840 | ||
| 3841 | static struct reloc_control *alloc_reloc_control(void) | ||
| 3842 | { | ||
| 3843 | struct reloc_control *rc; | ||
| 3844 | |||
| 3845 | rc = kzalloc(sizeof(*rc), GFP_NOFS); | ||
| 3846 | if (!rc) | ||
| 3847 | return NULL; | ||
| 3848 | |||
| 3849 | INIT_LIST_HEAD(&rc->reloc_roots); | ||
| 3850 | backref_cache_init(&rc->backref_cache); | ||
| 3851 | mapping_tree_init(&rc->reloc_root_tree); | ||
| 3852 | extent_io_tree_init(&rc->processed_blocks, NULL, GFP_NOFS); | ||
| 3853 | return rc; | ||
| 3854 | } | ||
| 3855 | |||
| 3508 | /* | 3856 | /* | 
| 3509 | * function to relocate all extents in a block group. | 3857 | * function to relocate all extents in a block group. | 
| 3510 | */ | 3858 | */ | 
| @@ -3513,24 +3861,26 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start) | |||
| 3513 | struct btrfs_fs_info *fs_info = extent_root->fs_info; | 3861 | struct btrfs_fs_info *fs_info = extent_root->fs_info; | 
| 3514 | struct reloc_control *rc; | 3862 | struct reloc_control *rc; | 
| 3515 | int ret; | 3863 | int ret; | 
| 3864 | int rw = 0; | ||
| 3516 | int err = 0; | 3865 | int err = 0; | 
| 3517 | 3866 | ||
| 3518 | rc = kzalloc(sizeof(*rc), GFP_NOFS); | 3867 | rc = alloc_reloc_control(); | 
| 3519 | if (!rc) | 3868 | if (!rc) | 
| 3520 | return -ENOMEM; | 3869 | return -ENOMEM; | 
| 3521 | 3870 | ||
| 3522 | mapping_tree_init(&rc->reloc_root_tree); | 3871 | rc->extent_root = extent_root; | 
| 3523 | extent_io_tree_init(&rc->processed_blocks, NULL, GFP_NOFS); | ||
| 3524 | INIT_LIST_HEAD(&rc->reloc_roots); | ||
| 3525 | 3872 | ||
| 3526 | rc->block_group = btrfs_lookup_block_group(fs_info, group_start); | 3873 | rc->block_group = btrfs_lookup_block_group(fs_info, group_start); | 
| 3527 | BUG_ON(!rc->block_group); | 3874 | BUG_ON(!rc->block_group); | 
| 3528 | 3875 | ||
| 3529 | btrfs_init_workers(&rc->workers, "relocate", | 3876 | if (!rc->block_group->ro) { | 
| 3530 | fs_info->thread_pool_size, NULL); | 3877 | ret = btrfs_set_block_group_ro(extent_root, rc->block_group); | 
| 3531 | 3878 | if (ret) { | |
| 3532 | rc->extent_root = extent_root; | 3879 | err = ret; | 
| 3533 | btrfs_prepare_block_group_relocation(extent_root, rc->block_group); | 3880 | goto out; | 
| 3881 | } | ||
| 3882 | rw = 1; | ||
| 3883 | } | ||
| 3534 | 3884 | ||
| 3535 | rc->data_inode = create_reloc_inode(fs_info, rc->block_group); | 3885 | rc->data_inode = create_reloc_inode(fs_info, rc->block_group); | 
| 3536 | if (IS_ERR(rc->data_inode)) { | 3886 | if (IS_ERR(rc->data_inode)) { | 
| @@ -3547,9 +3897,6 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start) | |||
| 3547 | btrfs_wait_ordered_extents(fs_info->tree_root, 0, 0); | 3897 | btrfs_wait_ordered_extents(fs_info->tree_root, 0, 0); | 
| 3548 | 3898 | ||
| 3549 | while (1) { | 3899 | while (1) { | 
| 3550 | rc->extents_found = 0; | ||
| 3551 | rc->extents_skipped = 0; | ||
| 3552 | |||
| 3553 | mutex_lock(&fs_info->cleaner_mutex); | 3900 | mutex_lock(&fs_info->cleaner_mutex); | 
| 3554 | 3901 | ||
| 3555 | btrfs_clean_old_snapshots(fs_info->tree_root); | 3902 | btrfs_clean_old_snapshots(fs_info->tree_root); | 
| @@ -3558,7 +3905,7 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start) | |||
| 3558 | mutex_unlock(&fs_info->cleaner_mutex); | 3905 | mutex_unlock(&fs_info->cleaner_mutex); | 
| 3559 | if (ret < 0) { | 3906 | if (ret < 0) { | 
| 3560 | err = ret; | 3907 | err = ret; | 
| 3561 | break; | 3908 | goto out; | 
| 3562 | } | 3909 | } | 
| 3563 | 3910 | ||
| 3564 | if (rc->extents_found == 0) | 3911 | if (rc->extents_found == 0) | 
| @@ -3572,18 +3919,6 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start) | |||
| 3572 | invalidate_mapping_pages(rc->data_inode->i_mapping, | 3919 | invalidate_mapping_pages(rc->data_inode->i_mapping, | 
| 3573 | 0, -1); | 3920 | 0, -1); | 
| 3574 | rc->stage = UPDATE_DATA_PTRS; | 3921 | rc->stage = UPDATE_DATA_PTRS; | 
| 3575 | } else if (rc->stage == UPDATE_DATA_PTRS && | ||
| 3576 | rc->extents_skipped >= rc->extents_found) { | ||
| 3577 | iput(rc->data_inode); | ||
| 3578 | rc->data_inode = create_reloc_inode(fs_info, | ||
| 3579 | rc->block_group); | ||
| 3580 | if (IS_ERR(rc->data_inode)) { | ||
| 3581 | err = PTR_ERR(rc->data_inode); | ||
| 3582 | rc->data_inode = NULL; | ||
| 3583 | break; | ||
| 3584 | } | ||
| 3585 | rc->stage = MOVE_DATA_EXTENTS; | ||
| 3586 | rc->found_file_extent = 0; | ||
| 3587 | } | 3922 | } | 
| 3588 | } | 3923 | } | 
| 3589 | 3924 | ||
| @@ -3596,8 +3931,9 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start) | |||
| 3596 | WARN_ON(rc->block_group->reserved > 0); | 3931 | WARN_ON(rc->block_group->reserved > 0); | 
| 3597 | WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0); | 3932 | WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0); | 
| 3598 | out: | 3933 | out: | 
| 3934 | if (err && rw) | ||
| 3935 | btrfs_set_block_group_rw(extent_root, rc->block_group); | ||
| 3599 | iput(rc->data_inode); | 3936 | iput(rc->data_inode); | 
| 3600 | btrfs_stop_workers(&rc->workers); | ||
| 3601 | btrfs_put_block_group(rc->block_group); | 3937 | btrfs_put_block_group(rc->block_group); | 
| 3602 | kfree(rc); | 3938 | kfree(rc); | 
| 3603 | return err; | 3939 | return err; | 
| @@ -3608,7 +3944,7 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) | |||
| 3608 | struct btrfs_trans_handle *trans; | 3944 | struct btrfs_trans_handle *trans; | 
| 3609 | int ret; | 3945 | int ret; | 
| 3610 | 3946 | ||
| 3611 | trans = btrfs_start_transaction(root->fs_info->tree_root, 1); | 3947 | trans = btrfs_start_transaction(root->fs_info->tree_root, 0); | 
| 3612 | 3948 | ||
| 3613 | memset(&root->root_item.drop_progress, 0, | 3949 | memset(&root->root_item.drop_progress, 0, | 
| 3614 | sizeof(root->root_item.drop_progress)); | 3950 | sizeof(root->root_item.drop_progress)); | 
| @@ -3701,20 +4037,20 @@ int btrfs_recover_relocation(struct btrfs_root *root) | |||
| 3701 | if (list_empty(&reloc_roots)) | 4037 | if (list_empty(&reloc_roots)) | 
| 3702 | goto out; | 4038 | goto out; | 
| 3703 | 4039 | ||
| 3704 | rc = kzalloc(sizeof(*rc), GFP_NOFS); | 4040 | rc = alloc_reloc_control(); | 
| 3705 | if (!rc) { | 4041 | if (!rc) { | 
| 3706 | err = -ENOMEM; | 4042 | err = -ENOMEM; | 
| 3707 | goto out; | 4043 | goto out; | 
| 3708 | } | 4044 | } | 
| 3709 | 4045 | ||
| 3710 | mapping_tree_init(&rc->reloc_root_tree); | ||
| 3711 | INIT_LIST_HEAD(&rc->reloc_roots); | ||
| 3712 | btrfs_init_workers(&rc->workers, "relocate", | ||
| 3713 | root->fs_info->thread_pool_size, NULL); | ||
| 3714 | rc->extent_root = root->fs_info->extent_root; | 4046 | rc->extent_root = root->fs_info->extent_root; | 
| 3715 | 4047 | ||
| 3716 | set_reloc_control(rc); | 4048 | set_reloc_control(rc); | 
| 3717 | 4049 | ||
| 4050 | trans = btrfs_join_transaction(rc->extent_root, 1); | ||
| 4051 | |||
| 4052 | rc->merge_reloc_tree = 1; | ||
| 4053 | |||
| 3718 | while (!list_empty(&reloc_roots)) { | 4054 | while (!list_empty(&reloc_roots)) { | 
| 3719 | reloc_root = list_entry(reloc_roots.next, | 4055 | reloc_root = list_entry(reloc_roots.next, | 
| 3720 | struct btrfs_root, root_list); | 4056 | struct btrfs_root, root_list); | 
| @@ -3734,20 +4070,16 @@ int btrfs_recover_relocation(struct btrfs_root *root) | |||
| 3734 | fs_root->reloc_root = reloc_root; | 4070 | fs_root->reloc_root = reloc_root; | 
| 3735 | } | 4071 | } | 
| 3736 | 4072 | ||
| 3737 | trans = btrfs_start_transaction(rc->extent_root, 1); | ||
| 3738 | btrfs_commit_transaction(trans, rc->extent_root); | 4073 | btrfs_commit_transaction(trans, rc->extent_root); | 
| 3739 | 4074 | ||
| 3740 | merge_reloc_roots(rc); | 4075 | merge_reloc_roots(rc); | 
| 3741 | 4076 | ||
| 3742 | unset_reloc_control(rc); | 4077 | unset_reloc_control(rc); | 
| 3743 | 4078 | ||
| 3744 | trans = btrfs_start_transaction(rc->extent_root, 1); | 4079 | trans = btrfs_join_transaction(rc->extent_root, 1); | 
| 3745 | btrfs_commit_transaction(trans, rc->extent_root); | 4080 | btrfs_commit_transaction(trans, rc->extent_root); | 
| 3746 | out: | 4081 | out: | 
| 3747 | if (rc) { | 4082 | kfree(rc); | 
| 3748 | btrfs_stop_workers(&rc->workers); | ||
| 3749 | kfree(rc); | ||
| 3750 | } | ||
| 3751 | while (!list_empty(&reloc_roots)) { | 4083 | while (!list_empty(&reloc_roots)) { | 
| 3752 | reloc_root = list_entry(reloc_roots.next, | 4084 | reloc_root = list_entry(reloc_roots.next, | 
| 3753 | struct btrfs_root, root_list); | 4085 | struct btrfs_root, root_list); | 
| @@ -3813,3 +4145,130 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) | |||
| 3813 | btrfs_put_ordered_extent(ordered); | 4145 | btrfs_put_ordered_extent(ordered); | 
| 3814 | return 0; | 4146 | return 0; | 
| 3815 | } | 4147 | } | 
| 4148 | |||
| 4149 | void btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, | ||
| 4150 | struct btrfs_root *root, struct extent_buffer *buf, | ||
| 4151 | struct extent_buffer *cow) | ||
| 4152 | { | ||
| 4153 | struct reloc_control *rc; | ||
| 4154 | struct backref_node *node; | ||
| 4155 | int first_cow = 0; | ||
| 4156 | int level; | ||
| 4157 | int ret; | ||
| 4158 | |||
| 4159 | rc = root->fs_info->reloc_ctl; | ||
| 4160 | if (!rc) | ||
| 4161 | return; | ||
| 4162 | |||
| 4163 | BUG_ON(rc->stage == UPDATE_DATA_PTRS && | ||
| 4164 | root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID); | ||
| 4165 | |||
| 4166 | level = btrfs_header_level(buf); | ||
| 4167 | if (btrfs_header_generation(buf) <= | ||
| 4168 | btrfs_root_last_snapshot(&root->root_item)) | ||
| 4169 | first_cow = 1; | ||
| 4170 | |||
| 4171 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID && | ||
| 4172 | rc->create_reloc_tree) { | ||
| 4173 | WARN_ON(!first_cow && level == 0); | ||
| 4174 | |||
| 4175 | node = rc->backref_cache.path[level]; | ||
| 4176 | BUG_ON(node->bytenr != buf->start && | ||
| 4177 | node->new_bytenr != buf->start); | ||
| 4178 | |||
| 4179 | drop_node_buffer(node); | ||
| 4180 | extent_buffer_get(cow); | ||
| 4181 | node->eb = cow; | ||
| 4182 | node->new_bytenr = cow->start; | ||
| 4183 | |||
| 4184 | if (!node->pending) { | ||
| 4185 | list_move_tail(&node->list, | ||
| 4186 | &rc->backref_cache.pending[level]); | ||
| 4187 | node->pending = 1; | ||
| 4188 | } | ||
| 4189 | |||
| 4190 | if (first_cow) | ||
| 4191 | __mark_block_processed(rc, node); | ||
| 4192 | |||
| 4193 | if (first_cow && level > 0) | ||
| 4194 | rc->nodes_relocated += buf->len; | ||
| 4195 | } | ||
| 4196 | |||
| 4197 | if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS) { | ||
| 4198 | ret = replace_file_extents(trans, rc, root, cow); | ||
| 4199 | BUG_ON(ret); | ||
| 4200 | } | ||
| 4201 | } | ||
| 4202 | |||
| 4203 | /* | ||
| 4204 | * called before creating snapshot. it calculates metadata reservation | ||
| 4205 | * requried for relocating tree blocks in the snapshot | ||
| 4206 | */ | ||
| 4207 | void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle *trans, | ||
| 4208 | struct btrfs_pending_snapshot *pending, | ||
| 4209 | u64 *bytes_to_reserve) | ||
| 4210 | { | ||
| 4211 | struct btrfs_root *root; | ||
| 4212 | struct reloc_control *rc; | ||
| 4213 | |||
| 4214 | root = pending->root; | ||
| 4215 | if (!root->reloc_root) | ||
| 4216 | return; | ||
| 4217 | |||
| 4218 | rc = root->fs_info->reloc_ctl; | ||
| 4219 | if (!rc->merge_reloc_tree) | ||
| 4220 | return; | ||
| 4221 | |||
| 4222 | root = root->reloc_root; | ||
| 4223 | BUG_ON(btrfs_root_refs(&root->root_item) == 0); | ||
| 4224 | /* | ||
| 4225 | * relocation is in the stage of merging trees. the space | ||
| 4226 | * used by merging a reloc tree is twice the size of | ||
| 4227 | * relocated tree nodes in the worst case. half for cowing | ||
| 4228 | * the reloc tree, half for cowing the fs tree. the space | ||
| 4229 | * used by cowing the reloc tree will be freed after the | ||
| 4230 | * tree is dropped. if we create snapshot, cowing the fs | ||
| 4231 | * tree may use more space than it frees. so we need | ||
| 4232 | * reserve extra space. | ||
| 4233 | */ | ||
| 4234 | *bytes_to_reserve += rc->nodes_relocated; | ||
| 4235 | } | ||
| 4236 | |||
| 4237 | /* | ||
| 4238 | * called after snapshot is created. migrate block reservation | ||
| 4239 | * and create reloc root for the newly created snapshot | ||
| 4240 | */ | ||
| 4241 | void btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, | ||
| 4242 | struct btrfs_pending_snapshot *pending) | ||
| 4243 | { | ||
| 4244 | struct btrfs_root *root = pending->root; | ||
| 4245 | struct btrfs_root *reloc_root; | ||
| 4246 | struct btrfs_root *new_root; | ||
| 4247 | struct reloc_control *rc; | ||
| 4248 | int ret; | ||
| 4249 | |||
| 4250 | if (!root->reloc_root) | ||
| 4251 | return; | ||
| 4252 | |||
| 4253 | rc = root->fs_info->reloc_ctl; | ||
| 4254 | rc->merging_rsv_size += rc->nodes_relocated; | ||
| 4255 | |||
| 4256 | if (rc->merge_reloc_tree) { | ||
| 4257 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, | ||
| 4258 | rc->block_rsv, | ||
| 4259 | rc->nodes_relocated); | ||
| 4260 | BUG_ON(ret); | ||
| 4261 | } | ||
| 4262 | |||
| 4263 | new_root = pending->snap; | ||
| 4264 | reloc_root = create_reloc_root(trans, root->reloc_root, | ||
| 4265 | new_root->root_key.objectid); | ||
| 4266 | |||
| 4267 | __add_reloc_root(reloc_root); | ||
| 4268 | new_root->reloc_root = reloc_root; | ||
| 4269 | |||
| 4270 | if (rc->create_reloc_tree) { | ||
| 4271 | ret = clone_backref_node(trans, rc, root, reloc_root); | ||
| 4272 | BUG_ON(ret); | ||
| 4273 | } | ||
| 4274 | } | ||
