blob: c4f9ccf5cc2ac56ac7df9a335d43e2a7e03c28d9 [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
Ming Leib40813d2020-10-28 15:24:34 +0800299static void nbd_size_update(struct nbd_device *nbd, bool start)
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);
Ming Leib40813d2020-10-28 15:24:34 +0800316 if (start)
317 set_blocksize(bdev, config->blksize);
Jan Karac8a83a62019-01-14 09:48:09 +0100318 } else
Christoph Hellwig38430f02020-09-21 09:19:45 +0200319 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
Josef Bacik9e2b19672018-05-16 14:51:19 -0400320 bdput(bdev);
321 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200322 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
323}
324
Josef Bacik29eaadc2017-04-06 17:01:59 -0400325static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
326 loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200327{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400328 struct nbd_config *config = nbd->config;
329 config->blksize = blocksize;
330 config->bytesize = blocksize * nr_blocks;
Josef Bacikc3f7c932018-05-16 14:51:18 -0400331 if (nbd->task_recv != NULL)
Ming Leib40813d2020-10-28 15:24:34 +0800332 nbd_size_update(nbd, false);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200333}
334
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200335static void nbd_complete_rq(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200337 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Kevin Vigoree57a052018-06-04 10:40:12 -0600339 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200340 cmd->status ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200342 blk_mq_end_request(req, cmd->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Markus Pargmanne018e752015-04-02 10:11:39 +0200345/*
346 * Forcibly shutdown the socket causing all listeners to error
347 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200348static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700349{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400350 struct nbd_config *config = nbd->config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500351 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700352
Josef Bacik5ea8d102017-04-06 17:01:58 -0400353 if (config->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200354 return;
Xiubo Liec76a7b2019-09-17 17:26:05 +0530355 if (test_and_set_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500356 return;
357
Josef Bacik5ea8d102017-04-06 17:01:58 -0400358 for (i = 0; i < config->num_connections; i++) {
359 struct nbd_sock *nsock = config->socks[i];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500360 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400361 nbd_mark_nsock_dead(nbd, nsock, 0);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500362 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100363 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500364 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700365}
366
Mike Christie00514672019-08-13 11:39:50 -0500367static u32 req_to_nbd_cmd_type(struct request *req)
368{
369 switch (req_op(req)) {
370 case REQ_OP_DISCARD:
371 return NBD_CMD_TRIM;
372 case REQ_OP_FLUSH:
373 return NBD_CMD_FLUSH;
374 case REQ_OP_WRITE:
375 return NBD_CMD_WRITE;
376 case REQ_OP_READ:
377 return NBD_CMD_READ;
378 default:
379 return U32_MAX;
380 }
381}
382
Josef Bacik0eadf372016-09-08 12:33:40 -0700383static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
384 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700385{
Josef Bacik0eadf372016-09-08 12:33:40 -0700386 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
387 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400388 struct nbd_config *config;
Paul Clements7fdfd402007-10-16 23:27:37 -0700389
Josef Bacikde6346e2019-10-21 15:56:27 -0400390 if (!mutex_trylock(&cmd->lock))
391 return BLK_EH_RESET_TIMER;
392
Josef Bacik5ea8d102017-04-06 17:01:58 -0400393 if (!refcount_inc_not_zero(&nbd->config_refs)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200394 cmd->status = BLK_STS_TIMEOUT;
Josef Bacikde6346e2019-10-21 15:56:27 -0400395 mutex_unlock(&cmd->lock);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200396 goto done;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400397 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400398 config = nbd->config;
399
Hou Pud9709582020-02-28 01:40:29 -0500400 if (config->num_connections > 1 ||
401 (config->num_connections == 1 && nbd->tag_set.timeout)) {
Josef Bacikf3733242017-04-06 17:01:57 -0400402 dev_err_ratelimited(nbd_to_dev(nbd),
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600403 "Connection timed out, retrying (%d/%d alive)\n",
404 atomic_read(&config->live_connections),
405 config->num_connections);
Josef Bacikf3733242017-04-06 17:01:57 -0400406 /*
407 * Hooray we have more connections, requeue this IO, the submit
Hou Pud9709582020-02-28 01:40:29 -0500408 * path will put it on a real connection. Or if only one
409 * connection is configured, the submit path will wait util
410 * a new connection is reconfigured or util dead timeout.
Josef Bacikf3733242017-04-06 17:01:57 -0400411 */
Hou Pud9709582020-02-28 01:40:29 -0500412 if (config->socks) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400413 if (cmd->index < config->num_connections) {
Josef Bacikf3733242017-04-06 17:01:57 -0400414 struct nbd_sock *nsock =
Josef Bacik5ea8d102017-04-06 17:01:58 -0400415 config->socks[cmd->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400416 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400417 /* We can have multiple outstanding requests, so
418 * we don't want to mark the nsock dead if we've
419 * already reconnected with a new socket, so
420 * only mark it dead if its the same socket we
421 * were sent out on.
422 */
423 if (cmd->cookie == nsock->cookie)
424 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400425 mutex_unlock(&nsock->tx_lock);
426 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400427 mutex_unlock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400428 nbd_requeue_cmd(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400429 nbd_config_put(nbd);
Christoph Hellwig66005932018-05-29 15:52:29 +0200430 return BLK_EH_DONE;
Josef Bacikf3733242017-04-06 17:01:57 -0400431 }
Josef Bacikf3733242017-04-06 17:01:57 -0400432 }
Mike Christie2da22da2019-08-13 11:39:52 -0500433
434 if (!nbd->tag_set.timeout) {
435 /*
436 * Userspace sets timeout=0 to disable socket disconnection,
437 * so just warn and reset the timer.
438 */
Hou Pu2c272542020-02-28 01:40:30 -0500439 struct nbd_sock *nsock = config->socks[cmd->index];
Mike Christie2da22da2019-08-13 11:39:52 -0500440 cmd->retries++;
441 dev_info(nbd_to_dev(nbd), "Possible stuck request %p: control (%s@%llu,%uB). Runtime %u seconds\n",
442 req, nbdcmd_to_ascii(req_to_nbd_cmd_type(req)),
443 (unsigned long long)blk_rq_pos(req) << 9,
444 blk_rq_bytes(req), (req->timeout / HZ) * cmd->retries);
445
Hou Pu2c272542020-02-28 01:40:30 -0500446 mutex_lock(&nsock->tx_lock);
447 if (cmd->cookie != nsock->cookie) {
448 nbd_requeue_cmd(cmd);
449 mutex_unlock(&nsock->tx_lock);
450 mutex_unlock(&cmd->lock);
451 nbd_config_put(nbd);
452 return BLK_EH_DONE;
453 }
454 mutex_unlock(&nsock->tx_lock);
Mike Christie2da22da2019-08-13 11:39:52 -0500455 mutex_unlock(&cmd->lock);
456 nbd_config_put(nbd);
457 return BLK_EH_RESET_TIMER;
458 }
459
460 dev_err_ratelimited(nbd_to_dev(nbd), "Connection timed out\n");
Xiubo Liec76a7b2019-09-17 17:26:05 +0530461 set_bit(NBD_RT_TIMEDOUT, &config->runtime_flags);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200462 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400463 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500464 sock_shutdown(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400465 nbd_config_put(nbd);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200466done:
467 blk_mq_complete_request(req);
468 return BLK_EH_DONE;
Paul Clements7fdfd402007-10-16 23:27:37 -0700469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
472 * Send or receive packet.
473 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500474static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400475 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400477 struct nbd_config *config = nbd->config;
478 struct socket *sock = config->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int result;
480 struct msghdr msg;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700481 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700483 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500484 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200485 "Attempted %s on closed socket in sock_xmit\n",
486 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700487 return -EINVAL;
488 }
489
Al Viroc9f2b6a2015-11-12 05:09:35 -0500490 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500491
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700492 noreclaim_flag = memalloc_noreclaim_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700494 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 msg.msg_name = NULL;
496 msg.msg_namelen = 0;
497 msg.msg_control = NULL;
498 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
500
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200501 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500502 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200503 else
Al Viroc1696ca2015-11-12 04:51:19 -0500504 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (result <= 0) {
507 if (result == 0)
508 result = -EPIPE; /* short read */
509 break;
510 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400511 if (sent)
512 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500513 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700515 memalloc_noreclaim_restore(noreclaim_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 return result;
518}
519
Josef Bacik32e67a32017-10-24 15:57:18 -0400520/*
521 * Different settings for sk->sk_sndtimeo can result in different return values
522 * if there is a signal pending when we enter sendmsg, because reasons?
523 */
524static inline int was_interrupted(int result)
525{
526 return result == -ERESTARTSYS || result == -EINTR;
527}
528
Paul Clements7fdfd402007-10-16 23:27:37 -0700529/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500530static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700532 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400533 struct nbd_config *config = nbd->config;
534 struct nbd_sock *nsock = config->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500535 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500536 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
537 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
538 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900539 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700540 struct bio *bio;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400541 u64 handle;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200542 u32 type;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400543 u32 nbd_cmd_flags = 0;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400544 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200545
David Howellsaa563d72018-10-20 00:57:56 +0100546 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500547
Mike Christie00514672019-08-13 11:39:50 -0500548 type = req_to_nbd_cmd_type(req);
549 if (type == U32_MAX)
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100550 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100552 if (rq_data_dir(req) == WRITE &&
Josef Bacik5ea8d102017-04-06 17:01:58 -0400553 (config->flags & NBD_FLAG_READ_ONLY)) {
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100554 dev_err_ratelimited(disk_to_dev(nbd->disk),
555 "Write on read-only\n");
556 return -EIO;
557 }
558
Shaun McDowell685c9b22017-05-25 23:55:54 -0400559 if (req->cmd_flags & REQ_FUA)
560 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
561
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400562 /* We did a partial send previously, and we at least sent the whole
563 * request struct, so just go and send the rest of the pages in the
564 * request.
565 */
566 if (sent) {
567 if (sent >= sizeof(request)) {
568 skip = sent - sizeof(request);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700569
570 /* initialize handle for tracing purposes */
571 handle = nbd_cmd_handle(cmd);
572
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400573 goto send_pages;
574 }
575 iov_iter_advance(&from, sent);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400576 } else {
577 cmd->cmd_cookie++;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400578 }
Josef Bacikf3733242017-04-06 17:01:57 -0400579 cmd->index = index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400580 cmd->cookie = nsock->cookie;
Mike Christie2da22da2019-08-13 11:39:52 -0500581 cmd->retries = 0;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400582 request.type = htonl(type | nbd_cmd_flags);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500583 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800584 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
585 request.len = htonl(size);
586 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400587 handle = nbd_cmd_handle(cmd);
588 memcpy(request.handle, &handle, sizeof(handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Matt Mullinsea106722019-04-26 11:49:48 -0700590 trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
591
Markus Pargmannd18509f2015-04-02 10:11:38 +0200592 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600593 req, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200594 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500595 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400596 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700597 trace_nbd_header_sent(req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400599 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400600 /* If we havne't sent anything we can just return BUSY,
601 * however if we have sent something we need to make
602 * sure we only allow this req to be sent until we are
603 * completely done.
604 */
605 if (sent) {
606 nsock->pending = req;
607 nsock->sent = sent;
608 }
Josef Bacikd7d94d42018-07-16 12:11:34 -0400609 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200610 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400611 }
Josef Bacika897b662016-12-05 16:20:29 -0500612 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200613 "Send control failed (result %d)\n", result);
Josef Bacikf3733242017-04-06 17:01:57 -0400614 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400616send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700617 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400618 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700619
Jens Axboe429a7872016-11-17 12:30:37 -0700620 bio = req->bio;
621 while (bio) {
622 struct bio *next = bio->bi_next;
623 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800624 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700625
626 bio_for_each_segment(bvec, bio, iter) {
627 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500628 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700629
Markus Pargmannd18509f2015-04-02 10:11:38 +0200630 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600631 req, bvec.bv_len);
David Howellsaa563d72018-10-20 00:57:56 +0100632 iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400633 if (skip) {
634 if (skip >= iov_iter_count(&from)) {
635 skip -= iov_iter_count(&from);
636 continue;
637 }
638 iov_iter_advance(&from, skip);
639 skip = 0;
640 }
641 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200642 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400643 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400644 /* We've already sent the header, we
645 * have no choice but to set pending and
646 * return BUSY.
647 */
648 nsock->pending = req;
649 nsock->sent = sent;
Josef Bacikd7d94d42018-07-16 12:11:34 -0400650 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200651 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400652 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700653 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200654 "Send data failed (result %d)\n",
655 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400656 return -EAGAIN;
Jens Axboe6c92e692007-08-16 13:43:12 +0200657 }
Jens Axboe429a7872016-11-17 12:30:37 -0700658 /*
659 * The completion might already have come in,
660 * so break for the last one instead of letting
661 * the iterator do it. This prevents use-after-free
662 * of the bio.
663 */
664 if (is_last)
665 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Jens Axboe429a7872016-11-17 12:30:37 -0700667 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400669out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700670 trace_nbd_payload_sent(req, handle);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400671 nsock->pending = NULL;
672 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500677static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400679 struct nbd_config *config = nbd->config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int result;
681 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700682 struct nbd_cmd *cmd;
683 struct request *req = NULL;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400684 u64 handle;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700685 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500686 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500687 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
688 struct iov_iter to;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400689 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 reply.magic = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100692 iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400693 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (result <= 0) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400695 if (!nbd_disconnected(config))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500696 dev_err(disk_to_dev(nbd->disk),
697 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200698 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700700
701 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700702 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700703 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200704 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700705 }
706
Josef Bacik8f3ea352018-07-16 12:11:35 -0400707 memcpy(&handle, reply.handle, sizeof(handle));
708 tag = nbd_handle_to_tag(handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700709 hwq = blk_mq_unique_tag_to_hwq(tag);
710 if (hwq < nbd->tag_set.nr_hw_queues)
711 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
712 blk_mq_unique_tag_to_tag(tag));
713 if (!req || !blk_mq_request_started(req)) {
714 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
715 tag, req);
716 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Andrew Hall2abd2de2019-04-26 11:49:49 -0700718 trace_nbd_header_received(req, handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700719 cmd = blk_mq_rq_to_pdu(req);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400720
721 mutex_lock(&cmd->lock);
722 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
723 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
724 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
725 ret = -ENOENT;
726 goto out;
727 }
Josef Bacik7ce23e82019-10-21 15:56:28 -0400728 if (cmd->status != BLK_STS_OK) {
729 dev_err(disk_to_dev(nbd->disk), "Command already handled %p\n",
730 req);
731 ret = -ENOENT;
732 goto out;
733 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400734 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
735 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
736 req);
737 ret = -ENOENT;
738 goto out;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700741 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200742 ntohl(reply.error));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200743 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400744 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Kevin Vigoree57a052018-06-04 10:40:12 -0600747 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200748 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200749 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800750 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200751
752 rq_for_each_segment(bvec, req, iter) {
David Howellsaa563d72018-10-20 00:57:56 +0100753 iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400754 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200755 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700756 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200757 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400758 /*
Hou Pud9709582020-02-28 01:40:29 -0500759 * If we've disconnected, we need to make sure we
Josef Bacikf3733242017-04-06 17:01:57 -0400760 * complete this request, otherwise error out
761 * and let the timeout stuff handle resubmitting
762 * this request onto another connection.
763 */
Hou Pud9709582020-02-28 01:40:29 -0500764 if (nbd_disconnected(config)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200765 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400766 goto out;
Josef Bacikf3733242017-04-06 17:01:57 -0400767 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400768 ret = -EIO;
769 goto out;
Jens Axboe6c92e692007-08-16 13:43:12 +0200770 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200771 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600772 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400775out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700776 trace_nbd_payload_received(req, handle);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400777 mutex_unlock(&cmd->lock);
778 return ret ? ERR_PTR(ret) : cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Josef Bacik9561a7a2016-11-22 14:04:40 -0500781static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500783 struct recv_thread_args *args = container_of(work,
784 struct recv_thread_args,
785 work);
786 struct nbd_device *nbd = args->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400787 struct nbd_config *config = nbd->config;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700788 struct nbd_cmd *cmd;
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200789 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Markus Pargmann19391832015-08-17 08:20:03 +0200791 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500792 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700793 if (IS_ERR(cmd)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400794 struct nbd_sock *nsock = config->socks[args->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400795
796 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400797 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400798 mutex_unlock(&nsock->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200799 break;
800 }
801
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200802 rq = blk_mq_rq_from_pdu(cmd);
803 if (likely(!blk_should_fake_timeout(rq->q)))
804 blk_mq_complete_request(rq);
Markus Pargmann19391832015-08-17 08:20:03 +0200805 }
Xiubo Li87aac3a2020-10-13 22:45:14 -0400806 nbd_config_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400807 atomic_dec(&config->recv_threads);
808 wake_up(&config->recv_wq);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400809 kfree(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Jens Axboe7baa8572018-11-08 10:24:07 -0700812static bool nbd_clear_req(struct request *req, void *data, bool reserved)
Josef Bacikfd8383f2016-09-08 12:33:37 -0700813{
Christoph Hellwigd250bf4e2018-05-30 18:51:00 +0200814 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700815
Josef Bacikde6346e2019-10-21 15:56:27 -0400816 mutex_lock(&cmd->lock);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200817 cmd->status = BLK_STS_IOERR;
Josef Bacikde6346e2019-10-21 15:56:27 -0400818 mutex_unlock(&cmd->lock);
819
Christoph Hellwig08e00292017-04-20 16:03:09 +0200820 blk_mq_complete_request(req);
Jens Axboe7baa8572018-11-08 10:24:07 -0700821 return true;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700822}
823
Wanlong Gaof4507162012-03-28 14:42:51 -0700824static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300826 blk_mq_quiesce_queue(nbd->disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700827 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300828 blk_mq_unquiesce_queue(nbd->disk->queue);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200829 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Josef Bacikf3733242017-04-06 17:01:57 -0400832static int find_fallback(struct nbd_device *nbd, int index)
833{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400834 struct nbd_config *config = nbd->config;
Josef Bacikf3733242017-04-06 17:01:57 -0400835 int new_index = -1;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400836 struct nbd_sock *nsock = config->socks[index];
Josef Bacikf3733242017-04-06 17:01:57 -0400837 int fallback = nsock->fallback_index;
838
Xiubo Liec76a7b2019-09-17 17:26:05 +0530839 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacikf3733242017-04-06 17:01:57 -0400840 return new_index;
841
Josef Bacik5ea8d102017-04-06 17:01:58 -0400842 if (config->num_connections <= 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400843 dev_err_ratelimited(disk_to_dev(nbd->disk),
Hou Pud9709582020-02-28 01:40:29 -0500844 "Dead connection, failed to find a fallback\n");
Josef Bacikf3733242017-04-06 17:01:57 -0400845 return new_index;
846 }
847
Josef Bacik5ea8d102017-04-06 17:01:58 -0400848 if (fallback >= 0 && fallback < config->num_connections &&
849 !config->socks[fallback]->dead)
Josef Bacikf3733242017-04-06 17:01:57 -0400850 return fallback;
851
852 if (nsock->fallback_index < 0 ||
Josef Bacik5ea8d102017-04-06 17:01:58 -0400853 nsock->fallback_index >= config->num_connections ||
854 config->socks[nsock->fallback_index]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400855 int i;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400856 for (i = 0; i < config->num_connections; i++) {
Josef Bacikf3733242017-04-06 17:01:57 -0400857 if (i == index)
858 continue;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400859 if (!config->socks[i]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400860 new_index = i;
861 break;
862 }
863 }
864 nsock->fallback_index = new_index;
865 if (new_index < 0) {
866 dev_err_ratelimited(disk_to_dev(nbd->disk),
867 "Dead connection, failed to find a fallback\n");
868 return new_index;
869 }
870 }
871 new_index = nsock->fallback_index;
872 return new_index;
873}
Paul Clements7fdfd402007-10-16 23:27:37 -0700874
Josef Bacik560bc4b2017-04-06 17:02:04 -0400875static int wait_for_reconnect(struct nbd_device *nbd)
876{
877 struct nbd_config *config = nbd->config;
878 if (!config->dead_conn_timeout)
879 return 0;
Xiubo Liec76a7b2019-09-17 17:26:05 +0530880 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacik560bc4b2017-04-06 17:02:04 -0400881 return 0;
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600882 return wait_event_timeout(config->conn_wait,
883 atomic_read(&config->live_connections) > 0,
884 config->dead_conn_timeout) > 0;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400885}
886
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400887static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700888{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700889 struct request *req = blk_mq_rq_from_pdu(cmd);
890 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400891 struct nbd_config *config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500892 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400893 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700894
Josef Bacik5ea8d102017-04-06 17:01:58 -0400895 if (!refcount_inc_not_zero(&nbd->config_refs)) {
896 dev_err_ratelimited(disk_to_dev(nbd->disk),
897 "Socks array is empty\n");
Josef Bacik6a468d52017-11-06 16:11:58 -0500898 blk_mq_start_request(req);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400899 return -EINVAL;
900 }
901 config = nbd->config;
902
903 if (index >= config->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500904 dev_err_ratelimited(disk_to_dev(nbd->disk),
905 "Attempted send on invalid socket\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -0400906 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500907 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400908 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500909 }
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200910 cmd->status = BLK_STS_OK;
Josef Bacikf3733242017-04-06 17:01:57 -0400911again:
Josef Bacik5ea8d102017-04-06 17:01:58 -0400912 nsock = config->socks[index];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500913 mutex_lock(&nsock->tx_lock);
Josef Bacikf3733242017-04-06 17:01:57 -0400914 if (nsock->dead) {
Josef Bacik560bc4b2017-04-06 17:02:04 -0400915 int old_index = index;
Josef Bacikf3733242017-04-06 17:01:57 -0400916 index = find_fallback(nbd, index);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500917 mutex_unlock(&nsock->tx_lock);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400918 if (index < 0) {
919 if (wait_for_reconnect(nbd)) {
920 index = old_index;
921 goto again;
922 }
923 /* All the sockets should already be down at this point,
924 * we just want to make sure that DISCONNECTED is set so
925 * any requests that come in that were queue'ed waiting
926 * for the reconnect timer don't trigger the timer again
927 * and instead just error out.
928 */
929 sock_shutdown(nbd);
930 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500931 blk_mq_start_request(req);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400932 return -EIO;
933 }
Josef Bacikf3733242017-04-06 17:01:57 -0400934 goto again;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700935 }
936
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400937 /* Handle the case that we have a pending request that was partially
938 * transmitted that _has_ to be serviced first. We need to call requeue
939 * here so that it gets put _after_ the request that is already on the
940 * dispatch list.
941 */
Josef Bacik6a468d52017-11-06 16:11:58 -0500942 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400943 if (unlikely(nsock->pending && nsock->pending != req)) {
Josef Bacikd7d94d42018-07-16 12:11:34 -0400944 nbd_requeue_cmd(cmd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400945 ret = 0;
946 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700947 }
Josef Bacikf3733242017-04-06 17:01:57 -0400948 /*
949 * Some failures are related to the link going down, so anything that
950 * returns EAGAIN can be retried on a different socket.
951 */
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400952 ret = nbd_send_cmd(nbd, cmd, index);
Josef Bacikf3733242017-04-06 17:01:57 -0400953 if (ret == -EAGAIN) {
954 dev_err_ratelimited(disk_to_dev(nbd->disk),
Josef Bacik6a468d52017-11-06 16:11:58 -0500955 "Request send failed, requeueing\n");
Josef Bacik799f9a32017-04-06 17:02:02 -0400956 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400957 nbd_requeue_cmd(cmd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500958 ret = 0;
Josef Bacikf3733242017-04-06 17:01:57 -0400959 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400960out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500961 mutex_unlock(&nsock->tx_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400962 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400963 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700964}
965
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200966static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700967 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700968{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700969 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400970 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700971
Josef Bacik9561a7a2016-11-22 14:04:40 -0500972 /*
973 * Since we look at the bio's to send the request over the network we
974 * need to make sure the completion work doesn't mark this request done
975 * before we are done doing our send. This keeps us from dereferencing
976 * freed data if we have particularly fast completions (ie we get the
977 * completion before we exit sock_xmit on the last bvec) or in the case
978 * that the server is misbehaving (or there was an error) before we're
979 * done sending everything over the wire.
980 */
Josef Bacik8f3ea352018-07-16 12:11:35 -0400981 mutex_lock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400982 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400983
984 /* We can be called directly from the user space process, which means we
985 * could possibly have signals pending so our sendmsg will fail. In
986 * this case we need to return that we are busy, otherwise error out as
987 * appropriate.
988 */
989 ret = nbd_handle_cmd(cmd, hctx->queue_num);
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400990 if (ret < 0)
991 ret = BLK_STS_IOERR;
992 else if (!ret)
993 ret = BLK_STS_OK;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400994 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500995
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400996 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Mike Christiecf1b2322019-10-17 16:27:34 -0500999static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
1000 int *err)
1001{
1002 struct socket *sock;
1003
1004 *err = 0;
1005 sock = sockfd_lookup(fd, err);
1006 if (!sock)
1007 return NULL;
1008
1009 if (sock->ops->shutdown == sock_no_shutdown) {
1010 dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
1011 *err = -EINVAL;
Sun Kedff10bb2019-11-19 14:09:11 +08001012 sockfd_put(sock);
Mike Christiecf1b2322019-10-17 16:27:34 -05001013 return NULL;
1014 }
1015
1016 return sock;
1017}
1018
Josef Bacike46c7282017-04-06 17:02:00 -04001019static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
1020 bool netlink)
Markus Pargmann23272a672015-10-29 11:51:16 +01001021{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001022 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001023 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001024 struct nbd_sock **socks;
1025 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -05001026 int err;
1027
Mike Christiecf1b2322019-10-17 16:27:34 -05001028 sock = nbd_get_socket(nbd, arg, &err);
Josef Bacik9442b732017-02-07 17:10:22 -05001029 if (!sock)
1030 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +01001031
Josef Bacike46c7282017-04-06 17:02:00 -04001032 if (!netlink && !nbd->task_setup &&
Xiubo Liec76a7b2019-09-17 17:26:05 +05301033 !test_bit(NBD_RT_BOUND, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -05001034 nbd->task_setup = current;
Josef Bacike46c7282017-04-06 17:02:00 -04001035
1036 if (!netlink &&
1037 (nbd->task_setup != current ||
Xiubo Liec76a7b2019-09-17 17:26:05 +05301038 test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
Josef Bacik9561a7a2016-11-22 14:04:40 -05001039 dev_err(disk_to_dev(nbd->disk),
1040 "Device being setup by another task");
Zheng Bin579dd912020-06-29 09:23:49 +08001041 err = -EBUSY;
1042 goto put_socket;
1043 }
1044
1045 nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
1046 if (!nsock) {
1047 err = -ENOMEM;
1048 goto put_socket;
Markus Pargmann23272a672015-10-29 11:51:16 +01001049 }
1050
Josef Bacik5ea8d102017-04-06 17:01:58 -04001051 socks = krealloc(config->socks, (config->num_connections + 1) *
Josef Bacik9561a7a2016-11-22 14:04:40 -05001052 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -04001053 if (!socks) {
Zheng Bin579dd912020-06-29 09:23:49 +08001054 kfree(nsock);
1055 err = -ENOMEM;
1056 goto put_socket;
Josef Bacik9b1355d2017-04-06 17:01:56 -04001057 }
Navid Emamdoost03bf73c2019-09-23 15:09:58 -05001058
1059 config->socks = socks;
1060
Josef Bacikf3733242017-04-06 17:01:57 -04001061 nsock->fallback_index = -1;
1062 nsock->dead = false;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001063 mutex_init(&nsock->tx_lock);
1064 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001065 nsock->pending = NULL;
1066 nsock->sent = 0;
Josef Bacik799f9a32017-04-06 17:02:02 -04001067 nsock->cookie = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001068 socks[config->num_connections++] = nsock;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001069 atomic_inc(&config->live_connections);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001070
1071 return 0;
Zheng Bin579dd912020-06-29 09:23:49 +08001072
1073put_socket:
1074 sockfd_put(sock);
1075 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +01001076}
1077
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001078static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
1079{
1080 struct nbd_config *config = nbd->config;
1081 struct socket *sock, *old;
1082 struct recv_thread_args *args;
1083 int i;
1084 int err;
1085
Mike Christiecf1b2322019-10-17 16:27:34 -05001086 sock = nbd_get_socket(nbd, arg, &err);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001087 if (!sock)
1088 return err;
1089
1090 args = kzalloc(sizeof(*args), GFP_KERNEL);
1091 if (!args) {
1092 sockfd_put(sock);
1093 return -ENOMEM;
1094 }
1095
1096 for (i = 0; i < config->num_connections; i++) {
1097 struct nbd_sock *nsock = config->socks[i];
1098
1099 if (!nsock->dead)
1100 continue;
1101
1102 mutex_lock(&nsock->tx_lock);
1103 if (!nsock->dead) {
1104 mutex_unlock(&nsock->tx_lock);
1105 continue;
1106 }
1107 sk_set_memalloc(sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001108 if (nbd->tag_set.timeout)
1109 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001110 atomic_inc(&config->recv_threads);
1111 refcount_inc(&nbd->config_refs);
1112 old = nsock->sock;
1113 nsock->fallback_index = -1;
1114 nsock->sock = sock;
1115 nsock->dead = false;
1116 INIT_WORK(&args->work, recv_work);
1117 args->index = i;
1118 args->nbd = nbd;
Josef Bacik799f9a32017-04-06 17:02:02 -04001119 nsock->cookie++;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001120 mutex_unlock(&nsock->tx_lock);
1121 sockfd_put(old);
1122
Xiubo Liec76a7b2019-09-17 17:26:05 +05301123 clear_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
Josef Bacik7a362ea2017-07-25 13:31:19 -04001124
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001125 /* We take the tx_mutex in an error path in the recv_work, so we
1126 * need to queue_work outside of the tx_mutex.
1127 */
Mike Christiee9e006f2019-08-04 14:10:06 -05001128 queue_work(nbd->recv_workq, &args->work);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001129
1130 atomic_inc(&config->live_connections);
1131 wake_up(&config->conn_wait);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001132 return 0;
1133 }
1134 sockfd_put(sock);
1135 kfree(args);
1136 return -ENOSPC;
1137}
1138
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001139static void nbd_bdev_reset(struct block_device *bdev)
1140{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -04001141 if (bdev->bd_openers > 1)
1142 return;
Christoph Hellwig611bee52020-08-23 11:10:41 +02001143 bd_set_nr_sectors(bdev, 0);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001144}
1145
Josef Bacik29eaadc2017-04-06 17:01:59 -04001146static void nbd_parse_flags(struct nbd_device *nbd)
Markus Pargmannd02cf532015-10-29 12:06:15 +01001147{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001148 struct nbd_config *config = nbd->config;
1149 if (config->flags & NBD_FLAG_READ_ONLY)
Josef Bacik29eaadc2017-04-06 17:01:59 -04001150 set_disk_ro(nbd->disk, true);
1151 else
1152 set_disk_ro(nbd->disk, false);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001153 if (config->flags & NBD_FLAG_SEND_TRIM)
Bart Van Assche8b904b52018-03-07 17:10:10 -08001154 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Shaun McDowell685c9b22017-05-25 23:55:54 -04001155 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1156 if (config->flags & NBD_FLAG_SEND_FUA)
1157 blk_queue_write_cache(nbd->disk->queue, true, true);
1158 else
1159 blk_queue_write_cache(nbd->disk->queue, true, false);
1160 }
Markus Pargmannd02cf532015-10-29 12:06:15 +01001161 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -06001162 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +01001163}
1164
Josef Bacik9561a7a2016-11-22 14:04:40 -05001165static void send_disconnects(struct nbd_device *nbd)
1166{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001167 struct nbd_config *config = nbd->config;
Al Viroc9f2b6a2015-11-12 05:09:35 -05001168 struct nbd_request request = {
1169 .magic = htonl(NBD_REQUEST_MAGIC),
1170 .type = htonl(NBD_CMD_DISC),
1171 };
1172 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1173 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001174 int i, ret;
1175
Josef Bacik5ea8d102017-04-06 17:01:58 -04001176 for (i = 0; i < config->num_connections; i++) {
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001177 struct nbd_sock *nsock = config->socks[i];
1178
David Howellsaa563d72018-10-20 00:57:56 +01001179 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001180 mutex_lock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001181 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001182 if (ret <= 0)
1183 dev_err(disk_to_dev(nbd->disk),
1184 "Send disconnect failed %d\n", ret);
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001185 mutex_unlock(&nsock->tx_lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001186 }
1187}
1188
Josef Bacik29eaadc2017-04-06 17:01:59 -04001189static int nbd_disconnect(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001190{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001191 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001192
Josef Bacik5ea8d102017-04-06 17:01:58 -04001193 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Xiubo Liec76a7b2019-09-17 17:26:05 +05301194 set_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
Xiubo Li8454d682019-09-17 17:26:06 +05301195 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags);
Josef Bacik2e134562017-07-21 10:48:13 -04001196 send_disconnects(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001197 return 0;
1198}
1199
Josef Bacik29eaadc2017-04-06 17:01:59 -04001200static void nbd_clear_sock(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001201{
1202 sock_shutdown(nbd);
1203 nbd_clear_que(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001204 nbd->task_setup = NULL;
Josef Bacik9442b732017-02-07 17:10:22 -05001205}
1206
Josef Bacik5ea8d102017-04-06 17:01:58 -04001207static void nbd_config_put(struct nbd_device *nbd)
1208{
1209 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1210 &nbd->config_lock)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001211 struct nbd_config *config = nbd->config;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001212 nbd_dev_dbg_close(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001213 nbd_size_clear(nbd);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301214 if (test_and_clear_bit(NBD_RT_HAS_PID_FILE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001215 &config->runtime_flags))
1216 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1217 nbd->task_recv = NULL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001218 nbd_clear_sock(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001219 if (config->num_connections) {
1220 int i;
1221 for (i = 0; i < config->num_connections; i++) {
1222 sockfd_put(config->socks[i]->sock);
1223 kfree(config->socks[i]);
1224 }
1225 kfree(config->socks);
1226 }
Ilya Dryomovfa976532017-05-23 17:49:55 +02001227 kfree(nbd->config);
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001228 nbd->config = NULL;
1229
Mike Christiee9e006f2019-08-04 14:10:06 -05001230 if (nbd->recv_workq)
1231 destroy_workqueue(nbd->recv_workq);
1232 nbd->recv_workq = NULL;
1233
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001234 nbd->tag_set.timeout = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001235 nbd->disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001236 nbd->disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001237 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001238 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Josef Bacika2c97902017-04-06 17:02:07 -04001239
Josef Bacik5ea8d102017-04-06 17:01:58 -04001240 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001241 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001242 module_put(THIS_MODULE);
1243 }
1244}
1245
Josef Bacike46c7282017-04-06 17:02:00 -04001246static int nbd_start_device(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001247{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001248 struct nbd_config *config = nbd->config;
1249 int num_connections = config->num_connections;
Josef Bacik9442b732017-02-07 17:10:22 -05001250 int error = 0, i;
1251
1252 if (nbd->task_recv)
1253 return -EBUSY;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001254 if (!config->socks)
Josef Bacik9442b732017-02-07 17:10:22 -05001255 return -EINVAL;
1256 if (num_connections > 1 &&
Josef Bacik5ea8d102017-04-06 17:01:58 -04001257 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
Josef Bacik9442b732017-02-07 17:10:22 -05001258 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001259 return -EINVAL;
Josef Bacik9442b732017-02-07 17:10:22 -05001260 }
1261
Mike Christiee9e006f2019-08-04 14:10:06 -05001262 nbd->recv_workq = alloc_workqueue("knbd%d-recv",
1263 WQ_MEM_RECLAIM | WQ_HIGHPRI |
1264 WQ_UNBOUND, 0, nbd->index);
1265 if (!nbd->recv_workq) {
1266 dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1267 return -ENOMEM;
1268 }
1269
Josef Bacik5ea8d102017-04-06 17:01:58 -04001270 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
Josef Bacik9442b732017-02-07 17:10:22 -05001271 nbd->task_recv = current;
Josef Bacik9442b732017-02-07 17:10:22 -05001272
Josef Bacik29eaadc2017-04-06 17:01:59 -04001273 nbd_parse_flags(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001274
1275 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1276 if (error) {
1277 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001278 return error;
Josef Bacik9442b732017-02-07 17:10:22 -05001279 }
Xiubo Liec76a7b2019-09-17 17:26:05 +05301280 set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags);
Josef Bacik9442b732017-02-07 17:10:22 -05001281
1282 nbd_dev_dbg_init(nbd);
1283 for (i = 0; i < num_connections; i++) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001284 struct recv_thread_args *args;
1285
1286 args = kzalloc(sizeof(*args), GFP_KERNEL);
1287 if (!args) {
1288 sock_shutdown(nbd);
Sun Ke5c0dd222020-01-22 11:18:57 +08001289 /*
1290 * If num_connections is m (2 < m),
1291 * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
1292 * But NO.(n + 1) failed. We still have n recv threads.
1293 * So, add flush_workqueue here to prevent recv threads
1294 * dropping the last config_refs and trying to destroy
1295 * the workqueue from inside the workqueue.
1296 */
1297 if (i)
1298 flush_workqueue(nbd->recv_workq);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001299 return -ENOMEM;
1300 }
1301 sk_set_memalloc(config->socks[i]->sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001302 if (nbd->tag_set.timeout)
1303 config->socks[i]->sock->sk->sk_sndtimeo =
1304 nbd->tag_set.timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001305 atomic_inc(&config->recv_threads);
1306 refcount_inc(&nbd->config_refs);
1307 INIT_WORK(&args->work, recv_work);
1308 args->nbd = nbd;
1309 args->index = i;
Mike Christiee9e006f2019-08-04 14:10:06 -05001310 queue_work(nbd->recv_workq, &args->work);
Josef Bacik9442b732017-02-07 17:10:22 -05001311 }
Ming Leib40813d2020-10-28 15:24:34 +08001312 nbd_size_update(nbd, true);
Josef Bacike46c7282017-04-06 17:02:00 -04001313 return error;
1314}
1315
1316static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1317{
1318 struct nbd_config *config = nbd->config;
1319 int ret;
1320
1321 ret = nbd_start_device(nbd);
1322 if (ret)
1323 return ret;
1324
Josef Bacike46c7282017-04-06 17:02:00 -04001325 if (max_part)
Christoph Hellwig38430f02020-09-21 09:19:45 +02001326 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
Josef Bacike46c7282017-04-06 17:02:00 -04001327 mutex_unlock(&nbd->config_lock);
1328 ret = wait_event_interruptible(config->recv_wq,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001329 atomic_read(&config->recv_threads) == 0);
Mike Christie1c058392019-12-08 16:51:50 -06001330 if (ret)
Josef Bacik5ea8d102017-04-06 17:01:58 -04001331 sock_shutdown(nbd);
Mike Christie1c058392019-12-08 16:51:50 -06001332 flush_workqueue(nbd->recv_workq);
1333
Josef Bacik9442b732017-02-07 17:10:22 -05001334 mutex_lock(&nbd->config_lock);
Josef Bacik76aa1d32018-05-16 14:51:22 -04001335 nbd_bdev_reset(bdev);
Josef Bacik9442b732017-02-07 17:10:22 -05001336 /* user requested, ignore socket errors */
Xiubo Liec76a7b2019-09-17 17:26:05 +05301337 if (test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001338 ret = 0;
Xiubo Liec76a7b2019-09-17 17:26:05 +05301339 if (test_bit(NBD_RT_TIMEDOUT, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001340 ret = -ETIMEDOUT;
1341 return ret;
Josef Bacik9442b732017-02-07 17:10:22 -05001342}
Markus Pargmann30d53d92015-08-17 08:20:06 +02001343
Josef Bacik29eaadc2017-04-06 17:01:59 -04001344static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1345 struct block_device *bdev)
1346{
Josef Bacik2516ab12017-04-06 17:02:03 -04001347 sock_shutdown(nbd);
Munehisa Kamata2b5c8f02019-07-31 20:13:10 +08001348 __invalidate_device(bdev, true);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001349 nbd_bdev_reset(bdev);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301350 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
Josef Bacike46c7282017-04-06 17:02:00 -04001351 &nbd->config->runtime_flags))
1352 nbd_config_put(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001353}
1354
Xiubo Li553768d2019-05-29 15:16:05 -05001355static bool nbd_is_valid_blksize(unsigned long blksize)
1356{
1357 if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
1358 blksize > PAGE_SIZE)
1359 return false;
1360 return true;
1361}
1362
Mike Christie55313e92019-08-13 11:39:49 -05001363static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
1364{
1365 nbd->tag_set.timeout = timeout * HZ;
Mike Christie2da22da2019-08-13 11:39:52 -05001366 if (timeout)
1367 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
Hou Puacb19e12020-08-10 08:00:44 -04001368 else
1369 blk_queue_rq_timeout(nbd->disk->queue, 30 * HZ);
Mike Christie55313e92019-08-13 11:39:49 -05001370}
1371
Josef Bacik9561a7a2016-11-22 14:04:40 -05001372/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -07001373static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -07001374 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001376 struct nbd_config *config = nbd->config;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -05001379 case NBD_DISCONNECT:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001380 return nbd_disconnect(nbd);
Markus Pargmann23272a672015-10-29 11:51:16 +01001381 case NBD_CLEAR_SOCK:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001382 nbd_clear_sock_ioctl(nbd, bdev);
1383 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001384 case NBD_SET_SOCK:
Josef Bacike46c7282017-04-06 17:02:00 -04001385 return nbd_add_socket(nbd, arg, false);
Josef Bacik9442b732017-02-07 17:10:22 -05001386 case NBD_SET_BLKSIZE:
Xiubo Li553768d2019-05-29 15:16:05 -05001387 if (!arg)
1388 arg = NBD_DEF_BLKSIZE;
1389 if (!nbd_is_valid_blksize(arg))
Jens Axboebc811f02018-09-04 11:52:34 -06001390 return -EINVAL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001391 nbd_size_set(nbd, arg,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001392 div_s64(config->bytesize, arg));
Josef Bacike5445412017-02-13 10:39:47 -05001393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 case NBD_SET_SIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001395 nbd_size_set(nbd, config->blksize,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001396 div_s64(arg, config->blksize));
Josef Bacike5445412017-02-13 10:39:47 -05001397 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +02001398 case NBD_SET_SIZE_BLOCKS:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001399 nbd_size_set(nbd, config->blksize, arg);
Josef Bacike5445412017-02-13 10:39:47 -05001400 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -07001401 case NBD_SET_TIMEOUT:
Mike Christie2da22da2019-08-13 11:39:52 -05001402 nbd_set_cmd_timeout(nbd, arg);
Paul Clements7fdfd402007-10-16 23:27:37 -07001403 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001404
Paul Clements2f012502012-10-04 17:16:15 -07001405 case NBD_SET_FLAGS:
Josef Bacik5ea8d102017-04-06 17:01:58 -04001406 config->flags = arg;
Paul Clements2f012502012-10-04 17:16:15 -07001407 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001408 case NBD_DO_IT:
Josef Bacike46c7282017-04-06 17:02:00 -04001409 return nbd_start_device_ioctl(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -08001411 /*
1412 * This is for compatibility only. The queue is always cleared
1413 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 0;
1416 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -07001417 /*
1418 * For compatibility only, we no longer keep a list of
1419 * outstanding requests.
1420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return 0;
1422 }
Pavel Machek1a2ad212009-04-02 16:58:41 -07001423 return -ENOTTY;
1424}
1425
1426static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1427 unsigned int cmd, unsigned long arg)
1428{
Wanlong Gaof4507162012-03-28 14:42:51 -07001429 struct nbd_device *nbd = bdev->bd_disk->private_data;
Josef Bacike46c7282017-04-06 17:02:00 -04001430 struct nbd_config *config = nbd->config;
1431 int error = -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001432
1433 if (!capable(CAP_SYS_ADMIN))
1434 return -EPERM;
1435
Josef Bacik1dae69b2017-05-05 22:25:18 -04001436 /* The block layer will pass back some non-nbd ioctls in case we have
1437 * special handling for them, but we don't so just return an error.
1438 */
1439 if (_IOC_TYPE(cmd) != 0xab)
1440 return -EINVAL;
1441
Josef Bacik9561a7a2016-11-22 14:04:40 -05001442 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001443
1444 /* Don't allow ioctl operations on a nbd device that was created with
1445 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1446 */
Xiubo Liec76a7b2019-09-17 17:26:05 +05301447 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
Josef Bacike46c7282017-04-06 17:02:00 -04001448 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1449 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1450 else
1451 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -05001452 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -07001453 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Josef Bacik5ea8d102017-04-06 17:01:58 -04001456static struct nbd_config *nbd_alloc_config(void)
1457{
1458 struct nbd_config *config;
1459
1460 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1461 if (!config)
1462 return NULL;
1463 atomic_set(&config->recv_threads, 0);
1464 init_waitqueue_head(&config->recv_wq);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001465 init_waitqueue_head(&config->conn_wait);
Xiubo Li553768d2019-05-29 15:16:05 -05001466 config->blksize = NBD_DEF_BLKSIZE;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001467 atomic_set(&config->live_connections, 0);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001468 try_module_get(THIS_MODULE);
1469 return config;
1470}
1471
1472static int nbd_open(struct block_device *bdev, fmode_t mode)
1473{
1474 struct nbd_device *nbd;
1475 int ret = 0;
1476
1477 mutex_lock(&nbd_index_mutex);
1478 nbd = bdev->bd_disk->private_data;
1479 if (!nbd) {
1480 ret = -ENXIO;
1481 goto out;
1482 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001483 if (!refcount_inc_not_zero(&nbd->refs)) {
1484 ret = -ENXIO;
1485 goto out;
1486 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001487 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1488 struct nbd_config *config;
1489
1490 mutex_lock(&nbd->config_lock);
1491 if (refcount_inc_not_zero(&nbd->config_refs)) {
1492 mutex_unlock(&nbd->config_lock);
1493 goto out;
1494 }
1495 config = nbd->config = nbd_alloc_config();
1496 if (!config) {
1497 ret = -ENOMEM;
1498 mutex_unlock(&nbd->config_lock);
1499 goto out;
1500 }
1501 refcount_set(&nbd->config_refs, 1);
Josef Bacikc6a47592017-04-06 17:02:06 -04001502 refcount_inc(&nbd->refs);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001503 mutex_unlock(&nbd->config_lock);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001504 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Josef Bacikfe1f9e62018-05-16 14:51:21 -04001505 } else if (nbd_disconnected(nbd->config)) {
Christoph Hellwig38430f02020-09-21 09:19:45 +02001506 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001507 }
1508out:
1509 mutex_unlock(&nbd_index_mutex);
1510 return ret;
1511}
1512
1513static void nbd_release(struct gendisk *disk, fmode_t mode)
1514{
1515 struct nbd_device *nbd = disk->private_data;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001516 struct block_device *bdev = bdget_disk(disk, 0);
1517
Xiubo Liec76a7b2019-09-17 17:26:05 +05301518 if (test_bit(NBD_RT_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001519 bdev->bd_openers == 0)
1520 nbd_disconnect_and_put(nbd);
1521
Josef Bacik5ea8d102017-04-06 17:01:58 -04001522 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001523 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001524}
1525
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001526static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 .owner = THIS_MODULE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001529 .open = nbd_open,
1530 .release = nbd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001531 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -05001532 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533};
1534
Markus Pargmann30d53d92015-08-17 08:20:06 +02001535#if IS_ENABLED(CONFIG_DEBUG_FS)
1536
1537static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1538{
1539 struct nbd_device *nbd = s->private;
1540
1541 if (nbd->task_recv)
1542 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +02001543
1544 return 0;
1545}
1546
1547static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1548{
1549 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1550}
1551
1552static const struct file_operations nbd_dbg_tasks_ops = {
1553 .open = nbd_dbg_tasks_open,
1554 .read = seq_read,
1555 .llseek = seq_lseek,
1556 .release = single_release,
1557};
1558
1559static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1560{
1561 struct nbd_device *nbd = s->private;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001562 u32 flags = nbd->config->flags;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001563
1564 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1565
1566 seq_puts(s, "Known flags:\n");
1567
1568 if (flags & NBD_FLAG_HAS_FLAGS)
1569 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1570 if (flags & NBD_FLAG_READ_ONLY)
1571 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1572 if (flags & NBD_FLAG_SEND_FLUSH)
1573 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
Shaun McDowell685c9b22017-05-25 23:55:54 -04001574 if (flags & NBD_FLAG_SEND_FUA)
1575 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
Markus Pargmann30d53d92015-08-17 08:20:06 +02001576 if (flags & NBD_FLAG_SEND_TRIM)
1577 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1578
1579 return 0;
1580}
1581
1582static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1583{
1584 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1585}
1586
1587static const struct file_operations nbd_dbg_flags_ops = {
1588 .open = nbd_dbg_flags_open,
1589 .read = seq_read,
1590 .llseek = seq_lseek,
1591 .release = single_release,
1592};
1593
1594static int nbd_dev_dbg_init(struct nbd_device *nbd)
1595{
1596 struct dentry *dir;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001597 struct nbd_config *config = nbd->config;
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001598
1599 if (!nbd_dbg_dir)
1600 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001601
1602 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001603 if (!dir) {
1604 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1605 nbd_name(nbd));
1606 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001607 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001608 config->dbg_dir = dir;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001609
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001610 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001611 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -07001612 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001613 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -04001614 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001615
1616 return 0;
1617}
1618
1619static void nbd_dev_dbg_close(struct nbd_device *nbd)
1620{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001621 debugfs_remove_recursive(nbd->config->dbg_dir);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001622}
1623
1624static int nbd_dbg_init(void)
1625{
1626 struct dentry *dbg_dir;
1627
1628 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001629 if (!dbg_dir)
1630 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001631
1632 nbd_dbg_dir = dbg_dir;
1633
1634 return 0;
1635}
1636
1637static void nbd_dbg_close(void)
1638{
1639 debugfs_remove_recursive(nbd_dbg_dir);
1640}
1641
1642#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1643
1644static int nbd_dev_dbg_init(struct nbd_device *nbd)
1645{
1646 return 0;
1647}
1648
1649static void nbd_dev_dbg_close(struct nbd_device *nbd)
1650{
1651}
1652
1653static int nbd_dbg_init(void)
1654{
1655 return 0;
1656}
1657
1658static void nbd_dbg_close(void)
1659{
1660}
1661
1662#endif
1663
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001664static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1665 unsigned int hctx_idx, unsigned int numa_node)
Josef Bacikfd8383f2016-09-08 12:33:37 -07001666{
1667 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001668 cmd->nbd = set->driver_data;
Josef Bacikd7d94d42018-07-16 12:11:34 -04001669 cmd->flags = 0;
Josef Bacik8f3ea352018-07-16 12:11:35 -04001670 mutex_init(&cmd->lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001671 return 0;
1672}
1673
Eric Biggersf363b082017-03-30 13:39:16 -07001674static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001675 .queue_rq = nbd_queue_rq,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +02001676 .complete = nbd_complete_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001677 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001678 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001679};
1680
Josef Bacikb0d91112017-02-01 16:11:40 -05001681static int nbd_dev_add(int index)
1682{
1683 struct nbd_device *nbd;
1684 struct gendisk *disk;
1685 struct request_queue *q;
1686 int err = -ENOMEM;
1687
1688 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1689 if (!nbd)
1690 goto out;
1691
1692 disk = alloc_disk(1 << part_shift);
1693 if (!disk)
1694 goto out_free_nbd;
1695
1696 if (index >= 0) {
1697 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1698 GFP_KERNEL);
1699 if (err == -ENOSPC)
1700 err = -EEXIST;
1701 } else {
1702 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1703 if (err >= 0)
1704 index = err;
1705 }
1706 if (err < 0)
1707 goto out_free_disk;
1708
Josef Bacike46c7282017-04-06 17:02:00 -04001709 nbd->index = index;
Josef Bacikb0d91112017-02-01 16:11:40 -05001710 nbd->disk = disk;
1711 nbd->tag_set.ops = &nbd_mq_ops;
1712 nbd->tag_set.nr_hw_queues = 1;
1713 nbd->tag_set.queue_depth = 128;
1714 nbd->tag_set.numa_node = NUMA_NO_NODE;
1715 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1716 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Ming Lei56d18f62019-02-15 19:13:24 +08001717 BLK_MQ_F_BLOCKING;
Josef Bacikb0d91112017-02-01 16:11:40 -05001718 nbd->tag_set.driver_data = nbd;
Xiubo Li8454d682019-09-17 17:26:06 +05301719 nbd->destroy_complete = NULL;
Josef Bacikb0d91112017-02-01 16:11:40 -05001720
1721 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1722 if (err)
1723 goto out_free_idr;
1724
1725 q = blk_mq_init_queue(&nbd->tag_set);
1726 if (IS_ERR(q)) {
1727 err = PTR_ERR(q);
1728 goto out_free_tags;
1729 }
1730 disk->queue = q;
1731
1732 /*
1733 * Tell the block layer that we are not a rotational device
1734 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08001735 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1736 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Josef Bacik6df133a2018-05-23 13:35:59 -04001737 disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001738 disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001739 blk_queue_max_discard_sectors(disk->queue, 0);
Josef Bacikebb16d02017-04-18 16:22:51 -04001740 blk_queue_max_segment_size(disk->queue, UINT_MAX);
Josef Bacik1cc1f172017-04-20 15:47:01 -04001741 blk_queue_max_segments(disk->queue, USHRT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001742 blk_queue_max_hw_sectors(disk->queue, 65536);
1743 disk->queue->limits.max_sectors = 256;
1744
Josef Bacikb0d91112017-02-01 16:11:40 -05001745 mutex_init(&nbd->config_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001746 refcount_set(&nbd->config_refs, 0);
Josef Bacikc6a47592017-04-06 17:02:06 -04001747 refcount_set(&nbd->refs, 1);
1748 INIT_LIST_HEAD(&nbd->list);
Josef Bacikb0d91112017-02-01 16:11:40 -05001749 disk->major = NBD_MAJOR;
1750 disk->first_minor = index << part_shift;
1751 disk->fops = &nbd_fops;
1752 disk->private_data = nbd;
1753 sprintf(disk->disk_name, "nbd%d", index);
Josef Bacikb0d91112017-02-01 16:11:40 -05001754 add_disk(disk);
Josef Bacik47d902b2017-04-06 17:02:05 -04001755 nbd_total_devices++;
Josef Bacikb0d91112017-02-01 16:11:40 -05001756 return index;
1757
1758out_free_tags:
1759 blk_mq_free_tag_set(&nbd->tag_set);
1760out_free_idr:
1761 idr_remove(&nbd_index_idr, index);
1762out_free_disk:
1763 put_disk(disk);
1764out_free_nbd:
1765 kfree(nbd);
1766out:
1767 return err;
1768}
1769
Josef Bacike46c7282017-04-06 17:02:00 -04001770static int find_free_cb(int id, void *ptr, void *data)
1771{
1772 struct nbd_device *nbd = ptr;
1773 struct nbd_device **found = data;
1774
1775 if (!refcount_read(&nbd->config_refs)) {
1776 *found = nbd;
1777 return 1;
1778 }
1779 return 0;
1780}
1781
1782/* Netlink interface. */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001783static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001784 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1785 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1786 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1787 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1788 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1789 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1790 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
Josef Bacik560bc4b2017-04-06 17:02:04 -04001791 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001792 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
Josef Bacike46c7282017-04-06 17:02:00 -04001793};
1794
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001795static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001796 [NBD_SOCK_FD] = { .type = NLA_U32 },
1797};
1798
Josef Bacik47d902b2017-04-06 17:02:05 -04001799/* We don't use this right now since we don't parse the incoming list, but we
1800 * still want it here so userspace knows what to expect.
1801 */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001802static const struct nla_policy __attribute__((unused))
Josef Bacik47d902b2017-04-06 17:02:05 -04001803nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1804 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1805 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1806};
1807
Mike Christie4ddeaae82019-05-29 15:16:06 -05001808static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
1809{
1810 struct nbd_config *config = nbd->config;
1811 u64 bsize = config->blksize;
1812 u64 bytes = config->bytesize;
1813
1814 if (info->attrs[NBD_ATTR_SIZE_BYTES])
1815 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1816
1817 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1818 bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1819 if (!bsize)
1820 bsize = NBD_DEF_BLKSIZE;
1821 if (!nbd_is_valid_blksize(bsize)) {
1822 printk(KERN_ERR "Invalid block size %llu\n", bsize);
1823 return -EINVAL;
1824 }
1825 }
1826
1827 if (bytes != config->bytesize || bsize != config->blksize)
1828 nbd_size_set(nbd, bsize, div64_u64(bytes, bsize));
1829 return 0;
1830}
1831
Josef Bacike46c7282017-04-06 17:02:00 -04001832static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1833{
Xiubo Li8454d682019-09-17 17:26:06 +05301834 DECLARE_COMPLETION_ONSTACK(destroy_complete);
Josef Bacike46c7282017-04-06 17:02:00 -04001835 struct nbd_device *nbd = NULL;
1836 struct nbd_config *config;
1837 int index = -1;
1838 int ret;
Josef Bacika2c97902017-04-06 17:02:07 -04001839 bool put_dev = false;
Josef Bacike46c7282017-04-06 17:02:00 -04001840
1841 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1842 return -EPERM;
1843
1844 if (info->attrs[NBD_ATTR_INDEX])
1845 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1846 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1847 printk(KERN_ERR "nbd: must specify at least one socket\n");
1848 return -EINVAL;
1849 }
1850 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1851 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1852 return -EINVAL;
1853 }
1854again:
1855 mutex_lock(&nbd_index_mutex);
1856 if (index == -1) {
1857 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1858 if (ret == 0) {
1859 int new_index;
1860 new_index = nbd_dev_add(-1);
1861 if (new_index < 0) {
1862 mutex_unlock(&nbd_index_mutex);
1863 printk(KERN_ERR "nbd: failed to add new device\n");
Gustavo A. R. Silva09799622018-02-12 11:14:55 -06001864 return new_index;
Josef Bacike46c7282017-04-06 17:02:00 -04001865 }
1866 nbd = idr_find(&nbd_index_idr, new_index);
1867 }
1868 } else {
1869 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike6a76272017-08-14 18:25:33 +00001870 if (!nbd) {
1871 ret = nbd_dev_add(index);
1872 if (ret < 0) {
1873 mutex_unlock(&nbd_index_mutex);
1874 printk(KERN_ERR "nbd: failed to add new device\n");
1875 return ret;
1876 }
1877 nbd = idr_find(&nbd_index_idr, index);
1878 }
Josef Bacike46c7282017-04-06 17:02:00 -04001879 }
Josef Bacike46c7282017-04-06 17:02:00 -04001880 if (!nbd) {
1881 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1882 index);
Josef Bacikc6a47592017-04-06 17:02:06 -04001883 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001884 return -EINVAL;
1885 }
Xiubo Li8454d682019-09-17 17:26:06 +05301886
1887 if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
1888 test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) {
1889 nbd->destroy_complete = &destroy_complete;
1890 mutex_unlock(&nbd_index_mutex);
1891
1892 /* Wait untill the the nbd stuff is totally destroyed */
1893 wait_for_completion(&destroy_complete);
1894 goto again;
1895 }
1896
Josef Bacikc6a47592017-04-06 17:02:06 -04001897 if (!refcount_inc_not_zero(&nbd->refs)) {
1898 mutex_unlock(&nbd_index_mutex);
1899 if (index == -1)
1900 goto again;
1901 printk(KERN_ERR "nbd: device at index %d is going down\n",
1902 index);
1903 return -EINVAL;
1904 }
1905 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001906
1907 mutex_lock(&nbd->config_lock);
1908 if (refcount_read(&nbd->config_refs)) {
1909 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001910 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001911 if (index == -1)
1912 goto again;
1913 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1914 return -EBUSY;
1915 }
1916 if (WARN_ON(nbd->config)) {
1917 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001918 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001919 return -EINVAL;
1920 }
1921 config = nbd->config = nbd_alloc_config();
1922 if (!nbd->config) {
1923 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001924 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001925 printk(KERN_ERR "nbd: couldn't allocate config\n");
1926 return -ENOMEM;
1927 }
1928 refcount_set(&nbd->config_refs, 1);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301929 set_bit(NBD_RT_BOUND, &config->runtime_flags);
Josef Bacike46c7282017-04-06 17:02:00 -04001930
Mike Christie4ddeaae82019-05-29 15:16:06 -05001931 ret = nbd_genl_size_set(info, nbd);
1932 if (ret)
1933 goto out;
1934
Mike Christie55313e92019-08-13 11:39:49 -05001935 if (info->attrs[NBD_ATTR_TIMEOUT])
1936 nbd_set_cmd_timeout(nbd,
1937 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04001938 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1939 config->dead_conn_timeout =
1940 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1941 config->dead_conn_timeout *= HZ;
1942 }
Josef Bacike46c7282017-04-06 17:02:00 -04001943 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1944 config->flags =
1945 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
Josef Bacika2c97902017-04-06 17:02:07 -04001946 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1947 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1948 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05301949 set_bit(NBD_RT_DESTROY_ON_DISCONNECT,
Josef Bacika2c97902017-04-06 17:02:07 -04001950 &config->runtime_flags);
Xiubo Li8454d682019-09-17 17:26:06 +05301951 set_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04001952 put_dev = true;
Xiubo Li8454d682019-09-17 17:26:06 +05301953 } else {
1954 clear_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04001955 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001956 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05301957 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001958 &config->runtime_flags);
1959 }
Josef Bacika2c97902017-04-06 17:02:07 -04001960 }
1961
Josef Bacike46c7282017-04-06 17:02:00 -04001962 if (info->attrs[NBD_ATTR_SOCKETS]) {
1963 struct nlattr *attr;
1964 int rem, fd;
1965
1966 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1967 rem) {
1968 struct nlattr *socks[NBD_SOCK_MAX+1];
1969
1970 if (nla_type(attr) != NBD_SOCK_ITEM) {
1971 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1972 ret = -EINVAL;
1973 goto out;
1974 }
Johannes Berg8cb08172019-04-26 14:07:28 +02001975 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
1976 attr,
1977 nbd_sock_policy,
1978 info->extack);
Josef Bacike46c7282017-04-06 17:02:00 -04001979 if (ret != 0) {
1980 printk(KERN_ERR "nbd: error processing sock list\n");
1981 ret = -EINVAL;
1982 goto out;
1983 }
1984 if (!socks[NBD_SOCK_FD])
1985 continue;
1986 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1987 ret = nbd_add_socket(nbd, fd, true);
1988 if (ret)
1989 goto out;
1990 }
1991 }
1992 ret = nbd_start_device(nbd);
1993out:
1994 mutex_unlock(&nbd->config_lock);
1995 if (!ret) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05301996 set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags);
Josef Bacike46c7282017-04-06 17:02:00 -04001997 refcount_inc(&nbd->config_refs);
1998 nbd_connect_reply(info, nbd->index);
1999 }
2000 nbd_config_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04002001 if (put_dev)
2002 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002003 return ret;
2004}
2005
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002006static void nbd_disconnect_and_put(struct nbd_device *nbd)
2007{
2008 mutex_lock(&nbd->config_lock);
2009 nbd_disconnect(nbd);
2010 nbd_clear_sock(nbd);
2011 mutex_unlock(&nbd->config_lock);
Mike Christiee9e006f2019-08-04 14:10:06 -05002012 /*
2013 * Make sure recv thread has finished, so it does not drop the last
2014 * config ref and try to destroy the workqueue from inside the work
2015 * queue.
2016 */
2017 flush_workqueue(nbd->recv_workq);
Xiubo Liec76a7b2019-09-17 17:26:05 +05302018 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002019 &nbd->config->runtime_flags))
2020 nbd_config_put(nbd);
2021}
2022
Josef Bacike46c7282017-04-06 17:02:00 -04002023static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
2024{
2025 struct nbd_device *nbd;
2026 int index;
2027
2028 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2029 return -EPERM;
2030
2031 if (!info->attrs[NBD_ATTR_INDEX]) {
2032 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
2033 return -EINVAL;
2034 }
2035 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2036 mutex_lock(&nbd_index_mutex);
2037 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike46c7282017-04-06 17:02:00 -04002038 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04002039 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04002040 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
2041 index);
2042 return -EINVAL;
2043 }
Josef Bacikc6a47592017-04-06 17:02:06 -04002044 if (!refcount_inc_not_zero(&nbd->refs)) {
2045 mutex_unlock(&nbd_index_mutex);
2046 printk(KERN_ERR "nbd: device at index %d is going down\n",
2047 index);
2048 return -EINVAL;
2049 }
2050 mutex_unlock(&nbd_index_mutex);
2051 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2052 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002053 return 0;
Josef Bacikc6a47592017-04-06 17:02:06 -04002054 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002055 nbd_disconnect_and_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002056 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002057 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002058 return 0;
2059}
2060
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002061static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
2062{
2063 struct nbd_device *nbd = NULL;
2064 struct nbd_config *config;
2065 int index;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002066 int ret = 0;
Josef Bacika2c97902017-04-06 17:02:07 -04002067 bool put_dev = false;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002068
2069 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2070 return -EPERM;
2071
2072 if (!info->attrs[NBD_ATTR_INDEX]) {
2073 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
2074 return -EINVAL;
2075 }
2076 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2077 mutex_lock(&nbd_index_mutex);
2078 nbd = idr_find(&nbd_index_idr, index);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002079 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04002080 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002081 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
2082 index);
2083 return -EINVAL;
2084 }
Josef Bacikc6a47592017-04-06 17:02:06 -04002085 if (!refcount_inc_not_zero(&nbd->refs)) {
2086 mutex_unlock(&nbd_index_mutex);
2087 printk(KERN_ERR "nbd: device at index %d is going down\n",
2088 index);
2089 return -EINVAL;
2090 }
2091 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002092
2093 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2094 dev_err(nbd_to_dev(nbd),
2095 "not configured, cannot reconfigure\n");
Josef Bacikc6a47592017-04-06 17:02:06 -04002096 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002097 return -EINVAL;
2098 }
2099
2100 mutex_lock(&nbd->config_lock);
2101 config = nbd->config;
Xiubo Liec76a7b2019-09-17 17:26:05 +05302102 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002103 !nbd->task_recv) {
2104 dev_err(nbd_to_dev(nbd),
2105 "not configured, cannot reconfigure\n");
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002106 ret = -EINVAL;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002107 goto out;
2108 }
2109
Mike Christie4ddeaae82019-05-29 15:16:06 -05002110 ret = nbd_genl_size_set(info, nbd);
2111 if (ret)
2112 goto out;
2113
Mike Christie55313e92019-08-13 11:39:49 -05002114 if (info->attrs[NBD_ATTR_TIMEOUT])
2115 nbd_set_cmd_timeout(nbd,
2116 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04002117 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
2118 config->dead_conn_timeout =
2119 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
2120 config->dead_conn_timeout *= HZ;
2121 }
Josef Bacika2c97902017-04-06 17:02:07 -04002122 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
2123 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
2124 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302125 if (!test_and_set_bit(NBD_RT_DESTROY_ON_DISCONNECT,
Josef Bacika2c97902017-04-06 17:02:07 -04002126 &config->runtime_flags))
2127 put_dev = true;
Xiubo Li8454d682019-09-17 17:26:06 +05302128 set_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04002129 } else {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302130 if (test_and_clear_bit(NBD_RT_DESTROY_ON_DISCONNECT,
Josef Bacika2c97902017-04-06 17:02:07 -04002131 &config->runtime_flags))
2132 refcount_inc(&nbd->refs);
Xiubo Li8454d682019-09-17 17:26:06 +05302133 clear_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
Josef Bacika2c97902017-04-06 17:02:07 -04002134 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002135
2136 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302137 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002138 &config->runtime_flags);
2139 } else {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302140 clear_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002141 &config->runtime_flags);
2142 }
Josef Bacika2c97902017-04-06 17:02:07 -04002143 }
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002144
2145 if (info->attrs[NBD_ATTR_SOCKETS]) {
2146 struct nlattr *attr;
2147 int rem, fd;
2148
2149 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2150 rem) {
2151 struct nlattr *socks[NBD_SOCK_MAX+1];
2152
2153 if (nla_type(attr) != NBD_SOCK_ITEM) {
2154 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
2155 ret = -EINVAL;
2156 goto out;
2157 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002158 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2159 attr,
2160 nbd_sock_policy,
2161 info->extack);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002162 if (ret != 0) {
2163 printk(KERN_ERR "nbd: error processing sock list\n");
2164 ret = -EINVAL;
2165 goto out;
2166 }
2167 if (!socks[NBD_SOCK_FD])
2168 continue;
2169 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2170 ret = nbd_reconnect_socket(nbd, fd);
2171 if (ret) {
2172 if (ret == -ENOSPC)
2173 ret = 0;
2174 goto out;
2175 }
2176 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
2177 }
2178 }
2179out:
2180 mutex_unlock(&nbd->config_lock);
2181 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002182 nbd_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04002183 if (put_dev)
2184 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002185 return ret;
2186}
2187
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002188static const struct genl_small_ops nbd_connect_genl_ops[] = {
Josef Bacike46c7282017-04-06 17:02:00 -04002189 {
2190 .cmd = NBD_CMD_CONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002191 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002192 .doit = nbd_genl_connect,
2193 },
2194 {
2195 .cmd = NBD_CMD_DISCONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002196 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002197 .doit = nbd_genl_disconnect,
2198 },
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002199 {
2200 .cmd = NBD_CMD_RECONFIGURE,
Johannes Bergef6243a2019-04-26 14:07:31 +02002201 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002202 .doit = nbd_genl_reconfigure,
2203 },
Josef Bacik47d902b2017-04-06 17:02:05 -04002204 {
2205 .cmd = NBD_CMD_STATUS,
Johannes Bergef6243a2019-04-26 14:07:31 +02002206 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacik47d902b2017-04-06 17:02:05 -04002207 .doit = nbd_genl_status,
2208 },
Josef Bacike46c7282017-04-06 17:02:00 -04002209};
2210
Josef Bacik799f9a32017-04-06 17:02:02 -04002211static const struct genl_multicast_group nbd_mcast_grps[] = {
2212 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2213};
2214
Josef Bacike46c7282017-04-06 17:02:00 -04002215static struct genl_family nbd_genl_family __ro_after_init = {
2216 .hdrsize = 0,
2217 .name = NBD_GENL_FAMILY_NAME,
2218 .version = NBD_GENL_VERSION,
2219 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002220 .small_ops = nbd_connect_genl_ops,
2221 .n_small_ops = ARRAY_SIZE(nbd_connect_genl_ops),
Josef Bacike46c7282017-04-06 17:02:00 -04002222 .maxattr = NBD_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01002223 .policy = nbd_attr_policy,
Josef Bacik799f9a32017-04-06 17:02:02 -04002224 .mcgrps = nbd_mcast_grps,
2225 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
Josef Bacike46c7282017-04-06 17:02:00 -04002226};
2227
Josef Bacik47d902b2017-04-06 17:02:05 -04002228static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2229{
2230 struct nlattr *dev_opt;
2231 u8 connected = 0;
2232 int ret;
2233
2234 /* This is a little racey, but for status it's ok. The
2235 * reason we don't take a ref here is because we can't
2236 * take a ref in the index == -1 case as we would need
2237 * to put under the nbd_index_mutex, which could
2238 * deadlock if we are configured to remove ourselves
2239 * once we're disconnected.
2240 */
2241 if (refcount_read(&nbd->config_refs))
2242 connected = 1;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002243 dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM);
Josef Bacik47d902b2017-04-06 17:02:05 -04002244 if (!dev_opt)
2245 return -EMSGSIZE;
2246 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2247 if (ret)
2248 return -EMSGSIZE;
2249 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2250 connected);
2251 if (ret)
2252 return -EMSGSIZE;
2253 nla_nest_end(reply, dev_opt);
2254 return 0;
2255}
2256
2257static int status_cb(int id, void *ptr, void *data)
2258{
2259 struct nbd_device *nbd = ptr;
2260 return populate_nbd_status(nbd, (struct sk_buff *)data);
2261}
2262
2263static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2264{
2265 struct nlattr *dev_list;
2266 struct sk_buff *reply;
2267 void *reply_head;
2268 size_t msg_size;
2269 int index = -1;
2270 int ret = -ENOMEM;
2271
2272 if (info->attrs[NBD_ATTR_INDEX])
2273 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2274
2275 mutex_lock(&nbd_index_mutex);
2276
2277 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2278 nla_attr_size(sizeof(u8)));
2279 msg_size *= (index == -1) ? nbd_total_devices : 1;
2280
2281 reply = genlmsg_new(msg_size, GFP_KERNEL);
2282 if (!reply)
2283 goto out;
2284 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2285 NBD_CMD_STATUS);
2286 if (!reply_head) {
2287 nlmsg_free(reply);
2288 goto out;
2289 }
2290
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002291 dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST);
Josef Bacik47d902b2017-04-06 17:02:05 -04002292 if (index == -1) {
2293 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2294 if (ret) {
2295 nlmsg_free(reply);
2296 goto out;
2297 }
2298 } else {
2299 struct nbd_device *nbd;
2300 nbd = idr_find(&nbd_index_idr, index);
2301 if (nbd) {
2302 ret = populate_nbd_status(nbd, reply);
2303 if (ret) {
2304 nlmsg_free(reply);
2305 goto out;
2306 }
2307 }
2308 }
2309 nla_nest_end(reply, dev_list);
2310 genlmsg_end(reply, reply_head);
Li RongQingcd46eb82019-02-19 13:14:07 +08002311 ret = genlmsg_reply(reply, info);
Josef Bacik47d902b2017-04-06 17:02:05 -04002312out:
2313 mutex_unlock(&nbd_index_mutex);
2314 return ret;
2315}
2316
Josef Bacike46c7282017-04-06 17:02:00 -04002317static void nbd_connect_reply(struct genl_info *info, int index)
2318{
2319 struct sk_buff *skb;
2320 void *msg_head;
2321 int ret;
2322
2323 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2324 if (!skb)
2325 return;
2326 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2327 NBD_CMD_CONNECT);
2328 if (!msg_head) {
2329 nlmsg_free(skb);
2330 return;
2331 }
2332 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2333 if (ret) {
2334 nlmsg_free(skb);
2335 return;
2336 }
2337 genlmsg_end(skb, msg_head);
2338 genlmsg_reply(skb, info);
2339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Josef Bacik799f9a32017-04-06 17:02:02 -04002341static void nbd_mcast_index(int index)
2342{
2343 struct sk_buff *skb;
2344 void *msg_head;
2345 int ret;
2346
2347 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2348 if (!skb)
2349 return;
2350 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2351 NBD_CMD_LINK_DEAD);
2352 if (!msg_head) {
2353 nlmsg_free(skb);
2354 return;
2355 }
2356 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2357 if (ret) {
2358 nlmsg_free(skb);
2359 return;
2360 }
2361 genlmsg_end(skb, msg_head);
2362 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2363}
2364
2365static void nbd_dead_link_work(struct work_struct *work)
2366{
2367 struct link_dead_args *args = container_of(work, struct link_dead_args,
2368 work);
2369 nbd_mcast_index(args->index);
2370 kfree(args);
2371}
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373static int __init nbd_init(void)
2374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int i;
2376
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08002377 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002379 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02002380 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002381 return -EINVAL;
2382 }
2383
2384 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02002385 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002386 part_shift = fls(max_part);
2387
Namhyung Kim5988ce22011-05-28 14:44:46 +02002388 /*
2389 * Adjust max_part according to part_shift as it is exported
2390 * to user space so that user can know the max number of
2391 * partition kernel should be able to manage.
2392 *
2393 * Note that -1 is required because partition 0 is reserved
2394 * for the whole disk.
2395 */
2396 max_part = (1UL << part_shift) - 1;
2397 }
2398
Namhyung Kim3b271082011-05-28 14:44:46 +02002399 if ((1UL << part_shift) > DISK_MAX_PARTS)
2400 return -EINVAL;
2401
2402 if (nbds_max > 1UL << (MINORBITS - part_shift))
2403 return -EINVAL;
2404
Mike Christiee9e006f2019-08-04 14:10:06 -05002405 if (register_blkdev(NBD_MAJOR, "nbd"))
Josef Bacikb0d91112017-02-01 16:11:40 -05002406 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Josef Bacike46c7282017-04-06 17:02:00 -04002408 if (genl_register_family(&nbd_genl_family)) {
2409 unregister_blkdev(NBD_MAJOR, "nbd");
Josef Bacike46c7282017-04-06 17:02:00 -04002410 return -EINVAL;
2411 }
Markus Pargmann30d53d92015-08-17 08:20:06 +02002412 nbd_dbg_init();
2413
Josef Bacikb0d91112017-02-01 16:11:40 -05002414 mutex_lock(&nbd_index_mutex);
2415 for (i = 0; i < nbds_max; i++)
2416 nbd_dev_add(i);
2417 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05002419}
2420
2421static int nbd_exit_cb(int id, void *ptr, void *data)
2422{
Josef Bacikc6a47592017-04-06 17:02:06 -04002423 struct list_head *list = (struct list_head *)data;
Josef Bacikb0d91112017-02-01 16:11:40 -05002424 struct nbd_device *nbd = ptr;
Josef Bacikc6a47592017-04-06 17:02:06 -04002425
Josef Bacikc6a47592017-04-06 17:02:06 -04002426 list_add_tail(&nbd->list, list);
Josef Bacikb0d91112017-02-01 16:11:40 -05002427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
2430static void __exit nbd_cleanup(void)
2431{
Josef Bacikc6a47592017-04-06 17:02:06 -04002432 struct nbd_device *nbd;
2433 LIST_HEAD(del_list);
2434
Markus Pargmann30d53d92015-08-17 08:20:06 +02002435 nbd_dbg_close();
2436
Josef Bacikc6a47592017-04-06 17:02:06 -04002437 mutex_lock(&nbd_index_mutex);
2438 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2439 mutex_unlock(&nbd_index_mutex);
2440
Josef Bacik60ae36a2017-04-28 09:49:19 -04002441 while (!list_empty(&del_list)) {
2442 nbd = list_first_entry(&del_list, struct nbd_device, list);
2443 list_del_init(&nbd->list);
2444 if (refcount_read(&nbd->refs) != 1)
Josef Bacikc6a47592017-04-06 17:02:06 -04002445 printk(KERN_ERR "nbd: possibly leaking a device\n");
2446 nbd_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002447 }
2448
Josef Bacikb0d91112017-02-01 16:11:40 -05002449 idr_destroy(&nbd_index_idr);
Josef Bacike46c7282017-04-06 17:02:00 -04002450 genl_unregister_family(&nbd_genl_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
2454module_init(nbd_init);
2455module_exit(nbd_cleanup);
2456
2457MODULE_DESCRIPTION("Network Block Device");
2458MODULE_LICENSE("GPL");
2459
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07002460module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002461MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2462module_param(max_part, int, 0444);
Josef Bacik7a8362a2017-08-14 18:56:16 +00002463MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");