nbd.c (806276b7f07a39a1cc3f38bb1ef5c573d4594a38) nbd.c (f363b089be0a39fe4282c688118a51d21f952bc7)
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 *
7 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
8 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>

--- 33 unchanged lines hidden (view full) ---

42#include <linux/nbd.h>
43
44static DEFINE_IDR(nbd_index_idr);
45static DEFINE_MUTEX(nbd_index_mutex);
46
47struct nbd_sock {
48 struct socket *sock;
49 struct mutex tx_lock;
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 *
7 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
8 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>

--- 33 unchanged lines hidden (view full) ---

42#include <linux/nbd.h>
43
44static DEFINE_IDR(nbd_index_idr);
45static DEFINE_MUTEX(nbd_index_mutex);
46
47struct nbd_sock {
48 struct socket *sock;
49 struct mutex tx_lock;
50 struct request *pending;
51 int sent;
52};
53
54#define NBD_TIMEDOUT 0
55#define NBD_DISCONNECT_REQUESTED 1
56#define NBD_DISCONNECTED 2
57#define NBD_RUNNING 3
58
59struct nbd_device {

--- 61 unchanged lines hidden (view full) ---

121 case NBD_CMD_FLUSH: return "flush";
122 case NBD_CMD_TRIM: return "trim/discard";
123 }
124 return "invalid";
125}
126
127static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
128{
50};
51
52#define NBD_TIMEDOUT 0
53#define NBD_DISCONNECT_REQUESTED 1
54#define NBD_DISCONNECTED 2
55#define NBD_RUNNING 3
56
57struct nbd_device {

--- 61 unchanged lines hidden (view full) ---

119 case NBD_CMD_FLUSH: return "flush";
120 case NBD_CMD_TRIM: return "trim/discard";
121 }
122 return "invalid";
123}
124
125static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
126{
129 if (bdev->bd_openers <= 1)
130 bd_set_size(bdev, 0);
127 bd_set_size(bdev, 0);
131 set_capacity(nbd->disk, 0);
132 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
133
134 return 0;
135}
136
137static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev)
138{

--- 49 unchanged lines hidden (view full) ---

188static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
189 bool reserved)
190{
191 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
192 struct nbd_device *nbd = cmd->nbd;
193
194 dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
195 set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
128 set_capacity(nbd->disk, 0);
129 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
130
131 return 0;
132}
133
134static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev)
135{

--- 49 unchanged lines hidden (view full) ---

185static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
186 bool reserved)
187{
188 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
189 struct nbd_device *nbd = cmd->nbd;
190
191 dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
192 set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
196 req->errors = -EIO;
193 req->errors++;
197
198 mutex_lock(&nbd->config_lock);
199 sock_shutdown(nbd);
200 mutex_unlock(&nbd->config_lock);
201 return BLK_EH_HANDLED;
202}
203
204/*
205 * Send or receive packet.
206 */
207static int sock_xmit(struct nbd_device *nbd, int index, int send,
194
195 mutex_lock(&nbd->config_lock);
196 sock_shutdown(nbd);
197 mutex_unlock(&nbd->config_lock);
198 return BLK_EH_HANDLED;
199}
200
201/*
202 * Send or receive packet.
203 */
204static int sock_xmit(struct nbd_device *nbd, int index, int send,
208 struct iov_iter *iter, int msg_flags, int *sent)
205 struct iov_iter *iter, int msg_flags)
209{
210 struct socket *sock = nbd->socks[index]->sock;
211 int result;
212 struct msghdr msg;
213 unsigned long pflags = current->flags;
214
215 if (unlikely(!sock)) {
216 dev_err_ratelimited(disk_to_dev(nbd->disk),

--- 18 unchanged lines hidden (view full) ---

235 else
236 result = sock_recvmsg(sock, &msg, msg.msg_flags);
237
238 if (result <= 0) {
239 if (result == 0)
240 result = -EPIPE; /* short read */
241 break;
242 }
206{
207 struct socket *sock = nbd->socks[index]->sock;
208 int result;
209 struct msghdr msg;
210 unsigned long pflags = current->flags;
211
212 if (unlikely(!sock)) {
213 dev_err_ratelimited(disk_to_dev(nbd->disk),

--- 18 unchanged lines hidden (view full) ---

232 else
233 result = sock_recvmsg(sock, &msg, msg.msg_flags);
234
235 if (result <= 0) {
236 if (result == 0)
237 result = -EPIPE; /* short read */
238 break;
239 }
243 if (sent)
244 *sent += result;
245 } while (msg_data_left(&msg));
246
247 tsk_restore_flags(current, pflags, PF_MEMALLOC);
248
249 return result;
250}
251
252/* always call with the tx_lock held */
253static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
254{
255 struct request *req = blk_mq_rq_from_pdu(cmd);
240 } while (msg_data_left(&msg));
241
242 tsk_restore_flags(current, pflags, PF_MEMALLOC);
243
244 return result;
245}
246
247/* always call with the tx_lock held */
248static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
249{
250 struct request *req = blk_mq_rq_from_pdu(cmd);
256 struct nbd_sock *nsock = nbd->socks[index];
257 int result;
258 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
259 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
260 struct iov_iter from;
261 unsigned long size = blk_rq_bytes(req);
262 struct bio *bio;
263 u32 type;
264 u32 tag = blk_mq_unique_tag(req);
251 int result;
252 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
253 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
254 struct iov_iter from;
255 unsigned long size = blk_rq_bytes(req);
256 struct bio *bio;
257 u32 type;
258 u32 tag = blk_mq_unique_tag(req);
265 int sent = nsock->sent, skip = 0;
266
267 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
268
269 switch (req_op(req)) {
270 case REQ_OP_DISCARD:
271 type = NBD_CMD_TRIM;
272 break;
273 case REQ_OP_FLUSH:

--- 11 unchanged lines hidden (view full) ---

285
286 if (rq_data_dir(req) == WRITE &&
287 (nbd->flags & NBD_FLAG_READ_ONLY)) {
288 dev_err_ratelimited(disk_to_dev(nbd->disk),
289 "Write on read-only\n");
290 return -EIO;
291 }
292
259
260 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
261
262 switch (req_op(req)) {
263 case REQ_OP_DISCARD:
264 type = NBD_CMD_TRIM;
265 break;
266 case REQ_OP_FLUSH:

--- 11 unchanged lines hidden (view full) ---

278
279 if (rq_data_dir(req) == WRITE &&
280 (nbd->flags & NBD_FLAG_READ_ONLY)) {
281 dev_err_ratelimited(disk_to_dev(nbd->disk),
282 "Write on read-only\n");
283 return -EIO;
284 }
285
293 /* We did a partial send previously, and we at least sent the whole
294 * request struct, so just go and send the rest of the pages in the
295 * request.
296 */
297 if (sent) {
298 if (sent >= sizeof(request)) {
299 skip = sent - sizeof(request);
300 goto send_pages;
301 }
302 iov_iter_advance(&from, sent);
303 }
304 request.type = htonl(type);
305 if (type != NBD_CMD_FLUSH) {
306 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
307 request.len = htonl(size);
308 }
309 memcpy(request.handle, &tag, sizeof(tag));
310
311 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
312 cmd, nbdcmd_to_ascii(type),
313 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
314 result = sock_xmit(nbd, index, 1, &from,
286 request.type = htonl(type);
287 if (type != NBD_CMD_FLUSH) {
288 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
289 request.len = htonl(size);
290 }
291 memcpy(request.handle, &tag, sizeof(tag));
292
293 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
294 cmd, nbdcmd_to_ascii(type),
295 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
296 result = sock_xmit(nbd, index, 1, &from,
315 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
297 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
316 if (result <= 0) {
298 if (result <= 0) {
317 if (result == -ERESTARTSYS) {
318 /* If we havne't sent anything we can just return BUSY,
319 * however if we have sent something we need to make
320 * sure we only allow this req to be sent until we are
321 * completely done.
322 */
323 if (sent) {
324 nsock->pending = req;
325 nsock->sent = sent;
326 }
327 return BLK_MQ_RQ_QUEUE_BUSY;
328 }
329 dev_err_ratelimited(disk_to_dev(nbd->disk),
330 "Send control failed (result %d)\n", result);
331 return -EIO;
332 }
299 dev_err_ratelimited(disk_to_dev(nbd->disk),
300 "Send control failed (result %d)\n", result);
301 return -EIO;
302 }
333send_pages:
303
334 if (type != NBD_CMD_WRITE)
304 if (type != NBD_CMD_WRITE)
335 goto out;
305 return 0;
336
337 bio = req->bio;
338 while (bio) {
339 struct bio *next = bio->bi_next;
340 struct bvec_iter iter;
341 struct bio_vec bvec;
342
343 bio_for_each_segment(bvec, bio, iter) {
344 bool is_last = !next && bio_iter_last(bvec, iter);
345 int flags = is_last ? 0 : MSG_MORE;
346
347 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
348 cmd, bvec.bv_len);
349 iov_iter_bvec(&from, ITER_BVEC | WRITE,
350 &bvec, 1, bvec.bv_len);
306
307 bio = req->bio;
308 while (bio) {
309 struct bio *next = bio->bi_next;
310 struct bvec_iter iter;
311 struct bio_vec bvec;
312
313 bio_for_each_segment(bvec, bio, iter) {
314 bool is_last = !next && bio_iter_last(bvec, iter);
315 int flags = is_last ? 0 : MSG_MORE;
316
317 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
318 cmd, bvec.bv_len);
319 iov_iter_bvec(&from, ITER_BVEC | WRITE,
320 &bvec, 1, bvec.bv_len);
351 if (skip) {
352 if (skip >= iov_iter_count(&from)) {
353 skip -= iov_iter_count(&from);
354 continue;
355 }
356 iov_iter_advance(&from, skip);
357 skip = 0;
358 }
359 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
321 result = sock_xmit(nbd, index, 1, &from, flags);
360 if (result <= 0) {
322 if (result <= 0) {
361 if (result == -ERESTARTSYS) {
362 /* We've already sent the header, we
363 * have no choice but to set pending and
364 * return BUSY.
365 */
366 nsock->pending = req;
367 nsock->sent = sent;
368 return BLK_MQ_RQ_QUEUE_BUSY;
369 }
370 dev_err(disk_to_dev(nbd->disk),
371 "Send data failed (result %d)\n",
372 result);
373 return -EIO;
374 }
375 /*
376 * The completion might already have come in,
377 * so break for the last one instead of letting
378 * the iterator do it. This prevents use-after-free
379 * of the bio.
380 */
381 if (is_last)
382 break;
383 }
384 bio = next;
385 }
323 dev_err(disk_to_dev(nbd->disk),
324 "Send data failed (result %d)\n",
325 result);
326 return -EIO;
327 }
328 /*
329 * The completion might already have come in,
330 * so break for the last one instead of letting
331 * the iterator do it. This prevents use-after-free
332 * of the bio.
333 */
334 if (is_last)
335 break;
336 }
337 bio = next;
338 }
386out:
387 nsock->pending = NULL;
388 nsock->sent = 0;
389 return 0;
390}
391
392/* NULL returned = something went wrong, inform userspace */
393static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
394{
395 int result;
396 struct nbd_reply reply;
397 struct nbd_cmd *cmd;
398 struct request *req = NULL;
399 u16 hwq;
400 u32 tag;
401 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
402 struct iov_iter to;
403
404 reply.magic = 0;
405 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
339 return 0;
340}
341
342/* NULL returned = something went wrong, inform userspace */
343static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
344{
345 int result;
346 struct nbd_reply reply;
347 struct nbd_cmd *cmd;
348 struct request *req = NULL;
349 u16 hwq;
350 u32 tag;
351 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
352 struct iov_iter to;
353
354 reply.magic = 0;
355 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
406 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
356 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL);
407 if (result <= 0) {
408 if (!test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) &&
409 !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
410 dev_err(disk_to_dev(nbd->disk),
411 "Receive control failed (result %d)\n", result);
412 return ERR_PTR(result);
413 }
414

--- 13 unchanged lines hidden (view full) ---

428 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
429 tag, req);
430 return ERR_PTR(-ENOENT);
431 }
432 cmd = blk_mq_rq_to_pdu(req);
433 if (ntohl(reply.error)) {
434 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
435 ntohl(reply.error));
357 if (result <= 0) {
358 if (!test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) &&
359 !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
360 dev_err(disk_to_dev(nbd->disk),
361 "Receive control failed (result %d)\n", result);
362 return ERR_PTR(result);
363 }
364

--- 13 unchanged lines hidden (view full) ---

378 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
379 tag, req);
380 return ERR_PTR(-ENOENT);
381 }
382 cmd = blk_mq_rq_to_pdu(req);
383 if (ntohl(reply.error)) {
384 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
385 ntohl(reply.error));
436 req->errors = -EIO;
386 req->errors++;
437 return cmd;
438 }
439
440 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
441 if (rq_data_dir(req) != WRITE) {
442 struct req_iterator iter;
443 struct bio_vec bvec;
444
445 rq_for_each_segment(bvec, req, iter) {
446 iov_iter_bvec(&to, ITER_BVEC | READ,
447 &bvec, 1, bvec.bv_len);
387 return cmd;
388 }
389
390 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
391 if (rq_data_dir(req) != WRITE) {
392 struct req_iterator iter;
393 struct bio_vec bvec;
394
395 rq_for_each_segment(bvec, req, iter) {
396 iov_iter_bvec(&to, ITER_BVEC | READ,
397 &bvec, 1, bvec.bv_len);
448 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
398 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL);
449 if (result <= 0) {
450 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
451 result);
399 if (result <= 0) {
400 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
401 result);
452 req->errors = -EIO;
402 req->errors++;
453 return cmd;
454 }
455 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
456 cmd, bvec.bv_len);
457 }
458 } else {
459 /* See the comment in nbd_queue_rq. */
460 wait_for_completion(&cmd->send_complete);

--- 53 unchanged lines hidden (view full) ---

514
515static void nbd_clear_req(struct request *req, void *data, bool reserved)
516{
517 struct nbd_cmd *cmd;
518
519 if (!blk_mq_request_started(req))
520 return;
521 cmd = blk_mq_rq_to_pdu(req);
403 return cmd;
404 }
405 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
406 cmd, bvec.bv_len);
407 }
408 } else {
409 /* See the comment in nbd_queue_rq. */
410 wait_for_completion(&cmd->send_complete);

--- 53 unchanged lines hidden (view full) ---

464
465static void nbd_clear_req(struct request *req, void *data, bool reserved)
466{
467 struct nbd_cmd *cmd;
468
469 if (!blk_mq_request_started(req))
470 return;
471 cmd = blk_mq_rq_to_pdu(req);
522 req->errors = -EIO;
472 req->errors++;
523 nbd_end_request(cmd);
524}
525
526static void nbd_clear_que(struct nbd_device *nbd)
527{
528 BUG_ON(nbd->magic != NBD_MAGIC);
529
530 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
531 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
532}
533
534
473 nbd_end_request(cmd);
474}
475
476static void nbd_clear_que(struct nbd_device *nbd)
477{
478 BUG_ON(nbd->magic != NBD_MAGIC);
479
480 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
481 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
482}
483
484
535static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
485static void nbd_handle_cmd(struct nbd_cmd *cmd, int index)
536{
537 struct request *req = blk_mq_rq_from_pdu(cmd);
538 struct nbd_device *nbd = cmd->nbd;
539 struct nbd_sock *nsock;
486{
487 struct request *req = blk_mq_rq_from_pdu(cmd);
488 struct nbd_device *nbd = cmd->nbd;
489 struct nbd_sock *nsock;
540 int ret;
541
542 if (index >= nbd->num_connections) {
543 dev_err_ratelimited(disk_to_dev(nbd->disk),
544 "Attempted send on invalid socket\n");
490
491 if (index >= nbd->num_connections) {
492 dev_err_ratelimited(disk_to_dev(nbd->disk),
493 "Attempted send on invalid socket\n");
545 return -EINVAL;
494 goto error_out;
546 }
547
548 if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) {
549 dev_err_ratelimited(disk_to_dev(nbd->disk),
550 "Attempted send on closed socket\n");
495 }
496
497 if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) {
498 dev_err_ratelimited(disk_to_dev(nbd->disk),
499 "Attempted send on closed socket\n");
551 return -EINVAL;
500 goto error_out;
552 }
553
554 req->errors = 0;
555
556 nsock = nbd->socks[index];
557 mutex_lock(&nsock->tx_lock);
558 if (unlikely(!nsock->sock)) {
559 mutex_unlock(&nsock->tx_lock);
560 dev_err_ratelimited(disk_to_dev(nbd->disk),
561 "Attempted send on closed socket\n");
501 }
502
503 req->errors = 0;
504
505 nsock = nbd->socks[index];
506 mutex_lock(&nsock->tx_lock);
507 if (unlikely(!nsock->sock)) {
508 mutex_unlock(&nsock->tx_lock);
509 dev_err_ratelimited(disk_to_dev(nbd->disk),
510 "Attempted send on closed socket\n");
562 return -EINVAL;
511 goto error_out;
563 }
564
512 }
513
565 /* Handle the case that we have a pending request that was partially
566 * transmitted that _has_ to be serviced first. We need to call requeue
567 * here so that it gets put _after_ the request that is already on the
568 * dispatch list.
569 */
570 if (unlikely(nsock->pending && nsock->pending != req)) {
571 blk_mq_requeue_request(req, true);
572 ret = 0;
573 goto out;
514 if (nbd_send_cmd(nbd, cmd, index) != 0) {
515 dev_err_ratelimited(disk_to_dev(nbd->disk),
516 "Request send failed\n");
517 req->errors++;
518 nbd_end_request(cmd);
574 }
519 }
575 ret = nbd_send_cmd(nbd, cmd, index);
576out:
520
577 mutex_unlock(&nsock->tx_lock);
521 mutex_unlock(&nsock->tx_lock);
578 return ret;
522
523 return;
524
525error_out:
526 req->errors++;
527 nbd_end_request(cmd);
579}
580
581static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
582 const struct blk_mq_queue_data *bd)
583{
584 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
528}
529
530static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
531 const struct blk_mq_queue_data *bd)
532{
533 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
585 int ret;
586
587 /*
588 * Since we look at the bio's to send the request over the network we
589 * need to make sure the completion work doesn't mark this request done
590 * before we are done doing our send. This keeps us from dereferencing
591 * freed data if we have particularly fast completions (ie we get the
592 * completion before we exit sock_xmit on the last bvec) or in the case
593 * that the server is misbehaving (or there was an error) before we're
594 * done sending everything over the wire.
595 */
596 init_completion(&cmd->send_complete);
597 blk_mq_start_request(bd->rq);
534
535 /*
536 * Since we look at the bio's to send the request over the network we
537 * need to make sure the completion work doesn't mark this request done
538 * before we are done doing our send. This keeps us from dereferencing
539 * freed data if we have particularly fast completions (ie we get the
540 * completion before we exit sock_xmit on the last bvec) or in the case
541 * that the server is misbehaving (or there was an error) before we're
542 * done sending everything over the wire.
543 */
544 init_completion(&cmd->send_complete);
545 blk_mq_start_request(bd->rq);
598
599 /* We can be called directly from the user space process, which means we
600 * could possibly have signals pending so our sendmsg will fail. In
601 * this case we need to return that we are busy, otherwise error out as
602 * appropriate.
603 */
604 ret = nbd_handle_cmd(cmd, hctx->queue_num);
605 if (ret < 0)
606 ret = BLK_MQ_RQ_QUEUE_ERROR;
607 if (!ret)
608 ret = BLK_MQ_RQ_QUEUE_OK;
546 nbd_handle_cmd(cmd, hctx->queue_num);
609 complete(&cmd->send_complete);
610
547 complete(&cmd->send_complete);
548
611 return ret;
549 return BLK_MQ_RQ_QUEUE_OK;
612}
613
614static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
615 unsigned long arg)
616{
617 struct socket *sock;
618 struct nbd_sock **socks;
619 struct nbd_sock *nsock;

--- 18 unchanged lines hidden (view full) ---

638 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
639 if (!nsock)
640 return -ENOMEM;
641
642 nbd->socks = socks;
643
644 mutex_init(&nsock->tx_lock);
645 nsock->sock = sock;
550}
551
552static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
553 unsigned long arg)
554{
555 struct socket *sock;
556 struct nbd_sock **socks;
557 struct nbd_sock *nsock;

--- 18 unchanged lines hidden (view full) ---

576 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
577 if (!nsock)
578 return -ENOMEM;
579
580 nbd->socks = socks;
581
582 mutex_init(&nsock->tx_lock);
583 nsock->sock = sock;
646 nsock->pending = NULL;
647 nsock->sent = 0;
648 socks[nbd->num_connections++] = nsock;
649
650 if (max_part)
651 bdev->bd_invalidated = 1;
652 return 0;
653}
654
655/* Reset all properties of an NBD device */

