Add branch coverage for lcov-1.10 or above

lcov-1.10 made branch coverage off by default. So, this enable it if
lcov-1.10 or above.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Make apply_defered_bfree() static

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Fix shadow_bitmap_modify() in fsck

shadow_bitmap_modify() is missing to consider about mapoffset when
calculate modification length.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Add REQ_META annotation for volmap

This allow to pass additional bio flags when flushing.

By using it, this also adds REQ_META annotation for sb, bitmap data,
volmap, logmap. With this, we can see REQ_META via blktrace data.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Fix missing tux3_end_backend() on do_commit() for error path

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
More verbose if -ENOSPC error

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Remove unnecesarry I_DIRTY in tux3_clear_dirty_inode_nolock()

If tux3_clear_dirty_inode_nolock() was called for frontend delta,
inode should has I_DIRTY already. Otherwise, by following race, delta
dirty and I_DIRTY become inconsistent state.

       cpu0                                 cpu1
__tux3_mark_inode_dirty()
    tux3_dirty_inode()
        mark delta0 dirty
                                     tux3_clear_dirty_inode_nolock()
                                          clear I_DIRTY and delta0
    __mark_inode_dirty()
        mark I_DIRTY

Therefore, tux3_clear_dirty_inode_nolock() should be called only for
backend delta, i.e. inode should be marked as I_DIRTY.  So, this patch
removes unnecessary "inode->i_state |= I_DIRTY".

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Fix inode->dirtied_when in the case of re-dirty

__mark_inode_dirty() doesn't know about delta boundary (we don't clear
I_DIRTY before flush, in order to prevent the inode to be freed). So,
if inode was re-dirtied for frontend delta while flushing old delta,
->dirtied_when may not be updated by __mark_inode_dirty() forever.

Although we don't use ->dirtied_when, bdi flusher uses ->dirtied_when
to decide flush timing, so we have to update ->dirtied_when ourself.

           cpu0                                      cpu1

__tux3_mark_inode_dirty()
    tux3_dirty_inode()
    __mark_inode_dirty()
    /* core dirty is still dirty */
                                      __tux3_clear_dirty_inode()
				      /* clear I_DIRTY and backend dirty */

So, if inode is not marked frontend dirty but was still marked as
I_DIRTY, this patch updates inode->dirtied_when (like __mark_inode_dirty()).

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Remove mark_buffer_dirty() in tux3_set_buffer_dirty_list()

In previous patch, we added re-dirty detection.

But tux3_set_buffer_dirty_list() is calling mark_buffer_dirty() like
below, so we can't detect re-dirty properly. Because,
mark_buffer_dirty() can set I_DIRTY_PAGES without our ->dirty_inode()
hook.

    tux3_set_buffer_dirty_list()
        mark_buffer_dirty()
            __set_page_dirty()
	        __mark_inode_dirty(I_DIRTY_PAGES)
    __tux3_mark_inode_dirty(I_DIRTY_PAGES)

To fix this, opencode mark_buffer_dirty(), then remove
__mark_inode_dirty() from tux3_set_buffer_dirty_list(). Instead, it
returns 1 or 0, to tell whether caller needs to call
__tux3_mark_inode_dirty().

With this changes, we can avoid to call unnecessary
__tux3_mark_inode_dirty() too.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Add sb parameter to defer_bfree()

To add sanity check for defer_bfree(), this adds "sb" parameter to
defer_bfree().

With this check, we can notify the bug early, not after
commit. Already, some bugs found on tests/* with this.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Fix comment in seg_alloc()

-ENOSPC in backend is the bug of block reservation of frontend.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Implement dleaf2_dump()

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Use tux3_dbg() in ->leaf_dump()

trace() can be disabled by options. But ->leaf_dump() wants to dump
always.

So, this uses __tux3_dbg(), instead of trace_on(). [However, those are
checking tux3_trace option for now. We should remove this check?]

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Consolidate paranoia check for dleaf2 sentinel

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Use rq->seg_cnt, instead of rq->seg_idx for dleaf2_read()

For now, we are using rq->seg_idx to know the result of
dleaf2_read(). However, it is not consists with dleaf2_write().

So, this uses rq->seg_cnt to return the number of seg[] which is read,
instead of rq->seg_idx.

After this, we can know available seg[] from rq->seg_cnt by both of
read/write.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Fix partial allocation and rewrite dleaf2_write()

Current dleaf2_write() is assuming rq->seg_alloc() can allocate the
seg[] to fit dleaf2's space. However, of course, it is not true.

Furthermore, current implementation has some limitations (e.g. can't
write across leaves, assuming to read existing seg[] by dleaf2_read()
as preparation, inefficient overwrite mode).

So, this rewrites dleaf2_write(). Strategy is,

	1) Find start dex position.
	2) Find end dex position.
	3) Allocates seg[].
	4a) If seg[] is fit to dleaf2 space, apply seg[] to dleaf2. Done.

	4b) If seg[] is not fit to dleaf2 or there is no seg[] space, we do
	    (2) again after shrinking key region.
	5) If some seg[] was fit to dleaf2 space, do split. Then, do from (1)
	   again.

Note for seg[] shrinking here,

New helpers

	- ->seg_find() finds available blocks from block bitmap.
	- ->seg_alloc() commits allocation of blocks found by ->seg_find()
	- ->seg_free() frees blocks replaced by new seg[]

To do shrink for (4b), we uses ->seg_find() and ->seg_alloc() pair to
avoid to modify bitmap by temporary state.

With this new implementation, we can write across multiple leaves by
one btree_write() call. And IMO, new implement is simpler than older
one.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Optimize dleaf2_lookup_index() for end of seg[] in dleaf2_write()

New dleaf2_write() looks up extents in dleaf2 multiple times - start
of seg[], end of seg[], end of seg[] after shrinking seg[].

We don't need to the extent from all extents in dleaf2. Because, for
example, end of seg[] never be before start.

So, this optimizes by limiting the range to lookup the extent by
dleaf2_lookup_index().

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Optimize split position for append write in dleaf2_write()

Append write is common operation, and if append write, split at center
is not optimal for leaf split.

So, this splits leaf at sentinel, if leaf is most right and key is >=
sentinel.

FIXME1: we should apply this for bnode too.
FIXME2: this changes split position only if most right leaf. Should we
split at sentinel by filling hole too?

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Implement ->seg_find() and ->seg_alloc() for new dleaf2_write()

This implements ->seg_find() and ->seg_alloc() for new dleaf2_write().

FIXME: current implement is using bfree() to shrink seg[]. However, we
should avoid to modify bitmap. For example, ->seg_find() just check
available blocks, and ->seg_alloc() modify bitmap actually.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Add support to write across multiple leaves to btree_write()

New dleaf2_write() remains key->len if didn't write on current leaf.
To write remaining key, this calls cursor_advance() before dirty leaf.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>