@@ -47,6 +47,8 @@ static DEFINE_MUTEX(nbd_index_mutex);
47
47
struct nbd_sock {
48
48
struct socket * sock ;
49
49
struct mutex tx_lock ;
50
+ struct request * pending ;
51
+ int sent ;
50
52
};
51
53
52
54
#define NBD_TIMEDOUT 0
@@ -124,7 +126,8 @@ static const char *nbdcmd_to_ascii(int cmd)
124
126
125
127
static int nbd_size_clear (struct nbd_device * nbd , struct block_device * bdev )
126
128
{
127
- bd_set_size (bdev , 0 );
129
+ if (bdev -> bd_openers <= 1 )
130
+ bd_set_size (bdev , 0 );
128
131
set_capacity (nbd -> disk , 0 );
129
132
kobject_uevent (& nbd_to_dev (nbd )-> kobj , KOBJ_CHANGE );
130
133
@@ -190,7 +193,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
190
193
191
194
dev_err (nbd_to_dev (nbd ), "Connection timed out, shutting down connection\n" );
192
195
set_bit (NBD_TIMEDOUT , & nbd -> runtime_flags );
193
- req -> errors ++ ;
196
+ req -> errors = - EIO ;
194
197
195
198
mutex_lock (& nbd -> config_lock );
196
199
sock_shutdown (nbd );
@@ -202,7 +205,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
202
205
* Send or receive packet.
203
206
*/
204
207
static int sock_xmit (struct nbd_device * nbd , int index , int send ,
205
- struct iov_iter * iter , int msg_flags )
208
+ struct iov_iter * iter , int msg_flags , int * sent )
206
209
{
207
210
struct socket * sock = nbd -> socks [index ]-> sock ;
208
211
int result ;
@@ -237,6 +240,8 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send,
237
240
result = - EPIPE ; /* short read */
238
241
break ;
239
242
}
243
+ if (sent )
244
+ * sent += result ;
240
245
} while (msg_data_left (& msg ));
241
246
242
247
tsk_restore_flags (current , pflags , PF_MEMALLOC );
@@ -248,6 +253,7 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send,
248
253
static int nbd_send_cmd (struct nbd_device * nbd , struct nbd_cmd * cmd , int index )
249
254
{
250
255
struct request * req = blk_mq_rq_from_pdu (cmd );
256
+ struct nbd_sock * nsock = nbd -> socks [index ];
251
257
int result ;
252
258
struct nbd_request request = {.magic = htonl (NBD_REQUEST_MAGIC )};
253
259
struct kvec iov = {.iov_base = & request , .iov_len = sizeof (request )};
@@ -256,6 +262,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
256
262
struct bio * bio ;
257
263
u32 type ;
258
264
u32 tag = blk_mq_unique_tag (req );
265
+ int sent = nsock -> sent , skip = 0 ;
259
266
260
267
iov_iter_kvec (& from , WRITE | ITER_KVEC , & iov , 1 , sizeof (request ));
261
268
@@ -283,6 +290,17 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
283
290
return - EIO ;
284
291
}
285
292
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
+ }
286
304
request .type = htonl (type );
287
305
if (type != NBD_CMD_FLUSH ) {
288
306
request .from = cpu_to_be64 ((u64 )blk_rq_pos (req ) << 9 );
@@ -294,15 +312,27 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
294
312
cmd , nbdcmd_to_ascii (type ),
295
313
(unsigned long long )blk_rq_pos (req ) << 9 , blk_rq_bytes (req ));
296
314
result = sock_xmit (nbd , index , 1 , & from ,
297
- (type == NBD_CMD_WRITE ) ? MSG_MORE : 0 );
315
+ (type == NBD_CMD_WRITE ) ? MSG_MORE : 0 , & sent );
298
316
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
+ }
299
329
dev_err_ratelimited (disk_to_dev (nbd -> disk ),
300
330
"Send control failed (result %d)\n" , result );
301
331
return - EIO ;
302
332
}
303
-
333
+ send_pages :
304
334
if (type != NBD_CMD_WRITE )
305
- return 0 ;
335
+ goto out ;
306
336
307
337
bio = req -> bio ;
308
338
while (bio ) {
@@ -318,8 +348,25 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
318
348
cmd , bvec .bv_len );
319
349
iov_iter_bvec (& from , ITER_BVEC | WRITE ,
320
350
& bvec , 1 , bvec .bv_len );
321
- result = sock_xmit (nbd , index , 1 , & from , flags );
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 );
322
360
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
+ }
323
370
dev_err (disk_to_dev (nbd -> disk ),
324
371
"Send data failed (result %d)\n" ,
325
372
result );
@@ -336,6 +383,9 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
336
383
}
337
384
bio = next ;
338
385
}
386
+ out :
387
+ nsock -> pending = NULL ;
388
+ nsock -> sent = 0 ;
339
389
return 0 ;
340
390
}
341
391
@@ -353,7 +403,7 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
353
403
354
404
reply .magic = 0 ;
355
405
iov_iter_kvec (& to , READ | ITER_KVEC , & iov , 1 , sizeof (reply ));
356
- result = sock_xmit (nbd , index , 0 , & to , MSG_WAITALL );
406
+ result = sock_xmit (nbd , index , 0 , & to , MSG_WAITALL , NULL );
357
407
if (result <= 0 ) {
358
408
if (!test_bit (NBD_DISCONNECTED , & nbd -> runtime_flags ) &&
359
409
!test_bit (NBD_DISCONNECT_REQUESTED , & nbd -> runtime_flags ))
@@ -383,7 +433,7 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
383
433
if (ntohl (reply .error )) {
384
434
dev_err (disk_to_dev (nbd -> disk ), "Other side returned error (%d)\n" ,
385
435
ntohl (reply .error ));
386
- req -> errors ++ ;
436
+ req -> errors = - EIO ;
387
437
return cmd ;
388
438
}
389
439
@@ -395,11 +445,11 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
395
445
rq_for_each_segment (bvec , req , iter ) {
396
446
iov_iter_bvec (& to , ITER_BVEC | READ ,
397
447
& bvec , 1 , bvec .bv_len );
398
- result = sock_xmit (nbd , index , 0 , & to , MSG_WAITALL );
448
+ result = sock_xmit (nbd , index , 0 , & to , MSG_WAITALL , NULL );
399
449
if (result <= 0 ) {
400
450
dev_err (disk_to_dev (nbd -> disk ), "Receive data failed (result %d)\n" ,
401
451
result );
402
- req -> errors ++ ;
452
+ req -> errors = - EIO ;
403
453
return cmd ;
404
454
}
405
455
dev_dbg (nbd_to_dev (nbd ), "request %p: got %d bytes data\n" ,
@@ -469,7 +519,7 @@ static void nbd_clear_req(struct request *req, void *data, bool reserved)
469
519
if (!blk_mq_request_started (req ))
470
520
return ;
471
521
cmd = blk_mq_rq_to_pdu (req );
472
- req -> errors ++ ;
522
+ req -> errors = - EIO ;
473
523
nbd_end_request (cmd );
474
524
}
475
525
@@ -482,22 +532,23 @@ static void nbd_clear_que(struct nbd_device *nbd)
482
532
}
483
533
484
534
485
- static void nbd_handle_cmd (struct nbd_cmd * cmd , int index )
535
+ static int nbd_handle_cmd (struct nbd_cmd * cmd , int index )
486
536
{
487
537
struct request * req = blk_mq_rq_from_pdu (cmd );
488
538
struct nbd_device * nbd = cmd -> nbd ;
489
539
struct nbd_sock * nsock ;
540
+ int ret ;
490
541
491
542
if (index >= nbd -> num_connections ) {
492
543
dev_err_ratelimited (disk_to_dev (nbd -> disk ),
493
544
"Attempted send on invalid socket\n" );
494
- goto error_out ;
545
+ return - EINVAL ;
495
546
}
496
547
497
548
if (test_bit (NBD_DISCONNECTED , & nbd -> runtime_flags )) {
498
549
dev_err_ratelimited (disk_to_dev (nbd -> disk ),
499
550
"Attempted send on closed socket\n" );
500
- goto error_out ;
551
+ return - EINVAL ;
501
552
}
502
553
503
554
req -> errors = 0 ;
@@ -508,29 +559,30 @@ static void nbd_handle_cmd(struct nbd_cmd *cmd, int index)
508
559
mutex_unlock (& nsock -> tx_lock );
509
560
dev_err_ratelimited (disk_to_dev (nbd -> disk ),
510
561
"Attempted send on closed socket\n" );
511
- goto error_out ;
562
+ return - EINVAL ;
512
563
}
513
564
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 );
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 ;
519
574
}
520
-
575
+ ret = nbd_send_cmd (nbd , cmd , index );
576
+ out :
521
577
mutex_unlock (& nsock -> tx_lock );
522
-
523
- return ;
524
-
525
- error_out :
526
- req -> errors ++ ;
527
- nbd_end_request (cmd );
578
+ return ret ;
528
579
}
529
580
530
581
static int nbd_queue_rq (struct blk_mq_hw_ctx * hctx ,
531
582
const struct blk_mq_queue_data * bd )
532
583
{
533
584
struct nbd_cmd * cmd = blk_mq_rq_to_pdu (bd -> rq );
585
+ int ret ;
534
586
535
587
/*
536
588
* Since we look at the bio's to send the request over the network we
@@ -543,10 +595,20 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
543
595
*/
544
596
init_completion (& cmd -> send_complete );
545
597
blk_mq_start_request (bd -> rq );
546
- nbd_handle_cmd (cmd , hctx -> queue_num );
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 ;
547
609
complete (& cmd -> send_complete );
548
610
549
- return BLK_MQ_RQ_QUEUE_OK ;
611
+ return ret ;
550
612
}
551
613
552
614
static int nbd_add_socket (struct nbd_device * nbd , struct block_device * bdev ,
@@ -581,6 +643,8 @@ static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
581
643
582
644
mutex_init (& nsock -> tx_lock );
583
645
nsock -> sock = sock ;
646
+ nsock -> pending = NULL ;
647
+ nsock -> sent = 0 ;
584
648
socks [nbd -> num_connections ++ ] = nsock ;
585
649
586
650
if (max_part )
@@ -602,6 +666,8 @@ static void nbd_reset(struct nbd_device *nbd)
602
666
603
667
static void nbd_bdev_reset (struct block_device * bdev )
604
668
{
669
+ if (bdev -> bd_openers > 1 )
670
+ return ;
605
671
set_device_ro (bdev , false);
606
672
bdev -> bd_inode -> i_size = 0 ;
607
673
if (max_part > 0 ) {
@@ -634,7 +700,7 @@ static void send_disconnects(struct nbd_device *nbd)
634
700
635
701
for (i = 0 ; i < nbd -> num_connections ; i ++ ) {
636
702
iov_iter_kvec (& from , WRITE | ITER_KVEC , & iov , 1 , sizeof (request ));
637
- ret = sock_xmit (nbd , i , 1 , & from , 0 );
703
+ ret = sock_xmit (nbd , i , 1 , & from , 0 , NULL );
638
704
if (ret <= 0 )
639
705
dev_err (disk_to_dev (nbd -> disk ),
640
706
"Send disconnect failed %d\n" , ret );
@@ -665,7 +731,8 @@ static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev)
665
731
{
666
732
sock_shutdown (nbd );
667
733
nbd_clear_que (nbd );
668
- kill_bdev (bdev );
734
+
735
+ __invalidate_device (bdev , true);
669
736
nbd_bdev_reset (bdev );
670
737
/*
671
738
* We want to give the run thread a chance to wait for everybody
@@ -781,7 +848,10 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
781
848
nbd_size_set (nbd , bdev , nbd -> blksize , arg );
782
849
return 0 ;
783
850
case NBD_SET_TIMEOUT :
784
- nbd -> tag_set .timeout = arg * HZ ;
851
+ if (arg ) {
852
+ nbd -> tag_set .timeout = arg * HZ ;
853
+ blk_queue_rq_timeout (nbd -> disk -> queue , arg * HZ );
854
+ }
785
855
return 0 ;
786
856
787
857
case NBD_SET_FLAGS :
0 commit comments