blob: 59c452fff83526842a1f260d2423d7e71b44206a [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
Josef Bacikdc2b7762021-02-22 15:09:53 -050081#define NBD_RT_DISCONNECT_ON_CLOSE 6
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070082
Xiubo Li8454d682019-09-17 17:26:06 +053083#define NBD_DESTROY_ON_DISCONNECT 0
84#define NBD_DISCONNECT_REQUESTED 1
85
Josef Bacik5ea8d102017-04-06 17:01:58 -040086struct nbd_config {
Markus Pargmann22d109c2015-08-17 08:20:09 +020087 u32 flags;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070088 unsigned long runtime_flags;
Josef Bacik560bc4b2017-04-06 17:02:04 -040089 u64 dead_conn_timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -040090
Josef Bacik9561a7a2016-11-22 14:04:40 -050091 struct nbd_sock **socks;
Josef Bacik9561a7a2016-11-22 14:04:40 -050092 int num_connections;
Josef Bacik560bc4b2017-04-06 17:02:04 -040093 atomic_t live_connections;
94 wait_queue_head_t conn_wait;
Josef Bacik5ea8d102017-04-06 17:01:58 -040095
Josef Bacik9561a7a2016-11-22 14:04:40 -050096 atomic_t recv_threads;
97 wait_queue_head_t recv_wq;
Josef Bacikef77b512016-12-02 16:19:12 -050098 loff_t blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020099 loff_t bytesize;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200100#if IS_ENABLED(CONFIG_DEBUG_FS)
101 struct dentry *dbg_dir;
102#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +0200103};
104
Josef Bacik5ea8d102017-04-06 17:01:58 -0400105struct nbd_device {
106 struct blk_mq_tag_set tag_set;
107
Josef Bacike46c7282017-04-06 17:02:00 -0400108 int index;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400109 refcount_t config_refs;
Josef Bacikc6a47592017-04-06 17:02:06 -0400110 refcount_t refs;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400111 struct nbd_config *config;
112 struct mutex config_lock;
113 struct gendisk *disk;
Mike Christiee9e006f2019-08-04 14:10:06 -0500114 struct workqueue_struct *recv_workq;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400115
Josef Bacikc6a47592017-04-06 17:02:06 -0400116 struct list_head list;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400117 struct task_struct *task_recv;
118 struct task_struct *task_setup;
Xiubo Li8454d682019-09-17 17:26:06 +0530119
120 struct completion *destroy_complete;
121 unsigned long flags;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400122};
123
Josef Bacikd7d94d42018-07-16 12:11:34 -0400124#define NBD_CMD_REQUEUED 1
125
Josef Bacikfd8383f2016-09-08 12:33:37 -0700126struct nbd_cmd {
127 struct nbd_device *nbd;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400128 struct mutex lock;
Josef Bacikf3733242017-04-06 17:01:57 -0400129 int index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400130 int cookie;
Mike Christie2da22da2019-08-13 11:39:52 -0500131 int retries;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200132 blk_status_t status;
Josef Bacikd7d94d42018-07-16 12:11:34 -0400133 unsigned long flags;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400134 u32 cmd_cookie;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700135};
136
Markus Pargmann30d53d92015-08-17 08:20:06 +0200137#if IS_ENABLED(CONFIG_DEBUG_FS)
138static struct dentry *nbd_dbg_dir;
139#endif
140
141#define nbd_name(nbd) ((nbd)->disk->disk_name)
142
Wanlong Gaof4507162012-03-28 14:42:51 -0700143#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Xiubo Li553768d2019-05-29 15:16:05 -0500145#define NBD_DEF_BLKSIZE 1024
146
Ingo van Lil9c7a4162006-07-01 04:36:36 -0700147static unsigned int nbds_max = 16;
Josef Bacik7a8362a2017-08-14 18:56:16 +0000148static int max_part = 16;
Josef Bacikb0d91112017-02-01 16:11:40 -0500149static int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Josef Bacik9442b732017-02-07 17:10:22 -0500151static int nbd_dev_dbg_init(struct nbd_device *nbd);
152static void nbd_dev_dbg_close(struct nbd_device *nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400153static void nbd_config_put(struct nbd_device *nbd);
Josef Bacike46c7282017-04-06 17:02:00 -0400154static void nbd_connect_reply(struct genl_info *info, int index);
Josef Bacik47d902b2017-04-06 17:02:05 -0400155static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
Josef Bacik799f9a32017-04-06 17:02:02 -0400156static void nbd_dead_link_work(struct work_struct *work);
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -0700157static void nbd_disconnect_and_put(struct nbd_device *nbd);
Josef Bacik9442b732017-02-07 17:10:22 -0500158
Markus Pargmannd18509f2015-04-02 10:11:38 +0200159static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Markus Pargmannd18509f2015-04-02 10:11:38 +0200161 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Josef Bacikd7d94d42018-07-16 12:11:34 -0400164static void nbd_requeue_cmd(struct nbd_cmd *cmd)
165{
166 struct request *req = blk_mq_rq_from_pdu(cmd);
167
168 if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
169 blk_mq_requeue_request(req, true);
170}
171
Josef Bacik8f3ea352018-07-16 12:11:35 -0400172#define NBD_COOKIE_BITS 32
173
174static u64 nbd_cmd_handle(struct nbd_cmd *cmd)
175{
176 struct request *req = blk_mq_rq_from_pdu(cmd);
177 u32 tag = blk_mq_unique_tag(req);
178 u64 cookie = cmd->cmd_cookie;
179
180 return (cookie << NBD_COOKIE_BITS) | tag;
181}
182
183static u32 nbd_handle_to_tag(u64 handle)
184{
185 return (u32)handle;
186}
187
188static u32 nbd_handle_to_cookie(u64 handle)
189{
190 return (u32)(handle >> NBD_COOKIE_BITS);
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static const char *nbdcmd_to_ascii(int cmd)
194{
195 switch (cmd) {
196 case NBD_CMD_READ: return "read";
197 case NBD_CMD_WRITE: return "write";
198 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800199 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700200 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 return "invalid";
203}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Josef Bacik5ea8d102017-04-06 17:01:58 -0400205static ssize_t pid_show(struct device *dev,
206 struct device_attribute *attr, char *buf)
207{
208 struct gendisk *disk = dev_to_disk(dev);
209 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
210
211 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
212}
213
Bhumika Goyaldfbde552017-08-21 17:13:08 +0530214static const struct device_attribute pid_attr = {
Joe Perches5657a812018-05-24 13:38:59 -0600215 .attr = { .name = "pid", .mode = 0444},
Josef Bacik5ea8d102017-04-06 17:01:58 -0400216 .show = pid_show,
217};
218
Josef Bacikc6a47592017-04-06 17:02:06 -0400219static void nbd_dev_remove(struct nbd_device *nbd)
220{
221 struct gendisk *disk = nbd->disk;
Josef Bacik8364da42018-05-16 14:51:17 -0400222 struct request_queue *q;
223
Josef Bacikc6a47592017-04-06 17:02:06 -0400224 if (disk) {
Josef Bacik8364da42018-05-16 14:51:17 -0400225 q = disk->queue;
Josef Bacikc6a47592017-04-06 17:02:06 -0400226 del_gendisk(disk);
Josef Bacik8364da42018-05-16 14:51:17 -0400227 blk_cleanup_queue(q);
Josef Bacikc6a47592017-04-06 17:02:06 -0400228 blk_mq_free_tag_set(&nbd->tag_set);
Josef Bacika2c97902017-04-06 17:02:07 -0400229 disk->private_data = NULL;
Josef Bacikc6a47592017-04-06 17:02:06 -0400230 put_disk(disk);
231 }
Xiubo Li8454d682019-09-17 17:26:06 +0530232
233 /*
234 * Place this in the last just before the nbd is freed to
235 * make sure that the disk and the related kobject are also
236 * totally removed to avoid duplicate creation of the same
237 * one.
238 */
239 if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) && nbd->destroy_complete)
240 complete(nbd->destroy_complete);
241
Josef Bacikc6a47592017-04-06 17:02:06 -0400242 kfree(nbd);
243}
244
245static void nbd_put(struct nbd_device *nbd)
246{
247 if (refcount_dec_and_mutex_lock(&nbd->refs,
248 &nbd_index_mutex)) {
249 idr_remove(&nbd_index_idr, nbd->index);
Josef Bacikc6a47592017-04-06 17:02:06 -0400250 nbd_dev_remove(nbd);
Xiubo Li86248812019-09-19 11:44:27 +0530251 mutex_unlock(&nbd_index_mutex);
Josef Bacikc6a47592017-04-06 17:02:06 -0400252 }
253}
254
Josef Bacik799f9a32017-04-06 17:02:02 -0400255static int nbd_disconnected(struct nbd_config *config)
Josef Bacikf3733242017-04-06 17:01:57 -0400256{
Xiubo Liec76a7b2019-09-17 17:26:05 +0530257 return test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags) ||
258 test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
Josef Bacik799f9a32017-04-06 17:02:02 -0400259}
260
261static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
262 int notify)
263{
264 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
265 struct link_dead_args *args;
266 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
267 if (args) {
268 INIT_WORK(&args->work, nbd_dead_link_work);
269 args->index = nbd->index;
270 queue_work(system_wq, &args->work);
271 }
272 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400273 if (!nsock->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400274 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600275 if (atomic_dec_return(&nbd->config->live_connections) == 0) {
Xiubo Liec76a7b2019-09-17 17:26:05 +0530276 if (test_and_clear_bit(NBD_RT_DISCONNECT_REQUESTED,
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600277 &nbd->config->runtime_flags)) {
Xiubo Liec76a7b2019-09-17 17:26:05 +0530278 set_bit(NBD_RT_DISCONNECTED,
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600279 &nbd->config->runtime_flags);
280 dev_info(nbd_to_dev(nbd),
281 "Disconnected due to user request.\n");
282 }
283 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400284 }
Josef Bacikf3733242017-04-06 17:01:57 -0400285 nsock->dead = true;
286 nsock->pending = NULL;
287 nsock->sent = 0;
288}
289
Josef Bacik29eaadc2017-04-06 17:01:59 -0400290static void nbd_size_clear(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200291{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400292 if (nbd->config->bytesize) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400293 set_capacity(nbd->disk, 0);
294 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
295 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200296}
297
Ming Leib40813d2020-10-28 15:24:34 +0800298static void nbd_size_update(struct nbd_device *nbd, bool start)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200299{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400300 struct nbd_config *config = nbd->config;
Josef Bacik9e2b19672018-05-16 14:51:19 -0400301 struct block_device *bdev = bdget_disk(nbd->disk, 0);
Christoph Hellwig611bee52020-08-23 11:10:41 +0200302 sector_t nr_sectors = config->bytesize >> 9;
Josef Bacik9e2b19672018-05-16 14:51:19 -0400303
Josef Bacik6df133a2018-05-23 13:35:59 -0400304 if (config->flags & NBD_FLAG_SEND_TRIM) {
305 nbd->disk->queue->limits.discard_granularity = config->blksize;
Josef Bacik07ce2132018-06-05 11:41:23 -0400306 nbd->disk->queue->limits.discard_alignment = config->blksize;
Josef Bacik6df133a2018-05-23 13:35:59 -0400307 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
308 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400309 blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
310 blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
Christoph Hellwig611bee52020-08-23 11:10:41 +0200311 set_capacity(nbd->disk, nr_sectors);
Josef Bacik9e2b19672018-05-16 14:51:19 -0400312 if (bdev) {
Jan Karac8a83a62019-01-14 09:48:09 +0100313 if (bdev->bd_disk) {
Christoph Hellwig611bee52020-08-23 11:10:41 +0200314 bd_set_nr_sectors(bdev, nr_sectors);
Ming Leib40813d2020-10-28 15:24:34 +0800315 if (start)
316 set_blocksize(bdev, config->blksize);
Jan Karac8a83a62019-01-14 09:48:09 +0100317 } else
Christoph Hellwig38430f02020-09-21 09:19:45 +0200318 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
Josef Bacik9e2b19672018-05-16 14:51:19 -0400319 bdput(bdev);
320 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200321 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
322}
323
Josef Bacik29eaadc2017-04-06 17:01:59 -0400324static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
325 loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200326{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400327 struct nbd_config *config = nbd->config;
328 config->blksize = blocksize;
329 config->bytesize = blocksize * nr_blocks;
Josef Bacikc3f7c932018-05-16 14:51:18 -0400330 if (nbd->task_recv != NULL)
Ming Leib40813d2020-10-28 15:24:34 +0800331 nbd_size_update(nbd, false);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200332}
333
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200334static void nbd_complete_rq(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200336 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Kevin Vigoree57a052018-06-04 10:40:12 -0600338 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200339 cmd->status ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200341 blk_mq_end_request(req, cmd->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Markus Pargmanne018e752015-04-02 10:11:39 +0200344/*
345 * Forcibly shutdown the socket causing all listeners to error
346 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200347static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700348{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400349 struct nbd_config *config = nbd->config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500350 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700351
Josef Bacik5ea8d102017-04-06 17:01:58 -0400352 if (config->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200353 return;
Xiubo Liec76a7b2019-09-17 17:26:05 +0530354 if (test_and_set_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500355 return;
356
Josef Bacik5ea8d102017-04-06 17:01:58 -0400357 for (i = 0; i < config->num_connections; i++) {
358 struct nbd_sock *nsock = config->socks[i];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500359 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400360 nbd_mark_nsock_dead(nbd, nsock, 0);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500361 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100362 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500363 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700364}
365
Mike Christie00514672019-08-13 11:39:50 -0500366static u32 req_to_nbd_cmd_type(struct request *req)
367{
368 switch (req_op(req)) {
369 case REQ_OP_DISCARD:
370 return NBD_CMD_TRIM;
371 case REQ_OP_FLUSH:
372 return NBD_CMD_FLUSH;
373 case REQ_OP_WRITE:
374 return NBD_CMD_WRITE;
375 case REQ_OP_READ:
376 return NBD_CMD_READ;
377 default:
378 return U32_MAX;
379 }
380}
381
Josef Bacik0eadf372016-09-08 12:33:40 -0700382static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
383 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700384{
Josef Bacik0eadf372016-09-08 12:33:40 -0700385 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
386 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400387 struct nbd_config *config;
Paul Clements7fdfd402007-10-16 23:27:37 -0700388
Josef Bacikde6346e2019-10-21 15:56:27 -0400389 if (!mutex_trylock(&cmd->lock))
390 return BLK_EH_RESET_TIMER;
391
Josef Bacik5ea8d102017-04-06 17:01:58 -0400392 if (!refcount_inc_not_zero(&nbd->config_refs)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200393 cmd->status = BLK_STS_TIMEOUT;
Josef Bacikde6346e2019-10-21 15:56:27 -0400394 mutex_unlock(&cmd->lock);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200395 goto done;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400396 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400397 config = nbd->config;
398
Hou Pud9709582020-02-28 01:40:29 -0500399 if (config->num_connections > 1 ||
400 (config->num_connections == 1 && nbd->tag_set.timeout)) {
Josef Bacikf3733242017-04-06 17:01:57 -0400401 dev_err_ratelimited(nbd_to_dev(nbd),
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600402 "Connection timed out, retrying (%d/%d alive)\n",
403 atomic_read(&config->live_connections),
404 config->num_connections);
Josef Bacikf3733242017-04-06 17:01:57 -0400405 /*
406 * Hooray we have more connections, requeue this IO, the submit
Hou Pud9709582020-02-28 01:40:29 -0500407 * path will put it on a real connection. Or if only one
408 * connection is configured, the submit path will wait util
409 * a new connection is reconfigured or util dead timeout.
Josef Bacikf3733242017-04-06 17:01:57 -0400410 */
Hou Pud9709582020-02-28 01:40:29 -0500411 if (config->socks) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400412 if (cmd->index < config->num_connections) {
Josef Bacikf3733242017-04-06 17:01:57 -0400413 struct nbd_sock *nsock =
Josef Bacik5ea8d102017-04-06 17:01:58 -0400414 config->socks[cmd->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400415 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400416 /* We can have multiple outstanding requests, so
417 * we don't want to mark the nsock dead if we've
418 * already reconnected with a new socket, so
419 * only mark it dead if its the same socket we
420 * were sent out on.
421 */
422 if (cmd->cookie == nsock->cookie)
423 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400424 mutex_unlock(&nsock->tx_lock);
425 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400426 mutex_unlock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400427 nbd_requeue_cmd(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400428 nbd_config_put(nbd);
Christoph Hellwig66005932018-05-29 15:52:29 +0200429 return BLK_EH_DONE;
Josef Bacikf3733242017-04-06 17:01:57 -0400430 }
Josef Bacikf3733242017-04-06 17:01:57 -0400431 }
Mike Christie2da22da2019-08-13 11:39:52 -0500432
433 if (!nbd->tag_set.timeout) {
434 /*
435 * Userspace sets timeout=0 to disable socket disconnection,
436 * so just warn and reset the timer.
437 */
Hou Pu2c272542020-02-28 01:40:30 -0500438 struct nbd_sock *nsock = config->socks[cmd->index];
Mike Christie2da22da2019-08-13 11:39:52 -0500439 cmd->retries++;
440 dev_info(nbd_to_dev(nbd), "Possible stuck request %p: control (%s@%llu,%uB). Runtime %u seconds\n",
441 req, nbdcmd_to_ascii(req_to_nbd_cmd_type(req)),
442 (unsigned long long)blk_rq_pos(req) << 9,
443 blk_rq_bytes(req), (req->timeout / HZ) * cmd->retries);
444
Hou Pu2c272542020-02-28 01:40:30 -0500445 mutex_lock(&nsock->tx_lock);
446 if (cmd->cookie != nsock->cookie) {
447 nbd_requeue_cmd(cmd);
448 mutex_unlock(&nsock->tx_lock);
449 mutex_unlock(&cmd->lock);
450 nbd_config_put(nbd);
451 return BLK_EH_DONE;
452 }
453 mutex_unlock(&nsock->tx_lock);
Mike Christie2da22da2019-08-13 11:39:52 -0500454 mutex_unlock(&cmd->lock);
455 nbd_config_put(nbd);
456 return BLK_EH_RESET_TIMER;
457 }
458
459 dev_err_ratelimited(nbd_to_dev(nbd), "Connection timed out\n");
Xiubo Liec76a7b2019-09-17 17:26:05 +0530460 set_bit(NBD_RT_TIMEDOUT, &config->runtime_flags);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200461 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400462 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500463 sock_shutdown(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400464 nbd_config_put(nbd);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200465done:
466 blk_mq_complete_request(req);
467 return BLK_EH_DONE;
Paul Clements7fdfd402007-10-16 23:27:37 -0700468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
471 * Send or receive packet.
472 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500473static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400474 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400476 struct nbd_config *config = nbd->config;
477 struct socket *sock = config->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int result;
479 struct msghdr msg;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700480 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700482 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500483 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200484 "Attempted %s on closed socket in sock_xmit\n",
485 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700486 return -EINVAL;
487 }
488
Al Viroc9f2b6a2015-11-12 05:09:35 -0500489 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500490
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700491 noreclaim_flag = memalloc_noreclaim_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700493 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 msg.msg_name = NULL;
495 msg.msg_namelen = 0;
496 msg.msg_control = NULL;
497 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
499
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200500 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500501 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200502 else
Al Viroc1696ca2015-11-12 04:51:19 -0500503 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (result <= 0) {
506 if (result == 0)
507 result = -EPIPE; /* short read */
508 break;
509 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400510 if (sent)
511 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500512 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700514 memalloc_noreclaim_restore(noreclaim_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return result;
517}
518
Josef Bacik32e67a32017-10-24 15:57:18 -0400519/*
520 * Different settings for sk->sk_sndtimeo can result in different return values
521 * if there is a signal pending when we enter sendmsg, because reasons?
522 */
523static inline int was_interrupted(int result)
524{
525 return result == -ERESTARTSYS || result == -EINTR;
526}
527
Paul Clements7fdfd402007-10-16 23:27:37 -0700528/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500529static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700531 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400532 struct nbd_config *config = nbd->config;
533 struct nbd_sock *nsock = config->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500534 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500535 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
536 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
537 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900538 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700539 struct bio *bio;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400540 u64 handle;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200541 u32 type;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400542 u32 nbd_cmd_flags = 0;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400543 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200544
David Howellsaa563d72018-10-20 00:57:56 +0100545 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500546
Mike Christie00514672019-08-13 11:39:50 -0500547 type = req_to_nbd_cmd_type(req);
548 if (type == U32_MAX)
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100549 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100551 if (rq_data_dir(req) == WRITE &&
Josef Bacik5ea8d102017-04-06 17:01:58 -0400552 (config->flags & NBD_FLAG_READ_ONLY)) {
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100553 dev_err_ratelimited(disk_to_dev(nbd->disk),
554 "Write on read-only\n");
555 return -EIO;
556 }
557
Shaun McDowell685c9b22017-05-25 23:55:54 -0400558 if (req->cmd_flags & REQ_FUA)
559 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
560
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400561 /* We did a partial send previously, and we at least sent the whole
562 * request struct, so just go and send the rest of the pages in the
563 * request.
564 */
565 if (sent) {
566 if (sent >= sizeof(request)) {
567 skip = sent - sizeof(request);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700568
569 /* initialize handle for tracing purposes */
570 handle = nbd_cmd_handle(cmd);
571
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400572 goto send_pages;
573 }
574 iov_iter_advance(&from, sent);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400575 } else {
576 cmd->cmd_cookie++;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400577 }
Josef Bacikf3733242017-04-06 17:01:57 -0400578 cmd->index = index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400579 cmd->cookie = nsock->cookie;
Mike Christie2da22da2019-08-13 11:39:52 -0500580 cmd->retries = 0;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400581 request.type = htonl(type | nbd_cmd_flags);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500582 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800583 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
584 request.len = htonl(size);
585 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400586 handle = nbd_cmd_handle(cmd);
587 memcpy(request.handle, &handle, sizeof(handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Matt Mullinsea106722019-04-26 11:49:48 -0700589 trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
590
Markus Pargmannd18509f2015-04-02 10:11:38 +0200591 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600592 req, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200593 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500594 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400595 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Andrew Hall2abd2de2019-04-26 11:49:49 -0700596 trace_nbd_header_sent(req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400598 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400599 /* If we havne't sent anything we can just return BUSY,
600 * however if we have sent something we need to make
601 * sure we only allow this req to be sent until we are
602 * completely done.
603 */
604 if (sent) {
605 nsock->pending = req;
606 nsock->sent = sent;
607 }
Josef Bacikd7d94d42018-07-16 12:11:34 -0400608 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200609 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400610 }
Josef Bacika897b662016-12-05 16:20:29 -0500611 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200612 "Send control failed (result %d)\n", result);
Josef Bacikf3733242017-04-06 17:01:57 -0400613 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400615send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700616 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400617 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700618
Jens Axboe429a7872016-11-17 12:30:37 -0700619 bio = req->bio;
620 while (bio) {
621 struct bio *next = bio->bi_next;
622 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800623 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700624
625 bio_for_each_segment(bvec, bio, iter) {
626 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500627 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700628
Markus Pargmannd18509f2015-04-02 10:11:38 +0200629 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600630 req, bvec.bv_len);
David Howellsaa563d72018-10-20 00:57:56 +0100631 iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400632 if (skip) {
633 if (skip >= iov_iter_count(&from)) {
634 skip -= iov_iter_count(&from);
635 continue;
636 }
637 iov_iter_advance(&from, skip);
638 skip = 0;
639 }
640 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200641 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400642 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400643 /* We've already sent the header, we
644 * have no choice but to set pending and
645 * return BUSY.
646 */
647 nsock->pending = req;
648 nsock->sent = sent;
Josef Bacikd7d94d42018-07-16 12:11:34 -0400649 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200650 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400651 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700652 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200653 "Send data failed (result %d)\n",
654 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400655 return -EAGAIN;
Jens Axboe6c92e692007-08-16 13:43:12 +0200656 }
Jens Axboe429a7872016-11-17 12:30:37 -0700657 /*
658 * The completion might already have come in,
659 * so break for the last one instead of letting
660 * the iterator do it. This prevents use-after-free
661 * of the bio.
662 */
663 if (is_last)
664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Jens Axboe429a7872016-11-17 12:30:37 -0700666 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400668out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700669 trace_nbd_payload_sent(req, handle);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400670 nsock->pending = NULL;
671 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500676static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400678 struct nbd_config *config = nbd->config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int result;
680 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700681 struct nbd_cmd *cmd;
682 struct request *req = NULL;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400683 u64 handle;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700684 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500685 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500686 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
687 struct iov_iter to;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400688 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 reply.magic = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100691 iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400692 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (result <= 0) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400694 if (!nbd_disconnected(config))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500695 dev_err(disk_to_dev(nbd->disk),
696 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200697 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700699
700 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700701 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700702 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200703 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700704 }
705
Josef Bacik8f3ea352018-07-16 12:11:35 -0400706 memcpy(&handle, reply.handle, sizeof(handle));
707 tag = nbd_handle_to_tag(handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700708 hwq = blk_mq_unique_tag_to_hwq(tag);
709 if (hwq < nbd->tag_set.nr_hw_queues)
710 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
711 blk_mq_unique_tag_to_tag(tag));
712 if (!req || !blk_mq_request_started(req)) {
713 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
714 tag, req);
715 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Andrew Hall2abd2de2019-04-26 11:49:49 -0700717 trace_nbd_header_received(req, handle);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700718 cmd = blk_mq_rq_to_pdu(req);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400719
720 mutex_lock(&cmd->lock);
721 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
722 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
723 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
724 ret = -ENOENT;
725 goto out;
726 }
Josef Bacik7ce23e82019-10-21 15:56:28 -0400727 if (cmd->status != BLK_STS_OK) {
728 dev_err(disk_to_dev(nbd->disk), "Command already handled %p\n",
729 req);
730 ret = -ENOENT;
731 goto out;
732 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400733 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
734 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
735 req);
736 ret = -ENOENT;
737 goto out;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700740 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200741 ntohl(reply.error));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200742 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Kevin Vigoree57a052018-06-04 10:40:12 -0600746 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200747 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200748 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800749 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200750
751 rq_for_each_segment(bvec, req, iter) {
David Howellsaa563d72018-10-20 00:57:56 +0100752 iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400753 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200754 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700755 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200756 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400757 /*
Hou Pud9709582020-02-28 01:40:29 -0500758 * If we've disconnected, we need to make sure we
Josef Bacikf3733242017-04-06 17:01:57 -0400759 * complete this request, otherwise error out
760 * and let the timeout stuff handle resubmitting
761 * this request onto another connection.
762 */
Hou Pud9709582020-02-28 01:40:29 -0500763 if (nbd_disconnected(config)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200764 cmd->status = BLK_STS_IOERR;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400765 goto out;
Josef Bacikf3733242017-04-06 17:01:57 -0400766 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400767 ret = -EIO;
768 goto out;
Jens Axboe6c92e692007-08-16 13:43:12 +0200769 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200770 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Kevin Vigoree57a052018-06-04 10:40:12 -0600771 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 }
Josef Bacik8f3ea352018-07-16 12:11:35 -0400774out:
Andrew Hall2abd2de2019-04-26 11:49:49 -0700775 trace_nbd_payload_received(req, handle);
Josef Bacik8f3ea352018-07-16 12:11:35 -0400776 mutex_unlock(&cmd->lock);
777 return ret ? ERR_PTR(ret) : cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Josef Bacik9561a7a2016-11-22 14:04:40 -0500780static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500782 struct recv_thread_args *args = container_of(work,
783 struct recv_thread_args,
784 work);
785 struct nbd_device *nbd = args->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400786 struct nbd_config *config = nbd->config;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700787 struct nbd_cmd *cmd;
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200788 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Markus Pargmann19391832015-08-17 08:20:03 +0200790 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500791 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700792 if (IS_ERR(cmd)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400793 struct nbd_sock *nsock = config->socks[args->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400794
795 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400796 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400797 mutex_unlock(&nsock->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200798 break;
799 }
800
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200801 rq = blk_mq_rq_from_pdu(cmd);
802 if (likely(!blk_should_fake_timeout(rq->q)))
803 blk_mq_complete_request(rq);
Markus Pargmann19391832015-08-17 08:20:03 +0200804 }
Xiubo Li87aac3a2020-10-13 22:45:14 -0400805 nbd_config_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400806 atomic_dec(&config->recv_threads);
807 wake_up(&config->recv_wq);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400808 kfree(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Jens Axboe7baa8572018-11-08 10:24:07 -0700811static bool nbd_clear_req(struct request *req, void *data, bool reserved)
Josef Bacikfd8383f2016-09-08 12:33:37 -0700812{
Christoph Hellwigd250bf42018-05-30 18:51:00 +0200813 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700814
Xie Yongjie0ee8d92021-08-13 23:13:30 +0800815 /* don't abort one completed request */
816 if (blk_mq_request_completed(req))
817 return true;
818
Josef Bacikde6346e2019-10-21 15:56:27 -0400819 mutex_lock(&cmd->lock);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200820 cmd->status = BLK_STS_IOERR;
Josef Bacikde6346e2019-10-21 15:56:27 -0400821 mutex_unlock(&cmd->lock);
822
Christoph Hellwig08e00292017-04-20 16:03:09 +0200823 blk_mq_complete_request(req);
Jens Axboe7baa8572018-11-08 10:24:07 -0700824 return true;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700825}
826
Wanlong Gaof4507162012-03-28 14:42:51 -0700827static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300829 blk_mq_quiesce_queue(nbd->disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700830 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300831 blk_mq_unquiesce_queue(nbd->disk->queue);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200832 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Josef Bacikf3733242017-04-06 17:01:57 -0400835static int find_fallback(struct nbd_device *nbd, int index)
836{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400837 struct nbd_config *config = nbd->config;
Josef Bacikf3733242017-04-06 17:01:57 -0400838 int new_index = -1;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400839 struct nbd_sock *nsock = config->socks[index];
Josef Bacikf3733242017-04-06 17:01:57 -0400840 int fallback = nsock->fallback_index;
841
Xiubo Liec76a7b2019-09-17 17:26:05 +0530842 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacikf3733242017-04-06 17:01:57 -0400843 return new_index;
844
Josef Bacik5ea8d102017-04-06 17:01:58 -0400845 if (config->num_connections <= 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400846 dev_err_ratelimited(disk_to_dev(nbd->disk),
Hou Pud9709582020-02-28 01:40:29 -0500847 "Dead connection, failed to find a fallback\n");
Josef Bacikf3733242017-04-06 17:01:57 -0400848 return new_index;
849 }
850
Josef Bacik5ea8d102017-04-06 17:01:58 -0400851 if (fallback >= 0 && fallback < config->num_connections &&
852 !config->socks[fallback]->dead)
Josef Bacikf3733242017-04-06 17:01:57 -0400853 return fallback;
854
855 if (nsock->fallback_index < 0 ||
Josef Bacik5ea8d102017-04-06 17:01:58 -0400856 nsock->fallback_index >= config->num_connections ||
857 config->socks[nsock->fallback_index]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400858 int i;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400859 for (i = 0; i < config->num_connections; i++) {
Josef Bacikf3733242017-04-06 17:01:57 -0400860 if (i == index)
861 continue;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400862 if (!config->socks[i]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400863 new_index = i;
864 break;
865 }
866 }
867 nsock->fallback_index = new_index;
868 if (new_index < 0) {
869 dev_err_ratelimited(disk_to_dev(nbd->disk),
870 "Dead connection, failed to find a fallback\n");
871 return new_index;
872 }
873 }
874 new_index = nsock->fallback_index;
875 return new_index;
876}
Paul Clements7fdfd402007-10-16 23:27:37 -0700877
Josef Bacik560bc4b2017-04-06 17:02:04 -0400878static int wait_for_reconnect(struct nbd_device *nbd)
879{
880 struct nbd_config *config = nbd->config;
881 if (!config->dead_conn_timeout)
882 return 0;
Xiubo Liec76a7b2019-09-17 17:26:05 +0530883 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
Josef Bacik560bc4b2017-04-06 17:02:04 -0400884 return 0;
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600885 return wait_event_timeout(config->conn_wait,
886 atomic_read(&config->live_connections) > 0,
887 config->dead_conn_timeout) > 0;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400888}
889
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400890static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700891{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700892 struct request *req = blk_mq_rq_from_pdu(cmd);
893 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400894 struct nbd_config *config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500895 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400896 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700897
Josef Bacik5ea8d102017-04-06 17:01:58 -0400898 if (!refcount_inc_not_zero(&nbd->config_refs)) {
899 dev_err_ratelimited(disk_to_dev(nbd->disk),
900 "Socks array is empty\n");
Josef Bacik6a468d52017-11-06 16:11:58 -0500901 blk_mq_start_request(req);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400902 return -EINVAL;
903 }
904 config = nbd->config;
905
906 if (index >= config->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500907 dev_err_ratelimited(disk_to_dev(nbd->disk),
908 "Attempted send on invalid socket\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -0400909 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500910 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400911 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500912 }
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200913 cmd->status = BLK_STS_OK;
Josef Bacikf3733242017-04-06 17:01:57 -0400914again:
Josef Bacik5ea8d102017-04-06 17:01:58 -0400915 nsock = config->socks[index];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500916 mutex_lock(&nsock->tx_lock);
Josef Bacikf3733242017-04-06 17:01:57 -0400917 if (nsock->dead) {
Josef Bacik560bc4b2017-04-06 17:02:04 -0400918 int old_index = index;
Josef Bacikf3733242017-04-06 17:01:57 -0400919 index = find_fallback(nbd, index);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500920 mutex_unlock(&nsock->tx_lock);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400921 if (index < 0) {
922 if (wait_for_reconnect(nbd)) {
923 index = old_index;
924 goto again;
925 }
926 /* All the sockets should already be down at this point,
927 * we just want to make sure that DISCONNECTED is set so
928 * any requests that come in that were queue'ed waiting
929 * for the reconnect timer don't trigger the timer again
930 * and instead just error out.
931 */
932 sock_shutdown(nbd);
933 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500934 blk_mq_start_request(req);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400935 return -EIO;
936 }
Josef Bacikf3733242017-04-06 17:01:57 -0400937 goto again;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700938 }
939
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400940 /* Handle the case that we have a pending request that was partially
941 * transmitted that _has_ to be serviced first. We need to call requeue
942 * here so that it gets put _after_ the request that is already on the
943 * dispatch list.
944 */
Josef Bacik6a468d52017-11-06 16:11:58 -0500945 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400946 if (unlikely(nsock->pending && nsock->pending != req)) {
Josef Bacikd7d94d42018-07-16 12:11:34 -0400947 nbd_requeue_cmd(cmd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400948 ret = 0;
949 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700950 }
Josef Bacikf3733242017-04-06 17:01:57 -0400951 /*
952 * Some failures are related to the link going down, so anything that
953 * returns EAGAIN can be retried on a different socket.
954 */
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400955 ret = nbd_send_cmd(nbd, cmd, index);
Josef Bacikf3733242017-04-06 17:01:57 -0400956 if (ret == -EAGAIN) {
957 dev_err_ratelimited(disk_to_dev(nbd->disk),
Josef Bacik6a468d52017-11-06 16:11:58 -0500958 "Request send failed, requeueing\n");
Josef Bacik799f9a32017-04-06 17:02:02 -0400959 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400960 nbd_requeue_cmd(cmd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500961 ret = 0;
Josef Bacikf3733242017-04-06 17:01:57 -0400962 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400963out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500964 mutex_unlock(&nsock->tx_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400965 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400966 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700967}
968
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200969static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700970 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700971{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700972 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400973 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700974
Josef Bacik9561a7a2016-11-22 14:04:40 -0500975 /*
976 * Since we look at the bio's to send the request over the network we
977 * need to make sure the completion work doesn't mark this request done
978 * before we are done doing our send. This keeps us from dereferencing
979 * freed data if we have particularly fast completions (ie we get the
980 * completion before we exit sock_xmit on the last bvec) or in the case
981 * that the server is misbehaving (or there was an error) before we're
982 * done sending everything over the wire.
983 */
Josef Bacik8f3ea352018-07-16 12:11:35 -0400984 mutex_lock(&cmd->lock);
Josef Bacikd7d94d42018-07-16 12:11:34 -0400985 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400986
987 /* We can be called directly from the user space process, which means we
988 * could possibly have signals pending so our sendmsg will fail. In
989 * this case we need to return that we are busy, otherwise error out as
990 * appropriate.
991 */
992 ret = nbd_handle_cmd(cmd, hctx->queue_num);
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400993 if (ret < 0)
994 ret = BLK_STS_IOERR;
995 else if (!ret)
996 ret = BLK_STS_OK;
Josef Bacik8f3ea352018-07-16 12:11:35 -0400997 mutex_unlock(&cmd->lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500998
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400999 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Mike Christiecf1b2322019-10-17 16:27:34 -05001002static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
1003 int *err)
1004{
1005 struct socket *sock;
1006
1007 *err = 0;
1008 sock = sockfd_lookup(fd, err);
1009 if (!sock)
1010 return NULL;
1011
1012 if (sock->ops->shutdown == sock_no_shutdown) {
1013 dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
1014 *err = -EINVAL;
Sun Kedff10bb2019-11-19 14:09:11 +08001015 sockfd_put(sock);
Mike Christiecf1b2322019-10-17 16:27:34 -05001016 return NULL;
1017 }
1018
1019 return sock;
1020}
1021
Josef Bacike46c7282017-04-06 17:02:00 -04001022static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
1023 bool netlink)
Markus Pargmann23272a672015-10-29 11:51:16 +01001024{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001025 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001026 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001027 struct nbd_sock **socks;
1028 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -05001029 int err;
1030
Mike Christiecf1b2322019-10-17 16:27:34 -05001031 sock = nbd_get_socket(nbd, arg, &err);
Josef Bacik9442b732017-02-07 17:10:22 -05001032 if (!sock)
1033 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +01001034
Josef Bacik41f6f4a2021-01-25 12:21:02 -05001035 /*
1036 * We need to make sure we don't get any errant requests while we're
1037 * reallocating the ->socks array.
1038 */
1039 blk_mq_freeze_queue(nbd->disk->queue);
1040
Josef Bacike46c7282017-04-06 17:02:00 -04001041 if (!netlink && !nbd->task_setup &&
Xiubo Liec76a7b2019-09-17 17:26:05 +05301042 !test_bit(NBD_RT_BOUND, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -05001043 nbd->task_setup = current;
Josef Bacike46c7282017-04-06 17:02:00 -04001044
1045 if (!netlink &&
1046 (nbd->task_setup != current ||
Xiubo Liec76a7b2019-09-17 17:26:05 +05301047 test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
Josef Bacik9561a7a2016-11-22 14:04:40 -05001048 dev_err(disk_to_dev(nbd->disk),
1049 "Device being setup by another task");
Zheng Bin579dd912020-06-29 09:23:49 +08001050 err = -EBUSY;
1051 goto put_socket;
1052 }
1053
1054 nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
1055 if (!nsock) {
1056 err = -ENOMEM;
1057 goto put_socket;
Markus Pargmann23272a672015-10-29 11:51:16 +01001058 }
1059
Josef Bacik5ea8d102017-04-06 17:01:58 -04001060 socks = krealloc(config->socks, (config->num_connections + 1) *
Josef Bacik9561a7a2016-11-22 14:04:40 -05001061 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -04001062 if (!socks) {
Zheng Bin579dd912020-06-29 09:23:49 +08001063 kfree(nsock);
1064 err = -ENOMEM;
1065 goto put_socket;
Josef Bacik9b1355d2017-04-06 17:01:56 -04001066 }
Navid Emamdoost03bf73c2019-09-23 15:09:58 -05001067
1068 config->socks = socks;
1069
Josef Bacikf3733242017-04-06 17:01:57 -04001070 nsock->fallback_index = -1;
1071 nsock->dead = false;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001072 mutex_init(&nsock->tx_lock);
1073 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001074 nsock->pending = NULL;
1075 nsock->sent = 0;
Josef Bacik799f9a32017-04-06 17:02:02 -04001076 nsock->cookie = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001077 socks[config->num_connections++] = nsock;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001078 atomic_inc(&config->live_connections);
Josef Bacik41f6f4a2021-01-25 12:21:02 -05001079 blk_mq_unfreeze_queue(nbd->disk->queue);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001080
1081 return 0;
Zheng Bin579dd912020-06-29 09:23:49 +08001082
1083put_socket:
Josef Bacik41f6f4a2021-01-25 12:21:02 -05001084 blk_mq_unfreeze_queue(nbd->disk->queue);
Zheng Bin579dd912020-06-29 09:23:49 +08001085 sockfd_put(sock);
1086 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +01001087}
1088
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001089static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
1090{
1091 struct nbd_config *config = nbd->config;
1092 struct socket *sock, *old;
1093 struct recv_thread_args *args;
1094 int i;
1095 int err;
1096
Mike Christiecf1b2322019-10-17 16:27:34 -05001097 sock = nbd_get_socket(nbd, arg, &err);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001098 if (!sock)
1099 return err;
1100
1101 args = kzalloc(sizeof(*args), GFP_KERNEL);
1102 if (!args) {
1103 sockfd_put(sock);
1104 return -ENOMEM;
1105 }
1106
1107 for (i = 0; i < config->num_connections; i++) {
1108 struct nbd_sock *nsock = config->socks[i];
1109
1110 if (!nsock->dead)
1111 continue;
1112
1113 mutex_lock(&nsock->tx_lock);
1114 if (!nsock->dead) {
1115 mutex_unlock(&nsock->tx_lock);
1116 continue;
1117 }
1118 sk_set_memalloc(sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001119 if (nbd->tag_set.timeout)
1120 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001121 atomic_inc(&config->recv_threads);
1122 refcount_inc(&nbd->config_refs);
1123 old = nsock->sock;
1124 nsock->fallback_index = -1;
1125 nsock->sock = sock;
1126 nsock->dead = false;
1127 INIT_WORK(&args->work, recv_work);
1128 args->index = i;
1129 args->nbd = nbd;
Josef Bacik799f9a32017-04-06 17:02:02 -04001130 nsock->cookie++;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001131 mutex_unlock(&nsock->tx_lock);
1132 sockfd_put(old);
1133
Xiubo Liec76a7b2019-09-17 17:26:05 +05301134 clear_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
Josef Bacik7a362ea2017-07-25 13:31:19 -04001135
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001136 /* We take the tx_mutex in an error path in the recv_work, so we
1137 * need to queue_work outside of the tx_mutex.
1138 */
Mike Christiee9e006f2019-08-04 14:10:06 -05001139 queue_work(nbd->recv_workq, &args->work);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001140
1141 atomic_inc(&config->live_connections);
1142 wake_up(&config->conn_wait);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001143 return 0;
1144 }
1145 sockfd_put(sock);
1146 kfree(args);
1147 return -ENOSPC;
1148}
1149
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001150static void nbd_bdev_reset(struct block_device *bdev)
1151{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -04001152 if (bdev->bd_openers > 1)
1153 return;
Christoph Hellwig611bee52020-08-23 11:10:41 +02001154 bd_set_nr_sectors(bdev, 0);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001155}
1156
Josef Bacik29eaadc2017-04-06 17:01:59 -04001157static void nbd_parse_flags(struct nbd_device *nbd)
Markus Pargmannd02cf532015-10-29 12:06:15 +01001158{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001159 struct nbd_config *config = nbd->config;
1160 if (config->flags & NBD_FLAG_READ_ONLY)
Josef Bacik29eaadc2017-04-06 17:01:59 -04001161 set_disk_ro(nbd->disk, true);
1162 else
1163 set_disk_ro(nbd->disk, false);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001164 if (config->flags & NBD_FLAG_SEND_TRIM)
Bart Van Assche8b904b52018-03-07 17:10:10 -08001165 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Shaun McDowell685c9b22017-05-25 23:55:54 -04001166 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1167 if (config->flags & NBD_FLAG_SEND_FUA)
1168 blk_queue_write_cache(nbd->disk->queue, true, true);
1169 else
1170 blk_queue_write_cache(nbd->disk->queue, true, false);
1171 }
Markus Pargmannd02cf532015-10-29 12:06:15 +01001172 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -06001173 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +01001174}
1175
Josef Bacik9561a7a2016-11-22 14:04:40 -05001176static void send_disconnects(struct nbd_device *nbd)
1177{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001178 struct nbd_config *config = nbd->config;
Al Viroc9f2b6a2015-11-12 05:09:35 -05001179 struct nbd_request request = {
1180 .magic = htonl(NBD_REQUEST_MAGIC),
1181 .type = htonl(NBD_CMD_DISC),
1182 };
1183 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1184 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001185 int i, ret;
1186
Josef Bacik5ea8d102017-04-06 17:01:58 -04001187 for (i = 0; i < config->num_connections; i++) {
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001188 struct nbd_sock *nsock = config->socks[i];
1189
David Howellsaa563d72018-10-20 00:57:56 +01001190 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001191 mutex_lock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001192 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001193 if (ret <= 0)
1194 dev_err(disk_to_dev(nbd->disk),
1195 "Send disconnect failed %d\n", ret);
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001196 mutex_unlock(&nsock->tx_lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001197 }
1198}
1199
Josef Bacik29eaadc2017-04-06 17:01:59 -04001200static int nbd_disconnect(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001201{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001202 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001203
Josef Bacik5ea8d102017-04-06 17:01:58 -04001204 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Xiubo Liec76a7b2019-09-17 17:26:05 +05301205 set_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
Xiubo Li8454d682019-09-17 17:26:06 +05301206 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags);
Josef Bacik2e134562017-07-21 10:48:13 -04001207 send_disconnects(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001208 return 0;
1209}
1210
Josef Bacik29eaadc2017-04-06 17:01:59 -04001211static void nbd_clear_sock(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001212{
1213 sock_shutdown(nbd);
1214 nbd_clear_que(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001215 nbd->task_setup = NULL;
Josef Bacik9442b732017-02-07 17:10:22 -05001216}
1217
Josef Bacik5ea8d102017-04-06 17:01:58 -04001218static void nbd_config_put(struct nbd_device *nbd)
1219{
1220 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1221 &nbd->config_lock)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001222 struct nbd_config *config = nbd->config;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001223 nbd_dev_dbg_close(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001224 nbd_size_clear(nbd);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301225 if (test_and_clear_bit(NBD_RT_HAS_PID_FILE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001226 &config->runtime_flags))
1227 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1228 nbd->task_recv = NULL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001229 nbd_clear_sock(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001230 if (config->num_connections) {
1231 int i;
1232 for (i = 0; i < config->num_connections; i++) {
1233 sockfd_put(config->socks[i]->sock);
1234 kfree(config->socks[i]);
1235 }
1236 kfree(config->socks);
1237 }
Ilya Dryomovfa976532017-05-23 17:49:55 +02001238 kfree(nbd->config);
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001239 nbd->config = NULL;
1240
Mike Christiee9e006f2019-08-04 14:10:06 -05001241 if (nbd->recv_workq)
1242 destroy_workqueue(nbd->recv_workq);
1243 nbd->recv_workq = NULL;
1244
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001245 nbd->tag_set.timeout = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001246 nbd->disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001247 nbd->disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001248 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001249 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Josef Bacika2c97902017-04-06 17:02:07 -04001250
Josef Bacik5ea8d102017-04-06 17:01:58 -04001251 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001252 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001253 module_put(THIS_MODULE);
1254 }
1255}
1256
Josef Bacike46c7282017-04-06 17:02:00 -04001257static int nbd_start_device(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001258{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001259 struct nbd_config *config = nbd->config;
1260 int num_connections = config->num_connections;
Josef Bacik9442b732017-02-07 17:10:22 -05001261 int error = 0, i;
1262
1263 if (nbd->task_recv)
1264 return -EBUSY;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001265 if (!config->socks)
Josef Bacik9442b732017-02-07 17:10:22 -05001266 return -EINVAL;
1267 if (num_connections > 1 &&
Josef Bacik5ea8d102017-04-06 17:01:58 -04001268 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
Josef Bacik9442b732017-02-07 17:10:22 -05001269 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001270 return -EINVAL;
Josef Bacik9442b732017-02-07 17:10:22 -05001271 }
1272
Mike Christiee9e006f2019-08-04 14:10:06 -05001273 nbd->recv_workq = alloc_workqueue("knbd%d-recv",
1274 WQ_MEM_RECLAIM | WQ_HIGHPRI |
1275 WQ_UNBOUND, 0, nbd->index);
1276 if (!nbd->recv_workq) {
1277 dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1278 return -ENOMEM;
1279 }
1280
Josef Bacik5ea8d102017-04-06 17:01:58 -04001281 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
Josef Bacik9442b732017-02-07 17:10:22 -05001282 nbd->task_recv = current;
Josef Bacik9442b732017-02-07 17:10:22 -05001283
Josef Bacik29eaadc2017-04-06 17:01:59 -04001284 nbd_parse_flags(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001285
1286 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1287 if (error) {
1288 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001289 return error;
Josef Bacik9442b732017-02-07 17:10:22 -05001290 }
Xiubo Liec76a7b2019-09-17 17:26:05 +05301291 set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags);
Josef Bacik9442b732017-02-07 17:10:22 -05001292
1293 nbd_dev_dbg_init(nbd);
1294 for (i = 0; i < num_connections; i++) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001295 struct recv_thread_args *args;
1296
1297 args = kzalloc(sizeof(*args), GFP_KERNEL);
1298 if (!args) {
1299 sock_shutdown(nbd);
Sun Ke5c0dd222020-01-22 11:18:57 +08001300 /*
1301 * If num_connections is m (2 < m),
1302 * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
1303 * But NO.(n + 1) failed. We still have n recv threads.
1304 * So, add flush_workqueue here to prevent recv threads
1305 * dropping the last config_refs and trying to destroy
1306 * the workqueue from inside the workqueue.
1307 */
1308 if (i)
1309 flush_workqueue(nbd->recv_workq);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001310 return -ENOMEM;
1311 }
1312 sk_set_memalloc(config->socks[i]->sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001313 if (nbd->tag_set.timeout)
1314 config->socks[i]->sock->sk->sk_sndtimeo =
1315 nbd->tag_set.timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001316 atomic_inc(&config->recv_threads);
1317 refcount_inc(&nbd->config_refs);
1318 INIT_WORK(&args->work, recv_work);
1319 args->nbd = nbd;
1320 args->index = i;
Mike Christiee9e006f2019-08-04 14:10:06 -05001321 queue_work(nbd->recv_workq, &args->work);
Josef Bacik9442b732017-02-07 17:10:22 -05001322 }
Ming Leib40813d2020-10-28 15:24:34 +08001323 nbd_size_update(nbd, true);
Josef Bacike46c7282017-04-06 17:02:00 -04001324 return error;
1325}
1326
1327static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1328{
1329 struct nbd_config *config = nbd->config;
1330 int ret;
1331
1332 ret = nbd_start_device(nbd);
1333 if (ret)
1334 return ret;
1335
Josef Bacike46c7282017-04-06 17:02:00 -04001336 if (max_part)
Christoph Hellwig38430f02020-09-21 09:19:45 +02001337 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
Josef Bacike46c7282017-04-06 17:02:00 -04001338 mutex_unlock(&nbd->config_lock);
1339 ret = wait_event_interruptible(config->recv_wq,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001340 atomic_read(&config->recv_threads) == 0);
Mike Christie1c058392019-12-08 16:51:50 -06001341 if (ret)
Josef Bacik5ea8d102017-04-06 17:01:58 -04001342 sock_shutdown(nbd);
Mike Christie1c058392019-12-08 16:51:50 -06001343 flush_workqueue(nbd->recv_workq);
1344
Josef Bacik9442b732017-02-07 17:10:22 -05001345 mutex_lock(&nbd->config_lock);
Josef Bacik76aa1d32018-05-16 14:51:22 -04001346 nbd_bdev_reset(bdev);
Josef Bacik9442b732017-02-07 17:10:22 -05001347 /* user requested, ignore socket errors */
Xiubo Liec76a7b2019-09-17 17:26:05 +05301348 if (test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001349 ret = 0;
Xiubo Liec76a7b2019-09-17 17:26:05 +05301350 if (test_bit(NBD_RT_TIMEDOUT, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001351 ret = -ETIMEDOUT;
1352 return ret;
Josef Bacik9442b732017-02-07 17:10:22 -05001353}
Markus Pargmann30d53d92015-08-17 08:20:06 +02001354
Josef Bacik29eaadc2017-04-06 17:01:59 -04001355static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1356 struct block_device *bdev)
1357{
Josef Bacik2516ab12017-04-06 17:02:03 -04001358 sock_shutdown(nbd);
Munehisa Kamata2b5c8f02019-07-31 20:13:10 +08001359 __invalidate_device(bdev, true);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001360 nbd_bdev_reset(bdev);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301361 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
Josef Bacike46c7282017-04-06 17:02:00 -04001362 &nbd->config->runtime_flags))
1363 nbd_config_put(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001364}
1365
Xiubo Li553768d2019-05-29 15:16:05 -05001366static bool nbd_is_valid_blksize(unsigned long blksize)
1367{
1368 if (!blksize || !is_power_of_2(blksize) || blksize < 512 ||
1369 blksize > PAGE_SIZE)
1370 return false;
1371 return true;
1372}
1373
Mike Christie55313e92019-08-13 11:39:49 -05001374static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
1375{
1376 nbd->tag_set.timeout = timeout * HZ;
Mike Christie2da22da2019-08-13 11:39:52 -05001377 if (timeout)
1378 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
Hou Puacb19e12020-08-10 08:00:44 -04001379 else
1380 blk_queue_rq_timeout(nbd->disk->queue, 30 * HZ);
Mike Christie55313e92019-08-13 11:39:49 -05001381}
1382
Josef Bacik9561a7a2016-11-22 14:04:40 -05001383/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -07001384static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -07001385 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001387 struct nbd_config *config = nbd->config;
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -05001390 case NBD_DISCONNECT:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001391 return nbd_disconnect(nbd);
Markus Pargmann23272a672015-10-29 11:51:16 +01001392 case NBD_CLEAR_SOCK:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001393 nbd_clear_sock_ioctl(nbd, bdev);
1394 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001395 case NBD_SET_SOCK:
Josef Bacike46c7282017-04-06 17:02:00 -04001396 return nbd_add_socket(nbd, arg, false);
Josef Bacik9442b732017-02-07 17:10:22 -05001397 case NBD_SET_BLKSIZE:
Xiubo Li553768d2019-05-29 15:16:05 -05001398 if (!arg)
1399 arg = NBD_DEF_BLKSIZE;
1400 if (!nbd_is_valid_blksize(arg))
Jens Axboebc811f02018-09-04 11:52:34 -06001401 return -EINVAL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001402 nbd_size_set(nbd, arg,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001403 div_s64(config->bytesize, arg));
Josef Bacike5445412017-02-13 10:39:47 -05001404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 case NBD_SET_SIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001406 nbd_size_set(nbd, config->blksize,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001407 div_s64(arg, config->blksize));
Josef Bacike5445412017-02-13 10:39:47 -05001408 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +02001409 case NBD_SET_SIZE_BLOCKS:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001410 nbd_size_set(nbd, config->blksize, arg);
Josef Bacike5445412017-02-13 10:39:47 -05001411 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -07001412 case NBD_SET_TIMEOUT:
Mike Christie2da22da2019-08-13 11:39:52 -05001413 nbd_set_cmd_timeout(nbd, arg);
Paul Clements7fdfd402007-10-16 23:27:37 -07001414 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001415
Paul Clements2f012502012-10-04 17:16:15 -07001416 case NBD_SET_FLAGS:
Josef Bacik5ea8d102017-04-06 17:01:58 -04001417 config->flags = arg;
Paul Clements2f012502012-10-04 17:16:15 -07001418 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001419 case NBD_DO_IT:
Josef Bacike46c7282017-04-06 17:02:00 -04001420 return nbd_start_device_ioctl(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -08001422 /*
1423 * This is for compatibility only. The queue is always cleared
1424 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return 0;
1427 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -07001428 /*
1429 * For compatibility only, we no longer keep a list of
1430 * outstanding requests.
1431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
1433 }
Pavel Machek1a2ad212009-04-02 16:58:41 -07001434 return -ENOTTY;
1435}
1436
1437static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1438 unsigned int cmd, unsigned long arg)
1439{
Wanlong Gaof4507162012-03-28 14:42:51 -07001440 struct nbd_device *nbd = bdev->bd_disk->private_data;
Josef Bacike46c7282017-04-06 17:02:00 -04001441 struct nbd_config *config = nbd->config;
1442 int error = -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001443
1444 if (!capable(CAP_SYS_ADMIN))
1445 return -EPERM;
1446
Josef Bacik1dae69b2017-05-05 22:25:18 -04001447 /* The block layer will pass back some non-nbd ioctls in case we have
1448 * special handling for them, but we don't so just return an error.
1449 */
1450 if (_IOC_TYPE(cmd) != 0xab)
1451 return -EINVAL;
1452
Josef Bacik9561a7a2016-11-22 14:04:40 -05001453 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001454
1455 /* Don't allow ioctl operations on a nbd device that was created with
1456 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1457 */
Xiubo Liec76a7b2019-09-17 17:26:05 +05301458 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
Josef Bacike46c7282017-04-06 17:02:00 -04001459 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1460 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1461 else
1462 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -05001463 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -07001464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Josef Bacik5ea8d102017-04-06 17:01:58 -04001467static struct nbd_config *nbd_alloc_config(void)
1468{
1469 struct nbd_config *config;
1470
1471 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1472 if (!config)
1473 return NULL;
1474 atomic_set(&config->recv_threads, 0);
1475 init_waitqueue_head(&config->recv_wq);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001476 init_waitqueue_head(&config->conn_wait);
Xiubo Li553768d2019-05-29 15:16:05 -05001477 config->blksize = NBD_DEF_BLKSIZE;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001478 atomic_set(&config->live_connections, 0);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001479 try_module_get(THIS_MODULE);
1480 return config;
1481}
1482
1483static int nbd_open(struct block_device *bdev, fmode_t mode)
1484{
1485 struct nbd_device *nbd;
1486 int ret = 0;
1487
1488 mutex_lock(&nbd_index_mutex);
1489 nbd = bdev->bd_disk->private_data;
1490 if (!nbd) {
1491 ret = -ENXIO;
1492 goto out;
1493 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001494 if (!refcount_inc_not_zero(&nbd->refs)) {
1495 ret = -ENXIO;
1496 goto out;
1497 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001498 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1499 struct nbd_config *config;
1500
1501 mutex_lock(&nbd->config_lock);
1502 if (refcount_inc_not_zero(&nbd->config_refs)) {
1503 mutex_unlock(&nbd->config_lock);
1504 goto out;
1505 }
1506 config = nbd->config = nbd_alloc_config();
1507 if (!config) {
1508 ret = -ENOMEM;
1509 mutex_unlock(&nbd->config_lock);
1510 goto out;
1511 }
1512 refcount_set(&nbd->config_refs, 1);
Josef Bacikc6a47592017-04-06 17:02:06 -04001513 refcount_inc(&nbd->refs);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001514 mutex_unlock(&nbd->config_lock);
Christoph Hellwig38430f02020-09-21 09:19:45 +02001515 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Josef Bacikfe1f9e62018-05-16 14:51:21 -04001516 } else if (nbd_disconnected(nbd->config)) {
Christoph Hellwig38430f02020-09-21 09:19:45 +02001517 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001518 }
1519out:
1520 mutex_unlock(&nbd_index_mutex);
1521 return ret;
1522}
1523
1524static void nbd_release(struct gendisk *disk, fmode_t mode)
1525{
1526 struct nbd_device *nbd = disk->private_data;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001527 struct block_device *bdev = bdget_disk(disk, 0);
1528
Xiubo Liec76a7b2019-09-17 17:26:05 +05301529 if (test_bit(NBD_RT_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001530 bdev->bd_openers == 0)
1531 nbd_disconnect_and_put(nbd);
Christoph Hellwig2bd645b2020-11-09 18:30:59 +01001532 bdput(bdev);
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001533
Josef Bacik5ea8d102017-04-06 17:01:58 -04001534 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001535 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001536}
1537
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001538static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 .owner = THIS_MODULE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001541 .open = nbd_open,
1542 .release = nbd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001543 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -05001544 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545};
1546
Markus Pargmann30d53d92015-08-17 08:20:06 +02001547#if IS_ENABLED(CONFIG_DEBUG_FS)
1548
1549static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1550{
1551 struct nbd_device *nbd = s->private;
1552
1553 if (nbd->task_recv)
1554 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +02001555
1556 return 0;
1557}
1558
1559static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1560{
1561 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1562}
1563
1564static const struct file_operations nbd_dbg_tasks_ops = {
1565 .open = nbd_dbg_tasks_open,
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = single_release,
1569};
1570
1571static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1572{
1573 struct nbd_device *nbd = s->private;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001574 u32 flags = nbd->config->flags;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001575
1576 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1577
1578 seq_puts(s, "Known flags:\n");
1579
1580 if (flags & NBD_FLAG_HAS_FLAGS)
1581 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1582 if (flags & NBD_FLAG_READ_ONLY)
1583 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1584 if (flags & NBD_FLAG_SEND_FLUSH)
1585 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
Shaun McDowell685c9b22017-05-25 23:55:54 -04001586 if (flags & NBD_FLAG_SEND_FUA)
1587 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
Markus Pargmann30d53d92015-08-17 08:20:06 +02001588 if (flags & NBD_FLAG_SEND_TRIM)
1589 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1590
1591 return 0;
1592}
1593
1594static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1595{
1596 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1597}
1598
1599static const struct file_operations nbd_dbg_flags_ops = {
1600 .open = nbd_dbg_flags_open,
1601 .read = seq_read,
1602 .llseek = seq_lseek,
1603 .release = single_release,
1604};
1605
1606static int nbd_dev_dbg_init(struct nbd_device *nbd)
1607{
1608 struct dentry *dir;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001609 struct nbd_config *config = nbd->config;
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001610
1611 if (!nbd_dbg_dir)
1612 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001613
1614 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001615 if (!dir) {
1616 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1617 nbd_name(nbd));
1618 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001619 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001620 config->dbg_dir = dir;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001621
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001622 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001623 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -07001624 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001625 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -04001626 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001627
1628 return 0;
1629}
1630
1631static void nbd_dev_dbg_close(struct nbd_device *nbd)
1632{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001633 debugfs_remove_recursive(nbd->config->dbg_dir);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001634}
1635
1636static int nbd_dbg_init(void)
1637{
1638 struct dentry *dbg_dir;
1639
1640 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001641 if (!dbg_dir)
1642 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001643
1644 nbd_dbg_dir = dbg_dir;
1645
1646 return 0;
1647}
1648
1649static void nbd_dbg_close(void)
1650{
1651 debugfs_remove_recursive(nbd_dbg_dir);
1652}
1653
1654#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1655
1656static int nbd_dev_dbg_init(struct nbd_device *nbd)
1657{
1658 return 0;
1659}
1660
1661static void nbd_dev_dbg_close(struct nbd_device *nbd)
1662{
1663}
1664
1665static int nbd_dbg_init(void)
1666{
1667 return 0;
1668}
1669
1670static void nbd_dbg_close(void)
1671{
1672}
1673
1674#endif
1675
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001676static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1677 unsigned int hctx_idx, unsigned int numa_node)
Josef Bacikfd8383f2016-09-08 12:33:37 -07001678{
1679 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001680 cmd->nbd = set->driver_data;
Josef Bacikd7d94d42018-07-16 12:11:34 -04001681 cmd->flags = 0;
Josef Bacik8f3ea352018-07-16 12:11:35 -04001682 mutex_init(&cmd->lock);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001683 return 0;
1684}
1685
Eric Biggersf363b082017-03-30 13:39:16 -07001686static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001687 .queue_rq = nbd_queue_rq,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +02001688 .complete = nbd_complete_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001689 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001690 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001691};
1692
Josef Bacikb0d91112017-02-01 16:11:40 -05001693static int nbd_dev_add(int index)
1694{
1695 struct nbd_device *nbd;
1696 struct gendisk *disk;
1697 struct request_queue *q;
1698 int err = -ENOMEM;
1699
1700 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1701 if (!nbd)
1702 goto out;
1703
1704 disk = alloc_disk(1 << part_shift);
1705 if (!disk)
1706 goto out_free_nbd;
1707
1708 if (index >= 0) {
1709 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1710 GFP_KERNEL);
1711 if (err == -ENOSPC)
1712 err = -EEXIST;
1713 } else {
1714 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1715 if (err >= 0)
1716 index = err;
1717 }
1718 if (err < 0)
1719 goto out_free_disk;
1720
Josef Bacike46c7282017-04-06 17:02:00 -04001721 nbd->index = index;
Josef Bacikb0d91112017-02-01 16:11:40 -05001722 nbd->disk = disk;
1723 nbd->tag_set.ops = &nbd_mq_ops;
1724 nbd->tag_set.nr_hw_queues = 1;
1725 nbd->tag_set.queue_depth = 128;
1726 nbd->tag_set.numa_node = NUMA_NO_NODE;
1727 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1728 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
Ming Lei56d18f62019-02-15 19:13:24 +08001729 BLK_MQ_F_BLOCKING;
Josef Bacikb0d91112017-02-01 16:11:40 -05001730 nbd->tag_set.driver_data = nbd;
Xiubo Li8454d682019-09-17 17:26:06 +05301731 nbd->destroy_complete = NULL;
Josef Bacikb0d91112017-02-01 16:11:40 -05001732
1733 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1734 if (err)
1735 goto out_free_idr;
1736
1737 q = blk_mq_init_queue(&nbd->tag_set);
1738 if (IS_ERR(q)) {
1739 err = PTR_ERR(q);
1740 goto out_free_tags;
1741 }
1742 disk->queue = q;
1743
1744 /*
1745 * Tell the block layer that we are not a rotational device
1746 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08001747 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1748 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Josef Bacik6df133a2018-05-23 13:35:59 -04001749 disk->queue->limits.discard_granularity = 0;
Josef Bacik07ce2132018-06-05 11:41:23 -04001750 disk->queue->limits.discard_alignment = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001751 blk_queue_max_discard_sectors(disk->queue, 0);
Josef Bacikebb16d02017-04-18 16:22:51 -04001752 blk_queue_max_segment_size(disk->queue, UINT_MAX);
Josef Bacik1cc1f172017-04-20 15:47:01 -04001753 blk_queue_max_segments(disk->queue, USHRT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001754 blk_queue_max_hw_sectors(disk->queue, 65536);
1755 disk->queue->limits.max_sectors = 256;
1756
Josef Bacikb0d91112017-02-01 16:11:40 -05001757 mutex_init(&nbd->config_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001758 refcount_set(&nbd->config_refs, 0);
Josef Bacikc6a47592017-04-06 17:02:06 -04001759 refcount_set(&nbd->refs, 1);
1760 INIT_LIST_HEAD(&nbd->list);
Josef Bacikb0d91112017-02-01 16:11:40 -05001761 disk->major = NBD_MAJOR;
1762 disk->first_minor = index << part_shift;
1763 disk->fops = &nbd_fops;
1764 disk->private_data = nbd;
1765 sprintf(disk->disk_name, "nbd%d", index);
Josef Bacikb0d91112017-02-01 16:11:40 -05001766 add_disk(disk);
Josef Bacik47d902b2017-04-06 17:02:05 -04001767 nbd_total_devices++;
Josef Bacikb0d91112017-02-01 16:11:40 -05001768 return index;
1769
1770out_free_tags:
1771 blk_mq_free_tag_set(&nbd->tag_set);
1772out_free_idr:
1773 idr_remove(&nbd_index_idr, index);
1774out_free_disk:
1775 put_disk(disk);
1776out_free_nbd:
1777 kfree(nbd);
1778out:
1779 return err;
1780}
1781
Josef Bacike46c7282017-04-06 17:02:00 -04001782static int find_free_cb(int id, void *ptr, void *data)
1783{
1784 struct nbd_device *nbd = ptr;
1785 struct nbd_device **found = data;
1786
1787 if (!refcount_read(&nbd->config_refs)) {
1788 *found = nbd;
1789 return 1;
1790 }
1791 return 0;
1792}
1793
1794/* Netlink interface. */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001795static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001796 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1797 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1798 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1799 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1800 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1801 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1802 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
Josef Bacik560bc4b2017-04-06 17:02:04 -04001803 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001804 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
Josef Bacike46c7282017-04-06 17:02:00 -04001805};
1806
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001807static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
Josef Bacike46c7282017-04-06 17:02:00 -04001808 [NBD_SOCK_FD] = { .type = NLA_U32 },
1809};
1810
Josef Bacik47d902b2017-04-06 17:02:05 -04001811/* We don't use this right now since we don't parse the incoming list, but we
1812 * still want it here so userspace knows what to expect.
1813 */
Stephen Hemmingera86c4122018-07-18 09:32:43 -07001814static const struct nla_policy __attribute__((unused))
Josef Bacik47d902b2017-04-06 17:02:05 -04001815nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1816 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1817 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1818};
1819
Mike Christie4ddeaae82019-05-29 15:16:06 -05001820static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
1821{
1822 struct nbd_config *config = nbd->config;
1823 u64 bsize = config->blksize;
1824 u64 bytes = config->bytesize;
1825
1826 if (info->attrs[NBD_ATTR_SIZE_BYTES])
1827 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1828
1829 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1830 bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1831 if (!bsize)
1832 bsize = NBD_DEF_BLKSIZE;
1833 if (!nbd_is_valid_blksize(bsize)) {
1834 printk(KERN_ERR "Invalid block size %llu\n", bsize);
1835 return -EINVAL;
1836 }
1837 }
1838
1839 if (bytes != config->bytesize || bsize != config->blksize)
1840 nbd_size_set(nbd, bsize, div64_u64(bytes, bsize));
1841 return 0;
1842}
1843
Josef Bacike46c7282017-04-06 17:02:00 -04001844static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1845{
Xiubo Li8454d682019-09-17 17:26:06 +05301846 DECLARE_COMPLETION_ONSTACK(destroy_complete);
Josef Bacike46c7282017-04-06 17:02:00 -04001847 struct nbd_device *nbd = NULL;
1848 struct nbd_config *config;
1849 int index = -1;
1850 int ret;
Josef Bacika2c97902017-04-06 17:02:07 -04001851 bool put_dev = false;
Josef Bacike46c7282017-04-06 17:02:00 -04001852
1853 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1854 return -EPERM;
1855
1856 if (info->attrs[NBD_ATTR_INDEX])
1857 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1858 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1859 printk(KERN_ERR "nbd: must specify at least one socket\n");
1860 return -EINVAL;
1861 }
1862 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1863 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1864 return -EINVAL;
1865 }
1866again:
1867 mutex_lock(&nbd_index_mutex);
1868 if (index == -1) {
1869 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1870 if (ret == 0) {
1871 int new_index;
1872 new_index = nbd_dev_add(-1);
1873 if (new_index < 0) {
1874 mutex_unlock(&nbd_index_mutex);
1875 printk(KERN_ERR "nbd: failed to add new device\n");
Gustavo A. R. Silva09799622018-02-12 11:14:55 -06001876 return new_index;
Josef Bacike46c7282017-04-06 17:02:00 -04001877 }
1878 nbd = idr_find(&nbd_index_idr, new_index);
1879 }
1880 } else {
1881 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike6a76272017-08-14 18:25:33 +00001882 if (!nbd) {
1883 ret = nbd_dev_add(index);
1884 if (ret < 0) {
1885 mutex_unlock(&nbd_index_mutex);
1886 printk(KERN_ERR "nbd: failed to add new device\n");
1887 return ret;
1888 }
1889 nbd = idr_find(&nbd_index_idr, index);
1890 }
Josef Bacike46c7282017-04-06 17:02:00 -04001891 }
Josef Bacike46c7282017-04-06 17:02:00 -04001892 if (!nbd) {
1893 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1894 index);
Josef Bacikc6a47592017-04-06 17:02:06 -04001895 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001896 return -EINVAL;
1897 }
Xiubo Li8454d682019-09-17 17:26:06 +05301898
1899 if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
1900 test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) {
1901 nbd->destroy_complete = &destroy_complete;
1902 mutex_unlock(&nbd_index_mutex);
1903
1904 /* Wait untill the the nbd stuff is totally destroyed */
1905 wait_for_completion(&destroy_complete);
1906 goto again;
1907 }
1908
Josef Bacikc6a47592017-04-06 17:02:06 -04001909 if (!refcount_inc_not_zero(&nbd->refs)) {
1910 mutex_unlock(&nbd_index_mutex);
1911 if (index == -1)
1912 goto again;
1913 printk(KERN_ERR "nbd: device at index %d is going down\n",
1914 index);
1915 return -EINVAL;
1916 }
1917 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001918
1919 mutex_lock(&nbd->config_lock);
1920 if (refcount_read(&nbd->config_refs)) {
1921 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001922 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001923 if (index == -1)
1924 goto again;
1925 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1926 return -EBUSY;
1927 }
1928 if (WARN_ON(nbd->config)) {
1929 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001930 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001931 return -EINVAL;
1932 }
1933 config = nbd->config = nbd_alloc_config();
1934 if (!nbd->config) {
1935 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001936 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001937 printk(KERN_ERR "nbd: couldn't allocate config\n");
1938 return -ENOMEM;
1939 }
1940 refcount_set(&nbd->config_refs, 1);
Xiubo Liec76a7b2019-09-17 17:26:05 +05301941 set_bit(NBD_RT_BOUND, &config->runtime_flags);
Josef Bacike46c7282017-04-06 17:02:00 -04001942
Mike Christie4ddeaae82019-05-29 15:16:06 -05001943 ret = nbd_genl_size_set(info, nbd);
1944 if (ret)
1945 goto out;
1946
Mike Christie55313e92019-08-13 11:39:49 -05001947 if (info->attrs[NBD_ATTR_TIMEOUT])
1948 nbd_set_cmd_timeout(nbd,
1949 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04001950 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1951 config->dead_conn_timeout =
1952 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1953 config->dead_conn_timeout *= HZ;
1954 }
Josef Bacike46c7282017-04-06 17:02:00 -04001955 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1956 config->flags =
1957 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
Josef Bacika2c97902017-04-06 17:02:07 -04001958 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1959 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1960 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
Josef Bacikdc2b7762021-02-22 15:09:53 -05001961 /*
1962 * We have 1 ref to keep the device around, and then 1
1963 * ref for our current operation here, which will be
1964 * inherited by the config. If we already have
1965 * DESTROY_ON_DISCONNECT set then we know we don't have
1966 * that extra ref already held so we don't need the
1967 * put_dev.
1968 */
1969 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1970 &nbd->flags))
1971 put_dev = true;
Xiubo Li8454d682019-09-17 17:26:06 +05301972 } else {
Josef Bacikdc2b7762021-02-22 15:09:53 -05001973 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1974 &nbd->flags))
1975 refcount_inc(&nbd->refs);
Josef Bacika2c97902017-04-06 17:02:07 -04001976 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001977 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05301978 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07001979 &config->runtime_flags);
1980 }
Josef Bacika2c97902017-04-06 17:02:07 -04001981 }
1982
Josef Bacike46c7282017-04-06 17:02:00 -04001983 if (info->attrs[NBD_ATTR_SOCKETS]) {
1984 struct nlattr *attr;
1985 int rem, fd;
1986
1987 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1988 rem) {
1989 struct nlattr *socks[NBD_SOCK_MAX+1];
1990
1991 if (nla_type(attr) != NBD_SOCK_ITEM) {
1992 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1993 ret = -EINVAL;
1994 goto out;
1995 }
Johannes Berg8cb08172019-04-26 14:07:28 +02001996 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
1997 attr,
1998 nbd_sock_policy,
1999 info->extack);
Josef Bacike46c7282017-04-06 17:02:00 -04002000 if (ret != 0) {
2001 printk(KERN_ERR "nbd: error processing sock list\n");
2002 ret = -EINVAL;
2003 goto out;
2004 }
2005 if (!socks[NBD_SOCK_FD])
2006 continue;
2007 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2008 ret = nbd_add_socket(nbd, fd, true);
2009 if (ret)
2010 goto out;
2011 }
2012 }
2013 ret = nbd_start_device(nbd);
2014out:
2015 mutex_unlock(&nbd->config_lock);
2016 if (!ret) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302017 set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags);
Josef Bacike46c7282017-04-06 17:02:00 -04002018 refcount_inc(&nbd->config_refs);
2019 nbd_connect_reply(info, nbd->index);
2020 }
2021 nbd_config_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04002022 if (put_dev)
2023 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002024 return ret;
2025}
2026
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002027static void nbd_disconnect_and_put(struct nbd_device *nbd)
2028{
2029 mutex_lock(&nbd->config_lock);
2030 nbd_disconnect(nbd);
Xie Yongjie0ee8d92021-08-13 23:13:30 +08002031 sock_shutdown(nbd);
Mike Christiee9e006f2019-08-04 14:10:06 -05002032 /*
2033 * Make sure recv thread has finished, so it does not drop the last
2034 * config ref and try to destroy the workqueue from inside the work
Xie Yongjie0ee8d92021-08-13 23:13:30 +08002035 * queue. And this also ensure that we can safely call nbd_clear_que()
2036 * to cancel the inflight I/Os.
Mike Christiee9e006f2019-08-04 14:10:06 -05002037 */
Sun Kecde4b552021-05-12 19:43:30 +08002038 if (nbd->recv_workq)
2039 flush_workqueue(nbd->recv_workq);
Xie Yongjie0ee8d92021-08-13 23:13:30 +08002040 nbd_clear_que(nbd);
2041 nbd->task_setup = NULL;
2042 mutex_unlock(&nbd->config_lock);
2043
Xiubo Liec76a7b2019-09-17 17:26:05 +05302044 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002045 &nbd->config->runtime_flags))
2046 nbd_config_put(nbd);
2047}
2048
Josef Bacike46c7282017-04-06 17:02:00 -04002049static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
2050{
2051 struct nbd_device *nbd;
2052 int index;
2053
2054 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2055 return -EPERM;
2056
2057 if (!info->attrs[NBD_ATTR_INDEX]) {
2058 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
2059 return -EINVAL;
2060 }
2061 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2062 mutex_lock(&nbd_index_mutex);
2063 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike46c7282017-04-06 17:02:00 -04002064 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04002065 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04002066 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
2067 index);
2068 return -EINVAL;
2069 }
Josef Bacikc6a47592017-04-06 17:02:06 -04002070 if (!refcount_inc_not_zero(&nbd->refs)) {
2071 mutex_unlock(&nbd_index_mutex);
2072 printk(KERN_ERR "nbd: device at index %d is going down\n",
2073 index);
2074 return -EINVAL;
2075 }
2076 mutex_unlock(&nbd_index_mutex);
2077 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2078 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002079 return 0;
Josef Bacikc6a47592017-04-06 17:02:06 -04002080 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002081 nbd_disconnect_and_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002082 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002083 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04002084 return 0;
2085}
2086
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002087static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
2088{
2089 struct nbd_device *nbd = NULL;
2090 struct nbd_config *config;
2091 int index;
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002092 int ret = 0;
Josef Bacika2c97902017-04-06 17:02:07 -04002093 bool put_dev = false;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002094
2095 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2096 return -EPERM;
2097
2098 if (!info->attrs[NBD_ATTR_INDEX]) {
2099 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
2100 return -EINVAL;
2101 }
2102 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2103 mutex_lock(&nbd_index_mutex);
2104 nbd = idr_find(&nbd_index_idr, index);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002105 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04002106 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002107 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
2108 index);
2109 return -EINVAL;
2110 }
Josef Bacikc6a47592017-04-06 17:02:06 -04002111 if (!refcount_inc_not_zero(&nbd->refs)) {
2112 mutex_unlock(&nbd_index_mutex);
2113 printk(KERN_ERR "nbd: device at index %d is going down\n",
2114 index);
2115 return -EINVAL;
2116 }
2117 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002118
2119 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2120 dev_err(nbd_to_dev(nbd),
2121 "not configured, cannot reconfigure\n");
Josef Bacikc6a47592017-04-06 17:02:06 -04002122 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002123 return -EINVAL;
2124 }
2125
2126 mutex_lock(&nbd->config_lock);
2127 config = nbd->config;
Xiubo Liec76a7b2019-09-17 17:26:05 +05302128 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002129 !nbd->task_recv) {
2130 dev_err(nbd_to_dev(nbd),
2131 "not configured, cannot reconfigure\n");
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002132 ret = -EINVAL;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002133 goto out;
2134 }
2135
Mike Christie4ddeaae82019-05-29 15:16:06 -05002136 ret = nbd_genl_size_set(info, nbd);
2137 if (ret)
2138 goto out;
2139
Mike Christie55313e92019-08-13 11:39:49 -05002140 if (info->attrs[NBD_ATTR_TIMEOUT])
2141 nbd_set_cmd_timeout(nbd,
2142 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
Josef Bacik560bc4b2017-04-06 17:02:04 -04002143 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
2144 config->dead_conn_timeout =
2145 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
2146 config->dead_conn_timeout *= HZ;
2147 }
Josef Bacika2c97902017-04-06 17:02:07 -04002148 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
2149 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
2150 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
Josef Bacikdc2b7762021-02-22 15:09:53 -05002151 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
2152 &nbd->flags))
Josef Bacika2c97902017-04-06 17:02:07 -04002153 put_dev = true;
2154 } else {
Josef Bacikdc2b7762021-02-22 15:09:53 -05002155 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
2156 &nbd->flags))
Josef Bacika2c97902017-04-06 17:02:07 -04002157 refcount_inc(&nbd->refs);
2158 }
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002159
2160 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302161 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002162 &config->runtime_flags);
2163 } else {
Xiubo Liec76a7b2019-09-17 17:26:05 +05302164 clear_bit(NBD_RT_DISCONNECT_ON_CLOSE,
Doron Roberts-Kedes08ba91e2018-06-15 14:05:32 -07002165 &config->runtime_flags);
2166 }
Josef Bacika2c97902017-04-06 17:02:07 -04002167 }
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002168
2169 if (info->attrs[NBD_ATTR_SOCKETS]) {
2170 struct nlattr *attr;
2171 int rem, fd;
2172
2173 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2174 rem) {
2175 struct nlattr *socks[NBD_SOCK_MAX+1];
2176
2177 if (nla_type(attr) != NBD_SOCK_ITEM) {
2178 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
2179 ret = -EINVAL;
2180 goto out;
2181 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002182 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2183 attr,
2184 nbd_sock_policy,
2185 info->extack);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002186 if (ret != 0) {
2187 printk(KERN_ERR "nbd: error processing sock list\n");
2188 ret = -EINVAL;
2189 goto out;
2190 }
2191 if (!socks[NBD_SOCK_FD])
2192 continue;
2193 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2194 ret = nbd_reconnect_socket(nbd, fd);
2195 if (ret) {
2196 if (ret == -ENOSPC)
2197 ret = 0;
2198 goto out;
2199 }
2200 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
2201 }
2202 }
2203out:
2204 mutex_unlock(&nbd->config_lock);
2205 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002206 nbd_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04002207 if (put_dev)
2208 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002209 return ret;
2210}
2211
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002212static const struct genl_small_ops nbd_connect_genl_ops[] = {
Josef Bacike46c7282017-04-06 17:02:00 -04002213 {
2214 .cmd = NBD_CMD_CONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002215 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002216 .doit = nbd_genl_connect,
2217 },
2218 {
2219 .cmd = NBD_CMD_DISCONNECT,
Johannes Bergef6243a2019-04-26 14:07:31 +02002220 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacike46c7282017-04-06 17:02:00 -04002221 .doit = nbd_genl_disconnect,
2222 },
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002223 {
2224 .cmd = NBD_CMD_RECONFIGURE,
Johannes Bergef6243a2019-04-26 14:07:31 +02002225 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacikb7aa3d32017-04-06 17:02:01 -04002226 .doit = nbd_genl_reconfigure,
2227 },
Josef Bacik47d902b2017-04-06 17:02:05 -04002228 {
2229 .cmd = NBD_CMD_STATUS,
Johannes Bergef6243a2019-04-26 14:07:31 +02002230 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Josef Bacik47d902b2017-04-06 17:02:05 -04002231 .doit = nbd_genl_status,
2232 },
Josef Bacike46c7282017-04-06 17:02:00 -04002233};
2234
Josef Bacik799f9a32017-04-06 17:02:02 -04002235static const struct genl_multicast_group nbd_mcast_grps[] = {
2236 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2237};
2238
Josef Bacike46c7282017-04-06 17:02:00 -04002239static struct genl_family nbd_genl_family __ro_after_init = {
2240 .hdrsize = 0,
2241 .name = NBD_GENL_FAMILY_NAME,
2242 .version = NBD_GENL_VERSION,
2243 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -07002244 .small_ops = nbd_connect_genl_ops,
2245 .n_small_ops = ARRAY_SIZE(nbd_connect_genl_ops),
Josef Bacike46c7282017-04-06 17:02:00 -04002246 .maxattr = NBD_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01002247 .policy = nbd_attr_policy,
Josef Bacik799f9a32017-04-06 17:02:02 -04002248 .mcgrps = nbd_mcast_grps,
2249 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
Josef Bacike46c7282017-04-06 17:02:00 -04002250};
2251
Josef Bacik47d902b2017-04-06 17:02:05 -04002252static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2253{
2254 struct nlattr *dev_opt;
2255 u8 connected = 0;
2256 int ret;
2257
2258 /* This is a little racey, but for status it's ok. The
2259 * reason we don't take a ref here is because we can't
2260 * take a ref in the index == -1 case as we would need
2261 * to put under the nbd_index_mutex, which could
2262 * deadlock if we are configured to remove ourselves
2263 * once we're disconnected.
2264 */
2265 if (refcount_read(&nbd->config_refs))
2266 connected = 1;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002267 dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM);
Josef Bacik47d902b2017-04-06 17:02:05 -04002268 if (!dev_opt)
2269 return -EMSGSIZE;
2270 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2271 if (ret)
2272 return -EMSGSIZE;
2273 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2274 connected);
2275 if (ret)
2276 return -EMSGSIZE;
2277 nla_nest_end(reply, dev_opt);
2278 return 0;
2279}
2280
2281static int status_cb(int id, void *ptr, void *data)
2282{
2283 struct nbd_device *nbd = ptr;
2284 return populate_nbd_status(nbd, (struct sk_buff *)data);
2285}
2286
2287static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2288{
2289 struct nlattr *dev_list;
2290 struct sk_buff *reply;
2291 void *reply_head;
2292 size_t msg_size;
2293 int index = -1;
2294 int ret = -ENOMEM;
2295
2296 if (info->attrs[NBD_ATTR_INDEX])
2297 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2298
2299 mutex_lock(&nbd_index_mutex);
2300
2301 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2302 nla_attr_size(sizeof(u8)));
2303 msg_size *= (index == -1) ? nbd_total_devices : 1;
2304
2305 reply = genlmsg_new(msg_size, GFP_KERNEL);
2306 if (!reply)
2307 goto out;
2308 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2309 NBD_CMD_STATUS);
2310 if (!reply_head) {
2311 nlmsg_free(reply);
2312 goto out;
2313 }
2314
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002315 dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST);
Josef Bacik47d902b2017-04-06 17:02:05 -04002316 if (index == -1) {
2317 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2318 if (ret) {
2319 nlmsg_free(reply);
2320 goto out;
2321 }
2322 } else {
2323 struct nbd_device *nbd;
2324 nbd = idr_find(&nbd_index_idr, index);
2325 if (nbd) {
2326 ret = populate_nbd_status(nbd, reply);
2327 if (ret) {
2328 nlmsg_free(reply);
2329 goto out;
2330 }
2331 }
2332 }
2333 nla_nest_end(reply, dev_list);
2334 genlmsg_end(reply, reply_head);
Li RongQingcd46eb82019-02-19 13:14:07 +08002335 ret = genlmsg_reply(reply, info);
Josef Bacik47d902b2017-04-06 17:02:05 -04002336out:
2337 mutex_unlock(&nbd_index_mutex);
2338 return ret;
2339}
2340
Josef Bacike46c7282017-04-06 17:02:00 -04002341static void nbd_connect_reply(struct genl_info *info, 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_reply(skb, info, &nbd_genl_family, 0,
2351 NBD_CMD_CONNECT);
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_reply(skb, info);
2363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Josef Bacik799f9a32017-04-06 17:02:02 -04002365static void nbd_mcast_index(int index)
2366{
2367 struct sk_buff *skb;
2368 void *msg_head;
2369 int ret;
2370
2371 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2372 if (!skb)
2373 return;
2374 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2375 NBD_CMD_LINK_DEAD);
2376 if (!msg_head) {
2377 nlmsg_free(skb);
2378 return;
2379 }
2380 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2381 if (ret) {
2382 nlmsg_free(skb);
2383 return;
2384 }
2385 genlmsg_end(skb, msg_head);
2386 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2387}
2388
2389static void nbd_dead_link_work(struct work_struct *work)
2390{
2391 struct link_dead_args *args = container_of(work, struct link_dead_args,
2392 work);
2393 nbd_mcast_index(args->index);
2394 kfree(args);
2395}
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397static int __init nbd_init(void)
2398{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 int i;
2400
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08002401 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002403 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02002404 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002405 return -EINVAL;
2406 }
2407
2408 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02002409 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002410 part_shift = fls(max_part);
2411
Namhyung Kim5988ce22011-05-28 14:44:46 +02002412 /*
2413 * Adjust max_part according to part_shift as it is exported
2414 * to user space so that user can know the max number of
2415 * partition kernel should be able to manage.
2416 *
2417 * Note that -1 is required because partition 0 is reserved
2418 * for the whole disk.
2419 */
2420 max_part = (1UL << part_shift) - 1;
2421 }
2422
Namhyung Kim3b271082011-05-28 14:44:46 +02002423 if ((1UL << part_shift) > DISK_MAX_PARTS)
2424 return -EINVAL;
2425
2426 if (nbds_max > 1UL << (MINORBITS - part_shift))
2427 return -EINVAL;
2428
Mike Christiee9e006f2019-08-04 14:10:06 -05002429 if (register_blkdev(NBD_MAJOR, "nbd"))
Josef Bacikb0d91112017-02-01 16:11:40 -05002430 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Josef Bacike46c7282017-04-06 17:02:00 -04002432 if (genl_register_family(&nbd_genl_family)) {
2433 unregister_blkdev(NBD_MAJOR, "nbd");
Josef Bacike46c7282017-04-06 17:02:00 -04002434 return -EINVAL;
2435 }
Markus Pargmann30d53d92015-08-17 08:20:06 +02002436 nbd_dbg_init();
2437
Josef Bacikb0d91112017-02-01 16:11:40 -05002438 mutex_lock(&nbd_index_mutex);
2439 for (i = 0; i < nbds_max; i++)
2440 nbd_dev_add(i);
2441 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05002443}
2444
2445static int nbd_exit_cb(int id, void *ptr, void *data)
2446{
Josef Bacikc6a47592017-04-06 17:02:06 -04002447 struct list_head *list = (struct list_head *)data;
Josef Bacikb0d91112017-02-01 16:11:40 -05002448 struct nbd_device *nbd = ptr;
Josef Bacikc6a47592017-04-06 17:02:06 -04002449
Josef Bacikc6a47592017-04-06 17:02:06 -04002450 list_add_tail(&nbd->list, list);
Josef Bacikb0d91112017-02-01 16:11:40 -05002451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
2454static void __exit nbd_cleanup(void)
2455{
Josef Bacikc6a47592017-04-06 17:02:06 -04002456 struct nbd_device *nbd;
2457 LIST_HEAD(del_list);
2458
Markus Pargmann30d53d92015-08-17 08:20:06 +02002459 nbd_dbg_close();
2460
Josef Bacikc6a47592017-04-06 17:02:06 -04002461 mutex_lock(&nbd_index_mutex);
2462 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2463 mutex_unlock(&nbd_index_mutex);
2464
Josef Bacik60ae36a2017-04-28 09:49:19 -04002465 while (!list_empty(&del_list)) {
2466 nbd = list_first_entry(&del_list, struct nbd_device, list);
2467 list_del_init(&nbd->list);
2468 if (refcount_read(&nbd->refs) != 1)
Josef Bacikc6a47592017-04-06 17:02:06 -04002469 printk(KERN_ERR "nbd: possibly leaking a device\n");
2470 nbd_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002471 }
2472
Josef Bacikb0d91112017-02-01 16:11:40 -05002473 idr_destroy(&nbd_index_idr);
Josef Bacike46c7282017-04-06 17:02:00 -04002474 genl_unregister_family(&nbd_genl_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
2478module_init(nbd_init);
2479module_exit(nbd_cleanup);
2480
2481MODULE_DESCRIPTION("Network Block Device");
2482MODULE_LICENSE("GPL");
2483
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07002484module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002485MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2486module_param(max_part, int, 0444);
Josef Bacik7a8362a2017-08-14 18:56:16 +00002487MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");