blkdebug.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. Block I/O error injection using blkdebug
  2. ----------------------------------------
  3. Copyright (C) 2014 Red Hat Inc
  4. This work is licensed under the terms of the GNU GPL, version 2 or later. See
  5. the COPYING file in the top-level directory.
  6. The blkdebug block driver is a rule-based error injection engine. It can be
  7. used to exercise error code paths in block drivers including ENOSPC (out of
  8. space) and EIO.
  9. This document gives an overview of the features available in blkdebug.
  10. Background
  11. ----------
  12. Block drivers have many error code paths that handle I/O errors. Image formats
  13. are especially complex since metadata I/O errors during cluster allocation or
  14. while updating tables happen halfway through request processing and require
  15. discipline to keep image files consistent.
  16. Error injection allows test cases to trigger I/O errors at specific points.
  17. This way, all error paths can be tested to make sure they are correct.
  18. Rules
  19. -----
  20. The blkdebug block driver takes a list of "rules" that tell the error injection
  21. engine when to fail an I/O request.
  22. Each I/O request is evaluated against the rules. If a rule matches the request
  23. then its "action" is executed.
  24. Rules can be placed in a configuration file; the configuration file
  25. follows the same .ini-like format used by QEMU's -readconfig option, and
  26. each section of the file represents a rule.
  27. The following configuration file defines a single rule:
  28. $ cat blkdebug.conf
  29. [inject-error]
  30. event = "read_aio"
  31. errno = "28"
  32. This rule fails all aio read requests with ENOSPC (28). Note that the errno
  33. value depends on the host. On Linux, see
  34. /usr/include/asm-generic/errno-base.h for errno values.
  35. Invoke QEMU as follows:
  36. $ qemu-system-x86_64
  37. -drive if=none,cache=none,file=blkdebug:blkdebug.conf:test.img,id=drive0 \
  38. -device virtio-blk-pci,drive=drive0,id=virtio-blk-pci0
  39. Rules support the following attributes:
  40. event - which type of operation to match (e.g. read_aio, write_aio,
  41. flush_to_os, flush_to_disk). See the "Events" section for
  42. information on events.
  43. state - (optional) the engine must be in this state number in order for this
  44. rule to match. See the "State transitions" section for information
  45. on states.
  46. errno - the numeric errno value to return when a request matches this rule.
  47. The errno values depend on the host since the numeric values are not
  48. standarized in the POSIX specification.
  49. sector - (optional) a sector number that the request must overlap in order to
  50. match this rule
  51. once - (optional, default "off") only execute this action on the first
  52. matching request
  53. immediately - (optional, default "off") return a NULL BlockAIOCB
  54. pointer and fail without an errno instead. This
  55. exercises the code path where BlockAIOCB fails and the
  56. caller's BlockCompletionFunc is not invoked.
  57. Events
  58. ------
  59. Block drivers provide information about the type of I/O request they are about
  60. to make so rules can match specific types of requests. For example, the qcow2
  61. block driver tells blkdebug when it accesses the L1 table so rules can match
  62. only L1 table accesses and not other metadata or guest data requests.
  63. The core events are:
  64. read_aio - guest data read
  65. write_aio - guest data write
  66. flush_to_os - write out unwritten block driver state (e.g. cached metadata)
  67. flush_to_disk - flush the host block device's disk cache
  68. See block/blkdebug.c:event_names[] for the full list of events. You may need
  69. to grep block driver source code to understand the meaning of specific events.
  70. State transitions
  71. -----------------
  72. There are cases where more power is needed to match a particular I/O request in
  73. a longer sequence of requests. For example:
  74. write_aio
  75. flush_to_disk
  76. write_aio
  77. How do we match the 2nd write_aio but not the first? This is where state
  78. transitions come in.
  79. The error injection engine has an integer called the "state" that always starts
  80. initialized to 1. The state integer is internal to blkdebug and cannot be
  81. observed from outside but rules can interact with it for powerful matching
  82. behavior.
  83. Rules can be conditional on the current state and they can transition to a new
  84. state.
  85. When a rule's "state" attribute is non-zero then the current state must equal
  86. the attribute in order for the rule to match.
  87. For example, to match the 2nd write_aio:
  88. [set-state]
  89. event = "write_aio"
  90. state = "1"
  91. new_state = "2"
  92. [inject-error]
  93. event = "write_aio"
  94. state = "2"
  95. errno = "5"
  96. The first write_aio request matches the set-state rule and transitions from
  97. state 1 to state 2. Once state 2 has been entered, the set-state rule no
  98. longer matches since it requires state 1. But the inject-error rule now
  99. matches the next write_aio request and injects EIO (5).
  100. State transition rules support the following attributes:
  101. event - which type of operation to match (e.g. read_aio, write_aio,
  102. flush_to_os, flush_to_disk). See the "Events" section for
  103. information on events.
  104. state - (optional) the engine must be in this state number in order for this
  105. rule to match
  106. new_state - transition to this state number
  107. Suspend and resume
  108. ------------------
  109. Exercising code paths in block drivers may require specific ordering amongst
  110. concurrent requests. The "breakpoint" feature allows requests to be halted on
  111. a blkdebug event and resumed later. This makes it possible to achieve
  112. deterministic ordering when multiple requests are in flight.
  113. Breakpoints on blkdebug events are associated with a user-defined "tag" string.
  114. This tag serves as an identifier by which the request can be resumed at a later
  115. point.
  116. See the qemu-io(1) break, resume, remove_break, and wait_break commands for
  117. details.