blob: 9adfb5445f8dca5a88a4ffe59d3573ed5b854e02 [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 }
291
Josef Bacik560bc4b2017-04-06 17:02:04 -0400292 /* If we are waiting on our dead timer then we could get timeout
293 * callbacks for our request. For this we just want to reset the timer
294 * and let the queue side take care of everything.
295 */
296 if (!completion_done(&cmd->send_complete)) {
297 nbd_config_put(nbd);
298 return BLK_EH_RESET_TIMER;
299 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400300 config = nbd->config;
301
302 if (config->num_connections > 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400303 dev_err_ratelimited(nbd_to_dev(nbd),
304 "Connection timed out, retrying\n");
Josef Bacikf3733242017-04-06 17:01:57 -0400305 /*
306 * Hooray we have more connections, requeue this IO, the submit
307 * path will put it on a real connection.
308 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400309 if (config->socks && config->num_connections > 1) {
310 if (cmd->index < config->num_connections) {
Josef Bacikf3733242017-04-06 17:01:57 -0400311 struct nbd_sock *nsock =
Josef Bacik5ea8d102017-04-06 17:01:58 -0400312 config->socks[cmd->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400313 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400314 /* We can have multiple outstanding requests, so
315 * we don't want to mark the nsock dead if we've
316 * already reconnected with a new socket, so
317 * only mark it dead if its the same socket we
318 * were sent out on.
319 */
320 if (cmd->cookie == nsock->cookie)
321 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400322 mutex_unlock(&nsock->tx_lock);
323 }
Josef Bacikf3733242017-04-06 17:01:57 -0400324 blk_mq_requeue_request(req, true);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400325 nbd_config_put(nbd);
Josef Bacikf3733242017-04-06 17:01:57 -0400326 return BLK_EH_NOT_HANDLED;
327 }
Josef Bacikf3733242017-04-06 17:01:57 -0400328 } else {
329 dev_err_ratelimited(nbd_to_dev(nbd),
330 "Connection timed out\n");
331 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400332 set_bit(NBD_TIMEDOUT, &config->runtime_flags);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200333 cmd->status = BLK_STS_IOERR;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500334 sock_shutdown(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400335 nbd_config_put(nbd);
336
Josef Bacik0eadf372016-09-08 12:33:40 -0700337 return BLK_EH_HANDLED;
Paul Clements7fdfd402007-10-16 23:27:37 -0700338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * Send or receive packet.
342 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500343static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400344 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400346 struct nbd_config *config = nbd->config;
347 struct socket *sock = config->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int result;
349 struct msghdr msg;
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700350 unsigned int noreclaim_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700352 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500353 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200354 "Attempted %s on closed socket in sock_xmit\n",
355 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700356 return -EINVAL;
357 }
358
Al Viroc9f2b6a2015-11-12 05:09:35 -0500359 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500360
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700361 noreclaim_flag = memalloc_noreclaim_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700363 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 msg.msg_name = NULL;
365 msg.msg_namelen = 0;
366 msg.msg_control = NULL;
367 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
369
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200370 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500371 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200372 else
Al Viroc1696ca2015-11-12 04:51:19 -0500373 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (result <= 0) {
376 if (result == 0)
377 result = -EPIPE; /* short read */
378 break;
379 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400380 if (sent)
381 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500382 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Vlastimil Babkaf1083042017-05-08 15:59:53 -0700384 memalloc_noreclaim_restore(noreclaim_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 return result;
387}
388
Josef Bacik32e67a32017-10-24 15:57:18 -0400389/*
390 * Different settings for sk->sk_sndtimeo can result in different return values
391 * if there is a signal pending when we enter sendmsg, because reasons?
392 */
393static inline int was_interrupted(int result)
394{
395 return result == -ERESTARTSYS || result == -EINTR;
396}
397
Paul Clements7fdfd402007-10-16 23:27:37 -0700398/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500399static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700401 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400402 struct nbd_config *config = nbd->config;
403 struct nbd_sock *nsock = config->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500404 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500405 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
406 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
407 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900408 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700409 struct bio *bio;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200410 u32 type;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400411 u32 nbd_cmd_flags = 0;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500412 u32 tag = blk_mq_unique_tag(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400413 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200414
Al Viroc9f2b6a2015-11-12 05:09:35 -0500415 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
416
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100417 switch (req_op(req)) {
418 case REQ_OP_DISCARD:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200419 type = NBD_CMD_TRIM;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100420 break;
421 case REQ_OP_FLUSH:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200422 type = NBD_CMD_FLUSH;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100423 break;
424 case REQ_OP_WRITE:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200425 type = NBD_CMD_WRITE;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100426 break;
427 case REQ_OP_READ:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200428 type = NBD_CMD_READ;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100429 break;
430 default:
431 return -EIO;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100434 if (rq_data_dir(req) == WRITE &&
Josef Bacik5ea8d102017-04-06 17:01:58 -0400435 (config->flags & NBD_FLAG_READ_ONLY)) {
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100436 dev_err_ratelimited(disk_to_dev(nbd->disk),
437 "Write on read-only\n");
438 return -EIO;
439 }
440
Shaun McDowell685c9b22017-05-25 23:55:54 -0400441 if (req->cmd_flags & REQ_FUA)
442 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
443
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400444 /* We did a partial send previously, and we at least sent the whole
445 * request struct, so just go and send the rest of the pages in the
446 * request.
447 */
448 if (sent) {
449 if (sent >= sizeof(request)) {
450 skip = sent - sizeof(request);
451 goto send_pages;
452 }
453 iov_iter_advance(&from, sent);
454 }
Josef Bacikf3733242017-04-06 17:01:57 -0400455 cmd->index = index;
Josef Bacik799f9a32017-04-06 17:02:02 -0400456 cmd->cookie = nsock->cookie;
Shaun McDowell685c9b22017-05-25 23:55:54 -0400457 request.type = htonl(type | nbd_cmd_flags);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500458 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800459 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
460 request.len = htonl(size);
461 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500462 memcpy(request.handle, &tag, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Markus Pargmannd18509f2015-04-02 10:11:38 +0200464 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700465 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200466 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500467 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400468 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400470 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400471 /* If we havne't sent anything we can just return BUSY,
472 * however if we have sent something we need to make
473 * sure we only allow this req to be sent until we are
474 * completely done.
475 */
476 if (sent) {
477 nsock->pending = req;
478 nsock->sent = sent;
479 }
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200480 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400481 }
Josef Bacika897b662016-12-05 16:20:29 -0500482 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200483 "Send control failed (result %d)\n", result);
Josef Bacikf3733242017-04-06 17:01:57 -0400484 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400486send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700487 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400488 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700489
Jens Axboe429a7872016-11-17 12:30:37 -0700490 bio = req->bio;
491 while (bio) {
492 struct bio *next = bio->bi_next;
493 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800494 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700495
496 bio_for_each_segment(bvec, bio, iter) {
497 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500498 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700499
Markus Pargmannd18509f2015-04-02 10:11:38 +0200500 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700501 cmd, bvec.bv_len);
Al Viroc9f2b6a2015-11-12 05:09:35 -0500502 iov_iter_bvec(&from, ITER_BVEC | WRITE,
503 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400504 if (skip) {
505 if (skip >= iov_iter_count(&from)) {
506 skip -= iov_iter_count(&from);
507 continue;
508 }
509 iov_iter_advance(&from, skip);
510 skip = 0;
511 }
512 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200513 if (result <= 0) {
Josef Bacik32e67a32017-10-24 15:57:18 -0400514 if (was_interrupted(result)) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400515 /* We've already sent the header, we
516 * have no choice but to set pending and
517 * return BUSY.
518 */
519 nsock->pending = req;
520 nsock->sent = sent;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200521 return BLK_STS_RESOURCE;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400522 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700523 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200524 "Send data failed (result %d)\n",
525 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400526 return -EAGAIN;
Jens Axboe6c92e692007-08-16 13:43:12 +0200527 }
Jens Axboe429a7872016-11-17 12:30:37 -0700528 /*
529 * The completion might already have come in,
530 * so break for the last one instead of letting
531 * the iterator do it. This prevents use-after-free
532 * of the bio.
533 */
534 if (is_last)
535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Jens Axboe429a7872016-11-17 12:30:37 -0700537 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400539out:
540 nsock->pending = NULL;
541 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500546static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400548 struct nbd_config *config = nbd->config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int result;
550 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700551 struct nbd_cmd *cmd;
552 struct request *req = NULL;
553 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500554 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500555 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
556 struct iov_iter to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 reply.magic = 0;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500559 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400560 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (result <= 0) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400562 if (!nbd_disconnected(config))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500563 dev_err(disk_to_dev(nbd->disk),
564 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200565 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700567
568 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700569 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700570 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200571 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700572 }
573
Josef Bacik9561a7a2016-11-22 14:04:40 -0500574 memcpy(&tag, reply.handle, sizeof(u32));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800575
Josef Bacikfd8383f2016-09-08 12:33:37 -0700576 hwq = blk_mq_unique_tag_to_hwq(tag);
577 if (hwq < nbd->tag_set.nr_hw_queues)
578 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
579 blk_mq_unique_tag_to_tag(tag));
580 if (!req || !blk_mq_request_started(req)) {
581 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
582 tag, req);
583 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700585 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700587 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200588 ntohl(reply.error));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200589 cmd->status = BLK_STS_IOERR;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700590 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
Josef Bacikfd8383f2016-09-08 12:33:37 -0700593 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200594 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200595 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800596 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200597
598 rq_for_each_segment(bvec, req, iter) {
Al Viroc9f2b6a2015-11-12 05:09:35 -0500599 iov_iter_bvec(&to, ITER_BVEC | READ,
600 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400601 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200602 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700603 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200604 result);
Josef Bacikf3733242017-04-06 17:01:57 -0400605 /*
606 * If we've disconnected or we only have 1
607 * connection then we need to make sure we
608 * complete this request, otherwise error out
609 * and let the timeout stuff handle resubmitting
610 * this request onto another connection.
611 */
Josef Bacik5ea8d102017-04-06 17:01:58 -0400612 if (nbd_disconnected(config) ||
613 config->num_connections <= 1) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200614 cmd->status = BLK_STS_IOERR;
Josef Bacikf3733242017-04-06 17:01:57 -0400615 return cmd;
616 }
617 return ERR_PTR(-EIO);
Jens Axboe6c92e692007-08-16 13:43:12 +0200618 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200619 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700620 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500622 } else {
623 /* See the comment in nbd_queue_rq. */
624 wait_for_completion(&cmd->send_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700626 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Josef Bacik9561a7a2016-11-22 14:04:40 -0500629static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500631 struct recv_thread_args *args = container_of(work,
632 struct recv_thread_args,
633 work);
634 struct nbd_device *nbd = args->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400635 struct nbd_config *config = nbd->config;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700636 struct nbd_cmd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Markus Pargmann19391832015-08-17 08:20:03 +0200638 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500639 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700640 if (IS_ERR(cmd)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -0400641 struct nbd_sock *nsock = config->socks[args->index];
Josef Bacikf3733242017-04-06 17:01:57 -0400642
643 mutex_lock(&nsock->tx_lock);
Josef Bacik799f9a32017-04-06 17:02:02 -0400644 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400645 mutex_unlock(&nsock->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200646 break;
647 }
648
Christoph Hellwig08e00292017-04-20 16:03:09 +0200649 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd));
Markus Pargmann19391832015-08-17 08:20:03 +0200650 }
Josef Bacik5ea8d102017-04-06 17:01:58 -0400651 atomic_dec(&config->recv_threads);
652 wake_up(&config->recv_wq);
653 nbd_config_put(nbd);
654 kfree(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Josef Bacikfd8383f2016-09-08 12:33:37 -0700657static void nbd_clear_req(struct request *req, void *data, bool reserved)
658{
659 struct nbd_cmd *cmd;
660
661 if (!blk_mq_request_started(req))
662 return;
663 cmd = blk_mq_rq_to_pdu(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200664 cmd->status = BLK_STS_IOERR;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200665 blk_mq_complete_request(req);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700666}
667
Wanlong Gaof4507162012-03-28 14:42:51 -0700668static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300670 blk_mq_quiesce_queue(nbd->disk->queue);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700671 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Sagi Grimbergb52c2e92017-07-04 09:57:09 +0300672 blk_mq_unquiesce_queue(nbd->disk->queue);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200673 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Josef Bacikf3733242017-04-06 17:01:57 -0400676static int find_fallback(struct nbd_device *nbd, int index)
677{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400678 struct nbd_config *config = nbd->config;
Josef Bacikf3733242017-04-06 17:01:57 -0400679 int new_index = -1;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400680 struct nbd_sock *nsock = config->socks[index];
Josef Bacikf3733242017-04-06 17:01:57 -0400681 int fallback = nsock->fallback_index;
682
Josef Bacik5ea8d102017-04-06 17:01:58 -0400683 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
Josef Bacikf3733242017-04-06 17:01:57 -0400684 return new_index;
685
Josef Bacik5ea8d102017-04-06 17:01:58 -0400686 if (config->num_connections <= 1) {
Josef Bacikf3733242017-04-06 17:01:57 -0400687 dev_err_ratelimited(disk_to_dev(nbd->disk),
688 "Attempted send on invalid socket\n");
689 return new_index;
690 }
691
Josef Bacik5ea8d102017-04-06 17:01:58 -0400692 if (fallback >= 0 && fallback < config->num_connections &&
693 !config->socks[fallback]->dead)
Josef Bacikf3733242017-04-06 17:01:57 -0400694 return fallback;
695
696 if (nsock->fallback_index < 0 ||
Josef Bacik5ea8d102017-04-06 17:01:58 -0400697 nsock->fallback_index >= config->num_connections ||
698 config->socks[nsock->fallback_index]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400699 int i;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400700 for (i = 0; i < config->num_connections; i++) {
Josef Bacikf3733242017-04-06 17:01:57 -0400701 if (i == index)
702 continue;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400703 if (!config->socks[i]->dead) {
Josef Bacikf3733242017-04-06 17:01:57 -0400704 new_index = i;
705 break;
706 }
707 }
708 nsock->fallback_index = new_index;
709 if (new_index < 0) {
710 dev_err_ratelimited(disk_to_dev(nbd->disk),
711 "Dead connection, failed to find a fallback\n");
712 return new_index;
713 }
714 }
715 new_index = nsock->fallback_index;
716 return new_index;
717}
Paul Clements7fdfd402007-10-16 23:27:37 -0700718
Josef Bacik560bc4b2017-04-06 17:02:04 -0400719static int wait_for_reconnect(struct nbd_device *nbd)
720{
721 struct nbd_config *config = nbd->config;
722 if (!config->dead_conn_timeout)
723 return 0;
724 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
725 return 0;
726 wait_event_interruptible_timeout(config->conn_wait,
727 atomic_read(&config->live_connections),
728 config->dead_conn_timeout);
729 return atomic_read(&config->live_connections);
730}
731
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400732static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700733{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700734 struct request *req = blk_mq_rq_from_pdu(cmd);
735 struct nbd_device *nbd = cmd->nbd;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400736 struct nbd_config *config;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500737 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400738 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700739
Josef Bacik5ea8d102017-04-06 17:01:58 -0400740 if (!refcount_inc_not_zero(&nbd->config_refs)) {
741 dev_err_ratelimited(disk_to_dev(nbd->disk),
742 "Socks array is empty\n");
743 return -EINVAL;
744 }
745 config = nbd->config;
746
747 if (index >= config->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500748 dev_err_ratelimited(disk_to_dev(nbd->disk),
749 "Attempted send on invalid socket\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -0400750 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400751 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500752 }
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200753 cmd->status = BLK_STS_OK;
Josef Bacikf3733242017-04-06 17:01:57 -0400754again:
Josef Bacik5ea8d102017-04-06 17:01:58 -0400755 nsock = config->socks[index];
Josef Bacik9561a7a2016-11-22 14:04:40 -0500756 mutex_lock(&nsock->tx_lock);
Josef Bacikf3733242017-04-06 17:01:57 -0400757 if (nsock->dead) {
Josef Bacik560bc4b2017-04-06 17:02:04 -0400758 int old_index = index;
Josef Bacikf3733242017-04-06 17:01:57 -0400759 index = find_fallback(nbd, index);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500760 mutex_unlock(&nsock->tx_lock);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400761 if (index < 0) {
762 if (wait_for_reconnect(nbd)) {
763 index = old_index;
764 goto again;
765 }
766 /* All the sockets should already be down at this point,
767 * we just want to make sure that DISCONNECTED is set so
768 * any requests that come in that were queue'ed waiting
769 * for the reconnect timer don't trigger the timer again
770 * and instead just error out.
771 */
772 sock_shutdown(nbd);
773 nbd_config_put(nbd);
774 return -EIO;
775 }
Josef Bacikf3733242017-04-06 17:01:57 -0400776 goto again;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700777 }
778
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400779 /* Handle the case that we have a pending request that was partially
780 * transmitted that _has_ to be serviced first. We need to call requeue
781 * here so that it gets put _after_ the request that is already on the
782 * dispatch list.
783 */
784 if (unlikely(nsock->pending && nsock->pending != req)) {
785 blk_mq_requeue_request(req, true);
786 ret = 0;
787 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700788 }
Josef Bacikf3733242017-04-06 17:01:57 -0400789 /*
790 * Some failures are related to the link going down, so anything that
791 * returns EAGAIN can be retried on a different socket.
792 */
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400793 ret = nbd_send_cmd(nbd, cmd, index);
Josef Bacikf3733242017-04-06 17:01:57 -0400794 if (ret == -EAGAIN) {
795 dev_err_ratelimited(disk_to_dev(nbd->disk),
796 "Request send failed trying another connection\n");
Josef Bacik799f9a32017-04-06 17:02:02 -0400797 nbd_mark_nsock_dead(nbd, nsock, 1);
Josef Bacikf3733242017-04-06 17:01:57 -0400798 mutex_unlock(&nsock->tx_lock);
799 goto again;
800 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400801out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500802 mutex_unlock(&nsock->tx_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400803 nbd_config_put(nbd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400804 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700805}
806
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200807static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Josef Bacikfd8383f2016-09-08 12:33:37 -0700808 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700809{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700810 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400811 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700812
Josef Bacik9561a7a2016-11-22 14:04:40 -0500813 /*
814 * Since we look at the bio's to send the request over the network we
815 * need to make sure the completion work doesn't mark this request done
816 * before we are done doing our send. This keeps us from dereferencing
817 * freed data if we have particularly fast completions (ie we get the
818 * completion before we exit sock_xmit on the last bvec) or in the case
819 * that the server is misbehaving (or there was an error) before we're
820 * done sending everything over the wire.
821 */
822 init_completion(&cmd->send_complete);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700823 blk_mq_start_request(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400824
825 /* We can be called directly from the user space process, which means we
826 * could possibly have signals pending so our sendmsg will fail. In
827 * this case we need to return that we are busy, otherwise error out as
828 * appropriate.
829 */
830 ret = nbd_handle_cmd(cmd, hctx->queue_num);
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400831 if (ret < 0)
832 ret = BLK_STS_IOERR;
833 else if (!ret)
834 ret = BLK_STS_OK;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500835 complete(&cmd->send_complete);
836
Josef Bacik6e60a3b2017-10-02 16:22:08 -0400837 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Josef Bacike46c7282017-04-06 17:02:00 -0400840static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
841 bool netlink)
Markus Pargmann23272a672015-10-29 11:51:16 +0100842{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400843 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -0500844 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500845 struct nbd_sock **socks;
846 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -0500847 int err;
848
849 sock = sockfd_lookup(arg, &err);
850 if (!sock)
851 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100852
Josef Bacike46c7282017-04-06 17:02:00 -0400853 if (!netlink && !nbd->task_setup &&
854 !test_bit(NBD_BOUND, &config->runtime_flags))
Josef Bacik9561a7a2016-11-22 14:04:40 -0500855 nbd->task_setup = current;
Josef Bacike46c7282017-04-06 17:02:00 -0400856
857 if (!netlink &&
858 (nbd->task_setup != current ||
859 test_bit(NBD_BOUND, &config->runtime_flags))) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500860 dev_err(disk_to_dev(nbd->disk),
861 "Device being setup by another task");
Josef Bacik9b1355d2017-04-06 17:01:56 -0400862 sockfd_put(sock);
Josef Bacike46c7282017-04-06 17:02:00 -0400863 return -EBUSY;
Markus Pargmann23272a672015-10-29 11:51:16 +0100864 }
865
Josef Bacik5ea8d102017-04-06 17:01:58 -0400866 socks = krealloc(config->socks, (config->num_connections + 1) *
Josef Bacik9561a7a2016-11-22 14:04:40 -0500867 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400868 if (!socks) {
869 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500870 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400871 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500872 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400873 if (!nsock) {
874 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500875 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400876 }
Markus Pargmann23272a672015-10-29 11:51:16 +0100877
Josef Bacik5ea8d102017-04-06 17:01:58 -0400878 config->socks = socks;
Markus Pargmann23272a672015-10-29 11:51:16 +0100879
Josef Bacikf3733242017-04-06 17:01:57 -0400880 nsock->fallback_index = -1;
881 nsock->dead = false;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500882 mutex_init(&nsock->tx_lock);
883 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400884 nsock->pending = NULL;
885 nsock->sent = 0;
Josef Bacik799f9a32017-04-06 17:02:02 -0400886 nsock->cookie = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -0400887 socks[config->num_connections++] = nsock;
Josef Bacik560bc4b2017-04-06 17:02:04 -0400888 atomic_inc(&config->live_connections);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500889
890 return 0;
Markus Pargmann23272a672015-10-29 11:51:16 +0100891}
892
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400893static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
894{
895 struct nbd_config *config = nbd->config;
896 struct socket *sock, *old;
897 struct recv_thread_args *args;
898 int i;
899 int err;
900
901 sock = sockfd_lookup(arg, &err);
902 if (!sock)
903 return err;
904
905 args = kzalloc(sizeof(*args), GFP_KERNEL);
906 if (!args) {
907 sockfd_put(sock);
908 return -ENOMEM;
909 }
910
911 for (i = 0; i < config->num_connections; i++) {
912 struct nbd_sock *nsock = config->socks[i];
913
914 if (!nsock->dead)
915 continue;
916
917 mutex_lock(&nsock->tx_lock);
918 if (!nsock->dead) {
919 mutex_unlock(&nsock->tx_lock);
920 continue;
921 }
922 sk_set_memalloc(sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -0400923 if (nbd->tag_set.timeout)
924 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400925 atomic_inc(&config->recv_threads);
926 refcount_inc(&nbd->config_refs);
927 old = nsock->sock;
928 nsock->fallback_index = -1;
929 nsock->sock = sock;
930 nsock->dead = false;
931 INIT_WORK(&args->work, recv_work);
932 args->index = i;
933 args->nbd = nbd;
Josef Bacik799f9a32017-04-06 17:02:02 -0400934 nsock->cookie++;
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400935 mutex_unlock(&nsock->tx_lock);
936 sockfd_put(old);
937
Josef Bacik7a362ea2017-07-25 13:31:19 -0400938 clear_bit(NBD_DISCONNECTED, &config->runtime_flags);
939
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400940 /* We take the tx_mutex in an error path in the recv_work, so we
941 * need to queue_work outside of the tx_mutex.
942 */
943 queue_work(recv_workqueue, &args->work);
Josef Bacik560bc4b2017-04-06 17:02:04 -0400944
945 atomic_inc(&config->live_connections);
946 wake_up(&config->conn_wait);
Josef Bacikb7aa3d32017-04-06 17:02:01 -0400947 return 0;
948 }
949 sockfd_put(sock);
950 kfree(args);
951 return -ENOSPC;
952}
953
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100954static void nbd_bdev_reset(struct block_device *bdev)
955{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -0400956 if (bdev->bd_openers > 1)
957 return;
Josef Bacik29eaadc2017-04-06 17:01:59 -0400958 bd_set_size(bdev, 0);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100959 if (max_part > 0) {
960 blkdev_reread_part(bdev);
961 bdev->bd_invalidated = 1;
962 }
963}
964
Josef Bacik29eaadc2017-04-06 17:01:59 -0400965static void nbd_parse_flags(struct nbd_device *nbd)
Markus Pargmannd02cf532015-10-29 12:06:15 +0100966{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400967 struct nbd_config *config = nbd->config;
968 if (config->flags & NBD_FLAG_READ_ONLY)
Josef Bacik29eaadc2017-04-06 17:01:59 -0400969 set_disk_ro(nbd->disk, true);
970 else
971 set_disk_ro(nbd->disk, false);
Josef Bacik5ea8d102017-04-06 17:01:58 -0400972 if (config->flags & NBD_FLAG_SEND_TRIM)
Markus Pargmannd02cf532015-10-29 12:06:15 +0100973 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Shaun McDowell685c9b22017-05-25 23:55:54 -0400974 if (config->flags & NBD_FLAG_SEND_FLUSH) {
975 if (config->flags & NBD_FLAG_SEND_FUA)
976 blk_queue_write_cache(nbd->disk->queue, true, true);
977 else
978 blk_queue_write_cache(nbd->disk->queue, true, false);
979 }
Markus Pargmannd02cf532015-10-29 12:06:15 +0100980 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600981 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100982}
983
Josef Bacik9561a7a2016-11-22 14:04:40 -0500984static void send_disconnects(struct nbd_device *nbd)
985{
Josef Bacik5ea8d102017-04-06 17:01:58 -0400986 struct nbd_config *config = nbd->config;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500987 struct nbd_request request = {
988 .magic = htonl(NBD_REQUEST_MAGIC),
989 .type = htonl(NBD_CMD_DISC),
990 };
991 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
992 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500993 int i, ret;
994
Josef Bacik5ea8d102017-04-06 17:01:58 -0400995 for (i = 0; i < config->num_connections; i++) {
Josef Bacikb4b2aec2017-07-21 10:48:14 -0400996 struct nbd_sock *nsock = config->socks[i];
997
Al Viroc9f2b6a2015-11-12 05:09:35 -0500998 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
Josef Bacikb4b2aec2017-07-21 10:48:14 -0400999 mutex_lock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -04001000 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001001 if (ret <= 0)
1002 dev_err(disk_to_dev(nbd->disk),
1003 "Send disconnect failed %d\n", ret);
Josef Bacikb4b2aec2017-07-21 10:48:14 -04001004 mutex_unlock(&nsock->tx_lock);
Josef Bacik9561a7a2016-11-22 14:04:40 -05001005 }
1006}
1007
Josef Bacik29eaadc2017-04-06 17:01:59 -04001008static int nbd_disconnect(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001009{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001010 struct nbd_config *config = nbd->config;
Josef Bacik9442b732017-02-07 17:10:22 -05001011
Josef Bacik5ea8d102017-04-06 17:01:58 -04001012 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Josef Bacik2e134562017-07-21 10:48:13 -04001013 set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
1014 send_disconnects(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001015 return 0;
1016}
1017
Josef Bacik29eaadc2017-04-06 17:01:59 -04001018static void nbd_clear_sock(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001019{
1020 sock_shutdown(nbd);
1021 nbd_clear_que(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001022 nbd->task_setup = NULL;
Josef Bacik9442b732017-02-07 17:10:22 -05001023}
1024
Josef Bacik5ea8d102017-04-06 17:01:58 -04001025static void nbd_config_put(struct nbd_device *nbd)
1026{
1027 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1028 &nbd->config_lock)) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001029 struct nbd_config *config = nbd->config;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001030 nbd_dev_dbg_close(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001031 nbd_size_clear(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001032 if (test_and_clear_bit(NBD_HAS_PID_FILE,
1033 &config->runtime_flags))
1034 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1035 nbd->task_recv = NULL;
Josef Bacik29eaadc2017-04-06 17:01:59 -04001036 nbd_clear_sock(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001037 if (config->num_connections) {
1038 int i;
1039 for (i = 0; i < config->num_connections; i++) {
1040 sockfd_put(config->socks[i]->sock);
1041 kfree(config->socks[i]);
1042 }
1043 kfree(config->socks);
1044 }
Ilya Dryomovfa976532017-05-23 17:49:55 +02001045 kfree(nbd->config);
Ilya Dryomovaf622b82017-05-23 17:49:54 +02001046 nbd->config = NULL;
1047
1048 nbd->tag_set.timeout = 0;
1049 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Josef Bacika2c97902017-04-06 17:02:07 -04001050
Josef Bacik5ea8d102017-04-06 17:01:58 -04001051 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001052 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001053 module_put(THIS_MODULE);
1054 }
1055}
1056
Josef Bacike46c7282017-04-06 17:02:00 -04001057static int nbd_start_device(struct nbd_device *nbd)
Josef Bacik9442b732017-02-07 17:10:22 -05001058{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001059 struct nbd_config *config = nbd->config;
1060 int num_connections = config->num_connections;
Josef Bacik9442b732017-02-07 17:10:22 -05001061 int error = 0, i;
1062
1063 if (nbd->task_recv)
1064 return -EBUSY;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001065 if (!config->socks)
Josef Bacik9442b732017-02-07 17:10:22 -05001066 return -EINVAL;
1067 if (num_connections > 1 &&
Josef Bacik5ea8d102017-04-06 17:01:58 -04001068 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
Josef Bacik9442b732017-02-07 17:10:22 -05001069 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001070 return -EINVAL;
Josef Bacik9442b732017-02-07 17:10:22 -05001071 }
1072
Josef Bacik5ea8d102017-04-06 17:01:58 -04001073 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
Josef Bacik9442b732017-02-07 17:10:22 -05001074 nbd->task_recv = current;
Josef Bacik9442b732017-02-07 17:10:22 -05001075
Josef Bacik29eaadc2017-04-06 17:01:59 -04001076 nbd_parse_flags(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001077
1078 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1079 if (error) {
1080 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Josef Bacik5ea8d102017-04-06 17:01:58 -04001081 return error;
Josef Bacik9442b732017-02-07 17:10:22 -05001082 }
Josef Bacik29eaadc2017-04-06 17:01:59 -04001083 set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
Josef Bacik9442b732017-02-07 17:10:22 -05001084
1085 nbd_dev_dbg_init(nbd);
1086 for (i = 0; i < num_connections; i++) {
Josef Bacik5ea8d102017-04-06 17:01:58 -04001087 struct recv_thread_args *args;
1088
1089 args = kzalloc(sizeof(*args), GFP_KERNEL);
1090 if (!args) {
1091 sock_shutdown(nbd);
1092 return -ENOMEM;
1093 }
1094 sk_set_memalloc(config->socks[i]->sock->sk);
Josef Bacika7ee8cf2017-07-21 10:48:15 -04001095 if (nbd->tag_set.timeout)
1096 config->socks[i]->sock->sk->sk_sndtimeo =
1097 nbd->tag_set.timeout;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001098 atomic_inc(&config->recv_threads);
1099 refcount_inc(&nbd->config_refs);
1100 INIT_WORK(&args->work, recv_work);
1101 args->nbd = nbd;
1102 args->index = i;
1103 queue_work(recv_workqueue, &args->work);
Josef Bacik9442b732017-02-07 17:10:22 -05001104 }
Josef Bacik639812a2017-10-09 13:12:10 -04001105 nbd_size_update(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001106 return error;
1107}
1108
1109static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1110{
1111 struct nbd_config *config = nbd->config;
1112 int ret;
1113
1114 ret = nbd_start_device(nbd);
1115 if (ret)
1116 return ret;
1117
1118 bd_set_size(bdev, config->bytesize);
1119 if (max_part)
1120 bdev->bd_invalidated = 1;
1121 mutex_unlock(&nbd->config_lock);
1122 ret = wait_event_interruptible(config->recv_wq,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001123 atomic_read(&config->recv_threads) == 0);
Josef Bacike46c7282017-04-06 17:02:00 -04001124 if (ret)
Josef Bacik5ea8d102017-04-06 17:01:58 -04001125 sock_shutdown(nbd);
Josef Bacik9442b732017-02-07 17:10:22 -05001126 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001127 bd_set_size(bdev, 0);
Josef Bacik9442b732017-02-07 17:10:22 -05001128 /* user requested, ignore socket errors */
Josef Bacik5ea8d102017-04-06 17:01:58 -04001129 if (test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001130 ret = 0;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001131 if (test_bit(NBD_TIMEDOUT, &config->runtime_flags))
Josef Bacike46c7282017-04-06 17:02:00 -04001132 ret = -ETIMEDOUT;
1133 return ret;
Josef Bacik9442b732017-02-07 17:10:22 -05001134}
Markus Pargmann30d53d92015-08-17 08:20:06 +02001135
Josef Bacik29eaadc2017-04-06 17:01:59 -04001136static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1137 struct block_device *bdev)
1138{
Josef Bacik2516ab12017-04-06 17:02:03 -04001139 sock_shutdown(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001140 kill_bdev(bdev);
1141 nbd_bdev_reset(bdev);
Josef Bacike46c7282017-04-06 17:02:00 -04001142 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1143 &nbd->config->runtime_flags))
1144 nbd_config_put(nbd);
Josef Bacik29eaadc2017-04-06 17:01:59 -04001145}
1146
Josef Bacik9561a7a2016-11-22 14:04:40 -05001147/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -07001148static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -07001149 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001151 struct nbd_config *config = nbd->config;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -05001154 case NBD_DISCONNECT:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001155 return nbd_disconnect(nbd);
Markus Pargmann23272a672015-10-29 11:51:16 +01001156 case NBD_CLEAR_SOCK:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001157 nbd_clear_sock_ioctl(nbd, bdev);
1158 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001159 case NBD_SET_SOCK:
Josef Bacike46c7282017-04-06 17:02:00 -04001160 return nbd_add_socket(nbd, arg, false);
Josef Bacik9442b732017-02-07 17:10:22 -05001161 case NBD_SET_BLKSIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001162 nbd_size_set(nbd, arg,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001163 div_s64(config->bytesize, arg));
Josef Bacike5445412017-02-13 10:39:47 -05001164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 case NBD_SET_SIZE:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001166 nbd_size_set(nbd, config->blksize,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001167 div_s64(arg, config->blksize));
Josef Bacike5445412017-02-13 10:39:47 -05001168 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +02001169 case NBD_SET_SIZE_BLOCKS:
Josef Bacik29eaadc2017-04-06 17:01:59 -04001170 nbd_size_set(nbd, config->blksize, arg);
Josef Bacike5445412017-02-13 10:39:47 -05001171 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -07001172 case NBD_SET_TIMEOUT:
Josef Bacikf8586852017-03-24 14:08:28 -04001173 if (arg) {
1174 nbd->tag_set.timeout = arg * HZ;
1175 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
1176 }
Paul Clements7fdfd402007-10-16 23:27:37 -07001177 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001178
Paul Clements2f012502012-10-04 17:16:15 -07001179 case NBD_SET_FLAGS:
Josef Bacik5ea8d102017-04-06 17:01:58 -04001180 config->flags = arg;
Paul Clements2f012502012-10-04 17:16:15 -07001181 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -05001182 case NBD_DO_IT:
Josef Bacike46c7282017-04-06 17:02:00 -04001183 return nbd_start_device_ioctl(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -08001185 /*
1186 * This is for compatibility only. The queue is always cleared
1187 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -07001191 /*
1192 * For compatibility only, we no longer keep a list of
1193 * outstanding requests.
1194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return 0;
1196 }
Pavel Machek1a2ad212009-04-02 16:58:41 -07001197 return -ENOTTY;
1198}
1199
1200static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1201 unsigned int cmd, unsigned long arg)
1202{
Wanlong Gaof4507162012-03-28 14:42:51 -07001203 struct nbd_device *nbd = bdev->bd_disk->private_data;
Josef Bacike46c7282017-04-06 17:02:00 -04001204 struct nbd_config *config = nbd->config;
1205 int error = -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -07001206
1207 if (!capable(CAP_SYS_ADMIN))
1208 return -EPERM;
1209
Josef Bacik1dae69b2017-05-05 22:25:18 -04001210 /* The block layer will pass back some non-nbd ioctls in case we have
1211 * special handling for them, but we don't so just return an error.
1212 */
1213 if (_IOC_TYPE(cmd) != 0xab)
1214 return -EINVAL;
1215
Josef Bacik9561a7a2016-11-22 14:04:40 -05001216 mutex_lock(&nbd->config_lock);
Josef Bacike46c7282017-04-06 17:02:00 -04001217
1218 /* Don't allow ioctl operations on a nbd device that was created with
1219 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1220 */
1221 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1222 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1223 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1224 else
1225 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -05001226 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -07001227 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Josef Bacik5ea8d102017-04-06 17:01:58 -04001230static struct nbd_config *nbd_alloc_config(void)
1231{
1232 struct nbd_config *config;
1233
1234 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1235 if (!config)
1236 return NULL;
1237 atomic_set(&config->recv_threads, 0);
1238 init_waitqueue_head(&config->recv_wq);
Josef Bacik560bc4b2017-04-06 17:02:04 -04001239 init_waitqueue_head(&config->conn_wait);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001240 config->blksize = 1024;
Josef Bacik560bc4b2017-04-06 17:02:04 -04001241 atomic_set(&config->live_connections, 0);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001242 try_module_get(THIS_MODULE);
1243 return config;
1244}
1245
1246static int nbd_open(struct block_device *bdev, fmode_t mode)
1247{
1248 struct nbd_device *nbd;
1249 int ret = 0;
1250
1251 mutex_lock(&nbd_index_mutex);
1252 nbd = bdev->bd_disk->private_data;
1253 if (!nbd) {
1254 ret = -ENXIO;
1255 goto out;
1256 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001257 if (!refcount_inc_not_zero(&nbd->refs)) {
1258 ret = -ENXIO;
1259 goto out;
1260 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001261 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1262 struct nbd_config *config;
1263
1264 mutex_lock(&nbd->config_lock);
1265 if (refcount_inc_not_zero(&nbd->config_refs)) {
1266 mutex_unlock(&nbd->config_lock);
1267 goto out;
1268 }
1269 config = nbd->config = nbd_alloc_config();
1270 if (!config) {
1271 ret = -ENOMEM;
1272 mutex_unlock(&nbd->config_lock);
1273 goto out;
1274 }
1275 refcount_set(&nbd->config_refs, 1);
Josef Bacikc6a47592017-04-06 17:02:06 -04001276 refcount_inc(&nbd->refs);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001277 mutex_unlock(&nbd->config_lock);
1278 }
1279out:
1280 mutex_unlock(&nbd_index_mutex);
1281 return ret;
1282}
1283
1284static void nbd_release(struct gendisk *disk, fmode_t mode)
1285{
1286 struct nbd_device *nbd = disk->private_data;
1287 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001288 nbd_put(nbd);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001289}
1290
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001291static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 .owner = THIS_MODULE,
Josef Bacik5ea8d102017-04-06 17:01:58 -04001294 .open = nbd_open,
1295 .release = nbd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001296 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -05001297 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298};
1299
Markus Pargmann30d53d92015-08-17 08:20:06 +02001300#if IS_ENABLED(CONFIG_DEBUG_FS)
1301
1302static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1303{
1304 struct nbd_device *nbd = s->private;
1305
1306 if (nbd->task_recv)
1307 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +02001308
1309 return 0;
1310}
1311
1312static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1313{
1314 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1315}
1316
1317static const struct file_operations nbd_dbg_tasks_ops = {
1318 .open = nbd_dbg_tasks_open,
1319 .read = seq_read,
1320 .llseek = seq_lseek,
1321 .release = single_release,
1322};
1323
1324static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1325{
1326 struct nbd_device *nbd = s->private;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001327 u32 flags = nbd->config->flags;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001328
1329 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1330
1331 seq_puts(s, "Known flags:\n");
1332
1333 if (flags & NBD_FLAG_HAS_FLAGS)
1334 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1335 if (flags & NBD_FLAG_READ_ONLY)
1336 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1337 if (flags & NBD_FLAG_SEND_FLUSH)
1338 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
Shaun McDowell685c9b22017-05-25 23:55:54 -04001339 if (flags & NBD_FLAG_SEND_FUA)
1340 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
Markus Pargmann30d53d92015-08-17 08:20:06 +02001341 if (flags & NBD_FLAG_SEND_TRIM)
1342 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1343
1344 return 0;
1345}
1346
1347static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1348{
1349 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1350}
1351
1352static const struct file_operations nbd_dbg_flags_ops = {
1353 .open = nbd_dbg_flags_open,
1354 .read = seq_read,
1355 .llseek = seq_lseek,
1356 .release = single_release,
1357};
1358
1359static int nbd_dev_dbg_init(struct nbd_device *nbd)
1360{
1361 struct dentry *dir;
Josef Bacik5ea8d102017-04-06 17:01:58 -04001362 struct nbd_config *config = nbd->config;
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001363
1364 if (!nbd_dbg_dir)
1365 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001366
1367 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001368 if (!dir) {
1369 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1370 nbd_name(nbd));
1371 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001372 }
Josef Bacik5ea8d102017-04-06 17:01:58 -04001373 config->dbg_dir = dir;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001374
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001375 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001376 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -07001377 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001378 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -04001379 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001380
1381 return 0;
1382}
1383
1384static void nbd_dev_dbg_close(struct nbd_device *nbd)
1385{
Josef Bacik5ea8d102017-04-06 17:01:58 -04001386 debugfs_remove_recursive(nbd->config->dbg_dir);
Markus Pargmann30d53d92015-08-17 08:20:06 +02001387}
1388
1389static int nbd_dbg_init(void)
1390{
1391 struct dentry *dbg_dir;
1392
1393 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +02001394 if (!dbg_dir)
1395 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001396
1397 nbd_dbg_dir = dbg_dir;
1398
1399 return 0;
1400}
1401
1402static void nbd_dbg_close(void)
1403{
1404 debugfs_remove_recursive(nbd_dbg_dir);
1405}
1406
1407#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1408
1409static int nbd_dev_dbg_init(struct nbd_device *nbd)
1410{
1411 return 0;
1412}
1413
1414static void nbd_dev_dbg_close(struct nbd_device *nbd)
1415{
1416}
1417
1418static int nbd_dbg_init(void)
1419{
1420 return 0;
1421}
1422
1423static void nbd_dbg_close(void)
1424{
1425}
1426
1427#endif
1428
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001429static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1430 unsigned int hctx_idx, unsigned int numa_node)
Josef Bacikfd8383f2016-09-08 12:33:37 -07001431{
1432 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001433 cmd->nbd = set->driver_data;
Josef Bacikfd8383f2016-09-08 12:33:37 -07001434 return 0;
1435}
1436
Eric Biggersf363b082017-03-30 13:39:16 -07001437static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001438 .queue_rq = nbd_queue_rq,
Christoph Hellwig1e388ae2017-04-20 16:03:06 +02001439 .complete = nbd_complete_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001440 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001441 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001442};
1443
Josef Bacikb0d91112017-02-01 16:11:40 -05001444static int nbd_dev_add(int index)
1445{
1446 struct nbd_device *nbd;
1447 struct gendisk *disk;
1448 struct request_queue *q;
1449 int err = -ENOMEM;
1450
1451 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1452 if (!nbd)
1453 goto out;
1454
1455 disk = alloc_disk(1 << part_shift);
1456 if (!disk)
1457 goto out_free_nbd;
1458
1459 if (index >= 0) {
1460 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1461 GFP_KERNEL);
1462 if (err == -ENOSPC)
1463 err = -EEXIST;
1464 } else {
1465 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1466 if (err >= 0)
1467 index = err;
1468 }
1469 if (err < 0)
1470 goto out_free_disk;
1471
Josef Bacike46c7282017-04-06 17:02:00 -04001472 nbd->index = index;
Josef Bacikb0d91112017-02-01 16:11:40 -05001473 nbd->disk = disk;
1474 nbd->tag_set.ops = &nbd_mq_ops;
1475 nbd->tag_set.nr_hw_queues = 1;
1476 nbd->tag_set.queue_depth = 128;
1477 nbd->tag_set.numa_node = NUMA_NO_NODE;
1478 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1479 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1480 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
1481 nbd->tag_set.driver_data = nbd;
1482
1483 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1484 if (err)
1485 goto out_free_idr;
1486
1487 q = blk_mq_init_queue(&nbd->tag_set);
1488 if (IS_ERR(q)) {
1489 err = PTR_ERR(q);
1490 goto out_free_tags;
1491 }
1492 disk->queue = q;
1493
1494 /*
1495 * Tell the block layer that we are not a rotational device
1496 */
1497 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
1498 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1499 disk->queue->limits.discard_granularity = 512;
1500 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Josef Bacikebb16d02017-04-18 16:22:51 -04001501 blk_queue_max_segment_size(disk->queue, UINT_MAX);
Josef Bacik1cc1f172017-04-20 15:47:01 -04001502 blk_queue_max_segments(disk->queue, USHRT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001503 blk_queue_max_hw_sectors(disk->queue, 65536);
1504 disk->queue->limits.max_sectors = 256;
1505
Josef Bacikb0d91112017-02-01 16:11:40 -05001506 mutex_init(&nbd->config_lock);
Josef Bacik5ea8d102017-04-06 17:01:58 -04001507 refcount_set(&nbd->config_refs, 0);
Josef Bacikc6a47592017-04-06 17:02:06 -04001508 refcount_set(&nbd->refs, 1);
1509 INIT_LIST_HEAD(&nbd->list);
Josef Bacikb0d91112017-02-01 16:11:40 -05001510 disk->major = NBD_MAJOR;
1511 disk->first_minor = index << part_shift;
1512 disk->fops = &nbd_fops;
1513 disk->private_data = nbd;
1514 sprintf(disk->disk_name, "nbd%d", index);
Josef Bacikb0d91112017-02-01 16:11:40 -05001515 add_disk(disk);
Josef Bacik47d902b2017-04-06 17:02:05 -04001516 nbd_total_devices++;
Josef Bacikb0d91112017-02-01 16:11:40 -05001517 return index;
1518
1519out_free_tags:
1520 blk_mq_free_tag_set(&nbd->tag_set);
1521out_free_idr:
1522 idr_remove(&nbd_index_idr, index);
1523out_free_disk:
1524 put_disk(disk);
1525out_free_nbd:
1526 kfree(nbd);
1527out:
1528 return err;
1529}
1530
Josef Bacike46c7282017-04-06 17:02:00 -04001531static int find_free_cb(int id, void *ptr, void *data)
1532{
1533 struct nbd_device *nbd = ptr;
1534 struct nbd_device **found = data;
1535
1536 if (!refcount_read(&nbd->config_refs)) {
1537 *found = nbd;
1538 return 1;
1539 }
1540 return 0;
1541}
1542
1543/* Netlink interface. */
1544static struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1545 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1546 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1547 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1548 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1549 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1550 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1551 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
Josef Bacik560bc4b2017-04-06 17:02:04 -04001552 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001553 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
Josef Bacike46c7282017-04-06 17:02:00 -04001554};
1555
1556static struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1557 [NBD_SOCK_FD] = { .type = NLA_U32 },
1558};
1559
Josef Bacik47d902b2017-04-06 17:02:05 -04001560/* We don't use this right now since we don't parse the incoming list, but we
1561 * still want it here so userspace knows what to expect.
1562 */
1563static struct nla_policy __attribute__((unused))
1564nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1565 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1566 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1567};
1568
Josef Bacike46c7282017-04-06 17:02:00 -04001569static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1570{
1571 struct nbd_device *nbd = NULL;
1572 struct nbd_config *config;
1573 int index = -1;
1574 int ret;
Josef Bacika2c97902017-04-06 17:02:07 -04001575 bool put_dev = false;
Josef Bacike46c7282017-04-06 17:02:00 -04001576
1577 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1578 return -EPERM;
1579
1580 if (info->attrs[NBD_ATTR_INDEX])
1581 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1582 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1583 printk(KERN_ERR "nbd: must specify at least one socket\n");
1584 return -EINVAL;
1585 }
1586 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1587 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1588 return -EINVAL;
1589 }
1590again:
1591 mutex_lock(&nbd_index_mutex);
1592 if (index == -1) {
1593 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1594 if (ret == 0) {
1595 int new_index;
1596 new_index = nbd_dev_add(-1);
1597 if (new_index < 0) {
1598 mutex_unlock(&nbd_index_mutex);
1599 printk(KERN_ERR "nbd: failed to add new device\n");
1600 return ret;
1601 }
1602 nbd = idr_find(&nbd_index_idr, new_index);
1603 }
1604 } else {
1605 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike6a76272017-08-14 18:25:33 +00001606 if (!nbd) {
1607 ret = nbd_dev_add(index);
1608 if (ret < 0) {
1609 mutex_unlock(&nbd_index_mutex);
1610 printk(KERN_ERR "nbd: failed to add new device\n");
1611 return ret;
1612 }
1613 nbd = idr_find(&nbd_index_idr, index);
1614 }
Josef Bacike46c7282017-04-06 17:02:00 -04001615 }
Josef Bacike46c7282017-04-06 17:02:00 -04001616 if (!nbd) {
1617 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1618 index);
Josef Bacikc6a47592017-04-06 17:02:06 -04001619 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001620 return -EINVAL;
1621 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001622 if (!refcount_inc_not_zero(&nbd->refs)) {
1623 mutex_unlock(&nbd_index_mutex);
1624 if (index == -1)
1625 goto again;
1626 printk(KERN_ERR "nbd: device at index %d is going down\n",
1627 index);
1628 return -EINVAL;
1629 }
1630 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001631
1632 mutex_lock(&nbd->config_lock);
1633 if (refcount_read(&nbd->config_refs)) {
1634 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001635 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001636 if (index == -1)
1637 goto again;
1638 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1639 return -EBUSY;
1640 }
1641 if (WARN_ON(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 return -EINVAL;
1645 }
1646 config = nbd->config = nbd_alloc_config();
1647 if (!nbd->config) {
1648 mutex_unlock(&nbd->config_lock);
Josef Bacikc6a47592017-04-06 17:02:06 -04001649 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001650 printk(KERN_ERR "nbd: couldn't allocate config\n");
1651 return -ENOMEM;
1652 }
1653 refcount_set(&nbd->config_refs, 1);
1654 set_bit(NBD_BOUND, &config->runtime_flags);
1655
1656 if (info->attrs[NBD_ATTR_SIZE_BYTES]) {
1657 u64 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1658 nbd_size_set(nbd, config->blksize,
1659 div64_u64(bytes, config->blksize));
1660 }
1661 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1662 u64 bsize =
1663 nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1664 nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
1665 }
1666 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1667 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1668 nbd->tag_set.timeout = timeout * HZ;
1669 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1670 }
Josef Bacik560bc4b2017-04-06 17:02:04 -04001671 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1672 config->dead_conn_timeout =
1673 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1674 config->dead_conn_timeout *= HZ;
1675 }
Josef Bacike46c7282017-04-06 17:02:00 -04001676 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1677 config->flags =
1678 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
Josef Bacika2c97902017-04-06 17:02:07 -04001679 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1680 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1681 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1682 set_bit(NBD_DESTROY_ON_DISCONNECT,
1683 &config->runtime_flags);
1684 put_dev = true;
1685 }
1686 }
1687
Josef Bacike46c7282017-04-06 17:02:00 -04001688 if (info->attrs[NBD_ATTR_SOCKETS]) {
1689 struct nlattr *attr;
1690 int rem, fd;
1691
1692 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1693 rem) {
1694 struct nlattr *socks[NBD_SOCK_MAX+1];
1695
1696 if (nla_type(attr) != NBD_SOCK_ITEM) {
1697 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1698 ret = -EINVAL;
1699 goto out;
1700 }
1701 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
Linus Torvalds8d65b082017-05-02 16:40:27 -07001702 nbd_sock_policy, info->extack);
Josef Bacike46c7282017-04-06 17:02:00 -04001703 if (ret != 0) {
1704 printk(KERN_ERR "nbd: error processing sock list\n");
1705 ret = -EINVAL;
1706 goto out;
1707 }
1708 if (!socks[NBD_SOCK_FD])
1709 continue;
1710 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1711 ret = nbd_add_socket(nbd, fd, true);
1712 if (ret)
1713 goto out;
1714 }
1715 }
1716 ret = nbd_start_device(nbd);
1717out:
1718 mutex_unlock(&nbd->config_lock);
1719 if (!ret) {
1720 set_bit(NBD_HAS_CONFIG_REF, &config->runtime_flags);
1721 refcount_inc(&nbd->config_refs);
1722 nbd_connect_reply(info, nbd->index);
1723 }
1724 nbd_config_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04001725 if (put_dev)
1726 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001727 return ret;
1728}
1729
1730static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
1731{
1732 struct nbd_device *nbd;
1733 int index;
1734
1735 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1736 return -EPERM;
1737
1738 if (!info->attrs[NBD_ATTR_INDEX]) {
1739 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
1740 return -EINVAL;
1741 }
1742 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1743 mutex_lock(&nbd_index_mutex);
1744 nbd = idr_find(&nbd_index_idr, index);
Josef Bacike46c7282017-04-06 17:02:00 -04001745 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001746 mutex_unlock(&nbd_index_mutex);
Josef Bacike46c7282017-04-06 17:02:00 -04001747 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1748 index);
1749 return -EINVAL;
1750 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001751 if (!refcount_inc_not_zero(&nbd->refs)) {
1752 mutex_unlock(&nbd_index_mutex);
1753 printk(KERN_ERR "nbd: device at index %d is going down\n",
1754 index);
1755 return -EINVAL;
1756 }
1757 mutex_unlock(&nbd_index_mutex);
1758 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1759 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001760 return 0;
Josef Bacikc6a47592017-04-06 17:02:06 -04001761 }
Josef Bacike46c7282017-04-06 17:02:00 -04001762 mutex_lock(&nbd->config_lock);
1763 nbd_disconnect(nbd);
1764 mutex_unlock(&nbd->config_lock);
1765 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1766 &nbd->config->runtime_flags))
1767 nbd_config_put(nbd);
1768 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001769 nbd_put(nbd);
Josef Bacike46c7282017-04-06 17:02:00 -04001770 return 0;
1771}
1772
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001773static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
1774{
1775 struct nbd_device *nbd = NULL;
1776 struct nbd_config *config;
1777 int index;
1778 int ret = -EINVAL;
Josef Bacika2c97902017-04-06 17:02:07 -04001779 bool put_dev = false;
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001780
1781 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1782 return -EPERM;
1783
1784 if (!info->attrs[NBD_ATTR_INDEX]) {
1785 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
1786 return -EINVAL;
1787 }
1788 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1789 mutex_lock(&nbd_index_mutex);
1790 nbd = idr_find(&nbd_index_idr, index);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001791 if (!nbd) {
Josef Bacikc6a47592017-04-06 17:02:06 -04001792 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001793 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
1794 index);
1795 return -EINVAL;
1796 }
Josef Bacikc6a47592017-04-06 17:02:06 -04001797 if (!refcount_inc_not_zero(&nbd->refs)) {
1798 mutex_unlock(&nbd_index_mutex);
1799 printk(KERN_ERR "nbd: device at index %d is going down\n",
1800 index);
1801 return -EINVAL;
1802 }
1803 mutex_unlock(&nbd_index_mutex);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001804
1805 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1806 dev_err(nbd_to_dev(nbd),
1807 "not configured, cannot reconfigure\n");
Josef Bacikc6a47592017-04-06 17:02:06 -04001808 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001809 return -EINVAL;
1810 }
1811
1812 mutex_lock(&nbd->config_lock);
1813 config = nbd->config;
1814 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1815 !nbd->task_recv) {
1816 dev_err(nbd_to_dev(nbd),
1817 "not configured, cannot reconfigure\n");
1818 goto out;
1819 }
1820
1821 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1822 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1823 nbd->tag_set.timeout = timeout * HZ;
1824 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1825 }
Josef Bacik560bc4b2017-04-06 17:02:04 -04001826 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1827 config->dead_conn_timeout =
1828 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1829 config->dead_conn_timeout *= HZ;
1830 }
Josef Bacika2c97902017-04-06 17:02:07 -04001831 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1832 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1833 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1834 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1835 &config->runtime_flags))
1836 put_dev = true;
1837 } else {
1838 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1839 &config->runtime_flags))
1840 refcount_inc(&nbd->refs);
1841 }
1842 }
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001843
1844 if (info->attrs[NBD_ATTR_SOCKETS]) {
1845 struct nlattr *attr;
1846 int rem, fd;
1847
1848 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1849 rem) {
1850 struct nlattr *socks[NBD_SOCK_MAX+1];
1851
1852 if (nla_type(attr) != NBD_SOCK_ITEM) {
1853 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1854 ret = -EINVAL;
1855 goto out;
1856 }
1857 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
Linus Torvalds8d65b082017-05-02 16:40:27 -07001858 nbd_sock_policy, info->extack);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001859 if (ret != 0) {
1860 printk(KERN_ERR "nbd: error processing sock list\n");
1861 ret = -EINVAL;
1862 goto out;
1863 }
1864 if (!socks[NBD_SOCK_FD])
1865 continue;
1866 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1867 ret = nbd_reconnect_socket(nbd, fd);
1868 if (ret) {
1869 if (ret == -ENOSPC)
1870 ret = 0;
1871 goto out;
1872 }
1873 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
1874 }
1875 }
1876out:
1877 mutex_unlock(&nbd->config_lock);
1878 nbd_config_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04001879 nbd_put(nbd);
Josef Bacika2c97902017-04-06 17:02:07 -04001880 if (put_dev)
1881 nbd_put(nbd);
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001882 return ret;
1883}
1884
Josef Bacike46c7282017-04-06 17:02:00 -04001885static const struct genl_ops nbd_connect_genl_ops[] = {
1886 {
1887 .cmd = NBD_CMD_CONNECT,
1888 .policy = nbd_attr_policy,
1889 .doit = nbd_genl_connect,
1890 },
1891 {
1892 .cmd = NBD_CMD_DISCONNECT,
1893 .policy = nbd_attr_policy,
1894 .doit = nbd_genl_disconnect,
1895 },
Josef Bacikb7aa3d32017-04-06 17:02:01 -04001896 {
1897 .cmd = NBD_CMD_RECONFIGURE,
1898 .policy = nbd_attr_policy,
1899 .doit = nbd_genl_reconfigure,
1900 },
Josef Bacik47d902b2017-04-06 17:02:05 -04001901 {
1902 .cmd = NBD_CMD_STATUS,
1903 .policy = nbd_attr_policy,
1904 .doit = nbd_genl_status,
1905 },
Josef Bacike46c7282017-04-06 17:02:00 -04001906};
1907
Josef Bacik799f9a32017-04-06 17:02:02 -04001908static const struct genl_multicast_group nbd_mcast_grps[] = {
1909 { .name = NBD_GENL_MCAST_GROUP_NAME, },
1910};
1911
Josef Bacike46c7282017-04-06 17:02:00 -04001912static struct genl_family nbd_genl_family __ro_after_init = {
1913 .hdrsize = 0,
1914 .name = NBD_GENL_FAMILY_NAME,
1915 .version = NBD_GENL_VERSION,
1916 .module = THIS_MODULE,
1917 .ops = nbd_connect_genl_ops,
1918 .n_ops = ARRAY_SIZE(nbd_connect_genl_ops),
1919 .maxattr = NBD_ATTR_MAX,
Josef Bacik799f9a32017-04-06 17:02:02 -04001920 .mcgrps = nbd_mcast_grps,
1921 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
Josef Bacike46c7282017-04-06 17:02:00 -04001922};
1923
Josef Bacik47d902b2017-04-06 17:02:05 -04001924static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
1925{
1926 struct nlattr *dev_opt;
1927 u8 connected = 0;
1928 int ret;
1929
1930 /* This is a little racey, but for status it's ok. The
1931 * reason we don't take a ref here is because we can't
1932 * take a ref in the index == -1 case as we would need
1933 * to put under the nbd_index_mutex, which could
1934 * deadlock if we are configured to remove ourselves
1935 * once we're disconnected.
1936 */
1937 if (refcount_read(&nbd->config_refs))
1938 connected = 1;
1939 dev_opt = nla_nest_start(reply, NBD_DEVICE_ITEM);
1940 if (!dev_opt)
1941 return -EMSGSIZE;
1942 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
1943 if (ret)
1944 return -EMSGSIZE;
1945 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
1946 connected);
1947 if (ret)
1948 return -EMSGSIZE;
1949 nla_nest_end(reply, dev_opt);
1950 return 0;
1951}
1952
1953static int status_cb(int id, void *ptr, void *data)
1954{
1955 struct nbd_device *nbd = ptr;
1956 return populate_nbd_status(nbd, (struct sk_buff *)data);
1957}
1958
1959static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
1960{
1961 struct nlattr *dev_list;
1962 struct sk_buff *reply;
1963 void *reply_head;
1964 size_t msg_size;
1965 int index = -1;
1966 int ret = -ENOMEM;
1967
1968 if (info->attrs[NBD_ATTR_INDEX])
1969 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1970
1971 mutex_lock(&nbd_index_mutex);
1972
1973 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
1974 nla_attr_size(sizeof(u8)));
1975 msg_size *= (index == -1) ? nbd_total_devices : 1;
1976
1977 reply = genlmsg_new(msg_size, GFP_KERNEL);
1978 if (!reply)
1979 goto out;
1980 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
1981 NBD_CMD_STATUS);
1982 if (!reply_head) {
1983 nlmsg_free(reply);
1984 goto out;
1985 }
1986
1987 dev_list = nla_nest_start(reply, NBD_ATTR_DEVICE_LIST);
1988 if (index == -1) {
1989 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
1990 if (ret) {
1991 nlmsg_free(reply);
1992 goto out;
1993 }
1994 } else {
1995 struct nbd_device *nbd;
1996 nbd = idr_find(&nbd_index_idr, index);
1997 if (nbd) {
1998 ret = populate_nbd_status(nbd, reply);
1999 if (ret) {
2000 nlmsg_free(reply);
2001 goto out;
2002 }
2003 }
2004 }
2005 nla_nest_end(reply, dev_list);
2006 genlmsg_end(reply, reply_head);
2007 genlmsg_reply(reply, info);
2008 ret = 0;
2009out:
2010 mutex_unlock(&nbd_index_mutex);
2011 return ret;
2012}
2013
Josef Bacike46c7282017-04-06 17:02:00 -04002014static void nbd_connect_reply(struct genl_info *info, int index)
2015{
2016 struct sk_buff *skb;
2017 void *msg_head;
2018 int ret;
2019
2020 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2021 if (!skb)
2022 return;
2023 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2024 NBD_CMD_CONNECT);
2025 if (!msg_head) {
2026 nlmsg_free(skb);
2027 return;
2028 }
2029 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2030 if (ret) {
2031 nlmsg_free(skb);
2032 return;
2033 }
2034 genlmsg_end(skb, msg_head);
2035 genlmsg_reply(skb, info);
2036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Josef Bacik799f9a32017-04-06 17:02:02 -04002038static void nbd_mcast_index(int index)
2039{
2040 struct sk_buff *skb;
2041 void *msg_head;
2042 int ret;
2043
2044 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2045 if (!skb)
2046 return;
2047 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2048 NBD_CMD_LINK_DEAD);
2049 if (!msg_head) {
2050 nlmsg_free(skb);
2051 return;
2052 }
2053 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2054 if (ret) {
2055 nlmsg_free(skb);
2056 return;
2057 }
2058 genlmsg_end(skb, msg_head);
2059 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2060}
2061
2062static void nbd_dead_link_work(struct work_struct *work)
2063{
2064 struct link_dead_args *args = container_of(work, struct link_dead_args,
2065 work);
2066 nbd_mcast_index(args->index);
2067 kfree(args);
2068}
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070static int __init nbd_init(void)
2071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 int i;
2073
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08002074 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002076 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02002077 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002078 return -EINVAL;
2079 }
2080
2081 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02002082 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002083 part_shift = fls(max_part);
2084
Namhyung Kim5988ce22011-05-28 14:44:46 +02002085 /*
2086 * Adjust max_part according to part_shift as it is exported
2087 * to user space so that user can know the max number of
2088 * partition kernel should be able to manage.
2089 *
2090 * Note that -1 is required because partition 0 is reserved
2091 * for the whole disk.
2092 */
2093 max_part = (1UL << part_shift) - 1;
2094 }
2095
Namhyung Kim3b271082011-05-28 14:44:46 +02002096 if ((1UL << part_shift) > DISK_MAX_PARTS)
2097 return -EINVAL;
2098
2099 if (nbds_max > 1UL << (MINORBITS - part_shift))
2100 return -EINVAL;
Josef Bacik124d6db2017-02-01 16:11:11 -05002101 recv_workqueue = alloc_workqueue("knbd-recv",
2102 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2103 if (!recv_workqueue)
2104 return -ENOMEM;
Namhyung Kim3b271082011-05-28 14:44:46 +02002105
Josef Bacik6330a2d2017-02-15 16:49:48 -05002106 if (register_blkdev(NBD_MAJOR, "nbd")) {
2107 destroy_workqueue(recv_workqueue);
Josef Bacikb0d91112017-02-01 16:11:40 -05002108 return -EIO;
Josef Bacik6330a2d2017-02-15 16:49:48 -05002109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Josef Bacike46c7282017-04-06 17:02:00 -04002111 if (genl_register_family(&nbd_genl_family)) {
2112 unregister_blkdev(NBD_MAJOR, "nbd");
2113 destroy_workqueue(recv_workqueue);
2114 return -EINVAL;
2115 }
Markus Pargmann30d53d92015-08-17 08:20:06 +02002116 nbd_dbg_init();
2117
Josef Bacikb0d91112017-02-01 16:11:40 -05002118 mutex_lock(&nbd_index_mutex);
2119 for (i = 0; i < nbds_max; i++)
2120 nbd_dev_add(i);
2121 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05002123}
2124
2125static int nbd_exit_cb(int id, void *ptr, void *data)
2126{
Josef Bacikc6a47592017-04-06 17:02:06 -04002127 struct list_head *list = (struct list_head *)data;
Josef Bacikb0d91112017-02-01 16:11:40 -05002128 struct nbd_device *nbd = ptr;
Josef Bacikc6a47592017-04-06 17:02:06 -04002129
Josef Bacikc6a47592017-04-06 17:02:06 -04002130 list_add_tail(&nbd->list, list);
Josef Bacikb0d91112017-02-01 16:11:40 -05002131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134static void __exit nbd_cleanup(void)
2135{
Josef Bacikc6a47592017-04-06 17:02:06 -04002136 struct nbd_device *nbd;
2137 LIST_HEAD(del_list);
2138
Markus Pargmann30d53d92015-08-17 08:20:06 +02002139 nbd_dbg_close();
2140
Josef Bacikc6a47592017-04-06 17:02:06 -04002141 mutex_lock(&nbd_index_mutex);
2142 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2143 mutex_unlock(&nbd_index_mutex);
2144
Josef Bacik60ae36a2017-04-28 09:49:19 -04002145 while (!list_empty(&del_list)) {
2146 nbd = list_first_entry(&del_list, struct nbd_device, list);
2147 list_del_init(&nbd->list);
2148 if (refcount_read(&nbd->refs) != 1)
Josef Bacikc6a47592017-04-06 17:02:06 -04002149 printk(KERN_ERR "nbd: possibly leaking a device\n");
2150 nbd_put(nbd);
Josef Bacikc6a47592017-04-06 17:02:06 -04002151 }
2152
Josef Bacikb0d91112017-02-01 16:11:40 -05002153 idr_destroy(&nbd_index_idr);
Josef Bacike46c7282017-04-06 17:02:00 -04002154 genl_unregister_family(&nbd_genl_family);
Josef Bacik124d6db2017-02-01 16:11:11 -05002155 destroy_workqueue(recv_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
2159module_init(nbd_init);
2160module_exit(nbd_cleanup);
2161
2162MODULE_DESCRIPTION("Network Block Device");
2163MODULE_LICENSE("GPL");
2164
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07002165module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07002166MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2167module_param(max_part, int, 0444);
Josef Bacik7a8362a2017-08-14 18:56:16 +00002168MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");