Conflicts:
	buse.c
	buse.h
	busexmp.c
This commit is contained in:
comatose 2012-12-03 08:29:18 +09:00
commit 18c7ede4fb
3 changed files with 134 additions and 114 deletions

231
buse.c
View File

@ -18,150 +18,159 @@
*/ */
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
u_int64_t ntohll(u_int64_t a) { u_int64_t ntohll(u_int64_t a) {
return a; return a;
} }
#else #else
u_int64_t ntohll(u_int64_t a) { u_int64_t ntohll(u_int64_t a) {
u_int32_t lo = a & 0xffffffff; u_int32_t lo = a & 0xffffffff;
u_int32_t hi = a >> 32U; u_int32_t hi = a >> 32U;
lo = ntohl(lo); lo = ntohl(lo);
hi = ntohl(hi); hi = ntohl(hi);
return ((u_int64_t) lo) << 32U | hi; return ((u_int64_t) lo) << 32U | hi;
} }
#endif #endif
#define htonll ntohll #define htonll ntohll
static int read_all(int fd, void *buf, size_t count) static int read_all(int fd, char* buf, size_t count)
{ {
int bytes_read; int bytes_read;
while (count > 0) { while (count > 0) {
bytes_read = read(fd, buf, count); bytes_read = read(fd, buf, count);
assert(bytes_read > 0); assert(bytes_read > 0);
buf = (char *)buf + bytes_read; buf += bytes_read;
count -= bytes_read; count -= bytes_read;
} }
assert(count == 0); assert(count == 0);
return 0; return 0;
} }
static int write_all(int fd, const void *buf, size_t count) static int write_all(int fd, char* buf, size_t count)
{ {
int bytes_written; int bytes_written;
while (count > 0) { while (count > 0) {
bytes_written = write(fd, buf, count); bytes_written = write(fd, buf, count);
assert(bytes_written > 0); assert(bytes_written > 0);
buf = (char *)buf + bytes_written; buf += bytes_written;
count -= bytes_written; count -= bytes_written;
} }
assert(count == 0); assert(count == 0);
return 0; return 0;
} }
int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *userdata) int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *userdata)
{ {
int sp[2]; int sp[2];
int nbd, sk, err, tmp_fd; int nbd, sk, err, tmp_fd;
u_int64_t from; u_int64_t from;
u_int32_t len, bytes_read; u_int32_t len, bytes_read;
char *dev_file; char *dev_file;
struct nbd_request request; struct nbd_request request;
struct nbd_reply reply; struct nbd_reply reply;
void *chunk; void *chunk;
(void) userdata; (void) userdata;
assert(argc == 2); assert(argc == 2);
dev_file = argv[1]; dev_file = argv[1];
assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp)); assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp));
nbd = open(dev_file, O_RDWR); nbd = open(dev_file, O_RDWR);
assert(nbd != -1); assert(nbd != -1);
assert(ioctl(nbd, NBD_SET_SIZE, aop->size) != -1); assert(ioctl(nbd, NBD_SET_SIZE, aop->size) != -1);
assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1); assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1);
if (!fork()) { if (!fork()) {
/* The child needs to continue setting things up. */ /* The child needs to continue setting things up. */
close(sp[0]); close(sp[0]);
sk = sp[1]; sk = sp[1];
assert(ioctl(nbd, NBD_SET_SOCK, sk) != -1); if(ioctl(nbd, NBD_SET_SOCK, sk) == -1){
assert(ioctl(nbd, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM) != -1); fprintf(stderr, "ioctl(nbd, NBD_SET_SOCK, sk) failed.[%s]\n", strerror(errno));
err = ioctl(nbd, NBD_DO_IT); }
fprintf(stderr, "nbd device terminated with code %d\n", err); else if(ioctl(nbd, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM) == -1){
if (err == -1) fprintf(stderr, "ioctl(nbd, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM) failed.[%s]\n", strerror(errno));
fprintf(stderr, "%s\n", strerror(errno)); }
else{
assert(ioctl(nbd, NBD_CLEAR_QUE) != -1); err = ioctl(nbd, NBD_DO_IT);
assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1); fprintf(stderr, "nbd device terminated with code %d\n", err);
if (err == -1)
exit(0); fprintf(stderr, "%s\n", strerror(errno));
} }
/* The parent opens the device file at least once, to make sure the ioctl(nbd, NBD_CLEAR_QUE);
* partition table is updated. Then it closes it and starts serving up ioctl(nbd, NBD_CLEAR_SOCK);
* requests. */
tmp_fd = open(dev_file, O_RDONLY); exit(0);
assert(tmp_fd != -1); }
close(tmp_fd);
close(sp[1]); /* The parent opens the device file at least once, to make sure the
sk = sp[0]; * partition table is updated. Then it closes it and starts serving up
* requests. */
reply.magic = htonl(NBD_REPLY_MAGIC); tmp_fd = open(dev_file, O_RDONLY);
reply.error = htonl(0); assert(tmp_fd != -1);
close(tmp_fd);
while (1) { close(sp[1]);
bytes_read = read(sk, &request, sizeof(request)); sk = sp[0];
assert(bytes_read == sizeof(request));
memcpy(reply.handle, request.handle, sizeof(reply.handle));
len = ntohl(request.len); reply.magic = htonl(NBD_REPLY_MAGIC);
from = ntohll(request.from); reply.error = htonl(0);
assert(request.magic == htonl(NBD_REQUEST_MAGIC));
switch(ntohl(request.type)) { while ((bytes_read = read(sk, &request, sizeof(request))) > 0) {
/* I may at some point need to deal with the the fact that the assert(bytes_read == sizeof(request));
* official nbd server has a maximum buffer size, and divides up memcpy(reply.handle, request.handle, sizeof(reply.handle));
* oversized requests into multiple pieces. This applies to reads
* and writes. len = ntohl(request.len);
*/ from = ntohll(request.from);
case NBD_CMD_READ: assert(request.magic == htonl(NBD_REQUEST_MAGIC));
/* fprintf(stderr, "Request for read of size %d\n", len); */
chunk = malloc(len + sizeof(struct nbd_reply)); switch(ntohl(request.type)) {
aop->read((char *)chunk + sizeof(struct nbd_reply), len, from); /* I may at some point need to deal with the the fact that the
memcpy(chunk, &reply, sizeof(struct nbd_reply)); * official nbd server has a maximum buffer size, and divides up
write_all(sk, chunk, len + sizeof(struct nbd_reply)); * oversized requests into multiple pieces. This applies to reads
free(chunk); * and writes.
break; */
case NBD_CMD_WRITE: case NBD_CMD_READ:
/* fprintf(stderr, "Request for write of size %d\n", len); */ /* fprintf(stderr, "Request for read of size %d\n", len); */
chunk = malloc(len); chunk = malloc(len + sizeof(struct nbd_reply));
read_all(sk, chunk, len); reply.error = aop->read((char *)chunk + sizeof(struct nbd_reply), len, from);
aop->write(chunk, len, from); write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
free(chunk); if(reply.error == 0)
write_all(sk, &reply, sizeof(struct nbd_reply)); write_all(sk, (char*)chunk, len);
break; free(chunk);
case NBD_CMD_DISC: break;
/* Handle a disconnect request. */ case NBD_CMD_WRITE:
aop->disc(); /* fprintf(stderr, "Request for write of size %d\n", len); */
return 0; chunk = malloc(len);
case NBD_CMD_FLUSH: read_all(sk, chunk, len);
aop->flush(); reply.error = aop->write(chunk, len, from);
break; free(chunk);
case NBD_CMD_TRIM: write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
aop->trim(from, len); break;
break; case NBD_CMD_DISC:
default: /* Handle a disconnect request. */
assert(0); aop->disc();
} return 0;
case NBD_CMD_FLUSH:
reply.error = aop->flush();
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
break;
case NBD_CMD_TRIM:
reply.error = aop->trim(from, len);
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
break;
default:
assert(0);
} }
}
return 0; if (bytes_read == -1)
fprintf(stderr, "%s\n", strerror(errno));
return 0;
} }

