cls_rbd_client: fix locking function indentation

Greg's editor was set to spaces only.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
cls_rbd: return negative pool id if parent does not exist

This lets us use get_parent in a multi-op transaction without getting
-ENOENT for the entire transaction.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
cls_rbd: make get_parent return valid data when layering is disabled

This means clients can treat an error in their multi-object
transaction as a failure for all of them. This makes the client side
much simpler since it can call get_parent on images that don't support
layering, or do support it but don't have parents, and not need to
check the return value of every operation in the transaction.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: rename variables for more consistency

Rename: poolname -> pool_name
        snapname -> snap_name
        snapid   -> snap_id

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: move watch/unwatch to ImageCtx methods

This fixes close_image() on a partially initialized ImageCtx, and
paves the way for opening parent images without watching their
headers.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: allow an image to be opened without watching

Watching the header of a parent image could produce unreasonable
delays. If hundreds of child images watch the same parent, taking a
snapshot or resizing the parent would wait until all the children are
notified. Since the children are based on snapshots, they don't care
about any changes to the current version of the parent image, and
don't need to re-read the header on each change. Nothing children need
to access their parent snapshot will change.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: store parent info in snapshot metadata

If an image is open to a snapshot, return the features or overlap from
that snapshot.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: allow opening an image by id

If we have an ImageCtx with an id set, don't bother setting the name.
Just go directly to the header object in ImageCtx::init().

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: move all side-effects to snap_{un}set()

All callers need to do this anyway.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: open parent image when opening a child

Extract a helper out of get_parent_info. The parent may become unset
while the child is open, so detect changes in it during ictx_refresh().
Don't watch the parent image, since we only care about the read-only
snapshot the child references, which cannot change.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: drop unnecessary std:: and struct prefixes

Most of this file do not use them.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
cls_rbd, cls_rbd_client, test_cls_rbd: copyup method

Fixes: #2559
Signed-off-by: Dan Mick <dan.mick@inktank.com>
librbd, rbd.py, test_rbd.py: flatten methods

librbd:
- copyup_block for individual blocks (for read/write path)
- flatten for iterating across whole image (for rbd cli)

rbd.py:
Image.flatten (whole image)

Fixes: #2606
Signed-off-by: Dan Mick <dan.mick@inktank.com>
Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: round #blocks in image_info() for edge cases

Signed-off-by: Dan Mick <dan.mick@inktank.com>
rbd: flatten command (copyup an entire image)

Fixes: #2561
Signed-off-by: Dan Mick <dan.mick@inktank.com>
librbd: split into multiple files

This was long overdue. There's still a bunch of stuff in internal.cc
that should be moved elsewhere, but this is a good start.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
qa: remove rbd image when done

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
qa: add script to run rbd/librbd tests

Run on old and new style images, with different features. This is
intended to ease development, as opposed to being part of the qa
suite. It should be run from the src directory.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
cls_rbd: don't duplicate encoding

Just skip the parts before encoding instead of returning early.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
librbd: make ImageCtx methods take snap_id parameters

This makes it easier to use without racing with snap_set.
Requests in the cache, for example, store their snap_id
and may not be sent for a long time after being created.

Also rename the parameters for these methods so they
don't alias member variables.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>