--- 5 unchanged lines hidden (view full) ---

661 set_capacity(nbd->disk, 0);
662 nbd->flags = 0;
663 nbd->tag_set.timeout = 0;
664 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
665}
666
667static void nbd_bdev_reset(struct block_device *bdev)
668{
584 socks[nbd->num_connections++] = nsock;
585
586 if (max_part)
587 bdev->bd_invalidated = 1;
588 return 0;
589}
590
591/* Reset all properties of an NBD device */

--- 5 unchanged lines hidden (view full) ---

597 set_capacity(nbd->disk, 0);
598 nbd->flags = 0;
599 nbd->tag_set.timeout = 0;
600 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
601}
602
603static void nbd_bdev_reset(struct block_device *bdev)
604{
669 if (bdev->bd_openers > 1)
670 return;
671 set_device_ro(bdev, false);
672 bdev->bd_inode->i_size = 0;
673 if (max_part > 0) {
674 blkdev_reread_part(bdev);
675 bdev->bd_invalidated = 1;
676 }
677}
678

--- 16 unchanged lines hidden (view full) ---

695 .type = htonl(NBD_CMD_DISC),
696 };
697 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
698 struct iov_iter from;
699 int i, ret;
700
701 for (i = 0; i < nbd->num_connections; i++) {
702 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
605 set_device_ro(bdev, false);
606 bdev->bd_inode->i_size = 0;
607 if (max_part > 0) {
608 blkdev_reread_part(bdev);
609 bdev->bd_invalidated = 1;
610 }
611}
612

