blob: 39e5f7fae3efb76ab888ec19b2b8a8fdb4b258b4 [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
Ingo van Lil9c7a4162006-07-01 04:36:36 -070066static unsigned int nbds_max = 16;
Paul Clements20a81432008-02-08 04:21:51 -080067static struct nbd_device *nbd_dev;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070068static int max_part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * Use just one lock (or at most 1 per NIC). Two arguments for this:
72 * 1. Each NIC is essentially a synchronization point for all servers
73 * accessed through that NIC so there's no need to have more locks
74 * than NICs anyway.
75 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
76 * down each lock to the point where they're actually slower than just
77 * a single lock.
78 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
79 */
80static DEFINE_SPINLOCK(nbd_lock);
81
Markus Pargmannd18509f2015-04-02 10:11:38 +020082static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Markus Pargmannd18509f2015-04-02 10:11:38 +020084 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static const char *nbdcmd_to_ascii(int cmd)
88{
89 switch (cmd) {
90 case NBD_CMD_READ: return "read";
91 case NBD_CMD_WRITE: return "write";
92 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -080093 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -070094 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96 return "invalid";
97}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Markus Pargmannd18509f2015-04-02 10:11:38 +020099static void nbd_end_request(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500101 int error = req->errors ? -EIO : 0;
Jens Axboe165125e2007-07-24 09:28:11 +0200102 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long flags;
104
Markus Pargmannd18509f2015-04-02 10:11:38 +0200105 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req,
106 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo1011c1b2009-05-07 22:24:45 +0900109 __blk_end_request_all(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 spin_unlock_irqrestore(q->queue_lock, flags);
111}
112
Markus Pargmanne018e752015-04-02 10:11:39 +0200113/*
114 * Forcibly shutdown the socket causing all listeners to error
115 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700116static void sock_shutdown(struct nbd_device *nbd, int lock)
Paul Clements7fdfd402007-10-16 23:27:37 -0700117{
Paul Clements7fdfd402007-10-16 23:27:37 -0700118 if (lock)
Wanlong Gaof4507162012-03-28 14:42:51 -0700119 mutex_lock(&nbd->tx_lock);
120 if (nbd->sock) {
121 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
122 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
123 nbd->sock = NULL;
Paul Clements7fdfd402007-10-16 23:27:37 -0700124 }
125 if (lock)
Wanlong Gaof4507162012-03-28 14:42:51 -0700126 mutex_unlock(&nbd->tx_lock);
Paul Clements7fdfd402007-10-16 23:27:37 -0700127}
128
129static void nbd_xmit_timeout(unsigned long arg)
130{
131 struct task_struct *task = (struct task_struct *)arg;
132
133 printk(KERN_WARNING "nbd: killing hung xmit (%s, pid: %d)\n",
134 task->comm, task->pid);
135 force_sig(SIGKILL, task);
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * Send or receive packet.
140 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700141static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int msg_flags)
143{
Wanlong Gaof4507162012-03-28 14:42:51 -0700144 struct socket *sock = nbd->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int result;
146 struct msghdr msg;
147 struct kvec iov;
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700148 sigset_t blocked, oldset;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700149 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700151 if (unlikely(!sock)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700152 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200153 "Attempted %s on closed socket in sock_xmit\n",
154 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700155 return -EINVAL;
156 }
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* Allow interception of SIGKILL only
159 * Don't allow other signals to interrupt the transmission */
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700160 siginitsetinv(&blocked, sigmask(SIGKILL));
161 sigprocmask(SIG_SETMASK, &blocked, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Mel Gorman7f338fe2012-07-31 16:44:32 -0700163 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700165 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 iov.iov_base = buf;
167 iov.iov_len = size;
168 msg.msg_name = NULL;
169 msg.msg_namelen = 0;
170 msg.msg_control = NULL;
171 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
173
Paul Clements7fdfd402007-10-16 23:27:37 -0700174 if (send) {
175 struct timer_list ti;
176
Wanlong Gaof4507162012-03-28 14:42:51 -0700177 if (nbd->xmit_timeout) {
Paul Clements7fdfd402007-10-16 23:27:37 -0700178 init_timer(&ti);
179 ti.function = nbd_xmit_timeout;
180 ti.data = (unsigned long)current;
Wanlong Gaof4507162012-03-28 14:42:51 -0700181 ti.expires = jiffies + nbd->xmit_timeout;
Paul Clements7fdfd402007-10-16 23:27:37 -0700182 add_timer(&ti);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
Wanlong Gaof4507162012-03-28 14:42:51 -0700185 if (nbd->xmit_timeout)
Paul Clements7fdfd402007-10-16 23:27:37 -0700186 del_timer_sync(&ti);
187 } else
Namhyung Kim35fbf5b2011-05-28 14:44:46 +0200188 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
189 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if (signal_pending(current)) {
192 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700194 task_pid_nr(current), current->comm,
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700195 dequeue_signal_lock(current, &current->blocked, &info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 result = -EINTR;
Wanlong Gaof4507162012-03-28 14:42:51 -0700197 sock_shutdown(nbd, !send);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 }
200
201 if (result <= 0) {
202 if (result == 0)
203 result = -EPIPE; /* short read */
204 break;
205 }
206 size -= result;
207 buf += result;
208 } while (size > 0);
209
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700210 sigprocmask(SIG_SETMASK, &oldset, NULL);
Mel Gorman7f338fe2012-07-31 16:44:32 -0700211 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 return result;
214}
215
Wanlong Gaof4507162012-03-28 14:42:51 -0700216static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int flags)
218{
219 int result;
220 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700221 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
222 bvec->bv_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 kunmap(bvec->bv_page);
224 return result;
225}
226
Paul Clements7fdfd402007-10-16 23:27:37 -0700227/* always call with the tx_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -0700228static int nbd_send_req(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
NeilBrown5705f702007-09-25 12:35:59 +0200230 int result, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct nbd_request request;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900232 unsigned long size = blk_rq_bytes(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700234 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 request.magic = htonl(NBD_REQUEST_MAGIC);
236 request.type = htonl(nbd_cmd(req));
Alex Bligh75f187a2013-02-27 17:05:23 -0800237
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700238 if (nbd_cmd(req) != NBD_CMD_FLUSH && nbd_cmd(req) != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800239 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
240 request.len = htonl(size);
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 memcpy(request.handle, &req, sizeof(req));
243
Markus Pargmannd18509f2015-04-02 10:11:38 +0200244 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
245 req, nbdcmd_to_ascii(nbd_cmd(req)),
246 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700247 result = sock_xmit(nbd, 1, &request, sizeof(request),
Paul Clements7fdfd402007-10-16 23:27:37 -0700248 (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700250 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200251 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200252 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 if (nbd_cmd(req) == NBD_CMD_WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200256 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800257 struct bio_vec bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /*
259 * we are really probing at internals to determine
260 * whether to set MSG_MORE or not...
261 */
NeilBrown5705f702007-09-25 12:35:59 +0200262 rq_for_each_segment(bvec, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200263 flags = 0;
Kent Overstreet4550dd62013-08-07 14:26:21 -0700264 if (!rq_iter_last(bvec, iter))
Jens Axboe6c92e692007-08-16 13:43:12 +0200265 flags = MSG_MORE;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200266 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
267 req, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800268 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200269 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700270 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200271 "Send data failed (result %d)\n",
272 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200273 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Wanlong Gaof4507162012-03-28 14:42:51 -0700280static struct request *nbd_find_request(struct nbd_device *nbd,
Denis Cheng0cbc591b2007-10-16 23:26:14 -0700281 struct request *xreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Denis Chengd2c97402007-10-16 23:26:14 -0700283 struct request *req, *tmp;
Herbert Xu4b2f0262006-01-06 00:09:47 -0800284 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Wanlong Gaof4507162012-03-28 14:42:51 -0700286 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800287 if (unlikely(err))
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200288 return ERR_PTR(err);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800289
Wanlong Gaof4507162012-03-28 14:42:51 -0700290 spin_lock(&nbd->queue_lock);
291 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (req != xreq)
293 continue;
294 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700295 spin_unlock(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return req;
297 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700298 spin_unlock(&nbd->queue_lock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800299
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200300 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Wanlong Gaof4507162012-03-28 14:42:51 -0700303static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 int result;
306 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700307 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 MSG_WAITALL);
309 kunmap(bvec->bv_page);
310 return result;
311}
312
313/* NULL returned = something went wrong, inform userspace */
Wanlong Gaof4507162012-03-28 14:42:51 -0700314static struct request *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 int result;
317 struct nbd_reply reply;
318 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700321 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700323 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200324 "Receive control failed (result %d)\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto harderror;
326 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700327
328 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700329 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700330 (unsigned long)ntohl(reply.magic));
331 result = -EPROTO;
332 goto harderror;
333 }
334
Wanlong Gaof4507162012-03-28 14:42:51 -0700335 req = nbd_find_request(nbd, *(struct request **)reply.handle);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700336 if (IS_ERR(req)) {
Herbert Xu4b2f0262006-01-06 00:09:47 -0800337 result = PTR_ERR(req);
338 if (result != -ENOENT)
339 goto harderror;
340
Wanlong Gaof4507162012-03-28 14:42:51 -0700341 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200342 reply.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 result = -EBADR;
344 goto harderror;
345 }
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700348 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200349 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 req->errors++;
351 return req;
352 }
353
Markus Pargmannd18509f2015-04-02 10:11:38 +0200354 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (nbd_cmd(req) == NBD_CMD_READ) {
NeilBrown5705f702007-09-25 12:35:59 +0200356 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800357 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200358
359 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800360 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200361 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700362 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200363 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200364 req->errors++;
365 return req;
366 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200367 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
368 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 }
371 return req;
372harderror:
Wanlong Gaof4507162012-03-28 14:42:51 -0700373 nbd->harderror = result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return NULL;
375}
376
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200377static ssize_t pid_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800379{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200380 struct gendisk *disk = dev_to_disk(dev);
381
382 return sprintf(buf, "%ld\n",
Paul Clements6b39bb62006-12-06 20:40:53 -0800383 (long) ((struct nbd_device *)disk->private_data)->pid);
384}
385
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200386static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700387 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800388 .show = pid_show,
389};
390
Wanlong Gaof4507162012-03-28 14:42:51 -0700391static int nbd_do_it(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct request *req;
WANG Cong84963042007-05-09 02:33:36 -0700394 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Wanlong Gaof4507162012-03-28 14:42:51 -0700396 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Mel Gorman7f338fe2012-07-31 16:44:32 -0700398 sk_set_memalloc(nbd->sock->sk);
Wanlong Gaof4507162012-03-28 14:42:51 -0700399 nbd->pid = task_pid_nr(current);
400 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700401 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700402 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
403 nbd->pid = 0;
WANG Cong84963042007-05-09 02:33:36 -0700404 return ret;
405 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800406
Wanlong Gaof4507162012-03-28 14:42:51 -0700407 while ((req = nbd_read_stat(nbd)) != NULL)
Markus Pargmannd18509f2015-04-02 10:11:38 +0200408 nbd_end_request(nbd, req);
Paul Clements6b39bb62006-12-06 20:40:53 -0800409
Wanlong Gaof4507162012-03-28 14:42:51 -0700410 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
411 nbd->pid = 0;
WANG Cong84963042007-05-09 02:33:36 -0700412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Wanlong Gaof4507162012-03-28 14:42:51 -0700415static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct request *req;
418
Wanlong Gaof4507162012-03-28 14:42:51 -0700419 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Herbert Xu4b2f0262006-01-06 00:09:47 -0800421 /*
Wanlong Gaof4507162012-03-28 14:42:51 -0700422 * Because we have set nbd->sock to NULL under the tx_lock, all
Herbert Xu4b2f0262006-01-06 00:09:47 -0800423 * modifications to the list must have completed by now. For
424 * the same reason, the active_req must be NULL.
425 *
426 * As a consequence, we don't need to take the spin lock while
427 * purging the list here.
428 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700429 BUG_ON(nbd->sock);
430 BUG_ON(nbd->active_req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800431
Wanlong Gaof4507162012-03-28 14:42:51 -0700432 while (!list_empty(&nbd->queue_head)) {
433 req = list_entry(nbd->queue_head.next, struct request,
Herbert Xu4b2f0262006-01-06 00:09:47 -0800434 queuelist);
435 list_del_init(&req->queuelist);
436 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200437 nbd_end_request(nbd, req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800438 }
Paul Clementsfded4e02012-09-17 14:09:02 -0700439
440 while (!list_empty(&nbd->waiting_queue)) {
441 req = list_entry(nbd->waiting_queue.next, struct request,
442 queuelist);
443 list_del_init(&req->queuelist);
444 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200445 nbd_end_request(nbd, req);
Paul Clementsfded4e02012-09-17 14:09:02 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Paul Clements7fdfd402007-10-16 23:27:37 -0700449
Wanlong Gaof4507162012-03-28 14:42:51 -0700450static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700451{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200452 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700453 goto error_out;
454
455 nbd_cmd(req) = NBD_CMD_READ;
456 if (rq_data_dir(req) == WRITE) {
Paul Clementsa336d292012-10-04 17:16:18 -0700457 if ((req->cmd_flags & REQ_DISCARD)) {
458 WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM));
459 nbd_cmd(req) = NBD_CMD_TRIM;
460 } else
461 nbd_cmd(req) = NBD_CMD_WRITE;
Paul Clements2f012502012-10-04 17:16:15 -0700462 if (nbd->flags & NBD_FLAG_READ_ONLY) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700463 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200464 "Write on read-only\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700465 goto error_out;
466 }
467 }
468
Alex Bligh75f187a2013-02-27 17:05:23 -0800469 if (req->cmd_flags & REQ_FLUSH) {
470 BUG_ON(unlikely(blk_rq_sectors(req)));
471 nbd_cmd(req) = NBD_CMD_FLUSH;
472 }
473
Laurent Vivier48cf6062008-04-29 01:02:46 -0700474 req->errors = 0;
475
Wanlong Gaof4507162012-03-28 14:42:51 -0700476 mutex_lock(&nbd->tx_lock);
477 if (unlikely(!nbd->sock)) {
478 mutex_unlock(&nbd->tx_lock);
479 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200480 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700481 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700482 }
483
Wanlong Gaof4507162012-03-28 14:42:51 -0700484 nbd->active_req = req;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700485
Wanlong Gaof4507162012-03-28 14:42:51 -0700486 if (nbd_send_req(nbd, req) != 0) {
487 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700488 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200489 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700490 } else {
Wanlong Gaof4507162012-03-28 14:42:51 -0700491 spin_lock(&nbd->queue_lock);
Chetan Loke01ff5db2012-07-31 08:47:13 +0200492 list_add_tail(&req->queuelist, &nbd->queue_head);
Wanlong Gaof4507162012-03-28 14:42:51 -0700493 spin_unlock(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700494 }
495
Wanlong Gaof4507162012-03-28 14:42:51 -0700496 nbd->active_req = NULL;
497 mutex_unlock(&nbd->tx_lock);
498 wake_up_all(&nbd->active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700499
500 return;
501
502error_out:
503 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200504 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700505}
506
507static int nbd_thread(void *data)
508{
Wanlong Gaof4507162012-03-28 14:42:51 -0700509 struct nbd_device *nbd = data;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700510 struct request *req;
511
Dongsheng Yang8698a742014-03-11 18:09:12 +0800512 set_user_nice(current, MIN_NICE);
Wanlong Gaof4507162012-03-28 14:42:51 -0700513 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
Laurent Vivier48cf6062008-04-29 01:02:46 -0700514 /* wait for something to do */
Wanlong Gaof4507162012-03-28 14:42:51 -0700515 wait_event_interruptible(nbd->waiting_wq,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700516 kthread_should_stop() ||
Wanlong Gaof4507162012-03-28 14:42:51 -0700517 !list_empty(&nbd->waiting_queue));
Laurent Vivier48cf6062008-04-29 01:02:46 -0700518
519 /* extract request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700520 if (list_empty(&nbd->waiting_queue))
Laurent Vivier48cf6062008-04-29 01:02:46 -0700521 continue;
522
Wanlong Gaof4507162012-03-28 14:42:51 -0700523 spin_lock_irq(&nbd->queue_lock);
524 req = list_entry(nbd->waiting_queue.next, struct request,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700525 queuelist);
526 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700527 spin_unlock_irq(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700528
529 /* handle request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700530 nbd_handle_req(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700531 }
532 return 0;
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
536 * We always wait for result of write, for now. It would be nice to make it optional
537 * in future
Wanlong Gaof4507162012-03-28 14:42:51 -0700538 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
540 */
541
Pavel Machek15746fc2009-04-02 16:58:42 -0700542static void do_nbd_request(struct request_queue *q)
Alex Elder398eb082013-02-27 17:05:28 -0800543 __releases(q->queue_lock) __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct request *req;
546
Tejun Heo9934c8c2009-05-08 11:54:16 +0900547 while ((req = blk_fetch_request(q)) != NULL) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700548 struct nbd_device *nbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Laurent Vivier48cf6062008-04-29 01:02:46 -0700550 spin_unlock_irq(q->queue_lock);
551
Wanlong Gaof4507162012-03-28 14:42:51 -0700552 nbd = req->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Wanlong Gaof4507162012-03-28 14:42:51 -0700554 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Markus Pargmannd18509f2015-04-02 10:11:38 +0200556 dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n",
557 req, req->cmd_type);
558
Wanlong Gaof4507162012-03-28 14:42:51 -0700559 if (unlikely(!nbd->sock)) {
560 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200561 "Attempted send on closed socket\n");
Paul Clements4d48a542009-02-11 13:04:45 -0800562 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200563 nbd_end_request(nbd, req);
Paul Clements4d48a542009-02-11 13:04:45 -0800564 spin_lock_irq(q->queue_lock);
565 continue;
566 }
567
Wanlong Gaof4507162012-03-28 14:42:51 -0700568 spin_lock_irq(&nbd->queue_lock);
569 list_add_tail(&req->queuelist, &nbd->waiting_queue);
570 spin_unlock_irq(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Wanlong Gaof4507162012-03-28 14:42:51 -0700572 wake_up(&nbd->waiting_wq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Pavel Machek1a2ad212009-04-02 16:58:41 -0700578/* Must be called with tx_lock held */
579
Wanlong Gaof4507162012-03-28 14:42:51 -0700580static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700581 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700584 case NBD_DISCONNECT: {
585 struct request sreq;
586
Wanlong Gaof4507162012-03-28 14:42:51 -0700587 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800588 if (!nbd->sock)
589 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700590
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800591 mutex_unlock(&nbd->tx_lock);
592 fsync_bdev(bdev);
593 mutex_lock(&nbd->tx_lock);
FUJITA Tomonori4f54eec2008-04-29 09:54:37 +0200594 blk_rq_init(NULL, &sreq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200595 sreq.cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 nbd_cmd(&sreq) = NBD_CMD_DISC;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800597
598 /* Check again after getting mutex back. */
Wanlong Gaof4507162012-03-28 14:42:51 -0700599 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -EINVAL;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800601
Paul Clementsc378f702013-07-03 15:09:04 -0700602 nbd->disconnect = 1;
603
Wanlong Gaof4507162012-03-28 14:42:51 -0700604 nbd_send_req(nbd, &sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700605 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Pavel Machek1a2ad212009-04-02 16:58:41 -0700608 case NBD_CLEAR_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500609 struct socket *sock = nbd->sock;
Wanlong Gaof4507162012-03-28 14:42:51 -0700610 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700611 nbd_clear_que(nbd);
612 BUG_ON(!list_empty(&nbd->queue_head));
Paul Clementsfded4e02012-09-17 14:09:02 -0700613 BUG_ON(!list_empty(&nbd->waiting_queue));
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800614 kill_bdev(bdev);
Al Viroe2511572014-03-05 20:41:36 -0500615 if (sock)
616 sockfd_put(sock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700617 return 0;
618 }
619
620 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500621 struct socket *sock;
622 int err;
623 if (nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500625 sock = sockfd_lookup(arg, &err);
626 if (sock) {
627 nbd->sock = sock;
628 if (max_part > 0)
629 bdev->bd_invalidated = 1;
630 nbd->disconnect = 0; /* we're connected now */
631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700633 return -EINVAL;
634 }
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 case NBD_SET_BLKSIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700637 nbd->blksize = arg;
638 nbd->bytesize &= ~(nbd->blksize-1);
639 bdev->bd_inode->i_size = nbd->bytesize;
640 set_blocksize(bdev, nbd->blksize);
641 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 case NBD_SET_SIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700645 nbd->bytesize = arg & ~(nbd->blksize-1);
646 bdev->bd_inode->i_size = nbd->bytesize;
647 set_blocksize(bdev, nbd->blksize);
648 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700650
Paul Clements7fdfd402007-10-16 23:27:37 -0700651 case NBD_SET_TIMEOUT:
Wanlong Gaof4507162012-03-28 14:42:51 -0700652 nbd->xmit_timeout = arg * HZ;
Paul Clements7fdfd402007-10-16 23:27:37 -0700653 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700654
Paul Clements2f012502012-10-04 17:16:15 -0700655 case NBD_SET_FLAGS:
656 nbd->flags = arg;
657 return 0;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 case NBD_SET_SIZE_BLOCKS:
Wanlong Gaof4507162012-03-28 14:42:51 -0700660 nbd->bytesize = ((u64) arg) * nbd->blksize;
661 bdev->bd_inode->i_size = nbd->bytesize;
662 set_blocksize(bdev, nbd->blksize);
663 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700665
666 case NBD_DO_IT: {
667 struct task_struct *thread;
Al Viroe2511572014-03-05 20:41:36 -0500668 struct socket *sock;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700669 int error;
670
Wanlong Gaof4507162012-03-28 14:42:51 -0700671 if (nbd->pid)
Pavel Machekc91192d2009-01-15 13:51:03 -0800672 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500673 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700675
Wanlong Gaof4507162012-03-28 14:42:51 -0700676 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700677
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800678 if (nbd->flags & NBD_FLAG_READ_ONLY)
679 set_device_ro(bdev, true);
Paul Clementsa336d292012-10-04 17:16:18 -0700680 if (nbd->flags & NBD_FLAG_SEND_TRIM)
681 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
682 nbd->disk->queue);
Alex Bligh75f187a2013-02-27 17:05:23 -0800683 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
684 blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
685 else
686 blk_queue_flush(nbd->disk->queue, 0);
Paul Clementsa336d292012-10-04 17:16:18 -0700687
Markus Pargmannd06df602015-04-02 10:11:36 +0200688 thread = kthread_run(nbd_thread, nbd, "%s",
689 nbd->disk->disk_name);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700690 if (IS_ERR(thread)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700691 mutex_lock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700692 return PTR_ERR(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700693 }
Markus Pargmannd06df602015-04-02 10:11:36 +0200694
Wanlong Gaof4507162012-03-28 14:42:51 -0700695 error = nbd_do_it(nbd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700696 kthread_stop(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700697
Wanlong Gaof4507162012-03-28 14:42:51 -0700698 mutex_lock(&nbd->tx_lock);
WANG Cong84963042007-05-09 02:33:36 -0700699 if (error)
700 return error;
Wanlong Gaof4507162012-03-28 14:42:51 -0700701 sock_shutdown(nbd, 0);
Al Viroe2511572014-03-05 20:41:36 -0500702 sock = nbd->sock;
703 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700704 nbd_clear_que(nbd);
705 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800706 kill_bdev(bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700707 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800708 set_device_ro(bdev, false);
Al Viroe2511572014-03-05 20:41:36 -0500709 if (sock)
710 sockfd_put(sock);
Alex Bligh75f187a2013-02-27 17:05:23 -0800711 nbd->flags = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700712 nbd->bytesize = 0;
Al Viroa8cdc302008-03-02 09:33:33 -0500713 bdev->bd_inode->i_size = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700714 set_capacity(nbd->disk, 0);
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700715 if (max_part > 0)
Al Viroa8cdc302008-03-02 09:33:33 -0500716 ioctl_by_bdev(bdev, BLKRRPART, 0);
Paul Clementsc378f702013-07-03 15:09:04 -0700717 if (nbd->disconnect) /* user requested, ignore socket errors */
718 return 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700719 return nbd->harderror;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800723 /*
724 * This is for compatibility only. The queue is always cleared
725 * by NBD_DO_IT or NBD_CLEAR_SOCK.
726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 case NBD_PRINT_DEBUG:
Wanlong Gaof4507162012-03-28 14:42:51 -0700730 dev_info(disk_to_dev(nbd->disk),
WANG Cong5eedf542011-08-19 14:48:28 +0200731 "next = %p, prev = %p, head = %p\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700732 nbd->queue_head.next, nbd->queue_head.prev,
733 &nbd->queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700736 return -ENOTTY;
737}
738
739static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
740 unsigned int cmd, unsigned long arg)
741{
Wanlong Gaof4507162012-03-28 14:42:51 -0700742 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700743 int error;
744
745 if (!capable(CAP_SYS_ADMIN))
746 return -EPERM;
747
Wanlong Gaof4507162012-03-28 14:42:51 -0700748 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700749
Wanlong Gaof4507162012-03-28 14:42:51 -0700750 mutex_lock(&nbd->tx_lock);
751 error = __nbd_ioctl(bdev, nbd, cmd, arg);
752 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700753
754 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700757static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200760 .ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761};
762
763/*
764 * And here should be modules and kernel interface
765 * (Just smiley confuses emacs :-)
766 */
767
768static int __init nbd_init(void)
769{
770 int err = -ENOMEM;
771 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700772 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800774 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700776 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +0200777 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700778 return -EINVAL;
779 }
780
781 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +0200782 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700783 part_shift = fls(max_part);
784
Namhyung Kim5988ce22011-05-28 14:44:46 +0200785 /*
786 * Adjust max_part according to part_shift as it is exported
787 * to user space so that user can know the max number of
788 * partition kernel should be able to manage.
789 *
790 * Note that -1 is required because partition 0 is reserved
791 * for the whole disk.
792 */
793 max_part = (1UL << part_shift) - 1;
794 }
795
Namhyung Kim3b271082011-05-28 14:44:46 +0200796 if ((1UL << part_shift) > DISK_MAX_PARTS)
797 return -EINVAL;
798
799 if (nbds_max > 1UL << (MINORBITS - part_shift))
800 return -EINVAL;
801
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +0530802 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
803 if (!nbd_dev)
804 return -ENOMEM;
805
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700806 for (i = 0; i < nbds_max; i++) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700807 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!disk)
809 goto out;
810 nbd_dev[i].disk = disk;
811 /*
812 * The new linux 2.5 block layer implementation requires
813 * every gendisk to have its very own request_queue struct.
814 * These structs are big so we dynamically allocate them.
815 */
816 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
817 if (!disk->queue) {
818 put_disk(disk);
819 goto out;
820 }
Jens Axboe31dcfab2008-10-31 10:06:37 +0100821 /*
822 * Tell the block layer that we are not a rotational device
823 */
824 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600825 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -0700826 disk->queue->limits.discard_granularity = 512;
827 disk->queue->limits.max_discard_sectors = UINT_MAX;
828 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -0700829 blk_queue_max_hw_sectors(disk->queue, 65536);
830 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 if (register_blkdev(NBD_MAJOR, "nbd")) {
834 err = -EIO;
835 goto out;
836 }
837
838 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700840 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -0700842 nbd_dev[i].magic = NBD_MAGIC;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700843 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 spin_lock_init(&nbd_dev[i].queue_lock);
845 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
Ingo Molnar82d4dc52006-03-23 03:00:38 -0800846 mutex_init(&nbd_dev[i].tx_lock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800847 init_waitqueue_head(&nbd_dev[i].active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700848 init_waitqueue_head(&nbd_dev[i].waiting_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 nbd_dev[i].blksize = 1024;
Paul Clements4b86a872007-10-16 23:27:36 -0700850 nbd_dev[i].bytesize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700852 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 disk->fops = &nbd_fops;
854 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 sprintf(disk->disk_name, "nbd%d", i);
Paul Clements4b86a872007-10-16 23:27:36 -0700856 set_capacity(disk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 add_disk(disk);
858 }
859
860 return 0;
861out:
862 while (i--) {
863 blk_cleanup_queue(nbd_dev[i].disk->queue);
864 put_disk(nbd_dev[i].disk);
865 }
Sven Wegenerf3944d62008-08-20 14:09:07 -0700866 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return err;
868}
869
870static void __exit nbd_cleanup(void)
871{
872 int i;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700873 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700875 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (disk) {
877 del_gendisk(disk);
878 blk_cleanup_queue(disk->queue);
879 put_disk(disk);
880 }
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -0700883 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
885}
886
887module_init(nbd_init);
888module_exit(nbd_cleanup);
889
890MODULE_DESCRIPTION("Network Block Device");
891MODULE_LICENSE("GPL");
892
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -0700893module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700894MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
895module_param(max_part, int, 0444);
896MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");