xref: /openbmc/qemu/util/block-helpers.c (revision 7d87775f)
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 "block-helpers.h"
14 
15 /**
16  * check_block_size:
17  * @name: The name of the property being validated
18  * @value: The block size in bytes
19  * @errp: A pointer to an area to store an error
20  *
21  * This function checks that the block size meets the following conditions:
22  * 1. At least MIN_BLOCK_SIZE
23  * 2. No larger than MAX_BLOCK_SIZE
24  * 3. A power of 2
25  *
26  * Returns: true on success, false on failure
27  */
28 bool check_block_size(const char *name, int64_t value, Error **errp)
29 {
30     if (!value) {
31         /* unset */
32         return true;
33     }
34 
35     if (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE
36         || (value & (value - 1))) {
37         error_setg(errp,
38                    "parameter %s must be a power of 2 between %" PRId64
39                    " and %" PRId64,
40                    name, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);
41         return false;
42     }
43     return true;
44 }
45