2
buse.h
View File

@ -63,7 +63,7 @@ struct nbd_reply {
struct buse_operations { struct buse_operations {
int (*read)(void *buf, u_int32_t len, u_int64_t offset); int (*read)(void *buf, u_int32_t len, u_int64_t offset);
int (*write)(const void *buf, u_int32_t len, u_int64_t offset); int (*write)(const void *buf, u_int32_t len, u_int64_t offset);
int (*disc)(); void (*disc)();
int (*flush)(); int (*flush)();
int (*trim)(u_int64_t from, u_int32_t len); int (*trim)(u_int64_t from, u_int32_t len);

View File

@ -20,9 +20,20 @@ static int xmp_write(const void *buf, u_int32_t len, u_int64_t offset)
return 0; return 0;
} }
static int xmp_disc() static void xmp_disc()
{ {
fprintf(stderr, "Received a disconnect request.\n"); fprintf(stderr, "Received a disconnect request.\n");
}
static int xmp_flush()
{
fprintf(stderr, "Received a flush request.\n");
return 0;
}
static int xmp_trim(u_int64_t from, u_int32_t len)
{
fprintf(stderr, "T %lu %u\n", from, len);
return 0; return 0;
} }
@ -41,8 +52,8 @@ static struct buse_operations aop = {
.read = xmp_read, .read = xmp_read,
.write = xmp_write, .write = xmp_write,
.disc = xmp_disc, .disc = xmp_disc,
.trim = xmp_trim,
.flush = xmp_flush, .flush = xmp_flush,
.trim = xmp_trim,
.size = 128 * 1024 * 1024, .size = 128 * 1024 * 1024,
}; };