blob: 42a53956aefeac1ed831948803b424a0cd6c2668 [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 Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40#include <asm/types.h>
41
42#include <linux/nbd.h>
43
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070044#define NBD_TIMEDOUT 0
45#define NBD_DISCONNECT_REQUESTED 1
46
Markus Pargmann13e71d62015-04-02 10:11:35 +020047struct nbd_device {
Markus Pargmann22d109c2015-08-17 08:20:09 +020048 u32 flags;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070049 unsigned long runtime_flags;
Markus Pargmann13e71d62015-04-02 10:11:35 +020050 struct socket * sock; /* If == NULL, device is not ready, yet */
51 int magic;
52
Josef Bacikfd8383f2016-09-08 12:33:37 -070053 struct blk_mq_tag_set tag_set;
Markus Pargmann13e71d62015-04-02 10:11:35 +020054
55 struct mutex tx_lock;
56 struct gendisk *disk;
Josef Bacik75991662016-12-02 16:19:12 -050057 loff_t blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020058 loff_t bytesize;
Markus Pargmann7e2893a2015-08-17 08:20:00 +020059
Markus Pargmann23272a672015-10-29 11:51:16 +010060 /* protects initialization and shutdown of the socket */
61 spinlock_t sock_lock;
Markus Pargmann7e2893a2015-08-17 08:20:00 +020062 struct task_struct *task_recv;
63 struct task_struct *task_send;
Markus Pargmann30d53d92015-08-17 08:20:06 +020064
65#if IS_ENABLED(CONFIG_DEBUG_FS)
66 struct dentry *dbg_dir;
67#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +020068};
69
Josef Bacikfd8383f2016-09-08 12:33:37 -070070struct nbd_cmd {
71 struct nbd_device *nbd;
72 struct list_head list;
73};
74
Markus Pargmann30d53d92015-08-17 08:20:06 +020075#if IS_ENABLED(CONFIG_DEBUG_FS)
76static struct dentry *nbd_dbg_dir;
77#endif
78
79#define nbd_name(nbd) ((nbd)->disk->disk_name)
80
Wanlong Gaof4507162012-03-28 14:42:51 -070081#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Ingo van Lil9c7a4162006-07-01 04:36:36 -070083static unsigned int nbds_max = 16;
Paul Clements20a81432008-02-08 04:21:51 -080084static struct nbd_device *nbd_dev;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070085static int max_part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Markus Pargmannd18509f2015-04-02 10:11:38 +020087static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Markus Pargmannd18509f2015-04-02 10:11:38 +020089 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Markus Pargmann37091fd2015-07-27 07:36:49 +020092static bool nbd_is_connected(struct nbd_device *nbd)
93{
94 return !!nbd->task_recv;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static const char *nbdcmd_to_ascii(int cmd)
98{
99 switch (cmd) {
100 case NBD_CMD_READ: return "read";
101 case NBD_CMD_WRITE: return "write";
102 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800103 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700104 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106 return "invalid";
107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Markus Pargmann37091fd2015-07-27 07:36:49 +0200109static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
110{
111 bdev->bd_inode->i_size = 0;
112 set_capacity(nbd->disk, 0);
113 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
114
115 return 0;
116}
117
118static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev)
119{
120 if (!nbd_is_connected(nbd))
121 return;
122
123 bdev->bd_inode->i_size = nbd->bytesize;
124 set_capacity(nbd->disk, nbd->bytesize >> 9);
125 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
126}
127
128static int nbd_size_set(struct nbd_device *nbd, struct block_device *bdev,
Josef Bacik75991662016-12-02 16:19:12 -0500129 loff_t blocksize, loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200130{
131 int ret;
132
133 ret = set_blocksize(bdev, blocksize);
134 if (ret)
135 return ret;
136
137 nbd->blksize = blocksize;
Josef Bacik75991662016-12-02 16:19:12 -0500138 nbd->bytesize = blocksize * nr_blocks;
Markus Pargmann37091fd2015-07-27 07:36:49 +0200139
140 nbd_size_update(nbd, bdev);
141
142 return 0;
143}
144
Josef Bacikfd8383f2016-09-08 12:33:37 -0700145static void nbd_end_request(struct nbd_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700147 struct nbd_device *nbd = cmd->nbd;
148 struct request *req = blk_mq_rq_from_pdu(cmd);
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500149 int error = req->errors ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Josef Bacikfd8383f2016-09-08 12:33:37 -0700151 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", cmd,
Markus Pargmannd18509f2015-04-02 10:11:38 +0200152 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Josef Bacikfd8383f2016-09-08 12:33:37 -0700154 blk_mq_complete_request(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Markus Pargmanne018e752015-04-02 10:11:39 +0200157/*
158 * Forcibly shutdown the socket causing all listeners to error
159 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200160static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700161{
Josef Bacikc2611892016-09-08 12:33:38 -0700162 struct socket *sock;
163
Josef Bacik0eadf372016-09-08 12:33:40 -0700164 spin_lock(&nbd->sock_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100165
166 if (!nbd->sock) {
John W. Linville423221d2016-10-24 15:13:25 -0400167 spin_unlock(&nbd->sock_lock);
Markus Pargmann260bbce2015-08-17 08:20:02 +0200168 return;
Markus Pargmann23272a672015-10-29 11:51:16 +0100169 }
Markus Pargmann260bbce2015-08-17 08:20:02 +0200170
Josef Bacikc2611892016-09-08 12:33:38 -0700171 sock = nbd->sock;
Markus Pargmann260bbce2015-08-17 08:20:02 +0200172 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
Markus Pargmann260bbce2015-08-17 08:20:02 +0200173 nbd->sock = NULL;
Josef Bacik0eadf372016-09-08 12:33:40 -0700174 spin_unlock(&nbd->sock_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100175
Josef Bacikc2611892016-09-08 12:33:38 -0700176 kernel_sock_shutdown(sock, SHUT_RDWR);
177 sockfd_put(sock);
Paul Clements7fdfd402007-10-16 23:27:37 -0700178}
179
Josef Bacik0eadf372016-09-08 12:33:40 -0700180static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
181 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700182{
Josef Bacik0eadf372016-09-08 12:33:40 -0700183 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
184 struct nbd_device *nbd = cmd->nbd;
Josef Bacikc2611892016-09-08 12:33:38 -0700185 struct socket *sock = NULL;
Paul Clements7fdfd402007-10-16 23:27:37 -0700186
Josef Bacik0eadf372016-09-08 12:33:40 -0700187 spin_lock(&nbd->sock_lock);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200188
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700189 set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200190
Josef Bacikc2611892016-09-08 12:33:38 -0700191 if (nbd->sock) {
192 sock = nbd->sock;
193 get_file(sock->file);
194 }
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200195
Josef Bacik0eadf372016-09-08 12:33:40 -0700196 spin_unlock(&nbd->sock_lock);
Josef Bacikc2611892016-09-08 12:33:38 -0700197 if (sock) {
198 kernel_sock_shutdown(sock, SHUT_RDWR);
199 sockfd_put(sock);
200 }
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200201
Josef Bacik0eadf372016-09-08 12:33:40 -0700202 req->errors++;
Markus Pargmann23272a672015-10-29 11:51:16 +0100203 dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
Josef Bacik0eadf372016-09-08 12:33:40 -0700204 return BLK_EH_HANDLED;
Paul Clements7fdfd402007-10-16 23:27:37 -0700205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * Send or receive packet.
209 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700210static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int msg_flags)
212{
Wanlong Gaof4507162012-03-28 14:42:51 -0700213 struct socket *sock = nbd->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int result;
215 struct msghdr msg;
216 struct kvec iov;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700217 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700219 if (unlikely(!sock)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700220 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200221 "Attempted %s on closed socket in sock_xmit\n",
222 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700223 return -EINVAL;
224 }
225
Mel Gorman7f338fe2012-07-31 16:44:32 -0700226 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700228 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 iov.iov_base = buf;
230 iov.iov_len = size;
231 msg.msg_name = NULL;
232 msg.msg_namelen = 0;
233 msg.msg_control = NULL;
234 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
236
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200237 if (send)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200239 else
Namhyung Kim35fbf5b2011-05-28 14:44:46 +0200240 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
241 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (result <= 0) {
244 if (result == 0)
245 result = -EPIPE; /* short read */
246 break;
247 }
248 size -= result;
249 buf += result;
250 } while (size > 0);
251
Mel Gorman7f338fe2012-07-31 16:44:32 -0700252 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return result;
255}
256
Wanlong Gaof4507162012-03-28 14:42:51 -0700257static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int flags)
259{
260 int result;
261 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700262 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
263 bvec->bv_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 kunmap(bvec->bv_page);
265 return result;
266}
267
Paul Clements7fdfd402007-10-16 23:27:37 -0700268/* always call with the tx_lock held */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700269static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700271 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik4b7c09a2017-01-19 16:08:49 -0500272 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct nbd_request request;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900274 unsigned long size = blk_rq_bytes(req);
Jens Axboe3a381ab2016-11-17 12:30:37 -0700275 struct bio *bio;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200276 u32 type;
277
278 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
279 type = NBD_CMD_DISC;
Mike Christiec2df40d2016-06-05 14:32:17 -0500280 else if (req_op(req) == REQ_OP_DISCARD)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200281 type = NBD_CMD_TRIM;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500282 else if (req_op(req) == REQ_OP_FLUSH)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200283 type = NBD_CMD_FLUSH;
284 else if (rq_data_dir(req) == WRITE)
285 type = NBD_CMD_WRITE;
286 else
287 type = NBD_CMD_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700289 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 request.magic = htonl(NBD_REQUEST_MAGIC);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200291 request.type = htonl(type);
292 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800293 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
294 request.len = htonl(size);
295 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700296 memcpy(request.handle, &req->tag, sizeof(req->tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Markus Pargmannd18509f2015-04-02 10:11:38 +0200298 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700299 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200300 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700301 result = sock_xmit(nbd, 1, &request, sizeof(request),
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200302 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700304 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200305 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200306 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Jens Axboe3a381ab2016-11-17 12:30:37 -0700309 if (type != NBD_CMD_WRITE)
310 return 0;
311
Jens Axboe3a381ab2016-11-17 12:30:37 -0700312 bio = req->bio;
313 while (bio) {
314 struct bio *next = bio->bi_next;
315 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800316 struct bio_vec bvec;
Jens Axboe3a381ab2016-11-17 12:30:37 -0700317
318 bio_for_each_segment(bvec, bio, iter) {
319 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacik4b7c09a2017-01-19 16:08:49 -0500320 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe3a381ab2016-11-17 12:30:37 -0700321
Markus Pargmannd18509f2015-04-02 10:11:38 +0200322 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700323 cmd, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800324 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200325 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700326 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200327 "Send data failed (result %d)\n",
328 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200329 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200330 }
Jens Axboe3a381ab2016-11-17 12:30:37 -0700331 /*
332 * The completion might already have come in,
333 * so break for the last one instead of letting
334 * the iterator do it. This prevents use-after-free
335 * of the bio.
336 */
337 if (is_last)
338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Jens Axboe3a381ab2016-11-17 12:30:37 -0700340 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Wanlong Gaof4507162012-03-28 14:42:51 -0700345static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 int result;
348 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700349 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 MSG_WAITALL);
351 kunmap(bvec->bv_page);
352 return result;
353}
354
355/* NULL returned = something went wrong, inform userspace */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700356static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int result;
359 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700360 struct nbd_cmd *cmd;
361 struct request *req = NULL;
362 u16 hwq;
363 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700366 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700368 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200369 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200370 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700372
373 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700374 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700375 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200376 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700377 }
378
Josef Bacikfd8383f2016-09-08 12:33:37 -0700379 memcpy(&tag, reply.handle, sizeof(int));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800380
Josef Bacikfd8383f2016-09-08 12:33:37 -0700381 hwq = blk_mq_unique_tag_to_hwq(tag);
382 if (hwq < nbd->tag_set.nr_hw_queues)
383 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
384 blk_mq_unique_tag_to_tag(tag));
385 if (!req || !blk_mq_request_started(req)) {
386 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
387 tag, req);
388 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700390 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700393 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200394 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700396 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
Josef Bacikfd8383f2016-09-08 12:33:37 -0700399 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200400 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200401 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800402 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200403
404 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800405 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200406 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700407 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200408 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200409 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700410 return cmd;
Jens Axboe6c92e692007-08-16 13:43:12 +0200411 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200412 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700413 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700416 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200419static ssize_t pid_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800421{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200422 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200423 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200424
Markus Pargmann6521d392015-08-17 08:20:05 +0200425 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800426}
427
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200428static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700429 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800430 .show = pid_show,
431};
432
Markus Pargmann37091fd2015-07-27 07:36:49 +0200433static int nbd_thread_recv(struct nbd_device *nbd, struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700435 struct nbd_cmd *cmd;
WANG Cong84963042007-05-09 02:33:36 -0700436 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Wanlong Gaof4507162012-03-28 14:42:51 -0700438 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Mel Gorman7f338fe2012-07-31 16:44:32 -0700440 sk_set_memalloc(nbd->sock->sk);
Markus Pargmann6521d392015-08-17 08:20:05 +0200441
Wanlong Gaof4507162012-03-28 14:42:51 -0700442 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700443 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700444 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
WANG Cong84963042007-05-09 02:33:36 -0700445 return ret;
446 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800447
Markus Pargmann37091fd2015-07-27 07:36:49 +0200448 nbd_size_update(nbd, bdev);
449
Markus Pargmann19391832015-08-17 08:20:03 +0200450 while (1) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700451 cmd = nbd_read_stat(nbd);
452 if (IS_ERR(cmd)) {
453 ret = PTR_ERR(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200454 break;
455 }
456
Josef Bacikfd8383f2016-09-08 12:33:37 -0700457 nbd_end_request(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200458 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800459
Markus Pargmann37091fd2015-07-27 07:36:49 +0200460 nbd_size_clear(nbd, bdev);
461
Markus Pargmann6521d392015-08-17 08:20:05 +0200462 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Josef Bacikfd8383f2016-09-08 12:33:37 -0700466static void nbd_clear_req(struct request *req, void *data, bool reserved)
467{
468 struct nbd_cmd *cmd;
469
470 if (!blk_mq_request_started(req))
471 return;
472 cmd = blk_mq_rq_to_pdu(req);
473 req->errors++;
474 nbd_end_request(cmd);
475}
476
Wanlong Gaof4507162012-03-28 14:42:51 -0700477static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Wanlong Gaof4507162012-03-28 14:42:51 -0700479 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Herbert Xu4b2f0262006-01-06 00:09:47 -0800481 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700482 * Because we have set nbd->sock to NULL under the tx_lock, all
Josef Bacikfd8383f2016-09-08 12:33:37 -0700483 * modifications to the list must have completed by now.
Herbert Xu4b2f0262006-01-06 00:09:47 -0800484 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700485 BUG_ON(nbd->sock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800486
Josef Bacikfd8383f2016-09-08 12:33:37 -0700487 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200488 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Paul Clements7fdfd402007-10-16 23:27:37 -0700491
Josef Bacikfd8383f2016-09-08 12:33:37 -0700492static void nbd_handle_cmd(struct nbd_cmd *cmd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700493{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700494 struct request *req = blk_mq_rq_from_pdu(cmd);
495 struct nbd_device *nbd = cmd->nbd;
496
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200497 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700498 goto error_out;
499
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200500 if (rq_data_dir(req) == WRITE &&
501 (nbd->flags & NBD_FLAG_READ_ONLY)) {
502 dev_err(disk_to_dev(nbd->disk),
503 "Write on read-only\n");
504 goto error_out;
Alex Bligh75f187a2013-02-27 17:05:23 -0800505 }
506
Laurent Vivier48cf6062008-04-29 01:02:46 -0700507 req->errors = 0;
508
Wanlong Gaof4507162012-03-28 14:42:51 -0700509 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700510 nbd->task_send = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700511 if (unlikely(!nbd->sock)) {
512 mutex_unlock(&nbd->tx_lock);
513 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200514 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700515 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700516 }
517
Josef Bacikfd8383f2016-09-08 12:33:37 -0700518 if (nbd_send_cmd(nbd, cmd) != 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700519 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700520 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700521 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700522 }
523
Josef Bacikfd8383f2016-09-08 12:33:37 -0700524 nbd->task_send = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700525 mutex_unlock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700526
527 return;
528
529error_out:
530 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700531 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700532}
533
Josef Bacikfd8383f2016-09-08 12:33:37 -0700534static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
535 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700536{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700537 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700538
Josef Bacikfd8383f2016-09-08 12:33:37 -0700539 blk_mq_start_request(bd->rq);
540 nbd_handle_cmd(cmd);
541 return BLK_MQ_RQ_QUEUE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Markus Pargmann23272a672015-10-29 11:51:16 +0100544static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
545{
546 int ret = 0;
547
548 spin_lock_irq(&nbd->sock_lock);
549
550 if (nbd->sock) {
551 ret = -EBUSY;
552 goto out;
553 }
554
555 nbd->sock = sock;
556
557out:
558 spin_unlock_irq(&nbd->sock_lock);
559
560 return ret;
561}
562
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100563/* Reset all properties of an NBD device */
564static void nbd_reset(struct nbd_device *nbd)
565{
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700566 nbd->runtime_flags = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100567 nbd->blksize = 1024;
568 nbd->bytesize = 0;
569 set_capacity(nbd->disk, 0);
570 nbd->flags = 0;
Josef Bacik0eadf372016-09-08 12:33:40 -0700571 nbd->tag_set.timeout = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100572 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100573}
574
575static void nbd_bdev_reset(struct block_device *bdev)
576{
577 set_device_ro(bdev, false);
578 bdev->bd_inode->i_size = 0;
579 if (max_part > 0) {
580 blkdev_reread_part(bdev);
581 bdev->bd_invalidated = 1;
582 }
583}
584
Markus Pargmannd02cf532015-10-29 12:06:15 +0100585static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
586{
587 if (nbd->flags & NBD_FLAG_READ_ONLY)
588 set_device_ro(bdev, true);
589 if (nbd->flags & NBD_FLAG_SEND_TRIM)
590 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
591 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600592 blk_queue_write_cache(nbd->disk->queue, true, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100593 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600594 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100595}
596
Markus Pargmann30d53d92015-08-17 08:20:06 +0200597static int nbd_dev_dbg_init(struct nbd_device *nbd);
598static void nbd_dev_dbg_close(struct nbd_device *nbd);
599
Pavel Machek1a2ad212009-04-02 16:58:41 -0700600/* Must be called with tx_lock held */
601
Wanlong Gaof4507162012-03-28 14:42:51 -0700602static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700603 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700606 case NBD_DISCONNECT: {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700607 struct request *sreq;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700608
Wanlong Gaof4507162012-03-28 14:42:51 -0700609 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800610 if (!nbd->sock)
611 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700612
Josef Bacikfd8383f2016-09-08 12:33:37 -0700613 sreq = blk_mq_alloc_request(bdev_get_queue(bdev), WRITE, 0);
Christophe JAILLET77291852016-10-30 05:28:27 +0100614 if (IS_ERR(sreq))
Josef Bacikfd8383f2016-09-08 12:33:37 -0700615 return -ENOMEM;
616
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800617 mutex_unlock(&nbd->tx_lock);
618 fsync_bdev(bdev);
619 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700620 sreq->cmd_type = REQ_TYPE_DRV_PRIV;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800621
622 /* Check again after getting mutex back. */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700623 if (!nbd->sock) {
624 blk_mq_free_request(sreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EINVAL;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700626 }
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800627
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700628 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags);
Paul Clementsc378f702013-07-03 15:09:04 -0700629
Josef Bacikfd8383f2016-09-08 12:33:37 -0700630 nbd_send_cmd(nbd, blk_mq_rq_to_pdu(sreq));
631 blk_mq_free_request(sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700632 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Markus Pargmann23272a672015-10-29 11:51:16 +0100635 case NBD_CLEAR_SOCK:
636 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700637 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800638 kill_bdev(bdev);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700639 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700640
641 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500642 int err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100643 struct socket *sock = sockfd_lookup(arg, &err);
644
645 if (!sock)
646 return err;
647
648 err = nbd_set_socket(nbd, sock);
649 if (!err && max_part)
650 bdev->bd_invalidated = 1;
651
652 return err;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700653 }
654
Markus Pargmann37091fd2015-07-27 07:36:49 +0200655 case NBD_SET_BLKSIZE: {
Arnd Bergmann5e454c62016-03-05 00:49:31 +0100656 loff_t bsize = div_s64(nbd->bytesize, arg);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200657
658 return nbd_size_set(nbd, bdev, arg, bsize);
659 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 case NBD_SET_SIZE:
Markus Pargmann37091fd2015-07-27 07:36:49 +0200662 return nbd_size_set(nbd, bdev, nbd->blksize,
Jens Axboe25640e72016-12-03 12:08:03 -0700663 div_s64(arg, nbd->blksize));
Markus Pargmann37091fd2015-07-27 07:36:49 +0200664
665 case NBD_SET_SIZE_BLOCKS:
666 return nbd_size_set(nbd, bdev, nbd->blksize, arg);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700667
Paul Clements7fdfd402007-10-16 23:27:37 -0700668 case NBD_SET_TIMEOUT:
Josef Bacik521a7e32017-03-24 14:08:28 -0400669 if (arg) {
670 nbd->tag_set.timeout = arg * HZ;
671 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
672 }
Paul Clements7fdfd402007-10-16 23:27:37 -0700673 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700674
Paul Clements2f012502012-10-04 17:16:15 -0700675 case NBD_SET_FLAGS:
676 nbd->flags = arg;
677 return 0;
678
Pavel Machek1a2ad212009-04-02 16:58:41 -0700679 case NBD_DO_IT: {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700680 int error;
681
Markus Pargmann6521d392015-08-17 08:20:05 +0200682 if (nbd->task_recv)
Pavel Machekc91192d2009-01-15 13:51:03 -0800683 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500684 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700686
Vegard Nossum97240962016-05-27 12:59:35 +0200687 /* We have to claim the device under the lock */
688 nbd->task_recv = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700689 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700690
Markus Pargmannd02cf532015-10-29 12:06:15 +0100691 nbd_parse_flags(nbd, bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700692
Markus Pargmann30d53d92015-08-17 08:20:06 +0200693 nbd_dev_dbg_init(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200694 error = nbd_thread_recv(nbd, bdev);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200695 nbd_dev_dbg_close(nbd);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700696
Wanlong Gaof4507162012-03-28 14:42:51 -0700697 mutex_lock(&nbd->tx_lock);
Vegard Nossum97240962016-05-27 12:59:35 +0200698 nbd->task_recv = NULL;
Markus Pargmann19391832015-08-17 08:20:03 +0200699
Markus Pargmann36e47be2015-08-17 08:20:01 +0200700 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700701 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800702 kill_bdev(bdev);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100703 nbd_bdev_reset(bdev);
704
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700705 /* user requested, ignore socket errors */
706 if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100707 error = 0;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700708 if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100709 error = -ETIMEDOUT;
710
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100711 nbd_reset(nbd);
712
Markus Pargmann19391832015-08-17 08:20:03 +0200713 return error;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800717 /*
718 * This is for compatibility only. The queue is always cleared
719 * by NBD_DO_IT or NBD_CLEAR_SOCK.
720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -0700724 /*
725 * For compatibility only, we no longer keep a list of
726 * outstanding requests.
727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700730 return -ENOTTY;
731}
732
733static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
734 unsigned int cmd, unsigned long arg)
735{
Wanlong Gaof4507162012-03-28 14:42:51 -0700736 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700737 int error;
738
739 if (!capable(CAP_SYS_ADMIN))
740 return -EPERM;
741
Wanlong Gaof4507162012-03-28 14:42:51 -0700742 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700743
Wanlong Gaof4507162012-03-28 14:42:51 -0700744 mutex_lock(&nbd->tx_lock);
745 error = __nbd_ioctl(bdev, nbd, cmd, arg);
746 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700747
748 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700751static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200754 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500755 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756};
757
Markus Pargmann30d53d92015-08-17 08:20:06 +0200758#if IS_ENABLED(CONFIG_DEBUG_FS)
759
760static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
761{
762 struct nbd_device *nbd = s->private;
763
764 if (nbd->task_recv)
765 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
766 if (nbd->task_send)
767 seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send));
768
769 return 0;
770}
771
772static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
773{
774 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
775}
776
777static const struct file_operations nbd_dbg_tasks_ops = {
778 .open = nbd_dbg_tasks_open,
779 .read = seq_read,
780 .llseek = seq_lseek,
781 .release = single_release,
782};
783
784static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
785{
786 struct nbd_device *nbd = s->private;
787 u32 flags = nbd->flags;
788
789 seq_printf(s, "Hex: 0x%08x\n\n", flags);
790
791 seq_puts(s, "Known flags:\n");
792
793 if (flags & NBD_FLAG_HAS_FLAGS)
794 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
795 if (flags & NBD_FLAG_READ_ONLY)
796 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
797 if (flags & NBD_FLAG_SEND_FLUSH)
798 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
799 if (flags & NBD_FLAG_SEND_TRIM)
800 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
801
802 return 0;
803}
804
805static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
806{
807 return single_open(file, nbd_dbg_flags_show, inode->i_private);
808}
809
810static const struct file_operations nbd_dbg_flags_ops = {
811 .open = nbd_dbg_flags_open,
812 .read = seq_read,
813 .llseek = seq_lseek,
814 .release = single_release,
815};
816
817static int nbd_dev_dbg_init(struct nbd_device *nbd)
818{
819 struct dentry *dir;
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200820
821 if (!nbd_dbg_dir)
822 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200823
824 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200825 if (!dir) {
826 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
827 nbd_name(nbd));
828 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200829 }
830 nbd->dbg_dir = dir;
831
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200832 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
833 debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -0700834 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik75991662016-12-02 16:19:12 -0500835 debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -0400836 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200837
838 return 0;
839}
840
841static void nbd_dev_dbg_close(struct nbd_device *nbd)
842{
843 debugfs_remove_recursive(nbd->dbg_dir);
844}
845
846static int nbd_dbg_init(void)
847{
848 struct dentry *dbg_dir;
849
850 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200851 if (!dbg_dir)
852 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200853
854 nbd_dbg_dir = dbg_dir;
855
856 return 0;
857}
858
859static void nbd_dbg_close(void)
860{
861 debugfs_remove_recursive(nbd_dbg_dir);
862}
863
864#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
865
866static int nbd_dev_dbg_init(struct nbd_device *nbd)
867{
868 return 0;
869}
870
871static void nbd_dev_dbg_close(struct nbd_device *nbd)
872{
873}
874
875static int nbd_dbg_init(void)
876{
877 return 0;
878}
879
880static void nbd_dbg_close(void)
881{
882}
883
884#endif
885
Josef Bacikfd8383f2016-09-08 12:33:37 -0700886static int nbd_init_request(void *data, struct request *rq,
887 unsigned int hctx_idx, unsigned int request_idx,
888 unsigned int numa_node)
889{
890 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
891
892 cmd->nbd = data;
893 INIT_LIST_HEAD(&cmd->list);
894 return 0;
895}
896
897static struct blk_mq_ops nbd_mq_ops = {
898 .queue_rq = nbd_queue_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700899 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -0700900 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700901};
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/*
904 * And here should be modules and kernel interface
905 * (Just smiley confuses emacs :-)
906 */
907
908static int __init nbd_init(void)
909{
910 int err = -ENOMEM;
911 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700912 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800914 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700916 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +0200917 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700918 return -EINVAL;
919 }
920
921 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +0200922 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700923 part_shift = fls(max_part);
924
Namhyung Kim5988ce22011-05-28 14:44:46 +0200925 /*
926 * Adjust max_part according to part_shift as it is exported
927 * to user space so that user can know the max number of
928 * partition kernel should be able to manage.
929 *
930 * Note that -1 is required because partition 0 is reserved
931 * for the whole disk.
932 */
933 max_part = (1UL << part_shift) - 1;
934 }
935
Namhyung Kim3b271082011-05-28 14:44:46 +0200936 if ((1UL << part_shift) > DISK_MAX_PARTS)
937 return -EINVAL;
938
939 if (nbds_max > 1UL << (MINORBITS - part_shift))
940 return -EINVAL;
941
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +0530942 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
943 if (!nbd_dev)
944 return -ENOMEM;
945
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700946 for (i = 0; i < nbds_max; i++) {
Jeff Moyer952d07a2017-01-09 15:20:31 -0500947 struct request_queue *q;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700948 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!disk)
950 goto out;
951 nbd_dev[i].disk = disk;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700952
953 nbd_dev[i].tag_set.ops = &nbd_mq_ops;
954 nbd_dev[i].tag_set.nr_hw_queues = 1;
955 nbd_dev[i].tag_set.queue_depth = 128;
956 nbd_dev[i].tag_set.numa_node = NUMA_NO_NODE;
957 nbd_dev[i].tag_set.cmd_size = sizeof(struct nbd_cmd);
958 nbd_dev[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Josef Bacik005043a2016-09-21 16:55:31 -0400959 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700960 nbd_dev[i].tag_set.driver_data = &nbd_dev[i];
961
962 err = blk_mq_alloc_tag_set(&nbd_dev[i].tag_set);
963 if (err) {
964 put_disk(disk);
965 goto out;
966 }
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /*
969 * The new linux 2.5 block layer implementation requires
970 * every gendisk to have its very own request_queue struct.
971 * These structs are big so we dynamically allocate them.
972 */
Jeff Moyer952d07a2017-01-09 15:20:31 -0500973 q = blk_mq_init_queue(&nbd_dev[i].tag_set);
974 if (IS_ERR(q)) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700975 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 put_disk(disk);
977 goto out;
978 }
Jeff Moyer952d07a2017-01-09 15:20:31 -0500979 disk->queue = q;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700980
Jens Axboe31dcfab2008-10-31 10:06:37 +0100981 /*
982 * Tell the block layer that we are not a rotational device
983 */
984 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600985 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -0700986 disk->queue->limits.discard_granularity = 512;
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600987 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Paul Clementsa336d292012-10-04 17:16:18 -0700988 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -0700989 blk_queue_max_hw_sectors(disk->queue, 65536);
990 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
993 if (register_blkdev(NBD_MAJOR, "nbd")) {
994 err = -EIO;
995 goto out;
996 }
997
998 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Markus Pargmann30d53d92015-08-17 08:20:06 +02001000 nbd_dbg_init();
1001
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001002 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -07001004 nbd_dev[i].magic = NBD_MAGIC;
Markus Pargmann23272a672015-10-29 11:51:16 +01001005 spin_lock_init(&nbd_dev[i].sock_lock);
Ingo Molnar82d4dc52006-03-23 03:00:38 -08001006 mutex_init(&nbd_dev[i].tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001008 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 disk->fops = &nbd_fops;
1010 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sprintf(disk->disk_name, "nbd%d", i);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001012 nbd_reset(&nbd_dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 add_disk(disk);
1014 }
1015
1016 return 0;
1017out:
1018 while (i--) {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001019 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 blk_cleanup_queue(nbd_dev[i].disk->queue);
1021 put_disk(nbd_dev[i].disk);
1022 }
Sven Wegenerf3944d62008-08-20 14:09:07 -07001023 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return err;
1025}
1026
1027static void __exit nbd_cleanup(void)
1028{
1029 int i;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001030
1031 nbd_dbg_close();
1032
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001033 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001035 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (disk) {
1037 del_gendisk(disk);
1038 blk_cleanup_queue(disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001039 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 put_disk(disk);
1041 }
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -07001044 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
1046}
1047
1048module_init(nbd_init);
1049module_exit(nbd_cleanup);
1050
1051MODULE_DESCRIPTION("Network Block Device");
1052MODULE_LICENSE("GPL");
1053
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001054module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001055MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1056module_param(max_part, int, 0444);
1057MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");