From e49ded9330fd8e02e2315e25f29a14de2fe4b66e Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Wed, 23 Nov 2011 14:34:43 -0800 Subject: [PATCH] Cleaned up the README. Copied some constants and struct definitions to buse.h so that the code doesn't rely on a hard-coded location of nbd.h. --- Makefile | 2 +- README | 16 +++++++--------- buse.c | 1 - buse.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 8eb3ffb..31b5966 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ OBJS := $(TARGET:=.o) $(LIBOBJS) STATIC_LIB := libbuse.a CC := /usr/bin/gcc -CFLAGS := -g -pedantic -Wall -Wextra -std=c99 -I$(HOME)/src/nbd +CFLAGS := -g -pedantic -Wall -Wextra -std=c99 LDFLAGS := -L. -lbuse .PHONY: all clean diff --git a/README b/README index c39171e..096e28d 100644 --- a/README +++ b/README @@ -1,14 +1,12 @@ -BUSE - A block device in user space +BUSE - A block device in userspace Adam Cozzette Fall 2011 -This piece of code was inspired by FUSE, which allows the development of Linux -file systems that run in user space. The goal of BUSE is to allow virtual block -devices to run in user space as well. - -Currently BUSE is a work in progress and the existing code is hacked together. -Soon I hope to finish implementing everything robustly. +This piece of software was inspired by FUSE, which allows the development of +Linux file systems that run in userspace. The goal of BUSE is to allow virtual +block devices to run in userspace as well. Currently BUSE is experimental and +should not be used for production code. Implementing a block device with BUSE is fairly straightforward. Simply fill struct buse_operations (declared in buse.h) with function pointers that define @@ -19,5 +17,5 @@ struct. busexmp.c is a simple example example that shows how this is done. The implementation of BUSE itself relies on NBD, the Linux network block device, which allows a remote machine to serve requests for reads and writes to a virtual block device on the local machine. BUSE sets up an NBD server and client -on the same machine, with the server running executing the code defined by the -BUSE user. +on the same machine, with the server executing the code defined by the BUSE +user. diff --git a/buse.c b/buse.c index f1552ab..db2dedb 100644 --- a/buse.c +++ b/buse.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/buse.h b/buse.h index 7c84848..a130d63 100644 --- a/buse.h +++ b/buse.h @@ -1,8 +1,57 @@ #ifndef BUSE_H_INCLUDED #define BUSE_H_INCLUDED +/* Most of this file was copied from nbd.h in the nbd distribution. */ + +#include #include +#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 +}; + +/* 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 */ +}; + 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);