blob: f3536e68e63fd9afa2e272d406eeac9b6dab9a94 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38#include <asm/types.h>
39
40#include <linux/nbd.h>
41
Markus Pargmann13e71d62015-04-02 10:11:35 +020042struct nbd_device {
43 int flags;
44 int harderror; /* Code of hard error */
45 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 pid_t pid; /* pid of nbd-client, if attached */
60 int xmit_timeout;
61 int disconnect; /* a disconnect has been requested by user */
Markus Pargmann7e2893a2015-08-17 08:20:00 +020062
63 struct timer_list timeout_timer;
64 struct task_struct *task_recv;
65 struct task_struct *task_send;
Markus Pargmann13e71d62015-04-02 10:11:35 +020066};
67
Wanlong Gaof4507162012-03-28 14:42:51 -070068#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Ingo van Lil9c7a4162006-07-01 04:36:36 -070070static unsigned int nbds_max = 16;
Paul Clements20a81432008-02-08 04:21:51 -080071static struct nbd_device *nbd_dev;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070072static int max_part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Use just one lock (or at most 1 per NIC). Two arguments for this:
76 * 1. Each NIC is essentially a synchronization point for all servers
77 * accessed through that NIC so there's no need to have more locks
78 * than NICs anyway.
79 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
80 * down each lock to the point where they're actually slower than just
81 * a single lock.
82 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
83 */
84static DEFINE_SPINLOCK(nbd_lock);
85
Markus Pargmannd18509f2015-04-02 10:11:38 +020086static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Markus Pargmannd18509f2015-04-02 10:11:38 +020088 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91static const char *nbdcmd_to_ascii(int cmd)
92{
93 switch (cmd) {
94 case NBD_CMD_READ: return "read";
95 case NBD_CMD_WRITE: return "write";
96 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -080097 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -070098 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 return "invalid";
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Markus Pargmannd18509f2015-04-02 10:11:38 +0200103static void nbd_end_request(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500105 int error = req->errors ? -EIO : 0;
Jens Axboe165125e2007-07-24 09:28:11 +0200106 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned long flags;
108
Markus Pargmannd18509f2015-04-02 10:11:38 +0200109 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req,
110 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo1011c1b2009-05-07 22:24:45 +0900113 __blk_end_request_all(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 spin_unlock_irqrestore(q->queue_lock, flags);
115}
116
Markus Pargmanne018e752015-04-02 10:11:39 +0200117/*
118 * Forcibly shutdown the socket causing all listeners to error
119 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700120static void sock_shutdown(struct nbd_device *nbd, int lock)
Paul Clements7fdfd402007-10-16 23:27:37 -0700121{
Paul Clements7fdfd402007-10-16 23:27:37 -0700122 if (lock)
Wanlong Gaof4507162012-03-28 14:42:51 -0700123 mutex_lock(&nbd->tx_lock);
124 if (nbd->sock) {
125 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
126 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
127 nbd->sock = NULL;
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200128 del_timer_sync(&nbd->timeout_timer);
Paul Clements7fdfd402007-10-16 23:27:37 -0700129 }
130 if (lock)
Wanlong Gaof4507162012-03-28 14:42:51 -0700131 mutex_unlock(&nbd->tx_lock);
Paul Clements7fdfd402007-10-16 23:27:37 -0700132}
133
134static void nbd_xmit_timeout(unsigned long arg)
135{
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200136 struct nbd_device *nbd = (struct nbd_device *)arg;
137 struct task_struct *task;
Paul Clements7fdfd402007-10-16 23:27:37 -0700138
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200139 if (list_empty(&nbd->queue_head))
140 return;
141
142 nbd->disconnect = 1;
143
144 task = READ_ONCE(nbd->task_recv);
145 if (task)
146 force_sig(SIGKILL, task);
147
148 task = READ_ONCE(nbd->task_send);
149 if (task)
150 force_sig(SIGKILL, nbd->task_send);
151
152 dev_err(nbd_to_dev(nbd), "Connection timed out, killed receiver and sender, shutting down connection\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * Send or receive packet.
157 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700158static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int msg_flags)
160{
Wanlong Gaof4507162012-03-28 14:42:51 -0700161 struct socket *sock = nbd->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int result;
163 struct msghdr msg;
164 struct kvec iov;
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700165 sigset_t blocked, oldset;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700166 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700168 if (unlikely(!sock)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700169 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200170 "Attempted %s on closed socket in sock_xmit\n",
171 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700172 return -EINVAL;
173 }
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Allow interception of SIGKILL only
176 * Don't allow other signals to interrupt the transmission */
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700177 siginitsetinv(&blocked, sigmask(SIGKILL));
178 sigprocmask(SIG_SETMASK, &blocked, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Mel Gorman7f338fe2012-07-31 16:44:32 -0700180 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700182 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 iov.iov_base = buf;
184 iov.iov_len = size;
185 msg.msg_name = NULL;
186 msg.msg_namelen = 0;
187 msg.msg_control = NULL;
188 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
190
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200191 if (send)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200193 else
Namhyung Kim35fbf5b2011-05-28 14:44:46 +0200194 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
195 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (result <= 0) {
198 if (result == 0)
199 result = -EPIPE; /* short read */
200 break;
201 }
202 size -= result;
203 buf += result;
204 } while (size > 0);
205
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700206 sigprocmask(SIG_SETMASK, &oldset, NULL);
Mel Gorman7f338fe2012-07-31 16:44:32 -0700207 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200209 if (!send && nbd->xmit_timeout)
210 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return result;
213}
214
Wanlong Gaof4507162012-03-28 14:42:51 -0700215static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int flags)
217{
218 int result;
219 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700220 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
221 bvec->bv_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 kunmap(bvec->bv_page);
223 return result;
224}
225
Paul Clements7fdfd402007-10-16 23:27:37 -0700226/* always call with the tx_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -0700227static int nbd_send_req(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
NeilBrown5705f702007-09-25 12:35:59 +0200229 int result, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct nbd_request request;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900231 unsigned long size = blk_rq_bytes(req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200232 u32 type;
233
234 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
235 type = NBD_CMD_DISC;
236 else if (req->cmd_flags & REQ_DISCARD)
237 type = NBD_CMD_TRIM;
238 else if (req->cmd_flags & REQ_FLUSH)
239 type = NBD_CMD_FLUSH;
240 else if (rq_data_dir(req) == WRITE)
241 type = NBD_CMD_WRITE;
242 else
243 type = NBD_CMD_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700245 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 request.magic = htonl(NBD_REQUEST_MAGIC);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200247 request.type = htonl(type);
248 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800249 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
250 request.len = htonl(size);
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 memcpy(request.handle, &req, sizeof(req));
253
Markus Pargmannd18509f2015-04-02 10:11:38 +0200254 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200255 req, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200256 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700257 result = sock_xmit(nbd, 1, &request, sizeof(request),
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200258 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700260 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200261 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200262 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200265 if (type == NBD_CMD_WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200266 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800267 struct bio_vec bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /*
269 * we are really probing at internals to determine
270 * whether to set MSG_MORE or not...
271 */
NeilBrown5705f702007-09-25 12:35:59 +0200272 rq_for_each_segment(bvec, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200273 flags = 0;
Kent Overstreet4550dd62013-08-07 14:26:21 -0700274 if (!rq_iter_last(bvec, iter))
Jens Axboe6c92e692007-08-16 13:43:12 +0200275 flags = MSG_MORE;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200276 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
277 req, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800278 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200279 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700280 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200281 "Send data failed (result %d)\n",
282 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200283 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Wanlong Gaof4507162012-03-28 14:42:51 -0700290static struct request *nbd_find_request(struct nbd_device *nbd,
Denis Cheng0cbc591b2007-10-16 23:26:14 -0700291 struct request *xreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Denis Chengd2c97402007-10-16 23:26:14 -0700293 struct request *req, *tmp;
Herbert Xu4b2f0262006-01-06 00:09:47 -0800294 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Wanlong Gaof4507162012-03-28 14:42:51 -0700296 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800297 if (unlikely(err))
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200298 return ERR_PTR(err);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800299
Wanlong Gaof4507162012-03-28 14:42:51 -0700300 spin_lock(&nbd->queue_lock);
301 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (req != xreq)
303 continue;
304 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700305 spin_unlock(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return req;
307 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700308 spin_unlock(&nbd->queue_lock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800309
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200310 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Wanlong Gaof4507162012-03-28 14:42:51 -0700313static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 int result;
316 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700317 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 MSG_WAITALL);
319 kunmap(bvec->bv_page);
320 return result;
321}
322
323/* NULL returned = something went wrong, inform userspace */
Wanlong Gaof4507162012-03-28 14:42:51 -0700324static struct request *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int result;
327 struct nbd_reply reply;
328 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700331 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700333 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200334 "Receive control failed (result %d)\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto harderror;
336 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700337
338 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700339 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700340 (unsigned long)ntohl(reply.magic));
341 result = -EPROTO;
342 goto harderror;
343 }
344
Wanlong Gaof4507162012-03-28 14:42:51 -0700345 req = nbd_find_request(nbd, *(struct request **)reply.handle);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700346 if (IS_ERR(req)) {
Herbert Xu4b2f0262006-01-06 00:09:47 -0800347 result = PTR_ERR(req);
348 if (result != -ENOENT)
349 goto harderror;
350
Wanlong Gaof4507162012-03-28 14:42:51 -0700351 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200352 reply.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 result = -EBADR;
354 goto harderror;
355 }
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700358 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200359 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 req->errors++;
361 return req;
362 }
363
Markus Pargmannd18509f2015-04-02 10:11:38 +0200364 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200365 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200366 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800367 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200368
369 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800370 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200371 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700372 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200373 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200374 req->errors++;
375 return req;
376 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200377 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
378 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 }
381 return req;
382harderror:
Wanlong Gaof4507162012-03-28 14:42:51 -0700383 nbd->harderror = result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return NULL;
385}
386
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200387static ssize_t pid_show(struct device *dev,
388 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800389{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200390 struct gendisk *disk = dev_to_disk(dev);
391
392 return sprintf(buf, "%ld\n",
Paul Clements6b39bb62006-12-06 20:40:53 -0800393 (long) ((struct nbd_device *)disk->private_data)->pid);
394}
395
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200396static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700397 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800398 .show = pid_show,
399};
400
Wanlong Gaof4507162012-03-28 14:42:51 -0700401static int nbd_do_it(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct request *req;
WANG Cong84963042007-05-09 02:33:36 -0700404 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Wanlong Gaof4507162012-03-28 14:42:51 -0700406 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Mel Gorman7f338fe2012-07-31 16:44:32 -0700408 sk_set_memalloc(nbd->sock->sk);
Wanlong Gaof4507162012-03-28 14:42:51 -0700409 nbd->pid = task_pid_nr(current);
410 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700411 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700412 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
413 nbd->pid = 0;
WANG Cong84963042007-05-09 02:33:36 -0700414 return ret;
415 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800416
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200417 nbd->task_recv = current;
418
Wanlong Gaof4507162012-03-28 14:42:51 -0700419 while ((req = nbd_read_stat(nbd)) != NULL)
Markus Pargmannd18509f2015-04-02 10:11:38 +0200420 nbd_end_request(nbd, req);
Paul Clements6b39bb62006-12-06 20:40:53 -0800421
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200422 nbd->task_recv = NULL;
423
424 if (signal_pending(current)) {
425 siginfo_t info;
426
427 ret = dequeue_signal_lock(current, &current->blocked, &info);
428 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
429 task_pid_nr(current), current->comm, ret);
430 sock_shutdown(nbd, 1);
431 ret = -ETIMEDOUT;
432 }
433
Wanlong Gaof4507162012-03-28 14:42:51 -0700434 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
435 nbd->pid = 0;
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200436 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Wanlong Gaof4507162012-03-28 14:42:51 -0700439static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct request *req;
442
Wanlong Gaof4507162012-03-28 14:42:51 -0700443 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Herbert Xu4b2f0262006-01-06 00:09:47 -0800445 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700446 * Because we have set nbd->sock to NULL under the tx_lock, all
Herbert Xu4b2f0262006-01-06 00:09:47 -0800447 * modifications to the list must have completed by now. For
448 * the same reason, the active_req must be NULL.
449 *
450 * As a consequence, we don't need to take the spin lock while
451 * purging the list here.
452 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700453 BUG_ON(nbd->sock);
454 BUG_ON(nbd->active_req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800455
Wanlong Gaof4507162012-03-28 14:42:51 -0700456 while (!list_empty(&nbd->queue_head)) {
457 req = list_entry(nbd->queue_head.next, struct request,
Herbert Xu4b2f0262006-01-06 00:09:47 -0800458 queuelist);
459 list_del_init(&req->queuelist);
460 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200461 nbd_end_request(nbd, req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800462 }
Paul Clementsfded4e02012-09-17 14:09:02 -0700463
464 while (!list_empty(&nbd->waiting_queue)) {
465 req = list_entry(nbd->waiting_queue.next, struct request,
466 queuelist);
467 list_del_init(&req->queuelist);
468 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200469 nbd_end_request(nbd, req);
Paul Clementsfded4e02012-09-17 14:09:02 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Paul Clements7fdfd402007-10-16 23:27:37 -0700473
Wanlong Gaof4507162012-03-28 14:42:51 -0700474static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700475{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200476 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700477 goto error_out;
478
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200479 if (rq_data_dir(req) == WRITE &&
480 (nbd->flags & NBD_FLAG_READ_ONLY)) {
481 dev_err(disk_to_dev(nbd->disk),
482 "Write on read-only\n");
483 goto error_out;
Alex Bligh75f187a2013-02-27 17:05:23 -0800484 }
485
Laurent Vivier48cf6062008-04-29 01:02:46 -0700486 req->errors = 0;
487
Wanlong Gaof4507162012-03-28 14:42:51 -0700488 mutex_lock(&nbd->tx_lock);
489 if (unlikely(!nbd->sock)) {
490 mutex_unlock(&nbd->tx_lock);
491 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200492 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700493 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700494 }
495
Wanlong Gaof4507162012-03-28 14:42:51 -0700496 nbd->active_req = req;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700497
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200498 if (nbd->xmit_timeout && list_empty_careful(&nbd->queue_head))
499 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
500
Wanlong Gaof4507162012-03-28 14:42:51 -0700501 if (nbd_send_req(nbd, req) != 0) {
502 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700503 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200504 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700505 } else {
Wanlong Gaof4507162012-03-28 14:42:51 -0700506 spin_lock(&nbd->queue_lock);
Chetan Loke01ff5db2012-07-31 08:47:13 +0200507 list_add_tail(&req->queuelist, &nbd->queue_head);
Wanlong Gaof4507162012-03-28 14:42:51 -0700508 spin_unlock(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700509 }
510
Wanlong Gaof4507162012-03-28 14:42:51 -0700511 nbd->active_req = NULL;
512 mutex_unlock(&nbd->tx_lock);
513 wake_up_all(&nbd->active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700514
515 return;
516
517error_out:
518 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200519 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700520}
521
522static int nbd_thread(void *data)
523{
Wanlong Gaof4507162012-03-28 14:42:51 -0700524 struct nbd_device *nbd = data;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700525 struct request *req;
526
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200527 nbd->task_send = current;
528
Dongsheng Yang8698a742014-03-11 18:09:12 +0800529 set_user_nice(current, MIN_NICE);
Wanlong Gaof4507162012-03-28 14:42:51 -0700530 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
Laurent Vivier48cf6062008-04-29 01:02:46 -0700531 /* wait for something to do */
Wanlong Gaof4507162012-03-28 14:42:51 -0700532 wait_event_interruptible(nbd->waiting_wq,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700533 kthread_should_stop() ||
Wanlong Gaof4507162012-03-28 14:42:51 -0700534 !list_empty(&nbd->waiting_queue));
Laurent Vivier48cf6062008-04-29 01:02:46 -0700535
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200536 if (signal_pending(current)) {
537 siginfo_t info;
538 int ret;
539
540 ret = dequeue_signal_lock(current, &current->blocked,
541 &info);
542 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
543 task_pid_nr(current), current->comm, ret);
544 sock_shutdown(nbd, 1);
545 break;
546 }
547
Laurent Vivier48cf6062008-04-29 01:02:46 -0700548 /* extract request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700549 if (list_empty(&nbd->waiting_queue))
Laurent Vivier48cf6062008-04-29 01:02:46 -0700550 continue;
551
Wanlong Gaof4507162012-03-28 14:42:51 -0700552 spin_lock_irq(&nbd->queue_lock);
553 req = list_entry(nbd->waiting_queue.next, struct request,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700554 queuelist);
555 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700556 spin_unlock_irq(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700557
558 /* handle request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700559 nbd_handle_req(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700560 }
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200561
562 nbd->task_send = NULL;
563
Laurent Vivier48cf6062008-04-29 01:02:46 -0700564 return 0;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
568 * We always wait for result of write, for now. It would be nice to make it optional
569 * in future
Wanlong Gaof4507162012-03-28 14:42:51 -0700570 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
572 */
573
Pavel Machek15746fc2009-04-02 16:58:42 -0700574static void do_nbd_request(struct request_queue *q)
Alex Elder398eb082013-02-27 17:05:28 -0800575 __releases(q->queue_lock) __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct request *req;
578
Tejun Heo9934c8c2009-05-08 11:54:16 +0900579 while ((req = blk_fetch_request(q)) != NULL) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700580 struct nbd_device *nbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Laurent Vivier48cf6062008-04-29 01:02:46 -0700582 spin_unlock_irq(q->queue_lock);
583
Wanlong Gaof4507162012-03-28 14:42:51 -0700584 nbd = req->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Wanlong Gaof4507162012-03-28 14:42:51 -0700586 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Markus Pargmannd18509f2015-04-02 10:11:38 +0200588 dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n",
589 req, req->cmd_type);
590
Wanlong Gaof4507162012-03-28 14:42:51 -0700591 if (unlikely(!nbd->sock)) {
592 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200593 "Attempted send on closed socket\n");
Paul Clements4d48a542009-02-11 13:04:45 -0800594 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200595 nbd_end_request(nbd, req);
Paul Clements4d48a542009-02-11 13:04:45 -0800596 spin_lock_irq(q->queue_lock);
597 continue;
598 }
599
Wanlong Gaof4507162012-03-28 14:42:51 -0700600 spin_lock_irq(&nbd->queue_lock);
601 list_add_tail(&req->queuelist, &nbd->waiting_queue);
602 spin_unlock_irq(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Wanlong Gaof4507162012-03-28 14:42:51 -0700604 wake_up(&nbd->waiting_wq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Pavel Machek1a2ad212009-04-02 16:58:41 -0700610/* Must be called with tx_lock held */
611
Wanlong Gaof4507162012-03-28 14:42:51 -0700612static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700613 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700616 case NBD_DISCONNECT: {
617 struct request sreq;
618
Wanlong Gaof4507162012-03-28 14:42:51 -0700619 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800620 if (!nbd->sock)
621 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700622
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800623 mutex_unlock(&nbd->tx_lock);
624 fsync_bdev(bdev);
625 mutex_lock(&nbd->tx_lock);
FUJITA Tomonori4f54eec2008-04-29 09:54:37 +0200626 blk_rq_init(NULL, &sreq);
Christoph Hellwig4f8c9512015-04-17 22:37:16 +0200627 sreq.cmd_type = REQ_TYPE_DRV_PRIV;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800628
629 /* Check again after getting mutex back. */
Wanlong Gaof4507162012-03-28 14:42:51 -0700630 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -EINVAL;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800632
Paul Clementsc378f702013-07-03 15:09:04 -0700633 nbd->disconnect = 1;
634
Wanlong Gaof4507162012-03-28 14:42:51 -0700635 nbd_send_req(nbd, &sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700636 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Pavel Machek1a2ad212009-04-02 16:58:41 -0700639 case NBD_CLEAR_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500640 struct socket *sock = nbd->sock;
Wanlong Gaof4507162012-03-28 14:42:51 -0700641 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700642 nbd_clear_que(nbd);
643 BUG_ON(!list_empty(&nbd->queue_head));
Paul Clementsfded4e02012-09-17 14:09:02 -0700644 BUG_ON(!list_empty(&nbd->waiting_queue));
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800645 kill_bdev(bdev);
Al Viroe2511572014-03-05 20:41:36 -0500646 if (sock)
647 sockfd_put(sock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700648 return 0;
649 }
650
651 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500652 struct socket *sock;
653 int err;
654 if (nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500656 sock = sockfd_lookup(arg, &err);
657 if (sock) {
658 nbd->sock = sock;
659 if (max_part > 0)
660 bdev->bd_invalidated = 1;
661 nbd->disconnect = 0; /* we're connected now */
662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700664 return -EINVAL;
665 }
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 case NBD_SET_BLKSIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700668 nbd->blksize = arg;
669 nbd->bytesize &= ~(nbd->blksize-1);
670 bdev->bd_inode->i_size = nbd->bytesize;
671 set_blocksize(bdev, nbd->blksize);
672 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 case NBD_SET_SIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700676 nbd->bytesize = arg & ~(nbd->blksize-1);
677 bdev->bd_inode->i_size = nbd->bytesize;
678 set_blocksize(bdev, nbd->blksize);
679 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700681
Paul Clements7fdfd402007-10-16 23:27:37 -0700682 case NBD_SET_TIMEOUT:
Wanlong Gaof4507162012-03-28 14:42:51 -0700683 nbd->xmit_timeout = arg * HZ;
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200684 if (arg)
685 mod_timer(&nbd->timeout_timer,
686 jiffies + nbd->xmit_timeout);
687 else
688 del_timer_sync(&nbd->timeout_timer);
689
Paul Clements7fdfd402007-10-16 23:27:37 -0700690 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700691
Paul Clements2f012502012-10-04 17:16:15 -0700692 case NBD_SET_FLAGS:
693 nbd->flags = arg;
694 return 0;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 case NBD_SET_SIZE_BLOCKS:
Wanlong Gaof4507162012-03-28 14:42:51 -0700697 nbd->bytesize = ((u64) arg) * nbd->blksize;
698 bdev->bd_inode->i_size = nbd->bytesize;
699 set_blocksize(bdev, nbd->blksize);
700 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700702
703 case NBD_DO_IT: {
704 struct task_struct *thread;
Al Viroe2511572014-03-05 20:41:36 -0500705 struct socket *sock;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700706 int error;
707
Wanlong Gaof4507162012-03-28 14:42:51 -0700708 if (nbd->pid)
Pavel Machekc91192d2009-01-15 13:51:03 -0800709 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500710 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700712
Wanlong Gaof4507162012-03-28 14:42:51 -0700713 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700714
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800715 if (nbd->flags & NBD_FLAG_READ_ONLY)
716 set_device_ro(bdev, true);
Paul Clementsa336d292012-10-04 17:16:18 -0700717 if (nbd->flags & NBD_FLAG_SEND_TRIM)
718 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
719 nbd->disk->queue);
Alex Bligh75f187a2013-02-27 17:05:23 -0800720 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
721 blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
722 else
723 blk_queue_flush(nbd->disk->queue, 0);
Paul Clementsa336d292012-10-04 17:16:18 -0700724
Markus Pargmannd06df602015-04-02 10:11:36 +0200725 thread = kthread_run(nbd_thread, nbd, "%s",
726 nbd->disk->disk_name);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700727 if (IS_ERR(thread)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700728 mutex_lock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700729 return PTR_ERR(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700730 }
Markus Pargmannd06df602015-04-02 10:11:36 +0200731
Wanlong Gaof4507162012-03-28 14:42:51 -0700732 error = nbd_do_it(nbd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700733 kthread_stop(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700734
Wanlong Gaof4507162012-03-28 14:42:51 -0700735 mutex_lock(&nbd->tx_lock);
WANG Cong84963042007-05-09 02:33:36 -0700736 if (error)
737 return error;
Wanlong Gaof4507162012-03-28 14:42:51 -0700738 sock_shutdown(nbd, 0);
Al Viroe2511572014-03-05 20:41:36 -0500739 sock = nbd->sock;
740 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700741 nbd_clear_que(nbd);
742 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800743 kill_bdev(bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700744 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800745 set_device_ro(bdev, false);
Al Viroe2511572014-03-05 20:41:36 -0500746 if (sock)
747 sockfd_put(sock);
Alex Bligh75f187a2013-02-27 17:05:23 -0800748 nbd->flags = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700749 nbd->bytesize = 0;
Al Viroa8cdc302008-03-02 09:33:33 -0500750 bdev->bd_inode->i_size = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700751 set_capacity(nbd->disk, 0);
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700752 if (max_part > 0)
Ming Lei9dcd1372015-05-06 12:26:25 +0800753 blkdev_reread_part(bdev);
Paul Clementsc378f702013-07-03 15:09:04 -0700754 if (nbd->disconnect) /* user requested, ignore socket errors */
755 return 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700756 return nbd->harderror;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700757 }
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800760 /*
761 * This is for compatibility only. The queue is always cleared
762 * by NBD_DO_IT or NBD_CLEAR_SOCK.
763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case NBD_PRINT_DEBUG:
Wanlong Gaof4507162012-03-28 14:42:51 -0700767 dev_info(disk_to_dev(nbd->disk),
WANG Cong5eedf542011-08-19 14:48:28 +0200768 "next = %p, prev = %p, head = %p\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700769 nbd->queue_head.next, nbd->queue_head.prev,
770 &nbd->queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
772 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700773 return -ENOTTY;
774}
775
776static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
777 unsigned int cmd, unsigned long arg)
778{
Wanlong Gaof4507162012-03-28 14:42:51 -0700779 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700780 int error;
781
782 if (!capable(CAP_SYS_ADMIN))
783 return -EPERM;
784
Wanlong Gaof4507162012-03-28 14:42:51 -0700785 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700786
Wanlong Gaof4507162012-03-28 14:42:51 -0700787 mutex_lock(&nbd->tx_lock);
788 error = __nbd_ioctl(bdev, nbd, cmd, arg);
789 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700790
791 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700794static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200797 .ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
799
800/*
801 * And here should be modules and kernel interface
802 * (Just smiley confuses emacs :-)
803 */
804
805static int __init nbd_init(void)
806{
807 int err = -ENOMEM;
808 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700809 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800811 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700813 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +0200814 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700815 return -EINVAL;
816 }
817
818 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +0200819 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700820 part_shift = fls(max_part);
821
Namhyung Kim5988ce22011-05-28 14:44:46 +0200822 /*
823 * Adjust max_part according to part_shift as it is exported
824 * to user space so that user can know the max number of
825 * partition kernel should be able to manage.
826 *
827 * Note that -1 is required because partition 0 is reserved
828 * for the whole disk.
829 */
830 max_part = (1UL << part_shift) - 1;
831 }
832
Namhyung Kim3b271082011-05-28 14:44:46 +0200833 if ((1UL << part_shift) > DISK_MAX_PARTS)
834 return -EINVAL;
835
836 if (nbds_max > 1UL << (MINORBITS - part_shift))
837 return -EINVAL;
838
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +0530839 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
840 if (!nbd_dev)
841 return -ENOMEM;
842
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700843 for (i = 0; i < nbds_max; i++) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700844 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (!disk)
846 goto out;
847 nbd_dev[i].disk = disk;
848 /*
849 * The new linux 2.5 block layer implementation requires
850 * every gendisk to have its very own request_queue struct.
851 * These structs are big so we dynamically allocate them.
852 */
853 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
854 if (!disk->queue) {
855 put_disk(disk);
856 goto out;
857 }
Jens Axboe31dcfab2008-10-31 10:06:37 +0100858 /*
859 * Tell the block layer that we are not a rotational device
860 */
861 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600862 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -0700863 disk->queue->limits.discard_granularity = 512;
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600864 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Paul Clementsa336d292012-10-04 17:16:18 -0700865 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -0700866 blk_queue_max_hw_sectors(disk->queue, 65536);
867 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
870 if (register_blkdev(NBD_MAJOR, "nbd")) {
871 err = -EIO;
872 goto out;
873 }
874
875 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700877 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -0700879 nbd_dev[i].magic = NBD_MAGIC;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700880 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 spin_lock_init(&nbd_dev[i].queue_lock);
882 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
Ingo Molnar82d4dc52006-03-23 03:00:38 -0800883 mutex_init(&nbd_dev[i].tx_lock);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200884 init_timer(&nbd_dev[i].timeout_timer);
885 nbd_dev[i].timeout_timer.function = nbd_xmit_timeout;
886 nbd_dev[i].timeout_timer.data = (unsigned long)&nbd_dev[i];
Herbert Xu4b2f0262006-01-06 00:09:47 -0800887 init_waitqueue_head(&nbd_dev[i].active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700888 init_waitqueue_head(&nbd_dev[i].waiting_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 nbd_dev[i].blksize = 1024;
Paul Clements4b86a872007-10-16 23:27:36 -0700890 nbd_dev[i].bytesize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700892 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 disk->fops = &nbd_fops;
894 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sprintf(disk->disk_name, "nbd%d", i);
Paul Clements4b86a872007-10-16 23:27:36 -0700896 set_capacity(disk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 add_disk(disk);
898 }
899
900 return 0;
901out:
902 while (i--) {
903 blk_cleanup_queue(nbd_dev[i].disk->queue);
904 put_disk(nbd_dev[i].disk);
905 }
Sven Wegenerf3944d62008-08-20 14:09:07 -0700906 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return err;
908}
909
910static void __exit nbd_cleanup(void)
911{
912 int i;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700913 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700915 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (disk) {
917 del_gendisk(disk);
918 blk_cleanup_queue(disk->queue);
919 put_disk(disk);
920 }
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -0700923 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
925}
926
927module_init(nbd_init);
928module_exit(nbd_cleanup);
929
930MODULE_DESCRIPTION("Network Block Device");
931MODULE_LICENSE("GPL");
932
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700933module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700934MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
935module_param(max_part, int, 0444);
936MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");