blob: 394f8ec83cf02444083688650688f8f9773f2b10 [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{
Josef Bacikeb108752017-02-13 10:39:47 -0500111 bd_set_size(bdev, 0);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200112 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{
Josef Bacikeb108752017-02-13 10:39:47 -0500120 blk_queue_logical_block_size(nbd->disk->queue, nbd->blksize);
121 blk_queue_physical_block_size(nbd->disk->queue, nbd->blksize);
122 bd_set_size(bdev, nbd->bytesize);
Jan Karaf3fc8892019-01-14 09:48:09 +0100123 set_blocksize(bdev, nbd->blksize);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200124 set_capacity(nbd->disk, nbd->bytesize >> 9);
125 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
126}
127
Josef Bacikeb108752017-02-13 10:39:47 -0500128static void 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{
Markus Pargmann37091fd2015-07-27 07:36:49 +0200131 nbd->blksize = blocksize;
Josef Bacik75991662016-12-02 16:19:12 -0500132 nbd->bytesize = blocksize * nr_blocks;
Josef Bacikeb108752017-02-13 10:39:47 -0500133 if (nbd_is_connected(nbd))
134 nbd_size_update(nbd, bdev);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200135}
136
Josef Bacikfd8383f2016-09-08 12:33:37 -0700137static void nbd_end_request(struct nbd_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700139 struct nbd_device *nbd = cmd->nbd;
140 struct request *req = blk_mq_rq_from_pdu(cmd);
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500141 int error = req->errors ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Josef Bacikfd8383f2016-09-08 12:33:37 -0700143 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", cmd,
Markus Pargmannd18509f2015-04-02 10:11:38 +0200144 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Josef Bacikfd8383f2016-09-08 12:33:37 -0700146 blk_mq_complete_request(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Markus Pargmanne018e752015-04-02 10:11:39 +0200149/*
150 * Forcibly shutdown the socket causing all listeners to error
151 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200152static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700153{
Josef Bacikc2611892016-09-08 12:33:38 -0700154 struct socket *sock;
155
Josef Bacik0eadf372016-09-08 12:33:40 -0700156 spin_lock(&nbd->sock_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100157
158 if (!nbd->sock) {
John W. Linville423221d2016-10-24 15:13:25 -0400159 spin_unlock(&nbd->sock_lock);
Markus Pargmann260bbce2015-08-17 08:20:02 +0200160 return;
Markus Pargmann23272a672015-10-29 11:51:16 +0100161 }
Markus Pargmann260bbce2015-08-17 08:20:02 +0200162
Josef Bacikc2611892016-09-08 12:33:38 -0700163 sock = nbd->sock;
Markus Pargmann260bbce2015-08-17 08:20:02 +0200164 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
Markus Pargmann260bbce2015-08-17 08:20:02 +0200165 nbd->sock = NULL;
Josef Bacik0eadf372016-09-08 12:33:40 -0700166 spin_unlock(&nbd->sock_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100167
Josef Bacikc2611892016-09-08 12:33:38 -0700168 kernel_sock_shutdown(sock, SHUT_RDWR);
169 sockfd_put(sock);
Paul Clements7fdfd402007-10-16 23:27:37 -0700170}
171
Josef Bacik0eadf372016-09-08 12:33:40 -0700172static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
173 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700174{
Josef Bacik0eadf372016-09-08 12:33:40 -0700175 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
176 struct nbd_device *nbd = cmd->nbd;
Josef Bacikc2611892016-09-08 12:33:38 -0700177 struct socket *sock = NULL;
Paul Clements7fdfd402007-10-16 23:27:37 -0700178
Josef Bacik0eadf372016-09-08 12:33:40 -0700179 spin_lock(&nbd->sock_lock);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200180
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700181 set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200182
Josef Bacikc2611892016-09-08 12:33:38 -0700183 if (nbd->sock) {
184 sock = nbd->sock;
185 get_file(sock->file);
186 }
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200187
Josef Bacik0eadf372016-09-08 12:33:40 -0700188 spin_unlock(&nbd->sock_lock);
Josef Bacikc2611892016-09-08 12:33:38 -0700189 if (sock) {
190 kernel_sock_shutdown(sock, SHUT_RDWR);
191 sockfd_put(sock);
192 }
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200193
Josef Bacik0eadf372016-09-08 12:33:40 -0700194 req->errors++;
Markus Pargmann23272a672015-10-29 11:51:16 +0100195 dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
Josef Bacik0eadf372016-09-08 12:33:40 -0700196 return BLK_EH_HANDLED;
Paul Clements7fdfd402007-10-16 23:27:37 -0700197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * Send or receive packet.
201 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700202static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int msg_flags)
204{
Wanlong Gaof4507162012-03-28 14:42:51 -0700205 struct socket *sock = nbd->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int result;
207 struct msghdr msg;
208 struct kvec iov;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700209 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700211 if (unlikely(!sock)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700212 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200213 "Attempted %s on closed socket in sock_xmit\n",
214 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700215 return -EINVAL;
216 }
217
Mel Gorman7f338fe2012-07-31 16:44:32 -0700218 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700220 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 iov.iov_base = buf;
222 iov.iov_len = size;
223 msg.msg_name = NULL;
224 msg.msg_namelen = 0;
225 msg.msg_control = NULL;
226 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
228
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200229 if (send)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200231 else
Namhyung Kim35fbf5b2011-05-28 14:44:46 +0200232 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
233 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (result <= 0) {
236 if (result == 0)
237 result = -EPIPE; /* short read */
238 break;
239 }
240 size -= result;
241 buf += result;
242 } while (size > 0);
243
Mel Gorman7f338fe2012-07-31 16:44:32 -0700244 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 return result;
247}
248
Wanlong Gaof4507162012-03-28 14:42:51 -0700249static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int flags)
251{
252 int result;
253 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700254 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
255 bvec->bv_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 kunmap(bvec->bv_page);
257 return result;
258}
259
Paul Clements7fdfd402007-10-16 23:27:37 -0700260/* always call with the tx_lock held */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700261static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700263 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik4b7c09a2017-01-19 16:08:49 -0500264 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct nbd_request request;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900266 unsigned long size = blk_rq_bytes(req);
Jens Axboe3a381ab2016-11-17 12:30:37 -0700267 struct bio *bio;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200268 u32 type;
269
270 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
271 type = NBD_CMD_DISC;
Mike Christiec2df40d2016-06-05 14:32:17 -0500272 else if (req_op(req) == REQ_OP_DISCARD)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200273 type = NBD_CMD_TRIM;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500274 else if (req_op(req) == REQ_OP_FLUSH)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200275 type = NBD_CMD_FLUSH;
276 else if (rq_data_dir(req) == WRITE)
277 type = NBD_CMD_WRITE;
278 else
279 type = NBD_CMD_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700281 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 request.magic = htonl(NBD_REQUEST_MAGIC);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200283 request.type = htonl(type);
284 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800285 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
286 request.len = htonl(size);
287 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700288 memcpy(request.handle, &req->tag, sizeof(req->tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Markus Pargmannd18509f2015-04-02 10:11:38 +0200290 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700291 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200292 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700293 result = sock_xmit(nbd, 1, &request, sizeof(request),
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200294 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700296 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200297 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200298 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Jens Axboe3a381ab2016-11-17 12:30:37 -0700301 if (type != NBD_CMD_WRITE)
302 return 0;
303
Jens Axboe3a381ab2016-11-17 12:30:37 -0700304 bio = req->bio;
305 while (bio) {
306 struct bio *next = bio->bi_next;
307 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800308 struct bio_vec bvec;
Jens Axboe3a381ab2016-11-17 12:30:37 -0700309
310 bio_for_each_segment(bvec, bio, iter) {
311 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacik4b7c09a2017-01-19 16:08:49 -0500312 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe3a381ab2016-11-17 12:30:37 -0700313
Markus Pargmannd18509f2015-04-02 10:11:38 +0200314 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700315 cmd, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800316 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200317 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700318 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200319 "Send data failed (result %d)\n",
320 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200321 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200322 }
Jens Axboe3a381ab2016-11-17 12:30:37 -0700323 /*
324 * The completion might already have come in,
325 * so break for the last one instead of letting
326 * the iterator do it. This prevents use-after-free
327 * of the bio.
328 */
329 if (is_last)
330 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Jens Axboe3a381ab2016-11-17 12:30:37 -0700332 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Wanlong Gaof4507162012-03-28 14:42:51 -0700337static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 int result;
340 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700341 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 MSG_WAITALL);
343 kunmap(bvec->bv_page);
344 return result;
345}
346
347/* NULL returned = something went wrong, inform userspace */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700348static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 int result;
351 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700352 struct nbd_cmd *cmd;
353 struct request *req = NULL;
354 u16 hwq;
355 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700358 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700360 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200361 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200362 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700364
365 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700366 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700367 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200368 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700369 }
370
Josef Bacikfd8383f2016-09-08 12:33:37 -0700371 memcpy(&tag, reply.handle, sizeof(int));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800372
Josef Bacikfd8383f2016-09-08 12:33:37 -0700373 hwq = blk_mq_unique_tag_to_hwq(tag);
374 if (hwq < nbd->tag_set.nr_hw_queues)
375 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
376 blk_mq_unique_tag_to_tag(tag));
377 if (!req || !blk_mq_request_started(req)) {
378 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
379 tag, req);
380 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700382 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700385 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200386 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700388 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Josef Bacikfd8383f2016-09-08 12:33:37 -0700391 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200392 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200393 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800394 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200395
396 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800397 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200398 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700399 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200400 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200401 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700402 return cmd;
Jens Axboe6c92e692007-08-16 13:43:12 +0200403 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200404 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700405 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700408 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200411static ssize_t pid_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800413{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200414 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200415 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200416
Markus Pargmann6521d392015-08-17 08:20:05 +0200417 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800418}
419
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200420static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700421 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800422 .show = pid_show,
423};
424
Markus Pargmann37091fd2015-07-27 07:36:49 +0200425static int nbd_thread_recv(struct nbd_device *nbd, struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700427 struct nbd_cmd *cmd;
WANG Cong84963042007-05-09 02:33:36 -0700428 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Wanlong Gaof4507162012-03-28 14:42:51 -0700430 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Mel Gorman7f338fe2012-07-31 16:44:32 -0700432 sk_set_memalloc(nbd->sock->sk);
Markus Pargmann6521d392015-08-17 08:20:05 +0200433
Wanlong Gaof4507162012-03-28 14:42:51 -0700434 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700435 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700436 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
WANG Cong84963042007-05-09 02:33:36 -0700437 return ret;
438 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800439
Markus Pargmann37091fd2015-07-27 07:36:49 +0200440 nbd_size_update(nbd, bdev);
441
Markus Pargmann19391832015-08-17 08:20:03 +0200442 while (1) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700443 cmd = nbd_read_stat(nbd);
444 if (IS_ERR(cmd)) {
445 ret = PTR_ERR(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200446 break;
447 }
448
Josef Bacikfd8383f2016-09-08 12:33:37 -0700449 nbd_end_request(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200450 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800451
Markus Pargmann37091fd2015-07-27 07:36:49 +0200452 nbd_size_clear(nbd, bdev);
453
Markus Pargmann6521d392015-08-17 08:20:05 +0200454 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200455 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Josef Bacikfd8383f2016-09-08 12:33:37 -0700458static void nbd_clear_req(struct request *req, void *data, bool reserved)
459{
460 struct nbd_cmd *cmd;
461
462 if (!blk_mq_request_started(req))
463 return;
464 cmd = blk_mq_rq_to_pdu(req);
465 req->errors++;
466 nbd_end_request(cmd);
467}
468
Wanlong Gaof4507162012-03-28 14:42:51 -0700469static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Wanlong Gaof4507162012-03-28 14:42:51 -0700471 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Herbert Xu4b2f0262006-01-06 00:09:47 -0800473 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700474 * Because we have set nbd->sock to NULL under the tx_lock, all
Josef Bacikfd8383f2016-09-08 12:33:37 -0700475 * modifications to the list must have completed by now.
Herbert Xu4b2f0262006-01-06 00:09:47 -0800476 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700477 BUG_ON(nbd->sock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800478
Josef Bacikfd8383f2016-09-08 12:33:37 -0700479 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200480 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Paul Clements7fdfd402007-10-16 23:27:37 -0700483
Josef Bacikfd8383f2016-09-08 12:33:37 -0700484static void nbd_handle_cmd(struct nbd_cmd *cmd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700485{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700486 struct request *req = blk_mq_rq_from_pdu(cmd);
487 struct nbd_device *nbd = cmd->nbd;
488
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200489 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700490 goto error_out;
491
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200492 if (rq_data_dir(req) == WRITE &&
493 (nbd->flags & NBD_FLAG_READ_ONLY)) {
494 dev_err(disk_to_dev(nbd->disk),
495 "Write on read-only\n");
496 goto error_out;
Alex Bligh75f187a2013-02-27 17:05:23 -0800497 }
498
Laurent Vivier48cf6062008-04-29 01:02:46 -0700499 req->errors = 0;
500
Wanlong Gaof4507162012-03-28 14:42:51 -0700501 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700502 nbd->task_send = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700503 if (unlikely(!nbd->sock)) {
504 mutex_unlock(&nbd->tx_lock);
505 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200506 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700507 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700508 }
509
Josef Bacikfd8383f2016-09-08 12:33:37 -0700510 if (nbd_send_cmd(nbd, cmd) != 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700511 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700512 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700513 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700514 }
515
Josef Bacikfd8383f2016-09-08 12:33:37 -0700516 nbd->task_send = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700517 mutex_unlock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700518
519 return;
520
521error_out:
522 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 -0700526static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
527 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700528{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700529 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700530
Josef Bacikfd8383f2016-09-08 12:33:37 -0700531 blk_mq_start_request(bd->rq);
532 nbd_handle_cmd(cmd);
533 return BLK_MQ_RQ_QUEUE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Markus Pargmann23272a672015-10-29 11:51:16 +0100536static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
537{
538 int ret = 0;
539
540 spin_lock_irq(&nbd->sock_lock);
541
542 if (nbd->sock) {
543 ret = -EBUSY;
544 goto out;
545 }
546
547 nbd->sock = sock;
548
549out:
550 spin_unlock_irq(&nbd->sock_lock);
551
552 return ret;
553}
554
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100555/* Reset all properties of an NBD device */
556static void nbd_reset(struct nbd_device *nbd)
557{
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700558 nbd->runtime_flags = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100559 nbd->blksize = 1024;
560 nbd->bytesize = 0;
561 set_capacity(nbd->disk, 0);
562 nbd->flags = 0;
Josef Bacik0eadf372016-09-08 12:33:40 -0700563 nbd->tag_set.timeout = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100564 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100565}
566
567static void nbd_bdev_reset(struct block_device *bdev)
568{
569 set_device_ro(bdev, false);
570 bdev->bd_inode->i_size = 0;
571 if (max_part > 0) {
572 blkdev_reread_part(bdev);
573 bdev->bd_invalidated = 1;
574 }
575}
576
Markus Pargmannd02cf532015-10-29 12:06:15 +0100577static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
578{
579 if (nbd->flags & NBD_FLAG_READ_ONLY)
580 set_device_ro(bdev, true);
581 if (nbd->flags & NBD_FLAG_SEND_TRIM)
582 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
583 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600584 blk_queue_write_cache(nbd->disk->queue, true, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100585 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600586 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100587}
588
Markus Pargmann30d53d92015-08-17 08:20:06 +0200589static int nbd_dev_dbg_init(struct nbd_device *nbd);
590static void nbd_dev_dbg_close(struct nbd_device *nbd);
591
Pavel Machek1a2ad212009-04-02 16:58:41 -0700592/* Must be called with tx_lock held */
593
Wanlong Gaof4507162012-03-28 14:42:51 -0700594static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700595 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700598 case NBD_DISCONNECT: {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700599 struct request *sreq;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700600
Wanlong Gaof4507162012-03-28 14:42:51 -0700601 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800602 if (!nbd->sock)
603 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700604
Josef Bacikfd8383f2016-09-08 12:33:37 -0700605 sreq = blk_mq_alloc_request(bdev_get_queue(bdev), WRITE, 0);
Christophe JAILLET77291852016-10-30 05:28:27 +0100606 if (IS_ERR(sreq))
Josef Bacikfd8383f2016-09-08 12:33:37 -0700607 return -ENOMEM;
608
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800609 mutex_unlock(&nbd->tx_lock);
610 fsync_bdev(bdev);
611 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700612 sreq->cmd_type = REQ_TYPE_DRV_PRIV;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800613
614 /* Check again after getting mutex back. */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700615 if (!nbd->sock) {
616 blk_mq_free_request(sreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EINVAL;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700618 }
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800619
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700620 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags);
Paul Clementsc378f702013-07-03 15:09:04 -0700621
Josef Bacikfd8383f2016-09-08 12:33:37 -0700622 nbd_send_cmd(nbd, blk_mq_rq_to_pdu(sreq));
623 blk_mq_free_request(sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700624 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Markus Pargmann23272a672015-10-29 11:51:16 +0100627 case NBD_CLEAR_SOCK:
628 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700629 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800630 kill_bdev(bdev);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700631 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700632
633 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500634 int err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100635 struct socket *sock = sockfd_lookup(arg, &err);
636
637 if (!sock)
638 return err;
639
640 err = nbd_set_socket(nbd, sock);
641 if (!err && max_part)
642 bdev->bd_invalidated = 1;
643
644 return err;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700645 }
646
Markus Pargmann37091fd2015-07-27 07:36:49 +0200647 case NBD_SET_BLKSIZE: {
Arnd Bergmann5e454c62016-03-05 00:49:31 +0100648 loff_t bsize = div_s64(nbd->bytesize, arg);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200649
Josef Bacikeb108752017-02-13 10:39:47 -0500650 nbd_size_set(nbd, bdev, arg, bsize);
651 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +0200652 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 case NBD_SET_SIZE:
Josef Bacikeb108752017-02-13 10:39:47 -0500655 nbd_size_set(nbd, bdev, nbd->blksize,
656 div_s64(arg, nbd->blksize));
657 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +0200658 case NBD_SET_SIZE_BLOCKS:
Josef Bacikeb108752017-02-13 10:39:47 -0500659 nbd_size_set(nbd, bdev, nbd->blksize, arg);
660 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -0700661 case NBD_SET_TIMEOUT:
Josef Bacik521a7e32017-03-24 14:08:28 -0400662 if (arg) {
663 nbd->tag_set.timeout = arg * HZ;
664 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
665 }
Paul Clements7fdfd402007-10-16 23:27:37 -0700666 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700667
Paul Clements2f012502012-10-04 17:16:15 -0700668 case NBD_SET_FLAGS:
669 nbd->flags = arg;
670 return 0;
671
Pavel Machek1a2ad212009-04-02 16:58:41 -0700672 case NBD_DO_IT: {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700673 int error;
674
Markus Pargmann6521d392015-08-17 08:20:05 +0200675 if (nbd->task_recv)
Pavel Machekc91192d2009-01-15 13:51:03 -0800676 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500677 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700679
Vegard Nossum97240962016-05-27 12:59:35 +0200680 /* We have to claim the device under the lock */
681 nbd->task_recv = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700682 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700683
Markus Pargmannd02cf532015-10-29 12:06:15 +0100684 nbd_parse_flags(nbd, bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700685
Markus Pargmann30d53d92015-08-17 08:20:06 +0200686 nbd_dev_dbg_init(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200687 error = nbd_thread_recv(nbd, bdev);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200688 nbd_dev_dbg_close(nbd);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700689
Wanlong Gaof4507162012-03-28 14:42:51 -0700690 mutex_lock(&nbd->tx_lock);
Vegard Nossum97240962016-05-27 12:59:35 +0200691 nbd->task_recv = NULL;
Markus Pargmann19391832015-08-17 08:20:03 +0200692
Markus Pargmann36e47be2015-08-17 08:20:01 +0200693 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700694 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800695 kill_bdev(bdev);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100696 nbd_bdev_reset(bdev);
697
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700698 /* user requested, ignore socket errors */
699 if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100700 error = 0;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700701 if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100702 error = -ETIMEDOUT;
703
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100704 nbd_reset(nbd);
705
Markus Pargmann19391832015-08-17 08:20:03 +0200706 return error;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700707 }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800710 /*
711 * This is for compatibility only. The queue is always cleared
712 * by NBD_DO_IT or NBD_CLEAR_SOCK.
713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -0700717 /*
718 * For compatibility only, we no longer keep a list of
719 * outstanding requests.
720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700723 return -ENOTTY;
724}
725
726static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
727 unsigned int cmd, unsigned long arg)
728{
Wanlong Gaof4507162012-03-28 14:42:51 -0700729 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700730 int error;
731
732 if (!capable(CAP_SYS_ADMIN))
733 return -EPERM;
734
Wanlong Gaof4507162012-03-28 14:42:51 -0700735 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700736
Wanlong Gaof4507162012-03-28 14:42:51 -0700737 mutex_lock(&nbd->tx_lock);
738 error = __nbd_ioctl(bdev, nbd, cmd, arg);
739 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700740
741 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700744static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200747 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500748 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749};
750
Markus Pargmann30d53d92015-08-17 08:20:06 +0200751#if IS_ENABLED(CONFIG_DEBUG_FS)
752
753static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
754{
755 struct nbd_device *nbd = s->private;
756
757 if (nbd->task_recv)
758 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
759 if (nbd->task_send)
760 seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send));
761
762 return 0;
763}
764
765static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
766{
767 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
768}
769
770static const struct file_operations nbd_dbg_tasks_ops = {
771 .open = nbd_dbg_tasks_open,
772 .read = seq_read,
773 .llseek = seq_lseek,
774 .release = single_release,
775};
776
777static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
778{
779 struct nbd_device *nbd = s->private;
780 u32 flags = nbd->flags;
781
782 seq_printf(s, "Hex: 0x%08x\n\n", flags);
783
784 seq_puts(s, "Known flags:\n");
785
786 if (flags & NBD_FLAG_HAS_FLAGS)
787 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
788 if (flags & NBD_FLAG_READ_ONLY)
789 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
790 if (flags & NBD_FLAG_SEND_FLUSH)
791 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
792 if (flags & NBD_FLAG_SEND_TRIM)
793 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
794
795 return 0;
796}
797
798static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
799{
800 return single_open(file, nbd_dbg_flags_show, inode->i_private);
801}
802
803static const struct file_operations nbd_dbg_flags_ops = {
804 .open = nbd_dbg_flags_open,
805 .read = seq_read,
806 .llseek = seq_lseek,
807 .release = single_release,
808};
809
810static int nbd_dev_dbg_init(struct nbd_device *nbd)
811{
812 struct dentry *dir;
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200813
814 if (!nbd_dbg_dir)
815 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200816
817 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200818 if (!dir) {
819 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
820 nbd_name(nbd));
821 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200822 }
823 nbd->dbg_dir = dir;
824
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200825 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
826 debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -0700827 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik75991662016-12-02 16:19:12 -0500828 debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -0400829 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200830
831 return 0;
832}
833
834static void nbd_dev_dbg_close(struct nbd_device *nbd)
835{
836 debugfs_remove_recursive(nbd->dbg_dir);
837}
838
839static int nbd_dbg_init(void)
840{
841 struct dentry *dbg_dir;
842
843 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200844 if (!dbg_dir)
845 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200846
847 nbd_dbg_dir = dbg_dir;
848
849 return 0;
850}
851
852static void nbd_dbg_close(void)
853{
854 debugfs_remove_recursive(nbd_dbg_dir);
855}
856
857#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
858
859static int nbd_dev_dbg_init(struct nbd_device *nbd)
860{
861 return 0;
862}
863
864static void nbd_dev_dbg_close(struct nbd_device *nbd)
865{
866}
867
868static int nbd_dbg_init(void)
869{
870 return 0;
871}
872
873static void nbd_dbg_close(void)
874{
875}
876
877#endif
878
Josef Bacikfd8383f2016-09-08 12:33:37 -0700879static int nbd_init_request(void *data, struct request *rq,
880 unsigned int hctx_idx, unsigned int request_idx,
881 unsigned int numa_node)
882{
883 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
884
885 cmd->nbd = data;
886 INIT_LIST_HEAD(&cmd->list);
887 return 0;
888}
889
890static struct blk_mq_ops nbd_mq_ops = {
891 .queue_rq = nbd_queue_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700892 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -0700893 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700894};
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896/*
897 * And here should be modules and kernel interface
898 * (Just smiley confuses emacs :-)
899 */
900
901static int __init nbd_init(void)
902{
903 int err = -ENOMEM;
904 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700905 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800907 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700909 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +0200910 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700911 return -EINVAL;
912 }
913
914 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +0200915 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700916 part_shift = fls(max_part);
917
Namhyung Kim5988ce22011-05-28 14:44:46 +0200918 /*
919 * Adjust max_part according to part_shift as it is exported
920 * to user space so that user can know the max number of
921 * partition kernel should be able to manage.
922 *
923 * Note that -1 is required because partition 0 is reserved
924 * for the whole disk.
925 */
926 max_part = (1UL << part_shift) - 1;
927 }
928
Namhyung Kim3b271082011-05-28 14:44:46 +0200929 if ((1UL << part_shift) > DISK_MAX_PARTS)
930 return -EINVAL;
931
932 if (nbds_max > 1UL << (MINORBITS - part_shift))
933 return -EINVAL;
934
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +0530935 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
936 if (!nbd_dev)
937 return -ENOMEM;
938
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700939 for (i = 0; i < nbds_max; i++) {
Jeff Moyer952d07a2017-01-09 15:20:31 -0500940 struct request_queue *q;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700941 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!disk)
943 goto out;
944 nbd_dev[i].disk = disk;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700945
946 nbd_dev[i].tag_set.ops = &nbd_mq_ops;
947 nbd_dev[i].tag_set.nr_hw_queues = 1;
948 nbd_dev[i].tag_set.queue_depth = 128;
949 nbd_dev[i].tag_set.numa_node = NUMA_NO_NODE;
950 nbd_dev[i].tag_set.cmd_size = sizeof(struct nbd_cmd);
951 nbd_dev[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Josef Bacik005043a2016-09-21 16:55:31 -0400952 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700953 nbd_dev[i].tag_set.driver_data = &nbd_dev[i];
954
955 err = blk_mq_alloc_tag_set(&nbd_dev[i].tag_set);
956 if (err) {
957 put_disk(disk);
958 goto out;
959 }
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /*
962 * The new linux 2.5 block layer implementation requires
963 * every gendisk to have its very own request_queue struct.
964 * These structs are big so we dynamically allocate them.
965 */
Jeff Moyer952d07a2017-01-09 15:20:31 -0500966 q = blk_mq_init_queue(&nbd_dev[i].tag_set);
967 if (IS_ERR(q)) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700968 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 put_disk(disk);
970 goto out;
971 }
Jeff Moyer952d07a2017-01-09 15:20:31 -0500972 disk->queue = q;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700973
Jens Axboe31dcfab2008-10-31 10:06:37 +0100974 /*
975 * Tell the block layer that we are not a rotational device
976 */
977 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600978 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -0700979 disk->queue->limits.discard_granularity = 512;
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600980 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Paul Clementsa336d292012-10-04 17:16:18 -0700981 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -0700982 blk_queue_max_hw_sectors(disk->queue, 65536);
983 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 if (register_blkdev(NBD_MAJOR, "nbd")) {
987 err = -EIO;
988 goto out;
989 }
990
991 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Markus Pargmann30d53d92015-08-17 08:20:06 +0200993 nbd_dbg_init();
994
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700995 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -0700997 nbd_dev[i].magic = NBD_MAGIC;
Markus Pargmann23272a672015-10-29 11:51:16 +0100998 spin_lock_init(&nbd_dev[i].sock_lock);
Ingo Molnar82d4dc52006-03-23 03:00:38 -0800999 mutex_init(&nbd_dev[i].tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001001 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 disk->fops = &nbd_fops;
1003 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sprintf(disk->disk_name, "nbd%d", i);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001005 nbd_reset(&nbd_dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 add_disk(disk);
1007 }
1008
1009 return 0;
1010out:
1011 while (i--) {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001012 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 blk_cleanup_queue(nbd_dev[i].disk->queue);
1014 put_disk(nbd_dev[i].disk);
1015 }
Sven Wegenerf3944d62008-08-20 14:09:07 -07001016 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return err;
1018}
1019
1020static void __exit nbd_cleanup(void)
1021{
1022 int i;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001023
1024 nbd_dbg_close();
1025
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001026 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001028 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (disk) {
1030 del_gendisk(disk);
1031 blk_cleanup_queue(disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001032 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 put_disk(disk);
1034 }
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -07001037 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
1039}
1040
1041module_init(nbd_init);
1042module_exit(nbd_cleanup);
1043
1044MODULE_DESCRIPTION("Network Block Device");
1045MODULE_LICENSE("GPL");
1046
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001047module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001048MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1049module_param(max_part, int, 0444);
1050MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");