-
Notifications
You must be signed in to change notification settings - Fork 102
/
rnbd-clt.c
1736 lines (1474 loc) · 42.7 KB
/
rnbd-clt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* RDMA Network Block Driver
*
* Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
* Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
* Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
*/
#undef pr_fmt
#define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/hdreg.h>
#include <linux/scatterlist.h>
#include <linux/idr.h>
#include "rnbd-clt.h"
MODULE_DESCRIPTION("RDMA Network Block Device Client");
MODULE_LICENSE("GPL");
static int rnbd_client_major;
static DEFINE_IDA(index_ida);
static DEFINE_MUTEX(ida_lock);
static DEFINE_MUTEX(sess_lock);
static LIST_HEAD(sess_list);
/*
* Maximum number of partitions an instance can have.
* 6 bits = 64 minors = 63 partitions (one minor is used for the device itself)
*/
#define RNBD_PART_BITS 6
static inline bool rnbd_clt_get_sess(struct rnbd_clt_session *sess)
{
return refcount_inc_not_zero(&sess->refcount);
}
static void free_sess(struct rnbd_clt_session *sess);
static void rnbd_clt_put_sess(struct rnbd_clt_session *sess)
{
might_sleep();
if (refcount_dec_and_test(&sess->refcount))
free_sess(sess);
}
static void rnbd_clt_put_dev(struct rnbd_clt_dev *dev)
{
might_sleep();
if (!refcount_dec_and_test(&dev->refcount))
return;
mutex_lock(&ida_lock);
ida_simple_remove(&index_ida, dev->clt_device_id);
mutex_unlock(&ida_lock);
kfree(dev->hw_queues);
kfree(dev->pathname);
rnbd_clt_put_sess(dev->sess);
mutex_destroy(&dev->lock);
kfree(dev);
}
static inline bool rnbd_clt_get_dev(struct rnbd_clt_dev *dev)
{
return refcount_inc_not_zero(&dev->refcount);
}
static int rnbd_clt_set_dev_attr(struct rnbd_clt_dev *dev,
const struct rnbd_msg_open_rsp *rsp)
{
struct rnbd_clt_session *sess = dev->sess;
if (!rsp->logical_block_size)
return -EINVAL;
dev->device_id = le32_to_cpu(rsp->device_id);
dev->nsectors = le64_to_cpu(rsp->nsectors);
dev->logical_block_size = le16_to_cpu(rsp->logical_block_size);
dev->physical_block_size = le16_to_cpu(rsp->physical_block_size);
dev->max_write_same_sectors = le32_to_cpu(rsp->max_write_same_sectors);
dev->max_discard_sectors = le32_to_cpu(rsp->max_discard_sectors);
dev->discard_granularity = le32_to_cpu(rsp->discard_granularity);
dev->discard_alignment = le32_to_cpu(rsp->discard_alignment);
dev->secure_discard = le16_to_cpu(rsp->secure_discard);
dev->rotational = rsp->rotational;
dev->max_hw_sectors = sess->max_io_size / SECTOR_SIZE;
dev->max_segments = BMAX_SEGMENTS;
return 0;
}
static int rnbd_clt_change_capacity(struct rnbd_clt_dev *dev,
size_t new_nsectors)
{
rnbd_clt_info(dev, "Device size changed from %zu to %zu sectors\n",
dev->nsectors, new_nsectors);
dev->nsectors = new_nsectors;
set_capacity(dev->gd, dev->nsectors);
revalidate_disk_size(dev->gd, true);
return 0;
}
static int process_msg_open_rsp(struct rnbd_clt_dev *dev,
struct rnbd_msg_open_rsp *rsp)
{
int err = 0;
mutex_lock(&dev->lock);
if (dev->dev_state == DEV_STATE_UNMAPPED) {
rnbd_clt_info(dev,
"Ignoring Open-Response message from server for unmapped device\n");
err = -ENOENT;
goto out;
}
if (dev->dev_state == DEV_STATE_MAPPED_DISCONNECTED) {
u64 nsectors = le64_to_cpu(rsp->nsectors);
/*
* If the device was remapped and the size changed in the
* meantime we need to revalidate it
*/
if (dev->nsectors != nsectors)
rnbd_clt_change_capacity(dev, nsectors);
rnbd_clt_info(dev, "Device online, device remapped successfully\n");
}
err = rnbd_clt_set_dev_attr(dev, rsp);
if (err)
goto out;
dev->dev_state = DEV_STATE_MAPPED;
out:
mutex_unlock(&dev->lock);
return err;
}
int rnbd_clt_resize_disk(struct rnbd_clt_dev *dev, size_t newsize)
{
int ret = 0;
mutex_lock(&dev->lock);
if (dev->dev_state != DEV_STATE_MAPPED) {
pr_err("Failed to set new size of the device, device is not opened\n");
ret = -ENOENT;
goto out;
}
ret = rnbd_clt_change_capacity(dev, newsize);
out:
mutex_unlock(&dev->lock);
return ret;
}
static inline void rnbd_clt_dev_requeue(struct rnbd_queue *q)
{
if (WARN_ON(!q->hctx))
return;
/* We can come here from interrupt, thus async=true */
blk_mq_run_hw_queue(q->hctx, true);
}
enum {
RNBD_DELAY_IFBUSY = -1,
};
/**
* rnbd_get_cpu_qlist() - finds a list with HW queues to be rerun
* @sess: Session to find a queue for
* @cpu: Cpu to start the search from
*
* Description:
* Each CPU has a list of HW queues, which needs to be rerun. If a list
* is not empty - it is marked with a bit. This function finds first
* set bit in a bitmap and returns corresponding CPU list.
*/
static struct rnbd_cpu_qlist *
rnbd_get_cpu_qlist(struct rnbd_clt_session *sess, int cpu)
{
int bit;
/* Search from cpu to nr_cpu_ids */
bit = find_next_bit(sess->cpu_queues_bm, nr_cpu_ids, cpu);
if (bit < nr_cpu_ids) {
return per_cpu_ptr(sess->cpu_queues, bit);
} else if (cpu != 0) {
/* Search from 0 to cpu */
bit = find_next_bit(sess->cpu_queues_bm, cpu, 0);
if (bit < cpu)
return per_cpu_ptr(sess->cpu_queues, bit);
}
return NULL;
}
static inline int nxt_cpu(int cpu)
{
return (cpu + 1) % nr_cpu_ids;
}
/**
* rnbd_rerun_if_needed() - rerun next queue marked as stopped
* @sess: Session to rerun a queue on
*
* Description:
* Each CPU has it's own list of HW queues, which should be rerun.
* Function finds such list with HW queues, takes a list lock, picks up
* the first HW queue out of the list and requeues it.
*
* Return:
* True if the queue was requeued, false otherwise.
*
* Context:
* Does not matter.
*/
static bool rnbd_rerun_if_needed(struct rnbd_clt_session *sess)
{
struct rnbd_queue *q = NULL;
struct rnbd_cpu_qlist *cpu_q;
unsigned long flags;
int *cpup;
/*
* To keep fairness and not to let other queues starve we always
* try to wake up someone else in round-robin manner. That of course
* increases latency but queues always have a chance to be executed.
*/
cpup = get_cpu_ptr(sess->cpu_rr);
for (cpu_q = rnbd_get_cpu_qlist(sess, nxt_cpu(*cpup)); cpu_q;
cpu_q = rnbd_get_cpu_qlist(sess, nxt_cpu(cpu_q->cpu))) {
if (!spin_trylock_irqsave(&cpu_q->requeue_lock, flags))
continue;
if (unlikely(!test_bit(cpu_q->cpu, sess->cpu_queues_bm)))
goto unlock;
q = list_first_entry_or_null(&cpu_q->requeue_list,
typeof(*q), requeue_list);
if (WARN_ON(!q))
goto clear_bit;
list_del_init(&q->requeue_list);
clear_bit_unlock(0, &q->in_list);
if (list_empty(&cpu_q->requeue_list)) {
/* Clear bit if nothing is left */
clear_bit:
clear_bit(cpu_q->cpu, sess->cpu_queues_bm);
}
unlock:
spin_unlock_irqrestore(&cpu_q->requeue_lock, flags);
if (q)
break;
}
/**
* Saves the CPU that is going to be requeued on the per-cpu var. Just
* incrementing it doesn't work because rnbd_get_cpu_qlist() will
* always return the first CPU with something on the queue list when the
* value stored on the var is greater than the last CPU with something
* on the list.
*/
if (cpu_q)
*cpup = cpu_q->cpu;
put_cpu_var(sess->cpu_rr);
if (q)
rnbd_clt_dev_requeue(q);
return q;
}
/**
* rnbd_rerun_all_if_idle() - rerun all queues left in the list if
* session is idling (there are no requests
* in-flight).
* @sess: Session to rerun the queues on
*
* Description:
* This function tries to rerun all stopped queues if there are no
* requests in-flight anymore. This function tries to solve an obvious
* problem, when number of tags < than number of queues (hctx), which
* are stopped and put to sleep. If last permit, which has been just put,
* does not wake up all left queues (hctxs), IO requests hang forever.
*
* That can happen when all number of permits, say N, have been exhausted
* from one CPU, and we have many block devices per session, say M.
* Each block device has it's own queue (hctx) for each CPU, so eventually
* we can put that number of queues (hctxs) to sleep: M x nr_cpu_ids.
* If number of permits N < M x nr_cpu_ids finally we will get an IO hang.
*
* To avoid this hang last caller of rnbd_put_permit() (last caller is the
* one who observes sess->busy == 0) must wake up all remaining queues.
*
* Context:
* Does not matter.
*/
static void rnbd_rerun_all_if_idle(struct rnbd_clt_session *sess)
{
bool requeued;
do {
requeued = rnbd_rerun_if_needed(sess);
} while (atomic_read(&sess->busy) == 0 && requeued);
}
static struct rtrs_permit *rnbd_get_permit(struct rnbd_clt_session *sess,
enum rtrs_clt_con_type con_type,
int wait)
{
struct rtrs_permit *permit;
permit = rtrs_clt_get_permit(sess->rtrs, con_type,
wait ? RTRS_PERMIT_WAIT :
RTRS_PERMIT_NOWAIT);
if (likely(permit))
/* We have a subtle rare case here, when all permits can be
* consumed before busy counter increased. This is safe,
* because loser will get NULL as a permit, observe 0 busy
* counter and immediately restart the queue himself.
*/
atomic_inc(&sess->busy);
return permit;
}
static void rnbd_put_permit(struct rnbd_clt_session *sess,
struct rtrs_permit *permit)
{
rtrs_clt_put_permit(sess->rtrs, permit);
atomic_dec(&sess->busy);
/* Paired with rnbd_clt_dev_add_to_requeue(). Decrement first
* and then check queue bits.
*/
smp_mb__after_atomic();
rnbd_rerun_all_if_idle(sess);
}
static struct rnbd_iu *rnbd_get_iu(struct rnbd_clt_session *sess,
enum rtrs_clt_con_type con_type,
int wait)
{
struct rnbd_iu *iu;
struct rtrs_permit *permit;
permit = rnbd_get_permit(sess, con_type,
wait ? RTRS_PERMIT_WAIT :
RTRS_PERMIT_NOWAIT);
if (unlikely(!permit))
return NULL;
iu = rtrs_permit_to_pdu(permit);
iu->permit = permit;
/*
* 1st reference is dropped after finishing sending a "user" message,
* 2nd reference is dropped after confirmation with the response is
* returned.
* 1st and 2nd can happen in any order, so the rnbd_iu should be
* released (rtrs_permit returned to ibbtrs) only leased after both
* are finished.
*/
atomic_set(&iu->refcount, 2);
init_waitqueue_head(&iu->comp.wait);
iu->comp.errno = INT_MAX;
return iu;
}
static void rnbd_put_iu(struct rnbd_clt_session *sess, struct rnbd_iu *iu)
{
if (atomic_dec_and_test(&iu->refcount))
rnbd_put_permit(sess, iu->permit);
}
static void rnbd_softirq_done_fn(struct request *rq)
{
struct rnbd_clt_dev *dev = rq->rq_disk->private_data;
struct rnbd_clt_session *sess = dev->sess;
struct rnbd_iu *iu;
iu = blk_mq_rq_to_pdu(rq);
rnbd_put_permit(sess, iu->permit);
blk_mq_end_request(rq, errno_to_blk_status(iu->errno));
}
static void msg_io_conf(void *priv, int errno)
{
struct rnbd_iu *iu = priv;
struct rnbd_clt_dev *dev = iu->dev;
struct request *rq = iu->rq;
int rw = rq_data_dir(rq);
iu->errno = errno;
blk_mq_complete_request(rq);
if (errno)
rnbd_clt_info_rl(dev, "%s I/O failed with err: %d\n",
rw == READ ? "read" : "write", errno);
}
static void wake_up_iu_comp(struct rnbd_iu *iu, int errno)
{
iu->comp.errno = errno;
wake_up(&iu->comp.wait);
}
static void msg_conf(void *priv, int errno)
{
struct rnbd_iu *iu = priv;
iu->errno = errno;
schedule_work(&iu->work);
}
enum wait_type {
NO_WAIT = 0,
WAIT = 1
};
static int send_usr_msg(struct rtrs_clt *rtrs, int dir,
struct rnbd_iu *iu, struct kvec *vec,
size_t len, struct scatterlist *sg, unsigned int sg_len,
void (*conf)(struct work_struct *work),
int *errno, enum wait_type wait)
{
int err;
struct rtrs_clt_req_ops req_ops;
INIT_WORK(&iu->work, conf);
req_ops = (struct rtrs_clt_req_ops) {
.priv = iu,
.conf_fn = msg_conf,
};
err = rtrs_clt_request(dir, &req_ops, rtrs, iu->permit,
vec, 1, len, sg, sg_len);
if (!err && wait) {
wait_event(iu->comp.wait, iu->comp.errno != INT_MAX);
*errno = iu->comp.errno;
} else {
*errno = 0;
}
return err;
}
static void msg_close_conf(struct work_struct *work)
{
struct rnbd_iu *iu = container_of(work, struct rnbd_iu, work);
struct rnbd_clt_dev *dev = iu->dev;
wake_up_iu_comp(iu, iu->errno);
rnbd_put_iu(dev->sess, iu);
rnbd_clt_put_dev(dev);
}
static int send_msg_close(struct rnbd_clt_dev *dev, u32 device_id, bool wait)
{
struct rnbd_clt_session *sess = dev->sess;
struct rnbd_msg_close msg;
struct rnbd_iu *iu;
struct kvec vec = {
.iov_base = &msg,
.iov_len = sizeof(msg)
};
int err, errno;
iu = rnbd_get_iu(sess, RTRS_ADMIN_CON, RTRS_PERMIT_WAIT);
if (!iu)
return -ENOMEM;
iu->buf = NULL;
iu->dev = dev;
sg_mark_end(&iu->sglist[0]);
msg.hdr.type = cpu_to_le16(RNBD_MSG_CLOSE);
msg.device_id = cpu_to_le32(device_id);
WARN_ON(!rnbd_clt_get_dev(dev));
err = send_usr_msg(sess->rtrs, WRITE, iu, &vec, 0, NULL, 0,
msg_close_conf, &errno, wait);
if (err) {
rnbd_clt_put_dev(dev);
rnbd_put_iu(sess, iu);
} else {
err = errno;
}
rnbd_put_iu(sess, iu);
return err;
}
static void msg_open_conf(struct work_struct *work)
{
struct rnbd_iu *iu = container_of(work, struct rnbd_iu, work);
struct rnbd_msg_open_rsp *rsp = iu->buf;
struct rnbd_clt_dev *dev = iu->dev;
int errno = iu->errno;
if (errno) {
rnbd_clt_err(dev,
"Opening failed, server responded: %d\n",
errno);
} else {
errno = process_msg_open_rsp(dev, rsp);
if (errno) {
u32 device_id = le32_to_cpu(rsp->device_id);
/*
* If server thinks its fine, but we fail to process
* then be nice and send a close to server.
*/
(void)send_msg_close(dev, device_id, NO_WAIT);
}
}
kfree(rsp);
wake_up_iu_comp(iu, errno);
rnbd_put_iu(dev->sess, iu);
rnbd_clt_put_dev(dev);
}
static void msg_sess_info_conf(struct work_struct *work)
{
struct rnbd_iu *iu = container_of(work, struct rnbd_iu, work);
struct rnbd_msg_sess_info_rsp *rsp = iu->buf;
struct rnbd_clt_session *sess = iu->sess;
if (!iu->errno)
sess->ver = min_t(u8, rsp->ver, RNBD_PROTO_VER_MAJOR);
kfree(rsp);
wake_up_iu_comp(iu, iu->errno);
rnbd_put_iu(sess, iu);
rnbd_clt_put_sess(sess);
}
static int send_msg_open(struct rnbd_clt_dev *dev, bool wait)
{
struct rnbd_clt_session *sess = dev->sess;
struct rnbd_msg_open_rsp *rsp;
struct rnbd_msg_open msg;
struct rnbd_iu *iu;
struct kvec vec = {
.iov_base = &msg,
.iov_len = sizeof(msg)
};
int err, errno;
rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
if (!rsp)
return -ENOMEM;
iu = rnbd_get_iu(sess, RTRS_ADMIN_CON, RTRS_PERMIT_WAIT);
if (!iu) {
kfree(rsp);
return -ENOMEM;
}
iu->buf = rsp;
iu->dev = dev;
sg_init_one(iu->sglist, rsp, sizeof(*rsp));
msg.hdr.type = cpu_to_le16(RNBD_MSG_OPEN);
msg.access_mode = dev->access_mode;
strlcpy(msg.dev_name, dev->pathname, sizeof(msg.dev_name));
WARN_ON(!rnbd_clt_get_dev(dev));
err = send_usr_msg(sess->rtrs, READ, iu,
&vec, sizeof(*rsp), iu->sglist, 1,
msg_open_conf, &errno, wait);
if (err) {
rnbd_clt_put_dev(dev);
rnbd_put_iu(sess, iu);
kfree(rsp);
} else {
err = errno;
}
rnbd_put_iu(sess, iu);
return err;
}
static int send_msg_sess_info(struct rnbd_clt_session *sess, bool wait)
{
struct rnbd_msg_sess_info_rsp *rsp;
struct rnbd_msg_sess_info msg;
struct rnbd_iu *iu;
struct kvec vec = {
.iov_base = &msg,
.iov_len = sizeof(msg)
};
int err, errno;
rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
if (!rsp)
return -ENOMEM;
iu = rnbd_get_iu(sess, RTRS_ADMIN_CON, RTRS_PERMIT_WAIT);
if (!iu) {
kfree(rsp);
return -ENOMEM;
}
iu->buf = rsp;
iu->sess = sess;
sg_init_one(iu->sglist, rsp, sizeof(*rsp));
msg.hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO);
msg.ver = RNBD_PROTO_VER_MAJOR;
if (!rnbd_clt_get_sess(sess)) {
/*
* That can happen only in one case, when RTRS has restablished
* the connection and link_ev() is called, but session is almost
* dead, last reference on session is put and caller is waiting
* for RTRS to close everything.
*/
err = -ENODEV;
goto put_iu;
}
err = send_usr_msg(sess->rtrs, READ, iu,
&vec, sizeof(*rsp), iu->sglist, 1,
msg_sess_info_conf, &errno, wait);
if (err) {
rnbd_clt_put_sess(sess);
put_iu:
rnbd_put_iu(sess, iu);
kfree(rsp);
} else {
err = errno;
}
rnbd_put_iu(sess, iu);
return err;
}
static void set_dev_states_to_disconnected(struct rnbd_clt_session *sess)
{
struct rnbd_clt_dev *dev;
mutex_lock(&sess->lock);
list_for_each_entry(dev, &sess->devs_list, list) {
rnbd_clt_err(dev, "Device disconnected.\n");
mutex_lock(&dev->lock);
if (dev->dev_state == DEV_STATE_MAPPED)
dev->dev_state = DEV_STATE_MAPPED_DISCONNECTED;
mutex_unlock(&dev->lock);
}
mutex_unlock(&sess->lock);
}
static void remap_devs(struct rnbd_clt_session *sess)
{
struct rnbd_clt_dev *dev;
struct rtrs_attrs attrs;
int err;
/*
* Careful here: we are called from RTRS link event directly,
* thus we can't send any RTRS request and wait for response
* or RTRS will not be able to complete request with failure
* if something goes wrong (failing of outstanding requests
* happens exactly from the context where we are blocking now).
*
* So to avoid deadlocks each usr message sent from here must
* be asynchronous.
*/
err = send_msg_sess_info(sess, NO_WAIT);
if (err) {
pr_err("send_msg_sess_info(\"%s\"): %d\n", sess->sessname, err);
return;
}
err = rtrs_clt_query(sess->rtrs, &attrs);
if (err) {
pr_err("rtrs_clt_query(\"%s\"): %d\n", sess->sessname, err);
return;
}
mutex_lock(&sess->lock);
sess->max_io_size = attrs.max_io_size;
list_for_each_entry(dev, &sess->devs_list, list) {
bool skip;
mutex_lock(&dev->lock);
skip = (dev->dev_state == DEV_STATE_INIT);
mutex_unlock(&dev->lock);
if (skip)
/*
* When device is establishing connection for the first
* time - do not remap, it will be closed soon.
*/
continue;
rnbd_clt_info(dev, "session reconnected, remapping device\n");
err = send_msg_open(dev, NO_WAIT);
if (err) {
rnbd_clt_err(dev, "send_msg_open(): %d\n", err);
break;
}
}
mutex_unlock(&sess->lock);
}
static void rnbd_clt_link_ev(void *priv, enum rtrs_clt_link_ev ev)
{
struct rnbd_clt_session *sess = priv;
switch (ev) {
case RTRS_CLT_LINK_EV_DISCONNECTED:
set_dev_states_to_disconnected(sess);
break;
case RTRS_CLT_LINK_EV_RECONNECTED:
remap_devs(sess);
break;
default:
pr_err("Unknown session event received (%d), session: %s\n",
ev, sess->sessname);
}
}
static void rnbd_init_cpu_qlists(struct rnbd_cpu_qlist __percpu *cpu_queues)
{
unsigned int cpu;
struct rnbd_cpu_qlist *cpu_q;
for_each_possible_cpu(cpu) {
cpu_q = per_cpu_ptr(cpu_queues, cpu);
cpu_q->cpu = cpu;
INIT_LIST_HEAD(&cpu_q->requeue_list);
spin_lock_init(&cpu_q->requeue_lock);
}
}
static void destroy_mq_tags(struct rnbd_clt_session *sess)
{
if (sess->tag_set.tags)
blk_mq_free_tag_set(&sess->tag_set);
}
static inline void wake_up_rtrs_waiters(struct rnbd_clt_session *sess)
{
sess->rtrs_ready = true;
wake_up_all(&sess->rtrs_waitq);
}
static void close_rtrs(struct rnbd_clt_session *sess)
{
might_sleep();
if (!IS_ERR_OR_NULL(sess->rtrs)) {
rtrs_clt_close(sess->rtrs);
sess->rtrs = NULL;
wake_up_rtrs_waiters(sess);
}
}
static void free_sess(struct rnbd_clt_session *sess)
{
WARN_ON(!list_empty(&sess->devs_list));
might_sleep();
close_rtrs(sess);
destroy_mq_tags(sess);
if (!list_empty(&sess->list)) {
mutex_lock(&sess_lock);
list_del(&sess->list);
mutex_unlock(&sess_lock);
}
free_percpu(sess->cpu_queues);
free_percpu(sess->cpu_rr);
mutex_destroy(&sess->lock);
kfree(sess);
}
static struct rnbd_clt_session *alloc_sess(const char *sessname)
{
struct rnbd_clt_session *sess;
int err, cpu;
sess = kzalloc_node(sizeof(*sess), GFP_KERNEL, NUMA_NO_NODE);
if (!sess)
return ERR_PTR(-ENOMEM);
strlcpy(sess->sessname, sessname, sizeof(sess->sessname));
atomic_set(&sess->busy, 0);
mutex_init(&sess->lock);
INIT_LIST_HEAD(&sess->devs_list);
INIT_LIST_HEAD(&sess->list);
bitmap_zero(sess->cpu_queues_bm, NR_CPUS);
init_waitqueue_head(&sess->rtrs_waitq);
refcount_set(&sess->refcount, 1);
sess->cpu_queues = alloc_percpu(struct rnbd_cpu_qlist);
if (!sess->cpu_queues) {
err = -ENOMEM;
goto err;
}
rnbd_init_cpu_qlists(sess->cpu_queues);
/*
* That is simple percpu variable which stores cpu indeces, which are
* incremented on each access. We need that for the sake of fairness
* to wake up queues in a round-robin manner.
*/
sess->cpu_rr = alloc_percpu(int);
if (!sess->cpu_rr) {
err = -ENOMEM;
goto err;
}
for_each_possible_cpu(cpu)
* per_cpu_ptr(sess->cpu_rr, cpu) = cpu;
return sess;
err:
free_sess(sess);
return ERR_PTR(err);
}
static int wait_for_rtrs_connection(struct rnbd_clt_session *sess)
{
wait_event(sess->rtrs_waitq, sess->rtrs_ready);
if (IS_ERR_OR_NULL(sess->rtrs))
return -ECONNRESET;
return 0;
}
static void wait_for_rtrs_disconnection(struct rnbd_clt_session *sess)
__releases(&sess_lock)
__acquires(&sess_lock)
{
DEFINE_WAIT(wait);
prepare_to_wait(&sess->rtrs_waitq, &wait, TASK_UNINTERRUPTIBLE);
if (IS_ERR_OR_NULL(sess->rtrs)) {
finish_wait(&sess->rtrs_waitq, &wait);
return;
}
mutex_unlock(&sess_lock);
/* loop in caller, see __find_and_get_sess().
* You can't leave mutex locked and call schedule(), you will catch a
* deadlock with a caller of free_sess(), which has just put the last
* reference and is about to take the sess_lock in order to delete
* the session from the list.
*/
schedule();
mutex_lock(&sess_lock);
}
static struct rnbd_clt_session *__find_and_get_sess(const char *sessname)
__releases(&sess_lock)
__acquires(&sess_lock)
{
struct rnbd_clt_session *sess, *sn;
int err;
again:
list_for_each_entry_safe(sess, sn, &sess_list, list) {
if (strcmp(sessname, sess->sessname))
continue;
if (sess->rtrs_ready && IS_ERR_OR_NULL(sess->rtrs))
/*
* No RTRS connection, session is dying.
*/
continue;
if (rnbd_clt_get_sess(sess)) {
/*
* Alive session is found, wait for RTRS connection.
*/
mutex_unlock(&sess_lock);
err = wait_for_rtrs_connection(sess);
if (err)
rnbd_clt_put_sess(sess);
mutex_lock(&sess_lock);
if (err)
/* Session is dying, repeat the loop */
goto again;
return sess;
}
/*
* Ref is 0, session is dying, wait for RTRS disconnect
* in order to avoid session names clashes.
*/
wait_for_rtrs_disconnection(sess);
/*
* RTRS is disconnected and soon session will be freed,
* so repeat a loop.
*/
goto again;
}
return NULL;
}
static struct
rnbd_clt_session *find_or_create_sess(const char *sessname, bool *first)
{
struct rnbd_clt_session *sess = NULL;
mutex_lock(&sess_lock);
sess = __find_and_get_sess(sessname);
if (!sess) {
sess = alloc_sess(sessname);
if (IS_ERR(sess)) {
mutex_unlock(&sess_lock);
return sess;
}
list_add(&sess->list, &sess_list);
*first = true;
} else
*first = false;
mutex_unlock(&sess_lock);
return sess;
}
static int rnbd_client_open(struct block_device *block_device, fmode_t mode)
{
struct rnbd_clt_dev *dev = block_device->bd_disk->private_data;
if (dev->read_only && (mode & FMODE_WRITE))
return -EPERM;
if (dev->dev_state == DEV_STATE_UNMAPPED ||
!rnbd_clt_get_dev(dev))
return -EIO;
return 0;
}
static void rnbd_client_release(struct gendisk *gen, fmode_t mode)
{
struct rnbd_clt_dev *dev = gen->private_data;
rnbd_clt_put_dev(dev);
}
static int rnbd_client_getgeo(struct block_device *block_device,
struct hd_geometry *geo)
{
u64 size;
struct rnbd_clt_dev *dev;
dev = block_device->bd_disk->private_data;
size = dev->size * (dev->logical_block_size / SECTOR_SIZE);
geo->cylinders = size >> 6; /* size/64 */
geo->heads = 4;
geo->sectors = 16;
geo->start = 0;
return 0;
}
static const struct block_device_operations rnbd_client_ops = {
.owner = THIS_MODULE,
.open = rnbd_client_open,
.release = rnbd_client_release,
.getgeo = rnbd_client_getgeo
};
/* The amount of data that belongs to an I/O and the amount of data that
* should be read or written to the disk (bi_size) can differ.
*
* E.g. When WRITE_SAME is used, only a small amount of data is
* transferred that is then written repeatedly over a lot of sectors.
*
* Get the size of data to be transferred via RTRS by summing up the size
* of the scather-gather list entries.
*/
static size_t rnbd_clt_get_sg_size(struct scatterlist *sglist, u32 len)
{
struct scatterlist *sg;
size_t tsize = 0;
int i;
for_each_sg(sglist, sg, len, i)
tsize += sg->length;
return tsize;
}
static int rnbd_client_xfer_request(struct rnbd_clt_dev *dev,
struct request *rq,
struct rnbd_iu *iu)
{