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