2013-06-28 16:50:13 +02:00
|
|
|
/*
|
|
|
|
* buse - block-device userspace extensions
|
|
|
|
* Copyright (C) 2013 Adam Cozzette
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2011-09-20 11:30:00 +02:00
|
|
|
#include <assert.h>
|
2011-11-15 23:44:38 +01:00
|
|
|
#include <errno.h>
|
2011-09-20 11:30:00 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/types.h>
|
2011-09-20 12:32:13 +02:00
|
|
|
#include <netinet/in.h>
|
2011-09-20 11:30:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-20 12:32:13 +02:00
|
|
|
#include <string.h>
|
2011-09-20 11:30:00 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-10-10 08:20:39 +02:00
|
|
|
#include "buse.h"
|
2011-09-20 11:30:00 +02:00
|
|
|
|
2011-09-27 13:56:36 +02:00
|
|
|
/*
|
|
|
|
* These helper functions were taken from cliserv.h in the nbd distribution.
|
|
|
|
*/
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
u_int64_t ntohll(u_int64_t a) {
|
2012-11-30 16:42:29 +01:00
|
|
|
return a;
|
2011-09-27 13:56:36 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
u_int64_t ntohll(u_int64_t a) {
|
2012-11-30 16:42:29 +01:00
|
|
|
u_int32_t lo = a & 0xffffffff;
|
|
|
|
u_int32_t hi = a >> 32U;
|
|
|
|
lo = ntohl(lo);
|
|
|
|
hi = ntohl(hi);
|
|
|
|
return ((u_int64_t) lo) << 32U | hi;
|
2011-09-27 13:56:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#define htonll ntohll
|
|
|
|
|
2012-12-01 17:08:13 +01:00
|
|
|
static int read_all(int fd, char* buf, size_t count)
|
2011-11-17 20:17:53 +01:00
|
|
|
{
|
2012-11-30 16:42:29 +01:00
|
|
|
int bytes_read;
|
2011-11-17 20:17:53 +01:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
while (count > 0) {
|
|
|
|
bytes_read = read(fd, buf, count);
|
|
|
|
assert(bytes_read > 0);
|
2012-12-01 17:08:13 +01:00
|
|
|
buf += bytes_read;
|
2012-11-30 16:42:29 +01:00
|
|
|
count -= bytes_read;
|
|
|
|
}
|
|
|
|
assert(count == 0);
|
2011-11-17 20:17:53 +01:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
return 0;
|
2011-11-17 20:17:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-01 17:08:13 +01:00
|
|
|
static int write_all(int fd, char* buf, size_t count)
|
2011-11-17 20:17:53 +01:00
|
|
|
{
|
2012-11-30 16:42:29 +01:00
|
|
|
int bytes_written;
|
2011-11-17 20:17:53 +01:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
while (count > 0) {
|
|
|
|
bytes_written = write(fd, buf, count);
|
|
|
|
assert(bytes_written > 0);
|
2012-12-01 17:08:13 +01:00
|
|
|
buf += bytes_written;
|
2012-11-30 16:42:29 +01:00
|
|
|
count -= bytes_written;
|
|
|
|
}
|
|
|
|
assert(count == 0);
|
2011-11-17 20:17:53 +01:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
return 0;
|
2011-11-17 20:17:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-07 12:58:26 +01:00
|
|
|
int buse_main(const char* dev_file, const struct buse_operations *aop, void *userdata)
|
2011-09-20 08:01:11 +02:00
|
|
|
{
|
2012-11-30 16:42:29 +01:00
|
|
|
int sp[2];
|
|
|
|
int nbd, sk, err, tmp_fd;
|
|
|
|
u_int64_t from;
|
2012-12-07 07:13:43 +01:00
|
|
|
u_int32_t len;
|
|
|
|
ssize_t bytes_read;
|
2012-11-30 16:42:29 +01:00
|
|
|
struct nbd_request request;
|
|
|
|
struct nbd_reply reply;
|
2012-12-03 00:29:18 +01:00
|
|
|
void *chunk;
|
2011-09-20 11:30:00 +02:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp));
|
2011-09-20 11:30:00 +02:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
nbd = open(dev_file, O_RDWR);
|
|
|
|
assert(nbd != -1);
|
2011-09-20 11:30:00 +02:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
assert(ioctl(nbd, NBD_SET_SIZE, aop->size) != -1);
|
|
|
|
assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1);
|
2011-09-20 11:30:00 +02:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
if (!fork()) {
|
|
|
|
/* The child needs to continue setting things up. */
|
|
|
|
close(sp[0]);
|
|
|
|
sk = sp[1];
|
2011-09-20 11:30:00 +02:00
|
|
|
|
2012-11-30 16:42:29 +01:00
|
|
|
if(ioctl(nbd, NBD_SET_SOCK, sk) == -1){
|
|
|
|
fprintf(stderr, "ioctl(nbd, NBD_SET_SOCK, sk) failed.[%s]\n", strerror(errno));
|
2011-09-20 11:30:00 +02:00
|
|
|
}
|
2013-06-27 19:27:10 +02:00
|
|
|
#if defined NBD_SET_FLAGS && defined NBD_FLAG_SEND_TRIM
|
2012-11-30 16:42:29 +01:00
|
|
|
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));
|
|
|
|
}
|
2013-06-27 19:27:10 +02:00
|
|
|
#endif
|
2012-11-30 16:42:29 +01:00
|
|
|
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));
|
2011-09-20 12:32:13 +02:00
|
|
|
}
|
|
|
|
|
2012-12-01 17:08:13 +01:00
|
|
|
ioctl(nbd, NBD_CLEAR_QUE);
|
|
|
|
ioctl(nbd, NBD_CLEAR_SOCK);
|
2012-11-30 16:42:29 +01:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The parent opens the device file at least once, to make sure the
|
|
|
|
* partition table is updated. Then it closes it and starts serving up
|
|
|
|
* requests. */
|
|
|
|
|
|
|
|
tmp_fd = open(dev_file, O_RDONLY);
|
|
|
|
assert(tmp_fd != -1);
|
|
|
|
close(tmp_fd);
|
|
|
|
|
|
|
|
close(sp[1]);
|
|
|
|
sk = sp[0];
|
|
|
|
|
|
|
|
reply.magic = htonl(NBD_REPLY_MAGIC);
|
|
|
|
reply.error = htonl(0);
|
|
|
|
|
|
|
|
while ((bytes_read = read(sk, &request, sizeof(request))) > 0) {
|
|
|
|
assert(bytes_read == sizeof(request));
|
|
|
|
memcpy(reply.handle, request.handle, sizeof(reply.handle));
|
|
|
|
|
|
|
|
len = ntohl(request.len);
|
|
|
|
from = ntohll(request.from);
|
|
|
|
assert(request.magic == htonl(NBD_REQUEST_MAGIC));
|
|
|
|
|
|
|
|
switch(ntohl(request.type)) {
|
|
|
|
/* I may at some point need to deal with the the fact that the
|
|
|
|
* official nbd server has a maximum buffer size, and divides up
|
|
|
|
* oversized requests into multiple pieces. This applies to reads
|
|
|
|
* and writes.
|
|
|
|
*/
|
|
|
|
case NBD_CMD_READ:
|
2012-12-07 12:58:26 +01:00
|
|
|
fprintf(stderr, "Request for read of size %d\n", len);
|
2013-06-27 22:51:07 +02:00
|
|
|
assert(aop->read);
|
2012-12-07 12:58:26 +01:00
|
|
|
chunk = malloc(len);
|
|
|
|
reply.error = aop->read(chunk, len, from, userdata);
|
2012-12-01 17:08:13 +01:00
|
|
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
|
|
|
if(reply.error == 0)
|
|
|
|
write_all(sk, (char*)chunk, len);
|
2012-11-30 16:42:29 +01:00
|
|
|
free(chunk);
|
|
|
|
break;
|
|
|
|
case NBD_CMD_WRITE:
|
2012-12-07 12:58:26 +01:00
|
|
|
fprintf(stderr, "Request for write of size %d\n", len);
|
2013-06-27 22:51:07 +02:00
|
|
|
assert(aop->write);
|
2012-11-30 16:42:29 +01:00
|
|
|
chunk = malloc(len);
|
|
|
|
read_all(sk, chunk, len);
|
2012-12-07 12:58:26 +01:00
|
|
|
reply.error = aop->write(chunk, len, from, userdata);
|
2012-11-30 16:42:29 +01:00
|
|
|
free(chunk);
|
2012-12-01 17:08:13 +01:00
|
|
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
2012-11-30 16:42:29 +01:00
|
|
|
break;
|
|
|
|
case NBD_CMD_DISC:
|
|
|
|
/* Handle a disconnect request. */
|
2013-06-27 22:51:07 +02:00
|
|
|
assert(aop->disc);
|
2012-12-07 12:58:26 +01:00
|
|
|
aop->disc(userdata);
|
2012-11-30 16:42:29 +01:00
|
|
|
return 0;
|
2013-06-27 19:27:10 +02:00
|
|
|
#if defined NBD_CMD_FLUSH
|
2012-11-30 16:42:29 +01:00
|
|
|
case NBD_CMD_FLUSH:
|
2013-06-27 22:51:07 +02:00
|
|
|
assert(aop->flush);
|
2012-12-07 12:58:26 +01:00
|
|
|
reply.error = aop->flush(userdata);
|
2012-12-01 17:08:13 +01:00
|
|
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
2012-11-30 16:42:29 +01:00
|
|
|
break;
|
2013-06-27 19:27:10 +02:00
|
|
|
#endif
|
|
|
|
#if defined NBD_CMD_TRIM
|
2012-11-30 16:42:29 +01:00
|
|
|
case NBD_CMD_TRIM:
|
2013-06-27 22:51:07 +02:00
|
|
|
assert(aop->trim);
|
2012-12-07 12:58:26 +01:00
|
|
|
reply.error = aop->trim(from, len, userdata);
|
2012-12-01 17:08:13 +01:00
|
|
|
write_all(sk, (char*)&reply, sizeof(struct nbd_reply));
|
2012-11-30 16:42:29 +01:00
|
|
|
break;
|
2013-06-27 19:27:10 +02:00
|
|
|
#endif
|
2012-11-30 16:42:29 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bytes_read == -1)
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
|
|
|
return 0;
|
2011-09-20 08:01:11 +02:00
|
|
|
}
|