--- 16 unchanged lines hidden (view full) ---

629 .type = htonl(NBD_CMD_DISC),
630 };
631 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
632 struct iov_iter from;
633 int i, ret;
634
635 for (i = 0; i < nbd->num_connections; i++) {
636 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
703 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
637 ret = sock_xmit(nbd, i, 1, &from, 0);
704 if (ret <= 0)
705 dev_err(disk_to_dev(nbd->disk),
706 "Send disconnect failed %d\n", ret);
707 }
708}
709
710static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev)
711{

--- 14 unchanged lines hidden (view full) ---

726 send_disconnects(nbd);
727 return 0;
728}
729
730static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev)
731{
732 sock_shutdown(nbd);
733 nbd_clear_que(nbd);
638 if (ret <= 0)
639 dev_err(disk_to_dev(nbd->disk),
640 "Send disconnect failed %d\n", ret);
641 }
642}
643
644static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev)
645{

--- 14 unchanged lines hidden (view full) ---

660 send_disconnects(nbd);
661 return 0;
662}
663
664static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev)
665{
666 sock_shutdown(nbd);
667 nbd_clear_que(nbd);
734
735 __invalidate_device(bdev, true);
668 kill_bdev(bdev);
736 nbd_bdev_reset(bdev);
737 /*
738 * We want to give the run thread a chance to wait for everybody
739 * to clean up and then do it's own cleanup.
740 */
741 if (!test_bit(NBD_RUNNING, &nbd->runtime_flags) &&
742 nbd->num_connections) {
743 int i;

--- 99 unchanged lines hidden (view full) ---

843 case NBD_SET_SIZE:
844 nbd_size_set(nbd, bdev, nbd->blksize,
845 div_s64(arg, nbd->blksize));
846 return 0;
847 case NBD_SET_SIZE_BLOCKS:
848 nbd_size_set(nbd, bdev, nbd->blksize, arg);
849 return 0;
850 case NBD_SET_TIMEOUT:
669 nbd_bdev_reset(bdev);
670 /*
671 * We want to give the run thread a chance to wait for everybody
672 * to clean up and then do it's own cleanup.
673 */
674 if (!test_bit(NBD_RUNNING, &nbd->runtime_flags) &&
675 nbd->num_connections) {
676 int i;

--- 99 unchanged lines hidden (view full) ---

776 case NBD_SET_SIZE:
777 nbd_size_set(nbd, bdev, nbd->blksize,
778 div_s64(arg, nbd->blksize));
779 return 0;
780 case NBD_SET_SIZE_BLOCKS:
781 nbd_size_set(nbd, bdev, nbd->blksize, arg);
782 return 0;
783 case NBD_SET_TIMEOUT:
851 if (arg) {
852 nbd->tag_set.timeout = arg * HZ;
853 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
854 }
784 nbd->tag_set.timeout = arg * HZ;
855 return 0;
856
857 case NBD_SET_FLAGS:
858 nbd->flags = arg;
859 return 0;
860 case NBD_DO_IT:
861 return nbd_start_device(nbd, bdev);
862 case NBD_CLEAR_QUE:

--- 167 unchanged lines hidden (view full) ---

1030 unsigned int hctx_idx, unsigned int request_idx,
1031 unsigned int numa_node)
1032{
1033 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
1034 cmd->nbd = data;
1035 return 0;
1036}
1037
785 return 0;
786
787 case NBD_SET_FLAGS:
788 nbd->flags = arg;
789 return 0;
790 case NBD_DO_IT:
791 return nbd_start_device(nbd, bdev);
792 case NBD_CLEAR_QUE:

--- 167 unchanged lines hidden (view full) ---

960 unsigned int hctx_idx, unsigned int request_idx,
961 unsigned int numa_node)
962{
963 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
964 cmd->nbd = data;
965 return 0;
966}
967
1038static struct blk_mq_ops nbd_mq_ops = {
968static const struct blk_mq_ops nbd_mq_ops = {
1039 .queue_rq = nbd_queue_rq,
1040 .init_request = nbd_init_request,
1041 .timeout = nbd_xmit_timeout,
1042};
1043
1044static void nbd_dev_remove(struct nbd_device *nbd)
1045{
1046 struct gendisk *disk = nbd->disk;

--- 176 unchanged lines hidden ---
969 .queue_rq = nbd_queue_rq,
970 .init_request = nbd_init_request,
971 .timeout = nbd_xmit_timeout,
972};
973
974static void nbd_dev_remove(struct nbd_device *nbd)
975{
976 struct gendisk *disk = nbd->disk;

--- 176 unchanged lines hidden ---