From 36a70c004eae7b183fb2709cb99f528f5b138f93 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 27 Jun 2013 13:27:10 -0400 Subject: [PATCH 1/5] buse.c: conditional defines to work with older nbd versions --- buse.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/buse.c b/buse.c index c842de1..508fea3 100644 --- a/buse.c +++ b/buse.c @@ -88,9 +88,11 @@ int buse_main(const char* dev_file, const struct buse_operations *aop, void *use if(ioctl(nbd, NBD_SET_SOCK, sk) == -1){ fprintf(stderr, "ioctl(nbd, NBD_SET_SOCK, sk) failed.[%s]\n", strerror(errno)); } +#if defined NBD_SET_FLAGS && defined NBD_FLAG_SEND_TRIM else if(ioctl(nbd, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM) == -1){ fprintf(stderr, "ioctl(nbd, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM) failed.[%s]\n", strerror(errno)); } +#endif else{ err = ioctl(nbd, NBD_DO_IT); fprintf(stderr, "nbd device terminated with code %d\n", err); @@ -153,14 +155,18 @@ int buse_main(const char* dev_file, const struct buse_operations *aop, void *use /* Handle a disconnect request. */ aop->disc(userdata); return 0; +#if defined NBD_CMD_FLUSH case NBD_CMD_FLUSH: reply.error = aop->flush(userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); break; +#endif +#if defined NBD_CMD_TRIM case NBD_CMD_TRIM: reply.error = aop->trim(from, len, userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); break; +#endif default: assert(0); } From 8439322930a4cebdeb3c7ec1438f33d65fea9fc5 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 27 Jun 2013 13:27:40 -0400 Subject: [PATCH 2/5] buse.h: use distro's nbd.h instead of copy/paste --- buse.h | 55 +------------------------------------------------------ 1 file changed, 1 insertion(+), 54 deletions(-) diff --git a/buse.h b/buse.h index 96c6298..4cedf1b 100644 --- a/buse.h +++ b/buse.h @@ -8,60 +8,7 @@ extern "C" { /* Most of this file was copied from nbd.h in the nbd distribution. */ #include #include - -#define NBD_SET_SOCK _IO( 0xab, 0 ) -#define NBD_SET_BLKSIZE _IO( 0xab, 1 ) -#define NBD_SET_SIZE _IO( 0xab, 2 ) -#define NBD_DO_IT _IO( 0xab, 3 ) -#define NBD_CLEAR_SOCK _IO( 0xab, 4 ) -#define NBD_CLEAR_QUE _IO( 0xab, 5 ) -#define NBD_PRINT_DEBUG _IO( 0xab, 6 ) -#define NBD_SET_SIZE_BLOCKS _IO( 0xab, 7 ) -#define NBD_DISCONNECT _IO( 0xab, 8 ) -#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 - }; - - /* 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 */ -#define NBD_FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */ -#define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */ -#define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */ - - /* 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 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 */ - }; +#include struct buse_operations { int (*read)(void *buf, u_int32_t len, u_int64_t offset, void *userdata); From c322c1035f2b18851355c12951ce6872471fd1b2 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 27 Jun 2013 15:09:17 -0400 Subject: [PATCH 3/5] loopback.c: fix bug in call to buse_main() --- loopback.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loopback.c b/loopback.c index 24c1bab..4e90f34 100644 --- a/loopback.c +++ b/loopback.c @@ -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(argv[1], &bop, NULL); + buse_main(argv[2], &bop, NULL); return 0; } From e4fd3b729fb227e7a862be7d9a8d6ffd3429bb61 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 27 Jun 2013 16:47:27 -0400 Subject: [PATCH 4/5] loopback.c, busexmp.c: get rid of compiler warnings --- busexmp.c | 16 ++++++++++++---- loopback.c | 2 ++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/busexmp.c b/busexmp.c index 9b01f7c..f5fa75a 100644 --- a/busexmp.c +++ b/busexmp.c @@ -5,33 +5,40 @@ #include "buse.h" static void *data; +static int xmpl_debug = 1; static int xmp_read(void *buf, u_int32_t len, u_int64_t offset, void *userdata) { - fprintf(stderr, "R - %lu, %u\n", offset, len); + if (*(int *)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, void *userdata) { - fprintf(stderr, "W - %lu, %u\n", offset, len); + if (*(int *)userdata) + fprintf(stderr, "W - %lu, %u\n", offset, len); memcpy((char *)data + offset, buf, len); return 0; } static void xmp_disc(void *userdata) { + (void)(userdata); fprintf(stderr, "Received a disconnect request.\n"); } static int xmp_flush(void *userdata) { + (void)(userdata); fprintf(stderr, "Received a flush request.\n"); return 0; } -static int xmp_trim(u_int64_t from, u_int32_t len, void *userdata){ +static int xmp_trim(u_int64_t from, u_int32_t len, void *userdata) +{ + (void)(userdata); fprintf(stderr, "T - %lu, %u\n", from, len); return 0; } @@ -48,7 +55,8 @@ static struct buse_operations aop = { int main(int argc, char *argv[]) { + (void)(argc); data = malloc(aop.size); - return buse_main(argv[1], &aop, NULL); + return buse_main(argv[1], &aop, (void *)&xmpl_debug); } diff --git a/loopback.c b/loopback.c index 4e90f34..f491d59 100644 --- a/loopback.c +++ b/loopback.c @@ -21,6 +21,7 @@ static void usage(void) static int loopback_read(void *buf, u_int32_t len, u_int64_t offset, void *userdata) { int bytes_read; + (void)(userdata); lseek64(fd, offset, SEEK_SET); while (len > 0) { @@ -36,6 +37,7 @@ static int loopback_read(void *buf, u_int32_t len, u_int64_t offset, void *userd static int loopback_write(const void *buf, u_int32_t len, u_int64_t offset, void *userdata) { int bytes_written; + (void)(userdata); lseek64(fd, offset, SEEK_SET); while (len > 0) { From 6ba3d2604221699854c30dc4c99f42ca522afc56 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Thu, 27 Jun 2013 16:51:07 -0400 Subject: [PATCH 5/5] buse.c: add assertions to avoid null function ptrs --- buse.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/buse.c b/buse.c index 508fea3..dd9ea64 100644 --- a/buse.c +++ b/buse.c @@ -136,6 +136,7 @@ int buse_main(const char* dev_file, const struct buse_operations *aop, void *use */ case NBD_CMD_READ: fprintf(stderr, "Request for read of size %d\n", len); + assert(aop->read); chunk = malloc(len); reply.error = aop->read(chunk, len, from, userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); @@ -145,6 +146,7 @@ int buse_main(const char* dev_file, const struct buse_operations *aop, void *use break; case NBD_CMD_WRITE: fprintf(stderr, "Request for write of size %d\n", len); + assert(aop->write); chunk = malloc(len); read_all(sk, chunk, len); reply.error = aop->write(chunk, len, from, userdata); @@ -153,16 +155,19 @@ int buse_main(const char* dev_file, const struct buse_operations *aop, void *use break; case NBD_CMD_DISC: /* Handle a disconnect request. */ + assert(aop->disc); aop->disc(userdata); return 0; #if defined NBD_CMD_FLUSH case NBD_CMD_FLUSH: + assert(aop->flush); reply.error = aop->flush(userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); break; #endif #if defined NBD_CMD_TRIM case NBD_CMD_TRIM: + assert(aop->trim); reply.error = aop->trim(from, len, userdata); write_all(sk, (char*)&reply, sizeof(struct nbd_reply)); break;