xref: /openbmc/qemu/util/block-helpers.c (revision 284d697c)
1 /*
2  * Block utility functions
3  *
4  * Copyright IBM, Corp. 2011
5  * Copyright (c) 2020 Coiby Xu <coiby.xu@gmail.com>
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8  * See the COPYING file in the top-level directory.
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qapi/qmp/qerror.h"
14 #include "block-helpers.h"
15 
16 /**
17  * check_block_size:
18  * @id: The unique ID of the object
19  * @name: The name of the property being validated
20  * @value: The block size in bytes
21  * @errp: A pointer to an area to store an error
22  *
23  * This function checks that the block size meets the following conditions:
24  * 1. At least MIN_BLOCK_SIZE
25  * 2. No larger than MAX_BLOCK_SIZE
26  * 3. A power of 2
27  */
28 void check_block_size(const char *id, const char *name, int64_t value,
29                       Error **errp)
30 {
31     /* value of 0 means "unset" */
32     if (value && (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE)) {
33         error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
34                    id, name, value, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);
35         return;
36     }
37 
38     /* We rely on power-of-2 blocksizes for bitmasks */
39     if ((value & (value - 1)) != 0) {
40         error_setg(errp,
41                    "Property %s.%s doesn't take value '%" PRId64
42                    "', it's not a power of 2",
43                    id, name, value);
44         return;
45     }
46 }
47