Skip to contentSkip to conversation

DRBD: a block device for HA clusters

PATCH In review Philipp Reisner 14 participants 84 messages Apr 30, 2009 – May 6, 2009 Page 2 of 2

Subsystems (3)
lore
Share & tools
lore
#30 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

In reply to Philipp Reisner
Quoted 4 lines · On Thu, 30 Apr 2009 13:26:38 +0200 Philipp Reisner…

The lru_cache is a fixed size cache of equal sized objects. It allows its users to do arbitrary transactions in case an element in the cache needs to be replaced. Its replacement policy is LRU.

None of this really looks drbd-specific.

Would it not be better to present this as a general library function? lib/lru_cache.c?

I think I might have asked this before. If I did, then thwap-to-you for not permanently answering it in the changelog ;)

drivers/block/drbd/lru_cache.h line 114

Quoted 7 lines

...

drivers/block/drbd/lru_cache.h new file +4 -0
+#define lc_e_base(lc)  ((char *)((lc)->slot + (lc)->nr_elements))
+#define lc_entry(lc, i) ((struct lc_element *) \
+		       (lc_e_base(lc) + (i)*(lc)->element_size))
+#define lc_index_of(lc, e) (((char *)(e) - lc_e_base(lc))/(lc)->element_size)

The macros reference their arguments multiple times and hence are inefficient and/or buggy and/or unpredictable when passed an expression with side-effects.

If possible this should be fixed by turning them into regular C functions. Inlined C functions if that makes sense (it frequently doesn't).

A pleasing side-effect of this conversion is that for some reason developers are more likely to document C functions than they are macros (hint).

I don't understand what these macros are doing and can't be bothered reverse-engineering the code to work that out. But all the typecasting looks fishy.

Quoted 19 lines

...

drivers/block/drbd/lru_cache.c new file +16 -0
+static inline void lc_init(struct lru_cache *lc,
+		const size_t bytes, const char *name,
+		const unsigned int e_count, const size_t e_size,
+		void *private_p)
+{
+	struct lc_element *e;
+	unsigned int i;
+
+	BUG_ON(!e_count);
+
+	memset(lc, 0, bytes);
+	INIT_LIST_HEAD(&lc->in_use);
+	INIT_LIST_HEAD(&lc->lru);
+	INIT_LIST_HEAD(&lc->free);
+	lc->element_size = e_size;
+	lc->nr_elements  = e_count;

drivers/block/drbd/lru_cache.c line 68

Quoted 10 lines
drivers/block/drbd/lru_cache.c new file +10 -0
+	lc->new_number	 = -1;
+	lc->lc_private   = private_p;
+	lc->name         = name;
+	for (i = 0; i < e_count; i++) {
+		e = lc_entry(lc, i);
+		e->lc_number = LC_FREE;
+		list_add(&e->list, &lc->free);
+		/* memset(,0,) did the rest of init for us */
+	}
+}

How's about you remove all `inline' keywords from the whole patchset and then go back and inline the functions where there is a demonstrable benefit? This function won't be one of them!

drivers/block/drbd/lru_cache.c line 97

Quoted 11 lines

...

drivers/block/drbd/lru_cache.c new file +8 -0
+/**
+ * lc_free: Frees memory allocated by lc_alloc.
+ * @lc: The lru_cache object
+ */
+void lc_free(struct lru_cache *lc)
+{
+	vfree(lc);
+}

vmalloc() is a last-resort thing. It generates slower-to-access memory and can cause internal fragmentation of the vmalloc arena, leading to total machine failure.

Can it be avoided? Often it _can_ be avoided, and the code falls back to vmalloc() if the more robust memory allocation schemes failed.

Quoted 5 lines

+/**
+ * lc_reset: does a full reset for @lc and the hash table slots.
+ * It is roughly the equivalent of re-allocating a fresh lru_cache object,
+ * basically a short cut to lc_free(lc); lc = lc_alloc(...);
+ */

Comment purports to be kerneldoc but doesn't document the formal argument.

Quoted 20 lines
drivers/block/drbd/lru_cache.c new file +20 -0
+void lc_reset(struct lru_cache *lc)
+{
+	lc_init(lc, size_of_lc(lc->nr_elements, lc->element_size), lc->name,
+			lc->nr_elements, lc->element_size, lc->lc_private);
+}
+
+size_t	lc_printf_stats(struct seq_file *seq, struct lru_cache *lc)
+{
+	/* NOTE:
+	 * total calls to lc_get are
+	 * (starving + hits + misses)
+	 * misses include "dirty" count (update from an other thread in
+	 * progress) and "changed", when this in fact lead to an successful
+	 * update of the cache.
+	 */
+	return seq_printf(seq, "\t%s: used:%u/%u "
+		"hits:%lu misses:%lu starving:%lu dirty:%lu changed:%lu\n",
+		lc->name, lc->used, lc->nr_elements,
+		lc->hits, lc->misses, lc->starving, lc->dirty, lc->changed);
+}

drivers/block/drbd/lru_cache.c line 133

Quoted 10 lines
drivers/block/drbd/lru_cache.c new file +10 -0
+
+static unsigned int lc_hash_fn(struct lru_cache *lc, unsigned int enr)
+{
+	return enr % lc->nr_elements;
+}
+
+
+/**
+ * lc_find: Returns the pointer to an element, if the element is present
+ * in the hash table. In case it is not this function returns NULL.

Unfortunately the above must be done in a single 140 column line - kerneldoc doesn't understand leading lines which have a newline in the middle.

Please review all kerneldoc comments in the patchset - I won't commeent on them further.

Quoted 9 lines

+ * @lc: The lru_cache object
+ * @enr: element number
+ */

drivers/block/drbd/lru_cache.c new file +5 -0
+struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
+{
+	struct hlist_node *n;
+	struct lc_element *e;
+

drivers/block/drbd/lru_cache.c

Quoted 10 lines
drivers/block/drbd/lru_cache.c new file +8 -0
+	BUG_ON(!lc);
+	hlist_for_each_entry(e, n, lc->slot + lc_hash_fn(lc, enr), colision) {
+		if (e->lc_number == enr)
+			return e;
+	}
+	return NULL;
+}
+
 
...

So I assume that the caller of this facility must provide the locking for its internals. Is that documented somewhere?

maintainer
#31 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 11 lines · Andrew Morton
Quoted 4 lines · On Thu, 30 Apr 2009 13:26:38 +0200 Philipp Reisner…

The lru_cache is a fixed size cache of equal sized objects. It allows its users to do arbitrary transactions in case an element in the cache needs to be replaced. Its replacement policy is LRU.

None of this really looks drbd-specific.

Would it not be better to present this as a general library function? lib/lru_cache.c?

yes. will do.

Quoted 19 lines

I think I might have asked this before. If I did, then thwap-to-you for not permanently answering it in the changelog ;)

drivers/block/drbd/lru_cache.h line 114

Quoted 7 lines

...

drivers/block/drbd/lru_cache.h new file +4 -0
+#define lc_e_base(lc)  ((char *)((lc)->slot + (lc)->nr_elements))
+#define lc_entry(lc, i) ((struct lc_element *) \
+		       (lc_e_base(lc) + (i)*(lc)->element_size))
+#define lc_index_of(lc, e) (((char *)(e) - lc_e_base(lc))/(lc)->element_size)

The macros reference their arguments multiple times and hence are inefficient and/or buggy and/or unpredictable when passed an expression with side-effects.

If possible this should be fixed by turning them into regular C functions. Inlined C functions if that makes sense (it frequently doesn't).

Quoted 7 lines

A pleasing side-effect of this conversion is that for some reason developers are more likely to document C functions than they are macros (hint).

I don't understand what these macros are doing and can't be bothered reverse-engineering the code to work that out. But all the typecasting looks fishy.

I have a half finished rewrite of that piece code anyways, so I guess I'll have to finish it then.

basically it allocates one continguous memory block, to avoid additional small allocations and pointer arrays.

in memory structure is

struct lru_cache {
        struct list_head active;
        struct list_head quiet;
        struct list_head free;
        size_t element_size;          <-- parameter to "lc_alloc"
        unsigned int  nr_elements;    <-- parameter to "lc_alloc"
        unsigned int  new_number;

        unsigned int used;
        unsigned long flags;
        unsigned long hits, misses, starving, dirty, changed;

        struct lc_element *changing_element; /* just for paranoia */

        const char *name;

        struct hlist_head slot[0];
        /* hash colision chains here, then element storage. */
};

so we have fixed size list heads,
size of a single such "element", to allow the user to add small payload;
number of hash slots and "elements" following this header; some counters;
hlist_slot[0];
}
following:
struct hlist_head[nr_elements];
array of element_size blobs[nr_elements];

these "blobs" start with the struct lru_element,
possibly followed by some user payload.

the "index" you are asking about later is
index into that "blob" array,
and is used primarily to initialize the state of this thing from an on-disk representation (the "activity log", "AL"), for crash recovery purposes.

the typecasting is necessary to get from the slot[0] to the "elements" skipping the hash slots.
using "container of" or something like that would obscure the fact that, as currently implemented, the "lru_element" _must_ be the first member of any payload structure. but see below.

Quoted 1 line
Quoted 1 line

+static inline void lc_init(struct lru_cache *lc,

<24 lines/>

Quoted 5 lines
Quoted 1 line

+}

How's about you remove all `inline' keywords from the whole patchset and then go back and inline the functions where there is a demonstrable benefit? This function won't be one of them!

will do.

Quoted 13 lines

drivers/block/drbd/lru_cache.c line 97

Quoted 8 lines
drivers/block/drbd/lru_cache.c new file +8 -0
+/**
+ * lc_free: Frees memory allocated by lc_alloc.
+ * @lc: The lru_cache object
+ */
+void lc_free(struct lru_cache *lc)
+{
+	vfree(lc);
+}

vmalloc() is a last-resort thing. It generates slower-to-access memory and can cause internal fragmentation of the vmalloc arena, leading to total machine failure.

Quoted 2 lines

Can it be avoided? Often it _can_ be avoided, and the code falls back to vmalloc() if the more robust memory allocation schemes failed.

yes, it can be avoided.
it used to be a kmalloc. but the mentioned "single memory block" implementation hit some kmalloc limit, and we have then been lazy, going the simple "two-line-patch" way.

we'll go for separate, and independently kmalloc'ed,

	struct hlist_head *slot;
	struct lru_element *blob;

then, or maybe for "struct page *blob_pages;" instead.

Quoted 2 lines

Please review all kerneldoc comments in the patchset - I won't commeent on them further.

will do.

Quoted 17 lines
Quoted 17 lines

+ * @lc: The lru_cache object
+ * @enr: element number
+ */

drivers/block/drbd/lru_cache.c new file +13 -0
+struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
+{
+	struct hlist_node *n;
+	struct lc_element *e;
+
+	BUG_ON(!lc);
+	hlist_for_each_entry(e, n, lc->slot + lc_hash_fn(lc, enr), colision) {
+		if (e->lc_number == enr)
+			return e;
+	}
+	return NULL;
+}
+
Quoted 6 lines
Quoted 2 lines

...

So I assume that the caller of this facility must provide the locking for its internals. Is that documented somewhere?

in my half-finished rewrite ;)

Thanks,

Lars

#32 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 42 lines · On Sat, 2 May 2009 17:26:20 +0200 Lars Ellenberg <[email protected]>…

in memory structure is

struct lru_cache {
        struct list_head active;
        struct list_head quiet;
        struct list_head free;
        size_t element_size;          <-- parameter to "lc_alloc"
        unsigned int  nr_elements;    <-- parameter to "lc_alloc"
        unsigned int  new_number;

        unsigned int used;
        unsigned long flags;
        unsigned long hits, misses, starving, dirty, changed;

        struct lc_element *changing_element; /* just for paranoia */

        const char *name;

        struct hlist_head slot[0];
        /* hash colision chains here, then element storage. */
};

so we have fixed size list heads,
size of a single such "element", to allow the user to add small payload;
number of hash slots and "elements" following this header; some counters;
hlist_slot[0];
}
following:
struct hlist_head[nr_elements];
array of element_size blobs[nr_elements];

these "blobs" start with the struct lru_element,
possibly followed by some user payload.

the "index" you are asking about later is
index into that "blob" array,
and is used primarily to initialize the state of this thing from an on-disk representation (the "activity log", "AL"), for crash recovery purposes.

Quoted 5 lines

the typecasting is necessary to get from the slot[0] to the "elements" skipping the hash slots.
using "container of" or something like that would obscure the fact that, as currently implemented, the "lru_element" _must_ be the first member of any payload structure.

I still don't see why the lru_element must be the first member of the user's outer, containing structure.

maintainer
#33 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 44 lines · Andrew Morton
Quoted 42 lines · On Sat, 2 May 2009 17:26:20 +0200 Lars Ellenberg <[email protected]>…

in memory structure is

struct lru_cache {
        struct list_head active;
        struct list_head quiet;
        struct list_head free;
        size_t element_size;          <-- parameter to "lc_alloc"
        unsigned int  nr_elements;    <-- parameter to "lc_alloc"
        unsigned int  new_number;

        unsigned int used;
        unsigned long flags;
        unsigned long hits, misses, starving, dirty, changed;

        struct lc_element *changing_element; /* just for paranoia */

        const char *name;

        struct hlist_head slot[0];
        /* hash colision chains here, then element storage. */
};

so we have fixed size list heads,
size of a single such "element", to allow the user to add small payload;
number of hash slots and "elements" following this header; some counters;
hlist_slot[0];
}
following:
struct hlist_head[nr_elements];
array of element_size blobs[nr_elements];

these "blobs" start with the struct lru_element,
possibly followed by some user payload.

the "index" you are asking about later is
index into that "blob" array,
and is used primarily to initialize the state of this thing from an on-disk representation (the "activity log", "AL"), for crash recovery purposes.

Quoted 8 lines
Quoted 5 lines

the typecasting is necessary to get from the slot[0] to the "elements" skipping the hash slots.
using "container of" or something like that would obscure the fact that, as currently implemented, the "lru_element" _must_ be the first member of any payload structure.

I still don't see why the lru_element must be the first member of the user's outer, containing structure.

ok, arguably one could also record the offset_of beneath the element_size, and add that in when doing the lc_element *e = blob[index] + offset. would not make it much more appealing, though.

Lars

#34 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 51 lines · On Sat, 2 May 2009 20:13:12 +0200 Lars Ellenberg <[email protected]>…
Quoted 50 lines · Andrew Morton
Quoted 47 lines · On Sat, 2 May 2009 17:26:20 +0200 Lars Ellenberg <[email protected]>…

in memory structure is

struct lru_cache {
        struct list_head active;
        struct list_head quiet;
        struct list_head free;
        size_t element_size;          <-- parameter to "lc_alloc"
        unsigned int  nr_elements;    <-- parameter to "lc_alloc"
        unsigned int  new_number;

        unsigned int used;
        unsigned long flags;
        unsigned long hits, misses, starving, dirty, changed;

        struct lc_element *changing_element; /* just for paranoia */

        const char *name;

        struct hlist_head slot[0];
        /* hash colision chains here, then element storage. */
};

so we have fixed size list heads,
size of a single such "element", to allow the user to add small payload;
number of hash slots and "elements" following this header; some counters;
hlist_slot[0];
}
following:
struct hlist_head[nr_elements];
array of element_size blobs[nr_elements];

these "blobs" start with the struct lru_element,
possibly followed by some user payload.

the "index" you are asking about later is
index into that "blob" array,
and is used primarily to initialize the state of this thing from an on-disk representation (the "activity log", "AL"), for crash recovery purposes.

the typecasting is necessary to get from the slot[0] to the "elements" skipping the hash slots.
using "container of" or something like that would obscure the fact that, as currently implemented, the "lru_element" _must_ be the first member of any payload structure.

Quoted 8 lines
Quoted 2 lines

I still don't see why the lru_element must be the first member of the user's outer, containing structure.

ok, arguably one could also record the offset_of beneath the element_size, and add that in when doing the lc_element *e = blob[index] + offset. would not make it much more appealing, though.

You appear to believe that I understood the relevance of all the above text. I didn't ;)

Let's start again.

Why can't I do

struct foo {
	int x;
	struct lc_element lc;
	..
};

and then use the lru library code to handle my foo objects?

maintainer
#35 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 7 lines · Andrew Morton

You appear to believe that I understood the relevance of all the above text. I didn't ;)

Let's start again.

Why can't I do

Quoted 7 lines
struct foo {
	int x;
	struct lc_element lc;
	..
};

and then use the lru library code to handle my foo objects?

to do so, you'd
struct lru_cache *foo_cache =

	lc_alloc("dummy", 42, sizeof(struct foo), NULL);

that would alloc
sizeof(struct lru_cache)
+ 42 * sizeof(struct hlist_head)
+ 42 * sizeof(struct foo);

the number of elements in the cache is fixed for its lifetime, only the reference counts, position on the lru lists, and tracked "house numbers" change.

the lru code sometimes addresses the lc_element part of the object just via their index in this memory area, which is useful to initialize the elements from some on-disk recorded state.

it does so by assuming a struct lc_element
every sizeof(struct foo) bytes
after the initial 42 hlist slots.

there, that was it.

to make this generic, appart from adding some paranoia like a BUG_ON(sizeof(foo) < sizeof(lc_element)), and possibly rounding any "odd" element_size parameter to get proper alignment, it would need to pass in offset_of(struct foo, lc),
so it could do

#define lc_e_base(lc) \
        ((char *)((lc)->slot + (lc)->nr_elements))

static inline struct lc_element * lc_element_by_index(struct lru_cache *lc, unsigned int i) {
BUG_ON(i >= lc->nr_elements);
return (struct lc_element *) (lc_e_base(lc)
+ i * lc->element_size
+ lc->offset_of);
}

lc->offset_of is what is currently missing from the code. lc_element_by_index() is what currently is badly named lc_entry().

a real lc_entry would then become
#define lc_entry(ptr, type, member) \
container_of(ptr, type, member)

so what now is
-struct foo *bar = (struct foo *) lc_entry(foo_cache, 7);

would become
+struct lc_element *e = lc_element_by_index(foo_cache, 7); +struct foo *bar = lc_entry(e, struct foo, lc);

does that make sense?

any suggestions to split up the one vmalloc into several allocations? kmalloc? a bunch of zero order pages?
or are we reinventing the wheel?
(probably we do, anyways; but is that particular wheel already in kernel?)

Lars

#36 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

In reply to Philipp Reisner

After digging through this a bit, I would recommend rewriting this whole part. The part that definitely needs to go is the vmalloc() of your whole LRU at once.

Here's some code I've been fiddling around with for a while. It uses a kmem_cache and a shrinker callback (although it does need a small patch to the shrinker code) to provide a dynamically sized LRU list for fixed-size objects.

This hasn't even been compile-tested, and it probably has some minor locking errors, but it should have enough comments to make up for it. This explicitly does *NOT* have your custom hash-lookup code in it, with the premise that generally that part should be left up to whatever architecture best fits the user of the LRU list.

