#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*orig_root, u64 num_blocks, u64 search_start, u64
search_end, struct btrfs_key *ins);
static int finish_current_insert(struct btrfs_trans_handle *trans, struct
btrfs_root *extent_root);
static int del_pending_extents(struct btrfs_trans_handle *trans, struct
btrfs_root *extent_root);
struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
struct btrfs_block_group_cache
*hint, int data)
{
struct btrfs_block_group_cache *cache[8];
struct btrfs_block_group_cache *found_group = NULL;
struct btrfs_fs_info *info = root->fs_info;
u64 used;
u64 last = 0;
u64 hint_last;
int i;
int ret;
int full_search = 0;
if (hint) {
used = btrfs_block_group_used(&hint->item);
if (used < (hint->key.offset * 2) / 3) {
return hint;
}
radix_tree_tag_clear(&info->block_group_radix,
hint->key.objectid + hint->key.offset - 1,
BTRFS_BLOCK_GROUP_AVAIL);
last = hint->key.objectid + hint->key.offset;
hint_last = last;
} else {
hint_last = 0;
last = 0;
}
while(1) {
ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
(void **)cache,
last, ARRAY_SIZE(cache),
BTRFS_BLOCK_GROUP_AVAIL);
if (!ret)
break;
for (i = 0; i < ret; i++) {
used = btrfs_block_group_used(&cache[i]->item);
if (used < (cache[i]->key.offset * 2) / 3) {
info->block_group_cache = cache[i];
found_group = cache[i];
goto found;
}
radix_tree_tag_clear(&info->block_group_radix,
cache[i]->key.objectid +
cache[i]->key.offset - 1,
BTRFS_BLOCK_GROUP_AVAIL);
last = cache[i]->key.objectid +
cache[i]->key.offset;
}
}
last = hint_last;
again:
while(1) {
ret = radix_tree_gang_lookup(&info->block_group_radix,
(void **)cache,
last, ARRAY_SIZE(cache));
if (!ret)
break;
for (i = 0; i < ret; i++) {
used = btrfs_block_group_used(&cache[i]->item);
if (used < cache[i]->key.offset) {
info->block_group_cache = cache[i];
found_group = cache[i];
goto found;
}
radix_tree_tag_clear(&info->block_group_radix,
cache[i]->key.objectid +
cache[i]->key.offset - 1,
BTRFS_BLOCK_GROUP_AVAIL);
last = cache[i]->key.objectid +
cache[i]->key.offset;
}
}
info->block_group_cache = NULL;
if (!full_search) {
last = 0;
full_search = 1