Esempio n. 1
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;
}