-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshannon_block.c
More file actions
1646 lines (1380 loc) · 43.3 KB
/
Copy pathshannon_block.c
File metadata and controls
1646 lines (1380 loc) · 43.3 KB
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
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/blkdev.h>
#include <linux/genhd.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/pci.h>
#include <linux/spinlock.h>
#ifdef CONFIG_SUSE_PRODUCT_CODE
#include <linux/suse_version.h>
#endif
#include "shannon_port.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) || \
(defined(SHANNON_RHEL_RELEASE_OVER_8_0)) || \
(defined(SHANNON_SUSE_RELEASE_OVER_1_12_5))
#include <linux/blk-mq.h>
#endif
#include "shannon_block.h"
#include "shannon_device.h"
#include "shannon_time.h"
extern void increase_ns_pending_bios(struct shannon_namespace *ns);
extern void decrease_ns_pending_bios(struct shannon_namespace *ns);
extern int check_and_alloc_lpmt(struct shannon_disk *sdisk, struct shannon_bio *sbio, unsigned int logicb_shift);
extern int shannon_fio_cpumask_set_enable;
int shannon_use_iosched = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) || \
(defined(SHANNON_RHEL_RELEASE_OVER_8_0)) || \
defined(SHANNON_SUSE_RELEASE_OVER_1_12_5)
struct shannon_blk_mq_data {
struct blk_mq_tag_set tag_set;
void *original_data;
};
#endif
// Helpers
const char *get_gendisk_name(shannon_gendisk_t *gd)
{
return ((struct gendisk *) gd)->disk_name;
}
// genhd.h
shannon_gendisk_t *shannon_alloc_disk(int minors)
{
return alloc_disk(minors);
}
extern struct block_device_operations shannon_ops;
extern struct block_device_operations shannon_ops_ns;
int shannon_init_gendisk(shannon_gendisk_t *disk, char *name, int major, int minor_span, int first_minor, shannon_request_queue_t *rq, void *pri)
{
struct gendisk *gd = (struct gendisk *)disk;
snprintf(gd->disk_name, 32, name);
gd->major = major;
gd->minors = minor_span;
gd->first_minor = first_minor;
debugs0("disk_name=%s, major=%d, minors=%d, first_minor=%d.\n",
gd->disk_name, gd->major, gd->minors, gd->first_minor);
gd->queue = (struct request_queue *)rq;
gd->private_data = pri;
gd->fops = &shannon_ops;
/* dfX is a conventional disk; pXvolX is namespace. */
if (*name != 'd')
gd->fops = &shannon_ops_ns;
return 0;
}
void shannon_set_capacity(shannon_gendisk_t *disk, shannon_sector_t size)
{
set_capacity((struct gendisk *)disk, size);
}
void shannon_set_disk_ro(shannon_gendisk_t *disk, int flag)
{
set_disk_ro((struct gendisk *)disk, flag);
}
void shannon_put_disk(shannon_gendisk_t *disk)
{
put_disk((struct gendisk *)disk);
}
void shannon_add_disk(shannon_gendisk_t *disk)
{
add_disk((struct gendisk *)disk);
}
void shannon_del_gendisk(shannon_gendisk_t *gp)
{
del_gendisk((struct gendisk *)gp);
}
// fs.h
int shannon_register_blkdev(unsigned int major, const char *name)
{
return register_blkdev(major, name);
}
void shannon_unregister_blkdev(unsigned int major, const char *name)
{
unregister_blkdev(major, name);
}
// blkdev.h
void shannon_blk_queue_block_size(shannon_request_queue_t *queue, unsigned int logical_size, unsigned int physical_size)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
blk_queue_logical_block_size((struct request_queue *)queue, logical_size);
blk_queue_physical_block_size((struct request_queue *)queue, physical_size);
#else
blk_queue_hardsect_size((struct request_queue *)queue, logical_size);
#endif
}
void shannon_blk_queue_max_hw_sectors(shannon_request_queue_t *q, unsigned int max_hw_sectors)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
blk_queue_max_hw_sectors((struct request_queue *)q, max_hw_sectors);
#else
blk_queue_max_sectors((struct request_queue *)q, max_hw_sectors);
#endif
}
void shannon_blk_queue_io_min(shannon_request_queue_t *queue, unsigned int min)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
blk_queue_io_min((struct request_queue *)queue, min);
#endif
}
void shannon_blk_queue_io_opt(shannon_request_queue_t *queue, unsigned int opt)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
blk_queue_io_opt((struct request_queue *)queue, opt);
#endif
}
static int shannon_should_trim_bio(shannon_bio_t *bio)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
return ((struct bio *)bio)->bi_opf && (bio_op((struct bio *)bio) == REQ_OP_DISCARD);
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
return ((struct bio *)bio)->bi_rw & REQ_DISCARD;
#else
#if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 28)
return bio_discard((struct bio *)bio);
#else
return bio_rw_flagged((struct bio *)bio, BIO_RW_DISCARD);
#endif
#endif
#else
return 0;
#endif
#endif
}
void shannon_queue_flag_set(int flag, shannon_request_queue_t *queue)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
set_bit(flag, &(((struct request_queue *)queue)->queue_flags));
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) && \
!defined(SHANNON_SUSE_RELEASE_OVER_1_12_5)
queue_flag_set_unlocked(flag, (struct request_queue *)queue);
#else
blk_queue_flag_set(flag, (struct request_queue *)queue);
#endif
}
void shannon_queue_flag_clear(int flag, shannon_request_queue_t *queue)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
clear_bit(flag, &(((struct request_queue *)queue)->queue_flags));
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) && \
!defined(SHANNON_SUSE_RELEASE_OVER_1_12_5)
queue_flag_clear_unlocked(flag, (struct request_queue *)queue);
#else
blk_queue_flag_clear(flag, (struct request_queue *)queue);
#endif
}
void shannon_trim_setting(shannon_request_queue_t *queue)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)
// RHEL 6.0 support discard in kernel 2.6.32
#ifdef SHANNON_ON_RHEL
#ifdef SHANNON_RHEL_RELEASE_OVER_6_0
((struct request_queue *)queue)->limits.discard_granularity = PAGE_SIZE;
#endif
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
((struct request_queue *)queue)->limits.discard_granularity = PAGE_SIZE;
#endif
((struct request_queue *)queue)->limits.max_discard_sectors = (UINT_MAX >> 9) & ~7;
// #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
// ((struct request_queue *)queue)->limits.discard_zeroes_data = 1;
// #endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) && \
!defined(SHANNON_SUSE_RELEASE_OVER_1_12_5)
queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, (struct request_queue *)queue);
#else
blk_queue_flag_set(QUEUE_FLAG_DISCARD, (struct request_queue *)queue);
#endif
#endif
}
void shannon_rotational_setting(shannon_request_queue_t *queue)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
shannon_queue_flag_set(QUEUE_FLAG_NONROT, queue);
#endif
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) || \
(defined(SHANNON_RHEL_RELEASE_OVER_8_0)) || \
defined(SHANNON_SUSE_RELEASE_OVER_1_12_5)
void shannon_blk_mq_free_tag_set(struct blk_mq_tag_set *tag_set)
{
blk_mq_free_tag_set(tag_set);
}
void shannon_blk_cleanup_queue(shannon_request_queue_t *q)
{
struct request_queue *queue = (struct request_queue *) q;
struct shannon_blk_mq_data *data = (struct shannon_blk_mq_data*) queue->queuedata;
if (data && shannon_use_iosched) {
shannon_blk_mq_free_tag_set(&data->tag_set);
shannon_kfree(data);
}
blk_cleanup_queue(queue);
}
#else
void shannon_blk_cleanup_queue(shannon_request_queue_t *q)
{
blk_cleanup_queue((struct request_queue *)q);
}
#endif
// bio.h
static void shannon_bio_endio(shannon_bio_t *bio, int error)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
if (error)
bio_endio((struct bio *)bio, 0, error);
else
bio_endio((struct bio *)bio, ((struct bio *)bio)->bi_size, 0);
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
bio_endio((struct bio *)bio, error);
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
#ifdef SHANNON_ON_SUSE
#ifdef SHANNON_SUSE_RELEASE_BELOW_1_12_5
((struct bio *)bio)->bi_error = error;
bio_endio((struct bio *)bio);
#else
((struct bio *)bio)->bi_status = error;
bio_endio((struct bio *)bio);
#endif
#else
((struct bio *)bio)->bi_error = error;
bio_endio((struct bio *)bio);
#endif
#else
((struct bio *)bio)->bi_status = error;
bio_endio((struct bio *)bio);
#endif
}
int shannon_bio_flagged(shannon_bio_t *bio, unsigned int flag)
{
return bio_flagged((struct bio *)bio, flag);
}
unsigned long shannon_bio_data_dir(shannon_bio_t *bio)
{
return bio_data_dir((struct bio *)bio);
}
static unsigned int shannon_bio_segments(shannon_bio_t *bio)
{
return bio_segments((struct bio *)bio);
}
static unsigned int get_bi_size(shannon_bio_t *bio)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
return ((struct bio *)bio)->bi_iter.bi_size;
#else
return ((struct bio *)bio)->bi_size;
#endif
}
shannon_sector_t get_bi_sector(shannon_bio_t *bio)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
return ((struct bio *)bio)->bi_iter.bi_sector;
#else
return ((struct bio *)bio)->bi_sector;
#endif
}
extern shannon_gendisk_t *get_gendisk_from_ns(struct shannon_namespace *ns);
extern unsigned int ns_get_logicb_size(struct shannon_namespace *ns);
extern unsigned int ns_get_logicb_shift(struct shannon_namespace *ns);
extern struct shannon_pool *ns_get_pool(struct shannon_namespace *ns);
extern struct shannon_disk *get_shannon_disk_from_ns(struct shannon_namespace *ns);
extern u64 pool_used_logicbs(struct shannon_pool *sp);
extern u64 pool_available_cap(struct shannon_pool *sp);
extern shannon_gendisk_t *get_gendisk_from_sdev(struct shannon_dev *sdev);
extern unsigned int get_logicb_size(struct shannon_dev *sdev);
extern unsigned int get_logicb_shift(struct shannon_dev *sdev);
extern struct shannon_disk *get_shannon_disk_from_sdev(struct shannon_dev *sdev);
extern int shannon_check_availability(struct shannon_dev *sdev);
extern int shannon_check_availability_ns(struct shannon_namespace *);
extern int shannon_disk_readonly(struct shannon_dev *sdev);
extern int shannon_disk_readonly_ns(struct shannon_namespace *);
extern int shannon_submit_bio(struct shannon_dev *sdev, struct shannon_bio *sbio);
extern int shannon_submit_bio_throttling_ns(struct shannon_namespace *ns, struct shannon_bio *sbio);
extern void shannon_discard(struct shannon_dev *sdev, logicb64_t start_lba, logicb64_t end_lba);
extern void shannon_discard_ns(struct shannon_namespace *ns, logicb64_t start_lba, logicb64_t end_lba, int del_ns);
#ifdef CONFIG_SHANNON_DEBUG
void set_sbio_debug_tag(struct shannon_bio *sbio, unsigned long tag)
{
sbio->tag = tag;
}
#endif
int shannon_alloc_bounce_pages(shannon_sg_list_t *sgtable, int first_size, int total_size)
{
int page_cnt = 0, i = 0, j = 0;
int first_page_offest = 0, last_page_len = 0;
struct page *page = NULL;
shannon_sg_list_t *sge = sgtable;
if (first_size) {
page_cnt++;
first_page_offest = PAGE_SIZE - first_size;
}
page_cnt += (total_size - first_size) / PAGE_SIZE;
last_page_len = (total_size - first_size) % PAGE_SIZE;
if (last_page_len)
page_cnt++;
for (i = 0; i < page_cnt; i++) {
page = alloc_page(GFP_KERNEL);
if (!page)
goto fail_out;
if ((i == 0) && first_page_offest)
shannon_sg_set_page(sge, page, first_size, first_page_offest);
else if ((i == page_cnt - 1) && last_page_len)
shannon_sg_set_page(sge, page, last_page_len, 0);
else
shannon_sg_set_page(sge, page, PAGE_SIZE, 0);
sge = shannon_sg_next(sge);
}
shannon_sg_mark_end(sge);
return page_cnt;
fail_out:
shannon_for_each_sg(sgtable, sge, i, j)
__free_page(shannon_sg_page(sge));
return -ENOMEM;
}
/* We presume that page_address will never return NULL, which is true for 64bit platforms */
void shannon_copy_bounce_pages(shannon_bio_t *lbio, shannon_sg_list_t *sgtable, int to_sg)
{
struct bio *bio = (struct bio *)lbio;
shannon_sg_list_t *sge = sgtable;
int sge_offset, sge_len;
int bvec_offset, bvec_len;
int copy_len;
void *sge_addr, *bvec_addr;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
struct bio_vec bvec;
struct bvec_iter iter;
sge_len = shannon_sg_length(sge);
sge_offset = shannon_sg_offset(sge);
bio_for_each_segment(bvec, bio, iter) {
bvec_len = bvec.bv_len;
bvec_offset = bvec.bv_offset;
do {
if (sge_len > bvec_len)
copy_len = bvec_len;
else
copy_len = sge_len;
bvec_addr = page_address(bvec.bv_page) + bvec_offset;
sge_addr = page_address(shannon_sg_page(sge)) + sge_offset;
if (to_sg)
memcpy(sge_addr, bvec_addr, copy_len);
else
memcpy(bvec_addr, sge_addr, copy_len);
bvec_offset += copy_len;
bvec_len -= copy_len;
sge_offset += copy_len;
sge_len -= copy_len;
if (sge_len <= 0) {
sge = shannon_sg_next(sge);
sge_len = shannon_sg_length(sge);
sge_offset = shannon_sg_offset(sge);
}
} while (bvec_len > 0);
}
#else
struct bio_vec *bvec;
int i;
sge_len = shannon_sg_length(sge);
sge_offset = shannon_sg_offset(sge);
bio_for_each_segment(bvec, bio, i) {
bvec_len = bvec->bv_len;
bvec_offset = bvec->bv_offset;
do {
if (sge_len > bvec_len)
copy_len = bvec_len;
else
copy_len = sge_len;
bvec_addr = page_address(bvec->bv_page) + bvec_offset;
sge_addr = page_address(shannon_sg_page(sge)) + sge_offset;
if (to_sg)
memcpy(sge_addr, bvec_addr, copy_len);
else
memcpy(bvec_addr, sge_addr, copy_len);
bvec_offset += copy_len;
bvec_len -= copy_len;
sge_offset += copy_len;
sge_len -= copy_len;
if (sge_len <= 0) {
sge = shannon_sg_next(sge);
sge_len = shannon_sg_length(sge);
sge_offset = shannon_sg_offset(sge);
}
} while (bvec_len > 0);
}
#endif
}
void shannon_free_bounce_pages(shannon_sg_list_t *sgtable, int sg_count)
{
shannon_sg_list_t *sge;
int i;
shannon_for_each_sg(sgtable, sge, sg_count, i)
__free_page(shannon_sg_page(sge));
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
int shannon_convert_bio(struct shannon_bio *sbio, shannon_bio_t *lbio, unsigned int logicb_size)
{
shannon_sg_list_t *sg = NULL;
struct bio *bio = (struct bio *)lbio;
struct bio_vec bvec;
int offset, remained_size, map_size = 0, undone = 0;
struct bvec_iter iter;
int ret = 0;
int page_offset, mappable_size;
sbio->bio_size = get_bi_size(lbio);
sbio->segments = shannon_bio_segments(lbio);
sbio->sg_count = 2 * (((sbio->bio_size + logicb_size - 1)/logicb_size) + sbio->segments);
sbio->sg = shannon_sg_alloc(sbio->sg_count, GFP_SHANNON);
if (sbio->sg == NULL) {
sbio->sg = shannon_sg_vzalloc(sbio->sg_count);
if (sbio->sg == NULL) {
shannon_err("alloc sg failed.\n");
return -ENOMEM;
}
sbio->is_valloc = 1;
}
shannon_sg_init_table(sbio->sg, sbio->sg_count);
sbio->start_sector = get_bi_sector(lbio);
sbio->first_size = logicb_size - ((sbio->start_sector << 9) & (logicb_size - 1));
sbio->first_size = sbio->first_size % logicb_size;
sbio->first_size = min(sbio->bio_size, sbio->first_size);
bio_for_each_segment(bvec, bio, iter) {
if (bvec.bv_offset & 0x7) {
sbio->need_bounce = 1;
}
if (bvec.bv_len & 0x7) {
sbio->need_bounce = 1;
}
}
if (sbio->need_bounce) {
sbio->has_hole = 0;
ret = shannon_alloc_bounce_pages(sbio->sg, sbio->first_size, sbio->bio_size);
if (ret <= 0) {
goto old_fashion;
} else
sbio->used_sg_count = ret;
if (sbio->dma_dir == SHANNON_DMA_TODEVICE)
shannon_copy_bounce_pages(bio, sbio->sg, 1);
return 0;
}
old_fashion:
remained_size = sbio->first_size ? sbio->first_size : logicb_size;
sbio->has_hole = 0;
if (sbio->need_bounce) {
sbio->has_hole = 1;
sbio->need_bounce = 0;
}
sbio->used_sg_count = 0;
bio_for_each_segment(bvec, bio, iter) {
/* all segments' bv_offset have to be 0, except the first one. */
if ((iter.bi_idx != bio->bi_iter.bi_idx) && (bvec.bv_offset != 0)) {
sbio->has_hole = 1;
}
/* all segments have to end at 4096, except the last one. */
if ((iter.bi_idx != (sbio->segments - 1)) && ((bvec.bv_offset + bvec.bv_len) != 4096)) {
sbio->has_hole = 1;
}
offset = 0; /* the offset start from bvec->bv_offset. */
page_offset = bvec.bv_offset + offset;
while (offset < bvec.bv_len) {
mappable_size = PAGE_SIZE - (page_offset & (PAGE_SIZE -1));
if (mappable_size > (bvec.bv_len - offset))
mappable_size = bvec.bv_len - offset;
if (mappable_size < remained_size) {
map_size = mappable_size;
undone = 1;
} else {
map_size = remained_size;
undone = 0;
}
remained_size -= map_size;
if (remained_size == 0)
remained_size = logicb_size;
sg = sg ? shannon_sg_next(sg) : sbio->sg;
shannon_sg_set_page(sg, bvec.bv_page + (page_offset / PAGE_SIZE), map_size, page_offset % PAGE_SIZE);
sbio->used_sg_count++;
offset += map_size;
page_offset = bvec.bv_offset + offset;
}
}
shannon_sg_mark_end(sg);
return 0;
}
#else
int shannon_convert_bio(struct shannon_bio *sbio, shannon_bio_t *lbio, unsigned int logicb_size)
{
shannon_sg_list_t *sg = NULL;
struct bio *bio = (struct bio *)lbio;
struct bio_vec *bvec;
int i, offset, remained_size, map_size = 0, undone = 0;
int page_offset, mappable_size;
int ret = 0;
sbio->bio_size = get_bi_size(lbio);
sbio->segments = shannon_bio_segments(lbio);
sbio->sg_count = 2 * (((sbio->bio_size + logicb_size - 1)/logicb_size) + sbio->segments);
sbio->sg = shannon_sg_alloc(sbio->sg_count, GFP_SHANNON);
if (sbio->sg == NULL) {
sbio->sg = shannon_sg_vzalloc(sbio->sg_count);
if (sbio->sg == NULL) {
shannon_err("alloc sg failed.\n");
return -ENOMEM;
}
sbio->is_valloc = 1;
}
shannon_sg_init_table(sbio->sg, sbio->sg_count);
sbio->start_sector = get_bi_sector(lbio);
sbio->first_size = logicb_size - ((sbio->start_sector << 9) & (logicb_size - 1));
sbio->first_size = sbio->first_size % logicb_size;
sbio->first_size = min(sbio->bio_size, sbio->first_size);
bio_for_each_segment(bvec, bio, i) {
if (bvec->bv_offset & 0x7) {
sbio->need_bounce = 1;
}
if (bvec->bv_len & 0x7) {
sbio->need_bounce = 1;
}
}
if (sbio->need_bounce) {
sbio->has_hole = 0;
ret = shannon_alloc_bounce_pages(sbio->sg, sbio->first_size, sbio->bio_size);
if (ret <= 0) {
goto old_fashion;
}else
sbio->used_sg_count = ret;
if (sbio->dma_dir == SHANNON_DMA_TODEVICE)
shannon_copy_bounce_pages(bio, sbio->sg, 1);
return 0;
}
old_fashion:
remained_size = sbio->first_size ? sbio->first_size : logicb_size;
sbio->has_hole = 0;
if (sbio->need_bounce) {
sbio->has_hole = 1;
sbio->need_bounce = 0;
}
sbio->used_sg_count = 0;
bio_for_each_segment(bvec, bio, i) {
/* all segments' bv_offset have to be 0, except the first one. */
if ((i != bio->bi_idx) && (bvec->bv_offset != 0)) {
sbio->has_hole = 1;
}
/* all segments have to end at 4096, except the last one. */
if ((i != (sbio->segments - 1)) && ((bvec->bv_offset + bvec->bv_len) != 4096)) {
sbio->has_hole = 1;
}
offset = 0; /* the offset start from bvec->bv_offset. */
page_offset = bvec->bv_offset + offset;
while (offset < bvec->bv_len) {
mappable_size = PAGE_SIZE - (page_offset & (PAGE_SIZE -1));
if (mappable_size > (bvec->bv_len - offset))
mappable_size = bvec->bv_len - offset;
if (mappable_size < remained_size) {
map_size = mappable_size;
undone = 1;
} else {
map_size = remained_size;
undone = 0;
}
remained_size -= map_size;
if (remained_size == 0)
remained_size = logicb_size;
sg = sg ? shannon_sg_next(sg) : sbio->sg;
shannon_sg_set_page(sg, bvec->bv_page + (page_offset / PAGE_SIZE), map_size, page_offset % PAGE_SIZE);
sbio->used_sg_count++;
offset += map_size;
page_offset = bvec->bv_offset + offset;
}
}
shannon_sg_mark_end(sg);
return 0;
}
#endif
void submit_sbio_task(struct shannon_work_struct *work)
{
struct shannon_bio *sbio = container_of(work, struct shannon_bio, make_req_work);
struct shannon_dev *sdev = (struct shannon_dev *)sbio->data;
int ret;
ret = shannon_submit_bio(sdev, sbio);
if (ret) {
shannon_end_io_acct(get_gendisk_from_sdev(sdev), sbio->bio, 0);
if (sbio->sg) {
if (sbio->is_valloc)
shannon_sg_vfree(sbio->sg, sbio->sg_count);
else
shannon_sg_free(sbio->sg, sbio->sg_count);
}
shannon_bio_endio(sbio->bio, ret);
free_sbio(sbio);
}
}
void submit_sbio_task_ns(struct shannon_work_struct *work)
{
struct shannon_bio *sbio = container_of(work, struct shannon_bio, make_req_work);
struct shannon_namespace *ns = (struct shannon_namespace *)sbio->data;
int ret;
ret = shannon_submit_bio_throttling_ns(ns, sbio);
if (ret) {
shannon_end_io_acct(get_gendisk_from_ns(ns), sbio->bio, 0);
if (sbio->sg) {
if (sbio->is_valloc)
shannon_sg_vfree(sbio->sg, sbio->sg_count);
else
shannon_sg_free(sbio->sg, sbio->sg_count);
}
shannon_bio_endio(sbio->bio, ret);
free_sbio(sbio);
decrease_ns_pending_bios(ns);
}
}
extern int get_sdev_numa_node(struct shannon_dev *sdev);
void shannon_fio_cpumask_set(struct shannon_dev *sdev)
{
int numa_node = get_sdev_numa_node(sdev);
shannon_cpumask_struct_t *scpumask;
if (numa_node < 0)
return;
if (shannon_get_current_comm() == NULL)
return;
if (0 == shannon_strcmp(shannon_get_current_comm(), "fio")) {
scpumask = shannon_get_current_cpus_allowed();
if(shannon_not_set_cpumask(scpumask)){
shannon_set_node_cpus_allowed((shannon_task_struct_t *)shannon_current(), numa_node);
}
}
return;
}
extern shannon_workqueue_struct_t *shannon_percpu_wq;
int shannon_make_request(shannon_request_queue_t *q, shannon_bio_t *bio)
{
struct shannon_dev *sdev = ((struct request_queue *)q)->queuedata;
struct shannon_bio *sbio;
int ret;
unsigned int logicb_size = get_logicb_size(sdev);
unsigned int logicb_shift = get_logicb_shift(sdev);
if (shannon_check_availability(sdev))
{
shannon_bio_endio(bio, -EIO);
return 0;
}
if (unlikely(shannon_should_trim_bio(bio))) {
/* in sectors */
u64 trim_start = get_bi_sector(bio);
u64 trim_end = trim_start + (get_bi_size(bio) >> 9);
trim_start = _ALIGN_UP(trim_start, logicb_size/512);
trim_end = _ALIGN_DOWN(trim_end, logicb_size/512);
/* in logicbs */
trim_start = trim_start >> (logicb_shift - 9);
trim_end = trim_end >> (logicb_shift - 9);
shannon_discard(sdev, trim_start, trim_end);
shannon_bio_endio(bio, 0);
return 0;
}
if (unlikely(get_bi_size(bio) == 0)) {
shannon_bio_endio(bio, 0);
return 0;
}
if (unlikely(shannon_disk_readonly(sdev)) && (shannon_bio_data_dir(bio) == LINUX_BIO_WRITE)) {
shannon_bio_endio(bio, -EIO);
return 0;
}
if (shannon_fio_cpumask_set_enable) {
shannon_fio_cpumask_set(sdev);
}
sbio = alloc_sbio(GFP_SHANNON);
sbio->bio = bio;
sbio->lreq = NULL;
#ifdef CONFIG_SHANNON_ATOMIC_WRITE_VERIFY
shannon_strncpy(sbio->atomic_write_comm, shannon_get_current_comm(), 32);
#endif
if (shannon_bio_data_dir(bio))
sbio->dma_dir = SHANNON_DMA_TODEVICE;
else
sbio->dma_dir = SHANNON_DMA_FROMDEVICE;
SHANNON_INIT_LIST_HEAD(&sbio->global_list);
set_sbio_debug_tag(sbio, MAKE_REQUEST_TAG);
ret = shannon_convert_bio(sbio, bio, logicb_size);
if (ret)
goto free_sbio;
if (sbio->dma_dir == SHANNON_DMA_TODEVICE) {
if (unlikely(check_and_alloc_lpmt(get_shannon_disk_from_sdev(sdev), sbio, logicb_shift))) {
ret = -EIO;
goto free_sg_list;
}
}
// skipped request queue and io scheduler, must do disk statistics manually.
sbio->start_time = get_jiffies();
shannon_start_io_acct(get_gendisk_from_sdev(sdev), sbio->bio);
if (likely(shannon_percpu_wq)) {
sbio->data = sdev;
shannon_init_work(&sbio->make_req_work, submit_sbio_task);
shannon_queue_work(shannon_percpu_wq, &sbio->make_req_work);
} else {
ret = shannon_submit_bio(sdev, sbio);
if (ret)
goto end_io_acct;
}
return 0;
end_io_acct:
shannon_end_io_acct(get_gendisk_from_sdev(sdev), sbio->bio, 0);
free_sg_list:
if (sbio->need_bounce)
shannon_free_bounce_pages(sbio->sg, sbio->used_sg_count);
if (sbio->sg_count) {
if (sbio->is_valloc)
shannon_sg_vfree(sbio->sg, sbio->sg_count);
else
shannon_sg_free(sbio->sg, sbio->sg_count);
}
free_sbio:
free_sbio(sbio);
shannon_bio_endio(bio, ret);
return 0;
}
struct shannon_dev *get_shannon_dev_from_ns(struct shannon_namespace *ns);
int check_sbio_is_overwrited(struct shannon_bio *sbio, struct shannon_namespace *ns);
int shannon_make_request_ns(shannon_request_queue_t *q, shannon_bio_t *bio)
{
struct shannon_namespace *ns = ((struct request_queue *)q)->queuedata;
struct shannon_bio *sbio;
int ret;
unsigned int logicb_size = ns_get_logicb_size(ns);
unsigned int logicb_shift = ns_get_logicb_shift(ns);
increase_ns_pending_bios(ns);
if (shannon_check_availability_ns(ns)) {
shannon_bio_endio(bio, -EIO);
decrease_ns_pending_bios(ns);
return 0;
}
if (unlikely(shannon_should_trim_bio(bio))) {
/* in sectors */
u64 trim_start = get_bi_sector(bio);
u64 trim_end = trim_start + (get_bi_size(bio) >> 9);
trim_start = _ALIGN_UP(trim_start, logicb_size/512);
trim_end = _ALIGN_DOWN(trim_end, logicb_size/512);
/* in logicbs */
trim_start = trim_start >> (logicb_shift - 9);
trim_end = trim_end >> (logicb_shift - 9);
shannon_discard_ns(ns, trim_start, trim_end, 0);
shannon_bio_endio(bio, 0);
decrease_ns_pending_bios(ns);
return 0;
}
if (unlikely(get_bi_size(bio) == 0)) {
shannon_bio_endio(bio, 0);
decrease_ns_pending_bios(ns);
return 0;
}
sbio = alloc_sbio(GFP_SHANNON);
sbio->bio = bio;
sbio->lreq = NULL;
if (shannon_bio_data_dir(bio))
sbio->dma_dir = SHANNON_DMA_TODEVICE;
else
sbio->dma_dir = SHANNON_DMA_FROMDEVICE;
SHANNON_INIT_LIST_HEAD(&sbio->global_list);
set_sbio_debug_tag(sbio, MAKE_REQUEST_TAG);
ret = shannon_convert_bio(sbio, bio, logicb_size);
if (ret)
goto free_sbio;
if (sbio->dma_dir == SHANNON_DMA_TODEVICE) {
if (unlikely(check_and_alloc_lpmt(get_shannon_disk_from_ns(ns), sbio, logicb_shift))) {
ret = -EIO;
goto free_sg_list;
}
}
if ((shannon_bio_data_dir(bio) == LINUX_BIO_WRITE)
&& (unlikely(shannon_disk_readonly_ns(ns))
|| (pool_used_logicbs(ns_get_pool(ns)) > pool_available_cap(ns_get_pool(ns))/8
&& !check_sbio_is_overwrited(sbio, ns)))) {
ret = -EIO;
goto free_sg_list;
}
/* Do disk accounting manually. */
sbio->start_time = get_jiffies();
shannon_start_io_acct(get_gendisk_from_ns(ns), sbio->bio);
if (likely(shannon_percpu_wq)) {
sbio->data = ns;
shannon_init_work(&sbio->make_req_work, submit_sbio_task_ns);
shannon_queue_work(shannon_percpu_wq, &sbio->make_req_work);
} else {
ret = shannon_submit_bio_throttling_ns(ns, sbio);
if (ret)
goto end_io_acct;
}
return 0;
end_io_acct:
shannon_end_io_acct(get_gendisk_from_ns(ns), sbio->bio, 0);
free_sg_list:
if (sbio->sg) {
if (sbio->is_valloc)
shannon_sg_vfree(sbio->sg, sbio->sg_count);
else
shannon_sg_free(sbio->sg, sbio->sg_count);
}
free_sbio:
free_sbio(sbio);
shannon_bio_endio(bio, ret);
decrease_ns_pending_bios(ns);
return 0;
}
// make_request
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
static blk_qc_t shannon_make_request_wrapper(struct request_queue *q, struct bio *bio)
{
shannon_make_request(q, bio);
return BLK_QC_T_NONE;
}