I did design it so that it is suitable for usage with a lockless-RCU hash-lookup. The user would need to make sure to specify SLAB_DESTROY_BY_RCU in the flags field and make sure to include some kind of generation counter (see the various docs on SLAB_DESTROY_BY_RCU). On the other hand it would also work fine with a single spinlock used to control updates of the lookup datastructure.

The user will need to use spin_trylock() or similar in their ->evict() callback, as that is called with the internal list spinlock held. On the other hand, if one CPU has their lookup table locked and is trying to lru_cache_get() an object, there's not a whole lot that another CPU can do to free anything referenced by that lookup table anyways. I don't see it as a big issue.

Nowhere-near-signed-off-by Kyle Moffett <[email protected]>
struct lru_cache_type {
	const char *name;
	size_t size, align;
	unsigned long flags;
	int seeks;

	/*
	 * Return nonzero if the passed object may be evicted (IE: freed).
	 * Otherwise return zero and it will be "touched" (IE: moved to the
	 * tail of the LRU) so that later scans will try other objects.
	 *
	 * If you return nonero from this function, "obj" will be immediately
	 * kmem_cache_free()d.
	 */
	unsigned int (*evict)(struct lru_cache *lc, void *obj);
};
struct lru_cache {
	const struct lru_cache_type *type;
	size_t offset;

	struct kmem_cache *cache;
	struct shrinker shrinker;

	/* The least-recently used item in the LRU is at the head */
	spinlock_t lock;
	struct list_head lru;
	struct list_head in_use;
	unsigned long nr_lru;
	unsigned long nr_in_use;
};
struct lru_cache_elem {
	/* The node is either on the LRU or on this in-use list */
	struct list_head node;
	const struct lru_cache *lc;
	atomic_t refcount;
}

/*
* FIXME: Needs a patch to make the shinker->shrink() function take an extra
* argument with the address of the "struct shrinker".
*/
static int lru_cache_shrink__(struct shrinker *s, int nr_scan, gfp_t flags)

{
	return lru_cache_shrink(container_of(s, struct lru_cache, shrinker),
				nr_scan, flags);
}

int lru_cache_create(struct lru_cache *lc, const struct lru_cache_type *type)

{
	/* Align the size so an lru_cache_elem can sit at the end */
	size_t align = MAX(type->align, __alignof__(struct lru_cache_elem));
	size_t size = ALIGN(type->size, __alignof__(struct lru_cache_elem));
	lc->offset = size;

	/* Now add space for that element */
	size += sizeof(struct lru_cache_elem);

	/* Initialize internal fields */
	lc->type = type;
	INIT_LIST_HEAD(&lc->lru);
	INIT_LIST_HEAD(&lc->in_use);
	lc->nr_lru = 0;
	lc->nr_in_use = 0;

	/* Allocate the fixed-sized-object cache */
	lc->cache = kmem_cache_create(type->name, size, align,
			type->flags, NULL);
	if (!lc->cache)
		return -ENOMEM;

	/* Now initialize and register our shrinker */
	lc->shrinker.shrink = &lru_cache_shrink;
	lc->shrinker.seeks = type->seeks;
	register_shrinker(&lc->shrinker);

	return 0;
}

/*
* Before you can call this function, you must free all of the objects on the
* LRU list (which in turn means they must all have zeroed refcounts), and
* you must ensure that no other functions will be called on this lru-cache. */
void lru_cache_destroy(struct lru_cache *lc)

{
	BUG_ON(lc->nr_lru);
	BUG_ON(lc->nr_in_use);
	BUG_ON(!list_empty(&lc->lru));
	BUG_ON(!list_empty(&lc->in_use));

	unregister_shrinker(&lc->shrinker);
	kmem_cache_destroy(lc->cache);
	lc->cache = NULL;
}

void *lru_cache_alloc(struct lru_cache *lc, gfp_t flags)

{
	struct lru_cache_elem *elem;
	void *obj = kmem_cache_alloc(lc->cache, flags);
	if (!obj)
		return NULL;

	elem = obj + lc->offset;
	atomic_set(&elem->refcount, 1);
	elem->lc = lc;
	smp_wmb();

	spin_lock(&lc->lock);
	list_add_tail(&elem->node, &lc->in_use);
	lc->in_use++;
	spin_unlock(&lc->lock);

	return obj;
}

/*
* You must ensure that the lru object has a zero refcount and can no longer
* be looked up before calling lru_cache_free(). Specifically, you must
* ensure that lru_cache_get() cannot be called on this object. */
void lru_cache_free(struct lru_cache *lc, void *obj)

{
	struct lru_cache_elem *elem = obj + lc->offset;
	BUG_ON(elem->lc != lc);

	spin_lock(&lc->lock);
	BUG_ON(atomic_read(&elem->refcount));
	list_del(&elem->node);
	lc->nr_lru--;
	spin_unlock(&lc->lock);

	kmem_cache_free(lc->cache, obj);
}

/*
* This may be called at any time between lru_cache_create() and
* lru_cache_destroy() by the shrinker code to reduce our memory usage. */
int lru_cache_shrink(struct lru_cache *lc, int nr_scan, gfp_t flags)

{
	struct lru_cache_elem *elem, *n;
	unsigned long nr_lru;
	int nr_left = nr_scan;

	spin_lock(&lc->lock);

	/* Try to scan the number of requested objects */
	list_for_each_entry_safe(elem, n, &lc->lru, node) {
		void *obj;
		if (!nr_left--)
			break;

		/* Sanity check */
		BUG_ON(atomic_read(&elem->refcount));

		/* Ask them if we can free this item */
		obj = ((void *)elem) - lc->offset;
		if (!lc->type->evict(lc, obj)) {
			/*
			 * They wouldn't let us free it, so move it to the
			 * other end of the LRU so we can keep scanning.
			 */
			list_del(&elem->node);
			list_add_tail(&elem->node, &lc->lru);
			continue;
		}

		/* Remove this node from the LRU and free it */
		list_del(&elem->node);
		lc->nr_lru--;
		kmem_cache_free(obj);
	}

	nr_lru = lc->nr_lru;
	spin_unlock(&lc->lock);

	/* Now if we were asked to scan, tell the kmem_cache to shrink */
	if (nr_scan)
		kmem_cache_shrink(lc->cache);

	return (nr_lru < (unsigned long)INT_MAX) ? (int)nr_lru : INT_MAX;
}

/* Use this function if you already have a reference to "obj" */ void *lru_cache_get__(struct lru_cache *lc, void *obj)

{
	struct lru_cache_elem *elem = obj + lc->offset;
	BUG_ON(elem->lc != lc);
	atomic_inc(&elem->refcount);
	return obj;
}

/*
* If you do not already have a reference to "obj", you must wrap the
* combined lookup + lru_cache_get() in rcu_read_lock/unlock(). */
void *lru_cache_get(struct lru_cache *lc, void *obj)

{
	struct lru_cache_elem *elem = obj + lc->offset;
	BUG_ON(elem->lc != lc);

	/* Fastpath:  If it's already referenced just add another one */
	if (atomic_inc_not_zero(&elem->refcount))
		return obj;

	/* Slowpath:  Need to lock the lru-cache and mark the object used */
	spin_lock(&lc->lock);

	/* One more attempt at the fastpath, now that we've got the lock */
	if (atomic_inc_not_zero(&elem->refcount))
		goto out;

	/*
	 * Ok, it has a zero refcount and we've got the lock, anybody else in
	 * here trying to lru_cache_get() this object will wait until we are
	 * done.
	 */

	/* Remove it from the LRU */
	BUG_ON(!lc->nr_lru);
	list_del(&elem->node);
	lc->nr_lru--;

	/* Add it to the in-use list */
	list_add_tail(&elem->node, &lc->in_use);
	lc->nr_in_use++;

	/* Acquire a reference */
	atomic_set(&elem->refcount, 1);

out:

	spin_unlock(&lc->lock);
	return obj;
}

/* This releases one reference */
void lru_cache_put(struct lru_cache *lc, void *obj)

{
	struct lru_cache_elem *elem = obj + lc->offset;
	BUG_ON(elem->lc != lc);
	BUG_ON(!atomic_read(&elem->refcount));

	/* Drop the refcount; if it's still nonzero, we're done */
	if (atomic_dec_return(&elem->refcount))
		return;

	/* We need to take the lru-cache lock to make sure we release it */
	spin_lock(&lc->lock);
	if (atomic_read(&elem->refcount))
		goto out;

	/*
	 * Ok, it has a zero refcount and we hold the lock, anybody trying to
	 * lru_cache_get() this object will block until we're done.
	 */

	/* Remove it from the in-use list */
	BUG_ON(!lc->nr_in_use);
	list_del(&elem->node);
	lc->nr_in_use--;

	/* Add it to the LRU list */
	list_add_tail(&elem->node, &lc->lru);
	lc->nr_lru++;

out:

	spin_unlock(&lc->lock);
}
maintainer
#37 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 8 lines · Kyle Moffett

After digging through this a bit, I would recommend rewriting this whole part. The part that definitely needs to go is the vmalloc() of your whole LRU at once.

Here's some code I've been fiddling around with for a while. It uses a kmem_cache and a shrinker callback (although it does need a small patch to the shrinker code) to provide a dynamically sized LRU list for fixed-size objects.

Thanks.

When we created our lru_cache stuff, we considered embedding callbacks and internal locking, but decided against it. Conceptually it should be more like the "list.h" list handling infrastructure.

The user will have their own locking in place anyways, and in general their critical section will be a few lines of code larger than the "lru cache" manipulation itself.

And, the specific use of our implementation is that there is a pre-selected maximum count of in-use objects, and the user gets feedback about changes to this "active" set of objects.

Think of a small room with N seats, and one entrance. Any number of people outside.
Only N fit in the room.
Seats are numbered (index), people have id (element number).

Our lru_cache implementation is the man at the door, keepeing track of which seats are empty,
and seeing to it that if one needs to go in, either there is an empty seat available,
or first the "most dispensable" one gets out.

That is where the focus is:
make the set of active objects easily trackable.
So one can easily keep track of who is in, and who is not, by writing a log of just this "diff":
seat index was occupied by element_nr A, but now is by element_nr B.

The lru part is to make it more easy to determine
who the "most dispensable" is.

So from looking at your code, it may be fine for the "lru" part, but it is not suitable for our purposes.

Lars

#38 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 7 lines · Lars Ellenberg

When we created our lru_cache stuff, we considered embedding callbacks and internal locking, but decided against it.  Conceptually it should be more like the "list.h" list handling infrastructure.

The user will have their own locking in place anyways, and in general their critical section will be a few lines of code larger than the "lru cache" manipulation itself.

One of the major design-points for the code I'm fiddling with is that it allows you to use RCU on your lookup table, which basically means lock-free lookup (although I haven't stress-tested that part of it yet so the code itself may have subtle bugs). With a bit more work it's probably even possible to make it lock-free even when adding a reference to an object that's currently on the LRU.

Quoted 3 lines

And, the specific use of our implementation is that there is a pre-selected maximum count of in-use objects, and the user gets feedback about changes to this "active" set of objects.

Another major design point (and the reason for the single "evict" callback) is that my code does not require manual tuning, it responds to memory-pressure dynamically using the "shrinker" mechanism. So on a box with 128MB of RAM your LRU cache will be automatically size-limited by other activity on the system to an appropriate size; yet it can scale up to tens or hundreds of megabytes on a system with hundreds of gigs of RAM under heavy IO load.

The real deal-breaker for your code is its usage of "vmalloc", it's unlikely to be merged when it relies on vmalloc() of a large continuous block for operation.

Quoted 5 lines

That is where the focus is:
make the set of active objects easily trackable.
So one can easily keep track of who is in, and who is not, by writing a log of just this "diff":
seat index was occupied by element_nr A, but now is by element_nr B.

This could be very easily done with tracepoints and a few minor tweaks to the implementation I provided. I could add an object number and various statistics similar to the ones in your code; the only reason I didn't before is I did not need them and they detracted slightly from the simplicity of the implementation (just 271 lines).

Keep in mind that by using the kmem_cache infrastructure, you get to take advantage of all of the other SLAB debugging features on objects allocated through your LRUs. This includes redzones and poison-on-free. It also makes the number of objects and the number of pages allocated show up in /proc/slabinfo and the various SLAB/SLUB debug tools.

Quoted 2 lines

So from looking at your code, it may be fine for the "lru" part, but it is not suitable for our purposes.

It would need an extra layer stacked on top to handle the hash-table lookups, but it would solve the vmalloc issue and allow your LRU lists to dynamically size a bit better. It's also not that difficult to apply memory allocation limits (aside from the default memory-pressure) and add additional statistics and debugging info.

Cheers,
Kyle Moffett

maintainer
#39 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 10 lines · Kyle Moffett
Quoted 7 lines · Lars Ellenberg

When we created our lru_cache stuff, we considered embedding callbacks and internal locking, but decided against it.  Conceptually it should be more like the "list.h" list handling infrastructure.

The user will have their own locking in place anyways, and in general their critical section will be a few lines of code larger than the "lru cache" manipulation itself.

Quoted 6 lines

One of the major design-points for the code I'm fiddling with is that it allows you to use RCU on your lookup table, which basically means lock-free lookup (although I haven't stress-tested that part of it yet so the code itself may have subtle bugs). With a bit more work it's probably even possible to make it lock-free even when adding a reference to an object that's currently on the LRU.

sounds good.

maybe it is only late, and I just don't see how good your code fits what we are doing, after just adding twenty-or-so lines of wrapper code. let me sleep about it.

still, some comments below.

Quoted 11 lines
Quoted 3 lines

And, the specific use of our implementation is that there is a pre-selected maximum count of in-use objects, and the user gets feedback about changes to this "active" set of objects.

Another major design point (and the reason for the single "evict" callback) is that my code does not require manual tuning, it responds to memory-pressure dynamically using the "shrinker" mechanism. So on a box with 128MB of RAM your LRU cache will be automatically size-limited by other activity on the system to an appropriate size; yet it can scale up to tens or hundreds of megabytes on a system with hundreds of gigs of RAM under heavy IO load.

I'm in the IO path.
I absolutely do not want some stupid excessive read_ahead setting or an out-of-bounds badly hacked php cronjob to limit the amount of write-out I can do when things get tight (because of that badly behaved process, most likely).

And I do not want to let it grow to an arbitrary number of objects, that is even one important point of it: giving the admin a tuning knob to trade resync-time after crash vs. frequency of meta-data updates. I _want_ manual tuning. We are talking about (at max) a few thousand small (<=56 bytes, even on 64bit arch), a few hundred kB.

I'm sure your approach has a number of valid uses. I don't yet see it as a good fit for our purpose, though. well, maybe it could replace the "inter part" of what we do.

Quoted 3 lines

The real deal-breaker for your code is its usage of "vmalloc", it's unlikely to be merged when it relies on vmalloc() of a large continuous block for operation.

yes, I already got that part, thanks ;)
it is not a problem, its only in there because we have been lazy.

it is easily changed, either by using kmalloc (the objects, and the maximum number of objects are both small), or some array of pages. maybe even using a kmem_cache, partly following your code, if that makes you happy ;-)

still, the main purpose of your "lru_cache",
and what we do with our "lru_cache",
seems to me to be different.
maybe we should change our name to something more appropriate.

Quoted 12 lines
Quoted 5 lines

That is where the focus is:
make the set of active objects easily trackable.
So one can easily keep track of who is in, and who is not, by writing a log of just this "diff":
seat index was occupied by element_nr A, but now is by element_nr B.

This could be very easily done with tracepoints and a few minor tweaks to the implementation I provided. I could add an object number and various statistics similar to the ones in your code; the only reason I didn't before is I did not need them and they detracted slightly from the simplicity of the implementation (just 271 lines).

Quoted 3 lines

Keep in mind that by using the kmem_cache infrastructure, you get to take advantage of all of the other SLAB debugging features on objects allocated through your LRUs.

I'm not oposed to it. I just say we don't need it. We never allocate through our LRUs.
It is all just reference counting, and changing "seat lables". Changing seat lables is the important part of it, actually.

Quoted 5 lines
Quoted 2 lines

So from looking at your code, it may be fine for the "lru" part, but it is not suitable for our purposes.

It would need an extra layer stacked on top to handle the hash-table lookups,

yes, we need a fast way to know "is this id currently in the active set?". that is what we use it most often for.

for our use case, now using label for element_number, the most important functions would be

lru_cache_is_label_present(),
lru_cache_get_by_label(),
which would need to say "EAGAIN", once the active set is full and all are in fact used. and also return back the previous label now evicted, (or "was free anyways") if successful.

lru_cache_try_get_by_label(),
which is just a combination of these two.

Quoted 1 line

but it would solve the vmalloc issue

I hear you ;)

Quoted 1 line

and allow your LRU lists to dynamically size a bit better.

We currently neither want nor need that.

Quoted 3 lines

It's also not that difficult to apply memory allocation limits (aside from the default memory-pressure) and add additional statistics and debugging info.

Of course, nothing is difficult, if it has been done already ;) We don't need memory allocation limits, we want a limit on the number of active objects. Boils down to the same thing, but for different reasons.

I dislike the embeded lock as well as the callback.
I see what you need it for.
But for our purposes, we probably would need to just set a "return NO_PLEASE;". And we do need to embed calls to this stuff in larger critical sections. Which means the embeded lock can never have contention, it would always use the fast path, ok. But still: unneccessary.

We just happen to also have the "lru" part somewhere. But in actually it is all about having a fixed number of "seats", with fixed indices and changeing labels, then changing these "seat labels", while persistently journalling the currently in-use labels.

but wait for the next post to see a better documented (or possibly rewritten) implementation of this.

Lars

#40 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 14 lines · Lars Ellenberg
Quoted 5 lines · Kyle Moffett
Quoted 3 lines · Lars Ellenberg

And, the specific use of our implementation is that there is a pre-selected maximum count of in-use objects, and the user gets feedback about changes to this "active" set of objects.

Quoted 7 lines

Another major design point (and the reason for the single "evict" callback) is that my code does not require manual tuning, it responds to memory-pressure dynamically using the "shrinker" mechanism.  So on a box with 128MB of RAM your LRU cache will be automatically size-limited by other activity on the system to an appropriate size; yet it can scale up to tens or hundreds of megabytes on a system with hundreds of gigs of RAM under heavy IO load.

Quoted 5 lines

I'm in the IO path.
I absolutely do not want some stupid excessive read_ahead setting or an out-of-bounds badly hacked php cronjob to limit the amount of write-out I can do when things get tight (because of that badly behaved process, most likely).

I completely understand this part, but I think these issues can be satisfied by the code without limiting its use for other LRU purposes.
You should also remember that some people *will* want to throttle
DRBD I/O at the expense of other forms of I/O, and memory pressure is a *big* part of how that is managed.

There are a couple trivial tunables you can apply to the model I provided to dramatically change the effect of memory pressure on the LRU:

(1) The biggie: Make sure that the proccess(es) using the
writeout-centric LRUs have PF_LESS_THROTTLE or similar so that they are throttled less than other processes. This is good advice regardless.

