blob: d3be8ea2f80dc4dcb177134e2fe9ae6ab5a8a77b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Network block device - make block devices work over TCP
3 *
4 * Note that you can not swap over this thing, yet. Seems to work but
5 * deadlocks sometimes - you can not swap over TCP in general.
6 *
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
9 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070010 * This file is released under GPLv2 or later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070012 * (part of code stolen from loop.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/major.h>
16
17#include <linux/blkdev.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/sched.h>
21#include <linux/fs.h>
22#include <linux/bio.h>
23#include <linux/stat.h>
24#include <linux/errno.h>
25#include <linux/file.h>
26#include <linux/ioctl.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020027#include <linux/mutex.h>
Herbert Xu4b2f0262006-01-06 00:09:47 -080028#include <linux/compiler.h>
29#include <linux/err.h>
30#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/sock.h>
Trond Myklebust91cf45f2007-11-12 18:10:39 -080033#include <linux/net.h>
Laurent Vivier48cf6062008-04-29 01:02:46 -070034#include <linux/kthread.h>
Markus Pargmannb9c495b2015-04-02 10:11:37 +020035#include <linux/types.h>
Markus Pargmann30d53d92015-08-17 08:20:06 +020036#include <linux/debugfs.h>
Josef Bacikfd8383f2016-09-08 12:33:37 -070037#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/types.h>
41
42#include <linux/nbd.h>
43
Josef Bacikb0d91112017-02-01 16:11:40 -050044static DEFINE_IDR(nbd_index_idr);
45static DEFINE_MUTEX(nbd_index_mutex);
46
Josef Bacik9561a7a2016-11-22 14:04:40 -050047struct nbd_sock {
48 struct socket *sock;
49 struct mutex tx_lock;
50};
51
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070052#define NBD_TIMEDOUT 0
53#define NBD_DISCONNECT_REQUESTED 1
Josef Bacik9561a7a2016-11-22 14:04:40 -050054#define NBD_DISCONNECTED 2
55#define NBD_RUNNING 3
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070056
Markus Pargmann13e71d62015-04-02 10:11:35 +020057struct nbd_device {
Markus Pargmann22d109c2015-08-17 08:20:09 +020058 u32 flags;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070059 unsigned long runtime_flags;
Josef Bacik9561a7a2016-11-22 14:04:40 -050060 struct nbd_sock **socks;
Markus Pargmann13e71d62015-04-02 10:11:35 +020061 int magic;
62
Josef Bacikfd8383f2016-09-08 12:33:37 -070063 struct blk_mq_tag_set tag_set;
Markus Pargmann13e71d62015-04-02 10:11:35 +020064
Josef Bacik9561a7a2016-11-22 14:04:40 -050065 struct mutex config_lock;
Markus Pargmann13e71d62015-04-02 10:11:35 +020066 struct gendisk *disk;
Josef Bacik9561a7a2016-11-22 14:04:40 -050067 int num_connections;
68 atomic_t recv_threads;
69 wait_queue_head_t recv_wq;
Josef Bacikef77b512016-12-02 16:19:12 -050070 loff_t blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020071 loff_t bytesize;
Markus Pargmann7e2893a2015-08-17 08:20:00 +020072
Markus Pargmann7e2893a2015-08-17 08:20:00 +020073 struct task_struct *task_recv;
Josef Bacik9561a7a2016-11-22 14:04:40 -050074 struct task_struct *task_setup;
Markus Pargmann30d53d92015-08-17 08:20:06 +020075
76#if IS_ENABLED(CONFIG_DEBUG_FS)
77 struct dentry *dbg_dir;
78#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +020079};
80
Josef Bacikfd8383f2016-09-08 12:33:37 -070081struct nbd_cmd {
82 struct nbd_device *nbd;
Josef Bacik9561a7a2016-11-22 14:04:40 -050083 struct completion send_complete;
Josef Bacikfd8383f2016-09-08 12:33:37 -070084};
85
Markus Pargmann30d53d92015-08-17 08:20:06 +020086#if IS_ENABLED(CONFIG_DEBUG_FS)
87static struct dentry *nbd_dbg_dir;
88#endif
89
90#define nbd_name(nbd) ((nbd)->disk->disk_name)
91
Wanlong Gaof4507162012-03-28 14:42:51 -070092#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Ingo van Lil9c7a4162006-07-01 04:36:36 -070094static unsigned int nbds_max = 16;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070095static int max_part;
Josef Bacik124d6db2017-02-01 16:11:11 -050096static struct workqueue_struct *recv_workqueue;
Josef Bacikb0d91112017-02-01 16:11:40 -050097static int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Josef Bacik9442b732017-02-07 17:10:22 -050099static int nbd_dev_dbg_init(struct nbd_device *nbd);
100static void nbd_dev_dbg_close(struct nbd_device *nbd);
101
102
Markus Pargmannd18509f2015-04-02 10:11:38 +0200103static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Markus Pargmannd18509f2015-04-02 10:11:38 +0200105 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Markus Pargmann37091fd2015-07-27 07:36:49 +0200108static bool nbd_is_connected(struct nbd_device *nbd)
109{
110 return !!nbd->task_recv;
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static const char *nbdcmd_to_ascii(int cmd)
114{
115 switch (cmd) {
116 case NBD_CMD_READ: return "read";
117 case NBD_CMD_WRITE: return "write";
118 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800119 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700120 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 return "invalid";
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Markus Pargmann37091fd2015-07-27 07:36:49 +0200125static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
126{
Josef Bacike5445412017-02-13 10:39:47 -0500127 bd_set_size(bdev, 0);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200128 set_capacity(nbd->disk, 0);
129 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
130
131 return 0;
132}
133
134static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev)
135{
Josef Bacike5445412017-02-13 10:39:47 -0500136 blk_queue_logical_block_size(nbd->disk->queue, nbd->blksize);
137 blk_queue_physical_block_size(nbd->disk->queue, nbd->blksize);
138 bd_set_size(bdev, nbd->bytesize);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200139 set_capacity(nbd->disk, nbd->bytesize >> 9);
140 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
141}
142
Josef Bacike5445412017-02-13 10:39:47 -0500143static void nbd_size_set(struct nbd_device *nbd, struct block_device *bdev,
Josef Bacikef77b512016-12-02 16:19:12 -0500144 loff_t blocksize, loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200145{
Markus Pargmann37091fd2015-07-27 07:36:49 +0200146 nbd->blksize = blocksize;
Josef Bacikef77b512016-12-02 16:19:12 -0500147 nbd->bytesize = blocksize * nr_blocks;
Josef Bacike5445412017-02-13 10:39:47 -0500148 if (nbd_is_connected(nbd))
149 nbd_size_update(nbd, bdev);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200150}
151
Josef Bacikfd8383f2016-09-08 12:33:37 -0700152static void nbd_end_request(struct nbd_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700154 struct nbd_device *nbd = cmd->nbd;
155 struct request *req = blk_mq_rq_from_pdu(cmd);
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500156 int error = req->errors ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Josef Bacikfd8383f2016-09-08 12:33:37 -0700158 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", cmd,
Markus Pargmannd18509f2015-04-02 10:11:38 +0200159 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Josef Bacikfd8383f2016-09-08 12:33:37 -0700161 blk_mq_complete_request(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Markus Pargmanne018e752015-04-02 10:11:39 +0200164/*
165 * Forcibly shutdown the socket causing all listeners to error
166 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200167static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700168{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500169 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700170
Josef Bacik9561a7a2016-11-22 14:04:40 -0500171 if (nbd->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200172 return;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500173 if (test_and_set_bit(NBD_DISCONNECTED, &nbd->runtime_flags))
174 return;
175
176 for (i = 0; i < nbd->num_connections; i++) {
177 struct nbd_sock *nsock = nbd->socks[i];
178 mutex_lock(&nsock->tx_lock);
179 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
180 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100181 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500182 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700183}
184
Josef Bacik0eadf372016-09-08 12:33:40 -0700185static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
186 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700187{
Josef Bacik0eadf372016-09-08 12:33:40 -0700188 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
189 struct nbd_device *nbd = cmd->nbd;
Paul Clements7fdfd402007-10-16 23:27:37 -0700190
Markus Pargmann23272a672015-10-29 11:51:16 +0100191 dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -0500192 set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
193 req->errors++;
194
Josef Bacik9561a7a2016-11-22 14:04:40 -0500195 mutex_lock(&nbd->config_lock);
196 sock_shutdown(nbd);
197 mutex_unlock(&nbd->config_lock);
Josef Bacik0eadf372016-09-08 12:33:40 -0700198 return BLK_EH_HANDLED;
Paul Clements7fdfd402007-10-16 23:27:37 -0700199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * Send or receive packet.
203 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500204static int sock_xmit(struct nbd_device *nbd, int index, int send,
205 struct iov_iter *iter, int msg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500207 struct socket *sock = nbd->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int result;
209 struct msghdr msg;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700210 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700212 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500213 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200214 "Attempted %s on closed socket in sock_xmit\n",
215 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700216 return -EINVAL;
217 }
218
Al Viroc9f2b6a2015-11-12 05:09:35 -0500219 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500220
Mel Gorman7f338fe2012-07-31 16:44:32 -0700221 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700223 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 msg.msg_name = NULL;
225 msg.msg_namelen = 0;
226 msg.msg_control = NULL;
227 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
229
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200230 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500231 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200232 else
Al Viroc1696ca2015-11-12 04:51:19 -0500233 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (result <= 0) {
236 if (result == 0)
237 result = -EPIPE; /* short read */
238 break;
239 }
Al Viroc1696ca2015-11-12 04:51:19 -0500240 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Mel Gorman7f338fe2012-07-31 16:44:32 -0700242 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 return result;
245}
246
Paul Clements7fdfd402007-10-16 23:27:37 -0700247/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500248static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700250 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500251 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500252 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
253 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
254 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900255 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700256 struct bio *bio;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200257 u32 type;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500258 u32 tag = blk_mq_unique_tag(req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200259
Al Viroc9f2b6a2015-11-12 05:09:35 -0500260 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
261
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100262 switch (req_op(req)) {
263 case REQ_OP_DISCARD:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200264 type = NBD_CMD_TRIM;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100265 break;
266 case REQ_OP_FLUSH:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200267 type = NBD_CMD_FLUSH;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100268 break;
269 case REQ_OP_WRITE:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200270 type = NBD_CMD_WRITE;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100271 break;
272 case REQ_OP_READ:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200273 type = NBD_CMD_READ;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100274 break;
275 default:
276 return -EIO;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100279 if (rq_data_dir(req) == WRITE &&
280 (nbd->flags & NBD_FLAG_READ_ONLY)) {
281 dev_err_ratelimited(disk_to_dev(nbd->disk),
282 "Write on read-only\n");
283 return -EIO;
284 }
285
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200286 request.type = htonl(type);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500287 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800288 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
289 request.len = htonl(size);
290 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500291 memcpy(request.handle, &tag, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Markus Pargmannd18509f2015-04-02 10:11:38 +0200293 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700294 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200295 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500296 result = sock_xmit(nbd, index, 1, &from,
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200297 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (result <= 0) {
Josef Bacika897b662016-12-05 16:20:29 -0500299 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200300 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200301 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
Jens Axboe429a7872016-11-17 12:30:37 -0700304 if (type != NBD_CMD_WRITE)
305 return 0;
306
Jens Axboe429a7872016-11-17 12:30:37 -0700307 bio = req->bio;
308 while (bio) {
309 struct bio *next = bio->bi_next;
310 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800311 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700312
313 bio_for_each_segment(bvec, bio, iter) {
314 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500315 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700316
Markus Pargmannd18509f2015-04-02 10:11:38 +0200317 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700318 cmd, bvec.bv_len);
Al Viroc9f2b6a2015-11-12 05:09:35 -0500319 iov_iter_bvec(&from, ITER_BVEC | WRITE,
320 &bvec, 1, bvec.bv_len);
321 result = sock_xmit(nbd, index, 1, &from, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200322 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700323 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200324 "Send data failed (result %d)\n",
325 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200326 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200327 }
Jens Axboe429a7872016-11-17 12:30:37 -0700328 /*
329 * The completion might already have come in,
330 * so break for the last one instead of letting
331 * the iterator do it. This prevents use-after-free
332 * of the bio.
333 */
334 if (is_last)
335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Jens Axboe429a7872016-11-17 12:30:37 -0700337 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500343static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 int result;
346 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700347 struct nbd_cmd *cmd;
348 struct request *req = NULL;
349 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500350 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500351 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
352 struct iov_iter to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 reply.magic = 0;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500355 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
356 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (result <= 0) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500358 if (!test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) &&
359 !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
360 dev_err(disk_to_dev(nbd->disk),
361 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200362 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700364
365 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700366 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700367 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200368 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700369 }
370
Josef Bacik9561a7a2016-11-22 14:04:40 -0500371 memcpy(&tag, reply.handle, sizeof(u32));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800372
Josef Bacikfd8383f2016-09-08 12:33:37 -0700373 hwq = blk_mq_unique_tag_to_hwq(tag);
374 if (hwq < nbd->tag_set.nr_hw_queues)
375 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
376 blk_mq_unique_tag_to_tag(tag));
377 if (!req || !blk_mq_request_started(req)) {
378 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
379 tag, req);
380 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700382 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700384 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200385 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700387 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Josef Bacikfd8383f2016-09-08 12:33:37 -0700390 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200391 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200392 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800393 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200394
395 rq_for_each_segment(bvec, req, iter) {
Al Viroc9f2b6a2015-11-12 05:09:35 -0500396 iov_iter_bvec(&to, ITER_BVEC | READ,
397 &bvec, 1, bvec.bv_len);
398 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200399 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700400 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200401 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200402 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700403 return cmd;
Jens Axboe6c92e692007-08-16 13:43:12 +0200404 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200405 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700406 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500408 } else {
409 /* See the comment in nbd_queue_rq. */
410 wait_for_completion(&cmd->send_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700412 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200415static ssize_t pid_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800417{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200418 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200419 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200420
Markus Pargmann6521d392015-08-17 08:20:05 +0200421 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800422}
423
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200424static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700425 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800426 .show = pid_show,
427};
428
Josef Bacik9561a7a2016-11-22 14:04:40 -0500429struct recv_thread_args {
430 struct work_struct work;
431 struct nbd_device *nbd;
432 int index;
433};
434
435static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500437 struct recv_thread_args *args = container_of(work,
438 struct recv_thread_args,
439 work);
440 struct nbd_device *nbd = args->nbd;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700441 struct nbd_cmd *cmd;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500442 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Wanlong Gaof4507162012-03-28 14:42:51 -0700444 BUG_ON(nbd->magic != NBD_MAGIC);
Markus Pargmann19391832015-08-17 08:20:03 +0200445 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500446 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700447 if (IS_ERR(cmd)) {
448 ret = PTR_ERR(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200449 break;
450 }
451
Josef Bacikfd8383f2016-09-08 12:33:37 -0700452 nbd_end_request(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200453 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800454
Josef Bacik9561a7a2016-11-22 14:04:40 -0500455 /*
456 * We got an error, shut everybody down if this wasn't the result of a
457 * disconnect request.
458 */
459 if (ret && !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
460 sock_shutdown(nbd);
461 atomic_dec(&nbd->recv_threads);
462 wake_up(&nbd->recv_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Josef Bacikfd8383f2016-09-08 12:33:37 -0700465static void nbd_clear_req(struct request *req, void *data, bool reserved)
466{
467 struct nbd_cmd *cmd;
468
469 if (!blk_mq_request_started(req))
470 return;
471 cmd = blk_mq_rq_to_pdu(req);
472 req->errors++;
473 nbd_end_request(cmd);
474}
475
Wanlong Gaof4507162012-03-28 14:42:51 -0700476static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Wanlong Gaof4507162012-03-28 14:42:51 -0700478 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Josef Bacikfd8383f2016-09-08 12:33:37 -0700480 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200481 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Paul Clements7fdfd402007-10-16 23:27:37 -0700484
Josef Bacik9561a7a2016-11-22 14:04:40 -0500485static void nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700486{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700487 struct request *req = blk_mq_rq_from_pdu(cmd);
488 struct nbd_device *nbd = cmd->nbd;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500489 struct nbd_sock *nsock;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700490
Josef Bacik9561a7a2016-11-22 14:04:40 -0500491 if (index >= nbd->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500492 dev_err_ratelimited(disk_to_dev(nbd->disk),
493 "Attempted send on invalid socket\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -0500494 goto error_out;
495 }
496
497 if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) {
Josef Bacika897b662016-12-05 16:20:29 -0500498 dev_err_ratelimited(disk_to_dev(nbd->disk),
499 "Attempted send on closed socket\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -0500500 goto error_out;
501 }
502
Laurent Vivier48cf6062008-04-29 01:02:46 -0700503 req->errors = 0;
504
Josef Bacik9561a7a2016-11-22 14:04:40 -0500505 nsock = nbd->socks[index];
506 mutex_lock(&nsock->tx_lock);
507 if (unlikely(!nsock->sock)) {
508 mutex_unlock(&nsock->tx_lock);
Josef Bacika897b662016-12-05 16:20:29 -0500509 dev_err_ratelimited(disk_to_dev(nbd->disk),
510 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700511 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700512 }
513
Josef Bacik9561a7a2016-11-22 14:04:40 -0500514 if (nbd_send_cmd(nbd, cmd, index) != 0) {
Josef Bacika897b662016-12-05 16:20:29 -0500515 dev_err_ratelimited(disk_to_dev(nbd->disk),
516 "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700517 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700518 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700519 }
520
Josef Bacik9561a7a2016-11-22 14:04:40 -0500521 mutex_unlock(&nsock->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700522
523 return;
524
525error_out:
526 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700527 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700528}
529
Josef Bacikfd8383f2016-09-08 12:33:37 -0700530static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
531 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700532{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700533 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700534
Josef Bacik9561a7a2016-11-22 14:04:40 -0500535 /*
536 * Since we look at the bio's to send the request over the network we
537 * need to make sure the completion work doesn't mark this request done
538 * before we are done doing our send. This keeps us from dereferencing
539 * freed data if we have particularly fast completions (ie we get the
540 * completion before we exit sock_xmit on the last bvec) or in the case
541 * that the server is misbehaving (or there was an error) before we're
542 * done sending everything over the wire.
543 */
544 init_completion(&cmd->send_complete);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700545 blk_mq_start_request(bd->rq);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500546 nbd_handle_cmd(cmd, hctx->queue_num);
547 complete(&cmd->send_complete);
548
Josef Bacikfd8383f2016-09-08 12:33:37 -0700549 return BLK_MQ_RQ_QUEUE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Josef Bacik9442b732017-02-07 17:10:22 -0500552static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
553 unsigned long arg)
Markus Pargmann23272a672015-10-29 11:51:16 +0100554{
Josef Bacik9442b732017-02-07 17:10:22 -0500555 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500556 struct nbd_sock **socks;
557 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -0500558 int err;
559
560 sock = sockfd_lookup(arg, &err);
561 if (!sock)
562 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100563
Josef Bacik9561a7a2016-11-22 14:04:40 -0500564 if (!nbd->task_setup)
565 nbd->task_setup = current;
566 if (nbd->task_setup != current) {
567 dev_err(disk_to_dev(nbd->disk),
568 "Device being setup by another task");
569 return -EINVAL;
Markus Pargmann23272a672015-10-29 11:51:16 +0100570 }
571
Josef Bacik9561a7a2016-11-22 14:04:40 -0500572 socks = krealloc(nbd->socks, (nbd->num_connections + 1) *
573 sizeof(struct nbd_sock *), GFP_KERNEL);
574 if (!socks)
575 return -ENOMEM;
576 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
577 if (!nsock)
578 return -ENOMEM;
Markus Pargmann23272a672015-10-29 11:51:16 +0100579
Josef Bacik9561a7a2016-11-22 14:04:40 -0500580 nbd->socks = socks;
Markus Pargmann23272a672015-10-29 11:51:16 +0100581
Josef Bacik9561a7a2016-11-22 14:04:40 -0500582 mutex_init(&nsock->tx_lock);
583 nsock->sock = sock;
584 socks[nbd->num_connections++] = nsock;
585
Josef Bacik9442b732017-02-07 17:10:22 -0500586 if (max_part)
587 bdev->bd_invalidated = 1;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500588 return 0;
Markus Pargmann23272a672015-10-29 11:51:16 +0100589}
590
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100591/* Reset all properties of an NBD device */
592static void nbd_reset(struct nbd_device *nbd)
593{
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700594 nbd->runtime_flags = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100595 nbd->blksize = 1024;
596 nbd->bytesize = 0;
597 set_capacity(nbd->disk, 0);
598 nbd->flags = 0;
Josef Bacik0eadf372016-09-08 12:33:40 -0700599 nbd->tag_set.timeout = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100600 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100601}
602
603static void nbd_bdev_reset(struct block_device *bdev)
604{
605 set_device_ro(bdev, false);
606 bdev->bd_inode->i_size = 0;
607 if (max_part > 0) {
608 blkdev_reread_part(bdev);
609 bdev->bd_invalidated = 1;
610 }
611}
612
Markus Pargmannd02cf532015-10-29 12:06:15 +0100613static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
614{
615 if (nbd->flags & NBD_FLAG_READ_ONLY)
616 set_device_ro(bdev, true);
617 if (nbd->flags & NBD_FLAG_SEND_TRIM)
618 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
619 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600620 blk_queue_write_cache(nbd->disk->queue, true, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100621 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600622 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100623}
624
Josef Bacik9561a7a2016-11-22 14:04:40 -0500625static void send_disconnects(struct nbd_device *nbd)
626{
Al Viroc9f2b6a2015-11-12 05:09:35 -0500627 struct nbd_request request = {
628 .magic = htonl(NBD_REQUEST_MAGIC),
629 .type = htonl(NBD_CMD_DISC),
630 };
631 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
632 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500633 int i, ret;
634
Josef Bacik9561a7a2016-11-22 14:04:40 -0500635 for (i = 0; i < nbd->num_connections; i++) {
Al Viroc9f2b6a2015-11-12 05:09:35 -0500636 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
637 ret = sock_xmit(nbd, i, 1, &from, 0);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500638 if (ret <= 0)
639 dev_err(disk_to_dev(nbd->disk),
640 "Send disconnect failed %d\n", ret);
641 }
642}
643
Josef Bacik9442b732017-02-07 17:10:22 -0500644static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev)
645{
646 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
647 if (!nbd->socks)
648 return -EINVAL;
649
650 mutex_unlock(&nbd->config_lock);
651 fsync_bdev(bdev);
652 mutex_lock(&nbd->config_lock);
653
654 /* Check again after getting mutex back. */
655 if (!nbd->socks)
656 return -EINVAL;
657
658 if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED,
659 &nbd->runtime_flags))
660 send_disconnects(nbd);
661 return 0;
662}
663
664static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev)
665{
666 sock_shutdown(nbd);
667 nbd_clear_que(nbd);
668 kill_bdev(bdev);
669 nbd_bdev_reset(bdev);
670 /*
671 * We want to give the run thread a chance to wait for everybody
672 * to clean up and then do it's own cleanup.
673 */
674 if (!test_bit(NBD_RUNNING, &nbd->runtime_flags) &&
675 nbd->num_connections) {
676 int i;
677
Josef Bacik6a8a2152017-03-01 11:47:22 -0500678 for (i = 0; i < nbd->num_connections; i++) {
679 sockfd_put(nbd->socks[i]->sock);
Josef Bacik9442b732017-02-07 17:10:22 -0500680 kfree(nbd->socks[i]);
Josef Bacik6a8a2152017-03-01 11:47:22 -0500681 }
Josef Bacik9442b732017-02-07 17:10:22 -0500682 kfree(nbd->socks);
683 nbd->socks = NULL;
684 nbd->num_connections = 0;
685 }
686 nbd->task_setup = NULL;
687
688 return 0;
689}
690
691static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev)
692{
693 struct recv_thread_args *args;
694 int num_connections = nbd->num_connections;
695 int error = 0, i;
696
697 if (nbd->task_recv)
698 return -EBUSY;
699 if (!nbd->socks)
700 return -EINVAL;
701 if (num_connections > 1 &&
702 !(nbd->flags & NBD_FLAG_CAN_MULTI_CONN)) {
703 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
704 error = -EINVAL;
705 goto out_err;
706 }
707
708 set_bit(NBD_RUNNING, &nbd->runtime_flags);
709 blk_mq_update_nr_hw_queues(&nbd->tag_set, nbd->num_connections);
710 args = kcalloc(num_connections, sizeof(*args), GFP_KERNEL);
711 if (!args) {
712 error = -ENOMEM;
713 goto out_err;
714 }
715 nbd->task_recv = current;
716 mutex_unlock(&nbd->config_lock);
717
718 nbd_parse_flags(nbd, bdev);
719
720 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
721 if (error) {
722 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
723 goto out_recv;
724 }
725
726 nbd_size_update(nbd, bdev);
727
728 nbd_dev_dbg_init(nbd);
729 for (i = 0; i < num_connections; i++) {
730 sk_set_memalloc(nbd->socks[i]->sock->sk);
731 atomic_inc(&nbd->recv_threads);
732 INIT_WORK(&args[i].work, recv_work);
733 args[i].nbd = nbd;
734 args[i].index = i;
735 queue_work(recv_workqueue, &args[i].work);
736 }
737 wait_event_interruptible(nbd->recv_wq,
738 atomic_read(&nbd->recv_threads) == 0);
739 for (i = 0; i < num_connections; i++)
740 flush_work(&args[i].work);
741 nbd_dev_dbg_close(nbd);
742 nbd_size_clear(nbd, bdev);
743 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
744out_recv:
745 mutex_lock(&nbd->config_lock);
746 nbd->task_recv = NULL;
747out_err:
748 clear_bit(NBD_RUNNING, &nbd->runtime_flags);
749 nbd_clear_sock(nbd, bdev);
750
751 /* user requested, ignore socket errors */
752 if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
753 error = 0;
754 if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags))
755 error = -ETIMEDOUT;
756
757 nbd_reset(nbd);
758 return error;
759}
Markus Pargmann30d53d92015-08-17 08:20:06 +0200760
Josef Bacik9561a7a2016-11-22 14:04:40 -0500761/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -0700762static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700763 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -0500766 case NBD_DISCONNECT:
767 return nbd_disconnect(nbd, bdev);
Markus Pargmann23272a672015-10-29 11:51:16 +0100768 case NBD_CLEAR_SOCK:
Josef Bacik9442b732017-02-07 17:10:22 -0500769 return nbd_clear_sock(nbd, bdev);
770 case NBD_SET_SOCK:
771 return nbd_add_socket(nbd, bdev, arg);
772 case NBD_SET_BLKSIZE:
Josef Bacike5445412017-02-13 10:39:47 -0500773 nbd_size_set(nbd, bdev, arg,
774 div_s64(nbd->bytesize, arg));
775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 case NBD_SET_SIZE:
Josef Bacike5445412017-02-13 10:39:47 -0500777 nbd_size_set(nbd, bdev, nbd->blksize,
778 div_s64(arg, nbd->blksize));
779 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +0200780 case NBD_SET_SIZE_BLOCKS:
Josef Bacike5445412017-02-13 10:39:47 -0500781 nbd_size_set(nbd, bdev, nbd->blksize, arg);
782 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -0700783 case NBD_SET_TIMEOUT:
Josef Bacik0eadf372016-09-08 12:33:40 -0700784 nbd->tag_set.timeout = arg * HZ;
Paul Clements7fdfd402007-10-16 23:27:37 -0700785 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700786
Paul Clements2f012502012-10-04 17:16:15 -0700787 case NBD_SET_FLAGS:
788 nbd->flags = arg;
789 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -0500790 case NBD_DO_IT:
791 return nbd_start_device(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800793 /*
794 * This is for compatibility only. The queue is always cleared
795 * by NBD_DO_IT or NBD_CLEAR_SOCK.
796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -0700799 /*
800 * For compatibility only, we no longer keep a list of
801 * outstanding requests.
802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700805 return -ENOTTY;
806}
807
808static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
809 unsigned int cmd, unsigned long arg)
810{
Wanlong Gaof4507162012-03-28 14:42:51 -0700811 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700812 int error;
813
814 if (!capable(CAP_SYS_ADMIN))
815 return -EPERM;
816
Wanlong Gaof4507162012-03-28 14:42:51 -0700817 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700818
Josef Bacik9561a7a2016-11-22 14:04:40 -0500819 mutex_lock(&nbd->config_lock);
Wanlong Gaof4507162012-03-28 14:42:51 -0700820 error = __nbd_ioctl(bdev, nbd, cmd, arg);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500821 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700822
823 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700826static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200829 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500830 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831};
832
Markus Pargmann30d53d92015-08-17 08:20:06 +0200833#if IS_ENABLED(CONFIG_DEBUG_FS)
834
835static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
836{
837 struct nbd_device *nbd = s->private;
838
839 if (nbd->task_recv)
840 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +0200841
842 return 0;
843}
844
845static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
846{
847 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
848}
849
850static const struct file_operations nbd_dbg_tasks_ops = {
851 .open = nbd_dbg_tasks_open,
852 .read = seq_read,
853 .llseek = seq_lseek,
854 .release = single_release,
855};
856
857static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
858{
859 struct nbd_device *nbd = s->private;
860 u32 flags = nbd->flags;
861
862 seq_printf(s, "Hex: 0x%08x\n\n", flags);
863
864 seq_puts(s, "Known flags:\n");
865
866 if (flags & NBD_FLAG_HAS_FLAGS)
867 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
868 if (flags & NBD_FLAG_READ_ONLY)
869 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
870 if (flags & NBD_FLAG_SEND_FLUSH)
871 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
872 if (flags & NBD_FLAG_SEND_TRIM)
873 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
874
875 return 0;
876}
877
878static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
879{
880 return single_open(file, nbd_dbg_flags_show, inode->i_private);
881}
882
883static const struct file_operations nbd_dbg_flags_ops = {
884 .open = nbd_dbg_flags_open,
885 .read = seq_read,
886 .llseek = seq_lseek,
887 .release = single_release,
888};
889
890static int nbd_dev_dbg_init(struct nbd_device *nbd)
891{
892 struct dentry *dir;
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200893
894 if (!nbd_dbg_dir)
895 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200896
897 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200898 if (!dir) {
899 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
900 nbd_name(nbd));
901 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200902 }
903 nbd->dbg_dir = dir;
904
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200905 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
906 debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -0700907 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacikef77b512016-12-02 16:19:12 -0500908 debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -0400909 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200910
911 return 0;
912}
913
914static void nbd_dev_dbg_close(struct nbd_device *nbd)
915{
916 debugfs_remove_recursive(nbd->dbg_dir);
917}
918
919static int nbd_dbg_init(void)
920{
921 struct dentry *dbg_dir;
922
923 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200924 if (!dbg_dir)
925 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200926
927 nbd_dbg_dir = dbg_dir;
928
929 return 0;
930}
931
932static void nbd_dbg_close(void)
933{
934 debugfs_remove_recursive(nbd_dbg_dir);
935}
936
937#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
938
939static int nbd_dev_dbg_init(struct nbd_device *nbd)
940{
941 return 0;
942}
943
944static void nbd_dev_dbg_close(struct nbd_device *nbd)
945{
946}
947
948static int nbd_dbg_init(void)
949{
950 return 0;
951}
952
953static void nbd_dbg_close(void)
954{
955}
956
957#endif
958
Josef Bacikfd8383f2016-09-08 12:33:37 -0700959static int nbd_init_request(void *data, struct request *rq,
960 unsigned int hctx_idx, unsigned int request_idx,
961 unsigned int numa_node)
962{
963 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700964 cmd->nbd = data;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700965 return 0;
966}
967
Eric Biggersf363b082017-03-30 13:39:16 -0700968static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700969 .queue_rq = nbd_queue_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700970 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -0700971 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700972};
973
Josef Bacikb0d91112017-02-01 16:11:40 -0500974static void nbd_dev_remove(struct nbd_device *nbd)
975{
976 struct gendisk *disk = nbd->disk;
977 nbd->magic = 0;
978 if (disk) {
979 del_gendisk(disk);
980 blk_cleanup_queue(disk->queue);
981 blk_mq_free_tag_set(&nbd->tag_set);
982 put_disk(disk);
983 }
984 kfree(nbd);
985}
986
987static int nbd_dev_add(int index)
988{
989 struct nbd_device *nbd;
990 struct gendisk *disk;
991 struct request_queue *q;
992 int err = -ENOMEM;
993
994 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
995 if (!nbd)
996 goto out;
997
998 disk = alloc_disk(1 << part_shift);
999 if (!disk)
1000 goto out_free_nbd;
1001
1002 if (index >= 0) {
1003 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1004 GFP_KERNEL);
1005 if (err == -ENOSPC)
1006 err = -EEXIST;
1007 } else {
1008 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1009 if (err >= 0)
1010 index = err;
1011 }
1012 if (err < 0)
1013 goto out_free_disk;
1014
1015 nbd->disk = disk;
1016 nbd->tag_set.ops = &nbd_mq_ops;
1017 nbd->tag_set.nr_hw_queues = 1;
1018 nbd->tag_set.queue_depth = 128;
1019 nbd->tag_set.numa_node = NUMA_NO_NODE;
1020 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1021 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1022 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
1023 nbd->tag_set.driver_data = nbd;
1024
1025 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1026 if (err)
1027 goto out_free_idr;
1028
1029 q = blk_mq_init_queue(&nbd->tag_set);
1030 if (IS_ERR(q)) {
1031 err = PTR_ERR(q);
1032 goto out_free_tags;
1033 }
1034 disk->queue = q;
1035
1036 /*
1037 * Tell the block layer that we are not a rotational device
1038 */
1039 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
1040 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1041 disk->queue->limits.discard_granularity = 512;
1042 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
1043 disk->queue->limits.discard_zeroes_data = 0;
1044 blk_queue_max_hw_sectors(disk->queue, 65536);
1045 disk->queue->limits.max_sectors = 256;
1046
1047 nbd->magic = NBD_MAGIC;
1048 mutex_init(&nbd->config_lock);
1049 disk->major = NBD_MAJOR;
1050 disk->first_minor = index << part_shift;
1051 disk->fops = &nbd_fops;
1052 disk->private_data = nbd;
1053 sprintf(disk->disk_name, "nbd%d", index);
1054 init_waitqueue_head(&nbd->recv_wq);
1055 nbd_reset(nbd);
1056 add_disk(disk);
1057 return index;
1058
1059out_free_tags:
1060 blk_mq_free_tag_set(&nbd->tag_set);
1061out_free_idr:
1062 idr_remove(&nbd_index_idr, index);
1063out_free_disk:
1064 put_disk(disk);
1065out_free_nbd:
1066 kfree(nbd);
1067out:
1068 return err;
1069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * And here should be modules and kernel interface
1073 * (Just smiley confuses emacs :-)
1074 */
1075
1076static int __init nbd_init(void)
1077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 int i;
1079
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08001080 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001082 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02001083 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001084 return -EINVAL;
1085 }
1086
1087 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02001088 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001089 part_shift = fls(max_part);
1090
Namhyung Kim5988ce22011-05-28 14:44:46 +02001091 /*
1092 * Adjust max_part according to part_shift as it is exported
1093 * to user space so that user can know the max number of
1094 * partition kernel should be able to manage.
1095 *
1096 * Note that -1 is required because partition 0 is reserved
1097 * for the whole disk.
1098 */
1099 max_part = (1UL << part_shift) - 1;
1100 }
1101
Namhyung Kim3b271082011-05-28 14:44:46 +02001102 if ((1UL << part_shift) > DISK_MAX_PARTS)
1103 return -EINVAL;
1104
1105 if (nbds_max > 1UL << (MINORBITS - part_shift))
1106 return -EINVAL;
Josef Bacik124d6db2017-02-01 16:11:11 -05001107 recv_workqueue = alloc_workqueue("knbd-recv",
1108 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1109 if (!recv_workqueue)
1110 return -ENOMEM;
Namhyung Kim3b271082011-05-28 14:44:46 +02001111
Josef Bacik6330a2d2017-02-15 16:49:48 -05001112 if (register_blkdev(NBD_MAJOR, "nbd")) {
1113 destroy_workqueue(recv_workqueue);
Josef Bacikb0d91112017-02-01 16:11:40 -05001114 return -EIO;
Josef Bacik6330a2d2017-02-15 16:49:48 -05001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Markus Pargmann30d53d92015-08-17 08:20:06 +02001117 nbd_dbg_init();
1118
Josef Bacikb0d91112017-02-01 16:11:40 -05001119 mutex_lock(&nbd_index_mutex);
1120 for (i = 0; i < nbds_max; i++)
1121 nbd_dev_add(i);
1122 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05001124}
1125
1126static int nbd_exit_cb(int id, void *ptr, void *data)
1127{
1128 struct nbd_device *nbd = ptr;
1129 nbd_dev_remove(nbd);
1130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133static void __exit nbd_cleanup(void)
1134{
Markus Pargmann30d53d92015-08-17 08:20:06 +02001135 nbd_dbg_close();
1136
Josef Bacikb0d91112017-02-01 16:11:40 -05001137 idr_for_each(&nbd_index_idr, &nbd_exit_cb, NULL);
1138 idr_destroy(&nbd_index_idr);
Josef Bacik124d6db2017-02-01 16:11:11 -05001139 destroy_workqueue(recv_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143module_init(nbd_init);
1144module_exit(nbd_cleanup);
1145
1146MODULE_DESCRIPTION("Network Block Device");
1147MODULE_LICENSE("GPL");
1148
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001149module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001150MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1151module_param(max_part, int, 0444);
1152MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");