Btrfs: Disable tree defrag in SSD mode
[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 bytenr;
31         u32 blocksize;
32         int ret;
33
34         blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
35         nritems = btrfs_header_nritems(node);
36         for (i = 0; i < nritems; i++) {
37                 bytenr = btrfs_node_blockptr(node, i);
38                 ret = readahead_tree_block(root, bytenr, blocksize);
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,
47                             int cache_only, u64 *last_ret)
48 {
49         struct extent_buffer *next;
50         struct extent_buffer *cur;
51         u64 bytenr;
52         int ret = 0;
53         int is_extent = 0;
54
55         WARN_ON(*level < 0);
56         WARN_ON(*level >= BTRFS_MAX_LEVEL);
57
58         if (root->fs_info->extent_root == root)
59                 is_extent = 1;
60
61         if (*level == 1 && cache_only && path->nodes[1] &&
62             !btrfs_buffer_defrag(path->nodes[1])) {
63                 goto out;
64         }
65         while(*level > 0) {
66                 WARN_ON(*level < 0);
67                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
68                 cur = path->nodes[*level];
69
70                 if (!cache_only && *level > 1 && path->slots[*level] == 0)
71                         reada_defrag(root, cur);
72
73                 if (btrfs_header_level(cur) != *level)
74                         WARN_ON(1);
75
76                 if (path->slots[*level] >=
77                     btrfs_header_nritems(cur))
78                         break;
79
80                 if (*level == 1) {
81                         WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
82                                                         trans->transid);
83                         ret = btrfs_realloc_node(trans, root,
84                                                  path->nodes[*level],
85                                                  path->slots[*level],
86                                                  cache_only, last_ret,
87                                                  &root->defrag_progress);
88                         if (is_extent)
89                                 btrfs_extent_post_op(trans, root);
90
91                         break;
92                 }
93                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
94
95                 if (cache_only) {
96                         next = btrfs_find_tree_block(root, bytenr,
97                                            btrfs_level_size(root, *level - 1));
98                         if (!next || !btrfs_buffer_uptodate(next) ||
99                             !btrfs_buffer_defrag(next)) {
100                                 free_extent_buffer(next);
101                                 path->slots[*level]++;
102                                 continue;
103                         }
104                 } else {
105                         next = read_tree_block(root, bytenr,
106                                        btrfs_level_size(root, *level - 1));
107                 }
108                 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
109                                       path->slots[*level], &next);
110                 BUG_ON(ret);
111                 if (is_extent)
112                         btrfs_extent_post_op(trans, root);
113
114                 WARN_ON(*level <= 0);
115                 if (path->nodes[*level-1])
116                         free_extent_buffer(path->nodes[*level-1]);
117                 path->nodes[*level-1] = next;
118                 *level = btrfs_header_level(next);
119                 path->slots[*level] = 0;
120         }
121         WARN_ON(*level < 0);
122         WARN_ON(*level >= BTRFS_MAX_LEVEL);
123
124         btrfs_clear_buffer_defrag(path->nodes[*level]);
125 out:
126         free_extent_buffer(path->nodes[*level]);
127         path->nodes[*level] = NULL;
128         *level += 1;
129         WARN_ON(ret && ret != -EAGAIN);
130         return ret;
131 }
132
133 static int defrag_walk_up(struct btrfs_trans_handle *trans,
134                           struct btrfs_root *root,
135                           struct btrfs_path *path, int *level,
136                           int cache_only)
137 {
138         int i;
139         int slot;
140         struct extent_buffer *node;
141
142         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
143                 slot = path->slots[i];
144                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
145                         path->slots[i]++;
146                         *level = i;
147                         node = path->nodes[i];
148                         WARN_ON(i == 0);
149                         btrfs_node_key_to_cpu(node, &root->defrag_progress,
150                                               path->slots[i]);
151                         root->defrag_level = i;
152                         return 0;
153                 } else {
154                         btrfs_clear_buffer_defrag(path->nodes[*level]);
155                         free_extent_buffer(path->nodes[*level]);
156                         path->nodes[*level] = NULL;
157                         *level = i + 1;
158                 }
159         }
160         return 1;
161 }
162
163 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
164                         struct btrfs_root *root, int cache_only)
165 {
166         struct btrfs_path *path = NULL;
167         struct extent_buffer *tmp;
168         int ret = 0;
169         int wret;
170         int level;
171         int orig_level;
172         int i;
173         int is_extent = 0;
174         u64 last_ret = 0;
175
176         if (root->fs_info->extent_root == root)
177                 is_extent = 1;
178
179         if (root->ref_cows == 0 && !is_extent)
180                 goto out;
181
182         if (btrfs_test_opt(root, SSD))
183                 goto out;
184
185         path = btrfs_alloc_path();
186         if (!path)
187                 return -ENOMEM;
188
189         level = btrfs_header_level(root->node);
190         orig_level = level;
191
192         if (level == 0) {
193                 goto out;
194         }
195         if (root->defrag_progress.objectid == 0) {
196                 extent_buffer_get(root->node);
197                 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
198                 BUG_ON(ret);
199                 path->nodes[level] = root->node;
200                 path->slots[level] = 0;
201                 if (is_extent)
202                         btrfs_extent_post_op(trans, root);
203         } else {
204                 level = root->defrag_level;
205                 path->lowest_level = level;
206                 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
207                                          path, 0, 1);
208
209                 if (is_extent)
210                         btrfs_extent_post_op(trans, root);
211
212                 if (wret < 0) {
213                         ret = wret;
214                         goto out;
215                 }
216
217                 while(level > 0 && !path->nodes[level])
218                         level--;
219
220                 if (!path->nodes[level]) {
221                         ret = 0;
222                         goto out;
223                 }
224         }
225
226         while(1) {
227                 wret = defrag_walk_down(trans, root, path, &level, cache_only,
228                                         &last_ret);
229                 if (wret > 0)
230                         break;
231                 if (wret < 0)
232                         ret = wret;
233
234                 wret = defrag_walk_up(trans, root, path, &level, cache_only);
235                 if (wret > 0)
236                         break;
237                 if (wret < 0)
238                         ret = wret;
239                 else
240                         ret = -EAGAIN;
241                 break;
242         }
243         for (i = 0; i <= orig_level; i++) {
244                 if (path->nodes[i]) {
245                         free_extent_buffer(path->nodes[i]);
246                         path->nodes[i] = NULL;
247                 }
248         }
249 out:
250         if (path)
251                 btrfs_free_path(path);
252         if (ret != -EAGAIN) {
253                 memset(&root->defrag_progress, 0,
254                        sizeof(root->defrag_progress));
255         }
256         return ret;
257 }