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