(2) Change the "seeks" variable in the lru_cache_info structure.
That is forwarded to the "shrinker" code to determine how to weight memory pressure on these objects. Specifically, that number is intended to represent a rough analogy of the number of seeks that it takes to recreate an object purged from the LRU. Critical LRUs can have an artificially inflated "seeks" value to reflect desired weighting.

(3) Add "nr_elem_min" and "nr_elem_max" counters which apply
minimum or maximum bounds for the LRU list, while still allowing it to dynamically resize within some range. An example where this is useful is a large simulation program which writes out the results of its computation to a DRBD array at the end. You want to let memory pressure from the simulation program push out other objects from the LRU while its running, then allow the LRU to grow large again as it frees memory and submits I/O.

(4) Apply a "lru_scan_factor" which acts as a multiplier or divider
on the "nr_scan" in the lru_cache_shrink() function (as well as its return value). This will also cause a change in the weighting of the shrinker code; if your LRU has 1000 objects in it with 2 seeks to recreate each object, it will be asked to free much more than if you claim it has 100 objects with 8 seeks per object.

Quoted 5 lines

And I do not want to let it grow to an arbitrary number of objects, that is even one important point of it: giving the admin a tuning knob to trade resync-time after crash vs. frequency of meta-data updates. I _want_ manual tuning. We are talking about (at max) a few thousand small (<=56 bytes, even on 64bit arch), a few hundred kB.

Hmm, I'd be interested to see the numbers on this for large many-terabyte volumes. You should consider that most journalling filesystems have to make similar tradeoffs; people get really frustrated if things do not (A) have a useful automatic default and
(B) have a runtime-modifiable knob. See the recent exceptionally long ext3/ext4 threads on delayed allocation and writeback for some of the flamewars that can result.

Quoted 4 lines

still, the main purpose of your "lru_cache",
and what we do with our "lru_cache",
seems to me to be different.
maybe we should change our name to something more appropriate.

I definitely agree that what you have isn't really properly named as an "lru_cache". It does make me curious, though, what precisely the performance differences are (for DRBD specifically) between an appropriately-tuned LRU cache and your fixed-size working set.

Quoted 8 lines
Quoted 3 lines

Keep in mind that by using the kmem_cache infrastructure, you get to take advantage of all of the other SLAB debugging features on objects allocated through your LRUs.

I'm not oposed to it. I just say we don't need it. We never allocate through our LRUs.
It is all just reference counting, and changing "seat lables". Changing seat lables is the important part of it, actually.

Well, technically what you're doing is using your LRU as a fixed-size kmem_cache which returns the oldest object if there are no more free slots.

Quoted 12 lines
Quoted 5 lines
Quoted 2 lines

So from looking at your code, it may be fine for the "lru" part, but it is not suitable for our purposes.

It would need an extra layer stacked on top to handle the hash-table lookups,

yes, we need a fast way to know "is this id currently in the active set?". that is what we use it most often for.

for our use case, now using label for element_number, the most important functions would be

Quoted 8 lines

lru_cache_is_label_present(),
lru_cache_get_by_label(),
which would need to say "EAGAIN", once the active set is full and all are in fact used.  and also return back the previous label now evicted, (or "was free anyways") if successful.

lru_cache_try_get_by_label(),
which is just a combination of these two.

I'll try to do a simple mostly-lockless hash-table lookup on top of this, to show you what my thoughts are.

Quoted 2 lines

but wait for the next post to see a better documented (or possibly rewritten) implementation of this.

Yeah, I'm definitely reworking it now that I have a better understanding of what the DRBD code really wants. My main intention is to have the code be flexible enough that filesystems and other sorts of network-related code can use it transparently, without requiring much in the way of manual tuning. See Linus' various comments on why he *hates* manual tunables.

Cheers,
Kyle Moffett

#41 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 6 lines · Kyle Moffett

There are a couple trivial tunables you can apply to the model I provided to dramatically change the effect of memory pressure on the LRU:

[...]

Ooh, I forgot to mention another biggie: There's a way to allocate a reserve pool of memory (I don't remember the exact API, sorry), which can be attached to a specific kmem_cache to be used by processes attempting writeout. This would allow you to allocate more in-use elements to make forward progress, even if all of your existing elements are already in-use.

Cheers,
Kyle Moffett

#42 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 8 lines · Kyle Moffett
Quoted 6 lines · Kyle Moffett

There are a couple trivial tunables you can apply to the model I provided to dramatically change the effect of memory pressure on the LRU:

[...]

Quoted 6 lines

Ooh, I forgot to mention another biggie: There's a way to allocate a reserve pool of memory (I don't remember the exact API, sorry), which can be attached to a specific kmem_cache to be used by processes attempting writeout. This would allow you to allocate more in-use elements to make forward progress, even if all of your existing elements are already in-use.

Lars,

is using a mempool for allocation, in combination with a shrinker callback for freeing older entries an option for DRBD?

It looks like that could get rid of a fair amount of custom infrastructure.

Signature · 1 line
All rights reversed.
maintainer
#43 lore raw

Re: [PATCH 02/16] DRBD: lru_cache

Quoted 16 lines · Rik van Riel
Quoted 8 lines · Kyle Moffett
Quoted 6 lines · Kyle Moffett

There are a couple trivial tunables you can apply to the model I provided to dramatically change the effect of memory pressure on the LRU:

[...]

Quoted 6 lines

Ooh, I forgot to mention another biggie: There's a way to allocate a reserve pool of memory (I don't remember the exact API, sorry), which can be attached to a specific kmem_cache to be used by processes attempting writeout. This would allow you to allocate more in-use elements to make forward progress, even if all of your existing elements are already in-use.

Quoted 7 lines

Lars,

is using a mempool for allocation, in combination with a shrinker callback for freeing older entries an option for DRBD?

It looks like that could get rid of a fair amount of custom infrastructure.

I'm going to look into it.

Lars

#44 lore raw

Re: [PATCH 01/16] DRBD: major.h

In reply to Philipp Reisner
Quoted 12 lines · On Thu, 30 Apr 2009 13:26:37 +0200 Philipp Reisner…

Since we have had a LANANA major number for years, and it is documented in devices.txt, I think that this first patch can go upstream without further changes.

---

include/linux/major.h +0 -0
index 058ec15..6a8ca98 100644

include/linux/major.h line 150

Quoted 6 lines
include/linux/major.h +1 -0
@@ -145,6 +145,7 @@
 #define UNIX98_PTY_MAJOR_COUNT	8
 #define UNIX98_PTY_SLAVE_MAJOR	(UNIX98_PTY_MASTER_MAJOR+UNIX98_PTY_MAJOR_COUNT)
 
+#define DRBD_MAJOR		147
 #define RTF_MAJOR		150
 #define RAW_MAJOR		162

Yup. I'll merge this, after having given it a vaguely useful title, "drbd: add major number to major.h".

#45 lore raw
In reply to Philipp Reisner
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

How fast is it?

Is it being used anywhere for anything? If so, where and what?

(it would be useful to add such info to the changelog, and to maintain it)

#46 lore raw
Quoted 6 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

How fast is it?

From experience, it achieves performance of approx. 98% of wire or

spindle speed, so it is considered rather efficient code.

Quoted 1 line

Is it being used anywhere for anything? If so, where and what?

