2
0

block-helpers.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #include "qemu/osdep.h"
  11. #include "qapi/error.h"
  12. #include "qapi/qmp/qerror.h"
  13. #include "block-helpers.h"
  14. /**
  15. * check_block_size:
  16. * @id: The unique ID of the object
  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. void check_block_size(const char *id, const char *name, int64_t value,
  27. Error **errp)
  28. {
  29. /* value of 0 means "unset" */
  30. if (value && (value < MIN_BLOCK_SIZE || value > MAX_BLOCK_SIZE)) {
  31. error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
  32. id, name, value, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);
  33. return;
  34. }
  35. /* We rely on power-of-2 blocksizes for bitmasks */
  36. if ((value & (value - 1)) != 0) {
  37. error_setg(errp,
  38. "Property %s.%s doesn't take value '%" PRId64
  39. "', it's not a power of 2",
  40. id, name, value);
  41. return;
  42. }
  43. }