initial trim support

This commit is contained in:
comatose 2012-12-01 00:42:29 +09:00
parent a1219f920f
commit 01919ef8a1
3 changed files with 155 additions and 125 deletions

18
buse.c
View File

@ -66,7 +66,8 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
int sp[2];
int nbd, sk, err, tmp_fd;
u_int64_t from;
u_int32_t len, bytes_read;
u_int32_t len;
ssize_t bytes_read;
char *dev_file;
struct nbd_request request;
struct nbd_reply reply;
@ -90,11 +91,18 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
close(sp[0]);
sk = sp[1];
assert(ioctl(nbd, NBD_SET_SOCK, sk) != -1);
if(ioctl(nbd, NBD_SET_SOCK, sk) == -1){
fprintf(stderr, "ioctl(nbd, NBD_SET_SOCK, sk) failed.[%s]\n", strerror(errno));
}
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));
}
else{
err = ioctl(nbd, NBD_DO_IT);
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_SOCK) != -1);
@ -116,8 +124,7 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
reply.magic = htonl(NBD_REPLY_MAGIC);
reply.error = htonl(0);
while (1) {
bytes_read = read(sk, &request, sizeof(request));
while ((bytes_read = read(sk, &request, sizeof(request))) > 0) {
assert(bytes_read == sizeof(request));
memcpy(reply.handle, request.handle, sizeof(reply.handle));
@ -161,6 +168,7 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
assert(0);
}
}
if (bytes_read == -1)
fprintf(stderr, "%s\n", strerror(errno));
return 0;
}

6
buse.h
View File

@ -26,6 +26,12 @@ enum {
NBD_CMD_TRIM = 4
};
/* values for flags field */
#define NBD_FLAG_HAS_FLAGS (1 << 0)
#define NBD_FLAG_READ_ONLY (1 << 1)
/* there is a gap here to match userspace */
#define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */
/* Magic numbers */
#define NBD_REQUEST_MAGIC 0x25609513
#define NBD_REPLY_MAGIC 0x67446698

View File

@ -9,12 +9,14 @@ static void *data;
static int xmp_read(void *buf, u_int32_t len, u_int64_t offset)
{
memcpy(buf, (char *)data + offset, len);
fprintf(stderr, "R %lu %u\n", offset, len);
return 0;
}
static int xmp_write(const void *buf, u_int32_t len, u_int64_t offset)
{
memcpy((char *)data + offset, buf, len);
fprintf(stderr, "W %lu %u\n", offset, len);
return 0;
}
@ -24,10 +26,24 @@ static int xmp_disc()
return 0;
}
static int xmp_flush()
{
fprintf(stderr, "Received a flush request.\n");
return 0;
}
static int xmp_trim(u_int64_t from, u_int32_t len)
{
fprintf(stderr, "T %lu %u\n", from, len);
return 0;
}
static struct buse_operations aop = {
.read = xmp_read,
.write = xmp_write,
.disc = xmp_disc,
.flush = xmp_flush,
.trim = xmp_trim,
.size = 128 * 1024 * 1024,
};