blob: a9e398019f38156893bfe36204691886090b05ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Macheka2531292010-07-18 14:27:13 +02007 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
9 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070010 * This file is released under GPLv2 or later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070012 * (part of code stolen from loop.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
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 Bergmann2a48fc02010-06-02 14:28:52 +020027#include <linux/mutex.h>
Herbert Xu4b2f0262006-01-06 00:09:47 -080028#include <linux/compiler.h>
29#include <linux/err.h>
30#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/sock.h>
Trond Myklebust91cf45f2007-11-12 18:10:39 -080033#include <linux/net.h>
Laurent Vivier48cf6062008-04-29 01:02:46 -070034#include <linux/kthread.h>
Markus Pargmannb9c495b2015-04-02 10:11:37 +020035#include <linux/types.h>
Markus Pargmann30d53d92015-08-17 08:20:06 +020036#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#include <asm/types.h>
40
41#include <linux/nbd.h>
42
Markus Pargmann13e71d62015-04-02 10:11:35 +020043struct nbd_device {
Markus Pargmann22d109c2015-08-17 08:20:09 +020044 u32 flags;
Markus Pargmann13e71d62015-04-02 10:11:35 +020045 struct socket * sock; /* If == NULL, device is not ready, yet */
46 int magic;
47
48 spinlock_t queue_lock;
49 struct list_head queue_head; /* Requests waiting result */
50 struct request *active_req;
51 wait_queue_head_t active_wq;
52 struct list_head waiting_queue; /* Requests to be sent */
53 wait_queue_head_t waiting_wq;
54
55 struct mutex tx_lock;
56 struct gendisk *disk;
57 int blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020058 loff_t bytesize;
Markus Pargmann13e71d62015-04-02 10:11:35 +020059 int xmit_timeout;
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +010060 bool timedout;
Markus Pargmann696697c2015-08-17 08:20:07 +020061 bool disconnect; /* a disconnect has been requested by user */
Markus Pargmann7e2893a2015-08-17 08:20:00 +020062
63 struct timer_list timeout_timer;
Markus Pargmann23272a672015-10-29 11:51:16 +010064 /* protects initialization and shutdown of the socket */
65 spinlock_t sock_lock;
Markus Pargmann7e2893a2015-08-17 08:20:00 +020066 struct task_struct *task_recv;
67 struct task_struct *task_send;
Markus Pargmann30d53d92015-08-17 08:20:06 +020068
69#if IS_ENABLED(CONFIG_DEBUG_FS)
70 struct dentry *dbg_dir;
71#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +020072};
73
Markus Pargmann30d53d92015-08-17 08:20:06 +020074#if IS_ENABLED(CONFIG_DEBUG_FS)
75static struct dentry *nbd_dbg_dir;
76#endif
77
78#define nbd_name(nbd) ((nbd)->disk->disk_name)
79
Wanlong Gaof4507162012-03-28 14:42:51 -070080#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Ingo van Lil9c7a4162006-07-01 04:36:36 -070082static unsigned int nbds_max = 16;
Paul Clements20a81432008-02-08 04:21:51 -080083static struct nbd_device *nbd_dev;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070084static int max_part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Use just one lock (or at most 1 per NIC). Two arguments for this:
88 * 1. Each NIC is essentially a synchronization point for all servers
89 * accessed through that NIC so there's no need to have more locks
90 * than NICs anyway.
91 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
92 * down each lock to the point where they're actually slower than just
93 * a single lock.
94 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
95 */
96static DEFINE_SPINLOCK(nbd_lock);
97
Markus Pargmannd18509f2015-04-02 10:11:38 +020098static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Markus Pargmannd18509f2015-04-02 10:11:38 +0200100 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Markus Pargmann37091fd2015-07-27 07:36:49 +0200103static bool nbd_is_connected(struct nbd_device *nbd)
104{
105 return !!nbd->task_recv;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static const char *nbdcmd_to_ascii(int cmd)
109{
110 switch (cmd) {
111 case NBD_CMD_READ: return "read";
112 case NBD_CMD_WRITE: return "write";
113 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800114 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700115 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 return "invalid";
118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Markus Pargmann37091fd2015-07-27 07:36:49 +0200120static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
121{
122 bdev->bd_inode->i_size = 0;
123 set_capacity(nbd->disk, 0);
124 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
125
126 return 0;
127}
128
129static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev)
130{
131 if (!nbd_is_connected(nbd))
132 return;
133
134 bdev->bd_inode->i_size = nbd->bytesize;
135 set_capacity(nbd->disk, nbd->bytesize >> 9);
136 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
137}
138
139static int nbd_size_set(struct nbd_device *nbd, struct block_device *bdev,
140 int blocksize, int nr_blocks)
141{
142 int ret;
143
144 ret = set_blocksize(bdev, blocksize);
145 if (ret)
146 return ret;
147
148 nbd->blksize = blocksize;
149 nbd->bytesize = (loff_t)blocksize * (loff_t)nr_blocks;
150
151 nbd_size_update(nbd, bdev);
152
153 return 0;
154}
155
Markus Pargmannd18509f2015-04-02 10:11:38 +0200156static void nbd_end_request(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500158 int error = req->errors ? -EIO : 0;
Jens Axboe165125e2007-07-24 09:28:11 +0200159 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned long flags;
161
Markus Pargmannd18509f2015-04-02 10:11:38 +0200162 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req,
163 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo1011c1b2009-05-07 22:24:45 +0900166 __blk_end_request_all(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 spin_unlock_irqrestore(q->queue_lock, flags);
168}
169
Markus Pargmanne018e752015-04-02 10:11:39 +0200170/*
171 * Forcibly shutdown the socket causing all listeners to error
172 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200173static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700174{
Markus Pargmann23272a672015-10-29 11:51:16 +0100175 spin_lock_irq(&nbd->sock_lock);
176
177 if (!nbd->sock) {
178 spin_unlock_irq(&nbd->sock_lock);
Markus Pargmann260bbce2015-08-17 08:20:02 +0200179 return;
Markus Pargmann23272a672015-10-29 11:51:16 +0100180 }
Markus Pargmann260bbce2015-08-17 08:20:02 +0200181
182 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
183 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
Markus Pargmann23272a672015-10-29 11:51:16 +0100184 sockfd_put(nbd->sock);
Markus Pargmann260bbce2015-08-17 08:20:02 +0200185 nbd->sock = NULL;
Markus Pargmann23272a672015-10-29 11:51:16 +0100186 spin_unlock_irq(&nbd->sock_lock);
187
188 del_timer(&nbd->timeout_timer);
Paul Clements7fdfd402007-10-16 23:27:37 -0700189}
190
191static void nbd_xmit_timeout(unsigned long arg)
192{
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200193 struct nbd_device *nbd = (struct nbd_device *)arg;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200194 unsigned long flags;
Paul Clements7fdfd402007-10-16 23:27:37 -0700195
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200196 if (list_empty(&nbd->queue_head))
197 return;
198
Markus Pargmann23272a672015-10-29 11:51:16 +0100199 spin_lock_irqsave(&nbd->sock_lock, flags);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200200
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100201 nbd->timedout = true;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200202
Markus Pargmann23272a672015-10-29 11:51:16 +0100203 if (nbd->sock)
204 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200205
Markus Pargmann23272a672015-10-29 11:51:16 +0100206 spin_unlock_irqrestore(&nbd->sock_lock, flags);
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200207
Markus Pargmann23272a672015-10-29 11:51:16 +0100208 dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * Send or receive packet.
213 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700214static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int msg_flags)
216{
Wanlong Gaof4507162012-03-28 14:42:51 -0700217 struct socket *sock = nbd->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int result;
219 struct msghdr msg;
220 struct kvec iov;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700221 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700223 if (unlikely(!sock)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700224 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200225 "Attempted %s on closed socket in sock_xmit\n",
226 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700227 return -EINVAL;
228 }
229
Mel Gorman7f338fe2012-07-31 16:44:32 -0700230 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700232 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 iov.iov_base = buf;
234 iov.iov_len = size;
235 msg.msg_name = NULL;
236 msg.msg_namelen = 0;
237 msg.msg_control = NULL;
238 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
240
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200241 if (send)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200243 else
Namhyung Kim35fbf5b2011-05-28 14:44:46 +0200244 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
245 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (result <= 0) {
248 if (result == 0)
249 result = -EPIPE; /* short read */
250 break;
251 }
252 size -= result;
253 buf += result;
254 } while (size > 0);
255
Mel Gorman7f338fe2012-07-31 16:44:32 -0700256 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200258 if (!send && nbd->xmit_timeout)
259 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return result;
262}
263
Wanlong Gaof4507162012-03-28 14:42:51 -0700264static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int flags)
266{
267 int result;
268 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700269 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
270 bvec->bv_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 kunmap(bvec->bv_page);
272 return result;
273}
274
Paul Clements7fdfd402007-10-16 23:27:37 -0700275/* always call with the tx_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -0700276static int nbd_send_req(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
NeilBrown5705f702007-09-25 12:35:59 +0200278 int result, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct nbd_request request;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900280 unsigned long size = blk_rq_bytes(req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200281 u32 type;
282
283 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
284 type = NBD_CMD_DISC;
Mike Christiec2df40d2016-06-05 14:32:17 -0500285 else if (req_op(req) == REQ_OP_DISCARD)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200286 type = NBD_CMD_TRIM;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500287 else if (req_op(req) == REQ_OP_FLUSH)
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200288 type = NBD_CMD_FLUSH;
289 else if (rq_data_dir(req) == WRITE)
290 type = NBD_CMD_WRITE;
291 else
292 type = NBD_CMD_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700294 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 request.magic = htonl(NBD_REQUEST_MAGIC);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200296 request.type = htonl(type);
297 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800298 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
299 request.len = htonl(size);
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 memcpy(request.handle, &req, sizeof(req));
302
Markus Pargmannd18509f2015-04-02 10:11:38 +0200303 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200304 req, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200305 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700306 result = sock_xmit(nbd, 1, &request, sizeof(request),
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200307 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700309 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200310 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200311 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200314 if (type == NBD_CMD_WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200315 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800316 struct bio_vec bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*
318 * we are really probing at internals to determine
319 * whether to set MSG_MORE or not...
320 */
NeilBrown5705f702007-09-25 12:35:59 +0200321 rq_for_each_segment(bvec, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200322 flags = 0;
Kent Overstreet4550dd62013-08-07 14:26:21 -0700323 if (!rq_iter_last(bvec, iter))
Jens Axboe6c92e692007-08-16 13:43:12 +0200324 flags = MSG_MORE;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200325 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
326 req, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800327 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200328 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700329 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200330 "Send data failed (result %d)\n",
331 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200332 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Wanlong Gaof4507162012-03-28 14:42:51 -0700339static struct request *nbd_find_request(struct nbd_device *nbd,
Denis Cheng0cbc591b2007-10-16 23:26:14 -0700340 struct request *xreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Denis Chengd2c97402007-10-16 23:26:14 -0700342 struct request *req, *tmp;
Herbert Xu4b2f0262006-01-06 00:09:47 -0800343 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Wanlong Gaof4507162012-03-28 14:42:51 -0700345 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800346 if (unlikely(err))
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200347 return ERR_PTR(err);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800348
Wanlong Gaof4507162012-03-28 14:42:51 -0700349 spin_lock(&nbd->queue_lock);
350 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (req != xreq)
352 continue;
353 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700354 spin_unlock(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return req;
356 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700357 spin_unlock(&nbd->queue_lock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800358
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200359 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Wanlong Gaof4507162012-03-28 14:42:51 -0700362static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 int result;
365 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700366 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 MSG_WAITALL);
368 kunmap(bvec->bv_page);
369 return result;
370}
371
372/* NULL returned = something went wrong, inform userspace */
Wanlong Gaof4507162012-03-28 14:42:51 -0700373static struct request *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 int result;
376 struct nbd_reply reply;
377 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700380 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700382 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200383 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200384 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700386
387 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700388 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700389 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200390 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700391 }
392
Wanlong Gaof4507162012-03-28 14:42:51 -0700393 req = nbd_find_request(nbd, *(struct request **)reply.handle);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700394 if (IS_ERR(req)) {
Herbert Xu4b2f0262006-01-06 00:09:47 -0800395 result = PTR_ERR(req);
396 if (result != -ENOENT)
Markus Pargmann19391832015-08-17 08:20:03 +0200397 return ERR_PTR(result);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800398
Wanlong Gaof4507162012-03-28 14:42:51 -0700399 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200400 reply.handle);
Markus Pargmann19391832015-08-17 08:20:03 +0200401 return ERR_PTR(-EBADR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700405 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200406 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 req->errors++;
408 return req;
409 }
410
Markus Pargmannd18509f2015-04-02 10:11:38 +0200411 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200412 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200413 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800414 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200415
416 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800417 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200418 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700419 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200420 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200421 req->errors++;
422 return req;
423 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200424 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
425 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
428 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200431static ssize_t pid_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800433{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200434 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200435 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200436
Markus Pargmann6521d392015-08-17 08:20:05 +0200437 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800438}
439
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200440static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700441 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800442 .show = pid_show,
443};
444
Markus Pargmann37091fd2015-07-27 07:36:49 +0200445static int nbd_thread_recv(struct nbd_device *nbd, struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct request *req;
WANG Cong84963042007-05-09 02:33:36 -0700448 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Wanlong Gaof4507162012-03-28 14:42:51 -0700450 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Mel Gorman7f338fe2012-07-31 16:44:32 -0700452 sk_set_memalloc(nbd->sock->sk);
Markus Pargmann6521d392015-08-17 08:20:05 +0200453
Wanlong Gaof4507162012-03-28 14:42:51 -0700454 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700455 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700456 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
WANG Cong84963042007-05-09 02:33:36 -0700457 return ret;
458 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800459
Markus Pargmann37091fd2015-07-27 07:36:49 +0200460 nbd_size_update(nbd, bdev);
461
Markus Pargmann19391832015-08-17 08:20:03 +0200462 while (1) {
463 req = nbd_read_stat(nbd);
464 if (IS_ERR(req)) {
465 ret = PTR_ERR(req);
466 break;
467 }
468
Markus Pargmannd18509f2015-04-02 10:11:38 +0200469 nbd_end_request(nbd, req);
Markus Pargmann19391832015-08-17 08:20:03 +0200470 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800471
Markus Pargmann37091fd2015-07-27 07:36:49 +0200472 nbd_size_clear(nbd, bdev);
473
Markus Pargmann6521d392015-08-17 08:20:05 +0200474 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Wanlong Gaof4507162012-03-28 14:42:51 -0700478static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct request *req;
481
Wanlong Gaof4507162012-03-28 14:42:51 -0700482 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Herbert Xu4b2f0262006-01-06 00:09:47 -0800484 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700485 * Because we have set nbd->sock to NULL under the tx_lock, all
Herbert Xu4b2f0262006-01-06 00:09:47 -0800486 * modifications to the list must have completed by now. For
487 * the same reason, the active_req must be NULL.
488 *
489 * As a consequence, we don't need to take the spin lock while
490 * purging the list here.
491 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700492 BUG_ON(nbd->sock);
493 BUG_ON(nbd->active_req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800494
Wanlong Gaof4507162012-03-28 14:42:51 -0700495 while (!list_empty(&nbd->queue_head)) {
496 req = list_entry(nbd->queue_head.next, struct request,
Herbert Xu4b2f0262006-01-06 00:09:47 -0800497 queuelist);
498 list_del_init(&req->queuelist);
499 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200500 nbd_end_request(nbd, req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800501 }
Paul Clementsfded4e02012-09-17 14:09:02 -0700502
503 while (!list_empty(&nbd->waiting_queue)) {
504 req = list_entry(nbd->waiting_queue.next, struct request,
505 queuelist);
506 list_del_init(&req->queuelist);
507 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200508 nbd_end_request(nbd, req);
Paul Clementsfded4e02012-09-17 14:09:02 -0700509 }
Markus Pargmanne78273c2015-08-17 08:20:04 +0200510 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Paul Clements7fdfd402007-10-16 23:27:37 -0700513
Wanlong Gaof4507162012-03-28 14:42:51 -0700514static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700515{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200516 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700517 goto error_out;
518
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200519 if (rq_data_dir(req) == WRITE &&
520 (nbd->flags & NBD_FLAG_READ_ONLY)) {
521 dev_err(disk_to_dev(nbd->disk),
522 "Write on read-only\n");
523 goto error_out;
Alex Bligh75f187a2013-02-27 17:05:23 -0800524 }
525
Laurent Vivier48cf6062008-04-29 01:02:46 -0700526 req->errors = 0;
527
Wanlong Gaof4507162012-03-28 14:42:51 -0700528 mutex_lock(&nbd->tx_lock);
529 if (unlikely(!nbd->sock)) {
530 mutex_unlock(&nbd->tx_lock);
531 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200532 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700533 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700534 }
535
Wanlong Gaof4507162012-03-28 14:42:51 -0700536 nbd->active_req = req;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700537
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200538 if (nbd->xmit_timeout && list_empty_careful(&nbd->queue_head))
539 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
540
Wanlong Gaof4507162012-03-28 14:42:51 -0700541 if (nbd_send_req(nbd, req) != 0) {
542 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700543 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200544 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700545 } else {
Wanlong Gaof4507162012-03-28 14:42:51 -0700546 spin_lock(&nbd->queue_lock);
Chetan Loke01ff5db2012-07-31 08:47:13 +0200547 list_add_tail(&req->queuelist, &nbd->queue_head);
Wanlong Gaof4507162012-03-28 14:42:51 -0700548 spin_unlock(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700549 }
550
Wanlong Gaof4507162012-03-28 14:42:51 -0700551 nbd->active_req = NULL;
552 mutex_unlock(&nbd->tx_lock);
553 wake_up_all(&nbd->active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700554
555 return;
556
557error_out:
558 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200559 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700560}
561
Markus Pargmanncad73b22015-08-17 08:20:08 +0200562static int nbd_thread_send(void *data)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700563{
Wanlong Gaof4507162012-03-28 14:42:51 -0700564 struct nbd_device *nbd = data;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700565 struct request *req;
566
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200567 nbd->task_send = current;
568
Dongsheng Yang8698a742014-03-11 18:09:12 +0800569 set_user_nice(current, MIN_NICE);
Wanlong Gaof4507162012-03-28 14:42:51 -0700570 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
Laurent Vivier48cf6062008-04-29 01:02:46 -0700571 /* wait for something to do */
Wanlong Gaof4507162012-03-28 14:42:51 -0700572 wait_event_interruptible(nbd->waiting_wq,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700573 kthread_should_stop() ||
Wanlong Gaof4507162012-03-28 14:42:51 -0700574 !list_empty(&nbd->waiting_queue));
Laurent Vivier48cf6062008-04-29 01:02:46 -0700575
576 /* extract request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700577 if (list_empty(&nbd->waiting_queue))
Laurent Vivier48cf6062008-04-29 01:02:46 -0700578 continue;
579
Wanlong Gaof4507162012-03-28 14:42:51 -0700580 spin_lock_irq(&nbd->queue_lock);
581 req = list_entry(nbd->waiting_queue.next, struct request,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700582 queuelist);
583 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700584 spin_unlock_irq(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700585
586 /* handle request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700587 nbd_handle_req(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700588 }
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200589
590 nbd->task_send = NULL;
591
Laurent Vivier48cf6062008-04-29 01:02:46 -0700592 return 0;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * We always wait for result of write, for now. It would be nice to make it optional
597 * in future
Wanlong Gaof4507162012-03-28 14:42:51 -0700598 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
600 */
601
Markus Pargmanncad73b22015-08-17 08:20:08 +0200602static void nbd_request_handler(struct request_queue *q)
Alex Elder398eb082013-02-27 17:05:28 -0800603 __releases(q->queue_lock) __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct request *req;
606
Tejun Heo9934c8c2009-05-08 11:54:16 +0900607 while ((req = blk_fetch_request(q)) != NULL) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700608 struct nbd_device *nbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Laurent Vivier48cf6062008-04-29 01:02:46 -0700610 spin_unlock_irq(q->queue_lock);
611
Wanlong Gaof4507162012-03-28 14:42:51 -0700612 nbd = req->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Wanlong Gaof4507162012-03-28 14:42:51 -0700614 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Markus Pargmannd18509f2015-04-02 10:11:38 +0200616 dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n",
617 req, req->cmd_type);
618
Wanlong Gaof4507162012-03-28 14:42:51 -0700619 if (unlikely(!nbd->sock)) {
Dan Streetmanda6ccaa2016-01-14 13:42:32 -0500620 dev_err_ratelimited(disk_to_dev(nbd->disk),
621 "Attempted send on closed socket\n");
Paul Clements4d48a542009-02-11 13:04:45 -0800622 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200623 nbd_end_request(nbd, req);
Paul Clements4d48a542009-02-11 13:04:45 -0800624 spin_lock_irq(q->queue_lock);
625 continue;
626 }
627
Wanlong Gaof4507162012-03-28 14:42:51 -0700628 spin_lock_irq(&nbd->queue_lock);
629 list_add_tail(&req->queuelist, &nbd->waiting_queue);
630 spin_unlock_irq(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Wanlong Gaof4507162012-03-28 14:42:51 -0700632 wake_up(&nbd->waiting_wq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Markus Pargmann23272a672015-10-29 11:51:16 +0100638static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
639{
640 int ret = 0;
641
642 spin_lock_irq(&nbd->sock_lock);
643
644 if (nbd->sock) {
645 ret = -EBUSY;
646 goto out;
647 }
648
649 nbd->sock = sock;
650
651out:
652 spin_unlock_irq(&nbd->sock_lock);
653
654 return ret;
655}
656
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100657/* Reset all properties of an NBD device */
658static void nbd_reset(struct nbd_device *nbd)
659{
660 nbd->disconnect = false;
661 nbd->timedout = false;
662 nbd->blksize = 1024;
663 nbd->bytesize = 0;
664 set_capacity(nbd->disk, 0);
665 nbd->flags = 0;
666 nbd->xmit_timeout = 0;
667 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
668 del_timer_sync(&nbd->timeout_timer);
669}
670
671static void nbd_bdev_reset(struct block_device *bdev)
672{
673 set_device_ro(bdev, false);
674 bdev->bd_inode->i_size = 0;
675 if (max_part > 0) {
676 blkdev_reread_part(bdev);
677 bdev->bd_invalidated = 1;
678 }
679}
680
Markus Pargmannd02cf532015-10-29 12:06:15 +0100681static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
682{
683 if (nbd->flags & NBD_FLAG_READ_ONLY)
684 set_device_ro(bdev, true);
685 if (nbd->flags & NBD_FLAG_SEND_TRIM)
686 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
687 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600688 blk_queue_write_cache(nbd->disk->queue, true, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100689 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600690 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100691}
692
Markus Pargmann30d53d92015-08-17 08:20:06 +0200693static int nbd_dev_dbg_init(struct nbd_device *nbd);
694static void nbd_dev_dbg_close(struct nbd_device *nbd);
695
Pavel Machek1a2ad212009-04-02 16:58:41 -0700696/* Must be called with tx_lock held */
697
Wanlong Gaof4507162012-03-28 14:42:51 -0700698static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700699 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700702 case NBD_DISCONNECT: {
703 struct request sreq;
704
Wanlong Gaof4507162012-03-28 14:42:51 -0700705 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800706 if (!nbd->sock)
707 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700708
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800709 mutex_unlock(&nbd->tx_lock);
710 fsync_bdev(bdev);
711 mutex_lock(&nbd->tx_lock);
FUJITA Tomonori4f54eec2008-04-29 09:54:37 +0200712 blk_rq_init(NULL, &sreq);
Christoph Hellwig4f8c9512015-04-17 22:37:16 +0200713 sreq.cmd_type = REQ_TYPE_DRV_PRIV;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800714
715 /* Check again after getting mutex back. */
Wanlong Gaof4507162012-03-28 14:42:51 -0700716 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -EINVAL;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800718
Markus Pargmann696697c2015-08-17 08:20:07 +0200719 nbd->disconnect = true;
Paul Clementsc378f702013-07-03 15:09:04 -0700720
Wanlong Gaof4507162012-03-28 14:42:51 -0700721 nbd_send_req(nbd, &sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700722 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Markus Pargmann23272a672015-10-29 11:51:16 +0100725 case NBD_CLEAR_SOCK:
726 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700727 nbd_clear_que(nbd);
728 BUG_ON(!list_empty(&nbd->queue_head));
Paul Clementsfded4e02012-09-17 14:09:02 -0700729 BUG_ON(!list_empty(&nbd->waiting_queue));
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800730 kill_bdev(bdev);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700731 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700732
733 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500734 int err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100735 struct socket *sock = sockfd_lookup(arg, &err);
736
737 if (!sock)
738 return err;
739
740 err = nbd_set_socket(nbd, sock);
741 if (!err && max_part)
742 bdev->bd_invalidated = 1;
743
744 return err;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700745 }
746
Markus Pargmann37091fd2015-07-27 07:36:49 +0200747 case NBD_SET_BLKSIZE: {
Arnd Bergmann5e454c62016-03-05 00:49:31 +0100748 loff_t bsize = div_s64(nbd->bytesize, arg);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200749
750 return nbd_size_set(nbd, bdev, arg, bsize);
751 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 case NBD_SET_SIZE:
Markus Pargmann37091fd2015-07-27 07:36:49 +0200754 return nbd_size_set(nbd, bdev, nbd->blksize,
755 arg / nbd->blksize);
756
757 case NBD_SET_SIZE_BLOCKS:
758 return nbd_size_set(nbd, bdev, nbd->blksize, arg);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700759
Paul Clements7fdfd402007-10-16 23:27:37 -0700760 case NBD_SET_TIMEOUT:
Wanlong Gaof4507162012-03-28 14:42:51 -0700761 nbd->xmit_timeout = arg * HZ;
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200762 if (arg)
763 mod_timer(&nbd->timeout_timer,
764 jiffies + nbd->xmit_timeout);
765 else
766 del_timer_sync(&nbd->timeout_timer);
767
Paul Clements7fdfd402007-10-16 23:27:37 -0700768 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700769
Paul Clements2f012502012-10-04 17:16:15 -0700770 case NBD_SET_FLAGS:
771 nbd->flags = arg;
772 return 0;
773
Pavel Machek1a2ad212009-04-02 16:58:41 -0700774 case NBD_DO_IT: {
775 struct task_struct *thread;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700776 int error;
777
Markus Pargmann6521d392015-08-17 08:20:05 +0200778 if (nbd->task_recv)
Pavel Machekc91192d2009-01-15 13:51:03 -0800779 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500780 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700782
Vegard Nossum97240962016-05-27 12:59:35 +0200783 /* We have to claim the device under the lock */
784 nbd->task_recv = current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700785 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700786
Markus Pargmannd02cf532015-10-29 12:06:15 +0100787 nbd_parse_flags(nbd, bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700788
Markus Pargmanncad73b22015-08-17 08:20:08 +0200789 thread = kthread_run(nbd_thread_send, nbd, "%s",
Markus Pargmann30d53d92015-08-17 08:20:06 +0200790 nbd_name(nbd));
Pavel Machek1a2ad212009-04-02 16:58:41 -0700791 if (IS_ERR(thread)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700792 mutex_lock(&nbd->tx_lock);
Vegard Nossum97240962016-05-27 12:59:35 +0200793 nbd->task_recv = NULL;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700794 return PTR_ERR(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700795 }
Markus Pargmannd06df602015-04-02 10:11:36 +0200796
Markus Pargmann30d53d92015-08-17 08:20:06 +0200797 nbd_dev_dbg_init(nbd);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200798 error = nbd_thread_recv(nbd, bdev);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200799 nbd_dev_dbg_close(nbd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700800 kthread_stop(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700801
Wanlong Gaof4507162012-03-28 14:42:51 -0700802 mutex_lock(&nbd->tx_lock);
Vegard Nossum97240962016-05-27 12:59:35 +0200803 nbd->task_recv = NULL;
Markus Pargmann19391832015-08-17 08:20:03 +0200804
Markus Pargmann36e47be2015-08-17 08:20:01 +0200805 sock_shutdown(nbd);
Wanlong Gaof4507162012-03-28 14:42:51 -0700806 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800807 kill_bdev(bdev);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100808 nbd_bdev_reset(bdev);
809
Paul Clementsc378f702013-07-03 15:09:04 -0700810 if (nbd->disconnect) /* user requested, ignore socket errors */
Markus Pargmann1f7b5cf2015-10-29 12:01:34 +0100811 error = 0;
812 if (nbd->timedout)
813 error = -ETIMEDOUT;
814
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100815 nbd_reset(nbd);
816
Markus Pargmann19391832015-08-17 08:20:03 +0200817 return error;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700818 }
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800821 /*
822 * This is for compatibility only. The queue is always cleared
823 * by NBD_DO_IT or NBD_CLEAR_SOCK.
824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 case NBD_PRINT_DEBUG:
Wanlong Gaof4507162012-03-28 14:42:51 -0700828 dev_info(disk_to_dev(nbd->disk),
WANG Cong5eedf542011-08-19 14:48:28 +0200829 "next = %p, prev = %p, head = %p\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700830 nbd->queue_head.next, nbd->queue_head.prev,
831 &nbd->queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700834 return -ENOTTY;
835}
836
837static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
838 unsigned int cmd, unsigned long arg)
839{
Wanlong Gaof4507162012-03-28 14:42:51 -0700840 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700841 int error;
842
843 if (!capable(CAP_SYS_ADMIN))
844 return -EPERM;
845
Wanlong Gaof4507162012-03-28 14:42:51 -0700846 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700847
Wanlong Gaof4507162012-03-28 14:42:51 -0700848 mutex_lock(&nbd->tx_lock);
849 error = __nbd_ioctl(bdev, nbd, cmd, arg);
850 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700851
852 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700855static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200858 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500859 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Markus Pargmann30d53d92015-08-17 08:20:06 +0200862#if IS_ENABLED(CONFIG_DEBUG_FS)
863
864static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
865{
866 struct nbd_device *nbd = s->private;
867
868 if (nbd->task_recv)
869 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
870 if (nbd->task_send)
871 seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send));
872
873 return 0;
874}
875
876static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
877{
878 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
879}
880
881static const struct file_operations nbd_dbg_tasks_ops = {
882 .open = nbd_dbg_tasks_open,
883 .read = seq_read,
884 .llseek = seq_lseek,
885 .release = single_release,
886};
887
888static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
889{
890 struct nbd_device *nbd = s->private;
891 u32 flags = nbd->flags;
892
893 seq_printf(s, "Hex: 0x%08x\n\n", flags);
894
895 seq_puts(s, "Known flags:\n");
896
897 if (flags & NBD_FLAG_HAS_FLAGS)
898 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
899 if (flags & NBD_FLAG_READ_ONLY)
900 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
901 if (flags & NBD_FLAG_SEND_FLUSH)
902 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
903 if (flags & NBD_FLAG_SEND_TRIM)
904 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
905
906 return 0;
907}
908
909static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
910{
911 return single_open(file, nbd_dbg_flags_show, inode->i_private);
912}
913
914static const struct file_operations nbd_dbg_flags_ops = {
915 .open = nbd_dbg_flags_open,
916 .read = seq_read,
917 .llseek = seq_lseek,
918 .release = single_release,
919};
920
921static int nbd_dev_dbg_init(struct nbd_device *nbd)
922{
923 struct dentry *dir;
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200924
925 if (!nbd_dbg_dir)
926 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200927
928 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200929 if (!dir) {
930 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
931 nbd_name(nbd));
932 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200933 }
934 nbd->dbg_dir = dir;
935
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200936 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
937 debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
938 debugfs_create_u32("timeout", 0444, dir, &nbd->xmit_timeout);
939 debugfs_create_u32("blocksize", 0444, dir, &nbd->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -0400940 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200941
942 return 0;
943}
944
945static void nbd_dev_dbg_close(struct nbd_device *nbd)
946{
947 debugfs_remove_recursive(nbd->dbg_dir);
948}
949
950static int nbd_dbg_init(void)
951{
952 struct dentry *dbg_dir;
953
954 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200955 if (!dbg_dir)
956 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200957
958 nbd_dbg_dir = dbg_dir;
959
960 return 0;
961}
962
963static void nbd_dbg_close(void)
964{
965 debugfs_remove_recursive(nbd_dbg_dir);
966}
967
968#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
969
970static int nbd_dev_dbg_init(struct nbd_device *nbd)
971{
972 return 0;
973}
974
975static void nbd_dev_dbg_close(struct nbd_device *nbd)
976{
977}
978
979static int nbd_dbg_init(void)
980{
981 return 0;
982}
983
984static void nbd_dbg_close(void)
985{
986}
987
988#endif
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/*
991 * And here should be modules and kernel interface
992 * (Just smiley confuses emacs :-)
993 */
994
995static int __init nbd_init(void)
996{
997 int err = -ENOMEM;
998 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700999 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08001001 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001003 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02001004 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001005 return -EINVAL;
1006 }
1007
1008 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02001009 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001010 part_shift = fls(max_part);
1011
Namhyung Kim5988ce22011-05-28 14:44:46 +02001012 /*
1013 * Adjust max_part according to part_shift as it is exported
1014 * to user space so that user can know the max number of
1015 * partition kernel should be able to manage.
1016 *
1017 * Note that -1 is required because partition 0 is reserved
1018 * for the whole disk.
1019 */
1020 max_part = (1UL << part_shift) - 1;
1021 }
1022
Namhyung Kim3b271082011-05-28 14:44:46 +02001023 if ((1UL << part_shift) > DISK_MAX_PARTS)
1024 return -EINVAL;
1025
1026 if (nbds_max > 1UL << (MINORBITS - part_shift))
1027 return -EINVAL;
1028
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +05301029 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
1030 if (!nbd_dev)
1031 return -ENOMEM;
1032
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001033 for (i = 0; i < nbds_max; i++) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001034 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (!disk)
1036 goto out;
1037 nbd_dev[i].disk = disk;
1038 /*
1039 * The new linux 2.5 block layer implementation requires
1040 * every gendisk to have its very own request_queue struct.
1041 * These structs are big so we dynamically allocate them.
1042 */
Markus Pargmanncad73b22015-08-17 08:20:08 +02001043 disk->queue = blk_init_queue(nbd_request_handler, &nbd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!disk->queue) {
1045 put_disk(disk);
1046 goto out;
1047 }
Jens Axboe31dcfab2008-10-31 10:06:37 +01001048 /*
1049 * Tell the block layer that we are not a rotational device
1050 */
1051 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -06001052 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -07001053 disk->queue->limits.discard_granularity = 512;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06001054 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Paul Clementsa336d292012-10-04 17:16:18 -07001055 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -07001056 blk_queue_max_hw_sectors(disk->queue, 65536);
1057 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 if (register_blkdev(NBD_MAJOR, "nbd")) {
1061 err = -EIO;
1062 goto out;
1063 }
1064
1065 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Markus Pargmann30d53d92015-08-17 08:20:06 +02001067 nbd_dbg_init();
1068
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001069 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -07001071 nbd_dev[i].magic = NBD_MAGIC;
Laurent Vivier48cf6062008-04-29 01:02:46 -07001072 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 spin_lock_init(&nbd_dev[i].queue_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +01001074 spin_lock_init(&nbd_dev[i].sock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
Ingo Molnar82d4dc52006-03-23 03:00:38 -08001076 mutex_init(&nbd_dev[i].tx_lock);
Markus Pargmann7e2893a2015-08-17 08:20:00 +02001077 init_timer(&nbd_dev[i].timeout_timer);
1078 nbd_dev[i].timeout_timer.function = nbd_xmit_timeout;
1079 nbd_dev[i].timeout_timer.data = (unsigned long)&nbd_dev[i];
Herbert Xu4b2f0262006-01-06 00:09:47 -08001080 init_waitqueue_head(&nbd_dev[i].active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -07001081 init_waitqueue_head(&nbd_dev[i].waiting_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001083 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 disk->fops = &nbd_fops;
1085 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 sprintf(disk->disk_name, "nbd%d", i);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +01001087 nbd_reset(&nbd_dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 add_disk(disk);
1089 }
1090
1091 return 0;
1092out:
1093 while (i--) {
1094 blk_cleanup_queue(nbd_dev[i].disk->queue);
1095 put_disk(nbd_dev[i].disk);
1096 }
Sven Wegenerf3944d62008-08-20 14:09:07 -07001097 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return err;
1099}
1100
1101static void __exit nbd_cleanup(void)
1102{
1103 int i;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001104
1105 nbd_dbg_close();
1106
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001107 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001109 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (disk) {
1111 del_gendisk(disk);
1112 blk_cleanup_queue(disk->queue);
1113 put_disk(disk);
1114 }
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -07001117 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
1119}
1120
1121module_init(nbd_init);
1122module_exit(nbd_cleanup);
1123
1124MODULE_DESCRIPTION("Network Block Device");
1125MODULE_LICENSE("GPL");
1126
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001127module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001128MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1129module_param(max_part, int, 0444);
1130MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");