rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2009 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/writeback.h> |
| 22 | #include <linux/blkdev.h> |
| 23 | #include <linux/rbtree.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include "ctree.h" |
| 26 | #include "disk-io.h" |
| 27 | #include "transaction.h" |
| 28 | #include "volumes.h" |
| 29 | #include "locking.h" |
| 30 | #include "btrfs_inode.h" |
| 31 | #include "async-thread.h" |
| 32 | #include "free-space-cache.h" |
| 33 | #include "inode-map.h" |
| 34 | #include "qgroup.h" |
| 35 | #include "print-tree.h" |
| 36 | |
| 37 | /* |
| 38 | * backref_node, mapping_node and tree_block start with this |
| 39 | */ |
| 40 | struct tree_entry { |
| 41 | struct rb_node rb_node; |
| 42 | u64 bytenr; |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * present a tree block in the backref cache |
| 47 | */ |
| 48 | struct backref_node { |
| 49 | struct rb_node rb_node; |
| 50 | u64 bytenr; |
| 51 | |
| 52 | u64 new_bytenr; |
| 53 | /* objectid of tree block owner, can be not uptodate */ |
| 54 | u64 owner; |
| 55 | /* link to pending, changed or detached list */ |
| 56 | struct list_head list; |
| 57 | /* list of upper level blocks reference this block */ |
| 58 | struct list_head upper; |
| 59 | /* list of child blocks in the cache */ |
| 60 | struct list_head lower; |
| 61 | /* NULL if this node is not tree root */ |
| 62 | struct btrfs_root *root; |
| 63 | /* extent buffer got by COW the block */ |
| 64 | struct extent_buffer *eb; |
| 65 | /* level of tree block */ |
| 66 | unsigned int level:8; |
| 67 | /* is the block in non-reference counted tree */ |
| 68 | unsigned int cowonly:1; |
| 69 | /* 1 if no child node in the cache */ |
| 70 | unsigned int lowest:1; |
| 71 | /* is the extent buffer locked */ |
| 72 | unsigned int locked:1; |
| 73 | /* has the block been processed */ |
| 74 | unsigned int processed:1; |
| 75 | /* have backrefs of this block been checked */ |
| 76 | unsigned int checked:1; |
| 77 | /* |
| 78 | * 1 if corresponding block has been cowed but some upper |
| 79 | * level block pointers may not point to the new location |
| 80 | */ |
| 81 | unsigned int pending:1; |
| 82 | /* |
| 83 | * 1 if the backref node isn't connected to any other |
| 84 | * backref node. |
| 85 | */ |
| 86 | unsigned int detached:1; |
| 87 | }; |
| 88 | |
| 89 | /* |
| 90 | * present a block pointer in the backref cache |
| 91 | */ |
| 92 | struct backref_edge { |
| 93 | struct list_head list[2]; |
| 94 | struct backref_node *node[2]; |
| 95 | }; |
| 96 | |
| 97 | #define LOWER 0 |
| 98 | #define UPPER 1 |
| 99 | #define RELOCATION_RESERVED_NODES 256 |
| 100 | |
| 101 | struct backref_cache { |
| 102 | /* red black tree of all backref nodes in the cache */ |
| 103 | struct rb_root rb_root; |
| 104 | /* for passing backref nodes to btrfs_reloc_cow_block */ |
| 105 | struct backref_node *path[BTRFS_MAX_LEVEL]; |
| 106 | /* |
| 107 | * list of blocks that have been cowed but some block |
| 108 | * pointers in upper level blocks may not reflect the |
| 109 | * new location |
| 110 | */ |
| 111 | struct list_head pending[BTRFS_MAX_LEVEL]; |
| 112 | /* list of backref nodes with no child node */ |
| 113 | struct list_head leaves; |
| 114 | /* list of blocks that have been cowed in current transaction */ |
| 115 | struct list_head changed; |
| 116 | /* list of detached backref node. */ |
| 117 | struct list_head detached; |
| 118 | |
| 119 | u64 last_trans; |
| 120 | |
| 121 | int nr_nodes; |
| 122 | int nr_edges; |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * map address of tree root to tree |
| 127 | */ |
| 128 | struct mapping_node { |
| 129 | struct rb_node rb_node; |
| 130 | u64 bytenr; |
| 131 | void *data; |
| 132 | }; |
| 133 | |
| 134 | struct mapping_tree { |
| 135 | struct rb_root rb_root; |
| 136 | spinlock_t lock; |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * present a tree block to process |
| 141 | */ |
| 142 | struct tree_block { |
| 143 | struct rb_node rb_node; |
| 144 | u64 bytenr; |
| 145 | struct btrfs_key key; |
| 146 | unsigned int level:8; |
| 147 | unsigned int key_ready:1; |
| 148 | }; |
| 149 | |
| 150 | #define MAX_EXTENTS 128 |
| 151 | |
| 152 | struct file_extent_cluster { |
| 153 | u64 start; |
| 154 | u64 end; |
| 155 | u64 boundary[MAX_EXTENTS]; |
| 156 | unsigned int nr; |
| 157 | }; |
| 158 | |
| 159 | struct reloc_control { |
| 160 | /* block group to relocate */ |
| 161 | struct btrfs_block_group_cache *block_group; |
| 162 | /* extent tree */ |
| 163 | struct btrfs_root *extent_root; |
| 164 | /* inode for moving data */ |
| 165 | struct inode *data_inode; |
| 166 | |
| 167 | struct btrfs_block_rsv *block_rsv; |
| 168 | |
| 169 | struct backref_cache backref_cache; |
| 170 | |
| 171 | struct file_extent_cluster cluster; |
| 172 | /* tree blocks have been processed */ |
| 173 | struct extent_io_tree processed_blocks; |
| 174 | /* map start of tree root to corresponding reloc tree */ |
| 175 | struct mapping_tree reloc_root_tree; |
| 176 | /* list of reloc trees */ |
| 177 | struct list_head reloc_roots; |
| 178 | /* size of metadata reservation for merging reloc trees */ |
| 179 | u64 merging_rsv_size; |
| 180 | /* size of relocated tree nodes */ |
| 181 | u64 nodes_relocated; |
| 182 | /* reserved size for block group relocation*/ |
| 183 | u64 reserved_bytes; |
| 184 | |
| 185 | u64 search_start; |
| 186 | u64 extents_found; |
| 187 | |
| 188 | unsigned int stage:8; |
| 189 | unsigned int create_reloc_tree:1; |
| 190 | unsigned int merge_reloc_tree:1; |
| 191 | unsigned int found_file_extent:1; |
| 192 | }; |
| 193 | |
| 194 | /* stages of data relocation */ |
| 195 | #define MOVE_DATA_EXTENTS 0 |
| 196 | #define UPDATE_DATA_PTRS 1 |
| 197 | |
| 198 | static void remove_backref_node(struct backref_cache *cache, |
| 199 | struct backref_node *node); |
| 200 | static void __mark_block_processed(struct reloc_control *rc, |
| 201 | struct backref_node *node); |
| 202 | |
| 203 | static void mapping_tree_init(struct mapping_tree *tree) |
| 204 | { |
| 205 | tree->rb_root = RB_ROOT; |
| 206 | spin_lock_init(&tree->lock); |
| 207 | } |
| 208 | |
| 209 | static void backref_cache_init(struct backref_cache *cache) |
| 210 | { |
| 211 | int i; |
| 212 | cache->rb_root = RB_ROOT; |
| 213 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) |
| 214 | INIT_LIST_HEAD(&cache->pending[i]); |
| 215 | INIT_LIST_HEAD(&cache->changed); |
| 216 | INIT_LIST_HEAD(&cache->detached); |
| 217 | INIT_LIST_HEAD(&cache->leaves); |
| 218 | } |
| 219 | |
| 220 | static void backref_cache_cleanup(struct backref_cache *cache) |
| 221 | { |
| 222 | struct backref_node *node; |
| 223 | int i; |
| 224 | |
| 225 | while (!list_empty(&cache->detached)) { |
| 226 | node = list_entry(cache->detached.next, |
| 227 | struct backref_node, list); |
| 228 | remove_backref_node(cache, node); |
| 229 | } |
| 230 | |
| 231 | while (!list_empty(&cache->leaves)) { |
| 232 | node = list_entry(cache->leaves.next, |
| 233 | struct backref_node, lower); |
| 234 | remove_backref_node(cache, node); |
| 235 | } |
| 236 | |
| 237 | cache->last_trans = 0; |
| 238 | |
| 239 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) |
| 240 | ASSERT(list_empty(&cache->pending[i])); |
| 241 | ASSERT(list_empty(&cache->changed)); |
| 242 | ASSERT(list_empty(&cache->detached)); |
| 243 | ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); |
| 244 | ASSERT(!cache->nr_nodes); |
| 245 | ASSERT(!cache->nr_edges); |
| 246 | } |
| 247 | |
| 248 | static struct backref_node *alloc_backref_node(struct backref_cache *cache) |
| 249 | { |
| 250 | struct backref_node *node; |
| 251 | |
| 252 | node = kzalloc(sizeof(*node), GFP_NOFS); |
| 253 | if (node) { |
| 254 | INIT_LIST_HEAD(&node->list); |
| 255 | INIT_LIST_HEAD(&node->upper); |
| 256 | INIT_LIST_HEAD(&node->lower); |
| 257 | RB_CLEAR_NODE(&node->rb_node); |
| 258 | cache->nr_nodes++; |
| 259 | } |
| 260 | return node; |
| 261 | } |
| 262 | |
| 263 | static void free_backref_node(struct backref_cache *cache, |
| 264 | struct backref_node *node) |
| 265 | { |
| 266 | if (node) { |
| 267 | cache->nr_nodes--; |
| 268 | kfree(node); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static struct backref_edge *alloc_backref_edge(struct backref_cache *cache) |
| 273 | { |
| 274 | struct backref_edge *edge; |
| 275 | |
| 276 | edge = kzalloc(sizeof(*edge), GFP_NOFS); |
| 277 | if (edge) |
| 278 | cache->nr_edges++; |
| 279 | return edge; |
| 280 | } |
| 281 | |
| 282 | static void free_backref_edge(struct backref_cache *cache, |
| 283 | struct backref_edge *edge) |
| 284 | { |
| 285 | if (edge) { |
| 286 | cache->nr_edges--; |
| 287 | kfree(edge); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr, |
| 292 | struct rb_node *node) |
| 293 | { |
| 294 | struct rb_node **p = &root->rb_node; |
| 295 | struct rb_node *parent = NULL; |
| 296 | struct tree_entry *entry; |
| 297 | |
| 298 | while (*p) { |
| 299 | parent = *p; |
| 300 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 301 | |
| 302 | if (bytenr < entry->bytenr) |
| 303 | p = &(*p)->rb_left; |
| 304 | else if (bytenr > entry->bytenr) |
| 305 | p = &(*p)->rb_right; |
| 306 | else |
| 307 | return parent; |
| 308 | } |
| 309 | |
| 310 | rb_link_node(node, parent, p); |
| 311 | rb_insert_color(node, root); |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | static struct rb_node *tree_search(struct rb_root *root, u64 bytenr) |
| 316 | { |
| 317 | struct rb_node *n = root->rb_node; |
| 318 | struct tree_entry *entry; |
| 319 | |
| 320 | while (n) { |
| 321 | entry = rb_entry(n, struct tree_entry, rb_node); |
| 322 | |
| 323 | if (bytenr < entry->bytenr) |
| 324 | n = n->rb_left; |
| 325 | else if (bytenr > entry->bytenr) |
| 326 | n = n->rb_right; |
| 327 | else |
| 328 | return n; |
| 329 | } |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr) |
| 334 | { |
| 335 | |
| 336 | struct btrfs_fs_info *fs_info = NULL; |
| 337 | struct backref_node *bnode = rb_entry(rb_node, struct backref_node, |
| 338 | rb_node); |
| 339 | if (bnode->root) |
| 340 | fs_info = bnode->root->fs_info; |
| 341 | btrfs_panic(fs_info, errno, |
| 342 | "Inconsistency in backref cache found at offset %llu", |
| 343 | bytenr); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * walk up backref nodes until reach node presents tree root |
| 348 | */ |
| 349 | static struct backref_node *walk_up_backref(struct backref_node *node, |
| 350 | struct backref_edge *edges[], |
| 351 | int *index) |
| 352 | { |
| 353 | struct backref_edge *edge; |
| 354 | int idx = *index; |
| 355 | |
| 356 | while (!list_empty(&node->upper)) { |
| 357 | edge = list_entry(node->upper.next, |
| 358 | struct backref_edge, list[LOWER]); |
| 359 | edges[idx++] = edge; |
| 360 | node = edge->node[UPPER]; |
| 361 | } |
| 362 | BUG_ON(node->detached); |
| 363 | *index = idx; |
| 364 | return node; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * walk down backref nodes to find start of next reference path |
| 369 | */ |
| 370 | static struct backref_node *walk_down_backref(struct backref_edge *edges[], |
| 371 | int *index) |
| 372 | { |
| 373 | struct backref_edge *edge; |
| 374 | struct backref_node *lower; |
| 375 | int idx = *index; |
| 376 | |
| 377 | while (idx > 0) { |
| 378 | edge = edges[idx - 1]; |
| 379 | lower = edge->node[LOWER]; |
| 380 | if (list_is_last(&edge->list[LOWER], &lower->upper)) { |
| 381 | idx--; |
| 382 | continue; |
| 383 | } |
| 384 | edge = list_entry(edge->list[LOWER].next, |
| 385 | struct backref_edge, list[LOWER]); |
| 386 | edges[idx - 1] = edge; |
| 387 | *index = idx; |
| 388 | return edge->node[UPPER]; |
| 389 | } |
| 390 | *index = 0; |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | static void unlock_node_buffer(struct backref_node *node) |
| 395 | { |
| 396 | if (node->locked) { |
| 397 | btrfs_tree_unlock(node->eb); |
| 398 | node->locked = 0; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | static void drop_node_buffer(struct backref_node *node) |
| 403 | { |
| 404 | if (node->eb) { |
| 405 | unlock_node_buffer(node); |
| 406 | free_extent_buffer(node->eb); |
| 407 | node->eb = NULL; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | static void drop_backref_node(struct backref_cache *tree, |
| 412 | struct backref_node *node) |
| 413 | { |
| 414 | BUG_ON(!list_empty(&node->upper)); |
| 415 | |
| 416 | drop_node_buffer(node); |
| 417 | list_del(&node->list); |
| 418 | list_del(&node->lower); |
| 419 | if (!RB_EMPTY_NODE(&node->rb_node)) |
| 420 | rb_erase(&node->rb_node, &tree->rb_root); |
| 421 | free_backref_node(tree, node); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * remove a backref node from the backref cache |
| 426 | */ |
| 427 | static void remove_backref_node(struct backref_cache *cache, |
| 428 | struct backref_node *node) |
| 429 | { |
| 430 | struct backref_node *upper; |
| 431 | struct backref_edge *edge; |
| 432 | |
| 433 | if (!node) |
| 434 | return; |
| 435 | |
| 436 | BUG_ON(!node->lowest && !node->detached); |
| 437 | while (!list_empty(&node->upper)) { |
| 438 | edge = list_entry(node->upper.next, struct backref_edge, |
| 439 | list[LOWER]); |
| 440 | upper = edge->node[UPPER]; |
| 441 | list_del(&edge->list[LOWER]); |
| 442 | list_del(&edge->list[UPPER]); |
| 443 | free_backref_edge(cache, edge); |
| 444 | |
| 445 | if (RB_EMPTY_NODE(&upper->rb_node)) { |
| 446 | BUG_ON(!list_empty(&node->upper)); |
| 447 | drop_backref_node(cache, node); |
| 448 | node = upper; |
| 449 | node->lowest = 1; |
| 450 | continue; |
| 451 | } |
| 452 | /* |
| 453 | * add the node to leaf node list if no other |
| 454 | * child block cached. |
| 455 | */ |
| 456 | if (list_empty(&upper->lower)) { |
| 457 | list_add_tail(&upper->lower, &cache->leaves); |
| 458 | upper->lowest = 1; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | drop_backref_node(cache, node); |
| 463 | } |
| 464 | |
| 465 | static void update_backref_node(struct backref_cache *cache, |
| 466 | struct backref_node *node, u64 bytenr) |
| 467 | { |
| 468 | struct rb_node *rb_node; |
| 469 | rb_erase(&node->rb_node, &cache->rb_root); |
| 470 | node->bytenr = bytenr; |
| 471 | rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node); |
| 472 | if (rb_node) |
| 473 | backref_tree_panic(rb_node, -EEXIST, bytenr); |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * update backref cache after a transaction commit |
| 478 | */ |
| 479 | static int update_backref_cache(struct btrfs_trans_handle *trans, |
| 480 | struct backref_cache *cache) |
| 481 | { |
| 482 | struct backref_node *node; |
| 483 | int level = 0; |
| 484 | |
| 485 | if (cache->last_trans == 0) { |
| 486 | cache->last_trans = trans->transid; |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | if (cache->last_trans == trans->transid) |
| 491 | return 0; |
| 492 | |
| 493 | /* |
| 494 | * detached nodes are used to avoid unnecessary backref |
| 495 | * lookup. transaction commit changes the extent tree. |
| 496 | * so the detached nodes are no longer useful. |
| 497 | */ |
| 498 | while (!list_empty(&cache->detached)) { |
| 499 | node = list_entry(cache->detached.next, |
| 500 | struct backref_node, list); |
| 501 | remove_backref_node(cache, node); |
| 502 | } |
| 503 | |
| 504 | while (!list_empty(&cache->changed)) { |
| 505 | node = list_entry(cache->changed.next, |
| 506 | struct backref_node, list); |
| 507 | list_del_init(&node->list); |
| 508 | BUG_ON(node->pending); |
| 509 | update_backref_node(cache, node, node->new_bytenr); |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * some nodes can be left in the pending list if there were |
| 514 | * errors during processing the pending nodes. |
| 515 | */ |
| 516 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
| 517 | list_for_each_entry(node, &cache->pending[level], list) { |
| 518 | BUG_ON(!node->pending); |
| 519 | if (node->bytenr == node->new_bytenr) |
| 520 | continue; |
| 521 | update_backref_node(cache, node, node->new_bytenr); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | cache->last_trans = 0; |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | static int should_ignore_root(struct btrfs_root *root) |
| 531 | { |
| 532 | struct btrfs_root *reloc_root; |
| 533 | |
| 534 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 535 | return 0; |
| 536 | |
| 537 | reloc_root = root->reloc_root; |
| 538 | if (!reloc_root) |
| 539 | return 0; |
| 540 | |
| 541 | if (btrfs_header_generation(reloc_root->commit_root) == |
| 542 | root->fs_info->running_transaction->transid) |
| 543 | return 0; |
| 544 | /* |
| 545 | * if there is reloc tree and it was created in previous |
| 546 | * transaction backref lookup can find the reloc tree, |
| 547 | * so backref node for the fs tree root is useless for |
| 548 | * relocation. |
| 549 | */ |
| 550 | return 1; |
| 551 | } |
| 552 | /* |
| 553 | * find reloc tree by address of tree root |
| 554 | */ |
| 555 | static struct btrfs_root *find_reloc_root(struct reloc_control *rc, |
| 556 | u64 bytenr) |
| 557 | { |
| 558 | struct rb_node *rb_node; |
| 559 | struct mapping_node *node; |
| 560 | struct btrfs_root *root = NULL; |
| 561 | |
| 562 | spin_lock(&rc->reloc_root_tree.lock); |
| 563 | rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr); |
| 564 | if (rb_node) { |
| 565 | node = rb_entry(rb_node, struct mapping_node, rb_node); |
| 566 | root = (struct btrfs_root *)node->data; |
| 567 | } |
| 568 | spin_unlock(&rc->reloc_root_tree.lock); |
| 569 | return root; |
| 570 | } |
| 571 | |
| 572 | static int is_cowonly_root(u64 root_objectid) |
| 573 | { |
| 574 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 575 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 576 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 577 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
| 578 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 579 | root_objectid == BTRFS_CSUM_TREE_OBJECTID || |
| 580 | root_objectid == BTRFS_UUID_TREE_OBJECTID || |
| 581 | root_objectid == BTRFS_QUOTA_TREE_OBJECTID || |
| 582 | root_objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) |
| 583 | return 1; |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info, |
| 588 | u64 root_objectid) |
| 589 | { |
| 590 | struct btrfs_key key; |
| 591 | |
| 592 | key.objectid = root_objectid; |
| 593 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 594 | if (is_cowonly_root(root_objectid)) |
| 595 | key.offset = 0; |
| 596 | else |
| 597 | key.offset = (u64)-1; |
| 598 | |
| 599 | return btrfs_get_fs_root(fs_info, &key, false); |
| 600 | } |
| 601 | |
| 602 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 603 | static noinline_for_stack |
| 604 | struct btrfs_root *find_tree_root(struct reloc_control *rc, |
| 605 | struct extent_buffer *leaf, |
| 606 | struct btrfs_extent_ref_v0 *ref0) |
| 607 | { |
| 608 | struct btrfs_root *root; |
| 609 | u64 root_objectid = btrfs_ref_root_v0(leaf, ref0); |
| 610 | u64 generation = btrfs_ref_generation_v0(leaf, ref0); |
| 611 | |
| 612 | BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID); |
| 613 | |
| 614 | root = read_fs_root(rc->extent_root->fs_info, root_objectid); |
| 615 | BUG_ON(IS_ERR(root)); |
| 616 | |
| 617 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
| 618 | generation != btrfs_root_generation(&root->root_item)) |
| 619 | return NULL; |
| 620 | |
| 621 | return root; |
| 622 | } |
| 623 | #endif |
| 624 | |
| 625 | static noinline_for_stack |
| 626 | int find_inline_backref(struct extent_buffer *leaf, int slot, |
| 627 | unsigned long *ptr, unsigned long *end) |
| 628 | { |
| 629 | struct btrfs_key key; |
| 630 | struct btrfs_extent_item *ei; |
| 631 | struct btrfs_tree_block_info *bi; |
| 632 | u32 item_size; |
| 633 | |
| 634 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 635 | |
| 636 | item_size = btrfs_item_size_nr(leaf, slot); |
| 637 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 638 | if (item_size < sizeof(*ei)) { |
| 639 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 640 | return 1; |
| 641 | } |
| 642 | #endif |
| 643 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); |
| 644 | WARN_ON(!(btrfs_extent_flags(leaf, ei) & |
| 645 | BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 646 | |
| 647 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 648 | item_size <= sizeof(*ei) + sizeof(*bi)) { |
| 649 | WARN_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 650 | return 1; |
| 651 | } |
| 652 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 653 | item_size <= sizeof(*ei)) { |
| 654 | WARN_ON(item_size < sizeof(*ei)); |
| 655 | return 1; |
| 656 | } |
| 657 | |
| 658 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
| 659 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 660 | *ptr = (unsigned long)(bi + 1); |
| 661 | } else { |
| 662 | *ptr = (unsigned long)(ei + 1); |
| 663 | } |
| 664 | *end = (unsigned long)ei + item_size; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * build backref tree for a given tree block. root of the backref tree |
| 670 | * corresponds the tree block, leaves of the backref tree correspond |
| 671 | * roots of b-trees that reference the tree block. |
| 672 | * |
| 673 | * the basic idea of this function is check backrefs of a given block |
| 674 | * to find upper level blocks that reference the block, and then check |
| 675 | * backrefs of these upper level blocks recursively. the recursion stop |
| 676 | * when tree root is reached or backrefs for the block is cached. |
| 677 | * |
| 678 | * NOTE: if we find backrefs for a block are cached, we know backrefs |
| 679 | * for all upper level blocks that directly/indirectly reference the |
| 680 | * block are also cached. |
| 681 | */ |
| 682 | static noinline_for_stack |
| 683 | struct backref_node *build_backref_tree(struct reloc_control *rc, |
| 684 | struct btrfs_key *node_key, |
| 685 | int level, u64 bytenr) |
| 686 | { |
| 687 | struct backref_cache *cache = &rc->backref_cache; |
| 688 | struct btrfs_path *path1; |
| 689 | struct btrfs_path *path2; |
| 690 | struct extent_buffer *eb; |
| 691 | struct btrfs_root *root; |
| 692 | struct backref_node *cur; |
| 693 | struct backref_node *upper; |
| 694 | struct backref_node *lower; |
| 695 | struct backref_node *node = NULL; |
| 696 | struct backref_node *exist = NULL; |
| 697 | struct backref_edge *edge; |
| 698 | struct rb_node *rb_node; |
| 699 | struct btrfs_key key; |
| 700 | unsigned long end; |
| 701 | unsigned long ptr; |
| 702 | LIST_HEAD(list); |
| 703 | LIST_HEAD(useless); |
| 704 | int cowonly; |
| 705 | int ret; |
| 706 | int err = 0; |
| 707 | bool need_check = true; |
| 708 | |
| 709 | path1 = btrfs_alloc_path(); |
| 710 | path2 = btrfs_alloc_path(); |
| 711 | if (!path1 || !path2) { |
| 712 | err = -ENOMEM; |
| 713 | goto out; |
| 714 | } |
| 715 | path1->reada = READA_FORWARD; |
| 716 | path2->reada = READA_FORWARD; |
| 717 | |
| 718 | node = alloc_backref_node(cache); |
| 719 | if (!node) { |
| 720 | err = -ENOMEM; |
| 721 | goto out; |
| 722 | } |
| 723 | |
| 724 | node->bytenr = bytenr; |
| 725 | node->level = level; |
| 726 | node->lowest = 1; |
| 727 | cur = node; |
| 728 | again: |
| 729 | end = 0; |
| 730 | ptr = 0; |
| 731 | key.objectid = cur->bytenr; |
| 732 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 733 | key.offset = (u64)-1; |
| 734 | |
| 735 | path1->search_commit_root = 1; |
| 736 | path1->skip_locking = 1; |
| 737 | ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1, |
| 738 | 0, 0); |
| 739 | if (ret < 0) { |
| 740 | err = ret; |
| 741 | goto out; |
| 742 | } |
| 743 | ASSERT(ret); |
| 744 | ASSERT(path1->slots[0]); |
| 745 | |
| 746 | path1->slots[0]--; |
| 747 | |
| 748 | WARN_ON(cur->checked); |
| 749 | if (!list_empty(&cur->upper)) { |
| 750 | /* |
| 751 | * the backref was added previously when processing |
| 752 | * backref of type BTRFS_TREE_BLOCK_REF_KEY |
| 753 | */ |
| 754 | ASSERT(list_is_singular(&cur->upper)); |
| 755 | edge = list_entry(cur->upper.next, struct backref_edge, |
| 756 | list[LOWER]); |
| 757 | ASSERT(list_empty(&edge->list[UPPER])); |
| 758 | exist = edge->node[UPPER]; |
| 759 | /* |
| 760 | * add the upper level block to pending list if we need |
| 761 | * check its backrefs |
| 762 | */ |
| 763 | if (!exist->checked) |
| 764 | list_add_tail(&edge->list[UPPER], &list); |
| 765 | } else { |
| 766 | exist = NULL; |
| 767 | } |
| 768 | |
| 769 | while (1) { |
| 770 | cond_resched(); |
| 771 | eb = path1->nodes[0]; |
| 772 | |
| 773 | if (ptr >= end) { |
| 774 | if (path1->slots[0] >= btrfs_header_nritems(eb)) { |
| 775 | ret = btrfs_next_leaf(rc->extent_root, path1); |
| 776 | if (ret < 0) { |
| 777 | err = ret; |
| 778 | goto out; |
| 779 | } |
| 780 | if (ret > 0) |
| 781 | break; |
| 782 | eb = path1->nodes[0]; |
| 783 | } |
| 784 | |
| 785 | btrfs_item_key_to_cpu(eb, &key, path1->slots[0]); |
| 786 | if (key.objectid != cur->bytenr) { |
| 787 | WARN_ON(exist); |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 792 | key.type == BTRFS_METADATA_ITEM_KEY) { |
| 793 | ret = find_inline_backref(eb, path1->slots[0], |
| 794 | &ptr, &end); |
| 795 | if (ret) |
| 796 | goto next; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | if (ptr < end) { |
| 801 | /* update key for inline back ref */ |
| 802 | struct btrfs_extent_inline_ref *iref; |
| 803 | int type; |
| 804 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 805 | type = btrfs_get_extent_inline_ref_type(eb, iref, |
| 806 | BTRFS_REF_TYPE_BLOCK); |
| 807 | if (type == BTRFS_REF_TYPE_INVALID) { |
| 808 | err = -EINVAL; |
| 809 | goto out; |
| 810 | } |
| 811 | key.type = type; |
| 812 | key.offset = btrfs_extent_inline_ref_offset(eb, iref); |
| 813 | |
| 814 | WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY && |
| 815 | key.type != BTRFS_SHARED_BLOCK_REF_KEY); |
| 816 | } |
| 817 | |
| 818 | if (exist && |
| 819 | ((key.type == BTRFS_TREE_BLOCK_REF_KEY && |
| 820 | exist->owner == key.offset) || |
| 821 | (key.type == BTRFS_SHARED_BLOCK_REF_KEY && |
| 822 | exist->bytenr == key.offset))) { |
| 823 | exist = NULL; |
| 824 | goto next; |
| 825 | } |
| 826 | |
| 827 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 828 | if (key.type == BTRFS_SHARED_BLOCK_REF_KEY || |
| 829 | key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 830 | if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 831 | struct btrfs_extent_ref_v0 *ref0; |
| 832 | ref0 = btrfs_item_ptr(eb, path1->slots[0], |
| 833 | struct btrfs_extent_ref_v0); |
| 834 | if (key.objectid == key.offset) { |
| 835 | root = find_tree_root(rc, eb, ref0); |
| 836 | if (root && !should_ignore_root(root)) |
| 837 | cur->root = root; |
| 838 | else |
| 839 | list_add(&cur->list, &useless); |
| 840 | break; |
| 841 | } |
| 842 | if (is_cowonly_root(btrfs_ref_root_v0(eb, |
| 843 | ref0))) |
| 844 | cur->cowonly = 1; |
| 845 | } |
| 846 | #else |
| 847 | ASSERT(key.type != BTRFS_EXTENT_REF_V0_KEY); |
| 848 | if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 849 | #endif |
| 850 | if (key.objectid == key.offset) { |
| 851 | /* |
| 852 | * only root blocks of reloc trees use |
| 853 | * backref of this type. |
| 854 | */ |
| 855 | root = find_reloc_root(rc, cur->bytenr); |
| 856 | ASSERT(root); |
| 857 | cur->root = root; |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | edge = alloc_backref_edge(cache); |
| 862 | if (!edge) { |
| 863 | err = -ENOMEM; |
| 864 | goto out; |
| 865 | } |
| 866 | rb_node = tree_search(&cache->rb_root, key.offset); |
| 867 | if (!rb_node) { |
| 868 | upper = alloc_backref_node(cache); |
| 869 | if (!upper) { |
| 870 | free_backref_edge(cache, edge); |
| 871 | err = -ENOMEM; |
| 872 | goto out; |
| 873 | } |
| 874 | upper->bytenr = key.offset; |
| 875 | upper->level = cur->level + 1; |
| 876 | /* |
| 877 | * backrefs for the upper level block isn't |
| 878 | * cached, add the block to pending list |
| 879 | */ |
| 880 | list_add_tail(&edge->list[UPPER], &list); |
| 881 | } else { |
| 882 | upper = rb_entry(rb_node, struct backref_node, |
| 883 | rb_node); |
| 884 | ASSERT(upper->checked); |
| 885 | INIT_LIST_HEAD(&edge->list[UPPER]); |
| 886 | } |
| 887 | list_add_tail(&edge->list[LOWER], &cur->upper); |
| 888 | edge->node[LOWER] = cur; |
| 889 | edge->node[UPPER] = upper; |
| 890 | |
| 891 | goto next; |
| 892 | } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) { |
| 893 | goto next; |
| 894 | } |
| 895 | |
| 896 | /* key.type == BTRFS_TREE_BLOCK_REF_KEY */ |
| 897 | root = read_fs_root(rc->extent_root->fs_info, key.offset); |
| 898 | if (IS_ERR(root)) { |
| 899 | err = PTR_ERR(root); |
| 900 | goto out; |
| 901 | } |
| 902 | |
| 903 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 904 | cur->cowonly = 1; |
| 905 | |
| 906 | if (btrfs_root_level(&root->root_item) == cur->level) { |
| 907 | /* tree root */ |
| 908 | ASSERT(btrfs_root_bytenr(&root->root_item) == |
| 909 | cur->bytenr); |
| 910 | if (should_ignore_root(root)) |
| 911 | list_add(&cur->list, &useless); |
| 912 | else |
| 913 | cur->root = root; |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | level = cur->level + 1; |
| 918 | |
| 919 | /* |
| 920 | * searching the tree to find upper level blocks |
| 921 | * reference the block. |
| 922 | */ |
| 923 | path2->search_commit_root = 1; |
| 924 | path2->skip_locking = 1; |
| 925 | path2->lowest_level = level; |
| 926 | ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0); |
| 927 | path2->lowest_level = 0; |
| 928 | if (ret < 0) { |
| 929 | err = ret; |
| 930 | goto out; |
| 931 | } |
| 932 | if (ret > 0 && path2->slots[level] > 0) |
| 933 | path2->slots[level]--; |
| 934 | |
| 935 | eb = path2->nodes[level]; |
| 936 | if (btrfs_node_blockptr(eb, path2->slots[level]) != |
| 937 | cur->bytenr) { |
| 938 | btrfs_err(root->fs_info, |
| 939 | "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)", |
| 940 | cur->bytenr, level - 1, root->objectid, |
| 941 | node_key->objectid, node_key->type, |
| 942 | node_key->offset); |
| 943 | err = -ENOENT; |
| 944 | goto out; |
| 945 | } |
| 946 | lower = cur; |
| 947 | need_check = true; |
| 948 | for (; level < BTRFS_MAX_LEVEL; level++) { |
| 949 | if (!path2->nodes[level]) { |
| 950 | ASSERT(btrfs_root_bytenr(&root->root_item) == |
| 951 | lower->bytenr); |
| 952 | if (should_ignore_root(root)) |
| 953 | list_add(&lower->list, &useless); |
| 954 | else |
| 955 | lower->root = root; |
| 956 | break; |
| 957 | } |
| 958 | |
| 959 | edge = alloc_backref_edge(cache); |
| 960 | if (!edge) { |
| 961 | err = -ENOMEM; |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | eb = path2->nodes[level]; |
| 966 | rb_node = tree_search(&cache->rb_root, eb->start); |
| 967 | if (!rb_node) { |
| 968 | upper = alloc_backref_node(cache); |
| 969 | if (!upper) { |
| 970 | free_backref_edge(cache, edge); |
| 971 | err = -ENOMEM; |
| 972 | goto out; |
| 973 | } |
| 974 | upper->bytenr = eb->start; |
| 975 | upper->owner = btrfs_header_owner(eb); |
| 976 | upper->level = lower->level + 1; |
| 977 | if (!test_bit(BTRFS_ROOT_REF_COWS, |
| 978 | &root->state)) |
| 979 | upper->cowonly = 1; |
| 980 | |
| 981 | /* |
| 982 | * if we know the block isn't shared |
| 983 | * we can void checking its backrefs. |
| 984 | */ |
| 985 | if (btrfs_block_can_be_shared(root, eb)) |
| 986 | upper->checked = 0; |
| 987 | else |
| 988 | upper->checked = 1; |
| 989 | |
| 990 | /* |
| 991 | * add the block to pending list if we |
| 992 | * need check its backrefs, we only do this once |
| 993 | * while walking up a tree as we will catch |
| 994 | * anything else later on. |
| 995 | */ |
| 996 | if (!upper->checked && need_check) { |
| 997 | need_check = false; |
| 998 | list_add_tail(&edge->list[UPPER], |
| 999 | &list); |
| 1000 | } else { |
| 1001 | if (upper->checked) |
| 1002 | need_check = true; |
| 1003 | INIT_LIST_HEAD(&edge->list[UPPER]); |
| 1004 | } |
| 1005 | } else { |
| 1006 | upper = rb_entry(rb_node, struct backref_node, |
| 1007 | rb_node); |
| 1008 | ASSERT(upper->checked); |
| 1009 | INIT_LIST_HEAD(&edge->list[UPPER]); |
| 1010 | if (!upper->owner) |
| 1011 | upper->owner = btrfs_header_owner(eb); |
| 1012 | } |
| 1013 | list_add_tail(&edge->list[LOWER], &lower->upper); |
| 1014 | edge->node[LOWER] = lower; |
| 1015 | edge->node[UPPER] = upper; |
| 1016 | |
| 1017 | if (rb_node) |
| 1018 | break; |
| 1019 | lower = upper; |
| 1020 | upper = NULL; |
| 1021 | } |
| 1022 | btrfs_release_path(path2); |
| 1023 | next: |
| 1024 | if (ptr < end) { |
| 1025 | ptr += btrfs_extent_inline_ref_size(key.type); |
| 1026 | if (ptr >= end) { |
| 1027 | WARN_ON(ptr > end); |
| 1028 | ptr = 0; |
| 1029 | end = 0; |
| 1030 | } |
| 1031 | } |
| 1032 | if (ptr >= end) |
| 1033 | path1->slots[0]++; |
| 1034 | } |
| 1035 | btrfs_release_path(path1); |
| 1036 | |
| 1037 | cur->checked = 1; |
| 1038 | WARN_ON(exist); |
| 1039 | |
| 1040 | /* the pending list isn't empty, take the first block to process */ |
| 1041 | if (!list_empty(&list)) { |
| 1042 | edge = list_entry(list.next, struct backref_edge, list[UPPER]); |
| 1043 | list_del_init(&edge->list[UPPER]); |
| 1044 | cur = edge->node[UPPER]; |
| 1045 | goto again; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * everything goes well, connect backref nodes and insert backref nodes |
| 1050 | * into the cache. |
| 1051 | */ |
| 1052 | ASSERT(node->checked); |
| 1053 | cowonly = node->cowonly; |
| 1054 | if (!cowonly) { |
| 1055 | rb_node = tree_insert(&cache->rb_root, node->bytenr, |
| 1056 | &node->rb_node); |
| 1057 | if (rb_node) |
| 1058 | backref_tree_panic(rb_node, -EEXIST, node->bytenr); |
| 1059 | list_add_tail(&node->lower, &cache->leaves); |
| 1060 | } |
| 1061 | |
| 1062 | list_for_each_entry(edge, &node->upper, list[LOWER]) |
| 1063 | list_add_tail(&edge->list[UPPER], &list); |
| 1064 | |
| 1065 | while (!list_empty(&list)) { |
| 1066 | edge = list_entry(list.next, struct backref_edge, list[UPPER]); |
| 1067 | list_del_init(&edge->list[UPPER]); |
| 1068 | upper = edge->node[UPPER]; |
| 1069 | if (upper->detached) { |
| 1070 | list_del(&edge->list[LOWER]); |
| 1071 | lower = edge->node[LOWER]; |
| 1072 | free_backref_edge(cache, edge); |
| 1073 | if (list_empty(&lower->upper)) |
| 1074 | list_add(&lower->list, &useless); |
| 1075 | continue; |
| 1076 | } |
| 1077 | |
| 1078 | if (!RB_EMPTY_NODE(&upper->rb_node)) { |
| 1079 | if (upper->lowest) { |
| 1080 | list_del_init(&upper->lower); |
| 1081 | upper->lowest = 0; |
| 1082 | } |
| 1083 | |
| 1084 | list_add_tail(&edge->list[UPPER], &upper->lower); |
| 1085 | continue; |
| 1086 | } |
| 1087 | |
| 1088 | if (!upper->checked) { |
| 1089 | /* |
| 1090 | * Still want to blow up for developers since this is a |
| 1091 | * logic bug. |
| 1092 | */ |
| 1093 | ASSERT(0); |
| 1094 | err = -EINVAL; |
| 1095 | goto out; |
| 1096 | } |
| 1097 | if (cowonly != upper->cowonly) { |
| 1098 | ASSERT(0); |
| 1099 | err = -EINVAL; |
| 1100 | goto out; |
| 1101 | } |
| 1102 | |
| 1103 | if (!cowonly) { |
| 1104 | rb_node = tree_insert(&cache->rb_root, upper->bytenr, |
| 1105 | &upper->rb_node); |
| 1106 | if (rb_node) |
| 1107 | backref_tree_panic(rb_node, -EEXIST, |
| 1108 | upper->bytenr); |
| 1109 | } |
| 1110 | |
| 1111 | list_add_tail(&edge->list[UPPER], &upper->lower); |
| 1112 | |
| 1113 | list_for_each_entry(edge, &upper->upper, list[LOWER]) |
| 1114 | list_add_tail(&edge->list[UPPER], &list); |
| 1115 | } |
| 1116 | /* |
| 1117 | * process useless backref nodes. backref nodes for tree leaves |
| 1118 | * are deleted from the cache. backref nodes for upper level |
| 1119 | * tree blocks are left in the cache to avoid unnecessary backref |
| 1120 | * lookup. |
| 1121 | */ |
| 1122 | while (!list_empty(&useless)) { |
| 1123 | upper = list_entry(useless.next, struct backref_node, list); |
| 1124 | list_del_init(&upper->list); |
| 1125 | ASSERT(list_empty(&upper->upper)); |
| 1126 | if (upper == node) |
| 1127 | node = NULL; |
| 1128 | if (upper->lowest) { |
| 1129 | list_del_init(&upper->lower); |
| 1130 | upper->lowest = 0; |
| 1131 | } |
| 1132 | while (!list_empty(&upper->lower)) { |
| 1133 | edge = list_entry(upper->lower.next, |
| 1134 | struct backref_edge, list[UPPER]); |
| 1135 | list_del(&edge->list[UPPER]); |
| 1136 | list_del(&edge->list[LOWER]); |
| 1137 | lower = edge->node[LOWER]; |
| 1138 | free_backref_edge(cache, edge); |
| 1139 | |
| 1140 | if (list_empty(&lower->upper)) |
| 1141 | list_add(&lower->list, &useless); |
| 1142 | } |
| 1143 | __mark_block_processed(rc, upper); |
| 1144 | if (upper->level > 0) { |
| 1145 | list_add(&upper->list, &cache->detached); |
| 1146 | upper->detached = 1; |
| 1147 | } else { |
| 1148 | rb_erase(&upper->rb_node, &cache->rb_root); |
| 1149 | free_backref_node(cache, upper); |
| 1150 | } |
| 1151 | } |
| 1152 | out: |
| 1153 | btrfs_free_path(path1); |
| 1154 | btrfs_free_path(path2); |
| 1155 | if (err) { |
| 1156 | while (!list_empty(&useless)) { |
| 1157 | lower = list_entry(useless.next, |
| 1158 | struct backref_node, list); |
| 1159 | list_del_init(&lower->list); |
| 1160 | } |
| 1161 | while (!list_empty(&list)) { |
| 1162 | edge = list_first_entry(&list, struct backref_edge, |
| 1163 | list[UPPER]); |
| 1164 | list_del(&edge->list[UPPER]); |
| 1165 | list_del(&edge->list[LOWER]); |
| 1166 | lower = edge->node[LOWER]; |
| 1167 | upper = edge->node[UPPER]; |
| 1168 | free_backref_edge(cache, edge); |
| 1169 | |
| 1170 | /* |
| 1171 | * Lower is no longer linked to any upper backref nodes |
| 1172 | * and isn't in the cache, we can free it ourselves. |
| 1173 | */ |
| 1174 | if (list_empty(&lower->upper) && |
| 1175 | RB_EMPTY_NODE(&lower->rb_node)) |
| 1176 | list_add(&lower->list, &useless); |
| 1177 | |
| 1178 | if (!RB_EMPTY_NODE(&upper->rb_node)) |
| 1179 | continue; |
| 1180 | |
| 1181 | /* Add this guy's upper edges to the list to process */ |
| 1182 | list_for_each_entry(edge, &upper->upper, list[LOWER]) |
| 1183 | list_add_tail(&edge->list[UPPER], &list); |
| 1184 | if (list_empty(&upper->upper)) |
| 1185 | list_add(&upper->list, &useless); |
| 1186 | } |
| 1187 | |
| 1188 | while (!list_empty(&useless)) { |
| 1189 | lower = list_entry(useless.next, |
| 1190 | struct backref_node, list); |
| 1191 | list_del_init(&lower->list); |
| 1192 | if (lower == node) |
| 1193 | node = NULL; |
| 1194 | free_backref_node(cache, lower); |
| 1195 | } |
| 1196 | |
| 1197 | remove_backref_node(cache, node); |
| 1198 | return ERR_PTR(err); |
| 1199 | } |
| 1200 | ASSERT(!node || !node->detached); |
| 1201 | return node; |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * helper to add backref node for the newly created snapshot. |
| 1206 | * the backref node is created by cloning backref node that |
| 1207 | * corresponds to root of source tree |
| 1208 | */ |
| 1209 | static int clone_backref_node(struct btrfs_trans_handle *trans, |
| 1210 | struct reloc_control *rc, |
| 1211 | struct btrfs_root *src, |
| 1212 | struct btrfs_root *dest) |
| 1213 | { |
| 1214 | struct btrfs_root *reloc_root = src->reloc_root; |
| 1215 | struct backref_cache *cache = &rc->backref_cache; |
| 1216 | struct backref_node *node = NULL; |
| 1217 | struct backref_node *new_node; |
| 1218 | struct backref_edge *edge; |
| 1219 | struct backref_edge *new_edge; |
| 1220 | struct rb_node *rb_node; |
| 1221 | |
| 1222 | if (cache->last_trans > 0) |
| 1223 | update_backref_cache(trans, cache); |
| 1224 | |
| 1225 | rb_node = tree_search(&cache->rb_root, src->commit_root->start); |
| 1226 | if (rb_node) { |
| 1227 | node = rb_entry(rb_node, struct backref_node, rb_node); |
| 1228 | if (node->detached) |
| 1229 | node = NULL; |
| 1230 | else |
| 1231 | BUG_ON(node->new_bytenr != reloc_root->node->start); |
| 1232 | } |
| 1233 | |
| 1234 | if (!node) { |
| 1235 | rb_node = tree_search(&cache->rb_root, |
| 1236 | reloc_root->commit_root->start); |
| 1237 | if (rb_node) { |
| 1238 | node = rb_entry(rb_node, struct backref_node, |
| 1239 | rb_node); |
| 1240 | BUG_ON(node->detached); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | if (!node) |
| 1245 | return 0; |
| 1246 | |
| 1247 | new_node = alloc_backref_node(cache); |
| 1248 | if (!new_node) |
| 1249 | return -ENOMEM; |
| 1250 | |
| 1251 | new_node->bytenr = dest->node->start; |
| 1252 | new_node->level = node->level; |
| 1253 | new_node->lowest = node->lowest; |
| 1254 | new_node->checked = 1; |
| 1255 | new_node->root = dest; |
| 1256 | |
| 1257 | if (!node->lowest) { |
| 1258 | list_for_each_entry(edge, &node->lower, list[UPPER]) { |
| 1259 | new_edge = alloc_backref_edge(cache); |
| 1260 | if (!new_edge) |
| 1261 | goto fail; |
| 1262 | |
| 1263 | new_edge->node[UPPER] = new_node; |
| 1264 | new_edge->node[LOWER] = edge->node[LOWER]; |
| 1265 | list_add_tail(&new_edge->list[UPPER], |
| 1266 | &new_node->lower); |
| 1267 | } |
| 1268 | } else { |
| 1269 | list_add_tail(&new_node->lower, &cache->leaves); |
| 1270 | } |
| 1271 | |
| 1272 | rb_node = tree_insert(&cache->rb_root, new_node->bytenr, |
| 1273 | &new_node->rb_node); |
| 1274 | if (rb_node) |
| 1275 | backref_tree_panic(rb_node, -EEXIST, new_node->bytenr); |
| 1276 | |
| 1277 | if (!new_node->lowest) { |
| 1278 | list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) { |
| 1279 | list_add_tail(&new_edge->list[LOWER], |
| 1280 | &new_edge->node[LOWER]->upper); |
| 1281 | } |
| 1282 | } |
| 1283 | return 0; |
| 1284 | fail: |
| 1285 | while (!list_empty(&new_node->lower)) { |
| 1286 | new_edge = list_entry(new_node->lower.next, |
| 1287 | struct backref_edge, list[UPPER]); |
| 1288 | list_del(&new_edge->list[UPPER]); |
| 1289 | free_backref_edge(cache, new_edge); |
| 1290 | } |
| 1291 | free_backref_node(cache, new_node); |
| 1292 | return -ENOMEM; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * helper to add 'address of tree root -> reloc tree' mapping |
| 1297 | */ |
| 1298 | static int __must_check __add_reloc_root(struct btrfs_root *root) |
| 1299 | { |
| 1300 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1301 | struct rb_node *rb_node; |
| 1302 | struct mapping_node *node; |
| 1303 | struct reloc_control *rc = fs_info->reloc_ctl; |
| 1304 | |
| 1305 | node = kmalloc(sizeof(*node), GFP_NOFS); |
| 1306 | if (!node) |
| 1307 | return -ENOMEM; |
| 1308 | |
| 1309 | node->bytenr = root->commit_root->start; |
| 1310 | node->data = root; |
| 1311 | |
| 1312 | spin_lock(&rc->reloc_root_tree.lock); |
| 1313 | rb_node = tree_insert(&rc->reloc_root_tree.rb_root, |
| 1314 | node->bytenr, &node->rb_node); |
| 1315 | spin_unlock(&rc->reloc_root_tree.lock); |
| 1316 | if (rb_node) { |
| 1317 | btrfs_panic(fs_info, -EEXIST, |
| 1318 | "Duplicate root found for start=%llu while inserting into relocation tree", |
| 1319 | node->bytenr); |
| 1320 | } |
| 1321 | |
| 1322 | list_add_tail(&root->root_list, &rc->reloc_roots); |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * helper to delete the 'address of tree root -> reloc tree' |
| 1328 | * mapping |
| 1329 | */ |
| 1330 | static void __del_reloc_root(struct btrfs_root *root) |
| 1331 | { |
| 1332 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1333 | struct rb_node *rb_node; |
| 1334 | struct mapping_node *node = NULL; |
| 1335 | struct reloc_control *rc = fs_info->reloc_ctl; |
| 1336 | |
| 1337 | if (rc && root->node) { |
| 1338 | spin_lock(&rc->reloc_root_tree.lock); |
| 1339 | rb_node = tree_search(&rc->reloc_root_tree.rb_root, |
| 1340 | root->commit_root->start); |
| 1341 | if (rb_node) { |
| 1342 | node = rb_entry(rb_node, struct mapping_node, rb_node); |
| 1343 | rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); |
| 1344 | RB_CLEAR_NODE(&node->rb_node); |
| 1345 | } |
| 1346 | spin_unlock(&rc->reloc_root_tree.lock); |
| 1347 | if (!node) |
| 1348 | return; |
| 1349 | BUG_ON((struct btrfs_root *)node->data != root); |
| 1350 | } |
| 1351 | |
| 1352 | spin_lock(&fs_info->trans_lock); |
| 1353 | list_del_init(&root->root_list); |
| 1354 | spin_unlock(&fs_info->trans_lock); |
| 1355 | kfree(node); |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | * helper to update the 'address of tree root -> reloc tree' |
| 1360 | * mapping |
| 1361 | */ |
| 1362 | static int __update_reloc_root(struct btrfs_root *root) |
| 1363 | { |
| 1364 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1365 | struct rb_node *rb_node; |
| 1366 | struct mapping_node *node = NULL; |
| 1367 | struct reloc_control *rc = fs_info->reloc_ctl; |
| 1368 | |
| 1369 | spin_lock(&rc->reloc_root_tree.lock); |
| 1370 | rb_node = tree_search(&rc->reloc_root_tree.rb_root, |
| 1371 | root->commit_root->start); |
| 1372 | if (rb_node) { |
| 1373 | node = rb_entry(rb_node, struct mapping_node, rb_node); |
| 1374 | rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); |
| 1375 | } |
| 1376 | spin_unlock(&rc->reloc_root_tree.lock); |
| 1377 | |
| 1378 | if (!node) |
| 1379 | return 0; |
| 1380 | BUG_ON((struct btrfs_root *)node->data != root); |
| 1381 | |
| 1382 | spin_lock(&rc->reloc_root_tree.lock); |
| 1383 | node->bytenr = root->node->start; |
| 1384 | rb_node = tree_insert(&rc->reloc_root_tree.rb_root, |
| 1385 | node->bytenr, &node->rb_node); |
| 1386 | spin_unlock(&rc->reloc_root_tree.lock); |
| 1387 | if (rb_node) |
| 1388 | backref_tree_panic(rb_node, -EEXIST, node->bytenr); |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, |
| 1393 | struct btrfs_root *root, u64 objectid) |
| 1394 | { |
| 1395 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1396 | struct btrfs_root *reloc_root; |
| 1397 | struct extent_buffer *eb; |
| 1398 | struct btrfs_root_item *root_item; |
| 1399 | struct btrfs_key root_key; |
| 1400 | int ret; |
| 1401 | |
| 1402 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 1403 | BUG_ON(!root_item); |
| 1404 | |
| 1405 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 1406 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 1407 | root_key.offset = objectid; |
| 1408 | |
| 1409 | if (root->root_key.objectid == objectid) { |
| 1410 | u64 commit_root_gen; |
| 1411 | |
| 1412 | /* called by btrfs_init_reloc_root */ |
| 1413 | ret = btrfs_copy_root(trans, root, root->commit_root, &eb, |
| 1414 | BTRFS_TREE_RELOC_OBJECTID); |
| 1415 | BUG_ON(ret); |
| 1416 | /* |
| 1417 | * Set the last_snapshot field to the generation of the commit |
| 1418 | * root - like this ctree.c:btrfs_block_can_be_shared() behaves |
| 1419 | * correctly (returns true) when the relocation root is created |
| 1420 | * either inside the critical section of a transaction commit |
| 1421 | * (through transaction.c:qgroup_account_snapshot()) and when |
| 1422 | * it's created before the transaction commit is started. |
| 1423 | */ |
| 1424 | commit_root_gen = btrfs_header_generation(root->commit_root); |
| 1425 | btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen); |
| 1426 | } else { |
| 1427 | /* |
| 1428 | * called by btrfs_reloc_post_snapshot_hook. |
| 1429 | * the source tree is a reloc tree, all tree blocks |
| 1430 | * modified after it was created have RELOC flag |
| 1431 | * set in their headers. so it's OK to not update |
| 1432 | * the 'last_snapshot'. |
| 1433 | */ |
| 1434 | ret = btrfs_copy_root(trans, root, root->node, &eb, |
| 1435 | BTRFS_TREE_RELOC_OBJECTID); |
| 1436 | BUG_ON(ret); |
| 1437 | } |
| 1438 | |
| 1439 | memcpy(root_item, &root->root_item, sizeof(*root_item)); |
| 1440 | btrfs_set_root_bytenr(root_item, eb->start); |
| 1441 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
| 1442 | btrfs_set_root_generation(root_item, trans->transid); |
| 1443 | |
| 1444 | if (root->root_key.objectid == objectid) { |
| 1445 | btrfs_set_root_refs(root_item, 0); |
| 1446 | memset(&root_item->drop_progress, 0, |
| 1447 | sizeof(struct btrfs_disk_key)); |
| 1448 | root_item->drop_level = 0; |
| 1449 | } |
| 1450 | |
| 1451 | btrfs_tree_unlock(eb); |
| 1452 | free_extent_buffer(eb); |
| 1453 | |
| 1454 | ret = btrfs_insert_root(trans, fs_info->tree_root, |
| 1455 | &root_key, root_item); |
| 1456 | BUG_ON(ret); |
| 1457 | kfree(root_item); |
| 1458 | |
| 1459 | reloc_root = btrfs_read_fs_root(fs_info->tree_root, &root_key); |
| 1460 | BUG_ON(IS_ERR(reloc_root)); |
| 1461 | reloc_root->last_trans = trans->transid; |
| 1462 | return reloc_root; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * create reloc tree for a given fs tree. reloc tree is just a |
| 1467 | * snapshot of the fs tree with special root objectid. |
| 1468 | */ |
| 1469 | int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, |
| 1470 | struct btrfs_root *root) |
| 1471 | { |
| 1472 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1473 | struct btrfs_root *reloc_root; |
| 1474 | struct reloc_control *rc = fs_info->reloc_ctl; |
| 1475 | struct btrfs_block_rsv *rsv; |
| 1476 | int clear_rsv = 0; |
| 1477 | int ret; |
| 1478 | |
| 1479 | if (root->reloc_root) { |
| 1480 | reloc_root = root->reloc_root; |
| 1481 | reloc_root->last_trans = trans->transid; |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | if (!rc || !rc->create_reloc_tree || |
| 1486 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1487 | return 0; |
| 1488 | |
| 1489 | if (!trans->reloc_reserved) { |
| 1490 | rsv = trans->block_rsv; |
| 1491 | trans->block_rsv = rc->block_rsv; |
| 1492 | clear_rsv = 1; |
| 1493 | } |
| 1494 | reloc_root = create_reloc_root(trans, root, root->root_key.objectid); |
| 1495 | if (clear_rsv) |
| 1496 | trans->block_rsv = rsv; |
| 1497 | |
| 1498 | ret = __add_reloc_root(reloc_root); |
| 1499 | BUG_ON(ret < 0); |
| 1500 | root->reloc_root = reloc_root; |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * update root item of reloc tree |
| 1506 | */ |
| 1507 | int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, |
| 1508 | struct btrfs_root *root) |
| 1509 | { |
| 1510 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1511 | struct btrfs_root *reloc_root; |
| 1512 | struct btrfs_root_item *root_item; |
| 1513 | int ret; |
| 1514 | |
| 1515 | if (!root->reloc_root) |
| 1516 | goto out; |
| 1517 | |
| 1518 | reloc_root = root->reloc_root; |
| 1519 | root_item = &reloc_root->root_item; |
| 1520 | |
| 1521 | if (fs_info->reloc_ctl->merge_reloc_tree && |
| 1522 | btrfs_root_refs(root_item) == 0) { |
| 1523 | root->reloc_root = NULL; |
| 1524 | __del_reloc_root(reloc_root); |
| 1525 | } |
| 1526 | |
| 1527 | if (reloc_root->commit_root != reloc_root->node) { |
| 1528 | __update_reloc_root(reloc_root); |
| 1529 | btrfs_set_root_node(root_item, reloc_root->node); |
| 1530 | free_extent_buffer(reloc_root->commit_root); |
| 1531 | reloc_root->commit_root = btrfs_root_node(reloc_root); |
| 1532 | } |
| 1533 | |
| 1534 | ret = btrfs_update_root(trans, fs_info->tree_root, |
| 1535 | &reloc_root->root_key, root_item); |
| 1536 | BUG_ON(ret); |
| 1537 | |
| 1538 | out: |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * helper to find first cached inode with inode number >= objectid |
| 1544 | * in a subvolume |
| 1545 | */ |
| 1546 | static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid) |
| 1547 | { |
| 1548 | struct rb_node *node; |
| 1549 | struct rb_node *prev; |
| 1550 | struct btrfs_inode *entry; |
| 1551 | struct inode *inode; |
| 1552 | |
| 1553 | spin_lock(&root->inode_lock); |
| 1554 | again: |
| 1555 | node = root->inode_tree.rb_node; |
| 1556 | prev = NULL; |
| 1557 | while (node) { |
| 1558 | prev = node; |
| 1559 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 1560 | |
| 1561 | if (objectid < btrfs_ino(entry)) |
| 1562 | node = node->rb_left; |
| 1563 | else if (objectid > btrfs_ino(entry)) |
| 1564 | node = node->rb_right; |
| 1565 | else |
| 1566 | break; |
| 1567 | } |
| 1568 | if (!node) { |
| 1569 | while (prev) { |
| 1570 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
| 1571 | if (objectid <= btrfs_ino(entry)) { |
| 1572 | node = prev; |
| 1573 | break; |
| 1574 | } |
| 1575 | prev = rb_next(prev); |
| 1576 | } |
| 1577 | } |
| 1578 | while (node) { |
| 1579 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 1580 | inode = igrab(&entry->vfs_inode); |
| 1581 | if (inode) { |
| 1582 | spin_unlock(&root->inode_lock); |
| 1583 | return inode; |
| 1584 | } |
| 1585 | |
| 1586 | objectid = btrfs_ino(entry) + 1; |
| 1587 | if (cond_resched_lock(&root->inode_lock)) |
| 1588 | goto again; |
| 1589 | |
| 1590 | node = rb_next(node); |
| 1591 | } |
| 1592 | spin_unlock(&root->inode_lock); |
| 1593 | return NULL; |
| 1594 | } |
| 1595 | |
| 1596 | static int in_block_group(u64 bytenr, |
| 1597 | struct btrfs_block_group_cache *block_group) |
| 1598 | { |
| 1599 | if (bytenr >= block_group->key.objectid && |
| 1600 | bytenr < block_group->key.objectid + block_group->key.offset) |
| 1601 | return 1; |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * get new location of data |
| 1607 | */ |
| 1608 | static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr, |
| 1609 | u64 bytenr, u64 num_bytes) |
| 1610 | { |
| 1611 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 1612 | struct btrfs_path *path; |
| 1613 | struct btrfs_file_extent_item *fi; |
| 1614 | struct extent_buffer *leaf; |
| 1615 | int ret; |
| 1616 | |
| 1617 | path = btrfs_alloc_path(); |
| 1618 | if (!path) |
| 1619 | return -ENOMEM; |
| 1620 | |
| 1621 | bytenr -= BTRFS_I(reloc_inode)->index_cnt; |
| 1622 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 1623 | btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0); |
| 1624 | if (ret < 0) |
| 1625 | goto out; |
| 1626 | if (ret > 0) { |
| 1627 | ret = -ENOENT; |
| 1628 | goto out; |
| 1629 | } |
| 1630 | |
| 1631 | leaf = path->nodes[0]; |
| 1632 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1633 | struct btrfs_file_extent_item); |
| 1634 | |
| 1635 | BUG_ON(btrfs_file_extent_offset(leaf, fi) || |
| 1636 | btrfs_file_extent_compression(leaf, fi) || |
| 1637 | btrfs_file_extent_encryption(leaf, fi) || |
| 1638 | btrfs_file_extent_other_encoding(leaf, fi)); |
| 1639 | |
| 1640 | if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) { |
| 1641 | ret = -EINVAL; |
| 1642 | goto out; |
| 1643 | } |
| 1644 | |
| 1645 | *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 1646 | ret = 0; |
| 1647 | out: |
| 1648 | btrfs_free_path(path); |
| 1649 | return ret; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | * update file extent items in the tree leaf to point to |
| 1654 | * the new locations. |
| 1655 | */ |
| 1656 | static noinline_for_stack |
| 1657 | int replace_file_extents(struct btrfs_trans_handle *trans, |
| 1658 | struct reloc_control *rc, |
| 1659 | struct btrfs_root *root, |
| 1660 | struct extent_buffer *leaf) |
| 1661 | { |
| 1662 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1663 | struct btrfs_key key; |
| 1664 | struct btrfs_file_extent_item *fi; |
| 1665 | struct inode *inode = NULL; |
| 1666 | u64 parent; |
| 1667 | u64 bytenr; |
| 1668 | u64 new_bytenr = 0; |
| 1669 | u64 num_bytes; |
| 1670 | u64 end; |
| 1671 | u32 nritems; |
| 1672 | u32 i; |
| 1673 | int ret = 0; |
| 1674 | int first = 1; |
| 1675 | int dirty = 0; |
| 1676 | |
| 1677 | if (rc->stage != UPDATE_DATA_PTRS) |
| 1678 | return 0; |
| 1679 | |
| 1680 | /* reloc trees always use full backref */ |
| 1681 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1682 | parent = leaf->start; |
| 1683 | else |
| 1684 | parent = 0; |
| 1685 | |
| 1686 | nritems = btrfs_header_nritems(leaf); |
| 1687 | for (i = 0; i < nritems; i++) { |
| 1688 | cond_resched(); |
| 1689 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 1690 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 1691 | continue; |
| 1692 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 1693 | if (btrfs_file_extent_type(leaf, fi) == |
| 1694 | BTRFS_FILE_EXTENT_INLINE) |
| 1695 | continue; |
| 1696 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 1697 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 1698 | if (bytenr == 0) |
| 1699 | continue; |
| 1700 | if (!in_block_group(bytenr, rc->block_group)) |
| 1701 | continue; |
| 1702 | |
| 1703 | /* |
| 1704 | * if we are modifying block in fs tree, wait for readpage |
| 1705 | * to complete and drop the extent cache |
| 1706 | */ |
| 1707 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1708 | if (first) { |
| 1709 | inode = find_next_inode(root, key.objectid); |
| 1710 | first = 0; |
| 1711 | } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) { |
| 1712 | btrfs_add_delayed_iput(inode); |
| 1713 | inode = find_next_inode(root, key.objectid); |
| 1714 | } |
| 1715 | if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) { |
| 1716 | end = key.offset + |
| 1717 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1718 | WARN_ON(!IS_ALIGNED(key.offset, |
| 1719 | fs_info->sectorsize)); |
| 1720 | WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); |
| 1721 | end--; |
| 1722 | ret = try_lock_extent(&BTRFS_I(inode)->io_tree, |
| 1723 | key.offset, end); |
| 1724 | if (!ret) |
| 1725 | continue; |
| 1726 | |
| 1727 | btrfs_drop_extent_cache(BTRFS_I(inode), |
| 1728 | key.offset, end, 1); |
| 1729 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 1730 | key.offset, end); |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | ret = get_new_location(rc->data_inode, &new_bytenr, |
| 1735 | bytenr, num_bytes); |
| 1736 | if (ret) { |
| 1737 | /* |
| 1738 | * Don't have to abort since we've not changed anything |
| 1739 | * in the file extent yet. |
| 1740 | */ |
| 1741 | break; |
| 1742 | } |
| 1743 | |
| 1744 | btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr); |
| 1745 | dirty = 1; |
| 1746 | |
| 1747 | key.offset -= btrfs_file_extent_offset(leaf, fi); |
| 1748 | ret = btrfs_inc_extent_ref(trans, fs_info, new_bytenr, |
| 1749 | num_bytes, parent, |
| 1750 | btrfs_header_owner(leaf), |
| 1751 | key.objectid, key.offset); |
| 1752 | if (ret) { |
| 1753 | btrfs_abort_transaction(trans, ret); |
| 1754 | break; |
| 1755 | } |
| 1756 | |
| 1757 | ret = btrfs_free_extent(trans, fs_info, bytenr, num_bytes, |
| 1758 | parent, btrfs_header_owner(leaf), |
| 1759 | key.objectid, key.offset); |
| 1760 | if (ret) { |
| 1761 | btrfs_abort_transaction(trans, ret); |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | if (dirty) |
| 1766 | btrfs_mark_buffer_dirty(leaf); |
| 1767 | if (inode) |
| 1768 | btrfs_add_delayed_iput(inode); |
| 1769 | return ret; |
| 1770 | } |
| 1771 | |
| 1772 | static noinline_for_stack |
| 1773 | int memcmp_node_keys(struct extent_buffer *eb, int slot, |
| 1774 | struct btrfs_path *path, int level) |
| 1775 | { |
| 1776 | struct btrfs_disk_key key1; |
| 1777 | struct btrfs_disk_key key2; |
| 1778 | btrfs_node_key(eb, &key1, slot); |
| 1779 | btrfs_node_key(path->nodes[level], &key2, path->slots[level]); |
| 1780 | return memcmp(&key1, &key2, sizeof(key1)); |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * try to replace tree blocks in fs tree with the new blocks |
| 1785 | * in reloc tree. tree blocks haven't been modified since the |
| 1786 | * reloc tree was create can be replaced. |
| 1787 | * |
| 1788 | * if a block was replaced, level of the block + 1 is returned. |
| 1789 | * if no block got replaced, 0 is returned. if there are other |
| 1790 | * errors, a negative error number is returned. |
| 1791 | */ |
| 1792 | static noinline_for_stack |
| 1793 | int replace_path(struct btrfs_trans_handle *trans, |
| 1794 | struct btrfs_root *dest, struct btrfs_root *src, |
| 1795 | struct btrfs_path *path, struct btrfs_key *next_key, |
| 1796 | int lowest_level, int max_level) |
| 1797 | { |
| 1798 | struct btrfs_fs_info *fs_info = dest->fs_info; |
| 1799 | struct extent_buffer *eb; |
| 1800 | struct extent_buffer *parent; |
| 1801 | struct btrfs_key key; |
| 1802 | u64 old_bytenr; |
| 1803 | u64 new_bytenr; |
| 1804 | u64 old_ptr_gen; |
| 1805 | u64 new_ptr_gen; |
| 1806 | u64 last_snapshot; |
| 1807 | u32 blocksize; |
| 1808 | int cow = 0; |
| 1809 | int level; |
| 1810 | int ret; |
| 1811 | int slot; |
| 1812 | |
| 1813 | BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 1814 | BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID); |
| 1815 | |
| 1816 | last_snapshot = btrfs_root_last_snapshot(&src->root_item); |
| 1817 | again: |
| 1818 | slot = path->slots[lowest_level]; |
| 1819 | btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot); |
| 1820 | |
| 1821 | eb = btrfs_lock_root_node(dest); |
| 1822 | btrfs_set_lock_blocking(eb); |
| 1823 | level = btrfs_header_level(eb); |
| 1824 | |
| 1825 | if (level < lowest_level) { |
| 1826 | btrfs_tree_unlock(eb); |
| 1827 | free_extent_buffer(eb); |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | if (cow) { |
| 1832 | ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb); |
| 1833 | BUG_ON(ret); |
| 1834 | } |
| 1835 | btrfs_set_lock_blocking(eb); |
| 1836 | |
| 1837 | if (next_key) { |
| 1838 | next_key->objectid = (u64)-1; |
| 1839 | next_key->type = (u8)-1; |
| 1840 | next_key->offset = (u64)-1; |
| 1841 | } |
| 1842 | |
| 1843 | parent = eb; |
| 1844 | while (1) { |
| 1845 | level = btrfs_header_level(parent); |
| 1846 | BUG_ON(level < lowest_level); |
| 1847 | |
| 1848 | ret = btrfs_bin_search(parent, &key, level, &slot); |
| 1849 | if (ret && slot > 0) |
| 1850 | slot--; |
| 1851 | |
| 1852 | if (next_key && slot + 1 < btrfs_header_nritems(parent)) |
| 1853 | btrfs_node_key_to_cpu(parent, next_key, slot + 1); |
| 1854 | |
| 1855 | old_bytenr = btrfs_node_blockptr(parent, slot); |
| 1856 | blocksize = fs_info->nodesize; |
| 1857 | old_ptr_gen = btrfs_node_ptr_generation(parent, slot); |
| 1858 | |
| 1859 | if (level <= max_level) { |
| 1860 | eb = path->nodes[level]; |
| 1861 | new_bytenr = btrfs_node_blockptr(eb, |
| 1862 | path->slots[level]); |
| 1863 | new_ptr_gen = btrfs_node_ptr_generation(eb, |
| 1864 | path->slots[level]); |
| 1865 | } else { |
| 1866 | new_bytenr = 0; |
| 1867 | new_ptr_gen = 0; |
| 1868 | } |
| 1869 | |
| 1870 | if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) { |
| 1871 | ret = level; |
| 1872 | break; |
| 1873 | } |
| 1874 | |
| 1875 | if (new_bytenr == 0 || old_ptr_gen > last_snapshot || |
| 1876 | memcmp_node_keys(parent, slot, path, level)) { |
| 1877 | if (level <= lowest_level) { |
| 1878 | ret = 0; |
| 1879 | break; |
| 1880 | } |
| 1881 | |
| 1882 | eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen); |
| 1883 | if (IS_ERR(eb)) { |
| 1884 | ret = PTR_ERR(eb); |
| 1885 | break; |
| 1886 | } else if (!extent_buffer_uptodate(eb)) { |
| 1887 | ret = -EIO; |
| 1888 | free_extent_buffer(eb); |
| 1889 | break; |
| 1890 | } |
| 1891 | btrfs_tree_lock(eb); |
| 1892 | if (cow) { |
| 1893 | ret = btrfs_cow_block(trans, dest, eb, parent, |
| 1894 | slot, &eb); |
| 1895 | BUG_ON(ret); |
| 1896 | } |
| 1897 | btrfs_set_lock_blocking(eb); |
| 1898 | |
| 1899 | btrfs_tree_unlock(parent); |
| 1900 | free_extent_buffer(parent); |
| 1901 | |
| 1902 | parent = eb; |
| 1903 | continue; |
| 1904 | } |
| 1905 | |
| 1906 | if (!cow) { |
| 1907 | btrfs_tree_unlock(parent); |
| 1908 | free_extent_buffer(parent); |
| 1909 | cow = 1; |
| 1910 | goto again; |
| 1911 | } |
| 1912 | |
| 1913 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 1914 | path->slots[level]); |
| 1915 | btrfs_release_path(path); |
| 1916 | |
| 1917 | path->lowest_level = level; |
| 1918 | ret = btrfs_search_slot(trans, src, &key, path, 0, 1); |
| 1919 | path->lowest_level = 0; |
| 1920 | BUG_ON(ret); |
| 1921 | |
| 1922 | /* |
| 1923 | * Info qgroup to trace both subtrees. |
| 1924 | * |
| 1925 | * We must trace both trees. |
| 1926 | * 1) Tree reloc subtree |
| 1927 | * If not traced, we will leak data numbers |
| 1928 | * 2) Fs subtree |
| 1929 | * If not traced, we will double count old data |
| 1930 | * and tree block numbers, if current trans doesn't free |
| 1931 | * data reloc tree inode. |
| 1932 | */ |
| 1933 | ret = btrfs_qgroup_trace_subtree(trans, src, parent, |
| 1934 | btrfs_header_generation(parent), |
| 1935 | btrfs_header_level(parent)); |
| 1936 | if (ret < 0) |
| 1937 | break; |
| 1938 | ret = btrfs_qgroup_trace_subtree(trans, dest, |
| 1939 | path->nodes[level], |
| 1940 | btrfs_header_generation(path->nodes[level]), |
| 1941 | btrfs_header_level(path->nodes[level])); |
| 1942 | if (ret < 0) |
| 1943 | break; |
| 1944 | |
| 1945 | /* |
| 1946 | * swap blocks in fs tree and reloc tree. |
| 1947 | */ |
| 1948 | btrfs_set_node_blockptr(parent, slot, new_bytenr); |
| 1949 | btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen); |
| 1950 | btrfs_mark_buffer_dirty(parent); |
| 1951 | |
| 1952 | btrfs_set_node_blockptr(path->nodes[level], |
| 1953 | path->slots[level], old_bytenr); |
| 1954 | btrfs_set_node_ptr_generation(path->nodes[level], |
| 1955 | path->slots[level], old_ptr_gen); |
| 1956 | btrfs_mark_buffer_dirty(path->nodes[level]); |
| 1957 | |
| 1958 | ret = btrfs_inc_extent_ref(trans, fs_info, old_bytenr, |
| 1959 | blocksize, path->nodes[level]->start, |
| 1960 | src->root_key.objectid, level - 1, 0); |
| 1961 | BUG_ON(ret); |
| 1962 | ret = btrfs_inc_extent_ref(trans, fs_info, new_bytenr, |
| 1963 | blocksize, 0, dest->root_key.objectid, |
| 1964 | level - 1, 0); |
| 1965 | BUG_ON(ret); |
| 1966 | |
| 1967 | ret = btrfs_free_extent(trans, fs_info, new_bytenr, blocksize, |
| 1968 | path->nodes[level]->start, |
| 1969 | src->root_key.objectid, level - 1, 0); |
| 1970 | BUG_ON(ret); |
| 1971 | |
| 1972 | ret = btrfs_free_extent(trans, fs_info, old_bytenr, blocksize, |
| 1973 | 0, dest->root_key.objectid, level - 1, |
| 1974 | 0); |
| 1975 | BUG_ON(ret); |
| 1976 | |
| 1977 | btrfs_unlock_up_safe(path, 0); |
| 1978 | |
| 1979 | ret = level; |
| 1980 | break; |
| 1981 | } |
| 1982 | btrfs_tree_unlock(parent); |
| 1983 | free_extent_buffer(parent); |
| 1984 | return ret; |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * helper to find next relocated block in reloc tree |
| 1989 | */ |
| 1990 | static noinline_for_stack |
| 1991 | int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, |
| 1992 | int *level) |
| 1993 | { |
| 1994 | struct extent_buffer *eb; |
| 1995 | int i; |
| 1996 | u64 last_snapshot; |
| 1997 | u32 nritems; |
| 1998 | |
| 1999 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 2000 | |
| 2001 | for (i = 0; i < *level; i++) { |
| 2002 | free_extent_buffer(path->nodes[i]); |
| 2003 | path->nodes[i] = NULL; |
| 2004 | } |
| 2005 | |
| 2006 | for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) { |
| 2007 | eb = path->nodes[i]; |
| 2008 | nritems = btrfs_header_nritems(eb); |
| 2009 | while (path->slots[i] + 1 < nritems) { |
| 2010 | path->slots[i]++; |
| 2011 | if (btrfs_node_ptr_generation(eb, path->slots[i]) <= |
| 2012 | last_snapshot) |
| 2013 | continue; |
| 2014 | |
| 2015 | *level = i; |
| 2016 | return 0; |
| 2017 | } |
| 2018 | free_extent_buffer(path->nodes[i]); |
| 2019 | path->nodes[i] = NULL; |
| 2020 | } |
| 2021 | return 1; |
| 2022 | } |
| 2023 | |
| 2024 | /* |
| 2025 | * walk down reloc tree to find relocated block of lowest level |
| 2026 | */ |
| 2027 | static noinline_for_stack |
| 2028 | int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, |
| 2029 | int *level) |
| 2030 | { |
| 2031 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2032 | struct extent_buffer *eb = NULL; |
| 2033 | int i; |
| 2034 | u64 bytenr; |
| 2035 | u64 ptr_gen = 0; |
| 2036 | u64 last_snapshot; |
| 2037 | u32 nritems; |
| 2038 | |
| 2039 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 2040 | |
| 2041 | for (i = *level; i > 0; i--) { |
| 2042 | eb = path->nodes[i]; |
| 2043 | nritems = btrfs_header_nritems(eb); |
| 2044 | while (path->slots[i] < nritems) { |
| 2045 | ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]); |
| 2046 | if (ptr_gen > last_snapshot) |
| 2047 | break; |
| 2048 | path->slots[i]++; |
| 2049 | } |
| 2050 | if (path->slots[i] >= nritems) { |
| 2051 | if (i == *level) |
| 2052 | break; |
| 2053 | *level = i + 1; |
| 2054 | return 0; |
| 2055 | } |
| 2056 | if (i == 1) { |
| 2057 | *level = i; |
| 2058 | return 0; |
| 2059 | } |
| 2060 | |
| 2061 | bytenr = btrfs_node_blockptr(eb, path->slots[i]); |
| 2062 | eb = read_tree_block(fs_info, bytenr, ptr_gen); |
| 2063 | if (IS_ERR(eb)) { |
| 2064 | return PTR_ERR(eb); |
| 2065 | } else if (!extent_buffer_uptodate(eb)) { |
| 2066 | free_extent_buffer(eb); |
| 2067 | return -EIO; |
| 2068 | } |
| 2069 | BUG_ON(btrfs_header_level(eb) != i - 1); |
| 2070 | path->nodes[i - 1] = eb; |
| 2071 | path->slots[i - 1] = 0; |
| 2072 | } |
| 2073 | return 1; |
| 2074 | } |
| 2075 | |
| 2076 | /* |
| 2077 | * invalidate extent cache for file extents whose key in range of |
| 2078 | * [min_key, max_key) |
| 2079 | */ |
| 2080 | static int invalidate_extent_cache(struct btrfs_root *root, |
| 2081 | struct btrfs_key *min_key, |
| 2082 | struct btrfs_key *max_key) |
| 2083 | { |
| 2084 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2085 | struct inode *inode = NULL; |
| 2086 | u64 objectid; |
| 2087 | u64 start, end; |
| 2088 | u64 ino; |
| 2089 | |
| 2090 | objectid = min_key->objectid; |
| 2091 | while (1) { |
| 2092 | cond_resched(); |
| 2093 | iput(inode); |
| 2094 | |
| 2095 | if (objectid > max_key->objectid) |
| 2096 | break; |
| 2097 | |
| 2098 | inode = find_next_inode(root, objectid); |
| 2099 | if (!inode) |
| 2100 | break; |
| 2101 | ino = btrfs_ino(BTRFS_I(inode)); |
| 2102 | |
| 2103 | if (ino > max_key->objectid) { |
| 2104 | iput(inode); |
| 2105 | break; |
| 2106 | } |
| 2107 | |
| 2108 | objectid = ino + 1; |
| 2109 | if (!S_ISREG(inode->i_mode)) |
| 2110 | continue; |
| 2111 | |
| 2112 | if (unlikely(min_key->objectid == ino)) { |
| 2113 | if (min_key->type > BTRFS_EXTENT_DATA_KEY) |
| 2114 | continue; |
| 2115 | if (min_key->type < BTRFS_EXTENT_DATA_KEY) |
| 2116 | start = 0; |
| 2117 | else { |
| 2118 | start = min_key->offset; |
| 2119 | WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize)); |
| 2120 | } |
| 2121 | } else { |
| 2122 | start = 0; |
| 2123 | } |
| 2124 | |
| 2125 | if (unlikely(max_key->objectid == ino)) { |
| 2126 | if (max_key->type < BTRFS_EXTENT_DATA_KEY) |
| 2127 | continue; |
| 2128 | if (max_key->type > BTRFS_EXTENT_DATA_KEY) { |
| 2129 | end = (u64)-1; |
| 2130 | } else { |
| 2131 | if (max_key->offset == 0) |
| 2132 | continue; |
| 2133 | end = max_key->offset; |
| 2134 | WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); |
| 2135 | end--; |
| 2136 | } |
| 2137 | } else { |
| 2138 | end = (u64)-1; |
| 2139 | } |
| 2140 | |
| 2141 | /* the lock_extent waits for readpage to complete */ |
| 2142 | lock_extent(&BTRFS_I(inode)->io_tree, start, end); |
| 2143 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1); |
| 2144 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
| 2145 | } |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
| 2149 | static int find_next_key(struct btrfs_path *path, int level, |
| 2150 | struct btrfs_key *key) |
| 2151 | |
| 2152 | { |
| 2153 | while (level < BTRFS_MAX_LEVEL) { |
| 2154 | if (!path->nodes[level]) |
| 2155 | break; |
| 2156 | if (path->slots[level] + 1 < |
| 2157 | btrfs_header_nritems(path->nodes[level])) { |
| 2158 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 2159 | path->slots[level] + 1); |
| 2160 | return 0; |
| 2161 | } |
| 2162 | level++; |
| 2163 | } |
| 2164 | return 1; |
| 2165 | } |
| 2166 | |
| 2167 | /* |
| 2168 | * merge the relocated tree blocks in reloc tree with corresponding |
| 2169 | * fs tree. |
| 2170 | */ |
| 2171 | static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, |
| 2172 | struct btrfs_root *root) |
| 2173 | { |
| 2174 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 2175 | LIST_HEAD(inode_list); |
| 2176 | struct btrfs_key key; |
| 2177 | struct btrfs_key next_key; |
| 2178 | struct btrfs_trans_handle *trans = NULL; |
| 2179 | struct btrfs_root *reloc_root; |
| 2180 | struct btrfs_root_item *root_item; |
| 2181 | struct btrfs_path *path; |
| 2182 | struct extent_buffer *leaf; |
| 2183 | int level; |
| 2184 | int max_level; |
| 2185 | int replaced = 0; |
| 2186 | int ret; |
| 2187 | int err = 0; |
| 2188 | u32 min_reserved; |
| 2189 | |
| 2190 | path = btrfs_alloc_path(); |
| 2191 | if (!path) |
| 2192 | return -ENOMEM; |
| 2193 | path->reada = READA_FORWARD; |
| 2194 | |
| 2195 | reloc_root = root->reloc_root; |
| 2196 | root_item = &reloc_root->root_item; |
| 2197 | |
| 2198 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 2199 | level = btrfs_root_level(root_item); |
| 2200 | extent_buffer_get(reloc_root->node); |
| 2201 | path->nodes[level] = reloc_root->node; |
| 2202 | path->slots[level] = 0; |
| 2203 | } else { |
| 2204 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
| 2205 | |
| 2206 | level = root_item->drop_level; |
| 2207 | BUG_ON(level == 0); |
| 2208 | path->lowest_level = level; |
| 2209 | ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0); |
| 2210 | path->lowest_level = 0; |
| 2211 | if (ret < 0) { |
| 2212 | btrfs_free_path(path); |
| 2213 | return ret; |
| 2214 | } |
| 2215 | |
| 2216 | btrfs_node_key_to_cpu(path->nodes[level], &next_key, |
| 2217 | path->slots[level]); |
| 2218 | WARN_ON(memcmp(&key, &next_key, sizeof(key))); |
| 2219 | |
| 2220 | btrfs_unlock_up_safe(path, 0); |
| 2221 | } |
| 2222 | |
| 2223 | min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; |
| 2224 | memset(&next_key, 0, sizeof(next_key)); |
| 2225 | |
| 2226 | while (1) { |
| 2227 | ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved, |
| 2228 | BTRFS_RESERVE_FLUSH_ALL); |
| 2229 | if (ret) { |
| 2230 | err = ret; |
| 2231 | goto out; |
| 2232 | } |
| 2233 | trans = btrfs_start_transaction(root, 0); |
| 2234 | if (IS_ERR(trans)) { |
| 2235 | err = PTR_ERR(trans); |
| 2236 | trans = NULL; |
| 2237 | goto out; |
| 2238 | } |
| 2239 | trans->block_rsv = rc->block_rsv; |
| 2240 | |
| 2241 | replaced = 0; |
| 2242 | max_level = level; |
| 2243 | |
| 2244 | ret = walk_down_reloc_tree(reloc_root, path, &level); |
| 2245 | if (ret < 0) { |
| 2246 | err = ret; |
| 2247 | goto out; |
| 2248 | } |
| 2249 | if (ret > 0) |
| 2250 | break; |
| 2251 | |
| 2252 | if (!find_next_key(path, level, &key) && |
| 2253 | btrfs_comp_cpu_keys(&next_key, &key) >= 0) { |
| 2254 | ret = 0; |
| 2255 | } else { |
| 2256 | ret = replace_path(trans, root, reloc_root, path, |
| 2257 | &next_key, level, max_level); |
| 2258 | } |
| 2259 | if (ret < 0) { |
| 2260 | err = ret; |
| 2261 | goto out; |
| 2262 | } |
| 2263 | |
| 2264 | if (ret > 0) { |
| 2265 | level = ret; |
| 2266 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 2267 | path->slots[level]); |
| 2268 | replaced = 1; |
| 2269 | } |
| 2270 | |
| 2271 | ret = walk_up_reloc_tree(reloc_root, path, &level); |
| 2272 | if (ret > 0) |
| 2273 | break; |
| 2274 | |
| 2275 | BUG_ON(level == 0); |
| 2276 | /* |
| 2277 | * save the merging progress in the drop_progress. |
| 2278 | * this is OK since root refs == 1 in this case. |
| 2279 | */ |
| 2280 | btrfs_node_key(path->nodes[level], &root_item->drop_progress, |
| 2281 | path->slots[level]); |
| 2282 | root_item->drop_level = level; |
| 2283 | |
| 2284 | btrfs_end_transaction_throttle(trans); |
| 2285 | trans = NULL; |
| 2286 | |
| 2287 | btrfs_btree_balance_dirty(fs_info); |
| 2288 | |
| 2289 | if (replaced && rc->stage == UPDATE_DATA_PTRS) |
| 2290 | invalidate_extent_cache(root, &key, &next_key); |
| 2291 | } |
| 2292 | |
| 2293 | /* |
| 2294 | * handle the case only one block in the fs tree need to be |
| 2295 | * relocated and the block is tree root. |
| 2296 | */ |
| 2297 | leaf = btrfs_lock_root_node(root); |
| 2298 | ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf); |
| 2299 | btrfs_tree_unlock(leaf); |
| 2300 | free_extent_buffer(leaf); |
| 2301 | if (ret < 0) |
| 2302 | err = ret; |
| 2303 | out: |
| 2304 | btrfs_free_path(path); |
| 2305 | |
| 2306 | if (err == 0) { |
| 2307 | memset(&root_item->drop_progress, 0, |
| 2308 | sizeof(root_item->drop_progress)); |
| 2309 | root_item->drop_level = 0; |
| 2310 | btrfs_set_root_refs(root_item, 0); |
| 2311 | btrfs_update_reloc_root(trans, root); |
| 2312 | } |
| 2313 | |
| 2314 | if (trans) |
| 2315 | btrfs_end_transaction_throttle(trans); |
| 2316 | |
| 2317 | btrfs_btree_balance_dirty(fs_info); |
| 2318 | |
| 2319 | if (replaced && rc->stage == UPDATE_DATA_PTRS) |
| 2320 | invalidate_extent_cache(root, &key, &next_key); |
| 2321 | |
| 2322 | return err; |
| 2323 | } |
| 2324 | |
| 2325 | static noinline_for_stack |
| 2326 | int prepare_to_merge(struct reloc_control *rc, int err) |
| 2327 | { |
| 2328 | struct btrfs_root *root = rc->extent_root; |
| 2329 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2330 | struct btrfs_root *reloc_root; |
| 2331 | struct btrfs_trans_handle *trans; |
| 2332 | LIST_HEAD(reloc_roots); |
| 2333 | u64 num_bytes = 0; |
| 2334 | int ret; |
| 2335 | |
| 2336 | mutex_lock(&fs_info->reloc_mutex); |
| 2337 | rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; |
| 2338 | rc->merging_rsv_size += rc->nodes_relocated * 2; |
| 2339 | mutex_unlock(&fs_info->reloc_mutex); |
| 2340 | |
| 2341 | again: |
| 2342 | if (!err) { |
| 2343 | num_bytes = rc->merging_rsv_size; |
| 2344 | ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes, |
| 2345 | BTRFS_RESERVE_FLUSH_ALL); |
| 2346 | if (ret) |
| 2347 | err = ret; |
| 2348 | } |
| 2349 | |
| 2350 | trans = btrfs_join_transaction(rc->extent_root); |
| 2351 | if (IS_ERR(trans)) { |
| 2352 | if (!err) |
| 2353 | btrfs_block_rsv_release(fs_info, rc->block_rsv, |
| 2354 | num_bytes); |
| 2355 | return PTR_ERR(trans); |
| 2356 | } |
| 2357 | |
| 2358 | if (!err) { |
| 2359 | if (num_bytes != rc->merging_rsv_size) { |
| 2360 | btrfs_end_transaction(trans); |
| 2361 | btrfs_block_rsv_release(fs_info, rc->block_rsv, |
| 2362 | num_bytes); |
| 2363 | goto again; |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | rc->merge_reloc_tree = 1; |
| 2368 | |
| 2369 | while (!list_empty(&rc->reloc_roots)) { |
| 2370 | reloc_root = list_entry(rc->reloc_roots.next, |
| 2371 | struct btrfs_root, root_list); |
| 2372 | list_del_init(&reloc_root->root_list); |
| 2373 | |
| 2374 | root = read_fs_root(fs_info, reloc_root->root_key.offset); |
| 2375 | BUG_ON(IS_ERR(root)); |
| 2376 | BUG_ON(root->reloc_root != reloc_root); |
| 2377 | |
| 2378 | /* |
| 2379 | * set reference count to 1, so btrfs_recover_relocation |
| 2380 | * knows it should resumes merging |
| 2381 | */ |
| 2382 | if (!err) |
| 2383 | btrfs_set_root_refs(&reloc_root->root_item, 1); |
| 2384 | btrfs_update_reloc_root(trans, root); |
| 2385 | |
| 2386 | list_add(&reloc_root->root_list, &reloc_roots); |
| 2387 | } |
| 2388 | |
| 2389 | list_splice(&reloc_roots, &rc->reloc_roots); |
| 2390 | |
| 2391 | if (!err) |
| 2392 | btrfs_commit_transaction(trans); |
| 2393 | else |
| 2394 | btrfs_end_transaction(trans); |
| 2395 | return err; |
| 2396 | } |
| 2397 | |
| 2398 | static noinline_for_stack |
| 2399 | void free_reloc_roots(struct list_head *list) |
| 2400 | { |
| 2401 | struct btrfs_root *reloc_root; |
| 2402 | |
| 2403 | while (!list_empty(list)) { |
| 2404 | reloc_root = list_entry(list->next, struct btrfs_root, |
| 2405 | root_list); |
| 2406 | __del_reloc_root(reloc_root); |
| 2407 | free_extent_buffer(reloc_root->node); |
| 2408 | free_extent_buffer(reloc_root->commit_root); |
| 2409 | reloc_root->node = NULL; |
| 2410 | reloc_root->commit_root = NULL; |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | static noinline_for_stack |
| 2415 | void merge_reloc_roots(struct reloc_control *rc) |
| 2416 | { |
| 2417 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 2418 | struct btrfs_root *root; |
| 2419 | struct btrfs_root *reloc_root; |
| 2420 | LIST_HEAD(reloc_roots); |
| 2421 | int found = 0; |
| 2422 | int ret = 0; |
| 2423 | again: |
| 2424 | root = rc->extent_root; |
| 2425 | |
| 2426 | /* |
| 2427 | * this serializes us with btrfs_record_root_in_transaction, |
| 2428 | * we have to make sure nobody is in the middle of |
| 2429 | * adding their roots to the list while we are |
| 2430 | * doing this splice |
| 2431 | */ |
| 2432 | mutex_lock(&fs_info->reloc_mutex); |
| 2433 | list_splice_init(&rc->reloc_roots, &reloc_roots); |
| 2434 | mutex_unlock(&fs_info->reloc_mutex); |
| 2435 | |
| 2436 | while (!list_empty(&reloc_roots)) { |
| 2437 | found = 1; |
| 2438 | reloc_root = list_entry(reloc_roots.next, |
| 2439 | struct btrfs_root, root_list); |
| 2440 | |
| 2441 | if (btrfs_root_refs(&reloc_root->root_item) > 0) { |
| 2442 | root = read_fs_root(fs_info, |
| 2443 | reloc_root->root_key.offset); |
| 2444 | BUG_ON(IS_ERR(root)); |
| 2445 | BUG_ON(root->reloc_root != reloc_root); |
| 2446 | |
| 2447 | ret = merge_reloc_root(rc, root); |
| 2448 | if (ret) { |
| 2449 | if (list_empty(&reloc_root->root_list)) |
| 2450 | list_add_tail(&reloc_root->root_list, |
| 2451 | &reloc_roots); |
| 2452 | goto out; |
| 2453 | } |
| 2454 | } else { |
| 2455 | list_del_init(&reloc_root->root_list); |
| 2456 | } |
| 2457 | |
| 2458 | ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1); |
| 2459 | if (ret < 0) { |
| 2460 | if (list_empty(&reloc_root->root_list)) |
| 2461 | list_add_tail(&reloc_root->root_list, |
| 2462 | &reloc_roots); |
| 2463 | goto out; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | if (found) { |
| 2468 | found = 0; |
| 2469 | goto again; |
| 2470 | } |
| 2471 | out: |
| 2472 | if (ret) { |
| 2473 | btrfs_handle_fs_error(fs_info, ret, NULL); |
| 2474 | if (!list_empty(&reloc_roots)) |
| 2475 | free_reloc_roots(&reloc_roots); |
| 2476 | |
| 2477 | /* new reloc root may be added */ |
| 2478 | mutex_lock(&fs_info->reloc_mutex); |
| 2479 | list_splice_init(&rc->reloc_roots, &reloc_roots); |
| 2480 | mutex_unlock(&fs_info->reloc_mutex); |
| 2481 | if (!list_empty(&reloc_roots)) |
| 2482 | free_reloc_roots(&reloc_roots); |
| 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * We used to have |
| 2487 | * |
| 2488 | * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); |
| 2489 | * |
| 2490 | * here, but it's wrong. If we fail to start the transaction in |
| 2491 | * prepare_to_merge() we will have only 0 ref reloc roots, none of which |
| 2492 | * have actually been removed from the reloc_root_tree rb tree. This is |
| 2493 | * fine because we're bailing here, and we hold a reference on the root |
| 2494 | * for the list that holds it, so these roots will be cleaned up when we |
| 2495 | * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root |
| 2496 | * will be cleaned up on unmount. |
| 2497 | * |
| 2498 | * The remaining nodes will be cleaned up by free_reloc_control. |
| 2499 | */ |
| 2500 | } |
| 2501 | |
| 2502 | static void free_block_list(struct rb_root *blocks) |
| 2503 | { |
| 2504 | struct tree_block *block; |
| 2505 | struct rb_node *rb_node; |
| 2506 | while ((rb_node = rb_first(blocks))) { |
| 2507 | block = rb_entry(rb_node, struct tree_block, rb_node); |
| 2508 | rb_erase(rb_node, blocks); |
| 2509 | kfree(block); |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, |
| 2514 | struct btrfs_root *reloc_root) |
| 2515 | { |
| 2516 | struct btrfs_fs_info *fs_info = reloc_root->fs_info; |
| 2517 | struct btrfs_root *root; |
| 2518 | |
| 2519 | if (reloc_root->last_trans == trans->transid) |
| 2520 | return 0; |
| 2521 | |
| 2522 | root = read_fs_root(fs_info, reloc_root->root_key.offset); |
| 2523 | BUG_ON(IS_ERR(root)); |
| 2524 | BUG_ON(root->reloc_root != reloc_root); |
| 2525 | |
| 2526 | return btrfs_record_root_in_trans(trans, root); |
| 2527 | } |
| 2528 | |
| 2529 | static noinline_for_stack |
| 2530 | struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, |
| 2531 | struct reloc_control *rc, |
| 2532 | struct backref_node *node, |
| 2533 | struct backref_edge *edges[]) |
| 2534 | { |
| 2535 | struct backref_node *next; |
| 2536 | struct btrfs_root *root; |
| 2537 | int index = 0; |
| 2538 | |
| 2539 | next = node; |
| 2540 | while (1) { |
| 2541 | cond_resched(); |
| 2542 | next = walk_up_backref(next, edges, &index); |
| 2543 | root = next->root; |
| 2544 | BUG_ON(!root); |
| 2545 | BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state)); |
| 2546 | |
| 2547 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 2548 | record_reloc_root_in_trans(trans, root); |
| 2549 | break; |
| 2550 | } |
| 2551 | |
| 2552 | btrfs_record_root_in_trans(trans, root); |
| 2553 | root = root->reloc_root; |
| 2554 | |
| 2555 | if (next->new_bytenr != root->node->start) { |
| 2556 | BUG_ON(next->new_bytenr); |
| 2557 | BUG_ON(!list_empty(&next->list)); |
| 2558 | next->new_bytenr = root->node->start; |
| 2559 | next->root = root; |
| 2560 | list_add_tail(&next->list, |
| 2561 | &rc->backref_cache.changed); |
| 2562 | __mark_block_processed(rc, next); |
| 2563 | break; |
| 2564 | } |
| 2565 | |
| 2566 | WARN_ON(1); |
| 2567 | root = NULL; |
| 2568 | next = walk_down_backref(edges, &index); |
| 2569 | if (!next || next->level <= node->level) |
| 2570 | break; |
| 2571 | } |
| 2572 | if (!root) |
| 2573 | return NULL; |
| 2574 | |
| 2575 | next = node; |
| 2576 | /* setup backref node path for btrfs_reloc_cow_block */ |
| 2577 | while (1) { |
| 2578 | rc->backref_cache.path[next->level] = next; |
| 2579 | if (--index < 0) |
| 2580 | break; |
| 2581 | next = edges[index]->node[UPPER]; |
| 2582 | } |
| 2583 | return root; |
| 2584 | } |
| 2585 | |
| 2586 | /* |
| 2587 | * select a tree root for relocation. return NULL if the block |
| 2588 | * is reference counted. we should use do_relocation() in this |
| 2589 | * case. return a tree root pointer if the block isn't reference |
| 2590 | * counted. return -ENOENT if the block is root of reloc tree. |
| 2591 | */ |
| 2592 | static noinline_for_stack |
| 2593 | struct btrfs_root *select_one_root(struct backref_node *node) |
| 2594 | { |
| 2595 | struct backref_node *next; |
| 2596 | struct btrfs_root *root; |
| 2597 | struct btrfs_root *fs_root = NULL; |
| 2598 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2599 | int index = 0; |
| 2600 | |
| 2601 | next = node; |
| 2602 | while (1) { |
| 2603 | cond_resched(); |
| 2604 | next = walk_up_backref(next, edges, &index); |
| 2605 | root = next->root; |
| 2606 | BUG_ON(!root); |
| 2607 | |
| 2608 | /* no other choice for non-references counted tree */ |
| 2609 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
| 2610 | return root; |
| 2611 | |
| 2612 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) |
| 2613 | fs_root = root; |
| 2614 | |
| 2615 | if (next != node) |
| 2616 | return NULL; |
| 2617 | |
| 2618 | next = walk_down_backref(edges, &index); |
| 2619 | if (!next || next->level <= node->level) |
| 2620 | break; |
| 2621 | } |
| 2622 | |
| 2623 | if (!fs_root) |
| 2624 | return ERR_PTR(-ENOENT); |
| 2625 | return fs_root; |
| 2626 | } |
| 2627 | |
| 2628 | static noinline_for_stack |
| 2629 | u64 calcu_metadata_size(struct reloc_control *rc, |
| 2630 | struct backref_node *node, int reserve) |
| 2631 | { |
| 2632 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 2633 | struct backref_node *next = node; |
| 2634 | struct backref_edge *edge; |
| 2635 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2636 | u64 num_bytes = 0; |
| 2637 | int index = 0; |
| 2638 | |
| 2639 | BUG_ON(reserve && node->processed); |
| 2640 | |
| 2641 | while (next) { |
| 2642 | cond_resched(); |
| 2643 | while (1) { |
| 2644 | if (next->processed && (reserve || next != node)) |
| 2645 | break; |
| 2646 | |
| 2647 | num_bytes += fs_info->nodesize; |
| 2648 | |
| 2649 | if (list_empty(&next->upper)) |
| 2650 | break; |
| 2651 | |
| 2652 | edge = list_entry(next->upper.next, |
| 2653 | struct backref_edge, list[LOWER]); |
| 2654 | edges[index++] = edge; |
| 2655 | next = edge->node[UPPER]; |
| 2656 | } |
| 2657 | next = walk_down_backref(edges, &index); |
| 2658 | } |
| 2659 | return num_bytes; |
| 2660 | } |
| 2661 | |
| 2662 | static int reserve_metadata_space(struct btrfs_trans_handle *trans, |
| 2663 | struct reloc_control *rc, |
| 2664 | struct backref_node *node) |
| 2665 | { |
| 2666 | struct btrfs_root *root = rc->extent_root; |
| 2667 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2668 | u64 num_bytes; |
| 2669 | int ret; |
| 2670 | u64 tmp; |
| 2671 | |
| 2672 | num_bytes = calcu_metadata_size(rc, node, 1) * 2; |
| 2673 | |
| 2674 | trans->block_rsv = rc->block_rsv; |
| 2675 | rc->reserved_bytes += num_bytes; |
| 2676 | |
| 2677 | /* |
| 2678 | * We are under a transaction here so we can only do limited flushing. |
| 2679 | * If we get an enospc just kick back -EAGAIN so we know to drop the |
| 2680 | * transaction and try to refill when we can flush all the things. |
| 2681 | */ |
| 2682 | ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes, |
| 2683 | BTRFS_RESERVE_FLUSH_LIMIT); |
| 2684 | if (ret) { |
| 2685 | tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES; |
| 2686 | while (tmp <= rc->reserved_bytes) |
| 2687 | tmp <<= 1; |
| 2688 | /* |
| 2689 | * only one thread can access block_rsv at this point, |
| 2690 | * so we don't need hold lock to protect block_rsv. |
| 2691 | * we expand more reservation size here to allow enough |
| 2692 | * space for relocation and we will return eailer in |
| 2693 | * enospc case. |
| 2694 | */ |
| 2695 | rc->block_rsv->size = tmp + fs_info->nodesize * |
| 2696 | RELOCATION_RESERVED_NODES; |
| 2697 | return -EAGAIN; |
| 2698 | } |
| 2699 | |
| 2700 | return 0; |
| 2701 | } |
| 2702 | |
| 2703 | /* |
| 2704 | * relocate a block tree, and then update pointers in upper level |
| 2705 | * blocks that reference the block to point to the new location. |
| 2706 | * |
| 2707 | * if called by link_to_upper, the block has already been relocated. |
| 2708 | * in that case this function just updates pointers. |
| 2709 | */ |
| 2710 | static int do_relocation(struct btrfs_trans_handle *trans, |
| 2711 | struct reloc_control *rc, |
| 2712 | struct backref_node *node, |
| 2713 | struct btrfs_key *key, |
| 2714 | struct btrfs_path *path, int lowest) |
| 2715 | { |
| 2716 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 2717 | struct backref_node *upper; |
| 2718 | struct backref_edge *edge; |
| 2719 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2720 | struct btrfs_root *root; |
| 2721 | struct extent_buffer *eb; |
| 2722 | u32 blocksize; |
| 2723 | u64 bytenr; |
| 2724 | u64 generation; |
| 2725 | int slot; |
| 2726 | int ret; |
| 2727 | int err = 0; |
| 2728 | |
| 2729 | BUG_ON(lowest && node->eb); |
| 2730 | |
| 2731 | path->lowest_level = node->level + 1; |
| 2732 | rc->backref_cache.path[node->level] = node; |
| 2733 | list_for_each_entry(edge, &node->upper, list[LOWER]) { |
| 2734 | cond_resched(); |
| 2735 | |
| 2736 | upper = edge->node[UPPER]; |
| 2737 | root = select_reloc_root(trans, rc, upper, edges); |
| 2738 | BUG_ON(!root); |
| 2739 | |
| 2740 | if (upper->eb && !upper->locked) { |
| 2741 | if (!lowest) { |
| 2742 | ret = btrfs_bin_search(upper->eb, key, |
| 2743 | upper->level, &slot); |
| 2744 | BUG_ON(ret); |
| 2745 | bytenr = btrfs_node_blockptr(upper->eb, slot); |
| 2746 | if (node->eb->start == bytenr) |
| 2747 | goto next; |
| 2748 | } |
| 2749 | drop_node_buffer(upper); |
| 2750 | } |
| 2751 | |
| 2752 | if (!upper->eb) { |
| 2753 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
| 2754 | if (ret) { |
| 2755 | if (ret < 0) |
| 2756 | err = ret; |
| 2757 | else |
| 2758 | err = -ENOENT; |
| 2759 | |
| 2760 | btrfs_release_path(path); |
| 2761 | break; |
| 2762 | } |
| 2763 | |
| 2764 | if (!upper->eb) { |
| 2765 | upper->eb = path->nodes[upper->level]; |
| 2766 | path->nodes[upper->level] = NULL; |
| 2767 | } else { |
| 2768 | BUG_ON(upper->eb != path->nodes[upper->level]); |
| 2769 | } |
| 2770 | |
| 2771 | upper->locked = 1; |
| 2772 | path->locks[upper->level] = 0; |
| 2773 | |
| 2774 | slot = path->slots[upper->level]; |
| 2775 | btrfs_release_path(path); |
| 2776 | } else { |
| 2777 | ret = btrfs_bin_search(upper->eb, key, upper->level, |
| 2778 | &slot); |
| 2779 | BUG_ON(ret); |
| 2780 | } |
| 2781 | |
| 2782 | bytenr = btrfs_node_blockptr(upper->eb, slot); |
| 2783 | if (lowest) { |
| 2784 | if (bytenr != node->bytenr) { |
| 2785 | btrfs_err(root->fs_info, |
| 2786 | "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu", |
| 2787 | bytenr, node->bytenr, slot, |
| 2788 | upper->eb->start); |
| 2789 | err = -EIO; |
| 2790 | goto next; |
| 2791 | } |
| 2792 | } else { |
| 2793 | if (node->eb->start == bytenr) |
| 2794 | goto next; |
| 2795 | } |
| 2796 | |
| 2797 | blocksize = root->fs_info->nodesize; |
| 2798 | generation = btrfs_node_ptr_generation(upper->eb, slot); |
| 2799 | eb = read_tree_block(fs_info, bytenr, generation); |
| 2800 | if (IS_ERR(eb)) { |
| 2801 | err = PTR_ERR(eb); |
| 2802 | goto next; |
| 2803 | } else if (!extent_buffer_uptodate(eb)) { |
| 2804 | free_extent_buffer(eb); |
| 2805 | err = -EIO; |
| 2806 | goto next; |
| 2807 | } |
| 2808 | btrfs_tree_lock(eb); |
| 2809 | btrfs_set_lock_blocking(eb); |
| 2810 | |
| 2811 | if (!node->eb) { |
| 2812 | ret = btrfs_cow_block(trans, root, eb, upper->eb, |
| 2813 | slot, &eb); |
| 2814 | btrfs_tree_unlock(eb); |
| 2815 | free_extent_buffer(eb); |
| 2816 | if (ret < 0) { |
| 2817 | err = ret; |
| 2818 | goto next; |
| 2819 | } |
| 2820 | BUG_ON(node->eb != eb); |
| 2821 | } else { |
| 2822 | btrfs_set_node_blockptr(upper->eb, slot, |
| 2823 | node->eb->start); |
| 2824 | btrfs_set_node_ptr_generation(upper->eb, slot, |
| 2825 | trans->transid); |
| 2826 | btrfs_mark_buffer_dirty(upper->eb); |
| 2827 | |
| 2828 | ret = btrfs_inc_extent_ref(trans, root->fs_info, |
| 2829 | node->eb->start, blocksize, |
| 2830 | upper->eb->start, |
| 2831 | btrfs_header_owner(upper->eb), |
| 2832 | node->level, 0); |
| 2833 | BUG_ON(ret); |
| 2834 | |
| 2835 | ret = btrfs_drop_subtree(trans, root, eb, upper->eb); |
| 2836 | BUG_ON(ret); |
| 2837 | } |
| 2838 | next: |
| 2839 | if (!upper->pending) |
| 2840 | drop_node_buffer(upper); |
| 2841 | else |
| 2842 | unlock_node_buffer(upper); |
| 2843 | if (err) |
| 2844 | break; |
| 2845 | } |
| 2846 | |
| 2847 | if (!err && node->pending) { |
| 2848 | drop_node_buffer(node); |
| 2849 | list_move_tail(&node->list, &rc->backref_cache.changed); |
| 2850 | node->pending = 0; |
| 2851 | } |
| 2852 | |
| 2853 | path->lowest_level = 0; |
| 2854 | BUG_ON(err == -ENOSPC); |
| 2855 | return err; |
| 2856 | } |
| 2857 | |
| 2858 | static int link_to_upper(struct btrfs_trans_handle *trans, |
| 2859 | struct reloc_control *rc, |
| 2860 | struct backref_node *node, |
| 2861 | struct btrfs_path *path) |
| 2862 | { |
| 2863 | struct btrfs_key key; |
| 2864 | |
| 2865 | btrfs_node_key_to_cpu(node->eb, &key, 0); |
| 2866 | return do_relocation(trans, rc, node, &key, path, 0); |
| 2867 | } |
| 2868 | |
| 2869 | static int finish_pending_nodes(struct btrfs_trans_handle *trans, |
| 2870 | struct reloc_control *rc, |
| 2871 | struct btrfs_path *path, int err) |
| 2872 | { |
| 2873 | LIST_HEAD(list); |
| 2874 | struct backref_cache *cache = &rc->backref_cache; |
| 2875 | struct backref_node *node; |
| 2876 | int level; |
| 2877 | int ret; |
| 2878 | |
| 2879 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
| 2880 | while (!list_empty(&cache->pending[level])) { |
| 2881 | node = list_entry(cache->pending[level].next, |
| 2882 | struct backref_node, list); |
| 2883 | list_move_tail(&node->list, &list); |
| 2884 | BUG_ON(!node->pending); |
| 2885 | |
| 2886 | if (!err) { |
| 2887 | ret = link_to_upper(trans, rc, node, path); |
| 2888 | if (ret < 0) |
| 2889 | err = ret; |
| 2890 | } |
| 2891 | } |
| 2892 | list_splice_init(&list, &cache->pending[level]); |
| 2893 | } |
| 2894 | return err; |
| 2895 | } |
| 2896 | |
| 2897 | static void mark_block_processed(struct reloc_control *rc, |
| 2898 | u64 bytenr, u32 blocksize) |
| 2899 | { |
| 2900 | set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1, |
| 2901 | EXTENT_DIRTY); |
| 2902 | } |
| 2903 | |
| 2904 | static void __mark_block_processed(struct reloc_control *rc, |
| 2905 | struct backref_node *node) |
| 2906 | { |
| 2907 | u32 blocksize; |
| 2908 | if (node->level == 0 || |
| 2909 | in_block_group(node->bytenr, rc->block_group)) { |
| 2910 | blocksize = rc->extent_root->fs_info->nodesize; |
| 2911 | mark_block_processed(rc, node->bytenr, blocksize); |
| 2912 | } |
| 2913 | node->processed = 1; |
| 2914 | } |
| 2915 | |
| 2916 | /* |
| 2917 | * mark a block and all blocks directly/indirectly reference the block |
| 2918 | * as processed. |
| 2919 | */ |
| 2920 | static void update_processed_blocks(struct reloc_control *rc, |
| 2921 | struct backref_node *node) |
| 2922 | { |
| 2923 | struct backref_node *next = node; |
| 2924 | struct backref_edge *edge; |
| 2925 | struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2926 | int index = 0; |
| 2927 | |
| 2928 | while (next) { |
| 2929 | cond_resched(); |
| 2930 | while (1) { |
| 2931 | if (next->processed) |
| 2932 | break; |
| 2933 | |
| 2934 | __mark_block_processed(rc, next); |
| 2935 | |
| 2936 | if (list_empty(&next->upper)) |
| 2937 | break; |
| 2938 | |
| 2939 | edge = list_entry(next->upper.next, |
| 2940 | struct backref_edge, list[LOWER]); |
| 2941 | edges[index++] = edge; |
| 2942 | next = edge->node[UPPER]; |
| 2943 | } |
| 2944 | next = walk_down_backref(edges, &index); |
| 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | static int tree_block_processed(u64 bytenr, struct reloc_control *rc) |
| 2949 | { |
| 2950 | u32 blocksize = rc->extent_root->fs_info->nodesize; |
| 2951 | |
| 2952 | if (test_range_bit(&rc->processed_blocks, bytenr, |
| 2953 | bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL)) |
| 2954 | return 1; |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
| 2958 | static int get_tree_block_key(struct btrfs_fs_info *fs_info, |
| 2959 | struct tree_block *block) |
| 2960 | { |
| 2961 | struct extent_buffer *eb; |
| 2962 | |
| 2963 | BUG_ON(block->key_ready); |
| 2964 | eb = read_tree_block(fs_info, block->bytenr, block->key.offset); |
| 2965 | if (IS_ERR(eb)) { |
| 2966 | return PTR_ERR(eb); |
| 2967 | } else if (!extent_buffer_uptodate(eb)) { |
| 2968 | free_extent_buffer(eb); |
| 2969 | return -EIO; |
| 2970 | } |
| 2971 | WARN_ON(btrfs_header_level(eb) != block->level); |
| 2972 | if (block->level == 0) |
| 2973 | btrfs_item_key_to_cpu(eb, &block->key, 0); |
| 2974 | else |
| 2975 | btrfs_node_key_to_cpu(eb, &block->key, 0); |
| 2976 | free_extent_buffer(eb); |
| 2977 | block->key_ready = 1; |
| 2978 | return 0; |
| 2979 | } |
| 2980 | |
| 2981 | /* |
| 2982 | * helper function to relocate a tree block |
| 2983 | */ |
| 2984 | static int relocate_tree_block(struct btrfs_trans_handle *trans, |
| 2985 | struct reloc_control *rc, |
| 2986 | struct backref_node *node, |
| 2987 | struct btrfs_key *key, |
| 2988 | struct btrfs_path *path) |
| 2989 | { |
| 2990 | struct btrfs_root *root; |
| 2991 | int ret = 0; |
| 2992 | |
| 2993 | if (!node) |
| 2994 | return 0; |
| 2995 | |
| 2996 | BUG_ON(node->processed); |
| 2997 | root = select_one_root(node); |
| 2998 | if (root == ERR_PTR(-ENOENT)) { |
| 2999 | update_processed_blocks(rc, node); |
| 3000 | goto out; |
| 3001 | } |
| 3002 | |
| 3003 | if (!root || test_bit(BTRFS_ROOT_REF_COWS, &root->state)) { |
| 3004 | ret = reserve_metadata_space(trans, rc, node); |
| 3005 | if (ret) |
| 3006 | goto out; |
| 3007 | } |
| 3008 | |
| 3009 | if (root) { |
| 3010 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) { |
| 3011 | BUG_ON(node->new_bytenr); |
| 3012 | BUG_ON(!list_empty(&node->list)); |
| 3013 | btrfs_record_root_in_trans(trans, root); |
| 3014 | root = root->reloc_root; |
| 3015 | node->new_bytenr = root->node->start; |
| 3016 | node->root = root; |
| 3017 | list_add_tail(&node->list, &rc->backref_cache.changed); |
| 3018 | } else { |
| 3019 | path->lowest_level = node->level; |
| 3020 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
| 3021 | btrfs_release_path(path); |
| 3022 | if (ret > 0) |
| 3023 | ret = 0; |
| 3024 | } |
| 3025 | if (!ret) |
| 3026 | update_processed_blocks(rc, node); |
| 3027 | } else { |
| 3028 | ret = do_relocation(trans, rc, node, key, path, 1); |
| 3029 | } |
| 3030 | out: |
| 3031 | if (ret || node->level == 0 || node->cowonly) |
| 3032 | remove_backref_node(&rc->backref_cache, node); |
| 3033 | return ret; |
| 3034 | } |
| 3035 | |
| 3036 | /* |
| 3037 | * relocate a list of blocks |
| 3038 | */ |
| 3039 | static noinline_for_stack |
| 3040 | int relocate_tree_blocks(struct btrfs_trans_handle *trans, |
| 3041 | struct reloc_control *rc, struct rb_root *blocks) |
| 3042 | { |
| 3043 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3044 | struct backref_node *node; |
| 3045 | struct btrfs_path *path; |
| 3046 | struct tree_block *block; |
| 3047 | struct rb_node *rb_node; |
| 3048 | int ret; |
| 3049 | int err = 0; |
| 3050 | |
| 3051 | path = btrfs_alloc_path(); |
| 3052 | if (!path) { |
| 3053 | err = -ENOMEM; |
| 3054 | goto out_free_blocks; |
| 3055 | } |
| 3056 | |
| 3057 | rb_node = rb_first(blocks); |
| 3058 | while (rb_node) { |
| 3059 | block = rb_entry(rb_node, struct tree_block, rb_node); |
| 3060 | if (!block->key_ready) |
| 3061 | readahead_tree_block(fs_info, block->bytenr); |
| 3062 | rb_node = rb_next(rb_node); |
| 3063 | } |
| 3064 | |
| 3065 | rb_node = rb_first(blocks); |
| 3066 | while (rb_node) { |
| 3067 | block = rb_entry(rb_node, struct tree_block, rb_node); |
| 3068 | if (!block->key_ready) { |
| 3069 | err = get_tree_block_key(fs_info, block); |
| 3070 | if (err) |
| 3071 | goto out_free_path; |
| 3072 | } |
| 3073 | rb_node = rb_next(rb_node); |
| 3074 | } |
| 3075 | |
| 3076 | rb_node = rb_first(blocks); |
| 3077 | while (rb_node) { |
| 3078 | block = rb_entry(rb_node, struct tree_block, rb_node); |
| 3079 | |
| 3080 | node = build_backref_tree(rc, &block->key, |
| 3081 | block->level, block->bytenr); |
| 3082 | if (IS_ERR(node)) { |
| 3083 | err = PTR_ERR(node); |
| 3084 | goto out; |
| 3085 | } |
| 3086 | |
| 3087 | ret = relocate_tree_block(trans, rc, node, &block->key, |
| 3088 | path); |
| 3089 | if (ret < 0) { |
| 3090 | if (ret != -EAGAIN || rb_node == rb_first(blocks)) |
| 3091 | err = ret; |
| 3092 | goto out; |
| 3093 | } |
| 3094 | rb_node = rb_next(rb_node); |
| 3095 | } |
| 3096 | out: |
| 3097 | err = finish_pending_nodes(trans, rc, path, err); |
| 3098 | |
| 3099 | out_free_path: |
| 3100 | btrfs_free_path(path); |
| 3101 | out_free_blocks: |
| 3102 | free_block_list(blocks); |
| 3103 | return err; |
| 3104 | } |
| 3105 | |
| 3106 | static noinline_for_stack |
| 3107 | int prealloc_file_extent_cluster(struct inode *inode, |
| 3108 | struct file_extent_cluster *cluster) |
| 3109 | { |
| 3110 | u64 alloc_hint = 0; |
| 3111 | u64 start; |
| 3112 | u64 end; |
| 3113 | u64 offset = BTRFS_I(inode)->index_cnt; |
| 3114 | u64 num_bytes; |
| 3115 | int nr = 0; |
| 3116 | int ret = 0; |
| 3117 | u64 prealloc_start = cluster->start - offset; |
| 3118 | u64 prealloc_end = cluster->end - offset; |
| 3119 | u64 cur_offset; |
| 3120 | struct extent_changeset *data_reserved = NULL; |
| 3121 | |
| 3122 | BUG_ON(cluster->start != cluster->boundary[0]); |
| 3123 | inode_lock(inode); |
| 3124 | |
| 3125 | ret = btrfs_check_data_free_space(inode, &data_reserved, prealloc_start, |
| 3126 | prealloc_end + 1 - prealloc_start); |
| 3127 | if (ret) |
| 3128 | goto out; |
| 3129 | |
| 3130 | cur_offset = prealloc_start; |
| 3131 | while (nr < cluster->nr) { |
| 3132 | start = cluster->boundary[nr] - offset; |
| 3133 | if (nr + 1 < cluster->nr) |
| 3134 | end = cluster->boundary[nr + 1] - 1 - offset; |
| 3135 | else |
| 3136 | end = cluster->end - offset; |
| 3137 | |
| 3138 | lock_extent(&BTRFS_I(inode)->io_tree, start, end); |
| 3139 | num_bytes = end + 1 - start; |
| 3140 | if (cur_offset < start) |
| 3141 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 3142 | cur_offset, start - cur_offset); |
| 3143 | ret = btrfs_prealloc_file_range(inode, 0, start, |
| 3144 | num_bytes, num_bytes, |
| 3145 | end + 1, &alloc_hint); |
| 3146 | cur_offset = end + 1; |
| 3147 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
| 3148 | if (ret) |
| 3149 | break; |
| 3150 | nr++; |
| 3151 | } |
| 3152 | if (cur_offset < prealloc_end) |
| 3153 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 3154 | cur_offset, prealloc_end + 1 - cur_offset); |
| 3155 | out: |
| 3156 | inode_unlock(inode); |
| 3157 | extent_changeset_free(data_reserved); |
| 3158 | return ret; |
| 3159 | } |
| 3160 | |
| 3161 | static noinline_for_stack |
| 3162 | int setup_extent_mapping(struct inode *inode, u64 start, u64 end, |
| 3163 | u64 block_start) |
| 3164 | { |
| 3165 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 3166 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 3167 | struct extent_map *em; |
| 3168 | int ret = 0; |
| 3169 | |
| 3170 | em = alloc_extent_map(); |
| 3171 | if (!em) |
| 3172 | return -ENOMEM; |
| 3173 | |
| 3174 | em->start = start; |
| 3175 | em->len = end + 1 - start; |
| 3176 | em->block_len = em->len; |
| 3177 | em->block_start = block_start; |
| 3178 | em->bdev = fs_info->fs_devices->latest_bdev; |
| 3179 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 3180 | |
| 3181 | lock_extent(&BTRFS_I(inode)->io_tree, start, end); |
| 3182 | while (1) { |
| 3183 | write_lock(&em_tree->lock); |
| 3184 | ret = add_extent_mapping(em_tree, em, 0); |
| 3185 | write_unlock(&em_tree->lock); |
| 3186 | if (ret != -EEXIST) { |
| 3187 | free_extent_map(em); |
| 3188 | break; |
| 3189 | } |
| 3190 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); |
| 3191 | } |
| 3192 | unlock_extent(&BTRFS_I(inode)->io_tree, start, end); |
| 3193 | return ret; |
| 3194 | } |
| 3195 | |
| 3196 | static int relocate_file_extent_cluster(struct inode *inode, |
| 3197 | struct file_extent_cluster *cluster) |
| 3198 | { |
| 3199 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 3200 | u64 page_start; |
| 3201 | u64 page_end; |
| 3202 | u64 offset = BTRFS_I(inode)->index_cnt; |
| 3203 | unsigned long index; |
| 3204 | unsigned long last_index; |
| 3205 | struct page *page; |
| 3206 | struct file_ra_state *ra; |
| 3207 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
| 3208 | int nr = 0; |
| 3209 | int ret = 0; |
| 3210 | |
| 3211 | if (!cluster->nr) |
| 3212 | return 0; |
| 3213 | |
| 3214 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
| 3215 | if (!ra) |
| 3216 | return -ENOMEM; |
| 3217 | |
| 3218 | ret = prealloc_file_extent_cluster(inode, cluster); |
| 3219 | if (ret) |
| 3220 | goto out; |
| 3221 | |
| 3222 | file_ra_state_init(ra, inode->i_mapping); |
| 3223 | |
| 3224 | ret = setup_extent_mapping(inode, cluster->start - offset, |
| 3225 | cluster->end - offset, cluster->start); |
| 3226 | if (ret) |
| 3227 | goto out; |
| 3228 | |
| 3229 | index = (cluster->start - offset) >> PAGE_SHIFT; |
| 3230 | last_index = (cluster->end - offset) >> PAGE_SHIFT; |
| 3231 | while (index <= last_index) { |
| 3232 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), |
| 3233 | PAGE_SIZE); |
| 3234 | if (ret) |
| 3235 | goto out; |
| 3236 | |
| 3237 | page = find_lock_page(inode->i_mapping, index); |
| 3238 | if (!page) { |
| 3239 | page_cache_sync_readahead(inode->i_mapping, |
| 3240 | ra, NULL, index, |
| 3241 | last_index + 1 - index); |
| 3242 | page = find_or_create_page(inode->i_mapping, index, |
| 3243 | mask); |
| 3244 | if (!page) { |
| 3245 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 3246 | PAGE_SIZE); |
| 3247 | ret = -ENOMEM; |
| 3248 | goto out; |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | if (PageReadahead(page)) { |
| 3253 | page_cache_async_readahead(inode->i_mapping, |
| 3254 | ra, NULL, page, index, |
| 3255 | last_index + 1 - index); |
| 3256 | } |
| 3257 | |
| 3258 | if (!PageUptodate(page)) { |
| 3259 | btrfs_readpage(NULL, page); |
| 3260 | lock_page(page); |
| 3261 | if (!PageUptodate(page)) { |
| 3262 | unlock_page(page); |
| 3263 | put_page(page); |
| 3264 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 3265 | PAGE_SIZE); |
| 3266 | ret = -EIO; |
| 3267 | goto out; |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | page_start = page_offset(page); |
| 3272 | page_end = page_start + PAGE_SIZE - 1; |
| 3273 | |
| 3274 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end); |
| 3275 | |
| 3276 | set_page_extent_mapped(page); |
| 3277 | |
| 3278 | if (nr < cluster->nr && |
| 3279 | page_start + offset == cluster->boundary[nr]) { |
| 3280 | set_extent_bits(&BTRFS_I(inode)->io_tree, |
| 3281 | page_start, page_end, |
| 3282 | EXTENT_BOUNDARY); |
| 3283 | nr++; |
| 3284 | } |
| 3285 | |
| 3286 | btrfs_set_extent_delalloc(inode, page_start, page_end, NULL, 0); |
| 3287 | set_page_dirty(page); |
| 3288 | |
| 3289 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 3290 | page_start, page_end); |
| 3291 | unlock_page(page); |
| 3292 | put_page(page); |
| 3293 | |
| 3294 | index++; |
| 3295 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 3296 | btrfs_throttle(fs_info); |
| 3297 | } |
| 3298 | WARN_ON(nr != cluster->nr); |
| 3299 | out: |
| 3300 | kfree(ra); |
| 3301 | return ret; |
| 3302 | } |
| 3303 | |
| 3304 | static noinline_for_stack |
| 3305 | int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key, |
| 3306 | struct file_extent_cluster *cluster) |
| 3307 | { |
| 3308 | int ret; |
| 3309 | |
| 3310 | if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) { |
| 3311 | ret = relocate_file_extent_cluster(inode, cluster); |
| 3312 | if (ret) |
| 3313 | return ret; |
| 3314 | cluster->nr = 0; |
| 3315 | } |
| 3316 | |
| 3317 | if (!cluster->nr) |
| 3318 | cluster->start = extent_key->objectid; |
| 3319 | else |
| 3320 | BUG_ON(cluster->nr >= MAX_EXTENTS); |
| 3321 | cluster->end = extent_key->objectid + extent_key->offset - 1; |
| 3322 | cluster->boundary[cluster->nr] = extent_key->objectid; |
| 3323 | cluster->nr++; |
| 3324 | |
| 3325 | if (cluster->nr >= MAX_EXTENTS) { |
| 3326 | ret = relocate_file_extent_cluster(inode, cluster); |
| 3327 | if (ret) |
| 3328 | return ret; |
| 3329 | cluster->nr = 0; |
| 3330 | } |
| 3331 | return 0; |
| 3332 | } |
| 3333 | |
| 3334 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3335 | static int get_ref_objectid_v0(struct reloc_control *rc, |
| 3336 | struct btrfs_path *path, |
| 3337 | struct btrfs_key *extent_key, |
| 3338 | u64 *ref_objectid, int *path_change) |
| 3339 | { |
| 3340 | struct btrfs_key key; |
| 3341 | struct extent_buffer *leaf; |
| 3342 | struct btrfs_extent_ref_v0 *ref0; |
| 3343 | int ret; |
| 3344 | int slot; |
| 3345 | |
| 3346 | leaf = path->nodes[0]; |
| 3347 | slot = path->slots[0]; |
| 3348 | while (1) { |
| 3349 | if (slot >= btrfs_header_nritems(leaf)) { |
| 3350 | ret = btrfs_next_leaf(rc->extent_root, path); |
| 3351 | if (ret < 0) |
| 3352 | return ret; |
| 3353 | BUG_ON(ret > 0); |
| 3354 | leaf = path->nodes[0]; |
| 3355 | slot = path->slots[0]; |
| 3356 | if (path_change) |
| 3357 | *path_change = 1; |
| 3358 | } |
| 3359 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 3360 | if (key.objectid != extent_key->objectid) |
| 3361 | return -ENOENT; |
| 3362 | |
| 3363 | if (key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 3364 | slot++; |
| 3365 | continue; |
| 3366 | } |
| 3367 | ref0 = btrfs_item_ptr(leaf, slot, |
| 3368 | struct btrfs_extent_ref_v0); |
| 3369 | *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0); |
| 3370 | break; |
| 3371 | } |
| 3372 | return 0; |
| 3373 | } |
| 3374 | #endif |
| 3375 | |
| 3376 | /* |
| 3377 | * helper to add a tree block to the list. |
| 3378 | * the major work is getting the generation and level of the block |
| 3379 | */ |
| 3380 | static int add_tree_block(struct reloc_control *rc, |
| 3381 | struct btrfs_key *extent_key, |
| 3382 | struct btrfs_path *path, |
| 3383 | struct rb_root *blocks) |
| 3384 | { |
| 3385 | struct extent_buffer *eb; |
| 3386 | struct btrfs_extent_item *ei; |
| 3387 | struct btrfs_tree_block_info *bi; |
| 3388 | struct tree_block *block; |
| 3389 | struct rb_node *rb_node; |
| 3390 | u32 item_size; |
| 3391 | int level = -1; |
| 3392 | u64 generation; |
| 3393 | |
| 3394 | eb = path->nodes[0]; |
| 3395 | item_size = btrfs_item_size_nr(eb, path->slots[0]); |
| 3396 | |
| 3397 | if (extent_key->type == BTRFS_METADATA_ITEM_KEY || |
| 3398 | item_size >= sizeof(*ei) + sizeof(*bi)) { |
| 3399 | ei = btrfs_item_ptr(eb, path->slots[0], |
| 3400 | struct btrfs_extent_item); |
| 3401 | if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) { |
| 3402 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3403 | level = btrfs_tree_block_level(eb, bi); |
| 3404 | } else { |
| 3405 | level = (int)extent_key->offset; |
| 3406 | } |
| 3407 | generation = btrfs_extent_generation(eb, ei); |
| 3408 | } else { |
| 3409 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3410 | u64 ref_owner; |
| 3411 | int ret; |
| 3412 | |
| 3413 | BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 3414 | ret = get_ref_objectid_v0(rc, path, extent_key, |
| 3415 | &ref_owner, NULL); |
| 3416 | if (ret < 0) |
| 3417 | return ret; |
| 3418 | BUG_ON(ref_owner >= BTRFS_MAX_LEVEL); |
| 3419 | level = (int)ref_owner; |
| 3420 | /* FIXME: get real generation */ |
| 3421 | generation = 0; |
| 3422 | #else |
| 3423 | BUG(); |
| 3424 | #endif |
| 3425 | } |
| 3426 | |
| 3427 | btrfs_release_path(path); |
| 3428 | |
| 3429 | BUG_ON(level == -1); |
| 3430 | |
| 3431 | block = kmalloc(sizeof(*block), GFP_NOFS); |
| 3432 | if (!block) |
| 3433 | return -ENOMEM; |
| 3434 | |
| 3435 | block->bytenr = extent_key->objectid; |
| 3436 | block->key.objectid = rc->extent_root->fs_info->nodesize; |
| 3437 | block->key.offset = generation; |
| 3438 | block->level = level; |
| 3439 | block->key_ready = 0; |
| 3440 | |
| 3441 | rb_node = tree_insert(blocks, block->bytenr, &block->rb_node); |
| 3442 | if (rb_node) |
| 3443 | backref_tree_panic(rb_node, -EEXIST, block->bytenr); |
| 3444 | |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
| 3448 | /* |
| 3449 | * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY |
| 3450 | */ |
| 3451 | static int __add_tree_block(struct reloc_control *rc, |
| 3452 | u64 bytenr, u32 blocksize, |
| 3453 | struct rb_root *blocks) |
| 3454 | { |
| 3455 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3456 | struct btrfs_path *path; |
| 3457 | struct btrfs_key key; |
| 3458 | int ret; |
| 3459 | bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
| 3460 | |
| 3461 | if (tree_block_processed(bytenr, rc)) |
| 3462 | return 0; |
| 3463 | |
| 3464 | if (tree_search(blocks, bytenr)) |
| 3465 | return 0; |
| 3466 | |
| 3467 | path = btrfs_alloc_path(); |
| 3468 | if (!path) |
| 3469 | return -ENOMEM; |
| 3470 | again: |
| 3471 | key.objectid = bytenr; |
| 3472 | if (skinny) { |
| 3473 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 3474 | key.offset = (u64)-1; |
| 3475 | } else { |
| 3476 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3477 | key.offset = blocksize; |
| 3478 | } |
| 3479 | |
| 3480 | path->search_commit_root = 1; |
| 3481 | path->skip_locking = 1; |
| 3482 | ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0); |
| 3483 | if (ret < 0) |
| 3484 | goto out; |
| 3485 | |
| 3486 | if (ret > 0 && skinny) { |
| 3487 | if (path->slots[0]) { |
| 3488 | path->slots[0]--; |
| 3489 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3490 | path->slots[0]); |
| 3491 | if (key.objectid == bytenr && |
| 3492 | (key.type == BTRFS_METADATA_ITEM_KEY || |
| 3493 | (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3494 | key.offset == blocksize))) |
| 3495 | ret = 0; |
| 3496 | } |
| 3497 | |
| 3498 | if (ret) { |
| 3499 | skinny = false; |
| 3500 | btrfs_release_path(path); |
| 3501 | goto again; |
| 3502 | } |
| 3503 | } |
| 3504 | if (ret) { |
| 3505 | ASSERT(ret == 1); |
| 3506 | btrfs_print_leaf(path->nodes[0]); |
| 3507 | btrfs_err(fs_info, |
| 3508 | "tree block extent item (%llu) is not found in extent tree", |
| 3509 | bytenr); |
| 3510 | WARN_ON(1); |
| 3511 | ret = -EINVAL; |
| 3512 | goto out; |
| 3513 | } |
| 3514 | |
| 3515 | ret = add_tree_block(rc, &key, path, blocks); |
| 3516 | out: |
| 3517 | btrfs_free_path(path); |
| 3518 | return ret; |
| 3519 | } |
| 3520 | |
| 3521 | /* |
| 3522 | * helper to check if the block use full backrefs for pointers in it |
| 3523 | */ |
| 3524 | static int block_use_full_backref(struct reloc_control *rc, |
| 3525 | struct extent_buffer *eb) |
| 3526 | { |
| 3527 | u64 flags; |
| 3528 | int ret; |
| 3529 | |
| 3530 | if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) || |
| 3531 | btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV) |
| 3532 | return 1; |
| 3533 | |
| 3534 | ret = btrfs_lookup_extent_info(NULL, rc->extent_root->fs_info, |
| 3535 | eb->start, btrfs_header_level(eb), 1, |
| 3536 | NULL, &flags); |
| 3537 | BUG_ON(ret); |
| 3538 | |
| 3539 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 3540 | ret = 1; |
| 3541 | else |
| 3542 | ret = 0; |
| 3543 | return ret; |
| 3544 | } |
| 3545 | |
| 3546 | static int delete_block_group_cache(struct btrfs_fs_info *fs_info, |
| 3547 | struct btrfs_block_group_cache *block_group, |
| 3548 | struct inode *inode, |
| 3549 | u64 ino) |
| 3550 | { |
| 3551 | struct btrfs_key key; |
| 3552 | struct btrfs_root *root = fs_info->tree_root; |
| 3553 | struct btrfs_trans_handle *trans; |
| 3554 | int ret = 0; |
| 3555 | |
| 3556 | if (inode) |
| 3557 | goto truncate; |
| 3558 | |
| 3559 | key.objectid = ino; |
| 3560 | key.type = BTRFS_INODE_ITEM_KEY; |
| 3561 | key.offset = 0; |
| 3562 | |
| 3563 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 3564 | if (IS_ERR(inode) || is_bad_inode(inode)) { |
| 3565 | if (!IS_ERR(inode)) |
| 3566 | iput(inode); |
| 3567 | return -ENOENT; |
| 3568 | } |
| 3569 | |
| 3570 | truncate: |
| 3571 | ret = btrfs_check_trunc_cache_free_space(fs_info, |
| 3572 | &fs_info->global_block_rsv); |
| 3573 | if (ret) |
| 3574 | goto out; |
| 3575 | |
| 3576 | trans = btrfs_join_transaction(root); |
| 3577 | if (IS_ERR(trans)) { |
| 3578 | ret = PTR_ERR(trans); |
| 3579 | goto out; |
| 3580 | } |
| 3581 | |
| 3582 | ret = btrfs_truncate_free_space_cache(trans, block_group, inode); |
| 3583 | |
| 3584 | btrfs_end_transaction(trans); |
| 3585 | btrfs_btree_balance_dirty(fs_info); |
| 3586 | out: |
| 3587 | iput(inode); |
| 3588 | return ret; |
| 3589 | } |
| 3590 | |
| 3591 | /* |
| 3592 | * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY |
| 3593 | * this function scans fs tree to find blocks reference the data extent |
| 3594 | */ |
| 3595 | static int find_data_references(struct reloc_control *rc, |
| 3596 | struct btrfs_key *extent_key, |
| 3597 | struct extent_buffer *leaf, |
| 3598 | struct btrfs_extent_data_ref *ref, |
| 3599 | struct rb_root *blocks) |
| 3600 | { |
| 3601 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3602 | struct btrfs_path *path; |
| 3603 | struct tree_block *block; |
| 3604 | struct btrfs_root *root; |
| 3605 | struct btrfs_file_extent_item *fi; |
| 3606 | struct rb_node *rb_node; |
| 3607 | struct btrfs_key key; |
| 3608 | u64 ref_root; |
| 3609 | u64 ref_objectid; |
| 3610 | u64 ref_offset; |
| 3611 | u32 ref_count; |
| 3612 | u32 nritems; |
| 3613 | int err = 0; |
| 3614 | int added = 0; |
| 3615 | int counted; |
| 3616 | int ret; |
| 3617 | |
| 3618 | ref_root = btrfs_extent_data_ref_root(leaf, ref); |
| 3619 | ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref); |
| 3620 | ref_offset = btrfs_extent_data_ref_offset(leaf, ref); |
| 3621 | ref_count = btrfs_extent_data_ref_count(leaf, ref); |
| 3622 | |
| 3623 | /* |
| 3624 | * This is an extent belonging to the free space cache, lets just delete |
| 3625 | * it and redo the search. |
| 3626 | */ |
| 3627 | if (ref_root == BTRFS_ROOT_TREE_OBJECTID) { |
| 3628 | ret = delete_block_group_cache(fs_info, rc->block_group, |
| 3629 | NULL, ref_objectid); |
| 3630 | if (ret != -ENOENT) |
| 3631 | return ret; |
| 3632 | ret = 0; |
| 3633 | } |
| 3634 | |
| 3635 | path = btrfs_alloc_path(); |
| 3636 | if (!path) |
| 3637 | return -ENOMEM; |
| 3638 | path->reada = READA_FORWARD; |
| 3639 | |
| 3640 | root = read_fs_root(fs_info, ref_root); |
| 3641 | if (IS_ERR(root)) { |
| 3642 | err = PTR_ERR(root); |
| 3643 | goto out; |
| 3644 | } |
| 3645 | |
| 3646 | key.objectid = ref_objectid; |
| 3647 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 3648 | if (ref_offset > ((u64)-1 << 32)) |
| 3649 | key.offset = 0; |
| 3650 | else |
| 3651 | key.offset = ref_offset; |
| 3652 | |
| 3653 | path->search_commit_root = 1; |
| 3654 | path->skip_locking = 1; |
| 3655 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3656 | if (ret < 0) { |
| 3657 | err = ret; |
| 3658 | goto out; |
| 3659 | } |
| 3660 | |
| 3661 | leaf = path->nodes[0]; |
| 3662 | nritems = btrfs_header_nritems(leaf); |
| 3663 | /* |
| 3664 | * the references in tree blocks that use full backrefs |
| 3665 | * are not counted in |
| 3666 | */ |
| 3667 | if (block_use_full_backref(rc, leaf)) |
| 3668 | counted = 0; |
| 3669 | else |
| 3670 | counted = 1; |
| 3671 | rb_node = tree_search(blocks, leaf->start); |
| 3672 | if (rb_node) { |
| 3673 | if (counted) |
| 3674 | added = 1; |
| 3675 | else |
| 3676 | path->slots[0] = nritems; |
| 3677 | } |
| 3678 | |
| 3679 | while (ref_count > 0) { |
| 3680 | while (path->slots[0] >= nritems) { |
| 3681 | ret = btrfs_next_leaf(root, path); |
| 3682 | if (ret < 0) { |
| 3683 | err = ret; |
| 3684 | goto out; |
| 3685 | } |
| 3686 | if (WARN_ON(ret > 0)) |
| 3687 | goto out; |
| 3688 | |
| 3689 | leaf = path->nodes[0]; |
| 3690 | nritems = btrfs_header_nritems(leaf); |
| 3691 | added = 0; |
| 3692 | |
| 3693 | if (block_use_full_backref(rc, leaf)) |
| 3694 | counted = 0; |
| 3695 | else |
| 3696 | counted = 1; |
| 3697 | rb_node = tree_search(blocks, leaf->start); |
| 3698 | if (rb_node) { |
| 3699 | if (counted) |
| 3700 | added = 1; |
| 3701 | else |
| 3702 | path->slots[0] = nritems; |
| 3703 | } |
| 3704 | } |
| 3705 | |
| 3706 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 3707 | if (WARN_ON(key.objectid != ref_objectid || |
| 3708 | key.type != BTRFS_EXTENT_DATA_KEY)) |
| 3709 | break; |
| 3710 | |
| 3711 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3712 | struct btrfs_file_extent_item); |
| 3713 | |
| 3714 | if (btrfs_file_extent_type(leaf, fi) == |
| 3715 | BTRFS_FILE_EXTENT_INLINE) |
| 3716 | goto next; |
| 3717 | |
| 3718 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 3719 | extent_key->objectid) |
| 3720 | goto next; |
| 3721 | |
| 3722 | key.offset -= btrfs_file_extent_offset(leaf, fi); |
| 3723 | if (key.offset != ref_offset) |
| 3724 | goto next; |
| 3725 | |
| 3726 | if (counted) |
| 3727 | ref_count--; |
| 3728 | if (added) |
| 3729 | goto next; |
| 3730 | |
| 3731 | if (!tree_block_processed(leaf->start, rc)) { |
| 3732 | block = kmalloc(sizeof(*block), GFP_NOFS); |
| 3733 | if (!block) { |
| 3734 | err = -ENOMEM; |
| 3735 | break; |
| 3736 | } |
| 3737 | block->bytenr = leaf->start; |
| 3738 | btrfs_item_key_to_cpu(leaf, &block->key, 0); |
| 3739 | block->level = 0; |
| 3740 | block->key_ready = 1; |
| 3741 | rb_node = tree_insert(blocks, block->bytenr, |
| 3742 | &block->rb_node); |
| 3743 | if (rb_node) |
| 3744 | backref_tree_panic(rb_node, -EEXIST, |
| 3745 | block->bytenr); |
| 3746 | } |
| 3747 | if (counted) |
| 3748 | added = 1; |
| 3749 | else |
| 3750 | path->slots[0] = nritems; |
| 3751 | next: |
| 3752 | path->slots[0]++; |
| 3753 | |
| 3754 | } |
| 3755 | out: |
| 3756 | btrfs_free_path(path); |
| 3757 | return err; |
| 3758 | } |
| 3759 | |
| 3760 | /* |
| 3761 | * helper to find all tree blocks that reference a given data extent |
| 3762 | */ |
| 3763 | static noinline_for_stack |
| 3764 | int add_data_references(struct reloc_control *rc, |
| 3765 | struct btrfs_key *extent_key, |
| 3766 | struct btrfs_path *path, |
| 3767 | struct rb_root *blocks) |
| 3768 | { |
| 3769 | struct btrfs_key key; |
| 3770 | struct extent_buffer *eb; |
| 3771 | struct btrfs_extent_data_ref *dref; |
| 3772 | struct btrfs_extent_inline_ref *iref; |
| 3773 | unsigned long ptr; |
| 3774 | unsigned long end; |
| 3775 | u32 blocksize = rc->extent_root->fs_info->nodesize; |
| 3776 | int ret = 0; |
| 3777 | int err = 0; |
| 3778 | |
| 3779 | eb = path->nodes[0]; |
| 3780 | ptr = btrfs_item_ptr_offset(eb, path->slots[0]); |
| 3781 | end = ptr + btrfs_item_size_nr(eb, path->slots[0]); |
| 3782 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3783 | if (ptr + sizeof(struct btrfs_extent_item_v0) == end) |
| 3784 | ptr = end; |
| 3785 | else |
| 3786 | #endif |
| 3787 | ptr += sizeof(struct btrfs_extent_item); |
| 3788 | |
| 3789 | while (ptr < end) { |
| 3790 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 3791 | key.type = btrfs_get_extent_inline_ref_type(eb, iref, |
| 3792 | BTRFS_REF_TYPE_DATA); |
| 3793 | if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 3794 | key.offset = btrfs_extent_inline_ref_offset(eb, iref); |
| 3795 | ret = __add_tree_block(rc, key.offset, blocksize, |
| 3796 | blocks); |
| 3797 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 3798 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 3799 | ret = find_data_references(rc, extent_key, |
| 3800 | eb, dref, blocks); |
| 3801 | } else { |
| 3802 | ret = -EINVAL; |
| 3803 | btrfs_err(rc->extent_root->fs_info, |
| 3804 | "extent %llu slot %d has an invalid inline ref type", |
| 3805 | eb->start, path->slots[0]); |
| 3806 | } |
| 3807 | if (ret) { |
| 3808 | err = ret; |
| 3809 | goto out; |
| 3810 | } |
| 3811 | ptr += btrfs_extent_inline_ref_size(key.type); |
| 3812 | } |
| 3813 | WARN_ON(ptr > end); |
| 3814 | |
| 3815 | while (1) { |
| 3816 | cond_resched(); |
| 3817 | eb = path->nodes[0]; |
| 3818 | if (path->slots[0] >= btrfs_header_nritems(eb)) { |
| 3819 | ret = btrfs_next_leaf(rc->extent_root, path); |
| 3820 | if (ret < 0) { |
| 3821 | err = ret; |
| 3822 | break; |
| 3823 | } |
| 3824 | if (ret > 0) |
| 3825 | break; |
| 3826 | eb = path->nodes[0]; |
| 3827 | } |
| 3828 | |
| 3829 | btrfs_item_key_to_cpu(eb, &key, path->slots[0]); |
| 3830 | if (key.objectid != extent_key->objectid) |
| 3831 | break; |
| 3832 | |
| 3833 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3834 | if (key.type == BTRFS_SHARED_DATA_REF_KEY || |
| 3835 | key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 3836 | #else |
| 3837 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); |
| 3838 | if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 3839 | #endif |
| 3840 | ret = __add_tree_block(rc, key.offset, blocksize, |
| 3841 | blocks); |
| 3842 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 3843 | dref = btrfs_item_ptr(eb, path->slots[0], |
| 3844 | struct btrfs_extent_data_ref); |
| 3845 | ret = find_data_references(rc, extent_key, |
| 3846 | eb, dref, blocks); |
| 3847 | } else { |
| 3848 | ret = 0; |
| 3849 | } |
| 3850 | if (ret) { |
| 3851 | err = ret; |
| 3852 | break; |
| 3853 | } |
| 3854 | path->slots[0]++; |
| 3855 | } |
| 3856 | out: |
| 3857 | btrfs_release_path(path); |
| 3858 | if (err) |
| 3859 | free_block_list(blocks); |
| 3860 | return err; |
| 3861 | } |
| 3862 | |
| 3863 | /* |
| 3864 | * helper to find next unprocessed extent |
| 3865 | */ |
| 3866 | static noinline_for_stack |
| 3867 | int find_next_extent(struct reloc_control *rc, struct btrfs_path *path, |
| 3868 | struct btrfs_key *extent_key) |
| 3869 | { |
| 3870 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3871 | struct btrfs_key key; |
| 3872 | struct extent_buffer *leaf; |
| 3873 | u64 start, end, last; |
| 3874 | int ret; |
| 3875 | |
| 3876 | last = rc->block_group->key.objectid + rc->block_group->key.offset; |
| 3877 | while (1) { |
| 3878 | cond_resched(); |
| 3879 | if (rc->search_start >= last) { |
| 3880 | ret = 1; |
| 3881 | break; |
| 3882 | } |
| 3883 | |
| 3884 | key.objectid = rc->search_start; |
| 3885 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3886 | key.offset = 0; |
| 3887 | |
| 3888 | path->search_commit_root = 1; |
| 3889 | path->skip_locking = 1; |
| 3890 | ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, |
| 3891 | 0, 0); |
| 3892 | if (ret < 0) |
| 3893 | break; |
| 3894 | next: |
| 3895 | leaf = path->nodes[0]; |
| 3896 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 3897 | ret = btrfs_next_leaf(rc->extent_root, path); |
| 3898 | if (ret != 0) |
| 3899 | break; |
| 3900 | leaf = path->nodes[0]; |
| 3901 | } |
| 3902 | |
| 3903 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 3904 | if (key.objectid >= last) { |
| 3905 | ret = 1; |
| 3906 | break; |
| 3907 | } |
| 3908 | |
| 3909 | if (key.type != BTRFS_EXTENT_ITEM_KEY && |
| 3910 | key.type != BTRFS_METADATA_ITEM_KEY) { |
| 3911 | path->slots[0]++; |
| 3912 | goto next; |
| 3913 | } |
| 3914 | |
| 3915 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3916 | key.objectid + key.offset <= rc->search_start) { |
| 3917 | path->slots[0]++; |
| 3918 | goto next; |
| 3919 | } |
| 3920 | |
| 3921 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 3922 | key.objectid + fs_info->nodesize <= |
| 3923 | rc->search_start) { |
| 3924 | path->slots[0]++; |
| 3925 | goto next; |
| 3926 | } |
| 3927 | |
| 3928 | ret = find_first_extent_bit(&rc->processed_blocks, |
| 3929 | key.objectid, &start, &end, |
| 3930 | EXTENT_DIRTY, NULL); |
| 3931 | |
| 3932 | if (ret == 0 && start <= key.objectid) { |
| 3933 | btrfs_release_path(path); |
| 3934 | rc->search_start = end + 1; |
| 3935 | } else { |
| 3936 | if (key.type == BTRFS_EXTENT_ITEM_KEY) |
| 3937 | rc->search_start = key.objectid + key.offset; |
| 3938 | else |
| 3939 | rc->search_start = key.objectid + |
| 3940 | fs_info->nodesize; |
| 3941 | memcpy(extent_key, &key, sizeof(key)); |
| 3942 | return 0; |
| 3943 | } |
| 3944 | } |
| 3945 | btrfs_release_path(path); |
| 3946 | return ret; |
| 3947 | } |
| 3948 | |
| 3949 | static void set_reloc_control(struct reloc_control *rc) |
| 3950 | { |
| 3951 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3952 | |
| 3953 | mutex_lock(&fs_info->reloc_mutex); |
| 3954 | fs_info->reloc_ctl = rc; |
| 3955 | mutex_unlock(&fs_info->reloc_mutex); |
| 3956 | } |
| 3957 | |
| 3958 | static void unset_reloc_control(struct reloc_control *rc) |
| 3959 | { |
| 3960 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3961 | |
| 3962 | mutex_lock(&fs_info->reloc_mutex); |
| 3963 | fs_info->reloc_ctl = NULL; |
| 3964 | mutex_unlock(&fs_info->reloc_mutex); |
| 3965 | } |
| 3966 | |
| 3967 | static int check_extent_flags(u64 flags) |
| 3968 | { |
| 3969 | if ((flags & BTRFS_EXTENT_FLAG_DATA) && |
| 3970 | (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) |
| 3971 | return 1; |
| 3972 | if (!(flags & BTRFS_EXTENT_FLAG_DATA) && |
| 3973 | !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) |
| 3974 | return 1; |
| 3975 | if ((flags & BTRFS_EXTENT_FLAG_DATA) && |
| 3976 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 3977 | return 1; |
| 3978 | return 0; |
| 3979 | } |
| 3980 | |
| 3981 | static noinline_for_stack |
| 3982 | int prepare_to_relocate(struct reloc_control *rc) |
| 3983 | { |
| 3984 | struct btrfs_trans_handle *trans; |
| 3985 | int ret; |
| 3986 | |
| 3987 | rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info, |
| 3988 | BTRFS_BLOCK_RSV_TEMP); |
| 3989 | if (!rc->block_rsv) |
| 3990 | return -ENOMEM; |
| 3991 | |
| 3992 | memset(&rc->cluster, 0, sizeof(rc->cluster)); |
| 3993 | rc->search_start = rc->block_group->key.objectid; |
| 3994 | rc->extents_found = 0; |
| 3995 | rc->nodes_relocated = 0; |
| 3996 | rc->merging_rsv_size = 0; |
| 3997 | rc->reserved_bytes = 0; |
| 3998 | rc->block_rsv->size = rc->extent_root->fs_info->nodesize * |
| 3999 | RELOCATION_RESERVED_NODES; |
| 4000 | ret = btrfs_block_rsv_refill(rc->extent_root, |
| 4001 | rc->block_rsv, rc->block_rsv->size, |
| 4002 | BTRFS_RESERVE_FLUSH_ALL); |
| 4003 | if (ret) |
| 4004 | return ret; |
| 4005 | |
| 4006 | rc->create_reloc_tree = 1; |
| 4007 | set_reloc_control(rc); |
| 4008 | |
| 4009 | trans = btrfs_join_transaction(rc->extent_root); |
| 4010 | if (IS_ERR(trans)) { |
| 4011 | unset_reloc_control(rc); |
| 4012 | /* |
| 4013 | * extent tree is not a ref_cow tree and has no reloc_root to |
| 4014 | * cleanup. And callers are responsible to free the above |
| 4015 | * block rsv. |
| 4016 | */ |
| 4017 | return PTR_ERR(trans); |
| 4018 | } |
| 4019 | btrfs_commit_transaction(trans); |
| 4020 | return 0; |
| 4021 | } |
| 4022 | |
| 4023 | static noinline_for_stack int relocate_block_group(struct reloc_control *rc) |
| 4024 | { |
| 4025 | struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 4026 | struct rb_root blocks = RB_ROOT; |
| 4027 | struct btrfs_key key; |
| 4028 | struct btrfs_trans_handle *trans = NULL; |
| 4029 | struct btrfs_path *path; |
| 4030 | struct btrfs_extent_item *ei; |
| 4031 | u64 flags; |
| 4032 | u32 item_size; |
| 4033 | int ret; |
| 4034 | int err = 0; |
| 4035 | int progress = 0; |
| 4036 | |
| 4037 | path = btrfs_alloc_path(); |
| 4038 | if (!path) |
| 4039 | return -ENOMEM; |
| 4040 | path->reada = READA_FORWARD; |
| 4041 | |
| 4042 | ret = prepare_to_relocate(rc); |
| 4043 | if (ret) { |
| 4044 | err = ret; |
| 4045 | goto out_free; |
| 4046 | } |
| 4047 | |
| 4048 | while (1) { |
| 4049 | rc->reserved_bytes = 0; |
| 4050 | ret = btrfs_block_rsv_refill(rc->extent_root, |
| 4051 | rc->block_rsv, rc->block_rsv->size, |
| 4052 | BTRFS_RESERVE_FLUSH_ALL); |
| 4053 | if (ret) { |
| 4054 | err = ret; |
| 4055 | break; |
| 4056 | } |
| 4057 | progress++; |
| 4058 | trans = btrfs_start_transaction(rc->extent_root, 0); |
| 4059 | if (IS_ERR(trans)) { |
| 4060 | err = PTR_ERR(trans); |
| 4061 | trans = NULL; |
| 4062 | break; |
| 4063 | } |
| 4064 | restart: |
| 4065 | if (update_backref_cache(trans, &rc->backref_cache)) { |
| 4066 | btrfs_end_transaction(trans); |
| 4067 | trans = NULL; |
| 4068 | continue; |
| 4069 | } |
| 4070 | |
| 4071 | ret = find_next_extent(rc, path, &key); |
| 4072 | if (ret < 0) |
| 4073 | err = ret; |
| 4074 | if (ret != 0) |
| 4075 | break; |
| 4076 | |
| 4077 | rc->extents_found++; |
| 4078 | |
| 4079 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4080 | struct btrfs_extent_item); |
| 4081 | item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); |
| 4082 | if (item_size >= sizeof(*ei)) { |
| 4083 | flags = btrfs_extent_flags(path->nodes[0], ei); |
| 4084 | ret = check_extent_flags(flags); |
| 4085 | BUG_ON(ret); |
| 4086 | |
| 4087 | } else { |
| 4088 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4089 | u64 ref_owner; |
| 4090 | int path_change = 0; |
| 4091 | |
| 4092 | BUG_ON(item_size != |
| 4093 | sizeof(struct btrfs_extent_item_v0)); |
| 4094 | ret = get_ref_objectid_v0(rc, path, &key, &ref_owner, |
| 4095 | &path_change); |
| 4096 | if (ret < 0) { |
| 4097 | err = ret; |
| 4098 | break; |
| 4099 | } |
| 4100 | if (ref_owner < BTRFS_FIRST_FREE_OBJECTID) |
| 4101 | flags = BTRFS_EXTENT_FLAG_TREE_BLOCK; |
| 4102 | else |
| 4103 | flags = BTRFS_EXTENT_FLAG_DATA; |
| 4104 | |
| 4105 | if (path_change) { |
| 4106 | btrfs_release_path(path); |
| 4107 | |
| 4108 | path->search_commit_root = 1; |
| 4109 | path->skip_locking = 1; |
| 4110 | ret = btrfs_search_slot(NULL, rc->extent_root, |
| 4111 | &key, path, 0, 0); |
| 4112 | if (ret < 0) { |
| 4113 | err = ret; |
| 4114 | break; |
| 4115 | } |
| 4116 | BUG_ON(ret > 0); |
| 4117 | } |
| 4118 | #else |
| 4119 | BUG(); |
| 4120 | #endif |
| 4121 | } |
| 4122 | |
| 4123 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 4124 | ret = add_tree_block(rc, &key, path, &blocks); |
| 4125 | } else if (rc->stage == UPDATE_DATA_PTRS && |
| 4126 | (flags & BTRFS_EXTENT_FLAG_DATA)) { |
| 4127 | ret = add_data_references(rc, &key, path, &blocks); |
| 4128 | } else { |
| 4129 | btrfs_release_path(path); |
| 4130 | ret = 0; |
| 4131 | } |
| 4132 | if (ret < 0) { |
| 4133 | err = ret; |
| 4134 | break; |
| 4135 | } |
| 4136 | |
| 4137 | if (!RB_EMPTY_ROOT(&blocks)) { |
| 4138 | ret = relocate_tree_blocks(trans, rc, &blocks); |
| 4139 | if (ret < 0) { |
| 4140 | /* |
| 4141 | * if we fail to relocate tree blocks, force to update |
| 4142 | * backref cache when committing transaction. |
| 4143 | */ |
| 4144 | rc->backref_cache.last_trans = trans->transid - 1; |
| 4145 | |
| 4146 | if (ret != -EAGAIN) { |
| 4147 | err = ret; |
| 4148 | break; |
| 4149 | } |
| 4150 | rc->extents_found--; |
| 4151 | rc->search_start = key.objectid; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | btrfs_end_transaction_throttle(trans); |
| 4156 | btrfs_btree_balance_dirty(fs_info); |
| 4157 | trans = NULL; |
| 4158 | |
| 4159 | if (rc->stage == MOVE_DATA_EXTENTS && |
| 4160 | (flags & BTRFS_EXTENT_FLAG_DATA)) { |
| 4161 | rc->found_file_extent = 1; |
| 4162 | ret = relocate_data_extent(rc->data_inode, |
| 4163 | &key, &rc->cluster); |
| 4164 | if (ret < 0) { |
| 4165 | err = ret; |
| 4166 | break; |
| 4167 | } |
| 4168 | } |
| 4169 | } |
| 4170 | if (trans && progress && err == -ENOSPC) { |
| 4171 | ret = btrfs_force_chunk_alloc(trans, fs_info, |
| 4172 | rc->block_group->flags); |
| 4173 | if (ret == 1) { |
| 4174 | err = 0; |
| 4175 | progress = 0; |
| 4176 | goto restart; |
| 4177 | } |
| 4178 | } |
| 4179 | |
| 4180 | btrfs_release_path(path); |
| 4181 | clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY); |
| 4182 | |
| 4183 | if (trans) { |
| 4184 | btrfs_end_transaction_throttle(trans); |
| 4185 | btrfs_btree_balance_dirty(fs_info); |
| 4186 | } |
| 4187 | |
| 4188 | if (!err) { |
| 4189 | ret = relocate_file_extent_cluster(rc->data_inode, |
| 4190 | &rc->cluster); |
| 4191 | if (ret < 0) |
| 4192 | err = ret; |
| 4193 | } |
| 4194 | |
| 4195 | rc->create_reloc_tree = 0; |
| 4196 | set_reloc_control(rc); |
| 4197 | |
| 4198 | backref_cache_cleanup(&rc->backref_cache); |
| 4199 | btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1); |
| 4200 | |
| 4201 | err = prepare_to_merge(rc, err); |
| 4202 | |
| 4203 | merge_reloc_roots(rc); |
| 4204 | |
| 4205 | rc->merge_reloc_tree = 0; |
| 4206 | unset_reloc_control(rc); |
| 4207 | btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1); |
| 4208 | |
| 4209 | /* get rid of pinned extents */ |
| 4210 | trans = btrfs_join_transaction(rc->extent_root); |
| 4211 | if (IS_ERR(trans)) { |
| 4212 | err = PTR_ERR(trans); |
| 4213 | goto out_free; |
| 4214 | } |
| 4215 | btrfs_commit_transaction(trans); |
| 4216 | out_free: |
| 4217 | btrfs_free_block_rsv(fs_info, rc->block_rsv); |
| 4218 | btrfs_free_path(path); |
| 4219 | return err; |
| 4220 | } |
| 4221 | |
| 4222 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 4223 | struct btrfs_root *root, u64 objectid) |
| 4224 | { |
| 4225 | struct btrfs_path *path; |
| 4226 | struct btrfs_inode_item *item; |
| 4227 | struct extent_buffer *leaf; |
| 4228 | int ret; |
| 4229 | |
| 4230 | path = btrfs_alloc_path(); |
| 4231 | if (!path) |
| 4232 | return -ENOMEM; |
| 4233 | |
| 4234 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 4235 | if (ret) |
| 4236 | goto out; |
| 4237 | |
| 4238 | leaf = path->nodes[0]; |
| 4239 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 4240 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
| 4241 | btrfs_set_inode_generation(leaf, item, 1); |
| 4242 | btrfs_set_inode_size(leaf, item, 0); |
| 4243 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
| 4244 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS | |
| 4245 | BTRFS_INODE_PREALLOC); |
| 4246 | btrfs_mark_buffer_dirty(leaf); |
| 4247 | out: |
| 4248 | btrfs_free_path(path); |
| 4249 | return ret; |
| 4250 | } |
| 4251 | |
| 4252 | /* |
| 4253 | * helper to create inode for data relocation. |
| 4254 | * the inode is in data relocation tree and its link count is 0 |
| 4255 | */ |
| 4256 | static noinline_for_stack |
| 4257 | struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, |
| 4258 | struct btrfs_block_group_cache *group) |
| 4259 | { |
| 4260 | struct inode *inode = NULL; |
| 4261 | struct btrfs_trans_handle *trans; |
| 4262 | struct btrfs_root *root; |
| 4263 | struct btrfs_key key; |
| 4264 | u64 objectid; |
| 4265 | int err = 0; |
| 4266 | |
| 4267 | root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID); |
| 4268 | if (IS_ERR(root)) |
| 4269 | return ERR_CAST(root); |
| 4270 | |
| 4271 | trans = btrfs_start_transaction(root, 6); |
| 4272 | if (IS_ERR(trans)) |
| 4273 | return ERR_CAST(trans); |
| 4274 | |
| 4275 | err = btrfs_find_free_objectid(root, &objectid); |
| 4276 | if (err) |
| 4277 | goto out; |
| 4278 | |
| 4279 | err = __insert_orphan_inode(trans, root, objectid); |
| 4280 | BUG_ON(err); |
| 4281 | |
| 4282 | key.objectid = objectid; |
| 4283 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4284 | key.offset = 0; |
| 4285 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 4286 | BUG_ON(IS_ERR(inode) || is_bad_inode(inode)); |
| 4287 | BTRFS_I(inode)->index_cnt = group->key.objectid; |
| 4288 | |
| 4289 | err = btrfs_orphan_add(trans, BTRFS_I(inode)); |
| 4290 | out: |
| 4291 | btrfs_end_transaction(trans); |
| 4292 | btrfs_btree_balance_dirty(fs_info); |
| 4293 | if (err) { |
| 4294 | if (inode) |
| 4295 | iput(inode); |
| 4296 | inode = ERR_PTR(err); |
| 4297 | } |
| 4298 | return inode; |
| 4299 | } |
| 4300 | |
| 4301 | static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info) |
| 4302 | { |
| 4303 | struct reloc_control *rc; |
| 4304 | |
| 4305 | rc = kzalloc(sizeof(*rc), GFP_NOFS); |
| 4306 | if (!rc) |
| 4307 | return NULL; |
| 4308 | |
| 4309 | INIT_LIST_HEAD(&rc->reloc_roots); |
| 4310 | backref_cache_init(&rc->backref_cache); |
| 4311 | mapping_tree_init(&rc->reloc_root_tree); |
| 4312 | extent_io_tree_init(&rc->processed_blocks, NULL); |
| 4313 | return rc; |
| 4314 | } |
| 4315 | |
| 4316 | /* |
| 4317 | * Print the block group being relocated |
| 4318 | */ |
| 4319 | static void describe_relocation(struct btrfs_fs_info *fs_info, |
| 4320 | struct btrfs_block_group_cache *block_group) |
| 4321 | { |
| 4322 | char buf[128]; /* prefixed by a '|' that'll be dropped */ |
| 4323 | u64 flags = block_group->flags; |
| 4324 | |
| 4325 | /* Shouldn't happen */ |
| 4326 | if (!flags) { |
| 4327 | strcpy(buf, "|NONE"); |
| 4328 | } else { |
| 4329 | char *bp = buf; |
| 4330 | |
| 4331 | #define DESCRIBE_FLAG(f, d) \ |
| 4332 | if (flags & BTRFS_BLOCK_GROUP_##f) { \ |
| 4333 | bp += snprintf(bp, buf - bp + sizeof(buf), "|%s", d); \ |
| 4334 | flags &= ~BTRFS_BLOCK_GROUP_##f; \ |
| 4335 | } |
| 4336 | DESCRIBE_FLAG(DATA, "data"); |
| 4337 | DESCRIBE_FLAG(SYSTEM, "system"); |
| 4338 | DESCRIBE_FLAG(METADATA, "metadata"); |
| 4339 | DESCRIBE_FLAG(RAID0, "raid0"); |
| 4340 | DESCRIBE_FLAG(RAID1, "raid1"); |
| 4341 | DESCRIBE_FLAG(DUP, "dup"); |
| 4342 | DESCRIBE_FLAG(RAID10, "raid10"); |
| 4343 | DESCRIBE_FLAG(RAID5, "raid5"); |
| 4344 | DESCRIBE_FLAG(RAID6, "raid6"); |
| 4345 | if (flags) |
| 4346 | snprintf(buf, buf - bp + sizeof(buf), "|0x%llx", flags); |
| 4347 | #undef DESCRIBE_FLAG |
| 4348 | } |
| 4349 | |
| 4350 | btrfs_info(fs_info, |
| 4351 | "relocating block group %llu flags %s", |
| 4352 | block_group->key.objectid, buf + 1); |
| 4353 | } |
| 4354 | |
| 4355 | /* |
| 4356 | * function to relocate all extents in a block group. |
| 4357 | */ |
| 4358 | int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start) |
| 4359 | { |
| 4360 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4361 | struct reloc_control *rc; |
| 4362 | struct inode *inode; |
| 4363 | struct btrfs_path *path; |
| 4364 | int ret; |
| 4365 | int rw = 0; |
| 4366 | int err = 0; |
| 4367 | |
| 4368 | rc = alloc_reloc_control(fs_info); |
| 4369 | if (!rc) |
| 4370 | return -ENOMEM; |
| 4371 | |
| 4372 | rc->extent_root = extent_root; |
| 4373 | |
| 4374 | rc->block_group = btrfs_lookup_block_group(fs_info, group_start); |
| 4375 | BUG_ON(!rc->block_group); |
| 4376 | |
| 4377 | ret = btrfs_inc_block_group_ro(fs_info, rc->block_group); |
| 4378 | if (ret) { |
| 4379 | err = ret; |
| 4380 | goto out; |
| 4381 | } |
| 4382 | rw = 1; |
| 4383 | |
| 4384 | path = btrfs_alloc_path(); |
| 4385 | if (!path) { |
| 4386 | err = -ENOMEM; |
| 4387 | goto out; |
| 4388 | } |
| 4389 | |
| 4390 | inode = lookup_free_space_inode(fs_info, rc->block_group, path); |
| 4391 | btrfs_free_path(path); |
| 4392 | |
| 4393 | if (!IS_ERR(inode)) |
| 4394 | ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0); |
| 4395 | else |
| 4396 | ret = PTR_ERR(inode); |
| 4397 | |
| 4398 | if (ret && ret != -ENOENT) { |
| 4399 | err = ret; |
| 4400 | goto out; |
| 4401 | } |
| 4402 | |
| 4403 | rc->data_inode = create_reloc_inode(fs_info, rc->block_group); |
| 4404 | if (IS_ERR(rc->data_inode)) { |
| 4405 | err = PTR_ERR(rc->data_inode); |
| 4406 | rc->data_inode = NULL; |
| 4407 | goto out; |
| 4408 | } |
| 4409 | |
| 4410 | describe_relocation(fs_info, rc->block_group); |
| 4411 | |
| 4412 | btrfs_wait_block_group_reservations(rc->block_group); |
| 4413 | btrfs_wait_nocow_writers(rc->block_group); |
| 4414 | btrfs_wait_ordered_roots(fs_info, U64_MAX, |
| 4415 | rc->block_group->key.objectid, |
| 4416 | rc->block_group->key.offset); |
| 4417 | |
| 4418 | while (1) { |
| 4419 | mutex_lock(&fs_info->cleaner_mutex); |
| 4420 | ret = relocate_block_group(rc); |
| 4421 | mutex_unlock(&fs_info->cleaner_mutex); |
| 4422 | if (ret < 0) |
| 4423 | err = ret; |
| 4424 | |
| 4425 | /* |
| 4426 | * We may have gotten ENOSPC after we already dirtied some |
| 4427 | * extents. If writeout happens while we're relocating a |
| 4428 | * different block group we could end up hitting the |
| 4429 | * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in |
| 4430 | * btrfs_reloc_cow_block. Make sure we write everything out |
| 4431 | * properly so we don't trip over this problem, and then break |
| 4432 | * out of the loop if we hit an error. |
| 4433 | */ |
| 4434 | if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) { |
| 4435 | ret = btrfs_wait_ordered_range(rc->data_inode, 0, |
| 4436 | (u64)-1); |
| 4437 | if (ret) |
| 4438 | err = ret; |
| 4439 | invalidate_mapping_pages(rc->data_inode->i_mapping, |
| 4440 | 0, -1); |
| 4441 | rc->stage = UPDATE_DATA_PTRS; |
| 4442 | } |
| 4443 | |
| 4444 | if (err < 0) |
| 4445 | goto out; |
| 4446 | |
| 4447 | if (rc->extents_found == 0) |
| 4448 | break; |
| 4449 | |
| 4450 | btrfs_info(fs_info, "found %llu extents", rc->extents_found); |
| 4451 | |
| 4452 | } |
| 4453 | |
| 4454 | WARN_ON(rc->block_group->pinned > 0); |
| 4455 | WARN_ON(rc->block_group->reserved > 0); |
| 4456 | WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0); |
| 4457 | out: |
| 4458 | if (err && rw) |
| 4459 | btrfs_dec_block_group_ro(rc->block_group); |
| 4460 | iput(rc->data_inode); |
| 4461 | btrfs_put_block_group(rc->block_group); |
| 4462 | kfree(rc); |
| 4463 | return err; |
| 4464 | } |
| 4465 | |
| 4466 | static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) |
| 4467 | { |
| 4468 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4469 | struct btrfs_trans_handle *trans; |
| 4470 | int ret, err; |
| 4471 | |
| 4472 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
| 4473 | if (IS_ERR(trans)) |
| 4474 | return PTR_ERR(trans); |
| 4475 | |
| 4476 | memset(&root->root_item.drop_progress, 0, |
| 4477 | sizeof(root->root_item.drop_progress)); |
| 4478 | root->root_item.drop_level = 0; |
| 4479 | btrfs_set_root_refs(&root->root_item, 0); |
| 4480 | ret = btrfs_update_root(trans, fs_info->tree_root, |
| 4481 | &root->root_key, &root->root_item); |
| 4482 | |
| 4483 | err = btrfs_end_transaction(trans); |
| 4484 | if (err) |
| 4485 | return err; |
| 4486 | return ret; |
| 4487 | } |
| 4488 | |
| 4489 | /* |
| 4490 | * recover relocation interrupted by system crash. |
| 4491 | * |
| 4492 | * this function resumes merging reloc trees with corresponding fs trees. |
| 4493 | * this is important for keeping the sharing of tree blocks |
| 4494 | */ |
| 4495 | int btrfs_recover_relocation(struct btrfs_root *root) |
| 4496 | { |
| 4497 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4498 | LIST_HEAD(reloc_roots); |
| 4499 | struct btrfs_key key; |
| 4500 | struct btrfs_root *fs_root; |
| 4501 | struct btrfs_root *reloc_root; |
| 4502 | struct btrfs_path *path; |
| 4503 | struct extent_buffer *leaf; |
| 4504 | struct reloc_control *rc = NULL; |
| 4505 | struct btrfs_trans_handle *trans; |
| 4506 | int ret; |
| 4507 | int err = 0; |
| 4508 | |
| 4509 | path = btrfs_alloc_path(); |
| 4510 | if (!path) |
| 4511 | return -ENOMEM; |
| 4512 | path->reada = READA_BACK; |
| 4513 | |
| 4514 | key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 4515 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4516 | key.offset = (u64)-1; |
| 4517 | |
| 4518 | while (1) { |
| 4519 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, |
| 4520 | path, 0, 0); |
| 4521 | if (ret < 0) { |
| 4522 | err = ret; |
| 4523 | goto out; |
| 4524 | } |
| 4525 | if (ret > 0) { |
| 4526 | if (path->slots[0] == 0) |
| 4527 | break; |
| 4528 | path->slots[0]--; |
| 4529 | } |
| 4530 | leaf = path->nodes[0]; |
| 4531 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4532 | btrfs_release_path(path); |
| 4533 | |
| 4534 | if (key.objectid != BTRFS_TREE_RELOC_OBJECTID || |
| 4535 | key.type != BTRFS_ROOT_ITEM_KEY) |
| 4536 | break; |
| 4537 | |
| 4538 | reloc_root = btrfs_read_fs_root(root, &key); |
| 4539 | if (IS_ERR(reloc_root)) { |
| 4540 | err = PTR_ERR(reloc_root); |
| 4541 | goto out; |
| 4542 | } |
| 4543 | |
| 4544 | list_add(&reloc_root->root_list, &reloc_roots); |
| 4545 | |
| 4546 | if (btrfs_root_refs(&reloc_root->root_item) > 0) { |
| 4547 | fs_root = read_fs_root(fs_info, |
| 4548 | reloc_root->root_key.offset); |
| 4549 | if (IS_ERR(fs_root)) { |
| 4550 | ret = PTR_ERR(fs_root); |
| 4551 | if (ret != -ENOENT) { |
| 4552 | err = ret; |
| 4553 | goto out; |
| 4554 | } |
| 4555 | ret = mark_garbage_root(reloc_root); |
| 4556 | if (ret < 0) { |
| 4557 | err = ret; |
| 4558 | goto out; |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | if (key.offset == 0) |
| 4564 | break; |
| 4565 | |
| 4566 | key.offset--; |
| 4567 | } |
| 4568 | btrfs_release_path(path); |
| 4569 | |
| 4570 | if (list_empty(&reloc_roots)) |
| 4571 | goto out; |
| 4572 | |
| 4573 | rc = alloc_reloc_control(fs_info); |
| 4574 | if (!rc) { |
| 4575 | err = -ENOMEM; |
| 4576 | goto out; |
| 4577 | } |
| 4578 | |
| 4579 | rc->extent_root = fs_info->extent_root; |
| 4580 | |
| 4581 | set_reloc_control(rc); |
| 4582 | |
| 4583 | trans = btrfs_join_transaction(rc->extent_root); |
| 4584 | if (IS_ERR(trans)) { |
| 4585 | unset_reloc_control(rc); |
| 4586 | err = PTR_ERR(trans); |
| 4587 | goto out_free; |
| 4588 | } |
| 4589 | |
| 4590 | rc->merge_reloc_tree = 1; |
| 4591 | |
| 4592 | while (!list_empty(&reloc_roots)) { |
| 4593 | reloc_root = list_entry(reloc_roots.next, |
| 4594 | struct btrfs_root, root_list); |
| 4595 | list_del(&reloc_root->root_list); |
| 4596 | |
| 4597 | if (btrfs_root_refs(&reloc_root->root_item) == 0) { |
| 4598 | list_add_tail(&reloc_root->root_list, |
| 4599 | &rc->reloc_roots); |
| 4600 | continue; |
| 4601 | } |
| 4602 | |
| 4603 | fs_root = read_fs_root(fs_info, reloc_root->root_key.offset); |
| 4604 | if (IS_ERR(fs_root)) { |
| 4605 | err = PTR_ERR(fs_root); |
| 4606 | list_add_tail(&reloc_root->root_list, &reloc_roots); |
| 4607 | goto out_free; |
| 4608 | } |
| 4609 | |
| 4610 | err = __add_reloc_root(reloc_root); |
| 4611 | BUG_ON(err < 0); /* -ENOMEM or logic error */ |
| 4612 | fs_root->reloc_root = reloc_root; |
| 4613 | } |
| 4614 | |
| 4615 | err = btrfs_commit_transaction(trans); |
| 4616 | if (err) |
| 4617 | goto out_free; |
| 4618 | |
| 4619 | merge_reloc_roots(rc); |
| 4620 | |
| 4621 | unset_reloc_control(rc); |
| 4622 | |
| 4623 | trans = btrfs_join_transaction(rc->extent_root); |
| 4624 | if (IS_ERR(trans)) { |
| 4625 | err = PTR_ERR(trans); |
| 4626 | goto out_free; |
| 4627 | } |
| 4628 | err = btrfs_commit_transaction(trans); |
| 4629 | out_free: |
| 4630 | kfree(rc); |
| 4631 | out: |
| 4632 | if (!list_empty(&reloc_roots)) |
| 4633 | free_reloc_roots(&reloc_roots); |
| 4634 | |
| 4635 | btrfs_free_path(path); |
| 4636 | |
| 4637 | if (err == 0) { |
| 4638 | /* cleanup orphan inode in data relocation tree */ |
| 4639 | fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID); |
| 4640 | if (IS_ERR(fs_root)) |
| 4641 | err = PTR_ERR(fs_root); |
| 4642 | else |
| 4643 | err = btrfs_orphan_cleanup(fs_root); |
| 4644 | } |
| 4645 | return err; |
| 4646 | } |
| 4647 | |
| 4648 | /* |
| 4649 | * helper to add ordered checksum for data relocation. |
| 4650 | * |
| 4651 | * cloning checksum properly handles the nodatasum extents. |
| 4652 | * it also saves CPU time to re-calculate the checksum. |
| 4653 | */ |
| 4654 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
| 4655 | { |
| 4656 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4657 | struct btrfs_ordered_sum *sums; |
| 4658 | struct btrfs_ordered_extent *ordered; |
| 4659 | int ret; |
| 4660 | u64 disk_bytenr; |
| 4661 | u64 new_bytenr; |
| 4662 | LIST_HEAD(list); |
| 4663 | |
| 4664 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); |
| 4665 | BUG_ON(ordered->file_offset != file_pos || ordered->len != len); |
| 4666 | |
| 4667 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; |
| 4668 | ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr, |
| 4669 | disk_bytenr + len - 1, &list, 0); |
| 4670 | if (ret) |
| 4671 | goto out; |
| 4672 | |
| 4673 | while (!list_empty(&list)) { |
| 4674 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 4675 | list_del_init(&sums->list); |
| 4676 | |
| 4677 | /* |
| 4678 | * We need to offset the new_bytenr based on where the csum is. |
| 4679 | * We need to do this because we will read in entire prealloc |
| 4680 | * extents but we may have written to say the middle of the |
| 4681 | * prealloc extent, so we need to make sure the csum goes with |
| 4682 | * the right disk offset. |
| 4683 | * |
| 4684 | * We can do this because the data reloc inode refers strictly |
| 4685 | * to the on disk bytes, so we don't have to worry about |
| 4686 | * disk_len vs real len like with real inodes since it's all |
| 4687 | * disk length. |
| 4688 | */ |
| 4689 | new_bytenr = ordered->start + (sums->bytenr - disk_bytenr); |
| 4690 | sums->bytenr = new_bytenr; |
| 4691 | |
| 4692 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 4693 | } |
| 4694 | out: |
| 4695 | btrfs_put_ordered_extent(ordered); |
| 4696 | return ret; |
| 4697 | } |
| 4698 | |
| 4699 | int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, |
| 4700 | struct btrfs_root *root, struct extent_buffer *buf, |
| 4701 | struct extent_buffer *cow) |
| 4702 | { |
| 4703 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4704 | struct reloc_control *rc; |
| 4705 | struct backref_node *node; |
| 4706 | int first_cow = 0; |
| 4707 | int level; |
| 4708 | int ret = 0; |
| 4709 | |
| 4710 | rc = fs_info->reloc_ctl; |
| 4711 | if (!rc) |
| 4712 | return 0; |
| 4713 | |
| 4714 | BUG_ON(rc->stage == UPDATE_DATA_PTRS && |
| 4715 | root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID); |
| 4716 | |
| 4717 | level = btrfs_header_level(buf); |
| 4718 | if (btrfs_header_generation(buf) <= |
| 4719 | btrfs_root_last_snapshot(&root->root_item)) |
| 4720 | first_cow = 1; |
| 4721 | |
| 4722 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID && |
| 4723 | rc->create_reloc_tree) { |
| 4724 | WARN_ON(!first_cow && level == 0); |
| 4725 | |
| 4726 | node = rc->backref_cache.path[level]; |
| 4727 | BUG_ON(node->bytenr != buf->start && |
| 4728 | node->new_bytenr != buf->start); |
| 4729 | |
| 4730 | drop_node_buffer(node); |
| 4731 | extent_buffer_get(cow); |
| 4732 | node->eb = cow; |
| 4733 | node->new_bytenr = cow->start; |
| 4734 | |
| 4735 | if (!node->pending) { |
| 4736 | list_move_tail(&node->list, |
| 4737 | &rc->backref_cache.pending[level]); |
| 4738 | node->pending = 1; |
| 4739 | } |
| 4740 | |
| 4741 | if (first_cow) |
| 4742 | __mark_block_processed(rc, node); |
| 4743 | |
| 4744 | if (first_cow && level > 0) |
| 4745 | rc->nodes_relocated += buf->len; |
| 4746 | } |
| 4747 | |
| 4748 | if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS) |
| 4749 | ret = replace_file_extents(trans, rc, root, cow); |
| 4750 | return ret; |
| 4751 | } |
| 4752 | |
| 4753 | /* |
| 4754 | * called before creating snapshot. it calculates metadata reservation |
| 4755 | * required for relocating tree blocks in the snapshot |
| 4756 | */ |
| 4757 | void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending, |
| 4758 | u64 *bytes_to_reserve) |
| 4759 | { |
| 4760 | struct btrfs_root *root; |
| 4761 | struct reloc_control *rc; |
| 4762 | |
| 4763 | root = pending->root; |
| 4764 | if (!root->reloc_root) |
| 4765 | return; |
| 4766 | |
| 4767 | rc = root->fs_info->reloc_ctl; |
| 4768 | if (!rc->merge_reloc_tree) |
| 4769 | return; |
| 4770 | |
| 4771 | root = root->reloc_root; |
| 4772 | BUG_ON(btrfs_root_refs(&root->root_item) == 0); |
| 4773 | /* |
| 4774 | * relocation is in the stage of merging trees. the space |
| 4775 | * used by merging a reloc tree is twice the size of |
| 4776 | * relocated tree nodes in the worst case. half for cowing |
| 4777 | * the reloc tree, half for cowing the fs tree. the space |
| 4778 | * used by cowing the reloc tree will be freed after the |
| 4779 | * tree is dropped. if we create snapshot, cowing the fs |
| 4780 | * tree may use more space than it frees. so we need |
| 4781 | * reserve extra space. |
| 4782 | */ |
| 4783 | *bytes_to_reserve += rc->nodes_relocated; |
| 4784 | } |
| 4785 | |
| 4786 | /* |
| 4787 | * called after snapshot is created. migrate block reservation |
| 4788 | * and create reloc root for the newly created snapshot |
| 4789 | */ |
| 4790 | int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, |
| 4791 | struct btrfs_pending_snapshot *pending) |
| 4792 | { |
| 4793 | struct btrfs_root *root = pending->root; |
| 4794 | struct btrfs_root *reloc_root; |
| 4795 | struct btrfs_root *new_root; |
| 4796 | struct reloc_control *rc; |
| 4797 | int ret; |
| 4798 | |
| 4799 | if (!root->reloc_root) |
| 4800 | return 0; |
| 4801 | |
| 4802 | rc = root->fs_info->reloc_ctl; |
| 4803 | rc->merging_rsv_size += rc->nodes_relocated; |
| 4804 | |
| 4805 | if (rc->merge_reloc_tree) { |
| 4806 | ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 4807 | rc->block_rsv, |
| 4808 | rc->nodes_relocated, 1); |
| 4809 | if (ret) |
| 4810 | return ret; |
| 4811 | } |
| 4812 | |
| 4813 | new_root = pending->snap; |
| 4814 | reloc_root = create_reloc_root(trans, root->reloc_root, |
| 4815 | new_root->root_key.objectid); |
| 4816 | if (IS_ERR(reloc_root)) |
| 4817 | return PTR_ERR(reloc_root); |
| 4818 | |
| 4819 | ret = __add_reloc_root(reloc_root); |
| 4820 | BUG_ON(ret < 0); |
| 4821 | new_root->reloc_root = reloc_root; |
| 4822 | |
| 4823 | if (rc->create_reloc_tree) |
| 4824 | ret = clone_backref_node(trans, rc, root, reloc_root); |
| 4825 | return ret; |
| 4826 | } |