Ejemplo n.º 1
0
static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
{
    int r, i;
    enum allocation_event ev;
    struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
    dm_block_t old_len = smm->ll.nr_blocks;

    /*
     * Flick into a mode where all blocks get allocated in the new area.
     */
    smm->begin = old_len;
    memcpy(sm, &bootstrap_ops, sizeof(*sm));

    /*
     * Extend.
     */
    r = sm_ll_extend(&smm->ll, extra_blocks);
    if (r)
        goto out;

    /*
     * We repeatedly increment then commit until the commit doesn't
     * allocate any new blocks.
     */
    do {
        for (i = old_len; !r && i < smm->begin; i++) {
            r = sm_ll_inc(&smm->ll, i, &ev);
            if (r)
                goto out;
        }
        old_len = smm->begin;

        r = apply_bops(smm);
        if (r) {
            DMERR("%s: apply_bops failed", __func__);
            goto out;
        }

        r = sm_ll_commit(&smm->ll);
        if (r)
            goto out;

    } while (old_len != smm->begin);

out:
    /*
     * Switch back to normal behaviour.
     */
    memcpy(sm, &ops, sizeof(*sm));
    return r;
}
int dm_sm_metadata_create(struct dm_space_map *sm,
			  struct dm_transaction_manager *tm,
			  dm_block_t nr_blocks,
			  dm_block_t superblock)
{
	int r;
	dm_block_t i;
	enum allocation_event ev;
	struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);

	smm->begin = superblock + 1;
	smm->recursion_count = 0;
	smm->allocated_this_transaction = 0;
	brb_init(&smm->uncommitted);
	threshold_init(&smm->threshold);

	memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));

	r = sm_ll_new_metadata(&smm->ll, tm);
	if (r)
		return r;

	if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
		nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
	r = sm_ll_extend(&smm->ll, nr_blocks);
	if (r)
		return r;

	memcpy(&smm->sm, &ops, sizeof(smm->sm));

	/*
	 * Now we need to update the newly created data structures with the
	 * allocated blocks that they were built from.
	 */
	for (i = superblock; !r && i < smm->begin; i++)
		r = sm_ll_inc(&smm->ll, i, &ev);

	if (r)
		return r;

	r = apply_bops(smm);
	if (r) {
		DMERR("%s: apply_bops failed", __func__);
		return r;
	}

	return sm_metadata_commit(sm);
}
int dm_sm_metadata_create(struct dm_space_map *sm,
			  struct dm_transaction_manager *tm,
			  dm_block_t nr_blocks,
			  dm_block_t superblock)
{
	int r;
	dm_block_t i;
	enum allocation_event ev;
	struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);

	smm->begin = superblock + 1;
	smm->recursion_count = 0;
	smm->allocated_this_transaction = 0;
	smm->nr_uncommitted = 0;

	memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));

	r = sm_ll_new_metadata(&smm->ll, tm);
	if (r)
		return r;

	r = sm_ll_extend(&smm->ll, nr_blocks);
	if (r)
		return r;

	memcpy(&smm->sm, &ops, sizeof(smm->sm));

	/*
	 * Now we need to update the newly created data structures with the
	 * allocated blocks that they were built from.
	 */
	for (i = superblock; !r && i < smm->begin; i++)
		r = sm_ll_inc(&smm->ll, i, &ev);

	if (r)
		return r;

	return sm_metadata_commit(sm);
}