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