Skip to content

Commit

Permalink
block: use Error mechanism instead of -errno for block_job_set_speed()
Browse files Browse the repository at this point in the history
There are at least two different errors that can occur in
block_job_set_speed(): the job might not support setting speeds or the
value might be invalid.

Use the Error mechanism to report the error where it occurs.

Signed-off-by: Stefan Hajnoczi <[email protected]>
Acked-by: Kevin Wolf <[email protected]>
Signed-off-by: Luiz Capitulino <[email protected]>
  • Loading branch information
Stefan Hajnoczi authored and Luiz Capitulino committed Apr 27, 2012
1 parent fd7f8c6 commit 9e6636c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
17 changes: 10 additions & 7 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -4114,18 +4114,21 @@ void block_job_complete(BlockJob *job, int ret)
bdrv_set_in_use(bs, 0);
}

int block_job_set_speed(BlockJob *job, int64_t value)
void block_job_set_speed(BlockJob *job, int64_t value, Error **errp)
{
int rc;
Error *local_err = NULL;

if (!job->job_type->set_speed) {
return -ENOTSUP;
error_set(errp, QERR_NOT_SUPPORTED);
return;
}
rc = job->job_type->set_speed(job, value);
if (rc == 0) {
job->speed = value;
job->job_type->set_speed(job, value, &local_err);
if (error_is_set(&local_err)) {
error_propagate(errp, local_err);
return;
}
return rc;

job->speed = value;
}

void block_job_cancel(BlockJob *job)
Expand Down
6 changes: 3 additions & 3 deletions block/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ static void coroutine_fn stream_run(void *opaque)
block_job_complete(&s->common, ret);
}

static int stream_set_speed(BlockJob *job, int64_t value)
static void stream_set_speed(BlockJob *job, int64_t value, Error **errp)
{
StreamBlockJob *s = container_of(job, StreamBlockJob, common);

if (value < 0) {
return -EINVAL;
error_set(errp, QERR_INVALID_PARAMETER, "value");
return;
}
ratelimit_set_speed(&s->limit, value / BDRV_SECTOR_SIZE);
return 0;
}

static BlockJobType stream_job_type = {
Expand Down
5 changes: 3 additions & 2 deletions block_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ typedef struct BlockJobType {
const char *job_type;

/** Optional callback for job types that support setting a speed limit */
int (*set_speed)(BlockJob *job, int64_t value);
void (*set_speed)(BlockJob *job, int64_t value, Error **errp);
} BlockJobType;

/**
Expand Down Expand Up @@ -375,11 +375,12 @@ void block_job_complete(BlockJob *job, int ret);
* block_job_set_speed:
* @job: The job to set the speed for.
* @speed: The new value
* @errp: Error object.
*
* Set a rate-limiting parameter for the job; the actual meaning may
* vary depending on the job type.
*/
int block_job_set_speed(BlockJob *job, int64_t value);
void block_job_set_speed(BlockJob *job, int64_t value, Error **errp);

/**
* block_job_cancel:
Expand Down
4 changes: 1 addition & 3 deletions blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1145,9 +1145,7 @@ void qmp_block_job_set_speed(const char *device, int64_t value, Error **errp)
return;
}

if (block_job_set_speed(job, value) < 0) {
error_set(errp, QERR_NOT_SUPPORTED);
}
block_job_set_speed(job, value, errp);
}

void qmp_block_job_cancel(const char *device, Error **errp)
Expand Down
1 change: 1 addition & 0 deletions qapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@
#
# Returns: Nothing on success
# If the job type does not support throttling, NotSupported
# If the speed value is invalid, InvalidParameter
# If streaming is not active on this device, DeviceNotActive
#
# Since: 1.1
Expand Down

0 comments on commit 9e6636c

Please sign in to comment.