From: Roman Stratiienko <roman.stratiienko@xxxxxxxxxxxxxxx> Prepare base for the nbd-root patch: - allow to reuse sock_xmit without struct nbd_device as an argument. - allow to reuse nbd_add_socket with struct socket as an argument. Signed-off-by: Roman Stratiienko <roman.stratiienko@xxxxxxxxxxxxxxx> Reviewed-by: Aleksandr Bulyshchenko <A.Bulyshchenko@xxxxxxxxxxxxxxx> --- drivers/block/nbd.c | 62 +++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 3a9bca3aa093..63fcfb38e640 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -404,22 +404,13 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req, /* * Send or receive packet. */ -static int sock_xmit(struct nbd_device *nbd, int index, int send, +static int sock_xmit(struct socket *sock, int send, struct iov_iter *iter, int msg_flags, int *sent) { - struct nbd_config *config = nbd->config; - struct socket *sock = config->socks[index]->sock; int result; struct msghdr msg; unsigned int noreclaim_flag; - if (unlikely(!sock)) { - dev_err_ratelimited(disk_to_dev(nbd->disk), - "Attempted %s on closed socket in sock_xmit\n", - (send ? "send" : "recv")); - return -EINVAL; - } - msg.msg_iter = *iter; noreclaim_flag = memalloc_noreclaim_save(); @@ -450,6 +441,22 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send, return result; } +static int nbd_xmit(struct nbd_device *nbd, int index, int send, + struct iov_iter *iter, int msg_flags, int *sent) +{ + struct nbd_config *config = nbd->config; + struct socket *sock = config->socks[index]->sock; + + if (unlikely(!sock)) { + dev_err_ratelimited(disk_to_dev(nbd->disk), + "Attempted %s on closed socket in %s\n", + (send ? "send" : "recv"), __func__); + return -EINVAL; + } + + return sock_xmit(sock, send, iter, msg_flags, sent); +} + /* * Different settings for sk->sk_sndtimeo can result in different return values * if there is a signal pending when we enter sendmsg, because reasons? @@ -537,7 +544,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n", req, nbdcmd_to_ascii(type), (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); - result = sock_xmit(nbd, index, 1, &from, + result = nbd_xmit(nbd, index, 1, &from, (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent); trace_nbd_header_sent(req, handle); if (result <= 0) { @@ -583,7 +590,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) iov_iter_advance(&from, skip); skip = 0; } - result = sock_xmit(nbd, index, 1, &from, flags, &sent); + result = nbd_xmit(nbd, index, 1, &from, flags, &sent); if (result <= 0) { if (was_interrupted(result)) { /* We've already sent the header, we @@ -635,7 +642,7 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index) reply.magic = 0; iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply)); - result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); + result = nbd_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); if (result <= 0) { if (!nbd_disconnected(config)) dev_err(disk_to_dev(nbd->disk), @@ -690,7 +697,7 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index) rq_for_each_segment(bvec, req, iter) { iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len); - result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); + result = nbd_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); if (result <= 0) { dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n", result); @@ -931,18 +938,12 @@ static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx, return ret; } -static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, +static int nbd_add_socket(struct nbd_device *nbd, struct socket *sock, bool netlink) { struct nbd_config *config = nbd->config; - struct socket *sock; struct nbd_sock **socks; struct nbd_sock *nsock; - int err; - - sock = sockfd_lookup(arg, &err); - if (!sock) - return err; if (!netlink && !nbd->task_setup && !test_bit(NBD_BOUND, &config->runtime_flags)) @@ -984,6 +985,19 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, return 0; } +static int nbd_add_socket_fd(struct nbd_device *nbd, unsigned long arg, + bool netlink) +{ + struct socket *sock; + int err; + + sock = sockfd_lookup(arg, &err); + if (!sock) + return err; + + return nbd_add_socket(nbd, sock, netlink); +} + static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg) { struct nbd_config *config = nbd->config; @@ -1087,7 +1101,7 @@ static void send_disconnects(struct nbd_device *nbd) iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request)); mutex_lock(&nsock->tx_lock); - ret = sock_xmit(nbd, i, 1, &from, 0, NULL); + ret = nbd_xmit(nbd, i, 1, &from, 0, NULL); if (ret <= 0) dev_err(disk_to_dev(nbd->disk), "Send disconnect failed %d\n", ret); @@ -1249,7 +1263,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, nbd_clear_sock_ioctl(nbd, bdev); return 0; case NBD_SET_SOCK: - return nbd_add_socket(nbd, arg, false); + return nbd_add_socket_fd(nbd, arg, false); case NBD_SET_BLKSIZE: if (!arg || !is_power_of_2(arg) || arg < 512 || arg > PAGE_SIZE) @@ -1821,7 +1835,7 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) if (!socks[NBD_SOCK_FD]) continue; fd = (int)nla_get_u32(socks[NBD_SOCK_FD]); - ret = nbd_add_socket(nbd, fd, true); + ret = nbd_add_socket_fd(nbd, fd, true); if (ret) goto out; } -- 2.17.1