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