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, |
| 26 | struct btrfs_node *node) |
| 27 | { |
| 28 | int i; |
| 29 | u32 nritems; |
| 30 | u64 blocknr; |
| 31 | int ret; |
| 32 | |
| 33 | nritems = btrfs_header_nritems(&node->header); |
| 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 | { |
| 47 | struct buffer_head *next; |
| 48 | struct buffer_head *cur; |
| 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) |
| 65 | reada_defrag(root, btrfs_buffer_node(cur)); |
| 66 | |
| 67 | if (btrfs_header_level(btrfs_buffer_header(cur)) != *level) |
| 68 | WARN_ON(1); |
| 69 | |
| 70 | if (path->slots[*level] >= |
| 71 | btrfs_header_nritems(btrfs_buffer_header(cur))) |
| 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 | } |
| 83 | blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur), |
| 84 | path->slots[*level]); |
| 85 | |
| 86 | if (cache_only) { |
| 87 | next = btrfs_find_tree_block(root, blocknr); |
| 88 | if (!next || !buffer_uptodate(next) || |
| 89 | buffer_locked(next)) { |
| 90 | brelse(next); |
| 91 | path->slots[*level]++; |
| 92 | continue; |
| 93 | } |
| 94 | } else { |
| 95 | next = read_tree_block(root, blocknr); |
| 96 | } |
| 97 | ret = btrfs_cow_block(trans, root, next, path->nodes[*level], |
| 98 | path->slots[*level], &next); |
| 99 | BUG_ON(ret); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 100 | ret = btrfs_realloc_node(trans, root, next, cache_only, |
| 101 | last_ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 102 | BUG_ON(ret); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 103 | |
| 104 | if (is_extent) |
| 105 | btrfs_extent_post_op(trans, root); |
| 106 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 107 | WARN_ON(*level <= 0); |
| 108 | if (path->nodes[*level-1]) |
| 109 | btrfs_block_release(root, path->nodes[*level-1]); |
| 110 | path->nodes[*level-1] = next; |
| 111 | *level = btrfs_header_level(btrfs_buffer_header(next)); |
| 112 | path->slots[*level] = 0; |
| 113 | } |
| 114 | WARN_ON(*level < 0); |
| 115 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
| 116 | btrfs_block_release(root, path->nodes[*level]); |
| 117 | path->nodes[*level] = NULL; |
| 118 | *level += 1; |
| 119 | WARN_ON(ret); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int defrag_walk_up(struct btrfs_trans_handle *trans, |
| 124 | struct btrfs_root *root, |
| 125 | struct btrfs_path *path, int *level, |
| 126 | int cache_only) |
| 127 | { |
| 128 | int i; |
| 129 | int slot; |
| 130 | struct btrfs_node *node; |
| 131 | |
| 132 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
| 133 | slot = path->slots[i]; |
| 134 | if (slot < btrfs_header_nritems( |
| 135 | btrfs_buffer_header(path->nodes[i])) - 1) { |
| 136 | path->slots[i]++; |
| 137 | *level = i; |
| 138 | node = btrfs_buffer_node(path->nodes[i]); |
| 139 | WARN_ON(i == 0); |
| 140 | btrfs_disk_key_to_cpu(&root->defrag_progress, |
| 141 | &node->ptrs[path->slots[i]].key); |
| 142 | root->defrag_level = i; |
| 143 | return 0; |
| 144 | } else { |
| 145 | btrfs_block_release(root, path->nodes[*level]); |
| 146 | path->nodes[*level] = NULL; |
| 147 | *level = i + 1; |
| 148 | } |
| 149 | } |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, |
| 154 | struct btrfs_root *root, int cache_only) |
| 155 | { |
| 156 | struct btrfs_path *path = NULL; |
| 157 | struct buffer_head *tmp; |
| 158 | int ret = 0; |
| 159 | int wret; |
| 160 | int level; |
| 161 | int orig_level; |
| 162 | int i; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 163 | int is_extent = 0; |
| 164 | u64 last_ret = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 165 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 166 | if (root->fs_info->extent_root == root) |
| 167 | is_extent = 1; |
| 168 | |
| 169 | if (root->ref_cows == 0 && !is_extent) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 170 | goto out; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 171 | path = btrfs_alloc_path(); |
| 172 | if (!path) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | level = btrfs_header_level(btrfs_buffer_header(root->node)); |
| 176 | orig_level = level; |
| 177 | if (level == 0) { |
| 178 | goto out; |
| 179 | } |
| 180 | if (root->defrag_progress.objectid == 0) { |
| 181 | get_bh(root->node); |
| 182 | ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); |
| 183 | BUG_ON(ret); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 184 | ret = btrfs_realloc_node(trans, root, root->node, cache_only, |
| 185 | &last_ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 186 | BUG_ON(ret); |
| 187 | path->nodes[level] = root->node; |
| 188 | path->slots[level] = 0; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 189 | if (is_extent) |
| 190 | btrfs_extent_post_op(trans, root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 191 | } else { |
| 192 | level = root->defrag_level; |
| 193 | path->lowest_level = level; |
| 194 | wret = btrfs_search_slot(trans, root, &root->defrag_progress, |
| 195 | path, 0, 1); |
| 196 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 197 | if (is_extent) |
| 198 | btrfs_extent_post_op(trans, root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 199 | if (wret < 0) { |
| 200 | ret = wret; |
| 201 | goto out; |
| 202 | } |
| 203 | while(level > 0 && !path->nodes[level]) |
| 204 | level--; |
| 205 | if (!path->nodes[level]) { |
| 206 | ret = 0; |
| 207 | goto out; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | while(1) { |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame^] | 212 | wret = defrag_walk_down(trans, root, path, &level, cache_only, |
| 213 | &last_ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 214 | if (wret > 0) |
| 215 | break; |
| 216 | if (wret < 0) |
| 217 | ret = wret; |
| 218 | |
| 219 | wret = defrag_walk_up(trans, root, path, &level, cache_only); |
| 220 | if (wret > 0) |
| 221 | break; |
| 222 | if (wret < 0) |
| 223 | ret = wret; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 224 | ret = -EAGAIN; |
| 225 | break; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 226 | } |
| 227 | for (i = 0; i <= orig_level; i++) { |
| 228 | if (path->nodes[i]) { |
| 229 | btrfs_block_release(root, path->nodes[i]); |
| 230 | path->nodes[i] = 0; |
| 231 | } |
| 232 | } |
| 233 | out: |
| 234 | if (path) |
| 235 | btrfs_free_path(path); |
| 236 | if (ret != -EAGAIN) { |
| 237 | memset(&root->defrag_progress, 0, |
| 238 | sizeof(root->defrag_progress)); |
| 239 | } |
| 240 | return ret; |
| 241 | } |