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