blob: 5f2a4240a204d54fc6fe87e569dc6165d5190530 [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 = {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400169 .attr = { .name = "pid", .mode = S_IRUGO},
170 .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;
176 if (disk) {
177 del_gendisk(disk);
178 blk_cleanup_queue(disk->queue);
179 blk_mq_free_tag_set(&nbd->tag_set);
Josef Bacika2c97902017-04-06 17:02:07 -0400180 disk->private_data = NULL;
Josef Bacikc6a47592017-04-06 17:02:06 -0400181 put_disk(disk);
182 }
183 kfree(nbd);
184}
185
186static void nbd_put(struct nbd_device *nbd)
187{
188 if (refcount_dec_and_mutex_lock(&nbd->refs,
189 &nbd_index_mutex)) {
190 idr_remove(&nbd_index_idr, nbd->index);
191 mutex_unlock(&nbd_index_mutex);
192 nbd_dev_remove(nbd);
193 }
194}
195
Josef Bacik799f9a32017-04-06 17:02:02 -0400196static int nbd_disconnected(struct nbd_config *config)
Josef Bacikf3733242017-04-06 17:01:57 -0400197{
Josef Bacik799f9a32017-04-06 17:02:02 -0400198 return test_bit(NBD_DISCONNECTED, &config->runtime_flags) ||
199 test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
200}
201
202static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
203 int notify)
204{
205 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
206 struct link_dead_args *args;
207 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
208 if (args) {
209 INIT_WORK(&args->work, nbd_dead_link_work);
210 args->index = nbd->index;
211 queue_work(system_wq, &args->work);
212 }
213 }
Josef Bacik560bc4b2017-04-06 17:02:04 -0400214 if (!nsock->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400215 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400216 atomic_dec(&nbd->config->live_connections);
217 }
Josef Bacikf3733242017-04-06 17:01:57 -0400218 nsock->dead = true;
219 nsock->pending = NULL;
220 nsock->sent = 0;
221}
222
Josef Bacik29eaadc2017-04-06 17:01:59 -0400223static void nbd_size_clear(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200224{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400225 if (nbd->config->bytesize) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400226 set_capacity(nbd->disk, 0);
227 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
228 }
Markus Pargmann37091fd2015-07-27 07:36:49 +0200229}
230
Josef Bacik29eaadc2017-04-06 17:01:59 -0400231static void nbd_size_update(struct nbd_device *nbd)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200232{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400233 struct nbd_config *config = nbd->config;
234 blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
235 blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400236 set_capacity(nbd->disk, config->bytesize >> 9);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200237 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
238}
239
Josef Bacik29eaadc2017-04-06 17:01:59 -0400240static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
241 loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200242{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400243 struct nbd_config *config = nbd->config;
244 config->blksize = blocksize;
245 config->bytesize = blocksize * nr_blocks;
Markus Pargmann37091fd2015-07-27 07:36:49 +0200246}
247
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200248static void nbd_complete_rq(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200250 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200252 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", cmd,
253 cmd->status ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Christoph Hellwig1e388ae2017-04-20 16:03:06 +0200255 blk_mq_end_request(req, cmd->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Markus Pargmanne018e752015-04-02 10:11:39 +0200258/*
259 * Forcibly shutdown the socket causing all listeners to error
260 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200261static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700262{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400263 struct nbd_config *config = nbd->config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500264 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700265
Josef Bacik5ea8d102017-04-06 17:01:58 -0400266 if (config->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200267 return;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400268 if (test_and_set_bit(NBD_DISCONNECTED, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500269 return;
270
Josef Bacik5ea8d102017-04-06 17:01:58 -0400271 for (i = 0; i < config->num_connections; i++) {
272 struct nbd_sock *nsock = config->socks[i];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500273 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400274 nbd_mark_nsock_dead(nbd, nsock, 0);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500275 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100276 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500277 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700278}
279
Josef Bacik0eadf372016-09-08 12:33:40 -0700280static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
281 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700282{
Josef Bacik0eadf372016-09-08 12:33:40 -0700283 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
284 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400285 struct nbd_config *config;
Paul Clements7fdfd402007-10-16 23:27:37 -0700286
Josef Bacik5ea8d102017-04-06 17:01:58 -0400287 if (!refcount_inc_not_zero(&nbd->config_refs)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200288 cmd->status = BLK_STS_TIMEOUT;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400289 return BLK_EH_HANDLED;
290 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400291 config = nbd->config;
292
293 if (config->num_connections > 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400294 dev_err_ratelimited(nbd_to_dev(nbd),
295 "Connection timed out, retrying\n");
Josef Bacikf3733242017-04-06 17:01:57 -0400296 /*
297 * Hooray we have more connections, requeue this IO, the submit
298 * path will put it on a real connection.
299 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400300 if (config->socks && config->num_connections > 1) {
301 if (cmd->index < config->num_connections) {
Josef Bacikf3733242017-04-06 17:01:57 -0400302 struct nbd_sock *nsock =
Josef Bacik5ea8d102017-04-06 17:01:58 -0400303 config->socks[cmd->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400304 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400305 /* We can have multiple outstanding requests, so
306 * we don't want to mark the nsock dead if we've
307 * already reconnected with a new socket, so
308 * only mark it dead if its the same socket we
309 * were sent out on.
310 */
311 if (cmd->cookie == nsock->cookie)
312 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400313 mutex_unlock(&nsock->tx_lock);
314 }
Josef Bacikf3733242017-04-06 17:01:57 -0400315 blk_mq_requeue_request(req, true);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400316 nbd_config_put(nbd);
Josef Bacikf3733242017-04-06 17:01:57 -0400317 return BLK_EH_NOT_HANDLED;
318 }
Josef Bacikf3733242017-04-06 17:01:57 -0400319 } else {
320 dev_err_ratelimited(nbd_to_dev(nbd),
321 "Connection timed out\n");
322 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400323 set_bit(NBD_TIMEDOUT, &config->runtime_flags);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200324 cmd->status = BLK_STS_IOERR;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500325 sock_shutdown(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400326 nbd_config_put(nbd);
327
Josef Bacik0eadf372016-09-08 12:33:40 -0700328 return BLK_EH_HANDLED;
Paul Clements7fdfd402007-10-16 23:27:37 -0700329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*
332 * Send or receive packet.
333 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500334static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400335 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400337 struct nbd_config *config = nbd->config;
338 struct socket *sock = config->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int result;
340 struct msghdr msg;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700341 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700343 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500344 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200345 "Attempted %s on closed socket in sock_xmit\n",
346 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700347 return -EINVAL;
348 }
349
Al Viroc9f2b6a2015-11-12 05:09:35 -0500350 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500351
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700352 noreclaim_flag = memalloc_noreclaim_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700354 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 msg.msg_name = NULL;
356 msg.msg_namelen = 0;
357 msg.msg_control = NULL;
358 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
360
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200361 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500362 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200363 else
Al Viroc1696ca2015-11-12 04:51:19 -0500364 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (result <= 0) {
367 if (result == 0)
368 result = -EPIPE; /* short read */
369 break;
370 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400371 if (sent)
372 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500373 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700375 memalloc_noreclaim_restore(noreclaim_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 return result;
378}
379
Josef Bacik32e67a32017-10-24 15:57:18 -0400380/*
381 * Different settings for sk->sk_sndtimeo can result in different return values
382 * if there is a signal pending when we enter sendmsg, because reasons?
383 */
384static inline int was_interrupted(int result)
385{
386 return result == -ERESTARTSYS || result == -EINTR;
387}
388
Paul Clements7fdfd402007-10-16 23:27:37 -0700389/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500390static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700392 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400393 struct nbd_config *config = nbd->config;
394 struct nbd_sock *nsock = config->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500395 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500396 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
397 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
398 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900399 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700400 struct bio *bio;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200401 u32 type;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400402 u32 nbd_cmd_flags = 0;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500403 u32 tag = blk_mq_unique_tag(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400404 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200405
Al Viroc9f2b6a2015-11-12 05:09:35 -0500406 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
407
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100408 switch (req_op(req)) {
409 case REQ_OP_DISCARD:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200410 type = NBD_CMD_TRIM;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100411 break;
412 case REQ_OP_FLUSH:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200413 type = NBD_CMD_FLUSH;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100414 break;
415 case REQ_OP_WRITE:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200416 type = NBD_CMD_WRITE;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100417 break;
418 case REQ_OP_READ:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200419 type = NBD_CMD_READ;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100420 break;
421 default:
422 return -EIO;
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Christoph Hellwig09fc54c2017-01-31 16:57:28 +0100425 if (rq_data_dir(req) == WRITE &&
Josef Bacik5ea8d102017-04-06 17:01:58 -0400426 (config->flags & NBD_FLAG_READ_ONLY)) {
Christoph Hellwig09fc54c2017-01-31 16:57:28 +0100427 dev_err_ratelimited(disk_to_dev(nbd->disk),
428 "Write on read-only\n");
429 return -EIO;
430 }
431
Shaun McDowell685c9b22017-05-25 23:55:54 -0400432 if (req->cmd_flags & REQ_FUA)
433 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
434
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400435 /* We did a partial send previously, and we at least sent the whole
436 * request struct, so just go and send the rest of the pages in the
437 * request.
438 */
439 if (sent) {
440 if (sent >= sizeof(request)) {
441 skip = sent - sizeof(request);
442 goto send_pages;
443 }
444 iov_iter_advance(&from, sent);
445 }
Josef Bacikf3733242017-04-06 17:01:57 -0400446 cmd->index = index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400447 cmd->cookie = nsock->cookie;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400448 request.type = htonl(type | nbd_cmd_flags);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500449 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800450 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
451 request.len = htonl(size);
452 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500453 memcpy(request.handle, &tag, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Markus Pargmannd18509f2015-04-02 10:11:38 +0200455 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700456 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200457 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500458 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400459 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400461 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400462 /* If we havne't sent anything we can just return BUSY,
463 * however if we have sent something we need to make
464 * sure we only allow this req to be sent until we are
465 * completely done.
466 */
467 if (sent) {
468 nsock->pending = req;
469 nsock->sent = sent;
470 }
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200471 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400472 }
Josef Bacika897b662016-12-05 16:20:29 -0500473 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200474 "Send control failed (result %d)\n", result);
Josef Bacikf3733242017-04-06 17:01:57 -0400475 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400477send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700478 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400479 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700480
Jens Axboe429a7872016-11-17 12:30:37 -0700481 bio = req->bio;
482 while (bio) {
483 struct bio *next = bio->bi_next;
484 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800485 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700486
487 bio_for_each_segment(bvec, bio, iter) {
488 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500489 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700490
Markus Pargmannd18509f2015-04-02 10:11:38 +0200491 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700492 cmd, bvec.bv_len);
Al Viroc9f2b6a2015-11-12 05:09:35 -0500493 iov_iter_bvec(&from, ITER_BVEC | WRITE,
494 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400495 if (skip) {
496 if (skip >= iov_iter_count(&from)) {
497 skip -= iov_iter_count(&from);
498 continue;
499 }
500 iov_iter_advance(&from, skip);
501 skip = 0;
502 }
503 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200504 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400505 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400506 /* We've already sent the header, we
507 * have no choice but to set pending and
508 * return BUSY.
509 */
510 nsock->pending = req;
511 nsock->sent = sent;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200512 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400513 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700514 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200515 "Send data failed (result %d)\n",
516 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400517 return -EAGAIN;
Jens Axboe6c92e692007-08-16 13:43:12 +0200518 }
Jens Axboe429a7872016-11-17 12:30:37 -0700519 /*
520 * The completion might already have come in,
521 * so break for the last one instead of letting
522 * the iterator do it. This prevents use-after-free
523 * of the bio.
524 */
525 if (is_last)
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Jens Axboe429a7872016-11-17 12:30:37 -0700528 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400530out:
531 nsock->pending = NULL;
532 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500537static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400539 struct nbd_config *config = nbd->config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int result;
541 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700542 struct nbd_cmd *cmd;
543 struct request *req = NULL;
544 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500545 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500546 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
547 struct iov_iter to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 reply.magic = 0;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500550 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400551 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (result <= 0) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400553 if (!nbd_disconnected(config))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500554 dev_err(disk_to_dev(nbd->disk),
555 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200556 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700558
559 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700560 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700561 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200562 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700563 }
564
Josef Bacik9561a7a2016-11-22 14:04:40 -0500565 memcpy(&tag, reply.handle, sizeof(u32));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800566
Josef Bacikfd8383f2016-09-08 12:33:37 -0700567 hwq = blk_mq_unique_tag_to_hwq(tag);
568 if (hwq < nbd->tag_set.nr_hw_queues)
569 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
570 blk_mq_unique_tag_to_tag(tag));
571 if (!req || !blk_mq_request_started(req)) {
572 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
573 tag, req);
574 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700576 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700578 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200579 ntohl(reply.error));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200580 cmd->status = BLK_STS_IOERR;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700581 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Josef Bacikfd8383f2016-09-08 12:33:37 -0700584 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200585 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200586 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800587 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200588
589 rq_for_each_segment(bvec, req, iter) {
Al Viroc9f2b6a2015-11-12 05:09:35 -0500590 iov_iter_bvec(&to, ITER_BVEC | READ,
591 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400592 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200593 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700594 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200595 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400596 /*
597 * If we've disconnected or we only have 1
598 * connection then we need to make sure we
599 * complete this request, otherwise error out
600 * and let the timeout stuff handle resubmitting
601 * this request onto another connection.
602 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400603 if (nbd_disconnected(config) ||
604 config->num_connections <= 1) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200605 cmd->status = BLK_STS_IOERR;
Josef Bacikf3733242017-04-06 17:01:57 -0400606 return cmd;
607 }
608 return ERR_PTR(-EIO);
Jens Axboe6c92e692007-08-16 13:43:12 +0200609 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200610 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700611 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500613 } else {
614 /* See the comment in nbd_queue_rq. */
615 wait_for_completion(&cmd->send_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700617 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Josef Bacik9561a7a2016-11-22 14:04:40 -0500620static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500622 struct recv_thread_args *args = container_of(work,
623 struct recv_thread_args,
624 work);
625 struct nbd_device *nbd = args->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400626 struct nbd_config *config = nbd->config;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700627 struct nbd_cmd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Markus Pargmann19391832015-08-17 08:20:03 +0200629 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500630 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700631 if (IS_ERR(cmd)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400632 struct nbd_sock *nsock = config->socks[args->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400633
634 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400635 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400636 mutex_unlock(&nsock->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200637 break;
638 }
639
Christoph Hellwig08e00292017-04-20 16:03:09 +0200640 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd));
Markus Pargmann19391832015-08-17 08:20:03 +0200641 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400642 atomic_dec(&config->recv_threads);
643 wake_up(&config->recv_wq);
644 nbd_config_put(nbd);
645 kfree(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Josef Bacikfd8383f2016-09-08 12:33:37 -0700648static void nbd_clear_req(struct request *req, void *data, bool reserved)
649{
650 struct nbd_cmd *cmd;
651
652 if (!blk_mq_request_started(req))
653 return;
654 cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200655 cmd->status = BLK_STS_IOERR;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200656 blk_mq_complete_request(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700657}
658
Wanlong Gaof4507162012-03-28 14:42:51 -0700659static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300661 blk_mq_quiesce_queue(nbd->disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700662 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300663 blk_mq_unquiesce_queue(nbd->disk->queue);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200664 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Josef Bacikf3733242017-04-06 17:01:57 -0400667static int find_fallback(struct nbd_device *nbd, int index)
668{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400669 struct nbd_config *config = nbd->config;
Josef Bacikf3733242017-04-06 17:01:57 -0400670 int new_index = -1;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400671 struct nbd_sock *nsock = config->socks[index];
Josef Bacikf3733242017-04-06 17:01:57 -0400672 int fallback = nsock->fallback_index;
673
Josef Bacik5ea8d102017-04-06 17:01:58 -0400674 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
Josef Bacikf3733242017-04-06 17:01:57 -0400675 return new_index;
676
Josef Bacik5ea8d102017-04-06 17:01:58 -0400677 if (config->num_connections <= 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400678 dev_err_ratelimited(disk_to_dev(nbd->disk),
679 "Attempted send on invalid socket\n");
680 return new_index;
681 }
682
Josef Bacik5ea8d102017-04-06 17:01:58 -0400683 if (fallback >= 0 && fallback < config->num_connections &&
684 !config->socks[fallback]->dead)
Josef Bacikf3733242017-04-06 17:01:57 -0400685 return fallback;
686
687 if (nsock->fallback_index < 0 ||
Josef Bacik5ea8d102017-04-06 17:01:58 -0400688 nsock->fallback_index >= config->num_connections ||
689 config->socks[nsock->fallback_index]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400690 int i;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400691 for (i = 0; i < config->num_connections; i++) {
Josef Bacikf3733242017-04-06 17:01:57 -0400692 if (i == index)
693 continue;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400694 if (!config->socks[i]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400695 new_index = i;
696 break;
697 }
698 }
699 nsock->fallback_index = new_index;
700 if (new_index < 0) {
701 dev_err_ratelimited(disk_to_dev(nbd->disk),
702 "Dead connection, failed to find a fallback\n");
703 return new_index;
704 }
705 }
706 new_index = nsock->fallback_index;
707 return new_index;
708}
Paul Clements7fdfd402007-10-16 23:27:37 -0700709
Josef Bacik560bc4b2017-04-06 17:02:04 -0400710static int wait_for_reconnect(struct nbd_device *nbd)
711{
712 struct nbd_config *config = nbd->config;
713 if (!config->dead_conn_timeout)
714 return 0;
715 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
716 return 0;
Josef Bacikff57dc92017-11-06 16:11:57 -0500717 wait_event_timeout(config->conn_wait,
718 atomic_read(&config->live_connections),
719 config->dead_conn_timeout);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400720 return atomic_read(&config->live_connections);
721}
722
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400723static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700724{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700725 struct request *req = blk_mq_rq_from_pdu(cmd);
726 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400727 struct nbd_config *config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500728 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400729 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700730
Josef Bacik5ea8d102017-04-06 17:01:58 -0400731 if (!refcount_inc_not_zero(&nbd->config_refs)) {
732 dev_err_ratelimited(disk_to_dev(nbd->disk),
733 "Socks array is empty\n");
Josef Bacik6a468d52017-11-06 16:11:58 -0500734 blk_mq_start_request(req);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400735 return -EINVAL;
736 }
737 config = nbd->config;
738
739 if (index >= config->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500740 dev_err_ratelimited(disk_to_dev(nbd->disk),
741 "Attempted send on invalid socket\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -0400742 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500743 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400744 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500745 }
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200746 cmd->status = BLK_STS_OK;
Josef Bacikf3733242017-04-06 17:01:57 -0400747again:
Josef Bacik5ea8d102017-04-06 17:01:58 -0400748 nsock = config->socks[index];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500749 mutex_lock(&nsock->tx_lock);
Josef Bacikf3733242017-04-06 17:01:57 -0400750 if (nsock->dead) {
Josef Bacik560bc4b2017-04-06 17:02:04 -0400751 int old_index = index;
Josef Bacikf3733242017-04-06 17:01:57 -0400752 index = find_fallback(nbd, index);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500753 mutex_unlock(&nsock->tx_lock);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400754 if (index < 0) {
755 if (wait_for_reconnect(nbd)) {
756 index = old_index;
757 goto again;
758 }
759 /* All the sockets should already be down at this point,
760 * we just want to make sure that DISCONNECTED is set so
761 * any requests that come in that were queue'ed waiting
762 * for the reconnect timer don't trigger the timer again
763 * and instead just error out.
764 */
765 sock_shutdown(nbd);
766 nbd_config_put(nbd);
Josef Bacik6a468d52017-11-06 16:11:58 -0500767 blk_mq_start_request(req);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400768 return -EIO;
769 }
Josef Bacikf3733242017-04-06 17:01:57 -0400770 goto again;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700771 }
772
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400773 /* Handle the case that we have a pending request that was partially
774 * transmitted that _has_ to be serviced first. We need to call requeue
775 * here so that it gets put _after_ the request that is already on the
776 * dispatch list.
777 */
Josef Bacik6a468d52017-11-06 16:11:58 -0500778 blk_mq_start_request(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400779 if (unlikely(nsock->pending && nsock->pending != req)) {
780 blk_mq_requeue_request(req, true);
781 ret = 0;
782 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700783 }
Josef Bacikf3733242017-04-06 17:01:57 -0400784 /*
785 * Some failures are related to the link going down, so anything that
786 * returns EAGAIN can be retried on a different socket.
787 */
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400788 ret = nbd_send_cmd(nbd, cmd, index);
Josef Bacikf3733242017-04-06 17:01:57 -0400789 if (ret == -EAGAIN) {
790 dev_err_ratelimited(disk_to_dev(nbd->disk),
Josef Bacik6a468d52017-11-06 16:11:58 -0500791 "Request send failed, requeueing\n");
Josef Bacik799f9a32017-04-06 17:02:02 -0400792 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacik6a468d52017-11-06 16:11:58 -0500793 blk_mq_requeue_request(req, true);
794 ret = 0;
Josef Bacikf3733242017-04-06 17:01:57 -0400795 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400796out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500797 mutex_unlock(&nsock->tx_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400798 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400799 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700800}
801
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200802static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700803 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700804{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700805 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400806 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700807
Josef Bacik9561a7a2016-11-22 14:04:40 -0500808 /*
809 * Since we look at the bio's to send the request over the network we
810 * need to make sure the completion work doesn't mark this request done
811 * before we are done doing our send. This keeps us from dereferencing
812 * freed data if we have particularly fast completions (ie we get the
813 * completion before we exit sock_xmit on the last bvec) or in the case
814 * that the server is misbehaving (or there was an error) before we're
815 * done sending everything over the wire.
816 */
817 init_completion(&cmd->send_complete);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400818
819 /* We can be called directly from the user space process, which means we
820 * could possibly have signals pending so our sendmsg will fail. In
821 * this case we need to return that we are busy, otherwise error out as
822 * appropriate.
823 */
824 ret = nbd_handle_cmd(cmd, hctx->queue_num);
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400825 if (ret < 0)
826 ret = BLK_STS_IOERR;
827 else if (!ret)
828 ret = BLK_STS_OK;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500829 complete(&cmd->send_complete);
830
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400831 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Josef Bacike46c7282017-04-06 17:02:00 -0400834static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
835 bool netlink)
Markus Pargmann23272a672015-10-29 11:51:16 +0100836{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400837 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -0500838 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500839 struct nbd_sock **socks;
840 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -0500841 int err;
842
843 sock = sockfd_lookup(arg, &err);
844 if (!sock)
845 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100846
Josef Bacike46c7282017-04-06 17:02:00 -0400847 if (!netlink && !nbd->task_setup &&
848 !test_bit(NBD_BOUND, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500849 nbd->task_setup = current;
Josef Bacike46c7282017-04-06 17:02:00 -0400850
851 if (!netlink &&
852 (nbd->task_setup != current ||
853 test_bit(NBD_BOUND, &config->runtime_flags))) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500854 dev_err(disk_to_dev(nbd->disk),
855 "Device being setup by another task");
Josef Bacik9b1355d2017-04-06 17:01:56 -0400856 sockfd_put(sock);
Josef Bacike46c7282017-04-06 17:02:00 -0400857 return -EBUSY;
Markus Pargmann23272a672015-10-29 11:51:16 +0100858 }
859
Josef Bacik5ea8d102017-04-06 17:01:58 -0400860 socks = krealloc(config->socks, (config->num_connections + 1) *
Josef Bacik9561a7a2016-11-22 14:04:40 -0500861 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400862 if (!socks) {
863 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500864 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400865 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500866 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400867 if (!nsock) {
868 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500869 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400870 }
Markus Pargmann23272a672015-10-29 11:51:16 +0100871
Josef Bacik5ea8d102017-04-06 17:01:58 -0400872 config->socks = socks;
Markus Pargmann23272a672015-10-29 11:51:16 +0100873
Josef Bacikf3733242017-04-06 17:01:57 -0400874 nsock->fallback_index = -1;
875 nsock->dead = false;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500876 mutex_init(&nsock->tx_lock);
877 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400878 nsock->pending = NULL;
879 nsock->sent = 0;
Josef Bacik799f9a32017-04-06 17:02:02 -0400880 nsock->cookie = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400881 socks[config->num_connections++] = nsock;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400882 atomic_inc(&config->live_connections);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500883
884 return 0;
Markus Pargmann23272a672015-10-29 11:51:16 +0100885}
886
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400887static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
888{
889 struct nbd_config *config = nbd->config;
890 struct socket *sock, *old;
891 struct recv_thread_args *args;
892 int i;
893 int err;
894
895 sock = sockfd_lookup(arg, &err);
896 if (!sock)
897 return err;
898
899 args = kzalloc(sizeof(*args), GFP_KERNEL);
900 if (!args) {
901 sockfd_put(sock);
902 return -ENOMEM;
903 }
904
905 for (i = 0; i < config->num_connections; i++) {
906 struct nbd_sock *nsock = config->socks[i];
907
908 if (!nsock->dead)
909 continue;
910
911 mutex_lock(&nsock->tx_lock);
912 if (!nsock->dead) {
913 mutex_unlock(&nsock->tx_lock);
914 continue;
915 }
916 sk_set_memalloc(sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -0400917 if (nbd->tag_set.timeout)
918 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400919 atomic_inc(&config->recv_threads);
920 refcount_inc(&nbd->config_refs);
921 old = nsock->sock;
922 nsock->fallback_index = -1;
923 nsock->sock = sock;
924 nsock->dead = false;
925 INIT_WORK(&args->work, recv_work);
926 args->index = i;
927 args->nbd = nbd;
Josef Bacik799f9a32017-04-06 17:02:02 -0400928 nsock->cookie++;
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400929 mutex_unlock(&nsock->tx_lock);
930 sockfd_put(old);
931
Josef Bacik7a362ea2017-07-25 13:31:19 -0400932 clear_bit(NBD_DISCONNECTED, &config->runtime_flags);
933
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400934 /* We take the tx_mutex in an error path in the recv_work, so we
935 * need to queue_work outside of the tx_mutex.
936 */
937 queue_work(recv_workqueue, &args->work);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400938
939 atomic_inc(&config->live_connections);
940 wake_up(&config->conn_wait);
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400941 return 0;
942 }
943 sockfd_put(sock);
944 kfree(args);
945 return -ENOSPC;
946}
947
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100948static void nbd_bdev_reset(struct block_device *bdev)
949{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -0400950 if (bdev->bd_openers > 1)
951 return;
Josef Bacik29eaadc2017-04-06 17:01:59 -0400952 bd_set_size(bdev, 0);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100953 if (max_part > 0) {
954 blkdev_reread_part(bdev);
955 bdev->bd_invalidated = 1;
956 }
957}
958
Josef Bacik29eaadc2017-04-06 17:01:59 -0400959static void nbd_parse_flags(struct nbd_device *nbd)
Markus Pargmannd02cf532015-10-29 12:06:15 +0100960{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400961 struct nbd_config *config = nbd->config;
962 if (config->flags & NBD_FLAG_READ_ONLY)
Josef Bacik29eaadc2017-04-06 17:01:59 -0400963 set_disk_ro(nbd->disk, true);
964 else
965 set_disk_ro(nbd->disk, false);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400966 if (config->flags & NBD_FLAG_SEND_TRIM)
Markus Pargmannd02cf532015-10-29 12:06:15 +0100967 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Shaun McDowell685c9b22017-05-25 23:55:54 -0400968 if (config->flags & NBD_FLAG_SEND_FLUSH) {
969 if (config->flags & NBD_FLAG_SEND_FUA)
970 blk_queue_write_cache(nbd->disk->queue, true, true);
971 else
972 blk_queue_write_cache(nbd->disk->queue, true, false);
973 }
Markus Pargmannd02cf532015-10-29 12:06:15 +0100974 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600975 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100976}
977
Josef Bacik9561a7a2016-11-22 14:04:40 -0500978static void send_disconnects(struct nbd_device *nbd)
979{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400980 struct nbd_config *config = nbd->config;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500981 struct nbd_request request = {
982 .magic = htonl(NBD_REQUEST_MAGIC),
983 .type = htonl(NBD_CMD_DISC),
984 };
985 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
986 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500987 int i, ret;
988
Josef Bacik5ea8d102017-04-06 17:01:58 -0400989 for (i = 0; i < config->num_connections; i++) {
Josef Bacikb4b2aec2017-07-21 10:48:14 -0400990 struct nbd_sock *nsock = config->socks[i];
991
Al Viroc9f2b6a2015-11-12 05:09:35 -0500992 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
Josef Bacikb4b2aec2017-07-21 10:48:14 -0400993 mutex_lock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400994 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500995 if (ret <= 0)
996 dev_err(disk_to_dev(nbd->disk),
997 "Send disconnect failed %d\n", ret);
Josef Bacikb4b2aec2017-07-21 10:48:14 -0400998 mutex_unlock(&nsock->tx_lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500999 }
1000}
1001
Josef Bacik29eaadc2017-04-06 17:01:59 -04001002static int nbd_disconnect(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001003{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001004 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001005
Josef Bacik5ea8d102017-04-06 17:01:58 -04001006 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Josef Bacik2e134562017-07-21 10:48:13 -04001007 set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
1008 send_disconnects(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001009 return 0;
1010}
1011
Josef Bacik29eaadc2017-04-06 17:01:59 -04001012static void nbd_clear_sock(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001013{
1014 sock_shutdown(nbd);
1015 nbd_clear_que(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001016 nbd->task_setup = NULL;
Josef Bacik9442b732017-02-07 17:10:22 -05001017}
1018
Josef Bacik5ea8d102017-04-06 17:01:58 -04001019static void nbd_config_put(struct nbd_device *nbd)
1020{
1021 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1022 &nbd->config_lock)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001023 struct nbd_config *config = nbd->config;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001024 nbd_dev_dbg_close(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001025 nbd_size_clear(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001026 if (test_and_clear_bit(NBD_HAS_PID_FILE,
1027 &config->runtime_flags))
1028 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1029 nbd->task_recv = NULL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001030 nbd_clear_sock(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001031 if (config->num_connections) {
1032 int i;
1033 for (i = 0; i < config->num_connections; i++) {
1034 sockfd_put(config->socks[i]->sock);
1035 kfree(config->socks[i]);
1036 }
1037 kfree(config->socks);
1038 }
Ilya Dryomovfa976532017-05-23 17:49:55 +02001039 kfree(nbd->config);
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001040 nbd->config = NULL;
1041
1042 nbd->tag_set.timeout = 0;
1043 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Josef Bacika2c97902017-04-06 17:02:07 -04001044
Josef Bacik5ea8d102017-04-06 17:01:58 -04001045 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001046 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001047 module_put(THIS_MODULE);
1048 }
1049}
1050
Josef Bacike46c7282017-04-06 17:02:00 -04001051static int nbd_start_device(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001052{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001053 struct nbd_config *config = nbd->config;
1054 int num_connections = config->num_connections;
Josef Bacik9442b732017-02-07 17:10:22 -05001055 int error = 0, i;
1056
1057 if (nbd->task_recv)
1058 return -EBUSY;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001059 if (!config->socks)
Josef Bacik9442b732017-02-07 17:10:22 -05001060 return -EINVAL;
1061 if (num_connections > 1 &&
Josef Bacik5ea8d102017-04-06 17:01:58 -04001062 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
Josef Bacik9442b732017-02-07 17:10:22 -05001063 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001064 return -EINVAL;
Josef Bacik9442b732017-02-07 17:10:22 -05001065 }
1066
Josef Bacik5ea8d102017-04-06 17:01:58 -04001067 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
Josef Bacik9442b732017-02-07 17:10:22 -05001068 nbd->task_recv = current;
Josef Bacik9442b732017-02-07 17:10:22 -05001069
Josef Bacik29eaadc2017-04-06 17:01:59 -04001070 nbd_parse_flags(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001071
1072 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1073 if (error) {
1074 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001075 return error;
Josef Bacik9442b732017-02-07 17:10:22 -05001076 }
Josef Bacik29eaadc2017-04-06 17:01:59 -04001077 set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
Josef Bacik9442b732017-02-07 17:10:22 -05001078
1079 nbd_dev_dbg_init(nbd);
1080 for (i = 0; i < num_connections; i++) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001081 struct recv_thread_args *args;
1082
1083 args = kzalloc(sizeof(*args), GFP_KERNEL);
1084 if (!args) {
1085 sock_shutdown(nbd);
1086 return -ENOMEM;
1087 }
1088 sk_set_memalloc(config->socks[i]->sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001089 if (nbd->tag_set.timeout)
1090 config->socks[i]->sock->sk->sk_sndtimeo =
1091 nbd->tag_set.timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001092 atomic_inc(&config->recv_threads);
1093 refcount_inc(&nbd->config_refs);
1094 INIT_WORK(&args->work, recv_work);
1095 args->nbd = nbd;
1096 args->index = i;
1097 queue_work(recv_workqueue, &args->work);
Josef Bacik9442b732017-02-07 17:10:22 -05001098 }
Josef Bacik639812a2017-10-09 13:12:10 -04001099 nbd_size_update(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001100 return error;
1101}
1102
1103static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1104{
1105 struct nbd_config *config = nbd->config;
1106 int ret;
1107
1108 ret = nbd_start_device(nbd);
1109 if (ret)
1110 return ret;
1111
1112 bd_set_size(bdev, config->bytesize);
1113 if (max_part)
1114 bdev->bd_invalidated = 1;
1115 mutex_unlock(&nbd->config_lock);
1116 ret = wait_event_interruptible(config->recv_wq,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001117 atomic_read(&config->recv_threads) == 0);
Josef Bacike46c7282017-04-06 17:02:00 -04001118 if (ret)
Josef Bacik5ea8d102017-04-06 17:01:58 -04001119 sock_shutdown(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001120 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001121 bd_set_size(bdev, 0);
Josef Bacik9442b732017-02-07 17:10:22 -05001122 /* user requested, ignore socket errors */
Josef Bacik5ea8d102017-04-06 17:01:58 -04001123 if (test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001124 ret = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001125 if (test_bit(NBD_TIMEDOUT, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001126 ret = -ETIMEDOUT;
1127 return ret;
Josef Bacik9442b732017-02-07 17:10:22 -05001128}
Markus Pargmann30d53d92015-08-17 08:20:06 +02001129
Josef Bacik29eaadc2017-04-06 17:01:59 -04001130static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1131 struct block_device *bdev)
1132{
Josef Bacik2516ab12017-04-06 17:02:03 -04001133 sock_shutdown(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001134 kill_bdev(bdev);
1135 nbd_bdev_reset(bdev);
Josef Bacike46c7282017-04-06 17:02:00 -04001136 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1137 &nbd->config->runtime_flags))
1138 nbd_config_put(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001139}
1140
Josef Bacik9561a7a2016-11-22 14:04:40 -05001141/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -07001142static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -07001143 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001145 struct nbd_config *config = nbd->config;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -05001148 case NBD_DISCONNECT:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001149 return nbd_disconnect(nbd);
Markus Pargmann23272a672015-10-29 11:51:16 +01001150 case NBD_CLEAR_SOCK:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001151 nbd_clear_sock_ioctl(nbd, bdev);
1152 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001153 case NBD_SET_SOCK:
Josef Bacike46c7282017-04-06 17:02:00 -04001154 return nbd_add_socket(nbd, arg, false);
Josef Bacik9442b732017-02-07 17:10:22 -05001155 case NBD_SET_BLKSIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001156 nbd_size_set(nbd, arg,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001157 div_s64(config->bytesize, arg));
Josef Bacike5445412017-02-13 10:39:47 -05001158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 case NBD_SET_SIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001160 nbd_size_set(nbd, config->blksize,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001161 div_s64(arg, config->blksize));
Josef Bacike5445412017-02-13 10:39:47 -05001162 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +02001163 case NBD_SET_SIZE_BLOCKS:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001164 nbd_size_set(nbd, config->blksize, arg);
Josef Bacike5445412017-02-13 10:39:47 -05001165 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -07001166 case NBD_SET_TIMEOUT:
Josef Bacikf8586852017-03-24 14:08:28 -04001167 if (arg) {
1168 nbd->tag_set.timeout = arg * HZ;
1169 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
1170 }
Paul Clements7fdfd402007-10-16 23:27:37 -07001171 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001172
Paul Clements2f012502012-10-04 17:16:15 -07001173 case NBD_SET_FLAGS:
Josef Bacik5ea8d102017-04-06 17:01:58 -04001174 config->flags = arg;
Paul Clements2f012502012-10-04 17:16:15 -07001175 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001176 case NBD_DO_IT:
Josef Bacike46c7282017-04-06 17:02:00 -04001177 return nbd_start_device_ioctl(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -08001179 /*
1180 * This is for compatibility only. The queue is always cleared
1181 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
1184 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -07001185 /*
1186 * For compatibility only, we no longer keep a list of
1187 * outstanding requests.
1188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190 }
Pavel Machek1a2ad212009-04-02 16:58:41 -07001191 return -ENOTTY;
1192}
1193
1194static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1195 unsigned int cmd, unsigned long arg)
1196{
Wanlong Gaof4507162012-03-28 14:42:51 -07001197 struct nbd_device *nbd = bdev->bd_disk->private_data;
Josef Bacike46c7282017-04-06 17:02:00 -04001198 struct nbd_config *config = nbd->config;
1199 int error = -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001200
1201 if (!capable(CAP_SYS_ADMIN))
1202 return -EPERM;
1203
Josef Bacik1dae69b2017-05-05 22:25:18 -04001204 /* The block layer will pass back some non-nbd ioctls in case we have
1205 * special handling for them, but we don't so just return an error.
1206 */
1207 if (_IOC_TYPE(cmd) != 0xab)
1208 return -EINVAL;
1209
Josef Bacik9561a7a2016-11-22 14:04:40 -05001210 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001211
1212 /* Don't allow ioctl operations on a nbd device that was created with
1213 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1214 */
1215 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1216 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1217 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1218 else
1219 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -05001220 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -07001221 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Josef Bacik5ea8d102017-04-06 17:01:58 -04001224static struct nbd_config *nbd_alloc_config(void)
1225{
1226 struct nbd_config *config;
1227
1228 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1229 if (!config)
1230 return NULL;
1231 atomic_set(&config->recv_threads, 0);
1232 init_waitqueue_head(&config->recv_wq);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001233 init_waitqueue_head(&config->conn_wait);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001234 config->blksize = 1024;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001235 atomic_set(&config->live_connections, 0);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001236 try_module_get(THIS_MODULE);
1237 return config;
1238}
1239
1240static int nbd_open(struct block_device *bdev, fmode_t mode)
1241{
1242 struct nbd_device *nbd;
1243 int ret = 0;
1244
1245 mutex_lock(&nbd_index_mutex);
1246 nbd = bdev->bd_disk->private_data;
1247 if (!nbd) {
1248 ret = -ENXIO;
1249 goto out;
1250 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001251 if (!refcount_inc_not_zero(&nbd->refs)) {
1252 ret = -ENXIO;
1253 goto out;
1254 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001255 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1256 struct nbd_config *config;
1257
1258 mutex_lock(&nbd->config_lock);
1259 if (refcount_inc_not_zero(&nbd->config_refs)) {
1260 mutex_unlock(&nbd->config_lock);
1261 goto out;
1262 }
1263 config = nbd->config = nbd_alloc_config();
1264 if (!config) {
1265 ret = -ENOMEM;
1266 mutex_unlock(&nbd->config_lock);
1267 goto out;
1268 }
1269 refcount_set(&nbd->config_refs, 1);
Josef Bacikc6a47592017-04-06 17:02:06 -04001270 refcount_inc(&nbd->refs);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001271 mutex_unlock(&nbd->config_lock);
1272 }
1273out:
1274 mutex_unlock(&nbd_index_mutex);
1275 return ret;
1276}
1277
1278static void nbd_release(struct gendisk *disk, fmode_t mode)
1279{
1280 struct nbd_device *nbd = disk->private_data;
1281 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001282 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001283}
1284
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001285static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 .owner = THIS_MODULE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001288 .open = nbd_open,
1289 .release = nbd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001290 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -05001291 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292};
1293
Markus Pargmann30d53d92015-08-17 08:20:06 +02001294#if IS_ENABLED(CONFIG_DEBUG_FS)
1295
1296static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1297{
1298 struct nbd_device *nbd = s->private;
1299
1300 if (nbd->task_recv)
1301 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +02001302
1303 return 0;
1304}
1305
1306static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1307{
1308 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1309}
1310
1311static const struct file_operations nbd_dbg_tasks_ops = {
1312 .open = nbd_dbg_tasks_open,
1313 .read = seq_read,
1314 .llseek = seq_lseek,
1315 .release = single_release,
1316};
1317
1318static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1319{
1320 struct nbd_device *nbd = s->private;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001321 u32 flags = nbd->config->flags;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001322
1323 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1324
1325 seq_puts(s, "Known flags:\n");
1326
1327 if (flags & NBD_FLAG_HAS_FLAGS)
1328 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1329 if (flags & NBD_FLAG_READ_ONLY)
1330 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1331 if (flags & NBD_FLAG_SEND_FLUSH)
1332 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
Shaun McDowell685c9b22017-05-25 23:55:54 -04001333 if (flags & NBD_FLAG_SEND_FUA)
1334 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
Markus Pargmann30d53d92015-08-17 08:20:06 +02001335 if (flags & NBD_FLAG_SEND_TRIM)
1336 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1337
1338 return 0;
1339}
1340
1341static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1342{
1343 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1344}
1345
1346static const struct file_operations nbd_dbg_flags_ops = {
1347 .open = nbd_dbg_flags_open,
1348 .read = seq_read,
1349 .llseek = seq_lseek,
1350 .release = single_release,
1351};
1352
1353static int nbd_dev_dbg_init(struct nbd_device *nbd)
1354{
1355 struct dentry *dir;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001356 struct nbd_config *config = nbd->config;
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001357
1358 if (!nbd_dbg_dir)
1359 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001360
1361 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001362 if (!dir) {
1363 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1364 nbd_name(nbd));
1365 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001366 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001367 config->dbg_dir = dir;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001368
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001369 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001370 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -07001371 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001372 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -04001373 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001374
1375 return 0;
1376}
1377
1378static void nbd_dev_dbg_close(struct nbd_device *nbd)
1379{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001380 debugfs_remove_recursive(nbd->config->dbg_dir);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001381}
1382
1383static int nbd_dbg_init(void)
1384{
1385 struct dentry *dbg_dir;
1386
1387 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001388 if (!dbg_dir)
1389 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001390
1391 nbd_dbg_dir = dbg_dir;
1392
1393 return 0;
1394}
1395
1396static void nbd_dbg_close(void)
1397{
1398 debugfs_remove_recursive(nbd_dbg_dir);
1399}
1400
1401#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1402
1403static int nbd_dev_dbg_init(struct nbd_device *nbd)
1404{
1405 return 0;
1406}
1407
1408static void nbd_dev_dbg_close(struct nbd_device *nbd)
1409{
1410}
1411
1412static int nbd_dbg_init(void)
1413{
1414 return 0;
1415}
1416
1417static void nbd_dbg_close(void)
1418{
1419}
1420
1421#endif
1422
Christoph Hellwigd6296d32017-05-01 10:19:08 -06001423static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1424 unsigned int hctx_idx, unsigned int numa_node)
Josef Bacikfd8383f2016-09-08 12:33:37 -07001425{
1426 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd6296d32017-05-01 10:19:08 -06001427 cmd->nbd = set->driver_data;
Josef Bacikfd8383f2016-09-08 12:33:37 -07001428 return 0;
1429}
1430
Eric Biggersf363b082017-03-30 13:39:16 -07001431static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001432 .queue_rq = nbd_queue_rq,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +02001433 .complete = nbd_complete_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001434 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001435 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001436};
1437
Josef Bacikb0d91112017-02-01 16:11:40 -05001438static int nbd_dev_add(int index)
1439{
1440 struct nbd_device *nbd;
1441 struct gendisk *disk;
1442 struct request_queue *q;
1443 int err = -ENOMEM;
1444
1445 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1446 if (!nbd)
1447 goto out;
1448
1449 disk = alloc_disk(1 << part_shift);
1450 if (!disk)
1451 goto out_free_nbd;
1452
1453 if (index >= 0) {
1454 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1455 GFP_KERNEL);
1456 if (err == -ENOSPC)
1457 err = -EEXIST;
1458 } else {
1459 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1460 if (err >= 0)
1461 index = err;
1462 }
1463 if (err < 0)
1464 goto out_free_disk;
1465
Josef Bacike46c7282017-04-06 17:02:00 -04001466 nbd->index = index;
Josef Bacikb0d91112017-02-01 16:11:40 -05001467 nbd->disk = disk;
1468 nbd->tag_set.ops = &nbd_mq_ops;
1469 nbd->tag_set.nr_hw_queues = 1;
1470 nbd->tag_set.queue_depth = 128;
1471 nbd->tag_set.numa_node = NUMA_NO_NODE;
1472 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1473 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1474 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
1475 nbd->tag_set.driver_data = nbd;
1476
1477 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1478 if (err)
1479 goto out_free_idr;
1480
1481 q = blk_mq_init_queue(&nbd->tag_set);
1482 if (IS_ERR(q)) {
1483 err = PTR_ERR(q);
1484 goto out_free_tags;
1485 }
1486 disk->queue = q;
1487
1488 /*
1489 * Tell the block layer that we are not a rotational device
1490 */
1491 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
1492 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1493 disk->queue->limits.discard_granularity = 512;
1494 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Josef Bacikebb16d02017-04-18 16:22:51 -04001495 blk_queue_max_segment_size(disk->queue, UINT_MAX);
Josef Bacik1cc1f172017-04-20 15:47:01 -04001496 blk_queue_max_segments(disk->queue, USHRT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001497 blk_queue_max_hw_sectors(disk->queue, 65536);
1498 disk->queue->limits.max_sectors = 256;
1499
Josef Bacikb0d91112017-02-01 16:11:40 -05001500 mutex_init(&nbd->config_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001501 refcount_set(&nbd->config_refs, 0);
Josef Bacikc6a47592017-04-06 17:02:06 -04001502 refcount_set(&nbd->refs, 1);
1503 INIT_LIST_HEAD(&nbd->list);
Josef Bacikb0d91112017-02-01 16:11:40 -05001504 disk->major = NBD_MAJOR;
1505 disk->first_minor = index << part_shift;
1506 disk->fops = &nbd_fops;
1507 disk->private_data = nbd;
1508 sprintf(disk->disk_name, "nbd%d", index);
Josef Bacikb0d91112017-02-01 16:11:40 -05001509 add_disk(disk);
Josef Bacik47d902b2017-04-06 17:02:05 -04001510 nbd_total_devices++;
Josef Bacikb0d91112017-02-01 16:11:40 -05001511 return index;
1512
1513out_free_tags:
1514 blk_mq_free_tag_set(&nbd->tag_set);
1515out_free_idr:
1516 idr_remove(&nbd_index_idr, index);
1517out_free_disk:
1518 put_disk(disk);
1519out_free_nbd:
1520 kfree(nbd);
1521out:
1522 return err;
1523}
1524
Josef Bacike46c7282017-04-06 17:02:00 -04001525static int find_free_cb(int id, void *ptr, void *data)
1526{
1527 struct nbd_device *nbd = ptr;
1528 struct nbd_device **found = data;
1529
1530 if (!refcount_read(&nbd->config_refs)) {
1531 *found = nbd;
1532 return 1;
1533 }
1534 return 0;
1535}
1536
1537/* Netlink interface. */
1538static struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1539 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1540 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1541 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1542 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1543 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1544 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1545 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
Josef Bacik560bc4b2017-04-06 17:02:04 -04001546 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001547 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
Josef Bacike46c7282017-04-06 17:02:00 -04001548};
1549
1550static struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1551 [NBD_SOCK_FD] = { .type = NLA_U32 },
1552};
1553
Josef Bacik47d902b2017-04-06 17:02:05 -04001554/* We don't use this right now since we don't parse the incoming list, but we
1555 * still want it here so userspace knows what to expect.
1556 */
1557static struct nla_policy __attribute__((unused))
1558nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1559 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1560 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1561};
1562
Josef Bacike46c7282017-04-06 17:02:00 -04001563static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1564{
1565 struct nbd_device *nbd = NULL;
1566 struct nbd_config *config;
1567 int index = -1;
1568 int ret;
Josef Bacika2c97902017-04-06 17:02:07 -04001569 bool put_dev = false;
Josef Bacike46c7282017-04-06 17:02:00 -04001570
1571 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1572 return -EPERM;
1573
1574 if (info->attrs[NBD_ATTR_INDEX])
1575 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1576 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1577 printk(KERN_ERR "nbd: must specify at least one socket\n");
1578 return -EINVAL;
1579 }
1580 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1581 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1582 return -EINVAL;
1583 }
1584again:
1585 mutex_lock(&nbd_index_mutex);
1586 if (index == -1) {
1587 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1588 if (ret == 0) {
1589 int new_index;
1590 new_index = nbd_dev_add(-1);
1591 if (new_index < 0) {
1592 mutex_unlock(&nbd_index_mutex);
1593 printk(KERN_ERR "nbd: failed to add new device\n");
1594 return ret;
1595 }
1596 nbd = idr_find(&nbd_index_idr, new_index);
1597 }
1598 } else {
1599 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike6a76272017-08-14 18:25:33 +00001600 if (!nbd) {
1601 ret = nbd_dev_add(index);
1602 if (ret < 0) {
1603 mutex_unlock(&nbd_index_mutex);
1604 printk(KERN_ERR "nbd: failed to add new device\n");
1605 return ret;
1606 }
1607 nbd = idr_find(&nbd_index_idr, index);
1608 }
Josef Bacike46c7282017-04-06 17:02:00 -04001609 }
Josef Bacike46c7282017-04-06 17:02:00 -04001610 if (!nbd) {
1611 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1612 index);
Josef Bacikc6a47592017-04-06 17:02:06 -04001613 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001614 return -EINVAL;
1615 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001616 if (!refcount_inc_not_zero(&nbd->refs)) {
1617 mutex_unlock(&nbd_index_mutex);
1618 if (index == -1)
1619 goto again;
1620 printk(KERN_ERR "nbd: device at index %d is going down\n",
1621 index);
1622 return -EINVAL;
1623 }
1624 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001625
1626 mutex_lock(&nbd->config_lock);
1627 if (refcount_read(&nbd->config_refs)) {
1628 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001629 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001630 if (index == -1)
1631 goto again;
1632 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1633 return -EBUSY;
1634 }
1635 if (WARN_ON(nbd->config)) {
1636 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001637 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001638 return -EINVAL;
1639 }
1640 config = nbd->config = nbd_alloc_config();
1641 if (!nbd->config) {
1642 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001643 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001644 printk(KERN_ERR "nbd: couldn't allocate config\n");
1645 return -ENOMEM;
1646 }
1647 refcount_set(&nbd->config_refs, 1);
1648 set_bit(NBD_BOUND, &config->runtime_flags);
1649
1650 if (info->attrs[NBD_ATTR_SIZE_BYTES]) {
1651 u64 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1652 nbd_size_set(nbd, config->blksize,
1653 div64_u64(bytes, config->blksize));
1654 }
1655 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1656 u64 bsize =
1657 nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1658 nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
1659 }
1660 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1661 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1662 nbd->tag_set.timeout = timeout * HZ;
1663 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1664 }
Josef Bacik560bc4b2017-04-06 17:02:04 -04001665 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1666 config->dead_conn_timeout =
1667 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1668 config->dead_conn_timeout *= HZ;
1669 }
Josef Bacike46c7282017-04-06 17:02:00 -04001670 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1671 config->flags =
1672 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
Josef Bacika2c97902017-04-06 17:02:07 -04001673 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1674 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1675 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1676 set_bit(NBD_DESTROY_ON_DISCONNECT,
1677 &config->runtime_flags);
1678 put_dev = true;
1679 }
1680 }
1681
Josef Bacike46c7282017-04-06 17:02:00 -04001682 if (info->attrs[NBD_ATTR_SOCKETS]) {
1683 struct nlattr *attr;
1684 int rem, fd;
1685
1686 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1687 rem) {
1688 struct nlattr *socks[NBD_SOCK_MAX+1];
1689
1690 if (nla_type(attr) != NBD_SOCK_ITEM) {
1691 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1692 ret = -EINVAL;
1693 goto out;
1694 }
1695 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
Linus Torvalds8d65b082017-05-02 16:40:27 -07001696 nbd_sock_policy, info->extack);
Josef Bacike46c7282017-04-06 17:02:00 -04001697 if (ret != 0) {
1698 printk(KERN_ERR "nbd: error processing sock list\n");
1699 ret = -EINVAL;
1700 goto out;
1701 }
1702 if (!socks[NBD_SOCK_FD])
1703 continue;
1704 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1705 ret = nbd_add_socket(nbd, fd, true);
1706 if (ret)
1707 goto out;
1708 }
1709 }
1710 ret = nbd_start_device(nbd);
1711out:
1712 mutex_unlock(&nbd->config_lock);
1713 if (!ret) {
1714 set_bit(NBD_HAS_CONFIG_REF, &config->runtime_flags);
1715 refcount_inc(&nbd->config_refs);
1716 nbd_connect_reply(info, nbd->index);
1717 }
1718 nbd_config_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04001719 if (put_dev)
1720 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001721 return ret;
1722}
1723
1724static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
1725{
1726 struct nbd_device *nbd;
1727 int index;
1728
1729 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1730 return -EPERM;
1731
1732 if (!info->attrs[NBD_ATTR_INDEX]) {
1733 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
1734 return -EINVAL;
1735 }
1736 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1737 mutex_lock(&nbd_index_mutex);
1738 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike46c7282017-04-06 17:02:00 -04001739 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001740 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001741 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1742 index);
1743 return -EINVAL;
1744 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001745 if (!refcount_inc_not_zero(&nbd->refs)) {
1746 mutex_unlock(&nbd_index_mutex);
1747 printk(KERN_ERR "nbd: device at index %d is going down\n",
1748 index);
1749 return -EINVAL;
1750 }
1751 mutex_unlock(&nbd_index_mutex);
1752 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1753 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001754 return 0;
Josef Bacikc6a47592017-04-06 17:02:06 -04001755 }
Josef Bacike46c7282017-04-06 17:02:00 -04001756 mutex_lock(&nbd->config_lock);
1757 nbd_disconnect(nbd);
1758 mutex_unlock(&nbd->config_lock);
1759 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1760 &nbd->config->runtime_flags))
1761 nbd_config_put(nbd);
1762 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001763 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001764 return 0;
1765}
1766
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001767static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
1768{
1769 struct nbd_device *nbd = NULL;
1770 struct nbd_config *config;
1771 int index;
1772 int ret = -EINVAL;
Josef Bacika2c97902017-04-06 17:02:07 -04001773 bool put_dev = false;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001774
1775 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1776 return -EPERM;
1777
1778 if (!info->attrs[NBD_ATTR_INDEX]) {
1779 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
1780 return -EINVAL;
1781 }
1782 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1783 mutex_lock(&nbd_index_mutex);
1784 nbd = idr_find(&nbd_index_idr, index);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001785 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001786 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001787 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
1788 index);
1789 return -EINVAL;
1790 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001791 if (!refcount_inc_not_zero(&nbd->refs)) {
1792 mutex_unlock(&nbd_index_mutex);
1793 printk(KERN_ERR "nbd: device at index %d is going down\n",
1794 index);
1795 return -EINVAL;
1796 }
1797 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001798
1799 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1800 dev_err(nbd_to_dev(nbd),
1801 "not configured, cannot reconfigure\n");
Josef Bacikc6a47592017-04-06 17:02:06 -04001802 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001803 return -EINVAL;
1804 }
1805
1806 mutex_lock(&nbd->config_lock);
1807 config = nbd->config;
1808 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1809 !nbd->task_recv) {
1810 dev_err(nbd_to_dev(nbd),
1811 "not configured, cannot reconfigure\n");
1812 goto out;
1813 }
1814
1815 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1816 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1817 nbd->tag_set.timeout = timeout * HZ;
1818 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1819 }
Josef Bacik560bc4b2017-04-06 17:02:04 -04001820 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1821 config->dead_conn_timeout =
1822 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1823 config->dead_conn_timeout *= HZ;
1824 }
Josef Bacika2c97902017-04-06 17:02:07 -04001825 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1826 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1827 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1828 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1829 &config->runtime_flags))
1830 put_dev = true;
1831 } else {
1832 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1833 &config->runtime_flags))
1834 refcount_inc(&nbd->refs);
1835 }
1836 }
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001837
1838 if (info->attrs[NBD_ATTR_SOCKETS]) {
1839 struct nlattr *attr;
1840 int rem, fd;
1841
1842 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1843 rem) {
1844 struct nlattr *socks[NBD_SOCK_MAX+1];
1845
1846 if (nla_type(attr) != NBD_SOCK_ITEM) {
1847 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1848 ret = -EINVAL;
1849 goto out;
1850 }
1851 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
Linus Torvalds8d65b082017-05-02 16:40:27 -07001852 nbd_sock_policy, info->extack);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001853 if (ret != 0) {
1854 printk(KERN_ERR "nbd: error processing sock list\n");
1855 ret = -EINVAL;
1856 goto out;
1857 }
1858 if (!socks[NBD_SOCK_FD])
1859 continue;
1860 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1861 ret = nbd_reconnect_socket(nbd, fd);
1862 if (ret) {
1863 if (ret == -ENOSPC)
1864 ret = 0;
1865 goto out;
1866 }
1867 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
1868 }
1869 }
1870out:
1871 mutex_unlock(&nbd->config_lock);
1872 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001873 nbd_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04001874 if (put_dev)
1875 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001876 return ret;
1877}
1878
Josef Bacike46c7282017-04-06 17:02:00 -04001879static const struct genl_ops nbd_connect_genl_ops[] = {
1880 {
1881 .cmd = NBD_CMD_CONNECT,
1882 .policy = nbd_attr_policy,
1883 .doit = nbd_genl_connect,
1884 },
1885 {
1886 .cmd = NBD_CMD_DISCONNECT,
1887 .policy = nbd_attr_policy,
1888 .doit = nbd_genl_disconnect,
1889 },
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001890 {
1891 .cmd = NBD_CMD_RECONFIGURE,
1892 .policy = nbd_attr_policy,
1893 .doit = nbd_genl_reconfigure,
1894 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001895 {
1896 .cmd = NBD_CMD_STATUS,
1897 .policy = nbd_attr_policy,
1898 .doit = nbd_genl_status,
1899 },
Josef Bacike46c7282017-04-06 17:02:00 -04001900};
1901
Josef Bacik799f9a32017-04-06 17:02:02 -04001902static const struct genl_multicast_group nbd_mcast_grps[] = {
1903 { .name = NBD_GENL_MCAST_GROUP_NAME, },
1904};
1905
Josef Bacike46c7282017-04-06 17:02:00 -04001906static struct genl_family nbd_genl_family __ro_after_init = {
1907 .hdrsize = 0,
1908 .name = NBD_GENL_FAMILY_NAME,
1909 .version = NBD_GENL_VERSION,
1910 .module = THIS_MODULE,
1911 .ops = nbd_connect_genl_ops,
1912 .n_ops = ARRAY_SIZE(nbd_connect_genl_ops),
1913 .maxattr = NBD_ATTR_MAX,
Josef Bacik799f9a32017-04-06 17:02:02 -04001914 .mcgrps = nbd_mcast_grps,
1915 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
Josef Bacike46c7282017-04-06 17:02:00 -04001916};
1917
Josef Bacik47d902b2017-04-06 17:02:05 -04001918static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
1919{
1920 struct nlattr *dev_opt;
1921 u8 connected = 0;
1922 int ret;
1923
1924 /* This is a little racey, but for status it's ok. The
1925 * reason we don't take a ref here is because we can't
1926 * take a ref in the index == -1 case as we would need
1927 * to put under the nbd_index_mutex, which could
1928 * deadlock if we are configured to remove ourselves
1929 * once we're disconnected.
1930 */
1931 if (refcount_read(&nbd->config_refs))
1932 connected = 1;
1933 dev_opt = nla_nest_start(reply, NBD_DEVICE_ITEM);
1934 if (!dev_opt)
1935 return -EMSGSIZE;
1936 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
1937 if (ret)
1938 return -EMSGSIZE;
1939 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
1940 connected);
1941 if (ret)
1942 return -EMSGSIZE;
1943 nla_nest_end(reply, dev_opt);
1944 return 0;
1945}
1946
1947static int status_cb(int id, void *ptr, void *data)
1948{
1949 struct nbd_device *nbd = ptr;
1950 return populate_nbd_status(nbd, (struct sk_buff *)data);
1951}
1952
1953static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
1954{
1955 struct nlattr *dev_list;
1956 struct sk_buff *reply;
1957 void *reply_head;
1958 size_t msg_size;
1959 int index = -1;
1960 int ret = -ENOMEM;
1961
1962 if (info->attrs[NBD_ATTR_INDEX])
1963 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1964
1965 mutex_lock(&nbd_index_mutex);
1966
1967 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
1968 nla_attr_size(sizeof(u8)));
1969 msg_size *= (index == -1) ? nbd_total_devices : 1;
1970
1971 reply = genlmsg_new(msg_size, GFP_KERNEL);
1972 if (!reply)
1973 goto out;
1974 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
1975 NBD_CMD_STATUS);
1976 if (!reply_head) {
1977 nlmsg_free(reply);
1978 goto out;
1979 }
1980
1981 dev_list = nla_nest_start(reply, NBD_ATTR_DEVICE_LIST);
1982 if (index == -1) {
1983 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
1984 if (ret) {
1985 nlmsg_free(reply);
1986 goto out;
1987 }
1988 } else {
1989 struct nbd_device *nbd;
1990 nbd = idr_find(&nbd_index_idr, index);
1991 if (nbd) {
1992 ret = populate_nbd_status(nbd, reply);
1993 if (ret) {
1994 nlmsg_free(reply);
1995 goto out;
1996 }
1997 }
1998 }
1999 nla_nest_end(reply, dev_list);
2000 genlmsg_end(reply, reply_head);
2001 genlmsg_reply(reply, info);
2002 ret = 0;
2003out:
2004 mutex_unlock(&nbd_index_mutex);
2005 return ret;
2006}
2007
Josef Bacike46c7282017-04-06 17:02:00 -04002008static void nbd_connect_reply(struct genl_info *info, int index)
2009{
2010 struct sk_buff *skb;
2011 void *msg_head;
2012 int ret;
2013
2014 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2015 if (!skb)
2016 return;
2017 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2018 NBD_CMD_CONNECT);
2019 if (!msg_head) {
2020 nlmsg_free(skb);
2021 return;
2022 }
2023 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2024 if (ret) {
2025 nlmsg_free(skb);
2026 return;
2027 }
2028 genlmsg_end(skb, msg_head);
2029 genlmsg_reply(skb, info);
2030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Josef Bacik799f9a32017-04-06 17:02:02 -04002032static void nbd_mcast_index(int index)
2033{
2034 struct sk_buff *skb;
2035 void *msg_head;
2036 int ret;
2037
2038 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2039 if (!skb)
2040 return;
2041 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2042 NBD_CMD_LINK_DEAD);
2043 if (!msg_head) {
2044 nlmsg_free(skb);
2045 return;
2046 }
2047 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2048 if (ret) {
2049 nlmsg_free(skb);
2050 return;
2051 }
2052 genlmsg_end(skb, msg_head);
2053 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2054}
2055
2056static void nbd_dead_link_work(struct work_struct *work)
2057{
2058 struct link_dead_args *args = container_of(work, struct link_dead_args,
2059 work);
2060 nbd_mcast_index(args->index);
2061 kfree(args);
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static int __init nbd_init(void)
2065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 int i;
2067
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08002068 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002070 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02002071 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002072 return -EINVAL;
2073 }
2074
2075 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02002076 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002077 part_shift = fls(max_part);
2078
Namhyung Kim5988ce22011-05-28 14:44:46 +02002079 /*
2080 * Adjust max_part according to part_shift as it is exported
2081 * to user space so that user can know the max number of
2082 * partition kernel should be able to manage.
2083 *
2084 * Note that -1 is required because partition 0 is reserved
2085 * for the whole disk.
2086 */
2087 max_part = (1UL << part_shift) - 1;
2088 }
2089
Namhyung Kim3b271082011-05-28 14:44:46 +02002090 if ((1UL << part_shift) > DISK_MAX_PARTS)
2091 return -EINVAL;
2092
2093 if (nbds_max > 1UL << (MINORBITS - part_shift))
2094 return -EINVAL;
Josef Bacik124d6db2017-02-01 16:11:11 -05002095 recv_workqueue = alloc_workqueue("knbd-recv",
2096 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2097 if (!recv_workqueue)
2098 return -ENOMEM;
Namhyung Kim3b271082011-05-28 14:44:46 +02002099
Josef Bacik6330a2d2017-02-15 16:49:48 -05002100 if (register_blkdev(NBD_MAJOR, "nbd")) {
2101 destroy_workqueue(recv_workqueue);
Josef Bacikb0d91112017-02-01 16:11:40 -05002102 return -EIO;
Josef Bacik6330a2d2017-02-15 16:49:48 -05002103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Josef Bacike46c7282017-04-06 17:02:00 -04002105 if (genl_register_family(&nbd_genl_family)) {
2106 unregister_blkdev(NBD_MAJOR, "nbd");
2107 destroy_workqueue(recv_workqueue);
2108 return -EINVAL;
2109 }
Markus Pargmann30d53d92015-08-17 08:20:06 +02002110 nbd_dbg_init();
2111
Josef Bacikb0d91112017-02-01 16:11:40 -05002112 mutex_lock(&nbd_index_mutex);
2113 for (i = 0; i < nbds_max; i++)
2114 nbd_dev_add(i);
2115 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05002117}
2118
2119static int nbd_exit_cb(int id, void *ptr, void *data)
2120{
Josef Bacikc6a47592017-04-06 17:02:06 -04002121 struct list_head *list = (struct list_head *)data;
Josef Bacikb0d91112017-02-01 16:11:40 -05002122 struct nbd_device *nbd = ptr;
Josef Bacikc6a47592017-04-06 17:02:06 -04002123
Josef Bacikc6a47592017-04-06 17:02:06 -04002124 list_add_tail(&nbd->list, list);
Josef Bacikb0d91112017-02-01 16:11:40 -05002125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128static void __exit nbd_cleanup(void)
2129{
Josef Bacikc6a47592017-04-06 17:02:06 -04002130 struct nbd_device *nbd;
2131 LIST_HEAD(del_list);
2132
Markus Pargmann30d53d92015-08-17 08:20:06 +02002133 nbd_dbg_close();
2134
Josef Bacikc6a47592017-04-06 17:02:06 -04002135 mutex_lock(&nbd_index_mutex);
2136 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2137 mutex_unlock(&nbd_index_mutex);
2138
Josef Bacik60ae36a2017-04-28 09:49:19 -04002139 while (!list_empty(&del_list)) {
2140 nbd = list_first_entry(&del_list, struct nbd_device, list);
2141 list_del_init(&nbd->list);
2142 if (refcount_read(&nbd->refs) != 1)
Josef Bacikc6a47592017-04-06 17:02:06 -04002143 printk(KERN_ERR "nbd: possibly leaking a device\n");
2144 nbd_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002145 }
2146
Josef Bacikb0d91112017-02-01 16:11:40 -05002147 idr_destroy(&nbd_index_idr);
Josef Bacike46c7282017-04-06 17:02:00 -04002148 genl_unregister_family(&nbd_genl_family);
Josef Bacik124d6db2017-02-01 16:11:11 -05002149 destroy_workqueue(recv_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153module_init(nbd_init);
2154module_exit(nbd_cleanup);
2155
2156MODULE_DESCRIPTION("Network Block Device");
2157MODULE_LICENSE("GPL");
2158
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07002159module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002160MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2161module_param(max_part, int, 0444);
Josef Bacik7a8362a2017-08-14 18:56:16 +00002162MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");