blob: 90c2effb5ded8d1048551c1130e8f17c06c8f21b [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>
Xiubo Li8454d682019-09-17 17:26:06 +053029#include <linux/completion.h>
Herbert Xu4b2f0262006-01-06 00:09:47 -080030#include <linux/err.h>
31#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <net/sock.h>
Trond Myklebust91cf45f2007-11-12 18:10:39 -080034#include <linux/net.h>
Laurent Vivier48cf6062008-04-29 01:02:46 -070035#include <linux/kthread.h>
Markus Pargmannb9c495b2015-04-02 10:11:37 +020036#include <linux/types.h>
Markus Pargmann30d53d92015-08-17 08:20:06 +020037#include <linux/debugfs.h>
Josef Bacikfd8383f2016-09-08 12:33:37 -070038#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/types.h>
42
43#include <linux/nbd.h>
Josef Bacike46c7282017-04-06 17:02:00 -040044#include <linux/nbd-netlink.h>
45#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Matt Mullinsea106722019-04-26 11:49:48 -070047#define CREATE_TRACE_POINTS
48#include <trace/events/nbd.h>
49
Josef Bacikb0d91112017-02-01 16:11:40 -050050static DEFINE_IDR(nbd_index_idr);
51static DEFINE_MUTEX(nbd_index_mutex);
Josef Bacik47d902b2017-04-06 17:02:05 -040052static int nbd_total_devices = 0;
Josef Bacikb0d91112017-02-01 16:11:40 -050053
Josef Bacik9561a7a2016-11-22 14:04:40 -050054struct nbd_sock {
55 struct socket *sock;
56 struct mutex tx_lock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -040057 struct request *pending;
58 int sent;
Josef Bacikf3733242017-04-06 17:01:57 -040059 bool dead;
60 int fallback_index;
Josef Bacik799f9a32017-04-06 17:02:02 -040061 int cookie;
Josef Bacik9561a7a2016-11-22 14:04:40 -050062};
63
Josef Bacik5ea8d102017-04-06 17:01:58 -040064struct recv_thread_args {
65 struct work_struct work;
66 struct nbd_device *nbd;
67 int index;
68};
69
Josef Bacik799f9a32017-04-06 17:02:02 -040070struct link_dead_args {
71 struct work_struct work;
72 int index;
73};
74
Xiubo Liec76a7b2019-09-17 17:26:05 +053075#define NBD_RT_TIMEDOUT 0
76#define NBD_RT_DISCONNECT_REQUESTED 1
77#define NBD_RT_DISCONNECTED 2
78#define NBD_RT_HAS_PID_FILE 3
79#define NBD_RT_HAS_CONFIG_REF 4
80#define NBD_RT_BOUND 5
81#define NBD_RT_DESTROY_ON_DISCONNECT 6
82#define NBD_RT_DISCONNECT_ON_CLOSE 7
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070083
Xiubo Li8454d682019-09-17 17:26:06 +053084#define NBD_DESTROY_ON_DISCONNECT 0
85#define NBD_DISCONNECT_REQUESTED 1
86
Josef Bacik5ea8d102017-04-06 17:01:58 -040087struct nbd_config {
Markus Pargmann22d109c2015-08-17 08:20:09 +020088 u32 flags;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070089 unsigned long runtime_flags;
Josef Bacik560bc4b2017-04-06 17:02:04 -040090 u64 dead_conn_timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -040091
Josef Bacik9561a7a2016-11-22 14:04:40 -050092 struct nbd_sock **socks;
Josef Bacik9561a7a2016-11-22 14:04:40 -050093 int num_connections;
Josef Bacik560bc4b2017-04-06 17:02:04 -040094 atomic_t live_connections;
95 wait_queue_head_t conn_wait;
Josef Bacik5ea8d102017-04-06 17:01:58 -040096
Josef Bacik9561a7a2016-11-22 14:04:40 -050097 atomic_t recv_threads;
98 wait_queue_head_t recv_wq;
Josef Bacikef77b512016-12-02 16:19:12 -050099 loff_t blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +0200100 loff_t bytesize;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200101#if IS_ENABLED(CONFIG_DEBUG_FS)
102 struct dentry *dbg_dir;
103#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +0200104};
105
Josef Bacik5ea8d102017-04-06 17:01:58 -0400106struct nbd_device {
107 struct blk_mq_tag_set tag_set;
108
Josef Bacike46c7282017-04-06 17:02:00 -0400109 int index;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400110 refcount_t config_refs;
Josef Bacikc6a47592017-04-06 17:02:06 -0400111 refcount_t refs;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400112 struct nbd_config *config;
113 struct mutex config_lock;
114 struct gendisk *disk;
Mike Christiee9e006f2019-08-04 14:10:06 -0500115 struct workqueue_struct *recv_workq;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400116
Josef Bacikc6a47592017-04-06 17:02:06 -0400117 struct list_head list;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400118 struct task_struct *task_recv;
119 struct task_struct *task_setup;
Xiubo Li8454d682019-09-17 17:26:06 +0530120
121 struct completion *destroy_complete;
122 unsigned long flags;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400123};
124
Josef Bacikd7d94d42018-07-16 12:11:34 -0400125#define NBD_CMD_REQUEUED 1
126
Josef Bacikfd8383f2016-09-08 12:33:37 -0700127struct nbd_cmd {
128 struct nbd_device *nbd;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400129 struct mutex lock;
Josef Bacikf3733242017-04-06 17:01:57 -0400130 int index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400131 int cookie;
Mike Christie2da22da2019-08-13 11:39:52 -0500132 int retries;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200133 blk_status_t status;
Josef Bacikd7d94d42018-07-16 12:11:34 -0400134 unsigned long flags;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400135 u32 cmd_cookie;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700136};
137
Markus Pargmann30d53d92015-08-17 08:20:06 +0200138#if IS_ENABLED(CONFIG_DEBUG_FS)
139static struct dentry *nbd_dbg_dir;
140#endif
141
142#define nbd_name(nbd) ((nbd)->disk->disk_name)
143
Wanlong Gaof4507162012-03-28 14:42:51 -0700144#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Xiubo Li553768d2019-05-29 15:16:05 -0500146#define NBD_DEF_BLKSIZE 1024
147
Ingo van Lil9c7a4162006-07-01 04:36:36 -0700148static unsigned int nbds_max = 16;
Josef Bacik7a8362a2017-08-14 18:56:16 +0000149static int max_part = 16;
Josef Bacikb0d91112017-02-01 16:11:40 -0500150static int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Josef Bacik9442b732017-02-07 17:10:22 -0500152static int nbd_dev_dbg_init(struct nbd_device *nbd);
153static void nbd_dev_dbg_close(struct nbd_device *nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400154static void nbd_config_put(struct nbd_device *nbd);
Josef Bacike46c7282017-04-06 17:02:00 -0400155static void nbd_connect_reply(struct genl_info *info, int index);
Josef Bacik47d902b2017-04-06 17:02:05 -0400156static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
Josef Bacik799f9a32017-04-06 17:02:02 -0400157static void nbd_dead_link_work(struct work_struct *work);
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -0700158static void nbd_disconnect_and_put(struct nbd_device *nbd);
Josef Bacik9442b732017-02-07 17:10:22 -0500159
Markus Pargmannd18509f2015-04-02 10:11:38 +0200160static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Markus Pargmannd18509f2015-04-02 10:11:38 +0200162 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Josef Bacikd7d94d42018-07-16 12:11:34 -0400165static void nbd_requeue_cmd(struct nbd_cmd *cmd)
166{
167 struct request *req = blk_mq_rq_from_pdu(cmd);
168
169 if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
170 blk_mq_requeue_request(req, true);
171}
172
Josef Bacik8f3ea352018-07-16 12:11:35 -0400173#define NBD_COOKIE_BITS 32
174
175static u64 nbd_cmd_handle(struct nbd_cmd *cmd)
176{
177 struct request *req = blk_mq_rq_from_pdu(cmd);
178 u32 tag = blk_mq_unique_tag(req);
179 u64 cookie = cmd->cmd_cookie;
180
181 return (cookie << NBD_COOKIE_BITS) | tag;
182}
183
184static u32 nbd_handle_to_tag(u64 handle)
185{
186 return (u32)handle;
187}
188
189static u32 nbd_handle_to_cookie(u64 handle)
190{
191 return (u32)(handle >> NBD_COOKIE_BITS);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static const char *nbdcmd_to_ascii(int cmd)
195{
196 switch (cmd) {
197 case NBD_CMD_READ: return "read";
198 case NBD_CMD_WRITE: return "write";
199 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800200 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700201 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 return "invalid";
204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Josef Bacik5ea8d102017-04-06 17:01:58 -0400206static ssize_t pid_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
209 struct gendisk *disk = dev_to_disk(dev);
210 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
211
212 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
213}
214
Bhumika Goyaldfbde552017-08-21 17:13:08 +0530215static const struct device_attribute pid_attr = {
Joe Perches5657a812018-05-24 13:38:59 -0600216 .attr = { .name = "pid", .mode = 0444},
Josef Bacik5ea8d102017-04-06 17:01:58 -0400217 .show = pid_show,
218};
219
Josef Bacikc6a47592017-04-06 17:02:06 -0400220static void nbd_dev_remove(struct nbd_device *nbd)
221{
222 struct gendisk *disk = nbd->disk;
Josef Bacik8364da42018-05-16 14:51:17 -0400223 struct request_queue *q;
224
Josef Bacikc6a47592017-04-06 17:02:06 -0400225 if (disk) {
Josef Bacik8364da42018-05-16 14:51:17 -0400226 q = disk->queue;
Josef Bacikc6a47592017-04-06 17:02:06 -0400227 del_gendisk(disk);
Josef Bacik8364da42018-05-16 14:51:17 -0400228 blk_cleanup_queue(q);
Josef Bacikc6a47592017-04-06 17:02:06 -0400229 blk_mq_free_tag_set(&nbd->tag_set);
Josef Bacika2c97902017-04-06 17:02:07 -0400230 disk->private_data = NULL;
Josef Bacikc6a47592017-04-06 17:02:06 -0400231 put_disk(disk);
232 }
Xiubo Li8454d682019-09-17 17:26:06 +0530233
234 /*
235 * Place this in the last just before the nbd is freed to
236 * make sure that the disk and the related kobject are also
237 * totally removed to avoid duplicate creation of the same
238 * one.
239 */
240 if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) && nbd->destroy_complete)
241 complete(nbd->destroy_complete);
242
Josef Bacikc6a47592017-04-06 17:02:06 -0400243 kfree(nbd);
244}
245
246static void nbd_put(struct nbd_device *nbd)
247{
248 if (refcount_dec_and_mutex_lock(&nbd->refs,
249 &nbd_index_mutex)) {
250 idr_remove(&nbd_index_idr, nbd->index);
Josef Bacikc6a47592017-04-06 17:02:06 -0400251 nbd_dev_remove(nbd);
Xiubo Li86248812019-09-19 11:44:27 +0530252 mutex_unlock(&nbd_index_mutex);
Josef Bacikc6a47592017-04-06 17:02:06 -0400253 }
254}
255
Josef Bacik799f9a32017-04-06 17:02:02 -0400256static int nbd_disconnected(struct nbd_config *config)
Josef Bacikf3733242017-04-06 17:01:57 -0400257{
Xiubo Liec76a7b2019-09-17 17:26:05 +0530258 return test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags) ||
259 test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
Josef Bacik799f9a32017-04-06 17:02:02 -0400260}
261
262static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
263 int notify)
264{
265 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
266 struct link_dead_args *args;
267 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
268 if (args) {
269 INIT_WORK(&args->work, nbd_dead_link_work);
270 args->index = nbd->index;
271 queue_work(system_wq, &args->work);
272 }
273 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400274 if (!nsock->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400275 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600276 if (atomic_dec_return(&nbd->config->live_connections) == 0) {
Xiubo Liec76a7b2019-09-17 17:26:05 +0530277 if (test_and_clear_bit(NBD_RT_DISCONNECT_REQUESTED,
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600278 &nbd->config->runtime_flags)) {
Xiubo Liec76a7b2019-09-17 17:26:05 +0530279 set_bit(NBD_RT_DISCONNECTED,
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600280 &nbd->config->runtime_flags);
281 dev_info(nbd_to_dev(nbd),
282 "Disconnected due to user request.\n");
283 }
284 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400285 }
Josef Bacikf3733242017-04-06 17:01:57 -0400286 nsock->dead = true;
287 nsock->pending = NULL;
288 nsock->sent = 0;
289}
290
Josef Bacik29eaadc2017-04-06 17:01:59 -0400291static void nbd_size_clear(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200292{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400293 if (nbd->config->bytesize) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400294 set_capacity(nbd->disk, 0);
295 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
296 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200297}
298
Josef Bacik29eaadc2017-04-06 17:01:59 -0400299static void nbd_size_update(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200300{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400301 struct nbd_config *config = nbd->config;
Josef Bacik9e2b19672018-05-16 14:51:19 -0400302 struct block_device *bdev = bdget_disk(nbd->disk, 0);
Christoph Hellwig611bee52020-08-23 11:10:41 +0200303 sector_t nr_sectors = config->bytesize >> 9;
Josef Bacik9e2b19672018-05-16 14:51:19 -0400304
Josef Bacik6df133a2018-05-23 13:35:59 -0400305 if (config->flags & NBD_FLAG_SEND_TRIM) {
306 nbd->disk->queue->limits.discard_granularity = config->blksize;
Josef Bacik07ce2132018-06-05 11:41:23 -0400307 nbd->disk->queue->limits.discard_alignment = config->blksize;
Josef Bacik6df133a2018-05-23 13:35:59 -0400308 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
309 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400310 blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
311 blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
Christoph Hellwig611bee52020-08-23 11:10:41 +0200312 set_capacity(nbd->disk, nr_sectors);
Josef Bacik9e2b19672018-05-16 14:51:19 -0400313 if (bdev) {
Jan Karac8a83a62019-01-14 09:48:09 +0100314 if (bdev->bd_disk) {
Christoph Hellwig611bee52020-08-23 11:10:41 +0200315 bd_set_nr_sectors(bdev, nr_sectors);
Jan Karac8a83a62019-01-14 09:48:09 +0100316 set_blocksize(bdev, config->blksize);
317 } else
Christoph Hellwig38430f02020-09-21 09:19:45 +0200318 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
Josef Bacik9e2b19672018-05-16 14:51:19 -0400319 bdput(bdev);
320 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200321 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
322}
323
Josef Bacik29eaadc2017-04-06 17:01:59 -0400324static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
325 loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200326{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400327 struct nbd_config *config = nbd->config;
328 config->blksize = blocksize;
329 config->bytesize = blocksize * nr_blocks;
Josef Bacikc3f7c932018-05-16 14:51:18 -0400330 if (nbd->task_recv != NULL)
331 nbd_size_update(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200332}
333
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200334static void nbd_complete_rq(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200336 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Kevin Vigoree57a052018-06-04 10:40:12 -0600338 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200339 cmd->status ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200341 blk_mq_end_request(req, cmd->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Markus Pargmanne018e752015-04-02 10:11:39 +0200344/*
345 * Forcibly shutdown the socket causing all listeners to error
346 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200347static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700348{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400349 struct nbd_config *config = nbd->config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500350 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700351
Josef Bacik5ea8d102017-04-06 17:01:58 -0400352 if (config->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200353 return;
Xiubo Liec76a7b2019-09-17 17:26:05 +0530354 if (test_and_set_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500355 return;
356
Josef Bacik5ea8d102017-04-06 17:01:58 -0400357 for (i = 0; i < config->num_connections; i++) {
358 struct nbd_sock *nsock = config->socks[i];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500359 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400360 nbd_mark_nsock_dead(nbd, nsock, 0);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500361 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100362 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500363 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700364}
365
Mike Christie00514672019-08-13 11:39:50 -0500366static u32 req_to_nbd_cmd_type(struct request *req)
367{
368 switch (req_op(req)) {
369 case REQ_OP_DISCARD:
370 return NBD_CMD_TRIM;
371 case REQ_OP_FLUSH:
372 return NBD_CMD_FLUSH;
373 case REQ_OP_WRITE:
374 return NBD_CMD_WRITE;
375 case REQ_OP_READ:
376 return NBD_CMD_READ;
377 default:
378 return U32_MAX;
379 }
380}
381
Josef Bacik0eadf372016-09-08 12:33:40 -0700382static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
383 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700384{
Josef Bacik0eadf372016-09-08 12:33:40 -0700385 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
386 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400387 struct nbd_config *config;
Paul Clements7fdfd402007-10-16 23:27:37 -0700388
Josef Bacikde6346e2019-10-21 15:56:27 -0400389 if (!mutex_trylock(&cmd->lock))
390 return BLK_EH_RESET_TIMER;
391
Josef Bacik5ea8d102017-04-06 17:01:58 -0400392 if (!refcount_inc_not_zero(&nbd->config_refs)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200393 cmd->status = BLK_STS_TIMEOUT;
Josef Bacikde6346e2019-10-21 15:56:27 -0400394 mutex_unlock(&cmd->lock);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200395 goto done;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400396 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400397 config = nbd->config;
398
Hou Pud9709582020-02-28 01:40:29 -0500399 if (config->num_connections > 1 ||
400 (config->num_connections == 1 && nbd->tag_set.timeout)) {
Josef Bacikf3733242017-04-06 17:01:57 -0400401 dev_err_ratelimited(nbd_to_dev(nbd),
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600402 "Connection timed out, retrying (%d/%d alive)\n",
403 atomic_read(&config->live_connections),
404 config->num_connections);
Josef Bacikf3733242017-04-06 17:01:57 -0400405 /*
406 * Hooray we have more connections, requeue this IO, the submit
Hou Pud9709582020-02-28 01:40:29 -0500407 * path will put it on a real connection. Or if only one
408 * connection is configured, the submit path will wait util
409 * a new connection is reconfigured or util dead timeout.
Josef Bacikf3733242017-04-06 17:01:57 -0400410 */
Hou Pud9709582020-02-28 01:40:29 -0500411 if (config->socks) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400412 if (cmd->index < config->num_connections) {
Josef Bacikf3733242017-04-06 17:01:57 -0400413 struct nbd_sock *nsock =
Josef Bacik5ea8d102017-04-06 17:01:58 -0400414 config->socks[cmd->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400415 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400416 /* We can have multiple outstanding requests, so
417 * we don't want to mark the nsock dead if we've
418 * already reconnected with a new socket, so
419 * only mark it dead if its the same socket we
420 * were sent out on.
421 */
422 if (cmd->cookie == nsock->cookie)
423 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400424 mutex_unlock(&nsock->tx_lock);
425 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400426 mutex_unlock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400427 nbd_requeue_cmd(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400428 nbd_config_put(nbd);
Christoph Hellwig66005932018-05-29 15:52:29 +0200429 return BLK_EH_DONE;
Josef Bacikf3733242017-04-06 17:01:57 -0400430 }
Josef Bacikf3733242017-04-06 17:01:57 -0400431 }
Mike Christie2da22da2019-08-13 11:39:52 -0500432
433 if (!nbd->tag_set.timeout) {
434 /*
435 * Userspace sets timeout=0 to disable socket disconnection,
436 * so just warn and reset the timer.
437 */
Hou Pu2c272542020-02-28 01:40:30 -0500438 struct nbd_sock *nsock = config->socks[cmd->index];
Mike Christie2da22da2019-08-13 11:39:52 -0500439 cmd->retries++;
440 dev_info(nbd_to_dev(nbd), "Possible stuck request %p: control (%s@%llu,%uB). Runtime %u seconds\n",
441 req, nbdcmd_to_ascii(req_to_nbd_cmd_type(req)),
442 (unsigned long long)blk_rq_pos(req) << 9,
443 blk_rq_bytes(req), (req->timeout / HZ) * cmd->retries);
444
Hou Pu2c272542020-02-28 01:40:30 -0500445 mutex_lock(&nsock->tx_lock);
446 if (cmd->cookie != nsock->cookie) {
447 nbd_requeue_cmd(cmd);
448 mutex_unlock(&nsock->tx_lock);
449 mutex_unlock(&cmd->lock);
450 nbd_config_put(nbd);
451 return BLK_EH_DONE;
452 }
453 mutex_unlock(&nsock->tx_lock);
Mike Christie2da22da2019-08-13 11:39:52 -0500454 mutex_unlock(&cmd->lock);
455 nbd_config_put(nbd);
456 return BLK_EH_RESET_TIMER;
457 }
458
459 dev_err_ratelimited(nbd_to_dev(nbd), "Connection timed out\n");
Xiubo Liec76a7b2019-09-17 17:26:05 +0530460 set_bit(NBD_RT_TIMEDOUT, &config->runtime_flags);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200461 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400462 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500463 sock_shutdown(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400464 nbd_config_put(nbd);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200465done:
466 blk_mq_complete_request(req);
467 return BLK_EH_DONE;
Paul Clements7fdfd402007-10-16 23:27:37 -0700468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
471 * Send or receive packet.
472 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500473static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400474 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400476 struct nbd_config *config = nbd->config;
477 struct socket *sock = config->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int result;
479 struct msghdr msg;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700480 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700482 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500483 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200484 "Attempted %s on closed socket in sock_xmit\n",
485 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700486 return -EINVAL;
487 }
488
Al Viroc9f2b6a2015-11-12 05:09:35 -0500489 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500490
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700491 noreclaim_flag = memalloc_noreclaim_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700493 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 msg.msg_name = NULL;
495 msg.msg_namelen = 0;
496 msg.msg_control = NULL;
497 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
499
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200500 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500501 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200502 else
Al Viroc1696ca2015-11-12 04:51:19 -0500503 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (result <= 0) {
506 if (result == 0)
507 result = -EPIPE; /* short read */
508 break;
509 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400510 if (sent)
511 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500512 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700514 memalloc_noreclaim_restore(noreclaim_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return result;
517}
518
Josef Bacik32e67a32017-10-24 15:57:18 -0400519/*
520 * Different settings for sk->sk_sndtimeo can result in different return values
521 * if there is a signal pending when we enter sendmsg, because reasons?
522 */
523static inline int was_interrupted(int result)
524{
525 return result == -ERESTARTSYS || result == -EINTR;
526}
527
Paul Clements7fdfd402007-10-16 23:27:37 -0700528/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500529static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700531 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400532 struct nbd_config *config = nbd->config;
533 struct nbd_sock *nsock = config->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500534 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500535 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
536 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
537 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900538 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700539 struct bio *bio;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400540 u64 handle;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200541 u32 type;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400542 u32 nbd_cmd_flags = 0;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400543 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200544
David Howellsaa563d72018-10-20 00:57:56 +0100545 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500546
Mike Christie00514672019-08-13 11:39:50 -0500547 type = req_to_nbd_cmd_type(req);
548 if (type == U32_MAX)
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100549 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100551 if (rq_data_dir(req) == WRITE &&
Josef Bacik5ea8d102017-04-06 17:01:58 -0400552 (config->flags & NBD_FLAG_READ_ONLY)) {
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100553 dev_err_ratelimited(disk_to_dev(nbd->disk),
554 "Write on read-only\n");
555 return -EIO;
556 }
557
Shaun McDowell685c9b22017-05-25 23:55:54 -0400558 if (req->cmd_flags & REQ_FUA)
559 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
560
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400561 /* We did a partial send previously, and we at least sent the whole
562 * request struct, so just go and send the rest of the pages in the
563 * request.
564 */
565 if (sent) {
566 if (sent >= sizeof(request)) {
567 skip = sent - sizeof(request);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700568
569 /* initialize handle for tracing purposes */
570 handle = nbd_cmd_handle(cmd);
571
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400572 goto send_pages;
573 }
574 iov_iter_advance(&from, sent);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400575 } else {
576 cmd->cmd_cookie++;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400577 }
Josef Bacikf3733242017-04-06 17:01:57 -0400578 cmd->index = index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400579 cmd->cookie = nsock->cookie;
Mike Christie2da22da2019-08-13 11:39:52 -0500580 cmd->retries = 0;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400581 request.type = htonl(type | nbd_cmd_flags);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500582 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800583 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
584 request.len = htonl(size);
585 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400586 handle = nbd_cmd_handle(cmd);
587 memcpy(request.handle, &handle, sizeof(handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Matt Mullinsea106722019-04-26 11:49:48 -0700589 trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
590
Markus Pargmannd18509f2015-04-02 10:11:38 +0200591 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600592 req, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200593 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500594 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400595 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700596 trace_nbd_header_sent(req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400598 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400599 /* If we havne't sent anything we can just return BUSY,
600 * however if we have sent something we need to make
601 * sure we only allow this req to be sent until we are
602 * completely done.
603 */
604 if (sent) {
605 nsock->pending = req;
606 nsock->sent = sent;
607 }
Josef Bacikd7d94d42018-07-16 12:11:34 -0400608 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200609 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400610 }
Josef Bacika897b662016-12-05 16:20:29 -0500611 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200612 "Send control failed (result %d)\n", result);
Josef Bacikf3733242017-04-06 17:01:57 -0400613 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400615send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700616 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400617 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700618
Jens Axboe429a7872016-11-17 12:30:37 -0700619 bio = req->bio;
620 while (bio) {
621 struct bio *next = bio->bi_next;
622 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800623 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700624
625 bio_for_each_segment(bvec, bio, iter) {
626 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500627 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700628
Markus Pargmannd18509f2015-04-02 10:11:38 +0200629 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600630 req, bvec.bv_len);
David Howellsaa563d72018-10-20 00:57:56 +0100631 iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400632 if (skip) {
633 if (skip >= iov_iter_count(&from)) {
634 skip -= iov_iter_count(&from);
635 continue;
636 }
637 iov_iter_advance(&from, skip);
638 skip = 0;
639 }
640 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200641 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400642 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400643 /* We've already sent the header, we
644 * have no choice but to set pending and
645 * return BUSY.
646 */
647 nsock->pending = req;
648 nsock->sent = sent;
Josef Bacikd7d94d42018-07-16 12:11:34 -0400649 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200650 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400651 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700652 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200653 "Send data failed (result %d)\n",
654 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400655 return -EAGAIN;
Jens Axboe6c92e692007-08-16 13:43:12 +0200656 }
Jens Axboe429a7872016-11-17 12:30:37 -0700657 /*
658 * The completion might already have come in,
659 * so break for the last one instead of letting
660 * the iterator do it. This prevents use-after-free
661 * of the bio.
662 */
663 if (is_last)
664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Jens Axboe429a7872016-11-17 12:30:37 -0700666 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400668out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700669 trace_nbd_payload_sent(req, handle);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400670 nsock->pending = NULL;
671 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500676static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400678 struct nbd_config *config = nbd->config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int result;
680 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700681 struct nbd_cmd *cmd;
682 struct request *req = NULL;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400683 u64 handle;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700684 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500685 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500686 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
687 struct iov_iter to;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400688 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 reply.magic = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100691 iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400692 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (result <= 0) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400694 if (!nbd_disconnected(config))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500695 dev_err(disk_to_dev(nbd->disk),
696 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200697 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700699
700 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700701 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700702 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200703 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700704 }
705
Josef Bacik8f3ea352018-07-16 12:11:35 -0400706 memcpy(&handle, reply.handle, sizeof(handle));
707 tag = nbd_handle_to_tag(handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700708 hwq = blk_mq_unique_tag_to_hwq(tag);
709 if (hwq < nbd->tag_set.nr_hw_queues)
710 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
711 blk_mq_unique_tag_to_tag(tag));
712 if (!req || !blk_mq_request_started(req)) {
713 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
714 tag, req);
715 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Andrew Hall2abd2de2019-04-26 11:49:49 -0700717 trace_nbd_header_received(req, handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700718 cmd = blk_mq_rq_to_pdu(req);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400719
720 mutex_lock(&cmd->lock);
721 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
722 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
723 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
724 ret = -ENOENT;
725 goto out;
726 }
Josef Bacik7ce23e82019-10-21 15:56:28 -0400727 if (cmd->status != BLK_STS_OK) {
728 dev_err(disk_to_dev(nbd->disk), "Command already handled %p\n",
729 req);
730 ret = -ENOENT;
731 goto out;
732 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400733 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
734 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
735 req);
736 ret = -ENOENT;
737 goto out;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700740 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200741 ntohl(reply.error));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200742 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Kevin Vigoree57a052018-06-04 10:40:12 -0600746 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200747 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200748 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800749 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200750
751 rq_for_each_segment(bvec, req, iter) {
David Howellsaa563d72018-10-20 00:57:56 +0100752 iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400753 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200754 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700755 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200756 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400757 /*
Hou Pud9709582020-02-28 01:40:29 -0500758 * If we've disconnected, we need to make sure we
Josef Bacikf3733242017-04-06 17:01:57 -0400759 * complete this request, otherwise error out
760 * and let the timeout stuff handle resubmitting
761 * this request onto another connection.
762 */
Hou Pud9709582020-02-28 01:40:29 -0500763 if (nbd_disconnected(config)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200764 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400765 goto out;
Josef Bacikf3733242017-04-06 17:01:57 -0400766 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400767 ret = -EIO;
768 goto out;
Jens Axboe6c92e692007-08-16 13:43:12 +0200769 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200770 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600771 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400774out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700775 trace_nbd_payload_received(req, handle);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400776 mutex_unlock(&cmd->lock);
777 return ret ? ERR_PTR(ret) : cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Josef Bacik9561a7a2016-11-22 14:04:40 -0500780static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500782 struct recv_thread_args *args = container_of(work,
783 struct recv_thread_args,
784 work);
785 struct nbd_device *nbd = args->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400786 struct nbd_config *config = nbd->config;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700787 struct nbd_cmd *cmd;
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200788 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Markus Pargmann19391832015-08-17 08:20:03 +0200790 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500791 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700792 if (IS_ERR(cmd)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400793 struct nbd_sock *nsock = config->socks[args->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400794
795 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400796 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400797 mutex_unlock(&nsock->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200798 break;
799 }
800
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200801 rq = blk_mq_rq_from_pdu(cmd);
802 if (likely(!blk_should_fake_timeout(rq->q)))
803 blk_mq_complete_request(rq);
Markus Pargmann19391832015-08-17 08:20:03 +0200804 }
Xiubo Li87aac3a2020-10-13 22:45:14 -0400805 nbd_config_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400806 atomic_dec(&config->recv_threads);
807 wake_up(&config->recv_wq);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400808 kfree(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Jens Axboe7baa8572018-11-08 10:24:07 -0700811static bool nbd_clear_req(struct request *req, void *data, bool reserved)
Josef Bacikfd8383f2016-09-08 12:33:37 -0700812{
Christoph Hellwigd250bf4e2018-05-30 18:51:00 +0200813 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700814
Josef Bacikde6346e2019-10-21 15:56:27 -0400815 mutex_lock(&cmd->lock);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200816 cmd->status = BLK_STS_IOERR;
Josef Bacikde6346e2019-10-21 15:56:27 -0400817 mutex_unlock(&cmd->lock);
818
Christoph Hellwig08e00292017-04-20 16:03:09 +0200819 blk_mq_complete_request(req);
Jens Axboe7baa8572018-11-08 10:24:07 -0700820 return true;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700821}
822
Wanlong Gaof4507162012-03-28 14:42:51 -0700823static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300825 blk_mq_quiesce_queue(nbd->disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700826 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300827 blk_mq_unquiesce_queue(nbd->disk->queue);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200828 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Josef Bacikf3733242017-04-06 17:01:57 -0400831static int find_fallback(struct nbd_device *nbd, int index)
832{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400833 struct nbd_config *config = nbd->config;
Josef Bacikf3733242017-04-06 17:01:57 -0400834 int new_index = -1;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400835 struct nbd_sock *nsock = config->socks[index];
Josef Bacikf3733242017-04-06 17:01:57 -0400836 int fallback = nsock->fallback_index;
837
Xiubo Liec76a7b2019-09-17 17:26:05 +0530838 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacikf3733242017-04-06 17:01:57 -0400839 return new_index;
840
Josef Bacik5ea8d102017-04-06 17:01:58 -0400841 if (config->num_connections <= 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400842 dev_err_ratelimited(disk_to_dev(nbd->disk),
Hou Pud9709582020-02-28 01:40:29 -0500843 "Dead connection, failed to find a fallback\n");
Josef Bacikf3733242017-04-06 17:01:57 -0400844 return new_index;
845 }
846
Josef Bacik5ea8d102017-04-06 17:01:58 -0400847 if (fallback >= 0 && fallback < config->num_connections &&
848 !config->socks[fallback]->dead)
Josef Bacikf3733242017-04-06 17:01:57 -0400849 return fallback;
850
851 if (nsock->fallback_index < 0 ||
Josef Bacik5ea8d102017-04-06 17:01:58 -0400852 nsock->fallback_index >= config->num_connections ||
853 config->socks[nsock->fallback_index]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400854 int i;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400855 for (i = 0; i < config->num_connections; i++) {
Josef Bacikf3733242017-04-06 17:01:57 -0400856 if (i == index)
857 continue;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400858 if (!config->socks[i]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400859 new_index = i;
860 break;
861 }
862 }
863 nsock->fallback_index = new_index;
864 if (new_index < 0) {
865 dev_err_ratelimited(disk_to_dev(nbd->disk),
866 "Dead connection, failed to find a fallback\n");
867 return new_index;
868 }
869 }
870 new_index = nsock->fallback_index;
871 return new_index;
872}
Paul Clements7fdfd402007-10-16 23:27:37 -0700873
Josef Bacik560bc4b2017-04-06 17:02:04 -0400874static int wait_for_reconnect(struct nbd_device *nbd)
875{
876 struct nbd_config *config = nbd->config;
877 if (!config->dead_conn_timeout)
878 return 0;
Xiubo Liec76a7b2019-09-17 17:26:05 +0530879 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacik560bc4b2017-04-06 17:02:04 -0400880 return 0;
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600881 return wait_event_timeout(config->conn_wait,
882 atomic_read(&config->live_connections) > 0,
883 config->dead_conn_timeout) > 0;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400884}
885
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400886static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700887{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700888 struct request *req = blk_mq_rq_from_pdu(cmd);
889 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400890 struct nbd_config *config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500891 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400892 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700893
Josef Bacik5ea8d102017-04-06 17:01:58 -0400894 if (!refcount_inc_not_zero(&nbd->config_refs)) {
895 dev_err_ratelimited(disk_to_dev(nbd->disk),
896 "Socks array is empty\n");
Josef Bacik6a468d52017-11-06 16:11:58 -0500897 blk_mq_start_request(req);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400898 return -EINVAL;
899 }
900 config = nbd->config;
901
902 if (index >= config->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500903 dev_err_ratelimited(disk_to_dev(nbd->disk),
904 "Attempted send on invalid socket\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -0400905 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500906 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400907 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500908 }
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200909 cmd->status = BLK_STS_OK;
Josef Bacikf3733242017-04-06 17:01:57 -0400910again:
Josef Bacik5ea8d102017-04-06 17:01:58 -0400911 nsock = config->socks[index];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500912 mutex_lock(&nsock->tx_lock);
Josef Bacikf3733242017-04-06 17:01:57 -0400913 if (nsock->dead) {
Josef Bacik560bc4b2017-04-06 17:02:04 -0400914 int old_index = index;
Josef Bacikf3733242017-04-06 17:01:57 -0400915 index = find_fallback(nbd, index);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500916 mutex_unlock(&nsock->tx_lock);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400917 if (index < 0) {
918 if (wait_for_reconnect(nbd)) {
919 index = old_index;
920 goto again;
921 }
922 /* All the sockets should already be down at this point,
923 * we just want to make sure that DISCONNECTED is set so
924 * any requests that come in that were queue'ed waiting
925 * for the reconnect timer don't trigger the timer again
926 * and instead just error out.
927 */
928 sock_shutdown(nbd);
929 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500930 blk_mq_start_request(req);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400931 return -EIO;
932 }
Josef Bacikf3733242017-04-06 17:01:57 -0400933 goto again;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700934 }
935
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400936 /* Handle the case that we have a pending request that was partially
937 * transmitted that _has_ to be serviced first. We need to call requeue
938 * here so that it gets put _after_ the request that is already on the
939 * dispatch list.
940 */
Josef Bacik6a468d52017-11-06 16:11:58 -0500941 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400942 if (unlikely(nsock->pending && nsock->pending != req)) {
Josef Bacikd7d94d42018-07-16 12:11:34 -0400943 nbd_requeue_cmd(cmd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400944 ret = 0;
945 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700946 }
Josef Bacikf3733242017-04-06 17:01:57 -0400947 /*
948 * Some failures are related to the link going down, so anything that
949 * returns EAGAIN can be retried on a different socket.
950 */
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400951 ret = nbd_send_cmd(nbd, cmd, index);
Josef Bacikf3733242017-04-06 17:01:57 -0400952 if (ret == -EAGAIN) {
953 dev_err_ratelimited(disk_to_dev(nbd->disk),
Josef Bacik6a468d52017-11-06 16:11:58 -0500954 "Request send failed, requeueing\n");
Josef Bacik799f9a32017-04-06 17:02:02 -0400955 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400956 nbd_requeue_cmd(cmd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500957 ret = 0;
Josef Bacikf3733242017-04-06 17:01:57 -0400958 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400959out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500960 mutex_unlock(&nsock->tx_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400961 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400962 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700963}
964
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200965static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700966 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700967{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700968 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400969 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700970
Josef Bacik9561a7a2016-11-22 14:04:40 -0500971 /*
972 * Since we look at the bio's to send the request over the network we
973 * need to make sure the completion work doesn't mark this request done
974 * before we are done doing our send. This keeps us from dereferencing
975 * freed data if we have particularly fast completions (ie we get the
976 * completion before we exit sock_xmit on the last bvec) or in the case
977 * that the server is misbehaving (or there was an error) before we're
978 * done sending everything over the wire.
979 */
Josef Bacik8f3ea352018-07-16 12:11:35 -0400980 mutex_lock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400981 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400982
983 /* We can be called directly from the user space process, which means we
984 * could possibly have signals pending so our sendmsg will fail. In
985 * this case we need to return that we are busy, otherwise error out as
986 * appropriate.
987 */
988 ret = nbd_handle_cmd(cmd, hctx->queue_num);
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400989 if (ret < 0)
990 ret = BLK_STS_IOERR;
991 else if (!ret)
992 ret = BLK_STS_OK;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400993 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500994
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400995 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Mike Christiecf1b2322019-10-17 16:27:34 -0500998static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
999 int *err)
1000{
1001 struct socket *sock;
1002
1003 *err = 0;
1004 sock = sockfd_lookup(fd, err);
1005 if (!sock)
1006 return NULL;
1007
1008 if (sock->ops->shutdown == sock_no_shutdown) {
1009 dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
1010 *err = -EINVAL;
Sun Kedff10bb2019-11-19 14:09:11 +08001011 sockfd_put(sock);
Mike Christiecf1b2322019-10-17 16:27:34 -05001012 return NULL;
1013 }
1014
1015 return sock;
1016}
1017
Josef Bacike46c7282017-04-06 17:02:00 -04001018static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
1019 bool netlink)
Markus Pargmann23272a672015-10-29 11:51:16 +01001020{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001021 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001022 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001023 struct nbd_sock **socks;
1024 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -05001025 int err;
1026
Mike Christiecf1b2322019-10-17 16:27:34 -05001027 sock = nbd_get_socket(nbd, arg, &err);
Josef Bacik9442b732017-02-07 17:10:22 -05001028 if (!sock)
1029 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +01001030
Josef Bacike46c7282017-04-06 17:02:00 -04001031 if (!netlink && !nbd->task_setup &&
Xiubo Liec76a7b2019-09-17 17:26:05 +05301032 !test_bit(NBD_RT_BOUND, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -05001033 nbd->task_setup = current;
Josef Bacike46c7282017-04-06 17:02:00 -04001034
1035 if (!netlink &&
1036 (nbd->task_setup != current ||
Xiubo Liec76a7b2019-09-17 17:26:05 +05301037 test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
Josef Bacik9561a7a2016-11-22 14:04:40 -05001038 dev_err(disk_to_dev(nbd->disk),
1039 "Device being setup by another task");
Zheng Bin579dd912020-06-29 09:23:49 +08001040 err = -EBUSY;
1041 goto put_socket;
1042 }
1043
1044 nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
1045 if (!nsock) {
1046 err = -ENOMEM;
1047 goto put_socket;
Markus Pargmann23272a672015-10-29 11:51:16 +01001048 }
1049
Josef Bacik5ea8d102017-04-06 17:01:58 -04001050 socks = krealloc(config->socks, (config->num_connections + 1) *
Josef Bacik9561a7a2016-11-22 14:04:40 -05001051 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -04001052 if (!socks) {
Zheng Bin579dd912020-06-29 09:23:49 +08001053 kfree(nsock);
1054 err = -ENOMEM;
1055 goto put_socket;
Josef Bacik9b1355d2017-04-06 17:01:56 -04001056 }
Navid Emamdoost03bf73c2019-09-23 15:09:58 -05001057
1058 config->socks = socks;
1059
Josef Bacikf3733242017-04-06 17:01:57 -04001060 nsock->fallback_index = -1;
1061 nsock->dead = false;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001062 mutex_init(&nsock->tx_lock);
1063 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001064 nsock->pending = NULL;
1065 nsock->sent = 0;
Josef Bacik799f9a32017-04-06 17:02:02 -04001066 nsock->cookie = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001067 socks[config->num_connections++] = nsock;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001068 atomic_inc(&config->live_connections);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001069
1070 return 0;
Zheng Bin579dd912020-06-29 09:23:49 +08001071
1072put_socket:
1073 sockfd_put(sock);
1074 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +01001075}
1076
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001077static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
1078{
1079 struct nbd_config *config = nbd->config;
1080 struct socket *sock, *old;
1081 struct recv_thread_args *args;
1082 int i;
1083 int err;
1084
Mike Christiecf1b2322019-10-17 16:27:34 -05001085 sock = nbd_get_socket(nbd, arg, &err);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001086 if (!sock)
1087 return err;
1088
1089 args = kzalloc(sizeof(*args), GFP_KERNEL);
1090 if (!args) {
1091 sockfd_put(sock);
1092 return -ENOMEM;
1093 }
1094
1095 for (i = 0; i < config->num_connections; i++) {
1096 struct nbd_sock *nsock = config->socks[i];
1097
1098 if (!nsock->dead)
1099 continue;
1100
1101 mutex_lock(&nsock->tx_lock);
1102 if (!nsock->dead) {
1103 mutex_unlock(&nsock->tx_lock);
1104 continue;
1105 }
1106 sk_set_memalloc(sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001107 if (nbd->tag_set.timeout)
1108 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001109 atomic_inc(&config->recv_threads);
1110 refcount_inc(&nbd->config_refs);
1111 old = nsock->sock;
1112 nsock->fallback_index = -1;
1113 nsock->sock = sock;
1114 nsock->dead = false;
1115 INIT_WORK(&args->work, recv_work);
1116 args->index = i;
1117 args->nbd = nbd;
Josef Bacik799f9a32017-04-06 17:02:02 -04001118 nsock->cookie++;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001119 mutex_unlock(&nsock->tx_lock);
1120 sockfd_put(old);
1121
Xiubo Liec76a7b2019-09-17 17:26:05 +05301122 clear_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
Josef Bacik7a362ea2017-07-25 13:31:19 -04001123
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001124 /* We take the tx_mutex in an error path in the recv_work, so we
1125 * need to queue_work outside of the tx_mutex.
1126 */
Mike Christiee9e006f2019-08-04 14:10:06 -05001127 queue_work(nbd->recv_workq, &args->work);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001128
1129 atomic_inc(&config->live_connections);
1130 wake_up(&config->conn_wait);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001131 return 0;
1132 }
1133 sockfd_put(sock);
1134 kfree(args);
1135 return -ENOSPC;
1136}
1137
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001138static void nbd_bdev_reset(struct block_device *bdev)
1139{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -04001140 if (bdev->bd_openers > 1)
1141 return;
Christoph Hellwig611bee52020-08-23 11:10:41 +02001142 bd_set_nr_sectors(bdev, 0);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001143}
1144
Josef Bacik29eaadc2017-04-06 17:01:59 -04001145static void nbd_parse_flags(struct nbd_device *nbd)
Markus Pargmannd02cf532015-10-29 12:06:15 +01001146{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001147 struct nbd_config *config = nbd->config;
1148 if (config->flags & NBD_FLAG_READ_ONLY)
Josef Bacik29eaadc2017-04-06 17:01:59 -04001149 set_disk_ro(nbd->disk, true);
1150 else
1151 set_disk_ro(nbd->disk, false);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001152 if (config->flags & NBD_FLAG_SEND_TRIM)
Bart Van Assche8b904b52018-03-07 17:10:10 -08001153 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Shaun McDowell685c9b22017-05-25 23:55:54 -04001154 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1155 if (config->flags & NBD_FLAG_SEND_FUA)
1156 blk_queue_write_cache(nbd->disk->queue, true, true);
1157 else
1158 blk_queue_write_cache(nbd->disk->queue, true, false);
1159 }
Markus Pargmannd02cf532015-10-29 12:06:15 +01001160 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -06001161 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +01001162}
1163
Josef Bacik9561a7a2016-11-22 14:04:40 -05001164static void send_disconnects(struct nbd_device *nbd)
1165{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001166 struct nbd_config *config = nbd->config;
Al Viroc9f2b6a2015-11-12 05:09:35 -05001167 struct nbd_request request = {
1168 .magic = htonl(NBD_REQUEST_MAGIC),
1169 .type = htonl(NBD_CMD_DISC),
1170 };
1171 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1172 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001173 int i, ret;
1174
Josef Bacik5ea8d102017-04-06 17:01:58 -04001175 for (i = 0; i < config->num_connections; i++) {
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001176 struct nbd_sock *nsock = config->socks[i];
1177
David Howellsaa563d72018-10-20 00:57:56 +01001178 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001179 mutex_lock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001180 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001181 if (ret <= 0)
1182 dev_err(disk_to_dev(nbd->disk),
1183 "Send disconnect failed %d\n", ret);
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001184 mutex_unlock(&nsock->tx_lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001185 }
1186}
1187
Josef Bacik29eaadc2017-04-06 17:01:59 -04001188static int nbd_disconnect(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001189{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001190 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001191
Josef Bacik5ea8d102017-04-06 17:01:58 -04001192 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Xiubo Liec76a7b2019-09-17 17:26:05 +05301193 set_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
Xiubo Li8454d682019-09-17 17:26:06 +05301194 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags);
Josef Bacik2e134562017-07-21 10:48:13 -04001195 send_disconnects(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001196 return 0;
1197}
1198
Josef Bacik29eaadc2017-04-06 17:01:59 -04001199static void nbd_clear_sock(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001200{
1201 sock_shutdown(nbd);
1202 nbd_clear_que(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001203 nbd->task_setup = NULL;
Josef Bacik9442b732017-02-07 17:10:22 -05001204}
1205
Josef Bacik5ea8d102017-04-06 17:01:58 -04001206static void nbd_config_put(struct nbd_device *nbd)
1207{
1208 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1209 &nbd->config_lock)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001210 struct nbd_config *config = nbd->config;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001211 nbd_dev_dbg_close(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001212 nbd_size_clear(nbd);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301213 if (test_and_clear_bit(NBD_RT_HAS_PID_FILE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001214 &config->runtime_flags))
1215 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1216 nbd->task_recv = NULL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001217 nbd_clear_sock(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001218 if (config->num_connections) {
1219 int i;
1220 for (i = 0; i < config->num_connections; i++) {
1221 sockfd_put(config->socks[i]->sock);
1222 kfree(config->socks[i]);
1223 }
1224 kfree(config->socks);
1225 }
Ilya Dryomovfa976532017-05-23 17:49:55 +02001226 kfree(nbd->config);
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001227 nbd->config = NULL;
1228
Mike Christiee9e006f2019-08-04 14:10:06 -05001229 if (nbd->recv_workq)
1230 destroy_workqueue(nbd->recv_workq);
1231 nbd->recv_workq = NULL;
1232
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001233 nbd->tag_set.timeout = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001234 nbd->disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001235 nbd->disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001236 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001237 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Josef Bacika2c97902017-04-06 17:02:07 -04001238
Josef Bacik5ea8d102017-04-06 17:01:58 -04001239 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001240 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001241 module_put(THIS_MODULE);
1242 }
1243}
1244
Josef Bacike46c7282017-04-06 17:02:00 -04001245static int nbd_start_device(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001246{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001247 struct nbd_config *config = nbd->config;
1248 int num_connections = config->num_connections;
Josef Bacik9442b732017-02-07 17:10:22 -05001249 int error = 0, i;
1250
1251 if (nbd->task_recv)
1252 return -EBUSY;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001253 if (!config->socks)
Josef Bacik9442b732017-02-07 17:10:22 -05001254 return -EINVAL;
1255 if (num_connections > 1 &&
Josef Bacik5ea8d102017-04-06 17:01:58 -04001256 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
Josef Bacik9442b732017-02-07 17:10:22 -05001257 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001258 return -EINVAL;
Josef Bacik9442b732017-02-07 17:10:22 -05001259 }
1260
Mike Christiee9e006f2019-08-04 14:10:06 -05001261 nbd->recv_workq = alloc_workqueue("knbd%d-recv",
1262 WQ_MEM_RECLAIM | WQ_HIGHPRI |
1263 WQ_UNBOUND, 0, nbd->index);
1264 if (!nbd->recv_workq) {
1265 dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1266 return -ENOMEM;
1267 }
1268
Josef Bacik5ea8d102017-04-06 17:01:58 -04001269 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
Josef Bacik9442b732017-02-07 17:10:22 -05001270 nbd->task_recv = current;
Josef Bacik9442b732017-02-07 17:10:22 -05001271
Josef Bacik29eaadc2017-04-06 17:01:59 -04001272 nbd_parse_flags(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001273
1274 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1275 if (error) {
1276 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001277 return error;
Josef Bacik9442b732017-02-07 17:10:22 -05001278 }
Xiubo Liec76a7b2019-09-17 17:26:05 +05301279 set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags);
Josef Bacik9442b732017-02-07 17:10:22 -05001280
1281 nbd_dev_dbg_init(nbd);
1282 for (i = 0; i < num_connections; i++) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001283 struct recv_thread_args *args;
1284
1285 args = kzalloc(sizeof(*args), GFP_KERNEL);
1286 if (!args) {
1287 sock_shutdown(nbd);
Sun Ke5c0dd222020-01-22 11:18:57 +08001288 /*
1289 * If num_connections is m (2 < m),
1290 * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
1291 * But NO.(n + 1) failed. We still have n recv threads.
1292 * So, add flush_workqueue here to prevent recv threads
1293 * dropping the last config_refs and trying to destroy
1294 * the workqueue from inside the workqueue.
1295 */
1296 if (i)
1297 flush_workqueue(nbd->recv_workq);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001298 return -ENOMEM;
1299 }
1300 sk_set_memalloc(config->socks[i]->sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001301 if (nbd->tag_set.timeout)
1302 config->socks[i]->sock->sk->sk_sndtimeo =
1303 nbd->tag_set.timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001304 atomic_inc(&config->recv_threads);
1305 refcount_inc(&nbd->config_refs);
1306 INIT_WORK(&args->work, recv_work);
1307 args->nbd = nbd;
1308 args->index = i;
Mike Christiee9e006f2019-08-04 14:10:06 -05001309 queue_work(nbd->recv_workq, &args->work);
Josef Bacik9442b732017-02-07 17:10:22 -05001310 }
Josef Bacik639812a2017-10-09 13:12:10 -04001311 nbd_size_update(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001312 return error;
1313}
1314
1315static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1316{
1317 struct nbd_config *config = nbd->config;
1318 int ret;
1319
1320 ret = nbd_start_device(nbd);
1321 if (ret)
1322 return ret;
1323
Josef Bacike46c7282017-04-06 17:02:00 -04001324 if (max_part)
Christoph Hellwig38430f02020-09-21 09:19:45 +02001325 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
Josef Bacike46c7282017-04-06 17:02:00 -04001326 mutex_unlock(&nbd->config_lock);
1327 ret = wait_event_interruptible(config->recv_wq,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001328 atomic_read(&config->recv_threads) == 0);
Mike Christie1c058392019-12-08 16:51:50 -06001329 if (ret)
Josef Bacik5ea8d102017-04-06 17:01:58 -04001330 sock_shutdown(nbd);
Mike Christie1c058392019-12-08 16:51:50 -06001331 flush_workqueue(nbd->recv_workq);
1332
Josef Bacik9442b732017-02-07 17:10:22 -05001333 mutex_lock(&nbd->config_lock);
Josef Bacik76aa1d32018-05-16 14:51:22 -04001334 nbd_bdev_reset(bdev);
Josef Bacik9442b732017-02-07 17:10:22 -05001335 /* user requested, ignore socket errors */
Xiubo Liec76a7b2019-09-17 17:26:05 +05301336 if (test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001337 ret = 0;
Xiubo Liec76a7b2019-09-17 17:26:05 +05301338 if (test_bit(NBD_RT_TIMEDOUT, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001339 ret = -ETIMEDOUT;
1340 return ret;
Josef Bacik9442b732017-02-07 17:10:22 -05001341}
Markus Pargmann30d53d92015-08-17 08:20:06 +02001342
Josef Bacik29eaadc2017-04-06 17:01:59 -04001343static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1344 struct block_device *bdev)
1345{
Josef Bacik2516ab12017-04-06 17:02:03 -04001346 sock_shutdown(nbd);
Munehisa Kamata2b5c8f02019-07-31 20:13:10 +08001347 __invalidate_device(bdev, true);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001348 nbd_bdev_reset(bdev);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301349 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
Josef Bacike46c7282017-04-06 17:02:00 -04001350 &nbd->config->runtime_flags))
1351 nbd_config_put(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001352}
1353
Xiubo Li553768d2019-05-29 15:16:05 -05001354static bool nbd_is_valid_blksize(unsigned long blksize)
1355{
1356 if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
1357 blksize > PAGE_SIZE)
1358 return false;
1359 return true;
1360}
1361
Mike Christie55313e92019-08-13 11:39:49 -05001362static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
1363{
1364 nbd->tag_set.timeout = timeout * HZ;
Mike Christie2da22da2019-08-13 11:39:52 -05001365 if (timeout)
1366 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
Hou Puacb19e12020-08-10 08:00:44 -04001367 else
1368 blk_queue_rq_timeout(nbd->disk->queue, 30 * HZ);
Mike Christie55313e92019-08-13 11:39:49 -05001369}
1370
Josef Bacik9561a7a2016-11-22 14:04:40 -05001371/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -07001372static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -07001373 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001375 struct nbd_config *config = nbd->config;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -05001378 case NBD_DISCONNECT:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001379 return nbd_disconnect(nbd);
Markus Pargmann23272a672015-10-29 11:51:16 +01001380 case NBD_CLEAR_SOCK:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001381 nbd_clear_sock_ioctl(nbd, bdev);
1382 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001383 case NBD_SET_SOCK:
Josef Bacike46c7282017-04-06 17:02:00 -04001384 return nbd_add_socket(nbd, arg, false);
Josef Bacik9442b732017-02-07 17:10:22 -05001385 case NBD_SET_BLKSIZE:
Xiubo Li553768d2019-05-29 15:16:05 -05001386 if (!arg)
1387 arg = NBD_DEF_BLKSIZE;
1388 if (!nbd_is_valid_blksize(arg))
Jens Axboebc811f02018-09-04 11:52:34 -06001389 return -EINVAL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001390 nbd_size_set(nbd, arg,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001391 div_s64(config->bytesize, arg));
Josef Bacike5445412017-02-13 10:39:47 -05001392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 case NBD_SET_SIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001394 nbd_size_set(nbd, config->blksize,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001395 div_s64(arg, config->blksize));
Josef Bacike5445412017-02-13 10:39:47 -05001396 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +02001397 case NBD_SET_SIZE_BLOCKS:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001398 nbd_size_set(nbd, config->blksize, arg);
Josef Bacike5445412017-02-13 10:39:47 -05001399 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -07001400 case NBD_SET_TIMEOUT:
Mike Christie2da22da2019-08-13 11:39:52 -05001401 nbd_set_cmd_timeout(nbd, arg);
Paul Clements7fdfd402007-10-16 23:27:37 -07001402 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001403
Paul Clements2f012502012-10-04 17:16:15 -07001404 case NBD_SET_FLAGS:
Josef Bacik5ea8d102017-04-06 17:01:58 -04001405 config->flags = arg;
Paul Clements2f012502012-10-04 17:16:15 -07001406 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001407 case NBD_DO_IT:
Josef Bacike46c7282017-04-06 17:02:00 -04001408 return nbd_start_device_ioctl(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -08001410 /*
1411 * This is for compatibility only. The queue is always cleared
1412 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return 0;
1415 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -07001416 /*
1417 * For compatibility only, we no longer keep a list of
1418 * outstanding requests.
1419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421 }
Pavel Machek1a2ad212009-04-02 16:58:41 -07001422 return -ENOTTY;
1423}
1424
1425static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1426 unsigned int cmd, unsigned long arg)
1427{
Wanlong Gaof4507162012-03-28 14:42:51 -07001428 struct nbd_device *nbd = bdev->bd_disk->private_data;
Josef Bacike46c7282017-04-06 17:02:00 -04001429 struct nbd_config *config = nbd->config;
1430 int error = -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001431
1432 if (!capable(CAP_SYS_ADMIN))
1433 return -EPERM;
1434
Josef Bacik1dae69b2017-05-05 22:25:18 -04001435 /* The block layer will pass back some non-nbd ioctls in case we have
1436 * special handling for them, but we don't so just return an error.
1437 */
1438 if (_IOC_TYPE(cmd) != 0xab)
1439 return -EINVAL;
1440
Josef Bacik9561a7a2016-11-22 14:04:40 -05001441 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001442
1443 /* Don't allow ioctl operations on a nbd device that was created with
1444 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1445 */
Xiubo Liec76a7b2019-09-17 17:26:05 +05301446 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
Josef Bacike46c7282017-04-06 17:02:00 -04001447 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1448 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1449 else
1450 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -05001451 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -07001452 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Josef Bacik5ea8d102017-04-06 17:01:58 -04001455static struct nbd_config *nbd_alloc_config(void)
1456{
1457 struct nbd_config *config;
1458
1459 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1460 if (!config)
1461 return NULL;
1462 atomic_set(&config->recv_threads, 0);
1463 init_waitqueue_head(&config->recv_wq);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001464 init_waitqueue_head(&config->conn_wait);
Xiubo Li553768d2019-05-29 15:16:05 -05001465 config->blksize = NBD_DEF_BLKSIZE;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001466 atomic_set(&config->live_connections, 0);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001467 try_module_get(THIS_MODULE);
1468 return config;
1469}
1470
1471static int nbd_open(struct block_device *bdev, fmode_t mode)
1472{
1473 struct nbd_device *nbd;
1474 int ret = 0;
1475
1476 mutex_lock(&nbd_index_mutex);
1477 nbd = bdev->bd_disk->private_data;
1478 if (!nbd) {
1479 ret = -ENXIO;
1480 goto out;
1481 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001482 if (!refcount_inc_not_zero(&nbd->refs)) {
1483 ret = -ENXIO;
1484 goto out;
1485 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001486 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1487 struct nbd_config *config;
1488
1489 mutex_lock(&nbd->config_lock);
1490 if (refcount_inc_not_zero(&nbd->config_refs)) {
1491 mutex_unlock(&nbd->config_lock);
1492 goto out;
1493 }
1494 config = nbd->config = nbd_alloc_config();
1495 if (!config) {
1496 ret = -ENOMEM;
1497 mutex_unlock(&nbd->config_lock);
1498 goto out;
1499 }
1500 refcount_set(&nbd->config_refs, 1);
Josef Bacikc6a47592017-04-06 17:02:06 -04001501 refcount_inc(&nbd->refs);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001502 mutex_unlock(&nbd->config_lock);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001503 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Josef Bacikfe1f9e62018-05-16 14:51:21 -04001504 } else if (nbd_disconnected(nbd->config)) {
Christoph Hellwig38430f02020-09-21 09:19:45 +02001505 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001506 }
1507out:
1508 mutex_unlock(&nbd_index_mutex);
1509 return ret;
1510}
1511
1512static void nbd_release(struct gendisk *disk, fmode_t mode)
1513{
1514 struct nbd_device *nbd = disk->private_data;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001515 struct block_device *bdev = bdget_disk(disk, 0);
1516
Xiubo Liec76a7b2019-09-17 17:26:05 +05301517 if (test_bit(NBD_RT_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001518 bdev->bd_openers == 0)
1519 nbd_disconnect_and_put(nbd);
1520
Josef Bacik5ea8d102017-04-06 17:01:58 -04001521 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001522 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001523}
1524
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001525static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 .owner = THIS_MODULE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001528 .open = nbd_open,
1529 .release = nbd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001530 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -05001531 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532};
1533
Markus Pargmann30d53d92015-08-17 08:20:06 +02001534#if IS_ENABLED(CONFIG_DEBUG_FS)
1535
1536static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1537{
1538 struct nbd_device *nbd = s->private;
1539
1540 if (nbd->task_recv)
1541 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +02001542
1543 return 0;
1544}
1545
1546static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1547{
1548 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1549}
1550
1551static const struct file_operations nbd_dbg_tasks_ops = {
1552 .open = nbd_dbg_tasks_open,
1553 .read = seq_read,
1554 .llseek = seq_lseek,
1555 .release = single_release,
1556};
1557
1558static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1559{
1560 struct nbd_device *nbd = s->private;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001561 u32 flags = nbd->config->flags;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001562
1563 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1564
1565 seq_puts(s, "Known flags:\n");
1566
1567 if (flags & NBD_FLAG_HAS_FLAGS)
1568 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1569 if (flags & NBD_FLAG_READ_ONLY)
1570 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1571 if (flags & NBD_FLAG_SEND_FLUSH)
1572 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
Shaun McDowell685c9b22017-05-25 23:55:54 -04001573 if (flags & NBD_FLAG_SEND_FUA)
1574 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
Markus Pargmann30d53d92015-08-17 08:20:06 +02001575 if (flags & NBD_FLAG_SEND_TRIM)
1576 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1577
1578 return 0;
1579}
1580
1581static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1582{
1583 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1584}
1585
1586static const struct file_operations nbd_dbg_flags_ops = {
1587 .open = nbd_dbg_flags_open,
1588 .read = seq_read,
1589 .llseek = seq_lseek,
1590 .release = single_release,
1591};
1592
1593static int nbd_dev_dbg_init(struct nbd_device *nbd)
1594{
1595 struct dentry *dir;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001596 struct nbd_config *config = nbd->config;
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001597
1598 if (!nbd_dbg_dir)
1599 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001600
1601 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001602 if (!dir) {
1603 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1604 nbd_name(nbd));
1605 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001606 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001607 config->dbg_dir = dir;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001608
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001609 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001610 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -07001611 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001612 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -04001613 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001614
1615 return 0;
1616}
1617
1618static void nbd_dev_dbg_close(struct nbd_device *nbd)
1619{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001620 debugfs_remove_recursive(nbd->config->dbg_dir);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001621}
1622
1623static int nbd_dbg_init(void)
1624{
1625 struct dentry *dbg_dir;
1626
1627 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001628 if (!dbg_dir)
1629 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001630
1631 nbd_dbg_dir = dbg_dir;
1632
1633 return 0;
1634}
1635
1636static void nbd_dbg_close(void)
1637{
1638 debugfs_remove_recursive(nbd_dbg_dir);
1639}
1640
1641#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1642
1643static int nbd_dev_dbg_init(struct nbd_device *nbd)
1644{
1645 return 0;
1646}
1647
1648static void nbd_dev_dbg_close(struct nbd_device *nbd)
1649{
1650}
1651
1652static int nbd_dbg_init(void)
1653{
1654 return 0;
1655}
1656
1657static void nbd_dbg_close(void)
1658{
1659}
1660
1661#endif
1662
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001663static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1664 unsigned int hctx_idx, unsigned int numa_node)
Josef Bacikfd8383f2016-09-08 12:33:37 -07001665{
1666 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001667 cmd->nbd = set->driver_data;
Josef Bacikd7d94d42018-07-16 12:11:34 -04001668 cmd->flags = 0;
Josef Bacik8f3ea352018-07-16 12:11:35 -04001669 mutex_init(&cmd->lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001670 return 0;
1671}
1672
Eric Biggersf363b082017-03-30 13:39:16 -07001673static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001674 .queue_rq = nbd_queue_rq,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +02001675 .complete = nbd_complete_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001676 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001677 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001678};
1679
Josef Bacikb0d91112017-02-01 16:11:40 -05001680static int nbd_dev_add(int index)
1681{
1682 struct nbd_device *nbd;
1683 struct gendisk *disk;
1684 struct request_queue *q;
1685 int err = -ENOMEM;
1686
1687 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1688 if (!nbd)
1689 goto out;
1690
1691 disk = alloc_disk(1 << part_shift);
1692 if (!disk)
1693 goto out_free_nbd;
1694
1695 if (index >= 0) {
1696 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1697 GFP_KERNEL);
1698 if (err == -ENOSPC)
1699 err = -EEXIST;
1700 } else {
1701 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1702 if (err >= 0)
1703 index = err;
1704 }
1705 if (err < 0)
1706 goto out_free_disk;
1707
Josef Bacike46c7282017-04-06 17:02:00 -04001708 nbd->index = index;
Josef Bacikb0d91112017-02-01 16:11:40 -05001709 nbd->disk = disk;
1710 nbd->tag_set.ops = &nbd_mq_ops;
1711 nbd->tag_set.nr_hw_queues = 1;
1712 nbd->tag_set.queue_depth = 128;
1713 nbd->tag_set.numa_node = NUMA_NO_NODE;
1714 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1715 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Ming Lei56d18f62019-02-15 19:13:24 +08001716 BLK_MQ_F_BLOCKING;
Josef Bacikb0d91112017-02-01 16:11:40 -05001717 nbd->tag_set.driver_data = nbd;
Xiubo Li8454d682019-09-17 17:26:06 +05301718 nbd->destroy_complete = NULL;
Josef Bacikb0d91112017-02-01 16:11:40 -05001719
1720 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1721 if (err)
1722 goto out_free_idr;
1723
1724 q = blk_mq_init_queue(&nbd->tag_set);
1725 if (IS_ERR(q)) {
1726 err = PTR_ERR(q);
1727 goto out_free_tags;
1728 }
1729 disk->queue = q;
1730
1731 /*
1732 * Tell the block layer that we are not a rotational device
1733 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08001734 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1735 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Josef Bacik6df133a2018-05-23 13:35:59 -04001736 disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001737 disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001738 blk_queue_max_discard_sectors(disk->queue, 0);
Josef Bacikebb16d02017-04-18 16:22:51 -04001739 blk_queue_max_segment_size(disk->queue, UINT_MAX);
Josef Bacik1cc1f172017-04-20 15:47:01 -04001740 blk_queue_max_segments(disk->queue, USHRT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001741 blk_queue_max_hw_sectors(disk->queue, 65536);
1742 disk->queue->limits.max_sectors = 256;
1743
Josef Bacikb0d91112017-02-01 16:11:40 -05001744 mutex_init(&nbd->config_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001745 refcount_set(&nbd->config_refs, 0);
Josef Bacikc6a47592017-04-06 17:02:06 -04001746 refcount_set(&nbd->refs, 1);
1747 INIT_LIST_HEAD(&nbd->list);
Josef Bacikb0d91112017-02-01 16:11:40 -05001748 disk->major = NBD_MAJOR;
1749 disk->first_minor = index << part_shift;
1750 disk->fops = &nbd_fops;
1751 disk->private_data = nbd;
1752 sprintf(disk->disk_name, "nbd%d", index);
Josef Bacikb0d91112017-02-01 16:11:40 -05001753 add_disk(disk);
Josef Bacik47d902b2017-04-06 17:02:05 -04001754 nbd_total_devices++;
Josef Bacikb0d91112017-02-01 16:11:40 -05001755 return index;
1756
1757out_free_tags:
1758 blk_mq_free_tag_set(&nbd->tag_set);
1759out_free_idr:
1760 idr_remove(&nbd_index_idr, index);
1761out_free_disk:
1762 put_disk(disk);
1763out_free_nbd:
1764 kfree(nbd);
1765out:
1766 return err;
1767}
1768
Josef Bacike46c7282017-04-06 17:02:00 -04001769static int find_free_cb(int id, void *ptr, void *data)
1770{
1771 struct nbd_device *nbd = ptr;
1772 struct nbd_device **found = data;
1773
1774 if (!refcount_read(&nbd->config_refs)) {
1775 *found = nbd;
1776 return 1;
1777 }
1778 return 0;
1779}
1780
1781/* Netlink interface. */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001782static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001783 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1784 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1785 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1786 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1787 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1788 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1789 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
Josef Bacik560bc4b2017-04-06 17:02:04 -04001790 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001791 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
Josef Bacike46c7282017-04-06 17:02:00 -04001792};
1793
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001794static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001795 [NBD_SOCK_FD] = { .type = NLA_U32 },
1796};
1797
Josef Bacik47d902b2017-04-06 17:02:05 -04001798/* We don't use this right now since we don't parse the incoming list, but we
1799 * still want it here so userspace knows what to expect.
1800 */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001801static const struct nla_policy __attribute__((unused))
Josef Bacik47d902b2017-04-06 17:02:05 -04001802nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1803 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1804 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1805};
1806
Mike Christie4ddeaae82019-05-29 15:16:06 -05001807static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
1808{
1809 struct nbd_config *config = nbd->config;
1810 u64 bsize = config->blksize;
1811 u64 bytes = config->bytesize;
1812
1813 if (info->attrs[NBD_ATTR_SIZE_BYTES])
1814 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1815
1816 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1817 bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1818 if (!bsize)
1819 bsize = NBD_DEF_BLKSIZE;
1820 if (!nbd_is_valid_blksize(bsize)) {
1821 printk(KERN_ERR "Invalid block size %llu\n", bsize);
1822 return -EINVAL;
1823 }
1824 }
1825
1826 if (bytes != config->bytesize || bsize != config->blksize)
1827 nbd_size_set(nbd, bsize, div64_u64(bytes, bsize));
1828 return 0;
1829}
1830
Josef Bacike46c7282017-04-06 17:02:00 -04001831static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1832{
Xiubo Li8454d682019-09-17 17:26:06 +05301833 DECLARE_COMPLETION_ONSTACK(destroy_complete);
Josef Bacike46c7282017-04-06 17:02:00 -04001834 struct nbd_device *nbd = NULL;
1835 struct nbd_config *config;
1836 int index = -1;
1837 int ret;
Josef Bacika2c97902017-04-06 17:02:07 -04001838 bool put_dev = false;
Josef Bacike46c7282017-04-06 17:02:00 -04001839
1840 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1841 return -EPERM;
1842
1843 if (info->attrs[NBD_ATTR_INDEX])
1844 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1845 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1846 printk(KERN_ERR "nbd: must specify at least one socket\n");
1847 return -EINVAL;
1848 }
1849 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1850 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1851 return -EINVAL;
1852 }
1853again:
1854 mutex_lock(&nbd_index_mutex);
1855 if (index == -1) {
1856 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1857 if (ret == 0) {
1858 int new_index;
1859 new_index = nbd_dev_add(-1);
1860 if (new_index < 0) {
1861 mutex_unlock(&nbd_index_mutex);
1862 printk(KERN_ERR "nbd: failed to add new device\n");
Gustavo A. R. Silva09799622018-02-12 11:14:55 -06001863 return new_index;
Josef Bacike46c7282017-04-06 17:02:00 -04001864 }
1865 nbd = idr_find(&nbd_index_idr, new_index);
1866 }
1867 } else {
1868 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike6a76272017-08-14 18:25:33 +00001869 if (!nbd) {
1870 ret = nbd_dev_add(index);
1871 if (ret < 0) {
1872 mutex_unlock(&nbd_index_mutex);
1873 printk(KERN_ERR "nbd: failed to add new device\n");
1874 return ret;
1875 }
1876 nbd = idr_find(&nbd_index_idr, index);
1877 }
Josef Bacike46c7282017-04-06 17:02:00 -04001878 }
Josef Bacike46c7282017-04-06 17:02:00 -04001879 if (!nbd) {
1880 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1881 index);
Josef Bacikc6a47592017-04-06 17:02:06 -04001882 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001883 return -EINVAL;
1884 }
Xiubo Li8454d682019-09-17 17:26:06 +05301885
1886 if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
1887 test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) {
1888 nbd->destroy_complete = &destroy_complete;
1889 mutex_unlock(&nbd_index_mutex);
1890
1891 /* Wait untill the the nbd stuff is totally destroyed */
1892 wait_for_completion(&destroy_complete);
1893 goto again;
1894 }
1895
Josef Bacikc6a47592017-04-06 17:02:06 -04001896 if (!refcount_inc_not_zero(&nbd->refs)) {
1897 mutex_unlock(&nbd_index_mutex);
1898 if (index == -1)
1899 goto again;
1900 printk(KERN_ERR "nbd: device at index %d is going down\n",
1901 index);
1902 return -EINVAL;
1903 }
1904 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001905
1906 mutex_lock(&nbd->config_lock);
1907 if (refcount_read(&nbd->config_refs)) {
1908 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001909 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001910 if (index == -1)
1911 goto again;
1912 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1913 return -EBUSY;
1914 }
1915 if (WARN_ON(nbd->config)) {
1916 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001917 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001918 return -EINVAL;
1919 }
1920 config = nbd->config = nbd_alloc_config();
1921 if (!nbd->config) {
1922 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001923 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001924 printk(KERN_ERR "nbd: couldn't allocate config\n");
1925 return -ENOMEM;
1926 }
1927 refcount_set(&nbd->config_refs, 1);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301928 set_bit(NBD_RT_BOUND, &config->runtime_flags);
Josef Bacike46c7282017-04-06 17:02:00 -04001929
Mike Christie4ddeaae82019-05-29 15:16:06 -05001930 ret = nbd_genl_size_set(info, nbd);
1931 if (ret)
1932 goto out;
1933
Mike Christie55313e92019-08-13 11:39:49 -05001934 if (info->attrs[NBD_ATTR_TIMEOUT])
1935 nbd_set_cmd_timeout(nbd,
1936 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04001937 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1938 config->dead_conn_timeout =
1939 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1940 config->dead_conn_timeout *= HZ;
1941 }
Josef Bacike46c7282017-04-06 17:02:00 -04001942 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1943 config->flags =
1944 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
Josef Bacika2c97902017-04-06 17:02:07 -04001945 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1946 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1947 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05301948 set_bit(NBD_RT_DESTROY_ON_DISCONNECT,
Josef Bacika2c97902017-04-06 17:02:07 -04001949 &config->runtime_flags);
Xiubo Li8454d682019-09-17 17:26:06 +05301950 set_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04001951 put_dev = true;
Xiubo Li8454d682019-09-17 17:26:06 +05301952 } else {
1953 clear_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04001954 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001955 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05301956 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001957 &config->runtime_flags);
1958 }
Josef Bacika2c97902017-04-06 17:02:07 -04001959 }
1960
Josef Bacike46c7282017-04-06 17:02:00 -04001961 if (info->attrs[NBD_ATTR_SOCKETS]) {
1962 struct nlattr *attr;
1963 int rem, fd;
1964
1965 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1966 rem) {
1967 struct nlattr *socks[NBD_SOCK_MAX+1];
1968
1969 if (nla_type(attr) != NBD_SOCK_ITEM) {
1970 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1971 ret = -EINVAL;
1972 goto out;
1973 }
Johannes Berg8cb08172019-04-26 14:07:28 +02001974 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
1975 attr,
1976 nbd_sock_policy,
1977 info->extack);
Josef Bacike46c7282017-04-06 17:02:00 -04001978 if (ret != 0) {
1979 printk(KERN_ERR "nbd: error processing sock list\n");
1980 ret = -EINVAL;
1981 goto out;
1982 }
1983 if (!socks[NBD_SOCK_FD])
1984 continue;
1985 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1986 ret = nbd_add_socket(nbd, fd, true);
1987 if (ret)
1988 goto out;
1989 }
1990 }
1991 ret = nbd_start_device(nbd);
1992out:
1993 mutex_unlock(&nbd->config_lock);
1994 if (!ret) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05301995 set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags);
Josef Bacike46c7282017-04-06 17:02:00 -04001996 refcount_inc(&nbd->config_refs);
1997 nbd_connect_reply(info, nbd->index);
1998 }
1999 nbd_config_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04002000 if (put_dev)
2001 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002002 return ret;
2003}
2004
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002005static void nbd_disconnect_and_put(struct nbd_device *nbd)
2006{
2007 mutex_lock(&nbd->config_lock);
2008 nbd_disconnect(nbd);
2009 nbd_clear_sock(nbd);
2010 mutex_unlock(&nbd->config_lock);
Mike Christiee9e006f2019-08-04 14:10:06 -05002011 /*
2012 * Make sure recv thread has finished, so it does not drop the last
2013 * config ref and try to destroy the workqueue from inside the work
2014 * queue.
2015 */
2016 flush_workqueue(nbd->recv_workq);
Xiubo Liec76a7b2019-09-17 17:26:05 +05302017 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002018 &nbd->config->runtime_flags))
2019 nbd_config_put(nbd);
2020}
2021
Josef Bacike46c7282017-04-06 17:02:00 -04002022static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
2023{
2024 struct nbd_device *nbd;
2025 int index;
2026
2027 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2028 return -EPERM;
2029
2030 if (!info->attrs[NBD_ATTR_INDEX]) {
2031 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
2032 return -EINVAL;
2033 }
2034 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2035 mutex_lock(&nbd_index_mutex);
2036 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike46c7282017-04-06 17:02:00 -04002037 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04002038 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04002039 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
2040 index);
2041 return -EINVAL;
2042 }
Josef Bacikc6a47592017-04-06 17:02:06 -04002043 if (!refcount_inc_not_zero(&nbd->refs)) {
2044 mutex_unlock(&nbd_index_mutex);
2045 printk(KERN_ERR "nbd: device at index %d is going down\n",
2046 index);
2047 return -EINVAL;
2048 }
2049 mutex_unlock(&nbd_index_mutex);
2050 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2051 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002052 return 0;
Josef Bacikc6a47592017-04-06 17:02:06 -04002053 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002054 nbd_disconnect_and_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002055 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002056 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002057 return 0;
2058}
2059
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002060static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
2061{
2062 struct nbd_device *nbd = NULL;
2063 struct nbd_config *config;
2064 int index;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002065 int ret = 0;
Josef Bacika2c97902017-04-06 17:02:07 -04002066 bool put_dev = false;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002067
2068 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2069 return -EPERM;
2070
2071 if (!info->attrs[NBD_ATTR_INDEX]) {
2072 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
2073 return -EINVAL;
2074 }
2075 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2076 mutex_lock(&nbd_index_mutex);
2077 nbd = idr_find(&nbd_index_idr, index);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002078 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04002079 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002080 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
2081 index);
2082 return -EINVAL;
2083 }
Josef Bacikc6a47592017-04-06 17:02:06 -04002084 if (!refcount_inc_not_zero(&nbd->refs)) {
2085 mutex_unlock(&nbd_index_mutex);
2086 printk(KERN_ERR "nbd: device at index %d is going down\n",
2087 index);
2088 return -EINVAL;
2089 }
2090 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002091
2092 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2093 dev_err(nbd_to_dev(nbd),
2094 "not configured, cannot reconfigure\n");
Josef Bacikc6a47592017-04-06 17:02:06 -04002095 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002096 return -EINVAL;
2097 }
2098
2099 mutex_lock(&nbd->config_lock);
2100 config = nbd->config;
Xiubo Liec76a7b2019-09-17 17:26:05 +05302101 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002102 !nbd->task_recv) {
2103 dev_err(nbd_to_dev(nbd),
2104 "not configured, cannot reconfigure\n");
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002105 ret = -EINVAL;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002106 goto out;
2107 }
2108
Mike Christie4ddeaae82019-05-29 15:16:06 -05002109 ret = nbd_genl_size_set(info, nbd);
2110 if (ret)
2111 goto out;
2112
Mike Christie55313e92019-08-13 11:39:49 -05002113 if (info->attrs[NBD_ATTR_TIMEOUT])
2114 nbd_set_cmd_timeout(nbd,
2115 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04002116 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
2117 config->dead_conn_timeout =
2118 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
2119 config->dead_conn_timeout *= HZ;
2120 }
Josef Bacika2c97902017-04-06 17:02:07 -04002121 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
2122 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
2123 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302124 if (!test_and_set_bit(NBD_RT_DESTROY_ON_DISCONNECT,
Josef Bacika2c97902017-04-06 17:02:07 -04002125 &config->runtime_flags))
2126 put_dev = true;
Xiubo Li8454d682019-09-17 17:26:06 +05302127 set_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04002128 } else {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302129 if (test_and_clear_bit(NBD_RT_DESTROY_ON_DISCONNECT,
Josef Bacika2c97902017-04-06 17:02:07 -04002130 &config->runtime_flags))
2131 refcount_inc(&nbd->refs);
Xiubo Li8454d682019-09-17 17:26:06 +05302132 clear_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04002133 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002134
2135 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302136 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002137 &config->runtime_flags);
2138 } else {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302139 clear_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002140 &config->runtime_flags);
2141 }
Josef Bacika2c97902017-04-06 17:02:07 -04002142 }
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002143
2144 if (info->attrs[NBD_ATTR_SOCKETS]) {
2145 struct nlattr *attr;
2146 int rem, fd;
2147
2148 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2149 rem) {
2150 struct nlattr *socks[NBD_SOCK_MAX+1];
2151
2152 if (nla_type(attr) != NBD_SOCK_ITEM) {
2153 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
2154 ret = -EINVAL;
2155 goto out;
2156 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002157 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2158 attr,
2159 nbd_sock_policy,
2160 info->extack);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002161 if (ret != 0) {
2162 printk(KERN_ERR "nbd: error processing sock list\n");
2163 ret = -EINVAL;
2164 goto out;
2165 }
2166 if (!socks[NBD_SOCK_FD])
2167 continue;
2168 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2169 ret = nbd_reconnect_socket(nbd, fd);
2170 if (ret) {
2171 if (ret == -ENOSPC)
2172 ret = 0;
2173 goto out;
2174 }
2175 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
2176 }
2177 }
2178out:
2179 mutex_unlock(&nbd->config_lock);
2180 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002181 nbd_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04002182 if (put_dev)
2183 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002184 return ret;
2185}
2186
Josef Bacike46c7282017-04-06 17:02:00 -04002187static const struct genl_ops nbd_connect_genl_ops[] = {
2188 {
2189 .cmd = NBD_CMD_CONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002190 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002191 .doit = nbd_genl_connect,
2192 },
2193 {
2194 .cmd = NBD_CMD_DISCONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002195 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002196 .doit = nbd_genl_disconnect,
2197 },
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002198 {
2199 .cmd = NBD_CMD_RECONFIGURE,
Johannes Bergef6243a2019-04-26 14:07:31 +02002200 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002201 .doit = nbd_genl_reconfigure,
2202 },
Josef Bacik47d902b2017-04-06 17:02:05 -04002203 {
2204 .cmd = NBD_CMD_STATUS,
Johannes Bergef6243a2019-04-26 14:07:31 +02002205 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacik47d902b2017-04-06 17:02:05 -04002206 .doit = nbd_genl_status,
2207 },
Josef Bacike46c7282017-04-06 17:02:00 -04002208};
2209
Josef Bacik799f9a32017-04-06 17:02:02 -04002210static const struct genl_multicast_group nbd_mcast_grps[] = {
2211 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2212};
2213
Josef Bacike46c7282017-04-06 17:02:00 -04002214static struct genl_family nbd_genl_family __ro_after_init = {
2215 .hdrsize = 0,
2216 .name = NBD_GENL_FAMILY_NAME,
2217 .version = NBD_GENL_VERSION,
2218 .module = THIS_MODULE,
2219 .ops = nbd_connect_genl_ops,
2220 .n_ops = ARRAY_SIZE(nbd_connect_genl_ops),
2221 .maxattr = NBD_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01002222 .policy = nbd_attr_policy,
Josef Bacik799f9a32017-04-06 17:02:02 -04002223 .mcgrps = nbd_mcast_grps,
2224 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
Josef Bacike46c7282017-04-06 17:02:00 -04002225};
2226
Josef Bacik47d902b2017-04-06 17:02:05 -04002227static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2228{
2229 struct nlattr *dev_opt;
2230 u8 connected = 0;
2231 int ret;
2232
2233 /* This is a little racey, but for status it's ok. The
2234 * reason we don't take a ref here is because we can't
2235 * take a ref in the index == -1 case as we would need
2236 * to put under the nbd_index_mutex, which could
2237 * deadlock if we are configured to remove ourselves
2238 * once we're disconnected.
2239 */
2240 if (refcount_read(&nbd->config_refs))
2241 connected = 1;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002242 dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM);
Josef Bacik47d902b2017-04-06 17:02:05 -04002243 if (!dev_opt)
2244 return -EMSGSIZE;
2245 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2246 if (ret)
2247 return -EMSGSIZE;
2248 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2249 connected);
2250 if (ret)
2251 return -EMSGSIZE;
2252 nla_nest_end(reply, dev_opt);
2253 return 0;
2254}
2255
2256static int status_cb(int id, void *ptr, void *data)
2257{
2258 struct nbd_device *nbd = ptr;
2259 return populate_nbd_status(nbd, (struct sk_buff *)data);
2260}
2261
2262static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2263{
2264 struct nlattr *dev_list;
2265 struct sk_buff *reply;
2266 void *reply_head;
2267 size_t msg_size;
2268 int index = -1;
2269 int ret = -ENOMEM;
2270
2271 if (info->attrs[NBD_ATTR_INDEX])
2272 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2273
2274 mutex_lock(&nbd_index_mutex);
2275
2276 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2277 nla_attr_size(sizeof(u8)));
2278 msg_size *= (index == -1) ? nbd_total_devices : 1;
2279
2280 reply = genlmsg_new(msg_size, GFP_KERNEL);
2281 if (!reply)
2282 goto out;
2283 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2284 NBD_CMD_STATUS);
2285 if (!reply_head) {
2286 nlmsg_free(reply);
2287 goto out;
2288 }
2289
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002290 dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST);
Josef Bacik47d902b2017-04-06 17:02:05 -04002291 if (index == -1) {
2292 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2293 if (ret) {
2294 nlmsg_free(reply);
2295 goto out;
2296 }
2297 } else {
2298 struct nbd_device *nbd;
2299 nbd = idr_find(&nbd_index_idr, index);
2300 if (nbd) {
2301 ret = populate_nbd_status(nbd, reply);
2302 if (ret) {
2303 nlmsg_free(reply);
2304 goto out;
2305 }
2306 }
2307 }
2308 nla_nest_end(reply, dev_list);
2309 genlmsg_end(reply, reply_head);
Li RongQingcd46eb82019-02-19 13:14:07 +08002310 ret = genlmsg_reply(reply, info);
Josef Bacik47d902b2017-04-06 17:02:05 -04002311out:
2312 mutex_unlock(&nbd_index_mutex);
2313 return ret;
2314}
2315
Josef Bacike46c7282017-04-06 17:02:00 -04002316static void nbd_connect_reply(struct genl_info *info, int index)
2317{
2318 struct sk_buff *skb;
2319 void *msg_head;
2320 int ret;
2321
2322 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2323 if (!skb)
2324 return;
2325 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2326 NBD_CMD_CONNECT);
2327 if (!msg_head) {
2328 nlmsg_free(skb);
2329 return;
2330 }
2331 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2332 if (ret) {
2333 nlmsg_free(skb);
2334 return;
2335 }
2336 genlmsg_end(skb, msg_head);
2337 genlmsg_reply(skb, info);
2338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Josef Bacik799f9a32017-04-06 17:02:02 -04002340static void nbd_mcast_index(int index)
2341{
2342 struct sk_buff *skb;
2343 void *msg_head;
2344 int ret;
2345
2346 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2347 if (!skb)
2348 return;
2349 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2350 NBD_CMD_LINK_DEAD);
2351 if (!msg_head) {
2352 nlmsg_free(skb);
2353 return;
2354 }
2355 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2356 if (ret) {
2357 nlmsg_free(skb);
2358 return;
2359 }
2360 genlmsg_end(skb, msg_head);
2361 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2362}
2363
2364static void nbd_dead_link_work(struct work_struct *work)
2365{
2366 struct link_dead_args *args = container_of(work, struct link_dead_args,
2367 work);
2368 nbd_mcast_index(args->index);
2369 kfree(args);
2370}
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372static int __init nbd_init(void)
2373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 int i;
2375
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08002376 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002378 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02002379 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002380 return -EINVAL;
2381 }
2382
2383 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02002384 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002385 part_shift = fls(max_part);
2386
Namhyung Kim5988ce22011-05-28 14:44:46 +02002387 /*
2388 * Adjust max_part according to part_shift as it is exported
2389 * to user space so that user can know the max number of
2390 * partition kernel should be able to manage.
2391 *
2392 * Note that -1 is required because partition 0 is reserved
2393 * for the whole disk.
2394 */
2395 max_part = (1UL << part_shift) - 1;
2396 }
2397
Namhyung Kim3b271082011-05-28 14:44:46 +02002398 if ((1UL << part_shift) > DISK_MAX_PARTS)
2399 return -EINVAL;
2400
2401 if (nbds_max > 1UL << (MINORBITS - part_shift))
2402 return -EINVAL;
2403
Mike Christiee9e006f2019-08-04 14:10:06 -05002404 if (register_blkdev(NBD_MAJOR, "nbd"))
Josef Bacikb0d91112017-02-01 16:11:40 -05002405 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Josef Bacike46c7282017-04-06 17:02:00 -04002407 if (genl_register_family(&nbd_genl_family)) {
2408 unregister_blkdev(NBD_MAJOR, "nbd");
Josef Bacike46c7282017-04-06 17:02:00 -04002409 return -EINVAL;
2410 }
Markus Pargmann30d53d92015-08-17 08:20:06 +02002411 nbd_dbg_init();
2412
Josef Bacikb0d91112017-02-01 16:11:40 -05002413 mutex_lock(&nbd_index_mutex);
2414 for (i = 0; i < nbds_max; i++)
2415 nbd_dev_add(i);
2416 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05002418}
2419
2420static int nbd_exit_cb(int id, void *ptr, void *data)
2421{
Josef Bacikc6a47592017-04-06 17:02:06 -04002422 struct list_head *list = (struct list_head *)data;
Josef Bacikb0d91112017-02-01 16:11:40 -05002423 struct nbd_device *nbd = ptr;
Josef Bacikc6a47592017-04-06 17:02:06 -04002424
Josef Bacikc6a47592017-04-06 17:02:06 -04002425 list_add_tail(&nbd->list, list);
Josef Bacikb0d91112017-02-01 16:11:40 -05002426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
2429static void __exit nbd_cleanup(void)
2430{
Josef Bacikc6a47592017-04-06 17:02:06 -04002431 struct nbd_device *nbd;
2432 LIST_HEAD(del_list);
2433
Markus Pargmann30d53d92015-08-17 08:20:06 +02002434 nbd_dbg_close();
2435
Josef Bacikc6a47592017-04-06 17:02:06 -04002436 mutex_lock(&nbd_index_mutex);
2437 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2438 mutex_unlock(&nbd_index_mutex);
2439
Josef Bacik60ae36a2017-04-28 09:49:19 -04002440 while (!list_empty(&del_list)) {
2441 nbd = list_first_entry(&del_list, struct nbd_device, list);
2442 list_del_init(&nbd->list);
2443 if (refcount_read(&nbd->refs) != 1)
Josef Bacikc6a47592017-04-06 17:02:06 -04002444 printk(KERN_ERR "nbd: possibly leaking a device\n");
2445 nbd_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002446 }
2447
Josef Bacikb0d91112017-02-01 16:11:40 -05002448 idr_destroy(&nbd_index_idr);
Josef Bacike46c7282017-04-06 17:02:00 -04002449 genl_unregister_family(&nbd_genl_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
2453module_init(nbd_init);
2454module_exit(nbd_cleanup);
2455
2456MODULE_DESCRIPTION("Network Block Device");
2457MODULE_LICENSE("GPL");
2458
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07002459module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002460MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2461module_param(max_part, int, 0444);
Josef Bacik7a8362a2017-08-14 18:56:16 +00002462MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");