rbd: add new snapshots at the tail

This fixes a bug that went in with this commit:

    commit f6e0c99092cca7be00fca4080cfc7081739ca544
    Author: Alex Elder <elder@inktank.com>
    Date:   Thu Aug 2 11:29:46 2012 -0500
    rbd: simplify __rbd_init_snaps_header()

The problem is that a new rbd snapshot needs to go either after an
existing snapshot entry, or at the *end* of an rbd device's snapshot
list.  As originally coded, it is placed at the beginning.  This was
based on the assumption the list would be empty (so it wouldn't
matter), but in fact if multiple new snapshots are added to an empty
list in one shot the list will be non-empty after the first one is
added.

This addresses http://tracker.newdream.net/issues/3063

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: rename rbd_id_get()

This should have been done as part of this commit:

    commit de71a2970d57463d3d965025e33ec3adcf391248
    Author: Alex Elder <elder@inktank.com>
    Date:   Tue Jul 3 16:01:19 2012 -0500
    rbd: rename rbd_device->id

rbd_id_get() is assigning the rbd_dev->dev_id field.  Change the
name of that function as well as rbd_id_put() to more obviously
reflect what they are affecting.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: kill rbd_dev->q

A copy of rbd_dev->disk->queue is held in rbd_dev->q, but it's
never actually used.  So get just get rid of the field.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: move locking out of rbd_header_set_snap()

Move the calls to get the header semaphore out of
rbd_header_set_snap() and into its caller.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: rename __rbd_init_snaps_header()

The name __rbd_init_snaps_header() doesn't really convey what that
function does very well.  Its purpose is to scan a new snapshot
context and either create or destroy snapshot device entries so
that local host's view is consistent with the reality maintained
on the OSDs.  This patch just changes the name of this function,
to be rbd_dev_snap_devs_update().

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: simplify snap_by_name() interface

There is only one caller of snap_by_name(), and it passes two values
found within an rbd device structure.

Change the interface so it just passes the address of the rbd_dev,
and use local variables to hold frequently-referenced values derived
from that.

While in this function, rename the "seq" argument to be "snap_id" to
be consistent with usage elsewhere.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: kill rbd_image_header->total_snaps

The "total_snaps" field in an rbd header structure is never any
different from the value of "num_snaps" stored within a snapshot
context.  Avoid any confusion by just using the value held within
the snapshot context, and get rid of the "total_snaps" field.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: record mapped size

There are several fields in an rbd_device structure that reflect
the state of the current mapping, which can be different from the
underlying rbd image (if a snapshot is mapped).  Add to that set
of fields the size of the mapped image, and use it when setting the
capacity of the disk.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: separate changeable fields

This is a silly patch that will probably go away or will get merged
into something else.  It just separates the fields that never change
in an rbd image header structure from the filds that do.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: do some header initialization earlier

Move some of the code that initializes an rbd header out of
rbd_init_disk() and into its caller.

Move the code at the end of rbd_init_disk() that sets the device
capacity and activates the Linux device out of that function and
into the caller, ensuring we still have the disk size available
where we need it.

Update rbd_free_disk() so it still aligns well as an inverse of
rbd_init_disk(), moving the rbd_header_free() call out to its
caller.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: simplify rbd_init_disk() a bit

This just simplifies a few things in rbd_init_disk(), now that the
previous patch has moved a bunch of initialization code out if it.
Done separately to facilitate review.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: expand lock protection in rbd_add()

Expand the region of code in rbd_add() protected by the header
semaphore to include the complete initialization sequence.  It may
not be strictly necessary, but it doesn't hurt.  And with the
upcoming changes to the order of steps here this offers easy
protection.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: don't register snapshots in bus_add_dev()

When rbd_bus_add_dev() is called (one spot--in rbd_add()), the rbd
image header has not even been read yet.  This means that the list
of snapshots will be empty at the time of the call.  As a result,
there is no need for the code that calls rbd_register_snap_dev()
for each entry in that list--so get rid of it.

Once the header has been read (just after returning), a call will
be made to rbd_dev_snap_devs_update(), which will then find every
snapshot in the context to be new and will therefore call
rbd_register_snap_dev() via __rbd_add_snap_dev() accomplishing
the same thing.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: assign header name later

Move the assignment of the header name for an rbd image a bit later,
outside rbd_add_parse_args() and into its caller.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: call set_snap() before snap_devs_update()

rbd_header_set_snap() is a simple initialization routine for 3-4
fields in a struct rbd_dev.  It has to be called after the snapshot
context for the rbd_dev has been initialized.  Call it right after
the header has been read.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: read the header before registering device

Read the rbd header information and call rbd_header_set_snap()
earlier--before registering the block device or setting up the
sysfs entries for the image.  The latter action provide access to
some of the information that's only available after doing this, so
this will make sure it's valid right away.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: defer setting device id

Hold off setting the device id and formatting the device name
in rbd_add() until just before it's needed.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: call rbd_init_disk() sooner

Call rbd_init_disk() from rbd_add() as soon as we have the major
device number for the mapping.

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: set initial capacity in rbd_init_disk()

Move the setting of the initial capacity for an rbd image mapping
into rb_init_disk().

Signed-off-by: Alex Elder <elder@inktank.com>
rbd: set up watch before announcing disk

We're ready to handle header object (refresh) events at the point we
call rbd_bus_add_dev().  Set up the watch request on the rbd image
header right after that, just before we force an update via the
call to rbd_dev_snap_devs_update().

Signed-off-by: Alex Elder <elder@inktank.com>