It is used by many customers (thousands world-wide, I'm sure) to replicate block device data locally (to replace more expensive SANs while achieving higher availablity) or async/remotely (for disaster recovery).

The code is rather stable, the first drbd deployments date back many years - drbd0.7 for example has been shipping with SLES10/9, and 0.6 with SLES8 already. The new drbd8 code is shipping on SLE11 and used also in combination with OCFS2.

So we very much welcome the renewed and persistent interest of merging the code in mainline (once all serious issues are addressed).

Even if in the long-term a merge with other raid implementations is pursued (which I'd welcome even more), the existence of so many deployments means we'll need the code for awhile still.

Regards,
Lars

Signature · 3 lines
SuSE Labs, OPS Engineering, Novell, Inc.
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
"Experience is the name everyone gives to their mistakes." -- Oscar Wilde
#47 lore raw
Quoted 12 lines · Lars Marowsky-Bree
Quoted 1 line

Is it being used anywhere for anything? If so, where and what?

It is used by many customers (thousands world-wide, I'm sure) to replicate block device data locally (to replace more expensive SANs while achieving higher availablity) or async/remotely (for disaster recovery).

The code is rather stable, the first drbd deployments date back many years - drbd0.7 for example has been shipping with SLES10/9, and 0.6 with SLES8 already. The new drbd8 code is shipping on SLE11 and used also in combination with OCFS2.

Quoted 6 lines

So we very much welcome the renewed and persistent interest of merging the code in mainline (once all serious issues are addressed).

Even if in the long-term a merge with other raid implementations is pursued (which I'd welcome even more), the existence of so many deployments means we'll need the code for awhile still.

I've not looked through the patchset, and it's a bit outside my domain of expertise, but I can attest we have had requests to
merge it in Fedora (which we've given the usual "get it upstream" response to). The folks who run the Fedora infrastructure have been enthusiastic about it for a while (which is why I ended up on the CC for this thread I guess). I don't have details about their exact use-cases, but if desired, I can find out more.

Dave

#48 lore raw
Quoted 21 lines · On Fri, 1 May 2009 09:14:25 -0400 Dave Jones <[email protected]> wrote:
Quoted 12 lines · Lars Marowsky-Bree
Quoted 1 line

Is it being used anywhere for anything? If so, where and what?

It is used by many customers (thousands world-wide, I'm sure) to replicate block device data locally (to replace more expensive SANs while achieving higher availablity) or async/remotely (for disaster recovery).

The code is rather stable, the first drbd deployments date back many years - drbd0.7 for example has been shipping with SLES10/9, and 0.6 with SLES8 already. The new drbd8 code is shipping on SLE11 and used also in combination with OCFS2.

Quoted 6 lines

So we very much welcome the renewed and persistent interest of merging the code in mainline (once all serious issues are addressed).

Even if in the long-term a merge with other raid implementations is pursued (which I'd welcome even more), the existence of so many deployments means we'll need the code for awhile still.

Quoted 8 lines

I've not looked through the patchset, and it's a bit outside my domain of expertise, but I can attest we have had requests to
merge it in Fedora (which we've given the usual "get it upstream" response to). The folks who run the Fedora infrastructure have been enthusiastic about it for a while (which is why I ended up on the CC for this thread I guess). I don't have details about their exact use-cases, but if desired, I can find out more.

Oh. Thanks. Well we should all get cracking on it then.

#49 lore raw
In reply to Lars Marowsky-Bree
Quoted 4 lines · Lars Marowsky-Bree

It is used by many customers (thousands world-wide, I'm sure) to replicate block device data locally (to replace more expensive SANs while achieving higher availablity) or async/remotely (for disaster recovery).

While covering Linux-HA success stories really, most of them are using DRBD as well: http://moin.linux-ha.org/lha/SuccessStories

C.

Signature · 1 line
Bruce Schneier does not sleep. He prempts everything.
#50 lore raw
In reply to Andrew Morton
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything?  If so, where and what?

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Bart.

#51 lore raw
Quoted 10 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything?  If so, where and what?

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

Willy

#52 lore raw
Quoted 5 lines · Willy Tarreau
Quoted 4 lines · Bart Van Assche
Quoted 2 lines · Andrew Morton

On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner <[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

This is a repost of DRBD

Is it being used anywhere for anything?  If so, where and what?

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

David Lang

#53 lore raw
Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

And probably a few others I forget.

James

#54 lore raw
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

David Lang

#55 lore raw
Quoted 42 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

James

#56 lore raw
Quoted 56 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 42 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

Quoted 6 lines

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

but the issue is that at the time the failure is taking place, neither side _knows_ that the other side is running. In fact, they both think that the other side is dead.

with DRDB, when the two sides start talking again they will discover that they are different and complain, loudly, to the sysadmin that they need help

with md/ndb you have the situation where both sides will try to resync to the other side as soon as the packets can get through. this can end up corrupting both sides if it's not caught fast enough

David Lang

#57 lore raw
Quoted 65 lines · On Sun, 2009-05-03 at 07:56 -0700, [email protected] wrote:
Quoted 56 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 42 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

Quoted 6 lines

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

Quoted 3 lines

but the issue is that at the time the failure is taking place, neither side _knows_ that the other side is running. In fact, they both think that the other side is dead.

Resolving this is the job of the HA harness, as I said ... the usual solution being either third node pings or confirmable switchover.

Quoted 3 lines

with DRDB, when the two sides start talking again they will discover that they are different and complain, loudly, to the sysadmin that they need help

The object of HA is to prevent data becoming toast, not to point it out to the sysadmin after the fact.

Quoted 3 lines

with md/ndb you have the situation where both sides will try to resync to the other side as soon as the packets can get through. this can end up corrupting both sides if it's not caught fast enough

Actually, that's just your implementation: md/nbd does nothing to re-establish the replication, it has to be done by the HA harness after split brain resolution. What a correct harness would do is to compare the HA event log and the intent logs to see if there had been activity to both sides after loss of contact and, if their had, to flag the data corruption problem and not resume replication.

This corruption situation isn't unique to replication ... any time you may potentially have allowed both sides to write to a data store, you get it, that's why it's the job of the HA harness to sort out whether a split brain happened and what to do about it *first*.

James

#58 lore raw
Quoted 65 lines · James Bottomley
Quoted 65 lines · James Bottomley
Quoted 63 lines

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 42 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

Quoted 6 lines
Quoted 3 lines

but the issue is that at the time the failure is taking place, neither side _knows_ that the other side is running. In fact, they both think that the other side is dead.

Resolving this is the job of the HA harness, as I said ... the usual solution being either third node pings or confirmable switchover.

and none of those solutions are failsafe in a distributed environment (in a local environment you can have a race to see which system powers off the other first to ensure that at most one is running, but you can't do that reliably remotely)

Quoted 6 lines
Quoted 3 lines

with DRDB, when the two sides start talking again they will discover that they are different and complain, loudly, to the sysadmin that they need help

The object of HA is to prevent data becoming toast, not to point it out to the sysadmin after the fact.

it needs to do both

Quoted 11 lines
Quoted 3 lines

with md/ndb you have the situation where both sides will try to resync to the other side as soon as the packets can get through. this can end up corrupting both sides if it's not caught fast enough

Actually, that's just your implementation: md/nbd does nothing to re-establish the replication, it has to be done by the HA harness after split brain resolution. What a correct harness would do is to compare the HA event log and the intent logs to see if there had been activity to both sides after loss of contact and, if their had, to flag the data corruption problem and not resume replication.

Quoted 4 lines

This corruption situation isn't unique to replication ... any time you may potentially have allowed both sides to write to a data store, you get it, that's why it's the job of the HA harness to sort out whether a split brain happened and what to do about it *first*.

but you can have packets sitting in the network buffers waiting to get to the remote machine, then once the connection is reestablished those packets will go out. no remounting needed., just connectivity restored. (this isn't as bad as if the system tries to re-sync to the temprarily unavailable drive by itself, but it can still corrupt things)

a cluster spread across different locations has problems to face that a cluster within easy cabling distance does not.

DRDB has been extensivly tested and build to survive in the harsher environment. md/ndb is a reasonable approximation for the simple enviornment of two servers in one datacenter, but that doesn't mean that it handles the rest of the possible conditions.

David Lang

#59 lore raw
Quoted 71 lines · On Sun, 2009-05-03 at 08:22 -0700, [email protected] wrote:
Quoted 69 lines · James Bottomley
Quoted 68 lines · James Bottomley
Quoted 57 lines

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 42 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

Quoted 6 lines

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

but the issue is that at the time the failure is taking place, neither side _knows_ that the other side is running. In fact, they both think that the other side is dead.

Quoted 7 lines
Quoted 2 lines

Resolving this is the job of the HA harness, as I said ... the usual solution being either third node pings or confirmable switchover.

and none of those solutions are failsafe in a distributed environment (in a local environment you can have a race to see which system powers off the other first to ensure that at most one is running, but you can't do that reliably remotely)

Um, yes they are, that's why they're used.

Do you understand how they work?

Third node ping means that there has to be an external third node acting as mediator (like a quorum device) ... usually in a third location. A node surviving has to make contact with it before failover can proceed automatically (the running node has to be in contact to keep running).

Confirmable switchover is where the cluster detects the failure and pages an admin to check on the remote and confirm or deny the switch over manually. Without the confirmation it just waits.

Both of these mechanisms are robust to split brain. By and large most enterprises I've seen go for confirmable switchover, but some do implement third node ping.

Quoted 25 lines
Quoted 6 lines
Quoted 3 lines

with DRDB, when the two sides start talking again they will discover that they are different and complain, loudly, to the sysadmin that they need help

The object of HA is to prevent data becoming toast, not to point it out to the sysadmin after the fact.

it needs to do both

Quoted 11 lines
Quoted 3 lines

with md/ndb you have the situation where both sides will try to resync to the other side as soon as the packets can get through. this can end up corrupting both sides if it's not caught fast enough

Actually, that's just your implementation: md/nbd does nothing to re-establish the replication, it has to be done by the HA harness after split brain resolution. What a correct harness would do is to compare the HA event log and the intent logs to see if there had been activity to both sides after loss of contact and, if their had, to flag the data corruption problem and not resume replication.

Quoted 4 lines

This corruption situation isn't unique to replication ... any time you may potentially have allowed both sides to write to a data store, you get it, that's why it's the job of the HA harness to sort out whether a split brain happened and what to do about it *first*.

Quoted 5 lines

but you can have packets sitting in the network buffers waiting to get to the remote machine, then once the connection is reestablished those packets will go out. no remounting needed., just connectivity restored. (this isn't as bad as if the system tries to re-sync to the temprarily unavailable drive by itself, but it can still corrupt things)

This is an interesting thought, but not what happens. As soon as the HA harness stops replication, which it does at the instant failure is detected, the closure of the socket kills all the in flight network data.

There is an variant of this problem that occurs with device mapper queue_if_no_path (on local disks) which does exactly what you say (keeps unsaved data around in the queue forever), but that's fixed by not using queue_if_no_path for HA. Maybe that's what you were thinking of?

Quoted 5 lines

a cluster spread across different locations has problems to face that a cluster within easy cabling distance does not.

DRDB has been extensivly tested and build to survive in the harsher environment.

There are commercial HA products based on md/nbd, so I'd say it's also hardened for harsher environments

Quoted 3 lines

md/ndb is a reasonable approximation for the simple
enviornment of two servers in one datacenter, but that doesn't mean that it handles the rest of the possible conditions.

The implementations I've seen do ... and that includes some fairly exotic cascading WAN replication ones.

James

#60 lore raw
Quoted 80 lines · James Bottomley
Quoted 71 lines · On Sun, 2009-05-03 at 08:22 -0700, [email protected] wrote:
Quoted 69 lines · James Bottomley
Quoted 68 lines · James Bottomley
Quoted 57 lines

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 42 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

Quoted 6 lines

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

but the issue is that at the time the failure is taking place, neither side _knows_ that the other side is running. In fact, they both think that the other side is dead.

Quoted 7 lines
Quoted 2 lines

Resolving this is the job of the HA harness, as I said ... the usual solution being either third node pings or confirmable switchover.

and none of those solutions are failsafe in a distributed environment (in a local environment you can have a race to see which system powers off the other first to ensure that at most one is running, but you can't do that reliably remotely)

Quoted 8 lines

Um, yes they are, that's why they're used.

Do you understand how they work?

Third node ping means that there has to be an external third node acting as mediator (like a quorum device) ... usually in a third location. A node surviving has to make contact with it before failover can proceed automatically (the running node has to be in contact to keep running).

this is what I understood, there are many cases where this doesn't work well

Quoted 3 lines

Confirmable switchover is where the cluster detects the failure and pages an admin to check on the remote and confirm or deny the switch over manually. Without the confirmation it just waits.

this I did not understand

Quoted 3 lines

Both of these mechanisms are robust to split brain. By and large most enterprises I've seen go for confirmable switchover, but some do implement third node ping.

it depends on how much tolerance teh business has for things to be down as a result of a problem with the third node (including communications to it) and how long they are willing to be down while waiting for a sysadmin to be paged

Quoted 16 lines
Quoted 10 lines
Quoted 4 lines

This corruption situation isn't unique to replication ... any time you may potentially have allowed both sides to write to a data store, you get it, that's why it's the job of the HA harness to sort out whether a split brain happened and what to do about it *first*.

but you can have packets sitting in the network buffers waiting to get to the remote machine, then once the connection is reestablished those packets will go out. no remounting needed., just connectivity restored. (this isn't as bad as if the system tries to re-sync to the temprarily unavailable drive by itself, but it can still corrupt things)

This is an interesting thought, but not what happens. As soon as the HA harness stops replication, which it does at the instant failure is detected, the closure of the socket kills all the in flight network data.

Quoted 4 lines

There is an variant of this problem that occurs with device mapper queue_if_no_path (on local disks) which does exactly what you say (keeps unsaved data around in the queue forever), but that's fixed by not using queue_if_no_path for HA. Maybe that's what you were thinking of?

is there a mechanism in ndb that prevents it from beign mounted more than once? if so then could have the same protection that DRDB has, if not it is possible for it to be mounted more than once place and therefor get corrupted.

Quoted 8 lines
Quoted 5 lines

a cluster spread across different locations has problems to face that a cluster within easy cabling distance does not.

DRDB has been extensivly tested and build to survive in the harsher environment.

There are commercial HA products based on md/nbd, so I'd say it's also hardened for harsher environments

which ones?

David Lang

#61 lore raw
Quoted 86 lines · On Sun, 2009-05-03 at 08:48 -0700, [email protected] wrote:
Quoted 84 lines · James Bottomley
Quoted 71 lines · On Sun, 2009-05-03 at 08:22 -0700, [email protected] wrote:
Quoted 69 lines · James Bottomley
Quoted 68 lines · James Bottomley
Quoted 57 lines

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 42 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 40 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 11 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 9 lines · Willy Tarreau
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner…

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

Quoted 6 lines

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

but the issue is that at the time the failure is taking place, neither side _knows_ that the other side is running. In fact, they both think that the other side is dead.

Quoted 7 lines
Quoted 2 lines

Resolving this is the job of the HA harness, as I said ... the usual solution being either third node pings or confirmable switchover.

and none of those solutions are failsafe in a distributed environment (in a local environment you can have a race to see which system powers off the other first to ensure that at most one is running, but you can't do that reliably remotely)

Um, yes they are, that's why they're used.

Do you understand how they work?

Quoted 7 lines
Quoted 4 lines

Third node ping means that there has to be an external third node acting as mediator (like a quorum device) ... usually in a third location. A node surviving has to make contact with it before failover can proceed automatically (the running node has to be in contact to keep running).

this is what I understood, there are many cases where this doesn't work well

You mean there are situations where both can be down? Sure, but a) they're rare and b) it's still not a split brain.

Quoted 6 lines
Quoted 3 lines

Confirmable switchover is where the cluster detects the failure and pages an admin to check on the remote and confirm or deny the switch over manually. Without the confirmation it just waits.

this I did not understand

Quoted 8 lines
Quoted 3 lines

Both of these mechanisms are robust to split brain. By and large most enterprises I've seen go for confirmable switchover, but some do implement third node ping.

it depends on how much tolerance teh business has for things to be down as a result of a problem with the third node (including communications to it) and how long they are willing to be down while waiting for a sysadmin to be paged

Usually for geo disaster type situations, the recovery plans I've seen actually *require* manual intervention (likely because they don't fully trust their HA suppliers, of course ...)

Quoted 21 lines
Quoted 16 lines
Quoted 10 lines
Quoted 4 lines

This corruption situation isn't unique to replication ... any time you may potentially have allowed both sides to write to a data store, you get it, that's why it's the job of the HA harness to sort out whether a split brain happened and what to do about it *first*.

but you can have packets sitting in the network buffers waiting to get to the remote machine, then once the connection is reestablished those packets will go out. no remounting needed., just connectivity restored. (this isn't as bad as if the system tries to re-sync to the temprarily unavailable drive by itself, but it can still corrupt things)

This is an interesting thought, but not what happens. As soon as the HA harness stops replication, which it does at the instant failure is detected, the closure of the socket kills all the in flight network data.

Quoted 4 lines

There is an variant of this problem that occurs with device mapper queue_if_no_path (on local disks) which does exactly what you say (keeps unsaved data around in the queue forever), but that's fixed by not using queue_if_no_path for HA. Maybe that's what you were thinking of?

Quoted 4 lines

is there a mechanism in ndb that prevents it from beign mounted more than once? if so then could have the same protection that DRDB has, if not it is possible for it to be mounted more than once place and therefor get corrupted.

That's not really relevant, is it? An ordinary disk doesn't have this property either. Mediating simultaneous access is the job of the HA harness. If the device does it for you, fine, the harness can make use of that (as long as the device gets it right) but all good HA harnesses sort out the usual case where the device doesn't do it.

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

a cluster spread across different locations has problems to face that a cluster within easy cabling distance does not.

DRDB has been extensivly tested and build to survive in the harsher environment.

There are commercial HA products based on md/nbd, so I'd say it's also hardened for harsher environments

which ones?

SteelEye LifeKeeper. It actually supports both drbd and md/nbd.

James

#62 lore raw
Quoted 35 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 27 lines · On Sun, 2009-05-03 at 08:48 -0700, [email protected] wrote:
Quoted 20 lines · James Bottomley
Quoted 8 lines · On Sun, 2009-05-03 at 08:22 -0700, [email protected] wrote:
Quoted 4 lines · James Bottomley

This corruption situation isn't unique to replication ... any time you may potentially have allowed both sides to write to a data store, you get it, that's why it's the job of the HA harness to sort out whether a split brain happened and what to do about it *first*.

Quoted 5 lines

but you can have packets sitting in the network buffers waiting to get to the remote machine, then once the connection is reestablished those packets will go out. no remounting needed., just connectivity restored. (this isn't as bad as if the system tries to re-sync to the temprarily unavailable drive by itself, but it can still corrupt things)

This is an interesting thought, but not what happens. As soon as the HA harness stops replication, which it does at the instant failure is detected, the closure of the socket kills all the in flight network data.

Quoted 4 lines

There is an variant of this problem that occurs with device mapper queue_if_no_path (on local disks) which does exactly what you say (keeps unsaved data around in the queue forever), but that's fixed by not using queue_if_no_path for HA. Maybe that's what you were thinking of?

Quoted 4 lines

is there a mechanism in ndb that prevents it from beign mounted more than once? if so then could have the same protection that DRDB has, if not it is possible for it to be mounted more than once place and therefor get corrupted.

Quoted 5 lines

That's not really relevant, is it? An ordinary disk doesn't have this property either. Mediating simultaneous access is the job of the HA harness. If the device does it for you, fine, the harness can make use of that (as long as the device gets it right) but all good HA harnesses sort out the usual case where the device doesn't do it.

with a local disk you can mount it multiple times, write to it from all the mounts, and not have any problems, because all access goes through a common layer.

you would have this sort of problem if you used one partition as part of multiple md arrays, but the md layer itself would detect and prevent this (because it would see both arrays), but again, in a multi-machine situation you don't have the common layer to do the detection.

you can rely on the HA layer to detect and prevent all of this (and apparently there are people doing this, I wasn't aware of it), but I've seen enough problems with every HA implementation I've dealt with over the years (both opensource and commercial), that I would be very uncomfortable depending on this exclusivly. having the disk replication layer detect this adds a significant amount of safety in my eyes.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

There are commercial HA products based on md/nbd, so I'd say it's also hardened for harsher environments

which ones?

SteelEye LifeKeeper. It actually supports both drbd and md/nbd.

thanks for the info.

David Lang

author maintainer
#63 lore raw
In reply to James Bottomley
Quoted 11 lines · On Sunday 03 May 2009 16:45:25 James Bottomley wrote:
Quoted 10 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 9 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 6 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 5 lines · Willy Tarreau
Quoted 4 lines · Bart Van Assche

On Fri, May 1, 2009 at 10:59 AM, Andrew Morton

Quoted 1 line · <[email protected]> wrote:

On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner

Quoted 41 lines · <[email protected]> wrote:
Quoted 30 lines
Quoted 30 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

Quoted 7 lines

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

When you do asynchronous replication, how do you ensure that implicit write-after-write dependencies in the stream of writes you get from the file system above, are not violated on the secondary ?

There might be a disk scheduler on the secondary.

-Phil

Signature · 6 lines
: Dipl-Ing Philipp Reisner
: LINBIT | Your Way to High Availability
: Tel: +43-1-8178292-50, Fax: +43-1-8178292-82
: http://www.linbit.com

DRBD(R) and LINBIT(R) are registered trademarks of LINBIT, Austria.
#64 lore raw
Quoted 62 lines · Philipp Reisner
Quoted 11 lines · On Sunday 03 May 2009 16:45:25 James Bottomley wrote:
Quoted 10 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 9 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 6 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 5 lines · Willy Tarreau
Quoted 4 lines · Bart Van Assche

On Fri, May 1, 2009 at 10:59 AM, Andrew Morton

Quoted 1 line · <[email protected]> wrote:

On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner

Quoted 41 lines · <[email protected]> wrote:
Quoted 30 lines
Quoted 30 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

Quoted 7 lines

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

Quoted 3 lines

When you do asynchronous replication, how do you ensure that implicit write-after-write dependencies in the stream of writes you get from the file system above, are not violated on the secondary ?

Are you telling me drbd doesn't currently do this?

The way nbd does it (in the updated tools is to use DIRECT_IO and fsync).

Quoted 1 line

There might be a disk scheduler on the secondary.

There usually is a disk scheduler ... you just have to take the required action to persuade it to preserve ordering ... a simplistic way of doing this is to switch to the noop scheduler.

James

author maintainer
#65 lore raw
Quoted 64 lines · On Monday 04 May 2009 19:24:11 James Bottomley wrote:
Quoted 63 lines · Philipp Reisner
Quoted 11 lines · On Sunday 03 May 2009 16:45:25 James Bottomley wrote:
Quoted 10 lines · On Sun, 2009-05-03 at 07:36 -0700, [email protected] wrote:
Quoted 9 lines · James Bottomley

Subject: Re: [PATCH 00/16] DRBD: a block device for HA clusters

Quoted 6 lines · On Sat, 2009-05-02 at 22:40 -0700, [email protected] wrote:
Quoted 5 lines · Willy Tarreau
Quoted 4 lines · Bart Van Assche

On Fri, May 1, 2009 at 10:59 AM, Andrew Morton

Quoted 1 line · <[email protected]> wrote:

On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner

Quoted 48 lines · <[email protected]> wrote:
Quoted 31 lines
Quoted 31 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

This is a repost of DRBD

Is it being used anywhere for anything? If so, where and what?

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Quoted 5 lines
Quoted 1 line

Confirmed, I have several customers who're doing exactly that.

I will also say that there are a lot of us out here who would have a use for DRDB in our HA setups, but have held off implementing it specificly because it's not yet in the upstream kernel.

Actually, that's not a particularly strong reason because we already have an in-kernel replicator that has much of the functionality of drbd that you could use. The main reason for wanting drbd in kernel is that it has a *current* user base.

Both the in kernel md/nbd and drbd do sync and async replication with primary side bitmaps. The main differences are:

* md/nbd can do 1 to N replication,
* drbd can do active/active replication (useful for cluster filesystems)
* The chunk size of the md/nbd is tunable
* With the updated nbd-tools, current md/nbd can do point in
time rollback on transaction logged secondaries (a BCS requirement)
* drbd manages the mirror state explicitly, md/nbd needs a user space helper

Quoted 6 lines
Quoted 1 line

And probably a few others I forget.

one very big one:

DRDB has better support for dealing with split brain situations and recovering from them.

I don't really think so. The decision about which (or if a) node should be killed lies with the HA harness outside of the province of the replication.

One could argue that the symmetric active mode of drbd allows both nodes to continue rather than having the harness make a kill decision about one. However, if they both alter the same data, you get an irreconcilable data corruption fault which, one can argue, is directly counter to HA principles and so allowing drbd continuation is arguably the wrong thing to do.

Quoted 6 lines
Quoted 3 lines

When you do asynchronous replication, how do you ensure that implicit write-after-write dependencies in the stream of writes you get from the file system above, are not violated on the secondary ?

Are you telling me drbd doesn't currently do this?

No I am not. DRBD does exactly this!
But I am wondering how that is achieved in the MD/NBD stack when running in async mode.

The issue is covered since the early days in DRBD, (back in 2000). The issue, and the solution we have in DRBD is described in this paper:

http://www.drbd.org/fileadmin/drbd/publications/drbd_paper_for_NLUUG_2001.pdf

Quoted 2 lines

The way nbd does it (in the updated tools is to use DIRECT_IO and fsync).

Is that available in the existing tools ? -- Are the updated tools something that will be available in the future ?

Are you telling me md/ndb (async) doesn't currently do this ?

Quoted 5 lines
Quoted 1 line

There might be a disk scheduler on the secondary.

There usually is a disk scheduler ... you just have to take the required action to persuade it to preserve ordering ... a simplistic way of doing this is to switch to the noop scheduler.

The issue actually goes further down the stack. Not only the in kernel disk scheduler might reorder something, also the driver and finally the drive might do so.

What we have in DRBD boils down to:

* We obey all possible write after write dependencies in the stream of writes we get from the upper layers. And generate DRBD internal reorder barriers for the packet stream.
* On the secondary node we impose these barriers onto the stream of writes submitted to the stack below us by either:

- Let previously submitted write-IO drain before we submit write-IO after such an DRBD barrier. (That we have since 2000 or so)

- Additionally issue a blkdev_issue_flush()

- Use write requests with BIO_RW_BARRIER. This method has two advantages:
We can continue to submit writes after the DRBD internal barrier immediately, and the number of requests with BIO_RW_BARRIER can be further reduced.
See section 6 of
http://www.drbd.org/fileadmin/drbd/publications/drbd8.pdf
for more details, and nice illustrations.

Unfortunately only high end SAN devices seem to benefit from this method. For most in-machine-disk controlers this method does not achieve the highest throughput.

Expressed in other words:
We allow reordering on the secondary node to an extend so that we can guarantee that no implicit write-after-write dependencies are violated.

Coming back to the idea of disabling the in Linux IO scheduler. It might solve the issue for some devices, but it does not guarantee to solve it.

-Phil

Signature · 6 lines
: Dipl-Ing Philipp Reisner
: LINBIT | Your Way to High Availability
: Tel: +43-1-8178292-50, Fax: +43-1-8178292-82
: http://www.linbit.com

DRBD(R) and LINBIT(R) are registered trademarks of LINBIT, Austria.
#66 lore raw
Quoted 10 lines · Philipp Reisner
Quoted 6 lines
Quoted 3 lines

When you do asynchronous replication, how do you ensure that implicit write-after-write dependencies in the stream of writes you get from the file system above, are not violated on the secondary ?

Are you telling me drbd doesn't currently do this?

No I am not. DRBD does exactly this!
But I am wondering how that is achieved in the MD/NBD stack when running in async mode.

The explanation is below.

Quoted 10 lines

The issue is covered since the early days in DRBD, (back in 2000). The issue, and the solution we have in DRBD is described in this paper:

http://www.drbd.org/fileadmin/drbd/publications/drbd_paper_for_NLUUG_2001.pdf

Quoted 2 lines

The way nbd does it (in the updated tools is to use DIRECT_IO and fsync).

Is that available in the existing tools ? -- Are the updated tools something that will be available in the future ?

It's in the existing.

Quoted 1 line

Are you telling me md/ndb (async) doesn't currently do this ?

I just described how it doe this ... I don't quite see how that translates into telling you it doesn't do this.

Quoted 23 lines
Quoted 5 lines
Quoted 1 line

There might be a disk scheduler on the secondary.

There usually is a disk scheduler ... you just have to take the required action to persuade it to preserve ordering ... a simplistic way of doing this is to switch to the noop scheduler.

The issue actually goes further down the stack. Not only the in kernel disk scheduler might reorder something, also the driver and finally the drive might do so.

What we have in DRBD boils down to:

* We obey all possible write after write dependencies in the stream of writes we get from the upper layers. And generate DRBD internal reorder barriers for the packet stream.
* On the secondary node we impose these barriers onto the stream of writes submitted to the stack below us by either:

- Let previously submitted write-IO drain before we submit write-IO after such an DRBD barrier. (That we have since 2000 or so)

- Additionally issue a blkdev_issue_flush()

Quoted 7 lines

- Use write requests with BIO_RW_BARRIER. This method has two advantages:
We can continue to submit writes after the DRBD internal barrier immediately, and the number of requests with BIO_RW_BARRIER can be further reduced.
See section 6 of
http://www.drbd.org/fileadmin/drbd/publications/drbd8.pdf
for more details, and nice illustrations.

THere's a slight error in there ... we don't use ordered tags for barriers (yet). I don't think it will really matter because the main domain of ordering problems is the scheduler, which REQ_BARRIER does cope with, it just means the queue drains for a barrier.

Quoted 10 lines

Unfortunately only high end SAN devices seem to benefit from this method. For most in-machine-disk controlers this method does not achieve the highest throughput.

Expressed in other words:
We allow reordering on the secondary node to an extend so that we can guarantee that no implicit write-after-write dependencies are violated.

Coming back to the idea of disabling the in Linux IO scheduler. It might solve the issue for some devices, but it does not guarantee to solve it.

I think you'll find the dio/fsync method above actually does solve all of these issues (mainly because it enforces the semantics from top to bottom in the stack). I agree one could use more elaborate semantics like you do for drbd, but since the simple ones worked efficiently for md/nbd, there didn't seem to be much point.

James

author maintainer
#67 lore raw
Quoted 5 lines · On Tuesday 05 May 2009 16:09:45 James Bottomley wrote:
Quoted 4 lines · Philipp Reisner
Quoted 4 lines
Quoted 3 lines

When you do asynchronous replication, how do you ensure that implicit write-after-write dependencies in the stream of writes you get from the file system above, are not violated on the secondary ?

[...]

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

The way nbd does it (in the updated tools is to use DIRECT_IO and fsync).

[...]

Quoted 6 lines

I think you'll find the dio/fsync method above actually does solve all of these issues (mainly because it enforces the semantics from top to bottom in the stack). I agree one could use more elaborate semantics like you do for drbd, but since the simple ones worked efficiently for md/nbd, there didn't seem to be much point.

Do I get it right, that you enforce the exact same write order on the secondary node as the stream of writes was comming in on the primary?

Using either DIRECT_IO or fsync() calls ?

Is DIRECT_IO/fsync() enabled by default ?

-Phil

Signature · 6 lines
: Dipl-Ing Philipp Reisner
: LINBIT | Your Way to High Availability
: Tel: +43-1-8178292-50, Fax: +43-1-8178292-82
: http://www.linbit.com

DRBD(R) and LINBIT(R) are registered trademarks of LINBIT, Austria.
#68 lore raw
Quoted 18 lines · Philipp Reisner
Quoted 5 lines · On Tuesday 05 May 2009 16:09:45 James Bottomley wrote:
Quoted 4 lines · Philipp Reisner
Quoted 4 lines
Quoted 3 lines

When you do asynchronous replication, how do you ensure that implicit write-after-write dependencies in the stream of writes you get from the file system above, are not violated on the secondary ?

[...]

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

The way nbd does it (in the updated tools is to use DIRECT_IO and fsync).

[...]

Quoted 6 lines

I think you'll find the dio/fsync method above actually does solve all of these issues (mainly because it enforces the semantics from top to bottom in the stack). I agree one could use more elaborate semantics like you do for drbd, but since the simple ones worked efficiently for md/nbd, there didn't seem to be much point.

Quoted 2 lines

Do I get it right, that you enforce the exact same write order on the secondary node as the stream of writes was comming in on the primary?

Um, yes ... that's the text book way of doing replication: write order preservation.

Quoted 1 line

Using either DIRECT_IO or fsync() calls ?

Yes.

Quoted 1 line

Is DIRECT_IO/fsync() enabled by default ?

I'd have to look at the tools (and, unfortunately, there are many variants) but it was certainly true in the variant I used. However, the current main use case of md/nbd is a secondary transaction log to allow rollback anyway, so the incoming network stream is stored on the device in write order and the problem doesn't arise.

I also think you're not quite looking at the important case: if you think about it, the real necessity for the ordered domain is the network, not so much the actual secondary server. The reason is that it's very hard to find a failure case where the write order on the secondary from the network tap to disk actually matters (as long as the flight into the network tap was in order). The standard failure is of the primary, not the secondary, so the network stream stops and so does the secondary writing: as long as we guarantee to stop at a consistent point in flight, everything works. If the secondary fails while the primary is still up, that's just a standard replay to bring the secondary back into replication, so the issue doesn't arise there either.

The case where it does matter is failure of the primary followed by instantaneous failure of the secondary before the actual network stream completes, so guaranteeing that the secondary can be brought back up consistently. However, this is an incredibly rare failure scenario given the tight race timings.

James

author maintainer
#69 lore raw

Am Dienstag 05 Mai 2009 19:05:46 schrieb James Bottomley:

Quoted 27 lines · Philipp Reisner
Quoted 14 lines · On Tuesday 05 May 2009 16:09:45 James Bottomley wrote:
Quoted 5 lines · Philipp Reisner
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

When you do asynchronous replication, how do you ensure that implicit write-after-write dependencies in the stream of writes you get from the file system above, are not violated on the secondary ?

[...]

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The way nbd does it (in the updated tools is to use DIRECT_IO and fsync).

[...]

Quoted 8 lines
Quoted 5 lines

I think you'll find the dio/fsync method above actually does solve all of these issues (mainly because it enforces the semantics from top to bottom in the stack). I agree one could use more elaborate semantics like you do for drbd, but since the simple ones worked efficiently for md/nbd, there didn't seem to be much point.

Do I get it right, that you enforce the exact same write order on the secondary node as the stream of writes was comming in on the primary?

Um, yes ... that's the text book way of doing replication: write order preservation.

Quoted 8 lines
Quoted 1 line

Using either DIRECT_IO or fsync() calls ?

Yes.

Quoted 1 line

Is DIRECT_IO/fsync() enabled by default ?

I'd have to look at the tools (and, unfortunately, there are many variants) but it was certainly true in the variant I used.

[...]

My experience is that enforcing the exact same write order as on the primary by using IO draining, kills performance. - Of course things are changing in a world where everybody uses a RAID controller with a gig of battery backed RAM. But there are for sure some embedded users that run the replication technology on top of plain hard disks.

What I want to work out is, that in DRBD we have that capability to allow limited reordering on the secondary, to achieve the highest possible performance, while maintaining these implicit write-after-write dependencies.

Quoted 4 lines

I also think you're not quite looking at the important case: if you think about it, the real necessity for the ordered domain is the network, not so much the actual secondary server. The reason is that it's very hard to find a failure case where the write order on the

Quoted 8 lines

secondary from the network tap to disk actually matters (as long as the flight into the network tap was in order). The standard failure is of the primary, not the secondary, so the network stream stops and so does the secondary writing: as long as we guarantee to stop at a consistent point in flight, everything works. If the secondary fails while the primary is still up, that's just a standard replay to bring the secondary back into replication, so the issue doesn't arise there either.

A common power failure is possible. We aim for an HA system, we can not ignore a possible failure scenario. No user will buy: Well in most scenarios we do it correctly, in the unlikely case of a common power failure, and you loose your former primary at the same time, you might have a secondary with the last write but not that one write before!

Correctness before efficiency!

But I will now stop this discussion now. Proving that DRBD does some details better than the md/nbd approch gets pointless, when we agreed that DRBD can get merged as a driver. We will focus on the necessary code cleanups.

-Phil

#70 lore raw
Quoted 13 lines · Philipp Reisner
Quoted 4 lines

I also think you're not quite looking at the important case: if you think about it, the real necessity for the ordered domain is the network, not so much the actual secondary server. The reason is that it's very hard to find a failure case where the write order on the

Quoted 8 lines

secondary from the network tap to disk actually matters (as long as the flight into the network tap was in order). The standard failure is of the primary, not the secondary, so the network stream stops and so does the secondary writing: as long as we guarantee to stop at a consistent point in flight, everything works. If the secondary fails while the primary is still up, that's just a standard replay to bring the secondary back into replication, so the issue doesn't arise there either.

Quoted 7 lines

A common power failure is possible. We aim for an HA system, we can not ignore a possible failure scenario. No user will buy: Well in most scenarios we do it correctly, in the unlikely case of a common power failure, and you loose your former primary at the same time, you might have a secondary with the last write but not that one write before!

Correctness before efficiency!

Well, you have to agree that during a resync from the activity log, which plays up the primary disk from one end to another, the secondary is completely corrupt if a primary failure occurs before the resync completes. That's something that's triggered by a network outage, and so is a far more common event than cascading dual failures. It's all really a question of where you focus your effort to eliminate the corner cases.

Quoted 4 lines

But I will now stop this discussion now. Proving that DRBD does some details better than the md/nbd approch gets pointless, when we agreed that DRBD can get merged as a driver. We will focus on the necessary code cleanups.

I agree. Also HA is full of corner cases like this and opinion is endlessly divided over which corner cases are more important than which others.

James

author maintainer
#71 lore raw

[...]

Quoted 9 lines

Well, you have to agree that during a resync from the activity log, which plays up the primary disk from one end to another, the secondary is completely corrupt if a primary failure occurs before the resync completes. That's something that's triggered by a network outage, and so is a far more common event than cascading dual failures. It's all really a question of where you focus your effort to eliminate the corner cases.

I fully agree. Just not not leave this unanswered: With DRBD we provide a snapshot-resync-target handler. By using LVM's snapshotting mechanism a snapshot is taken before it becomes resync-target. In case the resync completes gracefully, the snapshot is automatically removed.

Which is still inferior to a full transaction log on the secondary.

-Phil

#72 lore raw
In reply to Philipp Reisner
Quoted 5 lines · Philipp Reisner

What we have in DRBD boils down to:

* We obey all possible write after write dependencies in the stream of writes we get from the upper layers. And generate DRBD internal reorder barriers for the packet stream.

Hello Philipp,

I couldn't find a call to blk_queue_ordered() in the DRBD 8.3.1 source code. This made me wonder how DRBD obtains information about barriers that is generated by filesystems like ext3 with the option barrier=1 ?

Bart.

author maintainer
#73 lore raw
Quoted 9 lines · On Tuesday 05 May 2009 17:03:13 Bart Van Assche wrote:

On Tue, May 5, 2009 at 10:21 AM, Philipp Reisner

Quoted 5 lines · <[email protected]> wrote:

What we have in DRBD boils down to:

* We obey all possible write after write dependencies in the stream of writes we get from the upper layers. And generate DRBD internal reorder barriers for the packet stream.

Quoted 6 lines

Hello Philipp,

I couldn't find a call to blk_queue_ordered() in the DRBD 8.3.1 source code. This made me wonder how DRBD obtains information about barriers that is generated by filesystems like ext3 with the option barrier=1 ?

Hi Bart,

I was refering to implicit write after write dependencies, that one needs to obey when doing asynchronous replication.

Up to now we do not offer barrier support for the layers above us. That will follow sooner or later.

Here is an example, why it is not completely trivial:

Imagine DRBD on top of a dm-linear on both nodes. When you start, both dm-linear mappings sit on top of something that supports barriers itself. -- Then the user replaces the backing device below the dm-linear on the secondary node with something that does not support barriers.

When we get a write request with the BIO_RW_BARRIER flag set in from the FS, we submit this locally, ship it over to the peer and submit it there. Unfortunately it fails now with ENOTSUP on the peer.

We can not ship that error back to the upper layer, because our mirror is already inconsistent. We have to resubmit it with BIO_RW_BARRIER cleared, and other means to enforce write ordering... Then tell the other node that we prefer to no longer accept BIO_RW_BARRIER etc...

-Phil

Signature · 6 lines
: Dipl-Ing Philipp Reisner
: LINBIT | Your Way to High Availability
: Tel: +43-1-8178292-50, Fax: +43-1-8178292-82
: http://www.linbit.com

DRBD(R) and LINBIT(R) are registered trademarks of LINBIT, Austria.
#74 lore raw
Quoted 10 lines · Philipp Reisner

Up to now we do not offer barrier support for the layers above us. That will follow sooner or later.

Here is an example, why it is not completely trivial:

Imagine DRBD on top of a dm-linear on both nodes. When you start, both dm-linear mappings sit on top of something that supports barriers itself. -- Then the user replaces the backing device below the dm-linear on the secondary node with something that does not support barriers.

The same problem exists essentially for md raid1 as well, and I'd not consider it objectionable if you took a brutal approach:

Quoted 7 lines

When we get a write request with the BIO_RW_BARRIER flag set in from the FS, we submit this locally, ship it over to the peer and submit it there. Unfortunately it fails now with ENOTSUP on the peer.

We can not ship that error back to the upper layer, because our mirror is already inconsistent.

Disconnect the secondary with a loud error as to why (incompatible change of the device below). (Re-)negotiate barrier capability at connect time; then, resync.

Regards,
Lars

Signature · 3 lines
SuSE Labs, OPS Engineering, Novell, Inc.
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
"Experience is the name everyone gives to their mistakes." -- Oscar Wilde
author maintainer
#75 lore raw
In reply to Willy Tarreau

Am Sonntag 03 Mai 2009 07:36:00 schrieb Willy Tarreau:

Quoted 5 lines · Bart Van Assche
Quoted 4 lines

On Fri, May 1, 2009 at 10:59 AM, Andrew Morton

Quoted 1 line · <[email protected]> wrote:

On Thu, 30 Apr 2009 13:26:36 +0200 Philipp Reisner

Quoted 9 lines · <[email protected]> wrote:
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

This is a repost of DRBD

Is it being used anywhere for anything? ?If so, where and what?

One popular application is to run iSCSI and HA software on top of DRBD in order to build a highly available iSCSI storage target.

Confirmed, I have several customers who're doing exactly that.

Besides storage targets, DRBD is also very popular for databases, it is widely used with PostgreSQL and MySQL. It is advertised by both database projects, to use their DB on top of DRBD to form HA clusters.

Raw numbers of installations:

We have an opt-in global usage counter. See http://www.drbd.org/usage/year/
If we assume that 30% of all users agree that their DRBD installation gets counted, then we have more then 12000 new installations in April. (4245 Installations where counted)

It seems that nowadays most of our users get DRBD through their distributions. Distributions that include DRBD are (list incomplete):
Debian, Ubuntu, SLES, CentOS

-Phil

#76 lore raw

Philipp Reisner skrev:

Quoted 2 lines

It seems that nowadays most of our users get DRBD through their distributions. Distributions that include DRBD are (list incomplete): Debian, Ubuntu, SLES, CentOS

Mandriva has been including it since ~4 years too...

--
Thomas

#77 lore raw
In reply to Philipp Reisner
Quoted 8 lines · On Thursday April 30, [email protected] wrote:

Hi,

This is a repost of DRBD, to keep you updated about the ongoing cleanups and improvements.

Patch set attached. Git tree available:
git pull git://git.drbd.org/linux-2.6-drbd.git drbd

Quoted 8 lines

We are looking for reviews!

Description

DRBD is a shared-nothing, synchronously replicated block device. It is designed to serve as a building block for high availability clusters and in this context, is a "drop-in" replacement for shared storage. Simplistically, you could see it as a network RAID 1.

I know this is minor, but it bugs me every time I see that phrase
"shared-nothing". Surely the network is shared?? And the code...
Can you just say "DRBD is a synchronously replicated block device"? or would we have to call it SRBD then?
Or maybe "shared-nothing" is an accepted technical term in the clustering world??

Quoted 4 lines

Although I use the "RAID1+NBD" metaphor myself, recent discussion unveiled that one needs to understand the differences as well. Here are just two examples of that:

All this should probably be in a patch against Documentation/drbd.txt

Quoted 4 lines

1) Think of a two node HA cluster. Node A is active ('primary' in DRBD speak) has the filesystem mounted and the application running. Node B is in standby mode ('secondary' in DRBD speak).

If there some strong technical reason to only allow 2 nodes? Was it Asimov who said the only sensible numbers were 0, 1, and infinity? (People still get surprised that md/raid1 can do 2 or 3 or n drives, and that md/raid5 can handle just 2 :-)

Quoted 2 lines

We loose network connectivity, the primary node continues to run, the

lose

Quoted 4 lines

secondary no longer gets updates.

Then we have a complete power failure, both nodes are down. Then they power up the data center again, but at first the get only the power

they

Quoted 11 lines

circuit of node B up and running again.

Should node B offer the service right now ?
( DRBD has configurable policies for that )

Later on they manage to get node A up and running again, now lets assume node B was chosen to be the new primary node. What needs to be done ?

Modifications on B since it became primary needs to be resynced to A. Modifications on A sind it lost contact to B needs to be taken out.

Quoted 6 lines

DRBD does that.

How do you fit that into a RAID1+NBD model ? NBD is just a block transport, it does not offer the ability to exchange dirty bitmaps or data generation identifiers, nor does the RAID1 code has a concept of that.

Not 100% true, but I - at least partly - get your point. As md stores bitmaps and data generation identifiers on the block device, these can be transferred over NBD just like any other data on the block device.
However I think that part of your point is that DRBD can transfer them more efficiently (e.g. it compresses the bitmap before transferring it
- I assume the compression you use is much more effective than gzip?? else why both to code your own).
I suspect there is more to your point that I am missing. You say "nor does the RAID1 code has a concept of that". It isn't clear what you are referring to. RAID1 does have a concept of dirty bitmaps as you know, and it does have a concept of data generation, though it is quite possibly weaker than the concept that DRBD has. I'd need to explore the DRBD code more to be sure.

Quoted 8 lines

2) When using DRBD over small bandwidth links, one has to run a resync, DRBD offers the option to do a "checksum based resync". Similar to rsync it at first only exchanges a checksum, and transmits the whole data block only if the checksums differ.

That again is something that does not fit into the concepts of NBD or RAID1.

Interesting idea.... RAID1 does have a mode where it reads both (all) devices and compares them to see if they match or not. Doing this compare with checksums rather than memcmp would not be an enormous change.

I'm beginning to imagine an enhanced NBD as a model for what DRBD does.
This enhanced NBD not only supports read and write of blocks but also:

- maintains the local bitmap and sets bits before allowing a write
- can return a strong checksum rather than the data of a block
- provides sequence numbers in a way that I don't fully understand yet, but which allows consistent write ordering.
- allows reads to be compressed so that the bitmap can be transferred efficiently.

I can imagine that md/raid1 could be made to work well with an enhanced NBD like this.

Quoted 7 lines

DRBD can also be used in dual-Primary mode (device writable on both nodes), which means it can exhibit shared disk semantics in a shared-nothing cluster. Needless to say, on top of dual-Primary DRBD utilizing a cluster file system is necessary to maintain for cache coherency.

Quoted 7 lines

More background on this can be found in this paper:
http://www.drbd.org/fileadmin/drbd/publications/drbd8.pdf

Beyond that, DRBD addresses various issues of cluster partitioning, which the MD/NBD stack, to the best of our knowledge, does not solve. The above-mentioned paper goes into some detail about that as well.

Agreed - MD/NBD could probably be easily confused by cluster partitioning, though I suspect that in many simple cases it would get it right. I haven't given it enough thought to be sure. I doubt the enhancements necessary would be very significant though.

Quoted 6 lines

DRBD can operate in synchronous mode, or in asynchronous mode. I want to point out that we guarantee not to violate a single possible write after write dependency when writing on the standby node. More on that can be found in this paper:
http://www.drbd.org/fileadmin/drbd/publications/drbd_lk9.pdf

I really must read and understand this paper..

So... what would you think of working towards incorporating all of the DRBD functionality into md/raid1??
I suspect that it would be a mutually beneficial exercise, except for the small fact that it would take a significant amount of time and effort. I'd be will to shuffle some priorities and put in some effort if it was a direction that you would be open to exploring.

Whether the current DRBD code gets merged or not is possibly a separate question, though I would hope that if we followed the path of merging DRBD into md/raid1, then any duplicate code would eventually be excised from the kernel.

What do you think?

NeilBrown

#78 lore raw

I am not a DRDB developer, but I can answer some of your questions below.

Quoted 13 lines · Neil Brown
Quoted 12 lines · On Thursday April 30, [email protected] wrote:

Hi,

This is a repost of DRBD, to keep you updated about the ongoing cleanups and improvements.

Patch set attached. Git tree available:
git pull git://git.drbd.org/linux-2.6-drbd.git drbd

We are looking for reviews!

Description

Quoted 7 lines
Quoted 4 lines

DRBD is a shared-nothing, synchronously replicated block device. It is designed to serve as a building block for high availability clusters and in this context, is a "drop-in" replacement for shared storage. Simplistically, you could see it as a network RAID 1.

I know this is minor, but it bugs me every time I see that phrase
"shared-nothing". Surely the network is shared??

the logical network(s) as a whole are shared, but physicaly they can be redundant, multi-pathed, etc.

Quoted 5 lines

And the code...
Can you just say "DRBD is a synchronously replicated block device"? or would we have to call it SRBD then?
Or maybe "shared-nothing" is an accepted technical term in the clustering world??

DRDB can be configured to be synchronous or asynchronous.

'shared-nothing' is a accepted technical term in the clustering world for when two systems are not using any single device.

in the case of a network, I commonly setup systems where the network has two switches (connected togeather with fiber so that an electrical problem in one switch cannot short out the other) with the primary box plugged into one switch and the backup box plugged into another. I also make sure that my systems primary and backup systems are in seperate racks, so that if something goes wrong in one rack that causes an excessive amount of heat it won't affect the backup systems (and yes, this has happened to me when I got lazy and stopped checking on this)

at this point the network switch is not shared (although the logical network is)

in the case of disk storage the common situation is 'shared-disk' where you have one disk array and both machines are plugged into it.

this gives you a single point of failure if the disk array crashes (even if it has redundant controllers, power supplies, etc things still happen), and the disk array can only be in one physical location.

DRDB lets you logicly setup your systems as if they were a 'shared-disk' architecture, but with the hardware being 'shared-nothing'

you can have the two halves of the cluster in different states, so that even a major disaster like a earthquake won't kill the system. (a classic case of 'shared-nothing'

Quoted 9 lines
Quoted 4 lines

1) Think of a two node HA cluster. Node A is active ('primary' in DRBD speak) has the filesystem mounted and the application running. Node B is in standby mode ('secondary' in DRBD speak).

If there some strong technical reason to only allow 2 nodes? Was it Asimov who said the only sensible numbers were 0, 1, and infinity? (People still get surprised that md/raid1 can do 2 or 3 or n drives, and that md/raid5 can handle just 2 :-)

in this case we have 1 replica (or '1 other machine'), so we are on an 'interesting number' ;-)

many people would love to see DRDB extended beyond this, but my understanding is that doing so in non-trivial.

Quoted 14 lines
Quoted 6 lines

DRBD can also be used in dual-Primary mode (device writable on both nodes), which means it can exhibit shared disk semantics in a shared-nothing cluster. Needless to say, on top of dual-Primary DRBD utilizing a cluster file system is necessary to maintain for cache coherency.

Quoted 7 lines

More background on this can be found in this paper:
http://www.drbd.org/fileadmin/drbd/publications/drbd8.pdf

Beyond that, DRBD addresses various issues of cluster partitioning, which the MD/NBD stack, to the best of our knowledge, does not solve. The above-mentioned paper goes into some detail about that as well.

Quoted 4 lines

Agreed - MD/NBD could probably be easily confused by cluster partitioning, though I suspect that in many simple cases it would get it right. I haven't given it enough thought to be sure. I doubt the enhancements necessary would be very significant though.

think of two different threads doing writes directly to their side of the mirror and the system needs to notice this happening and copy the data to the other half of the mirror (with GFS working above you to coordinate the two threads and make sure they don't make conflicting writes)

it's not a trivial task.

David Lang

maintainer
#79 lore raw
In reply to Neil Brown
Quoted 5 lines · Neil Brown

I know this is minor, but it bugs me every time I see that phrase "shared-nothing".

Or maybe "shared-nothing" is an accepted technical term in the clustering world??

yes.

Quoted 1 line

All this should probably be in a patch against Documentation/drbd.txt

Ok.

Quoted 5 lines
Quoted 3 lines

1) Think of a two node HA cluster. Node A is active ('primary' in DRBD speak) has the filesystem mounted and the application running. Node B is in standby mode ('secondary' in DRBD speak).

If there some strong technical reason to only allow 2 nodes?

It "just" has not yet been implemented.
I'm working on that, though.

Quoted 9 lines
Quoted 4 lines

How do you fit that into a RAID1+NBD model ? NBD is just a block transport, it does not offer the ability to exchange dirty bitmaps or data generation identifiers, nor does the RAID1 code has a concept of that.

Not 100% true, but I - at least partly - get your point. As md stores bitmaps and data generation identifiers on the block device, these can be transferred over NBD just like any other data on the block device.

Do you have one dirty bitmap per mirror (yet) ?
Do you _merge_ them?

the "NBD" mirrors are remote, and once you lose communication, they may be (and in general, you have to assume they are) modified by which ever node they are directly attached to.

Quoted 4 lines

However I think that part of your point is that DRBD can transfer them more efficiently (e.g. it compresses the bitmap before transferring it
- I assume the compression you use is much more effective than gzip?? else why both to code your own).

No, the point was that we have one bitmap per mirror (though currently number of mirrors == 2, only), and that we do merge them.

but to answer the question:
why bother to implement our own encoding? because we know a lot about the data to be encoded.

the compression of the bitmap transfer we just added very recently. for a bitmap, with large chunks of bits set or unset, it is efficient to just code the runlength.
to use gzip in kernel would add yet an other huge overhead for code tables and so on.
during testing of this encoding, applying it to an already gzip'ed file was able to compress it even further, btw.
though on english plain text, gzip compression is _much_ more effective.

Quoted 2 lines

You say "nor does the RAID1 code has a concept of that". It isn't clear what you are referring to.

The concept that one of the mirrors (the "nbd" one in that picture) may have been accessed independently, without MD knowning, because the node this MD (and its "local" mirror) was living on suffered from power outage.

The concept of both mirrors being modified _simultaneously_, (e.g. living below a cluster file system).

Quoted 13 lines
Quoted 7 lines

2) When using DRBD over small bandwidth links, one has to run a resync, DRBD offers the option to do a "checksum based resync". Similar to rsync it at first only exchanges a checksum, and transmits the whole data block only if the checksums differ.

That again is something that does not fit into the concepts of NBD or RAID1.

Interesting idea.... RAID1 does have a mode where it reads both (all) devices and compares them to see if they match or not. Doing this compare with checksums rather than memcmp would not be an enormous change.

Quoted 5 lines

I'm beginning to imagine an enhanced NBD as a model for what DRBD does. This enhanced NBD not only supports read and write of blocks but also:

- maintains the local bitmap and sets bits before allowing a write

right.

Quoted 1 line

- can return a strong checksum rather than the data of a block

ok.

Quoted 2 lines

- provides sequence numbers in a way that I don't fully understand yet, but which allows consistent write ordering.

yes, please.

Quoted 2 lines

- allows reads to be compressed so that the bitmap can be transferred efficiently.

yep.

add to that
- can exchange data generations on handshake,
- can refuse the handshake (consistent data, but evolved differently than the other copy; diverging data sets detected!)
- is bi-directional, can _push_ writes!

and whatever else I forgot just now.

Quoted 2 lines

I can imagine that md/raid1 could be made to work well with an enhanced NBD like this.

of course.

Quoted 14 lines
Quoted 6 lines

DRBD can also be used in dual-Primary mode (device writable on both nodes), which means it can exhibit shared disk semantics in a shared-nothing cluster. Needless to say, on top of dual-Primary DRBD utilizing a cluster file system is necessary to maintain for cache coherency.

Quoted 7 lines

More background on this can be found in this paper:
http://www.drbd.org/fileadmin/drbd/publications/drbd8.pdf

Beyond that, DRBD addresses various issues of cluster partitioning, which the MD/NBD stack, to the best of our knowledge, does not solve. The above-mentioned paper goes into some detail about that as well.

Quoted 4 lines

Agreed - MD/NBD could probably be easily confused by cluster partitioning, though I suspect that in many simple cases it would get it right. I haven't given it enough thought to be sure. I doubt the enhancements necessary would be very significant though.

The most significant part is probably the bidirectional nature and the "refuse it" part of the handshake.

Quoted 9 lines
Quoted 5 lines

DRBD can operate in synchronous mode, or in asynchronous mode. I want to point out that we guarantee not to violate a single possible write after write dependency when writing on the standby node. More on that can be found in this paper:
http://www.drbd.org/fileadmin/drbd/publications/drbd_lk9.pdf

I really must read and understand this paper..

Quoted 6 lines

So... what would you think of working towards incorporating all of the DRBD functionality into md/raid1??
I suspect that it would be a mutually beneficial exercise, except for the small fact that it would take a significant amount of time and effort. I'd be will to shuffle some priorities and put in some effort if it was a direction that you would be open to exploring.

Sure. But yes, full ack on the time and effort part ;)

Quoted 4 lines

Whether the current DRBD code gets merged or not is possibly a separate question, though I would hope that if we followed the path of merging DRBD into md/raid1, then any duplicate code would eventually be excised from the kernel.

Rumor [http://lwn.net/Articles/326818/] has it, that the various in kernel raid implementations are being unified right now, anyways?

If you want to stick to "replication is almost identical to RAID1", best not to forget "this may be a remote mirror", there may be more than one entity accessing it, this may be part of a bi-directional (active-active) replication setup.

For further ideas on what could be done with replication (enhancing the strict "raid1" notion), see also
http://www.drbd.org/fileadmin/drbd/publications/drbd9.linux-kongress.2008.pdf

- time shift replication
- generic point in time recovery of block device data
- (remote) backup by periodically, round-robin re-sync of "raid" members, then "dropping" them again.
...

No useable code on those ideas, yet,
but a lot of thought. It is not all handwaving.

Lars

#80 lore raw
Quoted 4 lines · On Sunday May 3, [email protected] wrote:
Quoted 1 line

If there some strong technical reason to only allow 2 nodes?

It "just" has not yet been implemented.
I'm working on that, though.

:-)

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

How do you fit that into a RAID1+NBD model ? NBD is just a block transport, it does not offer the ability to exchange dirty bitmaps or data generation identifiers, nor does the RAID1 code has a concept of that.

Quoted 7 lines
Quoted 4 lines

Not 100% true, but I - at least partly - get your point. As md stores bitmaps and data generation identifiers on the block device, these can be transferred over NBD just like any other data on the block device.

Do you have one dirty bitmap per mirror (yet) ?
Do you _merge_ them?

md doesn't merge bitmaps yet. However if I found a need to, I would simple read a bitmap in userspace and feed it into the kernel via
/sys/block/mdX/md/md/bitmap_set_bits

We sort-of have one bitmap per mirror, but only because the one bitmap is mirrored...

Quoted 5 lines

the "NBD" mirrors are remote, and once you lose communication, they may be (and in general, you have to assume they are) modified by which ever node they are directly attached to.

Quoted 7 lines
Quoted 4 lines

However I think that part of your point is that DRBD can transfer them more efficiently (e.g. it compresses the bitmap before transferring it
- I assume the compression you use is much more effective than gzip?? else why both to code your own).

No, the point was that we have one bitmap per mirror (though currently number of mirrors == 2, only), and that we do merge them.

Right. I imagine much of the complexity of that could be handled in user-space while setting an a DRBD instance (??).

Quoted 5 lines

but to answer the question:
why bother to implement our own encoding? because we know a lot about the data to be encoded.

Quoted 8 lines

the compression of the bitmap transfer we just added very recently. for a bitmap, with large chunks of bits set or unset, it is efficient to just code the runlength.
to use gzip in kernel would add yet an other huge overhead for code tables and so on.
during testing of this encoding, applying it to an already gzip'ed file was able to compress it even further, btw.
though on english plain text, gzip compression is _much_ more effective.

I just tried a little experiment.
I created a 128meg file and randomly set 1000 bits in it. I compressed it with "gzip --best" and the result was 4Meg. Not particularly impressive.
I then tried to compress it wit bzip2 and got 3452 bytes. Now *that* is impressive. I suspect your encoding might do a little better, but I wonder if it is worth the effort.
I'm not certain that my test file is entirely realistic, but it is still an interesting experiment.

Why do you do this compression in the kernel? It seems to me that it would be quite practical to do it all in user-space, thus making it really easy to use pre-existing libraries.

BTW, the kernel already contains various compression code as part of the crypto API.

Quoted 11 lines
Quoted 3 lines

You say "nor does the RAID1 code has a concept of that". It isn't clear what you are referring to.

The concept that one of the mirrors (the "nbd" one in that picture) may have been accessed independently, without MD knowning, because the node this MD (and its "local" mirror) was living on suffered from power outage.

The concept of both mirrors being modified _simultaneously_, (e.g. living below a cluster file system).

Yes, that is an important concept. Certainly one of the bits that would need to be added to md.

Quoted 7 lines
Quoted 4 lines

Whether the current DRBD code gets merged or not is possibly a separate question, though I would hope that if we followed the path of merging DRBD into md/raid1, then any duplicate code would eventually be excised from the kernel.

Rumor [http://lwn.net/Articles/326818/] has it, that the various in kernel raid implementations are being unified right now, anyways?

I'm not holding my breath on that one...
I think that merging DRBD with md/raid1 would be significantly easier that any sort of merge between md and dm. But (in either case) I'll do what I can to assist any effort that is technically sound.

Quoted 10 lines

If you want to stick to "replication is almost identical to RAID1", best not to forget "this may be a remote mirror", there may be more than one entity accessing it, this may be part of a bi-directional (active-active) replication setup.

For further ideas on what could be done with replication (enhancing the strict "raid1" notion), see also
http://www.drbd.org/fileadmin/drbd/publications/drbd9.linux-kongress.2008.pdf

Quoted 8 lines

- time shift replication
- generic point in time recovery of block device data
- (remote) backup by periodically, round-robin re-sync of "raid" members, then "dropping" them again.
...

No useable code on those ideas, yet,
but a lot of thought. It is not all handwaving.

:-)

I'll have to do a bit of reading I see. I'll then try to rough out a design and plan for merging DRBD functionality with md/raid1. At the very least that would give me enough background understanding to be able to sensibly review your code submission.

NeilBrown

maintainer
#81 lore raw
Quoted 19 lines · Neil Brown
Quoted 4 lines · On Sunday May 3, [email protected] wrote:
Quoted 1 line

If there some strong technical reason to only allow 2 nodes?

It "just" has not yet been implemented.
I'm working on that, though.

:-)

Quoted 11 lines
Quoted 10 lines
Quoted 5 lines

How do you fit that into a RAID1+NBD model ? NBD is just a block transport, it does not offer the ability to exchange dirty bitmaps or data generation identifiers, nor does the RAID1 code has a concept of that.

Not 100% true, but I - at least partly - get your point. As md stores bitmaps and data generation identifiers on the block device, these can be transferred over NBD just like any other data on the block device.

Quoted 6 lines
Quoted 2 lines

Do you have one dirty bitmap per mirror (yet) ?
Do you _merge_ them?

md doesn't merge bitmaps yet. However if I found a need to, I would simple read a bitmap in userspace and feed it into the kernel via
/sys/block/mdX/md/md/bitmap_set_bits

ah, ok. right. that would do it.

Quoted 2 lines

We sort-of have one bitmap per mirror, but only because the one bitmap is mirrored...

Which it could not be while replication link is down, so once replication link is back (or remote node is back, which is not easily distinguishable just there, blablabla), you'd need to fetch the remote bitmap, and merge it with the local bitmap (feeding it into bitmap_set_bits),
then re-attach the "failed" mirror.

The reasoning in the commit 9b1d1dac181d8c1b9492e05cee660a985d035a06, which adds that feature, exactly describes this use case.

There, again, our simple run-length encoding scheme does make very much sense, as the numbers dropping out of it during decoding are exactly the runlengths, and could be fed into this almost directly.

Quoted 9 lines
Quoted 9 lines

the "NBD" mirrors are remote, and once you lose communication, they may be (and in general, you have to assume they are) modified by which ever node they are directly attached to.

Quoted 4 lines

However I think that part of your point is that DRBD can transfer them more efficiently (e.g. it compresses the bitmap before transferring it
- I assume the compression you use is much more effective than gzip?? else why both to code your own).

Quoted 5 lines
Quoted 2 lines

No, the point was that we have one bitmap per mirror (though currently number of mirrors == 2, only), and that we do merge them.

Right. I imagine much of the complexity of that could be handled in user-space while setting an a DRBD instance (??).

possibly.
you'd need to involve these steps on each and every communication loss and network handshake. I think that would make the system slower to react on e.g. "flaky" replication links.

you are thinking in the "MD" paradigma: at any point in time, there is only one MD instance involved, the mirror transports (currently dumb block devices) simply do what they are told.

in DRBD, we have multiple (ok, two) instances talking to each other, and I think that is the better approach for (remote) replication.

Quoted 13 lines
Quoted 4 lines

but to answer the question:
why bother to implement our own encoding? because we know a lot about the data to be encoded.

Quoted 8 lines

the compression of the bitmap transfer we just added very recently. for a bitmap, with large chunks of bits set or unset, it is efficient to just code the runlength.
to use gzip in kernel would add yet an other huge overhead for code tables and so on.
during testing of this encoding, applying it to an already gzip'ed file was able to compress it even further, btw.
though on english plain text, gzip compression is _much_ more effective.

Quoted 7 lines

I just tried a little experiment.
I created a 128meg file and randomly set 1000 bits in it. I compressed it with "gzip --best" and the result was 4Meg. Not particularly impressive.
I then tried to compress it wit bzip2 and got 3452 bytes. Now *that* is impressive. I suspect your encoding might do a little better, but I wonder if it is worth the effort.

The effort is minimal.
The cpu overhead is negligible (compared with bzip2, or any other generic compression scheme), and the memory overhead is next to none (just a small scratch buffer, to assemble the network packet). No tables or anything involved.
Especially the _decoding_ part has this nice property:
chunk = 0;

  while (!eof) {
	vli_decode_bits(&rl, input); /* number of unset bits */
	chunk += rl;
	vli_decode_bits(&rl, input); /* number of set bits */
	bitmap_dirty_bits(bitmap, chunk, chunk + rl);
	chunk += rl;

}

The source code is there.

For your example, on average you'd have (128 << 23) / 1000 "clear" bits, then one set bit. The encoding transfers
"first bit unset -- ca. (1<<20), 1, ca. (1<<20), 1, ca. (1<<20), 1, ...", using 2 bits for the "1", and up to 29 bit for the "ca. 1<<20". should be in the very same ballpark as your bzip2 result.

Quoted 2 lines

I'm not certain that my test file is entirely realistic, but it is still an interesting experiment.

It is not ;) but still...
If you are interessted, I can dig up my throw away user land code, that has been used to evaluate various such schemes. But it is so ugly that I won't post it to lkml.

Quoted 3 lines

Why do you do this compression in the kernel? It seems to me that it would be quite practical to do it all in user-space, thus making it really easy to use pre-existing libraries.

Because the bitmap exchange happens in kernel.

If considering to rewrite a replication solution,
one can start to reconsider design choices.

But DRBD as of now does the connection handshake and bitmap exchange in kernel. We wanted to have a fast compression scheme suitable for bitmaps, without cpu or memory overhead. This does it quite nicely.

I can dig up my userland throwaway code used during evaluation of various encoding schemes again, if you are interessted.

Quoted 2 lines

BTW, the kernel already contains various compression code as part of the crypto API.

Of course I know. But you are not really suggesting that I should do bzip2 in kernel to exchange the bitmap. And on decoding, I want those runlengths, not the actual plain bitmap.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

You say "nor does the RAID1 code has a concept of that". It isn't clear what you are referring to.

The concept that one of the mirrors (the "nbd" one in that picture) may have been accessed independently, without MD knowning, because the node this MD (and its "local" mirror) was living on suffered from power outage.

or the link has been down,
and the remote side decided to go active with it.

or the link has been taken down,
to activate the other side, knowingly creating a data set divergence, to do some off-site processing.

Quoted 11 lines
Quoted 2 lines

The concept of both mirrors being modified _simultaneously_, (e.g. living below a cluster file system).

Yes, that is an important concept. Certainly one of the bits that would need to be added to md.

Quoted 5 lines
Quoted 4 lines

Whether the current DRBD code gets merged or not is possibly a separate question, though I would hope that if we followed the path of merging DRBD into md/raid1, then any duplicate code would eventually be excised from the kernel.

Quoted 7 lines
Quoted 2 lines

Rumor [http://lwn.net/Articles/326818/] has it, that the various in kernel raid implementations are being unified right now, anyways?

I'm not holding my breath on that one...
I think that merging DRBD with md/raid1 would be significantly easier that any sort of merge between md and dm. But (in either case) I'll do what I can to assist any effort that is technically sound.

D'accord.

Quoted 18 lines
Quoted 9 lines

If you want to stick to "replication is almost identical to RAID1", best not to forget "this may be a remote mirror", there may be more than one entity accessing it, this may be part of a bi-directional (active-active) replication setup.

For further ideas on what could be done with replication (enhancing the strict "raid1" notion), see also
http://www.drbd.org/fileadmin/drbd/publications/drbd9.linux-kongress.2008.pdf

Quoted 8 lines

- time shift replication
- generic point in time recovery of block device data
- (remote) backup by periodically, round-robin re-sync of "raid" members, then "dropping" them again.
...

No useable code on those ideas, yet,
but a lot of thought. It is not all handwaving.

Quoted 6 lines

:-)

I'll have to do a bit of reading I see. I'll then try to rough out a design and plan for merging DRBD functionality with md/raid1. At the very least that would give me enough background understanding to be able to sensibly review your code submission.

Thanks. Please give particular attention to the "taxonomy paper" referenced therein, so we are going to use the same terms.

Lars

#82 lore raw
Quoted 6 lines · Lars Ellenberg

Which it could not be while replication link is down, so once replication link is back (or remote node is back, which is not easily distinguishable just there, blablabla), you'd need to fetch the remote bitmap, and merge it with the local bitmap (feeding it into bitmap_set_bits),
then re-attach the "failed" mirror.

Note that this sacrifices transactional consistency on the sync target; an understandable trade-off (versus recording the stream of writes entirely, which consumes space and possibly more resync bandwidth), but a noteworthy one.

Quoted 3 lines

But DRBD as of now does the connection handshake and bitmap exchange in kernel. We wanted to have a fast compression scheme suitable for bitmaps, without cpu or memory overhead. This does it quite nicely.

Sharing the connection between meta- and regular data also avoids some ordering issues between channels, which probably helps simplify some aspects of drbd.

Conceivably, the kernel could escalate such metadata/out-of-band communications to user-space for handling, and user-space would then afterwards instruct the continuation of the stream processing.

Quoted 2 lines

or the link has been down,
and the remote side decided to go active with it.

That is arguably a horrible failure on behalf of the cluster stack being used, but indeed something drbd must be able to recover from.

Regards,
Lars

Signature · 3 lines
SuSE Labs, OPS Engineering, Novell, Inc.
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
"Experience is the name everyone gives to their mistakes." -- Oscar Wilde
maintainer
#83 lore raw
In reply to Lars Ellenberg
Quoted 44 lines
Quoted 13 lines
Quoted 4 lines

but to answer the question:
why bother to implement our own encoding? because we know a lot about the data to be encoded.

Quoted 8 lines

the compression of the bitmap transfer we just added very recently. for a bitmap, with large chunks of bits set or unset, it is efficient to just code the runlength.
to use gzip in kernel would add yet an other huge overhead for code tables and so on.
during testing of this encoding, applying it to an already gzip'ed file was able to compress it even further, btw.
though on english plain text, gzip compression is _much_ more effective.

Quoted 7 lines

I just tried a little experiment.
I created a 128meg file and randomly set 1000 bits in it. I compressed it with "gzip --best" and the result was 4Meg. Not particularly impressive.
I then tried to compress it wit bzip2 and got 3452 bytes. Now *that* is impressive. I suspect your encoding might do a little better, but I wonder if it is worth the effort.

The effort is minimal.
The cpu overhead is negligible (compared with bzip2, or any other generic compression scheme), and the memory overhead is next to none (just a small scratch buffer, to assemble the network packet). No tables or anything involved.
Especially the _decoding_ part has this nice property:
chunk = 0;

  while (!eof) {
	vli_decode_bits(&rl, input); /* number of unset bits */
	chunk += rl;
	vli_decode_bits(&rl, input); /* number of set bits */
	bitmap_dirty_bits(bitmap, chunk, chunk + rl);
	chunk += rl;

}

The source code is there.

For your example, on average you'd have (128 << 23) / 1000 "clear" bits, then one set bit. The encoding transfers
"first bit unset -- ca. (1<<20), 1, ca. (1<<20), 1, ca. (1<<20), 1, ...", using 2 bits for the "1", and up to 29 bit for the "ca. 1<<20". should be in the very same ballpark as your bzip2 result.

Quoted 7 lines
Quoted 2 lines

I'm not certain that my test file is entirely realistic, but it is still an interesting experiment.

It is not ;) but still...
If you are interessted, I can dig up my throw away user land code, that has been used to evaluate various such schemes. But it is so ugly that I won't post it to lkml.

I found round about ten different versions of that throw away code. Oh well. So I just hacked up an other one.

For your entertainment, prepare some example bitmaps. From all my real-world example bitmaps I can see that (at least with 4KiB bitmap granularity), areas with alternating single-bit (which is the only run length that does not compress) are rare.

Comments wellcome. Have fun.

Lars

/* vim: set foldmethod=marker foldlevel=1 foldenable :
*
* Copyright 2009 Lars Ellenberg <[email protected]>
* Licence: GPL
*
* Purpose: demonstrate the simple, but efficient (for bitmaps, anyways),
* encoding (usually: compression) used to exchange the DRBD bitmap.
* Note: DRBD transmits incompressible chunks as plain text.
* This demo does not, to better show the properties of the encoding method.
* See also the comments just above the "struct code_chunk"
*
* More than half of this file is (almost) verbatim copy
* from other .c and .h files, so you won't need the extra files,
* like the generic find_next_bit.c or the drbd_vli.h.
*
* Tested on i686 and x86_64 Debian.
* Might have issues on other archs, though I think it should not. *
* For USAGE,
* see show_usage_and_die() below. */

#include <sys/mman.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <linux/types.h>
#include <errno.h>
#include <endian.h>
#include <byteswap.h>

/* gcc -g -O3 -Wall -o vli_bitstream_demo vli_bitstream_demo.c */ /* for easy debugging with gdb, do

 *  gdb --args ./x 3< IN 4> OUT
 *  then set in_fd and out_fd to 3 and 4,
 *  and "run".

*/
static int in_fd = 0; /* 0: stdin */
static int out_fd = 1; /* 1: stdout */

/* (almost) verbatim copied files from elsewhere {{{2 */

/* find bit helpers from linux kernel tree {{{3 */

/* taken from arch/x86/include/asm/bitops.h {{{4 */ static inline unsigned long __ffs(unsigned long word)

{
	asm("bsf %1,%0"
		: "=r" (word)
		: "rm" (word));
	return word;
}

static inline unsigned long ffz(unsigned long word)

{
	asm("bsf %1,%0"
		: "=r" (word)
		: "r" (~word));
	return word;
}

/* taken from lib/find_next_bit.c {{{4 */

/* find_next_bit.c: fallback find next bit implementation
*
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
* Written by David Howells ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version. */

#define BITS_PER_LONG (sizeof(long)*8)
#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)

/*
* Find the next set bit in a memory region.
*/
unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
unsigned long offset)

{
	const unsigned long *p = addr + BITOP_WORD(offset);
	unsigned long result = offset & ~(BITS_PER_LONG-1);
	unsigned long tmp;

	if (offset >= size)
		return size;
	size -= result;
	offset %= BITS_PER_LONG;
	if (offset) {
		tmp = *(p++);
		tmp &= (~0UL << offset);
		if (size < BITS_PER_LONG)
			goto found_first;
		if (tmp)
			goto found_middle;
		size -= BITS_PER_LONG;
		result += BITS_PER_LONG;
	}
	while (size & ~(BITS_PER_LONG-1)) {
		if ((tmp = *(p++)))
			goto found_middle;
		result += BITS_PER_LONG;
		size -= BITS_PER_LONG;
	}
	if (!size)
		return result;
	tmp = *p;

found_first:

	tmp &= (~0UL >> (BITS_PER_LONG - size));
	if (tmp == 0UL)		/* Are any bits set? */
		return result + size;	/* Nope. */
found_middle:
	return result + __ffs(tmp);
}

/*
* This implementation of find_{first,next}_zero_bit was stolen from
* Linus' asm-alpha/bitops.h.
*/
unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
unsigned long offset)

{
	const unsigned long *p = addr + BITOP_WORD(offset);
	unsigned long result = offset & ~(BITS_PER_LONG-1);
	unsigned long tmp;

	if (offset >= size)
		return size;
	size -= result;
	offset %= BITS_PER_LONG;
	if (offset) {
		tmp = *(p++);
		tmp |= ~0UL >> (BITS_PER_LONG - offset);
		if (size < BITS_PER_LONG)
			goto found_first;
		if (~tmp)
			goto found_middle;
		size -= BITS_PER_LONG;
		result += BITS_PER_LONG;
	}
	while (size & ~(BITS_PER_LONG-1)) {
		if (~(tmp = *(p++)))
			goto found_middle;
		result += BITS_PER_LONG;
		size -= BITS_PER_LONG;
	}
	if (!size)
		return result;
	tmp = *p;

found_first:

	tmp |= ~0UL << size;
	if (tmp == ~0UL)	/* Are any bits zero? */
		return result + size;	/* Nope. */
found_middle:
	return result + ffz(tmp);
}

/*
* Find the first set bit in a memory region.
*/
unsigned long find_first_bit(const unsigned long *addr, unsigned long size)

{
	const unsigned long *p = addr;
	unsigned long result = 0;
	unsigned long tmp;

	while (size & ~(BITS_PER_LONG-1)) {
		if ((tmp = *(p++)))
			goto found;
		result += BITS_PER_LONG;
		size -= BITS_PER_LONG;
	}
	if (!size)
		return result;

	tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
	if (tmp == 0UL)		/* Are any bits set? */
		return result + size;	/* Nope. */
found:
	return result + __ffs(tmp);
}

/*
* Find the first cleared bit in a memory region.
*/
unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)

{
	const unsigned long *p = addr;
	unsigned long result = 0;
	unsigned long tmp;

	while (size & ~(BITS_PER_LONG-1)) {
		if (~(tmp = *(p++)))
			goto found;
		result += BITS_PER_LONG;
		size -= BITS_PER_LONG;
	}
	if (!size)
		return result;

	tmp = (*p) | (~0UL << size);
	if (tmp == ~0UL)	/* Are any bits zero? */
		return result + size;	/* Nope. */
found:
	return result + ffz(tmp);
}

/* end of find_next_bit.c }}}1 */

/* the VLI code implementation from drbd_vli.h {{{3 */

#define u64 __u64
#define u8 __u8
#define BUG() abort()

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define le64_to_cpu(x) (x)
#elif __BYTE_ORDER == __BIG_ENDIAN
#define le64_to_cpu(x) bswap_64(x)
#else
#error "endian?"
#endif

/*
* At a granularity of 4KiB storage represented per bit,
* and stroage sizes of several TiB,
* and possibly small-bandwidth replication,
* the bitmap transfer time can take much too long,
* if transmitted in plain text.
*
* We try to reduce the transfered bitmap information
* by encoding runlengths of bit polarity.
*
* We never actually need to encode a "zero" (runlengths are positive).
* But then we have to store the value of the first bit.
* The first bit of information thus shall encode if the first runlength
* gives the number of set or unset bits.
*
* We assume that large areas are either completely set or unset,
* which gives good compression with any runlength method,
* even when encoding the runlength as fixed size 32bit/64bit integers. *
* Still, there may be areas where the polarity flips every few bits,
* and encoding the runlength sequence of those areas with fix size
* integers would be much worse than plaintext.
*
* We want to encode small runlength values with minimum code length,
* while still being able to encode a Huge run of all zeros efficiently. *
* Thus we need a Variable Length Integer encoding, VLI.
*
* For some cases, we produce more code bits than plaintext input.
* We need to send incompressible chunks as plaintext, skip over them
* and then see if the next chunk compresses better.
*
* We don't care too much about "excellent" compression ratio for large
* runlengths (all set/all clear): whether we achieve a factor of 100
* or 1000 is not that much of an issue.
* We do not want to waste too much on short runlengths in the "noisy"
* parts of the bitmap, though.
*
* There are endless variants of VLI, we experimented with:
* * simple byte-based
* * various bit based with different code word length.
*
* To avoid yet an other configuration parameter (choice of bitmap compression
* algorithm) which was difficult to explain and tune, we just chose the one
* variant that turned out best in all test cases.
* Based on real world usage patterns, with device sizes ranging from a few GiB
* to several TiB, file server/mailserver/webserver/mysql/postgress,
* mostly idle to really busy, the all time winner (though sometimes only
* marginally better) is:
*/

/*
* encoding is "visualised" as
* __little endian__ bitstream, least significant bit first (left most) *
* this particular encoding is chosen so that the prefix code
* starts as unary encoding the level, then modified so that
* 10 levels can be described in 8bit, with minimal overhead
* for the smaller levels.
*
* Number of data bits follow fibonacci sequence, with the exception of the
* last level (+1 data bit, so it makes 64bit total). The only worse code when
* encoding bit polarity runlength is 1 plain bits => 2 code bits.
prefix data bits max val Nº data bits

0 x                                                         0x2            1
10 x                                                        0x4            1
110 xx                                                      0x8            2
1110 xxx                                                   0x10            3
11110 xxx xx                                               0x30            5
111110 xx xxxxxx                                          0x130            8
11111100  xxxxxxxx xxxxx                                 0x2130           13
11111110  xxxxxxxx xxxxxxxx xxxxx                      0x202130           21
11111101  xxxxxxxx xxxxxxxx xxxxxxxx  xxxxxxxx xx   0x400202130           34
11111111  xxxxxxxx xxxxxxxx xxxxxxxx  xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 56

* maximum encodable value: 0x100000400202130 == 2**56 + some */

/* compression "table":
transmitted x 0.29

 as plaintext x                                  ........................
             x                                   ........................
            x                                    ........................
           x    0.59                         0.21........................
          x      ........................................................
         x       .. c ...................................................
        x    0.44.. o ...................................................
       x .......... d ...................................................
      x  .......... e ...................................................
     X.............   ...................................................
    x.............. b ...................................................

2.0x............... i ...................................................
#X................ t ................................................... #................. s ........................... plain bits ..........
-+-----------------------------------------------------------------------
1 16 32 64
*/

/* LEVEL: (total bits, prefix bits, prefix value),
* sorted ascending by number of total bits.
* The rest of the code table is calculated at compiletime from this. */

/* fibonacci data 1, 1, ... */

#define VLI_L_1_1() do { \
	LEVEL( 2, 1, 0x00); \
	LEVEL( 3, 2, 0x01); \
	LEVEL( 5, 3, 0x03); \
	LEVEL( 7, 4, 0x07); \
	LEVEL(10, 5, 0x0f); \
	LEVEL(14, 6, 0x1f); \
	LEVEL(21, 8, 0x3f); \
	LEVEL(29, 8, 0x7f); \
	LEVEL(42, 8, 0xbf); \
	LEVEL(64, 8, 0xff); \
	} while (0)

/* finds a suitable level to decode the least significant part of in.
* returns number of bits consumed.
*
* BUG() for bad input, as that would mean a buggy code table. */
static inline int vli_decode_bits(u64 *out, const u64 in)

{
	u64 adj = 1;
#define LEVEL(t,b,v)					\
	do {						\
		if ((in & ((1 << b) -1)) == v) {	\
			*out = ((in & ((~0ULL) >> (64-t))) >> b) + adj;	\
			return t;			\
		}					\
		adj += 1ULL << (t - b);			\
	} while (0)

	VLI_L_1_1();

	/* NOT REACHED, if VLI_LEVELS code table is defined properly */
	BUG();

#undef LEVEL
}

/* return number of code bits needed,
* or negative error number */
static inline int __vli_encode_bits(u64 *out, const u64 in)

{
	u64 max = 0;
	u64 adj = 1;

	if (in == 0)
		return -EINVAL;
#define LEVEL(t,b,v) do {		\
		max += 1ULL << (t - b);	\
		if (in <= max) {	\
			if (out)	\
				*out = ((in - adj) << b) | v;	\
			return t;	\
		}			\
		adj = max + 1;		\
	} while (0)

	VLI_L_1_1();

	return -EOVERFLOW;

#undef LEVEL
}

#undef VLI_L_1_1

/* code from here down is independend of actually used bit code */

/*
* Code length is determined by some unique (e.g. unary) prefix.
* This encodes arbitrary bit length, not whole bytes: we have a bit-stream,
* not a byte stream.
*/

/* for the bitstream, we need a cursor */

struct bitstream_cursor {
	/* the current byte */
	u8 *b;
	/* the current bit within *b, nomalized: 0..7 */
	unsigned int bit;
};

/* initialize cursor to point to first bit of stream */
static inline void bitstream_cursor_reset(struct bitstream_cursor *cur, void *s)

{
	cur->b = s;
	cur->bit = 0;
}

/* advance cursor by that many bits; maximum expected input value: 64,
* but depending on VLI implementation, it may be more. */
static inline void bitstream_cursor_advance(struct bitstream_cursor *cur, unsigned int bits)

{
	bits += cur->bit;
	cur->b = cur->b + (bits >> 3);
	cur->bit = bits & 7;
}

/* the bitstream itself knows its length */

struct bitstream {
	struct bitstream_cursor cur;
	unsigned char *buf;
	size_t buf_len;		/* in bytes */

	/* for input stream:
	 * number of trailing 0 bits for padding
	 * total number of valid bits in stream: buf_len * 8 - pad_bits */
	unsigned int pad_bits;
};

static inline void bitstream_init(struct bitstream *bs, void *s, size_t len, unsigned int pad_bits)

{
	bs->buf = s;
	bs->buf_len = len;
	bs->pad_bits = pad_bits;
	bitstream_cursor_reset(&bs->cur, bs->buf);
}

static inline void bitstream_rewind(struct bitstream *bs)

{
	bitstream_cursor_reset(&bs->cur, bs->buf);
	memset(bs->buf, 0, bs->buf_len);
}

/* Put (at most 64) least significant bits of val into bitstream, and advance cursor.
* Ignores "pad_bits".
* Returns zero if bits == 0 (nothing to do).
* Returns number of bits used if successful.
*
* If there is not enough room left in bitstream,
* leaves bitstream unchanged and returns -ENOBUFS.
*/
static inline int bitstream_put_bits(struct bitstream *bs, u64 val, const unsigned int bits)

{
	unsigned char *b = bs->cur.b;
	unsigned int tmp;

	if (bits == 0)
		return 0;

	if ((bs->cur.b + ((bs->cur.bit + bits -1) >> 3)) - bs->buf >= bs->buf_len)
		return -ENOBUFS;

	/* paranoia: strip off hi bits; they should not be set anyways. */
	if (bits < 64)
		val &= ~0ULL >> (64 - bits);

	*b++ |= (val & 0xff) << bs->cur.bit;

	for (tmp = 8 - bs->cur.bit; tmp < bits; tmp += 8)
		*b++ |= (val >> tmp) & 0xff;

	bitstream_cursor_advance(&bs->cur, bits);
	return bits;
}

/* Fetch (at most 64) bits from bitstream into *out, and advance cursor.
*
* If more than 64 bits are requested, returns -EINVAL and leave *out unchanged. *
* If there are less than the requested number of valid bits left in the
* bitstream, still fetches all available bits.
*
* Returns number of actually fetched bits.
*/
static inline int bitstream_get_bits(struct bitstream *bs, u64 *out, int bits)

{
	u64 val;
	unsigned int n;

	if (bits > 64)
		return -EINVAL;

	if (bs->cur.b + ((bs->cur.bit + bs->pad_bits + bits -1) >> 3) - bs->buf >= bs->buf_len)
		bits = ((bs->buf_len - (bs->cur.b - bs->buf)) << 3)
			- bs->cur.bit - bs->pad_bits;

	if (bits == 0) {
		*out = 0;
		return 0;
	}

	/* get the high bits */
	val = 0;
	n = (bs->cur.bit + bits + 7) >> 3;
	/* n may be at most 9, if cur.bit + bits > 64 */
	/* which means this copies at most 8 byte */
	if (n) {
		memcpy(&val, bs->cur.b+1, n - 1);
		val = le64_to_cpu(val) << (8 - bs->cur.bit);
	}

	/* we still need the low bits */
	val |= bs->cur.b[0] >> bs->cur.bit;

	/* and mask out bits we don't want */
	val &= ~0ULL >> (64 - bits);

	bitstream_cursor_advance(&bs->cur, bits);
	*out = val;

	return bits;
}

/* encodes @in as vli into @bs;

* return values
* > 0: number of bits successfully stored in bitstream
* -ENOBUFS @bs is full
* -EINVAL input zero (invalid)
* -EOVERFLOW input too large for this vli code (invalid) */
static inline int vli_encode_bits(struct bitstream *bs, u64 in)

{
	u64 code = code;
	int bits = __vli_encode_bits(&code, in);

	if (bits <= 0)
		return bits;

	return bitstream_put_bits(bs, code, bits);
}

/* end of drbd_vli.h }}}1 */

static char *progname;

void show_usage_and_die(void)
{
fprintf(stderr,
"Usage: %s subcommand subcommand-options ...\n"
" Subcommands:\n"
" plain-to-rl <in_file>:\n"

"    mmap()s in_file,\n"
"    Writes a stream of 32bit native unsigned int to stdout,\n"
"    representing the runlengths of set and unset bits.\n"
"    First runlength is _unset_ bits,\n"
"    and is the only runlength that may be zero.\n\n"

" rl-to-vli\n"

"    Takes the output of 'plain-to-rl' from stdin,\n"
"    and encodes those runlengths into variable length integer (VLI)\n"
"    bitstream chunks (to stdout).\n"
"    export RL_TO_VLI_STATS=any-value gives extra stats on this one.\n\n"

" vli-to-rl\n"

"    Takes the output of 'rl-to-vli' from stdin,\n"
"    and decodes those VLI chunks back into a stream of\n"
"    32bit native unsigned int (to stdout).\n\n"

" rl-to-plain\n"
" Takes a stream of 32bit native unsigned int runlengths from stdin\n"
" and writes the corresponding plaintext to stdout.\n\n"

"Note that in the proper implementation, incompressible chunks\n" "are stored as plain. This is not done here, to better demonstrate\n" "the properties of this method\n\n"

"shell examples:\n"
" do_try() { ( set -vxeC\n"

"    local f=${1:? missing input file};\n"
"    export RL_TO_VLI_STATS=whatever\n"
"    time %s plain-to-rl \"$f\" > RL;\n"
"    time %s rl-to-vli < RL > BS;\n"
"    time %s vli-to-rl < BS > DE;\n"
"    time %s rl-to-plain < DE > R;\n"
"    ls -l \"$f\" R BS RL DE;\n"
"    time cmp RL DE;\n"
"    time cmp \"$f\" R )\n"
"  }\n"
"  # rm -f RL BS DE R # for cleanup, because of set -C\n"
"  do_try example_bitmap\n"

"The bit stream in BS is the compressed representation of infile\n\n"

"Stupid comparison for the fun of it. Prepare some example_bitmap, then:\n" "f=example_bitmap\n"
"time { gzip --best < $f | tee C | gunzip > /dev/null; ls -l C; }\n" "time { bzip2 < $f | tee C | bunzip2 > /dev/null; ls -l C; }\n" "time { %s plain-to-rl $f | %s rl-to-vli |\n"
" tee C | %s vli-to-rl |\n"
" %s rl-to-plain > /dev/null; ls -l C; }\n",

		progname,
		progname, progname, progname, progname,
		progname, progname, progname, progname);
	exit(1);
}

static char *subcmd;
#define eprintf(fmt, args...) fprintf(stderr, "%s: " fmt, subcmd , ## args)

#define RING_SIZE 1024
static unsigned RL[RING_SIZE];

/* plain-to-rl {{{2 */

/* also used in vli-to-rl */
void write_RL(int i)

{
	ssize_t s = i * sizeof(RL[0]);
	ssize_t c = write(out_fd, RL, s);
	if (c < 0) {
		eprintf("error writing runlength blob to stdout: %m\n");
		exit(6);
	}
	if (c != s) {
		eprintf("short write: %lu != %lu\n",
				(unsigned long)c, (unsigned long)s);
		exit(7);
	}
}

/* don't risk bit number wrap around.
* could be coded around, of course. */
#define MAX_BYTES ((off_t)((unsigned int)(~0) >> 3))

int plain_to_rl(int argc, char **argv)

{
	struct stat sb;
	unsigned long current_bit = 0;
	unsigned long n_bits;
	unsigned long tmp;
	unsigned long *bm;
	char *in_file;
	int toggle = 0;
	int i = 0;
	int fd;

	if (argc != 1) {
		eprintf("missing input file argument\n");
		return 1;
	}

	in_file = argv[0];
	fd = open(in_file, O_RDONLY);
	if (fd < 0) {
		eprintf("open('%s'): %m\n", in_file);
		return 2;
	}

	if (fstat(fd, &sb)) {
		eprintf("fstat(%s): %m\n", in_file);
		return 3;
	}

	if ((sb.st_mode & S_IFMT) != S_IFREG) {
		eprintf("%s: not a regular file\n", in_file);
		return 4;
	}

	if (sb.st_size > MAX_BYTES) {
		eprintf("%s too big, only scanning first %lu bytes\n",
				in_file, MAX_BYTES);
		sb.st_size = MAX_BYTES;
	}

	/* maybe TODO: allow start offset and size to be specified,
	 * possibly use mmap2 */
	bm = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0);
	if (bm == MAP_FAILED) {
		eprintf("mmap(%s): %m\n", in_file);
		return 5;
	}

	n_bits = sb.st_size << 3;
	for (;;) {
		toggle = !toggle;

		tmp = toggle ? find_next_bit(bm, n_bits, current_bit)
		             : find_next_zero_bit(bm, n_bits, current_bit);

		if (tmp >= n_bits)
			RL[i++] = n_bits - current_bit;
		else
			RL[i++] = tmp - current_bit;
		if (i == RING_SIZE || tmp >= n_bits) {
			write_RL(i);
			if (tmp >= n_bits)
				break;
			i = 0;
		}
		current_bit = tmp;
	}
	close(1);
	return 0;
}

/* plain-to-rl }}}1 */

/* rl-to-vli {{{2 */

/* drbd on-network packet is preceeded by an 8 byte header, btw.
* also, we transmit an incompressible chunk as plain, obviously.
* left out here for demonstation of the encoding properties only. */

/* also used from vli_to_rl */
/* CAUTION: do not increase OUTPUT_BUFF_SIZE without also changing
* the chunk.head format, see head_to_bytes() below.
* in a proper implementation, you would add some magic or checksum,
* plus a flag for interleaved plaintext (for incompressible chunks). */
#define OUTPUT_BUFF_SIZE 4096

static struct code_chunk {
	unsigned short head;

#define head_to_bytes(head) ((head & 0x0fff) +1)
#define head_to_pad_bits(head) ((head & 0x7000) >> 12)
#define head_is_first_bit_set(head) ((head & 0x8000) != 0)

	unsigned char code[OUTPUT_BUFF_SIZE];
} chunk;

ssize_t pipe_read(int fd, void *buf, ssize_t l)

{
	ssize_t c, count = 0;
	int loop = 0;
	do {
		c = read(in_fd, buf + count, l);
		if (c < 0) {
			eprintf("error reading from stdin: %m\n");
			exit(2);
		}
		if (c == 0 && ++loop > 3)
			break;
		count += c;
		l -= c;
	} while (l);
	return count;
}

int read_RL()

{
	int count = pipe_read(in_fd, RL, sizeof(RL));
	if (count % sizeof(RL[0])) {
		eprintf("short read, not modulo native unsingned int!\n"
				" %u %% %u == %u\n", count, (unsigned)sizeof(RL[0]),
				count % (unsigned)sizeof(RL[0]));
		exit(2);
	}
	return count / sizeof(RL[0]);
}

/* one could save (8*sizeof(head) + up to 7) bits every chunk, by just
* streaming the code bits one after the other.
* but then you have no easy way to detect truncated code during decode */
void write_code_chunk_rewind_bs(struct bitstream *bs, int first_bit_set)

{
	ssize_t c;
	ssize_t s = bs->cur.b - bs->buf + !!bs->cur.bit;

	chunk.head = first_bit_set ? 0x8000 : 0;
	/* pad bits */
	chunk.head |= (0x7 & (8 - bs->cur.bit)) << 12;
	/* code bytes */
	chunk.head |= 0x0fff & (s - 1);

	/* should not happen? */
	if (!s)
		return;

	s += sizeof(chunk.head);
	c = write(out_fd, &chunk, s);
	if (c < 0) {
		eprintf("error writing code blob to stdout: %m\n");
		exit(6);
	}
	if (c != s) {
		eprintf("short write: %lu != %lu\n",
				(unsigned long)c, (unsigned long)s);
		exit(7);
	}
	bitstream_rewind(bs);
}
static struct {
       unsigned n;
       unsigned plain;
       unsigned code;
} stats[2]; /* compressible, incompressible */

void do_stats(struct bitstream *bs, int n_chunks, unsigned long plain)

{
	unsigned long code = ((bs->cur.b - bs->buf) + !!bs->cur.bit + 2) * 8;
	/* eprintf("chunk:%u plain_bits:%lu code_bits:%u\n",
		n_chunks, plain, code); */
	++stats[code > plain].n;
	stats[code > plain].plain += plain;
	stats[code > plain].code += code;
}

void print_stat_summary(void)

{
	unsigned total_n = stats[0].n + stats[1].n;
	unsigned long total_plain_bits = stats[0].plain + stats[1].plain;
	unsigned long total_code_bits = stats[0].code + stats[1].code;

	eprintf("stats: %u chunks, %u compressed, %u uncompressed\n"
		"\tonly compressible: %u plain bits -> %u code bits\n",
		total_n, stats[0].n, stats[1].n,
		stats[0].plain, stats[0].code);
	eprintf("total saved: %.2f%%\n",
			100.0 * (total_plain_bits - total_code_bits)
			    / total_plain_bits);
}

int rl_to_vli(int argc, char **argv)

{
	struct bitstream bs;
	unsigned long plain_bits = 0;
	int l = read_RL();
	int first_is_set;
	int odd_is_set = 1;
	int i;
	int bits;
	int stats = NULL != getenv("RL_TO_VLI_STATS");
	int n_chunks = 0;

	if (l == 0) {
		eprintf("empty input!\n");
		return 3;
	}

	bitstream_init(&bs, chunk.code, OUTPUT_BUFF_SIZE, 0);
	i = !RL[0];
	first_is_set = i;
	do {
		while (i < l) {
			/* paranoia: catch zero runlength.
			 * can only happen if bitmap was modified while it was scanned. */
			if (RL[i] == 0) {
				eprintf("unexpected zero runlength i=%d\n", i);
				return 4;
			}
redo:
			bits = vli_encode_bits(&bs, RL[i]);
			if (bits == -ENOBUFS) { /* buffer full */
				if (stats) {
					do_stats(&bs, n_chunks++, plain_bits);
					plain_bits = 0;
				}
				write_code_chunk_rewind_bs(&bs, first_is_set);
				/* current will be first of next packet */
				first_is_set = (i & 1) == odd_is_set;
				goto redo;
			}
			if (bits <= 0) {
				eprintf("error while encoding runlength: %d\n", bits);
				return 5;
			}
			if (stats)
				plain_bits += RL[i];
			i++;
		}
		if (l & 1)
			odd_is_set = !odd_is_set;
		l = read_RL();
		i = 0;
	} while (l);
	if (bs.cur.b != bs.buf || bs.cur.bit)
		write_code_chunk_rewind_bs(&bs, first_is_set);
	if (stats)
		print_stat_summary();
	return 0;
}

/* rl-to-vli }}}1 */

/* vli-to-rl {{{2 */

ssize_t read_one_code_chunk(struct bitstream *bs)

{
	static unsigned long total_read_bytes;
	ssize_t len;
	int bytes;

	len = pipe_read(in_fd, &chunk.head, sizeof(chunk.head));
	if (len == 0)
		return 0;
	if (len != 2) {
		eprintf("short read reading chunk.head\n");
		exit(2);
	}
	total_read_bytes += len;
	bytes = head_to_bytes(chunk.head);
	len = pipe_read(in_fd, &chunk.code, bytes);
	if (len != bytes) {
		eprintf("short read reading chunk.code: %d %d (%lu)\n",
			(unsigned)len, bytes, total_read_bytes);
		exit(2);
	}
	total_read_bytes += len;
	bitstream_init(bs, chunk.code, bytes, head_to_pad_bits(chunk.head));
	return len + 2;
}

int vli_to_rl(int argc, char **argv)

{
	struct bitstream bs;
	__u64 look_ahead;
	__u64 tmp;
	__u64 rl;
	int toggle;
	int have;
	int bits;
	int first = 1;
	int i = 0;

	while (read_one_code_chunk(&bs)) {
		look_ahead = 0;
		tmp = 0;
		have = 0;
		toggle = !head_is_first_bit_set(chunk.head);
		if (first) {
			first = 0;
			if (!toggle)
				RL[i++] = 0;
		}
		for (;;) {
			toggle = !toggle;
			/* get fresh bits */
			bits = bitstream_get_bits(&bs, &tmp, 64 - have);
			if (bits < 0)
				return 3;
			look_ahead |= tmp << have;
			have += bits;
			if (have == 0)
				break;

			/* consume one code number */
			bits = vli_decode_bits(&rl, look_ahead);
			if (bits <= 0)
				return 4;
			/* cannot possibly decode more bits than I had */
			if (have < bits)
				return 5;
			look_ahead >>= bits;
			have -= bits;

			RL[i++] = rl;
			if (i == RING_SIZE) {
				write_RL(i);
				i = 0;
			}
		}
	}
	write_RL(i);
	return 0;
}

/* vli-to-rl }}}1 */

/* rl-to-plain {{{2 */

int rl_to_plain(int argc, char **argv)

{
	/* yep, this is excessive, and only used here to quick'n'dirty
	 * code something that is streamable. */
	static unsigned char zeros[4096];
	static unsigned char FFFFs[4096];

	unsigned char *out;
	unsigned int rl;
	int l;
	int i;
	int set = 0;
	int minibuf_bits = 0;
	unsigned char minibuf = 0;

	/* no need to initialize zeros, static did that for us */
	memset(FFFFs, 0xff, sizeof(FFFFs));

	while ((l = read_RL())) {
		for (i = 0; i < l; i++, set = !set) {
			rl = RL[i];
			if (minibuf_bits || rl < 8) {
				if (set) /* set bits */
					minibuf |= ((1 << (rl < 7 ? rl : 7)) -1)
						<< minibuf_bits;
				if (rl < 8 - minibuf_bits) {
					minibuf_bits += rl;
					continue;
				}
				if (write(out_fd, &minibuf, 1) != 1) {
					eprintf("FIXME 1\n");
					exit(2);
				}
				rl -= 8 - minibuf_bits;
				minibuf = 0;
				minibuf_bits = 0;
			}

			out = set ? FFFFs : zeros;
			while (rl > 8) {
				size_t c = rl/8; /* 8 bits per byte */
				if (sizeof(FFFFs) < c)
					c = sizeof(FFFFs);
				if (write(out_fd, out, c) != c) {
					eprintf("FIXME 2\n");
					exit(2);
				}
				rl -= c * 8;
			}
			if (rl) {
				if (set)
					minibuf = (1 << rl) -1;
				minibuf_bits = rl;
			}
		}
	}
	if (minibuf_bits)
		if (write(out_fd, &minibuf, 1) != 1) {
			eprintf("FIXME 3\n");
			exit(2);
		}

	return 0;
}

/* rl_to_plain }}}1 */

int main(int argc, char **argv)

{
	progname = strrchr(argv[0], '/');
	if (!progname)
		progname = argv[0];
	else
		progname++;

	if (argc < 2)
		show_usage_and_die();
	subcmd = argv[1];
	if (!strcmp(subcmd, "plain-to-rl"))
		return plain_to_rl(argc-2, argv+2);
	if (!strcmp(subcmd, "rl-to-vli"))
		return rl_to_vli(argc-2, argv+2);
	if (!strcmp(subcmd, "vli-to-rl"))
		return vli_to_rl(argc-2, argv+2);
	if (!strcmp(subcmd, "rl-to-plain"))
		return rl_to_plain(argc-2, argv+2);

	fprintf(stderr, "%s %s: unimplemented subcommand\n", progname, subcmd);
	return 1;
}