Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Network block device - make block devices work over TCP |
| 3 | * |
| 4 | * Note that you can not swap over this thing, yet. Seems to work but |
| 5 | * deadlocks sometimes - you can not swap over TCP in general. |
| 6 | * |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 7 | * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com> |
| 9 | * |
Pavel Machek | dbf492d | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 10 | * This file is released under GPLv2 or later. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Pavel Machek | dbf492d | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 12 | * (part of code stolen from loop.c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/major.h> |
| 16 | |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/sched.h> |
| 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> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 28 | #include <linux/compiler.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/sock.h> |
Trond Myklebust | 91cf45f | 2007-11-12 18:10:39 -0800 | [diff] [blame] | 33 | #include <linux/net.h> |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 34 | #include <linux/kthread.h> |
Markus Pargmann | b9c495b | 2015-04-02 10:11:37 +0200 | [diff] [blame] | 35 | #include <linux/types.h> |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 36 | #include <linux/debugfs.h> |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 37 | #include <linux/blk-mq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 39 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/types.h> |
| 41 | |
| 42 | #include <linux/nbd.h> |
| 43 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 44 | static DEFINE_IDR(nbd_index_idr); |
| 45 | static DEFINE_MUTEX(nbd_index_mutex); |
| 46 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 47 | struct nbd_sock { |
| 48 | struct socket *sock; |
| 49 | struct mutex tx_lock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 50 | struct request *pending; |
| 51 | int sent; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 54 | #define NBD_TIMEDOUT 0 |
| 55 | #define NBD_DISCONNECT_REQUESTED 1 |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 56 | #define NBD_DISCONNECTED 2 |
| 57 | #define NBD_RUNNING 3 |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 58 | |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 59 | struct nbd_device { |
Markus Pargmann | 22d109c | 2015-08-17 08:20:09 +0200 | [diff] [blame] | 60 | u32 flags; |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 61 | unsigned long runtime_flags; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 62 | struct nbd_sock **socks; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 63 | int magic; |
| 64 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 65 | struct blk_mq_tag_set tag_set; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 66 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 67 | struct mutex config_lock; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 68 | struct gendisk *disk; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 69 | int num_connections; |
| 70 | atomic_t recv_threads; |
| 71 | wait_queue_head_t recv_wq; |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 72 | loff_t blksize; |
Markus Pargmann | b9c495b | 2015-04-02 10:11:37 +0200 | [diff] [blame] | 73 | loff_t bytesize; |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 74 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 75 | struct task_struct *task_recv; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 76 | struct task_struct *task_setup; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 77 | |
| 78 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 79 | struct dentry *dbg_dir; |
| 80 | #endif |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 83 | struct nbd_cmd { |
| 84 | struct nbd_device *nbd; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 85 | struct completion send_complete; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 88 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 89 | static struct dentry *nbd_dbg_dir; |
| 90 | #endif |
| 91 | |
| 92 | #define nbd_name(nbd) ((nbd)->disk->disk_name) |
| 93 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 94 | #define NBD_MAGIC 0x68797548 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Ingo van Lil | 9c7a416 | 2006-07-01 04:36:36 -0700 | [diff] [blame] | 96 | static unsigned int nbds_max = 16; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 97 | static int max_part; |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 98 | static struct workqueue_struct *recv_workqueue; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 99 | static int part_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 101 | static int nbd_dev_dbg_init(struct nbd_device *nbd); |
| 102 | static void nbd_dev_dbg_close(struct nbd_device *nbd); |
| 103 | |
| 104 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 105 | static inline struct device *nbd_to_dev(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 107 | return disk_to_dev(nbd->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 110 | static bool nbd_is_connected(struct nbd_device *nbd) |
| 111 | { |
| 112 | return !!nbd->task_recv; |
| 113 | } |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | static const char *nbdcmd_to_ascii(int cmd) |
| 116 | { |
| 117 | switch (cmd) { |
| 118 | case NBD_CMD_READ: return "read"; |
| 119 | case NBD_CMD_WRITE: return "write"; |
| 120 | case NBD_CMD_DISC: return "disconnect"; |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 121 | case NBD_CMD_FLUSH: return "flush"; |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 122 | case NBD_CMD_TRIM: return "trim/discard"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | return "invalid"; |
| 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 127 | static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev) |
| 128 | { |
Ratna Manoj Bolla | abbbdf1 | 2017-03-24 14:08:29 -0400 | [diff] [blame] | 129 | if (bdev->bd_openers <= 1) |
| 130 | bd_set_size(bdev, 0); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 131 | set_capacity(nbd->disk, 0); |
| 132 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev) |
| 138 | { |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 139 | blk_queue_logical_block_size(nbd->disk->queue, nbd->blksize); |
| 140 | blk_queue_physical_block_size(nbd->disk->queue, nbd->blksize); |
| 141 | bd_set_size(bdev, nbd->bytesize); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 142 | set_capacity(nbd->disk, nbd->bytesize >> 9); |
| 143 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 144 | } |
| 145 | |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 146 | static void nbd_size_set(struct nbd_device *nbd, struct block_device *bdev, |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 147 | loff_t blocksize, loff_t nr_blocks) |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 148 | { |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 149 | nbd->blksize = blocksize; |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 150 | nbd->bytesize = blocksize * nr_blocks; |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 151 | if (nbd_is_connected(nbd)) |
| 152 | nbd_size_update(nbd, bdev); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 155 | static void nbd_end_request(struct nbd_cmd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 157 | struct nbd_device *nbd = cmd->nbd; |
| 158 | struct request *req = blk_mq_rq_from_pdu(cmd); |
Kiyoshi Ueda | 097c94a | 2007-12-11 17:44:06 -0500 | [diff] [blame] | 159 | int error = req->errors ? -EIO : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 161 | dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", cmd, |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 162 | error ? "failed" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 164 | blk_mq_complete_request(req, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Markus Pargmann | e018e75 | 2015-04-02 10:11:39 +0200 | [diff] [blame] | 167 | /* |
| 168 | * Forcibly shutdown the socket causing all listeners to error |
| 169 | */ |
Markus Pargmann | 36e47be | 2015-08-17 08:20:01 +0200 | [diff] [blame] | 170 | static void sock_shutdown(struct nbd_device *nbd) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 171 | { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 172 | int i; |
Josef Bacik | c261189 | 2016-09-08 12:33:38 -0700 | [diff] [blame] | 173 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 174 | if (nbd->num_connections == 0) |
Markus Pargmann | 260bbce | 2015-08-17 08:20:02 +0200 | [diff] [blame] | 175 | return; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 176 | if (test_and_set_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) |
| 177 | return; |
| 178 | |
| 179 | for (i = 0; i < nbd->num_connections; i++) { |
| 180 | struct nbd_sock *nsock = nbd->socks[i]; |
| 181 | mutex_lock(&nsock->tx_lock); |
| 182 | kernel_sock_shutdown(nsock->sock, SHUT_RDWR); |
| 183 | mutex_unlock(&nsock->tx_lock); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 184 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 185 | dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n"); |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 188 | static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req, |
| 189 | bool reserved) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 190 | { |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 191 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req); |
| 192 | struct nbd_device *nbd = cmd->nbd; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 193 | |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 194 | dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n"); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 195 | set_bit(NBD_TIMEDOUT, &nbd->runtime_flags); |
Josef Bacik | c103b4d | 2017-03-24 14:08:27 -0400 | [diff] [blame] | 196 | req->errors = -EIO; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 197 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 198 | mutex_lock(&nbd->config_lock); |
| 199 | sock_shutdown(nbd); |
| 200 | mutex_unlock(&nbd->config_lock); |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 201 | return BLK_EH_HANDLED; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* |
| 205 | * Send or receive packet. |
| 206 | */ |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 207 | static int sock_xmit(struct nbd_device *nbd, int index, int send, |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 208 | struct iov_iter *iter, int msg_flags, int *sent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 210 | struct socket *sock = nbd->socks[index]->sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | int result; |
| 212 | struct msghdr msg; |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 213 | unsigned long pflags = current->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 215 | if (unlikely(!sock)) { |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 216 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 217 | "Attempted %s on closed socket in sock_xmit\n", |
| 218 | (send ? "send" : "recv")); |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 219 | return -EINVAL; |
| 220 | } |
| 221 | |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 222 | msg.msg_iter = *iter; |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 223 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 224 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | do { |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 226 | sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | msg.msg_name = NULL; |
| 228 | msg.msg_namelen = 0; |
| 229 | msg.msg_control = NULL; |
| 230 | msg.msg_controllen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | msg.msg_flags = msg_flags | MSG_NOSIGNAL; |
| 232 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 233 | if (send) |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 234 | result = sock_sendmsg(sock, &msg); |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 235 | else |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 236 | result = sock_recvmsg(sock, &msg, msg.msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (result <= 0) { |
| 239 | if (result == 0) |
| 240 | result = -EPIPE; /* short read */ |
| 241 | break; |
| 242 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 243 | if (sent) |
| 244 | *sent += result; |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 245 | } while (msg_data_left(&msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 247 | tsk_restore_flags(current, pflags, PF_MEMALLOC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | return result; |
| 250 | } |
| 251 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 252 | /* always call with the tx_lock held */ |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 253 | static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 255 | struct request *req = blk_mq_rq_from_pdu(cmd); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 256 | struct nbd_sock *nsock = nbd->socks[index]; |
Josef Bacik | d61b7f9 | 2017-01-19 16:08:49 -0500 | [diff] [blame] | 257 | int result; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 258 | struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)}; |
| 259 | struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)}; |
| 260 | struct iov_iter from; |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 261 | unsigned long size = blk_rq_bytes(req); |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 262 | struct bio *bio; |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 263 | u32 type; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 264 | u32 tag = blk_mq_unique_tag(req); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 265 | int sent = nsock->sent, skip = 0; |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 266 | |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 267 | iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request)); |
| 268 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 269 | switch (req_op(req)) { |
| 270 | case REQ_OP_DISCARD: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 271 | type = NBD_CMD_TRIM; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 272 | break; |
| 273 | case REQ_OP_FLUSH: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 274 | type = NBD_CMD_FLUSH; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 275 | break; |
| 276 | case REQ_OP_WRITE: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 277 | type = NBD_CMD_WRITE; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 278 | break; |
| 279 | case REQ_OP_READ: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 280 | type = NBD_CMD_READ; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 281 | break; |
| 282 | default: |
| 283 | return -EIO; |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Christoph Hellwig | 09fc54c | 2017-01-31 16:57:28 +0100 | [diff] [blame] | 286 | if (rq_data_dir(req) == WRITE && |
| 287 | (nbd->flags & NBD_FLAG_READ_ONLY)) { |
| 288 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 289 | "Write on read-only\n"); |
| 290 | return -EIO; |
| 291 | } |
| 292 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 293 | /* We did a partial send previously, and we at least sent the whole |
| 294 | * request struct, so just go and send the rest of the pages in the |
| 295 | * request. |
| 296 | */ |
| 297 | if (sent) { |
| 298 | if (sent >= sizeof(request)) { |
| 299 | skip = sent - sizeof(request); |
| 300 | goto send_pages; |
| 301 | } |
| 302 | iov_iter_advance(&from, sent); |
| 303 | } |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 304 | request.type = htonl(type); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 305 | if (type != NBD_CMD_FLUSH) { |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 306 | request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9); |
| 307 | request.len = htonl(size); |
| 308 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 309 | memcpy(request.handle, &tag, sizeof(tag)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 311 | dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n", |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 312 | cmd, nbdcmd_to_ascii(type), |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 313 | (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 314 | result = sock_xmit(nbd, index, 1, &from, |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 315 | (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (result <= 0) { |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 317 | if (result == -ERESTARTSYS) { |
| 318 | /* If we havne't sent anything we can just return BUSY, |
| 319 | * however if we have sent something we need to make |
| 320 | * sure we only allow this req to be sent until we are |
| 321 | * completely done. |
| 322 | */ |
| 323 | if (sent) { |
| 324 | nsock->pending = req; |
| 325 | nsock->sent = sent; |
| 326 | } |
| 327 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 328 | } |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 329 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 330 | "Send control failed (result %d)\n", result); |
Markus Pargmann | dab5313 | 2015-04-02 10:11:40 +0200 | [diff] [blame] | 331 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 333 | send_pages: |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 334 | if (type != NBD_CMD_WRITE) |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 335 | goto out; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 336 | |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 337 | bio = req->bio; |
| 338 | while (bio) { |
| 339 | struct bio *next = bio->bi_next; |
| 340 | struct bvec_iter iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 341 | struct bio_vec bvec; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 342 | |
| 343 | bio_for_each_segment(bvec, bio, iter) { |
| 344 | bool is_last = !next && bio_iter_last(bvec, iter); |
Josef Bacik | d61b7f9 | 2017-01-19 16:08:49 -0500 | [diff] [blame] | 345 | int flags = is_last ? 0 : MSG_MORE; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 346 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 347 | dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n", |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 348 | cmd, bvec.bv_len); |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 349 | iov_iter_bvec(&from, ITER_BVEC | WRITE, |
| 350 | &bvec, 1, bvec.bv_len); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 351 | if (skip) { |
| 352 | if (skip >= iov_iter_count(&from)) { |
| 353 | skip -= iov_iter_count(&from); |
| 354 | continue; |
| 355 | } |
| 356 | iov_iter_advance(&from, skip); |
| 357 | skip = 0; |
| 358 | } |
| 359 | result = sock_xmit(nbd, index, 1, &from, flags, &sent); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 360 | if (result <= 0) { |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 361 | if (result == -ERESTARTSYS) { |
| 362 | /* We've already sent the header, we |
| 363 | * have no choice but to set pending and |
| 364 | * return BUSY. |
| 365 | */ |
| 366 | nsock->pending = req; |
| 367 | nsock->sent = sent; |
| 368 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 369 | } |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 370 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 371 | "Send data failed (result %d)\n", |
| 372 | result); |
Markus Pargmann | dab5313 | 2015-04-02 10:11:40 +0200 | [diff] [blame] | 373 | return -EIO; |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 374 | } |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 375 | /* |
| 376 | * The completion might already have come in, |
| 377 | * so break for the last one instead of letting |
| 378 | * the iterator do it. This prevents use-after-free |
| 379 | * of the bio. |
| 380 | */ |
| 381 | if (is_last) |
| 382 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 384 | bio = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 386 | out: |
| 387 | nsock->pending = NULL; |
| 388 | nsock->sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | /* NULL returned = something went wrong, inform userspace */ |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 393 | static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | int result; |
| 396 | struct nbd_reply reply; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 397 | struct nbd_cmd *cmd; |
| 398 | struct request *req = NULL; |
| 399 | u16 hwq; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 400 | u32 tag; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 401 | struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)}; |
| 402 | struct iov_iter to; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | reply.magic = 0; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 405 | iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply)); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 406 | result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | if (result <= 0) { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 408 | if (!test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) && |
| 409 | !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags)) |
| 410 | dev_err(disk_to_dev(nbd->disk), |
| 411 | "Receive control failed (result %d)\n", result); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 412 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 414 | |
| 415 | if (ntohl(reply.magic) != NBD_REPLY_MAGIC) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 416 | dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n", |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 417 | (unsigned long)ntohl(reply.magic)); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 418 | return ERR_PTR(-EPROTO); |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 421 | memcpy(&tag, reply.handle, sizeof(u32)); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 422 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 423 | hwq = blk_mq_unique_tag_to_hwq(tag); |
| 424 | if (hwq < nbd->tag_set.nr_hw_queues) |
| 425 | req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq], |
| 426 | blk_mq_unique_tag_to_tag(tag)); |
| 427 | if (!req || !blk_mq_request_started(req)) { |
| 428 | dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n", |
| 429 | tag, req); |
| 430 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 432 | cmd = blk_mq_rq_to_pdu(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | if (ntohl(reply.error)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 434 | dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n", |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 435 | ntohl(reply.error)); |
Josef Bacik | c103b4d | 2017-03-24 14:08:27 -0400 | [diff] [blame] | 436 | req->errors = -EIO; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 437 | return cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 440 | dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd); |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 441 | if (rq_data_dir(req) != WRITE) { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 442 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 443 | struct bio_vec bvec; |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 444 | |
| 445 | rq_for_each_segment(bvec, req, iter) { |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 446 | iov_iter_bvec(&to, ITER_BVEC | READ, |
| 447 | &bvec, 1, bvec.bv_len); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 448 | result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 449 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 450 | dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n", |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 451 | result); |
Josef Bacik | c103b4d | 2017-03-24 14:08:27 -0400 | [diff] [blame] | 452 | req->errors = -EIO; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 453 | return cmd; |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 454 | } |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 455 | dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n", |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 456 | cmd, bvec.bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 458 | } else { |
| 459 | /* See the comment in nbd_queue_rq. */ |
| 460 | wait_for_completion(&cmd->send_complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 462 | return cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 465 | static ssize_t pid_show(struct device *dev, |
| 466 | struct device_attribute *attr, char *buf) |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 467 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 468 | struct gendisk *disk = dev_to_disk(dev); |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 469 | struct nbd_device *nbd = (struct nbd_device *)disk->private_data; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 470 | |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 471 | return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv)); |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 474 | static struct device_attribute pid_attr = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 475 | .attr = { .name = "pid", .mode = S_IRUGO}, |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 476 | .show = pid_show, |
| 477 | }; |
| 478 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 479 | struct recv_thread_args { |
| 480 | struct work_struct work; |
| 481 | struct nbd_device *nbd; |
| 482 | int index; |
| 483 | }; |
| 484 | |
| 485 | static void recv_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 487 | struct recv_thread_args *args = container_of(work, |
| 488 | struct recv_thread_args, |
| 489 | work); |
| 490 | struct nbd_device *nbd = args->nbd; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 491 | struct nbd_cmd *cmd; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 492 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 494 | BUG_ON(nbd->magic != NBD_MAGIC); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 495 | while (1) { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 496 | cmd = nbd_read_stat(nbd, args->index); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 497 | if (IS_ERR(cmd)) { |
| 498 | ret = PTR_ERR(cmd); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 502 | nbd_end_request(cmd); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 503 | } |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 504 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 505 | /* |
| 506 | * We got an error, shut everybody down if this wasn't the result of a |
| 507 | * disconnect request. |
| 508 | */ |
| 509 | if (ret && !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags)) |
| 510 | sock_shutdown(nbd); |
| 511 | atomic_dec(&nbd->recv_threads); |
| 512 | wake_up(&nbd->recv_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 515 | static void nbd_clear_req(struct request *req, void *data, bool reserved) |
| 516 | { |
| 517 | struct nbd_cmd *cmd; |
| 518 | |
| 519 | if (!blk_mq_request_started(req)) |
| 520 | return; |
| 521 | cmd = blk_mq_rq_to_pdu(req); |
Josef Bacik | c103b4d | 2017-03-24 14:08:27 -0400 | [diff] [blame] | 522 | req->errors = -EIO; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 523 | nbd_end_request(cmd); |
| 524 | } |
| 525 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 526 | static void nbd_clear_que(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 528 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 530 | blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL); |
Markus Pargmann | e78273c | 2015-08-17 08:20:04 +0200 | [diff] [blame] | 531 | dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 534 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 535 | static int nbd_handle_cmd(struct nbd_cmd *cmd, int index) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 536 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 537 | struct request *req = blk_mq_rq_from_pdu(cmd); |
| 538 | struct nbd_device *nbd = cmd->nbd; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 539 | struct nbd_sock *nsock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 540 | int ret; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 541 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 542 | if (index >= nbd->num_connections) { |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 543 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 544 | "Attempted send on invalid socket\n"); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 545 | return -EINVAL; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) { |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 549 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 550 | "Attempted send on closed socket\n"); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 551 | return -EINVAL; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 552 | } |
| 553 | |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 554 | req->errors = 0; |
| 555 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 556 | nsock = nbd->socks[index]; |
| 557 | mutex_lock(&nsock->tx_lock); |
| 558 | if (unlikely(!nsock->sock)) { |
| 559 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 560 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 561 | "Attempted send on closed socket\n"); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 562 | return -EINVAL; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 565 | /* Handle the case that we have a pending request that was partially |
| 566 | * transmitted that _has_ to be serviced first. We need to call requeue |
| 567 | * here so that it gets put _after_ the request that is already on the |
| 568 | * dispatch list. |
| 569 | */ |
| 570 | if (unlikely(nsock->pending && nsock->pending != req)) { |
| 571 | blk_mq_requeue_request(req, true); |
| 572 | ret = 0; |
| 573 | goto out; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 574 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 575 | ret = nbd_send_cmd(nbd, cmd, index); |
| 576 | out: |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 577 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 578 | return ret; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 581 | static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 582 | const struct blk_mq_queue_data *bd) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 583 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 584 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 585 | int ret; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 586 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 587 | /* |
| 588 | * Since we look at the bio's to send the request over the network we |
| 589 | * need to make sure the completion work doesn't mark this request done |
| 590 | * before we are done doing our send. This keeps us from dereferencing |
| 591 | * freed data if we have particularly fast completions (ie we get the |
| 592 | * completion before we exit sock_xmit on the last bvec) or in the case |
| 593 | * that the server is misbehaving (or there was an error) before we're |
| 594 | * done sending everything over the wire. |
| 595 | */ |
| 596 | init_completion(&cmd->send_complete); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 597 | blk_mq_start_request(bd->rq); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 598 | |
| 599 | /* We can be called directly from the user space process, which means we |
| 600 | * could possibly have signals pending so our sendmsg will fail. In |
| 601 | * this case we need to return that we are busy, otherwise error out as |
| 602 | * appropriate. |
| 603 | */ |
| 604 | ret = nbd_handle_cmd(cmd, hctx->queue_num); |
| 605 | if (ret < 0) |
| 606 | ret = BLK_MQ_RQ_QUEUE_ERROR; |
| 607 | if (!ret) |
| 608 | ret = BLK_MQ_RQ_QUEUE_OK; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 609 | complete(&cmd->send_complete); |
| 610 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 611 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 614 | static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev, |
| 615 | unsigned long arg) |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 616 | { |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 617 | struct socket *sock; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 618 | struct nbd_sock **socks; |
| 619 | struct nbd_sock *nsock; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 620 | int err; |
| 621 | |
| 622 | sock = sockfd_lookup(arg, &err); |
| 623 | if (!sock) |
| 624 | return err; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 625 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 626 | if (!nbd->task_setup) |
| 627 | nbd->task_setup = current; |
| 628 | if (nbd->task_setup != current) { |
| 629 | dev_err(disk_to_dev(nbd->disk), |
| 630 | "Device being setup by another task"); |
| 631 | return -EINVAL; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 634 | socks = krealloc(nbd->socks, (nbd->num_connections + 1) * |
| 635 | sizeof(struct nbd_sock *), GFP_KERNEL); |
| 636 | if (!socks) |
| 637 | return -ENOMEM; |
| 638 | nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL); |
| 639 | if (!nsock) |
| 640 | return -ENOMEM; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 641 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 642 | nbd->socks = socks; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 643 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 644 | mutex_init(&nsock->tx_lock); |
| 645 | nsock->sock = sock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 646 | nsock->pending = NULL; |
| 647 | nsock->sent = 0; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 648 | socks[nbd->num_connections++] = nsock; |
| 649 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 650 | if (max_part) |
| 651 | bdev->bd_invalidated = 1; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 652 | return 0; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 655 | /* Reset all properties of an NBD device */ |
| 656 | static void nbd_reset(struct nbd_device *nbd) |
| 657 | { |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 658 | nbd->runtime_flags = 0; |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 659 | nbd->blksize = 1024; |
| 660 | nbd->bytesize = 0; |
| 661 | set_capacity(nbd->disk, 0); |
| 662 | nbd->flags = 0; |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 663 | nbd->tag_set.timeout = 0; |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 664 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static void nbd_bdev_reset(struct block_device *bdev) |
| 668 | { |
Ratna Manoj Bolla | abbbdf1 | 2017-03-24 14:08:29 -0400 | [diff] [blame] | 669 | if (bdev->bd_openers > 1) |
| 670 | return; |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 671 | set_device_ro(bdev, false); |
| 672 | bdev->bd_inode->i_size = 0; |
| 673 | if (max_part > 0) { |
| 674 | blkdev_reread_part(bdev); |
| 675 | bdev->bd_invalidated = 1; |
| 676 | } |
| 677 | } |
| 678 | |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 679 | static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev) |
| 680 | { |
| 681 | if (nbd->flags & NBD_FLAG_READ_ONLY) |
| 682 | set_device_ro(bdev, true); |
| 683 | if (nbd->flags & NBD_FLAG_SEND_TRIM) |
| 684 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
| 685 | if (nbd->flags & NBD_FLAG_SEND_FLUSH) |
Jens Axboe | aafb1ee | 2016-03-30 10:10:53 -0600 | [diff] [blame] | 686 | blk_queue_write_cache(nbd->disk->queue, true, false); |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 687 | else |
Jens Axboe | aafb1ee | 2016-03-30 10:10:53 -0600 | [diff] [blame] | 688 | blk_queue_write_cache(nbd->disk->queue, false, false); |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 691 | static void send_disconnects(struct nbd_device *nbd) |
| 692 | { |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 693 | struct nbd_request request = { |
| 694 | .magic = htonl(NBD_REQUEST_MAGIC), |
| 695 | .type = htonl(NBD_CMD_DISC), |
| 696 | }; |
| 697 | struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)}; |
| 698 | struct iov_iter from; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 699 | int i, ret; |
| 700 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 701 | for (i = 0; i < nbd->num_connections; i++) { |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 702 | iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request)); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 703 | ret = sock_xmit(nbd, i, 1, &from, 0, NULL); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 704 | if (ret <= 0) |
| 705 | dev_err(disk_to_dev(nbd->disk), |
| 706 | "Send disconnect failed %d\n", ret); |
| 707 | } |
| 708 | } |
| 709 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 710 | static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev) |
| 711 | { |
| 712 | dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); |
| 713 | if (!nbd->socks) |
| 714 | return -EINVAL; |
| 715 | |
| 716 | mutex_unlock(&nbd->config_lock); |
| 717 | fsync_bdev(bdev); |
| 718 | mutex_lock(&nbd->config_lock); |
| 719 | |
| 720 | /* Check again after getting mutex back. */ |
| 721 | if (!nbd->socks) |
| 722 | return -EINVAL; |
| 723 | |
| 724 | if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED, |
| 725 | &nbd->runtime_flags)) |
| 726 | send_disconnects(nbd); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev) |
| 731 | { |
| 732 | sock_shutdown(nbd); |
| 733 | nbd_clear_que(nbd); |
Ratna Manoj Bolla | abbbdf1 | 2017-03-24 14:08:29 -0400 | [diff] [blame] | 734 | |
| 735 | __invalidate_device(bdev, true); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 736 | nbd_bdev_reset(bdev); |
| 737 | /* |
| 738 | * We want to give the run thread a chance to wait for everybody |
| 739 | * to clean up and then do it's own cleanup. |
| 740 | */ |
| 741 | if (!test_bit(NBD_RUNNING, &nbd->runtime_flags) && |
| 742 | nbd->num_connections) { |
| 743 | int i; |
| 744 | |
Josef Bacik | 6a8a215 | 2017-03-01 11:47:22 -0500 | [diff] [blame] | 745 | for (i = 0; i < nbd->num_connections; i++) { |
| 746 | sockfd_put(nbd->socks[i]->sock); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 747 | kfree(nbd->socks[i]); |
Josef Bacik | 6a8a215 | 2017-03-01 11:47:22 -0500 | [diff] [blame] | 748 | } |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 749 | kfree(nbd->socks); |
| 750 | nbd->socks = NULL; |
| 751 | nbd->num_connections = 0; |
| 752 | } |
| 753 | nbd->task_setup = NULL; |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev) |
| 759 | { |
| 760 | struct recv_thread_args *args; |
| 761 | int num_connections = nbd->num_connections; |
| 762 | int error = 0, i; |
| 763 | |
| 764 | if (nbd->task_recv) |
| 765 | return -EBUSY; |
| 766 | if (!nbd->socks) |
| 767 | return -EINVAL; |
| 768 | if (num_connections > 1 && |
| 769 | !(nbd->flags & NBD_FLAG_CAN_MULTI_CONN)) { |
| 770 | dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n"); |
| 771 | error = -EINVAL; |
| 772 | goto out_err; |
| 773 | } |
| 774 | |
| 775 | set_bit(NBD_RUNNING, &nbd->runtime_flags); |
| 776 | blk_mq_update_nr_hw_queues(&nbd->tag_set, nbd->num_connections); |
| 777 | args = kcalloc(num_connections, sizeof(*args), GFP_KERNEL); |
| 778 | if (!args) { |
| 779 | error = -ENOMEM; |
| 780 | goto out_err; |
| 781 | } |
| 782 | nbd->task_recv = current; |
| 783 | mutex_unlock(&nbd->config_lock); |
| 784 | |
| 785 | nbd_parse_flags(nbd, bdev); |
| 786 | |
| 787 | error = device_create_file(disk_to_dev(nbd->disk), &pid_attr); |
| 788 | if (error) { |
| 789 | dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); |
| 790 | goto out_recv; |
| 791 | } |
| 792 | |
| 793 | nbd_size_update(nbd, bdev); |
| 794 | |
| 795 | nbd_dev_dbg_init(nbd); |
| 796 | for (i = 0; i < num_connections; i++) { |
| 797 | sk_set_memalloc(nbd->socks[i]->sock->sk); |
| 798 | atomic_inc(&nbd->recv_threads); |
| 799 | INIT_WORK(&args[i].work, recv_work); |
| 800 | args[i].nbd = nbd; |
| 801 | args[i].index = i; |
| 802 | queue_work(recv_workqueue, &args[i].work); |
| 803 | } |
| 804 | wait_event_interruptible(nbd->recv_wq, |
| 805 | atomic_read(&nbd->recv_threads) == 0); |
| 806 | for (i = 0; i < num_connections; i++) |
| 807 | flush_work(&args[i].work); |
| 808 | nbd_dev_dbg_close(nbd); |
| 809 | nbd_size_clear(nbd, bdev); |
| 810 | device_remove_file(disk_to_dev(nbd->disk), &pid_attr); |
| 811 | out_recv: |
| 812 | mutex_lock(&nbd->config_lock); |
| 813 | nbd->task_recv = NULL; |
| 814 | out_err: |
| 815 | clear_bit(NBD_RUNNING, &nbd->runtime_flags); |
| 816 | nbd_clear_sock(nbd, bdev); |
| 817 | |
| 818 | /* user requested, ignore socket errors */ |
| 819 | if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags)) |
| 820 | error = 0; |
| 821 | if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags)) |
| 822 | error = -ETIMEDOUT; |
| 823 | |
| 824 | nbd_reset(nbd); |
| 825 | return error; |
| 826 | } |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 827 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 828 | /* Must be called with config_lock held */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 829 | static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 830 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | switch (cmd) { |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 833 | case NBD_DISCONNECT: |
| 834 | return nbd_disconnect(nbd, bdev); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 835 | case NBD_CLEAR_SOCK: |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 836 | return nbd_clear_sock(nbd, bdev); |
| 837 | case NBD_SET_SOCK: |
| 838 | return nbd_add_socket(nbd, bdev, arg); |
| 839 | case NBD_SET_BLKSIZE: |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 840 | nbd_size_set(nbd, bdev, arg, |
| 841 | div_s64(nbd->bytesize, arg)); |
| 842 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | case NBD_SET_SIZE: |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 844 | nbd_size_set(nbd, bdev, nbd->blksize, |
| 845 | div_s64(arg, nbd->blksize)); |
| 846 | return 0; |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 847 | case NBD_SET_SIZE_BLOCKS: |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 848 | nbd_size_set(nbd, bdev, nbd->blksize, arg); |
| 849 | return 0; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 850 | case NBD_SET_TIMEOUT: |
Josef Bacik | f858685 | 2017-03-24 14:08:28 -0400 | [diff] [blame] | 851 | if (arg) { |
| 852 | nbd->tag_set.timeout = arg * HZ; |
| 853 | blk_queue_rq_timeout(nbd->disk->queue, arg * HZ); |
| 854 | } |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 855 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 856 | |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 857 | case NBD_SET_FLAGS: |
| 858 | nbd->flags = arg; |
| 859 | return 0; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 860 | case NBD_DO_IT: |
| 861 | return nbd_start_device(nbd, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | case NBD_CLEAR_QUE: |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 863 | /* |
| 864 | * This is for compatibility only. The queue is always cleared |
| 865 | * by NBD_DO_IT or NBD_CLEAR_SOCK. |
| 866 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | return 0; |
| 868 | case NBD_PRINT_DEBUG: |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 869 | /* |
| 870 | * For compatibility only, we no longer keep a list of |
| 871 | * outstanding requests. |
| 872 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | return 0; |
| 874 | } |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 875 | return -ENOTTY; |
| 876 | } |
| 877 | |
| 878 | static int nbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 879 | unsigned int cmd, unsigned long arg) |
| 880 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 881 | struct nbd_device *nbd = bdev->bd_disk->private_data; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 882 | int error; |
| 883 | |
| 884 | if (!capable(CAP_SYS_ADMIN)) |
| 885 | return -EPERM; |
| 886 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 887 | BUG_ON(nbd->magic != NBD_MAGIC); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 888 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 889 | mutex_lock(&nbd->config_lock); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 890 | error = __nbd_ioctl(bdev, nbd, cmd, arg); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 891 | mutex_unlock(&nbd->config_lock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 892 | |
| 893 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 896 | static const struct block_device_operations nbd_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | { |
| 898 | .owner = THIS_MODULE, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 899 | .ioctl = nbd_ioctl, |
Al Viro | 263a3df | 2016-01-07 10:04:37 -0500 | [diff] [blame] | 900 | .compat_ioctl = nbd_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | }; |
| 902 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 903 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 904 | |
| 905 | static int nbd_dbg_tasks_show(struct seq_file *s, void *unused) |
| 906 | { |
| 907 | struct nbd_device *nbd = s->private; |
| 908 | |
| 909 | if (nbd->task_recv) |
| 910 | seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv)); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | static int nbd_dbg_tasks_open(struct inode *inode, struct file *file) |
| 916 | { |
| 917 | return single_open(file, nbd_dbg_tasks_show, inode->i_private); |
| 918 | } |
| 919 | |
| 920 | static const struct file_operations nbd_dbg_tasks_ops = { |
| 921 | .open = nbd_dbg_tasks_open, |
| 922 | .read = seq_read, |
| 923 | .llseek = seq_lseek, |
| 924 | .release = single_release, |
| 925 | }; |
| 926 | |
| 927 | static int nbd_dbg_flags_show(struct seq_file *s, void *unused) |
| 928 | { |
| 929 | struct nbd_device *nbd = s->private; |
| 930 | u32 flags = nbd->flags; |
| 931 | |
| 932 | seq_printf(s, "Hex: 0x%08x\n\n", flags); |
| 933 | |
| 934 | seq_puts(s, "Known flags:\n"); |
| 935 | |
| 936 | if (flags & NBD_FLAG_HAS_FLAGS) |
| 937 | seq_puts(s, "NBD_FLAG_HAS_FLAGS\n"); |
| 938 | if (flags & NBD_FLAG_READ_ONLY) |
| 939 | seq_puts(s, "NBD_FLAG_READ_ONLY\n"); |
| 940 | if (flags & NBD_FLAG_SEND_FLUSH) |
| 941 | seq_puts(s, "NBD_FLAG_SEND_FLUSH\n"); |
| 942 | if (flags & NBD_FLAG_SEND_TRIM) |
| 943 | seq_puts(s, "NBD_FLAG_SEND_TRIM\n"); |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | static int nbd_dbg_flags_open(struct inode *inode, struct file *file) |
| 949 | { |
| 950 | return single_open(file, nbd_dbg_flags_show, inode->i_private); |
| 951 | } |
| 952 | |
| 953 | static const struct file_operations nbd_dbg_flags_ops = { |
| 954 | .open = nbd_dbg_flags_open, |
| 955 | .read = seq_read, |
| 956 | .llseek = seq_lseek, |
| 957 | .release = single_release, |
| 958 | }; |
| 959 | |
| 960 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 961 | { |
| 962 | struct dentry *dir; |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 963 | |
| 964 | if (!nbd_dbg_dir) |
| 965 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 966 | |
| 967 | dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 968 | if (!dir) { |
| 969 | dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n", |
| 970 | nbd_name(nbd)); |
| 971 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 972 | } |
| 973 | nbd->dbg_dir = dir; |
| 974 | |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 975 | debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops); |
| 976 | debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize); |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 977 | debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout); |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 978 | debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize); |
Josef Bacik | d366a0f | 2016-06-08 10:32:10 -0400 | [diff] [blame] | 979 | debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 985 | { |
| 986 | debugfs_remove_recursive(nbd->dbg_dir); |
| 987 | } |
| 988 | |
| 989 | static int nbd_dbg_init(void) |
| 990 | { |
| 991 | struct dentry *dbg_dir; |
| 992 | |
| 993 | dbg_dir = debugfs_create_dir("nbd", NULL); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 994 | if (!dbg_dir) |
| 995 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 996 | |
| 997 | nbd_dbg_dir = dbg_dir; |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | static void nbd_dbg_close(void) |
| 1003 | { |
| 1004 | debugfs_remove_recursive(nbd_dbg_dir); |
| 1005 | } |
| 1006 | |
| 1007 | #else /* IS_ENABLED(CONFIG_DEBUG_FS) */ |
| 1008 | |
| 1009 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 1010 | { |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 1015 | { |
| 1016 | } |
| 1017 | |
| 1018 | static int nbd_dbg_init(void) |
| 1019 | { |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | static void nbd_dbg_close(void) |
| 1024 | { |
| 1025 | } |
| 1026 | |
| 1027 | #endif |
| 1028 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1029 | static int nbd_init_request(void *data, struct request *rq, |
| 1030 | unsigned int hctx_idx, unsigned int request_idx, |
| 1031 | unsigned int numa_node) |
| 1032 | { |
| 1033 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1034 | cmd->nbd = data; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | static struct blk_mq_ops nbd_mq_ops = { |
| 1039 | .queue_rq = nbd_queue_rq, |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1040 | .init_request = nbd_init_request, |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 1041 | .timeout = nbd_xmit_timeout, |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1042 | }; |
| 1043 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1044 | static void nbd_dev_remove(struct nbd_device *nbd) |
| 1045 | { |
| 1046 | struct gendisk *disk = nbd->disk; |
| 1047 | nbd->magic = 0; |
| 1048 | if (disk) { |
| 1049 | del_gendisk(disk); |
| 1050 | blk_cleanup_queue(disk->queue); |
| 1051 | blk_mq_free_tag_set(&nbd->tag_set); |
| 1052 | put_disk(disk); |
| 1053 | } |
| 1054 | kfree(nbd); |
| 1055 | } |
| 1056 | |
| 1057 | static int nbd_dev_add(int index) |
| 1058 | { |
| 1059 | struct nbd_device *nbd; |
| 1060 | struct gendisk *disk; |
| 1061 | struct request_queue *q; |
| 1062 | int err = -ENOMEM; |
| 1063 | |
| 1064 | nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL); |
| 1065 | if (!nbd) |
| 1066 | goto out; |
| 1067 | |
| 1068 | disk = alloc_disk(1 << part_shift); |
| 1069 | if (!disk) |
| 1070 | goto out_free_nbd; |
| 1071 | |
| 1072 | if (index >= 0) { |
| 1073 | err = idr_alloc(&nbd_index_idr, nbd, index, index + 1, |
| 1074 | GFP_KERNEL); |
| 1075 | if (err == -ENOSPC) |
| 1076 | err = -EEXIST; |
| 1077 | } else { |
| 1078 | err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL); |
| 1079 | if (err >= 0) |
| 1080 | index = err; |
| 1081 | } |
| 1082 | if (err < 0) |
| 1083 | goto out_free_disk; |
| 1084 | |
| 1085 | nbd->disk = disk; |
| 1086 | nbd->tag_set.ops = &nbd_mq_ops; |
| 1087 | nbd->tag_set.nr_hw_queues = 1; |
| 1088 | nbd->tag_set.queue_depth = 128; |
| 1089 | nbd->tag_set.numa_node = NUMA_NO_NODE; |
| 1090 | nbd->tag_set.cmd_size = sizeof(struct nbd_cmd); |
| 1091 | nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | |
| 1092 | BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING; |
| 1093 | nbd->tag_set.driver_data = nbd; |
| 1094 | |
| 1095 | err = blk_mq_alloc_tag_set(&nbd->tag_set); |
| 1096 | if (err) |
| 1097 | goto out_free_idr; |
| 1098 | |
| 1099 | q = blk_mq_init_queue(&nbd->tag_set); |
| 1100 | if (IS_ERR(q)) { |
| 1101 | err = PTR_ERR(q); |
| 1102 | goto out_free_tags; |
| 1103 | } |
| 1104 | disk->queue = q; |
| 1105 | |
| 1106 | /* |
| 1107 | * Tell the block layer that we are not a rotational device |
| 1108 | */ |
| 1109 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); |
| 1110 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue); |
| 1111 | disk->queue->limits.discard_granularity = 512; |
| 1112 | blk_queue_max_discard_sectors(disk->queue, UINT_MAX); |
| 1113 | disk->queue->limits.discard_zeroes_data = 0; |
| 1114 | blk_queue_max_hw_sectors(disk->queue, 65536); |
| 1115 | disk->queue->limits.max_sectors = 256; |
| 1116 | |
| 1117 | nbd->magic = NBD_MAGIC; |
| 1118 | mutex_init(&nbd->config_lock); |
| 1119 | disk->major = NBD_MAJOR; |
| 1120 | disk->first_minor = index << part_shift; |
| 1121 | disk->fops = &nbd_fops; |
| 1122 | disk->private_data = nbd; |
| 1123 | sprintf(disk->disk_name, "nbd%d", index); |
| 1124 | init_waitqueue_head(&nbd->recv_wq); |
| 1125 | nbd_reset(nbd); |
| 1126 | add_disk(disk); |
| 1127 | return index; |
| 1128 | |
| 1129 | out_free_tags: |
| 1130 | blk_mq_free_tag_set(&nbd->tag_set); |
| 1131 | out_free_idr: |
| 1132 | idr_remove(&nbd_index_idr, index); |
| 1133 | out_free_disk: |
| 1134 | put_disk(disk); |
| 1135 | out_free_nbd: |
| 1136 | kfree(nbd); |
| 1137 | out: |
| 1138 | return err; |
| 1139 | } |
| 1140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * And here should be modules and kernel interface |
| 1143 | * (Just smiley confuses emacs :-) |
| 1144 | */ |
| 1145 | |
| 1146 | static int __init nbd_init(void) |
| 1147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | int i; |
| 1149 | |
Adrian Bunk | 5b7b18c | 2006-03-25 03:07:04 -0800 | [diff] [blame] | 1150 | BUILD_BUG_ON(sizeof(struct nbd_request) != 28); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1152 | if (max_part < 0) { |
WANG Cong | 7742ce4 | 2011-08-19 14:48:28 +0200 | [diff] [blame] | 1153 | printk(KERN_ERR "nbd: max_part must be >= 0\n"); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1154 | return -EINVAL; |
| 1155 | } |
| 1156 | |
| 1157 | part_shift = 0; |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1158 | if (max_part > 0) { |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1159 | part_shift = fls(max_part); |
| 1160 | |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1161 | /* |
| 1162 | * Adjust max_part according to part_shift as it is exported |
| 1163 | * to user space so that user can know the max number of |
| 1164 | * partition kernel should be able to manage. |
| 1165 | * |
| 1166 | * Note that -1 is required because partition 0 is reserved |
| 1167 | * for the whole disk. |
| 1168 | */ |
| 1169 | max_part = (1UL << part_shift) - 1; |
| 1170 | } |
| 1171 | |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1172 | if ((1UL << part_shift) > DISK_MAX_PARTS) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | if (nbds_max > 1UL << (MINORBITS - part_shift)) |
| 1176 | return -EINVAL; |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 1177 | recv_workqueue = alloc_workqueue("knbd-recv", |
| 1178 | WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); |
| 1179 | if (!recv_workqueue) |
| 1180 | return -ENOMEM; |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1181 | |
Josef Bacik | 6330a2d | 2017-02-15 16:49:48 -0500 | [diff] [blame] | 1182 | if (register_blkdev(NBD_MAJOR, "nbd")) { |
| 1183 | destroy_workqueue(recv_workqueue); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1184 | return -EIO; |
Josef Bacik | 6330a2d | 2017-02-15 16:49:48 -0500 | [diff] [blame] | 1185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1187 | nbd_dbg_init(); |
| 1188 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1189 | mutex_lock(&nbd_index_mutex); |
| 1190 | for (i = 0; i < nbds_max; i++) |
| 1191 | nbd_dev_add(i); |
| 1192 | mutex_unlock(&nbd_index_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | return 0; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | static int nbd_exit_cb(int id, void *ptr, void *data) |
| 1197 | { |
| 1198 | struct nbd_device *nbd = ptr; |
| 1199 | nbd_dev_remove(nbd); |
| 1200 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | static void __exit nbd_cleanup(void) |
| 1204 | { |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1205 | nbd_dbg_close(); |
| 1206 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1207 | idr_for_each(&nbd_index_idr, &nbd_exit_cb, NULL); |
| 1208 | idr_destroy(&nbd_index_idr); |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 1209 | destroy_workqueue(recv_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | unregister_blkdev(NBD_MAJOR, "nbd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | module_init(nbd_init); |
| 1214 | module_exit(nbd_cleanup); |
| 1215 | |
| 1216 | MODULE_DESCRIPTION("Network Block Device"); |
| 1217 | MODULE_LICENSE("GPL"); |
| 1218 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 1219 | module_param(nbds_max, int, 0444); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1220 | MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)"); |
| 1221 | module_param(max_part, int, 0444); |
| 1222 | MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)"); |