428 lines · plain
1=================2Thin provisioning3=================4 5Introduction6============7 8This document describes a collection of device-mapper targets that9between them implement thin-provisioning and snapshots.10 11The main highlight of this implementation, compared to the previous12implementation of snapshots, is that it allows many virtual devices to13be stored on the same data volume. This simplifies administration and14allows the sharing of data between volumes, thus reducing disk usage.15 16Another significant feature is support for an arbitrary depth of17recursive snapshots (snapshots of snapshots of snapshots ...). The18previous implementation of snapshots did this by chaining together19lookup tables, and so performance was O(depth). This new20implementation uses a single data structure to avoid this degradation21with depth. Fragmentation may still be an issue, however, in some22scenarios.23 24Metadata is stored on a separate device from data, giving the25administrator some freedom, for example to:26 27- Improve metadata resilience by storing metadata on a mirrored volume28 but data on a non-mirrored one.29 30- Improve performance by storing the metadata on SSD.31 32Status33======34 35These targets are considered safe for production use. But different use36cases will have different performance characteristics, for example due37to fragmentation of the data volume.38 39If you find this software is not performing as expected please mail40dm-devel@redhat.com with details and we'll try our best to improve41things for you.42 43Userspace tools for checking and repairing the metadata have been fully44developed and are available as 'thin_check' and 'thin_repair'. The name45of the package that provides these utilities varies by distribution (on46a Red Hat distribution it is named 'device-mapper-persistent-data').47 48Cookbook49========50 51This section describes some quick recipes for using thin provisioning.52They use the dmsetup program to control the device-mapper driver53directly. End users will be advised to use a higher-level volume54manager such as LVM2 once support has been added.55 56Pool device57-----------58 59The pool device ties together the metadata volume and the data volume.60It maps I/O linearly to the data volume and updates the metadata via61two mechanisms:62 63- Function calls from the thin targets64 65- Device-mapper 'messages' from userspace which control the creation of new66 virtual devices amongst other things.67 68Setting up a fresh pool device69------------------------------70 71Setting up a pool device requires a valid metadata device, and a72data device. If you do not have an existing metadata device you can73make one by zeroing the first 4k to indicate empty metadata.74 75 dd if=/dev/zero of=$metadata_dev bs=4096 count=176 77The amount of metadata you need will vary according to how many blocks78are shared between thin devices (i.e. through snapshots). If you have79less sharing than average you'll need a larger-than-average metadata device.80 81As a guide, we suggest you calculate the number of bytes to use in the82metadata device as 48 * $data_dev_size / $data_block_size but round it up83to 2MB if the answer is smaller. If you're creating large numbers of84snapshots which are recording large amounts of change, you may find you85need to increase this.86 87The largest size supported is 16GB: If the device is larger,88a warning will be issued and the excess space will not be used.89 90Reloading a pool table91----------------------92 93You may reload a pool's table, indeed this is how the pool is resized94if it runs out of space. (N.B. While specifying a different metadata95device when reloading is not forbidden at the moment, things will go96wrong if it does not route I/O to exactly the same on-disk location as97previously.)98 99Using an existing pool device100-----------------------------101 102::103 104 dmsetup create pool \105 --table "0 20971520 thin-pool $metadata_dev $data_dev \106 $data_block_size $low_water_mark"107 108$data_block_size gives the smallest unit of disk space that can be109allocated at a time expressed in units of 512-byte sectors.110$data_block_size must be between 128 (64KB) and 2097152 (1GB) and a111multiple of 128 (64KB). $data_block_size cannot be changed after the112thin-pool is created. People primarily interested in thin provisioning113may want to use a value such as 1024 (512KB). People doing lots of114snapshotting may want a smaller value such as 128 (64KB). If you are115not zeroing newly-allocated data, a larger $data_block_size in the116region of 256000 (128MB) is suggested.117 118$low_water_mark is expressed in blocks of size $data_block_size. If119free space on the data device drops below this level then a dm event120will be triggered which a userspace daemon should catch allowing it to121extend the pool device. Only one such event will be sent.122 123No special event is triggered if a just resumed device's free space is below124the low water mark. However, resuming a device always triggers an125event; a userspace daemon should verify that free space exceeds the low126water mark when handling this event.127 128A low water mark for the metadata device is maintained in the kernel and129will trigger a dm event if free space on the metadata device drops below130it.131 132Updating on-disk metadata133-------------------------134 135On-disk metadata is committed every time a FLUSH or FUA bio is written.136If no such requests are made then commits will occur every second. This137means the thin-provisioning target behaves like a physical disk that has138a volatile write cache. If power is lost you may lose some recent139writes. The metadata should always be consistent in spite of any crash.140 141If data space is exhausted the pool will either error or queue IO142according to the configuration (see: error_if_no_space). If metadata143space is exhausted or a metadata operation fails: the pool will error IO144until the pool is taken offline and repair is performed to 1) fix any145potential inconsistencies and 2) clear the flag that imposes repair.146Once the pool's metadata device is repaired it may be resized, which147will allow the pool to return to normal operation. Note that if a pool148is flagged as needing repair, the pool's data and metadata devices149cannot be resized until repair is performed. It should also be noted150that when the pool's metadata space is exhausted the current metadata151transaction is aborted. Given that the pool will cache IO whose152completion may have already been acknowledged to upper IO layers153(e.g. filesystem) it is strongly suggested that consistency checks154(e.g. fsck) be performed on those layers when repair of the pool is155required.156 157Thin provisioning158-----------------159 160i) Creating a new thinly-provisioned volume.161 162 To create a new thinly- provisioned volume you must send a message to an163 active pool device, /dev/mapper/pool in this example::164 165 dmsetup message /dev/mapper/pool 0 "create_thin 0"166 167 Here '0' is an identifier for the volume, a 24-bit number. It's up168 to the caller to allocate and manage these identifiers. If the169 identifier is already in use, the message will fail with -EEXIST.170 171ii) Using a thinly-provisioned volume.172 173 Thinly-provisioned volumes are activated using the 'thin' target::174 175 dmsetup create thin --table "0 2097152 thin /dev/mapper/pool 0"176 177 The last parameter is the identifier for the thinp device.178 179Internal snapshots180------------------181 182i) Creating an internal snapshot.183 184 Snapshots are created with another message to the pool.185 186 N.B. If the origin device that you wish to snapshot is active, you187 must suspend it before creating the snapshot to avoid corruption.188 This is NOT enforced at the moment, so please be careful!189 190 ::191 192 dmsetup suspend /dev/mapper/thin193 dmsetup message /dev/mapper/pool 0 "create_snap 1 0"194 dmsetup resume /dev/mapper/thin195 196 Here '1' is the identifier for the volume, a 24-bit number. '0' is the197 identifier for the origin device.198 199ii) Using an internal snapshot.200 201 Once created, the user doesn't have to worry about any connection202 between the origin and the snapshot. Indeed the snapshot is no203 different from any other thinly-provisioned device and can be204 snapshotted itself via the same method. It's perfectly legal to205 have only one of them active, and there's no ordering requirement on206 activating or removing them both. (This differs from conventional207 device-mapper snapshots.)208 209 Activate it exactly the same way as any other thinly-provisioned volume::210 211 dmsetup create snap --table "0 2097152 thin /dev/mapper/pool 1"212 213External snapshots214------------------215 216You can use an external **read only** device as an origin for a217thinly-provisioned volume. Any read to an unprovisioned area of the218thin device will be passed through to the origin. Writes trigger219the allocation of new blocks as usual.220 221One use case for this is VM hosts that want to run guests on222thinly-provisioned volumes but have the base image on another device223(possibly shared between many VMs).224 225You must not write to the origin device if you use this technique!226Of course, you may write to the thin device and take internal snapshots227of the thin volume.228 229i) Creating a snapshot of an external device230 231 This is the same as creating a thin device.232 You don't mention the origin at this stage.233 234 ::235 236 dmsetup message /dev/mapper/pool 0 "create_thin 0"237 238ii) Using a snapshot of an external device.239 240 Append an extra parameter to the thin target specifying the origin::241 242 dmsetup create snap --table "0 2097152 thin /dev/mapper/pool 0 /dev/image"243 244 N.B. All descendants (internal snapshots) of this snapshot require the245 same extra origin parameter.246 247Deactivation248------------249 250All devices using a pool must be deactivated before the pool itself251can be.252 253::254 255 dmsetup remove thin256 dmsetup remove snap257 dmsetup remove pool258 259Reference260=========261 262'thin-pool' target263------------------264 265i) Constructor266 267 ::268 269 thin-pool <metadata dev> <data dev> <data block size (sectors)> \270 <low water mark (blocks)> [<number of feature args> [<arg>]*]271 272 Optional feature arguments:273 274 skip_block_zeroing:275 Skip the zeroing of newly-provisioned blocks.276 277 ignore_discard:278 Disable discard support.279 280 no_discard_passdown:281 Don't pass discards down to the underlying282 data device, but just remove the mapping.283 284 read_only:285 Don't allow any changes to be made to the pool286 metadata. This mode is only available after the287 thin-pool has been created and first used in full288 read/write mode. It cannot be specified on initial289 thin-pool creation.290 291 error_if_no_space:292 Error IOs, instead of queueing, if no space.293 294 Data block size must be between 64KB (128 sectors) and 1GB295 (2097152 sectors) inclusive.296 297 298ii) Status299 300 ::301 302 <transaction id> <used metadata blocks>/<total metadata blocks>303 <used data blocks>/<total data blocks> <held metadata root>304 ro|rw|out_of_data_space [no_]discard_passdown [error|queue]_if_no_space305 needs_check|- metadata_low_watermark306 307 transaction id:308 A 64-bit number used by userspace to help synchronise with metadata309 from volume managers.310 311 used data blocks / total data blocks312 If the number of free blocks drops below the pool's low water mark a313 dm event will be sent to userspace. This event is edge-triggered and314 it will occur only once after each resume so volume manager writers315 should register for the event and then check the target's status.316 317 held metadata root:318 The location, in blocks, of the metadata root that has been319 'held' for userspace read access. '-' indicates there is no320 held root.321 322 discard_passdown|no_discard_passdown323 Whether or not discards are actually being passed down to the324 underlying device. When this is enabled when loading the table,325 it can get disabled if the underlying device doesn't support it.326 327 ro|rw|out_of_data_space328 If the pool encounters certain types of device failures it will329 drop into a read-only metadata mode in which no changes to330 the pool metadata (like allocating new blocks) are permitted.331 332 In serious cases where even a read-only mode is deemed unsafe333 no further I/O will be permitted and the status will just334 contain the string 'Fail'. The userspace recovery tools335 should then be used.336 337 error_if_no_space|queue_if_no_space338 If the pool runs out of data or metadata space, the pool will339 either queue or error the IO destined to the data device. The340 default is to queue the IO until more space is added or the341 'no_space_timeout' expires. The 'no_space_timeout' dm-thin-pool342 module parameter can be used to change this timeout -- it343 defaults to 60 seconds but may be disabled using a value of 0.344 345 needs_check346 A metadata operation has failed, resulting in the needs_check347 flag being set in the metadata's superblock. The metadata348 device must be deactivated and checked/repaired before the349 thin-pool can be made fully operational again. '-' indicates350 needs_check is not set.351 352 metadata_low_watermark:353 Value of metadata low watermark in blocks. The kernel sets this354 value internally but userspace needs to know this value to355 determine if an event was caused by crossing this threshold.356 357iii) Messages358 359 create_thin <dev id>360 Create a new thinly-provisioned device.361 <dev id> is an arbitrary unique 24-bit identifier chosen by362 the caller.363 364 create_snap <dev id> <origin id>365 Create a new snapshot of another thinly-provisioned device.366 <dev id> is an arbitrary unique 24-bit identifier chosen by367 the caller.368 <origin id> is the identifier of the thinly-provisioned device369 of which the new device will be a snapshot.370 371 delete <dev id>372 Deletes a thin device. Irreversible.373 374 set_transaction_id <current id> <new id>375 Userland volume managers, such as LVM, need a way to376 synchronise their external metadata with the internal metadata of the377 pool target. The thin-pool target offers to store an378 arbitrary 64-bit transaction id and return it on the target's379 status line. To avoid races you must provide what you think380 the current transaction id is when you change it with this381 compare-and-swap message.382 383 reserve_metadata_snap384 Reserve a copy of the data mapping btree for use by userland.385 This allows userland to inspect the mappings as they were when386 this message was executed. Use the pool's status command to387 get the root block associated with the metadata snapshot.388 389 release_metadata_snap390 Release a previously reserved copy of the data mapping btree.391 392'thin' target393-------------394 395i) Constructor396 397 ::398 399 thin <pool dev> <dev id> [<external origin dev>]400 401 pool dev:402 the thin-pool device, e.g. /dev/mapper/my_pool or 253:0403 404 dev id:405 the internal device identifier of the device to be406 activated.407 408 external origin dev:409 an optional block device outside the pool to be treated as a410 read-only snapshot origin: reads to unprovisioned areas of the411 thin target will be mapped to this device.412 413The pool doesn't store any size against the thin devices. If you414load a thin target that is smaller than you've been using previously,415then you'll have no access to blocks mapped beyond the end. If you416load a target that is bigger than before, then extra blocks will be417provisioned as and when needed.418 419ii) Status420 421 <nr mapped sectors> <highest mapped sector>422 If the pool has encountered device errors and failed, the status423 will just contain the string 'Fail'. The userspace recovery424 tools should then be used.425 426 In the case where <nr mapped sectors> is 0, there is no highest427 mapped sector and the value of <highest mapped sector> is unspecified.428