Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 "ctree.h" |
| 21 | #include "disk-io.h" |
| 22 | #include "print-tree.h" |
| 23 | #include "transaction.h" |
| 24 | |
| 25 | static void reada_defrag(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 26 | struct extent_buffer *node) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 27 | { |
| 28 | int i; |
| 29 | u32 nritems; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 30 | u64 bytenr; |
| 31 | u32 blocksize; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 32 | int ret; |
| 33 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 34 | blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 35 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 36 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 37 | bytenr = btrfs_node_blockptr(node, i); |
| 38 | ret = readahead_tree_block(root, bytenr, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 39 | if (ret) |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static int defrag_walk_down(struct btrfs_trans_handle *trans, |
| 45 | struct btrfs_root *root, |
| 46 | struct btrfs_path *path, int *level, |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 47 | int cache_only, u64 *last_ret) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 48 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 49 | struct extent_buffer *next; |
| 50 | struct extent_buffer *cur; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 51 | u64 bytenr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 52 | int ret = 0; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 53 | int is_extent = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 54 | |
| 55 | WARN_ON(*level < 0); |
| 56 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 57 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 58 | if (root->fs_info->extent_root == root) |
| 59 | is_extent = 1; |
| 60 | |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 61 | if (*level == 1 && cache_only && path->nodes[1] && |
| 62 | !btrfs_buffer_defrag(path->nodes[1])) { |
| 63 | goto out; |
| 64 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 65 | while(*level > 0) { |
| 66 | WARN_ON(*level < 0); |
| 67 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 68 | cur = path->nodes[*level]; |
| 69 | |
| 70 | if (!cache_only && *level > 1 && path->slots[*level] == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 71 | reada_defrag(root, cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 72 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 73 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 74 | WARN_ON(1); |
| 75 | |
| 76 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 77 | btrfs_header_nritems(cur)) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 78 | break; |
| 79 | |
| 80 | if (*level == 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 81 | WARN_ON(btrfs_header_generation(path->nodes[*level]) != |
| 82 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 83 | ret = btrfs_realloc_node(trans, root, |
| 84 | path->nodes[*level], |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 85 | path->slots[*level], |
| 86 | cache_only, last_ret, |
| 87 | &root->defrag_progress); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 88 | if (is_extent) |
| 89 | btrfs_extent_post_op(trans, root); |
| 90 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 91 | break; |
| 92 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 93 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 94 | |
| 95 | if (cache_only) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 96 | next = btrfs_find_tree_block(root, bytenr, |
| 97 | btrfs_level_size(root, *level - 1)); |
Chris Mason | cf786e7 | 2007-10-15 16:22:11 -0400 | [diff] [blame] | 98 | if (!next || !btrfs_buffer_uptodate(next) || |
| 99 | !btrfs_buffer_defrag(next)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 100 | free_extent_buffer(next); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 101 | path->slots[*level]++; |
| 102 | continue; |
| 103 | } |
Chris Mason | 0999df5 | 2008-04-01 13:48:14 -0400 | [diff] [blame] | 104 | btrfs_verify_block_csum(root, next); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 105 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 106 | next = read_tree_block(root, bytenr, |
| 107 | btrfs_level_size(root, *level - 1)); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 108 | } |
| 109 | ret = btrfs_cow_block(trans, root, next, path->nodes[*level], |
| 110 | path->slots[*level], &next); |
| 111 | BUG_ON(ret); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 112 | if (is_extent) |
| 113 | btrfs_extent_post_op(trans, root); |
| 114 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 115 | WARN_ON(*level <= 0); |
| 116 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 117 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 118 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 119 | *level = btrfs_header_level(next); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 120 | path->slots[*level] = 0; |
| 121 | } |
| 122 | WARN_ON(*level < 0); |
| 123 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 124 | |
| 125 | btrfs_clear_buffer_defrag(path->nodes[*level]); |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 126 | out: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 127 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 128 | path->nodes[*level] = NULL; |
| 129 | *level += 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 130 | WARN_ON(ret && ret != -EAGAIN); |
| 131 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int defrag_walk_up(struct btrfs_trans_handle *trans, |
| 135 | struct btrfs_root *root, |
| 136 | struct btrfs_path *path, int *level, |
| 137 | int cache_only) |
| 138 | { |
| 139 | int i; |
| 140 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 141 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 142 | |
| 143 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
| 144 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 145 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 146 | path->slots[i]++; |
| 147 | *level = i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 148 | node = path->nodes[i]; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 149 | WARN_ON(i == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 150 | btrfs_node_key_to_cpu(node, &root->defrag_progress, |
| 151 | path->slots[i]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 152 | root->defrag_level = i; |
| 153 | return 0; |
| 154 | } else { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 155 | btrfs_clear_buffer_defrag(path->nodes[*level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 156 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 157 | path->nodes[*level] = NULL; |
| 158 | *level = i + 1; |
| 159 | } |
| 160 | } |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, |
| 165 | struct btrfs_root *root, int cache_only) |
| 166 | { |
| 167 | struct btrfs_path *path = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 168 | struct extent_buffer *tmp; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 169 | int ret = 0; |
| 170 | int wret; |
| 171 | int level; |
| 172 | int orig_level; |
| 173 | int i; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 174 | int is_extent = 0; |
| 175 | u64 last_ret = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 176 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 177 | if (root->fs_info->extent_root == root) |
| 178 | is_extent = 1; |
| 179 | |
| 180 | if (root->ref_cows == 0 && !is_extent) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 181 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 182 | |
Chris Mason | 9afbb0b | 2008-02-15 13:19:35 -0500 | [diff] [blame] | 183 | if (btrfs_test_opt(root, SSD)) |
| 184 | goto out; |
| 185 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 186 | path = btrfs_alloc_path(); |
| 187 | if (!path) |
| 188 | return -ENOMEM; |
| 189 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 190 | level = btrfs_header_level(root->node); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 191 | orig_level = level; |
Chris Mason | 0f1ebbd | 2007-10-15 16:19:46 -0400 | [diff] [blame] | 192 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 193 | if (level == 0) { |
| 194 | goto out; |
| 195 | } |
| 196 | if (root->defrag_progress.objectid == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 197 | extent_buffer_get(root->node); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 198 | ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); |
| 199 | BUG_ON(ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 200 | path->nodes[level] = root->node; |
| 201 | path->slots[level] = 0; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 202 | if (is_extent) |
| 203 | btrfs_extent_post_op(trans, root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 204 | } else { |
| 205 | level = root->defrag_level; |
| 206 | path->lowest_level = level; |
| 207 | wret = btrfs_search_slot(trans, root, &root->defrag_progress, |
| 208 | path, 0, 1); |
| 209 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 210 | if (is_extent) |
| 211 | btrfs_extent_post_op(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 212 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 213 | if (wret < 0) { |
| 214 | ret = wret; |
| 215 | goto out; |
| 216 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 217 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 218 | while(level > 0 && !path->nodes[level]) |
| 219 | level--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 220 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 221 | if (!path->nodes[level]) { |
| 222 | ret = 0; |
| 223 | goto out; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | while(1) { |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 228 | wret = defrag_walk_down(trans, root, path, &level, cache_only, |
| 229 | &last_ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 230 | if (wret > 0) |
| 231 | break; |
| 232 | if (wret < 0) |
| 233 | ret = wret; |
| 234 | |
| 235 | wret = defrag_walk_up(trans, root, path, &level, cache_only); |
| 236 | if (wret > 0) |
| 237 | break; |
| 238 | if (wret < 0) |
| 239 | ret = wret; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 240 | else |
| 241 | ret = -EAGAIN; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 242 | break; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 243 | } |
| 244 | for (i = 0; i <= orig_level; i++) { |
| 245 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 246 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 247 | path->nodes[i] = NULL; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | out: |
| 251 | if (path) |
| 252 | btrfs_free_path(path); |
| 253 | if (ret != -EAGAIN) { |
| 254 | memset(&root->defrag_progress, 0, |
| 255 | sizeof(root->defrag_progress)); |
| 256 | } |
| 257 | return ret; |
| 258 | } |