Merge branch 'bugfixes'

This commit is contained in:
Pete Nelson 2013-06-27 17:07:56 -04:00
commit a612b72df8
4 changed files with 27 additions and 59 deletions

11
buse.c
View File

@ -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);
@ -134,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));
@ -143,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);
@ -151,16 +155,23 @@ 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;
#endif
default:
assert(0);
}

55
buse.h
View File

@ -8,60 +8,7 @@ extern "C" {
/* Most of this file was copied from nbd.h in the nbd distribution. */
#include <linux/types.h>
#include <sys/types.h>
#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 <linux/nbd.h>
struct buse_operations {
int (*read)(void *buf, u_int32_t len, u_int64_t offset, void *userdata);

View File

@ -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);
}

View File

@ -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) {
@ -78,7 +80,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;
}