示例#1
0
int add_to_free_space_tree(struct btrfs_trans_handle *trans,
			   u64 start, u64 size)
{
	struct btrfs_block_group_cache *block_group;
	struct btrfs_path *path;
	int ret;

	if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
		return 0;

	path = btrfs_alloc_path();
	if (!path) {
		ret = -ENOMEM;
		goto out;
	}

	block_group = btrfs_lookup_block_group(trans->fs_info, start);
	if (!block_group) {
		ASSERT(0);
		ret = -ENOENT;
		goto out;
	}

	mutex_lock(&block_group->free_space_lock);
	ret = __add_to_free_space_tree(trans, block_group, path, start, size);
	mutex_unlock(&block_group->free_space_lock);

	btrfs_put_block_group(block_group);
out:
	btrfs_free_path(path);
	if (ret)
		btrfs_abort_transaction(trans, ret);
	return ret;
}
static int test_merge_none(struct btrfs_trans_handle *trans,
			   struct btrfs_fs_info *fs_info,
			   struct btrfs_block_group_cache *cache,
			   struct btrfs_path *path)
{
	struct free_space_extent extents[] = {
		{cache->key.objectid, BITMAP_RANGE},
		{cache->key.objectid + 2 * BITMAP_RANGE, BITMAP_RANGE},
		{cache->key.objectid + 4 * BITMAP_RANGE, BITMAP_RANGE},
	};
	int ret;

	ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
					    cache->key.objectid,
					    cache->key.offset);
	if (ret) {
		test_msg("Could not remove free space\n");
		return ret;
	}

	ret = __add_to_free_space_tree(trans, fs_info, cache, path,
				       cache->key.objectid, BITMAP_RANGE);
	if (ret) {
		test_msg("Could not add free space\n");
		return ret;
	}

	ret = __add_to_free_space_tree(trans, fs_info, cache, path,
				       cache->key.objectid + 4 * BITMAP_RANGE,
				       BITMAP_RANGE);
	if (ret) {
		test_msg("Could not add free space\n");
		return ret;
	}

	ret = __add_to_free_space_tree(trans, fs_info, cache, path,
				       cache->key.objectid + 2 * BITMAP_RANGE,
				       BITMAP_RANGE);
	if (ret) {
		test_msg("Could not add free space\n");
		return ret;
	}

	return check_free_space_extents(trans, fs_info, cache, path,
					extents, ARRAY_SIZE(extents));
}
示例#3
0
static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
					struct btrfs_block_group_cache *block_group,
					struct btrfs_path *path)
{
	int ret;

	block_group->needs_free_space = 0;

	ret = add_new_free_space_info(trans, block_group, path);
	if (ret)
		return ret;

	return __add_to_free_space_tree(trans, block_group, path,
					block_group->key.objectid,
					block_group->key.offset);
}
/*
 * Populate the free space tree by walking the extent tree. Operations on the
 * extent tree that happen as a result of writes to the free space tree will go
 * through the normal add/remove hooks.
 */
static int populate_free_space_tree(struct btrfs_trans_handle *trans,
                                    struct btrfs_fs_info *fs_info,
                                    struct btrfs_block_group_cache *block_group)
{
    struct btrfs_root *extent_root = fs_info->extent_root;
    struct btrfs_path *path, *path2;
    struct btrfs_key key;
    u64 start, end;
    int ret;

    path = btrfs_alloc_path();
    if (!path)
        return -ENOMEM;
    path->reada = 1;

    path2 = btrfs_alloc_path();
    if (!path2) {
        btrfs_free_path(path);
        return -ENOMEM;
    }

    ret = add_new_free_space_info(trans, fs_info, block_group, path2);
    if (ret)
        goto out;

    mutex_lock(&block_group->free_space_lock);

    /*
     * Iterate through all of the extent and metadata items in this block
     * group, adding the free space between them and the free space at the
     * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
     * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
     * contained in.
     */
    key.objectid = block_group->key.objectid;
    key.type = BTRFS_EXTENT_ITEM_KEY;
    key.offset = 0;

    ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
    if (ret < 0)
        goto out_locked;
    ASSERT(ret == 0);

    start = block_group->key.objectid;
    end = block_group->key.objectid + block_group->key.offset;
    while (1) {
        btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);

        if (key.type == BTRFS_EXTENT_ITEM_KEY ||
                key.type == BTRFS_METADATA_ITEM_KEY) {
            if (key.objectid >= end)
                break;

            if (start < key.objectid) {
                ret = __add_to_free_space_tree(trans, fs_info,
                                               block_group,
                                               path2, start,
                                               key.objectid -
                                               start);
                if (ret)
                    goto out_locked;
            }
            start = key.objectid;
            if (key.type == BTRFS_METADATA_ITEM_KEY)
                start += fs_info->tree_root->nodesize;
            else
                start += key.offset;
        } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
            if (key.objectid != block_group->key.objectid)
                break;
        }

        ret = btrfs_next_item(extent_root, path);
        if (ret < 0)
            goto out_locked;
        if (ret)
            break;
    }
    if (start < end) {
        ret = __add_to_free_space_tree(trans, fs_info, block_group,
                                       path2, start, end - start);
        if (ret)
            goto out_locked;
    }

    ret = 0;
out_locked:
    mutex_unlock(&block_group->free_space_lock);
out:
    btrfs_free_path(path2);
    btrfs_free_path(path);
    return ret;
}