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