]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/block/nbd.c
nbd: restructure sock_shutdown
[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 */
7e2893a1
MP
62
63 struct timer_list timeout_timer;
64 struct task_struct *task_recv;
65 struct task_struct *task_send;
13e71d69
MP
66};
67
f4507164 68#define NBD_MAGIC 0x68797548
1da177e4 69
9c7a4169 70static unsigned int nbds_max = 16;
20a8143e 71static struct nbd_device *nbd_dev;
d71a6d73 72static int max_part;
1da177e4
LT
73
74/*
75 * Use just one lock (or at most 1 per NIC). Two arguments for this:
76 * 1. Each NIC is essentially a synchronization point for all servers
77 * accessed through that NIC so there's no need to have more locks
78 * than NICs anyway.
79 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
80 * down each lock to the point where they're actually slower than just
81 * a single lock.
82 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
83 */
84static DEFINE_SPINLOCK(nbd_lock);
85
d18509f5 86static inline struct device *nbd_to_dev(struct nbd_device *nbd)
1da177e4 87{
d18509f5 88 return disk_to_dev(nbd->disk);
1da177e4
LT
89}
90
91static const char *nbdcmd_to_ascii(int cmd)
92{
93 switch (cmd) {
94 case NBD_CMD_READ: return "read";
95 case NBD_CMD_WRITE: return "write";
96 case NBD_CMD_DISC: return "disconnect";
75f187ab 97 case NBD_CMD_FLUSH: return "flush";
a336d298 98 case NBD_CMD_TRIM: return "trim/discard";
1da177e4
LT
99 }
100 return "invalid";
101}
1da177e4 102
d18509f5 103static void nbd_end_request(struct nbd_device *nbd, struct request *req)
1da177e4 104{
097c94a4 105 int error = req->errors ? -EIO : 0;
165125e1 106 struct request_queue *q = req->q;
1da177e4
LT
107 unsigned long flags;
108
d18509f5
MP
109 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req,
110 error ? "failed" : "done");
1da177e4
LT
111
112 spin_lock_irqsave(q->queue_lock, flags);
1011c1b9 113 __blk_end_request_all(req, error);
1da177e4
LT
114 spin_unlock_irqrestore(q->queue_lock, flags);
115}
116
e018e757
MP
117/*
118 * Forcibly shutdown the socket causing all listeners to error
119 */
36e47bee 120static void sock_shutdown(struct nbd_device *nbd)
7fdfd406 121{
260bbce4
MP
122 if (!nbd->sock)
123 return;
124
125 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
126 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
127 nbd->sock = NULL;
128 del_timer_sync(&nbd->timeout_timer);
7fdfd406
PC
129}
130
131static void nbd_xmit_timeout(unsigned long arg)
132{
7e2893a1
MP
133 struct nbd_device *nbd = (struct nbd_device *)arg;
134 struct task_struct *task;
135
136 if (list_empty(&nbd->queue_head))
137 return;
138
139 nbd->disconnect = 1;
140
141 task = READ_ONCE(nbd->task_recv);
142 if (task)
143 force_sig(SIGKILL, task);
7fdfd406 144
7e2893a1
MP
145 task = READ_ONCE(nbd->task_send);
146 if (task)
147 force_sig(SIGKILL, nbd->task_send);
148
149 dev_err(nbd_to_dev(nbd), "Connection timed out, killed receiver and sender, shutting down connection\n");
7fdfd406
PC
150}
151
1da177e4
LT
152/*
153 * Send or receive packet.
154 */
f4507164 155static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
1da177e4
LT
156 int msg_flags)
157{
f4507164 158 struct socket *sock = nbd->sock;
1da177e4
LT
159 int result;
160 struct msghdr msg;
161 struct kvec iov;
be0ef957 162 sigset_t blocked, oldset;
7f338fe4 163 unsigned long pflags = current->flags;
1da177e4 164
ffc41cf8 165 if (unlikely(!sock)) {
f4507164 166 dev_err(disk_to_dev(nbd->disk),
7f1b90f9
WC
167 "Attempted %s on closed socket in sock_xmit\n",
168 (send ? "send" : "recv"));
ffc41cf8
MS
169 return -EINVAL;
170 }
171
1da177e4
LT
172 /* Allow interception of SIGKILL only
173 * Don't allow other signals to interrupt the transmission */
be0ef957
ON
174 siginitsetinv(&blocked, sigmask(SIGKILL));
175 sigprocmask(SIG_SETMASK, &blocked, &oldset);
1da177e4 176
7f338fe4 177 current->flags |= PF_MEMALLOC;
1da177e4 178 do {
7f338fe4 179 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
1da177e4
LT
180 iov.iov_base = buf;
181 iov.iov_len = size;
182 msg.msg_name = NULL;
183 msg.msg_namelen = 0;
184 msg.msg_control = NULL;
185 msg.msg_controllen = 0;
1da177e4
LT
186 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
187
7e2893a1 188 if (send)
1da177e4 189 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
7e2893a1 190 else
35fbf5bc
NK
191 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
192 msg.msg_flags);
1da177e4 193
1da177e4
LT
194 if (result <= 0) {
195 if (result == 0)
196 result = -EPIPE; /* short read */
197 break;
198 }
199 size -= result;
200 buf += result;
201 } while (size > 0);
202
be0ef957 203 sigprocmask(SIG_SETMASK, &oldset, NULL);
7f338fe4 204 tsk_restore_flags(current, pflags, PF_MEMALLOC);
1da177e4 205
7e2893a1
MP
206 if (!send && nbd->xmit_timeout)
207 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
208
1da177e4
LT
209 return result;
210}
211
f4507164 212static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
1da177e4
LT
213 int flags)
214{
215 int result;
216 void *kaddr = kmap(bvec->bv_page);
f4507164
WG
217 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
218 bvec->bv_len, flags);
1da177e4
LT
219 kunmap(bvec->bv_page);
220 return result;
221}
222
7fdfd406 223/* always call with the tx_lock held */
f4507164 224static int nbd_send_req(struct nbd_device *nbd, struct request *req)
1da177e4 225{
5705f702 226 int result, flags;
1da177e4 227 struct nbd_request request;
1011c1b9 228 unsigned long size = blk_rq_bytes(req);
9dc6c806
CH
229 u32 type;
230
231 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
232 type = NBD_CMD_DISC;
233 else if (req->cmd_flags & REQ_DISCARD)
234 type = NBD_CMD_TRIM;
235 else if (req->cmd_flags & REQ_FLUSH)
236 type = NBD_CMD_FLUSH;
237 else if (rq_data_dir(req) == WRITE)
238 type = NBD_CMD_WRITE;
239 else
240 type = NBD_CMD_READ;
1da177e4 241
04cfac4e 242 memset(&request, 0, sizeof(request));
1da177e4 243 request.magic = htonl(NBD_REQUEST_MAGIC);
9dc6c806
CH
244 request.type = htonl(type);
245 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
75f187ab
AB
246 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
247 request.len = htonl(size);
248 }
1da177e4
LT
249 memcpy(request.handle, &req, sizeof(req));
250
d18509f5 251 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
9dc6c806 252 req, nbdcmd_to_ascii(type),
d18509f5 253 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
f4507164 254 result = sock_xmit(nbd, 1, &request, sizeof(request),
9dc6c806 255 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
1da177e4 256 if (result <= 0) {
f4507164 257 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 258 "Send control failed (result %d)\n", result);
dab5313a 259 return -EIO;
1da177e4
LT
260 }
261
9dc6c806 262 if (type == NBD_CMD_WRITE) {
5705f702 263 struct req_iterator iter;
7988613b 264 struct bio_vec bvec;
1da177e4
LT
265 /*
266 * we are really probing at internals to determine
267 * whether to set MSG_MORE or not...
268 */
5705f702 269 rq_for_each_segment(bvec, req, iter) {
6c92e699 270 flags = 0;
4550dd6c 271 if (!rq_iter_last(bvec, iter))
6c92e699 272 flags = MSG_MORE;
d18509f5
MP
273 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
274 req, bvec.bv_len);
7988613b 275 result = sock_send_bvec(nbd, &bvec, flags);
6c92e699 276 if (result <= 0) {
f4507164 277 dev_err(disk_to_dev(nbd->disk),
7f1b90f9
WC
278 "Send data failed (result %d)\n",
279 result);
dab5313a 280 return -EIO;
6c92e699 281 }
1da177e4
LT
282 }
283 }
1da177e4 284 return 0;
1da177e4
LT
285}
286
f4507164 287static struct request *nbd_find_request(struct nbd_device *nbd,
0cbc591b 288 struct request *xreq)
1da177e4 289{
d2c9740b 290 struct request *req, *tmp;
4b2f0260 291 int err;
1da177e4 292
f4507164 293 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
4b2f0260 294 if (unlikely(err))
de9ad6d4 295 return ERR_PTR(err);
4b2f0260 296
f4507164
WG
297 spin_lock(&nbd->queue_lock);
298 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
1da177e4
LT
299 if (req != xreq)
300 continue;
301 list_del_init(&req->queuelist);
f4507164 302 spin_unlock(&nbd->queue_lock);
1da177e4
LT
303 return req;
304 }
f4507164 305 spin_unlock(&nbd->queue_lock);
4b2f0260 306
de9ad6d4 307 return ERR_PTR(-ENOENT);
1da177e4
LT
308}
309
f4507164 310static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
1da177e4
LT
311{
312 int result;
313 void *kaddr = kmap(bvec->bv_page);
f4507164 314 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
1da177e4
LT
315 MSG_WAITALL);
316 kunmap(bvec->bv_page);
317 return result;
318}
319
320/* NULL returned = something went wrong, inform userspace */
f4507164 321static struct request *nbd_read_stat(struct nbd_device *nbd)
1da177e4
LT
322{
323 int result;
324 struct nbd_reply reply;
325 struct request *req;
1da177e4
LT
326
327 reply.magic = 0;
f4507164 328 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
1da177e4 329 if (result <= 0) {
f4507164 330 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 331 "Receive control failed (result %d)\n", result);
1da177e4
LT
332 goto harderror;
333 }
e4b57e08
MF
334
335 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
f4507164 336 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
e4b57e08
MF
337 (unsigned long)ntohl(reply.magic));
338 result = -EPROTO;
339 goto harderror;
340 }
341
f4507164 342 req = nbd_find_request(nbd, *(struct request **)reply.handle);
801678c5 343 if (IS_ERR(req)) {
4b2f0260
HX
344 result = PTR_ERR(req);
345 if (result != -ENOENT)
346 goto harderror;
347
f4507164 348 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
7f1b90f9 349 reply.handle);
1da177e4
LT
350 result = -EBADR;
351 goto harderror;
352 }
353
1da177e4 354 if (ntohl(reply.error)) {
f4507164 355 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
7f1b90f9 356 ntohl(reply.error));
1da177e4
LT
357 req->errors++;
358 return req;
359 }
360
d18509f5 361 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
9dc6c806 362 if (rq_data_dir(req) != WRITE) {
5705f702 363 struct req_iterator iter;
7988613b 364 struct bio_vec bvec;
5705f702
N
365
366 rq_for_each_segment(bvec, req, iter) {
7988613b 367 result = sock_recv_bvec(nbd, &bvec);
6c92e699 368 if (result <= 0) {
f4507164 369 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
7f1b90f9 370 result);
6c92e699
JA
371 req->errors++;
372 return req;
373 }
d18509f5
MP
374 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
375 req, bvec.bv_len);
1da177e4
LT
376 }
377 }
378 return req;
379harderror:
f4507164 380 nbd->harderror = result;
1da177e4
LT
381 return NULL;
382}
383
edfaa7c3
KS
384static ssize_t pid_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
6b39bb65 386{
edfaa7c3
KS
387 struct gendisk *disk = dev_to_disk(dev);
388
389 return sprintf(buf, "%ld\n",
6b39bb65
PC
390 (long) ((struct nbd_device *)disk->private_data)->pid);
391}
392
edfaa7c3 393static struct device_attribute pid_attr = {
01e8ef11 394 .attr = { .name = "pid", .mode = S_IRUGO},
6b39bb65
PC
395 .show = pid_show,
396};
397
f4507164 398static int nbd_do_it(struct nbd_device *nbd)
1da177e4
LT
399{
400 struct request *req;
84963048 401 int ret;
1da177e4 402
f4507164 403 BUG_ON(nbd->magic != NBD_MAGIC);
1da177e4 404
7f338fe4 405 sk_set_memalloc(nbd->sock->sk);
f4507164
WG
406 nbd->pid = task_pid_nr(current);
407 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
84963048 408 if (ret) {
f4507164
WG
409 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
410 nbd->pid = 0;
84963048
WC
411 return ret;
412 }
6b39bb65 413
7e2893a1
MP
414 nbd->task_recv = current;
415
f4507164 416 while ((req = nbd_read_stat(nbd)) != NULL)
d18509f5 417 nbd_end_request(nbd, req);
6b39bb65 418
7e2893a1
MP
419 nbd->task_recv = NULL;
420
421 if (signal_pending(current)) {
422 siginfo_t info;
423
424 ret = dequeue_signal_lock(current, &current->blocked, &info);
425 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
426 task_pid_nr(current), current->comm, ret);
36e47bee
MP
427 mutex_lock(&nbd->tx_lock);
428 sock_shutdown(nbd);
429 mutex_unlock(&nbd->tx_lock);
7e2893a1
MP
430 ret = -ETIMEDOUT;
431 }
432
f4507164
WG
433 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
434 nbd->pid = 0;
7e2893a1 435 return ret;
1da177e4
LT
436}
437
f4507164 438static void nbd_clear_que(struct nbd_device *nbd)
1da177e4
LT
439{
440 struct request *req;
441
f4507164 442 BUG_ON(nbd->magic != NBD_MAGIC);
1da177e4 443
4b2f0260 444 /*
f4507164 445 * Because we have set nbd->sock to NULL under the tx_lock, all
4b2f0260
HX
446 * modifications to the list must have completed by now. For
447 * the same reason, the active_req must be NULL.
448 *
449 * As a consequence, we don't need to take the spin lock while
450 * purging the list here.
451 */
f4507164
WG
452 BUG_ON(nbd->sock);
453 BUG_ON(nbd->active_req);
4b2f0260 454
f4507164
WG
455 while (!list_empty(&nbd->queue_head)) {
456 req = list_entry(nbd->queue_head.next, struct request,
4b2f0260
HX
457 queuelist);
458 list_del_init(&req->queuelist);
459 req->errors++;
d18509f5 460 nbd_end_request(nbd, req);
4b2f0260 461 }
fded4e09
PC
462
463 while (!list_empty(&nbd->waiting_queue)) {
464 req = list_entry(nbd->waiting_queue.next, struct request,
465 queuelist);
466 list_del_init(&req->queuelist);
467 req->errors++;
d18509f5 468 nbd_end_request(nbd, req);
fded4e09 469 }
1da177e4
LT
470}
471
7fdfd406 472
f4507164 473static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
48cf6061 474{
33659ebb 475 if (req->cmd_type != REQ_TYPE_FS)
48cf6061
LV
476 goto error_out;
477
9dc6c806
CH
478 if (rq_data_dir(req) == WRITE &&
479 (nbd->flags & NBD_FLAG_READ_ONLY)) {
480 dev_err(disk_to_dev(nbd->disk),
481 "Write on read-only\n");
482 goto error_out;
75f187ab
AB
483 }
484
48cf6061
LV
485 req->errors = 0;
486
f4507164
WG
487 mutex_lock(&nbd->tx_lock);
488 if (unlikely(!nbd->sock)) {
489 mutex_unlock(&nbd->tx_lock);
490 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 491 "Attempted send on closed socket\n");
15746fca 492 goto error_out;
48cf6061
LV
493 }
494
f4507164 495 nbd->active_req = req;
48cf6061 496
7e2893a1
MP
497 if (nbd->xmit_timeout && list_empty_careful(&nbd->queue_head))
498 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
499
f4507164
WG
500 if (nbd_send_req(nbd, req) != 0) {
501 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
48cf6061 502 req->errors++;
d18509f5 503 nbd_end_request(nbd, req);
48cf6061 504 } else {
f4507164 505 spin_lock(&nbd->queue_lock);
01ff5dbc 506 list_add_tail(&req->queuelist, &nbd->queue_head);
f4507164 507 spin_unlock(&nbd->queue_lock);
48cf6061
LV
508 }
509
f4507164
WG
510 nbd->active_req = NULL;
511 mutex_unlock(&nbd->tx_lock);
512 wake_up_all(&nbd->active_wq);
48cf6061
LV
513
514 return;
515
516error_out:
517 req->errors++;
d18509f5 518 nbd_end_request(nbd, req);
48cf6061
LV
519}
520
521static int nbd_thread(void *data)
522{
f4507164 523 struct nbd_device *nbd = data;
48cf6061
LV
524 struct request *req;
525
7e2893a1
MP
526 nbd->task_send = current;
527
8698a745 528 set_user_nice(current, MIN_NICE);
f4507164 529 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
48cf6061 530 /* wait for something to do */
f4507164 531 wait_event_interruptible(nbd->waiting_wq,
48cf6061 532 kthread_should_stop() ||
f4507164 533 !list_empty(&nbd->waiting_queue));
48cf6061 534
7e2893a1
MP
535 if (signal_pending(current)) {
536 siginfo_t info;
537 int ret;
538
539 ret = dequeue_signal_lock(current, &current->blocked,
540 &info);
541 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
542 task_pid_nr(current), current->comm, ret);
36e47bee
MP
543 mutex_lock(&nbd->tx_lock);
544 sock_shutdown(nbd);
545 mutex_unlock(&nbd->tx_lock);
7e2893a1
MP
546 break;
547 }
548
48cf6061 549 /* extract request */
f4507164 550 if (list_empty(&nbd->waiting_queue))
48cf6061
LV
551 continue;
552
f4507164
WG
553 spin_lock_irq(&nbd->queue_lock);
554 req = list_entry(nbd->waiting_queue.next, struct request,
48cf6061
LV
555 queuelist);
556 list_del_init(&req->queuelist);
f4507164 557 spin_unlock_irq(&nbd->queue_lock);
48cf6061
LV
558
559 /* handle request */
f4507164 560 nbd_handle_req(nbd, req);
48cf6061 561 }
7e2893a1
MP
562
563 nbd->task_send = NULL;
564
48cf6061
LV
565 return 0;
566}
567
1da177e4
LT
568/*
569 * We always wait for result of write, for now. It would be nice to make it optional
570 * in future
f4507164 571 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
1da177e4
LT
572 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
573 */
574
15746fca 575static void do_nbd_request(struct request_queue *q)
398eb085 576 __releases(q->queue_lock) __acquires(q->queue_lock)
1da177e4
LT
577{
578 struct request *req;
579
9934c8c0 580 while ((req = blk_fetch_request(q)) != NULL) {
f4507164 581 struct nbd_device *nbd;
1da177e4 582
48cf6061
LV
583 spin_unlock_irq(q->queue_lock);
584
f4507164 585 nbd = req->rq_disk->private_data;
1da177e4 586
f4507164 587 BUG_ON(nbd->magic != NBD_MAGIC);
1da177e4 588
d18509f5
MP
589 dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n",
590 req, req->cmd_type);
591
f4507164
WG
592 if (unlikely(!nbd->sock)) {
593 dev_err(disk_to_dev(nbd->disk),
7f1b90f9 594 "Attempted send on closed socket\n");
4d48a542 595 req->errors++;
d18509f5 596 nbd_end_request(nbd, req);
4d48a542
PC
597 spin_lock_irq(q->queue_lock);
598 continue;
599 }
600
f4507164
WG
601 spin_lock_irq(&nbd->queue_lock);
602 list_add_tail(&req->queuelist, &nbd->waiting_queue);
603 spin_unlock_irq(&nbd->queue_lock);
1da177e4 604
f4507164 605 wake_up(&nbd->waiting_wq);
4b2f0260 606
1da177e4 607 spin_lock_irq(q->queue_lock);
1da177e4 608 }
1da177e4
LT
609}
610
1a2ad211 611/* Must be called with tx_lock held */
1da177e4 612
f4507164 613static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
1a2ad211
PM
614 unsigned int cmd, unsigned long arg)
615{
1da177e4 616 switch (cmd) {
1a2ad211
PM
617 case NBD_DISCONNECT: {
618 struct request sreq;
619
f4507164 620 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
3a2d63f8
PB
621 if (!nbd->sock)
622 return -EINVAL;
1a2ad211 623
3a2d63f8
PB
624 mutex_unlock(&nbd->tx_lock);
625 fsync_bdev(bdev);
626 mutex_lock(&nbd->tx_lock);
4f54eec8 627 blk_rq_init(NULL, &sreq);
4f8c9510 628 sreq.cmd_type = REQ_TYPE_DRV_PRIV;
3a2d63f8
PB
629
630 /* Check again after getting mutex back. */
f4507164 631 if (!nbd->sock)
1da177e4 632 return -EINVAL;
3a2d63f8 633
c378f70a
PC
634 nbd->disconnect = 1;
635
f4507164 636 nbd_send_req(nbd, &sreq);
c378f70a 637 return 0;
1a2ad211 638 }
1da177e4 639
1a2ad211 640 case NBD_CLEAR_SOCK: {
e2511578 641 struct socket *sock = nbd->sock;
f4507164 642 nbd->sock = NULL;
f4507164
WG
643 nbd_clear_que(nbd);
644 BUG_ON(!list_empty(&nbd->queue_head));
fded4e09 645 BUG_ON(!list_empty(&nbd->waiting_queue));
3a2d63f8 646 kill_bdev(bdev);
e2511578
AV
647 if (sock)
648 sockfd_put(sock);
1a2ad211
PM
649 return 0;
650 }
651
652 case NBD_SET_SOCK: {
e2511578
AV
653 struct socket *sock;
654 int err;
655 if (nbd->sock)
1da177e4 656 return -EBUSY;
e2511578
AV
657 sock = sockfd_lookup(arg, &err);
658 if (sock) {
659 nbd->sock = sock;
660 if (max_part > 0)
661 bdev->bd_invalidated = 1;
662 nbd->disconnect = 0; /* we're connected now */
663 return 0;
1da177e4 664 }
1a2ad211
PM
665 return -EINVAL;
666 }
667
1da177e4 668 case NBD_SET_BLKSIZE:
f4507164
WG
669 nbd->blksize = arg;
670 nbd->bytesize &= ~(nbd->blksize-1);
671 bdev->bd_inode->i_size = nbd->bytesize;
672 set_blocksize(bdev, nbd->blksize);
673 set_capacity(nbd->disk, nbd->bytesize >> 9);
1da177e4 674 return 0;
1a2ad211 675
1da177e4 676 case NBD_SET_SIZE:
f4507164
WG
677 nbd->bytesize = arg & ~(nbd->blksize-1);
678 bdev->bd_inode->i_size = nbd->bytesize;
679 set_blocksize(bdev, nbd->blksize);
680 set_capacity(nbd->disk, nbd->bytesize >> 9);
1da177e4 681 return 0;
1a2ad211 682
7fdfd406 683 case NBD_SET_TIMEOUT:
f4507164 684 nbd->xmit_timeout = arg * HZ;
7e2893a1
MP
685 if (arg)
686 mod_timer(&nbd->timeout_timer,
687 jiffies + nbd->xmit_timeout);
688 else
689 del_timer_sync(&nbd->timeout_timer);
690
7fdfd406 691 return 0;
1a2ad211 692
2f012508
PC
693 case NBD_SET_FLAGS:
694 nbd->flags = arg;
695 return 0;
696
1da177e4 697 case NBD_SET_SIZE_BLOCKS:
f4507164
WG
698 nbd->bytesize = ((u64) arg) * nbd->blksize;
699 bdev->bd_inode->i_size = nbd->bytesize;
700 set_blocksize(bdev, nbd->blksize);
701 set_capacity(nbd->disk, nbd->bytesize >> 9);
1da177e4 702 return 0;
1a2ad211
PM
703
704 case NBD_DO_IT: {
705 struct task_struct *thread;
e2511578 706 struct socket *sock;
1a2ad211
PM
707 int error;
708
f4507164 709 if (nbd->pid)
c91192d6 710 return -EBUSY;
e2511578 711 if (!nbd->sock)
1da177e4 712 return -EINVAL;
1a2ad211 713
f4507164 714 mutex_unlock(&nbd->tx_lock);
1a2ad211 715
a83e814b
PB
716 if (nbd->flags & NBD_FLAG_READ_ONLY)
717 set_device_ro(bdev, true);
a336d298
PC
718 if (nbd->flags & NBD_FLAG_SEND_TRIM)
719 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
720 nbd->disk->queue);
75f187ab
AB
721 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
722 blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
723 else
724 blk_queue_flush(nbd->disk->queue, 0);
a336d298 725
d06df60b
MP
726 thread = kthread_run(nbd_thread, nbd, "%s",
727 nbd->disk->disk_name);
1a2ad211 728 if (IS_ERR(thread)) {
f4507164 729 mutex_lock(&nbd->tx_lock);
48cf6061 730 return PTR_ERR(thread);
1a2ad211 731 }
d06df60b 732
f4507164 733 error = nbd_do_it(nbd);
48cf6061 734 kthread_stop(thread);
1a2ad211 735
f4507164 736 mutex_lock(&nbd->tx_lock);
84963048
WC
737 if (error)
738 return error;
36e47bee 739 sock_shutdown(nbd);
e2511578
AV
740 sock = nbd->sock;
741 nbd->sock = NULL;
f4507164
WG
742 nbd_clear_que(nbd);
743 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
3a2d63f8 744 kill_bdev(bdev);
a336d298 745 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
a83e814b 746 set_device_ro(bdev, false);
e2511578
AV
747 if (sock)
748 sockfd_put(sock);
75f187ab 749 nbd->flags = 0;
f4507164 750 nbd->bytesize = 0;
a8cdc308 751 bdev->bd_inode->i_size = 0;
f4507164 752 set_capacity(nbd->disk, 0);
d71a6d73 753 if (max_part > 0)
9dcd1379 754 blkdev_reread_part(bdev);
c378f70a
PC
755 if (nbd->disconnect) /* user requested, ignore socket errors */
756 return 0;
f4507164 757 return nbd->harderror;
1a2ad211
PM
758 }
759
1da177e4 760 case NBD_CLEAR_QUE:
4b2f0260
HX
761 /*
762 * This is for compatibility only. The queue is always cleared
763 * by NBD_DO_IT or NBD_CLEAR_SOCK.
764 */
1da177e4 765 return 0;
1a2ad211 766
1da177e4 767 case NBD_PRINT_DEBUG:
f4507164 768 dev_info(disk_to_dev(nbd->disk),
5eedf541 769 "next = %p, prev = %p, head = %p\n",
f4507164
WG
770 nbd->queue_head.next, nbd->queue_head.prev,
771 &nbd->queue_head);
1da177e4
LT
772 return 0;
773 }
1a2ad211
PM
774 return -ENOTTY;
775}
776
777static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
778 unsigned int cmd, unsigned long arg)
779{
f4507164 780 struct nbd_device *nbd = bdev->bd_disk->private_data;
1a2ad211
PM
781 int error;
782
783 if (!capable(CAP_SYS_ADMIN))
784 return -EPERM;
785
f4507164 786 BUG_ON(nbd->magic != NBD_MAGIC);
1a2ad211 787
f4507164
WG
788 mutex_lock(&nbd->tx_lock);
789 error = __nbd_ioctl(bdev, nbd, cmd, arg);
790 mutex_unlock(&nbd->tx_lock);
1a2ad211
PM
791
792 return error;
1da177e4
LT
793}
794
83d5cde4 795static const struct block_device_operations nbd_fops =
1da177e4
LT
796{
797 .owner = THIS_MODULE,
8a6cfeb6 798 .ioctl = nbd_ioctl,
1da177e4
LT
799};
800
801/*
802 * And here should be modules and kernel interface
803 * (Just smiley confuses emacs :-)
804 */
805
806static int __init nbd_init(void)
807{
808 int err = -ENOMEM;
809 int i;
d71a6d73 810 int part_shift;
1da177e4 811
5b7b18cc 812 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
1da177e4 813
d71a6d73 814 if (max_part < 0) {
7742ce4a 815 printk(KERN_ERR "nbd: max_part must be >= 0\n");
d71a6d73
LV
816 return -EINVAL;
817 }
818
819 part_shift = 0;
5988ce23 820 if (max_part > 0) {
d71a6d73
LV
821 part_shift = fls(max_part);
822
5988ce23
NK
823 /*
824 * Adjust max_part according to part_shift as it is exported
825 * to user space so that user can know the max number of
826 * partition kernel should be able to manage.
827 *
828 * Note that -1 is required because partition 0 is reserved
829 * for the whole disk.
830 */
831 max_part = (1UL << part_shift) - 1;
832 }
833
3b271082
NK
834 if ((1UL << part_shift) > DISK_MAX_PARTS)
835 return -EINVAL;
836
837 if (nbds_max > 1UL << (MINORBITS - part_shift))
838 return -EINVAL;
839
ff6b8090
SM
840 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
841 if (!nbd_dev)
842 return -ENOMEM;
843
40be0c28 844 for (i = 0; i < nbds_max; i++) {
d71a6d73 845 struct gendisk *disk = alloc_disk(1 << part_shift);
1da177e4
LT
846 if (!disk)
847 goto out;
848 nbd_dev[i].disk = disk;
849 /*
850 * The new linux 2.5 block layer implementation requires
851 * every gendisk to have its very own request_queue struct.
852 * These structs are big so we dynamically allocate them.
853 */
854 disk->queue = blk_init_queue(do_nbd_request, &nbd_lock);
855 if (!disk->queue) {
856 put_disk(disk);
857 goto out;
858 }
31dcfab0
JA
859 /*
860 * Tell the block layer that we are not a rotational device
861 */
862 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
b277da0a 863 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
a336d298 864 disk->queue->limits.discard_granularity = 512;
2bb4cd5c 865 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
a336d298 866 disk->queue->limits.discard_zeroes_data = 0;
078be02b
MB
867 blk_queue_max_hw_sectors(disk->queue, 65536);
868 disk->queue->limits.max_sectors = 256;
1da177e4
LT
869 }
870
871 if (register_blkdev(NBD_MAJOR, "nbd")) {
872 err = -EIO;
873 goto out;
874 }
875
876 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
1da177e4 877
40be0c28 878 for (i = 0; i < nbds_max; i++) {
1da177e4 879 struct gendisk *disk = nbd_dev[i].disk;
f4507164 880 nbd_dev[i].magic = NBD_MAGIC;
48cf6061 881 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
1da177e4
LT
882 spin_lock_init(&nbd_dev[i].queue_lock);
883 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
82d4dc5a 884 mutex_init(&nbd_dev[i].tx_lock);
7e2893a1
MP
885 init_timer(&nbd_dev[i].timeout_timer);
886 nbd_dev[i].timeout_timer.function = nbd_xmit_timeout;
887 nbd_dev[i].timeout_timer.data = (unsigned long)&nbd_dev[i];
4b2f0260 888 init_waitqueue_head(&nbd_dev[i].active_wq);
48cf6061 889 init_waitqueue_head(&nbd_dev[i].waiting_wq);
1da177e4 890 nbd_dev[i].blksize = 1024;
4b86a872 891 nbd_dev[i].bytesize = 0;
1da177e4 892 disk->major = NBD_MAJOR;
d71a6d73 893 disk->first_minor = i << part_shift;
1da177e4
LT
894 disk->fops = &nbd_fops;
895 disk->private_data = &nbd_dev[i];
1da177e4 896 sprintf(disk->disk_name, "nbd%d", i);
4b86a872 897 set_capacity(disk, 0);
1da177e4
LT
898 add_disk(disk);
899 }
900
901 return 0;
902out:
903 while (i--) {
904 blk_cleanup_queue(nbd_dev[i].disk->queue);
905 put_disk(nbd_dev[i].disk);
906 }
f3944d61 907 kfree(nbd_dev);
1da177e4
LT
908 return err;
909}
910
911static void __exit nbd_cleanup(void)
912{
913 int i;
40be0c28 914 for (i = 0; i < nbds_max; i++) {
1da177e4 915 struct gendisk *disk = nbd_dev[i].disk;
40be0c28 916 nbd_dev[i].magic = 0;
1da177e4
LT
917 if (disk) {
918 del_gendisk(disk);
919 blk_cleanup_queue(disk->queue);
920 put_disk(disk);
921 }
922 }
1da177e4 923 unregister_blkdev(NBD_MAJOR, "nbd");
f3944d61 924 kfree(nbd_dev);
1da177e4
LT
925 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
926}
927
928module_init(nbd_init);
929module_exit(nbd_cleanup);
930
931MODULE_DESCRIPTION("Network Block Device");
932MODULE_LICENSE("GPL");
933
40be0c28 934module_param(nbds_max, int, 0444);
d71a6d73
LV
935MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
936module_param(max_part, int, 0444);
937MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");