| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) 2009 Oracle. All rights reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <linux/sched.h> |
| 7 |
#include <linux/pagemap.h> |
| 8 |
#include <linux/writeback.h> |
| 9 |
#include <linux/blkdev.h> |
| 10 |
#include <linux/rbtree.h> |
| 11 |
#include <linux/slab.h> |
| 12 |
#include <linux/error-injection.h> |
| 13 |
#include "ctree.h" |
| 14 |
#include "disk-io.h" |
| 15 |
#include "transaction.h" |
| 16 |
#include "volumes.h" |
| 17 |
#include "locking.h" |
| 18 |
#include "btrfs_inode.h" |
| 19 |
#include "async-thread.h" |
| 20 |
#include "free-space-cache.h" |
| 21 |
#include "qgroup.h" |
| 22 |
#include "print-tree.h" |
| 23 |
#include "delalloc-space.h" |
| 24 |
#include "block-group.h" |
| 25 |
#include "backref.h" |
| 26 |
#include "misc.h" |
| 27 |
#include "subpage.h" |
| 28 |
#include "zoned.h" |
| 29 |
#include "inode-item.h" |
| 30 |
#include "space-info.h" |
| 31 |
#include "fs.h" |
| 32 |
#include "accessors.h" |
| 33 |
#include "extent-tree.h" |
| 34 |
#include "root-tree.h" |
| 35 |
#include "file-item.h" |
| 36 |
#include "relocation.h" |
| 37 |
#include "super.h" |
| 38 |
#include "tree-checker.h" |
| 39 |
#include "raid-stripe-tree.h" |
| 40 |
#include "free-space-tree.h" |
| 41 |
|
| 42 |
/* |
| 43 |
* Relocation overview |
| 44 |
* |
| 45 |
* [What does relocation do] |
| 46 |
* |
| 47 |
* The objective of relocation is to relocate all extents of the target block |
| 48 |
* group to other block groups. |
| 49 |
* This is utilized by resize (shrink only), profile converting, compacting |
| 50 |
* space, or balance routine to spread chunks over devices. |
| 51 |
* |
| 52 |
* Before | After |
| 53 |
* ------------------------------------------------------------------ |
| 54 |
* BG A: 10 data extents | BG A: deleted |
| 55 |
* BG B: 2 data extents | BG B: 10 data extents (2 old + 8 relocated) |
| 56 |
* BG C: 1 extents | BG C: 3 data extents (1 old + 2 relocated) |
| 57 |
* |
| 58 |
* [How does relocation work] |
| 59 |
* |
| 60 |
* 1. Mark the target block group read-only |
| 61 |
* New extents won't be allocated from the target block group. |
| 62 |
* |
| 63 |
* 2.1 Record each extent in the target block group |
| 64 |
* To build a proper map of extents to be relocated. |
| 65 |
* |
| 66 |
* 2.2 Build data reloc tree and reloc trees |
| 67 |
* Data reloc tree will contain an inode, recording all newly relocated |
| 68 |
* data extents. |
| 69 |
* There will be only one data reloc tree for one data block group. |
| 70 |
* |
| 71 |
* Reloc tree will be a special snapshot of its source tree, containing |
| 72 |
* relocated tree blocks. |
| 73 |
* Each tree referring to a tree block in target block group will get its |
| 74 |
* reloc tree built. |
| 75 |
* |
| 76 |
* 2.3 Swap source tree with its corresponding reloc tree |
| 77 |
* Each involved tree only refers to new extents after swap. |
| 78 |
* |
| 79 |
* 3. Cleanup reloc trees and data reloc tree. |
| 80 |
* As old extents in the target block group are still referenced by reloc |
| 81 |
* trees, we need to clean them up before really freeing the target block |
| 82 |
* group. |
| 83 |
* |
| 84 |
* The main complexity is in steps 2.2 and 2.3. |
| 85 |
* |
| 86 |
* The entry point of relocation is relocate_block_group() function. |
| 87 |
*/ |
| 88 |
|
| 89 |
#define RELOCATION_RESERVED_NODES 256 |
| 90 |
/* |
| 91 |
* map address of tree root to tree |
| 92 |
*/ |
| 93 |
struct mapping_node { |
| 94 |
union { |
| 95 |
/* Use rb_simple_node for search/insert */ |
| 96 |
struct { |
| 97 |
struct rb_node rb_node; |
| 98 |
u64 bytenr; |
| 99 |
}; |
| 100 |
|
| 101 |
struct rb_simple_node simple_node; |
| 102 |
}; |
| 103 |
void *data; |
| 104 |
}; |
| 105 |
|
| 106 |
struct mapping_tree { |
| 107 |
struct rb_root rb_root; |
| 108 |
spinlock_t lock; |
| 109 |
}; |
| 110 |
|
| 111 |
/* |
| 112 |
* present a tree block to process |
| 113 |
*/ |
| 114 |
struct tree_block { |
| 115 |
union { |
| 116 |
/* Use rb_simple_node for search/insert */ |
| 117 |
struct { |
| 118 |
struct rb_node rb_node; |
| 119 |
u64 bytenr; |
| 120 |
}; |
| 121 |
|
| 122 |
struct rb_simple_node simple_node; |
| 123 |
}; |
| 124 |
u64 owner; |
| 125 |
struct btrfs_key key; |
| 126 |
u8 level; |
| 127 |
bool key_ready; |
| 128 |
}; |
| 129 |
|
| 130 |
#define MAX_EXTENTS 128 |
| 131 |
|
| 132 |
struct file_extent_cluster { |
| 133 |
u64 start; |
| 134 |
u64 end; |
| 135 |
u64 boundary[MAX_EXTENTS]; |
| 136 |
unsigned int nr; |
| 137 |
u64 owning_root; |
| 138 |
}; |
| 139 |
|
| 140 |
/* Stages of data relocation. */ |
| 141 |
enum reloc_stage { |
| 142 |
MOVE_DATA_EXTENTS, |
| 143 |
UPDATE_DATA_PTRS |
| 144 |
}; |
| 145 |
|
| 146 |
struct reloc_control { |
| 147 |
/* block group to relocate */ |
| 148 |
struct btrfs_block_group *block_group; |
| 149 |
/* extent tree */ |
| 150 |
struct btrfs_root *extent_root; |
| 151 |
/* inode for moving data */ |
| 152 |
struct inode *data_inode; |
| 153 |
|
| 154 |
struct btrfs_block_rsv *block_rsv; |
| 155 |
|
| 156 |
struct btrfs_backref_cache backref_cache; |
| 157 |
|
| 158 |
struct file_extent_cluster cluster; |
| 159 |
/* tree blocks have been processed */ |
| 160 |
struct extent_io_tree processed_blocks; |
| 161 |
/* map start of tree root to corresponding reloc tree */ |
| 162 |
struct mapping_tree reloc_root_tree; |
| 163 |
/* list of reloc trees */ |
| 164 |
struct list_head reloc_roots; |
| 165 |
/* list of subvolume trees that get relocated */ |
| 166 |
struct list_head dirty_subvol_roots; |
| 167 |
/* size of metadata reservation for merging reloc trees */ |
| 168 |
u64 merging_rsv_size; |
| 169 |
/* size of relocated tree nodes */ |
| 170 |
u64 nodes_relocated; |
| 171 |
/* reserved size for block group relocation*/ |
| 172 |
u64 reserved_bytes; |
| 173 |
|
| 174 |
u64 search_start; |
| 175 |
u64 extents_found; |
| 176 |
|
| 177 |
enum reloc_stage stage; |
| 178 |
bool create_reloc_tree; |
| 179 |
bool merge_reloc_tree; |
| 180 |
bool found_file_extent; |
| 181 |
}; |
| 182 |
|
| 183 |
static void mark_block_processed(struct reloc_control *rc, |
| 184 |
struct btrfs_backref_node *node) |
| 185 |
{ |
| 186 |
u32 blocksize; |
| 187 |
|
| 188 |
if (node->level == 0 || |
| 189 |
in_range(node->bytenr, rc->block_group->start, |
| 190 |
rc->block_group->length)) { |
| 191 |
blocksize = rc->extent_root->fs_info->nodesize; |
| 192 |
btrfs_set_extent_bit(&rc->processed_blocks, node->bytenr, |
| 193 |
node->bytenr + blocksize - 1, EXTENT_DIRTY, |
| 194 |
NULL); |
| 195 |
} |
| 196 |
node->processed = 1; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* walk up backref nodes until reach node presents tree root |
| 201 |
*/ |
| 202 |
static struct btrfs_backref_node *walk_up_backref( |
| 203 |
struct btrfs_backref_node *node, |
| 204 |
struct btrfs_backref_edge *edges[], int *index) |
| 205 |
{ |
| 206 |
struct btrfs_backref_edge *edge; |
| 207 |
int idx = *index; |
| 208 |
|
| 209 |
while (!list_empty(&node->upper)) { |
| 210 |
edge = list_first_entry(&node->upper, struct btrfs_backref_edge, |
| 211 |
list[LOWER]); |
| 212 |
edges[idx++] = edge; |
| 213 |
node = edge->node[UPPER]; |
| 214 |
} |
| 215 |
BUG_ON(node->detached); |
| 216 |
*index = idx; |
| 217 |
return node; |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* walk down backref nodes to find start of next reference path |
| 222 |
*/ |
| 223 |
static struct btrfs_backref_node *walk_down_backref( |
| 224 |
struct btrfs_backref_edge *edges[], int *index) |
| 225 |
{ |
| 226 |
struct btrfs_backref_edge *edge; |
| 227 |
struct btrfs_backref_node *lower; |
| 228 |
int idx = *index; |
| 229 |
|
| 230 |
while (idx > 0) { |
| 231 |
edge = edges[idx - 1]; |
| 232 |
lower = edge->node[LOWER]; |
| 233 |
if (list_is_last(&edge->list[LOWER], &lower->upper)) { |
| 234 |
idx--; |
| 235 |
continue; |
| 236 |
} |
| 237 |
edge = list_first_entry(&edge->list[LOWER], struct btrfs_backref_edge, |
| 238 |
list[LOWER]); |
| 239 |
edges[idx - 1] = edge; |
| 240 |
*index = idx; |
| 241 |
return edge->node[UPPER]; |
| 242 |
} |
| 243 |
*index = 0; |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
|
| 247 |
static bool reloc_root_is_dead(const struct btrfs_root *root) |
| 248 |
{ |
| 249 |
/* |
| 250 |
* Pair with set_bit/clear_bit in clean_dirty_subvols and |
| 251 |
* btrfs_update_reloc_root. We need to see the updated bit before |
| 252 |
* trying to access reloc_root |
| 253 |
*/ |
| 254 |
smp_rmb(); |
| 255 |
if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)) |
| 256 |
return true; |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* Check if this subvolume tree has valid reloc tree. |
| 262 |
* |
| 263 |
* Reloc tree after swap is considered dead, thus not considered as valid. |
| 264 |
* This is enough for most callers, as they don't distinguish dead reloc root |
| 265 |
* from no reloc root. But btrfs_should_ignore_reloc_root() below is a |
| 266 |
* special case. |
| 267 |
*/ |
| 268 |
static bool have_reloc_root(const struct btrfs_root *root) |
| 269 |
{ |
| 270 |
if (reloc_root_is_dead(root)) |
| 271 |
return false; |
| 272 |
if (!root->reloc_root) |
| 273 |
return false; |
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
bool btrfs_should_ignore_reloc_root(const struct btrfs_root *root) |
| 278 |
{ |
| 279 |
struct btrfs_root *reloc_root; |
| 280 |
|
| 281 |
if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
| 282 |
return false; |
| 283 |
|
| 284 |
/* This root has been merged with its reloc tree, we can ignore it */ |
| 285 |
if (reloc_root_is_dead(root)) |
| 286 |
return true; |
| 287 |
|
| 288 |
reloc_root = root->reloc_root; |
| 289 |
if (!reloc_root) |
| 290 |
return false; |
| 291 |
|
| 292 |
if (btrfs_header_generation(reloc_root->commit_root) == |
| 293 |
root->fs_info->running_transaction->transid) |
| 294 |
return false; |
| 295 |
/* |
| 296 |
* If there is reloc tree and it was created in previous transaction |
| 297 |
* backref lookup can find the reloc tree, so backref node for the fs |
| 298 |
* tree root is useless for relocation. |
| 299 |
*/ |
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
/* |
| 304 |
* find reloc tree by address of tree root |
| 305 |
*/ |
| 306 |
struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 307 |
{ |
| 308 |
struct reloc_control *rc = fs_info->reloc_ctl; |
| 309 |
struct rb_node *rb_node; |
| 310 |
struct mapping_node *node; |
| 311 |
struct btrfs_root *root = NULL; |
| 312 |
|
| 313 |
ASSERT(rc); |
| 314 |
spin_lock(&rc->reloc_root_tree.lock); |
| 315 |
rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr); |
| 316 |
if (rb_node) { |
| 317 |
node = rb_entry(rb_node, struct mapping_node, rb_node); |
| 318 |
root = node->data; |
| 319 |
} |
| 320 |
spin_unlock(&rc->reloc_root_tree.lock); |
| 321 |
return btrfs_grab_root(root); |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* For useless nodes, do two major clean ups: |
| 326 |
* |
| 327 |
* - Cleanup the children edges and nodes |
| 328 |
* If child node is also orphan (no parent) during cleanup, then the child |
| 329 |
* node will also be cleaned up. |
| 330 |
* |
| 331 |
* - Freeing up leaves (level 0), keeps nodes detached |
| 332 |
* For nodes, the node is still cached as "detached" |
| 333 |
* |
| 334 |
* Return false if @node is not in the @useless_nodes list. |
| 335 |
* Return true if @node is in the @useless_nodes list. |
| 336 |
*/ |
| 337 |
static bool handle_useless_nodes(struct reloc_control *rc, |
| 338 |
struct btrfs_backref_node *node) |
| 339 |
{ |
| 340 |
struct btrfs_backref_cache *cache = &rc->backref_cache; |
| 341 |
struct list_head *useless_node = &cache->useless_node; |
| 342 |
bool ret = false; |
| 343 |
|
| 344 |
while (!list_empty(useless_node)) { |
| 345 |
struct btrfs_backref_node *cur; |
| 346 |
|
| 347 |
cur = list_first_entry(useless_node, struct btrfs_backref_node, |
| 348 |
list); |
| 349 |
list_del_init(&cur->list); |
| 350 |
|
| 351 |
/* Only tree root nodes can be added to @useless_nodes */ |
| 352 |
ASSERT(list_empty(&cur->upper)); |
| 353 |
|
| 354 |
if (cur == node) |
| 355 |
ret = true; |
| 356 |
|
| 357 |
/* Cleanup the lower edges */ |
| 358 |
while (!list_empty(&cur->lower)) { |
| 359 |
struct btrfs_backref_edge *edge; |
| 360 |
struct btrfs_backref_node *lower; |
| 361 |
|
| 362 |
edge = list_first_entry(&cur->lower, struct btrfs_backref_edge, |
| 363 |
list[UPPER]); |
| 364 |
list_del(&edge->list[UPPER]); |
| 365 |
list_del(&edge->list[LOWER]); |
| 366 |
lower = edge->node[LOWER]; |
| 367 |
btrfs_backref_free_edge(cache, edge); |
| 368 |
|
| 369 |
/* Child node is also orphan, queue for cleanup */ |
| 370 |
if (list_empty(&lower->upper)) |
| 371 |
list_add(&lower->list, useless_node); |
| 372 |
} |
| 373 |
/* Mark this block processed for relocation */ |
| 374 |
mark_block_processed(rc, cur); |
| 375 |
|
| 376 |
/* |
| 377 |
* Backref nodes for tree leaves are deleted from the cache. |
| 378 |
* Backref nodes for upper level tree blocks are left in the |
| 379 |
* cache to avoid unnecessary backref lookup. |
| 380 |
*/ |
| 381 |
if (cur->level > 0) { |
| 382 |
cur->detached = 1; |
| 383 |
} else { |
| 384 |
rb_erase(&cur->rb_node, &cache->rb_root); |
| 385 |
btrfs_backref_free_node(cache, cur); |
| 386 |
} |
| 387 |
} |
| 388 |
return ret; |
| 389 |
} |
| 390 |
|
| 391 |
/* |
| 392 |
* Build backref tree for a given tree block. Root of the backref tree |
| 393 |
* corresponds the tree block, leaves of the backref tree correspond roots of |
| 394 |
* b-trees that reference the tree block. |
| 395 |
* |
| 396 |
* The basic idea of this function is check backrefs of a given block to find |
| 397 |
* upper level blocks that reference the block, and then check backrefs of |
| 398 |
* these upper level blocks recursively. The recursion stops when tree root is |
| 399 |
* reached or backrefs for the block is cached. |
| 400 |
* |
| 401 |
* NOTE: if we find that backrefs for a block are cached, we know backrefs for |
| 402 |
* all upper level blocks that directly/indirectly reference the block are also |
| 403 |
* cached. |
| 404 |
*/ |
| 405 |
static noinline_for_stack struct btrfs_backref_node *build_backref_tree( |
| 406 |
struct btrfs_trans_handle *trans, |
| 407 |
struct reloc_control *rc, struct btrfs_key *node_key, |
| 408 |
int level, u64 bytenr) |
| 409 |
{ |
| 410 |
struct btrfs_backref_iter *iter; |
| 411 |
struct btrfs_backref_cache *cache = &rc->backref_cache; |
| 412 |
/* For searching parent of TREE_BLOCK_REF */ |
| 413 |
struct btrfs_path *path; |
| 414 |
struct btrfs_backref_node *cur; |
| 415 |
struct btrfs_backref_node *node = NULL; |
| 416 |
struct btrfs_backref_edge *edge; |
| 417 |
int ret; |
| 418 |
|
| 419 |
iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info); |
| 420 |
if (!iter) |
| 421 |
return ERR_PTR(-ENOMEM); |
| 422 |
path = btrfs_alloc_path(); |
| 423 |
if (!path) { |
| 424 |
ret = -ENOMEM; |
| 425 |
goto out; |
| 426 |
} |
| 427 |
|
| 428 |
node = btrfs_backref_alloc_node(cache, bytenr, level); |
| 429 |
if (!node) { |
| 430 |
ret = -ENOMEM; |
| 431 |
goto out; |
| 432 |
} |
| 433 |
|
| 434 |
cur = node; |
| 435 |
|
| 436 |
/* Breadth-first search to build backref cache */ |
| 437 |
do { |
| 438 |
ret = btrfs_backref_add_tree_node(trans, cache, path, iter, |
| 439 |
node_key, cur); |
| 440 |
if (ret < 0) |
| 441 |
goto out; |
| 442 |
|
| 443 |
edge = list_first_entry_or_null(&cache->pending_edge, |
| 444 |
struct btrfs_backref_edge, list[UPPER]); |
| 445 |
/* |
| 446 |
* The pending list isn't empty, take the first block to |
| 447 |
* process |
| 448 |
*/ |
| 449 |
if (edge) { |
| 450 |
list_del_init(&edge->list[UPPER]); |
| 451 |
cur = edge->node[UPPER]; |
| 452 |
} |
| 453 |
} while (edge); |
| 454 |
|
| 455 |
/* Finish the upper linkage of newly added edges/nodes */ |
| 456 |
ret = btrfs_backref_finish_upper_links(cache, node); |
| 457 |
if (ret < 0) |
| 458 |
goto out; |
| 459 |
|
| 460 |
if (handle_useless_nodes(rc, node)) |
| 461 |
node = NULL; |
| 462 |
out: |
| 463 |
btrfs_free_path(iter->path); |
| 464 |
kfree(iter); |
| 465 |
btrfs_free_path(path); |
| 466 |
if (ret) { |
| 467 |
btrfs_backref_error_cleanup(cache, node); |
| 468 |
return ERR_PTR(ret); |
| 469 |
} |
| 470 |
ASSERT(!node || !node->detached); |
| 471 |
ASSERT(list_empty(&cache->useless_node) && |
| 472 |
list_empty(&cache->pending_edge)); |
| 473 |
return node; |
| 474 |
} |
| 475 |
|
| 476 |
/* |
| 477 |
* helper to add 'address of tree root -> reloc tree' mapping |
| 478 |
*/ |
| 479 |
static int __add_reloc_root(struct btrfs_root *root) |
| 480 |
{ |
| 481 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 482 |
struct rb_node *rb_node; |
| 483 |
struct mapping_node *node; |
| 484 |
struct reloc_control *rc = fs_info->reloc_ctl; |
| 485 |
|
| 486 |
node = kmalloc_obj(*node, GFP_NOFS); |
| 487 |
if (!node) |
| 488 |
return -ENOMEM; |
| 489 |
|
| 490 |
node->bytenr = root->commit_root->start; |
| 491 |
node->data = root; |
| 492 |
|
| 493 |
spin_lock(&rc->reloc_root_tree.lock); |
| 494 |
rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root, &node->simple_node); |
| 495 |
spin_unlock(&rc->reloc_root_tree.lock); |
| 496 |
if (rb_node) { |
| 497 |
btrfs_err(fs_info, |
| 498 |
"Duplicate root found for start=%llu while inserting into relocation tree", |
| 499 |
node->bytenr); |
| 500 |
return -EEXIST; |
| 501 |
} |
| 502 |
|
| 503 |
list_add_tail(&root->root_list, &rc->reloc_roots); |
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
/* |
| 508 |
* helper to delete the 'address of tree root -> reloc tree' |
| 509 |
* mapping |
| 510 |
*/ |
| 511 |
static void __del_reloc_root(struct btrfs_root *root) |
| 512 |
{ |
| 513 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 514 |
struct rb_node *rb_node; |
| 515 |
struct mapping_node AUTO_KFREE(node); |
| 516 |
struct reloc_control *rc = fs_info->reloc_ctl; |
| 517 |
bool put_ref = false; |
| 518 |
|
| 519 |
if (rc && root->node) { |
| 520 |
spin_lock(&rc->reloc_root_tree.lock); |
| 521 |
rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, |
| 522 |
root->commit_root->start); |
| 523 |
if (rb_node) { |
| 524 |
node = rb_entry(rb_node, struct mapping_node, rb_node); |
| 525 |
rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); |
| 526 |
RB_CLEAR_NODE(&node->rb_node); |
| 527 |
} |
| 528 |
spin_unlock(&rc->reloc_root_tree.lock); |
| 529 |
ASSERT(!node || (struct btrfs_root *)node->data == root); |
| 530 |
} |
| 531 |
|
| 532 |
/* |
| 533 |
* We only put the reloc root here if it's on the list. There's a lot |
| 534 |
* of places where the pattern is to splice the rc->reloc_roots, process |
| 535 |
* the reloc roots, and then add the reloc root back onto |
| 536 |
* rc->reloc_roots. If we call __del_reloc_root while it's off of the |
| 537 |
* list we don't want the reference being dropped, because the guy |
| 538 |
* messing with the list is in charge of the reference. |
| 539 |
*/ |
| 540 |
spin_lock(&fs_info->trans_lock); |
| 541 |
if (!list_empty(&root->root_list)) { |
| 542 |
put_ref = true; |
| 543 |
list_del_init(&root->root_list); |
| 544 |
} |
| 545 |
spin_unlock(&fs_info->trans_lock); |
| 546 |
if (put_ref) |
| 547 |
btrfs_put_root(root); |
| 548 |
} |
| 549 |
|
| 550 |
/* |
| 551 |
* helper to update the 'address of tree root -> reloc tree' |
| 552 |
* mapping |
| 553 |
*/ |
| 554 |
static int __update_reloc_root(struct btrfs_root *root) |
| 555 |
{ |
| 556 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 557 |
struct rb_node *rb_node; |
| 558 |
struct mapping_node *node = NULL; |
| 559 |
struct reloc_control *rc = fs_info->reloc_ctl; |
| 560 |
|
| 561 |
spin_lock(&rc->reloc_root_tree.lock); |
| 562 |
rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, |
| 563 |
root->commit_root->start); |
| 564 |
if (rb_node) { |
| 565 |
node = rb_entry(rb_node, struct mapping_node, rb_node); |
| 566 |
rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); |
| 567 |
} |
| 568 |
spin_unlock(&rc->reloc_root_tree.lock); |
| 569 |
|
| 570 |
if (!node) |
| 571 |
return 0; |
| 572 |
BUG_ON((struct btrfs_root *)node->data != root); |
| 573 |
|
| 574 |
spin_lock(&rc->reloc_root_tree.lock); |
| 575 |
node->bytenr = root->node->start; |
| 576 |
rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root, &node->simple_node); |
| 577 |
spin_unlock(&rc->reloc_root_tree.lock); |
| 578 |
if (rb_node) |
| 579 |
btrfs_backref_panic(fs_info, node->bytenr, -EEXIST); |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, |
| 584 |
struct btrfs_root *root, u64 objectid) |
| 585 |
{ |
| 586 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 587 |
struct btrfs_root *reloc_root; |
| 588 |
struct extent_buffer *eb; |
| 589 |
struct btrfs_root_item AUTO_KFREE(root_item); |
| 590 |
struct btrfs_key root_key; |
| 591 |
int ret = 0; |
| 592 |
|
| 593 |
root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 594 |
if (!root_item) |
| 595 |
return ERR_PTR(-ENOMEM); |
| 596 |
|
| 597 |
root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 598 |
root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 599 |
root_key.offset = objectid; |
| 600 |
|
| 601 |
if (btrfs_root_id(root) == objectid) { |
| 602 |
u64 commit_root_gen; |
| 603 |
|
| 604 |
/* |
| 605 |
* Relocation will wait for cleaner thread, and any half-dropped |
| 606 |
* subvolume will be fully cleaned up at mount time. |
| 607 |
* So here we shouldn't hit a subvolume with non-zero drop_progress. |
| 608 |
* |
| 609 |
* If this isn't the case, error out since it can make us attempt to |
| 610 |
* drop references for extents that were already dropped before. |
| 611 |
*/ |
| 612 |
if (unlikely(btrfs_disk_key_objectid(&root->root_item.drop_progress))) { |
| 613 |
struct btrfs_key cpu_key; |
| 614 |
|
| 615 |
btrfs_disk_key_to_cpu(&cpu_key, &root->root_item.drop_progress); |
| 616 |
btrfs_err(fs_info, |
| 617 |
"cannot relocate partially dropped subvolume %llu, drop progress key " BTRFS_KEY_FMT, |
| 618 |
objectid, BTRFS_KEY_FMT_VALUE(&cpu_key)); |
| 619 |
return ERR_PTR(-EUCLEAN); |
| 620 |
} |
| 621 |
|
| 622 |
/* called by btrfs_init_reloc_root */ |
| 623 |
ret = btrfs_copy_root(trans, root, root->commit_root, &eb, |
| 624 |
BTRFS_TREE_RELOC_OBJECTID); |
| 625 |
if (ret) |
| 626 |
return ERR_PTR(ret); |
| 627 |
|
| 628 |
/* |
| 629 |
* Set the last_snapshot field to the generation of the commit |
| 630 |
* root - like this ctree.c:btrfs_block_can_be_shared() behaves |
| 631 |
* correctly (returns true) when the relocation root is created |
| 632 |
* either inside the critical section of a transaction commit |
| 633 |
* (through transaction.c:qgroup_account_snapshot()) and when |
| 634 |
* it's created before the transaction commit is started. |
| 635 |
*/ |
| 636 |
commit_root_gen = btrfs_header_generation(root->commit_root); |
| 637 |
btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen); |
| 638 |
} else { |
| 639 |
/* |
| 640 |
* called by btrfs_reloc_post_snapshot_hook. |
| 641 |
* the source tree is a reloc tree, all tree blocks |
| 642 |
* modified after it was created have RELOC flag |
| 643 |
* set in their headers. so it's OK to not update |
| 644 |
* the 'last_snapshot'. |
| 645 |
*/ |
| 646 |
ret = btrfs_copy_root(trans, root, root->node, &eb, |
| 647 |
BTRFS_TREE_RELOC_OBJECTID); |
| 648 |
if (ret) |
| 649 |
return ERR_PTR(ret); |
| 650 |
} |
| 651 |
|
| 652 |
/* |
| 653 |
* We have changed references at this point, we must abort the |
| 654 |
* transaction if anything fails (i.e. 'goto abort'). |
| 655 |
*/ |
| 656 |
|
| 657 |
memcpy(root_item, &root->root_item, sizeof(*root_item)); |
| 658 |
btrfs_set_root_bytenr(root_item, eb->start); |
| 659 |
btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
| 660 |
btrfs_set_root_generation(root_item, trans->transid); |
| 661 |
|
| 662 |
if (btrfs_root_id(root) == objectid) { |
| 663 |
btrfs_set_root_refs(root_item, 0); |
| 664 |
memset(&root_item->drop_progress, 0, |
| 665 |
sizeof(struct btrfs_disk_key)); |
| 666 |
btrfs_set_root_drop_level(root_item, 0); |
| 667 |
} |
| 668 |
|
| 669 |
btrfs_tree_unlock(eb); |
| 670 |
free_extent_buffer(eb); |
| 671 |
|
| 672 |
ret = btrfs_insert_root(trans, fs_info->tree_root, |
| 673 |
&root_key, root_item); |
| 674 |
if (ret) |
| 675 |
goto abort; |
| 676 |
|
| 677 |
reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key); |
| 678 |
if (IS_ERR(reloc_root)) { |
| 679 |
ret = PTR_ERR(reloc_root); |
| 680 |
goto abort; |
| 681 |
} |
| 682 |
set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state); |
| 683 |
btrfs_set_root_last_trans(reloc_root, trans->transid); |
| 684 |
return reloc_root; |
| 685 |
|
| 686 |
abort: |
| 687 |
btrfs_abort_transaction(trans, ret); |
| 688 |
return ERR_PTR(ret); |
| 689 |
} |
| 690 |
|
| 691 |
/* |
| 692 |
* create reloc tree for a given fs tree. reloc tree is just a |
| 693 |
* snapshot of the fs tree with special root objectid. |
| 694 |
* |
| 695 |
* The reloc_root comes out of here with two references, one for |
| 696 |
* root->reloc_root, and another for being on the rc->reloc_roots list. |
| 697 |
*/ |
| 698 |
int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, |
| 699 |
struct btrfs_root *root) |
| 700 |
{ |
| 701 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 702 |
struct btrfs_root *reloc_root; |
| 703 |
struct reloc_control *rc = fs_info->reloc_ctl; |
| 704 |
struct btrfs_block_rsv *rsv; |
| 705 |
int clear_rsv = 0; |
| 706 |
int ret; |
| 707 |
|
| 708 |
if (!rc) |
| 709 |
return 0; |
| 710 |
|
| 711 |
/* |
| 712 |
* The subvolume has reloc tree but the swap is finished, no need to |
| 713 |
* create/update the dead reloc tree |
| 714 |
*/ |
| 715 |
if (reloc_root_is_dead(root)) |
| 716 |
return 0; |
| 717 |
|
| 718 |
/* |
| 719 |
* This is subtle but important. We do not do |
| 720 |
* record_root_in_transaction for reloc roots, instead we record their |
| 721 |
* corresponding fs root, and then here we update the last trans for the |
| 722 |
* reloc root. This means that we have to do this for the entire life |
| 723 |
* of the reloc root, regardless of which stage of the relocation we are |
| 724 |
* in. |
| 725 |
*/ |
| 726 |
if (root->reloc_root) { |
| 727 |
reloc_root = root->reloc_root; |
| 728 |
btrfs_set_root_last_trans(reloc_root, trans->transid); |
| 729 |
return 0; |
| 730 |
} |
| 731 |
|
| 732 |
/* |
| 733 |
* We are merging reloc roots, we do not need new reloc trees. Also |
| 734 |
* reloc trees never need their own reloc tree. |
| 735 |
*/ |
| 736 |
if (!rc->create_reloc_tree || btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) |
| 737 |
return 0; |
| 738 |
|
| 739 |
if (!trans->reloc_reserved) { |
| 740 |
rsv = trans->block_rsv; |
| 741 |
trans->block_rsv = rc->block_rsv; |
| 742 |
clear_rsv = 1; |
| 743 |
} |
| 744 |
reloc_root = create_reloc_root(trans, root, btrfs_root_id(root)); |
| 745 |
if (clear_rsv) |
| 746 |
trans->block_rsv = rsv; |
| 747 |
if (IS_ERR(reloc_root)) |
| 748 |
return PTR_ERR(reloc_root); |
| 749 |
|
| 750 |
ret = __add_reloc_root(reloc_root); |
| 751 |
ASSERT(ret != -EEXIST); |
| 752 |
if (ret) { |
| 753 |
/* Pairs with create_reloc_root */ |
| 754 |
btrfs_put_root(reloc_root); |
| 755 |
return ret; |
| 756 |
} |
| 757 |
root->reloc_root = btrfs_grab_root(reloc_root); |
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
/* |
| 762 |
* update root item of reloc tree |
| 763 |
*/ |
| 764 |
int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, |
| 765 |
struct btrfs_root *root) |
| 766 |
{ |
| 767 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 768 |
struct btrfs_root *reloc_root; |
| 769 |
struct btrfs_root_item *root_item; |
| 770 |
int ret; |
| 771 |
|
| 772 |
if (!have_reloc_root(root)) |
| 773 |
return 0; |
| 774 |
|
| 775 |
reloc_root = root->reloc_root; |
| 776 |
root_item = &reloc_root->root_item; |
| 777 |
|
| 778 |
/* |
| 779 |
* We are probably ok here, but __del_reloc_root() will drop its ref of |
| 780 |
* the root. We have the ref for root->reloc_root, but just in case |
| 781 |
* hold it while we update the reloc root. |
| 782 |
*/ |
| 783 |
btrfs_grab_root(reloc_root); |
| 784 |
|
| 785 |
/* root->reloc_root will stay until current relocation finished */ |
| 786 |
if (fs_info->reloc_ctl && fs_info->reloc_ctl->merge_reloc_tree && |
| 787 |
btrfs_root_refs(root_item) == 0) { |
| 788 |
set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); |
| 789 |
/* |
| 790 |
* Mark the tree as dead before we change reloc_root so |
| 791 |
* have_reloc_root will not touch it from now on. |
| 792 |
*/ |
| 793 |
smp_wmb(); |
| 794 |
__del_reloc_root(reloc_root); |
| 795 |
} |
| 796 |
|
| 797 |
if (reloc_root->commit_root != reloc_root->node) { |
| 798 |
__update_reloc_root(reloc_root); |
| 799 |
btrfs_set_root_node(root_item, reloc_root->node); |
| 800 |
free_extent_buffer(reloc_root->commit_root); |
| 801 |
reloc_root->commit_root = btrfs_root_node(reloc_root); |
| 802 |
} |
| 803 |
|
| 804 |
ret = btrfs_update_root(trans, fs_info->tree_root, |
| 805 |
&reloc_root->root_key, root_item); |
| 806 |
btrfs_put_root(reloc_root); |
| 807 |
return ret; |
| 808 |
} |
| 809 |
|
| 810 |
/* |
| 811 |
* get new location of data |
| 812 |
*/ |
| 813 |
static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr, |
| 814 |
u64 bytenr, u64 num_bytes) |
| 815 |
{ |
| 816 |
struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 817 |
BTRFS_PATH_AUTO_FREE(path); |
| 818 |
struct btrfs_file_extent_item *fi; |
| 819 |
struct extent_buffer *leaf; |
| 820 |
int ret; |
| 821 |
|
| 822 |
path = btrfs_alloc_path(); |
| 823 |
if (!path) |
| 824 |
return -ENOMEM; |
| 825 |
|
| 826 |
bytenr -= BTRFS_I(reloc_inode)->reloc_block_group_start; |
| 827 |
ret = btrfs_lookup_file_extent(NULL, root, path, |
| 828 |
btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0); |
| 829 |
if (ret < 0) |
| 830 |
return ret; |
| 831 |
if (ret > 0) |
| 832 |
return -ENOENT; |
| 833 |
|
| 834 |
leaf = path->nodes[0]; |
| 835 |
fi = btrfs_item_ptr(leaf, path->slots[0], |
| 836 |
struct btrfs_file_extent_item); |
| 837 |
|
| 838 |
BUG_ON(btrfs_file_extent_offset(leaf, fi) || |
| 839 |
btrfs_file_extent_compression(leaf, fi) || |
| 840 |
btrfs_file_extent_encryption(leaf, fi) || |
| 841 |
btrfs_file_extent_other_encoding(leaf, fi)); |
| 842 |
|
| 843 |
if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) |
| 844 |
return -EINVAL; |
| 845 |
|
| 846 |
*new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 847 |
return 0; |
| 848 |
} |
| 849 |
|
| 850 |
/* |
| 851 |
* update file extent items in the tree leaf to point to |
| 852 |
* the new locations. |
| 853 |
*/ |
| 854 |
static noinline_for_stack |
| 855 |
int replace_file_extents(struct btrfs_trans_handle *trans, |
| 856 |
struct reloc_control *rc, |
| 857 |
struct btrfs_root *root, |
| 858 |
struct extent_buffer *leaf) |
| 859 |
{ |
| 860 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 861 |
struct btrfs_key key; |
| 862 |
struct btrfs_file_extent_item *fi; |
| 863 |
struct btrfs_inode *inode = NULL; |
| 864 |
u64 parent; |
| 865 |
u64 bytenr; |
| 866 |
u64 new_bytenr = 0; |
| 867 |
u64 num_bytes; |
| 868 |
u64 end; |
| 869 |
u32 nritems; |
| 870 |
u32 i; |
| 871 |
int ret = 0; |
| 872 |
int first = 1; |
| 873 |
|
| 874 |
if (rc->stage != UPDATE_DATA_PTRS) |
| 875 |
return 0; |
| 876 |
|
| 877 |
/* reloc trees always use full backref */ |
| 878 |
if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) |
| 879 |
parent = leaf->start; |
| 880 |
else |
| 881 |
parent = 0; |
| 882 |
|
| 883 |
nritems = btrfs_header_nritems(leaf); |
| 884 |
for (i = 0; i < nritems; i++) { |
| 885 |
struct btrfs_ref ref = { 0 }; |
| 886 |
|
| 887 |
cond_resched(); |
| 888 |
btrfs_item_key_to_cpu(leaf, &key, i); |
| 889 |
if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 890 |
continue; |
| 891 |
fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 892 |
if (btrfs_file_extent_type(leaf, fi) == |
| 893 |
BTRFS_FILE_EXTENT_INLINE) |
| 894 |
continue; |
| 895 |
bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 896 |
num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 897 |
if (bytenr == 0) |
| 898 |
continue; |
| 899 |
if (!in_range(bytenr, rc->block_group->start, |
| 900 |
rc->block_group->length)) |
| 901 |
continue; |
| 902 |
|
| 903 |
/* |
| 904 |
* if we are modifying block in fs tree, wait for read_folio |
| 905 |
* to complete and drop the extent cache |
| 906 |
*/ |
| 907 |
if (btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID) { |
| 908 |
if (first) { |
| 909 |
inode = btrfs_find_first_inode(root, key.objectid); |
| 910 |
first = 0; |
| 911 |
} else if (inode && btrfs_ino(inode) < key.objectid) { |
| 912 |
btrfs_add_delayed_iput(inode); |
| 913 |
inode = btrfs_find_first_inode(root, key.objectid); |
| 914 |
} |
| 915 |
if (inode && btrfs_ino(inode) == key.objectid) { |
| 916 |
struct extent_state *cached_state = NULL; |
| 917 |
|
| 918 |
end = key.offset + |
| 919 |
btrfs_file_extent_num_bytes(leaf, fi); |
| 920 |
WARN_ON(!IS_ALIGNED(key.offset, |
| 921 |
fs_info->sectorsize)); |
| 922 |
WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); |
| 923 |
end--; |
| 924 |
/* Take mmap lock to serialize with reflinks. */ |
| 925 |
if (!down_read_trylock(&inode->i_mmap_lock)) |
| 926 |
continue; |
| 927 |
ret = btrfs_try_lock_extent(&inode->io_tree, key.offset, |
| 928 |
end, &cached_state); |
| 929 |
if (!ret) { |
| 930 |
up_read(&inode->i_mmap_lock); |
| 931 |
continue; |
| 932 |
} |
| 933 |
|
| 934 |
btrfs_drop_extent_map_range(inode, key.offset, end, true); |
| 935 |
btrfs_unlock_extent(&inode->io_tree, key.offset, end, |
| 936 |
&cached_state); |
| 937 |
up_read(&inode->i_mmap_lock); |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
ret = get_new_location(rc->data_inode, &new_bytenr, |
| 942 |
bytenr, num_bytes); |
| 943 |
if (ret) { |
| 944 |
/* |
| 945 |
* Don't have to abort since we've not changed anything |
| 946 |
* in the file extent yet. |
| 947 |
*/ |
| 948 |
break; |
| 949 |
} |
| 950 |
|
| 951 |
btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr); |
| 952 |
|
| 953 |
key.offset -= btrfs_file_extent_offset(leaf, fi); |
| 954 |
ref.action = BTRFS_ADD_DELAYED_REF; |
| 955 |
ref.bytenr = new_bytenr; |
| 956 |
ref.num_bytes = num_bytes; |
| 957 |
ref.parent = parent; |
| 958 |
ref.owning_root = btrfs_root_id(root); |
| 959 |
ref.ref_root = btrfs_header_owner(leaf); |
| 960 |
btrfs_init_data_ref(&ref, key.objectid, key.offset, |
| 961 |
btrfs_root_id(root), false); |
| 962 |
ret = btrfs_inc_extent_ref(trans, &ref); |
| 963 |
if (unlikely(ret)) { |
| 964 |
btrfs_abort_transaction(trans, ret); |
| 965 |
break; |
| 966 |
} |
| 967 |
|
| 968 |
ref.action = BTRFS_DROP_DELAYED_REF; |
| 969 |
ref.bytenr = bytenr; |
| 970 |
ref.num_bytes = num_bytes; |
| 971 |
ref.parent = parent; |
| 972 |
ref.owning_root = btrfs_root_id(root); |
| 973 |
ref.ref_root = btrfs_header_owner(leaf); |
| 974 |
btrfs_init_data_ref(&ref, key.objectid, key.offset, |
| 975 |
btrfs_root_id(root), false); |
| 976 |
ret = btrfs_free_extent(trans, &ref); |
| 977 |
if (unlikely(ret)) { |
| 978 |
btrfs_abort_transaction(trans, ret); |
| 979 |
break; |
| 980 |
} |
| 981 |
} |
| 982 |
if (inode) |
| 983 |
btrfs_add_delayed_iput(inode); |
| 984 |
return ret; |
| 985 |
} |
| 986 |
|
| 987 |
static noinline_for_stack int memcmp_node_keys(const struct extent_buffer *eb, |
| 988 |
int slot, const struct btrfs_path *path, |
| 989 |
int level) |
| 990 |
{ |
| 991 |
struct btrfs_disk_key key1; |
| 992 |
struct btrfs_disk_key key2; |
| 993 |
btrfs_node_key(eb, &key1, slot); |
| 994 |
btrfs_node_key(path->nodes[level], &key2, path->slots[level]); |
| 995 |
return memcmp(&key1, &key2, sizeof(key1)); |
| 996 |
} |
| 997 |
|
| 998 |
/* |
| 999 |
* try to replace tree blocks in fs tree with the new blocks |
| 1000 |
* in reloc tree. tree blocks haven't been modified since the |
| 1001 |
* reloc tree was create can be replaced. |
| 1002 |
* |
| 1003 |
* if a block was replaced, level of the block + 1 is returned. |
| 1004 |
* if no block got replaced, 0 is returned. if there are other |
| 1005 |
* errors, a negative error number is returned. |
| 1006 |
*/ |
| 1007 |
static noinline_for_stack |
| 1008 |
int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, |
| 1009 |
struct btrfs_root *dest, struct btrfs_root *src, |
| 1010 |
struct btrfs_path *path, struct btrfs_key *next_key, |
| 1011 |
int lowest_level, int max_level) |
| 1012 |
{ |
| 1013 |
struct btrfs_fs_info *fs_info = dest->fs_info; |
| 1014 |
struct extent_buffer *eb; |
| 1015 |
struct extent_buffer *parent; |
| 1016 |
struct btrfs_ref ref = { 0 }; |
| 1017 |
struct btrfs_key key; |
| 1018 |
u64 old_bytenr; |
| 1019 |
u64 new_bytenr; |
| 1020 |
u64 old_ptr_gen; |
| 1021 |
u64 new_ptr_gen; |
| 1022 |
u64 last_snapshot; |
| 1023 |
u32 blocksize; |
| 1024 |
int cow = 0; |
| 1025 |
int level; |
| 1026 |
int ret; |
| 1027 |
int slot; |
| 1028 |
|
| 1029 |
ASSERT(btrfs_root_id(src) == BTRFS_TREE_RELOC_OBJECTID); |
| 1030 |
ASSERT(btrfs_root_id(dest) != BTRFS_TREE_RELOC_OBJECTID); |
| 1031 |
|
| 1032 |
last_snapshot = btrfs_root_last_snapshot(&src->root_item); |
| 1033 |
again: |
| 1034 |
slot = path->slots[lowest_level]; |
| 1035 |
btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot); |
| 1036 |
|
| 1037 |
eb = btrfs_lock_root_node(dest); |
| 1038 |
level = btrfs_header_level(eb); |
| 1039 |
|
| 1040 |
if (level < lowest_level) { |
| 1041 |
btrfs_tree_unlock(eb); |
| 1042 |
free_extent_buffer(eb); |
| 1043 |
return 0; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (cow) { |
| 1047 |
ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb, |
| 1048 |
BTRFS_NESTING_COW); |
| 1049 |
if (ret) { |
| 1050 |
btrfs_tree_unlock(eb); |
| 1051 |
free_extent_buffer(eb); |
| 1052 |
return ret; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
if (next_key) { |
| 1057 |
next_key->objectid = (u64)-1; |
| 1058 |
next_key->type = (u8)-1; |
| 1059 |
next_key->offset = (u64)-1; |
| 1060 |
} |
| 1061 |
|
| 1062 |
parent = eb; |
| 1063 |
while (1) { |
| 1064 |
level = btrfs_header_level(parent); |
| 1065 |
ASSERT(level >= lowest_level); |
| 1066 |
|
| 1067 |
ret = btrfs_bin_search(parent, 0, &key, &slot); |
| 1068 |
if (ret < 0) |
| 1069 |
break; |
| 1070 |
if (ret && slot > 0) |
| 1071 |
slot--; |
| 1072 |
|
| 1073 |
if (next_key && slot + 1 < btrfs_header_nritems(parent)) |
| 1074 |
btrfs_node_key_to_cpu(parent, next_key, slot + 1); |
| 1075 |
|
| 1076 |
old_bytenr = btrfs_node_blockptr(parent, slot); |
| 1077 |
blocksize = fs_info->nodesize; |
| 1078 |
old_ptr_gen = btrfs_node_ptr_generation(parent, slot); |
| 1079 |
|
| 1080 |
if (level <= max_level) { |
| 1081 |
eb = path->nodes[level]; |
| 1082 |
new_bytenr = btrfs_node_blockptr(eb, |
| 1083 |
path->slots[level]); |
| 1084 |
new_ptr_gen = btrfs_node_ptr_generation(eb, |
| 1085 |
path->slots[level]); |
| 1086 |
} else { |
| 1087 |
new_bytenr = 0; |
| 1088 |
new_ptr_gen = 0; |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) { |
| 1092 |
ret = level; |
| 1093 |
break; |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (new_bytenr == 0 || old_ptr_gen > last_snapshot || |
| 1097 |
memcmp_node_keys(parent, slot, path, level)) { |
| 1098 |
if (level <= lowest_level) { |
| 1099 |
ret = 0; |
| 1100 |
break; |
| 1101 |
} |
| 1102 |
|
| 1103 |
eb = btrfs_read_node_slot(parent, slot); |
| 1104 |
if (IS_ERR(eb)) { |
| 1105 |
ret = PTR_ERR(eb); |
| 1106 |
break; |
| 1107 |
} |
| 1108 |
btrfs_tree_lock(eb); |
| 1109 |
if (cow) { |
| 1110 |
ret = btrfs_cow_block(trans, dest, eb, parent, |
| 1111 |
slot, &eb, |
| 1112 |
BTRFS_NESTING_COW); |
| 1113 |
if (ret) { |
| 1114 |
btrfs_tree_unlock(eb); |
| 1115 |
free_extent_buffer(eb); |
| 1116 |
break; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
btrfs_tree_unlock(parent); |
| 1121 |
free_extent_buffer(parent); |
| 1122 |
|
| 1123 |
parent = eb; |
| 1124 |
continue; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (!cow) { |
| 1128 |
btrfs_tree_unlock(parent); |
| 1129 |
free_extent_buffer(parent); |
| 1130 |
cow = 1; |
| 1131 |
goto again; |
| 1132 |
} |
| 1133 |
|
| 1134 |
btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 1135 |
path->slots[level]); |
| 1136 |
btrfs_release_path(path); |
| 1137 |
|
| 1138 |
path->lowest_level = level; |
| 1139 |
set_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &src->state); |
| 1140 |
ret = btrfs_search_slot(trans, src, &key, path, 0, 1); |
| 1141 |
clear_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &src->state); |
| 1142 |
path->lowest_level = 0; |
| 1143 |
if (ret) { |
| 1144 |
if (ret > 0) |
| 1145 |
ret = -ENOENT; |
| 1146 |
break; |
| 1147 |
} |
| 1148 |
|
| 1149 |
/* |
| 1150 |
* Info qgroup to trace both subtrees. |
| 1151 |
* |
| 1152 |
* We must trace both trees. |
| 1153 |
* 1) Tree reloc subtree |
| 1154 |
* If not traced, we will leak data numbers |
| 1155 |
* 2) Fs subtree |
| 1156 |
* If not traced, we will double count old data |
| 1157 |
* |
| 1158 |
* We don't scan the subtree right now, but only record |
| 1159 |
* the swapped tree blocks. |
| 1160 |
* The real subtree rescan is delayed until we have new |
| 1161 |
* CoW on the subtree root node before transaction commit. |
| 1162 |
*/ |
| 1163 |
ret = btrfs_qgroup_add_swapped_blocks(dest, |
| 1164 |
rc->block_group, parent, slot, |
| 1165 |
path->nodes[level], path->slots[level], |
| 1166 |
last_snapshot); |
| 1167 |
if (ret < 0) |
| 1168 |
break; |
| 1169 |
/* |
| 1170 |
* swap blocks in fs tree and reloc tree. |
| 1171 |
*/ |
| 1172 |
btrfs_set_node_blockptr(parent, slot, new_bytenr); |
| 1173 |
btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen); |
| 1174 |
|
| 1175 |
btrfs_set_node_blockptr(path->nodes[level], |
| 1176 |
path->slots[level], old_bytenr); |
| 1177 |
btrfs_set_node_ptr_generation(path->nodes[level], |
| 1178 |
path->slots[level], old_ptr_gen); |
| 1179 |
|
| 1180 |
ref.action = BTRFS_ADD_DELAYED_REF; |
| 1181 |
ref.bytenr = old_bytenr; |
| 1182 |
ref.num_bytes = blocksize; |
| 1183 |
ref.parent = path->nodes[level]->start; |
| 1184 |
ref.owning_root = btrfs_root_id(src); |
| 1185 |
ref.ref_root = btrfs_root_id(src); |
| 1186 |
btrfs_init_tree_ref(&ref, level - 1, 0, true); |
| 1187 |
ret = btrfs_inc_extent_ref(trans, &ref); |
| 1188 |
if (unlikely(ret)) { |
| 1189 |
btrfs_abort_transaction(trans, ret); |
| 1190 |
break; |
| 1191 |
} |
| 1192 |
|
| 1193 |
ref.action = BTRFS_ADD_DELAYED_REF; |
| 1194 |
ref.bytenr = new_bytenr; |
| 1195 |
ref.num_bytes = blocksize; |
| 1196 |
ref.parent = 0; |
| 1197 |
ref.owning_root = btrfs_root_id(dest); |
| 1198 |
ref.ref_root = btrfs_root_id(dest); |
| 1199 |
btrfs_init_tree_ref(&ref, level - 1, 0, true); |
| 1200 |
ret = btrfs_inc_extent_ref(trans, &ref); |
| 1201 |
if (unlikely(ret)) { |
| 1202 |
btrfs_abort_transaction(trans, ret); |
| 1203 |
break; |
| 1204 |
} |
| 1205 |
|
| 1206 |
/* We don't know the real owning_root, use 0. */ |
| 1207 |
ref.action = BTRFS_DROP_DELAYED_REF; |
| 1208 |
ref.bytenr = new_bytenr; |
| 1209 |
ref.num_bytes = blocksize; |
| 1210 |
ref.parent = path->nodes[level]->start; |
| 1211 |
ref.owning_root = 0; |
| 1212 |
ref.ref_root = btrfs_root_id(src); |
| 1213 |
btrfs_init_tree_ref(&ref, level - 1, 0, true); |
| 1214 |
ret = btrfs_free_extent(trans, &ref); |
| 1215 |
if (unlikely(ret)) { |
| 1216 |
btrfs_abort_transaction(trans, ret); |
| 1217 |
break; |
| 1218 |
} |
| 1219 |
|
| 1220 |
/* We don't know the real owning_root, use 0. */ |
| 1221 |
ref.action = BTRFS_DROP_DELAYED_REF; |
| 1222 |
ref.bytenr = old_bytenr; |
| 1223 |
ref.num_bytes = blocksize; |
| 1224 |
ref.parent = 0; |
| 1225 |
ref.owning_root = 0; |
| 1226 |
ref.ref_root = btrfs_root_id(dest); |
| 1227 |
btrfs_init_tree_ref(&ref, level - 1, 0, true); |
| 1228 |
ret = btrfs_free_extent(trans, &ref); |
| 1229 |
if (unlikely(ret)) { |
| 1230 |
btrfs_abort_transaction(trans, ret); |
| 1231 |
break; |
| 1232 |
} |
| 1233 |
|
| 1234 |
btrfs_unlock_up_safe(path, 0); |
| 1235 |
|
| 1236 |
ret = level; |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
btrfs_tree_unlock(parent); |
| 1240 |
free_extent_buffer(parent); |
| 1241 |
return ret; |
| 1242 |
} |
| 1243 |
|
| 1244 |
/* |
| 1245 |
* helper to find next relocated block in reloc tree |
| 1246 |
*/ |
| 1247 |
static noinline_for_stack |
| 1248 |
int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, |
| 1249 |
int *level) |
| 1250 |
{ |
| 1251 |
struct extent_buffer *eb; |
| 1252 |
int i; |
| 1253 |
u64 last_snapshot; |
| 1254 |
u32 nritems; |
| 1255 |
|
| 1256 |
last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 1257 |
|
| 1258 |
for (i = 0; i < *level; i++) { |
| 1259 |
free_extent_buffer(path->nodes[i]); |
| 1260 |
path->nodes[i] = NULL; |
| 1261 |
} |
| 1262 |
|
| 1263 |
for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) { |
| 1264 |
eb = path->nodes[i]; |
| 1265 |
nritems = btrfs_header_nritems(eb); |
| 1266 |
while (path->slots[i] + 1 < nritems) { |
| 1267 |
path->slots[i]++; |
| 1268 |
if (btrfs_node_ptr_generation(eb, path->slots[i]) <= |
| 1269 |
last_snapshot) |
| 1270 |
continue; |
| 1271 |
|
| 1272 |
*level = i; |
| 1273 |
return 0; |
| 1274 |
} |
| 1275 |
free_extent_buffer(path->nodes[i]); |
| 1276 |
path->nodes[i] = NULL; |
| 1277 |
} |
| 1278 |
return 1; |
| 1279 |
} |
| 1280 |
|
| 1281 |
/* |
| 1282 |
* walk down reloc tree to find relocated block of lowest level |
| 1283 |
*/ |
| 1284 |
static noinline_for_stack |
| 1285 |
int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, |
| 1286 |
int *level) |
| 1287 |
{ |
| 1288 |
struct extent_buffer *eb = NULL; |
| 1289 |
int i; |
| 1290 |
u64 ptr_gen = 0; |
| 1291 |
u64 last_snapshot; |
| 1292 |
u32 nritems; |
| 1293 |
|
| 1294 |
last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
| 1295 |
|
| 1296 |
for (i = *level; i > 0; i--) { |
| 1297 |
eb = path->nodes[i]; |
| 1298 |
nritems = btrfs_header_nritems(eb); |
| 1299 |
while (path->slots[i] < nritems) { |
| 1300 |
ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]); |
| 1301 |
if (ptr_gen > last_snapshot) |
| 1302 |
break; |
| 1303 |
path->slots[i]++; |
| 1304 |
} |
| 1305 |
if (path->slots[i] >= nritems) { |
| 1306 |
if (i == *level) |
| 1307 |
break; |
| 1308 |
*level = i + 1; |
| 1309 |
return 0; |
| 1310 |
} |
| 1311 |
if (i == 1) { |
| 1312 |
*level = i; |
| 1313 |
return 0; |
| 1314 |
} |
| 1315 |
|
| 1316 |
eb = btrfs_read_node_slot(eb, path->slots[i]); |
| 1317 |
if (IS_ERR(eb)) |
| 1318 |
return PTR_ERR(eb); |
| 1319 |
BUG_ON(btrfs_header_level(eb) != i - 1); |
| 1320 |
path->nodes[i - 1] = eb; |
| 1321 |
path->slots[i - 1] = 0; |
| 1322 |
} |
| 1323 |
return 1; |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* |
| 1327 |
* invalidate extent cache for file extents whose key in range of |
| 1328 |
* [min_key, max_key) |
| 1329 |
*/ |
| 1330 |
static int invalidate_extent_cache(struct btrfs_root *root, |
| 1331 |
const struct btrfs_key *min_key, |
| 1332 |
const struct btrfs_key *max_key) |
| 1333 |
{ |
| 1334 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 1335 |
struct btrfs_inode *inode = NULL; |
| 1336 |
u64 objectid; |
| 1337 |
u64 start, end; |
| 1338 |
u64 ino; |
| 1339 |
|
| 1340 |
objectid = min_key->objectid; |
| 1341 |
while (1) { |
| 1342 |
struct extent_state *cached_state = NULL; |
| 1343 |
|
| 1344 |
cond_resched(); |
| 1345 |
if (inode) |
| 1346 |
iput(&inode->vfs_inode); |
| 1347 |
|
| 1348 |
if (objectid > max_key->objectid) |
| 1349 |
break; |
| 1350 |
|
| 1351 |
inode = btrfs_find_first_inode(root, objectid); |
| 1352 |
if (!inode) |
| 1353 |
break; |
| 1354 |
ino = btrfs_ino(inode); |
| 1355 |
|
| 1356 |
if (ino > max_key->objectid) { |
| 1357 |
iput(&inode->vfs_inode); |
| 1358 |
break; |
| 1359 |
} |
| 1360 |
|
| 1361 |
objectid = ino + 1; |
| 1362 |
if (!S_ISREG(inode->vfs_inode.i_mode)) |
| 1363 |
continue; |
| 1364 |
|
| 1365 |
if (unlikely(min_key->objectid == ino)) { |
| 1366 |
if (min_key->type > BTRFS_EXTENT_DATA_KEY) |
| 1367 |
continue; |
| 1368 |
if (min_key->type < BTRFS_EXTENT_DATA_KEY) |
| 1369 |
start = 0; |
| 1370 |
else { |
| 1371 |
start = min_key->offset; |
| 1372 |
WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize)); |
| 1373 |
} |
| 1374 |
} else { |
| 1375 |
start = 0; |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (unlikely(max_key->objectid == ino)) { |
| 1379 |
if (max_key->type < BTRFS_EXTENT_DATA_KEY) |
| 1380 |
continue; |
| 1381 |
if (max_key->type > BTRFS_EXTENT_DATA_KEY) { |
| 1382 |
end = (u64)-1; |
| 1383 |
} else { |
| 1384 |
if (max_key->offset == 0) |
| 1385 |
continue; |
| 1386 |
end = max_key->offset; |
| 1387 |
WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); |
| 1388 |
end--; |
| 1389 |
} |
| 1390 |
} else { |
| 1391 |
end = (u64)-1; |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* the lock_extent waits for read_folio to complete */ |
| 1395 |
btrfs_lock_extent(&inode->io_tree, start, end, &cached_state); |
| 1396 |
btrfs_drop_extent_map_range(inode, start, end, true); |
| 1397 |
btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state); |
| 1398 |
} |
| 1399 |
return 0; |
| 1400 |
} |
| 1401 |
|
| 1402 |
static int find_next_key(struct btrfs_path *path, int level, |
| 1403 |
struct btrfs_key *key) |
| 1404 |
|
| 1405 |
{ |
| 1406 |
while (level < BTRFS_MAX_LEVEL) { |
| 1407 |
if (!path->nodes[level]) |
| 1408 |
break; |
| 1409 |
if (path->slots[level] + 1 < |
| 1410 |
btrfs_header_nritems(path->nodes[level])) { |
| 1411 |
btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1412 |
path->slots[level] + 1); |
| 1413 |
return 0; |
| 1414 |
} |
| 1415 |
level++; |
| 1416 |
} |
| 1417 |
return 1; |
| 1418 |
} |
| 1419 |
|
| 1420 |
/* |
| 1421 |
* Insert current subvolume into reloc_control::dirty_subvol_roots |
| 1422 |
*/ |
| 1423 |
static int insert_dirty_subvol(struct btrfs_trans_handle *trans, |
| 1424 |
struct reloc_control *rc, |
| 1425 |
struct btrfs_root *root) |
| 1426 |
{ |
| 1427 |
struct btrfs_root *reloc_root = root->reloc_root; |
| 1428 |
struct btrfs_root_item *reloc_root_item; |
| 1429 |
int ret; |
| 1430 |
|
| 1431 |
/* @root must be a subvolume tree root with a valid reloc tree */ |
| 1432 |
ASSERT(btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID); |
| 1433 |
ASSERT(reloc_root); |
| 1434 |
|
| 1435 |
reloc_root_item = &reloc_root->root_item; |
| 1436 |
memset(&reloc_root_item->drop_progress, 0, |
| 1437 |
sizeof(reloc_root_item->drop_progress)); |
| 1438 |
btrfs_set_root_drop_level(reloc_root_item, 0); |
| 1439 |
btrfs_set_root_refs(reloc_root_item, 0); |
| 1440 |
ret = btrfs_update_reloc_root(trans, root); |
| 1441 |
if (ret) |
| 1442 |
return ret; |
| 1443 |
|
| 1444 |
if (list_empty(&root->reloc_dirty_list)) { |
| 1445 |
btrfs_grab_root(root); |
| 1446 |
list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots); |
| 1447 |
} |
| 1448 |
|
| 1449 |
return 0; |
| 1450 |
} |
| 1451 |
|
| 1452 |
static int clean_dirty_subvols(struct reloc_control *rc) |
| 1453 |
{ |
| 1454 |
struct btrfs_root *root; |
| 1455 |
struct btrfs_root *next; |
| 1456 |
int ret = 0; |
| 1457 |
int ret2; |
| 1458 |
|
| 1459 |
list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots, |
| 1460 |
reloc_dirty_list) { |
| 1461 |
if (btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID) { |
| 1462 |
/* Merged subvolume, cleanup its reloc root */ |
| 1463 |
struct btrfs_root *reloc_root = root->reloc_root; |
| 1464 |
|
| 1465 |
list_del_init(&root->reloc_dirty_list); |
| 1466 |
root->reloc_root = NULL; |
| 1467 |
/* |
| 1468 |
* Need barrier to ensure clear_bit() only happens after |
| 1469 |
* root->reloc_root = NULL. Pairs with have_reloc_root. |
| 1470 |
*/ |
| 1471 |
smp_wmb(); |
| 1472 |
clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); |
| 1473 |
if (reloc_root) { |
| 1474 |
/* |
| 1475 |
* btrfs_drop_snapshot drops our ref we hold for |
| 1476 |
* ->reloc_root. If it fails however we must |
| 1477 |
* drop the ref ourselves. |
| 1478 |
*/ |
| 1479 |
ret2 = btrfs_drop_snapshot(reloc_root, false, true); |
| 1480 |
if (ret2 < 0) { |
| 1481 |
btrfs_put_root(reloc_root); |
| 1482 |
if (!ret) |
| 1483 |
ret = ret2; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
btrfs_put_root(root); |
| 1487 |
} else { |
| 1488 |
/* Orphan reloc tree, just clean it up */ |
| 1489 |
ret2 = btrfs_drop_snapshot(root, false, true); |
| 1490 |
if (ret2 < 0) { |
| 1491 |
btrfs_put_root(root); |
| 1492 |
if (!ret) |
| 1493 |
ret = ret2; |
| 1494 |
} |
| 1495 |
} |
| 1496 |
} |
| 1497 |
return ret; |
| 1498 |
} |
| 1499 |
|
| 1500 |
/* |
| 1501 |
* merge the relocated tree blocks in reloc tree with corresponding |
| 1502 |
* fs tree. |
| 1503 |
*/ |
| 1504 |
static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, |
| 1505 |
struct btrfs_root *root) |
| 1506 |
{ |
| 1507 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 1508 |
struct btrfs_key key; |
| 1509 |
struct btrfs_key next_key; |
| 1510 |
struct btrfs_trans_handle *trans = NULL; |
| 1511 |
struct btrfs_root *reloc_root; |
| 1512 |
struct btrfs_root_item *root_item; |
| 1513 |
struct btrfs_path *path; |
| 1514 |
struct extent_buffer *leaf; |
| 1515 |
int reserve_level; |
| 1516 |
int level; |
| 1517 |
int max_level; |
| 1518 |
int replaced = 0; |
| 1519 |
int ret = 0; |
| 1520 |
u32 min_reserved; |
| 1521 |
|
| 1522 |
path = btrfs_alloc_path(); |
| 1523 |
if (!path) |
| 1524 |
return -ENOMEM; |
| 1525 |
path->reada = READA_FORWARD; |
| 1526 |
|
| 1527 |
reloc_root = root->reloc_root; |
| 1528 |
root_item = &reloc_root->root_item; |
| 1529 |
|
| 1530 |
if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 1531 |
level = btrfs_root_level(root_item); |
| 1532 |
refcount_inc(&reloc_root->node->refs); |
| 1533 |
path->nodes[level] = reloc_root->node; |
| 1534 |
path->slots[level] = 0; |
| 1535 |
} else { |
| 1536 |
btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
| 1537 |
|
| 1538 |
level = btrfs_root_drop_level(root_item); |
| 1539 |
BUG_ON(level == 0); |
| 1540 |
path->lowest_level = level; |
| 1541 |
ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0); |
| 1542 |
path->lowest_level = 0; |
| 1543 |
if (ret < 0) { |
| 1544 |
btrfs_free_path(path); |
| 1545 |
return ret; |
| 1546 |
} |
| 1547 |
|
| 1548 |
btrfs_node_key_to_cpu(path->nodes[level], &next_key, |
| 1549 |
path->slots[level]); |
| 1550 |
WARN_ON(memcmp(&key, &next_key, sizeof(key))); |
| 1551 |
|
| 1552 |
btrfs_unlock_up_safe(path, 0); |
| 1553 |
} |
| 1554 |
|
| 1555 |
/* |
| 1556 |
* In merge_reloc_root(), we modify the upper level pointer to swap the |
| 1557 |
* tree blocks between reloc tree and subvolume tree. Thus for tree |
| 1558 |
* block COW, we COW at most from level 1 to root level for each tree. |
| 1559 |
* |
| 1560 |
* Thus the needed metadata size is at most root_level * nodesize, |
| 1561 |
* and * 2 since we have two trees to COW. |
| 1562 |
*/ |
| 1563 |
reserve_level = max_t(int, 1, btrfs_root_level(root_item)); |
| 1564 |
min_reserved = fs_info->nodesize * reserve_level * 2; |
| 1565 |
memset(&next_key, 0, sizeof(next_key)); |
| 1566 |
|
| 1567 |
while (1) { |
| 1568 |
ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv, |
| 1569 |
min_reserved, |
| 1570 |
BTRFS_RESERVE_FLUSH_LIMIT); |
| 1571 |
if (ret) |
| 1572 |
goto out; |
| 1573 |
trans = btrfs_start_transaction(root, 0); |
| 1574 |
if (IS_ERR(trans)) { |
| 1575 |
ret = PTR_ERR(trans); |
| 1576 |
trans = NULL; |
| 1577 |
goto out; |
| 1578 |
} |
| 1579 |
|
| 1580 |
/* |
| 1581 |
* At this point we no longer have a reloc_control, so we can't |
| 1582 |
* depend on btrfs_init_reloc_root to update our last_trans. |
| 1583 |
* |
| 1584 |
* But that's ok, we started the trans handle on our |
| 1585 |
* corresponding fs_root, which means it's been added to the |
| 1586 |
* dirty list. At commit time we'll still call |
| 1587 |
* btrfs_update_reloc_root() and update our root item |
| 1588 |
* appropriately. |
| 1589 |
*/ |
| 1590 |
btrfs_set_root_last_trans(reloc_root, trans->transid); |
| 1591 |
trans->block_rsv = rc->block_rsv; |
| 1592 |
|
| 1593 |
replaced = 0; |
| 1594 |
max_level = level; |
| 1595 |
|
| 1596 |
ret = walk_down_reloc_tree(reloc_root, path, &level); |
| 1597 |
if (ret < 0) |
| 1598 |
goto out; |
| 1599 |
if (ret > 0) |
| 1600 |
break; |
| 1601 |
|
| 1602 |
if (!find_next_key(path, level, &key) && |
| 1603 |
btrfs_comp_cpu_keys(&next_key, &key) >= 0) { |
| 1604 |
ret = 0; |
| 1605 |
} else { |
| 1606 |
ret = replace_path(trans, rc, root, reloc_root, path, |
| 1607 |
&next_key, level, max_level); |
| 1608 |
} |
| 1609 |
if (ret < 0) |
| 1610 |
goto out; |
| 1611 |
if (ret > 0) { |
| 1612 |
level = ret; |
| 1613 |
btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 1614 |
path->slots[level]); |
| 1615 |
replaced = 1; |
| 1616 |
} |
| 1617 |
|
| 1618 |
ret = walk_up_reloc_tree(reloc_root, path, &level); |
| 1619 |
if (ret > 0) |
| 1620 |
break; |
| 1621 |
|
| 1622 |
BUG_ON(level == 0); |
| 1623 |
/* |
| 1624 |
* save the merging progress in the drop_progress. |
| 1625 |
* this is OK since root refs == 1 in this case. |
| 1626 |
*/ |
| 1627 |
btrfs_node_key(path->nodes[level], &root_item->drop_progress, |
| 1628 |
path->slots[level]); |
| 1629 |
btrfs_set_root_drop_level(root_item, level); |
| 1630 |
|
| 1631 |
btrfs_end_transaction_throttle(trans); |
| 1632 |
trans = NULL; |
| 1633 |
|
| 1634 |
btrfs_btree_balance_dirty(fs_info); |
| 1635 |
|
| 1636 |
if (replaced && rc->stage == UPDATE_DATA_PTRS) |
| 1637 |
invalidate_extent_cache(root, &key, &next_key); |
| 1638 |
} |
| 1639 |
|
| 1640 |
/* |
| 1641 |
* handle the case only one block in the fs tree need to be |
| 1642 |
* relocated and the block is tree root. |
| 1643 |
*/ |
| 1644 |
leaf = btrfs_lock_root_node(root); |
| 1645 |
ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf, |
| 1646 |
BTRFS_NESTING_COW); |
| 1647 |
btrfs_tree_unlock(leaf); |
| 1648 |
free_extent_buffer(leaf); |
| 1649 |
out: |
| 1650 |
btrfs_free_path(path); |
| 1651 |
|
| 1652 |
if (ret == 0) { |
| 1653 |
ret = insert_dirty_subvol(trans, rc, root); |
| 1654 |
if (ret) |
| 1655 |
btrfs_abort_transaction(trans, ret); |
| 1656 |
} |
| 1657 |
|
| 1658 |
if (trans) |
| 1659 |
btrfs_end_transaction_throttle(trans); |
| 1660 |
|
| 1661 |
btrfs_btree_balance_dirty(fs_info); |
| 1662 |
|
| 1663 |
if (replaced && rc->stage == UPDATE_DATA_PTRS) |
| 1664 |
invalidate_extent_cache(root, &key, &next_key); |
| 1665 |
|
| 1666 |
return ret; |
| 1667 |
} |
| 1668 |
|
| 1669 |
static noinline_for_stack |
| 1670 |
int prepare_to_merge(struct reloc_control *rc, int err) |
| 1671 |
{ |
| 1672 |
struct btrfs_root *root = rc->extent_root; |
| 1673 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 1674 |
struct btrfs_root *reloc_root; |
| 1675 |
struct btrfs_trans_handle *trans; |
| 1676 |
LIST_HEAD(reloc_roots); |
| 1677 |
u64 num_bytes = 0; |
| 1678 |
int ret; |
| 1679 |
|
| 1680 |
mutex_lock(&fs_info->reloc_mutex); |
| 1681 |
rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; |
| 1682 |
rc->merging_rsv_size += rc->nodes_relocated * 2; |
| 1683 |
mutex_unlock(&fs_info->reloc_mutex); |
| 1684 |
|
| 1685 |
again: |
| 1686 |
if (!err) { |
| 1687 |
num_bytes = rc->merging_rsv_size; |
| 1688 |
ret = btrfs_block_rsv_add(fs_info, rc->block_rsv, num_bytes, |
| 1689 |
BTRFS_RESERVE_FLUSH_ALL); |
| 1690 |
if (ret) |
| 1691 |
err = ret; |
| 1692 |
} |
| 1693 |
|
| 1694 |
trans = btrfs_join_transaction(rc->extent_root); |
| 1695 |
if (IS_ERR(trans)) { |
| 1696 |
if (!err) |
| 1697 |
btrfs_block_rsv_release(fs_info, rc->block_rsv, |
| 1698 |
num_bytes, NULL); |
| 1699 |
return PTR_ERR(trans); |
| 1700 |
} |
| 1701 |
|
| 1702 |
if (!err) { |
| 1703 |
if (num_bytes != rc->merging_rsv_size) { |
| 1704 |
btrfs_end_transaction(trans); |
| 1705 |
btrfs_block_rsv_release(fs_info, rc->block_rsv, |
| 1706 |
num_bytes, NULL); |
| 1707 |
goto again; |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
rc->merge_reloc_tree = true; |
| 1712 |
|
| 1713 |
while (!list_empty(&rc->reloc_roots)) { |
| 1714 |
reloc_root = list_first_entry(&rc->reloc_roots, |
| 1715 |
struct btrfs_root, root_list); |
| 1716 |
list_del_init(&reloc_root->root_list); |
| 1717 |
|
| 1718 |
root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, |
| 1719 |
false); |
| 1720 |
if (IS_ERR(root)) { |
| 1721 |
/* |
| 1722 |
* Even if we have an error we need this reloc root |
| 1723 |
* back on our list so we can clean up properly. |
| 1724 |
*/ |
| 1725 |
list_add(&reloc_root->root_list, &reloc_roots); |
| 1726 |
btrfs_abort_transaction(trans, (int)PTR_ERR(root)); |
| 1727 |
if (!err) |
| 1728 |
err = PTR_ERR(root); |
| 1729 |
break; |
| 1730 |
} |
| 1731 |
|
| 1732 |
if (unlikely(root->reloc_root != reloc_root)) { |
| 1733 |
if (root->reloc_root) { |
| 1734 |
btrfs_err(fs_info, |
| 1735 |
"reloc tree mismatch, root %lld has reloc root key (%lld %u %llu) gen %llu, expect reloc root key (%lld %u %llu) gen %llu", |
| 1736 |
btrfs_root_id(root), |
| 1737 |
btrfs_root_id(root->reloc_root), |
| 1738 |
root->reloc_root->root_key.type, |
| 1739 |
root->reloc_root->root_key.offset, |
| 1740 |
btrfs_root_generation( |
| 1741 |
&root->reloc_root->root_item), |
| 1742 |
btrfs_root_id(reloc_root), |
| 1743 |
reloc_root->root_key.type, |
| 1744 |
reloc_root->root_key.offset, |
| 1745 |
btrfs_root_generation( |
| 1746 |
&reloc_root->root_item)); |
| 1747 |
} else { |
| 1748 |
btrfs_err(fs_info, |
| 1749 |
"reloc tree mismatch, root %lld has no reloc root, expect reloc root key (%lld %u %llu) gen %llu", |
| 1750 |
btrfs_root_id(root), |
| 1751 |
btrfs_root_id(reloc_root), |
| 1752 |
reloc_root->root_key.type, |
| 1753 |
reloc_root->root_key.offset, |
| 1754 |
btrfs_root_generation( |
| 1755 |
&reloc_root->root_item)); |
| 1756 |
} |
| 1757 |
list_add(&reloc_root->root_list, &reloc_roots); |
| 1758 |
btrfs_put_root(root); |
| 1759 |
btrfs_abort_transaction(trans, -EUCLEAN); |
| 1760 |
if (!err) |
| 1761 |
err = -EUCLEAN; |
| 1762 |
break; |
| 1763 |
} |
| 1764 |
|
| 1765 |
/* |
| 1766 |
* set reference count to 1, so btrfs_recover_relocation |
| 1767 |
* knows it should resumes merging |
| 1768 |
*/ |
| 1769 |
if (!err) |
| 1770 |
btrfs_set_root_refs(&reloc_root->root_item, 1); |
| 1771 |
ret = btrfs_update_reloc_root(trans, root); |
| 1772 |
|
| 1773 |
/* |
| 1774 |
* Even if we have an error we need this reloc root back on our |
| 1775 |
* list so we can clean up properly. |
| 1776 |
*/ |
| 1777 |
list_add(&reloc_root->root_list, &reloc_roots); |
| 1778 |
btrfs_put_root(root); |
| 1779 |
|
| 1780 |
if (unlikely(ret)) { |
| 1781 |
btrfs_abort_transaction(trans, ret); |
| 1782 |
if (!err) |
| 1783 |
err = ret; |
| 1784 |
break; |
| 1785 |
} |
| 1786 |
} |
| 1787 |
|
| 1788 |
list_splice(&reloc_roots, &rc->reloc_roots); |
| 1789 |
|
| 1790 |
if (!err) |
| 1791 |
err = btrfs_commit_transaction(trans); |
| 1792 |
else |
| 1793 |
btrfs_end_transaction(trans); |
| 1794 |
return err; |
| 1795 |
} |
| 1796 |
|
| 1797 |
static noinline_for_stack |
| 1798 |
void free_reloc_roots(struct list_head *list) |
| 1799 |
{ |
| 1800 |
struct btrfs_root *reloc_root, *tmp; |
| 1801 |
|
| 1802 |
list_for_each_entry_safe(reloc_root, tmp, list, root_list) |
| 1803 |
__del_reloc_root(reloc_root); |
| 1804 |
} |
| 1805 |
|
| 1806 |
static noinline_for_stack |
| 1807 |
void merge_reloc_roots(struct reloc_control *rc) |
| 1808 |
{ |
| 1809 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 1810 |
struct btrfs_root *root; |
| 1811 |
struct btrfs_root *reloc_root; |
| 1812 |
LIST_HEAD(reloc_roots); |
| 1813 |
int found = 0; |
| 1814 |
int ret = 0; |
| 1815 |
again: |
| 1816 |
root = rc->extent_root; |
| 1817 |
|
| 1818 |
/* |
| 1819 |
* this serializes us with btrfs_record_root_in_transaction, |
| 1820 |
* we have to make sure nobody is in the middle of |
| 1821 |
* adding their roots to the list while we are |
| 1822 |
* doing this splice |
| 1823 |
*/ |
| 1824 |
mutex_lock(&fs_info->reloc_mutex); |
| 1825 |
list_splice_init(&rc->reloc_roots, &reloc_roots); |
| 1826 |
mutex_unlock(&fs_info->reloc_mutex); |
| 1827 |
|
| 1828 |
while (!list_empty(&reloc_roots)) { |
| 1829 |
found = 1; |
| 1830 |
reloc_root = list_first_entry(&reloc_roots, struct btrfs_root, root_list); |
| 1831 |
|
| 1832 |
root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, |
| 1833 |
false); |
| 1834 |
if (btrfs_root_refs(&reloc_root->root_item) > 0) { |
| 1835 |
if (WARN_ON(IS_ERR(root))) { |
| 1836 |
/* |
| 1837 |
* For recovery we read the fs roots on mount, |
| 1838 |
* and if we didn't find the root then we marked |
| 1839 |
* the reloc root as a garbage root. For normal |
| 1840 |
* relocation obviously the root should exist in |
| 1841 |
* memory. However there's no reason we can't |
| 1842 |
* handle the error properly here just in case. |
| 1843 |
*/ |
| 1844 |
ret = PTR_ERR(root); |
| 1845 |
goto out; |
| 1846 |
} |
| 1847 |
if (WARN_ON(root->reloc_root != reloc_root)) { |
| 1848 |
/* |
| 1849 |
* This can happen if on-disk metadata has some |
| 1850 |
* corruption, e.g. bad reloc tree key offset. |
| 1851 |
*/ |
| 1852 |
ret = -EINVAL; |
| 1853 |
goto out; |
| 1854 |
} |
| 1855 |
ret = merge_reloc_root(rc, root); |
| 1856 |
btrfs_put_root(root); |
| 1857 |
if (ret) { |
| 1858 |
if (list_empty(&reloc_root->root_list)) |
| 1859 |
list_add_tail(&reloc_root->root_list, |
| 1860 |
&reloc_roots); |
| 1861 |
goto out; |
| 1862 |
} |
| 1863 |
} else { |
| 1864 |
if (!IS_ERR(root)) { |
| 1865 |
if (root->reloc_root == reloc_root) { |
| 1866 |
root->reloc_root = NULL; |
| 1867 |
btrfs_put_root(reloc_root); |
| 1868 |
} |
| 1869 |
clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, |
| 1870 |
&root->state); |
| 1871 |
btrfs_put_root(root); |
| 1872 |
} |
| 1873 |
|
| 1874 |
list_del_init(&reloc_root->root_list); |
| 1875 |
/* Don't forget to queue this reloc root for cleanup */ |
| 1876 |
list_add_tail(&reloc_root->reloc_dirty_list, |
| 1877 |
&rc->dirty_subvol_roots); |
| 1878 |
} |
| 1879 |
} |
| 1880 |
|
| 1881 |
if (found) { |
| 1882 |
found = 0; |
| 1883 |
goto again; |
| 1884 |
} |
| 1885 |
out: |
| 1886 |
if (ret) { |
| 1887 |
btrfs_handle_fs_error(fs_info, ret, NULL); |
| 1888 |
free_reloc_roots(&reloc_roots); |
| 1889 |
|
| 1890 |
/* new reloc root may be added */ |
| 1891 |
mutex_lock(&fs_info->reloc_mutex); |
| 1892 |
list_splice_init(&rc->reloc_roots, &reloc_roots); |
| 1893 |
mutex_unlock(&fs_info->reloc_mutex); |
| 1894 |
free_reloc_roots(&reloc_roots); |
| 1895 |
} |
| 1896 |
|
| 1897 |
/* |
| 1898 |
* We used to have |
| 1899 |
* |
| 1900 |
* BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); |
| 1901 |
* |
| 1902 |
* here, but it's wrong. If we fail to start the transaction in |
| 1903 |
* prepare_to_merge() we will have only 0 ref reloc roots, none of which |
| 1904 |
* have actually been removed from the reloc_root_tree rb tree. This is |
| 1905 |
* fine because we're bailing here, and we hold a reference on the root |
| 1906 |
* for the list that holds it, so these roots will be cleaned up when we |
| 1907 |
* do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root |
| 1908 |
* will be cleaned up on unmount. |
| 1909 |
* |
| 1910 |
* The remaining nodes will be cleaned up by free_reloc_control. |
| 1911 |
*/ |
| 1912 |
} |
| 1913 |
|
| 1914 |
static void free_block_list(struct rb_root *blocks) |
| 1915 |
{ |
| 1916 |
struct tree_block *block; |
| 1917 |
struct rb_node *rb_node; |
| 1918 |
while ((rb_node = rb_first(blocks))) { |
| 1919 |
block = rb_entry(rb_node, struct tree_block, rb_node); |
| 1920 |
rb_erase(rb_node, blocks); |
| 1921 |
kfree(block); |
| 1922 |
} |
| 1923 |
} |
| 1924 |
|
| 1925 |
static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, |
| 1926 |
struct btrfs_root *reloc_root) |
| 1927 |
{ |
| 1928 |
struct btrfs_fs_info *fs_info = reloc_root->fs_info; |
| 1929 |
struct btrfs_root *root; |
| 1930 |
int ret; |
| 1931 |
|
| 1932 |
if (btrfs_get_root_last_trans(reloc_root) == trans->transid) |
| 1933 |
return 0; |
| 1934 |
|
| 1935 |
root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false); |
| 1936 |
|
| 1937 |
/* |
| 1938 |
* This should succeed, since we can't have a reloc root without having |
| 1939 |
* already looked up the actual root and created the reloc root for this |
| 1940 |
* root. |
| 1941 |
* |
| 1942 |
* However if there's some sort of corruption where we have a ref to a |
| 1943 |
* reloc root without a corresponding root this could return ENOENT. |
| 1944 |
*/ |
| 1945 |
if (IS_ERR(root)) { |
| 1946 |
DEBUG_WARN("error %ld reading root for reloc root", PTR_ERR(root)); |
| 1947 |
return PTR_ERR(root); |
| 1948 |
} |
| 1949 |
if (unlikely(root->reloc_root != reloc_root)) { |
| 1950 |
DEBUG_WARN("unexpected reloc root found"); |
| 1951 |
btrfs_err(fs_info, |
| 1952 |
"root %llu has two reloc roots associated with it", |
| 1953 |
reloc_root->root_key.offset); |
| 1954 |
btrfs_put_root(root); |
| 1955 |
return -EUCLEAN; |
| 1956 |
} |
| 1957 |
ret = btrfs_record_root_in_trans(trans, root); |
| 1958 |
btrfs_put_root(root); |
| 1959 |
|
| 1960 |
return ret; |
| 1961 |
} |
| 1962 |
|
| 1963 |
static noinline_for_stack |
| 1964 |
struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, |
| 1965 |
struct reloc_control *rc, |
| 1966 |
struct btrfs_backref_node *node, |
| 1967 |
struct btrfs_backref_edge *edges[]) |
| 1968 |
{ |
| 1969 |
struct btrfs_backref_node *next; |
| 1970 |
struct btrfs_root *root; |
| 1971 |
int index = 0; |
| 1972 |
int ret; |
| 1973 |
|
| 1974 |
next = walk_up_backref(node, edges, &index); |
| 1975 |
root = next->root; |
| 1976 |
|
| 1977 |
/* |
| 1978 |
* If there is no root, then our references for this block are |
| 1979 |
* incomplete, as we should be able to walk all the way up to a block |
| 1980 |
* that is owned by a root. |
| 1981 |
* |
| 1982 |
* This path is only for SHAREABLE roots, so if we come upon a |
| 1983 |
* non-SHAREABLE root then we have backrefs that resolve improperly. |
| 1984 |
* |
| 1985 |
* Both of these cases indicate file system corruption, or a bug in the |
| 1986 |
* backref walking code. |
| 1987 |
*/ |
| 1988 |
if (unlikely(!root)) { |
| 1989 |
btrfs_err(trans->fs_info, |
| 1990 |
"bytenr %llu doesn't have a backref path ending in a root", |
| 1991 |
node->bytenr); |
| 1992 |
return ERR_PTR(-EUCLEAN); |
| 1993 |
} |
| 1994 |
if (unlikely(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))) { |
| 1995 |
btrfs_err(trans->fs_info, |
| 1996 |
"bytenr %llu has multiple refs with one ending in a non-shareable root", |
| 1997 |
node->bytenr); |
| 1998 |
return ERR_PTR(-EUCLEAN); |
| 1999 |
} |
| 2000 |
|
| 2001 |
if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) { |
| 2002 |
ret = record_reloc_root_in_trans(trans, root); |
| 2003 |
if (ret) |
| 2004 |
return ERR_PTR(ret); |
| 2005 |
goto found; |
| 2006 |
} |
| 2007 |
|
| 2008 |
ret = btrfs_record_root_in_trans(trans, root); |
| 2009 |
if (ret) |
| 2010 |
return ERR_PTR(ret); |
| 2011 |
root = root->reloc_root; |
| 2012 |
|
| 2013 |
/* |
| 2014 |
* We could have raced with another thread which failed, so |
| 2015 |
* root->reloc_root may not be set, return ENOENT in this case. |
| 2016 |
*/ |
| 2017 |
if (!root) |
| 2018 |
return ERR_PTR(-ENOENT); |
| 2019 |
|
| 2020 |
if (unlikely(next->new_bytenr)) { |
| 2021 |
/* |
| 2022 |
* We just created the reloc root, so we shouldn't have |
| 2023 |
* ->new_bytenr set yet. If it is then we have multiple roots |
| 2024 |
* pointing at the same bytenr which indicates corruption, or |
| 2025 |
* we've made a mistake in the backref walking code. |
| 2026 |
*/ |
| 2027 |
ASSERT(next->new_bytenr == 0); |
| 2028 |
btrfs_err(trans->fs_info, |
| 2029 |
"bytenr %llu possibly has multiple roots pointing at the same bytenr %llu", |
| 2030 |
node->bytenr, next->bytenr); |
| 2031 |
return ERR_PTR(-EUCLEAN); |
| 2032 |
} |
| 2033 |
|
| 2034 |
next->new_bytenr = root->node->start; |
| 2035 |
btrfs_put_root(next->root); |
| 2036 |
next->root = btrfs_grab_root(root); |
| 2037 |
ASSERT(next->root); |
| 2038 |
mark_block_processed(rc, next); |
| 2039 |
found: |
| 2040 |
next = node; |
| 2041 |
/* setup backref node path for btrfs_reloc_cow_block */ |
| 2042 |
while (1) { |
| 2043 |
rc->backref_cache.path[next->level] = next; |
| 2044 |
if (--index < 0) |
| 2045 |
break; |
| 2046 |
next = edges[index]->node[UPPER]; |
| 2047 |
} |
| 2048 |
return root; |
| 2049 |
} |
| 2050 |
|
| 2051 |
/* |
| 2052 |
* Select a tree root for relocation. |
| 2053 |
* |
| 2054 |
* Return NULL if the block is not shareable. We should use do_relocation() in |
| 2055 |
* this case. |
| 2056 |
* |
| 2057 |
* Return a tree root pointer if the block is shareable. |
| 2058 |
* Return -ENOENT if the block is root of reloc tree. |
| 2059 |
*/ |
| 2060 |
static noinline_for_stack |
| 2061 |
struct btrfs_root *select_one_root(struct btrfs_backref_node *node) |
| 2062 |
{ |
| 2063 |
struct btrfs_backref_node *next; |
| 2064 |
struct btrfs_root *root; |
| 2065 |
struct btrfs_root *fs_root = NULL; |
| 2066 |
struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2067 |
int index = 0; |
| 2068 |
|
| 2069 |
next = node; |
| 2070 |
while (1) { |
| 2071 |
cond_resched(); |
| 2072 |
next = walk_up_backref(next, edges, &index); |
| 2073 |
root = next->root; |
| 2074 |
|
| 2075 |
/* |
| 2076 |
* This can occur if we have incomplete extent refs leading all |
| 2077 |
* the way up a particular path, in this case return -EUCLEAN. |
| 2078 |
*/ |
| 2079 |
if (unlikely(!root)) |
| 2080 |
return ERR_PTR(-EUCLEAN); |
| 2081 |
|
| 2082 |
/* No other choice for non-shareable tree */ |
| 2083 |
if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
| 2084 |
return root; |
| 2085 |
|
| 2086 |
if (btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID) |
| 2087 |
fs_root = root; |
| 2088 |
|
| 2089 |
if (next != node) |
| 2090 |
return NULL; |
| 2091 |
|
| 2092 |
next = walk_down_backref(edges, &index); |
| 2093 |
if (!next || next->level <= node->level) |
| 2094 |
break; |
| 2095 |
} |
| 2096 |
|
| 2097 |
if (!fs_root) |
| 2098 |
return ERR_PTR(-ENOENT); |
| 2099 |
return fs_root; |
| 2100 |
} |
| 2101 |
|
| 2102 |
static noinline_for_stack u64 calcu_metadata_size(struct reloc_control *rc, |
| 2103 |
struct btrfs_backref_node *node) |
| 2104 |
{ |
| 2105 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 2106 |
struct btrfs_backref_node *next = node; |
| 2107 |
struct btrfs_backref_edge *edge; |
| 2108 |
struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2109 |
u64 num_bytes = 0; |
| 2110 |
int index = 0; |
| 2111 |
|
| 2112 |
BUG_ON(node->processed); |
| 2113 |
|
| 2114 |
while (next) { |
| 2115 |
cond_resched(); |
| 2116 |
while (1) { |
| 2117 |
if (next->processed) |
| 2118 |
break; |
| 2119 |
|
| 2120 |
num_bytes += fs_info->nodesize; |
| 2121 |
|
| 2122 |
if (list_empty(&next->upper)) |
| 2123 |
break; |
| 2124 |
|
| 2125 |
edge = list_first_entry(&next->upper, struct btrfs_backref_edge, |
| 2126 |
list[LOWER]); |
| 2127 |
edges[index++] = edge; |
| 2128 |
next = edge->node[UPPER]; |
| 2129 |
} |
| 2130 |
next = walk_down_backref(edges, &index); |
| 2131 |
} |
| 2132 |
return num_bytes; |
| 2133 |
} |
| 2134 |
|
| 2135 |
static int refill_metadata_space(struct btrfs_trans_handle *trans, |
| 2136 |
struct reloc_control *rc, u64 num_bytes) |
| 2137 |
{ |
| 2138 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2139 |
int ret; |
| 2140 |
|
| 2141 |
trans->block_rsv = rc->block_rsv; |
| 2142 |
rc->reserved_bytes += num_bytes; |
| 2143 |
|
| 2144 |
/* |
| 2145 |
* We are under a transaction here so we can only do limited flushing. |
| 2146 |
* If we get an enospc just kick back -EAGAIN so we know to drop the |
| 2147 |
* transaction and try to refill when we can flush all the things. |
| 2148 |
*/ |
| 2149 |
ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv, num_bytes, |
| 2150 |
BTRFS_RESERVE_FLUSH_LIMIT); |
| 2151 |
if (ret) { |
| 2152 |
u64 tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES; |
| 2153 |
|
| 2154 |
while (tmp <= rc->reserved_bytes) |
| 2155 |
tmp <<= 1; |
| 2156 |
/* |
| 2157 |
* only one thread can access block_rsv at this point, |
| 2158 |
* so we don't need hold lock to protect block_rsv. |
| 2159 |
* we expand more reservation size here to allow enough |
| 2160 |
* space for relocation and we will return earlier in |
| 2161 |
* enospc case. |
| 2162 |
*/ |
| 2163 |
rc->block_rsv->size = tmp + fs_info->nodesize * |
| 2164 |
RELOCATION_RESERVED_NODES; |
| 2165 |
return -EAGAIN; |
| 2166 |
} |
| 2167 |
|
| 2168 |
return 0; |
| 2169 |
} |
| 2170 |
|
| 2171 |
static int reserve_metadata_space(struct btrfs_trans_handle *trans, |
| 2172 |
struct reloc_control *rc, |
| 2173 |
struct btrfs_backref_node *node) |
| 2174 |
{ |
| 2175 |
u64 num_bytes; |
| 2176 |
|
| 2177 |
num_bytes = calcu_metadata_size(rc, node) * 2; |
| 2178 |
return refill_metadata_space(trans, rc, num_bytes); |
| 2179 |
} |
| 2180 |
|
| 2181 |
/* |
| 2182 |
* relocate a block tree, and then update pointers in upper level |
| 2183 |
* blocks that reference the block to point to the new location. |
| 2184 |
* |
| 2185 |
* if called by link_to_upper, the block has already been relocated. |
| 2186 |
* in that case this function just updates pointers. |
| 2187 |
*/ |
| 2188 |
static int do_relocation(struct btrfs_trans_handle *trans, |
| 2189 |
struct reloc_control *rc, |
| 2190 |
struct btrfs_backref_node *node, |
| 2191 |
struct btrfs_key *key, |
| 2192 |
struct btrfs_path *path, int lowest) |
| 2193 |
{ |
| 2194 |
struct btrfs_backref_node *upper; |
| 2195 |
struct btrfs_backref_edge *edge; |
| 2196 |
struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2197 |
struct btrfs_root *root; |
| 2198 |
struct extent_buffer *eb; |
| 2199 |
u32 blocksize; |
| 2200 |
u64 bytenr; |
| 2201 |
int slot; |
| 2202 |
int ret = 0; |
| 2203 |
|
| 2204 |
/* |
| 2205 |
* If we are lowest then this is the first time we're processing this |
| 2206 |
* block, and thus shouldn't have an eb associated with it yet. |
| 2207 |
*/ |
| 2208 |
ASSERT(!lowest || !node->eb); |
| 2209 |
|
| 2210 |
path->lowest_level = node->level + 1; |
| 2211 |
rc->backref_cache.path[node->level] = node; |
| 2212 |
list_for_each_entry(edge, &node->upper, list[LOWER]) { |
| 2213 |
cond_resched(); |
| 2214 |
|
| 2215 |
upper = edge->node[UPPER]; |
| 2216 |
root = select_reloc_root(trans, rc, upper, edges); |
| 2217 |
if (IS_ERR(root)) { |
| 2218 |
ret = PTR_ERR(root); |
| 2219 |
goto next; |
| 2220 |
} |
| 2221 |
|
| 2222 |
if (upper->eb && !upper->locked) { |
| 2223 |
if (!lowest) { |
| 2224 |
ret = btrfs_bin_search(upper->eb, 0, key, &slot); |
| 2225 |
if (ret < 0) |
| 2226 |
goto next; |
| 2227 |
BUG_ON(ret); |
| 2228 |
bytenr = btrfs_node_blockptr(upper->eb, slot); |
| 2229 |
if (node->eb->start == bytenr) |
| 2230 |
goto next; |
| 2231 |
} |
| 2232 |
btrfs_backref_drop_node_buffer(upper); |
| 2233 |
} |
| 2234 |
|
| 2235 |
if (!upper->eb) { |
| 2236 |
ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
| 2237 |
if (ret) { |
| 2238 |
if (ret > 0) |
| 2239 |
ret = -ENOENT; |
| 2240 |
|
| 2241 |
btrfs_release_path(path); |
| 2242 |
break; |
| 2243 |
} |
| 2244 |
|
| 2245 |
if (!upper->eb) { |
| 2246 |
upper->eb = path->nodes[upper->level]; |
| 2247 |
path->nodes[upper->level] = NULL; |
| 2248 |
} else { |
| 2249 |
BUG_ON(upper->eb != path->nodes[upper->level]); |
| 2250 |
} |
| 2251 |
|
| 2252 |
upper->locked = 1; |
| 2253 |
path->locks[upper->level] = 0; |
| 2254 |
|
| 2255 |
slot = path->slots[upper->level]; |
| 2256 |
btrfs_release_path(path); |
| 2257 |
} else { |
| 2258 |
ret = btrfs_bin_search(upper->eb, 0, key, &slot); |
| 2259 |
if (ret < 0) |
| 2260 |
goto next; |
| 2261 |
BUG_ON(ret); |
| 2262 |
} |
| 2263 |
|
| 2264 |
bytenr = btrfs_node_blockptr(upper->eb, slot); |
| 2265 |
if (lowest) { |
| 2266 |
if (unlikely(bytenr != node->bytenr)) { |
| 2267 |
btrfs_err(root->fs_info, |
| 2268 |
"lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu", |
| 2269 |
bytenr, node->bytenr, slot, |
| 2270 |
upper->eb->start); |
| 2271 |
ret = -EIO; |
| 2272 |
goto next; |
| 2273 |
} |
| 2274 |
} else { |
| 2275 |
if (node->eb->start == bytenr) |
| 2276 |
goto next; |
| 2277 |
} |
| 2278 |
|
| 2279 |
blocksize = root->fs_info->nodesize; |
| 2280 |
eb = btrfs_read_node_slot(upper->eb, slot); |
| 2281 |
if (IS_ERR(eb)) { |
| 2282 |
ret = PTR_ERR(eb); |
| 2283 |
goto next; |
| 2284 |
} |
| 2285 |
btrfs_tree_lock(eb); |
| 2286 |
|
| 2287 |
if (!node->eb) { |
| 2288 |
ret = btrfs_cow_block(trans, root, eb, upper->eb, |
| 2289 |
slot, &eb, BTRFS_NESTING_COW); |
| 2290 |
btrfs_tree_unlock(eb); |
| 2291 |
free_extent_buffer(eb); |
| 2292 |
if (ret < 0) |
| 2293 |
goto next; |
| 2294 |
/* |
| 2295 |
* We've just COWed this block, it should have updated |
| 2296 |
* the correct backref node entry. |
| 2297 |
*/ |
| 2298 |
ASSERT(node->eb == eb); |
| 2299 |
} else { |
| 2300 |
struct btrfs_ref ref = { |
| 2301 |
.action = BTRFS_ADD_DELAYED_REF, |
| 2302 |
.bytenr = node->eb->start, |
| 2303 |
.num_bytes = blocksize, |
| 2304 |
.parent = upper->eb->start, |
| 2305 |
.owning_root = btrfs_header_owner(upper->eb), |
| 2306 |
.ref_root = btrfs_header_owner(upper->eb), |
| 2307 |
}; |
| 2308 |
|
| 2309 |
btrfs_set_node_blockptr(upper->eb, slot, |
| 2310 |
node->eb->start); |
| 2311 |
btrfs_set_node_ptr_generation(upper->eb, slot, |
| 2312 |
trans->transid); |
| 2313 |
btrfs_mark_buffer_dirty(trans, upper->eb); |
| 2314 |
|
| 2315 |
btrfs_init_tree_ref(&ref, node->level, |
| 2316 |
btrfs_root_id(root), false); |
| 2317 |
ret = btrfs_inc_extent_ref(trans, &ref); |
| 2318 |
if (!ret) |
| 2319 |
ret = btrfs_drop_subtree(trans, root, eb, |
| 2320 |
upper->eb); |
| 2321 |
if (unlikely(ret)) |
| 2322 |
btrfs_abort_transaction(trans, ret); |
| 2323 |
} |
| 2324 |
next: |
| 2325 |
if (!upper->pending) |
| 2326 |
btrfs_backref_drop_node_buffer(upper); |
| 2327 |
else |
| 2328 |
btrfs_backref_unlock_node_buffer(upper); |
| 2329 |
if (ret) |
| 2330 |
break; |
| 2331 |
} |
| 2332 |
|
| 2333 |
if (!ret && node->pending) { |
| 2334 |
btrfs_backref_drop_node_buffer(node); |
| 2335 |
list_del_init(&node->list); |
| 2336 |
node->pending = 0; |
| 2337 |
} |
| 2338 |
|
| 2339 |
path->lowest_level = 0; |
| 2340 |
|
| 2341 |
/* |
| 2342 |
* We should have allocated all of our space in the block rsv and thus |
| 2343 |
* shouldn't ENOSPC. |
| 2344 |
*/ |
| 2345 |
ASSERT(ret != -ENOSPC); |
| 2346 |
return ret; |
| 2347 |
} |
| 2348 |
|
| 2349 |
static int link_to_upper(struct btrfs_trans_handle *trans, |
| 2350 |
struct reloc_control *rc, |
| 2351 |
struct btrfs_backref_node *node, |
| 2352 |
struct btrfs_path *path) |
| 2353 |
{ |
| 2354 |
struct btrfs_key key; |
| 2355 |
|
| 2356 |
btrfs_node_key_to_cpu(node->eb, &key, 0); |
| 2357 |
return do_relocation(trans, rc, node, &key, path, 0); |
| 2358 |
} |
| 2359 |
|
| 2360 |
static int finish_pending_nodes(struct btrfs_trans_handle *trans, |
| 2361 |
struct reloc_control *rc, |
| 2362 |
struct btrfs_path *path, int err) |
| 2363 |
{ |
| 2364 |
LIST_HEAD(list); |
| 2365 |
struct btrfs_backref_cache *cache = &rc->backref_cache; |
| 2366 |
struct btrfs_backref_node *node; |
| 2367 |
int level; |
| 2368 |
int ret; |
| 2369 |
|
| 2370 |
for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
| 2371 |
while (!list_empty(&cache->pending[level])) { |
| 2372 |
node = list_first_entry(&cache->pending[level], |
| 2373 |
struct btrfs_backref_node, list); |
| 2374 |
list_move_tail(&node->list, &list); |
| 2375 |
BUG_ON(!node->pending); |
| 2376 |
|
| 2377 |
if (!err) { |
| 2378 |
ret = link_to_upper(trans, rc, node, path); |
| 2379 |
if (ret < 0) |
| 2380 |
err = ret; |
| 2381 |
} |
| 2382 |
} |
| 2383 |
list_splice_init(&list, &cache->pending[level]); |
| 2384 |
} |
| 2385 |
return err; |
| 2386 |
} |
| 2387 |
|
| 2388 |
/* |
| 2389 |
* mark a block and all blocks directly/indirectly reference the block |
| 2390 |
* as processed. |
| 2391 |
*/ |
| 2392 |
static void update_processed_blocks(struct reloc_control *rc, |
| 2393 |
struct btrfs_backref_node *node) |
| 2394 |
{ |
| 2395 |
struct btrfs_backref_node *next = node; |
| 2396 |
struct btrfs_backref_edge *edge; |
| 2397 |
struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; |
| 2398 |
int index = 0; |
| 2399 |
|
| 2400 |
while (next) { |
| 2401 |
cond_resched(); |
| 2402 |
while (1) { |
| 2403 |
if (next->processed) |
| 2404 |
break; |
| 2405 |
|
| 2406 |
mark_block_processed(rc, next); |
| 2407 |
|
| 2408 |
if (list_empty(&next->upper)) |
| 2409 |
break; |
| 2410 |
|
| 2411 |
edge = list_first_entry(&next->upper, struct btrfs_backref_edge, |
| 2412 |
list[LOWER]); |
| 2413 |
edges[index++] = edge; |
| 2414 |
next = edge->node[UPPER]; |
| 2415 |
} |
| 2416 |
next = walk_down_backref(edges, &index); |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
static int tree_block_processed(u64 bytenr, struct reloc_control *rc) |
| 2421 |
{ |
| 2422 |
u32 blocksize = rc->extent_root->fs_info->nodesize; |
| 2423 |
|
| 2424 |
if (btrfs_test_range_bit(&rc->processed_blocks, bytenr, |
| 2425 |
bytenr + blocksize - 1, EXTENT_DIRTY, NULL)) |
| 2426 |
return 1; |
| 2427 |
return 0; |
| 2428 |
} |
| 2429 |
|
| 2430 |
static int get_tree_block_key(struct btrfs_fs_info *fs_info, |
| 2431 |
struct tree_block *block) |
| 2432 |
{ |
| 2433 |
struct btrfs_tree_parent_check check = { |
| 2434 |
.level = block->level, |
| 2435 |
.owner_root = block->owner, |
| 2436 |
.transid = block->key.offset |
| 2437 |
}; |
| 2438 |
struct extent_buffer *eb; |
| 2439 |
|
| 2440 |
eb = read_tree_block(fs_info, block->bytenr, &check); |
| 2441 |
if (IS_ERR(eb)) |
| 2442 |
return PTR_ERR(eb); |
| 2443 |
if (unlikely(!extent_buffer_uptodate(eb))) { |
| 2444 |
free_extent_buffer(eb); |
| 2445 |
return -EIO; |
| 2446 |
} |
| 2447 |
if (block->level == 0) |
| 2448 |
btrfs_item_key_to_cpu(eb, &block->key, 0); |
| 2449 |
else |
| 2450 |
btrfs_node_key_to_cpu(eb, &block->key, 0); |
| 2451 |
free_extent_buffer(eb); |
| 2452 |
block->key_ready = true; |
| 2453 |
return 0; |
| 2454 |
} |
| 2455 |
|
| 2456 |
/* |
| 2457 |
* helper function to relocate a tree block |
| 2458 |
*/ |
| 2459 |
static int relocate_tree_block(struct btrfs_trans_handle *trans, |
| 2460 |
struct reloc_control *rc, |
| 2461 |
struct btrfs_backref_node *node, |
| 2462 |
struct btrfs_key *key, |
| 2463 |
struct btrfs_path *path) |
| 2464 |
{ |
| 2465 |
struct btrfs_root *root; |
| 2466 |
int ret = 0; |
| 2467 |
|
| 2468 |
if (!node) |
| 2469 |
return 0; |
| 2470 |
|
| 2471 |
/* |
| 2472 |
* If we fail here we want to drop our backref_node because we are going |
| 2473 |
* to start over and regenerate the tree for it. |
| 2474 |
*/ |
| 2475 |
ret = reserve_metadata_space(trans, rc, node); |
| 2476 |
if (ret) |
| 2477 |
goto out; |
| 2478 |
|
| 2479 |
BUG_ON(node->processed); |
| 2480 |
root = select_one_root(node); |
| 2481 |
if (IS_ERR(root)) { |
| 2482 |
ret = PTR_ERR(root); |
| 2483 |
|
| 2484 |
/* See explanation in select_one_root for the -EUCLEAN case. */ |
| 2485 |
ASSERT(ret == -ENOENT); |
| 2486 |
if (ret == -ENOENT) { |
| 2487 |
ret = 0; |
| 2488 |
update_processed_blocks(rc, node); |
| 2489 |
} |
| 2490 |
goto out; |
| 2491 |
} |
| 2492 |
|
| 2493 |
if (root) { |
| 2494 |
if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { |
| 2495 |
/* |
| 2496 |
* This block was the root block of a root, and this is |
| 2497 |
* the first time we're processing the block and thus it |
| 2498 |
* should not have had the ->new_bytenr modified. |
| 2499 |
* |
| 2500 |
* However in the case of corruption we could have |
| 2501 |
* multiple refs pointing to the same block improperly, |
| 2502 |
* and thus we would trip over these checks. ASSERT() |
| 2503 |
* for the developer case, because it could indicate a |
| 2504 |
* bug in the backref code, however error out for a |
| 2505 |
* normal user in the case of corruption. |
| 2506 |
*/ |
| 2507 |
ASSERT(node->new_bytenr == 0); |
| 2508 |
if (unlikely(node->new_bytenr)) { |
| 2509 |
btrfs_err(root->fs_info, |
| 2510 |
"bytenr %llu has improper references to it", |
| 2511 |
node->bytenr); |
| 2512 |
ret = -EUCLEAN; |
| 2513 |
goto out; |
| 2514 |
} |
| 2515 |
ret = btrfs_record_root_in_trans(trans, root); |
| 2516 |
if (ret) |
| 2517 |
goto out; |
| 2518 |
/* |
| 2519 |
* Another thread could have failed, need to check if we |
| 2520 |
* have reloc_root actually set. |
| 2521 |
*/ |
| 2522 |
if (!root->reloc_root) { |
| 2523 |
ret = -ENOENT; |
| 2524 |
goto out; |
| 2525 |
} |
| 2526 |
root = root->reloc_root; |
| 2527 |
node->new_bytenr = root->node->start; |
| 2528 |
btrfs_put_root(node->root); |
| 2529 |
node->root = btrfs_grab_root(root); |
| 2530 |
ASSERT(node->root); |
| 2531 |
} else { |
| 2532 |
btrfs_err(root->fs_info, |
| 2533 |
"bytenr %llu resolved to a non-shareable root", |
| 2534 |
node->bytenr); |
| 2535 |
ret = -EUCLEAN; |
| 2536 |
goto out; |
| 2537 |
} |
| 2538 |
if (!ret) |
| 2539 |
update_processed_blocks(rc, node); |
| 2540 |
} else { |
| 2541 |
ret = do_relocation(trans, rc, node, key, path, 1); |
| 2542 |
} |
| 2543 |
out: |
| 2544 |
if (ret || node->level == 0) |
| 2545 |
btrfs_backref_cleanup_node(&rc->backref_cache, node); |
| 2546 |
return ret; |
| 2547 |
} |
| 2548 |
|
| 2549 |
static int relocate_cowonly_block(struct btrfs_trans_handle *trans, |
| 2550 |
struct reloc_control *rc, struct tree_block *block, |
| 2551 |
struct btrfs_path *path) |
| 2552 |
{ |
| 2553 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2554 |
struct btrfs_root *root; |
| 2555 |
u64 num_bytes; |
| 2556 |
int nr_levels; |
| 2557 |
int ret; |
| 2558 |
|
| 2559 |
root = btrfs_get_fs_root(fs_info, block->owner, true); |
| 2560 |
if (IS_ERR(root)) |
| 2561 |
return PTR_ERR(root); |
| 2562 |
|
| 2563 |
nr_levels = max(btrfs_header_level(root->node) - block->level, 0) + 1; |
| 2564 |
|
| 2565 |
num_bytes = fs_info->nodesize * nr_levels; |
| 2566 |
ret = refill_metadata_space(trans, rc, num_bytes); |
| 2567 |
if (ret) { |
| 2568 |
btrfs_put_root(root); |
| 2569 |
return ret; |
| 2570 |
} |
| 2571 |
path->lowest_level = block->level; |
| 2572 |
if (root == root->fs_info->chunk_root) |
| 2573 |
btrfs_reserve_chunk_metadata(trans, false); |
| 2574 |
|
| 2575 |
ret = btrfs_search_slot(trans, root, &block->key, path, 0, 1); |
| 2576 |
path->lowest_level = 0; |
| 2577 |
btrfs_release_path(path); |
| 2578 |
|
| 2579 |
if (root == root->fs_info->chunk_root) |
| 2580 |
btrfs_trans_release_chunk_metadata(trans); |
| 2581 |
if (ret > 0) |
| 2582 |
ret = 0; |
| 2583 |
btrfs_put_root(root); |
| 2584 |
|
| 2585 |
return ret; |
| 2586 |
} |
| 2587 |
|
| 2588 |
/* |
| 2589 |
* relocate a list of blocks |
| 2590 |
*/ |
| 2591 |
static noinline_for_stack |
| 2592 |
int relocate_tree_blocks(struct btrfs_trans_handle *trans, |
| 2593 |
struct reloc_control *rc, struct rb_root *blocks) |
| 2594 |
{ |
| 2595 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 2596 |
struct btrfs_backref_node *node; |
| 2597 |
struct btrfs_path *path; |
| 2598 |
struct tree_block *block; |
| 2599 |
struct tree_block *next; |
| 2600 |
int ret = 0; |
| 2601 |
|
| 2602 |
path = btrfs_alloc_path(); |
| 2603 |
if (!path) { |
| 2604 |
ret = -ENOMEM; |
| 2605 |
goto out_free_blocks; |
| 2606 |
} |
| 2607 |
|
| 2608 |
/* Kick in readahead for tree blocks with missing keys */ |
| 2609 |
rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { |
| 2610 |
if (!block->key_ready) |
| 2611 |
btrfs_readahead_tree_block(fs_info, block->bytenr, |
| 2612 |
block->owner, 0, |
| 2613 |
block->level); |
| 2614 |
} |
| 2615 |
|
| 2616 |
/* Get first keys */ |
| 2617 |
rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { |
| 2618 |
if (!block->key_ready) { |
| 2619 |
ret = get_tree_block_key(fs_info, block); |
| 2620 |
if (ret) |
| 2621 |
goto out_free_path; |
| 2622 |
} |
| 2623 |
} |
| 2624 |
|
| 2625 |
/* Do tree relocation */ |
| 2626 |
rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { |
| 2627 |
/* |
| 2628 |
* For COWonly blocks, or the data reloc tree, we only need to |
| 2629 |
* COW down to the block, there's no need to generate a backref |
| 2630 |
* tree. |
| 2631 |
*/ |
| 2632 |
if (block->owner && |
| 2633 |
(!btrfs_is_fstree(block->owner) || |
| 2634 |
block->owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) { |
| 2635 |
ret = relocate_cowonly_block(trans, rc, block, path); |
| 2636 |
if (ret) |
| 2637 |
break; |
| 2638 |
continue; |
| 2639 |
} |
| 2640 |
|
| 2641 |
node = build_backref_tree(trans, rc, &block->key, |
| 2642 |
block->level, block->bytenr); |
| 2643 |
if (IS_ERR(node)) { |
| 2644 |
ret = PTR_ERR(node); |
| 2645 |
goto out; |
| 2646 |
} |
| 2647 |
|
| 2648 |
ret = relocate_tree_block(trans, rc, node, &block->key, |
| 2649 |
path); |
| 2650 |
if (ret < 0) |
| 2651 |
break; |
| 2652 |
} |
| 2653 |
out: |
| 2654 |
ret = finish_pending_nodes(trans, rc, path, ret); |
| 2655 |
|
| 2656 |
out_free_path: |
| 2657 |
btrfs_free_path(path); |
| 2658 |
out_free_blocks: |
| 2659 |
free_block_list(blocks); |
| 2660 |
return ret; |
| 2661 |
} |
| 2662 |
|
| 2663 |
static noinline_for_stack int prealloc_file_extent_cluster(struct reloc_control *rc) |
| 2664 |
{ |
| 2665 |
const struct file_extent_cluster *cluster = &rc->cluster; |
| 2666 |
struct btrfs_inode *inode = BTRFS_I(rc->data_inode); |
| 2667 |
u64 alloc_hint = 0; |
| 2668 |
u64 start; |
| 2669 |
u64 end; |
| 2670 |
u64 offset = inode->reloc_block_group_start; |
| 2671 |
u64 num_bytes; |
| 2672 |
int nr; |
| 2673 |
int ret = 0; |
| 2674 |
u64 prealloc_start = cluster->start - offset; |
| 2675 |
u64 prealloc_end = cluster->end - offset; |
| 2676 |
u64 cur_offset = prealloc_start; |
| 2677 |
|
| 2678 |
/* |
| 2679 |
* For blocksize < folio size case (either bs < page size or large folios), |
| 2680 |
* beyond i_size, all blocks are filled with zero. |
| 2681 |
* |
| 2682 |
* If the current cluster covers the above range, btrfs_do_readpage() |
| 2683 |
* will skip the read, and relocate_one_folio() will later writeback |
| 2684 |
* the padding zeros as new data, causing data corruption. |
| 2685 |
* |
| 2686 |
* Here we have to invalidate the cache covering our cluster. |
| 2687 |
*/ |
| 2688 |
ret = filemap_invalidate_inode(&inode->vfs_inode, true, prealloc_start, |
| 2689 |
prealloc_end); |
| 2690 |
if (ret < 0) |
| 2691 |
return ret; |
| 2692 |
|
| 2693 |
BUG_ON(cluster->start != cluster->boundary[0]); |
| 2694 |
ret = btrfs_alloc_data_chunk_ondemand(inode, |
| 2695 |
prealloc_end + 1 - prealloc_start); |
| 2696 |
if (ret) |
| 2697 |
return ret; |
| 2698 |
|
| 2699 |
btrfs_inode_lock(inode, 0); |
| 2700 |
for (nr = 0; nr < cluster->nr; nr++) { |
| 2701 |
struct extent_state *cached_state = NULL; |
| 2702 |
|
| 2703 |
start = cluster->boundary[nr] - offset; |
| 2704 |
if (nr + 1 < cluster->nr) |
| 2705 |
end = cluster->boundary[nr + 1] - 1 - offset; |
| 2706 |
else |
| 2707 |
end = cluster->end - offset; |
| 2708 |
|
| 2709 |
btrfs_lock_extent(&inode->io_tree, start, end, &cached_state); |
| 2710 |
num_bytes = end + 1 - start; |
| 2711 |
ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start, |
| 2712 |
num_bytes, num_bytes, |
| 2713 |
end + 1, &alloc_hint); |
| 2714 |
cur_offset = end + 1; |
| 2715 |
btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state); |
| 2716 |
if (ret) |
| 2717 |
break; |
| 2718 |
} |
| 2719 |
btrfs_inode_unlock(inode, 0); |
| 2720 |
|
| 2721 |
if (cur_offset < prealloc_end) |
| 2722 |
btrfs_free_reserved_data_space_noquota(inode, |
| 2723 |
prealloc_end + 1 - cur_offset); |
| 2724 |
return ret; |
| 2725 |
} |
| 2726 |
|
| 2727 |
static noinline_for_stack int setup_relocation_extent_mapping(struct reloc_control *rc) |
| 2728 |
{ |
| 2729 |
struct btrfs_inode *inode = BTRFS_I(rc->data_inode); |
| 2730 |
struct extent_map *em; |
| 2731 |
struct extent_state *cached_state = NULL; |
| 2732 |
u64 offset = inode->reloc_block_group_start; |
| 2733 |
u64 start = rc->cluster.start - offset; |
| 2734 |
u64 end = rc->cluster.end - offset; |
| 2735 |
int ret = 0; |
| 2736 |
|
| 2737 |
em = btrfs_alloc_extent_map(); |
| 2738 |
if (!em) |
| 2739 |
return -ENOMEM; |
| 2740 |
|
| 2741 |
em->start = start; |
| 2742 |
em->len = end + 1 - start; |
| 2743 |
em->disk_bytenr = rc->cluster.start; |
| 2744 |
em->disk_num_bytes = em->len; |
| 2745 |
em->ram_bytes = em->len; |
| 2746 |
em->flags |= EXTENT_FLAG_PINNED; |
| 2747 |
|
| 2748 |
btrfs_lock_extent(&inode->io_tree, start, end, &cached_state); |
| 2749 |
ret = btrfs_replace_extent_map_range(inode, em, false); |
| 2750 |
btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state); |
| 2751 |
btrfs_free_extent_map(em); |
| 2752 |
|
| 2753 |
return ret; |
| 2754 |
} |
| 2755 |
|
| 2756 |
/* |
| 2757 |
* Allow error injection to test balance/relocation cancellation |
| 2758 |
*/ |
| 2759 |
noinline int btrfs_should_cancel_balance(const struct btrfs_fs_info *fs_info) |
| 2760 |
{ |
| 2761 |
return atomic_read(&fs_info->balance_cancel_req) || |
| 2762 |
atomic_read(&fs_info->reloc_cancel_req) || |
| 2763 |
fatal_signal_pending(current); |
| 2764 |
} |
| 2765 |
ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE); |
| 2766 |
|
| 2767 |
static u64 get_cluster_boundary_end(const struct file_extent_cluster *cluster, |
| 2768 |
int cluster_nr) |
| 2769 |
{ |
| 2770 |
/* Last extent, use cluster end directly */ |
| 2771 |
if (cluster_nr >= cluster->nr - 1) |
| 2772 |
return cluster->end; |
| 2773 |
|
| 2774 |
/* Use next boundary start*/ |
| 2775 |
return cluster->boundary[cluster_nr + 1] - 1; |
| 2776 |
} |
| 2777 |
|
| 2778 |
static int relocate_one_folio(struct reloc_control *rc, |
| 2779 |
struct file_ra_state *ra, |
| 2780 |
int *cluster_nr, u64 *file_offset_ret) |
| 2781 |
{ |
| 2782 |
const struct file_extent_cluster *cluster = &rc->cluster; |
| 2783 |
struct inode *inode = rc->data_inode; |
| 2784 |
struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); |
| 2785 |
const u64 orig_file_offset = *file_offset_ret; |
| 2786 |
u64 offset = BTRFS_I(inode)->reloc_block_group_start; |
| 2787 |
const pgoff_t last_index = (cluster->end - offset) >> PAGE_SHIFT; |
| 2788 |
const pgoff_t index = orig_file_offset >> PAGE_SHIFT; |
| 2789 |
gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
| 2790 |
struct folio *folio; |
| 2791 |
u64 folio_start; |
| 2792 |
u64 folio_end; |
| 2793 |
u64 cur; |
| 2794 |
int ret; |
| 2795 |
const bool use_rst = btrfs_need_stripe_tree_update(fs_info, rc->block_group->flags); |
| 2796 |
|
| 2797 |
ASSERT(index <= last_index); |
| 2798 |
again: |
| 2799 |
folio = filemap_lock_folio(inode->i_mapping, index); |
| 2800 |
if (IS_ERR(folio)) { |
| 2801 |
|
| 2802 |
/* |
| 2803 |
* On relocation we're doing readahead on the relocation inode, |
| 2804 |
* but if the filesystem is backed by a RAID stripe tree we can |
| 2805 |
* get ENOENT (e.g. due to preallocated extents not being |
| 2806 |
* mapped in the RST) from the lookup. |
| 2807 |
* |
| 2808 |
* But readahead doesn't handle the error and submits invalid |
| 2809 |
* reads to the device, causing a assertion failures. |
| 2810 |
*/ |
| 2811 |
if (!use_rst) |
| 2812 |
page_cache_sync_readahead(inode->i_mapping, ra, NULL, |
| 2813 |
index, last_index + 1 - index); |
| 2814 |
folio = __filemap_get_folio(inode->i_mapping, index, |
| 2815 |
FGP_LOCK | FGP_ACCESSED | FGP_CREAT, |
| 2816 |
mask); |
| 2817 |
if (IS_ERR(folio)) |
| 2818 |
return PTR_ERR(folio); |
| 2819 |
} |
| 2820 |
|
| 2821 |
if (folio_test_readahead(folio) && !use_rst) |
| 2822 |
page_cache_async_readahead(inode->i_mapping, ra, NULL, |
| 2823 |
folio, last_index + 1 - index); |
| 2824 |
|
| 2825 |
if (!folio_test_uptodate(folio)) { |
| 2826 |
btrfs_read_folio(NULL, folio); |
| 2827 |
folio_lock(folio); |
| 2828 |
if (unlikely(!folio_test_uptodate(folio))) { |
| 2829 |
ret = -EIO; |
| 2830 |
goto release_folio; |
| 2831 |
} |
| 2832 |
if (folio->mapping != inode->i_mapping) { |
| 2833 |
folio_unlock(folio); |
| 2834 |
folio_put(folio); |
| 2835 |
goto again; |
| 2836 |
} |
| 2837 |
} |
| 2838 |
|
| 2839 |
/* |
| 2840 |
* We could have lost folio private when we dropped the lock to read the |
| 2841 |
* folio above, make sure we set_folio_extent_mapped() here so we have any |
| 2842 |
* of the subpage blocksize stuff we need in place. |
| 2843 |
*/ |
| 2844 |
ret = set_folio_extent_mapped(folio); |
| 2845 |
if (ret < 0) |
| 2846 |
goto release_folio; |
| 2847 |
|
| 2848 |
folio_start = folio_pos(folio); |
| 2849 |
folio_end = folio_start + folio_size(folio) - 1; |
| 2850 |
|
| 2851 |
/* |
| 2852 |
* Start from the cluster, as for subpage case, the cluster can start |
| 2853 |
* inside the folio. |
| 2854 |
*/ |
| 2855 |
cur = max(folio_start, cluster->boundary[*cluster_nr] - offset); |
| 2856 |
while (cur <= folio_end) { |
| 2857 |
struct extent_state *cached_state = NULL; |
| 2858 |
u64 extent_start = cluster->boundary[*cluster_nr] - offset; |
| 2859 |
u64 extent_end = get_cluster_boundary_end(cluster, |
| 2860 |
*cluster_nr) - offset; |
| 2861 |
u64 clamped_start = max(folio_start, extent_start); |
| 2862 |
u64 clamped_end = min(folio_end, extent_end); |
| 2863 |
u32 clamped_len = clamped_end + 1 - clamped_start; |
| 2864 |
|
| 2865 |
/* Reserve metadata for this range */ |
| 2866 |
ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), |
| 2867 |
clamped_len, clamped_len, |
| 2868 |
false); |
| 2869 |
if (ret) |
| 2870 |
goto release_folio; |
| 2871 |
|
| 2872 |
/* Mark the range delalloc and dirty for later writeback */ |
| 2873 |
btrfs_lock_extent(&BTRFS_I(inode)->io_tree, clamped_start, |
| 2874 |
clamped_end, &cached_state); |
| 2875 |
ret = btrfs_set_extent_delalloc(BTRFS_I(inode), clamped_start, |
| 2876 |
clamped_end, 0, &cached_state); |
| 2877 |
if (ret) { |
| 2878 |
btrfs_clear_extent_bit(&BTRFS_I(inode)->io_tree, |
| 2879 |
clamped_start, clamped_end, |
| 2880 |
EXTENT_LOCKED | EXTENT_BOUNDARY, |
| 2881 |
&cached_state); |
| 2882 |
btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 2883 |
clamped_len, true); |
| 2884 |
btrfs_delalloc_release_extents(BTRFS_I(inode), |
| 2885 |
clamped_len); |
| 2886 |
goto release_folio; |
| 2887 |
} |
| 2888 |
btrfs_folio_set_dirty(fs_info, folio, clamped_start, clamped_len); |
| 2889 |
|
| 2890 |
/* |
| 2891 |
* Set the boundary if it's inside the folio. |
| 2892 |
* Data relocation requires the destination extents to have the |
| 2893 |
* same size as the source. |
| 2894 |
* EXTENT_BOUNDARY bit prevents current extent from being merged |
| 2895 |
* with previous extent. |
| 2896 |
*/ |
| 2897 |
if (in_range(cluster->boundary[*cluster_nr] - offset, |
| 2898 |
folio_start, folio_size(folio))) { |
| 2899 |
u64 boundary_start = cluster->boundary[*cluster_nr] - |
| 2900 |
offset; |
| 2901 |
u64 boundary_end = boundary_start + |
| 2902 |
fs_info->sectorsize - 1; |
| 2903 |
|
| 2904 |
btrfs_set_extent_bit(&BTRFS_I(inode)->io_tree, |
| 2905 |
boundary_start, boundary_end, |
| 2906 |
EXTENT_BOUNDARY, NULL); |
| 2907 |
} |
| 2908 |
btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end, |
| 2909 |
&cached_state); |
| 2910 |
btrfs_delalloc_release_extents(BTRFS_I(inode), clamped_len); |
| 2911 |
cur += clamped_len; |
| 2912 |
|
| 2913 |
/* Crossed extent end, go to next extent */ |
| 2914 |
if (cur >= extent_end) { |
| 2915 |
(*cluster_nr)++; |
| 2916 |
/* Just finished the last extent of the cluster, exit. */ |
| 2917 |
if (*cluster_nr >= cluster->nr) |
| 2918 |
break; |
| 2919 |
} |
| 2920 |
} |
| 2921 |
folio_unlock(folio); |
| 2922 |
folio_put(folio); |
| 2923 |
|
| 2924 |
balance_dirty_pages_ratelimited(inode->i_mapping); |
| 2925 |
btrfs_throttle(fs_info); |
| 2926 |
if (btrfs_should_cancel_balance(fs_info)) |
| 2927 |
ret = -ECANCELED; |
| 2928 |
*file_offset_ret = folio_end + 1; |
| 2929 |
return ret; |
| 2930 |
|
| 2931 |
release_folio: |
| 2932 |
folio_unlock(folio); |
| 2933 |
folio_put(folio); |
| 2934 |
return ret; |
| 2935 |
} |
| 2936 |
|
| 2937 |
static int relocate_file_extent_cluster(struct reloc_control *rc) |
| 2938 |
{ |
| 2939 |
struct inode *inode = rc->data_inode; |
| 2940 |
const struct file_extent_cluster *cluster = &rc->cluster; |
| 2941 |
u64 offset = BTRFS_I(inode)->reloc_block_group_start; |
| 2942 |
u64 cur_file_offset = cluster->start - offset; |
| 2943 |
struct file_ra_state AUTO_KFREE(ra); |
| 2944 |
int cluster_nr = 0; |
| 2945 |
int ret = 0; |
| 2946 |
|
| 2947 |
if (!cluster->nr) |
| 2948 |
return 0; |
| 2949 |
|
| 2950 |
ra = kzalloc(sizeof(*ra), GFP_NOFS); |
| 2951 |
if (!ra) |
| 2952 |
return -ENOMEM; |
| 2953 |
|
| 2954 |
ret = prealloc_file_extent_cluster(rc); |
| 2955 |
if (ret) |
| 2956 |
return ret; |
| 2957 |
|
| 2958 |
file_ra_state_init(ra, inode->i_mapping); |
| 2959 |
|
| 2960 |
ret = setup_relocation_extent_mapping(rc); |
| 2961 |
if (ret) |
| 2962 |
return ret; |
| 2963 |
|
| 2964 |
while (cur_file_offset < cluster->end - offset) { |
| 2965 |
ret = relocate_one_folio(rc, ra, &cluster_nr, &cur_file_offset); |
| 2966 |
if (ret) |
| 2967 |
break; |
| 2968 |
} |
| 2969 |
if (ret == 0) |
| 2970 |
WARN_ON(cluster_nr != cluster->nr); |
| 2971 |
return ret; |
| 2972 |
} |
| 2973 |
|
| 2974 |
static noinline_for_stack int relocate_data_extent(struct reloc_control *rc, |
| 2975 |
const struct btrfs_key *extent_key) |
| 2976 |
{ |
| 2977 |
struct inode *inode = rc->data_inode; |
| 2978 |
struct file_extent_cluster *cluster = &rc->cluster; |
| 2979 |
int ret; |
| 2980 |
struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2981 |
|
| 2982 |
if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) { |
| 2983 |
ret = relocate_file_extent_cluster(rc); |
| 2984 |
if (ret) |
| 2985 |
return ret; |
| 2986 |
cluster->nr = 0; |
| 2987 |
} |
| 2988 |
|
| 2989 |
/* |
| 2990 |
* Under simple quotas, we set root->relocation_src_root when we find |
| 2991 |
* the extent. If adjacent extents have different owners, we can't merge |
| 2992 |
* them while relocating. Handle this by storing the owning root that |
| 2993 |
* started a cluster and if we see an extent from a different root break |
| 2994 |
* cluster formation (just like the above case of non-adjacent extents). |
| 2995 |
* |
| 2996 |
* Without simple quotas, relocation_src_root is always 0, so we should |
| 2997 |
* never see a mismatch, and it should have no effect on relocation |
| 2998 |
* clusters. |
| 2999 |
*/ |
| 3000 |
if (cluster->nr > 0 && cluster->owning_root != root->relocation_src_root) { |
| 3001 |
u64 tmp = root->relocation_src_root; |
| 3002 |
|
| 3003 |
/* |
| 3004 |
* root->relocation_src_root is the state that actually affects |
| 3005 |
* the preallocation we do here, so set it to the root owning |
| 3006 |
* the cluster we need to relocate. |
| 3007 |
*/ |
| 3008 |
root->relocation_src_root = cluster->owning_root; |
| 3009 |
ret = relocate_file_extent_cluster(rc); |
| 3010 |
if (ret) |
| 3011 |
return ret; |
| 3012 |
cluster->nr = 0; |
| 3013 |
/* And reset it back for the current extent's owning root. */ |
| 3014 |
root->relocation_src_root = tmp; |
| 3015 |
} |
| 3016 |
|
| 3017 |
if (!cluster->nr) { |
| 3018 |
cluster->start = extent_key->objectid; |
| 3019 |
cluster->owning_root = root->relocation_src_root; |
| 3020 |
} |
| 3021 |
else |
| 3022 |
BUG_ON(cluster->nr >= MAX_EXTENTS); |
| 3023 |
cluster->end = extent_key->objectid + extent_key->offset - 1; |
| 3024 |
cluster->boundary[cluster->nr] = extent_key->objectid; |
| 3025 |
cluster->nr++; |
| 3026 |
|
| 3027 |
if (cluster->nr >= MAX_EXTENTS) { |
| 3028 |
ret = relocate_file_extent_cluster(rc); |
| 3029 |
if (ret) |
| 3030 |
return ret; |
| 3031 |
cluster->nr = 0; |
| 3032 |
} |
| 3033 |
return 0; |
| 3034 |
} |
| 3035 |
|
| 3036 |
/* |
| 3037 |
* helper to add a tree block to the list. |
| 3038 |
* the major work is getting the generation and level of the block |
| 3039 |
*/ |
| 3040 |
static int add_tree_block(struct reloc_control *rc, |
| 3041 |
const struct btrfs_key *extent_key, |
| 3042 |
struct btrfs_path *path, |
| 3043 |
struct rb_root *blocks) |
| 3044 |
{ |
| 3045 |
struct extent_buffer *eb; |
| 3046 |
struct btrfs_extent_item *ei; |
| 3047 |
struct btrfs_tree_block_info *bi; |
| 3048 |
struct tree_block *block; |
| 3049 |
struct rb_node *rb_node; |
| 3050 |
u32 item_size; |
| 3051 |
int level = -1; |
| 3052 |
u64 generation; |
| 3053 |
u64 owner = 0; |
| 3054 |
|
| 3055 |
eb = path->nodes[0]; |
| 3056 |
item_size = btrfs_item_size(eb, path->slots[0]); |
| 3057 |
|
| 3058 |
if (extent_key->type == BTRFS_METADATA_ITEM_KEY || |
| 3059 |
item_size >= sizeof(*ei) + sizeof(*bi)) { |
| 3060 |
unsigned long ptr = 0, end; |
| 3061 |
|
| 3062 |
ei = btrfs_item_ptr(eb, path->slots[0], |
| 3063 |
struct btrfs_extent_item); |
| 3064 |
end = (unsigned long)ei + item_size; |
| 3065 |
if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) { |
| 3066 |
bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3067 |
level = btrfs_tree_block_level(eb, bi); |
| 3068 |
ptr = (unsigned long)(bi + 1); |
| 3069 |
} else { |
| 3070 |
level = (int)extent_key->offset; |
| 3071 |
ptr = (unsigned long)(ei + 1); |
| 3072 |
} |
| 3073 |
generation = btrfs_extent_generation(eb, ei); |
| 3074 |
|
| 3075 |
/* |
| 3076 |
* We're reading random blocks without knowing their owner ahead |
| 3077 |
* of time. This is ok most of the time, as all reloc roots and |
| 3078 |
* fs roots have the same lock type. However normal trees do |
| 3079 |
* not, and the only way to know ahead of time is to read the |
| 3080 |
* inline ref offset. We know it's an fs root if |
| 3081 |
* |
| 3082 |
* 1. There's more than one ref. |
| 3083 |
* 2. There's a SHARED_DATA_REF_KEY set. |
| 3084 |
* 3. FULL_BACKREF is set on the flags. |
| 3085 |
* |
| 3086 |
* Otherwise it's safe to assume that the ref offset == the |
| 3087 |
* owner of this block, so we can use that when calling |
| 3088 |
* read_tree_block. |
| 3089 |
*/ |
| 3090 |
if (btrfs_extent_refs(eb, ei) == 1 && |
| 3091 |
!(btrfs_extent_flags(eb, ei) & |
| 3092 |
BTRFS_BLOCK_FLAG_FULL_BACKREF) && |
| 3093 |
ptr < end) { |
| 3094 |
struct btrfs_extent_inline_ref *iref; |
| 3095 |
int type; |
| 3096 |
|
| 3097 |
iref = (struct btrfs_extent_inline_ref *)ptr; |
| 3098 |
type = btrfs_get_extent_inline_ref_type(eb, iref, |
| 3099 |
BTRFS_REF_TYPE_BLOCK); |
| 3100 |
if (type == BTRFS_REF_TYPE_INVALID) |
| 3101 |
return -EINVAL; |
| 3102 |
if (type == BTRFS_TREE_BLOCK_REF_KEY) |
| 3103 |
owner = btrfs_extent_inline_ref_offset(eb, iref); |
| 3104 |
} |
| 3105 |
} else { |
| 3106 |
btrfs_print_leaf(eb); |
| 3107 |
btrfs_err(rc->block_group->fs_info, |
| 3108 |
"unrecognized tree backref at tree block %llu slot %u", |
| 3109 |
eb->start, path->slots[0]); |
| 3110 |
btrfs_release_path(path); |
| 3111 |
return -EUCLEAN; |
| 3112 |
} |
| 3113 |
|
| 3114 |
btrfs_release_path(path); |
| 3115 |
|
| 3116 |
BUG_ON(level == -1); |
| 3117 |
|
| 3118 |
block = kmalloc_obj(*block, GFP_NOFS); |
| 3119 |
if (!block) |
| 3120 |
return -ENOMEM; |
| 3121 |
|
| 3122 |
block->bytenr = extent_key->objectid; |
| 3123 |
block->key.objectid = rc->extent_root->fs_info->nodesize; |
| 3124 |
block->key.offset = generation; |
| 3125 |
block->level = level; |
| 3126 |
block->key_ready = false; |
| 3127 |
block->owner = owner; |
| 3128 |
|
| 3129 |
rb_node = rb_simple_insert(blocks, &block->simple_node); |
| 3130 |
if (rb_node) |
| 3131 |
btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr, |
| 3132 |
-EEXIST); |
| 3133 |
|
| 3134 |
return 0; |
| 3135 |
} |
| 3136 |
|
| 3137 |
/* |
| 3138 |
* helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY |
| 3139 |
*/ |
| 3140 |
static int __add_tree_block(struct reloc_control *rc, |
| 3141 |
u64 bytenr, u32 blocksize, |
| 3142 |
struct rb_root *blocks) |
| 3143 |
{ |
| 3144 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3145 |
BTRFS_PATH_AUTO_FREE(path); |
| 3146 |
struct btrfs_key key; |
| 3147 |
int ret; |
| 3148 |
bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
| 3149 |
|
| 3150 |
if (tree_block_processed(bytenr, rc)) |
| 3151 |
return 0; |
| 3152 |
|
| 3153 |
if (rb_simple_search(blocks, bytenr)) |
| 3154 |
return 0; |
| 3155 |
|
| 3156 |
path = btrfs_alloc_path(); |
| 3157 |
if (!path) |
| 3158 |
return -ENOMEM; |
| 3159 |
again: |
| 3160 |
key.objectid = bytenr; |
| 3161 |
if (skinny) { |
| 3162 |
key.type = BTRFS_METADATA_ITEM_KEY; |
| 3163 |
key.offset = (u64)-1; |
| 3164 |
} else { |
| 3165 |
key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3166 |
key.offset = blocksize; |
| 3167 |
} |
| 3168 |
|
| 3169 |
path->search_commit_root = true; |
| 3170 |
path->skip_locking = true; |
| 3171 |
ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0); |
| 3172 |
if (ret < 0) |
| 3173 |
return ret; |
| 3174 |
|
| 3175 |
if (ret > 0 && skinny) { |
| 3176 |
if (path->slots[0]) { |
| 3177 |
path->slots[0]--; |
| 3178 |
btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3179 |
path->slots[0]); |
| 3180 |
if (key.objectid == bytenr && |
| 3181 |
(key.type == BTRFS_METADATA_ITEM_KEY || |
| 3182 |
(key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3183 |
key.offset == blocksize))) |
| 3184 |
ret = 0; |
| 3185 |
} |
| 3186 |
|
| 3187 |
if (ret) { |
| 3188 |
skinny = false; |
| 3189 |
btrfs_release_path(path); |
| 3190 |
goto again; |
| 3191 |
} |
| 3192 |
} |
| 3193 |
if (ret) { |
| 3194 |
ASSERT(ret == 1); |
| 3195 |
btrfs_print_leaf(path->nodes[0]); |
| 3196 |
btrfs_err(fs_info, |
| 3197 |
"tree block extent item (%llu) is not found in extent tree", |
| 3198 |
bytenr); |
| 3199 |
WARN_ON(1); |
| 3200 |
return -EINVAL; |
| 3201 |
} |
| 3202 |
|
| 3203 |
return add_tree_block(rc, &key, path, blocks); |
| 3204 |
} |
| 3205 |
|
| 3206 |
static int delete_block_group_cache(struct btrfs_block_group *block_group, |
| 3207 |
struct inode *inode, |
| 3208 |
u64 ino) |
| 3209 |
{ |
| 3210 |
struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 3211 |
struct btrfs_root *root = fs_info->tree_root; |
| 3212 |
struct btrfs_trans_handle *trans; |
| 3213 |
struct btrfs_inode *btrfs_inode; |
| 3214 |
int ret = 0; |
| 3215 |
|
| 3216 |
if (inode) |
| 3217 |
goto truncate; |
| 3218 |
|
| 3219 |
btrfs_inode = btrfs_iget(ino, root); |
| 3220 |
if (IS_ERR(btrfs_inode)) |
| 3221 |
return -ENOENT; |
| 3222 |
inode = &btrfs_inode->vfs_inode; |
| 3223 |
|
| 3224 |
truncate: |
| 3225 |
ret = btrfs_check_trunc_cache_free_space(fs_info, |
| 3226 |
&fs_info->global_block_rsv); |
| 3227 |
if (ret) |
| 3228 |
goto out; |
| 3229 |
|
| 3230 |
trans = btrfs_join_transaction(root); |
| 3231 |
if (IS_ERR(trans)) { |
| 3232 |
ret = PTR_ERR(trans); |
| 3233 |
goto out; |
| 3234 |
} |
| 3235 |
|
| 3236 |
ret = btrfs_truncate_free_space_cache(trans, block_group, inode); |
| 3237 |
|
| 3238 |
btrfs_end_transaction(trans); |
| 3239 |
btrfs_btree_balance_dirty(fs_info); |
| 3240 |
out: |
| 3241 |
iput(inode); |
| 3242 |
return ret; |
| 3243 |
} |
| 3244 |
|
| 3245 |
/* |
| 3246 |
* Locate the free space cache EXTENT_DATA in root tree leaf and delete the |
| 3247 |
* cache inode, to avoid free space cache data extent blocking data relocation. |
| 3248 |
*/ |
| 3249 |
static int delete_v1_space_cache(struct extent_buffer *leaf, |
| 3250 |
struct btrfs_block_group *block_group, |
| 3251 |
u64 data_bytenr) |
| 3252 |
{ |
| 3253 |
u64 space_cache_ino; |
| 3254 |
struct btrfs_file_extent_item *ei; |
| 3255 |
struct btrfs_key key; |
| 3256 |
bool found = false; |
| 3257 |
int i; |
| 3258 |
|
| 3259 |
if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID) |
| 3260 |
return 0; |
| 3261 |
|
| 3262 |
for (i = 0; i < btrfs_header_nritems(leaf); i++) { |
| 3263 |
u8 type; |
| 3264 |
|
| 3265 |
btrfs_item_key_to_cpu(leaf, &key, i); |
| 3266 |
if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 3267 |
continue; |
| 3268 |
ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 3269 |
type = btrfs_file_extent_type(leaf, ei); |
| 3270 |
|
| 3271 |
if ((type == BTRFS_FILE_EXTENT_REG || |
| 3272 |
type == BTRFS_FILE_EXTENT_PREALLOC) && |
| 3273 |
btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) { |
| 3274 |
found = true; |
| 3275 |
space_cache_ino = key.objectid; |
| 3276 |
break; |
| 3277 |
} |
| 3278 |
} |
| 3279 |
if (!found) |
| 3280 |
return -ENOENT; |
| 3281 |
|
| 3282 |
return delete_block_group_cache(block_group, NULL, space_cache_ino); |
| 3283 |
} |
| 3284 |
|
| 3285 |
/* |
| 3286 |
* helper to find all tree blocks that reference a given data extent |
| 3287 |
*/ |
| 3288 |
static noinline_for_stack int add_data_references(struct reloc_control *rc, |
| 3289 |
const struct btrfs_key *extent_key, |
| 3290 |
struct btrfs_path *path, |
| 3291 |
struct rb_root *blocks) |
| 3292 |
{ |
| 3293 |
struct btrfs_backref_walk_ctx ctx = { 0 }; |
| 3294 |
struct ulist_iterator leaf_uiter; |
| 3295 |
struct ulist_node *ref_node = NULL; |
| 3296 |
const u32 blocksize = rc->extent_root->fs_info->nodesize; |
| 3297 |
int ret = 0; |
| 3298 |
|
| 3299 |
btrfs_release_path(path); |
| 3300 |
|
| 3301 |
ctx.bytenr = extent_key->objectid; |
| 3302 |
ctx.skip_inode_ref_list = true; |
| 3303 |
ctx.fs_info = rc->extent_root->fs_info; |
| 3304 |
|
| 3305 |
ret = btrfs_find_all_leafs(&ctx); |
| 3306 |
if (ret < 0) |
| 3307 |
return ret; |
| 3308 |
|
| 3309 |
ULIST_ITER_INIT(&leaf_uiter); |
| 3310 |
while ((ref_node = ulist_next(ctx.refs, &leaf_uiter))) { |
| 3311 |
struct btrfs_tree_parent_check check = { 0 }; |
| 3312 |
struct extent_buffer *eb; |
| 3313 |
|
| 3314 |
eb = read_tree_block(ctx.fs_info, ref_node->val, &check); |
| 3315 |
if (IS_ERR(eb)) { |
| 3316 |
ret = PTR_ERR(eb); |
| 3317 |
break; |
| 3318 |
} |
| 3319 |
ret = delete_v1_space_cache(eb, rc->block_group, |
| 3320 |
extent_key->objectid); |
| 3321 |
free_extent_buffer(eb); |
| 3322 |
if (ret < 0) |
| 3323 |
break; |
| 3324 |
ret = __add_tree_block(rc, ref_node->val, blocksize, blocks); |
| 3325 |
if (ret < 0) |
| 3326 |
break; |
| 3327 |
} |
| 3328 |
if (ret < 0) |
| 3329 |
free_block_list(blocks); |
| 3330 |
ulist_free(ctx.refs); |
| 3331 |
return ret; |
| 3332 |
} |
| 3333 |
|
| 3334 |
/* |
| 3335 |
* helper to find next unprocessed extent |
| 3336 |
*/ |
| 3337 |
static noinline_for_stack |
| 3338 |
int find_next_extent(struct reloc_control *rc, struct btrfs_path *path, |
| 3339 |
struct btrfs_key *extent_key) |
| 3340 |
{ |
| 3341 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3342 |
struct btrfs_key key; |
| 3343 |
struct extent_buffer *leaf; |
| 3344 |
u64 start, end, last; |
| 3345 |
int ret; |
| 3346 |
|
| 3347 |
last = rc->block_group->start + rc->block_group->length; |
| 3348 |
while (1) { |
| 3349 |
bool block_found; |
| 3350 |
|
| 3351 |
cond_resched(); |
| 3352 |
if (rc->search_start >= last) { |
| 3353 |
ret = 1; |
| 3354 |
break; |
| 3355 |
} |
| 3356 |
|
| 3357 |
key.objectid = rc->search_start; |
| 3358 |
key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3359 |
key.offset = 0; |
| 3360 |
|
| 3361 |
path->search_commit_root = true; |
| 3362 |
path->skip_locking = true; |
| 3363 |
ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, |
| 3364 |
0, 0); |
| 3365 |
if (ret < 0) |
| 3366 |
break; |
| 3367 |
next: |
| 3368 |
leaf = path->nodes[0]; |
| 3369 |
if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 3370 |
ret = btrfs_next_leaf(rc->extent_root, path); |
| 3371 |
if (ret != 0) |
| 3372 |
break; |
| 3373 |
leaf = path->nodes[0]; |
| 3374 |
} |
| 3375 |
|
| 3376 |
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 3377 |
if (key.objectid >= last) { |
| 3378 |
ret = 1; |
| 3379 |
break; |
| 3380 |
} |
| 3381 |
|
| 3382 |
if (key.type != BTRFS_EXTENT_ITEM_KEY && |
| 3383 |
key.type != BTRFS_METADATA_ITEM_KEY) { |
| 3384 |
path->slots[0]++; |
| 3385 |
goto next; |
| 3386 |
} |
| 3387 |
|
| 3388 |
if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3389 |
key.objectid + key.offset <= rc->search_start) { |
| 3390 |
path->slots[0]++; |
| 3391 |
goto next; |
| 3392 |
} |
| 3393 |
|
| 3394 |
if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 3395 |
key.objectid + fs_info->nodesize <= |
| 3396 |
rc->search_start) { |
| 3397 |
path->slots[0]++; |
| 3398 |
goto next; |
| 3399 |
} |
| 3400 |
|
| 3401 |
block_found = btrfs_find_first_extent_bit(&rc->processed_blocks, |
| 3402 |
key.objectid, &start, &end, |
| 3403 |
EXTENT_DIRTY, NULL); |
| 3404 |
|
| 3405 |
if (block_found && start <= key.objectid) { |
| 3406 |
btrfs_release_path(path); |
| 3407 |
rc->search_start = end + 1; |
| 3408 |
} else { |
| 3409 |
if (key.type == BTRFS_EXTENT_ITEM_KEY) |
| 3410 |
rc->search_start = key.objectid + key.offset; |
| 3411 |
else |
| 3412 |
rc->search_start = key.objectid + |
| 3413 |
fs_info->nodesize; |
| 3414 |
memcpy(extent_key, &key, sizeof(key)); |
| 3415 |
return 0; |
| 3416 |
} |
| 3417 |
} |
| 3418 |
btrfs_release_path(path); |
| 3419 |
return ret; |
| 3420 |
} |
| 3421 |
|
| 3422 |
static void set_reloc_control(struct reloc_control *rc) |
| 3423 |
{ |
| 3424 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3425 |
|
| 3426 |
mutex_lock(&fs_info->reloc_mutex); |
| 3427 |
fs_info->reloc_ctl = rc; |
| 3428 |
mutex_unlock(&fs_info->reloc_mutex); |
| 3429 |
} |
| 3430 |
|
| 3431 |
static void unset_reloc_control(struct reloc_control *rc) |
| 3432 |
{ |
| 3433 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3434 |
|
| 3435 |
mutex_lock(&fs_info->reloc_mutex); |
| 3436 |
fs_info->reloc_ctl = NULL; |
| 3437 |
mutex_unlock(&fs_info->reloc_mutex); |
| 3438 |
} |
| 3439 |
|
| 3440 |
static noinline_for_stack |
| 3441 |
int prepare_to_relocate(struct reloc_control *rc) |
| 3442 |
{ |
| 3443 |
struct btrfs_trans_handle *trans; |
| 3444 |
int ret; |
| 3445 |
|
| 3446 |
rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info, |
| 3447 |
BTRFS_BLOCK_RSV_TEMP); |
| 3448 |
if (!rc->block_rsv) |
| 3449 |
return -ENOMEM; |
| 3450 |
|
| 3451 |
memset(&rc->cluster, 0, sizeof(rc->cluster)); |
| 3452 |
rc->search_start = rc->block_group->start; |
| 3453 |
rc->extents_found = 0; |
| 3454 |
rc->nodes_relocated = 0; |
| 3455 |
rc->merging_rsv_size = 0; |
| 3456 |
rc->reserved_bytes = 0; |
| 3457 |
rc->block_rsv->size = rc->extent_root->fs_info->nodesize * |
| 3458 |
RELOCATION_RESERVED_NODES; |
| 3459 |
ret = btrfs_block_rsv_refill(rc->extent_root->fs_info, |
| 3460 |
rc->block_rsv, rc->block_rsv->size, |
| 3461 |
BTRFS_RESERVE_FLUSH_ALL); |
| 3462 |
if (ret) |
| 3463 |
return ret; |
| 3464 |
|
| 3465 |
rc->create_reloc_tree = true; |
| 3466 |
set_reloc_control(rc); |
| 3467 |
|
| 3468 |
trans = btrfs_join_transaction(rc->extent_root); |
| 3469 |
if (IS_ERR(trans)) { |
| 3470 |
unset_reloc_control(rc); |
| 3471 |
/* |
| 3472 |
* extent tree is not a ref_cow tree and has no reloc_root to |
| 3473 |
* cleanup. And callers are responsible to free the above |
| 3474 |
* block rsv. |
| 3475 |
*/ |
| 3476 |
return PTR_ERR(trans); |
| 3477 |
} |
| 3478 |
|
| 3479 |
ret = btrfs_commit_transaction(trans); |
| 3480 |
if (ret) |
| 3481 |
unset_reloc_control(rc); |
| 3482 |
|
| 3483 |
return ret; |
| 3484 |
} |
| 3485 |
|
| 3486 |
static noinline_for_stack int relocate_block_group(struct reloc_control *rc) |
| 3487 |
{ |
| 3488 |
struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; |
| 3489 |
struct rb_root blocks = RB_ROOT; |
| 3490 |
struct btrfs_key key; |
| 3491 |
struct btrfs_trans_handle *trans = NULL; |
| 3492 |
BTRFS_PATH_AUTO_FREE(path); |
| 3493 |
struct btrfs_extent_item *ei; |
| 3494 |
u64 flags; |
| 3495 |
int ret; |
| 3496 |
int err = 0; |
| 3497 |
int progress = 0; |
| 3498 |
|
| 3499 |
path = btrfs_alloc_path(); |
| 3500 |
if (!path) |
| 3501 |
return -ENOMEM; |
| 3502 |
path->reada = READA_FORWARD; |
| 3503 |
|
| 3504 |
ret = prepare_to_relocate(rc); |
| 3505 |
if (ret) { |
| 3506 |
err = ret; |
| 3507 |
goto out_free; |
| 3508 |
} |
| 3509 |
|
| 3510 |
while (1) { |
| 3511 |
rc->reserved_bytes = 0; |
| 3512 |
ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv, |
| 3513 |
rc->block_rsv->size, |
| 3514 |
BTRFS_RESERVE_FLUSH_ALL); |
| 3515 |
if (ret) { |
| 3516 |
err = ret; |
| 3517 |
break; |
| 3518 |
} |
| 3519 |
progress++; |
| 3520 |
trans = btrfs_start_transaction(rc->extent_root, 0); |
| 3521 |
if (IS_ERR(trans)) { |
| 3522 |
err = PTR_ERR(trans); |
| 3523 |
trans = NULL; |
| 3524 |
break; |
| 3525 |
} |
| 3526 |
restart: |
| 3527 |
if (rc->backref_cache.last_trans != trans->transid) |
| 3528 |
btrfs_backref_release_cache(&rc->backref_cache); |
| 3529 |
rc->backref_cache.last_trans = trans->transid; |
| 3530 |
|
| 3531 |
ret = find_next_extent(rc, path, &key); |
| 3532 |
if (ret < 0) |
| 3533 |
err = ret; |
| 3534 |
if (ret != 0) |
| 3535 |
break; |
| 3536 |
|
| 3537 |
rc->extents_found++; |
| 3538 |
|
| 3539 |
ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3540 |
struct btrfs_extent_item); |
| 3541 |
flags = btrfs_extent_flags(path->nodes[0], ei); |
| 3542 |
|
| 3543 |
/* |
| 3544 |
* If we are relocating a simple quota owned extent item, we |
| 3545 |
* need to note the owner on the reloc data root so that when |
| 3546 |
* we allocate the replacement item, we can attribute it to the |
| 3547 |
* correct eventual owner (rather than the reloc data root). |
| 3548 |
*/ |
| 3549 |
if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE) { |
| 3550 |
struct btrfs_root *root = BTRFS_I(rc->data_inode)->root; |
| 3551 |
u64 owning_root_id = btrfs_get_extent_owner_root(fs_info, |
| 3552 |
path->nodes[0], |
| 3553 |
path->slots[0]); |
| 3554 |
|
| 3555 |
root->relocation_src_root = owning_root_id; |
| 3556 |
} |
| 3557 |
|
| 3558 |
if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 3559 |
ret = add_tree_block(rc, &key, path, &blocks); |
| 3560 |
} else if (rc->stage == UPDATE_DATA_PTRS && |
| 3561 |
(flags & BTRFS_EXTENT_FLAG_DATA)) { |
| 3562 |
ret = add_data_references(rc, &key, path, &blocks); |
| 3563 |
} else { |
| 3564 |
btrfs_release_path(path); |
| 3565 |
ret = 0; |
| 3566 |
} |
| 3567 |
if (ret < 0) { |
| 3568 |
err = ret; |
| 3569 |
break; |
| 3570 |
} |
| 3571 |
|
| 3572 |
if (!RB_EMPTY_ROOT(&blocks)) { |
| 3573 |
ret = relocate_tree_blocks(trans, rc, &blocks); |
| 3574 |
if (ret < 0) { |
| 3575 |
if (ret != -EAGAIN) { |
| 3576 |
err = ret; |
| 3577 |
break; |
| 3578 |
} |
| 3579 |
rc->extents_found--; |
| 3580 |
rc->search_start = key.objectid; |
| 3581 |
} |
| 3582 |
} |
| 3583 |
|
| 3584 |
btrfs_end_transaction_throttle(trans); |
| 3585 |
btrfs_btree_balance_dirty(fs_info); |
| 3586 |
trans = NULL; |
| 3587 |
|
| 3588 |
if (rc->stage == MOVE_DATA_EXTENTS && |
| 3589 |
(flags & BTRFS_EXTENT_FLAG_DATA)) { |
| 3590 |
rc->found_file_extent = true; |
| 3591 |
ret = relocate_data_extent(rc, &key); |
| 3592 |
if (ret < 0) { |
| 3593 |
err = ret; |
| 3594 |
break; |
| 3595 |
} |
| 3596 |
} |
| 3597 |
if (btrfs_should_cancel_balance(fs_info)) { |
| 3598 |
err = -ECANCELED; |
| 3599 |
break; |
| 3600 |
} |
| 3601 |
} |
| 3602 |
if (trans && progress && err == -ENOSPC) { |
| 3603 |
ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags); |
| 3604 |
if (ret == 1) { |
| 3605 |
err = 0; |
| 3606 |
progress = 0; |
| 3607 |
goto restart; |
| 3608 |
} |
| 3609 |
} |
| 3610 |
|
| 3611 |
btrfs_release_path(path); |
| 3612 |
btrfs_clear_extent_bit(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY, NULL); |
| 3613 |
|
| 3614 |
if (trans) { |
| 3615 |
btrfs_end_transaction_throttle(trans); |
| 3616 |
btrfs_btree_balance_dirty(fs_info); |
| 3617 |
} |
| 3618 |
|
| 3619 |
if (!err && !btrfs_fs_incompat(fs_info, REMAP_TREE)) { |
| 3620 |
ret = relocate_file_extent_cluster(rc); |
| 3621 |
if (ret < 0) |
| 3622 |
err = ret; |
| 3623 |
} |
| 3624 |
|
| 3625 |
rc->create_reloc_tree = false; |
| 3626 |
set_reloc_control(rc); |
| 3627 |
|
| 3628 |
btrfs_backref_release_cache(&rc->backref_cache); |
| 3629 |
btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL); |
| 3630 |
|
| 3631 |
/* |
| 3632 |
* Even in the case when the relocation is cancelled, we should all go |
| 3633 |
* through prepare_to_merge() and merge_reloc_roots(). |
| 3634 |
* |
| 3635 |
* For error (including cancelled balance), prepare_to_merge() will |
| 3636 |
* mark all reloc trees orphan, then queue them for cleanup in |
| 3637 |
* merge_reloc_roots() |
| 3638 |
*/ |
| 3639 |
err = prepare_to_merge(rc, err); |
| 3640 |
|
| 3641 |
merge_reloc_roots(rc); |
| 3642 |
|
| 3643 |
rc->merge_reloc_tree = false; |
| 3644 |
unset_reloc_control(rc); |
| 3645 |
btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL); |
| 3646 |
|
| 3647 |
/* get rid of pinned extents */ |
| 3648 |
trans = btrfs_join_transaction(rc->extent_root); |
| 3649 |
if (IS_ERR(trans)) { |
| 3650 |
err = PTR_ERR(trans); |
| 3651 |
goto out_free; |
| 3652 |
} |
| 3653 |
ret = btrfs_commit_transaction(trans); |
| 3654 |
if (ret && !err) |
| 3655 |
err = ret; |
| 3656 |
out_free: |
| 3657 |
ret = clean_dirty_subvols(rc); |
| 3658 |
if (ret < 0 && !err) |
| 3659 |
err = ret; |
| 3660 |
btrfs_free_block_rsv(fs_info, rc->block_rsv); |
| 3661 |
return err; |
| 3662 |
} |
| 3663 |
|
| 3664 |
static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 3665 |
struct btrfs_root *root, u64 objectid) |
| 3666 |
{ |
| 3667 |
BTRFS_PATH_AUTO_FREE(path); |
| 3668 |
struct btrfs_inode_item *item; |
| 3669 |
struct extent_buffer *leaf; |
| 3670 |
int ret; |
| 3671 |
|
| 3672 |
path = btrfs_alloc_path(); |
| 3673 |
if (!path) |
| 3674 |
return -ENOMEM; |
| 3675 |
|
| 3676 |
ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 3677 |
if (ret) |
| 3678 |
return ret; |
| 3679 |
|
| 3680 |
leaf = path->nodes[0]; |
| 3681 |
item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 3682 |
memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
| 3683 |
btrfs_set_inode_generation(leaf, item, 1); |
| 3684 |
btrfs_set_inode_size(leaf, item, 0); |
| 3685 |
btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
| 3686 |
btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS | |
| 3687 |
BTRFS_INODE_PREALLOC); |
| 3688 |
return 0; |
| 3689 |
} |
| 3690 |
|
| 3691 |
static void delete_orphan_inode(struct btrfs_trans_handle *trans, |
| 3692 |
struct btrfs_root *root, u64 objectid) |
| 3693 |
{ |
| 3694 |
BTRFS_PATH_AUTO_FREE(path); |
| 3695 |
struct btrfs_key key; |
| 3696 |
int ret = 0; |
| 3697 |
|
| 3698 |
path = btrfs_alloc_path(); |
| 3699 |
if (!path) { |
| 3700 |
ret = -ENOMEM; |
| 3701 |
goto out; |
| 3702 |
} |
| 3703 |
|
| 3704 |
key.objectid = objectid; |
| 3705 |
key.type = BTRFS_INODE_ITEM_KEY; |
| 3706 |
key.offset = 0; |
| 3707 |
ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3708 |
if (ret) { |
| 3709 |
if (ret > 0) |
| 3710 |
ret = -ENOENT; |
| 3711 |
goto out; |
| 3712 |
} |
| 3713 |
ret = btrfs_del_item(trans, root, path); |
| 3714 |
out: |
| 3715 |
if (ret) |
| 3716 |
btrfs_abort_transaction(trans, ret); |
| 3717 |
} |
| 3718 |
|
| 3719 |
/* |
| 3720 |
* helper to create inode for data relocation. |
| 3721 |
* the inode is in data relocation tree and its link count is 0 |
| 3722 |
*/ |
| 3723 |
static noinline_for_stack struct inode *create_reloc_inode( |
| 3724 |
const struct btrfs_block_group *group) |
| 3725 |
{ |
| 3726 |
struct btrfs_fs_info *fs_info = group->fs_info; |
| 3727 |
struct btrfs_inode *inode = NULL; |
| 3728 |
struct btrfs_trans_handle *trans; |
| 3729 |
struct btrfs_root *root; |
| 3730 |
u64 objectid; |
| 3731 |
int ret = 0; |
| 3732 |
|
| 3733 |
root = btrfs_grab_root(fs_info->data_reloc_root); |
| 3734 |
trans = btrfs_start_transaction(root, 6); |
| 3735 |
if (IS_ERR(trans)) { |
| 3736 |
btrfs_put_root(root); |
| 3737 |
return ERR_CAST(trans); |
| 3738 |
} |
| 3739 |
|
| 3740 |
ret = btrfs_get_free_objectid(root, &objectid); |
| 3741 |
if (ret) |
| 3742 |
goto out; |
| 3743 |
|
| 3744 |
ret = __insert_orphan_inode(trans, root, objectid); |
| 3745 |
if (ret) |
| 3746 |
goto out; |
| 3747 |
|
| 3748 |
inode = btrfs_iget(objectid, root); |
| 3749 |
if (IS_ERR(inode)) { |
| 3750 |
delete_orphan_inode(trans, root, objectid); |
| 3751 |
ret = PTR_ERR(inode); |
| 3752 |
inode = NULL; |
| 3753 |
goto out; |
| 3754 |
} |
| 3755 |
inode->reloc_block_group_start = group->start; |
| 3756 |
|
| 3757 |
ret = btrfs_orphan_add(trans, inode); |
| 3758 |
out: |
| 3759 |
btrfs_put_root(root); |
| 3760 |
btrfs_end_transaction(trans); |
| 3761 |
btrfs_btree_balance_dirty(fs_info); |
| 3762 |
if (ret) { |
| 3763 |
if (inode) |
| 3764 |
iput(&inode->vfs_inode); |
| 3765 |
return ERR_PTR(ret); |
| 3766 |
} |
| 3767 |
return &inode->vfs_inode; |
| 3768 |
} |
| 3769 |
|
| 3770 |
/* |
| 3771 |
* Mark start of chunk relocation that is cancellable. Check if the cancellation |
| 3772 |
* has been requested meanwhile and don't start in that case. |
| 3773 |
* NOTE: if this returns an error, reloc_chunk_end() must not be called. |
| 3774 |
* |
| 3775 |
* Return: |
| 3776 |
* 0 success |
| 3777 |
* -EINPROGRESS operation is already in progress, that's probably a bug |
| 3778 |
* -ECANCELED cancellation request was set before the operation started |
| 3779 |
*/ |
| 3780 |
static int reloc_chunk_start(struct btrfs_fs_info *fs_info) |
| 3781 |
{ |
| 3782 |
if (test_and_set_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) { |
| 3783 |
/* This should not happen */ |
| 3784 |
btrfs_err(fs_info, "reloc already running, cannot start"); |
| 3785 |
return -EINPROGRESS; |
| 3786 |
} |
| 3787 |
|
| 3788 |
if (atomic_read(&fs_info->reloc_cancel_req) > 0) { |
| 3789 |
btrfs_info(fs_info, "chunk relocation canceled on start"); |
| 3790 |
/* On cancel, clear all requests. */ |
| 3791 |
clear_and_wake_up_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags); |
| 3792 |
atomic_set(&fs_info->reloc_cancel_req, 0); |
| 3793 |
return -ECANCELED; |
| 3794 |
} |
| 3795 |
return 0; |
| 3796 |
} |
| 3797 |
|
| 3798 |
/* |
| 3799 |
* Mark end of chunk relocation that is cancellable and wake any waiters. |
| 3800 |
* NOTE: call only if a previous call to reloc_chunk_start() succeeded. |
| 3801 |
*/ |
| 3802 |
static void reloc_chunk_end(struct btrfs_fs_info *fs_info) |
| 3803 |
{ |
| 3804 |
ASSERT(test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)); |
| 3805 |
/* Requested after start, clear bit first so any waiters can continue */ |
| 3806 |
if (atomic_read(&fs_info->reloc_cancel_req) > 0) |
| 3807 |
btrfs_info(fs_info, "chunk relocation canceled during operation"); |
| 3808 |
clear_and_wake_up_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags); |
| 3809 |
atomic_set(&fs_info->reloc_cancel_req, 0); |
| 3810 |
} |
| 3811 |
|
| 3812 |
static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info) |
| 3813 |
{ |
| 3814 |
struct reloc_control *rc; |
| 3815 |
|
| 3816 |
rc = kzalloc_obj(*rc, GFP_NOFS); |
| 3817 |
if (!rc) |
| 3818 |
return NULL; |
| 3819 |
|
| 3820 |
INIT_LIST_HEAD(&rc->reloc_roots); |
| 3821 |
INIT_LIST_HEAD(&rc->dirty_subvol_roots); |
| 3822 |
btrfs_backref_init_cache(fs_info, &rc->backref_cache, true); |
| 3823 |
rc->reloc_root_tree.rb_root = RB_ROOT; |
| 3824 |
spin_lock_init(&rc->reloc_root_tree.lock); |
| 3825 |
btrfs_extent_io_tree_init(fs_info, &rc->processed_blocks, IO_TREE_RELOC_BLOCKS); |
| 3826 |
return rc; |
| 3827 |
} |
| 3828 |
|
| 3829 |
static void free_reloc_control(struct reloc_control *rc) |
| 3830 |
{ |
| 3831 |
struct mapping_node *node, *tmp; |
| 3832 |
|
| 3833 |
free_reloc_roots(&rc->reloc_roots); |
| 3834 |
rbtree_postorder_for_each_entry_safe(node, tmp, |
| 3835 |
&rc->reloc_root_tree.rb_root, rb_node) |
| 3836 |
kfree(node); |
| 3837 |
|
| 3838 |
kfree(rc); |
| 3839 |
} |
| 3840 |
|
| 3841 |
/* |
| 3842 |
* Print the block group being relocated |
| 3843 |
*/ |
| 3844 |
static void describe_relocation(struct btrfs_block_group *block_group) |
| 3845 |
{ |
| 3846 |
char buf[128] = "NONE"; |
| 3847 |
|
| 3848 |
btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf)); |
| 3849 |
|
| 3850 |
btrfs_info(block_group->fs_info, "relocating block group %llu flags %s", |
| 3851 |
block_group->start, buf); |
| 3852 |
} |
| 3853 |
|
| 3854 |
static const char *stage_to_string(enum reloc_stage stage) |
| 3855 |
{ |
| 3856 |
if (stage == MOVE_DATA_EXTENTS) |
| 3857 |
return "move data extents"; |
| 3858 |
if (stage == UPDATE_DATA_PTRS) |
| 3859 |
return "update data pointers"; |
| 3860 |
return "unknown"; |
| 3861 |
} |
| 3862 |
|
| 3863 |
static int add_remap_tree_entries(struct btrfs_trans_handle *trans, struct btrfs_path *path, |
| 3864 |
struct btrfs_key *entries, unsigned int num_entries) |
| 3865 |
{ |
| 3866 |
int ret; |
| 3867 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 3868 |
struct btrfs_item_batch batch; |
| 3869 |
u32 *data_sizes; |
| 3870 |
u32 max_items; |
| 3871 |
|
| 3872 |
max_items = BTRFS_LEAF_DATA_SIZE(trans->fs_info) / sizeof(struct btrfs_item); |
| 3873 |
|
| 3874 |
data_sizes = kzalloc(sizeof(u32) * min_t(u32, num_entries, max_items), GFP_NOFS); |
| 3875 |
if (!data_sizes) |
| 3876 |
return -ENOMEM; |
| 3877 |
|
| 3878 |
while (true) { |
| 3879 |
batch.keys = entries; |
| 3880 |
batch.data_sizes = data_sizes; |
| 3881 |
batch.total_data_size = 0; |
| 3882 |
batch.nr = min_t(u32, num_entries, max_items); |
| 3883 |
|
| 3884 |
ret = btrfs_insert_empty_items(trans, fs_info->remap_root, path, &batch); |
| 3885 |
btrfs_release_path(path); |
| 3886 |
|
| 3887 |
if (ret || num_entries <= max_items) |
| 3888 |
break; |
| 3889 |
|
| 3890 |
num_entries -= max_items; |
| 3891 |
entries += max_items; |
| 3892 |
} |
| 3893 |
|
| 3894 |
kfree(data_sizes); |
| 3895 |
|
| 3896 |
return ret; |
| 3897 |
} |
| 3898 |
|
| 3899 |
struct space_run { |
| 3900 |
u64 start; |
| 3901 |
u64 end; |
| 3902 |
}; |
| 3903 |
|
| 3904 |
static void parse_bitmap(u64 block_size, const unsigned long *bitmap, |
| 3905 |
unsigned long size, u64 address, struct space_run *space_runs, |
| 3906 |
unsigned int *num_space_runs) |
| 3907 |
{ |
| 3908 |
unsigned long pos, end; |
| 3909 |
u64 run_start, run_length; |
| 3910 |
|
| 3911 |
pos = find_first_bit(bitmap, size); |
| 3912 |
if (pos == size) |
| 3913 |
return; |
| 3914 |
|
| 3915 |
while (true) { |
| 3916 |
end = find_next_zero_bit(bitmap, size, pos); |
| 3917 |
|
| 3918 |
run_start = address + (pos * block_size); |
| 3919 |
run_length = (end - pos) * block_size; |
| 3920 |
|
| 3921 |
if (*num_space_runs != 0 && |
| 3922 |
space_runs[*num_space_runs - 1].end == run_start) { |
| 3923 |
space_runs[*num_space_runs - 1].end += run_length; |
| 3924 |
} else { |
| 3925 |
space_runs[*num_space_runs].start = run_start; |
| 3926 |
space_runs[*num_space_runs].end = run_start + run_length; |
| 3927 |
|
| 3928 |
(*num_space_runs)++; |
| 3929 |
} |
| 3930 |
|
| 3931 |
if (end == size) |
| 3932 |
break; |
| 3933 |
|
| 3934 |
pos = find_next_bit(bitmap, size, end + 1); |
| 3935 |
if (pos == size) |
| 3936 |
break; |
| 3937 |
} |
| 3938 |
} |
| 3939 |
|
| 3940 |
static void adjust_block_group_remap_bytes(struct btrfs_trans_handle *trans, |
| 3941 |
struct btrfs_block_group *bg, s64 diff) |
| 3942 |
{ |
| 3943 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 3944 |
bool bg_already_dirty = true; |
| 3945 |
bool mark_unused = false; |
| 3946 |
|
| 3947 |
spin_lock(&bg->lock); |
| 3948 |
bg->remap_bytes += diff; |
| 3949 |
if (bg->used == 0 && bg->remap_bytes == 0) |
| 3950 |
mark_unused = true; |
| 3951 |
spin_unlock(&bg->lock); |
| 3952 |
|
| 3953 |
if (mark_unused) |
| 3954 |
btrfs_mark_bg_unused(bg); |
| 3955 |
|
| 3956 |
spin_lock(&trans->transaction->dirty_bgs_lock); |
| 3957 |
if (list_empty(&bg->dirty_list)) { |
| 3958 |
list_add_tail(&bg->dirty_list, &trans->transaction->dirty_bgs); |
| 3959 |
bg_already_dirty = false; |
| 3960 |
btrfs_get_block_group(bg); |
| 3961 |
} |
| 3962 |
spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 3963 |
|
| 3964 |
/* Modified block groups are accounted for in the delayed_refs_rsv. */ |
| 3965 |
if (!bg_already_dirty) |
| 3966 |
btrfs_inc_delayed_refs_rsv_bg_updates(fs_info); |
| 3967 |
} |
| 3968 |
|
| 3969 |
/* Private structure for I/O from copy_remapped_data(). */ |
| 3970 |
struct reloc_io_private { |
| 3971 |
struct completion done; |
| 3972 |
refcount_t pending_refs; |
| 3973 |
blk_status_t status; |
| 3974 |
}; |
| 3975 |
|
| 3976 |
static void reloc_endio(struct btrfs_bio *bbio) |
| 3977 |
{ |
| 3978 |
struct reloc_io_private *priv = bbio->private; |
| 3979 |
|
| 3980 |
if (bbio->bio.bi_status) |
| 3981 |
WRITE_ONCE(priv->status, bbio->bio.bi_status); |
| 3982 |
|
| 3983 |
if (refcount_dec_and_test(&priv->pending_refs)) |
| 3984 |
complete(&priv->done); |
| 3985 |
|
| 3986 |
bio_put(&bbio->bio); |
| 3987 |
} |
| 3988 |
|
| 3989 |
static int copy_remapped_data_io(struct btrfs_fs_info *fs_info, |
| 3990 |
struct reloc_io_private *priv, |
| 3991 |
struct page **pages, u64 addr, u64 length, |
| 3992 |
blk_opf_t op) |
| 3993 |
{ |
| 3994 |
struct btrfs_bio *bbio; |
| 3995 |
int i; |
| 3996 |
|
| 3997 |
init_completion(&priv->done); |
| 3998 |
refcount_set(&priv->pending_refs, 1); |
| 3999 |
priv->status = 0; |
| 4000 |
|
| 4001 |
bbio = btrfs_bio_alloc(BIO_MAX_VECS, op, BTRFS_I(fs_info->btree_inode), |
| 4002 |
addr, reloc_endio, priv); |
| 4003 |
bbio->bio.bi_iter.bi_sector = (addr >> SECTOR_SHIFT); |
| 4004 |
bbio->is_remap = true; |
| 4005 |
|
| 4006 |
i = 0; |
| 4007 |
do { |
| 4008 |
size_t bytes = min_t(u64, length, PAGE_SIZE); |
| 4009 |
|
| 4010 |
if (bio_add_page(&bbio->bio, pages[i], bytes, 0) < bytes) { |
| 4011 |
refcount_inc(&priv->pending_refs); |
| 4012 |
btrfs_submit_bbio(bbio, 0); |
| 4013 |
|
| 4014 |
bbio = btrfs_bio_alloc(BIO_MAX_VECS, op, |
| 4015 |
BTRFS_I(fs_info->btree_inode), |
| 4016 |
addr, reloc_endio, priv); |
| 4017 |
bbio->bio.bi_iter.bi_sector = (addr >> SECTOR_SHIFT); |
| 4018 |
bbio->is_remap = true; |
| 4019 |
continue; |
| 4020 |
} |
| 4021 |
|
| 4022 |
i++; |
| 4023 |
addr += bytes; |
| 4024 |
length -= bytes; |
| 4025 |
} while (length); |
| 4026 |
|
| 4027 |
refcount_inc(&priv->pending_refs); |
| 4028 |
btrfs_submit_bbio(bbio, 0); |
| 4029 |
|
| 4030 |
if (!refcount_dec_and_test(&priv->pending_refs)) |
| 4031 |
wait_for_completion_io(&priv->done); |
| 4032 |
|
| 4033 |
return blk_status_to_errno(READ_ONCE(priv->status)); |
| 4034 |
} |
| 4035 |
|
| 4036 |
static int copy_remapped_data(struct btrfs_fs_info *fs_info, u64 old_addr, |
| 4037 |
u64 new_addr, u64 length) |
| 4038 |
{ |
| 4039 |
int ret; |
| 4040 |
u64 copy_len = min_t(u64, length, SZ_1M); |
| 4041 |
struct page **pages; |
| 4042 |
struct reloc_io_private priv; |
| 4043 |
unsigned int nr_pages = DIV_ROUND_UP(length, PAGE_SIZE); |
| 4044 |
|
| 4045 |
pages = kzalloc_objs(struct page *, nr_pages, GFP_NOFS); |
| 4046 |
if (!pages) |
| 4047 |
return -ENOMEM; |
| 4048 |
|
| 4049 |
ret = btrfs_alloc_page_array(nr_pages, pages, 0); |
| 4050 |
if (ret) { |
| 4051 |
ret = -ENOMEM; |
| 4052 |
goto end; |
| 4053 |
} |
| 4054 |
|
| 4055 |
/* Copy 1MB at a time, to avoid using too much memory. */ |
| 4056 |
do { |
| 4057 |
u64 to_copy = min_t(u64, length, copy_len); |
| 4058 |
|
| 4059 |
/* Limit to one bio. */ |
| 4060 |
to_copy = min_t(u64, to_copy, BIO_MAX_VECS << PAGE_SHIFT); |
| 4061 |
|
| 4062 |
ret = copy_remapped_data_io(fs_info, &priv, pages, old_addr, |
| 4063 |
to_copy, REQ_OP_READ); |
| 4064 |
if (ret) |
| 4065 |
goto end; |
| 4066 |
|
| 4067 |
ret = copy_remapped_data_io(fs_info, &priv, pages, new_addr, |
| 4068 |
to_copy, REQ_OP_WRITE); |
| 4069 |
if (ret) |
| 4070 |
goto end; |
| 4071 |
|
| 4072 |
if (to_copy == length) |
| 4073 |
break; |
| 4074 |
|
| 4075 |
old_addr += to_copy; |
| 4076 |
new_addr += to_copy; |
| 4077 |
length -= to_copy; |
| 4078 |
} while (true); |
| 4079 |
|
| 4080 |
ret = 0; |
| 4081 |
end: |
| 4082 |
for (int i = 0; i < nr_pages; i++) { |
| 4083 |
if (pages[i]) |
| 4084 |
__free_page(pages[i]); |
| 4085 |
} |
| 4086 |
kfree(pages); |
| 4087 |
|
| 4088 |
return ret; |
| 4089 |
} |
| 4090 |
|
| 4091 |
static int add_remap_item(struct btrfs_trans_handle *trans, |
| 4092 |
struct btrfs_path *path, u64 new_addr, u64 length, |
| 4093 |
u64 old_addr) |
| 4094 |
{ |
| 4095 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 4096 |
struct btrfs_remap_item remap = { 0 }; |
| 4097 |
struct btrfs_key key; |
| 4098 |
struct extent_buffer *leaf; |
| 4099 |
int ret; |
| 4100 |
|
| 4101 |
key.objectid = old_addr; |
| 4102 |
key.type = BTRFS_REMAP_KEY; |
| 4103 |
key.offset = length; |
| 4104 |
|
| 4105 |
ret = btrfs_insert_empty_item(trans, fs_info->remap_root, path, |
| 4106 |
&key, sizeof(struct btrfs_remap_item)); |
| 4107 |
if (ret) |
| 4108 |
return ret; |
| 4109 |
|
| 4110 |
leaf = path->nodes[0]; |
| 4111 |
btrfs_set_stack_remap_address(&remap, new_addr); |
| 4112 |
write_extent_buffer(leaf, &remap, btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4113 |
sizeof(struct btrfs_remap_item)); |
| 4114 |
|
| 4115 |
btrfs_release_path(path); |
| 4116 |
|
| 4117 |
return 0; |
| 4118 |
} |
| 4119 |
|
| 4120 |
static int add_remap_backref_item(struct btrfs_trans_handle *trans, |
| 4121 |
struct btrfs_path *path, u64 new_addr, |
| 4122 |
u64 length, u64 old_addr) |
| 4123 |
{ |
| 4124 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 4125 |
struct btrfs_remap_item remap = { 0 }; |
| 4126 |
struct btrfs_key key; |
| 4127 |
struct extent_buffer *leaf; |
| 4128 |
int ret; |
| 4129 |
|
| 4130 |
key.objectid = new_addr; |
| 4131 |
key.type = BTRFS_REMAP_BACKREF_KEY; |
| 4132 |
key.offset = length; |
| 4133 |
|
| 4134 |
ret = btrfs_insert_empty_item(trans, fs_info->remap_root, path, &key, |
| 4135 |
sizeof(struct btrfs_remap_item)); |
| 4136 |
if (ret) |
| 4137 |
return ret; |
| 4138 |
|
| 4139 |
leaf = path->nodes[0]; |
| 4140 |
btrfs_set_stack_remap_address(&remap, old_addr); |
| 4141 |
write_extent_buffer(leaf, &remap, btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4142 |
sizeof(struct btrfs_remap_item)); |
| 4143 |
|
| 4144 |
btrfs_release_path(path); |
| 4145 |
|
| 4146 |
return 0; |
| 4147 |
} |
| 4148 |
|
| 4149 |
static int move_existing_remap(struct btrfs_fs_info *fs_info, |
| 4150 |
struct btrfs_path *path, |
| 4151 |
struct btrfs_block_group *bg, u64 new_addr, |
| 4152 |
u64 length, u64 old_addr) |
| 4153 |
{ |
| 4154 |
struct btrfs_trans_handle *trans; |
| 4155 |
struct extent_buffer *leaf; |
| 4156 |
struct btrfs_remap_item *remap_ptr; |
| 4157 |
struct btrfs_remap_item remap = { 0 }; |
| 4158 |
struct btrfs_key key, ins; |
| 4159 |
u64 dest_addr, dest_length, min_size; |
| 4160 |
struct btrfs_block_group *dest_bg; |
| 4161 |
int ret; |
| 4162 |
const bool is_data = (bg->flags & BTRFS_BLOCK_GROUP_DATA); |
| 4163 |
struct btrfs_space_info *sinfo = bg->space_info; |
| 4164 |
bool mutex_taken = false; |
| 4165 |
bool bg_needs_free_space; |
| 4166 |
|
| 4167 |
spin_lock(&sinfo->lock); |
| 4168 |
btrfs_space_info_update_bytes_may_use(sinfo, length); |
| 4169 |
spin_unlock(&sinfo->lock); |
| 4170 |
|
| 4171 |
if (is_data) |
| 4172 |
min_size = fs_info->sectorsize; |
| 4173 |
else |
| 4174 |
min_size = fs_info->nodesize; |
| 4175 |
|
| 4176 |
ret = btrfs_reserve_extent(fs_info->fs_root, length, length, min_size, |
| 4177 |
0, 0, &ins, is_data, false); |
| 4178 |
if (unlikely(ret)) { |
| 4179 |
spin_lock(&sinfo->lock); |
| 4180 |
btrfs_space_info_update_bytes_may_use(sinfo, -length); |
| 4181 |
spin_unlock(&sinfo->lock); |
| 4182 |
return ret; |
| 4183 |
} |
| 4184 |
|
| 4185 |
if (ins.offset < length) { |
| 4186 |
spin_lock(&sinfo->lock); |
| 4187 |
btrfs_space_info_update_bytes_may_use(sinfo, ins.offset - length); |
| 4188 |
spin_unlock(&sinfo->lock); |
| 4189 |
} |
| 4190 |
|
| 4191 |
dest_addr = ins.objectid; |
| 4192 |
dest_length = ins.offset; |
| 4193 |
|
| 4194 |
dest_bg = btrfs_lookup_block_group(fs_info, dest_addr); |
| 4195 |
|
| 4196 |
if (!is_data && !IS_ALIGNED(dest_length, fs_info->nodesize)) { |
| 4197 |
u64 new_length = ALIGN_DOWN(dest_length, fs_info->nodesize); |
| 4198 |
|
| 4199 |
btrfs_free_reserved_extent(fs_info, dest_addr + new_length, |
| 4200 |
dest_length - new_length, 0); |
| 4201 |
|
| 4202 |
dest_length = new_length; |
| 4203 |
} |
| 4204 |
|
| 4205 |
trans = btrfs_join_transaction(fs_info->remap_root); |
| 4206 |
if (IS_ERR(trans)) { |
| 4207 |
ret = PTR_ERR(trans); |
| 4208 |
trans = NULL; |
| 4209 |
goto end; |
| 4210 |
} |
| 4211 |
|
| 4212 |
mutex_lock(&fs_info->remap_mutex); |
| 4213 |
mutex_taken = true; |
| 4214 |
|
| 4215 |
/* Find old remap entry. */ |
| 4216 |
key.objectid = old_addr; |
| 4217 |
key.type = BTRFS_REMAP_KEY; |
| 4218 |
key.offset = length; |
| 4219 |
|
| 4220 |
ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1); |
| 4221 |
if (ret == 1) { |
| 4222 |
/* |
| 4223 |
* Not a problem if the remap entry wasn't found: that means |
| 4224 |
* that another transaction has deallocated the data. |
| 4225 |
* move_existing_remaps() loops until the BG contains no |
| 4226 |
* remaps, so we can just return 0 in this case. |
| 4227 |
*/ |
| 4228 |
btrfs_release_path(path); |
| 4229 |
ret = 0; |
| 4230 |
goto end; |
| 4231 |
} else if (unlikely(ret)) { |
| 4232 |
goto end; |
| 4233 |
} |
| 4234 |
|
| 4235 |
ret = copy_remapped_data(fs_info, new_addr, dest_addr, dest_length); |
| 4236 |
if (unlikely(ret)) |
| 4237 |
goto end; |
| 4238 |
|
| 4239 |
/* Change data of old remap entry. */ |
| 4240 |
leaf = path->nodes[0]; |
| 4241 |
remap_ptr = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_remap_item); |
| 4242 |
btrfs_set_remap_address(leaf, remap_ptr, dest_addr); |
| 4243 |
btrfs_mark_buffer_dirty(trans, leaf); |
| 4244 |
|
| 4245 |
if (dest_length != length) { |
| 4246 |
key.offset = dest_length; |
| 4247 |
btrfs_set_item_key_safe(trans, path, &key); |
| 4248 |
} |
| 4249 |
|
| 4250 |
btrfs_release_path(path); |
| 4251 |
|
| 4252 |
if (dest_length != length) { |
| 4253 |
/* Add remap item for remainder. */ |
| 4254 |
ret = add_remap_item(trans, path, new_addr + dest_length, |
| 4255 |
length - dest_length, old_addr + dest_length); |
| 4256 |
if (unlikely(ret)) |
| 4257 |
goto end; |
| 4258 |
} |
| 4259 |
|
| 4260 |
/* Change or remove old backref. */ |
| 4261 |
key.objectid = new_addr; |
| 4262 |
key.type = BTRFS_REMAP_BACKREF_KEY; |
| 4263 |
key.offset = length; |
| 4264 |
|
| 4265 |
ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, -1, 1); |
| 4266 |
if (unlikely(ret)) { |
| 4267 |
if (ret == 1) { |
| 4268 |
btrfs_release_path(path); |
| 4269 |
ret = -ENOENT; |
| 4270 |
} |
| 4271 |
goto end; |
| 4272 |
} |
| 4273 |
|
| 4274 |
leaf = path->nodes[0]; |
| 4275 |
|
| 4276 |
if (dest_length == length) { |
| 4277 |
ret = btrfs_del_item(trans, fs_info->remap_root, path); |
| 4278 |
if (unlikely(ret)) { |
| 4279 |
btrfs_release_path(path); |
| 4280 |
goto end; |
| 4281 |
} |
| 4282 |
} else { |
| 4283 |
key.objectid += dest_length; |
| 4284 |
key.offset -= dest_length; |
| 4285 |
btrfs_set_item_key_safe(trans, path, &key); |
| 4286 |
btrfs_set_stack_remap_address(&remap, old_addr + dest_length); |
| 4287 |
|
| 4288 |
write_extent_buffer(leaf, &remap, |
| 4289 |
btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4290 |
sizeof(struct btrfs_remap_item)); |
| 4291 |
} |
| 4292 |
|
| 4293 |
btrfs_release_path(path); |
| 4294 |
|
| 4295 |
/* Add new backref. */ |
| 4296 |
ret = add_remap_backref_item(trans, path, dest_addr, dest_length, old_addr); |
| 4297 |
if (unlikely(ret)) |
| 4298 |
goto end; |
| 4299 |
|
| 4300 |
adjust_block_group_remap_bytes(trans, bg, -dest_length); |
| 4301 |
|
| 4302 |
ret = btrfs_add_to_free_space_tree(trans, new_addr, dest_length); |
| 4303 |
if (unlikely(ret)) |
| 4304 |
goto end; |
| 4305 |
|
| 4306 |
adjust_block_group_remap_bytes(trans, dest_bg, dest_length); |
| 4307 |
|
| 4308 |
mutex_lock(&dest_bg->free_space_lock); |
| 4309 |
bg_needs_free_space = test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, |
| 4310 |
&dest_bg->runtime_flags); |
| 4311 |
mutex_unlock(&dest_bg->free_space_lock); |
| 4312 |
|
| 4313 |
if (bg_needs_free_space) { |
| 4314 |
ret = btrfs_add_block_group_free_space(trans, dest_bg); |
| 4315 |
if (unlikely(ret)) |
| 4316 |
goto end; |
| 4317 |
} |
| 4318 |
|
| 4319 |
ret = btrfs_remove_from_free_space_tree(trans, dest_addr, dest_length); |
| 4320 |
if (unlikely(ret)) { |
| 4321 |
btrfs_remove_from_free_space_tree(trans, new_addr, dest_length); |
| 4322 |
goto end; |
| 4323 |
} |
| 4324 |
|
| 4325 |
ret = 0; |
| 4326 |
|
| 4327 |
end: |
| 4328 |
if (mutex_taken) |
| 4329 |
mutex_unlock(&fs_info->remap_mutex); |
| 4330 |
|
| 4331 |
btrfs_dec_block_group_reservations(fs_info, dest_addr); |
| 4332 |
|
| 4333 |
if (unlikely(ret)) { |
| 4334 |
btrfs_free_reserved_extent(fs_info, dest_addr, dest_length, 0); |
| 4335 |
|
| 4336 |
if (trans) { |
| 4337 |
btrfs_abort_transaction(trans, ret); |
| 4338 |
btrfs_end_transaction(trans); |
| 4339 |
} |
| 4340 |
} else { |
| 4341 |
btrfs_free_reserved_bytes(dest_bg, dest_length, 0); |
| 4342 |
|
| 4343 |
ret = btrfs_commit_transaction(trans); |
| 4344 |
} |
| 4345 |
|
| 4346 |
btrfs_put_block_group(dest_bg); |
| 4347 |
|
| 4348 |
return ret; |
| 4349 |
} |
| 4350 |
|
| 4351 |
static int move_existing_remaps(struct btrfs_fs_info *fs_info, |
| 4352 |
struct btrfs_block_group *bg, |
| 4353 |
struct btrfs_path *path) |
| 4354 |
{ |
| 4355 |
int ret; |
| 4356 |
struct btrfs_key key; |
| 4357 |
struct extent_buffer *leaf; |
| 4358 |
struct btrfs_remap_item *remap; |
| 4359 |
u64 old_addr; |
| 4360 |
|
| 4361 |
/* Look for backrefs in remap tree. */ |
| 4362 |
while (bg->remap_bytes > 0) { |
| 4363 |
key.objectid = bg->start; |
| 4364 |
key.type = BTRFS_REMAP_BACKREF_KEY; |
| 4365 |
key.offset = 0; |
| 4366 |
|
| 4367 |
ret = btrfs_search_slot(NULL, fs_info->remap_root, &key, path, 0, 0); |
| 4368 |
if (ret < 0) |
| 4369 |
return ret; |
| 4370 |
|
| 4371 |
leaf = path->nodes[0]; |
| 4372 |
|
| 4373 |
if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4374 |
ret = btrfs_next_leaf(fs_info->remap_root, path); |
| 4375 |
if (ret < 0) { |
| 4376 |
btrfs_release_path(path); |
| 4377 |
return ret; |
| 4378 |
} |
| 4379 |
|
| 4380 |
if (ret) { |
| 4381 |
btrfs_release_path(path); |
| 4382 |
break; |
| 4383 |
} |
| 4384 |
|
| 4385 |
leaf = path->nodes[0]; |
| 4386 |
} |
| 4387 |
|
| 4388 |
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4389 |
|
| 4390 |
if (key.type != BTRFS_REMAP_BACKREF_KEY) { |
| 4391 |
path->slots[0]++; |
| 4392 |
|
| 4393 |
if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4394 |
ret = btrfs_next_leaf(fs_info->remap_root, path); |
| 4395 |
if (ret < 0) { |
| 4396 |
btrfs_release_path(path); |
| 4397 |
return ret; |
| 4398 |
} |
| 4399 |
|
| 4400 |
if (ret) { |
| 4401 |
btrfs_release_path(path); |
| 4402 |
break; |
| 4403 |
} |
| 4404 |
|
| 4405 |
leaf = path->nodes[0]; |
| 4406 |
} |
| 4407 |
|
| 4408 |
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4409 |
} |
| 4410 |
|
| 4411 |
remap = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_remap_item); |
| 4412 |
old_addr = btrfs_remap_address(leaf, remap); |
| 4413 |
|
| 4414 |
btrfs_release_path(path); |
| 4415 |
|
| 4416 |
ret = move_existing_remap(fs_info, path, bg, key.objectid, |
| 4417 |
key.offset, old_addr); |
| 4418 |
if (ret) |
| 4419 |
return ret; |
| 4420 |
} |
| 4421 |
|
| 4422 |
ASSERT(bg->remap_bytes == 0); |
| 4423 |
|
| 4424 |
return 0; |
| 4425 |
} |
| 4426 |
|
| 4427 |
static int create_remap_tree_entries(struct btrfs_trans_handle *trans, |
| 4428 |
struct btrfs_path *path, |
| 4429 |
struct btrfs_block_group *bg) |
| 4430 |
{ |
| 4431 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 4432 |
struct btrfs_free_space_info *fsi; |
| 4433 |
struct btrfs_key key, found_key; |
| 4434 |
struct extent_buffer *leaf; |
| 4435 |
struct btrfs_root *space_root; |
| 4436 |
u32 extent_count; |
| 4437 |
struct space_run *space_runs = NULL; |
| 4438 |
unsigned int num_space_runs = 0; |
| 4439 |
struct btrfs_key *entries = NULL; |
| 4440 |
unsigned int max_entries, num_entries; |
| 4441 |
int ret; |
| 4442 |
|
| 4443 |
mutex_lock(&bg->free_space_lock); |
| 4444 |
|
| 4445 |
if (test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &bg->runtime_flags)) { |
| 4446 |
mutex_unlock(&bg->free_space_lock); |
| 4447 |
|
| 4448 |
ret = btrfs_add_block_group_free_space(trans, bg); |
| 4449 |
if (ret) |
| 4450 |
return ret; |
| 4451 |
|
| 4452 |
mutex_lock(&bg->free_space_lock); |
| 4453 |
} |
| 4454 |
|
| 4455 |
fsi = btrfs_search_free_space_info(trans, bg, path, 0); |
| 4456 |
if (IS_ERR(fsi)) { |
| 4457 |
mutex_unlock(&bg->free_space_lock); |
| 4458 |
return PTR_ERR(fsi); |
| 4459 |
} |
| 4460 |
|
| 4461 |
extent_count = btrfs_free_space_extent_count(path->nodes[0], fsi); |
| 4462 |
|
| 4463 |
btrfs_release_path(path); |
| 4464 |
|
| 4465 |
space_runs = kmalloc(sizeof(*space_runs) * extent_count, GFP_NOFS); |
| 4466 |
if (!space_runs) { |
| 4467 |
mutex_unlock(&bg->free_space_lock); |
| 4468 |
return -ENOMEM; |
| 4469 |
} |
| 4470 |
|
| 4471 |
key.objectid = bg->start; |
| 4472 |
key.type = 0; |
| 4473 |
key.offset = 0; |
| 4474 |
|
| 4475 |
space_root = btrfs_free_space_root(bg); |
| 4476 |
|
| 4477 |
ret = btrfs_search_slot(trans, space_root, &key, path, 0, 0); |
| 4478 |
if (ret < 0) { |
| 4479 |
mutex_unlock(&bg->free_space_lock); |
| 4480 |
goto out; |
| 4481 |
} |
| 4482 |
|
| 4483 |
ret = 0; |
| 4484 |
|
| 4485 |
while (true) { |
| 4486 |
leaf = path->nodes[0]; |
| 4487 |
|
| 4488 |
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4489 |
|
| 4490 |
if (found_key.objectid >= bg->start + bg->length) |
| 4491 |
break; |
| 4492 |
|
| 4493 |
if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) { |
| 4494 |
if (num_space_runs != 0 && |
| 4495 |
space_runs[num_space_runs - 1].end == found_key.objectid) { |
| 4496 |
space_runs[num_space_runs - 1].end = |
| 4497 |
found_key.objectid + found_key.offset; |
| 4498 |
} else { |
| 4499 |
ASSERT(num_space_runs < extent_count); |
| 4500 |
|
| 4501 |
space_runs[num_space_runs].start = found_key.objectid; |
| 4502 |
space_runs[num_space_runs].end = |
| 4503 |
found_key.objectid + found_key.offset; |
| 4504 |
|
| 4505 |
num_space_runs++; |
| 4506 |
} |
| 4507 |
} else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) { |
| 4508 |
void *bitmap; |
| 4509 |
unsigned long offset; |
| 4510 |
u32 data_size; |
| 4511 |
|
| 4512 |
offset = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4513 |
data_size = btrfs_item_size(leaf, path->slots[0]); |
| 4514 |
|
| 4515 |
if (data_size != 0) { |
| 4516 |
bitmap = kmalloc(data_size, GFP_NOFS); |
| 4517 |
if (!bitmap) { |
| 4518 |
mutex_unlock(&bg->free_space_lock); |
| 4519 |
ret = -ENOMEM; |
| 4520 |
goto out; |
| 4521 |
} |
| 4522 |
|
| 4523 |
read_extent_buffer(leaf, bitmap, offset, data_size); |
| 4524 |
|
| 4525 |
parse_bitmap(fs_info->sectorsize, bitmap, |
| 4526 |
data_size * BITS_PER_BYTE, |
| 4527 |
found_key.objectid, space_runs, |
| 4528 |
&num_space_runs); |
| 4529 |
|
| 4530 |
ASSERT(num_space_runs <= extent_count); |
| 4531 |
|
| 4532 |
kfree(bitmap); |
| 4533 |
} |
| 4534 |
} |
| 4535 |
|
| 4536 |
path->slots[0]++; |
| 4537 |
|
| 4538 |
if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4539 |
ret = btrfs_next_leaf(space_root, path); |
| 4540 |
if (ret != 0) { |
| 4541 |
if (ret == 1) |
| 4542 |
ret = 0; |
| 4543 |
break; |
| 4544 |
} |
| 4545 |
leaf = path->nodes[0]; |
| 4546 |
} |
| 4547 |
} |
| 4548 |
|
| 4549 |
btrfs_release_path(path); |
| 4550 |
|
| 4551 |
mutex_unlock(&bg->free_space_lock); |
| 4552 |
|
| 4553 |
max_entries = extent_count + 2; |
| 4554 |
entries = kmalloc(sizeof(*entries) * max_entries, GFP_NOFS); |
| 4555 |
if (!entries) { |
| 4556 |
ret = -ENOMEM; |
| 4557 |
goto out; |
| 4558 |
} |
| 4559 |
|
| 4560 |
num_entries = 0; |
| 4561 |
|
| 4562 |
if (num_space_runs == 0) { |
| 4563 |
entries[num_entries].objectid = bg->start; |
| 4564 |
entries[num_entries].type = BTRFS_IDENTITY_REMAP_KEY; |
| 4565 |
entries[num_entries].offset = bg->length; |
| 4566 |
num_entries++; |
| 4567 |
} else { |
| 4568 |
if (space_runs[0].start > bg->start) { |
| 4569 |
entries[num_entries].objectid = bg->start; |
| 4570 |
entries[num_entries].type = BTRFS_IDENTITY_REMAP_KEY; |
| 4571 |
entries[num_entries].offset = space_runs[0].start - bg->start; |
| 4572 |
num_entries++; |
| 4573 |
} |
| 4574 |
|
| 4575 |
for (unsigned int i = 1; i < num_space_runs; i++) { |
| 4576 |
entries[num_entries].objectid = space_runs[i - 1].end; |
| 4577 |
entries[num_entries].type = BTRFS_IDENTITY_REMAP_KEY; |
| 4578 |
entries[num_entries].offset = |
| 4579 |
space_runs[i].start - space_runs[i - 1].end; |
| 4580 |
num_entries++; |
| 4581 |
} |
| 4582 |
|
| 4583 |
if (space_runs[num_space_runs - 1].end < bg->start + bg->length) { |
| 4584 |
entries[num_entries].objectid = |
| 4585 |
space_runs[num_space_runs - 1].end; |
| 4586 |
entries[num_entries].type = BTRFS_IDENTITY_REMAP_KEY; |
| 4587 |
entries[num_entries].offset = |
| 4588 |
bg->start + bg->length - space_runs[num_space_runs - 1].end; |
| 4589 |
num_entries++; |
| 4590 |
} |
| 4591 |
|
| 4592 |
if (num_entries == 0) |
| 4593 |
goto out; |
| 4594 |
} |
| 4595 |
|
| 4596 |
bg->identity_remap_count = num_entries; |
| 4597 |
|
| 4598 |
ret = add_remap_tree_entries(trans, path, entries, num_entries); |
| 4599 |
|
| 4600 |
out: |
| 4601 |
kfree(entries); |
| 4602 |
kfree(space_runs); |
| 4603 |
|
| 4604 |
return ret; |
| 4605 |
} |
| 4606 |
|
| 4607 |
static int find_next_identity_remap(struct btrfs_trans_handle *trans, |
| 4608 |
struct btrfs_path *path, u64 bg_end, |
| 4609 |
u64 last_start, u64 *start, u64 *length) |
| 4610 |
{ |
| 4611 |
int ret; |
| 4612 |
struct btrfs_key key, found_key; |
| 4613 |
struct btrfs_root *remap_root = trans->fs_info->remap_root; |
| 4614 |
struct extent_buffer *leaf; |
| 4615 |
|
| 4616 |
key.objectid = last_start; |
| 4617 |
key.type = BTRFS_IDENTITY_REMAP_KEY; |
| 4618 |
key.offset = 0; |
| 4619 |
|
| 4620 |
ret = btrfs_search_slot(trans, remap_root, &key, path, 0, 0); |
| 4621 |
if (ret < 0) |
| 4622 |
goto out; |
| 4623 |
|
| 4624 |
leaf = path->nodes[0]; |
| 4625 |
while (true) { |
| 4626 |
if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 4627 |
ret = btrfs_next_leaf(remap_root, path); |
| 4628 |
|
| 4629 |
if (ret != 0) { |
| 4630 |
if (ret == 1) |
| 4631 |
ret = -ENOENT; |
| 4632 |
goto out; |
| 4633 |
} |
| 4634 |
|
| 4635 |
leaf = path->nodes[0]; |
| 4636 |
} |
| 4637 |
|
| 4638 |
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4639 |
|
| 4640 |
if (found_key.objectid >= bg_end) { |
| 4641 |
ret = -ENOENT; |
| 4642 |
goto out; |
| 4643 |
} |
| 4644 |
|
| 4645 |
if (found_key.type == BTRFS_IDENTITY_REMAP_KEY) { |
| 4646 |
*start = found_key.objectid; |
| 4647 |
*length = found_key.offset; |
| 4648 |
ret = 0; |
| 4649 |
goto out; |
| 4650 |
} |
| 4651 |
|
| 4652 |
path->slots[0]++; |
| 4653 |
} |
| 4654 |
|
| 4655 |
out: |
| 4656 |
btrfs_release_path(path); |
| 4657 |
|
| 4658 |
return ret; |
| 4659 |
} |
| 4660 |
|
| 4661 |
static int remove_chunk_stripes(struct btrfs_trans_handle *trans, |
| 4662 |
struct btrfs_chunk_map *chunk_map, |
| 4663 |
struct btrfs_path *path) |
| 4664 |
{ |
| 4665 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 4666 |
struct btrfs_key key; |
| 4667 |
struct extent_buffer *leaf; |
| 4668 |
struct btrfs_chunk *chunk; |
| 4669 |
int ret; |
| 4670 |
|
| 4671 |
key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4672 |
key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4673 |
key.offset = chunk_map->start; |
| 4674 |
|
| 4675 |
btrfs_reserve_chunk_metadata(trans, false); |
| 4676 |
|
| 4677 |
ret = btrfs_search_slot(trans, fs_info->chunk_root, &key, path, 0, 1); |
| 4678 |
if (ret) { |
| 4679 |
if (ret == 1) { |
| 4680 |
btrfs_release_path(path); |
| 4681 |
ret = -ENOENT; |
| 4682 |
} |
| 4683 |
btrfs_trans_release_chunk_metadata(trans); |
| 4684 |
return ret; |
| 4685 |
} |
| 4686 |
|
| 4687 |
leaf = path->nodes[0]; |
| 4688 |
|
| 4689 |
chunk = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_chunk); |
| 4690 |
btrfs_set_chunk_num_stripes(leaf, chunk, 0); |
| 4691 |
btrfs_set_chunk_sub_stripes(leaf, chunk, 0); |
| 4692 |
|
| 4693 |
btrfs_truncate_item(trans, path, offsetof(struct btrfs_chunk, stripe), 1); |
| 4694 |
|
| 4695 |
btrfs_mark_buffer_dirty(trans, leaf); |
| 4696 |
|
| 4697 |
btrfs_release_path(path); |
| 4698 |
btrfs_trans_release_chunk_metadata(trans); |
| 4699 |
|
| 4700 |
return 0; |
| 4701 |
} |
| 4702 |
|
| 4703 |
int btrfs_last_identity_remap_gone(struct btrfs_chunk_map *chunk_map, |
| 4704 |
struct btrfs_block_group *bg) |
| 4705 |
{ |
| 4706 |
struct btrfs_fs_info *fs_info = bg->fs_info; |
| 4707 |
struct btrfs_trans_handle *trans; |
| 4708 |
int ret; |
| 4709 |
unsigned int num_items; |
| 4710 |
BTRFS_PATH_AUTO_FREE(path); |
| 4711 |
|
| 4712 |
path = btrfs_alloc_path(); |
| 4713 |
if (!path) |
| 4714 |
return -ENOMEM; |
| 4715 |
|
| 4716 |
/* |
| 4717 |
* One item for each entry we're removing in the dev extents tree, and |
| 4718 |
* another for each device. DUP chunks are all on one device, |
| 4719 |
* everything else has one device per stripe. |
| 4720 |
*/ |
| 4721 |
if (bg->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4722 |
num_items = chunk_map->num_stripes + 1; |
| 4723 |
else |
| 4724 |
num_items = 2 * chunk_map->num_stripes; |
| 4725 |
|
| 4726 |
trans = btrfs_start_transaction_fallback_global_rsv(fs_info->tree_root, num_items); |
| 4727 |
if (IS_ERR(trans)) |
| 4728 |
return PTR_ERR(trans); |
| 4729 |
|
| 4730 |
ret = btrfs_remove_dev_extents(trans, chunk_map); |
| 4731 |
if (unlikely(ret)) { |
| 4732 |
btrfs_abort_transaction(trans, ret); |
| 4733 |
btrfs_end_transaction(trans); |
| 4734 |
return ret; |
| 4735 |
} |
| 4736 |
|
| 4737 |
mutex_lock(&trans->fs_info->chunk_mutex); |
| 4738 |
for (unsigned int i = 0; i < chunk_map->num_stripes; i++) { |
| 4739 |
ret = btrfs_update_device(trans, chunk_map->stripes[i].dev); |
| 4740 |
if (unlikely(ret)) { |
| 4741 |
mutex_unlock(&trans->fs_info->chunk_mutex); |
| 4742 |
btrfs_abort_transaction(trans, ret); |
| 4743 |
btrfs_end_transaction(trans); |
| 4744 |
return ret; |
| 4745 |
} |
| 4746 |
} |
| 4747 |
mutex_unlock(&trans->fs_info->chunk_mutex); |
| 4748 |
|
| 4749 |
write_lock(&trans->fs_info->mapping_tree_lock); |
| 4750 |
btrfs_chunk_map_device_clear_bits(chunk_map, CHUNK_ALLOCATED); |
| 4751 |
write_unlock(&trans->fs_info->mapping_tree_lock); |
| 4752 |
|
| 4753 |
btrfs_remove_bg_from_sinfo(bg); |
| 4754 |
|
| 4755 |
spin_lock(&bg->lock); |
| 4756 |
clear_bit(BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING, &bg->runtime_flags); |
| 4757 |
spin_unlock(&bg->lock); |
| 4758 |
|
| 4759 |
ret = remove_chunk_stripes(trans, chunk_map, path); |
| 4760 |
if (unlikely(ret)) { |
| 4761 |
btrfs_abort_transaction(trans, ret); |
| 4762 |
btrfs_end_transaction(trans); |
| 4763 |
return ret; |
| 4764 |
} |
| 4765 |
|
| 4766 |
ret = btrfs_commit_transaction(trans); |
| 4767 |
if (ret) |
| 4768 |
return ret; |
| 4769 |
|
| 4770 |
return 0; |
| 4771 |
} |
| 4772 |
|
| 4773 |
static void adjust_identity_remap_count(struct btrfs_trans_handle *trans, |
| 4774 |
struct btrfs_block_group *bg, int delta) |
| 4775 |
{ |
| 4776 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 4777 |
bool bg_already_dirty = true; |
| 4778 |
bool mark_fully_remapped = false; |
| 4779 |
|
| 4780 |
WARN_ON(delta < 0 && -delta > bg->identity_remap_count); |
| 4781 |
|
| 4782 |
spin_lock(&bg->lock); |
| 4783 |
|
| 4784 |
bg->identity_remap_count += delta; |
| 4785 |
|
| 4786 |
if (bg->identity_remap_count == 0 && |
| 4787 |
!test_bit(BLOCK_GROUP_FLAG_FULLY_REMAPPED, &bg->runtime_flags)) { |
| 4788 |
set_bit(BLOCK_GROUP_FLAG_FULLY_REMAPPED, &bg->runtime_flags); |
| 4789 |
mark_fully_remapped = true; |
| 4790 |
} |
| 4791 |
|
| 4792 |
spin_unlock(&bg->lock); |
| 4793 |
|
| 4794 |
spin_lock(&trans->transaction->dirty_bgs_lock); |
| 4795 |
if (list_empty(&bg->dirty_list)) { |
| 4796 |
list_add_tail(&bg->dirty_list, &trans->transaction->dirty_bgs); |
| 4797 |
bg_already_dirty = false; |
| 4798 |
btrfs_get_block_group(bg); |
| 4799 |
} |
| 4800 |
spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 4801 |
|
| 4802 |
/* Modified block groups are accounted for in the delayed_refs_rsv. */ |
| 4803 |
if (!bg_already_dirty) |
| 4804 |
btrfs_inc_delayed_refs_rsv_bg_updates(fs_info); |
| 4805 |
|
| 4806 |
if (mark_fully_remapped) |
| 4807 |
btrfs_mark_bg_fully_remapped(bg, trans); |
| 4808 |
} |
| 4809 |
|
| 4810 |
static int add_remap_entry(struct btrfs_trans_handle *trans, |
| 4811 |
struct btrfs_path *path, |
| 4812 |
struct btrfs_block_group *src_bg, u64 old_addr, |
| 4813 |
u64 new_addr, u64 length) |
| 4814 |
{ |
| 4815 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 4816 |
struct btrfs_key key, new_key; |
| 4817 |
int ret; |
| 4818 |
int identity_count_delta = 0; |
| 4819 |
|
| 4820 |
key.objectid = old_addr; |
| 4821 |
key.type = (u8)-1; |
| 4822 |
key.offset = (u64)-1; |
| 4823 |
|
| 4824 |
ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, -1, 1); |
| 4825 |
if (ret < 0) |
| 4826 |
goto end; |
| 4827 |
|
| 4828 |
if (path->slots[0] == 0) { |
| 4829 |
ret = -ENOENT; |
| 4830 |
goto end; |
| 4831 |
} |
| 4832 |
|
| 4833 |
path->slots[0]--; |
| 4834 |
|
| 4835 |
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4836 |
|
| 4837 |
if (key.type != BTRFS_IDENTITY_REMAP_KEY || |
| 4838 |
key.objectid > old_addr || |
| 4839 |
key.objectid + key.offset <= old_addr) { |
| 4840 |
ret = -ENOENT; |
| 4841 |
goto end; |
| 4842 |
} |
| 4843 |
|
| 4844 |
/* Shorten or delete identity mapping entry. */ |
| 4845 |
if (key.objectid == old_addr) { |
| 4846 |
ret = btrfs_del_item(trans, fs_info->remap_root, path); |
| 4847 |
if (ret) |
| 4848 |
goto end; |
| 4849 |
|
| 4850 |
identity_count_delta--; |
| 4851 |
} else { |
| 4852 |
new_key.objectid = key.objectid; |
| 4853 |
new_key.type = BTRFS_IDENTITY_REMAP_KEY; |
| 4854 |
new_key.offset = old_addr - key.objectid; |
| 4855 |
|
| 4856 |
btrfs_set_item_key_safe(trans, path, &new_key); |
| 4857 |
} |
| 4858 |
|
| 4859 |
btrfs_release_path(path); |
| 4860 |
|
| 4861 |
/* Create new remap entry. */ |
| 4862 |
ret = add_remap_item(trans, path, new_addr, length, old_addr); |
| 4863 |
if (ret) |
| 4864 |
goto end; |
| 4865 |
|
| 4866 |
/* Add entry for remainder of identity mapping, if necessary. */ |
| 4867 |
if (key.objectid + key.offset != old_addr + length) { |
| 4868 |
new_key.objectid = old_addr + length; |
| 4869 |
new_key.type = BTRFS_IDENTITY_REMAP_KEY; |
| 4870 |
new_key.offset = key.objectid + key.offset - old_addr - length; |
| 4871 |
|
| 4872 |
ret = btrfs_insert_empty_item(trans, fs_info->remap_root, |
| 4873 |
path, &new_key, 0); |
| 4874 |
if (ret) |
| 4875 |
goto end; |
| 4876 |
|
| 4877 |
btrfs_release_path(path); |
| 4878 |
|
| 4879 |
identity_count_delta++; |
| 4880 |
} |
| 4881 |
|
| 4882 |
/* Add backref. */ |
| 4883 |
ret = add_remap_backref_item(trans, path, new_addr, length, old_addr); |
| 4884 |
if (ret) |
| 4885 |
goto end; |
| 4886 |
|
| 4887 |
if (identity_count_delta != 0) |
| 4888 |
adjust_identity_remap_count(trans, src_bg, identity_count_delta); |
| 4889 |
|
| 4890 |
end: |
| 4891 |
btrfs_release_path(path); |
| 4892 |
|
| 4893 |
return ret; |
| 4894 |
} |
| 4895 |
|
| 4896 |
static int mark_chunk_remapped(struct btrfs_trans_handle *trans, |
| 4897 |
struct btrfs_path *path, u64 start) |
| 4898 |
{ |
| 4899 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 4900 |
struct btrfs_chunk_map *chunk_map; |
| 4901 |
struct btrfs_key key; |
| 4902 |
u64 type; |
| 4903 |
int ret; |
| 4904 |
struct extent_buffer *leaf; |
| 4905 |
struct btrfs_chunk *chunk; |
| 4906 |
|
| 4907 |
read_lock(&fs_info->mapping_tree_lock); |
| 4908 |
|
| 4909 |
chunk_map = btrfs_find_chunk_map_nolock(fs_info, start, 1); |
| 4910 |
if (!chunk_map) { |
| 4911 |
read_unlock(&fs_info->mapping_tree_lock); |
| 4912 |
return -ENOENT; |
| 4913 |
} |
| 4914 |
|
| 4915 |
chunk_map->type |= BTRFS_BLOCK_GROUP_REMAPPED; |
| 4916 |
type = chunk_map->type; |
| 4917 |
|
| 4918 |
read_unlock(&fs_info->mapping_tree_lock); |
| 4919 |
|
| 4920 |
key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4921 |
key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4922 |
key.offset = start; |
| 4923 |
|
| 4924 |
ret = btrfs_search_slot(trans, fs_info->chunk_root, &key, path, 0, 1); |
| 4925 |
if (ret == 1) { |
| 4926 |
ret = -ENOENT; |
| 4927 |
goto end; |
| 4928 |
} else if (ret < 0) |
| 4929 |
goto end; |
| 4930 |
|
| 4931 |
leaf = path->nodes[0]; |
| 4932 |
|
| 4933 |
chunk = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_chunk); |
| 4934 |
btrfs_set_chunk_type(leaf, chunk, type); |
| 4935 |
btrfs_mark_buffer_dirty(trans, leaf); |
| 4936 |
|
| 4937 |
ret = 0; |
| 4938 |
end: |
| 4939 |
btrfs_free_chunk_map(chunk_map); |
| 4940 |
btrfs_release_path(path); |
| 4941 |
|
| 4942 |
return ret; |
| 4943 |
} |
| 4944 |
|
| 4945 |
static int do_remap_reloc_trans(struct btrfs_fs_info *fs_info, |
| 4946 |
struct btrfs_block_group *src_bg, |
| 4947 |
struct btrfs_path *path, u64 *last_start) |
| 4948 |
{ |
| 4949 |
struct btrfs_trans_handle *trans; |
| 4950 |
struct btrfs_root *extent_root; |
| 4951 |
struct btrfs_key ins; |
| 4952 |
struct btrfs_block_group *dest_bg = NULL; |
| 4953 |
u64 start = 0, remap_length = 0; |
| 4954 |
u64 length, new_addr, min_size; |
| 4955 |
int ret; |
| 4956 |
const bool is_data = (src_bg->flags & BTRFS_BLOCK_GROUP_DATA); |
| 4957 |
bool no_more = false; |
| 4958 |
bool made_reservation = false, bg_needs_free_space; |
| 4959 |
struct btrfs_space_info *sinfo = src_bg->space_info; |
| 4960 |
|
| 4961 |
extent_root = btrfs_extent_root(fs_info, src_bg->start); |
| 4962 |
if (unlikely(!extent_root)) { |
| 4963 |
btrfs_err(fs_info, |
| 4964 |
"missing extent root for block group at offset %llu", |
| 4965 |
src_bg->start); |
| 4966 |
return -EUCLEAN; |
| 4967 |
} |
| 4968 |
|
| 4969 |
trans = btrfs_start_transaction(extent_root, 0); |
| 4970 |
if (IS_ERR(trans)) |
| 4971 |
return PTR_ERR(trans); |
| 4972 |
|
| 4973 |
mutex_lock(&fs_info->remap_mutex); |
| 4974 |
|
| 4975 |
ret = find_next_identity_remap(trans, path, src_bg->start + src_bg->length, |
| 4976 |
*last_start, &start, &remap_length); |
| 4977 |
if (ret == -ENOENT) { |
| 4978 |
no_more = true; |
| 4979 |
goto next; |
| 4980 |
} else if (ret) { |
| 4981 |
mutex_unlock(&fs_info->remap_mutex); |
| 4982 |
btrfs_end_transaction(trans); |
| 4983 |
return ret; |
| 4984 |
} |
| 4985 |
|
| 4986 |
/* Try to reserve enough space for block. */ |
| 4987 |
spin_lock(&sinfo->lock); |
| 4988 |
btrfs_space_info_update_bytes_may_use(sinfo, remap_length); |
| 4989 |
spin_unlock(&sinfo->lock); |
| 4990 |
|
| 4991 |
if (is_data) |
| 4992 |
min_size = fs_info->sectorsize; |
| 4993 |
else |
| 4994 |
min_size = fs_info->nodesize; |
| 4995 |
|
| 4996 |
/* |
| 4997 |
* We're using btrfs_reserve_extent() to allocate a contiguous |
| 4998 |
* logical address range, but this will become a remap item rather than |
| 4999 |
* an extent in the extent tree. |
| 5000 |
* |
| 5001 |
* Short allocations are fine: it means that we chop off the beginning |
| 5002 |
* of the identity remap that we're processing, and will tackle the |
| 5003 |
* rest of it the next time round. |
| 5004 |
*/ |
| 5005 |
ret = btrfs_reserve_extent(fs_info->fs_root, remap_length, remap_length, |
| 5006 |
min_size, 0, 0, &ins, is_data, false); |
| 5007 |
if (ret) { |
| 5008 |
spin_lock(&sinfo->lock); |
| 5009 |
btrfs_space_info_update_bytes_may_use(sinfo, -remap_length); |
| 5010 |
spin_unlock(&sinfo->lock); |
| 5011 |
|
| 5012 |
mutex_unlock(&fs_info->remap_mutex); |
| 5013 |
btrfs_end_transaction(trans); |
| 5014 |
return ret; |
| 5015 |
} |
| 5016 |
|
| 5017 |
if (ins.offset < remap_length) { |
| 5018 |
spin_lock(&sinfo->lock); |
| 5019 |
btrfs_space_info_update_bytes_may_use(sinfo, ins.offset - remap_length); |
| 5020 |
spin_unlock(&sinfo->lock); |
| 5021 |
} |
| 5022 |
|
| 5023 |
made_reservation = true; |
| 5024 |
|
| 5025 |
new_addr = ins.objectid; |
| 5026 |
length = ins.offset; |
| 5027 |
|
| 5028 |
if (!is_data && !IS_ALIGNED(length, fs_info->nodesize)) { |
| 5029 |
u64 new_length = ALIGN_DOWN(length, fs_info->nodesize); |
| 5030 |
|
| 5031 |
btrfs_free_reserved_extent(fs_info, new_addr + new_length, |
| 5032 |
length - new_length, 0); |
| 5033 |
|
| 5034 |
length = new_length; |
| 5035 |
} |
| 5036 |
|
| 5037 |
dest_bg = btrfs_lookup_block_group(fs_info, new_addr); |
| 5038 |
|
| 5039 |
mutex_lock(&dest_bg->free_space_lock); |
| 5040 |
bg_needs_free_space = test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, |
| 5041 |
&dest_bg->runtime_flags); |
| 5042 |
mutex_unlock(&dest_bg->free_space_lock); |
| 5043 |
|
| 5044 |
if (bg_needs_free_space) { |
| 5045 |
ret = btrfs_add_block_group_free_space(trans, dest_bg); |
| 5046 |
if (ret) |
| 5047 |
goto fail; |
| 5048 |
} |
| 5049 |
|
| 5050 |
ret = copy_remapped_data(fs_info, start, new_addr, length); |
| 5051 |
if (ret) |
| 5052 |
goto fail; |
| 5053 |
|
| 5054 |
ret = btrfs_remove_from_free_space_tree(trans, new_addr, length); |
| 5055 |
if (ret) |
| 5056 |
goto fail; |
| 5057 |
|
| 5058 |
ret = add_remap_entry(trans, path, src_bg, start, new_addr, length); |
| 5059 |
if (ret) { |
| 5060 |
btrfs_add_to_free_space_tree(trans, new_addr, length); |
| 5061 |
goto fail; |
| 5062 |
} |
| 5063 |
|
| 5064 |
adjust_block_group_remap_bytes(trans, dest_bg, length); |
| 5065 |
btrfs_free_reserved_bytes(dest_bg, length, 0); |
| 5066 |
|
| 5067 |
spin_lock(&sinfo->lock); |
| 5068 |
sinfo->bytes_readonly += length; |
| 5069 |
spin_unlock(&sinfo->lock); |
| 5070 |
|
| 5071 |
next: |
| 5072 |
if (dest_bg) |
| 5073 |
btrfs_put_block_group(dest_bg); |
| 5074 |
|
| 5075 |
if (made_reservation) |
| 5076 |
btrfs_dec_block_group_reservations(fs_info, new_addr); |
| 5077 |
|
| 5078 |
mutex_unlock(&fs_info->remap_mutex); |
| 5079 |
|
| 5080 |
if (src_bg->identity_remap_count == 0) { |
| 5081 |
bool mark_fully_remapped = false; |
| 5082 |
|
| 5083 |
spin_lock(&src_bg->lock); |
| 5084 |
if (!test_bit(BLOCK_GROUP_FLAG_FULLY_REMAPPED, &src_bg->runtime_flags)) { |
| 5085 |
mark_fully_remapped = true; |
| 5086 |
set_bit(BLOCK_GROUP_FLAG_FULLY_REMAPPED, &src_bg->runtime_flags); |
| 5087 |
} |
| 5088 |
spin_unlock(&src_bg->lock); |
| 5089 |
|
| 5090 |
if (mark_fully_remapped) |
| 5091 |
btrfs_mark_bg_fully_remapped(src_bg, trans); |
| 5092 |
} |
| 5093 |
|
| 5094 |
ret = btrfs_end_transaction(trans); |
| 5095 |
if (ret) |
| 5096 |
return ret; |
| 5097 |
|
| 5098 |
if (no_more) |
| 5099 |
return 1; |
| 5100 |
|
| 5101 |
*last_start = start; |
| 5102 |
|
| 5103 |
return 0; |
| 5104 |
|
| 5105 |
fail: |
| 5106 |
if (dest_bg) |
| 5107 |
btrfs_put_block_group(dest_bg); |
| 5108 |
|
| 5109 |
btrfs_free_reserved_extent(fs_info, new_addr, length, 0); |
| 5110 |
|
| 5111 |
mutex_unlock(&fs_info->remap_mutex); |
| 5112 |
btrfs_end_transaction(trans); |
| 5113 |
|
| 5114 |
return ret; |
| 5115 |
} |
| 5116 |
|
| 5117 |
static int do_remap_reloc(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
| 5118 |
struct btrfs_block_group *bg) |
| 5119 |
{ |
| 5120 |
u64 last_start = bg->start; |
| 5121 |
int ret; |
| 5122 |
|
| 5123 |
while (true) { |
| 5124 |
ret = do_remap_reloc_trans(fs_info, bg, path, &last_start); |
| 5125 |
if (ret) { |
| 5126 |
if (ret == 1) |
| 5127 |
ret = 0; |
| 5128 |
break; |
| 5129 |
} |
| 5130 |
} |
| 5131 |
|
| 5132 |
return ret; |
| 5133 |
} |
| 5134 |
|
| 5135 |
int btrfs_translate_remap(struct btrfs_fs_info *fs_info, u64 *logical, u64 *length) |
| 5136 |
{ |
| 5137 |
int ret; |
| 5138 |
struct btrfs_key key, found_key; |
| 5139 |
struct extent_buffer *leaf; |
| 5140 |
struct btrfs_remap_item *remap; |
| 5141 |
BTRFS_PATH_AUTO_FREE(path); |
| 5142 |
|
| 5143 |
path = btrfs_alloc_path(); |
| 5144 |
if (!path) |
| 5145 |
return -ENOMEM; |
| 5146 |
|
| 5147 |
key.objectid = *logical; |
| 5148 |
key.type = (u8)-1; |
| 5149 |
key.offset = (u64)-1; |
| 5150 |
|
| 5151 |
ret = btrfs_search_slot(NULL, fs_info->remap_root, &key, path, 0, 0); |
| 5152 |
if (ret < 0) |
| 5153 |
return ret; |
| 5154 |
|
| 5155 |
leaf = path->nodes[0]; |
| 5156 |
if (path->slots[0] == 0) |
| 5157 |
return -ENOENT; |
| 5158 |
|
| 5159 |
path->slots[0]--; |
| 5160 |
|
| 5161 |
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5162 |
|
| 5163 |
if (found_key.type != BTRFS_REMAP_KEY && |
| 5164 |
found_key.type != BTRFS_IDENTITY_REMAP_KEY) { |
| 5165 |
return -ENOENT; |
| 5166 |
} |
| 5167 |
|
| 5168 |
if (found_key.objectid > *logical || |
| 5169 |
found_key.objectid + found_key.offset <= *logical) { |
| 5170 |
return -ENOENT; |
| 5171 |
} |
| 5172 |
|
| 5173 |
if (*logical + *length > found_key.objectid + found_key.offset) |
| 5174 |
*length = found_key.objectid + found_key.offset - *logical; |
| 5175 |
|
| 5176 |
if (found_key.type == BTRFS_IDENTITY_REMAP_KEY) |
| 5177 |
return 0; |
| 5178 |
|
| 5179 |
remap = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_remap_item); |
| 5180 |
*logical += btrfs_remap_address(leaf, remap) - found_key.objectid; |
| 5181 |
|
| 5182 |
return 0; |
| 5183 |
} |
| 5184 |
|
| 5185 |
static int start_block_group_remapping(struct btrfs_fs_info *fs_info, |
| 5186 |
struct btrfs_path *path, |
| 5187 |
struct btrfs_block_group *bg) |
| 5188 |
{ |
| 5189 |
struct btrfs_trans_handle *trans; |
| 5190 |
bool bg_already_dirty = true; |
| 5191 |
int ret, ret2; |
| 5192 |
|
| 5193 |
ret = btrfs_cache_block_group(bg, true); |
| 5194 |
if (ret) |
| 5195 |
return ret; |
| 5196 |
|
| 5197 |
trans = btrfs_start_transaction(fs_info->remap_root, 0); |
| 5198 |
if (IS_ERR(trans)) |
| 5199 |
return PTR_ERR(trans); |
| 5200 |
|
| 5201 |
/* We need to run delayed refs, to make sure FST is up to date. */ |
| 5202 |
ret = btrfs_run_delayed_refs(trans, U64_MAX); |
| 5203 |
if (ret) { |
| 5204 |
btrfs_end_transaction(trans); |
| 5205 |
return ret; |
| 5206 |
} |
| 5207 |
|
| 5208 |
mutex_lock(&fs_info->remap_mutex); |
| 5209 |
|
| 5210 |
if (bg->flags & BTRFS_BLOCK_GROUP_REMAPPED) { |
| 5211 |
ret = 0; |
| 5212 |
goto end; |
| 5213 |
} |
| 5214 |
|
| 5215 |
ret = create_remap_tree_entries(trans, path, bg); |
| 5216 |
if (unlikely(ret)) { |
| 5217 |
btrfs_abort_transaction(trans, ret); |
| 5218 |
goto end; |
| 5219 |
} |
| 5220 |
|
| 5221 |
spin_lock(&bg->lock); |
| 5222 |
bg->flags |= BTRFS_BLOCK_GROUP_REMAPPED; |
| 5223 |
spin_unlock(&bg->lock); |
| 5224 |
|
| 5225 |
spin_lock(&trans->transaction->dirty_bgs_lock); |
| 5226 |
if (list_empty(&bg->dirty_list)) { |
| 5227 |
list_add_tail(&bg->dirty_list, &trans->transaction->dirty_bgs); |
| 5228 |
bg_already_dirty = false; |
| 5229 |
btrfs_get_block_group(bg); |
| 5230 |
} |
| 5231 |
spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 5232 |
|
| 5233 |
/* Modified block groups are accounted for in the delayed_refs_rsv. */ |
| 5234 |
if (!bg_already_dirty) |
| 5235 |
btrfs_inc_delayed_refs_rsv_bg_updates(fs_info); |
| 5236 |
|
| 5237 |
ret = mark_chunk_remapped(trans, path, bg->start); |
| 5238 |
if (unlikely(ret)) { |
| 5239 |
btrfs_abort_transaction(trans, ret); |
| 5240 |
goto end; |
| 5241 |
} |
| 5242 |
|
| 5243 |
ret = btrfs_remove_block_group_free_space(trans, bg); |
| 5244 |
if (unlikely(ret)) { |
| 5245 |
btrfs_abort_transaction(trans, ret); |
| 5246 |
goto end; |
| 5247 |
} |
| 5248 |
|
| 5249 |
btrfs_remove_free_space_cache(bg); |
| 5250 |
|
| 5251 |
end: |
| 5252 |
mutex_unlock(&fs_info->remap_mutex); |
| 5253 |
|
| 5254 |
ret2 = btrfs_end_transaction(trans); |
| 5255 |
if (!ret) |
| 5256 |
ret = ret2; |
| 5257 |
|
| 5258 |
return ret; |
| 5259 |
} |
| 5260 |
|
| 5261 |
static int do_nonremap_reloc(struct btrfs_fs_info *fs_info, bool verbose, |
| 5262 |
struct reloc_control *rc) |
| 5263 |
{ |
| 5264 |
int ret; |
| 5265 |
|
| 5266 |
while (1) { |
| 5267 |
enum reloc_stage finishes_stage; |
| 5268 |
|
| 5269 |
mutex_lock(&fs_info->cleaner_mutex); |
| 5270 |
ret = relocate_block_group(rc); |
| 5271 |
mutex_unlock(&fs_info->cleaner_mutex); |
| 5272 |
|
| 5273 |
finishes_stage = rc->stage; |
| 5274 |
/* |
| 5275 |
* We may have gotten ENOSPC after we already dirtied some |
| 5276 |
* extents. If writeout happens while we're relocating a |
| 5277 |
* different block group we could end up hitting the |
| 5278 |
* BUG_ON(rc->stage == UPDATE_DATA_PTRS) in |
| 5279 |
* btrfs_reloc_cow_block. Make sure we write everything out |
| 5280 |
* properly so we don't trip over this problem, and then break |
| 5281 |
* out of the loop if we hit an error. |
| 5282 |
*/ |
| 5283 |
if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) { |
| 5284 |
int wb_ret; |
| 5285 |
|
| 5286 |
wb_ret = btrfs_wait_ordered_range(BTRFS_I(rc->data_inode), |
| 5287 |
0, (u64)-1); |
| 5288 |
if (wb_ret && ret == 0) |
| 5289 |
ret = wb_ret; |
| 5290 |
invalidate_mapping_pages(rc->data_inode->i_mapping, 0, -1); |
| 5291 |
rc->stage = UPDATE_DATA_PTRS; |
| 5292 |
} |
| 5293 |
|
| 5294 |
if (ret < 0) |
| 5295 |
return ret; |
| 5296 |
|
| 5297 |
if (rc->extents_found == 0) |
| 5298 |
break; |
| 5299 |
|
| 5300 |
if (verbose) |
| 5301 |
btrfs_info(fs_info, "found %llu extents, stage: %s", |
| 5302 |
rc->extents_found, stage_to_string(finishes_stage)); |
| 5303 |
} |
| 5304 |
|
| 5305 |
WARN_ON(rc->block_group->pinned > 0); |
| 5306 |
WARN_ON(rc->block_group->reserved > 0); |
| 5307 |
WARN_ON(rc->block_group->used > 0); |
| 5308 |
|
| 5309 |
return 0; |
| 5310 |
} |
| 5311 |
|
| 5312 |
/* |
| 5313 |
* function to relocate all extents in a block group. |
| 5314 |
*/ |
| 5315 |
int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start, |
| 5316 |
bool verbose) |
| 5317 |
{ |
| 5318 |
struct btrfs_block_group *bg; |
| 5319 |
struct btrfs_root *extent_root = btrfs_extent_root(fs_info, group_start); |
| 5320 |
struct reloc_control *rc; |
| 5321 |
struct inode *inode; |
| 5322 |
struct btrfs_path *path = NULL; |
| 5323 |
int ret; |
| 5324 |
bool bg_is_ro = false; |
| 5325 |
|
| 5326 |
if (unlikely(!extent_root)) { |
| 5327 |
btrfs_err(fs_info, |
| 5328 |
"missing extent root for block group at offset %llu", |
| 5329 |
group_start); |
| 5330 |
return -EUCLEAN; |
| 5331 |
} |
| 5332 |
|
| 5333 |
/* |
| 5334 |
* This only gets set if we had a half-deleted snapshot on mount. We |
| 5335 |
* cannot allow relocation to start while we're still trying to clean up |
| 5336 |
* these pending deletions. |
| 5337 |
*/ |
| 5338 |
ret = wait_on_bit(&fs_info->flags, BTRFS_FS_UNFINISHED_DROPS, TASK_INTERRUPTIBLE); |
| 5339 |
if (ret) |
| 5340 |
return ret; |
| 5341 |
|
| 5342 |
/* We may have been woken up by close_ctree, so bail if we're closing. */ |
| 5343 |
if (btrfs_fs_closing(fs_info)) |
| 5344 |
return -EINTR; |
| 5345 |
|
| 5346 |
bg = btrfs_lookup_block_group(fs_info, group_start); |
| 5347 |
if (!bg) |
| 5348 |
return -ENOENT; |
| 5349 |
|
| 5350 |
/* |
| 5351 |
* Relocation of a data block group creates ordered extents. Without |
| 5352 |
* sb_start_write(), we can freeze the filesystem while unfinished |
| 5353 |
* ordered extents are left. Such ordered extents can cause a deadlock |
| 5354 |
* e.g. when syncfs() is waiting for their completion but they can't |
| 5355 |
* finish because they block when joining a transaction, due to the |
| 5356 |
* fact that the freeze locks are being held in write mode. |
| 5357 |
*/ |
| 5358 |
if (bg->flags & BTRFS_BLOCK_GROUP_DATA) |
| 5359 |
ASSERT(sb_write_started(fs_info->sb)); |
| 5360 |
|
| 5361 |
if (btrfs_pinned_by_swapfile(fs_info, bg)) { |
| 5362 |
btrfs_put_block_group(bg); |
| 5363 |
return -ETXTBSY; |
| 5364 |
} |
| 5365 |
|
| 5366 |
rc = alloc_reloc_control(fs_info); |
| 5367 |
if (!rc) { |
| 5368 |
btrfs_put_block_group(bg); |
| 5369 |
return -ENOMEM; |
| 5370 |
} |
| 5371 |
|
| 5372 |
ret = reloc_chunk_start(fs_info); |
| 5373 |
if (ret < 0) |
| 5374 |
goto out_put_bg; |
| 5375 |
|
| 5376 |
rc->extent_root = extent_root; |
| 5377 |
rc->block_group = bg; |
| 5378 |
|
| 5379 |
ret = btrfs_inc_block_group_ro(rc->block_group, true); |
| 5380 |
if (ret) |
| 5381 |
goto out; |
| 5382 |
bg_is_ro = true; |
| 5383 |
|
| 5384 |
path = btrfs_alloc_path(); |
| 5385 |
if (!path) { |
| 5386 |
ret = -ENOMEM; |
| 5387 |
goto out; |
| 5388 |
} |
| 5389 |
|
| 5390 |
inode = lookup_free_space_inode(rc->block_group, path); |
| 5391 |
btrfs_release_path(path); |
| 5392 |
|
| 5393 |
if (!IS_ERR(inode)) |
| 5394 |
ret = delete_block_group_cache(rc->block_group, inode, 0); |
| 5395 |
else |
| 5396 |
ret = PTR_ERR(inode); |
| 5397 |
|
| 5398 |
if (ret && ret != -ENOENT) |
| 5399 |
goto out; |
| 5400 |
|
| 5401 |
if (!btrfs_fs_incompat(fs_info, REMAP_TREE)) { |
| 5402 |
rc->data_inode = create_reloc_inode(rc->block_group); |
| 5403 |
if (IS_ERR(rc->data_inode)) { |
| 5404 |
ret = PTR_ERR(rc->data_inode); |
| 5405 |
rc->data_inode = NULL; |
| 5406 |
goto out; |
| 5407 |
} |
| 5408 |
} |
| 5409 |
|
| 5410 |
if (verbose) |
| 5411 |
describe_relocation(rc->block_group); |
| 5412 |
|
| 5413 |
btrfs_wait_block_group_reservations(rc->block_group); |
| 5414 |
btrfs_wait_nocow_writers(rc->block_group); |
| 5415 |
btrfs_wait_ordered_roots(fs_info, U64_MAX, rc->block_group); |
| 5416 |
|
| 5417 |
ret = btrfs_zone_finish(rc->block_group); |
| 5418 |
WARN_ON(ret && ret != -EAGAIN); |
| 5419 |
|
| 5420 |
if (should_relocate_using_remap_tree(bg)) { |
| 5421 |
if (bg->remap_bytes != 0) { |
| 5422 |
ret = move_existing_remaps(fs_info, bg, path); |
| 5423 |
if (ret) |
| 5424 |
goto out; |
| 5425 |
} |
| 5426 |
ret = start_block_group_remapping(fs_info, path, bg); |
| 5427 |
if (ret) |
| 5428 |
goto out; |
| 5429 |
|
| 5430 |
ret = do_remap_reloc(fs_info, path, rc->block_group); |
| 5431 |
if (ret) |
| 5432 |
goto out; |
| 5433 |
|
| 5434 |
btrfs_delete_unused_bgs(fs_info); |
| 5435 |
} else { |
| 5436 |
ret = do_nonremap_reloc(fs_info, verbose, rc); |
| 5437 |
} |
| 5438 |
|
| 5439 |
out: |
| 5440 |
if (ret && bg_is_ro) |
| 5441 |
btrfs_dec_block_group_ro(rc->block_group); |
| 5442 |
if (!btrfs_fs_incompat(fs_info, REMAP_TREE)) |
| 5443 |
iput(rc->data_inode); |
| 5444 |
btrfs_free_path(path); |
| 5445 |
reloc_chunk_end(fs_info); |
| 5446 |
out_put_bg: |
| 5447 |
btrfs_put_block_group(bg); |
| 5448 |
free_reloc_control(rc); |
| 5449 |
return ret; |
| 5450 |
} |
| 5451 |
|
| 5452 |
static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) |
| 5453 |
{ |
| 5454 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 5455 |
struct btrfs_trans_handle *trans; |
| 5456 |
int ret, err; |
| 5457 |
|
| 5458 |
trans = btrfs_start_transaction(fs_info->tree_root, 0); |
| 5459 |
if (IS_ERR(trans)) |
| 5460 |
return PTR_ERR(trans); |
| 5461 |
|
| 5462 |
memset(&root->root_item.drop_progress, 0, |
| 5463 |
sizeof(root->root_item.drop_progress)); |
| 5464 |
btrfs_set_root_drop_level(&root->root_item, 0); |
| 5465 |
btrfs_set_root_refs(&root->root_item, 0); |
| 5466 |
ret = btrfs_update_root(trans, fs_info->tree_root, |
| 5467 |
&root->root_key, &root->root_item); |
| 5468 |
|
| 5469 |
err = btrfs_end_transaction(trans); |
| 5470 |
if (err) |
| 5471 |
return err; |
| 5472 |
return ret; |
| 5473 |
} |
| 5474 |
|
| 5475 |
/* |
| 5476 |
* recover relocation interrupted by system crash. |
| 5477 |
* |
| 5478 |
* this function resumes merging reloc trees with corresponding fs trees. |
| 5479 |
* this is important for keeping the sharing of tree blocks |
| 5480 |
*/ |
| 5481 |
int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) |
| 5482 |
{ |
| 5483 |
LIST_HEAD(reloc_roots); |
| 5484 |
struct btrfs_key key; |
| 5485 |
struct btrfs_root *fs_root; |
| 5486 |
struct btrfs_root *reloc_root; |
| 5487 |
struct btrfs_path *path; |
| 5488 |
struct extent_buffer *leaf; |
| 5489 |
struct reloc_control *rc = NULL; |
| 5490 |
struct btrfs_trans_handle *trans; |
| 5491 |
int ret2; |
| 5492 |
int ret = 0; |
| 5493 |
|
| 5494 |
path = btrfs_alloc_path(); |
| 5495 |
if (!path) |
| 5496 |
return -ENOMEM; |
| 5497 |
path->reada = READA_BACK; |
| 5498 |
|
| 5499 |
key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 5500 |
key.type = BTRFS_ROOT_ITEM_KEY; |
| 5501 |
key.offset = (u64)-1; |
| 5502 |
|
| 5503 |
while (1) { |
| 5504 |
ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, |
| 5505 |
path, 0, 0); |
| 5506 |
if (ret < 0) |
| 5507 |
goto out; |
| 5508 |
if (ret > 0) { |
| 5509 |
if (path->slots[0] == 0) |
| 5510 |
break; |
| 5511 |
path->slots[0]--; |
| 5512 |
} |
| 5513 |
ret = 0; |
| 5514 |
leaf = path->nodes[0]; |
| 5515 |
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 5516 |
btrfs_release_path(path); |
| 5517 |
|
| 5518 |
if (key.objectid != BTRFS_TREE_RELOC_OBJECTID || |
| 5519 |
key.type != BTRFS_ROOT_ITEM_KEY) |
| 5520 |
break; |
| 5521 |
|
| 5522 |
reloc_root = btrfs_read_tree_root(fs_info->tree_root, &key); |
| 5523 |
if (IS_ERR(reloc_root)) { |
| 5524 |
ret = PTR_ERR(reloc_root); |
| 5525 |
goto out; |
| 5526 |
} |
| 5527 |
|
| 5528 |
set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state); |
| 5529 |
list_add(&reloc_root->root_list, &reloc_roots); |
| 5530 |
|
| 5531 |
if (btrfs_root_refs(&reloc_root->root_item) > 0) { |
| 5532 |
fs_root = btrfs_get_fs_root(fs_info, |
| 5533 |
reloc_root->root_key.offset, false); |
| 5534 |
if (IS_ERR(fs_root)) { |
| 5535 |
ret = PTR_ERR(fs_root); |
| 5536 |
if (ret != -ENOENT) |
| 5537 |
goto out; |
| 5538 |
ret = mark_garbage_root(reloc_root); |
| 5539 |
if (ret < 0) |
| 5540 |
goto out; |
| 5541 |
ret = 0; |
| 5542 |
} else { |
| 5543 |
btrfs_put_root(fs_root); |
| 5544 |
} |
| 5545 |
} |
| 5546 |
|
| 5547 |
if (key.offset == 0) |
| 5548 |
break; |
| 5549 |
|
| 5550 |
key.offset--; |
| 5551 |
} |
| 5552 |
btrfs_release_path(path); |
| 5553 |
|
| 5554 |
if (list_empty(&reloc_roots)) |
| 5555 |
goto out; |
| 5556 |
|
| 5557 |
rc = alloc_reloc_control(fs_info); |
| 5558 |
if (!rc) { |
| 5559 |
ret = -ENOMEM; |
| 5560 |
goto out; |
| 5561 |
} |
| 5562 |
|
| 5563 |
rc->extent_root = btrfs_extent_root(fs_info, 0); |
| 5564 |
if (unlikely(!rc->extent_root)) { |
| 5565 |
btrfs_err(fs_info, "missing extent root for extent at bytenr 0"); |
| 5566 |
ret = -EUCLEAN; |
| 5567 |
goto out; |
| 5568 |
} |
| 5569 |
|
| 5570 |
ret = reloc_chunk_start(fs_info); |
| 5571 |
if (ret < 0) |
| 5572 |
goto out_end; |
| 5573 |
|
| 5574 |
set_reloc_control(rc); |
| 5575 |
|
| 5576 |
trans = btrfs_join_transaction(rc->extent_root); |
| 5577 |
if (IS_ERR(trans)) { |
| 5578 |
ret = PTR_ERR(trans); |
| 5579 |
goto out_unset; |
| 5580 |
} |
| 5581 |
|
| 5582 |
rc->merge_reloc_tree = true; |
| 5583 |
|
| 5584 |
while (!list_empty(&reloc_roots)) { |
| 5585 |
reloc_root = list_first_entry(&reloc_roots, struct btrfs_root, root_list); |
| 5586 |
list_del(&reloc_root->root_list); |
| 5587 |
|
| 5588 |
if (btrfs_root_refs(&reloc_root->root_item) == 0) { |
| 5589 |
list_add_tail(&reloc_root->root_list, |
| 5590 |
&rc->reloc_roots); |
| 5591 |
continue; |
| 5592 |
} |
| 5593 |
|
| 5594 |
fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, |
| 5595 |
false); |
| 5596 |
if (IS_ERR(fs_root)) { |
| 5597 |
ret = PTR_ERR(fs_root); |
| 5598 |
list_add_tail(&reloc_root->root_list, &reloc_roots); |
| 5599 |
btrfs_end_transaction(trans); |
| 5600 |
goto out_unset; |
| 5601 |
} |
| 5602 |
|
| 5603 |
ret = __add_reloc_root(reloc_root); |
| 5604 |
ASSERT(ret != -EEXIST); |
| 5605 |
if (ret) { |
| 5606 |
list_add_tail(&reloc_root->root_list, &reloc_roots); |
| 5607 |
btrfs_put_root(fs_root); |
| 5608 |
btrfs_end_transaction(trans); |
| 5609 |
goto out_unset; |
| 5610 |
} |
| 5611 |
fs_root->reloc_root = btrfs_grab_root(reloc_root); |
| 5612 |
btrfs_put_root(fs_root); |
| 5613 |
} |
| 5614 |
|
| 5615 |
ret = btrfs_commit_transaction(trans); |
| 5616 |
if (ret) |
| 5617 |
goto out_unset; |
| 5618 |
|
| 5619 |
merge_reloc_roots(rc); |
| 5620 |
|
| 5621 |
unset_reloc_control(rc); |
| 5622 |
|
| 5623 |
trans = btrfs_join_transaction(rc->extent_root); |
| 5624 |
if (IS_ERR(trans)) { |
| 5625 |
ret = PTR_ERR(trans); |
| 5626 |
goto out_clean; |
| 5627 |
} |
| 5628 |
ret = btrfs_commit_transaction(trans); |
| 5629 |
out_clean: |
| 5630 |
ret2 = clean_dirty_subvols(rc); |
| 5631 |
if (ret2 < 0 && !ret) |
| 5632 |
ret = ret2; |
| 5633 |
out_unset: |
| 5634 |
unset_reloc_control(rc); |
| 5635 |
reloc_chunk_end(fs_info); |
| 5636 |
out_end: |
| 5637 |
free_reloc_control(rc); |
| 5638 |
out: |
| 5639 |
free_reloc_roots(&reloc_roots); |
| 5640 |
|
| 5641 |
btrfs_free_path(path); |
| 5642 |
|
| 5643 |
if (ret == 0 && !btrfs_fs_incompat(fs_info, REMAP_TREE)) { |
| 5644 |
/* cleanup orphan inode in data relocation tree */ |
| 5645 |
fs_root = btrfs_grab_root(fs_info->data_reloc_root); |
| 5646 |
ASSERT(fs_root); |
| 5647 |
ret = btrfs_orphan_cleanup(fs_root); |
| 5648 |
btrfs_put_root(fs_root); |
| 5649 |
} |
| 5650 |
return ret; |
| 5651 |
} |
| 5652 |
|
| 5653 |
/* |
| 5654 |
* helper to add ordered checksum for data relocation. |
| 5655 |
* |
| 5656 |
* cloning checksum properly handles the nodatasum extents. |
| 5657 |
* it also saves CPU time to re-calculate the checksum. |
| 5658 |
*/ |
| 5659 |
int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered) |
| 5660 |
{ |
| 5661 |
struct btrfs_inode *inode = ordered->inode; |
| 5662 |
struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 5663 |
u64 disk_bytenr = ordered->file_offset + inode->reloc_block_group_start; |
| 5664 |
struct btrfs_root *csum_root = btrfs_csum_root(fs_info, disk_bytenr); |
| 5665 |
LIST_HEAD(list); |
| 5666 |
int ret; |
| 5667 |
|
| 5668 |
if (unlikely(!csum_root)) { |
| 5669 |
btrfs_mark_ordered_extent_error(ordered); |
| 5670 |
btrfs_err(fs_info, |
| 5671 |
"missing csum root for extent at bytenr %llu", |
| 5672 |
disk_bytenr); |
| 5673 |
return -EUCLEAN; |
| 5674 |
} |
| 5675 |
|
| 5676 |
ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, |
| 5677 |
disk_bytenr + ordered->num_bytes - 1, |
| 5678 |
&list, false); |
| 5679 |
if (ret < 0) { |
| 5680 |
btrfs_mark_ordered_extent_error(ordered); |
| 5681 |
return ret; |
| 5682 |
} |
| 5683 |
|
| 5684 |
while (!list_empty(&list)) { |
| 5685 |
struct btrfs_ordered_sum *sums = |
| 5686 |
list_first_entry(&list, struct btrfs_ordered_sum, list); |
| 5687 |
|
| 5688 |
list_del_init(&sums->list); |
| 5689 |
|
| 5690 |
/* |
| 5691 |
* We need to offset the new_bytenr based on where the csum is. |
| 5692 |
* We need to do this because we will read in entire prealloc |
| 5693 |
* extents but we may have written to say the middle of the |
| 5694 |
* prealloc extent, so we need to make sure the csum goes with |
| 5695 |
* the right disk offset. |
| 5696 |
* |
| 5697 |
* We can do this because the data reloc inode refers strictly |
| 5698 |
* to the on disk bytes, so we don't have to worry about |
| 5699 |
* disk_len vs real len like with real inodes since it's all |
| 5700 |
* disk length. |
| 5701 |
*/ |
| 5702 |
sums->logical = ordered->disk_bytenr + sums->logical - disk_bytenr; |
| 5703 |
btrfs_add_ordered_sum(ordered, sums); |
| 5704 |
} |
| 5705 |
|
| 5706 |
return 0; |
| 5707 |
} |
| 5708 |
|
| 5709 |
int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, |
| 5710 |
struct btrfs_root *root, |
| 5711 |
const struct extent_buffer *buf, |
| 5712 |
struct extent_buffer *cow) |
| 5713 |
{ |
| 5714 |
struct btrfs_fs_info *fs_info = root->fs_info; |
| 5715 |
struct reloc_control *rc; |
| 5716 |
struct btrfs_backref_node *node; |
| 5717 |
int first_cow = 0; |
| 5718 |
int level; |
| 5719 |
int ret = 0; |
| 5720 |
|
| 5721 |
rc = fs_info->reloc_ctl; |
| 5722 |
if (!rc) |
| 5723 |
return 0; |
| 5724 |
|
| 5725 |
BUG_ON(rc->stage == UPDATE_DATA_PTRS && btrfs_is_data_reloc_root(root)); |
| 5726 |
|
| 5727 |
level = btrfs_header_level(buf); |
| 5728 |
if (btrfs_header_generation(buf) <= |
| 5729 |
btrfs_root_last_snapshot(&root->root_item)) |
| 5730 |
first_cow = 1; |
| 5731 |
|
| 5732 |
if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID && rc->create_reloc_tree) { |
| 5733 |
WARN_ON(!first_cow && level == 0); |
| 5734 |
|
| 5735 |
node = rc->backref_cache.path[level]; |
| 5736 |
|
| 5737 |
/* |
| 5738 |
* If node->bytenr != buf->start and node->new_bytenr != |
| 5739 |
* buf->start then we've got the wrong backref node for what we |
| 5740 |
* expected to see here and the cache is incorrect. |
| 5741 |
*/ |
| 5742 |
if (unlikely(node->bytenr != buf->start && node->new_bytenr != buf->start)) { |
| 5743 |
btrfs_err(fs_info, |
| 5744 |
"bytenr %llu was found but our backref cache was expecting %llu or %llu", |
| 5745 |
buf->start, node->bytenr, node->new_bytenr); |
| 5746 |
return -EUCLEAN; |
| 5747 |
} |
| 5748 |
|
| 5749 |
btrfs_backref_drop_node_buffer(node); |
| 5750 |
refcount_inc(&cow->refs); |
| 5751 |
node->eb = cow; |
| 5752 |
node->new_bytenr = cow->start; |
| 5753 |
|
| 5754 |
if (!node->pending) { |
| 5755 |
list_move_tail(&node->list, |
| 5756 |
&rc->backref_cache.pending[level]); |
| 5757 |
node->pending = 1; |
| 5758 |
} |
| 5759 |
|
| 5760 |
if (first_cow) |
| 5761 |
mark_block_processed(rc, node); |
| 5762 |
|
| 5763 |
if (first_cow && level > 0) |
| 5764 |
rc->nodes_relocated += buf->len; |
| 5765 |
} |
| 5766 |
|
| 5767 |
if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS) |
| 5768 |
ret = replace_file_extents(trans, rc, root, cow); |
| 5769 |
return ret; |
| 5770 |
} |
| 5771 |
|
| 5772 |
/* |
| 5773 |
* called before creating snapshot. it calculates metadata reservation |
| 5774 |
* required for relocating tree blocks in the snapshot |
| 5775 |
*/ |
| 5776 |
void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending, |
| 5777 |
u64 *bytes_to_reserve) |
| 5778 |
{ |
| 5779 |
struct btrfs_root *root = pending->root; |
| 5780 |
struct reloc_control *rc = root->fs_info->reloc_ctl; |
| 5781 |
|
| 5782 |
if (!rc || !have_reloc_root(root)) |
| 5783 |
return; |
| 5784 |
|
| 5785 |
if (!rc->merge_reloc_tree) |
| 5786 |
return; |
| 5787 |
|
| 5788 |
root = root->reloc_root; |
| 5789 |
BUG_ON(btrfs_root_refs(&root->root_item) == 0); |
| 5790 |
/* |
| 5791 |
* relocation is in the stage of merging trees. the space |
| 5792 |
* used by merging a reloc tree is twice the size of |
| 5793 |
* relocated tree nodes in the worst case. half for cowing |
| 5794 |
* the reloc tree, half for cowing the fs tree. the space |
| 5795 |
* used by cowing the reloc tree will be freed after the |
| 5796 |
* tree is dropped. if we create snapshot, cowing the fs |
| 5797 |
* tree may use more space than it frees. so we need |
| 5798 |
* reserve extra space. |
| 5799 |
*/ |
| 5800 |
*bytes_to_reserve += rc->nodes_relocated; |
| 5801 |
} |
| 5802 |
|
| 5803 |
/* |
| 5804 |
* called after snapshot is created. migrate block reservation |
| 5805 |
* and create reloc root for the newly created snapshot |
| 5806 |
* |
| 5807 |
* This is similar to btrfs_init_reloc_root(), we come out of here with two |
| 5808 |
* references held on the reloc_root, one for root->reloc_root and one for |
| 5809 |
* rc->reloc_roots. |
| 5810 |
*/ |
| 5811 |
int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, |
| 5812 |
struct btrfs_pending_snapshot *pending) |
| 5813 |
{ |
| 5814 |
struct btrfs_root *root = pending->root; |
| 5815 |
struct btrfs_root *reloc_root; |
| 5816 |
struct btrfs_root *new_root; |
| 5817 |
struct reloc_control *rc = root->fs_info->reloc_ctl; |
| 5818 |
int ret; |
| 5819 |
|
| 5820 |
if (!rc || !have_reloc_root(root)) |
| 5821 |
return 0; |
| 5822 |
|
| 5823 |
rc = root->fs_info->reloc_ctl; |
| 5824 |
rc->merging_rsv_size += rc->nodes_relocated; |
| 5825 |
|
| 5826 |
if (rc->merge_reloc_tree) { |
| 5827 |
ret = btrfs_block_rsv_migrate(&pending->block_rsv, |
| 5828 |
rc->block_rsv, |
| 5829 |
rc->nodes_relocated, true); |
| 5830 |
if (ret) |
| 5831 |
return ret; |
| 5832 |
} |
| 5833 |
|
| 5834 |
new_root = pending->snap; |
| 5835 |
reloc_root = create_reloc_root(trans, root->reloc_root, btrfs_root_id(new_root)); |
| 5836 |
if (IS_ERR(reloc_root)) |
| 5837 |
return PTR_ERR(reloc_root); |
| 5838 |
|
| 5839 |
ret = __add_reloc_root(reloc_root); |
| 5840 |
ASSERT(ret != -EEXIST); |
| 5841 |
if (ret) { |
| 5842 |
/* Pairs with create_reloc_root */ |
| 5843 |
btrfs_put_root(reloc_root); |
| 5844 |
return ret; |
| 5845 |
} |
| 5846 |
new_root->reloc_root = btrfs_grab_root(reloc_root); |
| 5847 |
return 0; |
| 5848 |
} |
| 5849 |
|
| 5850 |
/* |
| 5851 |
* Get the current bytenr for the block group which is being relocated. |
| 5852 |
* |
| 5853 |
* Return U64_MAX if no running relocation. |
| 5854 |
*/ |
| 5855 |
u64 btrfs_get_reloc_bg_bytenr(const struct btrfs_fs_info *fs_info) |
| 5856 |
{ |
| 5857 |
u64 logical = U64_MAX; |
| 5858 |
|
| 5859 |
lockdep_assert_held(&fs_info->reloc_mutex); |
| 5860 |
|
| 5861 |
if (fs_info->reloc_ctl && fs_info->reloc_ctl->block_group) |
| 5862 |
logical = fs_info->reloc_ctl->block_group->start; |
| 5863 |
return logical; |
| 5864 |
} |
| 5865 |
|
| 5866 |
static int insert_remap_item(struct btrfs_trans_handle *trans, struct btrfs_path *path, |
| 5867 |
u64 old_addr, u64 length, u64 new_addr) |
| 5868 |
{ |
| 5869 |
int ret; |
| 5870 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 5871 |
struct btrfs_key key; |
| 5872 |
struct btrfs_remap_item remap = { 0 }; |
| 5873 |
|
| 5874 |
if (old_addr == new_addr) { |
| 5875 |
/* Add new identity remap item. */ |
| 5876 |
key.objectid = old_addr; |
| 5877 |
key.type = BTRFS_IDENTITY_REMAP_KEY; |
| 5878 |
key.offset = length; |
| 5879 |
|
| 5880 |
ret = btrfs_insert_empty_item(trans, fs_info->remap_root, path, |
| 5881 |
&key, 0); |
| 5882 |
if (ret) |
| 5883 |
return ret; |
| 5884 |
} else { |
| 5885 |
/* Add new remap item. */ |
| 5886 |
key.objectid = old_addr; |
| 5887 |
key.type = BTRFS_REMAP_KEY; |
| 5888 |
key.offset = length; |
| 5889 |
|
| 5890 |
ret = btrfs_insert_empty_item(trans, fs_info->remap_root, |
| 5891 |
path, &key, sizeof(struct btrfs_remap_item)); |
| 5892 |
if (ret) |
| 5893 |
return ret; |
| 5894 |
|
| 5895 |
btrfs_set_stack_remap_address(&remap, new_addr); |
| 5896 |
|
| 5897 |
write_extent_buffer(path->nodes[0], &remap, |
| 5898 |
btrfs_item_ptr_offset(path->nodes[0], path->slots[0]), |
| 5899 |
sizeof(struct btrfs_remap_item)); |
| 5900 |
|
| 5901 |
btrfs_release_path(path); |
| 5902 |
|
| 5903 |
/* Add new backref item. */ |
| 5904 |
key.objectid = new_addr; |
| 5905 |
key.type = BTRFS_REMAP_BACKREF_KEY; |
| 5906 |
key.offset = length; |
| 5907 |
|
| 5908 |
ret = btrfs_insert_empty_item(trans, fs_info->remap_root, |
| 5909 |
path, &key, |
| 5910 |
sizeof(struct btrfs_remap_item)); |
| 5911 |
if (ret) |
| 5912 |
return ret; |
| 5913 |
|
| 5914 |
btrfs_set_stack_remap_address(&remap, old_addr); |
| 5915 |
|
| 5916 |
write_extent_buffer(path->nodes[0], &remap, |
| 5917 |
btrfs_item_ptr_offset(path->nodes[0], path->slots[0]), |
| 5918 |
sizeof(struct btrfs_remap_item)); |
| 5919 |
} |
| 5920 |
|
| 5921 |
btrfs_release_path(path); |
| 5922 |
|
| 5923 |
return 0; |
| 5924 |
} |
| 5925 |
|
| 5926 |
/* |
| 5927 |
* Punch a hole in the remap item or identity remap item pointed to by path, |
| 5928 |
* for the range [hole_start, hole_start + hole_length). |
| 5929 |
*/ |
| 5930 |
static int remove_range_from_remap_tree(struct btrfs_trans_handle *trans, |
| 5931 |
struct btrfs_path *path, |
| 5932 |
struct btrfs_block_group *bg, |
| 5933 |
u64 hole_start, u64 hole_length) |
| 5934 |
{ |
| 5935 |
int ret; |
| 5936 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 5937 |
struct extent_buffer *leaf = path->nodes[0]; |
| 5938 |
struct btrfs_key key; |
| 5939 |
u64 hole_end, new_addr, remap_start, remap_length, remap_end; |
| 5940 |
u64 overlap_length; |
| 5941 |
bool is_identity_remap; |
| 5942 |
int identity_count_delta = 0; |
| 5943 |
|
| 5944 |
hole_end = hole_start + hole_length; |
| 5945 |
|
| 5946 |
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 5947 |
|
| 5948 |
is_identity_remap = (key.type == BTRFS_IDENTITY_REMAP_KEY); |
| 5949 |
|
| 5950 |
remap_start = key.objectid; |
| 5951 |
remap_length = key.offset; |
| 5952 |
remap_end = remap_start + remap_length; |
| 5953 |
|
| 5954 |
if (is_identity_remap) { |
| 5955 |
new_addr = remap_start; |
| 5956 |
} else { |
| 5957 |
struct btrfs_remap_item *remap_ptr; |
| 5958 |
|
| 5959 |
remap_ptr = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_remap_item); |
| 5960 |
new_addr = btrfs_remap_address(leaf, remap_ptr); |
| 5961 |
} |
| 5962 |
|
| 5963 |
/* Delete old item. */ |
| 5964 |
ret = btrfs_del_item(trans, fs_info->remap_root, path); |
| 5965 |
btrfs_release_path(path); |
| 5966 |
if (ret) |
| 5967 |
return ret; |
| 5968 |
|
| 5969 |
if (is_identity_remap) { |
| 5970 |
identity_count_delta = -1; |
| 5971 |
} else { |
| 5972 |
/* Remove backref. */ |
| 5973 |
key.objectid = new_addr; |
| 5974 |
key.type = BTRFS_REMAP_BACKREF_KEY; |
| 5975 |
key.offset = remap_length; |
| 5976 |
|
| 5977 |
ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, -1, 1); |
| 5978 |
if (ret) { |
| 5979 |
if (ret == 1) { |
| 5980 |
btrfs_release_path(path); |
| 5981 |
ret = -ENOENT; |
| 5982 |
} |
| 5983 |
return ret; |
| 5984 |
} |
| 5985 |
|
| 5986 |
ret = btrfs_del_item(trans, fs_info->remap_root, path); |
| 5987 |
|
| 5988 |
btrfs_release_path(path); |
| 5989 |
|
| 5990 |
if (ret) |
| 5991 |
return ret; |
| 5992 |
} |
| 5993 |
|
| 5994 |
/* If hole_start > remap_start, re-add the start of the remap item. */ |
| 5995 |
if (hole_start > remap_start) { |
| 5996 |
ret = insert_remap_item(trans, path, remap_start, |
| 5997 |
hole_start - remap_start, new_addr); |
| 5998 |
if (ret) |
| 5999 |
return ret; |
| 6000 |
|
| 6001 |
if (is_identity_remap) |
| 6002 |
identity_count_delta++; |
| 6003 |
} |
| 6004 |
|
| 6005 |
/* If hole_end < remap_end, re-add the end of the remap item. */ |
| 6006 |
if (hole_end < remap_end) { |
| 6007 |
ret = insert_remap_item(trans, path, hole_end, |
| 6008 |
remap_end - hole_end, |
| 6009 |
hole_end - remap_start + new_addr); |
| 6010 |
if (ret) |
| 6011 |
return ret; |
| 6012 |
|
| 6013 |
if (is_identity_remap) |
| 6014 |
identity_count_delta++; |
| 6015 |
} |
| 6016 |
|
| 6017 |
if (identity_count_delta != 0) |
| 6018 |
adjust_identity_remap_count(trans, bg, identity_count_delta); |
| 6019 |
|
| 6020 |
overlap_length = min_t(u64, hole_end, remap_end) - |
| 6021 |
max_t(u64, hole_start, remap_start); |
| 6022 |
|
| 6023 |
if (!is_identity_remap) { |
| 6024 |
struct btrfs_block_group *dest_bg; |
| 6025 |
|
| 6026 |
dest_bg = btrfs_lookup_block_group(fs_info, new_addr); |
| 6027 |
if (unlikely(!dest_bg)) |
| 6028 |
return -EUCLEAN; |
| 6029 |
|
| 6030 |
adjust_block_group_remap_bytes(trans, dest_bg, -overlap_length); |
| 6031 |
btrfs_put_block_group(dest_bg); |
| 6032 |
ret = btrfs_add_to_free_space_tree(trans, |
| 6033 |
hole_start - remap_start + new_addr, |
| 6034 |
overlap_length); |
| 6035 |
if (ret) |
| 6036 |
return ret; |
| 6037 |
} |
| 6038 |
|
| 6039 |
ret = overlap_length; |
| 6040 |
|
| 6041 |
return ret; |
| 6042 |
} |
| 6043 |
|
| 6044 |
/* |
| 6045 |
* Return 1 if remove_range_from_remap_tree() has been called successfully, |
| 6046 |
* 0 if block group wasn't remapped, and a negative number on error. |
| 6047 |
*/ |
| 6048 |
int btrfs_remove_extent_from_remap_tree(struct btrfs_trans_handle *trans, |
| 6049 |
struct btrfs_path *path, |
| 6050 |
u64 bytenr, u64 num_bytes) |
| 6051 |
{ |
| 6052 |
struct btrfs_fs_info *fs_info = trans->fs_info; |
| 6053 |
struct btrfs_key key, found_key; |
| 6054 |
struct extent_buffer *leaf; |
| 6055 |
struct btrfs_block_group *bg; |
| 6056 |
int ret, length; |
| 6057 |
|
| 6058 |
if (!(btrfs_super_incompat_flags(fs_info->super_copy) & |
| 6059 |
BTRFS_FEATURE_INCOMPAT_REMAP_TREE)) |
| 6060 |
return 0; |
| 6061 |
|
| 6062 |
bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 6063 |
if (!bg) |
| 6064 |
return 0; |
| 6065 |
|
| 6066 |
mutex_lock(&fs_info->remap_mutex); |
| 6067 |
|
| 6068 |
if (!(bg->flags & BTRFS_BLOCK_GROUP_REMAPPED)) { |
| 6069 |
mutex_unlock(&fs_info->remap_mutex); |
| 6070 |
btrfs_put_block_group(bg); |
| 6071 |
return 0; |
| 6072 |
} |
| 6073 |
|
| 6074 |
do { |
| 6075 |
key.objectid = bytenr; |
| 6076 |
key.type = (u8)-1; |
| 6077 |
key.offset = (u64)-1; |
| 6078 |
|
| 6079 |
ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, -1, 1); |
| 6080 |
if (ret < 0) |
| 6081 |
goto end; |
| 6082 |
|
| 6083 |
leaf = path->nodes[0]; |
| 6084 |
if (path->slots[0] == 0) { |
| 6085 |
ret = -ENOENT; |
| 6086 |
goto end; |
| 6087 |
} |
| 6088 |
|
| 6089 |
path->slots[0]--; |
| 6090 |
|
| 6091 |
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6092 |
|
| 6093 |
if (found_key.type != BTRFS_IDENTITY_REMAP_KEY && |
| 6094 |
found_key.type != BTRFS_REMAP_KEY) { |
| 6095 |
ret = -ENOENT; |
| 6096 |
goto end; |
| 6097 |
} |
| 6098 |
|
| 6099 |
if (bytenr < found_key.objectid || |
| 6100 |
bytenr >= found_key.objectid + found_key.offset) { |
| 6101 |
ret = -ENOENT; |
| 6102 |
goto end; |
| 6103 |
} |
| 6104 |
|
| 6105 |
length = remove_range_from_remap_tree(trans, path, bg, bytenr, num_bytes); |
| 6106 |
if (length < 0) { |
| 6107 |
ret = length; |
| 6108 |
goto end; |
| 6109 |
} |
| 6110 |
|
| 6111 |
bytenr += length; |
| 6112 |
num_bytes -= length; |
| 6113 |
} while (num_bytes > 0); |
| 6114 |
|
| 6115 |
ret = 1; |
| 6116 |
|
| 6117 |
end: |
| 6118 |
mutex_unlock(&fs_info->remap_mutex); |
| 6119 |
|
| 6120 |
btrfs_put_block_group(bg); |
| 6121 |
btrfs_release_path(path); |
| 6122 |
|
| 6123 |
return ret; |
| 6124 |
} |