blob: 3ed1ef8ee5289d4202d0901f93138e9612bbdaff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Network block device - make block devices work over TCP
3 *
4 * Note that you can not swap over this thing, yet. Seems to work but
5 * deadlocks sometimes - you can not swap over TCP in general.
6 *
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
9 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070010 * This file is released under GPLv2 or later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070012 * (part of code stolen from loop.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/major.h>
16
17#include <linux/blkdev.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/sched.h>
Vlastimil Babkaf1083042017-05-08 15:59:53 -070021#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/fs.h>
23#include <linux/bio.h>
24#include <linux/stat.h>
25#include <linux/errno.h>
26#include <linux/file.h>
27#include <linux/ioctl.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020028#include <linux/mutex.h>
Herbert Xu4b2f0262006-01-06 00:09:47 -080029#include <linux/compiler.h>
30#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
Josef Bacikb0d91112017-02-01 16:11:40 -050047static DEFINE_IDR(nbd_index_idr);
48static DEFINE_MUTEX(nbd_index_mutex);
Josef Bacik47d902b2017-04-06 17:02:05 -040049static int nbd_total_devices = 0;
Josef Bacikb0d91112017-02-01 16:11:40 -050050
Josef Bacik9561a7a2016-11-22 14:04:40 -050051struct nbd_sock {
52 struct socket *sock;
53 struct mutex tx_lock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -040054 struct request *pending;
55 int sent;
Josef Bacikf3733242017-04-06 17:01:57 -040056 bool dead;
57 int fallback_index;
Josef Bacik799f9a32017-04-06 17:02:02 -040058 int cookie;
Josef Bacik9561a7a2016-11-22 14:04:40 -050059};
60
Josef Bacik5ea8d102017-04-06 17:01:58 -040061struct recv_thread_args {
62 struct work_struct work;
63 struct nbd_device *nbd;
64 int index;
65};
66
Josef Bacik799f9a32017-04-06 17:02:02 -040067struct link_dead_args {
68 struct work_struct work;
69 int index;
70};
71
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070072#define NBD_TIMEDOUT 0
73#define NBD_DISCONNECT_REQUESTED 1
Josef Bacik9561a7a2016-11-22 14:04:40 -050074#define NBD_DISCONNECTED 2
Josef Bacik5ea8d102017-04-06 17:01:58 -040075#define NBD_HAS_PID_FILE 3
Josef Bacike46c7282017-04-06 17:02:00 -040076#define NBD_HAS_CONFIG_REF 4
77#define NBD_BOUND 5
Josef Bacika2c97902017-04-06 17:02:07 -040078#define NBD_DESTROY_ON_DISCONNECT 6
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070079
Josef Bacik5ea8d102017-04-06 17:01:58 -040080struct nbd_config {
Markus Pargmann22d109c2015-08-17 08:20:09 +020081 u32 flags;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070082 unsigned long runtime_flags;
Josef Bacik560bc4b2017-04-06 17:02:04 -040083 u64 dead_conn_timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -040084
Josef Bacik9561a7a2016-11-22 14:04:40 -050085 struct nbd_sock **socks;
Josef Bacik9561a7a2016-11-22 14:04:40 -050086 int num_connections;
Josef Bacik560bc4b2017-04-06 17:02:04 -040087 atomic_t live_connections;
88 wait_queue_head_t conn_wait;
Josef Bacik5ea8d102017-04-06 17:01:58 -040089
Josef Bacik9561a7a2016-11-22 14:04:40 -050090 atomic_t recv_threads;
91 wait_queue_head_t recv_wq;
Josef Bacikef77b512016-12-02 16:19:12 -050092 loff_t blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020093 loff_t bytesize;
Markus Pargmann30d53d92015-08-17 08:20:06 +020094#if IS_ENABLED(CONFIG_DEBUG_FS)
95 struct dentry *dbg_dir;
96#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +020097};
98
Josef Bacik5ea8d102017-04-06 17:01:58 -040099struct nbd_device {
100 struct blk_mq_tag_set tag_set;
101
Josef Bacike46c7282017-04-06 17:02:00 -0400102 int index;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400103 refcount_t config_refs;
Josef Bacikc6a47592017-04-06 17:02:06 -0400104 refcount_t refs;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400105 struct nbd_config *config;
106 struct mutex config_lock;
107 struct gendisk *disk;
108
Josef Bacikc6a47592017-04-06 17:02:06 -0400109 struct list_head list;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400110 struct task_struct *task_recv;
111 struct task_struct *task_setup;
112};
113
Josef Bacikfd8383f2016-09-08 12:33:37 -0700114struct nbd_cmd {
115 struct nbd_device *nbd;
Josef Bacikf3733242017-04-06 17:01:57 -0400116 int index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400117 int cookie;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500118 struct completion send_complete;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200119 blk_status_t status;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700120};
121
Markus Pargmann30d53d92015-08-17 08:20:06 +0200122#if IS_ENABLED(CONFIG_DEBUG_FS)
123static struct dentry *nbd_dbg_dir;
124#endif
125
126#define nbd_name(nbd) ((nbd)->disk->disk_name)
127
Wanlong Gaof4507162012-03-28 14:42:51 -0700128#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Ingo van Lil9c7a4162006-07-01 04:36:36 -0700130static unsigned int nbds_max = 16;
Josef Bacik7a8362a2017-08-14 18:56:16 +0000131static int max_part = 16;
Josef Bacik124d6db2017-02-01 16:11:11 -0500132static struct workqueue_struct *recv_workqueue;
Josef Bacikb0d91112017-02-01 16:11:40 -0500133static int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Josef Bacik9442b732017-02-07 17:10:22 -0500135static int nbd_dev_dbg_init(struct nbd_device *nbd);
136static void nbd_dev_dbg_close(struct nbd_device *nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400137static void nbd_config_put(struct nbd_device *nbd);
Josef Bacike46c7282017-04-06 17:02:00 -0400138static void nbd_connect_reply(struct genl_info *info, int index);
Josef Bacik47d902b2017-04-06 17:02:05 -0400139static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
Josef Bacik799f9a32017-04-06 17:02:02 -0400140static void nbd_dead_link_work(struct work_struct *work);
Josef Bacik9442b732017-02-07 17:10:22 -0500141
Markus Pargmannd18509f2015-04-02 10:11:38 +0200142static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Markus Pargmannd18509f2015-04-02 10:11:38 +0200144 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static const char *nbdcmd_to_ascii(int cmd)
148{
149 switch (cmd) {
150 case NBD_CMD_READ: return "read";
151 case NBD_CMD_WRITE: return "write";
152 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800153 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700154 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 return "invalid";
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Josef Bacik5ea8d102017-04-06 17:01:58 -0400159static ssize_t pid_show(struct device *dev,
160 struct device_attribute *attr, char *buf)
161{
162 struct gendisk *disk = dev_to_disk(dev);
163 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
164
165 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
166}
167
Bhumika Goyaldfbde552017-08-21 17:13:08 +0530168static const struct device_attribute pid_attr = {
Joe Perches5657a812018-05-24 13:38:59 -0600169 .attr = { .name = "pid", .mode = 0444},
Josef Bacik5ea8d102017-04-06 17:01:58 -0400170 .show = pid_show,
171};
172
Josef Bacikc6a47592017-04-06 17:02:06 -0400173static void nbd_dev_remove(struct nbd_device *nbd)
174{
175 struct gendisk *disk = nbd->disk;
Josef Bacik8364da42018-05-16 14:51:17 -0400176 struct request_queue *q;
177
Josef Bacikc6a47592017-04-06 17:02:06 -0400178 if (disk) {
Josef Bacik8364da42018-05-16 14:51:17 -0400179 q = disk->queue;
Josef Bacikc6a47592017-04-06 17:02:06 -0400180 del_gendisk(disk);
Josef Bacik8364da42018-05-16 14:51:17 -0400181 blk_cleanup_queue(q);
Josef Bacikc6a47592017-04-06 17:02:06 -0400182 blk_mq_free_tag_set(&nbd->tag_set);
Josef Bacika2c97902017-04-06 17:02:07 -0400183 disk->private_data = NULL;
Josef Bacikc6a47592017-04-06 17:02:06 -0400184 put_disk(disk);
185 }
186 kfree(nbd);
187}
188
189static void nbd_put(struct nbd_device *nbd)
190{
191 if (refcount_dec_and_mutex_lock(&nbd->refs,
192 &nbd_index_mutex)) {
193 idr_remove(&nbd_index_idr, nbd->index);
194 mutex_unlock(&nbd_index_mutex);
195 nbd_dev_remove(nbd);
196 }
197}
198
Josef Bacik799f9a32017-04-06 17:02:02 -0400199static int nbd_disconnected(struct nbd_config *config)
Josef Bacikf3733242017-04-06 17:01:57 -0400200{
Josef Bacik799f9a32017-04-06 17:02:02 -0400201 return test_bit(NBD_DISCONNECTED, &config->runtime_flags) ||
202 test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
203}
204
205static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
206 int notify)
207{
208 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
209 struct link_dead_args *args;
210 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
211 if (args) {
212 INIT_WORK(&args->work, nbd_dead_link_work);
213 args->index = nbd->index;
214 queue_work(system_wq, &args->work);
215 }
216 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400217 if (!nsock->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400218 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600219 if (atomic_dec_return(&nbd->config->live_connections) == 0) {
220 if (test_and_clear_bit(NBD_DISCONNECT_REQUESTED,
221 &nbd->config->runtime_flags)) {
222 set_bit(NBD_DISCONNECTED,
223 &nbd->config->runtime_flags);
224 dev_info(nbd_to_dev(nbd),
225 "Disconnected due to user request.\n");
226 }
227 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400228 }
Josef Bacikf3733242017-04-06 17:01:57 -0400229 nsock->dead = true;
230 nsock->pending = NULL;
231 nsock->sent = 0;
232}
233
Josef Bacik29eaadc2017-04-06 17:01:59 -0400234static void nbd_size_clear(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200235{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400236 if (nbd->config->bytesize) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400237 set_capacity(nbd->disk, 0);
238 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
239 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200240}
241
Josef Bacik29eaadc2017-04-06 17:01:59 -0400242static void nbd_size_update(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200243{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400244 struct nbd_config *config = nbd->config;
Josef Bacik9e2b19672018-05-16 14:51:19 -0400245 struct block_device *bdev = bdget_disk(nbd->disk, 0);
246
Josef Bacik6df133a2018-05-23 13:35:59 -0400247 if (config->flags & NBD_FLAG_SEND_TRIM) {
248 nbd->disk->queue->limits.discard_granularity = config->blksize;
249 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
250 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400251 blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
252 blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400253 set_capacity(nbd->disk, config->bytesize >> 9);
Josef Bacik9e2b19672018-05-16 14:51:19 -0400254 if (bdev) {
255 if (bdev->bd_disk)
256 bd_set_size(bdev, config->bytesize);
257 else
258 bdev->bd_invalidated = 1;
259 bdput(bdev);
260 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200261 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
262}
263
Josef Bacik29eaadc2017-04-06 17:01:59 -0400264static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
265 loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200266{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400267 struct nbd_config *config = nbd->config;
268 config->blksize = blocksize;
269 config->bytesize = blocksize * nr_blocks;
Josef Bacikc3f7c932018-05-16 14:51:18 -0400270 if (nbd->task_recv != NULL)
271 nbd_size_update(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200272}
273
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200274static void nbd_complete_rq(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200276 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200278 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", cmd,
279 cmd->status ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200281 blk_mq_end_request(req, cmd->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Markus Pargmanne018e752015-04-02 10:11:39 +0200284/*
285 * Forcibly shutdown the socket causing all listeners to error
286 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200287static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700288{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400289 struct nbd_config *config = nbd->config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500290 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700291
Josef Bacik5ea8d102017-04-06 17:01:58 -0400292 if (config->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200293 return;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400294 if (test_and_set_bit(NBD_DISCONNECTED, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500295 return;
296
Josef Bacik5ea8d102017-04-06 17:01:58 -0400297 for (i = 0; i < config->num_connections; i++) {
298 struct nbd_sock *nsock = config->socks[i];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500299 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400300 nbd_mark_nsock_dead(nbd, nsock, 0);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500301 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100302 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500303 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700304}
305
Josef Bacik0eadf372016-09-08 12:33:40 -0700306static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
307 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700308{
Josef Bacik0eadf372016-09-08 12:33:40 -0700309 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
310 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400311 struct nbd_config *config;
Paul Clements7fdfd402007-10-16 23:27:37 -0700312
Josef Bacik5ea8d102017-04-06 17:01:58 -0400313 if (!refcount_inc_not_zero(&nbd->config_refs)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200314 cmd->status = BLK_STS_TIMEOUT;
Christoph Hellwige5eab012018-05-29 15:52:31 +0200315 goto done;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400316 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400317 config = nbd->config;
318
319 if (config->num_connections > 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400320 dev_err_ratelimited(nbd_to_dev(nbd),
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600321 "Connection timed out, retrying (%d/%d alive)\n",
322 atomic_read(&config->live_connections),
323 config->num_connections);
Josef Bacikf3733242017-04-06 17:01:57 -0400324 /*
325 * Hooray we have more connections, requeue this IO, the submit
326 * path will put it on a real connection.
327 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400328 if (config->socks && config->num_connections > 1) {
329 if (cmd->index < config->num_connections) {
Josef Bacikf3733242017-04-06 17:01:57 -0400330 struct nbd_sock *nsock =
Josef Bacik5ea8d102017-04-06 17:01:58 -0400331 config->socks[cmd->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400332 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400333 /* We can have multiple outstanding requests, so
334 * we don't want to mark the nsock dead if we've
335 * already reconnected with a new socket, so
336 * only mark it dead if its the same socket we
337 * were sent out on.
338 */
339 if (cmd->cookie == nsock->cookie)
340 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400341 mutex_unlock(&nsock->tx_lock);
342 }
Josef Bacikf3733242017-04-06 17:01:57 -0400343 blk_mq_requeue_request(req, true);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400344 nbd_config_put(nbd);
Christoph Hellwig66005932018-05-29 15:52:29 +0200345 return BLK_EH_DONE;
Josef Bacikf3733242017-04-06 17:01:57 -0400346 }
Josef Bacikf3733242017-04-06 17:01:57 -0400347 } else {
348 dev_err_ratelimited(nbd_to_dev(nbd),
349 "Connection timed out\n");
350 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400351 set_bit(NBD_TIMEDOUT, &config->runtime_flags);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200352 cmd->status = BLK_STS_IOERR;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500353 sock_shutdown(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400354 nbd_config_put(nbd);
Christoph Hellwige5eab012018-05-29 15:52:31 +0200355done:
356 blk_mq_complete_request(req);
357 return BLK_EH_DONE;
Paul Clements7fdfd402007-10-16 23:27:37 -0700358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * Send or receive packet.
362 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500363static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400364 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400366 struct nbd_config *config = nbd->config;
367 struct socket *sock = config->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int result;
369 struct msghdr msg;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700370 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700372 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500373 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200374 "Attempted %s on closed socket in sock_xmit\n",
375 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700376 return -EINVAL;
377 }
378
Al Viroc9f2b6a2015-11-12 05:09:35 -0500379 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500380
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700381 noreclaim_flag = memalloc_noreclaim_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700383 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 msg.msg_name = NULL;
385 msg.msg_namelen = 0;
386 msg.msg_control = NULL;
387 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
389
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200390 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500391 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200392 else
Al Viroc1696ca2015-11-12 04:51:19 -0500393 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (result <= 0) {
396 if (result == 0)
397 result = -EPIPE; /* short read */
398 break;
399 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400400 if (sent)
401 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500402 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700404 memalloc_noreclaim_restore(noreclaim_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 return result;
407}
408
Josef Bacik32e67a32017-10-24 15:57:18 -0400409/*
410 * Different settings for sk->sk_sndtimeo can result in different return values
411 * if there is a signal pending when we enter sendmsg, because reasons?
412 */
413static inline int was_interrupted(int result)
414{
415 return result == -ERESTARTSYS || result == -EINTR;
416}
417
Paul Clements7fdfd402007-10-16 23:27:37 -0700418/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500419static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700421 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400422 struct nbd_config *config = nbd->config;
423 struct nbd_sock *nsock = config->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500424 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500425 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
426 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
427 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900428 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700429 struct bio *bio;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200430 u32 type;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400431 u32 nbd_cmd_flags = 0;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500432 u32 tag = blk_mq_unique_tag(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400433 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200434
Al Viroc9f2b6a2015-11-12 05:09:35 -0500435 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
436
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100437 switch (req_op(req)) {
438 case REQ_OP_DISCARD:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200439 type = NBD_CMD_TRIM;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100440 break;
441 case REQ_OP_FLUSH:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200442 type = NBD_CMD_FLUSH;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100443 break;
444 case REQ_OP_WRITE:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200445 type = NBD_CMD_WRITE;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100446 break;
447 case REQ_OP_READ:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200448 type = NBD_CMD_READ;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100449 break;
450 default:
451 return -EIO;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100454 if (rq_data_dir(req) == WRITE &&
Josef Bacik5ea8d102017-04-06 17:01:58 -0400455 (config->flags & NBD_FLAG_READ_ONLY)) {
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100456 dev_err_ratelimited(disk_to_dev(nbd->disk),
457 "Write on read-only\n");
458 return -EIO;
459 }
460
Shaun McDowell685c9b22017-05-25 23:55:54 -0400461 if (req->cmd_flags & REQ_FUA)
462 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
463
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400464 /* We did a partial send previously, and we at least sent the whole
465 * request struct, so just go and send the rest of the pages in the
466 * request.
467 */
468 if (sent) {
469 if (sent >= sizeof(request)) {
470 skip = sent - sizeof(request);
471 goto send_pages;
472 }
473 iov_iter_advance(&from, sent);
474 }
Josef Bacikf3733242017-04-06 17:01:57 -0400475 cmd->index = index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400476 cmd->cookie = nsock->cookie;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400477 request.type = htonl(type | nbd_cmd_flags);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500478 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800479 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
480 request.len = htonl(size);
481 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500482 memcpy(request.handle, &tag, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Markus Pargmannd18509f2015-04-02 10:11:38 +0200484 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700485 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200486 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500487 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400488 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400490 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400491 /* If we havne't sent anything we can just return BUSY,
492 * however if we have sent something we need to make
493 * sure we only allow this req to be sent until we are
494 * completely done.
495 */
496 if (sent) {
497 nsock->pending = req;
498 nsock->sent = sent;
499 }
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200500 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400501 }
Josef Bacika897b662016-12-05 16:20:29 -0500502 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200503 "Send control failed (result %d)\n", result);
Josef Bacikf3733242017-04-06 17:01:57 -0400504 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400506send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700507 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400508 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700509
Jens Axboe429a7872016-11-17 12:30:37 -0700510 bio = req->bio;
511 while (bio) {
512 struct bio *next = bio->bi_next;
513 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800514 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700515
516 bio_for_each_segment(bvec, bio, iter) {
517 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500518 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700519
Markus Pargmannd18509f2015-04-02 10:11:38 +0200520 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700521 cmd, bvec.bv_len);
Al Viroc9f2b6a2015-11-12 05:09:35 -0500522 iov_iter_bvec(&from, ITER_BVEC | WRITE,
523 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400524 if (skip) {
525 if (skip >= iov_iter_count(&from)) {
526 skip -= iov_iter_count(&from);
527 continue;
528 }
529 iov_iter_advance(&from, skip);
530 skip = 0;
531 }
532 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200533 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400534 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400535 /* We've already sent the header, we
536 * have no choice but to set pending and
537 * return BUSY.
538 */
539 nsock->pending = req;
540 nsock->sent = sent;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200541 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400542 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700543 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200544 "Send data failed (result %d)\n",
545 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400546 return -EAGAIN;
Jens Axboe6c92e692007-08-16 13:43:12 +0200547 }
Jens Axboe429a7872016-11-17 12:30:37 -0700548 /*
549 * The completion might already have come in,
550 * so break for the last one instead of letting
551 * the iterator do it. This prevents use-after-free
552 * of the bio.
553 */
554 if (is_last)
555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Jens Axboe429a7872016-11-17 12:30:37 -0700557 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400559out:
560 nsock->pending = NULL;
561 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500566static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400568 struct nbd_config *config = nbd->config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int result;
570 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700571 struct nbd_cmd *cmd;
572 struct request *req = NULL;
573 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500574 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500575 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
576 struct iov_iter to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 reply.magic = 0;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500579 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400580 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (result <= 0) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400582 if (!nbd_disconnected(config))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500583 dev_err(disk_to_dev(nbd->disk),
584 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200585 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700587
588 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700589 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700590 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200591 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700592 }
593
Josef Bacik9561a7a2016-11-22 14:04:40 -0500594 memcpy(&tag, reply.handle, sizeof(u32));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800595
Josef Bacikfd8383f2016-09-08 12:33:37 -0700596 hwq = blk_mq_unique_tag_to_hwq(tag);
597 if (hwq < nbd->tag_set.nr_hw_queues)
598 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
599 blk_mq_unique_tag_to_tag(tag));
600 if (!req || !blk_mq_request_started(req)) {
601 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
602 tag, req);
603 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700605 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700607 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200608 ntohl(reply.error));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200609 cmd->status = BLK_STS_IOERR;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700610 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Josef Bacikfd8383f2016-09-08 12:33:37 -0700613 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200614 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200615 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800616 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200617
618 rq_for_each_segment(bvec, req, iter) {
Al Viroc9f2b6a2015-11-12 05:09:35 -0500619 iov_iter_bvec(&to, ITER_BVEC | READ,
620 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400621 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200622 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700623 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200624 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400625 /*
626 * If we've disconnected or we only have 1
627 * connection then we need to make sure we
628 * complete this request, otherwise error out
629 * and let the timeout stuff handle resubmitting
630 * this request onto another connection.
631 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400632 if (nbd_disconnected(config) ||
633 config->num_connections <= 1) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200634 cmd->status = BLK_STS_IOERR;
Josef Bacikf3733242017-04-06 17:01:57 -0400635 return cmd;
636 }
637 return ERR_PTR(-EIO);
Jens Axboe6c92e692007-08-16 13:43:12 +0200638 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200639 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700640 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500642 } else {
643 /* See the comment in nbd_queue_rq. */
644 wait_for_completion(&cmd->send_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700646 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Josef Bacik9561a7a2016-11-22 14:04:40 -0500649static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500651 struct recv_thread_args *args = container_of(work,
652 struct recv_thread_args,
653 work);
654 struct nbd_device *nbd = args->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400655 struct nbd_config *config = nbd->config;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700656 struct nbd_cmd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Markus Pargmann19391832015-08-17 08:20:03 +0200658 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500659 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700660 if (IS_ERR(cmd)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400661 struct nbd_sock *nsock = config->socks[args->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400662
663 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400664 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400665 mutex_unlock(&nsock->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200666 break;
667 }
668
Christoph Hellwig08e00292017-04-20 16:03:09 +0200669 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd));
Markus Pargmann19391832015-08-17 08:20:03 +0200670 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400671 atomic_dec(&config->recv_threads);
672 wake_up(&config->recv_wq);
673 nbd_config_put(nbd);
674 kfree(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Josef Bacikfd8383f2016-09-08 12:33:37 -0700677static void nbd_clear_req(struct request *req, void *data, bool reserved)
678{
Christoph Hellwigd250bf42018-05-30 18:51:00 +0200679 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700680
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200681 cmd->status = BLK_STS_IOERR;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200682 blk_mq_complete_request(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700683}
684
Wanlong Gaof4507162012-03-28 14:42:51 -0700685static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300687 blk_mq_quiesce_queue(nbd->disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700688 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300689 blk_mq_unquiesce_queue(nbd->disk->queue);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200690 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Josef Bacikf3733242017-04-06 17:01:57 -0400693static int find_fallback(struct nbd_device *nbd, int index)
694{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400695 struct nbd_config *config = nbd->config;
Josef Bacikf3733242017-04-06 17:01:57 -0400696 int new_index = -1;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400697 struct nbd_sock *nsock = config->socks[index];
Josef Bacikf3733242017-04-06 17:01:57 -0400698 int fallback = nsock->fallback_index;
699
Josef Bacik5ea8d102017-04-06 17:01:58 -0400700 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
Josef Bacikf3733242017-04-06 17:01:57 -0400701 return new_index;
702
Josef Bacik5ea8d102017-04-06 17:01:58 -0400703 if (config->num_connections <= 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400704 dev_err_ratelimited(disk_to_dev(nbd->disk),
705 "Attempted send on invalid socket\n");
706 return new_index;
707 }
708
Josef Bacik5ea8d102017-04-06 17:01:58 -0400709 if (fallback >= 0 && fallback < config->num_connections &&
710 !config->socks[fallback]->dead)
Josef Bacikf3733242017-04-06 17:01:57 -0400711 return fallback;
712
713 if (nsock->fallback_index < 0 ||
Josef Bacik5ea8d102017-04-06 17:01:58 -0400714 nsock->fallback_index >= config->num_connections ||
715 config->socks[nsock->fallback_index]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400716 int i;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400717 for (i = 0; i < config->num_connections; i++) {
Josef Bacikf3733242017-04-06 17:01:57 -0400718 if (i == index)
719 continue;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400720 if (!config->socks[i]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400721 new_index = i;
722 break;
723 }
724 }
725 nsock->fallback_index = new_index;
726 if (new_index < 0) {
727 dev_err_ratelimited(disk_to_dev(nbd->disk),
728 "Dead connection, failed to find a fallback\n");
729 return new_index;
730 }
731 }
732 new_index = nsock->fallback_index;
733 return new_index;
734}
Paul Clements7fdfd402007-10-16 23:27:37 -0700735
Josef Bacik560bc4b2017-04-06 17:02:04 -0400736static int wait_for_reconnect(struct nbd_device *nbd)
737{
738 struct nbd_config *config = nbd->config;
739 if (!config->dead_conn_timeout)
740 return 0;
741 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
742 return 0;
Kevin Vigor5e3c3a72018-05-30 10:45:11 -0600743 return wait_event_timeout(config->conn_wait,
744 atomic_read(&config->live_connections) > 0,
745 config->dead_conn_timeout) > 0;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400746}
747
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400748static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700749{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700750 struct request *req = blk_mq_rq_from_pdu(cmd);
751 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400752 struct nbd_config *config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500753 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400754 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700755
Josef Bacik5ea8d102017-04-06 17:01:58 -0400756 if (!refcount_inc_not_zero(&nbd->config_refs)) {
757 dev_err_ratelimited(disk_to_dev(nbd->disk),
758 "Socks array is empty\n");
Josef Bacik6a468d52017-11-06 16:11:58 -0500759 blk_mq_start_request(req);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400760 return -EINVAL;
761 }
762 config = nbd->config;
763
764 if (index >= config->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500765 dev_err_ratelimited(disk_to_dev(nbd->disk),
766 "Attempted send on invalid socket\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -0400767 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500768 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400769 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500770 }
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200771 cmd->status = BLK_STS_OK;
Josef Bacikf3733242017-04-06 17:01:57 -0400772again:
Josef Bacik5ea8d102017-04-06 17:01:58 -0400773 nsock = config->socks[index];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500774 mutex_lock(&nsock->tx_lock);
Josef Bacikf3733242017-04-06 17:01:57 -0400775 if (nsock->dead) {
Josef Bacik560bc4b2017-04-06 17:02:04 -0400776 int old_index = index;
Josef Bacikf3733242017-04-06 17:01:57 -0400777 index = find_fallback(nbd, index);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500778 mutex_unlock(&nsock->tx_lock);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400779 if (index < 0) {
780 if (wait_for_reconnect(nbd)) {
781 index = old_index;
782 goto again;
783 }
784 /* All the sockets should already be down at this point,
785 * we just want to make sure that DISCONNECTED is set so
786 * any requests that come in that were queue'ed waiting
787 * for the reconnect timer don't trigger the timer again
788 * and instead just error out.
789 */
790 sock_shutdown(nbd);
791 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500792 blk_mq_start_request(req);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400793 return -EIO;
794 }
Josef Bacikf3733242017-04-06 17:01:57 -0400795 goto again;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700796 }
797
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400798 /* Handle the case that we have a pending request that was partially
799 * transmitted that _has_ to be serviced first. We need to call requeue
800 * here so that it gets put _after_ the request that is already on the
801 * dispatch list.
802 */
Josef Bacik6a468d52017-11-06 16:11:58 -0500803 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400804 if (unlikely(nsock->pending && nsock->pending != req)) {
805 blk_mq_requeue_request(req, true);
806 ret = 0;
807 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700808 }
Josef Bacikf3733242017-04-06 17:01:57 -0400809 /*
810 * Some failures are related to the link going down, so anything that
811 * returns EAGAIN can be retried on a different socket.
812 */
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400813 ret = nbd_send_cmd(nbd, cmd, index);
Josef Bacikf3733242017-04-06 17:01:57 -0400814 if (ret == -EAGAIN) {
815 dev_err_ratelimited(disk_to_dev(nbd->disk),
Josef Bacik6a468d52017-11-06 16:11:58 -0500816 "Request send failed, requeueing\n");
Josef Bacik799f9a32017-04-06 17:02:02 -0400817 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacik6a468d52017-11-06 16:11:58 -0500818 blk_mq_requeue_request(req, true);
819 ret = 0;
Josef Bacikf3733242017-04-06 17:01:57 -0400820 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400821out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500822 mutex_unlock(&nsock->tx_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400823 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400824 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700825}
826
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200827static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700828 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700829{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700830 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400831 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700832
Josef Bacik9561a7a2016-11-22 14:04:40 -0500833 /*
834 * Since we look at the bio's to send the request over the network we
835 * need to make sure the completion work doesn't mark this request done
836 * before we are done doing our send. This keeps us from dereferencing
837 * freed data if we have particularly fast completions (ie we get the
838 * completion before we exit sock_xmit on the last bvec) or in the case
839 * that the server is misbehaving (or there was an error) before we're
840 * done sending everything over the wire.
841 */
842 init_completion(&cmd->send_complete);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400843
844 /* We can be called directly from the user space process, which means we
845 * could possibly have signals pending so our sendmsg will fail. In
846 * this case we need to return that we are busy, otherwise error out as
847 * appropriate.
848 */
849 ret = nbd_handle_cmd(cmd, hctx->queue_num);
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400850 if (ret < 0)
851 ret = BLK_STS_IOERR;
852 else if (!ret)
853 ret = BLK_STS_OK;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500854 complete(&cmd->send_complete);
855
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400856 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Josef Bacike46c7282017-04-06 17:02:00 -0400859static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
860 bool netlink)
Markus Pargmann23272a672015-10-29 11:51:16 +0100861{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400862 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -0500863 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500864 struct nbd_sock **socks;
865 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -0500866 int err;
867
868 sock = sockfd_lookup(arg, &err);
869 if (!sock)
870 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100871
Josef Bacike46c7282017-04-06 17:02:00 -0400872 if (!netlink && !nbd->task_setup &&
873 !test_bit(NBD_BOUND, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500874 nbd->task_setup = current;
Josef Bacike46c7282017-04-06 17:02:00 -0400875
876 if (!netlink &&
877 (nbd->task_setup != current ||
878 test_bit(NBD_BOUND, &config->runtime_flags))) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500879 dev_err(disk_to_dev(nbd->disk),
880 "Device being setup by another task");
Josef Bacik9b1355d2017-04-06 17:01:56 -0400881 sockfd_put(sock);
Josef Bacike46c7282017-04-06 17:02:00 -0400882 return -EBUSY;
Markus Pargmann23272a672015-10-29 11:51:16 +0100883 }
884
Josef Bacik5ea8d102017-04-06 17:01:58 -0400885 socks = krealloc(config->socks, (config->num_connections + 1) *
Josef Bacik9561a7a2016-11-22 14:04:40 -0500886 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400887 if (!socks) {
888 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500889 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400890 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500891 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400892 if (!nsock) {
893 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500894 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400895 }
Markus Pargmann23272a672015-10-29 11:51:16 +0100896
Josef Bacik5ea8d102017-04-06 17:01:58 -0400897 config->socks = socks;
Markus Pargmann23272a672015-10-29 11:51:16 +0100898
Josef Bacikf3733242017-04-06 17:01:57 -0400899 nsock->fallback_index = -1;
900 nsock->dead = false;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500901 mutex_init(&nsock->tx_lock);
902 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400903 nsock->pending = NULL;
904 nsock->sent = 0;
Josef Bacik799f9a32017-04-06 17:02:02 -0400905 nsock->cookie = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400906 socks[config->num_connections++] = nsock;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400907 atomic_inc(&config->live_connections);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500908
909 return 0;
Markus Pargmann23272a672015-10-29 11:51:16 +0100910}
911
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400912static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
913{
914 struct nbd_config *config = nbd->config;
915 struct socket *sock, *old;
916 struct recv_thread_args *args;
917 int i;
918 int err;
919
920 sock = sockfd_lookup(arg, &err);
921 if (!sock)
922 return err;
923
924 args = kzalloc(sizeof(*args), GFP_KERNEL);
925 if (!args) {
926 sockfd_put(sock);
927 return -ENOMEM;
928 }
929
930 for (i = 0; i < config->num_connections; i++) {
931 struct nbd_sock *nsock = config->socks[i];
932
933 if (!nsock->dead)
934 continue;
935
936 mutex_lock(&nsock->tx_lock);
937 if (!nsock->dead) {
938 mutex_unlock(&nsock->tx_lock);
939 continue;
940 }
941 sk_set_memalloc(sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -0400942 if (nbd->tag_set.timeout)
943 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400944 atomic_inc(&config->recv_threads);
945 refcount_inc(&nbd->config_refs);
946 old = nsock->sock;
947 nsock->fallback_index = -1;
948 nsock->sock = sock;
949 nsock->dead = false;
950 INIT_WORK(&args->work, recv_work);
951 args->index = i;
952 args->nbd = nbd;
Josef Bacik799f9a32017-04-06 17:02:02 -0400953 nsock->cookie++;
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400954 mutex_unlock(&nsock->tx_lock);
955 sockfd_put(old);
956
Josef Bacik7a362ea2017-07-25 13:31:19 -0400957 clear_bit(NBD_DISCONNECTED, &config->runtime_flags);
958
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400959 /* We take the tx_mutex in an error path in the recv_work, so we
960 * need to queue_work outside of the tx_mutex.
961 */
962 queue_work(recv_workqueue, &args->work);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400963
964 atomic_inc(&config->live_connections);
965 wake_up(&config->conn_wait);
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400966 return 0;
967 }
968 sockfd_put(sock);
969 kfree(args);
970 return -ENOSPC;
971}
972
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100973static void nbd_bdev_reset(struct block_device *bdev)
974{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -0400975 if (bdev->bd_openers > 1)
976 return;
Josef Bacik29eaadc2017-04-06 17:01:59 -0400977 bd_set_size(bdev, 0);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100978}
979
Josef Bacik29eaadc2017-04-06 17:01:59 -0400980static void nbd_parse_flags(struct nbd_device *nbd)
Markus Pargmannd02cf532015-10-29 12:06:15 +0100981{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400982 struct nbd_config *config = nbd->config;
983 if (config->flags & NBD_FLAG_READ_ONLY)
Josef Bacik29eaadc2017-04-06 17:01:59 -0400984 set_disk_ro(nbd->disk, true);
985 else
986 set_disk_ro(nbd->disk, false);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400987 if (config->flags & NBD_FLAG_SEND_TRIM)
Bart Van Assche8b904b52018-03-07 17:10:10 -0800988 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Shaun McDowell685c9b22017-05-25 23:55:54 -0400989 if (config->flags & NBD_FLAG_SEND_FLUSH) {
990 if (config->flags & NBD_FLAG_SEND_FUA)
991 blk_queue_write_cache(nbd->disk->queue, true, true);
992 else
993 blk_queue_write_cache(nbd->disk->queue, true, false);
994 }
Markus Pargmannd02cf532015-10-29 12:06:15 +0100995 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600996 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100997}
998
Josef Bacik9561a7a2016-11-22 14:04:40 -0500999static void send_disconnects(struct nbd_device *nbd)
1000{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001001 struct nbd_config *config = nbd->config;
Al Viroc9f2b6a2015-11-12 05:09:35 -05001002 struct nbd_request request = {
1003 .magic = htonl(NBD_REQUEST_MAGIC),
1004 .type = htonl(NBD_CMD_DISC),
1005 };
1006 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1007 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -05001008 int i, ret;
1009
Josef Bacik5ea8d102017-04-06 17:01:58 -04001010 for (i = 0; i < config->num_connections; i++) {
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001011 struct nbd_sock *nsock = config->socks[i];
1012
Al Viroc9f2b6a2015-11-12 05:09:35 -05001013 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001014 mutex_lock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001015 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001016 if (ret <= 0)
1017 dev_err(disk_to_dev(nbd->disk),
1018 "Send disconnect failed %d\n", ret);
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001019 mutex_unlock(&nsock->tx_lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001020 }
1021}
1022
Josef Bacik29eaadc2017-04-06 17:01:59 -04001023static int nbd_disconnect(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001024{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001025 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001026
Josef Bacik5ea8d102017-04-06 17:01:58 -04001027 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Josef Bacik2e134562017-07-21 10:48:13 -04001028 set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
1029 send_disconnects(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001030 return 0;
1031}
1032
Josef Bacik29eaadc2017-04-06 17:01:59 -04001033static void nbd_clear_sock(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001034{
1035 sock_shutdown(nbd);
1036 nbd_clear_que(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001037 nbd->task_setup = NULL;
Josef Bacik9442b732017-02-07 17:10:22 -05001038}
1039
Josef Bacik5ea8d102017-04-06 17:01:58 -04001040static void nbd_config_put(struct nbd_device *nbd)
1041{
1042 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1043 &nbd->config_lock)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001044 struct nbd_config *config = nbd->config;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001045 nbd_dev_dbg_close(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001046 nbd_size_clear(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001047 if (test_and_clear_bit(NBD_HAS_PID_FILE,
1048 &config->runtime_flags))
1049 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1050 nbd->task_recv = NULL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001051 nbd_clear_sock(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001052 if (config->num_connections) {
1053 int i;
1054 for (i = 0; i < config->num_connections; i++) {
1055 sockfd_put(config->socks[i]->sock);
1056 kfree(config->socks[i]);
1057 }
1058 kfree(config->socks);
1059 }
Ilya Dryomovfa976532017-05-23 17:49:55 +02001060 kfree(nbd->config);
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001061 nbd->config = NULL;
1062
1063 nbd->tag_set.timeout = 0;
Josef Bacik6df133a2018-05-23 13:35:59 -04001064 nbd->disk->queue->limits.discard_granularity = 0;
1065 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001066 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Josef Bacika2c97902017-04-06 17:02:07 -04001067
Josef Bacik5ea8d102017-04-06 17:01:58 -04001068 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001069 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001070 module_put(THIS_MODULE);
1071 }
1072}
1073
Josef Bacike46c7282017-04-06 17:02:00 -04001074static int nbd_start_device(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001075{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001076 struct nbd_config *config = nbd->config;
1077 int num_connections = config->num_connections;
Josef Bacik9442b732017-02-07 17:10:22 -05001078 int error = 0, i;
1079
1080 if (nbd->task_recv)
1081 return -EBUSY;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001082 if (!config->socks)
Josef Bacik9442b732017-02-07 17:10:22 -05001083 return -EINVAL;
1084 if (num_connections > 1 &&
Josef Bacik5ea8d102017-04-06 17:01:58 -04001085 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
Josef Bacik9442b732017-02-07 17:10:22 -05001086 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001087 return -EINVAL;
Josef Bacik9442b732017-02-07 17:10:22 -05001088 }
1089
Josef Bacik5ea8d102017-04-06 17:01:58 -04001090 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
Josef Bacik9442b732017-02-07 17:10:22 -05001091 nbd->task_recv = current;
Josef Bacik9442b732017-02-07 17:10:22 -05001092
Josef Bacik29eaadc2017-04-06 17:01:59 -04001093 nbd_parse_flags(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001094
1095 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1096 if (error) {
1097 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001098 return error;
Josef Bacik9442b732017-02-07 17:10:22 -05001099 }
Josef Bacik29eaadc2017-04-06 17:01:59 -04001100 set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
Josef Bacik9442b732017-02-07 17:10:22 -05001101
1102 nbd_dev_dbg_init(nbd);
1103 for (i = 0; i < num_connections; i++) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001104 struct recv_thread_args *args;
1105
1106 args = kzalloc(sizeof(*args), GFP_KERNEL);
1107 if (!args) {
1108 sock_shutdown(nbd);
1109 return -ENOMEM;
1110 }
1111 sk_set_memalloc(config->socks[i]->sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001112 if (nbd->tag_set.timeout)
1113 config->socks[i]->sock->sk->sk_sndtimeo =
1114 nbd->tag_set.timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001115 atomic_inc(&config->recv_threads);
1116 refcount_inc(&nbd->config_refs);
1117 INIT_WORK(&args->work, recv_work);
1118 args->nbd = nbd;
1119 args->index = i;
1120 queue_work(recv_workqueue, &args->work);
Josef Bacik9442b732017-02-07 17:10:22 -05001121 }
Josef Bacik639812a2017-10-09 13:12:10 -04001122 nbd_size_update(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001123 return error;
1124}
1125
1126static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1127{
1128 struct nbd_config *config = nbd->config;
1129 int ret;
1130
1131 ret = nbd_start_device(nbd);
1132 if (ret)
1133 return ret;
1134
Josef Bacike46c7282017-04-06 17:02:00 -04001135 if (max_part)
1136 bdev->bd_invalidated = 1;
1137 mutex_unlock(&nbd->config_lock);
1138 ret = wait_event_interruptible(config->recv_wq,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001139 atomic_read(&config->recv_threads) == 0);
Josef Bacike46c7282017-04-06 17:02:00 -04001140 if (ret)
Josef Bacik5ea8d102017-04-06 17:01:58 -04001141 sock_shutdown(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001142 mutex_lock(&nbd->config_lock);
Josef Bacik76aa1d32018-05-16 14:51:22 -04001143 nbd_bdev_reset(bdev);
Josef Bacik9442b732017-02-07 17:10:22 -05001144 /* user requested, ignore socket errors */
Josef Bacik5ea8d102017-04-06 17:01:58 -04001145 if (test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001146 ret = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001147 if (test_bit(NBD_TIMEDOUT, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001148 ret = -ETIMEDOUT;
1149 return ret;
Josef Bacik9442b732017-02-07 17:10:22 -05001150}
Markus Pargmann30d53d92015-08-17 08:20:06 +02001151
Josef Bacik29eaadc2017-04-06 17:01:59 -04001152static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1153 struct block_device *bdev)
1154{
Josef Bacik2516ab12017-04-06 17:02:03 -04001155 sock_shutdown(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001156 kill_bdev(bdev);
1157 nbd_bdev_reset(bdev);
Josef Bacike46c7282017-04-06 17:02:00 -04001158 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1159 &nbd->config->runtime_flags))
1160 nbd_config_put(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001161}
1162
Josef Bacik9561a7a2016-11-22 14:04:40 -05001163/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -07001164static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -07001165 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001167 struct nbd_config *config = nbd->config;
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -05001170 case NBD_DISCONNECT:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001171 return nbd_disconnect(nbd);
Markus Pargmann23272a672015-10-29 11:51:16 +01001172 case NBD_CLEAR_SOCK:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001173 nbd_clear_sock_ioctl(nbd, bdev);
1174 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001175 case NBD_SET_SOCK:
Josef Bacike46c7282017-04-06 17:02:00 -04001176 return nbd_add_socket(nbd, arg, false);
Josef Bacik9442b732017-02-07 17:10:22 -05001177 case NBD_SET_BLKSIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001178 nbd_size_set(nbd, arg,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001179 div_s64(config->bytesize, arg));
Josef Bacike5445412017-02-13 10:39:47 -05001180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 case NBD_SET_SIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001182 nbd_size_set(nbd, config->blksize,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001183 div_s64(arg, config->blksize));
Josef Bacike5445412017-02-13 10:39:47 -05001184 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +02001185 case NBD_SET_SIZE_BLOCKS:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001186 nbd_size_set(nbd, config->blksize, arg);
Josef Bacike5445412017-02-13 10:39:47 -05001187 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -07001188 case NBD_SET_TIMEOUT:
Josef Bacikf8586852017-03-24 14:08:28 -04001189 if (arg) {
1190 nbd->tag_set.timeout = arg * HZ;
1191 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
1192 }
Paul Clements7fdfd402007-10-16 23:27:37 -07001193 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001194
Paul Clements2f012502012-10-04 17:16:15 -07001195 case NBD_SET_FLAGS:
Josef Bacik5ea8d102017-04-06 17:01:58 -04001196 config->flags = arg;
Paul Clements2f012502012-10-04 17:16:15 -07001197 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001198 case NBD_DO_IT:
Josef Bacike46c7282017-04-06 17:02:00 -04001199 return nbd_start_device_ioctl(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -08001201 /*
1202 * This is for compatibility only. The queue is always cleared
1203 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -07001207 /*
1208 * For compatibility only, we no longer keep a list of
1209 * outstanding requests.
1210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return 0;
1212 }
Pavel Machek1a2ad212009-04-02 16:58:41 -07001213 return -ENOTTY;
1214}
1215
1216static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1217 unsigned int cmd, unsigned long arg)
1218{
Wanlong Gaof4507162012-03-28 14:42:51 -07001219 struct nbd_device *nbd = bdev->bd_disk->private_data;
Josef Bacike46c7282017-04-06 17:02:00 -04001220 struct nbd_config *config = nbd->config;
1221 int error = -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001222
1223 if (!capable(CAP_SYS_ADMIN))
1224 return -EPERM;
1225
Josef Bacik1dae69b2017-05-05 22:25:18 -04001226 /* The block layer will pass back some non-nbd ioctls in case we have
1227 * special handling for them, but we don't so just return an error.
1228 */
1229 if (_IOC_TYPE(cmd) != 0xab)
1230 return -EINVAL;
1231
Josef Bacik9561a7a2016-11-22 14:04:40 -05001232 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001233
1234 /* Don't allow ioctl operations on a nbd device that was created with
1235 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1236 */
1237 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1238 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1239 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1240 else
1241 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -05001242 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -07001243 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Josef Bacik5ea8d102017-04-06 17:01:58 -04001246static struct nbd_config *nbd_alloc_config(void)
1247{
1248 struct nbd_config *config;
1249
1250 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1251 if (!config)
1252 return NULL;
1253 atomic_set(&config->recv_threads, 0);
1254 init_waitqueue_head(&config->recv_wq);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001255 init_waitqueue_head(&config->conn_wait);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001256 config->blksize = 1024;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001257 atomic_set(&config->live_connections, 0);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001258 try_module_get(THIS_MODULE);
1259 return config;
1260}
1261
1262static int nbd_open(struct block_device *bdev, fmode_t mode)
1263{
1264 struct nbd_device *nbd;
1265 int ret = 0;
1266
1267 mutex_lock(&nbd_index_mutex);
1268 nbd = bdev->bd_disk->private_data;
1269 if (!nbd) {
1270 ret = -ENXIO;
1271 goto out;
1272 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001273 if (!refcount_inc_not_zero(&nbd->refs)) {
1274 ret = -ENXIO;
1275 goto out;
1276 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001277 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1278 struct nbd_config *config;
1279
1280 mutex_lock(&nbd->config_lock);
1281 if (refcount_inc_not_zero(&nbd->config_refs)) {
1282 mutex_unlock(&nbd->config_lock);
1283 goto out;
1284 }
1285 config = nbd->config = nbd_alloc_config();
1286 if (!config) {
1287 ret = -ENOMEM;
1288 mutex_unlock(&nbd->config_lock);
1289 goto out;
1290 }
1291 refcount_set(&nbd->config_refs, 1);
Josef Bacikc6a47592017-04-06 17:02:06 -04001292 refcount_inc(&nbd->refs);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001293 mutex_unlock(&nbd->config_lock);
Josef Bacikfe1f9e62018-05-16 14:51:21 -04001294 bdev->bd_invalidated = 1;
1295 } else if (nbd_disconnected(nbd->config)) {
1296 bdev->bd_invalidated = 1;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001297 }
1298out:
1299 mutex_unlock(&nbd_index_mutex);
1300 return ret;
1301}
1302
1303static void nbd_release(struct gendisk *disk, fmode_t mode)
1304{
1305 struct nbd_device *nbd = disk->private_data;
1306 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001307 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001308}
1309
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001310static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 .owner = THIS_MODULE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001313 .open = nbd_open,
1314 .release = nbd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001315 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -05001316 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317};
1318
Markus Pargmann30d53d92015-08-17 08:20:06 +02001319#if IS_ENABLED(CONFIG_DEBUG_FS)
1320
1321static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1322{
1323 struct nbd_device *nbd = s->private;
1324
1325 if (nbd->task_recv)
1326 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +02001327
1328 return 0;
1329}
1330
1331static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1332{
1333 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1334}
1335
1336static const struct file_operations nbd_dbg_tasks_ops = {
1337 .open = nbd_dbg_tasks_open,
1338 .read = seq_read,
1339 .llseek = seq_lseek,
1340 .release = single_release,
1341};
1342
1343static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1344{
1345 struct nbd_device *nbd = s->private;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001346 u32 flags = nbd->config->flags;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001347
1348 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1349
1350 seq_puts(s, "Known flags:\n");
1351
1352 if (flags & NBD_FLAG_HAS_FLAGS)
1353 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1354 if (flags & NBD_FLAG_READ_ONLY)
1355 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1356 if (flags & NBD_FLAG_SEND_FLUSH)
1357 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
Shaun McDowell685c9b22017-05-25 23:55:54 -04001358 if (flags & NBD_FLAG_SEND_FUA)
1359 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
Markus Pargmann30d53d92015-08-17 08:20:06 +02001360 if (flags & NBD_FLAG_SEND_TRIM)
1361 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1362
1363 return 0;
1364}
1365
1366static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1367{
1368 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1369}
1370
1371static const struct file_operations nbd_dbg_flags_ops = {
1372 .open = nbd_dbg_flags_open,
1373 .read = seq_read,
1374 .llseek = seq_lseek,
1375 .release = single_release,
1376};
1377
1378static int nbd_dev_dbg_init(struct nbd_device *nbd)
1379{
1380 struct dentry *dir;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001381 struct nbd_config *config = nbd->config;
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001382
1383 if (!nbd_dbg_dir)
1384 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001385
1386 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001387 if (!dir) {
1388 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1389 nbd_name(nbd));
1390 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001391 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001392 config->dbg_dir = dir;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001393
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001394 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001395 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -07001396 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001397 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -04001398 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001399
1400 return 0;
1401}
1402
1403static void nbd_dev_dbg_close(struct nbd_device *nbd)
1404{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001405 debugfs_remove_recursive(nbd->config->dbg_dir);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001406}
1407
1408static int nbd_dbg_init(void)
1409{
1410 struct dentry *dbg_dir;
1411
1412 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001413 if (!dbg_dir)
1414 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001415
1416 nbd_dbg_dir = dbg_dir;
1417
1418 return 0;
1419}
1420
1421static void nbd_dbg_close(void)
1422{
1423 debugfs_remove_recursive(nbd_dbg_dir);
1424}
1425
1426#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1427
1428static int nbd_dev_dbg_init(struct nbd_device *nbd)
1429{
1430 return 0;
1431}
1432
1433static void nbd_dev_dbg_close(struct nbd_device *nbd)
1434{
1435}
1436
1437static int nbd_dbg_init(void)
1438{
1439 return 0;
1440}
1441
1442static void nbd_dbg_close(void)
1443{
1444}
1445
1446#endif
1447
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001448static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1449 unsigned int hctx_idx, unsigned int numa_node)
Josef Bacikfd8383f2016-09-08 12:33:37 -07001450{
1451 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001452 cmd->nbd = set->driver_data;
Josef Bacikfd8383f2016-09-08 12:33:37 -07001453 return 0;
1454}
1455
Eric Biggersf363b082017-03-30 13:39:16 -07001456static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001457 .queue_rq = nbd_queue_rq,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +02001458 .complete = nbd_complete_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001459 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001460 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001461};
1462
Josef Bacikb0d91112017-02-01 16:11:40 -05001463static int nbd_dev_add(int index)
1464{
1465 struct nbd_device *nbd;
1466 struct gendisk *disk;
1467 struct request_queue *q;
1468 int err = -ENOMEM;
1469
1470 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1471 if (!nbd)
1472 goto out;
1473
1474 disk = alloc_disk(1 << part_shift);
1475 if (!disk)
1476 goto out_free_nbd;
1477
1478 if (index >= 0) {
1479 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1480 GFP_KERNEL);
1481 if (err == -ENOSPC)
1482 err = -EEXIST;
1483 } else {
1484 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1485 if (err >= 0)
1486 index = err;
1487 }
1488 if (err < 0)
1489 goto out_free_disk;
1490
Josef Bacike46c7282017-04-06 17:02:00 -04001491 nbd->index = index;
Josef Bacikb0d91112017-02-01 16:11:40 -05001492 nbd->disk = disk;
1493 nbd->tag_set.ops = &nbd_mq_ops;
1494 nbd->tag_set.nr_hw_queues = 1;
1495 nbd->tag_set.queue_depth = 128;
1496 nbd->tag_set.numa_node = NUMA_NO_NODE;
1497 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1498 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1499 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
1500 nbd->tag_set.driver_data = nbd;
1501
1502 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1503 if (err)
1504 goto out_free_idr;
1505
1506 q = blk_mq_init_queue(&nbd->tag_set);
1507 if (IS_ERR(q)) {
1508 err = PTR_ERR(q);
1509 goto out_free_tags;
1510 }
1511 disk->queue = q;
1512
1513 /*
1514 * Tell the block layer that we are not a rotational device
1515 */
Bart Van Assche8b904b52018-03-07 17:10:10 -08001516 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1517 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Josef Bacik6df133a2018-05-23 13:35:59 -04001518 disk->queue->limits.discard_granularity = 0;
1519 blk_queue_max_discard_sectors(disk->queue, 0);
Josef Bacikebb16d02017-04-18 16:22:51 -04001520 blk_queue_max_segment_size(disk->queue, UINT_MAX);
Josef Bacik1cc1f172017-04-20 15:47:01 -04001521 blk_queue_max_segments(disk->queue, USHRT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001522 blk_queue_max_hw_sectors(disk->queue, 65536);
1523 disk->queue->limits.max_sectors = 256;
1524
Josef Bacikb0d91112017-02-01 16:11:40 -05001525 mutex_init(&nbd->config_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001526 refcount_set(&nbd->config_refs, 0);
Josef Bacikc6a47592017-04-06 17:02:06 -04001527 refcount_set(&nbd->refs, 1);
1528 INIT_LIST_HEAD(&nbd->list);
Josef Bacikb0d91112017-02-01 16:11:40 -05001529 disk->major = NBD_MAJOR;
1530 disk->first_minor = index << part_shift;
1531 disk->fops = &nbd_fops;
1532 disk->private_data = nbd;
1533 sprintf(disk->disk_name, "nbd%d", index);
Josef Bacikb0d91112017-02-01 16:11:40 -05001534 add_disk(disk);
Josef Bacik47d902b2017-04-06 17:02:05 -04001535 nbd_total_devices++;
Josef Bacikb0d91112017-02-01 16:11:40 -05001536 return index;
1537
1538out_free_tags:
1539 blk_mq_free_tag_set(&nbd->tag_set);
1540out_free_idr:
1541 idr_remove(&nbd_index_idr, index);
1542out_free_disk:
1543 put_disk(disk);
1544out_free_nbd:
1545 kfree(nbd);
1546out:
1547 return err;
1548}
1549
Josef Bacike46c7282017-04-06 17:02:00 -04001550static int find_free_cb(int id, void *ptr, void *data)
1551{
1552 struct nbd_device *nbd = ptr;
1553 struct nbd_device **found = data;
1554
1555 if (!refcount_read(&nbd->config_refs)) {
1556 *found = nbd;
1557 return 1;
1558 }
1559 return 0;
1560}
1561
1562/* Netlink interface. */
1563static struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1564 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1565 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1566 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1567 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1568 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1569 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1570 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
Josef Bacik560bc4b2017-04-06 17:02:04 -04001571 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001572 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
Josef Bacike46c7282017-04-06 17:02:00 -04001573};
1574
1575static struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1576 [NBD_SOCK_FD] = { .type = NLA_U32 },
1577};
1578
Josef Bacik47d902b2017-04-06 17:02:05 -04001579/* We don't use this right now since we don't parse the incoming list, but we
1580 * still want it here so userspace knows what to expect.
1581 */
1582static struct nla_policy __attribute__((unused))
1583nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1584 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1585 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1586};
1587
Josef Bacike46c7282017-04-06 17:02:00 -04001588static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1589{
1590 struct nbd_device *nbd = NULL;
1591 struct nbd_config *config;
1592 int index = -1;
1593 int ret;
Josef Bacika2c97902017-04-06 17:02:07 -04001594 bool put_dev = false;
Josef Bacike46c7282017-04-06 17:02:00 -04001595
1596 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1597 return -EPERM;
1598
1599 if (info->attrs[NBD_ATTR_INDEX])
1600 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1601 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1602 printk(KERN_ERR "nbd: must specify at least one socket\n");
1603 return -EINVAL;
1604 }
1605 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1606 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1607 return -EINVAL;
1608 }
1609again:
1610 mutex_lock(&nbd_index_mutex);
1611 if (index == -1) {
1612 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1613 if (ret == 0) {
1614 int new_index;
1615 new_index = nbd_dev_add(-1);
1616 if (new_index < 0) {
1617 mutex_unlock(&nbd_index_mutex);
1618 printk(KERN_ERR "nbd: failed to add new device\n");
Gustavo A. R. Silva09799622018-02-12 11:14:55 -06001619 return new_index;
Josef Bacike46c7282017-04-06 17:02:00 -04001620 }
1621 nbd = idr_find(&nbd_index_idr, new_index);
1622 }
1623 } else {
1624 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike6a76272017-08-14 18:25:33 +00001625 if (!nbd) {
1626 ret = nbd_dev_add(index);
1627 if (ret < 0) {
1628 mutex_unlock(&nbd_index_mutex);
1629 printk(KERN_ERR "nbd: failed to add new device\n");
1630 return ret;
1631 }
1632 nbd = idr_find(&nbd_index_idr, index);
1633 }
Josef Bacike46c7282017-04-06 17:02:00 -04001634 }
Josef Bacike46c7282017-04-06 17:02:00 -04001635 if (!nbd) {
1636 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1637 index);
Josef Bacikc6a47592017-04-06 17:02:06 -04001638 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001639 return -EINVAL;
1640 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001641 if (!refcount_inc_not_zero(&nbd->refs)) {
1642 mutex_unlock(&nbd_index_mutex);
1643 if (index == -1)
1644 goto again;
1645 printk(KERN_ERR "nbd: device at index %d is going down\n",
1646 index);
1647 return -EINVAL;
1648 }
1649 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001650
1651 mutex_lock(&nbd->config_lock);
1652 if (refcount_read(&nbd->config_refs)) {
1653 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001654 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001655 if (index == -1)
1656 goto again;
1657 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1658 return -EBUSY;
1659 }
1660 if (WARN_ON(nbd->config)) {
1661 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001662 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001663 return -EINVAL;
1664 }
1665 config = nbd->config = nbd_alloc_config();
1666 if (!nbd->config) {
1667 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001668 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001669 printk(KERN_ERR "nbd: couldn't allocate config\n");
1670 return -ENOMEM;
1671 }
1672 refcount_set(&nbd->config_refs, 1);
1673 set_bit(NBD_BOUND, &config->runtime_flags);
1674
1675 if (info->attrs[NBD_ATTR_SIZE_BYTES]) {
1676 u64 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1677 nbd_size_set(nbd, config->blksize,
1678 div64_u64(bytes, config->blksize));
1679 }
1680 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1681 u64 bsize =
1682 nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1683 nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
1684 }
1685 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1686 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1687 nbd->tag_set.timeout = timeout * HZ;
1688 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1689 }
Josef Bacik560bc4b2017-04-06 17:02:04 -04001690 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1691 config->dead_conn_timeout =
1692 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1693 config->dead_conn_timeout *= HZ;
1694 }
Josef Bacike46c7282017-04-06 17:02:00 -04001695 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1696 config->flags =
1697 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
Josef Bacika2c97902017-04-06 17:02:07 -04001698 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1699 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1700 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1701 set_bit(NBD_DESTROY_ON_DISCONNECT,
1702 &config->runtime_flags);
1703 put_dev = true;
1704 }
1705 }
1706
Josef Bacike46c7282017-04-06 17:02:00 -04001707 if (info->attrs[NBD_ATTR_SOCKETS]) {
1708 struct nlattr *attr;
1709 int rem, fd;
1710
1711 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1712 rem) {
1713 struct nlattr *socks[NBD_SOCK_MAX+1];
1714
1715 if (nla_type(attr) != NBD_SOCK_ITEM) {
1716 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1717 ret = -EINVAL;
1718 goto out;
1719 }
1720 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
Linus Torvalds8d65b082017-05-02 16:40:27 -07001721 nbd_sock_policy, info->extack);
Josef Bacike46c7282017-04-06 17:02:00 -04001722 if (ret != 0) {
1723 printk(KERN_ERR "nbd: error processing sock list\n");
1724 ret = -EINVAL;
1725 goto out;
1726 }
1727 if (!socks[NBD_SOCK_FD])
1728 continue;
1729 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1730 ret = nbd_add_socket(nbd, fd, true);
1731 if (ret)
1732 goto out;
1733 }
1734 }
1735 ret = nbd_start_device(nbd);
1736out:
1737 mutex_unlock(&nbd->config_lock);
1738 if (!ret) {
1739 set_bit(NBD_HAS_CONFIG_REF, &config->runtime_flags);
1740 refcount_inc(&nbd->config_refs);
1741 nbd_connect_reply(info, nbd->index);
1742 }
1743 nbd_config_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04001744 if (put_dev)
1745 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001746 return ret;
1747}
1748
1749static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
1750{
1751 struct nbd_device *nbd;
1752 int index;
1753
1754 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1755 return -EPERM;
1756
1757 if (!info->attrs[NBD_ATTR_INDEX]) {
1758 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
1759 return -EINVAL;
1760 }
1761 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1762 mutex_lock(&nbd_index_mutex);
1763 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike46c7282017-04-06 17:02:00 -04001764 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001765 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001766 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1767 index);
1768 return -EINVAL;
1769 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001770 if (!refcount_inc_not_zero(&nbd->refs)) {
1771 mutex_unlock(&nbd_index_mutex);
1772 printk(KERN_ERR "nbd: device at index %d is going down\n",
1773 index);
1774 return -EINVAL;
1775 }
1776 mutex_unlock(&nbd_index_mutex);
1777 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1778 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001779 return 0;
Josef Bacikc6a47592017-04-06 17:02:06 -04001780 }
Josef Bacike46c7282017-04-06 17:02:00 -04001781 mutex_lock(&nbd->config_lock);
1782 nbd_disconnect(nbd);
Josef Bacik96d97e12018-05-16 14:51:20 -04001783 nbd_clear_sock(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001784 mutex_unlock(&nbd->config_lock);
1785 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1786 &nbd->config->runtime_flags))
1787 nbd_config_put(nbd);
1788 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001789 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001790 return 0;
1791}
1792
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001793static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
1794{
1795 struct nbd_device *nbd = NULL;
1796 struct nbd_config *config;
1797 int index;
1798 int ret = -EINVAL;
Josef Bacika2c97902017-04-06 17:02:07 -04001799 bool put_dev = false;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001800
1801 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1802 return -EPERM;
1803
1804 if (!info->attrs[NBD_ATTR_INDEX]) {
1805 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
1806 return -EINVAL;
1807 }
1808 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1809 mutex_lock(&nbd_index_mutex);
1810 nbd = idr_find(&nbd_index_idr, index);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001811 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001812 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001813 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
1814 index);
1815 return -EINVAL;
1816 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001817 if (!refcount_inc_not_zero(&nbd->refs)) {
1818 mutex_unlock(&nbd_index_mutex);
1819 printk(KERN_ERR "nbd: device at index %d is going down\n",
1820 index);
1821 return -EINVAL;
1822 }
1823 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001824
1825 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1826 dev_err(nbd_to_dev(nbd),
1827 "not configured, cannot reconfigure\n");
Josef Bacikc6a47592017-04-06 17:02:06 -04001828 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001829 return -EINVAL;
1830 }
1831
1832 mutex_lock(&nbd->config_lock);
1833 config = nbd->config;
1834 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1835 !nbd->task_recv) {
1836 dev_err(nbd_to_dev(nbd),
1837 "not configured, cannot reconfigure\n");
1838 goto out;
1839 }
1840
1841 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1842 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1843 nbd->tag_set.timeout = timeout * HZ;
1844 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1845 }
Josef Bacik560bc4b2017-04-06 17:02:04 -04001846 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1847 config->dead_conn_timeout =
1848 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1849 config->dead_conn_timeout *= HZ;
1850 }
Josef Bacika2c97902017-04-06 17:02:07 -04001851 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1852 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1853 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1854 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1855 &config->runtime_flags))
1856 put_dev = true;
1857 } else {
1858 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1859 &config->runtime_flags))
1860 refcount_inc(&nbd->refs);
1861 }
1862 }
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001863
1864 if (info->attrs[NBD_ATTR_SOCKETS]) {
1865 struct nlattr *attr;
1866 int rem, fd;
1867
1868 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1869 rem) {
1870 struct nlattr *socks[NBD_SOCK_MAX+1];
1871
1872 if (nla_type(attr) != NBD_SOCK_ITEM) {
1873 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1874 ret = -EINVAL;
1875 goto out;
1876 }
1877 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
Linus Torvalds8d65b082017-05-02 16:40:27 -07001878 nbd_sock_policy, info->extack);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001879 if (ret != 0) {
1880 printk(KERN_ERR "nbd: error processing sock list\n");
1881 ret = -EINVAL;
1882 goto out;
1883 }
1884 if (!socks[NBD_SOCK_FD])
1885 continue;
1886 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1887 ret = nbd_reconnect_socket(nbd, fd);
1888 if (ret) {
1889 if (ret == -ENOSPC)
1890 ret = 0;
1891 goto out;
1892 }
1893 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
1894 }
1895 }
1896out:
1897 mutex_unlock(&nbd->config_lock);
1898 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001899 nbd_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04001900 if (put_dev)
1901 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001902 return ret;
1903}
1904
Josef Bacike46c7282017-04-06 17:02:00 -04001905static const struct genl_ops nbd_connect_genl_ops[] = {
1906 {
1907 .cmd = NBD_CMD_CONNECT,
1908 .policy = nbd_attr_policy,
1909 .doit = nbd_genl_connect,
1910 },
1911 {
1912 .cmd = NBD_CMD_DISCONNECT,
1913 .policy = nbd_attr_policy,
1914 .doit = nbd_genl_disconnect,
1915 },
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001916 {
1917 .cmd = NBD_CMD_RECONFIGURE,
1918 .policy = nbd_attr_policy,
1919 .doit = nbd_genl_reconfigure,
1920 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001921 {
1922 .cmd = NBD_CMD_STATUS,
1923 .policy = nbd_attr_policy,
1924 .doit = nbd_genl_status,
1925 },
Josef Bacike46c7282017-04-06 17:02:00 -04001926};
1927
Josef Bacik799f9a32017-04-06 17:02:02 -04001928static const struct genl_multicast_group nbd_mcast_grps[] = {
1929 { .name = NBD_GENL_MCAST_GROUP_NAME, },
1930};
1931
Josef Bacike46c7282017-04-06 17:02:00 -04001932static struct genl_family nbd_genl_family __ro_after_init = {
1933 .hdrsize = 0,
1934 .name = NBD_GENL_FAMILY_NAME,
1935 .version = NBD_GENL_VERSION,
1936 .module = THIS_MODULE,
1937 .ops = nbd_connect_genl_ops,
1938 .n_ops = ARRAY_SIZE(nbd_connect_genl_ops),
1939 .maxattr = NBD_ATTR_MAX,
Josef Bacik799f9a32017-04-06 17:02:02 -04001940 .mcgrps = nbd_mcast_grps,
1941 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
Josef Bacike46c7282017-04-06 17:02:00 -04001942};
1943
Josef Bacik47d902b2017-04-06 17:02:05 -04001944static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
1945{
1946 struct nlattr *dev_opt;
1947 u8 connected = 0;
1948 int ret;
1949
1950 /* This is a little racey, but for status it's ok. The
1951 * reason we don't take a ref here is because we can't
1952 * take a ref in the index == -1 case as we would need
1953 * to put under the nbd_index_mutex, which could
1954 * deadlock if we are configured to remove ourselves
1955 * once we're disconnected.
1956 */
1957 if (refcount_read(&nbd->config_refs))
1958 connected = 1;
1959 dev_opt = nla_nest_start(reply, NBD_DEVICE_ITEM);
1960 if (!dev_opt)
1961 return -EMSGSIZE;
1962 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
1963 if (ret)
1964 return -EMSGSIZE;
1965 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
1966 connected);
1967 if (ret)
1968 return -EMSGSIZE;
1969 nla_nest_end(reply, dev_opt);
1970 return 0;
1971}
1972
1973static int status_cb(int id, void *ptr, void *data)
1974{
1975 struct nbd_device *nbd = ptr;
1976 return populate_nbd_status(nbd, (struct sk_buff *)data);
1977}
1978
1979static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
1980{
1981 struct nlattr *dev_list;
1982 struct sk_buff *reply;
1983 void *reply_head;
1984 size_t msg_size;
1985 int index = -1;
1986 int ret = -ENOMEM;
1987
1988 if (info->attrs[NBD_ATTR_INDEX])
1989 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1990
1991 mutex_lock(&nbd_index_mutex);
1992
1993 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
1994 nla_attr_size(sizeof(u8)));
1995 msg_size *= (index == -1) ? nbd_total_devices : 1;
1996
1997 reply = genlmsg_new(msg_size, GFP_KERNEL);
1998 if (!reply)
1999 goto out;
2000 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2001 NBD_CMD_STATUS);
2002 if (!reply_head) {
2003 nlmsg_free(reply);
2004 goto out;
2005 }
2006
2007 dev_list = nla_nest_start(reply, NBD_ATTR_DEVICE_LIST);
2008 if (index == -1) {
2009 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2010 if (ret) {
2011 nlmsg_free(reply);
2012 goto out;
2013 }
2014 } else {
2015 struct nbd_device *nbd;
2016 nbd = idr_find(&nbd_index_idr, index);
2017 if (nbd) {
2018 ret = populate_nbd_status(nbd, reply);
2019 if (ret) {
2020 nlmsg_free(reply);
2021 goto out;
2022 }
2023 }
2024 }
2025 nla_nest_end(reply, dev_list);
2026 genlmsg_end(reply, reply_head);
2027 genlmsg_reply(reply, info);
2028 ret = 0;
2029out:
2030 mutex_unlock(&nbd_index_mutex);
2031 return ret;
2032}
2033
Josef Bacike46c7282017-04-06 17:02:00 -04002034static void nbd_connect_reply(struct genl_info *info, int index)
2035{
2036 struct sk_buff *skb;
2037 void *msg_head;
2038 int ret;
2039
2040 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2041 if (!skb)
2042 return;
2043 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2044 NBD_CMD_CONNECT);
2045 if (!msg_head) {
2046 nlmsg_free(skb);
2047 return;
2048 }
2049 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2050 if (ret) {
2051 nlmsg_free(skb);
2052 return;
2053 }
2054 genlmsg_end(skb, msg_head);
2055 genlmsg_reply(skb, info);
2056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Josef Bacik799f9a32017-04-06 17:02:02 -04002058static void nbd_mcast_index(int index)
2059{
2060 struct sk_buff *skb;
2061 void *msg_head;
2062 int ret;
2063
2064 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2065 if (!skb)
2066 return;
2067 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2068 NBD_CMD_LINK_DEAD);
2069 if (!msg_head) {
2070 nlmsg_free(skb);
2071 return;
2072 }
2073 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2074 if (ret) {
2075 nlmsg_free(skb);
2076 return;
2077 }
2078 genlmsg_end(skb, msg_head);
2079 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2080}
2081
2082static void nbd_dead_link_work(struct work_struct *work)
2083{
2084 struct link_dead_args *args = container_of(work, struct link_dead_args,
2085 work);
2086 nbd_mcast_index(args->index);
2087 kfree(args);
2088}
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090static int __init nbd_init(void)
2091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 int i;
2093
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08002094 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002096 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02002097 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002098 return -EINVAL;
2099 }
2100
2101 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02002102 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002103 part_shift = fls(max_part);
2104
Namhyung Kim5988ce22011-05-28 14:44:46 +02002105 /*
2106 * Adjust max_part according to part_shift as it is exported
2107 * to user space so that user can know the max number of
2108 * partition kernel should be able to manage.
2109 *
2110 * Note that -1 is required because partition 0 is reserved
2111 * for the whole disk.
2112 */
2113 max_part = (1UL << part_shift) - 1;
2114 }
2115
Namhyung Kim3b271082011-05-28 14:44:46 +02002116 if ((1UL << part_shift) > DISK_MAX_PARTS)
2117 return -EINVAL;
2118
2119 if (nbds_max > 1UL << (MINORBITS - part_shift))
2120 return -EINVAL;
Josef Bacik124d6db2017-02-01 16:11:11 -05002121 recv_workqueue = alloc_workqueue("knbd-recv",
Dan Melnic2189c972017-09-18 13:08:51 -07002122 WQ_MEM_RECLAIM | WQ_HIGHPRI |
2123 WQ_UNBOUND, 0);
Josef Bacik124d6db2017-02-01 16:11:11 -05002124 if (!recv_workqueue)
2125 return -ENOMEM;
Namhyung Kim3b271082011-05-28 14:44:46 +02002126
Josef Bacik6330a2d2017-02-15 16:49:48 -05002127 if (register_blkdev(NBD_MAJOR, "nbd")) {
2128 destroy_workqueue(recv_workqueue);
Josef Bacikb0d91112017-02-01 16:11:40 -05002129 return -EIO;
Josef Bacik6330a2d2017-02-15 16:49:48 -05002130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Josef Bacike46c7282017-04-06 17:02:00 -04002132 if (genl_register_family(&nbd_genl_family)) {
2133 unregister_blkdev(NBD_MAJOR, "nbd");
2134 destroy_workqueue(recv_workqueue);
2135 return -EINVAL;
2136 }
Markus Pargmann30d53d92015-08-17 08:20:06 +02002137 nbd_dbg_init();
2138
Josef Bacikb0d91112017-02-01 16:11:40 -05002139 mutex_lock(&nbd_index_mutex);
2140 for (i = 0; i < nbds_max; i++)
2141 nbd_dev_add(i);
2142 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05002144}
2145
2146static int nbd_exit_cb(int id, void *ptr, void *data)
2147{
Josef Bacikc6a47592017-04-06 17:02:06 -04002148 struct list_head *list = (struct list_head *)data;
Josef Bacikb0d91112017-02-01 16:11:40 -05002149 struct nbd_device *nbd = ptr;
Josef Bacikc6a47592017-04-06 17:02:06 -04002150
Josef Bacikc6a47592017-04-06 17:02:06 -04002151 list_add_tail(&nbd->list, list);
Josef Bacikb0d91112017-02-01 16:11:40 -05002152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
2155static void __exit nbd_cleanup(void)
2156{
Josef Bacikc6a47592017-04-06 17:02:06 -04002157 struct nbd_device *nbd;
2158 LIST_HEAD(del_list);
2159
Markus Pargmann30d53d92015-08-17 08:20:06 +02002160 nbd_dbg_close();
2161
Josef Bacikc6a47592017-04-06 17:02:06 -04002162 mutex_lock(&nbd_index_mutex);
2163 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2164 mutex_unlock(&nbd_index_mutex);
2165
Josef Bacik60ae36a2017-04-28 09:49:19 -04002166 while (!list_empty(&del_list)) {
2167 nbd = list_first_entry(&del_list, struct nbd_device, list);
2168 list_del_init(&nbd->list);
2169 if (refcount_read(&nbd->refs) != 1)
Josef Bacikc6a47592017-04-06 17:02:06 -04002170 printk(KERN_ERR "nbd: possibly leaking a device\n");
2171 nbd_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002172 }
2173
Josef Bacikb0d91112017-02-01 16:11:40 -05002174 idr_destroy(&nbd_index_idr);
Josef Bacike46c7282017-04-06 17:02:00 -04002175 genl_unregister_family(&nbd_genl_family);
Josef Bacik124d6db2017-02-01 16:11:11 -05002176 destroy_workqueue(recv_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180module_init(nbd_init);
2181module_exit(nbd_cleanup);
2182
2183MODULE_DESCRIPTION("Network Block Device");
2184MODULE_LICENSE("GPL");
2185
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07002186module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002187MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2188module_param(max_part, int, 0444);
Josef Bacik7a8362a2017-08-14 18:56:16 +00002189MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");