Fixed a bug in buse.c where I was mistakenly relying on read and write syscalls
to complete an entire request in one call.
This commit is contained in:
parent
28eb460f25
commit
b6722defc5
50
buse.c
50
buse.c
|
@ -32,12 +32,42 @@ u_int64_t ntohll(u_int64_t a) {
|
||||||
#endif
|
#endif
|
||||||
#define htonll ntohll
|
#define htonll ntohll
|
||||||
|
|
||||||
|
static int read_all(int fd, void *buf, size_t count)
|
||||||
|
{
|
||||||
|
int bytes_read;
|
||||||
|
|
||||||
|
while (count > 0) {
|
||||||
|
bytes_read = read(fd, buf, count);
|
||||||
|
assert(bytes_read > 0);
|
||||||
|
buf = (char *)buf + bytes_read;
|
||||||
|
count -= bytes_read;
|
||||||
|
}
|
||||||
|
assert(count == 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int write_all(int fd, const void *buf, size_t count)
|
||||||
|
{
|
||||||
|
int bytes_written;
|
||||||
|
|
||||||
|
while (count > 0) {
|
||||||
|
bytes_written = write(fd, buf, count);
|
||||||
|
assert(bytes_written > 0);
|
||||||
|
buf = (char *)buf + bytes_written;
|
||||||
|
count -= bytes_written;
|
||||||
|
}
|
||||||
|
assert(count == 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *userdata)
|
int buse_main(int argc, char *argv[], 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, bytes_read, bytes_written;
|
u_int32_t len, bytes_read;
|
||||||
char *dev_file;
|
char *dev_file;
|
||||||
struct nbd_request request;
|
struct nbd_request request;
|
||||||
struct nbd_reply reply;
|
struct nbd_reply reply;
|
||||||
|
@ -45,8 +75,8 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
|
||||||
|
|
||||||
(void) userdata;
|
(void) userdata;
|
||||||
|
|
||||||
assert(argc == 2);
|
assert(argc == 3);
|
||||||
dev_file = argv[1];
|
dev_file = argv[2];
|
||||||
|
|
||||||
assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp));
|
assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp));
|
||||||
|
|
||||||
|
@ -64,6 +94,8 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
|
||||||
assert(ioctl(nbd, NBD_SET_SOCK, sk) != -1);
|
assert(ioctl(nbd, NBD_SET_SOCK, sk) != -1);
|
||||||
err = ioctl(nbd, NBD_DO_IT);
|
err = ioctl(nbd, NBD_DO_IT);
|
||||||
fprintf(stderr, "nbd device terminated with code %d\n", err);
|
fprintf(stderr, "nbd device terminated with code %d\n", err);
|
||||||
|
if (err == -1)
|
||||||
|
fprintf(stderr, "%s\n", strerror(errno));
|
||||||
|
|
||||||
assert(ioctl(nbd, NBD_CLEAR_QUE) != -1);
|
assert(ioctl(nbd, NBD_CLEAR_QUE) != -1);
|
||||||
assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1);
|
assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1);
|
||||||
|
@ -105,23 +137,21 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
|
||||||
chunk = malloc(len + sizeof(struct nbd_reply));
|
chunk = malloc(len + sizeof(struct nbd_reply));
|
||||||
aop->read((char *)chunk + sizeof(struct nbd_reply), len, from);
|
aop->read((char *)chunk + sizeof(struct nbd_reply), len, from);
|
||||||
memcpy(chunk, &reply, sizeof(struct nbd_reply));
|
memcpy(chunk, &reply, sizeof(struct nbd_reply));
|
||||||
bytes_written = write(sk, chunk, len + sizeof(struct nbd_reply));
|
write_all(sk, chunk, len + sizeof(struct nbd_reply));
|
||||||
assert(bytes_written == len + sizeof(struct nbd_reply));
|
|
||||||
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);
|
||||||
bytes_read = read(sk, chunk, len);
|
read_all(sk, chunk, len);
|
||||||
assert(bytes_read == len);
|
|
||||||
aop->write(chunk, len, from);
|
aop->write(chunk, len, from);
|
||||||
free(chunk);
|
free(chunk);
|
||||||
bytes_written = write(sk, &reply, sizeof(struct nbd_reply));
|
write_all(sk, &reply, sizeof(struct nbd_reply));
|
||||||
assert(bytes_written == sizeof(struct nbd_reply));
|
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_DISC:
|
case NBD_CMD_DISC:
|
||||||
|
/* Handle a disconnect request. */
|
||||||
aop->disc();
|
aop->disc();
|
||||||
break;
|
return 0;
|
||||||
case NBD_CMD_FLUSH:
|
case NBD_CMD_FLUSH:
|
||||||
aop->flush();
|
aop->flush();
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue