RBD Layering

RBD layering refers to the creation of copy-on-write clones of blockdevices. This allows for fast image creation, for example to clone agolden master image of a virtual machine into a new instance. Tosimplify the semantics, you can only create a clone of a snapshot -snapshots are always read-only, so the rest of the image isunaffected, and there’s no possibility of writing to themaccidentally.

From a user’s perspective, a clone is just like any other rbd image.You can take snapshots of them, read/write them, resize them, etc.There are no restrictions on clones from a user’s viewpoint.

Note: the terms child and parent below mean an rbd image createdby cloning, and the rbd image snapshot a child was cloned from.

Command line interface

Before cloning a snapshot, you must mark it as protected, to preventit from being deleted while child images refer to it:

  1. $ rbd snap protect pool/image@snap

Then you can perform the clone:

  1. $ rbd clone [--parent] pool/parent@snap [--image] pool2/child1

You can create a clone with different object sizes from the parent:

  1. $ rbd clone --order 25 pool/parent@snap pool2/child2

To delete the parent, you must first mark it unprotected, which checksthat there are no children left:

  1. $ rbd snap unprotect pool/image@snap
  2. Cannot unprotect: Still in use by pool2/image2
  3. $ rbd children pool/image@snap
  4. pool2/child1
  5. pool2/child2
  6. $ rbd flatten pool2/child1
  7. $ rbd rm pool2/child2
  8. $ rbd snap rm pool/image@snap
  9. Cannot remove a protected snapshot: pool/image@snap
  10. $ rbd snap unprotect pool/image@snap

Then the snapshot can be deleted like normal:

  1. $ rbd snap rm pool/image@snap

Implementation

Data Flow

In the initial implementation, called ‘trivial layering’, there willbe no tracking of which objects exist in a clone. A read that hits anon-existent object will attempt to read from the parent snapshot, andthis will continue recursively until an object exists or an image withno parent is found. This is done through the normal read path fromthe parent, so differing object sizes between parents and childrendo not matter.

Before a write to an object is performed, the object is checked forexistence. If it doesn’t exist, a copy-up operation is performed,which means reading the relevant range of data from the parentsnapshot and writing it (plus the original write) to the childimage. To prevent races with multiple writes trying to copy-up thesame object, this copy-up operation will include an atomic create. Ifthe atomic create fails, the original write is done instead. Thiscopy-up operation is implemented as a class method so that extrametadata can be stored by it in the future. In trivial layering, thecopy-up operation copies the entire range needed to the child object(that is, the full size of the child object). A future optimizationcould make this copy-up more fine-grained.

Another future optimization could be storing a bitmap of which objectsactually exist in a child. This would obviate the check for existencebefore each write, and let reads go directly to the parent if needed.

These optimizations are discussed in:

http://marc.info/?l=ceph-devel&m=129867273303846

Parent/Child relationships

Children store a reference to their parent in their header, as a tupleof (pool id, image id, snapshot id). This is enough information toopen the parent and read from it.

In addition to knowing which parent a given image has, we want to beable to tell if a protected snapshot still has children. This isaccomplished with a new per-pool object, rbd_children, which maps(parent pool id, parent image id, parent snapshot id) to a list ofchild image ids. This is stored in the same pool as the child imagebecause the client creating a clone already has read/write access toeverything in this pool, but may not have write access to the parent’spool. This lets a client with read-only access to one pool clone asnapshot from that pool into a pool they have full access to. Itincreases the cost of unprotecting an image, since this needs to checkfor children in every pool, but this is a rare operation. It wouldlikely only be done before removing old images, which is already muchmore expensive because it involves deleting every data object in theimage.

Protection

Internally, protection_state is a field in the header object thatcan be in three states. “protected”, “unprotected”, and“unprotecting”. The first two are set as the result of “rbdprotect/unprotect”. The “unprotecting” state is set while the “rbdunprotect” command checks for any child images. Only snapshots in the“protected” state may be cloned, so the “unprotected” state preventsa race like:

  • A: walk through all pools, look for clones, find none

  • B: create a clone

  • A: unprotect parent

  • A: rbd snap rm pool/parent@snap

Resizing

Resizing an rbd image is like truncating a sparse file. New space istreated as zeroes, and shrinking an rbd image deletes the contentsbeyond the old bounds. This means that if you have a 10G image full ofdata, and you resize it down to 5G and then up to 10G again, the last5G is treated as zeroes (and any objects that held that data wereremoved when the image was shrunk).

Layering complicates this because the absence of an object no longerimplies it should be treated as zeroes - if the object is part of aclone, it may mean that some data needs to be read from the parent.

To preserve the resizing behavior for clones, we need to keep track ofwhich objects could be stored in the parent. We can track this as theamount of overlap the child has with the parent, since resizing onlychanges the end of an image. When a child is created, its overlapis the size of the parent snapshot. On each subsequent resize, theoverlap is min(overlap, new_size). That is, shrinking the imagemay shrinks the overlap, but increasing the image’s size does notchange the overlap.

Objects that do not exist past the overlap are treated as zeroes.Objects that do not exist before that point fall back to readingfrom the parent.

Since this overlap changes over time, we store it as part of themetadata for a snapshot as well.

Renaming

Currently the rbd header object (that stores all the metadata about animage) is named after the name of the image. This makes renamingdisrupt clients who have the image open (such as children reading froma parent). To avoid this, we can name the header object by theid of the image, which does not change. That is, the name of theheader object could be rbd_header.$id, where $id is a unique id forthe image in the pool.

When a client opens an image, all it knows is the name. There isalready a per-pool rbd_directory object that maps image names toids, but if we relied on it to get the id, we could not open anyimages in that pool if that single object was unavailable. To avoidthis dependency, we can store the id of an image in an object calledrbd_id.$image_name, where $imagename is the name of the image. Theper-pool _rbd_directory object is still useful for listing all imagesin a pool, however.

Header changes

The header needs a few new fields:

  • int64_t parent_pool_id

  • string parent_image_id

  • uint64_t parent_snap_id

  • uint64_t overlap (how much of the image may be referring to the parent)

These are stored in a “parent” key, which is only present if the imagehas a parent.

cls_rbd

Some new methods are needed:

  1. /***************** methods on the rbd header *********************/
  2. /**
  3. * Sets the parent and overlap keys.
  4. * Fails if any of these keys exist, since the image already
  5. * had a parent.
  6. */
  7. set_parent(uint64_t pool_id, string image_id, uint64_t snap_id)
  8.  
  9. /**
  10. * returns the parent pool id, image id, snap id, and overlap, or -ENOENT
  11. * if parent_pool_id does not exist or is -1
  12. */
  13. get_parent(uint64_t snapid)
  14.  
  15. /**
  16. * Removes the parent key
  17. */
  18. remove_parent() // after all parent data is copied to the child
  19.  
  20. /*************** methods on the rbd_children object *****************/
  21.  
  22. add_child(uint64_t parent_pool_id, string parent_image_id,
  23. uint64_t parent_snap_id, string image_id);
  24. remove_child(uint64_t parent_pool_id, string parent_image_id,
  25. uint64_t parent_snap_id, string image_id);
  26. /**
  27. * List ids of a given parent
  28. */
  29. get_children(uint64_t parent_pool_id, string parent_image_id,
  30. uint64_t parent_snap_id, uint64_t max_return,
  31. string start);
  32. /**
  33. * list parent
  34. */
  35. get_parents(uint64_t max_return, uint64_t start_pool_id,
  36. string start_image_id, string start_snap_id);
  37.  
  38.  
  39. /************ methods on the rbd_id.$image_name object **************/
  40.  
  41. set_id(string id)
  42. get_id()
  43.  
  44. /************** methods on the rbd_directory object *****************/
  45.  
  46. dir_get_id(string name);
  47. dir_get_name(string id);
  48. dir_list(string start_after, uint64_t max_return);
  49. dir_add_image(string name, string id);
  50. dir_remove_image(string name, string id);
  51. dir_rename_image(string src, string dest, string id);

Two existing methods will change if the image supportslayering:

  1. snapshot_add - stores current overlap and has_parent with
  2. other snapshot metadata (images that don't have
  3. layering enabled aren't affected)
  4.  
  5. set_size - will adjust the parent overlap down as needed.

librbd

Opening a child image opens its parent (and this will continuerecursively as needed). This means that an ImageCtx will contain apointer to the parent image context. Differing object sizes won’tmatter, since reading from the parent will go through the parentimage context.

Discard will need to change for layered images so that it onlytruncates objects, and does not remove them. If we removed objects, wecould not tell if we needed to read them from the parent.

A new clone method will be added, which takes the same arguments ascreate except size (size of the parent image is used).

Instead of expanding the rbd_info struct, we will break the metadataretrieval into several API calls. Right now, the only users ofrbd_stat() other than ‘rbd info’ only use it to retrieve image size.