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;
+ struct extent_io_tree *free_space_cache;
+ struct extent_state *state;
u64 last;
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);
+ free_space_cache = &root->fs_info->free_space_cache;
+
again:
ret = cache_block_group(root, cache);
if (ret)
last = max(search_start, cache->key.objectid);
+ spin_lock_irq(&free_space_cache->lock);
+ state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
while(1) {
- ret = find_first_extent_bit(&root->fs_info->free_space_cache,
- last, &start, &end, EXTENT_DIRTY);
- if (ret) {
+ if (!state) {
if (!cache_miss)
cache_miss = last;
+ spin_unlock_irq(&free_space_cache->lock);
goto new_group;
}
- start = max(last, start);
- last = end + 1;
+ start = max(last, state->start);
+ last = state->end + 1;
if (last - start < num) {
if (last == cache->key.objectid + cache->key.offset)
cache_miss = start;
+ do {
+ state = extent_state_next(state);
+ } while(state && !(state->state & EXTENT_DIRTY));
continue;
}
+ spin_unlock_irq(&free_space_cache->lock);
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
high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
lenum = cpu_to_le64(ref_generation);
low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
-
-#if 0
- lenum = cpu_to_le64(owner);
- low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
- lenum = cpu_to_le64(owner_offset);
- low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
-#endif
+ if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
+ lenum = cpu_to_le64(owner);
+ low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
+ lenum = cpu_to_le64(owner_offset);
+ low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
+ }
return ((u64)high_crc << 32) | (u64)low_crc;
}
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);
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;
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;
}
struct btrfs_root *extent_root = info->extent_root;
struct extent_buffer *leaf;
int ret;
+ int extent_slot = 0;
+ int found_extent = 0;
+ int num_to_del = 1;
struct btrfs_extent_item *ei;
u32 refs;
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,
owner_objectid, owner_offset, 1);
if (ret == 0) {
- ret = btrfs_del_item(trans, extent_root, path);
+ struct btrfs_key found_key;
+ extent_slot = path->slots[0];
+ while(extent_slot > 0) {
+ extent_slot--;
+ btrfs_item_key_to_cpu(path->nodes[0], &found_key,
+ extent_slot);
+ if (found_key.objectid != bytenr)
+ break;
+ if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
+ found_key.offset == num_bytes) {
+ found_extent = 1;
+ break;
+ }
+ if (path->slots[0] - extent_slot > 5)
+ break;
+ }
+ if (!found_extent)
+ ret = btrfs_del_item(trans, extent_root, path);
} else {
btrfs_print_leaf(extent_root, path->nodes[0]);
WARN_ON(1);
root_objectid, ref_generation, owner_objectid,
owner_offset);
}
- btrfs_release_path(extent_root, path);
- ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
- if (ret < 0)
- return ret;
- BUG_ON(ret);
+ if (!found_extent) {
+ btrfs_release_path(extent_root, path);
+ ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
+ if (ret < 0)
+ return ret;
+ BUG_ON(ret);
+ extent_slot = path->slots[0];
+ }
leaf = path->nodes[0];
- ei = btrfs_item_ptr(leaf, path->slots[0],
+ ei = btrfs_item_ptr(leaf, extent_slot,
struct btrfs_extent_item);
refs = btrfs_extent_refs(leaf, ei);
BUG_ON(refs == 0);
refs -= 1;
btrfs_set_extent_refs(leaf, ei, refs);
+
btrfs_mark_buffer_dirty(leaf);
+ if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
+ /* if the back ref and the extent are next to each other
+ * they get deleted below in one shot
+ */
+ path->slots[0] = extent_slot;
+ num_to_del = 2;
+ } else if (found_extent) {
+ /* otherwise delete the extent back ref */
+ ret = btrfs_del_item(trans, extent_root, path);
+ BUG_ON(ret);
+ /* if refs are 0, we need to setup the path for deletion */
+ if (refs == 0) {
+ btrfs_release_path(extent_root, path);
+ ret = btrfs_search_slot(trans, extent_root, &key, path,
+ -1, 1);
+ if (ret < 0)
+ return ret;
+ BUG_ON(ret);
+ }
+ }
+
if (refs == 0) {
u64 super_used;
u64 root_used;
root_used = btrfs_root_used(&root->root_item);
btrfs_set_root_used(&root->root_item,
root_used - num_bytes);
-
- ret = btrfs_del_item(trans, extent_root, path);
+ ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
+ num_to_del);
if (ret) {
return ret;
}
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;
int ret;
int slot = 0;
u64 last_byte = 0;
+ u64 *last_ptr = NULL;
u64 orig_search_start = search_start;
int start_found;
struct extent_buffer *l;
struct btrfs_block_group_cache *block_group;
int full_scan = 0;
int wrapped = 0;
+ int empty_cluster;
u64 cached_start;
WARN_ON(num_bytes < root->sectorsize);
data = BTRFS_BLOCK_GROUP_MIXED;
}
- if (search_end == (u64)-1)
- search_end = btrfs_super_total_bytes(&info->super_copy);
+ if (!data) {
+ last_ptr = &root->fs_info->last_alloc;
+ empty_cluster = 128 * 1024;
+ }
+
+ if (data && btrfs_test_opt(root, SSD)) {
+ last_ptr = &root->fs_info->last_data_alloc;
+ empty_cluster = 2 * 1024 * 1024;
+ }
+
+ if (last_ptr) {
+ if (*last_ptr)
+ hint_byte = *last_ptr;
+ else {
+ hint_byte = hint_byte &
+ ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
+ empty_size += empty_cluster;
+ }
+ search_start = max(search_start, hint_byte);
+ }
+
+ 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 (last_ptr && *last_ptr && search_start != *last_ptr) {
+ *last_ptr = 0;
+ if (!empty_size) {
+ empty_size += empty_cluster;
+ 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];
}
ins->offset = num_bytes;
btrfs_free_path(path);
+ if (last_ptr) {
+ *last_ptr = ins->objectid + ins->offset;
+ if (*last_ptr ==
+ btrfs_super_total_bytes(&root->fs_info->super_copy)) {
+ *last_ptr = 0;
+ }
+ }
return 0;
new_group:
{
int ret;
int pending_ret;
- u64 super_used, root_used;
+ u64 super_used;
+ u64 root_used;
u64 search_start = 0;
u64 new_hint;
+ u32 sizes[2];
struct btrfs_fs_info *info = root->fs_info;
struct btrfs_root *extent_root = info->extent_root;
- struct btrfs_extent_item extent_item;
+ struct btrfs_extent_item *extent_item;
+ struct btrfs_extent_ref *ref;
struct btrfs_path *path;
-
- btrfs_set_stack_extent_refs(&extent_item, 1);
+ struct btrfs_key keys[2];
new_hint = max(hint_byte, root->fs_info->alloc_start);
if (new_hint < btrfs_super_total_bytes(&info->super_copy))
WARN_ON(trans->alloc_exclude_nr);
trans->alloc_exclude_start = ins->objectid;
trans->alloc_exclude_nr = ins->offset;
- ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
- sizeof(extent_item));
- trans->alloc_exclude_start = 0;
- trans->alloc_exclude_nr = 0;
- BUG_ON(ret);
+ memcpy(&keys[0], ins, sizeof(*ins));
+ keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
+ owner, owner_offset);
+ keys[1].objectid = ins->objectid;
+ keys[1].type = BTRFS_EXTENT_REF_KEY;
+ sizes[0] = sizeof(*extent_item);
+ sizes[1] = sizeof(*ref);
path = btrfs_alloc_path();
BUG_ON(!path);
- ret = btrfs_insert_extent_backref(trans, extent_root, path,
- ins->objectid, root_objectid,
- ref_generation, owner, owner_offset);
+
+ ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
+ sizes, 2);
BUG_ON(ret);
+ extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_extent_item);
+ btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
+ ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
+ struct btrfs_extent_ref);
+
+ btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
+ btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
+ btrfs_set_ref_objectid(path->nodes[0], ref, owner);
+ btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
+
+ btrfs_mark_buffer_dirty(path->nodes[0]);
+
+ trans->alloc_exclude_start = 0;
+ trans->alloc_exclude_nr = 0;
btrfs_free_path(path);
finish_current_insert(trans, extent_root);
pending_ret = del_pending_extents(trans, extent_root);
update_block:
ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
data);
- BUG_ON(ret);
+ if (ret) {
+ printk("update block group failed for %Lu %Lu\n",
+ ins->objectid, ins->offset);
+ BUG();
+ }
return 0;
}
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;
unsigned long last_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 extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
struct file_ra_state *ra;
ra = kzalloc(sizeof(*ra), GFP_NOFS);
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);
- root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
- 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);
/*
* note, this releases the path
*/
-static int relocate_one_reference(struct btrfs_root *extent_root,
+static int noinline relocate_one_reference(struct btrfs_root *extent_root,
struct btrfs_path *path,
struct btrfs_key *extent_key)
{
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;
while(1) {
ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
- BUG_ON(ret == 0);
-
if (ret < 0)
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;
u64 cur_byte;
u64 total_found;
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;
- while(1) {
+ 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;
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);
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;
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;