diff --git a/buse.c b/buse.c index 2776bcf..c842de1 100644 --- a/buse.c +++ b/buse.c @@ -61,23 +61,17 @@ static int write_all(int fd, char* buf, size_t count) return 0; } -int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *userdata) +int buse_main(const char* dev_file, const struct buse_operations *aop, void *userdata) { int sp[2]; int nbd, sk, err, tmp_fd; u_int64_t from; u_int32_t len; ssize_t bytes_read; - char *dev_file; struct nbd_request request; struct nbd_reply reply; void *chunk; - (void) userdata; - - assert(argc == 2); - dev_file = argv[1]; - assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp)); nbd = open(dev_file, O_RDWR); @@ -139,32 +133,32 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u * and writes. */ case NBD_CMD_READ: - /* fprintf(stderr, "Request for read of size %d\n", len); */ - chunk = malloc(len + sizeof(struct nbd_reply)); - reply.error = aop->read((char *)chunk + sizeof(struct nbd_reply), len, from); + fprintf(stderr, "Request for read of size %d\n", len); + chunk = malloc(len); + reply.error = aop->read(chunk, len, from, userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); if(reply.error == 0) write_all(sk, (char*)chunk, len); free(chunk); break; case NBD_CMD_WRITE: - /* fprintf(stderr, "Request for write of size %d\n", len); */ + fprintf(stderr, "Request for write of size %d\n", len); chunk = malloc(len); read_all(sk, chunk, len); - reply.error = aop->write(chunk, len, from); + reply.error = aop->write(chunk, len, from, userdata); free(chunk); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); break; case NBD_CMD_DISC: /* Handle a disconnect request. */ - aop->disc(); + aop->disc(userdata); return 0; case NBD_CMD_FLUSH: - reply.error = aop->flush(); + reply.error = aop->flush(userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); break; case NBD_CMD_TRIM: - reply.error = aop->trim(from, len); + reply.error = aop->trim(from, len, userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); break; default: diff --git a/buse.h b/buse.h index 56dad64..96c6298 100644 --- a/buse.h +++ b/buse.h @@ -5,7 +5,7 @@ extern "C" { #endif -/* Most of this file was copied from nbd.h in the nbd distribution. */ + /* Most of this file was copied from nbd.h in the nbd distribution. */ #include #include @@ -21,15 +21,15 @@ extern "C" { #define NBD_SET_TIMEOUT _IO( 0xab, 9 ) #define NBD_SET_FLAGS _IO( 0xab, 10 ) -enum { - NBD_CMD_READ = 0, - NBD_CMD_WRITE = 1, - NBD_CMD_DISC = 2, - NBD_CMD_FLUSH = 3, - NBD_CMD_TRIM = 4 -}; + enum { + NBD_CMD_READ = 0, + NBD_CMD_WRITE = 1, + NBD_CMD_DISC = 2, + NBD_CMD_FLUSH = 3, + NBD_CMD_TRIM = 4 + }; -/* values for flags field */ + /* values for flags field */ #define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */ #define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */ #define NBD_FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */ @@ -37,44 +37,43 @@ enum { #define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */ #define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */ -/* Magic numbers */ + /* Magic numbers */ #define NBD_REQUEST_MAGIC 0x25609513 #define NBD_REPLY_MAGIC 0x67446698 -/* - * This is the packet used for communication between client and - * server. All data are in network byte order. - */ -struct nbd_request { - __be32 magic; - __be32 type; /* == READ || == WRITE */ - char handle[8]; - __be64 from; - __be32 len; -} __attribute__ ((packed)); + /* + * This is the packet used for communication between client and + * server. All data are in network byte order. + */ + struct nbd_request { + __be32 magic; + __be32 type; /* == READ || == WRITE */ + char handle[8]; + __be64 from; + __be32 len; + } __attribute__ ((packed)); -/* - * This is the reply packet that nbd-server sends back to the client after - * it has completed an I/O request (or an error occurs). - */ -struct nbd_reply { - __be32 magic; - __be32 error; /* 0 = ok, else error */ - char handle[8]; /* handle you got from request */ -}; + /* + * This is the reply packet that nbd-server sends back to the client after + * it has completed an I/O request (or an error occurs). + */ + struct nbd_reply { + __be32 magic; + __be32 error; /* 0 = ok, else error */ + char handle[8]; /* handle you got from request */ + }; -struct buse_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)(); - int (*flush)(); - int (*trim)(u_int64_t from, u_int32_t len); + struct buse_operations { + int (*read)(void *buf, u_int32_t len, u_int64_t offset, void *userdata); + int (*write)(const void *buf, u_int32_t len, u_int64_t offset, void *userdata); + void (*disc)(void *userdata); + int (*flush)(void *userdata); + int (*trim)(u_int64_t from, u_int32_t len, void *userdata); u_int64_t size; -}; + }; -int buse_main(int argc, char *argv[], const struct buse_operations *bop, - void *userdata); + int buse_main(const char* dev_file, const struct buse_operations *bop, void *userdata); #ifdef __cplusplus } diff --git a/busexmp.c b/busexmp.c index 7a8db41..9b01f7c 100644 --- a/busexmp.c +++ b/busexmp.c @@ -6,32 +6,32 @@ static void *data; -static int xmp_read(void *buf, u_int32_t len, u_int64_t offset) +static int xmp_read(void *buf, u_int32_t len, u_int64_t offset, void *userdata) { fprintf(stderr, "R - %lu, %u\n", offset, len); memcpy(buf, (char *)data + offset, len); return 0; } -static int xmp_write(const void *buf, u_int32_t len, u_int64_t offset) +static int xmp_write(const void *buf, u_int32_t len, u_int64_t offset, void *userdata) { fprintf(stderr, "W - %lu, %u\n", offset, len); memcpy((char *)data + offset, buf, len); return 0; } -static void xmp_disc() +static void xmp_disc(void *userdata) { fprintf(stderr, "Received a disconnect request.\n"); } -static int xmp_flush() +static int xmp_flush(void *userdata) { fprintf(stderr, "Received a flush request.\n"); return 0; } -static int xmp_trim(u_int64_t from, u_int32_t len){ +static int xmp_trim(u_int64_t from, u_int32_t len, void *userdata){ fprintf(stderr, "T - %lu, %u\n", from, len); return 0; } @@ -50,5 +50,5 @@ int main(int argc, char *argv[]) { data = malloc(aop.size); - return buse_main(argc, argv, &aop, NULL); + return buse_main(argv[1], &aop, NULL); } diff --git a/loopback.c b/loopback.c index 86179e0..24c1bab 100644 --- a/loopback.c +++ b/loopback.c @@ -18,7 +18,7 @@ static void usage(void) fprintf(stderr, "Usage: loopback \n"); } -static int loopback_read(void *buf, u_int32_t len, u_int64_t offset) +static int loopback_read(void *buf, u_int32_t len, u_int64_t offset, void *userdata) { int bytes_read; @@ -33,7 +33,7 @@ static int loopback_read(void *buf, u_int32_t len, u_int64_t offset) return 0; } -static int loopback_write(const void *buf, u_int32_t len, u_int64_t offset) +static int loopback_write(const void *buf, u_int32_t len, u_int64_t offset, void *userdata) { int bytes_written; @@ -78,7 +78,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "The size of this device is %ld bytes.\n", size); bop.size = size; - buse_main(argc, argv, &bop, NULL); + buse_main(argv[1], &bop, NULL); return 0; }