buse/busexmp.c

91 lines
2.3 KiB
C
Raw Permalink Normal View History

/*
* busexmp - example memory-based block device using BUSE
* Copyright (C) 2013 Adam Cozzette
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buse.h"
2011-09-20 10:31:35 +02:00
2011-10-10 07:30:30 +02:00
static void *data;
static int xmpl_debug = 1;
2011-10-10 07:30:30 +02:00
2012-12-07 12:58:26 +01:00
static int xmp_read(void *buf, u_int32_t len, u_int64_t offset, void *userdata)
2011-09-20 10:31:35 +02:00
{
if (*(int *)userdata)
fprintf(stderr, "R - %lu, %u\n", offset, len);
2012-12-03 00:25:06 +01:00
memcpy(buf, (char *)data + offset, len);
return 0;
}
2012-12-07 12:58:26 +01:00
static int xmp_write(const void *buf, u_int32_t len, u_int64_t offset, void *userdata)
2011-10-10 07:30:30 +02:00
{
if (*(int *)userdata)
fprintf(stderr, "W - %lu, %u\n", offset, len);
2012-12-03 00:25:06 +01:00
memcpy((char *)data + offset, buf, len);
return 0;
2011-10-10 07:30:30 +02:00
}
2012-12-07 12:58:26 +01:00
static void xmp_disc(void *userdata)
{
(void)(userdata);
2012-12-03 00:25:06 +01:00
fprintf(stderr, "Received a disconnect request.\n");
2012-11-30 16:42:29 +01:00
}
2012-12-07 12:58:26 +01:00
static int xmp_flush(void *userdata)
2012-11-30 16:42:29 +01:00
{
(void)(userdata);
2012-11-30 16:42:29 +01:00
fprintf(stderr, "Received a flush request.\n");
return 0;
}
static int xmp_trim(u_int64_t from, u_int32_t len, void *userdata)
{
(void)(userdata);
2012-12-03 00:25:06 +01:00
fprintf(stderr, "T - %lu, %u\n", from, len);
return 0;
}
static struct buse_operations aop = {
2012-12-03 00:25:06 +01:00
.read = xmp_read,
.write = xmp_write,
.disc = xmp_disc,
.flush = xmp_flush,
2012-11-30 16:42:29 +01:00
.trim = xmp_trim,
2012-12-03 00:25:06 +01:00
.size = 128 * 1024 * 1024,
};
int main(int argc, char *argv[])
{
2016-01-31 22:17:11 +01:00
if (argc != 2)
{
fprintf(stderr,
"Usage:\n"
" %s /dev/nbd0\n"
"Don't forget to load nbd kernel module (`modprobe nbd`) and\n"
"run example from root.\n", argv[0]);
return 1;
}
2012-12-03 00:25:06 +01:00
data = malloc(aop.size);
2011-10-10 07:30:30 +02:00
return buse_main(argv[1], &aop, (void *)&xmpl_debug);
2011-09-20 10:31:35 +02:00
}