thin-provisioning.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. Introduction
  2. ============
  3. This document describes a collection of device-mapper targets that
  4. between them implement thin-provisioning and snapshots.
  5. The main highlight of this implementation, compared to the previous
  6. implementation of snapshots, is that it allows many virtual devices to
  7. be stored on the same data volume. This simplifies administration and
  8. allows the sharing of data between volumes, thus reducing disk usage.
  9. Another significant feature is support for an arbitrary depth of
  10. recursive snapshots (snapshots of snapshots of snapshots ...). The
  11. previous implementation of snapshots did this by chaining together
  12. lookup tables, and so performance was O(depth). This new
  13. implementation uses a single data structure to avoid this degradation
  14. with depth. Fragmentation may still be an issue, however, in some
  15. scenarios.
  16. Metadata is stored on a separate device from data, giving the
  17. administrator some freedom, for example to:
  18. - Improve metadata resilience by storing metadata on a mirrored volume
  19. but data on a non-mirrored one.
  20. - Improve performance by storing the metadata on SSD.
  21. Status
  22. ======
  23. These targets are very much still in the EXPERIMENTAL state. Please
  24. do not yet rely on them in production. But do experiment and offer us
  25. feedback. Different use cases will have different performance
  26. characteristics, for example due to fragmentation of the data volume.
  27. If you find this software is not performing as expected please mail
  28. dm-devel@redhat.com with details and we'll try our best to improve
  29. things for you.
  30. Userspace tools for checking and repairing the metadata are under
  31. development.
  32. Cookbook
  33. ========
  34. This section describes some quick recipes for using thin provisioning.
  35. They use the dmsetup program to control the device-mapper driver
  36. directly. End users will be advised to use a higher-level volume
  37. manager such as LVM2 once support has been added.
  38. Pool device
  39. -----------
  40. The pool device ties together the metadata volume and the data volume.
  41. It maps I/O linearly to the data volume and updates the metadata via
  42. two mechanisms:
  43. - Function calls from the thin targets
  44. - Device-mapper 'messages' from userspace which control the creation of new
  45. virtual devices amongst other things.
  46. Setting up a fresh pool device
  47. ------------------------------
  48. Setting up a pool device requires a valid metadata device, and a
  49. data device. If you do not have an existing metadata device you can
  50. make one by zeroing the first 4k to indicate empty metadata.
  51. dd if=/dev/zero of=$metadata_dev bs=4096 count=1
  52. The amount of metadata you need will vary according to how many blocks
  53. are shared between thin devices (i.e. through snapshots). If you have
  54. less sharing than average you'll need a larger-than-average metadata device.
  55. As a guide, we suggest you calculate the number of bytes to use in the
  56. metadata device as 48 * $data_dev_size / $data_block_size but round it up
  57. to 2MB if the answer is smaller. If you're creating large numbers of
  58. snapshots which are recording large amounts of change, you may find you
  59. need to increase this.
  60. The largest size supported is 16GB: If the device is larger,
  61. a warning will be issued and the excess space will not be used.
  62. Reloading a pool table
  63. ----------------------
  64. You may reload a pool's table, indeed this is how the pool is resized
  65. if it runs out of space. (N.B. While specifying a different metadata
  66. device when reloading is not forbidden at the moment, things will go
  67. wrong if it does not route I/O to exactly the same on-disk location as
  68. previously.)
  69. Using an existing pool device
  70. -----------------------------
  71. dmsetup create pool \
  72. --table "0 20971520 thin-pool $metadata_dev $data_dev \
  73. $data_block_size $low_water_mark"
  74. $data_block_size gives the smallest unit of disk space that can be
  75. allocated at a time expressed in units of 512-byte sectors.
  76. $data_block_size must be between 128 (64KB) and 2097152 (1GB) and a
  77. multiple of 128 (64KB). $data_block_size cannot be changed after the
  78. thin-pool is created. People primarily interested in thin provisioning
  79. may want to use a value such as 1024 (512KB). People doing lots of
  80. snapshotting may want a smaller value such as 128 (64KB). If you are
  81. not zeroing newly-allocated data, a larger $data_block_size in the
  82. region of 256000 (128MB) is suggested.
  83. $low_water_mark is expressed in blocks of size $data_block_size. If
  84. free space on the data device drops below this level then a dm event
  85. will be triggered which a userspace daemon should catch allowing it to
  86. extend the pool device. Only one such event will be sent.
  87. No special event is triggered if a just resumed device's free space is below
  88. the low water mark. However, resuming a device always triggers an
  89. event; a userspace daemon should verify that free space exceeds the low
  90. water mark when handling this event.
  91. A low water mark for the metadata device is maintained in the kernel and
  92. will trigger a dm event if free space on the metadata device drops below
  93. it.
  94. Updating on-disk metadata
  95. -------------------------
  96. On-disk metadata is committed every time a FLUSH or FUA bio is written.
  97. If no such requests are made then commits will occur every second. This
  98. means the thin-provisioning target behaves like a physical disk that has
  99. a volatile write cache. If power is lost you may lose some recent
  100. writes. The metadata should always be consistent in spite of any crash.
  101. If data space is exhausted the pool will either error or queue IO
  102. according to the configuration (see: error_if_no_space). If metadata
  103. space is exhausted or a metadata operation fails: the pool will error IO
  104. until the pool is taken offline and repair is performed to 1) fix any
  105. potential inconsistencies and 2) clear the flag that imposes repair.
  106. Once the pool's metadata device is repaired it may be resized, which
  107. will allow the pool to return to normal operation. Note that if a pool
  108. is flagged as needing repair, the pool's data and metadata devices
  109. cannot be resized until repair is performed. It should also be noted
  110. that when the pool's metadata space is exhausted the current metadata
  111. transaction is aborted. Given that the pool will cache IO whose
  112. completion may have already been acknowledged to upper IO layers
  113. (e.g. filesystem) it is strongly suggested that consistency checks
  114. (e.g. fsck) be performed on those layers when repair of the pool is
  115. required.
  116. Thin provisioning
  117. -----------------
  118. i) Creating a new thinly-provisioned volume.
  119. To create a new thinly- provisioned volume you must send a message to an
  120. active pool device, /dev/mapper/pool in this example.
  121. dmsetup message /dev/mapper/pool 0 "create_thin 0"
  122. Here '0' is an identifier for the volume, a 24-bit number. It's up
  123. to the caller to allocate and manage these identifiers. If the
  124. identifier is already in use, the message will fail with -EEXIST.
  125. ii) Using a thinly-provisioned volume.
  126. Thinly-provisioned volumes are activated using the 'thin' target:
  127. dmsetup create thin --table "0 2097152 thin /dev/mapper/pool 0"
  128. The last parameter is the identifier for the thinp device.
  129. Internal snapshots
  130. ------------------
  131. i) Creating an internal snapshot.
  132. Snapshots are created with another message to the pool.
  133. N.B. If the origin device that you wish to snapshot is active, you
  134. must suspend it before creating the snapshot to avoid corruption.
  135. This is NOT enforced at the moment, so please be careful!
  136. dmsetup suspend /dev/mapper/thin
  137. dmsetup message /dev/mapper/pool 0 "create_snap 1 0"
  138. dmsetup resume /dev/mapper/thin
  139. Here '1' is the identifier for the volume, a 24-bit number. '0' is the
  140. identifier for the origin device.
  141. ii) Using an internal snapshot.
  142. Once created, the user doesn't have to worry about any connection
  143. between the origin and the snapshot. Indeed the snapshot is no
  144. different from any other thinly-provisioned device and can be
  145. snapshotted itself via the same method. It's perfectly legal to
  146. have only one of them active, and there's no ordering requirement on
  147. activating or removing them both. (This differs from conventional
  148. device-mapper snapshots.)
  149. Activate it exactly the same way as any other thinly-provisioned volume:
  150. dmsetup create snap --table "0 2097152 thin /dev/mapper/pool 1"
  151. External snapshots
  152. ------------------
  153. You can use an external _read only_ device as an origin for a
  154. thinly-provisioned volume. Any read to an unprovisioned area of the
  155. thin device will be passed through to the origin. Writes trigger
  156. the allocation of new blocks as usual.
  157. One use case for this is VM hosts that want to run guests on
  158. thinly-provisioned volumes but have the base image on another device
  159. (possibly shared between many VMs).
  160. You must not write to the origin device if you use this technique!
  161. Of course, you may write to the thin device and take internal snapshots
  162. of the thin volume.
  163. i) Creating a snapshot of an external device
  164. This is the same as creating a thin device.
  165. You don't mention the origin at this stage.
  166. dmsetup message /dev/mapper/pool 0 "create_thin 0"
  167. ii) Using a snapshot of an external device.
  168. Append an extra parameter to the thin target specifying the origin:
  169. dmsetup create snap --table "0 2097152 thin /dev/mapper/pool 0 /dev/image"
  170. N.B. All descendants (internal snapshots) of this snapshot require the
  171. same extra origin parameter.
  172. Deactivation
  173. ------------
  174. All devices using a pool must be deactivated before the pool itself
  175. can be.
  176. dmsetup remove thin
  177. dmsetup remove snap
  178. dmsetup remove pool
  179. Reference
  180. =========
  181. 'thin-pool' target
  182. ------------------
  183. i) Constructor
  184. thin-pool <metadata dev> <data dev> <data block size (sectors)> \
  185. <low water mark (blocks)> [<number of feature args> [<arg>]*]
  186. Optional feature arguments:
  187. skip_block_zeroing: Skip the zeroing of newly-provisioned blocks.
  188. ignore_discard: Disable discard support.
  189. no_discard_passdown: Don't pass discards down to the underlying
  190. data device, but just remove the mapping.
  191. read_only: Don't allow any changes to be made to the pool
  192. metadata.
  193. error_if_no_space: Error IOs, instead of queueing, if no space.
  194. Data block size must be between 64KB (128 sectors) and 1GB
  195. (2097152 sectors) inclusive.
  196. ii) Status
  197. <transaction id> <used metadata blocks>/<total metadata blocks>
  198. <used data blocks>/<total data blocks> <held metadata root>
  199. [no_]discard_passdown ro|rw
  200. transaction id:
  201. A 64-bit number used by userspace to help synchronise with metadata
  202. from volume managers.
  203. used data blocks / total data blocks
  204. If the number of free blocks drops below the pool's low water mark a
  205. dm event will be sent to userspace. This event is edge-triggered and
  206. it will occur only once after each resume so volume manager writers
  207. should register for the event and then check the target's status.
  208. held metadata root:
  209. The location, in blocks, of the metadata root that has been
  210. 'held' for userspace read access. '-' indicates there is no
  211. held root.
  212. discard_passdown|no_discard_passdown
  213. Whether or not discards are actually being passed down to the
  214. underlying device. When this is enabled when loading the table,
  215. it can get disabled if the underlying device doesn't support it.
  216. ro|rw|out_of_data_space
  217. If the pool encounters certain types of device failures it will
  218. drop into a read-only metadata mode in which no changes to
  219. the pool metadata (like allocating new blocks) are permitted.
  220. In serious cases where even a read-only mode is deemed unsafe
  221. no further I/O will be permitted and the status will just
  222. contain the string 'Fail'. The userspace recovery tools
  223. should then be used.
  224. error_if_no_space|queue_if_no_space
  225. If the pool runs out of data or metadata space, the pool will
  226. either queue or error the IO destined to the data device. The
  227. default is to queue the IO until more space is added or the
  228. 'no_space_timeout' expires. The 'no_space_timeout' dm-thin-pool
  229. module parameter can be used to change this timeout -- it
  230. defaults to 60 seconds but may be disabled using a value of 0.
  231. needs_check
  232. A metadata operation has failed, resulting in the needs_check
  233. flag being set in the metadata's superblock. The metadata
  234. device must be deactivated and checked/repaired before the
  235. thin-pool can be made fully operational again. '-' indicates
  236. needs_check is not set.
  237. iii) Messages
  238. create_thin <dev id>
  239. Create a new thinly-provisioned device.
  240. <dev id> is an arbitrary unique 24-bit identifier chosen by
  241. the caller.
  242. create_snap <dev id> <origin id>
  243. Create a new snapshot of another thinly-provisioned device.
  244. <dev id> is an arbitrary unique 24-bit identifier chosen by
  245. the caller.
  246. <origin id> is the identifier of the thinly-provisioned device
  247. of which the new device will be a snapshot.
  248. delete <dev id>
  249. Deletes a thin device. Irreversible.
  250. set_transaction_id <current id> <new id>
  251. Userland volume managers, such as LVM, need a way to
  252. synchronise their external metadata with the internal metadata of the
  253. pool target. The thin-pool target offers to store an
  254. arbitrary 64-bit transaction id and return it on the target's
  255. status line. To avoid races you must provide what you think
  256. the current transaction id is when you change it with this
  257. compare-and-swap message.
  258. reserve_metadata_snap
  259. Reserve a copy of the data mapping btree for use by userland.
  260. This allows userland to inspect the mappings as they were when
  261. this message was executed. Use the pool's status command to
  262. get the root block associated with the metadata snapshot.
  263. release_metadata_snap
  264. Release a previously reserved copy of the data mapping btree.
  265. 'thin' target
  266. -------------
  267. i) Constructor
  268. thin <pool dev> <dev id> [<external origin dev>]
  269. pool dev:
  270. the thin-pool device, e.g. /dev/mapper/my_pool or 253:0
  271. dev id:
  272. the internal device identifier of the device to be
  273. activated.
  274. external origin dev:
  275. an optional block device outside the pool to be treated as a
  276. read-only snapshot origin: reads to unprovisioned areas of the
  277. thin target will be mapped to this device.
  278. The pool doesn't store any size against the thin devices. If you
  279. load a thin target that is smaller than you've been using previously,
  280. then you'll have no access to blocks mapped beyond the end. If you
  281. load a target that is bigger than before, then extra blocks will be
  282. provisioned as and when needed.
  283. ii) Status
  284. <nr mapped sectors> <highest mapped sector>
  285. If the pool has encountered device errors and failed, the status
  286. will just contain the string 'Fail'. The userspace recovery
  287. tools should then be used.