From fa6dc0152cf920b440370b080b925b4c9cf2bbad Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Sun, 9 Oct 2011 22:30:30 -0700 Subject: [PATCH] Committing various changes. --- abuse.c | 23 +++++++++++++++++++---- abuse.h | 6 +++--- abusexmp.c | 16 +++++++++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/abuse.c b/abuse.c index 88f0585..da976bd 100644 --- a/abuse.c +++ b/abuse.c @@ -36,8 +36,7 @@ int abuse_main(int argc, char *argv[], const struct abuse_operations *aop, void int sp[2]; int nbd, sk, err, tmp_fd; u_int64_t from; - u_int32_t len; - int bytes_written, bytes_read; + u_int32_t len, bytes_read, bytes_written; char *dev_file; struct nbd_request request; struct nbd_reply reply; @@ -92,27 +91,43 @@ int abuse_main(int argc, char *argv[], const struct abuse_operations *aop, void len = ntohl(request.len); from = ntohll(request.from); - (void) from; assert(request.magic == htonl(NBD_REQUEST_MAGIC)); switch(ntohl(request.type)) { + /* I may at some point need to deal with the the fact that the + * official nbd server has a maximum buffer size, and divides up + * oversized requests into multiple pieces. This applies to reads + * and writes. + */ case NBD_CMD_READ: chunk = malloc(len + sizeof(struct nbd_reply)); aop->read((char *)chunk + sizeof(struct nbd_reply), len, from); memcpy(chunk, &reply, sizeof(struct nbd_reply)); bytes_written = write(sk, chunk, len + sizeof(struct nbd_reply)); + assert(bytes_written == len + sizeof(struct nbd_reply)); fprintf(stderr, "Wrote %d bytes.\n", bytes_written); - /* assert(bytes_written == len + sizeof(struct nbd_reply)); */ free(chunk); break; case NBD_CMD_WRITE: + chunk = malloc(len); + bytes_read = read(sk, &chunk, len); + assert(bytes_read == len); + aop->write(chunk, len, from); + free(chunk); + bytes_written = write(sk, &reply, sizeof(struct nbd_reply)); + assert(bytes_written == sizeof(struct nbd_reply)); break; case NBD_CMD_DISC: + aop->disc(); break; case NBD_CMD_FLUSH: + aop->flush(); break; case NBD_CMD_TRIM: + aop->trim(); break; + default: + assert(0); } } diff --git a/abuse.h b/abuse.h index 999f421..a4b2bfd 100644 --- a/abuse.h +++ b/abuse.h @@ -6,9 +6,9 @@ struct abuse_operations { 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); - void (*disc)(); - void (*flush)(); - void (*trim)(); + int (*disc)(); + int (*flush)(); + int (*trim)(); u_int64_t size; }; diff --git a/abusexmp.c b/abusexmp.c index 0b8992d..a5e334c 100644 --- a/abusexmp.c +++ b/abusexmp.c @@ -3,21 +3,31 @@ #include "abuse.h" +static void *data; + static int xmp_read(void *buf, u_int32_t len, u_int64_t offset) { - (void) offset; - + /* memcpy(buf, (char *)data + offset, len); */ memset(buf, 0, len); return 0; } +static int xmp_write(const void *buf, u_int32_t len, u_int64_t offset) +{ + memcpy((char *)data + offset, buf, len); + return 0; +} + static struct abuse_operations aop = { .read = xmp_read, - .size = 1024, + .write = xmp_write, + .size = 128 * 1024 * 1024, }; int main(int argc, char *argv[]) { + data = malloc(aop.size); + return abuse_main(argc, argv, &aop, NULL); }