[functional-tests] roll all the C code into a single libft.so
This commit is contained in:
		@@ -1,898 +0,0 @@
 | 
			
		||||
#define _GNU_SOURCE
 | 
			
		||||
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <libaio.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <linux/fs.h>
 | 
			
		||||
#include <sys/ioctl.h>
 | 
			
		||||
 | 
			
		||||
#include "list.h"
 | 
			
		||||
#include "bcache.h"
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
static void warn(const char *msg)
 | 
			
		||||
{
 | 
			
		||||
	fprintf(stderr, "%s\n", msg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FIXME: raise a condition somehow?
 | 
			
		||||
static void raise(const char *msg)
 | 
			
		||||
{
 | 
			
		||||
	warn(msg);
 | 
			
		||||
	exit(1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Assumes the list is not empty.
 | 
			
		||||
 */
 | 
			
		||||
static inline struct list_head *list_pop(struct list_head *head)
 | 
			
		||||
{
 | 
			
		||||
	struct list_head *l;
 | 
			
		||||
 | 
			
		||||
	l = head->next;
 | 
			
		||||
	list_del(l);
 | 
			
		||||
	return l;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
struct control_block {
 | 
			
		||||
	struct list_head list;
 | 
			
		||||
	void *context;
 | 
			
		||||
	struct iocb cb;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct cb_set {
 | 
			
		||||
	struct list_head free;
 | 
			
		||||
	struct list_head allocated;
 | 
			
		||||
	struct control_block *vec;
 | 
			
		||||
} control_block_set;
 | 
			
		||||
 | 
			
		||||
static struct cb_set *cb_set_create(unsigned nr)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
	struct cb_set *cbs = malloc(sizeof(*cbs));
 | 
			
		||||
 | 
			
		||||
	if (!cbs)
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	cbs->vec = malloc(nr * sizeof(*cbs->vec));
 | 
			
		||||
	if (!cbs->vec) {
 | 
			
		||||
		free(cbs);
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	init_list_head(&cbs->free);
 | 
			
		||||
	init_list_head(&cbs->allocated);
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < nr; i++)
 | 
			
		||||
		list_add(&cbs->vec[i].list, &cbs->free);
 | 
			
		||||
 | 
			
		||||
	return cbs;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void cb_set_destroy(struct cb_set *cbs)
 | 
			
		||||
{
 | 
			
		||||
	if (!list_empty(&cbs->allocated))
 | 
			
		||||
		raise("async io still in flight");
 | 
			
		||||
 | 
			
		||||
	free(cbs->vec);
 | 
			
		||||
	free(cbs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct control_block *cb_alloc(struct cb_set *cbs, void *context)
 | 
			
		||||
{
 | 
			
		||||
	struct control_block *cb;
 | 
			
		||||
 | 
			
		||||
	if (list_empty(&cbs->free))
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	cb = container_of(list_pop(&cbs->free), struct control_block, list);
 | 
			
		||||
	cb->context = context;
 | 
			
		||||
	list_add(&cb->list, &cbs->allocated);
 | 
			
		||||
 | 
			
		||||
	return cb;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void cb_free(struct cb_set *cbs, struct control_block *cb)
 | 
			
		||||
{
 | 
			
		||||
	list_del(&cb->list);
 | 
			
		||||
	list_add(&cb->list, &cbs->free);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct control_block *iocb_to_cb(struct iocb *icb)
 | 
			
		||||
{
 | 
			
		||||
	return container_of(icb, struct control_block, cb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
// FIXME: get from linux headers
 | 
			
		||||
#define SECTOR_SHIFT 9
 | 
			
		||||
#define PAGE_SIZE 4096
 | 
			
		||||
 | 
			
		||||
enum dir {
 | 
			
		||||
	DIR_READ,
 | 
			
		||||
	DIR_WRITE
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct io_engine {
 | 
			
		||||
	io_context_t aio_context;
 | 
			
		||||
	struct cb_set *cbs;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct io_engine *engine_create(unsigned max_io)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct io_engine *e = malloc(sizeof(*e));
 | 
			
		||||
 | 
			
		||||
	if (!e)
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	e->aio_context = 0;
 | 
			
		||||
	r = io_setup(max_io, &e->aio_context);
 | 
			
		||||
	if (r < 0) {
 | 
			
		||||
		warn("io_setup failed");
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	e->cbs = cb_set_create(max_io);
 | 
			
		||||
	if (!e->cbs) {
 | 
			
		||||
		warn("couldn't create control block set");
 | 
			
		||||
		free(e);
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return e;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void engine_destroy(struct io_engine *e)
 | 
			
		||||
{
 | 
			
		||||
	cb_set_destroy(e->cbs);
 | 
			
		||||
	io_destroy(e->aio_context);
 | 
			
		||||
	free(e);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int engine_issue(struct io_engine *e, int fd, enum dir d,
 | 
			
		||||
			sector_t sb, sector_t se, void *data, void *context)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct iocb *cb_array[1];
 | 
			
		||||
	struct control_block *cb;
 | 
			
		||||
 | 
			
		||||
	if (((uint64_t) data) & (PAGE_SIZE - 1))
 | 
			
		||||
		return -1;
 | 
			
		||||
 | 
			
		||||
	cb = cb_alloc(e->cbs, context);
 | 
			
		||||
	if (!cb)
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	memset(&cb->cb, 0, sizeof(cb->cb));
 | 
			
		||||
 | 
			
		||||
	cb->cb.aio_fildes = (int) fd;
 | 
			
		||||
	cb->cb.u.c.buf = data;
 | 
			
		||||
	cb->cb.u.c.offset = sb << SECTOR_SHIFT;
 | 
			
		||||
	cb->cb.u.c.nbytes = (se - sb) << SECTOR_SHIFT;
 | 
			
		||||
	cb->cb.aio_lio_opcode = (d == DIR_READ) ? IO_CMD_PREAD : IO_CMD_PWRITE;
 | 
			
		||||
 | 
			
		||||
	cb_array[0] = &cb->cb;
 | 
			
		||||
	r = io_submit(e->aio_context, 1, cb_array);
 | 
			
		||||
	if (r < 0)
 | 
			
		||||
		cb_free(e->cbs, cb);
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define MAX_IO 64
 | 
			
		||||
typedef void complete_fn(void *context, int io_error);
 | 
			
		||||
 | 
			
		||||
static int engine_wait(struct io_engine *e, struct timespec *ts, complete_fn fn)
 | 
			
		||||
{
 | 
			
		||||
	int i, r;
 | 
			
		||||
	struct io_event event[MAX_IO];
 | 
			
		||||
	struct control_block *cb;
 | 
			
		||||
 | 
			
		||||
	memset(&event, 0, sizeof(event));
 | 
			
		||||
	r = io_getevents(e->aio_context, 1, MAX_IO, event, ts);
 | 
			
		||||
	if (r < 0) {
 | 
			
		||||
		warn("io_getevents failed");
 | 
			
		||||
		return r;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (r == 0)
 | 
			
		||||
		return 0;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < r; i++) {
 | 
			
		||||
		struct io_event *ev = event + i;
 | 
			
		||||
 | 
			
		||||
		cb = iocb_to_cb((struct iocb *) ev->obj);
 | 
			
		||||
 | 
			
		||||
		if (ev->res == cb->cb.u.c.nbytes)
 | 
			
		||||
			fn((void *) cb->context, 0);
 | 
			
		||||
 | 
			
		||||
		else if ((int) ev->res < 0)
 | 
			
		||||
			fn(cb->context, (int) ev->res);
 | 
			
		||||
 | 
			
		||||
		else {
 | 
			
		||||
			warn("short io");
 | 
			
		||||
			fn(cb->context, -ENODATA);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		cb_free(e->cbs, cb);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return -ENODATA;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
#if 0
 | 
			
		||||
struct timespec micro_to_ts(unsigned micro)
 | 
			
		||||
{
 | 
			
		||||
	struct timespec ts;
 | 
			
		||||
	ts.tv_sec = micro / 1000000u;
 | 
			
		||||
	ts.tv_nsec = (micro % 1000000) * 1000;
 | 
			
		||||
	return ts;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static unsigned ts_to_micro(struct timespec const *ts)
 | 
			
		||||
{
 | 
			
		||||
	unsigned micro = ts->tv_sec * 1000000;
 | 
			
		||||
	micro += ts->tv_nsec / 1000;
 | 
			
		||||
	return micro;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
#define MIN_BLOCKS 16
 | 
			
		||||
#define WRITEBACK_LOW_THRESHOLD_PERCENT 33
 | 
			
		||||
#define WRITEBACK_HIGH_THRESHOLD_PERCENT 66
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
static void *alloc_aligned(size_t len, size_t alignment)
 | 
			
		||||
{
 | 
			
		||||
	void *result = NULL;
 | 
			
		||||
	int r = posix_memalign(&result, alignment, len);
 | 
			
		||||
	if (r)
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
static bool test_flags(struct block *b, unsigned bits)
 | 
			
		||||
{
 | 
			
		||||
	return (b->flags & bits) != 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void set_flags(struct block *b, unsigned bits)
 | 
			
		||||
{
 | 
			
		||||
	b->flags |= bits;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void clear_flags(struct block *b, unsigned bits)
 | 
			
		||||
{
 | 
			
		||||
	b->flags &= ~bits;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
enum block_flags {
 | 
			
		||||
	BF_IO_PENDING = (1 << 0),
 | 
			
		||||
	BF_DIRTY = (1 << 1),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct bcache {
 | 
			
		||||
	int fd;
 | 
			
		||||
	sector_t block_sectors;
 | 
			
		||||
	uint64_t nr_data_blocks;
 | 
			
		||||
	uint64_t nr_cache_blocks;
 | 
			
		||||
 | 
			
		||||
	struct io_engine *engine;
 | 
			
		||||
 | 
			
		||||
	void *raw_data;
 | 
			
		||||
	struct block *raw_blocks;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Lists that categorise the blocks.
 | 
			
		||||
	 */
 | 
			
		||||
	unsigned nr_locked;
 | 
			
		||||
	unsigned nr_dirty;
 | 
			
		||||
	unsigned nr_io_pending;
 | 
			
		||||
 | 
			
		||||
	struct list_head free;
 | 
			
		||||
	struct list_head errored;
 | 
			
		||||
	struct list_head dirty;
 | 
			
		||||
	struct list_head clean;
 | 
			
		||||
	struct list_head io_pending;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Hash table.
 | 
			
		||||
	 */
 | 
			
		||||
	unsigned nr_buckets;
 | 
			
		||||
	unsigned hash_mask;
 | 
			
		||||
	struct list_head *buckets;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Statistics
 | 
			
		||||
	 */
 | 
			
		||||
	unsigned read_hits;
 | 
			
		||||
	unsigned read_misses;
 | 
			
		||||
	unsigned write_zeroes;
 | 
			
		||||
	unsigned write_hits;
 | 
			
		||||
	unsigned write_misses;
 | 
			
		||||
	unsigned prefetches;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
/*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
 | 
			
		||||
#define GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001UL
 | 
			
		||||
 | 
			
		||||
static unsigned hash(struct bcache *cache, uint64_t index)
 | 
			
		||||
{
 | 
			
		||||
	uint64_t h = index;
 | 
			
		||||
	h *= GOLDEN_RATIO_PRIME_64;
 | 
			
		||||
	return h & cache->hash_mask;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct block *hash_lookup(struct bcache *cache, uint64_t index)
 | 
			
		||||
{
 | 
			
		||||
	struct block *b;
 | 
			
		||||
	unsigned h = hash(cache, index);
 | 
			
		||||
 | 
			
		||||
	list_for_each_entry (b, cache->buckets + h, hash)
 | 
			
		||||
		if (b->index == index)
 | 
			
		||||
			return b;
 | 
			
		||||
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void hash_insert(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	unsigned h = hash(b->cache, b->index);
 | 
			
		||||
	list_add(&b->hash, b->cache->buckets + h);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void hash_remove(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	list_del(&b->hash);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Must return a power of 2.
 | 
			
		||||
 */
 | 
			
		||||
static unsigned calc_nr_buckets(unsigned nr_blocks)
 | 
			
		||||
{
 | 
			
		||||
	unsigned r = 8;
 | 
			
		||||
	unsigned n = nr_blocks / 4;
 | 
			
		||||
 | 
			
		||||
	if (n < 8)
 | 
			
		||||
		n = 8;
 | 
			
		||||
 | 
			
		||||
	while (r < n)
 | 
			
		||||
		r <<= 1;
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int hash_table_init(struct bcache *cache, unsigned nr_entries)
 | 
			
		||||
{
 | 
			
		||||
	unsigned i;
 | 
			
		||||
 | 
			
		||||
	cache->nr_buckets = calc_nr_buckets(nr_entries);
 | 
			
		||||
	cache->hash_mask = cache->nr_buckets - 1;
 | 
			
		||||
	cache->buckets = malloc(cache->nr_buckets * sizeof(*cache->buckets));
 | 
			
		||||
	if (!cache->buckets)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < cache->nr_buckets; i++)
 | 
			
		||||
		init_list_head(cache->buckets + i);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void hash_table_exit(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	free(cache->buckets);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
static int init_free_list(struct bcache *cache, unsigned count)
 | 
			
		||||
{
 | 
			
		||||
	unsigned i;
 | 
			
		||||
	size_t block_size = cache->block_sectors << SECTOR_SHIFT;
 | 
			
		||||
	unsigned char *data =
 | 
			
		||||
		(unsigned char *) alloc_aligned(count * block_size, PAGE_SIZE);
 | 
			
		||||
 | 
			
		||||
	/* Allocate the data for each block.  We page align the data. */
 | 
			
		||||
	if (!data)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	cache->raw_data = data;
 | 
			
		||||
	cache->raw_blocks = malloc(count * sizeof(*cache->raw_blocks));
 | 
			
		||||
 | 
			
		||||
	if (!cache->raw_blocks)
 | 
			
		||||
		free(cache->raw_data);
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < count; i++) {
 | 
			
		||||
		struct block *b = cache->raw_blocks + i;
 | 
			
		||||
		b->cache = cache;
 | 
			
		||||
		b->data = data + (block_size * i);
 | 
			
		||||
		list_add_tail(&b->list, &cache->free);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void exit_free_list(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	free(cache->raw_data);
 | 
			
		||||
	free(cache->raw_blocks);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct block *alloc_block(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	struct block *b = container_of(list_pop(&cache->free), struct block, list);
 | 
			
		||||
	return b;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------
 | 
			
		||||
 * Clean/dirty list management.
 | 
			
		||||
 * Always use these methods to ensure nr_dirty_ is correct.
 | 
			
		||||
 *--------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static void unlink_block(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	if (test_flags(b, BF_DIRTY))
 | 
			
		||||
		b->cache->nr_dirty--;
 | 
			
		||||
 | 
			
		||||
	list_del(&b->list);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void link_block(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	struct bcache *cache = b->cache;
 | 
			
		||||
 | 
			
		||||
	if (test_flags(b, BF_DIRTY)) {
 | 
			
		||||
		list_add_tail(&b->list, &cache->dirty);
 | 
			
		||||
		cache->nr_dirty++;
 | 
			
		||||
	} else
 | 
			
		||||
		list_add_tail(&b->list, &cache->clean);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void relink(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	unlink_block(b);
 | 
			
		||||
	link_block(b);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------
 | 
			
		||||
 * Low level IO handling
 | 
			
		||||
 *
 | 
			
		||||
 * We cannot have two concurrent writes on the same block.
 | 
			
		||||
 * eg, background writeback, put with dirty, flush?
 | 
			
		||||
 *
 | 
			
		||||
 * To avoid this we introduce some restrictions:
 | 
			
		||||
 *
 | 
			
		||||
 * i)  A held block can never be written back.
 | 
			
		||||
 * ii) You cannot get a block until writeback has completed.
 | 
			
		||||
 *
 | 
			
		||||
 *--------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * |b->list| should be valid (either pointing to itself, on one of the other
 | 
			
		||||
 * lists.
 | 
			
		||||
 */
 | 
			
		||||
static int issue_low_level(struct block *b, enum dir d)
 | 
			
		||||
{
 | 
			
		||||
	struct bcache *cache = b->cache;
 | 
			
		||||
	sector_t sb = b->index * cache->block_sectors;
 | 
			
		||||
	sector_t se = sb + cache->block_sectors;
 | 
			
		||||
	set_flags(b, BF_IO_PENDING);
 | 
			
		||||
 | 
			
		||||
	return engine_issue(cache->engine, cache->fd, d, sb, se, b->data, b);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void issue_read(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	assert(!test_flags(b, BF_IO_PENDING));
 | 
			
		||||
	issue_low_level(b, DIR_READ);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void issue_write(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	assert(!test_flags(b, BF_IO_PENDING));
 | 
			
		||||
	//b.v_->prepare(b.data_, b.index_);
 | 
			
		||||
	issue_low_level(b, DIR_WRITE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void complete_io(void *context, int err)
 | 
			
		||||
{
 | 
			
		||||
	struct block *b = context;
 | 
			
		||||
	struct bcache *cache = b->cache;
 | 
			
		||||
 | 
			
		||||
	b->error = err;
 | 
			
		||||
	clear_flags(b, BF_IO_PENDING);
 | 
			
		||||
	cache->nr_io_pending--;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * b is on the io_pending list, so we don't want to use unlink_block.
 | 
			
		||||
	 * Which would incorrectly adjust nr_dirty.
 | 
			
		||||
	 */
 | 
			
		||||
	list_del(&b->list);
 | 
			
		||||
 | 
			
		||||
	if (b->error)
 | 
			
		||||
		list_add_tail(&b->list, &cache->errored);
 | 
			
		||||
 | 
			
		||||
	else {
 | 
			
		||||
		clear_flags(b, BF_DIRTY);
 | 
			
		||||
		link_block(b);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int wait_io(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	return engine_wait(cache->engine, NULL, complete_io);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------
 | 
			
		||||
 * High level IO handling
 | 
			
		||||
 *--------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static void wait_all(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	while (!list_empty(&cache->io_pending))
 | 
			
		||||
		wait_io(cache);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void wait_specific(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	while (test_flags(b, BF_IO_PENDING))
 | 
			
		||||
		wait_io(b->cache);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static unsigned writeback(struct bcache *cache, unsigned count)
 | 
			
		||||
{
 | 
			
		||||
	unsigned actual = 0;
 | 
			
		||||
	struct block *b, *tmp;
 | 
			
		||||
 | 
			
		||||
	list_for_each_entry_safe (b, tmp, &cache->dirty, list) {
 | 
			
		||||
		if (actual == count)
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		// We can't writeback anything that's still in use.
 | 
			
		||||
		if (!b->ref_count) {
 | 
			
		||||
			issue_write(b);
 | 
			
		||||
			actual++;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return actual;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------
 | 
			
		||||
 * High level allocation
 | 
			
		||||
 *--------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
static struct block *find_unused_clean_block(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	struct block *b;
 | 
			
		||||
 | 
			
		||||
	list_for_each_entry (b, &cache->clean, list) {
 | 
			
		||||
		if (!b->ref_count) {
 | 
			
		||||
			unlink_block(b);
 | 
			
		||||
			hash_remove(b);
 | 
			
		||||
			return b;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct block *new_block(struct bcache *cache, block_address index)
 | 
			
		||||
{
 | 
			
		||||
	struct block *b;
 | 
			
		||||
 | 
			
		||||
	b = alloc_block(cache);
 | 
			
		||||
	while (!b && cache->nr_locked < cache->nr_cache_blocks) {
 | 
			
		||||
		b = find_unused_clean_block(cache);
 | 
			
		||||
		if (!b) {
 | 
			
		||||
			if (list_empty(&cache->io_pending))
 | 
			
		||||
				writeback(cache, 16);
 | 
			
		||||
			wait_io(cache);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (b) {
 | 
			
		||||
		init_list_head(&b->list);
 | 
			
		||||
		init_list_head(&b->hash);
 | 
			
		||||
		b->flags = 0;
 | 
			
		||||
		b->index = index;
 | 
			
		||||
		b->ref_count = 0;
 | 
			
		||||
		b->error = 0;
 | 
			
		||||
 | 
			
		||||
		hash_insert(b);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return b;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------
 | 
			
		||||
 * Block reference counting
 | 
			
		||||
 *--------------------------------------------------------------*/
 | 
			
		||||
struct bcache *bcache_create(int fd, sector_t block_sectors, uint64_t on_disk_blocks,
 | 
			
		||||
			     unsigned nr_cache_blocks)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct bcache *cache;
 | 
			
		||||
 | 
			
		||||
	cache = malloc(sizeof(*cache));
 | 
			
		||||
	if (!cache)
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	cache->fd = fd;
 | 
			
		||||
	cache->block_sectors = block_sectors;
 | 
			
		||||
	cache->nr_data_blocks = on_disk_blocks;
 | 
			
		||||
	cache->nr_cache_blocks = nr_cache_blocks;
 | 
			
		||||
 | 
			
		||||
	cache->engine = engine_create(nr_cache_blocks < 1024u ? nr_cache_blocks : 1024u);
 | 
			
		||||
	if (!cache->engine) {
 | 
			
		||||
		free(cache);
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cache->nr_locked = 0;
 | 
			
		||||
	cache->nr_dirty = 0;
 | 
			
		||||
	cache->nr_io_pending = 0;
 | 
			
		||||
 | 
			
		||||
	init_list_head(&cache->free);
 | 
			
		||||
	init_list_head(&cache->errored);
 | 
			
		||||
	init_list_head(&cache->dirty);
 | 
			
		||||
	init_list_head(&cache->clean);
 | 
			
		||||
	init_list_head(&cache->io_pending);
 | 
			
		||||
 | 
			
		||||
	if (hash_table_init(cache, nr_cache_blocks)) {
 | 
			
		||||
		engine_destroy(cache->engine);
 | 
			
		||||
		free(cache);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cache->read_hits = 0;
 | 
			
		||||
	cache->read_misses = 0;
 | 
			
		||||
	cache->write_zeroes = 0;
 | 
			
		||||
	cache->write_hits = 0;
 | 
			
		||||
	cache->write_misses = 0;
 | 
			
		||||
	cache->prefetches = 0;
 | 
			
		||||
 | 
			
		||||
	r = init_free_list(cache, nr_cache_blocks);
 | 
			
		||||
	if (r) {
 | 
			
		||||
		engine_destroy(cache->engine);
 | 
			
		||||
		hash_table_exit(cache);
 | 
			
		||||
		free(cache);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return cache;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define MD_BLOCK_SIZE 4096ull
 | 
			
		||||
 | 
			
		||||
struct bcache *bcache_simple(const char *path, unsigned nr_cache_blocks)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct stat info;
 | 
			
		||||
	struct bcache *cache;
 | 
			
		||||
	int fd = open(path, O_DIRECT | O_EXCL | O_RDONLY);
 | 
			
		||||
	uint64_t s;
 | 
			
		||||
 | 
			
		||||
	if (fd < 0) {
 | 
			
		||||
		raise("couldn't open cache file");
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r = fstat(fd, &info);
 | 
			
		||||
	if (r < 0) {
 | 
			
		||||
		raise("couldn't stat cache file");
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	s = info.st_size;
 | 
			
		||||
	cache = bcache_create(fd, MD_BLOCK_SIZE >> SECTOR_SHIFT,
 | 
			
		||||
			      s / MD_BLOCK_SIZE, nr_cache_blocks);
 | 
			
		||||
	if (!cache)
 | 
			
		||||
		close(fd);
 | 
			
		||||
 | 
			
		||||
	return cache;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void bcache_destroy(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	if (cache->nr_locked)
 | 
			
		||||
		warn("some blocks are still locked\n");
 | 
			
		||||
 | 
			
		||||
	flush_cache(cache);
 | 
			
		||||
	wait_all(cache);
 | 
			
		||||
	exit_free_list(cache);
 | 
			
		||||
	hash_table_exit(cache);
 | 
			
		||||
	engine_destroy(cache->engine);
 | 
			
		||||
	close(cache->fd);
 | 
			
		||||
	free(cache);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FIXME: we have to return an error code that can be turned into a Scheme
 | 
			
		||||
// condition.
 | 
			
		||||
static void check_index(struct bcache *cache, block_address index)
 | 
			
		||||
{
 | 
			
		||||
	if (index >= cache->nr_data_blocks)
 | 
			
		||||
		raise("block out of bounds");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint64_t get_nr_blocks(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	return cache->nr_data_blocks;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint64_t get_nr_locked(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	return cache->nr_locked;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void zero_block(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	b->cache->write_zeroes++;
 | 
			
		||||
	memset(b->data, 0, b->cache->block_sectors << SECTOR_SHIFT);
 | 
			
		||||
	set_flags(b, BF_DIRTY);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void hit(struct block *b, unsigned flags)
 | 
			
		||||
{
 | 
			
		||||
	struct bcache *cache = b->cache;
 | 
			
		||||
 | 
			
		||||
	if (flags & (GF_ZERO | GF_DIRTY))
 | 
			
		||||
		cache->write_hits++;
 | 
			
		||||
	else
 | 
			
		||||
		cache->read_hits++;
 | 
			
		||||
 | 
			
		||||
	relink(b);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void miss(struct bcache *cache, unsigned flags)
 | 
			
		||||
{
 | 
			
		||||
	if (flags & (GF_ZERO | GF_DIRTY))
 | 
			
		||||
		cache->write_misses++;
 | 
			
		||||
	else
 | 
			
		||||
		cache->read_misses++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct block *lookup_or_read_block(struct bcache *cache,
 | 
			
		||||
				  	  block_address index, unsigned flags)
 | 
			
		||||
{
 | 
			
		||||
	struct block *b = hash_lookup(cache, index);
 | 
			
		||||
 | 
			
		||||
	if (b) {
 | 
			
		||||
		// FIXME: this is insufficient.  We need to also catch a read
 | 
			
		||||
		// lock of a write locked block.  Ref count needs to distinguish.
 | 
			
		||||
		if (b->ref_count && (flags & (GF_DIRTY | GF_ZERO)))
 | 
			
		||||
			raise("concurrent write lock attempt");
 | 
			
		||||
 | 
			
		||||
		if (test_flags(b, BF_IO_PENDING)) {
 | 
			
		||||
			miss(cache, flags);
 | 
			
		||||
			wait_specific(b);
 | 
			
		||||
 | 
			
		||||
		} else
 | 
			
		||||
			hit(b, flags);
 | 
			
		||||
 | 
			
		||||
		unlink_block(b);
 | 
			
		||||
 | 
			
		||||
		if (flags & GF_ZERO)
 | 
			
		||||
			zero_block(b);
 | 
			
		||||
 | 
			
		||||
	} else {
 | 
			
		||||
		miss(cache, flags);
 | 
			
		||||
 | 
			
		||||
		b = new_block(cache, index);
 | 
			
		||||
		if (b) {
 | 
			
		||||
			if (flags & GF_ZERO)
 | 
			
		||||
				zero_block(b);
 | 
			
		||||
 | 
			
		||||
			else {
 | 
			
		||||
				issue_read(b);
 | 
			
		||||
				wait_specific(b);
 | 
			
		||||
 | 
			
		||||
				// we know the block is clean and unerrored.
 | 
			
		||||
				unlink_block(b);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (b && !b->error) {
 | 
			
		||||
		if (flags & (GF_DIRTY | GF_ZERO))
 | 
			
		||||
			set_flags(b, BF_DIRTY);
 | 
			
		||||
 | 
			
		||||
		link_block(b);
 | 
			
		||||
		return b;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct block *get_block(struct bcache *cache, block_address index, unsigned flags)
 | 
			
		||||
{
 | 
			
		||||
	check_index(cache, index);
 | 
			
		||||
 | 
			
		||||
	struct block *b = lookup_or_read_block(cache, index, flags);
 | 
			
		||||
	if (b) {
 | 
			
		||||
		if (!b->ref_count)
 | 
			
		||||
			cache->nr_locked++;
 | 
			
		||||
		b->ref_count++;
 | 
			
		||||
 | 
			
		||||
		return b;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	raise("couldn't get block");
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void preemptive_writeback(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	// FIXME: this ignores those blocks that are in the error state.  Track
 | 
			
		||||
	// nr_clean instead?
 | 
			
		||||
	unsigned nr_available = cache->nr_cache_blocks - (cache->nr_dirty - cache->nr_io_pending);
 | 
			
		||||
	if (nr_available < (WRITEBACK_LOW_THRESHOLD_PERCENT * cache->nr_cache_blocks / 100))
 | 
			
		||||
		writeback(cache, (WRITEBACK_HIGH_THRESHOLD_PERCENT * cache->nr_cache_blocks / 100) - nr_available);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void release_block(struct block *b)
 | 
			
		||||
{
 | 
			
		||||
	assert(b->ref_count);
 | 
			
		||||
 | 
			
		||||
	b->ref_count--;
 | 
			
		||||
	if (!b->ref_count)
 | 
			
		||||
		b->cache->nr_locked--;
 | 
			
		||||
 | 
			
		||||
	if (test_flags(b, BF_DIRTY))
 | 
			
		||||
		preemptive_writeback(b->cache);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int flush_cache(struct bcache *cache)
 | 
			
		||||
{
 | 
			
		||||
	while (!list_empty(&cache->dirty)) {
 | 
			
		||||
		struct block *b = container_of(list_pop(&cache->dirty), struct block, list);
 | 
			
		||||
		if (b->ref_count || test_flags(b, BF_IO_PENDING))
 | 
			
		||||
			// The superblock may well be still locked.
 | 
			
		||||
			continue;
 | 
			
		||||
 | 
			
		||||
		issue_write(b);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wait_all(cache);
 | 
			
		||||
 | 
			
		||||
	return list_empty(&cache->errored) ? 0 : -EIO;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void prefetch_block(struct bcache *cache, block_address index)
 | 
			
		||||
{
 | 
			
		||||
	check_index(cache, index);
 | 
			
		||||
	struct block *b = hash_lookup(cache, index);
 | 
			
		||||
 | 
			
		||||
	if (!b) {
 | 
			
		||||
		cache->prefetches++;
 | 
			
		||||
 | 
			
		||||
		b = new_block(cache, index);
 | 
			
		||||
		if (b)
 | 
			
		||||
			issue_read(b);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
@@ -1,69 +0,0 @@
 | 
			
		||||
#ifndef BCACHE_H
 | 
			
		||||
#define BCACHE_H
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
typedef uint64_t block_address;
 | 
			
		||||
typedef uint64_t sector_t;
 | 
			
		||||
 | 
			
		||||
struct block;
 | 
			
		||||
struct bcache;
 | 
			
		||||
 | 
			
		||||
// FIXME: allow the cache to be opened read only.
 | 
			
		||||
struct bcache *bcache_create(int fd, sector_t block_size,
 | 
			
		||||
			     uint64_t on_disk_blocks,
 | 
			
		||||
			     unsigned nr_cache_blocks);
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * A simpler way of creating a bcache that assumes 4k block size, and stats to
 | 
			
		||||
 * get the file size.
 | 
			
		||||
 */
 | 
			
		||||
struct bcache *bcache_simple(const char *path, unsigned nr_cache_blocks);
 | 
			
		||||
 | 
			
		||||
void bcache_destroy(struct bcache *cache);
 | 
			
		||||
uint64_t get_nr_blocks(struct bcache *cache);
 | 
			
		||||
uint64_t get_nr_locked(struct bcache *cache);
 | 
			
		||||
 | 
			
		||||
int flush_cache(struct bcache *cache);
 | 
			
		||||
 | 
			
		||||
struct bcache;
 | 
			
		||||
 | 
			
		||||
struct block {
 | 
			
		||||
	/* clients may only access these two fields */
 | 
			
		||||
	void *data;
 | 
			
		||||
	uint64_t index;
 | 
			
		||||
 | 
			
		||||
	struct bcache *cache;
 | 
			
		||||
	struct list_head list;
 | 
			
		||||
	struct list_head hash;
 | 
			
		||||
 | 
			
		||||
	unsigned flags;
 | 
			
		||||
	unsigned ref_count;
 | 
			
		||||
	int error;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum get_flags {
 | 
			
		||||
	/*
 | 
			
		||||
	 * The block will be zeroed before get_block returns it.  This
 | 
			
		||||
	 * potentially avoids a read if the block is not already in the cache.
 | 
			
		||||
	 * GF_DIRTY is implicit.
 | 
			
		||||
	 */
 | 
			
		||||
	GF_ZERO = (1 << 0),
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Indicates the caller is intending to change the data in the block, a
 | 
			
		||||
	 * writeback will occur after the block is released.
 | 
			
		||||
	 */
 | 
			
		||||
	GF_DIRTY = (1 << 1),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct block *get_block(struct bcache *cache, block_address index, unsigned flags);
 | 
			
		||||
void prefetch_block(struct bcache *cache, block_address index);
 | 
			
		||||
 | 
			
		||||
void release_block(struct block *b);
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@@ -26,7 +26,7 @@
 | 
			
		||||
          (srfi s8 receive)
 | 
			
		||||
          (utils))
 | 
			
		||||
 | 
			
		||||
  (define __ (load-shared-object "./bcache/bcache.so"))
 | 
			
		||||
  (define __ (load-shared-object "../lib/libft.so"))
 | 
			
		||||
 | 
			
		||||
  (define bcache-simple
 | 
			
		||||
    (foreign-procedure "bcache_simple" (string unsigned) ptr))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,216 +0,0 @@
 | 
			
		||||
#ifndef LIB_BLOCK_CACHE_LIST_H
 | 
			
		||||
#define LIB_BLOCK_CACHE_LIST_H
 | 
			
		||||
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Simple intrusive linked list code.  Lifted from Linux kernel.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * container_of - cast a member of a structure out to the containing structure
 | 
			
		||||
 * @ptr:	the pointer to the member.
 | 
			
		||||
 * @type:	the type of the container struct this is embedded in.
 | 
			
		||||
 * @member:	the name of the member within the struct.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
#define container_of(ptr, type, member) ({			\
 | 
			
		||||
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
 | 
			
		||||
	(type *)( (char *)__mptr - offsetof(type,member) );})
 | 
			
		||||
 | 
			
		||||
struct list_head {
 | 
			
		||||
	struct list_head *next, *prev;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static inline void init_list_head(struct list_head *list)
 | 
			
		||||
{
 | 
			
		||||
	list->next = list;
 | 
			
		||||
	list->prev = list;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline void __list_add(struct list_head *new_,
 | 
			
		||||
			      struct list_head *prev,
 | 
			
		||||
			      struct list_head *next)
 | 
			
		||||
{
 | 
			
		||||
	next->prev = new_;
 | 
			
		||||
	new_->next = next;
 | 
			
		||||
	new_->prev = prev;
 | 
			
		||||
	prev->next = new_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_add - add a new entry
 | 
			
		||||
 * @new_: new entry to be added
 | 
			
		||||
 * @head: list head to add it after
 | 
			
		||||
 *
 | 
			
		||||
 * Insert a new entry after the specified head.
 | 
			
		||||
 * This is good for implementing stacks.
 | 
			
		||||
 */
 | 
			
		||||
static inline void list_add(struct list_head *new_, struct list_head *head)
 | 
			
		||||
{
 | 
			
		||||
	__list_add(new_, head, head->next);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_add_tail - add a new entry
 | 
			
		||||
 * @new_: new entry to be added
 | 
			
		||||
 * @head: list head to add it before
 | 
			
		||||
 *
 | 
			
		||||
 * Insert a new entry before the specified head.
 | 
			
		||||
 * This is useful for implementing queues.
 | 
			
		||||
 */
 | 
			
		||||
static inline void list_add_tail(struct list_head *new_, struct list_head *head)
 | 
			
		||||
{
 | 
			
		||||
	__list_add(new_, head->prev, head);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Delete a list entry by making the prev/next entries
 | 
			
		||||
 * point to each other.
 | 
			
		||||
 *
 | 
			
		||||
 * This is only for internal list manipulation where we know
 | 
			
		||||
 * the prev/next entries already!
 | 
			
		||||
 */
 | 
			
		||||
static inline void __list_del(struct list_head * prev, struct list_head * next)
 | 
			
		||||
{
 | 
			
		||||
	next->prev = prev;
 | 
			
		||||
	prev->next = next;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_del - deletes entry from list.
 | 
			
		||||
 * @entry: the element to delete from the list.
 | 
			
		||||
 * Note: list_empty() on entry does not return true after this, the entry is
 | 
			
		||||
 * in an undefined state.
 | 
			
		||||
 */
 | 
			
		||||
static inline void __list_del_entry(struct list_head *entry)
 | 
			
		||||
{
 | 
			
		||||
	__list_del(entry->prev, entry->next);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline void list_del(struct list_head *entry)
 | 
			
		||||
{
 | 
			
		||||
	__list_del(entry->prev, entry->next);
 | 
			
		||||
	entry->next = NULL;
 | 
			
		||||
	entry->prev = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_del_init - deletes entry from list and reinitialize it.
 | 
			
		||||
 * @entry: the element to delete from the list.
 | 
			
		||||
 */
 | 
			
		||||
static inline void list_del_init(struct list_head *entry)
 | 
			
		||||
{
 | 
			
		||||
        __list_del_entry(entry);
 | 
			
		||||
        init_list_head(entry);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_move - delete from one list and add as another's head
 | 
			
		||||
 * @list: the entry to move
 | 
			
		||||
 * @head: the head that will precede our entry
 | 
			
		||||
 */
 | 
			
		||||
static inline void list_move(struct list_head *list, struct list_head *head)
 | 
			
		||||
{
 | 
			
		||||
	__list_del_entry(list);
 | 
			
		||||
	list_add(list, head);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_move_tail - delete from one list and add as another's tail
 | 
			
		||||
 * @list: the entry to move
 | 
			
		||||
 * @head: the head that will follow our entry
 | 
			
		||||
 */
 | 
			
		||||
static inline void list_move_tail(struct list_head *list,
 | 
			
		||||
				  struct list_head *head)
 | 
			
		||||
{
 | 
			
		||||
	__list_del_entry(list);
 | 
			
		||||
	list_add_tail(list, head);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_empty - tests whether a list is empty
 | 
			
		||||
 * @head: the list to test.
 | 
			
		||||
 */
 | 
			
		||||
static inline int list_empty(const struct list_head *head)
 | 
			
		||||
{
 | 
			
		||||
	return head->next == head;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_entry - get the struct for this entry
 | 
			
		||||
 * @ptr:	the &struct list_head pointer.
 | 
			
		||||
 * @type:	the type of the struct this is embedded in.
 | 
			
		||||
 * @member:	the name of the list_struct within the struct.
 | 
			
		||||
 */
 | 
			
		||||
#define list_entry(ptr, type, member) \
 | 
			
		||||
	container_of(ptr, type, member)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_first_entry - get the first element from a list
 | 
			
		||||
 * @ptr:	the list head to take the element from.
 | 
			
		||||
 * @type:	the type of the struct this is embedded in.
 | 
			
		||||
 * @member:	the name of the list_struct within the struct.
 | 
			
		||||
 *
 | 
			
		||||
 * Note, that list is expected to be not empty.
 | 
			
		||||
 */
 | 
			
		||||
#define list_first_entry(ptr, type, member) \
 | 
			
		||||
	list_entry((ptr)->next, type, member)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_next_entry - get the next element in list
 | 
			
		||||
 * @pos:	the type * to cursor
 | 
			
		||||
 * @member:	the name of the list_struct within the struct.
 | 
			
		||||
 */
 | 
			
		||||
#define list_next_entry(pos, member) \
 | 
			
		||||
	list_entry((pos)->member.next, typeof(*(pos)), member)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_for_each	-	iterate over a list
 | 
			
		||||
 * @pos:	the &struct list_head to use as a loop cursor.
 | 
			
		||||
 * @head:	the head for your list.
 | 
			
		||||
 */
 | 
			
		||||
#define list_for_each(pos, head) \
 | 
			
		||||
	for (pos = (head)->next; pos != (head); pos = pos->next)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_for_each_safe - iterate over a list safe against removal of list entry
 | 
			
		||||
 * @pos:	the &struct list_head to use as a loop cursor.
 | 
			
		||||
 * @n:		another &struct list_head to use as temporary storage
 | 
			
		||||
 * @head:	the head for your list.
 | 
			
		||||
 */
 | 
			
		||||
#define list_for_each_safe(pos, n, head) \
 | 
			
		||||
	for (pos = (head)->next, n = pos->next; pos != (head); \
 | 
			
		||||
		pos = n, n = pos->next)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_for_each_entry	-	iterate over list of given type
 | 
			
		||||
 * @pos:	the type * to use as a loop cursor.
 | 
			
		||||
 * @head:	the head for your list.
 | 
			
		||||
 * @member:	the name of the list_struct within the struct.
 | 
			
		||||
 */
 | 
			
		||||
#define list_for_each_entry(pos, head, member)				\
 | 
			
		||||
	for (pos = list_first_entry(head, typeof(*pos), member);	\
 | 
			
		||||
	     &pos->member != (head);					\
 | 
			
		||||
	     pos = list_next_entry(pos, member))
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 | 
			
		||||
 * @pos:	the type * to use as a loop cursor.
 | 
			
		||||
 * @n:		another type * to use as temporary storage
 | 
			
		||||
 * @head:	the head for your list.
 | 
			
		||||
 * @member:	the name of the list_struct within the struct.
 | 
			
		||||
 */
 | 
			
		||||
#define list_for_each_entry_safe(pos, n, head, member)			\
 | 
			
		||||
	for (pos = list_first_entry(head, typeof(*pos), member),	\
 | 
			
		||||
		n = list_next_entry(pos, member);			\
 | 
			
		||||
	     &pos->member != (head); 					\
 | 
			
		||||
	     pos = n, n = list_next_entry(n, member))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*----------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@@ -5,7 +5,7 @@
 | 
			
		||||
 | 
			
		||||
  (import (chezscheme))
 | 
			
		||||
 | 
			
		||||
  (define __ (load-shared-object "./crc32c/crc32c.so"))
 | 
			
		||||
  (define __ (load-shared-object "../lib/libft.so"))
 | 
			
		||||
 | 
			
		||||
  (define crc32c
 | 
			
		||||
          (foreign-procedure "crc32c" (unsigned-32 (* unsigned-8) unsigned) unsigned-32)))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,79 +0,0 @@
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
static const uint32_t crc32Table[256] = {
 | 
			
		||||
	0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L,
 | 
			
		||||
	0xC79A971FL, 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL,
 | 
			
		||||
	0x8AD958CFL, 0x78B2DBCCL, 0x6BE22838L, 0x9989AB3BL,
 | 
			
		||||
	0x4D43CFD0L, 0xBF284CD3L, 0xAC78BF27L, 0x5E133C24L,
 | 
			
		||||
	0x105EC76FL, 0xE235446CL, 0xF165B798L, 0x030E349BL,
 | 
			
		||||
	0xD7C45070L, 0x25AFD373L, 0x36FF2087L, 0xC494A384L,
 | 
			
		||||
	0x9A879FA0L, 0x68EC1CA3L, 0x7BBCEF57L, 0x89D76C54L,
 | 
			
		||||
	0x5D1D08BFL, 0xAF768BBCL, 0xBC267848L, 0x4E4DFB4BL,
 | 
			
		||||
	0x20BD8EDEL, 0xD2D60DDDL, 0xC186FE29L, 0x33ED7D2AL,
 | 
			
		||||
	0xE72719C1L, 0x154C9AC2L, 0x061C6936L, 0xF477EA35L,
 | 
			
		||||
	0xAA64D611L, 0x580F5512L, 0x4B5FA6E6L, 0xB93425E5L,
 | 
			
		||||
	0x6DFE410EL, 0x9F95C20DL, 0x8CC531F9L, 0x7EAEB2FAL,
 | 
			
		||||
	0x30E349B1L, 0xC288CAB2L, 0xD1D83946L, 0x23B3BA45L,
 | 
			
		||||
	0xF779DEAEL, 0x05125DADL, 0x1642AE59L, 0xE4292D5AL,
 | 
			
		||||
	0xBA3A117EL, 0x4851927DL, 0x5B016189L, 0xA96AE28AL,
 | 
			
		||||
	0x7DA08661L, 0x8FCB0562L, 0x9C9BF696L, 0x6EF07595L,
 | 
			
		||||
	0x417B1DBCL, 0xB3109EBFL, 0xA0406D4BL, 0x522BEE48L,
 | 
			
		||||
	0x86E18AA3L, 0x748A09A0L, 0x67DAFA54L, 0x95B17957L,
 | 
			
		||||
	0xCBA24573L, 0x39C9C670L, 0x2A993584L, 0xD8F2B687L,
 | 
			
		||||
	0x0C38D26CL, 0xFE53516FL, 0xED03A29BL, 0x1F682198L,
 | 
			
		||||
	0x5125DAD3L, 0xA34E59D0L, 0xB01EAA24L, 0x42752927L,
 | 
			
		||||
	0x96BF4DCCL, 0x64D4CECFL, 0x77843D3BL, 0x85EFBE38L,
 | 
			
		||||
	0xDBFC821CL, 0x2997011FL, 0x3AC7F2EBL, 0xC8AC71E8L,
 | 
			
		||||
	0x1C661503L, 0xEE0D9600L, 0xFD5D65F4L, 0x0F36E6F7L,
 | 
			
		||||
	0x61C69362L, 0x93AD1061L, 0x80FDE395L, 0x72966096L,
 | 
			
		||||
	0xA65C047DL, 0x5437877EL, 0x4767748AL, 0xB50CF789L,
 | 
			
		||||
	0xEB1FCBADL, 0x197448AEL, 0x0A24BB5AL, 0xF84F3859L,
 | 
			
		||||
	0x2C855CB2L, 0xDEEEDFB1L, 0xCDBE2C45L, 0x3FD5AF46L,
 | 
			
		||||
	0x7198540DL, 0x83F3D70EL, 0x90A324FAL, 0x62C8A7F9L,
 | 
			
		||||
	0xB602C312L, 0x44694011L, 0x5739B3E5L, 0xA55230E6L,
 | 
			
		||||
	0xFB410CC2L, 0x092A8FC1L, 0x1A7A7C35L, 0xE811FF36L,
 | 
			
		||||
	0x3CDB9BDDL, 0xCEB018DEL, 0xDDE0EB2AL, 0x2F8B6829L,
 | 
			
		||||
	0x82F63B78L, 0x709DB87BL, 0x63CD4B8FL, 0x91A6C88CL,
 | 
			
		||||
	0x456CAC67L, 0xB7072F64L, 0xA457DC90L, 0x563C5F93L,
 | 
			
		||||
	0x082F63B7L, 0xFA44E0B4L, 0xE9141340L, 0x1B7F9043L,
 | 
			
		||||
	0xCFB5F4A8L, 0x3DDE77ABL, 0x2E8E845FL, 0xDCE5075CL,
 | 
			
		||||
	0x92A8FC17L, 0x60C37F14L, 0x73938CE0L, 0x81F80FE3L,
 | 
			
		||||
	0x55326B08L, 0xA759E80BL, 0xB4091BFFL, 0x466298FCL,
 | 
			
		||||
	0x1871A4D8L, 0xEA1A27DBL, 0xF94AD42FL, 0x0B21572CL,
 | 
			
		||||
	0xDFEB33C7L, 0x2D80B0C4L, 0x3ED04330L, 0xCCBBC033L,
 | 
			
		||||
	0xA24BB5A6L, 0x502036A5L, 0x4370C551L, 0xB11B4652L,
 | 
			
		||||
	0x65D122B9L, 0x97BAA1BAL, 0x84EA524EL, 0x7681D14DL,
 | 
			
		||||
	0x2892ED69L, 0xDAF96E6AL, 0xC9A99D9EL, 0x3BC21E9DL,
 | 
			
		||||
	0xEF087A76L, 0x1D63F975L, 0x0E330A81L, 0xFC588982L,
 | 
			
		||||
	0xB21572C9L, 0x407EF1CAL, 0x532E023EL, 0xA145813DL,
 | 
			
		||||
	0x758FE5D6L, 0x87E466D5L, 0x94B49521L, 0x66DF1622L,
 | 
			
		||||
	0x38CC2A06L, 0xCAA7A905L, 0xD9F75AF1L, 0x2B9CD9F2L,
 | 
			
		||||
	0xFF56BD19L, 0x0D3D3E1AL, 0x1E6DCDEEL, 0xEC064EEDL,
 | 
			
		||||
	0xC38D26C4L, 0x31E6A5C7L, 0x22B65633L, 0xD0DDD530L,
 | 
			
		||||
	0x0417B1DBL, 0xF67C32D8L, 0xE52CC12CL, 0x1747422FL,
 | 
			
		||||
	0x49547E0BL, 0xBB3FFD08L, 0xA86F0EFCL, 0x5A048DFFL,
 | 
			
		||||
	0x8ECEE914L, 0x7CA56A17L, 0x6FF599E3L, 0x9D9E1AE0L,
 | 
			
		||||
	0xD3D3E1ABL, 0x21B862A8L, 0x32E8915CL, 0xC083125FL,
 | 
			
		||||
	0x144976B4L, 0xE622F5B7L, 0xF5720643L, 0x07198540L,
 | 
			
		||||
	0x590AB964L, 0xAB613A67L, 0xB831C993L, 0x4A5A4A90L,
 | 
			
		||||
	0x9E902E7BL, 0x6CFBAD78L, 0x7FAB5E8CL, 0x8DC0DD8FL,
 | 
			
		||||
	0xE330A81AL, 0x115B2B19L, 0x020BD8EDL, 0xF0605BEEL,
 | 
			
		||||
	0x24AA3F05L, 0xD6C1BC06L, 0xC5914FF2L, 0x37FACCF1L,
 | 
			
		||||
	0x69E9F0D5L, 0x9B8273D6L, 0x88D28022L, 0x7AB90321L,
 | 
			
		||||
	0xAE7367CAL, 0x5C18E4C9L, 0x4F48173DL, 0xBD23943EL,
 | 
			
		||||
	0xF36E6F75L, 0x0105EC76L, 0x12551F82L, 0xE03E9C81L,
 | 
			
		||||
	0x34F4F86AL, 0xC69F7B69L, 0xD5CF889DL, 0x27A40B9EL,
 | 
			
		||||
	0x79B737BAL, 0x8BDCB4B9L, 0x988C474DL, 0x6AE7C44EL,
 | 
			
		||||
	0xBE2DA0A5L, 0x4C4623A6L, 0x5F16D052L, 0xAD7D5351L
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
uint32_t crc32c(uint32_t crc, const void *buf, unsigned size)
 | 
			
		||||
{
 | 
			
		||||
	const uint8_t *p = buf;
 | 
			
		||||
 | 
			
		||||
	while (size--)
 | 
			
		||||
		crc = crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
 | 
			
		||||
 | 
			
		||||
	return crc;
 | 
			
		||||
}
 | 
			
		||||
@@ -1,574 +0,0 @@
 | 
			
		||||
#include <linux/dm-ioctl.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <sys/ioctl.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
// assuming div is a power of 2
 | 
			
		||||
static size_t round_up(size_t n, size_t div)
 | 
			
		||||
{
 | 
			
		||||
	size_t mask = div - 1;
 | 
			
		||||
	return (n + mask) & ~mask;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
static void *zalloc(size_t len)
 | 
			
		||||
{
 | 
			
		||||
	void *ptr = malloc(len);
 | 
			
		||||
	if (ptr)
 | 
			
		||||
		memset(ptr, 0, len);
 | 
			
		||||
 | 
			
		||||
	return ptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *payload(struct dm_ioctl *ctl)
 | 
			
		||||
{
 | 
			
		||||
	return ((unsigned char *) ctl) + ctl->data_start;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct dm_ioctl *alloc_ctl(size_t payload_size)
 | 
			
		||||
{
 | 
			
		||||
	size_t len = sizeof(struct dm_ioctl) + payload_size;
 | 
			
		||||
	struct dm_ioctl *ctl = zalloc(len);
 | 
			
		||||
 | 
			
		||||
	if (ctl) {
 | 
			
		||||
		ctl->version[0] = DM_VERSION_MAJOR;
 | 
			
		||||
		ctl->version[1] = DM_VERSION_MINOR;
 | 
			
		||||
		ctl->version[2] = DM_VERSION_PATCHLEVEL;
 | 
			
		||||
		ctl->data_size = len;
 | 
			
		||||
		ctl->data_start = sizeof(*ctl);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ctl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void free_ctl(struct dm_ioctl *ctl)
 | 
			
		||||
{
 | 
			
		||||
	free(ctl);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// realloc only copies the dm_ioctl struct, not the payload.
 | 
			
		||||
// old is always freed, even in case of error.
 | 
			
		||||
static struct dm_ioctl *realloc_ctl(struct dm_ioctl *old, size_t extra_payload)
 | 
			
		||||
{
 | 
			
		||||
	struct dm_ioctl *ctl;
 | 
			
		||||
	size_t old_payload_size = old->data_size - sizeof(struct dm_ioctl);
 | 
			
		||||
	size_t new_payload_size = old_payload_size + extra_payload;
 | 
			
		||||
 | 
			
		||||
	ctl = alloc_ctl(new_payload_size);
 | 
			
		||||
	if (ctl)
 | 
			
		||||
		memcpy(payload(ctl), payload(old), sizeof(*ctl));
 | 
			
		||||
 | 
			
		||||
	free_ctl(old);
 | 
			
		||||
	return ctl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
struct dm_interface {
 | 
			
		||||
	int fd;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// FIXME: pass in some form of log object?
 | 
			
		||||
struct dm_interface *dm_open()
 | 
			
		||||
{
 | 
			
		||||
	int fd;
 | 
			
		||||
	char path[1024];
 | 
			
		||||
	struct dm_interface *dmi;
 | 
			
		||||
 | 
			
		||||
	snprintf(path, sizeof(path), "/dev/%s/%s", DM_DIR, DM_CONTROL_NODE);
 | 
			
		||||
	fd = open(path, O_RDWR | O_EXCL);
 | 
			
		||||
	if (fd < 0)
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	dmi = zalloc(sizeof(*dmi));
 | 
			
		||||
	if (dmi)
 | 
			
		||||
		dmi->fd = fd;
 | 
			
		||||
	else
 | 
			
		||||
		close(fd);
 | 
			
		||||
 | 
			
		||||
	return dmi;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void dm_close(struct dm_interface *dmi)
 | 
			
		||||
{
 | 
			
		||||
	close(dmi->fd);
 | 
			
		||||
	free(dmi);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
struct dev_list {
 | 
			
		||||
	struct dev_list *next;
 | 
			
		||||
	unsigned major;
 | 
			
		||||
	unsigned minor;
 | 
			
		||||
	char *name;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void free_dev_list(struct dev_list *dl)
 | 
			
		||||
{
 | 
			
		||||
	struct dev_list *next;
 | 
			
		||||
 | 
			
		||||
	while (dl) {
 | 
			
		||||
		next = dl->next;
 | 
			
		||||
		free(dl->name);
 | 
			
		||||
		free(dl);
 | 
			
		||||
		dl = next;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct dev_list_builder {
 | 
			
		||||
	struct dev_list *head, *tail;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void dlb_init(struct dev_list_builder *dlb)
 | 
			
		||||
{
 | 
			
		||||
	dlb->head = dlb->tail = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dlb_append(struct dev_list_builder *dlb,
 | 
			
		||||
		      unsigned major, unsigned minor, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	struct dev_list *dl = malloc(sizeof(*dl));
 | 
			
		||||
 | 
			
		||||
	if (!dl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	dl->next = NULL;
 | 
			
		||||
	dl->major = major;
 | 
			
		||||
	dl->minor = minor;
 | 
			
		||||
	dl->name = strdup(name);
 | 
			
		||||
 | 
			
		||||
	if (dlb->head) {
 | 
			
		||||
		dlb->tail->next = dl;
 | 
			
		||||
		dlb->tail = dl;
 | 
			
		||||
	} else
 | 
			
		||||
		dlb->head = dlb->tail = dl;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct dev_list *dlb_get(struct dev_list_builder *dlb)
 | 
			
		||||
{
 | 
			
		||||
	return dlb->head;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
static int copy_string(char *dest, const char *src, size_t max)
 | 
			
		||||
{
 | 
			
		||||
	if (strlen(src) + 1 > max)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	strcpy(dest, src);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int copy_name(struct dm_ioctl *ctl, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	return copy_string(ctl->name, name, DM_NAME_LEN);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int copy_uuid(struct dm_ioctl *ctl, const char *uuid)
 | 
			
		||||
{
 | 
			
		||||
	return copy_string(ctl->uuid, uuid, DM_UUID_LEN);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
int dm_version(struct dm_interface *dmi, uint32_t *major, uint32_t *minor, uint32_t *patch)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct dm_ioctl *ctl = alloc_ctl(0);
 | 
			
		||||
 | 
			
		||||
	if (!ctl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	r = ioctl(dmi->fd, DM_VERSION, ctl);
 | 
			
		||||
	*major = ctl->version[0];
 | 
			
		||||
	*minor = ctl->version[1];
 | 
			
		||||
	*patch = ctl->version[2];
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_remove_all(struct dm_interface *dmi)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct dm_ioctl *ctl = alloc_ctl(0);
 | 
			
		||||
 | 
			
		||||
	if (!ctl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	r = ioctl(dmi->fd, DM_REMOVE_ALL, ctl);
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool list_devices(struct dm_interface *dmi, struct dm_ioctl *ctl,
 | 
			
		||||
			 size_t payload_size, struct dev_list **devs,
 | 
			
		||||
			 int *r)
 | 
			
		||||
{
 | 
			
		||||
	struct dm_name_list *nl;
 | 
			
		||||
	struct dev_list_builder dlb;
 | 
			
		||||
 | 
			
		||||
	*r = ioctl(dmi->fd, DM_LIST_DEVICES, ctl);
 | 
			
		||||
	if (*r < 0)
 | 
			
		||||
		return true;
 | 
			
		||||
 | 
			
		||||
	if (ctl->flags & DM_BUFFER_FULL_FLAG) {
 | 
			
		||||
		free_ctl(ctl);
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dlb_init(&dlb);
 | 
			
		||||
	nl = (struct dm_name_list *) payload(ctl);
 | 
			
		||||
 | 
			
		||||
	if (nl->dev) {
 | 
			
		||||
		for (;;) {
 | 
			
		||||
			dlb_append(&dlb, major(nl->dev), minor(nl->dev), nl->name);
 | 
			
		||||
 | 
			
		||||
			if (!nl->next)
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			nl = (struct dm_name_list *) (((unsigned char *) nl) + nl->next);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	*devs = dlb_get(&dlb);
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_list_devices(struct dm_interface *dmi, struct dev_list **devs)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct dm_ioctl *ctl;
 | 
			
		||||
	size_t payload_size = 8192;
 | 
			
		||||
 | 
			
		||||
	ctl = alloc_ctl(payload_size);
 | 
			
		||||
	if (!ctl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	while (!list_devices(dmi, ctl, payload_size, devs, &r)) {
 | 
			
		||||
		payload_size *= 2;
 | 
			
		||||
		ctl = realloc_ctl(ctl, payload_size);
 | 
			
		||||
		if (!ctl)
 | 
			
		||||
			return -ENOMEM;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_create_device(struct dm_interface *dmi, const char *name, const char *uuid)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct dm_ioctl *ctl = alloc_ctl(0);
 | 
			
		||||
 | 
			
		||||
	if (!ctl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	r = copy_name(ctl, name);
 | 
			
		||||
	if (r) {
 | 
			
		||||
		free_ctl(ctl);
 | 
			
		||||
		return r;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r = copy_uuid(ctl, name);
 | 
			
		||||
	if (r) {
 | 
			
		||||
		free_ctl(ctl);
 | 
			
		||||
		return r;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r = ioctl(dmi->fd, DM_DEV_CREATE, ctl);
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dev_cmd(struct dm_interface *dmi, const char *name, int request, unsigned flags)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	struct dm_ioctl *ctl = alloc_ctl(0);
 | 
			
		||||
	ctl->flags = flags;
 | 
			
		||||
	r = copy_name(ctl, name);
 | 
			
		||||
	if (r) {
 | 
			
		||||
		free_ctl(ctl);
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
	}
 | 
			
		||||
	r = ioctl(dmi->fd, request);
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_remove_device(struct dm_interface *dmi, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	return dev_cmd(dmi, name, DM_DEV_REMOVE, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_suspend_device(struct dm_interface *dmi, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	return dev_cmd(dmi, name, DM_DEV_SUSPEND, DM_SUSPEND_FLAG);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_resume_device(struct dm_interface *dmi, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	return dev_cmd(dmi, name, DM_DEV_SUSPEND, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_clear_device(struct dm_interface *dmi, const char *name)
 | 
			
		||||
{
 | 
			
		||||
	return dev_cmd(dmi, name, DM_TABLE_CLEAR, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
struct target {
 | 
			
		||||
	struct target *next;
 | 
			
		||||
 | 
			
		||||
	uint64_t len;
 | 
			
		||||
	char *type;
 | 
			
		||||
	char *args;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void free_targets(struct target *t)
 | 
			
		||||
{
 | 
			
		||||
	while (t) {
 | 
			
		||||
		struct target *next = t->next;
 | 
			
		||||
		free(t->type);
 | 
			
		||||
		free(t->args);
 | 
			
		||||
		t = next;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct target_builder {
 | 
			
		||||
	struct target *head, *tail;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void tb_init(struct target_builder *tb)
 | 
			
		||||
{
 | 
			
		||||
	tb->head = tb->tail = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int tb_append(struct target_builder *tb, uint64_t len, char *type, char *args)
 | 
			
		||||
{
 | 
			
		||||
	struct target *t = malloc(sizeof(*t));
 | 
			
		||||
	if (!t)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	t->next = NULL;
 | 
			
		||||
	t->len = len;
 | 
			
		||||
	t->type = strdup(type);
 | 
			
		||||
	t->args = strdup(args);
 | 
			
		||||
 | 
			
		||||
	if (tb->head) {
 | 
			
		||||
		tb->tail->next = t;
 | 
			
		||||
		tb->tail = t;
 | 
			
		||||
	} else
 | 
			
		||||
		tb->head = tb->tail = t;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct target *tb_get(struct target_builder *tb)
 | 
			
		||||
{
 | 
			
		||||
	return tb->head;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
// FIXME: provide some way of freeing a target list.
 | 
			
		||||
// FIXME: check the result from alloc_ctl is always being checked.
 | 
			
		||||
 | 
			
		||||
static size_t calc_load_payload(struct target *t)
 | 
			
		||||
{
 | 
			
		||||
	size_t space = 0;
 | 
			
		||||
 | 
			
		||||
	while (t) {
 | 
			
		||||
		space += sizeof(struct dm_target_spec);
 | 
			
		||||
		space += strlen(t->args) + 16;
 | 
			
		||||
		t = t->next;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return space + 128;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int prep_load(struct dm_ioctl *ctl, size_t payload_size,
 | 
			
		||||
		     const char *name, struct target *t)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	uint64_t current_sector = 0;
 | 
			
		||||
	struct dm_target_spec *spec;
 | 
			
		||||
 | 
			
		||||
	spec = payload(ctl);
 | 
			
		||||
 | 
			
		||||
	while (t) {
 | 
			
		||||
		spec->sector_start = current_sector;
 | 
			
		||||
		current_sector += t->len;
 | 
			
		||||
		spec->length = t->len;
 | 
			
		||||
		spec->status = 0;
 | 
			
		||||
		r = copy_string(spec->target_type, t->type, DM_MAX_TYPE_NAME);
 | 
			
		||||
		if (r)
 | 
			
		||||
			return r;
 | 
			
		||||
 | 
			
		||||
		r = copy_string((char *) spec + 1, t->args, payload_size);
 | 
			
		||||
		if (r)
 | 
			
		||||
			return r;
 | 
			
		||||
 | 
			
		||||
		spec->next = sizeof(*spec) + round_up(strlen(t->args) + 1, 8);
 | 
			
		||||
		payload_size -= spec->next;
 | 
			
		||||
		spec = (struct dm_target_spec *) (((char *) spec) + spec->next);
 | 
			
		||||
 | 
			
		||||
		t = t->next;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_load(struct dm_interface *dmi, const char *name,
 | 
			
		||||
	    struct target *targets)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	size_t payload_size = calc_load_payload(targets);
 | 
			
		||||
	struct dm_ioctl *ctl = alloc_ctl(payload_size);
 | 
			
		||||
 | 
			
		||||
	if (!ctl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	r = prep_load(ctl, payload_size, name, targets);
 | 
			
		||||
	if (r) {
 | 
			
		||||
		free_ctl(ctl);
 | 
			
		||||
		return r;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r = copy_name(ctl, name);
 | 
			
		||||
	if (r) {
 | 
			
		||||
		free_ctl(ctl);
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r = ioctl(dmi->fd, DM_TABLE_LOAD, ctl);
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
// returns false if control buffer too small.
 | 
			
		||||
static bool get_status(struct dm_interface *dmi, struct dm_ioctl *ctl,
 | 
			
		||||
		       const char *name, unsigned flags,
 | 
			
		||||
		       int *result)
 | 
			
		||||
{
 | 
			
		||||
	*result = copy_name(ctl, name);
 | 
			
		||||
	if (*result) {
 | 
			
		||||
		free_ctl(ctl);
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctl->flags = flags;
 | 
			
		||||
	ctl->target_count = 0;
 | 
			
		||||
 | 
			
		||||
	*result = ioctl(dmi->fd, DM_TABLE_STATUS, ctl);
 | 
			
		||||
	if (*result)
 | 
			
		||||
		return true;
 | 
			
		||||
 | 
			
		||||
	if (ctl->flags & DM_BUFFER_FULL_FLAG)
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int unpack_status(struct dm_ioctl *ctl, struct target **result)
 | 
			
		||||
{
 | 
			
		||||
	unsigned i;
 | 
			
		||||
	struct target_builder tb;
 | 
			
		||||
	struct dm_target_spec *spec = payload(ctl);
 | 
			
		||||
 | 
			
		||||
	tb_init(&tb);
 | 
			
		||||
	for (i = 0; i < ctl->target_count; i++) {
 | 
			
		||||
		tb_append(&tb, spec->length, spec->target_type, (char *) (spec + 1));
 | 
			
		||||
		spec = (struct dm_target_spec *) (((char *) spec) + spec->next);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	*result = tb_get(&tb);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int status_cmd(struct dm_interface *dmi, const char *name,
 | 
			
		||||
		      struct target **targets, unsigned flags)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	size_t payload_size = 8192;
 | 
			
		||||
	struct dm_ioctl *ctl = NULL;
 | 
			
		||||
 | 
			
		||||
	ctl = alloc_ctl(payload_size);
 | 
			
		||||
	if (!ctl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
retry:
 | 
			
		||||
	if (!get_status(dmi, ctl, name, flags, &r)) {
 | 
			
		||||
		payload_size *= 2;
 | 
			
		||||
		ctl = realloc_ctl(ctl, payload_size);
 | 
			
		||||
		if (!ctl)
 | 
			
		||||
			return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
		goto retry;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r = unpack_status(ctl, targets);
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_status(struct dm_interface *dmi, const char *name, struct target **targets)
 | 
			
		||||
{
 | 
			
		||||
	return status_cmd(dmi, name, targets, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int dm_table(struct dm_interface *dmi, const char *name, struct target **targets)
 | 
			
		||||
{
 | 
			
		||||
	return status_cmd(dmi, name, targets, DM_STATUS_TABLE_FLAG);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if 0
 | 
			
		||||
int dm_info(struct dm_interface *dmi, const char *name, struct target **targets)
 | 
			
		||||
{
 | 
			
		||||
	return status_cmd(dmi, name, targets, DM_STATUS_INFO_FLAG);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int dm_message(struct dm_interface *dmi, const char *name, uint64_t sector,
 | 
			
		||||
	       const char *msg_str)
 | 
			
		||||
{
 | 
			
		||||
	int r;
 | 
			
		||||
	size_t msg_len = strlen(msg_str) + 1;
 | 
			
		||||
	size_t payload_size = msg_len + 32;
 | 
			
		||||
	struct dm_ioctl *ctl = alloc_ctl(payload_size);
 | 
			
		||||
	struct dm_target_msg *msg;
 | 
			
		||||
 | 
			
		||||
	if (!ctl)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
	msg = payload(ctl);
 | 
			
		||||
	copy_name(ctl, name);
 | 
			
		||||
	msg->sector = sector;
 | 
			
		||||
	memcpy(msg->message, msg_str, msg_len);
 | 
			
		||||
 | 
			
		||||
	r = ioctl(dmi->fd, DM_TARGET_MSG, ctl);
 | 
			
		||||
	free_ctl(ctl);
 | 
			
		||||
 | 
			
		||||
	return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//----------------------------------------------------------------
 | 
			
		||||
@@ -16,7 +16,7 @@
 | 
			
		||||
          (srfi s8 receive)
 | 
			
		||||
          (utils))
 | 
			
		||||
 | 
			
		||||
  (define __ (load-shared-object "./device-mapper/dm-ioctl.so"))
 | 
			
		||||
  (define __ (load-shared-object "../lib/libft.so"))
 | 
			
		||||
 | 
			
		||||
  (define (fail msg)
 | 
			
		||||
    (raise
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user