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