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 | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 61 | while(*level > 0) { |
| 62 | WARN_ON(*level < 0); |
| 63 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 64 | cur = path->nodes[*level]; |
| 65 | |
| 66 | if (!cache_only && *level > 1 && path->slots[*level] == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 67 | reada_defrag(root, cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 68 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 69 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 70 | WARN_ON(1); |
| 71 | |
| 72 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 73 | btrfs_header_nritems(cur)) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 74 | break; |
| 75 | |
| 76 | if (*level == 1) { |
| 77 | ret = btrfs_realloc_node(trans, root, |
| 78 | path->nodes[*level], |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame^] | 79 | path->slots[*level], |
| 80 | cache_only, last_ret, |
| 81 | &root->defrag_progress); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 82 | if (is_extent) |
| 83 | btrfs_extent_post_op(trans, root); |
| 84 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 85 | break; |
| 86 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 87 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 88 | |
| 89 | if (cache_only) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 90 | next = btrfs_find_tree_block(root, bytenr, |
| 91 | btrfs_level_size(root, *level - 1)); |
Chris Mason | cf786e7 | 2007-10-15 16:22:11 -0400 | [diff] [blame] | 92 | if (!next || !btrfs_buffer_uptodate(next) || |
| 93 | !btrfs_buffer_defrag(next)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 94 | free_extent_buffer(next); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 95 | path->slots[*level]++; |
| 96 | continue; |
| 97 | } |
| 98 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 99 | next = read_tree_block(root, bytenr, |
| 100 | btrfs_level_size(root, *level - 1)); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 101 | } |
| 102 | ret = btrfs_cow_block(trans, root, next, path->nodes[*level], |
| 103 | path->slots[*level], &next); |
| 104 | BUG_ON(ret); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 105 | if (is_extent) |
| 106 | btrfs_extent_post_op(trans, root); |
| 107 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 108 | WARN_ON(*level <= 0); |
| 109 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 110 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 111 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 112 | *level = btrfs_header_level(next); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 113 | path->slots[*level] = 0; |
| 114 | } |
| 115 | WARN_ON(*level < 0); |
| 116 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 117 | |
| 118 | btrfs_clear_buffer_defrag(path->nodes[*level]); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 119 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 120 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 121 | path->nodes[*level] = NULL; |
| 122 | *level += 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame^] | 123 | WARN_ON(ret && ret != -EAGAIN); |
| 124 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static int defrag_walk_up(struct btrfs_trans_handle *trans, |
| 128 | struct btrfs_root *root, |
| 129 | struct btrfs_path *path, int *level, |
| 130 | int cache_only) |
| 131 | { |
| 132 | int i; |
| 133 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 134 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 135 | |
| 136 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
| 137 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 138 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 139 | path->slots[i]++; |
| 140 | *level = i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 141 | node = path->nodes[i]; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 142 | WARN_ON(i == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 143 | btrfs_node_key_to_cpu(node, &root->defrag_progress, |
| 144 | path->slots[i]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 145 | root->defrag_level = i; |
| 146 | return 0; |
| 147 | } else { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 148 | btrfs_clear_buffer_defrag(path->nodes[*level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 149 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 150 | path->nodes[*level] = NULL; |
| 151 | *level = i + 1; |
| 152 | } |
| 153 | } |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, |
| 158 | struct btrfs_root *root, int cache_only) |
| 159 | { |
| 160 | struct btrfs_path *path = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 161 | struct extent_buffer *tmp; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 162 | int ret = 0; |
| 163 | int wret; |
| 164 | int level; |
| 165 | int orig_level; |
| 166 | int i; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 167 | int is_extent = 0; |
| 168 | u64 last_ret = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 169 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 170 | if (root->fs_info->extent_root == root) |
| 171 | is_extent = 1; |
| 172 | |
| 173 | if (root->ref_cows == 0 && !is_extent) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 174 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 175 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 176 | path = btrfs_alloc_path(); |
| 177 | if (!path) |
| 178 | return -ENOMEM; |
| 179 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 180 | level = btrfs_header_level(root->node); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 181 | orig_level = level; |
Chris Mason | 0f1ebbd | 2007-10-15 16:19:46 -0400 | [diff] [blame] | 182 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 183 | if (level == 0) { |
| 184 | goto out; |
| 185 | } |
Chris Mason | 0f1ebbd | 2007-10-15 16:19:46 -0400 | [diff] [blame] | 186 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 187 | if (root->defrag_progress.objectid == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 188 | extent_buffer_get(root->node); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 189 | ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); |
| 190 | BUG_ON(ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 191 | path->nodes[level] = root->node; |
| 192 | path->slots[level] = 0; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 193 | if (is_extent) |
| 194 | btrfs_extent_post_op(trans, root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 195 | } else { |
| 196 | level = root->defrag_level; |
| 197 | path->lowest_level = level; |
| 198 | wret = btrfs_search_slot(trans, root, &root->defrag_progress, |
| 199 | path, 0, 1); |
| 200 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 201 | if (is_extent) |
| 202 | btrfs_extent_post_op(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 203 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 204 | if (wret < 0) { |
| 205 | ret = wret; |
| 206 | goto out; |
| 207 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 208 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 209 | while(level > 0 && !path->nodes[level]) |
| 210 | level--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 211 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 212 | if (!path->nodes[level]) { |
| 213 | ret = 0; |
| 214 | goto out; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | while(1) { |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 219 | wret = defrag_walk_down(trans, root, path, &level, cache_only, |
| 220 | &last_ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 221 | if (wret > 0) |
| 222 | break; |
| 223 | if (wret < 0) |
| 224 | ret = wret; |
| 225 | |
| 226 | wret = defrag_walk_up(trans, root, path, &level, cache_only); |
| 227 | if (wret > 0) |
| 228 | break; |
| 229 | if (wret < 0) |
| 230 | ret = wret; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 231 | ret = -EAGAIN; |
| 232 | break; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 233 | } |
| 234 | for (i = 0; i <= orig_level; i++) { |
| 235 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 236 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 237 | path->nodes[i] = NULL; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | out: |
| 241 | if (path) |
| 242 | btrfs_free_path(path); |
| 243 | if (ret != -EAGAIN) { |
| 244 | memset(&root->defrag_progress, 0, |
| 245 | sizeof(root->defrag_progress)); |
| 246 | } |
| 247 | return ret; |
| 248 | } |