]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/btrfs/tree-defrag.c
Btrfs: Create extent_buffer interface for large blocksizes
[mirror_ubuntu-bionic-kernel.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,
5f39d397 26 struct extent_buffer *node)
6702ed49
CM
27{
28 int i;
29 u32 nritems;
30 u64 blocknr;
31 int ret;
32
5f39d397 33 nritems = btrfs_header_nritems(node);
6702ed49
CM
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 46{
5f39d397
CM
47 struct extent_buffer *next;
48 struct extent_buffer *cur;
6702ed49
CM
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)
5f39d397 65 reada_defrag(root, cur);
6702ed49 66
5f39d397 67 if (btrfs_header_level(cur) != *level)
6702ed49
CM
68 WARN_ON(1);
69
70 if (path->slots[*level] >=
5f39d397 71 btrfs_header_nritems(cur))
6702ed49
CM
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 }
5f39d397 83 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
6702ed49
CM
84
85 if (cache_only) {
86 next = btrfs_find_tree_block(root, blocknr);
5f39d397
CM
87 /* FIXME, test for defrag */
88 if (!next || !btrfs_buffer_uptodate(next)) {
89 free_extent_buffer(next);
6702ed49
CM
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);
e9d0b13b
CM
99 ret = btrfs_realloc_node(trans, root, next, cache_only,
100 last_ret);
6702ed49 101 BUG_ON(ret);
e9d0b13b
CM
102
103 if (is_extent)
104 btrfs_extent_post_op(trans, root);
105
6702ed49
CM
106 WARN_ON(*level <= 0);
107 if (path->nodes[*level-1])
5f39d397 108 free_extent_buffer(path->nodes[*level-1]);
6702ed49 109 path->nodes[*level-1] = next;
5f39d397 110 *level = btrfs_header_level(next);
6702ed49
CM
111 path->slots[*level] = 0;
112 }
113 WARN_ON(*level < 0);
114 WARN_ON(*level >= BTRFS_MAX_LEVEL);
5f39d397 115#if 0
86479a04
CM
116 clear_buffer_defrag(path->nodes[*level]);
117 clear_buffer_defrag_done(path->nodes[*level]);
5f39d397
CM
118#endif
119 free_extent_buffer(path->nodes[*level]);
6702ed49
CM
120 path->nodes[*level] = NULL;
121 *level += 1;
122 WARN_ON(ret);
123 return 0;
124}
125
126static 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;
5f39d397 133 struct extent_buffer *node;
6702ed49
CM
134
135 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
136 slot = path->slots[i];
5f39d397 137 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
6702ed49
CM
138 path->slots[i]++;
139 *level = i;
5f39d397 140 node = path->nodes[i];
6702ed49 141 WARN_ON(i == 0);
5f39d397
CM
142 btrfs_node_key_to_cpu(node, &root->defrag_progress,
143 path->slots[i]);
6702ed49
CM
144 root->defrag_level = i;
145 return 0;
146 } else {
5f39d397 147 /*
f2183bde 148 clear_buffer_defrag(path->nodes[*level]);
86479a04 149 clear_buffer_defrag_done(path->nodes[*level]);
5f39d397
CM
150 */
151 free_extent_buffer(path->nodes[*level]);
6702ed49
CM
152 path->nodes[*level] = NULL;
153 *level = i + 1;
154 }
155 }
156 return 1;
157}
158
159int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
160 struct btrfs_root *root, int cache_only)
161{
162 struct btrfs_path *path = NULL;
5f39d397 163 struct extent_buffer *tmp;
6702ed49
CM
164 int ret = 0;
165 int wret;
166 int level;
167 int orig_level;
168 int i;
e9d0b13b
CM
169 int is_extent = 0;
170 u64 last_ret = 0;
171
172 if (root->fs_info->extent_root == root)
173 is_extent = 1;
6702ed49 174
e9d0b13b 175 if (root->ref_cows == 0 && !is_extent)
6702ed49 176 goto out;
5f39d397 177
6702ed49
CM
178 path = btrfs_alloc_path();
179 if (!path)
180 return -ENOMEM;
181
5f39d397 182 level = btrfs_header_level(root->node);
6702ed49
CM
183 orig_level = level;
184 if (level == 0) {
185 goto out;
186 }
187 if (root->defrag_progress.objectid == 0) {
5f39d397 188 extent_buffer_get(root->node);
6702ed49
CM
189 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
190 BUG_ON(ret);
e9d0b13b
CM
191 ret = btrfs_realloc_node(trans, root, root->node, cache_only,
192 &last_ret);
6702ed49
CM
193 BUG_ON(ret);
194 path->nodes[level] = root->node;
195 path->slots[level] = 0;
e9d0b13b
CM
196 if (is_extent)
197 btrfs_extent_post_op(trans, root);
6702ed49
CM
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
e9d0b13b
CM
204 if (is_extent)
205 btrfs_extent_post_op(trans, root);
5f39d397 206
6702ed49
CM
207 if (wret < 0) {
208 ret = wret;
209 goto out;
210 }
5f39d397 211
6702ed49
CM
212 while(level > 0 && !path->nodes[level])
213 level--;
5f39d397 214
6702ed49
CM
215 if (!path->nodes[level]) {
216 ret = 0;
217 goto out;
218 }
219 }
220
221 while(1) {
e9d0b13b
CM
222 wret = defrag_walk_down(trans, root, path, &level, cache_only,
223 &last_ret);
6702ed49
CM
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;
409eb95d
CM
234 ret = -EAGAIN;
235 break;
6702ed49
CM
236 }
237 for (i = 0; i <= orig_level; i++) {
238 if (path->nodes[i]) {
5f39d397 239 free_extent_buffer(path->nodes[i]);
6702ed49
CM
240 path->nodes[i] = 0;
241 }
242 }
243out:
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}