nbd: remove nbd_del_disk
[linux-2.6-microblaze.git] / drivers / block / nbd.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Network block device - make block devices work over TCP
4  *
5  * Note that you can not swap over this thing, yet. Seems to work but
6  * deadlocks sometimes - you can not swap over TCP in general.
7  * 
8  * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
9  * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
10  *
11  * (part of code stolen from loop.c)
12  */
13
14 #include <linux/major.h>
15
16 #include <linux/blkdev.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/sched/mm.h>
21 #include <linux/fs.h>
22 #include <linux/bio.h>
23 #include <linux/stat.h>
24 #include <linux/errno.h>
25 #include <linux/file.h>
26 #include <linux/ioctl.h>
27 #include <linux/mutex.h>
28 #include <linux/compiler.h>
29 #include <linux/completion.h>
30 #include <linux/err.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <net/sock.h>
34 #include <linux/net.h>
35 #include <linux/kthread.h>
36 #include <linux/types.h>
37 #include <linux/debugfs.h>
38 #include <linux/blk-mq.h>
39
40 #include <linux/uaccess.h>
41 #include <asm/types.h>
42
43 #include <linux/nbd.h>
44 #include <linux/nbd-netlink.h>
45 #include <net/genetlink.h>
46
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/nbd.h>
49
50 static DEFINE_IDR(nbd_index_idr);
51 static DEFINE_MUTEX(nbd_index_mutex);
52 static struct workqueue_struct *nbd_del_wq;
53 static int nbd_total_devices = 0;
54
55 struct nbd_sock {
56         struct socket *sock;
57         struct mutex tx_lock;
58         struct request *pending;
59         int sent;
60         bool dead;
61         int fallback_index;
62         int cookie;
63 };
64
65 struct recv_thread_args {
66         struct work_struct work;
67         struct nbd_device *nbd;
68         int index;
69 };
70
71 struct link_dead_args {
72         struct work_struct work;
73         int index;
74 };
75
76 #define NBD_RT_TIMEDOUT                 0
77 #define NBD_RT_DISCONNECT_REQUESTED     1
78 #define NBD_RT_DISCONNECTED             2
79 #define NBD_RT_HAS_PID_FILE             3
80 #define NBD_RT_HAS_CONFIG_REF           4
81 #define NBD_RT_BOUND                    5
82 #define NBD_RT_DISCONNECT_ON_CLOSE      6
83 #define NBD_RT_HAS_BACKEND_FILE         7
84
85 #define NBD_DESTROY_ON_DISCONNECT       0
86 #define NBD_DISCONNECT_REQUESTED        1
87
88 struct nbd_config {
89         u32 flags;
90         unsigned long runtime_flags;
91         u64 dead_conn_timeout;
92
93         struct nbd_sock **socks;
94         int num_connections;
95         atomic_t live_connections;
96         wait_queue_head_t conn_wait;
97
98         atomic_t recv_threads;
99         wait_queue_head_t recv_wq;
100         loff_t blksize;
101         loff_t bytesize;
102 #if IS_ENABLED(CONFIG_DEBUG_FS)
103         struct dentry *dbg_dir;
104 #endif
105 };
106
107 struct nbd_device {
108         struct blk_mq_tag_set tag_set;
109
110         int index;
111         refcount_t config_refs;
112         refcount_t refs;
113         struct nbd_config *config;
114         struct mutex config_lock;
115         struct gendisk *disk;
116         struct workqueue_struct *recv_workq;
117         struct work_struct remove_work;
118
119         struct list_head list;
120         struct task_struct *task_recv;
121         struct task_struct *task_setup;
122
123         struct completion *destroy_complete;
124         unsigned long flags;
125
126         char *backend;
127 };
128
129 #define NBD_CMD_REQUEUED        1
130
131 struct nbd_cmd {
132         struct nbd_device *nbd;
133         struct mutex lock;
134         int index;
135         int cookie;
136         int retries;
137         blk_status_t status;
138         unsigned long flags;
139         u32 cmd_cookie;
140 };
141
142 #if IS_ENABLED(CONFIG_DEBUG_FS)
143 static struct dentry *nbd_dbg_dir;
144 #endif
145
146 #define nbd_name(nbd) ((nbd)->disk->disk_name)
147
148 #define NBD_MAGIC 0x68797548
149
150 #define NBD_DEF_BLKSIZE 1024
151
152 static unsigned int nbds_max = 16;
153 static int max_part = 16;
154 static int part_shift;
155
156 static int nbd_dev_dbg_init(struct nbd_device *nbd);
157 static void nbd_dev_dbg_close(struct nbd_device *nbd);
158 static void nbd_config_put(struct nbd_device *nbd);
159 static void nbd_connect_reply(struct genl_info *info, int index);
160 static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
161 static void nbd_dead_link_work(struct work_struct *work);
162 static void nbd_disconnect_and_put(struct nbd_device *nbd);
163
164 static inline struct device *nbd_to_dev(struct nbd_device *nbd)
165 {
166         return disk_to_dev(nbd->disk);
167 }
168
169 static void nbd_requeue_cmd(struct nbd_cmd *cmd)
170 {
171         struct request *req = blk_mq_rq_from_pdu(cmd);
172
173         if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
174                 blk_mq_requeue_request(req, true);
175 }
176
177 #define NBD_COOKIE_BITS 32
178
179 static u64 nbd_cmd_handle(struct nbd_cmd *cmd)
180 {
181         struct request *req = blk_mq_rq_from_pdu(cmd);
182         u32 tag = blk_mq_unique_tag(req);
183         u64 cookie = cmd->cmd_cookie;
184
185         return (cookie << NBD_COOKIE_BITS) | tag;
186 }
187
188 static u32 nbd_handle_to_tag(u64 handle)
189 {
190         return (u32)handle;
191 }
192
193 static u32 nbd_handle_to_cookie(u64 handle)
194 {
195         return (u32)(handle >> NBD_COOKIE_BITS);
196 }
197
198 static const char *nbdcmd_to_ascii(int cmd)
199 {
200         switch (cmd) {
201         case  NBD_CMD_READ: return "read";
202         case NBD_CMD_WRITE: return "write";
203         case  NBD_CMD_DISC: return "disconnect";
204         case NBD_CMD_FLUSH: return "flush";
205         case  NBD_CMD_TRIM: return "trim/discard";
206         }
207         return "invalid";
208 }
209
210 static ssize_t pid_show(struct device *dev,
211                         struct device_attribute *attr, char *buf)
212 {
213         struct gendisk *disk = dev_to_disk(dev);
214         struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
215
216         return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
217 }
218
219 static const struct device_attribute pid_attr = {
220         .attr = { .name = "pid", .mode = 0444},
221         .show = pid_show,
222 };
223
224 static ssize_t backend_show(struct device *dev,
225                 struct device_attribute *attr, char *buf)
226 {
227         struct gendisk *disk = dev_to_disk(dev);
228         struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
229
230         return sprintf(buf, "%s\n", nbd->backend ?: "");
231 }
232
233 static const struct device_attribute backend_attr = {
234         .attr = { .name = "backend", .mode = 0444},
235         .show = backend_show,
236 };
237
238 /*
239  * Place this in the last just before the nbd is freed to
240  * make sure that the disk and the related kobject are also
241  * totally removed to avoid duplicate creation of the same
242  * one.
243  */
244 static void nbd_notify_destroy_completion(struct nbd_device *nbd)
245 {
246         if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
247             nbd->destroy_complete)
248                 complete(nbd->destroy_complete);
249 }
250
251 static void nbd_dev_remove(struct nbd_device *nbd)
252 {
253         struct gendisk *disk = nbd->disk;
254
255         del_gendisk(disk);
256         blk_cleanup_disk(disk);
257         blk_mq_free_tag_set(&nbd->tag_set);
258
259         /*
260          * Remove from idr after del_gendisk() completes, so if the same ID is
261          * reused, the following add_disk() will succeed.
262          */
263         mutex_lock(&nbd_index_mutex);
264         idr_remove(&nbd_index_idr, nbd->index);
265         nbd_notify_destroy_completion(nbd);
266         mutex_unlock(&nbd_index_mutex);
267
268         kfree(nbd);
269 }
270
271 static void nbd_dev_remove_work(struct work_struct *work)
272 {
273         nbd_dev_remove(container_of(work, struct nbd_device, remove_work));
274 }
275
276 static void nbd_put(struct nbd_device *nbd)
277 {
278         if (!refcount_dec_and_test(&nbd->refs))
279                 return;
280
281         /* Call del_gendisk() asynchrounously to prevent deadlock */
282         if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags))
283                 queue_work(nbd_del_wq, &nbd->remove_work);
284         else
285                 nbd_dev_remove(nbd);
286 }
287
288 static int nbd_disconnected(struct nbd_config *config)
289 {
290         return test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags) ||
291                 test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
292 }
293
294 static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
295                                 int notify)
296 {
297         if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
298                 struct link_dead_args *args;
299                 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
300                 if (args) {
301                         INIT_WORK(&args->work, nbd_dead_link_work);
302                         args->index = nbd->index;
303                         queue_work(system_wq, &args->work);
304                 }
305         }
306         if (!nsock->dead) {
307                 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
308                 if (atomic_dec_return(&nbd->config->live_connections) == 0) {
309                         if (test_and_clear_bit(NBD_RT_DISCONNECT_REQUESTED,
310                                                &nbd->config->runtime_flags)) {
311                                 set_bit(NBD_RT_DISCONNECTED,
312                                         &nbd->config->runtime_flags);
313                                 dev_info(nbd_to_dev(nbd),
314                                         "Disconnected due to user request.\n");
315                         }
316                 }
317         }
318         nsock->dead = true;
319         nsock->pending = NULL;
320         nsock->sent = 0;
321 }
322
323 static void nbd_size_clear(struct nbd_device *nbd)
324 {
325         if (nbd->config->bytesize) {
326                 set_capacity(nbd->disk, 0);
327                 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
328         }
329 }
330
331 static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
332                 loff_t blksize)
333 {
334         if (!blksize)
335                 blksize = NBD_DEF_BLKSIZE;
336         if (blksize < 512 || blksize > PAGE_SIZE || !is_power_of_2(blksize))
337                 return -EINVAL;
338
339         nbd->config->bytesize = bytesize;
340         nbd->config->blksize = blksize;
341
342         if (!nbd->task_recv)
343                 return 0;
344
345         if (nbd->config->flags & NBD_FLAG_SEND_TRIM) {
346                 nbd->disk->queue->limits.discard_granularity = blksize;
347                 nbd->disk->queue->limits.discard_alignment = blksize;
348                 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
349         }
350         blk_queue_logical_block_size(nbd->disk->queue, blksize);
351         blk_queue_physical_block_size(nbd->disk->queue, blksize);
352
353         if (max_part)
354                 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
355         if (!set_capacity_and_notify(nbd->disk, bytesize >> 9))
356                 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
357         return 0;
358 }
359
360 static void nbd_complete_rq(struct request *req)
361 {
362         struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
363
364         dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
365                 cmd->status ? "failed" : "done");
366
367         blk_mq_end_request(req, cmd->status);
368 }
369
370 /*
371  * Forcibly shutdown the socket causing all listeners to error
372  */
373 static void sock_shutdown(struct nbd_device *nbd)
374 {
375         struct nbd_config *config = nbd->config;
376         int i;
377
378         if (config->num_connections == 0)
379                 return;
380         if (test_and_set_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
381                 return;
382
383         for (i = 0; i < config->num_connections; i++) {
384                 struct nbd_sock *nsock = config->socks[i];
385                 mutex_lock(&nsock->tx_lock);
386                 nbd_mark_nsock_dead(nbd, nsock, 0);
387                 mutex_unlock(&nsock->tx_lock);
388         }
389         dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
390 }
391
392 static u32 req_to_nbd_cmd_type(struct request *req)
393 {
394         switch (req_op(req)) {
395         case REQ_OP_DISCARD:
396                 return NBD_CMD_TRIM;
397         case REQ_OP_FLUSH:
398                 return NBD_CMD_FLUSH;
399         case REQ_OP_WRITE:
400                 return NBD_CMD_WRITE;
401         case REQ_OP_READ:
402                 return NBD_CMD_READ;
403         default:
404                 return U32_MAX;
405         }
406 }
407
408 static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
409                                                  bool reserved)
410 {
411         struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
412         struct nbd_device *nbd = cmd->nbd;
413         struct nbd_config *config;
414
415         if (!mutex_trylock(&cmd->lock))
416                 return BLK_EH_RESET_TIMER;
417
418         if (!refcount_inc_not_zero(&nbd->config_refs)) {
419                 cmd->status = BLK_STS_TIMEOUT;
420                 mutex_unlock(&cmd->lock);
421                 goto done;
422         }
423         config = nbd->config;
424
425         if (config->num_connections > 1 ||
426             (config->num_connections == 1 && nbd->tag_set.timeout)) {
427                 dev_err_ratelimited(nbd_to_dev(nbd),
428                                     "Connection timed out, retrying (%d/%d alive)\n",
429                                     atomic_read(&config->live_connections),
430                                     config->num_connections);
431                 /*
432                  * Hooray we have more connections, requeue this IO, the submit
433                  * path will put it on a real connection. Or if only one
434                  * connection is configured, the submit path will wait util
435                  * a new connection is reconfigured or util dead timeout.
436                  */
437                 if (config->socks) {
438                         if (cmd->index < config->num_connections) {
439                                 struct nbd_sock *nsock =
440                                         config->socks[cmd->index];
441                                 mutex_lock(&nsock->tx_lock);
442                                 /* We can have multiple outstanding requests, so
443                                  * we don't want to mark the nsock dead if we've
444                                  * already reconnected with a new socket, so
445                                  * only mark it dead if its the same socket we
446                                  * were sent out on.
447                                  */
448                                 if (cmd->cookie == nsock->cookie)
449                                         nbd_mark_nsock_dead(nbd, nsock, 1);
450                                 mutex_unlock(&nsock->tx_lock);
451                         }
452                         mutex_unlock(&cmd->lock);
453                         nbd_requeue_cmd(cmd);
454                         nbd_config_put(nbd);
455                         return BLK_EH_DONE;
456                 }
457         }
458
459         if (!nbd->tag_set.timeout) {
460                 /*
461                  * Userspace sets timeout=0 to disable socket disconnection,
462                  * so just warn and reset the timer.
463                  */
464                 struct nbd_sock *nsock = config->socks[cmd->index];
465                 cmd->retries++;
466                 dev_info(nbd_to_dev(nbd), "Possible stuck request %p: control (%s@%llu,%uB). Runtime %u seconds\n",
467                         req, nbdcmd_to_ascii(req_to_nbd_cmd_type(req)),
468                         (unsigned long long)blk_rq_pos(req) << 9,
469                         blk_rq_bytes(req), (req->timeout / HZ) * cmd->retries);
470
471                 mutex_lock(&nsock->tx_lock);
472                 if (cmd->cookie != nsock->cookie) {
473                         nbd_requeue_cmd(cmd);
474                         mutex_unlock(&nsock->tx_lock);
475                         mutex_unlock(&cmd->lock);
476                         nbd_config_put(nbd);
477                         return BLK_EH_DONE;
478                 }
479                 mutex_unlock(&nsock->tx_lock);
480                 mutex_unlock(&cmd->lock);
481                 nbd_config_put(nbd);
482                 return BLK_EH_RESET_TIMER;
483         }
484
485         dev_err_ratelimited(nbd_to_dev(nbd), "Connection timed out\n");
486         set_bit(NBD_RT_TIMEDOUT, &config->runtime_flags);
487         cmd->status = BLK_STS_IOERR;
488         mutex_unlock(&cmd->lock);
489         sock_shutdown(nbd);
490         nbd_config_put(nbd);
491 done:
492         blk_mq_complete_request(req);
493         return BLK_EH_DONE;
494 }
495
496 /*
497  *  Send or receive packet.
498  */
499 static int sock_xmit(struct nbd_device *nbd, int index, int send,
500                      struct iov_iter *iter, int msg_flags, int *sent)
501 {
502         struct nbd_config *config = nbd->config;
503         struct socket *sock = config->socks[index]->sock;
504         int result;
505         struct msghdr msg;
506         unsigned int noreclaim_flag;
507
508         if (unlikely(!sock)) {
509                 dev_err_ratelimited(disk_to_dev(nbd->disk),
510                         "Attempted %s on closed socket in sock_xmit\n",
511                         (send ? "send" : "recv"));
512                 return -EINVAL;
513         }
514
515         msg.msg_iter = *iter;
516
517         noreclaim_flag = memalloc_noreclaim_save();
518         do {
519                 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
520                 msg.msg_name = NULL;
521                 msg.msg_namelen = 0;
522                 msg.msg_control = NULL;
523                 msg.msg_controllen = 0;
524                 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
525
526                 if (send)
527                         result = sock_sendmsg(sock, &msg);
528                 else
529                         result = sock_recvmsg(sock, &msg, msg.msg_flags);
530
531                 if (result <= 0) {
532                         if (result == 0)
533                                 result = -EPIPE; /* short read */
534                         break;
535                 }
536                 if (sent)
537                         *sent += result;
538         } while (msg_data_left(&msg));
539
540         memalloc_noreclaim_restore(noreclaim_flag);
541
542         return result;
543 }
544
545 /*
546  * Different settings for sk->sk_sndtimeo can result in different return values
547  * if there is a signal pending when we enter sendmsg, because reasons?
548  */
549 static inline int was_interrupted(int result)
550 {
551         return result == -ERESTARTSYS || result == -EINTR;
552 }
553
554 /* always call with the tx_lock held */
555 static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
556 {
557         struct request *req = blk_mq_rq_from_pdu(cmd);
558         struct nbd_config *config = nbd->config;
559         struct nbd_sock *nsock = config->socks[index];
560         int result;
561         struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
562         struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
563         struct iov_iter from;
564         unsigned long size = blk_rq_bytes(req);
565         struct bio *bio;
566         u64 handle;
567         u32 type;
568         u32 nbd_cmd_flags = 0;
569         int sent = nsock->sent, skip = 0;
570
571         iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
572
573         type = req_to_nbd_cmd_type(req);
574         if (type == U32_MAX)
575                 return -EIO;
576
577         if (rq_data_dir(req) == WRITE &&
578             (config->flags & NBD_FLAG_READ_ONLY)) {
579                 dev_err_ratelimited(disk_to_dev(nbd->disk),
580                                     "Write on read-only\n");
581                 return -EIO;
582         }
583
584         if (req->cmd_flags & REQ_FUA)
585                 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
586
587         /* We did a partial send previously, and we at least sent the whole
588          * request struct, so just go and send the rest of the pages in the
589          * request.
590          */
591         if (sent) {
592                 if (sent >= sizeof(request)) {
593                         skip = sent - sizeof(request);
594
595                         /* initialize handle for tracing purposes */
596                         handle = nbd_cmd_handle(cmd);
597
598                         goto send_pages;
599                 }
600                 iov_iter_advance(&from, sent);
601         } else {
602                 cmd->cmd_cookie++;
603         }
604         cmd->index = index;
605         cmd->cookie = nsock->cookie;
606         cmd->retries = 0;
607         request.type = htonl(type | nbd_cmd_flags);
608         if (type != NBD_CMD_FLUSH) {
609                 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
610                 request.len = htonl(size);
611         }
612         handle = nbd_cmd_handle(cmd);
613         memcpy(request.handle, &handle, sizeof(handle));
614
615         trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
616
617         dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
618                 req, nbdcmd_to_ascii(type),
619                 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
620         result = sock_xmit(nbd, index, 1, &from,
621                         (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
622         trace_nbd_header_sent(req, handle);
623         if (result <= 0) {
624                 if (was_interrupted(result)) {
625                         /* If we havne't sent anything we can just return BUSY,
626                          * however if we have sent something we need to make
627                          * sure we only allow this req to be sent until we are
628                          * completely done.
629                          */
630                         if (sent) {
631                                 nsock->pending = req;
632                                 nsock->sent = sent;
633                         }
634                         set_bit(NBD_CMD_REQUEUED, &cmd->flags);
635                         return BLK_STS_RESOURCE;
636                 }
637                 dev_err_ratelimited(disk_to_dev(nbd->disk),
638                         "Send control failed (result %d)\n", result);
639                 return -EAGAIN;
640         }
641 send_pages:
642         if (type != NBD_CMD_WRITE)
643                 goto out;
644
645         bio = req->bio;
646         while (bio) {
647                 struct bio *next = bio->bi_next;
648                 struct bvec_iter iter;
649                 struct bio_vec bvec;
650
651                 bio_for_each_segment(bvec, bio, iter) {
652                         bool is_last = !next && bio_iter_last(bvec, iter);
653                         int flags = is_last ? 0 : MSG_MORE;
654
655                         dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
656                                 req, bvec.bv_len);
657                         iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
658                         if (skip) {
659                                 if (skip >= iov_iter_count(&from)) {
660                                         skip -= iov_iter_count(&from);
661                                         continue;
662                                 }
663                                 iov_iter_advance(&from, skip);
664                                 skip = 0;
665                         }
666                         result = sock_xmit(nbd, index, 1, &from, flags, &sent);
667                         if (result <= 0) {
668                                 if (was_interrupted(result)) {
669                                         /* We've already sent the header, we
670                                          * have no choice but to set pending and
671                                          * return BUSY.
672                                          */
673                                         nsock->pending = req;
674                                         nsock->sent = sent;
675                                         set_bit(NBD_CMD_REQUEUED, &cmd->flags);
676                                         return BLK_STS_RESOURCE;
677                                 }
678                                 dev_err(disk_to_dev(nbd->disk),
679                                         "Send data failed (result %d)\n",
680                                         result);
681                                 return -EAGAIN;
682                         }
683                         /*
684                          * The completion might already have come in,
685                          * so break for the last one instead of letting
686                          * the iterator do it. This prevents use-after-free
687                          * of the bio.
688                          */
689                         if (is_last)
690                                 break;
691                 }
692                 bio = next;
693         }
694 out:
695         trace_nbd_payload_sent(req, handle);
696         nsock->pending = NULL;
697         nsock->sent = 0;
698         return 0;
699 }
700
701 /* NULL returned = something went wrong, inform userspace */
702 static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
703 {
704         struct nbd_config *config = nbd->config;
705         int result;
706         struct nbd_reply reply;
707         struct nbd_cmd *cmd;
708         struct request *req = NULL;
709         u64 handle;
710         u16 hwq;
711         u32 tag;
712         struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
713         struct iov_iter to;
714         int ret = 0;
715
716         reply.magic = 0;
717         iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply));
718         result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
719         if (result <= 0) {
720                 if (!nbd_disconnected(config))
721                         dev_err(disk_to_dev(nbd->disk),
722                                 "Receive control failed (result %d)\n", result);
723                 return ERR_PTR(result);
724         }
725
726         if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
727                 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
728                                 (unsigned long)ntohl(reply.magic));
729                 return ERR_PTR(-EPROTO);
730         }
731
732         memcpy(&handle, reply.handle, sizeof(handle));
733         tag = nbd_handle_to_tag(handle);
734         hwq = blk_mq_unique_tag_to_hwq(tag);
735         if (hwq < nbd->tag_set.nr_hw_queues)
736                 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
737                                        blk_mq_unique_tag_to_tag(tag));
738         if (!req || !blk_mq_request_started(req)) {
739                 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
740                         tag, req);
741                 return ERR_PTR(-ENOENT);
742         }
743         trace_nbd_header_received(req, handle);
744         cmd = blk_mq_rq_to_pdu(req);
745
746         mutex_lock(&cmd->lock);
747         if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
748                 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
749                         req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
750                 ret = -ENOENT;
751                 goto out;
752         }
753         if (cmd->status != BLK_STS_OK) {
754                 dev_err(disk_to_dev(nbd->disk), "Command already handled %p\n",
755                         req);
756                 ret = -ENOENT;
757                 goto out;
758         }
759         if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
760                 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
761                         req);
762                 ret = -ENOENT;
763                 goto out;
764         }
765         if (ntohl(reply.error)) {
766                 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
767                         ntohl(reply.error));
768                 cmd->status = BLK_STS_IOERR;
769                 goto out;
770         }
771
772         dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
773         if (rq_data_dir(req) != WRITE) {
774                 struct req_iterator iter;
775                 struct bio_vec bvec;
776
777                 rq_for_each_segment(bvec, req, iter) {
778                         iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
779                         result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
780                         if (result <= 0) {
781                                 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
782                                         result);
783                                 /*
784                                  * If we've disconnected, we need to make sure we
785                                  * complete this request, otherwise error out
786                                  * and let the timeout stuff handle resubmitting
787                                  * this request onto another connection.
788                                  */
789                                 if (nbd_disconnected(config)) {
790                                         cmd->status = BLK_STS_IOERR;
791                                         goto out;
792                                 }
793                                 ret = -EIO;
794                                 goto out;
795                         }
796                         dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
797                                 req, bvec.bv_len);
798                 }
799         }
800 out:
801         trace_nbd_payload_received(req, handle);
802         mutex_unlock(&cmd->lock);
803         return ret ? ERR_PTR(ret) : cmd;
804 }
805
806 static void recv_work(struct work_struct *work)
807 {
808         struct recv_thread_args *args = container_of(work,
809                                                      struct recv_thread_args,
810                                                      work);
811         struct nbd_device *nbd = args->nbd;
812         struct nbd_config *config = nbd->config;
813         struct nbd_cmd *cmd;
814         struct request *rq;
815
816         while (1) {
817                 cmd = nbd_read_stat(nbd, args->index);
818                 if (IS_ERR(cmd)) {
819                         struct nbd_sock *nsock = config->socks[args->index];
820
821                         mutex_lock(&nsock->tx_lock);
822                         nbd_mark_nsock_dead(nbd, nsock, 1);
823                         mutex_unlock(&nsock->tx_lock);
824                         break;
825                 }
826
827                 rq = blk_mq_rq_from_pdu(cmd);
828                 if (likely(!blk_should_fake_timeout(rq->q)))
829                         blk_mq_complete_request(rq);
830         }
831         nbd_config_put(nbd);
832         atomic_dec(&config->recv_threads);
833         wake_up(&config->recv_wq);
834         kfree(args);
835 }
836
837 static bool nbd_clear_req(struct request *req, void *data, bool reserved)
838 {
839         struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
840
841         mutex_lock(&cmd->lock);
842         cmd->status = BLK_STS_IOERR;
843         mutex_unlock(&cmd->lock);
844
845         blk_mq_complete_request(req);
846         return true;
847 }
848
849 static void nbd_clear_que(struct nbd_device *nbd)
850 {
851         blk_mq_quiesce_queue(nbd->disk->queue);
852         blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
853         blk_mq_unquiesce_queue(nbd->disk->queue);
854         dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
855 }
856
857 static int find_fallback(struct nbd_device *nbd, int index)
858 {
859         struct nbd_config *config = nbd->config;
860         int new_index = -1;
861         struct nbd_sock *nsock = config->socks[index];
862         int fallback = nsock->fallback_index;
863
864         if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
865                 return new_index;
866
867         if (config->num_connections <= 1) {
868                 dev_err_ratelimited(disk_to_dev(nbd->disk),
869                                     "Dead connection, failed to find a fallback\n");
870                 return new_index;
871         }
872
873         if (fallback >= 0 && fallback < config->num_connections &&
874             !config->socks[fallback]->dead)
875                 return fallback;
876
877         if (nsock->fallback_index < 0 ||
878             nsock->fallback_index >= config->num_connections ||
879             config->socks[nsock->fallback_index]->dead) {
880                 int i;
881                 for (i = 0; i < config->num_connections; i++) {
882                         if (i == index)
883                                 continue;
884                         if (!config->socks[i]->dead) {
885                                 new_index = i;
886                                 break;
887                         }
888                 }
889                 nsock->fallback_index = new_index;
890                 if (new_index < 0) {
891                         dev_err_ratelimited(disk_to_dev(nbd->disk),
892                                             "Dead connection, failed to find a fallback\n");
893                         return new_index;
894                 }
895         }
896         new_index = nsock->fallback_index;
897         return new_index;
898 }
899
900 static int wait_for_reconnect(struct nbd_device *nbd)
901 {
902         struct nbd_config *config = nbd->config;
903         if (!config->dead_conn_timeout)
904                 return 0;
905         if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
906                 return 0;
907         return wait_event_timeout(config->conn_wait,
908                                   atomic_read(&config->live_connections) > 0,
909                                   config->dead_conn_timeout) > 0;
910 }
911
912 static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
913 {
914         struct request *req = blk_mq_rq_from_pdu(cmd);
915         struct nbd_device *nbd = cmd->nbd;
916         struct nbd_config *config;
917         struct nbd_sock *nsock;
918         int ret;
919
920         if (!refcount_inc_not_zero(&nbd->config_refs)) {
921                 dev_err_ratelimited(disk_to_dev(nbd->disk),
922                                     "Socks array is empty\n");
923                 blk_mq_start_request(req);
924                 return -EINVAL;
925         }
926         config = nbd->config;
927
928         if (index >= config->num_connections) {
929                 dev_err_ratelimited(disk_to_dev(nbd->disk),
930                                     "Attempted send on invalid socket\n");
931                 nbd_config_put(nbd);
932                 blk_mq_start_request(req);
933                 return -EINVAL;
934         }
935         cmd->status = BLK_STS_OK;
936 again:
937         nsock = config->socks[index];
938         mutex_lock(&nsock->tx_lock);
939         if (nsock->dead) {
940                 int old_index = index;
941                 index = find_fallback(nbd, index);
942                 mutex_unlock(&nsock->tx_lock);
943                 if (index < 0) {
944                         if (wait_for_reconnect(nbd)) {
945                                 index = old_index;
946                                 goto again;
947                         }
948                         /* All the sockets should already be down at this point,
949                          * we just want to make sure that DISCONNECTED is set so
950                          * any requests that come in that were queue'ed waiting
951                          * for the reconnect timer don't trigger the timer again
952                          * and instead just error out.
953                          */
954                         sock_shutdown(nbd);
955                         nbd_config_put(nbd);
956                         blk_mq_start_request(req);
957                         return -EIO;
958                 }
959                 goto again;
960         }
961
962         /* Handle the case that we have a pending request that was partially
963          * transmitted that _has_ to be serviced first.  We need to call requeue
964          * here so that it gets put _after_ the request that is already on the
965          * dispatch list.
966          */
967         blk_mq_start_request(req);
968         if (unlikely(nsock->pending && nsock->pending != req)) {
969                 nbd_requeue_cmd(cmd);
970                 ret = 0;
971                 goto out;
972         }
973         /*
974          * Some failures are related to the link going down, so anything that
975          * returns EAGAIN can be retried on a different socket.
976          */
977         ret = nbd_send_cmd(nbd, cmd, index);
978         if (ret == -EAGAIN) {
979                 dev_err_ratelimited(disk_to_dev(nbd->disk),
980                                     "Request send failed, requeueing\n");
981                 nbd_mark_nsock_dead(nbd, nsock, 1);
982                 nbd_requeue_cmd(cmd);
983                 ret = 0;
984         }
985 out:
986         mutex_unlock(&nsock->tx_lock);
987         nbd_config_put(nbd);
988         return ret;
989 }
990
991 static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
992                         const struct blk_mq_queue_data *bd)
993 {
994         struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
995         int ret;
996
997         /*
998          * Since we look at the bio's to send the request over the network we
999          * need to make sure the completion work doesn't mark this request done
1000          * before we are done doing our send.  This keeps us from dereferencing
1001          * freed data if we have particularly fast completions (ie we get the
1002          * completion before we exit sock_xmit on the last bvec) or in the case
1003          * that the server is misbehaving (or there was an error) before we're
1004          * done sending everything over the wire.
1005          */
1006         mutex_lock(&cmd->lock);
1007         clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
1008
1009         /* We can be called directly from the user space process, which means we
1010          * could possibly have signals pending so our sendmsg will fail.  In
1011          * this case we need to return that we are busy, otherwise error out as
1012          * appropriate.
1013          */
1014         ret = nbd_handle_cmd(cmd, hctx->queue_num);
1015         if (ret < 0)
1016                 ret = BLK_STS_IOERR;
1017         else if (!ret)
1018                 ret = BLK_STS_OK;
1019         mutex_unlock(&cmd->lock);
1020
1021         return ret;
1022 }
1023
1024 static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
1025                                      int *err)
1026 {
1027         struct socket *sock;
1028
1029         *err = 0;
1030         sock = sockfd_lookup(fd, err);
1031         if (!sock)
1032                 return NULL;
1033
1034         if (sock->ops->shutdown == sock_no_shutdown) {
1035                 dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
1036                 *err = -EINVAL;
1037                 sockfd_put(sock);
1038                 return NULL;
1039         }
1040
1041         return sock;
1042 }
1043
1044 static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
1045                           bool netlink)
1046 {
1047         struct nbd_config *config = nbd->config;
1048         struct socket *sock;
1049         struct nbd_sock **socks;
1050         struct nbd_sock *nsock;
1051         int err;
1052
1053         sock = nbd_get_socket(nbd, arg, &err);
1054         if (!sock)
1055                 return err;
1056
1057         /*
1058          * We need to make sure we don't get any errant requests while we're
1059          * reallocating the ->socks array.
1060          */
1061         blk_mq_freeze_queue(nbd->disk->queue);
1062
1063         if (!netlink && !nbd->task_setup &&
1064             !test_bit(NBD_RT_BOUND, &config->runtime_flags))
1065                 nbd->task_setup = current;
1066
1067         if (!netlink &&
1068             (nbd->task_setup != current ||
1069              test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
1070                 dev_err(disk_to_dev(nbd->disk),
1071                         "Device being setup by another task");
1072                 err = -EBUSY;
1073                 goto put_socket;
1074         }
1075
1076         nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
1077         if (!nsock) {
1078                 err = -ENOMEM;
1079                 goto put_socket;
1080         }
1081
1082         socks = krealloc(config->socks, (config->num_connections + 1) *
1083                          sizeof(struct nbd_sock *), GFP_KERNEL);
1084         if (!socks) {
1085                 kfree(nsock);
1086                 err = -ENOMEM;
1087                 goto put_socket;
1088         }
1089
1090         config->socks = socks;
1091
1092         nsock->fallback_index = -1;
1093         nsock->dead = false;
1094         mutex_init(&nsock->tx_lock);
1095         nsock->sock = sock;
1096         nsock->pending = NULL;
1097         nsock->sent = 0;
1098         nsock->cookie = 0;
1099         socks[config->num_connections++] = nsock;
1100         atomic_inc(&config->live_connections);
1101         blk_mq_unfreeze_queue(nbd->disk->queue);
1102
1103         return 0;
1104
1105 put_socket:
1106         blk_mq_unfreeze_queue(nbd->disk->queue);
1107         sockfd_put(sock);
1108         return err;
1109 }
1110
1111 static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
1112 {
1113         struct nbd_config *config = nbd->config;
1114         struct socket *sock, *old;
1115         struct recv_thread_args *args;
1116         int i;
1117         int err;
1118
1119         sock = nbd_get_socket(nbd, arg, &err);
1120         if (!sock)
1121                 return err;
1122
1123         args = kzalloc(sizeof(*args), GFP_KERNEL);
1124         if (!args) {
1125                 sockfd_put(sock);
1126                 return -ENOMEM;
1127         }
1128
1129         for (i = 0; i < config->num_connections; i++) {
1130                 struct nbd_sock *nsock = config->socks[i];
1131
1132                 if (!nsock->dead)
1133                         continue;
1134
1135                 mutex_lock(&nsock->tx_lock);
1136                 if (!nsock->dead) {
1137                         mutex_unlock(&nsock->tx_lock);
1138                         continue;
1139                 }
1140                 sk_set_memalloc(sock->sk);
1141                 if (nbd->tag_set.timeout)
1142                         sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
1143                 atomic_inc(&config->recv_threads);
1144                 refcount_inc(&nbd->config_refs);
1145                 old = nsock->sock;
1146                 nsock->fallback_index = -1;
1147                 nsock->sock = sock;
1148                 nsock->dead = false;
1149                 INIT_WORK(&args->work, recv_work);
1150                 args->index = i;
1151                 args->nbd = nbd;
1152                 nsock->cookie++;
1153                 mutex_unlock(&nsock->tx_lock);
1154                 sockfd_put(old);
1155
1156                 clear_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
1157
1158                 /* We take the tx_mutex in an error path in the recv_work, so we
1159                  * need to queue_work outside of the tx_mutex.
1160                  */
1161                 queue_work(nbd->recv_workq, &args->work);
1162
1163                 atomic_inc(&config->live_connections);
1164                 wake_up(&config->conn_wait);
1165                 return 0;
1166         }
1167         sockfd_put(sock);
1168         kfree(args);
1169         return -ENOSPC;
1170 }
1171
1172 static void nbd_bdev_reset(struct block_device *bdev)
1173 {
1174         if (bdev->bd_openers > 1)
1175                 return;
1176         set_capacity(bdev->bd_disk, 0);
1177 }
1178
1179 static void nbd_parse_flags(struct nbd_device *nbd)
1180 {
1181         struct nbd_config *config = nbd->config;
1182         if (config->flags & NBD_FLAG_READ_ONLY)
1183                 set_disk_ro(nbd->disk, true);
1184         else
1185                 set_disk_ro(nbd->disk, false);
1186         if (config->flags & NBD_FLAG_SEND_TRIM)
1187                 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
1188         if (config->flags & NBD_FLAG_SEND_FLUSH) {
1189                 if (config->flags & NBD_FLAG_SEND_FUA)
1190                         blk_queue_write_cache(nbd->disk->queue, true, true);
1191                 else
1192                         blk_queue_write_cache(nbd->disk->queue, true, false);
1193         }
1194         else
1195                 blk_queue_write_cache(nbd->disk->queue, false, false);
1196 }
1197
1198 static void send_disconnects(struct nbd_device *nbd)
1199 {
1200         struct nbd_config *config = nbd->config;
1201         struct nbd_request request = {
1202                 .magic = htonl(NBD_REQUEST_MAGIC),
1203                 .type = htonl(NBD_CMD_DISC),
1204         };
1205         struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1206         struct iov_iter from;
1207         int i, ret;
1208
1209         for (i = 0; i < config->num_connections; i++) {
1210                 struct nbd_sock *nsock = config->socks[i];
1211
1212                 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
1213                 mutex_lock(&nsock->tx_lock);
1214                 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
1215                 if (ret <= 0)
1216                         dev_err(disk_to_dev(nbd->disk),
1217                                 "Send disconnect failed %d\n", ret);
1218                 mutex_unlock(&nsock->tx_lock);
1219         }
1220 }
1221
1222 static int nbd_disconnect(struct nbd_device *nbd)
1223 {
1224         struct nbd_config *config = nbd->config;
1225
1226         dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
1227         set_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
1228         set_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags);
1229         send_disconnects(nbd);
1230         return 0;
1231 }
1232
1233 static void nbd_clear_sock(struct nbd_device *nbd)
1234 {
1235         sock_shutdown(nbd);
1236         nbd_clear_que(nbd);
1237         nbd->task_setup = NULL;
1238 }
1239
1240 static void nbd_config_put(struct nbd_device *nbd)
1241 {
1242         if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1243                                         &nbd->config_lock)) {
1244                 struct nbd_config *config = nbd->config;
1245                 nbd_dev_dbg_close(nbd);
1246                 nbd_size_clear(nbd);
1247                 if (test_and_clear_bit(NBD_RT_HAS_PID_FILE,
1248                                        &config->runtime_flags))
1249                         device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1250                 nbd->task_recv = NULL;
1251                 if (test_and_clear_bit(NBD_RT_HAS_BACKEND_FILE,
1252                                        &config->runtime_flags)) {
1253                         device_remove_file(disk_to_dev(nbd->disk), &backend_attr);
1254                         kfree(nbd->backend);
1255                         nbd->backend = NULL;
1256                 }
1257                 nbd_clear_sock(nbd);
1258                 if (config->num_connections) {
1259                         int i;
1260                         for (i = 0; i < config->num_connections; i++) {
1261                                 sockfd_put(config->socks[i]->sock);
1262                                 kfree(config->socks[i]);
1263                         }
1264                         kfree(config->socks);
1265                 }
1266                 kfree(nbd->config);
1267                 nbd->config = NULL;
1268
1269                 if (nbd->recv_workq)
1270                         destroy_workqueue(nbd->recv_workq);
1271                 nbd->recv_workq = NULL;
1272
1273                 nbd->tag_set.timeout = 0;
1274                 nbd->disk->queue->limits.discard_granularity = 0;
1275                 nbd->disk->queue->limits.discard_alignment = 0;
1276                 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
1277                 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
1278
1279                 mutex_unlock(&nbd->config_lock);
1280                 nbd_put(nbd);
1281                 module_put(THIS_MODULE);
1282         }
1283 }
1284
1285 static int nbd_start_device(struct nbd_device *nbd)
1286 {
1287         struct nbd_config *config = nbd->config;
1288         int num_connections = config->num_connections;
1289         int error = 0, i;
1290
1291         if (nbd->task_recv)
1292                 return -EBUSY;
1293         if (!config->socks)
1294                 return -EINVAL;
1295         if (num_connections > 1 &&
1296             !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
1297                 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
1298                 return -EINVAL;
1299         }
1300
1301         nbd->recv_workq = alloc_workqueue("knbd%d-recv",
1302                                           WQ_MEM_RECLAIM | WQ_HIGHPRI |
1303                                           WQ_UNBOUND, 0, nbd->index);
1304         if (!nbd->recv_workq) {
1305                 dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1306                 return -ENOMEM;
1307         }
1308
1309         blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
1310         nbd->task_recv = current;
1311
1312         nbd_parse_flags(nbd);
1313
1314         error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1315         if (error) {
1316                 dev_err(disk_to_dev(nbd->disk), "device_create_file failed for pid!\n");
1317                 return error;
1318         }
1319         set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags);
1320
1321         nbd_dev_dbg_init(nbd);
1322         for (i = 0; i < num_connections; i++) {
1323                 struct recv_thread_args *args;
1324
1325                 args = kzalloc(sizeof(*args), GFP_KERNEL);
1326                 if (!args) {
1327                         sock_shutdown(nbd);
1328                         /*
1329                          * If num_connections is m (2 < m),
1330                          * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
1331                          * But NO.(n + 1) failed. We still have n recv threads.
1332                          * So, add flush_workqueue here to prevent recv threads
1333                          * dropping the last config_refs and trying to destroy
1334                          * the workqueue from inside the workqueue.
1335                          */
1336                         if (i)
1337                                 flush_workqueue(nbd->recv_workq);
1338                         return -ENOMEM;
1339                 }
1340                 sk_set_memalloc(config->socks[i]->sock->sk);
1341                 if (nbd->tag_set.timeout)
1342                         config->socks[i]->sock->sk->sk_sndtimeo =
1343                                 nbd->tag_set.timeout;
1344                 atomic_inc(&config->recv_threads);
1345                 refcount_inc(&nbd->config_refs);
1346                 INIT_WORK(&args->work, recv_work);
1347                 args->nbd = nbd;
1348                 args->index = i;
1349                 queue_work(nbd->recv_workq, &args->work);
1350         }
1351         return nbd_set_size(nbd, config->bytesize, config->blksize);
1352 }
1353
1354 static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1355 {
1356         struct nbd_config *config = nbd->config;
1357         int ret;
1358
1359         ret = nbd_start_device(nbd);
1360         if (ret)
1361                 return ret;
1362
1363         if (max_part)
1364                 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
1365         mutex_unlock(&nbd->config_lock);
1366         ret = wait_event_interruptible(config->recv_wq,
1367                                          atomic_read(&config->recv_threads) == 0);
1368         if (ret)
1369                 sock_shutdown(nbd);
1370         flush_workqueue(nbd->recv_workq);
1371
1372         mutex_lock(&nbd->config_lock);
1373         nbd_bdev_reset(bdev);
1374         /* user requested, ignore socket errors */
1375         if (test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags))
1376                 ret = 0;
1377         if (test_bit(NBD_RT_TIMEDOUT, &config->runtime_flags))
1378                 ret = -ETIMEDOUT;
1379         return ret;
1380 }
1381
1382 static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1383                                  struct block_device *bdev)
1384 {
1385         sock_shutdown(nbd);
1386         __invalidate_device(bdev, true);
1387         nbd_bdev_reset(bdev);
1388         if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
1389                                &nbd->config->runtime_flags))
1390                 nbd_config_put(nbd);
1391 }
1392
1393 static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
1394 {
1395         nbd->tag_set.timeout = timeout * HZ;
1396         if (timeout)
1397                 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1398         else
1399                 blk_queue_rq_timeout(nbd->disk->queue, 30 * HZ);
1400 }
1401
1402 /* Must be called with config_lock held */
1403 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
1404                        unsigned int cmd, unsigned long arg)
1405 {
1406         struct nbd_config *config = nbd->config;
1407         loff_t bytesize;
1408
1409         switch (cmd) {
1410         case NBD_DISCONNECT:
1411                 return nbd_disconnect(nbd);
1412         case NBD_CLEAR_SOCK:
1413                 nbd_clear_sock_ioctl(nbd, bdev);
1414                 return 0;
1415         case NBD_SET_SOCK:
1416                 return nbd_add_socket(nbd, arg, false);
1417         case NBD_SET_BLKSIZE:
1418                 return nbd_set_size(nbd, config->bytesize, arg);
1419         case NBD_SET_SIZE:
1420                 return nbd_set_size(nbd, arg, config->blksize);
1421         case NBD_SET_SIZE_BLOCKS:
1422                 if (check_mul_overflow((loff_t)arg, config->blksize, &bytesize))
1423                         return -EINVAL;
1424                 return nbd_set_size(nbd, bytesize, config->blksize);
1425         case NBD_SET_TIMEOUT:
1426                 nbd_set_cmd_timeout(nbd, arg);
1427                 return 0;
1428
1429         case NBD_SET_FLAGS:
1430                 config->flags = arg;
1431                 return 0;
1432         case NBD_DO_IT:
1433                 return nbd_start_device_ioctl(nbd, bdev);
1434         case NBD_CLEAR_QUE:
1435                 /*
1436                  * This is for compatibility only.  The queue is always cleared
1437                  * by NBD_DO_IT or NBD_CLEAR_SOCK.
1438                  */
1439                 return 0;
1440         case NBD_PRINT_DEBUG:
1441                 /*
1442                  * For compatibility only, we no longer keep a list of
1443                  * outstanding requests.
1444                  */
1445                 return 0;
1446         }
1447         return -ENOTTY;
1448 }
1449
1450 static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1451                      unsigned int cmd, unsigned long arg)
1452 {
1453         struct nbd_device *nbd = bdev->bd_disk->private_data;
1454         struct nbd_config *config = nbd->config;
1455         int error = -EINVAL;
1456
1457         if (!capable(CAP_SYS_ADMIN))
1458                 return -EPERM;
1459
1460         /* The block layer will pass back some non-nbd ioctls in case we have
1461          * special handling for them, but we don't so just return an error.
1462          */
1463         if (_IOC_TYPE(cmd) != 0xab)
1464                 return -EINVAL;
1465
1466         mutex_lock(&nbd->config_lock);
1467
1468         /* Don't allow ioctl operations on a nbd device that was created with
1469          * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1470          */
1471         if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
1472             (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1473                 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1474         else
1475                 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
1476         mutex_unlock(&nbd->config_lock);
1477         return error;
1478 }
1479
1480 static struct nbd_config *nbd_alloc_config(void)
1481 {
1482         struct nbd_config *config;
1483
1484         config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1485         if (!config)
1486                 return NULL;
1487         atomic_set(&config->recv_threads, 0);
1488         init_waitqueue_head(&config->recv_wq);
1489         init_waitqueue_head(&config->conn_wait);
1490         config->blksize = NBD_DEF_BLKSIZE;
1491         atomic_set(&config->live_connections, 0);
1492         try_module_get(THIS_MODULE);
1493         return config;
1494 }
1495
1496 static int nbd_open(struct block_device *bdev, fmode_t mode)
1497 {
1498         struct nbd_device *nbd;
1499         int ret = 0;
1500
1501         mutex_lock(&nbd_index_mutex);
1502         nbd = bdev->bd_disk->private_data;
1503         if (!nbd) {
1504                 ret = -ENXIO;
1505                 goto out;
1506         }
1507         if (!refcount_inc_not_zero(&nbd->refs)) {
1508                 ret = -ENXIO;
1509                 goto out;
1510         }
1511         if (!refcount_inc_not_zero(&nbd->config_refs)) {
1512                 struct nbd_config *config;
1513
1514                 mutex_lock(&nbd->config_lock);
1515                 if (refcount_inc_not_zero(&nbd->config_refs)) {
1516                         mutex_unlock(&nbd->config_lock);
1517                         goto out;
1518                 }
1519                 config = nbd->config = nbd_alloc_config();
1520                 if (!config) {
1521                         ret = -ENOMEM;
1522                         mutex_unlock(&nbd->config_lock);
1523                         goto out;
1524                 }
1525                 refcount_set(&nbd->config_refs, 1);
1526                 refcount_inc(&nbd->refs);
1527                 mutex_unlock(&nbd->config_lock);
1528                 if (max_part)
1529                         set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1530         } else if (nbd_disconnected(nbd->config)) {
1531                 if (max_part)
1532                         set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1533         }
1534 out:
1535         mutex_unlock(&nbd_index_mutex);
1536         return ret;
1537 }
1538
1539 static void nbd_release(struct gendisk *disk, fmode_t mode)
1540 {
1541         struct nbd_device *nbd = disk->private_data;
1542
1543         if (test_bit(NBD_RT_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
1544                         disk->part0->bd_openers == 0)
1545                 nbd_disconnect_and_put(nbd);
1546
1547         nbd_config_put(nbd);
1548         nbd_put(nbd);
1549 }
1550
1551 static const struct block_device_operations nbd_fops =
1552 {
1553         .owner =        THIS_MODULE,
1554         .open =         nbd_open,
1555         .release =      nbd_release,
1556         .ioctl =        nbd_ioctl,
1557         .compat_ioctl = nbd_ioctl,
1558 };
1559
1560 #if IS_ENABLED(CONFIG_DEBUG_FS)
1561
1562 static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1563 {
1564         struct nbd_device *nbd = s->private;
1565
1566         if (nbd->task_recv)
1567                 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
1568
1569         return 0;
1570 }
1571
1572 DEFINE_SHOW_ATTRIBUTE(nbd_dbg_tasks);
1573
1574 static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1575 {
1576         struct nbd_device *nbd = s->private;
1577         u32 flags = nbd->config->flags;
1578
1579         seq_printf(s, "Hex: 0x%08x\n\n", flags);
1580
1581         seq_puts(s, "Known flags:\n");
1582
1583         if (flags & NBD_FLAG_HAS_FLAGS)
1584                 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1585         if (flags & NBD_FLAG_READ_ONLY)
1586                 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1587         if (flags & NBD_FLAG_SEND_FLUSH)
1588                 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
1589         if (flags & NBD_FLAG_SEND_FUA)
1590                 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
1591         if (flags & NBD_FLAG_SEND_TRIM)
1592                 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1593
1594         return 0;
1595 }
1596
1597 DEFINE_SHOW_ATTRIBUTE(nbd_dbg_flags);
1598
1599 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1600 {
1601         struct dentry *dir;
1602         struct nbd_config *config = nbd->config;
1603
1604         if (!nbd_dbg_dir)
1605                 return -EIO;
1606
1607         dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
1608         if (!dir) {
1609                 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1610                         nbd_name(nbd));
1611                 return -EIO;
1612         }
1613         config->dbg_dir = dir;
1614
1615         debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_fops);
1616         debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
1617         debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
1618         debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
1619         debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_fops);
1620
1621         return 0;
1622 }
1623
1624 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1625 {
1626         debugfs_remove_recursive(nbd->config->dbg_dir);
1627 }
1628
1629 static int nbd_dbg_init(void)
1630 {
1631         struct dentry *dbg_dir;
1632
1633         dbg_dir = debugfs_create_dir("nbd", NULL);
1634         if (!dbg_dir)
1635                 return -EIO;
1636
1637         nbd_dbg_dir = dbg_dir;
1638
1639         return 0;
1640 }
1641
1642 static void nbd_dbg_close(void)
1643 {
1644         debugfs_remove_recursive(nbd_dbg_dir);
1645 }
1646
1647 #else  /* IS_ENABLED(CONFIG_DEBUG_FS) */
1648
1649 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1650 {
1651         return 0;
1652 }
1653
1654 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1655 {
1656 }
1657
1658 static int nbd_dbg_init(void)
1659 {
1660         return 0;
1661 }
1662
1663 static void nbd_dbg_close(void)
1664 {
1665 }
1666
1667 #endif
1668
1669 static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1670                             unsigned int hctx_idx, unsigned int numa_node)
1671 {
1672         struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
1673         cmd->nbd = set->driver_data;
1674         cmd->flags = 0;
1675         mutex_init(&cmd->lock);
1676         return 0;
1677 }
1678
1679 static const struct blk_mq_ops nbd_mq_ops = {
1680         .queue_rq       = nbd_queue_rq,
1681         .complete       = nbd_complete_rq,
1682         .init_request   = nbd_init_request,
1683         .timeout        = nbd_xmit_timeout,
1684 };
1685
1686 static int nbd_dev_add(int index)
1687 {
1688         struct nbd_device *nbd;
1689         struct gendisk *disk;
1690         int err = -ENOMEM;
1691
1692         nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1693         if (!nbd)
1694                 goto out;
1695
1696         nbd->tag_set.ops = &nbd_mq_ops;
1697         nbd->tag_set.nr_hw_queues = 1;
1698         nbd->tag_set.queue_depth = 128;
1699         nbd->tag_set.numa_node = NUMA_NO_NODE;
1700         nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1701         nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1702                 BLK_MQ_F_BLOCKING;
1703         nbd->tag_set.driver_data = nbd;
1704         INIT_WORK(&nbd->remove_work, nbd_dev_remove_work);
1705         nbd->destroy_complete = NULL;
1706         nbd->backend = NULL;
1707
1708         err = blk_mq_alloc_tag_set(&nbd->tag_set);
1709         if (err)
1710                 goto out_free_nbd;
1711
1712         if (index >= 0) {
1713                 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1714                                 GFP_KERNEL);
1715                 if (err == -ENOSPC)
1716                         err = -EEXIST;
1717         } else {
1718                 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1719                 if (err >= 0)
1720                         index = err;
1721         }
1722         if (err < 0)
1723                 goto out_free_tags;
1724         nbd->index = index;
1725
1726         disk = blk_mq_alloc_disk(&nbd->tag_set, NULL);
1727         if (IS_ERR(disk)) {
1728                 err = PTR_ERR(disk);
1729                 goto out_free_idr;
1730         }
1731         nbd->disk = disk;
1732
1733         /*
1734          * Tell the block layer that we are not a rotational device
1735          */
1736         blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1737         blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1738         disk->queue->limits.discard_granularity = 0;
1739         disk->queue->limits.discard_alignment = 0;
1740         blk_queue_max_discard_sectors(disk->queue, 0);
1741         blk_queue_max_segment_size(disk->queue, UINT_MAX);
1742         blk_queue_max_segments(disk->queue, USHRT_MAX);
1743         blk_queue_max_hw_sectors(disk->queue, 65536);
1744         disk->queue->limits.max_sectors = 256;
1745
1746         mutex_init(&nbd->config_lock);
1747         refcount_set(&nbd->config_refs, 0);
1748         refcount_set(&nbd->refs, 1);
1749         INIT_LIST_HEAD(&nbd->list);
1750         disk->major = NBD_MAJOR;
1751         disk->first_minor = index << part_shift;
1752         disk->minors = 1 << part_shift;
1753         disk->fops = &nbd_fops;
1754         disk->private_data = nbd;
1755         sprintf(disk->disk_name, "nbd%d", index);
1756         add_disk(disk);
1757         nbd_total_devices++;
1758         return index;
1759
1760 out_free_idr:
1761         idr_remove(&nbd_index_idr, index);
1762 out_free_tags:
1763         blk_mq_free_tag_set(&nbd->tag_set);
1764 out_free_nbd:
1765         kfree(nbd);
1766 out:
1767         return err;
1768 }
1769
1770 static int find_free_cb(int id, void *ptr, void *data)
1771 {
1772         struct nbd_device *nbd = ptr;
1773         struct nbd_device **found = data;
1774
1775         if (!refcount_read(&nbd->config_refs)) {
1776                 *found = nbd;
1777                 return 1;
1778         }
1779         return 0;
1780 }
1781
1782 /* Netlink interface. */
1783 static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1784         [NBD_ATTR_INDEX]                =       { .type = NLA_U32 },
1785         [NBD_ATTR_SIZE_BYTES]           =       { .type = NLA_U64 },
1786         [NBD_ATTR_BLOCK_SIZE_BYTES]     =       { .type = NLA_U64 },
1787         [NBD_ATTR_TIMEOUT]              =       { .type = NLA_U64 },
1788         [NBD_ATTR_SERVER_FLAGS]         =       { .type = NLA_U64 },
1789         [NBD_ATTR_CLIENT_FLAGS]         =       { .type = NLA_U64 },
1790         [NBD_ATTR_SOCKETS]              =       { .type = NLA_NESTED},
1791         [NBD_ATTR_DEAD_CONN_TIMEOUT]    =       { .type = NLA_U64 },
1792         [NBD_ATTR_DEVICE_LIST]          =       { .type = NLA_NESTED},
1793         [NBD_ATTR_BACKEND_IDENTIFIER]   =       { .type = NLA_STRING},
1794 };
1795
1796 static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1797         [NBD_SOCK_FD]                   =       { .type = NLA_U32 },
1798 };
1799
1800 /* We don't use this right now since we don't parse the incoming list, but we
1801  * still want it here so userspace knows what to expect.
1802  */
1803 static const struct nla_policy __attribute__((unused))
1804 nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1805         [NBD_DEVICE_INDEX]              =       { .type = NLA_U32 },
1806         [NBD_DEVICE_CONNECTED]          =       { .type = NLA_U8 },
1807 };
1808
1809 static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
1810 {
1811         struct nbd_config *config = nbd->config;
1812         u64 bsize = config->blksize;
1813         u64 bytes = config->bytesize;
1814
1815         if (info->attrs[NBD_ATTR_SIZE_BYTES])
1816                 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1817
1818         if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES])
1819                 bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1820
1821         if (bytes != config->bytesize || bsize != config->blksize)
1822                 return nbd_set_size(nbd, bytes, bsize);
1823         return 0;
1824 }
1825
1826 static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1827 {
1828         DECLARE_COMPLETION_ONSTACK(destroy_complete);
1829         struct nbd_device *nbd = NULL;
1830         struct nbd_config *config;
1831         int index = -1;
1832         int ret;
1833         bool put_dev = false;
1834
1835         if (!netlink_capable(skb, CAP_SYS_ADMIN))
1836                 return -EPERM;
1837
1838         if (info->attrs[NBD_ATTR_INDEX])
1839                 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1840         if (!info->attrs[NBD_ATTR_SOCKETS]) {
1841                 printk(KERN_ERR "nbd: must specify at least one socket\n");
1842                 return -EINVAL;
1843         }
1844         if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1845                 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1846                 return -EINVAL;
1847         }
1848 again:
1849         mutex_lock(&nbd_index_mutex);
1850         if (index == -1) {
1851                 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1852                 if (ret == 0) {
1853                         int new_index;
1854                         new_index = nbd_dev_add(-1);
1855                         if (new_index < 0) {
1856                                 mutex_unlock(&nbd_index_mutex);
1857                                 printk(KERN_ERR "nbd: failed to add new device\n");
1858                                 return new_index;
1859                         }
1860                         nbd = idr_find(&nbd_index_idr, new_index);
1861                 }
1862         } else {
1863                 nbd = idr_find(&nbd_index_idr, index);
1864                 if (!nbd) {
1865                         ret = nbd_dev_add(index);
1866                         if (ret < 0) {
1867                                 mutex_unlock(&nbd_index_mutex);
1868                                 printk(KERN_ERR "nbd: failed to add new device\n");
1869                                 return ret;
1870                         }
1871                         nbd = idr_find(&nbd_index_idr, index);
1872                 }
1873         }
1874         if (!nbd) {
1875                 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1876                        index);
1877                 mutex_unlock(&nbd_index_mutex);
1878                 return -EINVAL;
1879         }
1880
1881         if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
1882             test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) {
1883                 nbd->destroy_complete = &destroy_complete;
1884                 mutex_unlock(&nbd_index_mutex);
1885
1886                 /* Wait untill the the nbd stuff is totally destroyed */
1887                 wait_for_completion(&destroy_complete);
1888                 goto again;
1889         }
1890
1891         if (!refcount_inc_not_zero(&nbd->refs)) {
1892                 mutex_unlock(&nbd_index_mutex);
1893                 if (index == -1)
1894                         goto again;
1895                 printk(KERN_ERR "nbd: device at index %d is going down\n",
1896                        index);
1897                 return -EINVAL;
1898         }
1899         mutex_unlock(&nbd_index_mutex);
1900
1901         mutex_lock(&nbd->config_lock);
1902         if (refcount_read(&nbd->config_refs)) {
1903                 mutex_unlock(&nbd->config_lock);
1904                 nbd_put(nbd);
1905                 if (index == -1)
1906                         goto again;
1907                 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1908                 return -EBUSY;
1909         }
1910         if (WARN_ON(nbd->config)) {
1911                 mutex_unlock(&nbd->config_lock);
1912                 nbd_put(nbd);
1913                 return -EINVAL;
1914         }
1915         config = nbd->config = nbd_alloc_config();
1916         if (!nbd->config) {
1917                 mutex_unlock(&nbd->config_lock);
1918                 nbd_put(nbd);
1919                 printk(KERN_ERR "nbd: couldn't allocate config\n");
1920                 return -ENOMEM;
1921         }
1922         refcount_set(&nbd->config_refs, 1);
1923         set_bit(NBD_RT_BOUND, &config->runtime_flags);
1924
1925         ret = nbd_genl_size_set(info, nbd);
1926         if (ret)
1927                 goto out;
1928
1929         if (info->attrs[NBD_ATTR_TIMEOUT])
1930                 nbd_set_cmd_timeout(nbd,
1931                                     nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
1932         if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1933                 config->dead_conn_timeout =
1934                         nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1935                 config->dead_conn_timeout *= HZ;
1936         }
1937         if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1938                 config->flags =
1939                         nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
1940         if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1941                 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1942                 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1943                         /*
1944                          * We have 1 ref to keep the device around, and then 1
1945                          * ref for our current operation here, which will be
1946                          * inherited by the config.  If we already have
1947                          * DESTROY_ON_DISCONNECT set then we know we don't have
1948                          * that extra ref already held so we don't need the
1949                          * put_dev.
1950                          */
1951                         if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1952                                               &nbd->flags))
1953                                 put_dev = true;
1954                 } else {
1955                         if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1956                                                &nbd->flags))
1957                                 refcount_inc(&nbd->refs);
1958                 }
1959                 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
1960                         set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
1961                                 &config->runtime_flags);
1962                 }
1963         }
1964
1965         if (info->attrs[NBD_ATTR_SOCKETS]) {
1966                 struct nlattr *attr;
1967                 int rem, fd;
1968
1969                 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1970                                     rem) {
1971                         struct nlattr *socks[NBD_SOCK_MAX+1];
1972
1973                         if (nla_type(attr) != NBD_SOCK_ITEM) {
1974                                 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1975                                 ret = -EINVAL;
1976                                 goto out;
1977                         }
1978                         ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
1979                                                           attr,
1980                                                           nbd_sock_policy,
1981                                                           info->extack);
1982                         if (ret != 0) {
1983                                 printk(KERN_ERR "nbd: error processing sock list\n");
1984                                 ret = -EINVAL;
1985                                 goto out;
1986                         }
1987                         if (!socks[NBD_SOCK_FD])
1988                                 continue;
1989                         fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1990                         ret = nbd_add_socket(nbd, fd, true);
1991                         if (ret)
1992                                 goto out;
1993                 }
1994         }
1995         ret = nbd_start_device(nbd);
1996         if (ret)
1997                 goto out;
1998         if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
1999                 nbd->backend = nla_strdup(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
2000                                           GFP_KERNEL);
2001                 if (!nbd->backend) {
2002                         ret = -ENOMEM;
2003                         goto out;
2004                 }
2005         }
2006         ret = device_create_file(disk_to_dev(nbd->disk), &backend_attr);
2007         if (ret) {
2008                 dev_err(disk_to_dev(nbd->disk),
2009                         "device_create_file failed for backend!\n");
2010                 goto out;
2011         }
2012         set_bit(NBD_RT_HAS_BACKEND_FILE, &config->runtime_flags);
2013 out:
2014         mutex_unlock(&nbd->config_lock);
2015         if (!ret) {
2016                 set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags);
2017                 refcount_inc(&nbd->config_refs);
2018                 nbd_connect_reply(info, nbd->index);
2019         }
2020         nbd_config_put(nbd);
2021         if (put_dev)
2022                 nbd_put(nbd);
2023         return ret;
2024 }
2025
2026 static void nbd_disconnect_and_put(struct nbd_device *nbd)
2027 {
2028         mutex_lock(&nbd->config_lock);
2029         nbd_disconnect(nbd);
2030         nbd_clear_sock(nbd);
2031         mutex_unlock(&nbd->config_lock);
2032         /*
2033          * Make sure recv thread has finished, so it does not drop the last
2034          * config ref and try to destroy the workqueue from inside the work
2035          * queue.
2036          */
2037         if (nbd->recv_workq)
2038                 flush_workqueue(nbd->recv_workq);
2039         if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
2040                                &nbd->config->runtime_flags))
2041                 nbd_config_put(nbd);
2042 }
2043
2044 static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
2045 {
2046         struct nbd_device *nbd;
2047         int index;
2048
2049         if (!netlink_capable(skb, CAP_SYS_ADMIN))
2050                 return -EPERM;
2051
2052         if (!info->attrs[NBD_ATTR_INDEX]) {
2053                 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
2054                 return -EINVAL;
2055         }
2056         index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2057         mutex_lock(&nbd_index_mutex);
2058         nbd = idr_find(&nbd_index_idr, index);
2059         if (!nbd) {
2060                 mutex_unlock(&nbd_index_mutex);
2061                 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
2062                        index);
2063                 return -EINVAL;
2064         }
2065         if (!refcount_inc_not_zero(&nbd->refs)) {
2066                 mutex_unlock(&nbd_index_mutex);
2067                 printk(KERN_ERR "nbd: device at index %d is going down\n",
2068                        index);
2069                 return -EINVAL;
2070         }
2071         mutex_unlock(&nbd_index_mutex);
2072         if (!refcount_inc_not_zero(&nbd->config_refs))
2073                 goto put_nbd;
2074         nbd_disconnect_and_put(nbd);
2075         nbd_config_put(nbd);
2076 put_nbd:
2077         nbd_put(nbd);
2078         return 0;
2079 }
2080
2081 static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
2082 {
2083         struct nbd_device *nbd = NULL;
2084         struct nbd_config *config;
2085         int index;
2086         int ret = 0;
2087         bool put_dev = false;
2088
2089         if (!netlink_capable(skb, CAP_SYS_ADMIN))
2090                 return -EPERM;
2091
2092         if (!info->attrs[NBD_ATTR_INDEX]) {
2093                 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
2094                 return -EINVAL;
2095         }
2096         index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2097         mutex_lock(&nbd_index_mutex);
2098         nbd = idr_find(&nbd_index_idr, index);
2099         if (!nbd) {
2100                 mutex_unlock(&nbd_index_mutex);
2101                 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
2102                        index);
2103                 return -EINVAL;
2104         }
2105         if (nbd->backend) {
2106                 if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
2107                         if (nla_strcmp(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
2108                                        nbd->backend)) {
2109                                 mutex_unlock(&nbd_index_mutex);
2110                                 dev_err(nbd_to_dev(nbd),
2111                                         "backend image doesn't match with %s\n",
2112                                         nbd->backend);
2113                                 return -EINVAL;
2114                         }
2115                 } else {
2116                         mutex_unlock(&nbd_index_mutex);
2117                         dev_err(nbd_to_dev(nbd), "must specify backend\n");
2118                         return -EINVAL;
2119                 }
2120         }
2121         if (!refcount_inc_not_zero(&nbd->refs)) {
2122                 mutex_unlock(&nbd_index_mutex);
2123                 printk(KERN_ERR "nbd: device at index %d is going down\n",
2124                        index);
2125                 return -EINVAL;
2126         }
2127         mutex_unlock(&nbd_index_mutex);
2128
2129         if (!refcount_inc_not_zero(&nbd->config_refs)) {
2130                 dev_err(nbd_to_dev(nbd),
2131                         "not configured, cannot reconfigure\n");
2132                 nbd_put(nbd);
2133                 return -EINVAL;
2134         }
2135
2136         mutex_lock(&nbd->config_lock);
2137         config = nbd->config;
2138         if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
2139             !nbd->task_recv) {
2140                 dev_err(nbd_to_dev(nbd),
2141                         "not configured, cannot reconfigure\n");
2142                 ret = -EINVAL;
2143                 goto out;
2144         }
2145
2146         ret = nbd_genl_size_set(info, nbd);
2147         if (ret)
2148                 goto out;
2149
2150         if (info->attrs[NBD_ATTR_TIMEOUT])
2151                 nbd_set_cmd_timeout(nbd,
2152                                     nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
2153         if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
2154                 config->dead_conn_timeout =
2155                         nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
2156                 config->dead_conn_timeout *= HZ;
2157         }
2158         if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
2159                 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
2160                 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
2161                         if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
2162                                               &nbd->flags))
2163                                 put_dev = true;
2164                 } else {
2165                         if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
2166                                                &nbd->flags))
2167                                 refcount_inc(&nbd->refs);
2168                 }
2169
2170                 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
2171                         set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
2172                                         &config->runtime_flags);
2173                 } else {
2174                         clear_bit(NBD_RT_DISCONNECT_ON_CLOSE,
2175                                         &config->runtime_flags);
2176                 }
2177         }
2178
2179         if (info->attrs[NBD_ATTR_SOCKETS]) {
2180                 struct nlattr *attr;
2181                 int rem, fd;
2182
2183                 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2184                                     rem) {
2185                         struct nlattr *socks[NBD_SOCK_MAX+1];
2186
2187                         if (nla_type(attr) != NBD_SOCK_ITEM) {
2188                                 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
2189                                 ret = -EINVAL;
2190                                 goto out;
2191                         }
2192                         ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2193                                                           attr,
2194                                                           nbd_sock_policy,
2195                                                           info->extack);
2196                         if (ret != 0) {
2197                                 printk(KERN_ERR "nbd: error processing sock list\n");
2198                                 ret = -EINVAL;
2199                                 goto out;
2200                         }
2201                         if (!socks[NBD_SOCK_FD])
2202                                 continue;
2203                         fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2204                         ret = nbd_reconnect_socket(nbd, fd);
2205                         if (ret) {
2206                                 if (ret == -ENOSPC)
2207                                         ret = 0;
2208                                 goto out;
2209                         }
2210                         dev_info(nbd_to_dev(nbd), "reconnected socket\n");
2211                 }
2212         }
2213 out:
2214         mutex_unlock(&nbd->config_lock);
2215         nbd_config_put(nbd);
2216         nbd_put(nbd);
2217         if (put_dev)
2218                 nbd_put(nbd);
2219         return ret;
2220 }
2221
2222 static const struct genl_small_ops nbd_connect_genl_ops[] = {
2223         {
2224                 .cmd    = NBD_CMD_CONNECT,
2225                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2226                 .doit   = nbd_genl_connect,
2227         },
2228         {
2229                 .cmd    = NBD_CMD_DISCONNECT,
2230                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2231                 .doit   = nbd_genl_disconnect,
2232         },
2233         {
2234                 .cmd    = NBD_CMD_RECONFIGURE,
2235                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2236                 .doit   = nbd_genl_reconfigure,
2237         },
2238         {
2239                 .cmd    = NBD_CMD_STATUS,
2240                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2241                 .doit   = nbd_genl_status,
2242         },
2243 };
2244
2245 static const struct genl_multicast_group nbd_mcast_grps[] = {
2246         { .name = NBD_GENL_MCAST_GROUP_NAME, },
2247 };
2248
2249 static struct genl_family nbd_genl_family __ro_after_init = {
2250         .hdrsize        = 0,
2251         .name           = NBD_GENL_FAMILY_NAME,
2252         .version        = NBD_GENL_VERSION,
2253         .module         = THIS_MODULE,
2254         .small_ops      = nbd_connect_genl_ops,
2255         .n_small_ops    = ARRAY_SIZE(nbd_connect_genl_ops),
2256         .maxattr        = NBD_ATTR_MAX,
2257         .policy = nbd_attr_policy,
2258         .mcgrps         = nbd_mcast_grps,
2259         .n_mcgrps       = ARRAY_SIZE(nbd_mcast_grps),
2260 };
2261
2262 static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2263 {
2264         struct nlattr *dev_opt;
2265         u8 connected = 0;
2266         int ret;
2267
2268         /* This is a little racey, but for status it's ok.  The
2269          * reason we don't take a ref here is because we can't
2270          * take a ref in the index == -1 case as we would need
2271          * to put under the nbd_index_mutex, which could
2272          * deadlock if we are configured to remove ourselves
2273          * once we're disconnected.
2274          */
2275         if (refcount_read(&nbd->config_refs))
2276                 connected = 1;
2277         dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM);
2278         if (!dev_opt)
2279                 return -EMSGSIZE;
2280         ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2281         if (ret)
2282                 return -EMSGSIZE;
2283         ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2284                          connected);
2285         if (ret)
2286                 return -EMSGSIZE;
2287         nla_nest_end(reply, dev_opt);
2288         return 0;
2289 }
2290
2291 static int status_cb(int id, void *ptr, void *data)
2292 {
2293         struct nbd_device *nbd = ptr;
2294         return populate_nbd_status(nbd, (struct sk_buff *)data);
2295 }
2296
2297 static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2298 {
2299         struct nlattr *dev_list;
2300         struct sk_buff *reply;
2301         void *reply_head;
2302         size_t msg_size;
2303         int index = -1;
2304         int ret = -ENOMEM;
2305
2306         if (info->attrs[NBD_ATTR_INDEX])
2307                 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2308
2309         mutex_lock(&nbd_index_mutex);
2310
2311         msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2312                                   nla_attr_size(sizeof(u8)));
2313         msg_size *= (index == -1) ? nbd_total_devices : 1;
2314
2315         reply = genlmsg_new(msg_size, GFP_KERNEL);
2316         if (!reply)
2317                 goto out;
2318         reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2319                                        NBD_CMD_STATUS);
2320         if (!reply_head) {
2321                 nlmsg_free(reply);
2322                 goto out;
2323         }
2324
2325         dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST);
2326         if (index == -1) {
2327                 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2328                 if (ret) {
2329                         nlmsg_free(reply);
2330                         goto out;
2331                 }
2332         } else {
2333                 struct nbd_device *nbd;
2334                 nbd = idr_find(&nbd_index_idr, index);
2335                 if (nbd) {
2336                         ret = populate_nbd_status(nbd, reply);
2337                         if (ret) {
2338                                 nlmsg_free(reply);
2339                                 goto out;
2340                         }
2341                 }
2342         }
2343         nla_nest_end(reply, dev_list);
2344         genlmsg_end(reply, reply_head);
2345         ret = genlmsg_reply(reply, info);
2346 out:
2347         mutex_unlock(&nbd_index_mutex);
2348         return ret;
2349 }
2350
2351 static void nbd_connect_reply(struct genl_info *info, int index)
2352 {
2353         struct sk_buff *skb;
2354         void *msg_head;
2355         int ret;
2356
2357         skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2358         if (!skb)
2359                 return;
2360         msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2361                                      NBD_CMD_CONNECT);
2362         if (!msg_head) {
2363                 nlmsg_free(skb);
2364                 return;
2365         }
2366         ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2367         if (ret) {
2368                 nlmsg_free(skb);
2369                 return;
2370         }
2371         genlmsg_end(skb, msg_head);
2372         genlmsg_reply(skb, info);
2373 }
2374
2375 static void nbd_mcast_index(int index)
2376 {
2377         struct sk_buff *skb;
2378         void *msg_head;
2379         int ret;
2380
2381         skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2382         if (!skb)
2383                 return;
2384         msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2385                                      NBD_CMD_LINK_DEAD);
2386         if (!msg_head) {
2387                 nlmsg_free(skb);
2388                 return;
2389         }
2390         ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2391         if (ret) {
2392                 nlmsg_free(skb);
2393                 return;
2394         }
2395         genlmsg_end(skb, msg_head);
2396         genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2397 }
2398
2399 static void nbd_dead_link_work(struct work_struct *work)
2400 {
2401         struct link_dead_args *args = container_of(work, struct link_dead_args,
2402                                                    work);
2403         nbd_mcast_index(args->index);
2404         kfree(args);
2405 }
2406
2407 static int __init nbd_init(void)
2408 {
2409         int i;
2410
2411         BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
2412
2413         if (max_part < 0) {
2414                 printk(KERN_ERR "nbd: max_part must be >= 0\n");
2415                 return -EINVAL;
2416         }
2417
2418         part_shift = 0;
2419         if (max_part > 0) {
2420                 part_shift = fls(max_part);
2421
2422                 /*
2423                  * Adjust max_part according to part_shift as it is exported
2424                  * to user space so that user can know the max number of
2425                  * partition kernel should be able to manage.
2426                  *
2427                  * Note that -1 is required because partition 0 is reserved
2428                  * for the whole disk.
2429                  */
2430                 max_part = (1UL << part_shift) - 1;
2431         }
2432
2433         if ((1UL << part_shift) > DISK_MAX_PARTS)
2434                 return -EINVAL;
2435
2436         if (nbds_max > 1UL << (MINORBITS - part_shift))
2437                 return -EINVAL;
2438
2439         if (register_blkdev(NBD_MAJOR, "nbd"))
2440                 return -EIO;
2441
2442         nbd_del_wq = alloc_workqueue("nbd-del", WQ_UNBOUND, 0);
2443         if (!nbd_del_wq) {
2444                 unregister_blkdev(NBD_MAJOR, "nbd");
2445                 return -ENOMEM;
2446         }
2447
2448         if (genl_register_family(&nbd_genl_family)) {
2449                 destroy_workqueue(nbd_del_wq);
2450                 unregister_blkdev(NBD_MAJOR, "nbd");
2451                 return -EINVAL;
2452         }
2453         nbd_dbg_init();
2454
2455         mutex_lock(&nbd_index_mutex);
2456         for (i = 0; i < nbds_max; i++)
2457                 nbd_dev_add(i);
2458         mutex_unlock(&nbd_index_mutex);
2459         return 0;
2460 }
2461
2462 static int nbd_exit_cb(int id, void *ptr, void *data)
2463 {
2464         struct list_head *list = (struct list_head *)data;
2465         struct nbd_device *nbd = ptr;
2466
2467         /* Skip nbd that is being removed asynchronously */
2468         if (refcount_read(&nbd->refs))
2469                 list_add_tail(&nbd->list, list);
2470
2471         return 0;
2472 }
2473
2474 static void __exit nbd_cleanup(void)
2475 {
2476         struct nbd_device *nbd;
2477         LIST_HEAD(del_list);
2478
2479         nbd_dbg_close();
2480
2481         mutex_lock(&nbd_index_mutex);
2482         idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2483         mutex_unlock(&nbd_index_mutex);
2484
2485         while (!list_empty(&del_list)) {
2486                 nbd = list_first_entry(&del_list, struct nbd_device, list);
2487                 list_del_init(&nbd->list);
2488                 if (refcount_read(&nbd->refs) != 1)
2489                         printk(KERN_ERR "nbd: possibly leaking a device\n");
2490                 nbd_put(nbd);
2491         }
2492
2493         /* Also wait for nbd_dev_remove_work() completes */
2494         destroy_workqueue(nbd_del_wq);
2495
2496         idr_destroy(&nbd_index_idr);
2497         genl_unregister_family(&nbd_genl_family);
2498         unregister_blkdev(NBD_MAJOR, "nbd");
2499 }
2500
2501 module_init(nbd_init);
2502 module_exit(nbd_cleanup);
2503
2504 MODULE_DESCRIPTION("Network Block Device");
2505 MODULE_LICENSE("GPL");
2506
2507 module_param(nbds_max, int, 0444);
2508 MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2509 module_param(max_part, int, 0444);
2510 MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");