Blame view
fs/btrfs/tree-defrag.c
4.16 KB
6702ed490 Btrfs: Add run ti... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/* * Copyright (C) 2007 Oracle. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License v2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. */ #include <linux/sched.h> #include "ctree.h" #include "disk-io.h" #include "print-tree.h" #include "transaction.h" |
e7a84565b Btrfs: Add btree ... |
24 |
#include "locking.h" |
6702ed490 Btrfs: Add run ti... |
25 |
|
de78b51a2 btrfs: remove cac... |
26 27 28 |
/* * Defrag all the leaves in a given btree. * Read all the leaves and try to get key order to |
d352ac681 Btrfs: add and im... |
29 30 |
* better reflect disk order */ |
d397712bc Btrfs: Fix checkp... |
31 |
|
6702ed490 Btrfs: Add run ti... |
32 |
int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, |
de78b51a2 btrfs: remove cac... |
33 |
struct btrfs_root *root) |
6702ed490 Btrfs: Add run ti... |
34 35 |
{ struct btrfs_path *path = NULL; |
e7a84565b Btrfs: Add btree ... |
36 |
struct btrfs_key key; |
6702ed490 Btrfs: Add run ti... |
37 38 39 |
int ret = 0; int wret; int level; |
e7a84565b Btrfs: Add btree ... |
40 |
int next_key_ret = 0; |
e9d0b13b5 Btrfs: Btree defr... |
41 |
u64 last_ret = 0; |
3f157a2fd Btrfs: Online btr... |
42 |
u64 min_trans = 0; |
e7a84565b Btrfs: Add btree ... |
43 |
if (root->fs_info->extent_root == root) { |
1b1e2135d Btrfs: Add a per-... |
44 45 46 47 48 |
/* * there's recursion here right now in the tree locking, * we can't defrag the extent root without deadlock */ goto out; |
e7a84565b Btrfs: Add btree ... |
49 |
} |
925baeddc Btrfs: Start btre... |
50 |
|
27cdeb709 Btrfs: use bitfie... |
51 |
if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
6702ed490 Btrfs: Add run ti... |
52 |
goto out; |
5f39d397d Btrfs: Create ext... |
53 |
|
6702ed490 Btrfs: Add run ti... |
54 55 56 |
path = btrfs_alloc_path(); if (!path) return -ENOMEM; |
5f39d397d Btrfs: Create ext... |
57 |
level = btrfs_header_level(root->node); |
0f1ebbd15 Btrfs: Large bloc... |
58 |
|
d397712bc Btrfs: Fix checkp... |
59 |
if (level == 0) |
6702ed490 Btrfs: Add run ti... |
60 |
goto out; |
d397712bc Btrfs: Fix checkp... |
61 |
|
6702ed490 Btrfs: Add run ti... |
62 |
if (root->defrag_progress.objectid == 0) { |
e7a84565b Btrfs: Add btree ... |
63 |
struct extent_buffer *root_node; |
0ef3e66b6 Btrfs: Allocator ... |
64 |
u32 nritems; |
e7a84565b Btrfs: Add btree ... |
65 |
root_node = btrfs_lock_root_node(root); |
b4ce94de9 Btrfs: Change btr... |
66 |
btrfs_set_lock_blocking(root_node); |
e7a84565b Btrfs: Add btree ... |
67 |
nritems = btrfs_header_nritems(root_node); |
0ef3e66b6 Btrfs: Allocator ... |
68 69 |
root->defrag_max.objectid = 0; /* from above we know this is not a leaf */ |
e7a84565b Btrfs: Add btree ... |
70 |
btrfs_node_key_to_cpu(root_node, &root->defrag_max, |
0ef3e66b6 Btrfs: Allocator ... |
71 |
nritems - 1); |
e7a84565b Btrfs: Add btree ... |
72 73 74 |
btrfs_tree_unlock(root_node); free_extent_buffer(root_node); memset(&key, 0, sizeof(key)); |
6702ed490 Btrfs: Add run ti... |
75 |
} else { |
e7a84565b Btrfs: Add btree ... |
76 |
memcpy(&key, &root->defrag_progress, sizeof(key)); |
6702ed490 Btrfs: Add run ti... |
77 |
} |
e7a84565b Btrfs: Add btree ... |
78 |
path->keep_locks = 1; |
3f157a2fd Btrfs: Online btr... |
79 |
|
6174d3cb4 Btrfs: remove unu... |
80 |
ret = btrfs_search_forward(root, &key, path, min_trans); |
3f157a2fd Btrfs: Online btr... |
81 82 83 84 85 86 |
if (ret < 0) goto out; if (ret > 0) { ret = 0; goto out; } |
b3b4aa74b btrfs: drop unuse... |
87 |
btrfs_release_path(path); |
0376374a9 Btrfs: fix lockin... |
88 89 90 91 92 93 |
/* * We don't need a lock on a leaf. btrfs_realloc_node() will lock all * leafs from path->nodes[1], so set lowest_level to 1 to avoid later * a deadlock (attempting to write lock an already write locked leaf). */ path->lowest_level = 1; |
e7a84565b Btrfs: Add btree ... |
94 |
wret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
6702ed490 Btrfs: Add run ti... |
95 |
|
e7a84565b Btrfs: Add btree ... |
96 97 98 99 100 101 102 103 |
if (wret < 0) { ret = wret; goto out; } if (!path->nodes[1]) { ret = 0; goto out; } |
0376374a9 Btrfs: fix lockin... |
104 105 106 107 108 109 |
/* * The node at level 1 must always be locked when our path has * keep_locks set and lowest_level is 1, regardless of the value of * path->slots[1]. */ BUG_ON(path->locks[1] == 0); |
e7a84565b Btrfs: Add btree ... |
110 111 |
ret = btrfs_realloc_node(trans, root, path->nodes[1], 0, |
de78b51a2 btrfs: remove cac... |
112 |
&last_ret, |
e7a84565b Btrfs: Add btree ... |
113 |
&root->defrag_progress); |
8929ecfa5 Btrfs: Introduce ... |
114 115 116 117 |
if (ret) { WARN_ON(ret == -EAGAIN); goto out; } |
0376374a9 Btrfs: fix lockin... |
118 119 120 121 122 123 124 125 126 127 128 129 |
/* * Now that we reallocated the node we can find the next key. Note that * btrfs_find_next_key() can release our path and do another search * without COWing, this is because even with path->keep_locks = 1, * btrfs_search_slot() / ctree.c:unlock_up() does not keeps a lock on a * node when path->slots[node_level - 1] does not point to the last * item or a slot beyond the last item (ctree.c:unlock_up()). Therefore * we search for the next key after reallocating our node. */ path->slots[1] = btrfs_header_nritems(path->nodes[1]); next_key_ret = btrfs_find_next_key(root, path, &key, 1, min_trans); |
e7a84565b Btrfs: Add btree ... |
130 131 132 |
if (next_key_ret == 0) { memcpy(&root->defrag_progress, &key, sizeof(key)); ret = -EAGAIN; |
6702ed490 Btrfs: Add run ti... |
133 |
} |
6702ed490 Btrfs: Add run ti... |
134 |
out: |
527afb449 Btrfs: cleanup: r... |
135 |
btrfs_free_path(path); |
0ef3e66b6 Btrfs: Allocator ... |
136 137 138 139 140 141 142 143 144 145 |
if (ret == -EAGAIN) { if (root->defrag_max.objectid > root->defrag_progress.objectid) goto done; if (root->defrag_max.type > root->defrag_progress.type) goto done; if (root->defrag_max.offset > root->defrag_progress.offset) goto done; ret = 0; } done: |
6702ed490 Btrfs: Add run ti... |
146 147 148 |
if (ret != -EAGAIN) { memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
3f157a2fd Btrfs: Online btr... |
149 |
root->defrag_trans_start = trans->transid; |
6702ed490 Btrfs: Add run ti... |
150 151 152 |
} return ret; } |