blob: e4c5cc1079344110298f0becd938d91b27685924 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Network block device - make block devices work over TCP
3 *
4 * Note that you can not swap over this thing, yet. Seems to work but
5 * deadlocks sometimes - you can not swap over TCP in general.
6 *
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
9 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070010 * This file is released under GPLv2 or later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070012 * (part of code stolen from loop.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/major.h>
16
17#include <linux/blkdev.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/sched.h>
21#include <linux/fs.h>
22#include <linux/bio.h>
23#include <linux/stat.h>
24#include <linux/errno.h>
25#include <linux/file.h>
26#include <linux/ioctl.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020027#include <linux/mutex.h>
Herbert Xu4b2f0262006-01-06 00:09:47 -080028#include <linux/compiler.h>
29#include <linux/err.h>
30#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/sock.h>
Trond Myklebust91cf45f2007-11-12 18:10:39 -080033#include <linux/net.h>
Laurent Vivier48cf6062008-04-29 01:02:46 -070034#include <linux/kthread.h>
Markus Pargmannb9c495b2015-04-02 10:11:37 +020035#include <linux/types.h>
Markus Pargmann30d53d92015-08-17 08:20:06 +020036#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#include <asm/types.h>
40
41#include <linux/nbd.h>
42
Markus Pargmann13e71d62015-04-02 10:11:35 +020043struct nbd_device {
Markus Pargmann22d109c2015-08-17 08:20:09 +020044 u32 flags;
Markus Pargmann13e71d62015-04-02 10:11:35 +020045 struct socket * sock; /* If == NULL, device is not ready, yet */
46 int magic;
47
48 spinlock_t queue_lock;
49 struct list_head queue_head; /* Requests waiting result */
50 struct request *active_req;
51 wait_queue_head_t active_wq;
52 struct list_head waiting_queue; /* Requests to be sent */
53 wait_queue_head_t waiting_wq;
54
55 struct mutex tx_lock;
56 struct gendisk *disk;
57 int blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020058 loff_t bytesize;
Markus Pargmann13e71d62015-04-02 10:11:35 +020059 int xmit_timeout;
Markus Pargmann696697c2015-08-17 08:20:07 +020060 bool disconnect; /* a disconnect has been requested by user */
Markus Pargmann7e2893a2015-08-17 08:20:00 +020061
62 struct timer_list timeout_timer;
Markus Pargmanndcc909d2015-10-06 20:03:54 +020063 spinlock_t tasks_lock;
Markus Pargmann7e2893a2015-08-17 08:20:00 +020064 struct task_struct *task_recv;
65 struct task_struct *task_send;
Markus Pargmann30d53d92015-08-17 08:20:06 +020066
67#if IS_ENABLED(CONFIG_DEBUG_FS)
68 struct dentry *dbg_dir;
69#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +020070};
71
Markus Pargmann30d53d92015-08-17 08:20:06 +020072#if IS_ENABLED(CONFIG_DEBUG_FS)
73static struct dentry *nbd_dbg_dir;
74#endif
75
76#define nbd_name(nbd) ((nbd)->disk->disk_name)
77
Wanlong Gaof4507162012-03-28 14:42:51 -070078#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Ingo van Lil9c7a4162006-07-01 04:36:36 -070080static unsigned int nbds_max = 16;
Paul Clements20a81432008-02-08 04:21:51 -080081static struct nbd_device *nbd_dev;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070082static int max_part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * Use just one lock (or at most 1 per NIC). Two arguments for this:
86 * 1. Each NIC is essentially a synchronization point for all servers
87 * accessed through that NIC so there's no need to have more locks
88 * than NICs anyway.
89 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
90 * down each lock to the point where they're actually slower than just
91 * a single lock.
92 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
93 */
94static DEFINE_SPINLOCK(nbd_lock);
95
Markus Pargmannd18509f2015-04-02 10:11:38 +020096static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Markus Pargmannd18509f2015-04-02 10:11:38 +020098 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static const char *nbdcmd_to_ascii(int cmd)
102{
103 switch (cmd) {
104 case NBD_CMD_READ: return "read";
105 case NBD_CMD_WRITE: return "write";
106 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800107 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700108 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110 return "invalid";
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Markus Pargmannd18509f2015-04-02 10:11:38 +0200113static void nbd_end_request(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500115 int error = req->errors ? -EIO : 0;
Jens Axboe165125e2007-07-24 09:28:11 +0200116 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long flags;
118
Markus Pargmannd18509f2015-04-02 10:11:38 +0200119 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req,
120 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo1011c1b2009-05-07 22:24:45 +0900123 __blk_end_request_all(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 spin_unlock_irqrestore(q->queue_lock, flags);
125}
126
Markus Pargmanne018e752015-04-02 10:11:39 +0200127/*
128 * Forcibly shutdown the socket causing all listeners to error
129 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200130static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700131{
Markus Pargmann260bbce2015-08-17 08:20:02 +0200132 if (!nbd->sock)
133 return;
134
135 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
136 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
137 nbd->sock = NULL;
138 del_timer_sync(&nbd->timeout_timer);
Paul Clements7fdfd402007-10-16 23:27:37 -0700139}
140
141static void nbd_xmit_timeout(unsigned long arg)
142{
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200143 struct nbd_device *nbd = (struct nbd_device *)arg;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200144 unsigned long flags;
Paul Clements7fdfd402007-10-16 23:27:37 -0700145
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200146 if (list_empty(&nbd->queue_head))
147 return;
148
Markus Pargmann696697c2015-08-17 08:20:07 +0200149 nbd->disconnect = true;
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200150
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200151 spin_lock_irqsave(&nbd->tasks_lock, flags);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200152
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200153 if (nbd->task_recv)
154 force_sig(SIGKILL, nbd->task_recv);
155
156 if (nbd->task_send)
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200157 force_sig(SIGKILL, nbd->task_send);
158
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200159 spin_unlock_irqrestore(&nbd->tasks_lock, flags);
160
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200161 dev_err(nbd_to_dev(nbd), "Connection timed out, killed receiver and sender, shutting down connection\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * Send or receive packet.
166 */
Wanlong Gaof4507162012-03-28 14:42:51 -0700167static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int msg_flags)
169{
Wanlong Gaof4507162012-03-28 14:42:51 -0700170 struct socket *sock = nbd->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int result;
172 struct msghdr msg;
173 struct kvec iov;
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700174 sigset_t blocked, oldset;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700175 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700177 if (unlikely(!sock)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700178 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200179 "Attempted %s on closed socket in sock_xmit\n",
180 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700181 return -EINVAL;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* Allow interception of SIGKILL only
185 * Don't allow other signals to interrupt the transmission */
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700186 siginitsetinv(&blocked, sigmask(SIGKILL));
187 sigprocmask(SIG_SETMASK, &blocked, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Mel Gorman7f338fe2012-07-31 16:44:32 -0700189 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700191 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 iov.iov_base = buf;
193 iov.iov_len = size;
194 msg.msg_name = NULL;
195 msg.msg_namelen = 0;
196 msg.msg_control = NULL;
197 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
199
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200200 if (send)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200202 else
Namhyung Kim35fbf5b2011-05-28 14:44:46 +0200203 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
204 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (result <= 0) {
207 if (result == 0)
208 result = -EPIPE; /* short read */
209 break;
210 }
211 size -= result;
212 buf += result;
213 } while (size > 0);
214
Oleg Nesterovbe0ef952007-07-15 23:41:32 -0700215 sigprocmask(SIG_SETMASK, &oldset, NULL);
Mel Gorman7f338fe2012-07-31 16:44:32 -0700216 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200218 if (!send && nbd->xmit_timeout)
219 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return result;
222}
223
Wanlong Gaof4507162012-03-28 14:42:51 -0700224static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int flags)
226{
227 int result;
228 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700229 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
230 bvec->bv_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 kunmap(bvec->bv_page);
232 return result;
233}
234
Paul Clements7fdfd402007-10-16 23:27:37 -0700235/* always call with the tx_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -0700236static int nbd_send_req(struct nbd_device *nbd, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
NeilBrown5705f702007-09-25 12:35:59 +0200238 int result, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct nbd_request request;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900240 unsigned long size = blk_rq_bytes(req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200241 u32 type;
242
243 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
244 type = NBD_CMD_DISC;
245 else if (req->cmd_flags & REQ_DISCARD)
246 type = NBD_CMD_TRIM;
247 else if (req->cmd_flags & REQ_FLUSH)
248 type = NBD_CMD_FLUSH;
249 else if (rq_data_dir(req) == WRITE)
250 type = NBD_CMD_WRITE;
251 else
252 type = NBD_CMD_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Hani Benhabiles04cfac42014-06-06 14:38:30 -0700254 memset(&request, 0, sizeof(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 request.magic = htonl(NBD_REQUEST_MAGIC);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200256 request.type = htonl(type);
257 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800258 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
259 request.len = htonl(size);
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 memcpy(request.handle, &req, sizeof(req));
262
Markus Pargmannd18509f2015-04-02 10:11:38 +0200263 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200264 req, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200265 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Wanlong Gaof4507162012-03-28 14:42:51 -0700266 result = sock_xmit(nbd, 1, &request, sizeof(request),
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200267 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700269 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200270 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200271 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200274 if (type == NBD_CMD_WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200275 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800276 struct bio_vec bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /*
278 * we are really probing at internals to determine
279 * whether to set MSG_MORE or not...
280 */
NeilBrown5705f702007-09-25 12:35:59 +0200281 rq_for_each_segment(bvec, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200282 flags = 0;
Kent Overstreet4550dd62013-08-07 14:26:21 -0700283 if (!rq_iter_last(bvec, iter))
Jens Axboe6c92e692007-08-16 13:43:12 +0200284 flags = MSG_MORE;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200285 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
286 req, bvec.bv_len);
Kent Overstreet79886132013-11-23 17:19:00 -0800287 result = sock_send_bvec(nbd, &bvec, flags);
Jens Axboe6c92e692007-08-16 13:43:12 +0200288 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700289 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200290 "Send data failed (result %d)\n",
291 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200292 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Wanlong Gaof4507162012-03-28 14:42:51 -0700299static struct request *nbd_find_request(struct nbd_device *nbd,
Denis Cheng0cbc591b2007-10-16 23:26:14 -0700300 struct request *xreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Denis Chengd2c97402007-10-16 23:26:14 -0700302 struct request *req, *tmp;
Herbert Xu4b2f0262006-01-06 00:09:47 -0800303 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Wanlong Gaof4507162012-03-28 14:42:51 -0700305 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800306 if (unlikely(err))
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200307 return ERR_PTR(err);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800308
Wanlong Gaof4507162012-03-28 14:42:51 -0700309 spin_lock(&nbd->queue_lock);
310 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (req != xreq)
312 continue;
313 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700314 spin_unlock(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return req;
316 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700317 spin_unlock(&nbd->queue_lock);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800318
Markus Pargmannde9ad6d2015-04-02 10:11:41 +0200319 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Wanlong Gaof4507162012-03-28 14:42:51 -0700322static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int result;
325 void *kaddr = kmap(bvec->bv_page);
Wanlong Gaof4507162012-03-28 14:42:51 -0700326 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 MSG_WAITALL);
328 kunmap(bvec->bv_page);
329 return result;
330}
331
332/* NULL returned = something went wrong, inform userspace */
Wanlong Gaof4507162012-03-28 14:42:51 -0700333static struct request *nbd_read_stat(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int result;
336 struct nbd_reply reply;
337 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 reply.magic = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700340 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700342 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200343 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200344 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700346
347 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700348 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700349 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200350 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700351 }
352
Wanlong Gaof4507162012-03-28 14:42:51 -0700353 req = nbd_find_request(nbd, *(struct request **)reply.handle);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700354 if (IS_ERR(req)) {
Herbert Xu4b2f0262006-01-06 00:09:47 -0800355 result = PTR_ERR(req);
356 if (result != -ENOENT)
Markus Pargmann19391832015-08-17 08:20:03 +0200357 return ERR_PTR(result);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800358
Wanlong Gaof4507162012-03-28 14:42:51 -0700359 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200360 reply.handle);
Markus Pargmann19391832015-08-17 08:20:03 +0200361 return ERR_PTR(-EBADR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700365 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200366 ntohl(reply.error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 req->errors++;
368 return req;
369 }
370
Markus Pargmannd18509f2015-04-02 10:11:38 +0200371 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200372 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200373 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800374 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200375
376 rq_for_each_segment(bvec, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800377 result = sock_recv_bvec(nbd, &bvec);
Jens Axboe6c92e692007-08-16 13:43:12 +0200378 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700379 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200380 result);
Jens Axboe6c92e692007-08-16 13:43:12 +0200381 req->errors++;
382 return req;
383 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200384 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
385 req, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 }
388 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200391static ssize_t pid_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800393{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200394 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200395 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200396
Markus Pargmann6521d392015-08-17 08:20:05 +0200397 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800398}
399
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200400static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700401 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800402 .show = pid_show,
403};
404
Markus Pargmanncad73b22015-08-17 08:20:08 +0200405static int nbd_thread_recv(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct request *req;
WANG Cong84963042007-05-09 02:33:36 -0700408 int ret;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200409 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Wanlong Gaof4507162012-03-28 14:42:51 -0700411 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Mel Gorman7f338fe2012-07-31 16:44:32 -0700413 sk_set_memalloc(nbd->sock->sk);
Markus Pargmann6521d392015-08-17 08:20:05 +0200414
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200415 spin_lock_irqsave(&nbd->tasks_lock, flags);
Markus Pargmann6521d392015-08-17 08:20:05 +0200416 nbd->task_recv = current;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200417 spin_unlock_irqrestore(&nbd->tasks_lock, flags);
Markus Pargmann6521d392015-08-17 08:20:05 +0200418
Wanlong Gaof4507162012-03-28 14:42:51 -0700419 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
WANG Cong84963042007-05-09 02:33:36 -0700420 if (ret) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700421 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200422
423 spin_lock_irqsave(&nbd->tasks_lock, flags);
Markus Pargmann6521d392015-08-17 08:20:05 +0200424 nbd->task_recv = NULL;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200425 spin_unlock_irqrestore(&nbd->tasks_lock, flags);
426
WANG Cong84963042007-05-09 02:33:36 -0700427 return ret;
428 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800429
Markus Pargmann19391832015-08-17 08:20:03 +0200430 while (1) {
431 req = nbd_read_stat(nbd);
432 if (IS_ERR(req)) {
433 ret = PTR_ERR(req);
434 break;
435 }
436
Markus Pargmannd18509f2015-04-02 10:11:38 +0200437 nbd_end_request(nbd, req);
Markus Pargmann19391832015-08-17 08:20:03 +0200438 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800439
Markus Pargmann6521d392015-08-17 08:20:05 +0200440 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
441
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200442 spin_lock_irqsave(&nbd->tasks_lock, flags);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200443 nbd->task_recv = NULL;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200444 spin_unlock_irqrestore(&nbd->tasks_lock, flags);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200445
446 if (signal_pending(current)) {
Oleg Nesterovbe0e6f22015-11-06 16:32:22 -0800447 ret = kernel_dequeue_signal(NULL);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200448 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
449 task_pid_nr(current), current->comm, ret);
Markus Pargmann36e47be2015-08-17 08:20:01 +0200450 mutex_lock(&nbd->tx_lock);
451 sock_shutdown(nbd);
452 mutex_unlock(&nbd->tx_lock);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200453 ret = -ETIMEDOUT;
454 }
455
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200456 return ret;
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++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200481 nbd_end_request(nbd, req);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800482 }
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++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200489 nbd_end_request(nbd, req);
Paul Clementsfded4e02012-09-17 14:09:02 -0700490 }
Markus Pargmanne78273c2015-08-17 08:20:04 +0200491 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Paul Clements7fdfd402007-10-16 23:27:37 -0700494
Wanlong Gaof4507162012-03-28 14:42:51 -0700495static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700496{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200497 if (req->cmd_type != REQ_TYPE_FS)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700498 goto error_out;
499
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200500 if (rq_data_dir(req) == WRITE &&
501 (nbd->flags & NBD_FLAG_READ_ONLY)) {
502 dev_err(disk_to_dev(nbd->disk),
503 "Write on read-only\n");
504 goto error_out;
Alex Bligh75f187a2013-02-27 17:05:23 -0800505 }
506
Laurent Vivier48cf6062008-04-29 01:02:46 -0700507 req->errors = 0;
508
Wanlong Gaof4507162012-03-28 14:42:51 -0700509 mutex_lock(&nbd->tx_lock);
510 if (unlikely(!nbd->sock)) {
511 mutex_unlock(&nbd->tx_lock);
512 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200513 "Attempted send on closed socket\n");
Pavel Machek15746fc2009-04-02 16:58:42 -0700514 goto error_out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700515 }
516
Wanlong Gaof4507162012-03-28 14:42:51 -0700517 nbd->active_req = req;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700518
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200519 if (nbd->xmit_timeout && list_empty_careful(&nbd->queue_head))
520 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
521
Wanlong Gaof4507162012-03-28 14:42:51 -0700522 if (nbd_send_req(nbd, req) != 0) {
523 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
Laurent Vivier48cf6062008-04-29 01:02:46 -0700524 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200525 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700526 } else {
Wanlong Gaof4507162012-03-28 14:42:51 -0700527 spin_lock(&nbd->queue_lock);
Chetan Loke01ff5db2012-07-31 08:47:13 +0200528 list_add_tail(&req->queuelist, &nbd->queue_head);
Wanlong Gaof4507162012-03-28 14:42:51 -0700529 spin_unlock(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700530 }
531
Wanlong Gaof4507162012-03-28 14:42:51 -0700532 nbd->active_req = NULL;
533 mutex_unlock(&nbd->tx_lock);
534 wake_up_all(&nbd->active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700535
536 return;
537
538error_out:
539 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200540 nbd_end_request(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700541}
542
Markus Pargmanncad73b22015-08-17 08:20:08 +0200543static int nbd_thread_send(void *data)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700544{
Wanlong Gaof4507162012-03-28 14:42:51 -0700545 struct nbd_device *nbd = data;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700546 struct request *req;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200547 unsigned long flags;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700548
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200549 spin_lock_irqsave(&nbd->tasks_lock, flags);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200550 nbd->task_send = current;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200551 spin_unlock_irqrestore(&nbd->tasks_lock, flags);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200552
Dongsheng Yang8698a742014-03-11 18:09:12 +0800553 set_user_nice(current, MIN_NICE);
Wanlong Gaof4507162012-03-28 14:42:51 -0700554 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
Laurent Vivier48cf6062008-04-29 01:02:46 -0700555 /* wait for something to do */
Wanlong Gaof4507162012-03-28 14:42:51 -0700556 wait_event_interruptible(nbd->waiting_wq,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700557 kthread_should_stop() ||
Wanlong Gaof4507162012-03-28 14:42:51 -0700558 !list_empty(&nbd->waiting_queue));
Laurent Vivier48cf6062008-04-29 01:02:46 -0700559
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200560 if (signal_pending(current)) {
Oleg Nesterovbe0e6f22015-11-06 16:32:22 -0800561 int ret = kernel_dequeue_signal(NULL);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200562
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200563 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
564 task_pid_nr(current), current->comm, ret);
Markus Pargmann36e47be2015-08-17 08:20:01 +0200565 mutex_lock(&nbd->tx_lock);
566 sock_shutdown(nbd);
567 mutex_unlock(&nbd->tx_lock);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200568 break;
569 }
570
Laurent Vivier48cf6062008-04-29 01:02:46 -0700571 /* extract request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700572 if (list_empty(&nbd->waiting_queue))
Laurent Vivier48cf6062008-04-29 01:02:46 -0700573 continue;
574
Wanlong Gaof4507162012-03-28 14:42:51 -0700575 spin_lock_irq(&nbd->queue_lock);
576 req = list_entry(nbd->waiting_queue.next, struct request,
Laurent Vivier48cf6062008-04-29 01:02:46 -0700577 queuelist);
578 list_del_init(&req->queuelist);
Wanlong Gaof4507162012-03-28 14:42:51 -0700579 spin_unlock_irq(&nbd->queue_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700580
581 /* handle request */
Wanlong Gaof4507162012-03-28 14:42:51 -0700582 nbd_handle_req(nbd, req);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700583 }
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200584
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200585 spin_lock_irqsave(&nbd->tasks_lock, flags);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200586 nbd->task_send = NULL;
Markus Pargmanndcc909d2015-10-06 20:03:54 +0200587 spin_unlock_irqrestore(&nbd->tasks_lock, flags);
588
589 /* Clear maybe pending signals */
Oleg Nesterovbe0e6f22015-11-06 16:32:22 -0800590 if (signal_pending(current))
591 kernel_dequeue_signal(NULL);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200592
Laurent Vivier48cf6062008-04-29 01:02:46 -0700593 return 0;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
597 * We always wait for result of write, for now. It would be nice to make it optional
598 * in future
Wanlong Gaof4507162012-03-28 14:42:51 -0700599 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
601 */
602
Markus Pargmanncad73b22015-08-17 08:20:08 +0200603static void nbd_request_handler(struct request_queue *q)
Alex Elder398eb082013-02-27 17:05:28 -0800604 __releases(q->queue_lock) __acquires(q->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct request *req;
607
Tejun Heo9934c8c2009-05-08 11:54:16 +0900608 while ((req = blk_fetch_request(q)) != NULL) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700609 struct nbd_device *nbd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Laurent Vivier48cf6062008-04-29 01:02:46 -0700611 spin_unlock_irq(q->queue_lock);
612
Wanlong Gaof4507162012-03-28 14:42:51 -0700613 nbd = req->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Wanlong Gaof4507162012-03-28 14:42:51 -0700615 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Markus Pargmannd18509f2015-04-02 10:11:38 +0200617 dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n",
618 req, req->cmd_type);
619
Wanlong Gaof4507162012-03-28 14:42:51 -0700620 if (unlikely(!nbd->sock)) {
621 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200622 "Attempted send on closed socket\n");
Paul Clements4d48a542009-02-11 13:04:45 -0800623 req->errors++;
Markus Pargmannd18509f2015-04-02 10:11:38 +0200624 nbd_end_request(nbd, req);
Paul Clements4d48a542009-02-11 13:04:45 -0800625 spin_lock_irq(q->queue_lock);
626 continue;
627 }
628
Wanlong Gaof4507162012-03-28 14:42:51 -0700629 spin_lock_irq(&nbd->queue_lock);
630 list_add_tail(&req->queuelist, &nbd->waiting_queue);
631 spin_unlock_irq(&nbd->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Wanlong Gaof4507162012-03-28 14:42:51 -0700633 wake_up(&nbd->waiting_wq);
Herbert Xu4b2f0262006-01-06 00:09:47 -0800634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Markus Pargmann30d53d92015-08-17 08:20:06 +0200639static int nbd_dev_dbg_init(struct nbd_device *nbd);
640static void nbd_dev_dbg_close(struct nbd_device *nbd);
641
Pavel Machek1a2ad212009-04-02 16:58:41 -0700642/* Must be called with tx_lock held */
643
Wanlong Gaof4507162012-03-28 14:42:51 -0700644static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700645 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 switch (cmd) {
Pavel Machek1a2ad212009-04-02 16:58:41 -0700648 case NBD_DISCONNECT: {
649 struct request sreq;
650
Wanlong Gaof4507162012-03-28 14:42:51 -0700651 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800652 if (!nbd->sock)
653 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700654
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800655 mutex_unlock(&nbd->tx_lock);
656 fsync_bdev(bdev);
657 mutex_lock(&nbd->tx_lock);
FUJITA Tomonori4f54eec2008-04-29 09:54:37 +0200658 blk_rq_init(NULL, &sreq);
Christoph Hellwig4f8c9512015-04-17 22:37:16 +0200659 sreq.cmd_type = REQ_TYPE_DRV_PRIV;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800660
661 /* Check again after getting mutex back. */
Wanlong Gaof4507162012-03-28 14:42:51 -0700662 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EINVAL;
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800664
Markus Pargmann696697c2015-08-17 08:20:07 +0200665 nbd->disconnect = true;
Paul Clementsc378f702013-07-03 15:09:04 -0700666
Wanlong Gaof4507162012-03-28 14:42:51 -0700667 nbd_send_req(nbd, &sreq);
Paul Clementsc378f702013-07-03 15:09:04 -0700668 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Pavel Machek1a2ad212009-04-02 16:58:41 -0700671 case NBD_CLEAR_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500672 struct socket *sock = nbd->sock;
Wanlong Gaof4507162012-03-28 14:42:51 -0700673 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700674 nbd_clear_que(nbd);
675 BUG_ON(!list_empty(&nbd->queue_head));
Paul Clementsfded4e02012-09-17 14:09:02 -0700676 BUG_ON(!list_empty(&nbd->waiting_queue));
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800677 kill_bdev(bdev);
Al Viroe2511572014-03-05 20:41:36 -0500678 if (sock)
679 sockfd_put(sock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700680 return 0;
681 }
682
683 case NBD_SET_SOCK: {
Al Viroe2511572014-03-05 20:41:36 -0500684 struct socket *sock;
685 int err;
686 if (nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500688 sock = sockfd_lookup(arg, &err);
689 if (sock) {
690 nbd->sock = sock;
691 if (max_part > 0)
692 bdev->bd_invalidated = 1;
Markus Pargmann696697c2015-08-17 08:20:07 +0200693 nbd->disconnect = false; /* we're connected now */
Al Viroe2511572014-03-05 20:41:36 -0500694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700696 return -EINVAL;
697 }
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case NBD_SET_BLKSIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700700 nbd->blksize = arg;
701 nbd->bytesize &= ~(nbd->blksize-1);
702 bdev->bd_inode->i_size = nbd->bytesize;
703 set_blocksize(bdev, nbd->blksize);
704 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case NBD_SET_SIZE:
Wanlong Gaof4507162012-03-28 14:42:51 -0700708 nbd->bytesize = arg & ~(nbd->blksize-1);
709 bdev->bd_inode->i_size = nbd->bytesize;
710 set_blocksize(bdev, nbd->blksize);
711 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700713
Paul Clements7fdfd402007-10-16 23:27:37 -0700714 case NBD_SET_TIMEOUT:
Wanlong Gaof4507162012-03-28 14:42:51 -0700715 nbd->xmit_timeout = arg * HZ;
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200716 if (arg)
717 mod_timer(&nbd->timeout_timer,
718 jiffies + nbd->xmit_timeout);
719 else
720 del_timer_sync(&nbd->timeout_timer);
721
Paul Clements7fdfd402007-10-16 23:27:37 -0700722 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700723
Paul Clements2f012502012-10-04 17:16:15 -0700724 case NBD_SET_FLAGS:
725 nbd->flags = arg;
726 return 0;
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 case NBD_SET_SIZE_BLOCKS:
Wanlong Gaof4507162012-03-28 14:42:51 -0700729 nbd->bytesize = ((u64) arg) * nbd->blksize;
730 bdev->bd_inode->i_size = nbd->bytesize;
731 set_blocksize(bdev, nbd->blksize);
732 set_capacity(nbd->disk, nbd->bytesize >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700734
735 case NBD_DO_IT: {
736 struct task_struct *thread;
Al Viroe2511572014-03-05 20:41:36 -0500737 struct socket *sock;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700738 int error;
739
Markus Pargmann6521d392015-08-17 08:20:05 +0200740 if (nbd->task_recv)
Pavel Machekc91192d2009-01-15 13:51:03 -0800741 return -EBUSY;
Al Viroe2511572014-03-05 20:41:36 -0500742 if (!nbd->sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -EINVAL;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700744
Wanlong Gaof4507162012-03-28 14:42:51 -0700745 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700746
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800747 if (nbd->flags & NBD_FLAG_READ_ONLY)
748 set_device_ro(bdev, true);
Paul Clementsa336d292012-10-04 17:16:18 -0700749 if (nbd->flags & NBD_FLAG_SEND_TRIM)
750 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
751 nbd->disk->queue);
Alex Bligh75f187a2013-02-27 17:05:23 -0800752 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
753 blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
754 else
755 blk_queue_flush(nbd->disk->queue, 0);
Paul Clementsa336d292012-10-04 17:16:18 -0700756
Markus Pargmanncad73b22015-08-17 08:20:08 +0200757 thread = kthread_run(nbd_thread_send, nbd, "%s",
Markus Pargmann30d53d92015-08-17 08:20:06 +0200758 nbd_name(nbd));
Pavel Machek1a2ad212009-04-02 16:58:41 -0700759 if (IS_ERR(thread)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700760 mutex_lock(&nbd->tx_lock);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700761 return PTR_ERR(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700762 }
Markus Pargmannd06df602015-04-02 10:11:36 +0200763
Markus Pargmann30d53d92015-08-17 08:20:06 +0200764 nbd_dev_dbg_init(nbd);
Markus Pargmanncad73b22015-08-17 08:20:08 +0200765 error = nbd_thread_recv(nbd);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200766 nbd_dev_dbg_close(nbd);
Laurent Vivier48cf6062008-04-29 01:02:46 -0700767 kthread_stop(thread);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700768
Wanlong Gaof4507162012-03-28 14:42:51 -0700769 mutex_lock(&nbd->tx_lock);
Markus Pargmann19391832015-08-17 08:20:03 +0200770
Markus Pargmann36e47be2015-08-17 08:20:01 +0200771 sock_shutdown(nbd);
Al Viroe2511572014-03-05 20:41:36 -0500772 sock = nbd->sock;
773 nbd->sock = NULL;
Wanlong Gaof4507162012-03-28 14:42:51 -0700774 nbd_clear_que(nbd);
Paolo Bonzini3a2d63f82013-02-27 17:05:25 -0800775 kill_bdev(bdev);
Paul Clementsa336d292012-10-04 17:16:18 -0700776 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Paolo Bonzinia83e8142013-02-27 17:05:26 -0800777 set_device_ro(bdev, false);
Al Viroe2511572014-03-05 20:41:36 -0500778 if (sock)
779 sockfd_put(sock);
Alex Bligh75f187a2013-02-27 17:05:23 -0800780 nbd->flags = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700781 nbd->bytesize = 0;
Al Viroa8cdc302008-03-02 09:33:33 -0500782 bdev->bd_inode->i_size = 0;
Wanlong Gaof4507162012-03-28 14:42:51 -0700783 set_capacity(nbd->disk, 0);
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700784 if (max_part > 0)
Ming Lei9dcd1372015-05-06 12:26:25 +0800785 blkdev_reread_part(bdev);
Paul Clementsc378f702013-07-03 15:09:04 -0700786 if (nbd->disconnect) /* user requested, ignore socket errors */
787 return 0;
Markus Pargmann19391832015-08-17 08:20:03 +0200788 return error;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800792 /*
793 * This is for compatibility only. The queue is always cleared
794 * by NBD_DO_IT or NBD_CLEAR_SOCK.
795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 case NBD_PRINT_DEBUG:
Wanlong Gaof4507162012-03-28 14:42:51 -0700799 dev_info(disk_to_dev(nbd->disk),
WANG Cong5eedf542011-08-19 14:48:28 +0200800 "next = %p, prev = %p, head = %p\n",
Wanlong Gaof4507162012-03-28 14:42:51 -0700801 nbd->queue_head.next, nbd->queue_head.prev,
802 &nbd->queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700805 return -ENOTTY;
806}
807
808static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
809 unsigned int cmd, unsigned long arg)
810{
Wanlong Gaof4507162012-03-28 14:42:51 -0700811 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700812 int error;
813
814 if (!capable(CAP_SYS_ADMIN))
815 return -EPERM;
816
Wanlong Gaof4507162012-03-28 14:42:51 -0700817 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700818
Wanlong Gaof4507162012-03-28 14:42:51 -0700819 mutex_lock(&nbd->tx_lock);
820 error = __nbd_ioctl(bdev, nbd, cmd, arg);
821 mutex_unlock(&nbd->tx_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700822
823 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700826static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200829 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500830 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831};
832
Markus Pargmann30d53d92015-08-17 08:20:06 +0200833#if IS_ENABLED(CONFIG_DEBUG_FS)
834
835static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
836{
837 struct nbd_device *nbd = s->private;
838
839 if (nbd->task_recv)
840 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
841 if (nbd->task_send)
842 seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send));
843
844 return 0;
845}
846
847static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
848{
849 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
850}
851
852static const struct file_operations nbd_dbg_tasks_ops = {
853 .open = nbd_dbg_tasks_open,
854 .read = seq_read,
855 .llseek = seq_lseek,
856 .release = single_release,
857};
858
859static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
860{
861 struct nbd_device *nbd = s->private;
862 u32 flags = nbd->flags;
863
864 seq_printf(s, "Hex: 0x%08x\n\n", flags);
865
866 seq_puts(s, "Known flags:\n");
867
868 if (flags & NBD_FLAG_HAS_FLAGS)
869 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
870 if (flags & NBD_FLAG_READ_ONLY)
871 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
872 if (flags & NBD_FLAG_SEND_FLUSH)
873 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
874 if (flags & NBD_FLAG_SEND_TRIM)
875 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
876
877 return 0;
878}
879
880static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
881{
882 return single_open(file, nbd_dbg_flags_show, inode->i_private);
883}
884
885static const struct file_operations nbd_dbg_flags_ops = {
886 .open = nbd_dbg_flags_open,
887 .read = seq_read,
888 .llseek = seq_lseek,
889 .release = single_release,
890};
891
892static int nbd_dev_dbg_init(struct nbd_device *nbd)
893{
894 struct dentry *dir;
895 struct dentry *f;
896
897 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
898 if (IS_ERR_OR_NULL(dir)) {
899 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s' (%ld)\n",
900 nbd_name(nbd), PTR_ERR(dir));
901 return PTR_ERR(dir);
902 }
903 nbd->dbg_dir = dir;
904
905 f = debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
906 if (IS_ERR_OR_NULL(f)) {
907 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'tasks', %ld\n",
908 PTR_ERR(f));
909 return PTR_ERR(f);
910 }
911
912 f = debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
913 if (IS_ERR_OR_NULL(f)) {
914 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'size_bytes', %ld\n",
915 PTR_ERR(f));
916 return PTR_ERR(f);
917 }
918
919 f = debugfs_create_u32("timeout", 0444, dir, &nbd->xmit_timeout);
920 if (IS_ERR_OR_NULL(f)) {
921 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'timeout', %ld\n",
922 PTR_ERR(f));
923 return PTR_ERR(f);
924 }
925
926 f = debugfs_create_u32("blocksize", 0444, dir, &nbd->blksize);
927 if (IS_ERR_OR_NULL(f)) {
928 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'blocksize', %ld\n",
929 PTR_ERR(f));
930 return PTR_ERR(f);
931 }
932
933 f = debugfs_create_file("flags", 0444, dir, &nbd, &nbd_dbg_flags_ops);
934 if (IS_ERR_OR_NULL(f)) {
935 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'flags', %ld\n",
936 PTR_ERR(f));
937 return PTR_ERR(f);
938 }
939
940 return 0;
941}
942
943static void nbd_dev_dbg_close(struct nbd_device *nbd)
944{
945 debugfs_remove_recursive(nbd->dbg_dir);
946}
947
948static int nbd_dbg_init(void)
949{
950 struct dentry *dbg_dir;
951
952 dbg_dir = debugfs_create_dir("nbd", NULL);
953 if (IS_ERR(dbg_dir))
954 return PTR_ERR(dbg_dir);
955
956 nbd_dbg_dir = dbg_dir;
957
958 return 0;
959}
960
961static void nbd_dbg_close(void)
962{
963 debugfs_remove_recursive(nbd_dbg_dir);
964}
965
966#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
967
968static int nbd_dev_dbg_init(struct nbd_device *nbd)
969{
970 return 0;
971}
972
973static void nbd_dev_dbg_close(struct nbd_device *nbd)
974{
975}
976
977static int nbd_dbg_init(void)
978{
979 return 0;
980}
981
982static void nbd_dbg_close(void)
983{
984}
985
986#endif
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/*
989 * And here should be modules and kernel interface
990 * (Just smiley confuses emacs :-)
991 */
992
993static int __init nbd_init(void)
994{
995 int err = -ENOMEM;
996 int i;
Laurent Vivierd71a6d72008-04-29 01:02:51 -0700997 int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Adrian Bunk5b7b18c2006-03-25 03:07:04 -0800999 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001001 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02001002 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001003 return -EINVAL;
1004 }
1005
1006 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02001007 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001008 part_shift = fls(max_part);
1009
Namhyung Kim5988ce22011-05-28 14:44:46 +02001010 /*
1011 * Adjust max_part according to part_shift as it is exported
1012 * to user space so that user can know the max number of
1013 * partition kernel should be able to manage.
1014 *
1015 * Note that -1 is required because partition 0 is reserved
1016 * for the whole disk.
1017 */
1018 max_part = (1UL << part_shift) - 1;
1019 }
1020
Namhyung Kim3b271082011-05-28 14:44:46 +02001021 if ((1UL << part_shift) > DISK_MAX_PARTS)
1022 return -EINVAL;
1023
1024 if (nbds_max > 1UL << (MINORBITS - part_shift))
1025 return -EINVAL;
1026
Sudip Mukherjeeff6b8092015-01-27 18:08:22 +05301027 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
1028 if (!nbd_dev)
1029 return -ENOMEM;
1030
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001031 for (i = 0; i < nbds_max; i++) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001032 struct gendisk *disk = alloc_disk(1 << part_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!disk)
1034 goto out;
1035 nbd_dev[i].disk = disk;
1036 /*
1037 * The new linux 2.5 block layer implementation requires
1038 * every gendisk to have its very own request_queue struct.
1039 * These structs are big so we dynamically allocate them.
1040 */
Markus Pargmanncad73b22015-08-17 08:20:08 +02001041 disk->queue = blk_init_queue(nbd_request_handler, &nbd_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!disk->queue) {
1043 put_disk(disk);
1044 goto out;
1045 }
Jens Axboe31dcfab2008-10-31 10:06:37 +01001046 /*
1047 * Tell the block layer that we are not a rotational device
1048 */
1049 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -06001050 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
Paul Clementsa336d292012-10-04 17:16:18 -07001051 disk->queue->limits.discard_granularity = 512;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06001052 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Paul Clementsa336d292012-10-04 17:16:18 -07001053 disk->queue->limits.discard_zeroes_data = 0;
Michal Belczyk078be022013-04-30 15:28:28 -07001054 blk_queue_max_hw_sectors(disk->queue, 65536);
1055 disk->queue->limits.max_sectors = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
1058 if (register_blkdev(NBD_MAJOR, "nbd")) {
1059 err = -EIO;
1060 goto out;
1061 }
1062
1063 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Markus Pargmann30d53d92015-08-17 08:20:06 +02001065 nbd_dbg_init();
1066
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001067 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct gendisk *disk = nbd_dev[i].disk;
Wanlong Gaof4507162012-03-28 14:42:51 -07001069 nbd_dev[i].magic = NBD_MAGIC;
Laurent Vivier48cf6062008-04-29 01:02:46 -07001070 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 spin_lock_init(&nbd_dev[i].queue_lock);
Markus Pargmanndcc909d2015-10-06 20:03:54 +02001072 spin_lock_init(&nbd_dev[i].tasks_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
Ingo Molnar82d4dc52006-03-23 03:00:38 -08001074 mutex_init(&nbd_dev[i].tx_lock);
Markus Pargmann7e2893a2015-08-17 08:20:00 +02001075 init_timer(&nbd_dev[i].timeout_timer);
1076 nbd_dev[i].timeout_timer.function = nbd_xmit_timeout;
1077 nbd_dev[i].timeout_timer.data = (unsigned long)&nbd_dev[i];
Herbert Xu4b2f0262006-01-06 00:09:47 -08001078 init_waitqueue_head(&nbd_dev[i].active_wq);
Laurent Vivier48cf6062008-04-29 01:02:46 -07001079 init_waitqueue_head(&nbd_dev[i].waiting_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 nbd_dev[i].blksize = 1024;
Paul Clements4b86a872007-10-16 23:27:36 -07001081 nbd_dev[i].bytesize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 disk->major = NBD_MAJOR;
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001083 disk->first_minor = i << part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 disk->fops = &nbd_fops;
1085 disk->private_data = &nbd_dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 sprintf(disk->disk_name, "nbd%d", i);
Paul Clements4b86a872007-10-16 23:27:36 -07001087 set_capacity(disk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 add_disk(disk);
1089 }
1090
1091 return 0;
1092out:
1093 while (i--) {
1094 blk_cleanup_queue(nbd_dev[i].disk->queue);
1095 put_disk(nbd_dev[i].disk);
1096 }
Sven Wegenerf3944d62008-08-20 14:09:07 -07001097 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return err;
1099}
1100
1101static void __exit nbd_cleanup(void)
1102{
1103 int i;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001104
1105 nbd_dbg_close();
1106
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001107 for (i = 0; i < nbds_max; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct gendisk *disk = nbd_dev[i].disk;
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001109 nbd_dev[i].magic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (disk) {
1111 del_gendisk(disk);
1112 blk_cleanup_queue(disk->queue);
1113 put_disk(disk);
1114 }
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 unregister_blkdev(NBD_MAJOR, "nbd");
Sven Wegenerf3944d62008-08-20 14:09:07 -07001117 kfree(nbd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
1119}
1120
1121module_init(nbd_init);
1122module_exit(nbd_cleanup);
1123
1124MODULE_DESCRIPTION("Network Block Device");
1125MODULE_LICENSE("GPL");
1126
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001127module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001128MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1129module_param(max_part, int, 0444);
1130MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");