Btrfs: Create extent_buffer interface for large blocksizes
[safe/jmp/linux-2.6] / fs / btrfs / tree-defrag.c
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 extent_buffer *node)
27 {
28         int i;
29         u32 nritems;
30         u64 blocknr;
31         int ret;
32
33         nritems = btrfs_header_nritems(node);
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,
45                             int cache_only, u64 *last_ret)
46 {
47         struct extent_buffer *next;
48         struct extent_buffer *cur;
49         u64 blocknr;
50         int ret = 0;
51         int is_extent = 0;
52
53         WARN_ON(*level < 0);
54         WARN_ON(*level >= BTRFS_MAX_LEVEL);
55
56         if (root->fs_info->extent_root == root)
57                 is_extent = 1;
58
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, cur);
66
67                 if (btrfs_header_level(cur) != *level)
68                         WARN_ON(1);
69
70                 if (path->slots[*level] >=
71                     btrfs_header_nritems(cur))
72                         break;
73
74                 if (*level == 1) {
75                         ret = btrfs_realloc_node(trans, root,
76                                                  path->nodes[*level],
77                                                  cache_only, last_ret);
78                         if (is_extent)
79                                 btrfs_extent_post_op(trans, root);
80
81                         break;
82                 }
83                 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
84
85                 if (cache_only) {
86                         next = btrfs_find_tree_block(root, blocknr);
87                         /* FIXME, test for defrag */
88                         if (!next || !btrfs_buffer_uptodate(next)) {
89                                 free_extent_buffer(next);
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);
99                 ret = btrfs_realloc_node(trans, root, next, cache_only,
100                                          last_ret);
101                 BUG_ON(ret);
102
103                 if (is_extent)
104                         btrfs_extent_post_op(trans, root);
105
106                 WARN_ON(*level <= 0);
107                 if (path->nodes[*level-1])
108                         free_extent_buffer(path->nodes[*level-1]);
109                 path->nodes[*level-1] = next;
110                 *level = btrfs_header_level(next);
111                 path->slots[*level] = 0;
112         }
113         WARN_ON(*level < 0);
114         WARN_ON(*level >= BTRFS_MAX_LEVEL);
115 #if 0
116         clear_buffer_defrag(path->nodes[*level]);
117         clear_buffer_defrag_done(path->nodes[*level]);
118 #endif
119         free_extent_buffer(path->nodes[*level]);
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;
133         struct extent_buffer *node;
134
135         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
136                 slot = path->slots[i];
137                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
138                         path->slots[i]++;
139                         *level = i;
140                         node = path->nodes[i];
141                         WARN_ON(i == 0);
142                         btrfs_node_key_to_cpu(node, &root->defrag_progress,
143                                               path->slots[i]);
144                         root->defrag_level = i;
145                         return 0;
146                 } else {
147                         /*
148                         clear_buffer_defrag(path->nodes[*level]);
149                         clear_buffer_defrag_done(path->nodes[*level]);
150                         */
151                         free_extent_buffer(path->nodes[*level]);
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;
163         struct extent_buffer *tmp;
164         int ret = 0;
165         int wret;
166         int level;
167         int orig_level;
168         int i;
169         int is_extent = 0;
170         u64 last_ret = 0;
171
172         if (root->fs_info->extent_root == root)
173                 is_extent = 1;
174
175         if (root->ref_cows == 0 && !is_extent)
176                 goto out;
177
178         path = btrfs_alloc_path();
179         if (!path)
180                 return -ENOMEM;
181
182         level = btrfs_header_level(root->node);
183         orig_level = level;
184         if (level == 0) {
185                 goto out;
186         }
187         if (root->defrag_progress.objectid == 0) {
188                 extent_buffer_get(root->node);
189                 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
190                 BUG_ON(ret);
191                 ret = btrfs_realloc_node(trans, root, root->node, cache_only,
192                                          &last_ret);
193                 BUG_ON(ret);
194                 path->nodes[level] = root->node;
195                 path->slots[level] = 0;
196                 if (is_extent)
197                         btrfs_extent_post_op(trans, root);
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
204                 if (is_extent)
205                         btrfs_extent_post_op(trans, root);
206
207                 if (wret < 0) {
208                         ret = wret;
209                         goto out;
210                 }
211
212                 while(level > 0 && !path->nodes[level])
213                         level--;
214
215                 if (!path->nodes[level]) {
216                         ret = 0;
217                         goto out;
218                 }
219         }
220
221         while(1) {
222                 wret = defrag_walk_down(trans, root, path, &level, cache_only,
223                                         &last_ret);
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;
234                 ret = -EAGAIN;
235                 break;
236         }
237         for (i = 0; i <= orig_level; i++) {
238                 if (path->nodes[i]) {
239                         free_extent_buffer(path->nodes[i]);
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 }