Fix code style
parent
442a9d838d
commit
64c5c4ca26
|
@ -36,221 +36,210 @@
|
||||||
|
|
||||||
#include <linux/nbd-netlink.h>
|
#include <linux/nbd-netlink.h>
|
||||||
|
|
||||||
#define fail(...) \
|
#define fail(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
|
||||||
do { \
|
|
||||||
fprintf(stderr, __VA_ARGS__); \
|
|
||||||
exit(1); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
struct netlink_ctx {
|
struct netlink_ctx
|
||||||
struct nl_sock *sk;
|
{
|
||||||
int driver_id;
|
struct nl_sock *sk;
|
||||||
|
int driver_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void netlink_sock_alloc(struct netlink_ctx *ctx)
|
static void netlink_sock_alloc(struct netlink_ctx *ctx)
|
||||||
{
|
{
|
||||||
struct nl_sock *sk;
|
struct nl_sock *sk;
|
||||||
int nl_driver_id;
|
int nl_driver_id;
|
||||||
|
|
||||||
sk = nl_socket_alloc();
|
sk = nl_socket_alloc();
|
||||||
if (!sk)
|
if (!sk)
|
||||||
{
|
{
|
||||||
fail("Failed to alloc netlink socket\n");
|
fail("Failed to alloc netlink socket\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (genl_connect(sk))
|
if (genl_connect(sk))
|
||||||
{
|
{
|
||||||
nl_socket_free(sk);
|
nl_socket_free(sk);
|
||||||
fail("Couldn't connect to the generic netlink socket\n");
|
fail("Couldn't connect to the generic netlink socket\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
nl_driver_id = genl_ctrl_resolve(sk, "nbd");
|
nl_driver_id = genl_ctrl_resolve(sk, "nbd");
|
||||||
if (nl_driver_id < 0)
|
if (nl_driver_id < 0)
|
||||||
{
|
{
|
||||||
nl_socket_free(sk);
|
nl_socket_free(sk);
|
||||||
fail("Couldn't resolve the nbd netlink family\n");
|
fail("Couldn't resolve the nbd netlink family\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->driver_id = nl_driver_id;
|
ctx->driver_id = nl_driver_id;
|
||||||
ctx->sk = sk;
|
ctx->sk = sk;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void netlink_sock_free(struct netlink_ctx *ctx)
|
static void netlink_sock_free(struct netlink_ctx *ctx)
|
||||||
{
|
{
|
||||||
free(ctx->sk);
|
free(ctx->sk);
|
||||||
ctx->sk = NULL;
|
ctx->sk = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int netlink_status_cb(struct nl_msg *sk_msg, void *devnum)
|
static int netlink_status_cb(struct nl_msg *sk_msg, void *devnum)
|
||||||
{
|
{
|
||||||
struct nlmsghdr *nl_hdr;
|
struct nlmsghdr *nl_hdr;
|
||||||
struct genlmsghdr *gnl_hdr;
|
struct genlmsghdr *gnl_hdr;
|
||||||
struct nlattr *msg_attr[NBD_ATTR_MAX + 1];
|
struct nlattr *msg_attr[NBD_ATTR_MAX + 1];
|
||||||
struct nlattr *attr_data;
|
struct nlattr *attr_data;
|
||||||
int attr_len;
|
int attr_len;
|
||||||
uint32_t* dev_num;
|
uint32_t* dev_num;
|
||||||
|
|
||||||
dev_num = (uint32_t*)devnum;
|
dev_num = (uint32_t*)devnum;
|
||||||
|
|
||||||
nl_hdr = nlmsg_hdr(sk_msg);
|
nl_hdr = nlmsg_hdr(sk_msg);
|
||||||
gnl_hdr = (struct genlmsghdr *)nlmsg_data(nl_hdr);
|
gnl_hdr = (struct genlmsghdr *)nlmsg_data(nl_hdr);
|
||||||
attr_data = genlmsg_attrdata(gnl_hdr, 0);
|
attr_data = genlmsg_attrdata(gnl_hdr, 0);
|
||||||
attr_len = genlmsg_attrlen(gnl_hdr, 0);
|
attr_len = genlmsg_attrlen(gnl_hdr, 0);
|
||||||
|
|
||||||
if (nla_parse(msg_attr, NBD_ATTR_MAX, attr_data, attr_len, NULL))
|
if (nla_parse(msg_attr, NBD_ATTR_MAX, attr_data, attr_len, NULL))
|
||||||
{
|
|
||||||
fail("Failed to parse netlink response\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!msg_attr[NBD_ATTR_INDEX])
|
|
||||||
{
|
|
||||||
fail("Got malformed netlink reponse\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
*dev_num = nla_get_u32(msg_attr[NBD_ATTR_INDEX]);
|
|
||||||
|
|
||||||
return NL_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int netlink_configure(const int *sockfd, int sock_size,
|
|
||||||
int dev_num, uint64_t size,
|
|
||||||
uint64_t blocksize, uint64_t flags,
|
|
||||||
uint64_t cflags, uint64_t timeout,
|
|
||||||
uint64_t conn_timeout)
|
|
||||||
{
|
|
||||||
struct netlink_ctx ctx;
|
|
||||||
struct nlattr *msg_attr, *msg_opt_attr;
|
|
||||||
struct nl_msg *msg;
|
|
||||||
int i, err, sock;
|
|
||||||
uint32_t devnum;
|
|
||||||
|
|
||||||
netlink_sock_alloc(&ctx);
|
|
||||||
|
|
||||||
// A callback we set for a response we get on send
|
|
||||||
nl_socket_modify_cb(ctx.sk, NL_CB_VALID, NL_CB_CUSTOM, netlink_status_cb, &devnum);
|
|
||||||
|
|
||||||
msg = nlmsg_alloc();
|
|
||||||
if (!msg)
|
|
||||||
{
|
|
||||||
netlink_sock_free(&ctx);
|
|
||||||
fail("Failed to allocate netlking message\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, ctx.driver_id, 0, 0, NBD_CMD_CONNECT, 0);
|
|
||||||
|
|
||||||
if (dev_num >= 0)
|
|
||||||
{
|
|
||||||
NLA_PUT_U32(msg, NBD_ATTR_INDEX, (uint32_t)dev_num);
|
|
||||||
}
|
|
||||||
|
|
||||||
NLA_PUT_U64(msg, NBD_ATTR_SIZE_BYTES, size);
|
|
||||||
NLA_PUT_U64(msg, NBD_ATTR_BLOCK_SIZE_BYTES, blocksize);
|
|
||||||
NLA_PUT_U64(msg, NBD_ATTR_SERVER_FLAGS, flags);
|
|
||||||
NLA_PUT_U64(msg, NBD_ATTR_CLIENT_FLAGS, cflags);
|
|
||||||
|
|
||||||
if (timeout)
|
|
||||||
{
|
|
||||||
NLA_PUT_U64(msg, NBD_ATTR_TIMEOUT, timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conn_timeout)
|
|
||||||
{
|
|
||||||
NLA_PUT_U64(msg, NBD_ATTR_DEAD_CONN_TIMEOUT, conn_timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
msg_attr = nla_nest_start(msg, NBD_ATTR_SOCKETS);
|
|
||||||
if (!msg_attr)
|
|
||||||
{
|
|
||||||
goto nla_put_failure;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < sock_size; i++)
|
|
||||||
{
|
|
||||||
msg_opt_attr = nla_nest_start(msg, NBD_SOCK_ITEM);
|
|
||||||
if (!msg_opt_attr)
|
|
||||||
{
|
{
|
||||||
goto nla_put_failure;
|
fail("Failed to parse netlink response\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
sock = sockfd[i];
|
if (!msg_attr[NBD_ATTR_INDEX])
|
||||||
NLA_PUT_U32(msg, NBD_SOCK_FD, sock);
|
{
|
||||||
|
fail("Got malformed netlink reponse\n");
|
||||||
|
}
|
||||||
|
|
||||||
nla_nest_end(msg, msg_opt_attr);
|
*dev_num = nla_get_u32(msg_attr[NBD_ATTR_INDEX]);
|
||||||
}
|
|
||||||
|
|
||||||
nla_nest_end(msg, msg_attr);
|
return NL_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int netlink_configure(const int *sockfd, int sock_size, int dev_num, uint64_t size,
|
||||||
|
uint64_t blocksize, uint64_t flags, uint64_t cflags, uint64_t timeout, uint64_t conn_timeout)
|
||||||
|
{
|
||||||
|
struct netlink_ctx ctx;
|
||||||
|
struct nlattr *msg_attr, *msg_opt_attr;
|
||||||
|
struct nl_msg *msg;
|
||||||
|
int i, err, sock;
|
||||||
|
uint32_t devnum;
|
||||||
|
|
||||||
|
netlink_sock_alloc(&ctx);
|
||||||
|
|
||||||
|
// A callback we set for a response we get on send
|
||||||
|
nl_socket_modify_cb(ctx.sk, NL_CB_VALID, NL_CB_CUSTOM, netlink_status_cb, &devnum);
|
||||||
|
|
||||||
|
msg = nlmsg_alloc();
|
||||||
|
if (!msg)
|
||||||
|
{
|
||||||
|
netlink_sock_free(&ctx);
|
||||||
|
fail("Failed to allocate netlink message\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, ctx.driver_id, 0, 0, NBD_CMD_CONNECT, 0);
|
||||||
|
|
||||||
|
if (dev_num >= 0)
|
||||||
|
{
|
||||||
|
NLA_PUT_U32(msg, NBD_ATTR_INDEX, (uint32_t)dev_num);
|
||||||
|
}
|
||||||
|
|
||||||
|
NLA_PUT_U64(msg, NBD_ATTR_SIZE_BYTES, size);
|
||||||
|
NLA_PUT_U64(msg, NBD_ATTR_BLOCK_SIZE_BYTES, blocksize);
|
||||||
|
NLA_PUT_U64(msg, NBD_ATTR_SERVER_FLAGS, flags);
|
||||||
|
NLA_PUT_U64(msg, NBD_ATTR_CLIENT_FLAGS, cflags);
|
||||||
|
|
||||||
|
if (timeout)
|
||||||
|
{
|
||||||
|
NLA_PUT_U64(msg, NBD_ATTR_TIMEOUT, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conn_timeout)
|
||||||
|
{
|
||||||
|
NLA_PUT_U64(msg, NBD_ATTR_DEAD_CONN_TIMEOUT, conn_timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
msg_attr = nla_nest_start(msg, NBD_ATTR_SOCKETS);
|
||||||
|
if (!msg_attr)
|
||||||
|
{
|
||||||
|
goto nla_put_failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < sock_size; i++)
|
||||||
|
{
|
||||||
|
msg_opt_attr = nla_nest_start(msg, NBD_SOCK_ITEM);
|
||||||
|
if (!msg_opt_attr)
|
||||||
|
{
|
||||||
|
goto nla_put_failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
sock = sockfd[i];
|
||||||
|
NLA_PUT_U32(msg, NBD_SOCK_FD, sock);
|
||||||
|
|
||||||
|
nla_nest_end(msg, msg_opt_attr);
|
||||||
|
}
|
||||||
|
|
||||||
|
nla_nest_end(msg, msg_attr);
|
||||||
|
|
||||||
|
if ((err = nl_send_sync(ctx.sk, msg)) < 0)
|
||||||
|
{
|
||||||
|
netlink_sock_free(&ctx);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
if((err = nl_send_sync(ctx.sk, msg)) < 0)
|
|
||||||
{
|
|
||||||
netlink_sock_free(&ctx);
|
netlink_sock_free(&ctx);
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
netlink_sock_free(&ctx);
|
return devnum;
|
||||||
|
|
||||||
return devnum;
|
|
||||||
|
|
||||||
nla_put_failure:
|
nla_put_failure:
|
||||||
nlmsg_free(msg);
|
nlmsg_free(msg);
|
||||||
netlink_sock_free(&ctx);
|
netlink_sock_free(&ctx);
|
||||||
fail("Failed to create netlink message\n");
|
fail("Failed to create netlink message\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void netlink_disconnect(uint32_t dev_num)
|
static void netlink_disconnect(uint32_t dev_num)
|
||||||
{
|
{
|
||||||
struct netlink_ctx ctx;
|
struct netlink_ctx ctx;
|
||||||
struct nl_msg *msg;
|
struct nl_msg *msg;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
netlink_sock_alloc(&ctx);
|
netlink_sock_alloc(&ctx);
|
||||||
|
|
||||||
|
msg = nlmsg_alloc();
|
||||||
|
if (!msg)
|
||||||
|
{
|
||||||
|
netlink_sock_free(&ctx);
|
||||||
|
fail("Failed to allocate netlink message\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, ctx.driver_id, 0, 0, NBD_CMD_DISCONNECT, 0);
|
||||||
|
NLA_PUT_U32(msg, NBD_ATTR_INDEX, dev_num);
|
||||||
|
|
||||||
|
if ((err = nl_send_sync(ctx.sk, msg)) < 0)
|
||||||
|
{
|
||||||
|
netlink_sock_free(&ctx);
|
||||||
|
fail("Failed to send netlink message %d\n", err);
|
||||||
|
}
|
||||||
|
|
||||||
msg = nlmsg_alloc();
|
|
||||||
if (!msg)
|
|
||||||
{
|
|
||||||
netlink_sock_free(&ctx);
|
netlink_sock_free(&ctx);
|
||||||
fail("Failed to allocate netlking message\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, ctx.driver_id, 0, 0, NBD_CMD_DISCONNECT, 0);
|
return;
|
||||||
NLA_PUT_U32(msg, NBD_ATTR_INDEX, dev_num);
|
|
||||||
|
|
||||||
if ((err = nl_send_sync(ctx.sk, msg)) < 0)
|
|
||||||
{
|
|
||||||
netlink_sock_free(&ctx);
|
|
||||||
fail("Failed to send netlink message %d\n", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
netlink_sock_free(&ctx);
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
nla_put_failure:
|
nla_put_failure:
|
||||||
nlmsg_free(msg);
|
nlmsg_free(msg);
|
||||||
netlink_sock_free(&ctx);
|
netlink_sock_free(&ctx);
|
||||||
fail("Failed to create netlink message\n");
|
fail("Failed to create netlink message\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef fail
|
#undef fail
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
static int netlink_configure(const int *sockfd, int sock_size,
|
static int netlink_configure(const int *sockfd, int sock_size, int dev_num, uint64_t size,
|
||||||
int dev_num, uint64_t size,
|
uint64_t blocksize, uint64_t flags, uint64_t cflags, uint64_t timeout, uint64_t conn_timeout)
|
||||||
uint64_t blocksize, uint64_t flags,
|
|
||||||
uint64_t cflags, uint64_t timeout,
|
|
||||||
uint64_t conn_timeout)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, "netlink is not supported\n");
|
fprintf(stderr, "netlink is not supported\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void netlink_disconnect(uint32_t dev_num)
|
static void netlink_disconnect(uint32_t dev_num)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "netlink is not supported\n");
|
fprintf(stderr, "netlink is not supported\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -396,7 +385,7 @@ public:
|
||||||
" is returned after no I/O is performed on device.\n"
|
" is returned after no I/O is performed on device.\n"
|
||||||
" By default is not set.\n"
|
" By default is not set.\n"
|
||||||
" --nbd_destroy_on_disconnect 1\n"
|
" --nbd_destroy_on_disconnect 1\n"
|
||||||
" Delete the nbd device on disconnect\n."
|
" Delete the nbd device on disconnect.\n"
|
||||||
" --nbd_disconnect_on_close 1\n"
|
" --nbd_disconnect_on_close 1\n"
|
||||||
" Disconnect the nbd device on close by last opener.\n"
|
" Disconnect the nbd device on close by last opener.\n"
|
||||||
" --nbd_ro 1\n"
|
" --nbd_ro 1\n"
|
||||||
|
@ -560,13 +549,12 @@ public:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int err = netlink_configure(sockfd + 1, 1, devnum, device_size, 4096, flags, cflags, nbd_timeout, nbd_lease);
|
int err = netlink_configure(sockfd + 1, 1, devnum, device_size, 4096, flags, cflags, nbd_timeout, nbd_lease);
|
||||||
if (err < 0) {
|
if (err < 0)
|
||||||
if (err == -NLE_BUSY) {
|
{
|
||||||
|
if (err == -NLE_BUSY)
|
||||||
errno = EBUSY;
|
errno = EBUSY;
|
||||||
} else {
|
else
|
||||||
errno = EIO;
|
errno = EIO;
|
||||||
}
|
|
||||||
|
|
||||||
perror("netlink_configure");
|
perror("netlink_configure");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue