blob: 7139c8aae7a12e70a786e34f3d8771ff15d06d95 [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 */
62};
63
Wanlong Gaof4507162012-03-28 14:42:51 -070064#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#ifdef NDEBUG
67#define dprintk(flags, fmt...)
68#else /* NDEBUG */
69#define dprintk(flags, fmt...) do { \
70 if (debugflags & (flags)) printk(KERN_DEBUG fmt); \
71} while (0)
72#define DBG_IOCTL 0x0004
73#define DBG_INIT 0x0010
74#define DBG_EXIT 0x0020
75#define DBG_BLKDEV 0x0100
76#define DBG_RX 0x0200
77#define DBG_TX 0x0400
78static unsigned int debugflags;
79#endif /* NDEBUG */
80
Ingo van Lil9c7a4162006-07-01 04:36:36 -070081static unsigned int nbds_max = 16;
Paul Clements20a81432008-02-08 04:21:51 -080082static struct nbd_device *nbd_dev;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070083static int max_part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Use just one lock (or at most 1 per NIC). Two arguments for this:
87 * 1. Each NIC is essentially a synchronization point for all servers
88 * accessed through that NIC so there's no need to have more locks
89 * than NICs anyway.
90 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
91 * down each lock to the point where they're actually slower than just
92 * a single lock.
93 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
94 */
95static DEFINE_SPINLOCK(nbd_lock);
96
97#ifndef NDEBUG
98static const char *ioctl_cmd_to_ascii(int cmd)
99{
100 switch (cmd) {
101 case NBD_SET_SOCK: return "set-sock";
102 case NBD_SET_BLKSIZE: return "set-blksize";
103 case NBD_SET_SIZE: return "set-size";
Paul Clements2f012502012-10-04 17:16:15 -0700104 case NBD_SET_TIMEOUT: return "set-timeout";
105 case NBD_SET_FLAGS: return "set-flags";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 case NBD_DO_IT: return "do-it";
107 case NBD_CLEAR_SOCK: return "clear-sock";
108 case NBD_CLEAR_QUE: return "clear-que";
109 case NBD_PRINT_DEBUG: return "print-debug";
110 case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
111 case NBD_DISCONNECT: return "disconnect";
112 case BLKROSET: return "set-read-only";
113 case BLKFLSBUF: return "flush-buffer-cache";
114 }
115 return "unknown";
116}
117
118static const char *nbdcmd_to_ascii(int cmd)
119{
120 switch (cmd) {
121 case NBD_CMD_READ: return "read";
122 case NBD_CMD_WRITE: return "write";
123 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800124 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700125 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 return "invalid";
128}
129#endif /* NDEBUG */
130
131static void nbd_end_request(struct request *req)
132{
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500133 int error = req->errors ? -EIO : 0;
Jens Axboe165125e2007-07-24 09:28:11 +0200134 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned long flags;
136
137 dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name,
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500138 req, error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo1011c1b2009-05-07 22:24:45 +0900141 __blk_end_request_all(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spin_unlock_irqrestore(q->queue_lock, flags);
143}
144
Wanlong Gaof4507162012-03-28 14:42:51 -0700145static void sock_shutdown(struct nbd_device *nbd, int lock)
Paul Clements7fdfd402007-10-16 23:27:37 -0700146{
147 /* Forcibly shutdown the socket causing all listeners
148 * to error
149 *
150 * FIXME: This code is duplicated from sys_shutdown, but
151 * there should be a more generic interface rather than
152 * calling socket ops directly here */
153 if (lock)
Wanlong Gaof4507162012-03-28 14:42:51 -0700154 mutex_lock(&nbd->tx_lock);
155 if (nbd->sock) {
156 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
157 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
158 nbd->sock = NULL;
Paul Clements7fdfd402007-10-16 23:27:37 -0700159 }
160 if (lock)
Wanlong Gaof4507162012-03-28 14:42:51 -0700161 mutex_unlock(&nbd->tx_lock);
Paul Clements7fdfd402007-10-16 23:27:37 -0700162}
163
164static void nbd_xmit_timeout(unsigned long arg)
165{
166 struct task_struct *task = (struct task_struct *)arg;
167
168 printk(KERN_WARNING "nbd: killing hung xmit (%s, pid: %d)\n",
169 task->comm, task->pid);
170 force_sig(SIGKILL, task);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * Send or receive packet.
175 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700176static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int msg_flags)
178{
Wanlong Gaof4507162012-03-28 14:42:51 -0700179 struct socket *sock = nbd->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int result;
181 struct msghdr msg;
182 struct kvec iov;
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700183 sigset_t blocked, oldset;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700184 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700186 if (unlikely(!sock)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700187 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200188 "Attempted %s on closed socket in sock_xmit\n",
189 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700190 return -EINVAL;
191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Allow interception of SIGKILL only
194 * Don't allow other signals to interrupt the transmission */
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700195 siginitsetinv(&blocked, sigmask(SIGKILL));
196 sigprocmask(SIG_SETMASK, &blocked, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Mel Gorman7f338fe2012-07-31 16:44:32 -0700198 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700200 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 iov.iov_base = buf;
202 iov.iov_len = size;
203 msg.msg_name = NULL;
204 msg.msg_namelen = 0;
205 msg.msg_control = NULL;
206 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
208
Paul Clements7fdfd402007-10-16 23:27:37 -0700209 if (send) {
210 struct timer_list ti;
211
Wanlong Gaof4507162012-03-28 14:42:51 -0700212 if (nbd->xmit_timeout) {
Paul Clements7fdfd402007-10-16 23:27:37 -0700213 init_timer(&ti);
214 ti.function = nbd_xmit_timeout;
215 ti.data = (unsigned long)current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700216 ti.expires = jiffies + nbd->xmit_timeout;
Paul Clements7fdfd402007-10-16 23:27:37 -0700217 add_timer(&ti);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
Wanlong Gaof4507162012-03-28 14:42:51 -0700220 if (nbd->xmit_timeout)
Paul Clements7fdfd402007-10-16 23:27:37 -0700221 del_timer_sync(&ti);
222 } else
Namhyung Kim35fbf5b2011-05-28 14:44:46 +0200223 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
224 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (signal_pending(current)) {
227 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700229 task_pid_nr(current), current->comm,
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700230 dequeue_signal_lock(current, &current->blocked, &info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 result = -EINTR;
Wanlong Gaof4507162012-03-28 14:42:51 -0700232 sock_shutdown(nbd, !send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234 }
235
236 if (result <= 0) {
237 if (result == 0)
238 result = -EPIPE; /* short read */
239 break;
240 }
241 size -= result;
242 buf += result;
243 } while (size > 0);
244
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700245 sigprocmask(SIG_SETMASK, &oldset, NULL);
Mel Gorman7f338fe2012-07-31 16:44:32 -0700246 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 return result;
249}
250
Wanlong Gaof4507162012-03-28 14:42:51 -0700251static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int flags)
253{
254 int result;
255 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700256 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
257 bvec->bv_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 kunmap(bvec->bv_page);
259 return result;
260}
261
Paul Clements7fdfd402007-10-16 23:27:37 -0700262/* always call with the tx_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -0700263static int nbd_send_req(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
NeilBrown5705f702007-09-25 12:35:59 +0200265 int result, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct nbd_request request;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900267 unsigned long size = blk_rq_bytes(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700269 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 request.magic = htonl(NBD_REQUEST_MAGIC);
271 request.type = htonl(nbd_cmd(req));
Alex Bligh75f187a2013-02-27 17:05:23 -0800272
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700273 if (nbd_cmd(req) != NBD_CMD_FLUSH && nbd_cmd(req) != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800274 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
275 request.len = htonl(size);
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 memcpy(request.handle, &req, sizeof(req));
278
Tejun Heo83096eb2009-05-07 22:24:39 +0900279 dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%uB)\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700280 nbd->disk->disk_name, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 nbdcmd_to_ascii(nbd_cmd(req)),
Tejun Heo83096eb2009-05-07 22:24:39 +0900282 (unsigned long long)blk_rq_pos(req) << 9,
Tejun Heo1011c1b2009-05-07 22:24:45 +0900283 blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700284 result = sock_xmit(nbd, 1, &request, sizeof(request),
Paul Clements7fdfd402007-10-16 23:27:37 -0700285 (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700287 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200288 "Send control failed (result %d)\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto error_out;
290 }
291
292 if (nbd_cmd(req) == NBD_CMD_WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200293 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800294 struct bio_vec bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /*
296 * we are really probing at internals to determine
297 * whether to set MSG_MORE or not...
298 */
NeilBrown5705f702007-09-25 12:35:59 +0200299 rq_for_each_segment(bvec, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200300 flags = 0;
Kent Overstreet4550dd62013-08-07 14:26:21 -0700301 if (!rq_iter_last(bvec, iter))
Jens Axboe6c92e692007-08-16 13:43:12 +0200302 flags = MSG_MORE;
303 dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n",
Kent Overstreet79886132013-11-23 17:19:00 -0800304 nbd->disk->disk_name, req, bvec.bv_len);
305 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200306 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700307 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200308 "Send data failed (result %d)\n",
309 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200310 goto error_out;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315
316error_out:
Pavel Machek15746fc2009-04-02 16:58:42 -0700317 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Wanlong Gaof4507162012-03-28 14:42:51 -0700320static struct request *nbd_find_request(struct nbd_device *nbd,
Denis Cheng0cbc591b2007-10-16 23:26:14 -0700321 struct request *xreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Denis Chengd2c97402007-10-16 23:26:14 -0700323 struct request *req, *tmp;
Herbert Xu4b2f0262006-01-06 00:09:47 -0800324 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Wanlong Gaof4507162012-03-28 14:42:51 -0700326 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800327 if (unlikely(err))
328 goto out;
329
Wanlong Gaof4507162012-03-28 14:42:51 -0700330 spin_lock(&nbd->queue_lock);
331 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (req != xreq)
333 continue;
334 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700335 spin_unlock(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return req;
337 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700338 spin_unlock(&nbd->queue_lock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800339
340 err = -ENOENT;
341
342out:
343 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Wanlong Gaof4507162012-03-28 14:42:51 -0700346static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int result;
349 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700350 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 MSG_WAITALL);
352 kunmap(bvec->bv_page);
353 return result;
354}
355
356/* NULL returned = something went wrong, inform userspace */
Wanlong Gaof4507162012-03-28 14:42:51 -0700357static struct request *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int result;
360 struct nbd_reply reply;
361 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700364 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700366 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200367 "Receive control failed (result %d)\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto harderror;
369 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700370
371 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700372 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700373 (unsigned long)ntohl(reply.magic));
374 result = -EPROTO;
375 goto harderror;
376 }
377
Wanlong Gaof4507162012-03-28 14:42:51 -0700378 req = nbd_find_request(nbd, *(struct request **)reply.handle);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700379 if (IS_ERR(req)) {
Herbert Xu4b2f0262006-01-06 00:09:47 -0800380 result = PTR_ERR(req);
381 if (result != -ENOENT)
382 goto harderror;
383
Wanlong Gaof4507162012-03-28 14:42:51 -0700384 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200385 reply.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 result = -EBADR;
387 goto harderror;
388 }
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700391 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200392 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 req->errors++;
394 return req;
395 }
396
397 dprintk(DBG_RX, "%s: request %p: got reply\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700398 nbd->disk->disk_name, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (nbd_cmd(req) == NBD_CMD_READ) {
NeilBrown5705f702007-09-25 12:35:59 +0200400 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800401 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200402
403 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800404 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200405 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700406 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200407 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200408 req->errors++;
409 return req;
410 }
411 dprintk(DBG_RX, "%s: request %p: got %d bytes data\n",
Kent Overstreet79886132013-11-23 17:19:00 -0800412 nbd->disk->disk_name, req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415 return req;
416harderror:
Wanlong Gaof4507162012-03-28 14:42:51 -0700417 nbd->harderror = result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return NULL;
419}
420
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200421static ssize_t pid_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800423{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200424 struct gendisk *disk = dev_to_disk(dev);
425
426 return sprintf(buf, "%ld\n",
Paul Clements6b39bb62006-12-06 20:40:53 -0800427 (long) ((struct nbd_device *)disk->private_data)->pid);
428}
429
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200430static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700431 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800432 .show = pid_show,
433};
434
Wanlong Gaof4507162012-03-28 14:42:51 -0700435static int nbd_do_it(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct request *req;
WANG Cong84963042007-05-09 02:33:36 -0700438 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Wanlong Gaof4507162012-03-28 14:42:51 -0700440 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Mel Gorman7f338fe2012-07-31 16:44:32 -0700442 sk_set_memalloc(nbd->sock->sk);
Wanlong Gaof4507162012-03-28 14:42:51 -0700443 nbd->pid = task_pid_nr(current);
444 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700445 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700446 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
447 nbd->pid = 0;
WANG Cong84963042007-05-09 02:33:36 -0700448 return ret;
449 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800450
Wanlong Gaof4507162012-03-28 14:42:51 -0700451 while ((req = nbd_read_stat(nbd)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 nbd_end_request(req);
Paul Clements6b39bb62006-12-06 20:40:53 -0800453
Wanlong Gaof4507162012-03-28 14:42:51 -0700454 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
455 nbd->pid = 0;
WANG Cong84963042007-05-09 02:33:36 -0700456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Wanlong Gaof4507162012-03-28 14:42:51 -0700459static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct request *req;
462
Wanlong Gaof4507162012-03-28 14:42:51 -0700463 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Herbert Xu4b2f0262006-01-06 00:09:47 -0800465 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700466 * Because we have set nbd->sock to NULL under the tx_lock, all
Herbert Xu4b2f0262006-01-06 00:09:47 -0800467 * modifications to the list must have completed by now. For
468 * the same reason, the active_req must be NULL.
469 *
470 * As a consequence, we don't need to take the spin lock while
471 * purging the list here.
472 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700473 BUG_ON(nbd->sock);
474 BUG_ON(nbd->active_req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800475
Wanlong Gaof4507162012-03-28 14:42:51 -0700476 while (!list_empty(&nbd->queue_head)) {
477 req = list_entry(nbd->queue_head.next, struct request,
Herbert Xu4b2f0262006-01-06 00:09:47 -0800478 queuelist);
479 list_del_init(&req->queuelist);
480 req->errors++;
481 nbd_end_request(req);
482 }
Paul Clementsfded4e02012-09-17 14:09:02 -0700483
484 while (!list_empty(&nbd->waiting_queue)) {
485 req = list_entry(nbd->waiting_queue.next, struct request,
486 queuelist);
487 list_del_init(&req->queuelist);
488 req->errors++;
489 nbd_end_request(req);
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Paul Clements7fdfd402007-10-16 23:27:37 -0700493
Wanlong Gaof4507162012-03-28 14:42:51 -0700494static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700495{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200496 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700497 goto error_out;
498
499 nbd_cmd(req) = NBD_CMD_READ;
500 if (rq_data_dir(req) == WRITE) {
Paul Clementsa336d292012-10-04 17:16:18 -0700501 if ((req->cmd_flags & REQ_DISCARD)) {
502 WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM));
503 nbd_cmd(req) = NBD_CMD_TRIM;
504 } else
505 nbd_cmd(req) = NBD_CMD_WRITE;
Paul Clements2f012502012-10-04 17:16:15 -0700506 if (nbd->flags & NBD_FLAG_READ_ONLY) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700507 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200508 "Write on read-only\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700509 goto error_out;
510 }
511 }
512
Alex Bligh75f187a2013-02-27 17:05:23 -0800513 if (req->cmd_flags & REQ_FLUSH) {
514 BUG_ON(unlikely(blk_rq_sectors(req)));
515 nbd_cmd(req) = NBD_CMD_FLUSH;
516 }
517
Laurent Vivier48cf6062008-04-29 01:02:46 -0700518 req->errors = 0;
519
Wanlong Gaof4507162012-03-28 14:42:51 -0700520 mutex_lock(&nbd->tx_lock);
521 if (unlikely(!nbd->sock)) {
522 mutex_unlock(&nbd->tx_lock);
523 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200524 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700525 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700526 }
527
Wanlong Gaof4507162012-03-28 14:42:51 -0700528 nbd->active_req = req;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700529
Wanlong Gaof4507162012-03-28 14:42:51 -0700530 if (nbd_send_req(nbd, req) != 0) {
531 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700532 req->errors++;
533 nbd_end_request(req);
534 } else {
Wanlong Gaof4507162012-03-28 14:42:51 -0700535 spin_lock(&nbd->queue_lock);
Chetan Loke01ff5db2012-07-31 08:47:13 +0200536 list_add_tail(&req->queuelist, &nbd->queue_head);
Wanlong Gaof4507162012-03-28 14:42:51 -0700537 spin_unlock(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700538 }
539
Wanlong Gaof4507162012-03-28 14:42:51 -0700540 nbd->active_req = NULL;
541 mutex_unlock(&nbd->tx_lock);
542 wake_up_all(&nbd->active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700543
544 return;
545
546error_out:
547 req->errors++;
548 nbd_end_request(req);
549}
550
551static int nbd_thread(void *data)
552{
Wanlong Gaof4507162012-03-28 14:42:51 -0700553 struct nbd_device *nbd = data;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700554 struct request *req;
555
Dongsheng Yang8698a742014-03-11 18:09:12 +0800556 set_user_nice(current, MIN_NICE);
Wanlong Gaof4507162012-03-28 14:42:51 -0700557 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
Laurent Vivier48cf6062008-04-29 01:02:46 -0700558 /* wait for something to do */
Wanlong Gaof4507162012-03-28 14:42:51 -0700559 wait_event_interruptible(nbd->waiting_wq,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700560 kthread_should_stop() ||
Wanlong Gaof4507162012-03-28 14:42:51 -0700561 !list_empty(&nbd->waiting_queue));
Laurent Vivier48cf6062008-04-29 01:02:46 -0700562
563 /* extract request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700564 if (list_empty(&nbd->waiting_queue))
Laurent Vivier48cf6062008-04-29 01:02:46 -0700565 continue;
566
Wanlong Gaof4507162012-03-28 14:42:51 -0700567 spin_lock_irq(&nbd->queue_lock);
568 req = list_entry(nbd->waiting_queue.next, struct request,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700569 queuelist);
570 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700571 spin_unlock_irq(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700572
573 /* handle request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700574 nbd_handle_req(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700575 }
576 return 0;
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * We always wait for result of write, for now. It would be nice to make it optional
581 * in future
Wanlong Gaof4507162012-03-28 14:42:51 -0700582 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
584 */
585
Pavel Machek15746fc2009-04-02 16:58:42 -0700586static void do_nbd_request(struct request_queue *q)
Alex Elder398eb082013-02-27 17:05:28 -0800587 __releases(q->queue_lock) __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct request *req;
590
Tejun Heo9934c8c2009-05-08 11:54:16 +0900591 while ((req = blk_fetch_request(q)) != NULL) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700592 struct nbd_device *nbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Laurent Vivier48cf6062008-04-29 01:02:46 -0700594 spin_unlock_irq(q->queue_lock);
595
Jens Axboe4aff5e22006-08-10 08:44:47 +0200596 dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n",
597 req->rq_disk->disk_name, req, req->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Wanlong Gaof4507162012-03-28 14:42:51 -0700599 nbd = req->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Wanlong Gaof4507162012-03-28 14:42:51 -0700601 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Wanlong Gaof4507162012-03-28 14:42:51 -0700603 if (unlikely(!nbd->sock)) {
604 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200605 "Attempted send on closed socket\n");
Paul Clements4d48a542009-02-11 13:04:45 -0800606 req->errors++;
607 nbd_end_request(req);
608 spin_lock_irq(q->queue_lock);
609 continue;
610 }
611
Wanlong Gaof4507162012-03-28 14:42:51 -0700612 spin_lock_irq(&nbd->queue_lock);
613 list_add_tail(&req->queuelist, &nbd->waiting_queue);
614 spin_unlock_irq(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Wanlong Gaof4507162012-03-28 14:42:51 -0700616 wake_up(&nbd->waiting_wq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Pavel Machek1a2ad212009-04-02 16:58:41 -0700622/* Must be called with tx_lock held */
623
Wanlong Gaof4507162012-03-28 14:42:51 -0700624static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700625 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700628 case NBD_DISCONNECT: {
629 struct request sreq;
630
Wanlong Gaof4507162012-03-28 14:42:51 -0700631 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800632 if (!nbd->sock)
633 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700634
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800635 mutex_unlock(&nbd->tx_lock);
636 fsync_bdev(bdev);
637 mutex_lock(&nbd->tx_lock);
FUJITA Tomonori4f54eec2008-04-29 09:54:37 +0200638 blk_rq_init(NULL, &sreq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200639 sreq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 nbd_cmd(&sreq) = NBD_CMD_DISC;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800641
642 /* Check again after getting mutex back. */
Wanlong Gaof4507162012-03-28 14:42:51 -0700643 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800645
Paul Clementsc378f702013-07-03 15:09:04 -0700646 nbd->disconnect = 1;
647
Wanlong Gaof4507162012-03-28 14:42:51 -0700648 nbd_send_req(nbd, &sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700649 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Pavel Machek1a2ad212009-04-02 16:58:41 -0700652 case NBD_CLEAR_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500653 struct socket *sock = nbd->sock;
Wanlong Gaof4507162012-03-28 14:42:51 -0700654 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700655 nbd_clear_que(nbd);
656 BUG_ON(!list_empty(&nbd->queue_head));
Paul Clementsfded4e02012-09-17 14:09:02 -0700657 BUG_ON(!list_empty(&nbd->waiting_queue));
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800658 kill_bdev(bdev);
Al Viroe2511572014-03-05 20:41:36 -0500659 if (sock)
660 sockfd_put(sock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700661 return 0;
662 }
663
664 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500665 struct socket *sock;
666 int err;
667 if (nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500669 sock = sockfd_lookup(arg, &err);
670 if (sock) {
671 nbd->sock = sock;
672 if (max_part > 0)
673 bdev->bd_invalidated = 1;
674 nbd->disconnect = 0; /* we're connected now */
675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700677 return -EINVAL;
678 }
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 case NBD_SET_BLKSIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700681 nbd->blksize = arg;
682 nbd->bytesize &= ~(nbd->blksize-1);
683 bdev->bd_inode->i_size = nbd->bytesize;
684 set_blocksize(bdev, nbd->blksize);
685 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 case NBD_SET_SIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700689 nbd->bytesize = arg & ~(nbd->blksize-1);
690 bdev->bd_inode->i_size = nbd->bytesize;
691 set_blocksize(bdev, nbd->blksize);
692 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700694
Paul Clements7fdfd402007-10-16 23:27:37 -0700695 case NBD_SET_TIMEOUT:
Wanlong Gaof4507162012-03-28 14:42:51 -0700696 nbd->xmit_timeout = arg * HZ;
Paul Clements7fdfd402007-10-16 23:27:37 -0700697 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700698
Paul Clements2f012502012-10-04 17:16:15 -0700699 case NBD_SET_FLAGS:
700 nbd->flags = arg;
701 return 0;
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 case NBD_SET_SIZE_BLOCKS:
Wanlong Gaof4507162012-03-28 14:42:51 -0700704 nbd->bytesize = ((u64) arg) * nbd->blksize;
705 bdev->bd_inode->i_size = nbd->bytesize;
706 set_blocksize(bdev, nbd->blksize);
707 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700709
710 case NBD_DO_IT: {
711 struct task_struct *thread;
Al Viroe2511572014-03-05 20:41:36 -0500712 struct socket *sock;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700713 int error;
714
Wanlong Gaof4507162012-03-28 14:42:51 -0700715 if (nbd->pid)
Pavel Machekc91192d2009-01-15 13:51:03 -0800716 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500717 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700719
Wanlong Gaof4507162012-03-28 14:42:51 -0700720 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700721
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800722 if (nbd->flags & NBD_FLAG_READ_ONLY)
723 set_device_ro(bdev, true);
Paul Clementsa336d292012-10-04 17:16:18 -0700724 if (nbd->flags & NBD_FLAG_SEND_TRIM)
725 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
726 nbd->disk->queue);
Alex Bligh75f187a2013-02-27 17:05:23 -0800727 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
728 blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
729 else
730 blk_queue_flush(nbd->disk->queue, 0);
Paul Clementsa336d292012-10-04 17:16:18 -0700731
Markus Pargmannd06df602015-04-02 10:11:36 +0200732 thread = kthread_run(nbd_thread, nbd, "%s",
733 nbd->disk->disk_name);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700734 if (IS_ERR(thread)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700735 mutex_lock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700736 return PTR_ERR(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700737 }
Markus Pargmannd06df602015-04-02 10:11:36 +0200738
Wanlong Gaof4507162012-03-28 14:42:51 -0700739 error = nbd_do_it(nbd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700740 kthread_stop(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700741
Wanlong Gaof4507162012-03-28 14:42:51 -0700742 mutex_lock(&nbd->tx_lock);
WANG Cong84963042007-05-09 02:33:36 -0700743 if (error)
744 return error;
Wanlong Gaof4507162012-03-28 14:42:51 -0700745 sock_shutdown(nbd, 0);
Al Viroe2511572014-03-05 20:41:36 -0500746 sock = nbd->sock;
747 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700748 nbd_clear_que(nbd);
749 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800750 kill_bdev(bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700751 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800752 set_device_ro(bdev, false);
Al Viroe2511572014-03-05 20:41:36 -0500753 if (sock)
754 sockfd_put(sock);
Alex Bligh75f187a2013-02-27 17:05:23 -0800755 nbd->flags = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700756 nbd->bytesize = 0;
Al Viroa8cdc302008-03-02 09:33:33 -0500757 bdev->bd_inode->i_size = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700758 set_capacity(nbd->disk, 0);
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700759 if (max_part > 0)
Al Viroa8cdc302008-03-02 09:33:33 -0500760 ioctl_by_bdev(bdev, BLKRRPART, 0);
Paul Clementsc378f702013-07-03 15:09:04 -0700761 if (nbd->disconnect) /* user requested, ignore socket errors */
762 return 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700763 return nbd->harderror;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700764 }
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800767 /*
768 * This is for compatibility only. The queue is always cleared
769 * by NBD_DO_IT or NBD_CLEAR_SOCK.
770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 case NBD_PRINT_DEBUG:
Wanlong Gaof4507162012-03-28 14:42:51 -0700774 dev_info(disk_to_dev(nbd->disk),
WANG Cong5eedf542011-08-19 14:48:28 +0200775 "next = %p, prev = %p, head = %p\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700776 nbd->queue_head.next, nbd->queue_head.prev,
777 &nbd->queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 0;
779 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700780 return -ENOTTY;
781}
782
783static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
784 unsigned int cmd, unsigned long arg)
785{
Wanlong Gaof4507162012-03-28 14:42:51 -0700786 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700787 int error;
788
789 if (!capable(CAP_SYS_ADMIN))
790 return -EPERM;
791
Wanlong Gaof4507162012-03-28 14:42:51 -0700792 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700793
794 /* Anyone capable of this syscall can do *real bad* things */
795 dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700796 nbd->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700797
Wanlong Gaof4507162012-03-28 14:42:51 -0700798 mutex_lock(&nbd->tx_lock);
799 error = __nbd_ioctl(bdev, nbd, cmd, arg);
800 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700801
802 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700805static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200808 .ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809};
810
811/*
812 * And here should be modules and kernel interface
813 * (Just smiley confuses emacs :-)
814 */
815
816static int __init nbd_init(void)
817{
818 int err = -ENOMEM;
819 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700820 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800822 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700824 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +0200825 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700826 return -EINVAL;
827 }
828
829 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +0200830 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700831 part_shift = fls(max_part);
832
Namhyung Kim5988ce22011-05-28 14:44:46 +0200833 /*
834 * Adjust max_part according to part_shift as it is exported
835 * to user space so that user can know the max number of
836 * partition kernel should be able to manage.
837 *
838 * Note that -1 is required because partition 0 is reserved
839 * for the whole disk.
840 */
841 max_part = (1UL << part_shift) - 1;
842 }
843
Namhyung Kim3b271082011-05-28 14:44:46 +0200844 if ((1UL << part_shift) > DISK_MAX_PARTS)
845 return -EINVAL;
846
847 if (nbds_max > 1UL << (MINORBITS - part_shift))
848 return -EINVAL;
849
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +0530850 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
851 if (!nbd_dev)
852 return -ENOMEM;
853
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700854 for (i = 0; i < nbds_max; i++) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700855 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!disk)
857 goto out;
858 nbd_dev[i].disk = disk;
859 /*
860 * The new linux 2.5 block layer implementation requires
861 * every gendisk to have its very own request_queue struct.
862 * These structs are big so we dynamically allocate them.
863 */
864 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
865 if (!disk->queue) {
866 put_disk(disk);
867 goto out;
868 }
Jens Axboe31dcfab2008-10-31 10:06:37 +0100869 /*
870 * Tell the block layer that we are not a rotational device
871 */
872 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600873 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -0700874 disk->queue->limits.discard_granularity = 512;
875 disk->queue->limits.max_discard_sectors = UINT_MAX;
876 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -0700877 blk_queue_max_hw_sectors(disk->queue, 65536);
878 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 if (register_blkdev(NBD_MAJOR, "nbd")) {
882 err = -EIO;
883 goto out;
884 }
885
886 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
887 dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags);
888
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700889 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -0700891 nbd_dev[i].magic = NBD_MAGIC;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700892 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 spin_lock_init(&nbd_dev[i].queue_lock);
894 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
Ingo Molnar82d4dc52006-03-23 03:00:38 -0800895 mutex_init(&nbd_dev[i].tx_lock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800896 init_waitqueue_head(&nbd_dev[i].active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700897 init_waitqueue_head(&nbd_dev[i].waiting_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 nbd_dev[i].blksize = 1024;
Paul Clements4b86a872007-10-16 23:27:36 -0700899 nbd_dev[i].bytesize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700901 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 disk->fops = &nbd_fops;
903 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 sprintf(disk->disk_name, "nbd%d", i);
Paul Clements4b86a872007-10-16 23:27:36 -0700905 set_capacity(disk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 add_disk(disk);
907 }
908
909 return 0;
910out:
911 while (i--) {
912 blk_cleanup_queue(nbd_dev[i].disk->queue);
913 put_disk(nbd_dev[i].disk);
914 }
Sven Wegenerf3944d62008-08-20 14:09:07 -0700915 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return err;
917}
918
919static void __exit nbd_cleanup(void)
920{
921 int i;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700922 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700924 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (disk) {
926 del_gendisk(disk);
927 blk_cleanup_queue(disk->queue);
928 put_disk(disk);
929 }
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -0700932 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
934}
935
936module_init(nbd_init);
937module_exit(nbd_cleanup);
938
939MODULE_DESCRIPTION("Network Block Device");
940MODULE_LICENSE("GPL");
941
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700942module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700943MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
944module_param(max_part, int, 0444);
945MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946#ifndef NDEBUG
947module_param(debugflags, int, 0644);
948MODULE_PARM_DESC(debugflags, "flags for controlling debug output");
949#endif