btrfs_root *extent_root);
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
btrfs_root *extent_root);
+static int find_previous_extent(struct btrfs_root *root,
+ struct btrfs_path *path)
+{
+ struct btrfs_key found_key;
+ struct extent_buffer *leaf;
+ int ret;
+
+ while(1) {
+ if (path->slots[0] == 0) {
+ ret = btrfs_prev_leaf(root, path);
+ if (ret != 0)
+ return ret;
+ } else {
+ path->slots[0]--;
+ }
+ leaf = path->nodes[0];
+ btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+ if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
+ return 0;
+ }
+ return 1;
+}
static int cache_block_group(struct btrfs_root *root,
struct btrfs_block_group_cache *block_group)
int ret;
struct btrfs_key key;
struct extent_buffer *leaf;
- struct extent_map_tree *free_space_cache;
+ struct extent_io_tree *free_space_cache;
int slot;
u64 last = 0;
u64 hole_size;
first_free = block_group->key.objectid;
key.objectid = block_group->key.objectid;
key.offset = 0;
-
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
-
if (ret < 0)
return ret;
-
- if (ret && path->slots[0] > 0)
- path->slots[0]--;
-
+ ret = find_previous_extent(root, path);
+ if (ret < 0)
+ return ret;
+ if (ret == 0) {
+ leaf = path->nodes[0];
+ btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+ if (key.objectid + key.offset > first_free)
+ first_free = key.objectid + key.offset;
+ }
while(1) {
leaf = path->nodes[0];
slot = path->slots[0];
break;
}
}
-
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.objectid < block_group->key.objectid) {
- if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
- key.objectid + key.offset > first_free)
- first_free = key.objectid + key.offset;
goto next;
}
-
if (key.objectid >= block_group->key.objectid +
block_group->key.offset) {
break;
btrfs_fs_info *info,
u64 bytenr)
{
- struct extent_map_tree *block_group_cache;
+ struct extent_io_tree *block_group_cache;
struct btrfs_block_group_cache *block_group = NULL;
u64 ptr;
u64 start;
return block_group;
return NULL;
}
-static u64 find_search_start(struct btrfs_root *root,
- struct btrfs_block_group_cache **cache_ret,
- u64 search_start, int num,
- int data, int full_scan)
+static u64 noinline find_search_start(struct btrfs_root *root,
+ struct btrfs_block_group_cache **cache_ret,
+ u64 search_start, int num, int data)
{
int ret;
struct btrfs_block_group_cache *cache = *cache_ret;
u64 start = 0;
u64 end = 0;
u64 cache_miss = 0;
+ u64 total_fs_bytes;
int wrapped = 0;
if (!cache) {
goto out;
}
+ total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
again:
ret = cache_block_group(root, cache);
if (ret)
if (data != BTRFS_BLOCK_GROUP_MIXED &&
start + num > cache->key.objectid + cache->key.offset)
goto new_group;
+ if (start + num > total_fs_bytes)
+ goto new_group;
return start;
}
out:
last = cache->key.objectid + cache->key.offset;
wrapped:
cache = btrfs_lookup_block_group(root->fs_info, last);
- if (!cache) {
+ if (!cache || cache->key.objectid >= total_fs_bytes) {
no_cache:
if (!wrapped) {
wrapped = 1;
int data, int owner)
{
struct btrfs_block_group_cache *cache;
- struct extent_map_tree *block_group_cache;
+ struct extent_io_tree *block_group_cache;
struct btrfs_block_group_cache *found_group = NULL;
struct btrfs_fs_info *info = root->fs_info;
u64 used;
u64 end;
u64 free_check;
u64 ptr;
+ u64 total_fs_bytes;
int bit;
int ret;
int full_search = 0;
int data_swap = 0;
block_group_cache = &info->block_group_cache;
+ total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
if (!owner)
factor = 8;
else
bit = BLOCK_GROUP_METADATA;
- if (search_start) {
+ if (search_start && search_start < total_fs_bytes) {
struct btrfs_block_group_cache *shint;
shint = btrfs_lookup_block_group(info, search_start);
if (shint && (shint->data == data ||
}
}
}
- if (hint && (hint->data == data ||
- hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
+ if (hint && hint->key.objectid < total_fs_bytes &&
+ (hint->data == data || hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
used = btrfs_block_group_used(&hint->item);
if (used + hint->pinned <
div_factor(hint->key.offset, factor)) {
else
hint_last = search_start;
+ if (hint_last >= total_fs_bytes)
+ hint_last = search_start;
last = hint_last;
}
again:
last = cache->key.objectid + cache->key.offset;
used = btrfs_block_group_used(&cache->item);
+ if (cache->key.objectid > total_fs_bytes)
+ break;
+
if (full_search)
free_check = cache->key.offset;
else
return ret == 0;
}
-static int lookup_extent_backref(struct btrfs_trans_handle *trans,
- struct btrfs_root *root,
- struct btrfs_path *path, u64 bytenr,
- u64 root_objectid, u64 ref_generation,
- u64 owner, u64 owner_offset, int del)
+static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct btrfs_path *path, u64 bytenr,
+ u64 root_objectid,
+ u64 ref_generation, u64 owner,
+ u64 owner_offset, int del)
{
u64 hash;
struct btrfs_key key;
if (!path)
return -ENOMEM;
+ path->reada = 0;
key.objectid = bytenr;
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
key.offset = num_bytes;
btrfs_release_path(root->fs_info->extent_root, path);
+ path->reada = 0;
ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
path, bytenr, root_objectid,
ref_generation, owner, owner_offset);
WARN_ON(num_bytes < root->sectorsize);
path = btrfs_alloc_path();
+ path->reada = 0;
key.objectid = bytenr;
key.offset = num_bytes;
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
struct btrfs_path *path;
u64 bytenr;
u64 found_objectid;
- u64 root_objectid = 0;
+ u64 root_objectid = root->root_key.objectid;
u32 total_count = 0;
u32 cur_count;
- u32 refs;
u32 nritems;
int ret;
struct btrfs_key key;
}
item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
- refs = btrfs_extent_refs(l, item);
while (1) {
+ l = path->nodes[0];
nritems = btrfs_header_nritems(l);
if (path->slots[0] >= nritems) {
ret = btrfs_next_leaf(extent_root, path);
btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
if (found_key.objectid != bytenr)
break;
+
if (found_key.type != BTRFS_EXTENT_REF_KEY) {
path->slots[0]++;
continue;
struct btrfs_extent_ref);
found_objectid = btrfs_ref_root(l, ref_item);
- if (found_objectid != root_objectid)
- total_count++;
-
- if (total_count > 1)
+ if (found_objectid != root_objectid) {
+ total_count = 2;
goto out;
-
- if (root_objectid == 0)
- root_objectid = found_objectid;
-
+ }
+ total_count = 1;
path->slots[0]++;
}
if (cur_count == 0) {
total_count = 0;
goto out;
}
- if (total_count > 1)
- goto out;
if (level >= 0 && root->node == count_path->nodes[level])
goto out;
level++;
out:
btrfs_free_path(path);
return total_count;
-
}
-
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 owner_objectid)
{
int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
struct btrfs_root *root)
{
- struct extent_map_tree *block_group_cache;
+ struct extent_io_tree *block_group_cache;
struct btrfs_block_group_cache *cache;
int ret;
int err = 0;
return 0;
}
-int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
+int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
{
u64 last = 0;
u64 start;
u64 end;
- struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
+ struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
int ret;
while(1) {
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
- struct extent_map_tree *unpin)
+ struct extent_io_tree *unpin)
{
u64 start;
u64 end;
int ret;
- struct extent_map_tree *free_space_cache;
+ struct extent_io_tree *free_space_cache;
free_space_cache = &root->fs_info->free_space_cache;
while(1) {
return 0;
}
-static int finish_current_insert(struct btrfs_trans_handle *trans, struct
- btrfs_root *extent_root)
+static int finish_current_insert(struct btrfs_trans_handle *trans,
+ struct btrfs_root *extent_root)
{
u64 start;
u64 end;
if (btrfs_buffer_uptodate(buf)) {
u64 transid =
root->fs_info->running_transaction->transid;
- if (btrfs_header_generation(buf) == transid) {
+ u64 header_transid =
+ btrfs_header_generation(buf);
+ if (header_transid == transid) {
+ clean_tree_block(NULL, root, buf);
free_extent_buffer(buf);
return 1;
}
key.objectid = bytenr;
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
key.offset = num_bytes;
-
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
+ path->reada = 0;
ret = lookup_extent_backref(trans, extent_root, path,
bytenr, root_objectid,
ref_generation,
int err = 0;
u64 start;
u64 end;
- struct extent_map_tree *pending_del;
- struct extent_map_tree *pinned_extents;
+ struct extent_io_tree *pending_del;
+ struct extent_io_tree *pinned_extents;
pending_del = &extent_root->fs_info->pending_del;
pinned_extents = &extent_root->fs_info->pinned_extents;
* ins->offset == number of blocks
* Any available blocks before search_start are skipped.
*/
-static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
- *orig_root, u64 num_bytes, u64 empty_size,
- u64 search_start, u64 search_end, u64 hint_byte,
- struct btrfs_key *ins, u64 exclude_start,
- u64 exclude_nr, int data)
+static int noinline find_free_extent(struct btrfs_trans_handle *trans,
+ struct btrfs_root *orig_root,
+ u64 num_bytes, u64 empty_size,
+ u64 search_start, u64 search_end,
+ u64 hint_byte, struct btrfs_key *ins,
+ u64 exclude_start, u64 exclude_nr,
+ int data)
{
struct btrfs_path *path;
struct btrfs_key key;
data = BTRFS_BLOCK_GROUP_MIXED;
}
- if (search_end == (u64)-1)
- search_end = btrfs_super_total_bytes(&info->super_copy);
+ /* for SSD, cluster allocations together as much as possible */
+ if (btrfs_test_opt(root, SSD)) {
+ if (!data) {
+ if (root->fs_info->last_alloc)
+ hint_byte = root->fs_info->last_alloc;
+ else {
+ hint_byte = hint_byte &
+ ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
+ empty_size += 16 * 1024 * 1024;
+ }
+ }
+ }
+
+ search_end = min(search_end,
+ btrfs_super_total_bytes(&info->super_copy));
if (hint_byte) {
block_group = btrfs_lookup_block_group(info, hint_byte);
if (!block_group)
orig_search_start);
}
search_start = find_search_start(root, &block_group, search_start,
- total_needed, data, full_scan);
+ total_needed, data);
+
+ if (!data && btrfs_test_opt(root, SSD) && info->last_alloc &&
+ search_start != info->last_alloc) {
+ info->last_alloc = 0;
+ if (!empty_size) {
+ empty_size += 16 * 1024 * 1024;
+ total_needed += empty_size;
+ }
+ search_start = find_search_start(root, &block_group,
+ search_start, total_needed,
+ data);
+ }
+
search_start = stripe_align(root, search_start);
cached_start = search_start;
btrfs_init_path(path);
ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
if (ret < 0)
goto error;
-
- if (path->slots[0] > 0) {
- path->slots[0]--;
- }
-
+ ret = find_previous_extent(root, path);
+ if (ret < 0)
+ goto error;
l = path->nodes[0];
btrfs_item_key_to_cpu(l, &key, path->slots[0]);
-
- /*
- * walk backwards to find the first extent item key
- */
- while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
- if (path->slots[0] == 0) {
- ret = btrfs_prev_leaf(root, path);
- if (ret != 0) {
- ret = btrfs_search_slot(trans, root, ins,
- path, 0, 0);
- if (ret < 0)
- goto error;
- if (path->slots[0] > 0)
- path->slots[0]--;
- break;
- }
- } else {
- path->slots[0]--;
- }
- l = path->nodes[0];
- btrfs_item_key_to_cpu(l, &key, path->slots[0]);
- }
while (1) {
l = path->nodes[0];
slot = path->slots[0];
error:
btrfs_release_path(root, path);
btrfs_free_path(path);
+ if (btrfs_test_opt(root, SSD) && !ret && !data)
+ info->last_alloc = ins->objectid + ins->offset;
return ret;
}
/*
{
int ret;
int pending_ret;
- u64 super_used, root_used;
+ u64 super_used;
+ u64 root_used;
u64 search_start = 0;
u64 new_hint;
struct btrfs_fs_info *info = root->fs_info;
0, 0, 0);
return ERR_PTR(-ENOMEM);
}
+ btrfs_set_header_generation(buf, trans->transid);
+ clean_tree_block(trans, root, buf);
+ wait_on_tree_block_writeback(root, buf);
btrfs_set_buffer_uptodate(buf);
+
+ if (PageDirty(buf->first_page)) {
+ printk("page %lu dirty\n", buf->first_page->index);
+ WARN_ON(1);
+ }
+
set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
buf->start + buf->len - 1, GFP_NOFS);
- set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
+ set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->io_tree,
buf->start, buf->start + buf->len - 1,
EXTENT_CSUM, GFP_NOFS);
buf->flags |= EXTENT_CSUM;
- btrfs_set_buffer_defrag(buf);
+ if (!btrfs_test_opt(root, SSD))
+ btrfs_set_buffer_defrag(buf);
trans->blocks_used++;
return buf;
}
-static int drop_leaf_ref(struct btrfs_trans_handle *trans,
- struct btrfs_root *root, struct extent_buffer *leaf)
+static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct extent_buffer *leaf)
{
u64 leaf_owner;
u64 leaf_generation;
return 0;
}
-static void reada_walk_down(struct btrfs_root *root,
- struct extent_buffer *node)
+static void noinline reada_walk_down(struct btrfs_root *root,
+ struct extent_buffer *node,
+ int slot)
{
- int i;
- u32 nritems;
u64 bytenr;
- int ret;
+ u64 last = 0;
+ u32 nritems;
u32 refs;
- int level;
u32 blocksize;
+ int ret;
+ int i;
+ int level;
+ int skipped = 0;
nritems = btrfs_header_nritems(node);
level = btrfs_header_level(node);
- for (i = 0; i < nritems; i++) {
+ if (level)
+ return;
+
+ for (i = slot; i < nritems && skipped < 32; i++) {
bytenr = btrfs_node_blockptr(node, i);
- blocksize = btrfs_level_size(root, level - 1);
- ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
- BUG_ON(ret);
- if (refs != 1)
+ if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
+ (last > bytenr && last - bytenr > 32 * 1024))) {
+ skipped++;
continue;
+ }
+ blocksize = btrfs_level_size(root, level - 1);
+ if (i != slot) {
+ ret = lookup_extent_ref(NULL, root, bytenr,
+ blocksize, &refs);
+ BUG_ON(ret);
+ if (refs != 1) {
+ skipped++;
+ continue;
+ }
+ }
mutex_unlock(&root->fs_info->fs_mutex);
ret = readahead_tree_block(root, bytenr, blocksize);
+ last = bytenr + blocksize;
cond_resched();
mutex_lock(&root->fs_info->fs_mutex);
if (ret)
* helper function for drop_snapshot, this walks down the tree dropping ref
* counts as it goes.
*/
-static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
- *root, struct btrfs_path *path, int *level)
+static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct btrfs_path *path, int *level)
{
u64 root_owner;
u64 root_gen;
WARN_ON(*level >= BTRFS_MAX_LEVEL);
cur = path->nodes[*level];
- if (*level > 0 && path->slots[*level] == 0)
- reada_walk_down(root, cur);
-
if (btrfs_header_level(cur) != *level)
WARN_ON(1);
next = btrfs_find_tree_block(root, bytenr, blocksize);
if (!next || !btrfs_buffer_uptodate(next)) {
free_extent_buffer(next);
+ reada_walk_down(root, cur, path->slots[*level]);
mutex_unlock(&root->fs_info->fs_mutex);
next = read_tree_block(root, bytenr, blocksize);
mutex_lock(&root->fs_info->fs_mutex);
* to find the first node higher up where we haven't yet gone through
* all the slots
*/
-static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
- *root, struct btrfs_path *path, int *level)
+static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct btrfs_path *path, int *level)
{
u64 root_owner;
u64 root_gen;
return 0;
}
-static int relocate_inode_pages(struct inode *inode, u64 start, u64 len)
+static int noinline relocate_inode_pages(struct inode *inode, u64 start,
+ u64 len)
{
u64 page_start;
u64 page_end;
u64 delalloc_start;
u64 existing_delalloc;
unsigned long last_index;
- unsigned long first_index;
unsigned long i;
struct page *page;
struct btrfs_root *root = BTRFS_I(inode)->root;
- struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
- struct file_ra_state ra;
+ struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
+ struct file_ra_state *ra;
+
+ ra = kzalloc(sizeof(*ra), GFP_NOFS);
mutex_lock(&inode->i_mutex);
- first_index = start >> PAGE_CACHE_SHIFT;
+ i = start >> PAGE_CACHE_SHIFT;
last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
- memset(&ra, 0, sizeof(ra));
- file_ra_state_init(&ra, inode->i_mapping);
- btrfs_force_ra(inode->i_mapping, &ra, NULL, first_index, last_index);
+ file_ra_state_init(ra, inode->i_mapping);
+ btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
+ kfree(ra);
- for (i = first_index; i <= last_index; i++) {
+ for (; i <= last_index; i++) {
page = grab_cache_page(inode->i_mapping, i);
if (!page)
goto out_unlock;
page_start = (u64)page->index << PAGE_CACHE_SHIFT;
page_end = page_start + PAGE_CACHE_SIZE - 1;
- lock_extent(em_tree, page_start, page_end, GFP_NOFS);
+ lock_extent(io_tree, page_start, page_end, GFP_NOFS);
delalloc_start = page_start;
- existing_delalloc =
- count_range_bits(&BTRFS_I(inode)->extent_tree,
- &delalloc_start, page_end,
- PAGE_CACHE_SIZE, EXTENT_DELALLOC);
+ existing_delalloc = count_range_bits(io_tree,
+ &delalloc_start, page_end,
+ PAGE_CACHE_SIZE, EXTENT_DELALLOC);
- set_extent_delalloc(em_tree, page_start,
+ set_extent_delalloc(io_tree, page_start,
page_end, GFP_NOFS);
spin_lock(&root->fs_info->delalloc_lock);
existing_delalloc;
spin_unlock(&root->fs_info->delalloc_lock);
- unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
+ unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
set_page_dirty(page);
unlock_page(page);
page_cache_release(page);
return 0;
}
-static int relocate_one_reference(struct btrfs_root *extent_root,
+/*
+ * note, this releases the path
+ */
+static int noinline relocate_one_reference(struct btrfs_root *extent_root,
struct btrfs_path *path,
- struct btrfs_key *extent_key,
- u64 ref_root, u64 ref_gen, u64 ref_objectid,
- u64 ref_offset)
+ struct btrfs_key *extent_key)
{
struct inode *inode;
struct btrfs_root *found_root;
- struct btrfs_key root_location;
+ struct btrfs_key *root_location;
+ struct btrfs_extent_ref *ref;
+ u64 ref_root;
+ u64 ref_gen;
+ u64 ref_objectid;
+ u64 ref_offset;
int ret;
- root_location.objectid = ref_root;
+ ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_extent_ref);
+ ref_root = btrfs_ref_root(path->nodes[0], ref);
+ ref_gen = btrfs_ref_generation(path->nodes[0], ref);
+ ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
+ ref_offset = btrfs_ref_offset(path->nodes[0], ref);
+ btrfs_release_path(extent_root, path);
+
+ root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
+ root_location->objectid = ref_root;
if (ref_gen == 0)
- root_location.offset = 0;
+ root_location->offset = 0;
else
- root_location.offset = (u64)-1;
- root_location.type = BTRFS_ROOT_ITEM_KEY;
+ root_location->offset = (u64)-1;
+ root_location->type = BTRFS_ROOT_ITEM_KEY;
found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
- &root_location);
+ root_location);
BUG_ON(!found_root);
+ kfree(root_location);
if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
mutex_unlock(&extent_root->fs_info->fs_mutex);
return 0;
}
-static int relocate_one_extent(struct btrfs_root *extent_root,
- struct btrfs_path *path,
- struct btrfs_key *extent_key)
+static int noinline relocate_one_extent(struct btrfs_root *extent_root,
+ struct btrfs_path *path,
+ struct btrfs_key *extent_key)
{
struct btrfs_key key;
struct btrfs_key found_key;
- struct btrfs_extent_ref *ref;
struct extent_buffer *leaf;
- u64 ref_root;
- u64 ref_gen;
- u64 ref_objectid;
- u64 ref_offset;
u32 nritems;
u32 item_size;
int ret = 0;
while(1) {
ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
- BUG_ON(ret == 0);
-
if (ret < 0)
goto out;
key.offset = found_key.offset + 1;
item_size = btrfs_item_size_nr(leaf, path->slots[0]);
- ref = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_extent_ref);
- ref_root = btrfs_ref_root(leaf, ref);
- ref_gen = btrfs_ref_generation(leaf, ref);
- ref_objectid = btrfs_ref_objectid(leaf, ref);
- ref_offset = btrfs_ref_offset(leaf, ref);
- btrfs_release_path(extent_root, path);
-
- ret = relocate_one_reference(extent_root, path,
- extent_key, ref_root, ref_gen,
- ref_objectid, ref_offset);
+ ret = relocate_one_reference(extent_root, path, extent_key);
if (ret)
goto out;
}
return ret;
}
-static int find_overlapping_extent(struct btrfs_root *root,
- struct btrfs_path *path, u64 new_size)
-{
- struct btrfs_key found_key;
- struct extent_buffer *leaf;
- int ret;
-
- while(1) {
- if (path->slots[0] == 0) {
- ret = btrfs_prev_leaf(root, path);
- if (ret == 1) {
- return 1;
- }
- if (ret < 0)
- return ret;
- } else {
- path->slots[0]--;
- }
- leaf = path->nodes[0];
- btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
- if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
- if (found_key.objectid + found_key.offset > new_size)
- return 0;
- else
- return 1;
- }
- }
- return 1;
-}
-
int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
{
struct btrfs_trans_handle *trans;
struct btrfs_path *path;
u64 cur_byte;
u64 total_found;
- u64 ptr;
struct btrfs_fs_info *info = root->fs_info;
- struct extent_map_tree *block_group_cache;
+ struct extent_io_tree *block_group_cache;
struct btrfs_key key;
- struct btrfs_key found_key = { 0, 0, 0 };
+ struct btrfs_key found_key;
struct extent_buffer *leaf;
u32 nritems;
int ret;
- int slot;
+ int progress = 0;
btrfs_set_super_total_bytes(&info->super_copy, new_size);
+ clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
+ GFP_NOFS);
block_group_cache = &info->block_group_cache;
path = btrfs_alloc_path();
root = root->fs_info->extent_root;
again:
total_found = 0;
key.objectid = new_size;
- cur_byte = key.objectid;
key.offset = 0;
key.type = 0;
+ cur_byte = key.objectid;
+
+ ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+ if (ret < 0)
+ goto out;
+
+ ret = find_previous_extent(root, path);
+ if (ret < 0)
+ goto out;
+ if (ret == 0) {
+ leaf = path->nodes[0];
+ btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+ if (found_key.objectid + found_key.offset > new_size) {
+ cur_byte = found_key.objectid;
+ key.objectid = cur_byte;
+ }
+ }
+ btrfs_release_path(root, path);
+
while(1) {
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
goto out;
-next:
+
leaf = path->nodes[0];
- if (key.objectid == new_size - 1) {
- ret = find_overlapping_extent(root, path, new_size);
- if (ret != 0) {
- btrfs_release_path(root, path);
- ret = btrfs_search_slot(NULL, root, &key,
- path, 0, 0);
- if (ret < 0)
- goto out;
+ nritems = btrfs_header_nritems(leaf);
+next:
+ if (path->slots[0] >= nritems) {
+ ret = btrfs_next_leaf(root, path);
+ if (ret < 0)
+ goto out;
+ if (ret == 1) {
+ ret = 0;
+ break;
}
+ leaf = path->nodes[0];
+ nritems = btrfs_header_nritems(leaf);
}
- nritems = btrfs_header_nritems(leaf);
- ret = 0;
- slot = path->slots[0];
- if (slot < nritems)
- btrfs_item_key_to_cpu(leaf, &found_key, slot);
- if (slot == nritems ||
- btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
+
+ btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+ if (progress && need_resched()) {
+ memcpy(&key, &found_key, sizeof(key));
+ mutex_unlock(&root->fs_info->fs_mutex);
+ cond_resched();
+ mutex_lock(&root->fs_info->fs_mutex);
+ btrfs_release_path(root, path);
+ btrfs_search_slot(NULL, root, &key, path, 0, 0);
+ progress = 0;
+ goto next;
+ }
+ progress = 1;
+
+ if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
+ found_key.objectid + found_key.offset <= cur_byte) {
path->slots[0]++;
- if (path->slots[0] >= nritems) {
- ret = btrfs_next_leaf(root, path);
- if (ret < 0)
- goto out;
- if (ret == 1) {
- ret = 0;
- break;
- }
- }
goto next;
}
- btrfs_item_key_to_cpu(leaf, &found_key, slot);
- if (found_key.objectid + found_key.offset <= cur_byte)
- continue;
+
total_found++;
cur_byte = found_key.objectid + found_key.offset;
key.objectid = cur_byte;
key.offset = 0;
key.type = 0;
while(1) {
+ u64 ptr;
+
ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
if (ret < 0)
goto out;
-bg_next:
+
leaf = path->nodes[0];
nritems = btrfs_header_nritems(leaf);
- ret = 0;
- slot = path->slots[0];
- if (slot < nritems)
- btrfs_item_key_to_cpu(leaf, &found_key, slot);
- if (slot == nritems ||
- btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
- if (slot < nritems) {
- printk("shrinker found key %Lu %u %Lu\n",
- found_key.objectid, found_key.type,
- found_key.offset);
- path->slots[0]++;
- }
- if (path->slots[0] >= nritems) {
- ret = btrfs_next_leaf(root, path);
- if (ret < 0)
- break;
- if (ret == 1) {
- ret = 0;
- break;
- }
+bg_next:
+ if (path->slots[0] >= nritems) {
+ ret = btrfs_next_leaf(root, path);
+ if (ret < 0)
+ break;
+ if (ret == 1) {
+ ret = 0;
+ break;
}
+ leaf = path->nodes[0];
+ btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+ /*
+ * btrfs_next_leaf doesn't cow buffers, we have to
+ * do the search again
+ */
+ memcpy(&key, &found_key, sizeof(key));
+ btrfs_release_path(root, path);
+ goto resched_check;
+ }
+
+ btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+ if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
+ printk("shrinker found key %Lu %u %Lu\n",
+ found_key.objectid, found_key.type,
+ found_key.offset);
+ path->slots[0]++;
goto bg_next;
}
- btrfs_item_key_to_cpu(leaf, &found_key, slot);
ret = get_state_private(&info->block_group_cache,
found_key.objectid, &ptr);
if (!ret)
key.objectid = found_key.objectid + 1;
btrfs_del_item(trans, root, path);
btrfs_release_path(root, path);
+resched_check:
+ if (need_resched()) {
+ mutex_unlock(&root->fs_info->fs_mutex);
+ cond_resched();
+ mutex_lock(&root->fs_info->fs_mutex);
+ }
}
clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
GFP_NOFS);
u64 nr = 0;
u64 cur_byte;
u64 old_size;
+ unsigned long rem;
struct btrfs_block_group_cache *cache;
struct btrfs_block_group_item *item;
struct btrfs_fs_info *info = root->fs_info;
- struct extent_map_tree *block_group_cache;
+ struct extent_io_tree *block_group_cache;
struct btrfs_key key;
struct extent_buffer *leaf;
int ret;
struct btrfs_block_group_item);
btrfs_set_disk_block_group_used(leaf, item, 0);
- if (nr % 3) {
+ div_long_long_rem(nr, 3, &rem);
+ if (rem) {
btrfs_set_disk_block_group_flags(leaf, item,
BTRFS_BLOCK_GROUP_DATA);
} else {
int bit;
struct btrfs_block_group_cache *cache;
struct btrfs_fs_info *info = root->fs_info;
- struct extent_map_tree *block_group_cache;
+ struct extent_io_tree *block_group_cache;
struct btrfs_key key;
struct btrfs_key found_key;
struct extent_buffer *leaf;