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