2007-08-08 04:15:09 +08:00
|
|
|
/*
|
|
|
|
* 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,
|
2007-10-16 04:14:19 +08:00
|
|
|
struct extent_buffer *node)
|
2007-08-08 04:15:09 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 nritems;
|
2007-10-16 04:15:53 +08:00
|
|
|
u64 bytenr;
|
2008-05-13 00:59:19 +08:00
|
|
|
u64 gen;
|
2007-10-16 04:15:53 +08:00
|
|
|
u32 blocksize;
|
2007-08-08 04:15:09 +08:00
|
|
|
int ret;
|
|
|
|
|
2007-10-16 04:15:53 +08:00
|
|
|
blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
|
2007-10-16 04:14:19 +08:00
|
|
|
nritems = btrfs_header_nritems(node);
|
2007-08-08 04:15:09 +08:00
|
|
|
for (i = 0; i < nritems; i++) {
|
2007-10-16 04:15:53 +08:00
|
|
|
bytenr = btrfs_node_blockptr(node, i);
|
2008-05-13 00:59:19 +08:00
|
|
|
gen = btrfs_node_ptr_generation(node, i);
|
|
|
|
ret = readahead_tree_block(root, bytenr, blocksize, gen);
|
2007-08-08 04:15:09 +08:00
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int defrag_walk_down(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_root *root,
|
|
|
|
struct btrfs_path *path, int *level,
|
2007-08-11 02:06:19 +08:00
|
|
|
int cache_only, u64 *last_ret)
|
2007-08-08 04:15:09 +08:00
|
|
|
{
|
2007-10-16 04:14:19 +08:00
|
|
|
struct extent_buffer *next;
|
|
|
|
struct extent_buffer *cur;
|
2007-10-16 04:15:53 +08:00
|
|
|
u64 bytenr;
|
2008-05-13 01:39:03 +08:00
|
|
|
u64 ptr_gen;
|
2007-08-08 04:15:09 +08:00
|
|
|
int ret = 0;
|
2007-08-11 02:06:19 +08:00
|
|
|
int is_extent = 0;
|
2007-08-08 04:15:09 +08:00
|
|
|
|
|
|
|
WARN_ON(*level < 0);
|
|
|
|
WARN_ON(*level >= BTRFS_MAX_LEVEL);
|
|
|
|
|
2007-08-11 02:06:19 +08:00
|
|
|
if (root->fs_info->extent_root == root)
|
|
|
|
is_extent = 1;
|
|
|
|
|
2007-11-06 23:26:29 +08:00
|
|
|
if (*level == 1 && cache_only && path->nodes[1] &&
|
|
|
|
!btrfs_buffer_defrag(path->nodes[1])) {
|
|
|
|
goto out;
|
|
|
|
}
|
2007-08-08 04:15:09 +08:00
|
|
|
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)
|
2007-10-16 04:14:19 +08:00
|
|
|
reada_defrag(root, cur);
|
2007-08-08 04:15:09 +08:00
|
|
|
|
2007-10-16 04:14:19 +08:00
|
|
|
if (btrfs_header_level(cur) != *level)
|
2007-08-08 04:15:09 +08:00
|
|
|
WARN_ON(1);
|
|
|
|
|
|
|
|
if (path->slots[*level] >=
|
2007-10-16 04:14:19 +08:00
|
|
|
btrfs_header_nritems(cur))
|
2007-08-08 04:15:09 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (*level == 1) {
|
2007-12-11 22:25:06 +08:00
|
|
|
WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
|
|
|
|
trans->transid);
|
2007-08-08 04:15:09 +08:00
|
|
|
ret = btrfs_realloc_node(trans, root,
|
|
|
|
path->nodes[*level],
|
2007-10-16 04:22:39 +08:00
|
|
|
path->slots[*level],
|
|
|
|
cache_only, last_ret,
|
|
|
|
&root->defrag_progress);
|
2007-08-11 02:06:19 +08:00
|
|
|
if (is_extent)
|
|
|
|
btrfs_extent_post_op(trans, root);
|
|
|
|
|
2007-08-08 04:15:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-10-16 04:15:53 +08:00
|
|
|
bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
|
2008-05-13 01:39:03 +08:00
|
|
|
ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
|
2007-08-08 04:15:09 +08:00
|
|
|
|
|
|
|
if (cache_only) {
|
2007-10-16 04:15:53 +08:00
|
|
|
next = btrfs_find_tree_block(root, bytenr,
|
|
|
|
btrfs_level_size(root, *level - 1));
|
2008-05-13 01:39:03 +08:00
|
|
|
if (!next || !btrfs_buffer_uptodate(next, ptr_gen) ||
|
2007-10-16 04:22:11 +08:00
|
|
|
!btrfs_buffer_defrag(next)) {
|
2007-10-16 04:14:19 +08:00
|
|
|
free_extent_buffer(next);
|
2007-08-08 04:15:09 +08:00
|
|
|
path->slots[*level]++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
2007-10-16 04:15:53 +08:00
|
|
|
next = read_tree_block(root, bytenr,
|
2008-05-13 00:59:19 +08:00
|
|
|
btrfs_level_size(root, *level - 1),
|
2008-05-13 01:39:03 +08:00
|
|
|
ptr_gen);
|
2007-08-08 04:15:09 +08:00
|
|
|
}
|
|
|
|
ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
|
|
|
|
path->slots[*level], &next);
|
|
|
|
BUG_ON(ret);
|
2007-08-11 02:06:19 +08:00
|
|
|
if (is_extent)
|
|
|
|
btrfs_extent_post_op(trans, root);
|
|
|
|
|
2007-08-08 04:15:09 +08:00
|
|
|
WARN_ON(*level <= 0);
|
|
|
|
if (path->nodes[*level-1])
|
2007-10-16 04:14:19 +08:00
|
|
|
free_extent_buffer(path->nodes[*level-1]);
|
2007-08-08 04:15:09 +08:00
|
|
|
path->nodes[*level-1] = next;
|
2007-10-16 04:14:19 +08:00
|
|
|
*level = btrfs_header_level(next);
|
2007-08-08 04:15:09 +08:00
|
|
|
path->slots[*level] = 0;
|
|
|
|
}
|
|
|
|
WARN_ON(*level < 0);
|
|
|
|
WARN_ON(*level >= BTRFS_MAX_LEVEL);
|
2007-10-16 04:17:34 +08:00
|
|
|
|
|
|
|
btrfs_clear_buffer_defrag(path->nodes[*level]);
|
2007-11-06 23:26:29 +08:00
|
|
|
out:
|
2007-10-16 04:14:19 +08:00
|
|
|
free_extent_buffer(path->nodes[*level]);
|
2007-08-08 04:15:09 +08:00
|
|
|
path->nodes[*level] = NULL;
|
|
|
|
*level += 1;
|
2007-10-16 04:22:39 +08:00
|
|
|
WARN_ON(ret && ret != -EAGAIN);
|
|
|
|
return ret;
|
2007-08-08 04:15:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-10-16 04:14:19 +08:00
|
|
|
struct extent_buffer *node;
|
2007-08-08 04:15:09 +08:00
|
|
|
|
|
|
|
for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
|
|
|
|
slot = path->slots[i];
|
2007-10-16 04:14:19 +08:00
|
|
|
if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
|
2007-08-08 04:15:09 +08:00
|
|
|
path->slots[i]++;
|
|
|
|
*level = i;
|
2007-10-16 04:14:19 +08:00
|
|
|
node = path->nodes[i];
|
2007-08-08 04:15:09 +08:00
|
|
|
WARN_ON(i == 0);
|
2007-10-16 04:14:19 +08:00
|
|
|
btrfs_node_key_to_cpu(node, &root->defrag_progress,
|
|
|
|
path->slots[i]);
|
2007-08-08 04:15:09 +08:00
|
|
|
root->defrag_level = i;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2007-10-16 04:17:34 +08:00
|
|
|
btrfs_clear_buffer_defrag(path->nodes[*level]);
|
2007-10-16 04:14:19 +08:00
|
|
|
free_extent_buffer(path->nodes[*level]);
|
2007-08-08 04:15:09 +08:00
|
|
|
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;
|
2007-10-16 04:14:19 +08:00
|
|
|
struct extent_buffer *tmp;
|
2007-08-08 04:15:09 +08:00
|
|
|
int ret = 0;
|
|
|
|
int wret;
|
|
|
|
int level;
|
|
|
|
int orig_level;
|
|
|
|
int i;
|
2007-08-11 02:06:19 +08:00
|
|
|
int is_extent = 0;
|
|
|
|
u64 last_ret = 0;
|
|
|
|
|
|
|
|
if (root->fs_info->extent_root == root)
|
|
|
|
is_extent = 1;
|
2007-08-08 04:15:09 +08:00
|
|
|
|
2007-08-11 02:06:19 +08:00
|
|
|
if (root->ref_cows == 0 && !is_extent)
|
2007-08-08 04:15:09 +08:00
|
|
|
goto out;
|
2007-10-16 04:14:19 +08:00
|
|
|
|
2008-02-16 02:19:35 +08:00
|
|
|
if (btrfs_test_opt(root, SSD))
|
|
|
|
goto out;
|
|
|
|
|
2007-08-08 04:15:09 +08:00
|
|
|
path = btrfs_alloc_path();
|
|
|
|
if (!path)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-10-16 04:14:19 +08:00
|
|
|
level = btrfs_header_level(root->node);
|
2007-08-08 04:15:09 +08:00
|
|
|
orig_level = level;
|
2007-10-16 04:19:46 +08:00
|
|
|
|
2007-08-08 04:15:09 +08:00
|
|
|
if (level == 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (root->defrag_progress.objectid == 0) {
|
2008-05-25 02:04:53 +08:00
|
|
|
u32 nritems;
|
|
|
|
|
|
|
|
nritems = btrfs_header_nritems(root->node);
|
|
|
|
root->defrag_max.objectid = 0;
|
|
|
|
/* from above we know this is not a leaf */
|
|
|
|
btrfs_node_key_to_cpu(root->node, &root->defrag_max,
|
|
|
|
nritems - 1);
|
2007-10-16 04:14:19 +08:00
|
|
|
extent_buffer_get(root->node);
|
2007-08-08 04:15:09 +08:00
|
|
|
ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
|
|
|
|
BUG_ON(ret);
|
|
|
|
path->nodes[level] = root->node;
|
|
|
|
path->slots[level] = 0;
|
2007-08-11 02:06:19 +08:00
|
|
|
if (is_extent)
|
|
|
|
btrfs_extent_post_op(trans, root);
|
2007-08-08 04:15:09 +08:00
|
|
|
} else {
|
|
|
|
level = root->defrag_level;
|
|
|
|
path->lowest_level = level;
|
|
|
|
wret = btrfs_search_slot(trans, root, &root->defrag_progress,
|
|
|
|
path, 0, 1);
|
|
|
|
|
2007-08-11 02:06:19 +08:00
|
|
|
if (is_extent)
|
|
|
|
btrfs_extent_post_op(trans, root);
|
2007-10-16 04:14:19 +08:00
|
|
|
|
2007-08-08 04:15:09 +08:00
|
|
|
if (wret < 0) {
|
|
|
|
ret = wret;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-10-16 04:14:19 +08:00
|
|
|
|
2007-08-08 04:15:09 +08:00
|
|
|
while(level > 0 && !path->nodes[level])
|
|
|
|
level--;
|
2007-10-16 04:14:19 +08:00
|
|
|
|
2007-08-08 04:15:09 +08:00
|
|
|
if (!path->nodes[level]) {
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
2007-08-11 02:06:19 +08:00
|
|
|
wret = defrag_walk_down(trans, root, path, &level, cache_only,
|
|
|
|
&last_ret);
|
2007-08-08 04:15:09 +08:00
|
|
|
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;
|
2007-11-06 23:26:24 +08:00
|
|
|
else
|
|
|
|
ret = -EAGAIN;
|
2007-08-09 08:17:12 +08:00
|
|
|
break;
|
2007-08-08 04:15:09 +08:00
|
|
|
}
|
|
|
|
for (i = 0; i <= orig_level; i++) {
|
|
|
|
if (path->nodes[i]) {
|
2007-10-16 04:14:19 +08:00
|
|
|
free_extent_buffer(path->nodes[i]);
|
2007-10-16 04:18:56 +08:00
|
|
|
path->nodes[i] = NULL;
|
2007-08-08 04:15:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (path)
|
|
|
|
btrfs_free_path(path);
|
2008-05-25 02:04:53 +08:00
|
|
|
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:
|
2007-08-08 04:15:09 +08:00
|
|
|
if (ret != -EAGAIN) {
|
|
|
|
memset(&root->defrag_progress, 0,
|
|
|
|
sizeof(root->defrag_progress));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|