File: block-helpers.c

package info (click to toggle)
qemu 1%3A5.2%2Bdfsg-11%2Bdeb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 203,452 kB
  • sloc: ansic: 2,327,684; pascal: 107,506; asm: 49,545; python: 40,498; sh: 35,286; cpp: 33,587; makefile: 15,209; perl: 6,965; xml: 3,028; objc: 1,460; php: 1,299; tcl: 1,070; yacc: 604; lex: 363; sql: 71; awk: 35; sed: 11
file content (46 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * Block utility functions
 *
 * Copyright IBM, Corp. 2011
 * Copyright (c) 2020 Coiby Xu <coiby.xu@gmail.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qapi/qmp/qerror.h"
#include "block-helpers.h"

/**
 * check_block_size:
 * @id: The unique ID of the object
 * @name: The name of the property being validated
 * @value: The block size in bytes
 * @errp: A pointer to an area to store an error
 *
 * This function checks that the block size meets the following conditions:
 * 1. At least MIN_BLOCK_SIZE
 * 2. No larger than MAX_BLOCK_SIZE
 * 3. A power of 2
 */
void check_block_size(const char *id, const char *name, int64_t value,
                      Error **errp)
{
    /* value of 0 means "unset" */
    if (value && (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE)) {
        error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
                   id, name, value, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);
        return;
    }

    /* We rely on power-of-2 blocksizes for bitmasks */
    if ((value & (value - 1)) != 0) {
        error_setg(errp,
                   "Property %s.%s doesn't take value '%" PRId64
                   "', it's not a power of 2",
                   id, name, value);
        return;
    }
}