btrfs_get_extent should treat inline extents as though they hold a whole block
[deliverable/linux.git] / fs / btrfs / tree-defrag.c
CommitLineData
6702ed49
CM
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
25static 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
42static int defrag_walk_down(struct btrfs_trans_handle *trans,
43 struct btrfs_root *root,
44 struct btrfs_path *path, int *level,
e9d0b13b 45 int cache_only, u64 *last_ret)
6702ed49
CM
46{
47 struct buffer_head *next;
48 struct buffer_head *cur;
49 u64 blocknr;
50 int ret = 0;
e9d0b13b 51 int is_extent = 0;
6702ed49
CM
52
53 WARN_ON(*level < 0);
54 WARN_ON(*level >= BTRFS_MAX_LEVEL);
55
e9d0b13b
CM
56 if (root->fs_info->extent_root == root)
57 is_extent = 1;
58
6702ed49
CM
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],
e9d0b13b
CM
77 cache_only, last_ret);
78 if (is_extent)
79 btrfs_extent_post_op(trans, root);
80
6702ed49
CM
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) ||
f2183bde 89 buffer_locked(next) || !buffer_defrag(next)) {
6702ed49
CM
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);
e9d0b13b
CM
100 ret = btrfs_realloc_node(trans, root, next, cache_only,
101 last_ret);
6702ed49 102 BUG_ON(ret);
e9d0b13b
CM
103
104 if (is_extent)
105 btrfs_extent_post_op(trans, root);
106
6702ed49
CM
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);
86479a04
CM
116 clear_buffer_defrag(path->nodes[*level]);
117 clear_buffer_defrag_done(path->nodes[*level]);
6702ed49
CM
118 btrfs_block_release(root, path->nodes[*level]);
119 path->nodes[*level] = NULL;
120 *level += 1;
121 WARN_ON(ret);
122 return 0;
123}
124
125static int defrag_walk_up(struct btrfs_trans_handle *trans,
126 struct btrfs_root *root,
127 struct btrfs_path *path, int *level,
128 int cache_only)
129{
130 int i;
131 int slot;
132 struct btrfs_node *node;
133
134 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
135 slot = path->slots[i];
136 if (slot < btrfs_header_nritems(
137 btrfs_buffer_header(path->nodes[i])) - 1) {
138 path->slots[i]++;
139 *level = i;
140 node = btrfs_buffer_node(path->nodes[i]);
141 WARN_ON(i == 0);
142 btrfs_disk_key_to_cpu(&root->defrag_progress,
143 &node->ptrs[path->slots[i]].key);
144 root->defrag_level = i;
145 return 0;
146 } else {
f2183bde 147 clear_buffer_defrag(path->nodes[*level]);
86479a04 148 clear_buffer_defrag_done(path->nodes[*level]);
6702ed49
CM
149 btrfs_block_release(root, path->nodes[*level]);
150 path->nodes[*level] = NULL;
151 *level = i + 1;
152 }
153 }
154 return 1;
155}
156
157int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
158 struct btrfs_root *root, int cache_only)
159{
160 struct btrfs_path *path = NULL;
161 struct buffer_head *tmp;
162 int ret = 0;
163 int wret;
164 int level;
165 int orig_level;
166 int i;
e9d0b13b
CM
167 int is_extent = 0;
168 u64 last_ret = 0;
169
170 if (root->fs_info->extent_root == root)
171 is_extent = 1;
6702ed49 172
e9d0b13b 173 if (root->ref_cows == 0 && !is_extent)
6702ed49 174 goto out;
6702ed49
CM
175 path = btrfs_alloc_path();
176 if (!path)
177 return -ENOMEM;
178
179 level = btrfs_header_level(btrfs_buffer_header(root->node));
180 orig_level = level;
181 if (level == 0) {
182 goto out;
183 }
184 if (root->defrag_progress.objectid == 0) {
185 get_bh(root->node);
186 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
187 BUG_ON(ret);
e9d0b13b
CM
188 ret = btrfs_realloc_node(trans, root, root->node, cache_only,
189 &last_ret);
6702ed49
CM
190 BUG_ON(ret);
191 path->nodes[level] = root->node;
192 path->slots[level] = 0;
e9d0b13b
CM
193 if (is_extent)
194 btrfs_extent_post_op(trans, root);
6702ed49
CM
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
e9d0b13b
CM
201 if (is_extent)
202 btrfs_extent_post_op(trans, root);
6702ed49
CM
203 if (wret < 0) {
204 ret = wret;
205 goto out;
206 }
207 while(level > 0 && !path->nodes[level])
208 level--;
209 if (!path->nodes[level]) {
210 ret = 0;
211 goto out;
212 }
213 }
214
215 while(1) {
e9d0b13b
CM
216 wret = defrag_walk_down(trans, root, path, &level, cache_only,
217 &last_ret);
6702ed49
CM
218 if (wret > 0)
219 break;
220 if (wret < 0)
221 ret = wret;
222
223 wret = defrag_walk_up(trans, root, path, &level, cache_only);
224 if (wret > 0)
225 break;
226 if (wret < 0)
227 ret = wret;
409eb95d
CM
228 ret = -EAGAIN;
229 break;
6702ed49
CM
230 }
231 for (i = 0; i <= orig_level; i++) {
232 if (path->nodes[i]) {
233 btrfs_block_release(root, path->nodes[i]);
234 path->nodes[i] = 0;
235 }
236 }
237out:
238 if (path)
239 btrfs_free_path(path);
240 if (ret != -EAGAIN) {
241 memset(&root->defrag_progress, 0,
242 sizeof(root->defrag_progress));
243 }
244 return ret;
245}
This page took 0.041894 seconds and 5 git commands to generate.