MonClient: use thread-safe RNG for picking monitors

Avoid using shared-state rand() when picking monitors.  This way we don't
screw with library users like test_librbd_fsx that rely on srand() and
rand() being deterministic.

Signed-off-by: Sage Weil <sage@inktank.com>
librbd: complete Context on error from ObjectCacher::readx

We were immediately completing for positive (successful) returns, but not
on error.  Do errors too, so that we don't drop ENOENT.

Signed-off-by: Sage Weil <sage@inktank.com>
librbd/AioCompletion: ensure read result assembly happens last

When reading from the cache, we can have a situation where each AioRead
completes as it is added to the completion, causing the final buffer
assembly to happen before the other requests are added.  It needs to run
only after we are finished adding requests.  Move it into a helper and
ensure it is only called when pending_count == 0 *and* building == false.

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/Striper: more debug

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/WritebackHandler: add may_copy_on_write()

Implement a new method to tell us whether a read to a particular region
could be affected by a write-triggered copy-on-write.

Signed-off-by: Sage Weil <sage@inktank.com>
librbd: remove parent logic from reads beneath the cache

Make reads beneath the cache (in the WritebackHandler) read the plain
objects beneath us.  Let the read-from-parent logic *above* the cache
handle the layering logic.

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/ObjectCacher: add object exists flag

Clear it (set to false) on ENOENT.

Reset it (to true) on discard().
Reset it (to true) on write completion.
Reset it (to true) on release.

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/ObjectCacher: clear complete if may-cow and !exists

If the object doesn't/didn't exist, and we finish a write, and the write
may have triggered a copy-on-write, clear the complete flag, since there
may be new data beneath us that we didn't see before.

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/ObjectCacher: remove BufferHeads on ENOENT

If we try a read and the object doesn't exist, toss out the BufferHead
(instead of keeping useless ZERO bh's around).

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/ObjectCacher: return ENOENT

If we are given a single ObjectExtent, and the object doesn't exist,
and we don't have any dirty|tx buffers, return ENOENT to the caller.

This lets librbd's layering logic do its thing.

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/ObjectCacher: trigger copy-on-write before read

If we are trying to read from an object, and it doesn't exist, but we have
dirty buffers, and a write could trigger copy-on-write, then flush our
write first to trigger the copy-on-write before we proceed with the read.
This ensures that a pattern like

CLONE
WRITE 0~10  (into cache)
READ  10~20

will flush the write and trigger a cow so that the read will see parent
data.

Signed-off-by: Sage Weil <sage@inktank.com>
test_librbd: add additoinal clone test

Test that reads will trigger a cow both when overlapping and not
overlapping with buffered writes.

Signed-off-by: Sage Weil <sage@inktank.com>
osdc/ObjectCacher: wake all rx waiters on ENOENT

If we discover an object doesn't exist, wake all rx waiters at the same
time.  This prevents a situation like:

- read extent A
- read extent B
- reply from A, ENOENT, exists=false
- read extent B (again), immediately tell caller ENOENT
- reply from B, ENOENT, tell caller ENOENT

Fixes: #3435
Signed-off-by: Sage Weil <sage@inktank.com>
librbd: better debug output

Signed-off-by: Sage Weil <sage@inktank.com>