diff --git a/abuse.c b/abuse.c index 7391847..fc4ffb4 100644 --- a/abuse.c +++ b/abuse.c @@ -13,7 +13,7 @@ #include "abuse.h" -int abuse_main(int argc, char *argv[], struct abuse_operations *aop) +int abuse_main(int argc, char *argv[], const struct abuse_operations *aop, void *userdata) { int sp[2]; int nbd, sk, err, tmp_fd; @@ -25,6 +25,8 @@ int abuse_main(int argc, char *argv[], struct abuse_operations *aop) struct nbd_reply reply; void *chunk; + (void) userdata; + assert(argc == 2); dev_file = argv[1]; @@ -73,16 +75,17 @@ int abuse_main(int argc, char *argv[], struct abuse_operations *aop) /* FIXME: these might need conversion from the network byte order. */ len = ntohl(request.len); from = request.from; + (void) from; assert(request.magic == htonl(NBD_REQUEST_MAGIC)); switch(ntohl(request.type)) { case NBD_CMD_READ: chunk = malloc(len + sizeof(struct nbd_reply)); - memset((char *)chunk + sizeof(struct nbd_reply), 0, len); + 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)); fprintf(stderr, "Wrote %d bytes.\n", bytes_written); - assert(bytes_written == len + sizeof(struct nbd_reply)); + /* assert(bytes_written == len + sizeof(struct nbd_reply)); */ free(chunk); break; /* We'll not worry about the other cases for now. */ diff --git a/abuse.h b/abuse.h index ced6ae6..38b3609 100644 --- a/abuse.h +++ b/abuse.h @@ -4,8 +4,8 @@ #include struct abuse_operations { - void (*read)(); - void (*write)(); + 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)(); @@ -13,6 +13,6 @@ struct abuse_operations { u_int64_t size; }; -int abuse_main(int argc, char *argv[], struct abuse_operations *aop); +int abuse_main(int argc, char *argv[], const struct abuse_operations *aop, void *userdata); #endif /* ABUSE_H_INCLUDED */ diff --git a/abuse_example.c b/abuse_example.c index eb6ce03..0b8992d 100644 --- a/abuse_example.c +++ b/abuse_example.c @@ -1,11 +1,23 @@ #include +#include #include "abuse.h" +static int xmp_read(void *buf, u_int32_t len, u_int64_t offset) +{ + (void) offset; + + memset(buf, 0, len); + + return 0; +} + +static struct abuse_operations aop = { + .read = xmp_read, + .size = 1024, +}; + int main(int argc, char *argv[]) { - struct abuse_operations aop; - aop.size = 1024; - - return abuse_main(argc, argv, &aop); + return abuse_main(argc, argv, &aop, NULL); }