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