blob: 4d30da26906064642be13628d677c10f64cce9e1 [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);
NeilBrown5705f702007-09-25 12:35:59 +0200272 int result, flags;
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
312 flags = 0;
313 bio = req->bio;
314 while (bio) {
315 struct bio *next = bio->bi_next;
316 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800317 struct bio_vec bvec;
Jens Axboe3a381ab2016-11-17 12:30:37 -0700318
319 bio_for_each_segment(bvec, bio, iter) {
320 bool is_last = !next && bio_iter_last(bvec, iter);
321
322 if (is_last)
Jens Axboe6c92e692007-08-16 13:43:12 +0200323 flags = MSG_MORE;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200324 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700325 cmd, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800326 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200327 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700328 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200329 "Send data failed (result %d)\n",
330 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200331 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200332 }
Jens Axboe3a381ab2016-11-17 12:30:37 -0700333 /*
334 * The completion might already have come in,
335 * so break for the last one instead of letting
336 * the iterator do it. This prevents use-after-free
337 * of the bio.
338 */
339 if (is_last)
340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Jens Axboe3a381ab2016-11-17 12:30:37 -0700342 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Wanlong Gaof4507162012-03-28 14:42:51 -0700347static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 int result;
350 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700351 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 MSG_WAITALL);
353 kunmap(bvec->bv_page);
354 return result;
355}
356
357/* NULL returned = something went wrong, inform userspace */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700358static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 int result;
361 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700362 struct nbd_cmd *cmd;
363 struct request *req = NULL;
364 u16 hwq;
365 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700368 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700370 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200371 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200372 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700374
375 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700376 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700377 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200378 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700379 }
380
Josef Bacikfd8383f2016-09-08 12:33:37 -0700381 memcpy(&tag, reply.handle, sizeof(int));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800382
Josef Bacikfd8383f2016-09-08 12:33:37 -0700383 hwq = blk_mq_unique_tag_to_hwq(tag);
384 if (hwq < nbd->tag_set.nr_hw_queues)
385 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
386 blk_mq_unique_tag_to_tag(tag));
387 if (!req || !blk_mq_request_started(req)) {
388 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
389 tag, req);
390 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700392 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700395 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200396 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700398 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Josef Bacikfd8383f2016-09-08 12:33:37 -0700401 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200402 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200403 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800404 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200405
406 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800407 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200408 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700409 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200410 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200411 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700412 return cmd;
Jens Axboe6c92e692007-08-16 13:43:12 +0200413 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200414 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700415 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700418 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200421static ssize_t pid_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800423{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200424 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200425 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200426
Markus Pargmann6521d392015-08-17 08:20:05 +0200427 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800428}
429
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200430static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700431 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800432 .show = pid_show,
433};
434
Markus Pargmann37091fd2015-07-27 07:36:49 +0200435static int nbd_thread_recv(struct nbd_device *nbd, struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700437 struct nbd_cmd *cmd;
WANG Cong84963042007-05-09 02:33:36 -0700438 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Wanlong Gaof4507162012-03-28 14:42:51 -0700440 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Mel Gorman7f338fe2012-07-31 16:44:32 -0700442 sk_set_memalloc(nbd->sock->sk);
Markus Pargmann6521d392015-08-17 08:20:05 +0200443
Wanlong Gaof4507162012-03-28 14:42:51 -0700444 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700445 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700446 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
WANG Cong84963042007-05-09 02:33:36 -0700447 return ret;
448 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800449
Markus Pargmann37091fd2015-07-27 07:36:49 +0200450 nbd_size_update(nbd, bdev);
451
Markus Pargmann19391832015-08-17 08:20:03 +0200452 while (1) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700453 cmd = nbd_read_stat(nbd);
454 if (IS_ERR(cmd)) {
455 ret = PTR_ERR(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200456 break;
457 }
458
Josef Bacikfd8383f2016-09-08 12:33:37 -0700459 nbd_end_request(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200460 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800461
Markus Pargmann37091fd2015-07-27 07:36:49 +0200462 nbd_size_clear(nbd, bdev);
463
Markus Pargmann6521d392015-08-17 08:20:05 +0200464 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Josef Bacikfd8383f2016-09-08 12:33:37 -0700468static void nbd_clear_req(struct request *req, void *data, bool reserved)
469{
470 struct nbd_cmd *cmd;
471
472 if (!blk_mq_request_started(req))
473 return;
474 cmd = blk_mq_rq_to_pdu(req);
475 req->errors++;
476 nbd_end_request(cmd);
477}
478
Wanlong Gaof4507162012-03-28 14:42:51 -0700479static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Wanlong Gaof4507162012-03-28 14:42:51 -0700481 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Herbert Xu4b2f0262006-01-06 00:09:47 -0800483 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700484 * Because we have set nbd->sock to NULL under the tx_lock, all
Josef Bacikfd8383f2016-09-08 12:33:37 -0700485 * modifications to the list must have completed by now.
Herbert Xu4b2f0262006-01-06 00:09:47 -0800486 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700487 BUG_ON(nbd->sock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800488
Josef Bacikfd8383f2016-09-08 12:33:37 -0700489 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200490 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Paul Clements7fdfd402007-10-16 23:27:37 -0700493
Josef Bacikfd8383f2016-09-08 12:33:37 -0700494static void nbd_handle_cmd(struct nbd_cmd *cmd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700495{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700496 struct request *req = blk_mq_rq_from_pdu(cmd);
497 struct nbd_device *nbd = cmd->nbd;
498
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200499 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700500 goto error_out;
501
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200502 if (rq_data_dir(req) == WRITE &&
503 (nbd->flags & NBD_FLAG_READ_ONLY)) {
504 dev_err(disk_to_dev(nbd->disk),
505 "Write on read-only\n");
506 goto error_out;
Alex Bligh75f187a2013-02-27 17:05:23 -0800507 }
508
Laurent Vivier48cf6062008-04-29 01:02:46 -0700509 req->errors = 0;
510
Wanlong Gaof4507162012-03-28 14:42:51 -0700511 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700512 nbd->task_send = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700513 if (unlikely(!nbd->sock)) {
514 mutex_unlock(&nbd->tx_lock);
515 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200516 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700517 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700518 }
519
Josef Bacikfd8383f2016-09-08 12:33:37 -0700520 if (nbd_send_cmd(nbd, cmd) != 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700521 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700522 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700523 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700524 }
525
Josef Bacikfd8383f2016-09-08 12:33:37 -0700526 nbd->task_send = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700527 mutex_unlock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700528
529 return;
530
531error_out:
532 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700533 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700534}
535
Josef Bacikfd8383f2016-09-08 12:33:37 -0700536static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
537 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700538{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700539 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700540
Josef Bacikfd8383f2016-09-08 12:33:37 -0700541 blk_mq_start_request(bd->rq);
542 nbd_handle_cmd(cmd);
543 return BLK_MQ_RQ_QUEUE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Markus Pargmann23272a672015-10-29 11:51:16 +0100546static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
547{
548 int ret = 0;
549
550 spin_lock_irq(&nbd->sock_lock);
551
552 if (nbd->sock) {
553 ret = -EBUSY;
554 goto out;
555 }
556
557 nbd->sock = sock;
558
559out:
560 spin_unlock_irq(&nbd->sock_lock);
561
562 return ret;
563}
564
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100565/* Reset all properties of an NBD device */
566static void nbd_reset(struct nbd_device *nbd)
567{
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700568 nbd->runtime_flags = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100569 nbd->blksize = 1024;
570 nbd->bytesize = 0;
571 set_capacity(nbd->disk, 0);
572 nbd->flags = 0;
Josef Bacik0eadf372016-09-08 12:33:40 -0700573 nbd->tag_set.timeout = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100574 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100575}
576
577static void nbd_bdev_reset(struct block_device *bdev)
578{
579 set_device_ro(bdev, false);
580 bdev->bd_inode->i_size = 0;
581 if (max_part > 0) {
582 blkdev_reread_part(bdev);
583 bdev->bd_invalidated = 1;
584 }
585}
586
Markus Pargmannd02cf532015-10-29 12:06:15 +0100587static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
588{
589 if (nbd->flags & NBD_FLAG_READ_ONLY)
590 set_device_ro(bdev, true);
591 if (nbd->flags & NBD_FLAG_SEND_TRIM)
592 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
593 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600594 blk_queue_write_cache(nbd->disk->queue, true, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100595 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600596 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100597}
598
Markus Pargmann30d53d92015-08-17 08:20:06 +0200599static int nbd_dev_dbg_init(struct nbd_device *nbd);
600static void nbd_dev_dbg_close(struct nbd_device *nbd);
601
Pavel Machek1a2ad212009-04-02 16:58:41 -0700602/* Must be called with tx_lock held */
603
Wanlong Gaof4507162012-03-28 14:42:51 -0700604static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700605 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700608 case NBD_DISCONNECT: {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700609 struct request *sreq;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700610
Wanlong Gaof4507162012-03-28 14:42:51 -0700611 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800612 if (!nbd->sock)
613 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700614
Josef Bacikfd8383f2016-09-08 12:33:37 -0700615 sreq = blk_mq_alloc_request(bdev_get_queue(bdev), WRITE, 0);
Christophe JAILLET77291852016-10-30 05:28:27 +0100616 if (IS_ERR(sreq))
Josef Bacikfd8383f2016-09-08 12:33:37 -0700617 return -ENOMEM;
618
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800619 mutex_unlock(&nbd->tx_lock);
620 fsync_bdev(bdev);
621 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700622 sreq->cmd_type = REQ_TYPE_DRV_PRIV;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800623
624 /* Check again after getting mutex back. */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700625 if (!nbd->sock) {
626 blk_mq_free_request(sreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -EINVAL;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700628 }
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800629
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700630 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags);
Paul Clementsc378f702013-07-03 15:09:04 -0700631
Josef Bacikfd8383f2016-09-08 12:33:37 -0700632 nbd_send_cmd(nbd, blk_mq_rq_to_pdu(sreq));
633 blk_mq_free_request(sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700634 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Markus Pargmann23272a672015-10-29 11:51:16 +0100637 case NBD_CLEAR_SOCK:
638 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700639 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800640 kill_bdev(bdev);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700641 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700642
643 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500644 int err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100645 struct socket *sock = sockfd_lookup(arg, &err);
646
647 if (!sock)
648 return err;
649
650 err = nbd_set_socket(nbd, sock);
651 if (!err && max_part)
652 bdev->bd_invalidated = 1;
653
654 return err;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700655 }
656
Markus Pargmann37091fd2015-07-27 07:36:49 +0200657 case NBD_SET_BLKSIZE: {
Arnd Bergmann5e454c62016-03-05 00:49:31 +0100658 loff_t bsize = div_s64(nbd->bytesize, arg);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200659
660 return nbd_size_set(nbd, bdev, arg, bsize);
661 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 case NBD_SET_SIZE:
Markus Pargmann37091fd2015-07-27 07:36:49 +0200664 return nbd_size_set(nbd, bdev, nbd->blksize,
Jens Axboe25640e72016-12-03 12:08:03 -0700665 div_s64(arg, nbd->blksize));
Markus Pargmann37091fd2015-07-27 07:36:49 +0200666
667 case NBD_SET_SIZE_BLOCKS:
668 return nbd_size_set(nbd, bdev, nbd->blksize, arg);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700669
Paul Clements7fdfd402007-10-16 23:27:37 -0700670 case NBD_SET_TIMEOUT:
Josef Bacik521a7e32017-03-24 14:08:28 -0400671 if (arg) {
672 nbd->tag_set.timeout = arg * HZ;
673 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
674 }
Paul Clements7fdfd402007-10-16 23:27:37 -0700675 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700676
Paul Clements2f012502012-10-04 17:16:15 -0700677 case NBD_SET_FLAGS:
678 nbd->flags = arg;
679 return 0;
680
Pavel Machek1a2ad212009-04-02 16:58:41 -0700681 case NBD_DO_IT: {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700682 int error;
683
Markus Pargmann6521d392015-08-17 08:20:05 +0200684 if (nbd->task_recv)
Pavel Machekc91192d2009-01-15 13:51:03 -0800685 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500686 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700688
Vegard Nossum97240962016-05-27 12:59:35 +0200689 /* We have to claim the device under the lock */
690 nbd->task_recv = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700691 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700692
Markus Pargmannd02cf532015-10-29 12:06:15 +0100693 nbd_parse_flags(nbd, bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700694
Markus Pargmann30d53d92015-08-17 08:20:06 +0200695 nbd_dev_dbg_init(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200696 error = nbd_thread_recv(nbd, bdev);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200697 nbd_dev_dbg_close(nbd);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700698
Wanlong Gaof4507162012-03-28 14:42:51 -0700699 mutex_lock(&nbd->tx_lock);
Vegard Nossum97240962016-05-27 12:59:35 +0200700 nbd->task_recv = NULL;
Markus Pargmann19391832015-08-17 08:20:03 +0200701
Markus Pargmann36e47be2015-08-17 08:20:01 +0200702 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700703 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800704 kill_bdev(bdev);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100705 nbd_bdev_reset(bdev);
706
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700707 /* user requested, ignore socket errors */
708 if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100709 error = 0;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700710 if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100711 error = -ETIMEDOUT;
712
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100713 nbd_reset(nbd);
714
Markus Pargmann19391832015-08-17 08:20:03 +0200715 return error;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800719 /*
720 * This is for compatibility only. The queue is always cleared
721 * by NBD_DO_IT or NBD_CLEAR_SOCK.
722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -0700726 /*
727 * For compatibility only, we no longer keep a list of
728 * outstanding requests.
729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 0;
731 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700732 return -ENOTTY;
733}
734
735static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
736 unsigned int cmd, unsigned long arg)
737{
Wanlong Gaof4507162012-03-28 14:42:51 -0700738 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700739 int error;
740
741 if (!capable(CAP_SYS_ADMIN))
742 return -EPERM;
743
Wanlong Gaof4507162012-03-28 14:42:51 -0700744 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700745
Wanlong Gaof4507162012-03-28 14:42:51 -0700746 mutex_lock(&nbd->tx_lock);
747 error = __nbd_ioctl(bdev, nbd, cmd, arg);
748 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700749
750 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700753static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200756 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500757 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758};
759
Markus Pargmann30d53d92015-08-17 08:20:06 +0200760#if IS_ENABLED(CONFIG_DEBUG_FS)
761
762static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
763{
764 struct nbd_device *nbd = s->private;
765
766 if (nbd->task_recv)
767 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
768 if (nbd->task_send)
769 seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send));
770
771 return 0;
772}
773
774static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
775{
776 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
777}
778
779static const struct file_operations nbd_dbg_tasks_ops = {
780 .open = nbd_dbg_tasks_open,
781 .read = seq_read,
782 .llseek = seq_lseek,
783 .release = single_release,
784};
785
786static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
787{
788 struct nbd_device *nbd = s->private;
789 u32 flags = nbd->flags;
790
791 seq_printf(s, "Hex: 0x%08x\n\n", flags);
792
793 seq_puts(s, "Known flags:\n");
794
795 if (flags & NBD_FLAG_HAS_FLAGS)
796 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
797 if (flags & NBD_FLAG_READ_ONLY)
798 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
799 if (flags & NBD_FLAG_SEND_FLUSH)
800 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
801 if (flags & NBD_FLAG_SEND_TRIM)
802 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
803
804 return 0;
805}
806
807static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
808{
809 return single_open(file, nbd_dbg_flags_show, inode->i_private);
810}
811
812static const struct file_operations nbd_dbg_flags_ops = {
813 .open = nbd_dbg_flags_open,
814 .read = seq_read,
815 .llseek = seq_lseek,
816 .release = single_release,
817};
818
819static int nbd_dev_dbg_init(struct nbd_device *nbd)
820{
821 struct dentry *dir;
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200822
823 if (!nbd_dbg_dir)
824 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200825
826 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200827 if (!dir) {
828 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
829 nbd_name(nbd));
830 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200831 }
832 nbd->dbg_dir = dir;
833
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200834 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
835 debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -0700836 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik75991662016-12-02 16:19:12 -0500837 debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -0400838 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200839
840 return 0;
841}
842
843static void nbd_dev_dbg_close(struct nbd_device *nbd)
844{
845 debugfs_remove_recursive(nbd->dbg_dir);
846}
847
848static int nbd_dbg_init(void)
849{
850 struct dentry *dbg_dir;
851
852 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200853 if (!dbg_dir)
854 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200855
856 nbd_dbg_dir = dbg_dir;
857
858 return 0;
859}
860
861static void nbd_dbg_close(void)
862{
863 debugfs_remove_recursive(nbd_dbg_dir);
864}
865
866#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
867
868static int nbd_dev_dbg_init(struct nbd_device *nbd)
869{
870 return 0;
871}
872
873static void nbd_dev_dbg_close(struct nbd_device *nbd)
874{
875}
876
877static int nbd_dbg_init(void)
878{
879 return 0;
880}
881
882static void nbd_dbg_close(void)
883{
884}
885
886#endif
887
Josef Bacikfd8383f2016-09-08 12:33:37 -0700888static int nbd_init_request(void *data, struct request *rq,
889 unsigned int hctx_idx, unsigned int request_idx,
890 unsigned int numa_node)
891{
892 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
893
894 cmd->nbd = data;
895 INIT_LIST_HEAD(&cmd->list);
896 return 0;
897}
898
899static struct blk_mq_ops nbd_mq_ops = {
900 .queue_rq = nbd_queue_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700901 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -0700902 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700903};
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/*
906 * And here should be modules and kernel interface
907 * (Just smiley confuses emacs :-)
908 */
909
910static int __init nbd_init(void)
911{
912 int err = -ENOMEM;
913 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700914 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800916 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700918 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +0200919 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700920 return -EINVAL;
921 }
922
923 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +0200924 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700925 part_shift = fls(max_part);
926
Namhyung Kim5988ce22011-05-28 14:44:46 +0200927 /*
928 * Adjust max_part according to part_shift as it is exported
929 * to user space so that user can know the max number of
930 * partition kernel should be able to manage.
931 *
932 * Note that -1 is required because partition 0 is reserved
933 * for the whole disk.
934 */
935 max_part = (1UL << part_shift) - 1;
936 }
937
Namhyung Kim3b271082011-05-28 14:44:46 +0200938 if ((1UL << part_shift) > DISK_MAX_PARTS)
939 return -EINVAL;
940
941 if (nbds_max > 1UL << (MINORBITS - part_shift))
942 return -EINVAL;
943
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +0530944 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
945 if (!nbd_dev)
946 return -ENOMEM;
947
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700948 for (i = 0; i < nbds_max; i++) {
Jeff Moyer952d07a2017-01-09 15:20:31 -0500949 struct request_queue *q;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700950 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!disk)
952 goto out;
953 nbd_dev[i].disk = disk;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700954
955 nbd_dev[i].tag_set.ops = &nbd_mq_ops;
956 nbd_dev[i].tag_set.nr_hw_queues = 1;
957 nbd_dev[i].tag_set.queue_depth = 128;
958 nbd_dev[i].tag_set.numa_node = NUMA_NO_NODE;
959 nbd_dev[i].tag_set.cmd_size = sizeof(struct nbd_cmd);
960 nbd_dev[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Josef Bacik005043a2016-09-21 16:55:31 -0400961 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700962 nbd_dev[i].tag_set.driver_data = &nbd_dev[i];
963
964 err = blk_mq_alloc_tag_set(&nbd_dev[i].tag_set);
965 if (err) {
966 put_disk(disk);
967 goto out;
968 }
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
971 * The new linux 2.5 block layer implementation requires
972 * every gendisk to have its very own request_queue struct.
973 * These structs are big so we dynamically allocate them.
974 */
Jeff Moyer952d07a2017-01-09 15:20:31 -0500975 q = blk_mq_init_queue(&nbd_dev[i].tag_set);
976 if (IS_ERR(q)) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700977 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 put_disk(disk);
979 goto out;
980 }
Jeff Moyer952d07a2017-01-09 15:20:31 -0500981 disk->queue = q;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700982
Jens Axboe31dcfab2008-10-31 10:06:37 +0100983 /*
984 * Tell the block layer that we are not a rotational device
985 */
986 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600987 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -0700988 disk->queue->limits.discard_granularity = 512;
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600989 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Paul Clementsa336d292012-10-04 17:16:18 -0700990 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -0700991 blk_queue_max_hw_sectors(disk->queue, 65536);
992 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 if (register_blkdev(NBD_MAJOR, "nbd")) {
996 err = -EIO;
997 goto out;
998 }
999
1000 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Markus Pargmann30d53d92015-08-17 08:20:06 +02001002 nbd_dbg_init();
1003
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001004 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -07001006 nbd_dev[i].magic = NBD_MAGIC;
Markus Pargmann23272a672015-10-29 11:51:16 +01001007 spin_lock_init(&nbd_dev[i].sock_lock);
Ingo Molnar82d4dc52006-03-23 03:00:38 -08001008 mutex_init(&nbd_dev[i].tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001010 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 disk->fops = &nbd_fops;
1012 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sprintf(disk->disk_name, "nbd%d", i);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001014 nbd_reset(&nbd_dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 add_disk(disk);
1016 }
1017
1018 return 0;
1019out:
1020 while (i--) {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001021 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 blk_cleanup_queue(nbd_dev[i].disk->queue);
1023 put_disk(nbd_dev[i].disk);
1024 }
Sven Wegenerf3944d62008-08-20 14:09:07 -07001025 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return err;
1027}
1028
1029static void __exit nbd_cleanup(void)
1030{
1031 int i;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001032
1033 nbd_dbg_close();
1034
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001035 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001037 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (disk) {
1039 del_gendisk(disk);
1040 blk_cleanup_queue(disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001041 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 put_disk(disk);
1043 }
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -07001046 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
1048}
1049
1050module_init(nbd_init);
1051module_exit(nbd_cleanup);
1052
1053MODULE_DESCRIPTION("Network Block Device");
1054MODULE_LICENSE("GPL");
1055
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001056module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001057MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1058module_param(max_part, int, 0444);
1059MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");