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