tree-defrag.c 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <linux/sched.h>
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"

static void reada_defrag(struct btrfs_root *root,
26
			 struct extent_buffer *node)
27 28 29
{
	int i;
	u32 nritems;
30 31
	u64 bytenr;
	u32 blocksize;
32 33
	int ret;

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 38
		bytenr = btrfs_node_blockptr(node, i);
		ret = readahead_tree_block(root, bytenr, blocksize);
39 40 41 42 43 44 45 46
		if (ret)
			break;
	}
}

static int defrag_walk_down(struct btrfs_trans_handle *trans,
			    struct btrfs_root *root,
			    struct btrfs_path *path, int *level,
47
			    int cache_only, u64 *last_ret)
48
{
49 50
	struct extent_buffer *next;
	struct extent_buffer *cur;
51
	u64 bytenr;
52
	int ret = 0;
53
	int is_extent = 0;
54 55 56 57

	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);

58 59 60
	if (root->fs_info->extent_root == root)
		is_extent = 1;

61 62 63 64
	if (*level == 1 && cache_only && path->nodes[1] &&
	    !btrfs_buffer_defrag(path->nodes[1])) {
		goto out;
	}
65 66 67 68 69 70
	while(*level > 0) {
		WARN_ON(*level < 0);
		WARN_ON(*level >= BTRFS_MAX_LEVEL);
		cur = path->nodes[*level];

		if (!cache_only && *level > 1 && path->slots[*level] == 0)
71
			reada_defrag(root, cur);
72

73
		if (btrfs_header_level(cur) != *level)
74 75 76
			WARN_ON(1);

		if (path->slots[*level] >=
77
		    btrfs_header_nritems(cur))
78 79 80
			break;

		if (*level == 1) {
81 82
			WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
							trans->transid);
83 84
			ret = btrfs_realloc_node(trans, root,
						 path->nodes[*level],
85 86 87
						 path->slots[*level],
						 cache_only, last_ret,
						 &root->defrag_progress);
88 89 90
			if (is_extent)
				btrfs_extent_post_op(trans, root);

91 92
			break;
		}
93
		bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
94 95

		if (cache_only) {
96 97
			next = btrfs_find_tree_block(root, bytenr,
					   btrfs_level_size(root, *level - 1));
98 99
			if (!next || !btrfs_buffer_uptodate(next) ||
			    !btrfs_buffer_defrag(next)) {
100
				free_extent_buffer(next);
101 102 103
				path->slots[*level]++;
				continue;
			}
104
			btrfs_verify_block_csum(root, next);
105
		} else {
106 107
			next = read_tree_block(root, bytenr,
				       btrfs_level_size(root, *level - 1));
108 109 110 111
		}
		ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
				      path->slots[*level], &next);
		BUG_ON(ret);
112 113 114
		if (is_extent)
			btrfs_extent_post_op(trans, root);

115 116
		WARN_ON(*level <= 0);
		if (path->nodes[*level-1])
117
			free_extent_buffer(path->nodes[*level-1]);
118
		path->nodes[*level-1] = next;
119
		*level = btrfs_header_level(next);
120 121 122 123
		path->slots[*level] = 0;
	}
	WARN_ON(*level < 0);
	WARN_ON(*level >= BTRFS_MAX_LEVEL);
124 125

	btrfs_clear_buffer_defrag(path->nodes[*level]);
126
out:
127
	free_extent_buffer(path->nodes[*level]);
128 129
	path->nodes[*level] = NULL;
	*level += 1;
130 131
	WARN_ON(ret && ret != -EAGAIN);
	return ret;
132 133 134 135 136 137 138 139 140
}

static int defrag_walk_up(struct btrfs_trans_handle *trans,
			  struct btrfs_root *root,
			  struct btrfs_path *path, int *level,
			  int cache_only)
{
	int i;
	int slot;
141
	struct extent_buffer *node;
142 143 144

	for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
		slot = path->slots[i];
145
		if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
146 147
			path->slots[i]++;
			*level = i;
148
			node = path->nodes[i];
149
			WARN_ON(i == 0);
150 151
			btrfs_node_key_to_cpu(node, &root->defrag_progress,
					      path->slots[i]);
152 153 154
			root->defrag_level = i;
			return 0;
		} else {
155
			btrfs_clear_buffer_defrag(path->nodes[*level]);
156
			free_extent_buffer(path->nodes[*level]);
157 158 159 160 161 162 163 164 165 166 167
			path->nodes[*level] = NULL;
			*level = i + 1;
		}
	}
	return 1;
}

int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
			struct btrfs_root *root, int cache_only)
{
	struct btrfs_path *path = NULL;
168
	struct extent_buffer *tmp;
169 170 171 172 173
	int ret = 0;
	int wret;
	int level;
	int orig_level;
	int i;
174 175 176 177 178
	int is_extent = 0;
	u64 last_ret = 0;

	if (root->fs_info->extent_root == root)
		is_extent = 1;
179

180
	if (root->ref_cows == 0 && !is_extent)
181
		goto out;
182

183 184 185
	if (btrfs_test_opt(root, SSD))
		goto out;

186 187 188 189
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

190
	level = btrfs_header_level(root->node);
191
	orig_level = level;
192

193 194 195 196
	if (level == 0) {
		goto out;
	}
	if (root->defrag_progress.objectid == 0) {
197
		extent_buffer_get(root->node);
198 199 200 201
		ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
		BUG_ON(ret);
		path->nodes[level] = root->node;
		path->slots[level] = 0;
202 203
		if (is_extent)
			btrfs_extent_post_op(trans, root);
204 205 206 207 208 209
	} else {
		level = root->defrag_level;
		path->lowest_level = level;
		wret = btrfs_search_slot(trans, root, &root->defrag_progress,
					 path, 0, 1);

210 211
		if (is_extent)
			btrfs_extent_post_op(trans, root);
212

213 214 215 216
		if (wret < 0) {
			ret = wret;
			goto out;
		}
217

218 219
		while(level > 0 && !path->nodes[level])
			level--;
220

221 222 223 224 225 226 227
		if (!path->nodes[level]) {
			ret = 0;
			goto out;
		}
	}

	while(1) {
228 229
		wret = defrag_walk_down(trans, root, path, &level, cache_only,
					&last_ret);
230 231 232 233 234 235 236 237 238 239
		if (wret > 0)
			break;
		if (wret < 0)
			ret = wret;

		wret = defrag_walk_up(trans, root, path, &level, cache_only);
		if (wret > 0)
			break;
		if (wret < 0)
			ret = wret;
240 241
		else
			ret = -EAGAIN;
242
		break;
243 244 245
	}
	for (i = 0; i <= orig_level; i++) {
		if (path->nodes[i]) {
246
			free_extent_buffer(path->nodes[i]);
247
			path->nodes[i] = NULL;
248 249 250 251 252 253 254 255 256 257 258
		}
	}
out:
	if (path)
		btrfs_free_path(path);
	if (ret != -EAGAIN) {
		memset(&root->defrag_progress, 0,
		       sizeof(root->defrag_progress));
	}
	return ret;
}