blob: c6ff8f922fd7e0b806626f1f1bf8fc02d58bcf42 [file] [log] [blame]
Thomas Gleixnereb1fe3b2019-05-24 12:03:47 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Network block device - make block devices work over TCP
4 *
5 * Note that you can not swap over this thing, yet. Seems to work but
6 * deadlocks sometimes - you can not swap over TCP in general.
7 *
Pavel Macheka2531292010-07-18 14:27:13 +02008 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
10 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070011 * (part of code stolen from loop.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/major.h>
15
16#include <linux/blkdev.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -070020#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/fs.h>
22#include <linux/bio.h>
23#include <linux/stat.h>
24#include <linux/errno.h>
25#include <linux/file.h>
26#include <linux/ioctl.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020027#include <linux/mutex.h>
Herbert Xu4b2f0262006-01-06 00:09:47 -080028#include <linux/compiler.h>
29#include <linux/err.h>
30#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/sock.h>
Trond Myklebust91cf45f2007-11-12 18:10:39 -080033#include <linux/net.h>
Laurent Vivier48cf6062008-04-29 01:02:46 -070034#include <linux/kthread.h>
Markus Pargmannb9c495b2015-04-02 10:11:37 +020035#include <linux/types.h>
Markus Pargmann30d53d92015-08-17 08:20:06 +020036#include <linux/debugfs.h>
Josef Bacikfd8383f2016-09-08 12:33:37 -070037#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/types.h>
41
42#include <linux/nbd.h>
Josef Bacike46c7282017-04-06 17:02:00 -040043#include <linux/nbd-netlink.h>
44#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Matt Mullinsea106722019-04-26 11:49:48 -070046#define CREATE_TRACE_POINTS
47#include <trace/events/nbd.h>
48
Josef Bacikb0d91112017-02-01 16:11:40 -050049static DEFINE_IDR(nbd_index_idr);
50static DEFINE_MUTEX(nbd_index_mutex);
Josef Bacik47d902b2017-04-06 17:02:05 -040051static int nbd_total_devices = 0;
Josef Bacikb0d91112017-02-01 16:11:40 -050052
Josef Bacik9561a7a2016-11-22 14:04:40 -050053struct nbd_sock {
54 struct socket *sock;
55 struct mutex tx_lock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -040056 struct request *pending;
57 int sent;
Josef Bacikf3733242017-04-06 17:01:57 -040058 bool dead;
59 int fallback_index;
Josef Bacik799f9a32017-04-06 17:02:02 -040060 int cookie;
Josef Bacik9561a7a2016-11-22 14:04:40 -050061};
62
Josef Bacik5ea8d102017-04-06 17:01:58 -040063struct recv_thread_args {
64 struct work_struct work;
65 struct nbd_device *nbd;
66 int index;
67};
68
Josef Bacik799f9a32017-04-06 17:02:02 -040069struct link_dead_args {
70 struct work_struct work;
71 int index;
72};
73
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070074#define NBD_TIMEDOUT 0
75#define NBD_DISCONNECT_REQUESTED 1
Josef Bacik9561a7a2016-11-22 14:04:40 -050076#define NBD_DISCONNECTED 2
Josef Bacik5ea8d102017-04-06 17:01:58 -040077#define NBD_HAS_PID_FILE 3
Josef Bacike46c7282017-04-06 17:02:00 -040078#define NBD_HAS_CONFIG_REF 4
79#define NBD_BOUND 5
Josef Bacika2c97902017-04-06 17:02:07 -040080#define NBD_DESTROY_ON_DISCONNECT 6
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -070081#define NBD_DISCONNECT_ON_CLOSE 7
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070082
Josef Bacik5ea8d102017-04-06 17:01:58 -040083struct nbd_config {
Markus Pargmann22d109c2015-08-17 08:20:09 +020084 u32 flags;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070085 unsigned long runtime_flags;
Josef Bacik560bc4b2017-04-06 17:02:04 -040086 u64 dead_conn_timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -040087
Josef Bacik9561a7a2016-11-22 14:04:40 -050088 struct nbd_sock **socks;
Josef Bacik9561a7a2016-11-22 14:04:40 -050089 int num_connections;
Josef Bacik560bc4b2017-04-06 17:02:04 -040090 atomic_t live_connections;
91 wait_queue_head_t conn_wait;
Josef Bacik5ea8d102017-04-06 17:01:58 -040092
Josef Bacik9561a7a2016-11-22 14:04:40 -050093 atomic_t recv_threads;
94 wait_queue_head_t recv_wq;
Josef Bacikef77b512016-12-02 16:19:12 -050095 loff_t blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020096 loff_t bytesize;
Markus Pargmann30d53d92015-08-17 08:20:06 +020097#if IS_ENABLED(CONFIG_DEBUG_FS)
98 struct dentry *dbg_dir;
99#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +0200100};
101
Josef Bacik5ea8d102017-04-06 17:01:58 -0400102struct nbd_device {
103 struct blk_mq_tag_set tag_set;
104
Josef Bacike46c7282017-04-06 17:02:00 -0400105 int index;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400106 refcount_t config_refs;
Josef Bacikc6a47592017-04-06 17:02:06 -0400107 refcount_t refs;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400108 struct nbd_config *config;
109 struct mutex config_lock;
110 struct gendisk *disk;
111
Josef Bacikc6a47592017-04-06 17:02:06 -0400112 struct list_head list;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400113 struct task_struct *task_recv;
114 struct task_struct *task_setup;
115};
116
Josef Bacikd7d94d42018-07-16 12:11:34 -0400117#define NBD_CMD_REQUEUED 1
118
Josef Bacikfd8383f2016-09-08 12:33:37 -0700119struct nbd_cmd {
120 struct nbd_device *nbd;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400121 struct mutex lock;
Josef Bacikf3733242017-04-06 17:01:57 -0400122 int index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400123 int cookie;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200124 blk_status_t status;
Josef Bacikd7d94d42018-07-16 12:11:34 -0400125 unsigned long flags;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400126 u32 cmd_cookie;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700127};
128
Markus Pargmann30d53d92015-08-17 08:20:06 +0200129#if IS_ENABLED(CONFIG_DEBUG_FS)
130static struct dentry *nbd_dbg_dir;
131#endif
132
133#define nbd_name(nbd) ((nbd)->disk->disk_name)
134
Wanlong Gaof4507162012-03-28 14:42:51 -0700135#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Xiubo Li553768d2019-05-29 15:16:05 -0500137#define NBD_DEF_BLKSIZE 1024
138
Ingo van Lil9c7a4162006-07-01 04:36:36 -0700139static unsigned int nbds_max = 16;
Josef Bacik7a8362a2017-08-14 18:56:16 +0000140static int max_part = 16;
Josef Bacik124d6db2017-02-01 16:11:11 -0500141static struct workqueue_struct *recv_workqueue;
Josef Bacikb0d91112017-02-01 16:11:40 -0500142static int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Josef Bacik9442b732017-02-07 17:10:22 -0500144static int nbd_dev_dbg_init(struct nbd_device *nbd);
145static void nbd_dev_dbg_close(struct nbd_device *nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400146static void nbd_config_put(struct nbd_device *nbd);
Josef Bacike46c7282017-04-06 17:02:00 -0400147static void nbd_connect_reply(struct genl_info *info, int index);
Josef Bacik47d902b2017-04-06 17:02:05 -0400148static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
Josef Bacik799f9a32017-04-06 17:02:02 -0400149static void nbd_dead_link_work(struct work_struct *work);
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -0700150static void nbd_disconnect_and_put(struct nbd_device *nbd);
Josef Bacik9442b732017-02-07 17:10:22 -0500151
Markus Pargmannd18509f2015-04-02 10:11:38 +0200152static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Markus Pargmannd18509f2015-04-02 10:11:38 +0200154 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Josef Bacikd7d94d42018-07-16 12:11:34 -0400157static void nbd_requeue_cmd(struct nbd_cmd *cmd)
158{
159 struct request *req = blk_mq_rq_from_pdu(cmd);
160
161 if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
162 blk_mq_requeue_request(req, true);
163}
164
Josef Bacik8f3ea352018-07-16 12:11:35 -0400165#define NBD_COOKIE_BITS 32
166
167static u64 nbd_cmd_handle(struct nbd_cmd *cmd)
168{
169 struct request *req = blk_mq_rq_from_pdu(cmd);
170 u32 tag = blk_mq_unique_tag(req);
171 u64 cookie = cmd->cmd_cookie;
172
173 return (cookie << NBD_COOKIE_BITS) | tag;
174}
175
176static u32 nbd_handle_to_tag(u64 handle)
177{
178 return (u32)handle;
179}
180
181static u32 nbd_handle_to_cookie(u64 handle)
182{
183 return (u32)(handle >> NBD_COOKIE_BITS);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static const char *nbdcmd_to_ascii(int cmd)
187{
188 switch (cmd) {
189 case NBD_CMD_READ: return "read";
190 case NBD_CMD_WRITE: return "write";
191 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800192 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700193 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 return "invalid";
196}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Josef Bacik5ea8d102017-04-06 17:01:58 -0400198static ssize_t pid_show(struct device *dev,
199 struct device_attribute *attr, char *buf)
200{
201 struct gendisk *disk = dev_to_disk(dev);
202 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
203
204 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
205}
206
Bhumika Goyaldfbde552017-08-21 17:13:08 +0530207static const struct device_attribute pid_attr = {
Joe Perches5657a812018-05-24 13:38:59 -0600208 .attr = { .name = "pid", .mode = 0444},
Josef Bacik5ea8d102017-04-06 17:01:58 -0400209 .show = pid_show,
210};
211
Josef Bacikc6a47592017-04-06 17:02:06 -0400212static void nbd_dev_remove(struct nbd_device *nbd)
213{
214 struct gendisk *disk = nbd->disk;
Josef Bacik8364da42018-05-16 14:51:17 -0400215 struct request_queue *q;
216
Josef Bacikc6a47592017-04-06 17:02:06 -0400217 if (disk) {
Josef Bacik8364da42018-05-16 14:51:17 -0400218 q = disk->queue;
Josef Bacikc6a47592017-04-06 17:02:06 -0400219 del_gendisk(disk);
Josef Bacik8364da42018-05-16 14:51:17 -0400220 blk_cleanup_queue(q);
Josef Bacikc6a47592017-04-06 17:02:06 -0400221 blk_mq_free_tag_set(&nbd->tag_set);
Josef Bacika2c97902017-04-06 17:02:07 -0400222 disk->private_data = NULL;
Josef Bacikc6a47592017-04-06 17:02:06 -0400223 put_disk(disk);
224 }
225 kfree(nbd);
226}
227
228static void nbd_put(struct nbd_device *nbd)
229{
230 if (refcount_dec_and_mutex_lock(&nbd->refs,
231 &nbd_index_mutex)) {
232 idr_remove(&nbd_index_idr, nbd->index);
233 mutex_unlock(&nbd_index_mutex);
234 nbd_dev_remove(nbd);
235 }
236}
237
Josef Bacik799f9a32017-04-06 17:02:02 -0400238static int nbd_disconnected(struct nbd_config *config)
Josef Bacikf3733242017-04-06 17:01:57 -0400239{
Josef Bacik799f9a32017-04-06 17:02:02 -0400240 return test_bit(NBD_DISCONNECTED, &config->runtime_flags) ||
241 test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
242}
243
244static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
245 int notify)
246{
247 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
248 struct link_dead_args *args;
249 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
250 if (args) {
251 INIT_WORK(&args->work, nbd_dead_link_work);
252 args->index = nbd->index;
253 queue_work(system_wq, &args->work);
254 }
255 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400256 if (!nsock->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400257 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600258 if (atomic_dec_return(&nbd->config->live_connections) == 0) {
259 if (test_and_clear_bit(NBD_DISCONNECT_REQUESTED,
260 &nbd->config->runtime_flags)) {
261 set_bit(NBD_DISCONNECTED,
262 &nbd->config->runtime_flags);
263 dev_info(nbd_to_dev(nbd),
264 "Disconnected due to user request.\n");
265 }
266 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400267 }
Josef Bacikf3733242017-04-06 17:01:57 -0400268 nsock->dead = true;
269 nsock->pending = NULL;
270 nsock->sent = 0;
271}
272
Josef Bacik29eaadc2017-04-06 17:01:59 -0400273static void nbd_size_clear(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200274{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400275 if (nbd->config->bytesize) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400276 set_capacity(nbd->disk, 0);
277 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
278 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200279}
280
Josef Bacik29eaadc2017-04-06 17:01:59 -0400281static void nbd_size_update(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200282{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400283 struct nbd_config *config = nbd->config;
Josef Bacik9e2b19672018-05-16 14:51:19 -0400284 struct block_device *bdev = bdget_disk(nbd->disk, 0);
285
Josef Bacik6df133a2018-05-23 13:35:59 -0400286 if (config->flags & NBD_FLAG_SEND_TRIM) {
287 nbd->disk->queue->limits.discard_granularity = config->blksize;
Josef Bacik07ce2132018-06-05 11:41:23 -0400288 nbd->disk->queue->limits.discard_alignment = config->blksize;
Josef Bacik6df133a2018-05-23 13:35:59 -0400289 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
290 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400291 blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
292 blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400293 set_capacity(nbd->disk, config->bytesize >> 9);
Josef Bacik9e2b19672018-05-16 14:51:19 -0400294 if (bdev) {
Jan Karac8a83a62019-01-14 09:48:09 +0100295 if (bdev->bd_disk) {
Josef Bacik9e2b19672018-05-16 14:51:19 -0400296 bd_set_size(bdev, config->bytesize);
Jan Karac8a83a62019-01-14 09:48:09 +0100297 set_blocksize(bdev, config->blksize);
298 } else
Josef Bacik9e2b19672018-05-16 14:51:19 -0400299 bdev->bd_invalidated = 1;
300 bdput(bdev);
301 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200302 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
303}
304
Josef Bacik29eaadc2017-04-06 17:01:59 -0400305static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
306 loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200307{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400308 struct nbd_config *config = nbd->config;
309 config->blksize = blocksize;
310 config->bytesize = blocksize * nr_blocks;
Josef Bacikc3f7c932018-05-16 14:51:18 -0400311 if (nbd->task_recv != NULL)
312 nbd_size_update(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200313}
314
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200315static void nbd_complete_rq(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200317 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Kevin Vigoree57a052018-06-04 10:40:12 -0600319 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200320 cmd->status ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200322 blk_mq_end_request(req, cmd->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Markus Pargmanne018e752015-04-02 10:11:39 +0200325/*
326 * Forcibly shutdown the socket causing all listeners to error
327 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200328static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700329{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400330 struct nbd_config *config = nbd->config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500331 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700332
Josef Bacik5ea8d102017-04-06 17:01:58 -0400333 if (config->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200334 return;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400335 if (test_and_set_bit(NBD_DISCONNECTED, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500336 return;
337
Josef Bacik5ea8d102017-04-06 17:01:58 -0400338 for (i = 0; i < config->num_connections; i++) {
339 struct nbd_sock *nsock = config->socks[i];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500340 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400341 nbd_mark_nsock_dead(nbd, nsock, 0);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500342 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100343 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500344 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700345}
346
Mike Christie00514672019-08-13 11:39:50 -0500347static u32 req_to_nbd_cmd_type(struct request *req)
348{
349 switch (req_op(req)) {
350 case REQ_OP_DISCARD:
351 return NBD_CMD_TRIM;
352 case REQ_OP_FLUSH:
353 return NBD_CMD_FLUSH;
354 case REQ_OP_WRITE:
355 return NBD_CMD_WRITE;
356 case REQ_OP_READ:
357 return NBD_CMD_READ;
358 default:
359 return U32_MAX;
360 }
361}
362
Josef Bacik0eadf372016-09-08 12:33:40 -0700363static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
364 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700365{
Josef Bacik0eadf372016-09-08 12:33:40 -0700366 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
367 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400368 struct nbd_config *config;
Paul Clements7fdfd402007-10-16 23:27:37 -0700369
Josef Bacik5ea8d102017-04-06 17:01:58 -0400370 if (!refcount_inc_not_zero(&nbd->config_refs)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200371 cmd->status = BLK_STS_TIMEOUT;
Christoph Hellwige5eab012018-05-29 15:52:31 +0200372 goto done;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400373 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400374 config = nbd->config;
375
Josef Bacik8f3ea352018-07-16 12:11:35 -0400376 if (!mutex_trylock(&cmd->lock))
377 return BLK_EH_RESET_TIMER;
378
Josef Bacik5ea8d102017-04-06 17:01:58 -0400379 if (config->num_connections > 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400380 dev_err_ratelimited(nbd_to_dev(nbd),
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600381 "Connection timed out, retrying (%d/%d alive)\n",
382 atomic_read(&config->live_connections),
383 config->num_connections);
Josef Bacikf3733242017-04-06 17:01:57 -0400384 /*
385 * Hooray we have more connections, requeue this IO, the submit
386 * path will put it on a real connection.
387 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400388 if (config->socks && config->num_connections > 1) {
389 if (cmd->index < config->num_connections) {
Josef Bacikf3733242017-04-06 17:01:57 -0400390 struct nbd_sock *nsock =
Josef Bacik5ea8d102017-04-06 17:01:58 -0400391 config->socks[cmd->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400392 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400393 /* We can have multiple outstanding requests, so
394 * we don't want to mark the nsock dead if we've
395 * already reconnected with a new socket, so
396 * only mark it dead if its the same socket we
397 * were sent out on.
398 */
399 if (cmd->cookie == nsock->cookie)
400 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400401 mutex_unlock(&nsock->tx_lock);
402 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400403 mutex_unlock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400404 nbd_requeue_cmd(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400405 nbd_config_put(nbd);
Christoph Hellwig66005932018-05-29 15:52:29 +0200406 return BLK_EH_DONE;
Josef Bacikf3733242017-04-06 17:01:57 -0400407 }
Josef Bacikf3733242017-04-06 17:01:57 -0400408 } else {
409 dev_err_ratelimited(nbd_to_dev(nbd),
410 "Connection timed out\n");
411 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400412 set_bit(NBD_TIMEDOUT, &config->runtime_flags);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200413 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400414 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500415 sock_shutdown(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400416 nbd_config_put(nbd);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200417done:
418 blk_mq_complete_request(req);
419 return BLK_EH_DONE;
Paul Clements7fdfd402007-10-16 23:27:37 -0700420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/*
423 * Send or receive packet.
424 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500425static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400426 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400428 struct nbd_config *config = nbd->config;
429 struct socket *sock = config->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int result;
431 struct msghdr msg;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700432 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700434 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500435 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200436 "Attempted %s on closed socket in sock_xmit\n",
437 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700438 return -EINVAL;
439 }
440
Al Viroc9f2b6a2015-11-12 05:09:35 -0500441 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500442
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700443 noreclaim_flag = memalloc_noreclaim_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700445 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 msg.msg_name = NULL;
447 msg.msg_namelen = 0;
448 msg.msg_control = NULL;
449 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
451
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200452 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500453 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200454 else
Al Viroc1696ca2015-11-12 04:51:19 -0500455 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (result <= 0) {
458 if (result == 0)
459 result = -EPIPE; /* short read */
460 break;
461 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400462 if (sent)
463 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500464 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700466 memalloc_noreclaim_restore(noreclaim_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 return result;
469}
470
Josef Bacik32e67a32017-10-24 15:57:18 -0400471/*
472 * Different settings for sk->sk_sndtimeo can result in different return values
473 * if there is a signal pending when we enter sendmsg, because reasons?
474 */
475static inline int was_interrupted(int result)
476{
477 return result == -ERESTARTSYS || result == -EINTR;
478}
479
Paul Clements7fdfd402007-10-16 23:27:37 -0700480/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500481static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700483 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400484 struct nbd_config *config = nbd->config;
485 struct nbd_sock *nsock = config->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500486 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500487 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
488 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
489 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900490 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700491 struct bio *bio;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400492 u64 handle;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200493 u32 type;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400494 u32 nbd_cmd_flags = 0;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400495 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200496
David Howellsaa563d72018-10-20 00:57:56 +0100497 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500498
Mike Christie00514672019-08-13 11:39:50 -0500499 type = req_to_nbd_cmd_type(req);
500 if (type == U32_MAX)
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100501 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100503 if (rq_data_dir(req) == WRITE &&
Josef Bacik5ea8d102017-04-06 17:01:58 -0400504 (config->flags & NBD_FLAG_READ_ONLY)) {
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100505 dev_err_ratelimited(disk_to_dev(nbd->disk),
506 "Write on read-only\n");
507 return -EIO;
508 }
509
Shaun McDowell685c9b22017-05-25 23:55:54 -0400510 if (req->cmd_flags & REQ_FUA)
511 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
512
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400513 /* We did a partial send previously, and we at least sent the whole
514 * request struct, so just go and send the rest of the pages in the
515 * request.
516 */
517 if (sent) {
518 if (sent >= sizeof(request)) {
519 skip = sent - sizeof(request);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700520
521 /* initialize handle for tracing purposes */
522 handle = nbd_cmd_handle(cmd);
523
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400524 goto send_pages;
525 }
526 iov_iter_advance(&from, sent);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400527 } else {
528 cmd->cmd_cookie++;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400529 }
Josef Bacikf3733242017-04-06 17:01:57 -0400530 cmd->index = index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400531 cmd->cookie = nsock->cookie;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400532 request.type = htonl(type | nbd_cmd_flags);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500533 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800534 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
535 request.len = htonl(size);
536 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400537 handle = nbd_cmd_handle(cmd);
538 memcpy(request.handle, &handle, sizeof(handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Matt Mullinsea106722019-04-26 11:49:48 -0700540 trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
541
Markus Pargmannd18509f2015-04-02 10:11:38 +0200542 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600543 req, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200544 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500545 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400546 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700547 trace_nbd_header_sent(req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400549 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400550 /* If we havne't sent anything we can just return BUSY,
551 * however if we have sent something we need to make
552 * sure we only allow this req to be sent until we are
553 * completely done.
554 */
555 if (sent) {
556 nsock->pending = req;
557 nsock->sent = sent;
558 }
Josef Bacikd7d94d42018-07-16 12:11:34 -0400559 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200560 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400561 }
Josef Bacika897b662016-12-05 16:20:29 -0500562 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200563 "Send control failed (result %d)\n", result);
Josef Bacikf3733242017-04-06 17:01:57 -0400564 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400566send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700567 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400568 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700569
Jens Axboe429a7872016-11-17 12:30:37 -0700570 bio = req->bio;
571 while (bio) {
572 struct bio *next = bio->bi_next;
573 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800574 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700575
576 bio_for_each_segment(bvec, bio, iter) {
577 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500578 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700579
Markus Pargmannd18509f2015-04-02 10:11:38 +0200580 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600581 req, bvec.bv_len);
David Howellsaa563d72018-10-20 00:57:56 +0100582 iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400583 if (skip) {
584 if (skip >= iov_iter_count(&from)) {
585 skip -= iov_iter_count(&from);
586 continue;
587 }
588 iov_iter_advance(&from, skip);
589 skip = 0;
590 }
591 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200592 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400593 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400594 /* We've already sent the header, we
595 * have no choice but to set pending and
596 * return BUSY.
597 */
598 nsock->pending = req;
599 nsock->sent = sent;
Josef Bacikd7d94d42018-07-16 12:11:34 -0400600 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200601 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400602 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700603 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200604 "Send data failed (result %d)\n",
605 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400606 return -EAGAIN;
Jens Axboe6c92e692007-08-16 13:43:12 +0200607 }
Jens Axboe429a7872016-11-17 12:30:37 -0700608 /*
609 * The completion might already have come in,
610 * so break for the last one instead of letting
611 * the iterator do it. This prevents use-after-free
612 * of the bio.
613 */
614 if (is_last)
615 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Jens Axboe429a7872016-11-17 12:30:37 -0700617 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400619out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700620 trace_nbd_payload_sent(req, handle);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400621 nsock->pending = NULL;
622 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500627static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400629 struct nbd_config *config = nbd->config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int result;
631 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700632 struct nbd_cmd *cmd;
633 struct request *req = NULL;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400634 u64 handle;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700635 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500636 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500637 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
638 struct iov_iter to;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400639 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 reply.magic = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100642 iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400643 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (result <= 0) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400645 if (!nbd_disconnected(config))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500646 dev_err(disk_to_dev(nbd->disk),
647 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200648 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700650
651 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700652 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700653 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200654 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700655 }
656
Josef Bacik8f3ea352018-07-16 12:11:35 -0400657 memcpy(&handle, reply.handle, sizeof(handle));
658 tag = nbd_handle_to_tag(handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700659 hwq = blk_mq_unique_tag_to_hwq(tag);
660 if (hwq < nbd->tag_set.nr_hw_queues)
661 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
662 blk_mq_unique_tag_to_tag(tag));
663 if (!req || !blk_mq_request_started(req)) {
664 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
665 tag, req);
666 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Andrew Hall2abd2de2019-04-26 11:49:49 -0700668 trace_nbd_header_received(req, handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700669 cmd = blk_mq_rq_to_pdu(req);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400670
671 mutex_lock(&cmd->lock);
672 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
673 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
674 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
675 ret = -ENOENT;
676 goto out;
677 }
678 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
679 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
680 req);
681 ret = -ENOENT;
682 goto out;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700685 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200686 ntohl(reply.error));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200687 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Kevin Vigoree57a052018-06-04 10:40:12 -0600691 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200692 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200693 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800694 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200695
696 rq_for_each_segment(bvec, req, iter) {
David Howellsaa563d72018-10-20 00:57:56 +0100697 iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400698 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200699 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700700 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200701 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400702 /*
703 * If we've disconnected or we only have 1
704 * connection then we need to make sure we
705 * complete this request, otherwise error out
706 * and let the timeout stuff handle resubmitting
707 * this request onto another connection.
708 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400709 if (nbd_disconnected(config) ||
710 config->num_connections <= 1) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200711 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400712 goto out;
Josef Bacikf3733242017-04-06 17:01:57 -0400713 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400714 ret = -EIO;
715 goto out;
Jens Axboe6c92e692007-08-16 13:43:12 +0200716 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200717 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600718 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400721out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700722 trace_nbd_payload_received(req, handle);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400723 mutex_unlock(&cmd->lock);
724 return ret ? ERR_PTR(ret) : cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Josef Bacik9561a7a2016-11-22 14:04:40 -0500727static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500729 struct recv_thread_args *args = container_of(work,
730 struct recv_thread_args,
731 work);
732 struct nbd_device *nbd = args->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400733 struct nbd_config *config = nbd->config;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700734 struct nbd_cmd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Markus Pargmann19391832015-08-17 08:20:03 +0200736 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500737 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700738 if (IS_ERR(cmd)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400739 struct nbd_sock *nsock = config->socks[args->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400740
741 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400742 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400743 mutex_unlock(&nsock->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200744 break;
745 }
746
Christoph Hellwig08e00292017-04-20 16:03:09 +0200747 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd));
Markus Pargmann19391832015-08-17 08:20:03 +0200748 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400749 atomic_dec(&config->recv_threads);
750 wake_up(&config->recv_wq);
751 nbd_config_put(nbd);
752 kfree(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Jens Axboe7baa8572018-11-08 10:24:07 -0700755static bool nbd_clear_req(struct request *req, void *data, bool reserved)
Josef Bacikfd8383f2016-09-08 12:33:37 -0700756{
Christoph Hellwigd250bf4e2018-05-30 18:51:00 +0200757 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700758
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200759 cmd->status = BLK_STS_IOERR;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200760 blk_mq_complete_request(req);
Jens Axboe7baa8572018-11-08 10:24:07 -0700761 return true;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700762}
763
Wanlong Gaof4507162012-03-28 14:42:51 -0700764static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300766 blk_mq_quiesce_queue(nbd->disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700767 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300768 blk_mq_unquiesce_queue(nbd->disk->queue);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200769 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Josef Bacikf3733242017-04-06 17:01:57 -0400772static int find_fallback(struct nbd_device *nbd, int index)
773{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400774 struct nbd_config *config = nbd->config;
Josef Bacikf3733242017-04-06 17:01:57 -0400775 int new_index = -1;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400776 struct nbd_sock *nsock = config->socks[index];
Josef Bacikf3733242017-04-06 17:01:57 -0400777 int fallback = nsock->fallback_index;
778
Josef Bacik5ea8d102017-04-06 17:01:58 -0400779 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
Josef Bacikf3733242017-04-06 17:01:57 -0400780 return new_index;
781
Josef Bacik5ea8d102017-04-06 17:01:58 -0400782 if (config->num_connections <= 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400783 dev_err_ratelimited(disk_to_dev(nbd->disk),
784 "Attempted send on invalid socket\n");
785 return new_index;
786 }
787
Josef Bacik5ea8d102017-04-06 17:01:58 -0400788 if (fallback >= 0 && fallback < config->num_connections &&
789 !config->socks[fallback]->dead)
Josef Bacikf3733242017-04-06 17:01:57 -0400790 return fallback;
791
792 if (nsock->fallback_index < 0 ||
Josef Bacik5ea8d102017-04-06 17:01:58 -0400793 nsock->fallback_index >= config->num_connections ||
794 config->socks[nsock->fallback_index]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400795 int i;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400796 for (i = 0; i < config->num_connections; i++) {
Josef Bacikf3733242017-04-06 17:01:57 -0400797 if (i == index)
798 continue;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400799 if (!config->socks[i]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400800 new_index = i;
801 break;
802 }
803 }
804 nsock->fallback_index = new_index;
805 if (new_index < 0) {
806 dev_err_ratelimited(disk_to_dev(nbd->disk),
807 "Dead connection, failed to find a fallback\n");
808 return new_index;
809 }
810 }
811 new_index = nsock->fallback_index;
812 return new_index;
813}
Paul Clements7fdfd402007-10-16 23:27:37 -0700814
Josef Bacik560bc4b2017-04-06 17:02:04 -0400815static int wait_for_reconnect(struct nbd_device *nbd)
816{
817 struct nbd_config *config = nbd->config;
818 if (!config->dead_conn_timeout)
819 return 0;
820 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
821 return 0;
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600822 return wait_event_timeout(config->conn_wait,
823 atomic_read(&config->live_connections) > 0,
824 config->dead_conn_timeout) > 0;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400825}
826
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400827static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700828{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700829 struct request *req = blk_mq_rq_from_pdu(cmd);
830 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400831 struct nbd_config *config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500832 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400833 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700834
Josef Bacik5ea8d102017-04-06 17:01:58 -0400835 if (!refcount_inc_not_zero(&nbd->config_refs)) {
836 dev_err_ratelimited(disk_to_dev(nbd->disk),
837 "Socks array is empty\n");
Josef Bacik6a468d52017-11-06 16:11:58 -0500838 blk_mq_start_request(req);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400839 return -EINVAL;
840 }
841 config = nbd->config;
842
843 if (index >= config->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500844 dev_err_ratelimited(disk_to_dev(nbd->disk),
845 "Attempted send on invalid socket\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -0400846 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500847 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400848 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500849 }
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200850 cmd->status = BLK_STS_OK;
Josef Bacikf3733242017-04-06 17:01:57 -0400851again:
Josef Bacik5ea8d102017-04-06 17:01:58 -0400852 nsock = config->socks[index];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500853 mutex_lock(&nsock->tx_lock);
Josef Bacikf3733242017-04-06 17:01:57 -0400854 if (nsock->dead) {
Josef Bacik560bc4b2017-04-06 17:02:04 -0400855 int old_index = index;
Josef Bacikf3733242017-04-06 17:01:57 -0400856 index = find_fallback(nbd, index);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500857 mutex_unlock(&nsock->tx_lock);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400858 if (index < 0) {
859 if (wait_for_reconnect(nbd)) {
860 index = old_index;
861 goto again;
862 }
863 /* All the sockets should already be down at this point,
864 * we just want to make sure that DISCONNECTED is set so
865 * any requests that come in that were queue'ed waiting
866 * for the reconnect timer don't trigger the timer again
867 * and instead just error out.
868 */
869 sock_shutdown(nbd);
870 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500871 blk_mq_start_request(req);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400872 return -EIO;
873 }
Josef Bacikf3733242017-04-06 17:01:57 -0400874 goto again;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700875 }
876
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400877 /* Handle the case that we have a pending request that was partially
878 * transmitted that _has_ to be serviced first. We need to call requeue
879 * here so that it gets put _after_ the request that is already on the
880 * dispatch list.
881 */
Josef Bacik6a468d52017-11-06 16:11:58 -0500882 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400883 if (unlikely(nsock->pending && nsock->pending != req)) {
Josef Bacikd7d94d42018-07-16 12:11:34 -0400884 nbd_requeue_cmd(cmd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400885 ret = 0;
886 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700887 }
Josef Bacikf3733242017-04-06 17:01:57 -0400888 /*
889 * Some failures are related to the link going down, so anything that
890 * returns EAGAIN can be retried on a different socket.
891 */
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400892 ret = nbd_send_cmd(nbd, cmd, index);
Josef Bacikf3733242017-04-06 17:01:57 -0400893 if (ret == -EAGAIN) {
894 dev_err_ratelimited(disk_to_dev(nbd->disk),
Josef Bacik6a468d52017-11-06 16:11:58 -0500895 "Request send failed, requeueing\n");
Josef Bacik799f9a32017-04-06 17:02:02 -0400896 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400897 nbd_requeue_cmd(cmd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500898 ret = 0;
Josef Bacikf3733242017-04-06 17:01:57 -0400899 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400900out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500901 mutex_unlock(&nsock->tx_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400902 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400903 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700904}
905
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200906static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700907 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700908{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700909 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400910 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700911
Josef Bacik9561a7a2016-11-22 14:04:40 -0500912 /*
913 * Since we look at the bio's to send the request over the network we
914 * need to make sure the completion work doesn't mark this request done
915 * before we are done doing our send. This keeps us from dereferencing
916 * freed data if we have particularly fast completions (ie we get the
917 * completion before we exit sock_xmit on the last bvec) or in the case
918 * that the server is misbehaving (or there was an error) before we're
919 * done sending everything over the wire.
920 */
Josef Bacik8f3ea352018-07-16 12:11:35 -0400921 mutex_lock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400922 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400923
924 /* We can be called directly from the user space process, which means we
925 * could possibly have signals pending so our sendmsg will fail. In
926 * this case we need to return that we are busy, otherwise error out as
927 * appropriate.
928 */
929 ret = nbd_handle_cmd(cmd, hctx->queue_num);
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400930 if (ret < 0)
931 ret = BLK_STS_IOERR;
932 else if (!ret)
933 ret = BLK_STS_OK;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400934 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500935
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Josef Bacike46c7282017-04-06 17:02:00 -0400939static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
940 bool netlink)
Markus Pargmann23272a672015-10-29 11:51:16 +0100941{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400942 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -0500943 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500944 struct nbd_sock **socks;
945 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -0500946 int err;
947
948 sock = sockfd_lookup(arg, &err);
949 if (!sock)
950 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100951
Josef Bacike46c7282017-04-06 17:02:00 -0400952 if (!netlink && !nbd->task_setup &&
953 !test_bit(NBD_BOUND, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500954 nbd->task_setup = current;
Josef Bacike46c7282017-04-06 17:02:00 -0400955
956 if (!netlink &&
957 (nbd->task_setup != current ||
958 test_bit(NBD_BOUND, &config->runtime_flags))) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500959 dev_err(disk_to_dev(nbd->disk),
960 "Device being setup by another task");
Josef Bacik9b1355d2017-04-06 17:01:56 -0400961 sockfd_put(sock);
Josef Bacike46c7282017-04-06 17:02:00 -0400962 return -EBUSY;
Markus Pargmann23272a672015-10-29 11:51:16 +0100963 }
964
Josef Bacik5ea8d102017-04-06 17:01:58 -0400965 socks = krealloc(config->socks, (config->num_connections + 1) *
Josef Bacik9561a7a2016-11-22 14:04:40 -0500966 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400967 if (!socks) {
968 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500969 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400970 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500971 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400972 if (!nsock) {
973 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500974 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400975 }
Markus Pargmann23272a672015-10-29 11:51:16 +0100976
Josef Bacik5ea8d102017-04-06 17:01:58 -0400977 config->socks = socks;
Markus Pargmann23272a672015-10-29 11:51:16 +0100978
Josef Bacikf3733242017-04-06 17:01:57 -0400979 nsock->fallback_index = -1;
980 nsock->dead = false;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500981 mutex_init(&nsock->tx_lock);
982 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400983 nsock->pending = NULL;
984 nsock->sent = 0;
Josef Bacik799f9a32017-04-06 17:02:02 -0400985 nsock->cookie = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400986 socks[config->num_connections++] = nsock;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400987 atomic_inc(&config->live_connections);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500988
989 return 0;
Markus Pargmann23272a672015-10-29 11:51:16 +0100990}
991
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400992static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
993{
994 struct nbd_config *config = nbd->config;
995 struct socket *sock, *old;
996 struct recv_thread_args *args;
997 int i;
998 int err;
999
1000 sock = sockfd_lookup(arg, &err);
1001 if (!sock)
1002 return err;
1003
1004 args = kzalloc(sizeof(*args), GFP_KERNEL);
1005 if (!args) {
1006 sockfd_put(sock);
1007 return -ENOMEM;
1008 }
1009
1010 for (i = 0; i < config->num_connections; i++) {
1011 struct nbd_sock *nsock = config->socks[i];
1012
1013 if (!nsock->dead)
1014 continue;
1015
1016 mutex_lock(&nsock->tx_lock);
1017 if (!nsock->dead) {
1018 mutex_unlock(&nsock->tx_lock);
1019 continue;
1020 }
1021 sk_set_memalloc(sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001022 if (nbd->tag_set.timeout)
1023 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001024 atomic_inc(&config->recv_threads);
1025 refcount_inc(&nbd->config_refs);
1026 old = nsock->sock;
1027 nsock->fallback_index = -1;
1028 nsock->sock = sock;
1029 nsock->dead = false;
1030 INIT_WORK(&args->work, recv_work);
1031 args->index = i;
1032 args->nbd = nbd;
Josef Bacik799f9a32017-04-06 17:02:02 -04001033 nsock->cookie++;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001034 mutex_unlock(&nsock->tx_lock);
1035 sockfd_put(old);
1036
Josef Bacik7a362ea2017-07-25 13:31:19 -04001037 clear_bit(NBD_DISCONNECTED, &config->runtime_flags);
1038
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001039 /* We take the tx_mutex in an error path in the recv_work, so we
1040 * need to queue_work outside of the tx_mutex.
1041 */
1042 queue_work(recv_workqueue, &args->work);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001043
1044 atomic_inc(&config->live_connections);
1045 wake_up(&config->conn_wait);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001046 return 0;
1047 }
1048 sockfd_put(sock);
1049 kfree(args);
1050 return -ENOSPC;
1051}
1052
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001053static void nbd_bdev_reset(struct block_device *bdev)
1054{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -04001055 if (bdev->bd_openers > 1)
1056 return;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001057 bd_set_size(bdev, 0);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001058}
1059
Josef Bacik29eaadc2017-04-06 17:01:59 -04001060static void nbd_parse_flags(struct nbd_device *nbd)
Markus Pargmannd02cf532015-10-29 12:06:15 +01001061{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001062 struct nbd_config *config = nbd->config;
1063 if (config->flags & NBD_FLAG_READ_ONLY)
Josef Bacik29eaadc2017-04-06 17:01:59 -04001064 set_disk_ro(nbd->disk, true);
1065 else
1066 set_disk_ro(nbd->disk, false);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001067 if (config->flags & NBD_FLAG_SEND_TRIM)
Bart Van Assche8b904b52018-03-07 17:10:10 -08001068 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Shaun McDowell685c9b22017-05-25 23:55:54 -04001069 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1070 if (config->flags & NBD_FLAG_SEND_FUA)
1071 blk_queue_write_cache(nbd->disk->queue, true, true);
1072 else
1073 blk_queue_write_cache(nbd->disk->queue, true, false);
1074 }
Markus Pargmannd02cf532015-10-29 12:06:15 +01001075 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -06001076 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +01001077}
1078
Josef Bacik9561a7a2016-11-22 14:04:40 -05001079static void send_disconnects(struct nbd_device *nbd)
1080{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001081 struct nbd_config *config = nbd->config;
Al Viroc9f2b6a2015-11-12 05:09:35 -05001082 struct nbd_request request = {
1083 .magic = htonl(NBD_REQUEST_MAGIC),
1084 .type = htonl(NBD_CMD_DISC),
1085 };
1086 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1087 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001088 int i, ret;
1089
Josef Bacik5ea8d102017-04-06 17:01:58 -04001090 for (i = 0; i < config->num_connections; i++) {
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001091 struct nbd_sock *nsock = config->socks[i];
1092
David Howellsaa563d72018-10-20 00:57:56 +01001093 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001094 mutex_lock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001095 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001096 if (ret <= 0)
1097 dev_err(disk_to_dev(nbd->disk),
1098 "Send disconnect failed %d\n", ret);
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001099 mutex_unlock(&nsock->tx_lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001100 }
1101}
1102
Josef Bacik29eaadc2017-04-06 17:01:59 -04001103static int nbd_disconnect(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001104{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001105 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001106
Josef Bacik5ea8d102017-04-06 17:01:58 -04001107 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Josef Bacik2e134562017-07-21 10:48:13 -04001108 set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
1109 send_disconnects(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001110 return 0;
1111}
1112
Josef Bacik29eaadc2017-04-06 17:01:59 -04001113static void nbd_clear_sock(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001114{
1115 sock_shutdown(nbd);
1116 nbd_clear_que(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001117 nbd->task_setup = NULL;
Josef Bacik9442b732017-02-07 17:10:22 -05001118}
1119
Josef Bacik5ea8d102017-04-06 17:01:58 -04001120static void nbd_config_put(struct nbd_device *nbd)
1121{
1122 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1123 &nbd->config_lock)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001124 struct nbd_config *config = nbd->config;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001125 nbd_dev_dbg_close(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001126 nbd_size_clear(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001127 if (test_and_clear_bit(NBD_HAS_PID_FILE,
1128 &config->runtime_flags))
1129 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1130 nbd->task_recv = NULL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001131 nbd_clear_sock(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001132 if (config->num_connections) {
1133 int i;
1134 for (i = 0; i < config->num_connections; i++) {
1135 sockfd_put(config->socks[i]->sock);
1136 kfree(config->socks[i]);
1137 }
1138 kfree(config->socks);
1139 }
Ilya Dryomovfa976532017-05-23 17:49:55 +02001140 kfree(nbd->config);
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001141 nbd->config = NULL;
1142
1143 nbd->tag_set.timeout = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001144 nbd->disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001145 nbd->disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001146 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001147 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Josef Bacika2c97902017-04-06 17:02:07 -04001148
Josef Bacik5ea8d102017-04-06 17:01:58 -04001149 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001150 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001151 module_put(THIS_MODULE);
1152 }
1153}
1154
Josef Bacike46c7282017-04-06 17:02:00 -04001155static int nbd_start_device(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001156{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001157 struct nbd_config *config = nbd->config;
1158 int num_connections = config->num_connections;
Josef Bacik9442b732017-02-07 17:10:22 -05001159 int error = 0, i;
1160
1161 if (nbd->task_recv)
1162 return -EBUSY;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001163 if (!config->socks)
Josef Bacik9442b732017-02-07 17:10:22 -05001164 return -EINVAL;
1165 if (num_connections > 1 &&
Josef Bacik5ea8d102017-04-06 17:01:58 -04001166 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
Josef Bacik9442b732017-02-07 17:10:22 -05001167 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001168 return -EINVAL;
Josef Bacik9442b732017-02-07 17:10:22 -05001169 }
1170
Josef Bacik5ea8d102017-04-06 17:01:58 -04001171 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
Josef Bacik9442b732017-02-07 17:10:22 -05001172 nbd->task_recv = current;
Josef Bacik9442b732017-02-07 17:10:22 -05001173
Josef Bacik29eaadc2017-04-06 17:01:59 -04001174 nbd_parse_flags(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001175
1176 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1177 if (error) {
1178 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001179 return error;
Josef Bacik9442b732017-02-07 17:10:22 -05001180 }
Josef Bacik29eaadc2017-04-06 17:01:59 -04001181 set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
Josef Bacik9442b732017-02-07 17:10:22 -05001182
1183 nbd_dev_dbg_init(nbd);
1184 for (i = 0; i < num_connections; i++) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001185 struct recv_thread_args *args;
1186
1187 args = kzalloc(sizeof(*args), GFP_KERNEL);
1188 if (!args) {
1189 sock_shutdown(nbd);
1190 return -ENOMEM;
1191 }
1192 sk_set_memalloc(config->socks[i]->sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001193 if (nbd->tag_set.timeout)
1194 config->socks[i]->sock->sk->sk_sndtimeo =
1195 nbd->tag_set.timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001196 atomic_inc(&config->recv_threads);
1197 refcount_inc(&nbd->config_refs);
1198 INIT_WORK(&args->work, recv_work);
1199 args->nbd = nbd;
1200 args->index = i;
1201 queue_work(recv_workqueue, &args->work);
Josef Bacik9442b732017-02-07 17:10:22 -05001202 }
Josef Bacik639812a2017-10-09 13:12:10 -04001203 nbd_size_update(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001204 return error;
1205}
1206
1207static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1208{
1209 struct nbd_config *config = nbd->config;
1210 int ret;
1211
1212 ret = nbd_start_device(nbd);
1213 if (ret)
1214 return ret;
1215
Josef Bacike46c7282017-04-06 17:02:00 -04001216 if (max_part)
1217 bdev->bd_invalidated = 1;
1218 mutex_unlock(&nbd->config_lock);
1219 ret = wait_event_interruptible(config->recv_wq,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001220 atomic_read(&config->recv_threads) == 0);
Josef Bacike46c7282017-04-06 17:02:00 -04001221 if (ret)
Josef Bacik5ea8d102017-04-06 17:01:58 -04001222 sock_shutdown(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001223 mutex_lock(&nbd->config_lock);
Josef Bacik76aa1d32018-05-16 14:51:22 -04001224 nbd_bdev_reset(bdev);
Josef Bacik9442b732017-02-07 17:10:22 -05001225 /* user requested, ignore socket errors */
Josef Bacik5ea8d102017-04-06 17:01:58 -04001226 if (test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001227 ret = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001228 if (test_bit(NBD_TIMEDOUT, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001229 ret = -ETIMEDOUT;
1230 return ret;
Josef Bacik9442b732017-02-07 17:10:22 -05001231}
Markus Pargmann30d53d92015-08-17 08:20:06 +02001232
Josef Bacik29eaadc2017-04-06 17:01:59 -04001233static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1234 struct block_device *bdev)
1235{
Josef Bacik2516ab12017-04-06 17:02:03 -04001236 sock_shutdown(nbd);
Munehisa Kamata2b5c8f02019-07-31 20:13:10 +08001237 __invalidate_device(bdev, true);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001238 nbd_bdev_reset(bdev);
Josef Bacike46c7282017-04-06 17:02:00 -04001239 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1240 &nbd->config->runtime_flags))
1241 nbd_config_put(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001242}
1243
Xiubo Li553768d2019-05-29 15:16:05 -05001244static bool nbd_is_valid_blksize(unsigned long blksize)
1245{
1246 if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
1247 blksize > PAGE_SIZE)
1248 return false;
1249 return true;
1250}
1251
Mike Christie55313e92019-08-13 11:39:49 -05001252static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
1253{
1254 nbd->tag_set.timeout = timeout * HZ;
1255 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1256}
1257
Josef Bacik9561a7a2016-11-22 14:04:40 -05001258/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -07001259static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -07001260 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001262 struct nbd_config *config = nbd->config;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -05001265 case NBD_DISCONNECT:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001266 return nbd_disconnect(nbd);
Markus Pargmann23272a672015-10-29 11:51:16 +01001267 case NBD_CLEAR_SOCK:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001268 nbd_clear_sock_ioctl(nbd, bdev);
1269 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001270 case NBD_SET_SOCK:
Josef Bacike46c7282017-04-06 17:02:00 -04001271 return nbd_add_socket(nbd, arg, false);
Josef Bacik9442b732017-02-07 17:10:22 -05001272 case NBD_SET_BLKSIZE:
Xiubo Li553768d2019-05-29 15:16:05 -05001273 if (!arg)
1274 arg = NBD_DEF_BLKSIZE;
1275 if (!nbd_is_valid_blksize(arg))
Jens Axboebc811f02018-09-04 11:52:34 -06001276 return -EINVAL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001277 nbd_size_set(nbd, arg,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001278 div_s64(config->bytesize, arg));
Josef Bacike5445412017-02-13 10:39:47 -05001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 case NBD_SET_SIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001281 nbd_size_set(nbd, config->blksize,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001282 div_s64(arg, config->blksize));
Josef Bacike5445412017-02-13 10:39:47 -05001283 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +02001284 case NBD_SET_SIZE_BLOCKS:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001285 nbd_size_set(nbd, config->blksize, arg);
Josef Bacike5445412017-02-13 10:39:47 -05001286 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -07001287 case NBD_SET_TIMEOUT:
Mike Christie55313e92019-08-13 11:39:49 -05001288 if (arg)
1289 nbd_set_cmd_timeout(nbd, arg);
Paul Clements7fdfd402007-10-16 23:27:37 -07001290 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001291
Paul Clements2f012502012-10-04 17:16:15 -07001292 case NBD_SET_FLAGS:
Josef Bacik5ea8d102017-04-06 17:01:58 -04001293 config->flags = arg;
Paul Clements2f012502012-10-04 17:16:15 -07001294 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001295 case NBD_DO_IT:
Josef Bacike46c7282017-04-06 17:02:00 -04001296 return nbd_start_device_ioctl(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -08001298 /*
1299 * This is for compatibility only. The queue is always cleared
1300 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -07001304 /*
1305 * For compatibility only, we no longer keep a list of
1306 * outstanding requests.
1307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309 }
Pavel Machek1a2ad212009-04-02 16:58:41 -07001310 return -ENOTTY;
1311}
1312
1313static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1314 unsigned int cmd, unsigned long arg)
1315{
Wanlong Gaof4507162012-03-28 14:42:51 -07001316 struct nbd_device *nbd = bdev->bd_disk->private_data;
Josef Bacike46c7282017-04-06 17:02:00 -04001317 struct nbd_config *config = nbd->config;
1318 int error = -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001319
1320 if (!capable(CAP_SYS_ADMIN))
1321 return -EPERM;
1322
Josef Bacik1dae69b2017-05-05 22:25:18 -04001323 /* The block layer will pass back some non-nbd ioctls in case we have
1324 * special handling for them, but we don't so just return an error.
1325 */
1326 if (_IOC_TYPE(cmd) != 0xab)
1327 return -EINVAL;
1328
Josef Bacik9561a7a2016-11-22 14:04:40 -05001329 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001330
1331 /* Don't allow ioctl operations on a nbd device that was created with
1332 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1333 */
1334 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1335 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1336 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1337 else
1338 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -05001339 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -07001340 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Josef Bacik5ea8d102017-04-06 17:01:58 -04001343static struct nbd_config *nbd_alloc_config(void)
1344{
1345 struct nbd_config *config;
1346
1347 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1348 if (!config)
1349 return NULL;
1350 atomic_set(&config->recv_threads, 0);
1351 init_waitqueue_head(&config->recv_wq);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001352 init_waitqueue_head(&config->conn_wait);
Xiubo Li553768d2019-05-29 15:16:05 -05001353 config->blksize = NBD_DEF_BLKSIZE;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001354 atomic_set(&config->live_connections, 0);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001355 try_module_get(THIS_MODULE);
1356 return config;
1357}
1358
1359static int nbd_open(struct block_device *bdev, fmode_t mode)
1360{
1361 struct nbd_device *nbd;
1362 int ret = 0;
1363
1364 mutex_lock(&nbd_index_mutex);
1365 nbd = bdev->bd_disk->private_data;
1366 if (!nbd) {
1367 ret = -ENXIO;
1368 goto out;
1369 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001370 if (!refcount_inc_not_zero(&nbd->refs)) {
1371 ret = -ENXIO;
1372 goto out;
1373 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001374 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1375 struct nbd_config *config;
1376
1377 mutex_lock(&nbd->config_lock);
1378 if (refcount_inc_not_zero(&nbd->config_refs)) {
1379 mutex_unlock(&nbd->config_lock);
1380 goto out;
1381 }
1382 config = nbd->config = nbd_alloc_config();
1383 if (!config) {
1384 ret = -ENOMEM;
1385 mutex_unlock(&nbd->config_lock);
1386 goto out;
1387 }
1388 refcount_set(&nbd->config_refs, 1);
Josef Bacikc6a47592017-04-06 17:02:06 -04001389 refcount_inc(&nbd->refs);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001390 mutex_unlock(&nbd->config_lock);
Josef Bacikfe1f9e62018-05-16 14:51:21 -04001391 bdev->bd_invalidated = 1;
1392 } else if (nbd_disconnected(nbd->config)) {
1393 bdev->bd_invalidated = 1;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001394 }
1395out:
1396 mutex_unlock(&nbd_index_mutex);
1397 return ret;
1398}
1399
1400static void nbd_release(struct gendisk *disk, fmode_t mode)
1401{
1402 struct nbd_device *nbd = disk->private_data;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001403 struct block_device *bdev = bdget_disk(disk, 0);
1404
1405 if (test_bit(NBD_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
1406 bdev->bd_openers == 0)
1407 nbd_disconnect_and_put(nbd);
1408
Josef Bacik5ea8d102017-04-06 17:01:58 -04001409 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001410 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001411}
1412
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001413static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 .owner = THIS_MODULE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001416 .open = nbd_open,
1417 .release = nbd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001418 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -05001419 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420};
1421
Markus Pargmann30d53d92015-08-17 08:20:06 +02001422#if IS_ENABLED(CONFIG_DEBUG_FS)
1423
1424static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1425{
1426 struct nbd_device *nbd = s->private;
1427
1428 if (nbd->task_recv)
1429 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +02001430
1431 return 0;
1432}
1433
1434static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1435{
1436 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1437}
1438
1439static const struct file_operations nbd_dbg_tasks_ops = {
1440 .open = nbd_dbg_tasks_open,
1441 .read = seq_read,
1442 .llseek = seq_lseek,
1443 .release = single_release,
1444};
1445
1446static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1447{
1448 struct nbd_device *nbd = s->private;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001449 u32 flags = nbd->config->flags;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001450
1451 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1452
1453 seq_puts(s, "Known flags:\n");
1454
1455 if (flags & NBD_FLAG_HAS_FLAGS)
1456 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1457 if (flags & NBD_FLAG_READ_ONLY)
1458 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1459 if (flags & NBD_FLAG_SEND_FLUSH)
1460 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
Shaun McDowell685c9b22017-05-25 23:55:54 -04001461 if (flags & NBD_FLAG_SEND_FUA)
1462 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
Markus Pargmann30d53d92015-08-17 08:20:06 +02001463 if (flags & NBD_FLAG_SEND_TRIM)
1464 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1465
1466 return 0;
1467}
1468
1469static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1470{
1471 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1472}
1473
1474static const struct file_operations nbd_dbg_flags_ops = {
1475 .open = nbd_dbg_flags_open,
1476 .read = seq_read,
1477 .llseek = seq_lseek,
1478 .release = single_release,
1479};
1480
1481static int nbd_dev_dbg_init(struct nbd_device *nbd)
1482{
1483 struct dentry *dir;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001484 struct nbd_config *config = nbd->config;
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001485
1486 if (!nbd_dbg_dir)
1487 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001488
1489 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001490 if (!dir) {
1491 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1492 nbd_name(nbd));
1493 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001494 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001495 config->dbg_dir = dir;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001496
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001497 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001498 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -07001499 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001500 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -04001501 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001502
1503 return 0;
1504}
1505
1506static void nbd_dev_dbg_close(struct nbd_device *nbd)
1507{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001508 debugfs_remove_recursive(nbd->config->dbg_dir);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001509}
1510
1511static int nbd_dbg_init(void)
1512{
1513 struct dentry *dbg_dir;
1514
1515 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001516 if (!dbg_dir)
1517 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001518
1519 nbd_dbg_dir = dbg_dir;
1520
1521 return 0;
1522}
1523
1524static void nbd_dbg_close(void)
1525{
1526 debugfs_remove_recursive(nbd_dbg_dir);
1527}
1528
1529#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1530
1531static int nbd_dev_dbg_init(struct nbd_device *nbd)
1532{
1533 return 0;
1534}
1535
1536static void nbd_dev_dbg_close(struct nbd_device *nbd)
1537{
1538}
1539
1540static int nbd_dbg_init(void)
1541{
1542 return 0;
1543}
1544
1545static void nbd_dbg_close(void)
1546{
1547}
1548
1549#endif
1550
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001551static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1552 unsigned int hctx_idx, unsigned int numa_node)
Josef Bacikfd8383f2016-09-08 12:33:37 -07001553{
1554 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001555 cmd->nbd = set->driver_data;
Josef Bacikd7d94d42018-07-16 12:11:34 -04001556 cmd->flags = 0;
Josef Bacik8f3ea352018-07-16 12:11:35 -04001557 mutex_init(&cmd->lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001558 return 0;
1559}
1560
Eric Biggersf363b082017-03-30 13:39:16 -07001561static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001562 .queue_rq = nbd_queue_rq,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +02001563 .complete = nbd_complete_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001564 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001565 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001566};
1567
Josef Bacikb0d91112017-02-01 16:11:40 -05001568static int nbd_dev_add(int index)
1569{
1570 struct nbd_device *nbd;
1571 struct gendisk *disk;
1572 struct request_queue *q;
1573 int err = -ENOMEM;
1574
1575 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1576 if (!nbd)
1577 goto out;
1578
1579 disk = alloc_disk(1 << part_shift);
1580 if (!disk)
1581 goto out_free_nbd;
1582
1583 if (index >= 0) {
1584 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1585 GFP_KERNEL);
1586 if (err == -ENOSPC)
1587 err = -EEXIST;
1588 } else {
1589 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1590 if (err >= 0)
1591 index = err;
1592 }
1593 if (err < 0)
1594 goto out_free_disk;
1595
Josef Bacike46c7282017-04-06 17:02:00 -04001596 nbd->index = index;
Josef Bacikb0d91112017-02-01 16:11:40 -05001597 nbd->disk = disk;
1598 nbd->tag_set.ops = &nbd_mq_ops;
1599 nbd->tag_set.nr_hw_queues = 1;
1600 nbd->tag_set.queue_depth = 128;
1601 nbd->tag_set.numa_node = NUMA_NO_NODE;
1602 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1603 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Ming Lei56d18f62019-02-15 19:13:24 +08001604 BLK_MQ_F_BLOCKING;
Josef Bacikb0d91112017-02-01 16:11:40 -05001605 nbd->tag_set.driver_data = nbd;
1606
1607 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1608 if (err)
1609 goto out_free_idr;
1610
1611 q = blk_mq_init_queue(&nbd->tag_set);
1612 if (IS_ERR(q)) {
1613 err = PTR_ERR(q);
1614 goto out_free_tags;
1615 }
1616 disk->queue = q;
1617
1618 /*
1619 * Tell the block layer that we are not a rotational device
1620 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08001621 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1622 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Josef Bacik6df133a2018-05-23 13:35:59 -04001623 disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001624 disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001625 blk_queue_max_discard_sectors(disk->queue, 0);
Josef Bacikebb16d02017-04-18 16:22:51 -04001626 blk_queue_max_segment_size(disk->queue, UINT_MAX);
Josef Bacik1cc1f172017-04-20 15:47:01 -04001627 blk_queue_max_segments(disk->queue, USHRT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001628 blk_queue_max_hw_sectors(disk->queue, 65536);
1629 disk->queue->limits.max_sectors = 256;
1630
Josef Bacikb0d91112017-02-01 16:11:40 -05001631 mutex_init(&nbd->config_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001632 refcount_set(&nbd->config_refs, 0);
Josef Bacikc6a47592017-04-06 17:02:06 -04001633 refcount_set(&nbd->refs, 1);
1634 INIT_LIST_HEAD(&nbd->list);
Josef Bacikb0d91112017-02-01 16:11:40 -05001635 disk->major = NBD_MAJOR;
1636 disk->first_minor = index << part_shift;
1637 disk->fops = &nbd_fops;
1638 disk->private_data = nbd;
1639 sprintf(disk->disk_name, "nbd%d", index);
Josef Bacikb0d91112017-02-01 16:11:40 -05001640 add_disk(disk);
Josef Bacik47d902b2017-04-06 17:02:05 -04001641 nbd_total_devices++;
Josef Bacikb0d91112017-02-01 16:11:40 -05001642 return index;
1643
1644out_free_tags:
1645 blk_mq_free_tag_set(&nbd->tag_set);
1646out_free_idr:
1647 idr_remove(&nbd_index_idr, index);
1648out_free_disk:
1649 put_disk(disk);
1650out_free_nbd:
1651 kfree(nbd);
1652out:
1653 return err;
1654}
1655
Josef Bacike46c7282017-04-06 17:02:00 -04001656static int find_free_cb(int id, void *ptr, void *data)
1657{
1658 struct nbd_device *nbd = ptr;
1659 struct nbd_device **found = data;
1660
1661 if (!refcount_read(&nbd->config_refs)) {
1662 *found = nbd;
1663 return 1;
1664 }
1665 return 0;
1666}
1667
1668/* Netlink interface. */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001669static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001670 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1671 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1672 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1673 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1674 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1675 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1676 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
Josef Bacik560bc4b2017-04-06 17:02:04 -04001677 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001678 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
Josef Bacike46c7282017-04-06 17:02:00 -04001679};
1680
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001681static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001682 [NBD_SOCK_FD] = { .type = NLA_U32 },
1683};
1684
Josef Bacik47d902b2017-04-06 17:02:05 -04001685/* We don't use this right now since we don't parse the incoming list, but we
1686 * still want it here so userspace knows what to expect.
1687 */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001688static const struct nla_policy __attribute__((unused))
Josef Bacik47d902b2017-04-06 17:02:05 -04001689nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1690 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1691 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1692};
1693
Mike Christie4ddeaae82019-05-29 15:16:06 -05001694static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
1695{
1696 struct nbd_config *config = nbd->config;
1697 u64 bsize = config->blksize;
1698 u64 bytes = config->bytesize;
1699
1700 if (info->attrs[NBD_ATTR_SIZE_BYTES])
1701 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1702
1703 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1704 bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1705 if (!bsize)
1706 bsize = NBD_DEF_BLKSIZE;
1707 if (!nbd_is_valid_blksize(bsize)) {
1708 printk(KERN_ERR "Invalid block size %llu\n", bsize);
1709 return -EINVAL;
1710 }
1711 }
1712
1713 if (bytes != config->bytesize || bsize != config->blksize)
1714 nbd_size_set(nbd, bsize, div64_u64(bytes, bsize));
1715 return 0;
1716}
1717
Josef Bacike46c7282017-04-06 17:02:00 -04001718static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1719{
1720 struct nbd_device *nbd = NULL;
1721 struct nbd_config *config;
1722 int index = -1;
1723 int ret;
Josef Bacika2c97902017-04-06 17:02:07 -04001724 bool put_dev = false;
Josef Bacike46c7282017-04-06 17:02:00 -04001725
1726 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1727 return -EPERM;
1728
1729 if (info->attrs[NBD_ATTR_INDEX])
1730 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1731 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1732 printk(KERN_ERR "nbd: must specify at least one socket\n");
1733 return -EINVAL;
1734 }
1735 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1736 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1737 return -EINVAL;
1738 }
1739again:
1740 mutex_lock(&nbd_index_mutex);
1741 if (index == -1) {
1742 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1743 if (ret == 0) {
1744 int new_index;
1745 new_index = nbd_dev_add(-1);
1746 if (new_index < 0) {
1747 mutex_unlock(&nbd_index_mutex);
1748 printk(KERN_ERR "nbd: failed to add new device\n");
Gustavo A. R. Silva09799622018-02-12 11:14:55 -06001749 return new_index;
Josef Bacike46c7282017-04-06 17:02:00 -04001750 }
1751 nbd = idr_find(&nbd_index_idr, new_index);
1752 }
1753 } else {
1754 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike6a76272017-08-14 18:25:33 +00001755 if (!nbd) {
1756 ret = nbd_dev_add(index);
1757 if (ret < 0) {
1758 mutex_unlock(&nbd_index_mutex);
1759 printk(KERN_ERR "nbd: failed to add new device\n");
1760 return ret;
1761 }
1762 nbd = idr_find(&nbd_index_idr, index);
1763 }
Josef Bacike46c7282017-04-06 17:02:00 -04001764 }
Josef Bacike46c7282017-04-06 17:02:00 -04001765 if (!nbd) {
1766 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1767 index);
Josef Bacikc6a47592017-04-06 17:02:06 -04001768 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001769 return -EINVAL;
1770 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001771 if (!refcount_inc_not_zero(&nbd->refs)) {
1772 mutex_unlock(&nbd_index_mutex);
1773 if (index == -1)
1774 goto again;
1775 printk(KERN_ERR "nbd: device at index %d is going down\n",
1776 index);
1777 return -EINVAL;
1778 }
1779 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001780
1781 mutex_lock(&nbd->config_lock);
1782 if (refcount_read(&nbd->config_refs)) {
1783 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001784 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001785 if (index == -1)
1786 goto again;
1787 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1788 return -EBUSY;
1789 }
1790 if (WARN_ON(nbd->config)) {
1791 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001792 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001793 return -EINVAL;
1794 }
1795 config = nbd->config = nbd_alloc_config();
1796 if (!nbd->config) {
1797 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001798 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001799 printk(KERN_ERR "nbd: couldn't allocate config\n");
1800 return -ENOMEM;
1801 }
1802 refcount_set(&nbd->config_refs, 1);
1803 set_bit(NBD_BOUND, &config->runtime_flags);
1804
Mike Christie4ddeaae82019-05-29 15:16:06 -05001805 ret = nbd_genl_size_set(info, nbd);
1806 if (ret)
1807 goto out;
1808
Mike Christie55313e92019-08-13 11:39:49 -05001809 if (info->attrs[NBD_ATTR_TIMEOUT])
1810 nbd_set_cmd_timeout(nbd,
1811 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04001812 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1813 config->dead_conn_timeout =
1814 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1815 config->dead_conn_timeout *= HZ;
1816 }
Josef Bacike46c7282017-04-06 17:02:00 -04001817 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1818 config->flags =
1819 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
Josef Bacika2c97902017-04-06 17:02:07 -04001820 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1821 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1822 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1823 set_bit(NBD_DESTROY_ON_DISCONNECT,
1824 &config->runtime_flags);
1825 put_dev = true;
1826 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001827 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
1828 set_bit(NBD_DISCONNECT_ON_CLOSE,
1829 &config->runtime_flags);
1830 }
Josef Bacika2c97902017-04-06 17:02:07 -04001831 }
1832
Josef Bacike46c7282017-04-06 17:02:00 -04001833 if (info->attrs[NBD_ATTR_SOCKETS]) {
1834 struct nlattr *attr;
1835 int rem, fd;
1836
1837 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1838 rem) {
1839 struct nlattr *socks[NBD_SOCK_MAX+1];
1840
1841 if (nla_type(attr) != NBD_SOCK_ITEM) {
1842 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1843 ret = -EINVAL;
1844 goto out;
1845 }
Johannes Berg8cb08172019-04-26 14:07:28 +02001846 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
1847 attr,
1848 nbd_sock_policy,
1849 info->extack);
Josef Bacike46c7282017-04-06 17:02:00 -04001850 if (ret != 0) {
1851 printk(KERN_ERR "nbd: error processing sock list\n");
1852 ret = -EINVAL;
1853 goto out;
1854 }
1855 if (!socks[NBD_SOCK_FD])
1856 continue;
1857 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1858 ret = nbd_add_socket(nbd, fd, true);
1859 if (ret)
1860 goto out;
1861 }
1862 }
1863 ret = nbd_start_device(nbd);
1864out:
1865 mutex_unlock(&nbd->config_lock);
1866 if (!ret) {
1867 set_bit(NBD_HAS_CONFIG_REF, &config->runtime_flags);
1868 refcount_inc(&nbd->config_refs);
1869 nbd_connect_reply(info, nbd->index);
1870 }
1871 nbd_config_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04001872 if (put_dev)
1873 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001874 return ret;
1875}
1876
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001877static void nbd_disconnect_and_put(struct nbd_device *nbd)
1878{
1879 mutex_lock(&nbd->config_lock);
1880 nbd_disconnect(nbd);
1881 nbd_clear_sock(nbd);
1882 mutex_unlock(&nbd->config_lock);
1883 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1884 &nbd->config->runtime_flags))
1885 nbd_config_put(nbd);
1886}
1887
Josef Bacike46c7282017-04-06 17:02:00 -04001888static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
1889{
1890 struct nbd_device *nbd;
1891 int index;
1892
1893 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1894 return -EPERM;
1895
1896 if (!info->attrs[NBD_ATTR_INDEX]) {
1897 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
1898 return -EINVAL;
1899 }
1900 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1901 mutex_lock(&nbd_index_mutex);
1902 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike46c7282017-04-06 17:02:00 -04001903 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001904 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001905 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1906 index);
1907 return -EINVAL;
1908 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001909 if (!refcount_inc_not_zero(&nbd->refs)) {
1910 mutex_unlock(&nbd_index_mutex);
1911 printk(KERN_ERR "nbd: device at index %d is going down\n",
1912 index);
1913 return -EINVAL;
1914 }
1915 mutex_unlock(&nbd_index_mutex);
1916 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1917 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001918 return 0;
Josef Bacikc6a47592017-04-06 17:02:06 -04001919 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001920 nbd_disconnect_and_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001921 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001922 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001923 return 0;
1924}
1925
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001926static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
1927{
1928 struct nbd_device *nbd = NULL;
1929 struct nbd_config *config;
1930 int index;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001931 int ret = 0;
Josef Bacika2c97902017-04-06 17:02:07 -04001932 bool put_dev = false;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001933
1934 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1935 return -EPERM;
1936
1937 if (!info->attrs[NBD_ATTR_INDEX]) {
1938 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
1939 return -EINVAL;
1940 }
1941 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1942 mutex_lock(&nbd_index_mutex);
1943 nbd = idr_find(&nbd_index_idr, index);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001944 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001945 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001946 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
1947 index);
1948 return -EINVAL;
1949 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001950 if (!refcount_inc_not_zero(&nbd->refs)) {
1951 mutex_unlock(&nbd_index_mutex);
1952 printk(KERN_ERR "nbd: device at index %d is going down\n",
1953 index);
1954 return -EINVAL;
1955 }
1956 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001957
1958 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1959 dev_err(nbd_to_dev(nbd),
1960 "not configured, cannot reconfigure\n");
Josef Bacikc6a47592017-04-06 17:02:06 -04001961 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001962 return -EINVAL;
1963 }
1964
1965 mutex_lock(&nbd->config_lock);
1966 config = nbd->config;
1967 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1968 !nbd->task_recv) {
1969 dev_err(nbd_to_dev(nbd),
1970 "not configured, cannot reconfigure\n");
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001971 ret = -EINVAL;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001972 goto out;
1973 }
1974
Mike Christie4ddeaae82019-05-29 15:16:06 -05001975 ret = nbd_genl_size_set(info, nbd);
1976 if (ret)
1977 goto out;
1978
Mike Christie55313e92019-08-13 11:39:49 -05001979 if (info->attrs[NBD_ATTR_TIMEOUT])
1980 nbd_set_cmd_timeout(nbd,
1981 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04001982 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1983 config->dead_conn_timeout =
1984 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1985 config->dead_conn_timeout *= HZ;
1986 }
Josef Bacika2c97902017-04-06 17:02:07 -04001987 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1988 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1989 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1990 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1991 &config->runtime_flags))
1992 put_dev = true;
1993 } else {
1994 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1995 &config->runtime_flags))
1996 refcount_inc(&nbd->refs);
1997 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001998
1999 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
2000 set_bit(NBD_DISCONNECT_ON_CLOSE,
2001 &config->runtime_flags);
2002 } else {
2003 clear_bit(NBD_DISCONNECT_ON_CLOSE,
2004 &config->runtime_flags);
2005 }
Josef Bacika2c97902017-04-06 17:02:07 -04002006 }
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002007
2008 if (info->attrs[NBD_ATTR_SOCKETS]) {
2009 struct nlattr *attr;
2010 int rem, fd;
2011
2012 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2013 rem) {
2014 struct nlattr *socks[NBD_SOCK_MAX+1];
2015
2016 if (nla_type(attr) != NBD_SOCK_ITEM) {
2017 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
2018 ret = -EINVAL;
2019 goto out;
2020 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002021 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2022 attr,
2023 nbd_sock_policy,
2024 info->extack);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002025 if (ret != 0) {
2026 printk(KERN_ERR "nbd: error processing sock list\n");
2027 ret = -EINVAL;
2028 goto out;
2029 }
2030 if (!socks[NBD_SOCK_FD])
2031 continue;
2032 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2033 ret = nbd_reconnect_socket(nbd, fd);
2034 if (ret) {
2035 if (ret == -ENOSPC)
2036 ret = 0;
2037 goto out;
2038 }
2039 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
2040 }
2041 }
2042out:
2043 mutex_unlock(&nbd->config_lock);
2044 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002045 nbd_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04002046 if (put_dev)
2047 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002048 return ret;
2049}
2050
Josef Bacike46c7282017-04-06 17:02:00 -04002051static const struct genl_ops nbd_connect_genl_ops[] = {
2052 {
2053 .cmd = NBD_CMD_CONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002054 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002055 .doit = nbd_genl_connect,
2056 },
2057 {
2058 .cmd = NBD_CMD_DISCONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002059 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002060 .doit = nbd_genl_disconnect,
2061 },
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002062 {
2063 .cmd = NBD_CMD_RECONFIGURE,
Johannes Bergef6243a2019-04-26 14:07:31 +02002064 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002065 .doit = nbd_genl_reconfigure,
2066 },
Josef Bacik47d902b2017-04-06 17:02:05 -04002067 {
2068 .cmd = NBD_CMD_STATUS,
Johannes Bergef6243a2019-04-26 14:07:31 +02002069 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacik47d902b2017-04-06 17:02:05 -04002070 .doit = nbd_genl_status,
2071 },
Josef Bacike46c7282017-04-06 17:02:00 -04002072};
2073
Josef Bacik799f9a32017-04-06 17:02:02 -04002074static const struct genl_multicast_group nbd_mcast_grps[] = {
2075 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2076};
2077
Josef Bacike46c7282017-04-06 17:02:00 -04002078static struct genl_family nbd_genl_family __ro_after_init = {
2079 .hdrsize = 0,
2080 .name = NBD_GENL_FAMILY_NAME,
2081 .version = NBD_GENL_VERSION,
2082 .module = THIS_MODULE,
2083 .ops = nbd_connect_genl_ops,
2084 .n_ops = ARRAY_SIZE(nbd_connect_genl_ops),
2085 .maxattr = NBD_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01002086 .policy = nbd_attr_policy,
Josef Bacik799f9a32017-04-06 17:02:02 -04002087 .mcgrps = nbd_mcast_grps,
2088 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
Josef Bacike46c7282017-04-06 17:02:00 -04002089};
2090
Josef Bacik47d902b2017-04-06 17:02:05 -04002091static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2092{
2093 struct nlattr *dev_opt;
2094 u8 connected = 0;
2095 int ret;
2096
2097 /* This is a little racey, but for status it's ok. The
2098 * reason we don't take a ref here is because we can't
2099 * take a ref in the index == -1 case as we would need
2100 * to put under the nbd_index_mutex, which could
2101 * deadlock if we are configured to remove ourselves
2102 * once we're disconnected.
2103 */
2104 if (refcount_read(&nbd->config_refs))
2105 connected = 1;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002106 dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM);
Josef Bacik47d902b2017-04-06 17:02:05 -04002107 if (!dev_opt)
2108 return -EMSGSIZE;
2109 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2110 if (ret)
2111 return -EMSGSIZE;
2112 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2113 connected);
2114 if (ret)
2115 return -EMSGSIZE;
2116 nla_nest_end(reply, dev_opt);
2117 return 0;
2118}
2119
2120static int status_cb(int id, void *ptr, void *data)
2121{
2122 struct nbd_device *nbd = ptr;
2123 return populate_nbd_status(nbd, (struct sk_buff *)data);
2124}
2125
2126static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2127{
2128 struct nlattr *dev_list;
2129 struct sk_buff *reply;
2130 void *reply_head;
2131 size_t msg_size;
2132 int index = -1;
2133 int ret = -ENOMEM;
2134
2135 if (info->attrs[NBD_ATTR_INDEX])
2136 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2137
2138 mutex_lock(&nbd_index_mutex);
2139
2140 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2141 nla_attr_size(sizeof(u8)));
2142 msg_size *= (index == -1) ? nbd_total_devices : 1;
2143
2144 reply = genlmsg_new(msg_size, GFP_KERNEL);
2145 if (!reply)
2146 goto out;
2147 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2148 NBD_CMD_STATUS);
2149 if (!reply_head) {
2150 nlmsg_free(reply);
2151 goto out;
2152 }
2153
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002154 dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST);
Josef Bacik47d902b2017-04-06 17:02:05 -04002155 if (index == -1) {
2156 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2157 if (ret) {
2158 nlmsg_free(reply);
2159 goto out;
2160 }
2161 } else {
2162 struct nbd_device *nbd;
2163 nbd = idr_find(&nbd_index_idr, index);
2164 if (nbd) {
2165 ret = populate_nbd_status(nbd, reply);
2166 if (ret) {
2167 nlmsg_free(reply);
2168 goto out;
2169 }
2170 }
2171 }
2172 nla_nest_end(reply, dev_list);
2173 genlmsg_end(reply, reply_head);
Li RongQingcd46eb82019-02-19 13:14:07 +08002174 ret = genlmsg_reply(reply, info);
Josef Bacik47d902b2017-04-06 17:02:05 -04002175out:
2176 mutex_unlock(&nbd_index_mutex);
2177 return ret;
2178}
2179
Josef Bacike46c7282017-04-06 17:02:00 -04002180static void nbd_connect_reply(struct genl_info *info, int index)
2181{
2182 struct sk_buff *skb;
2183 void *msg_head;
2184 int ret;
2185
2186 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2187 if (!skb)
2188 return;
2189 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2190 NBD_CMD_CONNECT);
2191 if (!msg_head) {
2192 nlmsg_free(skb);
2193 return;
2194 }
2195 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2196 if (ret) {
2197 nlmsg_free(skb);
2198 return;
2199 }
2200 genlmsg_end(skb, msg_head);
2201 genlmsg_reply(skb, info);
2202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Josef Bacik799f9a32017-04-06 17:02:02 -04002204static void nbd_mcast_index(int index)
2205{
2206 struct sk_buff *skb;
2207 void *msg_head;
2208 int ret;
2209
2210 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2211 if (!skb)
2212 return;
2213 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2214 NBD_CMD_LINK_DEAD);
2215 if (!msg_head) {
2216 nlmsg_free(skb);
2217 return;
2218 }
2219 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2220 if (ret) {
2221 nlmsg_free(skb);
2222 return;
2223 }
2224 genlmsg_end(skb, msg_head);
2225 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2226}
2227
2228static void nbd_dead_link_work(struct work_struct *work)
2229{
2230 struct link_dead_args *args = container_of(work, struct link_dead_args,
2231 work);
2232 nbd_mcast_index(args->index);
2233 kfree(args);
2234}
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236static int __init nbd_init(void)
2237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int i;
2239
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08002240 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002242 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02002243 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002244 return -EINVAL;
2245 }
2246
2247 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02002248 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002249 part_shift = fls(max_part);
2250
Namhyung Kim5988ce22011-05-28 14:44:46 +02002251 /*
2252 * Adjust max_part according to part_shift as it is exported
2253 * to user space so that user can know the max number of
2254 * partition kernel should be able to manage.
2255 *
2256 * Note that -1 is required because partition 0 is reserved
2257 * for the whole disk.
2258 */
2259 max_part = (1UL << part_shift) - 1;
2260 }
2261
Namhyung Kim3b271082011-05-28 14:44:46 +02002262 if ((1UL << part_shift) > DISK_MAX_PARTS)
2263 return -EINVAL;
2264
2265 if (nbds_max > 1UL << (MINORBITS - part_shift))
2266 return -EINVAL;
Josef Bacik124d6db2017-02-01 16:11:11 -05002267 recv_workqueue = alloc_workqueue("knbd-recv",
Dan Melnic2189c972017-09-18 13:08:51 -07002268 WQ_MEM_RECLAIM | WQ_HIGHPRI |
2269 WQ_UNBOUND, 0);
Josef Bacik124d6db2017-02-01 16:11:11 -05002270 if (!recv_workqueue)
2271 return -ENOMEM;
Namhyung Kim3b271082011-05-28 14:44:46 +02002272
Josef Bacik6330a2d2017-02-15 16:49:48 -05002273 if (register_blkdev(NBD_MAJOR, "nbd")) {
2274 destroy_workqueue(recv_workqueue);
Josef Bacikb0d91112017-02-01 16:11:40 -05002275 return -EIO;
Josef Bacik6330a2d2017-02-15 16:49:48 -05002276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Josef Bacike46c7282017-04-06 17:02:00 -04002278 if (genl_register_family(&nbd_genl_family)) {
2279 unregister_blkdev(NBD_MAJOR, "nbd");
2280 destroy_workqueue(recv_workqueue);
2281 return -EINVAL;
2282 }
Markus Pargmann30d53d92015-08-17 08:20:06 +02002283 nbd_dbg_init();
2284
Josef Bacikb0d91112017-02-01 16:11:40 -05002285 mutex_lock(&nbd_index_mutex);
2286 for (i = 0; i < nbds_max; i++)
2287 nbd_dev_add(i);
2288 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05002290}
2291
2292static int nbd_exit_cb(int id, void *ptr, void *data)
2293{
Josef Bacikc6a47592017-04-06 17:02:06 -04002294 struct list_head *list = (struct list_head *)data;
Josef Bacikb0d91112017-02-01 16:11:40 -05002295 struct nbd_device *nbd = ptr;
Josef Bacikc6a47592017-04-06 17:02:06 -04002296
Josef Bacikc6a47592017-04-06 17:02:06 -04002297 list_add_tail(&nbd->list, list);
Josef Bacikb0d91112017-02-01 16:11:40 -05002298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
2301static void __exit nbd_cleanup(void)
2302{
Josef Bacikc6a47592017-04-06 17:02:06 -04002303 struct nbd_device *nbd;
2304 LIST_HEAD(del_list);
2305
Markus Pargmann30d53d92015-08-17 08:20:06 +02002306 nbd_dbg_close();
2307
Josef Bacikc6a47592017-04-06 17:02:06 -04002308 mutex_lock(&nbd_index_mutex);
2309 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2310 mutex_unlock(&nbd_index_mutex);
2311
Josef Bacik60ae36a2017-04-28 09:49:19 -04002312 while (!list_empty(&del_list)) {
2313 nbd = list_first_entry(&del_list, struct nbd_device, list);
2314 list_del_init(&nbd->list);
2315 if (refcount_read(&nbd->refs) != 1)
Josef Bacikc6a47592017-04-06 17:02:06 -04002316 printk(KERN_ERR "nbd: possibly leaking a device\n");
2317 nbd_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002318 }
2319
Josef Bacikb0d91112017-02-01 16:11:40 -05002320 idr_destroy(&nbd_index_idr);
Josef Bacike46c7282017-04-06 17:02:00 -04002321 genl_unregister_family(&nbd_genl_family);
Josef Bacik124d6db2017-02-01 16:11:11 -05002322 destroy_workqueue(recv_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
2326module_init(nbd_init);
2327module_exit(nbd_cleanup);
2328
2329MODULE_DESCRIPTION("Network Block Device");
2330MODULE_LICENSE("GPL");
2331
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07002332module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002333MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2334module_param(max_part, int, 0444);
Josef Bacik7a8362a2017-08-14 18:56:16 +00002335MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");