tree-defrag.c 3.3 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
/*
 * 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"
24
#include "locking.h"
25 26 27 28 29

int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
			struct btrfs_root *root, int cache_only)
{
	struct btrfs_path *path = NULL;
30
	struct btrfs_key key;
31 32 33 34 35
	int ret = 0;
	int wret;
	int level;
	int orig_level;
	int i;
36
	int is_extent = 0;
37
	int next_key_ret = 0;
38 39
	u64 last_ret = 0;

40 41
	if (root->fs_info->extent_root == root) {
		mutex_lock(&root->fs_info->alloc_mutex);
42
		is_extent = 1;
43
	}
44

45
	if (root->ref_cows == 0 && !is_extent)
46
		goto out;
47

48 49 50
	if (btrfs_test_opt(root, SSD))
		goto out;

51 52 53 54
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

55
	level = btrfs_header_level(root->node);
56
	orig_level = level;
57

58 59 60 61
	if (level == 0) {
		goto out;
	}
	if (root->defrag_progress.objectid == 0) {
62
		struct extent_buffer *root_node;
C
Chris Mason 已提交
63 64
		u32 nritems;

65 66
		root_node = btrfs_lock_root_node(root);
		nritems = btrfs_header_nritems(root_node);
C
Chris Mason 已提交
67 68
		root->defrag_max.objectid = 0;
		/* from above we know this is not a leaf */
69
		btrfs_node_key_to_cpu(root_node, &root->defrag_max,
C
Chris Mason 已提交
70
				      nritems - 1);
71 72 73
		btrfs_tree_unlock(root_node);
		free_extent_buffer(root_node);
		memset(&key, 0, sizeof(key));
74
	} else {
75
		memcpy(&key, &root->defrag_progress, sizeof(key));
76 77
	}

78 79 80
	path->lowest_level = 1;
	path->keep_locks = 1;
	wret = btrfs_search_slot(trans, root, &key, path, 0, 1);
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	if (wret < 0) {
		ret = wret;
		goto out;
	}
	if (!path->nodes[1]) {
		ret = 0;
		goto out;
	}
	path->slots[1] = btrfs_header_nritems(path->nodes[1]);
	next_key_ret = btrfs_find_next_key(root, path, &key, 1);
	ret = btrfs_realloc_node(trans, root,
				 path->nodes[1], 0,
				 cache_only, &last_ret,
				 &root->defrag_progress);
	WARN_ON(ret && ret != -EAGAIN);
	if (next_key_ret == 0) {
		memcpy(&root->defrag_progress, &key, sizeof(key));
		ret = -EAGAIN;
100
	}
101 102 103 104 105 106

	for (i = 1; i < BTRFS_MAX_LEVEL; i++) {
		if (path->locks[i]) {
			btrfs_tree_unlock(path->nodes[i]);
			path->locks[i] = 0;
		}
107
		if (path->nodes[i]) {
108
			free_extent_buffer(path->nodes[i]);
109
			path->nodes[i] = NULL;
110 111
		}
	}
112 113 114
	if (is_extent)
		btrfs_extent_post_op(trans, root);

115
out:
116 117 118
	if (is_extent)
		mutex_unlock(&root->fs_info->alloc_mutex);

119 120
	if (path)
		btrfs_free_path(path);
C
Chris Mason 已提交
121 122 123 124 125 126 127 128 129 130
	if (ret == -EAGAIN) {
		if (root->defrag_max.objectid > root->defrag_progress.objectid)
			goto done;
		if (root->defrag_max.type > root->defrag_progress.type)
			goto done;
		if (root->defrag_max.offset > root->defrag_progress.offset)
			goto done;
		ret = 0;
	}
done:
131 132 133 134 135 136
	if (ret != -EAGAIN) {
		memset(&root->defrag_progress, 0,
		       sizeof(root->defrag_progress));
	}
	return ret;
}