int create_or_reload_device(struct crypt_device *cd, const char *name,
		     const char *type, struct crypt_dm_active_device *dmd)
{
	int r;
	enum devcheck device_check;
	struct dm_target *tgt;

	if (!type || !name || !single_segment(dmd))
		return -EINVAL;

	tgt = &dmd->segment;
	if (tgt->type != DM_CRYPT)
		return -EINVAL;

	/* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */
	r = check_devices(cd, name, NULL, &dmd->flags);
	if (r)
		return r;

	if (dmd->flags & CRYPT_ACTIVATE_REFRESH)
		r = _reload_device(cd, name, dmd);
	else {
		device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;

		r = device_block_adjust(cd, tgt->data_device, device_check,
					tgt->u.crypt.offset, &dmd->size, &dmd->flags);
		if (!r) {
			tgt->size = dmd->size;
			r = dm_create_device(cd, name, type, dmd);
		}
	}

	return r;
}
int create_or_reload_device(struct crypt_device *cd, const char *name,
		     const char *type, struct crypt_dm_active_device *dmd)
{
	int r;
	enum devcheck device_check;
	struct dm_target *tgt;

	if (!type || !name || !single_segment(dmd))
		return -EINVAL;

	tgt = &dmd->segment;
	if (tgt->type != DM_CRYPT)
		return -EINVAL;

	 
	r = check_devices(cd, name, NULL, &dmd->flags);
	if (r)
		return r;

	if (dmd->flags & CRYPT_ACTIVATE_REFRESH)
		r = _reload_device(cd, name, dmd);
	else {
		device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;

		r = device_block_adjust(cd, tgt->data_device, device_check,
					tgt->u.crypt.offset, &dmd->size, &dmd->flags);
		if (!r) {
			tgt->size = dmd->size;
			r = dm_create_device(cd, name, type, dmd);
		}
	}

	return r;
}