* 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;
|
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 sp[2];
|
||||||
int nbd, sk, err, tmp_fd;
|
int nbd, sk, err, tmp_fd;
|
||||||
u_int64_t from;
|
u_int64_t from;
|
||||||
u_int32_t len;
|
u_int32_t len;
|
||||||
ssize_t bytes_read;
|
ssize_t bytes_read;
|
||||||
char *dev_file;
|
|
||||||
struct nbd_request request;
|
struct nbd_request request;
|
||||||
struct nbd_reply reply;
|
struct nbd_reply reply;
|
||||||
void *chunk;
|
void *chunk;
|
||||||
|
|
||||||
(void) userdata;
|
|
||||||
|
|
||||||
assert(argc == 2);
|
|
||||||
dev_file = argv[1];
|
|
||||||
|
|
||||||
assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp));
|
assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp));
|
||||||
|
|
||||||
nbd = open(dev_file, O_RDWR);
|
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.
|
* and writes.
|
||||||
*/
|
*/
|
||||||
case NBD_CMD_READ:
|
case NBD_CMD_READ:
|
||||||
/* fprintf(stderr, "Request for read of size %d\n", len); */
|
fprintf(stderr, "Request for read of size %d\n", len);
|
||||||
chunk = malloc(len + sizeof(struct nbd_reply));
|
chunk = malloc(len);
|
||||||
reply.error = aop->read((char *)chunk + sizeof(struct nbd_reply), len, from);
|
reply.error = aop->read(chunk, len, from, userdata);
|
||||||
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
||||||
if(reply.error == 0)
|
if(reply.error == 0)
|
||||||
write_all(sk, (char*)chunk, len);
|
write_all(sk, (char*)chunk, len);
|
||||||
free(chunk);
|
free(chunk);
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_WRITE:
|
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);
|
chunk = malloc(len);
|
||||||
read_all(sk, chunk, len);
|
read_all(sk, chunk, len);
|
||||||
reply.error = aop->write(chunk, len, from);
|
reply.error = aop->write(chunk, len, from, userdata);
|
||||||
free(chunk);
|
free(chunk);
|
||||||
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_DISC:
|
case NBD_CMD_DISC:
|
||||||
/* Handle a disconnect request. */
|
/* Handle a disconnect request. */
|
||||||
aop->disc();
|
aop->disc(userdata);
|
||||||
return 0;
|
return 0;
|
||||||
case NBD_CMD_FLUSH:
|
case NBD_CMD_FLUSH:
|
||||||
reply.error = aop->flush();
|
reply.error = aop->flush(userdata);
|
||||||
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_TRIM:
|
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));
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
77
buse.h
77
buse.h
|
@ -5,7 +5,7 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#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 <linux/types.h>
|
#include <linux/types.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
@ -21,15 +21,15 @@ extern "C" {
|
||||||
#define NBD_SET_TIMEOUT _IO( 0xab, 9 )
|
#define NBD_SET_TIMEOUT _IO( 0xab, 9 )
|
||||||
#define NBD_SET_FLAGS _IO( 0xab, 10 )
|
#define NBD_SET_FLAGS _IO( 0xab, 10 )
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
NBD_CMD_READ = 0,
|
NBD_CMD_READ = 0,
|
||||||
NBD_CMD_WRITE = 1,
|
NBD_CMD_WRITE = 1,
|
||||||
NBD_CMD_DISC = 2,
|
NBD_CMD_DISC = 2,
|
||||||
NBD_CMD_FLUSH = 3,
|
NBD_CMD_FLUSH = 3,
|
||||||
NBD_CMD_TRIM = 4
|
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_HAS_FLAGS (1 << 0) /* Flags are there */
|
||||||
#define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
|
#define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
|
||||||
#define NBD_FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */
|
#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_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */
|
||||||
#define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */
|
#define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */
|
||||||
|
|
||||||
/* Magic numbers */
|
/* Magic numbers */
|
||||||
#define NBD_REQUEST_MAGIC 0x25609513
|
#define NBD_REQUEST_MAGIC 0x25609513
|
||||||
#define NBD_REPLY_MAGIC 0x67446698
|
#define NBD_REPLY_MAGIC 0x67446698
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is the packet used for communication between client and
|
* This is the packet used for communication between client and
|
||||||
* server. All data are in network byte order.
|
* server. All data are in network byte order.
|
||||||
*/
|
*/
|
||||||
struct nbd_request {
|
struct nbd_request {
|
||||||
__be32 magic;
|
__be32 magic;
|
||||||
__be32 type; /* == READ || == WRITE */
|
__be32 type; /* == READ || == WRITE */
|
||||||
char handle[8];
|
char handle[8];
|
||||||
__be64 from;
|
__be64 from;
|
||||||
__be32 len;
|
__be32 len;
|
||||||
} __attribute__ ((packed));
|
} __attribute__ ((packed));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is the reply packet that nbd-server sends back to the client after
|
* 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).
|
* it has completed an I/O request (or an error occurs).
|
||||||
*/
|
*/
|
||||||
struct nbd_reply {
|
struct nbd_reply {
|
||||||
__be32 magic;
|
__be32 magic;
|
||||||
__be32 error; /* 0 = ok, else error */
|
__be32 error; /* 0 = ok, else error */
|
||||||
char handle[8]; /* handle you got from request */
|
char handle[8]; /* handle you got from request */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct buse_operations {
|
struct buse_operations {
|
||||||
int (*read)(void *buf, u_int32_t len, u_int64_t offset);
|
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);
|
int (*write)(const void *buf, u_int32_t len, u_int64_t offset, void *userdata);
|
||||||
void (*disc)();
|
void (*disc)(void *userdata);
|
||||||
int (*flush)();
|
int (*flush)(void *userdata);
|
||||||
int (*trim)(u_int64_t from, u_int32_t len);
|
int (*trim)(u_int64_t from, u_int32_t len, void *userdata);
|
||||||
|
|
||||||
u_int64_t size;
|
u_int64_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
int buse_main(int argc, char *argv[], const struct buse_operations *bop,
|
int buse_main(const char* dev_file, const struct buse_operations *bop, void *userdata);
|
||||||
void *userdata);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
12
busexmp.c
12
busexmp.c
|
@ -6,32 +6,32 @@
|
||||||
|
|
||||||
static void *data;
|
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);
|
fprintf(stderr, "R - %lu, %u\n", offset, len);
|
||||||
memcpy(buf, (char *)data + offset, len);
|
memcpy(buf, (char *)data + offset, len);
|
||||||
return 0;
|
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);
|
fprintf(stderr, "W - %lu, %u\n", offset, len);
|
||||||
memcpy((char *)data + offset, buf, len);
|
memcpy((char *)data + offset, buf, len);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void xmp_disc()
|
static void xmp_disc(void *userdata)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Received a disconnect request.\n");
|
fprintf(stderr, "Received a disconnect request.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int xmp_flush()
|
static int xmp_flush(void *userdata)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Received a flush request.\n");
|
fprintf(stderr, "Received a flush request.\n");
|
||||||
return 0;
|
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);
|
fprintf(stderr, "T - %lu, %u\n", from, len);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -50,5 +50,5 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
data = malloc(aop.size);
|
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");
|
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;
|
int bytes_read;
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ static int loopback_read(void *buf, u_int32_t len, u_int64_t offset)
|
||||||
return 0;
|
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;
|
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);
|
fprintf(stderr, "The size of this device is %ld bytes.\n", size);
|
||||||
bop.size = size;
|
bop.size = size;
|
||||||
|
|
||||||
buse_main(argc, argv, &bop, NULL);
|
buse_main(argv[1], &bop, NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue