]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/block/nbd.c
block: move PM request support to IDE
[mirror_ubuntu-bionic-kernel.git] / drivers / block / nbd.c
CommitLineData
1da177e4
LT
1/*
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 *
a2531293 7 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
1da177e4
LT
8 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
9 *
dbf492d6 10 * This file is released under GPLv2 or later.
1da177e4 11 *
dbf492d6 12 * (part of code stolen from loop.c)
1da177e4
LT
13 */
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>
2a48fc0a 27#include <linux/mutex.h>
4b2f0260
HX
28#include <linux/compiler.h>
29#include <linux/err.h>
30#include <linux/kernel.h>
5a0e3ad6 31#include <linux/slab.h>
1da177e4 32#include <net/sock.h>
91cf45f0 33#include <linux/net.h>
48cf6061 34#include <linux/kthread.h>
b9c495bb 35#include <linux/types.h>
1da177e4 36
1da177e4
LT
37#include <asm/uaccess.h>
38#include <asm/types.h>
39
40#include <linux/nbd.h>
41
13e71d69
MP
42struct 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;
b9c495bb 58 loff_t bytesize;
13e71d69
MP
59 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
f4507164 64#define NBD_MAGIC 0x68797548
1da177e4 65
9c7a4169 66static unsigned int nbds_max = 16;
20a8143e 67static struct nbd_device *nbd_dev;
d71a6d73 68static int max_part;
1da177e4
LT
69
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
d18509f5 82static inline struct device *nbd_to_dev(struct nbd_device *nbd)
1da177e4 83{
d18509f5 84 return disk_to_dev(nbd->disk);
1da177e4
LT
85}
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";
75f187ab 93 case NBD_CMD_FLUSH: return "flush";
a336d298 94 case NBD_CMD_TRIM: return "trim/discard";
1da177e4
LT
95 }
96 return "invalid";
97}
1da177e4 98
d18509f5 99static void nbd_end_request(struct nbd_device *nbd, struct request *req)
1da177e4 100{
097c94a4 101 int error = req->errors ? -EIO : 0;
165125e1 102 struct request_queue *q = req->q;
1da177e4
LT
103 unsigned long flags;
104
d18509f5
MP
105 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req,
106 error ? "failed" : "done");
1da177e4
LT
107
108 spin_lock_irqsave(q->queue_lock, flags);
1011c1b9 109 __blk_end_request_all(req, error);
1da177e4
LT
110 spin_unlock_irqrestore(q->queue_lock, flags);
111}
112
e018e757
MP
113/*
114 * Forcibly shutdown the socket causing all listeners to error
115 */
f4507164 116static void sock_shutdown(struct nbd_device *nbd, int lock)
7fdfd406 117{
7fdfd406 118 if (lock)
f4507164
WG
119 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;
7fdfd406
PC
124 }
125 if (lock)
f4507164 126 mutex_unlock(&nbd->tx_lock);
7fdfd406
PC
127}
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
1da177e4
LT
138/*
139 * Send or receive packet.
140 */
f4507164 141static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
1da177e4
LT
142 int msg_flags)
143{
f4507164 144 struct socket *sock = nbd->sock;
1da177e4
LT
145 int result;
146 struct msghdr msg;
147 struct kvec iov;
be0ef957 148 sigset_t blocked, oldset;
7f338fe4 149 unsigned long pflags = current->flags;
1da177e4 150
ffc41cf8 151 if (unlikely(!sock)) {
f4507164 152 dev_err(disk_to_dev(nbd->disk),
7f1b90f9
WC
153 "Attempted %s on closed socket in sock_xmit\n",
154 (send ? "send" : "recv"));
ffc41cf8
MS
155 return -EINVAL;
156 }
157
1da177e4
LT
158 /* Allow interception of SIGKILL only
159 * Don't allow other signals to interrupt the transmission */
be0ef957
ON
160 siginitsetinv(&blocked, sigmask(SIGKILL));
161 sigprocmask(SIG_SETMASK, &blocked, &oldset);
1da177e4 162
7f338fe4 163 current->flags |= PF_MEMALLOC;
1da177e4 164 do {
7f338fe4 165 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
1da177e4
LT
166 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;
1da177e4
LT
172 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
173
7fdfd406
PC
174 if (send) {
175 struct timer_list ti;
176
f4507164 177 if (nbd->xmit_timeout) {
7fdfd406
PC
178 init_timer(&ti);
179 ti.function = nbd_xmit_timeout;
180 ti.data = (unsigned long)current;
f4507164 181 ti.expires = jiffies + nbd->xmit_timeout;
7fdfd406
PC
182 add_timer(&ti);
183 }
1da177e4 184 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
f4507164 185 if (nbd->xmit_timeout)
7fdfd406
PC
186 del_timer_sync(&ti);
187 } else
35fbf5bc
NK
188 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
189 msg.msg_flags);
1da177e4
LT
190
191 if (signal_pending(current)) {
192 siginfo_t info;
1da177e4 193 printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n",
ba25f9dc 194 task_pid_nr(current), current->comm,
be0ef957 195 dequeue_signal_lock(current, &current->blocked, &info));
1da177e4 196 result = -EINTR;
f4507164 197 sock_shutdown(nbd, !send);
1da177e4
LT
198 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
be0ef957 210 sigprocmask(SIG_SETMASK, &oldset, NULL);
7f338fe4 211 tsk_restore_flags(current, pflags, PF_MEMALLOC);
1da177e4
LT
212
213 return result;
214}
215
f4507164 216static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
1da177e4
LT
217 int flags)
218{
219 int result;
220 void *kaddr = kmap(bvec->bv_page);
f4507164
WG
221 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
222 bvec->bv_len, flags);
1da177e4
LT
223 kunmap(bvec->bv_page);
224 return result;
225}
226
7fdfd406 227/* always call with the tx_lock held */
f4507164 228static int nbd_send_req(struct nbd_device *nbd, struct request *req)
1da177e4 229{
5705f702 230 int result, flags;
1da177e4 231 struct nbd_request request;
1011c1b9 232 unsigned long size = blk_rq_bytes(req);
1da177e4 233
04cfac4e 234 memset(&request, 0, sizeof(request));
1da177e4
LT
235 request.magic = htonl(NBD_REQUEST_MAGIC);
236 request.type = htonl(nbd_cmd(req));
75f187ab 237
04cfac4e 238 if (nbd_cmd(req) != NBD_CMD_FLUSH && nbd_cmd(req) != NBD_CMD_DISC) {
75f187ab
AB
239 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
240 request.len = htonl(size);
241 }
1da177e4
LT
242 memcpy(request.handle, &req, sizeof(req));
243
d18509f5
MP
244 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));
f4507164 247 result = sock_xmit(nbd, 1, &request, sizeof(request),
7fdfd406 248 (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0);
1da177e4 249 if (result <= 0) {
f4507164 250 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 251 "Send control failed (result %d)\n", result);
dab5313a 252 return -EIO;
1da177e4
LT
253 }
254
255 if (nbd_cmd(req) == NBD_CMD_WRITE) {
5705f702 256 struct req_iterator iter;
7988613b 257 struct bio_vec bvec;
1da177e4
LT
258 /*
259 * we are really probing at internals to determine
260 * whether to set MSG_MORE or not...
261 */
5705f702 262 rq_for_each_segment(bvec, req, iter) {
6c92e699 263 flags = 0;
4550dd6c 264 if (!rq_iter_last(bvec, iter))
6c92e699 265 flags = MSG_MORE;
d18509f5
MP
266 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
267 req, bvec.bv_len);
7988613b 268 result = sock_send_bvec(nbd, &bvec, flags);
6c92e699 269 if (result <= 0) {
f4507164 270 dev_err(disk_to_dev(nbd->disk),
7f1b90f9
WC
271 "Send data failed (result %d)\n",
272 result);
dab5313a 273 return -EIO;
6c92e699 274 }
1da177e4
LT
275 }
276 }
1da177e4 277 return 0;
1da177e4
LT
278}
279
f4507164 280static struct request *nbd_find_request(struct nbd_device *nbd,
0cbc591b 281 struct request *xreq)
1da177e4 282{
d2c9740b 283 struct request *req, *tmp;
4b2f0260 284 int err;
1da177e4 285
f4507164 286 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
4b2f0260 287 if (unlikely(err))
de9ad6d4 288 return ERR_PTR(err);
4b2f0260 289
f4507164
WG
290 spin_lock(&nbd->queue_lock);
291 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
1da177e4
LT
292 if (req != xreq)
293 continue;
294 list_del_init(&req->queuelist);
f4507164 295 spin_unlock(&nbd->queue_lock);
1da177e4
LT
296 return req;
297 }
f4507164 298 spin_unlock(&nbd->queue_lock);
4b2f0260 299
de9ad6d4 300 return ERR_PTR(-ENOENT);
1da177e4
LT
301}
302
f4507164 303static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
1da177e4
LT
304{
305 int result;
306 void *kaddr = kmap(bvec->bv_page);
f4507164 307 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
1da177e4
LT
308 MSG_WAITALL);
309 kunmap(bvec->bv_page);
310 return result;
311}
312
313/* NULL returned = something went wrong, inform userspace */
f4507164 314static struct request *nbd_read_stat(struct nbd_device *nbd)
1da177e4
LT
315{
316 int result;
317 struct nbd_reply reply;
318 struct request *req;
1da177e4
LT
319
320 reply.magic = 0;
f4507164 321 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
1da177e4 322 if (result <= 0) {
f4507164 323 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 324 "Receive control failed (result %d)\n", result);
1da177e4
LT
325 goto harderror;
326 }
e4b57e08
MF
327
328 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
f4507164 329 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
e4b57e08
MF
330 (unsigned long)ntohl(reply.magic));
331 result = -EPROTO;
332 goto harderror;
333 }
334
f4507164 335 req = nbd_find_request(nbd, *(struct request **)reply.handle);
801678c5 336 if (IS_ERR(req)) {
4b2f0260
HX
337 result = PTR_ERR(req);
338 if (result != -ENOENT)
339 goto harderror;
340
f4507164 341 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
7f1b90f9 342 reply.handle);
1da177e4
LT
343 result = -EBADR;
344 goto harderror;
345 }
346
1da177e4 347 if (ntohl(reply.error)) {
f4507164 348 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
7f1b90f9 349 ntohl(reply.error));
1da177e4
LT
350 req->errors++;
351 return req;
352 }
353
d18509f5 354 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
1da177e4 355 if (nbd_cmd(req) == NBD_CMD_READ) {
5705f702 356 struct req_iterator iter;
7988613b 357 struct bio_vec bvec;
5705f702
N
358
359 rq_for_each_segment(bvec, req, iter) {
7988613b 360 result = sock_recv_bvec(nbd, &bvec);
6c92e699 361 if (result <= 0) {
f4507164 362 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
7f1b90f9 363 result);
6c92e699
JA
364 req->errors++;
365 return req;
366 }
d18509f5
MP
367 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
368 req, bvec.bv_len);
1da177e4
LT
369 }
370 }
371 return req;
372harderror:
f4507164 373 nbd->harderror = result;
1da177e4
LT
374 return NULL;
375}
376
edfaa7c3
KS
377static ssize_t pid_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
6b39bb65 379{
edfaa7c3
KS
380 struct gendisk *disk = dev_to_disk(dev);
381
382 return sprintf(buf, "%ld\n",
6b39bb65
PC
383 (long) ((struct nbd_device *)disk->private_data)->pid);
384}
385
edfaa7c3 386static struct device_attribute pid_attr = {
01e8ef11 387 .attr = { .name = "pid", .mode = S_IRUGO},
6b39bb65
PC
388 .show = pid_show,
389};
390
f4507164 391static int nbd_do_it(struct nbd_device *nbd)
1da177e4
LT
392{
393 struct request *req;
84963048 394 int ret;
1da177e4 395
f4507164 396 BUG_ON(nbd->magic != NBD_MAGIC);
1da177e4 397
7f338fe4 398 sk_set_memalloc(nbd->sock->sk);
f4507164
WG
399 nbd->pid = task_pid_nr(current);
400 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
84963048 401 if (ret) {
f4507164
WG
402 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
403 nbd->pid = 0;
84963048
WC
404 return ret;
405 }
6b39bb65 406
f4507164 407 while ((req = nbd_read_stat(nbd)) != NULL)
d18509f5 408 nbd_end_request(nbd, req);
6b39bb65 409
f4507164
WG
410 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
411 nbd->pid = 0;
84963048 412 return 0;
1da177e4
LT
413}
414
f4507164 415static void nbd_clear_que(struct nbd_device *nbd)
1da177e4
LT
416{
417 struct request *req;
418
f4507164 419 BUG_ON(nbd->magic != NBD_MAGIC);
1da177e4 420
4b2f0260 421 /*
f4507164 422 * Because we have set nbd->sock to NULL under the tx_lock, all
4b2f0260
HX
423 * 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 */
f4507164
WG
429 BUG_ON(nbd->sock);
430 BUG_ON(nbd->active_req);
4b2f0260 431
f4507164
WG
432 while (!list_empty(&nbd->queue_head)) {
433 req = list_entry(nbd->queue_head.next, struct request,
4b2f0260
HX
434 queuelist);
435 list_del_init(&req->queuelist);
436 req->errors++;
d18509f5 437 nbd_end_request(nbd, req);
4b2f0260 438 }
fded4e09
PC
439
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++;
d18509f5 445 nbd_end_request(nbd, req);
fded4e09 446 }
1da177e4
LT
447}
448
7fdfd406 449
f4507164 450static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
48cf6061 451{
33659ebb 452 if (req->cmd_type != REQ_TYPE_FS)
48cf6061
LV
453 goto error_out;
454
455 nbd_cmd(req) = NBD_CMD_READ;
456 if (rq_data_dir(req) == WRITE) {
a336d298
PC
457 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;
2f012508 462 if (nbd->flags & NBD_FLAG_READ_ONLY) {
f4507164 463 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 464 "Write on read-only\n");
48cf6061
LV
465 goto error_out;
466 }
467 }
468
75f187ab
AB
469 if (req->cmd_flags & REQ_FLUSH) {
470 BUG_ON(unlikely(blk_rq_sectors(req)));
471 nbd_cmd(req) = NBD_CMD_FLUSH;
472 }
473
48cf6061
LV
474 req->errors = 0;
475
f4507164
WG
476 mutex_lock(&nbd->tx_lock);
477 if (unlikely(!nbd->sock)) {
478 mutex_unlock(&nbd->tx_lock);
479 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 480 "Attempted send on closed socket\n");
15746fca 481 goto error_out;
48cf6061
LV
482 }
483
f4507164 484 nbd->active_req = req;
48cf6061 485
f4507164
WG
486 if (nbd_send_req(nbd, req) != 0) {
487 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
48cf6061 488 req->errors++;
d18509f5 489 nbd_end_request(nbd, req);
48cf6061 490 } else {
f4507164 491 spin_lock(&nbd->queue_lock);
01ff5dbc 492 list_add_tail(&req->queuelist, &nbd->queue_head);
f4507164 493 spin_unlock(&nbd->queue_lock);
48cf6061
LV
494 }
495
f4507164
WG
496 nbd->active_req = NULL;
497 mutex_unlock(&nbd->tx_lock);
498 wake_up_all(&nbd->active_wq);
48cf6061
LV
499
500 return;
501
502error_out:
503 req->errors++;
d18509f5 504 nbd_end_request(nbd, req);
48cf6061
LV
505}
506
507static int nbd_thread(void *data)
508{
f4507164 509 struct nbd_device *nbd = data;
48cf6061
LV
510 struct request *req;
511
8698a745 512 set_user_nice(current, MIN_NICE);
f4507164 513 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
48cf6061 514 /* wait for something to do */
f4507164 515 wait_event_interruptible(nbd->waiting_wq,
48cf6061 516 kthread_should_stop() ||
f4507164 517 !list_empty(&nbd->waiting_queue));
48cf6061
LV
518
519 /* extract request */
f4507164 520 if (list_empty(&nbd->waiting_queue))
48cf6061
LV
521 continue;
522
f4507164
WG
523 spin_lock_irq(&nbd->queue_lock);
524 req = list_entry(nbd->waiting_queue.next, struct request,
48cf6061
LV
525 queuelist);
526 list_del_init(&req->queuelist);
f4507164 527 spin_unlock_irq(&nbd->queue_lock);
48cf6061
LV
528
529 /* handle request */
f4507164 530 nbd_handle_req(nbd, req);
48cf6061
LV
531 }
532 return 0;
533}
534
1da177e4
LT
535/*
536 * We always wait for result of write, for now. It would be nice to make it optional
537 * in future
f4507164 538 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
1da177e4
LT
539 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
540 */
541
15746fca 542static void do_nbd_request(struct request_queue *q)
398eb085 543 __releases(q->queue_lock) __acquires(q->queue_lock)
1da177e4
LT
544{
545 struct request *req;
546
9934c8c0 547 while ((req = blk_fetch_request(q)) != NULL) {
f4507164 548 struct nbd_device *nbd;
1da177e4 549
48cf6061
LV
550 spin_unlock_irq(q->queue_lock);
551
f4507164 552 nbd = req->rq_disk->private_data;
1da177e4 553
f4507164 554 BUG_ON(nbd->magic != NBD_MAGIC);
1da177e4 555
d18509f5
MP
556 dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n",
557 req, req->cmd_type);
558
f4507164
WG
559 if (unlikely(!nbd->sock)) {
560 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 561 "Attempted send on closed socket\n");
4d48a542 562 req->errors++;
d18509f5 563 nbd_end_request(nbd, req);
4d48a542
PC
564 spin_lock_irq(q->queue_lock);
565 continue;
566 }
567
f4507164
WG
568 spin_lock_irq(&nbd->queue_lock);
569 list_add_tail(&req->queuelist, &nbd->waiting_queue);
570 spin_unlock_irq(&nbd->queue_lock);
1da177e4 571
f4507164 572 wake_up(&nbd->waiting_wq);
4b2f0260 573
1da177e4 574 spin_lock_irq(q->queue_lock);
1da177e4 575 }
1da177e4
LT
576}
577
1a2ad211 578/* Must be called with tx_lock held */
1da177e4 579
f4507164 580static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
1a2ad211
PM
581 unsigned int cmd, unsigned long arg)
582{
1da177e4 583 switch (cmd) {
1a2ad211
PM
584 case NBD_DISCONNECT: {
585 struct request sreq;
586
f4507164 587 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
3a2d63f8
PB
588 if (!nbd->sock)
589 return -EINVAL;
1a2ad211 590
3a2d63f8
PB
591 mutex_unlock(&nbd->tx_lock);
592 fsync_bdev(bdev);
593 mutex_lock(&nbd->tx_lock);
4f54eec8 594 blk_rq_init(NULL, &sreq);
4f8c9510 595 sreq.cmd_type = REQ_TYPE_DRV_PRIV;
1da177e4 596 nbd_cmd(&sreq) = NBD_CMD_DISC;
3a2d63f8
PB
597
598 /* Check again after getting mutex back. */
f4507164 599 if (!nbd->sock)
1da177e4 600 return -EINVAL;
3a2d63f8 601
c378f70a
PC
602 nbd->disconnect = 1;
603
f4507164 604 nbd_send_req(nbd, &sreq);
c378f70a 605 return 0;
1a2ad211 606 }
1da177e4 607
1a2ad211 608 case NBD_CLEAR_SOCK: {
e2511578 609 struct socket *sock = nbd->sock;
f4507164 610 nbd->sock = NULL;
f4507164
WG
611 nbd_clear_que(nbd);
612 BUG_ON(!list_empty(&nbd->queue_head));
fded4e09 613 BUG_ON(!list_empty(&nbd->waiting_queue));
3a2d63f8 614 kill_bdev(bdev);
e2511578
AV
615 if (sock)
616 sockfd_put(sock);
1a2ad211
PM
617 return 0;
618 }
619
620 case NBD_SET_SOCK: {
e2511578
AV
621 struct socket *sock;
622 int err;
623 if (nbd->sock)
1da177e4 624 return -EBUSY;
e2511578
AV
625 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;
1da177e4 632 }
1a2ad211
PM
633 return -EINVAL;
634 }
635
1da177e4 636 case NBD_SET_BLKSIZE:
f4507164
WG
637 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);
1da177e4 642 return 0;
1a2ad211 643
1da177e4 644 case NBD_SET_SIZE:
f4507164
WG
645 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);
1da177e4 649 return 0;
1a2ad211 650
7fdfd406 651 case NBD_SET_TIMEOUT:
f4507164 652 nbd->xmit_timeout = arg * HZ;
7fdfd406 653 return 0;
1a2ad211 654
2f012508
PC
655 case NBD_SET_FLAGS:
656 nbd->flags = arg;
657 return 0;
658
1da177e4 659 case NBD_SET_SIZE_BLOCKS:
f4507164
WG
660 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);
1da177e4 664 return 0;
1a2ad211
PM
665
666 case NBD_DO_IT: {
667 struct task_struct *thread;
e2511578 668 struct socket *sock;
1a2ad211
PM
669 int error;
670
f4507164 671 if (nbd->pid)
c91192d6 672 return -EBUSY;
e2511578 673 if (!nbd->sock)
1da177e4 674 return -EINVAL;
1a2ad211 675
f4507164 676 mutex_unlock(&nbd->tx_lock);
1a2ad211 677
a83e814b
PB
678 if (nbd->flags & NBD_FLAG_READ_ONLY)
679 set_device_ro(bdev, true);
a336d298
PC
680 if (nbd->flags & NBD_FLAG_SEND_TRIM)
681 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
682 nbd->disk->queue);
75f187ab
AB
683 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);
a336d298 687
d06df60b
MP
688 thread = kthread_run(nbd_thread, nbd, "%s",
689 nbd->disk->disk_name);
1a2ad211 690 if (IS_ERR(thread)) {
f4507164 691 mutex_lock(&nbd->tx_lock);
48cf6061 692 return PTR_ERR(thread);
1a2ad211 693 }
d06df60b 694
f4507164 695 error = nbd_do_it(nbd);
48cf6061 696 kthread_stop(thread);
1a2ad211 697
f4507164 698 mutex_lock(&nbd->tx_lock);
84963048
WC
699 if (error)
700 return error;
f4507164 701 sock_shutdown(nbd, 0);
e2511578
AV
702 sock = nbd->sock;
703 nbd->sock = NULL;
f4507164
WG
704 nbd_clear_que(nbd);
705 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
3a2d63f8 706 kill_bdev(bdev);
a336d298 707 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
a83e814b 708 set_device_ro(bdev, false);
e2511578
AV
709 if (sock)
710 sockfd_put(sock);
75f187ab 711 nbd->flags = 0;
f4507164 712 nbd->bytesize = 0;
a8cdc308 713 bdev->bd_inode->i_size = 0;
f4507164 714 set_capacity(nbd->disk, 0);
d71a6d73 715 if (max_part > 0)
a8cdc308 716 ioctl_by_bdev(bdev, BLKRRPART, 0);
c378f70a
PC
717 if (nbd->disconnect) /* user requested, ignore socket errors */
718 return 0;
f4507164 719 return nbd->harderror;
1a2ad211
PM
720 }
721
1da177e4 722 case NBD_CLEAR_QUE:
4b2f0260
HX
723 /*
724 * This is for compatibility only. The queue is always cleared
725 * by NBD_DO_IT or NBD_CLEAR_SOCK.
726 */
1da177e4 727 return 0;
1a2ad211 728
1da177e4 729 case NBD_PRINT_DEBUG:
f4507164 730 dev_info(disk_to_dev(nbd->disk),
5eedf541 731 "next = %p, prev = %p, head = %p\n",
f4507164
WG
732 nbd->queue_head.next, nbd->queue_head.prev,
733 &nbd->queue_head);
1da177e4
LT
734 return 0;
735 }
1a2ad211
PM
736 return -ENOTTY;
737}
738
739static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
740 unsigned int cmd, unsigned long arg)
741{
f4507164 742 struct nbd_device *nbd = bdev->bd_disk->private_data;
1a2ad211
PM
743 int error;
744
745 if (!capable(CAP_SYS_ADMIN))
746 return -EPERM;
747
f4507164 748 BUG_ON(nbd->magic != NBD_MAGIC);
1a2ad211 749
f4507164
WG
750 mutex_lock(&nbd->tx_lock);
751 error = __nbd_ioctl(bdev, nbd, cmd, arg);
752 mutex_unlock(&nbd->tx_lock);
1a2ad211
PM
753
754 return error;
1da177e4
LT
755}
756
83d5cde4 757static const struct block_device_operations nbd_fops =
1da177e4
LT
758{
759 .owner = THIS_MODULE,
8a6cfeb6 760 .ioctl = nbd_ioctl,
1da177e4
LT
761};
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;
d71a6d73 772 int part_shift;
1da177e4 773
5b7b18cc 774 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
1da177e4 775
d71a6d73 776 if (max_part < 0) {
7742ce4a 777 printk(KERN_ERR "nbd: max_part must be >= 0\n");
d71a6d73
LV
778 return -EINVAL;
779 }
780
781 part_shift = 0;
5988ce23 782 if (max_part > 0) {
d71a6d73
LV
783 part_shift = fls(max_part);
784
5988ce23
NK
785 /*
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
3b271082
NK
796 if ((1UL << part_shift) > DISK_MAX_PARTS)
797 return -EINVAL;
798
799 if (nbds_max > 1UL << (MINORBITS - part_shift))
800 return -EINVAL;
801
ff6b8090
SM
802 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
803 if (!nbd_dev)
804 return -ENOMEM;
805
40be0c28 806 for (i = 0; i < nbds_max; i++) {
d71a6d73 807 struct gendisk *disk = alloc_disk(1 << part_shift);
1da177e4
LT
808 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 }
31dcfab0
JA
821 /*
822 * Tell the block layer that we are not a rotational device
823 */
824 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
b277da0a 825 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
a336d298
PC
826 disk->queue->limits.discard_granularity = 512;
827 disk->queue->limits.max_discard_sectors = UINT_MAX;
828 disk->queue->limits.discard_zeroes_data = 0;
078be02b
MB
829 blk_queue_max_hw_sectors(disk->queue, 65536);
830 disk->queue->limits.max_sectors = 256;
1da177e4
LT
831 }
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);
1da177e4 839
40be0c28 840 for (i = 0; i < nbds_max; i++) {
1da177e4 841 struct gendisk *disk = nbd_dev[i].disk;
f4507164 842 nbd_dev[i].magic = NBD_MAGIC;
48cf6061 843 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
1da177e4
LT
844 spin_lock_init(&nbd_dev[i].queue_lock);
845 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
82d4dc5a 846 mutex_init(&nbd_dev[i].tx_lock);
4b2f0260 847 init_waitqueue_head(&nbd_dev[i].active_wq);
48cf6061 848 init_waitqueue_head(&nbd_dev[i].waiting_wq);
1da177e4 849 nbd_dev[i].blksize = 1024;
4b86a872 850 nbd_dev[i].bytesize = 0;
1da177e4 851 disk->major = NBD_MAJOR;
d71a6d73 852 disk->first_minor = i << part_shift;
1da177e4
LT
853 disk->fops = &nbd_fops;
854 disk->private_data = &nbd_dev[i];
1da177e4 855 sprintf(disk->disk_name, "nbd%d", i);
4b86a872 856 set_capacity(disk, 0);
1da177e4
LT
857 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 }
f3944d61 866 kfree(nbd_dev);
1da177e4
LT
867 return err;
868}
869
870static void __exit nbd_cleanup(void)
871{
872 int i;
40be0c28 873 for (i = 0; i < nbds_max; i++) {
1da177e4 874 struct gendisk *disk = nbd_dev[i].disk;
40be0c28 875 nbd_dev[i].magic = 0;
1da177e4
LT
876 if (disk) {
877 del_gendisk(disk);
878 blk_cleanup_queue(disk->queue);
879 put_disk(disk);
880 }
881 }
1da177e4 882 unregister_blkdev(NBD_MAJOR, "nbd");
f3944d61 883 kfree(nbd_dev);
1da177e4
LT
884 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
40be0c28 893module_param(nbds_max, int, 0444);
d71a6d73
LV
894MODULE_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)");