blob: 98b767d3171e91fb59f4b9c53d507c96266b0b02 [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);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200275 u32 type;
276
277 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
278 type = NBD_CMD_DISC;
Mike Christiec2df40d2016-06-05 14:32:17 -0500279 else if (req_op(req) == REQ_OP_DISCARD)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200280 type = NBD_CMD_TRIM;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500281 else if (req_op(req) == REQ_OP_FLUSH)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200282 type = NBD_CMD_FLUSH;
283 else if (rq_data_dir(req) == WRITE)
284 type = NBD_CMD_WRITE;
285 else
286 type = NBD_CMD_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700288 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 request.magic = htonl(NBD_REQUEST_MAGIC);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200290 request.type = htonl(type);
291 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800292 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
293 request.len = htonl(size);
294 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700295 memcpy(request.handle, &req->tag, sizeof(req->tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Markus Pargmannd18509f2015-04-02 10:11:38 +0200297 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700298 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200299 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700300 result = sock_xmit(nbd, 1, &request, sizeof(request),
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200301 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700303 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200304 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200305 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200308 if (type == NBD_CMD_WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200309 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800310 struct bio_vec bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
312 * we are really probing at internals to determine
313 * whether to set MSG_MORE or not...
314 */
NeilBrown5705f702007-09-25 12:35:59 +0200315 rq_for_each_segment(bvec, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200316 flags = 0;
Kent Overstreet4550dd62013-08-07 14:26:21 -0700317 if (!rq_iter_last(bvec, iter))
Jens Axboe6c92e692007-08-16 13:43:12 +0200318 flags = MSG_MORE;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200319 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700320 cmd, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800321 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200322 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700323 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200324 "Send data failed (result %d)\n",
325 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200326 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Wanlong Gaof4507162012-03-28 14:42:51 -0700333static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int result;
336 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700337 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 MSG_WAITALL);
339 kunmap(bvec->bv_page);
340 return result;
341}
342
343/* NULL returned = something went wrong, inform userspace */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700344static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 int result;
347 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700348 struct nbd_cmd *cmd;
349 struct request *req = NULL;
350 u16 hwq;
351 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700354 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700356 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200357 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200358 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700360
361 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700362 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700363 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200364 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700365 }
366
Josef Bacikfd8383f2016-09-08 12:33:37 -0700367 memcpy(&tag, reply.handle, sizeof(int));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800368
Josef Bacikfd8383f2016-09-08 12:33:37 -0700369 hwq = blk_mq_unique_tag_to_hwq(tag);
370 if (hwq < nbd->tag_set.nr_hw_queues)
371 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
372 blk_mq_unique_tag_to_tag(tag));
373 if (!req || !blk_mq_request_started(req)) {
374 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
375 tag, req);
376 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700378 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700381 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200382 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700384 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Josef Bacikfd8383f2016-09-08 12:33:37 -0700387 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200388 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200389 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800390 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200391
392 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800393 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200394 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700395 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200396 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200397 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700398 return cmd;
Jens Axboe6c92e692007-08-16 13:43:12 +0200399 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200400 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700401 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700404 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200407static ssize_t pid_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800409{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200410 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200411 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200412
Markus Pargmann6521d392015-08-17 08:20:05 +0200413 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800414}
415
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200416static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700417 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800418 .show = pid_show,
419};
420
Markus Pargmann37091fd2015-07-27 07:36:49 +0200421static int nbd_thread_recv(struct nbd_device *nbd, struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700423 struct nbd_cmd *cmd;
WANG Cong84963042007-05-09 02:33:36 -0700424 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Wanlong Gaof4507162012-03-28 14:42:51 -0700426 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Mel Gorman7f338fe2012-07-31 16:44:32 -0700428 sk_set_memalloc(nbd->sock->sk);
Markus Pargmann6521d392015-08-17 08:20:05 +0200429
Wanlong Gaof4507162012-03-28 14:42:51 -0700430 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700431 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700432 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
WANG Cong84963042007-05-09 02:33:36 -0700433 return ret;
434 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800435
Markus Pargmann37091fd2015-07-27 07:36:49 +0200436 nbd_size_update(nbd, bdev);
437
Markus Pargmann19391832015-08-17 08:20:03 +0200438 while (1) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700439 cmd = nbd_read_stat(nbd);
440 if (IS_ERR(cmd)) {
441 ret = PTR_ERR(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200442 break;
443 }
444
Josef Bacikfd8383f2016-09-08 12:33:37 -0700445 nbd_end_request(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200446 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800447
Markus Pargmann37091fd2015-07-27 07:36:49 +0200448 nbd_size_clear(nbd, bdev);
449
Markus Pargmann6521d392015-08-17 08:20:05 +0200450 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Josef Bacikfd8383f2016-09-08 12:33:37 -0700454static void nbd_clear_req(struct request *req, void *data, bool reserved)
455{
456 struct nbd_cmd *cmd;
457
458 if (!blk_mq_request_started(req))
459 return;
460 cmd = blk_mq_rq_to_pdu(req);
461 req->errors++;
462 nbd_end_request(cmd);
463}
464
Wanlong Gaof4507162012-03-28 14:42:51 -0700465static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Wanlong Gaof4507162012-03-28 14:42:51 -0700467 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Herbert Xu4b2f0262006-01-06 00:09:47 -0800469 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700470 * Because we have set nbd->sock to NULL under the tx_lock, all
Josef Bacikfd8383f2016-09-08 12:33:37 -0700471 * modifications to the list must have completed by now.
Herbert Xu4b2f0262006-01-06 00:09:47 -0800472 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700473 BUG_ON(nbd->sock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800474
Josef Bacikfd8383f2016-09-08 12:33:37 -0700475 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200476 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Paul Clements7fdfd402007-10-16 23:27:37 -0700479
Josef Bacikfd8383f2016-09-08 12:33:37 -0700480static void nbd_handle_cmd(struct nbd_cmd *cmd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700481{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700482 struct request *req = blk_mq_rq_from_pdu(cmd);
483 struct nbd_device *nbd = cmd->nbd;
484
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200485 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700486 goto error_out;
487
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200488 if (rq_data_dir(req) == WRITE &&
489 (nbd->flags & NBD_FLAG_READ_ONLY)) {
490 dev_err(disk_to_dev(nbd->disk),
491 "Write on read-only\n");
492 goto error_out;
Alex Bligh75f187a2013-02-27 17:05:23 -0800493 }
494
Laurent Vivier48cf6062008-04-29 01:02:46 -0700495 req->errors = 0;
496
Wanlong Gaof4507162012-03-28 14:42:51 -0700497 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700498 nbd->task_send = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700499 if (unlikely(!nbd->sock)) {
500 mutex_unlock(&nbd->tx_lock);
501 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200502 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700503 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700504 }
505
Josef Bacikfd8383f2016-09-08 12:33:37 -0700506 if (nbd_send_cmd(nbd, cmd) != 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700507 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700508 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700509 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700510 }
511
Josef Bacikfd8383f2016-09-08 12:33:37 -0700512 nbd->task_send = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700513 mutex_unlock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700514
515 return;
516
517error_out:
518 req->errors++;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700519 nbd_end_request(cmd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700520}
521
Josef Bacikfd8383f2016-09-08 12:33:37 -0700522static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
523 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700524{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700525 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700526
Josef Bacikfd8383f2016-09-08 12:33:37 -0700527 blk_mq_start_request(bd->rq);
528 nbd_handle_cmd(cmd);
529 return BLK_MQ_RQ_QUEUE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Markus Pargmann23272a672015-10-29 11:51:16 +0100532static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
533{
534 int ret = 0;
535
536 spin_lock_irq(&nbd->sock_lock);
537
538 if (nbd->sock) {
539 ret = -EBUSY;
540 goto out;
541 }
542
543 nbd->sock = sock;
544
545out:
546 spin_unlock_irq(&nbd->sock_lock);
547
548 return ret;
549}
550
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100551/* Reset all properties of an NBD device */
552static void nbd_reset(struct nbd_device *nbd)
553{
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700554 nbd->runtime_flags = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100555 nbd->blksize = 1024;
556 nbd->bytesize = 0;
557 set_capacity(nbd->disk, 0);
558 nbd->flags = 0;
Josef Bacik0eadf372016-09-08 12:33:40 -0700559 nbd->tag_set.timeout = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100560 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100561}
562
563static void nbd_bdev_reset(struct block_device *bdev)
564{
565 set_device_ro(bdev, false);
566 bdev->bd_inode->i_size = 0;
567 if (max_part > 0) {
568 blkdev_reread_part(bdev);
569 bdev->bd_invalidated = 1;
570 }
571}
572
Markus Pargmannd02cf532015-10-29 12:06:15 +0100573static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
574{
575 if (nbd->flags & NBD_FLAG_READ_ONLY)
576 set_device_ro(bdev, true);
577 if (nbd->flags & NBD_FLAG_SEND_TRIM)
578 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
579 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600580 blk_queue_write_cache(nbd->disk->queue, true, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100581 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600582 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100583}
584
Markus Pargmann30d53d92015-08-17 08:20:06 +0200585static int nbd_dev_dbg_init(struct nbd_device *nbd);
586static void nbd_dev_dbg_close(struct nbd_device *nbd);
587
Pavel Machek1a2ad212009-04-02 16:58:41 -0700588/* Must be called with tx_lock held */
589
Wanlong Gaof4507162012-03-28 14:42:51 -0700590static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700591 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700594 case NBD_DISCONNECT: {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700595 struct request *sreq;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700596
Wanlong Gaof4507162012-03-28 14:42:51 -0700597 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800598 if (!nbd->sock)
599 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700600
Josef Bacikfd8383f2016-09-08 12:33:37 -0700601 sreq = blk_mq_alloc_request(bdev_get_queue(bdev), WRITE, 0);
Christophe JAILLET77291852016-10-30 05:28:27 +0100602 if (IS_ERR(sreq))
Josef Bacikfd8383f2016-09-08 12:33:37 -0700603 return -ENOMEM;
604
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800605 mutex_unlock(&nbd->tx_lock);
606 fsync_bdev(bdev);
607 mutex_lock(&nbd->tx_lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700608 sreq->cmd_type = REQ_TYPE_DRV_PRIV;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800609
610 /* Check again after getting mutex back. */
Josef Bacikfd8383f2016-09-08 12:33:37 -0700611 if (!nbd->sock) {
612 blk_mq_free_request(sreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EINVAL;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700614 }
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800615
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700616 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags);
Paul Clementsc378f702013-07-03 15:09:04 -0700617
Josef Bacikfd8383f2016-09-08 12:33:37 -0700618 nbd_send_cmd(nbd, blk_mq_rq_to_pdu(sreq));
619 blk_mq_free_request(sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700620 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Markus Pargmann23272a672015-10-29 11:51:16 +0100623 case NBD_CLEAR_SOCK:
624 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700625 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800626 kill_bdev(bdev);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700627 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700628
629 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500630 int err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100631 struct socket *sock = sockfd_lookup(arg, &err);
632
633 if (!sock)
634 return err;
635
636 err = nbd_set_socket(nbd, sock);
637 if (!err && max_part)
638 bdev->bd_invalidated = 1;
639
640 return err;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700641 }
642
Markus Pargmann37091fd2015-07-27 07:36:49 +0200643 case NBD_SET_BLKSIZE: {
Arnd Bergmann5e454c62016-03-05 00:49:31 +0100644 loff_t bsize = div_s64(nbd->bytesize, arg);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200645
646 return nbd_size_set(nbd, bdev, arg, bsize);
647 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 case NBD_SET_SIZE:
Markus Pargmann37091fd2015-07-27 07:36:49 +0200650 return nbd_size_set(nbd, bdev, nbd->blksize,
Jens Axboe25640e72016-12-03 12:08:03 -0700651 div_s64(arg, nbd->blksize));
Markus Pargmann37091fd2015-07-27 07:36:49 +0200652
653 case NBD_SET_SIZE_BLOCKS:
654 return nbd_size_set(nbd, bdev, nbd->blksize, arg);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700655
Paul Clements7fdfd402007-10-16 23:27:37 -0700656 case NBD_SET_TIMEOUT:
Josef Bacik0eadf372016-09-08 12:33:40 -0700657 nbd->tag_set.timeout = arg * HZ;
Paul Clements7fdfd402007-10-16 23:27:37 -0700658 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700659
Paul Clements2f012502012-10-04 17:16:15 -0700660 case NBD_SET_FLAGS:
661 nbd->flags = arg;
662 return 0;
663
Pavel Machek1a2ad212009-04-02 16:58:41 -0700664 case NBD_DO_IT: {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700665 int error;
666
Markus Pargmann6521d392015-08-17 08:20:05 +0200667 if (nbd->task_recv)
Pavel Machekc91192d2009-01-15 13:51:03 -0800668 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500669 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700671
Vegard Nossum97240962016-05-27 12:59:35 +0200672 /* We have to claim the device under the lock */
673 nbd->task_recv = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700674 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700675
Markus Pargmannd02cf532015-10-29 12:06:15 +0100676 nbd_parse_flags(nbd, bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700677
Markus Pargmann30d53d92015-08-17 08:20:06 +0200678 nbd_dev_dbg_init(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200679 error = nbd_thread_recv(nbd, bdev);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200680 nbd_dev_dbg_close(nbd);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700681
Wanlong Gaof4507162012-03-28 14:42:51 -0700682 mutex_lock(&nbd->tx_lock);
Vegard Nossum97240962016-05-27 12:59:35 +0200683 nbd->task_recv = NULL;
Markus Pargmann19391832015-08-17 08:20:03 +0200684
Markus Pargmann36e47be2015-08-17 08:20:01 +0200685 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700686 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800687 kill_bdev(bdev);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100688 nbd_bdev_reset(bdev);
689
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700690 /* user requested, ignore socket errors */
691 if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100692 error = 0;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700693 if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags))
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100694 error = -ETIMEDOUT;
695
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100696 nbd_reset(nbd);
697
Markus Pargmann19391832015-08-17 08:20:03 +0200698 return error;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700699 }
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800702 /*
703 * This is for compatibility only. The queue is always cleared
704 * by NBD_DO_IT or NBD_CLEAR_SOCK.
705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -0700709 /*
710 * For compatibility only, we no longer keep a list of
711 * outstanding requests.
712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700715 return -ENOTTY;
716}
717
718static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
719 unsigned int cmd, unsigned long arg)
720{
Wanlong Gaof4507162012-03-28 14:42:51 -0700721 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700722 int error;
723
724 if (!capable(CAP_SYS_ADMIN))
725 return -EPERM;
726
Wanlong Gaof4507162012-03-28 14:42:51 -0700727 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700728
Wanlong Gaof4507162012-03-28 14:42:51 -0700729 mutex_lock(&nbd->tx_lock);
730 error = __nbd_ioctl(bdev, nbd, cmd, arg);
731 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700732
733 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700736static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200739 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500740 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741};
742
Markus Pargmann30d53d92015-08-17 08:20:06 +0200743#if IS_ENABLED(CONFIG_DEBUG_FS)
744
745static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
746{
747 struct nbd_device *nbd = s->private;
748
749 if (nbd->task_recv)
750 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
751 if (nbd->task_send)
752 seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send));
753
754 return 0;
755}
756
757static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
758{
759 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
760}
761
762static const struct file_operations nbd_dbg_tasks_ops = {
763 .open = nbd_dbg_tasks_open,
764 .read = seq_read,
765 .llseek = seq_lseek,
766 .release = single_release,
767};
768
769static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
770{
771 struct nbd_device *nbd = s->private;
772 u32 flags = nbd->flags;
773
774 seq_printf(s, "Hex: 0x%08x\n\n", flags);
775
776 seq_puts(s, "Known flags:\n");
777
778 if (flags & NBD_FLAG_HAS_FLAGS)
779 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
780 if (flags & NBD_FLAG_READ_ONLY)
781 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
782 if (flags & NBD_FLAG_SEND_FLUSH)
783 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
784 if (flags & NBD_FLAG_SEND_TRIM)
785 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
786
787 return 0;
788}
789
790static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
791{
792 return single_open(file, nbd_dbg_flags_show, inode->i_private);
793}
794
795static const struct file_operations nbd_dbg_flags_ops = {
796 .open = nbd_dbg_flags_open,
797 .read = seq_read,
798 .llseek = seq_lseek,
799 .release = single_release,
800};
801
802static int nbd_dev_dbg_init(struct nbd_device *nbd)
803{
804 struct dentry *dir;
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200805
806 if (!nbd_dbg_dir)
807 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200808
809 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200810 if (!dir) {
811 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
812 nbd_name(nbd));
813 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200814 }
815 nbd->dbg_dir = dir;
816
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200817 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
818 debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -0700819 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik75991662016-12-02 16:19:12 -0500820 debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -0400821 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200822
823 return 0;
824}
825
826static void nbd_dev_dbg_close(struct nbd_device *nbd)
827{
828 debugfs_remove_recursive(nbd->dbg_dir);
829}
830
831static int nbd_dbg_init(void)
832{
833 struct dentry *dbg_dir;
834
835 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200836 if (!dbg_dir)
837 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200838
839 nbd_dbg_dir = dbg_dir;
840
841 return 0;
842}
843
844static void nbd_dbg_close(void)
845{
846 debugfs_remove_recursive(nbd_dbg_dir);
847}
848
849#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
850
851static int nbd_dev_dbg_init(struct nbd_device *nbd)
852{
853 return 0;
854}
855
856static void nbd_dev_dbg_close(struct nbd_device *nbd)
857{
858}
859
860static int nbd_dbg_init(void)
861{
862 return 0;
863}
864
865static void nbd_dbg_close(void)
866{
867}
868
869#endif
870
Josef Bacikfd8383f2016-09-08 12:33:37 -0700871static int nbd_init_request(void *data, struct request *rq,
872 unsigned int hctx_idx, unsigned int request_idx,
873 unsigned int numa_node)
874{
875 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
876
877 cmd->nbd = data;
878 INIT_LIST_HEAD(&cmd->list);
879 return 0;
880}
881
882static struct blk_mq_ops nbd_mq_ops = {
883 .queue_rq = nbd_queue_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700884 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -0700885 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700886};
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/*
889 * And here should be modules and kernel interface
890 * (Just smiley confuses emacs :-)
891 */
892
893static int __init nbd_init(void)
894{
895 int err = -ENOMEM;
896 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700897 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800899 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700901 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +0200902 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700903 return -EINVAL;
904 }
905
906 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +0200907 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700908 part_shift = fls(max_part);
909
Namhyung Kim5988ce22011-05-28 14:44:46 +0200910 /*
911 * Adjust max_part according to part_shift as it is exported
912 * to user space so that user can know the max number of
913 * partition kernel should be able to manage.
914 *
915 * Note that -1 is required because partition 0 is reserved
916 * for the whole disk.
917 */
918 max_part = (1UL << part_shift) - 1;
919 }
920
Namhyung Kim3b271082011-05-28 14:44:46 +0200921 if ((1UL << part_shift) > DISK_MAX_PARTS)
922 return -EINVAL;
923
924 if (nbds_max > 1UL << (MINORBITS - part_shift))
925 return -EINVAL;
926
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +0530927 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
928 if (!nbd_dev)
929 return -ENOMEM;
930
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700931 for (i = 0; i < nbds_max; i++) {
Jeff Moyer952d07a2017-01-09 15:20:31 -0500932 struct request_queue *q;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700933 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!disk)
935 goto out;
936 nbd_dev[i].disk = disk;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700937
938 nbd_dev[i].tag_set.ops = &nbd_mq_ops;
939 nbd_dev[i].tag_set.nr_hw_queues = 1;
940 nbd_dev[i].tag_set.queue_depth = 128;
941 nbd_dev[i].tag_set.numa_node = NUMA_NO_NODE;
942 nbd_dev[i].tag_set.cmd_size = sizeof(struct nbd_cmd);
943 nbd_dev[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Josef Bacik005043a2016-09-21 16:55:31 -0400944 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700945 nbd_dev[i].tag_set.driver_data = &nbd_dev[i];
946
947 err = blk_mq_alloc_tag_set(&nbd_dev[i].tag_set);
948 if (err) {
949 put_disk(disk);
950 goto out;
951 }
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /*
954 * The new linux 2.5 block layer implementation requires
955 * every gendisk to have its very own request_queue struct.
956 * These structs are big so we dynamically allocate them.
957 */
Jeff Moyer952d07a2017-01-09 15:20:31 -0500958 q = blk_mq_init_queue(&nbd_dev[i].tag_set);
959 if (IS_ERR(q)) {
Josef Bacikfd8383f2016-09-08 12:33:37 -0700960 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 put_disk(disk);
962 goto out;
963 }
Jeff Moyer952d07a2017-01-09 15:20:31 -0500964 disk->queue = q;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700965
Jens Axboe31dcfab2008-10-31 10:06:37 +0100966 /*
967 * Tell the block layer that we are not a rotational device
968 */
969 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600970 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -0700971 disk->queue->limits.discard_granularity = 512;
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600972 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Paul Clementsa336d292012-10-04 17:16:18 -0700973 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -0700974 blk_queue_max_hw_sectors(disk->queue, 65536);
975 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
978 if (register_blkdev(NBD_MAJOR, "nbd")) {
979 err = -EIO;
980 goto out;
981 }
982
983 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Markus Pargmann30d53d92015-08-17 08:20:06 +0200985 nbd_dbg_init();
986
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700987 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -0700989 nbd_dev[i].magic = NBD_MAGIC;
Markus Pargmann23272a672015-10-29 11:51:16 +0100990 spin_lock_init(&nbd_dev[i].sock_lock);
Ingo Molnar82d4dc52006-03-23 03:00:38 -0800991 mutex_init(&nbd_dev[i].tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700993 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 disk->fops = &nbd_fops;
995 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 sprintf(disk->disk_name, "nbd%d", i);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100997 nbd_reset(&nbd_dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 add_disk(disk);
999 }
1000
1001 return 0;
1002out:
1003 while (i--) {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001004 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 blk_cleanup_queue(nbd_dev[i].disk->queue);
1006 put_disk(nbd_dev[i].disk);
1007 }
Sven Wegenerf3944d62008-08-20 14:09:07 -07001008 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return err;
1010}
1011
1012static void __exit nbd_cleanup(void)
1013{
1014 int i;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001015
1016 nbd_dbg_close();
1017
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001018 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001020 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (disk) {
1022 del_gendisk(disk);
1023 blk_cleanup_queue(disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001024 blk_mq_free_tag_set(&nbd_dev[i].tag_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 put_disk(disk);
1026 }
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -07001029 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
1031}
1032
1033module_init(nbd_init);
1034module_exit(nbd_cleanup);
1035
1036MODULE_DESCRIPTION("Network Block Device");
1037MODULE_LICENSE("GPL");
1038
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001039module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001040MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1041module_param(max_part, int, 0444);
1042MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");