* buse.c: Fixed a bug in read op.
This commit is contained in:
parent
9e9f31bf90
commit
897f21e4d3
24
buse.c
24
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:
|
||||
|
|
13
buse.h
13
buse.h
|
@ -64,17 +64,16 @@ struct nbd_reply {
|
|||
};
|
||||
|
||||
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);
|
||||
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
|
||||
}
|
||||
|
|
12
busexmp.c
12
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);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ static void usage(void)
|
|||
fprintf(stderr, "Usage: loopback <phyical device> <virtual device>\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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue